Source Code Cross Referenced for AlgorithmParameterGenerator1Test.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.security.spec.AlgorithmParameterSpec;
025:
026:        import org.apache.harmony.security.tests.support.MyAlgorithmParameterGeneratorSpi;
027:        import org.apache.harmony.security.tests.support.SpiEngUtils;
028:
029:        import junit.framework.TestCase;
030:
031:        /**
032:         * Tests for <code>AlgorithmParameterGenerator</code> class constructors and
033:         * methods.
034:         * 
035:         */
036:
037:        public class AlgorithmParameterGenerator1Test extends TestCase {
038:            /**
039:             * Constructor for AlgorithmParameterGeneratorTests.
040:             * 
041:             * @param arg0
042:             */
043:            public AlgorithmParameterGenerator1Test(String arg0) {
044:                super (arg0);
045:            }
046:
047:            private static String[] invalidValues = SpiEngUtils.invalidValues;
048:            private static String validAlgName = "DSA";
049:            private static String[] algs = { "DSA", "dsa", "Dsa", "DsA", "dsA" };
050:
051:            public static final String srvAlgorithmParameterGenerator = "AlgorithmParameterGenerator";
052:
053:            private static String validProviderName = null;
054:
055:            private static Provider validProvider = null;
056:
057:            private static boolean DSASupported = false;
058:
059:            static {
060:                validProvider = SpiEngUtils.isSupport(validAlgName,
061:                        srvAlgorithmParameterGenerator);
062:                DSASupported = (validProvider != null);
063:                validProviderName = (DSASupported ? validProvider.getName()
064:                        : null);
065:            }
066:
067:            protected AlgorithmParameterGenerator[] createAPGen() {
068:                if (!DSASupported) {
069:                    fail(validAlgName + " algorithm is not supported");
070:                    return null;
071:                }
072:                AlgorithmParameterGenerator[] apg = new AlgorithmParameterGenerator[3];
073:                try {
074:                    apg[0] = AlgorithmParameterGenerator
075:                            .getInstance(validAlgName);
076:                    apg[1] = AlgorithmParameterGenerator.getInstance(
077:                            validAlgName, validProvider);
078:                    apg[2] = AlgorithmParameterGenerator.getInstance(
079:                            validAlgName, validProviderName);
080:                    return apg;
081:                } catch (Exception e) {
082:                    e.printStackTrace();
083:                    return null;
084:                }
085:            }
086:
087:            /**
088:             * Test for <code>getInstance(String algorithm)</code> method
089:             * Assertion: 
090:             * throws NullPointerException must be thrown is null
091:             * throws NoSuchAlgorithmException must be thrown if algorithm is not available
092:             * 
093:             */
094:            public void testAlgorithmParameterGenerator01()
095:                    throws NoSuchAlgorithmException {
096:                try {
097:                    AlgorithmParameterGenerator.getInstance(null);
098:                    fail("NullPointerException or NoSuchAlgorithmException should be thrown");
099:                } catch (NullPointerException e) {
100:                } catch (NoSuchAlgorithmException e) {
101:                }
102:                for (int i = 0; i < invalidValues.length; i++) {
103:                    try {
104:                        AlgorithmParameterGenerator
105:                                .getInstance(invalidValues[i]);
106:                        fail("NoSuchAlgorithmException should be thrown");
107:                    } catch (NoSuchAlgorithmException e) {
108:                    }
109:                }
110:            }
111:
112:            /**
113:             * Test for <code>getInstance(String algorithm)</code> method
114:             * Assertion: returns AlgorithmParameterGenerator instance 
115:             * when algorithm is DSA
116:             */
117:            public void testAlgorithmParameterGenerator02()
118:                    throws NoSuchAlgorithmException {
119:                if (!DSASupported) {
120:                    fail(validAlgName + " algorithm is not supported");
121:                    return;
122:                }
123:                AlgorithmParameterGenerator apg;
124:                for (int i = 0; i < algs.length; i++) {
125:                    apg = AlgorithmParameterGenerator.getInstance(algs[i]);
126:                    assertEquals("Incorrect algorithm", apg.getAlgorithm(),
127:                            algs[i]);
128:                }
129:            }
130:
131:            /**
132:             * Test for <code>getInstance(String algorithm, String provider)</code>
133:             * method 
134:             * Assertion: 
135:             * throws IllegalArgumentException if provider is null or empty
136:             */
137:            public void testAlgorithmParameterGenerator03()
138:                    throws NoSuchAlgorithmException, NoSuchProviderException {
139:                if (!DSASupported) {
140:                    fail(validAlgName + " algorithm is not supported");
141:                    return;
142:                }
143:                String provider = null;
144:                for (int i = 0; i < algs.length; i++) {
145:                    try {
146:                        AlgorithmParameterGenerator.getInstance(algs[i],
147:                                provider);
148:                        fail("IllegalArgumentException must be thrown when provider is null");
149:                    } catch (IllegalArgumentException e) {
150:                    }
151:                    try {
152:                        AlgorithmParameterGenerator.getInstance(algs[i], "");
153:                        fail("IllegalArgumentException must be thrown when provider is empty");
154:                    } catch (IllegalArgumentException e) {
155:                    }
156:                }
157:            }
158:
159:            /**
160:             * Test for <code>getInstance(String algorithm, String provider)</code>
161:             * method 
162:             * Assertion: throws NoSuchProviderException if provider is not
163:             * available
164:             */
165:            public void testAlgorithmParameterGenerator04()
166:                    throws NoSuchAlgorithmException {
167:                if (!DSASupported) {
168:                    fail(validAlgName + " algorithm is not supported");
169:                    return;
170:                }
171:                for (int i = 0; i < algs.length; i++) {
172:                    for (int j = 1; j < invalidValues.length; j++) {
173:                        try {
174:                            AlgorithmParameterGenerator.getInstance(algs[i],
175:                                    invalidValues[j]);
176:                            fail("NoSuchProviderException must be thrown (provider: "
177:                                    .concat(invalidValues[j]));
178:                        } catch (NoSuchProviderException e) {
179:                        }
180:                    }
181:                }
182:            }
183:
184:            /**
185:             * Test for <code>getInstance(String algorithm, String provider)</code>
186:             * method 
187:             * Assertion: 
188:             * throws NullPointerException must be thrown is null
189:             * throws NoSuchAlgorithmException must be thrown if algorithm is not available
190:             */
191:            public void testAlgorithmParameterGenerator05()
192:                    throws NoSuchProviderException {
193:                if (!DSASupported) {
194:                    fail(validAlgName + " algorithm is not supported");
195:                    return;
196:                }
197:                try {
198:                    AlgorithmParameterGenerator.getInstance(null,
199:                            validProviderName);
200:                    fail("NullPointerException or NoSuchAlgorithmException should be thrown");
201:                } catch (NullPointerException e) {
202:                } catch (NoSuchAlgorithmException e) {
203:                }
204:                for (int i = 0; i < invalidValues.length; i++) {
205:                    try {
206:                        AlgorithmParameterGenerator.getInstance(
207:                                invalidValues[i], validProviderName);
208:                        fail("NoSuchAlgorithmException must be thrown when (algorithm: "
209:                                .concat(invalidValues[i].concat(")")));
210:                    } catch (NoSuchAlgorithmException e) {
211:                    }
212:                }
213:            }
214:
215:            /**
216:             * Test for <code>getInstance(String algorithm, String provider)</code>
217:             * method 
218:             * Assertion: return AlgorithmParameterGenerator
219:             */
220:            public void testAlgorithmParameterGenerator06()
221:                    throws NoSuchAlgorithmException, NoSuchProviderException {
222:                if (!DSASupported) {
223:                    fail(validAlgName + " algorithm is not supported");
224:                    return;
225:                }
226:                AlgorithmParameterGenerator apg;
227:                for (int i = 0; i < algs.length; i++) {
228:                    apg = AlgorithmParameterGenerator.getInstance(algs[i],
229:                            validProviderName);
230:                    assertEquals("Incorrect algorithm", algs[i], apg
231:                            .getAlgorithm());
232:                    assertEquals("Incorrect provider", apg.getProvider()
233:                            .getName(), validProviderName);
234:                }
235:            }
236:
237:            /**
238:             * Test for <code>getInstance(String algorithm, Provider provider)</code>
239:             * method 
240:             * Assertion: throws IllegalArgumentException when provider is null
241:             */
242:            public void testAlgorithmParameterGenerator07()
243:                    throws NoSuchAlgorithmException {
244:                if (!DSASupported) {
245:                    fail(validAlgName + " algorithm is not supported");
246:                    return;
247:                }
248:                Provider provider = null;
249:                for (int i = 0; i < algs.length; i++) {
250:                    try {
251:                        AlgorithmParameterGenerator.getInstance(algs[i],
252:                                provider);
253:                        fail("IllegalArgumentException must be thrown when provider is null");
254:                    } catch (IllegalArgumentException e) {
255:                    }
256:                }
257:            }
258:
259:            /**
260:             * Test for <code>getInstance(String algorithm, Provider provider)</code>
261:             * method 
262:             * Assertion: 
263:             * throws NullPointerException must be thrown is null
264:             * throws NoSuchAlgorithmException must be thrown if algorithm is not available
265:             */
266:            public void testAlgorithmParameterGenerator08() {
267:                if (!DSASupported) {
268:                    fail(validAlgName + " algorithm is not supported");
269:                    return;
270:                }
271:                try {
272:                    AlgorithmParameterGenerator
273:                            .getInstance(null, validProvider);
274:                    fail("NullPointerException or NoSuchAlgorithmException should be thrown");
275:                } catch (NullPointerException e) {
276:                } catch (NoSuchAlgorithmException e) {
277:                }
278:                for (int i = 0; i < invalidValues.length; i++) {
279:                    try {
280:                        AlgorithmParameterGenerator.getInstance(
281:                                invalidValues[i], validProvider);
282:                        fail("NoSuchAlgorithmException must be thrown (algorithm: "
283:                                .concat(invalidValues[i]).concat(")"));
284:                    } catch (NoSuchAlgorithmException e) {
285:                    }
286:                }
287:            }
288:
289:            /**
290:             * Test for <code>getInstance(String algorithm, Provider provider)</code>
291:             * method 
292:             * Assertion: returns AlgorithmParameterGenerator object
293:             */
294:            public void testAlgorithmParameterGenerator09()
295:                    throws NoSuchAlgorithmException {
296:                if (!DSASupported) {
297:                    fail(validAlgName + " algorithm is not supported");
298:                    return;
299:                }
300:                AlgorithmParameterGenerator apg;
301:                for (int i = 0; i < algs.length; i++) {
302:                    apg = AlgorithmParameterGenerator.getInstance(algs[i],
303:                            validProvider);
304:                    assertEquals("Incorrect algorithm", apg.getAlgorithm(),
305:                            algs[i]);
306:                    assertEquals("Incorrect provider", apg.getProvider(),
307:                            validProvider);
308:                }
309:            }
310:
311:            /**
312:             * Test for <code>generateParameters()</code> method
313:             * Assertion: returns AlgorithmParameters object
314:             */
315:            public void testAlgorithmParameterGenerator10()
316:                    throws NoSuchAlgorithmException {
317:                if (!DSASupported) {
318:                    fail(validAlgName + " algorithm is not supported");
319:                    return;
320:                }
321:                AlgorithmParameterGenerator apg = AlgorithmParameterGenerator
322:                        .getInstance(validAlgName);
323:                apg.init(512);
324:                AlgorithmParameters ap = apg.generateParameters();
325:                assertEquals("Incorrect algorithm", ap.getAlgorithm()
326:                        .toUpperCase(), apg.getAlgorithm().toUpperCase());
327:            }
328:
329:            /**
330:             * Test for <code>init(AlgorithmParameterSpec param)</code> and 
331:             * <code>init(AlgorithmParameterSpec param, SecureRandom random<code> 
332:             * methods
333:             * Assertion: throws InvalidAlgorithmParameterException when param is null
334:             */
335:            public void testAlgorithmParameterGenerator12() {
336:                if (!DSASupported) {
337:                    fail(validAlgName + " algorithm is not supported");
338:                    return;
339:                }
340:                SecureRandom random = new SecureRandom();
341:                AlgorithmParameterSpec aps = null;
342:                AlgorithmParameterGenerator[] apgs = createAPGen();
343:                assertNotNull(
344:                        "AlgorithmParameterGenerator objects were not created",
345:                        apgs);
346:                for (int i = 0; i < apgs.length; i++) {
347:                    try {
348:                        apgs[i].init(aps, random);
349:                        fail("InvalidAlgorithmParameterException must be throws when param is null");
350:                    } catch (InvalidAlgorithmParameterException e) {
351:                    }
352:                }
353:            }
354:
355:            /**
356:             * Test for <code>AlgorithmParameterGenerator</code> constructor 
357:             * Assertion: returns AlgorithmParameterGenerator object
358:             */
359:            public void testAlgorithmParameterGeneratorConstr()
360:                    throws NoSuchAlgorithmException {
361:                if (!DSASupported) {
362:                    fail(validAlgName + " algorithm is not supported");
363:                    return;
364:                }
365:                AlgorithmParameterGeneratorSpi spi = new MyAlgorithmParameterGeneratorSpi();
366:                AlgorithmParameterGenerator apg = new myAlgPG(spi,
367:                        validProvider, validAlgName);
368:                assertEquals("Incorrect algorithm", apg.getAlgorithm(),
369:                        validAlgName);
370:                assertEquals("Incorrect provider", apg.getProvider(),
371:                        validProvider);
372:                try {
373:                    apg.init(-10, null);
374:                    fail("IllegalArgumentException must be thrown");
375:                } catch (IllegalArgumentException e) {
376:                }
377:
378:                apg = new myAlgPG(null, null, null);
379:                assertNull("Incorrect algorithm", apg.getAlgorithm());
380:                assertNull("Incorrect provider", apg.getProvider());
381:                try {
382:                    apg.init(-10, null);
383:                    fail("NullPointerException must be thrown");
384:                } catch (NullPointerException e) {
385:                }
386:            }
387:
388:            public static void main(String args[]) {
389:                junit.textui.TestRunner
390:                        .run(AlgorithmParameterGenerator1Test.class);
391:            }
392:        }
393:
394:        /**
395:         * Additional class to verify AlgorithmParameterGenerator constructor
396:         */
397:        class myAlgPG extends AlgorithmParameterGenerator {
398:            public myAlgPG(AlgorithmParameterGeneratorSpi spi, Provider prov,
399:                    String alg) {
400:                super(spi, prov, alg);
401:            }
402:        }
ww__w.__j_a__v___a2_s__.co___m_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.