blob: f7a3118db80f81a6d210d275f0f28ced91c92a24 [file] [log] [blame]
The Android Open Source Projectb5de22c2012-04-01 00:00:00 -07001/*
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
17package tests.net;
18
19import java.io.IOException;
20import java.net.InetAddress;
21import java.net.InetSocketAddress;
22import java.net.ServerSocket;
23import java.net.Socket;
24import java.util.ArrayList;
25
26/**
27 * A test ServerSocket that you can't connect to --- connects will time out.
28 */
29public final class StuckServer {
30 private static final boolean DEBUG = false;
31
32 private ServerSocket serverSocket;
33 private InetSocketAddress address;
34 private ArrayList<Socket> clients = new ArrayList<Socket>();
35
36 public StuckServer(boolean useBacklog) throws IOException {
37 // Set a backlog and use it up so that we can expect the
38 // connection to time out. According to Stevens
39 // 4.5 "listen function", Linux adds 3 to the specified
40 // backlog, so we need to connect 4 times before it will hang.
41 // The trouble with this is that it won't hang forever.
42 // After 10s or so, the kernel allows a couple more connections.
43 // This mode is ony useful if you actually want to continue eventually; we use it to
44 // test non-blocking connects, for example, where you want to test every part of the code.
45 if (useBacklog) {
46 this.serverSocket = new ServerSocket(0, 1);
47 this.address = (InetSocketAddress) serverSocket.getLocalSocketAddress();
48 if (DEBUG) {
49 System.err.println("StuckServer: " + serverSocket);
50 }
51 for (int i = 0; i < 4; ++i) {
52 Socket client = new Socket(serverSocket.getInetAddress(), serverSocket.getLocalPort());
53 clients.add(client);
54 if (DEBUG) {
55 System.err.println("StuckServer client " + i + " - " + client);
56 }
57 }
58 } else {
59 // In general, though, you don't want to rely on listen(2) backlog. http://b/6971145.
60 // RFC 5737 implies this network will be unreachable. (There are two other networks
61 // to try if we have trouble with this one.)
62 // We've had trouble with 10.* in the past (because test labs running CTS often use
63 // net 10!) but hopefully this network will be better.
64 InetAddress testNet1 = InetAddress.getByAddress(new byte[] { (byte) 192, 0, 2, 0 });
65 this.address = new InetSocketAddress(testNet1, 80);
66 }
67 }
68
69 public InetSocketAddress getLocalSocketAddress() {
70 return address;
71 }
72
73 public int getLocalPort() {
74 return address.getPort();
75 }
76
77 public void close() throws IOException {
78 if (serverSocket != null) {
79 serverSocket.close();
80 }
81 for (Socket client : clients) {
82 client.close();
83 }
84 }
85}