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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @author Vladimir N. Molotkov
23 package tests.security.spec;
25 import dalvik.annotation.TestTargets;
26 import dalvik.annotation.TestLevel;
27 import dalvik.annotation.TestTargetNew;
28 import dalvik.annotation.TestTargetClass;
30 import junit.framework.TestCase;
32 import java.security.spec.EncodedKeySpec;
33 import java.security.spec.PKCS8EncodedKeySpec;
34 import java.util.Arrays;
37 * Tests for <code>PKCS8EncodedKeySpec</code> class fields and methods.
40 @TestTargetClass(PKCS8EncodedKeySpec.class)
41 public class PKCS8EncodedKeySpecTest extends TestCase {
48 * Test for <code>PKCS8EncodedKeySpec</code> constructor<br>
49 * Assertion: constructs new <code>PKCS8EncodedKeySpec</code>
50 * object using valid parameter
53 level = TestLevel.COMPLETE,
55 method = "PKCS8EncodedKeySpec",
58 public final void testPKCS8EncodedKeySpec() {
59 byte[] encodedKey = new byte[] {(byte)1,(byte)2,(byte)3,(byte)4};
61 EncodedKeySpec eks = new PKCS8EncodedKeySpec(encodedKey);
63 assertTrue(eks instanceof PKCS8EncodedKeySpec);
65 eks = new PKCS8EncodedKeySpec(null);
66 fail("expected NullPointerException");
67 } catch (NullPointerException e) {
73 * Test for <code>getEncoded()</code> method<br>
74 * Assertion: returns encoded key
77 level = TestLevel.COMPLETE,
79 method = "getEncoded",
82 public final void testGetEncoded() {
83 byte[] encodedKey = new byte[] {(byte)1,(byte)2,(byte)3,(byte)4};
85 PKCS8EncodedKeySpec meks = new PKCS8EncodedKeySpec(encodedKey);
87 byte[] ek = meks.getEncoded();
89 assertTrue(Arrays.equals(encodedKey, ek));
93 * Test for <code>getFormat()</code> method
94 * Assertion: returns format name (always "PKCS#8")
97 level = TestLevel.COMPLETE,
102 public final void testGetFormat() {
103 byte[] encodedKey = new byte[] {(byte)1,(byte)2,(byte)3,(byte)4};
105 PKCS8EncodedKeySpec meks = new PKCS8EncodedKeySpec(encodedKey);
107 assertEquals("PKCS#8", meks.getFormat());
111 * Tests that internal state of the object
112 * can not be changed by modifying initial
116 level = TestLevel.PARTIAL_COMPLETE,
118 method = "getEncoded",
121 public final void testIsStatePreserved1() {
123 byte[] encodedKey = new byte[] {(byte)1,(byte)2,(byte)3,(byte)4};
124 // Reference array's copy will be used for test
125 byte[] encodedKeyCopy = encodedKey.clone();
127 PKCS8EncodedKeySpec meks = new PKCS8EncodedKeySpec(encodedKeyCopy);
129 // Modify initial array's value
130 encodedKeyCopy[3] = (byte)5;
133 byte[] ek = meks.getEncoded();
135 // Check using reference array that
136 // byte value has not been changed
137 assertTrue(Arrays.equals(encodedKey, ek));
141 * Tests that internal state of the object
142 * can not be modified using returned value
143 * of <code>getEncoded()</code> method
146 level = TestLevel.PARTIAL_COMPLETE,
148 method = "getEncoded",
151 public final void testIsStatePreserved2() {
153 byte[] encodedKey = new byte[] {(byte)1,(byte)2,(byte)3,(byte)4};
154 // Reference array's copy will be used for test
155 byte[] encodedKeyCopy = encodedKey.clone();
157 PKCS8EncodedKeySpec meks = new PKCS8EncodedKeySpec(encodedKeyCopy);
159 byte[] ek = meks.getEncoded();
161 // Modify returned array
164 // Get encoded key again
165 byte[] ek1 = meks.getEncoded();
167 // Check using reference array that
168 // byte value has not been changed
169 assertTrue(Arrays.equals(encodedKey, ek1));