Path: blob/master/test/jdk/java/rmi/Naming/UnderscoreHost.java
41149 views
/*1* Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 508359426* @summary Ensure that Naming.java correctly parses host names with '_' in27* them.28* @author Vinod Johnson29*30* @library ../testlibrary31* @modules java.rmi/sun.rmi.registry32* java.rmi/sun.rmi.server33* java.rmi/sun.rmi.transport34* java.rmi/sun.rmi.transport.tcp35* @build TestLibrary UnderscoreHost_Stub36* @run main/othervm UnderscoreHost37*/3839import java.io.IOException;40import java.net.MalformedURLException;41import java.net.ServerSocket;42import java.net.Socket;43import java.rmi.Naming;44import java.rmi.Remote;45import java.rmi.RemoteException;46import java.rmi.registry.LocateRegistry;47import java.rmi.registry.Registry;48import java.rmi.server.RMISocketFactory;49import java.rmi.server.UnicastRemoteObject;5051public class UnderscoreHost extends UnicastRemoteObject implements Remote {52private static final String HOSTNAME = "foo_bar";53private static final String NAME = "name";54/*55* The socket factory captures the host name of the parsed URL, and56* then connects to the local host.57*/58private static class HostVerifyingSocketFactory extends RMISocketFactory {59String host;6061public synchronized Socket createSocket(String host, int port)62throws IOException {63if (this.host == null) {64// Only set it the first time, subsequent DGC dirty calls65// will be local host66this.host = host;67}68return new Socket("localhost", port);69}70public ServerSocket createServerSocket(int port) throws IOException {71return new ServerSocket(port);72}73}7475public UnderscoreHost() throws RemoteException {};7677public static void main(String args[]) {78UnderscoreHost t = null;79try {80HostVerifyingSocketFactory hvf = new HostVerifyingSocketFactory();81RMISocketFactory.setSocketFactory(hvf);82Registry r = TestLibrary.createRegistryOnEphemeralPort();83int port = TestLibrary.getRegistryPort(r);84t = new UnderscoreHost();85r.rebind(NAME, t);86Naming.lookup("rmi://" + HOSTNAME +87":" + port + "/" + NAME);88/*89* This test is coded to pass whether java.net.URI obeys90* RFC 2396 or RFC 3986 (see 5085902, 6394131, etc.).91*92* If java.net.URI obeys RFC 3986, so host names may93* contain underscores, then the Naming.lookup invocation94* should succeed-- but the host actually connected to95* must equal HOSTNAME.96*/97if (!hvf.host.equals(HOSTNAME)) {98throw new RuntimeException(99"java.rmi.Naming Parsing error:" +100hvf.host + ":" + HOSTNAME);101}102} catch (MalformedURLException e) {103/*104* If java.net.URI obeys RFC 2396, so host names must not105* contain underscores, then the Naming.lookup invocation106* should throw MalformedURLException-- so this is OK.107*/108} catch (IOException ioe) {109TestLibrary.bomb(ioe);110} catch (java.rmi.NotBoundException nbe) {111TestLibrary.bomb(nbe);112} finally {113TestLibrary.unexport(t);114}115116}117}118119120