Skip to content
Snippets Groups Projects

#664 Test JWK Generator

Merged Martin Vogel requested to merge feature/664-test-cert-generator into main
2 files
+ 62
0
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -5,17 +5,23 @@ import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.jwk.JWK;
import com.nimbusds.jose.jwk.KeyOperation;
import com.nimbusds.jose.jwk.KeyType;
import dev.fitko.fitconnect.core.crypto.JWECryptoService;
import dev.fitko.fitconnect.jwkvalidator.JWKValidator;
import dev.fitko.fitconnect.jwkvalidator.exceptions.LogLevel;
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
class JWKGeneratorTest {
@@ -82,6 +88,42 @@ class JWKGeneratorTest {
assertThat(keyParams.get("qi"), CoreMatchers.is(CoreMatchers.notNullValue()));
}
@Test
void testEncryptionAndDecryption() {
// Given
final JWKPair encryptionKeyPair = underTest.generateEncryptionKeyPair(4096);
final JWK publicKey = encryptionKeyPair.getPublicKey();
final JWK privateKey = encryptionKeyPair.getPrivateKey();
final var data = "test string to encrypt";
// When
final JWECryptoService cryptoService = new JWECryptoService(null);
final String encryptedData = cryptoService.encryptBytes(publicKey.toRSAKey(), data.getBytes(StandardCharsets.UTF_8));
final byte[] decryptedData = cryptoService.decryptToBytes(privateKey.toRSAKey(), encryptedData);
// Then
assertThat(data, is(new String(decryptedData)));
}
@Test
void testPublicEncryptionKeyValidationWithCorrectKeyLength() {
// Given
final JWKPair encryptionKeyPair = underTest.generateEncryptionKeyPair(4096);
final JWK publicKey = encryptionKeyPair.getPublicKey();
// Then
assertDoesNotThrow(() -> JWKValidator.withoutX5CValidation()
.withErrorLogLevel(LogLevel.ERROR)
.build()
.validate(publicKey.toRSAKey(), KeyOperation.WRAP_KEY));
}
@Test
void testPublicSignatureVerificationKey() {
@@ -105,6 +147,21 @@ class JWKGeneratorTest {
}
@Test
void testPublicSignatureKeyValidation() {
// Given
final JWKPair signatureKeyPair = underTest.generateSignatureKeyPair(4096);
final JWK publicKey = signatureKeyPair.getPublicKey();
// Then
assertDoesNotThrow(() -> JWKValidator.withoutX5CValidation()
.withErrorLogLevel(LogLevel.ERROR)
.build()
.validate(publicKey.toRSAKey(), KeyOperation.VERIFY));
}
@Test
void testPrivateSigningKey() {
Loading