716N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
716N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
716N/A *
716N/A * This code is free software; you can redistribute it and/or modify it
716N/A * under the terms of the GNU General Public License version 2 only, as
716N/A * published by the Free Software Foundation.
716N/A *
716N/A * This code is distributed in the hope that it will be useful, but WITHOUT
716N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
716N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
716N/A * version 2 for more details (a copy is included in the LICENSE file that
716N/A * accompanied this code).
716N/A *
716N/A * You should have received a copy of the GNU General Public License version
716N/A * 2 along with this work; if not, write to the Free Software Foundation,
716N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
716N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
716N/A */
716N/A
716N/A/**
716N/A * @test LocalRMIServerSocketFactoryTest.java
716N/A * @bug 6774170
716N/A * @summary Connect to a server socket returned by the LocalRMIServerSocketFactory.
716N/A *
716N/A * @author Daniel Fuchs
716N/A *
716N/A * @run compile -XDignore.symbol.file=true -source 1.6 -g LocalRMIServerSocketFactoryTest.java
716N/A * @run main LocalRMIServerSocketFactoryTest
716N/A */
716N/A
716N/Aimport sun.management.jmxremote.LocalRMIServerSocketFactory;
716N/Aimport java.io.IOException;
716N/Aimport java.net.InetAddress;
716N/Aimport java.net.NetworkInterface;
716N/Aimport java.net.ServerSocket;
716N/Aimport java.net.Socket;
716N/Aimport java.util.concurrent.SynchronousQueue;
716N/A
716N/Apublic class LocalRMIServerSocketFactoryTest {
716N/A
716N/A private static final SynchronousQueue<Exception> queue =
716N/A new SynchronousQueue<Exception>();
716N/A
716N/A static final class Result extends Exception {
716N/A
716N/A private Result() {
716N/A super("SUCCESS: No exception was thrown");
716N/A }
716N/A static final Result SUCCESS = new Result();
716N/A }
716N/A
716N/A private static void checkError(String message) throws Exception {
716N/A
716N/A // Wait for the server to set the error field.
716N/A final Exception x = queue.take();
716N/A
716N/A if (x == Result.SUCCESS) {
716N/A return;
716N/A }
716N/A
716N/A
716N/A // case of 6674166: this is very unlikely to happen, even if
716N/A // both 6674166 and 6774170 aren't fixed. If it happens
716N/A // however, it might indicate that neither defects are fixed.
716N/A
716N/A if (x instanceof NullPointerException) {
716N/A throw new Exception(message + " - " +
716N/A "Congratulations! it seems you have triggered 6674166. " +
716N/A "Neither 6674166 nor 6774170 seem to be fixed: " + x, x);
716N/A } else if (x instanceof IOException) {
716N/A throw new Exception(message + " - " +
716N/A "Unexpected IOException. Maybe you triggered 6674166? " +
716N/A x, x);
716N/A } else if (x != null) {
716N/A throw new Exception(message + " - " +
716N/A "Ouch, that's bad. " +
716N/A "This is a new kind of unexpected exception " +
716N/A x, x);
716N/A }
716N/A }
716N/A
716N/A public static void main(String[] args) throws Exception {
716N/A final LocalRMIServerSocketFactory f =
716N/A new LocalRMIServerSocketFactory();
716N/A final ServerSocket s = f.createServerSocket(0);
716N/A final int port = s.getLocalPort();
716N/A Thread t = new Thread() {
716N/A
716N/A public void run() {
716N/A while (true) {
716N/A Exception error = Result.SUCCESS;
716N/A try {
716N/A System.err.println("Accepting: ");
716N/A final Socket ss = s.accept();
716N/A System.err.println(ss.getInetAddress() + " accepted");
716N/A } catch (Exception x) {
716N/A x.printStackTrace();
716N/A error = x;
716N/A } finally {
716N/A try {
716N/A // wait for the client to get the exception.
716N/A queue.put(error);
716N/A } catch (Exception x) {
716N/A // too bad!
716N/A System.err.println("Could't send result to client!");
716N/A x.printStackTrace();
716N/A return;
716N/A }
716N/A }
716N/A }
716N/A }
716N/A };
716N/A t.setDaemon(true);
716N/A t.start();
716N/A
716N/A System.err.println("new Socket((String)null, port)");
716N/A final Socket s1 = new Socket((String) null, port);
716N/A checkError("new Socket((String)null, port)");
716N/A s1.close();
716N/A System.err.println("new Socket((String)null, port): PASSED");
716N/A
716N/A System.err.println("new Socket(InetAddress.getByName(null), port)");
716N/A final Socket s2 = new Socket(InetAddress.getByName(null), port);
716N/A checkError("new Socket(InetAddress.getByName(null), port)");
716N/A s2.close();
716N/A System.err.println("new Socket(InetAddress.getByName(null), port): PASSED");
716N/A
716N/A System.err.println("new Socket(localhost, port)");
716N/A final Socket s3 = new Socket("localhost", port);
716N/A checkError("new Socket(localhost, port)");
716N/A s3.close();
716N/A System.err.println("new Socket(localhost, port): PASSED");
716N/A
716N/A System.err.println("new Socket(127.0.0.1, port)");
716N/A final Socket s4 = new Socket("127.0.0.1", port);
716N/A checkError("new Socket(127.0.0.1, port)");
716N/A s4.close();
716N/A System.err.println("new Socket(127.0.0.1, port): PASSED");
716N/A
716N/A }
716N/A}