1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.net.ssl;
15
16 import org.junit.jupiter.api.BeforeEach;
17 import org.junit.jupiter.api.Test;
18
19 import ch.qos.logback.core.net.ssl.mock.MockContextAware;
20 import ch.qos.logback.core.net.ssl.mock.MockKeyManagerFactoryFactoryBean;
21 import ch.qos.logback.core.net.ssl.mock.MockKeyStoreFactoryBean;
22 import ch.qos.logback.core.net.ssl.mock.MockSecureRandomFactoryBean;
23 import ch.qos.logback.core.net.ssl.mock.MockTrustManagerFactoryFactoryBean;
24
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27
28
29
30
31
32
33 public class SSLContextFactoryBeanTest {
34
35 private static final String SSL_CONFIGURATION_MESSAGE_PATTERN = "SSL protocol '.*?' provider '.*?'";
36
37 private static final String KEY_MANAGER_FACTORY_MESSAGE_PATTERN = "key manager algorithm '.*?' provider '.*?'";
38
39 private static final String TRUST_MANAGER_FACTORY_MESSAGE_PATTERN = "trust manager algorithm '.*?' provider '.*?'";
40
41 private static final String KEY_STORE_MESSAGE_PATTERN = "key store of type '.*?' provider '.*?': .*";
42
43 private static final String TRUST_STORE_MESSAGE_PATTERN = "trust store of type '.*?' provider '.*?': .*";
44
45 private static final String SECURE_RANDOM_MESSAGE_PATTERN = "secure random algorithm '.*?' provider '.*?'";
46
47 private MockKeyManagerFactoryFactoryBean keyManagerFactory = new MockKeyManagerFactoryFactoryBean();
48
49 private MockTrustManagerFactoryFactoryBean trustManagerFactory = new MockTrustManagerFactoryFactoryBean();
50
51 private MockKeyStoreFactoryBean keyStore = new MockKeyStoreFactoryBean();
52
53 private MockKeyStoreFactoryBean trustStore = new MockKeyStoreFactoryBean();
54
55 private MockSecureRandomFactoryBean secureRandom = new MockSecureRandomFactoryBean();
56
57 private MockContextAware context = new MockContextAware();
58 private SSLContextFactoryBean factoryBean = new SSLContextFactoryBean();
59
60 @BeforeEach
61 public void setUp() throws Exception {
62 keyStore.setLocation(SSLTestConstants.KEYSTORE_JKS_RESOURCE);
63 trustStore.setLocation(SSLTestConstants.KEYSTORE_JKS_RESOURCE);
64 }
65
66 @Test
67 public void testCreateDefaultContext() throws Exception {
68
69 assertNotNull(factoryBean.createContext(context));
70 assertTrue(context.hasInfoMatching(SSL_CONFIGURATION_MESSAGE_PATTERN));
71 }
72
73 @Test
74 public void testCreateContext() throws Exception {
75 factoryBean.setKeyManagerFactory(keyManagerFactory);
76 factoryBean.setKeyStore(keyStore);
77 factoryBean.setTrustManagerFactory(trustManagerFactory);
78 factoryBean.setTrustStore(trustStore);
79 factoryBean.setSecureRandom(secureRandom);
80
81 assertNotNull(factoryBean.createContext(context));
82
83 assertTrue(keyManagerFactory.isFactoryCreated());
84 assertTrue(trustManagerFactory.isFactoryCreated());
85 assertTrue(keyStore.isKeyStoreCreated());
86 assertTrue(trustStore.isKeyStoreCreated());
87 assertTrue(secureRandom.isSecureRandomCreated());
88
89
90
91
92
93
94 assertTrue(context.hasInfoMatching(SSL_CONFIGURATION_MESSAGE_PATTERN));
95 assertTrue(context.hasInfoMatching(KEY_MANAGER_FACTORY_MESSAGE_PATTERN));
96 assertTrue(context.hasInfoMatching(TRUST_MANAGER_FACTORY_MESSAGE_PATTERN));
97 assertTrue(context.hasInfoMatching(KEY_STORE_MESSAGE_PATTERN));
98 assertTrue(context.hasInfoMatching(TRUST_STORE_MESSAGE_PATTERN));
99 assertTrue(context.hasInfoMatching(SECURE_RANDOM_MESSAGE_PATTERN));
100 }
101
102 }