Debian Codegraph @

object_db_test.go

raw
1 package gitobj
2
3 import (
4 "bytes"
5 "compress/zlib"
6 "crypto/sha1"
7 "encoding/hex"
8 "fmt"
9 "io"
10 "io/ioutil"
11 "strings"
12 "testing"
13 "time"
14
15 "github.com/stretchr/testify/assert"
16 "github.com/stretchr/testify/require"
17 )
18
19 const roundTripCommitSha string = `561ed224a6bd39232d902ad8023c0ebe44fbf6c5`
20 const roundTripCommit string = `tree f2ebdf9c967f69d57b370901f9344596ec47e51c
21 parent fe8fbf7de1cd9f08ae642e502bf5de94e523cc08
22 author brian m. carlson <[email protected]> 1543506816 +0000
23 committer brian m. carlson <[email protected]> 1543506816 +0000
24 gpgsig -----BEGIN PGP SIGNATURE-----
25 Version: GnuPG/MacGPG2 v2.2.9 (Darwin)
26
27 iQIGBAABCgAwFiEETbktHYzuflTwZxNFLQybwS+Cs6EFAlwAC4cSHGJrMjIwNEBn
28 aXRodWIuY29tAAoJEC0Mm8EvgrOhiRMN/2rTxkBb5BeQQeq7rPiIW8+29FzuvPeD
29 /DhxlRKwKut9h4qhtxNQszTezxhP4PLOkuMvUax2pGXCQ8cjkSswagmycev+AB4d
30 s0loG4SrEwvH8nAdr6qfNx4ZproRJ8QaEJqyN9SqF7PCWrUAoJKehdgA38WtYFws
31 ON+nIwzDIvgpoNI+DzgWrx16SOTp87xt8RaJOVK9JNZQk8zBh7rR2viS9CWLysmz
32 wOh3j4XI1TZ5IFJfpCxZzUDFgb6K3wpAX6Vux5F1f3cN5MsJn6WUJCmYCvwofeeZ
33 6LMqKgry7EA12l7Tv/JtmMeh+rbT5WLdMIsjascUaHRhpJDNqqHCKMEj1zh3QZNY
34 Hycdcs24JouVAtPwg07f1ncPU3aE624LnNRA9A6Ih6SkkKE4tgMVA5qkObDfwzLE
35 lWyBj2QKySaIdSlU2EcoH3UK33v/ofrRr3+bUkDgxdqeV/RkBVvfpeMwFVSFWseE
36 bCcotryLCZF7vBQU+pKC+EaZxQV9L5+McGzcDYxUmqrhwtR+azRBYFOw+lOT4sYD
37 FxdLFWCtmDhKPX5Ajci2gmyfgCwdIeDhSuOf2iQQGRpE6y7aka4AlaE=
38 =UyqL
39 -----END PGP SIGNATURE-----
40
41 pack/set: ignore packs without indices
42
43 When we look for packs to read, we look for a pack file, and then an
44 index, and fail if either one is missing. When Git looks for packs to
45 read, it looks only for indices and then checks if the pack is present.
46
47 The Git approach handles the case when there is an extra pack that lacks
48 an index, while our approach does not. Consequently, we can get various
49 errors (showing up so far only on Windows) when an index is missing.
50
51 If the index file cannot be read for any reason, simply skip the entire
52 pack altogether and continue on. This leaves us no more or less
53 functional than Git in terms of discovering objects and makes our error
54 handling more robust.
55 `
56
57 func TestDecodeObject(t *testing.T) {
58 testCases := []struct {
59 options []Option
60 sha string
61 }{
62 {
63 []Option{}, "af5626b4a114abcb82d63db7c8082c3c4756e51b",
64 },
65 {
66 []Option{ObjectFormat(ObjectFormatSHA256)}, "7506cbcf4c572be9e06a1fed35ac5b1df8b5a74d26c07f022648e5d95a9f6f2a",
67 },
68 }
69
70 for _, test := range testCases {
71 contents := "Hello, world!\n"
72
73 var buf bytes.Buffer
74
75 zw := zlib.NewWriter(&buf)
76 fmt.Fprintf(zw, "blob 14\x00%s", contents)
77 zw.Close()
78
79 b, err := NewMemoryBackend(map[string]io.ReadWriter{
80 test.sha: &buf,
81 })
82 require.NoError(t, err)
83
84 odb, err := FromBackend(b, test.options...)
85 require.NoError(t, err)
86
87 shaHex, _ := hex.DecodeString(test.sha)
88 obj, err := odb.Object(shaHex)
89 blob, ok := obj.(*Blob)
90
91 require.NoError(t, err)
92 require.True(t, ok)
93
94 got, err := ioutil.ReadAll(blob.Contents)
95 assert.Nil(t, err)
96 assert.Equal(t, contents, string(got))
97 }
98 }
99
100 func TestDecodeBlob(t *testing.T) {
101 testCases := []struct {
102 options []Option
103 sha string
104 }{
105 {
106 []Option{}, "af5626b4a114abcb82d63db7c8082c3c4756e51b",
107 },
108 {
109 []Option{ObjectFormat(ObjectFormatSHA256)}, "7506cbcf4c572be9e06a1fed35ac5b1df8b5a74d26c07f022648e5d95a9f6f2a",
110 },
111 }
112
113 for _, test := range testCases {
114 contents := "Hello, world!\n"
115
116 var buf bytes.Buffer
117
118 zw := zlib.NewWriter(&buf)
119 fmt.Fprintf(zw, "blob 14\x00%s", contents)
120 zw.Close()
121
122 b, err := NewMemoryBackend(map[string]io.ReadWriter{
123 test.sha: &buf,
124 })
125 require.NoError(t, err)
126
127 odb, err := FromBackend(b, test.options...)
128 require.NoError(t, err)
129
130 shaHex, _ := hex.DecodeString(test.sha)
131 blob, err := odb.Blob(shaHex)
132
133 assert.Nil(t, err)
134 assert.EqualValues(t, 14, blob.Size)
135
136 got, err := ioutil.ReadAll(blob.Contents)
137 assert.Nil(t, err)
138 assert.Equal(t, contents, string(got))
139 }
140 }
141
142 func TestDecodeTree(t *testing.T) {
143 testCases := []struct {
144 options []Option
145 size int64
146 treeSha string
147 blobSha string
148 }{
149 {
150 []Option{},
151 37,
152 "fcb545d5746547a597811b7441ed8eba307be1ff",
153 "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391",
154 },
155 {
156 []Option{ObjectFormat(ObjectFormatSHA256)},
157 49,
158 "eeea12da3c10b7ff20f96530ca613674f0b3292cb524c1b317b80e045adde0b6",
159 "473a0f4c3be8a93681a267e3b1e9a7dcda1185436fe141f7749120a303721813",
160 },
161 }
162
163 for _, test := range testCases {
164 hexSha, err := hex.DecodeString(test.treeSha)
165 require.Nil(t, err)
166
167 hexBlobSha, err := hex.DecodeString(test.blobSha)
168 require.Nil(t, err)
169
170 var buf bytes.Buffer
171
172 zw := zlib.NewWriter(&buf)
173 fmt.Fprintf(zw, "tree %d\x00", test.size)
174 fmt.Fprintf(zw, "100644 hello.txt\x00")
175 zw.Write(hexBlobSha)
176 zw.Close()
177
178 b, err := NewMemoryBackend(map[string]io.ReadWriter{
179 test.treeSha: &buf,
180 })
181 require.NoError(t, err)
182
183 odb, err := FromBackend(b, test.options...)
184 require.NoError(t, err)
185
186 tree, err := odb.Tree(hexSha)
187
188 assert.Nil(t, err)
189 require.Equal(t, 1, len(tree.Entries))
190 assert.Equal(t, &TreeEntry{
191 Name: "hello.txt",
192 Oid: hexBlobSha,
193 Filemode: 0100644,
194 }, tree.Entries[0])
195 }
196 }
197
198 func TestDecodeCommit(t *testing.T) {
199 testCases := []struct {
200 options []Option
201 size int64
202 treeSha string
203 commitSha string
204 }{
205 {
206 []Option{},
207 173,
208 "fcb545d5746547a597811b7441ed8eba307be1ff",
209 "d7283480bb6dc90be621252e1001a93871dcf511",
210 },
211 {
212 []Option{ObjectFormat(ObjectFormatSHA256)},
213 197,
214 "eeea12da3c10b7ff20f96530ca613674f0b3292cb524c1b317b80e045adde0b6",
215 "9b03a791a98a2c35621ea6870061fb17299b22e2bb5e9f6a7d5afd7dc0c23915",
216 },
217 }
218
219 for _, test := range testCases {
220 commitShaHex, err := hex.DecodeString(test.commitSha)
221 assert.Nil(t, err)
222
223 var buf bytes.Buffer
224
225 zw := zlib.NewWriter(&buf)
226 fmt.Fprintf(zw, "commit %d\x00", test.size)
227 fmt.Fprintf(zw, "tree %s\n", test.treeSha)
228 fmt.Fprintf(zw, "author Taylor Blau <[email protected]> 1494620424 -0600\n")
229 fmt.Fprintf(zw, "committer Taylor Blau <[email protected]> 1494620424 -0600\n")
230 fmt.Fprintf(zw, "\ninitial commit\n")
231 zw.Close()
232
233 b, err := NewMemoryBackend(map[string]io.ReadWriter{
234 test.commitSha: &buf,
235 })
236 require.NoError(t, err)
237
238 odb, err := FromBackend(b, test.options...)
239 require.NoError(t, err)
240
241 commit, err := odb.Commit(commitShaHex)
242
243 assert.Nil(t, err)
244 assert.Equal(t, "Taylor Blau <[email protected]> 1494620424 -0600", commit.Author)
245 assert.Equal(t, "Taylor Blau <[email protected]> 1494620424 -0600", commit.Committer)
246 assert.Equal(t, "initial commit", commit.Message)
247 assert.Equal(t, 0, len(commit.ParentIDs))
248 assert.Equal(t, test.treeSha, hex.EncodeToString(commit.TreeID))
249 }
250 }
251
252 func TestWriteBlob(t *testing.T) {
253 testCases := []struct {
254 options []Option
255 sha string
256 }{
257 {
258 []Option{}, "af5626b4a114abcb82d63db7c8082c3c4756e51b",
259 },
260 {
261 []Option{ObjectFormat(ObjectFormatSHA256)}, "7506cbcf4c572be9e06a1fed35ac5b1df8b5a74d26c07f022648e5d95a9f6f2a",
262 },
263 }
264
265 for _, test := range testCases {
266 b, err := NewMemoryBackend(nil)
267 require.NoError(t, err)
268
269 odb, err := FromBackend(b, test.options...)
270 require.NoError(t, err)
271
272 sha, err := odb.WriteBlob(&Blob{
273 Size: 14,
274 Contents: strings.NewReader("Hello, world!\n"),
275 })
276
277 _, s := b.Storage()
278
279 assert.Nil(t, err)
280 assert.Equal(t, test.sha, hex.EncodeToString(sha))
281 assert.NotNil(t, s.(*memoryStorer).fs[hex.EncodeToString(sha)])
282 }
283 }
284
285 func TestWriteTree(t *testing.T) {
286 testCases := []struct {
287 options []Option
288 treeSha string
289 blobSha string
290 }{
291 {
292 []Option{},
293 "fcb545d5746547a597811b7441ed8eba307be1ff",
294 "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391",
295 },
296 {
297 []Option{ObjectFormat(ObjectFormatSHA256)},
298 "eeea12da3c10b7ff20f96530ca613674f0b3292cb524c1b317b80e045adde0b6",
299 "473a0f4c3be8a93681a267e3b1e9a7dcda1185436fe141f7749120a303721813",
300 },
301 }
302
303 for _, test := range testCases {
304 b, err := NewMemoryBackend(nil)
305 require.NoError(t, err)
306
307 odb, err := FromBackend(b, test.options...)
308 require.NoError(t, err)
309
310 hexBlobSha, err := hex.DecodeString(test.blobSha)
311 require.Nil(t, err)
312
313 sha, err := odb.WriteTree(&Tree{Entries: []*TreeEntry{
314 {
315 Name: "hello.txt",
316 Oid: hexBlobSha,
317 Filemode: 0100644,
318 },
319 }})
320
321 _, s := b.Storage()
322
323 assert.Nil(t, err)
324 assert.Equal(t, test.treeSha, hex.EncodeToString(sha))
325 assert.NotNil(t, s.(*memoryStorer).fs[hex.EncodeToString(sha)])
326 }
327 }
328
329 func TestWriteCommit(t *testing.T) {
330 testCases := []struct {
331 options []Option
332 treeSha string
333 commitSha string
334 }{
335 {
336 []Option{},
337 "fcb545d5746547a597811b7441ed8eba307be1ff",
338 "fee8a35c2890cd6e0e28d24cc457fcecbd460962",
339 },
340 {
341 []Option{ObjectFormat(ObjectFormatSHA256)},
342 "eeea12da3c10b7ff20f96530ca613674f0b3292cb524c1b317b80e045adde0b6",
343 "fcafabe9e00f4e1375b2ba688edf30b96afe7a20c6176fefbc3f371b298f69d6",
344 },
345 }
346
347 for _, test := range testCases {
348 b, err := NewMemoryBackend(nil)
349 require.NoError(t, err)
350
351 odb, err := FromBackend(b, test.options...)
352 require.NoError(t, err)
353
354 when := time.Unix(1257894000, 0).UTC()
355 author := &Signature{Name: "John Doe", Email: "[email protected]", When: when}
356 committer := &Signature{Name: "Jane Doe", Email: "[email protected]", When: when}
357
358 treeHex, err := hex.DecodeString(test.treeSha)
359 assert.Nil(t, err)
360
361 sha, err := odb.WriteCommit(&Commit{
362 Author: author.String(),
363 Committer: committer.String(),
364 TreeID: treeHex,
365 Message: "initial commit",
366 })
367
368 _, s := b.Storage()
369
370 assert.Nil(t, err)
371 assert.Equal(t, test.commitSha, hex.EncodeToString(sha))
372 assert.NotNil(t, s.(*memoryStorer).fs[hex.EncodeToString(sha)])
373 }
374 }
375
376 func TestWriteCommitWithGPGSignature(t *testing.T) {
377 b, err := NewMemoryBackend(nil)
378 require.NoError(t, err)
379
380 odb, err := FromBackend(b)
381 require.NoError(t, err)
382
383 commit := new(Commit)
384 _, err = commit.Decode(
385 sha1.New(),
386 strings.NewReader(roundTripCommit), int64(len(roundTripCommit)))
387 require.NoError(t, err)
388
389 buf := new(bytes.Buffer)
390 commit.Encode(buf)
391 assert.Equal(t, roundTripCommit, buf.String())
392
393 sha, err := odb.WriteCommit(commit)
394
395 assert.Nil(t, err)
396 assert.Equal(t, roundTripCommitSha, hex.EncodeToString(sha))
397 }
398
399 func TestDecodeTag(t *testing.T) {
400 const sha = "7639ba293cd2c457070e8446ecdea56682af0f48"
401 tagShaHex, err := hex.DecodeString(sha)
402
403 var buf bytes.Buffer
404
405 zw := zlib.NewWriter(&buf)
406 fmt.Fprintf(zw, "tag 165\x00")
407 fmt.Fprintf(zw, "object 6161616161616161616161616161616161616161\n")
408 fmt.Fprintf(zw, "type commit\n")
409 fmt.Fprintf(zw, "tag v2.4.0\n")
410 fmt.Fprintf(zw, "tagger A U Thor <[email protected]>\n")
411 fmt.Fprintf(zw, "\n")
412 fmt.Fprintf(zw, "The quick brown fox jumps over the lazy dog.\n")
413 zw.Close()
414
415 b, err := NewMemoryBackend(map[string]io.ReadWriter{
416 sha: &buf,
417 })
418 require.NoError(t, err)
419
420 odb, err := FromBackend(b)
421 require.NoError(t, err)
422
423 tag, err := odb.Tag(tagShaHex)
424
425 assert.Nil(t, err)
426
427 assert.Equal(t, []byte("aaaaaaaaaaaaaaaaaaaa"), tag.Object)
428 assert.Equal(t, CommitObjectType, tag.ObjectType)
429 assert.Equal(t, "v2.4.0", tag.Name)
430 assert.Equal(t, "A U Thor <[email protected]>", tag.Tagger)
431 assert.Equal(t, "The quick brown fox jumps over the lazy dog.", tag.Message)
432 }
433
434 func TestWriteTag(t *testing.T) {
435 testCases := []struct {
436 options []Option
437 tagSha string
438 commitSha []byte
439 }{
440 {
441 []Option{},
442 "e614dda21829f4176d3db27fe62fb4aee2e2475d",
443 []byte("aaaaaaaaaaaaaaaaaaaa"),
444 },
445 {
446 []Option{ObjectFormat(ObjectFormatSHA256)},
447 "a297d8b92e8be21fbe1c96a64acc596f26c8b204eb291c71e371c832d3584651",
448 []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
449 },
450 }
451
452 for _, test := range testCases {
453 b, err := NewMemoryBackend(nil)
454 require.NoError(t, err)
455
456 odb, err := FromBackend(b, test.options...)
457 require.NoError(t, err)
458
459 sha, err := odb.WriteTag(&Tag{
460 Object: test.commitSha,
461 ObjectType: CommitObjectType,
462 Name: "v2.4.0",
463 Tagger: "A U Thor <[email protected]>",
464
465 Message: "The quick brown fox jumps over the lazy dog.",
466 })
467
468 _, s := b.Storage()
469
470 assert.Nil(t, err)
471 assert.Equal(t, test.tagSha, hex.EncodeToString(sha))
472 assert.NotNil(t, s.(*memoryStorer).fs[hex.EncodeToString(sha)])
473 }
474 }
475
476 func TestReadingAMissingObjectAfterClose(t *testing.T) {
477 sha, _ := hex.DecodeString("af5626b4a114abcb82d63db7c8082c3c4756e51b")
478
479 b, err := NewMemoryBackend(nil)
480 require.NoError(t, err)
481
482 ro, rw := b.Storage()
483
484 db := &ObjectDatabase{
485 ro: ro,
486 rw: rw,
487 closed: 1,
488 }
489
490 blob, err := db.Blob(sha)
491 assert.EqualError(t, err, "gitobj: cannot use closed *pack.Set")
492 assert.Nil(t, blob)
493 }
494
495 func TestClosingAnObjectDatabaseMoreThanOnce(t *testing.T) {
496 db, err := FromFilesystem("/tmp", "")
497 assert.Nil(t, err)
498
499 assert.Nil(t, db.Close())
500 assert.EqualError(t, db.Close(), "gitobj: *ObjectDatabase already closed")
501 }
502
503 func TestObjectDatabaseRootWithRoot(t *testing.T) {
504 db, err := FromFilesystem("/foo/bar/baz", "")
505 assert.Nil(t, err)
506
507 root, ok := db.Root()
508 assert.Equal(t, "/foo/bar/baz", root)
509 assert.True(t, ok)
510 }
511
512 func TestObjectDatabaseRootWithoutRoot(t *testing.T) {
513 root, ok := new(ObjectDatabase).Root()
514
515 assert.Equal(t, "", root)
516 assert.False(t, ok)
517 }