Source Code Cross Referenced for KeyPairGenerator1Test.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:        /**
019:         * @author Vera Y. Petrashkova
020:         * @version $Revision$
021:         */package org.apache.harmony.security.tests.java.security;
022:
023:        import java.security.*;
024:        import java.math.BigInteger;
025:        import java.security.spec.AlgorithmParameterSpec;
026:
027:        import org.apache.harmony.security.tests.support.MyKeyPairGenerator1;
028:        import org.apache.harmony.security.tests.support.MyKeyPairGenerator2;
029:        import org.apache.harmony.security.tests.support.SpiEngUtils;
030:
031:        import junit.framework.TestCase;
032:
033:        /**
034:         * Tests for <code>KeyPairGenerator</code> class constructors and methods.
035:         * 
036:         */
037:
038:        public class KeyPairGenerator1Test extends TestCase {
039:
040:            /**
041:             * Constructor for KayPairGeneratorTest.
042:             * 
043:             * @param arg0
044:             */
045:            public KeyPairGenerator1Test(String arg0) {
046:                super (arg0);
047:            }
048:
049:            private static String[] invalidValues = SpiEngUtils.invalidValues;
050:
051:            public static final String srvKeyPairGenerator = "KeyPairGenerator";
052:
053:            public static String[] algs = { "DSA", "dsa", "Dsa", "DsA", "dsA" };
054:
055:            public static String validAlgName = "DSA";
056:
057:            private static String validProviderName = null;
058:
059:            public static Provider validProvider = null;
060:
061:            private static boolean DSASupported = false;
062:
063:            public static String NotSupportMsg = "";
064:
065:            static {
066:                validProvider = SpiEngUtils.isSupport(validAlgName,
067:                        srvKeyPairGenerator);
068:                DSASupported = (validProvider != null);
069:                if (!DSASupported) {
070:                    NotSupportMsg = validAlgName
071:                            + " algorithm is not supported";
072:                }
073:                validProviderName = (DSASupported ? validProvider.getName()
074:                        : null);
075:            }
076:
077:            protected KeyPairGenerator[] createKPGen() {
078:                if (!DSASupported) {
079:                    fail(NotSupportMsg);
080:                    return null;
081:                }
082:                KeyPairGenerator[] kpg = new KeyPairGenerator[3];
083:                try {
084:                    kpg[0] = KeyPairGenerator.getInstance(validAlgName);
085:                    kpg[1] = KeyPairGenerator.getInstance(validAlgName,
086:                            validProvider);
087:                    kpg[2] = KeyPairGenerator.getInstance(validAlgName,
088:                            validProviderName);
089:                    return kpg;
090:                } catch (Exception e) {
091:                    e.printStackTrace();
092:                    return null;
093:                }
094:            }
095:
096:            /**
097:             * Test for <code>getInstance(String algorithm)</code> method 
098:             * Assertion:
099:             * throws NullPointerException  when algorithm is null
100:             * throws NoSuchAlgorithmException when algorithm is incorrect; 
101:             */
102:            public void testKeyPairGenerator01()
103:                    throws NoSuchAlgorithmException {
104:                try {
105:                    KeyPairGenerator.getInstance(null);
106:                    fail("NullPointerException or NoSuchAlgorithmException must be thrown  when algorithm is null");
107:                } catch (NoSuchAlgorithmException e) {
108:                } catch (NullPointerException e) {
109:                }
110:                for (int i = 0; i < invalidValues.length; i++) {
111:                    try {
112:                        KeyPairGenerator.getInstance(invalidValues[i]);
113:                        fail("NoSuchAlgorithmException must be thrown when algorithm is not available: "
114:                                .concat(invalidValues[i]));
115:                    } catch (NoSuchAlgorithmException e) {
116:                    }
117:                }
118:            }
119:
120:            /**
121:             * Test for <code>getInstance(String algorithm)</code> method 
122:             * Assertion: returns KeyPairGenerator object
123:             */
124:            public void testKeyPairGenerator02()
125:                    throws NoSuchAlgorithmException {
126:                if (!DSASupported) {
127:                    fail(NotSupportMsg);
128:                    return;
129:                }
130:                KeyPairGenerator kpg;
131:                for (int i = 0; i < algs.length; i++) {
132:                    kpg = KeyPairGenerator.getInstance(algs[i]);
133:                    assertEquals("Incorrect algorithm ", kpg.getAlgorithm()
134:                            .toUpperCase(), algs[i].toUpperCase());
135:                }
136:            }
137:
138:            /**
139:             * Test for <code>getInstance(String algorithm, String provider)</code>
140:             * method 
141:             * Assertion: throws IllegalArgumentException when provider is null or empty
142:             */
143:            public void testKeyPairGenerator03()
144:                    throws NoSuchAlgorithmException, NoSuchProviderException {
145:                if (!DSASupported) {
146:                    fail(NotSupportMsg);
147:                    return;
148:                }
149:                String provider = null;
150:                for (int i = 0; i < algs.length; i++) {
151:                    try {
152:                        KeyPairGenerator.getInstance(algs[i], provider);
153:                        fail("IllegalArgumentException must be thrown when provider is null");
154:                    } catch (IllegalArgumentException e) {
155:                    }
156:                    try {
157:                        KeyPairGenerator.getInstance(algs[i], "");
158:                        fail("IllegalArgumentException must be thrown when provider is empty");
159:                    } catch (IllegalArgumentException e) {
160:                    }
161:                }
162:            }
163:
164:            /**
165:             * Test for <code>getInstance(String algorithm, String provider)</code>
166:             * method 
167:             * Assertion:
168:             * throws NullPointerException  when algorithm is null
169:             * throws NoSuchAlgorithmException when algorithm is incorrect; 
170:             */
171:            public void testKeyPairGenerator04()
172:                    throws NoSuchAlgorithmException, IllegalArgumentException {
173:                if (!DSASupported) {
174:                    fail(NotSupportMsg);
175:                    return;
176:                }
177:                for (int i = 0; i < algs.length; i++) {
178:                    for (int j = 1; j < invalidValues.length; j++) {
179:                        try {
180:                            KeyPairGenerator.getInstance(algs[i],
181:                                    invalidValues[j]);
182:                            fail("NoSuchProviderException must be thrown (algorithm: "
183:                                    .concat(algs[i]).concat(" provider: ")
184:                                    .concat(invalidValues[j]).concat(")"));
185:                        } catch (NoSuchProviderException e) {
186:                        }
187:                    }
188:                }
189:            }
190:
191:            /**
192:             * Test for <code>getInstance(String algorithm, String provider)</code>
193:             * method 
194:             * Assertion: throws NoSuchAlgorithmException when algorithm is not
195:             * available oe null
196:             */
197:            public void testKeyPairGenerator05()
198:                    throws NoSuchProviderException, IllegalArgumentException {
199:                if (!DSASupported) {
200:                    fail(NotSupportMsg);
201:                    return;
202:                }
203:                try {
204:                    KeyPairGenerator.getInstance(null, validProviderName);
205:                    fail("NullPointerException or NoSuchAlgorithmException must be thrown  when algorithm is null");
206:                } catch (NoSuchAlgorithmException e) {
207:                } catch (NullPointerException e) {
208:                }
209:                for (int i = 0; i < invalidValues.length; i++) {
210:                    try {
211:                        KeyPairGenerator.getInstance(invalidValues[i],
212:                                validProviderName);
213:                        fail("NoSuchAlgorithmException must be thrown (algorithm: "
214:                                .concat(algs[i]).concat(" provider: ").concat(
215:                                        validProviderName).concat(")"));
216:                    } catch (NoSuchAlgorithmException e) {
217:                    }
218:                }
219:            }
220:
221:            /**
222:             * Test for <code>getInstance(String algorithm, String provider)</code>
223:             * method 
224:             * Assertion: returns KeyPairGenerator object
225:             */
226:            public void testKeyPairGenerator06()
227:                    throws NoSuchProviderException, NoSuchAlgorithmException,
228:                    IllegalArgumentException {
229:                if (!DSASupported) {
230:                    fail(NotSupportMsg);
231:                    return;
232:                }
233:                KeyPairGenerator kpg;
234:                for (int i = 0; i < algs.length; i++) {
235:                    kpg = KeyPairGenerator.getInstance(algs[i],
236:                            validProviderName);
237:                    assertEquals("Incorrect algorithm", kpg.getAlgorithm()
238:                            .toUpperCase(), algs[i].toUpperCase());
239:                    assertEquals("Incorrect provider", kpg.getProvider()
240:                            .getName(), validProviderName);
241:                }
242:            }
243:
244:            /**
245:             * Test for <code>getInstance(String algorithm, Provider provider)</code>
246:             * method 
247:             * Assertion: throws IllegalArgumentException when provider is null
248:             */
249:            public void testKeyPairGenerator07()
250:                    throws NoSuchAlgorithmException {
251:                if (!DSASupported) {
252:                    fail(NotSupportMsg);
253:                    return;
254:                }
255:                Provider provider = null;
256:                for (int i = 0; i < algs.length; i++) {
257:                    try {
258:                        KeyPairGenerator.getInstance(algs[i], provider);
259:                        fail("IllegalArgumentException must be thrown when provider is null");
260:                    } catch (IllegalArgumentException e) {
261:                    }
262:                }
263:            }
264:
265:            /**
266:             * Test for <code>getInstance(String algorithm, Provider provider)</code>
267:             * method 
268:             * Assertion: 
269:             * throws NullPointerException  when algorithm is null
270:             * throws NoSuchAlgorithmException when algorithm is incorrect; 
271:             */
272:            public void testKeyPairGenerator08()
273:                    throws IllegalArgumentException {
274:                if (!DSASupported) {
275:                    fail(NotSupportMsg);
276:                    return;
277:                }
278:                try {
279:                    KeyPairGenerator.getInstance(null, validProvider);
280:                    fail("NullPointerException or NoSuchAlgorithmException must be thrown  when algorithm is null");
281:                } catch (NoSuchAlgorithmException e) {
282:                } catch (NullPointerException e) {
283:                }
284:                for (int i = 0; i < invalidValues.length; i++) {
285:                    try {
286:                        KeyPairGenerator.getInstance(invalidValues[i],
287:                                validProvider);
288:                        fail("NoSuchAlgorithmException must be thrown (algorithm: "
289:                                .concat(algs[i]).concat(" provider: ").concat(
290:                                        validProviderName).concat(")"));
291:                    } catch (NoSuchAlgorithmException e) {
292:                    }
293:                }
294:            }
295:
296:            /**
297:             * Test for <code>getInstance(String algorithm, Provider provider)</code>
298:             * method 
299:             * Assertion: returns KeyPairGenerator object
300:             */
301:            public void testKeyPairGenerator09()
302:                    throws NoSuchAlgorithmException, IllegalArgumentException {
303:                if (!DSASupported) {
304:                    fail(NotSupportMsg);
305:                    return;
306:                }
307:                KeyPairGenerator kpg;
308:                for (int i = 0; i < algs.length; i++) {
309:                    kpg = KeyPairGenerator.getInstance(algs[i], validProvider);
310:                    assertEquals("Incorrect algorithm", kpg.getAlgorithm()
311:                            .toUpperCase(), algs[i].toUpperCase());
312:                    assertEquals("Incorrect provider", kpg.getProvider(),
313:                            validProvider);
314:                }
315:            }
316:
317:            /**
318:             * Test for <code>generateKeyPair()</code> and <code>genKeyPair()</code>
319:             * methods
320:             * Assertion: KeyPairGenerator was initialized before the invocation 
321:             * of these methods
322:             */
323:            public void testKeyPairGenerator10()
324:                    throws NoSuchAlgorithmException, NoSuchProviderException,
325:                    IllegalArgumentException {
326:                if (!DSASupported) {
327:                    fail(NotSupportMsg);
328:                    return;
329:                }
330:                KeyPairGenerator[] kpg = createKPGen();
331:                assertNotNull("KeyPairGenerator objects were not created", kpg);
332:                KeyPair kp, kp1;
333:                for (int i = 0; i < kpg.length; i++) {
334:                    kpg[i].initialize(512);
335:                    kp = kpg[i].generateKeyPair();
336:                    kp1 = kpg[i].genKeyPair();
337:
338:                    assertFalse("Incorrect private key", kp.getPrivate()
339:                            .equals(kp1.getPrivate()));
340:                    assertFalse("Incorrect public key", kp.getPublic().equals(
341:                            kp1.getPublic()));
342:                }
343:            }
344:
345:            /**
346:             * Test for methods: 
347:             * <code>initialize(int keysize)</code>
348:             * <code>initialize(int keysize, SecureRandom random)</code>
349:             * <code>initialize(AlgorithmParameterSpec param)</code>
350:             * <code>initialize(AlgorithmParameterSpec param, SecureRandom random)</code>
351:             * Assertion: throws InvalidParameterException or
352:             * InvalidAlgorithmParameterException when parameters keysize or param are
353:             * incorrect
354:             */
355:            public void testKeyPairGenerator11()
356:                    throws NoSuchAlgorithmException, NoSuchProviderException {
357:                if (!DSASupported) {
358:                    fail(NotSupportMsg);
359:                    return;
360:                }
361:                int[] keys = { -10000, -1024, -1, 0, 10000 };
362:                KeyPairGenerator[] kpg = createKPGen();
363:                assertNotNull("KeyPairGenerator objects were not created", kpg);
364:                SecureRandom random = new SecureRandom();
365:                AlgorithmParameterSpec aps = null;
366:
367:                for (int i = 0; i < kpg.length; i++) {
368:
369:                    for (int j = 0; j < keys.length; j++) {
370:                        try {
371:                            kpg[i].initialize(keys[j]);
372:                            kpg[i].initialize(keys[j], random);
373:                        } catch (InvalidParameterException e) {
374:                        }
375:                    }
376:
377:                    try {
378:                        kpg[i].initialize(aps);
379:                        kpg[i].initialize(aps, random);
380:                    } catch (InvalidAlgorithmParameterException e) {
381:                    }
382:                }
383:            }
384:
385:            /**
386:             * Test for methods: <code>initialize(int keysize)</code>
387:             * <code>initialize(int keysize, SecureRandom random)</code>
388:             * <code>initialize(AlgorithmParameterSpec param)</code>
389:             * <code>initialize(AlgorithmParameterSpec param, SecureRandom random)</code>
390:             * <code>generateKeyPair()</code>
391:             * <code>genKeyPair()</code>
392:             * Assertion: throws InvalidParameterException or
393:             * InvalidAlgorithmParameterException when parameters keysize or param are
394:             * incorrect Assertion: generateKeyPair() and genKeyPair() return null
395:             * KeyPair Additional class MyKeyPairGenerator1 is used
396:             */
397:            public void testKeyPairGenerator12() {
398:                int[] keys = { -1, -250, 1, 64, 512, 1024 };
399:                SecureRandom random = new SecureRandom();
400:                AlgorithmParameterSpec aps;
401:                KeyPairGenerator mKPG = new MyKeyPairGenerator1("");
402:                assertEquals("Incorrect algorithm", mKPG.getAlgorithm(),
403:                        MyKeyPairGenerator1.getResAlgorithm());
404:
405:                mKPG.generateKeyPair();
406:                mKPG.genKeyPair();
407:
408:                for (int i = 0; i < keys.length; i++) {
409:                    try {
410:                        mKPG.initialize(keys[i]);
411:                        fail("InvalidParameterException must be thrown (key: "
412:                                + Integer.toString(keys[i]) + ")");
413:                    } catch (InvalidParameterException e) {
414:                    }
415:                    try {
416:                        mKPG.initialize(keys[i], random);
417:                        fail("InvalidParameterException must be thrown (key: "
418:                                + Integer.toString(keys[i]) + ")");
419:                    } catch (InvalidParameterException e) {
420:                    }
421:                }
422:                try {
423:                    mKPG.initialize(100, null);
424:                    fail("InvalidParameterException must be thrown when random is null");
425:                } catch (InvalidParameterException e) {
426:                }
427:
428:                mKPG.initialize(100, random);
429:                assertEquals("Incorrect random", random,
430:                        ((MyKeyPairGenerator1) mKPG).secureRandom);
431:                assertEquals("Incorrect keysize", 100,
432:                        ((MyKeyPairGenerator1) mKPG).keySize);
433:                try {
434:                    mKPG.initialize(null, random);
435:                    fail("InvalidAlgorithmParameterException must be thrown when param is null");
436:                } catch (InvalidAlgorithmParameterException e) {
437:                }
438:                if (DSASupported) {
439:                    BigInteger bInt = new BigInteger("1");
440:                    aps = new java.security.spec.DSAParameterSpec(bInt, bInt,
441:                            bInt);
442:                    try {
443:                        mKPG.initialize(aps, null);
444:                        fail("InvalidParameterException must be thrown when random is null");
445:                    } catch (InvalidParameterException e) {
446:                    } catch (InvalidAlgorithmParameterException e) {
447:                        fail("Unexpected InvalidAlgorithmParameterException was thrown");
448:                    }
449:                    try {
450:                        mKPG.initialize(aps, random);
451:                        assertEquals("Incorrect random", random,
452:                                ((MyKeyPairGenerator1) mKPG).secureRandom);
453:                        assertEquals("Incorrect params", aps,
454:                                ((MyKeyPairGenerator1) mKPG).paramSpec);
455:                    } catch (InvalidAlgorithmParameterException e) {
456:                        fail("Unexpected InvalidAlgorithmParameterException was thrown");
457:                    }
458:                }
459:            }
460:
461:            /**
462:             * Test for methods: <code>initialize(int keysize)</code>
463:             * <code>initialize(int keysize, SecureRandom random)</code>
464:             * <code>initialize(AlgorithmParameterSpec param)</code>
465:             * <code>initialize(AlgorithmParameterSpec param, SecureRandom random)</code>
466:             * <code>generateKeyPair()</code>
467:             * <code>genKeyPair()</code>
468:             * Assertion: initialize(int ...) throws InvalidParameterException when
469:             * keysize in incorrect Assertion: initialize(AlgorithmParameterSpec
470:             * ...)throws UnsupportedOperationException Assertion: generateKeyPair() and
471:             * genKeyPair() return not null KeyPair Additional class MyKeyPairGenerator2
472:             * is used
473:             */
474:            public void testKeyPairGenerator13() {
475:                int[] keys = { -1, -250, 1, 63, -512, -1024 };
476:                SecureRandom random = new SecureRandom();
477:                KeyPairGenerator mKPG = new MyKeyPairGenerator2(null);
478:                assertEquals("Algorithm must be null", mKPG.getAlgorithm(),
479:                        MyKeyPairGenerator2.getResAlgorithm());
480:                assertNull("genKeyPair() must return null", mKPG.genKeyPair());
481:                assertNull("generateKeyPair() mut return null", mKPG
482:                        .generateKeyPair());
483:                for (int i = 0; i < keys.length; i++) {
484:                    try {
485:                        mKPG.initialize(keys[i]);
486:                        fail("InvalidParameterException must be thrown (key: "
487:                                + Integer.toString(keys[i]) + ")");
488:                    } catch (InvalidParameterException e) {
489:                    }
490:                    try {
491:                        mKPG.initialize(keys[i], random);
492:                        fail("InvalidParameterException must be thrown (key: "
493:                                + Integer.toString(keys[i]) + ")");
494:                    } catch (InvalidParameterException e) {
495:                    }
496:                }
497:                try {
498:                    mKPG.initialize(64);
499:                } catch (InvalidParameterException e) {
500:                    fail("Unexpected InvalidParameterException was thrown");
501:                }
502:                try {
503:                    mKPG.initialize(64, null);
504:                } catch (InvalidParameterException e) {
505:                    fail("Unexpected InvalidParameterException was thrown");
506:                }
507:                try {
508:                    mKPG.initialize(null, random);
509:                } catch (UnsupportedOperationException e) {
510:                    // on j2se1.4 this exception is not thrown
511:                } catch (InvalidAlgorithmParameterException e) {
512:                    fail("Unexpected InvalidAlgorithmParameterException was thrown");
513:                }
514:            }
515:
516:            public static void main(String args[]) {
517:                junit.textui.TestRunner.run(KeyPairGenerator1Test.class);
518:            }
519:        }
w__w__w__.__ja__v___a2_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.