Merge changes Ie5689ba6,I6056da19,I89f39129,I6760d579

* changes:
  Skip NetworkStatsManager CTS test before T.
  Fix testDataMigrationUtils fail on R device
  Fix testCallback fail caused by lacking of looper
  [MS84] Add CTS for data migration utils
This commit is contained in:
Treehugger Robot 2022-05-12 15:07:00 +00:00 • committed by Gerrit Code Review
commit 8ed131dd0b

View file

@ -29,8 +29,20 @@ import static android.app.usage.NetworkStats.Bucket.STATE_DEFAULT;
import static android.app.usage.NetworkStats.Bucket.STATE_FOREGROUND;
import static android.app.usage.NetworkStats.Bucket.TAG_NONE;
import static android.app.usage.NetworkStats.Bucket.UID_ALL;
import static android.net.netstats.NetworkStatsDataMigrationUtils.PREFIX_UID;
import static android.net.netstats.NetworkStatsDataMigrationUtils.PREFIX_UID_TAG;
import static android.net.netstats.NetworkStatsDataMigrationUtils.PREFIX_XT;
import static com.android.testutils.DevSdkIgnoreRuleKt.SC_V2;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import android.app.AppOpsManager;
import android.app.Instrumentation;
import android.app.usage.NetworkStats;
import android.app.usage.NetworkStatsManager;
import android.content.Context;
@ -40,7 +52,10 @@ import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkRequest;
import android.net.NetworkStatsCollection;
import android.net.NetworkStatsHistory;
import android.net.TrafficStats;
import android.net.netstats.NetworkStatsDataMigrationUtils;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Process;
@ -48,11 +63,23 @@ import android.os.RemoteException;
import android.os.SystemClock;
import android.platform.test.annotations.AppModeFull;
import android.telephony.TelephonyManager;
import android.test.InstrumentationTestCase;
import android.text.TextUtils;
import android.util.Log;
import androidx.test.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.compatibility.common.util.ShellIdentityUtils;
import com.android.compatibility.common.util.SystemUtil;
import com.android.modules.utils.build.SdkLevel;
import com.android.testutils.ConnectivityModuleTest;
import com.android.testutils.DevSdkIgnoreRule;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.IOException;
import java.io.InputStream;
@ -62,8 +89,18 @@ import java.net.URL;
import java.net.UnknownHostException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@ConnectivityModuleTest
@AppModeFull(reason = "instant apps cannot be granted USAGE_STATS")
@RunWith(AndroidJUnit4.class)
public class NetworkStatsManagerTest {
@Rule
public final DevSdkIgnoreRule ignoreRule = new DevSdkIgnoreRule(SC_V2 /* ignoreClassUpTo */);
public class NetworkStatsManagerTest extends InstrumentationTestCase {
private static final String LOG_TAG = "NetworkStatsManagerTest";
private static final String APPOPS_SET_SHELL_COMMAND = "appops set {0} {1} {2}";
private static final String APPOPS_GET_SHELL_COMMAND = "appops get {0} {1}";
@ -164,9 +201,11 @@ public class NetworkStatsManagerTest extends InstrumentationTestCase {
};
private String mPkg;
private Context mContext;
private NetworkStatsManager mNsm;
private ConnectivityManager mCm;
private PackageManager mPm;
private Instrumentation mInstrumentation;
private long mStartTime;
private long mEndTime;
@ -224,44 +263,40 @@ public class NetworkStatsManagerTest extends InstrumentationTestCase {
}
}
@Override
protected void setUp() throws Exception {
super.setUp();
mNsm = (NetworkStatsManager) getInstrumentation().getContext()
.getSystemService(Context.NETWORK_STATS_SERVICE);
@Before
public void setUp() throws Exception {
mContext = InstrumentationRegistry.getContext();
mNsm = mContext.getSystemService(NetworkStatsManager.class);
mNsm.setPollForce(true);
mCm = (ConnectivityManager) getInstrumentation().getContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
mPm = getInstrumentation().getContext().getPackageManager();
mPkg = getInstrumentation().getContext().getPackageName();
mCm = mContext.getSystemService(ConnectivityManager.class);
mPm = mContext.getPackageManager();
mPkg = mContext.getPackageName();
mInstrumentation = InstrumentationRegistry.getInstrumentation();
mWriteSettingsMode = getAppOpsMode(AppOpsManager.OPSTR_WRITE_SETTINGS);
setAppOpsMode(AppOpsManager.OPSTR_WRITE_SETTINGS, "allow");
mUsageStatsMode = getAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS);
}
@Override
protected void tearDown() throws Exception {
@After
public void tearDown() throws Exception {
if (mWriteSettingsMode != null) {
setAppOpsMode(AppOpsManager.OPSTR_WRITE_SETTINGS, mWriteSettingsMode);
}
if (mUsageStatsMode != null) {
setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, mUsageStatsMode);
}
super.tearDown();
}
private void setAppOpsMode(String appop, String mode) throws Exception {
final String command = MessageFormat.format(APPOPS_SET_SHELL_COMMAND, mPkg, appop, mode);
SystemUtil.runShellCommand(command);
SystemUtil.runShellCommand(mInstrumentation, command);
}
private String getAppOpsMode(String appop) throws Exception {
final String command = MessageFormat.format(APPOPS_GET_SHELL_COMMAND, mPkg, appop);
String result = SystemUtil.runShellCommand(command);
String result = SystemUtil.runShellCommand(mInstrumentation, command);
if (result == null) {
Log.w(LOG_TAG, "App op " + appop + " could not be read.");
}
@ -269,7 +304,7 @@ public class NetworkStatsManagerTest extends InstrumentationTestCase {
}
private boolean isInForeground() throws IOException {
String result = SystemUtil.runShellCommand(getInstrumentation(),
String result = SystemUtil.runShellCommand(mInstrumentation,
"cmd activity get-uid-state " + Process.myUid());
return result.contains("FOREGROUND");
}
@ -366,15 +401,14 @@ public class NetworkStatsManagerTest extends InstrumentationTestCase {
private String getSubscriberId(int networkIndex) {
int networkType = mNetworkInterfacesToTest[networkIndex].getNetworkType();
if (ConnectivityManager.TYPE_MOBILE == networkType) {
TelephonyManager tm = (TelephonyManager) getInstrumentation().getContext()
.getSystemService(Context.TELEPHONY_SERVICE);
TelephonyManager tm = mContext.getSystemService(TelephonyManager.class);
return ShellIdentityUtils.invokeMethodWithShellPermissions(tm,
(telephonyManager) -> telephonyManager.getSubscriberId());
}
return "";
}
@AppModeFull
@Test
public void testDeviceSummary() throws Exception {
for (int i = 0; i < mNetworkInterfacesToTest.length; ++i) {
if (!shouldTestThisNetworkType(i, MINUTE / 2)) {
@ -410,7 +444,7 @@ public class NetworkStatsManagerTest extends InstrumentationTestCase {
}
}
@AppModeFull
@Test
public void testUserSummary() throws Exception {
for (int i = 0; i < mNetworkInterfacesToTest.length; ++i) {
if (!shouldTestThisNetworkType(i, MINUTE / 2)) {
@ -446,7 +480,7 @@ public class NetworkStatsManagerTest extends InstrumentationTestCase {
}
}
@AppModeFull
@Test
public void testAppSummary() throws Exception {
for (int i = 0; i < mNetworkInterfacesToTest.length; ++i) {
// Use tolerance value that large enough to make sure stats of at
@ -522,7 +556,7 @@ public class NetworkStatsManagerTest extends InstrumentationTestCase {
}
}
@AppModeFull
@Test
public void testAppDetails() throws Exception {
for (int i = 0; i < mNetworkInterfacesToTest.length; ++i) {
// Relatively large tolerance to accommodate for history bucket size.
@ -565,7 +599,7 @@ public class NetworkStatsManagerTest extends InstrumentationTestCase {
}
}
@AppModeFull
@Test
public void testUidDetails() throws Exception {
for (int i = 0; i < mNetworkInterfacesToTest.length; ++i) {
// Relatively large tolerance to accommodate for history bucket size.
@ -619,7 +653,7 @@ public class NetworkStatsManagerTest extends InstrumentationTestCase {
}
}
@AppModeFull
@Test
public void testTagDetails() throws Exception {
for (int i = 0; i < mNetworkInterfacesToTest.length; ++i) {
// Relatively large tolerance to accommodate for history bucket size.
@ -726,7 +760,7 @@ public class NetworkStatsManagerTest extends InstrumentationTestCase {
bucket.getRxBytes(), bucket.getTxBytes()));
}
@AppModeFull
@Test
public void testUidTagStateDetails() throws Exception {
for (int i = 0; i < mNetworkInterfacesToTest.length; ++i) {
// Relatively large tolerance to accommodate for history bucket size.
@ -803,7 +837,7 @@ public class NetworkStatsManagerTest extends InstrumentationTestCase {
}
}
@AppModeFull
@Test
public void testCallback() throws Exception {
for (int i = 0; i < mNetworkInterfacesToTest.length; ++i) {
// Relatively large tolerance to accommodate for history bucket size.
@ -825,6 +859,44 @@ public class NetworkStatsManagerTest extends InstrumentationTestCase {
// storing files of >2MB in CTS.
mNsm.unregisterUsageCallback(usageCallback);
// For T- devices, the registerUsageCallback invocation below will need a looper
// from the thread that calls into the API, which is not available in the test.
if (SdkLevel.isAtLeastT()) {
mNsm.registerUsageCallback(mNetworkInterfacesToTest[i].getNetworkType(),
getSubscriberId(i), THRESHOLD_BYTES, usageCallback);
mNsm.unregisterUsageCallback(usageCallback);
}
}
}
@Test
public void testDataMigrationUtils() throws Exception {
if (!SdkLevel.isAtLeastT()) return;
final List<String> prefixes = List.of(PREFIX_UID, PREFIX_XT, PREFIX_UID_TAG);
for (final String prefix : prefixes) {
final long duration = TextUtils.equals(PREFIX_XT, prefix) ? TimeUnit.HOURS.toMillis(1)
: TimeUnit.HOURS.toMillis(2);
final NetworkStatsCollection collection =
NetworkStatsDataMigrationUtils.readPlatformCollection(prefix, duration);
final long now = System.currentTimeMillis();
final Set<Map.Entry<NetworkStatsCollection.Key, NetworkStatsHistory>> entries =
collection.getEntries().entrySet();
for (final Map.Entry<NetworkStatsCollection.Key, NetworkStatsHistory> entry : entries) {
for (final NetworkStatsHistory.Entry historyEntry : entry.getValue().getEntries()) {
// Verify all value fields are reasonable.
assertTrue(historyEntry.getBucketStart() <= now);
assertTrue(historyEntry.getActiveTime() <= duration);
assertTrue(historyEntry.getRxBytes() >= 0);
assertTrue(historyEntry.getRxPackets() >= 0);
assertTrue(historyEntry.getTxBytes() >= 0);
assertTrue(historyEntry.getTxPackets() >= 0);
assertTrue(historyEntry.getOperations() >= 0);
}
}
}
}