Adding test to cover EC private key generation.

Signed-off-by: Max Lambrecht <maxlambrecht@gmail.com>
This commit is contained in:
Max Lambrecht 2020-05-22 09:53:30 -03:00
parent ef2cdafab9
commit cd64eb7966
1 changed files with 18 additions and 1 deletions

View File

@ -1,9 +1,11 @@
package spiffe.internal; package spiffe.internal;
import com.nimbusds.jose.jwk.Curve;
import lombok.val; import lombok.val;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import spiffe.spiffeid.SpiffeId; import spiffe.spiffeid.SpiffeId;
import spiffe.spiffeid.TrustDomain; import spiffe.spiffeid.TrustDomain;
import spiffe.utils.TestUtils;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
@ -11,6 +13,7 @@ import java.net.URISyntaxException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey; import java.security.PrivateKey;
import java.security.cert.CertPathValidatorException; import java.security.cert.CertPathValidatorException;
@ -63,7 +66,7 @@ public class CertificateUtilsTest {
} }
@Test @Test
void testGeneratePrivateKey() throws URISyntaxException, IOException { void testGenerateiRsaPrivateKeyFromBytes() throws URISyntaxException, IOException {
val keyPath = Paths.get(toUri("testdata/internal/privateKeyRsa.pem")); val keyPath = Paths.get(toUri("testdata/internal/privateKeyRsa.pem"));
val keyBytes = Files.readAllBytes(keyPath); val keyBytes = Files.readAllBytes(keyPath);
@ -76,6 +79,20 @@ public class CertificateUtilsTest {
} }
} }
@Test
void testGenerateEcPrivateKeyFromBytes() {
KeyPair ecKeyPair = TestUtils.generateECKeyPair(Curve.P_256);
byte[] keyBytes = ecKeyPair.getPrivate().getEncoded();
try {
PrivateKey privateKey = CertificateUtils.generatePrivateKey(keyBytes);
assertNotNull(privateKey);
assertEquals("EC", privateKey.getAlgorithm());
} catch (InvalidKeySpecException | InvalidKeyException | NoSuchAlgorithmException e) {
fail("Should have generated key", e);
}
}
@Test @Test
void testGetSpiffeId() throws Exception { void testGetSpiffeId() throws Exception {
val rootCa = createRootCA("C = US, O = SPIFFE", "spiffe://domain.test" ); val rootCa = createRootCA("C = US, O = SPIFFE", "spiffe://domain.test" );