1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.crypto.random;
19
20 import java.lang.Thread.State;
21 import java.security.GeneralSecurityException;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.concurrent.TimeUnit;
26
27 import org.junit.jupiter.api.Test;
28 import org.junit.jupiter.api.Timeout;
29
30 public abstract class AbstractRandomTest {
31
32
33
34
35 private void checkRandomBytes(final CryptoRandom random, final int len) {
36 final byte[] bytes = new byte[len];
37 final byte[] bytes1 = new byte[len];
38 random.nextBytes(bytes);
39 random.nextBytes(bytes1);
40
41 while (Arrays.equals(bytes1, new byte[len]) || Arrays.equals(bytes, bytes1)) {
42 random.nextBytes(bytes1);
43 }
44 }
45
46 public abstract CryptoRandom getCryptoRandom() throws GeneralSecurityException;
47
48 @Test
49 @Timeout(value = 120000, unit = TimeUnit.MILLISECONDS)
50 public void testRandomBytes() throws Exception {
51 try (CryptoRandom random = getCryptoRandom()) {
52
53 checkRandomBytes(random, 16);
54
55 checkRandomBytes(random, 32);
56
57 checkRandomBytes(random, 128);
58
59 checkRandomBytes(random, 256);
60 }
61 }
62
63 @Test
64 @Timeout(value = 120000, unit = TimeUnit.MILLISECONDS)
65 public void testRandomBytesMultiThreaded() throws Exception {
66 final int threadCount = 100;
67 try (final CryptoRandom random = getCryptoRandom()) {
68 final List<Thread> threads = new ArrayList<>(threadCount);
69
70 for (int i = 0; i < threadCount; i++) {
71 final Thread t = new Thread(() -> {
72 checkRandomBytes(random, 10);
73 checkRandomBytes(random, 1000);
74 checkRandomBytes(random, 100000);
75 });
76 t.start();
77 threads.add(t);
78 }
79
80 for (final Thread t : threads) {
81 if (!t.getState().equals(State.NEW)) {
82 t.join();
83 }
84 }
85
86 }
87 }
88 }