-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Android module and Async Dns #7157
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
45d6196
Async Dns and Android Module
yschimke b976b06
Async Dns and Android Module
yschimke 3e067a0
Async Dns and Android Module
yschimke 6a33e3d
Async Dns and Android Module
yschimke b2f8048
Async Dns and Android Module
yschimke 5c032a5
Async Dns and Android Module
yschimke 3fac14d
Add some debugging, and correct dnsclass
yschimke 8230f90
Fixes
yschimke 127afde
Fix failing CI tests.
yschimke 55d913f
Review comments
yschimke e3407ba
Fix API
yschimke 7b6e9b5
Merge branch 'master' into async_dns2
yschimke cec1c28
Review comments
yschimke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Module okhttp-android | ||
|
|
||
| OkHttp Android library. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| OkHttp Android | ||
| ============== | ||
|
|
||
| Enhanced APIs for using OkHttp on Android. | ||
|
|
||
| At the moment, this will mostly likely only be useful for fastFallback. | ||
|
|
||
| Download | ||
| -------- | ||
|
|
||
| ```kotlin | ||
| implementation("com.squareup.okhttp3:okhttp-android:4.9.3") | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| public final class okhttp3/android/AndroidAsyncDns : okhttp3/AsyncDns { | ||
| public static final field Companion Lokhttp3/android/AndroidAsyncDns$Companion; | ||
| public fun <init> (Lokhttp3/AsyncDns$DnsClass;Landroid/net/Network;)V | ||
| public synthetic fun <init> (Lokhttp3/AsyncDns$DnsClass;Landroid/net/Network;ILkotlin/jvm/internal/DefaultConstructorMarker;)V | ||
| public fun query (Ljava/lang/String;Lokhttp3/AsyncDns$Callback;)V | ||
| } | ||
|
|
||
| public final class okhttp3/android/AndroidAsyncDns$Companion { | ||
| public final fun getIPv4 ()Lokhttp3/android/AndroidAsyncDns; | ||
| public final fun getIPv6 ()Lokhttp3/android/AndroidAsyncDns; | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import com.vanniktech.maven.publish.JavadocJar | ||
|
|
||
| plugins { | ||
| id("com.android.library") | ||
| kotlin("android") | ||
| id("de.mannodermaus.android-junit5") | ||
| id("org.jetbrains.dokka") | ||
| id("com.vanniktech.maven.publish.base") | ||
| id("binary-compatibility-validator") | ||
| } | ||
|
|
||
| android { | ||
| compileSdk = 31 | ||
|
|
||
| defaultConfig { | ||
| minSdk = 21 | ||
| targetSdk = 31 | ||
|
|
||
| // Make sure to use the AndroidJUnitRunner (or a sub-class) in order to hook in the JUnit 5 Test Builder | ||
| testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" | ||
| testInstrumentationRunnerArguments += mapOf( | ||
| "runnerBuilder" to "de.mannodermaus.junit5.AndroidJUnit5Builder", | ||
| "notPackage" to "org.bouncycastle" | ||
| ) | ||
|
|
||
| buildFeatures { | ||
| buildConfig = false | ||
| } | ||
| } | ||
|
|
||
| compileOptions { | ||
| targetCompatibility(JavaVersion.VERSION_11) | ||
| sourceCompatibility(JavaVersion.VERSION_11) | ||
| } | ||
|
|
||
| kotlinOptions { | ||
| jvmTarget = JavaVersion.VERSION_11.toString() | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
| api(libs.squareup.okio) | ||
| api(projects.okhttp) | ||
| compileOnly(libs.androidx.annotation) | ||
| compileOnly(libs.findbugs.jsr305) | ||
| compileOnly(libs.animalsniffer.annotations) | ||
| compileOnly(libs.robolectric.android) | ||
|
|
||
| testImplementation(projects.okhttpTestingSupport) | ||
| testImplementation(projects.mockwebserver3Junit5) | ||
| testImplementation(libs.junit) | ||
| testImplementation(libs.assertj.core) | ||
|
|
||
| androidTestImplementation(projects.okhttpTls) | ||
| androidTestImplementation(libs.assertj.core) | ||
| androidTestImplementation(projects.mockwebserver3Junit5) | ||
| androidTestImplementation(libs.androidx.test.runner) | ||
| androidTestImplementation(libs.junit.jupiter.api) | ||
| androidTestImplementation(libs.junit5android.core) | ||
| androidTestRuntimeOnly(libs.junit5android.runner) | ||
| } | ||
|
|
||
| mavenPublishing { | ||
| @Suppress("DEPRECATION") // Requires AGP 7.1.x | ||
| configure(com.vanniktech.maven.publish.AndroidLibrary(javadocJar = JavadocJar.Dokka("dokkaGfm"))) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
| package="okhttp.android.test"> | ||
|
|
||
| <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | ||
|
|
||
| </manifest> |
188 changes: 188 additions & 0 deletions
188
okhttp-android/src/androidTest/kotlin/okhttp3/android/AndroidAsyncDnsTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| /* | ||
| * Copyright (c) 2022 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 okhttp3.android | ||
|
|
||
| import android.content.Context | ||
| import android.net.ConnectivityManager | ||
| import androidx.test.platform.app.InstrumentationRegistry | ||
| import java.net.InetAddress | ||
| import java.net.UnknownHostException | ||
| import java.util.concurrent.CountDownLatch | ||
| import mockwebserver3.MockResponse | ||
| import mockwebserver3.MockWebServer | ||
| import mockwebserver3.junit5.internal.MockWebServerExtension | ||
| import okhttp3.AsyncDns | ||
| import okhttp3.OkHttpClient | ||
| import okhttp3.Request | ||
| import okhttp3.tls.HandshakeCertificates | ||
| import okhttp3.tls.HeldCertificate | ||
| import okio.IOException | ||
| import org.assertj.core.api.Assertions.assertThat | ||
| import org.assertj.core.api.Assertions.fail | ||
| import org.junit.jupiter.api.BeforeEach | ||
| import org.junit.jupiter.api.Disabled | ||
| import org.junit.jupiter.api.Test | ||
| import org.junit.jupiter.api.extension.ExtendWith | ||
| import org.opentest4j.TestAbortedException | ||
|
|
||
| /** | ||
| * Run with "./gradlew :android-test:connectedCheck" and make sure ANDROID_SDK_ROOT is set. | ||
| */ | ||
| @ExtendWith(MockWebServerExtension::class) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yay! |
||
| class AndroidAsyncDnsTest(val server: MockWebServer) { | ||
| private val localhostName: String = InetAddress.getByName("localhost").canonicalHostName | ||
|
|
||
| private val localhost: HandshakeCertificates by lazy { | ||
| // Generate a self-signed cert for the server to serve and the client to trust. | ||
| val heldCertificate = HeldCertificate.Builder() | ||
| .commonName("localhost") | ||
| .addSubjectAlternativeName(localhostName) | ||
| .build() | ||
| return@lazy HandshakeCertificates.Builder() | ||
| .addPlatformTrustedCertificates() | ||
| .heldCertificate(heldCertificate) | ||
| .addTrustedCertificate(heldCertificate.certificate) | ||
| .build() | ||
| } | ||
|
|
||
| private val client = OkHttpClient.Builder() | ||
| .dns(AsyncDns.toDns(AndroidAsyncDns.IPv4, AndroidAsyncDns.IPv6)) | ||
| .sslSocketFactory(localhost.sslSocketFactory(), localhost.trustManager) | ||
| .build() | ||
|
|
||
| @BeforeEach | ||
| fun init() { | ||
| server.useHttps(localhost.sslSocketFactory(), false) | ||
| } | ||
|
|
||
| @Test | ||
| @Disabled("java.net.UnknownHostException: No results for localhost, in CI.") | ||
| fun testRequest() { | ||
| server.enqueue(MockResponse()) | ||
|
|
||
| val call = client.newCall(Request.Builder().url(server.url("/")).build()) | ||
|
|
||
| call.execute().use { response -> | ||
| assertThat(response.code).isEqualTo(200) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun testRequestExternal() { | ||
| assumeNetwork() | ||
|
|
||
| val call = client.newCall(Request.Builder().url("https://google.com/robots.txt").build()) | ||
|
|
||
| call.execute().use { response -> | ||
| assertThat(response.code).isEqualTo(200) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun testRequestInvalid() { | ||
| val call = client.newCall(Request.Builder().url("https://google.invalid/").build()) | ||
|
|
||
| try { | ||
| call.execute() | ||
| fail("Request can't succeed") | ||
| } catch (ioe: IOException) { | ||
| assertThat(ioe).hasMessage("No results for google.invalid") | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| @Disabled("No results on CI for localhost") | ||
| fun testDnsRequest() { | ||
| val (allAddresses, exception) = dnsQuery(localhostName) | ||
|
|
||
| assertThat(exception).isNull() | ||
| assertThat(allAddresses).isNotEmpty | ||
| } | ||
|
|
||
| private fun dnsQuery(hostname: String): Pair<List<InetAddress>, Exception?> { | ||
| val allAddresses = mutableListOf<InetAddress>() | ||
yschimke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var exception: Exception? = null | ||
| 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() | ||
| } | ||
| }) | ||
|
|
||
| latch.await() | ||
|
|
||
| return Pair(allAddresses, exception) | ||
| } | ||
|
|
||
| @Test | ||
| fun testDnsRequestExternal() { | ||
| assumeNetwork() | ||
|
|
||
| val (allAddresses, exception) = dnsQuery("google.com") | ||
|
|
||
| assertThat(exception).isNull() | ||
| assertThat(allAddresses).isNotEmpty | ||
| } | ||
|
|
||
| @Test | ||
| fun testDnsRequestInvalid() { | ||
| val (allAddresses, exception) = dnsQuery("google.invalid") | ||
|
|
||
| assertThat(exception).isNull() | ||
| assertThat(allAddresses).isEmpty() | ||
| } | ||
|
|
||
| @Test | ||
| fun testRequestOnNetwork() { | ||
| assumeNetwork() | ||
|
|
||
| val context = InstrumentationRegistry.getInstrumentation().context | ||
| val connectivityManager = | ||
| context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | ||
|
|
||
| val network = | ||
| connectivityManager.activeNetwork ?: throw TestAbortedException("No active network") | ||
|
|
||
| val client = OkHttpClient.Builder() | ||
| .dns(AsyncDns.toDns(AndroidAsyncDns.IPv4, AndroidAsyncDns.IPv6)) | ||
| .socketFactory(network.socketFactory) | ||
| .build() | ||
|
|
||
| val call = | ||
| client.newCall(Request.Builder().url("https://google.com/robots.txt").build()) | ||
|
|
||
| call.execute().use { response -> | ||
| assertThat(response.code).isEqualTo(200) | ||
| } | ||
| } | ||
|
|
||
| private fun assumeNetwork() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice |
||
| try { | ||
| InetAddress.getByName("www.google.com") | ||
| } catch (uhe: UnknownHostException) { | ||
| throw TestAbortedException(uhe.message, uhe) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
| package="okhttp.android.test"> | ||
|
|
||
| <uses-permission android:name="android.permission.INTERNET" /> | ||
|
|
||
| </manifest> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are not nice from java, but as these are Android specific, I think Kotlin is a good assumption.