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   * @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 = TestLibrary.createRegistryOnUnusedPort();
  81             int port = TestLibrary.getRegistryPort(r);
  82             t = new UnderscoreHost();
  83             r.rebind(NAME, t);
  84             Naming.lookup("rmi://" + HOSTNAME +
  85                           ":" + port + "/" + NAME);
  86             /*
  87              * This test is coded to pass whether java.net.URI obeys
  88              * RFC 2396 or RFC 3986 (see 5085902, 6394131, etc.).
  89              *
  90              * If java.net.URI obeys RFC 3986, so host names may
  91              * contain underscores, then the Naming.lookup invocation
  92              * should succeed-- but the host actually connected to
  93              * must equal HOSTNAME.
  94              */
  95             if (!hvf.host.equals(HOSTNAME)) {
  96                 throw new RuntimeException(
  97                     "java.rmi.Naming Parsing error:" +
  98                     hvf.host + ":" + HOSTNAME);
  99             }
 100         } catch (MalformedURLException e) {
 101             /*
 102              * If java.net.URI obeys RFC 2396, so host names must not
 103              * contain underscores, then the Naming.lookup invocation
 104              * should throw MalformedURLException-- so this is OK.
 105              */
 106         } catch (IOException ioe) {
 107             TestLibrary.bomb(ioe);
 108         } catch (java.rmi.NotBoundException nbe) {
 109             TestLibrary.bomb(nbe);
 110         } finally {
 111             TestLibrary.unexport(t);
 112         }
 113 
 114     }
 115 }