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 */
     21 
     22 package java.security;
     23 
     24 import org.apache.harmony.security.tests.support.RandomImpl;
     25 
     26 import junit.framework.TestCase;
     27 
     28 /**
     29  * Tests for <code>SecureRandom</code> constructor and methods
     30  *
     31  */
     32 public class SecureRandomTest extends TestCase {
     33 
     34 	/**
     35 	 * SRProvider
     36 	 */
     37 	Provider p;
     38 
     39 	/*
     40 	 * @see TestCase#setUp()
     41 	 */
     42 	protected void setUp() throws Exception {
     43 		super.setUp();
     44 		p = new SRProvider();
     45 		Security.insertProviderAt(p, 1);
     46 	}
     47 
     48 	/*
     49 	 * @see TestCase#tearDown()
     50 	 */
     51 	protected void tearDown() throws Exception {
     52 		super.tearDown();
     53 		Security.removeProvider(p.getName());
     54 	}
     55 
     56 	public final void testNext() {
     57 		SecureRandom sr = new SecureRandom();
     58 		if (sr.next(1) != 1 || sr.next(2) != 3 || sr.next(3) != 7) {
     59 			fail("next failed");
     60 		}
     61 	}
     62 
     63 	/*
     64 	 * Class under test for void setSeed(long)
     65 	 */
     66 	public final void testSetSeedlong() {
     67 		SecureRandom sr = new SecureRandom();
     68 		sr.setSeed(12345);
     69 		if (!RandomImpl.runEngineSetSeed) {
     70 			fail("setSeed failed");
     71 		}
     72 	}
     73 
     74 	public final void testNextBytes() {
     75 		byte[] b = new byte[5];
     76 		SecureRandom sr = new SecureRandom();
     77 		sr.nextBytes(b);
     78 		for (int i = 0; i < b.length; i++) {
     79 			if (b[i] != (byte)(i + 0xF1)) {
     80 				fail("nextBytes failed");
     81 			}
     82 		}
     83 	}
     84 
     85 	/*
     86 	 * Class under test for void SecureRandom()
     87 	 */
     88 	public final void testSecureRandom() {
     89 		SecureRandom sr = new SecureRandom();
     90 		if (!sr.getAlgorithm().equals("someRandom")  ||
     91 				sr.getProvider()!= p) {
     92 			fail("incorrect SecureRandom implementation" + p.getName());
     93 		}
     94 	}
     95 
     96 	/*
     97 	 * Class under test for void SecureRandom(byte[])
     98 	 */
     99 	public final void testSecureRandombyteArray() {
    100 		byte[] b = {1,2,3};
    101 		new SecureRandom(b);
    102 		if (!RandomImpl.runEngineSetSeed) {
    103 			fail("No setSeed");
    104 		}
    105 	}
    106 
    107 	/*
    108 	 * Class under test for SecureRandom getInstance(String)
    109 	 */
    110 	public final void testGetInstanceString() {
    111 		SecureRandom sr = null;
    112 		try {
    113 			sr = SecureRandom.getInstance("someRandom");
    114 		} catch (NoSuchAlgorithmException e) {
    115 			fail(e.toString());
    116 		}
    117 		if (sr.getProvider() != p || !"someRandom".equals(sr.getAlgorithm())) {
    118 			fail("getInstance failed");
    119 		}
    120 	}
    121 
    122 	/*
    123 	 * Class under test for SecureRandom getInstance(String, String)
    124 	 */
    125 	public final void testGetInstanceStringString() throws Exception {
    126 		SecureRandom sr = SecureRandom.getInstance("someRandom", "SRProvider");
    127 		if (sr.getProvider() != p || !"someRandom".equals(sr.getAlgorithm())) {
    128 			fail("getInstance failed");
    129 		}
    130 	}
    131 
    132 	/*
    133 	 * Class under test for SecureRandom getInstance(String, Provider)
    134 	 */
    135 	public final void testGetInstanceStringProvider() throws Exception {
    136 		Provider p = new SRProvider();
    137         SecureRandom sr = SecureRandom.getInstance("someRandom", p);
    138         if (sr.getProvider() != p || !"someRandom".equals(sr.getAlgorithm())) {
    139             fail("getInstance failed");
    140         }
    141 	}
    142 
    143 	/*
    144 	 * Class under test for void setSeed(byte[])
    145 	 */
    146 	public final void testSetSeedbyteArray() {
    147 		byte[] b = {1,2,3};
    148 		SecureRandom sr = new SecureRandom();
    149 		sr.setSeed(b);
    150 		if (!RandomImpl.runEngineSetSeed) {
    151 			fail("setSeed failed");
    152 		}
    153 	}
    154 
    155 	public final void testGetSeed() {
    156 		byte[] b = SecureRandom.getSeed(4);
    157 		if( b.length != 4) {
    158 			fail("getSeed failed");
    159 		}
    160 	}
    161 
    162 	public final void testGenerateSeed() {
    163 		SecureRandom sr = new SecureRandom();
    164 		byte[] b = sr.generateSeed(4);
    165 		for (int i = 0; i < b.length; i++) {
    166 			if (b[i] != (byte)i) {
    167 				fail("generateSeed failed");
    168 			}
    169 		}
    170 	}
    171 
    172 	class SRProvider extends Provider {
    173         public SRProvider() {
    174             super("SRProvider", 1.0, "SRProvider for testing");
    175             put("SecureRandom.someRandom",
    176                     "org.apache.harmony.security.tests.support.RandomImpl");
    177         }
    178 	}
    179 }
    180