1 /*
   2  * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  *  @test LocalRMIServerSocketFactoryTest.java
  26  *  @bug 6774170
  27  *  @summary Connect to a server socket returned by the LocalRMIServerSocketFactory.
  28  *
  29  *  @author Daniel Fuchs
  30  *
  31  *  @run compile -XDignore.symbol.file=true -source 1.6 -g LocalRMIServerSocketFactoryTest.java
  32  *  @run main LocalRMIServerSocketFactoryTest
  33  */
  34 
  35 import sun.management.jmxremote.LocalRMIServerSocketFactory;
  36 import java.io.IOException;
  37 import java.net.InetAddress;
  38 import java.net.NetworkInterface;
  39 import java.net.ServerSocket;
  40 import java.net.Socket;
  41 import java.util.concurrent.SynchronousQueue;
  42 
  43 public class LocalRMIServerSocketFactoryTest {
  44 
  45     private static final SynchronousQueue<Exception> queue =
  46             new SynchronousQueue<Exception>();
  47 
  48     static final class Result extends Exception {
  49 
  50         private Result() {
  51             super("SUCCESS: No exception was thrown");
  52         }
  53         static final Result SUCCESS = new Result();
  54     }
  55 
  56     private static void checkError(String message) throws Exception {
  57 
  58         // Wait for the server to set the error field.
  59         final Exception x = queue.take();
  60 
  61         if (x == Result.SUCCESS) {
  62             return;
  63         }
  64 
  65 
  66         // case of 6674166: this is very unlikely to happen, even if
  67         //     both 6674166 and 6774170 aren't fixed. If it happens
  68         //     however, it might indicate that neither defects are fixed.
  69 
  70         if (x instanceof NullPointerException) {
  71             throw new Exception(message + " - " +
  72                     "Congratulations! it seems you have triggered 6674166. " +
  73                     "Neither 6674166 nor 6774170 seem to be fixed: " + x, x);
  74         } else if (x instanceof IOException) {
  75             throw new Exception(message + " - " +
  76                     "Unexpected IOException. Maybe you triggered 6674166? " +
  77                     x, x);
  78         } else if (x != null) {
  79             throw new Exception(message + " - " +
  80                     "Ouch, that's bad. " +
  81                     "This is a new kind of unexpected exception " +
  82                     x, x);
  83         }
  84     }
  85 
  86     public static void main(String[] args) throws Exception {
  87         final LocalRMIServerSocketFactory f =
  88                 new LocalRMIServerSocketFactory();
  89         final ServerSocket s = f.createServerSocket(0);
  90         final int port = s.getLocalPort();
  91         Thread t = new Thread() {
  92 
  93             public void run() {
  94                 while (true) {
  95                     Exception error = Result.SUCCESS;
  96                     try {
  97                         System.err.println("Accepting: ");
  98                         final Socket ss = s.accept();
  99                         System.err.println(ss.getInetAddress() + " accepted");
 100                     } catch (Exception x) {
 101                         x.printStackTrace();
 102                         error = x;
 103                     } finally {
 104                         try {
 105                             // wait for the client to get the exception.
 106                             queue.put(error);
 107                         } catch (Exception x) {
 108                             // too bad!
 109                             System.err.println("Could't send result to client!");
 110                             x.printStackTrace();
 111                             return;
 112                         }
 113                     }
 114                 }
 115             }
 116         };
 117         t.setDaemon(true);
 118         t.start();
 119 
 120         System.err.println("new Socket((String)null, port)");
 121         final Socket s1 = new Socket((String) null, port);
 122         checkError("new Socket((String)null, port)");
 123         s1.close();
 124         System.err.println("new Socket((String)null, port): PASSED");
 125 
 126         System.err.println("new Socket(InetAddress.getByName(null), port)");
 127         final Socket s2 = new Socket(InetAddress.getByName(null), port);
 128         checkError("new Socket(InetAddress.getByName(null), port)");
 129         s2.close();
 130         System.err.println("new Socket(InetAddress.getByName(null), port): PASSED");
 131 
 132         System.err.println("new Socket(localhost, port)");
 133         final Socket s3 = new Socket("localhost", port);
 134         checkError("new Socket(localhost, port)");
 135         s3.close();
 136         System.err.println("new Socket(localhost, port): PASSED");
 137 
 138         System.err.println("new Socket(127.0.0.1, port)");
 139         final Socket s4 = new Socket("127.0.0.1", port);
 140         checkError("new Socket(127.0.0.1, port)");
 141         s4.close();
 142         System.err.println("new Socket(127.0.0.1, port): PASSED");
 143 
 144     }
 145 }