Source Code Cross Referenced for SealedObjectTest.java in  » Apache-Harmony-Java-SE » javax-package » javax » crypto » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » javax package » javax.crypto 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.