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