IvParameterSpecTest.java revision 89c1feb0a69a7707b271086e749975b3f7acacf7
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.TestInfo; 27import dalvik.annotation.TestLevel; 28import dalvik.annotation.TestTarget; 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@TestInfo( 52 level = TestLevel.COMPLETE, 53 purpose = "", 54 targets = { 55 @TestTarget( 56 methodName = "IvParameterSpec", 57 methodArgs = {byte[].class} 58 ) 59 }) 60 public void testIvParameterSpec1() { 61 try { 62 new IvParameterSpec(null); 63 fail("Should raise an NullPointerException " 64 + "in the case of null byte array."); 65 } catch(NullPointerException e) { 66 } 67 68 byte[] iv = new byte[] {1, 2, 3, 4, 5}; 69 IvParameterSpec ivps = new IvParameterSpec(iv); 70 iv[0] ++; 71 assertFalse("The change of input array's content should not cause " 72 + "the change of internal array", iv[0] == ivps.getIV()[0]); 73 } 74 75 /** 76 * IvParameterSpec(byte[] iv) constructor testing. Checks that 77 * NullPointerException is thrown in the case of null input 78 * array and that input array is copied during initialization. 79 */ 80@TestInfo( 81 level = TestLevel.COMPLETE, 82 purpose = "", 83 targets = { 84 @TestTarget( 85 methodName = "IvParameterSpec", 86 methodArgs = {byte[].class, int.class, int.class} 87 ) 88 }) 89 public void testIvParameterSpec2() { 90 try { 91 new IvParameterSpec(null, 1, 1); 92 fail("Should raise an IllegalArgumentException " 93 + "in the case of null byte array."); 94 } catch(ArrayIndexOutOfBoundsException e) { 95 fail("Unexpected ArrayIndexOutOfBoundsException was thrown"); 96 } catch(IllegalArgumentException e) { 97 } catch(NullPointerException e) { 98 fail("Unexpected NullPointerException was thrown"); 99 } 100 101 try { 102 new IvParameterSpec(new byte[] {1, 2, 3}, 2, 2); 103 fail("Should raise an IllegalArgumentException " 104 + "if (iv.length - offset < len)."); 105 } catch(ArrayIndexOutOfBoundsException e) { 106 fail("Unexpected ArrayIndexOutOfBoundsException was thrown"); 107 } catch(IllegalArgumentException e) { 108 } catch(NullPointerException e) { 109 fail("Unexpected NullPointerException was thrown"); 110 } 111 112 try { 113 new IvParameterSpec(new byte[] {1, 2, 3}, -1, 1); 114 fail("Should raise an ArrayIndexOutOfBoundsException " 115 + "if offset index bytes outside the iv."); 116 } catch(ArrayIndexOutOfBoundsException e) { 117 } catch(IllegalArgumentException e) { 118 fail("Unexpected IllegalArgumentException was thrown"); 119 } catch(NullPointerException e) { 120 fail("Unexpected NullPointerException was thrown"); 121 } 122 123 /* TODO: DRL fail with java.lang.NegativeArraySizeException 124 try { 125 new IvParameterSpec(new byte[] {1, 2, 3}, 1, -2); 126 fail("Should raise an ArrayIndexOutOfBoundsException " 127 + "if len index bytes outside the iv."); 128 } catch(ArrayIndexOutOfBoundsException e) { 129 } catch(IllegalArgumentException e) { 130 fail("Unexpected IllegalArgumentException was thrown"); 131 } catch(NullPointerException e) { 132 fail("Unexpected NullPointerException was thrown"); 133 } 134 */ 135 136 byte[] iv = new byte[] {1, 2, 3, 4, 5}; 137 IvParameterSpec ivps = new IvParameterSpec(iv, 0, iv.length); 138 iv[0] ++; 139 assertFalse("The change of input array's content should not cause " 140 + "the change of internal array", iv[0] == ivps.getIV()[0]); 141 142 //Regression for HARMONY-1089 143 try { 144 new IvParameterSpec(new byte[2], 2, -1); 145 fail("ArrayIndexOutOfBoundsException expected"); 146 } catch (ArrayIndexOutOfBoundsException e) { 147 //expected 148 } 149 } 150 151@TestInfo( 152 level = TestLevel.COMPLETE, 153 purpose = "", 154 targets = { 155 @TestTarget( 156 methodName = "getIV", 157 methodArgs = {} 158 ) 159 }) 160 public void testGetIV() { 161 byte[] iv = new byte[] {1, 2, 3, 4, 5}; 162 IvParameterSpec ivps = new IvParameterSpec(iv); 163 iv = ivps.getIV(); 164 iv[0] ++; 165 assertFalse("The change of returned array should not cause " 166 + "the change of internal array", iv[0] == ivps.getIV()[0]); 167 } 168 169 public static Test suite() { 170 return new TestSuite(IvParameterSpecTest.class); 171 } 172 173 public static void main(String[] args) { 174 junit.textui.TestRunner.run(suite()); 175 } 176} 177 178