|
18 | 18 | package io.vertx.pgclient;
|
19 | 19 |
|
20 | 20 | import io.vertx.core.Vertx;
|
| 21 | +import io.vertx.core.VertxOptions; |
| 22 | +import io.vertx.core.buffer.Buffer; |
| 23 | +import io.vertx.core.dns.AddressResolverOptions; |
21 | 24 | import io.vertx.core.net.PemTrustOptions;
|
22 | 25 | import io.vertx.ext.unit.Async;
|
23 | 26 | import io.vertx.ext.unit.TestContext;
|
@@ -94,6 +97,54 @@ public void testTLSInvalidCertificate(TestContext ctx) {
|
94 | 97 | }));
|
95 | 98 | }
|
96 | 99 |
|
| 100 | + @Test |
| 101 | + public void testTLSInvalidHostname(TestContext ctx) { |
| 102 | + Async async = ctx.async(); |
| 103 | + PgConnection.connect( |
| 104 | + vertx, |
| 105 | + rule.options() |
| 106 | + .setSslMode(SslMode.VERIFY_FULL) |
| 107 | + // The hostname in the test certificate is thebrain.ca, so 'localhost' should make for a failed connection |
| 108 | + .setHost("localhost") |
| 109 | + .setHostnameVerificationAlgorithm("HTTPS") |
| 110 | + .setPemTrustOptions( |
| 111 | + new PemTrustOptions() |
| 112 | + .addCertValue(vertx.fileSystem().readFileBlocking("tls/server.crt")) |
| 113 | + ), |
| 114 | + ctx.asyncAssertFailure(err -> { |
| 115 | + ctx.assertEquals(err.getMessage(), "SSL handshake failed"); |
| 116 | + async.complete(); |
| 117 | + })); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testTLSCorrectHostname(TestContext ctx) { |
| 122 | + Vertx vertxWithHosts = Vertx.vertx( |
| 123 | + new VertxOptions() |
| 124 | + .setAddressResolverOptions( |
| 125 | + new AddressResolverOptions() |
| 126 | + .setHostsValue(Buffer.buffer("127.0.0.1 thebrain.ca\n")) |
| 127 | + ) |
| 128 | + ); |
| 129 | + |
| 130 | + Async async = ctx.async(); |
| 131 | + PgConnection.connect( |
| 132 | + vertxWithHosts, |
| 133 | + rule.options() |
| 134 | + .setSslMode(SslMode.VERIFY_FULL) |
| 135 | + .setHost("thebrain.ca") |
| 136 | + .setHostnameVerificationAlgorithm("HTTPS") |
| 137 | + .setPemTrustOptions( |
| 138 | + new PemTrustOptions() |
| 139 | + .addCertValue(vertxWithHosts.fileSystem().readFileBlocking("tls/server.crt")) |
| 140 | + ), |
| 141 | + ctx.asyncAssertSuccess(conn -> { |
| 142 | + ctx.assertTrue(conn.isSSL()); |
| 143 | + vertxWithHosts.close(); |
| 144 | + async.complete(); |
| 145 | + })); |
| 146 | + } |
| 147 | + |
97 | 148 | @Test
|
98 | 149 | public void testSslModeDisable(TestContext ctx) {
|
99 | 150 | Async async = ctx.async();
|
|
0 commit comments