1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
package net.usebox.gemini.server
import java.nio.file.FileSystems
import scala.concurrent.duration._
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import akka.util.ByteString
class ServerSpec extends AnyFlatSpec with Matchers {
def getPath(value: String) = FileSystems.getDefault().getPath(value)
def getPath(root: String, dir: String) =
FileSystems
.getDefault()
.getPath(root, dir)
.normalize()
behavior of "validPath"
it should "return true for the emtpy path" in {
Server(TestData.conf).validPath("") shouldBe true
}
it should "return true for valid paths" in {
List("/", "/file", "/./", "/.", "/dir/", "/dir/../").foreach { p =>
Server(TestData.conf).validPath(p) shouldBe true
}
}
it should "return false for invalid paths" in {
List("/../", "/..", "/dir/../..", "/dir/../..", "/./../", "/./dir/.././../")
.foreach { p =>
Server(TestData.conf).validPath(p) shouldBe false
}
}
behavior of "guessMimeType using the internal resolver"
it should "resolve a known MIME type" in {
Server(TestData.conf)
.guessMimeType(
getPath("file.html"),
None
) shouldBe "text/html"
}
it should "resolve de default MIME type for unknown types" in {
Server(TestData.conf)
.guessMimeType(
getPath("unknow"),
None
) shouldBe TestData.conf.defaultMimeType
}
it should "resolve gemini MIME type" in {
Server(TestData.conf)
.guessMimeType(
getPath("file.gmi"),
None
) shouldBe "text/gemini"
Server(TestData.conf)
.guessMimeType(
getPath("file.gemini"),
None
) shouldBe "text/gemini"
}
it should "resolve gemini MIME type, including parameters" in {
Server(TestData.conf)
.guessMimeType(
getPath("file.gmi"),
Some("param")
) shouldBe "text/gemini; param"
Server(TestData.conf)
.guessMimeType(
getPath("file.gemini"),
Some("param")
) shouldBe "text/gemini; param"
}
it should "gemini MIME type parameters are sanitized" in {
Server(TestData.conf)
.guessMimeType(
getPath("file.gmi"),
Some(" ; param")
) shouldBe "text/gemini; param"
}
behavior of "guessMimeType using the configured types"
it should "resolve a known MIME type" in {
Server(TestData.conf.copy(mimeTypes = TestData.mimeTypes))
.guessMimeType(
getPath("file.gmi"),
None
) shouldBe "config"
}
it should "include parameters for text/gemini MIME types" in {
Server(
TestData.conf.copy(mimeTypes = Some(Map("text/gemini" -> List(".gmi"))))
).guessMimeType(
getPath("file.gmi"),
Some("param")
) shouldBe "text/gemini; param"
}
it should "resolve de default MIME type for unknown types" in {
Server(TestData.conf.copy(mimeTypes = TestData.mimeTypes))
.guessMimeType(
getPath("unknow"),
None
) shouldBe TestData.conf.defaultMimeType
}
behavior of "decodeUTF8"
it should "return right on valid UTF-8 codes" in {
Server(TestData.conf)
.decodeUTF8(ByteString("válid UTF-8")) shouldBe Symbol(
"right"
)
}
it should "return left on invalid UTF-8 codes" in {
Server(TestData.conf)
.decodeUTF8(ByteString(Array(0xc3.toByte, 0x28.toByte))) shouldBe Symbol(
"left"
)
}
behavior of "handleReq"
it should "return bad request on URLs with no scheme" in {
Server(TestData.conf).handleReq("//localhost/") should matchPattern {
case _: BadRequest =>
}
}
it should "return proxy request refused on port mismatch" in {
Server(TestData.conf)
.handleReq("gemini://localhost:8080/") should matchPattern {
case _: ProxyRequestRefused =>
}
}
it should "return proxy request refused when port not provided and configured port is not default" in {
Server(TestData.conf.copy(port = 8080))
.handleReq("gemini://localhost/") should matchPattern {
case _: ProxyRequestRefused =>
}
}
it should "return success when port is provided and matches configured port (not default)" in {
Server(TestData.conf.copy(port = 8080))
.handleReq("gemini://localhost:8080/") should matchPattern {
case _: Success =>
}
}
it should "return proxy request refused when the vhost is not found" in {
Server(TestData.conf)
.handleReq("gemini://otherhost/") should matchPattern {
case _: ProxyRequestRefused =>
}
}
it should "return bad request when user info is present" in {
Server(TestData.conf)
.handleReq("gemini://user@localhost/") should matchPattern {
case _: BadRequest =>
}
}
it should "return bad request when the path is out of root dir" in {
Server(TestData.conf)
.handleReq("gemini://localhost/../../") should matchPattern {
case _: BadRequest =>
}
}
it should "return bad request for invalid URLs" in {
Server(TestData.conf)
.handleReq("gemini://localhost/ invalid") should matchPattern {
case _: BadRequest =>
}
}
it should "redirect to normalize the URL" in {
Server(TestData.conf)
.handleReq("gemini://localhost/./") should matchPattern {
case _: PermanentRedirect =>
}
}
it should "return not found if the path doesn't exist" in {
Server(TestData.conf)
.handleReq("gemini://localhost/doesnotexist") should matchPattern {
case _: NotFound =>
}
}
it should "return not found if a dot file" in {
Server(TestData.conf)
.handleReq("gemini://localhost/.dotfile") should matchPattern {
case _: NotFound =>
}
}
it should "return success on reading file" in {
Server(TestData.conf)
.handleReq("gemini://localhost/index.gmi") should matchPattern {
case Success(_, "text/gemini", Some(_), 25L) =>
}
}
it should "redirect and normalize request on a directory" in {
Server(TestData.conf)
.handleReq("gemini://localhost/dir") should matchPattern {
case _: PermanentRedirect =>
}
}
it should "return an existing index file when requesting a directory" in {
Server(TestData.conf)
.handleReq("gemini://localhost/") should matchPattern {
case Success(_, "text/gemini", Some(_), 25L) =>
}
}
it should "return proxy request refused for non gemini schemes" in {
Server(TestData.conf)
.handleReq("https://localhost/") should matchPattern {
case _: ProxyRequestRefused =>
}
}
it should "include gemini params for gemini MIME type" in {
Server(
TestData.conf.copy(virtualHosts =
List(TestData.conf.virtualHosts(0).copy(geminiParams = Some("test")))
)
).handleReq("gemini://localhost/index.gmi") should matchPattern {
case Success(_, "text/gemini; test", Some(_), 25L) =>
}
}
behavior of "handleReq, directory listings"
it should "return a directory listing if is enabled and no index" in {
Server(TestData.conf)
.handleReq("gemini://localhost/dir/") should matchPattern {
case _: DirListing =>
}
}
it should "return a directory listing, directory listing flags: vhost flag false, directories flag true" in {
Server(
TestData.conf.copy(virtualHosts =
List(
TestData.conf
.virtualHosts(0)
.copy(
directoryListing = false,
directories = List(
Directory(
getPath(getClass.getResource("/").getPath(), "dir/")
.toString(),
directoryListing = Some(true)
)
)
)
)
)
).handleReq("gemini://localhost/dir/") should matchPattern {
case _: DirListing =>
}
}
it should "return not found with no index, directory listing flags: vhost flag true, directories flag false" in {
Server(
TestData.conf.copy(virtualHosts =
List(
TestData.conf
.virtualHosts(0)
.copy(
directoryListing = true,
directories = List(
Directory(
getPath(getClass.getResource("/").getPath(), "dir/")
.toString(),
directoryListing = Some(false)
)
)
)
)
)
).handleReq("gemini://localhost/dir/") should matchPattern {
case _: NotFound =>
}
}
it should "return not found if directory listing is not enabled and no index" in {
Server(
TestData.conf.copy(virtualHosts =
List(TestData.conf.virtualHosts(0).copy(directoryListing = false))
)
).handleReq("gemini://localhost/dir/") should matchPattern {
case _: NotFound =>
}
}
behavior of "handleReq, user directories"
it should "return success on reading file" in {
Server(TestData.confUserDir).handleReq(
"gemini://localhost/~username/index.gmi"
) should matchPattern {
case Success(_, "text/gemini", Some(_), 38L) =>
}
}
it should "return redirect accessing the user directory without ending slash" in {
Server(TestData.confUserDir).handleReq(
"gemini://localhost/~username"
) should matchPattern {
case _: PermanentRedirect =>
}
}
it should "return success accessing the user directory index" in {
Server(TestData.confUserDir).handleReq(
"gemini://localhost/~username/"
) should matchPattern {
case Success(_, "text/gemini", Some(_), 38L) =>
}
}
it should "return bad request trying to exit the root directory" in {
Server(TestData.confUserDir).handleReq(
"gemini://localhost/~username/../../"
) should matchPattern {
case _: BadRequest =>
}
}
it should "return redirect to the virtual host root when leaving the user dir" in {
Server(TestData.confUserDir).handleReq(
"gemini://localhost/~username/../"
) should matchPattern {
case _: PermanentRedirect =>
}
}
it should "not translate root if used an invalid user pattern" in {
Server(TestData.confUserDir).handleReq(
"gemini://localhost/~username../"
) should matchPattern {
case _: NotFound =>
}
Server(TestData.confUserDir).handleReq(
"gemini://localhost/~0invalid/"
) should matchPattern {
case _: NotFound =>
}
}
it should "not translate root if no user directory path was provided" in {
Server(
TestData.conf.copy(virtualHosts =
List(
TestData.conf
.virtualHosts(0)
.copy(userDirectories = true)
)
)
).handleReq(
"gemini://localhost/~username/"
) should matchPattern {
case _: NotFound =>
}
}
object TestData {
val conf = ServiceConf(
address = "127.0.0.1",
port = 1965,
defaultMimeType = "text/plain",
idleTimeout = 10.seconds,
virtualHosts = List(
VirtualHost(
host = "localhost",
root = getClass.getResource("/").getPath()
)
),
genCertValidFor = 1.day,
enabledProtocols = Nil,
enabledCipherSuites = Nil
)
val confUserDir = conf.copy(virtualHosts =
List(
conf
.virtualHosts(0)
.copy(
userDirectories = true,
userDirectoryPath = Some(
getClass.getResource("/").getPath() + "{user}/public_gemini/"
)
)
)
)
val mimeTypes = Some(
Map(
"config" -> List(".gmi", ".gemini")
)
)
}
}
|