|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 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 | +import java.net.InetAddress; |
| 25 | +import java.net.InetSocketAddress; |
| 26 | +import java.net.ServerSocket; |
| 27 | +import java.net.SocketAddress; |
| 28 | +import java.util.Hashtable; |
| 29 | +import javax.naming.CommunicationException; |
| 30 | +import javax.naming.NamingException; |
| 31 | +import javax.naming.ServiceUnavailableException; |
| 32 | +import javax.naming.directory.DirContext; |
| 33 | +import javax.naming.directory.InitialDirContext; |
| 34 | + |
| 35 | +import jdk.testlibrary.net.URIBuilder; |
| 36 | + |
| 37 | +/** |
| 38 | + * @test |
| 39 | + * @bug 8290367 |
| 40 | + * @summary Check if com.sun.jndi.ldap.object.trustSerialData covers the creation |
| 41 | + * of RMI remote objects from the 'javaRemoteLocation' LDAP attribute. |
| 42 | + * @modules java.naming/com.sun.jndi.ldap |
| 43 | + * @library /lib/testlibrary ../lib |
| 44 | + * @build LDAPServer LDAPTestUtils |
| 45 | + * |
| 46 | + * @run main/othervm RemoteLocationAttributeTest |
| 47 | + * @run main/othervm -Dcom.sun.jndi.ldap.object.trustSerialData |
| 48 | + * RemoteLocationAttributeTest |
| 49 | + * @run main/othervm -Dcom.sun.jndi.ldap.object.trustSerialData=false |
| 50 | + * RemoteLocationAttributeTest |
| 51 | + * @run main/othervm -Dcom.sun.jndi.ldap.object.trustSerialData=true |
| 52 | + * RemoteLocationAttributeTest |
| 53 | + * @run main/othervm -Dcom.sun.jndi.ldap.object.trustSerialData=TrUe |
| 54 | + * RemoteLocationAttributeTest |
| 55 | + */ |
| 56 | + |
| 57 | +public class RemoteLocationAttributeTest { |
| 58 | + |
| 59 | + public static void main(String[] args) throws Exception { |
| 60 | + // Create unbound server socket |
| 61 | + ServerSocket serverSocket = new ServerSocket(); |
| 62 | + |
| 63 | + // Bind it to the loopback address |
| 64 | + SocketAddress sockAddr = new InetSocketAddress( |
| 65 | + InetAddress.getLoopbackAddress(), 0); |
| 66 | + serverSocket.bind(sockAddr); |
| 67 | + |
| 68 | + // Construct the provider URL for LDAPTestUtils |
| 69 | + String providerURL = URIBuilder.newBuilder() |
| 70 | + .scheme("ldap") |
| 71 | + .loopback() |
| 72 | + .port(serverSocket.getLocalPort()) |
| 73 | + .buildUnchecked().toString(); |
| 74 | + |
| 75 | + Hashtable<Object, Object> env; |
| 76 | + |
| 77 | + // Initialize test environment variables |
| 78 | + env = LDAPTestUtils.initEnv(serverSocket, providerURL, |
| 79 | + RemoteLocationAttributeTest.class.getName(), args, false); |
| 80 | + |
| 81 | + DirContext ctx = null; |
| 82 | + try { |
| 83 | + try { |
| 84 | + System.err.println(env); |
| 85 | + // connect to server |
| 86 | + ctx = new InitialDirContext(env); |
| 87 | + Object lookupResult = ctx.lookup("Test"); |
| 88 | + System.err.println("Lookup result:" + lookupResult); |
| 89 | + // Test doesn't provide RMI registry running at 127.0.0.1:1097, but if |
| 90 | + // there is one running on test host successful result is valid for |
| 91 | + // cases when reconstruction allowed. |
| 92 | + if (!RECONSTRUCTION_ALLOWED) { |
| 93 | + throw new AssertionError("Unexpected successful lookup"); |
| 94 | + } |
| 95 | + } finally { |
| 96 | + serverSocket.close(); |
| 97 | + } |
| 98 | + } catch (ServiceUnavailableException | CommunicationException connectionException) { |
| 99 | + // The remote location was properly reconstructed but connection to |
| 100 | + // RMI endpoint failed: |
| 101 | + // ServiceUnavailableException - no open socket on 127.0.0.1:1097 |
| 102 | + // CommunicationException - 127.0.0.1:1097 is open, but it is not RMI registry |
| 103 | + System.err.println("Got one of connection exceptions:" + connectionException); |
| 104 | + if (!RECONSTRUCTION_ALLOWED) { |
| 105 | + throw new AssertionError("Reconstruction not blocked, as expected"); |
| 106 | + } |
| 107 | + } catch (NamingException ne) { |
| 108 | + String message = ne.getMessage(); |
| 109 | + System.err.printf("Got NamingException with message: '%s'%n", message); |
| 110 | + if (RECONSTRUCTION_ALLOWED && EXPECTED_NAMING_EXCEPTION_MESSAGE.equals(message)) { |
| 111 | + throw new AssertionError("Reconstruction unexpectedly blocked"); |
| 112 | + } |
| 113 | + if (!RECONSTRUCTION_ALLOWED && !EXPECTED_NAMING_EXCEPTION_MESSAGE.equals(message)) { |
| 114 | + throw new AssertionError("Reconstruction not blocked"); |
| 115 | + } |
| 116 | + } finally { |
| 117 | + LDAPTestUtils.cleanup(ctx); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // Reconstruction of RMI remote objects is allowed if 'com.sun.jndi.ldap.object.trustSerialData' |
| 122 | + // is set to "true". If the system property is not specified it implies default "false" value |
| 123 | + private static final boolean RECONSTRUCTION_ALLOWED = |
| 124 | + Boolean.getBoolean("com.sun.jndi.ldap.object.trustSerialData"); |
| 125 | + |
| 126 | + // NamingException message when reconstruction is not allowed |
| 127 | + private static final String EXPECTED_NAMING_EXCEPTION_MESSAGE = "Object deserialization is not allowed"; |
| 128 | +} |
0 commit comments