✅ Add tests for missing trust and/or score values
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Wed, 8 Jan 2025 15:45:41 +0000 (16:45 +0100)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Wed, 8 Jan 2025 15:58:39 +0000 (16:58 +0100)
src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java

index cec4542..da95d87 100644 (file)
@@ -284,6 +284,52 @@ public class WebOfTrustPluginTest {
                }
 
                @Test
+               public void getIdentityTrustCanHandleMissingTrustValue() throws Exception {
+                       TestFcpConnection fcpConnection = createConnectionThatDeliversIdentityWithoutTrustButAScore();
+                       WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection);
+                       OwnIdentity ownIdentity = new OwnIdentity("own-id", "Own ID", "own-pub", "own-priv", emptySet(), emptyMap());
+                       CalculatedTrust calculatedTrust = webOfTrustPlugin.getIdentityTrust(ownIdentity, "other-id");
+                       assertThat(calculatedTrust.getTrust(), nullValue());
+                       assertThat(calculatedTrust.getScore(), equalTo(56));
+                       assertThat(calculatedTrust.getRank(), equalTo(1));
+               }
+
+               private TestFcpConnection createConnectionThatDeliversIdentityWithoutTrustButAScore() {
+                       return createFcpConnection(message -> (listener, connection) -> {
+                               Map<String, String> fields = createWebOfTrustReplyFields("Identity", true,
+                                               entries("Identities.Amount", "1"), identityNick0,
+                                               entries("Trusts.0.Value", "Nonexistent"),
+                                               entries("Scores.Amount", "1"), scoreValue
+                               );
+                               FcpMessage replyMessage = createPluginReply(message, fields);
+                               listener.receivedFCPPluginReply(connection, new FCPPluginReply(replyMessage, null));
+                       });
+               }
+
+               @Test
+               public void getIdentityTrustCanHandleMissingScoreAndRank() throws Exception {
+                       TestFcpConnection fcpConnection = createConnectionThatDeliversIdentityWithTrustButWithoutScore();
+                       WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection);
+                       OwnIdentity ownIdentity = new OwnIdentity("own-id", "Own ID", "own-pub", "own-priv", emptySet(), emptyMap());
+                       CalculatedTrust calculatedTrust = webOfTrustPlugin.getIdentityTrust(ownIdentity, "other-id");
+                       assertThat(calculatedTrust.getTrust(), equalTo((byte) 56));
+                       assertThat(calculatedTrust.getScore(), nullValue());
+                       assertThat(calculatedTrust.getRank(), nullValue());
+               }
+
+               private TestFcpConnection createConnectionThatDeliversIdentityWithTrustButWithoutScore() {
+                       return createFcpConnection(message -> (listener, connection) -> {
+                               Map<String, String> fields = createWebOfTrustReplyFields("Identity", true,
+                                               entries("Identities.Amount", "1"), identityNick0,
+                                               entries("Trusts.Amount", "1"), trustValue,
+                                               entries("Scores.0.Value", "Nonexistent")
+                               );
+                               FcpMessage replyMessage = createPluginReply(message, fields);
+                               listener.receivedFCPPluginReply(connection, new FCPPluginReply(replyMessage, null));
+                       });
+               }
+
+               @Test
                public void getIdentityTrustFailsWhenDifferentReplyIsSentByPlugin() {
                        FcpConnection fcpConnection = createConnectionThatSendsOtherMessage();
                        WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection);