Merge "DO NOT MERGE: Remove StuckServer using backlog and tests relying on it" into marshmallow-cts-dev
diff --git a/luni/src/test/java/libcore/java/net/ConcurrentCloseTest.java b/luni/src/test/java/libcore/java/net/ConcurrentCloseTest.java
index a629271..bff5ff7 100644
--- a/luni/src/test/java/libcore/java/net/ConcurrentCloseTest.java
+++ b/luni/src/test/java/libcore/java/net/ConcurrentCloseTest.java
@@ -19,24 +19,28 @@
 import java.io.IOException;
 import java.net.DatagramPacket;
 import java.net.DatagramSocket;
+import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.SocketAddress;
 import java.net.SocketException;
+import java.net.UnknownHostException;
 import java.nio.channels.AsynchronousCloseException;
 import java.nio.channels.ClosedChannelException;
 import java.nio.channels.SocketChannel;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.CopyOnWriteArrayList;
-import tests.net.StuckServer;
 
 /**
  * Test that Socket.close called on another thread interrupts a thread that's blocked doing
  * network I/O.
  */
 public class ConcurrentCloseTest extends junit.framework.TestCase {
+    private static final InetSocketAddress UNREACHABLE_ADDRESS
+            = new InetSocketAddress("192.0.2.0", 80); // RFC 6666
+
     public void test_accept() throws Exception {
         ServerSocket ss = new ServerSocket(0);
         new Killer(ss).start();
@@ -50,43 +54,36 @@
     }
 
     public void test_connect() throws Exception {
-        StuckServer ss = new StuckServer(false);
         Socket s = new Socket();
         new Killer(s).start();
         try {
             System.err.println("connect...");
-            s.connect(ss.getLocalSocketAddress());
+            s.connect(UNREACHABLE_ADDRESS);
             fail("connect returned: " + s + "!");
         } catch (SocketException expected) {
             assertEquals("Socket closed", expected.getMessage());
-        } finally {
-            ss.close();
         }
     }
 
     public void test_connect_timeout() throws Exception {
-        StuckServer ss = new StuckServer(false);
         Socket s = new Socket();
         new Killer(s).start();
         try {
             System.err.println("connect (with timeout)...");
-            s.connect(ss.getLocalSocketAddress(), 3600 * 1000);
+            s.connect(UNREACHABLE_ADDRESS, 3600 * 1000);
             fail("connect returned: " + s + "!");
         } catch (SocketException expected) {
             assertEquals("Socket closed", expected.getMessage());
-        } finally {
-            ss.close();
         }
     }
 
     public void test_connect_nonBlocking() throws Exception {
-        StuckServer ss = new StuckServer(false);
         SocketChannel s = SocketChannel.open();
         new Killer(s.socket()).start();
         try {
             System.err.println("connect (non-blocking)...");
             s.configureBlocking(false);
-            s.connect(ss.getLocalSocketAddress());
+            s.connect(UNREACHABLE_ADDRESS);
             while (!s.finishConnect()) {
                 // Spin like a mad thing!
             }
@@ -98,8 +95,6 @@
         } catch (ClosedChannelException alsoOkay) {
             // For now, I'm assuming that we're happy as long as we get any reasonable exception.
             // It may be that we're supposed to guarantee only one or the other.
-        } finally {
-            ss.close();
         }
     }
 
diff --git a/luni/src/test/java/libcore/java/net/OldSocketTest.java b/luni/src/test/java/libcore/java/net/OldSocketTest.java
index 4254516..1ab0f56 100644
--- a/luni/src/test/java/libcore/java/net/OldSocketTest.java
+++ b/luni/src/test/java/libcore/java/net/OldSocketTest.java
@@ -37,11 +37,13 @@
 import java.nio.channels.IllegalBlockingModeException;
 import java.nio.channels.SocketChannel;
 import java.security.Permission;
-import tests.net.StuckServer;
 import tests.support.Support_Configuration;
 
 public class OldSocketTest extends OldSocketTestCase {
 
+    private static final InetSocketAddress UNREACHABLE_ADDRESS
+            = new InetSocketAddress("192.0.2.0", 0); // RFC 6666
+
     ServerSocket ss;
 
     Socket s;
@@ -935,7 +937,7 @@
         byte[] theBytes = { 0, 0, 0, 0 };
         SocketAddress theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0);
         SocketAddress nonConnectableAddress = new InetSocketAddress(InetAddress.getByAddress(theBytes), 0);
-        SocketAddress nonReachableAddress = new InetSocketAddress(StuckServer.UNREACHABLE_ADDRESS, 0);
+        SocketAddress nonReachableAddress = UNREACHABLE_ADDRESS;
         SocketAddress invalidType = new mySocketAddress();
 
         Socket theSocket = null;
@@ -1154,7 +1156,7 @@
         byte[] theBytes = { 0, 0, 0, 0 };
         SocketAddress theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0);
         SocketAddress nonConnectableAddress = new InetSocketAddress(InetAddress.getByAddress(theBytes), 0);
-        SocketAddress nonReachableAddress = new InetSocketAddress(StuckServer.UNREACHABLE_ADDRESS, 0);
+        SocketAddress nonReachableAddress = UNREACHABLE_ADDRESS;
         SocketAddress invalidType = new mySocketAddress();
 
         Socket theSocket = null;
diff --git a/luni/src/test/java/libcore/java/net/URLConnectionTest.java b/luni/src/test/java/libcore/java/net/URLConnectionTest.java
index 3f831e0..6a0c6ae 100644
--- a/luni/src/test/java/libcore/java/net/URLConnectionTest.java
+++ b/luni/src/test/java/libcore/java/net/URLConnectionTest.java
@@ -77,7 +77,6 @@
 import libcore.java.security.TestKeyStore;
 import libcore.java.util.AbstractResourceLeakageDetectorTestCase;
 import libcore.javax.net.ssl.TestSSLContext;
-import tests.net.StuckServer;
 
 import static com.google.mockwebserver.SocketPolicy.DISCONNECT_AT_END;
 import static com.google.mockwebserver.SocketPolicy.DISCONNECT_AT_START;
@@ -1750,11 +1749,11 @@
      * addresses. This is typically one IPv4 address and one IPv6 address.
      */
     public void testConnectTimeouts() throws IOException {
-        StuckServer ss = new StuckServer(true);
-        int serverPort = ss.getLocalPort();
-        String hostName = ss.getLocalSocketAddress().getAddress().getHostAddress();
-        URLConnection urlConnection = new URL("http://" + hostName + ":" + serverPort + "/")
-                .openConnection();
+        // This test is suppressed in M branches via libcore/expectations/brokentests.txt.
+        // This test has been modified in M to remove a dependency on StuckServer testing
+        // infrastructure. It is not expected to work in M.
+        // The 192.0.2.0 address is an unreachable address from RFC 5737.
+        URLConnection urlConnection = new URL("http://192.0.2.0:80/").openConnection();
 
         int timeout = 1000;
         urlConnection.setConnectTimeout(timeout);
@@ -1767,8 +1766,6 @@
             int attempts = InetAddress.getAllByName("localhost").length; // one per IP address
             assertTrue("timeout=" +timeout + ", elapsed=" + elapsed + ", attempts=" + attempts,
                     Math.abs((attempts * timeout) - elapsed) < 500);
-        } finally {
-            ss.close();
         }
     }
 
diff --git a/luni/src/test/java/libcore/java/nio/channels/SelectorTest.java b/luni/src/test/java/libcore/java/nio/channels/SelectorTest.java
index 41b434d..3acef25 100644
--- a/luni/src/test/java/libcore/java/nio/channels/SelectorTest.java
+++ b/luni/src/test/java/libcore/java/nio/channels/SelectorTest.java
@@ -27,7 +27,6 @@
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 import junit.framework.TestCase;
-import tests.net.StuckServer;
 
 public class SelectorTest extends TestCase {
     public void testNonBlockingConnect_immediate() throws Exception {
@@ -51,24 +50,6 @@
         }
     }
 
-    public void testNonBlockingConnect_slow() throws Exception {
-        // Test the case where we have to wait for the connection.
-        Selector selector = Selector.open();
-        StuckServer ss = new StuckServer(true);
-        try {
-            SocketChannel sc = SocketChannel.open();
-            sc.configureBlocking(false);
-            sc.connect(ss.getLocalSocketAddress());
-            SelectionKey key = sc.register(selector, SelectionKey.OP_CONNECT);
-            assertEquals(1, selector.select());
-            assertEquals(SelectionKey.OP_CONNECT, key.readyOps());
-            sc.finishConnect();
-        } finally {
-            selector.close();
-            ss.close();
-        }
-    }
-
     // http://code.google.com/p/android/issues/detail?id=15388
     public void testInterrupted() throws IOException {
         Selector selector = Selector.open();
diff --git a/support/src/test/java/tests/net/StuckServer.java b/support/src/test/java/tests/net/StuckServer.java
deleted file mode 100644
index d6b038b..0000000
--- a/support/src/test/java/tests/net/StuckServer.java
+++ /dev/null
@@ -1,94 +0,0 @@
-/*
- * Copyright (C) 2010 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package tests.net;
-
-import java.io.IOException;
-import java.net.InetAddress;
-import java.net.InetSocketAddress;
-import java.net.ServerSocket;
-import java.net.Socket;
-import java.net.UnknownHostException;
-import java.util.ArrayList;
-
-/**
- * A test ServerSocket that you can't connect to --- connects will time out.
- */
-public final class StuckServer {
-    private static final boolean DEBUG = false;
-
-    // RFC 5737 implies this network -- "test net 1" -- will be unreachable.
-    // (There are two other networks to try if we have trouble with this one.)
-    // We've had trouble with 10.* in the past (because test labs running CTS often use
-    // net 10!) but hopefully this network will be better.
-    public static final InetAddress UNREACHABLE_ADDRESS;
-    static {
-        try {
-            UNREACHABLE_ADDRESS = InetAddress.getByAddress(new byte[] { (byte) 192, 0, 2, 0 });
-        } catch (UnknownHostException ex) {
-            throw new AssertionError(ex);
-        }
-    }
-
-    private ServerSocket serverSocket;
-    private InetSocketAddress address;
-    private ArrayList<Socket> clients = new ArrayList<Socket>();
-
-    public StuckServer(boolean useBacklog) throws IOException {
-        // Set a backlog and use it up so that we can expect the
-        // connection to time out. According to Stevens
-        // 4.5 "listen function", Linux adds 3 to the specified
-        // backlog, so we need to connect 4 times before it will hang.
-        // The trouble with this is that it won't hang forever.
-        // After 10s or so, the kernel allows a couple more connections.
-        // This mode is ony useful if you actually want to continue eventually; we use it to
-        // test non-blocking connects, for example, where you want to test every part of the code.
-        if (useBacklog) {
-            this.serverSocket = new ServerSocket(0, 1);
-            this.address = (InetSocketAddress) serverSocket.getLocalSocketAddress();
-            if (DEBUG) {
-                System.err.println("StuckServer: " + serverSocket);
-            }
-            for (int i = 0; i < 4; ++i) {
-                Socket client = new Socket(serverSocket.getInetAddress(), serverSocket.getLocalPort());
-                clients.add(client);
-                if (DEBUG) {
-                    System.err.println("StuckServer client " + i + " - " + client);
-                }
-            }
-        } else {
-            // In general, though, you don't want to rely on listen(2) backlog. http://b/6971145.
-            this.address = new InetSocketAddress(UNREACHABLE_ADDRESS, 80);
-        }
-    }
-
-    public InetSocketAddress getLocalSocketAddress() {
-        return address;
-    }
-
-    public int getLocalPort() {
-        return address.getPort();
-    }
-
-    public void close() throws IOException {
-        if (serverSocket != null) {
-            serverSocket.close();
-        }
-        for (Socket client : clients) {
-            client.close();
-        }
-    }
-}