1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 package org.apache.hadoop.crypto.random; 19 20 import java.io.IOException; 21 import java.util.Arrays; 22 23 import org.apache.commons.lang.SystemUtils; 24 import org.apache.hadoop.conf.Configuration; 25 import org.apache.hadoop.util.Shell.ShellCommandExecutor; 26 import org.junit.Assume; 27 import org.junit.Test; 28 29 public class TestOsSecureRandom { 30 getOsSecureRandom()31 private static OsSecureRandom getOsSecureRandom() throws IOException { 32 Assume.assumeTrue(SystemUtils.IS_OS_LINUX); 33 OsSecureRandom random = new OsSecureRandom(); 34 random.setConf(new Configuration()); 35 return random; 36 } 37 38 @Test(timeout=120000) testRandomBytes()39 public void testRandomBytes() throws Exception { 40 OsSecureRandom random = getOsSecureRandom(); 41 // len = 16 42 checkRandomBytes(random, 16); 43 // len = 32 44 checkRandomBytes(random, 32); 45 // len = 128 46 checkRandomBytes(random, 128); 47 // len = 256 48 checkRandomBytes(random, 256); 49 random.close(); 50 } 51 52 /** 53 * Test will timeout if secure random implementation always returns a 54 * constant value. 55 */ checkRandomBytes(OsSecureRandom random, int len)56 private void checkRandomBytes(OsSecureRandom random, int len) { 57 byte[] bytes = new byte[len]; 58 byte[] bytes1 = new byte[len]; 59 random.nextBytes(bytes); 60 random.nextBytes(bytes1); 61 62 while (Arrays.equals(bytes, bytes1)) { 63 random.nextBytes(bytes1); 64 } 65 } 66 67 /** 68 * Test will timeout if secure random implementation always returns a 69 * constant value. 70 */ 71 @Test(timeout=120000) testRandomInt()72 public void testRandomInt() throws Exception { 73 OsSecureRandom random = getOsSecureRandom(); 74 75 int rand1 = random.nextInt(); 76 int rand2 = random.nextInt(); 77 while (rand1 == rand2) { 78 rand2 = random.nextInt(); 79 } 80 random.close(); 81 } 82 83 /** 84 * Test will timeout if secure random implementation always returns a 85 * constant value. 86 */ 87 @Test(timeout=120000) testRandomLong()88 public void testRandomLong() throws Exception { 89 OsSecureRandom random = getOsSecureRandom(); 90 91 long rand1 = random.nextLong(); 92 long rand2 = random.nextLong(); 93 while (rand1 == rand2) { 94 rand2 = random.nextLong(); 95 } 96 random.close(); 97 } 98 99 /** 100 * Test will timeout if secure random implementation always returns a 101 * constant value. 102 */ 103 @Test(timeout=120000) testRandomFloat()104 public void testRandomFloat() throws Exception { 105 OsSecureRandom random = getOsSecureRandom(); 106 107 float rand1 = random.nextFloat(); 108 float rand2 = random.nextFloat(); 109 while (rand1 == rand2) { 110 rand2 = random.nextFloat(); 111 } 112 random.close(); 113 } 114 115 /** 116 * Test will timeout if secure random implementation always returns a 117 * constant value. 118 */ 119 @Test(timeout=120000) testRandomDouble()120 public void testRandomDouble() throws Exception { 121 OsSecureRandom random = getOsSecureRandom(); 122 123 double rand1 = random.nextDouble(); 124 double rand2 = random.nextDouble(); 125 while (rand1 == rand2) { 126 rand2 = random.nextDouble(); 127 } 128 random.close(); 129 } 130 131 @Test(timeout=120000) testRefillReservoir()132 public void testRefillReservoir() throws Exception { 133 OsSecureRandom random = getOsSecureRandom(); 134 135 for (int i = 0; i < 8196; i++) { 136 random.nextLong(); 137 } 138 random.close(); 139 } 140 } 141