|
24 | 24 | /*
|
25 | 25 | * @test
|
26 | 26 | * @bug 5016500
|
| 27 | + * @library /test/lib/ |
27 | 28 | * @summary Test SslRmi[Client|Server]SocketFactory SSL socket parameters.
|
28 | 29 | * @run main/othervm SSLSocketParametersTest 1
|
29 | 30 | * @run main/othervm SSLSocketParametersTest 2
|
|
33 | 34 | * @run main/othervm SSLSocketParametersTest 6
|
34 | 35 | * @run main/othervm SSLSocketParametersTest 7
|
35 | 36 | */
|
| 37 | +import jdk.test.lib.Asserts; |
| 38 | + |
36 | 39 | import java.io.IOException;
|
37 | 40 | import java.io.File;
|
38 | 41 | 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; |
41 | 44 | import java.rmi.Remote;
|
42 | 45 | import java.rmi.RemoteException;
|
43 |
| -import java.rmi.server.RMIClientSocketFactory; |
44 | 46 | import java.rmi.server.RMIServerSocketFactory;
|
45 | 47 | import java.rmi.server.UnicastRemoteObject;
|
46 | 48 | import javax.net.ssl.SSLContext;
|
|
50 | 52 | public class SSLSocketParametersTest implements Serializable {
|
51 | 53 |
|
52 | 54 | public interface Hello extends Remote {
|
53 |
| - public String sayHello() throws RemoteException; |
| 55 | + String sayHello() throws RemoteException; |
54 | 56 | }
|
55 | 57 |
|
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 { |
65 | 59 | public String sayHello() {
|
66 | 60 | return "Hello World!";
|
67 | 61 | }
|
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 |
| - } |
129 | 62 | }
|
130 | 63 |
|
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); |
134 | 68 | 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); |
152 | 73 | }
|
153 | 74 | }
|
154 | 75 |
|
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(), |
158 | 78 | 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 |
| - } |
168 | 79 | }
|
169 | 80 |
|
170 | 81 | public void runTest(int testNumber) throws Exception {
|
171 | 82 | System.out.println("Running test " + testNumber);
|
172 | 83 |
|
173 | 84 | switch (testNumber) {
|
174 | 85 | /* default constructor - default config */
|
175 |
| - case 1 -> testRmiCommunication(new ServerFactory(), false); |
| 86 | + case 1 -> |
| 87 | + testRmiCommunication(new SslRMIServerSocketFactory()); |
176 | 88 |
|
177 | 89 | /* 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)); |
179 | 92 |
|
180 | 93 | /* needClientAuth=true */
|
181 |
| - case 3 -> testRmiCommunication(new ServerFactory(null, null, null, true), false); |
| 94 | + case 3 -> |
| 95 | + testRmiCommunication(new SslRMIServerSocketFactory(null, null, null, true)); |
182 | 96 |
|
183 | 97 | /* 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 | + } |
186 | 105 |
|
187 | 106 | /* 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 | + } |
190 | 114 |
|
191 | 115 | /* client side dummy_ciphersuite */
|
192 | 116 | case 6 -> {
|
193 | 117 | System.setProperty("javax.rmi.ssl.client.enabledCipherSuites",
|
194 | 118 | "dummy_ciphersuite");
|
195 |
| - testRmiCommunication(new ServerFactory(), true); |
| 119 | + Asserts.assertThrows(ConnectIOException.class, |
| 120 | + () -> testRmiCommunication(new SslRMIServerSocketFactory())); |
196 | 121 | }
|
197 | 122 |
|
198 | 123 | /* client side dummy_protocol */
|
199 | 124 | case 7 -> {
|
200 | 125 | System.setProperty("javax.rmi.ssl.client.enabledProtocols",
|
201 | 126 | "dummy_protocol");
|
202 |
| - testRmiCommunication(new ServerFactory(), true); |
| 127 | + Asserts.assertThrows(ConnectIOException.class, |
| 128 | + () -> testRmiCommunication(new SslRMIServerSocketFactory())); |
203 | 129 | }
|
204 | 130 |
|
205 | 131 | default ->
|
|
0 commit comments