1af177fb937588e5be0e36612435acf76b76c1d5
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / freenet / wot / PluginWebOfTrustConnectorTest.kt
1 package net.pterodactylus.sone.freenet.wot
2
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.*
8 import org.hamcrest.*
9 import org.hamcrest.MatcherAssert.*
10 import org.hamcrest.Matchers.*
11 import org.hamcrest.core.*
12 import kotlin.test.*
13
14 /**
15  * Unit test for [PluginWebOfTrustConnector].
16  */
17 class PluginWebOfTrustConnectorTest {
18
19         private val ownIdentity = DefaultOwnIdentity("id", "nickname", "requestUri", "insertUri")
20         private val identity = DefaultIdentity("id-a", "alpha", "url://alpha")
21
22         @Test
23         fun `wot plugin can be pinged`() {
24                 createPluginConnector("Ping")
25                                 .connect { ping() }
26         }
27
28         @Test
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"))
49                 ))
50         }
51
52         @Test
53         fun `trusted identities are requested with correct own identity`() {
54                 createPluginConnector("GetIdentitiesByScore", hasField("Truster", equalTo("id")))
55                                 .connect { loadTrustedIdentities(ownIdentity) }
56         }
57
58         @Test
59         fun `trusted identities are requested with correct selection parameter`() {
60                 createPluginConnector("GetIdentitiesByScore", hasField("Selection", equalTo("+")))
61                                 .connect { loadTrustedIdentities(ownIdentity) }
62         }
63
64         @Test
65         fun `trusted identities are requested with empty context if null context requested`() {
66                 createPluginConnector("GetIdentitiesByScore", hasField("Context", equalTo("")))
67                                 .connect { loadTrustedIdentities(ownIdentity) }
68         }
69
70         @Test
71         fun `trusted identities are requested with context if context requested`() {
72                 createPluginConnector("GetIdentitiesByScore", hasField("Context", equalTo("TestContext")))
73                                 .connect { loadTrustedIdentities(ownIdentity, "TestContext") }
74         }
75
76         @Test
77         fun `trusted identities are requested with trust values`() {
78                 createPluginConnector("GetIdentitiesByScore", hasField("WantTrustValues", equalTo("true")))
79                                 .connect { loadTrustedIdentities(ownIdentity) }
80         }
81
82         @Test
83         fun `empty list of trusted identities is returned correctly`() {
84                 val trustedIdentities = createPluginConnector("GetIdentitiesByScore")
85                                 .connect { loadTrustedIdentities(ownIdentity) }
86                 assertThat(trustedIdentities, empty())
87         }
88
89         @Test
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(
100                                 allOf(
101                                                 isIdentity("id0", "nickname0", "request-uri0", empty<String>(), isEmptyMap()),
102                                                 isTrusted(ownIdentity, isTrust(null, null, null))
103                                 ),
104                                 allOf(
105                                                 isIdentity("id1", "nickname1", "request-uri1", empty<String>(), isEmptyMap()),
106                                                 isTrusted(ownIdentity, isTrust(null, null, null))
107                                 )
108                 ))
109         }
110
111         @Test
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(
118                                 allOf(
119                                                 isIdentity("id0", null, "request-uri0", empty<String>(), isEmptyMap()),
120                                                 isTrusted(ownIdentity, isTrust(null, null, null))
121                                 )
122                 ))
123         }
124
125         @Test
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())
136                 ))
137         }
138
139         @Test
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")))
152                 ))
153         }
154
155         @Test
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")
161                         put("Trust0", "12")
162                         put("Score0", "34")
163                         put("Rank0", "56")
164                 }.connect { loadTrustedIdentities(ownIdentity) }
165                 assertThat(trustedIdentities, contains(
166                                 allOf(
167                                                 isIdentity("id0", "nickname0", "request-uri0", empty(), isEmptyMap()),
168                                                 isTrusted(ownIdentity, isTrust(12, 34, 56))
169                                 )
170                 ))
171         }
172
173         @Test
174         fun `adding a context sends the correct own identity id`() {
175                 createPluginConnector("AddContext", hasField("Identity", equalTo(ownIdentity.id)))
176                                 .connect { addContext(ownIdentity, "TestContext") }
177         }
178
179         @Test
180         fun `adding a context sends the correct context`() {
181                 createPluginConnector("AddContext", hasField("Context", equalTo("TestContext")))
182                                 .connect { addContext(ownIdentity, "TestContext") }
183         }
184
185         @Test
186         fun `removing a context sends the correct own identity id`() {
187                 createPluginConnector("RemoveContext", hasField("Identity", equalTo(ownIdentity.id)))
188                                 .connect { removeContext(ownIdentity, "TestContext") }
189         }
190
191         @Test
192         fun `removing a context sends the correct context`() {
193                 createPluginConnector("RemoveContext", hasField("Context", equalTo("TestContext")))
194                                 .connect { removeContext(ownIdentity, "TestContext") }
195         }
196
197         @Test
198         fun `setting a property sends the correct identity id`() {
199                 createPluginConnector("SetProperty", hasField("Identity", equalTo(ownIdentity.id)))
200                                 .connect { setProperty(ownIdentity, "TestProperty", "TestValue") }
201         }
202
203         @Test
204         fun `setting a property sends the correct property name`() {
205                 createPluginConnector("SetProperty", hasField("Property", equalTo("TestProperty")))
206                                 .connect { setProperty(ownIdentity, "TestProperty", "TestValue") }
207         }
208
209         @Test
210         fun `setting a property sends the correct property value`() {
211                 createPluginConnector("SetProperty", hasField("Value", equalTo("TestValue")))
212                                 .connect { setProperty(ownIdentity, "TestProperty", "TestValue") }
213         }
214
215         @Test
216         fun `removing a property sends the correct identity id`() {
217                 createPluginConnector("RemoveProperty", hasField("Identity", equalTo(ownIdentity.id)))
218                                 .connect { removeProperty(ownIdentity, "TestProperty") }
219         }
220
221         @Test
222         fun `removing a property sends the correct property name`() {
223                 createPluginConnector("RemoveProperty", hasField("Property", equalTo("TestProperty")))
224                                 .connect { removeProperty(ownIdentity, "TestProperty") }
225         }
226
227         @Test
228         fun `getting trust sends correct own identity id`() {
229                 createPluginConnector("GetIdentity", hasField("Truster", equalTo(ownIdentity.id)))
230                                 .connect { getTrust(ownIdentity, identity) }
231         }
232
233         @Test
234         fun `getting trust sends correct identity id`() {
235                 createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id)))
236                                 .connect { getTrust(ownIdentity, identity) }
237         }
238
239         @Test
240         fun `getting trust returns correct trust values`() {
241                 val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) {
242                         put("Trust", "12")
243                         put("Score", "34")
244                         put("Rank", "56")
245                 }.connect { getTrust(ownIdentity, identity) }
246                 assertThat(trust, isTrust(12, 34, 56))
247         }
248
249         @Test
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")
253                         put("Score", "34")
254                         put("Rank", "56")
255                 }.connect { getTrust(ownIdentity, identity) }
256                 assertThat(trust, isTrust(null, 34, 56))
257         }
258
259         @Test
260         fun `getting trust reads incorrect numbers for score as null`() {
261                 val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) {
262                         put("Trust", "12")
263                         put("Score", "incorrect")
264                         put("Rank", "56")
265                 }.connect { getTrust(ownIdentity, identity) }
266                 assertThat(trust, isTrust(12, null, 56))
267         }
268
269         @Test
270         fun `getting trust reads incorrect numbers for rank as null`() {
271                 val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) {
272                         put("Trust", "12")
273                         put("Score", "34")
274                         put("Rank", "incorrect")
275                 }.connect { getTrust(ownIdentity, identity) }
276                 assertThat(trust, isTrust(12, 34, null))
277         }
278
279         @Test
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") }
283         }
284
285         @Test
286         fun `setting trust sends correct identity id`() {
287                 createPluginConnector("SetTrust", hasField("Trustee", equalTo(identity.id)))
288                                 .connect { setTrust(ownIdentity, identity, 123, "Test Trust") }
289         }
290
291         @Test
292         fun `setting trust sends correct trust value`() {
293                 createPluginConnector("SetTrust", hasField("Value", equalTo("123")))
294                                 .connect { setTrust(ownIdentity, identity, 123, "Test Trust") }
295         }
296
297         @Test
298         fun `setting trust sends correct comment`() {
299                 createPluginConnector("SetTrust", hasField("Comment", equalTo("Test Trust")))
300                                 .connect { setTrust(ownIdentity, identity, 123, "Test Trust") }
301         }
302
303         @Test
304         fun `removing trust sends correct own identity id`() {
305                 createPluginConnector("RemoveTrust", hasField("Truster", equalTo(ownIdentity.id)))
306                                 .connect { removeTrust(ownIdentity, identity) }
307         }
308
309         @Test
310         fun `removing trust sends correct identity id`() {
311                 createPluginConnector("RemoveTrust", hasField("Trustee", equalTo(identity.id)))
312                                 .connect { removeTrust(ownIdentity, identity) }
313         }
314
315 }
316
317 private fun <R> PluginConnector.connect(block: PluginWebOfTrustConnector.() -> R) =
318                 PluginWebOfTrustConnector(this).let(block)
319
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()
325                                         } else {
326                                                 assertThat(fields, fieldsMatcher)
327                                                 PluginReply(SimpleFieldSetBuilder().apply(build).get(), null)
328                                         }
329                 }
330
331 private const val wotPluginName = "plugins.WebOfTrust.WebOfTrust"