🚧 Add no-negative identity filter
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / freenet / wot / NoNegativeIdentityFilterTest.kt
1 package net.pterodactylus.sone.freenet.wot
2
3 import org.hamcrest.MatcherAssert.assertThat
4 import org.hamcrest.Matchers.containsInAnyOrder
5 import org.hamcrest.Matchers.empty
6 import org.junit.Test
7
8 class NoNegativeIdentityFilterTest {
9
10         @Test
11         fun `filter retains all identities with an explicit trust larger than or equal to 0`() {
12                 val allIdentities = mapOf(
13                         ownIdentity1.trust(identityA to trustExplicitely(50), identityB to trustExplicitely(0), identityC to trustExplicitely(-50))
14                 )
15                 val filteredIdentities = filter.filter(allIdentities)
16                 assertThat(filteredIdentities[ownIdentity1]!!, containsInAnyOrder(identityA, identityB))
17         }
18
19         @Test
20         fun `filter retains all identities with an implicit trust larger than or equal to 0`() {
21                 val allIdentities = mapOf(
22                         ownIdentity1.trust(identityA to trustImplicitely(50), identityB to trustImplicitely(0), identityC to trustImplicitely(-50))
23                 )
24                 val filteredIdentities = filter.filter(allIdentities)
25                 assertThat(filteredIdentities[ownIdentity1]!!, containsInAnyOrder(identityA, identityB))
26         }
27
28         @Test
29         fun `filter retains all identities with an explicit trust larger than or equal to 0 from all local identities that know that remote identity`() {
30                 val allIdentities = mapOf(
31                         ownIdentity1.trust(identityA to trustExplicitely(50), identityB to trustExplicitely(0)),
32                         ownIdentity2.trust(identityA to trustExplicitely(50), identityC to trustExplicitely(0))
33                 )
34                 val filteredIdentities = filter.filter(allIdentities)
35                 assertThat(filteredIdentities[ownIdentity1]!!, containsInAnyOrder(identityA, identityB))
36                 assertThat(filteredIdentities[ownIdentity2]!!, containsInAnyOrder(identityA, identityC))
37         }
38
39         @Test
40         fun `filter retains all identities with an implicit trust larger than or equal to 0 from all local identities that know that remote identity`() {
41                 val allIdentities = mapOf(
42                         ownIdentity1.trust(identityA to trustImplicitely(50), identityB to trustImplicitely(0)),
43                         ownIdentity2.trust(identityA to trustImplicitely(50), identityC to trustImplicitely(0))
44                 )
45                 val filteredIdentities = filter.filter(allIdentities)
46                 assertThat(filteredIdentities[ownIdentity1]!!, containsInAnyOrder(identityA, identityB))
47                 assertThat(filteredIdentities[ownIdentity2]!!, containsInAnyOrder(identityA, identityC))
48         }
49
50         @Test
51         fun `strict filter removes all identities that have an explicit negative value for any of the local identities`() {
52                 val allIdentities = mapOf(
53                         ownIdentity1.trust(identityA to trustExplicitely(50), identityB to trustExplicitely(50)),
54                         ownIdentity2.trust(identityB to trustExplicitely(-50))
55                 )
56                 val filteredIdentities = filter.filter(allIdentities)
57                 assertThat(filteredIdentities[ownIdentity1]!!, containsInAnyOrder(identityA))
58                 assertThat(filteredIdentities[ownIdentity2]!!, empty())
59         }
60
61         @Test
62         fun `strict filter removes all identities that have an implicit negative value for any of the local identities`() {
63                 val allIdentities = mapOf(
64                         ownIdentity1.trust(identityA to trustExplicitely(50), identityB to trustExplicitely(50)),
65                         ownIdentity2.trust(identityB to trustImplicitely(-50))
66                 )
67                 val filteredIdentities = filter.filter(allIdentities)
68                 assertThat(filteredIdentities[ownIdentity1]!!, containsInAnyOrder(identityA))
69                 assertThat(filteredIdentities[ownIdentity2]!!, empty())
70         }
71
72         private val filter = NoNegativeIdentityFilter()
73         private val ownIdentity1 = createOwnIdentity("1")
74         private val ownIdentity2 = createOwnIdentity("2")
75         private val identityA = createIdentity("A")
76         private val identityB = createIdentity("B")
77         private val identityC = createIdentity("C")
78
79 }
80
81 private fun OwnIdentity.trust(vararg identityTrust: Pair<Identity, Trust>) =
82         this to identityTrust.map { (identity, trust) -> identity.copy().setTrust(this, trust) }.toSet()
83
84 private fun Identity.copy() = DefaultIdentity(id, nickname, requestUri).apply {
85         contexts = this@copy.contexts
86         properties = this@copy.properties
87 }