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.NullPointerException;
26import java.security.InvalidKeyException;
27import java.util.Arrays;
28
29import javax.crypto.spec.DESedeKeySpec;
30
31import junit.framework.Test;
32import junit.framework.TestCase;
33import junit.framework.TestSuite;
34
35/**
36 */
37public class DESedeKeySpecTest extends TestCase {
38
39    /**
40     * Constructors testing. Tests behavior of each of two constructors
41     * in the cases of: null array, short array, normal array.
42     */
43    public void testDESedeKeySpec() {
44        try {
45            new DESedeKeySpec((byte []) null);
46            fail("Should raise an NullPointerException "
47                    + "in case of null byte array.");
48        } catch (NullPointerException e) {
49        } catch (InvalidKeyException e) {
50            fail("Should raise an NullPointerException "
51                    + "in case of null byte array.");
52        }
53        try {
54            new DESedeKeySpec(new byte [] {1, 2, 3});
55            fail("Should raise an InvalidKeyException on a short byte array.");
56        } catch (NullPointerException e) {
57            fail("Unexpected NullPointerException was thrown.");
58        } catch (InvalidKeyException e) {
59        }
60        try {
61            new DESedeKeySpec(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
62                                          1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
63                                          1, 2, 3, 4});
64        } catch (NullPointerException e) {
65            fail("Unexpected NullPointerException was thrown.");
66        } catch (InvalidKeyException e) {
67            fail("Unexpected InvalidKeyException was thrown.");
68        }
69        try {
70            new DESedeKeySpec((byte []) null, 1);
71            fail("Should raise an NullPointerException "
72                    + "in case of null byte array.");
73        } catch (NullPointerException e) {
74        } catch (InvalidKeyException e) {
75            fail("Should raise an NullPointerException "
76                    + "in case of null byte array.");
77        }
78        try {
79            new DESedeKeySpec(new byte []  {1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
80                                            1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
81                                            1, 2, 3, 4}, 1);
82            fail("Should raise an InvalidKeyException on a short byte array.");
83        } catch (NullPointerException e) {
84            fail("Unexpected NullPointerException was thrown.");
85        } catch (InvalidKeyException e) {
86        }
87        try {
88            new DESedeKeySpec(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
89                                          1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
90                                          1, 2, 3, 4, 5}, 1);
91        } catch (NullPointerException e) {
92            fail("Unexpected NullPointerException was thrown.");
93        } catch (InvalidKeyException e) {
94            fail("Unexpected InvalidKeyException was thrown.");
95        }
96    }
97
98    /**
99     * getKey() method testing. Checks that modification of returned key
100     * does not affect the internal key. Also test check an equality of
101     * the key with the key specified in the constructor. The object under
102     * the test is created by different constructors.
103     */
104    public void testGetKey() {
105        byte[] key = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2,
106                      1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2};
107        DESedeKeySpec ks;
108        try {
109            ks = new DESedeKeySpec(key);
110        } catch (InvalidKeyException e) {
111            fail("InvalidKeyException should not be thrown.");
112            return;
113        }
114        byte[] res = ks.getKey();
115        assertTrue("The returned array should be equal to the specified "
116                    + "in constructor.", Arrays.equals(key, res));
117        res[0] += 1;
118        assertFalse("The modification of returned key should not affect"
119                    + "the underlying key.", key[0] == res[0]);
120
121        byte[] key1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3,
122                       1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3};
123        try {
124            ks = new DESedeKeySpec(key1, 2);
125        } catch (InvalidKeyException e) {
126            fail("InvalidKeyException should not be thrown.");
127            return;
128        }
129        res = ks.getKey();
130        assertNotSame("The returned array should not be the same object "
131                    + "as specified in a constructor.", key1, res);
132        byte[] exp = new byte[24];
133        System.arraycopy(key1, 2, exp, 0, 24);
134        assertTrue("The returned array should be equal to the specified "
135                    + "in constructor.", Arrays.equals(exp, res));
136    }
137
138    /**
139     * isParityAdjusted(byte[] key, offset) method testing. Tests if the
140     * method throws appropriate exceptions on incorrect byte array, if
141     * it returns false on the key which is not parity adjusted, and if
142     * it returns true on parity adjusted key.
143     */
144    public void testIsParityAdjusted() {
145        try {
146            DESedeKeySpec.isParityAdjusted(null, 1);
147            fail("Should raise an NullPointerException "
148                    + "in case of null byte array.");
149        } catch (NullPointerException e) {
150        } catch (InvalidKeyException e) {
151            fail("Should raise an NullPointerException "
152                    + "in case of null byte array.");
153        }
154
155        byte[] key = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
156                      1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
157        try {
158            DESedeKeySpec.isParityAdjusted(key, 1);
159            fail("Should raise an InvalidKeyException "
160                    + "in case of short byte array.");
161        } catch (NullPointerException e) {
162            fail("Unexpected NullPointerException was thrown.");
163        } catch (InvalidKeyException e) {
164        }
165
166        byte[] key_not_pa = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2,
167                             1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2};
168        try {
169            assertFalse("Method returns true when false is expected.",
170                        DESedeKeySpec.isParityAdjusted(key_not_pa, 0));
171        } catch (NullPointerException e) {
172            fail("Unexpected NullPointerException was thrown.");
173        } catch (InvalidKeyException e) {
174            fail("Unexpected InvalidKeyException was thrown.");
175        }
176
177        byte[] key_pa = {(byte) 128, (byte) 131, (byte) 133, (byte) 134,
178                         (byte) 137, (byte) 138, (byte) 140, (byte) 143,
179                         (byte) 145, (byte) 146, (byte) 148, (byte) 151,
180                         (byte) 152, (byte) 155, (byte) 157, (byte) 158,
181                         (byte) 161, (byte) 162, (byte) 164, (byte) 167,
182                         (byte) 168, (byte) 171, (byte) 173, (byte) 174};
183        try {
184            assertTrue("Method returns false when true is expected.",
185                        DESedeKeySpec.isParityAdjusted(key_pa, 0));
186        } catch (NullPointerException e) {
187            fail("Unexpected NullPointerException was thrown.");
188        } catch (InvalidKeyException e) {
189            fail("Unexpected InvalidKeyException was thrown.");
190        }
191    }
192
193    public static Test suite() {
194        return new TestSuite(DESedeKeySpecTest.class);
195    }
196}
197
198