From 2bed25879f8eee43b23c4ad8a6ab6d45ff81d317 Mon Sep 17 00:00:00 2001 From: Yuri Schimke Date: Mon, 4 Dec 2023 06:48:05 +0000 Subject: [PATCH] Testing with ALPN Override (#8081) --- .../android/test/alpn/AlpnOverrideTest.kt | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 android-test/src/androidTest/java/okhttp/android/test/alpn/AlpnOverrideTest.kt diff --git a/android-test/src/androidTest/java/okhttp/android/test/alpn/AlpnOverrideTest.kt b/android-test/src/androidTest/java/okhttp/android/test/alpn/AlpnOverrideTest.kt new file mode 100644 index 000000000000..d4bdd36a5298 --- /dev/null +++ b/android-test/src/androidTest/java/okhttp/android/test/alpn/AlpnOverrideTest.kt @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2020 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package okhttp.android.test.alpn; + +import android.os.Build +import android.util.Log +import java.net.InetSocketAddress +import java.net.Proxy +import javax.net.ssl.SSLSocket +import javax.net.ssl.SSLSocketFactory +import okhttp3.* +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Tag +import org.junit.jupiter.api.Test + +/** + * Tests for ALPN overriding on Android. + */ +@Tag("Remote") +class AlpnOverrideTest { + + class CustomSSLSocketFactory( + delegate: SSLSocketFactory + ) : DelegatingSSLSocketFactory(delegate) { + override fun configureSocket(sslSocket: SSLSocket): SSLSocket { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + val parameters = sslSocket.sslParameters + Log.d("CustomSSLSocketFactory", "old applicationProtocols: $parameters.applicationProtocols") + parameters.applicationProtocols = arrayOf("x-amzn-http-ca") + sslSocket.sslParameters = parameters + } + + return sslSocket + } + } + + var client = OkHttpClient() + + @Test + fun getWithCustomSocketFactory() { + client = client.newBuilder() + .sslSocketFactory(CustomSSLSocketFactory(client.sslSocketFactory), client.x509TrustManager!!) + .connectionSpecs(listOf( + ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) + .supportsTlsExtensions(false) + .build() + )) + .eventListener(object : EventListener() { + override fun connectionAcquired(call: Call, connection: Connection) { + val sslSocket = connection.socket() as SSLSocket + println("Requested " + sslSocket.sslParameters.applicationProtocols.joinToString()) + println("Negotiated " + sslSocket.applicationProtocol) + } + }) + .build() + + val request = Request.Builder() + .url("https://www.google.com") + .build() + client.newCall(request).execute().use { response -> + assertThat(response.code).isEqualTo(200) + } + } +}