Home | History | Annotate | Download | only in security
      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 Boris V. Kuznetsov
     20 * @version $Revision$
     21 */
     22 
     23 package tests.java.security;
     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.security.NoSuchAlgorithmException;
     31 import java.security.NoSuchProviderException;
     32 import java.security.Provider;
     33 import java.security.SecureRandom;
     34 import java.security.Security;
     35 
     36 import org.apache.harmony.security.tests.support.RandomImpl;
     37 
     38 import junit.framework.TestCase;
     39 @TestTargetClass(SecureRandom.class)
     40 /**
     41  * Tests for <code>SecureRandom</code> constructor and methods
     42  *
     43  */
     44 public class SecureRandomTest extends TestCase {
     45 
     46     /**
     47      * SRProvider
     48      */
     49     Provider p;
     50 
     51     /*
     52      * @see TestCase#setUp()
     53      */
     54     protected void setUp() throws Exception {
     55         super.setUp();
     56         p = new SRProvider();
     57         Security.insertProviderAt(p, 1);
     58     }
     59 
     60     /*
     61      * @see TestCase#tearDown()
     62      */
     63     protected void tearDown() throws Exception {
     64         super.tearDown();
     65         Security.removeProvider(p.getName());
     66     }
     67 
     68     @TestTargetNew(
     69         level = TestLevel.PARTIAL,
     70         notes = "Verification of negative and boundary parameters missed",
     71         method = "next",
     72         args = {int.class}
     73     )
     74     public final void testNext() {
     75         MySecureRandom sr = new MySecureRandom();
     76         if (sr.nextElement(1) != 1 || sr.nextElement(2) != 3 || sr.nextElement(3) != 7) {
     77             fail("next failed");
     78         }
     79     }
     80 
     81     /*
     82      * Class under test for void setSeed(long)
     83      */
     84     @TestTargetNew(
     85         level = TestLevel.PARTIAL_COMPLETE,
     86         notes = "",
     87         method = "setSeed",
     88         args = {long.class}
     89     )
     90     public final void testSetSeedlong() {
     91         SecureRandom sr = new SecureRandom();
     92         sr.setSeed(12345);
     93         if (!RandomImpl.runEngineSetSeed) {
     94             fail("setSeed failed");
     95         }
     96     }
     97 
     98     @TestTargetNew(
     99         level = TestLevel.PARTIAL_COMPLETE,
    100         notes = "",
    101         method = "nextBytes",
    102         args = {byte[].class}
    103     )
    104     public final void testNextBytes() {
    105         byte[] b = new byte[5];
    106         SecureRandom sr = new SecureRandom();
    107         sr.nextBytes(b);
    108         for (int i = 0; i < b.length; i++) {
    109             if (b[i] != (byte)(i + 0xF1)) {
    110                 fail("nextBytes failed");
    111             }
    112         }
    113 
    114         try {
    115             sr.nextBytes(null);
    116             fail("expected exception");
    117         } catch (Exception e) {
    118             // ok
    119         }
    120     }
    121 
    122     /*
    123      * Class under test for void SecureRandom()
    124      */
    125     @TestTargetNew(
    126         level = TestLevel.COMPLETE,
    127         notes = "",
    128         method = "SecureRandom",
    129         args = {}
    130     )
    131     public final void testSecureRandom() {
    132         SecureRandom sr = new SecureRandom();
    133         if (!sr.getAlgorithm().equals("someRandom")  ||
    134                 sr.getProvider()!= p) {
    135             fail("incorrect SecureRandom implementation" + p.getName());
    136         }
    137     }
    138 
    139     /*
    140      * Class under test for void SecureRandom(byte[])
    141      */
    142     @TestTargetNew(
    143         level = TestLevel.PARTIAL,
    144         notes = "Null parameter checking missed",
    145         method = "SecureRandom",
    146         args = {byte[].class}
    147     )
    148     public final void testSecureRandombyteArray() {
    149         byte[] b = {1,2,3};
    150         new SecureRandom(b);
    151 
    152         if (!RandomImpl.runEngineSetSeed) {
    153             fail("No setSeed");
    154         }
    155 
    156 
    157     }
    158 
    159     /*
    160      * Class under test for SecureRandom getInstance(String)
    161      */
    162     @TestTargetNew(
    163         level = TestLevel.PARTIAL,
    164         notes = "NoSuchAlgorithmException checking missed",
    165         method = "getInstance",
    166         args = {java.lang.String.class}
    167     )
    168     public final void testGetInstanceString() {
    169         SecureRandom sr = null;
    170         try {
    171             sr = SecureRandom.getInstance("someRandom");
    172         } catch (NoSuchAlgorithmException e) {
    173             fail(e.toString());
    174         }
    175         if (sr.getProvider() != p || !"someRandom".equals(sr.getAlgorithm())) {
    176             fail("getInstance failed");
    177         }
    178     }
    179 
    180     /*
    181      * Class under test for SecureRandom getInstance(String, String)
    182      */
    183     @TestTargetNew(
    184         level = TestLevel.COMPLETE,
    185         notes = "",
    186         method = "getInstance",
    187         args = {java.lang.String.class, java.lang.String.class}
    188     )
    189     public final void testGetInstanceStringString() throws Exception {
    190         SecureRandom sr = SecureRandom.getInstance("someRandom", "SRProvider");
    191         if (sr.getProvider() != p || !"someRandom".equals(sr.getAlgorithm())) {
    192             fail("getInstance failed");
    193         }
    194 
    195         try {
    196             SecureRandom r = SecureRandom.getInstance("anotherRandom", "SRProvider");
    197             fail("expected NoSuchAlgorithmException");
    198         } catch (NoSuchAlgorithmException e) {
    199             // ok
    200         } catch (NoSuchProviderException e) {
    201             fail("unexpected: " + e);
    202         } catch (IllegalArgumentException e) {
    203             fail("unexpected: " + e);
    204         } catch (NullPointerException e) {
    205             fail("unexpected: " + e);
    206         }
    207 
    208         try {
    209             SecureRandom r = SecureRandom.getInstance("someRandom", "UnknownProvider");
    210             fail("expected NoSuchProviderException");
    211         } catch (NoSuchProviderException e) {
    212             // ok
    213         } catch (NoSuchAlgorithmException e) {
    214             fail("unexpected: " + e);
    215         } catch (IllegalArgumentException e) {
    216             fail("unexpected: " + e);
    217         } catch (NullPointerException e) {
    218             fail("unexpected: " + e);
    219         }
    220 
    221         try {
    222             SecureRandom r = SecureRandom.getInstance("someRandom", (String)null);
    223             fail("expected IllegalArgumentException");
    224         } catch (NoSuchProviderException e) {
    225             fail("unexpected: " + e);
    226         } catch (NoSuchAlgorithmException e) {
    227             fail("unexpected: " + e);
    228         } catch (IllegalArgumentException e) {
    229             // ok
    230         } catch (NullPointerException e) {
    231             fail("unexpected: " + e);
    232         }
    233 
    234         try {
    235             SecureRandom r = SecureRandom.getInstance(null, "SRProvider");
    236             fail("expected NullPointerException");
    237         } catch (NoSuchProviderException e) {
    238             fail("unexpected: " + e);
    239         } catch (NoSuchAlgorithmException e) {
    240             fail("unexpected: " + e);
    241         } catch (IllegalArgumentException e) {
    242             fail("unexpected: " + e);
    243         } catch (NullPointerException e) {
    244             // ok
    245         }
    246     }
    247 
    248     /*
    249      * Class under test for SecureRandom getInstance(String, Provider)
    250      */
    251     @TestTargetNew(
    252         level = TestLevel.COMPLETE,
    253         notes = "",
    254         method = "getInstance",
    255         args = {java.lang.String.class, java.security.Provider.class}
    256     )
    257     public final void testGetInstanceStringProvider() throws Exception {
    258         Provider p = new SRProvider();
    259         SecureRandom sr = SecureRandom.getInstance("someRandom", p);
    260         if (sr.getProvider() != p || !"someRandom".equals(sr.getAlgorithm())) {
    261             fail("getInstance failed");
    262         }
    263 
    264         try {
    265             SecureRandom r = SecureRandom.getInstance("unknownRandom", p);
    266             fail("expected NoSuchAlgorithmException");
    267         } catch (NoSuchAlgorithmException e) {
    268             // ok
    269         } catch (IllegalArgumentException e) {
    270             fail("unexpected: " + e);
    271         } catch (NullPointerException e) {
    272             fail("unexpected: " + e);
    273         }
    274 
    275 
    276         try {
    277             SecureRandom r = SecureRandom.getInstance(null, p);
    278             fail("expected NullPointerException");
    279         } catch (NoSuchAlgorithmException e) {
    280             fail("unexpected: " + e);
    281         } catch (IllegalArgumentException e) {
    282             fail("unexpected: " + e);
    283         } catch (NullPointerException e) {
    284             // ok
    285         }
    286 
    287         try {
    288             SecureRandom r = SecureRandom.getInstance("anyRandom", (Provider)null);
    289             fail("expected IllegalArgumentException");
    290         } catch (NoSuchAlgorithmException e) {
    291             fail("unexpected: " + e);
    292         } catch (IllegalArgumentException e) {
    293             // ok
    294         } catch (NullPointerException e) {
    295             fail("unexpected: " + e);
    296         }
    297 
    298 
    299     }
    300 
    301     /*
    302      * Class under test for void setSeed(byte[])
    303      */
    304     @TestTargetNew(
    305         level = TestLevel.PARTIAL_COMPLETE,
    306         notes = "Verification with null parameter missed",
    307         method = "setSeed",
    308         args = {byte[].class}
    309     )
    310     public final void testSetSeedbyteArray() {
    311         byte[] b = {1,2,3};
    312         SecureRandom sr = new SecureRandom();
    313         sr.setSeed(b);
    314         if (!RandomImpl.runEngineSetSeed) {
    315             fail("setSeed failed");
    316         }
    317 
    318     }
    319 
    320     @TestTargetNew(
    321         level = TestLevel.PARTIAL_COMPLETE,
    322         notes = "Verification with invalid parameter missed",
    323         method = "getSeed",
    324         args = {int.class}
    325     )
    326     public final void testGetSeed() {
    327         byte[] b = SecureRandom.getSeed(4);
    328         if( b.length != 4) {
    329             fail("getSeed failed");
    330         }
    331     }
    332 
    333     @TestTargetNew(
    334         level = TestLevel.PARTIAL_COMPLETE,
    335         notes = "Verification with invalid parameter missed",
    336         method = "generateSeed",
    337         args = {int.class}
    338     )
    339     public final void testGenerateSeed() {
    340         SecureRandom sr = new SecureRandom();
    341         byte[] b = sr.generateSeed(4);
    342         for (int i = 0; i < b.length; i++) {
    343             if (b[i] != (byte)i) {
    344                 fail("generateSeed failed");
    345             }
    346         }
    347     }
    348 
    349 
    350 
    351     public class SRProvider extends Provider {
    352         public SRProvider() {
    353             super("SRProvider", 1.0, "SRProvider for testing");
    354             put("SecureRandom.someRandom",
    355                     "org.apache.harmony.security.tests.support.RandomImpl");
    356         }
    357     }
    358 
    359     class MySecureRandom extends SecureRandom {
    360         public MySecureRandom(){
    361             super();
    362         }
    363 
    364         public int nextElement(int numBits) {
    365             return super.next(numBits);
    366         }
    367     }
    368 }
    369