summaryrefslogtreecommitdiffstats
path: root/tests/CoreTests/android/core/SocketTest.java
blob: 9db6077476a0280621acaeed01e47699bc54b9a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * Copyright (C) 2008 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 android.core;

import junit.framework.Assert;
import junit.framework.TestCase;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.nio.channels.SocketChannel;
import java.util.concurrent.Semaphore;

import android.test.suitebuilder.annotation.LargeTest;
import android.test.suitebuilder.annotation.SmallTest;
import android.test.suitebuilder.annotation.Suppress;

/**
 * Regression tests for various socket related problems. And a few general
 * socket tests.
 */
public class SocketTest extends TestCase {

    private static final String NON_EXISTING_ADDRESS = "123.123.123.123";

    private static final String KNOW_GOOD_ADDRESS = "209.85.129.147";

    private static final String PACKAGE_DROPPING_ADDRESS = "191.167.0.1";
    
    // Test for basic bind/connect/accept behavior.
    @SmallTest
    public void testSocketSimple() throws Exception {
        ServerSocket ss;
        Socket s, s1;
        int port;

        IOException lastEx = null;

        ss = new ServerSocket();

        for (port = 9900; port < 9999; port++) {
            try {
                ss.bind(new InetSocketAddress("127.0.0.1", port));
                lastEx = null;
                break;
            } catch (IOException ex) {
                lastEx = ex;
            }
        }

        if (lastEx != null) {
            throw lastEx;
        }

        s = new Socket("127.0.0.1", port);

        s1 = ss.accept();

        s.getOutputStream().write(0xa5);

        assertEquals(0xa5, s1.getInputStream().read());

        s1.getOutputStream().write(0x5a);
        assertEquals(0x5a, s.getInputStream().read());
    }
    
    // Regression test for #820068: Wildcard address
    @SmallTest
    public void testWildcardAddress() throws Exception {
        Socket s2 = new Socket();
        s2.bind(new InetSocketAddress((InetAddress) null, 12345));
        byte[] addr = s2.getLocalAddress().getAddress();
        for (int i = 0; i < 4; i++) {
            assertEquals("Not the wildcard address", 0, addr[i]);
        }
    }
    
    // Regression test for #865753: server sockets not closing properly
    @SmallTest
    public void testServerSocketClose() throws Exception {
        ServerSocket s3 = new ServerSocket(23456);
        s3.close();
        ServerSocket s4 = new ServerSocket(23456);
        s4.close();
    }
    
    // Regression test for #876985: SO_REUSEADDR not working properly
    
    private Exception serverError = null;
    
    @LargeTest
    public void testSetReuseAddress() throws IOException {
        InetSocketAddress addr = new InetSocketAddress(8383);

        final ServerSocket serverSock = new ServerSocket();
        serverSock.setReuseAddress(true);
        serverSock.bind(addr);

        final Semaphore semThreadEnd = new Semaphore(0);
        new Thread() {
            @Override
            public void run() {
                try {
                    Socket sock = serverSock.accept();
                    sock.getInputStream().read();
                    sock.close();
                } catch (IOException e) {
                    serverError = e;
                }
                semThreadEnd.release();
            }
        }.start();

        // Give the server a bit of time for startup
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
            // Ignored.
        }
        
        Socket client = new Socket("localhost", 8383);
        client.getOutputStream().write(1);
        // Just leave this connection open from the client side. It will be
        // closed from the server side so the server stays in the TIME_WAIT
        // state for a while. setReuseAddress() should be able to handle this.

        try {
            semThreadEnd.acquire();
        } catch (InterruptedException e) {
            // ignore
        }
        serverSock.close();

        ServerSocket serverSock2 = new ServerSocket();
        serverSock2.setReuseAddress(true);
        serverSock2.bind(addr);
        serverSock2.close();
        
        if (serverError != null) {
            throw new RuntimeException("Server must complete without error", serverError);
        }
    }

    // Regression for 916701, a wrong exception was thrown after timeout of
    // a ServerSocket.
    @LargeTest
    public void testTimeoutException() throws IOException {
        ServerSocket s = new ServerSocket(9800);
        s.setSoTimeout(2000);
        try {
            s.accept();
        } catch (SocketTimeoutException e) {
            // this is ok.
        }
    }

    // Regression for issue 1001980, openening a SocketChannel threw an Exception
    @SmallTest
    public void testNativeSocketChannelOpen() throws IOException {
        SocketChannel.open();
    }

// Regression test for issue 1018016, connecting ignored a set timeout.
//
// Disabled because test behaves differently depending on networking
// environment. It works fine in the emulator and one the device with
// WLAN, but when 3G comes into play, the possible existence of a
// proxy makes it fail.
//
//    @LargeTest
//    public void testSocketSetSOTimeout() throws IOException {
//        Socket sock = new Socket();
//        int timeout = 5000;
//        long start = System.currentTimeMillis();
//        try {
//            sock.connect(new InetSocketAddress(NON_EXISTING_ADDRESS, 80), timeout);
//        } catch (SocketTimeoutException e) {
//            // expected
//            long delay = System.currentTimeMillis() - start;
//            if (Math.abs(delay - timeout) > 1000) {
//                fail("timeout was not accurate. expected: " + timeout
//                        + " actual: " + delay + " miliseconds.");
//            }
//        } finally {
//            try {
//                sock.close();
//            } catch (IOException ioe) {
//                // ignore
//            }
//        }
//    }
    
    /**
     * Regression test for 1062928: Dotted IP addresses (e.g., 192.168.100.1)
     * appear to be broken in the M5 SDK.
     * 
     * Tests that a connection given a ip-addressv4 such as 192.168.100.100 does
     * not fail - sdk m5 seems only to accept dns names instead of ip numbers.
     * ip 209.85.129.147 (one address of www.google.com) on port 80 (http) is
     * used to test the connection.
     */

// Commenting out this test since it is flaky, even at the best of times.  See
// #1191317 for Info.
    @Suppress
    public void disable_testConnectWithIP4IPAddr() {
        // call a Google Web server
        InetSocketAddress scktAddrss = new InetSocketAddress(KNOW_GOOD_ADDRESS,
                80);
        Socket clntSckt = new Socket();
        try {
            clntSckt.connect(scktAddrss, 5000);
        } catch (Throwable e) {
            fail("connection problem:" + e.getClass().getName() + ": "
                    + e.getMessage());
        } finally {
            try {
                clntSckt.close();
            } catch (Exception e) {
                // ignore
            }
        }
    }

    
    // Regression test for #1058962: Socket.close() does not cause
    // socket.connect() to return immediately.
    private Socket client;
    
    private Exception error;
    
    private boolean connected;

// This test isn't working now, but really should work.
// TODO Enable this test again.

    @Suppress
    public void disable_testSocketConnectClose() {
        try {
            client = new Socket();
            
            new Thread() {
                @Override
                public void run() {
                    try {
                        client.connect(new InetSocketAddress(PACKAGE_DROPPING_ADDRESS, 1357));
                    } catch (Exception ex) {
                        error = ex;
                    }
                    
                    connected = true;
                }
            }.start();
            
            Thread.sleep(1000);
            
            Assert.assertNull("Connect must not fail immediately. Maybe try different address.", error);
            Assert.assertFalse("Connect must not succeed. Maybe try different address.", connected);
            
            client.close();
            
            Thread.sleep(1000);

            if (error == null) {
                fail("Socket connect still ongoing");
            } else if (!(error instanceof SocketException)) {
                fail("Socket connect interrupted with wrong error: " + error.toString());
            }
            
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
            
    }
    
}