#!/usr/bin/env python3 import argparse import base64 import json import subprocess import sys import threading import time import uuid from pathlib import Path from urllib.parse import quote ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / 'tests')) from client import Client, Failure, check RESULTS = [] def compose(*args, check_rc=True): proc = subprocess.run(['docker', 'compose', *args], cwd=ROOT, capture_output=True, text=True, encoding='utf-8') if check_rc and proc.returncode != 0: raise Failure('docker compose ' + ' '.join(args[:2]) + ' failed') return proc.stdout def psql(sql): return compose('exec', '-T', 'postgres', 'psql', '-U', 'messenger', '-d', 'messenger', '-tAX', '-c', sql).strip() def valkey(*cmd): script = 'valkey-cli --no-auth-warning -a "$(cut -d" " -f2 /run/secrets/valkey_auth)" ' + ' '.join(cmd) return compose('exec', '-T', 'valkey', 'sh', '-c', script).strip() def basic(username, password): return 'Basic ' + base64.b64encode((username + ':' + password).encode()).decode() def wait_ready(c, timeout=60): deadline = time.monotonic() + timeout while time.monotonic() < deadline: try: c.call('GET', '/health/ready', schema='Health') return except Failure: time.sleep(0.5) raise Failure('API not ready after %ds' % timeout) def test(name): def deco(fn): fn.test_name = name return fn return deco @test('wrong password / unknown user -> одинаковый 401 Basic') def t_wrong_password(c, a): alice = c.user('wp') u = alice['registration']['username'] r1 = c.call('POST', '/api/v1/auth/sessions', 401, headers={'Authorization': basic(u, 'wrong-password-123')}) r2 = c.call('POST', '/api/v1/auth/sessions', 401, headers={'Authorization': basic('no_such_' + uuid.uuid4().hex[:8], 'wrong-password-123')}) check(r1['json']['error']['message'] == r2['json']['error']['message'], 'responses reveal user existence') c.call('POST', '/api/v1/auth/sessions', 401, headers={'Authorization': 'Basic !!!not-base64'}) c.call('POST', '/api/v1/auth/sessions', 401, headers={'Authorization': 'Bearer ' + alice['token']}) t1 = min(c.call('POST', '/api/v1/auth/sessions', 401, headers={'Authorization': basic(u, 'wrong-password-123')})['elapsed'] for _ in range(5)) t2 = min(c.call('POST', '/api/v1/auth/sessions', 401, headers={'Authorization': basic('nouser_x1', 'wrong-password-123')})['elapsed'] for _ in range(5)) check(t2 > t1 * 0.5, 'unknown user answered much faster than known one (%.3f vs %.3f s)' % (t2, t1)) return 'known=%.0fms unknown=%.0fms' % (t1 * 1000, t2 * 1000) @test('пароль: 12..128 code points, двоеточие допустимо') def t_password_rules(c, a): name = 'u_pw_' + uuid.uuid4().hex[:10] c.call('POST', '/api/v1/users', 400, body={'username': name, 'display_name': 'x', 'password': 'short'}) c.call('POST', '/api/v1/users', 400, body={'username': name, 'display_name': 'x', 'password': 'x' * 129}) pw = 'пароль:с:двоеточием🙂' c.call('POST', '/api/v1/users', 201, body={'username': name, 'display_name': 'x', 'password': pw}, schema='User') c.call('POST', '/api/v1/auth/sessions', 201, headers={'Authorization': basic(name, pw)}, schema='Session') @test('logout: 204, затем 401 на запрос и повторный logout') def t_logout(c, a): alice = c.user('lo') second = c.call('POST', '/api/v1/auth/sessions', 201, headers={'Authorization': basic(alice['registration']['username'], alice['registration']['password'])}, schema='Session')['json'] c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=alice) c.call('GET', '/api/v1/users/me', 401, actor=alice) c.call('DELETE', '/api/v1/auth/sessions/current', 401, actor=alice) c.call('GET', '/api/v1/users/me', 200, headers={'Authorization': 'Bearer ' + second['token']}, schema='User') @test('токен непрозрачный, в БД только SHA-256, пароль — Argon2id') def t_storage_format(c, a): alice = c.user('st') tok = alice['token'] check(len(tok) >= 43 and alice['id'] not in tok and alice['registration']['username'] not in tok, 'token is not opaque') check(psql("SELECT count(*) FROM sessions WHERE encode(token_hash,'escape') LIKE '%%%s%%'" % tok[:20]) == '0', 'raw token stored') h = psql("SELECT password_hash FROM users WHERE id = '%s'" % alice['id']) check(h.startswith('$argon2id$v=19$m=19456,t=2,p=1$'), 'password is not Argon2id') check(alice['registration']['password'] not in h, 'plaintext password stored') @test('expiry при SESSION_TTL_SECONDS=2 (api-short-ttl :8081)') def t_expiry(c, a): compose('--profile', 'ttl', 'up', '-d', 'api-short-ttl') short = Client(a.short_ttl_url, timeout=10) wait_ready(short) u = short.user('ttl') sess_exp = psql("SELECT extract(epoch FROM s.expires_at - s.created_at) FROM sessions s JOIN users u ON u.id=s.user_id WHERE u.id='%s'" % u['id']) check(abs(float(sess_exp) - 2) < 0.01, 'TTL is not 2 seconds: ' + sess_exp) short.call('GET', '/api/v1/users/me', 200, actor=u, schema='User') time.sleep(2.5) short.call('GET', '/api/v1/users/me', 401, actor=u) c.call('GET', '/api/v1/users/me', 401, actor=u) compose('--profile', 'ttl', 'stop', 'api-short-ttl') @test('гонка регистрации одного username: ровно один 201') def t_register_race(c, a): name = 'u_race_' + uuid.uuid4().hex[:10] codes = [] lock = threading.Lock() barrier = threading.Barrier(20) def worker(): cl = Client(a.base_url, timeout=30) barrier.wait() r = cl.call('POST', '/api/v1/users', [201, 409], body={'username': name, 'display_name': 'race', 'password': 'Race-password-123'}) with lock: codes.append(r['status']) threads = [threading.Thread(target=worker) for _ in range(20)] for t in threads: t.start() for t in threads: t.join() check(codes.count(201) == 1 and codes.count(409) == 19, 'unexpected codes: %s' % sorted(codes)) check(psql("SELECT count(*) FROM users WHERE username='%s'" % name) == '1', 'duplicate user rows') return '201x1, 409x19' @test('rollback составной операции создания чата') def t_chat_rollback(c, a): alice = c.user('rb') title = 'rollback-' + uuid.uuid4().hex before = int(psql("SELECT xact_rollback FROM pg_stat_database WHERE datname='messenger'")) c.call('POST', '/api/v1/chats', 400, actor=alice, body={'title': title, 'member_ids': [alice['id'], str(uuid.uuid4())]}) check(psql("SELECT count(*) FROM chats WHERE title='%s'" % title) == '0', 'chat row survived failed transaction') check(psql("SELECT count(*) FROM chat_members m JOIN chats ch ON ch.id=m.chat_id WHERE ch.title='%s'" % title) == '0', 'orphan members') deadline = time.monotonic() + 15 while True: after = int(psql("SELECT xact_rollback FROM pg_stat_database WHERE datname='messenger'")) if after > before or time.monotonic() > deadline: break time.sleep(0.5) check(after > before, 'no rollback observed in pg_stat_database') listed = c.call('GET', '/api/v1/chats?limit=100', actor=alice, schema='ChatPage')['json'] check(all(x['title'] != title for x in listed['items']), 'rolled back chat is visible') return 'xact_rollback %d -> %d' % (before, after) @test('параметризованные запросы: SQL-символы сохраняются как текст') def t_sql_text(c, a): alice, bob, eve, chat = c.bootstrap() payload = "'); DROP TABLE messages; -- \\x00? $1 %s" m = c.message(alice, chat, payload)['json'] check(m['text'] == payload, 'text changed') page = c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages', actor=bob, schema='MessagePage')['json'] check(page['items'][-1]['text'] == payload, 'text changed on read') c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages?cursor=' + quote("' OR 1=1 --"), 400, actor=bob) @test('пагинация 250 сообщений: без потерь и повторов, курсор чужого чата -> 400') def t_pagination(c, a): alice, bob, eve, chat = c.bootstrap() sent = [c.message(alice, chat, 'm%03d' % i)['json']['id'] for i in range(250)] seen, cursor, pages = [], None, 0 while True: p = '/api/v1/chats/' + chat['id'] + '/messages?limit=37' + ('&cursor=' + quote(cursor, safe='') if cursor else '') page = c.call('GET', p, actor=bob, schema='MessagePage')['json'] seen += [m['id'] for m in page['items']] pages += 1 cursor = page['next_cursor'] if not cursor: break last_cursor = cursor check(seen == sent, 'history order/content mismatch') other = c.call('POST', '/api/v1/chats', 201, actor=alice, body={'title': 'другой', 'member_ids': [alice['id']]}, schema='Chat')['json'] c.call('GET', '/api/v1/chats/' + other['id'] + '/messages?cursor=' + quote(last_cursor, safe=''), 400, actor=alice) return '%d pages' % pages @test('Valkey реально кэширует сессии (TTL <= 60s), FLUSHALL не разлогинивает') def t_cache_used(c, a): alice = c.user('cache') c.call('GET', '/api/v1/users/me', actor=alice, schema='User') keys = valkey('--scan', '--pattern', "'sess:*'").split() check(keys, 'no session keys in valkey') ttl = int(valkey('TTL', keys[0])) check(0 < ttl <= 60, 'cache TTL not bounded: %d' % ttl) check(alice['token'] not in ' '.join(keys), 'raw token used as cache key') valkey('FLUSHALL') c.call('GET', '/api/v1/users/me', 200, actor=alice, schema='User') check(valkey('DBSIZE') != '0', 'cache was not repopulated after miss') return 'keys=%d ttl=%ds' % (len(keys), ttl) @test('отказ Valkey: API обходит кэш, logout -> 503 (не оставляет отозванный токен в кэше)') def t_valkey_down(c, a): alice = c.user('vk') chat = c.call('POST', '/api/v1/chats', 201, actor=alice, body={'title': 'vk', 'member_ids': [alice['id']]}, schema='Chat')['json'] compose('stop', 'valkey') try: c.call('GET', '/health/ready', 200, schema='Health') t0 = time.monotonic() c.call('GET', '/api/v1/users/me', 200, actor=alice, schema='User') c.message(alice, chat, 'без кэша') fresh = c.user('vk2') c.call('GET', '/api/v1/users/me', 401, headers={'Authorization': 'Bearer ' + 'x' * 43}) c.call('GET', '/api/v1/users/me', 401) c.call('DELETE', '/api/v1/auth/sessions/current', 503, actor=alice) elapsed = time.monotonic() - t0 finally: compose('start', 'valkey') deadline = time.monotonic() + 30 while True: r = c.call('DELETE', '/api/v1/auth/sessions/current', [204, 503], actor=alice) if r['status'] == 204: break check(time.monotonic() < deadline, 'logout did not recover after valkey start') time.sleep(0.5) c.call('GET', '/api/v1/users/me', 401, actor=alice) c.call('GET', '/api/v1/users/me', 200, actor=fresh, schema='User') return 'degraded requests took %.2fs total' % elapsed @test('отказ PostgreSQL: ready -> 503, бизнес-запросы 503, аноним/неизвестный токен не проходят') def t_postgres_down(c, a): alice, bob, eve, chat = c.bootstrap() msg = c.message(alice, chat, 'до отказа БД')['json'] compose('stop', 'postgres') try: c.call('GET', '/health/live', 200, schema='Health') c.call('GET', '/health/ready', 503) c.call('GET', '/api/v1/chats', 503, actor=alice) c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 503, actor=alice, body={'text': 'во время отказа'}) c.call('GET', '/api/v1/users/me', 401) c.call('GET', '/api/v1/users/me', 503, headers={'Authorization': 'Bearer ' + 'y' * 43}) c.call('POST', '/api/v1/users', 503, body={'username': 'u_' + uuid.uuid4().hex[:10], 'display_name': 'x', 'password': 'Outage-password-1'}) finally: compose('start', 'postgres') wait_ready(c) page = c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages', actor=bob, schema='MessagePage')['json'] check(page['items'] == [msg], 'data changed across database outage') TESTS = [t_wrong_password, t_password_rules, t_logout, t_storage_format, t_expiry, t_register_race, t_chat_rollback, t_sql_text, t_pagination, t_cache_used, t_valkey_down, t_postgres_down] OUTAGE = {t_valkey_down, t_postgres_down, t_expiry} def main(): if hasattr(sys.stdout, 'reconfigure'): sys.stdout.reconfigure(encoding='utf-8') p = argparse.ArgumentParser(description='Собственные проверки lab2 (уровень 4)') p.add_argument('--base-url', default='http://127.0.0.1:8080') p.add_argument('--short-ttl-url', default='http://127.0.0.1:8081') p.add_argument('--skip-outages', action='store_true') p.add_argument('--json', help='записать сводку в файл') a = p.parse_args() c = Client(a.base_url, timeout=15) wait_ready(c) failed = 0 for t in TESTS: if a.skip_outages and t in OUTAGE: continue start = time.monotonic() try: note = t(c, a) or '' status = 'PASS' except Failure as e: note, status = str(e), 'FAIL' failed += 1 dur = time.monotonic() - start RESULTS.append({'test': t.test_name, 'status': status, 'seconds': round(dur, 2), 'note': note}) print('%-4s %6.2fs %s%s' % (status, dur, t.test_name, (' [' + note + ']') if note else '')) if a.json: Path(a.json).write_text(json.dumps(RESULTS, ensure_ascii=False, indent=2) + '\n', encoding='utf-8') print('%d/%d passed' % (len(RESULTS) - failed, len(RESULTS))) sys.exit(1 if failed else 0) if __name__ == '__main__': main()