PageRenderTime 175ms CodeModel.GetById 4ms app.highlight 154ms RepoModel.GetById 1ms app.codeStats 1ms

/luni/src/test/java/libcore/java/net/OldSocketTest.java

https://github.com/PAmoto/android_libcore
Java | 2623 lines | 2020 code | 325 blank | 278 comment | 81 complexity | 3db3c7a930b50371665dfa2c4b464775 MD5 | raw file
   1/*
   2 *  Licensed to the Apache Software Foundation (ASF) under one or more
   3 *  contributor license agreements.  See the NOTICE file distributed with
   4 *  this work for additional information regarding copyright ownership.
   5 *  The ASF licenses this file to You under the Apache License, Version 2.0
   6 *  (the "License"); you may not use this file except in compliance with
   7 *  the License.  You may obtain a copy of the License at
   8 *
   9 *     http://www.apache.org/licenses/LICENSE-2.0
  10 *
  11 *  Unless required by applicable law or agreed to in writing, software
  12 *  distributed under the License is distributed on an "AS IS" BASIS,
  13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14 *  See the License for the specific language governing permissions and
  15 *  limitations under the License.
  16 */
  17
  18package libcore.java.net;
  19
  20import java.io.IOException;
  21import java.io.InputStream;
  22import java.io.OutputStream;
  23import java.net.ConnectException;
  24import java.net.Inet4Address;
  25import java.net.Inet6Address;
  26import java.net.InetAddress;
  27import java.net.InetSocketAddress;
  28import java.net.Proxy;
  29import java.net.ServerSocket;
  30import java.net.Socket;
  31import java.net.SocketAddress;
  32import java.net.SocketException;
  33import java.net.SocketImpl;
  34import java.net.SocketTimeoutException;
  35import java.net.UnknownHostException;
  36import java.nio.channels.IllegalBlockingModeException;
  37import java.nio.channels.SocketChannel;
  38import java.security.Permission;
  39import tests.support.Support_Configuration;
  40import tests.support.Support_PortManager;
  41
  42public class OldSocketTest extends OldSocketTestCase {
  43
  44    ServerSocket ss;
  45
  46    Socket s;
  47
  48    Thread t;
  49
  50    SecurityManager sm = new SecurityManager() {
  51
  52        public void checkPermission(Permission perm) {}
  53
  54        public void checkConnect(String host, int port) {
  55            throw new SecurityException();
  56        }
  57    };
  58
  59    public void test_Constructor() {
  60        // create the socket and then validate some basic state
  61        s = new Socket();
  62        assertFalse("new socket should not be connected", s.isConnected());
  63        assertFalse("new socket should not be bound", s.isBound());
  64        assertFalse("new socket should not be closed", s.isClosed());
  65        assertFalse("new socket should not be in InputShutdown", s
  66                .isInputShutdown());
  67        assertFalse("new socket should not be in OutputShutdown", s
  68                .isOutputShutdown());
  69
  70    }
  71
  72    public void test_ConstructorLjava_lang_StringI() throws IOException {
  73        // Test for method java.net.Socket(java.lang.String, int)
  74        int sport = startServer("Cons String,I");
  75        s = new Socket(InetAddress.getLocalHost().getHostName(), sport);
  76        assertTrue("Failed to create socket", s.getPort() == sport);
  77
  78        //regression for HARMONY-946
  79        ServerSocket ss = null;
  80        Socket s = null;
  81        try {
  82            ss = new ServerSocket(0);
  83            s = new Socket("0.0.0.0", ss.getLocalPort());
  84        } finally {
  85            try {
  86                ss.close();
  87            } catch(Exception e) {
  88                //ignore
  89            }
  90            try {
  91                s.close();
  92            } catch(Exception e) {
  93                //ignore
  94            }
  95        }
  96
  97        try {
  98            new Socket("unknown.host", 0);
  99            fail("UnknownHostException was not thrown.");
 100        } catch(UnknownHostException uhe) {
 101            //expected
 102        }
 103        Socket socket = null;
 104        try {
 105            socket = new Socket(InetAddress.getByName(null), sport);
 106            InetAddress address = socket.getLocalAddress();
 107            if (Boolean.getBoolean("java.net.preferIPv6Addresses")) {
 108                assertTrue(
 109                    address.equals(InetAddress.getByName("::1")) ||
 110                    address.equals(InetAddress.getByName("0:0:0:0:0:0:0:1")));
 111            } else {
 112                assertEquals(address, InetAddress.getByName("127.0.0.1"));
 113            }
 114        } finally {
 115            try {
 116                socket.close();
 117            } catch(Exception e) {}
 118        }
 119    }
 120
 121    public void test_ConstructorLjava_lang_StringILjava_net_InetAddressI()
 122            throws IOException {
 123        // Test for method java.net.Socket(java.lang.String, int,
 124        // java.net.InetAddress, int)
 125        int sport = startServer("Cons String,I,InetAddress,I");
 126        int portNumber = Support_PortManager.getNextPort();
 127        s = new Socket(InetAddress.getLocalHost().getHostName(), sport,
 128                InetAddress.getLocalHost(), portNumber);
 129        assertTrue("Failed to create socket", s.getPort() == sport);
 130
 131        if (("true".equals(System.getProperty("java.net.preferIPv6Addresses")))
 132                && !("true".equals(System
 133                        .getProperty("java.net.preferIPv4Stack")))) {
 134
 135            // ALTERNATE IPv6 TEST
 136            if ("true".equals(System.getProperty("run.ipv6tests"))) {
 137                System.out
 138                        .println("Running testConstructorLjava_lang_StringILjava_net_InetAddressI(OldSocketTest) with IPv6GlobalAddressJcl4: "
 139                                + Support_Configuration.IPv6GlobalAddressJcl4);
 140                int testPort = Support_PortManager.getNextPort();
 141                Socket s1 = null, s2 = null;
 142                try {
 143                    s1 = new Socket(
 144                            Support_Configuration.IPv6GlobalAddressJcl4, 80,
 145                            InetAddress.getLocalHost(), testPort);
 146                } catch (IOException e) {
 147                    // check here if InetAddress.getLocalHost() is returning the
 148                    // loopback address.
 149                    // if so that is likely the cause of the failure
 150                    String warning = "";
 151                    try {
 152                        InetAddress returnedLocalHost = InetAddress
 153                                .getLocalHost();
 154                        // don't use isLoopbackAddress for some configurations
 155                        // as they do not have it
 156                        if (returnedLocalHost.isLoopbackAddress()) {
 157                            warning = " - WARNING RETURNED LOCAL HOST IS THE LOOPBACK ADDRESS - MACHINE IS LIKELY NOT CONFIGURED CORRECTLY - THIS LIKELY CAUSED THE FAILURE";
 158
 159                        }
 160                    } catch (Exception ex) {
 161                        warning = " - WARNING COULD NOT GET LOCAL HOST - " + ex;
 162                    }
 163
 164                    fail("Exception creating 1st socket" + warning + ": " + e);
 165                }
 166                boolean exception = false;
 167                try {
 168                    s2 = new Socket(
 169                            Support_Configuration.IPv6GlobalAddressJcl4, 80,
 170                            InetAddress.getLocalHost(), testPort);
 171                } catch (IOException e) {
 172                    exception = true;
 173                }
 174                try {
 175                    s1.close();
 176                    if (!exception)
 177                        s2.close();
 178                } catch (IOException e) {
 179                }
 180                assertTrue("Was able to create two sockets on same port",
 181                        exception);
 182            }
 183
 184        } else {
 185            int testPort = Support_PortManager.getNextPort();
 186            Socket s1 = null, s2 = null;
 187            int serverPort = ss.getLocalPort();
 188            try {
 189                s1 = new Socket("127.0.0.1", serverPort, InetAddress
 190                        .getLocalHost(), testPort);
 191            } catch (IOException e) {
 192                e.printStackTrace();
 193
 194                // check here if InetAddress.getLocalHost() is returning the
 195                // loopback address.
 196                // if so that is likely the cause of the failure
 197                String warning = "";
 198                try {
 199                    InetAddress returnedLocalHost = InetAddress.getLocalHost();
 200                    // don't use isLoopbackAddress for some configurations as
 201                    // they do not have it
 202                    if (returnedLocalHost.isLoopbackAddress()) {
 203                        warning = " - WARNING RETURNED LOCAL HOST IS THE LOOPBACK ADDRESS - MACHINE IS LIKELY NOT CONFIGURED CORRECTLY - THIS LIKELY CAUSED THE FAILURE";
 204
 205                    }
 206                } catch (Exception ex) {
 207                    warning = " - WARNING COULD NOT GET LOCAL HOST - " + ex;
 208                }
 209
 210                fail("Exception creating 1st socket" + warning + ": " + e);
 211            }
 212            boolean exception = false;
 213            try {
 214                s2 = new Socket("127.0.0.1", serverPort, InetAddress
 215                        .getLocalHost(), testPort);
 216            } catch (IOException e) {
 217                exception = true;
 218            }
 219            try {
 220                s1.close();
 221                if (!exception)
 222                    s2.close();
 223            } catch (IOException e) {
 224            }
 225            assertTrue("Was able to create two sockets on same port", exception);
 226        }
 227    }
 228
 229    public void test_ConstructorLjava_lang_StringIZ() throws IOException {
 230        // Test for method java.net.Socket(java.lang.String, int, boolean)
 231        int sport = startServer("Cons String,I,Z");
 232        s = new Socket(InetAddress.getLocalHost().getHostName(), sport, true);
 233        assertTrue("Failed to create socket", s.getPort() == sport);
 234
 235        s = new Socket(InetAddress.getLocalHost().getHostName(), sport, false);
 236    }
 237
 238    public void test_ConstructorLjava_net_InetAddressI() throws IOException {
 239        // Test for method java.net.Socket(java.net.InetAddress, int)
 240        int sport = startServer("Cons InetAddress,I");
 241        s = new Socket(InetAddress.getLocalHost(), sport);
 242        assertTrue("Failed to create socket", s.getPort() == sport);
 243    }
 244
 245    public void test_ConstructorLjava_net_InetAddressILjava_net_InetAddressI()
 246            throws IOException {
 247        // Test for method java.net.Socket(java.net.InetAddress, int,
 248        // java.net.InetAddress, int)
 249        int sport = startServer("Cons InetAddress,I,InetAddress,I");
 250        int portNumber = Support_PortManager.getNextPort();
 251        s = new Socket(InetAddress.getLocalHost().getHostName(), sport,
 252                InetAddress.getLocalHost(), portNumber);
 253        assertTrue("Failed to create socket", s.getLocalPort() == portNumber);
 254    }
 255
 256    public void test_ConstructorLjava_net_InetAddressIZ() throws IOException {
 257        // Test for method java.net.Socket(java.net.InetAddress, int, boolean)
 258        int sport = startServer("Cons InetAddress,I,Z");
 259        s = new Socket(InetAddress.getLocalHost(), sport, true);
 260        assertTrue("Failed to create socket", s.getPort() == sport);
 261
 262        s = new Socket(InetAddress.getLocalHost(), sport, false);
 263    }
 264
 265    public void test_close() throws IOException {
 266        // Test for method void java.net.Socket.close()
 267        int sport = startServer("SServer close");
 268        int portNumber = Support_PortManager.getNextPort();
 269        s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
 270        try {
 271            s.setSoLinger(false, 100);
 272        } catch (IOException e) {
 273            handleException(e, SO_LINGER);
 274        }
 275        s.close();
 276        try {
 277            s.getOutputStream();
 278            fail("IOException was not thrown.");
 279        } catch (java.io.IOException e) {
 280            //expected
 281        }
 282    }
 283
 284    public void test_getInetAddress() throws IOException {
 285        // Test for method java.net.InetAddress java.net.Socket.getInetAddress()
 286        int sport = startServer("SServer getInetAddress");
 287        int portNumber = Support_PortManager.getNextPort();
 288        s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
 289        assertTrue("Returned incorrect InetAddress", s.getInetAddress().equals(
 290                InetAddress.getLocalHost()));
 291
 292    }
 293
 294    public void test_getInputStream() throws IOException {
 295        // Simple fetch test
 296        ServerSocket server = new ServerSocket(0);
 297        Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort());
 298        InputStream is = client.getInputStream();
 299        assertNotNull("Failed to get stream", is);
 300        is.close();
 301        client.close();
 302        server.close();
 303    }
 304
 305    public void test_getKeepAlive() {
 306        try {
 307            int sport = startServer("SServer getKeepAlive");
 308            int portNumber = Support_PortManager.getNextPort();
 309            Socket theSocket = new Socket(InetAddress.getLocalHost(), sport,
 310                    null, portNumber);
 311            theSocket.setKeepAlive(true);
 312            assertTrue("getKeepAlive false when it should be true", theSocket
 313                    .getKeepAlive());
 314            theSocket.setKeepAlive(false);
 315            assertFalse("getKeepAlive true when it should be False", theSocket
 316                    .getKeepAlive());
 317            theSocket.close();
 318            try {
 319                theSocket.setKeepAlive(false);
 320                fail("IOException was not thrown after calling setKeepAlive " +
 321                        "method.");
 322            } catch(IOException ioe) {
 323                //expected
 324            }
 325            try {
 326                theSocket.getKeepAlive();
 327                fail("IOException was not thrown after calling getKeepAlive +" +
 328                        "method.");
 329            } catch(IOException ioe) {
 330                //expected
 331            }
 332            ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_KEEPALIVE);
 333        } catch (Exception e) {
 334            handleException(e, SO_KEEPALIVE);
 335        }
 336    }
 337
 338    public void test_getLocalAddress() throws IOException {
 339        // Test for method java.net.InetAddress
 340        // java.net.Socket.getLocalAddress()
 341        int sport = startServer("SServer getLocAddress");
 342        int portNumber = Support_PortManager.getNextPort();
 343        s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
 344        assertEquals("Returned incorrect InetAddress",
 345                InetAddress.getLocalHost(), s.getLocalAddress());
 346
 347        // now validate that behaviour when the any address is returned
 348        String preferIPv4StackValue = System
 349                .getProperty("java.net.preferIPv4Stack");
 350        String preferIPv6AddressesValue = System
 351                .getProperty("java.net.preferIPv6Addresses");
 352
 353        s = new Socket();
 354        s.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0));
 355
 356        if (((preferIPv4StackValue == null) || preferIPv4StackValue
 357                .equalsIgnoreCase("false"))
 358                && (preferIPv6AddressesValue != null)
 359                && (preferIPv6AddressesValue.equals("true"))) {
 360            assertTrue(
 361                    "ANY address not returned correctly (getLocalAddress) with preferIPv6Addresses=true, preferIPv4Stack=false "
 362                            + s.getLocalSocketAddress(),
 363                    s.getLocalAddress() instanceof Inet6Address);
 364        } else {
 365            assertTrue(
 366                    "ANY address not returned correctly (getLocalAddress) with preferIPv6Addresses=true, preferIPv4Stack=true "
 367                            + s.getLocalSocketAddress(),
 368                    s.getLocalAddress() instanceof Inet4Address);
 369        }
 370        s.close();
 371
 372    }
 373
 374    public void test_getLocalPort() throws IOException {
 375        // Test for method int java.net.Socket.getLocalPort()
 376        int sport = startServer("SServer getLocalPort");
 377        int portNumber = Support_PortManager.getNextPort();
 378        s = new Socket(InetAddress.getLocalHost().getHostName(), sport,
 379                InetAddress.getLocalHost(), portNumber);
 380        assertTrue("Returned incorrect port", s.getLocalPort() == portNumber);
 381    }
 382
 383    @SuppressWarnings("deprecation")
 384    public void test_getOutputStream() throws IOException {
 385        // Test for method java.io.OutputStream
 386        // java.net.Socket.getOutputStream()
 387        int sport = startServer("SServer getOutputStream");
 388        int portNumber = Support_PortManager.getNextPort();
 389        s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
 390        java.io.OutputStream os = s.getOutputStream();
 391        assertNotNull("Failed to get stream", os);
 392        os.write(1);
 393        s.close();
 394        // Regression test for harmony-2934
 395        s = new Socket("127.0.0.1", Support_PortManager.getNextPort(),
 396                false);
 397        OutputStream o = s.getOutputStream();
 398        o.write(1);
 399        try {
 400            Thread.sleep(1000);
 401        } catch (InterruptedException e) {
 402        }
 403        o.close();
 404        s.close();
 405
 406        // Regression test for harmony-2942
 407        s = new Socket("0.0.0.0", Support_PortManager.getNextPort(),
 408                false);
 409        o = s.getOutputStream();
 410        o.write(1);
 411        try {
 412            Thread.sleep(1000);
 413        } catch (InterruptedException e) {
 414        }
 415        o.close();
 416        s.close();
 417    }
 418
 419    public void test_getPort() throws IOException {
 420        // Test for method int java.net.Socket.getPort()
 421        int sport = startServer("SServer getPort");
 422        int portNumber = Support_PortManager.getNextPort();
 423        s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
 424        assertTrue("Returned incorrect port" + s.getPort(),
 425                s.getPort() == sport);
 426    }
 427
 428    public void test_getSoLinger() {
 429        // Test for method int java.net.Socket.getSoLinger()
 430        int sport = startServer("SServer getSoLinger");
 431        try {
 432            int portNumber = Support_PortManager.getNextPort();
 433            s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
 434            s.setSoLinger(true, 200);
 435            assertEquals("Returned incorrect linger", 200, s.getSoLinger());
 436            ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_LINGER);
 437            s.setSoLinger(false, 0);
 438        } catch (Exception e) {
 439            handleException(e, SO_LINGER);
 440        }
 441
 442        try {
 443            int portNumber = Support_PortManager.getNextPort();
 444            s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
 445            s.close();
 446            try {
 447                s.getSoLinger();
 448                fail("SocketException was not thrown.");
 449            } catch(SocketException ioe) {
 450                //expected
 451            }
 452        } catch(Exception e) {
 453            fail("Unexpected exception was thrown: " + e.toString());
 454        }
 455    }
 456
 457    public void test_getReceiveBufferSize() {
 458        try {
 459            int sport = startServer("SServer getReceiveBufferSize");
 460            int portNumber = Support_PortManager.getNextPort();
 461            s = new Socket(InetAddress.getLocalHost().getHostName(), sport,
 462                    null, portNumber);
 463            s.setReceiveBufferSize(130);
 464            assertTrue("Incorrect buffer size", s.getReceiveBufferSize() >= 130);
 465            ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_RCVBUF);
 466        } catch (Exception e) {
 467            handleException(e, SO_RCVBUF);
 468        }
 469
 470        try {
 471            Socket newSocket = new Socket();
 472            newSocket.close();
 473            try {
 474                newSocket.getReceiveBufferSize();
 475                fail("SocketException was not thrown.");
 476            } catch(SocketException e) {
 477                //expected
 478            }
 479        } catch(Exception e) {
 480            fail("Unexpected exception.");
 481        }
 482    }
 483
 484    public void test_getSendBufferSize() {
 485        int sport = startServer("SServer setSendBufferSize");
 486        try {
 487            int portNumber = Support_PortManager.getNextPort();
 488            s = new Socket(InetAddress.getLocalHost().getHostName(), sport,
 489                    null, portNumber);
 490            s.setSendBufferSize(134);
 491            assertTrue("Incorrect buffer size", s.getSendBufferSize() >= 134);
 492            ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_SNDBUF);
 493        } catch (Exception e) {
 494            handleException(e, SO_SNDBUF);
 495        }
 496        try {
 497            int portNumber = Support_PortManager.getNextPort();
 498            s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
 499            s.close();
 500            try {
 501                s.getSendBufferSize();
 502                fail("IOException was not thrown.");
 503            } catch(IOException ioe) {
 504                //expected
 505            }
 506        } catch(Exception e) {
 507            fail("Unexpected exception was thrown: " + e.toString());
 508        }
 509    }
 510
 511    public void test_getSoTimeout() {
 512        // Test for method int java.net.Socket.getSoTimeout()
 513        int sport = startServer("SServer getSoTimeout");
 514        try {
 515            s = new Socket(InetAddress.getLocalHost(), sport);
 516            s.setSoTimeout(100);
 517            assertEquals("Returned incorrect sotimeout", 100, s.getSoTimeout());
 518            ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_TIMEOUT);
 519        } catch (Exception e) {
 520            handleException(e, SO_TIMEOUT);
 521        }
 522
 523        try {
 524            int portNumber = Support_PortManager.getNextPort();
 525            s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
 526            s.close();
 527            try {
 528                s.getSoTimeout();
 529                fail("SocketException was not thrown.");
 530            } catch(SocketException ioe) {
 531                //expected
 532            }
 533        } catch(Exception e) {
 534            fail("Unexpected exception was thrown: " + e.toString());
 535        }
 536    }
 537
 538    public void test_getTcpNoDelay() {
 539        // Test for method boolean java.net.Socket.getTcpNoDelay()
 540        int sport = startServer("SServer getTcpNoDelay");
 541        try {
 542            int portNumber = Support_PortManager.getNextPort();
 543            s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
 544            boolean bool = !s.getTcpNoDelay();
 545            s.setTcpNoDelay(bool);
 546            assertTrue("Failed to get no delay setting: " + s.getTcpNoDelay(),
 547                    s.getTcpNoDelay() == bool);
 548            ensureExceptionThrownIfOptionIsUnsupportedOnOS(TCP_NODELAY);
 549        } catch (Exception e) {
 550            handleException(e, TCP_NODELAY);
 551        }
 552
 553        try {
 554            int portNumber = Support_PortManager.getNextPort();
 555            s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
 556            s.close();
 557            try {
 558                s.getTcpNoDelay();
 559                fail("SocketException was not thrown.");
 560            } catch(SocketException ioe) {
 561                //expected
 562            }
 563        } catch(Exception e) {
 564            fail("Unexpected exception was thrown: " + e.toString());
 565        }
 566    }
 567
 568    public void test_setKeepAliveZ() throws Exception {
 569        // There is not really a good test for this as it is there to detect
 570        // crashed machines. Just make sure we can set it
 571        try {
 572            int sport = startServer("SServer setKeepAlive");
 573            int portNumber = Support_PortManager.getNextPort();
 574            Socket theSocket = new Socket(InetAddress.getLocalHost(), sport,
 575                    null, portNumber);
 576            theSocket.setKeepAlive(true);
 577            theSocket.setKeepAlive(false);
 578            ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_KEEPALIVE);
 579        } catch (Exception e) {
 580            handleException(e, SO_KEEPALIVE);
 581        }
 582        // regression test for HARMONY-1136
 583        new TestSocket((SocketImpl) null).setKeepAlive(true);
 584
 585        try {
 586            Socket theSocket = new Socket();
 587            theSocket.close();
 588            theSocket.setKeepAlive(true);
 589            fail("SocketException was not thrown.");
 590        } catch(SocketException ioe) {
 591            //expected
 592        }
 593    }
 594    class TestSocket extends Socket {
 595        public TestSocket(SocketImpl impl) throws SocketException {
 596            super(impl);
 597        }
 598    }
 599
 600    public void test_setSocketImplFactoryLjava_net_SocketImplFactory() {
 601        // Test for method void
 602        // java.net.Socket.setSocketImplFactory(java.net.SocketImplFactory)
 603
 604        // Cannot test as setting will cause the factory to be changed for
 605        // all subsequent sockets
 606
 607        SecurityManager sm = new SecurityManager() {
 608
 609            public void checkPermission(Permission perm) {
 610            }
 611
 612            public void checkSetFactory() {
 613                throw new SecurityException();
 614            }
 615        };
 616    }
 617
 618    public void test_setSendBufferSizeI() {
 619        try {
 620            int sport = startServer("SServer setSendBufferSizeI");
 621            int portNumber = Support_PortManager.getNextPort();
 622            s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
 623            s.setSendBufferSize(134);
 624            assertTrue("Incorrect buffer size", s.getSendBufferSize() >= 134);
 625            ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_SNDBUF);
 626        } catch (Exception e) {
 627            handleException(e, SO_SNDBUF);
 628        }
 629
 630        try {
 631            Socket theSocket = new Socket();
 632            theSocket.close();
 633            theSocket.setSendBufferSize(1);
 634            fail("SocketException was not thrown.");
 635        } catch(SocketException ioe) {
 636            //expected
 637        } catch(IOException ioe) {
 638            fail("IOException was thrown.");
 639        }
 640    }
 641
 642    public void test_setReceiveBufferSizeI() {
 643        try {
 644            int sport = startServer("SServer setReceiveBufferSizeI");
 645            int portNumber = Support_PortManager.getNextPort();
 646            s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
 647            s.setReceiveBufferSize(130);
 648            assertTrue("Incorrect buffer size", s.getReceiveBufferSize() >= 130);
 649            ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_RCVBUF);
 650        } catch (Exception e) {
 651            handleException(e, SO_RCVBUF);
 652        }
 653
 654        try {
 655            Socket theSocket = new Socket();
 656            theSocket.close();
 657            theSocket.setReceiveBufferSize(1);
 658            fail("SocketException was not thrown.");
 659        } catch(SocketException ioe) {
 660            //expected
 661        } catch(IOException ioe) {
 662            fail("IOException was thrown.");
 663        }
 664    }
 665
 666    public void test_setSoLingerZI() {
 667        // Test for method void java.net.Socket.setSoLinger(boolean, int)
 668        try {
 669            int sport = startServer("SServer setSoLingerZI");
 670            int portNumber = Support_PortManager.getNextPort();
 671            s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
 672            s.setSoLinger(true, 500);
 673            assertEquals("Set incorrect linger", 500, s.getSoLinger());
 674            ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_LINGER);
 675            s.setSoLinger(false, 0);
 676        } catch (Exception e) {
 677            handleException(e, SO_LINGER);
 678        }
 679
 680        try {
 681            Socket theSocket = new Socket();
 682            theSocket.close();
 683            theSocket.setSoLinger(true, 1);
 684            fail("SocketException was not thrown.");
 685        } catch(SocketException ioe) {
 686            //expected
 687        } catch(IOException ioe) {
 688            fail("IOException was thrown.");
 689        }
 690    }
 691
 692    public void test_setSoTimeoutI() {
 693        // Test for method void java.net.Socket.setSoTimeout(int)
 694        try {
 695            int sport = startServer("SServer seSoTimeoutI");
 696            int portNumber = Support_PortManager.getNextPort();
 697            s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
 698            s.setSoTimeout(100);
 699            assertEquals("Set incorrect sotimeout", 100, s.getSoTimeout());
 700            ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_TIMEOUT);
 701        } catch (Exception e) {
 702            handleException(e, SO_TIMEOUT);
 703        }
 704
 705        try {
 706            Socket theSocket = new Socket();
 707            theSocket.close();
 708            theSocket.setSoTimeout(1);
 709            fail("SocketException was not thrown.");
 710        } catch(SocketException ioe) {
 711            //expected
 712        } catch(IOException ioe) {
 713            fail("IOException was thrown.");
 714        }
 715    }
 716
 717    public void test_setTcpNoDelayZ() {
 718        // Test for method void java.net.Socket.setTcpNoDelay(boolean)
 719        try {
 720            int sport = startServer("SServer setTcpNoDelayZ");
 721            int portNumber = Support_PortManager.getNextPort();
 722            s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
 723            boolean bool;
 724            s.setTcpNoDelay(bool = !s.getTcpNoDelay());
 725            assertTrue("Failed to set no delay setting: " + s.getTcpNoDelay(),
 726                    s.getTcpNoDelay() == bool);
 727            ensureExceptionThrownIfOptionIsUnsupportedOnOS(TCP_NODELAY);
 728        } catch (Exception e) {
 729            handleException(e, TCP_NODELAY);
 730        }
 731
 732        try {
 733            Socket theSocket = new Socket();
 734            theSocket.close();
 735            theSocket.setTcpNoDelay(true);
 736            fail("SocketException was not thrown.");
 737        } catch(SocketException ioe) {
 738            //expected
 739        } catch(IOException ioe) {
 740            fail("IOException was thrown.");
 741        }
 742    }
 743
 744    public void test_toString() throws IOException {
 745        // Test for method java.lang.String java.net.Socket.toString()
 746        int sport = startServer("SServer toString");
 747        int portNumber = Support_PortManager.getNextPort();
 748        s = new Socket(InetAddress.getLocalHost().getHostName(), sport,
 749                InetAddress.getLocalHost(), portNumber);
 750        assertTrue("Returned incorrect string: " + s.toString()
 751                + " localHost: " + InetAddress.getLocalHost(), s.toString()
 752                .equals(
 753                        "Socket[addr=" + InetAddress.getLocalHost() + ",port="
 754                                + s.getPort() + ",localport="
 755                                + s.getLocalPort() + "]"));
 756    }
 757
 758    // AndroidOnly: RI returns wrong value for EOF
 759    public void test_shutdownInput() throws Exception {
 760        InetAddress addr = InetAddress.getLocalHost();
 761        int port = Support_PortManager.getNextPort();
 762        ServerSocket serverSocket = new ServerSocket(port, 5, addr);
 763        Socket theSocket = new Socket(addr, port);
 764        Socket servSock = serverSocket.accept();
 765
 766        InputStream theInput = theSocket.getInputStream();
 767        OutputStream theOutput = servSock.getOutputStream();
 768
 769        // shutdown the input
 770        theSocket.shutdownInput();
 771
 772        // send the regular data
 773        String sendString = new String("Test");
 774        theOutput.write(sendString.getBytes());
 775        theOutput.flush();
 776
 777        // give things some time to settle
 778        Thread.sleep(1000);
 779
 780        // RI fails here. It is a RI bug not to return 0 to indicate EOF
 781        assertEquals(0, theInput.available());
 782
 783        theSocket.close();
 784        serverSocket.close();
 785
 786        Socket socket = new Socket();
 787        socket.close();
 788        try {
 789            socket.shutdownInput();
 790            fail("IOException was not thrown.");
 791        } catch(IOException ioe) {
 792            //expected
 793        }
 794    }
 795
 796    public void test_shutdownOutput() throws IOException {
 797        InetAddress addr = InetAddress.getLocalHost();
 798        int port = Support_PortManager.getNextPort();
 799        ServerSocket serverSocket = new ServerSocket(port, 5, addr);
 800        Socket theSocket = new Socket(addr, port);
 801        Socket servSock = serverSocket.accept();
 802
 803        InputStream theInput = theSocket.getInputStream();
 804        OutputStream theOutput = servSock.getOutputStream();
 805
 806        // shutdown the output
 807        servSock.shutdownOutput();
 808
 809        // send the regular data
 810        String sendString = new String("Test");
 811        try {
 812            theOutput.write(sendString.getBytes());
 813            theOutput.flush();
 814            fail("No exception when writing on socket with output shutdown");
 815        } catch (Exception e) {
 816        }
 817
 818        theSocket.close();
 819        serverSocket.close();
 820
 821        try {
 822            theSocket.shutdownInput();
 823            fail("IOException was not thrown.");
 824        } catch(IOException ioe) {
 825            //expected
 826        }
 827    }
 828
 829    public void test_getLocalSocketAddress() throws IOException {
 830        // set up server connect and then validate that we get the right
 831        // response for the local address
 832        int sport = startServer("SServer getLocSocketAddress");
 833        int portNumber = Support_PortManager.getNextPort();
 834        s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
 835        assertTrue(
 836                "Returned incorrect InetSocketAddress(1):"
 837                        + s.getLocalSocketAddress().toString()
 838                        + "Expected: "
 839                        + (new InetSocketAddress(InetAddress.getLocalHost(),
 840                                portNumber)).toString(), s
 841                        .getLocalSocketAddress().equals(
 842                                new InetSocketAddress(InetAddress
 843                                        .getLocalHost(), portNumber)));
 844        s.close();
 845
 846        // now create a socket that is not bound and validate we get the
 847        // right answer
 848        Socket theSocket = new Socket();
 849        assertNull(
 850                "Returned incorrect InetSocketAddress -unbound socket- Expected null",
 851                theSocket.getLocalSocketAddress());
 852
 853        // now bind the socket and make sure we get the right answer
 854        portNumber = Support_PortManager.getNextPort();
 855        theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(),
 856                portNumber));
 857        assertTrue(
 858                "Returned incorrect InetSocketAddress(2):"
 859                        + theSocket.getLocalSocketAddress().toString()
 860                        + "Expected: "
 861                        + (new InetSocketAddress(InetAddress.getLocalHost(),
 862                                portNumber)).toString(), theSocket
 863                        .getLocalSocketAddress().equals(
 864                                new InetSocketAddress(InetAddress
 865                                        .getLocalHost(), portNumber)));
 866        theSocket.close();
 867
 868        // now validate that behaviour when the any address is returned
 869        s = new Socket();
 870        s.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0));
 871
 872        String preferIPv4StackValue = System
 873                .getProperty("java.net.preferIPv4Stack");
 874        String preferIPv6AddressesValue = System
 875                .getProperty("java.net.preferIPv6Addresses");
 876        if (((preferIPv4StackValue == null) || preferIPv4StackValue
 877                .equalsIgnoreCase("false"))
 878                && (preferIPv6AddressesValue != null)
 879                && (preferIPv6AddressesValue.equals("true"))) {
 880            assertTrue(
 881                    "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=false "
 882                            + s.getLocalSocketAddress(),
 883                    ((InetSocketAddress) s.getLocalSocketAddress())
 884                            .getAddress() instanceof Inet6Address);
 885        } else {
 886            assertTrue(
 887                    "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=true "
 888                            + s.getLocalSocketAddress(),
 889                    ((InetSocketAddress) s.getLocalSocketAddress())
 890                            .getAddress() instanceof Inet4Address);
 891        }
 892        s.close();
 893
 894        // now validate the same for getLocalAddress
 895        s = new Socket();
 896        s.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0));
 897        if (((preferIPv4StackValue == null) || preferIPv4StackValue
 898                .equalsIgnoreCase("false"))
 899                && (preferIPv6AddressesValue != null)
 900                && (preferIPv6AddressesValue.equals("true"))) {
 901            assertTrue(
 902                    "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=false "
 903                            + s.getLocalSocketAddress(),
 904                    ((InetSocketAddress) s.getLocalSocketAddress())
 905                            .getAddress() instanceof Inet6Address);
 906        } else {
 907            assertTrue(
 908                    "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=true "
 909                            + s.getLocalSocketAddress(),
 910                    ((InetSocketAddress) s.getLocalSocketAddress())
 911                            .getAddress() instanceof Inet4Address);
 912        }
 913        s.close();
 914    }
 915
 916    public void test_getRemoteSocketAddress() throws IOException {
 917        // set up server connect and then validate that we get the right
 918        // response for the remote address
 919        int sport = startServer("SServer getLocRemoteAddress");
 920        int portNumber = Support_PortManager.getNextPort();
 921        s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
 922        assertTrue("Returned incorrect InetSocketAddress(1):"
 923                + s.getLocalSocketAddress().toString(),
 924                s.getRemoteSocketAddress()
 925                        .equals(
 926                                new InetSocketAddress(InetAddress
 927                                        .getLocalHost(), sport)));
 928        s.close();
 929
 930        // now create one that is not connect and validate that we get the
 931        // right answer
 932        Socket theSocket = new Socket();
 933        portNumber = Support_PortManager.getNextPort();
 934        theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(),
 935                portNumber));
 936
 937        assertNull("Returned incorrect InetSocketAddress -unconnected socket:"
 938                + "Expected: NULL", theSocket.getRemoteSocketAddress());
 939
 940        // now connect and validate we get the right answer
 941        theSocket.connect(new InetSocketAddress(InetAddress.getLocalHost(),
 942                sport));
 943        assertTrue("Returned incorrect InetSocketAddress(2):"
 944                + theSocket.getRemoteSocketAddress().toString(),
 945                theSocket.getRemoteSocketAddress()
 946                        .equals(
 947                                new InetSocketAddress(InetAddress
 948                                        .getLocalHost(), sport)));
 949        theSocket.close();
 950
 951    }
 952
 953    public void test_isBound() throws IOException {
 954        InetAddress addr = InetAddress.getLocalHost();
 955        int port = Support_PortManager.getNextPort();
 956        ServerSocket serverSocket = new ServerSocket(port, 5, addr);
 957        Socket theSocket = new Socket(addr, port);
 958        Socket servSock = serverSocket.accept();
 959        assertTrue("Socket indicated  not bound when it should be (1)",
 960                theSocket.isBound());
 961        theSocket.close();
 962        serverSocket.close();
 963
 964        // now do it with the new constructors and revalidate. Connect causes
 965        // the socket to be bound
 966        InetSocketAddress theAddress = new InetSocketAddress(InetAddress
 967                .getLocalHost(), Support_PortManager.getNextPort());
 968        theSocket = new Socket();
 969        assertFalse("Socket indicated bound when it was not (2)", theSocket
 970                .isBound());
 971        serverSocket = new ServerSocket();
 972        serverSocket.bind(theAddress);
 973        theSocket.connect(theAddress);
 974        servSock = serverSocket.accept();
 975        assertTrue("Socket indicated not bound when it should be (2)",
 976                theSocket.isBound());
 977        theSocket.close();
 978        serverSocket.close();
 979
 980        // now test when we bind explicitly
 981        InetSocketAddress theLocalAddress = new InetSocketAddress(InetAddress
 982                .getLocalHost(), Support_PortManager.getNextPort());
 983        theSocket = new Socket();
 984        assertFalse("Socket indicated bound when it was not (3)", theSocket
 985                .isBound());
 986        theSocket.bind(theLocalAddress);
 987        assertTrue("Socket indicated not bound when it should be (3a)",
 988                theSocket.isBound());
 989        theSocket.close();
 990        assertTrue("Socket indicated not bound when it should be (3b)",
 991                theSocket.isBound());
 992    }
 993
 994    public void test_isConnected() throws IOException {
 995        InetAddress addr = InetAddress.getLocalHost();
 996        int port = Support_PortManager.getNextPort();
 997        ServerSocket serverSocket = new ServerSocket(port, 5, addr);
 998        Socket theSocket = new Socket(addr, port);
 999        Socket servSock = serverSocket.accept();
1000        assertTrue("Socket indicated  not connected when it should be",
1001                theSocket.isConnected());
1002        theSocket.close();
1003        serverSocket.close();
1004
1005        // now do it with the new constructors and revalidate
1006        InetSocketAddress theAddress = new InetSocketAddress(InetAddress
1007                .getLocalHost(), Support_PortManager.getNextPort());
1008        theSocket = new Socket();
1009        assertFalse("Socket indicated connected when it was not", theSocket
1010                .isConnected());
1011        serverSocket = new ServerSocket();
1012        serverSocket.bind(theAddress);
1013        theSocket.connect(theAddress);
1014        servSock = serverSocket.accept();
1015        assertTrue("Socket indicated  not connected when it should be",
1016                theSocket.isConnected());
1017        theSocket.close();
1018        serverSocket.close();
1019    }
1020
1021    public void test_isClosed() throws IOException {
1022        InetAddress addr = InetAddress.getLocalHost();
1023        int port = Support_PortManager.getNextPort();
1024        ServerSocket serverSocket = new ServerSocket(port, 5, addr);
1025        Socket theSocket = new Socket(addr, port);
1026        Socket servSock = serverSocket.accept();
1027
1028        // validate isClosed returns expected values
1029        assertFalse("Socket should indicate it is not closed(1):", theSocket
1030                .isClosed());
1031        theSocket.close();
1032        assertTrue("Socket should indicate it is closed(1):", theSocket
1033                .isClosed());
1034
1035        theSocket = new Socket(addr, port);
1036        assertFalse("Socket should indicate it is not closed(2):", theSocket
1037                .isClosed());
1038        theSocket.close();
1039        assertTrue("Socket should indicate it is closed(2):", theSocket
1040                .isClosed());
1041
1042        // validate that isClosed works ok for sockets returned from
1043        // ServerSocket.accept()
1044        assertFalse("Server Socket should indicate it is not closed:", servSock
1045                .isClosed());
1046        servSock.close();
1047        assertTrue("Server Socket should indicate it is closed:", servSock
1048                .isClosed());
1049    }
1050
1051    public void test_bindLjava_net_SocketAddress() throws IOException {
1052
1053        class mySocketAddress extends SocketAddress {
1054
1055            public mySocketAddress() {
1056            }
1057        }
1058
1059        // Address we cannot bind to
1060        Socket theSocket = new Socket();
1061        try {
1062            theSocket.bind(new InetSocketAddress(InetAddress
1063                    .getByAddress(Support_Configuration.nonLocalAddressBytes),
1064                    Support_PortManager.getNextPort()));
1065            fail("No exception when binding to bad address:"
1066                   + theSocket.getLocalSocketAddress().toString());
1067        } catch (IOException ex) {
1068        }
1069        theSocket.close();
1070
1071        // now create a socket that is not bound and then bind it
1072        theSocket = new Socket();
1073        int portNumber = Support_PortManager.getNextPort();
1074        theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(),
1075                portNumber));
1076
1077        // validate that the localSocketAddress reflects the address we
1078        // bound to
1079        assertTrue(
1080                "Local address not correct after bind:"
1081                        + theSocket.getLocalSocketAddress().toString()
1082                        + " Expected: "
1083                        + (new InetSocketAddress(InetAddress.getLocalHost(),
1084                                portNumber)).toString(), theSocket
1085                        .getLocalSocketAddress().equals(
1086                                new InetSocketAddress(InetAddress
1087                                        .getLocalHost(), portNumber)));
1088
1089        // make sure we can now connect and that connections appear to come
1090        // from the address we bound to.
1091        InetSocketAddress theAddress = new InetSocketAddress(InetAddress
1092                .getLocalHost(), Support_PortManager.getNextPort());
1093        ServerSocket serverSocket = new ServerSocket();
1094        serverSocket.bind(theAddress);
1095        theSocket.connect(theAddress);
1096        Socket servSock = serverSocket.accept();
1097        assertTrue(
1098                "Returned Remote address from server connected to does not match expected local address:"
1099                        + servSock.getRemoteSocketAddress().toString()
1100                        + " Expected: "
1101                        + (new InetSocketAddress(InetAddress.getLocalHost(),
1102                                portNumber)).toString(), servSock
1103                        .getRemoteSocketAddress().equals(
1104                                new InetSocketAddress(InetAddress
1105                                        .getLocalHost(), portNumber)));
1106        theSocket.close();
1107        servSock.close();
1108        serverSocket.close();
1109
1110        // validate if we pass in null that it picks an address for us and
1111        // all is ok
1112        theSocket = new Socket();
1113        theSocket.bind(null);
1114        assertNotNull("Bind with null did not work", theSocket
1115                .getLocalSocketAddress());
1116        theSocket.close();
1117
1118        // now check the error conditions
1119
1120        // Address that we have already bound to
1121        theSocket = new Socket();
1122        Socket theSocket2 = new Socket();
1123        try {
1124            theAddress = new InetSocketAddress(InetAddress.getLocalHost(),
1125                    Support_PortManager.getNextPort());
1126            theSocket.bind(theAddress);
1127            theSocket2.bind(theAddress);
1128            fail("No exception binding to address that is not available");
1129        } catch (IOException ex) {
1130        }
1131        theSocket.close();
1132        theSocket2.close();
1133
1134        // unsupported SocketAddress subclass
1135        theSocket = new Socket();
1136        try {
1137            theSocket.bind(new mySocketAddress());
1138            fail("No exception when binding using unsupported SocketAddress subclass");
1139        } catch (IllegalArgumentException ex) {
1140        }
1141        theSocket.close();
1142    }
1143
1144    public void test_bindLjava_net_SocketAddress_Proxy() throws IOException {
1145        //The Proxy will not impact on the bind operation.It can be assigned with any address.
1146        Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 0));
1147        Socket socket = new Socket(proxy);
1148
1149        try {
1150            InetAddress address = InetAddress.getByName("localhost");
1151            int port = 0;
1152            socket.bind(new InetSocketAddress(address, port));
1153
1154            assertEquals(address, socket.getLocalAddress());
1155            assertTrue(port!=socket.getLocalPort());
1156
1157        } finally {
1158            socket.close();
1159        }
1160    }
1161
1162    public void test_connectLjava_net_SocketAddress() throws Exception {
1163        // needed for some tests
1164        class mySocketAddress extends SocketAddress {
1165
1166            public mySocketAddress() {
1167            }
1168        }
1169
1170        class SocketCloser extends Thread {
1171
1172            int timeout = 0;
1173
1174            Socket theSocket = null;
1175
1176            public void run() {
1177                try {
1178                    Thread.sleep(timeout);
1179                    theSocket.close();
1180                } catch (Exception e) {
1181                }
1182                ;
1183                return;
1184            }
1185
1186            public SocketCloser(int timeout, Socket theSocket) {
1187                this.timeout = timeout;
1188                this.theSocket = theSocket;
1189            }
1190        }
1191
1192        // start by validating the error checks
1193        int portNumber = Support_PortManager.getNextPort();
1194        Socket theSocket = null;
1195        ServerSocket serverSocket = null;
1196        SocketAddress theAddress = null;
1197        SocketAddress nonConnectableAddress = null;
1198        SocketAddress nonReachableAddress = null;
1199        SocketAddress invalidType = null;
1200        // byte[] theBytes = {-1,-1,-1,-1};
1201        byte[] theBytes = { 0, 0, 0, 0 };
1202        theAddress = new InetSocketAddress(InetAddress.getLocalHost(),
1203                portNumber);
1204        nonConnectableAddress = new InetSocketAddress(InetAddress
1205                .getByAddress(theBytes), portNumber);
1206        nonReachableAddress = new InetSocketAddress(InetAddress
1207                .getByName(Support_Configuration.ResolvedNotExistingHost),
1208                portNumber);
1209
1210        invalidType = new mySocketAddress();
1211
1212        try {
1213            theSocket = new Socket();
1214            theSocket.connect(null);
1215            fail("No exception after null address passed in");
1216        } catch (Exception e) {
1217            assertTrue("Wrong exception null address passed in: "
1218                    + e.toString(), (e instanceof IllegalArgumentException));
1219        }
1220
1221        try {
1222            theSocket = new Socket();
1223            theSocket.connect(invalidType);
1224            fail("No exception when invalid socket address type passed in: ");
1225        } catch (Exception e) {
1226            assertTrue(
1227                    "Wrong exception when when invalid socket address type passed in: "
1228                            + e.toString(),
1229                    (e instanceof IllegalArgumentException));
1230        }
1231
1232        try {
1233            theSocket = new Socket();
1234            theSocket.connect(nonConnectableAddress);
1235            fail("No exception when non Connectable Address passed in: ");
1236        } catch (Exception e) {
1237            assertTrue(
1238                    "Wrong exception when non Connectable Address passed in: "
1239                            + e.toString(), (e instanceof ConnectException));
1240        }
1241
1242        // now validate that we get a connect exception if we try to connect to
1243        // an address on which nobody is listening
1244        try {
1245            theSocket = new Socket();
1246            theSocket.connect(theAddress);
1247            theSocket.close();
1248            fail("No exception when connecting to address nobody listening on: ");
1249        } catch (Exception e) {
1250            assertTrue(
1251                    "Wrong exception when connecting to address nobody listening on: "
1252                            + e.toString(), (e instanceof ConnectException));
1253        }
1254
1255        // now validate that we can actually connect when somebody is listening
1256        theSocket = new Socket();
1257        serverSocket = new ServerSocket();
1258        serverSocket.bind(theAddress);
1259        theSocket.connect(theAddress);
1260        theSocket.close();
1261        serverSocket.close();
1262
1263        // now validate that we can actually connect when somebody is listening
1264        theSocket = new Socket();
1265        serverSocket = new ServerSocket();
1266        serverSocket.bind(theAddress);
1267        theSocket.connect(theAddress);
1268
1269        // validate that when a socket is connected that it answers
1270        // correctly to related queries
1271        assertTrue("Socket did not returned connected when it is: ", theSocket
1272                .isConnected());
1273        assertFalse("Socket returned closed when it should be connected ",
1274                theSocket.isClosed());
1275        assertTrue("Socket returned not bound when it should be: ", theSocket
1276                .isBound());
1277        assertFalse(
1278                "Socket returned input Shutdown when it should be connected ",
1279                theSocket.isInputShutdown());
1280        assertFalse(
1281                "Socket returned output Shutdown when it should be connected ",
1282                theSocket.isOutputShutdown());
1283        assertTrue("Local port on connected socket was 0", theSocket
1284                .getLocalPort() != 0);
1285        theSocket.close();
1286        serverSocket.close();
1287
1288        // now validate that we get the right exception if we connect when we
1289        // are already connected
1290        try {
1291            theSocket = new Socket();
1292            serverSocket = new ServerSocket();
1293            serverSocket.bind(theAddress);
1294            theSocket.connect(theAddress);
1295            theSocket.connect(theAddress);
1296            theSocket.close();
1297            serverSocket.close();
1298            fail("No exception when we try to connect on a connected socket: ");
1299
1300        } catch (Exception e) {
1301            assertTrue(
1302                    "Wrong exception when connecting on socket that is allready connected"
1303                            + e.toString(), (e instanceof SocketException));
1304            assertFalse(
1305                    "Wrong exception when connecting on socket that is allready connected"
1306                            + e.toString(),
1307                    (e instanceof SocketTimeoutException));
1308            try {
1309                theSocket.close();
1310                serverSocket.close();
1311            } catch (Exception e2) {
1312            }
1313
1314        }
1315
1316        // now validate that connected socket can be used to read/write
1317        theSocket = new Socket();
1318        serverSocket = new ServerSocket();
1319        serverSocket.bind(theAddress);
1320        theSocket.connect(theAddress);
1321        Socket servSock = serverSocket.accept();
1322        InputStream theInput = theSocket.getInputStream();
1323        OutputStream theOutput = servSock.getOutputStream();
1324        InputStream theInput2 = servSock.getInputStream();
1325        OutputStream theOutput2 = theSocket.getOutputStream();
1326
1327        String sendString = new String("Test");
1328        theOutput.write(sendString.getBytes());
1329        theOutput.flush();
1330
1331        Thread.sleep(1000);
1332
1333        int totalBytesRead = 0;
1334        byte[] myBytes = new byte[100];
1335        while (theInput.available() > 0) {
1336            int bytesRead = theInput.read(myBytes, totalBytesRead,
1337                    myBytes.length - totalBytesRead);
1338            totalBytesRead = totalBytesRead + bytesRead;
1339        }
1340
1341        String receivedString = new String(myBytes, 0, totalBytesRead);
1342        assertTrue("Could not recv on socket connected with timeout:"
1343                + receivedString + ":" + sendString, receivedString
1344                .equals(sendString));
1345
1346        sendString = new String("SEND - Test");
1347        theOutput2.write(sendString.getBytes());
1348        theOutput2.flush();
1349        Thread.sleep(1000);
1350
1351        totalBytesRead = 0;
1352        myBytes = new byte[100];
1353        while (theInput2.available() > 0) {
1354            int bytesRead = theInput2.read(myBytes, totalBytesRead,
1355                    myBytes.length - totalBytesRead);
1356            totalBytesRead = totalBytesRead + bytesRead;
1357        }
1358
1359        receivedString = new String(myBytes, 0, totalBytesRead);
1360        assertTrue("Could not send on socket connected with timeout:"
1361                + receivedString + ":" + sendString, receivedString
1362                .equals(sendString));
1363
1364        theSocket.close();
1365        serverSocket.close();
1366
1367        SocketChannel channel = SocketChannel.open();
1368        channel.configureBlocking(false);
1369        Socket socket = channel.socket();
1370        int port = Support_PortManager.getNextPort();
1371        try {
1372            socket.connect( new InetSocketAddress(InetAddress.getLocalHost(),
1373                    Support_PortManager.getNextPort()));
1374            fail("IllegalBlockingModeException was not thrown.");
1375        } catch(IllegalBlockingModeException ibme) {
1376            //expected
1377        }
1378    }
1379
1380    public void test_connectLjava_net_SocketAddressI() throws Exception {
1381
1382        // needed for some tests
1383        class mySocketAddress extends SocketAddress {
1384
1385            public mySocketAddress() {
1386            }
1387        }
1388
1389        class SocketCloser extends Thread {
1390
1391            int timeout = 0;
1392
1393            Socket theSocket = null;
1394
1395            public void run() {
1396                try {
1397                    Thread.sleep(timeout);
1398                    theSocket.close();
1399                } catch (Exception e) {
1400                }
1401                return;
1402            }
1403
1404            public SocketCloser(int timeout, Socket theSocket) {
1405                this.timeout = timeout;
1406                this.theSocket = theSocket;
1407            }
1408        }
1409
1410        class SocketConnector extends Thread {
1411
1412            int timeout = 0;
1413
1414            Socket theSocket = null;
1415
1416            SocketAddress address = null;
1417
1418            public void run() {
1419                try {
1420                    theSocket.connect(address, timeout);
1421                } catch (Exception e) {
1422                }
1423
1424                return;
1425            }
1426
1427            public SocketConnector(int timeout, Socket theSocket,
1428                    SocketAddress address) {
1429                this.timeout = timeout;
1430                this.theSocket = theSocket;
1431                this.address = address;
1432            }
1433        }
1434
1435        // start by validating the error checks
1436        int portNumber = Support_PortManager.getNextPort();
1437        Socket theSocket = null;
1438        ServerSocket serverSocket = null;
1439        SocketAddress theAddress = null;
1440        SocketAddress nonConnectableAddress = null;
1441        SocketAddress nonReachableAddress = null;
1442        SocketAddress nonListeningAddress = null;
1443        SocketAddress invalidType = null;
1444        byte[] theBytes = { 0, 0, 0, 0 };
1445
1446        theAddress = new InetSocketAddress(InetAddress.getLocalHost(),
1447                portNumber);
1448        nonConnectableAddress = new InetSocketAddress(InetAddress
1449                .getByAddress(theBytes), portNumber);
1450        nonReachableAddress = new InetSocketAddress(InetAddress
1451                .getByName(Support_Configuration.ResolvedNotExistingHost),
1452                portNumber);
1453        // make sure we get another port
1454        Thread.sleep(7000);
1455        nonListeningAddress = new InetSocketAddress(InetAddress.getLocalHost(),
1456                Support_PortManager.getNextPort());
1457        invalidType = new mySocketAddress();
1458
1459        try {
1460            theSocket = new Socket();
1461            theSocket.connect(theAddress, -100);
1462            fail("No exception after negative timeout passed in");
1463        } catch (Exception e) {
1464            assertTrue("Wrong exception when negative timeout passed in: "
1465                    + e.toString(), (e instanceof IllegalArgumentException));
1466        }
1467
1468        try {
1469            theSocket = new Socket();
1470            theSocket.connect(null, 0);
1471            fail("No exception after null address passed in");
1472        } catch (Exception e) {
1473            assertTrue("Wrong exception null address passed in: "
1474                    + e.toString(), (e instanceof IllegalArgumentException));
1475        }
1476
1477        try {
1478            theSocket = new Socket();
1479            theSocket.connect(invalidType, 100000);
1480            fail("No exception when invalid socket address type passed in: ");
1481        } catch (Exception e) {
1482            assertTrue(
1483                    "Wrong exception when when invalid socket address type passed in: "
1484                            + e.toString(),
1485                    (e instanceof IllegalArgumentException));
1486        }
1487
1488        try {
1489            theSocket = new Socket();
1490            theSocket.connect(nonConnectableAddress, 100000);
1491            fail("No exception when non Connectable Address passed in: ");
1492        } catch (Exception e) {
1493            assertTrue(
1494                    "Wrong exception when non Connectable Address passed in: "
1495                            + e.toString(), (e instanceof SocketException));
1496        }
1497
1498        // now validate that we get a connect exception if we try to connect to
1499        // an address on which nobody is listening
1500        try {
1501            theSocket = new Socket();
1502            theSocket.connect(theAddress, 0);
1503            theSocket.close();
1504            fail("No timeout:No exception when connecting to address nobody listening on: ");
1505        } catch (Exception e) {
1506            assertTrue(
1507                    "No timeout:Wrong exception when connecting to address nobody listening on: "
1508                            + e.toString(), (e instanceof ConnectException));
1509        }
1510
1511        // now validate that we can actually connect when somebody is listening
1512        theSocket = new Socket();
1513        serverSocket = new ServerSocket();
1514        serverSocket.bind(theAddress);
1515        theSocket.connect(theAddress, 0);
1516        theSocket.close();
1517        serverSocket.close();
1518
1519        // now validate that we get a connect exception if we try to connect to
1520        // an address on which nobody is listening
1521        try {
1522            theSocket = new Socket();
1523            theSocket.connect(nonListeningAddress, 100000);
1524            theSocket.close();
1525            fail("No exception when connecting to address nobody listening on: ");
1526        } catch (Exception e) {
1527            assertTrue(
1528                    "Wrong exception when connecting to address nobody listening on: "
1529                            + e.toString(), (e instanceof ConnectException));
1530        }
1531
1532        // now validate that we get a interrupted exception if we try to connect
1533        // to an address on which nobody is accepting connections and the
1534        // timeout expired
1535        try {
1536            theSocket = new Socket();
1537            theSocket.connect(nonReachableAddress, 200);
1538            theSocket.close();
1539            fail("No interrupted exception when connecting to address nobody listening on with short timeout 200: ");
1540        } catch (Exception e) {
1541            assertTrue(
1542                    "Wrong exception when connecting to address nobody listening on with short timeout 200: "
1543                            + e.toString(),
1544                    (e instanceof SocketTimeoutException));
1545        }
1546
1547        // now validate that we get a interrupted exception if we try to connect
1548        // to an address on which nobody is accepting connections and the
1549        // timeout expired
1550        try {
1551            theSocket = new Socket();
1552            theSocket.connect(nonReachableAddress, 40);
1553            theSocket.close();
1554            fail("No interrupted exception when connecting to address nobody listening on with short timeout 40: ");
1555        } catch (Exception e) {
1556            assertTrue(
1557                    "Wrong exception when connecting to address nobody listening on with short timeout 40: "
1558                            + e.toString(),
1559                    (e instanceof SocketTimeoutException));
1560        }
1561
1562        // now validate that we can actually connect when somebody is listening
1563        new InetSocketAddress(InetAddress.getLocalHost(), Support_PortManager
1564                .getNextPort());
1565        theSocket = new Socket();
1566        serverSocket = new ServerSocket();
1567        serverSocket.bind(theAddress);
1568        theSocket.connect(theAddress, 100000);
1569
1570        // validate that when a socket is connected that it answers
1571        // correctly to related queries
1572        assertTrue("Socket did not returned connected when it is: ", theSocket
1573                .isConnected());
1574        assertFalse("Socket returned closed when it should be connected ",
1575                theSocket.isClosed());
1576        assertTrue("Socket returned not bound when it should be: ", theSocket
1577                .isBound());
1578        assertFalse(
1579                "Socket returned input Shutdown when it should be connected ",
1580                theSocket.isInputShutdown());
1581        assertFalse(
1582                "Socket returned output Shutdown when it should be connected ",
1583                theSocket.isOutputShutdown());
1584        assertTrue("Local port on connected socket was 0", theSocket
1585                .getLocalPort() != 0);
1586        theSocket.close();
1587        serverSocket.close();
1588
1589        // now validate that we get the right exception if we connect when we
1590        // are already connected
1591        try {
1592            new InetSocketAddress(InetAddress.getLocalHost(),
1593                    Support_PortManager.getNextPort());
1594            theSocket = new Socket();
1595            serverSocket = new ServerSocket();
1596            serverSocket.bind(theAddress);
1597            theSocket.connect(theAddress, 100000);
1598            theSocket.connect(theAddress, 100000);
1599            theSocket.close();
1600            serverSocket.close();
1601            fail("No exception when we try to connect on a connected socket: ");
1602
1603        } catch (Exception e) {
1604            assertTrue(
1605                    "Wrong exception when connecting on socket that is already connected"
1606                            + e.toString(), (e instanceof SocketException));
1607            assertFalse(
1608                    "Wrong exception when connecting on socket that is already connected"
1609                            + e.toString(),
1610                    (e instanceof SocketTimeoutException));
1611            try {
1612                theSocket.close();
1613                serverSocket.close();
1614            } catch (Exception e2) {
1615            }
1616
1617        }
1618
1619        // now validate that connected socket can be used to read/write
1620        new InetSocketAddress(InetAddress.getLocalHost(), Support_PortManager
1621                .getNextPort());
1622        theSocket = new Socket();
1623        serverSocket = new ServerSocket();
1624        serverSocket.bind(theAddress);
1625        theSocket.connect(theAddress, 100000);
1626        Socket servSock = serverSocket.accept();
1627        InputStream theInput = theSocket.getInputStream();
1628        OutputStream theOutput = servSock.getOutputStream();
1629        InputStream theInput2 = servSock.getInputStream();
1630        OutputStream theOutput2 = theSocket.getOutputStream();
1631
1632        String sendString = new String("Test");
1633        theOutput.write(sendString.getBytes());
1634        theOutput.flush();
1635
1636        Thread.sleep(1000);
1637
1638        int totalBytesRead = 0;
1639        byte[] myBytes = new byte[100];
1640        while (theInput.available() > 0) {
1641            int bytesRead = theInput.read(myBytes, totalBytesRead,
1642                    myBytes.length - totalBytesRead);
1643            totalBytesRead = totalBytesRead + bytesRead;
1644        }
1645
1646        String receivedString = new String(myBytes, 0, totalBytesRead);
1647        assertTrue("Could not recv on socket connected with timeout:"
1648                + receivedString + ":" + sendString, receivedString
1649                .equals(sendString));
1650
1651        sendString = new String("SEND - Test");
1652        theOutput2.write(sendString.getBytes());
1653        theOutput2.flush();
1654
1655        totalBytesRead = 0;
1656        myBytes = new byte[100];
1657        Thread.sleep(1000);
1658        while (theInput2.available() > 0) {
1659            int bytesRead = theInput2.read(myBytes, totalBytesRead,
1660                    myBytes.length - totalBytesRead);
1661            totalBytesRead = totalBytesRead + bytesRead;
1662        }
1663
1664        receivedString = new String(myBytes, 0, totalBytesRead);
1665        assertTrue("Could not send on socket connected with timeout:"
1666                + receivedString + ":" + sendString, receivedString
1667                .equals(sendString));
1668
1669        theSocket.close();
1670        serverSocket.close();
1671
1672        // now try to set options while we are connecting
1673        theSocket = new Socket();
1674        SocketConnector connector = new SocketConnector(5000, theSocket,
1675                nonReachableAddress);
1676        connector.start();
1677        theSocket.setSoTimeout(100);
1678        Thread.sleep(10);
1679        assertEquals("Socket option not set during connect: 10 ", 100,
1680                theSocket.getSoTimeout());
1681        Thread.sleep(50);
1682        theSocket.setSoTimeout(200);
1683        assertEquals("Socket option not set during connect: 50 ", 200,
1684                theSocket.getSoTimeout());
1685        Thread.sleep(5000);
1686        theSocket.close();
1687
1688        SocketChannel channel = SocketChannel.open();
1689        channel.configureBlocking(false);
1690        Socket socket = channel.socket();
1691        int port = Support_PortManager.getNextPort();
1692        try {
1693            socket.connect( new InetSocketAddress(InetAddress.getLocalHost(),
1694                    Support_PortManager.getNextPort()), port);
1695            fail("IllegalBlockingModeException was not thrown.");
1696        } catch(IllegalBlockingModeException ibme) {
1697            //expected
1698        }
1699    }
1700
1701    public void test_isInputShutdown() throws IOException {
1702        InetSocketAddress theAddress = new InetSocketAddress(InetAddress
1703                .getLocalHost(), Support_PortManager.getNextPort());
1704        Socket theSocket = new Socket();
1705        ServerSocket serverSocket = new ServerSocket();
1706        serverSocket.bind(theAddress);
1707        theSocket.connect(theAddress);
1708        Socket servSock = serverSocket.accept();
1709        InputStream theInput = theSocket.getInputStream();
1710        OutputStream theOutput = servSock.getOutputStream();
1711
1712        // make sure we get the right answer with newly connected socket
1713        assertFalse("Socket indicated input shutdown when it should not have",
1714                theSocket.isInputShutdown());
1715
1716        // shutdown the output
1717        theSocket.shutdownInput();
1718
1719        // make sure we get the right answer once it is shut down
1720        assertTrue(
1721                "Socket indicated input was NOT shutdown when it should have been",
1722                theSocket.isInputShutdown());
1723
1724        theSocket.close();
1725        serverSocket.close();
1726
1727        // make sure we get the right answer for closed sockets
1728        assertFalse(
1729                "Socket indicated input was shutdown when socket was closed",
1730                servSock.isInputShutdown());
1731
1732    }
1733
1734    public void test_isOutputShutdown() throws IOException {
1735        InetSocketAddress theAddress = new InetSocketAddress(InetAddress
1736                .getLocalHost(), Support_PortManager.getNextPort());
1737        Socket theSocket = new Socket();
1738        ServerSocket serverSocket = new ServerSocket();
1739        serverSocket.bind(theAddress);
1740        theSocket.connect(theAddress);
1741        Socket servSock = serverSocket.accept();
1742        InputStream theInput = theSocket.getInputStream();
1743        OutputStream theOutput = servSock.getOutputStream();
1744
1745        // make sure we get the right answer with newly connected socket
1746        assertFalse("Socket indicated output shutdown when it should not have",
1747                servSock.isOutputShutdown());
1748
1749        // shutdown the output
1750        servSock.shutdownOutput();
1751
1752        // make sure we get the right answer once it is shut down
1753        assertTrue(
1754                "Socket indicated output was NOT shutdown when it should have been",
1755                servSock.isOutputShutdown());
1756
1757        theSocket.close();
1758        serverSocket.close();
1759
1760        // make sure we get the right answer for closed sockets
1761        assertFalse(
1762                "Socket indicated output was output shutdown when the socket was closed",
1763                theSocket.isOutputShutdown());
1764
1765    }
1766
1767    public void test_setReuseAddressZ() {
1768
1769        try {
1770            InetAddress allAddresses[] = InetAddress.getAllByName(InetAddress
1771                    .getLocalHost().getHostName());
1772            if (allAddresses.length > 1) {
1773
1774                InetSocketAddress theAddress = new InetSocketAddress(
1775                        InetAddress.getLocalHost(), Support_PortManager
1776                                .getNextPort());
1777                ServerSocket serverSocket = new ServerSocket();
1778                serverSocket.bind(theAddress);
1779
1780                // try to bind to port address that is already in use with
1781                // reuseAddress = false.
1782                // On windows platforms the bind is allowed even then
1783                // reUseAddress is false (ONLY IF BOTH SOCKETS
1784                // ARE IPV4 Sockets) so our test uses the platform to determine
1785                // what the expected result is. It seems that on linux
1786                // platforms we also don't get an exception.
1787                InetSocketAddress theLocalAddress = new InetSocketAddress(
1788                        (InetAddress) allAddresses[1], Support_PortManager
1789                                .getNextPort());
1790                InetSocketAddress theOtherLocalAddress = new InetSocketAddress(
1791                        (InetAddress) allAddresses[0], theLocalAddress
1792                                .getPort());
1793                Socket theSocket = new Socket();
1794                theSocket.setReuseAddress(false);
1795                theSocket.bind(theLocalAddress);
1796                Socket theSocket2 = null;
1797                String platform = System.getProperty("os.name");
1798                try {
1799                    theSocket2 = new Socket();
1800                    theSocket2.setReuseAddress(false);
1801                    theSocket2.bind(theOtherLocalAddress);
1802
1803                    if ((!platform.startsWith("Linux"))
1804                            && ((!platform.startsWith("Windows")) ||
1805                            // for windows we don't get an exception with
1806                            // setreuse set to false unless one of the
1807                            // addresses we bind to is an IPv6 address and we
1808                            // are therefore using the IPv6 stack.
1809                            !((((InetAddress) allAddresses[0]) instanceof Inet4Address) && (((InetAddress) allAddresses[1]) instanceof Inet4Address)))) {
1810                        fail("No exception when setReuseAddress is false and we bind:"
1811                                + theLocalAddress.toString()
1812                                + ":"
1813                                + theOtherLocalAddress.toString());
1814                    }
1815                } catch (IOException ex) {
1816                    if ((platform.startsWith("Linux"))
1817                            || ((platform.startsWith("Windows")) && ((((InetAddress) allAddresses[0]) instanceof Inet4Address) && (((InetAddress) allAddresses[1]) instanceof Inet4Address)))) {
1818                        fail("Got unexpected exception when binding with setReuseAddress false on windows platform:"
1819                                + theAddress.toString() + ":" + ex.toString());
1820                    }
1821                }
1822                theSocket.close();
1823                theSocket2.close();
1824
1825                // try to bind to port that is already in use with reuseAddress
1826                // = true
1827                theLocalAddress = new InetSocketAddress(
1828                        (InetAddress) allAddresses[0], Support_PortManager
1829                                .getNextPort());
1830                theOtherLocalAddress = new InetSocketAddress(
1831                        (InetAddress) allAddresses[1], theLocalAddress
1832                                .getPort());
1833
1834                theSocket = new Socket();
1835                theSocket.setReuseAddress(true);
1836                theSocket.bind(theLocalAddress);
1837                try {
1838                    theSocket2 = new Socket();
1839                    theSocket2.setReuseAddress(true);
1840                    theSocket2.bind(theOtherLocalAddress);
1841                    theSocket2.close();
1842                } catch (IOException ex) {
1843                    fail("IOException when setReuseAddress is true and we bind :"
1844                            + ex.toString());
1845                }
1846                theSocket.close();
1847                serverSocket.close();
1848
1849                // try with default behavior which should be the same on all
1850                // platforms
1851                theLocalAddress = new InetSocketAddress(
1852                        (InetAddress) allAddresses[0], Support_PortManager
1853                                .getNextPort());
1854                theOtherLocalAddress = new InetSocketAddress(
1855                        (InetAddress) allAddresses[1], theLocalAddress
1856                                .getPort());
1857
1858                theSocket = new Socket();
1859                theSocket.bind(theLocalAddress);
1860                try {
1861                    theSocket2 = new Socket();
1862                    theSocket2.bind(theOtherLocalAddress);
1863                    theSocket2.close();
1864                    if ((!platform.startsWith("Linux"))
1865                            && ((!platform.startsWith("Windows")) || !((((InetAddress) allAddresses[0]) instanceof Inet4Address) && (((InetAddress) allAddresses[1]) instanceof Inet4Address)))) {
1866                        fail("No exception when setReuseAddress is default and we bind:"
1867                                + theLocalAddress.toString()
1868                                + ":"
1869                                + theOtherLocalAddress.toString());
1870                    }
1871                } catch (IOException ex) {
1872                    if ((platform.startsWith("Linux"))
1873                            || ((platform.startsWith("Windows")) && ((((InetAddress) allAddresses[0]) instanceof Inet4Address) && (((InetAddress) allAddresses[1]) instanceof Inet4Address)))) {
1874                        fail("Got unexpected exception when binding with setReuseAddress default on windows platform:"
1875                                + theAddress.toString() + ":" + ex.toString());
1876                    }
1877                }
1878                theSocket.close();
1879                serverSocket.close();
1880
1881                ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_REUSEADDR);
1882            }
1883        } catch (Exception e) {
1884            handleException(e, SO_REUSEADDR);
1885        }
1886
1887        try {
1888            Socket theSocket = new Socket();
1889            theSocket.close();
1890            theSocket.setReuseAddress(true);
1891            fail("SocketException was not thrown.");
1892        } catch(SocketException ioe) {
1893            //expected
1894        } catch(IOException ioe) {
1895            fail("IOException was thrown.");
1896        }
1897    }
1898
1899    public void test_getReuseAddress() {
1900        try {
1901            Socket theSocket = new Socket();
1902            theSocket.setReuseAddress(true);
1903            assertTrue("getReuseAddress false when it should be true",
1904                    theSocket.getReuseAddress());
1905            theSocket.setReuseAddress(false);
1906            assertFalse("getReuseAddress true when it should be False",
1907                    theSocket.getReuseAddress());
1908            ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_REUSEADDR);
1909        } catch (Exception e) {
1910            handleException(e, SO_REUSEADDR);
1911        }
1912
1913        try {
1914            Socket newSocket = new Socket();
1915            newSocket.close();
1916            try {
1917                newSocket.getReuseAddress();
1918                fail("SocketException was not thrown.");
1919            } catch(SocketException e) {
1920                //expected
1921            }
1922        } catch(Exception e) {
1923            fail("Unexpected exception.");
1924        }
1925    }
1926
1927    public void test_setOOBInlineZ() {
1928        // mostly tested in getOOBInline. Just set to make sure call works ok
1929        try {
1930            new InetSocketAddress(InetAddress.getLocalHost(),
1931                    Support_PortManager.getNextPort());
1932            Socket theSocket = new Socket();
1933            theSocket.setOOBInline(true);
1934            assertTrue("expected OOBIline to be true", theSocket.getOOBInline());
1935            ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_OOBINLINE);
1936        } catch (Exception e) {
1937            handleException(e, SO_OOBINLINE);
1938        }
1939
1940        try {
1941            Socket theSocket = new Socket();
1942            theSocket.close();
1943            theSocket.setOOBInline(true);
1944            fail("SocketException was not thrown.");
1945        } catch(SocketException ioe) {
1946            //expected
1947        } catch(IOException ioe) {
1948            fail("IOException was thrown.");
1949        }
1950    }
1951
1952    public void test_getOOBInline() {
1953
1954        try {
1955            new InetSocketAddress(InetAddress.getLocalHost(),
1956                    Support_PortManager.getNextPort());
1957            Socket theSocket = new Socket();
1958
1959            // validate that value reflects what we set it to true after true,
1960            // false after false and false after false false
1961            theSocket.setOOBInline(true);
1962            assertTrue("expected OOBIline to be true", theSocket.getOOBInline());
1963            theSocket.setOOBInline(false);
1964            assertFalse("expected OOBIline to be true", theSocket
1965                    .getOOBInline());
1966            theSocket.setOOBInline(false);
1967            assertFalse("expected OOBIline to be true", theSocket
1968                    .getOOBInline());
1969            theSocket.close();
1970            try {
1971                theSocket.getOOBInline();
1972                fail("SocketException was not thrown.");
1973            } catch(SocketException se) {
1974                //expected
1975            }
1976            ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_OOBINLINE);
1977
1978        } catch (Exception e) {
1979            handleException(e, SO_OOBINLINE);
1980        }
1981    }
1982
1983    public void test_setTrafficClassI() {
1984        try {
1985            int IPTOS_LOWCOST = 0x2;
1986            int IPTOS_THROUGHPUT = 0x8;
1987
1988            new InetSocketAddress(InetAddress.getLocalHost(),
1989                    Support_PortManager.getNextPort());
1990            Socket theSocket = new Socket();
1991
1992            // validate that value set must be between 0 and 255
1993            try {
1994                theSocket.setTrafficClass(256);
1995                fail("No exception was thrown when traffic class set to 256");
1996            } catch (IllegalArgumentException e) {
1997            }
1998
1999            try {
2000                theSocket.setTrafficClass(-1);
2001                fail("No exception was thrown when traffic class set to -1");
2002            } catch (IllegalArgumentException e) {
2003            }
2004
2005            // now validate that we can set it to some good values
2006            theSocket.setTrafficClass(IPTOS_LOWCOST);
2007            theSocket.setTrafficClass(IPTOS_THROUGHPUT);
2008            ensureExceptionThrownIfOptionIsUnsupportedOnOS(IP_TOS);
2009        } catch (Exception e) {
2010            handleException(e, IP_TOS);
2011        }
2012
2013        try {
2014            Socket theSocket = new Socket();
2015            theSocket.close();
2016            theSocket.setTrafficClass(0);
2017            fail("SocketException was not thrown.");
2018        } catch(SocketException ioe) {
2019            //expected
2020        } catch(IOException ioe) {
2021            fail("IOException was thrown.");
2022        }
2023    }
2024
2025    public void test_getTrafficClass() {
2026        try {
2027            new InetSocketAddress(InetAddress.getLocalHost(),
2028                    Support_PortManager.getNextPort());
2029            Socket theSocket = new Socket();
2030
2031            /*
2032             * we cannot actually check that the values are set as if a platform
2033             * does not support the option then it may come back unset even
2034             * though we set it so just get the value to make sure we can get it
2035             */
2036            int trafficClass = theSocket.getTrafficClass();
2037            ensureExceptionThrownIfOptionIsUnsupportedOnOS(IP_TOS);
2038        } catch (Exception e) {
2039            handleException(e, IP_TOS);
2040        }
2041    }
2042
2043    public void test_getChannel() throws Exception {
2044        assertNull(new Socket().getChannel());
2045
2046        SocketChannel channel = SocketChannel.open();
2047        Socket socket = channel.socket();
2048        assertEquals(channel, socket.getChannel());
2049        socket.close();
2050        channel.close();
2051    }
2052
2053    public void test_sendUrgentDataI() {
2054
2055        // Some platforms may not support urgent data in this case we will not
2056        // run these tests. For now run on all platforms until we find those
2057        // that do not support urgent data
2058        String platform = System.getProperty("os.name");
2059        if (!platform.equals("Dummy")) {
2060            // validate that when OOBInline is false that any urgent data
2061            // is silently ignored
2062            String urgentData = "U";
2063            try {
2064                InetSocketAddress theAddress = new InetSocketAddress(
2065                        InetAddress.getLocalHost(), Support_PortManager
2066                                .getNextPort());
2067                Socket theSocket = new Socket();
2068                ServerSocket serverSocket = new ServerSocket();
2069                serverSocket.bind(theAddress);
2070                theSocket.connect(theAddress);
2071                Socket servSock = serverSocket.accept();
2072                InputStream theInput = theSocket.getInputStream();
2073                OutputStream theOutput = servSock.getOutputStream();
2074
2075                // send the regular data
2076                String sendString = new String("Test");
2077                theOutput.write(sendString.getBytes());
2078                theOutput.flush();
2079
2080                // send the urgent data which should not be received
2081                theSocket.setOOBInline(false);
2082                servSock.sendUrgentData(urgentData.getBytes()[0]);
2083                theOutput.write(sendString.getBytes());
2084                theOutput.flush();
2085
2086                // give things some time to settle
2087                Thread.sleep(1000);
2088
2089                int totalBytesRead = 0;
2090                byte[] myBytes = new byte[100];
2091                while (theInput.available() > 0) {
2092                    int bytesRead = theInput.read(myBytes, totalBytesRead,
2093                            myBytes.length - totalBytesRead);
2094                    totalBytesRead = totalBytesRead + bytesRead;
2095                }
2096
2097                String receivedString = new String(myBytes, 0, totalBytesRead);
2098                //assertTrue("Urgent Data seems to have been received:"
2099                //        + receivedString + ":" + sendString, receivedString
2100                //        .equals(sendString + sendString));
2101
2102                theSocket.close();
2103                serverSocket.close();
2104
2105                // now validate that urgent data is received as expected. Expect
2106                // that it should be between the two writes.
2107                theAddress = new InetSocketAddress(InetAddress.getLocalHost(),
2108                        Support_PortManager.getNextPort());
2109                theSocket = new Socket();
2110                serverSocket = new ServerSocket();
2111                serverSocket.bind(theAddress);
2112                theSocket.connect(theAddress);
2113                servSock = serverSocket.accept();
2114                theInput = theSocket.getInputStream();
2115                theOutput = servSock.getOutputStream();
2116
2117                // send the regular data
2118                sendString = new String("Test - Urgent Data");
2119                theOutput.write(sendString.getBytes());
2120                theOutput.flush();
2121
2122                // send the urgent data which should be received
2123                theSocket.setOOBInline(true);
2124                servSock.sendUrgentData(urgentData.getBytes()[0]);
2125
2126                theOutput.write(sendString.getBytes());
2127                theOutput.flush();
2128
2129                Thread.sleep(1000);
2130
2131                totalBytesRead = 0;
2132                myBytes = new byte[100];
2133                while (theInput.available() > 0) {
2134                    int bytesRead = theInput.read(myBytes, totalBytesRead,
2135                            myBytes.length - totalBytesRead);
2136                    totalBytesRead = totalBytesRead + bytesRead;
2137                }
2138
2139                receivedString = new String(myBytes, 0, totalBytesRead);
2140                assertTrue("Urgent Data was not received with one urgent byte:"
2141                        + receivedString + ":" + sendString + urgentData
2142                        + sendString, receivedString.equals(sendString
2143                        + urgentData + sendString));
2144
2145                theSocket.close();
2146                serverSocket.close();
2147
2148                // now test case where we try to send two urgent bytes.
2149                theAddress = new InetSocketAddress(InetAddress.getLocalHost(),
2150                        Support_PortManager.getNextPort());
2151                theSocket = new Socket();
2152                serverSocket = new ServerSocket();
2153                serverSocket.bind(theAddress);
2154                theSocket.connect(theAddress);
2155                servSock = serverSocket.accept();
2156                theInput = theSocket.getInputStream();
2157                theOutput = servSock.getOutputStream();
2158
2159                // send the regular data
2160                sendString = new String("Test - Urgent Data");
2161                theOutput.write(sendString.getBytes());
2162                theOutput.flush();
2163
2164                // send the urgent data which should not be received
2165                theSocket.setOOBInline(true);
2166                servSock.sendUrgentData(urgentData.getBytes()[0]);
2167                servSock.sendUrgentData(urgentData.getBytes()[0]);
2168
2169                theOutput.write(sendString.getBytes());
2170                theOutput.flush();
2171
2172                Thread.sleep(1000);
2173
2174                totalBytesRead = 0;
2175                myBytes = new byte[100];
2176                while (theInput.available() > 0) {
2177                    int bytesRead = theInput.read(myBytes, totalBytesRead,
2178                            myBytes.length - totalBytesRead);
2179                    totalBytesRead = totalBytesRead + bytesRead;
2180                }
2181
2182                receivedString = new String(myBytes, 0, totalBytesRead);
2183                assertTrue(
2184                        "Did not get right byte of urgent data when two sent:"
2185                                + receivedString + ":" + sendString
2186                                + urgentData + urgentData + sendString,
2187                        receivedString.equals(sendString + urgentData
2188                                + urgentData + sendString));
2189
2190                theSocket.close();
2191                serverSocket.close();
2192
2193                /*
2194                 * TODO : These do not currently pass on XP SP2 and Server 2003
2195                 */
2196                if (!platform.startsWith("Windows")) {
2197                    // now test the case were we send turn the OOBInline on/off
2198                    theAddress = new InetSocketAddress(InetAddress
2199                            .getLocalHost(), Support_PortManager.getNextPort());
2200                    theSocket = new Socket();
2201                    serverSocket = new ServerSocket();
2202                    serverSocket.bind(theAddress);
2203                    theSocket.connect(theAddress);
2204                    servSock = serverSocket.accept();
2205                    theInput = theSocket.getInputStream();
2206                    theOutput = servSock.getOutputStream();
2207
2208                    // send the regular data
2209                    sendString = new String("Test - Urgent Data");
2210                    theOutput.write(sendString.getBytes());
2211                    theOutput.flush();
2212
2213                    // send the urgent data which should be received
2214                    theSocket.setOOBInline(true);
2215                    servSock.sendUrgentData(urgentData.getBytes()[0]);
2216
2217                    theOutput.write(sendString.getBytes());
2218                    theOutput.flush();
2219
2220                    Thread.sleep(1000);
2221
2222                    totalBytesRead = 0;
2223                    myBytes = new byte[100];
2224                    while (theInput.available() > 0) {
2225                        int bytesRead = theInput.read(myBytes, totalBytesRead,
2226                                myBytes.length - totalBytesRead);
2227                        totalBytesRead = totalBytesRead + bytesRead;
2228                    }
2229
2230                    receivedString = new String(myBytes, 0, totalBytesRead);
2231                    assertTrue(
2232                            "Did not get urgent data when turning on/off(1):"
2233                                    + receivedString + ":" + sendString
2234                                    + urgentData + sendString, receivedString
2235                                    .equals(sendString + urgentData
2236                                            + sendString));
2237
2238                    // send the regular data
2239                    sendString = new String("Test - Urgent Data");
2240                    theOutput.write(sendString.getBytes());
2241                    theOutput.flush();
2242
2243                    // send the urgent data which should not be received
2244                    theSocket.setOOBInline(false);
2245                    servSock.sendUrgentData(urgentData.getBytes()[0]);
2246
2247                    // send trailing data
2248                    theOutput.write(sendString.getBytes());
2249                    theOutput.flush();
2250
2251                    Thread.sleep(1000);
2252
2253                    totalBytesRead = 0;
2254                    myBytes = new byte[100];
2255                    while (theInput.available() > 0) {
2256                        int bytesRead = theInput.read(myBytes, totalBytesRead,
2257                                myBytes.length - totalBytesRead);
2258                        totalBytesRead = totalBytesRead + bytesRead;
2259                    }
2260
2261                    receivedString = new String(myBytes, 0, totalBytesRead);
2262                    //assertTrue(
2263                    //        "Got unexpected data data when turning on/off(2):"
2264                    //                + receivedString + ":" + sendString
2265                    //               + sendString, receivedString
2266                    //                .equals(sendString + sendString));
2267
2268                    // now turn back on and get data. Here we also
2269                    // get the previously sent byte of urgent data as it is
2270                    // still in the urgent buffer
2271
2272                    // send the regular data
2273                    sendString = new String("Test - Urgent Data");
2274                    theOutput.write(sendString.getBytes());
2275                    theOutput.flush();
2276
2277                    // send the urgent data which should be received again
2278                    theSocket.setOOBInline(true);
2279                    servSock.sendUrgentData(urgentData.getBytes()[0]);
2280
2281                    theOutput.write(sendString.getBytes());
2282                    theOutput.flush();
2283
2284                    Thread.sleep(1000);
2285
2286                    totalBytesRead = 0;
2287                    myBytes = new byte[100];
2288                    while (theInput.available() > 0) {
2289                        int bytesRead = theInput.read(myBytes, totalBytesRead,
2290                                myBytes.length - totalBytesRead);
2291                        totalBytesRead = totalBytesRead + bytesRead;
2292                    }
2293
2294                    receivedString = new String(myBytes, 0, totalBytesRead);
2295                    // depending on the platform we may get the previously sent
2296                    // urgent data or not (examples windows-yes, Linux-no).
2297                    // So accept either so long as we get the urgent data from
2298                    // when it was on.
2299                    //assertTrue(
2300                    //        "Did not get urgent data when turning on/off(3) GOT:"
2301                    //                + receivedString + ":Expected" + urgentData
2302                    //                + sendString + urgentData + sendString
2303                    //                + ":OR:" + sendString + urgentData
2304                    //                + sendString,
2305                    //        (receivedString.equals(urgentData + sendString
2306                    //                + urgentData + sendString) || receivedString
2307                    //                .equals(sendString + urgentData
2308                    //                        + sendString)));
2309
2310                    theSocket.close();
2311                    serverSocket.close();
2312                }
2313
2314                // now test the case where there is only urgent data
2315                theAddress = new InetSocketAddress(InetAddress.getLocalHost(),
2316                        Support_PortManager.getNextPort());
2317                theSocket = new Socket();
2318                serverSocket = new ServerSocket();
2319                serverSocket.bind(theAddress);
2320                theSocket.connect(theAddress);
2321                servSock = serverSocket.accept();
2322                theInput = theSocket.getInputStream();
2323                theOutput = servSock.getOutputStream();
2324
2325                // send the urgent data which should not be received.
2326                theSocket.setOOBInline(true);
2327                servSock.sendUrgentData(urgentData.getBytes()[0]);
2328
2329                Thread.sleep(1000);
2330
2331                totalBytesRead = 0;
2332                myBytes = new byte[100];
2333                while (theInput.available() > 0) {
2334                    int bytesRead = theInput.read(myBytes, totalBytesRead,
2335                            myBytes.length - totalBytesRead);
2336                    totalBytesRead = totalBytesRead + bytesRead;
2337                }
2338
2339                receivedString = new String(myBytes, 0, totalBytesRead);
2340                assertTrue("Did not get urgent data only urgent data sent:"
2341                        + receivedString + ":" + urgentData, receivedString
2342                        .equals(urgentData));
2343
2344            } catch (Exception e) {
2345                // for platforms that do not support urgent data we expect an
2346                // exception. For the others report an error.
2347                // TODO : Need to introduce a better test for the exception
2348                // so that the failure only occurs on platforms that support
2349                // urgent data
2350                fail("Platform:" + platform
2351                        + ": Got exception during sendUrgent data tests"
2352                        + e.toString());
2353            }
2354        }
2355
2356        try {
2357            Socket theSocket = new Socket();
2358            theSocket.close();
2359            theSocket.sendUrgentData(0);
2360            fail("IOException was not thrown.");
2361        } catch(IOException ioe) {
2362            //expected
2363        }
2364    }
2365
2366    public void test_setPerformancePreference_Int_Int_Int() throws Exception {
2367        Socket theSocket = new Socket();
2368        theSocket.setPerformancePreferences(1, 1, 1);
2369    }
2370
2371    public void test_ConstructorLjava_net_Proxy_Exception() {
2372
2373        SocketAddress addr1 = InetSocketAddress.createUnresolved("127.0.0.1",
2374                80);
2375        SocketAddress addr2 = new InetSocketAddress("localhost", 80);
2376
2377        Proxy proxy1 = new Proxy(Proxy.Type.HTTP, addr1);
2378        // IllegalArgumentException test
2379        try {
2380            new Socket(proxy1);
2381            fail("should throw IllegalArgumentException");
2382        } catch (IllegalArgumentException e) {
2383            // expected
2384        }
2385
2386        Proxy proxy2 = new Proxy(Proxy.Type.SOCKS, addr1);
2387        // should not throw any exception
2388        new Socket(proxy2);
2389        new Socket(Proxy.NO_PROXY);
2390
2391        try {
2392            new Socket((Proxy) null);
2393            fail("IllegalArgumentException was not thrown.");
2394        } catch(IllegalArgumentException iae) {
2395            //expected
2396        }
2397    }
2398
2399    public void test_ConstructorLSocketImpl() {
2400        MockSocketImpl msi = new MockSocketImpl();
2401        try {
2402            new TestSocket(msi);
2403        } catch (SocketException e) {
2404            fail("SocketException was thrown.");
2405        }
2406    }
2407
2408    public void test_connect_unknownhost() throws Exception {
2409        Socket socket = new Socket();
2410        InetSocketAddress socketAddress = new InetSocketAddress("unknownhost", 12345);
2411        try {
2412            socket.connect(socketAddress);
2413            fail("Should throw IOException");
2414        } catch (IOException e) {
2415            // expected
2416        }
2417    }
2418
2419    public void test_connect_unresolved_unknown() throws Exception {
2420        Socket socket = new Socket();
2421        InetSocketAddress unresolved = InetSocketAddress.createUnresolved("unknownhost", 12345);
2422        try {
2423            socket.connect(unresolved);
2424            fail("Should throw IOException");
2425        } catch (IOException e) {
2426            // expected
2427        }
2428    }
2429
2430    public void test_connect_unresolved() throws Exception {
2431        Socket socket = new Socket();
2432        InetSocketAddress unresolvedSocketAddress = InetSocketAddress.createUnresolved(
2433                Support_Configuration.SocksServerTestHost,
2434                Support_Configuration.SocksServerTestPort);
2435        try {
2436            socket.connect(unresolvedSocketAddress);
2437            fail("Should throw IOException");
2438        } catch (IOException e) {
2439            // expected
2440        }
2441    }
2442
2443    public void test_getOutputStream_shutdownOutput() throws Exception {
2444        // regression test for Harmony-873
2445        ServerSocket ss = new ServerSocket(0);
2446        Socket s = new Socket("127.0.0.1", ss.getLocalPort());
2447        ss.accept();
2448        s.shutdownOutput();
2449        try {
2450            s.getOutputStream();
2451            fail("should throw SocketException");
2452        } catch (IOException e) {
2453            // expected
2454        } finally {
2455            s.close();
2456        }
2457
2458        SocketChannel channel = SocketChannel.open(
2459                new InetSocketAddress(ss.getInetAddress(), ss.getLocalPort()));
2460        channel.configureBlocking(false);
2461        ss.accept();
2462        Socket socket = channel.socket();
2463
2464        OutputStream out = null;
2465
2466        try {
2467            out = socket.getOutputStream();
2468            out.write(1);
2469            fail("IllegalBlockingModeException was not thrown.");
2470        } catch(IllegalBlockingModeException ibme) {
2471            //expected
2472        } finally {
2473            if(out != null) out.close();
2474            socket.close();
2475            channel.close();
2476        }
2477    }
2478
2479    public void test_shutdownInputOutput_twice() throws Exception {
2480        // regression test for Harmony-2944
2481        Socket s = new Socket("0.0.0.0", 0, false);
2482        s.shutdownInput();
2483
2484        try {
2485            s.shutdownInput();
2486            fail("should throw SocketException");
2487        } catch (SocketException se) {
2488            // expected
2489        }
2490        s.shutdownOutput();
2491
2492        try {
2493            s.shutdownOutput();
2494            fail("should throw SocketException");
2495        } catch (SocketException se) {
2496            // expected
2497        }
2498    }
2499
2500    /**
2501     * Sets up the fixture, for example, open a network connection. This method
2502     * is called before a test is executed.
2503     *
2504     * @throws Exception
2505     */
2506    protected void setUp() throws Exception {
2507        super.setUp();
2508    }
2509
2510    /**
2511     * Tears down the fixture, for example, close a network connection. This
2512     * method is called after a test is executed.
2513     */
2514    protected void tearDown() {
2515        try {
2516            if (s != null)
2517                s.close();
2518        } catch (Exception e) {
2519        }
2520        try {
2521            if (ss != null)
2522                ss.close();
2523        } catch (Exception e) {
2524        }
2525        try {
2526            if (t != null)
2527                t.interrupt();
2528        } catch (Exception e) {
2529        }
2530    }
2531
2532    static class MockSecurityManager extends SecurityManager {
2533
2534        public void checkConnect(String host, int port) {
2535            if ("127.0.0.1".equals(host)) {
2536                throw new SecurityException("permission is not allowed");
2537            }
2538        }
2539
2540        public void checkPermission(Permission permission) {
2541            return;
2542        }
2543
2544    }
2545
2546    /**
2547     *
2548     */
2549    protected int startServer(String name) {
2550        int portNumber = Support_PortManager.getNextPort();
2551        try {
2552            ss = new ServerSocket(portNumber, 5);
2553        } catch (IOException e) {
2554            fail(name + ": " + e);
2555        }
2556        return ss.getLocalPort();
2557    }
2558
2559    class MockSocketImpl extends SocketImpl {
2560
2561        public MockSocketImpl() {
2562            super();
2563        }
2564
2565        @Override
2566        protected void accept(SocketImpl arg0) throws IOException {
2567        }
2568
2569        @Override
2570        protected int available() throws IOException {
2571            return 0;
2572        }
2573
2574        @Override
2575        protected void bind(InetAddress arg0, int arg1) throws IOException {
2576        }
2577
2578        @Override
2579        protected void close() throws IOException {
2580        }
2581
2582        @Override
2583        protected void connect(String arg0, int arg1) throws IOException {
2584        }
2585
2586        @Override
2587        protected void connect(InetAddress arg0, int arg1) throws IOException {
2588        }
2589
2590        @Override
2591        protected void connect(SocketAddress arg0, int arg1) throws IOException {
2592        }
2593
2594        @Override
2595        protected void create(boolean arg0) throws IOException {
2596        }
2597
2598        @Override
2599        protected InputStream getInputStream() throws IOException {
2600            return null;
2601        }
2602
2603        @Override
2604        protected OutputStream getOutputStream() throws IOException {
2605            return null;
2606        }
2607
2608        @Override
2609        protected void listen(int arg0) throws IOException {
2610        }
2611
2612        @Override
2613        protected void sendUrgentData(int arg0) throws IOException {
2614        }
2615
2616        public Object getOption(int arg0) throws SocketException {
2617            return null;
2618        }
2619
2620        public void setOption(int arg0, Object arg1) throws SocketException {
2621        }
2622    }
2623}