aboutsummaryrefslogblamecommitdiffstats
path: root/formats/packfile/parser_test.go
blob: 12d5f0dcabc70348a5fbce76d4165713a767d4e2 (plain) (tree)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412



























































































































































































































































































































































































































                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
package packfile

import (
	"bytes"
	"io"
	"io/ioutil"
	"os"

	. "gopkg.in/check.v1"
	"gopkg.in/src-d/go-git.v3/core"
	"gopkg.in/src-d/go-git.v3/storage/memory"
)

const (
	sigOffset   = 0
	verOffset   = 4
	countOffset = 8
)

type ParserSuite struct {
	fixtures map[string]*fix
}

type fix struct {
	path     string
	parser   *Parser
	seekable io.Seeker
}

func newFix(path string) (*fix, error) {
	fix := new(fix)
	fix.path = path

	f, err := os.Open(path)
	if err != nil {
		return nil, err
	}

	data, err := ioutil.ReadAll(f)
	if err != nil {
		return nil, err
	}

	if err = f.Close(); err != nil {
		return nil, err
	}

	seekable := NewSeekable(bytes.NewReader(data))
	fix.seekable = seekable
	fix.parser = NewParser(seekable)

	return fix, nil
}

func (f *fix) seek(o int64) error {
	_, err := f.seekable.Seek(o, os.SEEK_SET)
	return err
}

var _ = Suite(&ParserSuite{})

func (s *ParserSuite) SetUpSuite(c *C) {
	s.fixtures = make(map[string]*fix)
	for _, fixData := range []struct {
		id   string
		path string
	}{
		{"ofs-deltas", "fixtures/alcortesm-binary-relations.pack"},
		{"ref-deltas", "fixtures/git-fixture.ref-delta"},
	} {
		fix, err := newFix(fixData.path)
		c.Assert(err, IsNil,
			Commentf("setting up fixture id %s: %s", fixData.id, err))

		_, ok := s.fixtures[fixData.id]
		c.Assert(ok, Equals, false,
			Commentf("duplicated fixture id: %s", fixData.id))

		s.fixtures[fixData.id] = fix
	}
}

func (s *ParserSuite) TestSignature(c *C) {
	for id, fix := range s.fixtures {
		com := Commentf("fixture id = %s", id)
		err := fix.seek(sigOffset)
		c.Assert(err, IsNil, com)
		p := fix.parser

		sig, err := p.ReadSignature()
		c.Assert(err, IsNil, com)
		c.Assert(p.IsValidSignature(sig), Equals, true, com)
	}
}

func (s *ParserSuite) TestVersion(c *C) {
	for i, test := range [...]struct {
		fixID    string
		expected uint32
	}{
		{
			fixID:    "ofs-deltas",
			expected: uint32(2),
		}, {
			fixID:    "ref-deltas",
			expected: uint32(2),
		},
	} {
		com := Commentf("test %d) fixture id = %s", i, test.fixID)
		fix, ok := s.fixtures[test.fixID]
		c.Assert(ok, Equals, true, com)

		err := fix.seek(verOffset)
		c.Assert(err, IsNil, com)
		p := fix.parser

		v, err := p.ReadVersion()
		c.Assert(err, IsNil, com)
		c.Assert(v, Equals, test.expected, com)
		c.Assert(p.IsSupportedVersion(v), Equals, true, com)
	}
}

func (s *ParserSuite) TestCount(c *C) {
	for i, test := range [...]struct {
		fixID    string
		expected uint32
	}{
		{
			fixID:    "ofs-deltas",
			expected: uint32(0x50),
		}, {
			fixID:    "ref-deltas",
			expected: uint32(0x1c),
		},
	} {
		com := Commentf("test %d) fixture id = %s", i, test.fixID)
		fix, ok := s.fixtures[test.fixID]
		c.Assert(ok, Equals, true, com)

		err := fix.seek(countOffset)
		c.Assert(err, IsNil, com)
		p := fix.parser

		count, err := p.ReadCount()
		c.Assert(err, IsNil, com)
		c.Assert(count, Equals, test.expected, com)
	}
}

func (s *ParserSuite) TestReadObjectTypeAndLength(c *C) {
	for i, test := range [...]struct {
		fixID     string
		offset    int64
		expType   core.ObjectType
		expLength int64
	}{
		{
			fixID:     "ofs-deltas",
			offset:    12,
			expType:   core.CommitObject,
			expLength: 342,
		}, {
			fixID:     "ofs-deltas",
			offset:    1212,
			expType:   core.OFSDeltaObject,
			expLength: 104,
		}, {
			fixID:     "ofs-deltas",
			offset:    3193,
			expType:   core.TreeObject,
			expLength: 226,
		}, {
			fixID:     "ofs-deltas",
			offset:    3639,
			expType:   core.BlobObject,
			expLength: 90,
		}, {
			fixID:     "ofs-deltas",
			offset:    4504,
			expType:   core.BlobObject,
			expLength: 7107,
		}, {
			fixID:     "ref-deltas",
			offset:    84849,
			expType:   core.REFDeltaObject,
			expLength: 6,
		}, {
			fixID:     "ref-deltas",
			offset:    85070,
			expType:   core.REFDeltaObject,
			expLength: 8,
		},
	} {
		com := Commentf("test %d) fixture id = %s", i, test.fixID)
		fix, ok := s.fixtures[test.fixID]
		c.Assert(ok, Equals, true, com)

		err := fix.seek(test.offset)
		c.Assert(err, IsNil, com)
		p := fix.parser

		typ, length, err := p.ReadObjectTypeAndLength()
		c.Assert(err, IsNil, com)
		c.Assert(typ, Equals, test.expType, com)
		c.Assert(length, Equals, test.expLength, com)
	}
}

func (s *ParserSuite) TestReadNonDeltaObjectContent(c *C) {
	for i, test := range [...]struct {
		fixID    string
		offset   int64
		expected []byte
	}{
		{
			fixID:    "ofs-deltas",
			offset:   12,
			expected: []byte("tree 87c87d16e815a43e4e574dd8edd72c5450ac3a8e\nparent a87d72684d1cf68099ce6e9f68689e25e645a14c\nauthor Gorka Guardiola <Gorka Guardiola Múzquiz> 1450265632 +0100\ncommitter Gorka Guardiola <Gorka Guardiola Múzquiz> 1450265632 +0100\n\nChanged example to use dot.\nI did not remove the original files outside of the\ntex, I leave that to alcortes.\n"),
		}, {
			fixID:    "ofs-deltas",
			offset:   1610,
			expected: []byte("tree 4b4f0d9a07109ef0b8a3051138cc20cdb47fa513\nparent b373f85fa2594d7dcd9989f4a5858a81647fb8ea\nauthor Alberto Cortés <alberto@sourced.tech> 1448017995 +0100\ncommitter Alberto Cortés <alberto@sourced.tech> 1448018112 +0100\n\nMove generated images to it own dir (img/)\n\nFixes #1.\n"),
		}, {
			fixID:    "ofs-deltas",
			offset:   10566,
			expected: []byte("40000 map-slice\x00\x00\xce\xfb\x8ew\xf7\xa8\xc6\x1b\x99\xdd$\x91\xffH\xa3\xb0\xb1fy40000 simple-arrays\x00\x9a7\x81\xb7\xfd\x9d(Q\xe2\xa4H\x8c\x03\x90Z\xecy"),
		},
	} {
		com := Commentf("test %d) fixture id = %s", i, test.fixID)
		fix, ok := s.fixtures[test.fixID]
		c.Assert(ok, Equals, true, com)

		err := fix.seek(test.offset)
		c.Assert(err, IsNil, com)
		p := fix.parser

		_, _, err = p.ReadObjectTypeAndLength()
		c.Assert(err, IsNil, com)

		cont, err := p.ReadNonDeltaObjectContent()
		c.Assert(err, IsNil, com)
		c.Assert(cont, DeepEquals, test.expected, com)
	}
}

func (s *ParserSuite) TestReadOFSDeltaObjectContent(c *C) {
	for i, test := range [...]struct {
		fixID      string
		offset     int64
		expOffset  int64
		expType    core.ObjectType
		expContent []byte
	}{
		{
			fixID:      "ofs-deltas",
			offset:     1212,
			expOffset:  -212,
			expType:    core.CommitObject,
			expContent: []byte("tree c4573589ce78ac63769c20742b9a970f6e274a38\nparent 4571a24948494ebe1cb3dc18ca5a9286e79705ae\nauthor Alberto Cortés <alberto@sourced.tech> 1448139640 +0100\ncommitter Alberto Cortés <alberto@sourced.tech> 1448139640 +0100\n\nUpdate reference to binrels module\n"),
		}, {
			fixID:      "ofs-deltas",
			offset:     3514,
			expOffset:  -102,
			expType:    core.TreeObject,
			expContent: []byte("100644 .gitignore\x00\u007fA\x90[Mw\xabJ\x9a-3O\xcd\x0f\xb5\xdbn\x8e!\x83100644 .gitmodules\x00\xd4`\xa8>\x15\xcfd\x05\x81B7_\xc4\v\x04\xa7\xa9A\x85\n100644 Makefile\x00\x8c\x14\xdef\x12\xed\x15\x816y\xa6UK\xad\x993\v100644 binary-relations.tex\x00\x802\x05@\x11'^ \xf5<\xf7\xfd\x81%3\xd1o\xa9_$40000 graphs\x00\xdehu\x16\xc6\x0e\\H\x8e\xe9\xa1JIXE\xbaڽg\xc540000 imgs-gen\x00\xeb\"\xddhzg\xa3\x1f\xc8j\xc5\xfc\xe9\x96\xce\xce^40000 src\x00\x895\x11t\xff\x86\xa7\xea\xa6\xc0v%\x11E\x10f,ݒ\x1a"),
		}, {
			fixID:      "ofs-deltas",
			offset:     9806,
			expOffset:  -6613,
			expType:    core.TreeObject,
			expContent: []byte("100644 .gitignore\x00\u007fA\x90[Mw\xabJ\x9a-3O\xcd\x0f\xb5\xdbn\x8e!\x83100644 .gitmodules\x00\xd4`\xa8>\x15\xcfd\x05\x81B7_\xc4\v\x04\xa7\xa9A\x85\n100644 Makefile\x00\x8c\x14\xdef\x12\xed\x15\x816y\xa6UK\xad\x993\v100644 binary-relations.tex\x00I\x13~\xb8کEU\x9f\x99#\xc4E.\x9d>\uef1e\xad40000 graphs\x00\xb9\x00\xf34\xde\xff\xce@+\xbd\xf8 9\xb8=\xc1\xb9\x00\x84]40000 imgs-gen\x00\xeb\"\xddhzg\xa3\x1f\xc8j\xc5\xfc\xe9\x96\xce\xce^40000 src\x00\x895\x11t\xff\x86\xa7\xea\xa6\xc0v%\x11E\x10f,ݒ\x1a"),
		},
	} {
		com := Commentf("test %d) fixture id = %s", i, test.fixID)
		fix, ok := s.fixtures[test.fixID]
		c.Assert(ok, Equals, true, com)

		err := fix.seek(test.offset)
		c.Assert(err, IsNil, com)
		p := fix.parser

		_, _, err = p.ReadObjectTypeAndLength()
		c.Assert(err, IsNil, com)

		beforeJumpSize, err := p.Offset()
		c.Assert(err, IsNil, com)

		jump, err := p.ReadNegativeOffset()
		c.Assert(err, IsNil, com)
		c.Assert(jump, Equals, test.expOffset, com)

		err = fix.seek(beforeJumpSize)
		c.Assert(err, IsNil, com)

		cont, typ, err := p.ReadOFSDeltaObjectContent(test.offset)
		c.Assert(err, IsNil, com)
		c.Assert(typ, Equals, test.expType, com)
		c.Assert(cont, DeepEquals, test.expContent, com)
	}
}

func (s *ParserSuite) TestReadREFDeltaObjectContent(c *C) {
	for i, test := range [...]struct {
		fixID      string
		offset     int64
		deps       map[int64]core.Object
		expHash    core.Hash
		expType    core.ObjectType
		expContent []byte
	}{
		{
			fixID:  "ref-deltas",
			offset: 84849,
			deps: map[int64]core.Object{
				83607: newObject(core.TreeObject, []byte("100644 .gitignore\x002\x85\x8a\xad<8>\xd1\xff\n\x0f\x9b\xdf#\x1dT\xa0\f\x9e\x88100644 CHANGELOG\x00\xd3\xffS\xe0VJ\x9f\x87\xd8\xe8Kn(\xe5\x06\x0eQp\b\xaa100644 LICENSE\x00\xc1\x92\xbdj$\xea\x1a\xb0\x1dxhnA|\x8b\xdc|=\x19\u007f100644 binary.jpg\x00\xd5\xc0\xf4\xab\x81\x18\x97\xca\xdf\x03\xae\xc3X\xae`\xd2\x1f\x91\xc5\r40000 go\x00\xa3\x97q\xa7e\x1f\x97\xfa\xf5\xc7.\b\"M\x85\u007f\xc3Q3\xdb40000 json\x00Z\x87~j\x90j'C\xadnEٜ\x17\x93d*\xaf\x8e\xda40000 php\x00Xj\xf5gл^w\x1eI\xbd\xd9CO^\x0f\xb7m%\xfa40000 vendor\x00\xcfJ\xa3\xb3\x89t\xfb}\x81\xf3g\xc0\x83\x0f}x\xd6Z\xb8k")),
			},
			expHash:    core.NewHash("a8d315b2b1c615d43042c3a62402b8a54288cf5c"),
			expType:    core.TreeObject,
			expContent: []byte("100644 .gitignore\x002\x85\x8a\xad<8>\xd1\xff\n\x0f\x9b\xdf#\x1dT\xa0\f\x9e\x88100644 CHANGELOG\x00\xd3\xffS\xe0VJ\x9f\x87\xd8\xe8Kn(\xe5\x06\x0eQp\b\xaa100644 LICENSE\x00\xc1\x92\xbdj$\xea\x1a\xb0\x1dxhnA|\x8b\xdc|=\x19\u007f100644 binary.jpg\x00\xd5\xc0\xf4\xab\x81\x18\x97\xca\xdf\x03\xae\xc3X\xae`\xd2\x1f\x91\xc5\r40000 go\x00\xa3\x97q\xa7e\x1f\x97\xfa\xf5\xc7.\b\"M\x85\u007f\xc3Q3\xdb40000 json\x00Z\x87~j\x90j'C\xadnEٜ\x17\x93d*\xaf\x8e\xda40000 php\x00Xj\xf5gл^w\x1eI\xbd\xd9CO^\x0f\xb7m%\xfa"),
		}, {
			fixID:  "ref-deltas",
			offset: 85070,
			deps: map[int64]core.Object{
				84922: newObject(core.TreeObject, []byte("100644 .gitignore\x002\x85\x8a\xad<8>\xd1\xff\n\x0f\x9b\xdf#\x1dT\xa0\f\x9e\x88100644 CHANGELOG\x00\xd3\xffS\xe0VJ\x9f\x87\xd8\xe8Kn(\xe5\x06\x0eQp\b\xaa100644 LICENSE\x00\xc1\x92\xbdj$\xea\x1a\xb0\x1dxhnA|\x8b\xdc|=\x19\u007f100644 binary.jpg\x00\xd5\xc0\xf4\xab\x81\x18\x97\xca\xdf\x03\xae\xc3X\xae`\xd2\x1f\x91\xc5\r")),
				84849: newObject(core.TreeObject, []byte("100644 .gitignore\x002\x85\x8a\xad<8>\xd1\xff\n\x0f\x9b\xdf#\x1dT\xa0\f\x9e\x88100644 CHANGELOG\x00\xd3\xffS\xe0VJ\x9f\x87\xd8\xe8Kn(\xe5\x06\x0eQp\b\xaa100644 LICENSE\x00\xc1\x92\xbdj$\xea\x1a\xb0\x1dxhnA|\x8b\xdc|=\x19\u007f100644 binary.jpg\x00\xd5\xc0\xf4\xab\x81\x18\x97\xca\xdf\x03\xae\xc3X\xae`\xd2\x1f\x91\xc5\r40000 go\x00\xa3\x97q\xa7e\x1f\x97\xfa\xf5\xc7.\b\"M\x85\u007f\xc3Q3\xdb40000 json\x00Z\x87~j\x90j'C\xadnEٜ\x17\x93d*\xaf\x8e\xda40000 php\x00Xj\xf5gл^w\x1eI\xbd\xd9CO^\x0f\xb7m%\xfa")),
				83607: newObject(core.TreeObject, []byte("100644 .gitignore\x002\x85\x8a\xad<8>\xd1\xff\n\x0f\x9b\xdf#\x1dT\xa0\f\x9e\x88100644 CHANGELOG\x00\xd3\xffS\xe0VJ\x9f\x87\xd8\xe8Kn(\xe5\x06\x0eQp\b\xaa100644 LICENSE\x00\xc1\x92\xbdj$\xea\x1a\xb0\x1dxhnA|\x8b\xdc|=\x19\u007f100644 binary.jpg\x00\xd5\xc0\xf4\xab\x81\x18\x97\xca\xdf\x03\xae\xc3X\xae`\xd2\x1f\x91\xc5\r40000 go\x00\xa3\x97q\xa7e\x1f\x97\xfa\xf5\xc7.\b\"M\x85\u007f\xc3Q3\xdb40000 json\x00Z\x87~j\x90j'C\xadnEٜ\x17\x93d*\xaf\x8e\xda40000 php\x00Xj\xf5gл^w\x1eI\xbd\xd9CO^\x0f\xb7m%\xfa40000 vendor\x00\xcfJ\xa3\xb3\x89t\xfb}\x81\xf3g\xc0\x83\x0f}x\xd6Z\xb8k")),
			},
			expHash:    core.NewHash("eba74343e2f15d62adedfd8c883ee0262b5c8021"),
			expType:    core.TreeObject,
			expContent: []byte("100644 .gitignore\x002\x85\x8a\xad<8>\xd1\xff\n\x0f\x9b\xdf#\x1dT\xa0\f\x9e\x88100644 LICENSE\x00\xc1\x92\xbdj$\xea\x1a\xb0\x1dxhnA|\x8b\xdc|=\x19\u007f100644 binary.jpg\x00\xd5\xc0\xf4\xab\x81\x18\x97\xca\xdf\x03\xae\xc3X\xae`\xd2\x1f\x91\xc5\r"),
		},
	} {
		com := Commentf("test %d) fixture id = %s", i, test.fixID)
		fix, ok := s.fixtures[test.fixID]
		c.Assert(ok, Equals, true, com)

		err := fix.seek(test.offset)
		c.Assert(err, IsNil, com)
		p := fix.parser
		for k, v := range test.deps {
			err = p.Remember(k, v)
			c.Assert(err, IsNil, com)
		}

		_, _, err = p.ReadObjectTypeAndLength()
		c.Assert(err, IsNil, com)

		beforeHash, err := p.Offset()
		c.Assert(err, IsNil, com)

		hash, err := p.ReadHash()
		c.Assert(err, IsNil, com)
		c.Assert(hash, Equals, test.expHash, com)

		err = fix.seek(beforeHash)
		c.Assert(err, IsNil, com)

		cont, typ, err := p.ReadREFDeltaObjectContent()
		c.Assert(err, IsNil, com)
		c.Assert(typ, Equals, test.expType, com)
		c.Assert(cont, DeepEquals, test.expContent, com)

		p.ForgetAll()
	}
}

func newObject(t core.ObjectType, c []byte) *memory.Object {
	return memory.NewObject(t, int64(len(c)), c)
}

func (s *ParserSuite) TestReadHeaderBadSignatureError(c *C) {
	data := []byte{
		0x50, 0x42, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02,
		0x00, 0x00, 0x00, 0x50,
	}
	p := NewParser(NewSeekable(bytes.NewReader(data)))

	_, err := p.ReadHeader()
	c.Assert(err, ErrorMatches, ErrBadSignature.Error())
}

func (s *ParserSuite) TestReadHeaderEmptyPackfileError(c *C) {
	data := []byte{}
	p := NewParser(NewSeekable(bytes.NewReader(data)))

	_, err := p.ReadHeader()
	c.Assert(err, ErrorMatches, ErrEmptyPackfile.Error())
}

func (s *ParserSuite) TestReadHeaderUnsupportedVersionError(c *C) {
	data := []byte{
		0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x01,
		0x00, 0x00, 0x00, 0x50,
	}
	p := NewParser(NewSeekable(bytes.NewReader(data)))

	_, err := p.ReadHeader()
	c.Assert(err, ErrorMatches, ErrUnsupportedVersion.Error()+".*")
}

func (s *ParserSuite) TestReadHeader(c *C) {
	data := []byte{
		0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02,
		0x00, 0x00, 0x00, 0x50,
	}
	p := NewParser(NewSeekable(bytes.NewReader(data)))

	count, err := p.ReadHeader()
	c.Assert(err, IsNil)
	c.Assert(count, Equals, uint32(0x50))
}