From 9d6ae1c0bb9e8c4a2af98af81402695e838b2231 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Fri, 7 Jul 2023 16:37:29 +0200 Subject: [PATCH] Work on Network --- .../wot/dijkstra/sq/CertSynopsis.kt | 12 ++++++++--- .../wot/dijkstra/sq/Certification.kt | 2 +- .../org/pgpainless/wot/dijkstra/sq/Network.kt | 9 ++++++--- .../wot/dijkstra/CertificationSetTest.kt | 12 +++++------ .../wot/dijkstra/CertificationTest.kt | 6 +++--- .../pgpainless/wot/dijkstra/NetworkTest.kt | 20 +++++++++++++++++++ .../org/pgpainless/wot/dijkstra/PathTest.kt | 2 +- 7 files changed, 46 insertions(+), 17 deletions(-) diff --git a/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/sq/CertSynopsis.kt b/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/sq/CertSynopsis.kt index ac11901a..9f475372 100644 --- a/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/sq/CertSynopsis.kt +++ b/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/sq/CertSynopsis.kt @@ -16,9 +16,15 @@ import java.util.* */ data class CertSynopsis( val fingerprint: Fingerprint, - val expirationTime: Date?, - val revocationState: RevocationState, - val userIds : Map) { + val expirationTime: Date? = null, + val revocationState: RevocationState = RevocationState.notRevoked(), + val userIds : Map = mapOf()) { + + constructor(fingerprint: String, + expirationTime: Date? = null, + revocationState: RevocationState = RevocationState.notRevoked(), + userIds: Map = mapOf()): + this(Fingerprint(fingerprint), expirationTime, revocationState, userIds) override fun toString(): String { return if (userIds.isEmpty()) { diff --git a/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/sq/Certification.kt b/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/sq/Certification.kt index a7c988a0..c834504f 100644 --- a/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/sq/Certification.kt +++ b/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/sq/Certification.kt @@ -43,8 +43,8 @@ data class Certification( */ constructor( issuer: CertSynopsis, - targetUserId: String?, target: CertSynopsis, + targetUserId: String?, creationTime: Date) : this(issuer, target, targetUserId, creationTime, null, true, 120, Depth.limited(0), RegexSet.wildcard()) diff --git a/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/sq/Network.kt b/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/sq/Network.kt index 84a99212..ca88cfcc 100644 --- a/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/sq/Network.kt +++ b/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/sq/Network.kt @@ -71,22 +71,25 @@ class Network( private val protoEdges: MutableMap, CertificationSet> = mutableMapOf() private var referenceTime: ReferenceTime = ReferenceTime.now() - fun addNode(node: CertSynopsis) { + fun addNode(node: CertSynopsis): Builder { nodes[node.fingerprint] = node + return this } fun getNode(fingerprint: Fingerprint): CertSynopsis? { return nodes[fingerprint] } - fun addEdge(edge: Certification) { + fun addEdge(edge: Certification): Builder { protoEdges.getOrPut(Pair(edge.issuer.fingerprint, edge.target.fingerprint)) { CertificationSet.empty(edge.issuer, edge.target) }.add(edge) + return this } - fun setReferenceTime(time: ReferenceTime) { + fun setReferenceTime(time: ReferenceTime): Builder { this.referenceTime = time + return this } fun build(): Network { diff --git a/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/CertificationSetTest.kt b/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/CertificationSetTest.kt index f2a01619..2a14ea67 100644 --- a/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/CertificationSetTest.kt +++ b/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/CertificationSetTest.kt @@ -17,10 +17,10 @@ class CertificationSetTest { private val bob = CertSynopsis(Fingerprint("B"), null, RevocationState.notRevoked(), mapOf()) private val charlie = CertSynopsis(Fingerprint("C"), null, RevocationState.notRevoked(), mapOf()) - private val aliceSignsBob = Certification(alice, null, bob, Date()) - private val aliceSignsBobUserId = Certification(alice, "Bob ", bob, Date()) - private val aliceSignsCharlie = Certification(alice, null, charlie, Date()) - private val charlieSignsBob = Certification(charlie, null, bob, Date()) + private val aliceSignsBob = Certification(alice, bob, null, Date()) + private val aliceSignsBobUserId = Certification(alice, bob, "Bob ", Date()) + private val aliceSignsCharlie = Certification(alice, charlie, null, Date()) + private val charlieSignsBob = Certification(charlie, bob, null, Date()) @Test fun `verify that properties of an empty CertificationSet are also empty`() { @@ -112,8 +112,8 @@ class CertificationSetTest { fun `verify that for multiple Certifications over the same datum, only the most recent certifications are preserved`() { val now = Date() val fiveSecondsBefore = Date(now.time - 5000) - val old = Certification(alice, "Bob ", bob, fiveSecondsBefore) - val new = Certification(alice, "Bob ", bob, now) + val old = Certification(alice, bob, "Bob ", fiveSecondsBefore) + val new = Certification(alice, bob, "Bob ", now) val new2 = Certification(alice, bob, "Bob ", now, null, true, 44, Depth.auto(10), RegexSet.wildcard()) var set = CertificationSet(alice, bob, mapOf()) diff --git a/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/CertificationTest.kt b/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/CertificationTest.kt index 3940366b..56be10d9 100644 --- a/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/CertificationTest.kt +++ b/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/CertificationTest.kt @@ -28,21 +28,21 @@ class CertificationTest { @Test fun `verify result of toString() on certification`() { - val certification = Certification(alice, "Bob ", bob, Date()) + val certification = Certification(alice, bob, "Bob ", Date()) assertEquals("A certifies binding: Bob <-> B [120]", certification.toString()) } @Test fun `verify result of toString() on delegation`() { - val delegation = Certification(alice, null, bob, Date()) + val delegation = Certification(alice, bob, null, Date()) assertEquals("A certifies binding: null <-> B [120]", delegation.toString()) } @Test fun `verify result of toString() on delegation with userId-less issuer`() { - val delegation = Certification(charlie, null, bob, Date()) + val delegation = Certification(charlie, bob, null, Date()) assertEquals("C certifies binding: null <-> B [120]", delegation.toString()) } diff --git a/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/NetworkTest.kt b/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/NetworkTest.kt index e86ba4fe..32612a1f 100644 --- a/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/NetworkTest.kt +++ b/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/NetworkTest.kt @@ -5,8 +5,12 @@ package org.pgpainless.wot.dijkstra import org.junit.jupiter.api.Test +import org.pgpainless.wot.dijkstra.sq.CertSynopsis +import org.pgpainless.wot.dijkstra.sq.Certification +import org.pgpainless.wot.dijkstra.sq.Network import org.pgpainless.wot.dijkstra.sq.Network.Companion.empty import org.pgpainless.wot.dijkstra.sq.ReferenceTime.Companion.now +import java.util.Date import kotlin.test.assertEquals class NetworkTest { @@ -22,4 +26,20 @@ class NetworkTest { assertEquals(0, network.numberOfEdges) assertEquals(0, network.numberOfSignatures) } + + @Test + fun testSimpleNetwork() { + val alice = CertSynopsis("A") + val bob = CertSynopsis("B") + + val edge = Certification(alice, bob, null, Date()) + + val network = Network.builder() + .addNode(alice) + .addNode(bob) + .addEdge(edge) + .build() + + println(network) + } } \ No newline at end of file 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 index f68c060c..792b2067 100644 --- a/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/PathTest.kt +++ b/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/PathTest.kt @@ -32,7 +32,7 @@ class PathTest { // 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()) + private val alice_bob = Certification(alice, bob, null, Date()) // Root -> Root private val root_root = Certification(root, root, 120, Depth.limited(1))