@@ -3,7 +3,9 @@ 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"
1618 }
1719 V2IndexFanout = make ([]uint32 , indexFanoutEntries )
1820
19- V2IndexNames = []byte {
20- 0x1 , 0x1 , 0x1 , 0x1 , 0x1 , 0x1 , 0x1 , 0x1 , 0x1 , 0x1 ,
21- 0x1 , 0x1 , 0x1 , 0x1 , 0x1 , 0x1 , 0x1 , 0x1 , 0x1 , 0x1 ,
22-
23- 0x2 , 0x2 , 0x2 , 0x2 , 0x2 , 0x2 , 0x2 , 0x2 , 0x2 , 0x2 ,
24- 0x2 , 0x2 , 0x2 , 0x2 , 0x2 , 0x2 , 0x2 , 0x2 , 0x2 , 0x2 ,
25-
26- 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 ,
27- 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 ,
28- }
29- V2IndexSmallSha = V2IndexNames [0 :20 ]
30- V2IndexMediumSha = V2IndexNames [20 :40 ]
31- V2IndexLargeSha = V2IndexNames [40 :60 ]
32-
3321 V2IndexCRCs = []byte {
3422 0x0 , 0x0 , 0x0 , 0x0 ,
3523 0x1 , 0x1 , 0x1 , 0x1 ,
@@ -44,35 +32,38 @@ var (
4432 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , // filler data
4533 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x03 , // large offset
4634 }
47-
48- V2Index = & Index {
49- fanout : V2IndexFanout ,
50- version : & V2 {hash : sha1 .New ()},
51- }
5235)
5336
5437func TestIndexV2EntryExact (t * testing.T ) {
55- v := & V2 {hash : sha1 .New ()}
56- e , err := v .Entry (V2Index , 1 )
38+ for _ , algo := range []hash.Hash {sha1 .New (), sha256 .New ()} {
39+ index := newV2Index (algo )
40+ v := & V2 {hash : algo }
41+ e , err := v .Entry (index , 1 )
5742
58- assert .NoError (t , err )
59- assert .EqualValues (t , 2 , e .PackOffset )
43+ assert .NoError (t , err )
44+ assert .EqualValues (t , 2 , e .PackOffset )
45+ }
6046}
6147
6248func TestIndexV2EntryExtendedOffset (t * testing.T ) {
63- v := & V2 {hash : sha1 .New ()}
64- e , err := v .Entry (V2Index , 2 )
49+ for _ , algo := range []hash.Hash {sha1 .New (), sha256 .New ()} {
50+ index := newV2Index (algo )
51+ v := & V2 {hash : algo }
52+ e , err := v .Entry (index , 2 )
6553
66- assert .NoError (t , err )
67- assert .EqualValues (t , 3 , e .PackOffset )
54+ assert .NoError (t , err )
55+ assert .EqualValues (t , 3 , e .PackOffset )
56+ }
6857}
6958
7059func TestIndexVersionWidthV2 (t * testing.T ) {
71- v := & V2 {hash : sha1 .New ()}
72- assert .EqualValues (t , 8 , v .Width ())
60+ for _ , algo := range []hash.Hash {sha1 .New (), sha256 .New ()} {
61+ v := & V2 {hash : algo }
62+ assert .EqualValues (t , 8 , v .Width ())
63+ }
7364}
7465
75- func init () {
66+ func newV2Index ( hash hash. Hash ) * Index {
7667 V2IndexFanout [1 ] = 1
7768 V2IndexFanout [2 ] = 2
7869 V2IndexFanout [3 ] = 3
@@ -86,12 +77,23 @@ func init() {
8677 binary .BigEndian .PutUint32 (fanout [i * indexFanoutEntryWidth :], n )
8778 }
8879
89- buf := make ([]byte , 0 , indexOffsetV2Start + 3 * (indexObjectEntryV2Width )+ indexObjectLargeOffsetWidth )
80+ hashlen := hash .Size ()
81+ names := make ([]byte , hashlen * 3 )
82+
83+ for i := range names {
84+ names [i ] = byte ((i / hashlen ) + 1 )
85+ }
86+
87+ buf := make ([]byte , 0 , indexOffsetV2Start + 3 )
9088 buf = append (buf , V2IndexHeader ... )
9189 buf = append (buf , fanout ... )
92- buf = append (buf , V2IndexNames ... )
90+ buf = append (buf , names ... )
9391 buf = append (buf , V2IndexCRCs ... )
9492 buf = append (buf , V2IndexOffsets ... )
9593
96- V2Index .r = bytes .NewReader (buf )
94+ return & Index {
95+ fanout : V2IndexFanout ,
96+ version : & V2 {hash : hash },
97+ r : bytes .NewReader (buf ),
98+ }
9799}
0 commit comments