forked from mtusi2026/mtusi2026lab4
62 lines
4.4 KiB
Python
62 lines
4.4 KiB
Python
#!/usr/bin/env python3
|
|
import sys,uuid,json,struct,zlib,concurrent.futures,time,subprocess,threading
|
|
from pathlib import Path
|
|
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'tests'))
|
|
from client import Client,arguments,check,run
|
|
ROOT=Path(__file__).resolve().parents[1]
|
|
def compose(*args,input=None):
|
|
r=subprocess.run([str(ROOT/'scripts/compose.sh'),*args],input=input,capture_output=True,cwd=ROOT)
|
|
check(r.returncode==0,'Infrastructure command failed: '+r.stderr.decode()[-600:]);return r.stdout
|
|
|
|
def sql(text): return compose('exec','-T','postgres','psql','-U','messenger','-d','messenger','-At','-c',text).decode().strip()
|
|
def main():
|
|
args=arguments('Attachment validation, access and parallel upload')
|
|
c=Client(args.base_url,args.ca_file,timeout=100);a,b,e,chat=c.bootstrap();path='/api/v1/chats/'+chat['id']+'/attachments?filename='
|
|
image=(ROOT/'tests/fixtures/sample.png').read_bytes()
|
|
def upload(raw=image,name='image.png',mime='image/png',status=201,actor=a):
|
|
return c.call('POST',path+name,status,actor=actor,raw=raw,headers={'Content-Type':mime},schema='Attachment' if status==201 else None)
|
|
upload(name='../evil.png',status=400);upload(name='evil%5Cfile.png',status=400);upload(name='bad%00.png',status=400)
|
|
jpeg=(ROOT/'student-tests/fixtures/sample.jpg').read_bytes()
|
|
jpg=upload(raw=jpeg,name='photo.jpg',mime='image/jpeg')['json']
|
|
check(c.call('GET','/api/v1/attachments/'+jpg['id']+'/content',actor=b)['bytes']==jpeg,'JPEG changed')
|
|
upload(raw=b'wrong',status=415);upload(mime='image/jpeg',status=415);upload(raw=image[:25],status=415)
|
|
upload(raw=b'x'*(10485760+1),status=413);upload(actor=e,status=404)
|
|
huge=bytearray(image);huge[16:24]=struct.pack('>II',10000,10000);huge[29:33]=struct.pack('>I',zlib.crc32(huge[12:29]));upload(raw=bytes(huge),status=413)
|
|
att=upload()['json'];content='/api/v1/attachments/'+att['id']
|
|
c.call('GET',content,404,actor=e);c.call('GET',content+'/content',404,actor=e)
|
|
c.call('GET',content+'/content?variant=absent',404,actor=b)
|
|
other=c.call('POST','/api/v1/chats',201,actor=a,body={'title':'Other','member_ids':[a['id']]},schema='Chat')['json']
|
|
c.call('POST','/api/v1/chats/'+other['id']+'/messages',404,actor=a,body={'text':'','attachment_ids':[att['id']]},headers={'Idempotency-Key':uuid.uuid4().hex})
|
|
key=uuid.uuid4().hex;first=c.message(a,chat,'',attachments=[att['id']],key=key)['json']
|
|
check(c.message(a,chat,'',attachments=[att['id']],key=key)['json']==first,'attachment replay differs')
|
|
c.call('POST','/api/v1/chats/'+chat['id']+'/messages',409,actor=a,body={'text':'changed','attachment_ids':[att['id']]},headers={'Idempotency-Key':key})
|
|
# 10 real 1024x1024 PNG uploads, synchronized start; sample cgroup memory while running.
|
|
width=1024;raw=bytearray()
|
|
import random
|
|
rng=random.Random(104)
|
|
for _ in range(width): raw+=b'\0'+rng.randbytes(width*3)
|
|
def chunk(kind,data): return struct.pack('>I',len(data))+kind+data+struct.pack('>I',zlib.crc32(kind+data))
|
|
medium=b'\x89PNG\r\n\x1a\n'+chunk(b'IHDR',struct.pack('>IIBBBBB',width,width,8,2,0,0,0))+chunk(b'IDAT',zlib.compress(bytes(raw),1))+chunk(b'IEND',b'')
|
|
ids=compose('ps','-q','api').decode().split();samples=[];stop=threading.Event()
|
|
def memory():
|
|
while not stop.is_set():
|
|
vals=[]
|
|
for cid in ids:
|
|
out=subprocess.check_output(['docker','exec',cid,'cat','/sys/fs/cgroup/memory.current']);vals.append(int(out))
|
|
samples.append({'at':time.time(),'bytes':vals});stop.wait(.1)
|
|
barrier=threading.Barrier(10)
|
|
def parallel(i):
|
|
barrier.wait();t=time.monotonic();upload(raw=medium,name='parallel-'+str(i)+'.png');return time.monotonic()-t
|
|
poll=threading.Thread(target=memory);poll.start();started=time.monotonic()
|
|
try:
|
|
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as ex: times=list(ex.map(parallel,range(10)))
|
|
finally:stop.set();poll.join()
|
|
report={'checks':['filename','size','decoded pixels','damaged image','MIME mismatch','private chat','foreign attachment','idempotency'],
|
|
'concurrency':10,'file_bytes':len(medium),'decoded_pixels':width*width,'seconds':time.monotonic()-started,
|
|
'successful_uploads':10,'errors':0,'p95_seconds':sorted(times)[-1],
|
|
'peak_api_cgroup_bytes':[max(s['bytes'][i] for s in samples) for i in range(len(ids))],
|
|
'samples':samples,'limits':'512 MiB per API; one decoder/upload per API; 64 KiB copy buffer'}
|
|
(ROOT/'evidence/attachments.json').write_text(json.dumps(report,indent=2)+'\n')
|
|
print('PASS: attachment validation, access, replay and 10 parallel uploads')
|
|
if __name__=='__main__':run(main)
|