--- old/test/jdk/javax/naming/module/src/test/test/ConnectWithAuthzId.java 2018-10-09 10:56:45.397456705 +0100 +++ new/test/jdk/javax/naming/module/src/test/test/ConnectWithAuthzId.java 2018-10-09 10:56:45.025465159 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2018, 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 @@ -29,6 +29,7 @@ package test; +import java.io.PrintStream; import java.net.*; import java.util.*; import javax.naming.*; @@ -40,12 +41,18 @@ public class ConnectWithAuthzId { + static { + final PrintStream out = new PrintStream(System.out, true); + final PrintStream err = new PrintStream(System.err, true); + + System.setOut(out); + System.setErr(err); + } + // LDAP capture file private static final String LDAP_CAPTURE_FILE = System.getProperty("test.src") + "/src/test/test/ConnectWithAuthzId.ldap"; - // LDAPServer socket - private static ServerSocket serverSocket; public static void main(String[] args) throws Exception { @@ -68,67 +75,69 @@ * Launch the LDAP server with the ConnectWithAuthzId.ldap capture file */ - serverSocket = new ServerSocket(0); - new Thread(new Runnable() { - @Override - public void run() { - try { - new LDAPServer(serverSocket, LDAP_CAPTURE_FILE); - } catch (Exception e) { - System.out.println("ERROR: unable to launch LDAP server"); - e.printStackTrace(); - } - } - }).start(); - - /* - * Connect to the LDAP directory - */ + try (ServerSocket serverSocket = new ServerSocket()) { + serverSocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); + new Thread(new Runnable() { + @Override + public void run() { + try { + new LDAPServer(serverSocket, LDAP_CAPTURE_FILE); + } catch (Exception e) { + System.out.println("ERROR: unable to launch LDAP server"); + e.printStackTrace(); + } + } + }).start(); - Hashtable env = new Hashtable<>(); - env.put(Context.INITIAL_CONTEXT_FACTORY, - "com.sun.jndi.ldap.LdapCtxFactory"); - URI ldapUri = new URI(args[0]); - if (ldapUri.getPort() == -1) { - ldapUri = new URI(ldapUri.getScheme(), null, ldapUri.getHost(), - serverSocket.getLocalPort(), ldapUri.getPath(), null, null); - } - env.put(Context.PROVIDER_URL, ldapUri.toString()); - env.put(Context.SECURITY_AUTHENTICATION, "simple"); - env.put(Context.SECURITY_PRINCIPAL, "cn=admin,dc=ie,dc=oracle,dc=com"); - env.put(Context.SECURITY_CREDENTIALS, "changeit"); - env.put(LdapContext.CONTROL_FACTORIES, - "org.example.authz.AuthzIdResponseControlFactory"); - if (args[args.length - 1].equalsIgnoreCase("-trace")) { - env.put("com.sun.jndi.ldap.trace.ber", System.out); - } + /* + * Connect to the LDAP directory + */ + + Hashtable env = new Hashtable<>(); + env.put(Context.INITIAL_CONTEXT_FACTORY, + "com.sun.jndi.ldap.LdapCtxFactory"); + URI ldapUri = new URI(args[0]); + if (ldapUri.getPort() == -1) { + ldapUri = new URI(ldapUri.getScheme(), null, ldapUri.getHost(), + serverSocket.getLocalPort(), ldapUri.getPath(), null, null); + } + env.put(Context.PROVIDER_URL, ldapUri.toString()); + env.put(Context.SECURITY_AUTHENTICATION, "simple"); + env.put(Context.SECURITY_PRINCIPAL, "cn=admin,dc=ie,dc=oracle,dc=com"); + env.put(Context.SECURITY_CREDENTIALS, "changeit"); + env.put(LdapContext.CONTROL_FACTORIES, + "org.example.authz.AuthzIdResponseControlFactory"); + if (args[args.length - 1].equalsIgnoreCase("-trace")) { + env.put("com.sun.jndi.ldap.trace.ber", System.out); + } - System.out.println("ConnectWithAuthzId: connecting to " + ldapUri); - LdapContext ctx = null; - Control[] connectionControls = { new AuthzIdRequestControl(false) }; - - try { - ctx = new InitialLdapContext(env, connectionControls); - System.out.println("ConnectWithAuthzId: connected"); - // Retrieve the response controls - Control[] responseControls = ctx.getResponseControls(); - if (responseControls != null) { - for (Control responseControl : responseControls) { - System.out.println("ConnectWithAuthzId: received response" + - " control: " + responseControl.getID()); - if (responseControl instanceof AuthzIdResponseControl) { - AuthzIdResponseControl authzId = - (AuthzIdResponseControl)responseControl; - System.out.println("ConnectWithAuthzId: identity is " + - authzId.getIdentity()); + System.out.println("ConnectWithAuthzId: connecting to " + ldapUri); + LdapContext ctx = null; + Control[] connectionControls = { new AuthzIdRequestControl(false) }; + + try { + ctx = new InitialLdapContext(env, connectionControls); + System.out.println("ConnectWithAuthzId: connected"); + // Retrieve the response controls + Control[] responseControls = ctx.getResponseControls(); + if (responseControls != null) { + for (Control responseControl : responseControls) { + System.out.println("ConnectWithAuthzId: received response" + + " control: " + responseControl.getID()); + if (responseControl instanceof AuthzIdResponseControl) { + AuthzIdResponseControl authzId = + (AuthzIdResponseControl)responseControl; + System.out.println("ConnectWithAuthzId: identity is " + + authzId.getIdentity()); + } } } - } - } catch (NamingException e) { - System.err.println("ConnectWithAuthzId: error connecting " + e); - } finally { - if (ctx != null) { - ctx.close(); + } catch (NamingException e) { + System.err.println("ConnectWithAuthzId: error connecting " + e); + } finally { + if (ctx != null) { + ctx.close(); + } } } }