forked from Densaugeo/uploadserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
409 lines (316 loc) · 16.9 KB
/
test.py
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import os, requests, unittest, subprocess, time, urllib3, socket, shutil, sys
from pathlib import Path
assert 'VERBOSE' in os.environ, '$VERBOSE envionment variable not set'
VERBOSE = os.environ['VERBOSE']
assert VERBOSE in ['0', '1'], '$VERBOSE must be 0 or 1'
VERBOSE = int(VERBOSE)
assert 'PROTOCOL' in os.environ, '$PROTOCOL envionment variable not set'
PROTOCOL = os.environ['PROTOCOL']
assert PROTOCOL in ['HTTP', 'HTTPS'], 'Unknown $PROTOCOL: {}'.format(PROTOCOL)
def setUpModule():
os.mkdir(Path(__file__).parent / 'test-temp')
os.chdir(Path(__file__).parent / 'test-temp')
os.symlink('../uploadserver', 'uploadserver')
os.mkdir('directory-option-test')
class Suite(unittest.TestCase):
def setUp(self):
print()
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def tearDown(self):
if hasattr(self, 'server'): self.server.terminate()
# Verify a basic test can run. Most importantly, verify the sleep is long enough for the sever to start
def test_basic(self):
self.spawn_server()
res = self.get('/')
self.assertEqual(res.status_code, 200)
# Verify the --port argument is properly passed to the underlying http.server
def test_argument_passthrough(self):
self.spawn_server(port=8080)
res = self.get('/', port=8080)
self.assertEqual(res.status_code, 200)
self.assertRaises(requests.ConnectionError, lambda: self.get('/'))
# Verify /upload at least responds to GET
def test_upload_page_exists(self):
self.spawn_server()
res = self.get('/upload')
self.assertEqual(res.status_code, 200)
# Simple upload test
def test_upload(self):
self.spawn_server()
res = self.post('/upload', files={
'files': ('a-file', 'file-content'),
})
self.assertEqual(res.status_code, 204)
with open('a-file') as f: self.assertEqual(f.read(), 'file-content')
# Verify uploaded file is renamed if there is a collision
def test_upload_same_name_default(self):
file_name = 'b-file'
file_renamed = f'{file_name} (1)' # this is the auto-renaming pattern
self.spawn_server()
res = self.post('/upload', files={
'files': (file_name, 'file-content'),
})
self.assertEqual(res.status_code, 204)
res = self.post('/upload', files={
'files': (file_name, 'file-content-same-name'),
})
self.assertEqual(res.status_code, 204)
with open(file_name) as f: self.assertEqual(f.read(), 'file-content')
with open(file_renamed) as f: self.assertEqual(f.read(), 'file-content-same-name')
# Verify uploads replace existing file with the same name
def test_upload_same_name_replace(self):
file_name = 'c-file'
file_renamed = f'{file_name} (1)' # this is the auto-renaming pattern
self.spawn_server(allow_replace=True)
res = self.post('/upload', files={
'files': (file_name, 'file-content'),
})
self.assertEqual(res.status_code, 204)
res = self.post('/upload', files={
'files': (file_name, 'file-content-replaced'),
})
self.assertEqual(res.status_code, 204)
with open(file_name) as f: self.assertEqual(f.read(), 'file-content-replaced')
self.assertEqual(os.path.isfile(file_renamed), False)
def test_upload_bad_path(self):
self.spawn_server()
res = self.post('/uploadx', files={
'file_foo': ('a-file', 'file-content'),
})
self.assertEqual(res.status_code, 404)
# Test a malformed upload
def test_upload_bad_field_name(self):
self.spawn_server()
res = self.post('/upload', files={
'file_foo': ('a-file', 'file-content'),
})
self.assertEqual(res.status_code, 400)
def test_upload_no_files(self):
self.spawn_server()
res = self.post('/upload', files={
'files': ('', ''),
})
self.assertEqual(res.status_code, 400)
# Verify multiple file upload works
def test_multiple_upload(self):
self.spawn_server()
res = self.post('/upload', files=[
('files', ('file-1', 'file-content-1')),
('files', ('file-2', 'file-content-2')),
])
self.assertEqual(res.status_code, 204)
with open('file-1') as f: self.assertEqual(f.read(), 'file-content-1')
with open('file-2') as f: self.assertEqual(f.read(), 'file-content-2')
# Uploads large enough to need a temp file have slightly different handling that needs to be tested
def test_large_upload(self):
self.spawn_server()
file_content = 1024*'a' + 1024*'b' + 1024*'c' + 1024*'d'
res = self.post('/upload', files={
'files': ('a-larger-file', file_content),
})
self.assertEqual(res.status_code, 204)
with open('a-larger-file') as f: self.assertEqual(f.read(), file_content)
# Verify directory traversal attempts are contained within server folder
def test_directory_traversal(self):
self.spawn_server()
res = self.post('/upload', files={
'files': ('../dt-name', 'dt-content'),
})
with open('dt-name') as f: self.assertEqual(f.read(), 'dt-content')
self.assertFalse(Path('../dt-name').exists())
# Directory option was added to http.server in Python 3.7
if sys.version_info.major >= 3 and sys.version_info.minor >= 7:
def test_upload_respects_directory(self):
self.spawn_server(directory='directory-option-test')
res = self.post('/upload', files={
'files': ('directory-file', 'file-content'),
})
self.assertEqual(res.status_code, 204)
with open('directory-option-test/directory-file') as f: self.assertEqual(f.read(), 'file-content')
# There's no client-side testing to verify the theme or UI, but I can at least make sure the server runs
# when a theme is used
def test_with_theme_dark(self):
self.spawn_server(theme='dark')
res = self.post('/upload', files={
'files': ('theme-dark-file', 'content-for-dark'),
})
self.assertEqual(res.status_code, 204)
with open('theme-dark-file') as f: self.assertEqual(f.read(), 'content-for-dark')
# There's no client-side testing to verify the theme or UI, but I can at least make sure the server runs
# when a theme is used
def test_with_theme_light(self):
self.spawn_server(theme='light')
res = self.post('/upload', files={
'files': ('theme-light-file', 'content-for-light'),
})
self.assertEqual(res.status_code, 204)
with open('theme-light-file') as f: self.assertEqual(f.read(), 'content-for-light')
# Verify uploads are accepted when the token option is used and the correct token is supplied
def test_token_valid_validate_endpoint(self):
self.spawn_server(token='a-token')
# 'files' option is used for both files and other form data
res = self.post('/upload/validateToken', files={
'token': (None, 'a-token'),
})
self.assertEqual(res.status_code, 204)
# Verify uploads are accepted when the token option is used and the correct token is supplied
def test_token_valid_upload_endpoint(self):
self.spawn_server(token='a-token')
# 'files' option is used for both files and other form data
res = self.post('/upload', files={
'files': ('valid-token-upload', 'token-upload-content'),
'token': (None, 'a-token'),
})
self.assertEqual(res.status_code, 204)
with open('valid-token-upload') as f: self.assertEqual(f.read(), 'token-upload-content')
# Verify uploads are rejected when the token option is used and an incorrect token is supplied
def test_token_invalid_validate_endpoint(self):
self.spawn_server(token='a-token')
# 'files' option is used for both files and other form data
res = self.post('/upload/validateToken', files={
'token': (None, 'a-bad-token'),
})
self.assertEqual(res.status_code, 403)
# Verify uploads are rejected when the token option is used and an incorrect token is supplied
def test_token_invalid_upload_endpoint(self):
self.spawn_server(token='a-token')
# 'files' option is used for both files and other form data
res = self.post('/upload', files={
'files': ('invalid-token-upload', 'token-upload-content'),
'token': (None, 'a-bad-token'),
})
self.assertEqual(res.status_code, 403)
self.assertFalse(Path('invalid-token-upload').exists())
# Verify uploads are rejected when the token option is used and no token is supplied
def test_token_missing_validate_endpoint(self):
self.spawn_server(token='a-token')
# 'files' option is used for both files and other form data
res = self.post('/upload/validateToken', files={})
self.assertEqual(res.status_code, 403)
# Verify uploads are rejected when the token option is used and no token is supplied
def test_token_missing_upload_endpoint(self):
self.spawn_server(token='a-token')
# 'files' option is used for both files and other form data
res = self.post('/upload', files={
'files': ('missing-token-upload', 'token-upload-content'),
})
self.assertEqual(res.status_code, 403)
self.assertFalse(Path('missing-token-upload').exists())
if PROTOCOL == 'HTTPS':
def test_client_cert_valid(self):
self.spawn_server(client_certificate='../client.crt')
res = self.post('/upload', cert='../client.pem', files={
'files': ('valid-client-cert-upload', 'client-cert-upload-content'),
})
self.assertEqual(res.status_code, 204)
with open('valid-client-cert-upload') as f: self.assertEqual(f.read(), 'client-cert-upload-content')
if PROTOCOL == 'HTTPS':
def test_client_cert_invalid(self):
self.spawn_server(client_certificate='../client.crt')
self.assertRaises(requests.ConnectionError, lambda: self.post('/upload', cert='../server.pem', files={
'files': ('invalid-client-cert-upload', 'client-cert-upload-content')
}))
self.assertFalse(Path('invalid-client-cert-upload').exists())
if PROTOCOL == 'HTTPS':
def test_client_cert_missing(self):
self.spawn_server(client_certificate='../client.crt')
self.assertRaises(requests.ConnectionError, lambda: self.post('/upload', files={
'files': ('missing-client-cert-upload', 'client-cert-upload-content'),
}))
self.assertFalse(Path('missing-client-cert-upload').exists())
if PROTOCOL == 'HTTPS':
# Verify that uploadserver will refuse to start if given a certificate inside its server root
def test_certificate_not_allowed_in_root(self):
shutil.copyfile('../server.pem', 'server.pem')
result = subprocess.run(
['python', '-m', 'uploadserver', '-c', 'server.pem'],
stdout=None if VERBOSE else subprocess.DEVNULL,
stderr=None if VERBOSE else subprocess.DEVNULL,
)
self.assertEqual(result.returncode, 3)
# Verify example curl command works
def test_curl_example(self):
self.spawn_server()
result = subprocess.run([
'curl', '-X', 'POST', '{}://localhost:8000/upload'.format(PROTOCOL.lower()),
'--insecure', '-F', 'files=@../test-files/simple-example.txt',
],
stdout=None if VERBOSE else subprocess.DEVNULL,
stderr=None if VERBOSE else subprocess.DEVNULL,
)
self.assertEqual(result.returncode, 0)
with open('simple-example.txt') as f_actual, open('../test-files/simple-example.txt') as f_expected:
self.assertEqual(f_actual.read(), f_expected.read())
# Verify example curl command with multiple files works
def test_curl_multiple_example(self):
self.spawn_server()
result = subprocess.run([
'curl', '-X', 'POST', '{}://localhost:8000/upload'.format(PROTOCOL.lower()),
'--insecure', '-F', 'files=@../test-files/multiple-example-1.txt',
'-F', 'files=@../test-files/multiple-example-2.txt',
],
stdout=None if VERBOSE else subprocess.DEVNULL,
stderr=None if VERBOSE else subprocess.DEVNULL,
)
self.assertEqual(result.returncode, 0)
with open('multiple-example-1.txt') as f_actual, open('../test-files/multiple-example-1.txt') as f_expected:
self.assertEqual(f_actual.read(), f_expected.read())
with open('multiple-example-2.txt') as f_actual, open('../test-files/multiple-example-2.txt') as f_expected:
self.assertEqual(f_actual.read(), f_expected.read())
# Verify example curl command with token works
def test_curl_token_example(self):
self.spawn_server(token='helloworld')
result = subprocess.run([
'curl', '-X', 'POST', '{}://localhost:8000/upload'.format(PROTOCOL.lower()),
'--insecure', '-F', 'files=@../test-files/token-example.txt', '-F', 'token=helloworld',
],
stdout=None if VERBOSE else subprocess.DEVNULL,
stderr=None if VERBOSE else subprocess.DEVNULL,
)
self.assertEqual(result.returncode, 0)
with open('token-example.txt') as f_actual, open('../test-files/token-example.txt') as f_expected:
self.assertEqual(f_actual.read(), f_expected.read())
if PROTOCOL == 'HTTPS':
# Verify example curl command with mTLS works
def test_curl_mtls_example(self):
self.spawn_server(client_certificate='../client.crt')
result = subprocess.run([
'curl', '-X', 'POST', '{}://localhost:8000/upload'.format(PROTOCOL.lower()),
'--insecure', '--cert', '../client.pem', '-F', 'files=@../test-files/mtls-example.txt',
],
stdout=None if VERBOSE else subprocess.DEVNULL,
stderr=None if VERBOSE else subprocess.DEVNULL,
)
self.assertEqual(result.returncode, 0)
with open('mtls-example.txt') as f_actual, open('../test-files/mtls-example.txt') as f_expected:
self.assertEqual(f_actual.read(), f_expected.read())
def spawn_server(self, port=None, allow_replace=False, directory=None, theme=None, token=None,
server_certificate=('../server.pem' if PROTOCOL == 'HTTPS' else None), client_certificate=None
):
args = ['python3', '-u', '-m', 'uploadserver']
if port: args += [str(port)]
if allow_replace: args += ['--allow-replace']
if directory: args += ['-d', directory]
if theme: args += ['--theme', theme]
if token: args += ['-t', token]
if server_certificate: args += ['-c', server_certificate]
if client_certificate: args += ['--client-certificate', client_certificate]
self.server = subprocess.Popen(args)
# Wait for server to finish starting
for _ in range(10):
try:
# This is stupid. Even if the server starts accepting connections, sometimes when it first starts
# it only accpets connections *some of the time*, so the connection must be tested several times
# to make sure it is really finished starting. 6 successes in a row appears to be the minimum to
# guarantee it is really up. 9 or more causes the server to respond slowly, but only if using HTTP
for _ in range(6):
with socket.create_connection(('127.0.0.1', port or 8000)): pass
break
except (ConnectionRefusedError, ConnectionResetError):
time.sleep(0.01)
else:
raise Exception('Port {} not responding. Did the server fail to start?'.format(port or 8000))
def get(self, path, port=8000, *args, **kwargs):
return requests.get('{}://127.0.0.1:{}{}'.format(PROTOCOL.lower(), port, path), verify=False, *args, **kwargs)
def post(self, path, port=8000, *args, **kwargs):
return requests.post('{}://127.0.0.1:{}{}'.format(PROTOCOL.lower(), port, path), verify=False, *args, **kwargs)