1 /* 2 * Copyright (c) 2005, 2012, 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 26 * @bug 5083594 27 * @summary Ensure that Naming.java correctly parses host names with '_' in 28 * them. 29 * @author Vinod Johnson 30 * 31 * @library ../testlibrary 32 * @modules java.rmi/sun.rmi.registry 33 * java.rmi/sun.rmi.server 34 * java.rmi/sun.rmi.transport 35 * java.rmi/sun.rmi.transport.tcp 36 * @build TestLibrary UnderscoreHost_Stub 37 * @run main/othervm UnderscoreHost 38 */ 39 40 import java.io.IOException; 41 import java.net.MalformedURLException; 42 import java.net.ServerSocket; 43 import java.net.Socket; 44 import java.rmi.Naming; 45 import java.rmi.Remote; 46 import java.rmi.RemoteException; 47 import java.rmi.registry.LocateRegistry; 48 import java.rmi.registry.Registry; 49 import java.rmi.server.RMISocketFactory; 50 import java.rmi.server.UnicastRemoteObject; 51 52 public class UnderscoreHost extends UnicastRemoteObject implements Remote { 53 private static final String HOSTNAME = "foo_bar"; 54 private static final String NAME = "name"; 55 /* 56 * The socket factory captures the host name of the parsed URL, and 57 * then connects to the local host. 58 */ 59 private static class HostVerifyingSocketFactory extends RMISocketFactory { 60 String host; 61 62 public synchronized Socket createSocket(String host, int port) 63 throws IOException { 64 if (this.host == null) { 65 // Only set it the first time, subsequent DGC dirty calls 66 // will be local host 67 this.host = host; 68 } 69 return new Socket("localhost", port); 70 } 71 public ServerSocket createServerSocket(int port) throws IOException { 72 return new ServerSocket(port); 73 } 74 } 75 76 public UnderscoreHost() throws RemoteException {}; 77 78 public static void main(String args[]) { 79 UnderscoreHost t = null; 80 try { 81 HostVerifyingSocketFactory hvf = new HostVerifyingSocketFactory(); 82 RMISocketFactory.setSocketFactory(hvf); 83 Registry r = TestLibrary.createRegistryOnUnusedPort(); 84 int port = TestLibrary.getRegistryPort(r); 85 t = new UnderscoreHost(); 86 r.rebind(NAME, t); 87 Naming.lookup("rmi://" + HOSTNAME + 88 ":" + port + "/" + NAME); 89 /* 90 * This test is coded to pass whether java.net.URI obeys 91 * RFC 2396 or RFC 3986 (see 5085902, 6394131, etc.). 92 * 93 * If java.net.URI obeys RFC 3986, so host names may 94 * contain underscores, then the Naming.lookup invocation 95 * should succeed-- but the host actually connected to 96 * must equal HOSTNAME. 97 */ 98 if (!hvf.host.equals(HOSTNAME)) { 99 throw new RuntimeException( 100 "java.rmi.Naming Parsing error:" + 101 hvf.host + ":" + HOSTNAME); 102 } 103 } catch (MalformedURLException e) { 104 /* 105 * If java.net.URI obeys RFC 2396, so host names must not 106 * contain underscores, then the Naming.lookup invocation 107 * should throw MalformedURLException-- so this is OK. 108 */ 109 } catch (IOException ioe) { 110 TestLibrary.bomb(ioe); 111 } catch (java.rmi.NotBoundException nbe) { 112 TestLibrary.bomb(nbe); 113 } finally { 114 TestLibrary.unexport(t); 115 } 116 117 } 118 }