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 */ 21 22 package org.apache.harmony.crypto.tests.javax.crypto; 23 24 import java.security.InvalidKeyException; 25 import java.security.NoSuchAlgorithmException; 26 import java.security.Provider; 27 28 import javax.crypto.KeyAgreement; 29 import javax.crypto.KeyAgreementSpi; 30 31 import org.apache.harmony.crypto.tests.support.MyKeyAgreementSpi; 32 import org.apache.harmony.security.tests.support.SpiEngUtils; 33 34 import junit.framework.TestCase; 35 36 /** 37 * Tests for KeyAgreement constructor and methods 38 * 39 */ 40 41 public class KeyAgreement_Impl1Test extends TestCase { 42 43 public static final String srvKeyAgreement = "KeyAgreement"; 44 45 private static String defaultAlgorithm = "DH"; 46 47 48 private static Provider defaultProvider = null; 49 50 private static boolean DEFSupported = false; 51 52 private static final String NotSupportMsg = "There is no suitable provider for KeyAgreement"; 53 54 55 static { 56 defaultProvider = SpiEngUtils.isSupport(defaultAlgorithm, 57 srvKeyAgreement); 58 DEFSupported = (defaultProvider != null); 59 } 60 61 /** 62 * Test for <code>KeyAgreement</code> constructor Assertion: returns 63 * KeyAgreement object 64 */ 65 public void testKeyAgreement() throws NoSuchAlgorithmException, 66 InvalidKeyException, IllegalStateException { 67 if (!DEFSupported) { 68 fail(NotSupportMsg); 69 return; 70 } 71 KeyAgreementSpi spi = new MyKeyAgreementSpi(); 72 KeyAgreement keyA = new myKeyAgreement(spi, defaultProvider, 73 defaultAlgorithm); 74 assertEquals("Incorrect algorithm", keyA.getAlgorithm(), 75 defaultAlgorithm); 76 assertEquals("Incorrect provider", keyA.getProvider(), defaultProvider); 77 assertNull("Incorrect result", keyA.doPhase(null, true)); 78 assertEquals("Incorrect result", keyA.generateSecret().length, 0); 79 80 keyA = new myKeyAgreement(null, null, null); 81 assertNull("Algorithm must be null", keyA.getAlgorithm()); 82 assertNull("Provider must be null", keyA.getProvider()); 83 try { 84 keyA.doPhase(null, true); 85 fail("NullPointerException must be thrown"); 86 } catch (NullPointerException e) { 87 } 88 } 89 } 90 91 /** 92 * Additional class for KeyAgreement constructor verification 93 */ 94 95 class myKeyAgreement extends KeyAgreement { 96 97 public myKeyAgreement(KeyAgreementSpi keyAgreeSpi, Provider provider, 98 String algorithm) { 99 super(keyAgreeSpi, provider, algorithm); 100 } 101 } 102