forked from mtusi2026/mtusi2026lab5
43 lines
1.8 KiB
Python
43 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|
"""413, 415, 400 invalid_image — лимиты bytes/pixels/MIME."""
|
|
import sys
|
|
from pathlib import Path
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
from tests.client import Client, check
|
|
from student_tests.common import png_bytes, ok, fail
|
|
|
|
def main():
|
|
c = Client()
|
|
a, b, e, ch = c.bootstrap()
|
|
url = '/api/v1/chats/' + ch['id'] + '/attachments?filename=x.bin'
|
|
|
|
# 413: > 10 MiB
|
|
big = b'\x89PNG\r\n\x1a\n' + b'\x00' * (11 * 1024 * 1024)
|
|
r = c.call('POST', url, 413, actor=a, raw=big, headers={'Content-Type': 'image/png'})
|
|
ok('413 payload_too_large')
|
|
|
|
# 415: text/plain
|
|
r = c.call('POST', url, 415, actor=a, raw=b'hello', headers={'Content-Type': 'text/plain'})
|
|
ok('415 unsupported_media_type')
|
|
|
|
# 415: клиент говорит image/png, но содержимое не PNG/JPEG по DetectContentType
|
|
r = c.call('POST', url, 415, actor=a, raw=b'\x00' * 100, headers={'Content-Type': 'image/png'})
|
|
ok('415 mismatched content (client MIME not trusted)')
|
|
|
|
# 400: PNG-сигнатура валидна (DetectContentType -> image/png),
|
|
# но IHDR/IDAT битые -> DecodeConfig падает -> invalid_image (400)
|
|
broken_png = b'\x89PNG\r\n\x1a\n' + b'\x00' * 40
|
|
r = c.call('POST', url, 400, actor=a, raw=broken_png, headers={'Content-Type': 'image/png'})
|
|
ok('400 invalid image (signature ok, header broken)')
|
|
|
|
# 400: пиксели > MAX_IMAGE_PIXELS (если выставите 1e8 — этот тест не сработает;
|
|
# по умолчанию 40 MP, значит нужно 7000x7000 ≈ 49 MP)
|
|
huge = png_bytes(7000, 7000)
|
|
r = c.call('POST', url, 400, actor=a, raw=huge, headers={'Content-Type': 'image/png'})
|
|
ok('400 too many pixels')
|
|
|
|
if __name__ == '__main__':
|
|
try:
|
|
main()
|
|
except Exception as exc:
|
|
fail(str(exc)) |