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 java.lang.IllegalArgumentException;
26import java.util.Arrays;
27
28import javax.crypto.spec.RC2ParameterSpec;
29
30import junit.framework.Test;
31import junit.framework.TestCase;
32import junit.framework.TestSuite;
33
34/**
35 */
36public class RC2ParameterSpecTest extends TestCase {
37
38    /**
39     * RC2ParameterSpec(int effectiveKeyBits, byte[] iv) method testing.
40     * Tests that IllegalArgumentException is thrown in the case of
41     * inappropriate constructor parameters and that input iv array is
42     * copied to protect against subsequent modification.
43     */
44    public void testRC2ParameterSpec1() {
45        int effectiveKeyBits = 10;
46        byte[] iv = {1, 2, 3, 4, 5, 6, 7, 8};
47
48        try {
49            new RC2ParameterSpec(effectiveKeyBits, null);
50            fail("An IllegalArgumentException should be thrown "
51                    + "in the case of null iv.");
52        } catch (IllegalArgumentException e) {
53        }
54
55        try {
56            new RC2ParameterSpec(effectiveKeyBits, new byte[] {1, 2, 3, 4, 5});
57            fail("An IllegalArgumentException should be thrown "
58                    + "in the case of short iv.");
59        } catch (IllegalArgumentException e) {
60        }
61
62        RC2ParameterSpec ps = new RC2ParameterSpec(effectiveKeyBits, iv);
63        iv[0] ++;
64        assertFalse("The change of iv specified in the constructor "
65                    + "should not cause the change of internal array.",
66                    iv[0] == ps.getIV()[0]);
67    }
68
69    /**
70     * RC2ParameterSpec(int effectiveKeyBits, byte[] iv, int offset) method
71     * testing. Tests that IllegalArgumentException is thrown in the case of
72     * inappropriate constructor parameters and that input iv array is
73     * copied to protect against subsequent modification.
74     */
75    public void testRC2ParameterSpec2() {
76        int effectiveKeyBits = 10;
77        byte[] iv = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
78        int offset = 2;
79
80        try {
81            new RC2ParameterSpec(effectiveKeyBits, null, offset);
82            fail("An IllegalArgumentException should be thrown "
83                    + "in the case of null iv.");
84        } catch (IllegalArgumentException e) {
85        }
86
87        try {
88            new RC2ParameterSpec(effectiveKeyBits, iv, 4);
89            fail("An IllegalArgumentException should be thrown "
90                    + "in the case of short iv.");
91        } catch (IllegalArgumentException e) {
92        }
93
94        RC2ParameterSpec ps = new RC2ParameterSpec(effectiveKeyBits, iv, offset);
95        iv[offset] ++;
96        assertFalse("The change of iv specified in the constructor "
97                    + "should not cause the change of internal array.",
98                    iv[offset] == ps.getIV()[0]);
99    }
100
101    /**
102     * getEffectiveKeyBits() method testing. Tests that returned value is
103     * equal to the value specified in the constructor.
104     */
105    public void testGetEffectiveKeyBits() {
106        int effectiveKeyBits = 10;
107        byte[] iv = {1, 2, 3, 4, 5, 6, 7, 8};
108
109        RC2ParameterSpec ps = new RC2ParameterSpec(effectiveKeyBits, iv);
110        assertTrue("The returned effectiveKeyBits value is not equal to the "
111                + "value specified in the constructor.",
112                effectiveKeyBits == ps.getEffectiveKeyBits());
113    }
114
115    /**
116     * getIV() method testing. Tests that returned array is equal to the
117     * array specified in the constructor. Checks that modification
118     * of returned array does not affect the internal array. Also it checks
119     * that getIV() method returns null if iv is not specified.
120     */
121    public void testGetIV() {
122        int effectiveKeyBits = 10;
123        byte[] iv = new byte[] {1, 2, 3, 4, 5, 6, 7, 8};
124
125        RC2ParameterSpec ps = new RC2ParameterSpec(effectiveKeyBits, iv);
126        byte[] result = ps.getIV();
127        if (! Arrays.equals(iv, result)) {
128            fail("The returned iv is not equal to the specified "
129                    + "in the constructor.");
130        }
131        result[0] ++;
132        assertFalse("The change of returned by getIV() method iv "
133                    + "should not cause the change of internal array.",
134                    result[0] == ps.getIV()[0]);
135        ps = new RC2ParameterSpec(effectiveKeyBits);
136        assertNull("The getIV() method should return null if the parameter "
137                    + "set does not contain iv.", ps.getIV());
138    }
139
140    /**
141     * equals(Object obj) method testing. Tests the correctness of equal
142     * operation: it should be reflexive, symmetric, transitive, consistent
143     * and should be false on null object.
144     */
145    public void testEquals() {
146        int effectiveKeyBits = 10;
147        byte[] iv = new byte[] {1, 2, 3, 4, 5, 6, 7, 8};
148
149        RC2ParameterSpec ps1 = new RC2ParameterSpec(effectiveKeyBits, iv);
150        RC2ParameterSpec ps2 = new RC2ParameterSpec(effectiveKeyBits, iv);
151        RC2ParameterSpec ps3 = new RC2ParameterSpec(10,
152                                    new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9});
153
154        // checking for reflexive law:
155        assertTrue("The equivalence relation should be reflexive.",
156                                                        ps1.equals(ps1));
157
158        assertTrue("Objects built on the same parameters should be equal.",
159                                                        ps1.equals(ps2));
160        // checking for symmetric law:
161        assertTrue("The equivalence relation should be symmetric.",
162                                                        ps2.equals(ps1));
163
164        assertTrue("Objects built on the equal parameters should be equal.",
165                                                        ps2.equals(ps3));
166
167        // checking for transitive law:
168        assertTrue("The equivalence relation should be transitive.",
169                                                        ps1.equals(ps3));
170
171        assertFalse("Should return not be equal to null object.",
172                                                        ps1.equals(null));
173
174        ps2 = new RC2ParameterSpec(11, iv);
175        assertFalse("Objects should not be equal.", ps1.equals(ps2));
176
177        ps2 = new RC2ParameterSpec(11, new byte[] {9, 8, 7, 6, 5, 4, 3, 2, 1});
178        assertFalse("Objects should not be equal.", ps1.equals(ps2));
179    }
180
181    /**
182     * hashCode() method testing. Tests that for equal objects hash codes
183     * are equal.
184     */
185    public void testHashCode() {
186        int effectiveKeyBits = 0;
187        byte[] iv = new byte[] {1, 2, 3, 4, 5, 6, 7, 8};
188
189        RC2ParameterSpec ps1 = new RC2ParameterSpec(effectiveKeyBits, iv);
190        RC2ParameterSpec ps2 = new RC2ParameterSpec(effectiveKeyBits, iv);
191
192        assertTrue("Equal objects should have the same hash codes.",
193                                            ps1.hashCode() == ps2.hashCode());
194    }
195
196    public void test_constructorI() {
197        int effectiveKeyBits = 0;
198
199        RC2ParameterSpec ps1 = new RC2ParameterSpec(effectiveKeyBits);
200        RC2ParameterSpec ps2 = new RC2ParameterSpec(effectiveKeyBits);
201
202        assertTrue(ps1.equals(ps2));
203    }
204
205    public static Test suite() {
206        return new TestSuite(RC2ParameterSpecTest.class);
207    }
208}
209
210