/* * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/** * @test * @bug 8160768 * @key randomness intermittent * @summary ctx provider tests for ldap. * Two test cases need to establish connection to the * unreachable port on localhost. Each tries 5 connection * attempts with a random port expecting for connection to fail. * In rare cases it could establish connections due to services * running on these ports, therefore it can fail intermittently. * @modules java.naming/com.sun.jndi.ldap java.base/sun.net * @library /test/lib * @build jdk.test.lib.RandomFactory * @compile dnsprovider/TestDnsProvider.java * @run main/othervm LdapDnsProviderTest * @run main/othervm LdapDnsProviderTest nosm * @run main/othervm -Djava.security.manager=allow LdapDnsProviderTest smnodns * @run main/othervm -Djava.security.manager=allow LdapDnsProviderTest smdns * @run main/othervm LdapDnsProviderTest nosmbaddns
*/
class DNSSecurityManager extends SecurityManager {
if (url != null) {
env.put(Context.PROVIDER_URL, url);
}
// Set JNDI LDAP connect timeout property. It helps to prevent // initial bind operation from blocking in case of a local process // listening on the port specified in the URL. With the property set, // the bind operation will fail with timeout exception, and then it // could be retried with another port number.
env.put("com.sun.jndi.ldap.connect.timeout", "1000");
try {
ctx = new InitialDirContext(env);
SearchControls scl = new SearchControls();
scl.setSearchScope(SearchControls.SUBTREE_SCOPE);
((InitialDirContext)ctx).search( "ou=People,o=Test", "(objectClass=*)", scl); thrownew RuntimeException("Search should not complete");
} catch (NamingException e) {
passed = e.toString().contains(expected);
System.err.println((passed ? "Expected" : "Unexpected") + " NamingException observed: " + e.toString()); // Print stack trace only for unexpected exceptions if (!passed) {
e.printStackTrace();
}
} finally {
shutItDown(ctx);
} return passed;
}
}
// Pseudorandom number generator privatestaticfinal Random RND = RandomFactory.getRandom(); // Port numbers already seen to be generated by pseudorandom generator privatestaticfinal Set<Integer> SEEN_PORTS = new HashSet<>();
// Get random, previously unseen port number from [1111, PortConfig.getUpper()) range privatestaticint generateUnseenPort() { int port; do {
port = 1111 + RND.nextInt(PortConfig.getUpper() - 1111); // Seen ports will never contain more than maxAttempts*2 ports
} while (SEEN_PORTS.contains(port));
SEEN_PORTS.add(port); return port;
}
// Run test with ldap connection to localhost and random port. The test is expected to fail // with CommunicationException that is caused by connection refuse exception. // But in case if there is a service running on the same port the connection // will be established and then closed or timed-out. Both cases will generate exception // messages which differ from the expected one. // For such cases the test will be repeated with another random port. That will be done // maxAttempts times. If the expected exception won't be observed - test will be treated // as failed. privatestaticvoid runLocalHostTestWithRandomPort(String scheme, String path, int maxAttempts) { for (int attempt = 0; attempt <= maxAttempts; attempt++) { boolean attemptSuccessful = true; int port = generateUnseenPort();
// Construct URL for the current attempt
String url = scheme + "://localhost" + ":" + port + path;
// Construct text expected to be present in Exception message
String expected = "localhost:" + port;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.