2d02e3a1cd2db971066dfe0ac3f17177850659fc
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / freenet / wot / WebOfTrustConnectorTest.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 [WebOfTrustConnector].
16  */
17 class WebOfTrustConnectorTest {
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, 0, 0))
103                                 ),
104                                 allOf(
105                                                 isIdentity("id1", "nickname1", "request-uri1", empty<String>(), isEmptyMap()),
106                                                 isTrusted(ownIdentity, isTrust(null, 0, 0))
107                                 )
108                 ))
109         }
110
111         @Test
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())
122                 ))
123         }
124
125         @Test
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")))
138                 ))
139         }
140
141         @Test
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")
147                         put("Trust0", "12")
148                         put("Score0", "34")
149                         put("Rank0", "56")
150                 }.connect { loadTrustedIdentities(ownIdentity) }
151                 assertThat(trustedIdentities, contains(
152                                 allOf(
153                                                 isIdentity("id0", "nickname0", "request-uri0", empty(), isEmptyMap()),
154                                                 isTrusted(ownIdentity, isTrust(12, 34, 56))
155                                 )
156                 ))
157         }
158
159         @Test
160         fun `adding a context sends the correct own identity id`() {
161                 createPluginConnector("AddContext", hasField("Identity", equalTo(ownIdentity.id)))
162                                 .connect { addContext(ownIdentity, "TestContext") }
163         }
164
165         @Test
166         fun `adding a context sends the correct context`() {
167                 createPluginConnector("AddContext", hasField("Context", equalTo("TestContext")))
168                                 .connect { addContext(ownIdentity, "TestContext") }
169         }
170
171         @Test
172         fun `removing a context sends the correct own identity id`() {
173                 createPluginConnector("RemoveContext", hasField("Identity", equalTo(ownIdentity.id)))
174                                 .connect { removeContext(ownIdentity, "TestContext") }
175         }
176
177         @Test
178         fun `removing a context sends the correct context`() {
179                 createPluginConnector("RemoveContext", hasField("Context", equalTo("TestContext")))
180                                 .connect { removeContext(ownIdentity, "TestContext") }
181         }
182
183         @Test
184         fun `setting a property sends the correct identity id`() {
185                 createPluginConnector("SetProperty", hasField("Identity", equalTo(ownIdentity.id)))
186                                 .connect { setProperty(ownIdentity, "TestProperty", "TestValue") }
187         }
188
189         @Test
190         fun `setting a property sends the correct property name`() {
191                 createPluginConnector("SetProperty", hasField("Property", equalTo("TestProperty")))
192                                 .connect { setProperty(ownIdentity, "TestProperty", "TestValue") }
193         }
194
195         @Test
196         fun `setting a property sends the correct property value`() {
197                 createPluginConnector("SetProperty", hasField("Value", equalTo("TestValue")))
198                                 .connect { setProperty(ownIdentity, "TestProperty", "TestValue") }
199         }
200
201         @Test
202         fun `removing a property sends the correct identity id`() {
203                 createPluginConnector("RemoveProperty", hasField("Identity", equalTo(ownIdentity.id)))
204                                 .connect { removeProperty(ownIdentity, "TestProperty") }
205         }
206
207         @Test
208         fun `removing a property sends the correct property name`() {
209                 createPluginConnector("RemoveProperty", hasField("Property", equalTo("TestProperty")))
210                                 .connect { removeProperty(ownIdentity, "TestProperty") }
211         }
212
213         @Test
214         fun `getting trust sends correct own identity id`() {
215                 createPluginConnector("GetIdentity", hasField("Truster", equalTo(ownIdentity.id)))
216                                 .connect { getTrust(ownIdentity, identity) }
217         }
218
219         @Test
220         fun `getting trust sends correct identity id`() {
221                 createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id)))
222                                 .connect { getTrust(ownIdentity, identity) }
223         }
224
225         @Test
226         fun `getting trust returns correct trust values`() {
227                 val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) {
228                         put("Trust", "12")
229                         put("Score", "34")
230                         put("Rank", "56")
231                 }.connect { getTrust(ownIdentity, identity) }
232                 assertThat(trust, isTrust(12, 34, 56))
233         }
234
235         @Test
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")
239                         put("Score", "34")
240                         put("Rank", "56")
241                 }.connect { getTrust(ownIdentity, identity) }
242                 assertThat(trust, isTrust(null, 34, 56))
243         }
244
245         @Test
246         fun `getting trust reads incorrect numbers for score as null`() {
247                 val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) {
248                         put("Trust", "12")
249                         put("Score", "incorrect")
250                         put("Rank", "56")
251                 }.connect { getTrust(ownIdentity, identity) }
252                 assertThat(trust, isTrust(12, null, 56))
253         }
254
255         @Test
256         fun `getting trust reads incorrect numbers for rank as null`() {
257                 val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) {
258                         put("Trust", "12")
259                         put("Score", "34")
260                         put("Rank", "incorrect")
261                 }.connect { getTrust(ownIdentity, identity) }
262                 assertThat(trust, isTrust(12, 34, null))
263         }
264
265         @Test
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") }
269         }
270
271         @Test
272         fun `setting trust sends correct identity id`() {
273                 createPluginConnector("SetTrust", hasField("Trustee", equalTo(identity.id)))
274                                 .connect { setTrust(ownIdentity, identity, 123, "Test Trust") }
275         }
276
277         @Test
278         fun `setting trust sends correct trust value`() {
279                 createPluginConnector("SetTrust", hasField("Value", equalTo("123")))
280                                 .connect { setTrust(ownIdentity, identity, 123, "Test Trust") }
281         }
282
283         @Test
284         fun `setting trust sends correct comment`() {
285                 createPluginConnector("SetTrust", hasField("Comment", equalTo("Test Trust")))
286                                 .connect { setTrust(ownIdentity, identity, 123, "Test Trust") }
287         }
288
289         @Test
290         fun `removing trust sends correct own identity id`() {
291                 createPluginConnector("RemoveTrust", hasField("Truster", equalTo(ownIdentity.id)))
292                                 .connect { removeTrust(ownIdentity, identity) }
293         }
294
295         @Test
296         fun `removing trust sends correct identity id`() {
297                 createPluginConnector("RemoveTrust", hasField("Trustee", equalTo(identity.id)))
298                                 .connect { removeTrust(ownIdentity, identity) }
299         }
300
301 }
302
303 private fun <R> PluginConnector.connect(block: WebOfTrustConnector.() -> R) =
304                 WebOfTrustConnector(this).let(block)
305
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()
311                                         } else {
312                                                 assertThat(fields, fieldsMatcher)
313                                                 PluginReply(SimpleFieldSetBuilder().apply(build).get(), null)
314                                         }
315                 }
316
317 private const val wotPluginName = "plugins.WebOfTrust.WebOfTrust"