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.TestLevel;
     26 import dalvik.annotation.TestTargetClass;
     27 import dalvik.annotation.TestTargetNew;
     28 
     29 import junit.framework.TestCase;
     30 
     31 import org.apache.harmony.security.tests.support.spec.MyEncodedKeySpec;
     32 
     33 import java.security.spec.EncodedKeySpec;
     34 import java.util.Arrays;
     35 
     36 /**
     37  * Tests for <code>EncodedKeySpec</code> class fields and methods.
     38  *
     39  */
     40 @TestTargetClass(EncodedKeySpec.class)
     41 public class EncodedKeySpecTest extends TestCase {
     42 
     43     /**
     44      * Tests for constructor <code>EncodedKeySpec(byte[])</code><br>
     45      */
     46     @TestTargetNew(
     47         level = TestLevel.COMPLETE,
     48         notes = "",
     49         method = "EncodedKeySpec",
     50         args = {byte[].class}
     51     )
     52     public final void testEncodedKeySpec() {
     53         byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
     54         EncodedKeySpec eks = new MyEncodedKeySpec(encodedKey);
     55 
     56         assertTrue("wrong encoded key was returned", Arrays.equals(encodedKey,
     57                 eks.getEncoded()));
     58         assertEquals("wrong name of encoding format", "My", eks.getFormat());
     59 
     60         encodedKey = null;
     61         try {
     62             eks = new MyEncodedKeySpec(encodedKey);
     63             fail("expected NullPointerException");
     64         } catch (NullPointerException e) {
     65             //
     66         }
     67     }
     68 
     69     /**
     70      * Tests that <code>getEncoded()</code> method returns valid byte array
     71      */
     72     @TestTargetNew(
     73         level = TestLevel.COMPLETE,
     74         notes = "",
     75         method = "getEncoded",
     76         args = {}
     77     )
     78     public final void testGetEncoded() {
     79 
     80         byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
     81         EncodedKeySpec meks = new MyEncodedKeySpec(encodedKey);
     82 
     83         /* Get encoded key */
     84         byte[] ek = meks.getEncoded();
     85 
     86         /* Check returned array */
     87         boolean result = true;
     88         for (int i = 0; i < encodedKey.length; i++) {
     89             if (encodedKey[i] != ek[i]) {
     90                 /* indicate failure */
     91                 result = false;
     92             }
     93         }
     94         /* passed */
     95         assertTrue(result);
     96     }
     97 
     98     /**
     99      * Tests that internal state of the object can not be modified by modifying
    100      * initial array value
    101      */
    102     @TestTargetNew(
    103         level = TestLevel.PARTIAL_COMPLETE,
    104         notes = "",
    105         method = "getEncoded",
    106         args = {}
    107     )
    108     public final void testIsStatePreserved1() {
    109         /* Create initial byte array */
    110         byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
    111 
    112         EncodedKeySpec meks = new MyEncodedKeySpec(encodedKey);
    113 
    114         /* Modify initial array's value */
    115         encodedKey[3] = (byte) 5;
    116 
    117         /* Get encoded key */
    118         byte[] ek = meks.getEncoded();
    119 
    120         /* Check that byte value has not been changed */
    121         assertTrue(ek[3] == (byte) 4);
    122     }
    123 
    124     /**
    125      * Tests that internal state of the object can not be modified using
    126      * returned value of <code>getEncoded()</code> method
    127      */
    128     @TestTargetNew(
    129         level = TestLevel.PARTIAL_COMPLETE,
    130         notes = "",
    131         method = "getEncoded",
    132         args = {}
    133     )
    134     public final void testIsStatePreserved2() {
    135 
    136         byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
    137         EncodedKeySpec meks = new MyEncodedKeySpec(encodedKey);
    138 
    139         /* Get encoded key */
    140         byte[] ek = meks.getEncoded();
    141         /* Modify returned value */
    142         ek[3] = (byte) 5;
    143         /* Get encoded key again */
    144         byte[] ek1 = meks.getEncoded();
    145 
    146         /* Check that byte value has not been changed */
    147         assertTrue(ek1[3] == (byte) 4);
    148     }
    149 
    150 }
    151