Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/pkg/utils/path_test.go
2320 views
1
package utils
2
3
import "testing"
4
5
func TestEncodePath(t *testing.T) {
6
t.Log(EncodePath("http://localhost:5244/d/123#.png"))
7
}
8
9
func TestFixAndCleanPath(t *testing.T) {
10
datas := map[string]string{
11
"": "/",
12
".././": "/",
13
"../../.../": "/...",
14
"x//\\y/": "/x/y",
15
".././.x/.y/.//..x../..y..": "/.x/.y/..x../..y..",
16
}
17
for key, value := range datas {
18
if FixAndCleanPath(key) != value {
19
t.Logf("raw %s fix fail", key)
20
}
21
}
22
}
23
24
func TestValidateNameComponent(t *testing.T) {
25
validNames := []string{
26
"file.txt",
27
"abc",
28
"file_name-1",
29
}
30
for _, name := range validNames {
31
if err := ValidateNameComponent(name); err != nil {
32
t.Fatalf("expected valid name %q, got error: %v", name, err)
33
}
34
}
35
36
invalidNames := []string{
37
"",
38
".",
39
"..",
40
"a/b",
41
`a\b`,
42
"a..b",
43
string([]byte{'a', 0, 'b'}),
44
}
45
for _, name := range invalidNames {
46
if err := ValidateNameComponent(name); err == nil {
47
t.Fatalf("expected invalid name %q to be rejected", name)
48
}
49
}
50
}
51
52
func TestJoinUnderBase(t *testing.T) {
53
base := "/lanzou-y/shared/test1"
54
out, err := JoinUnderBase(base, "file.txt")
55
if err != nil {
56
t.Fatalf("expected join success, got error: %v", err)
57
}
58
if out != "/lanzou-y/shared/test1/file.txt" {
59
t.Fatalf("unexpected join result: %s", out)
60
}
61
62
if _, err := JoinUnderBase(base, "../admin/screts.txt"); err == nil {
63
t.Fatalf("expected traversal to be rejected")
64
}
65
if _, err := JoinUnderBase(base, "sub/child"); err == nil {
66
t.Fatalf("expected nested path to be rejected")
67
}
68
}
69
70