import http.client import tempfile import threading import unittest from pathlib import Path from server import MAX_FILE_BYTES, detect_image_type, load_allowed_targets, make_server PNG = b"\x89PNG\r\n\x1a\n" + b"test-image" def multipart( filename: str, payload: bytes, boundary: str = "test-boundary", metadata: dict[str, str] | None = None, ) -> tuple[bytes, str]: fields = metadata if metadata is not None else { "callsign": "W1AW", "name": "Test Operator", "email": "operator@example.com", "description": "A test image.", } body = b"".join( ( f"--{boundary}\r\n" f'Content-Disposition: form-data; name="{name}"\r\n\r\n' f"{value}\r\n" ).encode("utf-8") for name, value in fields.items() ) + ( f"--{boundary}\r\n" f'Content-Disposition: form-data; name="image"; filename="{filename}"\r\n' "Content-Type: application/octet-stream\r\n\r\n" ).encode() + payload + f"\r\n--{boundary}--\r\n".encode() return body, f"multipart/form-data; boundary={boundary}" class ServerTests(unittest.TestCase): def setUp(self): self.temp = tempfile.TemporaryDirectory() self.upload_dir = Path(self.temp.name) / "private" self.server = make_server( "127.0.0.1", 0, self.upload_dir, frozenset({"logo-contest", "911u19jasdl"}) ) self.thread = threading.Thread(target=self.server.serve_forever, daemon=True) self.thread.start() self.port = self.server.server_address[1] def tearDown(self): self.server.shutdown() self.server.server_close() self.thread.join() self.temp.cleanup() def request(self, method, path, body=None, headers=None): connection = http.client.HTTPConnection("127.0.0.1", self.port, timeout=3) connection.request(method, path, body=body, headers=headers or {}) response = connection.getresponse() data = response.read() connection.close() return response, data def test_accepts_png_and_stores_it_under_target(self): body, content_type = multipart("photo.png", PNG) response, data = self.request( "POST", "/logo-contest", body, {"Content-Type": content_type, "Content-Length": str(len(body))} ) self.assertEqual(response.status, 201, data) target_dir = self.upload_dir / "logo-contest" files = list(target_dir.iterdir()) self.assertEqual(len(files), 2) image = next(path for path in files if path.suffix == ".png") metadata = next(path for path in files if path.suffix == ".txt") self.assertEqual(image.read_bytes(), PNG) self.assertEqual(metadata.stem, image.stem) self.assertEqual( metadata.read_text(encoding="utf-8"), "Callsign: W1AW\nName: Test Operator\nEmail: operator@example.com\n" "Description:\nA test image.\n", ) self.assertNotIn("photo", image.name) self.assertEqual(image.stat().st_mode & 0o777, 0o600) self.assertEqual(metadata.stat().st_mode & 0o777, 0o600) self.assertEqual(target_dir.stat().st_mode & 0o777, 0o700) hidden_response, _ = self.request("GET", f"/logo-contest/{image.name}") hidden_metadata_response, _ = self.request("GET", f"/logo-contest/{metadata.name}") self.assertEqual(hidden_response.status, 404) self.assertEqual(hidden_metadata_response.status, 404) def test_root_is_rejected_but_target_page_is_available(self): root_response, _ = self.request("GET", "/") target_response, page = self.request("GET", "/911u19jasdl") self.assertEqual(root_response.status, 418) self.assertEqual(target_response.status, 200) self.assertIn(b"Blind Uploader", page) self.assertLess(page.index(b'name="callsign"'), page.index(b'name="image"')) self.assertIn(b'name="description" maxlength="1000"', page) def test_upload_to_root_is_rejected_as_teapot(self): body, content_type = multipart("photo.png", PNG) response, _ = self.request( "POST", "/", body, {"Content-Type": content_type, "Content-Length": str(len(body))} ) self.assertEqual(response.status, 418) def test_targets_are_stored_in_separate_directories(self): for target in ("logo-contest", "911u19jasdl"): body, content_type = multipart("photo.png", PNG) response, _ = self.request( "POST", f"/{target}", body, {"Content-Type": content_type, "Content-Length": str(len(body))} ) self.assertEqual(response.status, 201) self.assertEqual(len(list((self.upload_dir / target).iterdir())), 2) def test_invalid_or_nested_targets_are_rejected(self): for path in ("/bad.target", "/nested/target", "/%2e%2e", "/healthz"): body, content_type = multipart("photo.png", PNG) response, _ = self.request( "POST", path, body, {"Content-Type": content_type, "Content-Length": str(len(body))} ) self.assertEqual(response.status, 404) def test_unlisted_valid_target_is_rejected(self): response, _ = self.request("GET", "/invented-target") self.assertEqual(response.status, 404) def test_loads_target_allowlist(self): targets_file = Path(self.temp.name) / "targets.txt" targets_file.write_text("# campaigns\n/logo-contest\n\n/911u19jasdl\n", encoding="utf-8") self.assertEqual(load_allowed_targets(targets_file), {"logo-contest", "911u19jasdl"}) def test_rejects_invalid_allowlist_entry(self): targets_file = Path(self.temp.name) / "targets.txt" targets_file.write_text("/nested/target\n", encoding="utf-8") with self.assertRaises(ValueError): load_allowed_targets(targets_file) def test_rejects_non_image_content(self): body, content_type = multipart("fake.jpg", b"not really an image") response, _ = self.request( "POST", "/logo-contest", body, {"Content-Type": content_type, "Content-Length": str(len(body))} ) self.assertEqual(response.status, 400) self.assertFalse((self.upload_dir / "logo-contest").exists()) def test_rejects_missing_metadata(self): body, content_type = multipart("photo.png", PNG, metadata={}) response, _ = self.request( "POST", "/logo-contest", body, {"Content-Type": content_type, "Content-Length": str(len(body))} ) self.assertEqual(response.status, 400) self.assertFalse((self.upload_dir / "logo-contest").exists()) def test_rejects_description_over_1000_characters(self): body, content_type = multipart( "photo.png", PNG, metadata={ "callsign": "W1AW", "name": "Test Operator", "email": "operator@example.com", "description": "x" * 1001, }, ) response, _ = self.request( "POST", "/logo-contest", body, {"Content-Type": content_type, "Content-Length": str(len(body))} ) self.assertEqual(response.status, 400) self.assertFalse((self.upload_dir / "logo-contest").exists()) def test_rejects_oversized_request_before_reading_body(self): response, _ = self.request( "POST", "/logo-contest", b"", {"Content-Type": "multipart/form-data; boundary=x", "Content-Length": str(MAX_FILE_BYTES + 1024 * 1024 + 1)} ) self.assertEqual(response.status, 413) def test_unknown_paths_are_not_served(self): response, _ = self.request("GET", "/server.py") self.assertEqual(response.status, 404) def test_detects_supported_signatures(self): self.assertEqual(detect_image_type(b"\xff\xd8\xffrest"), (".jpg", "image/jpeg")) self.assertEqual(detect_image_type(PNG), (".png", "image/png")) self.assertEqual(detect_image_type(b"\x00\x00\x00\x18ftypheic\x00\x00\x00\x00heic"), (".heic", "image/heic")) if __name__ == "__main__": unittest.main()