From 17a0ecdd1da4ce1c5b3b577abe1f576effc8ea99 Mon Sep 17 00:00:00 2001 From: holt666 Date: Sat, 3 Jul 2021 20:55:22 +0800 Subject: [PATCH] add unit testcase --- src/test/java/io/xdag/core/AesTest.java | 53 +++++++++++ src/test/java/io/xdag/crypto/SampleKeys.java | 2 +- .../java/io/xdag/crypto/TuweniCryptoTest.java | 94 +++++++++++++++++++ 3 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 src/test/java/io/xdag/core/AesTest.java create mode 100644 src/test/java/io/xdag/crypto/TuweniCryptoTest.java diff --git a/src/test/java/io/xdag/core/AesTest.java b/src/test/java/io/xdag/core/AesTest.java new file mode 100644 index 00000000..b5eb863f --- /dev/null +++ b/src/test/java/io/xdag/core/AesTest.java @@ -0,0 +1,53 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2020-2030 The XdagJ Developers + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package io.xdag.core; + +import org.apache.tuweni.bytes.Bytes; +import org.junit.Test; +import static org.junit.Assert.assertArrayEquals; + +import java.nio.charset.StandardCharsets; + +import io.xdag.crypto.Aes; + +public class AesTest { + + private static Bytes raw = Bytes.wrap("test".getBytes(StandardCharsets.UTF_8)); + private static Bytes key = Bytes.fromHexString("1122334455667788112233445566778811223344556677881122334455667788"); + private static Bytes iv = Bytes.fromHexString("11223344556677881122334455667788"); + private static Bytes encrypted = Bytes.fromHexString("182b93aa58d6291381660e5bad673dd4"); + + @Test + public void testEncrypt() { + byte[] bytes = Aes.encrypt(raw.toArray(), key.toArray(), iv.toArray()); + assertArrayEquals(encrypted.toArray(), bytes); + } + + @Test + public void testDecrypt() { + byte[] bytes = Aes.decrypt(encrypted.toArray(), key.toArray(), iv.toArray()); + assertArrayEquals(raw.toArray(), bytes); + } + +} diff --git a/src/test/java/io/xdag/crypto/SampleKeys.java b/src/test/java/io/xdag/crypto/SampleKeys.java index 2349382b..688d4537 100644 --- a/src/test/java/io/xdag/crypto/SampleKeys.java +++ b/src/test/java/io/xdag/crypto/SampleKeys.java @@ -35,7 +35,7 @@ public class SampleKeys { public static final String PRIVATE_KEY_STRING = "a392604efc2fad9c0b3da43b5f698a2e3f270f170d859912be0d54742275c5f6"; public static final String PUBLIC_KEY_STRING = - "0x506bc1dc099358e5137292f4efdd57e400f29ba5132aa5d12b18dac1c1f6aab" + "506bc1dc099358e5137292f4efdd57e400f29ba5132aa5d12b18dac1c1f6aab" + "a645c0b7b58158babbfa6c6cd5a48aa7340a8749176b120e8516216787a13dc76"; public static final String PUBLIC_KEY_COMPRESS_STRING = "02506bc1dc099358e5137292f4efdd57e400f29ba5132aa5d12b18dac1c1f6aaba"; diff --git a/src/test/java/io/xdag/crypto/TuweniCryptoTest.java b/src/test/java/io/xdag/crypto/TuweniCryptoTest.java new file mode 100644 index 00000000..c36732e2 --- /dev/null +++ b/src/test/java/io/xdag/crypto/TuweniCryptoTest.java @@ -0,0 +1,94 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2020-2030 The XdagJ Developers + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package io.xdag.crypto; + +import static org.junit.Assert.*; + +import java.security.Security; + +import org.apache.tuweni.bytes.Bytes; +import org.apache.tuweni.bytes.Bytes32; +import org.apache.tuweni.crypto.SECP256K1; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.Test; + +public class TuweniCryptoTest { + + static { + if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) { + Security.addProvider(new BouncyCastleProvider()); + } + } + + @Test + public void createKeyPairRandom() { + SECP256K1.KeyPair keyPair = SECP256K1.KeyPair.random(); + assertNotNull(keyPair); + } + + @Test + public void createSecretKeyFromIntegerTest() { + SECP256K1.SecretKey privKey = SECP256K1.SecretKey.fromInteger(SampleKeys.PRIVATE_KEY); + assertEquals(privKey.bytes().toUnprefixedHexString(), SampleKeys.PRIVATE_KEY_STRING); + } + + @Test + public void createSecretKeyFromBytesTest() { + SECP256K1.SecretKey privKey = SECP256K1.SecretKey.fromBytes(Bytes32.fromHexString(SampleKeys.PRIVATE_KEY_STRING)); + assertEquals(privKey.bytes().toUnprefixedHexString(), SampleKeys.PRIVATE_KEY_STRING); + } + + @Test + public void createPublicKeyFromIntegerTest() { + SECP256K1.PublicKey publicKey = SECP256K1.PublicKey.fromInteger(SampleKeys.PUBLIC_KEY); + assertEquals(publicKey.bytes().toUnprefixedHexString(), SampleKeys.PUBLIC_KEY_STRING); + } + + @Test + public void createPublicKeyFromBytesTest() { + SECP256K1.PublicKey publicKey = SECP256K1.PublicKey.fromBytes(Bytes.fromHexString(SampleKeys.PUBLIC_KEY_STRING)); + assertEquals(publicKey.bytes().toUnprefixedHexString(), SampleKeys.PUBLIC_KEY_STRING); + } + + @Test + public void createPublicKeyFromSecretKeyTest() { + SECP256K1.SecretKey privKey = SECP256K1.SecretKey.fromBytes(Bytes32.fromHexString(SampleKeys.PRIVATE_KEY_STRING)); + SECP256K1.PublicKey publicKey = SECP256K1.PublicKey.fromSecretKey(privKey); + assertEquals(publicKey.bytes().toUnprefixedHexString(), SampleKeys.PUBLIC_KEY_STRING); + } + + @Test + public void createPublicKeyFromHexStringTest() { + SECP256K1.PublicKey publicKey = SECP256K1.PublicKey.fromHexString(SampleKeys.PUBLIC_KEY_STRING); + assertEquals(publicKey.bytes().toUnprefixedHexString(), SampleKeys.PUBLIC_KEY_STRING); + } + + @Test + public void createPublicKeyCompressFromIntegerTest() { + SECP256K1.PublicKey publicKey = SECP256K1.PublicKey.fromInteger(SampleKeys.PUBLIC_KEY); + Bytes pubBytes = Bytes.wrap(publicKey.asEcPoint().getEncoded(true)); + assertEquals(pubBytes.toUnprefixedHexString(), SampleKeys.PUBLIC_KEY_COMPRESS_STRING); + } + +}