From: David ‘Bombe’ Roden Date: Tue, 12 Feb 2019 19:44:05 +0000 (+0100) Subject: Fix parsing of SSK links without document name X-Git-Tag: v79^2~115 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=a2369ab3b5b0f3f6dcc9d9fd1fcdc02c3003da64;p=Sone.git Fix parsing of SSK links without document name --- diff --git a/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt b/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt index 64125c1..690ce6d 100644 --- a/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt +++ b/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt @@ -15,7 +15,6 @@ import net.pterodactylus.sone.text.LinkType.POST import net.pterodactylus.sone.text.LinkType.SONE import net.pterodactylus.sone.text.LinkType.SSK import net.pterodactylus.sone.text.LinkType.USK -import net.pterodactylus.sone.utils.let import org.bitpedia.util.Base32 import java.net.MalformedURLException @@ -68,7 +67,9 @@ class SoneTextParser(private val soneProvider: SoneProvider?, private val postPr } SSK, USK -> try { - FreenetLinkPart(link, FreenetURI(link).docName, trusted = context?.routingKey?.contentEquals(FreenetURI(link).routingKey) == true) + FreenetURI(link).let { uri -> + uri.docName ?: "${uri.keyType}@${uri.routingKey.freenetBase64}" + }.let { FreenetLinkPart(link, it, trusted = context?.routingKey?.contentEquals(FreenetURI(link).routingKey) == true) } } catch (e: MalformedURLException) { PlainTextPart(link) } @@ -195,3 +196,5 @@ private fun isPunctuation(char: Char) = char in punctuationChars private val whitespace = Regex("[\\u000a\u0020\u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u200c\u200d\u202f\u205f\u2060\u2800\u3000]") private data class NextLink(val position: Int, val linkType: LinkType, val link: String, val remainder: String) + +private val ByteArray.freenetBase64 get() = Base64.encode(this)!! diff --git a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java index ff2a175..46eecb7 100644 --- a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java +++ b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java @@ -211,6 +211,17 @@ public class SoneTextParserTest { } @Test + public void sskWithoutDocumentNameIsParsedCorrectly() { + Iterable parts = soneTextParser.parse( + "SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8", + null); + assertThat("Part Text", convertText(parts), + is("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|" + + "SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|" + + "SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU]")); + } + + @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|SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test]"));