1 package net.pterodactylus.sone.freenet.wot
3 import freenet.support.*
4 import freenet.support.api.*
5 import net.pterodactylus.sone.freenet.*
6 import net.pterodactylus.sone.freenet.plugin.*
7 import net.pterodactylus.sone.test.*
9 import org.hamcrest.MatcherAssert.*
10 import org.hamcrest.Matchers.*
11 import org.hamcrest.core.*
15 * Unit test for [PluginWebOfTrustConnector].
17 class PluginWebOfTrustConnectorTest {
19 private val ownIdentity = DefaultOwnIdentity("id", "nickname", "requestUri", "insertUri")
20 private val identity = DefaultIdentity("id-a", "alpha", "url://alpha")
23 fun `wot plugin can be pinged`() {
24 createPluginConnector("Ping")
29 fun `own identities are returned correctly`() {
30 val ownIdentities = createPluginConnector("GetOwnIdentities") {
31 put("Identity0", "id-0")
32 put("RequestURI0", "request-uri-0")
33 put("InsertURI0", "insert-uri-0")
34 put("Nickname0", "nickname-0")
35 put("Contexts0.Context0", "id-0-context-0")
36 put("Properties0.Property0.Name", "id-0-property-0-name")
37 put("Properties0.Property0.Value", "id-0-property-0-value")
38 put("Identity1", "id-1")
39 put("RequestURI1", "request-uri-1")
40 put("InsertURI1", "insert-uri-1")
41 put("Nickname1", "nickname-1")
42 put("Contexts1.Context0", "id-1-context-0")
43 put("Properties1.Property0.Name", "id-1-property-0-name")
44 put("Properties1.Property0.Value", "id-1-property-0-value")
45 }.connect { loadAllOwnIdentities() }
46 assertThat(ownIdentities, containsInAnyOrder(
47 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")),
48 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"))
53 fun `trusted identities are requested with correct own identity`() {
54 createPluginConnector("GetIdentitiesByScore", hasField("Truster", equalTo("id")))
55 .connect { loadTrustedIdentities(ownIdentity) }
59 fun `trusted identities are requested with correct selection parameter`() {
60 createPluginConnector("GetIdentitiesByScore", hasField("Selection", equalTo("+")))
61 .connect { loadTrustedIdentities(ownIdentity) }
65 fun `trusted identities are requested with empty context if null context requested`() {
66 createPluginConnector("GetIdentitiesByScore", hasField("Context", equalTo("")))
67 .connect { loadTrustedIdentities(ownIdentity) }
71 fun `trusted identities are requested with context if context requested`() {
72 createPluginConnector("GetIdentitiesByScore", hasField("Context", equalTo("TestContext")))
73 .connect { loadTrustedIdentities(ownIdentity, "TestContext") }
77 fun `trusted identities are requested with trust values`() {
78 createPluginConnector("GetIdentitiesByScore", hasField("WantTrustValues", equalTo("true")))
79 .connect { loadTrustedIdentities(ownIdentity) }
83 fun `empty list of trusted identities is returned correctly`() {
84 val trustedIdentities = createPluginConnector("GetIdentitiesByScore")
85 .connect { loadTrustedIdentities(ownIdentity) }
86 assertThat(trustedIdentities, empty())
90 fun `trusted identities without context, properties, or trust value are returned correctly`() {
91 val trustedIdentities = createPluginConnector("GetIdentitiesByScore") {
92 put("Identity0", "id0")
93 put("Nickname0", "nickname0")
94 put("RequestURI0", "request-uri0")
95 put("Identity1", "id1")
96 put("Nickname1", "nickname1")
97 put("RequestURI1", "request-uri1")
98 }.connect { loadTrustedIdentities(ownIdentity) }
99 assertThat(trustedIdentities, contains(
101 isIdentity("id0", "nickname0", "request-uri0", empty<String>(), isEmptyMap()),
102 isTrusted(ownIdentity, isTrust(null, null, null))
105 isIdentity("id1", "nickname1", "request-uri1", empty<String>(), isEmptyMap()),
106 isTrusted(ownIdentity, isTrust(null, null, null))
112 fun `trusted identity without nickname is returned correctly`() {
113 val trustedIdentities = createPluginConnector("GetIdentitiesByScore") {
114 put("Identity0", "id0")
115 put("RequestURI0", "request-uri0")
116 }.connect { loadTrustedIdentities(ownIdentity) }
117 assertThat(trustedIdentities, contains(
119 isIdentity("id0", null, "request-uri0", empty<String>(), isEmptyMap()),
120 isTrusted(ownIdentity, isTrust(null, null, null))
126 fun `trusted identity with contexts is returned correctly`() {
127 val trustedIdentities = createPluginConnector("GetIdentitiesByScore") {
128 put("Identity0", "id0")
129 put("Nickname0", "nickname0")
130 put("RequestURI0", "request-uri0")
131 put("Contexts0.Context0", "Context0")
132 put("Contexts0.Context1", "Context1")
133 }.connect { loadTrustedIdentities(ownIdentity) }
134 assertThat(trustedIdentities, contains(
135 isIdentity("id0", "nickname0", "request-uri0", containsInAnyOrder("Context0", "Context1"), isEmptyMap())
140 fun `trusted identity with properties is returned correctly`() {
141 val trustedIdentities = createPluginConnector("GetIdentitiesByScore") {
142 put("Identity0", "id0")
143 put("Nickname0", "nickname0")
144 put("RequestURI0", "request-uri0")
145 put("Properties0.Property0.Name", "foo")
146 put("Properties0.Property0.Value", "bar")
147 put("Properties0.Property1.Name", "baz")
148 put("Properties0.Property1.Value", "quo")
149 }.connect { loadTrustedIdentities(ownIdentity) }
150 assertThat(trustedIdentities, contains(
151 isIdentity("id0", "nickname0", "request-uri0", empty(), allOf(hasEntry("foo", "bar"), hasEntry("baz", "quo")))
156 fun `trusted identity with trust value is returned correctly`() {
157 val trustedIdentities = createPluginConnector("GetIdentitiesByScore") {
158 put("Identity0", "id0")
159 put("Nickname0", "nickname0")
160 put("RequestURI0", "request-uri0")
164 }.connect { loadTrustedIdentities(ownIdentity) }
165 assertThat(trustedIdentities, contains(
167 isIdentity("id0", "nickname0", "request-uri0", empty(), isEmptyMap()),
168 isTrusted(ownIdentity, isTrust(12, 34, 56))
174 fun `adding a context sends the correct own identity id`() {
175 createPluginConnector("AddContext", hasField("Identity", equalTo(ownIdentity.id)))
176 .connect { addContext(ownIdentity, "TestContext") }
180 fun `adding a context sends the correct context`() {
181 createPluginConnector("AddContext", hasField("Context", equalTo("TestContext")))
182 .connect { addContext(ownIdentity, "TestContext") }
186 fun `removing a context sends the correct own identity id`() {
187 createPluginConnector("RemoveContext", hasField("Identity", equalTo(ownIdentity.id)))
188 .connect { removeContext(ownIdentity, "TestContext") }
192 fun `removing a context sends the correct context`() {
193 createPluginConnector("RemoveContext", hasField("Context", equalTo("TestContext")))
194 .connect { removeContext(ownIdentity, "TestContext") }
198 fun `setting a property sends the correct identity id`() {
199 createPluginConnector("SetProperty", hasField("Identity", equalTo(ownIdentity.id)))
200 .connect { setProperty(ownIdentity, "TestProperty", "TestValue") }
204 fun `setting a property sends the correct property name`() {
205 createPluginConnector("SetProperty", hasField("Property", equalTo("TestProperty")))
206 .connect { setProperty(ownIdentity, "TestProperty", "TestValue") }
210 fun `setting a property sends the correct property value`() {
211 createPluginConnector("SetProperty", hasField("Value", equalTo("TestValue")))
212 .connect { setProperty(ownIdentity, "TestProperty", "TestValue") }
216 fun `removing a property sends the correct identity id`() {
217 createPluginConnector("RemoveProperty", hasField("Identity", equalTo(ownIdentity.id)))
218 .connect { removeProperty(ownIdentity, "TestProperty") }
222 fun `removing a property sends the correct property name`() {
223 createPluginConnector("RemoveProperty", hasField("Property", equalTo("TestProperty")))
224 .connect { removeProperty(ownIdentity, "TestProperty") }
228 fun `getting trust sends correct own identity id`() {
229 createPluginConnector("GetIdentity", hasField("Truster", equalTo(ownIdentity.id)))
230 .connect { getTrust(ownIdentity, identity) }
234 fun `getting trust sends correct identity id`() {
235 createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id)))
236 .connect { getTrust(ownIdentity, identity) }
240 fun `getting trust returns correct trust values`() {
241 val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) {
245 }.connect { getTrust(ownIdentity, identity) }
246 assertThat(trust, isTrust(12, 34, 56))
250 fun `getting trust reads incorrect numbers for trust as null`() {
251 val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) {
252 put("Trust", "incorrect")
255 }.connect { getTrust(ownIdentity, identity) }
256 assertThat(trust, isTrust(null, 34, 56))
260 fun `getting trust reads incorrect numbers for score as null`() {
261 val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) {
263 put("Score", "incorrect")
265 }.connect { getTrust(ownIdentity, identity) }
266 assertThat(trust, isTrust(12, null, 56))
270 fun `getting trust reads incorrect numbers for rank as null`() {
271 val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) {
274 put("Rank", "incorrect")
275 }.connect { getTrust(ownIdentity, identity) }
276 assertThat(trust, isTrust(12, 34, null))
280 fun `setting trust sends correct own identity id`() {
281 createPluginConnector("SetTrust", hasField("Truster", equalTo(ownIdentity.id)))
282 .connect { setTrust(ownIdentity, identity, 123, "Test Trust") }
286 fun `setting trust sends correct identity id`() {
287 createPluginConnector("SetTrust", hasField("Trustee", equalTo(identity.id)))
288 .connect { setTrust(ownIdentity, identity, 123, "Test Trust") }
292 fun `setting trust sends correct trust value`() {
293 createPluginConnector("SetTrust", hasField("Value", equalTo("123")))
294 .connect { setTrust(ownIdentity, identity, 123, "Test Trust") }
298 fun `setting trust sends correct comment`() {
299 createPluginConnector("SetTrust", hasField("Comment", equalTo("Test Trust")))
300 .connect { setTrust(ownIdentity, identity, 123, "Test Trust") }
304 fun `removing trust sends correct own identity id`() {
305 createPluginConnector("RemoveTrust", hasField("Truster", equalTo(ownIdentity.id)))
306 .connect { removeTrust(ownIdentity, identity) }
310 fun `removing trust sends correct identity id`() {
311 createPluginConnector("RemoveTrust", hasField("Trustee", equalTo(identity.id)))
312 .connect { removeTrust(ownIdentity, identity) }
317 private fun <R> PluginConnector.connect(block: PluginWebOfTrustConnector.() -> R) =
318 PluginWebOfTrustConnector(this).let(block)
320 fun createPluginConnector(message: String, fieldsMatcher: Matcher<SimpleFieldSet> = IsAnything<SimpleFieldSet>(), build: SimpleFieldSetBuilder.() -> Unit = {}) =
321 object : PluginConnector {
322 override suspend fun sendRequest(pluginName: String, fields: SimpleFieldSet, data: Bucket?) =
323 if ((pluginName != wotPluginName) || (fields.get("Message") != message)) {
324 throw PluginException()
326 assertThat(fields, fieldsMatcher)
327 PluginReply(SimpleFieldSetBuilder().apply(build).get(), null)
331 private const val wotPluginName = "plugins.WebOfTrust.WebOfTrust"