Path: blob/master/test/jdk/com/sun/jndi/ldap/LdapDnsProviderTest.java
41153 views
/*1* Copyright (c) 2018, 2020, 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.HashSet;30import java.util.Hashtable;31import java.util.Random;32import java.util.Set;33import java.util.concurrent.Callable;34import java.util.concurrent.ExecutionException;35import java.util.concurrent.FutureTask;3637import javax.naming.Context;38import javax.naming.InitialContext;39import javax.naming.NamingException;40import javax.naming.directory.InitialDirContext;41import javax.naming.directory.SearchControls;4243import sun.net.PortConfig;4445import jdk.test.lib.RandomFactory;4647/**48* @test49* @bug 816076850* @key randomness intermittent51* @summary ctx provider tests for ldap.52* Two test cases need to establish connection to the53* unreachable port on localhost. Each tries 5 connection54* attempts with a random port expecting for connection to fail.55* In rare cases it could establish connections due to services56* running on these ports, therefore it can fail intermittently.57* @modules java.naming/com.sun.jndi.ldap java.base/sun.net58* @library /test/lib59* @build jdk.test.lib.RandomFactory60* @compile dnsprovider/TestDnsProvider.java61* @run main/othervm LdapDnsProviderTest62* @run main/othervm LdapDnsProviderTest nosm63* @run main/othervm -Djava.security.manager=allow LdapDnsProviderTest smnodns64* @run main/othervm -Djava.security.manager=allow LdapDnsProviderTest smdns65* @run main/othervm LdapDnsProviderTest nosmbaddns66*/6768class DNSSecurityManager extends SecurityManager {6970private boolean dnsProvider = false;7172public void setAllowDnsProvider(boolean allow) {73dnsProvider = allow;74}7576@Override77public void checkPermission(Permission p) {78if (p.getName().equals("ldapDnsProvider") && !dnsProvider) {79throw new SecurityException(p.getName());80}81}82}8384class ProviderTest implements Callable<Boolean> {8586private final String url;87private final String expected;88private final Hashtable<String, String> env = new Hashtable<>(11);8990public ProviderTest(String url, String expected) {91this.url = url;92this.expected = expected;93env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");94}9596boolean shutItDown(InitialContext ctx) {97try {98if (ctx != null) ctx.close();99return true;100} catch (NamingException ex) {101return false;102}103}104105public Boolean call() {106boolean passed;107InitialContext ctx = null;108109if (url != null) {110env.put(Context.PROVIDER_URL, url);111}112113// Set JNDI LDAP connect timeout property. It helps to prevent114// initial bind operation from blocking in case of a local process115// listening on the port specified in the URL. With the property set,116// the bind operation will fail with timeout exception, and then it117// could be retried with another port number.118env.put("com.sun.jndi.ldap.connect.timeout", "1000");119120try {121ctx = new InitialDirContext(env);122SearchControls scl = new SearchControls();123scl.setSearchScope(SearchControls.SUBTREE_SCOPE);124((InitialDirContext)ctx).search(125"ou=People,o=Test", "(objectClass=*)", scl);126throw new RuntimeException("Search should not complete");127} catch (NamingException e) {128passed = e.toString().contains(expected);129System.err.println((passed ? "Expected" : "Unexpected") +130" NamingException observed: " + e.toString());131// Print stack trace only for unexpected exceptions132if (!passed) {133e.printStackTrace();134}135} finally {136shutItDown(ctx);137}138return passed;139}140}141142public class LdapDnsProviderTest {143144private static final String TEST_CLASSES =145System.getProperty("test.classes", ".");146147public static void writeFile(String content, File dstFile)148throws IOException149{150try (FileOutputStream dst = new FileOutputStream(dstFile)) {151byte[] buf = content.getBytes();152dst.write(buf, 0, buf.length);153}154}155156public static void installServiceConfigurationFile(String content) {157String filename = "javax.naming.ldap.spi.LdapDnsProvider";158159File dstDir = new File(TEST_CLASSES, "META-INF/services");160if (!dstDir.exists()) {161if (!dstDir.mkdirs()) {162throw new RuntimeException(163"could not create META-INF/services directory " + dstDir);164}165}166File dstFile = new File(dstDir, filename);167168try {169writeFile(content, dstFile);170} catch (IOException e) {171throw new RuntimeException("could not install " + dstFile, e);172}173}174175public static void main(String[] args) throws Exception {176if (args.length > 0 && args[0].equals("nosm")) {177// no security manager, serviceloader178installServiceConfigurationFile("dnsprovider.TestDnsProvider");179runTest("ldap:///dc=example,dc=com", "yupyupyup:389");180} else if (args.length > 0 && args[0].equals("smnodns")) {181// security manager & serviceloader182installServiceConfigurationFile("dnsprovider.TestDnsProvider");183// install security manager184System.setSecurityManager(new DNSSecurityManager());185runTest("ldap:///dc=example,dc=com", "ServiceConfigurationError");186} else if (args.length > 0 && args[0].equals("smdns")) {187// security manager & serviceloader188DNSSecurityManager sm = new DNSSecurityManager();189installServiceConfigurationFile("dnsprovider.TestDnsProvider");190// install security manager191System.setSecurityManager(sm);192sm.setAllowDnsProvider(true);193runTest("ldap:///dc=example,dc=com", "yupyupyup:389");194} else if (args.length > 0 && args[0].equals("nosmbaddns")) {195// no security manager, no serviceloader196// DefaultLdapDnsProvider197installServiceConfigurationFile("dnsprovider.MissingDnsProvider");198// no SecurityManager199runTest("ldap:///dc=example,dc=com", "not found");200} else {201// no security manager, no serviceloader202// DefaultLdapDnsProvider203System.err.println("TEST_CLASSES:");204System.err.println(TEST_CLASSES);205File f = new File(206TEST_CLASSES, "META-INF/services/javax.naming.ldap.spi.LdapDnsProvider");207if (f.exists()) {208f.delete();209}210211// no SecurityManager212runTest("ldap:///dc=example,dc=com", "localhost:389");213runTest("ldap://localhost/dc=example,dc=com", "localhost:389");214runLocalHostTestWithRandomPort("ldap", "/dc=example,dc=com", 5);215runLocalHostTestWithRandomPort("ldaps", "/dc=example,dc=com", 5);216runTest("ldaps://localhost/dc=example,dc=com", "localhost:636");217runTest(null, "localhost:389");218runTest("", "ConfigurationException");219}220}221222// Pseudorandom number generator223private static final Random RND = RandomFactory.getRandom();224// Port numbers already seen to be generated by pseudorandom generator225private static final Set<Integer> SEEN_PORTS = new HashSet<>();226227// Get random, previously unseen port number from [1111, PortConfig.getUpper()) range228private static int generateUnseenPort() {229int port;230do {231port = 1111 + RND.nextInt(PortConfig.getUpper() - 1111);232// Seen ports will never contain more than maxAttempts*2 ports233} while (SEEN_PORTS.contains(port));234SEEN_PORTS.add(port);235return port;236}237238// Run test with ldap connection to localhost and random port. The test is expected to fail239// with CommunicationException that is caused by connection refuse exception.240// But in case if there is a service running on the same port the connection241// will be established and then closed or timed-out. Both cases will generate exception242// messages which differ from the expected one.243// For such cases the test will be repeated with another random port. That will be done244// maxAttempts times. If the expected exception won't be observed - test will be treated245// as failed.246private static void runLocalHostTestWithRandomPort(String scheme, String path, int maxAttempts) {247for (int attempt = 0; attempt <= maxAttempts; attempt++) {248boolean attemptSuccessful = true;249int port = generateUnseenPort();250251// Construct URL for the current attempt252String url = scheme + "://localhost" + ":" + port + path;253254// Construct text expected to be present in Exception message255String expected = "localhost:" + port;256257System.err.printf("Iteration %d: Testing: url='%s', expected content='%s'%n",258attempt, url, expected);259260FutureTask<Boolean> future = new FutureTask<>(261new ProviderTest(url, expected));262new Thread(future).start();263while (!future.isDone()) {264try {265if (!future.get()) {266if (attempt == maxAttempts) {267throw new RuntimeException("Test failed, ProviderTest" +268" returned false " + maxAttempts + " times");269} else {270System.err.printf("Iteration %d failed:" +271" ProviderTest returned false%n", attempt);272attemptSuccessful = false;273}274}275} catch (InterruptedException | ExecutionException e) {276System.err.println("Iteration %d failed to execute provider test: " + e.getMessage());277attemptSuccessful = false;278}279}280if (attemptSuccessful) {281System.err.println("Test passed. It took " + (attempt + 1) + " iterations to complete");282break;283}284}285}286287private static void runTest(String url, String expected) {288FutureTask<Boolean> future =289new FutureTask<>(290new ProviderTest(url, expected));291new Thread(future).start();292293System.err.printf("Testing: url='%s', expected content='%s'%n", url, expected);294while (!future.isDone()) {295try {296if (!future.get()) {297System.err.println("Test failed");298throw new RuntimeException(299"Test failed, ProviderTest returned false");300}301} catch (Exception e) {302if (!e.toString().contains(expected)) {303System.err.println("Test failed");304throw new RuntimeException(305"Test failed, unexpected result");306}307}308}309System.err.println("Test passed");310}311312}313314315316