122 lines
4.5 KiB
Java
122 lines
4.5 KiB
Java
package org.gcube.informationsystem.impl.properties;
|
|
|
|
import java.security.Key;
|
|
import java.security.SecureRandom;
|
|
|
|
import javax.crypto.KeyGenerator;
|
|
|
|
import org.gcube.informationsystem.ContextTest;
|
|
import org.gcube.informationsystem.base.reference.Element;
|
|
import org.gcube.informationsystem.model.impl.properties.EncryptedImpl;
|
|
import org.gcube.informationsystem.model.reference.properties.Encrypted;
|
|
import org.gcube.informationsystem.model.reference.properties.Property;
|
|
import org.gcube.informationsystem.types.TypeMapper;
|
|
import org.gcube.informationsystem.utils.ElementMapper;
|
|
import org.junit.Assert;
|
|
import org.junit.Test;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
/**
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
*/
|
|
public class EncryptedTest extends ContextTest {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(EncryptedTest.class);
|
|
|
|
@Test
|
|
public void testEncryptDecrypt() throws Exception {
|
|
String value = "myValue";
|
|
logger.debug("Original Value {}", value);
|
|
|
|
String encrypted = EncryptedImpl.encrypt(value);
|
|
logger.debug("Encrypted Value {}", encrypted);
|
|
|
|
String decrypted = EncryptedImpl.decrypt(encrypted);
|
|
logger.debug("Decrypted Value {}", decrypted);
|
|
|
|
Assert.assertTrue(decrypted.compareTo(value)==0);
|
|
}
|
|
|
|
@Test
|
|
public void testEncryptDecryptWithSpecificKey() throws Exception {
|
|
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
|
|
SecureRandom rand = new SecureRandom();
|
|
keyGen.init(256, rand);
|
|
Key key = keyGen.generateKey();
|
|
|
|
String value = "myValue";
|
|
logger.debug("Original Value {}", value);
|
|
|
|
String encrypted = EncryptedImpl.encrypt(value, key);
|
|
logger.debug("Encrypted Value {}", encrypted);
|
|
|
|
String decrypted = EncryptedImpl.decrypt(encrypted, key);
|
|
logger.debug("Decrypted Value {}", decrypted);
|
|
|
|
Assert.assertTrue(decrypted.compareTo(value)==0);
|
|
}
|
|
|
|
@Test
|
|
public void testEncryptedClass() throws Exception {
|
|
String value = "myValue";
|
|
String encryptedValue = EncryptedImpl.encrypt(value);
|
|
|
|
Encrypted encrypted = new EncryptedImpl();
|
|
encrypted.setEncryptedValue(EncryptedImpl.encrypt(value));
|
|
|
|
String json = ElementMapper.marshal(encrypted);
|
|
logger.debug(json);
|
|
|
|
Property property = ElementMapper.unmarshal(Property.class, json);
|
|
Assert.assertTrue(property instanceof Encrypted);
|
|
Assert.assertTrue(((Encrypted) property).getEncryptedValue().compareTo(encryptedValue)==0);
|
|
String decryptedValue = EncryptedImpl.decrypt(((Encrypted) property).getEncryptedValue());
|
|
Assert.assertTrue(decryptedValue.compareTo(value)==0);
|
|
logger.debug(ElementMapper.marshal(property));
|
|
|
|
|
|
encrypted = ElementMapper.unmarshal(Encrypted.class, json);
|
|
Assert.assertTrue(property instanceof Encrypted);
|
|
Assert.assertTrue(encrypted.getEncryptedValue().compareTo(encryptedValue)==0);
|
|
decryptedValue = EncryptedImpl.decrypt(((Encrypted) property).getEncryptedValue());
|
|
Assert.assertTrue(decryptedValue.compareTo(value)==0);
|
|
logger.debug(ElementMapper.marshal(encrypted));
|
|
|
|
}
|
|
|
|
@Test
|
|
public void testClassDefinition() throws Exception {
|
|
Class<? extends Property> clz = Encrypted.class;
|
|
String json = TypeMapper.serializeType(clz);
|
|
logger.trace(json);
|
|
}
|
|
|
|
@Test
|
|
public void testEncryptedSpecilization() throws Exception {
|
|
String value = "myValue";
|
|
String encryptedValue = EncryptedImpl.encrypt(value);
|
|
String marshalled = "{\"" + Element.CLASS_PROPERTY + "\":\"MyEncrypted\",\"" + Element.SUPERCLASSES_PROPERTY + "\":[\"" + Encrypted.NAME + "\", \"" + Property.NAME + "\"],\"" + Encrypted.VALUE + "\":\""+ encryptedValue + "\"}";
|
|
Property property = ElementMapper.unmarshal(Property.class, marshalled);
|
|
Assert.assertTrue(property instanceof Encrypted);
|
|
Assert.assertTrue(((Encrypted) property).getEncryptedValue().compareTo(encryptedValue)==0);
|
|
logger.debug(ElementMapper.marshal(property));
|
|
|
|
Encrypted encrypted = ElementMapper.unmarshal(Encrypted.class, marshalled);
|
|
Assert.assertTrue(property instanceof Encrypted);
|
|
Assert.assertTrue(encrypted.getEncryptedValue().compareTo(encryptedValue)==0);
|
|
logger.debug(ElementMapper.marshal(encrypted));
|
|
|
|
}
|
|
|
|
@Test
|
|
public void decryptTest() throws Exception {
|
|
ContextTest.setContextByName(DEFAULT_TEST_SCOPE);
|
|
String encryptedValue = "yFdK2wXAOLkNoKKx+gopzA==";
|
|
String decryptedValue = EncryptedImpl.decrypt(encryptedValue);
|
|
logger.debug("Encrypted Value is : '{}' which decrypted is : '{}'", encryptedValue, decryptedValue);
|
|
String reEncryptedValue = EncryptedImpl.encrypt(decryptedValue);
|
|
Assert.assertTrue(encryptedValue.compareTo(reEncryptedValue)==0);
|
|
}
|
|
}
|