EncodedKeySpecTest.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; 25 26import org.apache.harmony.security.tests.support.spec.MyEncodedKeySpec; 27 28import junit.framework.TestCase; 29 30/** 31 * Tests for <code>EncodedKeySpec</code> class fields and methods. 32 * 33 */ 34 35public class EncodedKeySpecTest extends TestCase { 36 37 /** 38 * Constructor for EncodedKeySpecTest. 39 * @param name 40 */ 41 public EncodedKeySpecTest(String name) { 42 super(name); 43 } 44 45 /** 46 * Tests that <code>getEncoded()</code> method 47 * returns valid byte array 48 */ 49 public final void testGetEncoded() { 50 51 byte[] encodedKey = new byte[] {(byte)1,(byte)2,(byte)3,(byte)4}; 52 EncodedKeySpec meks = new MyEncodedKeySpec(encodedKey); 53 54 /* Get encoded key */ 55 byte[] ek = meks.getEncoded(); 56 57 /* Check returned array */ 58 boolean result = true; 59 for (int i=0; i<encodedKey.length; i++) { 60 if (encodedKey[i] != ek[i]) { 61 /* indicate failure */ 62 result = false; 63 } 64 } 65 /* passed */ 66 assertTrue(result); 67 } 68 69 /** 70 * Tests that internal state of the object 71 * can not be modified by modifying initial array value 72 */ 73 public final void testIsStatePreserved1() { 74 /* Create initial byte array */ 75 byte[] encodedKey = new byte[] {(byte)1,(byte)2,(byte)3,(byte)4}; 76 77 EncodedKeySpec meks = new MyEncodedKeySpec(encodedKey); 78 79 /* Modify initial array's value */ 80 encodedKey[3] = (byte)5; 81 82 /* Get encoded key */ 83 byte[] ek = meks.getEncoded(); 84 85 /* Check that byte value has not been changed */ 86 assertTrue(ek[3] == (byte)4); 87 } 88 89 /** 90 * Tests that internal state of the object 91 * can not be modified using returned value 92 * of <code>getEncoded()</code> method 93 */ 94 public final void testIsStatePreserved2() { 95 96 byte[] encodedKey = new byte[] {(byte)1,(byte)2,(byte)3,(byte)4}; 97 EncodedKeySpec meks = new MyEncodedKeySpec(encodedKey); 98 99 /* Get encoded key */ 100 byte[] ek = meks.getEncoded(); 101 /* Modify returned value */ 102 ek[3] = (byte)5; 103 /* Get encoded key again */ 104 byte[] ek1 = meks.getEncoded(); 105 106 /* Check that byte value has not been changed */ 107 assertTrue(ek1[3] == (byte)4); 108 } 109 110} 111