annotate tests/TLSTester.java @ 859:68994abe3e03

Also catch java.net.URISyntaxException and java.net.MalformedURLException and convert them to an IOException.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 01 Feb 2024 14:32:05 +0100 (14 months ago)
parents d74e850e2991
children 06f532009fda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
855
839d76eeb4ae Added missing Copyright header comments
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 851
diff changeset
1 /*
839d76eeb4ae Added missing Copyright header comments
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 851
diff changeset
2 * SPDX-License-Identifier: MPL-2.0
839d76eeb4ae Added missing Copyright header comments
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 851
diff changeset
3 *
839d76eeb4ae Added missing Copyright header comments
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 851
diff changeset
4 * This Source Code Form is subject to the terms of the Mozilla Public
839d76eeb4ae Added missing Copyright header comments
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 851
diff changeset
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
839d76eeb4ae Added missing Copyright header comments
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 851
diff changeset
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
839d76eeb4ae Added missing Copyright header comments
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 851
diff changeset
7 *
839d76eeb4ae Added missing Copyright header comments
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 851
diff changeset
8 * Copyright 2024 MonetDB Foundation;
839d76eeb4ae Added missing Copyright header comments
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 851
diff changeset
9 * Copyright August 2008 - 2023 MonetDB B.V.;
839d76eeb4ae Added missing Copyright header comments
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 851
diff changeset
10 * Copyright 1997 - July 2008 CWI.
839d76eeb4ae Added missing Copyright header comments
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 851
diff changeset
11 */
839d76eeb4ae Added missing Copyright header comments
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 851
diff changeset
12
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
13 import org.monetdb.mcl.net.Parameter;
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
14
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
15 import java.io.*;
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
16 import java.net.URL;
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
17 import java.net.URLConnection;
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
18 import java.nio.charset.StandardCharsets;
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
19 import java.nio.file.Files;
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
20 import java.sql.Connection;
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
21 import java.sql.DriverManager;
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
22 import java.sql.SQLException;
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
23 import java.util.HashMap;
805
2fee4b71baac Set ALPN protocol if the runtime supports it
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 804
diff changeset
24 import java.util.HashSet;
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
25 import java.util.Properties;
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
26
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
27 public class TLSTester {
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
28 final HashMap<String, File> fileCache = new HashMap<>();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
29 int verbose = 0;
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
30 String serverHost = null;
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
31 String altHost = null;
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
32 int serverPort = -1;
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
33 boolean enableTrusted = false;
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
34 File tempDir = null;
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
35 private final HashSet<String> preparedButNotRun = new HashSet<>();
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
36
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
37 public TLSTester(String[] args) {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
38 for (int i = 0; i < args.length; i++) {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
39 String arg = args[i];
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
40 if (arg.equals("-v")) {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
41 verbose = 1;
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
42 } else if (arg.equals("-a")) {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
43 altHost = args[++i];
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
44 } else if (arg.equals("-t")) {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
45 enableTrusted = true;
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
46 } else if (!arg.startsWith("-") && serverHost == null) {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
47 int idx = arg.indexOf(':');
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
48 if (idx > 0) {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
49 serverHost = arg.substring(0, idx);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
50 try {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
51 serverPort = Integer.parseInt(arg.substring(idx + 1));
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
52 if (serverPort > 0 && serverPort < 65536)
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
53 continue;
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
54 } catch (NumberFormatException ignored) {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
55 }
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
56 }
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
57 // if we get here it wasn't very valid
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
58 throw new IllegalArgumentException("Invalid argument: " + arg);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
59 } else {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
60 throw new IllegalArgumentException("Unexpected argument: " + arg);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
61 }
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
62 }
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
63 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
64
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
65 public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
66 Class.forName("org.monetdb.jdbc.MonetDriver");
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
67 TLSTester main = new TLSTester(args);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
68 main.run();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
69 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
70
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
71 private HashMap<String, Integer> loadPortMap(String testName) throws IOException {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
72 HashMap<String, Integer> portMap = new HashMap<>();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
73 InputStream in = fetchData("/?test=" + testName);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
74 BufferedReader br = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
75 for (String line = br.readLine(); line != null; line = br.readLine()) {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
76 int idx = line.indexOf(':');
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
77 String service = line.substring(0, idx);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
78 int port;
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
79 try {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
80 port = Integer.parseInt(line.substring(idx + 1));
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
81 } catch (NumberFormatException e) {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
82 throw new RuntimeException("Invalid port map line: " + line);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
83 }
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
84 portMap.put(service, port);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
85 }
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
86 return portMap;
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
87 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
88
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
89 private File resource(String resource) throws IOException {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
90 if (!fileCache.containsKey(resource))
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
91 fetchResource(resource);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
92 return fileCache.get(resource);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
93 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
94
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
95 private void fetchResource(String resource) throws IOException {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
96 if (!resource.startsWith("/")) {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
97 throw new IllegalArgumentException("Resource must start with slash: " + resource);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
98 }
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
99 if (tempDir == null) {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
100 tempDir = Files.createTempDirectory("tlstest").toFile();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
101 tempDir.deleteOnExit();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
102 }
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
103 File outPath = new File(tempDir, resource.substring(1));
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
104 try (InputStream in = fetchData(resource); FileOutputStream out = new FileOutputStream(outPath)) {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
105 byte[] buffer = new byte[12];
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
106 while (true) {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
107 int n = in.read(buffer);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
108 if (n <= 0)
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
109 break;
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
110 out.write(buffer, 0, n);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
111 }
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
112 }
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
113 fileCache.put(resource, outPath);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
114 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
115
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
116 private byte[] fetchBytes(String resource) throws IOException {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
117 ByteArrayOutputStream out = new ByteArrayOutputStream();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
118 try (InputStream in = fetchData(resource)) {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
119 byte[] buffer = new byte[22];
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
120 while (true) {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
121 int nread = in.read(buffer);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
122 if (nread <= 0)
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
123 break;
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
124 out.write(buffer, 0, nread);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
125 }
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
126 return out.toByteArray();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
127 }
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
128 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
129
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
130 private InputStream fetchData(String resource) throws IOException {
859
68994abe3e03 Also catch java.net.URISyntaxException and java.net.MalformedURLException and convert them to an IOException.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 858
diff changeset
131 URL url;
68994abe3e03 Also catch java.net.URISyntaxException and java.net.MalformedURLException and convert them to an IOException.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 858
diff changeset
132 try {
68994abe3e03 Also catch java.net.URISyntaxException and java.net.MalformedURLException and convert them to an IOException.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 858
diff changeset
133 // Note: as of Java version 20 java.net.URL(String) constructor is deprecated.
68994abe3e03 Also catch java.net.URISyntaxException and java.net.MalformedURLException and convert them to an IOException.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 858
diff changeset
134 // https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/net/URL.html#%3Cinit%3E(java.lang.String)
68994abe3e03 Also catch java.net.URISyntaxException and java.net.MalformedURLException and convert them to an IOException.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 858
diff changeset
135 url = new java.net.URI("http://" + serverHost + ":" + serverPort + resource).toURL();
68994abe3e03 Also catch java.net.URISyntaxException and java.net.MalformedURLException and convert them to an IOException.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 858
diff changeset
136 } catch (java.net.URISyntaxException | java.net.MalformedURLException e) {
68994abe3e03 Also catch java.net.URISyntaxException and java.net.MalformedURLException and convert them to an IOException.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 858
diff changeset
137 throw new IOException(e.getMessage());
68994abe3e03 Also catch java.net.URISyntaxException and java.net.MalformedURLException and convert them to an IOException.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 858
diff changeset
138 }
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
139 URLConnection conn = url.openConnection();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
140 conn.connect();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
141 return conn.getInputStream();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
142 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
143
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
144 private void run() throws IOException, SQLException {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
145 test_connect_plain();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
146 test_connect_tls();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
147 test_refuse_no_cert();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
148 test_refuse_wrong_cert();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
149 test_refuse_wrong_host();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
150 test_refuse_tlsv12();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
151 test_refuse_expired();
855
839d76eeb4ae Added missing Copyright header comments
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 851
diff changeset
152 // test_connect_client_auth1();
839d76eeb4ae Added missing Copyright header comments
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 851
diff changeset
153 // test_connect_client_auth2();
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
154 test_fail_tls_to_plain();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
155 test_fail_plain_to_tls();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
156 test_connect_server_name();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
157 test_connect_alpn_mapi9();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
158 test_connect_trusted();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
159 test_refuse_trusted_wrong_host();
805
2fee4b71baac Set ALPN protocol if the runtime supports it
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 804
diff changeset
160
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
161 // did we forget to call expectSucceed and expectFailure somewhere?
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
162 if (!preparedButNotRun.isEmpty()) {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
163 String names = String.join(", ", preparedButNotRun);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
164 throw new RuntimeException("Not all tests called expectSuccess/expectFailure: " + names);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
165 }
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
166 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
167
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
168 private void test_connect_plain() throws IOException, SQLException {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
169 attempt("connect_plain", "plain").with(Parameter.TLS, false).expectSuccess();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
170 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
171
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
172 private void test_connect_tls() throws IOException, SQLException {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
173 Attempt attempt = attempt("connect_tls", "server1");
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
174 attempt.withFile(Parameter.CERT, "/ca1.crt").expectSuccess();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
175 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
176
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
177 private void test_refuse_no_cert() throws IOException, SQLException {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
178 attempt("refuse_no_cert", "server1").expectFailure("PKIX path building failed");
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
179 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
180
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
181 private void test_refuse_wrong_cert() throws IOException, SQLException {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
182 Attempt attempt = attempt("refuse_wrong_cert", "server1");
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
183 attempt.withFile(Parameter.CERT, "/ca2.crt").expectFailure("PKIX path building failed");
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
184 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
185
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
186 private void test_refuse_wrong_host() throws IOException, SQLException {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
187 Attempt attempt = attempt("refuse_wrong_host", "server1").with(Parameter.HOST, altHost);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
188 attempt.withFile(Parameter.CERT, "/ca1.crt").expectFailure("No subject alternative DNS name");
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
189 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
190
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
191 private void test_refuse_tlsv12() throws IOException, SQLException {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
192 Attempt attempt = attempt("refuse_tlsv12", "tls12");
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
193 attempt.withFile(Parameter.CERT, "/ca1.crt").expectFailure("protocol_version");
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
194 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
195
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
196 private void test_refuse_expired() throws IOException, SQLException {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
197 Attempt attempt = attempt("refuse_expired", "expiredcert");
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
198 attempt.withFile(Parameter.CERT, "/ca1.crt").expectFailure("PKIX path validation failed");
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
199 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
200
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
201 private void test_connect_client_auth1() throws IOException, SQLException {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
202 attempt("connect_client_auth1", "clientauth")
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
203 .withFile(Parameter.CERT, "/ca1.crt")
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
204 .withFile(Parameter.CLIENTKEY, "/client2.keycrt")
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
205 .expectSuccess();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
206 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
207
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
208 private void test_connect_client_auth2() throws IOException, SQLException {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
209 attempt("connect_client_auth2", "clientauth")
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
210 .withFile(Parameter.CERT, "/ca1.crt")
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
211 .withFile(Parameter.CLIENTKEY, "/client2.key")
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
212 .withFile(Parameter.CLIENTCERT, "/client2.crt")
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
213 .expectSuccess();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
214 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
215
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
216 private void test_fail_tls_to_plain() throws IOException, SQLException {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
217 Attempt attempt = attempt("fail_tls_to_plain", "plain");
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
218 attempt.withFile(Parameter.CERT, "/ca1.crt").expectFailure("");
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
219
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
220 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
221
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
222 private void test_fail_plain_to_tls() throws IOException, SQLException {
851
157dcb2d6516 relax wording of expected exception
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 834
diff changeset
223 attempt("fail_plain_to_tls", "server1").with(Parameter.TLS, false).expectFailure("Cannot connect", "Could not connect");
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
224 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
225
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
226 private void test_connect_server_name() throws IOException, SQLException {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
227 Attempt attempt = attempt("connect_server_name", "sni");
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
228 attempt.withFile(Parameter.CERT, "/ca1.crt").expectSuccess();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
229 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
230
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
231 private void test_connect_alpn_mapi9() throws IOException, SQLException {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
232 attempt("connect_alpn_mapi9", "alpn_mapi9").withFile(Parameter.CERT, "/ca1.crt").expectSuccess();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
233 }
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
234
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
235 private void test_connect_trusted() throws IOException, SQLException {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
236 attempt("connect_trusted", null)
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
237 .with(Parameter.HOST, "monetdb.ergates.nl")
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
238 .with(Parameter.PORT, 50000)
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
239 .expectSuccess();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
240 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
241
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
242 private void test_refuse_trusted_wrong_host() throws IOException, SQLException {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
243 attempt("test_refuse_trusted_wrong_host", null)
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
244 .with(Parameter.HOST, "monetdbxyz.ergates.nl")
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
245 .with(Parameter.PORT, 50000)
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
246 .expectFailure("No subject alternative DNS name");
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
247 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
248
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
249 private Attempt attempt(String testName, String portName) throws IOException {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
250 preparedButNotRun.add(testName);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
251 return new Attempt(testName, portName);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
252 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
253
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
254 private class Attempt {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
255 private final String testName;
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
256 private final Properties props = new Properties();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
257 boolean disabled = false;
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
258
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
259 public Attempt(String testName, String portName) throws IOException {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
260 HashMap<String, Integer> portMap = loadPortMap(testName);
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
261
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
262 this.testName = testName;
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
263 with(Parameter.TLS, true);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
264 with(Parameter.HOST, serverHost);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
265 with(Parameter.SO_TIMEOUT, 3000);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
266 if (portName != null) {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
267 Integer port = portMap.get(portName);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
268 if (port != null) {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
269 with(Parameter.PORT, port);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
270 } else {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
271 throw new RuntimeException("Unknown port name: " + portName);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
272 }
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
273 }
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
274 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
275
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
276 private Attempt with(Parameter parm, String value) {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
277 props.setProperty(parm.name, value);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
278 return this;
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
279 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
280
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
281 private Attempt with(Parameter parm, int value) {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
282 props.setProperty(parm.name, Integer.toString(value));
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
283 return this;
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
284 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
285
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
286 private Attempt with(Parameter parm, boolean value) {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
287 props.setProperty(parm.name, value ? "true" : "false");
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
288 return this;
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
289 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
290
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
291 private Attempt withFile(Parameter parm, String certResource) throws IOException {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
292 File certFile = resource(certResource);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
293 String path = certFile.getPath();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
294 with(parm, path);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
295 return this;
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
296 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
297
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
298 public void expectSuccess() throws SQLException {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
299 preparedButNotRun.remove(testName);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
300 if (disabled)
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
301 return;
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
302 try {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
303 Connection conn = DriverManager.getConnection("jdbc:monetdb:", props);
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
304 conn.close();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
305 } catch (SQLException e) {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
306 if (e.getMessage().startsWith("Sorry, this is not a real MonetDB instance")) {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
307 // it looks like a failure but this is actually our success scenario
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
308 // because this is what the TLS Tester does when the connection succeeds.
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
309 return;
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
310 }
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
311 // other exceptions ARE errors and should be reported.
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
312 throw e;
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
313 }
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
314 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
315
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
316 public void expectFailure(String... expectedMessages) throws SQLException {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
317 if (disabled)
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
318 return;
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
319 try {
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
320 expectSuccess();
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
321 throw new RuntimeException("Expected test " + testName + " to throw an exception but it didn't");
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
322 } catch (SQLException e) {
855
839d76eeb4ae Added missing Copyright header comments
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 851
diff changeset
323 for (String expected : expectedMessages) {
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
324 if (e.getMessage().contains(expected))
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
325 return;
855
839d76eeb4ae Added missing Copyright header comments
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 851
diff changeset
326 }
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
327 String message = "Test " + testName + " threw the wrong exception: " + e.getMessage() + '\n' + "Expected:\n <" + String.join(">\n <", expectedMessages) + ">";
851
157dcb2d6516 relax wording of expected exception
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 834
diff changeset
328 throw new RuntimeException(message, e);
834
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
329 }
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
330 }
5aa19bbed0d6 Comments and formatting
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 805
diff changeset
331 }
802
5d04490bc58b Add tests using monetdb-tlstester
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
332 }