001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.net.ssl;
015
016import static org.junit.Assert.assertNotNull;
017import static org.junit.Assert.assertTrue;
018
019import org.junit.Before;
020import org.junit.Test;
021
022import ch.qos.logback.core.net.ssl.mock.MockContextAware;
023import ch.qos.logback.core.net.ssl.mock.MockKeyManagerFactoryFactoryBean;
024import ch.qos.logback.core.net.ssl.mock.MockKeyStoreFactoryBean;
025import ch.qos.logback.core.net.ssl.mock.MockSecureRandomFactoryBean;
026import ch.qos.logback.core.net.ssl.mock.MockTrustManagerFactoryFactoryBean;
027
028/**
029 * Unit tests for {@link SSLContextFactoryBean}.
030 *
031 * @author Carl Harris
032 */
033public class SSLContextFactoryBeanTest {
034
035    private static final String SSL_CONFIGURATION_MESSAGE_PATTERN = "SSL protocol '.*?' provider '.*?'";
036
037    private static final String KEY_MANAGER_FACTORY_MESSAGE_PATTERN = "key manager algorithm '.*?' provider '.*?'";
038
039    private static final String TRUST_MANAGER_FACTORY_MESSAGE_PATTERN = "trust manager algorithm '.*?' provider '.*?'";
040
041    private static final String KEY_STORE_MESSAGE_PATTERN = "key store of type '.*?' provider '.*?': .*";
042
043    private static final String TRUST_STORE_MESSAGE_PATTERN = "trust store of type '.*?' provider '.*?': .*";
044
045    private static final String SECURE_RANDOM_MESSAGE_PATTERN = "secure random algorithm '.*?' provider '.*?'";
046
047    private MockKeyManagerFactoryFactoryBean keyManagerFactory = new MockKeyManagerFactoryFactoryBean();
048
049    private MockTrustManagerFactoryFactoryBean trustManagerFactory = new MockTrustManagerFactoryFactoryBean();
050
051    private MockKeyStoreFactoryBean keyStore = new MockKeyStoreFactoryBean();
052
053    private MockKeyStoreFactoryBean trustStore = new MockKeyStoreFactoryBean();
054
055    private MockSecureRandomFactoryBean secureRandom = new MockSecureRandomFactoryBean();
056
057    private MockContextAware context = new MockContextAware();
058    private SSLContextFactoryBean factoryBean = new SSLContextFactoryBean();
059
060    @Before
061    public void setUp() throws Exception {
062        keyStore.setLocation(SSLTestConstants.KEYSTORE_JKS_RESOURCE);
063        trustStore.setLocation(SSLTestConstants.KEYSTORE_JKS_RESOURCE);
064    }
065
066    @Test
067    public void testCreateDefaultContext() throws Exception {
068        // should be able to create a context with no configuration at all
069        assertNotNull(factoryBean.createContext(context));
070        assertTrue(context.hasInfoMatching(SSL_CONFIGURATION_MESSAGE_PATTERN));
071    }
072
073    @Test
074    public void testCreateContext() throws Exception {
075        factoryBean.setKeyManagerFactory(keyManagerFactory);
076        factoryBean.setKeyStore(keyStore);
077        factoryBean.setTrustManagerFactory(trustManagerFactory);
078        factoryBean.setTrustStore(trustStore);
079        factoryBean.setSecureRandom(secureRandom);
080
081        assertNotNull(factoryBean.createContext(context));
082
083        assertTrue(keyManagerFactory.isFactoryCreated());
084        assertTrue(trustManagerFactory.isFactoryCreated());
085        assertTrue(keyStore.isKeyStoreCreated());
086        assertTrue(trustStore.isKeyStoreCreated());
087        assertTrue(secureRandom.isSecureRandomCreated());
088
089        // it's important that each configured component output an appropriate
090        // informational message to the context; i.e. this logging is not just
091        // for programmers, it's there for systems administrators to use in
092        // verifying that SSL is configured properly
093
094        assertTrue(context.hasInfoMatching(SSL_CONFIGURATION_MESSAGE_PATTERN));
095        assertTrue(context.hasInfoMatching(KEY_MANAGER_FACTORY_MESSAGE_PATTERN));
096        assertTrue(context.hasInfoMatching(TRUST_MANAGER_FACTORY_MESSAGE_PATTERN));
097        assertTrue(context.hasInfoMatching(KEY_STORE_MESSAGE_PATTERN));
098        assertTrue(context.hasInfoMatching(TRUST_STORE_MESSAGE_PATTERN));
099        assertTrue(context.hasInfoMatching(SECURE_RANDOM_MESSAGE_PATTERN));
100    }
101
102}