forked from TRON-US/go-btfs-files
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtarwriter_test.go
149 lines (127 loc) · 3.31 KB
/
tarwriter_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
package files
import (
"archive/tar"
"errors"
"io"
"testing"
"time"
)
func TestTarWriter(t *testing.T) {
tf := NewMapDirectory(map[string]Node{
"file.txt": NewBytesFile([]byte(text)),
"boop": NewMapDirectory(map[string]Node{
"a.txt": NewBytesFile([]byte("bleep")),
"b.txt": NewBytesFile([]byte("bloop")),
}),
"beep.txt": NewBytesFile([]byte("beep")),
})
pr, pw := io.Pipe()
tw, err := NewTarWriter(pw)
if err != nil {
t.Fatal(err)
}
tr := tar.NewReader(pr)
go func() {
defer tw.Close()
if err := tw.WriteFile(tf, ""); err != nil {
t.Error(err)
}
}()
var cur *tar.Header
checkHeader := func(name string, typ byte, size int64) {
if cur.Name != name {
t.Errorf("got wrong name: %s != %s", cur.Name, name)
}
if cur.Typeflag != typ {
t.Errorf("got wrong type: %d != %d", cur.Typeflag, typ)
}
if cur.Size != size {
t.Errorf("got wrong size: %d != %d", cur.Size, size)
}
now := time.Now()
if cur.ModTime.After(now) {
t.Errorf("wrote timestamp in the future: %s (now) < %s", now, cur.ModTime)
}
}
if cur, err = tr.Next(); err != nil {
t.Fatal(err)
}
checkHeader("", tar.TypeDir, 0)
if cur, err = tr.Next(); err != nil {
t.Fatal(err)
}
checkHeader("beep.txt", tar.TypeReg, 4)
if cur, err = tr.Next(); err != nil {
t.Fatal(err)
}
checkHeader("boop", tar.TypeDir, 0)
if cur, err = tr.Next(); err != nil {
t.Fatal(err)
}
checkHeader("boop/a.txt", tar.TypeReg, 5)
if cur, err = tr.Next(); err != nil {
t.Fatal(err)
}
checkHeader("boop/b.txt", tar.TypeReg, 5)
if cur, err = tr.Next(); err != nil {
t.Fatal(err)
}
checkHeader("file.txt", tar.TypeReg, 13)
if cur, err = tr.Next(); err != io.EOF {
t.Fatal(err)
}
}
func TestTarWriterRelativePathInsideRoot(t *testing.T) {
tf := NewMapDirectory(map[string]Node{
"file.txt": NewBytesFile([]byte(text)),
"boop": NewMapDirectory(map[string]Node{
"../a.txt": NewBytesFile([]byte("bleep")),
"b.txt": NewBytesFile([]byte("bloop")),
}),
"beep.txt": NewBytesFile([]byte("beep")),
})
tw, err := NewTarWriter(io.Discard)
if err != nil {
t.Fatal(err)
}
defer tw.Close()
if err := tw.WriteFile(tf, ""); err != nil {
t.Error(err)
}
}
func TestTarWriterFailsFileOutsideRoot(t *testing.T) {
tf := NewMapDirectory(map[string]Node{
"file.txt": NewBytesFile([]byte(text)),
"boop": NewMapDirectory(map[string]Node{
"../../a.txt": NewBytesFile([]byte("bleep")),
"b.txt": NewBytesFile([]byte("bloop")),
}),
"beep.txt": NewBytesFile([]byte("beep")),
})
tw, err := NewTarWriter(io.Discard)
if err != nil {
t.Fatal(err)
}
defer tw.Close()
if err := tw.WriteFile(tf, ""); !errors.Is(err, ErrUnixFSPathOutsideRoot) {
t.Errorf("unexpected error, wanted: %v; got: %v", ErrUnixFSPathOutsideRoot, err)
}
}
func TestTarWriterFailsFileOutsideRootWithBaseDir(t *testing.T) {
tf := NewMapDirectory(map[string]Node{
"../file.txt": NewBytesFile([]byte(text)),
"boop": NewMapDirectory(map[string]Node{
"a.txt": NewBytesFile([]byte("bleep")),
"b.txt": NewBytesFile([]byte("bloop")),
}),
"beep.txt": NewBytesFile([]byte("beep")),
})
tw, err := NewTarWriter(io.Discard)
if err != nil {
t.Fatal(err)
}
defer tw.Close()
if err := tw.WriteFile(tf, "test.tar"); !errors.Is(err, ErrUnixFSPathOutsideRoot) {
t.Errorf("unexpected error, wanted: %v; got: %v", ErrUnixFSPathOutsideRoot, err)
}
}