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