X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FFreenetInterfaceTest.java;h=c91de6887bfa068949539f77c803e390fb457eec;hb=03cec6a6772c2d836d94864adddaf544cbe9d72f;hp=a8f9812385361a69fc8bdd7d4dd20a1b9dc3f7b4;hpb=4115cb08fe44c1ad7b62ca4771020e21fbb02a4d;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 a8f9812..c91de68 100644
--- a/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java
+++ b/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java
@@ -4,8 +4,8 @@ import static freenet.client.FetchException.FetchExceptionMode.ALL_DATA_NOT_FOUN
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;
@@ -29,10 +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;
@@ -40,9 +38,10 @@ 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;
@@ -54,9 +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;
@@ -80,8 +82,6 @@ import org.mockito.ArgumentMatchers;
/**
* Unit test for {@link FreenetInterface}.
- *
- * @author David âBombeâ Roden
*/
public class FreenetInterfaceTest {
@@ -101,13 +101,15 @@ public class FreenetInterfaceTest {
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() {
+ 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
@@ -116,6 +118,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);
@@ -209,7 +212,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);
@@ -234,7 +237,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
@@ -242,7 +245,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
@@ -266,9 +269,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
@@ -276,9 +277,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
@@ -414,7 +413,7 @@ public class FreenetInterfaceTest {
@Test
public void insertTokenSupplierSuppliesInsertTokens() {
- InsertTokenSupplier insertTokenSupplier = freenetInterface.new InsertTokenSupplier();
+ InsertTokenSupplier insertTokenSupplier = new InsertTokenSupplier(freenetInterface);
assertThat(insertTokenSupplier.apply(image), notNullValue());
}
@@ -425,6 +424,54 @@ public class FreenetInterfaceTest {
}
@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());