Fix parsing of SSK links without document name
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 12 Feb 2019 19:44:05 +0000 (20:44 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 12 Feb 2019 19:44:05 +0000 (20:44 +0100)
src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt
src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java

index 64125c1..690ce6d 100644 (file)
@@ -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)!!
index ff2a175..46eecb7 100644 (file)
@@ -211,6 +211,17 @@ public class SoneTextParserTest {
        }
 
        @Test
+       public void sskWithoutDocumentNameIsParsedCorrectly() {
+               Iterable<Part> 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<Part> 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]"));