1 package net.pterodactylus.sone.core;
3 import static freenet.client.InsertException.CANCELLED;
4 import static freenet.client.InsertException.INTERNAL_ERROR;
5 import static freenet.keys.InsertableClientSSK.createRandom;
6 import static freenet.node.RequestStarter.INTERACTIVE_PRIORITY_CLASS;
7 import static freenet.node.RequestStarter.PREFETCH_PRIORITY_CLASS;
8 import static java.lang.System.currentTimeMillis;
9 import static java.util.concurrent.TimeUnit.DAYS;
10 import static java.util.concurrent.TimeUnit.SECONDS;
11 import static net.pterodactylus.sone.Matchers.delivers;
12 import static net.pterodactylus.sone.TestUtil.setFinalField;
13 import static org.hamcrest.MatcherAssert.assertThat;
14 import static org.hamcrest.Matchers.is;
15 import static org.hamcrest.Matchers.notNullValue;
16 import static org.hamcrest.Matchers.nullValue;
17 import static org.mockito.ArgumentCaptor.forClass;
18 import static org.mockito.Matchers.any;
19 import static org.mockito.Matchers.anyBoolean;
20 import static org.mockito.Matchers.anyShort;
21 import static org.mockito.Matchers.eq;
22 import static org.mockito.Mockito.doAnswer;
23 import static org.mockito.Mockito.doNothing;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.never;
26 import static org.mockito.Mockito.times;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29 import static org.mockito.Mockito.withSettings;
31 import java.io.IOException;
32 import java.net.MalformedURLException;
33 import java.util.HashMap;
34 import java.util.concurrent.CountDownLatch;
36 import net.pterodactylus.sone.TestUtil;
37 import net.pterodactylus.sone.core.FreenetInterface.Callback;
38 import net.pterodactylus.sone.core.FreenetInterface.Fetched;
39 import net.pterodactylus.sone.core.FreenetInterface.InsertToken;
40 import net.pterodactylus.sone.core.FreenetInterface.InsertTokenSupplier;
41 import net.pterodactylus.sone.core.event.ImageInsertAbortedEvent;
42 import net.pterodactylus.sone.core.event.ImageInsertFailedEvent;
43 import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
44 import net.pterodactylus.sone.core.event.ImageInsertStartedEvent;
45 import net.pterodactylus.sone.data.Image;
46 import net.pterodactylus.sone.data.ImageImpl;
47 import net.pterodactylus.sone.data.Sone;
48 import net.pterodactylus.sone.data.TemporaryImage;
49 import net.pterodactylus.sone.freenet.StringBucket;
51 import freenet.client.ClientMetadata;
52 import freenet.client.FetchException;
53 import freenet.client.FetchResult;
54 import freenet.client.HighLevelSimpleClient;
55 import freenet.client.InsertBlock;
56 import freenet.client.InsertContext;
57 import freenet.client.InsertException;
58 import freenet.client.async.ClientPutter;
59 import freenet.client.async.USKCallback;
60 import freenet.client.async.USKManager;
61 import freenet.crypt.DummyRandomSource;
62 import freenet.crypt.RandomSource;
63 import freenet.keys.FreenetURI;
64 import freenet.keys.InsertableClientSSK;
65 import freenet.keys.USK;
66 import freenet.node.Node;
67 import freenet.node.NodeClientCore;
68 import freenet.node.RequestClient;
69 import freenet.support.Base64;
70 import freenet.support.api.Bucket;
72 import com.google.common.eventbus.EventBus;
73 import org.junit.Before;
74 import org.junit.Test;
75 import org.mockito.ArgumentCaptor;
76 import org.mockito.invocation.InvocationOnMock;
77 import org.mockito.stubbing.Answer;
80 * Unit test for {@link FreenetInterface}.
82 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
84 public class FreenetInterfaceTest {
86 private final EventBus eventBus = mock(EventBus.class);
87 private final Node node = mock(Node.class);
88 private final NodeClientCore nodeClientCore = mock(NodeClientCore.class);
89 private final HighLevelSimpleClient highLevelSimpleClient = mock(HighLevelSimpleClient.class, withSettings().extraInterfaces(RequestClient.class));
90 private final RandomSource randomSource = new DummyRandomSource();
91 private final USKManager uskManager = mock(USKManager.class);
92 private FreenetInterface freenetInterface;
93 private final Sone sone = mock(Sone.class);
94 private final ArgumentCaptor<USKCallback> callbackCaptor = forClass(USKCallback.class);
95 private final SoneDownloader soneDownloader = mock(SoneDownloader.class);
96 private final Image image = mock(Image.class);
97 private InsertToken insertToken;
100 public void setupFreenetInterface() {
101 when(nodeClientCore.makeClient(anyShort(), anyBoolean(), anyBoolean())).thenReturn(highLevelSimpleClient);
102 setFinalField(node, "clientCore", nodeClientCore);
103 setFinalField(node, "random", randomSource);
104 setFinalField(nodeClientCore, "uskManager", uskManager);
105 freenetInterface = new FreenetInterface(eventBus, node);
106 insertToken = freenetInterface.new InsertToken(image);
110 public void setupSone() {
111 InsertableClientSSK insertSsk = createRandom(randomSource, "test-0");
112 when(sone.getId()).thenReturn(Base64.encode(insertSsk.getURI().getRoutingKey()));
113 when(sone.getRequestUri()).thenReturn(insertSsk.getURI().uskForSSK());
117 public void setupCallbackCaptorAndUskManager() {
118 doNothing().when(uskManager).subscribe(any(USK.class), callbackCaptor.capture(), anyBoolean(), any(RequestClient.class));
122 public void canFetchUri() throws MalformedURLException, FetchException {
123 FreenetURI freenetUri = new FreenetURI("KSK@GPLv3.txt");
124 FetchResult fetchResult = createFetchResult();
125 when(highLevelSimpleClient.fetch(freenetUri)).thenReturn(fetchResult);
126 Fetched fetched = freenetInterface.fetchUri(freenetUri);
127 assertThat(fetched, notNullValue());
128 assertThat(fetched.getFetchResult(), is(fetchResult));
129 assertThat(fetched.getFreenetUri(), is(freenetUri));
133 public void fetchFollowsRedirect() throws MalformedURLException, FetchException {
134 FreenetURI freenetUri = new FreenetURI("KSK@GPLv2.txt");
135 FreenetURI newFreenetUri = new FreenetURI("KSK@GPLv3.txt");
136 FetchResult fetchResult = createFetchResult();
137 FetchException fetchException = new FetchException(FetchException.PERMANENT_REDIRECT, newFreenetUri);
138 when(highLevelSimpleClient.fetch(freenetUri)).thenThrow(fetchException);
139 when(highLevelSimpleClient.fetch(newFreenetUri)).thenReturn(fetchResult);
140 Fetched fetched = freenetInterface.fetchUri(freenetUri);
141 assertThat(fetched.getFetchResult(), is(fetchResult));
142 assertThat(fetched.getFreenetUri(), is(newFreenetUri));
146 public void fetchReturnsNullOnFetchExceptions() throws MalformedURLException, FetchException {
147 FreenetURI freenetUri = new FreenetURI("KSK@GPLv2.txt");
148 FetchException fetchException = new FetchException(FetchException.ALL_DATA_NOT_FOUND);
149 when(highLevelSimpleClient.fetch(freenetUri)).thenThrow(fetchException);
150 Fetched fetched = freenetInterface.fetchUri(freenetUri);
151 assertThat(fetched, nullValue());
154 private FetchResult createFetchResult() {
155 ClientMetadata clientMetadata = new ClientMetadata("text/plain");
156 Bucket bucket = new StringBucket("Some Data.");
157 return new FetchResult(clientMetadata, bucket);
161 public void insertingAnImage() throws SoneException, InsertException, IOException {
162 TemporaryImage temporaryImage = new TemporaryImage("image-id");
163 temporaryImage.setMimeType("image/png");
164 byte[] imageData = new byte[] { 1, 2, 3, 4 };
165 temporaryImage.setImageData(imageData);
166 Image image = new ImageImpl("image-id");
167 InsertToken insertToken = freenetInterface.new InsertToken(image);
168 InsertContext insertContext = mock(InsertContext.class);
169 when(highLevelSimpleClient.getInsertContext(anyBoolean())).thenReturn(insertContext);
170 ClientPutter clientPutter = mock(ClientPutter.class);
171 ArgumentCaptor<InsertBlock> insertBlockCaptor = forClass(InsertBlock.class);
172 when(highLevelSimpleClient.insert(insertBlockCaptor.capture(), eq(false), eq((String) null), eq(false), eq(insertContext), eq(insertToken), anyShort())).thenReturn(clientPutter);
173 freenetInterface.insertImage(temporaryImage, image, insertToken);
174 assertThat(insertBlockCaptor.getValue().getData().getInputStream(), delivers(new byte[] { 1, 2, 3, 4 }));
175 assertThat(TestUtil.<ClientPutter>getPrivateField(insertToken, "clientPutter"), is(clientPutter));
176 verify(eventBus).post(any(ImageInsertStartedEvent.class));
179 @Test(expected = SoneInsertException.class)
180 public void insertExceptionCausesASoneException() throws InsertException, SoneException, IOException {
181 TemporaryImage temporaryImage = new TemporaryImage("image-id");
182 temporaryImage.setMimeType("image/png");
183 byte[] imageData = new byte[] { 1, 2, 3, 4 };
184 temporaryImage.setImageData(imageData);
185 Image image = new ImageImpl("image-id");
186 InsertToken insertToken = freenetInterface.new InsertToken(image);
187 InsertContext insertContext = mock(InsertContext.class);
188 when(highLevelSimpleClient.getInsertContext(anyBoolean())).thenReturn(insertContext);
189 ArgumentCaptor<InsertBlock> insertBlockCaptor = forClass(InsertBlock.class);
190 when(highLevelSimpleClient.insert(insertBlockCaptor.capture(), eq(false), eq((String) null), eq(false), eq(insertContext), eq(insertToken), anyShort())).thenThrow(InsertException.class);
191 freenetInterface.insertImage(temporaryImage, image, insertToken);
195 public void insertingADirectory() throws InsertException, SoneException {
196 FreenetURI freenetUri = mock(FreenetURI.class);
197 HashMap<String, Object> manifestEntries = new HashMap<String, Object>();
198 String defaultFile = "index.html";
199 FreenetURI resultingUri = mock(FreenetURI.class);
200 when(highLevelSimpleClient.insertManifest(eq(freenetUri), eq(manifestEntries), eq(defaultFile))).thenReturn(resultingUri);
201 assertThat(freenetInterface.insertDirectory(freenetUri, manifestEntries, defaultFile), is(resultingUri));
204 @Test(expected = SoneException.class)
205 public void insertExceptionIsForwardedAsSoneException() throws InsertException, SoneException {
206 when(highLevelSimpleClient.insertManifest(any(FreenetURI.class), any(HashMap.class), any(String.class))).thenThrow(InsertException.class);
207 freenetInterface.insertDirectory(null, null, null);
211 public void soneWithWrongRequestUriWillNotBeSubscribed() throws MalformedURLException {
212 when(sone.getRequestUri()).thenReturn(new FreenetURI("KSK@GPLv3.txt"));
213 freenetInterface.registerUsk(sone, null);
214 verify(uskManager, never()).subscribe(any(USK.class), any(USKCallback.class), anyBoolean(), any(RequestClient.class));
218 public void registeringAUskForARecentlyModifiedSone() throws MalformedURLException {
219 when(sone.getTime()).thenReturn(currentTimeMillis() - DAYS.toMillis(1));
220 freenetInterface.registerUsk(sone, null);
221 verify(uskManager).subscribe(any(USK.class), any(USKCallback.class), eq(true), eq((RequestClient) highLevelSimpleClient));
225 public void registeringAUskForAnOldSone() throws MalformedURLException {
226 when(sone.getTime()).thenReturn(currentTimeMillis() - DAYS.toMillis(365));
227 freenetInterface.registerUsk(sone, null);
228 verify(uskManager).subscribe(any(USK.class), any(USKCallback.class), eq(false), eq((RequestClient) highLevelSimpleClient));
232 public void registeringAUsk() {
233 FreenetURI freenetUri = createRandom(randomSource, "test-0").getURI().uskForSSK();
234 Callback callback = mock(Callback.class);
235 freenetInterface.registerUsk(freenetUri, callback);
236 verify(uskManager).subscribe(any(USK.class), any(USKCallback.class), anyBoolean(), eq((RequestClient) highLevelSimpleClient));
240 public void registeringANonUskKeyWillNotBeSubscribed() throws MalformedURLException {
241 FreenetURI freenetUri = new FreenetURI("KSK@GPLv3.txt");
242 Callback callback = mock(Callback.class);
243 freenetInterface.registerUsk(freenetUri, callback);
244 verify(uskManager, never()).subscribe(any(USK.class), any(USKCallback.class), anyBoolean(), eq((RequestClient) highLevelSimpleClient));
248 public void unregisteringANotRegisteredUskDoesNothing() {
249 FreenetURI freenetURI = createRandom(randomSource, "test-0").getURI().uskForSSK();
250 freenetInterface.unregisterUsk(freenetURI);
251 verify(uskManager, never()).unsubscribe(any(USK.class), any(USKCallback.class));
255 public void unregisteringARegisteredUsk() {
256 FreenetURI freenetURI = createRandom(randomSource, "test-0").getURI().uskForSSK();
257 Callback callback = mock(Callback.class);
258 freenetInterface.registerUsk(freenetURI, callback);
259 freenetInterface.unregisterUsk(freenetURI);
260 verify(uskManager).unsubscribe(any(USK.class), any(USKCallback.class));
264 public void unregisteringANotRegisteredSoneDoesNothing() {
265 freenetInterface.unregisterUsk(sone);
266 verify(uskManager, never()).unsubscribe(any(USK.class), any(USKCallback.class));
270 public void unregisteringARegisteredSoneUnregistersTheSone() {
271 freenetInterface.registerUsk(sone, null);
272 freenetInterface.unregisterUsk(sone);
273 verify(uskManager).unsubscribe(any(USK.class), any(USKCallback.class));
277 public void unregisteringASoneWithAWrongRequestKeyWillNotUnsubscribe() throws MalformedURLException {
278 freenetInterface.registerUsk(sone, null);
279 when(sone.getRequestUri()).thenReturn(new FreenetURI("KSK@GPLv3.txt"));
280 freenetInterface.unregisterUsk(sone);
281 verify(uskManager, never()).unsubscribe(any(USK.class), any(USKCallback.class));
285 public void callbackPrioritiesAreInteractive() {
286 freenetInterface.registerUsk(sone, null);
287 assertThat(callbackCaptor.getValue().getPollingPriorityNormal(), is(INTERACTIVE_PRIORITY_CLASS));
288 assertThat(callbackCaptor.getValue().getPollingPriorityProgress(), is(INTERACTIVE_PRIORITY_CLASS));
292 public void callbackForRegisteredSoneWithHigherEditionTriggersDownload() throws InterruptedException {
293 freenetInterface.registerUsk(sone, soneDownloader);
294 final CountDownLatch downloadTriggered = new CountDownLatch(1);
295 doAnswer(new Answer<Void>() {
297 public Void answer(InvocationOnMock invocation) throws Throwable {
298 downloadTriggered.countDown();
301 }).when(soneDownloader).fetchSone(sone);
302 callbackCaptor.getValue().onFoundEdition(1, null, null, null, false, (short) 0, null, false, false);
303 assertThat(downloadTriggered.await(1, SECONDS), is(true));
307 public void callbackForRegisteredSoneWithTheSameEditionDoesNotTriggerDownload() throws InterruptedException {
308 freenetInterface.registerUsk(sone, soneDownloader);
309 final CountDownLatch downloadTriggered = new CountDownLatch(1);
310 doAnswer(new Answer<Void>() {
312 public Void answer(InvocationOnMock invocation) throws Throwable {
313 downloadTriggered.countDown();
316 }).when(soneDownloader).fetchSone(sone);
317 callbackCaptor.getValue().onFoundEdition(0, null, null, null, false, (short) 0, null, false, false);
318 assertThat(downloadTriggered.await(1, SECONDS), is(false));
322 public void callbackForNormalUskUsesDifferentPriorities() {
323 Callback callback = mock(Callback.class);
324 FreenetURI uri = createRandom(randomSource, "test-0").getURI().uskForSSK();
325 freenetInterface.registerUsk(uri, callback);
326 assertThat(callbackCaptor.getValue().getPollingPriorityNormal(), is(PREFETCH_PRIORITY_CLASS));
327 assertThat(callbackCaptor.getValue().getPollingPriorityProgress(), is(INTERACTIVE_PRIORITY_CLASS));
331 public void callbackForNormalUskForwardsImportantParameters() throws MalformedURLException {
332 Callback callback = mock(Callback.class);
333 FreenetURI uri = createRandom(randomSource, "test-0").getURI().uskForSSK();
334 freenetInterface.registerUsk(uri, callback);
335 USK key = mock(USK.class);
336 when(key.getURI()).thenReturn(uri);
337 callbackCaptor.getValue().onFoundEdition(3, key, null, null, false, (short) 0, null, true, true);
338 verify(callback).editionFound(eq(uri), eq(3L), eq(true), eq(true));
342 public void fetchedRetainsUriAndFetchResult() {
343 FreenetURI freenetUri = mock(FreenetURI.class);
344 FetchResult fetchResult = mock(FetchResult.class);
345 Fetched fetched = new Fetched(freenetUri, fetchResult);
346 assertThat(fetched.getFreenetUri(), is(freenetUri));
347 assertThat(fetched.getFetchResult(), is(fetchResult));
351 public void cancellingAnInsertWillFireImageInsertAbortedEvent() {
352 ClientPutter clientPutter = mock(ClientPutter.class);
353 insertToken.setClientPutter(clientPutter);
354 ArgumentCaptor<ImageInsertStartedEvent> imageInsertStartedEvent = forClass(ImageInsertStartedEvent.class);
355 verify(eventBus).post(imageInsertStartedEvent.capture());
356 assertThat(imageInsertStartedEvent.getValue().image(), is(image));
357 insertToken.cancel();
358 ArgumentCaptor<ImageInsertAbortedEvent> imageInsertAbortedEvent = forClass(ImageInsertAbortedEvent.class);
359 verify(eventBus, times(2)).post(imageInsertAbortedEvent.capture());
360 assertThat(imageInsertAbortedEvent.getValue().image(), is(image));
364 public void failureWithoutExceptionSendsFailedEvent() {
365 insertToken.onFailure(null, null, null);
366 ArgumentCaptor<ImageInsertFailedEvent> imageInsertFailedEvent = forClass(ImageInsertFailedEvent.class);
367 verify(eventBus).post(imageInsertFailedEvent.capture());
368 assertThat(imageInsertFailedEvent.getValue().image(), is(image));
369 assertThat(imageInsertFailedEvent.getValue().cause(), nullValue());
373 public void failureSendsFailedEventWithException() {
374 InsertException insertException = new InsertException(INTERNAL_ERROR, "Internal error", null);
375 insertToken.onFailure(insertException, null, null);
376 ArgumentCaptor<ImageInsertFailedEvent> imageInsertFailedEvent = forClass(ImageInsertFailedEvent.class);
377 verify(eventBus).post(imageInsertFailedEvent.capture());
378 assertThat(imageInsertFailedEvent.getValue().image(), is(image));
379 assertThat(imageInsertFailedEvent.getValue().cause(), is((Throwable) insertException));
383 public void failureBecauseCancelledByUserSendsAbortedEvent() {
384 InsertException insertException = new InsertException(CANCELLED, null);
385 insertToken.onFailure(insertException, null, null);
386 ArgumentCaptor<ImageInsertAbortedEvent> imageInsertAbortedEvent = forClass(ImageInsertAbortedEvent.class);
387 verify(eventBus).post(imageInsertAbortedEvent.capture());
388 assertThat(imageInsertAbortedEvent.getValue().image(), is(image));
392 public void ignoredMethodsDoNotThrowExceptions() {
393 insertToken.onMajorProgress(null);
394 insertToken.onFetchable(null, null);
395 insertToken.onGeneratedMetadata(null, null, null);
399 public void generatedUriIsPostedOnSuccess() {
400 FreenetURI generatedUri = mock(FreenetURI.class);
401 insertToken.onGeneratedURI(generatedUri, null, null);
402 insertToken.onSuccess(null, null);
403 ArgumentCaptor<ImageInsertFinishedEvent> imageInsertFinishedEvent = forClass(ImageInsertFinishedEvent.class);
404 verify(eventBus).post(imageInsertFinishedEvent.capture());
405 assertThat(imageInsertFinishedEvent.getValue().image(), is(image));
406 assertThat(imageInsertFinishedEvent.getValue().resultingUri(), is(generatedUri));
410 public void insertTokenSupplierSuppliesInsertTokens() {
411 InsertTokenSupplier insertTokenSupplier = freenetInterface.new InsertTokenSupplier();
412 assertThat(insertTokenSupplier.apply(image), notNullValue());