From: David ‘Bombe’ Roden Date: Thu, 6 Oct 2016 17:33:09 +0000 (+0200) Subject: Increase test coverage for Sone text parser X-Git-Tag: 0.9.6^2~35 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=88c18e062171323f0c986e3e86261cadd9fe3b08;p=Sone.git Increase test coverage for Sone text parser --- diff --git a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java index b8d2fb4..74099f5 100644 --- a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java +++ b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java @@ -25,8 +25,10 @@ import static org.hamcrest.Matchers.notNullValue; import java.io.IOException; import java.util.Collection; +import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.impl.IdOnlySone; +import net.pterodactylus.sone.database.PostProvider; import net.pterodactylus.sone.database.SoneProvider; import com.google.common.base.Function; @@ -61,6 +63,175 @@ public class SoneTextParserTest { assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test.\n\nTest.")); } + @Test + public void consecutiveLinesAreSeparatedByLinefeed() { + Iterable parts = soneTextParser.parse("Text.\nText", null); + assertThat("Part Text", convertText(parts), is("Text.\nText")); + } + + @Test + public void freenetLinksHaveTheFreenetPrefixRemoved() { + Iterable parts = soneTextParser.parse("freenet:KSK@gpl.txt", null); + assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt|gpl.txt|gpl.txt]")); + } + + @Test + public void onlyTheFirstItemInALineIsPrefixedWithALineBreak() { + Iterable parts = soneTextParser.parse("Text.\nKSK@gpl.txt and KSK@gpl.txt", null); + assertThat("Part Text", convertText(parts), is("Text.\n[KSK@gpl.txt|gpl.txt|gpl.txt] and [KSK@gpl.txt|gpl.txt|gpl.txt]")); + } + + @Test + public void soneLinkWithTooShortSoneIdIsRenderedAsPlainText() { + Iterable parts = soneTextParser.parse("sone://too-short", null); + assertThat("Part Text", convertText(parts), is("sone://too-short")); + } + + @Test + public void soneLinkIsRenderedCorrectlyIfSoneIsNotPresent() { + SoneTextParser parser = new SoneTextParser(new AbsentSoneProvider(), null); + Iterable parts = parser.parse("sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU", null); + assertThat("Part Text", convertText(parts), is("[Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU]")); + } + + @Test + public void postLinkIsRenderedAsPlainTextIfPostIdIsTooShort() { + Iterable parts = soneTextParser.parse("post://too-short", null); + assertThat("Part Text", convertText(parts), is("post://too-short")); + } + + @Test + public void postLinkIsRenderedCorrectlyIfPostIsPresent() { + SoneTextParser parser = new SoneTextParser(null, new TestPostProvider()); + Iterable parts = parser.parse("post://f3757817-b45a-497a-803f-9c5aafc10dc6", null); + assertThat("Part Text", convertText(parts), is("[Post|f3757817-b45a-497a-803f-9c5aafc10dc6|text]")); + } + + @Test + public void postLinkIsRenderedAsPlainTextIfPostIsAbsent() { + SoneTextParser parser = new SoneTextParser(null, new AbsentPostProvider()); + Iterable parts = parser.parse("post://f3757817-b45a-497a-803f-9c5aafc10dc6", null); + assertThat("Part Text", convertText(parts), is("post://f3757817-b45a-497a-803f-9c5aafc10dc6")); + } + + @Test + public void nameOfFreenetLinkDoesNotContainUrlParameters() { + Iterable parts = soneTextParser.parse("KSK@gpl.txt?max-size=12345", null); + assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt?max-size=12345|gpl.txt|gpl.txt]")); + } + + @Test + public void trailingSlashInFreenetLinkIsRemovedForName() { + Iterable parts = soneTextParser.parse("KSK@gpl.txt/", null); + assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt/|gpl.txt|gpl.txt]")); + } + + @Test + public void lastMetaStringOfFreenetLinkIsUsedAsName() { + Iterable parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING", null); + assertThat("Part Text", convertText(parts), is("[CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING|COPYING|COPYING]")); + } + + @Test + public void freenetLinkWithoutMetaStringsAndDocNameGetsFirstNineCharactersOfKeyAsName() { + Iterable parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8", null); + assertThat("Part Text", convertText(parts), is("[CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|CHK@qM1nm|CHK@qM1nm]")); + } + + @Test + public void malformedKeyIsRenderedAsPlainText() { + Iterable parts = soneTextParser.parse("CHK@qM1nmgU", null); + assertThat("Part Text", convertText(parts), is("CHK@qM1nmgU")); + } + + @Test + public void httpsLinkHasItsPathsShortened() { + Iterable parts = soneTextParser.parse("https://test.test/some-long-path/file.txt", null); + assertThat("Part Text", convertText(parts), is("[https://test.test/some-long-path/file.txt|test.test/…/file.txt|test.test/…/file.txt]")); + } + + @Test + public void httpLinksHaveTheirLastSlashRemoved() { + Iterable parts = soneTextParser.parse("http://test.test/test/", null); + assertThat("Part Text", convertText(parts), is("[http://test.test/test/|test.test/…|test.test/…]")); + } + + @Test + public void wwwPrefixIsRemovedForHostnameWithTwoDotsAndNoPath() { + Iterable parts = soneTextParser.parse("http://www.test.test", null); + assertThat("Part Text", convertText(parts), is("[http://www.test.test|test.test|test.test]")); + } + + @Test + public void wwwPrefixIsRemovedForHostnameWithTwoDotsAndAPath() { + Iterable parts = soneTextParser.parse("http://www.test.test/test.html", null); + assertThat("Part Text", convertText(parts), is("[http://www.test.test/test.html|test.test/test.html|test.test/test.html]")); + } + + @Test + public void hostnameIsKeptIntactIfNotBeginningWithWww() { + Iterable parts = soneTextParser.parse("http://test.test.test/test.html", null); + assertThat("Part Text", convertText(parts), is("[http://test.test.test/test.html|test.test.test/test.html|test.test.test/test.html]")); + } + + @Test + public void hostnameWithOneDotButNoSlashIsKeptIntact() { + Iterable parts = soneTextParser.parse("http://test.test", null); + assertThat("Part Text", convertText(parts), is("[http://test.test|test.test|test.test]")); + } + + @Test + public void urlParametersAreRemovedForHttpLinks() { + Iterable parts = soneTextParser.parse("http://test.test?foo=bar", null); + assertThat("Part Text", convertText(parts), is("[http://test.test?foo=bar|test.test|test.test]")); + } + + @Test + public void emptyStringIsParsedCorrectly() { + Iterable parts = soneTextParser.parse("", null); + assertThat("Part Text", convertText(parts), is("")); + } + + @Test + public void linksAreParsedInCorrectOrder() { + Iterable parts = soneTextParser.parse("KSK@ CHK@", null); + assertThat("Part Text", convertText(parts), is("KSK@ CHK@")); + } + + @Test + public void sskLinkWithoutContextIsNotTrusted() { + Iterable parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", null); + assertThat("Part Text", convertText(parts), is("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test|test]")); + } + + @Test + public void sskLinkWithContextWithoutSoneIsNotTrusted() { + SoneTextParserContext context = new SoneTextParserContext(null, null); + Iterable parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context); + assertThat("Part Text", convertText(parts), is("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test|test]")); + } + + @Test + public void sskLinkWithContextWithDifferentSoneIsNotTrusted() { + SoneTextParserContext context = new SoneTextParserContext(null, new IdOnlySone("DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU")); + Iterable parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context); + assertThat("Part Text", convertText(parts), is("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test|test]")); + } + + @Test + public void sskLinkWithContextWithCorrectSoneIsTrusted() { + SoneTextParserContext context = new SoneTextParserContext(null, new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU")); + Iterable parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context); + assertThat("Part Text", convertText(parts), is("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|trusted|test|test]")); + } + + @Test + public void uskLinkWithContextWithCorrectSoneIsTrusted() { + SoneTextParserContext context = new SoneTextParserContext(null, new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU")); + Iterable parts = soneTextParser.parse("USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0", context); + assertThat("Part Text", convertText(parts), is("[USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0|trusted|test|test]")); + } + @SuppressWarnings("static-method") @Test public void testKSKLinks() throws IOException { @@ -168,6 +339,9 @@ public class SoneTextParserTest { } else if (part instanceof SonePart) { SonePart sonePart = (SonePart) part; text.append("[Sone|").append(sonePart.getSone().getId()).append(']'); + } else if (part instanceof PostPart) { + PostPart postPart = (PostPart) part; + text.append("[Post|").append(postPart.getPost().getId()).append("|").append(postPart.getPost().getText()).append("]"); } } return text.toString(); @@ -224,4 +398,86 @@ public class SoneTextParserTest { } + private static class AbsentSoneProvider extends TestSoneProvider { + + @Override + public Optional getSone(String soneId) { + return Optional.absent(); + } + + } + + private static class TestPostProvider implements PostProvider { + + @Override + public Optional getPost(final String postId) { + return Optional.of(new Post() { + @Override + public String getId() { + return postId; + } + + @Override + public boolean isLoaded() { + return false; + } + + @Override + public Sone getSone() { + return null; + } + + @Override + public Optional getRecipientId() { + return null; + } + + @Override + public Optional getRecipient() { + return null; + } + + @Override + public long getTime() { + return 0; + } + + @Override + public String getText() { + return "text"; + } + + @Override + public boolean isKnown() { + return false; + } + + @Override + public Post setKnown(boolean known) { + return null; + } + }); + } + + @Override + public Collection getPosts(String soneId) { + return null; + } + + @Override + public Collection getDirectedPosts(String recipientId) { + return null; + } + + } + + private static class AbsentPostProvider extends TestPostProvider { + + @Override + public Optional getPost(String postId) { + return Optional.absent(); + } + + } + }