#!/usr/bin/env python3 """Student negative and concurrency checks for the lab 4 attachment API.""" from concurrent.futures import ThreadPoolExecutor import http.client import hashlib import json from pathlib import Path import re import struct import subprocess import sys import threading import time import zlib ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / 'tests')) from client import Client, arguments, check, run from urllib.parse import quote, urlsplit MAX_UPLOAD_BYTES = 10 * 1024 * 1024 PARALLEL_UPLOADS = 10 ANSI_ESCAPE = re.compile(r'\x1b\[[0-?]*[ -/]*[@-~]') def png_chunk(kind, data): return ( struct.pack('>I', len(data)) + kind + data + struct.pack('>I', zlib.crc32(kind + data) & 0xffffffff) ) def random_png(width=1600, height=1600): seed = 0x4C414234 pixels = bytearray(width * height) for index in range(len(pixels)): seed = (1664525 * seed + 1013904223) & 0xffffffff pixels[index] = seed >> 24 rows = b''.join( b'\x00' + pixels[row * width:(row + 1) * width] for row in range(height) ) header = struct.pack('>IIBBBBB', width, height, 8, 0, 0, 0, 0) return ( b'\x89PNG\r\n\x1a\n' + png_chunk(b'IHDR', header) + png_chunk(b'IDAT', zlib.compress(rows)) + png_chunk(b'IEND', b'') ) def memory_bytes(value): match = re.match(r'\s*([\d.]+)\s*(B|KiB|MiB|GiB)', value) check(match is not None, 'could not parse docker stats memory value') factors = {'B': 1, 'KiB': 1024, 'MiB': 1024 ** 2, 'GiB': 1024 ** 3} return int(float(match.group(1)) * factors[match.group(2)]) def api_memory(container_ids): output = subprocess.run( ['docker', 'stats', '--no-stream', '--format', '{{.MemUsage}}', *container_ids], cwd=ROOT, check=True, capture_output=True, text=True, ).stdout lines = [line.strip() for line in output.splitlines() if line.strip()] check(len(lines) == len(container_ids), 'docker stats did not report every API replica') return sum(memory_bytes(line.split('/', 1)[0]) for line in lines) def make_upload(client, actor, chat_id, filename, content, content_type='image/png'): return client.call( 'POST', '/api/v1/chats/' + chat_id + '/attachments?filename=' + quote(filename, safe=''), 201, actor=actor, raw=content, headers={'Content-Type': content_type}, schema='Attachment', )['json'] def slow_upload(base_url, actor, chat_id, index, content): endpoint = urlsplit(base_url or 'http://localhost:8080') check(endpoint.scheme == 'http', 'memory test currently requires the local HTTP endpoint') connection = http.client.HTTPConnection( endpoint.hostname, endpoint.port or 80, timeout=30 ) path = ( '/api/v1/chats/' + chat_id + '/attachments?filename=' + quote('parallel-' + str(index) + '.png', safe='') ) try: connection.putrequest('POST', path) connection.putheader('Authorization', 'Bearer ' + actor['token']) connection.putheader('Accept', 'application/json') connection.putheader('Content-Type', 'image/png') connection.putheader('Content-Length', str(len(content))) connection.putheader('Connection', 'close') connection.endheaders() chunk_size = 64 * 1024 for offset in range(0, len(content), chunk_size): connection.send(content[offset:offset + chunk_size]) time.sleep(0.05) response = connection.getresponse() payload = json.loads(response.read()) check(response.status == 201, 'parallel upload did not return 201') check(bool(response.getheader('X-Request-Id')), 'parallel upload has no request ID') check(bool(response.getheader('X-Instance-Id')), 'parallel upload has no instance ID') from client import shape shape(payload, 'Attachment') return payload finally: connection.close() def main(): args = arguments('Attachment negative tests and 10-way upload memory measurement') client = Client(args.base_url, args.ca_file, timeout=30) alice, bob, eve, chat = client.bootstrap() fixture = (ROOT / 'tests' / 'fixtures' / 'sample.png').read_bytes() attachment = make_upload(client, alice, chat['id'], 'sample.png', fixture) large = b'x' * (MAX_UPLOAD_BYTES + 1) client.call( 'POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=large.png', 413, actor=alice, raw=large, headers={'Content-Type': 'image/png'}, ) pixel_header = ( b'\x89PNG\r\n\x1a\n' + png_chunk( b'IHDR', struct.pack('>IIBBBBB', 10000, 5000, 8, 0, 0, 0, 0), ) + png_chunk(b'IDAT', zlib.compress(b'')) + png_chunk(b'IEND', b'') ) client.call( 'POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=too-many-pixels.png', 413, actor=alice, raw=pixel_header, headers={'Content-Type': 'image/png'}, ) client.call( 'POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=broken.png', 415, actor=alice, raw=b'not a PNG image', headers={'Content-Type': 'image/png'}, ) client.call( 'POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=spoofed.jpg', 415, actor=alice, raw=fixture, headers={'Content-Type': 'image/jpeg'}, ) client.call( 'POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=..%2Fprivate.png', 400, actor=alice, raw=fixture, headers={'Content-Type': 'image/png'}, ) for path in ( '/api/v1/attachments/' + attachment['id'], '/api/v1/attachments/' + attachment['id'] + '/content', ): client.call('GET', path, 404, actor=eve) other_chat = client.call( 'POST', '/api/v1/chats', 201, actor=alice, body={'title': 'Другой чат', 'member_ids': [alice['id'], bob['id']]}, schema='Chat', )['json'] client.call( 'POST', '/api/v1/chats/' + other_chat['id'] + '/messages', 404, actor=alice, body={'text': 'Чужое вложение', 'attachment_ids': [attachment['id']]}, headers={'Idempotency-Key': 'cross-chat-attachment-check'}, ) content = random_png() check(0 < len(content) <= MAX_UPLOAD_BYTES, 'generated stress image exceeds upload limit') container_ids = subprocess.run( ['docker', 'compose', 'ps', '-q', 'api'], cwd=ROOT, check=True, capture_output=True, text=True, ).stdout.split() check(len(container_ids) >= 2, 'memory test requires at least two API containers') baseline = api_memory(container_ids) samples = [] latest = {} stats_process = subprocess.Popen( [ 'docker', 'stats', '--format', '{{.Name}}|{{.MemUsage}}', *container_ids, ], cwd=ROOT, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True, bufsize=1, ) def monitor(): for line in stats_process.stdout: line = ANSI_ESCAPE.sub('', line) parts = line.strip().split('|', 1) if len(parts) != 2: continue try: latest[parts[0]] = memory_bytes(parts[1].split('/', 1)[0]) except AssertionError: continue if len(latest) >= len(container_ids): samples.append(sum(latest.values())) monitor_thread = threading.Thread(target=monitor, daemon=True) monitor_thread.start() barrier = threading.Barrier(PARALLEL_UPLOADS) def worker(index): barrier.wait() return slow_upload( args.base_url, alice, chat['id'], index, content, ) started = time.monotonic() try: with ThreadPoolExecutor(max_workers=PARALLEL_UPLOADS) as pool: results = list(pool.map(worker, range(PARALLEL_UPLOADS))) finally: stats_process.terminate() stats_process.wait(timeout=5) monitor_thread.join(timeout=5) elapsed = time.monotonic() - started check(len(results) == PARALLEL_UPLOADS, 'not all simultaneous uploads completed') check(samples, 'Docker stats did not produce any in-flight memory samples') check(all(item['status'] == 'ready' for item in results), 'an upload was not ready') check(len({item['id'] for item in results}) == PARALLEL_UPLOADS, 'upload IDs were reused') peak = max(samples + [baseline]) report = { 'concurrent_uploads': PARALLEL_UPLOADS, 'image_size_bytes': len(content), 'image_sha256': hashlib.sha256(content).hexdigest(), 'elapsed_seconds': round(elapsed, 3), 'api_memory_baseline_bytes': baseline, 'api_memory_peak_sampled_bytes': peak, 'api_memory_sampled_delta_bytes': peak - baseline, 'memory_sample_interval_seconds': 1, 'memory_sample_count': len(samples), 'notes': 'Docker stats streams aggregate API-container memory at about one-second intervals; short peaks may be missed.', } output = ROOT / 'evidence' / 'attachment-concurrency.json' output.parent.mkdir(parents=True, exist_ok=True) output.write_text(json.dumps(report, indent=2) + '\n', encoding='utf-8') print(json.dumps(report, indent=2)) print('PASS: attachment limits, image validation, access controls and 10 concurrent uploads.') if __name__ == '__main__': run(main)