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