-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added tests for DFSPrivateKeyServiceImpl and PathEncryptionImpl classes * Added test for RemoveFromPrivateImpl and improved test for PathEncryptionImp class
- Loading branch information
Showing
3 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
...st/java/de/adorsys/datasafe/directory/impl/profile/keys/DFSPrivateKeyServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
|
||
} | ||
|
75 changes: 75 additions & 0 deletions
75
.../test/java/de/adorsys/datasafe/encrypiton/impl/pathencryption/PathEncryptionImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
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.Mock; | ||
|
||
import javax.crypto.SecretKey; | ||
import java.security.KeyStore; | ||
import java.util.function.Function; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class PathEncryptionImplTest extends BaseMockitoTest { | ||
private final KeyStoreService keyStoreService = new KeyStoreServiceImpl( | ||
EncryptionConfig.builder().build().getKeystore(), | ||
DaggerBCJuggler.builder().build() | ||
); | ||
String uriString = "https://192.168.178.0.1:9090/minio/first/folder"; | ||
PathEncryptionImpl pathEncryption; | ||
@Mock | ||
private SymmetricPathEncryptionService symmetricPathEncryptionService; | ||
@Mock | ||
private PrivateKeyService privateKeyService; | ||
|
||
@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(symmetricPathEncryptionService.decrypt(any(), any())).thenReturn(new Uri(uriString)); | ||
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")); | ||
|
||
Function<Uri, Uri> decrypt = pathEncryption.decryptor(userAuth); | ||
Uri decryptedPath = decrypt.apply(encryptedPath); | ||
Assertions.assertEquals(decryptedPath, new Uri(uriString)); | ||
|
||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...rc/test/java/de/adorsys/datasafe/privatestore/impl/actions/RemoveFromPrivateImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package de.adorsys.datasafe.privatestore.impl.actions; | ||
|
||
|
||
import de.adorsys.datasafe.encrypiton.api.types.UserID; | ||
import de.adorsys.datasafe.encrypiton.api.types.UserIDAuth; | ||
import de.adorsys.datasafe.privatestore.api.actions.EncryptedResourceResolver; | ||
import de.adorsys.datasafe.storage.api.actions.StorageRemoveService; | ||
import de.adorsys.datasafe.types.api.actions.RemoveRequest; | ||
import de.adorsys.datasafe.types.api.resource.AbsoluteLocation; | ||
import de.adorsys.datasafe.types.api.resource.BasePrivateResource; | ||
import de.adorsys.datasafe.types.api.resource.PrivateResource; | ||
import de.adorsys.datasafe.types.api.resource.Uri; | ||
import de.adorsys.datasafe.types.api.shared.BaseMockitoTest; | ||
import de.adorsys.datasafe.types.api.utils.ReadKeyPasswordTestFactory; | ||
import lombok.SneakyThrows; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
|
||
import java.net.URI; | ||
|
||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class RemoveFromPrivateImplTest extends BaseMockitoTest { | ||
private static final URI ABSOLUTE_PATH = URI.create("s3://absolute"); | ||
private static final String PATH = "./"; | ||
private final UserIDAuth auth = new UserIDAuth(new UserID(""), ReadKeyPasswordTestFactory.getForString("")); | ||
@Mock | ||
private EncryptedResourceResolver resolver; | ||
@Mock | ||
private StorageRemoveService removeService; | ||
private RemoveFromPrivateImpl removeFromPrivate; | ||
|
||
@Test | ||
@SneakyThrows | ||
void removePrivate() { | ||
removeFromPrivate = new RemoveFromPrivateImpl(resolver, removeService); | ||
AbsoluteLocation<PrivateResource> resource = BasePrivateResource.forAbsolutePrivate(ABSOLUTE_PATH); | ||
RemoveRequest<UserIDAuth, PrivateResource> removeReq = RemoveRequest.forDefaultPrivate(auth, new Uri(PATH)); | ||
when(resolver.encryptAndResolvePath(removeReq.getOwner(), removeReq.getLocation(), removeReq.getStorageIdentifier())) | ||
.thenReturn(resource); | ||
removeFromPrivate.remove(removeReq); | ||
|
||
verify(removeService).remove(resource); | ||
} | ||
} |