github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/data/block_tree_test.go (about) 1 package data 2 3 import ( 4 "fmt" 5 "github.com/golang/mock/gomock" 6 "github.com/stretchr/testify/require" 7 "reflect" 8 "testing" 9 ) 10 11 func makePaths(childIndices [][]int, 12 mockBlock BlockWithPtrs) [][]ParentBlockAndChildIndex { 13 result := [][]ParentBlockAndChildIndex(nil) 14 for _, indexList := range childIndices { 15 Path := []ParentBlockAndChildIndex(nil) 16 for _, childIndex := range indexList { 17 Path = append(Path, ParentBlockAndChildIndex{ 18 pblock: mockBlock, 19 childIndex: childIndex, 20 }) 21 } 22 result = append(result, Path) 23 } 24 return result 25 } 26 27 func TestCheckForHolesAndTruncate(t *testing.T) { 28 // Make a mock block that believes itself to have four children. 29 mockCtrl := gomock.NewController(t) 30 defer mockCtrl.Finish() 31 mockBlock := NewMockBlockWithPtrs(mockCtrl) 32 mockBlock.EXPECT().NumIndirectPtrs().Return(4).MinTimes(1) 33 34 goodExamples := [][][]int{ 35 {{3, 2}, {3, 3}}, 36 {{2, 3}, {3, 0}}, 37 {{1, 0}, {1, 1}, {1, 2}, {1, 3}, {2, 0}, {2, 1}}, 38 {{1, 2, 3}, {1, 3, 0}, {1, 3, 1}, {1, 3, 2}, {1, 3, 3}, {2, 0, 0}}, 39 } 40 for _, goodEx := range goodExamples { 41 paths := makePaths(goodEx, mockBlock) 42 newPaths := checkForHolesAndTruncate(paths) 43 require.True(t, reflect.DeepEqual(paths, newPaths), 44 fmt.Sprintf("Paths incorrectly truncated from %v to %v", 45 paths, newPaths)) 46 } 47 48 badExamples := [][][]int{ 49 {{3, 3}, {3, 2}}, 50 {{1, 3, 0}, {1, 3, 1}, {1, 3, 2}, {2, 0, 3}}, 51 {{1, 0}, {1, 1}, {1, 2}, {2, 0}, {2, 1}}, 52 } 53 // correctLengths holds the length that each bad example should be truncated 54 // to. 55 correctLengths := []int{1, 3, 3} 56 for idx, badEx := range badExamples { 57 paths := makePaths(badEx, mockBlock) 58 newPaths := checkForHolesAndTruncate(paths) 59 require.Len(t, newPaths, correctLengths[idx]) 60 } 61 }