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 Alexander Y. Kleymenov 20* @version $Revision$ 21*/ 22 23package org.apache.harmony.crypto.tests.javax.crypto.spec; 24 25import dalvik.annotation.TestTargetClass; 26import dalvik.annotation.TestTargets; 27import dalvik.annotation.TestLevel; 28import dalvik.annotation.TestTargetNew; 29 30import java.lang.NullPointerException; 31import java.lang.IllegalArgumentException; 32import java.lang.ArrayIndexOutOfBoundsException; 33 34import javax.crypto.spec.IvParameterSpec; 35 36import junit.framework.Test; 37import junit.framework.TestCase; 38import junit.framework.TestSuite; 39 40@TestTargetClass(IvParameterSpec.class) 41/** 42 */ 43 44public class IvParameterSpecTest extends TestCase { 45 46 /** 47 * IvParameterSpec(byte[] iv) constructor testing. Checks that 48 * NullPointerException is thrown in the case of null input 49 * array and that input array is copied during initialization. 50 */ 51 @TestTargetNew( 52 level = TestLevel.COMPLETE, 53 notes = "", 54 method = "IvParameterSpec", 55 args = {byte[].class} 56 ) 57 public void testIvParameterSpec1() { 58 try { 59 new IvParameterSpec(null); 60 fail("Should raise an NullPointerException " 61 + "in the case of null byte array."); 62 } catch(NullPointerException e) { 63 } 64 65 byte[] iv = new byte[] {1, 2, 3, 4, 5}; 66 IvParameterSpec ivps = new IvParameterSpec(iv); 67 iv[0] ++; 68 assertFalse("The change of input array's content should not cause " 69 + "the change of internal array", iv[0] == ivps.getIV()[0]); 70 } 71 72 /** 73 * IvParameterSpec(byte[] iv) constructor testing. Checks that 74 * NullPointerException is thrown in the case of null input 75 * array and that input array is copied during initialization. 76 */ 77 @TestTargetNew( 78 level = TestLevel.COMPLETE, 79 notes = "", 80 method = "IvParameterSpec", 81 args = {byte[].class, int.class, int.class} 82 ) 83 public void testIvParameterSpec2() { 84 try { 85 new IvParameterSpec(null, 1, 1); 86 fail("Should raise an IllegalArgumentException " 87 + "in the case of null byte array."); 88 } catch(ArrayIndexOutOfBoundsException e) { 89 fail("Unexpected ArrayIndexOutOfBoundsException was thrown"); 90 } catch(IllegalArgumentException e) { 91 } catch(NullPointerException e) { 92 fail("Unexpected NullPointerException was thrown"); 93 } 94 95 try { 96 new IvParameterSpec(new byte[] {1, 2, 3}, 2, 2); 97 fail("Should raise an IllegalArgumentException " 98 + "if (iv.length - offset < len)."); 99 } catch(ArrayIndexOutOfBoundsException e) { 100 fail("Unexpected ArrayIndexOutOfBoundsException was thrown"); 101 } catch(IllegalArgumentException e) { 102 } catch(NullPointerException e) { 103 fail("Unexpected NullPointerException was thrown"); 104 } 105 106 try { 107 new IvParameterSpec(new byte[] {1, 2, 3}, -1, 1); 108 fail("Should raise an ArrayIndexOutOfBoundsException " 109 + "if offset index bytes outside the iv."); 110 } catch(ArrayIndexOutOfBoundsException e) { 111 } catch(IllegalArgumentException e) { 112 fail("Unexpected IllegalArgumentException was thrown"); 113 } catch(NullPointerException e) { 114 fail("Unexpected NullPointerException was thrown"); 115 } 116 117 /* TODO: DRL fail with java.lang.NegativeArraySizeException 118 try { 119 new IvParameterSpec(new byte[] {1, 2, 3}, 1, -2); 120 fail("Should raise an ArrayIndexOutOfBoundsException " 121 + "if len index bytes outside the iv."); 122 } catch(ArrayIndexOutOfBoundsException e) { 123 } catch(IllegalArgumentException e) { 124 fail("Unexpected IllegalArgumentException was thrown"); 125 } catch(NullPointerException e) { 126 fail("Unexpected NullPointerException was thrown"); 127 } 128 */ 129 130 byte[] iv = new byte[] {1, 2, 3, 4, 5}; 131 IvParameterSpec ivps = new IvParameterSpec(iv, 0, iv.length); 132 iv[0] ++; 133 assertFalse("The change of input array's content should not cause " 134 + "the change of internal array", iv[0] == ivps.getIV()[0]); 135 136 //Regression for HARMONY-1089 137 try { 138 new IvParameterSpec(new byte[2], 2, -1); 139 fail("ArrayIndexOutOfBoundsException expected"); 140 } catch (ArrayIndexOutOfBoundsException e) { 141 //expected 142 } 143 } 144 145 @TestTargetNew( 146 level = TestLevel.COMPLETE, 147 notes = "", 148 method = "getIV", 149 args = {} 150 ) 151 public void testGetIV() { 152 byte[] iv = new byte[] {1, 2, 3, 4, 5}; 153 IvParameterSpec ivps = new IvParameterSpec(iv); 154 iv = ivps.getIV(); 155 iv[0] ++; 156 assertFalse("The change of returned array should not cause " 157 + "the change of internal array", iv[0] == ivps.getIV()[0]); 158 } 159 160 public static Test suite() { 161 return new TestSuite(IvParameterSpecTest.class); 162 } 163 164 public static void main(String[] args) { 165 junit.textui.TestRunner.run(suite()); 166 } 167} 168 169