001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Alexander Y. Kleymenov
020: * @version $Revision$
021: */package javax.crypto;
022:
023: import java.io.ByteArrayInputStream;
024: import java.io.ByteArrayOutputStream;
025: import java.io.ObjectInputStream;
026: import java.io.ObjectOutputStream;
027: import java.security.Key;
028: import java.util.Arrays;
029: import javax.crypto.Cipher;
030: import javax.crypto.KeyGenerator;
031: import javax.crypto.NullCipher;
032: import javax.crypto.spec.IvParameterSpec;
033: import javax.crypto.spec.SecretKeySpec;
034:
035: import junit.framework.TestCase;
036:
037: /**
038: */
039:
040: public class SealedObjectTest extends TestCase {
041:
042: /**
043: * readObject(ObjectInputStream s) method testing. Tests if the
044: * serialization/deserialization works correctly: object is serialized,
045: * deserialized, the content od deserialized object equals to the
046: * content of initial object.
047: */
048: public void testReadObject() throws Exception {
049: String secret = "secret string";
050: SealedObject so = new SealedObject(secret, new NullCipher());
051: ByteArrayOutputStream bos = new ByteArrayOutputStream();
052: ObjectOutputStream oos = new ObjectOutputStream(bos);
053: oos.writeObject(so);
054:
055: ObjectInputStream ois = new ObjectInputStream(
056: new ByteArrayInputStream(bos.toByteArray()));
057:
058: SealedObject so_des = (SealedObject) ois.readObject();
059: assertEquals(
060: "The secret content of deserialized object "
061: + "should be equal to the secret content of initial object",
062: secret, so_des.getObject(new NullCipher()));
063: assertEquals(
064: "The value returned by getAlgorithm() method of "
065: + "deserialized object should be equal to the value returned "
066: + "by getAlgorithm() method of initial object",
067: so.getAlgorithm(), so_des.getAlgorithm());
068: }
069:
070: /**
071: * SealedObject(Serializable object, Cipher c) method testing. Tests if the
072: * NullPointerException is thrown in the case of null cipher.
073: */
074: public void testSealedObject1() throws Exception {
075: String secret = "secret string";
076: try {
077: new SealedObject(secret, null);
078: fail("NullPointerException should be thrown in the case "
079: + "of null cipher.");
080: } catch (NullPointerException e) {
081: }
082: }
083:
084: /**
085: * SealedObject(SealedObject so) method testing. Tests if the
086: * NullPointerException is thrown in the case of null SealedObject.
087: */
088: public void testSealedObject2() throws Exception {
089: try {
090: new SealedObject(null);
091: fail("NullPointerException should be thrown in the case "
092: + "of null SealedObject.");
093: } catch (NullPointerException e) {
094: }
095:
096: String secret = "secret string";
097: Cipher cipher = new NullCipher();
098: SealedObject so1 = new SealedObject(secret, cipher);
099: SealedObject so2 = new SealedObject(so1);
100:
101: assertEquals("The secret content of the object should equals "
102: + "to the secret content of initial object.", secret,
103: so2.getObject(cipher));
104: assertEquals(
105: "The algorithm which was used to seal the object "
106: + "should be the same as the algorithm used to seal the "
107: + "initial object", so1.getAlgorithm(), so2
108: .getAlgorithm());
109: }
110:
111: /**
112: * getAlgorithm() method testing. Tests if the returned value equals to the
113: * corresponding value of Cipher object.
114: */
115: public void testGetAlgorithm() throws Exception {
116: String secret = "secret string";
117: String algorithm = "DES";
118: KeyGenerator kg = KeyGenerator.getInstance(algorithm);
119: Key key = kg.generateKey();
120:
121: Cipher cipher = Cipher.getInstance(algorithm);
122: cipher.init(Cipher.ENCRYPT_MODE, key);
123: SealedObject so = new SealedObject(secret, cipher);
124:
125: assertEquals("The algorithm name should be the same as used "
126: + "in cipher.", algorithm, so.getAlgorithm());
127: }
128:
129: /**
130: * getObject(Key key) method testing. Tests if the object sealed with
131: * encryption algorithm and specified parameters can be retrieved by
132: * specifying the cryptographic key.
133: */
134: public void testGetObject1() throws Exception {
135: KeyGenerator kg = KeyGenerator.getInstance("DES");
136: Key key = kg.generateKey();
137:
138: IvParameterSpec ips = new IvParameterSpec(new byte[] { 1, 2, 3,
139: 4, 5, 6, 7, 8 });
140:
141: Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
142: cipher.init(Cipher.ENCRYPT_MODE, key, ips);
143:
144: String secret = "secret string";
145: SealedObject so = new SealedObject(secret, cipher);
146:
147: assertEquals("The returned object does not equals to the "
148: + "original object.", secret, so.getObject(key));
149:
150: assertTrue("The encodedParams field of SealedObject object "
151: + "should contain the encoded algorithm parameters.",
152: Arrays.equals(so.encodedParams, cipher.getParameters()
153: .getEncoded()));
154: }
155:
156: /**
157: * getObject(Cipher c) method testing. Tests if the proper exception is
158: * thrown in the case of incorrect input parameters and if the object sealed
159: * with encryption algorithm and specified parameters can be retrieved by
160: * specifying the initialized Cipher object.
161: */
162: public void testGetObject2() throws Exception {
163: try {
164: new SealedObject("secret string", new NullCipher())
165: .getObject((Cipher) null);
166: fail("NullPointerException should be thrown in the case of "
167: + "null cipher.");
168: } catch (NullPointerException e) {
169: }
170:
171: KeyGenerator kg = KeyGenerator.getInstance("DES");
172: Key key = kg.generateKey();
173:
174: IvParameterSpec ips = new IvParameterSpec(new byte[] { 1, 2, 3,
175: 4, 5, 6, 7, 8 });
176:
177: Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
178: cipher.init(Cipher.ENCRYPT_MODE, key, ips);
179:
180: String secret = "secret string";
181: SealedObject so = new SealedObject(secret, cipher);
182:
183: cipher.init(Cipher.DECRYPT_MODE, key, ips);
184: assertEquals("The returned object does not equals to the "
185: + "original object.", secret, so.getObject(cipher));
186: }
187:
188: /**
189: * getObject(Key key, String provider) method testing. Tests if the proper
190: * exception is thrown in the case of incorrect input parameters and if the
191: * object sealed with encryption algorithm can be retrieved by specifying
192: * the cryptographic key and provider name.
193: */
194: public void testGetObject3() throws Exception {
195: try {
196: new SealedObject("secret string", new NullCipher())
197: .getObject(new SecretKeySpec(
198: new byte[] { 0, 0, 0 }, "algorithm"), null);
199: fail("IllegalArgumentException should be thrown in the case of "
200: + "null provider.");
201: } catch (IllegalArgumentException e) {
202: }
203:
204: try {
205: new SealedObject("secret string", new NullCipher())
206: .getObject(new SecretKeySpec(
207: new byte[] { 0, 0, 0 }, "algorithm"), "");
208: fail("IllegalArgumentException should be thrown in the case of "
209: + "empty provider.");
210: } catch (IllegalArgumentException e) {
211: }
212:
213: KeyGenerator kg = KeyGenerator.getInstance("DES");
214: Key key = kg.generateKey();
215:
216: Cipher cipher = Cipher.getInstance("DES");
217: String provider = cipher.getProvider().getName();
218: cipher.init(Cipher.ENCRYPT_MODE, key);
219:
220: String secret = "secret string";
221: SealedObject so = new SealedObject(secret, cipher);
222:
223: cipher.init(Cipher.DECRYPT_MODE, key);
224: assertEquals("The returned object does not equals to the "
225: + "original object.", secret, so.getObject(key,
226: provider));
227: }
228:
229: }
|