✅ Add tests for getNodeInformation()
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Sat, 23 Nov 2024 15:50:07 +0000 (16:50 +0100)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Sat, 23 Nov 2024 15:50:07 +0000 (16:50 +0100)
src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java
src/test/java/net/pterodactylus/fcp/test/NodeRefs.java

index 3227efb..507c429 100644 (file)
@@ -13,6 +13,7 @@ import net.pterodactylus.fcp.FcpListener;
 import net.pterodactylus.fcp.FcpMessage;
 import net.pterodactylus.fcp.FcpUtils;
 import net.pterodactylus.fcp.GetFailed;
+import net.pterodactylus.fcp.NodeData;
 import net.pterodactylus.fcp.NodeHello;
 import net.pterodactylus.fcp.NodeRef;
 import net.pterodactylus.fcp.Peer;
@@ -42,6 +43,7 @@ import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.BiConsumer;
 import java.util.function.BiFunction;
@@ -59,6 +61,7 @@ import static net.pterodactylus.fcp.AddPeer.Visibility.YES;
 import static net.pterodactylus.fcp.test.InputStreamMatchers.streamContaining;
 import static net.pterodactylus.fcp.test.Matchers.hasField;
 import static net.pterodactylus.fcp.test.Matchers.matches;
+import static net.pterodactylus.fcp.test.NodeRefs.copyNodeRefToMessage;
 import static net.pterodactylus.fcp.test.NodeRefs.createNodeRef;
 import static net.pterodactylus.fcp.test.PeerMatchers.peerWithIdentity;
 import static net.pterodactylus.fcp.test.Peers.createPeer;
@@ -904,6 +907,45 @@ public class FcpClientTest {
                }
        }
 
+       @Test
+       public void getNodeInformationSetsGiveOpennetRefsOnMessage() throws Exception {
+               FcpConnection fcpConnection = createFcpConnectionReactingToSingleMessage(named("GetNode").and(withField("GiveOpennetRef", "true")), this::sendNodeData);
+               try (FcpClient fcpClient = new FcpClient(fcpConnection)) {
+                       fcpClient.getNodeInformation(true, false, false);
+               }
+       }
+
+       @Test
+       public void getNodeInformationSetsWithPrivateOnMessage() throws Exception {
+               FcpConnection fcpConnection = createFcpConnectionReactingToSingleMessage(named("GetNode").and(withField("WithPrivate", "true")), this::sendNodeData);
+               try (FcpClient fcpClient = new FcpClient(fcpConnection)) {
+                       fcpClient.getNodeInformation(false, true, false);
+               }
+       }
+
+       @Test
+       public void getNodeInformationSetsWithVolatileOnMessage() throws Exception {
+               FcpConnection fcpConnection = createFcpConnectionReactingToSingleMessage(named("GetNode").and(withField("WithVolatile", "true")), this::sendNodeData);
+               try (FcpClient fcpClient = new FcpClient(fcpConnection)) {
+                       fcpClient.getNodeInformation(false, false, true);
+               }
+       }
+
+       @Test
+       public void getNodeInformationReturnsNodeData() throws Exception {
+               FcpConnection fcpConnection = createFcpConnectionReactingToSingleMessage(named("GetNode"), this::sendNodeData);
+               try (FcpClient fcpClient = new FcpClient(fcpConnection)) {
+                       NodeData nodeData = fcpClient.getNodeInformation(false, false, false);
+                       assertThat(nodeData.getIdentity(), equalTo("identity"));
+               }
+       }
+
+       private void sendNodeData(FcpListener listener, FcpConnection connection) {
+               FcpMessage nodeData = new FcpMessage("NodeData");
+               copyNodeRefToMessage(createNodeRef()).accept(nodeData);
+               listener.receivedNodeData(connection, new NodeData(nodeData));
+       }
+
        private static void doNothing(FcpListener listener, FcpConnection connection) {
                // do nothing.
        }
@@ -967,6 +1009,10 @@ public class FcpClientTest {
                return message -> message.getName().equals(name);
        }
 
+       private static Predicate<FcpMessage> withField(String name, String value) {
+               return message -> Objects.equals(message.getField(name), value);
+       }
+
        @Rule
        public final Timeout timeout = Timeout.seconds(5);
 
index a70ddae..3366b1b 100644 (file)
@@ -2,9 +2,15 @@ package net.pterodactylus.fcp.test;
 
 import net.pterodactylus.fcp.ARK;
 import net.pterodactylus.fcp.DSAGroup;
+import net.pterodactylus.fcp.FcpMessage;
 import net.pterodactylus.fcp.NodeRef;
 import net.pterodactylus.fcp.Version;
 
+import java.util.function.Consumer;
+
+import static java.util.Arrays.stream;
+import static java.util.stream.Collectors.joining;
+
 public class NodeRefs {
 
        public static NodeRef createNodeRef() {
@@ -19,8 +25,29 @@ public class NodeRefs {
                nodeRef.setNegotiationTypes(new int[] { 1, 2, 3 });
                nodeRef.setIdentity("identity");
                nodeRef.setName("name");
+               nodeRef.setLastGoodVersion(new Version("ProdNode", "1.0", "2.0", 300));
                nodeRef.setSignature("signature");
                return nodeRef;
        }
 
+       public static Consumer<FcpMessage> copyNodeRefToMessage(NodeRef nodeRef) {
+               return message -> {
+                       message.put("ark.pubURI", nodeRef.getARK().getPublicURI());
+                       message.put("ark.number", String.valueOf(nodeRef.getARK().getNumber()));
+                       message.put("location", String.valueOf(nodeRef.getLocation()));
+                       message.put("dsaGroup.g", nodeRef.getDSAGroup().getBase());
+                       message.put("dsaGroup.p", nodeRef.getDSAGroup().getPrime());
+                       message.put("dsaGroup.q", nodeRef.getDSAGroup().getSubprime());
+                       message.put("opennet", String.valueOf(nodeRef.isOpennet()));
+                       message.put("dsaPubKey.y", nodeRef.getDSAPublicKey());
+                       message.put("version", nodeRef.getVersion().toString());
+                       message.put("physical.udp", nodeRef.getPhysicalUDP());
+                       message.put("auth.negTypes", stream(nodeRef.getNegotiationTypes()).mapToObj(String::valueOf).collect(joining(";")));
+                       message.put("identity", nodeRef.getIdentity());
+                       message.put("name", nodeRef.getMyName());
+                       message.put("lastGoodVersion", nodeRef.getLastGoodVersion().toString());
+                       message.put("signature", nodeRef.getSignature());
+               };
+       }
+
 }