1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-12-25 12:27:58 +01:00

Test Path and Paths classes

This commit is contained in:
Paul Schaub 2023-06-30 18:07:33 +02:00
parent aa74d7d4f1
commit 72b68ad7e9
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 165 additions and 0 deletions

View file

@ -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<IllegalArgumentException> { 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<IllegalArgumentException> {
path.append(alice_bob)
// not enough depth
}
}
@Test
fun appendTest3() {
val path = Path(root)
path.append(root_alice__fully_trusted)
assertThrows<IllegalArgumentException> {
path.append(alice_root)
// cyclic path
}
}
@Test
fun appendTest4() {
val path = Path(root)
assertThrows<IllegalArgumentException> { 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())
}

View file

@ -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<IllegalArgumentException> {
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())
}