From: David ‘Bombe’ Roden Date: Fri, 1 Nov 2019 20:07:08 +0000 (+0100) Subject: ♻️ Extract interface for WOT connector X-Git-Tag: v81^2~77 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=fdc8dbd38e871da0a278bded063ba9dba8af9edc;p=Sone.git ♻️ Extract interface for WOT connector --- diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnector.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnector.kt new file mode 100644 index 0000000..78eb036 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnector.kt @@ -0,0 +1,168 @@ +/* + * Sone - WebOfTrustConnector.java - Copyright © 2010–2019 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.freenet.wot + +import com.google.inject.* +import freenet.support.* +import freenet.support.api.* +import net.pterodactylus.sone.freenet.* +import net.pterodactylus.sone.freenet.plugin.* +import net.pterodactylus.sone.utils.NumberParsers.* +import java.lang.String.* +import java.util.* +import java.util.logging.* +import java.util.logging.Logger +import java.util.logging.Logger.* + +/** + * Connector for the Web of Trust plugin. + */ +class PluginWebOfTrustConnector @Inject constructor(private val pluginConnector: PluginConnector) : WebOfTrustConnector { + + private val logger: Logger = getLogger(PluginWebOfTrustConnector::class.java.name) + + override fun loadAllOwnIdentities(): Set { + val (fields) = performRequest(SimpleFieldSetBuilder().put("Message", "GetOwnIdentities").get()) + var ownIdentityCounter = -1 + val ownIdentities = HashSet() + while (true) { + val id = fields.get("Identity" + ++ownIdentityCounter) ?: break + val requestUri = fields.get("RequestURI$ownIdentityCounter") + val insertUri = fields.get("InsertURI$ownIdentityCounter") + val nickname = fields.get("Nickname$ownIdentityCounter") + val ownIdentity = DefaultOwnIdentity(id, nickname, requestUri, insertUri) + ownIdentity.setContexts(parseContexts("Contexts$ownIdentityCounter.", fields)) + ownIdentity.properties = parseProperties("Properties$ownIdentityCounter.", fields) + ownIdentities.add(ownIdentity) + } + return ownIdentities + } + + override fun loadTrustedIdentities(ownIdentity: OwnIdentity, context: String?): Set { + val (fields) = performRequest(SimpleFieldSetBuilder().put("Message", "GetIdentitiesByScore").put("Truster", ownIdentity.id).put("Selection", "+").put("Context", context ?: "").put("WantTrustValues", "true").get()) + val identities = HashSet() + var identityCounter = -1 + while (true) { + val id = fields.get("Identity" + ++identityCounter) ?: break + val nickname = fields.get("Nickname$identityCounter") + val requestUri = fields.get("RequestURI$identityCounter") + val identity = DefaultIdentity(id, nickname, requestUri) + identity.setContexts(parseContexts("Contexts$identityCounter.", fields)) + identity.properties = parseProperties("Properties$identityCounter.", fields) + val trust = parseInt(fields.get("Trust$identityCounter"), null) + val score = parseInt(fields.get("Score$identityCounter"), 0)!! + val rank = parseInt(fields.get("Rank$identityCounter"), 0)!! + identity.setTrust(ownIdentity, Trust(trust, score, rank)) + identities.add(identity) + } + return identities + } + + @Throws(PluginException::class) + override fun addContext(ownIdentity: OwnIdentity, context: String) { + performRequest(SimpleFieldSetBuilder().put("Message", "AddContext").put("Identity", ownIdentity.id).put("Context", context).get()) + } + + @Throws(PluginException::class) + override fun removeContext(ownIdentity: OwnIdentity, context: String) { + performRequest(SimpleFieldSetBuilder().put("Message", "RemoveContext").put("Identity", ownIdentity.id).put("Context", context).get()) + } + + override fun setProperty(ownIdentity: OwnIdentity, name: String, value: String) { + performRequest(SimpleFieldSetBuilder().put("Message", "SetProperty").put("Identity", ownIdentity.id).put("Property", name).put("Value", value).get()) + } + + override fun removeProperty(ownIdentity: OwnIdentity, name: String) { + performRequest(SimpleFieldSetBuilder().put("Message", "RemoveProperty").put("Identity", ownIdentity.id).put("Property", name).get()) + } + + override fun getTrust(ownIdentity: OwnIdentity, identity: Identity): Trust { + val (fields) = performRequest(SimpleFieldSetBuilder().put("Message", "GetIdentity").put("Truster", ownIdentity.id).put("Identity", identity.id).get()) + val trust = fields.get("Trust") + val score = fields.get("Score") + val rank = fields.get("Rank") + var explicit: Int? = null + var implicit: Int? = null + var distance: Int? = null + try { + explicit = Integer.valueOf(trust) + } catch (nfe1: NumberFormatException) { + /* ignore. */ + } + + try { + implicit = Integer.valueOf(score) + } catch (nfe1: NumberFormatException) { + /* ignore. */ + } + + try { + distance = Integer.valueOf(rank) + } catch (nfe1: NumberFormatException) { + /* ignore. */ + } + + return Trust(explicit, implicit, distance) + } + + override fun setTrust(ownIdentity: OwnIdentity, identity: Identity, trust: Int, comment: String) { + performRequest(SimpleFieldSetBuilder().put("Message", "SetTrust").put("Truster", ownIdentity.id).put("Trustee", identity.id).put("Value", trust.toString()).put("Comment", comment).get()) + } + + override fun removeTrust(ownIdentity: OwnIdentity, identity: Identity) { + performRequest(SimpleFieldSetBuilder().put("Message", "RemoveTrust").put("Truster", ownIdentity.id).put("Trustee", identity.id).get()) + } + + override fun ping() { + performRequest(SimpleFieldSetBuilder().put("Message", "Ping").get()) + } + + private fun performRequest(fields: SimpleFieldSet, data: Bucket? = null): PluginReply { + logger.log(Level.FINE, format("Sending FCP Request: %s", fields.get("Message"))) + val pluginReply = pluginConnector.sendRequest(WOT_PLUGIN_NAME, "", fields, data) + logger.log(Level.FINEST, format("Received FCP Response for %s: %s", fields.get("Message"), pluginReply.fields.get("Message"))) + if ("Error" == pluginReply.fields.get("Message")) { + throw PluginException("Could not perform request for " + fields.get("Message")) + } + return pluginReply + } + +} + +private const val WOT_PLUGIN_NAME = "plugins.WebOfTrust.WebOfTrust" + +private fun parseContexts(prefix: String, fields: SimpleFieldSet): Set { + val contexts = HashSet() + var contextCounter = -1 + while (true) { + val context = fields.get(prefix + "Context" + ++contextCounter) ?: break + contexts.add(context) + } + return contexts +} + +private fun parseProperties(prefix: String, fields: SimpleFieldSet): Map { + val properties = HashMap() + var propertiesCounter = -1 + while (true) { + val propertyName = fields.get(prefix + "Property" + ++propertiesCounter + ".Name") ?: break + val propertyValue = fields.get(prefix + "Property" + propertiesCounter + ".Value") + properties[propertyName] = propertyValue + } + return properties +} diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.kt index 7131d22..8488f16 100644 --- a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.kt +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.kt @@ -1,45 +1,11 @@ -/* - * Sone - WebOfTrustConnector.java - Copyright © 2010–2019 David Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - package net.pterodactylus.sone.freenet.wot -import com.google.inject.* -import freenet.support.* -import freenet.support.api.* -import net.pterodactylus.sone.freenet.* import net.pterodactylus.sone.freenet.plugin.* -import net.pterodactylus.sone.utils.NumberParsers.* -import java.lang.String.* -import java.util.* -import java.util.logging.* -import java.util.logging.Logger -import java.util.logging.Logger.* /** - * Connector for the Web of Trust plugin. + * Connector for the web of trust plugin. */ -@Singleton -class WebOfTrustConnector @Inject constructor(private val pluginConnector: PluginConnector) { - - private val logger: Logger = getLogger(WebOfTrustConnector::class.java.name) - - fun stop() { - /* does nothing. */ - } +interface WebOfTrustConnector { /** * Loads all own identities from the Web of Trust plugin. @@ -48,22 +14,7 @@ class WebOfTrustConnector @Inject constructor(private val pluginConnector: Plugi * @throws WebOfTrustException if the own identities can not be loaded */ @Throws(WebOfTrustException::class) - fun loadAllOwnIdentities(): Set { - val (fields) = performRequest(SimpleFieldSetBuilder().put("Message", "GetOwnIdentities").get()) - var ownIdentityCounter = -1 - val ownIdentities = HashSet() - while (true) { - val id = fields.get("Identity" + ++ownIdentityCounter) ?: break - val requestUri = fields.get("RequestURI$ownIdentityCounter") - val insertUri = fields.get("InsertURI$ownIdentityCounter") - val nickname = fields.get("Nickname$ownIdentityCounter") - val ownIdentity = DefaultOwnIdentity(id, nickname, requestUri, insertUri) - ownIdentity.setContexts(parseContexts("Contexts$ownIdentityCounter.", fields)) - ownIdentity.properties = parseProperties("Properties$ownIdentityCounter.", fields) - ownIdentities.add(ownIdentity) - } - return ownIdentities - } + fun loadAllOwnIdentities(): Set /** * Loads all identities that the given identities trusts with a score of @@ -75,26 +26,7 @@ class WebOfTrustConnector @Inject constructor(private val pluginConnector: Plugi * @throws PluginException if an error occured talking to the Web of Trust plugin */ @Throws(PluginException::class) - @JvmOverloads - fun loadTrustedIdentities(ownIdentity: OwnIdentity, context: String? = null): Set { - val (fields) = performRequest(SimpleFieldSetBuilder().put("Message", "GetIdentitiesByScore").put("Truster", ownIdentity.id).put("Selection", "+").put("Context", context ?: "").put("WantTrustValues", "true").get()) - val identities = HashSet() - var identityCounter = -1 - while (true) { - val id = fields.get("Identity" + ++identityCounter) ?: break - val nickname = fields.get("Nickname$identityCounter") - val requestUri = fields.get("RequestURI$identityCounter") - val identity = DefaultIdentity(id, nickname, requestUri) - identity.setContexts(parseContexts("Contexts$identityCounter.", fields)) - identity.properties = parseProperties("Properties$identityCounter.", fields) - val trust = parseInt(fields.get("Trust$identityCounter"), null) - val score = parseInt(fields.get("Score$identityCounter"), 0)!! - val rank = parseInt(fields.get("Rank$identityCounter"), 0)!! - identity.setTrust(ownIdentity, Trust(trust, score, rank)) - identities.add(identity) - } - return identities - } + fun loadTrustedIdentities(ownIdentity: OwnIdentity, context: String? = null): Set /** * Adds the given context to the given identity. @@ -104,9 +36,7 @@ class WebOfTrustConnector @Inject constructor(private val pluginConnector: Plugi * @throws PluginException if an error occured talking to the Web of Trust plugin */ @Throws(PluginException::class) - fun addContext(ownIdentity: OwnIdentity, context: String) { - performRequest(SimpleFieldSetBuilder().put("Message", "AddContext").put("Identity", ownIdentity.id).put("Context", context).get()) - } + fun addContext(ownIdentity: OwnIdentity, context: String) /** * Removes the given context from the given identity. @@ -116,9 +46,7 @@ class WebOfTrustConnector @Inject constructor(private val pluginConnector: Plugi * @throws PluginException if an error occured talking to the Web of Trust plugin */ @Throws(PluginException::class) - fun removeContext(ownIdentity: OwnIdentity, context: String) { - performRequest(SimpleFieldSetBuilder().put("Message", "RemoveContext").put("Identity", ownIdentity.id).put("Context", context).get()) - } + fun removeContext(ownIdentity: OwnIdentity, context: String) /** * Sets the property with the given name to the given value. @@ -129,9 +57,7 @@ class WebOfTrustConnector @Inject constructor(private val pluginConnector: Plugi * @throws PluginException if an error occured talking to the Web of Trust plugin */ @Throws(PluginException::class) - fun setProperty(ownIdentity: OwnIdentity, name: String, value: String) { - performRequest(SimpleFieldSetBuilder().put("Message", "SetProperty").put("Identity", ownIdentity.id).put("Property", name).put("Value", value).get()) - } + fun setProperty(ownIdentity: OwnIdentity, name: String, value: String) /** * Removes the property with the given name. @@ -141,9 +67,7 @@ class WebOfTrustConnector @Inject constructor(private val pluginConnector: Plugi * @throws PluginException if an error occured talking to the Web of Trust plugin */ @Throws(PluginException::class) - fun removeProperty(ownIdentity: OwnIdentity, name: String) { - performRequest(SimpleFieldSetBuilder().put("Message", "RemoveProperty").put("Identity", ownIdentity.id).put("Property", name).get()) - } + fun removeProperty(ownIdentity: OwnIdentity, name: String) /** * Returns the trust for the given identity assigned to it by the given own @@ -155,34 +79,7 @@ class WebOfTrustConnector @Inject constructor(private val pluginConnector: Plugi * @throws PluginException if an error occured talking to the Web of Trust plugin */ @Throws(PluginException::class) - fun getTrust(ownIdentity: OwnIdentity, identity: Identity): Trust { - val (fields) = performRequest(SimpleFieldSetBuilder().put("Message", "GetIdentity").put("Truster", ownIdentity.id).put("Identity", identity.id).get()) - val trust = fields.get("Trust") - val score = fields.get("Score") - val rank = fields.get("Rank") - var explicit: Int? = null - var implicit: Int? = null - var distance: Int? = null - try { - explicit = Integer.valueOf(trust) - } catch (nfe1: NumberFormatException) { - /* ignore. */ - } - - try { - implicit = Integer.valueOf(score) - } catch (nfe1: NumberFormatException) { - /* ignore. */ - } - - try { - distance = Integer.valueOf(rank) - } catch (nfe1: NumberFormatException) { - /* ignore. */ - } - - return Trust(explicit, implicit, distance) - } + fun getTrust(ownIdentity: OwnIdentity, identity: Identity): Trust /** * Sets the trust for the given identity. @@ -194,9 +91,7 @@ class WebOfTrustConnector @Inject constructor(private val pluginConnector: Plugi * @throws PluginException if an error occured talking to the Web of Trust plugin */ @Throws(PluginException::class) - fun setTrust(ownIdentity: OwnIdentity, identity: Identity, trust: Int, comment: String) { - performRequest(SimpleFieldSetBuilder().put("Message", "SetTrust").put("Truster", ownIdentity.id).put("Trustee", identity.id).put("Value", trust.toString()).put("Comment", comment).get()) - } + fun setTrust(ownIdentity: OwnIdentity, identity: Identity, trust: Int, comment: String) /** * Removes any trust assignment of the given own identity for the given @@ -207,9 +102,7 @@ class WebOfTrustConnector @Inject constructor(private val pluginConnector: Plugi * @throws WebOfTrustException if an error occurs */ @Throws(WebOfTrustException::class) - fun removeTrust(ownIdentity: OwnIdentity, identity: Identity) { - performRequest(SimpleFieldSetBuilder().put("Message", "RemoveTrust").put("Truster", ownIdentity.id).put("Trustee", identity.id).get()) - } + fun removeTrust(ownIdentity: OwnIdentity, identity: Identity) /** * Pings the Web of Trust plugin. If the plugin can not be reached, a @@ -218,42 +111,11 @@ class WebOfTrustConnector @Inject constructor(private val pluginConnector: Plugi * @throws PluginException if the plugin is not loaded */ @Throws(PluginException::class) - fun ping() { - performRequest(SimpleFieldSetBuilder().put("Message", "Ping").get()) - } - - private fun performRequest(fields: SimpleFieldSet, data: Bucket? = null): PluginReply { - logger.log(Level.FINE, format("Sending FCP Request: %s", fields.get("Message"))) - val pluginReply = pluginConnector.sendRequest(WOT_PLUGIN_NAME, "", fields, data) - logger.log(Level.FINEST, format("Received FCP Response for %s: %s", fields.get("Message"), pluginReply.fields.get("Message"))) - if ("Error" == pluginReply.fields.get("Message")) { - throw PluginException("Could not perform request for " + fields.get("Message")) - } - return pluginReply - } + fun ping() -} - -private const val WOT_PLUGIN_NAME = "plugins.WebOfTrust.WebOfTrust" - -private fun parseContexts(prefix: String, fields: SimpleFieldSet): Set { - val contexts = HashSet() - var contextCounter = -1 - while (true) { - val context = fields.get(prefix + "Context" + ++contextCounter) ?: break - contexts.add(context) - } - return contexts -} + /** + * Stops the web of trust connector. + */ + fun stop() = Unit -private fun parseProperties(prefix: String, fields: SimpleFieldSet): Map { - val properties = HashMap() - var propertiesCounter = -1 - while (true) { - val propertyName = fields.get(prefix + "Property" + ++propertiesCounter + ".Name") ?: break - val propertyValue = fields.get(prefix + "Property" + propertiesCounter + ".Value") - properties[propertyName] = propertyValue - } - return properties } - diff --git a/src/main/kotlin/net/pterodactylus/sone/main/SoneModule.kt b/src/main/kotlin/net/pterodactylus/sone/main/SoneModule.kt index dcb5b3b..e754727 100644 --- a/src/main/kotlin/net/pterodactylus/sone/main/SoneModule.kt +++ b/src/main/kotlin/net/pterodactylus/sone/main/SoneModule.kt @@ -53,6 +53,7 @@ open class SoneModule(private val sonePlugin: SonePlugin, private val eventBus: bind(BaseL10n::class.java).toInstance(sonePlugin.l10n().base) loaders?.let { bind(Loaders::class.java).toInstance(it) } bind(MetricRegistry::class.java).`in`(Singleton::class.java) + bind(WebOfTrustConnector::class.java).to(PluginWebOfTrustConnector::class.java).`in`(Singleton::class.java) bindListener(Matchers.any(), object : TypeListener { override fun hear(typeLiteral: TypeLiteral, typeEncounter: TypeEncounter) { diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnectorTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnectorTest.kt new file mode 100644 index 0000000..e5d7da2 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnectorTest.kt @@ -0,0 +1,317 @@ +package net.pterodactylus.sone.freenet.wot + +import freenet.support.* +import freenet.support.api.* +import net.pterodactylus.sone.freenet.* +import net.pterodactylus.sone.freenet.plugin.* +import net.pterodactylus.sone.test.* +import org.hamcrest.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import org.hamcrest.core.* +import kotlin.test.* + +/** + * Unit test for [PluginWebOfTrustConnector]. + */ +class PluginWebOfTrustConnectorTest { + + private val ownIdentity = DefaultOwnIdentity("id", "nickname", "requestUri", "insertUri") + private val identity = DefaultIdentity("id-a", "alpha", "url://alpha") + + @Test + fun `wot plugin can be pinged`() { + createPluginConnector("Ping") + .connect { ping() } + } + + @Test + fun `own identities are returned correctly`() { + val ownIdentities = createPluginConnector("GetOwnIdentities") { + put("Identity0", "id-0") + put("RequestURI0", "request-uri-0") + put("InsertURI0", "insert-uri-0") + put("Nickname0", "nickname-0") + put("Contexts0.Context0", "id-0-context-0") + put("Properties0.Property0.Name", "id-0-property-0-name") + put("Properties0.Property0.Value", "id-0-property-0-value") + put("Identity1", "id-1") + put("RequestURI1", "request-uri-1") + put("InsertURI1", "insert-uri-1") + put("Nickname1", "nickname-1") + put("Contexts1.Context0", "id-1-context-0") + put("Properties1.Property0.Name", "id-1-property-0-name") + put("Properties1.Property0.Value", "id-1-property-0-value") + }.connect { loadAllOwnIdentities() } + assertThat(ownIdentities, containsInAnyOrder( + isOwnIdentity("id-0", "nickname-0", "request-uri-0", "insert-uri-0", contains("id-0-context-0"), hasEntry("id-0-property-0-name", "id-0-property-0-value")), + isOwnIdentity("id-1", "nickname-1", "request-uri-1", "insert-uri-1", contains("id-1-context-0"), hasEntry("id-1-property-0-name", "id-1-property-0-value")) + )) + } + + @Test + fun `trusted identities are requested with correct own identity`() { + createPluginConnector("GetIdentitiesByScore", hasField("Truster", equalTo("id"))) + .connect { loadTrustedIdentities(ownIdentity) } + } + + @Test + fun `trusted identities are requested with correct selection parameter`() { + createPluginConnector("GetIdentitiesByScore", hasField("Selection", equalTo("+"))) + .connect { loadTrustedIdentities(ownIdentity) } + } + + @Test + fun `trusted identities are requested with empty context if null context requested`() { + createPluginConnector("GetIdentitiesByScore", hasField("Context", equalTo(""))) + .connect { loadTrustedIdentities(ownIdentity) } + } + + @Test + fun `trusted identities are requested with context if context requested`() { + createPluginConnector("GetIdentitiesByScore", hasField("Context", equalTo("TestContext"))) + .connect { loadTrustedIdentities(ownIdentity, "TestContext") } + } + + @Test + fun `trusted identities are requested with trust values`() { + createPluginConnector("GetIdentitiesByScore", hasField("WantTrustValues", equalTo("true"))) + .connect { loadTrustedIdentities(ownIdentity) } + } + + @Test + fun `empty list of trusted identities is returned correctly`() { + val trustedIdentities = createPluginConnector("GetIdentitiesByScore") + .connect { loadTrustedIdentities(ownIdentity) } + assertThat(trustedIdentities, empty()) + } + + @Test + fun `trusted identities without context, properties, or trust value are returned correctly`() { + val trustedIdentities = createPluginConnector("GetIdentitiesByScore") { + put("Identity0", "id0") + put("Nickname0", "nickname0") + put("RequestURI0", "request-uri0") + put("Identity1", "id1") + put("Nickname1", "nickname1") + put("RequestURI1", "request-uri1") + }.connect { loadTrustedIdentities(ownIdentity) } + assertThat(trustedIdentities, contains( + allOf( + isIdentity("id0", "nickname0", "request-uri0", empty(), isEmptyMap()), + isTrusted(ownIdentity, isTrust(null, 0, 0)) + ), + allOf( + isIdentity("id1", "nickname1", "request-uri1", empty(), isEmptyMap()), + isTrusted(ownIdentity, isTrust(null, 0, 0)) + ) + )) + } + + @Test + fun `trusted identity with contexts is returned correctly`() { + val trustedIdentities = createPluginConnector("GetIdentitiesByScore") { + put("Identity0", "id0") + put("Nickname0", "nickname0") + put("RequestURI0", "request-uri0") + put("Contexts0.Context0", "Context0") + put("Contexts0.Context1", "Context1") + }.connect { loadTrustedIdentities(ownIdentity) } + assertThat(trustedIdentities, contains( + isIdentity("id0", "nickname0", "request-uri0", containsInAnyOrder("Context0", "Context1"), isEmptyMap()) + )) + } + + @Test + fun `trusted identity with properties is returned correctly`() { + val trustedIdentities = createPluginConnector("GetIdentitiesByScore") { + put("Identity0", "id0") + put("Nickname0", "nickname0") + put("RequestURI0", "request-uri0") + put("Properties0.Property0.Name", "foo") + put("Properties0.Property0.Value", "bar") + put("Properties0.Property1.Name", "baz") + put("Properties0.Property1.Value", "quo") + }.connect { loadTrustedIdentities(ownIdentity) } + assertThat(trustedIdentities, contains( + isIdentity("id0", "nickname0", "request-uri0", empty(), allOf(hasEntry("foo", "bar"), hasEntry("baz", "quo"))) + )) + } + + @Test + fun `trusted identity with trust value is returned correctly`() { + val trustedIdentities = createPluginConnector("GetIdentitiesByScore") { + put("Identity0", "id0") + put("Nickname0", "nickname0") + put("RequestURI0", "request-uri0") + put("Trust0", "12") + put("Score0", "34") + put("Rank0", "56") + }.connect { loadTrustedIdentities(ownIdentity) } + assertThat(trustedIdentities, contains( + allOf( + isIdentity("id0", "nickname0", "request-uri0", empty(), isEmptyMap()), + isTrusted(ownIdentity, isTrust(12, 34, 56)) + ) + )) + } + + @Test + fun `adding a context sends the correct own identity id`() { + createPluginConnector("AddContext", hasField("Identity", equalTo(ownIdentity.id))) + .connect { addContext(ownIdentity, "TestContext") } + } + + @Test + fun `adding a context sends the correct context`() { + createPluginConnector("AddContext", hasField("Context", equalTo("TestContext"))) + .connect { addContext(ownIdentity, "TestContext") } + } + + @Test + fun `removing a context sends the correct own identity id`() { + createPluginConnector("RemoveContext", hasField("Identity", equalTo(ownIdentity.id))) + .connect { removeContext(ownIdentity, "TestContext") } + } + + @Test + fun `removing a context sends the correct context`() { + createPluginConnector("RemoveContext", hasField("Context", equalTo("TestContext"))) + .connect { removeContext(ownIdentity, "TestContext") } + } + + @Test + fun `setting a property sends the correct identity id`() { + createPluginConnector("SetProperty", hasField("Identity", equalTo(ownIdentity.id))) + .connect { setProperty(ownIdentity, "TestProperty", "TestValue") } + } + + @Test + fun `setting a property sends the correct property name`() { + createPluginConnector("SetProperty", hasField("Property", equalTo("TestProperty"))) + .connect { setProperty(ownIdentity, "TestProperty", "TestValue") } + } + + @Test + fun `setting a property sends the correct property value`() { + createPluginConnector("SetProperty", hasField("Value", equalTo("TestValue"))) + .connect { setProperty(ownIdentity, "TestProperty", "TestValue") } + } + + @Test + fun `removing a property sends the correct identity id`() { + createPluginConnector("RemoveProperty", hasField("Identity", equalTo(ownIdentity.id))) + .connect { removeProperty(ownIdentity, "TestProperty") } + } + + @Test + fun `removing a property sends the correct property name`() { + createPluginConnector("RemoveProperty", hasField("Property", equalTo("TestProperty"))) + .connect { removeProperty(ownIdentity, "TestProperty") } + } + + @Test + fun `getting trust sends correct own identity id`() { + createPluginConnector("GetIdentity", hasField("Truster", equalTo(ownIdentity.id))) + .connect { getTrust(ownIdentity, identity) } + } + + @Test + fun `getting trust sends correct identity id`() { + createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) + .connect { getTrust(ownIdentity, identity) } + } + + @Test + fun `getting trust returns correct trust values`() { + val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) { + put("Trust", "12") + put("Score", "34") + put("Rank", "56") + }.connect { getTrust(ownIdentity, identity) } + assertThat(trust, isTrust(12, 34, 56)) + } + + @Test + fun `getting trust reads incorrect numbers for trust as null`() { + val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) { + put("Trust", "incorrect") + put("Score", "34") + put("Rank", "56") + }.connect { getTrust(ownIdentity, identity) } + assertThat(trust, isTrust(null, 34, 56)) + } + + @Test + fun `getting trust reads incorrect numbers for score as null`() { + val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) { + put("Trust", "12") + put("Score", "incorrect") + put("Rank", "56") + }.connect { getTrust(ownIdentity, identity) } + assertThat(trust, isTrust(12, null, 56)) + } + + @Test + fun `getting trust reads incorrect numbers for rank as null`() { + val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) { + put("Trust", "12") + put("Score", "34") + put("Rank", "incorrect") + }.connect { getTrust(ownIdentity, identity) } + assertThat(trust, isTrust(12, 34, null)) + } + + @Test + fun `setting trust sends correct own identity id`() { + createPluginConnector("SetTrust", hasField("Truster", equalTo(ownIdentity.id))) + .connect { setTrust(ownIdentity, identity, 123, "Test Trust") } + } + + @Test + fun `setting trust sends correct identity id`() { + createPluginConnector("SetTrust", hasField("Trustee", equalTo(identity.id))) + .connect { setTrust(ownIdentity, identity, 123, "Test Trust") } + } + + @Test + fun `setting trust sends correct trust value`() { + createPluginConnector("SetTrust", hasField("Value", equalTo("123"))) + .connect { setTrust(ownIdentity, identity, 123, "Test Trust") } + } + + @Test + fun `setting trust sends correct comment`() { + createPluginConnector("SetTrust", hasField("Comment", equalTo("Test Trust"))) + .connect { setTrust(ownIdentity, identity, 123, "Test Trust") } + } + + @Test + fun `removing trust sends correct own identity id`() { + createPluginConnector("RemoveTrust", hasField("Truster", equalTo(ownIdentity.id))) + .connect { removeTrust(ownIdentity, identity) } + } + + @Test + fun `removing trust sends correct identity id`() { + createPluginConnector("RemoveTrust", hasField("Trustee", equalTo(identity.id))) + .connect { removeTrust(ownIdentity, identity) } + } + +} + +private fun PluginConnector.connect(block: PluginWebOfTrustConnector.() -> R) = + PluginWebOfTrustConnector(this).let(block) + +fun createPluginConnector(message: String, fieldsMatcher: Matcher = IsAnything(), build: SimpleFieldSetBuilder.() -> Unit = {}) = + object : PluginConnector { + override fun sendRequest(pluginName: String, identifier: String, fields: SimpleFieldSet, data: Bucket?) = + if ((pluginName != wotPluginName) || (fields.get("Message") != message)) { + throw PluginException() + } else { + assertThat(fields, fieldsMatcher) + PluginReply(SimpleFieldSetBuilder().apply(build).get(), null) + } + } + +private const val wotPluginName = "plugins.WebOfTrust.WebOfTrust" diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustConnectorTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustConnectorTest.kt deleted file mode 100644 index 2d02e3a..0000000 --- a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustConnectorTest.kt +++ /dev/null @@ -1,317 +0,0 @@ -package net.pterodactylus.sone.freenet.wot - -import freenet.support.* -import freenet.support.api.* -import net.pterodactylus.sone.freenet.* -import net.pterodactylus.sone.freenet.plugin.* -import net.pterodactylus.sone.test.* -import org.hamcrest.* -import org.hamcrest.MatcherAssert.* -import org.hamcrest.Matchers.* -import org.hamcrest.core.* -import kotlin.test.* - -/** - * Unit test for [WebOfTrustConnector]. - */ -class WebOfTrustConnectorTest { - - private val ownIdentity = DefaultOwnIdentity("id", "nickname", "requestUri", "insertUri") - private val identity = DefaultIdentity("id-a", "alpha", "url://alpha") - - @Test - fun `wot plugin can be pinged`() { - createPluginConnector("Ping") - .connect { ping() } - } - - @Test - fun `own identities are returned correctly`() { - val ownIdentities = createPluginConnector("GetOwnIdentities") { - put("Identity0", "id-0") - put("RequestURI0", "request-uri-0") - put("InsertURI0", "insert-uri-0") - put("Nickname0", "nickname-0") - put("Contexts0.Context0", "id-0-context-0") - put("Properties0.Property0.Name", "id-0-property-0-name") - put("Properties0.Property0.Value", "id-0-property-0-value") - put("Identity1", "id-1") - put("RequestURI1", "request-uri-1") - put("InsertURI1", "insert-uri-1") - put("Nickname1", "nickname-1") - put("Contexts1.Context0", "id-1-context-0") - put("Properties1.Property0.Name", "id-1-property-0-name") - put("Properties1.Property0.Value", "id-1-property-0-value") - }.connect { loadAllOwnIdentities() } - assertThat(ownIdentities, containsInAnyOrder( - isOwnIdentity("id-0", "nickname-0", "request-uri-0", "insert-uri-0", contains("id-0-context-0"), hasEntry("id-0-property-0-name", "id-0-property-0-value")), - isOwnIdentity("id-1", "nickname-1", "request-uri-1", "insert-uri-1", contains("id-1-context-0"), hasEntry("id-1-property-0-name", "id-1-property-0-value")) - )) - } - - @Test - fun `trusted identities are requested with correct own identity`() { - createPluginConnector("GetIdentitiesByScore", hasField("Truster", equalTo("id"))) - .connect { loadTrustedIdentities(ownIdentity) } - } - - @Test - fun `trusted identities are requested with correct selection parameter`() { - createPluginConnector("GetIdentitiesByScore", hasField("Selection", equalTo("+"))) - .connect { loadTrustedIdentities(ownIdentity) } - } - - @Test - fun `trusted identities are requested with empty context if null context requested`() { - createPluginConnector("GetIdentitiesByScore", hasField("Context", equalTo(""))) - .connect { loadTrustedIdentities(ownIdentity) } - } - - @Test - fun `trusted identities are requested with context if context requested`() { - createPluginConnector("GetIdentitiesByScore", hasField("Context", equalTo("TestContext"))) - .connect { loadTrustedIdentities(ownIdentity, "TestContext") } - } - - @Test - fun `trusted identities are requested with trust values`() { - createPluginConnector("GetIdentitiesByScore", hasField("WantTrustValues", equalTo("true"))) - .connect { loadTrustedIdentities(ownIdentity) } - } - - @Test - fun `empty list of trusted identities is returned correctly`() { - val trustedIdentities = createPluginConnector("GetIdentitiesByScore") - .connect { loadTrustedIdentities(ownIdentity) } - assertThat(trustedIdentities, empty()) - } - - @Test - fun `trusted identities without context, properties, or trust value are returned correctly`() { - val trustedIdentities = createPluginConnector("GetIdentitiesByScore") { - put("Identity0", "id0") - put("Nickname0", "nickname0") - put("RequestURI0", "request-uri0") - put("Identity1", "id1") - put("Nickname1", "nickname1") - put("RequestURI1", "request-uri1") - }.connect { loadTrustedIdentities(ownIdentity) } - assertThat(trustedIdentities, contains( - allOf( - isIdentity("id0", "nickname0", "request-uri0", empty(), isEmptyMap()), - isTrusted(ownIdentity, isTrust(null, 0, 0)) - ), - allOf( - isIdentity("id1", "nickname1", "request-uri1", empty(), isEmptyMap()), - isTrusted(ownIdentity, isTrust(null, 0, 0)) - ) - )) - } - - @Test - fun `trusted identity with contexts is returned correctly`() { - val trustedIdentities = createPluginConnector("GetIdentitiesByScore") { - put("Identity0", "id0") - put("Nickname0", "nickname0") - put("RequestURI0", "request-uri0") - put("Contexts0.Context0", "Context0") - put("Contexts0.Context1", "Context1") - }.connect { loadTrustedIdentities(ownIdentity) } - assertThat(trustedIdentities, contains( - isIdentity("id0", "nickname0", "request-uri0", containsInAnyOrder("Context0", "Context1"), isEmptyMap()) - )) - } - - @Test - fun `trusted identity with properties is returned correctly`() { - val trustedIdentities = createPluginConnector("GetIdentitiesByScore") { - put("Identity0", "id0") - put("Nickname0", "nickname0") - put("RequestURI0", "request-uri0") - put("Properties0.Property0.Name", "foo") - put("Properties0.Property0.Value", "bar") - put("Properties0.Property1.Name", "baz") - put("Properties0.Property1.Value", "quo") - }.connect { loadTrustedIdentities(ownIdentity) } - assertThat(trustedIdentities, contains( - isIdentity("id0", "nickname0", "request-uri0", empty(), allOf(hasEntry("foo", "bar"), hasEntry("baz", "quo"))) - )) - } - - @Test - fun `trusted identity with trust value is returned correctly`() { - val trustedIdentities = createPluginConnector("GetIdentitiesByScore") { - put("Identity0", "id0") - put("Nickname0", "nickname0") - put("RequestURI0", "request-uri0") - put("Trust0", "12") - put("Score0", "34") - put("Rank0", "56") - }.connect { loadTrustedIdentities(ownIdentity) } - assertThat(trustedIdentities, contains( - allOf( - isIdentity("id0", "nickname0", "request-uri0", empty(), isEmptyMap()), - isTrusted(ownIdentity, isTrust(12, 34, 56)) - ) - )) - } - - @Test - fun `adding a context sends the correct own identity id`() { - createPluginConnector("AddContext", hasField("Identity", equalTo(ownIdentity.id))) - .connect { addContext(ownIdentity, "TestContext") } - } - - @Test - fun `adding a context sends the correct context`() { - createPluginConnector("AddContext", hasField("Context", equalTo("TestContext"))) - .connect { addContext(ownIdentity, "TestContext") } - } - - @Test - fun `removing a context sends the correct own identity id`() { - createPluginConnector("RemoveContext", hasField("Identity", equalTo(ownIdentity.id))) - .connect { removeContext(ownIdentity, "TestContext") } - } - - @Test - fun `removing a context sends the correct context`() { - createPluginConnector("RemoveContext", hasField("Context", equalTo("TestContext"))) - .connect { removeContext(ownIdentity, "TestContext") } - } - - @Test - fun `setting a property sends the correct identity id`() { - createPluginConnector("SetProperty", hasField("Identity", equalTo(ownIdentity.id))) - .connect { setProperty(ownIdentity, "TestProperty", "TestValue") } - } - - @Test - fun `setting a property sends the correct property name`() { - createPluginConnector("SetProperty", hasField("Property", equalTo("TestProperty"))) - .connect { setProperty(ownIdentity, "TestProperty", "TestValue") } - } - - @Test - fun `setting a property sends the correct property value`() { - createPluginConnector("SetProperty", hasField("Value", equalTo("TestValue"))) - .connect { setProperty(ownIdentity, "TestProperty", "TestValue") } - } - - @Test - fun `removing a property sends the correct identity id`() { - createPluginConnector("RemoveProperty", hasField("Identity", equalTo(ownIdentity.id))) - .connect { removeProperty(ownIdentity, "TestProperty") } - } - - @Test - fun `removing a property sends the correct property name`() { - createPluginConnector("RemoveProperty", hasField("Property", equalTo("TestProperty"))) - .connect { removeProperty(ownIdentity, "TestProperty") } - } - - @Test - fun `getting trust sends correct own identity id`() { - createPluginConnector("GetIdentity", hasField("Truster", equalTo(ownIdentity.id))) - .connect { getTrust(ownIdentity, identity) } - } - - @Test - fun `getting trust sends correct identity id`() { - createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) - .connect { getTrust(ownIdentity, identity) } - } - - @Test - fun `getting trust returns correct trust values`() { - val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) { - put("Trust", "12") - put("Score", "34") - put("Rank", "56") - }.connect { getTrust(ownIdentity, identity) } - assertThat(trust, isTrust(12, 34, 56)) - } - - @Test - fun `getting trust reads incorrect numbers for trust as null`() { - val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) { - put("Trust", "incorrect") - put("Score", "34") - put("Rank", "56") - }.connect { getTrust(ownIdentity, identity) } - assertThat(trust, isTrust(null, 34, 56)) - } - - @Test - fun `getting trust reads incorrect numbers for score as null`() { - val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) { - put("Trust", "12") - put("Score", "incorrect") - put("Rank", "56") - }.connect { getTrust(ownIdentity, identity) } - assertThat(trust, isTrust(12, null, 56)) - } - - @Test - fun `getting trust reads incorrect numbers for rank as null`() { - val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) { - put("Trust", "12") - put("Score", "34") - put("Rank", "incorrect") - }.connect { getTrust(ownIdentity, identity) } - assertThat(trust, isTrust(12, 34, null)) - } - - @Test - fun `setting trust sends correct own identity id`() { - createPluginConnector("SetTrust", hasField("Truster", equalTo(ownIdentity.id))) - .connect { setTrust(ownIdentity, identity, 123, "Test Trust") } - } - - @Test - fun `setting trust sends correct identity id`() { - createPluginConnector("SetTrust", hasField("Trustee", equalTo(identity.id))) - .connect { setTrust(ownIdentity, identity, 123, "Test Trust") } - } - - @Test - fun `setting trust sends correct trust value`() { - createPluginConnector("SetTrust", hasField("Value", equalTo("123"))) - .connect { setTrust(ownIdentity, identity, 123, "Test Trust") } - } - - @Test - fun `setting trust sends correct comment`() { - createPluginConnector("SetTrust", hasField("Comment", equalTo("Test Trust"))) - .connect { setTrust(ownIdentity, identity, 123, "Test Trust") } - } - - @Test - fun `removing trust sends correct own identity id`() { - createPluginConnector("RemoveTrust", hasField("Truster", equalTo(ownIdentity.id))) - .connect { removeTrust(ownIdentity, identity) } - } - - @Test - fun `removing trust sends correct identity id`() { - createPluginConnector("RemoveTrust", hasField("Trustee", equalTo(identity.id))) - .connect { removeTrust(ownIdentity, identity) } - } - -} - -private fun PluginConnector.connect(block: WebOfTrustConnector.() -> R) = - WebOfTrustConnector(this).let(block) - -fun createPluginConnector(message: String, fieldsMatcher: Matcher = IsAnything(), build: SimpleFieldSetBuilder.() -> Unit = {}) = - object : PluginConnector { - override fun sendRequest(pluginName: String, identifier: String, fields: SimpleFieldSet, data: Bucket?) = - if ((pluginName != wotPluginName) || (fields.get("Message") != message)) { - throw PluginException() - } else { - assertThat(fields, fieldsMatcher) - PluginReply(SimpleFieldSetBuilder().apply(build).get(), null) - } - } - -private const val wotPluginName = "plugins.WebOfTrust.WebOfTrust" diff --git a/src/test/kotlin/net/pterodactylus/sone/main/SoneModuleTest.kt b/src/test/kotlin/net/pterodactylus/sone/main/SoneModuleTest.kt index 7b5b4af..a5efbe6 100644 --- a/src/test/kotlin/net/pterodactylus/sone/main/SoneModuleTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/main/SoneModuleTest.kt @@ -219,4 +219,16 @@ class SoneModuleTest { assertThat(firstMetricRegistry, sameInstance(secondMetricRegistry)) } + @Test + fun `wot connector can be created`() { + assertThat(injector.getInstance(), notNullValue()) + } + + @Test + fun `wot connector is created as singleton`() { + val firstWebOfTrustConnector = injector.getInstance() + val secondWebOfTrustConnector = injector.getInstance() + assertThat(firstWebOfTrustConnector, sameInstance(secondWebOfTrustConnector)) + } + }