summaryrefslogtreecommitdiff
path: root/crypto/src/test/api/java/org/apache/harmony/crypto/tests/javax/crypto/spec/RC5ParameterSpecTest.java
blob: c5c63a62922cf2527576659e67c2c18940ff16ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * @author Alexander Y. Kleymenov
 */

package org.apache.harmony.crypto.tests.javax.crypto.spec;

import java.util.Arrays;

import javax.crypto.spec.RC5ParameterSpec;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 */

public class RC5ParameterSpecTest extends TestCase {

    /**
     * RC5ParameterSpec(int version, int rounds, int wordSize, byte[] iv) method
     * testing. Tests that IllegalArgumentException is thrown in the case of
     * inappropriate constructor parameters and that input iv array is
     * copied to protect against subsequent modification.
     */
    public void testRC5ParameterSpec1() {
        int version = 1;
        int rounds = 5;
        int wordSize = 16;
        byte[] iv = { 1, 2, 3, 4 };

        try {
            new RC5ParameterSpec(version, rounds, wordSize, null);
            fail("An IllegalArgumentException should be thrown "
                    + "in the case of null iv.");
        } catch (IllegalArgumentException e) {
        }

        try {
            new RC5ParameterSpec(version, rounds, wordSize + 8, iv);
            fail("An IllegalArgumentException should be thrown "
                    + "in the case of short iv.");
        } catch (IllegalArgumentException e) {
        }

        try {
            new RC5ParameterSpec(version, rounds, wordSize, new byte[] { 1, 2, 3 });
            fail("An IllegalArgumentException should be thrown "
                    + "in the case of short iv.");
        } catch (IllegalArgumentException e) {
        }

        RC5ParameterSpec ps = new RC5ParameterSpec(version, rounds,
                wordSize, iv);
        iv[0]++;
        assertFalse("The change of iv specified in the constructor "
                + "should not cause the change of internal array.",
                iv[0] == ps.getIV()[0]);
    }

    /**
     * RC5ParameterSpec(int version, int rounds, int wordSize, byte[] iv, int
     * offset) method testing. Tests that IllegalArgumentException is thrown in
     * the case of inappropriate constructor parameters and that input iv array
     * is copied to protect against subsequent modification.
     */
    public void testRC5ParameterSpec2() {
        int version = 1;
        int rounds = 5;
        int wordSize = 16;
        byte[] iv = { 1, 2, 3, 4, 5, 6 };
        int offset = 2;

        try {
            new RC5ParameterSpec(version, rounds, wordSize, null, offset);
            fail("An IllegalArgumentException should be thrown "
                    + "in the case of null iv.");
        } catch (IllegalArgumentException e) {
        }

        try {
            new RC5ParameterSpec(version, rounds, wordSize + 8, iv, offset);
            fail("An IllegalArgumentException should be thrown "
                    + "in the case of short iv.");
        } catch (IllegalArgumentException e) {
        }

        try {
            new RC5ParameterSpec(version, rounds, wordSize, iv, offset + 1);
            fail("An IllegalArgumentException should be thrown "
                    + "in the case of short iv.");
        } catch (IllegalArgumentException e) {
        }

        try {
            new RC5ParameterSpec(version, rounds, wordSize, new byte[] { 1, 2,
                    3, 4 }, offset);
            fail("An IllegalArgumentException should be thrown "
                    + "in the case of short iv.");
        } catch (IllegalArgumentException e) {
        }

        RC5ParameterSpec ps = new RC5ParameterSpec(version, rounds, wordSize,
                iv, offset);
        iv[offset]++;
        assertFalse("The change of iv specified in the constructor "
                + "should not cause the change of internal array.",
                iv[offset] == ps.getIV()[0]);

        // Regression test for HARMONY-1077
        try {
            new RC5ParameterSpec(0, 9, 77, new byte[] { 2 }, -100);
            fail("ArrayIndexOutOfBoundsException expected");
        } catch (ArrayIndexOutOfBoundsException e) {
            // expected
        }
    }

    /**
     * getVersion() method testing. Tests that returned value is
     * equal to the value specified in the constructor.
     */
    public void testGetVersion() {
        int version = 1;
        int rounds = 5;
        int wordSize = 16;

        RC5ParameterSpec ps = new RC5ParameterSpec(version, rounds, wordSize);
        assertTrue("The returned version value should be equal to the "
                + "value specified in the constructor.",
                ps.getVersion() == version);
    }

    /**
     * getRounds() method testing. Tests that returned value is
     * equal to the value specified in the constructor.
     */
    public void testGetRounds() {
        int version = 1;
        int rounds = 5;
        int wordSize = 16;

        RC5ParameterSpec ps = new RC5ParameterSpec(version, rounds, wordSize);
        assertTrue("The returned rounds value should be equal to the "
                + "value specified in the constructor.",
                ps.getRounds() == rounds);
    }

    /**
     * getWordSize() method testing. Tests that returned value is
     * equal to the value specified in the constructor.
     */
    public void testGetWordSize() {
        int version = 1;
        int rounds = 5;
        int wordSize = 16;

        RC5ParameterSpec ps = new RC5ParameterSpec(version, rounds, wordSize);
        assertTrue("The returned wordSize value should be equal to the "
                + "value specified in the constructor.",
                ps.getWordSize() == wordSize);
    }

    /**
     * getIV() method testing. Tests that returned array is equal to the
     * array specified in the constructor. Checks that modification
     * of returned array does not affect the internal array. Also it checks
     * that getIV() method returns null if iv is not specified.
     */
    public void testGetIV() {
        int version = 1;
        int rounds = 5;
        int wordSize = 16;
        byte[] iv = { 1, 2, 3, 4 };

        RC5ParameterSpec ps = new RC5ParameterSpec(version, rounds,
                wordSize, iv);
        byte[] result = ps.getIV();
        if (!Arrays.equals(iv, result)) {
            fail("The returned iv is not equal to the specified "
                    + "in the constructor.");
        }
        result[0]++;
        assertFalse("The change of returned by getIV() method iv "
                + "should not cause the change of internal array.",
                result[0] == ps.getIV()[0]);
        ps = new RC5ParameterSpec(version, rounds, wordSize);
        assertNull("The getIV() method should return null if the parameter "
                + "set does not contain IV.", ps.getIV());
    }

    /**
     * equals(Object obj) method testing. Tests the correctness of equal
     * operation: it should be reflexive, symmetric, transitive, consistent
     * and should be false on null object.
     */
    public void testEquals() {
        int version = 1;
        int rounds = 5;
        int wordSize = 16;
        byte[] iv = { 1, 2, 3, 4, 5, 6 };

        RC5ParameterSpec ps1 = new RC5ParameterSpec(version, rounds,
                wordSize, iv);
        RC5ParameterSpec ps2 = new RC5ParameterSpec(version, rounds,
                wordSize, iv);
        RC5ParameterSpec ps3 = new RC5ParameterSpec(version, rounds, wordSize,
                new byte[] { 1, 2, 3, 4 });
        // checking for reflexive law:
        assertTrue("The equivalence relation should be reflexive.",
                ps1.equals(ps1));

        assertTrue("Objects built on the same parameters should be equal.",
                ps1.equals(ps2));
        // checking for symmetric law:
        assertTrue("The equivalence relation should be symmetric.",
                ps2.equals(ps1));

        assertTrue("Objects built on the equal parameters should be equal.",
                ps2.equals(ps3));

        // checking for transitive law:
        assertTrue("The equivalence relation should be transitive.",
                ps1.equals(ps3));

        assertFalse("Should return not be equal to null object.",
                ps1.equals(null));

        ps2 = new RC5ParameterSpec(version + 1, rounds, wordSize, iv);
        assertFalse("Objects should not be equal.", ps1.equals(ps2));

        ps2 = new RC5ParameterSpec(version, rounds + 1, wordSize, iv);
        assertFalse("Objects should not be equal.", ps1.equals(ps2));

        ps2 = new RC5ParameterSpec(version, rounds, wordSize / 2, iv);
        assertFalse("Objects should not be equal.", ps1.equals(ps2));

        ps2 = new RC5ParameterSpec(version, rounds, wordSize,
                new byte[] { 4, 3, 2, 1 });
        assertFalse("Objects should not be equal.", ps1.equals(ps2));
    }

    /**
     * hashCode() method testing. Tests that for equal objects hash codes
     * are equal.
     */
    public void testHashCode() {
        int version = 1;
        int rounds = 5;
        int wordSize = 16;
        byte[] iv = { 1, 2, 3, 4, 5, 6 };

        RC5ParameterSpec ps1 = new RC5ParameterSpec(version, rounds,
                wordSize, iv);
        RC5ParameterSpec ps2 = new RC5ParameterSpec(version, rounds,
                wordSize, iv);
        assertTrue("Equal objects should have the same hash codes.",
                ps1.hashCode() == ps2.hashCode());
    }

    public static Test suite() {
        return new TestSuite(RC5ParameterSpecTest.class);
    }

}