Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/Naming/UnderscoreHost.java
38813 views
/*1* Copyright (c) 2005, 2012, 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* @build TestLibrary UnderscoreHost_Stub32* @run main/othervm UnderscoreHost33*/3435import java.io.IOException;36import java.net.MalformedURLException;37import java.net.ServerSocket;38import java.net.Socket;39import java.rmi.Naming;40import java.rmi.Remote;41import java.rmi.RemoteException;42import java.rmi.registry.LocateRegistry;43import java.rmi.registry.Registry;44import java.rmi.server.RMISocketFactory;45import java.rmi.server.UnicastRemoteObject;4647public class UnderscoreHost extends UnicastRemoteObject implements Remote {48private static final String HOSTNAME = "foo_bar";49private static final String NAME = "name";50/*51* The socket factory captures the host name of the parsed URL, and52* then connects to the local host.53*/54private static class HostVerifyingSocketFactory extends RMISocketFactory {55String host;5657public synchronized Socket createSocket(String host, int port)58throws IOException {59if (this.host == null) {60// Only set it the first time, subsequent DGC dirty calls61// will be local host62this.host = host;63}64return new Socket("localhost", port);65}66public ServerSocket createServerSocket(int port) throws IOException {67return new ServerSocket(port);68}69}7071public UnderscoreHost() throws RemoteException {};7273public static void main(String args[]) {74UnderscoreHost t = null;75try {76HostVerifyingSocketFactory hvf = new HostVerifyingSocketFactory();77RMISocketFactory.setSocketFactory(hvf);78Registry r = TestLibrary.createRegistryOnUnusedPort();79int port = TestLibrary.getRegistryPort(r);80t = new UnderscoreHost();81r.rebind(NAME, t);82Naming.lookup("rmi://" + HOSTNAME +83":" + port + "/" + NAME);84/*85* This test is coded to pass whether java.net.URI obeys86* RFC 2396 or RFC 3986 (see 5085902, 6394131, etc.).87*88* If java.net.URI obeys RFC 3986, so host names may89* contain underscores, then the Naming.lookup invocation90* should succeed-- but the host actually connected to91* must equal HOSTNAME.92*/93if (!hvf.host.equals(HOSTNAME)) {94throw new RuntimeException(95"java.rmi.Naming Parsing error:" +96hvf.host + ":" + HOSTNAME);97}98} catch (MalformedURLException e) {99/*100* If java.net.URI obeys RFC 2396, so host names must not101* contain underscores, then the Naming.lookup invocation102* should throw MalformedURLException-- so this is OK.103*/104} catch (IOException ioe) {105TestLibrary.bomb(ioe);106} catch (java.rmi.NotBoundException nbe) {107TestLibrary.bomb(nbe);108} finally {109TestLibrary.unexport(t);110}111112}113}114115116