1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package tests.security; 17 18 import java.security.NoSuchAlgorithmException; 19 import java.security.SecureRandom; 20 import java.util.Arrays; 21 import java.util.concurrent.Callable; 22 import java.util.concurrent.ExecutorCompletionService; 23 import java.util.concurrent.ExecutorService; 24 import java.util.concurrent.Executors; 25 import junit.framework.TestCase; 26 27 public abstract class SecureRandomTest extends TestCase { 28 29 private final String algorithmName; 30 31 private int counter = 0; 32 SecureRandomTest(String name)33 protected SecureRandomTest(String name) { 34 this.algorithmName = name; 35 } 36 setUp()37 protected void setUp() throws Exception { 38 super.setUp(); 39 } 40 testSecureRandom()41 public void testSecureRandom() { 42 SecureRandom secureRandom1 = null; 43 try { 44 secureRandom1 = SecureRandom.getInstance(algorithmName); 45 } catch (NoSuchAlgorithmException e) { 46 fail(e.getMessage()); 47 } 48 49 SecureRandom secureRandom2 = null; 50 try { 51 secureRandom2 = SecureRandom.getInstance(algorithmName); 52 } catch (NoSuchAlgorithmException e) { 53 fail(e.getMessage()); 54 } 55 56 byte[] testRandom1 = getRandomBytes(secureRandom1); 57 byte[] testRandom2 = getRandomBytes(secureRandom2); 58 59 assertFalse(Arrays.equals(testRandom1, testRandom2)); 60 61 62 } 63 getRandomBytes(SecureRandom random)64 private byte[] getRandomBytes(SecureRandom random) { 65 byte[] randomData = new byte[64]; 66 67 random.setSeed(System.currentTimeMillis()+counter); 68 counter++; 69 70 random.nextBytes(randomData); 71 72 return randomData; 73 } 74 75 testSecureRandomThreadSafety()76 public void testSecureRandomThreadSafety() throws Exception { 77 final SecureRandom secureRandom = SecureRandom.getInstance(algorithmName); 78 int threads = 2; 79 ExecutorService executor = Executors.newFixedThreadPool(threads); 80 ExecutorCompletionService ecs = new ExecutorCompletionService(executor); 81 for (int t = 0; t < threads; t++) { 82 ecs.submit(new Callable<Void>() { 83 public Void call () { 84 for (int i = 0; i < 1000; i++) { 85 secureRandom.generateSeed(1024); 86 } 87 return null; 88 } 89 }); 90 } 91 executor.shutdown(); 92 for (int i = 0; i < threads; i++) { 93 ecs.take().get(); 94 } 95 } 96 } 97