Search in sources :

Example 1 with SslClient

use of okhttp3.mockwebserver.internal.tls.SslClient in project okhttp by square.

the class ConnectionCoalescingTest method setUp.

@Before
public void setUp() throws Exception {
    rootCa = new HeldCertificate.Builder().serialNumber("1").ca(3).commonName("root").build();
    certificate = new HeldCertificate.Builder().issuedBy(rootCa).serialNumber("2").commonName(server.getHostName()).subjectAlternativeName(server.getHostName()).subjectAlternativeName("san.com").subjectAlternativeName("*.wildcard.com").subjectAlternativeName("differentdns.com").build();
    serverIps = Dns.SYSTEM.lookup(server.getHostName());
    dns.set(server.getHostName(), serverIps);
    dns.set("san.com", serverIps);
    dns.set("nonsan.com", serverIps);
    dns.set("www.wildcard.com", serverIps);
    dns.set("differentdns.com", Collections.<InetAddress>emptyList());
    SslClient sslClient = new SslClient.Builder().addTrustedCertificate(rootCa.certificate).build();
    client = new OkHttpClient.Builder().dns(dns).sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).build();
    SslClient serverSslClient = new SslClient.Builder().certificateChain(certificate, rootCa).build();
    server.useHttps(serverSslClient.socketFactory, false);
    url = server.url("/robots.txt");
}
Also used : SslClient(okhttp3.internal.tls.SslClient) HeldCertificate(okhttp3.internal.tls.HeldCertificate) Before(org.junit.Before)

Example 2 with SslClient

use of okhttp3.mockwebserver.internal.tls.SslClient in project okhttp by square.

the class ConnectionReuseTest method connectionsAreNotReusedIfSslSocketFactoryChanges.

@Test
public void connectionsAreNotReusedIfSslSocketFactoryChanges() throws Exception {
    enableHttps();
    server.enqueue(new MockResponse());
    server.enqueue(new MockResponse());
    Request request = new Request.Builder().url(server.url("/")).build();
    Response response = client.newCall(request).execute();
    response.body().close();
    // This client shares a connection pool but has a different SSL socket factory.
    SslClient sslClient2 = new SslClient.Builder().build();
    OkHttpClient anotherClient = client.newBuilder().sslSocketFactory(sslClient2.socketFactory, sslClient2.trustManager).build();
    // This client fails to connect because the new SSL socket factory refuses.
    try {
        anotherClient.newCall(request).execute();
        fail();
    } catch (SSLException expected) {
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) SslClient(okhttp3.internal.tls.SslClient) SSLException(javax.net.ssl.SSLException) Test(org.junit.Test)

Example 3 with SslClient

use of okhttp3.mockwebserver.internal.tls.SslClient in project okhttp by square.

the class OkHttpAsync method prepare.

@Override
public void prepare(final Benchmark benchmark) {
    concurrencyLevel = benchmark.concurrencyLevel;
    targetBacklog = benchmark.targetBacklog;
    client = new OkHttpClient.Builder().protocols(benchmark.protocols).dispatcher(new Dispatcher(new ThreadPoolExecutor(benchmark.concurrencyLevel, benchmark.concurrencyLevel, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()))).build();
    if (benchmark.tls) {
        SslClient sslClient = SslClient.localhost();
        SSLSocketFactory socketFactory = sslClient.socketFactory;
        HostnameVerifier hostnameVerifier = new HostnameVerifier() {

            @Override
            public boolean verify(String s, SSLSession session) {
                return true;
            }
        };
        client = client.newBuilder().sslSocketFactory(socketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build();
    }
    callback = new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            System.out.println("Failed: " + e);
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            ResponseBody body = response.body();
            long total = SynchronousHttpClient.readAllAndClose(body.byteStream());
            long finish = System.nanoTime();
            if (VERBOSE) {
                long start = (Long) response.request().tag();
                System.out.printf("Transferred % 8d bytes in %4d ms%n", total, TimeUnit.NANOSECONDS.toMillis(finish - start));
            }
            requestsInFlight.decrementAndGet();
        }
    };
}
Also used : Call(okhttp3.Call) SslClient(okhttp3.internal.tls.SslClient) SSLSession(javax.net.ssl.SSLSession) IOException(java.io.IOException) Dispatcher(okhttp3.Dispatcher) HostnameVerifier(javax.net.ssl.HostnameVerifier) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) Callback(okhttp3.Callback) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Example 4 with SslClient

use of okhttp3.mockwebserver.internal.tls.SslClient in project okhttp by square.

the class CallTest method httpsWithIpAddress.

@Test
public void httpsWithIpAddress() throws Exception {
    String localIpAddress = InetAddress.getLoopbackAddress().getHostAddress();
    // Create a certificate with an IP address in the subject alt name.
    HeldCertificate heldCertificate = new HeldCertificate.Builder().commonName("example.com").subjectAlternativeName(localIpAddress).build();
    SslClient sslClient = new SslClient.Builder().certificateChain(heldCertificate.keyPair, heldCertificate.certificate).addTrustedCertificate(heldCertificate.certificate).build();
    // Use that certificate on the server and trust it on the client.
    server.useHttps(sslClient.socketFactory, false);
    client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).protocols(Collections.singletonList(Protocol.HTTP_1_1)).build();
    // Make a request.
    server.enqueue(new MockResponse());
    HttpUrl url = server.url("/").newBuilder().host(localIpAddress).build();
    Request request = new Request.Builder().url(url).build();
    executeSynchronously(request).assertCode(200);
    // Confirm that the IP address was used in the host header.
    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals(localIpAddress + ":" + server.getPort(), recordedRequest.getHeader("Host"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) SslClient(okhttp3.internal.tls.SslClient) HeldCertificate(okhttp3.internal.tls.HeldCertificate) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 5 with SslClient

use of okhttp3.mockwebserver.internal.tls.SslClient in project okhttp by square.

the class OkUrlFactoryTest method testURLFilterRedirect.

@Test
public void testURLFilterRedirect() throws Exception {
    MockWebServer cleartextServer = new MockWebServer();
    cleartextServer.enqueue(new MockResponse().setBody("Blocked!"));
    final URL blockedURL = cleartextServer.url("/").url();
    SslClient contextBuilder = SslClient.localhost();
    server.useHttps(contextBuilder.socketFactory, false);
    factory.setClient(factory.client().newBuilder().sslSocketFactory(contextBuilder.socketFactory, contextBuilder.trustManager).followSslRedirects(true).build());
    factory.setUrlFilter(new URLFilter() {

        @Override
        public void checkURLPermitted(URL url) throws IOException {
            if (blockedURL.equals(url)) {
                throw new IOException("Blocked");
            }
        }
    });
    server.enqueue(new MockResponse().setResponseCode(302).addHeader("Location: " + blockedURL).setBody("This page has moved"));
    URL destination = server.url("/").url();
    try {
        HttpsURLConnection httpsConnection = (HttpsURLConnection) factory.open(destination);
        httpsConnection.getInputStream();
        fail("Connection was successful");
    } catch (IOException expected) {
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SslClient(okhttp3.internal.tls.SslClient) URLFilter(okhttp3.internal.URLFilter) MockWebServer(okhttp3.mockwebserver.MockWebServer) IOException(java.io.IOException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Aggregations

SslClient (okhttp3.internal.tls.SslClient)10 Test (org.junit.Test)6 HostnameVerifier (javax.net.ssl.HostnameVerifier)5 SSLSession (javax.net.ssl.SSLSession)5 OkHttpClient (okhttp3.OkHttpClient)4 MockResponse (okhttp3.mockwebserver.MockResponse)4 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)3 IOException (java.io.IOException)2 HttpCookie (java.net.HttpCookie)2 Callback (okhttp3.Callback)2 HttpUrl (okhttp3.HttpUrl)2 HeldCertificate (okhttp3.internal.tls.HeldCertificate)2 MockWebServer (okhttp3.mockwebserver.MockWebServer)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 SslClient (okhttp3.mockwebserver.internal.tls.SslClient)2 Context (android.content.Context)1 Bootstrap (io.netty.bootstrap.Bootstrap)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1