From 7a1df029205d5c22d559e6d3b82da1c4a94a7665 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Fri, 7 Jul 2023 19:20:07 +0200 Subject: [PATCH] more DSL --- .../org/pgpainless/wot/dijkstra/NetworkDSL.kt | 27 ++++++++++++++++++ .../org/pgpainless/wot/dijkstra/PathTest.kt | 28 ++++--------------- .../org/pgpainless/wot/dijkstra/PathsTest.kt | 20 ++----------- .../pgpainless/wot/dijkstra/RegexSetTest.kt | 9 +++++- 4 files changed, 43 insertions(+), 41 deletions(-) diff --git a/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/NetworkDSL.kt b/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/NetworkDSL.kt index 57250b1d..4b36bc35 100644 --- a/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/NetworkDSL.kt +++ b/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/NetworkDSL.kt @@ -40,6 +40,9 @@ interface NetworkDSL { fun Certification(issuer: CertSynopsis, target: CertSynopsis, userId: String): Certification = Certification(issuer, target, userId, Date()) + fun Certification(issuer: CertSynopsis, target: CertSynopsis, amount: Int, depth: Depth): Certification = + Certification(issuer, target, null, Date(), null, true, amount, depth, RegexSet.wildcard()) + /** * Add a single [CertSynopsis] built from a [String] fingerprint to the builder. */ @@ -150,6 +153,30 @@ interface NetworkDSL { return getEdgeFor(Fingerprint(issuer), Fingerprint(target), userId) } + fun Date.plusMillis(millis: Long): Date { + return Date(time + millis) + } + + fun Date.plusSeconds(seconds: Long): Date { + return plusMillis(1000L * seconds) + } + + fun Date.plusMinutes(minutes: Long): Date { + return plusSeconds(60 * minutes) + } + + fun Date.plusHours(hours: Long): Date { + return plusMinutes(60 * hours) + } + + fun Date.plusDays(days: Long): Date { + return plusHours(24 * days) + } + + fun domainRegex(domain: String): RegexSet { + return RegexSet.fromExpression("<[^>]+[@.]" + domain.replace(".", "\\.") + ">$") + } + /** * Lambda with Receiver. * 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 792b2067..e32f5574 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 @@ -7,23 +7,11 @@ import java.util.* import kotlin.test.assertEquals import kotlin.test.assertTrue -class PathTest { +class PathTest: NetworkDSL { - 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()) + private val root = CertSynopsis("aabbccddeeAABBCCDDEEaabbccddeeAABBCCDDEE") + private val alice = CertSynopsis("0000000000000000000000000000000000000000") + private val bob = CertSynopsis("1111111111111111111111111111111111111111") // Root -(255, 255)-> Alice private val root_alice__fully_trusted = Certification(root, alice, 255, Depth.unconstrained()) @@ -32,7 +20,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, bob, null, Date()) + private val alice_bob = Certification(alice, bob) // Root -> Root private val root_root = Certification(root, root, 120, Depth.limited(1)) @@ -102,10 +90,4 @@ class PathTest { val path = Path(root) assertThrows { path.append(root_root) } } - - /** - * Factory method for legible initialization of [Certification] objects for test purposes. - */ - 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 index 6174263a..067aa066 100644 --- a/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/PathsTest.kt +++ b/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/PathsTest.kt @@ -6,18 +6,10 @@ import org.pgpainless.wot.dijkstra.sq.* import java.util.* import kotlin.test.assertEquals -class PathsTest { +class PathsTest: NetworkDSL { - private val alice = CertSynopsis( - Fingerprint("0000000000000000000000000000000000000000"), - null, - RevocationState.notRevoked(), - mapOf()) - private val bob = CertSynopsis( - Fingerprint("1111111111111111111111111111111111111111"), - null, - RevocationState.notRevoked(), - mapOf()) + private val alice = CertSynopsis("0000000000000000000000000000000000000000") + private val bob = CertSynopsis("1111111111111111111111111111111111111111") private val alice_bob_1 = Certification(alice, bob, 140, Depth.unconstrained()) private val alice_bob_2 = Certification(alice, bob, 160, Depth.limited(1)) @@ -56,10 +48,4 @@ class PathsTest { paths.add(path, 250) } } - - /** - * Factory method to create a [Certification] more easily for test purposes. - */ - private 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/RegexSetTest.kt b/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/RegexSetTest.kt index 4bc4c59a..0b133224 100644 --- a/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/RegexSetTest.kt +++ b/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/RegexSetTest.kt @@ -5,7 +5,7 @@ import org.pgpainless.wot.dijkstra.sq.RegexSet import kotlin.test.assertFalse import kotlin.test.assertTrue -class RegexSetTest { +class RegexSetTest: NetworkDSL { private val exampleComRegex = "<[^>]+[@.]example\\.com>\$" private val pgpainlessOrgRegex = "<[^>]+[@.]pgpainless\\.org>\$" @@ -39,4 +39,11 @@ class RegexSetTest { assertFalse { multi.matches("Alice") } assertFalse { multi.matches("") } } + + @Test + fun `verify that a domain regex built with DLS properly works`() { + val regex = domainRegex("pgpainless.org") + assertTrue { regex.matches("Alice ") } + assertFalse { regex.matches("") } + } } \ No newline at end of file