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/12/03 15:40:05 UTC

[incubator-tuweni] branch master updated: Fix issue 168: Bytes32.wrap with offset is incorrect

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 d0a221c  Fix issue 168: Bytes32.wrap with offset is incorrect
     new 28bcd1d  Merge pull request #179 from atoulme/fix_168
d0a221c is described below

commit d0a221c753c5a99154d9bdcf413fa4d4f83a02dd
Author: Antoine Toulme <an...@lunar-ocean.com>
AuthorDate: Thu Dec 3 00:47:32 2020 -0800

    Fix issue 168: Bytes32.wrap with offset is incorrect
---
 .../main/java/org/apache/tuweni/bytes/Bytes32.java |  2 +-
 .../java/org/apache/tuweni/bytes/Bytes32Test.java  | 52 ++++++++++++++++++++++
 2 files changed, 53 insertions(+), 1 deletion(-)

diff --git a/bytes/src/main/java/org/apache/tuweni/bytes/Bytes32.java b/bytes/src/main/java/org/apache/tuweni/bytes/Bytes32.java
index c64de00..688a702 100644
--- a/bytes/src/main/java/org/apache/tuweni/bytes/Bytes32.java
+++ b/bytes/src/main/java/org/apache/tuweni/bytes/Bytes32.java
@@ -108,7 +108,7 @@ public interface Bytes32 extends Bytes {
     if (slice instanceof Bytes32) {
       return (Bytes32) slice;
     }
-    return new DelegatingBytes32(value);
+    return new DelegatingBytes32(slice);
   }
 
   /**
diff --git a/bytes/src/test/java/org/apache/tuweni/bytes/Bytes32Test.java b/bytes/src/test/java/org/apache/tuweni/bytes/Bytes32Test.java
index 5407f92..95404c3 100644
--- a/bytes/src/test/java/org/apache/tuweni/bytes/Bytes32Test.java
+++ b/bytes/src/test/java/org/apache/tuweni/bytes/Bytes32Test.java
@@ -20,6 +20,58 @@ import org.junit.jupiter.api.Test;
 class Bytes32Test {
 
   @Test
+  void testMutableBytes32WrapWithOffset() {
+    Bytes bytes = Bytes
+        .fromHexString(
+            "0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff");
+    MutableBytes mutableBytes = MutableBytes.create(48);
+    bytes.copyTo(mutableBytes);
+    assertEquals(
+        "0x112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00",
+        Bytes32.wrap(mutableBytes, 1).toHexString());
+  }
+
+  @Test
+  void testMutableBytes32SliceWithOffset() {
+    Bytes bytes = Bytes
+        .fromHexString(
+            "0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff");
+    MutableBytes mutableBytes = MutableBytes.create(48);
+    bytes.copyTo(mutableBytes);
+    assertEquals("0x11", mutableBytes.slice(1, 1).toHexString());
+    assertEquals("0x1122", mutableBytes.slice(1, 2).toHexString());
+    assertEquals("0x112233445566778899aa", mutableBytes.slice(1, 10).toHexString());
+    assertEquals("0x112233445566778899aabbccddeeff", mutableBytes.slice(1, 15).toHexString());
+    assertEquals(
+        "0x112233445566778899aabbccddeeff00112233445566778899aabbccddee",
+        mutableBytes.slice(1, 30).toHexString());
+    assertEquals(
+        "0x112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00",
+        mutableBytes.slice(1, 32).toHexString());
+    assertEquals(
+        "0x112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddee",
+        mutableBytes.slice(1, 46).toHexString());
+  }
+
+  @Test
+  void testBytes32SliceWithOffset() {
+    Bytes bytes = Bytes
+        .fromHexString(
+            "0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff");
+    assertEquals("0x11", bytes.slice(1, 1).toHexString());
+    assertEquals("0x1122", bytes.slice(1, 2).toHexString());
+    assertEquals("0x112233445566778899aa", bytes.slice(1, 10).toHexString());
+    assertEquals("0x112233445566778899aabbccddeeff", bytes.slice(1, 15).toHexString());
+    assertEquals("0x112233445566778899aabbccddeeff00112233445566778899aabbccddee", bytes.slice(1, 30).toHexString());
+    assertEquals(
+        "0x112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00",
+        bytes.slice(1, 32).toHexString());
+    assertEquals(
+        "0x112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddee",
+        bytes.slice(1, 46).toHexString());
+  }
+
+  @Test
   void failsWhenWrappingArraySmallerThan32() {
     Throwable exception = assertThrows(IllegalArgumentException.class, () -> Bytes32.wrap(new byte[31]));
     assertEquals("Expected 32 bytes but got 31", exception.getMessage());


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