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