forked from mholt/archiver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
archiver_test.go
485 lines (447 loc) · 11.9 KB
/
archiver_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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package archiver
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
)
func TestWithin(t *testing.T) {
for i, tc := range []struct {
path1, path2 string
expect bool
}{
{
path1: "/foo",
path2: "/foo/bar",
expect: true,
},
{
path1: "/foo",
path2: "/foobar/asdf",
expect: false,
},
{
path1: "/foobar/",
path2: "/foobar/asdf",
expect: true,
},
{
path1: "/foobar/asdf",
path2: "/foobar",
expect: false,
},
{
path1: "/foobar/asdf",
path2: "/foobar/",
expect: false,
},
{
path1: "/",
path2: "/asdf",
expect: true,
},
{
path1: "/asdf",
path2: "/asdf",
expect: true,
},
{
path1: "/",
path2: "/",
expect: true,
},
{
path1: "/foo/bar/daa",
path2: "/foo",
expect: false,
},
{
path1: "/foo/",
path2: "/foo/bar/daa",
expect: true,
},
} {
actual := within(tc.path1, tc.path2)
if actual != tc.expect {
t.Errorf("Test %d: [%s %s] Expected %t but got %t", i, tc.path1, tc.path2, tc.expect, actual)
}
}
}
func TestMultipleTopLevels(t *testing.T) {
for i, tc := range []struct {
set []string
expect bool
}{
{
set: []string{},
expect: false,
},
{
set: []string{"/foo"},
expect: false,
},
{
set: []string{"/foo", "/foo/bar"},
expect: false,
},
{
set: []string{"/foo", "/bar"},
expect: true,
},
{
set: []string{"/foo", "/foobar"},
expect: true,
},
{
set: []string{"foo", "foo/bar"},
expect: false,
},
{
set: []string{"foo", "/foo/bar"},
expect: false,
},
{
set: []string{"../foo", "foo/bar"},
expect: true,
},
{
set: []string{`C:\foo\bar`, `C:\foo\bar\zee`},
expect: false,
},
{
set: []string{`C:\`, `C:\foo\bar`},
expect: false,
},
{
set: []string{`D:\foo`, `E:\foo`},
expect: true,
},
{
set: []string{`D:\foo`, `D:\foo\bar`, `C:\foo`},
expect: true,
},
{
set: []string{"/foo", "/", "/bar"},
expect: true,
},
} {
actual := multipleTopLevels(tc.set)
if actual != tc.expect {
t.Errorf("Test %d: %v: Expected %t but got %t", i, tc.set, tc.expect, actual)
}
}
}
func TestMakeNameInArchive(t *testing.T) {
for i, tc := range []struct {
sourceInfo fakeFileInfo
source string // a file path explicitly listed by the user to include in the archive
baseDir string // the base or root directory or path within the archive which contains all other files
fpath string // the file path being walked; if source is a directory, this will be a child path
expect string
}{
{
sourceInfo: fakeFileInfo{isDir: false},
source: "foo.txt",
baseDir: "",
fpath: "foo.txt",
expect: "foo.txt",
},
{
sourceInfo: fakeFileInfo{isDir: false},
source: "foo.txt",
baseDir: "base",
fpath: "foo.txt",
expect: "base/foo.txt",
},
{
sourceInfo: fakeFileInfo{isDir: false},
source: "foo/bar.txt",
baseDir: "",
fpath: "foo/bar.txt",
expect: "bar.txt",
},
{
sourceInfo: fakeFileInfo{isDir: false},
source: "foo/bar.txt",
baseDir: "base",
fpath: "foo/bar.txt",
expect: "base/bar.txt",
},
{
sourceInfo: fakeFileInfo{isDir: true},
source: "foo/bar",
baseDir: "base",
fpath: "foo/bar",
expect: "base/bar",
},
{
sourceInfo: fakeFileInfo{isDir: false},
source: "/absolute/path.txt",
baseDir: "",
fpath: "/absolute/path.txt",
expect: "path.txt",
},
{
sourceInfo: fakeFileInfo{isDir: false},
source: "/absolute/sub/path.txt",
baseDir: "",
fpath: "/absolute/sub/path.txt",
expect: "path.txt",
},
{
sourceInfo: fakeFileInfo{isDir: false},
source: "/absolute/sub/path.txt",
baseDir: "base",
fpath: "/absolute/sub/path.txt",
expect: "base/path.txt",
},
{
sourceInfo: fakeFileInfo{isDir: false},
source: "sub/path.txt",
baseDir: "base/subbase",
fpath: "sub/path.txt",
expect: "base/subbase/path.txt",
},
{
sourceInfo: fakeFileInfo{isDir: true},
source: "sub/dir",
baseDir: "base/subbase",
fpath: "sub/dir/path.txt",
expect: "base/subbase/dir/path.txt",
},
{
sourceInfo: fakeFileInfo{isDir: true},
source: "sub/dir",
baseDir: "base/subbase",
fpath: "sub/dir/sub2/sub3/path.txt",
expect: "base/subbase/dir/sub2/sub3/path.txt",
},
{
sourceInfo: fakeFileInfo{isDir: true},
source: `/absolute/dir`,
baseDir: "base",
fpath: `/absolute/dir/sub1/sub2/file.txt`,
expect: "base/dir/sub1/sub2/file.txt",
},
} {
actual, err := makeNameInArchive(tc.sourceInfo, tc.source, tc.baseDir, tc.fpath)
if err != nil {
t.Errorf("Test %d: Got error: %v", i, err)
}
if actual != tc.expect {
t.Errorf("Test %d: Expected '%s' but got '%s'", i, tc.expect, actual)
}
}
}
// TODO: We need a new .rar file since we moved the test corpus into the testdata/corpus subfolder.
/*
func TestRarUnarchive(t *testing.T) {
au := DefaultRar
auStr := fmt.Sprintf("%s", au)
tmp, err := ioutil.TempDir("", "archiver_test")
if err != nil {
t.Fatalf("[%s] %v", auStr, err)
}
defer os.RemoveAll(tmp)
dest := filepath.Join(tmp, "extraction_test_"+auStr)
os.Mkdir(dest, 0755)
file := "testdata/sample.rar"
err = au.Unarchive(file, dest)
if err != nil {
t.Fatalf("[%s] extracting archive [%s -> %s]: didn't expect an error, but got: %v", auStr, file, dest, err)
}
// Check that what was extracted is what was compressed
// Extracting links isn't implemented yet (in github.com/nwaples/rardecode lib there are no methods to get symlink info)
// Files access modes may differs on different machines, we are comparing extracted(as archive host) and local git clone
symmetricTest(t, auStr, dest, false, false)
}
*/
func TestArchiveUnarchive(t *testing.T) {
for _, af := range archiveFormats {
au, ok := af.(archiverUnarchiver)
if !ok {
t.Errorf("%s (%T): not an Archiver and Unarchiver", af, af)
continue
}
testArchiveUnarchive(t, au)
}
}
func TestArchiveUnarchiveWithFolderPermissions(t *testing.T) {
dir := "testdata/corpus/proverbs/extra"
currentPerms, err := os.Stat(dir)
if err != nil {
t.Fatalf("%v", err)
}
err = os.Chmod(dir, 0700)
if err != nil {
t.Fatalf("%v", err)
}
defer func() {
err := os.Chmod(dir, currentPerms.Mode())
if err != nil {
t.Fatalf("%v", err)
}
}()
TestArchiveUnarchive(t)
}
func testArchiveUnarchive(t *testing.T, au archiverUnarchiver) {
auStr := fmt.Sprintf("%s", au)
tmp, err := ioutil.TempDir("", "archiver_test")
if err != nil {
t.Fatalf("[%s] %v", auStr, err)
}
defer os.RemoveAll(tmp)
// Test creating archive
outfile := filepath.Join(tmp, "archiver_test."+auStr)
err = au.Archive([]string{"testdata/corpus"}, outfile)
if err != nil {
t.Fatalf("[%s] making archive: didn't expect an error, but got: %v", auStr, err)
}
// Test format matching (TODO: Make this its own test, out of band with the archive/unarchive tests)
//testMatching(t, au, outfile) // TODO: Disabled until we can finish implementing this for compressed tar formats
// Test extracting archive
dest := filepath.Join(tmp, "extraction_test_"+auStr)
os.Mkdir(dest, 0755)
err = au.Unarchive(outfile, dest)
if err != nil {
t.Fatalf("[%s] extracting archive [%s -> %s]: didn't expect an error, but got: %v", auStr, outfile, dest, err)
}
// Check that what was extracted is what was compressed
symmetricTest(t, auStr, dest, true, true)
}
// testMatching tests that au can match the format of archiveFile.
func testMatching(t *testing.T, au archiverUnarchiver, archiveFile string) {
m, ok := au.(Matcher)
if !ok {
t.Logf("[NOTICE] %T (%s) is not a Matcher", au, au)
return
}
file, err := os.Open(archiveFile)
if err != nil {
t.Fatalf("[%s] opening file for matching: %v", au, err)
}
defer file.Close()
tmpBuf := make([]byte, 2048)
io.ReadFull(file, tmpBuf)
matched, err := m.Match(file)
if err != nil {
t.Fatalf("%s (%T): testing matching: got error, expected none: %v", m, m, err)
}
if !matched {
t.Fatalf("%s (%T): format should have matched, but didn't", m, m)
}
}
// symmetricTest compares the contents of a destination directory to the contents
// of the test corpus and tests that they are equal.
func symmetricTest(t *testing.T, formatName, dest string, testSymlinks, testModes bool) {
var expectedFileCount int
filepath.Walk("testdata/corpus", func(fpath string, info os.FileInfo, err error) error {
if testSymlinks || (info.Mode()&os.ModeSymlink) == 0 {
expectedFileCount++
}
return nil
})
// If outputs equals inputs, we're good; traverse output files
// and compare file names, file contents, and file count.
var actualFileCount int
filepath.Walk(dest, func(fpath string, info os.FileInfo, err error) error {
if fpath == dest {
return nil
}
if testSymlinks || (info.Mode()&os.ModeSymlink) == 0 {
actualFileCount++
}
origPath, err := filepath.Rel(dest, fpath)
if err != nil {
t.Fatalf("[%s] %s: Error inducing original file path: %v", formatName, fpath, err)
}
origPath = filepath.Join("testdata", origPath)
expectedFileInfo, err := os.Lstat(origPath)
if err != nil {
t.Fatalf("[%s] %s: Error obtaining original file info: %v", formatName, fpath, err)
}
if !testSymlinks && (expectedFileInfo.Mode()&os.ModeSymlink) != 0 {
return nil
}
actualFileInfo, err := os.Lstat(fpath)
if err != nil {
t.Fatalf("[%s] %s: Error obtaining actual file info: %v", formatName, fpath, err)
}
if testModes && actualFileInfo.Mode() != expectedFileInfo.Mode() {
t.Fatalf("[%s] %s: File mode differed between on disk and compressed", formatName,
expectedFileInfo.Mode().String()+" : "+actualFileInfo.Mode().String())
}
if info.IsDir() {
// stat dir instead of read file
_, err = os.Stat(origPath)
if err != nil {
t.Fatalf("[%s] %s: Couldn't stat original directory (%s): %v", formatName,
fpath, origPath, err)
}
return nil
}
if (actualFileInfo.Mode() & os.ModeSymlink) != 0 {
expectedLinkTarget, err := os.Readlink(origPath)
if err != nil {
t.Fatalf("[%s] %s: Couldn't read original symlink target: %v", formatName, origPath, err)
}
actualLinkTarget, err := os.Readlink(fpath)
if err != nil {
t.Fatalf("[%s] %s: Couldn't read actual symlink target: %v", formatName, fpath, err)
}
if expectedLinkTarget != actualLinkTarget {
t.Fatalf("[%s] %s: Symlink targets differed between on disk and compressed", formatName, origPath)
}
return nil
}
expected, err := ioutil.ReadFile(origPath)
if err != nil {
t.Fatalf("[%s] %s: Couldn't open original file (%s) from disk: %v", formatName,
fpath, origPath, err)
}
actual, err := ioutil.ReadFile(fpath)
if err != nil {
t.Fatalf("[%s] %s: Couldn't open new file from disk: %v", formatName, fpath, err)
}
if !bytes.Equal(expected, actual) {
t.Fatalf("[%s] %s: File contents differed between on disk and compressed", formatName, origPath)
}
return nil
})
if got, want := actualFileCount, expectedFileCount; got != want {
t.Fatalf("[%s] Expected %d resulting files, got %d", formatName, want, got)
}
}
var archiveFormats = []interface{}{
DefaultZip,
DefaultTar,
DefaultTarBrotli,
DefaultTarBz2,
DefaultTarGz,
DefaultTarLz4,
DefaultTarSz,
DefaultTarXz,
DefaultTarZstd,
}
type archiverUnarchiver interface {
Archiver
Unarchiver
}
type fakeFileInfo struct {
name string
size int64
mode os.FileMode
modTime time.Time
isDir bool
sys interface{}
}
func (ffi fakeFileInfo) Name() string { return ffi.name }
func (ffi fakeFileInfo) Size() int64 { return ffi.size }
func (ffi fakeFileInfo) Mode() os.FileMode { return ffi.mode }
func (ffi fakeFileInfo) ModTime() time.Time { return ffi.modTime }
func (ffi fakeFileInfo) IsDir() bool { return ffi.isDir }
func (ffi fakeFileInfo) Sys() interface{} { return ffi.sys }