1/*
2 * Copyright 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.apache.harmony.crypto.tests.javax.crypto.spec;
18
19import java.util.Arrays;
20
21import javax.crypto.spec.GCMParameterSpec;
22
23import junit.framework.TestCase;
24
25public class GCMParameterSpecTest extends TestCase {
26    private static final byte[] TEST_IV = new byte[8];
27
28    public void testConstructor_IntByteArray_Success() throws Exception {
29        new GCMParameterSpec(8, TEST_IV);
30    }
31
32    public void testConstructor_IntByteArray_NegativeTLen_Failure() throws Exception {
33        try {
34            new GCMParameterSpec(-1, TEST_IV);
35            fail("Should throw IllegalArgumentException");
36        } catch (IllegalArgumentException expected) {
37        }
38    }
39
40    public void testConstructor_IntByteArray_NullIv_Failure() throws Exception {
41        try {
42            new GCMParameterSpec(8, null);
43            fail("Should throw IllegalArgumentException");
44        } catch (IllegalArgumentException expected) {
45        }
46    }
47
48    public void testConstructor_IntByteArrayWithOffsets_Success() throws Exception {
49        new GCMParameterSpec(8, TEST_IV, 0, TEST_IV.length);
50    }
51
52    public void testConstructor_IntByteArrayWithOffsets_NullIv_Failure() throws Exception {
53        try {
54            new GCMParameterSpec(8, null, 0, TEST_IV.length);
55            fail("Should throw IllegalArgumentException");
56        } catch (IllegalArgumentException expected) {
57        }
58    }
59
60    public void testConstructor_IntByteArrayWithOffsets_NegativeOffset_Failure() throws Exception {
61        try {
62            new GCMParameterSpec(8, TEST_IV, -1, TEST_IV.length);
63            fail("Should throw IllegalArgumentException");
64        } catch (IllegalArgumentException expected) {
65        }
66    }
67
68    public void testConstructor_IntByteArrayWithOffsets_TooLongLength_Failure() throws Exception {
69        try {
70            new GCMParameterSpec(8, TEST_IV, 0, TEST_IV.length + 1);
71            fail("Should throw IllegalArgumentException");
72        } catch (IllegalArgumentException expected) {
73        }
74    }
75
76    public void testGetIV_Success() throws Exception {
77        GCMParameterSpec spec = new GCMParameterSpec(8, TEST_IV);
78
79        byte[] actual = spec.getIV();
80        assertEquals(Arrays.toString(TEST_IV), Arrays.toString(actual));
81
82        // XOR with 0xFF so we're sure we changed the array
83        for (int i = 0; i < actual.length; i++) {
84            actual[i] ^= 0xFF;
85        }
86
87        assertFalse("Changing the IV returned shouldn't change the parameter spec",
88                Arrays.equals(spec.getIV(), actual));
89        assertEquals(Arrays.toString(TEST_IV), Arrays.toString(spec.getIV()));
90    }
91
92    public void testGetIV_Subarray_Success() throws Exception {
93        GCMParameterSpec spec = new GCMParameterSpec(8, TEST_IV, 2, 4);
94        assertEquals(Arrays.toString(Arrays.copyOfRange(TEST_IV, 2, 6)),
95                Arrays.toString(spec.getIV()));
96    }
97
98    public void testGetTLen_Success() throws Exception {
99        GCMParameterSpec spec = new GCMParameterSpec(8, TEST_IV);
100        assertEquals(8, spec.getTLen());
101    }
102}
103