File: index_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 (179 lines) | stat: -rw-r--r-- 4,525 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package pack

import (
	"bytes"
	"crypto/sha1"
	"encoding/binary"
	"fmt"
	"testing"

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

var (
	idx *Index
)

func TestIndexEntrySearch(t *testing.T) {
	e, err := idx.Entry([]byte{
		0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
		0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
	})

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

func TestIndexEntrySearchClampLeft(t *testing.T) {
	e, err := idx.Entry([]byte{
		0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
		0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
	})

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

func TestIndexEntrySearchClampRight(t *testing.T) {
	e, err := idx.Entry([]byte{
		0xff, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
		0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
	})

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

func TestIndexSearchOutOfBounds(t *testing.T) {
	e, err := idx.Entry([]byte{
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	})

	assert.True(t, IsNotFound(err), "expected err to be 'not found'")
	assert.Nil(t, e)
}

func TestIndexEntryNotFound(t *testing.T) {
	e, err := idx.Entry([]byte{
		0x1, 0x6, 0x6, 0x6, 0x6, 0x6, 0x6, 0x6, 0x6, 0x6,
		0x6, 0x6, 0x6, 0x6, 0x6, 0x6, 0x6, 0x6, 0x6, 0x6,
	})

	assert.True(t, IsNotFound(err), "expected err to be 'not found'")
	assert.Nil(t, e)
}

func TestIndexCount(t *testing.T) {
	fanout := make([]uint32, 256)
	for i := 0; i < len(fanout); i++ {
		fanout[i] = uint32(i)
	}

	idx := &Index{fanout: fanout}

	assert.EqualValues(t, 255, idx.Count())
}

func TestIndexIsNotFound(t *testing.T) {
	assert.True(t, IsNotFound(errNotFound),
		"expected 'errNotFound' to satisfy 'IsNotFound()'")
}

func TestIndexIsNotFoundForOtherErrors(t *testing.T) {
	assert.False(t, IsNotFound(fmt.Errorf("gitobj/pack: misc")),
		"expected 'err' not to satisfy 'IsNotFound()'")
}

// init generates some fixture data and then constructs an *Index instance using
// it.
func init() {
	// eps is the number of SHA1 names generated under each 0x<t> slot.
	const eps = 5

	hdr := []byte{
		0xff, 0x74, 0x4f, 0x63, // Index file v2+ magic header
		0x00, 0x00, 0x00, 0x02, // 4-byte version indicator
	}

	// Create a fanout table using uint32s (later marshalled using
	// binary.BigEndian).
	//
	// Since we have an even distribution of SHA1s in the generated index,
	// each entry will increase by the number of entries per slot (see: eps
	// above).
	fanout := make([]uint32, indexFanoutEntries)
	for i := 0; i < len(fanout); i++ {
		// Begin the index at (i+1), since the fanout table mandates
		// objects less than the value at index "i".
		fanout[i] = uint32((i + 1) * eps)
	}

	offs := make([]uint32, 0, 256*eps)
	crcs := make([]uint32, 0, 256*eps)

	names := make([][]byte, 0, 256*eps)
	for i := 0; i < 256; i++ {
		// For each name, generate a unique SHA using the prefix "i",
		// and then suffix "j".
		//
		// In other words, when i=1, we will generate:
		//   []byte{0x1 0x0 0x0 0x0 ...}
		//   []byte{0x1 0x1 0x1 0x1 ...}
		//   []byte{0x1 0x2 0x2 0x2 ...}
		//
		// and etc.
		for j := 0; j < eps; j++ {
			var sha [20]byte

			sha[0] = byte(i)
			for r := 1; r < len(sha); r++ {
				sha[r] = byte(j)
			}

			cpy := make([]byte, len(sha))
			copy(cpy, sha[:])

			names = append(names, cpy)
			offs = append(offs, uint32((i*eps)+j))
			crcs = append(crcs, 0)
		}
	}

	// Create a buffer to hold the index contents:
	buf := bytes.NewBuffer(hdr)

	// Write each value in the fanout table using a 32bit network byte-order
	// integer.
	for _, f := range fanout {
		binary.Write(buf, binary.BigEndian, f)
	}
	// Write each SHA1 name to the table next.
	for _, name := range names {
		buf.Write(name)
	}
	// Then write each of the CRC values in network byte-order as a 32bit
	// unsigned integer.
	for _, crc := range crcs {
		binary.Write(buf, binary.BigEndian, crc)
	}
	// Do the same with the offsets.
	for _, off := range offs {
		binary.Write(buf, binary.BigEndian, off)
	}

	idx = &Index{
		fanout: fanout,
		// version is unimportant here, use V2 since it's more common in
		// the wild.
		version: &V2{hash: sha1.New()},

		// *bytes.Buffer does not implement io.ReaderAt, but
		// *bytes.Reader does.
		//
		// Call (*bytes.Buffer).Bytes() to get the data, and then
		// construct a new *bytes.Reader with it to implement
		// io.ReaderAt.
		r: bytes.NewReader(buf.Bytes()),
	}
}