🔀 Merge “release/v81” into “master”
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / freenet / AsyncFreenetInterfaceTest.kt
1 /**
2  * Sone - AsyncFreenetInterfaceTest.kt - Copyright © 2019–2020 David ‘Bombe’ 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.freenet
19
20 import freenet.client.*
21 import freenet.keys.*
22 import freenet.support.io.*
23 import kotlinx.coroutines.*
24 import net.pterodactylus.sone.core.*
25 import org.hamcrest.MatcherAssert.*
26 import org.hamcrest.Matchers.*
27 import java.util.concurrent.atomic.*
28 import kotlin.test.*
29
30 class AsyncFreenetInterfaceTest {
31
32         @Test
33         fun `returned deferred is completed by success`() = runBlocking {
34                 val result = FetchResult(ClientMetadata(), NullBucket())
35                 val freenetClient = object : FreenetClient {
36                         override fun fetch(freenetKey: FreenetURI) = result
37                 }
38                 val freenetInterface = AsyncFreenetInterface(freenetClient)
39                 val fetched = async { freenetInterface.fetchUri(FreenetURI("KSK@GPL.txt")) }
40
41                 withTimeout(1000) {
42                         assertThat(fetched.await(), equalTo(Fetched(FreenetURI("KSK@GPL.txt"), result)))
43                 }
44         }
45
46         @Test
47         fun `permanent redircts are being followed`() = runBlocking {
48                 val result = FetchResult(ClientMetadata(), NullBucket())
49                 val freenetClient = object : FreenetClient {
50                         val redirected = AtomicBoolean(false)
51                         override fun fetch(freenetKey: FreenetURI) =
52                                         if (redirected.compareAndSet(false, true))
53                                                 throw FetchException(FetchException.FetchExceptionMode.PERMANENT_REDIRECT, FreenetURI("KSK@GPLv3.txt"))
54                                         else result
55                 }
56                 val freenetInterface = AsyncFreenetInterface(freenetClient)
57                 val fetched = async { freenetInterface.fetchUri(FreenetURI("KSK@GPL.txt")) }
58
59                 withTimeout(1000) {
60                         assertThat(fetched.await(), equalTo(Fetched(FreenetURI("KSK@GPLv3.txt"), result)))
61                 }
62         }
63
64         @Test
65         fun `fetch errors are being re-thrown`() = runBlocking<Unit> {
66                 val freenetClient = object : FreenetClient {
67                         override fun fetch(freenetKey: FreenetURI) =
68                                         throw FetchException(FetchException.FetchExceptionMode.ALL_DATA_NOT_FOUND)
69                 }
70                 val freenetInterface = AsyncFreenetInterface(freenetClient)
71                 val fetched = supervisorScope { async { freenetInterface.fetchUri(FreenetURI("KSK@GPL.txt")) } }
72
73                 withTimeout(1000) {
74                         assertFailsWith(FetchException::class) {
75                                 fetched.await()
76                         }
77                 }
78         }
79
80 }