X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2Fwot%2FNoNegativeIdentityFilterTest.kt;fp=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2Fwot%2FNoNegativeIdentityFilterTest.kt;h=05c627cffd9539c9bf9610c43273c935ef0ad95d;hp=0000000000000000000000000000000000000000;hb=5247fb4b48329d1525e7b2537a81c29ddfb102fe;hpb=a5c00f1b023ee87a8d3c2f2ece6b955c87e62afb diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/NoNegativeIdentityFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/NoNegativeIdentityFilterTest.kt new file mode 100644 index 0000000..05c627c --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/NoNegativeIdentityFilterTest.kt @@ -0,0 +1,87 @@ +package net.pterodactylus.sone.freenet.wot + +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.containsInAnyOrder +import org.hamcrest.Matchers.empty +import org.junit.Test + +class NoNegativeIdentityFilterTest { + + @Test + fun `filter retains all identities with an explicit trust larger than or equal to 0`() { + val allIdentities = mapOf( + ownIdentity1.trust(identityA to trustExplicitely(50), identityB to trustExplicitely(0), identityC to trustExplicitely(-50)) + ) + val filteredIdentities = filter.filter(allIdentities) + assertThat(filteredIdentities[ownIdentity1]!!, containsInAnyOrder(identityA, identityB)) + } + + @Test + fun `filter retains all identities with an implicit trust larger than or equal to 0`() { + val allIdentities = mapOf( + ownIdentity1.trust(identityA to trustImplicitely(50), identityB to trustImplicitely(0), identityC to trustImplicitely(-50)) + ) + val filteredIdentities = filter.filter(allIdentities) + assertThat(filteredIdentities[ownIdentity1]!!, containsInAnyOrder(identityA, identityB)) + } + + @Test + fun `filter retains all identities with an explicit trust larger than or equal to 0 from all local identities that know that remote identity`() { + val allIdentities = mapOf( + ownIdentity1.trust(identityA to trustExplicitely(50), identityB to trustExplicitely(0)), + ownIdentity2.trust(identityA to trustExplicitely(50), identityC to trustExplicitely(0)) + ) + val filteredIdentities = filter.filter(allIdentities) + assertThat(filteredIdentities[ownIdentity1]!!, containsInAnyOrder(identityA, identityB)) + assertThat(filteredIdentities[ownIdentity2]!!, containsInAnyOrder(identityA, identityC)) + } + + @Test + fun `filter retains all identities with an implicit trust larger than or equal to 0 from all local identities that know that remote identity`() { + val allIdentities = mapOf( + ownIdentity1.trust(identityA to trustImplicitely(50), identityB to trustImplicitely(0)), + ownIdentity2.trust(identityA to trustImplicitely(50), identityC to trustImplicitely(0)) + ) + val filteredIdentities = filter.filter(allIdentities) + assertThat(filteredIdentities[ownIdentity1]!!, containsInAnyOrder(identityA, identityB)) + assertThat(filteredIdentities[ownIdentity2]!!, containsInAnyOrder(identityA, identityC)) + } + + @Test + fun `strict filter removes all identities that have an explicit negative value for any of the local identities`() { + val allIdentities = mapOf( + ownIdentity1.trust(identityA to trustExplicitely(50), identityB to trustExplicitely(50)), + ownIdentity2.trust(identityB to trustExplicitely(-50)) + ) + val filteredIdentities = filter.filter(allIdentities) + assertThat(filteredIdentities[ownIdentity1]!!, containsInAnyOrder(identityA)) + assertThat(filteredIdentities[ownIdentity2]!!, empty()) + } + + @Test + fun `strict filter removes all identities that have an implicit negative value for any of the local identities`() { + val allIdentities = mapOf( + ownIdentity1.trust(identityA to trustExplicitely(50), identityB to trustExplicitely(50)), + ownIdentity2.trust(identityB to trustImplicitely(-50)) + ) + val filteredIdentities = filter.filter(allIdentities) + assertThat(filteredIdentities[ownIdentity1]!!, containsInAnyOrder(identityA)) + assertThat(filteredIdentities[ownIdentity2]!!, empty()) + } + + private val filter = NoNegativeIdentityFilter() + private val ownIdentity1 = createOwnIdentity("1") + private val ownIdentity2 = createOwnIdentity("2") + private val identityA = createIdentity("A") + private val identityB = createIdentity("B") + private val identityC = createIdentity("C") + +} + +private fun OwnIdentity.trust(vararg identityTrust: Pair) = + this to identityTrust.map { (identity, trust) -> identity.copy().setTrust(this, trust) }.toSet() + +private fun Identity.copy() = DefaultIdentity(id, nickname, requestUri).apply { + contexts = this@copy.contexts + properties = this@copy.properties +}