MCPcopy
hub / github.com/etcd-io/bbolt / corruptRandomLeafPageInBucket

Function corruptRandomLeafPageInBucket

tx_check_test.go:161–190  ·  view source on GitHub ↗

corruptRandomLeafPage corrupts one random leaf page.

(t testing.TB, db *bbolt.DB, bucketName []byte)

Source from the content-addressed store, hash-verified

159
160// corruptRandomLeafPage corrupts one random leaf page.
161func corruptRandomLeafPageInBucket(t testing.TB, db *bbolt.DB, bucketName []byte) (victimPageId common.Pgid, validPageIds []common.Pgid) {
162 bucketRootPageId := mustGetBucketRootPage(t, db, bucketName)
163 bucketRootPage, _, err := guts_cli.ReadPage(db.Path(), uint64(bucketRootPageId))
164 require.NoError(t, err)
165 require.True(t, bucketRootPage.IsBranchPage())
166
167 // Retrieve all the leaf pages included in the branch page, and pick up random one from them.
168 var bucketPageIds []common.Pgid
169 for _, bpe := range bucketRootPage.BranchPageElements() {
170 bucketPageIds = append(bucketPageIds, bpe.Pgid())
171 }
172 randomIdx := rand.Intn(len(bucketPageIds))
173 victimPageId = bucketPageIds[randomIdx]
174 validPageIds = append(bucketPageIds[:randomIdx], bucketPageIds[randomIdx+1:]...)
175
176 victimPage, victimBuf, err := guts_cli.ReadPage(db.Path(), uint64(victimPageId))
177 require.NoError(t, err)
178 require.True(t, victimPage.IsLeafPage())
179 require.True(t, victimPage.Count() > 1)
180
181 // intentionally make the second key < the first key.
182 element := victimPage.LeafPageElement(1)
183 key := element.Key()
184 key[0] = 0
185
186 // Write the corrupt page to db file.
187 err = guts_cli.WritePage(db.Path(), victimBuf)
188 require.NoError(t, err)
189 return victimPageId, validPageIds
190}
191
192func corruptRootPage(t testing.TB, db *bbolt.DB, bucketName []byte) {
193 bucketRootPageId := mustGetBucketRootPage(t, db, bucketName)

Callers 1

TestTx_Check_CorruptPageFunction · 0.85

Calls 11

ReadPageFunction · 0.92
WritePageFunction · 0.92
mustGetBucketRootPageFunction · 0.85
IsBranchPageMethod · 0.80
BranchPageElementsMethod · 0.80
IsLeafPageMethod · 0.80
LeafPageElementMethod · 0.80
CountMethod · 0.65
PathMethod · 0.45
PgidMethod · 0.45
KeyMethod · 0.45

Tested by

no test coverage detected