forked from restic/rest-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers_test.go
60 lines (54 loc) · 1.49 KB
/
handlers_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
package restserver
import (
"path/filepath"
"testing"
)
func TestJoin(t *testing.T) {
var tests = []struct {
base, name string
result string
}{
{"/", "foo/bar", "/foo/bar"},
{"/srv/server", "foo/bar", "/srv/server/foo/bar"},
{"/srv/server", "/foo/bar", "/srv/server/foo/bar"},
{"/srv/server", "foo/../bar", "/srv/server/bar"},
{"/srv/server", "../bar", "/srv/server/bar"},
{"/srv/server", "..", "/srv/server"},
{"/srv/server", "../..", "/srv/server"},
{"/srv/server", "/repo/data/", "/srv/server/repo/data"},
{"/srv/server", "/repo/data/../..", "/srv/server"},
{"/srv/server", "/repo/data/../data/../../..", "/srv/server"},
{"/srv/server", "/repo/data/../data/../../..", "/srv/server"},
}
for _, test := range tests {
t.Run("", func(t *testing.T) {
got, err := join(filepath.FromSlash(test.base), test.name)
if err != nil {
t.Fatal(err)
}
want := filepath.FromSlash(test.result)
if got != want {
t.Fatalf("wrong result returned, want %v, got %v", want, got)
}
})
}
}
func TestIsUserPath(t *testing.T) {
var tests = []struct {
username string
path string
result bool
}{
{"foo", "/", false},
{"foo", "/foo", true},
{"foo", "/foo/", true},
{"foo", "/foo/bar", true},
{"foo", "/foobar", false},
}
for _, test := range tests {
result := isUserPath(test.username, test.path)
if result != test.result {
t.Errorf("isUserPath(%q, %q) was incorrect, got: %v, want: %v.", test.username, test.path, result, test.result)
}
}
}