From d3a6a614790af590ec6ef33be3167ad287887e24 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 8 Jan 2025 08:34:27 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=90=9B=20Fix=20parsing=20of=20WoT=20own=20?= =?utf8?q?identities=20(and=20add=20test)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../pterodactylus/fcp/plugin/WebOfTrustPlugin.java | 14 ++++++- .../fcp/plugin/WebOfTrustPluginTest.java | 48 ++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java b/src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java index 0882471..cc47ccd 100644 --- a/src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java +++ b/src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java @@ -128,12 +128,22 @@ public class WebOfTrustPlugin { throw new FcpException("WebOfTrust Plugin did not reply with “OwnIdentities” message!"); } Set ownIdentities = new HashSet(); - for (int identityIndex = 1; replies.containsKey("Identity" + identityIndex); identityIndex++) { + for (int identityIndex = 0; replies.containsKey("Identity" + identityIndex); identityIndex++) { String identity = replies.get("Identity" + identityIndex); String nickname = replies.get("Nickname" + identityIndex); String requestUri = replies.get("RequestURI" + identityIndex); String insertUri = replies.get("InsertURI" + identityIndex); - ownIdentities.add(new OwnIdentity(identity, nickname, requestUri, insertUri, emptySet(), emptyMap())); + Set contexts = new HashSet<>(); + for (int contextIndex = 0; replies.containsKey("Contexts" + identityIndex + ".Context" + contextIndex); contextIndex++) { + contexts.add(replies.get("Contexts" + identityIndex + ".Context" + contextIndex)); + } + Map properties = new HashMap<>(); + for (int propertyIndex = 0; replies.containsKey("Properties" + identityIndex + ".Property" + propertyIndex + ".Name"); propertyIndex++) { + String key = replies.get("Properties" + identityIndex + ".Property" + propertyIndex + ".Name"); + String value = replies.get("Properties" + identityIndex + ".Property" + propertyIndex + ".Value"); + properties.put(key, value); + } + ownIdentities.add(new OwnIdentity(identity, nickname, requestUri, insertUri, contexts, properties)); } return ownIdentities; } diff --git a/src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java b/src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java index 2adc5c2..dab71e0 100644 --- a/src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java +++ b/src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java @@ -14,15 +14,21 @@ import org.junit.rules.Timeout; import java.io.IOException; import java.util.HashMap; import java.util.Map; +import java.util.Set; import java.util.function.BiConsumer; import java.util.function.Function; +import static net.pterodactylus.fcp.test.IdentityMatchers.hasContexts; +import static net.pterodactylus.fcp.test.IdentityMatchers.hasProperties; import static net.pterodactylus.fcp.test.IdentityMatchers.isOwnIdentity; import static net.pterodactylus.fcp.test.Matchers.hasField; import static net.pterodactylus.fcp.test.Matchers.isNamed; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.aMapWithSize; import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.nullValue; import static org.junit.Assert.assertThrows; @@ -124,6 +130,48 @@ public class WebOfTrustPluginTest { assertThrows(FcpException.class, () -> webOfTrustPlugin.createIdentity("Test Nickname", "test-context", true, "some-request-uri", "insert-uri")); } + @Test + public void gettingOwnIdentitiesSendsCorrectMessage() throws Exception { + TestFcpConnection fcpConnection = createConnectionThatDeliversOwnIdentities(); + WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection); + webOfTrustPlugin.getOwnIdentites(); + assertThat(fcpConnection.sentMessages.get(0), allOf( + isNamed(equalTo("FCPPluginMessage")), + hasField("Identifier", not(nullValue())), + hasField("PluginName", equalTo("plugins.WebOfTrust.WebOfTrust")), + hasField("Param.Message", equalTo("GetOwnIdentities")) + )); + } + + @Test + public void gettingOwnIdentitiesParsesOwnIdentitiesCorrectly() throws Exception { + TestFcpConnection fcpConnection = createConnectionThatDeliversOwnIdentities(); + WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection); + Set ownIdentities = webOfTrustPlugin.getOwnIdentites(); + assertThat(ownIdentities, containsInAnyOrder( + allOf(isOwnIdentity(equalTo("identity-0"), equalTo("Nick 0"), equalTo("request-uri-0"), equalTo("insert-uri-0")), + hasContexts(containsInAnyOrder("test-context-1", "test-context-2")), + hasProperties(allOf(aMapWithSize(2), hasEntry("prop1", "value1"), hasEntry("prop2", "value2")))), + allOf(isOwnIdentity(equalTo("identity-1"), equalTo("Nick 1"), equalTo("request-uri-1"), equalTo("insert-uri-1")), + hasContexts(containsInAnyOrder("test-context-2", "test-context-3")), + hasProperties(allOf(aMapWithSize(2), hasEntry("prop3", "value3"), hasEntry("prop4", "value4")))) + )); + } + + private TestFcpConnection createConnectionThatDeliversOwnIdentities() { + return createFcpConnection(message -> (listener, connection) -> { + Map fields = createWebOfTrustReplyFields("OwnIdentities", true, + "Amount", "2", "Identity0", "identity-0", "Nickname0", "Nick 0", "RequestURI0", "request-uri-0", "InsertURI0", "insert-uri-0", + "Contexts0.Context0", "test-context-1", "Contexts0.Context1", "test-context-2", "Properties0.Property0.Name", "prop1", "Properties0.Property0.Value", "value1", "Properties0.Property1.Name", "prop2", "Properties0.Property1.Value", "value2", + "Identity1", "identity-1", "Nickname1", "Nick 1", "RequestURI1", "request-uri-1", "InsertURI1", "insert-uri-1", + "Contexts1.Context0", "test-context-2", "Contexts1.Context1", "test-context-3", "Properties1.Property0.Name", "prop3", "Properties1.Property0.Value", "value3", "Properties1.Property1.Name", "prop4", "Properties1.Property1.Value", "value4" + ); + FcpMessage replyMessage = createPluginReply(message, fields); + listener.receivedFCPPluginReply(connection, new FCPPluginReply(replyMessage, null)); + }); + } + + private static WebOfTrustPlugin createWebOfTrustPlugin(FcpConnection fcpConnection) { return new WebOfTrustPlugin(new FcpClient(fcpConnection)); } -- 2.7.4