Выполнение lab4 на 4
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
"""Attachment endpoints only; synthetic users and one PNG, no tokens in output."""
|
||||
import hashlib
|
||||
from pathlib import Path
|
||||
import struct
|
||||
import sys
|
||||
from urllib.parse import quote
|
||||
import uuid
|
||||
import zlib
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
from client import Client, arguments, check, run
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments('Attachment upload, metadata, content and validation')
|
||||
c = Client(args.base_url or 'http://127.0.0.1:8084', args.ca_file)
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
attachment, content = c.attachment(alice, chat)
|
||||
path = '/api/v1/attachments/' + attachment['id']
|
||||
check(attachment['variants'] == [] and attachment['error_code'] is None, 'invalid ready metadata')
|
||||
fetched = c.call('GET', path, actor=bob, schema='Attachment')['json']
|
||||
check(fetched == attachment, 'metadata changed between replicas')
|
||||
download = c.call('GET', path + '/content', actor=bob)
|
||||
check(download['bytes'] == content, 'original changed')
|
||||
check(download['headers'].get_content_type() == 'image/png', 'wrong MIME')
|
||||
check(hashlib.sha256(download['bytes']).hexdigest() == attachment['sha256'], 'checksum mismatch')
|
||||
print('PASS: upload 201 ready; participant metadata/download; original and checksum match')
|
||||
for suffix in ('', '/content'):
|
||||
c.call('GET', path + suffix, 404, actor=eve)
|
||||
c.call('GET', path + suffix, 401)
|
||||
c.call('GET', '/api/v1/attachments/' + str(uuid.uuid4()), 404, actor=alice)
|
||||
c.call('GET', path + '/content?variant=thumbnail', 404, actor=alice)
|
||||
print('PASS: unauthorized 401, outsider/unknown attachment 404, missing variant 404')
|
||||
upload = '/api/v1/chats/' + chat['id'] + '/attachments?filename=photo.png'
|
||||
c.call('POST', upload, 404, actor=eve, raw=content, headers={'Content-Type': 'image/png'})
|
||||
c.call('POST', upload, 401, raw=content, headers={'Content-Type': 'image/png'})
|
||||
for label, raw, mime, expected in (
|
||||
('corrupt', b'not an image', 'image/png', 415),
|
||||
('truncated', content[:33], 'image/png', 415),
|
||||
('spoofed MIME', content, 'image/jpeg', 415),
|
||||
('unsupported MIME', content, 'text/plain', 415),
|
||||
):
|
||||
c.call('POST', upload, expected, actor=alice, raw=raw, headers={'Content-Type': mime})
|
||||
print('PASS:', label, 'returns', expected)
|
||||
huge = bytearray(content)
|
||||
huge[16:24] = struct.pack('>II', 40001, 1000)
|
||||
huge[29:33] = struct.pack('>I', zlib.crc32(huge[12:29]))
|
||||
c.call('POST', upload, 413, actor=alice, raw=bytes(huge), headers={'Content-Type': 'image/png'})
|
||||
print('PASS: excessive pixel dimensions return 413')
|
||||
for name in ('', '../photo.png', 'a\\photo.png', 'a\x00.png', 'a\n.png', 'я' * 256):
|
||||
c.call('POST', upload.split('?')[0] + '?filename=' + quote(name, safe=''), 400,
|
||||
actor=alice, raw=content, headers={'Content-Type': 'image/png'})
|
||||
print('PASS: invalid filenames return 400')
|
||||
# Declared length is rejected before attempting an unbounded body read.
|
||||
c.call('POST', upload, 413, actor=alice, raw=b'',
|
||||
headers={'Content-Type': 'image/png', 'Content-Length': str(10485761)})
|
||||
print('PASS: declared byte length above 10 MiB returns 413')
|
||||
print('PASS: attachment HTTP checks completed; message attachment_ids not tested yet')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
Reference in New Issue
Block a user