|
4 | 4 | import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
5 | 5 | import static com.github.tomakehurst.wiremock.client.WireMock.request; |
6 | 6 | import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; |
| 7 | +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; |
7 | 8 | import static org.junit.jupiter.api.Assertions.assertEquals; |
8 | 9 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
9 | 10 | import static org.junit.jupiter.api.Assertions.assertThrows; |
|
15 | 16 | import com.github.jamesnetherton.zulip.client.exception.ZulipClientException; |
16 | 17 | import com.github.jamesnetherton.zulip.client.exception.ZulipRateLimitExceededException; |
17 | 18 | import com.github.jamesnetherton.zulip.client.http.ZulipConfiguration; |
| 19 | +import com.github.tomakehurst.wiremock.WireMockServer; |
18 | 20 | import java.io.BufferedReader; |
| 21 | +import java.io.File; |
| 22 | +import java.io.FileWriter; |
19 | 23 | import java.io.IOException; |
20 | 24 | import java.io.InputStreamReader; |
21 | 25 | import java.io.OutputStream; |
22 | 26 | import java.net.ServerSocket; |
23 | 27 | import java.net.Socket; |
24 | 28 | import java.net.URL; |
25 | 29 | import java.nio.charset.StandardCharsets; |
| 30 | +import java.security.SecureRandom; |
| 31 | +import java.security.cert.Certificate; |
| 32 | +import java.security.cert.X509Certificate; |
| 33 | +import java.util.Base64; |
26 | 34 | import java.util.Collections; |
27 | 35 | import java.util.List; |
28 | 36 | import java.util.concurrent.CountDownLatch; |
29 | 37 | import java.util.concurrent.ExecutorService; |
30 | 38 | import java.util.concurrent.Executors; |
31 | 39 | import java.util.concurrent.TimeUnit; |
| 40 | +import javax.net.ssl.SSLContext; |
| 41 | +import javax.net.ssl.SSLSocket; |
| 42 | +import javax.net.ssl.TrustManager; |
| 43 | +import javax.net.ssl.X509TrustManager; |
32 | 44 | import org.junit.jupiter.api.Test; |
33 | 45 |
|
34 | 46 | public class ZulipCommonsHttpClientTest extends ZulipApiTestBase { |
@@ -135,6 +147,86 @@ public void ignoredParameters() throws Exception { |
135 | 147 | } |
136 | 148 | } |
137 | 149 |
|
| 150 | + @Test |
| 151 | + public void certBundle() throws Exception { |
| 152 | + WireMockServer httpsServer = new WireMockServer(options().dynamicHttpsPort()); |
| 153 | + httpsServer.start(); |
| 154 | + |
| 155 | + try { |
| 156 | + X509Certificate cert = getServerCertificate("localhost", httpsServer.httpsPort()); |
| 157 | + File certFile = writeCertToPem(cert); |
| 158 | + |
| 159 | + httpsServer.stubFor(request("GET", urlPathEqualTo("/api/v1/messages")) |
| 160 | + .willReturn(aResponse() |
| 161 | + .withStatus(200) |
| 162 | + .withBody("{\"result\":\"success\",\"msg\":\"\"}"))); |
| 163 | + |
| 164 | + ZulipConfiguration configuration = new ZulipConfiguration( |
| 165 | + new URL("https://localhost:" + httpsServer.httpsPort()), "test@test.com", "abc123"); |
| 166 | + configuration.setCertBundle(certFile.getAbsolutePath()); |
| 167 | + |
| 168 | + ZulipCommonsHttpClient client = new ZulipCommonsHttpClient(configuration); |
| 169 | + ZulipApiResponse response = client.get("messages", Collections.emptyMap(), ZulipApiResponse.class); |
| 170 | + assertNotNull(response); |
| 171 | + } finally { |
| 172 | + httpsServer.stop(); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + public void certBundleWithUntrustedServer() throws Exception { |
| 178 | + WireMockServer httpsServer = new WireMockServer(options().dynamicHttpsPort()); |
| 179 | + httpsServer.start(); |
| 180 | + |
| 181 | + try { |
| 182 | + File emptyCertFile = File.createTempFile("empty-cert", ".pem"); |
| 183 | + emptyCertFile.deleteOnExit(); |
| 184 | + |
| 185 | + ZulipConfiguration configuration = new ZulipConfiguration( |
| 186 | + new URL("https://localhost:" + httpsServer.httpsPort()), "test@test.com", "abc123"); |
| 187 | + configuration.setCertBundle(emptyCertFile.getAbsolutePath()); |
| 188 | + |
| 189 | + ZulipCommonsHttpClient client = new ZulipCommonsHttpClient(configuration); |
| 190 | + assertThrows(ZulipClientException.class, () -> { |
| 191 | + client.get("messages", Collections.emptyMap(), ZulipApiResponse.class); |
| 192 | + }); |
| 193 | + } finally { |
| 194 | + httpsServer.stop(); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + private X509Certificate getServerCertificate(String host, int port) throws Exception { |
| 199 | + SSLContext sslContext = SSLContext.getInstance("TLS"); |
| 200 | + sslContext.init(null, new TrustManager[] { new X509TrustManager() { |
| 201 | + public X509Certificate[] getAcceptedIssuers() { |
| 202 | + return new X509Certificate[0]; |
| 203 | + } |
| 204 | + |
| 205 | + public void checkClientTrusted(X509Certificate[] certs, String authType) { |
| 206 | + } |
| 207 | + |
| 208 | + public void checkServerTrusted(X509Certificate[] certs, String authType) { |
| 209 | + } |
| 210 | + } }, new SecureRandom()); |
| 211 | + |
| 212 | + try (SSLSocket socket = (SSLSocket) sslContext.getSocketFactory().createSocket(host, port)) { |
| 213 | + socket.startHandshake(); |
| 214 | + Certificate[] certs = socket.getSession().getPeerCertificates(); |
| 215 | + return (X509Certificate) certs[0]; |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + private File writeCertToPem(X509Certificate cert) throws Exception { |
| 220 | + File certFile = File.createTempFile("test-cert", ".pem"); |
| 221 | + certFile.deleteOnExit(); |
| 222 | + try (FileWriter fw = new FileWriter(certFile)) { |
| 223 | + fw.write("-----BEGIN CERTIFICATE-----\n"); |
| 224 | + fw.write(Base64.getMimeEncoder(64, new byte[] { '\n' }).encodeToString(cert.getEncoded())); |
| 225 | + fw.write("\n-----END CERTIFICATE-----\n"); |
| 226 | + } |
| 227 | + return certFile; |
| 228 | + } |
| 229 | + |
138 | 230 | private class FakeServer { |
139 | 231 | private final ServerSocket serverSocket; |
140 | 232 | private final ExecutorService executor; |
|
0 commit comments