Home | History | Annotate | Download | only in net
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package tests.net;
     18 
     19 import java.io.IOException;
     20 import java.net.InetAddress;
     21 import java.net.InetSocketAddress;
     22 import java.net.ServerSocket;
     23 import java.net.Socket;
     24 import java.net.UnknownHostException;
     25 import java.util.ArrayList;
     26 
     27 /**
     28  * A test ServerSocket that you can't connect to --- connects will time out.
     29  */
     30 public final class StuckServer {
     31     private static final boolean DEBUG = false;
     32 
     33     // RFC 5737 implies this network -- "test net 1" -- will be unreachable.
     34     // (There are two other networks to try if we have trouble with this one.)
     35     // We've had trouble with 10.* in the past (because test labs running CTS often use
     36     // net 10!) but hopefully this network will be better.
     37     public static final InetAddress UNREACHABLE_ADDRESS;
     38     static {
     39         try {
     40             UNREACHABLE_ADDRESS = InetAddress.getByAddress(new byte[] { (byte) 192, 0, 2, 0 });
     41         } catch (UnknownHostException ex) {
     42             throw new AssertionError(ex);
     43         }
     44     }
     45 
     46     private ServerSocket serverSocket;
     47     private InetSocketAddress address;
     48     private ArrayList<Socket> clients = new ArrayList<Socket>();
     49 
     50     public StuckServer(boolean useBacklog) throws IOException {
     51         // Set a backlog and use it up so that we can expect the
     52         // connection to time out. According to Stevens
     53         // 4.5 "listen function", Linux adds 3 to the specified
     54         // backlog, so we need to connect 4 times before it will hang.
     55         // The trouble with this is that it won't hang forever.
     56         // After 10s or so, the kernel allows a couple more connections.
     57         // This mode is ony useful if you actually want to continue eventually; we use it to
     58         // test non-blocking connects, for example, where you want to test every part of the code.
     59         if (useBacklog) {
     60             this.serverSocket = new ServerSocket(0, 1);
     61             this.address = (InetSocketAddress) serverSocket.getLocalSocketAddress();
     62             if (DEBUG) {
     63                 System.err.println("StuckServer: " + serverSocket);
     64             }
     65             for (int i = 0; i < 4; ++i) {
     66                 Socket client = new Socket(serverSocket.getInetAddress(), serverSocket.getLocalPort());
     67                 clients.add(client);
     68                 if (DEBUG) {
     69                     System.err.println("StuckServer client " + i + " - " + client);
     70                 }
     71             }
     72         } else {
     73             // In general, though, you don't want to rely on listen(2) backlog. http://b/6971145.
     74             this.address = new InetSocketAddress(UNREACHABLE_ADDRESS, 80);
     75         }
     76     }
     77 
     78     public InetSocketAddress getLocalSocketAddress() {
     79         return address;
     80     }
     81 
     82     public int getLocalPort() {
     83         return address.getPort();
     84     }
     85 
     86     public void close() throws IOException {
     87         if (serverSocket != null) {
     88             serverSocket.close();
     89         }
     90         for (Socket client : clients) {
     91             client.close();
     92         }
     93     }
     94 }
     95