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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @author Vera Y. Petrashkova
23 package org.apache.harmony.crypto.tests.javax.crypto;
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;
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;
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;
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;
54 import junit.framework.TestCase;
57 @TestTargetClass(KeyAgreement.class)
59 * Tests for KeyAgreement constructor and methods
63 public class KeyAgreementTest extends TestCase {
65 public static final String srvKeyAgreement = "KeyAgreement";
67 private static String defaultAlgorithm = "DH";
69 private static String defaultProviderName = null;
71 private static Provider defaultProvider = null;
73 private static boolean DEFSupported = false;
75 private static final String NotSupportMsg = "There is no suitable provider for KeyAgreement";
77 private static final String[] invalidValues = SpiEngUtils.invalidValues;
79 private static String[] validValues = { "DH", "dH",
82 private static PrivateKey privKey = null;
84 private static PublicKey publKey = null;
86 private static boolean initKeys = false;
89 defaultProvider = SpiEngUtils.isSupport(defaultAlgorithm,
91 DEFSupported = (defaultProvider != null);
92 defaultProviderName = (DEFSupported ? defaultProvider.getName() : null);
95 private void createKeys() throws Exception {
97 TestKeyPair tkp = new TestKeyPair(defaultAlgorithm);
98 privKey = tkp.getPrivate();
99 publKey = tkp.getPublic();
105 private KeyAgreement[] createKAs() throws Exception {
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);
118 public static String getDefAlg() {
119 return defaultAlgorithm;
123 * Test for <code> getInstance(String algorithm) </code> method Assertions:
124 * throws NullPointerException when algorithm is null throws
125 * NoSuchAlgorithmException when algorithm isnot available
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}
133 public void testGetInstanceString01() throws NoSuchAlgorithmException {
135 KeyAgreement.getInstance(null);
136 fail("NullPointerException or NoSuchAlgorithmException should be thrown if algorithm is null");
137 } catch (NullPointerException e) {
138 } catch (NoSuchAlgorithmException e) {
140 for (int i = 0; i < invalidValues.length; i++) {
142 KeyAgreement.getInstance(invalidValues[i]);
143 fail("NoSuchAlgorithmException must be thrown");
144 } catch (NoSuchAlgorithmException e) {
150 * Test for <code> getInstance(String algorithm) </code> method Assertions:
151 * returns KeyAgreement object
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}
159 public void testGetInstanceString02() throws NoSuchAlgorithmException {
165 for (int i = 0; i < validValues.length; i++) {
166 keyA = KeyAgreement.getInstance(validValues[i]);
167 assertEquals("Incorrect algorithm", keyA.getAlgorithm(),
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
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}
183 public void testGetInstanceStringString01()
184 throws NoSuchAlgorithmException, IllegalArgumentException,
185 NoSuchProviderException {
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) {
196 for (int i = 0; i < invalidValues.length; i++) {
198 KeyAgreement.getInstance(invalidValues[i], defaultProviderName);
199 fail("NoSuchAlgorithmException must be thrown");
200 } catch (NoSuchAlgorithmException e) {
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
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}
217 public void testGetInstanceStringString02()
218 throws IllegalArgumentException, NoSuchAlgorithmException,
219 NoSuchProviderException {
224 String provider = null;
225 for (int i = 0; i < validValues.length; i++) {
227 KeyAgreement.getInstance(validValues[i], provider);
228 fail("IllegalArgumentException must be thrown when provider is null");
229 } catch (IllegalArgumentException e) {
232 KeyAgreement.getInstance(validValues[i], "");
233 fail("IllegalArgumentException must be thrown when provider is empty");
234 } catch (IllegalArgumentException e) {
236 for (int j = 1; j < invalidValues.length; j++) {
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) {
249 * Test for <code> getInstance(String algorithm, String provider)</code>
250 * method Assertions: returns KeyAgreement object
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}
258 public void testGetInstanceStringString03()
259 throws IllegalArgumentException, NoSuchAlgorithmException,
260 NoSuchProviderException {
266 for (int i = 0; i < validValues.length; i++) {
268 .getInstance(validValues[i], defaultProviderName);
269 assertEquals("Incorrect algorithm", keyA.getAlgorithm(),
271 assertEquals("Incorrect provider", keyA.getProvider().getName(),
272 defaultProviderName);
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
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}
287 public void testGetInstanceStringProvider01()
288 throws NoSuchAlgorithmException, IllegalArgumentException {
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) {
299 for (int i = 0; i < invalidValues.length; i++) {
301 KeyAgreement.getInstance(invalidValues[i], defaultProvider);
302 fail("NoSuchAlgorithmException must be thrown");
303 } catch (NoSuchAlgorithmException e) {
309 * Test for <code> getInstance(String algorithm, Provider provider)</code>
310 * method Assertions: throws IllegalArgumentException when provider is null
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}
318 public void testGetInstanceStringProvider02()
319 throws NoSuchAlgorithmException, IllegalArgumentException {
324 Provider provider = null;
325 for (int i = 0; i < invalidValues.length; i++) {
327 KeyAgreement.getInstance(invalidValues[i], provider);
328 fail("IllegalArgumentException must be thrown");
329 } catch (IllegalArgumentException e) {
335 * Test for <code> getInstance(String algorithm, Provider provider)</code>
336 * method Assertions: returns KeyAgreement object
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}
344 public void testGetInstanceStringProvider03()
345 throws IllegalArgumentException, NoSuchAlgorithmException {
351 for (int i = 0; i < validValues.length; i++) {
352 keyA = KeyAgreement.getInstance(validValues[i], defaultProvider);
353 assertEquals("Incorrect algorithm", keyA.getAlgorithm(),
355 assertEquals("Incorrect provider", keyA.getProvider(),
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
371 level = TestLevel.PARTIAL_COMPLETE,
372 notes = "Checks functionality only.",
374 args = {java.security.Key.class}
377 level = TestLevel.PARTIAL_COMPLETE,
378 notes = "Checks functionality only.",
379 method = "generateSecret",
383 level = TestLevel.PARTIAL_COMPLETE,
384 notes = "Checks functionality only.",
385 method = "generateSecret",
386 args = {byte[].class, int.class}
389 level = TestLevel.PARTIAL_COMPLETE,
390 notes = "Checks functionality only.",
391 method = "generateSecret",
392 args = {java.lang.String.class}
395 level = TestLevel.PARTIAL_COMPLETE,
396 notes = "Checks functionality only.",
397 clazz = KeyAgreementSpi.class,
398 method = "engineGenerateSecret",
402 level = TestLevel.PARTIAL_COMPLETE,
403 notes = "Checks functionality only.",
404 clazz = KeyAgreementSpi.class,
405 method = "engineGenerateSecret",
406 args = {byte[].class, int.class}
409 level = TestLevel.PARTIAL_COMPLETE,
410 notes = "Checks functionality only.",
411 clazz = KeyAgreementSpi.class,
412 method = "engineGenerateSecret",
413 args = {java.lang.String.class}
416 public void testGenerateSecret03() throws Exception {
422 KeyAgreement[] kAgs = createKAs();
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");
441 * Test for <code>doPhase(Key key, boolean lastPhase)</code> method
442 * Assertion: throws InvalidKeyException if key is not appropriate
446 level = TestLevel.COMPLETE,
449 args = {java.security.Key.class, boolean.class}
452 level = TestLevel.COMPLETE,
454 clazz = KeyAgreementSpi.class,
455 method = "engineDoPhase",
456 args = {java.security.Key.class, boolean.class}
459 public void testDoPhase() throws Exception {
465 KeyAgreement[] kAgs = createKAs();
466 DHParameterSpec dhPs = ((DHPrivateKey) privKey).getParams();
467 SecureRandom randomNull = null;
468 SecureRandom random = new SecureRandom();
470 for (int i = 0; i < kAgs.length; i++) {
472 kAgs[i].doPhase(publKey, true);
473 fail("IllegalStateException expected");
474 } catch (IllegalStateException e) {
478 kAgs[i].init(privKey);
481 kAgs[i].doPhase(privKey, false);
482 fail("InvalidKeyException must be throw");
483 } catch (InvalidKeyException e) {
487 kAgs[i].doPhase(privKey, true);
488 fail("InvalidKeyException must be throw");
489 } catch (InvalidKeyException e) {
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);
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
508 level = TestLevel.PARTIAL_COMPLETE,
509 notes = "Checks InvalidKeyException.",
511 args = {java.security.Key.class}
514 level = TestLevel.PARTIAL_COMPLETE,
515 notes = "Checks InvalidKeyException.",
517 args = {java.security.Key.class, java.security.spec.AlgorithmParameterSpec.class}
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}
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}
534 public void testInit01() throws Exception {
540 KeyAgreement[] kAgs = createKAs();
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++) {
548 kAgs[i].init(publKey);
549 fail("InvalidKeyException must be throw");
550 } catch (InvalidKeyException e) {
553 kAgs[i].init(publKey, new SecureRandom());
554 fail("InvalidKeyException must be throw");
555 } catch (InvalidKeyException e) {
558 kAgs[i].init(publKey, random);
559 fail("InvalidKeyException must be throw");
560 } catch (InvalidKeyException e) {
563 kAgs[i].init(publKey, dhPs);
564 fail("InvalidKeyException must be throw");
565 } catch (InvalidKeyException e) {
568 kAgs[i].init(publKey, aps);
569 fail("InvalidKeyException must be throw");
570 } catch (InvalidKeyException e) {
573 kAgs[i].init(publKey, dhPs, new SecureRandom());
574 fail("InvalidKeyException must be throw");
575 } catch (InvalidKeyException e) {
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
589 level = TestLevel.PARTIAL_COMPLETE,
590 notes = "Checks InvalidAlgorithmParameterException.This is a complete subset of tests for exceptions checking for init methods group",
592 args = {java.security.Key.class, java.security.spec.AlgorithmParameterSpec.class}
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}
602 public void testInit02() throws Exception {
608 KeyAgreement[] kAgs = createKAs();
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++) {
615 kAgs[i].init(privKey, dsa);
616 fail("InvalidAlgorithmParameterException or InvalidKeyException must be throw");
617 } catch (InvalidAlgorithmParameterException e) {
618 } catch (InvalidKeyException e) {
621 kAgs[i].init(privKey, dsa, new SecureRandom());
622 fail("InvalidAlgorithmParameterException or InvalidKeyException must be throw");
623 } catch (InvalidAlgorithmParameterException e) {
624 } catch (InvalidKeyException e) {
627 kAgs[i].init(privKey, dsa, random);
628 fail("InvalidAlgorithmParameterException or InvalidKeyException must be throw");
629 } catch (InvalidAlgorithmParameterException e) {
630 } catch (InvalidKeyException e) {
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
643 level = TestLevel.PARTIAL_COMPLETE,
644 notes = "Checks functionality.",
646 args = {java.security.Key.class}
649 level = TestLevel.PARTIAL_COMPLETE,
650 notes = "Checks functionality.",
652 args = {java.security.Key.class, java.security.SecureRandom.class}
655 level = TestLevel.PARTIAL_COMPLETE,
656 notes = "Checks functionality.",
657 method = "generateSecret",
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}
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}
675 level = TestLevel.PARTIAL_COMPLETE,
676 notes = "Checks functionality.",
677 clazz = KeyAgreementSpi.class,
678 method = "engineGenerateSecret",
682 public void testInit03() throws Exception {
688 KeyAgreement[] kAgs = createKAs();
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,
704 for (int j = 0; j < bbRes1.length; j++) {
705 assertEquals("Incorrect byte (index: ".concat(
706 Integer.toString(i)).concat(")"), bbRes1[j], bbRes2[j]);
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,
713 for (int j = 0; j < bbRes1.length; j++) {
714 assertEquals("Incorrect byte (index: ".concat(
715 Integer.toString(i)).concat(")"), bbRes1[j], bbRes3[j]);
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
729 level = TestLevel.PARTIAL,
730 notes = "Checks functionality.",
732 args = {java.security.Key.class, java.security.spec.AlgorithmParameterSpec.class}
735 level = TestLevel.COMPLETE,
736 notes = "Checks functionality.",
738 args = {java.security.Key.class, java.security.spec.AlgorithmParameterSpec.class, java.security.SecureRandom.class}
741 level = TestLevel.PARTIAL,
742 notes = "Checks functionality.",
743 method = "generateSecret",
747 level = TestLevel.PARTIAL,
748 notes = "Checks functionality.",
749 clazz = KeyAgreementSpi.class,
750 method = "engineInit",
751 args = {java.security.Key.class, SecureRandom.class}
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}
761 level = TestLevel.PARTIAL,
762 notes = "Checks functionality.",
763 clazz = KeyAgreementSpi.class,
764 method = "engineGenerateSecret",
768 public void testInit04() throws Exception,
769 InvalidAlgorithmParameterException {
775 KeyAgreement[] kAgs = createKAs();
777 DHParameterSpec dhPs = ((DHPrivateKey) privKey).getParams();
778 AlgorithmParameterSpec aps = new RSAKeyGenParameterSpec(10, new BigInteger("10"));
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,
794 for (int j = 0; j < bbRes1.length; j++) {
795 assertEquals("Incorrect byte (index: ".concat(
796 Integer.toString(i)).concat(")"), bbRes1[j], bbRes2[j]);
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,
803 for (int j = 0; j < bbRes1.length; j++) {
804 assertEquals("Incorrect byte (index: ".concat(
805 Integer.toString(i)).concat(")"), bbRes1[j], bbRes3[j]);
809 kAgs[i].init(publKey, dhPs, random);
810 fail("InvalidKeyException expected");
811 } catch (InvalidKeyException e) {
815 kAgs[i].init(privKey, aps, random);
816 fail("InvalidAlgorithmParameterException expected");
817 } catch (InvalidAlgorithmParameterException e) {
823 class Mock_KeyAgreement extends KeyAgreement {
824 protected Mock_KeyAgreement(KeyAgreementSpi arg0, Provider arg1, String arg2) {
825 super(arg0, arg1, arg2);
830 level = TestLevel.COMPLETE,
832 method = "KeyAgreement",
833 args = {javax.crypto.KeyAgreementSpi.class, java.security.Provider.class, java.lang.String.class}
835 public void test_constructor() {
836 assertNotNull(new Mock_KeyAgreement(null, null, null));
840 level = TestLevel.COMPLETE,
842 method = "getAlgorithm",
845 public void test_getAlgorithm() throws NoSuchAlgorithmException {
846 Mock_KeyAgreement mka = new Mock_KeyAgreement(null, null, null);
847 assertNull(mka.getAlgorithm());
850 for (int i = 0; i < validValues.length; i++) {
851 keyA = KeyAgreement.getInstance(validValues[i]);
852 assertEquals("Incorrect algorithm", keyA.getAlgorithm(),
858 level = TestLevel.COMPLETE,
860 method = "getProvider",
863 public void test_getProvider() throws NoSuchAlgorithmException {
865 for (int i = 0; i < validValues.length; i++) {
866 keyA = KeyAgreement.getInstance(validValues[i]);
867 assertNotNull(keyA.getProvider());
873 level = TestLevel.PARTIAL_COMPLETE,
875 method = "generateSecret",
876 args = {byte[].class, int.class}
879 level = TestLevel.PARTIAL_COMPLETE,
881 clazz = KeyAgreementSpi.class,
882 method = "engineGenerateSecret",
883 args = {byte[].class, int.class}
885 public void test_generateSecret$BI() throws Exception {
891 KeyAgreement[] kAgs = createKAs();
892 KeyAgreement ka = KeyAgreement.getInstance("DH");
894 byte[] bb1 = new byte[1];
896 ka.generateSecret(bb1, 0);
897 fail("IllegalStateException expected");
898 } catch (IllegalStateException e) {
902 ka.doPhase(publKey, true);
904 ka.generateSecret(bb1, 0);
905 fail("ShortBufferException expected");
906 } catch (ShortBufferException e) {
913 level = TestLevel.PARTIAL_COMPLETE,
915 method = "generateSecret",
916 args = {java.lang.String.class}
919 level = TestLevel.PARTIAL_COMPLETE,
921 clazz = KeyAgreementSpi.class,
922 method = "engineGenerateSecret",
923 args = {java.lang.String.class}
925 public void test_generateSecretLjava_lang_String() throws Exception {
931 KeyAgreement[] kAgs = createKAs();
932 KeyAgreement ka = KeyAgreement.getInstance("DH");
934 byte[] bb1 = new byte[1];
936 ka.generateSecret("dh");
937 fail("IllegalStateException expected");
938 } catch (IllegalStateException e) {
942 ka.doPhase(publKey, true);
944 ka.generateSecret("Wrong alg name");
945 fail("NoSuchAlgorithmException expected");
946 } catch (NoSuchAlgorithmException e) {
952 level = TestLevel.COMPLETE,
955 args = {java.security.Key.class, java.security.SecureRandom.class}
957 public void test_initLjava_security_KeyLjava_security_SecureRandom() throws Exception {
963 KeyAgreement[] kAgs = createKAs();
964 KeyAgreement ka = KeyAgreement.getInstance("DH");
966 ka.init(privKey, new SecureRandom());
968 ka.init(publKey, new SecureRandom());
969 fail("InvalidKeyException expected");
970 } catch (InvalidKeyException e) {