From 19971c6f7a8c8b3a047199640e4dbab004f3ee99 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 24 Nov 2015 22:00:52 +0100 Subject: [PATCH 1/1] Automatically en- and decode peer note texts --- .../java/net/pterodactylus/fcp/ModifyPeerNote.java | 6 ++++- src/main/java/net/pterodactylus/fcp/PeerNote.java | 6 ++++- .../net/pterodactylus/fcp/ModifyPeerNoteTest.java | 28 ++++++++++++++++++++++ .../java/net/pterodactylus/fcp/PeerNoteTest.java | 23 ++++++++++++++++++ 4 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 src/test/java/net/pterodactylus/fcp/ModifyPeerNoteTest.java create mode 100644 src/test/java/net/pterodactylus/fcp/PeerNoteTest.java diff --git a/src/main/java/net/pterodactylus/fcp/ModifyPeerNote.java b/src/main/java/net/pterodactylus/fcp/ModifyPeerNote.java index 67691ab..c7338ff 100644 --- a/src/main/java/net/pterodactylus/fcp/ModifyPeerNote.java +++ b/src/main/java/net/pterodactylus/fcp/ModifyPeerNote.java @@ -18,6 +18,8 @@ package net.pterodactylus.fcp; +import com.google.common.base.Charsets; + /** * The “ModifyPeerNote” command modifies a peer note. * @@ -25,6 +27,8 @@ package net.pterodactylus.fcp; */ public class ModifyPeerNote extends FcpMessage { + private static final FreenetBase64 BASE64_ENCODER = new FreenetBase64(); + public ModifyPeerNote(String identifier, String nodeIdentifier) { super("ModifyPeerNote"); setField("Identifier", identifier); @@ -40,7 +44,7 @@ public class ModifyPeerNote extends FcpMessage { } public void setNoteText(String noteText) { - setField("NoteText", noteText); + setField("NoteText", BASE64_ENCODER.encode(noteText.getBytes(Charsets.UTF_8))); } public void setPeerNoteType(PeerNoteType peerNoteType) { diff --git a/src/main/java/net/pterodactylus/fcp/PeerNote.java b/src/main/java/net/pterodactylus/fcp/PeerNote.java index 1962eee..034e40f 100644 --- a/src/main/java/net/pterodactylus/fcp/PeerNote.java +++ b/src/main/java/net/pterodactylus/fcp/PeerNote.java @@ -18,6 +18,8 @@ package net.pterodactylus.fcp; +import com.google.common.base.Charsets; + /** * The “PeerNote” message contains a private note that has been entered for a * darknet peer. @@ -26,6 +28,8 @@ package net.pterodactylus.fcp; */ public class PeerNote extends BaseMessage { + private static final FreenetBase64 BASE64_DECODER = new FreenetBase64(); + /** The type for base64 encoded peer notes. */ public static final int TYPE_PRIVATE_PEER_NOTE = 1; @@ -54,7 +58,7 @@ public class PeerNote extends BaseMessage { * @return The note text */ public String getNoteText() { - return getField("NoteText"); + return new String(BASE64_DECODER.decode(getField("NoteText")), Charsets.UTF_8); } /** diff --git a/src/test/java/net/pterodactylus/fcp/ModifyPeerNoteTest.java b/src/test/java/net/pterodactylus/fcp/ModifyPeerNoteTest.java new file mode 100644 index 0000000..f82e012 --- /dev/null +++ b/src/test/java/net/pterodactylus/fcp/ModifyPeerNoteTest.java @@ -0,0 +1,28 @@ +package net.pterodactylus.fcp; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +import org.junit.Test; + +/** + * Unit test for {@link ModifyPeerNote}. + * + * @author David ‘Bombe’ Roden + */ +public class ModifyPeerNoteTest { + + private static final String IDENTIFIER = "identifier"; + private static final String NODE_IDENTIFIER = "node_identifier"; + private static final String ENCODED_NOTE_TEXT = "VWJlck5vZGUgKHVudGlsIEkgaGF2ZSByZWFsIHBlZXJzKQ=="; + private static final String DECODED_NOTE_TEXT = "UberNode (until I have real peers)"; + + @Test + public void noteTextIsEncodedCorrectly() { + ModifyPeerNote modifyPeerNote = new ModifyPeerNote(IDENTIFIER, NODE_IDENTIFIER); + modifyPeerNote.setNoteText(DECODED_NOTE_TEXT); + assertThat(modifyPeerNote.getField("NoteText"), is(ENCODED_NOTE_TEXT)); + } + + +} diff --git a/src/test/java/net/pterodactylus/fcp/PeerNoteTest.java b/src/test/java/net/pterodactylus/fcp/PeerNoteTest.java new file mode 100644 index 0000000..ce73f4c --- /dev/null +++ b/src/test/java/net/pterodactylus/fcp/PeerNoteTest.java @@ -0,0 +1,23 @@ +package net.pterodactylus.fcp; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +import org.junit.Test; + +/** + * Unit test for {@link PeerNote}. + * + * @author David ‘Bombe’ Roden + */ +public class PeerNoteTest { + + @Test + public void peerNoteIsDecodedCorrectly() { + FcpMessage receivedMessage = new FcpMessage("PeerNote"); + receivedMessage.setField("NoteText", "VWJlck5vZGUgKHVudGlsIEkgaGF2ZSByZWFsIHBlZXJzKQ=="); + PeerNote peerNote = new PeerNote(receivedMessage); + assertThat(peerNote.getNoteText(), is("UberNode (until I have real peers)")); + } + +} -- 2.7.4