forked from TrueCloudLab/frostfs-sdk-java
152 lines
5.6 KiB
Java
152 lines
5.6 KiB
Java
package info.frostfs.sdk;
|
|
|
|
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
|
|
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(ValidationFrostFSException.class, () -> KeyExtension.getPrivateKeyFromWIF(""));
|
|
assertThrows(ValidationFrostFSException.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(ValidationFrostFSException.class, () -> KeyExtension.loadPublicKey(null));
|
|
assertThrows(ValidationFrostFSException.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(ValidationFrostFSException.class, () -> KeyExtension.loadPrivateKey(null));
|
|
assertThrows(ValidationFrostFSException.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(ValidationFrostFSException.class, () -> KeyExtension.getPublicKeyFromBytes(null));
|
|
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.getPublicKeyFromBytes(new byte[]{}));
|
|
assertThrows(ValidationFrostFSException.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(ValidationFrostFSException.class, () -> KeyExtension.getScriptHash(null));
|
|
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.getScriptHash(new byte[]{}));
|
|
assertThrows(ValidationFrostFSException.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(ValidationFrostFSException.class, () -> KeyExtension.publicKeyToAddress(null));
|
|
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.publicKeyToAddress(new byte[]{}));
|
|
assertThrows(ValidationFrostFSException.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(ValidationFrostFSException.class, () -> KeyExtension.compress(null));
|
|
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.compress(new byte[]{}));
|
|
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.compress(PUBLIC_KEY));
|
|
}
|
|
|
|
}
|