1 package net.pterodactylus.fcp.quelaton;
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.empty;
5 import static org.hamcrest.Matchers.is;
6 import static org.hamcrest.Matchers.sameInstance;
7 import static org.mockito.Mockito.mock;
8 import static org.mockito.Mockito.verify;
10 import java.io.IOException;
11 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.ExecutorService;
13 import java.util.concurrent.Executors;
14 import java.util.concurrent.Future;
15 import java.util.concurrent.atomic.AtomicBoolean;
16 import java.util.concurrent.atomic.AtomicReference;
18 import net.pterodactylus.fcp.AllData;
19 import net.pterodactylus.fcp.BaseMessage;
20 import net.pterodactylus.fcp.CloseConnectionDuplicateClientName;
21 import net.pterodactylus.fcp.ConfigData;
22 import net.pterodactylus.fcp.DataFound;
23 import net.pterodactylus.fcp.EndListPeerNotes;
24 import net.pterodactylus.fcp.EndListPeers;
25 import net.pterodactylus.fcp.EndListPersistentRequests;
26 import net.pterodactylus.fcp.FCPPluginReply;
27 import net.pterodactylus.fcp.FcpConnection;
28 import net.pterodactylus.fcp.FcpMessage;
29 import net.pterodactylus.fcp.FinishedCompression;
30 import net.pterodactylus.fcp.GetFailed;
31 import net.pterodactylus.fcp.IdentifierCollision;
32 import net.pterodactylus.fcp.NodeData;
33 import net.pterodactylus.fcp.NodeHello;
34 import net.pterodactylus.fcp.Peer;
35 import net.pterodactylus.fcp.PeerNote;
36 import net.pterodactylus.fcp.PeerRemoved;
37 import net.pterodactylus.fcp.PersistentGet;
38 import net.pterodactylus.fcp.PersistentPut;
39 import net.pterodactylus.fcp.PersistentPutDir;
40 import net.pterodactylus.fcp.PersistentRequestModified;
41 import net.pterodactylus.fcp.PersistentRequestRemoved;
42 import net.pterodactylus.fcp.PluginInfo;
43 import net.pterodactylus.fcp.ProtocolError;
44 import net.pterodactylus.fcp.PutFailed;
45 import net.pterodactylus.fcp.PutFetchable;
46 import net.pterodactylus.fcp.PutSuccessful;
47 import net.pterodactylus.fcp.ReceivedBookmarkFeed;
48 import net.pterodactylus.fcp.SSKKeypair;
49 import net.pterodactylus.fcp.SentFeed;
50 import net.pterodactylus.fcp.SimpleProgress;
51 import net.pterodactylus.fcp.StartedCompression;
52 import net.pterodactylus.fcp.SubscribedUSKUpdate;
53 import net.pterodactylus.fcp.TestDDAComplete;
54 import net.pterodactylus.fcp.TestDDAReply;
55 import net.pterodactylus.fcp.URIGenerated;
56 import net.pterodactylus.fcp.UnknownNodeIdentifier;
57 import net.pterodactylus.fcp.UnknownPeerNoteType;
59 import org.junit.Test;
62 * Unit test for {@link FcpReplySequence}.
64 * @author <a href="bombe@freenetproject.org">David ‘Bombe’ Roden</a>
66 public class FcpReplySequenceTest {
68 private final FcpConnection fcpConnection = mock(FcpConnection.class);
69 private final ExecutorService executorService = Executors.newSingleThreadExecutor();
70 private final TestFcpReplySequence replySequence = new TestFcpReplySequence(executorService, fcpConnection);
71 private final FcpMessage fcpMessage = new FcpMessage("Test");
74 public void canSendMessage() throws IOException, ExecutionException, InterruptedException {
75 FcpReplySequence replySequence = createBasicReplySequence();
76 replySequence.send(fcpMessage).get();
77 verify(fcpConnection).sendMessage(fcpMessage);
80 private FcpReplySequence createBasicReplySequence() {
81 return new FcpReplySequence(executorService, fcpConnection) {
83 protected boolean isFinished() {
90 public void sendingAMessageRegistersTheWaiterAsFcpListener() throws IOException {
91 FcpReplySequence replySequence = createBasicReplySequence();
92 replySequence.send(fcpMessage);
93 verify(fcpConnection).addFcpListener(replySequence);
97 public void closingTheReplyWaiterRemovesTheFcpListener() throws IOException {
98 FcpReplySequence replySequence = createBasicReplySequence();
99 replySequence.send(fcpMessage);
100 replySequence.close();
101 verify(fcpConnection).removeFcpListener(replySequence);
104 private <M extends BaseMessage> void waitForASpecificMessage(MessageReceiver<M> messageReceiver, Class<M> messageClass, MessageCreator<M> messageCreator) throws IOException, InterruptedException, ExecutionException {
105 waitForASpecificMessage(messageReceiver, messageCreator.create(new FcpMessage(messageClass.getSimpleName())));
108 private <M extends BaseMessage> void waitForASpecificMessage(MessageReceiver<M> messageReceiver, M message) throws IOException, InterruptedException, ExecutionException {
109 replySequence.setExpectedMessage(message.getName());
110 Future<Boolean> result = replySequence.send(fcpMessage);
111 messageReceiver.receiveMessage(fcpConnection, message);
112 assertThat(result.get(), is(true));
115 private <M extends BaseMessage> M createMessage(Class<M> messageClass, MessageCreator<M> messageCreator) {
116 return messageCreator.create(new FcpMessage(messageClass.getSimpleName()));
119 private interface MessageCreator<M extends BaseMessage> {
121 M create(FcpMessage fcpMessage);
126 public void waitingForNodeHelloWorks() throws IOException, ExecutionException, InterruptedException {
127 waitForASpecificMessage(replySequence::receivedNodeHello, NodeHello.class, NodeHello::new);
131 public void waitingForConnectionClosedDuplicateClientNameWorks() throws IOException, ExecutionException, InterruptedException {
132 waitForASpecificMessage( replySequence::receivedCloseConnectionDuplicateClientName, CloseConnectionDuplicateClientName.class, CloseConnectionDuplicateClientName::new);
136 public void waitingForSSKKeypairWorks() throws InterruptedException, ExecutionException, IOException {
137 waitForASpecificMessage(replySequence::receivedSSKKeypair, SSKKeypair.class, SSKKeypair::new);
141 public void waitForPeerWorks() throws InterruptedException, ExecutionException, IOException {
142 waitForASpecificMessage(replySequence::receivedPeer, Peer.class, Peer::new);
146 public void waitForEndListPeersWorks() throws InterruptedException, ExecutionException, IOException {
147 waitForASpecificMessage(replySequence::receivedEndListPeers, EndListPeers.class, EndListPeers::new);
151 public void waitForPeerNoteWorks() throws InterruptedException, ExecutionException, IOException {
152 waitForASpecificMessage(replySequence::receivedPeerNote, PeerNote.class, PeerNote::new);
156 public void waitForEndListPeerNotesWorks() throws InterruptedException, ExecutionException, IOException {
157 waitForASpecificMessage(replySequence::receivedEndListPeerNotes, EndListPeerNotes.class, EndListPeerNotes::new);
161 public void waitForPeerRemovedWorks() throws InterruptedException, ExecutionException, IOException {
162 waitForASpecificMessage(replySequence::receivedPeerRemoved, PeerRemoved.class, PeerRemoved::new);
166 public void waitForNodeDataWorks() throws InterruptedException, ExecutionException, IOException {
167 waitForASpecificMessage(replySequence::receivedNodeData, new NodeData(
168 new FcpMessage("NodeData").put("ark.pubURI", "")
169 .put("ark.number", "0")
170 .put("auth.negTypes", "")
171 .put("version", "0,0,0,0")
172 .put("lastGoodVersion", "0,0,0,0")));
176 public void waitForTestDDAReplyWorks() throws InterruptedException, ExecutionException, IOException {
177 waitForASpecificMessage(replySequence::receivedTestDDAReply, TestDDAReply.class, TestDDAReply::new);
181 public void waitForTestDDACompleteWorks() throws InterruptedException, ExecutionException, IOException {
182 waitForASpecificMessage(replySequence::receivedTestDDAComplete, TestDDAComplete.class, TestDDAComplete::new);
186 public void waitForPersistentGetWorks() throws InterruptedException, ExecutionException, IOException {
187 waitForASpecificMessage(replySequence::receivedPersistentGet, PersistentGet.class, PersistentGet::new);
191 public void waitForPersistentPutWorks() throws InterruptedException, ExecutionException, IOException {
192 waitForASpecificMessage(replySequence::receivedPersistentPut, PersistentPut.class, PersistentPut::new);
196 public void waitForEndListPersistentRequestsWorks() throws InterruptedException, ExecutionException, IOException {
197 waitForASpecificMessage(replySequence::receivedEndListPersistentRequests, EndListPersistentRequests.class, EndListPersistentRequests::new);
201 public void waitForURIGeneratedWorks() throws InterruptedException, ExecutionException, IOException {
202 waitForASpecificMessage(replySequence::receivedURIGenerated, URIGenerated.class, URIGenerated::new);
206 public void waitForDataFoundWorks() throws InterruptedException, ExecutionException, IOException {
207 waitForASpecificMessage(replySequence::receivedDataFound, DataFound.class, DataFound::new);
211 public void waitForAllDataWorks() throws InterruptedException, ExecutionException, IOException {
212 waitForASpecificMessage(replySequence::receivedAllData, new AllData(new FcpMessage("AllData"), null));
216 public void waitForSimpleProgressWorks() throws InterruptedException, ExecutionException, IOException {
217 waitForASpecificMessage(replySequence::receivedSimpleProgress, SimpleProgress.class, SimpleProgress::new);
221 public void waitForStartedCompressionWorks() throws InterruptedException, ExecutionException, IOException {
222 waitForASpecificMessage(replySequence::receivedStartedCompression, StartedCompression.class, StartedCompression::new);
226 public void waitForFinishedCompressionWorks() throws InterruptedException, ExecutionException, IOException {
227 waitForASpecificMessage(replySequence::receivedFinishedCompression, FinishedCompression.class, FinishedCompression::new);
231 public void waitForUnknownPeerNoteTypeWorks() throws InterruptedException, ExecutionException, IOException {
232 waitForASpecificMessage(replySequence::receivedUnknownPeerNoteType, UnknownPeerNoteType.class, UnknownPeerNoteType::new);
236 public void waitForUnknownNodeIdentifierWorks() throws InterruptedException, ExecutionException, IOException {
237 waitForASpecificMessage(replySequence::receivedUnknownNodeIdentifier, UnknownNodeIdentifier.class, UnknownNodeIdentifier::new);
241 public void waitForConfigDataWorks() throws InterruptedException, ExecutionException, IOException {
242 waitForASpecificMessage(replySequence::receivedConfigData, ConfigData.class, ConfigData::new);
246 public void waitForGetFailedWorks() throws InterruptedException, ExecutionException, IOException {
247 waitForASpecificMessage(replySequence::receivedGetFailed, GetFailed.class, GetFailed::new);
251 public void waitForPutFailedWorks() throws InterruptedException, ExecutionException, IOException {
252 waitForASpecificMessage(replySequence::receivedPutFailed, PutFailed.class, PutFailed::new);
256 public void waitForIdentifierCollisionWorks() throws InterruptedException, ExecutionException, IOException {
257 waitForASpecificMessage(replySequence::receivedIdentifierCollision, IdentifierCollision.class, IdentifierCollision::new);
261 public void waitForPersistentPutDirWorks() throws InterruptedException, ExecutionException, IOException {
262 waitForASpecificMessage(replySequence::receivedPersistentPutDir, PersistentPutDir.class, PersistentPutDir::new);
266 public void waitForPersistentRequestRemovedWorks() throws InterruptedException, ExecutionException, IOException {
267 waitForASpecificMessage(replySequence::receivedPersistentRequestRemoved, PersistentRequestRemoved.class, PersistentRequestRemoved::new);
271 public void waitForSubscribedUSKUpdateWorks() throws InterruptedException, ExecutionException, IOException {
272 waitForASpecificMessage(replySequence::receivedSubscribedUSKUpdate, SubscribedUSKUpdate.class, SubscribedUSKUpdate::new);
276 public void waitForPluginInfoWorks() throws InterruptedException, ExecutionException, IOException {
277 waitForASpecificMessage(replySequence::receivedPluginInfo, PluginInfo.class, PluginInfo::new);
281 public void waitForFCPPluginReply() throws InterruptedException, ExecutionException, IOException {
282 waitForASpecificMessage(replySequence::receivedFCPPluginReply, new FCPPluginReply(new FcpMessage("FCPPluginReply"), null));
286 public void waitForPersistentRequestModifiedWorks() throws InterruptedException, ExecutionException, IOException {
287 waitForASpecificMessage(replySequence::receivedPersistentRequestModified, PersistentRequestModified.class, PersistentRequestModified::new);
291 public void waitForPutSuccessfulWorks() throws InterruptedException, ExecutionException, IOException {
292 waitForASpecificMessage(replySequence::receivedPutSuccessful, PutSuccessful.class, PutSuccessful::new);
296 public void waitForPutFetchableWorks() throws InterruptedException, ExecutionException, IOException {
297 waitForASpecificMessage(replySequence::receivedPutFetchable, PutFetchable.class, PutFetchable::new);
301 public void waitForSentFeedWorks() throws InterruptedException, ExecutionException, IOException {
302 waitForASpecificMessage(replySequence::receivedSentFeed, SentFeed.class, SentFeed::new);
306 public void waitForReceivedBookmarkFeedWorks() throws InterruptedException, ExecutionException, IOException {
307 waitForASpecificMessage(replySequence::receivedBookmarkFeed, ReceivedBookmarkFeed.class, ReceivedBookmarkFeed::new);
311 public void waitForProtocolErrorWorks() throws InterruptedException, ExecutionException, IOException {
312 waitForASpecificMessage(replySequence::receivedProtocolError, ProtocolError.class, ProtocolError::new);
316 public void waitForUnknownMessageWorks() throws IOException, ExecutionException, InterruptedException {
317 replySequence.setExpectedMessage("SomeFcpMessage");
318 Future<Boolean> result = replySequence.send(fcpMessage);
319 replySequence.receivedMessage(fcpConnection, new FcpMessage("SomeFcpMessage"));
320 assertThat(result.get(), is(true));
324 public void waitingForMultipleMessagesWorks() throws IOException, ExecutionException, InterruptedException {
325 TestFcpReplySequence replySequence = new TestFcpReplySequence(executorService, fcpConnection) {
326 private final AtomicBoolean gotPutFailed = new AtomicBoolean();
327 private final AtomicBoolean gotGetFailed = new AtomicBoolean();
330 protected boolean isFinished() {
331 return gotPutFailed.get() && gotGetFailed.get();
335 protected Boolean getResult() {
340 protected void consumePutFailed(PutFailed putFailed) {
341 gotPutFailed.set(true);
345 protected void consumeGetFailed(GetFailed getFailed) {
346 gotGetFailed.set(true);
349 Future<?> result = replySequence.send(fcpMessage);
350 assertThat(result.isDone(), is(false));
351 replySequence.receivedGetFailed(fcpConnection, new GetFailed(new FcpMessage("GetFailed")));
352 assertThat(result.isDone(), is(false));
353 replySequence.receivedPutFailed(fcpConnection, new PutFailed(new FcpMessage("PutFailed")));
354 assertThat(result.get(), is(true));
358 public void waitingForConnectionClosureWorks() throws IOException, ExecutionException, InterruptedException {
359 replySequence.setExpectedMessage("none");
360 Future<Boolean> result = replySequence.send(fcpMessage);
361 Throwable throwable = new Throwable();
362 replySequence.connectionClosed(fcpConnection, throwable);
365 } catch (ExecutionException e) {
367 while (t.getCause() != null) {
370 assertThat(t, sameInstance(throwable));
375 private interface MessageReceiver<M> {
377 void receiveMessage(FcpConnection fcpConnection, M message);
381 private static class TestFcpReplySequence extends FcpReplySequence<Boolean> {
383 private final AtomicReference<String> gotMessage = new AtomicReference<>();
384 private final AtomicReference<String> expectedMessage = new AtomicReference<>();
386 public TestFcpReplySequence(ExecutorService executorService, FcpConnection fcpConnection) {
387 super(executorService, fcpConnection);
390 public void setExpectedMessage(String expectedMessage) {
391 this.expectedMessage.set(expectedMessage);
395 protected boolean isFinished() {
400 protected Boolean getResult() {
401 return expectedMessage.get().equals(gotMessage.get());
405 protected void consumeNodeHello(NodeHello nodeHello) {
406 gotMessage.set(nodeHello.getName());
410 protected void consumeCloseConnectionDuplicateClientName(
411 CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
412 gotMessage.set(closeConnectionDuplicateClientName.getName());
416 protected void consumeSSKKeypair(SSKKeypair sskKeypair) {
417 gotMessage.set(sskKeypair.getName());
421 protected void consumePeer(Peer peer) {
422 gotMessage.set(peer.getName());
426 protected void consumeEndListPeers(EndListPeers endListPeers) {
427 gotMessage.set(endListPeers.getName());
431 protected void consumePeerNote(PeerNote peerNote) {
432 gotMessage.set(peerNote.getName());
436 protected void consumeEndListPeerNotes(EndListPeerNotes endListPeerNotes) {
437 gotMessage.set(endListPeerNotes.getName());
441 protected void consumePeerRemoved(PeerRemoved peerRemoved) {
442 gotMessage.set(peerRemoved.getName());
446 protected void consumeNodeData(NodeData nodeData) {
447 gotMessage.set(nodeData.getName());
451 protected void consumeTestDDAReply(TestDDAReply testDDAReply) {
452 gotMessage.set(testDDAReply.getName());
456 protected void consumeTestDDAComplete(TestDDAComplete testDDAComplete) {
457 gotMessage.set(testDDAComplete.getName());
461 protected void consumePersistentGet(PersistentGet persistentGet) {
462 gotMessage.set(persistentGet.getName());
466 protected void consumePersistentPut(PersistentPut persistentPut) {
467 gotMessage.set(persistentPut.getName());
471 protected void consumeEndListPersistentRequests(EndListPersistentRequests endListPersistentRequests) {
472 gotMessage.set(endListPersistentRequests.getName());
476 protected void consumeURIGenerated(URIGenerated uriGenerated) {
477 gotMessage.set(uriGenerated.getName());
481 protected void consumeDataFound(DataFound dataFound) {
482 gotMessage.set(dataFound.getName());
486 protected void consumeAllData(AllData allData) {
487 gotMessage.set(allData.getName());
491 protected void consumeSimpleProgress(SimpleProgress simpleProgress) {
492 gotMessage.set(simpleProgress.getName());
496 protected void consumeStartedCompression(StartedCompression startedCompression) {
497 gotMessage.set(startedCompression.getName());
501 protected void consumeFinishedCompression(FinishedCompression finishedCompression) {
502 gotMessage.set(finishedCompression.getName());
506 protected void consumeUnknownPeerNoteType(UnknownPeerNoteType unknownPeerNoteType) {
507 gotMessage.set(unknownPeerNoteType.getName());
511 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
512 gotMessage.set(unknownNodeIdentifier.getName());
516 protected void consumeConfigData(ConfigData configData) {
517 gotMessage.set(configData.getName());
521 protected void consumeGetFailed(GetFailed getFailed) {
522 gotMessage.set(getFailed.getName());
526 protected void consumePutFailed(PutFailed putFailed) {
527 gotMessage.set(putFailed.getName());
531 protected void consumeIdentifierCollision(IdentifierCollision identifierCollision) {
532 gotMessage.set(identifierCollision.getName());
536 protected void consumePersistentPutDir(PersistentPutDir persistentPutDir) {
537 gotMessage.set(persistentPutDir.getName());
541 protected void consumePersistentRequestRemoved(PersistentRequestRemoved persistentRequestRemoved) {
542 gotMessage.set(persistentRequestRemoved.getName());
546 protected void consumeSubscribedUSKUpdate(SubscribedUSKUpdate subscribedUSKUpdate) {
547 gotMessage.set(subscribedUSKUpdate.getName());
551 protected void consumePluginInfo(PluginInfo pluginInfo) {
552 gotMessage.set(pluginInfo.getName());
556 protected void consumeFCPPluginReply(FCPPluginReply fcpPluginReply) {
557 gotMessage.set(fcpPluginReply.getName());
561 protected void consumePersistentRequestModified(PersistentRequestModified persistentRequestModified) {
562 gotMessage.set(persistentRequestModified.getName());
566 protected void consumePutSuccessful(PutSuccessful putSuccessful) {
567 gotMessage.set(putSuccessful.getName());
571 protected void consumePutFetchable(PutFetchable putFetchable) {
572 gotMessage.set(putFetchable.getName());
576 protected void consumeSentFeed(SentFeed sentFeed) {
577 gotMessage.set(sentFeed.getName());
581 protected void consumeReceivedBookmarkFeed(ReceivedBookmarkFeed receivedBookmarkFeed) {
582 gotMessage.set(receivedBookmarkFeed.getName());
586 protected void consumeProtocolError(ProtocolError protocolError) {
587 gotMessage.set(protocolError.getName());
591 protected void consumeUnknownMessage(FcpMessage fcpMessage) {
592 gotMessage.set(fcpMessage.getName());