1 package net.pterodactylus.sone.freenet.wot
3 import org.hamcrest.MatcherAssert.assertThat
4 import org.hamcrest.Matchers.containsInAnyOrder
5 import org.hamcrest.Matchers.empty
8 class NoNegativeIdentityFilterTest {
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))
15 val filteredIdentities = filter.filter(allIdentities)
16 assertThat(filteredIdentities[ownIdentity1]!!, containsInAnyOrder(identityA, identityB))
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))
24 val filteredIdentities = filter.filter(allIdentities)
25 assertThat(filteredIdentities[ownIdentity1]!!, containsInAnyOrder(identityA, identityB))
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))
34 val filteredIdentities = filter.filter(allIdentities)
35 assertThat(filteredIdentities[ownIdentity1]!!, containsInAnyOrder(identityA, identityB))
36 assertThat(filteredIdentities[ownIdentity2]!!, containsInAnyOrder(identityA, identityC))
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))
45 val filteredIdentities = filter.filter(allIdentities)
46 assertThat(filteredIdentities[ownIdentity1]!!, containsInAnyOrder(identityA, identityB))
47 assertThat(filteredIdentities[ownIdentity2]!!, containsInAnyOrder(identityA, identityC))
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))
56 val filteredIdentities = filter.filter(allIdentities)
57 assertThat(filteredIdentities[ownIdentity1]!!, containsInAnyOrder(identityA))
58 assertThat(filteredIdentities[ownIdentity2]!!, empty())
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))
67 val filteredIdentities = filter.filter(allIdentities)
68 assertThat(filteredIdentities[ownIdentity1]!!, containsInAnyOrder(identityA))
69 assertThat(filteredIdentities[ownIdentity2]!!, empty())
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")
81 private fun OwnIdentity.trust(vararg identityTrust: Pair<Identity, Trust>) =
82 this to identityTrust.map { (identity, trust) -> identity.copy().setTrust(this, trust) }.toSet()
84 private fun Identity.copy() = DefaultIdentity(id, nickname, requestUri).apply {
85 contexts = this@copy.contexts
86 properties = this@copy.properties