package org.bouncycastle.crypto.tls.test;

import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.bouncycastle.crypto.tls.AlwaysValidVerifyer;
import org.bouncycastle.crypto.tls.TlsProtocolHandler;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.encoders.Hex;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class BasicTlsTest
    extends TestCase
{
    private static final int PORT_NO = 8003;
    private static final String CLIENT = "client";
    private static final char[] CLIENT_PASSWORD = "clientPassword".toCharArray();
    private static final char[] SERVER_PASSWORD = "serverPassword".toCharArray();
    private static final char[] TRUST_STORE_PASSWORD = "trustPassword".toCharArray();

    public void testConnection()
        throws Exception
    {
        Thread server = new HTTPSServerThread();

        server.start();

        Thread.yield();
        
        AlwaysValidVerifyer verifyer = new AlwaysValidVerifyer();
        Socket s = null;

        for (int i = 0; s == null && i != 3; i++)
        {
            Thread.sleep(1000);

            try
            {
                s = new Socket("localhost", PORT_NO);
            }
            catch (IOException e)
            {
                // ignore
            }
        }

        if (s == null)
        {
            throw new IOException("unable to connect");
        }
        
        long time = System.currentTimeMillis();
        TlsProtocolHandler handler = new TlsProtocolHandler(s.getInputStream(), s.getOutputStream());
        handler.connect(verifyer);
        InputStream is = handler.getTlsInputStream();
        OutputStream os = handler.getTlsOuputStream();

        os.write("GET / HTTP/1.1\r\n\r\n".getBytes());

        time = System.currentTimeMillis();
        byte[] buf = new byte[4096];
        int read = 0;
        int total = 0;

        while ((read = is.read(buf, total, buf.length - total)) > 0)
        {
            total += read;
        }

        is.close();

        byte[] expected = Hex.decode("485454502f312e3120323030204f4b0d0a436f6e74656e742d547970653a20746578742f68"
             + "746d6c0d0a0d0a3c68746d6c3e0d0a3c626f64793e0d0a48656c6c6f20576f726c64210d0a3c2f626f64793e0d0a3c2f"
             + "68746d6c3e0d0a");
       assertEquals(total, expected.length);

        byte[] tmp = new byte[expected.length];
        System.arraycopy(buf, 0, tmp, 0, total);
        assertTrue(Arrays.areEqual(expected, tmp));
    }

    public static TestSuite suite()
    {
        return new TestSuite(BasicTlsTest.class);
    }

    public static void main (String[] args)
        throws Exception
    {
        junit.textui.TestRunner.run(suite());
    }
}

@@ -499,16 +499,18 @@
 = 8003;%0A
+//
     priv
@@ -552,16 +552,18 @@
 lient%22;%0A
+//
     priv
@@ -628,32 +628,34 @@
 .toCharArray();%0A
+//
     private stat
@@ -716,24 +716,26 @@
 harArray();%0A
+//
     private 
@@ -1463,32 +1463,34 @@
      %7D%0A        %0A
+//
         long tim
@@ -1694,19 +1694,16 @@
 dler.get
-Tls
 InputStr
@@ -1750,13 +1750,11 @@
 .get
-Tls
 Ou
+t
 putS
@@ -1815,24 +1815,26 @@
 tBytes());%0A%0A
+//
         time

Fix warnings