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 Alexander Y. Kleymenov
     20 * @version $Revision$
     21 */
     22 
     23 package org.apache.harmony.crypto.tests.javax.crypto.spec;
     24 
     25 import dalvik.annotation.TestTargetClass;
     26 import dalvik.annotation.TestTargets;
     27 import dalvik.annotation.TestLevel;
     28 import dalvik.annotation.TestTargetNew;
     29 
     30 import java.lang.IllegalArgumentException;
     31 import java.util.Arrays;
     32 
     33 import javax.crypto.spec.RC2ParameterSpec;
     34 
     35 import junit.framework.Test;
     36 import junit.framework.TestCase;
     37 import junit.framework.TestSuite;
     38 
     39 @TestTargetClass(RC2ParameterSpec.class)
     40 /**
     41  */
     42 
     43 public class RC2ParameterSpecTest extends TestCase {
     44 
     45     /**
     46      * RC2ParameterSpec(int effectiveKeyBits, byte[] iv) method testing.
     47      * Tests that IllegalArgumentException is thrown in the case of
     48      * inappropriate constructor parameters and that input iv array is
     49      * copied to protect against subsequent modification.
     50      */
     51     @TestTargetNew(
     52         level = TestLevel.COMPLETE,
     53         notes = "",
     54         method = "RC2ParameterSpec",
     55         args = {int.class, byte[].class}
     56     )
     57     public void testRC2ParameterSpec1() {
     58         int effectiveKeyBits = 10;
     59         byte[] iv = {1, 2, 3, 4, 5, 6, 7, 8};
     60 
     61         try {
     62             new RC2ParameterSpec(effectiveKeyBits, null);
     63             fail("An IllegalArgumentException should be thrown "
     64                     + "in the case of null iv.");
     65         } catch (IllegalArgumentException e) {
     66         }
     67 
     68         try {
     69             new RC2ParameterSpec(effectiveKeyBits, new byte[] {1, 2, 3, 4, 5});
     70             fail("An IllegalArgumentException should be thrown "
     71                     + "in the case of short iv.");
     72         } catch (IllegalArgumentException e) {
     73         }
     74 
     75         RC2ParameterSpec ps = new RC2ParameterSpec(effectiveKeyBits, iv);
     76         iv[0] ++;
     77         assertFalse("The change of iv specified in the constructor "
     78                     + "should not cause the change of internal array.",
     79                     iv[0] == ps.getIV()[0]);
     80     }
     81 
     82     /**
     83      * RC2ParameterSpec(int effectiveKeyBits, byte[] iv, int offset) method
     84      * testing. Tests that IllegalArgumentException is thrown in the case of
     85      * inappropriate constructor parameters and that input iv array is
     86      * copied to protect against subsequent modification.
     87      */
     88     @TestTargetNew(
     89         level = TestLevel.COMPLETE,
     90         notes = "",
     91         method = "RC2ParameterSpec",
     92         args = {int.class, byte[].class, int.class}
     93     )
     94     public void testRC2ParameterSpec2() {
     95         int effectiveKeyBits = 10;
     96         byte[] iv = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
     97         int offset = 2;
     98 
     99         try {
    100             new RC2ParameterSpec(effectiveKeyBits, null, offset);
    101             fail("An IllegalArgumentException should be thrown "
    102                     + "in the case of null iv.");
    103         } catch (IllegalArgumentException e) {
    104         }
    105 
    106         try {
    107             new RC2ParameterSpec(effectiveKeyBits, iv, 4);
    108             fail("An IllegalArgumentException should be thrown "
    109                     + "in the case of short iv.");
    110         } catch (IllegalArgumentException e) {
    111         }
    112 
    113         RC2ParameterSpec ps = new RC2ParameterSpec(effectiveKeyBits, iv, offset);
    114         iv[offset] ++;
    115         assertFalse("The change of iv specified in the constructor "
    116                     + "should not cause the change of internal array.",
    117                     iv[offset] == ps.getIV()[0]);
    118     }
    119 
    120     /**
    121      * getEffectiveKeyBits() method testing. Tests that returned value is
    122      * equal to the value specified in the constructor.
    123      */
    124     @TestTargetNew(
    125         level = TestLevel.COMPLETE,
    126         notes = "",
    127         method = "getEffectiveKeyBits",
    128         args = {}
    129     )
    130     public void testGetEffectiveKeyBits() {
    131         int effectiveKeyBits = 10;
    132         byte[] iv = {1, 2, 3, 4, 5, 6, 7, 8};
    133 
    134         RC2ParameterSpec ps = new RC2ParameterSpec(effectiveKeyBits, iv);
    135         assertTrue("The returned effectiveKeyBits value is not equal to the "
    136                 + "value specified in the constructor.",
    137                 effectiveKeyBits == ps.getEffectiveKeyBits());
    138     }
    139 
    140     /**
    141      * getIV() method testing. Tests that returned array is equal to the
    142      * array specified in the constructor. Checks that modification
    143      * of returned array does not affect the internal array. Also it checks
    144      * that getIV() method returns null if iv is not specified.
    145      */
    146     @TestTargetNew(
    147         level = TestLevel.COMPLETE,
    148         notes = "",
    149         method = "getIV",
    150         args = {}
    151     )
    152     public void testGetIV() {
    153         int effectiveKeyBits = 10;
    154         byte[] iv = new byte[] {1, 2, 3, 4, 5, 6, 7, 8};
    155 
    156         RC2ParameterSpec ps = new RC2ParameterSpec(effectiveKeyBits, iv);
    157         byte[] result = ps.getIV();
    158         if (! Arrays.equals(iv, result)) {
    159             fail("The returned iv is not equal to the specified "
    160                     + "in the constructor.");
    161         }
    162         result[0] ++;
    163         assertFalse("The change of returned by getIV() method iv "
    164                     + "should not cause the change of internal array.",
    165                     result[0] == ps.getIV()[0]);
    166         ps = new RC2ParameterSpec(effectiveKeyBits);
    167         assertNull("The getIV() method should return null if the parameter "
    168                     + "set does not contain iv.", ps.getIV());
    169     }
    170 
    171     /**
    172      * equals(Object obj) method testing. Tests the correctness of equal
    173      * operation: it should be reflexive, symmetric, transitive, consistent
    174      * and should be false on null object.
    175      */
    176     @TestTargetNew(
    177         level = TestLevel.COMPLETE,
    178         notes = "",
    179         method = "equals",
    180         args = {java.lang.Object.class}
    181     )
    182     public void testEquals() {
    183         int effectiveKeyBits = 10;
    184         byte[] iv = new byte[] {1, 2, 3, 4, 5, 6, 7, 8};
    185 
    186         RC2ParameterSpec ps1 = new RC2ParameterSpec(effectiveKeyBits, iv);
    187         RC2ParameterSpec ps2 = new RC2ParameterSpec(effectiveKeyBits, iv);
    188         RC2ParameterSpec ps3 = new RC2ParameterSpec(10,
    189                                     new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9});
    190 
    191         // checking for reflexive law:
    192         assertTrue("The equivalence relation should be reflexive.",
    193                                                         ps1.equals(ps1));
    194 
    195         assertTrue("Objects built on the same parameters should be equal.",
    196                                                         ps1.equals(ps2));
    197         // checking for symmetric law:
    198         assertTrue("The equivalence relation should be symmetric.",
    199                                                         ps2.equals(ps1));
    200 
    201         assertTrue("Objects built on the equal parameters should be equal.",
    202                                                         ps2.equals(ps3));
    203 
    204         // checking for transitive law:
    205         assertTrue("The equivalence relation should be transitive.",
    206                                                         ps1.equals(ps3));
    207 
    208         assertFalse("Should return not be equal to null object.",
    209                                                         ps1.equals(null));
    210 
    211         ps2 = new RC2ParameterSpec(11, iv);
    212         assertFalse("Objects should not be equal.", ps1.equals(ps2));
    213 
    214         ps2 = new RC2ParameterSpec(11, new byte[] {9, 8, 7, 6, 5, 4, 3, 2, 1});
    215         assertFalse("Objects should not be equal.", ps1.equals(ps2));
    216     }
    217 
    218     /**
    219      * hashCode() method testing. Tests that for equal objects hash codes
    220      * are equal.
    221      */
    222     @TestTargetNew(
    223         level = TestLevel.COMPLETE,
    224         notes = "",
    225         method = "hashCode",
    226         args = {}
    227     )
    228     public void testHashCode() {
    229         int effectiveKeyBits = 0;
    230         byte[] iv = new byte[] {1, 2, 3, 4, 5, 6, 7, 8};
    231 
    232         RC2ParameterSpec ps1 = new RC2ParameterSpec(effectiveKeyBits, iv);
    233         RC2ParameterSpec ps2 = new RC2ParameterSpec(effectiveKeyBits, iv);
    234 
    235         assertTrue("Equal objects should have the same hash codes.",
    236                                             ps1.hashCode() == ps2.hashCode());
    237     }
    238 
    239     @TestTargetNew(
    240         level = TestLevel.COMPLETE,
    241         notes = "",
    242         method = "RC2ParameterSpec",
    243         args = {int.class}
    244     )
    245     public void test_constructorI() {
    246         int effectiveKeyBits = 0;
    247 
    248         RC2ParameterSpec ps1 = new RC2ParameterSpec(effectiveKeyBits);
    249         RC2ParameterSpec ps2 = new RC2ParameterSpec(effectiveKeyBits);
    250 
    251         assertTrue(ps1.equals(ps2));
    252     }
    253 
    254     public static Test suite() {
    255         return new TestSuite(RC2ParameterSpecTest.class);
    256     }
    257 }
    258 
    259