From 0fe3669f39ad1a0cd1a84effc05a3d4718aba26b Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 6 Sep 2024 12:34:25 +0200 Subject: [PATCH] =?utf8?q?=E2=9C=85=20Add=20test=20for=20Peer?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit And in the process also fix a bug! --- src/main/java/net/pterodactylus/fcp/Peer.java | 5 +- src/test/java/net/pterodactylus/fcp/PeerTest.java | 199 ++++++++++++++++++++++ 2 files changed, 202 insertions(+), 2 deletions(-) create mode 100644 src/test/java/net/pterodactylus/fcp/PeerTest.java diff --git a/src/main/java/net/pterodactylus/fcp/Peer.java b/src/main/java/net/pterodactylus/fcp/Peer.java index 4decfdf..0ad5a43 100644 --- a/src/main/java/net/pterodactylus/fcp/Peer.java +++ b/src/main/java/net/pterodactylus/fcp/Peer.java @@ -57,6 +57,7 @@ public class Peer extends BaseMessage implements Identifiable { nodeRef.setOpennet(isOpennet()); nodeRef.setPhysicalUDP(getPhysicalUDP()); nodeRef.setVersion(getVersion()); + nodeRef.setSignature(getField("sig")); return nodeRef; } @@ -134,7 +135,7 @@ public class Peer extends BaseMessage implements Identifiable { * @return The ARK of the peer */ public ARK getARK() { - return new ARK(getField("ark.pubURI"), getField("ark.number")); + return new ARK(getField("ark.pubURI"), getField("ark.privURI"), getField("ark.number")); } /** @@ -175,7 +176,7 @@ public class Peer extends BaseMessage implements Identifiable { * false otherwise */ public boolean isTestnet() { - return Boolean.valueOf("testnet"); + return Boolean.valueOf(getField("testnet")); } /** diff --git a/src/test/java/net/pterodactylus/fcp/PeerTest.java b/src/test/java/net/pterodactylus/fcp/PeerTest.java new file mode 100644 index 0000000..ceb0c30 --- /dev/null +++ b/src/test/java/net/pterodactylus/fcp/PeerTest.java @@ -0,0 +1,199 @@ +package net.pterodactylus.fcp; + +import org.junit.Test; + +import java.util.function.Consumer; + +import static net.pterodactylus.fcp.test.ArkMatchers.isArk; +import static net.pterodactylus.fcp.test.DsaGroupMatchers.isDsaGroup; +import static net.pterodactylus.fcp.test.VersionMatchers.isVersion; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.aMapWithSize; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasEntry; + +public class PeerTest { + + @Test + public void peerReturnsCorrectNodeRef() { + NodeRef nodeRef = peer.getNodeRef(); + assertThat(nodeRef.getARK(), isArk(equalTo("ark-public-uri"), equalTo("ark-private-uri"), equalTo(123))); + assertThat(nodeRef.getDSAGroup(), isDsaGroup(equalTo("dsa-group-base"), equalTo("dsa-group-prime"), equalTo("dsa-group-subprime"))); + assertThat(nodeRef.getDSAPublicKey(), equalTo("dsa-public-key")); + assertThat(nodeRef.getIdentity(), equalTo("identity")); + assertThat(nodeRef.getLastGoodVersion(), isVersion(equalTo("Node"), equalTo("0.1.2"), equalTo("1.2.3"), equalTo(234))); + assertThat(nodeRef.getLocation(), equalTo(0.4)); + assertThat(nodeRef.getMyName(), equalTo("Test Node")); + assertThat(nodeRef.getNegotiationTypes(), equalTo(new int[] { 1, 2, 3 })); + assertThat(nodeRef.getPhysicalUDP(), equalTo("physical-udp")); + assertThat(nodeRef.getSignature(), equalTo("signature")); + assertThat(nodeRef.getVersion(), isVersion(equalTo("TestNode"), equalTo("1.2.3"), equalTo("2.3.4"), equalTo(345))); + } + + @Test + public void peerReturnsCorrectIdentifier() { + assertThat(peer.getIdentifier(), equalTo("identifier")); + } + + @Test + public void peerReturnsCorrectPhysicalUdp() { + assertThat(peer.getPhysicalUDP(), equalTo("physical-udp")); + } + + @Test + public void peerReturnsOpennetIfFieldIsSetToTrue() { + assertThat(createPeer(m -> m.setField("opennet", "true")).isOpennet(), equalTo(true)); + } + + @Test + public void peerReturnsNotOpennetIfFieldIsSetToFalse() { + assertThat(createPeer(m -> m.setField("opennet", "false")).isOpennet(), equalTo(false)); + } + + @Test + public void peerReturnsNotOpennetIfFieldIsNotSet() { + assertThat(createPeer(m -> m.setField("opennet", "")).isOpennet(), equalTo(false)); + } + + @Test + public void peerReturnsSeedIfFieldIsSetToTrue() { + assertThat(createPeer(m -> m.setField("seed", "true")).isSeed(), equalTo(true)); + } + + @Test + public void peerReturnsNotSeedIfFieldIsSetToFalse() { + assertThat(createPeer(m -> m.setField("seed", "false")).isSeed(), equalTo(false)); + } + + @Test + public void peerReturnsNotSeedIfFieldIsNotSet() { + assertThat(createPeer(m -> m.setField("seed", "")).isSeed(), equalTo(false)); + } + + @Test + public void peerReturnsDsaPublicKeyCorrectly() { + assertThat(peer.getDSAPublicKey(), equalTo("dsa-public-key")); + } + + @Test + public void peerReturnsDsaGroupCorrectly() { + assertThat(peer.getDSAGroup(), isDsaGroup(equalTo("dsa-group-base"), equalTo("dsa-group-prime"), equalTo("dsa-group-subprime"))); + } + + @Test + public void peerReturnsCorrectLastGoodVersion() { + assertThat(peer.getLastGoodVersion(), isVersion(equalTo("Node"), equalTo("0.1.2"), equalTo("1.2.3"), equalTo(234))); + } + + @Test + public void peerReturnsCorrectArk() { + assertThat(peer.getARK(), isArk(equalTo("ark-public-uri"), equalTo("ark-private-uri"), equalTo(123))); + } + + @Test + public void peerReturnsCorrectIdentity() { + assertThat(peer.getIdentity(), equalTo("identity")); + } + + @Test + public void peerReturnsCorrectName() { + assertThat(peer.getMyName(), equalTo("Test Node")); + } + + @Test + public void peerReturnsCorrectLocation() { + assertThat(peer.getLocation(), equalTo(0.4)); + } + + @Test + public void peerReturnsTestnetIfFieldIsSetToTrue() { + assertThat(createPeer(m -> m.setField("testnet", "true")).isTestnet(), equalTo(true)); + } + + @Test + public void peerReturnsNotTestnetIfFieldIsSetToFalse() { + assertThat(createPeer(m -> m.setField("testnet", "false")).isTestnet(), equalTo(false)); + } + + @Test + public void peerReturnsNotTestnetIfFieldIsNotSet() { + assertThat(createPeer(m -> m.setField("testnet", "")).isTestnet(), equalTo(false)); + } + + @Test + public void peerReturnsCorrectVersion() { + assertThat(peer.getVersion(), isVersion(equalTo("TestNode"), equalTo("1.2.3"), equalTo("2.3.4"), equalTo(345))); + } + + @Test + public void peerReturnsCorrectNegotiationTypes() { + assertThat(peer.getNegotiationTypes(), equalTo(new int[] { 1, 2, 3 })); + } + + @Test + public void peerReturnsCorrectVolatileFields() { + assertThat(peer.getVolatileFields(), allOf( + aMapWithSize(3), + hasEntry("volatile.a", "A1"), + hasEntry("volatile.b", "B2"), + hasEntry("volatile.c", "C3") + )); + } + + @Test + public void peerReturnsCorrectVolatileField() { + assertThat(peer.getVolatile("a"), equalTo("A1")); + } + + @Test + public void peerReturnsCorrectMetadataFields() { + assertThat(peer.getMetadataFields(), allOf( + aMapWithSize(3), + hasEntry("metadata.a", "MA1"), + hasEntry("metadata.b", "MB2"), + hasEntry("metadata.c", "MC3") + )); + } + + @Test + public void peerReturnsCorrectMetadataField() { + assertThat(peer.getMetadata("a"), equalTo("MA1")); + } + + private static Peer createPeer() { + return createPeer(m -> { + }); + } + + private static Peer createPeer(Consumer messageModifier) { + FcpMessage peerMessage = new FcpMessage("Peer"); + peerMessage.setField("Identifier", "identifier"); + peerMessage.setField("ark.number", "123"); + peerMessage.setField("ark.privURI", "ark-private-uri"); + peerMessage.setField("ark.pubURI", "ark-public-uri"); + peerMessage.setField("auth.negTypes", "1;2;3"); + peerMessage.setField("dsaGroup.g", "dsa-group-base"); + peerMessage.setField("dsaGroup.p", "dsa-group-prime"); + peerMessage.setField("dsaGroup.q", "dsa-group-subprime"); + peerMessage.setField("dsaPubKey.y", "dsa-public-key"); + peerMessage.setField("identity", "identity"); + peerMessage.setField("lastGoodVersion", "Node,0.1.2,1.2.3,234"); + peerMessage.setField("location", "0.4"); + peerMessage.setField("myName", "Test Node"); + peerMessage.setField("physical.udp", "physical-udp"); + peerMessage.setField("sig", "signature"); + peerMessage.setField("version", "TestNode,1.2.3,2.3.4,345"); + peerMessage.setField("volatile.a", "A1"); + peerMessage.setField("volatile.b", "B2"); + peerMessage.setField("volatile.c", "C3"); + peerMessage.setField("metadata.a", "MA1"); + peerMessage.setField("metadata.b", "MB2"); + peerMessage.setField("metadata.c", "MC3"); + messageModifier.accept(peerMessage); + return new Peer(peerMessage); + } + + private final Peer peer = createPeer(); + +} -- 2.7.4