"""Replay concrete reviewer cases through real HTTP and controlled dependency freezes.""" import io import json import struct import subprocess import time import zlib from concurrent.futures import ThreadPoolExecutor from pathlib import Path from client import Client, arguments, check, run ROOT=Path(__file__).resolve().parents[1] def compose(*args): return subprocess.check_output(['docker','compose',*args],cwd=ROOT,text=True,stderr=subprocess.DEVNULL).strip() def png(width,height): # Complete PNG, including all scanlines and CRCs, without allocating a bitmap. def chunk(kind,data): return struct.pack('>I',len(data))+kind+data+struct.pack('>I',zlib.crc32(kind+data)) compressor=zlib.compressobj() packed=io.BytesIO() row=b'\0'*(width+1) for _ in range(height): packed.write(compressor.compress(row)) packed.write(compressor.flush()) return (b'\x89PNG\r\n\x1a\n'+chunk(b'IHDR',struct.pack('>IIBBBBB',width,height,8,0,0,0,0)) +chunk(b'IDAT',packed.getvalue())+chunk(b'IEND',b'')) DIRECT='''import json,sys,time,urllib.request,urllib.error p=json.load(sys.stdin);h={} if p.get('token'):h['Authorization']='Bearer '+p['token'] r=urllib.request.Request('http://127.0.0.1:8080'+p['path'],headers=h) t=time.monotonic() try:r=urllib.request.urlopen(r,timeout=10) except urllib.error.HTTPError as e:r=e with r: data=json.load(r) if r.code>=400: assert data['error']['request_id']==r.headers['X-Request-Id'] assert data['error']['code']=='unavailable' print(json.dumps({'status':r.code,'seconds':round(time.monotonic()-t,4), 'request_id_present':bool(r.headers.get('X-Request-Id')),'instance_id_present':bool(r.headers.get('X-Instance-Id'))})) ''' LOCKER='''import asyncio,sys,asyncpg from src.core.settings import settings async def f(): c=await asyncpg.connect(settings.database_url.get_secret_value()) await c.execute('BEGIN; LOCK TABLE messages IN ACCESS EXCLUSIVE MODE') print('LOCKED',flush=True) await asyncio.to_thread(sys.stdin.readline) await c.execute('ROLLBACK');await c.close() asyncio.run(f()) ''' PENDING='''import asyncio,asyncpg from src.core.settings import settings async def f(): c=await asyncpg.connect(settings.database_url.get_secret_value()) print(await c.fetchval("SELECT count(*) FROM pg_stat_activity WHERE wait_event_type='Lock' AND query LIKE '%FROM messages%'")) await c.close() asyncio.run(f()) ''' def main(): args=arguments('API reviewer regression scenarios') c=Client(args.base_url,args.ca_file,timeout=15) alice,bob,_,chat=c.bootstrap() history='/api/v1/chats/'+chat['id']+'/messages' c.message(alice,chat,'Контроль до отказов') results=[] for name in ('review123\n','review123\r','review123\x00','review123\t'): c.call('POST','/api/v1/users',400,body={'username':name,'display_name':'Review','password':'valid-password-123'}) results.append({'case':'username_control_characters','result':'PASS','status':400}) data=(ROOT/'tests/fixtures/sample.png').read_bytes() upload='/api/v1/chats/'+chat['id']+'/attachments' for label,filename,payload,mime,status in [ ('truncated_png','sample.png',data[:-12],'image/png',415), ('header_only_png','sample.png',data[:33],'image/png',415), ('mismatched_mime','sample.png',data,'image/jpeg',415), ('DEL_filename','a%7F.png',data,'image/png',400), ('missing_filename',None,data,'image/png',400), ]: c.call('POST',upload+('' if filename is None else '?filename='+filename),status, actor=alice,raw=payload,headers={'Content-Type':mime}) results.append({'case':label,'result':'PASS','status':status}) for width,height,status in ((8000,5000,202),(8000,5001,413),(20000,10000,413)): payload=png(width,height) reply=c.call('POST',upload+'?filename=dimensions.png',status,actor=alice,raw=payload, headers={'Content-Type':'image/png'},schema='Attachment' if status==202 else None) if status==202: c.wait_ready(alice, reply['json']) check(c.call('GET','/api/v1/attachments/'+reply['json']['id']+'/content',actor=bob)['bytes']==payload, '40 MP original changed') results.append({'case':'pixels','dimensions':[width,height],'result':'PASS','status':status}) api=compose('ps','-q','api').split()[0] # Actual application credentials must not permit creating unrelated buckets. code='''from src.core.storage import get_storage from minio.error import S3Error import uuid s=get_storage();b='forbidden-'+uuid.uuid4().hex try:s.make_bucket(b) except S3Error as e:assert e.code=='AccessDenied';print('PASS') else:s.remove_bucket(b);raise AssertionError('API can create foreign buckets') ''' check(subprocess.check_output(['docker','exec',api,'python','-c',code],text=True).strip()=='PASS', 'API has excessive S3 rights') results.append({'case':'API_cannot_create_foreign_bucket','result':'PASS'}) def direct(path,token=None): reply=subprocess.run(['docker','exec','-i',api,'python','-c',DIRECT], input=json.dumps({'path':path,'token':token}),text=True,capture_output=True,timeout=13,check=True) return json.loads(reply.stdout) transfer_code="""import asyncio,http.client,json,sys,time from sqlalchemy import func,select from sqlalchemy.ext.asyncio import async_sessionmaker from src.core.db.session import create_engine from src.core.db.models import Attachment,UploadIntent p=json.load(sys.stdin) async def counts(): e=create_engine();f=async_sessionmaker(e) try: async with f() as s: return [await s.scalar(select(func.count()).select_from(m)) for m in (Attachment,UploadIntent)] finally:await e.dispose() before=asyncio.run(counts()) c=http.client.HTTPConnection('127.0.0.1',8080,timeout=8) c.putrequest('POST',p['path']) c.putheader('Authorization','Bearer '+p['token']) c.putheader('Content-Type','image/png');c.putheader('Content-Length','1000');c.endheaders() c.send(b'\\x89PNG\\r\\n\\x1a\\nshort');c.close();time.sleep(.3) assert before==asyncio.run(counts()),'interrupted client created durable state' c=http.client.HTTPConnection('127.0.0.1',8080,timeout=8) c.request('POST',p['path'],body=(b'x'*65536 for _ in range(161)), headers={'Authorization':'Bearer '+p['token'],'Content-Type':'image/png'},encode_chunked=True) r=c.getresponse();body=json.loads(r.read()) assert r.status==413 and body['error']['code']=='payload_too_large' assert body['error']['request_id']==r.getheader('X-Request-Id') c.close();assert before==asyncio.run(counts()),'oversized chunked body created durable state' print('PASS') """ transfer=subprocess.run(['docker','exec','-i',api,'python','-c',transfer_code], input=json.dumps({'path':upload+'?filename=interrupted.png','token':alice['token']}), text=True,capture_output=True,timeout=20,check=True) check(transfer.stdout.strip()=='PASS','transfer limits failed') results.append({'case':'client_abort_and_oversized_chunked_body','result':'PASS', 'oversized_status':413,'durable_state_unchanged':True}) direct(history,alice['token']) # Start an actual history SELECT, hold it on a table lock, then freeze PG. locker=subprocess.Popen(['docker','exec','-i',api,'python','-u','-c',LOCKER], stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True) check(locker.stdout.readline().strip()=='LOCKED','test table lock was not acquired') paused=False pool=ThreadPoolExecutor(max_workers=2) query=pool.submit(direct,history,alice['token']) try: deadline=time.monotonic()+2 while True: pending=int(subprocess.check_output(['docker','exec',api,'python','-c',PENDING],text=True)) if pending:break check(time.monotonic()deadline:raise time.sleep(.2) results.append({'case':'PostgreSQL_resume_without_API_restart','result':'PASS'}) print(json.dumps({'result':'PASS','checks':results},ensure_ascii=False,indent=2)) if __name__=='__main__': run(main)