Files
mtusi2026lab3/scripts/verify_teacher_http.py
T

194 lines
12 KiB
Python

"""Independent HTTP regressions derived from teacher review; no production mutation hooks."""
import argparse
import base64
import concurrent.futures
import hashlib
import importlib.util
import json
import struct
import subprocess
import time
import urllib.parse
import uuid
import zlib
from pathlib import Path
def png_chunk(kind, content):
return struct.pack('!I',len(content))+kind+content+struct.pack('!I',zlib.crc32(kind+content)&0xffffffff)
def tiny_png(filter_byte=0, bad_idat=False, width=1, height=1):
return (b'\x89PNG\r\n\x1a\n'+png_chunk(b'IHDR',struct.pack('!IIBBBBB',width,height,8,2,0,0,0))
+png_chunk(b'IDAT',b'not-a-zlib-stream' if bad_idat else zlib.compress(bytes([filter_byte,0,0,0])))
+png_chunk(b'IEND',b''))
def main():
p=argparse.ArgumentParser()
p.add_argument('--lab',type=int,choices=(3,4,5),required=True)
p.add_argument('--repo',type=Path,required=True)
p.add_argument('--base-url',required=True)
p.add_argument('--output',type=Path,required=True)
a=p.parse_args()
spec=importlib.util.spec_from_file_location('public_client',a.repo/'tests/client.py')
mod=importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
c=mod.Client(a.base_url,timeout=12)
results=[]
def case(label,fn):
started=time.monotonic()
try:
fn()
row={'case':label,'status':'PASS'}
except Exception as e:
row={'case':label,'status':'FAIL','error':str(e)[:700]}
row['elapsed_s']=round(time.monotonic()-started,4)
results.append(row)
print(row,flush=True)
alice,bob,eve,chat=c.bootstrap()
path='/api/v1/chats/'+chat['id']+'/messages'
def msg(body,key=None,actor=alice,status=201):
return c.call('POST',path,status,body=body,actor=actor,headers={'Idempotency-Key':uuid.uuid4().hex if key is None else key},schema='Message' if status==201 else None)
def equal(actual,expected):
assert actual==expected,'value did not match expected result'
for body in ({'text':None},{'text':123},{'text':True},{'text':[]},{'text':'x','TEXT':'y'},{'Text':'x'},{'text':'x','attachment_ids':None},{'text':'x','attachment_ids':{}},{}):
case('message rejects wrong types/extra '+str(body),lambda body=body:msg(body,status=400))
for key in ('','contains space','x'*129,'bad/key'):
case('invalid idempotency key '+repr(key[:20]),lambda key=key:msg({'text':'x'},key=key,status=400))
case('missing idempotency key',lambda:c.call('POST',path,400,body={'text':'x'},actor=alice))
for body in (b'{bad',b'\xff',b'{"text":"x"}{}'):
case('malformed UTF8/JSON '+repr(body),lambda body=body:c.call('POST',path,400,raw=body,actor=alice,headers={'Content-Type':'application/json','Idempotency-Key':uuid.uuid4().hex}))
for endpoint in ('/api/v1/chats',path):
for query in ('limit=','limit=0','limit=101','limit=abc','limit=1&limit=2','cursor=','cursor=W10','cursor=bnVsbA'):
case('pagination '+endpoint+'?'+query,lambda endpoint=endpoint,query=query:c.call('GET',endpoint+'?'+query,400,actor=alice))
c.message(alice,chat,'cursor1')
c.message(alice,chat,'cursor2')
cursor=c.call('GET',path+'?limit=1',actor=alice,schema='MessagePage')['json']['next_cursor']
decoded=json.loads(base64.urlsafe_b64decode(cursor+'='*(-len(cursor)%4)))
if isinstance(decoded,list) and len(decoded)==4:
for index,value in ((2,None),(2,1),(2,'2026-13-01T00:00:00Z'),(3,False),(3,[]),(3,{}),(3,'not-a-uuid')):
data=list(decoded); data[index]=value
corrupt=base64.urlsafe_b64encode(json.dumps(data).encode()).decode().rstrip('=')
case('typed cursor '+str((index,value)),lambda corrupt=corrupt:c.call('GET',path+'?cursor='+corrupt,400,actor=alice))
case('invalid UUID path',lambda:c.call('GET','/api/v1/chats/not-a-uuid',400,actor=alice))
for bad in ([],{},123,False):
case('member UUID '+str(bad),lambda bad=bad:c.call('POST','/api/v1/chats',400,actor=alice,body={'title':'x','member_ids':[alice['id'],bad]}))
case('unknown member400',lambda:c.call('POST','/api/v1/chats',400,actor=alice,body={'title':'x','member_ids':[alice['id'],str(uuid.uuid4())]}))
for body in (b'{bad',b'{}',b'null',b' '):
auth=base64.b64encode((alice['registration']['username']+':'+alice['registration']['password']).encode()).decode()
case('login rejects body '+repr(body),lambda body=body,auth=auth:c.call('POST','/api/v1/auth/sessions',400,raw=body,headers={'Authorization':'Basic '+auth,'Content-Type':'application/json'}))
case('nonASCII Bearer401',lambda:c.call('GET','/api/v1/users/me',401,headers={'Authorization':'Bearer é'}))
for suffix in ('\n',' '):
case('username trailing whitespace',lambda suffix=suffix:c.call('POST','/api/v1/users',400,body={'username':'review'+uuid.uuid4().hex[:8]+suffix,'display_name':'x','password':'Password123456'}))
case('blank password400',lambda:c.call('POST','/api/v1/users',400,body={'username':'r'+uuid.uuid4().hex[:15],'display_name':'x','password':' '*12}))
def unicode_login():
username='r'+uuid.uuid4().hex[:15]
password='я:'*64
user=c.call('POST','/api/v1/users',201,body={'username':username,'display_name':'Никита\0имя','password':password},schema='User')['json']
equal(user['display_name'],'Никита\0имя')
auth=base64.b64encode((username+':'+password).encode()).decode()
session=c.call('POST','/api/v1/auth/sessions',201,headers={'Authorization':'Basic '+auth},schema='Session')['json']
equal(session['user'],user)
case('UTF8 colon128 password and NUL display name',unicode_login)
def canonical_repeat():
key=uuid.uuid4().hex
original=msg({'text':'canonical'},key)['json']
equal(msg({'attachment_ids':[],'text':'canonical'},key)['json'],original)
msg({'text':'changed'},key,status=409)
case('omitted attachment_ids equals [] and conflict',canonical_repeat)
def concurrent_repeat():
key=uuid.uuid4().hex
with concurrent.futures.ThreadPoolExecutor(max_workers=16) as ex:
rows=list(ex.map(lambda _:msg({'text':'concurrent'},key)['json'],range(32)))
equal(len({x['id'] for x in rows}),1)
equal(len({json.dumps(x,sort_keys=True) for x in rows}),1)
case('32 concurrent same key all201 same message',concurrent_repeat)
def independent_scope():
key=uuid.uuid4().hex
one=msg({'text':'scope'},key)['json']
two=msg({'text':'scope'},key,actor=bob)['json']
assert one['id']!=two['id'] and two['sender_id']==bob['id']
another=c.call('POST','/api/v1/chats',201,actor=alice,body={'title':'another','member_ids':[alice['id']]},schema='Chat')['json']
three=c.message(alice,another,'scope',key=key)['json']
assert three['id']!=one['id'] and three['chat_id']==another['id']
case('key scope user and chat',independent_scope)
case('outsider cannot send',lambda:msg({'text':'outsider'},actor=eve,status=404))
def nul_message():
key=uuid.uuid4().hex
original=msg({'text':'abc\0def'},key)['json']
equal(original['text'],'abc\0def')
equal(msg({'text':'abc\0def'},key)['json'],original)
allrows=c.call('GET',path+'?limit=100',actor=bob,schema='MessagePage')['json']['items']
equal(next(x for x in allrows if x['id']==original['id']),original)
case('NUL message exact post replay history',nul_message)
def logout():
actor=c.user('logout')
c.call('GET','/api/v1/users/me',actor=actor,schema='User')
c.call('DELETE','/api/v1/auth/sessions/current',204,actor=actor)
for _ in range(4): c.call('GET','/api/v1/users/me',401,actor=actor)
case('logout visible through all replicas',logout)
if a.lab>=4:
fixture=(a.repo/'tests/fixtures/sample.png').read_bytes()
upload='/api/v1/chats/'+chat['id']+'/attachments'
def upl(raw=fixture,mime='image/png',name='sample.png',expected=None):
return c.call('POST',upload+'?filename='+urllib.parse.quote(name,safe=''),expected or (201 if a.lab==4 else 202),actor=alice,raw=raw,headers={'Content-Type':mime},schema='Attachment' if expected is None else None)
for label,raw,mime in (
('header only',fixture[:33],'image/png'),('bad IDAT',tiny_png(bad_idat=True),'image/png'),
('invalid filter',tiny_png(filter_byte=5),'image/png'),
('empty JPEG scan',bytes.fromhex('ffd8ffc0000b080001000101011100ffda0008010100003f00ffd9'),'image/jpeg'),
('MIME mismatch',fixture,'image/jpeg')):
case('image rejects '+label,lambda raw=raw,mime=mime:upl(raw=raw,mime=mime,expected=415))
jpeg_code="from PIL import Image; from io import BytesIO; import random,sys; im=Image.frombytes('RGB',(800,600),random.Random(42).randbytes(800*600*3)); b=BytesIO(); im.save(b,format='JPEG',quality=95); sys.stdout.buffer.write(b.getvalue())"
jpeg=subprocess.check_output([str(a.repo.absolute()/'.venv/bin/python'),'-c',jpeg_code])
case('valid real JPEG accepted',lambda:upl(raw=jpeg,mime='image/jpeg',name='real.jpg'))
case('half JPEG entropy with EOI rejected',lambda:upl(raw=jpeg[:len(jpeg)//2]+b'\xff\xd9',mime='image/jpeg',name='half.jpg',expected=415))
sos=jpeg.index(b'\xff\xda')
bad_sos=bytearray(jpeg);bad_sos[sos+5]=250
case('JPEG SOS unknown component rejected',lambda:upl(raw=bytes(bad_sos),mime='image/jpeg',name='sos.jpg',expected=415))
palette=(b'\x89PNG\r\n\x1a\n'+png_chunk(b'IHDR',struct.pack('!IIBBBBB',1,1,8,3,0,0,0))+png_chunk(b'PLTE',bytes(771))+png_chunk(b'IDAT',zlib.compress(b'\0\0'))+png_chunk(b'IEND',b''))
case('PNG invalid palette257 rejected',lambda:upl(raw=palette,expected=415))
for name in ('../x.png','a\\b.png','a\0b.png','a\x7fb.png','a\x85b.png',' '):
case('filename '+repr(name),lambda name=name:upl(name=name,expected=400))
case('filename missing',lambda:c.call('POST',upload,400,actor=alice,raw=fixture,headers={'Content-Type':'image/png'}))
case('pixel bomb413',lambda:upl(raw=tiny_png(width=20000,height=10000),expected=413))
case('too many bytes413',lambda:upl(raw=fixture+b'x'*(10485761-len(fixture)),expected=413))
attachments=[]
for _ in range(2):
att=upl()['json']
if a.lab==5: att=c.wait_ready(alice,att)
attachments.append(att)
def attachment_checks():
ids=[x['id'] for x in reversed(attachments)]
key=uuid.uuid4().hex
sent=msg({'text':'','attachment_ids':ids},key)['json']
equal(sent['attachment_ids'],ids)
equal(msg({'text':'','attachment_ids':ids},key)['json'],sent)
msg({'text':'','attachment_ids':list(reversed(ids))},key,status=409)
rows=c.call('GET',path+'?limit=100',actor=bob,schema='MessagePage')['json']['items']
equal(next(x for x in rows if x['id']==sent['id']),sent)
for att in attachments:
route='/api/v1/attachments/'+att['id']
for suffix in ('','/content'): c.call('GET',route+suffix,404,actor=eve)
content=c.call('GET',route+'/content',actor=bob)['bytes']
equal(hashlib.sha256(content).hexdigest(),att['sha256'])
msg({'text':'unknown','attachment_ids':[str(uuid.uuid4())]},status=404)
case('attachment order ACL original checksum',attachment_checks)
if a.lab==4:
case('ready missing thumbnail404',lambda:c.call('GET','/api/v1/attachments/'+attachments[0]['id']+'/content?variant=thumbnail',404,actor=alice))
# Valid PNG padded by a legal ancillary chunk, exactly 10 MiB.
def exact_limit():
base=tiny_png()
content=base[:-12]+png_chunk(b'tEXt',b'padding\0'+b'x'*(10485760-len(base)-12-8))+base[-12:]
equal(len(content),10485760)
att=upl(raw=content)['json']
equal(att['size_bytes'],10485760)
case('exact10MiB allowed through LB',exact_limit)
report={'lab':a.lab,'base_url':a.base_url,'tests':results,'passed':sum(x['status']=='PASS' for x in results),'failed':sum(x['status']=='FAIL' for x in results)}
a.output.parent.mkdir(parents=True,exist_ok=True)
a.output.write_text(json.dumps(report,ensure_ascii=False,indent=2)+'\n')
print('TOTAL',report['passed'],'PASS',report['failed'],'FAIL',flush=True)
return int(report['failed']>0)
if __name__=='__main__': raise SystemExit(main())