Add unit test when querying TYPE_WIFI + null/empty/non-null subscriberId
Starting with API level 31, the subscriberId is applicable for the wifi network. Considering applications may use null or an empty string as subscriberId (for instance, cts), frameworks create MATCH_WIFI_WILDCARD NetworkTemplate when querying wifi network with null or an empty string which is the behavior before API level 31. Bug: 188915450 Test: atest -c NetworkStatsManagerTest Change-Id: Id4ae06840e1749997e970b8f1ec391060967bd47
This commit is contained in:
@@ -24,8 +24,8 @@ import static org.mockito.ArgumentMatchers.any;
|
|||||||
import static org.mockito.ArgumentMatchers.anyInt;
|
import static org.mockito.ArgumentMatchers.anyInt;
|
||||||
import static org.mockito.ArgumentMatchers.anyLong;
|
import static org.mockito.ArgumentMatchers.anyLong;
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
import static org.mockito.ArgumentMatchers.argThat;
|
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.reset;
|
||||||
import static org.mockito.Mockito.times;
|
import static org.mockito.Mockito.times;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
@@ -52,6 +52,7 @@ import org.mockito.invocation.InvocationOnMock;
|
|||||||
@RunWith(AndroidJUnit4.class)
|
@RunWith(AndroidJUnit4.class)
|
||||||
@SmallTest
|
@SmallTest
|
||||||
public class NetworkStatsManagerTest {
|
public class NetworkStatsManagerTest {
|
||||||
|
private static final String TEST_SUBSCRIBER_ID = "subid";
|
||||||
|
|
||||||
private @Mock INetworkStatsService mService;
|
private @Mock INetworkStatsService mService;
|
||||||
private @Mock INetworkStatsSession mStatsSession;
|
private @Mock INetworkStatsSession mStatsSession;
|
||||||
@@ -69,7 +70,6 @@ public class NetworkStatsManagerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testQueryDetails() throws RemoteException {
|
public void testQueryDetails() throws RemoteException {
|
||||||
final String subscriberId = "subid";
|
|
||||||
final long startTime = 1;
|
final long startTime = 1;
|
||||||
final long endTime = 100;
|
final long endTime = 100;
|
||||||
final int uid1 = 10001;
|
final int uid1 = 10001;
|
||||||
@@ -113,7 +113,7 @@ public class NetworkStatsManagerTest {
|
|||||||
.then((InvocationOnMock inv) -> {
|
.then((InvocationOnMock inv) -> {
|
||||||
NetworkTemplate template = inv.getArgument(0);
|
NetworkTemplate template = inv.getArgument(0);
|
||||||
assertEquals(MATCH_MOBILE_ALL, template.getMatchRule());
|
assertEquals(MATCH_MOBILE_ALL, template.getMatchRule());
|
||||||
assertEquals(subscriberId, template.getSubscriberId());
|
assertEquals(TEST_SUBSCRIBER_ID, template.getSubscriberId());
|
||||||
return history1;
|
return history1;
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -124,13 +124,13 @@ public class NetworkStatsManagerTest {
|
|||||||
.then((InvocationOnMock inv) -> {
|
.then((InvocationOnMock inv) -> {
|
||||||
NetworkTemplate template = inv.getArgument(0);
|
NetworkTemplate template = inv.getArgument(0);
|
||||||
assertEquals(MATCH_MOBILE_ALL, template.getMatchRule());
|
assertEquals(MATCH_MOBILE_ALL, template.getMatchRule());
|
||||||
assertEquals(subscriberId, template.getSubscriberId());
|
assertEquals(TEST_SUBSCRIBER_ID, template.getSubscriberId());
|
||||||
return history2;
|
return history2;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
NetworkStats stats = mManager.queryDetails(
|
NetworkStats stats = mManager.queryDetails(
|
||||||
ConnectivityManager.TYPE_MOBILE, subscriberId, startTime, endTime);
|
ConnectivityManager.TYPE_MOBILE, TEST_SUBSCRIBER_ID, startTime, endTime);
|
||||||
|
|
||||||
NetworkStats.Bucket bucket = new NetworkStats.Bucket();
|
NetworkStats.Bucket bucket = new NetworkStats.Bucket();
|
||||||
|
|
||||||
@@ -166,35 +166,30 @@ public class NetworkStatsManagerTest {
|
|||||||
assertFalse(stats.hasNextBucket());
|
assertFalse(stats.hasNextBucket());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
private void runQueryDetailsAndCheckTemplate(int networkType, String subscriberId,
|
||||||
public void testQueryDetails_NoSubscriberId() throws RemoteException {
|
NetworkTemplate expectedTemplate) throws RemoteException {
|
||||||
final long startTime = 1;
|
final long startTime = 1;
|
||||||
final long endTime = 100;
|
final long endTime = 100;
|
||||||
final int uid1 = 10001;
|
final int uid1 = 10001;
|
||||||
final int uid2 = 10002;
|
final int uid2 = 10002;
|
||||||
|
|
||||||
|
reset(mStatsSession);
|
||||||
when(mService.openSessionForUsageStats(anyInt(), anyString())).thenReturn(mStatsSession);
|
when(mService.openSessionForUsageStats(anyInt(), anyString())).thenReturn(mStatsSession);
|
||||||
when(mStatsSession.getRelevantUids()).thenReturn(new int[] { uid1, uid2 });
|
when(mStatsSession.getRelevantUids()).thenReturn(new int[] { uid1, uid2 });
|
||||||
|
|
||||||
NetworkStats stats = mManager.queryDetails(
|
|
||||||
ConnectivityManager.TYPE_MOBILE, null, startTime, endTime);
|
|
||||||
|
|
||||||
when(mStatsSession.getHistoryIntervalForUid(any(NetworkTemplate.class),
|
when(mStatsSession.getHistoryIntervalForUid(any(NetworkTemplate.class),
|
||||||
anyInt(), anyInt(), anyInt(), anyInt(), anyLong(), anyLong()))
|
anyInt(), anyInt(), anyInt(), anyInt(), anyLong(), anyLong()))
|
||||||
.thenReturn(new NetworkStatsHistory(10, 0));
|
.thenReturn(new NetworkStatsHistory(10, 0));
|
||||||
|
NetworkStats stats = mManager.queryDetails(
|
||||||
|
networkType, subscriberId, startTime, endTime);
|
||||||
|
|
||||||
verify(mStatsSession, times(1)).getHistoryIntervalForUid(
|
verify(mStatsSession, times(1)).getHistoryIntervalForUid(
|
||||||
argThat((NetworkTemplate t) ->
|
eq(expectedTemplate),
|
||||||
// No subscriberId: MATCH_MOBILE_WILDCARD template
|
|
||||||
t.getMatchRule() == NetworkTemplate.MATCH_MOBILE_WILDCARD),
|
|
||||||
eq(uid1), eq(android.net.NetworkStats.SET_ALL),
|
eq(uid1), eq(android.net.NetworkStats.SET_ALL),
|
||||||
eq(android.net.NetworkStats.TAG_NONE),
|
eq(android.net.NetworkStats.TAG_NONE),
|
||||||
eq(NetworkStatsHistory.FIELD_ALL), eq(startTime), eq(endTime));
|
eq(NetworkStatsHistory.FIELD_ALL), eq(startTime), eq(endTime));
|
||||||
|
|
||||||
verify(mStatsSession, times(1)).getHistoryIntervalForUid(
|
verify(mStatsSession, times(1)).getHistoryIntervalForUid(
|
||||||
argThat((NetworkTemplate t) ->
|
eq(expectedTemplate),
|
||||||
// No subscriberId: MATCH_MOBILE_WILDCARD template
|
|
||||||
t.getMatchRule() == NetworkTemplate.MATCH_MOBILE_WILDCARD),
|
|
||||||
eq(uid2), eq(android.net.NetworkStats.SET_ALL),
|
eq(uid2), eq(android.net.NetworkStats.SET_ALL),
|
||||||
eq(android.net.NetworkStats.TAG_NONE),
|
eq(android.net.NetworkStats.TAG_NONE),
|
||||||
eq(NetworkStatsHistory.FIELD_ALL), eq(startTime), eq(endTime));
|
eq(NetworkStatsHistory.FIELD_ALL), eq(startTime), eq(endTime));
|
||||||
@@ -202,6 +197,25 @@ public class NetworkStatsManagerTest {
|
|||||||
assertFalse(stats.hasNextBucket());
|
assertFalse(stats.hasNextBucket());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNetworkTemplateWhenRunningQueryDetails_NoSubscriberId() throws RemoteException {
|
||||||
|
runQueryDetailsAndCheckTemplate(ConnectivityManager.TYPE_MOBILE,
|
||||||
|
null /* subscriberId */, NetworkTemplate.buildTemplateMobileWildcard());
|
||||||
|
runQueryDetailsAndCheckTemplate(ConnectivityManager.TYPE_WIFI,
|
||||||
|
"" /* subscriberId */, NetworkTemplate.buildTemplateWifiWildcard());
|
||||||
|
runQueryDetailsAndCheckTemplate(ConnectivityManager.TYPE_WIFI,
|
||||||
|
null /* subscriberId */, NetworkTemplate.buildTemplateWifiWildcard());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNetworkTemplateWhenRunningQueryDetails_MergedCarrierWifi()
|
||||||
|
throws RemoteException {
|
||||||
|
runQueryDetailsAndCheckTemplate(ConnectivityManager.TYPE_WIFI,
|
||||||
|
TEST_SUBSCRIBER_ID,
|
||||||
|
NetworkTemplate.buildTemplateWifi(NetworkTemplate.WIFI_NETWORKID_ALL,
|
||||||
|
TEST_SUBSCRIBER_ID));
|
||||||
|
}
|
||||||
|
|
||||||
private void assertBucketMatches(Entry expected, NetworkStats.Bucket actual) {
|
private void assertBucketMatches(Entry expected, NetworkStats.Bucket actual) {
|
||||||
assertEquals(expected.uid, actual.getUid());
|
assertEquals(expected.uid, actual.getUid());
|
||||||
assertEquals(expected.rxBytes, actual.getRxBytes());
|
assertEquals(expected.rxBytes, actual.getRxBytes());
|
||||||
|
|||||||
Reference in New Issue
Block a user