Skip to content

Commit bec83f3

Browse files
committed
8211920: Close server socket and cleanups in test/jdk/javax/naming/module/RunBasic.java
Reviewed-by: yan, andrew Backport-of: e61252d
1 parent 59b3859 commit bec83f3

File tree

8 files changed

+538
-469
lines changed

8 files changed

+538
-469
lines changed

test/jdk/javax/naming/module/RunBasic.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
import jdk.test.lib.process.ProcessTools;
2828

2929
import java.io.IOException;
30+
import java.net.InetAddress;
3031
import java.nio.file.Files;
3132
import java.nio.file.Path;
33+
import java.time.Duration;
3234
import java.util.Collection;
3335
import java.util.Collections;
3436
import java.util.List;
@@ -65,6 +67,8 @@ public class RunBasic {
6567

6668
private static final List<String> JAVA_CMDS;
6769

70+
static final String HOST_NAME = InetAddress.getLoopbackAddress().getHostName();
71+
6872
static {
6973
String javaPath = JDKToolFinder.getJDKTool("java");
7074

@@ -85,6 +89,8 @@ public static void main(String[] args) throws Throwable {
8589
prepareModule("test", "--module-source-path",
8690
Path.of(TEST_SRC, "src").toString());
8791

92+
System.out.println("Hostname: [" + HOST_NAME + "]");
93+
8894
// run tests
8995
runTest("java.desktop", "test.StoreObject");
9096
runTest("person", "test.StorePerson");
@@ -98,9 +104,12 @@ public static void main(String[] args) throws Throwable {
98104
private static void prepareModule(String mod, String... opts)
99105
throws IOException {
100106
System.out.println("Preparing the '" + mod + "' module...");
107+
long start = System.nanoTime();
101108
makeDir("mods", mod);
102109
CompilerUtils.compile(Path.of(TEST_SRC, "src", mod),
103110
Path.of("mods", (mod.equals("test") ? "" : mod)), opts);
111+
Duration duration = Duration.ofNanos(System.nanoTime() - start);
112+
System.out.println("completed: duration - " + duration );
104113
}
105114

106115
private static void makeDir(String first, String... more)
@@ -111,7 +120,7 @@ private static void makeDir(String first, String... more)
111120
private static void runTest(String desc, String clsName) throws Throwable {
112121
System.out.println("Running with the '" + desc + "' module...");
113122
runJava("-Dtest.src=" + TEST_SRC, "-p", "mods", "-m", "test/" + clsName,
114-
"ldap://localhost/dc=ie,dc=oracle,dc=com");
123+
"ldap://" + HOST_NAME + "/dc=ie,dc=oracle,dc=com");
115124
}
116125

117126
private static void runJava(String... opts) throws Throwable {

test/jdk/javax/naming/module/src/test/test/ConnectWithAuthzId.java

Lines changed: 69 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -29,6 +29,7 @@
2929

3030
package test;
3131

32+
import java.io.PrintStream;
3233
import java.net.*;
3334
import java.util.*;
3435
import javax.naming.*;
@@ -40,12 +41,18 @@
4041

4142
public class ConnectWithAuthzId {
4243

44+
static {
45+
final PrintStream out = new PrintStream(System.out, true);
46+
final PrintStream err = new PrintStream(System.err, true);
47+
48+
System.setOut(out);
49+
System.setErr(err);
50+
}
51+
4352
// LDAP capture file
4453
private static final String LDAP_CAPTURE_FILE =
4554
System.getProperty("test.src") +
4655
"/src/test/test/ConnectWithAuthzId.ldap";
47-
// LDAPServer socket
48-
private static ServerSocket serverSocket;
4956

5057
public static void main(String[] args) throws Exception {
5158

@@ -68,67 +75,69 @@ public static void main(String[] args) throws Exception {
6875
* Launch the LDAP server with the ConnectWithAuthzId.ldap capture file
6976
*/
7077

71-
serverSocket = new ServerSocket(0);
72-
new Thread(new Runnable() {
73-
@Override
74-
public void run() {
75-
try {
76-
new LDAPServer(serverSocket, LDAP_CAPTURE_FILE);
77-
} catch (Exception e) {
78-
System.out.println("ERROR: unable to launch LDAP server");
79-
e.printStackTrace();
80-
}
78+
try (ServerSocket serverSocket = new ServerSocket()) {
79+
serverSocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
80+
new Thread(new Runnable() {
81+
@Override
82+
public void run() {
83+
try {
84+
new LDAPServer(serverSocket, LDAP_CAPTURE_FILE);
85+
} catch (Exception e) {
86+
System.out.println("ERROR: unable to launch LDAP server");
87+
e.printStackTrace();
88+
}
89+
}
90+
}).start();
91+
92+
/*
93+
* Connect to the LDAP directory
94+
*/
95+
96+
Hashtable<String,Object> env = new Hashtable<>();
97+
env.put(Context.INITIAL_CONTEXT_FACTORY,
98+
"com.sun.jndi.ldap.LdapCtxFactory");
99+
URI ldapUri = new URI(args[0]);
100+
if (ldapUri.getPort() == -1) {
101+
ldapUri = new URI(ldapUri.getScheme(), null, ldapUri.getHost(),
102+
serverSocket.getLocalPort(), ldapUri.getPath(), null, null);
103+
}
104+
env.put(Context.PROVIDER_URL, ldapUri.toString());
105+
env.put(Context.SECURITY_AUTHENTICATION, "simple");
106+
env.put(Context.SECURITY_PRINCIPAL, "cn=admin,dc=ie,dc=oracle,dc=com");
107+
env.put(Context.SECURITY_CREDENTIALS, "changeit");
108+
env.put(LdapContext.CONTROL_FACTORIES,
109+
"org.example.authz.AuthzIdResponseControlFactory");
110+
if (args[args.length - 1].equalsIgnoreCase("-trace")) {
111+
env.put("com.sun.jndi.ldap.trace.ber", System.out);
81112
}
82-
}).start();
83-
84-
/*
85-
* Connect to the LDAP directory
86-
*/
87-
88-
Hashtable<String,Object> env = new Hashtable<>();
89-
env.put(Context.INITIAL_CONTEXT_FACTORY,
90-
"com.sun.jndi.ldap.LdapCtxFactory");
91-
URI ldapUri = new URI(args[0]);
92-
if (ldapUri.getPort() == -1) {
93-
ldapUri = new URI(ldapUri.getScheme(), null, ldapUri.getHost(),
94-
serverSocket.getLocalPort(), ldapUri.getPath(), null, null);
95-
}
96-
env.put(Context.PROVIDER_URL, ldapUri.toString());
97-
env.put(Context.SECURITY_AUTHENTICATION, "simple");
98-
env.put(Context.SECURITY_PRINCIPAL, "cn=admin,dc=ie,dc=oracle,dc=com");
99-
env.put(Context.SECURITY_CREDENTIALS, "changeit");
100-
env.put(LdapContext.CONTROL_FACTORIES,
101-
"org.example.authz.AuthzIdResponseControlFactory");
102-
if (args[args.length - 1].equalsIgnoreCase("-trace")) {
103-
env.put("com.sun.jndi.ldap.trace.ber", System.out);
104-
}
105113

106-
System.out.println("ConnectWithAuthzId: connecting to " + ldapUri);
107-
LdapContext ctx = null;
108-
Control[] connectionControls = { new AuthzIdRequestControl(false) };
109-
110-
try {
111-
ctx = new InitialLdapContext(env, connectionControls);
112-
System.out.println("ConnectWithAuthzId: connected");
113-
// Retrieve the response controls
114-
Control[] responseControls = ctx.getResponseControls();
115-
if (responseControls != null) {
116-
for (Control responseControl : responseControls) {
117-
System.out.println("ConnectWithAuthzId: received response" +
118-
" control: " + responseControl.getID());
119-
if (responseControl instanceof AuthzIdResponseControl) {
120-
AuthzIdResponseControl authzId =
121-
(AuthzIdResponseControl)responseControl;
122-
System.out.println("ConnectWithAuthzId: identity is " +
123-
authzId.getIdentity());
114+
System.out.println("ConnectWithAuthzId: connecting to " + ldapUri);
115+
LdapContext ctx = null;
116+
Control[] connectionControls = { new AuthzIdRequestControl(false) };
117+
118+
try {
119+
ctx = new InitialLdapContext(env, connectionControls);
120+
System.out.println("ConnectWithAuthzId: connected");
121+
// Retrieve the response controls
122+
Control[] responseControls = ctx.getResponseControls();
123+
if (responseControls != null) {
124+
for (Control responseControl : responseControls) {
125+
System.out.println("ConnectWithAuthzId: received response" +
126+
" control: " + responseControl.getID());
127+
if (responseControl instanceof AuthzIdResponseControl) {
128+
AuthzIdResponseControl authzId =
129+
(AuthzIdResponseControl)responseControl;
130+
System.out.println("ConnectWithAuthzId: identity is " +
131+
authzId.getIdentity());
132+
}
124133
}
125134
}
126-
}
127-
} catch (NamingException e) {
128-
System.err.println("ConnectWithAuthzId: error connecting " + e);
129-
} finally {
130-
if (ctx != null) {
131-
ctx.close();
135+
} catch (NamingException e) {
136+
System.err.println("ConnectWithAuthzId: error connecting " + e);
137+
} finally {
138+
if (ctx != null) {
139+
ctx.close();
140+
}
132141
}
133142
}
134143
}

test/jdk/javax/naming/module/src/test/test/ConnectWithFoo.java

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -28,6 +28,7 @@
2828

2929
package test;
3030

31+
import java.io.PrintStream;
3132
import java.net.*;
3233
import java.util.*;
3334
import javax.naming.*;
@@ -38,11 +39,17 @@
3839

3940
public class ConnectWithFoo {
4041

42+
static {
43+
final PrintStream out = new PrintStream(System.out, true);
44+
final PrintStream err = new PrintStream(System.err, true);
45+
46+
System.setOut(out);
47+
System.setErr(err);
48+
}
49+
4150
// LDAP capture file
4251
private static final String LDAP_CAPTURE_FILE =
4352
System.getProperty("test.src") + "/src/test/test/ConnectWithFoo.ldap";
44-
// LDAPServer socket
45-
private static ServerSocket serverSocket;
4653

4754
public static void main(String[] args) throws Exception {
4855

@@ -65,48 +72,50 @@ public static void main(String[] args) throws Exception {
6572
* Launch the LDAP server with the ConnectWithFoo.ldap capture file
6673
*/
6774

68-
serverSocket = new ServerSocket(0);
69-
new Thread(new Runnable() {
70-
@Override
71-
public void run() {
72-
try {
73-
new LDAPServer(serverSocket, LDAP_CAPTURE_FILE);
74-
} catch (Exception e) {
75-
System.out.println("ERROR: unable to launch LDAP server");
76-
e.printStackTrace();
77-
}
75+
try (ServerSocket serverSocket = new ServerSocket()) {
76+
serverSocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
77+
new Thread(new Runnable() {
78+
@Override
79+
public void run() {
80+
try {
81+
new LDAPServer(serverSocket, LDAP_CAPTURE_FILE);
82+
} catch (Exception e) {
83+
System.out.println("ERROR: unable to launch LDAP server");
84+
e.printStackTrace();
85+
}
86+
}
87+
}).start();
88+
89+
/*
90+
* Connect to the LDAP directory
91+
*/
92+
93+
Hashtable<String,Object> env = new Hashtable<>();
94+
env.put(Context.INITIAL_CONTEXT_FACTORY,
95+
"com.sun.jndi.ldap.LdapCtxFactory");
96+
URI ldapUri = new URI(args[0]);
97+
if (ldapUri.getPort() == -1) {
98+
ldapUri = new URI(ldapUri.getScheme(), null, ldapUri.getHost(),
99+
serverSocket.getLocalPort(), ldapUri.getPath(), null, null);
100+
}
101+
env.put(Context.PROVIDER_URL, ldapUri.toString());
102+
if (args[args.length - 1].equalsIgnoreCase("-trace")) {
103+
env.put("com.sun.jndi.ldap.trace.ber", System.out);
78104
}
79-
}).start();
80-
81-
/*
82-
* Connect to the LDAP directory
83-
*/
84-
85-
Hashtable<String,Object> env = new Hashtable<>();
86-
env.put(Context.INITIAL_CONTEXT_FACTORY,
87-
"com.sun.jndi.ldap.LdapCtxFactory");
88-
URI ldapUri = new URI(args[0]);
89-
if (ldapUri.getPort() == -1) {
90-
ldapUri = new URI(ldapUri.getScheme(), null, ldapUri.getHost(),
91-
serverSocket.getLocalPort(), ldapUri.getPath(), null, null);
92-
}
93-
env.put(Context.PROVIDER_URL, ldapUri.toString());
94-
if (args[args.length - 1].equalsIgnoreCase("-trace")) {
95-
env.put("com.sun.jndi.ldap.trace.ber", System.out);
96-
}
97105

98-
System.out.println("ConnectWithFoo: connecting to " + ldapUri);
99-
LdapContext ctx = null;
100-
Control[] connectionControls = { new FooControl(false) };
101-
102-
try {
103-
ctx = new InitialLdapContext(env, connectionControls);
104-
System.out.println("ConnectWithFoo: connected");
105-
} catch (NamingException e) {
106-
System.err.println("ConnectWithFoo: error connecting " + e);
107-
} finally {
108-
if (ctx != null) {
109-
ctx.close();
106+
System.out.println("ConnectWithFoo: connecting to " + ldapUri);
107+
LdapContext ctx = null;
108+
Control[] connectionControls = { new FooControl(false) };
109+
110+
try {
111+
ctx = new InitialLdapContext(env, connectionControls);
112+
System.out.println("ConnectWithFoo: connected");
113+
} catch (NamingException e) {
114+
System.err.println("ConnectWithFoo: error connecting " + e);
115+
} finally {
116+
if (ctx != null) {
117+
ctx.close();
118+
}
110119
}
111120
}
112121
}

0 commit comments

Comments
 (0)