1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.net.ssl;
15
16 import java.security.NoSuchAlgorithmException;
17 import java.security.NoSuchProviderException;
18 import java.security.SecureRandom;
19
20 import org.junit.jupiter.api.Assertions;
21 import org.junit.jupiter.api.Test;
22
23
24
25
26
27
28 public class SecureRandomFactoryBeanTest {
29
30 private SecureRandomFactoryBean factoryBean = new SecureRandomFactoryBean();
31
32 @Test
33 public void testDefaults() throws Exception {
34 Assertions.assertNotNull(factoryBean.createSecureRandom());
35 }
36
37 @Test
38 public void testExplicitProvider() throws Exception {
39 SecureRandom secureRandom = SecureRandom.getInstance(SSL.DEFAULT_SECURE_RANDOM_ALGORITHM);
40 factoryBean.setProvider(secureRandom.getProvider().getName());
41 Assertions.assertNotNull(factoryBean.createSecureRandom());
42 }
43
44 @Test
45 public void testUnknownProvider() throws Exception {
46 factoryBean.setProvider(SSLTestConstants.FAKE_PROVIDER_NAME);
47 try {
48 factoryBean.createSecureRandom();
49 Assertions.fail("expected NoSuchProviderException");
50 } catch (NoSuchProviderException ex) {
51 Assertions.assertTrue(ex.getMessage().contains(SSLTestConstants.FAKE_PROVIDER_NAME));
52 }
53 }
54
55 @Test
56 public void testUnknownAlgorithm() throws Exception {
57 factoryBean.setAlgorithm(SSLTestConstants.FAKE_ALGORITHM_NAME);
58 try {
59 factoryBean.createSecureRandom();
60 Assertions.fail("expected NoSuchAlgorithmException");
61 } catch (NoSuchAlgorithmException ex) {
62 Assertions.assertTrue(ex.getMessage().contains(SSLTestConstants.FAKE_ALGORITHM_NAME));
63 }
64 }
65
66 }