package in.partake.view; import static in.partake.view.util.Helper.escapeTwitterResponse; import static in.partake.view.util.Helper.h; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; import in.partake.view.util.Helper; import org.junit.Assert; import org.junit.Test; public class ViewHelperTest { @Test public void testToEscapeHTML() { Assert.assertEquals("", h("")); Assert.assertEquals("", h(null)); Assert.assertEquals(" ", h(" ")); Assert.assertEquals("test", h("test")); Assert.assertEquals("&", h("&")); Assert.assertEquals("<", h("<")); Assert.assertEquals(">", h(">")); Assert.assertEquals(""", h("\"")); Assert.assertEquals("'", h("\'")); Assert.assertEquals("", h(Character.toString('\0'))); // NUL Assert.assertEquals("", h(Character.toString('\u202E'))); // RLO Assert.assertEquals("\t", h("\t")); Assert.assertEquals("\r", h("\r")); Assert.assertEquals("\n", h("\n")); Assert.assertEquals("\r\n", h("\r\n")); Assert.assertEquals("@screen_name", h("@screen_name")); Assert.assertEquals("#hashtag", h("#hashtag")); Assert.assertEquals("&<tag>", h("&<tag>")); Assert.assertEquals("漢字&ひらがな", h("漢字&ひらがな")); Assert.assertEquals("サロゲートペア→𠮟", h("サロゲートペア→𠮟")); Assert.assertEquals("double "quoted"", h("double \"quoted\"")); Assert.assertEquals("<script></script>", h("<script></script>")); } @Test public void testToCleanupHTML() throws Exception { String dirty = "<script>alert('hoge')</script>"; String sanity = Helper.cleanupHTML(dirty); assertThat(sanity.contains("script"), is(false)); } @Test public void testToCleanupAnchor() throws Exception { String dirty = "<a href=\"http://partake.in/events/5ca0b9eb-10d3-437f-bdc2-712727d1d518\">link</a>"; String sanity = Helper.cleanupHTML(dirty); assertThat(sanity, is(equalTo(dirty))); } @Test public void testEscapeTwitterResponse() { Assert.assertEquals("", escapeTwitterResponse("")); Assert.assertEquals("", escapeTwitterResponse(null)); Assert.assertEquals(" ", escapeTwitterResponse(" ")); Assert.assertEquals("test", escapeTwitterResponse("test")); Assert.assertEquals("&", escapeTwitterResponse("&")); Assert.assertEquals("<", escapeTwitterResponse("<")); // Twitterは返さないはずだけど脆弱性につながると嫌だし念の為にテスト Assert.assertEquals(">", escapeTwitterResponse(">")); // Twitterは返さないはずだけど脆弱性につながると嫌だし念の為にテスト Assert.assertEquals("<", escapeTwitterResponse("<")); Assert.assertEquals(">", escapeTwitterResponse(">")); Assert.assertEquals(""", escapeTwitterResponse("\"")); Assert.assertEquals("'", escapeTwitterResponse("\'")); Assert.assertEquals("", escapeTwitterResponse(Character.toString('\0'))); // NUL Assert.assertEquals("", escapeTwitterResponse(Character.toString('\u202E'))); // RLO Assert.assertEquals("\t", escapeTwitterResponse("\t")); Assert.assertEquals("\r", escapeTwitterResponse("\r")); Assert.assertEquals("\n", escapeTwitterResponse("\n")); Assert.assertEquals("\r\n", escapeTwitterResponse("\r\n")); Assert.assertEquals("@screen_name", escapeTwitterResponse("@screen_name")); Assert.assertEquals("#hashtag", escapeTwitterResponse("#hashtag")); Assert.assertEquals("&<tag>", escapeTwitterResponse("&<tag>")); Assert.assertEquals("漢字&ひらがな", escapeTwitterResponse("漢字&ひらがな")); Assert.assertEquals("サロゲートペア→𠮟", escapeTwitterResponse("サロゲートペア→𠮟")); Assert.assertEquals("double "quoted"", escapeTwitterResponse("double \"quoted\"")); Assert.assertEquals("<script></script>", escapeTwitterResponse("<script></script>")); } }