From 1f08bad4a6330c461a8fc39617640c6ec67e8782 Mon Sep 17 00:00:00 2001 From: Thendo20 Date: Tue, 16 Jul 2024 12:48:40 +0200 Subject: [PATCH] Added tests for DFSPrivateKeyServiceImpl and PathEncryptionImpl classes --- .../keys/DFSPrivateKeyServiceImplTest.java | 51 +++++++++++++ .../PathEncryptionImplTest.java | 71 +++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 datasafe-directory/datasafe-directory-impl/src/test/java/de/adorsys/datasafe/directory/impl/profile/keys/DFSPrivateKeyServiceImplTest.java create mode 100644 datasafe-encryption/datasafe-encryption-impl/src/test/java/de/adorsys/datasafe/encrypiton/impl/pathencryption/PathEncryptionImplTest.java diff --git a/datasafe-directory/datasafe-directory-impl/src/test/java/de/adorsys/datasafe/directory/impl/profile/keys/DFSPrivateKeyServiceImplTest.java b/datasafe-directory/datasafe-directory-impl/src/test/java/de/adorsys/datasafe/directory/impl/profile/keys/DFSPrivateKeyServiceImplTest.java new file mode 100644 index 000000000..4e250960e --- /dev/null +++ b/datasafe-directory/datasafe-directory-impl/src/test/java/de/adorsys/datasafe/directory/impl/profile/keys/DFSPrivateKeyServiceImplTest.java @@ -0,0 +1,51 @@ +package de.adorsys.datasafe.directory.impl.profile.keys; + + +import de.adorsys.datasafe.directory.api.profile.keys.DocumentKeyStoreOperations; +import de.adorsys.datasafe.encrypiton.api.keystore.KeyStoreService; +import de.adorsys.datasafe.encrypiton.api.types.UserID; +import de.adorsys.datasafe.encrypiton.api.types.UserIDAuth; +import de.adorsys.datasafe.types.api.shared.BaseMockitoTest; +import de.adorsys.datasafe.types.api.types.ReadKeyPassword; +import lombok.SneakyThrows; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.*; + +import java.security.*; + + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +public class DFSPrivateKeyServiceImplTest extends BaseMockitoTest { + @Mock + private DocumentKeyStoreOperations keyStoreOper; + @Mock + private KeyStoreService keyStoreService; + DFSPrivateKeyServiceImpl privateKeyService; + @BeforeEach + public void setUp() { + privateKeyService = new DFSPrivateKeyServiceImpl(keyStoreOper); + } + + @Test + @SneakyThrows + public void getKeyPair(){ + ReadKeyPassword readKeyPassword = new ReadKeyPassword("keypass".toCharArray()); + UserID user = new UserID("user1"); + UserIDAuth userAuth = new UserIDAuth(user, readKeyPassword); + + KeyPairGenerator KeyGen = KeyPairGenerator.getInstance("RSA"); + KeyPair keyPair = KeyGen.generateKeyPair(); + + when(keyStoreOper.getKeyPair(any())).thenReturn(keyPair); + + KeyPair keyPair1 = privateKeyService.getKeyPair(userAuth); + Assertions.assertEquals(keyPair.getPublic(), keyPair1.getPublic()); + Assertions.assertEquals(keyPair.getPrivate(), keyPair1.getPrivate()); + } + +} + diff --git a/datasafe-encryption/datasafe-encryption-impl/src/test/java/de/adorsys/datasafe/encrypiton/impl/pathencryption/PathEncryptionImplTest.java b/datasafe-encryption/datasafe-encryption-impl/src/test/java/de/adorsys/datasafe/encrypiton/impl/pathencryption/PathEncryptionImplTest.java new file mode 100644 index 000000000..07e98a248 --- /dev/null +++ b/datasafe-encryption/datasafe-encryption-impl/src/test/java/de/adorsys/datasafe/encrypiton/impl/pathencryption/PathEncryptionImplTest.java @@ -0,0 +1,71 @@ +package de.adorsys.datasafe.encrypiton.impl.pathencryption; + +import de.adorsys.datasafe.directory.api.profile.keys.PrivateKeyService; +import de.adorsys.datasafe.encrypiton.api.keystore.KeyStoreService; +import de.adorsys.datasafe.encrypiton.api.pathencryption.encryption.SymmetricPathEncryptionService; +import de.adorsys.datasafe.encrypiton.api.types.UserID; +import de.adorsys.datasafe.encrypiton.api.types.UserIDAuth; +import de.adorsys.datasafe.encrypiton.api.types.encryption.EncryptionConfig; +import de.adorsys.datasafe.encrypiton.api.types.encryption.KeyCreationConfig; +import de.adorsys.datasafe.encrypiton.api.types.keystore.*; +import de.adorsys.datasafe.encrypiton.impl.keystore.KeyStoreServiceImpl; +import de.adorsys.datasafe.types.api.resource.Uri; +import de.adorsys.datasafe.types.api.shared.BaseMockitoTest; +import de.adorsys.datasafe.types.api.types.ReadKeyPassword; +import de.adorsys.datasafe.types.api.types.ReadStorePassword; +import de.adorsys.keymanagement.juggler.services.DaggerBCJuggler; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.*; + +import javax.crypto.SecretKey; +import java.security.KeyStore; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +public class PathEncryptionImplTest extends BaseMockitoTest { + String uriString = "https://192.168.178.0.1:9090/minio/first/folder"; + private final KeyStoreService keyStoreService = new KeyStoreServiceImpl( + EncryptionConfig.builder().build().getKeystore(), + DaggerBCJuggler.builder().build() + ); + @Mock + private SymmetricPathEncryptionService symmetricPathEncryptionService; + @Mock + private PrivateKeyService privateKeyService; + + PathEncryptionImpl pathEncryption; + + + + @BeforeEach + void setUp() { + pathEncryption = new PathEncryptionImpl(symmetricPathEncryptionService, privateKeyService); + } + + @Test + public void testPathEncryption() { + ReadStorePassword storePassword = new ReadStorePassword("storepass"); + ReadKeyPassword readKeyPassword = new ReadKeyPassword("keypass".toCharArray()); + KeyStoreAuth keyStoreAuth = new KeyStoreAuth(storePassword, readKeyPassword); + KeyID keyID = new KeyID("secret"); + + KeyCreationConfig config = KeyCreationConfig.builder().signKeyNumber(0).encKeyNumber(1).build(); + KeyStore keystore = keyStoreService.createKeyStore(keyStoreAuth,config); + KeyStoreAccess keyStoreAccess = new KeyStoreAccess(keystore, keyStoreAuth); + + SecretKey secretKey = keyStoreService.getSecretKey(keyStoreAccess, keyID); + SecretKeyIDWithKey secretKeyID = new SecretKeyIDWithKey(keyID, secretKey); + + when(symmetricPathEncryptionService.encrypt(any(), any())).thenReturn(new Uri(uriString + ".enc")); + when(privateKeyService.pathEncryptionSecretKey(any())).thenReturn(new AuthPathEncryptionSecretKey(secretKeyID,secretKeyID)); + + UserID user = new UserID("user1"); + UserIDAuth userAuth = new UserIDAuth(user, readKeyPassword); + + Uri encryptedPath = pathEncryption.encrypt(userAuth, new Uri(uriString)); + Assertions.assertEquals(encryptedPath, new Uri(uriString + ".enc")); + } +}