2019-02-13 12:33:29 +01:00
|
|
|
package org.gcube.informationsystem.resourceregistry.er;
|
|
|
|
|
|
|
|
import java.security.Key;
|
|
|
|
|
|
|
|
import org.gcube.informationsystem.model.impl.properties.EncryptedImpl;
|
|
|
|
import org.gcube.informationsystem.model.reference.properties.Encrypted;
|
2019-10-28 15:06:31 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.ContextTest;
|
2019-02-13 12:33:29 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment;
|
|
|
|
import org.junit.Assert;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
2019-10-28 15:06:31 +01:00
|
|
|
public class EncryptionTest extends ContextTest {
|
2019-02-13 12:33:29 +01:00
|
|
|
|
|
|
|
public static final String PLAIN_VALUE = "my-test";
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void test() throws Exception {
|
|
|
|
|
|
|
|
Encrypted encrypted = new EncryptedImpl();
|
|
|
|
String contextEncryptedValue = EncryptedImpl.encrypt(PLAIN_VALUE);
|
|
|
|
encrypted.setEncryptedValue(contextEncryptedValue);
|
|
|
|
|
|
|
|
String getContextEncryptedValue = encrypted.getEncryptedValue();
|
|
|
|
Assert.assertTrue(getContextEncryptedValue.compareTo(getContextEncryptedValue)==0);
|
|
|
|
|
|
|
|
|
|
|
|
// Decrypting with Context Key (default key)
|
|
|
|
String decryptedValue = EncryptedImpl.decrypt(contextEncryptedValue);
|
|
|
|
Assert.assertTrue(decryptedValue.compareTo(PLAIN_VALUE)==0);
|
|
|
|
|
|
|
|
// Encrypting with DB Key
|
|
|
|
Key databaseKey = DatabaseEnvironment.getDatabaseKey();
|
|
|
|
String dbEncryptedValue = EncryptedImpl.encrypt(decryptedValue, databaseKey);
|
|
|
|
|
|
|
|
// Setting the value encrypted with DB key
|
|
|
|
encrypted.setEncryptedValue(dbEncryptedValue);
|
|
|
|
|
|
|
|
String getDBEncryptedValue = encrypted.getEncryptedValue();
|
|
|
|
Assert.assertTrue(getDBEncryptedValue.compareTo(dbEncryptedValue)==0);
|
|
|
|
|
|
|
|
decryptedValue = EncryptedImpl.decrypt(getDBEncryptedValue, databaseKey);
|
|
|
|
Assert.assertTrue(decryptedValue.compareTo(PLAIN_VALUE)==0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|