X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FFreenetInterfaceTest.java;h=9391bde8d73a570486761304067bd709840ec91c;hb=a57dae72e505fec934a9516b88fea75c473f7641;hp=a0d8d0c382d53946d1cb5a3d91008eb91c6112b0;hpb=25bbbf38673ca903872bc3d4633020d01603ab61;p=Sone.git
diff --git a/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java b/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java
index a0d8d0c..9391bde 100644
--- a/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java
+++ b/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java
@@ -1,10 +1,11 @@
package net.pterodactylus.sone.core;
+import static freenet.client.FetchException.FetchExceptionMode.ALL_DATA_NOT_FOUND;
import static freenet.keys.InsertableClientSSK.createRandom;
import static freenet.node.RequestStarter.INTERACTIVE_PRIORITY_CLASS;
import static freenet.node.RequestStarter.PREFETCH_PRIORITY_CLASS;
-import static net.pterodactylus.sone.Matchers.delivers;
-import static net.pterodactylus.sone.TestUtil.setFinalField;
+import static net.pterodactylus.sone.test.Matchers.delivers;
+import static net.pterodactylus.sone.test.TestUtil.setFinalField;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
@@ -12,6 +13,7 @@ import static org.hamcrest.Matchers.nullValue;
import static org.mockito.ArgumentCaptor.forClass;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyShort;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
@@ -19,6 +21,7 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;
@@ -26,9 +29,8 @@ import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
-import net.pterodactylus.sone.TestUtil;
+import net.pterodactylus.sone.core.FreenetInterface.BackgroundFetchCallback;
import net.pterodactylus.sone.core.FreenetInterface.Callback;
-import net.pterodactylus.sone.core.FreenetInterface.Fetched;
import net.pterodactylus.sone.core.FreenetInterface.InsertToken;
import net.pterodactylus.sone.core.FreenetInterface.InsertTokenSupplier;
import net.pterodactylus.sone.core.event.ImageInsertAbortedEvent;
@@ -36,11 +38,13 @@ import net.pterodactylus.sone.core.event.ImageInsertFailedEvent;
import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
import net.pterodactylus.sone.core.event.ImageInsertStartedEvent;
import net.pterodactylus.sone.data.Image;
-import net.pterodactylus.sone.data.impl.ImageImpl;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.data.TemporaryImage;
+import net.pterodactylus.sone.data.impl.ImageImpl;
+import net.pterodactylus.sone.test.TestUtil;
import freenet.client.ClientMetadata;
+import freenet.client.FetchContext;
import freenet.client.FetchException;
import freenet.client.FetchException.FetchExceptionMode;
import freenet.client.FetchResult;
@@ -49,7 +53,12 @@ import freenet.client.InsertBlock;
import freenet.client.InsertContext;
import freenet.client.InsertException;
import freenet.client.InsertException.InsertExceptionMode;
+import freenet.client.Metadata;
+import freenet.client.async.ClientContext;
+import freenet.client.async.ClientGetCallback;
+import freenet.client.async.ClientGetter;
import freenet.client.async.ClientPutter;
+import freenet.client.async.SnoopMetadata;
import freenet.client.async.USKCallback;
import freenet.client.async.USKManager;
import freenet.crypt.DummyRandomSource;
@@ -66,6 +75,7 @@ import freenet.support.io.ArrayBucket;
import freenet.support.io.ResumeFailedException;
import com.google.common.eventbus.EventBus;
+import org.hamcrest.*;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
@@ -73,8 +83,6 @@ import org.mockito.ArgumentMatchers;
/**
* Unit test for {@link FreenetInterface}.
- *
- * @author David âBombeâ Roden
*/
public class FreenetInterfaceTest {
@@ -90,6 +98,20 @@ public class FreenetInterfaceTest {
private final Image image = mock(Image.class);
private InsertToken insertToken;
private final Bucket bucket = mock(Bucket.class);
+ private final ArgumentCaptor clientGetCallback = forClass(ClientGetCallback.class);
+ private final FreenetURI uri = new FreenetURI("KSK@pgl.png");
+ private final FetchResult fetchResult = mock(FetchResult.class);
+ private final BackgroundFetchCallback backgroundFetchCallback = mock(BackgroundFetchCallback.class);
+ private final ClientGetter clientGetter = mock(ClientGetter.class);
+
+ public FreenetInterfaceTest() throws MalformedURLException {
+ }
+
+ @Before
+ public void setupHighLevelSimpleClient() throws Exception {
+ when(highLevelSimpleClient.getFetchContext()).thenReturn(mock(FetchContext.class));
+ when(highLevelSimpleClient.fetch(eq(uri), anyLong(), any(ClientGetCallback.class), any(FetchContext.class), anyShort())).thenReturn( clientGetter);
+ }
@Before
public void setupFreenetInterface() {
@@ -97,6 +119,7 @@ public class FreenetInterfaceTest {
setFinalField(node, "clientCore", nodeClientCore);
setFinalField(node, "random", randomSource);
setFinalField(nodeClientCore, "uskManager", uskManager);
+ setFinalField(nodeClientCore, "clientContext", mock(ClientContext.class));
freenetInterface = new FreenetInterface(eventBus, node);
insertToken = freenetInterface.new InsertToken(image);
insertToken.setBucket(bucket);
@@ -141,7 +164,7 @@ public class FreenetInterfaceTest {
@Test
public void fetchReturnsNullOnFetchExceptions() throws MalformedURLException, FetchException {
FreenetURI freenetUri = new FreenetURI("KSK@GPLv2.txt");
- FetchException fetchException = new FetchException(FetchExceptionMode.ALL_DATA_NOT_FOUND);
+ FetchException fetchException = new FetchException(ALL_DATA_NOT_FOUND);
when(highLevelSimpleClient.fetch(freenetUri)).thenThrow(fetchException);
Fetched fetched = freenetInterface.fetchUri(freenetUri);
assertThat(fetched, nullValue());
@@ -190,7 +213,7 @@ public class FreenetInterfaceTest {
@Test
public void insertingADirectory() throws InsertException, SoneException {
FreenetURI freenetUri = mock(FreenetURI.class);
- HashMap manifestEntries = new HashMap();
+ HashMap manifestEntries = new HashMap<>();
String defaultFile = "index.html";
FreenetURI resultingUri = mock(FreenetURI.class);
when(highLevelSimpleClient.insertManifest(eq(freenetUri), eq(manifestEntries), eq(defaultFile))).thenReturn(resultingUri);
@@ -215,7 +238,7 @@ public class FreenetInterfaceTest {
FreenetURI freenetUri = createRandom(randomSource, "test-0").getURI().uskForSSK();
Callback callback = mock(Callback.class);
freenetInterface.registerUsk(freenetUri, callback);
- verify(uskManager).subscribe(any(USK.class), any(USKCallback.class), anyBoolean(), eq((RequestClient) highLevelSimpleClient));
+ verify(uskManager).subscribe(any(USK.class), any(USKCallback.class), anyBoolean(), any(RequestClient.class));
}
@Test
@@ -223,7 +246,7 @@ public class FreenetInterfaceTest {
FreenetURI freenetUri = new FreenetURI("KSK@GPLv3.txt");
Callback callback = mock(Callback.class);
freenetInterface.registerUsk(freenetUri, callback);
- verify(uskManager, never()).subscribe(any(USK.class), any(USKCallback.class), anyBoolean(), eq((RequestClient) highLevelSimpleClient));
+ verify(uskManager, never()).subscribe(any(USK.class), any(USKCallback.class), anyBoolean(), any(RequestClient.class));
}
@Test
@@ -247,9 +270,7 @@ public class FreenetInterfaceTest {
throws MalformedURLException {
FreenetURI freenetUri = createRandom(randomSource, "test-0").getURI();
freenetInterface.registerActiveUsk(freenetUri, null);
- verify(uskManager, never()).subscribe(any(USK.class),
- any(USKCallback.class), anyBoolean(),
- eq((RequestClient) highLevelSimpleClient));
+ verify(uskManager, never()).subscribe(any(USK.class), any(USKCallback.class), anyBoolean(), any(RequestClient.class));
}
@Test
@@ -257,9 +278,7 @@ public class FreenetInterfaceTest {
throws MalformedURLException {
FreenetURI freenetUri = createRandom(randomSource, "test-0").getURI();
freenetInterface.registerPassiveUsk(freenetUri, null);
- verify(uskManager, never()).subscribe(any(USK.class),
- any(USKCallback.class), anyBoolean(),
- eq((RequestClient) highLevelSimpleClient));
+ verify(uskManager, never()).subscribe(any(USK.class), any(USKCallback.class), anyBoolean(), any(RequestClient.class));
}
@Test
@@ -335,22 +354,23 @@ public class FreenetInterfaceTest {
insertToken.setClientPutter(clientPutter);
ArgumentCaptor imageInsertStartedEvent = forClass(ImageInsertStartedEvent.class);
verify(eventBus).post(imageInsertStartedEvent.capture());
- assertThat(imageInsertStartedEvent.getValue().image(), is(image));
+ assertThat(imageInsertStartedEvent.getValue().getImage(), is(image));
insertToken.cancel();
ArgumentCaptor imageInsertAbortedEvent = forClass(ImageInsertAbortedEvent.class);
verify(eventBus, times(2)).post(imageInsertAbortedEvent.capture());
verify(bucket).free();
- assertThat(imageInsertAbortedEvent.getValue().image(), is(image));
+ assertThat(imageInsertAbortedEvent.getValue().getImage(), is(image));
}
@Test
public void failureWithoutExceptionSendsFailedEvent() {
- insertToken.onFailure(null, null);
+ InsertException insertException = new InsertException(mock(InsertException.class));
+ insertToken.onFailure(insertException, null);
ArgumentCaptor imageInsertFailedEvent = forClass(ImageInsertFailedEvent.class);
verify(eventBus).post(imageInsertFailedEvent.capture());
verify(bucket).free();
- assertThat(imageInsertFailedEvent.getValue().image(), is(image));
- assertThat(imageInsertFailedEvent.getValue().cause(), nullValue());
+ assertThat(imageInsertFailedEvent.getValue().getImage(), is(image));
+ assertThat(imageInsertFailedEvent.getValue().getCause(), Matchers.is(insertException));
}
@Test
@@ -360,8 +380,8 @@ public class FreenetInterfaceTest {
ArgumentCaptor imageInsertFailedEvent = forClass(ImageInsertFailedEvent.class);
verify(eventBus).post(imageInsertFailedEvent.capture());
verify(bucket).free();
- assertThat(imageInsertFailedEvent.getValue().image(), is(image));
- assertThat(imageInsertFailedEvent.getValue().cause(), is((Throwable) insertException));
+ assertThat(imageInsertFailedEvent.getValue().getImage(), is(image));
+ assertThat(imageInsertFailedEvent.getValue().getCause(), is((Throwable) insertException));
}
@Test
@@ -371,7 +391,7 @@ public class FreenetInterfaceTest {
ArgumentCaptor imageInsertAbortedEvent = forClass(ImageInsertAbortedEvent.class);
verify(eventBus).post(imageInsertAbortedEvent.capture());
verify(bucket).free();
- assertThat(imageInsertAbortedEvent.getValue().image(), is(image));
+ assertThat(imageInsertAbortedEvent.getValue().getImage(), is(image));
}
@Test
@@ -389,14 +409,101 @@ public class FreenetInterfaceTest {
ArgumentCaptor imageInsertFinishedEvent = forClass(ImageInsertFinishedEvent.class);
verify(eventBus).post(imageInsertFinishedEvent.capture());
verify(bucket).free();
- assertThat(imageInsertFinishedEvent.getValue().image(), is(image));
- assertThat(imageInsertFinishedEvent.getValue().resultingUri(), is(generatedUri));
+ assertThat(imageInsertFinishedEvent.getValue().getImage(), is(image));
+ assertThat(imageInsertFinishedEvent.getValue().getResultingUri(), is(generatedUri));
}
@Test
public void insertTokenSupplierSuppliesInsertTokens() {
- InsertTokenSupplier insertTokenSupplier = freenetInterface.new InsertTokenSupplier();
+ InsertTokenSupplier insertTokenSupplier = new InsertTokenSupplier(freenetInterface);
assertThat(insertTokenSupplier.apply(image), notNullValue());
}
+ @Test
+ public void backgroundFetchCanBeStarted() throws Exception {
+ freenetInterface.startFetch(uri, backgroundFetchCallback);
+ verify(highLevelSimpleClient).fetch(eq(uri), anyLong(), any(ClientGetCallback.class), any(FetchContext.class), anyShort());
+ }
+
+ @Test
+ public void backgroundFetchRegistersSnoopAndRestartsTheRequest() throws Exception {
+ freenetInterface.startFetch(uri, backgroundFetchCallback);
+ verify(clientGetter).setMetaSnoop(any(SnoopMetadata.class));
+ verify(clientGetter).restart(eq(uri), anyBoolean(), any(ClientContext.class));
+ }
+
+ @Test
+ public void requestIsNotCancelledForImageMimeType() {
+ verifySnoopCancelsRequestForMimeType("image/png", false);
+ verify(backgroundFetchCallback, never()).failed(uri);
+ }
+
+ @Test
+ public void requestIsCancelledForNullMimeType() {
+ verifySnoopCancelsRequestForMimeType(null, true);
+ verify(backgroundFetchCallback, never()).shouldCancel(eq(uri), ArgumentMatchers.any(), anyLong());
+ verify(backgroundFetchCallback).failed(uri);
+ }
+
+ @Test
+ public void requestIsCancelledForVideoMimeType() {
+ verifySnoopCancelsRequestForMimeType("video/mkv", true);
+ verify(backgroundFetchCallback).failed(uri);
+ }
+
+ @Test
+ public void requestIsCancelledForAudioMimeType() {
+ verifySnoopCancelsRequestForMimeType("audio/mpeg", true);
+ verify(backgroundFetchCallback).failed(uri);
+ }
+
+ @Test
+ public void requestIsCancelledForTextMimeType() {
+ verifySnoopCancelsRequestForMimeType("text/plain", true);
+ verify(backgroundFetchCallback).failed(uri);
+ }
+
+ private void verifySnoopCancelsRequestForMimeType(String mimeType, boolean cancel) {
+ when(backgroundFetchCallback.shouldCancel(eq(uri), eq(mimeType), anyLong())).thenReturn(cancel);
+ freenetInterface.startFetch(uri, backgroundFetchCallback);
+ ArgumentCaptor snoopMetadata = forClass(SnoopMetadata.class);
+ verify(clientGetter).setMetaSnoop(snoopMetadata.capture());
+ Metadata metadata = mock(Metadata.class);
+ when(metadata.getMIMEType()).thenReturn(mimeType);
+ assertThat(snoopMetadata.getValue().snoopMetadata(metadata, mock(ClientContext.class)), is(cancel));
+ }
+
+ @Test
+ public void callbackOfBackgroundFetchIsNotifiedOnSuccess() throws Exception {
+ freenetInterface.startFetch(uri, backgroundFetchCallback);
+ verify(highLevelSimpleClient).fetch(eq(uri), anyLong(), clientGetCallback.capture(), any(FetchContext.class), anyShort());
+ when(fetchResult.getMimeType()).thenReturn("image/png");
+ when(fetchResult.asByteArray()).thenReturn(new byte[] { 1, 2, 3, 4, 5 });
+ clientGetCallback.getValue().onSuccess(fetchResult, mock(ClientGetter.class));
+ verify(backgroundFetchCallback).loaded(uri, "image/png", new byte[] { 1, 2, 3, 4, 5 });
+ verifyNoMoreInteractions(backgroundFetchCallback);
+ }
+
+ @Test
+ public void callbackOfBackgroundFetchIsNotifiedOnFailure() throws Exception {
+ freenetInterface.startFetch(uri, backgroundFetchCallback);
+ verify(highLevelSimpleClient).fetch(eq(uri), anyLong(), clientGetCallback.capture(), any(FetchContext.class), anyShort());
+ when(fetchResult.getMimeType()).thenReturn("image/png");
+ when(fetchResult.asByteArray()).thenReturn(new byte[] { 1, 2, 3, 4, 5 });
+ clientGetCallback.getValue().onFailure(new FetchException(ALL_DATA_NOT_FOUND), mock(ClientGetter.class));
+ verify(backgroundFetchCallback).failed(uri);
+ verifyNoMoreInteractions(backgroundFetchCallback);
+ }
+
+ @Test
+ public void callbackOfBackgroundFetchIsNotifiedAsFailureIfBucketCanNotBeLoaded() throws Exception {
+ freenetInterface.startFetch(uri, backgroundFetchCallback);
+ verify(highLevelSimpleClient).fetch(eq(uri), anyLong(), clientGetCallback.capture(), any(FetchContext.class), anyShort());
+ when(fetchResult.getMimeType()).thenReturn("image/png");
+ when(fetchResult.asByteArray()).thenThrow(IOException.class);
+ clientGetCallback.getValue().onSuccess(fetchResult, mock(ClientGetter.class));
+ verify(backgroundFetchCallback).failed(uri);
+ verifyNoMoreInteractions(backgroundFetchCallback);
+ }
+
}