šŸ› Fix parsing of WoT own identities (and add test)
authorDavid ā€˜Bombe’ Roden <bombe@freenetproject.org>
Wed, 8 Jan 2025 07:34:27 +0000 (08:34 +0100)
committerDavid ā€˜Bombe’ Roden <bombe@freenetproject.org>
Wed, 8 Jan 2025 07:39:13 +0000 (08:39 +0100)
src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java
src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java

index 0882471..cc47ccd 100644 (file)
@@ -128,12 +128,22 @@ public class WebOfTrustPlugin {
                        throw new FcpException("WebOfTrust Plugin did not reply with ā€œOwnIdentitiesā€ message!");
                }
                Set<OwnIdentity> ownIdentities = new HashSet<OwnIdentity>();
-               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<String> contexts = new HashSet<>();
+                       for (int contextIndex = 0; replies.containsKey("Contexts" + identityIndex + ".Context" + contextIndex); contextIndex++) {
+                               contexts.add(replies.get("Contexts" + identityIndex + ".Context" + contextIndex));
+                       }
+                       Map<String, String> 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;
        }
index 2adc5c2..dab71e0 100644 (file)
@@ -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<OwnIdentity> 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<String, String> 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));
        }