From: David ‘Bombe’ Roden Date: Fri, 22 Nov 2024 21:33:55 +0000 (+0100) Subject: ✅ Add tests for modifyPeerNote() X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=f6cbed59fe81b0d1839a03d4472e103f74c3d7b5;p=jFCPlib.git ✅ Add tests for modifyPeerNote() --- 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()); } }; }