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 dalvik.annotation.TestTargets;
26import dalvik.annotation.TestLevel;
27import dalvik.annotation.TestTargetNew;
28import dalvik.annotation.TestTargetClass;
29
30import junit.framework.TestCase;
31
32import java.security.spec.EncodedKeySpec;
33import java.security.spec.PKCS8EncodedKeySpec;
34import java.security.spec.X509EncodedKeySpec;
35import java.util.Arrays;
36
37/**
38 * Tests for <code>X509EncodedKeySpec</code> class fields and methods
39 *
40 */
41@TestTargetClass(X509EncodedKeySpec.class)
42public class X509EncodedKeySpecTest extends TestCase {
43
44    //
45    // Test cases
46    //
47
48    /**
49     * Test for <code>X509EncodedKeySpec</code> constructor<br>
50     * Assertion: constructs new <code>X509EncodedKeySpec</code>
51     * object using valid parameter
52     */
53    @TestTargetNew(
54        level = TestLevel.COMPLETE,
55        notes = "",
56        method = "X509EncodedKeySpec",
57        args = {byte[].class}
58    )
59    public final void testX509EncodedKeySpec() {
60        byte[] encodedKey = new byte[] {(byte)1,(byte)2,(byte)3,(byte)4};
61
62        EncodedKeySpec eks = new X509EncodedKeySpec(encodedKey);
63
64        assertTrue(eks instanceof X509EncodedKeySpec);
65        try {
66            eks = new X509EncodedKeySpec(null);
67            fail("expected NullPointerException");
68        } catch (NullPointerException e) {
69            // ok
70        }
71    }
72
73    /**
74     * Test for <code>getEncoded()</code> method<br>
75     * Assertion: returns encoded key
76     */
77    @TestTargetNew(
78        level = TestLevel.COMPLETE,
79        notes = "",
80        method = "getEncoded",
81        args = {}
82    )
83    public final void testGetEncoded() {
84        byte[] encodedKey = new byte[] {(byte)1,(byte)2,(byte)3,(byte)4};
85
86        X509EncodedKeySpec eks = new X509EncodedKeySpec(encodedKey);
87
88        byte[] ek = eks.getEncoded();
89
90        assertTrue(Arrays.equals(encodedKey, ek));
91    }
92
93    /**
94     * Test for <code>getFormat()</code> method
95     * Assertion: returns format name (always "X.509")
96     */
97    @TestTargetNew(
98        level = TestLevel.COMPLETE,
99        notes = "",
100        method = "getFormat",
101        args = {}
102    )
103    public final void testGetFormat() {
104        byte[] encodedKey = new byte[] {(byte)1,(byte)2,(byte)3,(byte)4};
105
106        X509EncodedKeySpec meks = new X509EncodedKeySpec(encodedKey);
107
108        assertEquals("X.509", meks.getFormat());
109    }
110
111    /**
112     * Tests that internal state of the object
113     * can not be changed by modifying initial
114     * array value
115     */
116    @TestTargetNew(
117        level = TestLevel.PARTIAL_COMPLETE,
118        notes = "",
119        method = "getEncoded",
120        args = {}
121    )
122    public final void testIsStatePreserved1() {
123        // Reference array
124        byte[] encodedKey = new byte[] {(byte)1,(byte)2,(byte)3,(byte)4};
125        // Reference array's copy will be used for test
126        byte[] encodedKeyCopy = encodedKey.clone();
127
128        X509EncodedKeySpec meks = new X509EncodedKeySpec(encodedKeyCopy);
129
130        // Modify initial array's value
131        encodedKeyCopy[3] = (byte)5;
132
133        // Get encoded key
134        byte[] ek = meks.getEncoded();
135
136        // Check  using reference array that
137        // byte value has not been changed
138        assertTrue(Arrays.equals(encodedKey, ek));
139    }
140
141    /**
142     * Tests that internal state of the object
143     * can not be modified using returned value
144     * of <code>getEncoded()</code> method
145     */
146    @TestTargetNew(
147        level = TestLevel.PARTIAL_COMPLETE,
148        notes = "",
149        method = "getEncoded",
150        args = {}
151    )
152    public final void testIsStatePreserved2() {
153        // Reference array
154        byte[] encodedKey = new byte[] {(byte)1,(byte)2,(byte)3,(byte)4};
155        // Reference array's copy will be used for test
156        byte[] encodedKeyCopy = encodedKey.clone();
157
158        X509EncodedKeySpec meks = new X509EncodedKeySpec(encodedKeyCopy);
159
160        byte[] ek = meks.getEncoded();
161
162        // Modify returned array
163        ek[3] = (byte)5;
164
165        // Get encoded key again
166        byte[] ek1 = meks.getEncoded();
167
168        // Check using reference array that
169        // byte value has not been changed
170        assertTrue(Arrays.equals(encodedKey, ek1));
171    }
172
173}
174