View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
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   * Unit tests for {@link SSLContextFactoryBean}.
30   *
31   * @author Carl Harris
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          // should be able to create a context with no configuration at all
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          // it's important that each configured component output an appropriate
90          // informational message to the context; i.e. this logging is not just
91          // for programmers, it's there for systems administrators to use in
92          // verifying that SSL is configured properly
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 }