-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathtest.py
572 lines (436 loc) · 18.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
import os, subprocess, time, urllib3, shutil, sys
from pathlib import Path
import pytest, requests
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)
TEST_BASIC_AUTH = requests.auth.HTTPBasicAuth('foo', 'bar')
TEST_BASIC_AUTH_BAD_USER = requests.auth.HTTPBasicAuth('foo2', 'bar')
TEST_BASIC_AUTH_BAD_PASS = requests.auth.HTTPBasicAuth('foo', 'bar2')
TEST_BASIC_AUTH_2 = requests.auth.HTTPBasicAuth('another user', 'pass')
pytestmark = pytest.mark.filterwarnings(
'ignore::urllib3.exceptions.InsecureRequestWarning')
server_holder = [None]
####################
# Setup / Teardown #
####################
def setup_module():
os.mkdir(Path(__file__).parent / 'test-temp')
os.chdir(Path(__file__).parent / 'test-temp')
os.symlink('../uploadserver', 'uploadserver')
os.mkdir('directory-option-test')
def setup_function():
print()
def teardown_function():
if server_holder[0]: server_holder[0].terminate()
#########
# Tests #
#########
# Verify a basic test can run. Most importantly, verify the sleep is long enough
# for the sever to start
def test_basic():
spawn_server()
res = get('/')
assert res.status_code == 200
# Verify the --port argument is properly passed to the underlying http.server
def test_argument_passthrough():
spawn_server(port=8080)
res = get('/', port=8080)
assert res.status_code == 200
with pytest.raises(requests.ConnectionError): get('/')
# Verify /upload at least responds to GET
def test_upload_page_exists():
spawn_server()
res = get('/upload')
assert res.status_code == 200
# Simple upload test
def test_upload():
spawn_server()
res = post('/upload', files={
'files': ('a-file', 'file-content'),
})
assert res.status_code == 204
with open('a-file') as f: assert f.read() == 'file-content'
def test_upload_put():
spawn_server()
res = put('/upload', files={
'files': ('put-file', 'file-content'),
})
assert res.status_code == 204
with open('put-file') as f: assert f.read() == 'file-content'
def test_basic_auth_get():
spawn_server(basic_auth=TEST_BASIC_AUTH)
assert get('/', auth=TEST_BASIC_AUTH).status_code == 200
def test_basic_auth_get_no_credentials():
spawn_server(basic_auth=TEST_BASIC_AUTH)
assert get('/').status_code == 401
def test_basic_auth_get_bad_user():
spawn_server(basic_auth=TEST_BASIC_AUTH)
assert get('/', auth=TEST_BASIC_AUTH_BAD_USER).status_code == 401
def test_basic_auth_get_bad_pass():
spawn_server(basic_auth=TEST_BASIC_AUTH)
assert get('/', auth=TEST_BASIC_AUTH_BAD_PASS).status_code == 401
def test_basic_auth_get_upload_only():
spawn_server(basic_auth_upload=TEST_BASIC_AUTH)
assert get('/').status_code == 200
@pytest.mark.parametrize('condition', ['basic_auth', 'basic_auth_upload'])
def test_basic_auth_post(condition):
spawn_server(**{ condition: TEST_BASIC_AUTH })
assert post('/upload', auth=TEST_BASIC_AUTH, files={
'files': (condition, 'file-content'),
}).status_code == 204
with open(condition) as f: assert f.read() == 'file-content'
@pytest.mark.parametrize('condition', ['basic_auth', 'basic_auth_upload'])
def test_basic_auth_post_no_credentials(condition):
spawn_server(**{ condition: TEST_BASIC_AUTH })
assert post('/upload', files={
'files': ('unauth-file', 'file-content'),
}).status_code == 401
assert not Path('unauth-file').exists()
@pytest.mark.parametrize('condition', ['basic_auth', 'basic_auth_upload'])
def test_basic_auth_post_bad_user(condition):
spawn_server(**{ condition: TEST_BASIC_AUTH })
assert post('/upload', auth=TEST_BASIC_AUTH_BAD_USER, files={
'files': ('unauth-file', 'file-content'),
}).status_code == 401
assert not Path('unauth-file').exists()
@pytest.mark.parametrize('condition', ['basic_auth', 'basic_auth_upload'])
def test_basic_auth_post_bad_pass(condition):
spawn_server(**{ condition: TEST_BASIC_AUTH })
assert post('/upload', auth=TEST_BASIC_AUTH_BAD_PASS, files={
'files': ('unauth-file', 'file-content'),
}).status_code == 401
assert not Path('unauth-file').exists()
def test_basic_auth_no_remnants():
spawn_server(basic_auth=TEST_BASIC_AUTH)
with open('../test-files/token-remnant-bug.txt') as f:
# 'files' option is used for both files and other form data
assert post('/upload', files={
'files': ('token-remnant-bug.txt', f.read()),
}).status_code == 401
assert not Path('token-remnant-bug.txt').exists()
# Check for bug #29, in which a blocked upload left behind tmp files.
# Resolved by adding HTTP basic auth, which is not susceptible to this
assert next(Path('.').glob('tmp*'), None) is None
@pytest.mark.parametrize('auth', [TEST_BASIC_AUTH, TEST_BASIC_AUTH_2])
def test_dual_basic_auth(auth):
spawn_server(basic_auth=TEST_BASIC_AUTH,
basic_auth_upload=TEST_BASIC_AUTH_2)
res = get('/', auth=auth)
assert res.status_code == 200
def test_dual_basic_auth_upload():
spawn_server(basic_auth=TEST_BASIC_AUTH,
basic_auth_upload=TEST_BASIC_AUTH_2)
assert post('/upload', auth=TEST_BASIC_AUTH_2, files={
'files': ('dual-auth', 'dual-auth-content'),
}).status_code == 204
with open('dual-auth') as f: assert f.read() == 'dual-auth-content'
def test_dual_basic_auth_upload_wrong_login():
spawn_server(basic_auth=TEST_BASIC_AUTH,
basic_auth_upload=TEST_BASIC_AUTH_2)
assert post('/upload', auth=TEST_BASIC_AUTH, files={
'files': ('unauth-file', 'file-content'),
}).status_code == 401
assert not Path('unauth-file').exists()
# Verify uploaded file is renamed if there is a collision
def test_upload_same_name_default():
file_name = 'autorename'
file_renamed = f'{file_name} (1)' # this is the auto-renaming pattern
spawn_server()
res = post('/upload', files={
'files': (file_name, 'file-content'),
})
assert res.status_code == 204
res = post('/upload', files={
'files': (file_name, 'file-content-same-name'),
})
assert res.status_code == 204
with open(file_name) as f: assert f.read() == 'file-content'
with open(file_renamed) as f: assert f.read() == 'file-content-same-name'
# Verify uploads replace existing file with the same name
def test_upload_same_name_replace():
file_name = 'c-file'
file_renamed = f'{file_name} (1)' # this is the auto-renaming pattern
spawn_server(allow_replace=True)
res = post('/upload', files={
'files': (file_name, 'file-content'),
})
assert res.status_code == 204
res = post('/upload', files={
'files': (file_name, 'file-content-replaced'),
})
assert res.status_code == 204
with open(file_name) as f: assert f.read() == 'file-content-replaced'
assert os.path.isfile(file_renamed) == False
def test_upload_bad_path():
spawn_server()
res = post('/uploadx', files={
'file_foo': ('a-file', 'file-content'),
})
assert res.status_code == 404
# Test a malformed upload
def test_upload_bad_field_name():
spawn_server()
res = post('/upload', files={
'file_foo': ('a-file', 'file-content'),
})
assert res.status_code == 400
def test_upload_no_files():
spawn_server()
res = post('/upload', files={
'files': ('', ''),
})
assert res.status_code == 400
# Verify multiple file upload works
def test_multiple_upload():
spawn_server()
res = post('/upload', files=[
('files', ('file-1', 'file-content-1')),
('files', ('file-2', 'file-content-2')),
])
assert res.status_code == 204
with open('file-1') as f: assert f.read() == 'file-content-1'
with open('file-2') as f: assert 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():
spawn_server()
file_content = 1024*'a' + 1024*'b' + 1024*'c' + 1024*'d'
res = post('/upload', files={
'files': ('a-larger-file', file_content),
})
assert res.status_code == 204
with open('a-larger-file') as f: assert f.read() == file_content
def test_url_encoded_file_name():
spawn_server()
file_content = 'A message from a strangely named file'
res = post('/upload', files={
'files': ('url%2Eencoding.txt', file_content)
})
assert res.status_code == 204
with open('url%2Eencoding.txt') as f: assert f.read() == file_content
# Verify directory traversal attempts are contained within server folder
def test_directory_traversal():
spawn_server()
res = post('/upload', files={
'files': ('../dt-name', 'dt-content'),
})
with open('dt-name') as f: assert f.read() == 'dt-content'
assert not Path('../dt-name').exists()
def test_upload_respects_directory():
spawn_server(directory='directory-option-test')
res = post('/upload', files={
'files': ('directory-file', 'file-content'),
})
assert res.status_code == 204
with open('directory-option-test/directory-file') as f:
assert 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():
spawn_server(theme='dark')
res = post('/upload', files={
'files': ('theme-dark-file', 'content-for-dark'),
})
assert res.status_code == 204
with open('theme-dark-file') as f: assert 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():
spawn_server(theme='light')
res = post('/upload', files={
'files': ('theme-light-file', 'content-for-light'),
})
assert res.status_code == 204
with open('theme-light-file') as f: assert f.read() == 'content-for-light'
def test_directory_listing_injections():
spawn_server()
res = get('/')
assert res.status_code == 200
assert int(res.headers['Content-Length']) == len(res.text)
assert '<!-- Injected by uploadserver -->' in res.text
assert '<a href="/upload">File upload</a>' in res.text
# Test this on the CGI variant too, to validate the funny inheritance pattern
def test_directory_listing_injections_cgi():
spawn_server(cgi=True)
res = get('/')
assert res.status_code == 200
assert int(res.headers['Content-Length']) == len(res.text)
assert '<!-- Injected by uploadserver -->' in res.text
assert '<a href="/upload">File upload</a>' in res.text
if PROTOCOL == 'HTTPS':
def test_client_cert_valid():
spawn_server(client_certificate=('../client.pem', '../client.crt'))
res = post('/upload', cert='../client.pem', files={
'files': ('valid-client-cert-upload', 'client-cert-upload-content'),
})
assert res.status_code == 204
with open('valid-client-cert-upload') as f:
assert f.read() == 'client-cert-upload-content'
if PROTOCOL == 'HTTPS':
def test_client_cert_invalid():
spawn_server(client_certificate=('../client.pem', '../client.crt'))
with pytest.raises(requests.ConnectionError):
post('/upload', cert='../server.pem', files={
'files': ('invalid-client-cert-upload',
'client-cert-upload-content')
})
assert not Path('invalid-client-cert-upload').exists()
if PROTOCOL == 'HTTPS':
def test_client_cert_missing():
spawn_server(client_certificate=('../client.pem', '../client.crt'))
with pytest.raises(requests.ConnectionError):
post('/upload', files={
'files': ('missing-client-cert-upload',
'client-cert-upload-content'),
})
assert not 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():
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,
)
assert result.returncode == 3
# Verify example curl command works
def test_curl_example():
spawn_server()
result = subprocess.run([
'curl', '-X', 'POST', f'{PROTOCOL.lower()}://localhost:8000/upload',
'--insecure', '-F', 'files=@../test-files/simple-example.txt',
],
stdout=None if VERBOSE else subprocess.DEVNULL,
stderr=None if VERBOSE else subprocess.DEVNULL,
)
assert result.returncode == 0
with open('simple-example.txt') as f_actual:
with open('../test-files/simple-example.txt') as f_expected:
assert f_actual.read() == f_expected.read()
# Verify example curl command with multiple files works
def test_curl_multiple_example():
spawn_server()
result = subprocess.run([
'curl', '-X', 'POST', f'{PROTOCOL.lower()}://localhost:8000/upload',
'--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,
)
assert result.returncode == 0
with open('multiple-example-1.txt') as f_actual:
with open('../test-files/multiple-example-1.txt') as f_expected:
assert f_actual.read() == f_expected.read()
with open('multiple-example-2.txt') as f_actual:
with open('../test-files/multiple-example-2.txt') as f_expected:
assert f_actual.read() == f_expected.read()
if PROTOCOL == 'HTTPS':
# Verify example curl command with mTLS works
def test_curl_mtls_example():
spawn_server(client_certificate=('../client.pem', '../client.crt'))
result = subprocess.run([
'curl', '-X', 'POST',
f'{PROTOCOL.lower()}://localhost:8000/upload',
'--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,
)
assert result.returncode == 0
with open('mtls-example.txt') as f_actual:
with open('../test-files/mtls-example.txt') as f_expected:
assert f_actual.read() == f_expected.read()
# Verify example curl command with HTTP basic auth works
def test_http_basic_auth_example():
spawn_server(basic_auth=requests.auth.HTTPBasicAuth('hello', 'world'))
result = subprocess.run([
'curl', '-X', 'POST', f'{PROTOCOL.lower()}://localhost:8000/upload',
'--insecure', '-F', 'files=@../test-files/basic-auth-example.txt',
'-u', 'hello:world',
],
stdout=None if VERBOSE else subprocess.DEVNULL,
stderr=None if VERBOSE else subprocess.DEVNULL,
)
assert result.returncode == 0
with open('basic-auth-example.txt') as f_actual:
with open('../test-files/basic-auth-example.txt') as f_expected:
assert f_actual.read() == f_expected.read()
# Make sure --help output in readme is updated. Use Python 3.13 for help output
# (since its generated by argparse and sometimes changes between versions)
if sys.version_info.major == 3 and sys.version_info.minor == 13:
def test_help_info_in_readme():
result = subprocess.run([
sys.executable, '-u', '-m', 'uploadserver', '-h',
],
capture_output=True,
env={ 'COLUMNS': '80' },
)
assert result.returncode == 0
print(result.stdout)
with open('../README.md', 'rb') as f:
assert result.stdout in f.read(), '--help output not found in ' + \
'README.md, does the readme need to be updated?'
###########
# Helpers #
###########
# Cannot be made into a fixture because fixture do not allow passing arguments
# in Python 3.6 (so I could make it into a fixture now that 3.6 is long
# dropped...). All of these arguments (except for the bool ones) are really
# some_type | None, but Python 3.9 doesn't support |
def spawn_server(
port: int = None,
cgi: bool = False,
allow_replace: bool = False,
directory: str = None,
theme: str = None,
server_certificate: str = ('../server.pem' if PROTOCOL == 'HTTPS'
else None),
client_certificate: str = None,
basic_auth: requests.auth.HTTPBasicAuth = None,
basic_auth_upload: requests.auth.HTTPBasicAuth = None,
):
args = [sys.executable, '-u', '-m', 'uploadserver']
if port: args += [str(port)]
if cgi: args += ['--cgi']
if allow_replace: args += ['--allow-replace']
if directory: args += ['-d', directory]
if theme: args += ['--theme', theme]
if server_certificate: args += ['-c', server_certificate]
if client_certificate: args += ['--client-certificate',
client_certificate[1]]
if basic_auth:
args += ['--basic-auth', f'{basic_auth.username}:{basic_auth.password}']
if basic_auth_upload:
args += ['--basic-auth-upload',
f'{basic_auth_upload.username}:{basic_auth_upload.password}']
server_holder[0] = subprocess.Popen(args)
# Wait for server to finish starting
for _ in range(100):
try:
get('/', port=port or 8000,
cert=client_certificate[0] if client_certificate else None
)
break
except requests.exceptions.ConnectionError:
time.sleep(0.01)
else:
raise Exception(f'Port {port or 8000} not responding. Did the server '
'fail to start?')
def get(path: str, port: int = 8000, *args, **kwargs) -> requests.Response:
return requests.get(f'{PROTOCOL.lower()}://127.0.0.1:{port}{path}',
verify=False, *args, **kwargs)
def post(path: str, port: int = 8000, *args, **kwargs) -> requests.Response:
return requests.post(f'{PROTOCOL.lower()}://127.0.0.1:{port}{path}',
verify=False, *args, **kwargs)
def put(path: str, port: int = 8000, *args, **kwargs) -> requests.Response:
return requests.put(f'{PROTOCOL.lower()}://127.0.0.1:{port}{path}',
verify=False, *args, **kwargs)