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 Boris V. Kuznetsov
020: * @version $Revision$
021: */package org.apache.harmony.security.tests.java.security;
022:
023: import java.io.IOException;
024: import java.math.BigInteger;
025: import java.security.AlgorithmParameters;
026: import java.security.AlgorithmParametersSpi;
027: import java.security.Provider;
028: import java.security.Security;
029: import java.security.spec.AlgorithmParameterSpec;
030: import java.security.spec.DSAParameterSpec;
031: import java.security.spec.InvalidParameterSpecException;
032: import java.util.Arrays;
033:
034: import junit.framework.TestCase;
035:
036: /**
037: * Tests for <code>AlgorithmParameters</code> class constructors and
038: * methods.
039: *
040: */
041: public class AlgorithmParametersTest extends TestCase {
042:
043: /**
044: * Provider
045: */
046: Provider p;
047:
048: /*
049: * @see TestCase#setUp()
050: */
051: protected void setUp() throws Exception {
052: super .setUp();
053: p = new MyProvider();
054: Security.insertProviderAt(p, 1);
055: }
056:
057: /*
058: * @see TestCase#tearDown()
059: */
060: protected void tearDown() throws Exception {
061: super .tearDown();
062: Security.removeProvider(p.getName());
063: }
064:
065: /**
066: * @tests java.security.AlgorithmParameters#getAlgorithm()
067: */
068: public void test_getAlgorithm() throws Exception {
069:
070: // test: null value
071: AlgorithmParameters ap = new DummyAlgorithmParameters(null, p,
072: null);
073: assertNull(ap.getAlgorithm());
074:
075: // test: not null value
076: ap = new DummyAlgorithmParameters(null, p, "AAA");
077: assertEquals("AAA", ap.getAlgorithm());
078: }
079:
080: /**
081: * @tests java.security.AlgorithmParameters#getEncoded()
082: */
083: public void test_getEncoded() throws Exception {
084:
085: final byte[] enc = new byte[] { 0x02, 0x01, 0x03 };
086:
087: MyAlgorithmParameters paramSpi = new MyAlgorithmParameters() {
088: protected byte[] engineGetEncoded() throws IOException {
089: return enc;
090: }
091: };
092:
093: AlgorithmParameters params = new DummyAlgorithmParameters(
094: paramSpi, p, "algorithm");
095:
096: //
097: // test: IOException if not initialized
098: //
099: try {
100: params.getEncoded();
101: fail("should not get encoded from un-initialized instance");
102: } catch (IOException e) {
103: // expected
104: }
105:
106: //
107: // test: corresponding spi method is invoked
108: //
109: params.init(new MyAlgorithmParameterSpec());
110: assertSame(enc, params.getEncoded());
111: }
112:
113: /**
114: * @tests java.security.AlgorithmParameters#getEncoded(String)
115: */
116: public void test_getEncodedLjava_lang_String() throws Exception {
117:
118: final byte[] enc = new byte[] { 0x02, 0x01, 0x03 };
119:
120: final String strFormatParam = "format";
121:
122: MyAlgorithmParameters paramSpi = new MyAlgorithmParameters() {
123: protected byte[] engineGetEncoded(String format)
124: throws IOException {
125: assertEquals(strFormatParam, format);
126: return enc;
127: }
128: };
129:
130: AlgorithmParameters params = new DummyAlgorithmParameters(
131: paramSpi, p, "algorithm");
132:
133: //
134: // test: IOException if not initialized
135: //
136: try {
137: params.getEncoded(strFormatParam);
138: fail("should not get encoded from un-initialized instance");
139: } catch (IOException e) {
140: // expected
141: }
142:
143: //
144: // test: corresponding spi method is invoked
145: //
146: params.init(new MyAlgorithmParameterSpec());
147: assertSame(enc, params.getEncoded(strFormatParam));
148:
149: //
150: // test: if format param is null
151: // Regression test for HARMONY-2680
152: //
153: paramSpi = new MyAlgorithmParameters() {
154: protected byte[] engineGetEncoded(String format)
155: throws IOException {
156: assertNull(format); // null is passed to spi-provider
157: return enc;
158: }
159: };
160:
161: params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
162: params.init(new MyAlgorithmParameterSpec());
163: assertSame(enc, params.getEncoded(null));
164: }
165:
166: /**
167: * @tests java.security.AlgorithmParameters#getInstance(String)
168: */
169: public void test_getInstanceLjava_lang_String() throws Exception {
170:
171: AlgorithmParameters ap = AlgorithmParameters.getInstance("ABC");
172:
173: checkUnititialized(ap);
174:
175: ap.init(new MyAlgorithmParameterSpec());
176:
177: checkAP(ap, p);
178: }
179:
180: /**
181: * @tests java.security.AlgorithmParameters#getInstance(String, String)
182: */
183: public void test_getInstanceLjava_lang_StringLjava_lang_String()
184: throws Exception {
185:
186: AlgorithmParameters ap = AlgorithmParameters.getInstance("ABC",
187: "MyProvider");
188:
189: checkUnititialized(ap);
190:
191: ap.init(new byte[6]);
192:
193: checkAP(ap, p);
194: }
195:
196: /**
197: * @tests java.security.AlgorithmParameters#getParameterSpec(Class)
198: */
199: public void test_getParameterSpecLjava_lang_Class()
200: throws Exception {
201:
202: final MyAlgorithmParameterSpec myParamSpec = new MyAlgorithmParameterSpec();
203:
204: MyAlgorithmParameters paramSpi = new MyAlgorithmParameters() {
205: protected AlgorithmParameterSpec engineGetParameterSpec(
206: Class paramSpec) {
207: return myParamSpec;
208: }
209: };
210:
211: AlgorithmParameters params = new DummyAlgorithmParameters(
212: paramSpi, p, "algorithm");
213:
214: //
215: // test: InvalidParameterSpecException if not initialized
216: //
217: try {
218: params.getParameterSpec(null);
219: fail("No expected InvalidParameterSpecException");
220: } catch (InvalidParameterSpecException e) {
221: // expected
222: }
223: try {
224: params.getParameterSpec(MyAlgorithmParameterSpec.class);
225: fail("No expected InvalidParameterSpecException");
226: } catch (InvalidParameterSpecException e) {
227: // expected
228: }
229:
230: //
231: // test: corresponding spi method is invoked
232: //
233: params.init(new MyAlgorithmParameterSpec());
234: assertSame(myParamSpec, params
235: .getParameterSpec(MyAlgorithmParameterSpec.class));
236:
237: //
238: // test: if paramSpec is null
239: // Regression test for HARMONY-2733
240: //
241: paramSpi = new MyAlgorithmParameters() {
242:
243: protected AlgorithmParameterSpec engineGetParameterSpec(
244: Class paramSpec) {
245: assertNull(paramSpec); // null is passed to spi-provider
246: return null;
247: }
248: };
249:
250: params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
251: params.init(new MyAlgorithmParameterSpec());
252: assertNull(params.getParameterSpec(null));
253: }
254:
255: /**
256: * @tests java.security.AlgorithmParameters#getInstance(String, Provider)
257: */
258: public void test_getInstanceLjava_lang_StringLjava_security_Provider()
259: throws Exception {
260:
261: AlgorithmParameters ap = AlgorithmParameters.getInstance("ABC",
262: p);
263:
264: checkUnititialized(ap);
265:
266: ap.init(new byte[6], "aaa");
267:
268: checkAP(ap, p);
269: }
270:
271: /**
272: * @tests java.security.AlgorithmParameters#getProvider()
273: */
274: public void test_getProvider() throws Exception {
275: // test: null value
276: AlgorithmParameters ap = new DummyAlgorithmParameters(null,
277: null, "AAA");
278: assertNull(ap.getProvider());
279:
280: // test: not null value
281: ap = new DummyAlgorithmParameters(null, p, "AAA");
282: assertSame(p, ap.getProvider());
283: }
284:
285: /**
286: * @tests java.security.AlgorithmParameters#init(java.security.spec.AlgorithmParameterSpec)
287: */
288: public void test_initLjava_security_spec_AlgorithmParameterSpec()
289: throws Exception {
290:
291: //
292: // test: corresponding spi method is invoked
293: //
294: final MyAlgorithmParameterSpec spec = new MyAlgorithmParameterSpec();
295:
296: MyAlgorithmParameters paramSpi = new MyAlgorithmParameters() {
297: protected void engineInit(AlgorithmParameterSpec paramSpec)
298: throws InvalidParameterSpecException {
299: assertSame(spec, paramSpec);
300: runEngineInit_AlgParamSpec = true;
301: }
302: };
303:
304: AlgorithmParameters params = new DummyAlgorithmParameters(
305: paramSpi, p, "algorithm");
306:
307: params.init(spec);
308: assertTrue(paramSpi.runEngineInit_AlgParamSpec);
309:
310: //
311: // test: InvalidParameterSpecException if already initialized
312: //
313: try {
314: params.init(spec);
315: fail("No expected InvalidParameterSpecException");
316: } catch (InvalidParameterSpecException e) {
317: // expected
318: }
319:
320: params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
321: params.init(new byte[0]);
322: try {
323: params.init(spec);
324: fail("No expected InvalidParameterSpecException");
325: } catch (InvalidParameterSpecException e) {
326: // expected
327: }
328:
329: params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
330: params.init(new byte[0], "format");
331: try {
332: params.init(spec);
333: fail("No expected InvalidParameterSpecException");
334: } catch (InvalidParameterSpecException e) {
335: // expected
336: }
337:
338: //
339: // test: if paramSpec is null
340: //
341: paramSpi = new MyAlgorithmParameters() {
342:
343: protected void engineInit(AlgorithmParameterSpec paramSpec)
344: throws InvalidParameterSpecException {
345: assertNull(paramSpec);// null is passed to spi-provider
346: runEngineInit_AlgParamSpec = true;
347: }
348: };
349:
350: params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
351: params.init((AlgorithmParameterSpec) null);
352: assertTrue(paramSpi.runEngineInit_AlgParamSpec);
353: }
354:
355: /**
356: * @tests java.security.AlgorithmParameters#init(byte[])
357: */
358: public void test_init$B() throws Exception {
359:
360: //
361: // test: corresponding spi method is invoked
362: //
363: final byte[] enc = new byte[] { 0x02, 0x01, 0x03 };
364:
365: MyAlgorithmParameters paramSpi = new MyAlgorithmParameters() {
366: protected void engineInit(byte[] params) throws IOException {
367: runEngineInitB$ = true;
368: assertSame(enc, params);
369: }
370: };
371:
372: AlgorithmParameters params = new DummyAlgorithmParameters(
373: paramSpi, p, "algorithm");
374:
375: params.init(enc);
376: assertTrue(paramSpi.runEngineInitB$);
377:
378: //
379: // test: IOException if already initialized
380: //
381: try {
382: params.init(enc);
383: fail("No expected IOException");
384: } catch (IOException e) {
385: // expected
386: }
387:
388: params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
389: params.init(new MyAlgorithmParameterSpec());
390: try {
391: params.init(enc);
392: fail("No expected IOException");
393: } catch (IOException e) {
394: // expected
395: }
396:
397: params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
398: params.init(enc, "format");
399: try {
400: params.init(enc);
401: fail("No expected IOException");
402: } catch (IOException e) {
403: // expected
404: }
405:
406: //
407: // test: if params is null
408: //
409: paramSpi = new MyAlgorithmParameters() {
410:
411: protected void engineInit(byte[] params) throws IOException {
412: runEngineInitB$ = true;
413: assertNull(params); // null is passed to spi-provider
414: }
415: };
416:
417: params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
418: params.init((byte[]) null);
419: assertTrue(paramSpi.runEngineInitB$);
420: }
421:
422: /**
423: * @tests java.security.AlgorithmParameters#init(byte[],String)
424: */
425: public void test_init$BLjava_lang_String() throws Exception {
426:
427: //
428: // test: corresponding spi method is invoked
429: //
430: final byte[] enc = new byte[] { 0x02, 0x01, 0x03 };
431: final String strFormatParam = "format";
432:
433: MyAlgorithmParameters paramSpi = new MyAlgorithmParameters() {
434: protected void engineInit(byte[] params, String format)
435: throws IOException {
436:
437: runEngineInitB$String = true;
438: assertSame(enc, params);
439: assertSame(strFormatParam, format);
440: }
441: };
442:
443: AlgorithmParameters params = new DummyAlgorithmParameters(
444: paramSpi, p, "algorithm");
445:
446: params.init(enc, strFormatParam);
447: assertTrue(paramSpi.runEngineInitB$String);
448:
449: //
450: // test: IOException if already initialized
451: //
452: try {
453: params.init(enc, strFormatParam);
454: fail("No expected IOException");
455: } catch (IOException e) {
456: // expected
457: }
458:
459: params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
460: params.init(new MyAlgorithmParameterSpec());
461: try {
462: params.init(enc, strFormatParam);
463: fail("No expected IOException");
464: } catch (IOException e) {
465: // expected
466: }
467:
468: params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
469: params.init(enc);
470: try {
471: params.init(enc, strFormatParam);
472: fail("No expected IOException");
473: } catch (IOException e) {
474: // expected
475: }
476:
477: //
478: // test: if params and format are null
479: // Regression test for HARMONY-2724
480: //
481: paramSpi = new MyAlgorithmParameters() {
482:
483: protected void engineInit(byte[] params, String format)
484: throws IOException {
485:
486: runEngineInitB$String = true;
487:
488: // null is passed to spi-provider
489: assertNull(params);
490: assertNull(format);
491: }
492: };
493:
494: params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
495: params.init(null, null);
496: assertTrue(paramSpi.runEngineInitB$String);
497: }
498:
499: /**
500: * @tests java.security.AlgorithmParameters#toString()
501: */
502: public void test_toString() throws Exception {
503:
504: final String str = "AlgorithmParameters";
505:
506: MyAlgorithmParameters paramSpi = new MyAlgorithmParameters() {
507: protected String engineToString() {
508: return str;
509: }
510: };
511:
512: AlgorithmParameters params = new DummyAlgorithmParameters(
513: paramSpi, p, "algorithm");
514:
515: assertNull("unititialized", params.toString());
516:
517: params.init(new byte[0]);
518:
519: assertSame(str, params.toString());
520: }
521:
522: /**
523: * Tests DSA AlgorithmParameters provider
524: */
525: public void testDSAProvider() throws Exception {
526: AlgorithmParameters params = AlgorithmParameters
527: .getInstance("DSA");
528:
529: assertEquals("Algorithm", "DSA", params.getAlgorithm());
530:
531: // init(AlgorithmParameterSpec)
532: BigInteger p = BigInteger.ONE;
533: BigInteger q = BigInteger.TEN;
534: BigInteger g = BigInteger.ZERO;
535: params.init(new DSAParameterSpec(p, q, g));
536:
537: // getEncoded() and getEncoded(String) (TODO verify returned encoding)
538: byte[] enc = params.getEncoded();
539: assertNotNull(enc);
540: assertNotNull(params.getEncoded("ASN.1"));
541: // TODO assertNotNull(params.getEncoded(null)); // HARMONY-2680
542:
543: // getParameterSpec(Class)
544: DSAParameterSpec spec = params
545: .getParameterSpec(DSAParameterSpec.class);
546: assertEquals("p is wrong ", p, spec.getP());
547: assertEquals("q is wrong ", q, spec.getQ());
548: assertEquals("g is wrong ", g, spec.getG());
549:
550: // init(byte[])
551: params = AlgorithmParameters.getInstance("DSA");
552: params.init(enc);
553: assertTrue("param encoded is different", Arrays.equals(enc,
554: params.getEncoded()));
555:
556: // init(byte[], String)
557: params = AlgorithmParameters.getInstance("DSA");
558: params.init(enc, "ASN.1");
559: assertTrue("param encoded is different", Arrays.equals(enc,
560: params.getEncoded()));
561:
562: params = AlgorithmParameters.getInstance("DSA");
563: try {
564: params.init(enc, "DOUGLASMAWSON");
565: fail("unsupported format should have raised IOException");
566: } catch (IOException e) {
567: // expected
568: }
569: }
570:
571: /**
572: * Tests OAEP AlgorithmParameters provider
573: */
574: public void testOAEPProvider() throws Exception {
575: AlgorithmParameters params = AlgorithmParameters
576: .getInstance("OAEP");
577:
578: assertEquals("Algorithm", "OAEP", params.getAlgorithm());
579: }
580:
581: private void checkUnititialized(AlgorithmParameters ap) {
582: assertNull("Uninitialized: toString() failed", ap.toString());
583: }
584:
585: private void checkAP(AlgorithmParameters ap, Provider p)
586: throws Exception {
587:
588: assertSame("getProvider() failed", p, ap.getProvider());
589: assertEquals("getAlgorithm() failed", "ABC", ap.getAlgorithm());
590:
591: assertEquals("AlgorithmParameters", ap.toString());
592: assertTrue("toString() failed",
593: MyAlgorithmParameters.runEngineToString);
594: }
595:
596: @SuppressWarnings("serial")
597: private class MyProvider extends Provider {
598: MyProvider() {
599: super ("MyProvider", 1.0, "Provider for testing");
600: put("AlgorithmParameters.ABC", MyAlgorithmParameters.class
601: .getName());
602: }
603:
604: MyProvider(String name, double version, String info) {
605: super (name, version, info);
606: }
607: }
608:
609: private class MyAlgorithmParameterSpec implements
610: java.security.spec.AlgorithmParameterSpec {
611: }
612:
613: private class DummyAlgorithmParameters extends AlgorithmParameters {
614: public DummyAlgorithmParameters(
615: AlgorithmParametersSpi paramSpi, Provider provider,
616: String algorithm) {
617: super (paramSpi, provider, algorithm);
618: }
619: }
620:
621: public static class MyAlgorithmParameters extends
622: AlgorithmParametersSpi {
623:
624: public boolean runEngineInit_AlgParamSpec = false;
625:
626: public boolean runEngineInitB$ = false;
627:
628: public boolean runEngineInitB$String = false;
629:
630: public static boolean runEngineToString = false;
631:
632: protected void engineInit(AlgorithmParameterSpec paramSpec)
633: throws InvalidParameterSpecException {
634: }
635:
636: protected void engineInit(byte[] params) throws IOException {
637: }
638:
639: protected void engineInit(byte[] params, String format)
640: throws IOException {
641: }
642:
643: protected AlgorithmParameterSpec engineGetParameterSpec(
644: Class paramSpec) throws InvalidParameterSpecException {
645: return null;
646: }
647:
648: protected byte[] engineGetEncoded() throws IOException {
649: return null;
650: }
651:
652: protected byte[] engineGetEncoded(String format)
653: throws IOException {
654: return null;
655: }
656:
657: protected String engineToString() {
658: runEngineToString = true;
659: return "AlgorithmParameters";
660: }
661: }
662: }
|