package org.bouncycastle.crypto.tls.test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.SecureRandom;

import org.bouncycastle.crypto.tls.TlsServerProtocol;

/**
 * A simple test designed to conduct a TLS handshake with an external TLS client.
 * <p/>
 * Please refer to GnuTLSSetup.txt or OpenSSLSetup.txt, and x509-*.pem files in this package for
 * help configuring an external TLS client.
 */
public class TlsServerTest
{
    private static final SecureRandom secureRandom = new SecureRandom();

    public static void main(String[] args)
        throws Exception
    {
        InetAddress address = InetAddress.getLocalHost();
        int port = 5556;

        ServerSocket ss = new ServerSocket(port, 16, address);
        while (true)
        {
            Socket s = ss.accept();
            System.out.println("--------------------------------------------------------------------------------");
            System.out.println("Accepted " + s);
            ServerThread t = new ServerThread(s);
            t.start();
        }
    }

    static class ServerThread
        extends Thread
    {
        private final Socket s;

        ServerThread(Socket s)
        {
            this.s = s;
        }

        public void run()
        {
            try
            {
                long time1 = System.currentTimeMillis();

                MockTlsServer server = new MockTlsServer();
                TlsServerProtocol serverProtocol = new TlsServerProtocol(s.getInputStream(), s.getOutputStream(), secureRandom);
                serverProtocol.accept(server);
                // Streams.pipeAll(serverProtocol.getInputStream(), serverProtocol.getOutputStream());
                serverProtocol.close();

                long time2 = System.currentTimeMillis();
                System.out.println("Elapsed 1: " + (time2 - time1) + "ms");
            }
            catch (Exception e)
            {
                throw new RuntimeException(e);
            }
            finally
            {
                try
                {
                    s.close();
                }
                catch (IOException e)
                {
                }
                finally
                {
                }
            }
        }
    }
}

@@ -64,16 +64,45 @@
 eption;%0A
+import java.io.OutputStream;%0A
 import j
@@ -265,16 +265,106 @@
 rotocol;
+%0Aimport org.bouncycastle.util.io.Streams;%0Aimport org.bouncycastle.util.io.TeeOutputStream;
 %0A%0A/**%0A *
@@ -1480,66 +1480,8 @@
   %7B%0A
-                long time1 = System.currentTimeMillis();%0A%0A
     
@@ -1532,24 +1532,24 @@
 lsServer();%0A
+
             
@@ -1732,47 +1732,37 @@
     
-// Streams.pipeAll(serverProtocol.getIn
+OutputStream log = new TeeOut
 putS
@@ -1767,19 +1767,16 @@
 tStream(
-), 
 serverPr
@@ -1799,16 +1799,28 @@
 Stream()
+, System.out
 );%0A     
@@ -1834,163 +1834,100 @@
     
-serverProtocol.close();%0A%0A                long time2 = System.currentTimeMillis();%0A                System.out.println(%22Elapsed 1: %22 + (time2 - time1) + %22ms%22
+Streams.pipeAll(serverProtocol.getInputStream(), log);%0A                serverProtocol.close(
 );%0A 

Test server acts as an echo server and logs everything it echoes