Skip to content

Commit

Permalink
Merge pull request #152 from auth0/protocol-fix
Browse files Browse the repository at this point in the history
Disable HTTP 2 protocol on OkHttp client
  • Loading branch information
lbalmaceda authored Mar 19, 2018
2 parents eefbfba + 54c5d7c commit e82f4de
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import com.squareup.okhttp.ConnectionSpec;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Protocol;
import com.squareup.okhttp.TlsVersion;
import com.squareup.okhttp.logging.HttpLoggingInterceptor;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.net.ssl.SSLContext;
Expand All @@ -29,8 +31,9 @@ public class OkHttpClientFactory {
/**
* This method creates an instance of OKHttpClient according to the provided parameters.
* It is used internally and is not intended to be used directly.
*
* @param loggingEnabled Enable logging in the created OkHttpClient.
* @param tls12Enforced Enforce TLS 1.2 in the created OkHttpClient on devices with API 16-21
* @param tls12Enforced Enforce TLS 1.2 in the created OkHttpClient on devices with API 16-21
* @return new OkHttpClient instance created according to the parameters.
*/
public OkHttpClient createClient(boolean loggingEnabled, boolean tls12Enforced) {
Expand All @@ -45,6 +48,7 @@ OkHttpClient modifyClient(OkHttpClient client, boolean loggingEnabled, boolean t
if (tls12Enforced) {
enforceTls12(client);
}
client.setProtocols(Arrays.asList(Protocol.HTTP_1_1, Protocol.SPDY_3));
return client;
}

Expand All @@ -56,6 +60,7 @@ private void enableLogging(OkHttpClient client) {

/**
* Enable TLS 1.2 on the OkHttpClient on API 16-21, which is supported but not enabled by default.
*
* @link https://github.com/square/okhttp/issues/2372
* @see TLS12SocketFactory
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.auth0.android.request.internal;

import android.app.Activity;

import com.squareup.okhttp.ConnectionSpec;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Protocol;
import com.squareup.okhttp.TlsVersion;
import com.squareup.okhttp.logging.HttpLoggingInterceptor;

Expand All @@ -14,7 +13,6 @@
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

Expand All @@ -37,25 +35,34 @@
@Config(constants = com.auth0.android.auth0.BuildConfig.class, sdk = 21, manifest = Config.NONE)
public class OkHttpClientFactoryTest {

private Activity activity;
private OkHttpClientFactory factory;
@Mock private OkHttpClient mockClient;
@Mock
private OkHttpClient mockClient;

@Before
public void setUp(){
public void setUp() {
MockitoAnnotations.initMocks(this);
factory = new OkHttpClientFactory();
activity = Robolectric.setupActivity(Activity.class);
}

@Test
// Verify that there's no error when creating a new OkHttpClient instance
public void shouldCreateNewClient(){
public void shouldCreateNewClient() {
factory.createClient(false, false);
}

@Test
@Config(sdk=21)
public void shouldNotUseHttp2Protocol() {
OkHttpClient client = factory.createClient(false, false);
//Doesn't use default protocols
assertThat(client.getProtocols(), is(notNullValue()));
assertThat(client.getProtocols().contains(Protocol.HTTP_1_1), is(true));
assertThat(client.getProtocols().contains(Protocol.SPDY_3), is(true));
assertThat(client.getProtocols().contains(Protocol.HTTP_2), is(false));
}

@Test
@Config(sdk = 21)
public void shouldEnableLoggingTLS12Enforced() {
List list = generateInterceptorsMockList(mockClient);
OkHttpClient client = factory.modifyClient(mockClient, true, true);
Expand All @@ -64,34 +71,34 @@ public void shouldEnableLoggingTLS12Enforced() {
}

@Test
@Config(sdk=21)
public void shouldEnableLoggingTLS12NotEnforced(){
@Config(sdk = 21)
public void shouldEnableLoggingTLS12NotEnforced() {
List list = generateInterceptorsMockList(mockClient);
OkHttpClient client = factory.modifyClient(mockClient, true, false);
verifyLoggingEnabled(client, list);
verifyTLS12NotEnforced(client);
}

@Test
@Config(sdk=21)
public void shouldDisableLoggingTLS12Enforced(){
@Config(sdk = 21)
public void shouldDisableLoggingTLS12Enforced() {
List list = generateInterceptorsMockList(mockClient);
OkHttpClient client = factory.modifyClient(mockClient, false, true);
verifyLoggingDisabled(client, list);
verifyTLS12Enforced(client);
}

@Test
@Config(sdk=21)
public void shouldDisableLoggingTLS12NotEnforced(){
@Config(sdk = 21)
public void shouldDisableLoggingTLS12NotEnforced() {
List list = generateInterceptorsMockList(mockClient);
OkHttpClient client = factory.modifyClient(mockClient, false, false);
verifyLoggingDisabled(client, list);
verifyTLS12NotEnforced(client);
}

@Test
@Config(sdk=22)
@Config(sdk = 22)
public void shouldEnableLoggingTLS12Enforced_postLollipopTLS12NoEffect() {
List list = generateInterceptorsMockList(mockClient);
OkHttpClient client = factory.modifyClient(mockClient, true, true);
Expand All @@ -100,26 +107,26 @@ public void shouldEnableLoggingTLS12Enforced_postLollipopTLS12NoEffect() {
}

@Test
@Config(sdk=22)
public void shouldEnableLoggingTLS12NotEnforced_posLollipop(){
@Config(sdk = 22)
public void shouldEnableLoggingTLS12NotEnforced_posLollipop() {
List list = generateInterceptorsMockList(mockClient);
OkHttpClient client = factory.modifyClient(mockClient, true, false);
verifyLoggingEnabled(client, list);
verifyTLS12NotEnforced(client);
}

@Test
@Config(sdk=22)
public void shouldDisableLoggingTLS12Enforced_postLollipopTLS12NoEffect(){
@Config(sdk = 22)
public void shouldDisableLoggingTLS12Enforced_postLollipopTLS12NoEffect() {
List list = generateInterceptorsMockList(mockClient);
OkHttpClient client = factory.modifyClient(mockClient, false, true);
verifyLoggingDisabled(client, list);
verifyTLS12NotEnforced(client);
}

@Test
@Config(sdk=22)
public void shouldDisableLoggingTLS12NotEnforced_postLollipop(){
@Config(sdk = 22)
public void shouldDisableLoggingTLS12NotEnforced_postLollipop() {
List list = generateInterceptorsMockList(mockClient);
OkHttpClient client = factory.modifyClient(mockClient, false, false);
verifyLoggingDisabled(client, list);
Expand Down

0 comments on commit e82f4de

Please sign in to comment.