Skip to content

Commit 0c923e8

Browse files
committed
8303525: Refactor/cleanup open/test/jdk/javax/rmi/ssl/SSLSocketParametersTest.java
Backport-of: 704c6ea16cabc217588f430fd3c302d6df5e9c19
1 parent e97a8fa commit 0c923e8

File tree

2 files changed

+81
-115
lines changed

2 files changed

+81
-115
lines changed

test/jdk/javax/rmi/ssl/SSLSocketParametersTest.java

Lines changed: 41 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
/*
2525
* @test
2626
* @bug 5016500
27+
* @library /test/lib/
2728
* @summary Test SslRmi[Client|Server]SocketFactory SSL socket parameters.
2829
* @run main/othervm SSLSocketParametersTest 1
2930
* @run main/othervm SSLSocketParametersTest 2
@@ -33,14 +34,15 @@
3334
* @run main/othervm SSLSocketParametersTest 6
3435
* @run main/othervm SSLSocketParametersTest 7
3536
*/
37+
import jdk.test.lib.Asserts;
38+
3639
import java.io.IOException;
3740
import java.io.File;
3841
import java.io.Serializable;
39-
import java.net.ServerSocket;
40-
import java.net.Socket;
42+
import java.lang.ref.Reference;
43+
import java.rmi.ConnectIOException;
4144
import java.rmi.Remote;
4245
import java.rmi.RemoteException;
43-
import java.rmi.server.RMIClientSocketFactory;
4446
import java.rmi.server.RMIServerSocketFactory;
4547
import java.rmi.server.UnicastRemoteObject;
4648
import javax.net.ssl.SSLContext;
@@ -50,156 +52,80 @@
5052
public class SSLSocketParametersTest implements Serializable {
5153

5254
public interface Hello extends Remote {
53-
public String sayHello() throws RemoteException;
55+
String sayHello() throws RemoteException;
5456
}
5557

56-
public class HelloImpl extends UnicastRemoteObject implements Hello {
57-
58-
public HelloImpl(int port,
59-
RMIClientSocketFactory csf,
60-
RMIServerSocketFactory ssf)
61-
throws RemoteException {
62-
super(port, csf, ssf);
63-
}
64-
58+
public class HelloImpl implements Hello {
6559
public String sayHello() {
6660
return "Hello World!";
6761
}
68-
69-
public Remote runServer() throws IOException {
70-
System.out.println("Inside HelloImpl::runServer");
71-
// Get a remote stub for this RMI object
72-
//
73-
Remote stub = toStub(this);
74-
System.out.println("Stub = " + stub);
75-
return stub;
76-
}
77-
}
78-
79-
public class HelloClient {
80-
81-
public void runClient(Remote stub) throws IOException {
82-
System.out.println("Inside HelloClient::runClient");
83-
// "obj" is the identifier that we'll use to refer
84-
// to the remote object that implements the "Hello"
85-
// interface
86-
Hello obj = (Hello) stub;
87-
String message = obj.sayHello();
88-
System.out.println(message);
89-
}
90-
}
91-
92-
public static class ClientFactory extends SslRMIClientSocketFactory {
93-
94-
public ClientFactory() {
95-
super();
96-
}
97-
98-
public Socket createSocket(String host, int port) throws IOException {
99-
System.out.println("ClientFactory::Calling createSocket(" +
100-
host + "," + port + ")");
101-
return super.createSocket(host, port);
102-
}
103-
}
104-
105-
public static class ServerFactory extends SslRMIServerSocketFactory {
106-
107-
public ServerFactory() {
108-
super();
109-
}
110-
111-
public ServerFactory(String[] ciphers,
112-
String[] protocols,
113-
boolean need) {
114-
super(ciphers, protocols, need);
115-
}
116-
117-
public ServerFactory(SSLContext context,
118-
String[] ciphers,
119-
String[] protocols,
120-
boolean need) {
121-
super(context, ciphers, protocols, need);
122-
}
123-
124-
public ServerSocket createServerSocket(int port) throws IOException {
125-
System.out.println("ServerFactory::Calling createServerSocket(" +
126-
port + ")");
127-
return super.createServerSocket(port);
128-
}
12962
}
13063

131-
public void testRmiCommunication(RMIServerSocketFactory serverFactory, boolean expectException) {
132-
133-
HelloImpl server = null;
64+
public void testRmiCommunication(RMIServerSocketFactory serverSocketFactory) throws Exception {
65+
HelloImpl server = new HelloImpl();
66+
Hello stub = (Hello)UnicastRemoteObject.exportObject(server,
67+
0, new SslRMIClientSocketFactory(), serverSocketFactory);
13468
try {
135-
server = new HelloImpl(0,
136-
new ClientFactory(),
137-
serverFactory);
138-
Remote stub = server.runServer();
139-
HelloClient client = new HelloClient();
140-
client.runClient(stub);
141-
if (expectException) {
142-
throw new RuntimeException("Test completed without throwing an expected exception.");
143-
}
144-
145-
} catch (IOException exc) {
146-
if (!expectException) {
147-
throw new RuntimeException("An error occurred during test execution", exc);
148-
} else {
149-
System.out.println("Caught expected exception: " + exc);
150-
}
151-
69+
String msg = stub.sayHello();
70+
Asserts.assertEquals("Hello World!", msg);
71+
} finally {
72+
Reference.reachabilityFence(server);
15273
}
15374
}
15475

155-
private static void testServerFactory(String[] cipherSuites, String[] protocol, String expectedMessage) throws Exception {
156-
try {
157-
new ServerFactory(SSLContext.getDefault(),
76+
private static void testSslServerSocketFactory(String[] cipherSuites, String[] protocol) throws Exception {
77+
new SslRMIServerSocketFactory(SSLContext.getDefault(),
15878
cipherSuites, protocol, false);
159-
throw new RuntimeException(
160-
"The expected exception for "+ expectedMessage + " was not thrown.");
161-
} catch (IllegalArgumentException exc) {
162-
// expecting an exception with a specific message
163-
// anything else is an error
164-
if (!exc.getMessage().toLowerCase().contains(expectedMessage)) {
165-
throw exc;
166-
}
167-
}
16879
}
16980

17081
public void runTest(int testNumber) throws Exception {
17182
System.out.println("Running test " + testNumber);
17283

17384
switch (testNumber) {
17485
/* default constructor - default config */
175-
case 1 -> testRmiCommunication(new ServerFactory(), false);
86+
case 1 ->
87+
testRmiCommunication(new SslRMIServerSocketFactory());
17688

17789
/* non-default constructor - default config */
178-
case 2 -> testRmiCommunication(new ServerFactory(null, null, false), false);
90+
case 2 ->
91+
testRmiCommunication(new SslRMIServerSocketFactory(null, null, false));
17992

18093
/* needClientAuth=true */
181-
case 3 -> testRmiCommunication(new ServerFactory(null, null, null, true), false);
94+
case 3 ->
95+
testRmiCommunication(new SslRMIServerSocketFactory(null, null, null, true));
18296

18397
/* server side dummy_ciphersuite */
184-
case 4 ->
185-
testServerFactory(new String[]{"dummy_ciphersuite"}, null, "unsupported ciphersuite");
98+
case 4 -> {
99+
Exception exc = Asserts.assertThrows(IllegalArgumentException.class,
100+
() -> testSslServerSocketFactory(new String[]{"dummy_ciphersuite"}, null));
101+
if (!exc.getMessage().toLowerCase().contains("unsupported ciphersuite")) {
102+
throw exc;
103+
}
104+
}
186105

187106
/* server side dummy_protocol */
188-
case 5 ->
189-
testServerFactory(null, new String[]{"dummy_protocol"}, "unsupported protocol");
107+
case 5 -> {
108+
Exception thrown = Asserts.assertThrows(IllegalArgumentException.class,
109+
() -> testSslServerSocketFactory(null, new String[]{"dummy_protocol"}));
110+
if (!thrown.getMessage().toLowerCase().contains("unsupported protocol")) {
111+
throw thrown;
112+
}
113+
}
190114

191115
/* client side dummy_ciphersuite */
192116
case 6 -> {
193117
System.setProperty("javax.rmi.ssl.client.enabledCipherSuites",
194118
"dummy_ciphersuite");
195-
testRmiCommunication(new ServerFactory(), true);
119+
Asserts.assertThrows(ConnectIOException.class,
120+
() -> testRmiCommunication(new SslRMIServerSocketFactory()));
196121
}
197122

198123
/* client side dummy_protocol */
199124
case 7 -> {
200125
System.setProperty("javax.rmi.ssl.client.enabledProtocols",
201126
"dummy_protocol");
202-
testRmiCommunication(new ServerFactory(), true);
127+
Asserts.assertThrows(ConnectIOException.class,
128+
() -> testRmiCommunication(new SslRMIServerSocketFactory()));
203129
}
204130

205131
default ->

test/lib/jdk/test/lib/Asserts.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,46 @@ public static void assertStringsEqual(String str1, String str2,
553553
}
554554
}
555555

556+
/**
557+
* A functional interface for executing tests in assertThrownException
558+
*/
559+
@FunctionalInterface
560+
public interface TestMethod {
561+
void execute() throws Throwable;
562+
}
563+
564+
565+
public static <T extends Throwable> T assertThrows(Class<T> expected, TestMethod testMethod) {
566+
return assertThrows(expected, testMethod, "An unexpected exception was thrown.");
567+
}
568+
569+
/**
570+
* Asserts that the given exception (or a subclass of it) is thrown when
571+
* executing the test method.
572+
*
573+
* If the test method throws the correct exception, the exception is returned
574+
* to the caller for additional validation e.g., comparing the exception
575+
* message.
576+
*
577+
* @param expected The expected exception
578+
* @param testMethod The code to execute that should throw the exception
579+
* @param msg A description of the assumption
580+
* @return The thrown exception.
581+
*/
582+
public static <T extends Throwable> T assertThrows(Class<T> expected, TestMethod testMethod, String msg) {
583+
try {
584+
testMethod.execute();
585+
} catch (Throwable exc) {
586+
if (expected.isInstance(exc)) {
587+
return (T) exc;
588+
} else {
589+
fail(Objects.toString(msg, "An unexpected exception was thrown.")
590+
+ " Expected " + expected.getName(), exc);
591+
}
592+
}
593+
throw new RuntimeException("No exception was thrown. Expected: " + expected.getName());
594+
}
595+
556596
/**
557597
* Returns a string formatted with a message and expected and actual values.
558598
* @param lhs the actual value

0 commit comments

Comments
 (0)