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;
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;
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;
}
}
+ @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.
}
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);
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() {
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());
+ };
+ }
+
}