diff options
Diffstat (limited to 'libjava/classpath/testsuite/java.net')
6 files changed, 314 insertions, 0 deletions
diff --git a/libjava/classpath/testsuite/java.net/DatagramSocketSendReceiveTest.java b/libjava/classpath/testsuite/java.net/DatagramSocketSendReceiveTest.java new file mode 100644 index 00000000000..b1614d23e05 --- /dev/null +++ b/libjava/classpath/testsuite/java.net/DatagramSocketSendReceiveTest.java @@ -0,0 +1,114 @@ +import java.net.*; + +/* + * Start one thread for receiving a packet, wait for it to set up, + * send a packet to it, and wait until it completes. Compare the + * packet to make sure it came thru without errors. + */ + +public class DatagramSocketSendReceiveTest + implements Runnable +{ + public static final int port = 4000 + (int)(java.lang.Math.random() * 1000); + public static final String message = "hello"; + public static int count = 0; + public static String received; + + void send() + throws Exception + { + DatagramSocket sender = new DatagramSocket(); + InetAddress local = sender.getLocalAddress(); + byte []message_bytes = message.getBytes(); + + DatagramPacket packet = new DatagramPacket(message_bytes, + message_bytes.length, + local, port); + + sender.send(packet); + sender.close(); + } + void receive() + throws Exception + { + DatagramSocket socket = new DatagramSocket(port); + socket.setSoTimeout(10); + + byte[] buffer = new byte[100]; + DatagramPacket packet = new DatagramPacket(buffer, buffer.length); + + synchronized(this) { + notifyAll(); + } + + socket.receive(packet); + socket.close(); + + received = new String(buffer, 0, packet.getLength()); + + count++; + if ( message.length() != received.length() ) + throw new Exception("Receved "+ received.length()+ + " bytes but sent "+message.length() + " bytes"); + + if ( ! message.equals(received) ) + throw new Exception("Receved \""+ received+ + "\" but sent \""+message + "\""); + + } + + public void run() + { + String name = Thread.currentThread().getName(); + if (name.equals("timer")) { + try { + Thread.sleep(10000); + } catch (InterruptedException e){} + System.out.println("FAILED: timer triggered"); + System.exit(0); + } + try { + receive(); + } catch (Exception e) { + System.out.println("FAILED: receiver: " + e); + System.exit(0); + } + } + public static void main(String args[]) + { + try { + DatagramSocketSendReceiveTest sender = + new DatagramSocketSendReceiveTest(); + DatagramSocketSendReceiveTest receiver = + new DatagramSocketSendReceiveTest(); + Thread receiver_thread = new Thread(receiver); + + /* Make sure the test terminates even if it hangs on network */ + DatagramSocketSendReceiveTest timer = new DatagramSocketSendReceiveTest(); + Thread timer_thread = new Thread(timer, "timer"); + timer_thread.start(); + + synchronized(receiver) { + receiver_thread.start(); + receiver.wait(); + } + try { + sender.send(); + } catch (Exception e) { + System.out.println("FAILED: sender: " + e); + System.exit(0); + } + receiver_thread.join(); + + if (0 == count) + throw new Exception("Nothing received"); + + System.out.println("PASSED: DatagramSocket send/receive count="+count+ + " message="+received); + System.exit(0); + } catch (Exception e) { + System.out.println("FAILED: " + e); + System.exit(0); + } + } +} diff --git a/libjava/classpath/testsuite/java.net/DatagramSocketTest.java b/libjava/classpath/testsuite/java.net/DatagramSocketTest.java new file mode 100644 index 00000000000..4ee0d499068 --- /dev/null +++ b/libjava/classpath/testsuite/java.net/DatagramSocketTest.java @@ -0,0 +1,23 @@ +import java.net.*; + +public class DatagramSocketTest +{ + public static void main(String args[]) + { + try { + DatagramSocket socket = new DatagramSocket(); + + InetAddress local = socket.getLocalAddress(); + + int port = socket.getLocalPort(); + + socket.setSoTimeout(socket.getSoTimeout()); + + socket.close(); + + System.out.println("PASSED: new DatagramSocket()"); + } catch (Exception e) { + System.out.println("FAILED: " + e); + } + } +} diff --git a/libjava/classpath/testsuite/java.net/SocketSendReceiveTest.java b/libjava/classpath/testsuite/java.net/SocketSendReceiveTest.java new file mode 100644 index 00000000000..15ab983a44a --- /dev/null +++ b/libjava/classpath/testsuite/java.net/SocketSendReceiveTest.java @@ -0,0 +1,113 @@ +import java.net.*; +import java.io.*; + +/* + * Start one thread for receiving a packet, wait for it to set up, + * send a packet to it, and wait until it completes. Compare the + * packet to make sure it came thru without errors. + */ + +public class SocketSendReceiveTest + implements Runnable +{ + public static final int port = 4000 + (int)(java.lang.Math.random() * 2000); + public static final String message = "hello"; + public static int count = 0; + public static String received; + + void send() + throws Exception + { + InetAddress local = InetAddress.getLocalHost(); + Socket sender = new Socket(local, port); + byte []message_bytes = message.getBytes(); + + DataOutputStream out = new DataOutputStream(sender.getOutputStream()); + out.write(message_bytes, 0, message_bytes.length); + out.flush(); + sender.close(); + } + void receive() + throws Exception + { + ServerSocket socket = new ServerSocket(port); + + synchronized(this) { + notifyAll(); + } + + Socket connection = socket.accept(); + DataInputStream in = new DataInputStream(connection.getInputStream()); + + byte[] buffer = new byte[100]; + + int length = in.read(buffer); + + connection.close(); + socket.close(); + + received = new String(buffer, 0, length); + + count++; + if ( message.length() != received.length() ) + throw new Exception("Receved "+ received.length()+ + " bytes but sent "+message.length() + " bytes"); + + if ( ! message.equals(received) ) + throw new Exception("Receved \""+ received+ + "\" but sent \""+message + "\""); + } + + public void run() + { + String name = Thread.currentThread().getName(); + if (name.equals("timer")) { + try { + Thread.sleep(10000); + } catch (InterruptedException e){} + System.out.println("FAILED: timer triggered"); + System.exit(0); + } + try { + receive(); + } catch (Exception e) { + System.out.println("FAILED: receiver (port "+port + "): " + e); + System.exit(0); + } + } + public static void main(String args[]) + { + try { + SocketSendReceiveTest sender = new SocketSendReceiveTest(); + SocketSendReceiveTest receiver = new SocketSendReceiveTest(); + Thread receiver_thread = new Thread(receiver); + + /* Make sure the test terminates even if it hangs on network */ + SocketSendReceiveTest timer = new SocketSendReceiveTest(); + Thread timer_thread = new Thread(timer, "timer"); + timer_thread.start(); + + synchronized(receiver) { + receiver_thread.start(); + receiver.wait(); + } + try { + sender.send(); + } catch (Exception e) { + System.out.println("FAILED: receiver (port "+port + "): " + e); + System.exit(0); + } + receiver_thread.join(); + + if (0 == count) + throw new Exception("Nothing received"); + + System.out.println("PASSED: Socket send/receive count="+count+ + " message="+received); + System.exit(0); + } catch (Exception e) { + System.out.println("FAILED: " + e); + System.exit(0); + } + } +} diff --git a/libjava/classpath/testsuite/java.net/SocketTest.java b/libjava/classpath/testsuite/java.net/SocketTest.java new file mode 100644 index 00000000000..78a367b4d67 --- /dev/null +++ b/libjava/classpath/testsuite/java.net/SocketTest.java @@ -0,0 +1,34 @@ +import java.net.*; + +public class SocketTest +{ + public static void main(String args[]) + { + try { + Socket socket = new Socket("www.hungry.com", 80); + + InetAddress remote = socket.getInetAddress(); + InetAddress local = socket.getLocalAddress(); + + int rport = socket.getPort(); + int lport = socket.getLocalPort(); + + socket.setSoTimeout(socket.getSoTimeout()); + socket.setTcpNoDelay(socket.getTcpNoDelay()); + int linger = socket.getSoLinger(); + if (-1 != linger) + socket.setSoLinger(true, linger); + else + socket.setSoLinger(false, 0); + + String socketString = socket.toString(); + if (null == socketString) + throw new Exception("toString() failed"); + + socket.close(); + System.out.println("PASSED: new Socket()" + socketString); + } catch (Exception e) { + System.out.println("FAILED: " + e); + } + } +} diff --git a/libjava/classpath/testsuite/java.net/URLTest.java b/libjava/classpath/testsuite/java.net/URLTest.java new file mode 100644 index 00000000000..026ea12b56a --- /dev/null +++ b/libjava/classpath/testsuite/java.net/URLTest.java @@ -0,0 +1,23 @@ +import java.net.*; +import java.io.*; + +public class URLTest +{ + public static void main(String args[]) + { + try { + URL url = new URL("http://www.hungry.com/"); + InputStream stream = url.openStream(); + + int size = 0; + + while (-1 != stream.read()) { size++; } + + stream.close(); + + System.out.println("PASSED: new URL() size=" + size ); + } catch (Exception e) { + System.out.println("FAILED: " + e); + } + } +} diff --git a/libjava/classpath/testsuite/java.net/execute.exp b/libjava/classpath/testsuite/java.net/execute.exp new file mode 100644 index 00000000000..1092485c088 --- /dev/null +++ b/libjava/classpath/testsuite/java.net/execute.exp @@ -0,0 +1,7 @@ +# +# Author: Petter Reinholdtsen <pere@td.org.uit.no> + +# Load support procs +load_lib java.exp + +test-java-source |