Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/jndi/ldap/LdapDnsProviderTest.java
38855 views
/*1* Copyright (c) 2018, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425import java.io.File;26import java.io.FileOutputStream;27import java.io.IOException;28import java.security.Permission;29import java.util.Hashtable;30import java.util.concurrent.Callable;31import java.util.concurrent.FutureTask;3233import javax.naming.Context;34import javax.naming.InitialContext;35import javax.naming.NamingException;36import javax.naming.directory.InitialDirContext;37import javax.naming.directory.SearchControls;3839/**40* @test41* @bug 816076842* @summary ctx provider tests for ldap43* @compile dnsprovider/TestDnsProvider.java44* @run main/othervm LdapDnsProviderTest45* @run main/othervm LdapDnsProviderTest nosm46* @run main/othervm LdapDnsProviderTest smnodns47* @run main/othervm LdapDnsProviderTest smdns48* @run main/othervm LdapDnsProviderTest nosmbaddns49*/5051class DNSSecurityManager extends SecurityManager {52535455/* run main/othervm LdapDnsProviderTest5657* run main/othervm LdapDnsProviderTest nosm58* run main/othervm LdapDnsProviderTest smnodns59* run main/othervm LdapDnsProviderTest smdns60* run main/othervm LdapDnsProviderTest nosmbaddns61*/6263private boolean dnsProvider = false;6465public void setAllowDnsProvider(boolean allow) {66dnsProvider = allow;67}6869@Override70public void checkPermission(Permission p) {71if (p.getName().equals("ldapDnsProvider") && !dnsProvider) {72throw new SecurityException(p.getName());73}74}75}7677class ProviderTest implements Callable<Boolean> {7879private final String url;80private final String expected;81private final Hashtable<String, String> env = new Hashtable<>(11);8283public ProviderTest(String url, String expected) {84this.url = url;85this.expected = expected;86env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");87}8889boolean shutItDown(InitialContext ctx) {90try {91if (ctx != null) ctx.close();92return true;93} catch (NamingException ex) {94return false;95}96}9798public Boolean call() {99boolean passed;100InitialContext ctx = null;101102if (url != null) {103env.put(Context.PROVIDER_URL, url);104}105106try {107ctx = new InitialDirContext(env);108SearchControls scl = new SearchControls();109scl.setSearchScope(SearchControls.SUBTREE_SCOPE);110((InitialDirContext)ctx).search(111"ou=People,o=Test", "(objectClass=*)", scl);112throw new RuntimeException("Search should not complete");113} catch (NamingException e) {114e.printStackTrace();115passed = e.toString().contains(expected);116} finally {117shutItDown(ctx);118}119return passed;120}121}122123public class LdapDnsProviderTest {124125private static final String TEST_CLASSES =126System.getProperty("test.classes", ".");127128public static void writeFile(String content, File dstFile)129throws IOException130{131try (FileOutputStream dst = new FileOutputStream(dstFile)) {132byte[] buf = content.getBytes();133dst.write(buf, 0, buf.length);134}135}136137public static void installServiceConfigurationFile(String content) {138String filename = "com.sun.jndi.ldap.spi.LdapDnsProvider";139140File dstDir = new File(TEST_CLASSES, "META-INF/services");141if (!dstDir.exists()) {142if (!dstDir.mkdirs()) {143throw new RuntimeException(144"could not create META-INF/services directory " + dstDir);145}146}147File dstFile = new File(dstDir, filename);148149try {150writeFile(content, dstFile);151} catch (IOException e) {152throw new RuntimeException("could not install " + dstFile, e);153}154}155156public static void main(String[] args) throws Exception {157if (args.length > 0 && args[0].equals("nosm")) {158// no security manager, serviceloader159installServiceConfigurationFile("dnsprovider.TestDnsProvider");160runTest("ldap:///dc=example,dc=com", "yupyupyup:389");161} else if (args.length > 0 && args[0].equals("smnodns")) {162// security manager & serviceloader163installServiceConfigurationFile("dnsprovider.TestDnsProvider");164// install security manager165System.setSecurityManager(new DNSSecurityManager());166runTest("ldap:///dc=example,dc=com", "ServiceConfigurationError");167} else if (args.length > 0 && args[0].equals("smdns")) {168// security manager & serviceloader169DNSSecurityManager sm = new DNSSecurityManager();170installServiceConfigurationFile("dnsprovider.TestDnsProvider");171// install security manager172System.setSecurityManager(sm);173sm.setAllowDnsProvider(true);174runTest("ldap:///dc=example,dc=com", "yupyupyup:389");175} else if (args.length > 0 && args[0].equals("nosmbaddns")) {176// no security manager, no serviceloader177// DefaultLdapDnsProvider178installServiceConfigurationFile("dnsprovider.MissingDnsProvider");179// no SecurityManager180runTest("ldap:///dc=example,dc=com", "not found");181} else {182// no security manager, no serviceloader183// DefaultLdapDnsProvider184System.err.println("TEST_CLASSES:");185System.err.println(TEST_CLASSES);186File f = new File(187TEST_CLASSES, "META-INF/services/com.sun.jndi.ldap.spi.LdapDnsProvider");188if (f.exists()) {189f.delete();190}191192// no SecurityManager193runTest("ldap:///dc=example,dc=com", "localhost:389");194runTest("ldap://localhost/dc=example,dc=com", "localhost:389");195runTest("ldap://localhost:111/dc=example,dc=com", "localhost:111");196runTest("ldaps://localhost:111/dc=example,dc=com", "localhost:111");197runTest("ldaps://localhost/dc=example,dc=com", "localhost:636");198runTest(null, "localhost:389");199runTest("", "ConfigurationException");200}201}202203private static void runTest(String url, String expected) {204FutureTask<Boolean> future =205new FutureTask<>(206new ProviderTest(url, expected));207new Thread(future).start();208209System.err.println("Testing: " + url + ", " + expected);210while (!future.isDone()) {211try {212if (!future.get()) {213System.err.println("Test failed");214throw new RuntimeException(215"Test failed, ProviderTest returned false");216}217} catch (Exception e) {218if (!e.toString().contains(expected)) {219System.err.println("Test failed");220throw new RuntimeException(221"Test failed, unexpected result");222}223}224}225System.err.println("Test passed");226}227228}229230231232