dd2cc7acc4f85fd6ce2cf92e9399a672b646ea23
[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         @Test
20         fun `wot plugin can be pinged`() {
21                 val pluginConnector = createPluginConnector("Ping")
22                 val connector = WebOfTrustConnector(pluginConnector)
23                 connector.ping()
24         }
25
26         @Test
27         fun `own identities are returned correctly`() {
28                 val pluginConnector = createPluginConnector("GetOwnIdentities") {
29                         put("Identity0", "id-0")
30                         put("RequestURI0", "request-uri-0")
31                         put("InsertURI0", "insert-uri-0")
32                         put("Nickname0", "nickname-0")
33                         put("Contexts0.Context0", "id-0-context-0")
34                         put("Properties0.Property0.Name", "id-0-property-0-name")
35                         put("Properties0.Property0.Value", "id-0-property-0-value")
36                         put("Identity1", "id-1")
37                         put("RequestURI1", "request-uri-1")
38                         put("InsertURI1", "insert-uri-1")
39                         put("Nickname1", "nickname-1")
40                         put("Contexts1.Context0", "id-1-context-0")
41                         put("Properties1.Property0.Name", "id-1-property-0-name")
42                         put("Properties1.Property0.Value", "id-1-property-0-value")
43                 }
44                 val connector = WebOfTrustConnector(pluginConnector)
45                 val ownIdentities = connector.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                 val pluginConnector = createPluginConnector("GetIdentitiesByScore", hasField("Truster", equalTo("id")))
55                 val ownIdentity = DefaultOwnIdentity("id", "nickname", "requestUri", "insertUri")
56                 val connector = WebOfTrustConnector(pluginConnector)
57                 connector.loadTrustedIdentities(ownIdentity)
58         }
59
60         @Test
61         fun `trusted identities are requested with correct selection parameter`() {
62                 val pluginConnector = createPluginConnector("GetIdentitiesByScore", hasField("Selection", equalTo("+")))
63                 val ownIdentity = DefaultOwnIdentity("id", "nickname", "requestUri", "insertUri")
64                 val connector = WebOfTrustConnector(pluginConnector)
65                 connector.loadTrustedIdentities(ownIdentity)
66         }
67
68         @Test
69         fun `trusted identities are requested with empty context if null context requested`() {
70                 val pluginConnector = createPluginConnector("GetIdentitiesByScore", hasField("Context", equalTo("")))
71                 val ownIdentity = DefaultOwnIdentity("id", "nickname", "requestUri", "insertUri")
72                 val connector = WebOfTrustConnector(pluginConnector)
73                 connector.loadTrustedIdentities(ownIdentity)
74         }
75
76         @Test
77         fun `trusted identities are requested with context if context requested`() {
78                 val pluginConnector = createPluginConnector("GetIdentitiesByScore", hasField("Context", equalTo("TestContext")))
79                 val ownIdentity = DefaultOwnIdentity("id", "nickname", "requestUri", "insertUri")
80                 val connector = WebOfTrustConnector(pluginConnector)
81                 connector.loadTrustedIdentities(ownIdentity, "TestContext")
82         }
83
84         @Test
85         fun `trusted identities are requested with trust values`() {
86                 val pluginConnector = createPluginConnector("GetIdentitiesByScore", hasField("WantTrustValues", equalTo("true")))
87                 val ownIdentity = DefaultOwnIdentity("id", "nickname", "requestUri", "insertUri")
88                 val connector = WebOfTrustConnector(pluginConnector)
89                 val trustedIdentities = connector.loadTrustedIdentities(ownIdentity)
90         }
91
92         @Test
93         fun `empty list of trusted identities is returned correctly`() {
94                 val pluginConnector = createPluginConnector("GetIdentitiesByScore")
95                 val ownIdentity = DefaultOwnIdentity("id", "nickname", "requestUri", "insertUri")
96                 val connector = WebOfTrustConnector(pluginConnector)
97                 val trustedIdentities = connector.loadTrustedIdentities(ownIdentity)
98                 assertThat(trustedIdentities, empty())
99         }
100
101         @Test
102         fun `trusted identities without context, properties, or trust value are returned correctly`() {
103                 val pluginConnector = createPluginConnector("GetIdentitiesByScore") {
104                         put("Identity0", "id0")
105                         put("Nickname0", "nickname0")
106                         put("RequestURI0", "request-uri0")
107                         put("Identity1", "id1")
108                         put("Nickname1", "nickname1")
109                         put("RequestURI1", "request-uri1")
110                 }
111                 val ownIdentity = DefaultOwnIdentity("id", "nickname", "requestUri", "insertUri")
112                 val connector = WebOfTrustConnector(pluginConnector)
113                 val trustedIdentities = connector.loadTrustedIdentities(ownIdentity)
114                 assertThat(trustedIdentities, contains(
115                                 allOf(
116                                                 isIdentity("id0", "nickname0", "request-uri0", empty<String>(), isEmptyMap()),
117                                                 isTrusted(ownIdentity, isTrust(null, 0, 0))
118                                 ),
119                                 allOf(
120                                                 isIdentity("id1", "nickname1", "request-uri1", empty<String>(), isEmptyMap()),
121                                                 isTrusted(ownIdentity, isTrust(null, 0, 0))
122                                 )
123                 ))
124         }
125
126         @Test
127         fun `trusted identity with contexts is returned correctly`() {
128                 val pluginConnector = createPluginConnector("GetIdentitiesByScore") {
129                         put("Identity0", "id0")
130                         put("Nickname0", "nickname0")
131                         put("RequestURI0", "request-uri0")
132                         put("Contexts0.Context0", "Context0")
133                         put("Contexts0.Context1", "Context1")
134                 }
135                 val ownIdentity = DefaultOwnIdentity("id", "nickname", "requestUri", "insertUri")
136                 val connector = WebOfTrustConnector(pluginConnector)
137                 val trustedIdentities = connector.loadTrustedIdentities(ownIdentity)
138                 assertThat(trustedIdentities, contains(
139                                 isIdentity("id0", "nickname0", "request-uri0", containsInAnyOrder("Context0", "Context1"), isEmptyMap())
140                 ))
141         }
142
143         @Test
144         fun `trusted identity with properties is returned correctly`() {
145                 val pluginConnector = createPluginConnector("GetIdentitiesByScore") {
146                         put("Identity0", "id0")
147                         put("Nickname0", "nickname0")
148                         put("RequestURI0", "request-uri0")
149                         put("Properties0.Property0.Name", "foo")
150                         put("Properties0.Property0.Value", "bar")
151                         put("Properties0.Property1.Name", "baz")
152                         put("Properties0.Property1.Value", "quo")
153                 }
154                 val ownIdentity = DefaultOwnIdentity("id", "nickname", "requestUri", "insertUri")
155                 val connector = WebOfTrustConnector(pluginConnector)
156                 val trustedIdentities = connector.loadTrustedIdentities(ownIdentity)
157                 assertThat(trustedIdentities, contains(
158                                 isIdentity("id0", "nickname0", "request-uri0", empty(), allOf(hasEntry("foo", "bar"), hasEntry("baz", "quo")))
159                 ))
160         }
161
162         @Test
163         fun `trusted identity with trust value is returned correctly`() {
164                 val pluginConnector = createPluginConnector("GetIdentitiesByScore") {
165                         put("Identity0", "id0")
166                         put("Nickname0", "nickname0")
167                         put("RequestURI0", "request-uri0")
168                         put("Trust0", "12")
169                         put("Score0", "34")
170                         put("Rank0", "56")
171                 }
172                 val ownIdentity = DefaultOwnIdentity("id", "nickname", "requestUri", "insertUri")
173                 val connector = WebOfTrustConnector(pluginConnector)
174                 val trustedIdentities = connector.loadTrustedIdentities(ownIdentity)
175                 assertThat(trustedIdentities, contains(
176                                 allOf(
177                                                 isIdentity("id0", "nickname0", "request-uri0", empty(), isEmptyMap()),
178                                                 isTrusted(ownIdentity, isTrust(12, 34, 56))
179                                 )
180                 ))
181         }
182
183 }
184
185 fun createPluginConnector(message: String, fieldsMatcher: Matcher<SimpleFieldSet> = IsAnything<SimpleFieldSet>(), build: SimpleFieldSetBuilder.() -> Unit = {}) =
186                 object : PluginConnector {
187                         override fun sendRequest(pluginName: String, identifier: String, fields: SimpleFieldSet, data: Bucket?) =
188                                         if ((pluginName != wotPluginName) || (fields.get("Message") != message)) {
189                                                 throw PluginException()
190                                         } else {
191                                                 assertThat(fields, fieldsMatcher)
192                                                 PluginReply(SimpleFieldSetBuilder().apply(build).get(), null)
193                                         }
194                 }
195
196 private const val wotPluginName = "plugins.WebOfTrust.WebOfTrust"