/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache license, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the license for the specific language governing permissions and
 * limitations under the license.
 */
package org.apache.logging.log4j.core.net.ssl;

import java.io.IOException;
import java.io.OutputStream;

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

import org.junit.Assert;
import org.junit.Test;

public class SslConfigurationTest {
    private static final String TLS_TEST_HOST = "login.yahoo.com";
    private static final int TLS_TEST_PORT = 443;

    @Test
    public void emptyConfigurationDoesntCauseNullSSLSocketFactory() {
        final SslConfiguration sc = SslConfiguration.createSSLConfiguration(null, null, null);
        final SSLSocketFactory factory = sc.getSslSocketFactory();
        Assert.assertNotNull(factory);
    }

    @Test
    public void emptyConfigurationHasDefaultTrustStore() throws IOException {
        final SslConfiguration sc = SslConfiguration.createSSLConfiguration(null, null, null);
        final SSLSocketFactory factory = sc.getSslSocketFactory();
        try (final SSLSocket clientSocket = (SSLSocket) factory.createSocket(TLS_TEST_HOST, TLS_TEST_PORT)) {
            Assert.assertNotNull(clientSocket);
            clientSocket.close();
        }
    }

    @Test
    public void connectionFailsWithoutValidServerCertificate() throws IOException, StoreConfigurationException {
        final TrustStoreConfiguration tsc = new TrustStoreConfiguration(TestConstants.TRUSTSTORE_FILE, null, null, null);
        final SslConfiguration sc = SslConfiguration.createSSLConfiguration(null, null, tsc);
        final SSLSocketFactory factory = sc.getSslSocketFactory();
        try (final SSLSocket clientSocket = (SSLSocket) factory.createSocket(TLS_TEST_HOST, TLS_TEST_PORT)) {
            try (final OutputStream os = clientSocket.getOutputStream()) {
                os.write("GET config/login_verify2?".getBytes());
                Assert.fail("Expected IOException");
            } catch (final IOException e) {
                // Expected, do nothing.
            }
        }
    }

    @Test
    public void loadKeyStoreWithoutPassword() throws StoreConfigurationException {
        final KeyStoreConfiguration ksc = new KeyStoreConfiguration(TestConstants.KEYSTORE_FILE, null, null, null);
        final SslConfiguration sslConf = SslConfiguration.createSSLConfiguration(null, ksc, null);
        final SSLSocketFactory factory = sslConf.getSslSocketFactory();
        Assert.assertNotNull(factory);
    }
}

@@ -898,16 +898,54 @@
 tStream;
+%0Aimport java.net.UnknownHostException;
 %0A%0Aimport
@@ -1743,32 +1743,50 @@
 ocketFactory();%0A
+        try %7B%0A    
         try (fin
@@ -1883,32 +1883,36 @@
 ) %7B%0A            
+    
 Assert.assertNot
@@ -1943,16 +1943,20 @@
         
+    
 clientSo
@@ -1965,24 +1965,151 @@
 et.close();%0A
+            %7D%0A        %7D catch (UnknownHostException offline) %7B%0A            // this exception is thrown on Windows when offline%0A
         %7D%0A  
@@ -2511,32 +2511,50 @@
 ocketFactory();%0A
+        try %7B%0A    
         try (fin
@@ -2639,32 +2639,36 @@
 S_TEST_PORT)) %7B%0A
+    
             try 
@@ -2742,16 +2742,20 @@
         
+    
 os.write
@@ -2808,24 +2808,28 @@
             
+    
 Assert.fail(
@@ -2857,32 +2857,36 @@
 %22);%0A            
+   
+ 
 %7D catch (final I
@@ -2897,24 +2897,28 @@
 eption e) %7B%0A
+    
             
@@ -2950,33 +2950,164 @@
 ng.%0A            
-%7D
+    %7D%0A            %7D%0A        %7D catch (UnknownHostException offline) %7B%0A            // this exception is thrown on Windows when offline
 %0A        %7D%0A    %7D

Allow SslConfigurationTest to pass when offline and DNS host name lookup fails.