🐛 Make removePeer() return normally when node identifier is unknown next
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 6 Sep 2024 21:15:37 +0000 (23:15 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 6 Sep 2024 21:15:37 +0000 (23:15 +0200)
Also, add a test!

src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java
src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java

index 1144ab3..713050a 100644 (file)
@@ -59,6 +59,7 @@ import net.pterodactylus.fcp.ProtocolError;
 import net.pterodactylus.fcp.RemovePeer;
 import net.pterodactylus.fcp.SSKKeypair;
 import net.pterodactylus.fcp.SimpleProgress;
+import net.pterodactylus.fcp.UnknownNodeIdentifier;
 import net.pterodactylus.fcp.WatchGlobal;
 
 import java.io.Closeable;
@@ -730,7 +731,7 @@ public class FcpClient implements Closeable {
                        @Override
                        @SuppressWarnings("synthetic-access")
                        public void run() throws IOException {
-                               sendMessage(new RemovePeer(peer.getIdentity()));
+                               sendMessage(new RemovePeer(createIdentifier("remove-peer"), peer.getIdentity()));
                        }
 
                        /**
@@ -738,7 +739,16 @@ public class FcpClient implements Closeable {
                         */
                        @Override
                        public void receivedPeerRemoved(FcpConnection fcpConnection, PeerRemoved peerRemoved) {
-                               complete();
+                               if (peerRemoved.getNodeIdentifier().equals(peer.getIdentity())) {
+                                       complete();
+                               }
+                       }
+
+                       @Override
+                       public void receivedUnknownNodeIdentifier(FcpConnection fcpConnection, UnknownNodeIdentifier unknownNodeIdentifier) {
+                               if (unknownNodeIdentifier.getNodeIdentifier().equals(peer.getIdentity())) {
+                                       complete();
+                               }
                        }
                }.execute();
        }
index 92a0e4b..430dedf 100644 (file)
@@ -11,7 +11,10 @@ import net.pterodactylus.fcp.GetFailed;
 import net.pterodactylus.fcp.NodeHello;
 import net.pterodactylus.fcp.NodeRef;
 import net.pterodactylus.fcp.Peer;
+import net.pterodactylus.fcp.PeerRemoved;
+import net.pterodactylus.fcp.ProtocolError;
 import net.pterodactylus.fcp.SSKKeypair;
+import net.pterodactylus.fcp.UnknownNodeIdentifier;
 import org.hamcrest.Matcher;
 import org.junit.Rule;
 import org.junit.Test;
@@ -45,6 +48,7 @@ import static org.hamcrest.Matchers.anything;
 import static org.hamcrest.Matchers.contains;
 import static org.hamcrest.Matchers.containsInAnyOrder;
 import static org.hamcrest.Matchers.equalTo;
+import static org.junit.Assert.assertThrows;
 
 public class FcpClientTest {
 
@@ -469,6 +473,66 @@ public class FcpClientTest {
        }
 
        @Test
+       public void removePeerSendsRemovePeerMessageWithIdentityAsNodeIdentifier() throws Exception {
+               List<FcpMessage> sentMessages = new ArrayList<>();
+               FcpConnection fcpConnection = createFcpConnection(message -> {
+                       if (message.getName().equals("RemovePeer")) {
+                               sentMessages.add(message);
+                               FcpMessage peerRemovedMessage = new FcpMessage("PeerRemoved");
+                               peerRemovedMessage.put("NodeIdentifier", message.getField("NodeIdentifier"));
+                               return (listener, connection) -> listener.receivedPeerRemoved(connection, new PeerRemoved(peerRemovedMessage));
+                       }
+                       return FcpClientTest::doNothing;
+               });
+               try (FcpClient fcpClient = new FcpClient(fcpConnection)) {
+                       fcpClient.removePeer(createPeer());
+                       assertThat(sentMessages, contains(allOf(hasField("NodeIdentifier", equalTo("identity")))));
+               }
+       }
+
+       @Test
+       public void removePeerWithInvalidNodeIdentifierReturns() throws Exception {
+               FcpConnection fcpConnection = createFcpConnection(message -> {
+                       if (message.getName().equals("RemovePeer")) {
+                               return (listener, connection) -> listener.receivedUnknownNodeIdentifier(connection, new UnknownNodeIdentifier(message));
+                       }
+                       return FcpClientTest::doNothing;
+               });
+               try (FcpClient fcpClient = new FcpClient(fcpConnection)) {
+                       fcpClient.removePeer(createPeer());
+               }
+       }
+
+       @Test
+       public void removePeerWithInvalidNodeIdentifierIgnoresPositiveResultForDifferentNodeIdentifier() throws Exception {
+               removePeerAndVerifyThatNodeIdentifierIsNotBeingIgnored((listener, connection) -> {
+                       listener.receivedPeerRemoved(connection, new PeerRemoved(new FcpMessage("PeerRemoved").put("NodeIdentifier", "different-node")));
+                       listener.receivedProtocolError(connection, new ProtocolError(new FcpMessage("ProtocolError").put("Code", "123")));
+               });
+       }
+
+       @Test
+       public void removePeerWithInvalidNodeIdentifierIgnoresNegativeResultForDifferentNodeIdentifier() throws Exception {
+               removePeerAndVerifyThatNodeIdentifierIsNotBeingIgnored((listener, connection) -> {
+                       listener.receivedUnknownNodeIdentifier(connection, new UnknownNodeIdentifier(new FcpMessage("UnknownNodeIdentifier").put("NodeIdentifier", "different-node")));
+                       listener.receivedProtocolError(connection, new ProtocolError(new FcpMessage("ProtocolError").put("Code", "123")));
+               });
+       }
+
+       private static void removePeerAndVerifyThatNodeIdentifierIsNotBeingIgnored(BiConsumer<FcpListener, FcpConnection> responseGenerator) {
+               FcpConnection fcpConnection = createFcpConnection(message -> {
+                       if (message.getName().equals("RemovePeer")) {
+                               return responseGenerator;
+                       }
+                       return FcpClientTest::doNothing;
+               });
+               try (FcpClient fcpClient = new FcpClient(fcpConnection)) {
+                       FcpProtocolException fcpProtocolException = assertThrows(FcpProtocolException.class, () -> fcpClient.removePeer(createPeer()));
+                       assertThat(fcpProtocolException.getCode(), equalTo(123));
+               }
+       }
+
+       @Test
        public void generatingKeyPairSendsCorrectMessage() throws IOException, FcpException {
                FcpConnection fcpConnection = createFcpConnection(message -> {
                        if (message.getName().equals("GenerateSSK")) {