1 package net.pterodactylus.sone.core
3 import com.google.common.eventbus.*
4 import freenet.client.*
5 import freenet.client.FetchException.FetchExceptionMode.*
6 import freenet.client.InsertException.*
7 import freenet.client.async.*
10 import freenet.keys.InsertableClientSSK.*
12 import freenet.node.RequestStarter.*
13 import freenet.support.api.*
14 import freenet.support.io.*
15 import net.pterodactylus.sone.core.FreenetInterface.*
16 import net.pterodactylus.sone.core.event.*
17 import net.pterodactylus.sone.data.*
18 import net.pterodactylus.sone.data.impl.*
19 import net.pterodactylus.sone.freenet.wot.DefaultIdentity
20 import net.pterodactylus.sone.test.*
21 import net.pterodactylus.sone.test.Matchers.*
22 import net.pterodactylus.sone.test.TestUtil.*
23 import net.pterodactylus.sone.utils.*
24 import org.hamcrest.MatcherAssert.*
25 import org.hamcrest.Matchers.equalTo
26 import org.hamcrest.Matchers.notNullValue
27 import org.hamcrest.Matchers.nullValue
29 import org.junit.rules.*
31 import org.mockito.ArgumentCaptor.*
32 import org.mockito.ArgumentMatchers.eq
33 import org.mockito.Mockito.*
36 import kotlin.test.Test
39 * Unit test for [FreenetInterface].
41 class FreenetInterfaceTest {
45 val expectionException: ExpectedException = ExpectedException.none()
49 val silencedLogging = silencedLogging()
51 @Suppress("UnstableApiUsage")
52 private val eventBus = mock<EventBus>()
53 private val node = mock<Node>()
54 private val nodeClientCore = mock<NodeClientCore>()
55 private val highLevelSimpleClient: HighLevelSimpleClient = mock(HighLevelSimpleClient::class.java, withSettings().extraInterfaces(RequestClient::class.java))
56 private val randomSource = DummyRandomSource()
57 private val uskManager = mock<USKManager>()
58 private val sone = mock<Sone>()
59 private val callbackCaptor: ArgumentCaptor<USKCallback> = forClass(USKCallback::class.java)
60 private val image: Image = ImageImpl()
61 private val insertToken: InsertToken
62 private val bucket = mock<Bucket>()
63 private val clientGetCallback: ArgumentCaptor<ClientGetCallback> = forClass(ClientGetCallback::class.java)
64 private val uri = FreenetURI("KSK@pgl.png")
65 private val fetchResult = mock<FetchResult>()
66 private val backgroundFetchCallback = mock<BackgroundFetchCallback>()
67 private val clientGetter = mock<ClientGetter>()
68 private val soneUriCreator = SoneUriCreator()
69 private val freenetInterface: FreenetInterface
72 whenever(nodeClientCore.makeClient(anyShort(), anyBoolean(), anyBoolean())).thenReturn(highLevelSimpleClient)
73 setField(node, "clientCore", nodeClientCore)
74 setField(node, "random", randomSource)
75 setField(nodeClientCore, "uskManager", uskManager)
76 setField(nodeClientCore, "clientContext", mock<ClientContext>())
77 freenetInterface = FreenetInterface(eventBus, node, soneUriCreator)
78 insertToken = freenetInterface.InsertToken(image)
79 insertToken.setBucket(bucket)
83 fun setupHighLevelSimpleClient() {
84 whenever(highLevelSimpleClient.fetchContext).thenReturn(mock())
85 whenever(highLevelSimpleClient.fetch(eq(uri), anyLong(), any(ClientGetCallback::class.java), any(FetchContext::class.java), anyShort())).thenReturn(clientGetter)
90 val insertSsk = createRandom(randomSource, "test-0")
91 whenever(sone.id).thenReturn(insertSsk.uri.routingKey.asFreenetBase64)
92 whenever(sone.identity).thenReturn(DefaultIdentity("id", "name", insertSsk.uri.toString()))
96 fun setupCallbackCaptorAndUskManager() {
97 doNothing().whenever(uskManager).subscribe(any(USK::class.java), callbackCaptor.capture(), anyBoolean(), any(RequestClient::class.java))
101 fun `can fetch uri`() {
102 val freenetUri = FreenetURI("KSK@GPLv3.txt")
103 val fetchResult = createFetchResult()
104 whenever(highLevelSimpleClient.fetch(freenetUri)).thenReturn(fetchResult)
105 val fetched = freenetInterface.fetchUri(freenetUri)
106 assertThat(fetched, notNullValue())
107 assertThat(fetched!!.fetchResult, equalTo(fetchResult))
108 assertThat(fetched.freenetUri, equalTo(freenetUri))
112 fun `fetch follows redirect`() {
113 val freenetUri = FreenetURI("KSK@GPLv2.txt")
114 val newFreenetUri = FreenetURI("KSK@GPLv3.txt")
115 val fetchResult = createFetchResult()
116 val fetchException = FetchException(PERMANENT_REDIRECT, newFreenetUri)
117 whenever(highLevelSimpleClient.fetch(freenetUri)).thenThrow(fetchException)
118 whenever(highLevelSimpleClient.fetch(newFreenetUri)).thenReturn(fetchResult)
119 val fetched = freenetInterface.fetchUri(freenetUri)
120 assertThat(fetched!!.fetchResult, equalTo(fetchResult))
121 assertThat(fetched.freenetUri, equalTo(newFreenetUri))
125 fun `fetch returns null on fetch exceptions`() {
126 val freenetUri = FreenetURI("KSK@GPLv2.txt")
127 val fetchException = FetchException(ALL_DATA_NOT_FOUND)
128 whenever(highLevelSimpleClient.fetch(freenetUri)).thenThrow(fetchException)
129 val fetched = freenetInterface.fetchUri(freenetUri)
130 assertThat(fetched, nullValue())
133 private fun createFetchResult(): FetchResult {
134 val clientMetadata = ClientMetadata("text/plain")
135 val bucket = ArrayBucket("Some Data.".toByteArray())
136 return FetchResult(clientMetadata, bucket)
140 fun `inserting an image`() {
141 val temporaryImage = TemporaryImage("image-id")
142 temporaryImage.mimeType = "image/png"
143 val imageData = byteArrayOf(1, 2, 3, 4)
144 temporaryImage.imageData = imageData
145 val image = ImageImpl("image-id")
146 val insertToken = freenetInterface.InsertToken(image)
147 val insertContext = mock<InsertContext>()
148 whenever(highLevelSimpleClient.getInsertContext(anyBoolean())).thenReturn(insertContext)
149 val clientPutter = mock<ClientPutter>()
150 val insertBlockCaptor = forClass(InsertBlock::class.java)
151 whenever(highLevelSimpleClient.insert(insertBlockCaptor.capture(), eq(null as String?), eq(false), eq(insertContext), eq(insertToken), anyShort())).thenReturn(clientPutter)
152 freenetInterface.insertImage(temporaryImage, image, insertToken)
153 assertThat(insertBlockCaptor.value.data.inputStream, delivers(byteArrayOf(1, 2, 3, 4)))
154 assertThat(getPrivateField(insertToken, "clientPutter"), equalTo(clientPutter))
155 verify(eventBus).post(any(ImageInsertStartedEvent::class.java))
159 fun `insert exception causes a sone exception`() {
160 val temporaryImage = TemporaryImage("image-id")
161 temporaryImage.mimeType = "image/png"
162 val imageData = byteArrayOf(1, 2, 3, 4)
163 temporaryImage.imageData = imageData
164 val image = ImageImpl("image-id")
165 val insertToken = freenetInterface.InsertToken(image)
166 val insertContext = mock<InsertContext>()
167 whenever(highLevelSimpleClient.getInsertContext(anyBoolean())).thenReturn(insertContext)
168 val insertBlockCaptor = forClass(InsertBlock::class.java)
169 whenever(highLevelSimpleClient.insert(insertBlockCaptor.capture(), eq(null as String?), eq(false), eq(insertContext), eq(insertToken), anyShort())).thenThrow(InsertException::class.java)
170 expectionException.expect(SoneInsertException::class.java)
171 freenetInterface.insertImage(temporaryImage, image, insertToken)
175 fun `inserting a directory`() {
176 val freenetUri = mock<FreenetURI>()
177 val manifestEntries = HashMap<String, Any>()
178 val defaultFile = "index.html"
179 val resultingUri = mock<FreenetURI>()
180 whenever(highLevelSimpleClient.insertManifest(eq(freenetUri), eq(manifestEntries), eq(defaultFile))).thenReturn(resultingUri)
181 assertThat(freenetInterface.insertDirectory(freenetUri, manifestEntries, defaultFile), equalTo(resultingUri))
185 fun `insert exception is forwarded as sone exception`() {
186 whenever(highLevelSimpleClient.insertManifest(any(), any(), any())).thenThrow(InsertException::class.java)
187 expectionException.expect(SoneException::class.java)
188 freenetInterface.insertDirectory(null, null, null)
192 fun `sone with wrong request uri will not be subscribed`() {
193 freenetInterface.registerUsk(FreenetURI("KSK@GPLv3.txt"), null)
194 verify(uskManager, never()).subscribe(any(USK::class.java), any(USKCallback::class.java), anyBoolean(), any(RequestClient::class.java))
198 fun `registering a usk`() {
199 val freenetUri = createRandom(randomSource, "test-0").uri.uskForSSK()
200 val callback = mock<Callback>()
201 freenetInterface.registerUsk(freenetUri, callback)
202 verify(uskManager).subscribe(any(USK::class.java), any(USKCallback::class.java), anyBoolean(), any(RequestClient::class.java))
206 fun `registering a non-usk key will not be subscribed`() {
207 val freenetUri = FreenetURI("KSK@GPLv3.txt")
208 val callback = mock<Callback>()
209 freenetInterface.registerUsk(freenetUri, callback)
210 verify(uskManager, never()).subscribe(any(USK::class.java), any(USKCallback::class.java), anyBoolean(), any(RequestClient::class.java))
214 fun `registering an active usk will subscribe to it correctly`() {
215 val freenetUri = createRandom(randomSource, "test-0").uri.uskForSSK()
216 val uskCallback = mock<USKCallback>()
217 freenetInterface.registerActiveUsk(freenetUri, uskCallback)
218 verify(uskManager).subscribe(any(USK::class.java), eq(uskCallback), eq(true), any(RequestClient::class.java))
222 fun `registering an inactive usk will subscribe to it correctly`() {
223 val freenetUri = createRandom(randomSource, "test-0").uri.uskForSSK()
224 val uskCallback = mock<USKCallback>()
225 freenetInterface.registerPassiveUsk(freenetUri, uskCallback)
226 verify(uskManager).subscribe(any(USK::class.java), eq(uskCallback), eq(false), any(RequestClient::class.java))
230 fun `registering an active non-usk will not subscribe to a usk`() {
231 val freenetUri = createRandom(randomSource, "test-0").uri
232 freenetInterface.registerActiveUsk(freenetUri, null)
233 verify(uskManager, never()).subscribe(any(USK::class.java), any(USKCallback::class.java), anyBoolean(), any(RequestClient::class.java))
237 fun `registering an inactive non-usk will not subscribe to a usk`() {
238 val freenetUri = createRandom(randomSource, "test-0").uri
239 freenetInterface.registerPassiveUsk(freenetUri, null)
240 verify(uskManager, never()).subscribe(any(USK::class.java), any(USKCallback::class.java), anyBoolean(), any(RequestClient::class.java))
244 fun `unregistering a not registered usk does nothing`() {
245 val freenetURI = createRandom(randomSource, "test-0").uri.uskForSSK()
246 freenetInterface.unregisterUsk(freenetURI)
247 verify(uskManager, never()).unsubscribe(any(USK::class.java), any(USKCallback::class.java))
251 fun `unregistering a registered usk`() {
252 val freenetURI = createRandom(randomSource, "test-0").uri.uskForSSK()
253 val callback = mock<Callback>()
254 freenetInterface.registerUsk(freenetURI, callback)
255 freenetInterface.unregisterUsk(freenetURI)
256 verify(uskManager).unsubscribe(any(USK::class.java), any(USKCallback::class.java))
260 fun `unregistering a not registered sone does nothing`() {
261 freenetInterface.unregisterUsk(sone)
262 verify(uskManager, never()).unsubscribe(any(USK::class.java), any(USKCallback::class.java))
266 fun `unregistering a registered sone unregisters the sone`() {
267 freenetInterface.registerActiveUsk(soneUriCreator.getRequestUri(sone), mock())
268 freenetInterface.unregisterUsk(sone)
269 verify(uskManager).unsubscribe(any(USK::class.java), any(USKCallback::class.java))
273 fun `unregistering a sone with a wrong request key will not unsubscribe`() {
274 freenetInterface.registerUsk(FreenetURI("KSK@GPLv3.txt"), null)
275 freenetInterface.unregisterUsk(sone)
276 verify(uskManager, never()).unsubscribe(any(USK::class.java), any(USKCallback::class.java))
280 fun `callback for normal usk uses different priorities`() {
281 val callback = mock<Callback>()
282 val soneUri = createRandom(randomSource, "test-0").uri.uskForSSK()
283 freenetInterface.registerUsk(soneUri, callback)
284 assertThat(callbackCaptor.value.pollingPriorityNormal, equalTo(PREFETCH_PRIORITY_CLASS))
285 assertThat(callbackCaptor.value.pollingPriorityProgress, equalTo(INTERACTIVE_PRIORITY_CLASS))
289 fun `callback for normal usk forwards important parameters`() {
290 val callback = mock<Callback>()
291 val uri = createRandom(randomSource, "test-0").uri.uskForSSK()
292 freenetInterface.registerUsk(uri, callback)
293 val key = mock<USK>()
294 whenever(key.uri).thenReturn(uri)
295 callbackCaptor.value.onFoundEdition(3, key, null, false, 0.toShort(), null, true, true)
296 verify(callback).editionFound(eq(uri), eq(3L), eq(true), eq(true))
300 fun `fetched retains uri and fetch result`() {
301 val freenetUri = mock<FreenetURI>()
302 val fetchResult = mock<FetchResult>()
303 val (freenetUri1, fetchResult1) = Fetched(freenetUri, fetchResult)
304 assertThat(freenetUri1, equalTo(freenetUri))
305 assertThat(fetchResult1, equalTo(fetchResult))
309 fun `cancelling an insert will fire image insert aborted event`() {
310 val clientPutter = mock<ClientPutter>()
311 insertToken.setClientPutter(clientPutter)
312 val imageInsertStartedEvent = forClass(ImageInsertStartedEvent::class.java)
313 verify(eventBus).post(imageInsertStartedEvent.capture())
314 assertThat(imageInsertStartedEvent.value.image, equalTo(image))
316 val imageInsertAbortedEvent = forClass(ImageInsertAbortedEvent::class.java)
317 verify(eventBus, times(2)).post(imageInsertAbortedEvent.capture())
318 verify(bucket).free()
319 assertThat(imageInsertAbortedEvent.value.image, equalTo(image))
323 fun `failure without exception sends failed event`() {
324 val insertException = InsertException(mock<InsertException>())
325 insertToken.onFailure(insertException, null)
326 val imageInsertFailedEvent = forClass(ImageInsertFailedEvent::class.java)
327 verify(eventBus).post(imageInsertFailedEvent.capture())
328 verify(bucket).free()
329 assertThat(imageInsertFailedEvent.value.image, equalTo(image))
330 assertThat(imageInsertFailedEvent.value.cause, equalTo<Throwable>(insertException))
334 fun `failure sends failed event with exception`() {
335 val insertException = InsertException(InsertExceptionMode.INTERNAL_ERROR, "Internal error", null)
336 insertToken.onFailure(insertException, null)
337 val imageInsertFailedEvent = forClass(ImageInsertFailedEvent::class.java)
338 verify(eventBus).post(imageInsertFailedEvent.capture())
339 verify(bucket).free()
340 assertThat(imageInsertFailedEvent.value.image, equalTo(image))
341 assertThat(imageInsertFailedEvent.value.cause, equalTo(insertException as Throwable))
345 fun `failure because cancelled by user sends aborted event`() {
346 val insertException = InsertException(InsertExceptionMode.CANCELLED, null)
347 insertToken.onFailure(insertException, null)
348 val imageInsertAbortedEvent = forClass(ImageInsertAbortedEvent::class.java)
349 verify(eventBus).post(imageInsertAbortedEvent.capture())
350 verify(bucket).free()
351 assertThat(imageInsertAbortedEvent.value.image, equalTo(image))
355 fun `ignored methods do not throw exceptions`() {
356 insertToken.onResume(null)
357 insertToken.onFetchable(null)
358 insertToken.onGeneratedMetadata(null, null)
362 fun `generated uri is posted on success`() {
363 val generatedUri = mock<FreenetURI>()
364 insertToken.onGeneratedURI(generatedUri, null)
365 insertToken.onSuccess(null)
366 val imageInsertFinishedEvent = forClass(ImageInsertFinishedEvent::class.java)
367 verify(eventBus).post(imageInsertFinishedEvent.capture())
368 verify(bucket).free()
369 assertThat(imageInsertFinishedEvent.value.image, equalTo(image))
370 assertThat(imageInsertFinishedEvent.value.resultingUri, equalTo(generatedUri))
374 fun `insert token supplier supplies insert tokens`() {
375 val insertTokenSupplier = InsertTokenSupplier(freenetInterface)
376 assertThat(insertTokenSupplier.apply(image), notNullValue())
380 fun `background fetch can be started`() {
381 freenetInterface.startFetch(uri, backgroundFetchCallback)
382 verify(highLevelSimpleClient).fetch(eq(uri), anyLong(), any(ClientGetCallback::class.java), any(FetchContext::class.java), anyShort())
386 fun `background fetch registers snoop and restarts the request`() {
387 freenetInterface.startFetch(uri, backgroundFetchCallback)
388 verify(clientGetter).metaSnoop = any(SnoopMetadata::class.java)
389 verify(clientGetter).restart(eq(uri), anyBoolean(), any(ClientContext::class.java))
393 fun `request is not cancelled for image mime type`() {
394 verifySnoopCancelsRequestForMimeType("image/png", false)
395 verify(backgroundFetchCallback, never()).failed(uri)
399 fun `request is cancelled for null mime type`() {
400 verifySnoopCancelsRequestForMimeType(null, true)
401 verify(backgroundFetchCallback, never()).shouldCancel(eq(uri), any(), anyLong())
402 verify(backgroundFetchCallback).failed(uri)
406 fun `request is cancelled for video mime type`() {
407 verifySnoopCancelsRequestForMimeType("video/mkv", true)
408 verify(backgroundFetchCallback).failed(uri)
412 fun `request is cancelled for audio mime type`() {
413 verifySnoopCancelsRequestForMimeType("audio/mpeg", true)
414 verify(backgroundFetchCallback).failed(uri)
418 fun `request is cancelled for text mime type`() {
419 verifySnoopCancelsRequestForMimeType("text/plain", true)
420 verify(backgroundFetchCallback).failed(uri)
423 private fun verifySnoopCancelsRequestForMimeType(mimeType: String?, cancel: Boolean) {
424 whenever(backgroundFetchCallback.shouldCancel(eq(uri), if (mimeType != null) eq(mimeType) else isNull(), anyLong())).thenReturn(cancel)
425 freenetInterface.startFetch(uri, backgroundFetchCallback)
426 val snoopMetadata = forClass(SnoopMetadata::class.java)
427 verify(clientGetter).metaSnoop = snoopMetadata.capture()
428 val metadata = mock<Metadata>()
429 whenever(metadata.mimeType).thenReturn(mimeType)
430 assertThat(snoopMetadata.value.snoopMetadata(metadata, mock()), equalTo(cancel))
434 fun `callback of background fetch is notified on success`() {
435 freenetInterface.startFetch(uri, backgroundFetchCallback)
436 verify(highLevelSimpleClient).fetch(eq(uri), anyLong(), clientGetCallback.capture(), any(FetchContext::class.java), anyShort())
437 whenever(fetchResult.mimeType).thenReturn("image/png")
438 whenever(fetchResult.asByteArray()).thenReturn(byteArrayOf(1, 2, 3, 4, 5))
439 clientGetCallback.value.onSuccess(fetchResult, mock())
440 verify(backgroundFetchCallback).loaded(uri, "image/png", byteArrayOf(1, 2, 3, 4, 5))
441 verifyNoMoreInteractions(backgroundFetchCallback)
445 fun `callback of background fetch is notified on failure`() {
446 freenetInterface.startFetch(uri, backgroundFetchCallback)
447 verify(highLevelSimpleClient).fetch(eq(uri), anyLong(), clientGetCallback.capture(), any(FetchContext::class.java), anyShort())
448 whenever(fetchResult.mimeType).thenReturn("image/png")
449 whenever(fetchResult.asByteArray()).thenReturn(byteArrayOf(1, 2, 3, 4, 5))
450 clientGetCallback.value.onFailure(FetchException(ALL_DATA_NOT_FOUND), mock())
451 verify(backgroundFetchCallback).failed(uri)
452 verifyNoMoreInteractions(backgroundFetchCallback)
456 fun `callback of background fetch is notified as failure if bucket can not be loaded`() {
457 freenetInterface.startFetch(uri, backgroundFetchCallback)
458 verify(highLevelSimpleClient).fetch(eq(uri), anyLong(), clientGetCallback.capture(), any(FetchContext::class.java), anyShort())
459 whenever(fetchResult.mimeType).thenReturn("image/png")
460 whenever(fetchResult.asByteArray()).thenThrow(IOException::class.java)
461 clientGetCallback.value.onSuccess(fetchResult, mock())
462 verify(backgroundFetchCallback).failed(uri)
463 verifyNoMoreInteractions(backgroundFetchCallback)
467 fun `unregistering a registered USK with different edition unregisters USK`() {
468 val callback = mock<Callback>()
469 val uri = createRandom(randomSource, "test-123").uri.uskForSSK()
470 freenetInterface.registerUsk(uri, callback)
471 freenetInterface.unregisterUsk(uri.setSuggestedEdition(234))
472 verify(uskManager).unsubscribe(any<USK>(), any<USKCallback>())