-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_test.go
148 lines (131 loc) · 3.87 KB
/
index_test.go
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
// Copyright 2014 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package ggit
import (
"crypto/sha1"
"encoding/binary"
"reflect"
"testing"
"time"
)
type entryTestCase struct {
data []byte
e Entry
length uint32
hasError bool
}
func TestParseEntry(t *testing.T) {
tooShort := make([]byte, 69)
good := entryTestCase{
data: make([]byte, 80),
e: Entry{},
length: 80,
hasError: false}
good.e.Ctime = time.Unix(1, 2)
copy(good.data[0:8], []byte{0, 0, 0, 1, 0, 0, 0, 2})
good.e.Mtime = time.Unix(3, 4)
copy(good.data[8:16], []byte{0, 0, 0, 3, 0, 0, 0, 4})
good.e.Dev = 5
copy(good.data[16:20], []byte{0, 0, 0, 5})
good.e.Ino = 6
copy(good.data[20:24], []byte{0, 0, 0, 6})
// high 4 bits 1000 (regular file), low 9 bits 0644
good.e.Mode = 0x8000<<16 | 0x01a4
binary.BigEndian.PutUint32(good.data[24:28], good.e.Mode)
good.e.Uid = 7
copy(good.data[28:32], []byte{0, 0, 0, 7})
good.e.Gid = 8
copy(good.data[32:36], []byte{0, 0, 0, 8})
good.e.Size = 9
copy(good.data[36:40], []byte{0, 0, 0, 9})
emptyHash := sha1.Sum(make([]byte, 0))
copy(good.e.Hash[:], emptyHash[:])
copy(good.data[40:60], emptyHash[:])
// high bit 1 for assume-valid, rest 0
good.e.Flags = 0x80
copy(good.data[60:62], []byte{0, 0x80})
good.e.Path = []byte("foo/bar/file.txt")
copy(good.data[62:78], []byte(good.e.Path))
// Rest of the slice is NUL, so no need to set terminator or padding.
// Same as good, but the length divides 8 evenly without any padding.
goodLengthDivides8 := entryTestCase{
data: make([]byte, 80),
e: good.e,
length: good.length,
hasError: false}
copy(goodLengthDivides8.data, good.data)
goodLengthDivides8.e.Path = []byte(string(good.e.Path) + "2")
goodLengthDivides8.data[78] = '2'
goodLengthDivides8.length = 80
cases := []entryTestCase{
{tooShort, Entry{}, 0, true},
good, goodLengthDivides8}
for i := range cases {
e, length, err := parseEntry(cases[i].data)
gotError := err != nil
if cases[i].hasError != gotError {
t.Errorf("Expected error %v but instead got %v on case %v",
cases[i].hasError, err, i)
}
if cases[i].length != length {
t.Errorf("Expected length %v but got %v on case %v", cases[i].length,
length, i)
}
if !reflect.DeepEqual(cases[i].e, e) {
t.Errorf("Expected entry %v but got %v on case %v", cases[i].e, e,
i)
}
}
}
type indexHeaderTestCase struct {
data []byte
version, entries uint32
hasError bool
}
func fillSignature(data []byte) {
data[0] = 'D'
data[1] = 'I'
data[2] = 'R'
data[3] = 'C'
}
func fillChecksum(data []byte) {
idx := len(data) - sha1.Size
checksum := sha1.Sum(data[0:idx])
for i := 0; i < sha1.Size; i++ {
data[idx+i] = checksum[i]
}
}
func TestValidIndexHeader(t *testing.T) {
var zeros [12 + sha1.Size]byte
var badChecksum [12 + sha1.Size]byte
fillSignature(badChecksum[:])
var version2 [12 + sha1.Size]byte
fillSignature(version2[:])
binary.BigEndian.PutUint32(version2[4:8], 2)
fillChecksum(version2[:])
var entries4 [12 + sha1.Size]byte
fillSignature(entries4[:])
binary.BigEndian.PutUint32(entries4[8:12], 4)
fillChecksum(entries4[:])
cases := []indexHeaderTestCase{{zeros[:], 0, 0, true},
{badChecksum[:], 0, 0, true},
{version2[:], 2, 0, false},
{entries4[:], 0, 4, false}}
for i := range cases {
version, entries, err := parseIndexFileHeader(cases[i].data)
gotError := err != nil
if cases[i].hasError != gotError {
t.Errorf("Expected error %v did not match on case %v", cases[i].hasError, i)
}
if version != cases[i].version {
t.Errorf("Expected version %v but got %v on case %v", cases[i].version,
version, i)
}
if entries != cases[i].entries {
t.Errorf("Expected entries %v but got %v on case %v", cases[i].entries,
entries, i)
}
}
}