forked from bcgit/bc-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a basic DTLS server test, for use with an external client
- Loading branch information
1 parent
8c28e1d
commit f4f2a35
Showing
2 changed files
with
79 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
core/src/test/java/org/bouncycastle/crypto/tls/test/DTLSServerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package org.bouncycastle.crypto.tls.test; | ||
|
||
import java.net.DatagramPacket; | ||
import java.net.DatagramSocket; | ||
import java.net.InetAddress; | ||
import java.net.SocketTimeoutException; | ||
import java.security.SecureRandom; | ||
|
||
import org.bouncycastle.crypto.tls.DTLSServerProtocol; | ||
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 client. | ||
* <p/> | ||
* Please refer to GnuTLSSetup.txt or OpenSSLSetup.txt, and x509-*.pem files in this package for | ||
* help configuring an external DTLS client. | ||
*/ | ||
public class DTLSServerTest | ||
{ | ||
public static void main(String[] args) | ||
throws Exception | ||
{ | ||
InetAddress address = InetAddress.getLocalHost(); | ||
int port = 5556; | ||
|
||
int mtu = 1500; | ||
|
||
SecureRandom secureRandom = new SecureRandom(); | ||
|
||
DTLSServerProtocol serverProtocol = new DTLSServerProtocol(secureRandom); | ||
|
||
byte[] data = new byte[mtu]; | ||
DatagramPacket packet = new DatagramPacket(data, mtu); | ||
|
||
DatagramSocket socket = new DatagramSocket(port, address); | ||
socket.receive(packet); | ||
|
||
System.out.println("Accepting connection from " + packet.getAddress().getHostAddress() + ":" + port); | ||
socket.connect(packet.getAddress(), packet.getPort()); | ||
|
||
/* | ||
* NOTE: For simplicity, and since we don't yet have HelloVerifyRequest support, we just | ||
* discard the initial packet, which the client should re-send anyway. | ||
*/ | ||
|
||
DatagramTransport transport = new UDPTransport(socket, mtu); | ||
|
||
// Uncomment to see packets | ||
// transport = new LoggingDatagramTransport(transport, System.out); | ||
|
||
MockDTLSServer server = new MockDTLSServer(); | ||
DTLSTransport dtlsServer = serverProtocol.accept(server, transport); | ||
|
||
byte[] buf = new byte[dtlsServer.getReceiveLimit()]; | ||
|
||
while (!socket.isClosed()) | ||
{ | ||
try | ||
{ | ||
int length = dtlsServer.receive(buf, 0, buf.length, 60000); | ||
if (length >= 0) | ||
{ | ||
System.out.write(buf, 0, length); | ||
dtlsServer.send(buf, 0, length); | ||
} | ||
} | ||
catch (SocketTimeoutException ste) | ||
{ | ||
} | ||
} | ||
|
||
dtlsServer.close(); | ||
} | ||
} |