From 72b68ad7e9e8101df068345bc5255b5ca675833b Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Fri, 30 Jun 2023 18:07:33 +0200 Subject: [PATCH] Test Path and Paths classes --- .../org/pgpainless/wot/dijkstra/PathTest.kt | 103 ++++++++++++++++++ .../org/pgpainless/wot/dijkstra/PathsTest.kt | 62 +++++++++++ 2 files changed, 165 insertions(+) create mode 100644 wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/PathTest.kt create mode 100644 wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/PathsTest.kt diff --git a/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/PathTest.kt b/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/PathTest.kt new file mode 100644 index 00000000..a41d44af --- /dev/null +++ b/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/PathTest.kt @@ -0,0 +1,103 @@ +package org.pgpainless.wot.dijkstra + +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import org.pgpainless.wot.dijkstra.sq.* +import java.util.* +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +class PathTest { + + private val root = CertSynopsis( + Fingerprint("aabbccddeeAABBCCDDEEaabbccddeeAABBCCDDEE"), + null, + RevocationState.notRevoked(), + mapOf()) + private val alice = CertSynopsis( + Fingerprint("0000000000000000000000000000000000000000"), + null, + RevocationState.notRevoked(), + mapOf()) + private val bob = CertSynopsis( + Fingerprint("1111111111111111111111111111111111111111"), + null, + RevocationState.notRevoked(), + mapOf()) + + // Root -(255, 255)-> Alice + private val root_alice__fully_trusted = Certification(root, alice, 255, Depth.unconstrained()) + // Root -(60,0)-> Alice + private val root_alice__marginally_trusted = Certification(root, alice, 60, Depth.limited(0)) + // Alice -(255,255)-> Root + private val alice_root = Certification(alice, root, 255, Depth.unconstrained()) + // Alice -(120, 1)-> Bob + private val alice_bob = Certification(alice, null, bob, Date()) + // Root -> Root + private val root_root = Certification(root, root, 120, Depth.limited(1)) + + @Test + fun emptyPathTest() { + val empty = Path(root) + assertEquals(root, empty.target) + assertEquals(listOf(root), empty.certificates) + assertEquals(1, empty.length) + assertEquals(120, empty.amount) + assertTrue { empty.certifications.isEmpty() } + } + + @Test + fun appendTest() { + val path = Path(root) + assertEquals(listOf(root), path.certificates) + + assertThrows { path.append(alice_bob) } + assertEquals(listOf(root), path.certificates) + assertEquals(1, path.length) + + path.append(root_alice__fully_trusted) + assertEquals(listOf(root_alice__fully_trusted), path.certifications) + assertEquals(listOf(root, alice), path.certificates) + assertEquals(alice, path.target) + assertEquals(2, path.length) + assertEquals(255, path.amount) + + path.append(alice_bob) + assertEquals(listOf(root_alice__fully_trusted, alice_bob), path.certifications) + assertEquals(listOf(root, alice, bob), path.certificates) + assertEquals(bob, path.target) + assertEquals(3, path.length) + assertEquals(120, path.amount) + } + + @Test + fun appendTest2() { + val path = Path(root) + path.append(root_alice__marginally_trusted) + assertEquals(60, path.amount) + + assertThrows { + path.append(alice_bob) + // not enough depth + } + } + + @Test + fun appendTest3() { + val path = Path(root) + path.append(root_alice__fully_trusted) + assertThrows { + path.append(alice_root) + // cyclic path + } + } + + @Test + fun appendTest4() { + val path = Path(root) + assertThrows { path.append(root_root) } + } + + fun Certification(issuer: CertSynopsis, target: CertSynopsis, amount: Int, depth: Depth): Certification = + Certification(issuer, target, null, Date(), null, true, amount, depth, RegexSet.wildcard()) +} \ No newline at end of file diff --git a/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/PathsTest.kt b/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/PathsTest.kt new file mode 100644 index 00000000..187e558f --- /dev/null +++ b/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/PathsTest.kt @@ -0,0 +1,62 @@ +package org.pgpainless.wot.dijkstra + +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import org.pgpainless.wot.dijkstra.sq.* +import java.util.* +import kotlin.test.assertEquals + +class PathsTest { + + private val alice = CertSynopsis( + Fingerprint("0000000000000000000000000000000000000000"), + null, + RevocationState.notRevoked(), + mapOf()) + private val bob = CertSynopsis( + Fingerprint("1111111111111111111111111111111111111111"), + null, + RevocationState.notRevoked(), + mapOf()) + + private val alice_bob_1 = Certification(alice, bob, 140, Depth.unconstrained()) + private val alice_bob_2 = Certification(alice, bob, 160, Depth.limited(1)) + + @Test + fun emptyPathsTest() { + val empty = Paths() + assertEquals(0, empty.amount) + } + + @Test + fun singlePathTest() { + val path = Path(alice).apply { append(alice_bob_1) } + val single = Paths().apply { add(path, 140) } + + assertEquals(140, single.amount) + } + + @Test + fun twoPathsTest() { + val path1 = Path(alice).apply { append(alice_bob_1) } + val path2 = Path(alice).apply { append(alice_bob_2) } + val twoPaths = Paths().apply { + add(path1, 140) + add(path2, 160) + } + + assertEquals(300, twoPaths.amount) + } + + @Test + fun notEnoughAmountTest() { + val path = Path(alice).apply { append(alice_bob_1) } + val paths = Paths() + assertThrows { + paths.add(path, 250) + } + } + + fun Certification(issuer: CertSynopsis, target: CertSynopsis, amount: Int, depth: Depth): Certification = + Certification(issuer, target, null, Date(), null, true, amount, depth, RegexSet.wildcard()) +} \ No newline at end of file