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