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;
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.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.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;
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
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);
@Test
public void insertTokenSupplierSuppliesInsertTokens() {
- InsertTokenSupplier insertTokenSupplier = freenetInterface.new InsertTokenSupplier();
+ InsertTokenSupplier insertTokenSupplier = new InsertTokenSupplier(freenetInterface);
assertThat(insertTokenSupplier.apply(image), notNullValue());
}
}
@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.<String>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> 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());