diff options
Diffstat (limited to 'server/src/test/java/market/guess')
| -rw-r--r-- | server/src/test/java/market/guess/server/TestServer.java | 8 | ||||
| -rw-r--r-- | server/src/test/java/market/guess/server/UploadTest.java | 140 |
2 files changed, 147 insertions, 1 deletions
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; | |||
| 9 | import java.nio.charset.StandardCharsets; | 9 | import java.nio.charset.StandardCharsets; |
| 10 | import java.nio.file.Files; | 10 | import java.nio.file.Files; |
| 11 | import java.nio.file.Path; | 11 | import java.nio.file.Path; |
| 12 | import org.apache.catalina.core.StandardContext; | ||
| 12 | import org.apache.catalina.startup.Tomcat; | 13 | import org.apache.catalina.startup.Tomcat; |
| 13 | import org.apache.catalina.webresources.DirResourceSet; | 14 | import org.apache.catalina.webresources.DirResourceSet; |
| 14 | import org.apache.catalina.webresources.StandardRoot; | 15 | import org.apache.catalina.webresources.StandardRoot; |
| @@ -16,6 +17,7 @@ import org.apache.tomcat.util.scan.StandardJarScanner; | |||
| 16 | 17 | ||
| 17 | final class TestServer implements AutoCloseable { | 18 | final class TestServer implements AutoCloseable { |
| 18 | private final Tomcat tomcat; | 19 | private final Tomcat tomcat; |
| 20 | private final StandardContext ctx; | ||
| 19 | final String api; | 21 | final String api; |
| 20 | 22 | ||
| 21 | TestServer(Path base) throws Exception { | 23 | TestServer(Path base) throws Exception { |
| @@ -25,7 +27,7 @@ final class TestServer implements AutoCloseable { | |||
| 25 | tomcat.getConnector(); | 27 | tomcat.getConnector(); |
| 26 | 28 | ||
| 27 | var docBase = Files.createDirectories(base.resolve("webapp")); | 29 | var docBase = Files.createDirectories(base.resolve("webapp")); |
| 28 | var ctx = tomcat.addWebapp("/guess-market", docBase.toString()); | 30 | ctx = (StandardContext) tomcat.addWebapp("/guess-market", docBase.toString()); |
| 29 | ctx.setParentClassLoader(TestServer.class.getClassLoader()); | 31 | ctx.setParentClassLoader(TestServer.class.getClassLoader()); |
| 30 | ((StandardJarScanner) ctx.getJarScanner()).setScanClassPath(false); | 32 | ((StandardJarScanner) ctx.getJarScanner()).setScanClassPath(false); |
| 31 | var resources = new StandardRoot(ctx); | 33 | var resources = new StandardRoot(ctx); |
| @@ -41,6 +43,10 @@ final class TestServer implements AutoCloseable { | |||
| 41 | api = "http://localhost:" + tomcat.getConnector().getLocalPort() + "/guess-market/api"; | 43 | api = "http://localhost:" + tomcat.getConnector().getLocalPort() + "/guess-market/api"; |
| 42 | } | 44 | } |
| 43 | 45 | ||
| 46 | Path tempDir() { | ||
| 47 | return Path.of(ctx.getWorkPath()); | ||
| 48 | } | ||
| 49 | |||
| 44 | static HttpClient client() { | 50 | static HttpClient client() { |
| 45 | return HttpClient.newBuilder().cookieHandler(new CookieManager()).build(); | 51 | return HttpClient.newBuilder().cookieHandler(new CookieManager()).build(); |
| 46 | } | 52 | } |
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 @@ | |||
| 1 | package market.guess.server; | ||
| 2 | |||
| 3 | import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| 4 | import static org.junit.jupiter.api.Assertions.assertNull; | ||
| 5 | import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| 6 | |||
| 7 | import java.io.ByteArrayOutputStream; | ||
| 8 | import java.net.http.HttpClient; | ||
| 9 | import java.net.http.HttpRequest; | ||
| 10 | import java.net.http.HttpResponse; | ||
| 11 | import java.nio.charset.StandardCharsets; | ||
| 12 | import java.nio.file.Files; | ||
| 13 | import java.nio.file.Path; | ||
| 14 | import java.nio.file.FileSystems; | ||
| 15 | import java.nio.file.StandardWatchEventKinds; | ||
| 16 | import java.util.concurrent.TimeUnit; | ||
| 17 | import org.junit.jupiter.api.AfterAll; | ||
| 18 | import org.junit.jupiter.api.BeforeAll; | ||
| 19 | import org.junit.jupiter.api.Test; | ||
| 20 | import org.junit.jupiter.api.io.TempDir; | ||
| 21 | |||
| 22 | class UploadTest { | ||
| 23 | @TempDir static Path base; | ||
| 24 | private static TestServer server; | ||
| 25 | |||
| 26 | @BeforeAll | ||
| 27 | static void start() throws Exception { | ||
| 28 | server = new TestServer(base); | ||
| 29 | } | ||
| 30 | |||
| 31 | @AfterAll | ||
| 32 | static void stop() throws Exception { | ||
| 33 | server.close(); | ||
| 34 | } | ||
| 35 | |||
| 36 | private static HttpClient loggedIn(String name) throws Exception { | ||
| 37 | var client = TestServer.client(); | ||
| 38 | assertEquals(200, server.post(client, "/login", "username", name).statusCode()); | ||
| 39 | return client; | ||
| 40 | } | ||
| 41 | |||
| 42 | private static HttpResponse<String> upload(HttpClient client, String fileName, byte[] content) | ||
| 43 | throws Exception { | ||
| 44 | var boundary = "gm-test-boundary"; | ||
| 45 | var body = new ByteArrayOutputStream(); | ||
| 46 | body.writeBytes( | ||
| 47 | ("--" | ||
| 48 | + boundary | ||
| 49 | + "\r\nContent-Disposition: form-data; name=\"file\"; filename=\"" | ||
| 50 | + fileName | ||
| 51 | + "\"\r\nContent-Type: application/xml\r\n\r\n") | ||
| 52 | .getBytes(StandardCharsets.UTF_8)); | ||
| 53 | body.writeBytes(content); | ||
| 54 | body.writeBytes(("\r\n--" + boundary + "--\r\n").getBytes(StandardCharsets.UTF_8)); | ||
| 55 | var req = | ||
| 56 | server | ||
| 57 | .request("/upload") | ||
| 58 | .header("Content-Type", "multipart/form-data; boundary=" + boundary) | ||
| 59 | .POST(HttpRequest.BodyPublishers.ofByteArray(body.toByteArray())) | ||
| 60 | .build(); | ||
| 61 | return client.send(req, HttpResponse.BodyHandlers.ofString()); | ||
| 62 | } | ||
| 63 | |||
| 64 | private static byte[] sample(String name) throws Exception { | ||
| 65 | return Files.readAllBytes(Path.of("test-data/ex-3", name)); | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | private static int eventCount() throws Exception { | ||
| 70 | var body = server.get(TestServer.client(), "/health").body(); | ||
| 71 | return Integer.parseInt(body.replaceAll(".*\"events\":([0-9]+).*", "$1")); | ||
| 72 | } | ||
| 73 | |||
| 74 | @Test | ||
| 75 | void uploadsAccumulateWithUploaderAsMarketMaker() throws Exception { | ||
| 76 | var startCount = eventCount(); | ||
| 77 | var dana = loggedIn("Dana"); | ||
| 78 | var first = upload(dana, "small.xml", sample("small.xml")); | ||
| 79 | assertEquals(200, first.statusCode(), first.body()); | ||
| 80 | assertTrue(first.body().contains("\"eventsLoaded\":1"), first.body()); | ||
| 81 | |||
| 82 | var eli = loggedIn("Eli"); | ||
| 83 | var second = upload(eli, "multiple.xml", sample("multiple.xml")); | ||
| 84 | assertEquals(200, second.statusCode(), second.body()); | ||
| 85 | assertTrue(second.body().contains("\"eventsLoaded\":3"), second.body()); | ||
| 86 | |||
| 87 | assertEquals(startCount + 4, eventCount()); | ||
| 88 | |||
| 89 | var again = upload(eli, "small.xml", sample("small.xml")); | ||
| 90 | assertEquals(400, again.statusCode()); | ||
| 91 | assertTrue(again.body().contains("NAME_TAKEN"), again.body()); | ||
| 92 | assertTrue(again.body().contains("Mujtaba is Dead"), again.body()); | ||
| 93 | } | ||
| 94 | |||
| 95 | @Test | ||
| 96 | void uploadNeverTouchesTheDisk() throws Exception { | ||
| 97 | var client = loggedIn("Fay"); | ||
| 98 | var padding = "<!--" + "x".repeat((int) UploadServlet.MAX_BYTES - 4096) + "-->"; | ||
| 99 | var xml = | ||
| 100 | new String(sample("small.xml"), StandardCharsets.UTF_8) | ||
| 101 | .replace("Mujtaba is Dead", "Big Upload") | ||
| 102 | .replace("<GM-events>", padding + "<GM-events>"); | ||
| 103 | |||
| 104 | try (var watch = FileSystems.getDefault().newWatchService()) { | ||
| 105 | server.tempDir().register(watch, StandardWatchEventKinds.ENTRY_CREATE); | ||
| 106 | var res = upload(client, "big.xml", xml.getBytes(StandardCharsets.UTF_8)); | ||
| 107 | assertEquals(200, res.statusCode(), res.body()); | ||
| 108 | var key = watch.poll(1, TimeUnit.SECONDS); | ||
| 109 | assertNull(key == null ? null : key.pollEvents(), "upload wrote to " + server.tempDir()); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | @Test | ||
| 114 | void rejectsAnonymousOversizedAndEmptyUploads() throws Exception { | ||
| 115 | var anon = upload(TestServer.client(), "small.xml", sample("small.xml")); | ||
| 116 | assertEquals(401, anon.statusCode()); | ||
| 117 | |||
| 118 | var client = loggedIn("Gil"); | ||
| 119 | var huge = upload(client, "huge.xml", new byte[(int) UploadServlet.MAX_BYTES + 1]); | ||
| 120 | assertEquals(413, huge.statusCode(), huge.body()); | ||
| 121 | |||
| 122 | var notMultipart = server.post(client, "/upload", "file", "<Guess-Market/>"); | ||
| 123 | assertEquals(400, notMultipart.statusCode()); | ||
| 124 | assertTrue(notMultipart.body().contains("NOT_MULTIPART"), notMultipart.body()); | ||
| 125 | |||
| 126 | var malformed = upload(client, "bad.xml", "<Guess-Market>".getBytes(StandardCharsets.UTF_8)); | ||
| 127 | assertEquals(400, malformed.statusCode()); | ||
| 128 | assertTrue(malformed.body().contains("XML_PARSE_ERROR"), malformed.body()); | ||
| 129 | } | ||
| 130 | |||
| 131 | @Test | ||
| 132 | void refusesFilesNotNamedXml() throws Exception { | ||
| 133 | var client = loggedIn("Hila"); | ||
| 134 | var before = eventCount(); | ||
| 135 | var txt = upload(client, "small.txt", sample("small.xml")); | ||
| 136 | assertEquals(400, txt.statusCode(), txt.body()); | ||
| 137 | assertTrue(txt.body().contains("NOT_XML"), txt.body()); | ||
| 138 | assertEquals(before, eventCount()); | ||
| 139 | } | ||
| 140 | } | ||