/*
 * Copyright 2019 Mitsunori Komatsu (komamitsu)
 *
 * Licensed 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.komamitsu.fluency.fluentd.ingester.sender;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.komamitsu.fluency.fluentd.SSLTestServerSocketFactory;

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

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.jupiter.api.Assertions.assertEquals;

class SSLSocketBuilderTest
{
    private SSLServerSocket serverSocket;

    @BeforeEach
    void setUp()
            throws IOException, CertificateException, NoSuchAlgorithmException, UnrecoverableKeyException, KeyStoreException, KeyManagementException
    {
        serverSocket = new SSLTestServerSocketFactory().create();
    }

    @AfterEach
    void tearDown()
            throws IOException
    {
        if (serverSocket != null) {
            serverSocket.close();
        }
    }

    @Test
    void testWithServer()
            throws IOException, InterruptedException, ExecutionException, TimeoutException
    {
        final AtomicInteger readLen = new AtomicInteger();
        final byte[] buf = new byte[256];

        ExecutorService executorService = Executors.newSingleThreadExecutor();
        Future<Void> future = executorService.submit(new Callable<Void>()
        {
            @Override
            public Void call()
                    throws Exception
            {
                Socket clientSocket = serverSocket.accept();
                readLen.set(clientSocket.getInputStream().read(buf));
                return null;
            }
        });

        SSLSocket sslSocket = new SSLSocketBuilder("localhost", serverSocket.getLocalPort(), 5000, 5000, SSLSocketFactory.getDefault()).build();

        try {
            OutputStream outputStream = sslSocket.getOutputStream();
            outputStream.write("hello".getBytes("ASCII"));
            outputStream.flush();
        }
        finally {
            sslSocket.close();
        }

        future.get(10, TimeUnit.SECONDS);
        executorService.shutdown();
        executorService.awaitTermination(10, TimeUnit.SECONDS);

        assertEquals(new String(buf, 0, readLen.get(), "ASCII"), "hello");
    }
}

@@ -1666,16 +1666,107 @@
 tEquals;
+%0Aimport static org.komamitsu.fluency.fluentd.SSLTestClientSocketFactory.SSL_SOCKET_FACTORY;
 %0A%0Aclass 
@@ -3052,34 +3052,23 @@
  SSL
-SocketFactory.getDefault()
+_SOCKET_FACTORY
 ).bu

Use custom ssl socket factory in SSLSocketBuilderTest