forked from mtusi2026/mtusi2026lab4
46 lines
2.9 KiB
Python
46 lines
2.9 KiB
Python
"""Additional grade-3 checks: concurrent replay and invalid uploads."""
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
import uuid
|
|
import struct
|
|
import zlib
|
|
import subprocess
|
|
from client import Client, LAB, arguments, check, run
|
|
|
|
|
|
def main():
|
|
args = arguments('Extra acceptance checks')
|
|
c = Client(args.base_url)
|
|
alice,bob,eve,chat = c.bootstrap()
|
|
key = uuid.uuid4().hex
|
|
def send(_):
|
|
return c.message(alice,chat,'Concurrent retry',key=key)['json']
|
|
with ThreadPoolExecutor(max_workers=8) as pool:
|
|
results = list(pool.map(send,range(16)))
|
|
check(all(r==results[0] for r in results),'concurrent replay created different messages')
|
|
print('PASS: 16 concurrent retries return one message')
|
|
if LAB >= 4:
|
|
path = '/api/v1/chats/'+chat['id']+'/attachments'
|
|
content = open('tests/fixtures/sample.png','rb').read()
|
|
c.call('POST',path+'?filename=../x.png',400,actor=alice,raw=content,headers={'Content-Type':'image/png'})
|
|
c.call('POST',path+'?filename=x.jpg',415,actor=alice,raw=content,headers={'Content-Type':'image/jpeg'})
|
|
c.call('POST',path+'?filename=x.png',415,actor=alice,raw=b'broken',headers={'Content-Type':'image/png'})
|
|
c.call('POST',path+'?filename=x.png',413,actor=alice,raw=b'x'*(10485760+1),headers={'Content-Type':'image/png'})
|
|
# A real valid PNG >40 million pixels, highly compressible on the wire.
|
|
def chunk(kind, data):
|
|
return struct.pack('!I',len(data))+kind+data+struct.pack('!I',zlib.crc32(kind+data)&0xffffffff)
|
|
compressor = zlib.compressobj()
|
|
compressed = b''.join(compressor.compress(b'\0'*10001) for _ in range(4001))+compressor.flush()
|
|
huge = b'\x89PNG\r\n\x1a\n'+chunk(b'IHDR',struct.pack('!IIBBBBB',10000,4001,8,0,0,0,0))+chunk(b'IDAT',compressed)+chunk(b'IEND',b'')
|
|
c.call('POST',path+'?filename=huge.png',413,actor=alice,raw=huge,headers={'Content-Type':'image/png'})
|
|
jpeg = subprocess.check_output(['docker','compose','exec','-T','api1','python','-c',"from PIL import Image; import sys; Image.new('RGB',(8,8),'red').save(sys.stdout.buffer,format='JPEG')"])
|
|
uploaded = c.call('POST',path+'?filename=real.jpg',201,actor=alice,raw=jpeg,headers={'Content-Type':'image/jpeg'},schema='Attachment')['json']
|
|
downloaded = c.call('GET','/api/v1/attachments/'+uploaded['id']+'/content',actor=bob)
|
|
check(downloaded['bytes']==jpeg,'JPEG original changed')
|
|
other = c.call('POST','/api/v1/chats',201,actor=alice,body={'title':'Another chat','member_ids':[alice['id']]},schema='Chat')['json']
|
|
c.call('POST','/api/v1/chats/'+other['id']+'/messages',404,actor=alice,body={'text':'','attachment_ids':[uploaded['id']]},headers={'Idempotency-Key':uuid.uuid4().hex})
|
|
print('PASS: real JPEG roundtrip; >40 million pixels rejected; cross-chat attachment rejected')
|
|
print('PASS: unsafe filename, MIME mismatch, corruption and byte limit')
|
|
|
|
if __name__=='__main__':
|
|
run(main)
|