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 static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23 import static org.junit.jupiter.api.Assumptions.assumeTrue;
24
25 import java.io.IOException;
26 import java.lang.reflect.InvocationTargetException;
27 import java.security.GeneralSecurityException;
28 import java.util.Properties;
29
30 import org.junit.jupiter.api.Test;
31
32 public class CryptoRandomFactoryTest {
33
34 @Test
35 public void testAbstractRandom() {
36 final Properties properties = new Properties();
37 properties.setProperty(CryptoRandomFactory.CLASSES_KEY, AbstractRandom.class.getName());
38 final Exception ex = assertThrows(GeneralSecurityException.class, () -> CryptoRandomFactory.getCryptoRandom(properties));
39 final String message = ex.getMessage();
40 assertTrue(message.contains("InstantiationException"), message);
41 }
42
43 @Test
44 public void testDefaultRandom() throws GeneralSecurityException, IOException {
45 final Properties properties = new Properties();
46 try (final CryptoRandom random = CryptoRandomFactory.getCryptoRandom(properties)) {
47 final String name = random.getClass().getName();
48 if (OpenSslCryptoRandom.isNativeCodeEnabled()) {
49 assertEquals(OpenSslCryptoRandom.class.getName(), name);
50 } else {
51 assertEquals(JavaCryptoRandom.class.getName(), name);
52 }
53 }
54 }
55
56 @Test
57 public void testDefaultRandomClass() throws GeneralSecurityException, IOException {
58 try (final CryptoRandom random = CryptoRandomFactory.getCryptoRandom()) {
59 assertEquals(OpenSslCryptoRandom.class.getName(), random.getClass().getName());
60 }
61 }
62
63 @Test
64 public void testDummmyRandom() {
65 final Properties properties = new Properties();
66 properties.setProperty(CryptoRandomFactory.CLASSES_KEY, NoopRandom.class.getName());
67 final Exception ex = assertThrows(GeneralSecurityException.class, () -> CryptoRandomFactory.getCryptoRandom(properties));
68 final String message = ex.getMessage();
69 assertTrue(message.contains("NoSuchMethodException"), message);
70 }
71
72 @Test
73 public void testEmpty() throws Exception {
74 final Properties properties = new Properties();
75 properties.setProperty(CryptoRandomFactory.CLASSES_KEY, "");
76 CryptoRandomFactory.getCryptoRandom(properties).close();
77 }
78
79 @Test
80 public void testFailingRandom() {
81 final Properties properties = new Properties();
82 properties.setProperty(CryptoRandomFactory.CLASSES_KEY, FailingRandom.class.getName());
83 final Exception ex = assertThrows(GeneralSecurityException.class, () -> CryptoRandomFactory.getCryptoRandom(properties));
84
85 Throwable cause = ex.getCause();
86 assertEquals(IllegalArgumentException.class, cause.getClass());
87 cause = cause.getCause();
88 assertEquals(InvocationTargetException.class, cause.getClass());
89 cause = cause.getCause();
90 assertEquals(UnsatisfiedLinkError.class, cause.getClass());
91 }
92
93 @Test
94 public void testFullClassName() throws GeneralSecurityException, IOException {
95 final Properties props = new Properties();
96 props.setProperty(CryptoRandomFactory.CLASSES_KEY, JavaCryptoRandom.class.getName());
97 try (final CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props)) {
98 assertEquals(JavaCryptoRandom.class.getName(), random.getClass().getName());
99 }
100 }
101
102 @Test
103 public void testGetOSRandom() throws GeneralSecurityException, IOException {
104
105 assumeTrue(!System.getProperty("os.name").contains("Windows"));
106 final Properties properties = new Properties();
107 properties.setProperty(CryptoRandomFactory.CLASSES_KEY, CryptoRandomFactory.RandomProvider.OS.getClassName());
108 try (final CryptoRandom random = CryptoRandomFactory.getCryptoRandom(properties)) {
109 assertEquals(OsCryptoRandom.class.getName(), random.getClass().getName());
110 }
111 }
112
113 @Test
114 public void testInvalidRandom() {
115 final Properties properties = new Properties();
116 properties.setProperty(CryptoRandomFactory.CLASSES_KEY, "InvalidCipherName");
117
118 assertThrows(GeneralSecurityException.class, () -> CryptoRandomFactory.getCryptoRandom(properties));
119 }
120
121 @Test
122 public void testInvalidRandomClass() throws GeneralSecurityException, IOException {
123 final Properties properties = new Properties();
124 properties.setProperty("org.apache.commons.crypto.cipher", "OpenSsl");
125 try (final CryptoRandom random = CryptoRandomFactory.getCryptoRandom(properties)) {
126 assertEquals(OpenSslCryptoRandom.class.getName(), random.getClass().getName());
127 }
128 }
129
130 @Test
131 public void testNoClasses() {
132 final Properties properties = new Properties();
133
134
135 properties.setProperty(CryptoRandomFactory.CLASSES_KEY, ",");
136 assertThrows(IllegalArgumentException.class, () -> CryptoRandomFactory.getCryptoRandom(properties));
137 }
138
139 @Test
140 public void testNull() {
141 assertThrows(NullPointerException.class, () -> CryptoRandomFactory.getCryptoRandom(null));
142 }
143
144 }