📄 Update copyright lines with correct filenames
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / text / SoneTextParserTest.kt
1 /*
2  * Sone - SoneTextParserTest.kt - Copyright Â© 2011–2019 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.text
19
20 import com.google.inject.Guice.*
21 import net.pterodactylus.sone.data.*
22 import net.pterodactylus.sone.data.impl.*
23 import net.pterodactylus.sone.database.*
24 import net.pterodactylus.sone.test.*
25 import org.hamcrest.MatcherAssert.*
26 import org.hamcrest.Matchers.*
27 import kotlin.test.*
28
29 /**
30  * JUnit test case for [SoneTextParser].
31  */
32 class SoneTextParserTest {
33
34         private val soneTextParser = SoneTextParser(null, null)
35
36         @Test
37         fun `basic operation`() {
38                 val parts = soneTextParser.parse("Test.", null)
39                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java), equalTo("Test."))
40         }
41
42         @Test
43         fun `empty lines at start and end are stripped`() {
44                 val parts = soneTextParser.parse("\nTest.\n\n", null)
45                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java), equalTo("Test."))
46         }
47
48         @Test
49         fun `duplicate empty lines in the text are stripped`() {
50                 val parts = soneTextParser.parse("\nTest.\n\n\nTest.", null)
51                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java), equalTo("Test.\n\nTest."))
52         }
53
54         @Test
55         fun `consecutive lines are separated by linefeed`() {
56                 val parts = soneTextParser.parse("Text.\nText", null)
57                 assertThat("Part Text", convertText(parts), equalTo("Text.\nText"))
58         }
59
60         @Test
61         fun `freenet links have the freenet prefix removed`() {
62                 val parts = soneTextParser.parse("freenet:KSK@gpl.txt", null)
63                 assertThat("Part Text", convertText(parts), equalTo("[KSK@gpl.txt|KSK@gpl.txt|gpl.txt]"))
64         }
65
66         @Test
67         fun `only the first item in a line is prefixed with a line break`() {
68                 val parts = soneTextParser.parse("Text.\nKSK@gpl.txt and KSK@gpl.txt", null)
69                 assertThat("Part Text", convertText(parts), equalTo("Text.\n[KSK@gpl.txt|KSK@gpl.txt|gpl.txt] and [KSK@gpl.txt|KSK@gpl.txt|gpl.txt]"))
70         }
71
72         @Test
73         fun `sone link with too short sone ID is rendered as plain text`() {
74                 val parts = soneTextParser.parse("sone://too-short", null)
75                 assertThat("Part Text", convertText(parts), equalTo("sone://too-short"))
76         }
77
78         @Test
79         fun `sone link is rendered correctly if sone is not present`() {
80                 val parser = SoneTextParser(AbsentSoneProvider(), null)
81                 val parts = parser.parse("sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU", null)
82                 assertThat("Part Text", convertText(parts), equalTo("[Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU]"))
83         }
84
85         @Test
86         fun `sone and post can be parsed from the same text`() {
87                 val parser = SoneTextParser(TestSoneProvider(), TestPostProvider())
88                 val parts = parser.parse("Text sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU more text post://f3757817-b45a-497a-803f-9c5aafc10dc6 even more text", null)
89                 assertThat("Part Text", convertText(parts), equalTo("Text [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] more text [Post|f3757817-b45a-497a-803f-9c5aafc10dc6|text] even more text"))
90         }
91
92         @Test
93         fun `post link is rendered as plain text if post ID is too short`() {
94                 val parts = soneTextParser.parse("post://too-short", null)
95                 assertThat("Part Text", convertText(parts), equalTo("post://too-short"))
96         }
97
98         @Test
99         fun `post link is rendered correctly if post is present`() {
100                 val parser = SoneTextParser(null, TestPostProvider())
101                 val parts = parser.parse("post://f3757817-b45a-497a-803f-9c5aafc10dc6", null)
102                 assertThat("Part Text", convertText(parts), equalTo("[Post|f3757817-b45a-497a-803f-9c5aafc10dc6|text]"))
103         }
104
105         @Test
106         fun `post link is rendered as plain text if post is absent`() {
107                 val parser = SoneTextParser(null, AbsentPostProvider())
108                 val parts = parser.parse("post://f3757817-b45a-497a-803f-9c5aafc10dc6", null)
109                 assertThat("Part Text", convertText(parts), equalTo("post://f3757817-b45a-497a-803f-9c5aafc10dc6"))
110         }
111
112         @Test
113         fun `name of freenet link does not contain url parameters`() {
114                 val parts = soneTextParser.parse("KSK@gpl.txt?max-size=12345", null)
115                 assertThat("Part Text", convertText(parts), equalTo("[KSK@gpl.txt?max-size=12345|KSK@gpl.txt|gpl.txt]"))
116         }
117
118         @Test
119         fun `trailing slash in freenet link is removed for name`() {
120                 val parts = soneTextParser.parse("KSK@gpl.txt/", null)
121                 assertThat("Part Text", convertText(parts), equalTo("[KSK@gpl.txt/|KSK@gpl.txt/|gpl.txt]"))
122         }
123
124         @Test
125         fun `last meta string of freenet link is used as name`() {
126                 val parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING", null)
127                 assertThat("Part Text", convertText(parts), equalTo("[CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING|CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING|COPYING]"))
128         }
129
130         @Test
131         fun `freenet link without meta strings and doc name gets first nine characters of key as name`() {
132                 val parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8", null)
133                 assertThat("Part Text", convertText(parts), equalTo("[CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|CHK@qM1nm]"))
134         }
135
136         @Test
137         fun `malformed key is rendered as plain text`() {
138                 val parts = soneTextParser.parse("CHK@qM1nmgU", null)
139                 assertThat("Part Text", convertText(parts), equalTo("CHK@qM1nmgU"))
140         }
141
142         @Test
143         fun `https link has its paths shortened`() {
144                 val parts = soneTextParser.parse("https://test.test/some-long-path/file.txt", null)
145                 assertThat("Part Text", convertText(parts), equalTo("[https://test.test/some-long-path/file.txt|https://test.test/some-long-path/file.txt|test.test/…/file.txt]"))
146         }
147
148         @Test
149         fun `http links have their last slash removed`() {
150                 val parts = soneTextParser.parse("http://test.test/test/", null)
151                 assertThat("Part Text", convertText(parts), equalTo("[http://test.test/test/|http://test.test/test/|test.test/…]"))
152         }
153
154         @Test
155         fun `www prefix is removed for hostname with two dots and no path`() {
156                 val parts = soneTextParser.parse("http://www.test.test", null)
157                 assertThat("Part Text", convertText(parts), equalTo("[http://www.test.test|http://www.test.test|test.test]"))
158         }
159
160         @Test
161         fun `www prefix is removed for hostname with two dots and a path`() {
162                 val parts = soneTextParser.parse("http://www.test.test/test.html", null)
163                 assertThat("Part Text", convertText(parts), equalTo("[http://www.test.test/test.html|http://www.test.test/test.html|test.test/test.html]"))
164         }
165
166         @Test
167         fun `hostname is kept intact if not beginning with www`() {
168                 val parts = soneTextParser.parse("http://test.test.test/test.html", null)
169                 assertThat("Part Text", convertText(parts), equalTo("[http://test.test.test/test.html|http://test.test.test/test.html|test.test.test/test.html]"))
170         }
171
172         @Test
173         fun `hostname with one dot but no slash is kept intact`() {
174                 val parts = soneTextParser.parse("http://test.test", null)
175                 assertThat("Part Text", convertText(parts), equalTo("[http://test.test|http://test.test|test.test]"))
176         }
177
178         @Test
179         fun `url parameters are removed for http links`() {
180                 val parts = soneTextParser.parse("http://test.test?foo=bar", null)
181                 assertThat("Part Text", convertText(parts), equalTo("[http://test.test?foo=bar|http://test.test?foo=bar|test.test]"))
182         }
183
184         @Test
185         fun `empty string is parsed correctly`() {
186                 val parts = soneTextParser.parse("", null)
187                 assertThat("Part Text", convertText(parts), equalTo(""))
188         }
189
190         @Test
191         fun `links are parsed in correct order`() {
192                 val parts = soneTextParser.parse("KSK@ CHK@", null)
193                 assertThat("Part Text", convertText(parts), equalTo("KSK@ CHK@"))
194         }
195
196         @Test
197         fun `invalid ssk and usk link is parsed as text`() {
198                 val parts = soneTextParser.parse("SSK@a USK@a", null)
199                 assertThat("Part Text", convertText(parts), equalTo("SSK@a USK@a"))
200         }
201
202         @Test
203         fun `ssk without document name is parsed correctly`() {
204                 val parts = soneTextParser.parse(
205                                 "SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8", null)
206                 assertThat("Part Text", convertText(parts),
207                                 equalTo("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|"
208                                                 + "SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|"
209                                                 + "SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU]"))
210         }
211
212         @Test
213         fun `ssk link without context is not trusted`() {
214                 val parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", null)
215                 assertThat("Part Text", convertText(parts), equalTo("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test]"))
216         }
217
218         @Test
219         fun `ssk link with context without sone is not trusted`() {
220                 val context = SoneTextParserContext(null)
221                 val parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context)
222                 assertThat("Part Text", convertText(parts), equalTo("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test]"))
223         }
224
225         @Test
226         fun `ssk link with context with different sone is not trusted`() {
227                 val context = SoneTextParserContext(IdOnlySone("DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU"))
228                 val parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context)
229                 assertThat("Part Text", convertText(parts), equalTo("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test]"))
230         }
231
232         @Test
233         fun `ssk link with context with correct sone is trusted`() {
234                 val context = SoneTextParserContext(IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"))
235                 val parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context)
236                 assertThat("Part Text", convertText(parts), equalTo("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|trusted|SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test]"))
237         }
238
239         @Test
240         fun `usk link with context with correct sone is trusted`() {
241                 val context = SoneTextParserContext(IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"))
242                 val parts = soneTextParser.parse("USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0", context)
243                 assertThat("Part Text", convertText(parts), equalTo("[USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0|trusted|USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0|test]"))
244         }
245
246         @Test
247         fun `usk links with backlinks is parsed correctly`() {
248                 val context = SoneTextParserContext(IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"))
249                 val parts = soneTextParser.parse("USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0/../../../USK@nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI,DuQSUZiI~agF8c-6tjsFFGuZ8eICrzWCILB60nT8KKo,AQACAAE/sone/78/", context)
250                 assertThat("Part Text", convertText(parts), equalTo("[USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0|trusted|USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0|test]"))
251         }
252
253         @Test
254         fun `test basic ksk links`() {
255                 val parts: Iterable<Part> = soneTextParser.parse("KSK@gpl.txt", null)
256                 assertThat("Part Text", convertText(parts, FreenetLinkPart::class.java), equalTo("[KSK@gpl.txt|KSK@gpl.txt|gpl.txt]"))
257         }
258
259         @Test
260         fun `embedded ksk links are parsed correctly`() {
261                 val parts = soneTextParser.parse("Link is KSK@gpl.txt\u200b.", null)
262                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java, FreenetLinkPart::class.java), equalTo("Link is [KSK@gpl.txt|KSK@gpl.txt|gpl.txt]\u200b."))
263         }
264
265         @Test
266         fun `embedded ksk links and line breaks are parsed correctly`() {
267                 val parts = soneTextParser.parse("Link is KSK@gpl.txt\nKSK@test.dat\n", null)
268                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java, FreenetLinkPart::class.java), equalTo("Link is [KSK@gpl.txt|KSK@gpl.txt|gpl.txt]\n[KSK@test.dat|KSK@test.dat|test.dat]"))
269         }
270
271         @Test
272         fun `ksk links with backlinks are parsed correctly`() {
273                 val parts = soneTextParser.parse("KSK@gallery/../Sone/imageBrowser.html?album=30c930ee-97cd-11e9-bd44-f3e595768b77", null)
274                 assertThat("Part Text", convertText(parts, FreenetLinkPart::class.java), equalTo("[KSK@gallery|KSK@gallery|gallery]"))
275         }
276
277         @Test
278         fun `test empty lines and sone links`() {
279                 val soneTextParser = SoneTextParser(TestSoneProvider(), null)
280
281                 /* check basic links. */
282                 val parts = soneTextParser.parse("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff.", null)
283                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java, SonePart::class.java), equalTo("Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff."))
284         }
285
286         @Test
287         fun `test empy http links`() {
288                 val soneTextParser = SoneTextParser(TestSoneProvider(), null)
289
290                 /* check empty http links. */
291                 val parts = soneTextParser.parse("Some text. Empty link: http:// â€“ nice!", null)
292                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java), equalTo("Some text. Empty link: http:// â€“ nice!"))
293         }
294
295         @Test
296         fun `http link without parens ends at next closing paren`() {
297                 val parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc) â€“ nice!", null)
298                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java, LinkPart::class.java), equalTo("Some text (and a link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]) â€“ nice!"))
299         }
300
301         @Test
302         fun `usk link ends at first non numeric non slash character after version number`() {
303                 val parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0). Nice", null)
304                 assertThat("Part Text", convertText(parts), equalTo("Some link ([USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0|USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0|test]). Nice"))
305         }
306
307         @Test
308         fun `usk link with filename shows the filename`() {
309                 val parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0/images/image.jpg). Nice", null)
310                 assertThat("Part Text", convertText(parts), equalTo("Some link ([USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0/images/image.jpg|USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0/images/image.jpg|image.jpg]). Nice"))
311         }
312
313         @Test
314         fun `usk link without filename but ending in slash shows the path`() {
315                 val parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0/). Nice", null)
316                 assertThat("Part Text", convertText(parts), equalTo("Some link ([USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0|USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0|test]). Nice"))
317         }
318
319         @Test
320         fun `http link with opened and closed parens ends at next closing paren`() {
321                 val parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc_(def)) â€“ nice!", null)
322                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java, LinkPart::class.java), equalTo("Some text (and a link: [http://example.sone/abc_(def)|http://example.sone/abc_(def)|example.sone/abc_(def)]) â€“ nice!"))
323         }
324
325         @Test
326         fun `punctuation is ignored at end of link before whitespace`() {
327                 val parts = soneTextParser.parse("Some text and a link: http://example.sone/abc. Nice!", null)
328                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java, LinkPart::class.java), equalTo("Some text and a link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]. Nice!"))
329         }
330
331         @Test
332         fun `multiple punctuation characters are ignored at end of link before whitespace`() {
333                 val parts = soneTextParser.parse("Some text and a link: http://example.sone/abc... Nice!", null)
334                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java, LinkPart::class.java), equalTo("Some text and a link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]... Nice!"))
335         }
336
337         @Test
338         fun `commas are ignored at end of link before whitespace`() {
339                 val parts = soneTextParser.parse("Some text and a link: http://example.sone/abc, nice!", null)
340                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java, LinkPart::class.java), equalTo("Some text and a link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc], nice!"))
341         }
342
343         @Test
344         fun `exclamation marks are ignored at end of link before whitespace`() {
345                 val parts = soneTextParser.parse("A link: http://example.sone/abc!", null)
346                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java, LinkPart::class.java), equalTo("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]!"))
347         }
348
349         @Test
350         fun `question marks are ignored at end of link before whitespace`() {
351                 val parts = soneTextParser.parse("A link: http://example.sone/abc?", null)
352                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java, LinkPart::class.java), equalTo("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]?"))
353         }
354
355         @Test
356         fun `correct freemail address is linked to correctly`() {
357                 val parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null)
358                 assertThat("Part Text", convertText(parts), equalTo("Mail me at [Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]!"))
359         }
360
361         @Test
362         fun `freemail address with invalid freemail id is parsed as text`() {
363                 val parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!", null)
364                 assertThat("Part Text", convertText(parts), equalTo("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!"))
365         }
366
367         @Test
368         fun `freemail address with invalid sized freemail id is parsed as text`() {
369                 val parts = soneTextParser.parse("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null)
370                 assertThat("Part Text", convertText(parts), equalTo("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"))
371         }
372
373         @Test
374         fun `freemail address without local part is parsed as text`() {
375                 val parts = soneTextParser.parse("     @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null)
376                 assertThat("Part Text", convertText(parts), equalTo("     @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"))
377         }
378
379         @Test
380         fun `local part of freemail address can contain letters digits minus dot underscore`() {
381                 val parts = soneTextParser.parse("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null)
382                 assertThat("Part Text", convertText(parts), equalTo("[Freemail|ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]"))
383         }
384
385         private fun convertText(parts: Iterable<Part>, vararg validClasses: Class<*>): String {
386                 if (validClasses.isNotEmpty()) {
387                         assertThat(parts.map { it.javaClass }.distinct() - validClasses.distinct(), empty())
388                 }
389                 return parts.joinToString("") { part ->
390                         when (part) {
391                                 is PlainTextPart -> part.text
392                                 is FreenetLinkPart -> "[${part.link}|${if (part.trusted) "trusted|" else ""}${part.title}|${part.text}]"
393                                 is FreemailPart -> "[Freemail|${part.emailLocalPart}|${part.freemailId}|${part.identityId}]"
394                                 is LinkPart -> "[${part.link}|${part.title}|${part.text}]"
395                                 is SonePart -> "[Sone|${part.sone.id}]"
396                                 is PostPart -> "[Post|${part.post.id}|${part.post.text}]"
397                                 else -> throw NoSuchElementException()
398                         }
399                 }
400         }
401
402         @Test
403         fun `parser can be created by guice`() {
404                 val injector = createInjector(
405                                 SoneProvider::class.isProvidedByMock(),
406                                 PostProvider::class.isProvidedByMock()
407                 )
408                 assertThat(injector.getInstance<SoneTextParser>(), notNullValue())
409         }
410
411         /**
412          * Mock Sone provider.
413          */
414         private open class TestSoneProvider : SoneProvider {
415
416                 override val soneLoader = this::getSone
417                 override val sones: Collection<Sone> = emptySet()
418                 override val localSones: Collection<Sone> = emptySet()
419                 override val remoteSones: Collection<Sone> = emptySet()
420
421                 override fun getSone(soneId: String): Sone? = IdOnlySone(soneId)
422
423         }
424
425         private class AbsentSoneProvider : TestSoneProvider() {
426
427                 override fun getSone(soneId: String): Sone? = null
428
429         }
430
431         private open class TestPostProvider : PostProvider {
432
433                 override fun getPost(postId: String): Post? {
434                         return object : Post {
435                                 override val id = postId
436                                 override fun isLoaded() = false
437                                 override fun getSone() = null
438                                 override fun getRecipientId() = null
439                                 override fun getRecipient() = null
440                                 override fun getTime() = 0L
441                                 override fun getText() = "text"
442                                 override fun isKnown() = false
443                                 override fun setKnown(known: Boolean) = null
444                         }
445                 }
446
447                 override fun getPosts(soneId: String) = emptySet<Post>()
448                 override fun getDirectedPosts(recipientId: String) = emptySet<Post>()
449
450         }
451
452         private class AbsentPostProvider : TestPostProvider() {
453
454                 override fun getPost(postId: String): Post? = null
455
456         }
457
458 }