From 0f44ada389e6e168fdfe77fd029877737b3e775b Mon Sep 17 00:00:00 2001
From: Martin Vogel <martin.vogel@sinc.de>
Date: Thu, 22 Jun 2023 12:49:48 +0200
Subject: [PATCH] refactor(#664): add more tests

---
 tools/pom.xml                                 |  5 ++
 .../sdk/tools/JWKGeneratorTest.java           | 57 +++++++++++++++++++
 2 files changed, 62 insertions(+)

diff --git a/tools/pom.xml b/tools/pom.xml
index 37fb3a643..42ef6bb62 100644
--- a/tools/pom.xml
+++ b/tools/pom.xml
@@ -40,6 +40,11 @@
             <artifactId>hamcrest-all</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>dev.fitko.fitconnect.sdk</groupId>
+            <artifactId>core</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>
diff --git a/tools/src/test/java/dev/fitko/fitconnect/sdk/tools/JWKGeneratorTest.java b/tools/src/test/java/dev/fitko/fitconnect/sdk/tools/JWKGeneratorTest.java
index d27e7c386..681ee72c2 100644
--- a/tools/src/test/java/dev/fitko/fitconnect/sdk/tools/JWKGeneratorTest.java
+++ b/tools/src/test/java/dev/fitko/fitconnect/sdk/tools/JWKGeneratorTest.java
@@ -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() {
 
-- 
GitLab