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 2020/06/28 01:20:05 UTC

[incubator-tuweni] branch master updated: Add more coverage for rlpx

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 2f5d2e2  Add more coverage for rlpx
     new 3a060d4  Merge pull request #108 from atoulme/add_rlpx_stream_coverage
2f5d2e2 is described below

commit 2f5d2e2cfd52e2100c636c525c5796b11ade02c1
Author: Antoine Toulme <an...@lunar-ocean.com>
AuthorDate: Sat Jun 27 17:01:26 2020 -0700

    Add more coverage for rlpx
---
 .../tuweni/rlpx/RLPxConnectionFactoryTest.java     | 26 +++++++++++++
 .../apache/tuweni/units/bigints/UInt64Test.java    | 44 +++++++++++++++++-----
 2 files changed, 61 insertions(+), 9 deletions(-)

diff --git a/rlpx/src/test/java/org/apache/tuweni/rlpx/RLPxConnectionFactoryTest.java b/rlpx/src/test/java/org/apache/tuweni/rlpx/RLPxConnectionFactoryTest.java
index 49bff36..6d18081 100644
--- a/rlpx/src/test/java/org/apache/tuweni/rlpx/RLPxConnectionFactoryTest.java
+++ b/rlpx/src/test/java/org/apache/tuweni/rlpx/RLPxConnectionFactoryTest.java
@@ -27,6 +27,8 @@ import org.apache.tuweni.crypto.SECP256K1.SecretKey;
 import org.apache.tuweni.junit.BouncyCastleExtension;
 
 import java.security.SecureRandom;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 import java.util.concurrent.atomic.AtomicReference;
@@ -261,6 +263,30 @@ class RLPxConnectionFactoryTest {
   }
 
   @Test
+  void twoMessagesCollated() throws TimeoutException, InterruptedException {
+    KeyPair keyPair = KeyPair.random();
+    KeyPair peerKeyPair = KeyPair.random();
+
+    AtomicReference<RLPxConnection> peerConnectionReference = new AtomicReference<>();
+    Function<Bytes, AsyncResult<Bytes>> wireBytes = (bytes) -> {
+      AtomicReference<Bytes> responseReference = new AtomicReference<>();
+      peerConnectionReference.set(RLPxConnectionFactory.respondToHandshake(bytes, peerKeyPair, responseReference::set));
+      return AsyncResult.completed(responseReference.get());
+    };
+    AsyncResult<RLPxConnection> futureConn =
+        RLPxConnectionFactory.createHandshake(keyPair, peerKeyPair.publicKey(), wireBytes);
+
+    RLPxConnection conn = futureConn.get(1, TimeUnit.SECONDS);
+    RLPxConnection conn2 = peerConnectionReference.get();
+
+    List<RLPxMessage> messages = new ArrayList<>();
+    Bytes message = conn.write(new RLPxMessage(1, Bytes.fromHexString("deadbeef")));
+    Bytes message2 = conn.write(new RLPxMessage(1, Bytes.fromHexString("deadbeef")));
+    conn2.stream(Bytes.concatenate(message, message2), messages::add);
+    assertEquals(2, messages.size());
+  }
+
+  @Test
   void roundtripBytesEncryption() {
     KeyPair peerKeyPair = KeyPair.random();
 
diff --git a/units/src/test/java/org/apache/tuweni/units/bigints/UInt64Test.java b/units/src/test/java/org/apache/tuweni/units/bigints/UInt64Test.java
index 4537408..515e153 100644
--- a/units/src/test/java/org/apache/tuweni/units/bigints/UInt64Test.java
+++ b/units/src/test/java/org/apache/tuweni/units/bigints/UInt64Test.java
@@ -35,6 +35,11 @@ class UInt64Test {
     return UInt64.fromHexString(s);
   }
 
+
+  private static Bytes b(String s) {
+    return Bytes.fromHexString(s);
+  }
+
   @Test
   void valueOfLong() {
     assertThrows(IllegalArgumentException.class, () -> UInt64.valueOf(-1));
@@ -496,21 +501,34 @@ class UInt64Test {
 
   @ParameterizedTest
   @MethodSource("andProvider")
-  void and(UInt64 v1, UInt64 v2, UInt64 expected) {
-    assertValueEquals(expected, v1.and(v2));
+  void and(UInt64 v1, Object v2, UInt64 expected) {
+    if (v2 instanceof UInt64) {
+      assertValueEquals(expected, v1.and((UInt64) v2));
+    } else if (v2 instanceof Bytes) {
+      assertValueEquals(expected, v1.and((Bytes) v2));
+    } else {
+      throw new IllegalArgumentException(v2.getClass().getName());
+    }
   }
 
   private static Stream<Arguments> andProvider() {
     return Stream
         .of(
             Arguments.of(hv("0x00000000FFFFFFFF"), hv("0xFFFFFFFF00000000"), hv("0x0000000000000000")),
-            Arguments.of(hv("0x00000000FFFFFFFF"), hv("0xFFFFFFFFFF000000"), hv("0x00000000FF000000")));
+            Arguments.of(hv("0x00000000FFFFFFFF"), hv("0xFFFFFFFFFF000000"), hv("0x00000000FF000000")),
+            Arguments.of(hv("0x00000000FFFFFFFF"), b("0xFFFFFFFFFF000000"), hv("0x00000000FF000000")));
   }
 
   @ParameterizedTest
   @MethodSource("orProvider")
-  void or(UInt64 v1, UInt64 v2, UInt64 expected) {
-    assertValueEquals(expected, v1.or(v2));
+  void or(UInt64 v1, Object v2, UInt64 expected) {
+    if (v2 instanceof UInt64) {
+      assertValueEquals(expected, v1.or((UInt64) v2));
+    } else if (v2 instanceof Bytes) {
+      assertValueEquals(expected, v1.or((Bytes) v2));
+    } else {
+      throw new IllegalArgumentException(v2.getClass().getName());
+    }
   }
 
   private static Stream<Arguments> orProvider() {
@@ -518,13 +536,20 @@ class UInt64Test {
         .of(
             Arguments.of(hv("0x00000000FFFFFFFF"), hv("0xFFFFFFFF00000000"), hv("0xFFFFFFFFFFFFFFFF")),
             Arguments.of(hv("0x00000000FFFFFFFF"), hv("0xFFFFFFFF00000000"), hv("0xFFFFFFFFFFFFFFFF")),
-            Arguments.of(hv("0x00000000000000FF"), hv("0xFFFFFFFF00000000"), hv("0xFFFFFFFF000000FF")));
+            Arguments.of(hv("0x00000000000000FF"), hv("0xFFFFFFFF00000000"), hv("0xFFFFFFFF000000FF")),
+            Arguments.of(hv("0x00000000000000FF"), b("0xFFFFFFFF00000000"), hv("0xFFFFFFFF000000FF")));
   }
 
   @ParameterizedTest
   @MethodSource("xorProvider")
-  void xor(UInt64 v1, UInt64 v2, UInt64 expected) {
-    assertValueEquals(expected, v1.xor(v2));
+  void xor(UInt64 v1, Object v2, UInt64 expected) {
+    if (v2 instanceof UInt64) {
+      assertValueEquals(expected, v1.xor((UInt64) v2));
+    } else if (v2 instanceof Bytes) {
+      assertValueEquals(expected, v1.xor((Bytes) v2));
+    } else {
+      throw new IllegalArgumentException(v2.getClass().getName());
+    }
   }
 
   private static Stream<Arguments> xorProvider() {
@@ -532,7 +557,8 @@ class UInt64Test {
         .of(
             Arguments.of(hv("0xFFFFFFFFFFFFFFFF"), hv("0xFFFFFFFFFFFFFFFF"), hv("0x0000000000000000")),
             Arguments.of(hv("0x00000000FFFFFFFF"), hv("0xFFFFFFFF00000000"), hv("0xFFFFFFFFFFFFFFFF")),
-            Arguments.of(hv("0x00000000FFFFFFFF"), hv("0xFFFFFFFFFF000000"), hv("0xFFFFFFFF00FFFFFF")));
+            Arguments.of(hv("0x00000000FFFFFFFF"), hv("0xFFFFFFFFFF000000"), hv("0xFFFFFFFF00FFFFFF")),
+            Arguments.of(hv("0x00000000FFFFFFFF"), b("0xFFFFFFFFFF000000"), hv("0xFFFFFFFF00FFFFFF")));
   }
 
   @ParameterizedTest


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