Debian Codegraph @

pack/index_v1_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 V1IndexFanout = make([]uint32, indexFanoutEntries)
16 )
17
18 func TestIndexV1SearchExact(t *testing.T) {
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)
23
24 assert.NoError(t, err)
25 assert.EqualValues(t, 2, e.PackOffset)
26 }
27 }
28
29 func TestIndexVersionWidthV1(t *testing.T) {
30 for _, algo := range []hash.Hash{sha1.New(), sha256.New()} {
31 v := &V1{hash: algo}
32 assert.EqualValues(t, 0, v.Width())
33 }
34 }
35
36 func newV1Index(hash hash.Hash) *Index {
37 V1IndexFanout[1] = 1
38 V1IndexFanout[2] = 2
39 V1IndexFanout[3] = 3
40
41 for i := 3; i < len(V1IndexFanout); i++ {
42 V1IndexFanout[i] = 3
43 }
44
45 fanout := make([]byte, indexFanoutWidth)
46 for i, n := range V1IndexFanout {
47 binary.BigEndian.PutUint32(fanout[i*indexFanoutEntryWidth:], n)
48 }
49
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)
65
66 buf = append(buf, fanout...)
67 buf = append(buf, entries...)
68
69 return &Index{
70 fanout: V1IndexFanout,
71 version: &V1{hash: hash},
72 r: bytes.NewReader(buf),
73 }
74
75 }