diff --git a/tests/cts/net/src/android/net/cts/DnsResolverTest.java b/tests/cts/net/src/android/net/cts/DnsResolverTest.java index 22168b38f8..a0a1bab42e 100644 --- a/tests/cts/net/src/android/net/cts/DnsResolverTest.java +++ b/tests/cts/net/src/android/net/cts/DnsResolverTest.java @@ -25,15 +25,19 @@ import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR; import static android.net.cts.util.CtsNetUtils.TestNetworkCallback; import static android.system.OsConstants.ETIMEDOUT; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import android.annotation.NonNull; import android.annotation.Nullable; -import android.content.Context; import android.content.ContentResolver; +import android.content.Context; import android.content.pm.PackageManager; import android.net.ConnectivityManager; -import android.net.ConnectivityManager.NetworkCallback; import android.net.DnsResolver; -import android.net.LinkProperties; import android.net.Network; import android.net.NetworkCapabilities; import android.net.NetworkRequest; @@ -43,14 +47,20 @@ import android.os.CancellationSignal; import android.os.Handler; import android.os.Looper; import android.platform.test.annotations.AppModeFull; -import android.provider.Settings; import android.system.ErrnoException; -import android.test.AndroidTestCase; import android.util.Log; +import androidx.test.InstrumentationRegistry; +import androidx.test.runner.AndroidJUnit4; + import com.android.net.module.util.DnsPacket; import com.android.testutils.SkipPresubmit; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; @@ -61,7 +71,8 @@ import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; @AppModeFull(reason = "WRITE_SECURE_SETTINGS permission can't be granted to instant apps") -public class DnsResolverTest extends AndroidTestCase { +@RunWith(AndroidJUnit4.class) +public class DnsResolverTest { 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' @@ -91,6 +102,7 @@ public class DnsResolverTest extends AndroidTestCase { static final int QUERY_TIMES = 10; static final int NXDOMAIN = 3; + private Context mContext; private ContentResolver mCR; private ConnectivityManager mCM; private PackageManager mPackageManager; @@ -99,30 +111,27 @@ public class DnsResolverTest extends AndroidTestCase { private Executor mExecutorInline; private DnsResolver mDns; - private String mOldMode; - private String mOldDnsSpecifier; private TestNetworkCallback mWifiRequestCallback = null; - @Override - protected void setUp() throws Exception { - super.setUp(); - mCM = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE); + @Before + public void setUp() throws Exception { + mContext = InstrumentationRegistry.getContext(); + mCM = mContext.getSystemService(ConnectivityManager.class); mDns = DnsResolver.getInstance(); mExecutor = new Handler(Looper.getMainLooper())::post; mExecutorInline = (Runnable r) -> r.run(); - mCR = getContext().getContentResolver(); - mCtsNetUtils = new CtsNetUtils(getContext()); + mCR = mContext.getContentResolver(); + mCtsNetUtils = new CtsNetUtils(mContext); mCtsNetUtils.storePrivateDnsSetting(); mPackageManager = mContext.getPackageManager(); } - @Override - protected void tearDown() throws Exception { + @After + public void tearDown() throws Exception { mCtsNetUtils.restorePrivateDnsSetting(); if (mWifiRequestCallback != null) { mCM.unregisterNetworkCallback(mWifiRequestCallback); } - super.tearDown(); } private static String byteArrayToHexString(byte[] bytes) { @@ -298,42 +307,52 @@ public class DnsResolverTest extends AndroidTestCase { } } + @Test public void testRawQuery() throws Exception { doTestRawQuery(mExecutor); } + @Test public void testRawQueryInline() throws Exception { doTestRawQuery(mExecutorInline); } + @Test public void testRawQueryBlob() throws Exception { doTestRawQueryBlob(mExecutor); } + @Test public void testRawQueryBlobInline() throws Exception { doTestRawQueryBlob(mExecutorInline); } + @Test public void testRawQueryRoot() throws Exception { doTestRawQueryRoot(mExecutor); } + @Test public void testRawQueryRootInline() throws Exception { doTestRawQueryRoot(mExecutorInline); } + @Test public void testRawQueryNXDomain() throws Exception { doTestRawQueryNXDomain(mExecutor); } + @Test public void testRawQueryNXDomainInline() throws Exception { doTestRawQueryNXDomain(mExecutorInline); } + @Test public void testRawQueryNXDomainWithPrivateDns() throws Exception { doTestRawQueryNXDomainWithPrivateDns(mExecutor); } + @Test public void testRawQueryNXDomainInlineWithPrivateDns() throws Exception { doTestRawQueryNXDomainWithPrivateDns(mExecutorInline); } @@ -436,6 +455,7 @@ public class DnsResolverTest extends AndroidTestCase { } } + @Test public void testRawQueryCancel() throws InterruptedException { final String msg = "Test cancel RawQuery " + TEST_DOMAIN; // Start a DNS query and the cancel it immediately. Use VerifyCancelCallback to expect @@ -465,6 +485,7 @@ public class DnsResolverTest extends AndroidTestCase { } } + @Test public void testRawQueryBlobCancel() throws InterruptedException { final String msg = "Test cancel RawQuery blob " + byteArrayToHexString(TEST_BLOB); // Start a DNS query and the cancel it immediately. Use VerifyCancelCallback to expect @@ -493,6 +514,7 @@ public class DnsResolverTest extends AndroidTestCase { } } + @Test public void testCancelBeforeQuery() throws InterruptedException { final String msg = "Test cancelled RawQuery " + TEST_DOMAIN; for (Network network : getTestableNetworks()) { @@ -578,34 +600,42 @@ public class DnsResolverTest extends AndroidTestCase { } } + @Test public void testQueryForInetAddress() throws Exception { doTestQueryForInetAddress(mExecutor); } + @Test public void testQueryForInetAddressInline() throws Exception { doTestQueryForInetAddress(mExecutorInline); } + @Test public void testQueryForInetAddressIpv4() throws Exception { doTestQueryForInetAddressIpv4(mExecutor); } + @Test public void testQueryForInetAddressIpv4Inline() throws Exception { doTestQueryForInetAddressIpv4(mExecutorInline); } + @Test public void testQueryForInetAddressIpv6() throws Exception { doTestQueryForInetAddressIpv6(mExecutor); } + @Test public void testQueryForInetAddressIpv6Inline() throws Exception { doTestQueryForInetAddressIpv6(mExecutorInline); } + @Test public void testContinuousQueries() throws Exception { doTestContinuousQueries(mExecutor); } + @Test @SkipPresubmit(reason = "Flaky: b/159762682; add to presubmit after fixing") public void testContinuousQueriesInline() throws Exception { doTestContinuousQueries(mExecutorInline); @@ -625,6 +655,7 @@ public class DnsResolverTest extends AndroidTestCase { } } + @Test public void testQueryCancelForInetAddress() throws InterruptedException { final String msg = "Test cancel query for InetAddress " + TEST_DOMAIN; // Start a DNS query and the cancel it immediately. Use VerifyCancelInetAddressCallback to @@ -686,6 +717,7 @@ public class DnsResolverTest extends AndroidTestCase { } } + @Test public void testPrivateDnsBypass() throws InterruptedException { final Network[] testNetworks = getTestableNetworks();