Exemple #1
0
 void check(boolean pass) {
   if (pass) {
     passed();
   } else {
     failed();
   }
 }
Exemple #2
0
  void ServerSocketTests() throws Exception {
    ServerSocket s1 = new ServerSocket();

    test("ServerSocket.setReuseAddress(true)");
    s1.setReuseAddress(true);
    check(s1.getReuseAddress());

    test("Socket.setReuseAddress(false)");
    s1.setReuseAddress(false);
    check(!s1.getReuseAddress());

    /* bind to any port */
    s1.bind(new InetSocketAddress(0));

    test("Binding ServerSocket to port already in use should throw " + "a BindException");
    ServerSocket s2 = new ServerSocket();
    try {
      s2.bind(new InetSocketAddress(s1.getLocalPort()));
      failed();
    } catch (BindException e) {
      passed();
    }
    s2.close();

    s1.close();
  }
Exemple #3
0
  void DatagramSocketTests() throws Exception {
    DatagramSocket s1 = new DatagramSocket(null);

    test("DatagramSocket should be created with SO_REUSEADDR disabled");
    check(!s1.getReuseAddress());

    test("DatagramSocket.setReuseAddress(true)");
    s1.setReuseAddress(true);
    check(s1.getReuseAddress());

    test("DatagramSocket.setReuseAddress(false)");
    s1.setReuseAddress(false);
    check(!s1.getReuseAddress());

    /* bind to any port */
    s1.bind(new InetSocketAddress(0));

    test("Binding datagram socket to port already in use should throw " + "a BindException");
    DatagramSocket s2 = new DatagramSocket(null);
    try {
      s2.bind(new InetSocketAddress(s1.getLocalPort()));
      failed();
    } catch (BindException e) {
      passed();
    }
    s2.close();
    s1.close();

    // bind with SO_REUSEADDR enabled

    s1 = new DatagramSocket(null);
    s1.setReuseAddress(true);
    s1.bind(new InetSocketAddress(0));

    test(
        "Bind 2 datagram sockets to the same port - second "
            + "bind doesn't have SO_REUSEADDR enabled");
    s2 = new DatagramSocket(null);
    try {
      s2.bind(new InetSocketAddress(s1.getLocalPort()));
      failed();
    } catch (BindException e) {
      passed();
    }
    s2.close();

    test("Bind 2 datagram sockets to the same port - both have " + "SO_REUSEADDR enabled");
    s2 = new DatagramSocket(null);
    s2.setReuseAddress(true);
    try {
      s2.bind(new InetSocketAddress(s1.getLocalPort()));
      passed();
    } catch (BindException e) {
      if (System.getProperty("sun.net.useExclusiveBind") != null) {
        // exclusive bind enabled - expected result
        passed();
      } else {
        failed();
      }
    }
    s2.close();

    s1.close();
  }