Search in sources :

Example 36 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project robovm by robovm.

the class URLConnectionTest method testInspectSslAfterConnect.

/**
     * Test that we can inspect the SSL session after connect().
     * http://code.google.com/p/android/issues/detail?id=24431
     */
public void testInspectSslAfterConnect() throws Exception {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    server.enqueue(new MockResponse());
    server.play();
    HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    connection.connect();
    assertNotNull(connection.getHostnameVerifier());
    assertNull(connection.getLocalCertificates());
    assertNotNull(connection.getServerCertificates());
    assertNotNull(connection.getCipherSuite());
    assertNotNull(connection.getPeerPrincipal());
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 37 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project robovm by robovm.

the class URLConnectionTest method testConnectViaHttpProxyToHttps.

/**
     * We were verifying the wrong hostname when connecting to an HTTPS site
     * through a proxy. http://b/3097277
     */
private void testConnectViaHttpProxyToHttps(ProxyConfig proxyConfig) throws Exception {
    TestSSLContext testSSLContext = TestSSLContext.create();
    RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), true);
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
    server.enqueue(new MockResponse().setBody("this response comes via a secure proxy"));
    server.play();
    URL url = new URL("https://android.com/foo");
    HttpsURLConnection connection = (HttpsURLConnection) proxyConfig.connect(server, url);
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    connection.setHostnameVerifier(hostnameVerifier);
    assertContent("this response comes via a secure proxy", connection);
    RecordedRequest connect = server.takeRequest();
    assertEquals("Connect line failure on proxy", "CONNECT android.com:443 HTTP/1.1", connect.getRequestLine());
    assertContains(connect.getHeaders(), "Host: android.com");
    RecordedRequest get = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", get.getRequestLine());
    assertContains(get.getHeaders(), "Host: android.com");
    assertEquals(Arrays.asList("verify android.com"), hostnameVerifier.calls);
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 38 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project robovm by robovm.

the class URLConnectionTest method testConnectViaHttpsWithSSLFallback.

public void testConnectViaHttpsWithSSLFallback() throws IOException, InterruptedException {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    server.enqueue(new MockResponse().setSocketPolicy(DISCONNECT_AT_START));
    server.enqueue(new MockResponse().setBody("this response comes via SSL"));
    server.play();
    HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/foo").openConnection();
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    assertContent("this response comes via SSL", connection);
    // The first request will be an incomplete (bookkeeping) request
    // that the server disconnected from at start.
    server.takeRequest();
    // The request will be retried.
    RecordedRequest request = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 39 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project robovm by robovm.

the class URLConnectionTest method testSslFallback.

public void testSslFallback() throws Exception {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.FAIL_HANDSHAKE));
    server.enqueue(new MockResponse().setBody("This required a 2nd handshake"));
    server.play();
    HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    assertEquals("This required a 2nd handshake", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
    RecordedRequest first = server.takeRequest();
    assertEquals(0, first.getSequenceNumber());
    RecordedRequest retry = server.takeRequest();
    assertEquals(0, retry.getSequenceNumber());
    assertEquals("SSLv3", retry.getSslProtocol());
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 40 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project robovm by robovm.

the class URLConnectionTest method testConnectViaHttpProxyToHttpsUsingBadProxyAndHttpResponseCache.

/**
     * Tolerate bad https proxy response when using HttpResponseCache. http://b/6754912
     */
public void testConnectViaHttpProxyToHttpsUsingBadProxyAndHttpResponseCache() throws Exception {
    ProxyConfig proxyConfig = ProxyConfig.PROXY_SYSTEM_PROPERTY;
    TestSSLContext testSSLContext = TestSSLContext.create();
    initResponseCache();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), true);
    MockResponse badProxyResponse = new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders().setBody(// Key to reproducing b/6754912
    "bogus proxy connect response content");
    // We enqueue the bad response twice because the connection will
    // be retried with TLS_MODE_COMPATIBLE after the first connection
    // fails.
    server.enqueue(badProxyResponse);
    server.enqueue(badProxyResponse);
    server.play();
    URL url = new URL("https://android.com/foo");
    HttpsURLConnection connection = (HttpsURLConnection) proxyConfig.connect(server, url);
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    try {
        connection.connect();
        fail();
    } catch (SSLHandshakeException expected) {
    // Thrown when the connect causes SSLSocket.startHandshake() to throw
    // when it sees the "bogus proxy connect response content"
    // instead of a ServerHello handshake message.
    }
    RecordedRequest connect = server.takeRequest();
    assertEquals("Connect line failure on proxy", "CONNECT android.com:443 HTTP/1.1", connect.getRequestLine());
    assertContains(connect.getHeaders(), "Host: android.com");
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) SSLHandshakeException(javax.net.ssl.SSLHandshakeException)

Aggregations

HttpsURLConnection (javax.net.ssl.HttpsURLConnection)209 URL (java.net.URL)113 HttpURLConnection (java.net.HttpURLConnection)51 IOException (java.io.IOException)50 Test (org.junit.Test)39 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)29 InputStream (java.io.InputStream)27 HostnameVerifier (javax.net.ssl.HostnameVerifier)23 SSLContext (javax.net.ssl.SSLContext)23 OutputStream (java.io.OutputStream)20 MockResponse (com.google.mockwebserver.MockResponse)19 InputStreamReader (java.io.InputStreamReader)19 TestSSLContext (libcore.javax.net.ssl.TestSSLContext)19 URLConnection (java.net.URLConnection)17 BufferedReader (java.io.BufferedReader)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)14 MalformedURLException (java.net.MalformedURLException)13 Proxy (java.net.Proxy)13 MockResponse (okhttp3.mockwebserver.MockResponse)13