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 Boris V. Kuznetsov
20* @version $Revision$
21*/
22
23package org.apache.harmony.crypto.tests.javax.crypto;
24
25import java.security.SecureRandom;
26import java.util.Arrays;
27
28import javax.crypto.Cipher;
29import javax.crypto.CipherSpi;
30import javax.crypto.NullCipher;
31import javax.crypto.spec.SecretKeySpec;
32
33import junit.framework.TestCase;
34
35/**
36 *
37 * Tests for NullCipher
38 */
39public class NullCipherTest extends TestCase {
40
41    private Cipher c;
42
43    protected void setUp() throws Exception {
44        super.setUp();
45        c = new NullCipher();
46    }
47
48    public void testGetAlgorithm() {
49        c.getAlgorithm();
50    }
51
52    public void testGetBlockSize() {
53        assertEquals("Incorrect BlockSize", 1, c.getBlockSize());
54    }
55
56    public void testGetOutputSize() {
57        assertEquals("Incorrect OutputSize", 111, c.getOutputSize(111));
58    }
59
60    public void testGetIV() {
61        assertTrue("Incorrect IV", Arrays.equals(c.getIV(), new byte[8]));
62    }
63
64    public void testGetParameters() {
65        assertNull("Incorrect Parameters", c.getParameters());
66    }
67
68    public void testGetExemptionMechanism() {
69        assertNull("Incorrect ExemptionMechanism", c.getExemptionMechanism());
70    }
71
72    /*
73     * Class under test for void init(int, Key)
74     */
75    public void testInitintKey() throws Exception {
76        c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(new byte[1], "algorithm"));
77
78    }
79
80    /*
81     * Class under test for void init(int, Key, SecureRandom)
82     */
83    public void testInitintKeySecureRandom() throws Exception {
84        c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(new byte[1],
85                "algorithm"), new SecureRandom());
86    }
87
88    /*
89     * Class under test for void init(int, Key, AlgorithmParameterSpec)
90     */
91    public void testInitintKeyAlgorithmParameterSpec() throws Exception {
92        class myAlgorithmParameterSpec implements java.security.spec.AlgorithmParameterSpec {}
93        c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(new byte[1],
94                "algorithm"), new myAlgorithmParameterSpec());
95    }
96
97    /*
98     * Class under test for byte[] update(byte[])
99     */
100    public void testUpdatebyteArray() throws Exception {
101        byte [] b = {1, 2, 3, 4, 5};
102        byte [] r = c.update(b);
103        assertEquals("different length", b.length, r.length);
104        assertTrue("different content", Arrays.equals(b, r));
105    }
106
107    /*
108     * Class under test for byte[] update(byte[], int, int)
109     */
110    public void testUpdatebyteArrayintint() throws Exception {
111        byte [] b = {1, 2, 3, 4, 5};
112        byte [] r = c.update(b, 0, 5);
113        assertEquals("different length", b.length, r.length);
114        assertTrue("different content", Arrays.equals(b, r));
115
116        r = c.update(b, 1, 3);
117        assertEquals("different length", 3, r.length);
118        for (int i = 0; i < 3; i++) {
119            assertEquals("different content", b[i + 1], r[i]);
120        }
121    }
122
123    /*
124     * Class under test for int update(byte[], int, int, byte[])
125     */
126    public void testUpdatebyteArrayintintbyteArray() throws Exception {
127        byte [] b = {1, 2, 3, 4, 5};
128        byte [] r = new byte[5];
129        c.update(b, 0, 5, r);
130        assertTrue("different content", Arrays.equals(b, r));
131    }
132
133    /*
134     * Class under test for int update(byte[], int, int, byte[], int)
135     */
136    public void testUpdatebyteArrayintintbyteArrayint() throws Exception {
137        byte [] b = {1, 2, 3, 4, 5};
138        byte [] r = new byte[5];
139        c.update(b, 0, 5, r, 0);
140        assertTrue("different content", Arrays.equals(b, r));
141    }
142
143    /*
144     * Class under test for byte[] doFinal()
145     */
146    public void testDoFinal() throws Exception {
147        assertNull("doFinal failed", c.doFinal());
148    }
149
150    /*
151     * Class under test for int doFinal(byte[], int)
152     */
153    public void testDoFinalbyteArrayint() throws Exception {
154        byte [] r = new byte[5];
155        assertEquals("doFinal failed", 0, c.doFinal(r, 0));
156    }
157
158    /*
159     * Class under test for byte[] doFinal(byte[])
160     */
161    public void testDoFinalbyteArray() throws Exception {
162        byte [] b = {1, 2, 3, 4, 5};
163        byte [] r = null;
164        r = c.doFinal(b);
165        assertEquals("different length", b.length, r.length);
166        assertTrue("different content", Arrays.equals(b, r));
167    }
168
169    /*
170     * Class under test for byte[] doFinal(byte[], int, int)
171     */
172    public void testDoFinalbyteArrayintint() throws Exception {
173        byte [] b = {1, 2, 3, 4, 5};
174        byte [] r = null;
175        r = c.doFinal(b, 0, 5);
176        assertEquals("different length", b.length, r.length);
177        assertTrue("different content", Arrays.equals(b, r));
178
179        r = c.doFinal(b, 1, 3);
180        assertEquals("different length", 3, r.length);
181        for (int i = 0; i < 3; i++) {
182            assertEquals("different content", b[i + 1], r[i]);
183        }
184    }
185
186    /*
187     * Class under test for byte[] update(byte[], int, int)
188     */
189    public void testUpdatebyteArrayintint2() {
190        //Regression for HARMONY-758
191        try {
192            new NullCipher().update(new byte[1], 1, Integer.MAX_VALUE);
193            fail("Expected IllegalArgumentException was not thrown");
194        } catch (IllegalArgumentException e) {
195        }
196    }
197
198    /*
199     * Class under test for int doFinal(byte[], int, int, byte[])
200     */
201    public void testDoFinalbyteArrayintintbyteArray() throws Exception {
202        byte [] b = {1, 2, 3, 4, 5};
203        byte [] r = new byte[5];
204        c.doFinal(b, 0, 5, r);
205        assertTrue("different content", Arrays.equals(b, r));
206    }
207
208    /*
209     * Class under test for int doFinal(byte[], int, int, byte[])
210     */
211    public void testDoFinalbyteArrayintintbyteArray2() throws Exception {
212        //Regression for HARMONY-758
213        try {
214            new NullCipher().update(new byte[1], 1, Integer.MAX_VALUE,
215                    new byte[1]);
216            fail("Expected IllegalArgumentException was not thrown");
217        } catch (IllegalArgumentException e) {
218        }
219    }
220
221    /*
222     * Class under test for int doFinal(byte[], int, int, byte[])
223     */
224    public void testDoFinalbyteArrayintintbyteArray3() throws Exception {
225        //Regression for HARMONY-758
226        try {
227            new NullCipher().update(new byte[1], 0, 1, new byte[0]);
228            fail("Expected IndexOutOfBoundsException was not thrown");
229        } catch (IndexOutOfBoundsException e) {
230        }
231    }
232
233    /*
234     * Class under test for int doFinal(byte[], int, int, byte[], int)
235     */
236    public void testDoFinalbyteArrayintintbyteArrayint() throws Exception {
237        byte [] b = {1, 2, 3, 4, 5};
238        byte [] r = new byte[5];
239        c.doFinal(b, 0, 5, r, 0);
240        assertTrue("different content", Arrays.equals(b, r));
241    }
242
243    /*
244     * Class under test for int doFinal(byte[], int, int, byte[], int)
245     */
246    public void testDoFinalbyteArrayintintbyteArrayint2() throws Exception {
247        //Regression for HARMONY-758
248        try {
249            new NullCipher().update(new byte[1], 1, Integer.MAX_VALUE,
250                    new byte[1], 0);
251            fail("Expected IllegalArgumentException was not thrown");
252        } catch (IllegalArgumentException e) {
253        }
254    }
255
256    /*
257     * Class under test for int doFinal(byte[], int, int, byte[], int)
258     */
259    public void testDoFinalbyteArrayintintbyteArrayint3() throws Exception {
260        //Regression for HARMONY-758
261        try {
262            new NullCipher().update(new byte[1], 0, 1,
263                    new byte[0], 0);
264            fail("Expected IndexOutOfBoundsException was not thrown");
265        } catch (IndexOutOfBoundsException e) {
266        }
267    }
268}
269