KeyAgreementSpiTest.java revision f33eae7e84eb6d3b0f4e86b59605bb3de73009f3
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 40import dalvik.annotation.TestLevel; 41import dalvik.annotation.TestTargetClass; 42import dalvik.annotation.TestTargetNew; 43 44@TestTargetClass(KeyAgreementSpi.class) 45/** 46 * Tests for <code>KeyAgreementSpi</code> class constructors and methods. 47 * 48 */ 49 50public class KeyAgreementSpiTest extends TestCase { 51 class Mock_KeyAgreementSpi extends MyKeyAgreementSpi { 52 53 @Override 54 protected Key engineDoPhase(Key key, boolean lastPhase) throws InvalidKeyException, 55 IllegalStateException { 56 return super.engineDoPhase(key, lastPhase); 57 } 58 59 @Override 60 protected byte[] engineGenerateSecret() throws IllegalStateException { 61 return super.engineGenerateSecret(); 62 } 63 64 @Override 65 protected SecretKey engineGenerateSecret(String algorithm) throws IllegalStateException, 66 NoSuchAlgorithmException, InvalidKeyException { 67 return super.engineGenerateSecret(algorithm); 68 } 69 70 @Override 71 protected int engineGenerateSecret(byte[] sharedSecret, int offset) 72 throws IllegalStateException, ShortBufferException { 73 return super.engineGenerateSecret(sharedSecret, offset); 74 } 75 76 @Override 77 protected void engineInit(Key key, SecureRandom random) throws InvalidKeyException { 78 super.engineInit(key, random); 79 } 80 81 @Override 82 protected void engineInit(Key key, AlgorithmParameterSpec params, SecureRandom random) 83 throws InvalidKeyException, InvalidAlgorithmParameterException { 84 super.engineInit(key, params, random); 85 } 86 87 } 88 89 /** 90 * Test for <code>KeyAgreementSpi</code> constructor Assertion: constructs 91 * KeyAgreementSpi 92 */ 93 @TestTargetNew( 94 level = TestLevel.COMPLETE, 95 notes = "", 96 method = "KeyAgreementSpi", 97 args = {} 98 ) 99 public void testKeyAgreementSpi01() throws InvalidKeyException, 100 ShortBufferException, NoSuchAlgorithmException, 101 InvalidAlgorithmParameterException { 102 Mock_KeyAgreementSpi kaSpi = new Mock_KeyAgreementSpi(); 103 104 assertNull("Not null result", kaSpi.engineDoPhase(null, true)); 105 try { 106 kaSpi.engineDoPhase(null, false); 107 fail("IllegalStateException must be thrown"); 108 } catch (IllegalStateException e) { 109 } 110 byte[] bb = kaSpi.engineGenerateSecret(); 111 assertEquals("Length is not 0", bb.length, 0); 112 assertEquals("Returned integer is not 0", kaSpi.engineGenerateSecret(new byte[1], 10), -1); 113 assertNull("Not null result", kaSpi.engineGenerateSecret("aaa")); 114 try { 115 kaSpi.engineGenerateSecret(""); 116 fail("NoSuchAlgorithmException must be thrown"); 117 } catch (NoSuchAlgorithmException e) { 118 } 119 Key key = null; 120 try { 121 kaSpi.engineInit(key, new SecureRandom()); 122 fail("IllegalArgumentException must be thrown"); 123 } catch (IllegalArgumentException e) { 124 } 125 AlgorithmParameterSpec params = null; 126 try { 127 kaSpi.engineInit(key, params, new SecureRandom()); 128 fail("IllegalArgumentException must be thrown"); 129 } catch (IllegalArgumentException e) { 130 } 131 } 132} 133