File: index_v2_test.go

package info (click to toggle)
golang-github-git-lfs-gitobj 2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 432 kB
  • sloc: makefile: 2; sh: 1
file content (99 lines) | stat: -rw-r--r-- 2,123 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package pack

import (
	"bytes"
	"crypto/sha1"
	"crypto/sha256"
	"encoding/binary"
	"hash"
	"testing"

	"github.com/stretchr/testify/assert"
)

var (
	V2IndexHeader = []byte{
		0xff, 0x74, 0x4f, 0x63,
		0x00, 0x00, 0x00, 0x02,
	}
	V2IndexFanout = make([]uint32, indexFanoutEntries)

	V2IndexCRCs = []byte{
		0x0, 0x0, 0x0, 0x0,
		0x1, 0x1, 0x1, 0x1,
		0x2, 0x2, 0x2, 0x2,
	}

	V2IndexOffsets = []byte{
		0x00, 0x00, 0x00, 0x01,
		0x00, 0x00, 0x00, 0x02,
		0x80, 0x00, 0x00, 0x01, // use the second large offset

		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // filler data
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // large offset
	}
)

func TestIndexV2EntryExact(t *testing.T) {
	for _, algo := range []hash.Hash{sha1.New(), sha256.New()} {
		index := newV2Index(algo)
		v := &V2{hash: algo}
		e, err := v.Entry(index, 1)

		assert.NoError(t, err)
		assert.EqualValues(t, 2, e.PackOffset)
	}
}

func TestIndexV2EntryExtendedOffset(t *testing.T) {
	for _, algo := range []hash.Hash{sha1.New(), sha256.New()} {
		index := newV2Index(algo)
		v := &V2{hash: algo}
		e, err := v.Entry(index, 2)

		assert.NoError(t, err)
		assert.EqualValues(t, 3, e.PackOffset)
	}
}

func TestIndexVersionWidthV2(t *testing.T) {
	for _, algo := range []hash.Hash{sha1.New(), sha256.New()} {
		v := &V2{hash: algo}
		assert.EqualValues(t, 8, v.Width())
	}
}

func newV2Index(hash hash.Hash) *Index {
	V2IndexFanout[1] = 1
	V2IndexFanout[2] = 2
	V2IndexFanout[3] = 3

	for i := 3; i < len(V2IndexFanout); i++ {
		V2IndexFanout[i] = 3
	}

	fanout := make([]byte, indexFanoutWidth)
	for i, n := range V2IndexFanout {
		binary.BigEndian.PutUint32(fanout[i*indexFanoutEntryWidth:], n)
	}

	hashlen := hash.Size()
	names := make([]byte, hashlen*3)

	for i := range names {
		names[i] = byte((i / hashlen) + 1)
	}

	buf := make([]byte, 0, indexOffsetV2Start+3)
	buf = append(buf, V2IndexHeader...)
	buf = append(buf, fanout...)
	buf = append(buf, names...)
	buf = append(buf, V2IndexCRCs...)
	buf = append(buf, V2IndexOffsets...)

	return &Index{
		fanout:  V2IndexFanout,
		version: &V2{hash: hash},
		r:       bytes.NewReader(buf),
	}
}