5266N/A /*
5266N/A * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
5266N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5266N/A *
5266N/A * This code is free software; you can redistribute it and/or modify it
5266N/A * under the terms of the GNU General Public License version 2 only, as
5266N/A * published by the Free Software Foundation.
5266N/A *
5266N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5266N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5266N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5266N/A * version 2 for more details (a copy is included in the LICENSE file that
5266N/A * accompanied this code).
5266N/A *
5266N/A * You should have received a copy of the GNU General Public License version
5266N/A * 2 along with this work; if not, write to the Free Software Foundation,
5266N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5266N/A *
5266N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5266N/A * or visit www.oracle.com if you need additional information or have any
5266N/A * questions.
5266N/A */
0N/A
5266N/A /*
5266N/A * @test
5266N/A * @bug 5083594
5266N/A * @summary Ensure that Naming.java correctly parses host names with '_' in
5266N/A * them.
5266N/A * @author Vinod Johnson
5266N/A *
5266N/A * @library ../testlibrary
5551N/A * @build TestLibrary UnderscoreHost_Stub
5266N/A * @run main/othervm UnderscoreHost
0N/A */
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.net.MalformedURLException;
0N/Aimport java.net.ServerSocket;
0N/Aimport java.net.Socket;
0N/Aimport java.rmi.Naming;
0N/Aimport java.rmi.Remote;
0N/Aimport java.rmi.RemoteException;
0N/Aimport java.rmi.registry.LocateRegistry;
0N/Aimport java.rmi.registry.Registry;
0N/Aimport java.rmi.server.RMISocketFactory;
0N/Aimport java.rmi.server.UnicastRemoteObject;
0N/A
0N/Apublic class UnderscoreHost extends UnicastRemoteObject implements Remote {
0N/A private static final String HOSTNAME = "foo_bar";
0N/A private static final String NAME = "name";
0N/A /*
0N/A * The socket factory captures the host name of the parsed URL, and
0N/A * then connects to the local host.
0N/A */
0N/A private static class HostVerifyingSocketFactory extends RMISocketFactory {
0N/A String host;
0N/A
0N/A public synchronized Socket createSocket(String host, int port)
0N/A throws IOException {
0N/A if (this.host == null) {
0N/A // Only set it the first time, subsequent DGC dirty calls
0N/A // will be local host
0N/A this.host = host;
0N/A }
0N/A return new Socket("localhost", port);
0N/A }
0N/A public ServerSocket createServerSocket(int port) throws IOException {
0N/A return new ServerSocket(port);
0N/A }
0N/A }
0N/A
0N/A public UnderscoreHost() throws RemoteException {};
0N/A
0N/A public static void main(String args[]) {
0N/A UnderscoreHost t = null;
0N/A try {
0N/A HostVerifyingSocketFactory hvf = new HostVerifyingSocketFactory();
0N/A RMISocketFactory.setSocketFactory(hvf);
5266N/A Registry r = TestLibrary.createRegistryOnUnusedPort();
5266N/A int port = TestLibrary.getRegistryPort(r);
0N/A t = new UnderscoreHost();
0N/A r.rebind(NAME, t);
0N/A Naming.lookup("rmi://" + HOSTNAME +
5266N/A ":" + port + "/" + NAME);
0N/A /*
0N/A * This test is coded to pass whether java.net.URI obeys
0N/A * RFC 2396 or RFC 3986 (see 5085902, 6394131, etc.).
0N/A *
0N/A * If java.net.URI obeys RFC 3986, so host names may
0N/A * contain underscores, then the Naming.lookup invocation
0N/A * should succeed-- but the host actually connected to
0N/A * must equal HOSTNAME.
0N/A */
0N/A if (!hvf.host.equals(HOSTNAME)) {
0N/A throw new RuntimeException(
0N/A "java.rmi.Naming Parsing error:" +
0N/A hvf.host + ":" + HOSTNAME);
0N/A }
0N/A } catch (MalformedURLException e) {
0N/A /*
0N/A * If java.net.URI obeys RFC 2396, so host names must not
0N/A * contain underscores, then the Naming.lookup invocation
0N/A * should throw MalformedURLException-- so this is OK.
0N/A */
0N/A } catch (IOException ioe) {
0N/A TestLibrary.bomb(ioe);
0N/A } catch (java.rmi.NotBoundException nbe) {
0N/A TestLibrary.bomb(nbe);
0N/A } finally {
0N/A TestLibrary.unexport(t);
0N/A }
0N/A
0N/A }
0N/A}