100 lines
3.2 KiB
Java
100 lines
3.2 KiB
Java
package org.gcube.informationsystem.model.impl.properties;
|
|
|
|
import java.security.Key;
|
|
import java.security.SecureRandom;
|
|
|
|
import javax.crypto.KeyGenerator;
|
|
|
|
import org.gcube.informationsystem.base.reference.properties.Property;
|
|
import org.gcube.informationsystem.model.ContextTest;
|
|
import org.gcube.informationsystem.model.impl.properties.EncryptedImpl;
|
|
import org.gcube.informationsystem.model.reference.properties.Encrypted;
|
|
import org.gcube.informationsystem.types.TypeBinder;
|
|
import org.gcube.informationsystem.utils.ISMapper;
|
|
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";
|
|
|
|
Encrypted encrypted = new EncryptedImpl();
|
|
encrypted.setEncryptedValue(EncryptedImpl.encrypt(value));
|
|
|
|
String json = ISMapper.marshal(encrypted);
|
|
logger.debug(json);
|
|
|
|
Encrypted unmarshalled = ISMapper.unmarshal(Encrypted.class, json);
|
|
String decrypted = EncryptedImpl.decrypt(unmarshalled.getEncryptedValue());
|
|
|
|
Assert.assertTrue(decrypted.compareTo(value)==0);
|
|
}
|
|
|
|
@Test
|
|
public void testClassDefinition() throws Exception {
|
|
Class<? extends Property> clz = Encrypted.class;
|
|
String json = TypeBinder.serializeType(clz);
|
|
logger.trace(json);
|
|
}
|
|
|
|
@Test
|
|
public void testEncryptedSpecilization() throws Exception {
|
|
String marshalled = "{\"@class\":\"MyEncrypted\",\"@superClasses\":[\"Encrypted\", \"Property\"],\"value\":\"Encrypted\"}";
|
|
Property property = ISMapper.unmarshal(Property.class, marshalled);
|
|
logger.debug(ISMapper.marshal(property));
|
|
}
|
|
|
|
@Test
|
|
public void decryptTest() throws Exception {
|
|
ContextTest.setContext(GCUBE_DEVSEC);
|
|
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);
|
|
|
|
}
|
|
}
|