From 2790990866b2171b3762ba499657824d8a314012 Mon Sep 17 00:00:00 2001 From: Sudheer Shanka Date: Mon, 11 Jul 2022 18:07:47 +0000 Subject: [PATCH] Clear DNS cache after every network access. If the app tries to make a network connection in the foreground immediately after trying to do the same when it's network access was blocked, it could receive a UnknownHostException due to the cached DNS entry. So, clear the dns cache after every network access for now until we have a fix on the platform side. Bug: 237549250 Bug: 238064333 Bug: 237556596 Bug: 238962235 Bug: 238833668 Bug: 239262832 Test: atest ./tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java Ignore-AOSP-First: Will be cherrypicked to aosp later Change-Id: Ic603b1bd9da24ee3724054c4a91794ee569e97f6 --- tests/cts/hostside/app2/Android.bp | 2 +- .../android/cts/net/hostside/app2/MyBroadcastReceiver.java | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/cts/hostside/app2/Android.bp b/tests/cts/hostside/app2/Android.bp index edfaf9fdd3..db92f5c15a 100644 --- a/tests/cts/hostside/app2/Android.bp +++ b/tests/cts/hostside/app2/Android.bp @@ -21,7 +21,7 @@ package { android_test_helper_app { name: "CtsHostsideNetworkTestsApp2", defaults: ["cts_support_defaults"], - sdk_version: "test_current", + platform_apis: true, static_libs: [ "androidx.annotation_annotation", "CtsHostsideNetworkTestsAidl", diff --git a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyBroadcastReceiver.java b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyBroadcastReceiver.java index 771b404705..825f2c9019 100644 --- a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyBroadcastReceiver.java +++ b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyBroadcastReceiver.java @@ -48,6 +48,7 @@ import android.util.Log; import android.widget.Toast; import java.net.HttpURLConnection; +import java.net.InetAddress; import java.net.URL; /** @@ -182,6 +183,11 @@ public class MyBroadcastReceiver extends BroadcastReceiver { checkStatus = false; checkDetails = "Exception getting " + address + ": " + e; } + // If the app tries to make a network connection in the foreground immediately after + // trying to do the same when it's network access was blocked, it could receive a + // UnknownHostException due to the cached DNS entry. So, clear the dns cache after + // every network access for now until we have a fix on the platform side. + InetAddress.clearDnsCache(); Log.d(TAG, checkDetails); final String state, detailedState; if (networkInfo != null) {