import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.security.Permission; import java.util.Hashtable; import java.util.concurrent.Callable; import java.util.concurrent.FutureTask; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.naming.directory.InitialDirContext; import javax.naming.directory.SearchControls; /** * @test * @compile dnsprovider/TestDnsProvider.java * @run main/othervm LdapDnsProviderTest * @run main/othervm LdapDnsProviderTest nosm * @run main/othervm LdapDnsProviderTest smnodns * @run main/othervm LdapDnsProviderTest smdns * @modules java.naming/com.sun.jndi.ldap * @bug 8160768 * @summary ctx provider tests for ldap */ class DNSSecurityManager extends SecurityManager { private boolean dnsProvider = false; private String perm = javax.naming.ldap.LdapDnsProvider.DNSPROVIDER_PERMISSION; public void setAllowDnsProvider(boolean allow) { dnsProvider = allow; } @Override public void checkPermission(Permission p) { if (p.getName().equals(perm) && !dnsProvider) { throw new SecurityException(p.getName()); } } } class ProviderTest implements Callable { private final String expected; // private final LdapTestServer server; private final Hashtable env = new Hashtable(11); public ProviderTest(String expected) throws IOException { // this.server = new LdapTestServer(); this.expected = expected; env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); } boolean shutItDown(InitialContext ctx) { try { if (ctx != null) ctx.close(); return true; } catch (NamingException ex) { return false; } } public Boolean call() { boolean passed; InitialContext ctx = null; // String providerUrl = "ldap://localhost:" + server.getLocalPort(); String providerUrl = "ldap:///dc=example,dc=com"; try { env.put(Context.PROVIDER_URL, providerUrl); try { ctx = new InitialDirContext(env); SearchControls scl = new SearchControls(); scl.setSearchScope(SearchControls.SUBTREE_SCOPE); ((InitialDirContext)ctx).search( "ou=People,o=Test", "(objectClass=*)", scl); throw new RuntimeException("Search should not complete"); } catch (NamingException e) { System.out.println(e); e.printStackTrace(); passed = e.toString().indexOf(expected) > -1; } finally { shutItDown(ctx); } return passed; } catch (Exception e) { throw new RuntimeException(e); } } } public class LdapDnsProviderTest { private static final String testClasses = System.getProperty("test.classes", "."); private static final String testSrc = System.getProperty("test.src", "."); public static void copyFile(File srcFile, File dstFile) throws IOException { FileInputStream src = new FileInputStream(srcFile); FileOutputStream dst = new FileOutputStream(dstFile); byte[] buf = new byte[32768]; while (true) { int count = src.read(buf); if (count < 0) { break; } dst.write(buf, 0, count); } dst.close(); src.close(); } public static void installServiceConfigurationFile() { String filename = "javax.naming.ldap.LdapDnsProvider"; File dstDir = new File(testClasses, "META-INF/services"); if (!dstDir.exists()) { if (!dstDir.mkdirs()) { throw new RuntimeException( "could not create META-INF/services directory " + dstDir); } } File dstFile = new File(dstDir, filename); File srcDir = new File(testSrc); File srcFile = new File(srcDir, filename); try { copyFile(srcFile, dstFile); } catch (IOException e) { throw new RuntimeException("could not install " + dstFile, e); } } public static void main(String[] args) throws Exception { if (args.length > 0 && args[0].equals("nosm")) { // no security manager, serviceloader installServiceConfigurationFile(); runTest("yupyupyup:389"); } else if (args.length > 0 && args[0].equals("smnodns")) { // security manager & serviceloader installServiceConfigurationFile(); // install security manager System.setSecurityManager(new DNSSecurityManager()); runTest("ldapDnsProvider"); } else if (args.length > 0 && args[0].equals("smdns")) { // security manager & serviceloader DNSSecurityManager sm = new DNSSecurityManager(); installServiceConfigurationFile(); // install security manager System.setSecurityManager(sm); sm.setAllowDnsProvider(true); runTest("yupyupyup:389"); } else { // no security manager, no serviceloader // DefaultLdapDnsProvider File f = new File( testClasses, "META-INF/services/javax.naming.ldap.LdapDnsProvider"); if (f.exists()) { f.delete(); } // no SecurityManager runTest("localhost:389"); } } private static boolean runTest(String expected) throws Exception { FutureTask future = new FutureTask(new ProviderTest(expected)); new Thread(future).start(); while (!future.isDone()) { if ((Boolean) future.get()) { return true; } } throw new AssertionError("FAILED: " + expected); } }