Source Code Cross Referenced for KeyFactory2Test.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » security » tests » java » security » 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 » org package » org.apache.harmony.security.tests.java.security 
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:        package org.apache.harmony.security.tests.java.security;
019:
020:        import java.security.InvalidKeyException;
021:        import java.security.KeyFactory;
022:        import java.security.KeyPair;
023:        import java.security.KeyPairGenerator;
024:        import java.security.NoSuchAlgorithmException;
025:        import java.security.NoSuchProviderException;
026:        import java.security.PrivateKey;
027:        import java.security.Provider;
028:        import java.security.PublicKey;
029:        import java.security.SecureRandom;
030:        import java.security.Security;
031:        import java.security.spec.InvalidKeySpecException;
032:        import java.security.spec.KeySpec;
033:        import java.security.spec.PKCS8EncodedKeySpec;
034:        import java.security.spec.X509EncodedKeySpec;
035:        import java.util.Arrays;
036:        import java.util.Enumeration;
037:        import java.util.Vector;
038:
039:        public class KeyFactory2Test extends junit.framework.TestCase {
040:
041:            private static final String KEYFACTORY_ID = "KeyFactory.";
042:
043:            private String[] keyfactAlgs = null;
044:
045:            private String providerName = null;
046:
047:            static class KeepAlive extends Thread {
048:                int sleepTime, iterations;
049:
050:                public KeepAlive(int sleepTime, int iterations) {
051:                    this .sleepTime = sleepTime;
052:                    this .iterations = iterations;
053:                }
054:
055:                public void run() {
056:                    synchronized (this ) {
057:                        this .notify();
058:                    }
059:                    for (int i = 0; i < iterations; i++) {
060:                        try {
061:                            Thread.sleep(sleepTime);
062:                            System.out.print("[KA]");
063:                        } catch (InterruptedException e) {
064:                            System.out.print("[I]");
065:                            break;
066:                        }
067:                    }
068:                }
069:            }
070:
071:            private KeepAlive createKeepAlive(String alg) {
072:                if (alg.equals("RSA")) {
073:                    // 32 minutes
074:                    KeepAlive keepalive = new KeepAlive(240000, 8);
075:                    synchronized (keepalive) {
076:                        keepalive.start();
077:                        try {
078:                            keepalive.wait();
079:                        } catch (InterruptedException e) {
080:                            // ignore
081:                        }
082:                    }
083:                    return keepalive;
084:                }
085:                return null;
086:            }
087:
088:            /**
089:             * @tests java.security.KeyFactory#generatePrivate(java.security.spec.KeySpec)
090:             */
091:            public void test_generatePrivateLjava_security_spec_KeySpec() {
092:                // Test for method java.security.PrivateKey
093:                // java.security.KeyFactory.generatePrivate(java.security.spec.KeySpec)
094:                for (int i = 0; i < keyfactAlgs.length; i++) {
095:                    try {
096:                        KeyFactory fact = KeyFactory.getInstance(
097:                                keyfactAlgs[i], providerName);
098:                        KeyPairGenerator keyGen = KeyPairGenerator
099:                                .getInstance(keyfactAlgs[i]);
100:                        SecureRandom random = new SecureRandom(); // We don't use
101:                        // getInstance
102:                        keyGen.initialize(1024, random);
103:                        KeepAlive keepalive = createKeepAlive(keyfactAlgs[i]);
104:                        KeyPair keys = keyGen.generateKeyPair();
105:                        if (keepalive != null) {
106:                            keepalive.interrupt();
107:                        }
108:
109:                        KeySpec privateKeySpec = fact.getKeySpec(keys
110:                                .getPrivate(),
111:                                getPrivateKeySpecClass(keyfactAlgs[i]));
112:                        PrivateKey privateKey = fact
113:                                .generatePrivate(privateKeySpec);
114:                        boolean samePrivate = Arrays.equals(keys.getPrivate()
115:                                .getEncoded(), privateKey.getEncoded());
116:                        assertTrue(
117:                                "generatePrivate generated different key for algorithm "
118:                                        + keyfactAlgs[i], samePrivate);
119:                        fact.generatePrivate(new PKCS8EncodedKeySpec(keys
120:                                .getPrivate().getEncoded()));
121:                    } catch (InvalidKeySpecException e) {
122:                        fail("invalid key spec for algorithm " + keyfactAlgs[i]);
123:                    } catch (NoSuchAlgorithmException e) {
124:                        fail("getInstance did not find algorithm "
125:                                + keyfactAlgs[i]);
126:                    } catch (NoSuchProviderException e) {
127:                        fail("getInstance did not find provider "
128:                                + providerName);
129:                    }
130:                }
131:            }
132:
133:            /**
134:             * @tests java.security.KeyFactory#generatePublic(java.security.spec.KeySpec)
135:             */
136:            public void test_generatePublicLjava_security_spec_KeySpec() {
137:                // Test for method java.security.PublicKey
138:                // java.security.KeyFactory.generatePublic(java.security.spec.KeySpec)
139:                for (int i = 0; i < keyfactAlgs.length; i++) {
140:                    try {
141:                        KeyFactory fact = KeyFactory.getInstance(
142:                                keyfactAlgs[i], providerName);
143:                        KeyPairGenerator keyGen = KeyPairGenerator
144:                                .getInstance(keyfactAlgs[i]);
145:                        // We don't use getInstance
146:                        SecureRandom random = new SecureRandom();
147:                        keyGen.initialize(1024, random);
148:                        KeepAlive keepalive = createKeepAlive(keyfactAlgs[i]);
149:                        KeyPair keys = keyGen.generateKeyPair();
150:                        if (keepalive != null) {
151:                            keepalive.interrupt();
152:                        }
153:                        KeySpec publicKeySpec = fact.getKeySpec(keys
154:                                .getPublic(),
155:                                getPublicKeySpecClass(keyfactAlgs[i]));
156:                        PublicKey publicKey = fact
157:                                .generatePublic(publicKeySpec);
158:                        boolean samePublic = Arrays.equals(keys.getPublic()
159:                                .getEncoded(), publicKey.getEncoded());
160:                        assertTrue(
161:                                "generatePublic generated different key for algorithm "
162:                                        + keyfactAlgs[i], samePublic);
163:                    } catch (NoSuchAlgorithmException e) {
164:                        fail("getInstance did not find algorithm "
165:                                + keyfactAlgs[i]);
166:                    } catch (NoSuchProviderException e) {
167:                        fail("getInstance did not find provider "
168:                                + providerName);
169:                    } catch (InvalidKeySpecException e) {
170:                        fail("invalid key spec for algorithm " + keyfactAlgs[i]);
171:                    }
172:                }
173:            }
174:
175:            /**
176:             * @tests java.security.KeyFactory#getAlgorithm()
177:             */
178:            public void test_getAlgorithm() {
179:                // Test for method java.lang.String
180:                // java.security.KeyFactory.getAlgorithm()
181:                for (int i = 0; i < keyfactAlgs.length; i++) {
182:                    try {
183:                        KeyFactory fact = KeyFactory.getInstance(
184:                                keyfactAlgs[i], providerName);
185:                        assertTrue("getAlgorithm ok for algorithm "
186:                                + keyfactAlgs[i], fact.getAlgorithm().equals(
187:                                keyfactAlgs[i]));
188:                    } catch (NoSuchAlgorithmException e) {
189:                        fail("getInstance did not find algorithm "
190:                                + keyfactAlgs[i]);
191:                    } catch (NoSuchProviderException e) {
192:                        fail("getInstance did not find provider "
193:                                + providerName);
194:                    }
195:                }// end for
196:            }
197:
198:            /**
199:             * @tests java.security.KeyFactory#getInstance(java.lang.String)
200:             */
201:            public void test_getInstanceLjava_lang_String() {
202:                // Test for method java.security.KeyFactory
203:                // java.security.KeyFactory.getInstance(java.lang.String)
204:                for (int i = 0; i < keyfactAlgs.length; i++) {
205:                    try {
206:                        assertNotNull(KeyFactory.getInstance(keyfactAlgs[i]));
207:                    } catch (NoSuchAlgorithmException e) {
208:                        fail("getInstance did not find algorithm "
209:                                + keyfactAlgs[i]);
210:                    }
211:                }// end for
212:            }
213:
214:            /**
215:             * @tests java.security.KeyFactory#getInstance(java.lang.String,
216:             *        java.lang.String)
217:             */
218:            public void test_getInstanceLjava_lang_StringLjava_lang_String()
219:                    throws Exception {
220:
221:                // Test1: Test for method java.security.KeyFactory
222:                // java.security.KeyFactory.getInstance(java.lang.String,
223:                // java.lang.String)
224:                try {
225:                    Provider[] providers = Security
226:                            .getProviders("KeyFactory.DSA");
227:                    if (providers != null) {
228:                        for (int i = 0; i < providers.length; i++) {
229:                            KeyFactory.getInstance("DSA", providers[i]
230:                                    .getName());
231:                        }// end for
232:                    } else {
233:                        fail("No providers support KeyFactory.DSA");
234:                    }
235:                } catch (NoSuchAlgorithmException e) {
236:                    fail("getInstance did not find algorithm");
237:                } catch (NoSuchProviderException e) {
238:                    fail("getInstance did not find the provider");
239:                }
240:
241:                // Test2: Test with null provider name
242:                try {
243:                    KeyFactory.getInstance("DSA", (String) null);
244:                    fail("Expected IllegalArgumentException");
245:                } catch (IllegalArgumentException e) {
246:                    // Expected
247:                }
248:            }
249:
250:            /**
251:             * @tests java.security.KeyFactory#getKeySpec(java.security.Key,
252:             *        java.lang.Class)
253:             */
254:            public void test_getKeySpecLjava_security_KeyLjava_lang_Class() {
255:                // Test for method java.security.spec.KeySpec
256:                // java.security.KeyFactory.getKeySpec(java.security.Key,
257:                // java.lang.Class)
258:                for (int i = 0; i < keyfactAlgs.length; i++) {
259:                    try {
260:                        KeyFactory fact = KeyFactory.getInstance(
261:                                keyfactAlgs[i], providerName);
262:                        KeyPairGenerator keyGen = KeyPairGenerator
263:                                .getInstance(keyfactAlgs[i]);
264:
265:                        // We don't use getInstance
266:                        SecureRandom random = new SecureRandom();
267:                        keyGen.initialize(1024, random);
268:                        KeepAlive keepalive = createKeepAlive(keyfactAlgs[i]);
269:                        KeyPair keys = keyGen.generateKeyPair();
270:                        if (keepalive != null) {
271:                            keepalive.interrupt();
272:                        }
273:                        KeySpec privateKeySpec = fact.getKeySpec(keys
274:                                .getPrivate(),
275:                                getPrivateKeySpecClass(keyfactAlgs[i]));
276:                        KeySpec publicKeySpec = fact.getKeySpec(keys
277:                                .getPublic(),
278:                                getPublicKeySpecClass(keyfactAlgs[i]));
279:                        PrivateKey privateKey = fact
280:                                .generatePrivate(privateKeySpec);
281:                        PublicKey publicKey = fact
282:                                .generatePublic(publicKeySpec);
283:                        boolean samePublic = Arrays.equals(keys.getPublic()
284:                                .getEncoded(), publicKey.getEncoded());
285:                        boolean samePrivate = Arrays.equals(keys.getPrivate()
286:                                .getEncoded(), privateKey.getEncoded());
287:                        assertTrue(
288:                                "generatePrivate generated different key for algorithm "
289:                                        + keyfactAlgs[i], samePrivate);
290:                        assertTrue(
291:                                "generatePublic generated different key for algorithm "
292:                                        + keyfactAlgs[i], samePublic);
293:                        KeySpec encodedSpec = fact.getKeySpec(keys.getPublic(),
294:                                X509EncodedKeySpec.class);
295:                        assertTrue("improper key spec for encoded public key",
296:                                encodedSpec.getClass().equals(
297:                                        X509EncodedKeySpec.class));
298:                        encodedSpec = fact.getKeySpec(keys.getPrivate(),
299:                                PKCS8EncodedKeySpec.class);
300:                        assertTrue("improper key spec for encoded private key",
301:                                encodedSpec.getClass().equals(
302:                                        PKCS8EncodedKeySpec.class));
303:                    } catch (NoSuchAlgorithmException e) {
304:                        fail("getInstance did not find algorithm "
305:                                + keyfactAlgs[i]);
306:                    } catch (NoSuchProviderException e) {
307:                        fail("getInstance did not find provider "
308:                                + providerName);
309:                    } catch (InvalidKeySpecException e) {
310:                        fail("invalid key spec for algorithm " + keyfactAlgs[i]);
311:                    }
312:                }
313:            }
314:
315:            /**
316:             * @tests java.security.KeyFactory#getProvider()
317:             */
318:            public void test_getProvider() {
319:                // Test for method java.security.Provider
320:                // java.security.KeyFactory.getProvider()
321:                for (int i = 0; i < keyfactAlgs.length; i++) {
322:                    try {
323:                        KeyFactory fact = KeyFactory
324:                                .getInstance(keyfactAlgs[i]);
325:                        Provider p = fact.getProvider();
326:                        assertNotNull("provider is null for algorithm "
327:                                + keyfactAlgs[i], p);
328:                    } catch (NoSuchAlgorithmException e) {
329:                        fail("getInstance did not find algorithm "
330:                                + keyfactAlgs[i]);
331:                    }
332:                }// end for
333:            }
334:
335:            /**
336:             * @tests java.security.KeyFactory#translateKey(java.security.Key)
337:             */
338:            public void test_translateKeyLjava_security_Key() {
339:                // Test for method java.security.Key
340:                // java.security.KeyFactory.translateKey(java.security.Key)
341:                for (int i = 0; i < keyfactAlgs.length; i++) {
342:                    try {
343:                        KeyFactory fact = KeyFactory.getInstance(
344:                                keyfactAlgs[i], providerName);
345:                        KeyPairGenerator keyGen = KeyPairGenerator
346:                                .getInstance(keyfactAlgs[i]);
347:
348:                        // We don't use getInstance
349:                        SecureRandom random = new SecureRandom();
350:                        keyGen.initialize(1024, random);
351:                        KeepAlive keepalive = createKeepAlive(keyfactAlgs[i]);
352:                        KeyPair keys = keyGen.generateKeyPair();
353:                        if (keepalive != null) {
354:                            keepalive.interrupt();
355:                        }
356:                        fact.translateKey(keys.getPrivate());
357:                    } catch (NoSuchAlgorithmException e) {
358:                        fail("getInstance did not find algorithm "
359:                                + keyfactAlgs[i]);
360:                    } catch (NoSuchProviderException e) {
361:                        fail("getInstance did not find provider "
362:                                + providerName);
363:                    } catch (InvalidKeyException e) {
364:                        fail("generatePublic did not generate right spec for algorithm "
365:                                + keyfactAlgs[i]);
366:                    }
367:                }
368:            }
369:
370:            protected void setUp() {
371:                if (keyfactAlgs == null) {
372:                    Provider[] providers = Security.getProviders();
373:                    // Arbitrarily use the first provider that supports
374:                    // KeyFactory algorithms
375:                    for (Provider provider : providers) {
376:                        providerName = provider.getName();
377:                        keyfactAlgs = getKeyFactoryAlgorithms(providerName);
378:                        if (keyfactAlgs.length != 0) {
379:                            break;
380:                        }
381:                    }
382:                }
383:            }
384:
385:            /*
386:             * Returns the key algorithms that the given provider supports.
387:             */
388:            private String[] getKeyFactoryAlgorithms(String providerName) {
389:                Vector algs = new Vector();
390:
391:                Provider provider = Security.getProvider(providerName);
392:                if (provider == null)
393:                    return new String[0];
394:                Enumeration e = provider.keys();
395:                while (e.hasMoreElements()) {
396:                    String algorithm = (String) e.nextElement();
397:                    if (algorithm.startsWith(KEYFACTORY_ID)
398:                            && !algorithm.contains(" ")) {
399:                        algs.addElement(algorithm.substring(KEYFACTORY_ID
400:                                .length()));
401:                    }
402:                }
403:
404:                return (String[]) algs.toArray(new String[algs.size()]);
405:            }
406:
407:            /**
408:             * Returns the public key spec class for a given algorithm, or null if it is
409:             * not known.
410:             */
411:            private Class getPrivateKeySpecClass(String algName) {
412:                if (algName.equals("RSA")) {
413:                    return java.security.spec.RSAPrivateCrtKeySpec.class;
414:                }
415:                if (algName.equals("DSA")) {
416:                    return java.security.spec.DSAPrivateKeySpec.class;
417:                }
418:                return null;
419:            }
420:
421:            /**
422:             * Returns the private key spec class for a given algorithm, or null if it
423:             * is not known.
424:             */
425:            private Class getPublicKeySpecClass(String algName) {
426:                if (algName.equals("RSA")) {
427:                    return java.security.spec.RSAPublicKeySpec.class;
428:                }
429:                if (algName.equals("DSA")) {
430:                    return java.security.spec.DSAPublicKeySpec.class;
431:                }
432:                return null;
433:            }
434:        }
w_ww_.j___a__v___a__2___s_.___c_o_m_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.