jdk/test/java/rmi/Naming/UnderscoreHost.java
author juh
Tue, 25 Jun 2013 14:41:46 -0700
changeset 18552 005e115dc6ee
parent 14778 5947768d173d
child 30820 0d4717a011d3
permissions -rw-r--r--
8017326: Cleanup of the javadoc <code> tag in java.security.spec Summary: Convert javadoc <code> and <tt> tags to {@code ...} Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13256
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
     1
 /*
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
     2
  * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
     3
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
     4
  *
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
     5
  * This code is free software; you can redistribute it and/or modify it
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
     6
  * under the terms of the GNU General Public License version 2 only, as
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
     7
  * published by the Free Software Foundation.
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
     8
  *
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
     9
  * This code is distributed in the hope that it will be useful, but WITHOUT
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    10
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    11
  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    12
  * version 2 for more details (a copy is included in the LICENSE file that
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    13
  * accompanied this code).
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    14
  *
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    15
  * You should have received a copy of the GNU General Public License version
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    16
  * 2 along with this work; if not, write to the Free Software Foundation,
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    17
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    18
  *
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    19
  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    20
  * or visit www.oracle.com if you need additional information or have any
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    21
  * questions.
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    22
  */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
13256
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    24
 /*
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    25
  * @test
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    26
  * @bug 5083594
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    27
  * @summary Ensure that Naming.java correctly parses host names with '_' in
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    28
  * them.
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    29
  * @author Vinod Johnson
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    30
  *
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    31
  * @library ../testlibrary
14778
5947768d173d 8004748: clean up @build tags in RMI tests
smarks
parents: 13256
diff changeset
    32
  * @build TestLibrary UnderscoreHost_Stub
13256
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    33
  * @run main/othervm UnderscoreHost
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.net.ServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.Socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.rmi.Naming;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.rmi.Remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.rmi.RemoteException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.rmi.registry.LocateRegistry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.rmi.registry.Registry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.rmi.server.RMISocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.rmi.server.UnicastRemoteObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class UnderscoreHost extends UnicastRemoteObject implements Remote {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final String HOSTNAME = "foo_bar";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final String NAME = "name";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * The socket factory captures the host name of the parsed URL, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * then connects to the local host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static class HostVerifyingSocketFactory extends RMISocketFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        String host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        public synchronized Socket createSocket(String host, int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            throws IOException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            if (this.host == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                // Only set it the first time, subsequent DGC dirty calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                // will be local host
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                this.host = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            return new Socket("localhost", port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        public ServerSocket createServerSocket(int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            return new ServerSocket(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public UnderscoreHost() throws RemoteException {};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public static void main(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        UnderscoreHost t = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            HostVerifyingSocketFactory hvf = new HostVerifyingSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            RMISocketFactory.setSocketFactory(hvf);
13256
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    79
            Registry r = TestLibrary.createRegistryOnUnusedPort();
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    80
            int port = TestLibrary.getRegistryPort(r);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            t = new UnderscoreHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            r.rebind(NAME, t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            Naming.lookup("rmi://" + HOSTNAME +
13256
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    84
                          ":" + port + "/" + NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
             * This test is coded to pass whether java.net.URI obeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
             * RFC 2396 or RFC 3986 (see 5085902, 6394131, etc.).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
             * If java.net.URI obeys RFC 3986, so host names may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
             * contain underscores, then the Naming.lookup invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
             * should succeed-- but the host actually connected to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
             * must equal HOSTNAME.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            if (!hvf.host.equals(HOSTNAME)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    "java.rmi.Naming Parsing error:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    hvf.host + ":" + HOSTNAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
             * If java.net.URI obeys RFC 2396, so host names must not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
             * contain underscores, then the Naming.lookup invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
             * should throw MalformedURLException-- so this is OK.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            TestLibrary.bomb(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        } catch (java.rmi.NotBoundException nbe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            TestLibrary.bomb(nbe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            TestLibrary.unexport(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
}