comparison tests/TLSTester.java @ 805:2fee4b71baac monetdbs

Set ALPN protocol if the runtime supports it (Use introspection because Java 8 can't do it)
author Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
date Mon, 11 Dec 2023 15:47:19 +0100 (16 months ago)
parents 361441253305
children 5aa19bbed0d6
comparison
equal deleted inserted replaced
804:361441253305 805:2fee4b71baac
7 import java.nio.file.Files; 7 import java.nio.file.Files;
8 import java.sql.Connection; 8 import java.sql.Connection;
9 import java.sql.DriverManager; 9 import java.sql.DriverManager;
10 import java.sql.SQLException; 10 import java.sql.SQLException;
11 import java.util.HashMap; 11 import java.util.HashMap;
12 import java.util.HashSet;
12 import java.util.Properties; 13 import java.util.Properties;
14 import java.util.stream.Collectors;
13 15
14 public class TLSTester { 16 public class TLSTester {
15 int verbose = 0; 17 int verbose = 0;
16 String serverHost = null; 18 String serverHost = null;
17 String altHost = null; 19 String altHost = null;
18 int serverPort = -1; 20 int serverPort = -1;
19 boolean enableTrusted = false; 21 boolean enableTrusted = false;
20 File tempDir = null; 22 File tempDir = null;
21 final HashMap<String, File> fileCache = new HashMap<>(); 23 final HashMap<String, File> fileCache = new HashMap<>();
24 private HashSet<String> preparedButNotRun = new HashSet<>();
22 25
23 public TLSTester(String[] args) { 26 public TLSTester(String[] args) {
24 for (int i = 0; i < args.length; i++) { 27 for (int i = 0; i < args.length; i++) {
25 String arg = args[i]; 28 String arg = args[i];
26 if (arg.equals("-v")) { 29 if (arg.equals("-v")) {
131 // test_connect_client_auth1(); 134 // test_connect_client_auth1();
132 // test_connect_client_auth2(); 135 // test_connect_client_auth2();
133 test_fail_tls_to_plain(); 136 test_fail_tls_to_plain();
134 test_fail_plain_to_tls(); 137 test_fail_plain_to_tls();
135 test_connect_server_name(); 138 test_connect_server_name();
136 // test_connect_alpn_mapi9(); 139 test_connect_alpn_mapi9();
137 test_connect_trusted(); 140 test_connect_trusted();
138 test_refuse_trusted_wrong_host(); 141 test_refuse_trusted_wrong_host();
142
143 // did we forget to call expectSucceed and expectFailure somewhere?
144 if (!preparedButNotRun.isEmpty()) {
145 String names = String.join(", ", preparedButNotRun);
146 throw new RuntimeException("Not all tests called expectSuccess/expectFailure: " + names);
147 }
139 } 148 }
140 149
141 private void test_connect_plain() throws IOException, SQLException { 150 private void test_connect_plain() throws IOException, SQLException {
142 attempt("connect_plain", "plain").with(Parameter.TLS, false).expectSuccess(); 151 attempt("connect_plain", "plain").with(Parameter.TLS, false).expectSuccess();
143 } 152 }
200 Attempt attempt = attempt("connect_server_name", "sni"); 209 Attempt attempt = attempt("connect_server_name", "sni");
201 attempt.withFile(Parameter.CERT, "/ca1.crt").expectSuccess(); 210 attempt.withFile(Parameter.CERT, "/ca1.crt").expectSuccess();
202 } 211 }
203 212
204 private void test_connect_alpn_mapi9() throws IOException, SQLException { 213 private void test_connect_alpn_mapi9() throws IOException, SQLException {
205 attempt("connect_alpn_mapi9", ""); 214 attempt("connect_alpn_mapi9", "alpn_mapi9")
215 .withFile(Parameter.CERT, "/ca1.crt")
216 .expectSuccess();
206 } 217 }
207 218
208 private void test_connect_trusted() throws IOException, SQLException { 219 private void test_connect_trusted() throws IOException, SQLException {
209 attempt("connect_trusted", "alpn_mapi9") 220 attempt("connect_trusted", null)
210 .with(Parameter.HOST, "monetdb.ergates.nl") 221 .with(Parameter.HOST, "monetdb.ergates.nl")
211 .with(Parameter.PORT, 50000) 222 .with(Parameter.PORT, 50000)
212 .expectSuccess(); 223 .expectSuccess();
213 } 224 }
214 225
215 private void test_refuse_trusted_wrong_host() throws IOException, SQLException { 226 private void test_refuse_trusted_wrong_host() throws IOException, SQLException {
216 attempt("connect_trusted", null) 227 attempt("test_refuse_trusted_wrong_host", null)
217 .with(Parameter.HOST, "monetdbxyz.ergates.nl") 228 .with(Parameter.HOST, "monetdbxyz.ergates.nl")
218 .with(Parameter.PORT, 50000) 229 .with(Parameter.PORT, 50000)
219 .expectFailure("No subject alternative DNS name"); 230 .expectFailure("No subject alternative DNS name");
220 } 231 }
221 232
222 private Attempt attempt(String testName, String portName) throws IOException { 233 private Attempt attempt(String testName, String portName) throws IOException {
234 preparedButNotRun.add(testName);
223 return new Attempt(testName, portName); 235 return new Attempt(testName, portName);
224 } 236 }
225 237
226 private class Attempt { 238 private class Attempt {
227 private final String testName; 239 private final String testName;
266 with(parm, path); 278 with(parm, path);
267 return this; 279 return this;
268 } 280 }
269 281
270 public void expectSuccess() throws SQLException { 282 public void expectSuccess() throws SQLException {
283 preparedButNotRun.remove(testName);
271 if (disabled) 284 if (disabled)
272 return; 285 return;
273 try { 286 try {
274 Connection conn = DriverManager.getConnection("jdbc:monetdb:", props); 287 Connection conn = DriverManager.getConnection("jdbc:monetdb:", props);
275 conn.close(); 288 conn.close();