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 {
20 fun `wot plugin can be pinged`() {
21 val pluginConnector = createPluginConnector("Ping")
22 val connector = WebOfTrustConnector(pluginConnector)
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")
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"))
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)
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)
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)
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")
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)
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())
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")
111 val ownIdentity = DefaultOwnIdentity("id", "nickname", "requestUri", "insertUri")
112 val connector = WebOfTrustConnector(pluginConnector)
113 val trustedIdentities = connector.loadTrustedIdentities(ownIdentity)
114 assertThat(trustedIdentities, contains(
116 isIdentity("id0", "nickname0", "request-uri0", empty<String>(), isEmptyMap()),
117 isTrusted(ownIdentity, isTrust(null, 0, 0))
120 isIdentity("id1", "nickname1", "request-uri1", empty<String>(), isEmptyMap()),
121 isTrusted(ownIdentity, isTrust(null, 0, 0))
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")
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())
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")
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")))
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")
172 val ownIdentity = DefaultOwnIdentity("id", "nickname", "requestUri", "insertUri")
173 val connector = WebOfTrustConnector(pluginConnector)
174 val trustedIdentities = connector.loadTrustedIdentities(ownIdentity)
175 assertThat(trustedIdentities, contains(
177 isIdentity("id0", "nickname0", "request-uri0", empty(), isEmptyMap()),
178 isTrusted(ownIdentity, isTrust(12, 34, 56))
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()
191 assertThat(fields, fieldsMatcher)
192 PluginReply(SimpleFieldSetBuilder().apply(build).get(), null)
196 private const val wotPluginName = "plugins.WebOfTrust.WebOfTrust"