Skip to content

Commit

Permalink
Added test for RemoveFromPrivateImpl and improved test for PathEncryp…
Browse files Browse the repository at this point in the history
…tionImp class
  • Loading branch information
Thendo20 committed Jul 22, 2024
1 parent 1f08bad commit ee0493c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,27 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.*;
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 {
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()
);
String uriString = "https://192.168.178.0.1:9090/minio/first/folder";
PathEncryptionImpl pathEncryption;
@Mock
private SymmetricPathEncryptionService symmetricPathEncryptionService;
@Mock
private PrivateKeyService privateKeyService;

PathEncryptionImpl pathEncryption;



@BeforeEach
void setUp() {
pathEncryption = new PathEncryptionImpl(symmetricPathEncryptionService, privateKeyService);
Expand All @@ -53,19 +51,25 @@ public void testPathEncryption() {
KeyID keyID = new KeyID("secret");

KeyCreationConfig config = KeyCreationConfig.builder().signKeyNumber(0).encKeyNumber(1).build();
KeyStore keystore = keyStoreService.createKeyStore(keyStoreAuth,config);
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));
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));

}
}
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);
}
}

0 comments on commit ee0493c

Please sign in to comment.