001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049:
050: /*
051: * AddTest.java
052: *
053: * Created on April 1, 2002, 5:47 PM
054: */
055:
056: package org.jaffa.security;
057:
058: import junit.framework.TestCase;
059: import java.util.Arrays;
060: import java.io.UnsupportedEncodingException;
061: import java.io.File;
062: import javax.crypto.SecretKey;
063: import java.io.InputStream;
064: import java.io.FileNotFoundException;
065: import org.jaffa.util.SplitString;
066: import java.util.HashMap;
067: import java.lang.System;
068: import java.util.StringTokenizer;
069:
070: /**
071: *
072: * @author GautamJ
073: */
074: public class EncryptionHelperTest extends TestCase {
075:
076: /** Creates new QueryTest
077: * @param name The name of the test case.
078: */
079: public EncryptionHelperTest(String name) {
080: super (name);
081: }
082:
083: /** Sets up the fixture, by creating the UOW. This method is called before a test is executed.
084: */
085: protected void setUp() {
086: }
087:
088: /** Tears down the fixture, by closing the UOW. This method is called after a test is executed.
089: */
090: protected void tearDown() {
091: }
092:
093: /*
094: EncryptionHelper.encryptObjectForURL(source
095: EncryptionHelper.encryptStringForURL(source
096: EncryptionHelper.getObjectFromEncryptedURL(data
097: EncryptionHelper.getStringFromEncryptedURL(data
098: */
099:
100: public void testBasicFunctions() {
101: try {
102: byte b = 0x0D;
103: assertEquals("Test1 : toHex", 'D', EncryptionHelper
104: .toHex(b));
105:
106: char c = 'C';
107: assertEquals("Test2 : fromHex", 0x0C, EncryptionHelper
108: .fromHex(c));
109:
110: byte[] ba = new byte[] { (byte) 0xc5, (byte) 0x4f };
111: String baStr = EncryptionHelper.intoHexString(ba);
112: assertEquals("Test3 : intoHexString", "C54F", baStr);
113:
114: byte[] ba2 = EncryptionHelper.fromHexString("C54F");
115: assertTrue("Test4 : fromHexString", Arrays.equals(ba, ba2));
116:
117: } catch (Exception e) {
118: e.printStackTrace();
119: fail();
120: }
121: }
122:
123: public void testBasicFunctions2() {
124: try {
125: String a1 = "This is a strange test";
126: String a2 = EncryptionHelper.intoString(EncryptionHelper
127: .intoBytes(a1));
128: assertEquals("Test1. 8-Bit String to/From byte[]", a1, a2);
129:
130: try {
131: String b1 = "This is a strange test\u03a0";
132: EncryptionHelper.intoBytes(b1);
133: fail("No Exception Thrown");
134: } catch (UnsupportedEncodingException e) {
135: assertTrue("Test2. Exception Thrown", true);
136: }
137:
138: String c1 = "This is a strange test\u03a0";
139: byte[] c1b = EncryptionHelper.intoBytes16(c1);
140: String c2 = EncryptionHelper.intoString16(c1b);
141: byte[] c2b = EncryptionHelper.intoBytes16(c2);
142: assertEquals("Test3. 16-Bit String to/From byte[]", c1, c2);
143:
144: } catch (Exception e) {
145: e.printStackTrace();
146: fail();
147: }
148: }
149:
150: public void testCreateKey() {
151: File keyFile = null;
152: try {
153: String filename = "sample.key";
154: String fullname = null;
155: String cp = System.getProperty("java.class.path");
156: StringTokenizer stknzr = new StringTokenizer(cp,
157: File.pathSeparator);
158: while (stknzr.hasMoreTokens()) {
159: String dirName = stknzr.nextToken();
160: File dir = new File(dirName);
161: if (dir.isDirectory()) {
162: fullname = dirName + File.separatorChar + filename;
163: keyFile = new File(fullname);
164: break;
165: }
166: }
167:
168: if (keyFile == null)
169: fail("Cannot find any directory in the classpath " + cp);
170:
171: // Clean up key file
172: if (keyFile.exists())
173: assertTrue("Setup: old key file deleted", keyFile
174: .delete());
175:
176: EncryptionHelper.main(new String[] { fullname });
177: assertTrue("Test1a. Create Key File (new)", keyFile
178: .exists());
179:
180: EncryptionHelper.main(new String[] { fullname });
181: assertTrue("Test1b. Create Key File (overwrite)", keyFile
182: .exists());
183:
184: SecretKey sk = EncryptionHelper.readKey(keyFile);
185: this .assertNotNull("Test2. Read back Secuirty Key", sk);
186:
187: SecretKey sk2 = EncryptionHelper
188: .readKeyClassPath("sample.key");
189: this
190: .assertNotNull(
191: "Test3. Read back Secuirty Key via Class Path",
192: sk2);
193: } catch (Exception e) {
194: e.printStackTrace();
195: fail();
196: } finally {
197: // Clean up key file
198: if (keyFile != null && keyFile.exists())
199: keyFile.delete();
200: }
201: }
202:
203: /** See if we can encrypt a string for use as a url, and then un-encrypt it
204: */
205: public void testEncryptString() {
206: try {
207: String source = "This is another Test With Data !@#$^%&*()";
208: SecretKey key = EncryptionHelper.createKey();
209: assertNotNull("Test 1. Created a new Key", key);
210:
211: String data = EncryptionHelper.encryptStringForURL(source,
212: key);
213: String dest = EncryptionHelper.getStringFromEncryptedURL(
214: data, key);
215: assertEquals("Test 2. EncryptStringURL", source, dest);
216:
217: SecretKey key2 = EncryptionHelper.createKey();
218: assertNotNull("Test 3. Created another new Key", key2);
219:
220: // Test for using wrong decrypt key
221: try {
222: String dest2 = EncryptionHelper
223: .getStringFromEncryptedURL(data, key2);
224: fail("Should throw a wrong key exception");
225: } catch (java.security.GeneralSecurityException e) {
226: assertTrue("Test 4. Exception if wrong keys used", true);
227: }
228: } catch (Exception e) {
229: e.printStackTrace();
230: fail();
231: }
232: }
233:
234: public void testEncryptObject() {
235: try {
236: HashMap source = new HashMap();
237: source.put("freddy", "nightmare");
238: source.put("weird stuff", new Integer(1000));
239: source.put("unicode", "\u03a0 this is PI");
240:
241: SecretKey key = EncryptionHelper.createKey();
242: assertNotNull("Test 1. Created a new Key", key);
243:
244: String data = EncryptionHelper.encryptObjectForURL(source,
245: key);
246: HashMap dest = (HashMap) EncryptionHelper
247: .getObjectFromEncryptedURL(data, key);
248: assertEquals("Test 2. EncryptObjectURL (object)", source,
249: dest);
250: assertEquals("Test 2a. EncryptObjectURL (size)", source
251: .size(), dest.size());
252: assertEquals("Test 2b. EncryptObjectURL (keys)", source
253: .keySet(), dest.keySet());
254:
255: // Try to call this method with a non-serializable object...
256: try {
257: data = EncryptionHelper.encryptObjectForURL(Thread
258: .currentThread(), key);
259: fail("Exception should have been raised");
260: } catch (java.io.NotSerializableException e) {
261: assertTrue("Test 3. Object not serializable", true);
262: }
263: } catch (Exception e) {
264: e.printStackTrace();
265: fail();
266: }
267: }
268: }
|