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.assertTrue;
017
018import java.util.Arrays;
019
020import org.junit.Before;
021import org.junit.Test;
022
023import ch.qos.logback.core.ContextBase;
024import ch.qos.logback.core.net.ssl.mock.MockSSLConfigurable;
025
026/**
027 * Unit tests for {@link SSLParametersConfiguration}.
028 *
029 * @author Carl Harris
030 */
031public class SSLParametersConfigurationTest {
032
033    private MockSSLConfigurable configurable = new MockSSLConfigurable();
034
035    private SSLParametersConfiguration configuration = new SSLParametersConfiguration();
036
037    @Before
038    public void setUp() throws Exception {
039        configuration.setContext(new ContextBase());
040    }
041
042    @Test
043    public void testSetIncludedProtocols() throws Exception {
044        configurable.setSupportedProtocols(new String[] { "A", "B", "C", "D" });
045        configuration.setIncludedProtocols("A,B ,C, D");
046        configuration.configure(configurable);
047        assertTrue(Arrays.equals(new String[] { "A", "B", "C", "D" }, configurable.getEnabledProtocols()));
048    }
049
050    @Test
051    public void testSetExcludedProtocols() throws Exception {
052        configurable.setSupportedProtocols(new String[] { "A", "B" });
053        configuration.setExcludedProtocols("A");
054        configuration.configure(configurable);
055        assertTrue(Arrays.equals(new String[] { "B" }, configurable.getEnabledProtocols()));
056    }
057
058    @Test
059    public void testSetIncludedAndExcludedProtocols() throws Exception {
060        configurable.setSupportedProtocols(new String[] { "A", "B", "C" });
061        configuration.setIncludedProtocols("A, B");
062        configuration.setExcludedProtocols("B");
063        configuration.configure(configurable);
064        assertTrue(Arrays.equals(new String[] { "A" }, configurable.getEnabledProtocols()));
065    }
066
067    @Test
068    public void testSetIncludedCipherSuites() throws Exception {
069        configurable.setSupportedCipherSuites(new String[] { "A", "B", "C", "D" });
070        configuration.setIncludedCipherSuites("A,B ,C, D");
071        configuration.configure(configurable);
072        assertTrue(Arrays.equals(new String[] { "A", "B", "C", "D" }, configurable.getEnabledCipherSuites()));
073    }
074
075    @Test
076    public void testSetExcludedCipherSuites() throws Exception {
077        configurable.setSupportedCipherSuites(new String[] { "A", "B" });
078        configuration.setExcludedCipherSuites("A");
079        configuration.configure(configurable);
080        assertTrue(Arrays.equals(new String[] { "B" }, configurable.getEnabledCipherSuites()));
081    }
082
083    @Test
084    public void testSetExcludedAndIncludedCipherSuites() throws Exception {
085        configurable.setSupportedCipherSuites(new String[] { "A", "B", "C" });
086        configuration.setIncludedCipherSuites("A, B");
087        configuration.setExcludedCipherSuites("B");
088        configuration.configure(configurable);
089        assertTrue(Arrays.equals(new String[] { "A" }, configurable.getEnabledCipherSuites()));
090    }
091
092    @Test
093    public void testSetNeedClientAuth() throws Exception {
094        configuration.setNeedClientAuth(true);
095        configuration.configure(configurable);
096        assertTrue(configurable.isNeedClientAuth());
097    }
098
099    @Test
100    public void testSetWantClientAuth() throws Exception {
101        configuration.setWantClientAuth(true);
102        configuration.configure(configurable);
103        assertTrue(configurable.isWantClientAuth());
104    }
105
106    @Test
107    public void testPassDefaultProtocols() throws Exception {
108        final String[] protocols = new String[] { "A" };
109        configurable.setDefaultProtocols(protocols);
110        configuration.configure(configurable);
111        assertTrue(Arrays.equals(protocols, configurable.getEnabledProtocols()));
112    }
113
114    @Test
115    public void testPassDefaultCipherSuites() throws Exception {
116        final String[] cipherSuites = new String[] { "A" };
117        configurable.setDefaultCipherSuites(cipherSuites);
118        configuration.configure(configurable);
119        assertTrue(Arrays.equals(cipherSuites, configurable.getEnabledCipherSuites()));
120    }
121
122    @Test
123    public void testPassDefaultNeedClientAuth() throws Exception {
124        configurable.setNeedClientAuth(true);
125        configuration.configure(configurable);
126        assertTrue(configurable.isNeedClientAuth());
127    }
128
129    @Test
130    public void testPassDefaultWantClientAuth() throws Exception {
131        configurable.setWantClientAuth(true);
132        configuration.configure(configurable);
133        assertTrue(configurable.isWantClientAuth());
134    }
135
136}