From 6f0ab9c9a20b96890ed60bc168b28d29f5db39e5 Mon Sep 17 00:00:00 2001 From: Kostya Date: Sat, 10 Oct 2026 14:12:55 +0300 Subject: server: accept event file uploads held entirely in memory Add POST /api/upload: a logged-in user sends a Schema v3 file as a multipart part, its events join the market for everyone, and the uploader becomes their Market Maker. It uses the Servlet API's own multipart support, as the spec asks, and hands the part's stream to the v3 loader, so validation errors (bad XML, broken Exercise 1 rules, a name already in the market) come back as 400 with their reason. As in Exercise 1, a file whose name does not end in .xml is refused before it is read, since a request need not come from our client. The spec warns that the grading server cannot write files, and Tomcat writes any multipart part larger than @MultipartConfig's fileSizeThreshold to a temp file. That threshold defaults to 0, so a plain upload servlet always touches the disk. Cap uploads at 1 MB and put the threshold above the whole request limit, so every upload stays in memory. Bigger files get 413, requests without a session 401, and non-multipart bodies 400. Tomcat deletes spooled parts once the request ends, so comparing the files before and after an upload cannot catch spooling. The test instead watches the webapp's temp directory for any file being created while a near-limit upload runs. It fails if the threshold is set back to 0. --- .../test/java/market/guess/server/TestServer.java | 8 +- .../test/java/market/guess/server/UploadTest.java | 140 +++++++++++++++++++++ 2 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 server/src/test/java/market/guess/server/UploadTest.java (limited to 'server/src/test') diff --git a/server/src/test/java/market/guess/server/TestServer.java b/server/src/test/java/market/guess/server/TestServer.java index 9b77819..1ab87ae 100644 --- a/server/src/test/java/market/guess/server/TestServer.java +++ b/server/src/test/java/market/guess/server/TestServer.java @@ -9,6 +9,7 @@ import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import org.apache.catalina.core.StandardContext; import org.apache.catalina.startup.Tomcat; import org.apache.catalina.webresources.DirResourceSet; import org.apache.catalina.webresources.StandardRoot; @@ -16,6 +17,7 @@ import org.apache.tomcat.util.scan.StandardJarScanner; final class TestServer implements AutoCloseable { private final Tomcat tomcat; + private final StandardContext ctx; final String api; TestServer(Path base) throws Exception { @@ -25,7 +27,7 @@ final class TestServer implements AutoCloseable { tomcat.getConnector(); var docBase = Files.createDirectories(base.resolve("webapp")); - var ctx = tomcat.addWebapp("/guess-market", docBase.toString()); + ctx = (StandardContext) tomcat.addWebapp("/guess-market", docBase.toString()); ctx.setParentClassLoader(TestServer.class.getClassLoader()); ((StandardJarScanner) ctx.getJarScanner()).setScanClassPath(false); var resources = new StandardRoot(ctx); @@ -41,6 +43,10 @@ final class TestServer implements AutoCloseable { api = "http://localhost:" + tomcat.getConnector().getLocalPort() + "/guess-market/api"; } + Path tempDir() { + return Path.of(ctx.getWorkPath()); + } + static HttpClient client() { return HttpClient.newBuilder().cookieHandler(new CookieManager()).build(); } diff --git a/server/src/test/java/market/guess/server/UploadTest.java b/server/src/test/java/market/guess/server/UploadTest.java new file mode 100644 index 0000000..2602b7f --- /dev/null +++ b/server/src/test/java/market/guess/server/UploadTest.java @@ -0,0 +1,140 @@ +package market.guess.server; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.FileSystems; +import java.nio.file.StandardWatchEventKinds; +import java.util.concurrent.TimeUnit; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +class UploadTest { + @TempDir static Path base; + private static TestServer server; + + @BeforeAll + static void start() throws Exception { + server = new TestServer(base); + } + + @AfterAll + static void stop() throws Exception { + server.close(); + } + + private static HttpClient loggedIn(String name) throws Exception { + var client = TestServer.client(); + assertEquals(200, server.post(client, "/login", "username", name).statusCode()); + return client; + } + + private static HttpResponse upload(HttpClient client, String fileName, byte[] content) + throws Exception { + var boundary = "gm-test-boundary"; + var body = new ByteArrayOutputStream(); + body.writeBytes( + ("--" + + boundary + + "\r\nContent-Disposition: form-data; name=\"file\"; filename=\"" + + fileName + + "\"\r\nContent-Type: application/xml\r\n\r\n") + .getBytes(StandardCharsets.UTF_8)); + body.writeBytes(content); + body.writeBytes(("\r\n--" + boundary + "--\r\n").getBytes(StandardCharsets.UTF_8)); + var req = + server + .request("/upload") + .header("Content-Type", "multipart/form-data; boundary=" + boundary) + .POST(HttpRequest.BodyPublishers.ofByteArray(body.toByteArray())) + .build(); + return client.send(req, HttpResponse.BodyHandlers.ofString()); + } + + private static byte[] sample(String name) throws Exception { + return Files.readAllBytes(Path.of("test-data/ex-3", name)); + } + + + private static int eventCount() throws Exception { + var body = server.get(TestServer.client(), "/health").body(); + return Integer.parseInt(body.replaceAll(".*\"events\":([0-9]+).*", "$1")); + } + + @Test + void uploadsAccumulateWithUploaderAsMarketMaker() throws Exception { + var startCount = eventCount(); + var dana = loggedIn("Dana"); + var first = upload(dana, "small.xml", sample("small.xml")); + assertEquals(200, first.statusCode(), first.body()); + assertTrue(first.body().contains("\"eventsLoaded\":1"), first.body()); + + var eli = loggedIn("Eli"); + var second = upload(eli, "multiple.xml", sample("multiple.xml")); + assertEquals(200, second.statusCode(), second.body()); + assertTrue(second.body().contains("\"eventsLoaded\":3"), second.body()); + + assertEquals(startCount + 4, eventCount()); + + var again = upload(eli, "small.xml", sample("small.xml")); + assertEquals(400, again.statusCode()); + assertTrue(again.body().contains("NAME_TAKEN"), again.body()); + assertTrue(again.body().contains("Mujtaba is Dead"), again.body()); + } + + @Test + void uploadNeverTouchesTheDisk() throws Exception { + var client = loggedIn("Fay"); + var padding = ""; + var xml = + new String(sample("small.xml"), StandardCharsets.UTF_8) + .replace("Mujtaba is Dead", "Big Upload") + .replace("", padding + ""); + + try (var watch = FileSystems.getDefault().newWatchService()) { + server.tempDir().register(watch, StandardWatchEventKinds.ENTRY_CREATE); + var res = upload(client, "big.xml", xml.getBytes(StandardCharsets.UTF_8)); + assertEquals(200, res.statusCode(), res.body()); + var key = watch.poll(1, TimeUnit.SECONDS); + assertNull(key == null ? null : key.pollEvents(), "upload wrote to " + server.tempDir()); + } + } + + @Test + void rejectsAnonymousOversizedAndEmptyUploads() throws Exception { + var anon = upload(TestServer.client(), "small.xml", sample("small.xml")); + assertEquals(401, anon.statusCode()); + + var client = loggedIn("Gil"); + var huge = upload(client, "huge.xml", new byte[(int) UploadServlet.MAX_BYTES + 1]); + assertEquals(413, huge.statusCode(), huge.body()); + + var notMultipart = server.post(client, "/upload", "file", ""); + assertEquals(400, notMultipart.statusCode()); + assertTrue(notMultipart.body().contains("NOT_MULTIPART"), notMultipart.body()); + + var malformed = upload(client, "bad.xml", "".getBytes(StandardCharsets.UTF_8)); + assertEquals(400, malformed.statusCode()); + assertTrue(malformed.body().contains("XML_PARSE_ERROR"), malformed.body()); + } + + @Test + void refusesFilesNotNamedXml() throws Exception { + var client = loggedIn("Hila"); + var before = eventCount(); + var txt = upload(client, "small.txt", sample("small.xml")); + assertEquals(400, txt.statusCode(), txt.body()); + assertTrue(txt.body().contains("NOT_XML"), txt.body()); + assertEquals(before, eventCount()); + } +} -- cgit v1.2.3