PKCS8EncodedKeySpecTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
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*/ 21 22package org.apache.harmony.security.tests.java.security.spec; 23 24import java.security.spec.EncodedKeySpec; 25import java.security.spec.PKCS8EncodedKeySpec; 26import java.util.Arrays; 27 28import junit.framework.TestCase; 29 30/** 31 * Tests for <code>PKCS8EncodedKeySpec</code> class fields and methods. 32 * 33 */ 34public class PKCS8EncodedKeySpecTest extends TestCase { 35 36 /** 37 * Constructor for PKCS8EncodedKeySpecTest. 38 * @param name 39 */ 40 public PKCS8EncodedKeySpecTest(String name) { 41 super(name); 42 } 43 44 // 45 // Tests 46 // 47 48 /** 49 * Test for <code>PKCS8EncodedKeySpec</code> constructor<br> 50 * Assertion: constructs new <code>PKCS8EncodedKeySpec</code> 51 * object using valid parameter 52 */ 53 public final void testPKCS8EncodedKeySpec() { 54 byte[] encodedKey = new byte[] {(byte)1,(byte)2,(byte)3,(byte)4}; 55 56 EncodedKeySpec eks = new PKCS8EncodedKeySpec(encodedKey); 57 58 assertTrue(eks instanceof PKCS8EncodedKeySpec); 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 PKCS8EncodedKeySpec meks = new PKCS8EncodedKeySpec(encodedKey); 69 70 byte[] ek = meks.getEncoded(); 71 72 assertTrue(Arrays.equals(encodedKey, ek)); 73 } 74 75 /** 76 * Test for <code>getFormat()</code> method 77 * Assertion: returns format name (always "PKCS#8") 78 */ 79 public final void testGetFormat() { 80 byte[] encodedKey = new byte[] {(byte)1,(byte)2,(byte)3,(byte)4}; 81 82 PKCS8EncodedKeySpec meks = new PKCS8EncodedKeySpec(encodedKey); 83 84 assertEquals("PKCS#8", 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 PKCS8EncodedKeySpec meks = new PKCS8EncodedKeySpec(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 PKCS8EncodedKeySpec meks = new PKCS8EncodedKeySpec(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