From 74cbfd5aab166fa05d040b432b3a57dd3c18093c Mon Sep 17 00:00:00 2001 From: Aswin Sankar Date: Thu, 2 Dec 2021 04:25:08 +0000 Subject: [PATCH 1/2] DnsResolver: Make DnsException ctor public Making the DnsException constructor public is useful for apps using DnsResolver to write their tests, and for internal packages that may want to implement the DnsResolver.Callback interface. Test: for regression; CTS tests in b/208479811 Bug: 208464882 Change-Id: I14641688f53721c96e6df9596a7506912ba3aec0 --- framework/api/current.txt | 1 + framework/src/android/net/DnsResolver.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/framework/api/current.txt b/framework/api/current.txt index 33f4d14148..9a77a3ced5 100644 --- a/framework/api/current.txt +++ b/framework/api/current.txt @@ -196,6 +196,7 @@ package android.net { } public static class DnsResolver.DnsException extends java.lang.Exception { + ctor public DnsResolver.DnsException(int, @Nullable Throwable); field public final int code; } diff --git a/framework/src/android/net/DnsResolver.java b/framework/src/android/net/DnsResolver.java index dac88ad907..164160f067 100644 --- a/framework/src/android/net/DnsResolver.java +++ b/framework/src/android/net/DnsResolver.java @@ -164,7 +164,7 @@ public final class DnsResolver { */ @DnsError public final int code; - DnsException(@DnsError int code, @Nullable Throwable cause) { + public DnsException(@DnsError int code, @Nullable Throwable cause) { super(cause); this.code = code; } From ca942be00d897ac2c46cd6267e1bf6c3100b46b4 Mon Sep 17 00:00:00 2001 From: Aswin Sankar Date: Thu, 2 Dec 2021 04:23:12 +0000 Subject: [PATCH 2/2] CTS DnsResolverTest for DnsException ctor - Adds a CTS test to verify that DnsException can be subclassed and its constructor re-used. Test: Adds testDnsExceptionConstructor() CTS test. Bug: 208479811 Change-Id: Ia1dffe8ad5252b61af5a6ef0f6630f075081a6d1 --- .../src/android/net/cts/DnsResolverTest.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/cts/net/src/android/net/cts/DnsResolverTest.java b/tests/cts/net/src/android/net/cts/DnsResolverTest.java index a0a1bab42e..634665f05d 100644 --- a/tests/cts/net/src/android/net/cts/DnsResolverTest.java +++ b/tests/cts/net/src/android/net/cts/DnsResolverTest.java @@ -43,6 +43,7 @@ import android.net.NetworkCapabilities; import android.net.NetworkRequest; import android.net.ParseException; import android.net.cts.util.CtsNetUtils; +import android.os.Build; import android.os.CancellationSignal; import android.os.Handler; import android.os.Looper; @@ -54,10 +55,13 @@ import androidx.test.InstrumentationRegistry; import androidx.test.runner.AndroidJUnit4; import com.android.net.module.util.DnsPacket; +import com.android.testutils.DevSdkIgnoreRule; +import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo; import com.android.testutils.SkipPresubmit; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -73,6 +77,9 @@ import java.util.concurrent.TimeUnit; @AppModeFull(reason = "WRITE_SECURE_SETTINGS permission can't be granted to instant apps") @RunWith(AndroidJUnit4.class) public class DnsResolverTest { + @Rule + public final DevSdkIgnoreRule ignoreRule = new DevSdkIgnoreRule(); + private static final String TAG = "DnsResolverTest"; private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' @@ -805,4 +812,19 @@ public class DnsResolverTest { } } } + + /** Verifies that DnsResolver.DnsException can be subclassed and its constructor re-used. */ + @Test @IgnoreUpTo(Build.VERSION_CODES.R) + public void testDnsExceptionConstructor() throws InterruptedException { + class TestDnsException extends DnsResolver.DnsException { + TestDnsException(int code, @Nullable Throwable cause) { + super(code, cause); + } + } + try { + throw new TestDnsException(DnsResolver.ERROR_SYSTEM, null); + } catch (DnsResolver.DnsException e) { + assertEquals(DnsResolver.ERROR_SYSTEM, e.code); + } + } }