Improve CTS tests to really check background network restrictions.

BUG: 26685616
Change-Id: If2b1649435b0a4e5b8c383eb3196807a03359d70
This commit is contained in:
Felipe Leme
2016-02-08 12:16:51 -08:00
parent 4fc69f6c25
commit c50a6c7d4b
2 changed files with 76 additions and 13 deletions

View File

@@ -25,9 +25,14 @@ import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.test.InstrumentationTestCase;
import android.util.Log;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
/**
* Tests for the {@link ConnectivityManager} API.
*
@@ -38,27 +43,36 @@ import android.util.Log;
public class ConnectivityManagerTest extends InstrumentationTestCase {
private static final String TAG = "ConnectivityManagerTest";
static final String MANIFEST_RECEIVER = "ManifestReceiver";
static final String DYNAMIC_RECEIVER = "DynamicReceiver";
private static final String MANIFEST_RECEIVER = "ManifestReceiver";
private static final String DYNAMIC_RECEIVER = "DynamicReceiver";
private ConnectivityManager mCM;
private static final String STATUS_NETWORK_UNAVAILABLE_PREFIX = "NetworkUnavailable:";
private static final String STATUS_NETWORK_AVAILABLE_PREFIX = "NetworkAvailable:";
private ConnectivityManager mCm;
private int mUid;
@Override
public void setUp() throws Exception {
super.setUp();
mCM = (ConnectivityManager) getInstrumentation().getContext().getSystemService(
Activity.CONNECTIVITY_SERVICE);
final Context context = getInstrumentation().getContext();
mCm = (ConnectivityManager) context.getSystemService(Activity.CONNECTIVITY_SERVICE);
mUid = context.getPackageManager()
.getPackageInfo(context.getPackageName(), 0).applicationInfo.uid;
final boolean metered = mCm.isActiveNetworkMetered();
Log.i(TAG, getName() + ": uid=" + mUid + ", metered=" + metered);
assertTrue("Active network is not metered", metered);
}
public void testGetRestrictBackgroundStatus_disabled() {
public void testGetRestrictBackgroundStatus_disabled() throws Exception {
assertRestrictBackgroundStatus(RESTRICT_BACKGROUND_STATUS_DISABLED);
}
public void testGetRestrictBackgroundStatus_whitelisted() {
public void testGetRestrictBackgroundStatus_whitelisted() throws Exception {
assertRestrictBackgroundStatus(RESTRICT_BACKGROUND_STATUS_WHITELISTED);
}
public void testGetRestrictBackgroundStatus_enabled() {
public void testGetRestrictBackgroundStatus_enabled() throws Exception {
assertRestrictBackgroundStatus(RESTRICT_BACKGROUND_STATUS_ENABLED);
}
@@ -102,11 +116,34 @@ public class ConnectivityManagerTest extends InstrumentationTestCase {
return prefs.getInt(action, 0);
}
private void assertRestrictBackgroundStatus(int expectedStatus) {
final String expected = toString(expectedStatus);
private void assertRestrictBackgroundStatus(int expectedApiStatus) throws InterruptedException {
// First asserts the API returns the proper value...
final String expected = toString(expectedApiStatus);
Log.d(TAG, getName() + " (expecting " + expected + ")");
final int actualStatus = mCM.getRestrictBackgroundStatus();
assertEquals("wrong status", expected, toString(actualStatus));
final int apiStatus = mCm.getRestrictBackgroundStatus();
String actualApiStatus = toString(apiStatus);
assertEquals("wrong status", expected, actualApiStatus);
//...then use a background thread to verify the actual network status.
final LinkedBlockingQueue<String> result = new LinkedBlockingQueue<>(1);
new Thread(new Runnable() {
@Override
public void run() {
Log.d(TAG, "Running on thread " + Thread.currentThread().getName());
final Network network = mCm.getActiveNetwork();
final NetworkInfo networkInfo = mCm.getActiveNetworkInfo();
Log.d(TAG, "activeNetwork: " + network + " activeNetworkInfo: " + networkInfo);
final String prefix = network == null ?
STATUS_NETWORK_UNAVAILABLE_PREFIX : STATUS_NETWORK_AVAILABLE_PREFIX;
result.offer(prefix + networkInfo);
}
}, "CheckNetworkThread").start();
final String actualNetworkStatus = result.poll(10, TimeUnit.SECONDS);
assertNotNull("timeout waiting for background thread", actualNetworkStatus);
final String expectedPrefix = apiStatus == RESTRICT_BACKGROUND_STATUS_ENABLED ?
STATUS_NETWORK_UNAVAILABLE_PREFIX : STATUS_NETWORK_AVAILABLE_PREFIX;
assertTrue("Wrong network status for API status " + actualApiStatus + ": "
+ actualNetworkStatus, actualNetworkStatus.startsWith(expectedPrefix));
}
private String toString(int status) {

View File

@@ -18,23 +18,29 @@ package com.android.cts.net;
import com.android.ddmlib.Log;
import com.android.tradefed.device.DeviceNotAvailableException;
import com.android.tradefed.device.WifiHelper;
public class HostsideRestrictBackgroundNetworkTests extends HostsideNetworkTestCase {
private static final String TEST_APP2_PKG = "com.android.cts.net.hostside.app2";
private static final String TEST_APP2_APK = "CtsHostsideNetworkTestsApp2.apk";
private int mUid;
private WifiHelper mWifiHelper;
@Override
protected void setUp() throws Exception {
super.setUp();
mUid = getUid(TEST_PKG);
mWifiHelper = new WifiHelper(getDevice());
setWifiMeteredStatus(true);
setRestrictBackground(false);
uninstallPackage(TEST_APP2_PKG, false);
installPackage(TEST_APP2_APK);
startBroadcastReceiverService();
mUid = getUid(TEST_PKG);
}
@Override
@@ -43,6 +49,7 @@ public class HostsideRestrictBackgroundNetworkTests extends HostsideNetworkTestC
uninstallPackage(TEST_APP2_PKG, true);
setRestrictBackground(false);
setWifiMeteredStatus(false);
}
public void testGetRestrictBackgroundStatus_disabled() throws Exception {
@@ -151,6 +158,25 @@ public class HostsideRestrictBackgroundNetworkTests extends HostsideNetworkTestC
+ expected + ", got " + actual);
}
private void setWifiMeteredStatus(boolean metered) throws DeviceNotAvailableException {
mWifiHelper.enableWifi();
// TODO: if it's not guaranteed the device has wi-fi, we need to change the tests
// to make the actual verification of restrictions optional.
final String netId = mWifiHelper.getSSID();
assertNotNull("null SSID", netId);
assertFalse("empty SSID", netId.trim().isEmpty());
Log.i(TAG, "Setting wi-fi network " + netId + " metered status to " + metered);
final String setCommand = "cmd netpolicy set metered-network " + netId + " "+ metered;
final String result = runCommand(setCommand);
assertTrue("Command '" + setCommand + "' failed: " + result, result.trim().isEmpty());
// Sanity check.
final String newStatus = runCommand("cmd netpolicy get metered-network " + netId);
assertEquals("Metered status of wi-fi network " + netId + " not set properly",
newStatus.trim(), Boolean.toString(metered));
}
private void setRestrictBackground(boolean enabled) throws DeviceNotAvailableException {
runCommand("cmd netpolicy set restrict-background " + enabled);
final String output = runCommand("cmd netpolicy get restrict-background ").trim();