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 [WebOfTrustConnector].
17 class WebOfTrustConnectorTest {
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 val pluginConnector = createPluginConnector("GetIdentitiesByScore", hasField("Truster", equalTo("id")))
55 .connect { loadTrustedIdentities(ownIdentity) }
59 fun `trusted identities are requested with correct selection parameter`() {
60 val pluginConnector = createPluginConnector("GetIdentitiesByScore", hasField("Selection", equalTo("+")))
61 .connect { loadTrustedIdentities(ownIdentity) }
65 fun `trusted identities are requested with empty context if null context requested`() {
66 val pluginConnector = createPluginConnector("GetIdentitiesByScore", hasField("Context", equalTo("")))
67 .connect { loadTrustedIdentities(ownIdentity) }
71 fun `trusted identities are requested with context if context requested`() {
72 val pluginConnector = 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, 0, 0))
105 isIdentity("id1", "nickname1", "request-uri1", empty<String>(), isEmptyMap()),
106 isTrusted(ownIdentity, isTrust(null, 0, 0))
112 fun `trusted identity with contexts is returned correctly`() {
113 val trustedIdentities = createPluginConnector("GetIdentitiesByScore") {
114 put("Identity0", "id0")
115 put("Nickname0", "nickname0")
116 put("RequestURI0", "request-uri0")
117 put("Contexts0.Context0", "Context0")
118 put("Contexts0.Context1", "Context1")
119 }.connect { loadTrustedIdentities(ownIdentity) }
120 assertThat(trustedIdentities, contains(
121 isIdentity("id0", "nickname0", "request-uri0", containsInAnyOrder("Context0", "Context1"), isEmptyMap())
126 fun `trusted identity with properties is returned correctly`() {
127 val trustedIdentities = createPluginConnector("GetIdentitiesByScore") {
128 put("Identity0", "id0")
129 put("Nickname0", "nickname0")
130 put("RequestURI0", "request-uri0")
131 put("Properties0.Property0.Name", "foo")
132 put("Properties0.Property0.Value", "bar")
133 put("Properties0.Property1.Name", "baz")
134 put("Properties0.Property1.Value", "quo")
135 }.connect { loadTrustedIdentities(ownIdentity) }
136 assertThat(trustedIdentities, contains(
137 isIdentity("id0", "nickname0", "request-uri0", empty(), allOf(hasEntry("foo", "bar"), hasEntry("baz", "quo")))
142 fun `trusted identity with trust value is returned correctly`() {
143 val trustedIdentities = createPluginConnector("GetIdentitiesByScore") {
144 put("Identity0", "id0")
145 put("Nickname0", "nickname0")
146 put("RequestURI0", "request-uri0")
150 }.connect { loadTrustedIdentities(ownIdentity) }
151 assertThat(trustedIdentities, contains(
153 isIdentity("id0", "nickname0", "request-uri0", empty(), isEmptyMap()),
154 isTrusted(ownIdentity, isTrust(12, 34, 56))
160 fun `adding a context sends the correct own identity id`() {
161 createPluginConnector("AddContext", hasField("Identity", equalTo(ownIdentity.id)))
162 .connect { addContext(ownIdentity, "TestContext") }
166 fun `adding a context sends the correct context`() {
167 createPluginConnector("AddContext", hasField("Context", equalTo("TestContext")))
168 .connect { addContext(ownIdentity, "TestContext") }
172 fun `removing a context sends the correct own identity id`() {
173 createPluginConnector("RemoveContext", hasField("Identity", equalTo(ownIdentity.id)))
174 .connect { removeContext(ownIdentity, "TestContext") }
178 fun `removing a context sends the correct context`() {
179 createPluginConnector("RemoveContext", hasField("Context", equalTo("TestContext")))
180 .connect { removeContext(ownIdentity, "TestContext") }
184 fun `setting a property sends the correct identity id`() {
185 createPluginConnector("SetProperty", hasField("Identity", equalTo(ownIdentity.id)))
186 .connect { setProperty(ownIdentity, "TestProperty", "TestValue") }
190 fun `setting a property sends the correct property name`() {
191 createPluginConnector("SetProperty", hasField("Property", equalTo("TestProperty")))
192 .connect { setProperty(ownIdentity, "TestProperty", "TestValue") }
196 fun `setting a property sends the correct property value`() {
197 createPluginConnector("SetProperty", hasField("Value", equalTo("TestValue")))
198 .connect { setProperty(ownIdentity, "TestProperty", "TestValue") }
202 fun `removing a property sends the correct identity id`() {
203 createPluginConnector("RemoveProperty", hasField("Identity", equalTo(ownIdentity.id)))
204 .connect { removeProperty(ownIdentity, "TestProperty") }
208 fun `removing a property sends the correct property name`() {
209 createPluginConnector("RemoveProperty", hasField("Property", equalTo("TestProperty")))
210 .connect { removeProperty(ownIdentity, "TestProperty") }
214 fun `getting trust sends correct own identity id`() {
215 createPluginConnector("GetIdentity", hasField("Truster", equalTo(ownIdentity.id)))
216 .connect { getTrust(ownIdentity, identity) }
220 fun `getting trust sends correct identity id`() {
221 createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id)))
222 .connect { getTrust(ownIdentity, identity) }
226 fun `getting trust returns correct trust values`() {
227 val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) {
231 }.connect { getTrust(ownIdentity, identity) }
232 assertThat(trust, isTrust(12, 34, 56))
236 fun `getting trust reads incorrect numbers for trust as null`() {
237 val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) {
238 put("Trust", "incorrect")
241 }.connect { getTrust(ownIdentity, identity) }
242 assertThat(trust, isTrust(null, 34, 56))
246 fun `getting trust reads incorrect numbers for score as null`() {
247 val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) {
249 put("Score", "incorrect")
251 }.connect { getTrust(ownIdentity, identity) }
252 assertThat(trust, isTrust(12, null, 56))
256 fun `getting trust reads incorrect numbers for rank as null`() {
257 val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) {
260 put("Rank", "incorrect")
261 }.connect { getTrust(ownIdentity, identity) }
262 assertThat(trust, isTrust(12, 34, null))
266 fun `setting trust sends correct own identity id`() {
267 createPluginConnector("SetTrust", hasField("Truster", equalTo(ownIdentity.id)))
268 .connect { setTrust(ownIdentity, identity, 123, "Test Trust") }
272 fun `setting trust sends correct identity id`() {
273 createPluginConnector("SetTrust", hasField("Trustee", equalTo(identity.id)))
274 .connect { setTrust(ownIdentity, identity, 123, "Test Trust") }
278 fun `setting trust sends correct trust value`() {
279 createPluginConnector("SetTrust", hasField("Value", equalTo("123")))
280 .connect { setTrust(ownIdentity, identity, 123, "Test Trust") }
284 fun `setting trust sends correct comment`() {
285 createPluginConnector("SetTrust", hasField("Comment", equalTo("Test Trust")))
286 .connect { setTrust(ownIdentity, identity, 123, "Test Trust") }
290 fun `removing trust sends correct own identity id`() {
291 createPluginConnector("RemoveTrust", hasField("Truster", equalTo(ownIdentity.id)))
292 .connect { removeTrust(ownIdentity, identity) }
296 fun `removing trust sends correct identity id`() {
297 createPluginConnector("RemoveTrust", hasField("Trustee", equalTo(identity.id)))
298 .connect { removeTrust(ownIdentity, identity) }
303 private fun <R> PluginConnector.connect(block: WebOfTrustConnector.() -> R) =
304 WebOfTrustConnector(this).let(block)
306 fun createPluginConnector(message: String, fieldsMatcher: Matcher<SimpleFieldSet> = IsAnything<SimpleFieldSet>(), build: SimpleFieldSetBuilder.() -> Unit = {}) =
307 object : PluginConnector {
308 override fun sendRequest(pluginName: String, identifier: String, fields: SimpleFieldSet, data: Bucket?) =
309 if ((pluginName != wotPluginName) || (fields.get("Message") != message)) {
310 throw PluginException()
312 assertThat(fields, fieldsMatcher)
313 PluginReply(SimpleFieldSetBuilder().apply(build).get(), null)
317 private const val wotPluginName = "plugins.WebOfTrust.WebOfTrust"