-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormalize_key_test.go
More file actions
165 lines (158 loc) · 3.02 KB
/
normalize_key_test.go
File metadata and controls
165 lines (158 loc) · 3.02 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package storage_test
import (
"testing"
"github.com/AtomXZR/go-storage"
)
func TestNormalizeKey(t *testing.T) {
tests := []struct {
name string
input string
want string
wantErr bool
}{
// --- Valid: no leading slash ---
{
name: "simple key",
input: "foo.txt",
want: "foo.txt",
},
{
name: "nested path",
input: "images/avatars/foo.png",
want: "images/avatars/foo.png",
},
{
name: "deep nesting",
input: "a/b/c/d/e.txt",
want: "a/b/c/d/e.txt",
},
{
name: "file with dots in name",
input: "archive.tar.gz",
want: "archive.tar.gz",
},
{
name: "path with dots in name",
input: "releases/v1.2.3/binary.tar.gz",
want: "releases/v1.2.3/binary.tar.gz",
},
{
name: "leading slash simple",
input: "/foo.txt",
want: "foo.txt",
},
{
name: "leading slash nested",
input: "/images/avatars/foo.png",
want: "images/avatars/foo.png",
},
{
name: "double slash",
input: "images//foo.png",
want: "images/foo.png",
},
{
name: "triple slash",
input: "a///b///c.txt",
want: "a/b/c.txt",
},
{
name: "leading double slash",
input: "//foo.txt",
want: "foo.txt",
},
{
name: "dot segment in middle",
input: "images/./foo.png",
want: "images/foo.png",
},
{
name: "multiple dot segments",
input: "a/./b/./c.txt",
want: "a/b/c.txt",
},
{
name: "parent then child (resolves within root)",
input: "images/../images/foo.png",
want: "images/foo.png",
},
{
name: "redundant parent traversal that stays in bounds",
input: "a/b/../b/c.txt",
want: "a/b/c.txt",
},
{
name: "empty string",
input: "",
wantErr: true,
},
{
name: "root slash only",
input: "/",
wantErr: true,
},
{
name: "traversal at start",
input: "../etc/passwd",
wantErr: true,
},
{
name: "traversal with leading slash",
input: "/../etc/passwd",
want: "etc/passwd",
},
{
name: "traversal escapes deep path",
input: "a/b/../../../etc/passwd",
wantErr: true,
},
{
name: "just double dot",
input: "..",
wantErr: true,
},
{
name: "just dot",
input: ".",
wantErr: true,
},
{
name: "leading slash just dot",
input: "/.",
wantErr: true,
},
{
name: "leading slash just double dot",
input: "/..",
wantErr: true,
},
{
name: "trailing slash stripped by path.Clean",
input: "images/",
want: "images",
},
{
name: "nested trailing slash",
input: "images/avatars/",
want: "images/avatars",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := storage.NormalizeKey(tt.input)
if tt.wantErr {
if err == nil {
t.Errorf("normalizeKey(%q) expected error, got %q", tt.input, got)
}
return
}
if err != nil {
t.Errorf("normalizeKey(%q) unexpected error: %v", tt.input, err)
return
}
if got != tt.want {
t.Errorf("normalizeKey(%q) = %q, want %q", tt.input, got, tt.want)
}
})
}
}