-
Notifications
You must be signed in to change notification settings - Fork 0
/
sanitize_test.go
62 lines (56 loc) · 1.23 KB
/
sanitize_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
package sanitize
import (
"path/filepath"
"testing"
)
var tc = []struct {
path string
sane bool
}{
{"../../../", false},
{"/etc/passwd", false},
{"../etc/passwd/", false},
{"testdir/cat/catpicture", true},
{"./testdir/cat/catpicture", true},
}
func TestIsSane(t *testing.T) {
for _, tt := range tc {
rootPath, err := filepath.Abs("testdir/")
if err != nil {
t.Fatal(err)
}
sane, err := IsSane(rootPath, tt.path)
if err != nil {
t.Fatal(err)
}
if sane != tt.sane {
t.Errorf("Sane is %v, should be %v for path %v", sane, tt.sane, tt.path)
}
}
}
func TestIsSaneAbsOnly(t *testing.T) {
rootPath := "testdata/"
_, err := IsSane(rootPath, "./")
if err == nil {
t.Error("Did not get error for supplying a relative root path")
}
}
func TesErrorIfNotSane(t *testing.T) {
for _, tt := range tc {
rootPath, err := filepath.Abs("testdir/")
if err != nil {
t.Fatal(err)
}
err = ErrorIfNotSane(rootPath, tt.path)
switch err.(type) {
case nil:
if tt.sane == false {
t.Errorf("expected error to be returned for unsanitary path %v, did not get one", tt.path)
}
case PathNotSaneError:
if tt.sane == true {
t.Errorf("did not expect PathNotSaneError, got one for path %v", tt.path)
}
}
}
}