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

Path: adjust cycle detection to match our Path output style

This commit is contained in:
Heiko Schaefer 2023-07-15 19:32:03 +02:00
parent 65ff597f58
commit ba133cfc7d
No known key found for this signature in database
GPG key ID: 4A849A1904CCBD7D
2 changed files with 49 additions and 6 deletions

View file

@ -101,8 +101,18 @@ class Path(
"Not enough depth."
}
// root is c's target -> illegal cycle
var cyclic = root.fingerprint == nComponent.target.fingerprint
var cyclic = false
// An edge that points to the root is only allowed as the first and last edge
if ((edges.size > 0) && (root.fingerprint == nComponent.target.fingerprint)) {
cyclic = true
}
// And it's only legal if the edge points to the target User ID
// (XX: Do we know the target User ID here? Currently, we only check for != null)
if ((edges.size == 0) && (root.fingerprint == nComponent.target.fingerprint) && (nComponent.userId == null)) {
cyclic = true
}
for ((i, component) in edges.withIndex()) {
if (cyclic) {
break

View file

@ -7,7 +7,7 @@ import org.pgpainless.wot.dsl.NetworkDSL
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class PathTest: NetworkDSL {
class PathTest : NetworkDSL {
private val root = Node("aabbccddeeAABBCCDDEEaabbccddeeAABBCCDDEE")
private val alice = Node("0000000000000000000000000000000000000000")
@ -15,15 +15,37 @@ class PathTest: NetworkDSL {
// Root -(255, 255)-> Alice
private val root_alice__fully_trusted = EdgeComponent(root, alice, 255, Depth.unconstrained())
// Root -(60,0)-> Alice
private val root_alice__marginally_trusted = EdgeComponent(root, alice, 60, Depth.limited(0))
// Alice -(255,255)-> Root
private val alice_root = EdgeComponent(alice, root, 255, Depth.unconstrained())
// Alice -(120, 1)-> Bob
// Alice -(120, 0)-> Bob
private val alice_bob = EdgeComponent(alice, bob)
// Alice -(120, 1)-> Bob
private val alice_bob_depth_1 = EdgeComponent(alice, bob, 120, Depth.auto(1))
// Root -> Root
private val root_root = EdgeComponent(root, root, 120, Depth.limited(1))
private val root_selfsig = EdgeComponent(root, root, "root@example.org")
private val bob_selfsig = EdgeComponent(bob, bob, "bob@example.org")
@Test
fun `longer path that ends in a selfsig`() {
val path = Path(root)
path.append(root_alice__fully_trusted)
path.append(alice_bob_depth_1)
path.append(bob_selfsig)
assertEquals(120, path.amount)
assertEquals(0, path.residualDepth.value())
}
@Test
fun `verify that an empty Path is properly initialized`() {
val empty = Path(root)
@ -86,8 +108,19 @@ class PathTest: NetworkDSL {
}
@Test
fun `verify that a Path cannot point to its own root`() {
fun `trust root binding its identity is fine`() {
val path = Path(root)
assertThrows<IllegalArgumentException> { path.append(root_root) }
path.append(root_selfsig)
assertEquals(2, path.length)
}
@Test
fun `verify that a Path cannot point to its own root via a delegation`() {
val path = Path(root)
assertThrows<IllegalArgumentException> {
path.append(root_root)
}
}
}