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