1 package org.kuali.common.util.enc;
2
3 import org.jasypt.util.text.TextEncryptor;
4 import org.junit.Assert;
5 import org.junit.Test;
6 import org.kuali.common.util.spring.env.BasicEnvironmentService;
7 import org.kuali.common.util.spring.env.EnvironmentService;
8
9 public class EncContextTest {
10
11 @Test
12 public void test1() {
13 EnvironmentService env = new BasicEnvironmentService();
14 String password = "foo";
15 String text = "bar.baz";
16 TextEncryptor encryptor = EncUtils.getTextEncryptor(password);
17 EncryptionService enc1 = new DefaultEncryptionService(encryptor);
18 String encrypted1 = enc1.encrypt(text);
19 System.setProperty("enc.password", password);
20 EncContext ctx = new EncContext.Builder(env, "bar").removeSystemProperties(true).build();
21 Assert.assertTrue(ctx.getTextEncryptor().isPresent());
22 EncryptionService enc2 = new DefaultEncryptionService(encryptor);
23 String encrypted2 = enc2.encrypt(text);
24 String decrypted1 = enc1.decrypt(encrypted1);
25 String decrypted2 = enc2.decrypt(encrypted2);
26 Assert.assertEquals(text, decrypted1);
27 Assert.assertEquals(text, decrypted2);
28 }
29
30 @Test
31 public void test2() {
32 String password = "foo";
33 String text = "bar.baz";
34 TextEncryptor encryptor = EncUtils.getTextEncryptor(password);
35 EncryptionService enc1 = new DefaultEncryptionService(encryptor);
36 String encrypted1 = enc1.encrypt(text);
37 System.setProperty("properties.enc.password", password);
38 EncContext ctx = new EncContext.Builder().removeSystemProperties(true).build();
39 Assert.assertTrue(ctx.getTextEncryptor().isPresent());
40 EncryptionService enc2 = new DefaultEncryptionService(encryptor);
41 String encrypted2 = enc2.encrypt(text);
42 String decrypted1 = enc1.decrypt(encrypted1);
43 String decrypted2 = enc2.decrypt(encrypted2);
44 Assert.assertEquals(text, decrypted1);
45 Assert.assertEquals(text, decrypted2);
46 }
47
48 }