forked from mtusi2026/mtusi2026lab4
47 lines
2.3 KiB
Python
47 lines
2.3 KiB
Python
"""Black-box grade-4 checks; creates only synthetic data on the local lab."""
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
import threading
|
|
import uuid
|
|
from urllib.parse import quote
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
|
from client import Client, check
|
|
|
|
c = Client(timeout=45)
|
|
a,b,e,chat = c.bootstrap()
|
|
base = '/api/v1/chats/' + chat['id']
|
|
att, png = c.attachment(a, chat)
|
|
for name in ['../x.png', 'a\\b.png', '\x00.png', '\n.png']:
|
|
c.call('POST', base+'/attachments?filename='+quote(name, safe=''), 400, actor=a,
|
|
raw=png, headers={'Content-Type':'image/png'})
|
|
for data,mime,status in [(b'x'*(10485760+1),'image/png',413),
|
|
(b'not an image','image/png',415), (png,'image/jpeg',415),
|
|
(png[:20],'image/png',415)]:
|
|
c.call('POST', base+'/attachments?filename=x.png', status, actor=a,
|
|
raw=data, headers={'Content-Type':mime})
|
|
other = c.call('POST','/api/v1/chats',201,actor=a,
|
|
body={'title':'Other chat','member_ids':[a['id']]},schema='Chat')['json']
|
|
for target,ids,actor in [(other,[att['id']],a), (chat,[str(uuid.uuid4())],a), (chat,[att['id']],e)]:
|
|
c.call('POST','/api/v1/chats/'+target['id']+'/messages',404,actor=actor,
|
|
body={'text':'','attachment_ids':ids},headers={'Idempotency-Key':uuid.uuid4().hex})
|
|
for suffix in ['', '/content']:
|
|
c.call('GET','/api/v1/attachments/'+att['id']+suffix,404,actor=e)
|
|
# A member other than the uploader can attach and download the same file.
|
|
c.message(b, chat, '', attachments=[att['id']])
|
|
key = uuid.uuid4().hex
|
|
barrier = threading.Barrier(10)
|
|
def replay(_):
|
|
barrier.wait()
|
|
return Client(timeout=45).message(a,chat,'one logical message',key=key)['json']
|
|
with ThreadPoolExecutor(max_workers=10) as pool:
|
|
messages = list(pool.map(replay,range(10)))
|
|
check(all(m == messages[0] for m in messages), 'concurrent replay duplicated messages')
|
|
c.call('POST',base+'/messages',409,actor=a,body={'text':'different'},headers={'Idempotency-Key':key})
|
|
# Default [] and explicit [] are equivalent.
|
|
c.call('POST',base+'/messages',201,actor=a,body={'text':'one logical message','attachment_ids':[]},
|
|
headers={'Idempotency-Key':key},schema='Message')
|
|
print('PASS: invalid files, ACL, cross-chat references, concurrent idempotency')
|