OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / org / apache / harmony / crypto / tests / javax / crypto / KeyAgreementTest.java
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17
18 /**
19 * @author Vera Y. Petrashkova
20 * @version $Revision$
21 */
22
23 package org.apache.harmony.crypto.tests.javax.crypto;
24
25 import dalvik.annotation.KnownFailure;
26 import dalvik.annotation.TestTargetClass;
27 import dalvik.annotation.TestTargets;
28 import dalvik.annotation.TestLevel;
29 import dalvik.annotation.TestTargetNew;
30
31 import java.math.BigInteger;
32 import java.security.InvalidAlgorithmParameterException;
33 import java.security.InvalidKeyException;
34 import java.security.NoSuchAlgorithmException;
35 import java.security.NoSuchProviderException;
36 import java.security.PrivateKey;
37 import java.security.Provider;
38 import java.security.PublicKey;
39 import java.security.SecureRandom;
40 import java.security.spec.AlgorithmParameterSpec;
41 import java.security.spec.DSAParameterSpec;
42 import java.security.spec.RSAKeyGenParameterSpec;
43
44 import javax.crypto.KeyAgreement;
45 import javax.crypto.KeyAgreementSpi;
46 import javax.crypto.ShortBufferException;
47 import javax.crypto.interfaces.DHPrivateKey;
48 import javax.crypto.spec.DHParameterSpec;
49
50 import org.apache.harmony.crypto.tests.support.MyKeyAgreementSpi;
51 import org.apache.harmony.security.tests.support.SpiEngUtils;
52 import org.apache.harmony.security.tests.support.TestKeyPair;
53
54 import junit.framework.TestCase;
55
56
57 @TestTargetClass(KeyAgreement.class)
58 /**
59  * Tests for KeyAgreement constructor and methods
60  *
61  */
62
63 public class KeyAgreementTest extends TestCase {
64
65     public static final String srvKeyAgreement = "KeyAgreement";
66
67     private static String defaultAlgorithm = "DH";
68
69     private static String defaultProviderName = null;
70
71     private static Provider defaultProvider = null;
72
73     private static boolean DEFSupported = false;
74
75     private static final String NotSupportMsg = "There is no suitable provider for KeyAgreement";
76
77     private static final String[] invalidValues = SpiEngUtils.invalidValues;
78
79     private static String[] validValues = { "DH", "dH",
80             "Dh", "dh" };
81
82     private static PrivateKey privKey = null;
83
84     private static PublicKey publKey = null;
85
86     private static boolean initKeys = false;
87
88     static {
89         defaultProvider = SpiEngUtils.isSupport(defaultAlgorithm,
90                 srvKeyAgreement);
91         DEFSupported = (defaultProvider != null);
92         defaultProviderName = (DEFSupported ? defaultProvider.getName() : null);
93     }
94
95     private void createKeys() throws Exception {
96         if (!initKeys) {
97             TestKeyPair tkp = new TestKeyPair(defaultAlgorithm);
98             privKey = tkp.getPrivate();
99             publKey = tkp.getPublic();
100             initKeys = true;
101         }
102
103     }
104
105     private KeyAgreement[] createKAs() throws Exception {
106         if (!DEFSupported) {
107             fail(NotSupportMsg);
108         }
109
110         KeyAgreement[] ka = new KeyAgreement[3];
111         ka[0] = KeyAgreement.getInstance(defaultAlgorithm);
112         ka[1] = KeyAgreement.getInstance(defaultAlgorithm, defaultProvider);
113         ka[2] = KeyAgreement.getInstance(defaultAlgorithm,
114                 defaultProviderName);
115         return ka;
116     }
117
118     public static String getDefAlg() {
119         return defaultAlgorithm;
120     }
121
122     /**
123      * Test for <code> getInstance(String algorithm) </code> method Assertions:
124      * throws NullPointerException when algorithm is null throws
125      * NoSuchAlgorithmException when algorithm isnot available
126      */
127     @TestTargetNew(
128         level = TestLevel.PARTIAL_COMPLETE,
129         notes = "This is a complete subset of tests for getInstance method.",
130         method = "getInstance",
131         args = {java.lang.String.class}
132     )
133     public void testGetInstanceString01() throws NoSuchAlgorithmException {
134         try {
135             KeyAgreement.getInstance(null);
136             fail("NullPointerException or NoSuchAlgorithmException should be thrown if algorithm is null");
137         } catch (NullPointerException e) {
138         } catch (NoSuchAlgorithmException e) {
139         }
140         for (int i = 0; i < invalidValues.length; i++) {
141             try {
142                 KeyAgreement.getInstance(invalidValues[i]);
143                 fail("NoSuchAlgorithmException must be thrown");
144             } catch (NoSuchAlgorithmException e) {
145             }
146         }
147     }
148
149     /**
150      * Test for <code> getInstance(String algorithm) </code> method Assertions:
151      * returns KeyAgreement object
152      */
153     @TestTargetNew(
154         level = TestLevel.PARTIAL_COMPLETE,
155         notes = "This is a complete subset of tests for getInstance method.",
156         method = "getInstance",
157         args = {java.lang.String.class}
158     )
159     public void testGetInstanceString02() throws NoSuchAlgorithmException {
160         if (!DEFSupported) {
161             fail(NotSupportMsg);
162             return;
163         }
164         KeyAgreement keyA;
165         for (int i = 0; i < validValues.length; i++) {
166             keyA = KeyAgreement.getInstance(validValues[i]);
167             assertEquals("Incorrect algorithm", keyA.getAlgorithm(),
168                     validValues[i]);
169         }
170     }
171
172     /**
173      * Test for <code> getInstance(String algorithm, String provider)</code>
174      * method Assertions: throws NullPointerException when algorithm is null
175      * throws NoSuchAlgorithmException when algorithm is not available
176      */
177     @TestTargetNew(
178         level = TestLevel.PARTIAL_COMPLETE,
179         notes = "This is a complete subset of tests for getInstance method.",
180         method = "getInstance",
181         args = {java.lang.String.class, java.lang.String.class}
182     )
183     public void testGetInstanceStringString01()
184             throws NoSuchAlgorithmException, IllegalArgumentException,
185             NoSuchProviderException {
186         if (!DEFSupported) {
187             fail(NotSupportMsg);
188             return;
189         }
190         try {
191             KeyAgreement.getInstance(null, defaultProviderName);
192             fail("NullPointerException or NoSuchAlgorithmException should be thrown if algorithm is null");
193         } catch (NullPointerException e) {
194         } catch (NoSuchAlgorithmException e) {
195         }
196         for (int i = 0; i < invalidValues.length; i++) {
197             try {
198                 KeyAgreement.getInstance(invalidValues[i], defaultProviderName);
199                 fail("NoSuchAlgorithmException must be thrown");
200             } catch (NoSuchAlgorithmException e) {
201             }
202         }
203     }
204
205     /**
206      * Test for <code> getInstance(String algorithm, String provider)</code>
207      * method Assertions: throws IllegalArgumentException when provider is null
208      * or empty throws NoSuchProviderException when provider has not be
209      * configured
210      */
211     @TestTargetNew(
212         level = TestLevel.PARTIAL_COMPLETE,
213         notes = "This is a complete subset of tests for getInstance method.",
214         method = "getInstance",
215         args = {java.lang.String.class, java.lang.String.class}
216     )
217     public void testGetInstanceStringString02()
218             throws IllegalArgumentException, NoSuchAlgorithmException,
219             NoSuchProviderException {
220         if (!DEFSupported) {
221             fail(NotSupportMsg);
222             return;
223         }
224         String provider = null;
225         for (int i = 0; i < validValues.length; i++) {
226             try {
227                 KeyAgreement.getInstance(validValues[i], provider);
228                 fail("IllegalArgumentException must be thrown when provider is null");
229             } catch (IllegalArgumentException e) {
230             }
231             try {
232                 KeyAgreement.getInstance(validValues[i], "");
233                 fail("IllegalArgumentException must be thrown when provider is empty");
234             } catch (IllegalArgumentException e) {
235             }
236             for (int j = 1; j < invalidValues.length; j++) {
237                 try {
238                     KeyAgreement.getInstance(validValues[i], invalidValues[j]);
239                     fail("NoSuchProviderException must be thrown (algorithm: "
240                             .concat(validValues[i]).concat(" provider: ")
241                             .concat(invalidValues[j]).concat(")"));
242                 } catch (NoSuchProviderException e) {
243                 }
244             }
245         }
246     }
247
248     /**
249      * Test for <code> getInstance(String algorithm, String provider)</code>
250      * method Assertions: returns KeyAgreement object
251      */
252     @TestTargetNew(
253         level = TestLevel.PARTIAL_COMPLETE,
254         notes = "This is a complete subset of tests for getInstance method.",
255         method = "getInstance",
256         args = {java.lang.String.class, java.lang.String.class}
257     )
258     public void testGetInstanceStringString03()
259             throws IllegalArgumentException, NoSuchAlgorithmException,
260             NoSuchProviderException {
261         if (!DEFSupported) {
262             fail(NotSupportMsg);
263             return;
264         }
265         KeyAgreement keyA;
266         for (int i = 0; i < validValues.length; i++) {
267             keyA = KeyAgreement
268                     .getInstance(validValues[i], defaultProviderName);
269             assertEquals("Incorrect algorithm", keyA.getAlgorithm(),
270                     validValues[i]);
271             assertEquals("Incorrect provider", keyA.getProvider().getName(),
272                     defaultProviderName);
273         }
274     }
275
276     /**
277      * Test for <code> getInstance(String algorithm, Provider provider)</code>
278      * method Assertions: throws NullPointerException when algorithm is null
279      * throws NoSuchAlgorithmException when algorithm isnot available
280      */
281     @TestTargetNew(
282         level = TestLevel.PARTIAL_COMPLETE,
283         notes = "This is a complete subset of tests for getInstance method.",
284         method = "getInstance",
285         args = {java.lang.String.class, java.security.Provider.class}
286     )
287     public void testGetInstanceStringProvider01()
288             throws NoSuchAlgorithmException, IllegalArgumentException {
289         if (!DEFSupported) {
290             fail(NotSupportMsg);
291             return;
292         }
293         try {
294             KeyAgreement.getInstance(null, defaultProvider);
295             fail("NullPointerException or NoSuchAlgorithmException should be thrown if algorithm is null");
296         } catch (NullPointerException e) {
297         } catch (NoSuchAlgorithmException e) {
298         }
299         for (int i = 0; i < invalidValues.length; i++) {
300             try {
301                 KeyAgreement.getInstance(invalidValues[i], defaultProvider);
302                 fail("NoSuchAlgorithmException must be thrown");
303             } catch (NoSuchAlgorithmException e) {
304             }
305         }
306     }
307
308     /**
309      * Test for <code> getInstance(String algorithm, Provider provider)</code>
310      * method Assertions: throws IllegalArgumentException when provider is null
311      */
312     @TestTargetNew(
313         level = TestLevel.PARTIAL_COMPLETE,
314         notes = "This is a complete subset of tests for getInstance method.",
315         method = "getInstance",
316         args = {java.lang.String.class, java.security.Provider.class}
317     )
318     public void testGetInstanceStringProvider02()
319             throws NoSuchAlgorithmException, IllegalArgumentException {
320         if (!DEFSupported) {
321             fail(NotSupportMsg);
322             return;
323         }
324         Provider provider = null;
325         for (int i = 0; i < invalidValues.length; i++) {
326             try {
327                 KeyAgreement.getInstance(invalidValues[i], provider);
328                 fail("IllegalArgumentException must be thrown");
329             } catch (IllegalArgumentException e) {
330             }
331         }
332     }
333
334     /**
335      * Test for <code> getInstance(String algorithm, Provider provider)</code>
336      * method Assertions: returns KeyAgreement object
337      */
338     @TestTargetNew(
339         level = TestLevel.PARTIAL_COMPLETE,
340         notes = "This is a complete subset of tests for getInstance method.",
341         method = "getInstance",
342         args = {java.lang.String.class, java.security.Provider.class}
343     )
344     public void testGetInstanceStringProvider03()
345             throws IllegalArgumentException, NoSuchAlgorithmException {
346         if (!DEFSupported) {
347             fail(NotSupportMsg);
348             return;
349         }
350         KeyAgreement keyA;
351         for (int i = 0; i < validValues.length; i++) {
352             keyA = KeyAgreement.getInstance(validValues[i], defaultProvider);
353             assertEquals("Incorrect algorithm", keyA.getAlgorithm(),
354                     validValues[i]);
355             assertEquals("Incorrect provider", keyA.getProvider(),
356                     defaultProvider);
357         }
358     }
359
360     /**
361      * Test for the methods: <code>init(Key key)</code>
362      * <code>generateSecret()</code>
363      * <code>generateSecret(byte[] sharedsecret, int offset)</code>
364      * <code>generateSecret(String algorithm)</code>
365      * Assertions: initializes KeyAgreement; returns sharedSecret; puts
366      * sharedsecret in buffer and return numbers of bytes; returns SecretKey
367      * object
368      */
369     @TestTargets({
370         @TestTargetNew(
371             level = TestLevel.PARTIAL_COMPLETE,
372             notes = "Checks functionality only.",
373             method = "init",
374             args = {java.security.Key.class}
375         ),
376         @TestTargetNew(
377             level = TestLevel.PARTIAL_COMPLETE,
378             notes = "Checks functionality only.",
379             method = "generateSecret",
380             args = {}
381         ),
382         @TestTargetNew(
383             level = TestLevel.PARTIAL_COMPLETE,
384             notes = "Checks functionality only.",
385             method = "generateSecret",
386             args = {byte[].class, int.class}
387         ),
388         @TestTargetNew(
389             level = TestLevel.PARTIAL_COMPLETE,
390             notes = "Checks functionality only.",
391             method = "generateSecret",
392             args = {java.lang.String.class}
393         ),
394         @TestTargetNew(
395             level = TestLevel.PARTIAL_COMPLETE,
396             notes = "Checks functionality only.",
397             clazz = KeyAgreementSpi.class,
398             method = "engineGenerateSecret",
399             args = {}
400         ),
401         @TestTargetNew(
402             level = TestLevel.PARTIAL_COMPLETE,
403             notes = "Checks functionality only.",
404             clazz = KeyAgreementSpi.class,
405             method = "engineGenerateSecret",
406             args = {byte[].class, int.class}
407         ),
408         @TestTargetNew(
409             level = TestLevel.PARTIAL_COMPLETE,
410             notes = "Checks functionality only.",
411             clazz = KeyAgreementSpi.class,
412             method = "engineGenerateSecret",
413             args = {java.lang.String.class}
414         )
415     })
416     public void testGenerateSecret03() throws Exception {
417         if (!DEFSupported) {
418             fail(NotSupportMsg);
419             return;
420         }
421         createKeys();
422         KeyAgreement[] kAgs = createKAs();
423
424         byte[] bb;
425         byte[] bb1 = new byte[10];
426         for (int i = 0; i < kAgs.length; i++) {
427             kAgs[i].init(privKey);
428             kAgs[i].doPhase(publKey, true);
429             bb = kAgs[i].generateSecret();
430             kAgs[i].init(privKey);
431             kAgs[i].doPhase(publKey, true);
432             bb1 = new byte[bb.length + 10];
433             kAgs[i].generateSecret(bb1, 9);
434             kAgs[i].init(privKey);
435             kAgs[i].doPhase(publKey, true);
436             kAgs[i].generateSecret("DES");
437         }
438     }
439
440     /**
441      * Test for <code>doPhase(Key key, boolean lastPhase)</code> method
442      * Assertion: throws InvalidKeyException if key is not appropriate
443      */
444     @TestTargets({
445         @TestTargetNew(
446             level = TestLevel.COMPLETE,
447             notes = "",
448             method = "doPhase",
449             args = {java.security.Key.class, boolean.class}
450         ),
451         @TestTargetNew(
452             level = TestLevel.COMPLETE,
453             notes = "",
454             clazz = KeyAgreementSpi.class,
455             method = "engineDoPhase",
456             args = {java.security.Key.class, boolean.class}
457         )
458     })
459     public void testDoPhase() throws Exception {
460         if (!DEFSupported) {
461             fail(NotSupportMsg);
462             return;
463         }
464         createKeys();
465         KeyAgreement[] kAgs = createKAs();
466         DHParameterSpec dhPs = ((DHPrivateKey) privKey).getParams();
467         SecureRandom randomNull = null;
468         SecureRandom random = new SecureRandom();
469
470         for (int i = 0; i < kAgs.length; i++) {
471             try {
472                 kAgs[i].doPhase(publKey, true);
473                 fail("IllegalStateException expected");
474             } catch (IllegalStateException e) {
475                 //expected
476             }
477
478             kAgs[i].init(privKey);
479
480             try {
481                 kAgs[i].doPhase(privKey, false);
482                 fail("InvalidKeyException must be throw");
483             } catch (InvalidKeyException e) {
484             }
485
486             try {
487                 kAgs[i].doPhase(privKey, true);
488                 fail("InvalidKeyException must be throw");
489             } catch (InvalidKeyException e) {
490             }
491
492             kAgs[i].init(privKey, dhPs);
493             kAgs[i].doPhase(publKey, true);
494             kAgs[i].init(privKey, dhPs, random);
495             kAgs[i].doPhase(publKey, true);
496         }
497     }
498
499     /**
500      * Test for the methods <code>init(Key key)</code>
501      * <code>init(Key key, SecureRandom random)</code>
502      * <code>init(Key key, AlgorithmParameterSpec params)</code>
503      * <code>init(Key key, AlgorithmParameterSpec params, SecureRandom random)</code>
504      * Assertion: throws InvalidKeyException when key is inappropriate
505      */
506     @TestTargets({
507         @TestTargetNew(
508             level = TestLevel.PARTIAL_COMPLETE,
509             notes = "Checks InvalidKeyException.",
510             method = "init",
511             args = {java.security.Key.class}
512         ),
513         @TestTargetNew(
514             level = TestLevel.PARTIAL_COMPLETE,
515             notes = "Checks InvalidKeyException.",
516             method = "init",
517             args = {java.security.Key.class, java.security.spec.AlgorithmParameterSpec.class}
518         ),
519         @TestTargetNew(
520             level = TestLevel.PARTIAL_COMPLETE,
521             notes = "Checks InvalidKeyException.",
522             clazz = KeyAgreementSpi.class,
523             method = "engineInit",
524             args = {java.security.Key.class, java.security.spec.AlgorithmParameterSpec.class, java.security.SecureRandom.class}
525         ),
526         @TestTargetNew(
527             level = TestLevel.PARTIAL_COMPLETE,
528             notes = "Checks InvalidKeyException.",
529             clazz = KeyAgreementSpi.class,
530             method = "engineInit",
531             args = {java.security.Key.class, java.security.SecureRandom.class}
532         )
533     })
534     public void testInit01() throws Exception {
535         if (!DEFSupported) {
536             fail(NotSupportMsg);
537             return;
538         }
539         createKeys();
540         KeyAgreement[] kAgs = createKAs();
541
542         SecureRandom random = null;
543         AlgorithmParameterSpec aps = null;
544         DHParameterSpec dhPs = new DHParameterSpec(new BigInteger("56"),
545                 new BigInteger("56"));
546         for (int i = 0; i < kAgs.length; i++) {
547             try {
548                 kAgs[i].init(publKey);
549                 fail("InvalidKeyException must be throw");
550             } catch (InvalidKeyException e) {
551             }
552             try {
553                 kAgs[i].init(publKey, new SecureRandom());
554                 fail("InvalidKeyException must be throw");
555             } catch (InvalidKeyException e) {
556             }
557             try {
558                 kAgs[i].init(publKey, random);
559                 fail("InvalidKeyException must be throw");
560             } catch (InvalidKeyException e) {
561             }
562             try {
563                 kAgs[i].init(publKey, dhPs);
564                 fail("InvalidKeyException must be throw");
565             } catch (InvalidKeyException e) {
566             }
567             try {
568                 kAgs[i].init(publKey, aps);
569                 fail("InvalidKeyException must be throw");
570             } catch (InvalidKeyException e) {
571             }
572             try {
573                 kAgs[i].init(publKey, dhPs, new SecureRandom());
574                 fail("InvalidKeyException must be throw");
575             } catch (InvalidKeyException e) {
576             }
577         }
578     }
579
580     /**
581      * Test for the methods
582      * <code>init(Key key, AlgorithmParameterSpec params)</code>
583      * <code>init(Key key, AlgorithmParameterSpec params, SecureRandom random)</code>
584      * Assertion: throws AlgorithmParameterException when params are
585      * inappropriate
586      */
587     @TestTargets({
588         @TestTargetNew(
589             level = TestLevel.PARTIAL_COMPLETE,
590             notes = "Checks InvalidAlgorithmParameterException.This is a complete subset of tests for exceptions checking for init methods group",
591             method = "init",
592             args = {java.security.Key.class, java.security.spec.AlgorithmParameterSpec.class}
593         ),
594         @TestTargetNew(
595             level = TestLevel.PARTIAL_COMPLETE,
596             notes = "Checks InvalidAlgorithmParameterException.This is a complete subset of tests for exceptions checking for init methods group",
597             clazz = KeyAgreementSpi.class,
598             method = "engineInit",
599             args = {java.security.Key.class, java.security.spec.AlgorithmParameterSpec.class, java.security.SecureRandom.class}
600         )
601     })
602     public void testInit02() throws Exception {
603         if (!DEFSupported) {
604             fail(NotSupportMsg);
605             return;
606         }
607         createKeys();
608         KeyAgreement[] kAgs = createKAs();
609
610         SecureRandom random = null;
611         DSAParameterSpec dsa = new DSAParameterSpec(new BigInteger("56"),
612                 new BigInteger("56"), new BigInteger("56"));
613         for (int i = 0; i < kAgs.length; i++) {
614             try {
615                 kAgs[i].init(privKey, dsa);
616                 fail("InvalidAlgorithmParameterException or InvalidKeyException must be throw");
617             } catch (InvalidAlgorithmParameterException e) {
618             } catch (InvalidKeyException e) {
619             }
620             try {
621                 kAgs[i].init(privKey, dsa, new SecureRandom());
622                 fail("InvalidAlgorithmParameterException or InvalidKeyException must be throw");
623             } catch (InvalidAlgorithmParameterException e) {
624             } catch (InvalidKeyException e) {
625             }
626             try {
627                 kAgs[i].init(privKey, dsa, random);
628                 fail("InvalidAlgorithmParameterException or InvalidKeyException must be throw");
629             } catch (InvalidAlgorithmParameterException e) {
630             } catch (InvalidKeyException e) {
631             }
632         }
633     }
634
635     /**
636      * Test for the methods: <code>init(Key key)</code>
637      * <code>init(Key key, SecureRandom random)</code>
638      * <code>generateSecret()</code>
639      * Assertions: initializes KeyAgreement and returns byte array
640      */
641     @TestTargets({
642         @TestTargetNew(
643             level = TestLevel.PARTIAL_COMPLETE,
644             notes = "Checks functionality.",
645             method = "init",
646             args = {java.security.Key.class}
647         ),
648         @TestTargetNew(
649             level = TestLevel.PARTIAL_COMPLETE,
650             notes = "Checks functionality.",
651             method = "init",
652             args = {java.security.Key.class, java.security.SecureRandom.class}
653         ),
654         @TestTargetNew(
655             level = TestLevel.PARTIAL_COMPLETE,
656             notes = "Checks functionality.",
657             method = "generateSecret",
658             args = {}
659         ),
660         @TestTargetNew(
661             level = TestLevel.PARTIAL_COMPLETE,
662             notes = "Checks functionality.",
663             clazz = KeyAgreementSpi.class,
664             method = "engineInit",
665             args = {java.security.Key.class, java.security.SecureRandom.class}
666         ),
667         @TestTargetNew(
668             level = TestLevel.PARTIAL_COMPLETE,
669             notes = "Checks functionality.",
670             clazz = KeyAgreementSpi.class,
671             method = "engineInit",
672             args = {java.security.Key.class, java.security.spec.AlgorithmParameterSpec.class, java.security.SecureRandom.class}
673         ),
674         @TestTargetNew(
675             level = TestLevel.PARTIAL_COMPLETE,
676             notes = "Checks functionality.",
677             clazz = KeyAgreementSpi.class,
678             method = "engineGenerateSecret",
679             args = {}
680         )
681     })
682     public void testInit03() throws Exception {
683         if (!DEFSupported) {
684             fail(NotSupportMsg);
685             return;
686         }
687         createKeys();
688         KeyAgreement[] kAgs = createKAs();
689
690         byte[] bbRes1;
691         byte[] bbRes2;
692         byte[] bbRes3;
693         SecureRandom randomNull = null;
694         SecureRandom random = new SecureRandom();
695         for (int i = 0; i < kAgs.length; i++) {
696             kAgs[i].init(privKey);
697             kAgs[i].doPhase(publKey, true);
698             bbRes1 = kAgs[i].generateSecret();
699             kAgs[i].init(privKey, random);
700             kAgs[i].doPhase(publKey, true);
701             bbRes2 = kAgs[i].generateSecret();
702             assertEquals("Incorrect byte array length", bbRes1.length,
703                     bbRes2.length);
704             for (int j = 0; j < bbRes1.length; j++) {
705                 assertEquals("Incorrect byte (index: ".concat(
706                         Integer.toString(i)).concat(")"), bbRes1[j], bbRes2[j]);
707             }
708             kAgs[i].init(privKey, randomNull);
709             kAgs[i].doPhase(publKey, true);
710             bbRes3 = kAgs[i].generateSecret();
711             assertEquals("Incorrect byte array length", bbRes1.length,
712                     bbRes3.length);
713             for (int j = 0; j < bbRes1.length; j++) {
714                 assertEquals("Incorrect byte (index: ".concat(
715                         Integer.toString(i)).concat(")"), bbRes1[j], bbRes3[j]);
716             }
717         }
718     }
719
720     /**
721      * Test for the methods:
722      * <code>init(Key key, AlgorithmParameterSpec params)</code>
723      * <code>init(Key key, AlgorithmParameterSpec params, SecureRandom random)</code>
724      * <code>generateSecret()</code>
725      * Assertions: initializes KeyAgreement and returns byte array
726      */
727     @TestTargets({
728         @TestTargetNew(
729             level = TestLevel.PARTIAL,
730             notes = "Checks functionality.",
731             method = "init",
732             args = {java.security.Key.class, java.security.spec.AlgorithmParameterSpec.class}
733         ),
734         @TestTargetNew(
735             level = TestLevel.COMPLETE,
736             notes = "Checks functionality.",
737             method = "init",
738             args = {java.security.Key.class, java.security.spec.AlgorithmParameterSpec.class, java.security.SecureRandom.class}
739         ),
740         @TestTargetNew(
741             level = TestLevel.PARTIAL,
742             notes = "Checks functionality.",
743             method = "generateSecret",
744             args = {}
745         ),
746         @TestTargetNew(
747             level = TestLevel.PARTIAL,
748             notes = "Checks functionality.",
749             clazz = KeyAgreementSpi.class,
750             method = "engineInit",
751             args = {java.security.Key.class, SecureRandom.class}
752         ),
753         @TestTargetNew(
754             level = TestLevel.PARTIAL,
755             notes = "Checks functionality.",
756             clazz = KeyAgreementSpi.class,
757             method = "engineInit",
758             args = {java.security.Key.class, java.security.spec.AlgorithmParameterSpec.class, java.security.SecureRandom.class}
759         ),
760         @TestTargetNew(
761             level = TestLevel.PARTIAL,
762             notes = "Checks functionality.",
763             clazz = KeyAgreementSpi.class,
764             method = "engineGenerateSecret",
765             args = {}
766         )
767     })
768     public void testInit04() throws Exception,
769             InvalidAlgorithmParameterException {
770         if (!DEFSupported) {
771             fail(NotSupportMsg);
772             return;
773         }
774         createKeys();
775         KeyAgreement[] kAgs = createKAs();
776
777         DHParameterSpec dhPs = ((DHPrivateKey) privKey).getParams();
778         AlgorithmParameterSpec aps = new RSAKeyGenParameterSpec(10, new BigInteger("10"));
779
780         byte[] bbRes1;
781         byte[] bbRes2;
782         byte[] bbRes3;
783         SecureRandom randomNull = null;
784         SecureRandom random = new SecureRandom();
785         for (int i = 0; i < kAgs.length; i++) {
786             kAgs[i].init(privKey, dhPs);
787             kAgs[i].doPhase(publKey, true);
788             bbRes1 = kAgs[i].generateSecret();
789             kAgs[i].init(privKey, dhPs, random);
790             kAgs[i].doPhase(publKey, true);
791             bbRes2 = kAgs[i].generateSecret();
792             assertEquals("Incorrect byte array length", bbRes1.length,
793                     bbRes2.length);
794             for (int j = 0; j < bbRes1.length; j++) {
795                 assertEquals("Incorrect byte (index: ".concat(
796                         Integer.toString(i)).concat(")"), bbRes1[j], bbRes2[j]);
797             }
798             kAgs[i].init(privKey, dhPs, randomNull);
799             kAgs[i].doPhase(publKey, true);
800             bbRes3 = kAgs[i].generateSecret();
801             assertEquals("Incorrect byte array length", bbRes1.length,
802                     bbRes3.length);
803             for (int j = 0; j < bbRes1.length; j++) {
804                 assertEquals("Incorrect byte (index: ".concat(
805                         Integer.toString(i)).concat(")"), bbRes1[j], bbRes3[j]);
806             }
807
808             try {
809                 kAgs[i].init(publKey, dhPs, random);
810                 fail("InvalidKeyException expected");
811             } catch (InvalidKeyException e) {
812                 //expected
813             }
814             try {
815                 kAgs[i].init(privKey, aps, random);
816                 fail("InvalidAlgorithmParameterException expected");
817             } catch (InvalidAlgorithmParameterException e) {
818                 //expected
819             }
820         }
821     }
822
823     class Mock_KeyAgreement extends KeyAgreement {
824         protected Mock_KeyAgreement(KeyAgreementSpi arg0, Provider arg1, String arg2) {
825             super(arg0, arg1, arg2);
826         }
827     }
828
829     @TestTargetNew(
830         level = TestLevel.COMPLETE,
831         notes = "",
832         method = "KeyAgreement",
833         args = {javax.crypto.KeyAgreementSpi.class, java.security.Provider.class, java.lang.String.class}
834     )
835     public void test_constructor() {
836         assertNotNull(new Mock_KeyAgreement(null, null, null));
837     }
838
839     @TestTargetNew(
840         level = TestLevel.COMPLETE,
841         notes = "",
842         method = "getAlgorithm",
843         args = {}
844     )
845     public void test_getAlgorithm() throws NoSuchAlgorithmException {
846         Mock_KeyAgreement mka = new Mock_KeyAgreement(null, null, null);
847         assertNull(mka.getAlgorithm());
848
849         KeyAgreement keyA;
850         for (int i = 0; i < validValues.length; i++) {
851             keyA = KeyAgreement.getInstance(validValues[i]);
852             assertEquals("Incorrect algorithm", keyA.getAlgorithm(),
853                     validValues[i]);
854         }
855     }
856
857     @TestTargetNew(
858         level = TestLevel.COMPLETE,
859         notes = "",
860         method = "getProvider",
861         args = {}
862     )
863     public void test_getProvider() throws NoSuchAlgorithmException {
864         KeyAgreement keyA;
865         for (int i = 0; i < validValues.length; i++) {
866             keyA = KeyAgreement.getInstance(validValues[i]);
867             assertNotNull(keyA.getProvider());
868         }
869     }
870
871 @TestTargets({
872     @TestTargetNew(
873         level = TestLevel.PARTIAL_COMPLETE,
874         notes = "",
875         method = "generateSecret",
876         args = {byte[].class, int.class}
877     ),
878     @TestTargetNew(
879         level = TestLevel.PARTIAL_COMPLETE,
880         notes = "",
881         clazz = KeyAgreementSpi.class,
882         method = "engineGenerateSecret",
883         args = {byte[].class, int.class}
884     )})
885     public void test_generateSecret$BI() throws Exception {
886         if (!DEFSupported) {
887             fail(NotSupportMsg);
888             return;
889         }
890         createKeys();
891         KeyAgreement[] kAgs = createKAs();
892         KeyAgreement ka = KeyAgreement.getInstance("DH");
893
894         byte[] bb1 = new byte[1];
895         try {
896             ka.generateSecret(bb1, 0);
897             fail("IllegalStateException expected");
898         } catch (IllegalStateException e) {
899             //expected
900         }
901         ka.init(privKey);
902         ka.doPhase(publKey, true);
903         try {
904             ka.generateSecret(bb1, 0);
905             fail("ShortBufferException expected");
906         } catch (ShortBufferException e) {
907             //expected
908         }
909     }
910
911 @TestTargets({
912     @TestTargetNew(
913             level = TestLevel.PARTIAL_COMPLETE,
914             notes = "",
915             method = "generateSecret",
916             args = {java.lang.String.class}
917         ),
918     @TestTargetNew(
919         level = TestLevel.PARTIAL_COMPLETE,
920         notes = "",
921         clazz = KeyAgreementSpi.class,
922         method = "engineGenerateSecret",
923         args = {java.lang.String.class}
924     )})
925     public void test_generateSecretLjava_lang_String() throws Exception {
926         if (!DEFSupported) {
927             fail(NotSupportMsg);
928             return;
929         }
930         createKeys();
931         KeyAgreement[] kAgs = createKAs();
932         KeyAgreement ka = KeyAgreement.getInstance("DH");
933
934         byte[] bb1 = new byte[1];
935         try {
936             ka.generateSecret("dh");
937             fail("IllegalStateException expected");
938         } catch (IllegalStateException e) {
939             //expected
940         }
941         ka.init(privKey);
942         ka.doPhase(publKey, true);
943         try {
944             ka.generateSecret("Wrong alg name");
945             fail("NoSuchAlgorithmException expected");
946         } catch (NoSuchAlgorithmException e) {
947             //expected
948         }
949     }
950
951     @TestTargetNew(
952             level = TestLevel.COMPLETE,
953             notes = "",
954             method = "init",
955             args = {java.security.Key.class, java.security.SecureRandom.class}
956         )
957     public void test_initLjava_security_KeyLjava_security_SecureRandom() throws Exception {
958         if (!DEFSupported) {
959             fail(NotSupportMsg);
960             return;
961         }
962         createKeys();
963         KeyAgreement[] kAgs = createKAs();
964         KeyAgreement ka = KeyAgreement.getInstance("DH");
965
966         ka.init(privKey, new SecureRandom());
967         try {
968             ka.init(publKey, new SecureRandom());
969             fail("InvalidKeyException expected");
970         } catch (InvalidKeyException e) {
971             //expected
972         }
973     }
974 }