01: /*
02: * Copyright 2006-2007 The Kuali Foundation.
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.kuali.core.service.impl;
17:
18: import org.apache.commons.lang.StringUtils;
19: import org.kuali.core.service.EncryptionService;
20: import org.kuali.kfs.context.KualiTestBase;
21: import org.kuali.kfs.context.SpringContext;
22: import org.kuali.test.ConfigureContext;
23:
24: /**
25: * This class does gross checking of a particular implementation of an Encryption service.
26: */
27: @ConfigureContext
28: public class DemonstrationGradeEncryptionServiceImplTest extends
29: KualiTestBase {
30:
31: // it would be a terrible idea to ever use this particular secret key outside of this unit test
32: private final static String NOT_SO_SECRET_KEY_ONLY_INTENDED_FOR_TESTING = "sm3H8KUU8BTzpFULS9ME7g==";
33: private static final String TEST_VALUE = "77789";
34:
35: public void testEncrypt() throws Exception {
36: // assertEquals(SpringContext.getBean(EncryptionService.class).decrypt(SpringContext.getBean(EncryptionService.class).encrypt(TEST_VALUE)),
37: // TEST_VALUE);
38: // DemonstrationGradeEncryptionServiceImpl encryptionService = new DemonstrationGradeEncryptionServiceImpl();
39: // encryptionService.setSecretKey(NOT_SO_SECRET_KEY_ONLY_INTENDED_FOR_TESTING);
40: //
41: // String valueToHide = "The quick brown fox jumps over a lazy dog";
42: // assertTrue("Byte array should be equivalent to the target String", valueToHide.equals(new
43: // String(valueToHide.getBytes())));
44: // String encrypted = encryptionService.encrypt(valueToHide);
45: //
46: // // Verify the string can be decrypted:
47: // String clearText = encryptionService.decrypt(encrypted);
48: // assertTrue(clearText.equals(valueToHide));
49: //
50: // // Make sure it can be decrypted from a freshly created copy:
51: // encryptionService = new DemonstrationGradeEncryptionServiceImpl();
52: // encryptionService.setSecretKey(NOT_SO_SECRET_KEY_ONLY_INTENDED_FOR_TESTING);
53: // clearText = encryptionService.decrypt(encrypted);
54: // assertTrue(clearText.equals(valueToHide));
55: // System.err.println("This should be unintelligible: " + encrypted);
56: // System.err.println("Here is a freshly generated secret key: " + encryptionService.generateEncodedKey());
57:
58: EncryptionService encryptionService = SpringContext
59: .getBean(EncryptionService.class);
60: String valueToHide = "999999999";
61: // valueToHide = StringUtils.rightPad(valueToHide, 16);
62: String encrypted = encryptionService.encrypt(valueToHide)
63: + EncryptionService.ENCRYPTION_POST_PREFIX;
64: System.out.print(encrypted);
65: encrypted = StringUtils.stripEnd(encrypted,
66: EncryptionService.ENCRYPTION_POST_PREFIX);
67: String clearText = SpringContext.getBean(
68: EncryptionService.class).decrypt(encrypted);
69: assertTrue(clearText.equals(valueToHide));
70: //
71: // valueToHide = "My friend Joe";
72: // valueToHide = StringUtils.rightPad(valueToHide, 16);
73: // encrypted = encryptionService.encrypt(valueToHide);
74: // System.out.print(encrypted);
75: // clearText = encryptionService.decrypt(encrypted);
76: // assertTrue(clearText.equals(valueToHide));
77:
78: }
79:
80: }
|