Debian Codegraph @

pack/index_v2_test.go

raw
1 package pack
2
3 import (
4 "bytes"
5 "crypto/sha1"
6 "crypto/sha256"
7 "encoding/binary"
8 "hash"
9 "testing"
10
11 "github.com/stretchr/testify/assert"
12 )
13
14 var (
15 V2IndexHeader = []byte{
16 0xff, 0x74, 0x4f, 0x63,
17 0x00, 0x00, 0x00, 0x02,
18 }
19 V2IndexFanout = make([]uint32, indexFanoutEntries)
20
21 V2IndexCRCs = []byte{
22 0x0, 0x0, 0x0, 0x0,
23 0x1, 0x1, 0x1, 0x1,
24 0x2, 0x2, 0x2, 0x2,
25 }
26
27 V2IndexOffsets = []byte{
28 0x00, 0x00, 0x00, 0x01,
29 0x00, 0x00, 0x00, 0x02,
30 0x80, 0x00, 0x00, 0x01, // use the second large offset
31
32 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // filler data
33 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // large offset
34 }
35 )
36
37 func TestIndexV2EntryExact(t *testing.T) {
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)
42
43 assert.NoError(t, err)
44 assert.EqualValues(t, 2, e.PackOffset)
45 }
46 }
47
48 func TestIndexV2EntryExtendedOffset(t *testing.T) {
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)
53
54 assert.NoError(t, err)
55 assert.EqualValues(t, 3, e.PackOffset)
56 }
57 }
58
59 func TestIndexVersionWidthV2(t *testing.T) {
60 for _, algo := range []hash.Hash{sha1.New(), sha256.New()} {
61 v := &V2{hash: algo}
62 assert.EqualValues(t, 8, v.Width())
63 }
64 }
65
66 func newV2Index(hash hash.Hash) *Index {
67 V2IndexFanout[1] = 1
68 V2IndexFanout[2] = 2
69 V2IndexFanout[3] = 3
70
71 for i := 3; i < len(V2IndexFanout); i++ {
72 V2IndexFanout[i] = 3
73 }
74
75 fanout := make([]byte, indexFanoutWidth)
76 for i, n := range V2IndexFanout {
77 binary.BigEndian.PutUint32(fanout[i*indexFanoutEntryWidth:], n)
78 }
79
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)
88 buf = append(buf, V2IndexHeader...)
89 buf = append(buf, fanout...)
90 buf = append(buf, names...)
91 buf = append(buf, V2IndexCRCs...)
92 buf = append(buf, V2IndexOffsets...)
93
94 return &Index{
95 fanout: V2IndexFanout,
96 version: &V2{hash: hash},
97 r: bytes.NewReader(buf),
98 }
99 }