You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuweni.apache.org by to...@apache.org on 2019/05/27 22:47:58 UTC

[incubator-tuweni] branch master updated: Refine some of the logic and tests for dns discovery

This is an automated email from the ASF dual-hosted git repository.

toulmean pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git


The following commit(s) were added to refs/heads/master by this push:
     new 2f1776b  Refine some of the logic and tests for dns discovery
2f1776b is described below

commit 2f1776bbf2089263360f80ad2f3cc63890694f72
Author: Antoine Toulme <an...@lunar-ocean.com>
AuthorDate: Mon May 27 15:47:44 2019 -0700

    Refine some of the logic and tests for dns discovery
---
 .../kotlin/org/apache/tuweni/discovery/DNSEntry.kt | 28 +++++++----
 .../org/apache/tuweni/discovery/DNSEntryTest.kt    | 55 +++++++++++++++++-----
 2 files changed, 64 insertions(+), 19 deletions(-)

diff --git a/dns-discovery/src/main/kotlin/org/apache/tuweni/discovery/DNSEntry.kt b/dns-discovery/src/main/kotlin/org/apache/tuweni/discovery/DNSEntry.kt
index 5923e3b..022c6f1 100644
--- a/dns-discovery/src/main/kotlin/org/apache/tuweni/discovery/DNSEntry.kt
+++ b/dns-discovery/src/main/kotlin/org/apache/tuweni/discovery/DNSEntry.kt
@@ -48,7 +48,7 @@ internal interface DNSEntry {
       } else if (attrs.containsKey("enrtree-link")) {
         return ENRTreeLink(attrs)
       } else {
-        throw InvalidEntryException("$serialized should contain enrtree, enrtree-root or enrtree-link")
+        throw InvalidEntryException("$serialized should contain enrtree, enr, enrtree-root or enrtree-link")
       }
     }
   }
@@ -82,24 +82,36 @@ class ENRTreeRoot(attrs: Map<String, String>) : DNSEntry {
     sig = SECP256K1.Signature.fromBytes(Base64URLSafe.decode(attrs["sig"]!!))
     hash = Base32.decode(attrs["hash"]!!)
   }
+
+  override fun toString(): String {
+    val encodedHash = Base32.encode(hash)
+    return "enrtree-root=$version hash=${encodedHash.subSequence(0, encodedHash.indexOf("="))} seq=$seq sig=${Base64URLSafe.encode(sig.bytes())}"
+  }
 }
 
 class ENRTree(attrs: Map<String, String>) : DNSEntry {
 
+  val entries: List<String>
   init {
-    if (attrs["enrtree"] == null) {
-      throw InvalidEntryException("Missing attributes on enrtree entry")
-    }
+    val attr = attrs["enrtree"] ?: throw InvalidEntryException("Missing attributes on enrtree entry")
+    entries = attr.split(",")
+  }
+
+  override fun toString(): String {
+    return "enrtree=${entries.joinToString(",")}"
   }
 }
+
 class ENRTreeLink(attrs: Map<String, String>) : DNSEntry {
 
   val domainName: String
   init {
-    if (attrs["enrtree-link"] == null) {
-      throw InvalidEntryException("Missing attributes on enrtree-link entry")
-    }
-    domainName = attrs["enrtree-link"]!!
+    val attr = attrs["enrtree-link"] ?: throw InvalidEntryException("Missing attributes on enrtree-link entry")
+    domainName = attr
+  }
+
+  override fun toString(): String {
+    return "enrtree-link=$domainName"
   }
 }
 
diff --git a/dns-discovery/src/test/kotlin/org/apache/tuweni/discovery/DNSEntryTest.kt b/dns-discovery/src/test/kotlin/org/apache/tuweni/discovery/DNSEntryTest.kt
index a1ea440..e2ef1e8 100644
--- a/dns-discovery/src/test/kotlin/org/apache/tuweni/discovery/DNSEntryTest.kt
+++ b/dns-discovery/src/test/kotlin/org/apache/tuweni/discovery/DNSEntryTest.kt
@@ -28,42 +28,42 @@ class DNSEntryTest {
 
   @Test
   fun testInvalidEntry() {
-    val exception: InvalidEntryException = assertThrows({
+    val exception: InvalidEntryException = assertThrows {
       DNSEntry.readDNSEntry("garbage")
-    })
+    }
     assertEquals("Invalid record entry garbage", exception.message)
   }
 
   @Test
   fun testInvalidEntryMissingSeparator() {
-    val exception: InvalidEntryException = assertThrows({
+    val exception: InvalidEntryException = assertThrows {
       DNSEntry.readDNSEntry("garbage=abc def")
-    })
+    }
     assertEquals("Invalid record entry garbage=abc def", exception.message)
   }
 
   @Test
   fun testInvalidEntryMissingENR() {
-    val exception: InvalidEntryException = assertThrows({
+    val exception: InvalidEntryException = assertThrows {
       DNSEntry.readDNSEntry("garbage=abc def=gfh")
-    })
-    assertEquals("garbage=abc def=gfh should contain enrtree, enrtree-root or enrtree-link", exception.message)
+    }
+    assertEquals("garbage=abc def=gfh should contain enrtree, enr, enrtree-root or enrtree-link", exception.message)
   }
 
   @Test
   fun missingSigEntry() {
-    val exception: InvalidEntryException = assertThrows({
+    val exception: InvalidEntryException = assertThrows {
       DNSEntry.readDNSEntry("enrtree-root=v1 hash=TO4Q75OQ2N7DX4EOOR7X66A6OM seq=3")
-    })
+    }
     assertEquals("Missing attributes on root entry", exception.message)
   }
 
   @Test
   fun missingSeqEntry() {
-    val exception: InvalidEntryException = assertThrows({
+    val exception: InvalidEntryException = assertThrows {
       DNSEntry.readDNSEntry("enrtree-root=v1 hash=TO4Q75OQ2N7DX4EOOR7X66A6OM " +
         "sig=N-YY6UB9xD0hFx1Gmnt7v0RfSxch5tKyry2SRDoLx7B4GfPXagwLxQqyf7gAMvApFn_ORwZQekMWa_pXrcGCtwE=")
-    })
+    }
     assertEquals("Missing attributes on root entry", exception.message)
   }
 
@@ -93,4 +93,37 @@ class DNSEntryTest {
     assertNotNull(nodeRecord)
     nodeRecord.validate()
   }
+
+  @Test
+  fun testValidENRTreeNode() {
+    val entry = DNSEntry.readDNSEntry("enrtree=F4YWVKW4N6B2DDZWFS4XCUQBHY,JTNOVTCP6XZUMXDRANXA6SWXTM," +
+      "JGUFMSAGI7KZYB3P7IZW4S5Y3A")
+    val enr = entry as ENRTree
+    val entries = enr.entries
+    assertEquals(listOf("F4YWVKW4N6B2DDZWFS4XCUQBHY","JTNOVTCP6XZUMXDRANXA6SWXTM","JGUFMSAGI7KZYB3P7IZW4S5Y3A"), entries)
+  }
+
+  @Test
+  fun testRootToString() {
+    val root = ENRTreeRoot(mapOf(Pair("enrtree-root", "v1"), Pair("hash", "TO4Q75OQ2N7DX4EOOR7X66A6OM"), Pair("seq", "3"),
+      Pair("sig", "N-YY6UB9xD0hFx1Gmnt7v0RfSxch5tKyry2SRDoLx7B4GfPXagwLxQqyf7gAMvApFn_ORwZQekMWa_pXrcGCtwE=")))
+    assertEquals("enrtree-root=v1 hash=TO4Q75OQ2N7DX4EOOR7X66A6OM seq=3 sig=N-YY6UB9xD0hFx1Gmnt7v0RfSxch5tKyry" +
+      "2SRDoLx7B4GfPXagwLxQqyf7gAMvApFn_ORwZQekMWa_pXrcGCtwE=", root.toString())
+  }
+
+  @Test
+  fun testEntryToString() {
+    val entry = DNSEntry.readDNSEntry("enrtree=F4YWVKW4N6B2DDZWFS4XCUQBHY,JTNOVTCP6XZUMXDRANXA6SWXTM," +
+      "JGUFMSAGI7KZYB3P7IZW4S5Y3A")
+    assertEquals("enrtree=F4YWVKW4N6B2DDZWFS4XCUQBHY,JTNOVTCP6XZUMXDRANXA6SWXTM,JGUFMSAGI7KZYB3P7IZW4S5Y3A",
+      entry.toString())
+  }
+
+  @Test
+  fun testEntryLinkToString() {
+    val entry = DNSEntry.readDNSEntry("enrtree-link=AM5FCQLWIZX2QFPNJAP7VUERCCRNGRHWZG3YYHIUV7B" +
+      "VDQ5FDPRT2@morenodes.example.org")
+    assertEquals("enrtree-link=AM5FCQLWIZX2QFPNJAP7VUERCCRNGRHWZG3YYHIUV7BVDQ5FDPRT2@morenodes.example.org",
+      entry.toString())
+  }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tuweni.apache.org
For additional commands, e-mail: commits-help@tuweni.apache.org