-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfilename_test.go
86 lines (73 loc) · 2.25 KB
/
filename_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
package vpk
import "testing"
func TestFilenameInvalidUnix(t *testing.T) {
bad := [][]string{
// Path / File / Extension
{" ", " ", " "}, // ""
{"/", " ", " "}, // "/"
{"/etc", "passwd", " "}, // "/etc/passwd"
{"..", "hello", "txt"}, // "../hello.txt"
{"dir", "\x00", ""}, // "dir/\0"
{"", "hello", "t/x/t"}, // "hello.t/x/t"
{"", "he/llo", "txt"}, // "hel/lo.txt"
}
for _, b := range bad {
e := &entry{path: b[0], file: b[1], ext: b[2]}
if e.FilenameSafeUnix() {
t.Errorf("%q should not be a valid filename", e.Filename())
}
}
}
func TestFilenameInvalidWindows(t *testing.T) {
bad := [][]string{
// Path / File / Extension
{" ", " ", " "}, // ""
{"/", " ", " "}, // "/"
{"c:", "hello", "txt"}, // "c:/hello.txt"
{"..", "hello", "txt"}, // "../hello.txt"
{"dir", "\x00", " "}, // "dir/\0"
{"dir", "\x1f", " "}, // "dir/\x1f"
{" ", "con", " "}, // "con"
{" ", "com1", " "}, // "com1"
{" ", "?", " "}, // "?"
{" ", "dot.", " "}, // "dot."
{" ", "space ", " "}, // "space "
{" ", "hello", "t/x/t"}, // "hello.t/x/t"
{" ", "he/llo", "txt"}, // "hell/o.txt"
}
for _, b := range bad {
e := &entry{path: b[0], file: b[1], ext: b[2]}
if e.FilenameSafeWindows() {
t.Errorf("%q should not be a valid filename", e.Filename())
}
}
}
func TestFilenameValidUnix(t *testing.T) {
good := [][]string{
// Path / File / Extension
{" ", "hello", "txt"}, // "hello.txt"
{"one/two/three", "hello", "txt"}, // "one/two/three/hello.txt"
{" ", "con", " "}, // "con"
{" ", "com1", " "}, // "com1"
}
for _, b := range good {
e := &entry{path: b[0], file: b[1], ext: b[2]}
if !e.FilenameSafeUnix() {
t.Errorf("%q should be a valid filename", e.Filename())
}
}
}
func TestFilenameValidWindows(t *testing.T) {
good := [][]string{
// Path / File / Extension
{" ", "hello", "txt"}, // "hello.txt"
{"one/two/three", "hello", "txt"}, // "one/two/three/hello.txt"
{" ", "con", "txt"}, // "con.txt"
}
for _, b := range good {
e := &entry{path: b[0], file: b[1], ext: b[2]}
if !e.FilenameSafeWindows() {
t.Errorf("%q should be a valid filename", e.Filename())
}
}
}