-
Notifications
You must be signed in to change notification settings - Fork 72
/
gossa_test.go
383 lines (324 loc) Β· 11.4 KB
/
gossa_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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package main
import (
"archive/zip"
"bytes"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strings"
"testing"
)
func dieMaybe(t *testing.T, err error) {
if err != nil {
t.Fatal(err)
}
}
func trimSpaces(str string) string {
space := regexp.MustCompile(`\s+`)
return space.ReplaceAllString(str, " ")
}
func getRaw(t *testing.T, url string) []byte {
resp, err := http.Get(url)
dieMaybe(t, err)
body, err := ioutil.ReadAll(resp.Body)
dieMaybe(t, err)
return body
}
func getZip(t *testing.T, needle string, dest string) (int, bool) {
b := getRaw(t, dest)
unzipped, err := zip.NewReader(bytes.NewReader(b), int64(len(b)))
dieMaybe(t, err)
length := len(unzipped.File)
for _, file := range unzipped.File {
if file.Name == needle {
return length, true
}
}
return length, false
}
func get(t *testing.T, url string) string {
body := getRaw(t, url)
return trimSpaces(string(body))
}
func postDummyFile(t *testing.T, url string, path string, payload string) string {
// Generated by curl-to-Go: https://mholt.github.io/curl-to-go
body := strings.NewReader("------WebKitFormBoundarycCRIderiXxJWEUcU\r\nContent-Disposition: form-data; name=\"\u1112\u1161 \u1112\u1161\"; filename=\"\u1112\u1161 \u1112\u1161\"\r\nContent-Type: application/octet-stream\r\n\r\n" + payload)
req, err := http.NewRequest("POST", url+"post", body)
dieMaybe(t, err)
req.Header.Set("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarycCRIderiXxJWEUcU")
req.Header.Set("Gossa-Path", path)
resp, err := http.DefaultClient.Do(req)
dieMaybe(t, err)
defer resp.Body.Close()
bodyS, err := ioutil.ReadAll(resp.Body)
dieMaybe(t, err)
return trimSpaces(string(bodyS))
}
func postJSON(t *testing.T, url string, what string) string {
resp, err := http.Post(url, "application/json", bytes.NewBuffer([]byte(what)))
dieMaybe(t, err)
body, err := ioutil.ReadAll(resp.Body)
dieMaybe(t, err)
return trimSpaces(string(body))
}
func fetchAndTestDefault(t *testing.T, url string) string {
body0 := get(t, url)
if !strings.Contains(body0, `<title>/</title>`) {
t.Fatal("error title")
}
if !strings.Contains(body0, `<h1 onclick="return titleClick(event)">./</h1>`) {
t.Fatal("error header")
}
if !strings.Contains(body0, `href="hols">hols/</a>`) {
t.Fatal("error hols folder")
}
if !strings.Contains(body0, `href="[email protected]%20%2840%25%29">[email protected] (40%)/</a>`) {
t.Fatal("error [email protected] (40%) folder")
}
if !strings.Contains(body0, `href="%E4%B8%AD%E6%96%87">δΈζ/</a>`) {
t.Fatal("error δΈζ folder")
}
if !strings.Contains(body0, `href="custom_mime_type.types">custom_mime_type.types</a>`) {
t.Fatal("error row custom_mime_type")
}
return body0
}
func doTestRegular(t *testing.T, url string, testExtra bool) {
var payload, path, body0, body1, body2 string
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test fetching default path")
fetchAndTestDefault(t, url)
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test fetching another page")
body0 = get(t, url+"/hols")
if !strings.Contains(body0, "glasgow.jpg") {
t.Fatal("fetching a subfolder failed")
}
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test fetching an invalid path - redirected to root")
fetchAndTestDefault(t, url+"../../")
fetchAndTestDefault(t, url+"hols/../../")
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test fetching regular files")
body0 = get(t, url+"subdir_with%20space/file_with%20space.html")
body1 = get(t, url+"fancy-path/a")
if body0 != `<b>spacious!!</b> ` || body1 != `fancy! ` {
t.Fatal("fetching a regular file errored")
}
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test fetching a invalid file")
path = "../../../../../../../../../../etc/passwd"
if !testExtra && get(t, url+path) != `error` {
t.Fatal("fetching a invalid file didnt errored")
} else if testExtra {
fetchAndTestDefault(t, url+path) // extra path will just redirect to root dir
}
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test zipping of folder δΈζ")
len, foundFile := getZip(t, "ζͺζ‘.html", url+"zip?zipPath=%2F%E4%B8%AD%E6%96%87%2F&zipName=%E4%B8%AD%E6%96%87")
if len != 1 || !foundFile {
t.Fatal("invalid zip generated")
}
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test zipping of folder with hidden file")
_, foundHidden := getZip(t, ".hidden-folder/some-file", url+"zip?zipPath=%2fhols%2f&zipName=hols")
if foundHidden && !testExtra {
t.Fatal("invalid zip generated - shouldnt contain hidden folder")
} else if !foundHidden && testExtra {
t.Fatal("invalid zip generated - should contain hidden folder")
}
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test zip invalid path")
body0 = get(t, url+"zip?zipPath=%2Ftmp&zipName=subdir")
println(body0)
if body0 != `error` {
t.Fatal("zip passed for invalid path")
}
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test mkdir rpc")
body0 = postJSON(t, url+"rpc", `{"call":"mkdirp","args":["/AAA"]}`)
if body0 != `ok` {
t.Fatal("mkdir rpc errored")
}
body0 = fetchAndTestDefault(t, url)
if !strings.Contains(body0, `href="AAA">AAA/</a>`) {
t.Fatal("mkdir rpc folder not created")
}
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test invalid mkdir rpc")
body0 = postJSON(t, url+"rpc", `{"call":"mkdirp","args":["../BBB"]}`)
if body0 != `error` {
t.Fatal("invalid mkdir rpc didnt errored #0")
}
body0 = postJSON(t, url+"rpc", `{"call":"mkdirp","args":["/../BBB"]}`)
if body0 != `error` {
t.Fatal("invalid mkdir rpc didnt errored #1")
}
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test post file")
path = "%2F%E1%84%92%E1%85%A1%20%E1%84%92%E1%85%A1" // "αα
‘ αα
‘" encoded
payload = "123 αα
‘"
body0 = postDummyFile(t, url, path, payload)
body1 = get(t, url+path)
body2 = fetchAndTestDefault(t, url)
if body0 != `ok` || body1 != payload || !strings.Contains(body2, `href="%E1%84%92%E1%85%A1%20%E1%84%92%E1%85%A1">αα
‘ αα
‘</a>`) {
t.Fatal("post file errored")
}
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test post file incorrect path")
body0 = postDummyFile(t, url, "%2E%2E"+path, payload)
if !strings.Contains(body0, `err`) {
t.Fatal("post file incorrect path didnt errored")
}
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test post file")
path = "2024-01-02-10:36:58.png"
payload = "123123123123123123123123"
body0 = postDummyFile(t, url, path, payload)
body1 = get(t, url+path)
body2 = fetchAndTestDefault(t, url)
if body0 != `ok` || body1 != payload || !strings.Contains(body2, `href="2024-01-02-10:36:58.png"`) {
t.Fatal("post file errored")
}
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test mv rpc")
body0 = postJSON(t, url+"rpc", `{"call":"mv","args":["/AAA", "/hols/AAA"]}`)
body1 = fetchAndTestDefault(t, url)
if body0 != `ok` || strings.Contains(body1, `href="AAA">AAA/</a></td> </tr>`) {
t.Fatal("mv rpc errored")
}
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test upload in new folder")
payload = "test"
body0 = postDummyFile(t, url, "%2Fhols%2FAAA%2Fabcdef", payload)
body1 = get(t, url+"hols/AAA/abcdef")
if body0 != `ok` || body1 != payload {
t.Fatal("upload in new folder errored")
}
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test symlink, should succeed: ", testExtra)
body0 = get(t, url+"/support/")
hasListing := strings.Contains(body0, `readme.md`)
body1 = get(t, url+"/support/readme.md")
hasReadme := strings.Contains(body1, `the master branch is automatically built and pushed`)
if !testExtra && hasReadme {
t.Fatal("error symlink file reached where illegal")
} else if testExtra && !hasReadme {
t.Fatal("error symlink file unreachable")
}
if !testExtra && hasListing {
t.Fatal("error symlink folder reached where illegal")
} else if testExtra && !hasListing {
t.Fatal("error symlink folder unreachable")
}
if testExtra {
fmt.Println("\r\n~~~~~~~~~~ test symlink mkdir & cleanup")
body0 = postJSON(t, url+"rpc", `{"call":"mkdirp","args":["/support/testfolder"]}`)
if body0 != `ok` {
t.Fatal("error symlink mkdir")
}
body0 = postJSON(t, url+"rpc", `{"call":"rm","args":["/support/testfolder"]}`)
if body0 != `ok` {
t.Fatal("error symlink rm")
}
}
fmt.Println("\r\n~~~~~~~~~~ test hidden file, should succeed: ", testExtra)
body0 = get(t, url+"/.testhidden")
hasHidden := strings.Contains(body0, `test`)
if !testExtra && hasHidden {
t.Fatal("error hidden file reached where illegal")
} else if testExtra && !hasHidden {
t.Fatal("error hidden file unreachable")
}
//
fmt.Println("\r\n~~~~~~~~~~ test upload in new folder")
payload = "test"
body0 = postDummyFile(t, url, "%2Fhols%2FAAA%2Fabcdef", payload)
body1 = get(t, url+"hols/AAA/abcdef")
if body0 != `ok` || body1 != payload {
t.Fatal("upload in new folder errored")
}
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test rm rpc & cleanup")
body0 = postJSON(t, url+"rpc", `{"call":"rm","args":["/hols/AAA"]}`)
if body0 != `ok` {
t.Fatal("cleanup errored #0")
}
body0 = get(t, url+"hols/AAA")
if !strings.Contains(body0, `error`) {
t.Fatal("cleanup errored #1")
}
body0 = postJSON(t, url+"rpc", `{"call":"rm","args":["/αα
‘ αα
‘"]}`)
if body0 != `ok` {
t.Fatal("cleanup errored #2")
}
fmt.Printf("\r\n=========\r\n")
}
func doTestReadonly(t *testing.T, url string) {
var payload, path, body0, body1 string
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test fetching default path")
fetchAndTestDefault(t, url)
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test fetching an invalid path - redirected to root")
fetchAndTestDefault(t, url+"../../")
fetchAndTestDefault(t, url+"hols/../../")
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test fetching regular files")
body0 = get(t, url+"subdir_with%20space/file_with%20space.html")
body1 = get(t, url+"fancy-path/a")
if body0 != `<b>spacious!!</b> ` || body1 != `fancy! ` {
t.Fatal("fetching a regular file errored")
}
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test fetching a invalid file")
path = "../../../../../../../../../../etc/passwd"
if get(t, url+path) != `error` {
t.Fatal("fetching a invalid file didnt errored")
}
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test mkdir rpc")
body0 = postJSON(t, url+"rpc", `{"call":"mkdirp","args":["/AAA"]}`)
if body0 == `ok` {
t.Fatal("mkdir rpc passed - should not be allowed")
}
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test post file")
path = "%2F%E1%84%92%E1%85%A1%20%E1%84%92%E1%85%A1" // "αα
‘ αα
‘" encoded
payload = "123 αα
‘"
body0 = postDummyFile(t, url, path, payload)
get(t, url+path)
if body0 == `ok` {
t.Fatal("post file passed - should not be allowed")
}
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test mv rpc")
body0 = postJSON(t, url+"rpc", `{"call":"mv","args":["/AAA", "/hols/AAA"]}`)
fetchAndTestDefault(t, url)
if body0 == `ok` {
t.Fatal("mv rpc passed - should not be allowed")
}
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test rm rpc & cleanup")
body0 = postJSON(t, url+"rpc", `{"call":"rm","args":["/hols/AAA"]}`)
if body0 == `ok` {
t.Fatal("cleanup passed - should not be allowed")
}
fmt.Printf("\r\n=========\r\n")
}
func TestNormal(t *testing.T) {
fmt.Println("========== testing normal path ============")
doTestRegular(t, "http://127.0.0.1:8001/", false)
}
func TestExtra(t *testing.T) {
fmt.Println("========== testing extras options ============")
doTestRegular(t, "http://127.0.0.1:8001/fancy-path/", true)
}
func TestRo(t *testing.T) {
fmt.Println("========== testing read only ============")
doTestReadonly(t, "http://127.0.0.1:8001/")
}
func TestRunMain(t *testing.T) {
main()
}