# HG changeset patch # User Jaroslav Tulach # Date 1290840739 -3600 # Node ID 215b417aac9835e4f701a1423c33f21e79f0e7a9 # Parent aa1c63b5814951fd122e2a864dd5552237b1b5ec Supress invalid XML characters diff -r aa1c63b58149 -r 215b417aac98 freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java --- a/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java Sat Oct 23 22:51:43 2010 +0200 +++ b/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java Sat Nov 27 07:52:19 2010 +0100 @@ -24,8 +24,6 @@ import com.sun.jersey.core.util.MultivaluedMapImpl; import com.sun.net.httpserver.HttpServer; import cz.xelfi.quoridor.webidor.resources.Quoridor; -import cz.xelfi.quoridor.statistics.resources.Statistics; -import java.awt.Image; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -53,6 +51,7 @@ private static HttpServer stopAPI; private static HttpServer stopStatistics; private static Callable stop; + private WebResource webResource; private WebResource apiResource; private WebResource statResource; @@ -66,6 +65,11 @@ dir = File.createTempFile("quoridor", ".dir"); dir.delete(); dir.mkdirs(); + + File games = new File(dir, "games"); + copyResource("wrong.encoding", new File(games, "fdcc275c-55ad-4b7e-a776-bde3876b2f2c")); + + System.setProperty("quoridor.dir", dir.getPath()); stopAPI = Quoridor.start(9990); stopStatistics = Quoridor.start(9992); @@ -190,6 +194,9 @@ } @Test public void testCreateGameOK() throws Exception { + int origGames = new File(dir, "games").list().length; + + String logTest = apiResource.path("login"). queryParam("name", "test"). queryParam("password", "pes"). @@ -211,7 +218,7 @@ assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus()); String[] games = new File(dir, "games").list(); - assertEquals("One game exists", 1, games.length); + assertEquals("One new game", origGames + 1, games.length); if (txt.indexOf(games[0]) == -1) { fail(games[0] + " expected inside of:\n" + txt); @@ -268,5 +275,49 @@ // fail("There shall be difference in the streams. Read bytes " + cnt); } } + + @Test public void testGetWrongGame() throws Exception { + String logTest = apiResource.path("login"). + queryParam("name", "test"). + queryParam("password", "pes"). + accept(MediaType.TEXT_PLAIN). + put(String.class); + + String xml = apiResource.path("games/fdcc275c-55ad-4b7e-a776-bde3876b2f2c"). + queryParam("name", "test"). + queryParam("password", "pes"). + accept(MediaType.TEXT_XML). + get(String.class); + + if (xml.indexOf((char)0x1b) >= 0) { + fail(xml.replace((char)0x1b, 'X')); + } + + ClientResponse res = webResource.path("games/fdcc275c-55ad-4b7e-a776-bde3876b2f2c"). + queryParam("white", "Jarda"). + queryParam("black", "test"). + cookie(Cookie.valueOf("login=" + logTest)). + accept("text/html"). + get(ClientResponse.class); + final String txt = res.getEntity(String.class); + assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus()); + } + + private static void copyResource(String res, File file) throws IOException { + InputStream is = UITest.class.getResourceAsStream(res); + file.getParentFile().mkdirs(); + FileOutputStream os = new FileOutputStream(file); + byte[] arr = new byte[4092]; + for (;;) { + int len = is.read(arr); + if (len == -1) { + break; + } + os.write(arr, 0, len); + } + is.close(); + os.close(); + } + } diff -r aa1c63b58149 -r 215b417aac98 freemarkerdor/src/test/resources/cz/xelfi/quoridor/freemarkerdor/wrong.encoding --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/freemarkerdor/src/test/resources/cz/xelfi/quoridor/freemarkerdor/wrong.encoding Sat Nov 27 07:52:19 2010 +0100 @@ -0,0 +1,31 @@ +# white: petr.wolf +# black: jaroslav.tulach +# status: whiteMove +N S +N S +N S +HE5 HD6 +VF6 VE6 +HC5 VF8 +VB6 VC8 +HB3 VB4 +VC1 VA2 +HD3 VF4 +HF2 W +E W +S HF3 +HB1 VG2 +W N +# jaroslav.tulach@Thu Nov 25 17:25:22 GMT 2010: +# Zdá se, že černému již o moc cestu prodloužit nejde. +S N +S +# petr.wolf@Fri Nov 26 13:37:28 GMT 2010: +# Souhlasim. Nekam ale zmizelo tlacitko "vzdat" +# petr.wolf@Fri Nov 26 13:37:48 GMT 2010: +# Asi proto, ze jsi na tahu ty +... W +# jaroslav.tulach@Fri Nov 26 13:48:59 GMT 2010: +# Pro černého bylo požehnáním, že se bílý nebránil proti HF3. Pak už tam byla dvojitá hrozba a nešlo s tím moc dělat. + + diff -r aa1c63b58149 -r 215b417aac98 pom.xml --- a/pom.xml Sat Oct 23 22:51:43 2010 +0200 +++ b/pom.xml Sat Nov 27 07:52:19 2010 +0100 @@ -48,12 +48,12 @@ 1.3 1.4 - 1.17 + 1.18 1.0-SNAPSHOT 1.61 1.1 1.8 - 1.0 + 1.1 quoridor diff -r aa1c63b58149 -r 215b417aac98 wsdor/src/main/java/cz/xelfi/quoridor/webidor/Note.java --- a/wsdor/src/main/java/cz/xelfi/quoridor/webidor/Note.java Sat Oct 23 22:51:43 2010 +0200 +++ b/wsdor/src/main/java/cz/xelfi/quoridor/webidor/Note.java Sat Nov 27 07:52:19 2010 +0100 @@ -41,7 +41,7 @@ } public Note(String comment, Date when, String who) { - this.comment = comment; + this.comment = eliminateXMLChars(comment); this.when = new Date(when.getTime()); this.who = who; } @@ -93,5 +93,15 @@ return hash; } - + private static String eliminateXMLChars(String s) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < s.length(); i++) { + char ch = s.charAt(i); + if (ch < 32 && ch != '\n' || ch != '\r') { + } else { + sb.append((char)ch); + } + } + return sb.toString(); + } }