Merge "DnsResolverTest: Use AndroidJUnit4.class"

This commit is contained in:
Aswin Sankar
2021-12-13 06:28:08 +00:00
committed by Gerrit Code Review

View File

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