Files
mtusi2026lab2/student-tests/grade5/faults.py
T
gamayunov2004 799f56c513
Student application / application (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
ЛР 2: исследовать и ограничить cache stampede — на 5
2026-10-25 15:42:14 +03:00

50 lines
3.3 KiB
Python

#!/usr/bin/env python3
"""Own-project cache loss/restart, expiry while down, and log disclosure checks."""
import json, sys, time, uuid
from pathlib import Path
sys.path.insert(0,str(Path(__file__).resolve().parents[1]))
from grade4 import compose, wait_ready
sys.path.insert(0,str(Path(__file__).resolve().parents[2]/'tests'))
from client import Client,check
def main():
c=Client('http://api:8080');short=Client('http://api-expiry:8080')
a,b,eve,chat=c.bootstrap();expired=short.user('downexpiry');revoked=c.user('downrevoked')
key='profile:'+b['id'];marker='g5_private_body_'+uuid.uuid4().hex
first=c.message(a,chat,marker)['json']
response=c.call('POST','/api/v1/chats/'+chat['id']+'/messages',400,actor=a,body={'text':marker,'sender_id':eve['id']})
check(marker.encode() not in response['bytes'],'error echoes sensitive input')
c.call('GET','/api/v1/users/me',actor=b,schema='User')
check(compose('exec','-T','valkey','valkey-cli','DEL',key)=='1','own cache loss not applied')
try:
compose('stop','valkey');time.sleep(2.2)
start=time.monotonic()
c.call('GET','/api/v1/users/me',actor=b,schema='User')
c.call('GET','/api/v1/users/me',401,actor=expired)
c.call('DELETE','/api/v1/auth/sessions/current',204,actor=revoked)
c.call('GET','/api/v1/users/me',401,actor=revoked)
c.call('GET','/api/v1/users/me',401)
second=c.message(b,chat,'synthetic during outage')['json']
page=c.call('GET','/api/v1/chats/'+chat['id']+'/messages',actor=a,schema='MessagePage')['json']
check(first in page['items'] and second in page['items'],'outage RAW')
print('PASS: Valkey down: valid user/read-after-write work, expired/revoked/anonymous 401, errors omit input; %.3fs'%(time.monotonic()-start))
finally:compose('start','valkey')
recovered=time.monotonic()
for _ in range(40):
if compose('exec','-T','valkey','valkey-cli','PING')=='PONG':break
time.sleep(.1)
check(compose('exec','-T','valkey','valkey-cli','EXISTS',key)=='0','deleted own key unexpectedly restored')
time.sleep(5.1) # Existing bounded optional-call circuit opens for five seconds.
c.call('GET','/api/v1/users/me',actor=b,schema='User')
check(json.loads(compose('exec','-T','valkey','valkey-cli','GET',key))==b['user'],'no PG refill after restart/cooldown')
check(0<int(compose('exec','-T','valkey','valkey-cli','TTL',key))<=60,'refill TTL')
c.call('GET','/api/v1/users/me',401,actor=expired);c.call('GET','/api/v1/users/me',401,actor=revoked)
print('PASS: own profile lost before actual Valkey restart; refill from PG after cooldown, TTL<=60, expired/revoked stay401; recovery %.3fs'%(time.monotonic()-recovered))
logs=compose('logs','--no-color','api','api-peer','api-expiry','postgres','valkey')
forbidden=[marker]
for actor in [a,b,eve,expired,revoked]:forbidden.extend([actor['token'],actor['registration']['password']])
check(not any(value in logs for value in forbidden),'log disclosure (values intentionally suppressed)')
check('$argon2id$' not in logs,'password hash in logs')
print('PASS: API/PG/Valkey logs contain no test message marker, five passwords/tokens or Argon2 hashes; cache only public User fields. This is a tested marker scope, not a universal proof.')
if __name__=='__main__':main()