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