Skip to content

Commit

Permalink
add unit testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasMLK committed Jul 3, 2021
1 parent 85c948d commit 17a0ecd
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 1 deletion.
53 changes: 53 additions & 0 deletions src/test/java/io/xdag/core/AesTest.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
2 changes: 1 addition & 1 deletion src/test/java/io/xdag/crypto/SampleKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
94 changes: 94 additions & 0 deletions src/test/java/io/xdag/crypto/TuweniCryptoTest.java
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit 17a0ecd

Please sign in to comment.