forked from TrueCloudLab/frostfs-sdk-java
[#6] cover the cryptography module with junit tests
Signed-off-by: Ori Bruk <o.bruk@yadro.com>
This commit is contained in:
parent
0d76332f01
commit
c9a54d56fb
8 changed files with 432 additions and 34 deletions
|
@ -0,0 +1,66 @@
|
|||
package info.frostfs.sdk;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
public class ArrayHelperTest {
|
||||
|
||||
@Test
|
||||
void concatTest_success() {
|
||||
//Given
|
||||
var startBytes = new byte[]{1, 2, 3, 4, 5};
|
||||
var endBytes = new byte[]{6, 7, 8, 9};
|
||||
|
||||
//When
|
||||
var result = ArrayHelper.concat(startBytes, endBytes);
|
||||
|
||||
//Then
|
||||
assertThat(startBytes).hasSize(5);
|
||||
assertThat(endBytes).hasSize(4);
|
||||
assertThat(result).hasSize(9).startsWith(startBytes).endsWith(endBytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
void concatTest_endArrayIsEmpty() {
|
||||
//Given
|
||||
var startBytes = new byte[]{1, 2, 3, 4, 5};
|
||||
var endBytes = new byte[]{};
|
||||
|
||||
//When
|
||||
var result = ArrayHelper.concat(startBytes, endBytes);
|
||||
|
||||
//Then
|
||||
assertThat(startBytes).hasSize(5);
|
||||
assertThat(endBytes).hasSize(0);
|
||||
assertThat(result).hasSize(5).startsWith(startBytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
void concatTest_startArrayIsEmpty() {
|
||||
//Given
|
||||
var startBytes = new byte[]{};
|
||||
var endBytes = new byte[]{6, 7, 8, 9};
|
||||
|
||||
//When
|
||||
var result = ArrayHelper.concat(startBytes, endBytes);
|
||||
|
||||
//Then
|
||||
assertThat(startBytes).hasSize(0);
|
||||
assertThat(endBytes).hasSize(4);
|
||||
assertThat(result).hasSize(4).startsWith(endBytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
void concatTest_givenParamsIsNull() {
|
||||
//Given
|
||||
var startBytes = new byte[]{1, 2, 3, 4, 5};
|
||||
var endBytes = new byte[]{6, 7, 8, 9};
|
||||
|
||||
//When + Then
|
||||
assertThrows(IllegalArgumentException.class, () -> ArrayHelper.concat(startBytes, null));
|
||||
assertThrows(IllegalArgumentException.class, () -> ArrayHelper.concat(null, endBytes));
|
||||
assertThrows(IllegalArgumentException.class, () -> ArrayHelper.concat(null, null));
|
||||
}
|
||||
}
|
54
cryptography/src/test/java/info/frostfs/sdk/Base58Test.java
Normal file
54
cryptography/src/test/java/info/frostfs/sdk/Base58Test.java
Normal file
|
@ -0,0 +1,54 @@
|
|||
package info.frostfs.sdk;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
public class Base58Test {
|
||||
private static final String WIF = "L1YS4myg3xHPvi3FHeLaEt7G8upwJaWL5YLV7huviuUtXFpzBMqZ";
|
||||
private static final byte[] DECODE = new byte[]{
|
||||
-128, -128, -5, 30, -36, -118, 85, -67, -6, 81, 43, 93, -38, 106, 21, -88, 127, 15, 125, -79, -17, -40, 77,
|
||||
-15, 122, -88, 72, 109, -47, 125, -80, -40, -38, 1
|
||||
};
|
||||
|
||||
@Test
|
||||
void base58DecodeEncode() {
|
||||
//When + Then
|
||||
assertEquals(WIF, Base58.base58CheckEncode(Base58.base58CheckDecode(WIF)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void base58Decode_success() {
|
||||
//When
|
||||
var decode = Base58.base58CheckDecode(WIF);
|
||||
|
||||
//Then
|
||||
assertThat(decode).hasSize(34).containsExactly(DECODE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void base58Decode_wrong() {
|
||||
//When + Then
|
||||
assertThrows(IllegalArgumentException.class, () -> Base58.base58CheckDecode(null));
|
||||
assertThrows(IllegalArgumentException.class, () -> Base58.base58CheckDecode(""));
|
||||
assertThrows(IllegalArgumentException.class, () -> Base58.base58CheckDecode("WIF"));
|
||||
assertThrows(IllegalArgumentException.class, () -> Base58.base58CheckDecode("fh"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void base58Encode_success() {
|
||||
//When
|
||||
var encode = Base58.base58CheckEncode(DECODE);
|
||||
|
||||
//Then
|
||||
assertEquals(WIF, encode);
|
||||
}
|
||||
|
||||
@Test
|
||||
void base58Encode_wrong() {
|
||||
//When + Then
|
||||
assertThrows(IllegalArgumentException.class, () -> Base58.base58CheckEncode(null));
|
||||
}
|
||||
}
|
110
cryptography/src/test/java/info/frostfs/sdk/HelperTest.java
Normal file
110
cryptography/src/test/java/info/frostfs/sdk/HelperTest.java
Normal file
|
@ -0,0 +1,110 @@
|
|||
package info.frostfs.sdk;
|
||||
|
||||
import com.google.protobuf.Message;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
public class HelperTest {
|
||||
|
||||
@Test
|
||||
void getRipemd160_success() {
|
||||
//Given
|
||||
var value = new byte[]{1, 2, 3, 4, 5};
|
||||
var expected = new byte[]
|
||||
{-21, -126, 92, 75, 36, -12, 37, 7, 122, 6, 124, -61, -66, -12, 87, 120, 63, 90, -41, 5};
|
||||
//When
|
||||
var result = Helper.getRipemd160(value);
|
||||
|
||||
//Then
|
||||
assertThat(result).hasSize(20).containsExactly(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getRipemd160_givenParamIsNull() {
|
||||
//When + Then
|
||||
assertThrows(IllegalArgumentException.class, () -> Helper.getRipemd160(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getRipemd160_givenParamsIsEmpty() {
|
||||
//Given
|
||||
var value = new byte[]{};
|
||||
var expected = new byte[]
|
||||
{-100, 17, -123, -91, -59, -23, -4, 84, 97, 40, 8, -105, 126, -24, -11, 72, -78, 37, -115, 49};
|
||||
//When
|
||||
var result = Helper.getRipemd160(value);
|
||||
|
||||
//Then
|
||||
assertThat(result).hasSize(20).containsExactly(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getSha256Instance() {
|
||||
//When
|
||||
var result = Helper.getSha256Instance();
|
||||
|
||||
//Then
|
||||
assertEquals("SHA-256", result.getAlgorithm());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getSha256_bytes_success() {
|
||||
//Given
|
||||
var value = new byte[]{1, 2, 3, 4, 5};
|
||||
var expected = new byte[]{
|
||||
116, -8, 31, -31, 103, -39, -101, 76, -76, 29, 109, 12, -51, -88, 34, 120, -54, -18, -97, 62, 47, 37,
|
||||
-43, -27, -93, -109, 111, -13, -36, -20, 96, -48
|
||||
};
|
||||
|
||||
//When
|
||||
var result = Helper.getSha256(value);
|
||||
|
||||
//Then
|
||||
assertThat(result).hasSize(32).containsExactly(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getSha256_bytes_givenParamIsNull() {
|
||||
//When + Then
|
||||
assertThrows(IllegalArgumentException.class, () -> Helper.getSha256((byte[]) null));
|
||||
assertThrows(IllegalArgumentException.class, () -> Helper.getSha256((Message) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getSha256_bytes_givenParamsIsEmpty() {
|
||||
//Given
|
||||
var value = new byte[]{};
|
||||
var expected = new byte[]{
|
||||
-29, -80, -60, 66, -104, -4, 28, 20, -102, -5, -12, -56, -103, 111, -71, 36, 39, -82, 65, -28, 100,
|
||||
-101, -109, 76, -92, -107, -103, 27, 120, 82, -72, 85
|
||||
};
|
||||
|
||||
//When
|
||||
var result = Helper.getSha256(value);
|
||||
|
||||
//Then
|
||||
assertThat(result).hasSize(32).containsExactly(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getHexString_success() {
|
||||
//Given
|
||||
var value = new byte[]{1, 2, 3, 4, 5};
|
||||
|
||||
//When
|
||||
var result = Helper.getHexString(value);
|
||||
|
||||
//Then
|
||||
assertEquals("0102030405", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getHexString_wrong() {
|
||||
//When + Then
|
||||
assertThrows(IllegalArgumentException.class, () -> Helper.getHexString(null));
|
||||
assertThrows(IllegalArgumentException.class, () -> Helper.getHexString(new byte[]{}));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,151 @@
|
|||
package info.frostfs.sdk;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
public class KeyExtensionTest {
|
||||
private static final String WIF = "L1YS4myg3xHPvi3FHeLaEt7G8upwJaWL5YLV7huviuUtXFpzBMqZ";
|
||||
private static final String OWNER_ID = "NVxUSpEEJzYXZZtUs18PrJTD9QZkLLNQ8S";
|
||||
private static final byte[] PRIVATE_KEY = new byte[]{
|
||||
-128, -5, 30, -36, -118, 85, -67, -6, 81, 43, 93, -38, 106, 21, -88, 127, 15, 125, -79, -17, -40, 77, -15,
|
||||
122, -88, 72, 109, -47, 125, -80, -40, -38
|
||||
};
|
||||
private static final byte[] PUBLIC_KEY = new byte[]{
|
||||
3, 80, -111, 65, 23, 36, -4, -69, 80, 102, 98, -13, 13, -79, 24, 126, -116, 90, 56, 44, 127, 106, -125, 99,
|
||||
-48, -94, 104, 81, 120, -110, 30, 80, 70
|
||||
};
|
||||
private static final byte[] UNCOMPRESSED_PUBLIC_KEY = new byte[]{
|
||||
4, 80, -111, 65, 23, 36, -4, -69, 80, 102, 98, -13, 13, -79, 24, 126, -116, 90, 56, 44, 127, 106, -125, 99,
|
||||
-48, -94, 104, 81, 120, -110, 30, 80, 70, 76, -18, 53, -9, 79, -8, -25, -69, 12, 89, -103, 15, 126, 118,
|
||||
-68, -73, 65, -57, -26, 75, 4, -51, -40, -20, 75, 89, -59, 111, 96, -80, 56, 13
|
||||
};
|
||||
|
||||
@Test
|
||||
void getPrivateKeyFromWIF_success() {
|
||||
//When
|
||||
var privateKey = KeyExtension.getPrivateKeyFromWIF(WIF);
|
||||
|
||||
//Then
|
||||
assertThat(privateKey).hasSize(32).containsExactly(PRIVATE_KEY);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getPrivateKeyFromWIF_wrong() {
|
||||
//When + Then
|
||||
assertThrows(IllegalArgumentException.class, () -> KeyExtension.getPrivateKeyFromWIF(""));
|
||||
assertThrows(IllegalArgumentException.class, () -> KeyExtension.getPrivateKeyFromWIF(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadPublicKey_success() {
|
||||
//When
|
||||
var publicKey = KeyExtension.loadPublicKey(PRIVATE_KEY);
|
||||
|
||||
//Then
|
||||
assertThat(publicKey).hasSize(33).containsExactly(PUBLIC_KEY);
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadPublicKey_wrong() {
|
||||
//When + Then
|
||||
assertThrows(IllegalArgumentException.class, () -> KeyExtension.loadPublicKey(null));
|
||||
assertThrows(IllegalArgumentException.class, () -> KeyExtension.loadPublicKey(new byte[]{}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadPrivateKey_success() {
|
||||
//When
|
||||
var privateKey = KeyExtension.loadPrivateKey(PRIVATE_KEY);
|
||||
|
||||
//Then
|
||||
assertThat(privateKey.getEncoded()).hasSize(67).endsWith(PRIVATE_KEY);
|
||||
assertEquals("EC", privateKey.getAlgorithm());
|
||||
assertEquals("PKCS#8", privateKey.getFormat());
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadPrivateKey_wrong() {
|
||||
//When + Then
|
||||
assertThrows(IllegalArgumentException.class, () -> KeyExtension.loadPrivateKey(null));
|
||||
assertThrows(IllegalArgumentException.class, () -> KeyExtension.loadPrivateKey(new byte[]{}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getPublicKeyFromBytes_success() {
|
||||
//When
|
||||
var publicKey = KeyExtension.getPublicKeyFromBytes(PUBLIC_KEY);
|
||||
|
||||
//Then
|
||||
assertThat(publicKey.getEncoded()).hasSize(91).endsWith(UNCOMPRESSED_PUBLIC_KEY);
|
||||
assertEquals("EC", publicKey.getAlgorithm());
|
||||
assertEquals("X.509", publicKey.getFormat());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getPublicKeyFromBytes_wrong() {
|
||||
//When + Then
|
||||
assertThrows(IllegalArgumentException.class, () -> KeyExtension.getPublicKeyFromBytes(null));
|
||||
assertThrows(IllegalArgumentException.class, () -> KeyExtension.getPublicKeyFromBytes(new byte[]{}));
|
||||
assertThrows(IllegalArgumentException.class, () -> KeyExtension.getPublicKeyFromBytes(PRIVATE_KEY));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getScriptHash_success() {
|
||||
//Given
|
||||
var expected = new byte[]{
|
||||
110, 42, -125, -76, -25, -44, -94, 22, -98, 117, -100, -5, 103, 74, -128, -51, 37, -116, -102, 71
|
||||
};
|
||||
|
||||
//When
|
||||
var hash = KeyExtension.getScriptHash(PUBLIC_KEY);
|
||||
|
||||
//Then
|
||||
assertThat(hash).hasSize(20).containsExactly(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getScriptHash_wrong() {
|
||||
//When + Then
|
||||
assertThrows(IllegalArgumentException.class, () -> KeyExtension.getScriptHash(null));
|
||||
assertThrows(IllegalArgumentException.class, () -> KeyExtension.getScriptHash(new byte[]{}));
|
||||
assertThrows(IllegalArgumentException.class, () -> KeyExtension.getScriptHash(PRIVATE_KEY));
|
||||
}
|
||||
|
||||
@Test
|
||||
void publicKeyToAddress_success() {
|
||||
//When
|
||||
var address = KeyExtension.publicKeyToAddress(PUBLIC_KEY);
|
||||
|
||||
//Then
|
||||
assertEquals(OWNER_ID, address);
|
||||
}
|
||||
|
||||
@Test
|
||||
void publicKeyToAddress_wrong() {
|
||||
//When + Then
|
||||
assertThrows(IllegalArgumentException.class, () -> KeyExtension.publicKeyToAddress(null));
|
||||
assertThrows(IllegalArgumentException.class, () -> KeyExtension.publicKeyToAddress(new byte[]{}));
|
||||
assertThrows(IllegalArgumentException.class, () -> KeyExtension.publicKeyToAddress(PRIVATE_KEY));
|
||||
}
|
||||
|
||||
@Test
|
||||
void compress_success() {
|
||||
//When
|
||||
var publicKey = KeyExtension.compress(UNCOMPRESSED_PUBLIC_KEY);
|
||||
|
||||
//Then
|
||||
assertThat(publicKey).hasSize(33).containsExactly(PUBLIC_KEY);
|
||||
}
|
||||
|
||||
@Test
|
||||
void compress_wrong() {
|
||||
//When + Then
|
||||
assertThrows(IllegalArgumentException.class, () -> KeyExtension.compress(null));
|
||||
assertThrows(IllegalArgumentException.class, () -> KeyExtension.compress(new byte[]{}));
|
||||
assertThrows(IllegalArgumentException.class, () -> KeyExtension.compress(PUBLIC_KEY));
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue