Mercurial > hg > monetdb-java
comparison tests/TLSTester.java @ 875:06f532009fda
Add verbose mode to TLSTester
author | Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com> |
---|---|
date | Tue, 27 Feb 2024 11:32:03 +0100 (14 months ago) |
parents | 68994abe3e03 |
children | c2164decf7f1 |
comparison
equal
deleted
inserted
replaced
874:0e304689c415 | 875:06f532009fda |
---|---|
11 */ | 11 */ |
12 | 12 |
13 import org.monetdb.mcl.net.Parameter; | 13 import org.monetdb.mcl.net.Parameter; |
14 | 14 |
15 import java.io.*; | 15 import java.io.*; |
16 import java.net.URISyntaxException; | |
16 import java.net.URL; | 17 import java.net.URL; |
17 import java.net.URLConnection; | 18 import java.net.URLConnection; |
18 import java.nio.charset.StandardCharsets; | 19 import java.nio.charset.StandardCharsets; |
19 import java.nio.file.Files; | 20 import java.nio.file.Files; |
20 import java.sql.Connection; | 21 import java.sql.Connection; |
111 } | 112 } |
112 } | 113 } |
113 fileCache.put(resource, outPath); | 114 fileCache.put(resource, outPath); |
114 } | 115 } |
115 | 116 |
116 private byte[] fetchBytes(String resource) throws IOException { | |
117 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
118 try (InputStream in = fetchData(resource)) { | |
119 byte[] buffer = new byte[22]; | |
120 while (true) { | |
121 int nread = in.read(buffer); | |
122 if (nread <= 0) | |
123 break; | |
124 out.write(buffer, 0, nread); | |
125 } | |
126 return out.toByteArray(); | |
127 } | |
128 } | |
129 | |
130 private InputStream fetchData(String resource) throws IOException { | 117 private InputStream fetchData(String resource) throws IOException { |
131 URL url; | 118 String urlText = "http://" + serverHost + ":" + serverPort + resource; |
119 if (verbose > 0) { | |
120 System.out.println("Fetching " + resource + " from " + urlText); | |
121 } | |
122 URL url = null; | |
132 try { | 123 try { |
133 // Note: as of Java version 20 java.net.URL(String) constructor is deprecated. | 124 url = new java.net.URI(urlText).toURL(); |
134 // https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/net/URL.html#%3Cinit%3E(java.lang.String) | 125 URLConnection conn = url.openConnection(); |
135 url = new java.net.URI("http://" + serverHost + ":" + serverPort + resource).toURL(); | 126 conn.connect(); |
136 } catch (java.net.URISyntaxException | java.net.MalformedURLException e) { | 127 return conn.getInputStream(); |
137 throw new IOException(e.getMessage()); | 128 } catch (URISyntaxException | IOException e) { |
138 } | 129 throw new IOException("Cannot fetch resource " + resource + " from " + urlText + ": " + e, e); |
139 URLConnection conn = url.openConnection(); | 130 } |
140 conn.connect(); | |
141 return conn.getInputStream(); | |
142 } | 131 } |
143 | 132 |
144 private void run() throws IOException, SQLException { | 133 private void run() throws IOException, SQLException { |
145 test_connect_plain(); | 134 test_connect_plain(); |
146 test_connect_tls(); | 135 test_connect_tls(); |
182 Attempt attempt = attempt("refuse_wrong_cert", "server1"); | 171 Attempt attempt = attempt("refuse_wrong_cert", "server1"); |
183 attempt.withFile(Parameter.CERT, "/ca2.crt").expectFailure("PKIX path building failed"); | 172 attempt.withFile(Parameter.CERT, "/ca2.crt").expectFailure("PKIX path building failed"); |
184 } | 173 } |
185 | 174 |
186 private void test_refuse_wrong_host() throws IOException, SQLException { | 175 private void test_refuse_wrong_host() throws IOException, SQLException { |
176 if (altHost == null) | |
177 return; | |
187 Attempt attempt = attempt("refuse_wrong_host", "server1").with(Parameter.HOST, altHost); | 178 Attempt attempt = attempt("refuse_wrong_host", "server1").with(Parameter.HOST, altHost); |
188 attempt.withFile(Parameter.CERT, "/ca1.crt").expectFailure("No subject alternative DNS name"); | 179 attempt.withFile(Parameter.CERT, "/ca1.crt").expectFailure("No subject alternative DNS name"); |
189 } | 180 } |
190 | 181 |
191 private void test_refuse_tlsv12() throws IOException, SQLException { | 182 private void test_refuse_tlsv12() throws IOException, SQLException { |
297 | 288 |
298 public void expectSuccess() throws SQLException { | 289 public void expectSuccess() throws SQLException { |
299 preparedButNotRun.remove(testName); | 290 preparedButNotRun.remove(testName); |
300 if (disabled) | 291 if (disabled) |
301 return; | 292 return; |
293 startVerbose(); | |
302 try { | 294 try { |
303 Connection conn = DriverManager.getConnection("jdbc:monetdb:", props); | 295 Connection conn = DriverManager.getConnection("jdbc:monetdb:", props); |
304 conn.close(); | 296 conn.close(); |
297 throw new RuntimeException("Test " + testName + " was supposed to throw an Exception saying 'Sorry, this is not a real MonetDB instance'"); | |
305 } catch (SQLException e) { | 298 } catch (SQLException e) { |
306 if (e.getMessage().startsWith("Sorry, this is not a real MonetDB instance")) { | 299 if (e.getMessage().startsWith("Sorry, this is not a real MonetDB instance")) { |
307 // it looks like a failure but this is actually our success scenario | 300 // it looks like a failure but this is actually our success scenario |
308 // because this is what the TLS Tester does when the connection succeeds. | 301 // because this is what the TLS Tester does when the connection succeeds. |
302 endVerbose("succesful MAPI handshake, as expected"); | |
309 return; | 303 return; |
310 } | 304 } |
311 // other exceptions ARE errors and should be reported. | 305 // other exceptions ARE errors and should be reported. |
312 throw e; | 306 throw e; |
313 } | 307 } |
314 } | 308 } |
315 | 309 |
316 public void expectFailure(String... expectedMessages) throws SQLException { | 310 public void expectFailure(String... expectedMessages) throws SQLException { |
311 preparedButNotRun.remove(testName); | |
317 if (disabled) | 312 if (disabled) |
318 return; | 313 return; |
314 startVerbose(); | |
319 try { | 315 try { |
320 expectSuccess(); | 316 Connection conn = DriverManager.getConnection("jdbc:monetdb:", props); |
317 conn.close(); | |
321 throw new RuntimeException("Expected test " + testName + " to throw an exception but it didn't"); | 318 throw new RuntimeException("Expected test " + testName + " to throw an exception but it didn't"); |
322 } catch (SQLException e) { | 319 } catch (SQLException e) { |
323 for (String expected : expectedMessages) { | 320 for (String expected : expectedMessages) { |
324 if (e.getMessage().contains(expected)) | 321 if (e.getMessage().contains(expected)) { |
322 endVerbose("connection failed as expected, message: " + e.getMessage()); | |
325 return; | 323 return; |
324 } | |
326 } | 325 } |
327 String message = "Test " + testName + " threw the wrong exception: " + e.getMessage() + '\n' + "Expected:\n <" + String.join(">\n <", expectedMessages) + ">"; | 326 String message = "Test " + testName + " threw the wrong exception: " + e.getMessage() + '\n' + "Expected:\n <" + String.join(">\n <", expectedMessages) + ">"; |
328 throw new RuntimeException(message, e); | 327 throw new RuntimeException(message, e); |
329 } | 328 } |
330 } | 329 } |
330 | |
331 private void startVerbose() { | |
332 if (verbose == 0) | |
333 return; | |
334 | |
335 System.out.println("Test " + testName + ":"); | |
336 for (String key: props.stringPropertyNames()) { | |
337 Object value = props.get(key); | |
338 if (value == null) | |
339 System.out.println(" " + key + " is null"); | |
340 else | |
341 System.out.println(" " + key + " = " + value.toString()); | |
342 } | |
343 } | |
344 | |
345 private void endVerbose(String message) { | |
346 if (verbose > 0) { | |
347 System.out.println(" -> " + message); | |
348 System.out.println(); | |
349 } | |
350 } | |
331 } | 351 } |
332 } | 352 } |