@@ -3,59 +3,37 @@ package pack
33import (
44 "bytes"
55 "crypto/sha1"
6+ "crypto/sha256"
67 "encoding/binary"
8+ "hash"
79 "testing"
810
911 "github.com/stretchr/testify/assert"
1012)
1113
1214var (
1315 V1IndexFanout = make ([]uint32 , indexFanoutEntries )
14-
15- V1IndexSmallEntry = []byte {
16- 0x0 , 0x0 , 0x0 , 0x1 ,
17-
18- 0x1 , 0x1 , 0x1 , 0x1 , 0x1 , 0x1 , 0x1 , 0x1 , 0x1 , 0x1 ,
19- 0x1 , 0x1 , 0x1 , 0x1 , 0x1 , 0x1 , 0x1 , 0x1 , 0x1 , 0x1 ,
20- }
21- V1IndexSmallSha = V1IndexSmallEntry [4 :]
22-
23- V1IndexMediumEntry = []byte {
24- 0x0 , 0x0 , 0x0 , 0x2 ,
25-
26- 0x2 , 0x2 , 0x2 , 0x2 , 0x2 , 0x2 , 0x2 , 0x2 , 0x2 , 0x2 ,
27- 0x2 , 0x2 , 0x2 , 0x2 , 0x2 , 0x2 , 0x2 , 0x2 , 0x2 , 0x2 ,
28- }
29- V1IndexMediumSha = V1IndexMediumEntry [4 :]
30-
31- V1IndexLargeEntry = []byte {
32- 0x0 , 0x0 , 0x0 , 0x3 ,
33-
34- 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 ,
35- 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 ,
36- }
37- V1IndexLargeSha = V1IndexLargeEntry [4 :]
38-
39- V1Index = & Index {
40- fanout : V1IndexFanout ,
41- version : & V1 {hash : sha1 .New ()},
42- }
4316)
4417
4518func TestIndexV1SearchExact (t * testing.T ) {
46- v := & V1 {hash : sha1 .New ()}
47- e , err := v .Entry (V1Index , 1 )
19+ for _ , algo := range []hash.Hash {sha1 .New (), sha256 .New ()} {
20+ index := newV1Index (algo )
21+ v := & V1 {hash : algo }
22+ e , err := v .Entry (index , 1 )
4823
49- assert .NoError (t , err )
50- assert .EqualValues (t , 2 , e .PackOffset )
24+ assert .NoError (t , err )
25+ assert .EqualValues (t , 2 , e .PackOffset )
26+ }
5127}
5228
5329func TestIndexVersionWidthV1 (t * testing.T ) {
54- v := & V1 {hash : sha1 .New ()}
55- assert .EqualValues (t , 0 , v .Width ())
30+ for _ , algo := range []hash.Hash {sha1 .New (), sha256 .New ()} {
31+ v := & V1 {hash : algo }
32+ assert .EqualValues (t , 0 , v .Width ())
33+ }
5634}
5735
58- func init () {
36+ func newV1Index ( hash hash. Hash ) * Index {
5937 V1IndexFanout [1 ] = 1
6038 V1IndexFanout [2 ] = 2
6139 V1IndexFanout [3 ] = 3
@@ -69,12 +47,29 @@ func init() {
6947 binary .BigEndian .PutUint32 (fanout [i * indexFanoutEntryWidth :], n )
7048 }
7149
72- buf := make ([]byte , 0 , indexOffsetV1Start + (3 * indexObjectEntryV1Width ))
50+ hashlen := hash .Size ()
51+ entrylen := hashlen + indexObjectCRCWidth
52+ entries := make ([]byte , entrylen * 3 )
53+
54+ for i := 0 ; i < 3 ; i ++ {
55+ // For each entry, set the first three bytes to 0 and the
56+ // remainder to the same value. That creates an initial 4-byte
57+ // CRC field with the value of i+1, followed by a series of data
58+ // bytes which all have that same value.
59+ for j := entrylen * i + 3 ; j < entrylen * (i + 1 ); j ++ {
60+ entries [j ] = byte (i + 1 )
61+ }
62+ }
63+
64+ buf := make ([]byte , 0 , indexOffsetV1Start )
7365
7466 buf = append (buf , fanout ... )
75- buf = append (buf , V1IndexSmallEntry ... )
76- buf = append (buf , V1IndexMediumEntry ... )
77- buf = append (buf , V1IndexLargeEntry ... )
67+ buf = append (buf , entries ... )
68+
69+ return & Index {
70+ fanout : V1IndexFanout ,
71+ version : & V1 {hash : hash },
72+ r : bytes .NewReader (buf ),
73+ }
7874
79- V1Index .r = bytes .NewReader (buf )
8075}
0 commit comments