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 23package org.apache.harmony.crypto.tests.javax.crypto; 24 25import java.security.InvalidAlgorithmParameterException; 26import java.security.InvalidKeyException; 27import java.security.Key; 28import java.security.NoSuchAlgorithmException; 29import java.security.SecureRandom; 30import java.security.spec.AlgorithmParameterSpec; 31 32import javax.crypto.KeyAgreementSpi; 33import javax.crypto.SecretKey; 34import javax.crypto.ShortBufferException; 35 36import junit.framework.TestCase; 37 38import org.apache.harmony.crypto.tests.support.MyKeyAgreementSpi; 39 40/** 41 * Tests for <code>KeyAgreementSpi</code> class constructors and methods. 42 * 43 */ 44public class KeyAgreementSpiTest extends TestCase { 45 class Mock_KeyAgreementSpi extends MyKeyAgreementSpi { 46 47 @Override 48 protected Key engineDoPhase(Key key, boolean lastPhase) throws InvalidKeyException, 49 IllegalStateException { 50 return super.engineDoPhase(key, lastPhase); 51 } 52 53 @Override 54 protected byte[] engineGenerateSecret() throws IllegalStateException { 55 return super.engineGenerateSecret(); 56 } 57 58 @Override 59 protected SecretKey engineGenerateSecret(String algorithm) throws IllegalStateException, 60 NoSuchAlgorithmException, InvalidKeyException { 61 return super.engineGenerateSecret(algorithm); 62 } 63 64 @Override 65 protected int engineGenerateSecret(byte[] sharedSecret, int offset) 66 throws IllegalStateException, ShortBufferException { 67 return super.engineGenerateSecret(sharedSecret, offset); 68 } 69 70 @Override 71 protected void engineInit(Key key, SecureRandom random) throws InvalidKeyException { 72 super.engineInit(key, random); 73 } 74 75 @Override 76 protected void engineInit(Key key, AlgorithmParameterSpec params, SecureRandom random) 77 throws InvalidKeyException, InvalidAlgorithmParameterException { 78 super.engineInit(key, params, random); 79 } 80 81 } 82 83 /** 84 * Test for <code>KeyAgreementSpi</code> constructor Assertion: constructs 85 * KeyAgreementSpi 86 */ 87 public void testKeyAgreementSpi01() throws InvalidKeyException, 88 ShortBufferException, NoSuchAlgorithmException, 89 InvalidAlgorithmParameterException { 90 Mock_KeyAgreementSpi kaSpi = new Mock_KeyAgreementSpi(); 91 92 assertNull("Not null result", kaSpi.engineDoPhase(null, true)); 93 try { 94 kaSpi.engineDoPhase(null, false); 95 fail("IllegalStateException must be thrown"); 96 } catch (IllegalStateException e) { 97 } 98 byte[] bb = kaSpi.engineGenerateSecret(); 99 assertEquals("Length is not 0", bb.length, 0); 100 assertEquals("Returned integer is not 0", kaSpi.engineGenerateSecret(new byte[1], 10), -1); 101 assertNull("Not null result", kaSpi.engineGenerateSecret("aaa")); 102 try { 103 kaSpi.engineGenerateSecret(""); 104 fail("NoSuchAlgorithmException must be thrown"); 105 } catch (NoSuchAlgorithmException e) { 106 } 107 Key key = null; 108 try { 109 kaSpi.engineInit(key, new SecureRandom()); 110 fail("IllegalArgumentException must be thrown"); 111 } catch (IllegalArgumentException e) { 112 } 113 AlgorithmParameterSpec params = null; 114 try { 115 kaSpi.engineInit(key, params, new SecureRandom()); 116 fail("IllegalArgumentException must be thrown"); 117 } catch (IllegalArgumentException e) { 118 } 119 } 120} 121