1 /* 2 * Copyright (c) 2008, 2014, 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 * @modules jdk.management.agent/sun.management.jmxremote 32 * @run compile -XDignore.symbol.file=true -g LocalRMIServerSocketFactoryTest.java 33 * @run main LocalRMIServerSocketFactoryTest 34 */ 35 36 import sun.management.jmxremote.LocalRMIServerSocketFactory; 37 import java.io.IOException; 38 import java.net.InetAddress; 39 import java.net.NetworkInterface; 40 import java.net.ServerSocket; 41 import java.net.Socket; 42 import java.util.concurrent.SynchronousQueue; 43 44 public class LocalRMIServerSocketFactoryTest { 45 46 private static final SynchronousQueue<Exception> queue = 47 new SynchronousQueue<Exception>(); 48 private static volatile boolean isRunning = true; 49 50 static final class Result extends Exception { 51 52 private Result() { 53 super("SUCCESS: No exception was thrown"); 54 } 55 static final Result SUCCESS = new Result(); 56 } 57 58 private static void checkError(String message) throws Exception { 59 60 // Wait for the server to set the error field. 61 final Exception x = queue.take(); 62 63 if (x == Result.SUCCESS) { 64 return; 65 } 66 67 68 // case of 6674166: this is very unlikely to happen, even if 69 // both 6674166 and 6774170 aren't fixed. If it happens 70 // however, it might indicate that neither defects are fixed. 71 72 if (x instanceof NullPointerException) { 73 throw new Exception(message + " - " + 74 "Congratulations! it seems you have triggered 6674166. " + 75 "Neither 6674166 nor 6774170 seem to be fixed: " + x, x); 76 } else if (x instanceof IOException) { 77 throw new Exception(message + " - " + 78 "Unexpected IOException. Maybe you triggered 6674166? " + 79 x, x); 80 } else if (x != null) { 81 throw new Exception(message + " - " + 82 "Ouch, that's bad. " + 83 "This is a new kind of unexpected exception " + 84 x, x); 85 } 86 } 87 88 public static void main(String[] args) throws Exception { 89 final LocalRMIServerSocketFactory f = 90 new LocalRMIServerSocketFactory(); 91 final ServerSocket s = f.createServerSocket(0); 92 final int port = s.getLocalPort(); 93 Thread t = new Thread() { 94 95 public void run() { 96 while (isRunning) { 97 Exception error = Result.SUCCESS; 98 try { 99 System.err.println("Accepting: "); 100 final Socket ss = s.accept(); 101 System.err.println(ss.getInetAddress() + " accepted"); 102 } catch (Exception x) { 103 if (isRunning) { 104 x.printStackTrace(); 105 } 106 error = x; 107 } finally { 108 try { 109 if (isRunning) { 110 // wait for the client to get the exception. 111 queue.put(error); 112 } 113 } catch (Exception x) { 114 // too bad! 115 System.err.println("Could't send result to client!"); 116 x.printStackTrace(); 117 return; 118 } 119 } 120 } 121 } 122 }; 123 124 try { 125 t.start(); 126 127 System.err.println("new Socket((String)null, port)"); 128 final Socket s1 = new Socket((String) null, port); 129 checkError("new Socket((String)null, port)"); 130 s1.close(); 131 System.err.println("new Socket((String)null, port): PASSED"); 132 133 System.err.println("new Socket(InetAddress.getByName(null), port)"); 134 final Socket s2 = new Socket(InetAddress.getByName(null), port); 135 checkError("new Socket(InetAddress.getByName(null), port)"); 136 s2.close(); 137 System.err.println("new Socket(InetAddress.getByName(null), port): PASSED"); 138 139 System.err.println("new Socket(localhost, port)"); 140 final Socket s3 = new Socket("localhost", port); 141 checkError("new Socket(localhost, port)"); 142 s3.close(); 143 System.err.println("new Socket(localhost, port): PASSED"); 144 145 System.err.println("new Socket(127.0.0.1, port)"); 146 final Socket s4 = new Socket("127.0.0.1", port); 147 checkError("new Socket(127.0.0.1, port)"); 148 s4.close(); 149 System.err.println("new Socket(127.0.0.1, port): PASSED"); 150 } 151 finally { 152 isRunning = false; 153 s.close(); 154 t.join(); 155 } 156 } 157 }