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 05:55:32 UTC
[incubator-tuweni] branch master updated: More coverage
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 db1048d More coverage
new d595b5c Merge pull request #109 from atoulme/mo_coverage
db1048d is described below
commit db1048ddd7d357522c4c21a521e8bdfe9c985b75
Author: Antoine Toulme <an...@lunar-ocean.com>
AuthorDate: Sat Jun 27 22:43:01 2020 -0700
More coverage
---
.../java/org/apache/tuweni/bytes/Bytes48Test.java | 8 +--
.../{Bytes48Test.java => DelegateBytes32Test.java} | 56 ++++++++++-----------
.../{Bytes48Test.java => DelegateBytes48Test.java} | 52 +++++++++-----------
.../org/apache/tuweni/bytes/DelegateBytesTest.java | 2 +-
.../org/apache/tuweni/crypto/sodium/AuthTest.java | 15 ++++++
.../tuweni/crypto/sodium/DiffieHelmanTest.java | 8 +++
...{DiffieHelmanTest.java => KeyExchangeTest.java} | 36 ++++++++------
.../org/apache/tuweni/ethstats/NodeStatsTest.java | 33 ++++++-------
.../apache/tuweni/toml/MutableTomlArrayTest.java | 57 ++++++++++++++++++++++
9 files changed, 170 insertions(+), 97 deletions(-)
diff --git a/bytes/src/test/java/org/apache/tuweni/bytes/Bytes48Test.java b/bytes/src/test/java/org/apache/tuweni/bytes/Bytes48Test.java
index e3114a8..d2ce8cc 100644
--- a/bytes/src/test/java/org/apache/tuweni/bytes/Bytes48Test.java
+++ b/bytes/src/test/java/org/apache/tuweni/bytes/Bytes48Test.java
@@ -21,14 +21,14 @@ class Bytes48Test {
@Test
void failsWhenWrappingArraySmallerThan48() {
- Throwable exception = assertThrows(IllegalArgumentException.class, () -> Bytes48.wrap(new byte[31]));
- assertEquals("Expected 48 bytes but got 31", exception.getMessage());
+ Throwable exception = assertThrows(IllegalArgumentException.class, () -> Bytes48.wrap(new byte[47]));
+ assertEquals("Expected 48 bytes but got 47", exception.getMessage());
}
@Test
void failsWhenWrappingArrayLargerThan48() {
- Throwable exception = assertThrows(IllegalArgumentException.class, () -> Bytes48.wrap(new byte[33]));
- assertEquals("Expected 48 bytes but got 33", exception.getMessage());
+ Throwable exception = assertThrows(IllegalArgumentException.class, () -> Bytes48.wrap(new byte[49]));
+ assertEquals("Expected 48 bytes but got 49", exception.getMessage());
}
@Test
diff --git a/bytes/src/test/java/org/apache/tuweni/bytes/Bytes48Test.java b/bytes/src/test/java/org/apache/tuweni/bytes/DelegateBytes32Test.java
similarity index 50%
copy from bytes/src/test/java/org/apache/tuweni/bytes/Bytes48Test.java
copy to bytes/src/test/java/org/apache/tuweni/bytes/DelegateBytes32Test.java
index e3114a8..cc50fa4 100644
--- a/bytes/src/test/java/org/apache/tuweni/bytes/Bytes48Test.java
+++ b/bytes/src/test/java/org/apache/tuweni/bytes/DelegateBytes32Test.java
@@ -17,53 +17,47 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
-class Bytes48Test {
+class DelegateBytes32Test {
@Test
- void failsWhenWrappingArraySmallerThan48() {
- Throwable exception = assertThrows(IllegalArgumentException.class, () -> Bytes48.wrap(new byte[31]));
- assertEquals("Expected 48 bytes but got 31", exception.getMessage());
+ void failsWhenWrappingArraySmallerThan32() {
+ Throwable exception = assertThrows(IllegalArgumentException.class, () -> Bytes32.wrap(Bytes.wrap(new byte[31])));
+ assertEquals("Expected 32 bytes but got 31", exception.getMessage());
}
@Test
- void failsWhenWrappingArrayLargerThan48() {
- Throwable exception = assertThrows(IllegalArgumentException.class, () -> Bytes48.wrap(new byte[33]));
- assertEquals("Expected 48 bytes but got 33", exception.getMessage());
+ void failsWhenWrappingArrayLargerThan32() {
+ Throwable exception = assertThrows(IllegalArgumentException.class, () -> Bytes32.wrap(Bytes.wrap(new byte[33])));
+ assertEquals("Expected 32 bytes but got 33", exception.getMessage());
}
@Test
- void rightPadAValueToBytes48() {
- Bytes48 b48 = Bytes48.rightPad(Bytes.of(1, 2, 3));
- assertEquals(48, b48.size());
- for (int i = 3; i < 48; ++i) {
- assertEquals((byte) 0, b48.get(i));
- }
- assertEquals((byte) 1, b48.get(0));
- assertEquals((byte) 2, b48.get(1));
- assertEquals((byte) 3, b48.get(2));
+ void failsWhenLeftPaddingValueLargerThan32() {
+ Throwable exception = assertThrows(IllegalArgumentException.class, () -> Bytes32.leftPad(MutableBytes.create(33)));
+ assertEquals("Expected at most 32 bytes but got 33", exception.getMessage());
}
@Test
- void leftPadAValueToBytes48() {
- Bytes48 b48 = Bytes48.leftPad(Bytes.of(1, 2, 3));
- assertEquals(48, b48.size());
- for (int i = 0; i < 28; ++i) {
- assertEquals((byte) 0, b48.get(i));
- }
- assertEquals((byte) 1, b48.get(45));
- assertEquals((byte) 2, b48.get(46));
- assertEquals((byte) 3, b48.get(47));
+ void failsWhenRightPaddingValueLargerThan32() {
+ Throwable exception = assertThrows(IllegalArgumentException.class, () -> Bytes32.rightPad(MutableBytes.create(33)));
+ assertEquals("Expected at most 32 bytes but got 33", exception.getMessage());
}
@Test
- void failsWhenLeftPaddingValueLargerThan48() {
- Throwable exception = assertThrows(IllegalArgumentException.class, () -> Bytes48.leftPad(MutableBytes.create(49)));
- assertEquals("Expected at most 48 bytes but got 49", exception.getMessage());
+ void testSize() {
+ assertEquals(32, new DelegatingBytes32(Bytes32.random()).size());
}
@Test
- void failsWhenRightPaddingValueLargerThan48() {
- Throwable exception = assertThrows(IllegalArgumentException.class, () -> Bytes48.rightPad(MutableBytes.create(49)));
- assertEquals("Expected at most 48 bytes but got 49", exception.getMessage());
+ void testCopy() {
+ Bytes bytes = new DelegatingBytes32(Bytes32.random()).copy();
+ assertEquals(bytes, bytes.copy());
+ assertEquals(bytes, bytes.mutableCopy());
+ }
+
+ @Test
+ void testSlice() {
+ Bytes bytes = new DelegatingBytes32(Bytes32.random()).copy();
+ assertEquals(Bytes.wrap(new byte[] {bytes.get(2), bytes.get(3), bytes.get(4)}), bytes.slice(2, 3));
}
}
diff --git a/bytes/src/test/java/org/apache/tuweni/bytes/Bytes48Test.java b/bytes/src/test/java/org/apache/tuweni/bytes/DelegateBytes48Test.java
similarity index 67%
copy from bytes/src/test/java/org/apache/tuweni/bytes/Bytes48Test.java
copy to bytes/src/test/java/org/apache/tuweni/bytes/DelegateBytes48Test.java
index e3114a8..03bd043 100644
--- a/bytes/src/test/java/org/apache/tuweni/bytes/Bytes48Test.java
+++ b/bytes/src/test/java/org/apache/tuweni/bytes/DelegateBytes48Test.java
@@ -17,42 +17,18 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
-class Bytes48Test {
+class DelegateBytes48Test {
@Test
void failsWhenWrappingArraySmallerThan48() {
- Throwable exception = assertThrows(IllegalArgumentException.class, () -> Bytes48.wrap(new byte[31]));
- assertEquals("Expected 48 bytes but got 31", exception.getMessage());
+ Throwable exception = assertThrows(IllegalArgumentException.class, () -> Bytes48.wrap(Bytes.wrap(new byte[47])));
+ assertEquals("Expected 48 bytes but got 47", exception.getMessage());
}
@Test
void failsWhenWrappingArrayLargerThan48() {
- Throwable exception = assertThrows(IllegalArgumentException.class, () -> Bytes48.wrap(new byte[33]));
- assertEquals("Expected 48 bytes but got 33", exception.getMessage());
- }
-
- @Test
- void rightPadAValueToBytes48() {
- Bytes48 b48 = Bytes48.rightPad(Bytes.of(1, 2, 3));
- assertEquals(48, b48.size());
- for (int i = 3; i < 48; ++i) {
- assertEquals((byte) 0, b48.get(i));
- }
- assertEquals((byte) 1, b48.get(0));
- assertEquals((byte) 2, b48.get(1));
- assertEquals((byte) 3, b48.get(2));
- }
-
- @Test
- void leftPadAValueToBytes48() {
- Bytes48 b48 = Bytes48.leftPad(Bytes.of(1, 2, 3));
- assertEquals(48, b48.size());
- for (int i = 0; i < 28; ++i) {
- assertEquals((byte) 0, b48.get(i));
- }
- assertEquals((byte) 1, b48.get(45));
- assertEquals((byte) 2, b48.get(46));
- assertEquals((byte) 3, b48.get(47));
+ Throwable exception = assertThrows(IllegalArgumentException.class, () -> Bytes48.wrap(Bytes.wrap(new byte[49])));
+ assertEquals("Expected 48 bytes but got 49", exception.getMessage());
}
@Test
@@ -66,4 +42,22 @@ class Bytes48Test {
Throwable exception = assertThrows(IllegalArgumentException.class, () -> Bytes48.rightPad(MutableBytes.create(49)));
assertEquals("Expected at most 48 bytes but got 49", exception.getMessage());
}
+
+ @Test
+ void testSize() {
+ assertEquals(48, new DelegatingBytes48(Bytes48.random()).size());
+ }
+
+ @Test
+ void testCopy() {
+ Bytes bytes = new DelegatingBytes48(Bytes48.random()).copy();
+ assertEquals(bytes, bytes.copy());
+ assertEquals(bytes, bytes.mutableCopy());
+ }
+
+ @Test
+ void testSlice() {
+ Bytes bytes = new DelegatingBytes48(Bytes48.random()).copy();
+ assertEquals(Bytes.wrap(new byte[] {bytes.get(2), bytes.get(3), bytes.get(4)}), bytes.slice(2, 3));
+ }
}
diff --git a/bytes/src/test/java/org/apache/tuweni/bytes/DelegateBytesTest.java b/bytes/src/test/java/org/apache/tuweni/bytes/DelegateBytesTest.java
index bfc667c..a4762a7 100644
--- a/bytes/src/test/java/org/apache/tuweni/bytes/DelegateBytesTest.java
+++ b/bytes/src/test/java/org/apache/tuweni/bytes/DelegateBytesTest.java
@@ -12,7 +12,7 @@
*/
package org.apache.tuweni.bytes;
-public class DelegateBytesTest extends CommonBytesTests {
+class DelegateBytesTest extends CommonBytesTests {
@Override
Bytes h(String hex) {
diff --git a/crypto/src/test/java/org/apache/tuweni/crypto/sodium/AuthTest.java b/crypto/src/test/java/org/apache/tuweni/crypto/sodium/AuthTest.java
index 35a8da3..70478f6 100644
--- a/crypto/src/test/java/org/apache/tuweni/crypto/sodium/AuthTest.java
+++ b/crypto/src/test/java/org/apache/tuweni/crypto/sodium/AuthTest.java
@@ -13,6 +13,7 @@
package org.apache.tuweni.crypto.sodium;
import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
@@ -39,4 +40,18 @@ class AuthTest {
assertFalse(Auth.verify(tag, "An invalid input".getBytes(UTF_8), key));
assertFalse(Auth.verify(tag, input, Auth.Key.random()));
}
+
+ @Test
+ void testEquals() {
+ Auth.Key key = Auth.Key.random();
+ Auth.Key copy = Auth.Key.fromBytes(key.bytes());
+ assertEquals(key, copy);
+ }
+
+ @Test
+ void testDestroy() {
+ Auth.Key key = Auth.Key.random();
+ key.destroy();
+ assertTrue(key.isDestroyed());
+ }
}
diff --git a/crypto/src/test/java/org/apache/tuweni/crypto/sodium/DiffieHelmanTest.java b/crypto/src/test/java/org/apache/tuweni/crypto/sodium/DiffieHelmanTest.java
index c50746d..217bf0c 100644
--- a/crypto/src/test/java/org/apache/tuweni/crypto/sodium/DiffieHelmanTest.java
+++ b/crypto/src/test/java/org/apache/tuweni/crypto/sodium/DiffieHelmanTest.java
@@ -13,6 +13,7 @@
package org.apache.tuweni.crypto.sodium;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import org.junit.jupiter.api.BeforeAll;
@@ -59,4 +60,11 @@ class DiffieHelmanTest {
assertEquals(keyPair.publicKey(), keyPair2.publicKey());
assertEquals(keyPair.hashCode(), keyPair2.hashCode());
}
+
+ @Test
+ void testDestroy() {
+ DiffieHelman.KeyPair keyPair = DiffieHelman.KeyPair.random();
+ keyPair.secretKey().destroy();
+ assertTrue(keyPair.secretKey().isDestroyed());
+ }
}
diff --git a/crypto/src/test/java/org/apache/tuweni/crypto/sodium/DiffieHelmanTest.java b/crypto/src/test/java/org/apache/tuweni/crypto/sodium/KeyExchangeTest.java
similarity index 55%
copy from crypto/src/test/java/org/apache/tuweni/crypto/sodium/DiffieHelmanTest.java
copy to crypto/src/test/java/org/apache/tuweni/crypto/sodium/KeyExchangeTest.java
index c50746d..b682a2a 100644
--- a/crypto/src/test/java/org/apache/tuweni/crypto/sodium/DiffieHelmanTest.java
+++ b/crypto/src/test/java/org/apache/tuweni/crypto/sodium/KeyExchangeTest.java
@@ -13,12 +13,13 @@
package org.apache.tuweni.crypto.sodium;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
-class DiffieHelmanTest {
+class KeyExchangeTest {
@BeforeAll
static void checkAvailable() {
@@ -26,37 +27,44 @@ class DiffieHelmanTest {
}
@Test
- void testScalarMultiplication() {
- DiffieHelman.KeyPair keyPair = DiffieHelman.KeyPair.random();
- DiffieHelman.KeyPair secondKeyPair = DiffieHelman.KeyPair.random();
+ void testMatchingSession() {
+ KeyExchange.KeyPair clientKeyPair = KeyExchange.KeyPair.random();
+ KeyExchange.KeyPair serverKeyPair = KeyExchange.KeyPair.random();
+ KeyExchange.SessionKeyPair clientSessionKeyPair = KeyExchange.client(clientKeyPair, serverKeyPair.publicKey());
+ KeyExchange.SessionKeyPair serverSessionKeyPair = KeyExchange.server(serverKeyPair, clientKeyPair.publicKey());
- DiffieHelman.Secret scalar1 = DiffieHelman.Secret.forKeys(keyPair.secretKey(), secondKeyPair.publicKey());
- DiffieHelman.Secret scalar2 = DiffieHelman.Secret.forKeys(secondKeyPair.secretKey(), keyPair.publicKey());
-
- assertEquals(scalar1, scalar2);
+ assertEquals(clientSessionKeyPair.rx().bytes(), serverSessionKeyPair.tx().bytes());
+ assertEquals(clientSessionKeyPair.tx().bytes(), serverSessionKeyPair.rx().bytes());
}
@Test
void testEquals() {
- DiffieHelman.KeyPair keyPair = DiffieHelman.KeyPair.random();
- DiffieHelman.KeyPair keyPair2 = DiffieHelman.KeyPair.forSecretKey(keyPair.secretKey());
+ KeyExchange.KeyPair keyPair = KeyExchange.KeyPair.random();
+ KeyExchange.KeyPair keyPair2 = KeyExchange.KeyPair.forSecretKey(keyPair.secretKey());
assertEquals(keyPair, keyPair2);
assertEquals(keyPair.hashCode(), keyPair2.hashCode());
}
@Test
void testEqualsSecretKey() {
- DiffieHelman.KeyPair keyPair = DiffieHelman.KeyPair.random();
- DiffieHelman.KeyPair keyPair2 = DiffieHelman.KeyPair.forSecretKey(keyPair.secretKey());
+ KeyExchange.KeyPair keyPair = KeyExchange.KeyPair.random();
+ KeyExchange.KeyPair keyPair2 = KeyExchange.KeyPair.forSecretKey(keyPair.secretKey());
assertEquals(keyPair.secretKey(), keyPair2.secretKey());
assertEquals(keyPair.hashCode(), keyPair2.hashCode());
}
@Test
void testEqualsPublicKey() {
- DiffieHelman.KeyPair keyPair = DiffieHelman.KeyPair.random();
- DiffieHelman.KeyPair keyPair2 = DiffieHelman.KeyPair.forSecretKey(keyPair.secretKey());
+ KeyExchange.KeyPair keyPair = KeyExchange.KeyPair.random();
+ KeyExchange.KeyPair keyPair2 = KeyExchange.KeyPair.forSecretKey(keyPair.secretKey());
assertEquals(keyPair.publicKey(), keyPair2.publicKey());
assertEquals(keyPair.hashCode(), keyPair2.hashCode());
}
+
+ @Test
+ void testDestroy() {
+ KeyExchange.KeyPair keyPair = KeyExchange.KeyPair.random();
+ keyPair.secretKey().destroy();
+ assertTrue(keyPair.secretKey().isDestroyed());
+ }
}
diff --git a/bytes/src/test/java/org/apache/tuweni/bytes/DelegateBytesTest.java b/ethstats/src/test/java/org/apache/tuweni/ethstats/NodeStatsTest.java
similarity index 52%
copy from bytes/src/test/java/org/apache/tuweni/bytes/DelegateBytesTest.java
copy to ethstats/src/test/java/org/apache/tuweni/ethstats/NodeStatsTest.java
index bfc667c..5282771 100644
--- a/bytes/src/test/java/org/apache/tuweni/bytes/DelegateBytesTest.java
+++ b/ethstats/src/test/java/org/apache/tuweni/ethstats/NodeStatsTest.java
@@ -10,28 +10,25 @@
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
-package org.apache.tuweni.bytes;
+package org.apache.tuweni.ethstats;
-public class DelegateBytesTest extends CommonBytesTests {
+import static org.junit.jupiter.api.Assertions.assertEquals;
- @Override
- Bytes h(String hex) {
- return new DelegatingBytes(Bytes.fromHexString(hex));
- }
+import org.apache.tuweni.eth.EthJsonModule;
- @Override
- MutableBytes m(int size) {
- // no-op
- return MutableBytes.create(size);
- }
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.jupiter.api.Test;
- @Override
- Bytes w(byte[] bytes) {
- return new DelegatingBytes(Bytes.wrap(bytes));
- }
+class NodeStatsTest {
- @Override
- Bytes of(int... bytes) {
- return new DelegatingBytes(Bytes.of(bytes));
+ @Test
+ void toJson() throws JsonProcessingException {
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.registerModule(new EthJsonModule());
+ NodeStats stats = new NodeStats(true, true, true, 42, 23, 5000, 1234567);
+ assertEquals(
+ "{\"active\":true,\"syncing\":true,\"mining\":true,\"hashrate\":42,\"peers\":23,\"gasPrice\":5000,\"uptime\":1234567}",
+ mapper.writeValueAsString(stats));
}
}
diff --git a/toml/src/test/java/org/apache/tuweni/toml/MutableTomlArrayTest.java b/toml/src/test/java/org/apache/tuweni/toml/MutableTomlArrayTest.java
index 4e91ad7..9a10c9f 100644
--- a/toml/src/test/java/org/apache/tuweni/toml/MutableTomlArrayTest.java
+++ b/toml/src/test/java/org/apache/tuweni/toml/MutableTomlArrayTest.java
@@ -18,6 +18,11 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.OffsetDateTime;
+
import org.junit.jupiter.api.Test;
class MutableTomlArrayTest {
@@ -87,4 +92,56 @@ class MutableTomlArrayTest {
assertEquals(positionAt(9, 5), array.inputPositionOf(1));
assertThrows(IndexOutOfBoundsException.class, () -> array.get(2));
}
+
+ @Test
+ void shouldGetDouble() {
+ MutableTomlArray array = new MutableTomlArray().append(23.5d, positionAt(1, 1));
+ assertEquals(23.5d, array.getDouble(0));
+ }
+
+ @Test
+ void shouldGetLong() {
+ MutableTomlArray array = new MutableTomlArray().append(23L, positionAt(1, 1));
+ assertEquals(23L, array.getLong(0));
+ }
+
+ @Test
+ void shouldGetBoolean() {
+ MutableTomlArray array = new MutableTomlArray().append(false, positionAt(1, 1));
+ assertEquals(false, array.getBoolean(0));
+ }
+
+ @Test
+ void shouldGetOffSetDateTime() {
+ OffsetDateTime time = OffsetDateTime.now();
+ MutableTomlArray array = new MutableTomlArray().append(time, positionAt(1, 1));
+ assertEquals(time, array.getOffsetDateTime(0));
+ }
+
+ @Test
+ void shouldGetLocalDateTime() {
+ LocalDateTime time = LocalDateTime.now();
+ MutableTomlArray array = new MutableTomlArray().append(time, positionAt(1, 1));
+ assertEquals(time, array.getLocalDateTime(0));
+ }
+
+ @Test
+ void shouldGetLocalDate() {
+ LocalDate time = LocalDate.now();
+ MutableTomlArray array = new MutableTomlArray().append(time, positionAt(1, 1));
+ assertEquals(time, array.getLocalDate(0));
+ }
+
+ @Test
+ void shouldGetLocalTime() {
+ LocalTime time = LocalTime.now();
+ MutableTomlArray array = new MutableTomlArray().append(time, positionAt(1, 1));
+ assertEquals(time, array.getLocalTime(0));
+ }
+
+ @Test
+ void toJson() {
+ MutableTomlArray array = new MutableTomlArray().append("foo", positionAt(1, 1)).append("bar", positionAt(10, 1));
+ assertEquals("[\n \"foo\",\n \"bar\"\n]\n", array.toJson());
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tuweni.apache.org
For additional commands, e-mail: commits-help@tuweni.apache.org