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 22package org.apache.harmony.security.tests.java.security; 23 24import java.security.NoSuchAlgorithmException; 25import java.security.Provider; 26import java.security.SecureRandom; 27import java.security.Security; 28 29import junit.framework.TestCase; 30 31/** 32 * 33 * Tests for internal Secure Random implementation based on Random 34 * 35 */ 36public class SecureRandom_ImplTest extends TestCase { 37 38 /** 39 * Registered providers 40 */ 41 Provider providers[] = Security.getProviders(); 42 43 /* 44 * @see TestCase#setUp() 45 */ 46 protected void setUp() throws Exception { 47 super.setUp(); 48 // remove all registered providers 49 for (int i = 0; i < providers.length; i++) { 50 Security.removeProvider(providers[i].getName()); 51 } 52 } 53 54 /* 55 * @see TestCase#tearDown() 56 */ 57 protected void tearDown() throws Exception { 58 super.tearDown(); 59 // restore all registered providers 60 for (int i = 0; i < providers.length; i++) { 61 Security.addProvider(providers[i]); 62 } 63 } 64 65 /* 66 * Class under test for void setSeed(long) 67 */ 68 public final void testSetSeedlong() { 69 SecureRandom sr = new SecureRandom(); 70 sr.setSeed(0); 71 sr.setSeed(-1); 72 sr.setSeed(11111111111L); 73 } 74 75 public final void testNextBytes() { 76 SecureRandom sr = new SecureRandom(); 77 sr.nextBytes(new byte[20]); 78 sr.nextBytes(new byte[1]); 79 80 //Not specified behavior: throws NullPointerException if bytes is null 81 try { 82 sr.nextBytes(null); 83 } catch (NullPointerException e) { 84 } 85 sr.nextBytes(new byte[5]); 86 } 87 88 /* 89 * Class under test for SecureRandom getInstance(String) 90 */ 91 public final void testGetInstanceString() { 92 try { 93 SecureRandom.getInstance("SHA1PRNG"); 94 fail("No expected NoSuchAlgorithmException"); 95 } catch (NoSuchAlgorithmException e) { 96 } 97 } 98 99 public final void testGetProvider() { 100 assertNull("Non null provider", new SecureRandom().getProvider()); 101 } 102 103 public final void testGetAlgorithm() { 104 //test default implementation 105 SecureRandom sr = new SecureRandom(); 106 assertEquals("Incorrect algorithm", "SHA1PRNG", sr.getAlgorithm()); 107 assertNull("Incorrect provider", sr.getProvider()); 108 109 //just in case 110 sr.nextBytes(new byte[100]); 111 } 112 113 /* 114 * Class under test for void setSeed(byte[]) 115 */ 116 public final void testSetSeedbyteArray() { 117 SecureRandom sr = new SecureRandom(); 118 119 //Not specified behavior: throws NullPointerException if bytes is null 120 try { 121 sr.setSeed(null); 122 } catch (NullPointerException e) { 123 } 124 125 byte[] seed = {1,2,3,4}; 126 sr.setSeed(seed); 127 128 byte[] seed1 = {1,2,3,4, -2, 100, 9, 111}; 129 sr.setSeed(seed1); 130 } 131 132 public final void testGetSeed() { 133 byte[] seed = SecureRandom.getSeed(5); 134 new SecureRandom(seed).nextBytes(new byte[20]); 135 } 136 137 public final void testGenerateSeed() { 138 SecureRandom sr = new SecureRandom(); 139 byte[] seed = sr.generateSeed(5); 140 new SecureRandom(seed).nextBytes(new byte[20]); 141 } 142} 143