package org.bouncycastle.crypto.tls.test;

import java.net.DatagramSocket;
import java.net.InetAddress;
import java.security.SecureRandom;

import org.bouncycastle.crypto.tls.DTLSClientProtocol;
import org.bouncycastle.crypto.tls.DTLSTransport;
import org.bouncycastle.crypto.tls.DatagramTransport;
import org.bouncycastle.crypto.tls.UDPTransport;

/**
 * A simple test designed to conduct a DTLS handshake with an external DTLS server.
 * <p/>
 * Please refer to GnuTLSSetup.txt or OpenSSLSetup.txt, and x509-*.pem files in this package for
 * help configuring an external DTLS server.
 */
public class DTLSClientTest
{

    public static void main(String[] args)
        throws Exception
    {

        SecureRandom secureRandom = new SecureRandom();

        DatagramSocket socket = new DatagramSocket();
        socket.connect(InetAddress.getLocalHost(), 5556);

        int mtu = 1500;
        DatagramTransport transport = new UDPTransport(socket, mtu);

        transport = new UnreliableDatagramTransport(transport, secureRandom, 0, 0);

        transport = new LoggingDatagramTransport(transport, System.out);

        DTLSClientProtocol protocol = new DTLSClientProtocol(secureRandom);

        MockDTLSClient client = new MockDTLSClient();
        DTLSTransport dtlsClient = protocol.connect(client, transport);

        System.out.println("Receive limit: " + dtlsClient.getReceiveLimit());
        System.out.println("Send limit: " + dtlsClient.getSendLimit());

        // Send and hopefully receive a packet back

        byte[] request = "Hello World!\n".getBytes("UTF-8");
        dtlsClient.send(request, 0, request.length);

        byte[] response = new byte[dtlsClient.getReceiveLimit()];
        int received = dtlsClient.receive(response, 0, response.length, 30000);
        if (received >= 0)
        {
            System.out.println(new String(response, 0, received, "UTF-8"));
        }

        dtlsClient.close();
    }
}

@@ -36,16 +36,44 @@
 .test;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -312,32 +312,125 @@
 agramTransport;%0A
+import org.bouncycastle.crypto.tls.TlsClient;%0Aimport org.bouncycastle.crypto.tls.TlsSession;%0A
 import org.bounc
@@ -734,16 +734,89 @@
 ntTest%0A%7B
+%0A    private static final SecureRandom secureRandom = new SecureRandom();
 %0A%0A    pu
@@ -883,25 +883,24 @@
 n%0A    %7B%0A
-%0A
         
 SecureRa
@@ -895,134 +895,30 @@
     
-SecureRandom secureRandom = new SecureRandom();%0A%0A        DatagramSocket socket = new DatagramSocket();%0A        socket.connect(
+InetAddress address = 
 Inet
@@ -943,17 +943,9 @@
 st()
-, 5556);%0A
+;
 %0A   
@@ -957,322 +957,86 @@
 int 
-mtu = 1500;%0A        DatagramTransport transport = new UDPTransport(socket, mtu);%0A%0A        transport = new UnreliableDatagramTransport(transport, secureRandom, 0, 0);%0A%0A        transport = new LoggingDatagramTransport(transport, System.out);%0A%0A        DTLSClientProtocol protocol = new DTLSClientProtocol(secureRandom
+port = 5556;%0A%0A        TlsSession session = null; //createSession(address, port
 );%0A%0A
@@ -1086,19 +1086,27 @@
 SClient(
+session
 );%0A
+%0A
         
@@ -1127,50 +1127,50 @@
 dtls
-Client = protocol.connect(client, transpor
+ = openDTLSConnection(address, port, clien
 t);%0A
@@ -1217,30 +1217,24 @@
 it: %22 + dtls
-Client
 .getReceiveL
@@ -1286,30 +1286,24 @@
 it: %22 + dtls
-Client
 .getSendLimi
@@ -1435,22 +1435,16 @@
     dtls
-Client
 .send(re
@@ -1510,22 +1510,16 @@
 yte%5Bdtls
-Client
 .getRece
@@ -1558,22 +1558,16 @@
 d = dtls
-Client
 .receive
@@ -1745,21 +1745,979 @@
 dtls
-Client
 .close(
+);%0A    %7D%0A%0A    private static TlsSession createSession(InetAddress address, int port)%0A        throws IOException%0A    %7B%0A        MockDTLSClient client = new MockDTLSClient(null);%0A        DTLSTransport dtls = openDTLSConnection(address, port, client);%0A        TlsSession session = client.getSessionToResume();%0A        dtls.close();%0A        return session;%0A    %7D%0A%0A    private static DTLSTransport openDTLSConnection(InetAddress address, int port, TlsClient client)%0A        throws IOException%0A    %7B%0A        DatagramSocket socket = new DatagramSocket();%0A        socket.connect(address, port);%0A%0A        int mtu = 1500;%0A        DatagramTransport transport = new UDPTransport(socket, mtu);%0A        transport = new UnreliableDatagramTransport(transport, secureRandom, 0, 0);%0A        transport = new LoggingDatagramTransport(transport, System.out);%0A%0A        DTLSClientProtocol protocol = new DTLSClientProtocol(secureRandom);%0A%0A        return protocol.connect(client, transport
 );%0A 

Refactor and spec out code for session-resumption test