-
Notifications
You must be signed in to change notification settings - Fork 0
/
sum_test.go
312 lines (296 loc) · 11 KB
/
sum_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
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
package xsum_test
import (
"bytes"
"errors"
"hash"
"os"
"path/filepath"
"syscall"
"testing"
"golang.org/x/sync/semaphore"
"github.com/sclevine/xsum"
)
func TestSum_EachList(t *testing.T) {
sum := &xsum.Sum{Semaphore: semaphore.NewWeighted(2)}
h := xsum.NewHashFunc("test", newDummyHash)
mask := []byte{0x00, 0x8f, 0x28, 0x00, 0x40} // 0100
tests := []struct {
name string
files []xsum.File
res []sumResult
}{
{
"files",
[]xsum.File{
{Hash: h, Path: "testdata/testdir", Mask: xsum.NewMask(0100, xsum.AttrEmpty)},
{Hash: h, Path: "testdata/testdir", Mask: xsum.NewMask(0100, xsum.AttrNoData)},
{Hash: h, Path: "testdata/testdir", Mask: xsum.NewMask(0100, xsum.AttrNoName)},
{Hash: h, Path: "testdata/testdir", Mask: xsum.NewMask(0100, xsum.AttrFollow)},
{Hash: h, Path: "testdata/testdir/testfile", Mask: xsum.NewMask(0000, xsum.AttrEmpty)},
},
[]sumResult{
{
sum: bl('{', 0x30, 0x7c,
0x0a, 0x01, 0xff, // hashtype
0x31, 0x77, // set
0x30, 0x39, // testfilelink
0x04, 0x29, '{', 0x30, 0x25, 0xa0, 0x11, 0x30, 0x0f, 0x0a, 0x01, -1, 0x04, 0x0a, '{', "testfile", '}', 0xa1, 0x10, 0x30, 0x0e, 0x03, 0x05, mask, 0x03, 0x05, []byte{0x00, 0x08, 0x00, 0x00, 0x40}, '}',
0x04, 0x0c, "testfilelink",
0x30, 0x3a, // testfile
0x04, 0x2e, '{', 0x30, 0x2a, 0xa0, 0x16, 0x30, 0x14, 0x0a, 0x01, -1, 0x04, 0x0f, '{', "testcontents", 0x0a, '}', 0xa1, 0x10, 0x30, 0x0e, 0x03, 0x05, mask, 0x03, 0x05, []byte{0x00, 0x00, 0x00, 0x00, 0x00}, '}',
0x04, 0x08, "testfile",
'}'),
},
{
sum: bl('{', 0x30, 0x51,
0x0a, 0x01, 0xff, // hashtype
0x31, 0x4c, // set
0x30, 0x22, // testfile
0x04, 0x16, '{', 0x30, 0x12, 0xa1, 0x10, 0x30, 0x0e, 0x03, 0x05, mask, 0x03, 0x05, []byte{0x00, 0x00, 0x00, 0x00, 0x00}, '}',
0x04, 0x08, "testfile",
0x30, 0x26, // testfilelink
0x04, 0x16, '{', 0x30, 0x12, 0xa1, 0x10, 0x30, 0x0e, 0x03, 0x05, mask, 0x03, 0x05, []byte{0x00, 0x08, 0x00, 0x00, 0x40}, '}',
0x04, 0x0c, "testfilelink",
'}'),
},
{
sum: bl('{', 0x30, 0x64,
0x0a, 0x01, 0xff, // hashtype
0x31, 0x5f, // set
0x30, 0x2b, // testfilelink
0x04, 0x29, '{', 0x30, 0x25, 0xa0, 0x11, 0x30, 0x0f, 0x0a, 0x01, -1, 0x04, 0x0a, '{', "testfile", '}', 0xa1, 0x10, 0x30, 0x0e, 0x03, 0x05, mask, 0x03, 0x05, []byte{0x00, 0x08, 0x00, 0x00, 0x40}, '}',
0x30, 0x30, // testfile
0x04, 0x2e, '{', 0x30, 0x2a, 0xa0, 0x16, 0x30, 0x14, 0x0a, 0x01, -1, 0x04, 0x0f, '{', "testcontents", 0x0a, '}', 0xa1, 0x10, 0x30, 0x0e, 0x03, 0x05, mask, 0x03, 0x05, []byte{0x00, 0x00, 0x00, 0x00, 0x00}, '}',
'}'),
},
{
sum: bl('{', 0x30, 0x81, 0x81,
0x0a, 0x01, 0xff, // hashtype
0x31, 0x7c, // set
0x30, 0x3a, // testfile
0x04, 0x2e, '{', 0x30, 0x2a, 0xa0, 0x16, 0x30, 0x14, 0x0a, 0x01, -1, 0x04, 0x0f, '{', "testcontents", 0x0a, '}', 0xa1, 0x10, 0x30, 0x0e, 0x03, 0x05, mask, 0x03, 0x05, []byte{0x00, 0x00, 0x00, 0x00, 0x00}, '}',
0x04, 0x08, "testfile",
0x30, 0x3e, // testfilelink
0x04, 0x2e, '{', 0x30, 0x2a, 0xa0, 0x16, 0x30, 0x14, 0x0a, 0x01, -1, 0x04, 0x0f, '{', "testcontents", 0x0a, '}', 0xa1, 0x10, 0x30, 0x0e, 0x03, 0x05, mask, 0x03, 0x05, []byte{0x00, 0x00, 0x00, 0x00, 0x00}, '}',
0x04, 0x0c, "testfilelink",
'}'),
},
{sum: bl('{', "testcontents", 0x0a, '}')},
},
},
{
"links",
[]xsum.File{
{Hash: h, Path: "testdata/testdir/testfilelink", Mask: xsum.NewMask(0100, xsum.AttrEmpty)},
{Hash: h, Path: "testdata/testdir/testfilelink", Mask: xsum.NewMask(0100, xsum.AttrInclusive)},
{Hash: h, Path: "testdata/testdir/testfilelink", Mask: xsum.NewMask(0100, xsum.AttrFollow)},
{Hash: h, Path: "testdata/testdir/testfilelink", Mask: xsum.NewMask(0100, xsum.AttrFollow|xsum.AttrInclusive)},
{Hash: h, Path: "testdata/testdirlink", Mask: xsum.NewMask(0100, xsum.AttrEmpty)},
{Hash: h, Path: "testdata/testdirlink", Mask: xsum.NewMask(0100, xsum.AttrInclusive)},
{Hash: h, Path: "testdata/testdirlink", Mask: xsum.NewMask(0100, xsum.AttrFollow)},
{Hash: h, Path: "testdata/testdirlink", Mask: xsum.NewMask(0100, xsum.AttrFollow|xsum.AttrInclusive)},
{Hash: h, Path: "testdata/brokenlink", Mask: xsum.NewMask(0100, xsum.AttrEmpty)},
{Hash: h, Path: "testdata/brokenlink", Mask: xsum.NewMask(0100, xsum.AttrInclusive)},
{Hash: h, Path: "testdata/brokenlink", Mask: xsum.NewMask(0100, xsum.AttrFollow)},
{Hash: h, Path: "testdata/brokenlink", Mask: xsum.NewMask(0100, xsum.AttrFollow|xsum.AttrInclusive)},
},
[]sumResult{
{
sum: bl('{', "testcontents", 0x0a, '}'),
},
{
sum: bl('{', 0x30, 0x25,
0xa0, 0x11, 0x30, 0x0f, 0x0a, 0x01, 0xff, 0x04, 0x0a, '{', "testfile", '}',
0xa1, 0x10, 0x30, 0x0e, 0x03, 0x05, 0x00, 0x8f, 0x28, 0x00, 0x40, 0x03, 0x05, 0x00, 0x08, 0x00, 0x00, 0x40,
'}'),
lstat: true,
},
{
sum: bl('{', "testcontents", 0x0a, '}'),
},
{
sum: bl('{', 0x30, 0x2a,
0xa0, 0x16, 0x30, 0x14, 0x0a, 0x01, 0xff, 0x04, 0x0f, '{', "testcontents", 0x0a, '}',
0xa1, 0x10, 0x30, 0x0e, 0x03, 0x05, 0x00, 0x8f, 0x28, 0x00, 0x40, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00,
'}'),
},
{
sum: bl('{', 0x30, 0x7c,
0x0a, 0x01, 0xff, // hashtype
0x31, 0x77, // set
0x30, 0x39, // testfilelink
0x04, 0x29, '{', 0x30, 0x25, 0xa0, 0x11, 0x30, 0x0f, 0x0a, 0x01, -1, 0x04, 0x0a, '{', "testfile", '}', 0xa1, 0x10, 0x30, 0x0e, 0x03, 0x05, mask, 0x03, 0x05, []byte{0x00, 0x08, 0x00, 0x00, 0x40}, '}',
0x04, 0x0c, "testfilelink",
0x30, 0x3a, // testfile
0x04, 0x2e, '{', 0x30, 0x2a, 0xa0, 0x16, 0x30, 0x14, 0x0a, 0x01, -1, 0x04, 0x0f, '{', "testcontents", 0x0a, '}', 0xa1, 0x10, 0x30, 0x0e, 0x03, 0x05, mask, 0x03, 0x05, []byte{0x00, 0x00, 0x00, 0x00, 0x00}, '}',
0x04, 0x08, "testfile",
'}'),
},
{
sum: bl('{', 0x30, 0x24,
0xa0, 0x10, 0x30, 0x0e, 0x0a, 0x01, 0xff, 0x04, 0x09, '{', "testdir", '}',
0xa1, 0x10, 0x30, 0x0e, 0x03, 0x05, 0x00, 0x8f, 0x28, 0x00, 0x40, 0x03, 0x05, 0x00, 0x08, 0x00, 0x00, 0x40,
'}'),
lstat: true,
},
{
sum: bl('{', 0x30, 0x81, 0x81,
0x0a, 0x01, 0xff, // hashtype
0x31, 0x7c, // set
0x30, 0x3a, // testfile
0x04, 0x2e, '{', 0x30, 0x2a, 0xa0, 0x16, 0x30, 0x14, 0x0a, 0x01, -1, 0x04, 0x0f, '{', "testcontents", 0x0a, '}', 0xa1, 0x10, 0x30, 0x0e, 0x03, 0x05, mask, 0x03, 0x05, []byte{0x00, 0x00, 0x00, 0x00, 0x00}, '}',
0x04, 0x08, "testfile",
0x30, 0x3e, // testfilelink
0x04, 0x2e, '{', 0x30, 0x2a, 0xa0, 0x16, 0x30, 0x14, 0x0a, 0x01, -1, 0x04, 0x0f, '{', "testcontents", 0x0a, '}', 0xa1, 0x10, 0x30, 0x0e, 0x03, 0x05, mask, 0x03, 0x05, []byte{0x00, 0x00, 0x00, 0x00, 0x00}, '}',
0x04, 0x0c, "testfilelink",
'}'),
},
{
sum: bl('{', 0x30, 0x81, 0xa4,
0xa0, 0x81, 0x8f, 0x30, 0x81, 0x8c, 0x0a, 0x01, 0xff, 0x04, 0x81, 0x86,
'{', 0x30, 0x81, 0x81,
0x0a, 0x01, 0xff, // hashtype
0x31, 0x7c, // set
0x30, 0x3a, // testfile
0x04, 0x2e, '{', 0x30, 0x2a, 0xa0, 0x16, 0x30, 0x14, 0x0a, 0x01, -1, 0x04, 0x0f, '{', "testcontents", 0x0a, '}', 0xa1, 0x10, 0x30, 0x0e, 0x03, 0x05, mask, 0x03, 0x05, []byte{0x00, 0x00, 0x00, 0x00, 0x00}, '}',
0x04, 0x08, "testfile",
0x30, 0x3e, // testfilelink
0x04, 0x2e, '{', 0x30, 0x2a, 0xa0, 0x16, 0x30, 0x14, 0x0a, 0x01, -1, 0x04, 0x0f, '{', "testcontents", 0x0a, '}', 0xa1, 0x10, 0x30, 0x0e, 0x03, 0x05, mask, 0x03, 0x05, []byte{0x00, 0x00, 0x00, 0x00, 0x00}, '}',
0x04, 0x0c, "testfilelink",
'}',
0xa1, 0x10, 0x30, 0x0e, 0x03, 0x05, 0x00, 0x8f, 0x28, 0x00, 0x40, 0x03, 0x05, 0x00, 0x80, 0x00, 0x00, 0x40,
'}'),
},
{
err: errors.New("testdata/brokenlink: no such file or directory"),
},
{
sum: bl('{', 0x30, 0x24,
0xa0, 0x10, 0x30, 0x0e, 0x0a, 0x01, 0xff, 0x04, 0x09, '{', "invalid", '}',
0xa1, 0x10, 0x30, 0x0e, 0x03, 0x05, 0x00, 0x8f, 0x28, 0x00, 0x40, 0x03, 0x05, 0x00, 0x08, 0x00, 0x00, 0x40,
'}'),
lstat: true,
},
{
err: errors.New("testdata/brokenlink: no such file or directory"),
},
{
err: errors.New("testdata/brokenlink: no such file or directory"),
},
},
},
}
for _, tt := range tests {
var nodes []*xsum.Node
if err := sum.EachList(tt.files, func(node *xsum.Node) error {
nodes = append(nodes, node)
return nil
}); err != nil {
t.Errorf("xsum.EachList(`%s') error: %s", tt.name, err)
continue
}
for i, node := range nodes {
if node.File != tt.files[i] {
t.Errorf("xsum.EachList(`%s#%d') Node file: %+v != %+v (expected)", tt.name, i, node.File, tt.files[i])
}
if nErr, rErr := node.Err, tt.res[i].err; (nErr != nil) != (rErr != nil) ||
(rErr != nil && nErr.Error() != rErr.Error()) {
t.Errorf("xsum.EachList(`%s#%d') error: %s", tt.name, i, node.Err)
continue
} else if rErr != nil {
continue
}
if !bytes.Equal(node.Sum, tt.res[i].sum) {
t.Errorf("xsum.EachList(`%s#%d') Node sum:\n% x\n!=\n% x\n(expected)", tt.name, i, node.Sum, tt.res[i].sum)
}
if tt.res[i].lstat {
if m := lstatFileMode(t, tt.files[i].Path); node.Mode != m {
t.Errorf("xsum.EachList(`%s#%d') Node lstat mode: %o != %o (expected)", tt.name, i, node.Mode, m)
}
} else {
if m := statFileMode(t, tt.files[i].Path); node.Mode != m {
t.Errorf("xsum.EachList(`%s#%d') Node stat mode: %o != %o (expected)", tt.name, i, node.Mode, m)
}
}
if node.Sys.UID == nil || node.Sys.GID == nil {
t.Errorf("xsum.EachList(`%s#%d') Node unexpected sys: %+v", tt.name, i, node.Sys)
}
}
}
}
type sumResult struct {
sum []byte
err error
lstat bool
}
func newDummyHash() hash.Hash {
return &dummyHash{}
}
type dummyHash struct {
b []byte
}
func (d *dummyHash) Write(p []byte) (n int, err error) {
d.b = append(d.b, p...)
return len(p), nil
}
func (d *dummyHash) Sum(b []byte) []byte {
return append(b, append(append([]byte{'{'}, d.b...), '}')...)
}
func (d *dummyHash) Reset() {
d.b = nil
}
func (d *dummyHash) Size() int {
return len(d.b) + 2
}
func (d *dummyHash) BlockSize() int {
return 64
}
func lstatFileMode(t *testing.T, path string) os.FileMode {
t.Helper()
fi, err := os.Lstat(path)
if err != nil {
t.Fatal(err)
}
return fi.Mode()
}
func statFileMode(t *testing.T, path string) os.FileMode {
t.Helper()
fi, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
return fi.Mode()
}
func bl(v ...interface{}) []byte {
var out []byte
for _, b := range v {
switch b := b.(type) {
case byte:
out = append(out, b)
case []byte:
out = append(out, b...)
case string:
out = append(out, []byte(b)...)
case int:
out = append(out, byte(b))
case rune:
out = append(out, []byte(string([]rune{b}))...)
}
}
return out
}
func mkdev(t *testing.T, dev int, mode os.FileMode) string {
t.Helper()
p, err := os.MkdirTemp("", "xsum-test")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
os.RemoveAll(p)
})
path := filepath.Join(p, "device")
if err := syscall.Mknod(path, uint32(mode), dev); err != nil {
t.Fatal(err)
}
return path
}