Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OkHttpClient.Builder network pinning on Android #8376

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions android-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
val androidBuild = property("androidBuild").toString().toBoolean()

android {
compileSdk = 34
compileSdk = 35

namespace = "okhttp.android.test"

Expand All @@ -35,16 +35,16 @@ android {
}

compileOptions {
targetCompatibility(JavaVersion.VERSION_11)
sourceCompatibility(JavaVersion.VERSION_11)
targetCompatibility(JavaVersion.VERSION_17)
sourceCompatibility(JavaVersion.VERSION_17)
}

testOptions {
targetSdk = 34
targetSdk = 35
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_11.toString()
jvmTarget = JavaVersion.VERSION_17.toString()
}

// issue merging due to conflict with httpclient and something else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*
*/
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")

package okhttp.android.test

import android.content.Context
Expand All @@ -32,11 +34,13 @@ import java.net.UnknownHostException
import java.util.concurrent.CountDownLatch
import mockwebserver3.MockResponse
import mockwebserver3.junit4.MockWebServerRule
import okhttp3.AsyncDns
import okhttp3.Dns
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.android.AndroidAsyncDns
import okhttp3.android.ANDROID
import okhttp3.android.AndroidDns
import okhttp3.android.internal.AsyncDns
import okhttp3.tls.HandshakeCertificates
import okhttp3.tls.HeldCertificate
import okio.IOException
Expand All @@ -50,7 +54,7 @@ import org.junit.Test
/**
* Run with "./gradlew :android-test:connectedCheck -PandroidBuild=true" and make sure ANDROID_SDK_ROOT is set.
*/
class AndroidAsyncDnsTest {
class AndroidDnsTest {
@JvmField @Rule
val serverRule = MockWebServerRule()
private lateinit var client: OkHttpClient
Expand All @@ -74,7 +78,7 @@ class AndroidAsyncDnsTest {

client =
OkHttpClient.Builder()
.dns(AsyncDns.toDns(AndroidAsyncDns.IPv4, AndroidAsyncDns.IPv6))
.dns(Dns.ANDROID)
.sslSocketFactory(localhost.sslSocketFactory(), localhost.trustManager)
.build()

Expand Down Expand Up @@ -131,25 +135,33 @@ class AndroidAsyncDnsTest {
val latch = CountDownLatch(1)

// assumes an IPv4 address
AndroidAsyncDns.IPv4.query(
hostname,
object : AsyncDns.Callback {
override fun onResponse(
hostname: String,
addresses: List<InetAddress>,
) {
allAddresses.addAll(addresses)
latch.countDown()
}

override fun onFailure(
hostname: String,
e: IOException,
) {
exception = e
latch.countDown()
}
},
AndroidDns(AndroidDns.DnsClass.IPV4).query(
hostname = hostname,
originatingCall = null,
callback =
object : AsyncDns.Callback {
override fun onAddresses(
hasMore: Boolean,
hostname: String,
addresses: List<InetAddress>,
) {
allAddresses.addAll(addresses)
if (!hasMore) {
latch.countDown()
}
}

override fun onFailure(
hasMore: Boolean,
hostname: String,
e: IOException,
) {
exception = e
if (!hasMore) {
latch.countDown()
}
}
},
)

latch.await()
Expand Down Expand Up @@ -188,7 +200,7 @@ class AndroidAsyncDnsTest {

val client =
OkHttpClient.Builder()
.dns(AsyncDns.toDns(AndroidAsyncDns.IPv4, AndroidAsyncDns.IPv6))
.dns(Dns.ANDROID)
.socketFactory(network.socketFactory)
.build()

Expand All @@ -200,11 +212,13 @@ class AndroidAsyncDnsTest {
}
}

private fun assumeNetwork() {
try {
InetAddress.getByName("www.google.com")
} catch (uhe: UnknownHostException) {
throw AssumptionViolatedException(uhe.message, uhe)
companion object {
fun assumeNetwork() {
try {
InetAddress.getByName("www.google.com")
} catch (uhe: UnknownHostException) {
throw AssumptionViolatedException(uhe.message, uhe)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
*/
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")

package okhttp.android.test
package okhttp3.android

import assertk.assertFailure
import assertk.assertions.hasMessage
import assertk.assertions.isInstanceOf
import java.net.UnknownHostException
import okhttp3.AsyncDns
import okhttp3.android.AndroidAsyncDns
import okhttp3.android.AndroidDns.DnsClass
import okhttp3.android.internal.BlockingAsyncDns.Companion.asBlocking
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
Expand All @@ -31,17 +31,17 @@ import org.robolectric.shadow.api.Shadow

@Config(shadows = [ShadowDnsResolver::class], sdk = [34])
@RunWith(RobolectricTestRunner::class)
class AndroidAsyncDnsTest {
class AndroidDnsTest {
@Test
fun testDnsRequestInvalid() {
val asyncDns = AndroidAsyncDns.IPv4
val asyncDns = AndroidDns(DnsClass.IPV4)
val shadowDns: ShadowDnsResolver = Shadow.extract(asyncDns.resolver)

shadowDns.responder = {
throw IllegalArgumentException("Network.fromNetworkHandle refusing to instantiate NETID_UNSET Network.")
}

val dns = AsyncDns.toDns(asyncDns)
val dns = asyncDns.asBlocking()

assertFailure {
dns.lookup("google.invalid")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (C) 2024 Block, 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.
*/
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")

package okhttp3.android

import assertk.assertThat
import assertk.assertions.isEqualTo
import assertk.assertions.isNotEqualTo
import okhttp3.OkHttpClient
import okhttp3.android.NetworkSelection.withNetwork
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.robolectric.shadows.ShadowNetwork

@Config(shadows = [ShadowDnsResolver::class], sdk = [34])
@RunWith(RobolectricTestRunner::class)
class AndroidNetworkSelectionTest {
val network1 = ShadowNetwork.newInstance(1)
val network2 = ShadowNetwork.newInstance(2)

@Test
fun testEquality() {
val defaultClient =
OkHttpClient.Builder()
.withNetwork(network = null)
.build()

val network1Client =
defaultClient.newBuilder()
.withNetwork(network = network1)
.build()

assertThat(network1Client.dns).isNotEqualTo(defaultClient.dns)
assertThat(network1Client.socketFactory).isNotEqualTo(defaultClient.socketFactory)

val network2Client =
network1Client.newBuilder()
.withNetwork(network = network2)
.build()

assertThat(network2Client.socketFactory).isNotEqualTo(network1Client.socketFactory)

val defaultClient2 =
network2Client.newBuilder()
.withNetwork(network = null)
.build()

assertThat(defaultClient2.socketFactory).isNotEqualTo(network2Client.socketFactory)
assertThat(defaultClient2.socketFactory).isEqualTo(defaultClient.socketFactory)

val network1Client2 =
network2Client.newBuilder()
.withNetwork(network = network1)
.build()

assertThat(network1Client2.socketFactory).isEqualTo(network1Client.socketFactory)
}

@Test
fun testNotTaggedOnDefault() {
val defaultClient =
OkHttpClient.Builder()
.withNetwork(network = null)
.build()

val network1Client =
defaultClient.newBuilder()
.withNetwork(network = network1)
.build()
}

@Test
fun testTaggedOnSpecific() {
val network1Client =
OkHttpClient.Builder()
.withNetwork(network = network1)
.build()
}
}
Loading
Loading