From f6cbed59fe81b0d1839a03d4472e103f74c3d7b5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 22 Nov 2024 22:33:55 +0100 Subject: [PATCH] =?utf8?q?=E2=9C=85=20Add=20tests=20for=20modifyPeerNote()?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/fcp/highlevel/FcpClient.java | 28 +++++++++++-- .../pterodactylus/fcp/highlevel/FcpClientTest.java | 46 +++++++++++++++++++++- 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java b/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java index 713050a..491a219 100644 --- a/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java @@ -52,6 +52,7 @@ import net.pterodactylus.fcp.NodeHello; import net.pterodactylus.fcp.NodeRef; import net.pterodactylus.fcp.Peer; import net.pterodactylus.fcp.PeerNote; +import net.pterodactylus.fcp.PeerNoteType; import net.pterodactylus.fcp.PeerRemoved; import net.pterodactylus.fcp.PersistentGet; import net.pterodactylus.fcp.PersistentPut; @@ -803,6 +804,22 @@ public class FcpClient implements Closeable { } /** + * Replaces the private darknet comment peer note for the given peer. + * + * @param peer + * The peer + * @param noteText + * The new base64-encoded note text + * @throws IOException + * if an I/O error occurs + * @throws FcpException + * if an FCP error occurs + */ + public void modifyPeerNote(final Peer peer, final String noteText) throws IOException, FcpException { + modifyPeerNote(peer, noteText, 1); + } + + /** * Replaces the peer note for the given peer. * * @param peer @@ -816,7 +833,9 @@ public class FcpClient implements Closeable { * if an I/O error occurs * @throws FcpException * if an FCP error occurs + * @deprecated Use {@link #modifyPeerNote(Peer, String)} instead */ + @Deprecated public void modifyPeerNote(final Peer peer, final String noteText, final int noteType) throws IOException, FcpException { new ExtendedFcpAdapter() { @@ -826,15 +845,18 @@ public class FcpClient implements Closeable { @Override @SuppressWarnings("synthetic-access") public void run() throws IOException { - sendMessage(new ModifyPeerNote(peer.getIdentity(), noteText, noteType)); + ModifyPeerNote modifyPeerNote = new ModifyPeerNote(createIdentifier("modify-peer-note"), peer.getIdentity()); + modifyPeerNote.setNoteText(noteText); + modifyPeerNote.setPeerNoteType(PeerNoteType.PRIVATE_DARKNET_COMMENT); + sendMessage(modifyPeerNote); } /** * {@inheritDoc} */ @Override - public void receivedPeer(FcpConnection fcpConnection, Peer receivedPeer) { - if (receivedPeer.getIdentity().equals(peer.getIdentity())) { + public void receivedPeerNote(FcpConnection fcpConnection, PeerNote receivedPeerNote) { + if (receivedPeerNote.getNodeIdentifier().equals(peer.getIdentity())) { complete(); } } diff --git a/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java b/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java index 12976e7..040575a 100644 --- a/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java @@ -13,6 +13,7 @@ import net.pterodactylus.fcp.NodeHello; import net.pterodactylus.fcp.NodeRef; import net.pterodactylus.fcp.Peer; import net.pterodactylus.fcp.PeerNote; +import net.pterodactylus.fcp.PeerNoteType; import net.pterodactylus.fcp.PeerRemoved; import net.pterodactylus.fcp.ProtocolError; import net.pterodactylus.fcp.SSKKeypair; @@ -576,6 +577,49 @@ public class FcpClientTest { } @Test + public void modifyPeerNoteSendsCorrectMessage() throws Exception { + FcpConnection fcpConnection = createFcpConnection(message -> { + if (message.getName().equals("ModifyPeerNote") && message.getField("NoteText").equals("bmV3IG5vdGU=")) { + return (listener, connection) -> { + listener.receivedPeerNote(connection, new PeerNote(new FcpMessage("PeerNote").put("NodeIdentifier", message.getField("NodeIdentifier")))); + }; + } + return FcpClientTest::doNothing; + }); + try (FcpClient fcpClient = new FcpClient(fcpConnection)) { + fcpClient.modifyPeerNote(createPeer(), "new note"); + } + } + + @Test + public void modifyPeerNoteWaitsForCorrectPeerNoteReturnMessage() throws Exception { + FcpConnection fcpConnection = createFcpConnection(message -> { + if (message.getName().equals("ModifyPeerNote")) { + return (listener, connection) -> { + listener.receivedPeerNote(connection, new PeerNote(new FcpMessage("PeerNote").put("NodeIdentifier", "different-node"))); + try { + Thread.sleep(6000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + listener.receivedPeerNote(connection, new PeerNote(new FcpMessage("PeerNote").put("NodeIdentifier", message.getField("NodeIdentifier")))); + }; + } + return FcpClientTest::doNothing; + }); + Thread thread = new Thread(() -> { + try (FcpClient fcpClient = new FcpClient(fcpConnection)) { + fcpClient.modifyPeerNote(createPeer(), "new note"); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + thread.start(); + Thread.sleep(1000); + assertThat(thread.isAlive(), equalTo(true)); + } + + @Test public void generatingKeyPairSendsCorrectMessage() throws IOException, FcpException { FcpConnection fcpConnection = createFcpConnection(message -> { if (message.getName().equals("GenerateSSK")) { @@ -635,7 +679,7 @@ public class FcpClientTest { @Override public void sendMessage(FcpMessage fcpMessage) { BiConsumer listenerNotifier = messageConsumer.apply(fcpMessage); - new Thread(() -> listeners.forEach(listener -> listenerNotifier.accept(listener, this))).start(); + listeners.forEach(listener -> new Thread(() -> listenerNotifier.accept(listener, this)).start()); } }; } -- 2.7.4