From 7d20d1b8838996a1eff8392956fa5a5d24c0ef72 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 22 Nov 2024 21:26:44 +0100 Subject: [PATCH] =?utf8?q?=E2=9C=85=20Add=20test=20for=20getPeerNote()?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../pterodactylus/fcp/highlevel/FcpClientTest.java | 43 ++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java b/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java index 430dedf..12976e7 100644 --- a/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java @@ -3,6 +3,7 @@ package net.pterodactylus.fcp.highlevel; import net.pterodactylus.fcp.AddPeer.Trust; import net.pterodactylus.fcp.AddPeer.Visibility; import net.pterodactylus.fcp.AllData; +import net.pterodactylus.fcp.EndListPeerNotes; import net.pterodactylus.fcp.EndListPeers; import net.pterodactylus.fcp.FcpConnection; import net.pterodactylus.fcp.FcpListener; @@ -11,6 +12,7 @@ import net.pterodactylus.fcp.GetFailed; import net.pterodactylus.fcp.NodeHello; import net.pterodactylus.fcp.NodeRef; import net.pterodactylus.fcp.Peer; +import net.pterodactylus.fcp.PeerNote; import net.pterodactylus.fcp.PeerRemoved; import net.pterodactylus.fcp.ProtocolError; import net.pterodactylus.fcp.SSKKeypair; @@ -24,6 +26,7 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.net.URL; import java.util.ArrayList; +import java.util.Base64; import java.util.Collection; import java.util.List; import java.util.concurrent.atomic.AtomicReference; @@ -31,6 +34,7 @@ import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.Function; +import static java.nio.charset.StandardCharsets.UTF_8; import static net.pterodactylus.fcp.AddPeer.Trust.HIGH; import static net.pterodactylus.fcp.AddPeer.Trust.LOW; import static net.pterodactylus.fcp.AddPeer.Trust.NORMAL; @@ -48,6 +52,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.hamcrest.Matchers.nullValue; import static org.junit.Assert.assertThrows; public class FcpClientTest { @@ -533,6 +538,44 @@ public class FcpClientTest { } @Test + public void getPeerNoteReturnsPeerNote() throws Exception { + FcpConnection fcpConnection = createFcpConnection(message -> { + if (message.getName().equals("ListPeerNotes")) { + return (listener, connection) -> { + FcpMessage receivedMessage = new FcpMessage("PeerNote"); + receivedMessage.setField("NodeIdentifier", message.getField("NodeIdentifier")); + receivedMessage.setField("PeerNoteType", "1"); + receivedMessage.setField("NoteText", Base64.getEncoder().encodeToString("Peer Note".getBytes(UTF_8))); + listener.receivedPeerNote(connection, new PeerNote(new FcpMessage("PeerNote").put("NodeIdentifier", "different-node"))); + listener.receivedPeerNote(connection, new PeerNote(receivedMessage)); + listener.receivedEndListPeerNotes(connection, new EndListPeerNotes(new FcpMessage("EndListPeerNotes"))); + }; + } + return FcpClientTest::doNothing; + }); + try (FcpClient fcpClient = new FcpClient(fcpConnection)) { + PeerNote peerNote = fcpClient.getPeerNote(createPeer()); + assertThat(peerNote.getNodeIdentifier(), equalTo("identity")); + assertThat(peerNote.getPeerNoteType(), equalTo(1)); + assertThat(peerNote.getNoteText(), equalTo("Peer Note")); + } + } + + @Test + public void getPeerNoteWithInvalidNodeIdentifierReturnsNull() throws Exception { + FcpConnection fcpConnection = createFcpConnection(message -> { + if (message.getName().equals("ListPeerNotes")) { + return (listener, connection) -> listener.receivedEndListPeerNotes(connection, new EndListPeerNotes(new FcpMessage("EndListPeerNotes"))); + } + return FcpClientTest::doNothing; + }); + try (FcpClient fcpClient = new FcpClient(fcpConnection)) { + PeerNote peerNote = fcpClient.getPeerNote(createPeer()); + assertThat(peerNote, nullValue()); + } + } + + @Test public void generatingKeyPairSendsCorrectMessage() throws IOException, FcpException { FcpConnection fcpConnection = createFcpConnection(message -> { if (message.getName().equals("GenerateSSK")) { -- 2.7.4