1 package net.pterodactylus.fcp.quelaton;
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.is;
5 import static org.hamcrest.Matchers.sameInstance;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.verify;
9 import java.io.IOException;
10 import java.util.concurrent.ExecutionException;
11 import java.util.concurrent.ExecutorService;
12 import java.util.concurrent.Executors;
13 import java.util.concurrent.Future;
14 import java.util.concurrent.atomic.AtomicBoolean;
15 import java.util.concurrent.atomic.AtomicReference;
17 import net.pterodactylus.fcp.AllData;
18 import net.pterodactylus.fcp.BaseMessage;
19 import net.pterodactylus.fcp.CloseConnectionDuplicateClientName;
20 import net.pterodactylus.fcp.ConfigData;
21 import net.pterodactylus.fcp.DataFound;
22 import net.pterodactylus.fcp.EndListPeerNotes;
23 import net.pterodactylus.fcp.EndListPeers;
24 import net.pterodactylus.fcp.EndListPersistentRequests;
25 import net.pterodactylus.fcp.FCPPluginReply;
26 import net.pterodactylus.fcp.FcpConnection;
27 import net.pterodactylus.fcp.FcpMessage;
28 import net.pterodactylus.fcp.FinishedCompression;
29 import net.pterodactylus.fcp.GetFailed;
30 import net.pterodactylus.fcp.IdentifierCollision;
31 import net.pterodactylus.fcp.NodeData;
32 import net.pterodactylus.fcp.NodeHello;
33 import net.pterodactylus.fcp.Peer;
34 import net.pterodactylus.fcp.PeerNote;
35 import net.pterodactylus.fcp.PeerRemoved;
36 import net.pterodactylus.fcp.PersistentGet;
37 import net.pterodactylus.fcp.PersistentPut;
38 import net.pterodactylus.fcp.PersistentPutDir;
39 import net.pterodactylus.fcp.PersistentRequestModified;
40 import net.pterodactylus.fcp.PersistentRequestRemoved;
41 import net.pterodactylus.fcp.PluginInfo;
42 import net.pterodactylus.fcp.ProtocolError;
43 import net.pterodactylus.fcp.PutFailed;
44 import net.pterodactylus.fcp.PutFetchable;
45 import net.pterodactylus.fcp.PutSuccessful;
46 import net.pterodactylus.fcp.ReceivedBookmarkFeed;
47 import net.pterodactylus.fcp.SSKKeypair;
48 import net.pterodactylus.fcp.SentFeed;
49 import net.pterodactylus.fcp.SimpleProgress;
50 import net.pterodactylus.fcp.StartedCompression;
51 import net.pterodactylus.fcp.SubscribedUSKUpdate;
52 import net.pterodactylus.fcp.TestDDAComplete;
53 import net.pterodactylus.fcp.TestDDAReply;
54 import net.pterodactylus.fcp.URIGenerated;
55 import net.pterodactylus.fcp.UnknownNodeIdentifier;
56 import net.pterodactylus.fcp.UnknownPeerNoteType;
58 import org.junit.Test;
61 * Unit test for {@link FcpDialog}.
63 * @author <a href="bombe@freenetproject.org">David ‘Bombe’ Roden</a>
65 public class FcpDialogTest {
67 private final FcpConnection fcpConnection = mock(FcpConnection.class);
68 private final ExecutorService executorService = Executors.newSingleThreadExecutor();
69 private final TestFcpDialog dialog = new TestFcpDialog(executorService, fcpConnection);
70 private final FcpMessage fcpMessage = new FcpMessage("Test");
73 public void canSendMessage() throws IOException, ExecutionException, InterruptedException {
74 FcpDialog dialog = createBasicDialog();
75 dialog.send(fcpMessage).get();
76 verify(fcpConnection).sendMessage(fcpMessage);
79 private FcpDialog createBasicDialog() {
80 return new FcpDialog(executorService, fcpConnection) {
82 protected boolean isFinished() {
89 public void sendingAMessageRegistersTheWaiterAsFcpListener() throws IOException {
90 FcpDialog dialog = createBasicDialog();
91 dialog.send(fcpMessage);
92 verify(fcpConnection).addFcpListener(dialog);
96 public void closingTheReplyWaiterRemovesTheFcpListener() throws IOException {
97 FcpDialog dialog = createBasicDialog();
98 dialog.send(fcpMessage);
100 verify(fcpConnection).removeFcpListener(dialog);
103 private <M extends BaseMessage> void waitForASpecificMessage(MessageReceiver<M> messageReceiver, Class<M> messageClass, MessageCreator<M> messageCreator) throws IOException, InterruptedException, ExecutionException {
104 waitForASpecificMessage(messageReceiver, messageCreator.create(new FcpMessage(messageClass.getSimpleName())));
107 private <M extends BaseMessage> void waitForASpecificMessage(MessageReceiver<M> messageReceiver, M message) throws IOException, InterruptedException, ExecutionException {
108 dialog.setExpectedMessage(message.getName());
109 Future<Boolean> result = dialog.send(fcpMessage);
110 messageReceiver.receiveMessage(fcpConnection, message);
111 assertThat(result.get(), is(true));
114 private <M extends BaseMessage> M createMessage(Class<M> messageClass, MessageCreator<M> messageCreator) {
115 return messageCreator.create(new FcpMessage(messageClass.getSimpleName()));
118 private interface MessageCreator<M extends BaseMessage> {
120 M create(FcpMessage fcpMessage);
125 public void waitingForNodeHelloWorks() throws IOException, ExecutionException, InterruptedException {
126 waitForASpecificMessage(dialog::receivedNodeHello, NodeHello.class, NodeHello::new);
129 @Test(expected = ExecutionException.class)
130 public void waitingForConnectionClosedDuplicateClientNameWorks() throws IOException, ExecutionException, InterruptedException {
131 dialog.setExpectedMessage("");
132 Future<Boolean> result = dialog.send(fcpMessage);
133 dialog.receivedCloseConnectionDuplicateClientName(fcpConnection,
134 new CloseConnectionDuplicateClientName(new FcpMessage("CloseConnectionDuplicateClientName")));
139 public void waitingForSSKKeypairWorks() throws InterruptedException, ExecutionException, IOException {
140 waitForASpecificMessage(dialog::receivedSSKKeypair, SSKKeypair.class, SSKKeypair::new);
144 public void waitForPeerWorks() throws InterruptedException, ExecutionException, IOException {
145 waitForASpecificMessage(dialog::receivedPeer, Peer.class, Peer::new);
149 public void waitForEndListPeersWorks() throws InterruptedException, ExecutionException, IOException {
150 waitForASpecificMessage(dialog::receivedEndListPeers, EndListPeers.class, EndListPeers::new);
154 public void waitForPeerNoteWorks() throws InterruptedException, ExecutionException, IOException {
155 waitForASpecificMessage(dialog::receivedPeerNote, PeerNote.class, PeerNote::new);
159 public void waitForEndListPeerNotesWorks() throws InterruptedException, ExecutionException, IOException {
160 waitForASpecificMessage(dialog::receivedEndListPeerNotes, EndListPeerNotes.class, EndListPeerNotes::new);
164 public void waitForPeerRemovedWorks() throws InterruptedException, ExecutionException, IOException {
165 waitForASpecificMessage(dialog::receivedPeerRemoved, PeerRemoved.class, PeerRemoved::new);
169 public void waitForNodeDataWorks() throws InterruptedException, ExecutionException, IOException {
170 waitForASpecificMessage(dialog::receivedNodeData, new NodeData(
171 new FcpMessage("NodeData").put("ark.pubURI", "")
172 .put("ark.number", "0")
173 .put("auth.negTypes", "")
174 .put("version", "0,0,0,0")
175 .put("lastGoodVersion", "0,0,0,0")));
179 public void waitForTestDDAReplyWorks() throws InterruptedException, ExecutionException, IOException {
180 waitForASpecificMessage(dialog::receivedTestDDAReply, TestDDAReply.class, TestDDAReply::new);
184 public void waitForTestDDACompleteWorks() throws InterruptedException, ExecutionException, IOException {
185 waitForASpecificMessage(dialog::receivedTestDDAComplete, TestDDAComplete.class, TestDDAComplete::new);
189 public void waitForPersistentGetWorks() throws InterruptedException, ExecutionException, IOException {
190 waitForASpecificMessage(dialog::receivedPersistentGet, PersistentGet.class, PersistentGet::new);
194 public void waitForPersistentPutWorks() throws InterruptedException, ExecutionException, IOException {
195 waitForASpecificMessage(dialog::receivedPersistentPut, PersistentPut.class, PersistentPut::new);
199 public void waitForEndListPersistentRequestsWorks() throws InterruptedException, ExecutionException, IOException {
200 waitForASpecificMessage(dialog::receivedEndListPersistentRequests, EndListPersistentRequests.class, EndListPersistentRequests::new);
204 public void waitForURIGeneratedWorks() throws InterruptedException, ExecutionException, IOException {
205 waitForASpecificMessage(dialog::receivedURIGenerated, URIGenerated.class, URIGenerated::new);
209 public void waitForDataFoundWorks() throws InterruptedException, ExecutionException, IOException {
210 waitForASpecificMessage(dialog::receivedDataFound, DataFound.class, DataFound::new);
214 public void waitForAllDataWorks() throws InterruptedException, ExecutionException, IOException {
215 waitForASpecificMessage(dialog::receivedAllData, new AllData(new FcpMessage("AllData"), null));
219 public void waitForSimpleProgressWorks() throws InterruptedException, ExecutionException, IOException {
220 waitForASpecificMessage(dialog::receivedSimpleProgress, SimpleProgress.class, SimpleProgress::new);
224 public void waitForStartedCompressionWorks() throws InterruptedException, ExecutionException, IOException {
225 waitForASpecificMessage(dialog::receivedStartedCompression, StartedCompression.class, StartedCompression::new);
229 public void waitForFinishedCompressionWorks() throws InterruptedException, ExecutionException, IOException {
230 waitForASpecificMessage(dialog::receivedFinishedCompression, FinishedCompression.class, FinishedCompression::new);
234 public void waitForUnknownPeerNoteTypeWorks() throws InterruptedException, ExecutionException, IOException {
235 waitForASpecificMessage(dialog::receivedUnknownPeerNoteType, UnknownPeerNoteType.class, UnknownPeerNoteType::new);
239 public void waitForUnknownNodeIdentifierWorks() throws InterruptedException, ExecutionException, IOException {
240 waitForASpecificMessage(dialog::receivedUnknownNodeIdentifier, UnknownNodeIdentifier.class, UnknownNodeIdentifier::new);
244 public void waitForConfigDataWorks() throws InterruptedException, ExecutionException, IOException {
245 waitForASpecificMessage(dialog::receivedConfigData, ConfigData.class, ConfigData::new);
249 public void waitForGetFailedWorks() throws InterruptedException, ExecutionException, IOException {
250 waitForASpecificMessage(dialog::receivedGetFailed, GetFailed.class, GetFailed::new);
254 public void waitForPutFailedWorks() throws InterruptedException, ExecutionException, IOException {
255 waitForASpecificMessage(dialog::receivedPutFailed, PutFailed.class, PutFailed::new);
259 public void waitForIdentifierCollisionWorks() throws InterruptedException, ExecutionException, IOException {
260 waitForASpecificMessage(dialog::receivedIdentifierCollision, IdentifierCollision.class, IdentifierCollision::new);
264 public void waitForPersistentPutDirWorks() throws InterruptedException, ExecutionException, IOException {
265 waitForASpecificMessage(dialog::receivedPersistentPutDir, PersistentPutDir.class, PersistentPutDir::new);
269 public void waitForPersistentRequestRemovedWorks() throws InterruptedException, ExecutionException, IOException {
270 waitForASpecificMessage(dialog::receivedPersistentRequestRemoved, PersistentRequestRemoved.class, PersistentRequestRemoved::new);
274 public void waitForSubscribedUSKUpdateWorks() throws InterruptedException, ExecutionException, IOException {
275 waitForASpecificMessage(dialog::receivedSubscribedUSKUpdate, SubscribedUSKUpdate.class, SubscribedUSKUpdate::new);
279 public void waitForPluginInfoWorks() throws InterruptedException, ExecutionException, IOException {
280 waitForASpecificMessage(dialog::receivedPluginInfo, PluginInfo.class, PluginInfo::new);
284 public void waitForFCPPluginReply() throws InterruptedException, ExecutionException, IOException {
285 waitForASpecificMessage(dialog::receivedFCPPluginReply, new FCPPluginReply(new FcpMessage("FCPPluginReply"), null));
289 public void waitForPersistentRequestModifiedWorks() throws InterruptedException, ExecutionException, IOException {
290 waitForASpecificMessage(dialog::receivedPersistentRequestModified, PersistentRequestModified.class, PersistentRequestModified::new);
294 public void waitForPutSuccessfulWorks() throws InterruptedException, ExecutionException, IOException {
295 waitForASpecificMessage(dialog::receivedPutSuccessful, PutSuccessful.class, PutSuccessful::new);
299 public void waitForPutFetchableWorks() throws InterruptedException, ExecutionException, IOException {
300 waitForASpecificMessage(dialog::receivedPutFetchable, PutFetchable.class, PutFetchable::new);
304 public void waitForSentFeedWorks() throws InterruptedException, ExecutionException, IOException {
305 waitForASpecificMessage(dialog::receivedSentFeed, SentFeed.class, SentFeed::new);
309 public void waitForReceivedBookmarkFeedWorks() throws InterruptedException, ExecutionException, IOException {
310 waitForASpecificMessage(dialog::receivedBookmarkFeed, ReceivedBookmarkFeed.class, ReceivedBookmarkFeed::new);
314 public void waitForProtocolErrorWorks() throws InterruptedException, ExecutionException, IOException {
315 waitForASpecificMessage(dialog::receivedProtocolError, ProtocolError.class, ProtocolError::new);
319 public void waitForUnknownMessageWorks() throws IOException, ExecutionException, InterruptedException {
320 dialog.setExpectedMessage("SomeFcpMessage");
321 Future<Boolean> result = dialog.send(fcpMessage);
322 dialog.receivedMessage(fcpConnection, new FcpMessage("SomeFcpMessage"));
323 assertThat(result.get(), is(true));
327 public void waitingForMultipleMessagesWorks() throws IOException, ExecutionException, InterruptedException {
328 TestFcpDialog testFcpDialog = new TestFcpDialog(executorService, fcpConnection) {
329 private final AtomicBoolean gotPutFailed = new AtomicBoolean();
330 private final AtomicBoolean gotGetFailed = new AtomicBoolean();
333 protected boolean isFinished() {
334 return gotPutFailed.get() && gotGetFailed.get();
338 protected Boolean getResult() {
343 protected void consumePutFailed(PutFailed putFailed) {
344 gotPutFailed.set(true);
348 protected void consumeGetFailed(GetFailed getFailed) {
349 gotGetFailed.set(true);
352 Future<?> result = testFcpDialog.send(fcpMessage);
353 assertThat(result.isDone(), is(false));
354 testFcpDialog.receivedGetFailed(fcpConnection, new GetFailed(new FcpMessage("GetFailed")));
355 assertThat(result.isDone(), is(false));
356 testFcpDialog.receivedPutFailed(fcpConnection, new PutFailed(new FcpMessage("PutFailed")));
357 assertThat(result.get(), is(true));
361 public void waitingForConnectionClosureWorks() throws IOException, ExecutionException, InterruptedException {
362 dialog.setExpectedMessage("none");
363 Future<Boolean> result = dialog.send(fcpMessage);
364 Throwable throwable = new Throwable();
365 dialog.connectionClosed(fcpConnection, throwable);
368 } catch (ExecutionException e) {
370 while (t.getCause() != null) {
373 assertThat(t, sameInstance(throwable));
378 private interface MessageReceiver<M> {
380 void receiveMessage(FcpConnection fcpConnection, M message);
384 private static class TestFcpDialog extends FcpDialog<Boolean> {
386 private final AtomicReference<String> gotMessage = new AtomicReference<>();
387 private final AtomicReference<String> expectedMessage = new AtomicReference<>();
389 public TestFcpDialog(ExecutorService executorService, FcpConnection fcpConnection) {
390 super(executorService, fcpConnection);
393 public void setExpectedMessage(String expectedMessage) {
394 this.expectedMessage.set(expectedMessage);
398 protected boolean isFinished() {
403 protected Boolean getResult() {
404 return expectedMessage.get().equals(gotMessage.get());
408 protected void consumeNodeHello(NodeHello nodeHello) {
409 gotMessage.set(nodeHello.getName());
413 protected void consumeSSKKeypair(SSKKeypair sskKeypair) {
414 gotMessage.set(sskKeypair.getName());
418 protected void consumePeer(Peer peer) {
419 gotMessage.set(peer.getName());
423 protected void consumeEndListPeers(EndListPeers endListPeers) {
424 gotMessage.set(endListPeers.getName());
428 protected void consumePeerNote(PeerNote peerNote) {
429 gotMessage.set(peerNote.getName());
433 protected void consumeEndListPeerNotes(EndListPeerNotes endListPeerNotes) {
434 gotMessage.set(endListPeerNotes.getName());
438 protected void consumePeerRemoved(PeerRemoved peerRemoved) {
439 gotMessage.set(peerRemoved.getName());
443 protected void consumeNodeData(NodeData nodeData) {
444 gotMessage.set(nodeData.getName());
448 protected void consumeTestDDAReply(TestDDAReply testDDAReply) {
449 gotMessage.set(testDDAReply.getName());
453 protected void consumeTestDDAComplete(TestDDAComplete testDDAComplete) {
454 gotMessage.set(testDDAComplete.getName());
458 protected void consumePersistentGet(PersistentGet persistentGet) {
459 gotMessage.set(persistentGet.getName());
463 protected void consumePersistentPut(PersistentPut persistentPut) {
464 gotMessage.set(persistentPut.getName());
468 protected void consumeEndListPersistentRequests(EndListPersistentRequests endListPersistentRequests) {
469 gotMessage.set(endListPersistentRequests.getName());
473 protected void consumeURIGenerated(URIGenerated uriGenerated) {
474 gotMessage.set(uriGenerated.getName());
478 protected void consumeDataFound(DataFound dataFound) {
479 gotMessage.set(dataFound.getName());
483 protected void consumeAllData(AllData allData) {
484 gotMessage.set(allData.getName());
488 protected void consumeSimpleProgress(SimpleProgress simpleProgress) {
489 gotMessage.set(simpleProgress.getName());
493 protected void consumeStartedCompression(StartedCompression startedCompression) {
494 gotMessage.set(startedCompression.getName());
498 protected void consumeFinishedCompression(FinishedCompression finishedCompression) {
499 gotMessage.set(finishedCompression.getName());
503 protected void consumeUnknownPeerNoteType(UnknownPeerNoteType unknownPeerNoteType) {
504 gotMessage.set(unknownPeerNoteType.getName());
508 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
509 gotMessage.set(unknownNodeIdentifier.getName());
513 protected void consumeConfigData(ConfigData configData) {
514 gotMessage.set(configData.getName());
518 protected void consumeGetFailed(GetFailed getFailed) {
519 gotMessage.set(getFailed.getName());
523 protected void consumePutFailed(PutFailed putFailed) {
524 gotMessage.set(putFailed.getName());
528 protected void consumeIdentifierCollision(IdentifierCollision identifierCollision) {
529 gotMessage.set(identifierCollision.getName());
533 protected void consumePersistentPutDir(PersistentPutDir persistentPutDir) {
534 gotMessage.set(persistentPutDir.getName());
538 protected void consumePersistentRequestRemoved(PersistentRequestRemoved persistentRequestRemoved) {
539 gotMessage.set(persistentRequestRemoved.getName());
543 protected void consumeSubscribedUSKUpdate(SubscribedUSKUpdate subscribedUSKUpdate) {
544 gotMessage.set(subscribedUSKUpdate.getName());
548 protected void consumePluginInfo(PluginInfo pluginInfo) {
549 gotMessage.set(pluginInfo.getName());
553 protected void consumeFCPPluginReply(FCPPluginReply fcpPluginReply) {
554 gotMessage.set(fcpPluginReply.getName());
558 protected void consumePersistentRequestModified(PersistentRequestModified persistentRequestModified) {
559 gotMessage.set(persistentRequestModified.getName());
563 protected void consumePutSuccessful(PutSuccessful putSuccessful) {
564 gotMessage.set(putSuccessful.getName());
568 protected void consumePutFetchable(PutFetchable putFetchable) {
569 gotMessage.set(putFetchable.getName());
573 protected void consumeSentFeed(SentFeed sentFeed) {
574 gotMessage.set(sentFeed.getName());
578 protected void consumeReceivedBookmarkFeed(ReceivedBookmarkFeed receivedBookmarkFeed) {
579 gotMessage.set(receivedBookmarkFeed.getName());
583 protected void consumeProtocolError(ProtocolError protocolError) {
584 gotMessage.set(protocolError.getName());
588 protected void consumeUnknownMessage(FcpMessage fcpMessage) {
589 gotMessage.set(fcpMessage.getName());