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 Vladimir N. Molotkov
20* @version $Revision$
21*/
22
23package tests.security.spec;
24
25import junit.framework.TestCase;
26
27import java.security.spec.EncodedKeySpec;
28import java.security.spec.PKCS8EncodedKeySpec;
29import java.security.spec.X509EncodedKeySpec;
30import java.util.Arrays;
31
32/**
33 * Tests for <code>X509EncodedKeySpec</code> class fields and methods
34 *
35 */
36public class X509EncodedKeySpecTest extends TestCase {
37
38    //
39    // Test cases
40    //
41
42    /**
43     * Test for <code>X509EncodedKeySpec</code> constructor<br>
44     * Assertion: constructs new <code>X509EncodedKeySpec</code>
45     * object using valid parameter
46     */
47    public final void testX509EncodedKeySpec() {
48        byte[] encodedKey = new byte[] {(byte)1,(byte)2,(byte)3,(byte)4};
49
50        EncodedKeySpec eks = new X509EncodedKeySpec(encodedKey);
51
52        assertTrue(eks instanceof X509EncodedKeySpec);
53        try {
54            eks = new X509EncodedKeySpec(null);
55            fail("expected NullPointerException");
56        } catch (NullPointerException e) {
57            // ok
58        }
59    }
60
61    /**
62     * Test for <code>getEncoded()</code> method<br>
63     * Assertion: returns encoded key
64     */
65    public final void testGetEncoded() {
66        byte[] encodedKey = new byte[] {(byte)1,(byte)2,(byte)3,(byte)4};
67
68        X509EncodedKeySpec eks = new X509EncodedKeySpec(encodedKey);
69
70        byte[] ek = eks.getEncoded();
71
72        assertTrue(Arrays.equals(encodedKey, ek));
73    }
74
75    /**
76     * Test for <code>getFormat()</code> method
77     * Assertion: returns format name (always "X.509")
78     */
79    public final void testGetFormat() {
80        byte[] encodedKey = new byte[] {(byte)1,(byte)2,(byte)3,(byte)4};
81
82        X509EncodedKeySpec meks = new X509EncodedKeySpec(encodedKey);
83
84        assertEquals("X.509", meks.getFormat());
85    }
86
87    /**
88     * Tests that internal state of the object
89     * can not be changed by modifying initial
90     * array value
91     */
92    public final void testIsStatePreserved1() {
93        // Reference array
94        byte[] encodedKey = new byte[] {(byte)1,(byte)2,(byte)3,(byte)4};
95        // Reference array's copy will be used for test
96        byte[] encodedKeyCopy = encodedKey.clone();
97
98        X509EncodedKeySpec meks = new X509EncodedKeySpec(encodedKeyCopy);
99
100        // Modify initial array's value
101        encodedKeyCopy[3] = (byte)5;
102
103        // Get encoded key
104        byte[] ek = meks.getEncoded();
105
106        // Check  using reference array that
107        // byte value has not been changed
108        assertTrue(Arrays.equals(encodedKey, ek));
109    }
110
111    /**
112     * Tests that internal state of the object
113     * can not be modified using returned value
114     * of <code>getEncoded()</code> method
115     */
116    public final void testIsStatePreserved2() {
117        // Reference array
118        byte[] encodedKey = new byte[] {(byte)1,(byte)2,(byte)3,(byte)4};
119        // Reference array's copy will be used for test
120        byte[] encodedKeyCopy = encodedKey.clone();
121
122        X509EncodedKeySpec meks = new X509EncodedKeySpec(encodedKeyCopy);
123
124        byte[] ek = meks.getEncoded();
125
126        // Modify returned array
127        ek[3] = (byte)5;
128
129        // Get encoded key again
130        byte[] ek1 = meks.getEncoded();
131
132        // Check using reference array that
133        // byte value has not been changed
134        assertTrue(Arrays.equals(encodedKey, ek1));
135    }
136
137}
138