Initial load
diff --git a/test/java/net/Socket/AccurateTimeout.java b/test/java/net/Socket/AccurateTimeout.java
new file mode 100644
index 0000000..0bcff7b
--- /dev/null
+++ b/test/java/net/Socket/AccurateTimeout.java
@@ -0,0 +1,144 @@
+/*
+ * Copyright 2002 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/* @test
+ * @bug 4512028
+ * @summary Check the tolerance on read timeouts.
+ */
+import java.net.*;
+import java.io.*;
+
+public class AccurateTimeout {
+
+ static final int TOLERANCE = 100;
+
+ static boolean skipTest() {
+ String os = System.getProperty("os.name");
+ if (os.equals("Windows 95") ||
+ os.equals("Windows 98") ||
+ os.equals("Windows Me")) {
+
+ System.out.println("Due to an OS bug timeout tolerance cannot be tested on this OS");
+ return true;
+ }
+ return false;
+ }
+
+ public static void main(String args[]) throws Exception {
+
+ if (skipTest()) {
+ return;
+ }
+
+ int failures = 0;
+ int timeout;
+
+ System.out.println("");
+ System.out.println("Testing Socket.getInputStream().read() ...");
+ System.out.println("");
+
+ ServerSocket ss = new ServerSocket(0);
+ Socket s1 = new Socket(InetAddress.getLocalHost(), ss.getLocalPort());
+ Socket s2 = ss.accept();
+
+ InputStream in = s1.getInputStream();
+
+ timeout = 100;
+ while (timeout < 2500) {
+ s1.setSoTimeout(timeout);
+
+ long startTime = System.currentTimeMillis();
+ try {
+ in.read();
+ } catch (SocketTimeoutException e) {
+ }
+ long actual = System.currentTimeMillis() - startTime;
+
+ System.out.print("excepted: " + timeout + " actual: " + actual);
+
+ if (Math.abs(actual-timeout) > TOLERANCE) {
+ System.out.print(" *** FAIL: outside tolerance");
+ failures++;
+ } else {
+ System.out.print(" PASS.");
+ }
+
+ System.out.println("");
+ timeout += 200;
+ }
+
+ s1.close();
+ s2.close();
+ ss.close();
+
+
+ // ----------
+
+
+ System.out.println("");
+ System.out.println("Testing DatagramSocket.receive ...");
+ System.out.println("");
+
+ byte b[] = new byte[8];
+ DatagramPacket p = new DatagramPacket(b, b.length);
+
+ DatagramSocket ds = new DatagramSocket();
+
+ timeout = 100;
+ while (timeout < 2500) {
+ ds.setSoTimeout(timeout);
+
+ long startTime = System.currentTimeMillis();
+ try {
+ ds.receive(p);
+ } catch (SocketTimeoutException e) {
+ }
+ long actual = System.currentTimeMillis() - startTime;
+
+ System.out.print("excepted: " + timeout + " actual: " + actual);
+
+ if (Math.abs(actual-timeout) > TOLERANCE) {
+ System.out.print(" *** FAIL: outside tolerance");
+ failures++;
+ } else {
+ System.out.print(" PASS.");
+ }
+
+ System.out.println("");
+ timeout += 200;
+ }
+
+ ds.close();
+
+ System.out.println("");
+
+ // ---------
+
+ if (failures > 0) {
+ throw new Exception("Test failed: " + failures +
+ " test(s) outside tolerance");
+ }
+
+ }
+
+}
diff --git a/test/java/net/Socket/AddressTest.java b/test/java/net/Socket/AddressTest.java
new file mode 100644
index 0000000..c3553e9
--- /dev/null
+++ b/test/java/net/Socket/AddressTest.java
@@ -0,0 +1,156 @@
+/*
+ * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4507501
+ * @summary Test various methods that should throw IAE when passed improper SocketAddress
+ *
+ */
+
+import java.net.*;
+
+public class AddressTest {
+ class MySocketAddress extends SocketAddress {
+ public MySocketAddress() {
+ }
+ }
+
+ public AddressTest() throws Exception {
+ SocketAddress addr = new MySocketAddress();
+ Socket soc = new Socket();
+ ServerSocket serv = new ServerSocket();
+ DatagramSocket ds = new DatagramSocket((SocketAddress)null);
+ DatagramPacket pac = new DatagramPacket(new byte[20], 20);
+ MulticastSocket mul = new MulticastSocket((SocketAddress) null);
+ boolean ok = false;
+ try {
+ soc.bind(addr);
+ } catch (IllegalArgumentException e) {
+ ok = true;
+ } catch (Exception e2) {
+ }
+ if (!ok)
+ throw new RuntimeException("Socket.bind should throw IllegalArgumentException!");
+
+ ok = false;
+ soc.close();
+ soc = new Socket();
+ try {
+ soc.connect(addr, 100);
+ } catch (IllegalArgumentException e) {
+ ok = true;
+ } catch (Exception e2) {
+ }
+ if (!ok)
+ throw new RuntimeException("Socket.connect should throw IllegalArgumentException!");
+
+ ok = false;
+ try {
+ serv.bind(addr);
+ } catch (IllegalArgumentException e) {
+ ok = true;
+ } catch (Exception e2) {
+ }
+ if (!ok)
+ throw new RuntimeException("ServerSocket.bind should throw IllegalArgumentException!");
+
+ ok = false;
+
+ try {
+ pac.setSocketAddress(addr);
+ } catch (IllegalArgumentException e) {
+ ok = true;
+ } catch (Exception e2) {
+ }
+
+ if (!ok)
+ throw new RuntimeException("DatagramPacket.setSocketAddress should throw IllegalArgumentException");
+
+ ok = false;
+
+ try {
+ ds.bind(addr);
+ } catch (IllegalArgumentException e) {
+ ok = true;
+ } catch (Exception e2) {
+ }
+
+ if (!ok)
+ throw new RuntimeException("DatagramSocket.bind should throw IllegalArgumentException");
+
+ ok = false;
+
+ try {
+ ds.connect(addr);
+ } catch (IllegalArgumentException e) {
+ ok = true;
+ } catch (Exception e2) {
+ }
+
+ if (!ok)
+ throw new RuntimeException("DatagramSocket.connect should throw IllegalArgumentException");
+
+ ok = false;
+
+ try {
+ mul.bind(addr);
+ } catch (IllegalArgumentException e) {
+ ok = true;
+ } catch (Exception e2) {
+ }
+
+ if (!ok)
+ throw new RuntimeException("MulticastSocket.bind should throw IllegalArgumentException");
+
+ ok = false;
+
+ mul.close();
+ mul = new MulticastSocket(new InetSocketAddress(0));
+ try {
+ mul.joinGroup(addr, null);
+ } catch (IllegalArgumentException e) {
+ ok = true;
+ } catch (Exception e2) {
+ }
+
+ if (!ok)
+ throw new RuntimeException("MulticastSocket.joinGroup should throw IllegalArgumentException");
+
+ ok = false;
+ try {
+ mul.leaveGroup(addr, null);
+ } catch (IllegalArgumentException e) {
+ ok = true;
+ } catch (Exception e2) {
+ }
+
+ if (!ok)
+ throw new RuntimeException("MulticastSocket.leaveGroup should throw IllegalArgumentException");
+
+ }
+
+ public static void main(String[] args) throws Exception {
+ new AddressTest();
+ }
+}
diff --git a/test/java/net/Socket/CloseAvailable.java b/test/java/net/Socket/CloseAvailable.java
new file mode 100644
index 0000000..8ea407d
--- /dev/null
+++ b/test/java/net/Socket/CloseAvailable.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright 1998 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4091859
+ * @summary Test Socket.available()
+ *
+ */
+
+import java.net.*;
+import java.io.*;
+
+
+public class CloseAvailable implements Runnable {
+ static ServerSocket ss;
+ static InetAddress addr;
+ static int port;
+
+ public static void main(String[] args) throws Exception {
+ boolean error = true;
+ addr = InetAddress.getLocalHost();
+ ss = new ServerSocket(0);
+ port = ss.getLocalPort();
+
+ Thread t = new Thread(new CloseAvailable());
+ t.start();
+
+ Socket soc = ss.accept();
+
+ DataInputStream is = new DataInputStream(soc.getInputStream());
+ is.close();
+
+ try {
+ is.available();
+ }
+ catch (IOException ex) {
+ error = false;
+ }
+ if (error)
+ throw new RuntimeException("Available() can be called after stream closed.");
+ }
+
+ public void run() {
+ try {
+ Socket s = new Socket(addr, port);
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+}
diff --git a/test/java/net/Socket/DeadlockTest.java b/test/java/net/Socket/DeadlockTest.java
new file mode 100644
index 0000000..373c681
--- /dev/null
+++ b/test/java/net/Socket/DeadlockTest.java
@@ -0,0 +1,236 @@
+/*
+ * Copyright 1999 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4176738
+ * @summary Make sure a deadlock situation
+ * would not occur
+ */
+
+import java.net.*;
+import java.io.*;
+
+public class DeadlockTest {
+ public static void main(String [] argv) throws Exception {
+
+ // Start the server thread
+ Thread s1 = new Thread(new ServerThread());
+ s1.start();
+
+ // Sleep to make sure s1 has created a server socket
+ Thread.sleep(1000);
+
+ // Start the client thread
+ ClientThread ct = new ClientThread();
+ Thread c1 = new Thread(ct);
+ c1.start();
+
+ // Wait for the client thread to finish
+ c1.join(40000);
+
+ // If timeout, we assume there is a deadlock
+ if (c1.isAlive() == true) {
+ // Close the socket to force the server thread
+ // terminate too
+ s1.stop();
+ ct.getSock().close();
+ throw new Exception("Takes too long. Dead lock");
+ }
+ }
+}
+
+class ServerThread implements Runnable {
+
+ private static boolean dbg = false;
+
+ ObjectInputStream in;
+ ObjectOutputStream out;
+
+ ServerSocket server;
+
+ Socket sock;
+
+ public ServerThread() throws Exception {
+
+ }
+
+ public void ping(int cnt) {
+ Message.write(out, new PingMessage(cnt));
+ }
+
+ private int cnt = 1;
+
+ public void run() {
+
+ try {
+ if (Thread.currentThread().getName().startsWith("child") == false) {
+ server = new ServerSocket(4711);
+ sock = server.accept();
+
+ new Thread(this, "child").start();
+
+ out = new ObjectOutputStream(sock.getOutputStream());
+ out.flush();
+
+ if (dbg) System.out.println("*** ping0 ***");
+ ping(0);
+ if (dbg) System.out.println("*** ping1 ***");
+ ping(1);
+ if (dbg) System.out.println("*** ping2 ***");
+ ping(2);
+ if (dbg) System.out.println("*** ping3 ***");
+ ping(3);
+ if (dbg) System.out.println("*** ping4 ***");
+ ping(4);
+ if (dbg) System.out.println("*** end ***");
+ }
+
+ } catch (Throwable e) {
+ // If anything goes wrong, just quit.
+ }
+
+ if (Thread.currentThread().getName().startsWith("child")) {
+ try {
+
+ in = new ObjectInputStream(sock.getInputStream());
+
+ while (true) {
+ if (dbg) System.out.println("read " + cnt);
+ Message msg = (Message) in.readObject();
+ if (dbg) System.out.println("read done " + cnt++);
+ switch (msg.code) {
+ case Message.PING: {
+ if (true) System.out.println("ping recv'ed");
+ } break;
+ }
+
+ }
+
+ } catch (Throwable e) {
+ // If anything goes wrong, just quit. }
+ }
+ }
+ }
+}
+
+class ClientThread implements Runnable {
+
+ ObjectInputStream in;
+ ObjectOutputStream out;
+
+ Socket sock;
+
+ public ClientThread() throws Exception {
+ try {
+ System.out.println("About to create a socket");
+ sock = new Socket(InetAddress.getLocalHost().getHostName(), 4711);
+ System.out.println("connected");
+
+ out = new ObjectOutputStream(sock.getOutputStream());
+ out.flush();
+ } catch (Throwable e) {
+ System.out.println("client failed with: " + e);
+ e.printStackTrace();
+ throw new Exception("Unexpected exception");
+ }
+ }
+
+ public Socket getSock() {
+ return sock;
+ }
+
+ private int cnt = 1;
+
+ public void run() {
+ try {
+ in = new ObjectInputStream(sock.getInputStream());
+
+ int count = 0;
+
+ while (true) {
+ System.out.println("read " + cnt);
+ Message msg = (Message) in.readObject();
+ System.out.println("read done " + cnt++);
+ switch (msg.code) {
+ case Message.PING: {
+ System.out.println("ping recv'ed");
+ count++;
+ } break;
+ }
+ if (count == 5) {
+ sock.close();
+ break;
+ }
+ }
+ } catch (IOException ioe) {
+ } catch (Throwable e) {
+ // If anything went wrong, just quit
+ }
+ }
+
+}
+
+class Message implements java.io.Serializable {
+
+ static final int UNKNOWN = 0;
+ static final int PING = 1;
+
+ protected int code;
+
+ public Message() { this.code = UNKNOWN; }
+
+ public Message(int code) { this.code = code; }
+
+ private static int cnt = 1;
+
+ public static void write(ObjectOutput out, Message msg) {
+ try {
+ System.out.println("write message " + cnt);
+ out.writeObject(msg);
+ System.out.println("flush message");
+ out.flush();
+ System.out.println("write message done " + cnt++);
+ } catch (IOException ioe) {
+ // Ignore the exception
+ }
+ }
+}
+
+class PingMessage extends Message implements java.io.Serializable {
+
+ public PingMessage() {
+ code = Message.PING;
+ }
+
+ public PingMessage(int cnt)
+ {
+ code = Message.PING;
+ this.cnt = cnt;
+
+ data = new int[50000];
+ }
+
+ int cnt;
+ int[] data;
+}
diff --git a/test/java/net/Socket/FDClose.java b/test/java/net/Socket/FDClose.java
new file mode 100644
index 0000000..a64478a
--- /dev/null
+++ b/test/java/net/Socket/FDClose.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright 1998-2002 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/**
+ * @test
+ * @bug 4152799
+ * @summary test to see if interrupting a socket accept closes fd0
+ */
+import java.net.*;
+import java.io.*;
+import java.util.*;
+
+public class FDClose {
+
+ static boolean isServerReady = false;
+
+ public static void main(String[] args) throws Exception {
+
+ Thread me = Thread.currentThread();
+
+ // Put a thread waiting on SocketServer.Accept
+ AReader test = new AReader();
+ Thread readerThread = new Thread(test);
+ readerThread.start();
+
+ // wait for the server socket to be ready
+ while (!isServerReady) {
+ me.sleep(100);
+ }
+
+ // Interrupt the waiting thread
+ readerThread.interrupt();
+
+ // Wait another moment
+ me.sleep(100);
+
+ // Check to see if fd0 is closed
+ System.in.available();
+ }
+
+ public static class AReader implements Runnable {
+ public void run() {
+ try {
+ ServerSocket sock = new ServerSocket(0);
+ isServerReady = true;
+ sock.accept();
+ } catch (Exception e) {
+ }
+ }
+ }
+}
diff --git a/test/java/net/Socket/GetLocalAddress.java b/test/java/net/Socket/GetLocalAddress.java
new file mode 100644
index 0000000..34f879e
--- /dev/null
+++ b/test/java/net/Socket/GetLocalAddress.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright 1998 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4106601
+ * @summary Test the java.net.socket.GetLocalAddress method
+ *
+ */
+
+import java.net.*;
+
+public class GetLocalAddress implements Runnable {
+ static ServerSocket ss;
+ static InetAddress addr;
+ static int port;
+
+ public static void main(String args[]) throws Exception {
+ boolean error = true;
+ int linger = 65546;
+ int value = 0;
+ addr = InetAddress.getLocalHost();
+ ss = new ServerSocket(0);
+ port = ss.getLocalPort();
+
+ Thread t = new Thread(new GetLocalAddress());
+ t.start();
+ Socket soc = ss.accept();
+
+ if(addr.equals(soc.getLocalAddress())) {
+ error = false;
+ }
+ if (error)
+ throw new RuntimeException("Socket.GetLocalAddress failed.");
+ soc.close();
+ }
+
+ public void run() {
+ try {
+ Socket s = new Socket(addr, port);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+}
diff --git a/test/java/net/Socket/IDNTest.java b/test/java/net/Socket/IDNTest.java
new file mode 100644
index 0000000..c698b83
--- /dev/null
+++ b/test/java/net/Socket/IDNTest.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/**
+ * @test
+ * @bug 4957669 5017871
+ * @summary REGRESSION:create a socket with a Japanese hostname throws IllegalArgumentExcept; REGRESSION: host can't contain special chars such as space
+ */
+
+import java.net.Socket;
+import java.net.UnknownHostException;
+
+// internalized domain name test
+public class IDNTest {
+ public static void main(String[] args) {
+ try {
+ Socket s = new Socket("\u67f4\u7530\u82b3\u6a39", 8000);
+ } catch (UnknownHostException e) {
+ // Expected
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ try {
+ Socket s = new Socket("ho st", 8000);
+ } catch (UnknownHostException e) {
+ // Expected
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+}
diff --git a/test/java/net/Socket/InheritHandle.java b/test/java/net/Socket/InheritHandle.java
new file mode 100644
index 0000000..28dbe44
--- /dev/null
+++ b/test/java/net/Socket/InheritHandle.java
@@ -0,0 +1,108 @@
+/*
+ * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/* @test
+ @bug 6598160
+ @summary Windows IPv6 Socket implementation doesn't set the handle to not inherit
+ @author Chris Hegarty
+ */
+
+import java.net.ServerSocket;
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * This test is only really applicable to Windows machines that are running IPv6, but
+ * it should still pass on other platforms so we can just run it.
+ */
+
+public class InheritHandle
+{
+ static String java = System.getProperty("java.home") + File.separator +
+ "bin" + File.separator + "java";
+
+ public static void main(String[] args) {
+ if (args.length == 1) {
+ doWait();
+ } else {
+ startTest();
+ }
+
+ }
+
+ static void startTest() {
+ ServerSocket ss;
+ int port;
+ Process process;
+
+ // create a ServerSocket listening on any port
+ try {
+ ss = new ServerSocket(0);
+ port = ss.getLocalPort();
+ System.out.println("First ServerSocket listening on port " + port);
+ } catch (IOException e) {
+ System.out.println("Cannot create ServerSocket");
+ e.printStackTrace();
+ return;
+ }
+
+ // create another java process that simply waits. If the bug is present this
+ // process will inherit the native IPv6 handle for ss and cause the second
+ // ServerSocket constructor to throw a BindException
+ try {
+ process = Runtime.getRuntime().exec(java + " InheritHandle -doWait");
+ } catch (IOException ioe) {
+ System.out.println("Cannot create process");
+ ioe.printStackTrace();
+ return;
+ }
+
+ // Allow some time for the process to get started
+ try {
+ System.out.println("waiting...");
+ Thread.sleep(2 * 1000);
+
+ System.out.println("Now close the socket and try to create another" +
+ " one listening on the same port");
+ ss.close();
+ ss = new ServerSocket(port);
+ System.out.println("Second ServerSocket created successfully");
+ ss.close();
+
+ } catch (InterruptedException ie) {
+ } catch (IOException ioe) {
+ throw new RuntimeException("Failed: " + ioe);
+ } finally {
+ process.destroy();
+ }
+
+ System.out.println("OK");
+ }
+
+ static void doWait() {
+ try {
+ Thread.sleep(200 * 1000);
+ } catch (InterruptedException ie) {
+ }
+ }
+}
diff --git a/test/java/net/Socket/InheritTimeout.java b/test/java/net/Socket/InheritTimeout.java
new file mode 100644
index 0000000..5d6c3e0
--- /dev/null
+++ b/test/java/net/Socket/InheritTimeout.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2001-2002 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4508149
+ * @summary Setting ServerSocket.setSoTimeout shouldn't cause
+ * the timeout to be inherited by accepted connections
+ */
+
+import java.net.*;
+import java.io.InputStream;
+
+public class InheritTimeout {
+
+ class Reaper extends Thread {
+ Socket s;
+ int timeout;
+
+ Reaper(Socket s, int timeout) {
+ this.s = s;
+ this.timeout = timeout;
+ }
+
+ public void run() {
+ try {
+ Thread.currentThread().sleep(timeout);
+ s.close();
+ } catch (Exception e) {
+ }
+ }
+ }
+
+ InheritTimeout() throws Exception {
+ ServerSocket ss = new ServerSocket(0);
+ ss.setSoTimeout(1000);
+
+ InetAddress ia = InetAddress.getLocalHost();
+ InetSocketAddress isa =
+ new InetSocketAddress(ia, ss.getLocalPort());
+
+ // client establishes the connection
+ Socket s1 = new Socket();
+ s1.connect(isa);
+
+ // receive the connection
+ Socket s2 = ss.accept();
+
+ // schedule reaper to close the socket in 5 seconds
+ Reaper r = new Reaper(s2, 5000);
+ r.start();
+
+ boolean readTimedOut = false;
+ try {
+ s2.getInputStream().read();
+ } catch (SocketTimeoutException te) {
+ readTimedOut = true;
+ } catch (SocketException e) {
+ if (!s2.isClosed()) {
+ throw e;
+ }
+ }
+
+ s1.close();
+ ss.close();
+
+ if (readTimedOut) {
+ throw new Exception("Unexpected SocketTimeoutException throw!");
+ }
+ }
+
+ public static void main(String args[]) throws Exception {
+ new InheritTimeout();
+ }
+}
diff --git a/test/java/net/Socket/LingerTest.java b/test/java/net/Socket/LingerTest.java
new file mode 100644
index 0000000..52cbf56
--- /dev/null
+++ b/test/java/net/Socket/LingerTest.java
@@ -0,0 +1,141 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/**
+ * @test
+ * @bug 4796166
+ * @summary Linger interval delays usage of released file descriptor
+ */
+
+import java.net.*;
+import java.io.*;
+
+public class LingerTest {
+
+ static class Sender implements Runnable {
+ Socket s;
+
+ public Sender(Socket s) {
+ this.s = s;
+ }
+
+ public void run() {
+ System.out.println ("Sender starts");
+ try {
+ s.getOutputStream().write(new byte[128*1024]);
+ }
+ catch (IOException ioe) {
+ }
+ System.out.println ("Sender ends");
+ }
+ }
+
+ static class Closer implements Runnable {
+ Socket s;
+
+ public Closer(Socket s) {
+ this.s = s;
+ }
+
+ public void run() {
+ System.out.println ("Closer starts");
+ try {
+ s.close();
+ }
+ catch (IOException ioe) {
+ }
+ System.out.println ("Closer ends");
+ }
+ }
+
+ static class Another implements Runnable {
+ int port;
+ long delay;
+ boolean connected = false;
+
+ public Another(int port, long delay) {
+ this.port = port;
+ this.delay = delay;
+ }
+
+ public void run() {
+ System.out.println ("Another starts");
+ try {
+ Thread.currentThread().sleep(delay);
+ Socket s = new Socket("localhost", port);
+ synchronized (this) {
+ connected = true;
+ }
+ s.close();
+ }
+ catch (Exception ioe) {
+ ioe.printStackTrace();
+ }
+ System.out.println ("Another ends");
+ }
+
+ public synchronized boolean connected() {
+ return connected;
+ }
+ }
+
+ public static void main(String args[]) throws Exception {
+ ServerSocket ss = new ServerSocket(0);
+
+ Socket s1 = new Socket("localhost", ss.getLocalPort());
+ Socket s2 = ss.accept();
+
+
+ // setup conditions for untransmitted data and lengthy
+ // linger interval
+ s1.setSendBufferSize(128*1024);
+ s1.setSoLinger(true, 30);
+ s2.setReceiveBufferSize(1*1024);
+
+ // start sender
+ Thread thr = new Thread(new Sender(s1));
+ thr.start();
+
+ // another thread that will connect after 5 seconds.
+ Another another = new Another(ss.getLocalPort(), 5000);
+ thr = new Thread(another);
+ thr.start();
+
+ // give sender time to queue the data
+ Thread.currentThread().sleep(1000);
+
+ // close the socket asynchronously
+ (new Thread(new Closer(s1))).start();
+
+ // give another time to run
+ Thread.currentThread().sleep(10000);
+
+ // check that another is done
+ if (!another.connected()) {
+ throw new RuntimeException("Another thread is blocked");
+ }
+ System.out.println ("Main ends");
+
+ }
+
+}
diff --git a/test/java/net/Socket/LinkLocal.java b/test/java/net/Socket/LinkLocal.java
new file mode 100644
index 0000000..e65085d
--- /dev/null
+++ b/test/java/net/Socket/LinkLocal.java
@@ -0,0 +1,168 @@
+/*
+ * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4469866
+ * @summary Connecting to a link-local IPv6 address should not
+ * causes a SocketException to be thrown.
+ */
+import java.net.*;
+import java.util.Enumeration;
+
+public class LinkLocal {
+
+ static int testCount = 0;
+ static int failed = 0;
+
+ static void TcpTest(InetAddress ia) throws Exception {
+ System.out.println("**************************************");
+ testCount++;
+ System.out.println("Test " + testCount + ": TCP connect to " + ia);
+
+ /*
+ * Create ServerSocket on wildcard address and then
+ * try to connect Socket to link-local address.
+ */
+ ServerSocket ss = new ServerSocket(0);
+
+ Socket s = new Socket();
+ try {
+ s.connect(new InetSocketAddress(ia, ss.getLocalPort()));
+
+ System.out.println("Test passed - connection established.");
+
+ // connection was established so accept it
+ Socket s2 = ss.accept();
+ s2.close();
+ } catch (SocketException e) {
+ failed++;
+ System.out.println("Test failed: " + e);
+ }
+
+ // clean up
+ s.close();
+ ss.close();
+ }
+
+ static void UdpTest(InetAddress ia, boolean connected) throws Exception {
+
+ System.out.println("**************************************");
+ testCount++;
+
+ if (connected) {
+ System.out.println("Test " + testCount + ": UDP connect to " + ia);
+ } else {
+ System.out.println("Test " + testCount + ": UDP send to " + ia);
+ }
+
+ DatagramSocket ds1 = new DatagramSocket();
+ DatagramSocket ds2 = new DatagramSocket();
+
+ try {
+ byte b[] = "Hello".getBytes();
+ DatagramPacket p = new DatagramPacket(b, b.length);
+
+ if (connected) {
+ ds1.connect( new InetSocketAddress(ia, ds2.getLocalPort()) );
+ System.out.println("DatagramSocket connected.");
+ } else {
+ p.setAddress(ia);
+ p.setPort(ds2.getLocalPort());
+ }
+ ds1.send(p);
+ System.out.println("Packet has been sent.");
+
+ ds2.setSoTimeout(1000);
+ ds2.receive(p);
+ System.out.println("Test passed - packet received.");
+ } catch (SocketException e) {
+ failed++;
+ System.out.println("Test failed: " + e);
+ }
+
+ ds1.close();
+ ds2.close();
+ }
+
+ static void TestAddress(InetAddress ia) throws Exception {
+ TcpTest(ia);
+ UdpTest(ia, true); /* unconnected */
+ UdpTest(ia, false); /* connected */
+ }
+
+ public static void main(String args[]) throws Exception {
+
+ /*
+ * If an argument is provided ensure that it's
+ * a link-local IPv6 address.
+ */
+ if (args.length > 0) {
+ InetAddress ia = InetAddress.getByName(args[0]);
+
+ if ( !(ia instanceof Inet6Address) ||
+ !ia.isLinkLocalAddress()) {
+ throw new Exception(ia +
+ " is not a link-local IPv6 address");
+ }
+
+ TestAddress(ia);
+ }
+
+ /*
+ * If no argument is provided then enumerate the
+ * local addresses and run the test on each link-local
+ * IPv6 address.
+ */
+ if (args.length == 0) {
+ Enumeration nifs = NetworkInterface.getNetworkInterfaces();
+ while (nifs.hasMoreElements()) {
+ NetworkInterface ni = (NetworkInterface)nifs.nextElement();
+ Enumeration addrs = ni.getInetAddresses();
+ while (addrs.hasMoreElements()) {
+ InetAddress addr = (InetAddress)addrs.nextElement();
+
+ if (addr instanceof Inet6Address &&
+ addr.isLinkLocalAddress()) {
+
+ TestAddress(addr);
+ }
+ }
+ }
+ }
+
+ /*
+ * Print results
+ */
+ if (testCount == 0) {
+ System.out.println("No link-local IPv6 addresses - test skipped!");
+ } else {
+ System.out.println("**************************************");
+ System.out.println(testCount + " test(s) executed, " +
+ failed + " failed.");
+ if (failed > 0) {
+ throw new Exception( failed + " test(s) failed.");
+ }
+ }
+ }
+}
diff --git a/test/java/net/Socket/NullHost.java b/test/java/net/Socket/NullHost.java
new file mode 100644
index 0000000..b2c10d3
--- /dev/null
+++ b/test/java/net/Socket/NullHost.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2002 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4712609
+ * @summary Socket(String host, int port) throws NullPointerException if host is null
+ */
+
+import java.net.*;
+import java.io.IOException;
+
+public class NullHost {
+ class Server extends Thread {
+ private ServerSocket svr;
+
+ public Server() throws IOException {
+ svr = new ServerSocket();
+ svr.bind(new InetSocketAddress(0));
+ }
+
+ public int getPort() {
+ return svr.getLocalPort();
+ }
+
+ public void shutdown() {
+ try {
+ svr.close();
+ } catch (IOException e) {
+ }
+ }
+
+ public void run() {
+ Socket s;
+ try {
+ while (true) {
+ s = svr.accept();
+ s.close();
+ }
+ } catch (IOException e) {
+ }
+ }
+ }
+
+ public static void main(String[] args) throws IOException {
+ NullHost n = new NullHost();
+ }
+
+ public NullHost () throws IOException {
+ Server s = new Server();
+ int port = s.getPort();
+ s.start();
+ try {
+ Socket sock = new Socket((String)null, port);
+ sock.close();
+ sock = new Socket((String)null, port, true);
+ sock.close();
+ sock = new Socket((String)null, port, null, 0);
+ sock.close();
+
+ } catch (NullPointerException e) {
+ throw new RuntimeException("Got a NPE");
+ } finally {
+ s.shutdown();
+ }
+ }
+}
diff --git a/test/java/net/Socket/OldImpl.java b/test/java/net/Socket/OldImpl.java
new file mode 100644
index 0000000..4a15344
--- /dev/null
+++ b/test/java/net/Socket/OldImpl.java
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+import java.io.*;
+import java.net.*;
+
+
+/**
+ * @test
+ * @bug 5089488
+ * @summary java.net.Socket checks for old-style impls
+ */
+
+public class OldImpl {
+
+ /**
+ * A no-op SocketImpl descendant.
+ */
+ static class FunkySocketImpl extends SocketImpl {
+ protected void accept(SocketImpl impl) throws IOException {
+ }
+
+ protected int available(){
+ return 0;
+ }
+
+ protected void bind(InetAddress host, int port){
+ }
+
+ protected void close(){
+ }
+
+ protected void connect(InetAddress address, int port){
+ }
+
+ protected void connect(String host, int port){
+ }
+
+ protected void connect(SocketAddress a,int b){
+ }
+
+ protected void create(boolean stream){
+ }
+
+ protected InputStream getInputStream(){
+ return null;
+ }
+
+ protected OutputStream getOutputStream(){
+ return null;
+ }
+
+ protected void listen(int backlog){
+ }
+
+ public Object getOption(int optID){
+ return null;
+ }
+
+ public void setOption(int optID, Object value){
+ }
+
+ protected void sendUrgentData(int i){
+ }
+ }
+
+ static class FunkyWunkySocketImpl extends FunkySocketImpl {}
+
+ /**
+ * A no-op Socket descendant.
+ */
+ static class FunkySocket extends Socket {
+ public FunkySocket(SocketImpl impl) throws IOException {
+ super(impl);
+ }
+ }
+
+ public static void main(String args[]) throws Exception {
+ FunkyWunkySocketImpl socketImpl = new FunkyWunkySocketImpl();
+ FunkySocket socko = new FunkySocket(socketImpl);
+ if (socko.isBound()) {
+ throw new RuntimeException ("socket is not really bound");
+ }
+ }
+}
diff --git a/test/java/net/Socket/OldSocketImpl.jar b/test/java/net/Socket/OldSocketImpl.jar
new file mode 100644
index 0000000..6a646ba
--- /dev/null
+++ b/test/java/net/Socket/OldSocketImpl.jar
Binary files differ
diff --git a/test/java/net/Socket/OldSocketImpl.java b/test/java/net/Socket/OldSocketImpl.java
new file mode 100644
index 0000000..59a7d70
--- /dev/null
+++ b/test/java/net/Socket/OldSocketImpl.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+import java.io.*;
+import java.net.*;
+
+class OldSocketImpl extends SocketImpl {
+ public static void main(String[] args) throws Exception {
+ Socket.setSocketImplFactory(new SocketImplFactory() {
+ public SocketImpl createSocketImpl() {
+ return new OldSocketImpl();
+ }
+ });
+ Socket socket = new Socket("localhost", 23);
+ }
+
+ public void setOption(int optID, Object value) throws SocketException { }
+
+ public Object getOption(int optID) throws SocketException {
+ return null;
+ }
+
+ protected void create(boolean stream) throws IOException { }
+
+ protected void connect(String host, int port) throws IOException { }
+
+ protected void connect(InetAddress address, int port) throws IOException { }
+
+ // Not in 1.3...
+ // protected void connect(SocketAddress address, int timeout) throws IOException { }
+
+ protected void bind(InetAddress host, int port) throws IOException { }
+
+ protected void listen(int backlog) throws IOException { }
+
+ protected void accept(SocketImpl s) throws IOException { }
+
+ protected InputStream getInputStream() throws IOException {
+ return null;
+ }
+
+ protected OutputStream getOutputStream() throws IOException {
+ return null;
+ }
+
+ protected int available() throws IOException {
+ return 0;
+ }
+
+ protected void close() throws IOException { }
+
+ protected void sendUrgentData (int data) throws SocketException { }
+
+}
diff --git a/test/java/net/Socket/OldSocketImpl.sh b/test/java/net/Socket/OldSocketImpl.sh
new file mode 100644
index 0000000..8584546
--- /dev/null
+++ b/test/java/net/Socket/OldSocketImpl.sh
@@ -0,0 +1,57 @@
+#
+# Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
+# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+#
+# This code is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License version 2 only, as
+# published by the Free Software Foundation.
+#
+# This code is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+# version 2 for more details (a copy is included in the LICENSE file that
+# accompanied this code).
+#
+# You should have received a copy of the GNU General Public License version
+# 2 along with this work; if not, write to the Free Software Foundation,
+# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+# CA 95054 USA or visit www.sun.com if you need additional information or
+# have any questions.
+#
+
+# @test
+# @bug 6449565
+# @run shell/timeout=140 OldSocketImpl.sh
+# @summary Pre-1.4 SocketImpl no longer supported
+
+OS=`uname -s`
+case "$OS" in
+ SunOS | Linux )
+ PS=":"
+ FS="/"
+ ;;
+ Windows* )
+ PS=";"
+ FS="\\"
+ ;;
+ * )
+ echo "Unrecognized system!"
+ exit 1;
+ ;;
+esac
+
+# no need to compile the test. It is already compiled
+# with 1.3 and in OldStyleImpl.jar
+
+# run
+${TESTJAVA}${FS}bin${FS}java -cp ${TESTSRC}${FS}OldSocketImpl.jar OldSocketImpl
+result=$?
+if [ "$result" -ne "0" ]; then
+ exit 1
+fi
+
+# no failures, exit.
+exit 0
+
diff --git a/test/java/net/Socket/ProxyCons.java b/test/java/net/Socket/ProxyCons.java
new file mode 100644
index 0000000..f2f18fe
--- /dev/null
+++ b/test/java/net/Socket/ProxyCons.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4097826
+ * @summary SOCKS support inadequate
+ * @run main/timeout=40/othervm -DsocksProxyHost=nonexistant ProxyCons
+ */
+
+import java.net.*;
+public class ProxyCons {
+ class Server extends Thread {
+ ServerSocket server;
+ Server (ServerSocket server) {
+ super ();
+ this.server = server;
+ }
+ public void run () {
+ try {
+ Socket s = server.accept ();
+ while (!finished ()) {
+ Thread.sleep (500);
+ }
+ } catch (Exception e) {
+ }
+ }
+ boolean isFinished = false;
+
+ synchronized boolean finished () {
+ return (isFinished);
+ }
+ synchronized void done () {
+ isFinished = true;
+ }
+ }
+
+ public ProxyCons() {
+ }
+
+ void test() {
+ try {
+ ServerSocket ss = new ServerSocket();
+ ss.bind(new InetSocketAddress(0));
+ Server s = new Server(ss);
+ s.start();
+ Socket sock = new Socket(Proxy.NO_PROXY);
+ sock.connect(new InetSocketAddress("localhost", ss.getLocalPort()));
+ s.done();
+ sock.close();
+ } catch (java.io.IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static void main(String[] args) {
+ ProxyCons c = new ProxyCons();
+ c.test();
+ }
+}
diff --git a/test/java/net/Socket/RST.java b/test/java/net/Socket/RST.java
new file mode 100644
index 0000000..fb018dd
--- /dev/null
+++ b/test/java/net/Socket/RST.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @bug 4468997
+ * @summary SO_LINGER is ignored on Windows with Winsock 2
+ */
+import java.net.*;
+import java.io.*;
+
+public class RST implements Runnable {
+
+ Socket client;
+
+ public void run() {
+ try {
+ client.setSoLinger(true, 0); // hard reset
+ client.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ RST() throws Exception {
+ ServerSocket ss = new ServerSocket(0);
+ client = new Socket("localhost", ss.getLocalPort());
+ Socket server = ss.accept();
+
+ Thread thr = new Thread(this);
+ thr.start();
+
+ SocketException exc = null;
+ try {
+ InputStream in = server.getInputStream();
+
+ /*
+ * This read should throw a SocketException indicating a
+ * connection reset.
+ */
+ int n = in.read();
+ } catch (SocketException se) {
+ exc = se;
+ }
+
+ server.close();
+ ss.close();
+
+ if (exc == null) {
+ throw new Exception("Expected SocketException not thrown");
+ }
+ if (exc.getMessage().toLowerCase().indexOf("reset") == -1) {
+ throw new Exception("SocketException thrown but not expected \"connection reset\"");
+ }
+ }
+
+
+ public static void main(String args[]) throws Exception {
+ new RST();
+ }
+}
diff --git a/test/java/net/Socket/ReadTimeout.java b/test/java/net/Socket/ReadTimeout.java
new file mode 100644
index 0000000..f48608b
--- /dev/null
+++ b/test/java/net/Socket/ReadTimeout.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright 1998-2000 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/**
+ * @test
+ * @bug 4169831
+ * @summary test timeout on a socket read
+ * @run main/timeout=15 ReadTimeout
+ */
+
+import java.net.*;
+import java.io.*;
+
+public class ReadTimeout {
+ public static void main(String args[]) throws Exception {
+ InetAddress sin = null;
+ Socket soc = null,soc1 = null;
+ InputStream is = null;
+ OutputStream os = null;
+ ServerSocket srv = null;
+ int port = 0;
+ int tout = 1000;
+
+ sin = InetAddress.getLocalHost();
+ srv = new ServerSocket(port);
+ port = srv.getLocalPort();
+ soc = new Socket(sin, port, true);
+ soc1 = srv.accept();
+ soc.setSoTimeout(tout);
+
+ try {
+ is = soc.getInputStream();
+ os = soc1.getOutputStream();
+ is.read();
+ } catch(InterruptedIOException e) {
+ }
+
+ soc.close();
+ soc1.close();
+ srv.close();
+ }
+}
diff --git a/test/java/net/Socket/SetReceiveBufferSize.java b/test/java/net/Socket/SetReceiveBufferSize.java
new file mode 100644
index 0000000..0e38fd0
--- /dev/null
+++ b/test/java/net/Socket/SetReceiveBufferSize.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2000 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4326250
+ * @summary Test Socket.setReceiveBufferSize() throwin IllegalArgumentException
+ *
+ */
+
+import java.net.Socket;
+import java.net.ServerSocket;
+
+public class SetReceiveBufferSize {
+ class Server extends Thread {
+ private ServerSocket ss;
+ public Server(ServerSocket ss) {
+ this.ss = ss;
+ }
+
+ public void run() {
+ try {
+ ss.accept();
+ } catch (Exception e) {
+ }
+ }
+ }
+
+ public static void main(String[] args) throws Exception {
+ SetReceiveBufferSize s = new SetReceiveBufferSize();
+ }
+
+ public SetReceiveBufferSize() throws Exception {
+ ServerSocket ss = new ServerSocket(0);
+ Server serv = new Server(ss);
+ serv.start();
+ Socket s = new Socket("localhost", ss.getLocalPort());
+ try {
+ s.setReceiveBufferSize(0);
+ } catch (IllegalArgumentException e) {
+ return;
+ } catch (Exception ex) {
+ } finally {
+ ss.close();
+ }
+ throw new RuntimeException("IllegalArgumentException not thrown!");
+ }
+}
diff --git a/test/java/net/Socket/SetSoLinger.java b/test/java/net/Socket/SetSoLinger.java
new file mode 100644
index 0000000..f1d10ff
--- /dev/null
+++ b/test/java/net/Socket/SetSoLinger.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright 1998 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4151834
+ * @summary Test Socket.setSoLinger
+ *
+ */
+
+import java.net.*;
+
+public class SetSoLinger implements Runnable {
+ static ServerSocket ss;
+ static InetAddress addr;
+ static int port;
+
+ public static void main(String args[]) throws Exception {
+ boolean error = true;
+ int linger = 65546;
+ int value = 0;
+ addr = InetAddress.getLocalHost();
+ ss = new ServerSocket(0);
+ port = ss.getLocalPort();
+
+ Thread t = new Thread(new SetSoLinger());
+ t.start();
+ Socket soc = ss.accept();
+ soc.setSoLinger(true, linger);
+ value = soc.getSoLinger();
+ soc.close();
+
+ if(value != 65535)
+ throw new RuntimeException("Failed. Value not properly reduced.");
+ }
+
+ public void run() {
+ try {
+ Socket s = new Socket(addr, port);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+}
diff --git a/test/java/net/Socket/ShutdownBoth.java b/test/java/net/Socket/ShutdownBoth.java
new file mode 100644
index 0000000..d22261c
--- /dev/null
+++ b/test/java/net/Socket/ShutdownBoth.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4425485
+ * @summary Check that shutdownInput followed by shutdownOutput
+ * doesn't throw an exception.
+ */
+import java.net.*;
+
+public class ShutdownBoth {
+
+ public static void main(String args[]) throws Exception {
+ ServerSocket ss = new ServerSocket(0);
+ Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
+ Socket s2 = ss.accept();
+
+ s1.shutdownInput();
+ s1.shutdownOutput(); // failed b55
+
+ s1.close();
+ s2.close();
+ ss.close();
+ }
+
+}
diff --git a/test/java/net/Socket/SoTimeout.java b/test/java/net/Socket/SoTimeout.java
new file mode 100644
index 0000000..82cee0f
--- /dev/null
+++ b/test/java/net/Socket/SoTimeout.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright 1998 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/* @test
+ @bug 4156625
+ @summary Socket.setSoTimeout(T) can cause incorrect delay of T
+ under green threads
+ @author Tom Rodriguez
+ */
+
+/*
+ * This program depends a bit on the particular behaviour of the green
+ * threads scheduler to produce the problem, but given that the underlying
+ * bug a green threads bug, I think that's OK.
+ */
+
+import java.net.*;
+
+public class SoTimeout implements Runnable {
+ static ServerSocket serverSocket;
+ static long timeWritten;
+ static InetAddress addr;
+ static int port;
+
+ public static void main(String[] args) throws Exception {
+ addr = InetAddress.getLocalHost();
+ serverSocket = new ServerSocket(0);
+ port = serverSocket.getLocalPort();
+
+ byte[] b = new byte[12];
+ Thread t = new Thread(new SoTimeout());
+ t.start();
+
+ Socket s = serverSocket.accept();
+
+ // set a 1 second timeout on the socket
+ s.setSoTimeout(1000);
+
+ s.getInputStream().read(b, 0, b.length);
+ s.close();
+
+ long waited = System.currentTimeMillis() - timeWritten;
+
+ // this sequence should complete fairly quickly and if it
+ // takes something resembling the the SoTimeout value then
+ // we are probably incorrectly blocking and not waking up
+ if (waited > 500) {
+ throw new Exception("shouldn't take " + waited + " to complete");
+ }
+ }
+
+ public void run() {
+ try {
+ byte[] b = new byte[12];
+ Socket s = new Socket(addr, port);
+
+ Thread.yield();
+ timeWritten = System.currentTimeMillis();
+ s.getOutputStream().write(b, 0, 12);
+ s.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+}
diff --git a/test/java/net/Socket/SocketImplTest.java b/test/java/net/Socket/SocketImplTest.java
new file mode 100644
index 0000000..23e6cc0
--- /dev/null
+++ b/test/java/net/Socket/SocketImplTest.java
@@ -0,0 +1,186 @@
+/*
+ * Copyright 2002 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+import java.applet.Applet;
+import java.io.*;
+import java.net.*;
+
+/**
+ * Simple Applet for exposing the Socket constructor
+ * bug.
+ */
+public class SocketImplTest extends Applet {
+
+ static public void main(String[] args) {
+ System.setSecurityManager(new SecurityManager());
+ SocketImplTest s = new SocketImplTest();
+ s.init();
+ s.start();
+ }
+
+
+ /**
+ * A no-op SocketImpl descendant.
+ */
+ class MySocketImpl extends SocketImpl {
+ protected void accept(SocketImpl impl) throws IOException {
+ }
+
+ protected int available(){
+ return 0;
+ }
+
+ protected void bind(InetAddress host, int port){
+ }
+
+ protected void close(){
+ }
+
+ protected void connect(InetAddress address, int port){
+ }
+
+ protected void connect(String host, int port){
+ }
+
+ protected void connect(SocketAddress a, int t) throws IOException {
+ }
+
+
+ protected void create(boolean stream){
+ }
+
+ protected InputStream getInputStream(){
+ return null;
+ }
+
+ protected OutputStream getOutputStream(){
+ return null;
+ }
+
+ protected void listen(int backlog){
+ }
+
+ public Object getOption(int optID){
+ return null;
+ }
+
+ public void setOption(int optID, Object value){
+ }
+
+ protected void sendUrgentData(int i){
+ }
+ }
+
+ class MyDatagramSocketImpl extends DatagramSocketImpl {
+ protected void create() throws SocketException {
+ }
+
+ protected void bind(int lport, InetAddress laddr) throws SocketException {
+ }
+
+ protected void send(DatagramPacket p) throws IOException {
+ }
+
+ protected int peek(InetAddress i) throws IOException {
+ return 0;
+ }
+
+ protected int peekData(DatagramPacket p) throws IOException {
+ return 0;
+ }
+
+ protected void receive(DatagramPacket p) throws IOException {
+ }
+
+ protected void setTTL(byte ttl) throws IOException {
+ }
+
+ protected byte getTTL() throws IOException {
+ return 0;
+ }
+
+ protected void setTimeToLive(int ttl) throws IOException {
+ }
+
+ protected int getTimeToLive() throws IOException {
+ return 0;
+ }
+
+ protected void join(InetAddress inetaddr) throws IOException {
+ }
+
+ protected void leave(InetAddress inetaddr) throws IOException {
+ }
+
+ protected void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)
+ throws IOException {
+ }
+
+ protected void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf)
+ throws IOException {
+ }
+
+ protected void close() {
+ }
+
+ public Object getOption(int optID){
+ return null;
+ }
+
+ public void setOption(int optID, Object value){
+ }
+
+ }
+
+ /**
+ * A no-op Socket descendant.
+ */
+ class MySocket extends Socket {
+ public MySocket(SocketImpl impl) throws IOException {
+ super(impl);
+ }
+ }
+
+ class MyDatagramSocket extends DatagramSocket {
+ public MyDatagramSocket(DatagramSocketImpl impl) {
+ super(impl);
+ }
+ }
+
+ /**
+ * Our test case entrypoint. Generates
+ * a SecurityException.
+ */
+ public void init(){
+ MySocketImpl socketImpl = new MySocketImpl();
+ MyDatagramSocketImpl dgramSocketImpl = new MyDatagramSocketImpl();
+
+ try{
+ MySocket socko = new MySocket(socketImpl);
+ MyDatagramSocket dsock = new MyDatagramSocket(dgramSocketImpl);
+ } catch(IOException ioex){
+ System.err.println(ioex);
+ } catch(SecurityException sec) {
+ throw new RuntimeException("Failed. Creation of socket throwing SecurityException: ");
+ }
+ }
+}
diff --git a/test/java/net/Socket/TestAfterClose.java b/test/java/net/Socket/TestAfterClose.java
new file mode 100644
index 0000000..61c19cf
--- /dev/null
+++ b/test/java/net/Socket/TestAfterClose.java
@@ -0,0 +1,110 @@
+/*
+ * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6505016
+ * @summary Socket spec should clarify what getInetAddress/getPort/etc return after the Socket is closed
+ */
+
+import java.net.*;
+import java.io.*;
+
+public class TestAfterClose
+{
+ static int failCount;
+
+ public static void main(String[] args) {
+ try {
+ ServerSocket ss = new ServerSocket(0, 0, null);
+ Socket socket = new Socket("localhost", ss.getLocalPort());
+ ss.accept();
+ ss.close();
+ test(socket);
+ } catch (IOException ioe) {
+ ioe.printStackTrace();
+ }
+
+ if (failCount > 0)
+ throw new RuntimeException("Failed: failcount = " + failCount);
+
+ }
+
+ static void test(Socket socket) throws IOException {
+ //Before Close
+ int socketPort = socket.getPort();
+ InetAddress socketInetAddress = socket.getInetAddress();
+ SocketAddress socketRemoteSocketAddress = socket.getRemoteSocketAddress();
+ int socketLocalPort = socket.getLocalPort();
+
+ //After Close
+ socket.close();
+
+ if (socketPort != socket.getPort()) {
+ System.out.println("Socket.getPort failed");
+ failCount++;
+ }
+
+ if (!socket.getInetAddress().equals(socketInetAddress)) {
+ System.out.println("Socket.getInetAddress failed");
+ failCount++;
+ }
+
+ if (!socket.getRemoteSocketAddress().equals(socketRemoteSocketAddress)) {
+ System.out.println("Socket.getRemoteSocketAddresss failed");
+ failCount++;
+ }
+
+ if (socketLocalPort != socket.getLocalPort()) {
+ System.out.println("Socket.getLocalPort failed");
+ failCount++;
+ }
+
+ InetAddress anyAddr = null;
+ try {
+ anyAddr = InetAddress.getByAddress("",new byte[] {0,0,0,0});
+ } catch (UnknownHostException uhe) {
+ }
+
+ if (anyAddr != null && !socket.getLocalAddress().equals(anyAddr)) {
+ System.out.println("Socket.getLocalAddress failed");
+ failCount++;
+ }
+
+ InetSocketAddress addr = new InetSocketAddress(socket.getLocalPort());
+ if (!socket.getLocalSocketAddress().equals(addr)) {
+ System.out.println("Socket.getLocalSocketAddress failed");
+ failCount++;
+ }
+
+ if (!socket.isConnected()) {
+ System.out.println("Socket.isConnected failed");
+ failCount++;
+ }
+
+ if (!socket.isBound()) {
+ System.out.println("Socket.isBound failed");
+ failCount++;
+ }
+ }
+}
diff --git a/test/java/net/Socket/TestClose.java b/test/java/net/Socket/TestClose.java
new file mode 100644
index 0000000..b32c57c
--- /dev/null
+++ b/test/java/net/Socket/TestClose.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+/*
+ * @test
+ *
+ * @bug 4408755
+ *
+ * @summary This tests wether it's possible to get some informations
+ * out of a closed socket. This is for backward compatibility purposes.
+ */
+
+import java.net.*;
+
+public class TestClose {
+
+ public static void main(String[] args) throws Exception {
+ ServerSocket ss;
+ Socket s;
+ InetAddress ad1, ad2;
+ int port1, port2, serverport;
+
+ ss = new ServerSocket(0);
+ serverport = ss.getLocalPort();
+ s = new Socket("localhost", serverport);
+ s.close();
+ ss.close();
+ ad1 = ss.getInetAddress();
+ if (ad1 == null)
+ throw new RuntimeException("ServerSocket.getInetAddress() returned null");
+ port1 = ss.getLocalPort();
+ if (port1 != serverport)
+ throw new RuntimeException("ServerSocket.getLocalPort() returned the wrong value");
+ ad2 = s.getInetAddress();
+ if (ad2 == null)
+ throw new RuntimeException("Socket.getInetAddress() returned null");
+ port2 = s.getPort();
+ if (port2 != serverport)
+ throw new RuntimeException("Socket.getPort() returned wrong value");
+ ad2 = s.getLocalAddress();
+ if (ad2 == null)
+ throw new RuntimeException("Socket.getLocalAddress() returned null");
+ port2 = s.getLocalPort();
+ if (port2 == -1)
+ throw new RuntimeException("Socket.getLocalPort returned -1");
+ }
+}
diff --git a/test/java/net/Socket/TestTcpNoDelay.java b/test/java/net/Socket/TestTcpNoDelay.java
new file mode 100644
index 0000000..1f02187
--- /dev/null
+++ b/test/java/net/Socket/TestTcpNoDelay.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2006-2007 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/**
+ * @test
+ * @bug 6404388
+ * @summary VISTA: Socket setTcpNoDelay & setKeepAlive working incorrectly
+ */
+
+import java.net.*;
+import java.io.IOException;
+
+public class TestTcpNoDelay
+{
+ public static void main(String[] args) {
+ try {
+ Socket socket = new Socket();
+ boolean on = socket.getTcpNoDelay();
+ System.out.println("Get TCP_NODELAY = " + on);
+
+ boolean opposite = on ? false: true;
+ System.out.println("Set TCP_NODELAY to " + opposite);
+ socket.setTcpNoDelay(opposite);
+
+ boolean noDelay = socket.getTcpNoDelay();
+ System.out.println("Get TCP_NODELAY = " + noDelay);
+
+ if (noDelay != opposite)
+ throw new RuntimeException("setTcpNoDelay no working as expected");
+
+ } catch (IOException e){
+ e.printStackTrace();
+ }
+ }
+
+}
diff --git a/test/java/net/Socket/Timeout.java b/test/java/net/Socket/Timeout.java
new file mode 100644
index 0000000..b7aaf98
--- /dev/null
+++ b/test/java/net/Socket/Timeout.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 1998-2000 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/**
+ * @test
+ * @bug 4163126
+ * @summary test to see if timeout hangs
+ * @run main/timeout=15 Timeout
+ */
+import java.net.*;
+import java.io.*;
+
+public class Timeout {
+
+ public static ServerSocket sock;
+
+ public static void main(String[] args) throws Exception {
+ boolean success = false;
+ try {
+ ServerSocket sock;
+ sock = new ServerSocket(2333);
+ sock.setSoTimeout(2);
+ sock.accept();
+ } catch (InterruptedIOException e) {
+ success = true;
+ }
+ if (!success)
+ throw new RuntimeException("Socket timeout failure.");
+ }
+}
diff --git a/test/java/net/Socket/TrafficClass.java b/test/java/net/Socket/TrafficClass.java
new file mode 100644
index 0000000..ce3a24a
--- /dev/null
+++ b/test/java/net/Socket/TrafficClass.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4511783
+ * @summary Test that setTrafficClass/getTraffiClass don't
+ * throw an exception
+ */
+import java.net.*;
+import java.nio.*;
+import java.nio.channels.*;
+
+public class TrafficClass {
+
+ static final int IPTOS_RELIABILITY = 0x4;
+
+ static int failures = 0;
+
+ static void testDatagramSocket(DatagramSocket s) {
+ try {
+ s.setTrafficClass( IPTOS_RELIABILITY );
+ int tc = s.getTrafficClass();
+ } catch (Exception e) {
+ failures++;
+ System.err.println("testDatagramSocket failed: " + e);
+ }
+ }
+
+ static void testSocket(Socket s) {
+ try {
+ s.setTrafficClass(IPTOS_RELIABILITY);
+ int tc = s.getTrafficClass();
+ } catch (Exception e) {
+ failures++;
+ System.err.println("testSocket failed: " + e);
+ }
+
+ }
+
+ public static void main(String args[]) throws Exception {
+
+ DatagramSocket ds = new DatagramSocket();
+ testDatagramSocket(ds);
+
+ DatagramChannel dc = DatagramChannel.open();
+ testDatagramSocket(dc.socket());
+
+ Socket s = new Socket();
+ testSocket(s);
+
+ SocketChannel sc = SocketChannel.open();
+ testSocket(sc.socket());
+
+ if (failures > 0) {
+ throw new Exception(failures + " sub-test(s) failed - " +
+ "see log for details.");
+ }
+ }
+
+}
diff --git a/test/java/net/Socket/UrgentDataTest.java b/test/java/net/Socket/UrgentDataTest.java
new file mode 100644
index 0000000..2aac077
--- /dev/null
+++ b/test/java/net/Socket/UrgentDataTest.java
@@ -0,0 +1,152 @@
+/*
+ * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4092038
+ * @summary TCP Urgent data support
+ *
+ */
+
+import java.net.*;
+import java.io.*;
+
+public class UrgentDataTest {
+
+ Object opref;
+ boolean isClient, isServer;
+ String clHost;
+ ServerSocket listener;
+ Socket client, server;
+ int clPort;
+ InputStream clis, sis;
+ OutputStream clos, sos;
+
+ static void usage () {
+ System.out.println (" usage: java UrgentDataTest <runs client and server together>");
+ System.out.println (" usage: java UrgentDataTest -server <runs server alone>");
+ System.out.println (" usage: java UrgentDataTest -client host port <runs client and connects to"+
+ " specified server>");
+ }
+
+ public static void main (String args[]) {
+ try {
+ UrgentDataTest test = new UrgentDataTest ();
+ if (args.length == 0) {
+ test.listener = new ServerSocket (0);
+ test.isClient = true;
+ test.isServer = true;
+ test.clHost = "127.0.0.1";
+ test.clPort = test.listener.getLocalPort();
+ test.run();
+ } else if (args[0].equals ("-server")) {
+ test.listener = new ServerSocket (0);
+ System.out.println ("Server listening on port " + test.listener.getLocalPort());
+ test.isClient = false;
+ test.isServer = true;
+ test.run();
+ System.out.println ("Server: Completed OK");
+ } else if (args[0].equals ("-client")) {
+ test.isClient = true;
+ test.isServer = false;
+ test.clHost = args [1];
+ test.clPort = Integer.parseInt (args[2]);
+ test.run();
+ System.out.println ("Client: Completed OK");
+ } else {
+ usage ();
+ }
+ }
+ catch (ArrayIndexOutOfBoundsException e) {
+ usage();
+ }
+ catch (NumberFormatException e) {
+ usage();
+ }
+ catch (Exception e) {
+ e.printStackTrace ();
+ throw new RuntimeException ("Exception caught");
+ }
+ }
+
+ public void run () throws Exception {
+ if (isClient) {
+ client = new Socket (clHost, clPort);
+ clis = client.getInputStream();
+ clos = client.getOutputStream();
+ client.setOOBInline (true);
+ if (client.getOOBInline() != true) {
+ throw new RuntimeException ("Setting OOBINLINE failed");
+ }
+ }
+ if (isServer) {
+ server = listener.accept ();
+ sis = server.getInputStream();
+ sos = server.getOutputStream();
+ }
+ if (isClient) {
+ clos.write ("Hello".getBytes ());
+ client.sendUrgentData (100);
+ clos.write ("world".getBytes ());
+ }
+ // read Hello world from server (during which oob byte must have been dropped)
+ String s = "Helloworld";
+ if (isServer) {
+ for (int y=0; y<s.length(); y++) {
+ int c = sis.read ();
+ if (c != (int)s.charAt (y)) {
+ throw new RuntimeException ("Unexpected character read");
+ }
+ }
+ // Do the same from server to client
+ sos.write ("Hello".getBytes ());
+ server.sendUrgentData (101);
+ sos.write ("World".getBytes ());
+ }
+ if (isClient) {
+ // read Hello world from client (during which oob byte must have been read)
+ s="Hello";
+ for (int y=0; y<s.length(); y++) {
+ int c = clis.read ();
+ if (c != (int)s.charAt (y)) {
+ throw new RuntimeException ("Unexpected character read");
+ }
+ }
+ if (clis.read() != 101) {
+ throw new RuntimeException ("OOB byte not received");
+ }
+ s="World";
+ for (int y=0; y<s.length(); y++) {
+ int c = clis.read ();
+ if (c != (int)s.charAt (y)) {
+ throw new RuntimeException ("Unexpected character read");
+ }
+ }
+ }
+
+ if (isClient)
+ client.close ();
+ if (isServer)
+ server.close ();
+ }
+}
diff --git a/test/java/net/Socket/asyncClose/AsyncClose.java b/test/java/net/Socket/asyncClose/AsyncClose.java
new file mode 100644
index 0000000..793d62d
--- /dev/null
+++ b/test/java/net/Socket/asyncClose/AsyncClose.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4344135
+ * @summary Check that {Socket,ServerSocket,DatagramSocket}.close will
+ * cause any thread blocked on the socket to throw a SocketException.
+ * @run main/timeout=60 AsyncClose
+ */
+import java.net.*;
+import java.io.*;
+
+public class AsyncClose {
+
+ public static void main(String args[]) throws Exception {
+
+ AsyncCloseTest tests[] = {
+ new Socket_getInputStream_read(),
+ new Socket_getInputStream_read(5000),
+ new Socket_getOutputStream_write(),
+ new DatagramSocket_receive(),
+ new DatagramSocket_receive(5000),
+ new ServerSocket_accept(),
+ new ServerSocket_accept(5000),
+ };
+
+ int failures = 0;
+
+ for (int i=0; i<tests.length; i++) {
+ AsyncCloseTest tst = tests[i];
+
+ System.out.println("******************************");
+ System.out.println("Test: " + tst.description());
+
+ if (tst.go()) {
+ System.out.println("Passed.");
+ } else {
+ System.out.println("Failed: " + tst.failureReason());
+ failures++;
+ }
+ System.out.println("");
+
+ }
+
+ if (failures > 0) {
+ throw new Exception(failures + " sub-tests failed - see log.");
+ }
+ }
+}
diff --git a/test/java/net/Socket/asyncClose/AsyncCloseTest.java b/test/java/net/Socket/asyncClose/AsyncCloseTest.java
new file mode 100644
index 0000000..c42e448
--- /dev/null
+++ b/test/java/net/Socket/asyncClose/AsyncCloseTest.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * Abstract class representing an asynchronous close test - subclasses
+ * should override description() and go() methods.
+ */
+public abstract class AsyncCloseTest {
+
+ public abstract String description();
+
+ public abstract boolean go() throws Exception;
+
+
+ protected synchronized void failed(String reason) {
+ this.reason = reason;
+ }
+
+ public synchronized String failureReason() {
+ return reason;
+ }
+
+ protected synchronized void closed() {
+ closed = true;
+ }
+
+ protected synchronized boolean isClosed() {
+ return closed;
+ }
+
+ private String reason;
+ private boolean closed;
+
+}
diff --git a/test/java/net/Socket/asyncClose/BrokenPipe.java b/test/java/net/Socket/asyncClose/BrokenPipe.java
new file mode 100644
index 0000000..8edb909
--- /dev/null
+++ b/test/java/net/Socket/asyncClose/BrokenPipe.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test 1.1 01/09/19
+ * @bug 4511404
+ * @summary Check that a broken pipe error doesn't throw an exception
+ * indicating the socket is closed.
+ */
+import java.io.*;
+import java.net.*;
+
+public class BrokenPipe {
+
+ private static class Closer implements Runnable {
+ private final Socket s;
+
+ Closer(Socket s) {
+ this.s = s;
+ }
+
+ public void run() {
+ try {
+ /* gives time for 'write' to block */
+ Thread.sleep(5000);
+ s.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ public static void main(String[] args) throws Exception {
+ ServerSocket ss = new ServerSocket(0);
+ Socket client = new Socket(InetAddress.getLocalHost(),
+ ss.getLocalPort());
+ Socket server = ss.accept();
+ ss.close();
+ new Thread(new Closer(server)).start();
+
+ try {
+ client.getOutputStream().write(new byte[1000000]);
+ } catch (IOException ioe) {
+
+ /*
+ * Check that the exception text doesn't indicate the
+ * socket is closed. In tiger we should be able to
+ * replace this by catching a more specific exception.
+ */
+ String text = ioe.getMessage();
+ if (text.toLowerCase().indexOf("closed") >= 0) {
+ throw ioe;
+ }
+ }
+ server.close();
+ }
+
+}
diff --git a/test/java/net/Socket/asyncClose/DatagramSocket_receive.java b/test/java/net/Socket/asyncClose/DatagramSocket_receive.java
new file mode 100644
index 0000000..39494ad
--- /dev/null
+++ b/test/java/net/Socket/asyncClose/DatagramSocket_receive.java
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * Tests that a thread blocked in DatagramSocket.receive
+ * throws a SocketException if the socket is asynchronously closed.
+ */
+import java.net.*;
+
+public class DatagramSocket_receive extends AsyncCloseTest implements Runnable {
+ DatagramSocket s;
+ int timeout = 0;
+
+ public DatagramSocket_receive() {
+ }
+
+ public DatagramSocket_receive(int timeout) {
+ this.timeout = timeout;
+ }
+
+ public String description() {
+ String s = "DatagramSocket.receive";
+ if (timeout > 0) {
+ s += " (timeout specified)";
+ }
+ return s;
+ }
+
+ public void run() {
+ DatagramPacket p;
+ try {
+
+ byte b[] = new byte[1024];
+ p = new DatagramPacket(b, b.length);
+
+ if (timeout > 0) {
+ s.setSoTimeout(timeout);
+ }
+ } catch (Exception e) {
+ failed(e.getMessage());
+ return;
+ }
+
+ try {
+ s.receive(p);
+ } catch (SocketException se) {
+ closed();
+ } catch (Exception e) {
+ failed(e.getMessage());
+ }
+ }
+
+ public boolean go() throws Exception {
+ s = new DatagramSocket();
+
+ Thread thr = new Thread(this);
+ thr.start();
+
+ Thread.currentThread().sleep(1000);
+
+ s.close();
+
+ Thread.currentThread().sleep(1000);
+
+ if (isClosed()) {
+ return true;
+ } else {
+ failed("DatagramSocket.receive wasn't preempted");
+ return false;
+ }
+ }
+}
diff --git a/test/java/net/Socket/asyncClose/ServerSocket_accept.java b/test/java/net/Socket/asyncClose/ServerSocket_accept.java
new file mode 100644
index 0000000..cce1134
--- /dev/null
+++ b/test/java/net/Socket/asyncClose/ServerSocket_accept.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * Tests that a thread blocked in ServerSocket.accept
+ * throws a SocketException if the socket is asynchronously closed.
+ */
+import java.net.*;
+
+public class ServerSocket_accept extends AsyncCloseTest implements Runnable {
+ ServerSocket ss;
+ int timeout = 0;
+
+ public ServerSocket_accept() {
+ }
+
+ public ServerSocket_accept(int timeout) {
+ this.timeout = timeout;
+ }
+
+ public String description() {
+ String s = "ServerSocket.accept()";
+ if (timeout > 0) {
+ s += " (with timeout)";
+ }
+ return s;
+ }
+
+ public void run() {
+ try {
+ Socket s = ss.accept();
+ } catch (SocketException se) {
+ closed();
+ } catch (Exception e) {
+ failed(e.getMessage());
+ }
+ }
+
+ public boolean go() throws Exception {
+ ss = new ServerSocket(0);
+
+ Thread thr = new Thread(this);
+ thr.start();
+
+ Thread.currentThread().sleep(1000);
+
+ ss.close();
+
+ Thread.currentThread().sleep(1000);
+
+ if (isClosed()) {
+ return true;
+ } else {
+ failed("ServerSocket.accept() wasn't preempted");
+ return false;
+ }
+ }
+}
diff --git a/test/java/net/Socket/asyncClose/Socket_getInputStream_read.java b/test/java/net/Socket/asyncClose/Socket_getInputStream_read.java
new file mode 100644
index 0000000..1ecab7e
--- /dev/null
+++ b/test/java/net/Socket/asyncClose/Socket_getInputStream_read.java
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * Tests that a thread blocked in Socket.getInputStream().read()
+ * throws a SocketException if the socket is asynchronously closed.
+ */
+import java.net.*;
+import java.io.*;
+
+public class Socket_getInputStream_read extends AsyncCloseTest implements Runnable {
+ Socket s;
+ int timeout = 0;
+
+ public Socket_getInputStream_read() {
+ }
+
+ public Socket_getInputStream_read(int timeout) {
+ this.timeout = timeout;
+ }
+
+ public String description() {
+ String s = "Socket.getInputStream().read()";
+ if (timeout > 0) {
+ s += " (with timeout)";
+ }
+ return s;
+ }
+
+ public void run() {
+ InputStream in;
+
+ try {
+ in = s.getInputStream();
+ if (timeout > 0) {
+ s.setSoTimeout(timeout);
+ }
+ } catch (Exception e) {
+ failed(e.getMessage());
+ return;
+ }
+
+ try {
+ int n = in.read();
+ failed("getInptuStream().read() returned unexpectly!!");
+ } catch (SocketException se) {
+ closed();
+ } catch (Exception e) {
+ failed(e.getMessage());
+ }
+ }
+
+ public boolean go() throws Exception {
+
+ ServerSocket ss = new ServerSocket(0);
+
+ InetAddress lh = InetAddress.getLocalHost();
+ s = new Socket();
+ s.connect( new InetSocketAddress(lh, ss.getLocalPort()) );
+
+ Socket s2 = ss.accept();
+
+ Thread thr = new Thread(this);
+ thr.start();
+
+ Thread.currentThread().sleep(1000);
+
+ s.close();
+
+ Thread.currentThread().sleep(1000);
+
+ if (isClosed()) {
+ return true;
+ } else {
+ failed("getInputStream().read() wasn't preempted");
+ return false;
+ }
+
+ }
+}
diff --git a/test/java/net/Socket/asyncClose/Socket_getOutputStream_write.java b/test/java/net/Socket/asyncClose/Socket_getOutputStream_write.java
new file mode 100644
index 0000000..ed09c87
--- /dev/null
+++ b/test/java/net/Socket/asyncClose/Socket_getOutputStream_write.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * Tests that a thread blocked in Socket.getOutputStream().write()
+ * throws a SocketException if the socket is asynchronously closed.
+ */
+import java.net.*;
+import java.io.*;
+
+public class Socket_getOutputStream_write extends AsyncCloseTest implements Runnable {
+ Socket s;
+
+ public String description() {
+ return "Socket.getOutputStream().write()";
+ }
+
+ public void run() {
+ try {
+ OutputStream out = s.getOutputStream();
+ for (;;) {
+ byte b[] = new byte[8192];
+ out.write(b);
+ }
+ } catch (SocketException se) {
+ closed();
+ } catch (Exception e) {
+ failed(e.getMessage());
+ }
+ }
+
+ public boolean go() throws Exception {
+ ServerSocket ss = new ServerSocket(0);
+
+ InetAddress lh = InetAddress.getLocalHost();
+ s = new Socket();
+ s.connect( new InetSocketAddress(lh, ss.getLocalPort()) );
+
+ Socket s2 = ss.accept();
+
+ Thread thr = new Thread(this);
+ thr.start();
+
+ Thread.currentThread().sleep(2000);
+
+ s.close();
+
+ Thread.currentThread().sleep(2000);
+
+ if (isClosed()) {
+ return true;
+ } else {
+ failed("getOutputStream().write() wasn't preempted");
+ return false;
+ }
+ }
+}
diff --git a/test/java/net/Socket/reset/Test.java b/test/java/net/Socket/reset/Test.java
new file mode 100644
index 0000000..51f7df7
--- /dev/null
+++ b/test/java/net/Socket/reset/Test.java
@@ -0,0 +1,241 @@
+/*
+ * Copyright 2002 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @bug 4517622
+ * @summary SocketException on first read after error; -1 on subsequent reads
+ */
+import java.net.*;
+import java.io.*;
+import java.util.Random;
+
+public class Test {
+
+ static int TEST_COMBINATIONS = 5;
+
+ // test server that cycles through each combination of response
+
+ static class Server extends Thread {
+ ServerSocket ss;
+
+ public Server() throws IOException {
+ ss = new ServerSocket(0);
+ System.out.println("Server listening on port: " + getPort());
+ }
+
+ public void run() {
+
+ int testCombination = 0;
+
+ try {
+ for (;;) {
+ Socket s = ss.accept();
+
+ switch (testCombination) {
+ case 0:
+ s.setTcpNoDelay(false);
+ s.getOutputStream().write(new byte[256]);
+ s.setSoLinger(true, 0);
+ break;
+
+ case 1:
+ s.setTcpNoDelay(true);
+ s.getOutputStream().write(new byte[256]);
+ s.setSoLinger(true, 0);
+ break;
+
+ case 2:
+ s.getOutputStream().write("hello".getBytes());
+ s.setSoLinger(true, 0);
+ break;
+
+ case 3:
+ break; /* EOF test */
+
+ case 4:
+ s.getOutputStream().write(new byte[256]);
+ break;
+ }
+
+ s.close();
+
+ testCombination = (testCombination + 1) % TEST_COMBINATIONS;
+ }
+ } catch (IOException ioe) {
+ if (!ss.isClosed()) {
+ System.err.println("Server failed: " + ioe);
+ }
+ }
+ }
+
+ public int getPort() {
+ return ss.getLocalPort();
+ }
+
+ public void shutdown() {
+ try {
+ ss.close();
+ } catch (IOException ioe) { }
+ }
+
+ }
+
+ static final int STATE_DATA = 0;
+ static final int STATE_EOF = 1;
+ static final int STATE_IOE = 2;
+
+ static void Test(SocketAddress sa) throws Exception {
+ System.out.println("-----------");
+
+ Socket s = new Socket();
+ s.connect(sa);
+
+ byte b[] = new byte[50];
+ int state = STATE_DATA;
+ boolean failed = false;
+
+ Random rand = new Random();
+
+ for (int i=0; i<200; i++) {
+ switch (rand.nextInt(4)) {
+ case 0:
+ try {
+ s.getOutputStream().write("data".getBytes());
+ } catch (IOException ioe) { }
+ break;
+
+ case 1:
+ try {
+ int n = s.getInputStream().available();
+
+ // available should never return > 0 if read
+ // has already thrown IOE or returned EOF
+
+ if (n > 0 && state != STATE_DATA) {
+ System.out.println("FAILED!! available: " + n +
+ " (unexpected as IOE or EOF already received)");
+ failed = true;
+ }
+ } catch (IOException ioe) {
+ System.out.println("FAILED!!! available: " + ioe);
+ failed = true;
+ }
+ break;
+
+ case 2:
+ try {
+ int n = s.getInputStream().read(b);
+
+ if (n > 0 && state == STATE_IOE) {
+ System.out.println("FAILED!! read: " + n +
+ " (unexpected as IOE already thrown)");
+ failed = true;
+ }
+
+ if (n > 0 && state == STATE_EOF) {
+ System.out.println("FAILED!! read: " + n +
+ " (unexpected as EOF already received)");
+ failed = true;
+ }
+
+ if (n < 0) {
+ if (state == STATE_IOE) {
+ System.out.println("FAILED!! read: EOF " +
+ " (unexpected as IOE already thrown)");
+ failed = true;
+ }
+ if (state != STATE_EOF) {
+ System.out.println("read: EOF");
+ state = STATE_EOF;
+ }
+ }
+
+ } catch (IOException ioe) {
+ if (state == STATE_EOF) {
+ System.out.println("FAILED!! read: " + ioe +
+ " (unexpected as EOF already received)");
+ failed = true;
+ }
+ if (state != STATE_IOE) {
+ System.out.println("read: " + ioe);
+ state = STATE_IOE;
+ }
+ }
+ break;
+
+ case 3:
+ try {
+ Thread.currentThread().sleep(100);
+ } catch (Exception ie) { }
+ }
+
+ if (failed) {
+ failures++;
+ break;
+ }
+ }
+
+ s.close();
+ }
+
+ static int failures = 0;
+
+ public static void main(String args[]) throws Exception {
+ SocketAddress sa = null;
+ Server svr = null;
+
+ // server mode only
+ if (args.length > 0) {
+ if (args[0].equals("-server")) {
+ svr = new Server();
+ svr.start();
+ return;
+ }
+ }
+
+ // run standalone or connect to remote server
+ if (args.length > 0) {
+ InetAddress rh = InetAddress.getByName(args[0]);
+ int port = Integer.parseInt(args[1]);
+ sa = new InetSocketAddress(rh, port);
+ } else {
+ svr = new Server();
+ svr.start();
+
+ InetAddress lh = InetAddress.getLocalHost();
+ sa = new InetSocketAddress(lh, svr.getPort());
+ }
+
+ for (int i=0; i<10; i++) {
+ Test(sa);
+ }
+
+ if (svr != null) {
+ svr.shutdown();
+ }
+
+ if (failures > 0) {
+ throw new Exception(failures + " sub-test(s) failed.");
+ }
+ }
+}
diff --git a/test/java/net/Socket/setReuseAddress/Basic.java b/test/java/net/Socket/setReuseAddress/Basic.java
new file mode 100644
index 0000000..6c77237
--- /dev/null
+++ b/test/java/net/Socket/setReuseAddress/Basic.java
@@ -0,0 +1,219 @@
+/*
+ * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4476378
+ * @summary Check the specific behaviour of the setReuseAddress(boolean)
+ * method.
+ */
+import java.net.*;
+
+public class Basic {
+
+ static int testCount = 0;
+ static int failures = 0;
+
+ void test(String msg) {
+ testCount++;
+ System.out.println("***************************************");
+ System.out.println("Test " + testCount + ": " + msg);
+ }
+
+ void passed() {
+ System.out.println("Test passed.");
+ }
+
+ void failed() {
+ failures++;
+ System.out.println("Test failed.");
+ }
+
+ void check(boolean pass) {
+ if (pass) {
+ passed();
+ } else {
+ failed();
+ }
+ }
+
+ void SocketTests() throws Exception {
+ Socket s1 = new Socket();
+
+ test("Socket should be created with SO_REUSEADDR disabled");
+ check(!s1.getReuseAddress());
+
+ test("Socket.setReuseAddress(true)");
+ s1.setReuseAddress(true);
+ check(s1.getReuseAddress());
+
+ test("Socket.setReuseAddress(false)");
+ s1.setReuseAddress(false);
+ check(!s1.getReuseAddress() );
+
+ /* bind to any port */
+ s1.bind( new InetSocketAddress(0) );
+
+ test("Binding Socket to port already in use should throw " +
+ "a BindException");
+ Socket s2 = new Socket();
+ try {
+ s2.bind( new InetSocketAddress(s1.getLocalPort()) );
+ failed();
+ } catch (BindException e) {
+ passed();
+ }
+ s2.close();
+
+ s1.close();
+ }
+
+ void ServerSocketTests() throws Exception {
+ ServerSocket s1 = new ServerSocket();
+
+ test("ServerSocket.setReuseAddress(true)");
+ s1.setReuseAddress(true);
+ check(s1.getReuseAddress());
+
+ test("Socket.setReuseAddress(false)");
+ s1.setReuseAddress(false);
+ check(!s1.getReuseAddress() );
+
+ /* bind to any port */
+ s1.bind( new InetSocketAddress(0) );
+
+ test("Binding ServerSocket to port already in use should throw " +
+ "a BindException");
+ ServerSocket s2 = new ServerSocket();
+ try {
+ s2.bind( new InetSocketAddress(s1.getLocalPort()) );
+ failed();
+ } catch (BindException e) {
+ passed();
+ }
+ s2.close();
+
+ s1.close();
+ }
+
+ void DatagramSocketTests() throws Exception {
+ DatagramSocket s1 = new DatagramSocket(null);
+
+ test("DatagramSocket should be created with SO_REUSEADDR disabled");
+ check(!s1.getReuseAddress());
+
+ test("DatagramSocket.setReuseAddress(true)");
+ s1.setReuseAddress(true);
+ check(s1.getReuseAddress());
+
+ test("DatagramSocket.setReuseAddress(false)");
+ s1.setReuseAddress(false);
+ check(!s1.getReuseAddress() );
+
+ /* bind to any port */
+ s1.bind( new InetSocketAddress(0) );
+
+ test("Binding datagram socket to port already in use should throw " +
+ "a BindException");
+ DatagramSocket s2 = new DatagramSocket(null);
+ try {
+ s2.bind( new InetSocketAddress(s1.getLocalPort()) );
+ failed();
+ } catch (BindException e) {
+ passed();
+ }
+ s2.close();
+ s1.close();
+
+ // bind with SO_REUSEADDR enabled
+
+ s1 = new DatagramSocket(null);
+ s1.setReuseAddress(true);
+ s1.bind( new InetSocketAddress(0) );
+
+ test("Bind 2 datagram sockets to the same port - second " +
+ "bind doesn't have SO_REUSEADDR enabled");
+ s2 = new DatagramSocket(null);
+ try {
+ s2.bind( new InetSocketAddress(s1.getLocalPort()) );
+ failed();
+ } catch (BindException e) {
+ passed();
+ }
+ s2.close();
+
+ test("Bind 2 datagram sockets to the same port - both have " +
+ "SO_REUSEADDR enabled");
+ s2 = new DatagramSocket(null);
+ s2.setReuseAddress(true);
+ try {
+ s2.bind( new InetSocketAddress(s1.getLocalPort()) );
+ passed();
+ } catch (BindException e) {
+ failed();
+ }
+ s2.close();
+
+ s1.close();
+
+ }
+
+ void MulticastSocketTests() throws Exception {
+ test("Check SO_REUSEADDR is enabled in MulticastSocket()");
+ MulticastSocket s1 = new MulticastSocket();
+ check(s1.getReuseAddress());
+ s1.close();
+
+ test("Check that SO_REUSEADDR is not disabled by " +
+ "MulticastSocket.bind()");
+
+ s1 = new MulticastSocket(null);
+
+ // bind to specific address
+ InetSocketAddress isa = new InetSocketAddress(
+ InetAddress.getLocalHost(), 0);
+ s1.bind(isa);
+ check(s1.getReuseAddress());
+ s1.close();
+ }
+
+ Basic() throws Exception {
+
+ SocketTests();
+ ServerSocketTests();
+ DatagramSocketTests();
+ MulticastSocketTests();
+
+ System.out.println("***************************************");
+ System.out.println(testCount + " test(s) executed, " +
+ failures + " failure(s).");
+ if (failures > 0) {
+ throw new Exception(failures + " test(s) failed");
+ }
+ }
+
+ public static void main(String args[]) throws Exception {
+ new Basic();
+ }
+
+}
diff --git a/test/java/net/Socket/setReuseAddress/Restart.java b/test/java/net/Socket/setReuseAddress/Restart.java
new file mode 100644
index 0000000..8f7a578
--- /dev/null
+++ b/test/java/net/Socket/setReuseAddress/Restart.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4476378
+ * @summary Check that SO_REUSEADDR allows a server to restart
+ * after a crash.
+ */
+import java.net.*;
+
+public class Restart {
+
+ /*
+ * Test that a server can bind to the same port after
+ * a crash -- ie: while previous connection still in
+ * TIME_WAIT state we should be able to re-bind if
+ * SO_REUSEADDR is enabled.
+ */
+
+ public static void main(String args[]) throws Exception {
+
+ InetSocketAddress isa = new InetSocketAddress(0);
+ ServerSocket ss = new ServerSocket();
+ ss.bind(isa);
+
+ int port = ss.getLocalPort();
+
+ Socket s1 = new Socket(InetAddress.getLocalHost(), port);
+ Socket s2 = ss.accept();
+
+ // close server socket and the accepted connection
+ ss.close();
+ s2.close();
+
+ boolean failed = false;
+
+ ss = new ServerSocket();
+ ss.bind( new InetSocketAddress(port) );
+ ss.close();
+
+ // close the client socket
+ s1.close();
+ }
+}