Merge "Add kill switch for storing transport types" into main

This commit is contained in:
Treehugger Robot 2025-06-10 16:52:20 -07:00 • committed by Android (Google) Code Review
commit 4be13fb54d
4 changed files with 163 additions and 61 deletions

View file

@ -29,6 +29,7 @@ import android.net.NetworkCapabilities.TRANSPORT_ETHERNET
import android.net.NetworkCapabilities.TRANSPORT_TEST
import android.net.NetworkCapabilities.TRANSPORT_VPN
import android.net.NetworkCapabilities.TRANSPORT_WIFI
import android.net.NetworkIdentitySet.VERSION_ADD_SUB_ID
import android.net.NetworkIdentitySet.writeOptionalString
import android.os.Build
import android.telephony.TelephonyManager
@ -121,7 +122,7 @@ class NetworkIdentitySetTest {
val baos = ByteArrayOutputStream()
val outDataStream = DataOutputStream(baos)
originalSet.writeToStream(outDataStream)
originalSet.writeToStream(outDataStream, true /* storeTransportTypes */)
val bais = ByteArrayInputStream(baos.toByteArray())
val inDataStream = DataInputStream(bais)
@ -151,17 +152,17 @@ class NetworkIdentitySetTest {
}
@Test
fun testMigrateToSupportTransportTypes() {
doTestMigrateToSupportTransportTypes(TYPE_MOBILE, TRANSPORT_CELLULAR)
doTestMigrateToSupportTransportTypes(TYPE_WIFI, TRANSPORT_WIFI)
doTestMigrateToSupportTransportTypes(TYPE_BLUETOOTH, TRANSPORT_BLUETOOTH)
doTestMigrateToSupportTransportTypes(TYPE_ETHERNET, TRANSPORT_ETHERNET)
doTestMigrateToSupportTransportTypes(TYPE_VPN, TRANSPORT_VPN)
doTestMigrateToSupportTransportTypes(TYPE_TEST, TRANSPORT_TEST)
doTestMigrateToSupportTransportTypes(TYPE_WIMAX)
fun testMigrateToStoreTransportTypes() {
doTestMigrateToStoreTransportTypes(TYPE_MOBILE, TRANSPORT_CELLULAR)
doTestMigrateToStoreTransportTypes(TYPE_WIFI, TRANSPORT_WIFI)
doTestMigrateToStoreTransportTypes(TYPE_BLUETOOTH, TRANSPORT_BLUETOOTH)
doTestMigrateToStoreTransportTypes(TYPE_ETHERNET, TRANSPORT_ETHERNET)
doTestMigrateToStoreTransportTypes(TYPE_VPN, TRANSPORT_VPN)
doTestMigrateToStoreTransportTypes(TYPE_TEST, TRANSPORT_TEST)
doTestMigrateToStoreTransportTypes(TYPE_WIMAX)
}
private fun doTestMigrateToSupportTransportTypes(
private fun doTestMigrateToStoreTransportTypes(
legacyNetworkType: Int,
vararg expectedTransportTypes: Int
) {
@ -184,4 +185,35 @@ class NetworkIdentitySetTest {
assertEquals(expectedSet, deserializedSet)
}
@Test
fun testDowngradeFromStoreTransportTypes() {
val originalSet = NetworkIdentitySet()
originalSet.add(buildNetworkIdentity(TYPE_MOBILE, TRANSPORT_CELLULAR))
originalSet.add(buildNetworkIdentity(TYPE_WIFI, TRANSPORT_VPN, TRANSPORT_WIFI))
val baos = ByteArrayOutputStream()
val outDataStream = DataOutputStream(baos)
// Write to stream with storeTransportTypes disabled.
originalSet.writeToStream(outDataStream, false /* storeTransportTypes */)
val bais = ByteArrayInputStream(baos.toByteArray())
val inDataStream = DataInputStream(bais)
// Verify the output is in the corresponding format.
val version = inDataStream.readInt()
val size = inDataStream.readInt()
assertEquals(VERSION_ADD_SUB_ID, version)
assertEquals(2, size)
// Generate expected identity set with deduced transport type.
val expectedSet = NetworkIdentitySet()
expectedSet.add(buildNetworkIdentity(TYPE_MOBILE, TRANSPORT_CELLULAR))
expectedSet.add(buildNetworkIdentity(TYPE_WIFI, TRANSPORT_WIFI))
// Verify the transport types are from deduced transport type.
inDataStream.reset()
val deserializedSet = NetworkIdentitySet(inDataStream)
assertEquals(expectedSet, deserializedSet)
}
}

View file

@ -119,13 +119,19 @@ public class NetworkStatsCollectionTest {
RecurrenceRule.sClock = Clock.fixed(instant, ZoneId.systemDefault());
}
private NetworkStatsCollection buildNetworkStatsCollection(long bucketDuration) {
return new NetworkStatsCollection(bucketDuration, false /* useFastDataInput */,
true /* storeTransportTypes */);
}
@Test
public void testReadLegacyNetwork() throws Exception {
final File testFile =
new File(InstrumentationRegistry.getContext().getFilesDir(), TEST_FILE);
stageFile(R.raw.netstats_v1, testFile);
final NetworkStatsCollection collection = new NetworkStatsCollection(30 * MINUTE_IN_MILLIS);
final NetworkStatsCollection collection =
buildNetworkStatsCollection(30 * MINUTE_IN_MILLIS);
collection.readLegacyNetwork(testFile);
// verify that history read correctly
@ -153,7 +159,8 @@ public class NetworkStatsCollectionTest {
new File(InstrumentationRegistry.getContext().getFilesDir(), TEST_FILE);
stageFile(R.raw.netstats_uid_v4, testFile);
final NetworkStatsCollection collection = new NetworkStatsCollection(30 * MINUTE_IN_MILLIS);
final NetworkStatsCollection collection =
buildNetworkStatsCollection(30 * MINUTE_IN_MILLIS);
collection.readLegacyUid(testFile, false);
// verify that history read correctly
@ -181,7 +188,8 @@ public class NetworkStatsCollectionTest {
new File(InstrumentationRegistry.getContext().getFilesDir(), TEST_FILE);
stageFile(R.raw.netstats_uid_v4, testFile);
final NetworkStatsCollection collection = new NetworkStatsCollection(30 * MINUTE_IN_MILLIS);
final NetworkStatsCollection collection =
buildNetworkStatsCollection(30 * MINUTE_IN_MILLIS);
collection.readLegacyUid(testFile, true);
// verify that history read correctly
@ -208,7 +216,8 @@ public class NetworkStatsCollectionTest {
new File(InstrumentationRegistry.getContext().getFilesDir(), TEST_FILE);
stageFile(uidRes, testFile);
final NetworkStatsCollection collection = new NetworkStatsCollection(30 * MINUTE_IN_MILLIS);
final NetworkStatsCollection collection =
buildNetworkStatsCollection(30 * MINUTE_IN_MILLIS);
collection.readLegacyUid(testFile, true);
// now export into a unified format
@ -220,9 +229,11 @@ public class NetworkStatsCollectionTest {
@Test
public void testFastDataInputRead() throws Exception {
final NetworkStatsCollection legacyCollection =
new NetworkStatsCollection(30 * MINUTE_IN_MILLIS, false /* useFastDataInput */);
new NetworkStatsCollection(30 * MINUTE_IN_MILLIS, false /* useFastDataInput */,
true /* storeTransportTypes */);
final NetworkStatsCollection fastReadCollection =
new NetworkStatsCollection(30 * MINUTE_IN_MILLIS, true /* useFastDataInput */);
new NetworkStatsCollection(30 * MINUTE_IN_MILLIS, true /* useFastDataInput */,
true /* storeTransportTypes */);
final InputStream bis = getUidInputStreamFromRes(R.raw.netstats_uid_v4);
legacyCollection.read(bis);
bis.reset();
@ -232,7 +243,7 @@ public class NetworkStatsCollectionTest {
@Test
public void testStartEndAtomicBuckets() throws Exception {
final NetworkStatsCollection collection = new NetworkStatsCollection(HOUR_IN_MILLIS);
final NetworkStatsCollection collection = buildNetworkStatsCollection(HOUR_IN_MILLIS);
// record empty data straddling between buckets
final NetworkStats.Entry entry = new NetworkStats.Entry();
@ -247,7 +258,7 @@ public class NetworkStatsCollectionTest {
@Test
public void testAccessLevels() throws Exception {
final NetworkStatsCollection collection = new NetworkStatsCollection(HOUR_IN_MILLIS);
final NetworkStatsCollection collection = buildNetworkStatsCollection(HOUR_IN_MILLIS);
final NetworkStats.Entry entry = new NetworkStats.Entry();
final NetworkIdentitySet identSet = new NetworkIdentitySet();
identSet.add(new NetworkIdentity(TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN,
@ -314,8 +325,9 @@ public class NetworkStatsCollectionTest {
stageFile(R.raw.netstats_v1, testFile);
final NetworkStatsCollection emptyCollection =
new NetworkStatsCollection(30 * MINUTE_IN_MILLIS);
final NetworkStatsCollection collection = new NetworkStatsCollection(30 * MINUTE_IN_MILLIS);
buildNetworkStatsCollection(30 * MINUTE_IN_MILLIS);
final NetworkStatsCollection collection =
buildNetworkStatsCollection(30 * MINUTE_IN_MILLIS);
collection.readLegacyNetwork(testFile);
// We're in the future, but not that far off
@ -511,7 +523,7 @@ public class NetworkStatsCollectionTest {
setClock(Instant.parse("2012-06-01T00:00:00.00Z"));
// Create a simple history with a ton of measured usage
final NetworkStatsCollection large = new NetworkStatsCollection(HOUR_IN_MILLIS);
final NetworkStatsCollection large = buildNetworkStatsCollection(HOUR_IN_MILLIS);
final NetworkIdentitySet ident = new NetworkIdentitySet();
ident.add(new NetworkIdentity(ConnectivityManager.TYPE_MOBILE, -1, TEST_IMSI, null,
false, true, true, OEM_NONE, TEST_SUBID,
@ -532,7 +544,7 @@ public class NetworkStatsCollectionTest {
@Test
public void testRounding() throws Exception {
final NetworkStatsCollection coll = new NetworkStatsCollection(HOUR_IN_MILLIS);
final NetworkStatsCollection coll = buildNetworkStatsCollection(HOUR_IN_MILLIS);
// Special values should remain unchanged
for (long time : new long[] {

View file

@ -83,10 +83,16 @@ public final class NetworkStatsRecorderTest {
MockitoAnnotations.initMocks(this);
}
private NetworkStatsCollection buildNetworkStatsCollection(long bucketDuration) {
return new NetworkStatsCollection(bucketDuration, false /* useFastDataInput */,
true /* storeTransportTypes */);
}
private NetworkStatsRecorder buildRecorder(FileRotator rotator, boolean wipeOnError) {
return new NetworkStatsRecorder(rotator, mObserver, mDropBox, TEST_PREFIX,
HOUR_IN_MILLIS, false /* includeTags */, wipeOnError,
false /* useFastDataInput */, null /* baseDir */);
false /* useFastDataInput */, true /* storeTransportType */,
null /* baseDir */);
}
@Test
@ -110,7 +116,7 @@ public final class NetworkStatsRecorderTest {
@Test
public void testFileReadingMetrics_empty() {
final NetworkStatsCollection collection = new NetworkStatsCollection(30);
final NetworkStatsCollection collection = buildNetworkStatsCollection(30);
final NetworkStatsMetricsLogger.Dependencies deps =
mock(NetworkStatsMetricsLogger.Dependencies.class);
final NetworkStatsMetricsLogger logger = new NetworkStatsMetricsLogger(deps);
@ -146,7 +152,7 @@ public final class NetworkStatsRecorderTest {
@Test
public void testFileReadingMetrics() {
final NetworkStatsCollection collection = new NetworkStatsCollection(30);
final NetworkStatsCollection collection = buildNetworkStatsCollection(30);
final NetworkStats.Entry entry = new NetworkStats.Entry();
final NetworkIdentitySet identSet = new NetworkIdentitySet();
identSet.add(new NetworkIdentity.Builder().build());
@ -177,7 +183,7 @@ public final class NetworkStatsRecorderTest {
@Test
public void testFileReadingMetrics_fileAttributes() throws IOException {
final NetworkStatsCollection collection = new NetworkStatsCollection(30);
final NetworkStatsCollection collection = buildNetworkStatsCollection(30);
// Create files for testing. Only the first and the third files should be counted,
// with total 26 (each char takes 2 bytes) bytes in the content.

View file

@ -619,6 +619,12 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest {
return true;
}
@Override
public boolean isTetheringFeatureNotChickenedOut(@NonNull Context cts,
@NonNull String feature) {
return mFeatureFlags.getOrDefault(feature, true);
}
@Override
public boolean isTrafficStatsServiceRateLimitCacheEnabled(Context ctx,
boolean isClientCacheEnabled) {
@ -2676,7 +2682,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest {
return new NetworkStatsRecorder(new FileRotator(
directory, prefix, config.rotateAgeMillis, config.deleteAgeMillis),
observer, dropBox, prefix, config.bucketDuration, includeTags, wipeOnError,
false /* useFastDataInput */, directory);
false /* useFastDataInput */, true /* storeTransportTypes */, directory);
}
private NetworkStatsCollection getLegacyCollection(String prefix, boolean includeTags) {