Skip to content
Snippets Groups Projects
Commit 0f44ada3 authored by Martin Vogel's avatar Martin Vogel
Browse files

refactor(#664): add more tests

parent 4fc75111
No related branches found
No related tags found
1 merge request!211#664 Test JWK Generator
...@@ -40,6 +40,11 @@ ...@@ -40,6 +40,11 @@
<artifactId>hamcrest-all</artifactId> <artifactId>hamcrest-all</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>dev.fitko.fitconnect.sdk</groupId>
<artifactId>core</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
...@@ -5,17 +5,23 @@ import com.nimbusds.jose.JWSAlgorithm; ...@@ -5,17 +5,23 @@ import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.jwk.JWK; import com.nimbusds.jose.jwk.JWK;
import com.nimbusds.jose.jwk.KeyOperation; import com.nimbusds.jose.jwk.KeyOperation;
import com.nimbusds.jose.jwk.KeyType; 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.hamcrest.CoreMatchers;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.util.Map; import java.util.Map;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
class JWKGeneratorTest { class JWKGeneratorTest {
...@@ -82,6 +88,42 @@ class JWKGeneratorTest { ...@@ -82,6 +88,42 @@ class JWKGeneratorTest {
assertThat(keyParams.get("qi"), CoreMatchers.is(CoreMatchers.notNullValue())); 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 @Test
void testPublicSignatureVerificationKey() { void testPublicSignatureVerificationKey() {
...@@ -105,6 +147,21 @@ class JWKGeneratorTest { ...@@ -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 @Test
void testPrivateSigningKey() { void testPrivateSigningKey() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment