[FUI26] Address comments on aosp/1560408
Test: TH Bug: 174123988 Change-Id: I949eeb8903e903d8ede90521442b1f917d5cdfeb
This commit is contained in:
@@ -127,6 +127,7 @@ import static com.android.testutils.MiscAsserts.assertContainsExactly;
|
|||||||
import static com.android.testutils.MiscAsserts.assertEmpty;
|
import static com.android.testutils.MiscAsserts.assertEmpty;
|
||||||
import static com.android.testutils.MiscAsserts.assertLength;
|
import static com.android.testutils.MiscAsserts.assertLength;
|
||||||
import static com.android.testutils.MiscAsserts.assertRunsInAtMost;
|
import static com.android.testutils.MiscAsserts.assertRunsInAtMost;
|
||||||
|
import static com.android.testutils.MiscAsserts.assertSameElements;
|
||||||
import static com.android.testutils.MiscAsserts.assertThrows;
|
import static com.android.testutils.MiscAsserts.assertThrows;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
@@ -5806,20 +5807,8 @@ public class ConnectivityServiceTest {
|
|||||||
mCm.unregisterNetworkCallback(networkCallback);
|
mCm.unregisterNetworkCallback(networkCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> void assertSameElementsNoDuplicates(T[] expected, T[] actual) {
|
private void expectNotifyNetworkStatus(List<Network> networks, String defaultIface,
|
||||||
// Easier to implement than a proper "assertSameElements" method that also correctly deals
|
Integer vpnUid, String vpnIfname, List<String> underlyingIfaces) throws Exception {
|
||||||
// with duplicates.
|
|
||||||
final String msg = Arrays.toString(expected) + " != " + Arrays.toString(actual);
|
|
||||||
assertEquals(msg, expected.length, actual.length);
|
|
||||||
Set expectedSet = new ArraySet<>(Arrays.asList(expected));
|
|
||||||
assertEquals("expected contains duplicates", expectedSet.size(), expected.length);
|
|
||||||
// actual cannot have duplicates because it's the same length and has the same elements.
|
|
||||||
Set actualSet = new ArraySet<>(Arrays.asList(actual));
|
|
||||||
assertEquals(expectedSet, actualSet);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void expectNetworkStatus(Network[] networks, String defaultIface,
|
|
||||||
Integer vpnUid, String vpnIfname, String[] underlyingIfaces) throws Exception {
|
|
||||||
ArgumentCaptor<List<Network>> networksCaptor = ArgumentCaptor.forClass(List.class);
|
ArgumentCaptor<List<Network>> networksCaptor = ArgumentCaptor.forClass(List.class);
|
||||||
ArgumentCaptor<List<UnderlyingNetworkInfo>> vpnInfosCaptor =
|
ArgumentCaptor<List<UnderlyingNetworkInfo>> vpnInfosCaptor =
|
||||||
ArgumentCaptor.forClass(List.class);
|
ArgumentCaptor.forClass(List.class);
|
||||||
@@ -5827,26 +5816,24 @@ public class ConnectivityServiceTest {
|
|||||||
verify(mStatsManager, atLeastOnce()).notifyNetworkStatus(networksCaptor.capture(),
|
verify(mStatsManager, atLeastOnce()).notifyNetworkStatus(networksCaptor.capture(),
|
||||||
any(List.class), eq(defaultIface), vpnInfosCaptor.capture());
|
any(List.class), eq(defaultIface), vpnInfosCaptor.capture());
|
||||||
|
|
||||||
assertSameElementsNoDuplicates(networksCaptor.getValue().toArray(), networks);
|
assertSameElements(networksCaptor.getValue(), networks);
|
||||||
|
|
||||||
UnderlyingNetworkInfo[] infos =
|
List<UnderlyingNetworkInfo> infos = vpnInfosCaptor.getValue();
|
||||||
vpnInfosCaptor.getValue().toArray(new UnderlyingNetworkInfo[0]);
|
|
||||||
if (vpnUid != null) {
|
if (vpnUid != null) {
|
||||||
assertEquals("Should have exactly one VPN:", 1, infos.length);
|
assertEquals("Should have exactly one VPN:", 1, infos.size());
|
||||||
UnderlyingNetworkInfo info = infos[0];
|
UnderlyingNetworkInfo info = infos.get(0);
|
||||||
assertEquals("Unexpected VPN owner:", (int) vpnUid, info.getOwnerUid());
|
assertEquals("Unexpected VPN owner:", (int) vpnUid, info.getOwnerUid());
|
||||||
assertEquals("Unexpected VPN interface:", vpnIfname, info.getInterface());
|
assertEquals("Unexpected VPN interface:", vpnIfname, info.getInterface());
|
||||||
assertSameElementsNoDuplicates(underlyingIfaces,
|
assertSameElements(underlyingIfaces, info.getUnderlyingInterfaces());
|
||||||
info.getUnderlyingInterfaces().toArray(new String[0]));
|
|
||||||
} else {
|
} else {
|
||||||
assertEquals(0, infos.length);
|
assertEquals(0, infos.size());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void expectNetworkStatus(
|
private void expectNotifyNetworkStatus(
|
||||||
Network[] networks, String defaultIface) throws Exception {
|
List<Network> networks, String defaultIface) throws Exception {
|
||||||
expectNetworkStatus(networks, defaultIface, null, null, new String[0]);
|
expectNotifyNetworkStatus(networks, defaultIface, null, null, List.of());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -5854,8 +5841,8 @@ public class ConnectivityServiceTest {
|
|||||||
mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
|
mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
|
||||||
mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
|
mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
|
||||||
|
|
||||||
final Network[] onlyCell = new Network[] {mCellNetworkAgent.getNetwork()};
|
final List<Network> onlyCell = List.of(mCellNetworkAgent.getNetwork());
|
||||||
final Network[] onlyWifi = new Network[] {mWiFiNetworkAgent.getNetwork()};
|
final List<Network> onlyWifi = List.of(mWiFiNetworkAgent.getNetwork());
|
||||||
|
|
||||||
LinkProperties cellLp = new LinkProperties();
|
LinkProperties cellLp = new LinkProperties();
|
||||||
cellLp.setInterfaceName(MOBILE_IFNAME);
|
cellLp.setInterfaceName(MOBILE_IFNAME);
|
||||||
@@ -5866,7 +5853,7 @@ public class ConnectivityServiceTest {
|
|||||||
mCellNetworkAgent.connect(false);
|
mCellNetworkAgent.connect(false);
|
||||||
mCellNetworkAgent.sendLinkProperties(cellLp);
|
mCellNetworkAgent.sendLinkProperties(cellLp);
|
||||||
waitForIdle();
|
waitForIdle();
|
||||||
expectNetworkStatus(onlyCell, MOBILE_IFNAME);
|
expectNotifyNetworkStatus(onlyCell, MOBILE_IFNAME);
|
||||||
reset(mStatsManager);
|
reset(mStatsManager);
|
||||||
|
|
||||||
// Default network switch should update ifaces.
|
// Default network switch should update ifaces.
|
||||||
@@ -5874,37 +5861,37 @@ public class ConnectivityServiceTest {
|
|||||||
mWiFiNetworkAgent.sendLinkProperties(wifiLp);
|
mWiFiNetworkAgent.sendLinkProperties(wifiLp);
|
||||||
waitForIdle();
|
waitForIdle();
|
||||||
assertEquals(wifiLp, mService.getActiveLinkProperties());
|
assertEquals(wifiLp, mService.getActiveLinkProperties());
|
||||||
expectNetworkStatus(onlyWifi, WIFI_IFNAME);
|
expectNotifyNetworkStatus(onlyWifi, WIFI_IFNAME);
|
||||||
reset(mStatsManager);
|
reset(mStatsManager);
|
||||||
|
|
||||||
// Disconnect should update ifaces.
|
// Disconnect should update ifaces.
|
||||||
mWiFiNetworkAgent.disconnect();
|
mWiFiNetworkAgent.disconnect();
|
||||||
waitForIdle();
|
waitForIdle();
|
||||||
expectNetworkStatus(onlyCell, MOBILE_IFNAME);
|
expectNotifyNetworkStatus(onlyCell, MOBILE_IFNAME);
|
||||||
reset(mStatsManager);
|
reset(mStatsManager);
|
||||||
|
|
||||||
// Metered change should update ifaces
|
// Metered change should update ifaces
|
||||||
mCellNetworkAgent.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED);
|
mCellNetworkAgent.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED);
|
||||||
waitForIdle();
|
waitForIdle();
|
||||||
expectNetworkStatus(onlyCell, MOBILE_IFNAME);
|
expectNotifyNetworkStatus(onlyCell, MOBILE_IFNAME);
|
||||||
reset(mStatsManager);
|
reset(mStatsManager);
|
||||||
|
|
||||||
mCellNetworkAgent.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED);
|
mCellNetworkAgent.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED);
|
||||||
waitForIdle();
|
waitForIdle();
|
||||||
expectNetworkStatus(onlyCell, MOBILE_IFNAME);
|
expectNotifyNetworkStatus(onlyCell, MOBILE_IFNAME);
|
||||||
reset(mStatsManager);
|
reset(mStatsManager);
|
||||||
|
|
||||||
// Temp metered change shouldn't update ifaces
|
// Temp metered change shouldn't update ifaces
|
||||||
mCellNetworkAgent.addCapability(NET_CAPABILITY_TEMPORARILY_NOT_METERED);
|
mCellNetworkAgent.addCapability(NET_CAPABILITY_TEMPORARILY_NOT_METERED);
|
||||||
waitForIdle();
|
waitForIdle();
|
||||||
verify(mStatsManager, never()).notifyNetworkStatus(eq(Arrays.asList(onlyCell)),
|
verify(mStatsManager, never()).notifyNetworkStatus(eq(onlyCell),
|
||||||
any(List.class), eq(MOBILE_IFNAME), any(List.class));
|
any(List.class), eq(MOBILE_IFNAME), any(List.class));
|
||||||
reset(mStatsManager);
|
reset(mStatsManager);
|
||||||
|
|
||||||
// Roaming change should update ifaces
|
// Roaming change should update ifaces
|
||||||
mCellNetworkAgent.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING);
|
mCellNetworkAgent.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING);
|
||||||
waitForIdle();
|
waitForIdle();
|
||||||
expectNetworkStatus(onlyCell, MOBILE_IFNAME);
|
expectNotifyNetworkStatus(onlyCell, MOBILE_IFNAME);
|
||||||
reset(mStatsManager);
|
reset(mStatsManager);
|
||||||
|
|
||||||
// Test VPNs.
|
// Test VPNs.
|
||||||
@@ -5914,29 +5901,29 @@ public class ConnectivityServiceTest {
|
|||||||
mMockVpn.establishForMyUid(lp);
|
mMockVpn.establishForMyUid(lp);
|
||||||
assertUidRangesUpdatedForMyUid(true);
|
assertUidRangesUpdatedForMyUid(true);
|
||||||
|
|
||||||
final Network[] cellAndVpn = new Network[] {
|
final List<Network> cellAndVpn =
|
||||||
mCellNetworkAgent.getNetwork(), mMockVpn.getNetwork()};
|
List.of(mCellNetworkAgent.getNetwork(), mMockVpn.getNetwork());
|
||||||
|
|
||||||
// A VPN with default (null) underlying networks sets the underlying network's interfaces...
|
// A VPN with default (null) underlying networks sets the underlying network's interfaces...
|
||||||
expectNetworkStatus(cellAndVpn, MOBILE_IFNAME, Process.myUid(), VPN_IFNAME,
|
expectNotifyNetworkStatus(cellAndVpn, MOBILE_IFNAME, Process.myUid(), VPN_IFNAME,
|
||||||
new String[]{MOBILE_IFNAME});
|
List.of(MOBILE_IFNAME));
|
||||||
|
|
||||||
// ...and updates them as the default network switches.
|
// ...and updates them as the default network switches.
|
||||||
mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
|
mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
|
||||||
mWiFiNetworkAgent.connect(false);
|
mWiFiNetworkAgent.connect(false);
|
||||||
mWiFiNetworkAgent.sendLinkProperties(wifiLp);
|
mWiFiNetworkAgent.sendLinkProperties(wifiLp);
|
||||||
final Network[] onlyNull = new Network[]{null};
|
final Network[] onlyNull = new Network[]{null};
|
||||||
final Network[] wifiAndVpn = new Network[] {
|
final List<Network> wifiAndVpn =
|
||||||
mWiFiNetworkAgent.getNetwork(), mMockVpn.getNetwork()};
|
List.of(mWiFiNetworkAgent.getNetwork(), mMockVpn.getNetwork());
|
||||||
final Network[] cellAndWifi = new Network[] {
|
final List<Network> cellAndWifi =
|
||||||
mCellNetworkAgent.getNetwork(), mWiFiNetworkAgent.getNetwork()};
|
List.of(mCellNetworkAgent.getNetwork(), mWiFiNetworkAgent.getNetwork());
|
||||||
final Network[] cellNullAndWifi = new Network[] {
|
final Network[] cellNullAndWifi =
|
||||||
mCellNetworkAgent.getNetwork(), null, mWiFiNetworkAgent.getNetwork()};
|
new Network[]{mCellNetworkAgent.getNetwork(), null, mWiFiNetworkAgent.getNetwork()};
|
||||||
|
|
||||||
waitForIdle();
|
waitForIdle();
|
||||||
assertEquals(wifiLp, mService.getActiveLinkProperties());
|
assertEquals(wifiLp, mService.getActiveLinkProperties());
|
||||||
expectNetworkStatus(wifiAndVpn, WIFI_IFNAME, Process.myUid(), VPN_IFNAME,
|
expectNotifyNetworkStatus(wifiAndVpn, WIFI_IFNAME, Process.myUid(), VPN_IFNAME,
|
||||||
new String[]{WIFI_IFNAME});
|
List.of(WIFI_IFNAME));
|
||||||
reset(mStatsManager);
|
reset(mStatsManager);
|
||||||
|
|
||||||
// A VPN that sets its underlying networks passes the underlying interfaces, and influences
|
// A VPN that sets its underlying networks passes the underlying interfaces, and influences
|
||||||
@@ -5945,23 +5932,23 @@ public class ConnectivityServiceTest {
|
|||||||
// MOBILE_IFNAME even though the default network is wifi.
|
// MOBILE_IFNAME even though the default network is wifi.
|
||||||
// TODO: fix this to pass in the actual default network interface. Whether or not the VPN
|
// TODO: fix this to pass in the actual default network interface. Whether or not the VPN
|
||||||
// applies to the system server UID should not have any bearing on network stats.
|
// applies to the system server UID should not have any bearing on network stats.
|
||||||
mMockVpn.setUnderlyingNetworks(onlyCell);
|
mMockVpn.setUnderlyingNetworks(onlyCell.toArray(new Network[0]));
|
||||||
waitForIdle();
|
waitForIdle();
|
||||||
expectNetworkStatus(wifiAndVpn, MOBILE_IFNAME, Process.myUid(), VPN_IFNAME,
|
expectNotifyNetworkStatus(wifiAndVpn, MOBILE_IFNAME, Process.myUid(), VPN_IFNAME,
|
||||||
new String[]{MOBILE_IFNAME});
|
List.of(MOBILE_IFNAME));
|
||||||
reset(mStatsManager);
|
reset(mStatsManager);
|
||||||
|
|
||||||
mMockVpn.setUnderlyingNetworks(cellAndWifi);
|
mMockVpn.setUnderlyingNetworks(cellAndWifi.toArray(new Network[0]));
|
||||||
waitForIdle();
|
waitForIdle();
|
||||||
expectNetworkStatus(wifiAndVpn, MOBILE_IFNAME, Process.myUid(), VPN_IFNAME,
|
expectNotifyNetworkStatus(wifiAndVpn, MOBILE_IFNAME, Process.myUid(), VPN_IFNAME,
|
||||||
new String[]{MOBILE_IFNAME, WIFI_IFNAME});
|
List.of(MOBILE_IFNAME, WIFI_IFNAME));
|
||||||
reset(mStatsManager);
|
reset(mStatsManager);
|
||||||
|
|
||||||
// Null underlying networks are ignored.
|
// Null underlying networks are ignored.
|
||||||
mMockVpn.setUnderlyingNetworks(cellNullAndWifi);
|
mMockVpn.setUnderlyingNetworks(cellNullAndWifi);
|
||||||
waitForIdle();
|
waitForIdle();
|
||||||
expectNetworkStatus(wifiAndVpn, MOBILE_IFNAME, Process.myUid(), VPN_IFNAME,
|
expectNotifyNetworkStatus(wifiAndVpn, MOBILE_IFNAME, Process.myUid(), VPN_IFNAME,
|
||||||
new String[]{MOBILE_IFNAME, WIFI_IFNAME});
|
List.of(MOBILE_IFNAME, WIFI_IFNAME));
|
||||||
reset(mStatsManager);
|
reset(mStatsManager);
|
||||||
|
|
||||||
// If an underlying network disconnects, that interface should no longer be underlying.
|
// If an underlying network disconnects, that interface should no longer be underlying.
|
||||||
@@ -5974,8 +5961,8 @@ public class ConnectivityServiceTest {
|
|||||||
mCellNetworkAgent.disconnect();
|
mCellNetworkAgent.disconnect();
|
||||||
waitForIdle();
|
waitForIdle();
|
||||||
assertNull(mService.getLinkProperties(mCellNetworkAgent.getNetwork()));
|
assertNull(mService.getLinkProperties(mCellNetworkAgent.getNetwork()));
|
||||||
expectNetworkStatus(wifiAndVpn, MOBILE_IFNAME, Process.myUid(), VPN_IFNAME,
|
expectNotifyNetworkStatus(wifiAndVpn, MOBILE_IFNAME, Process.myUid(), VPN_IFNAME,
|
||||||
new String[]{MOBILE_IFNAME, WIFI_IFNAME});
|
List.of(MOBILE_IFNAME, WIFI_IFNAME));
|
||||||
|
|
||||||
// Confirm that we never tell NetworkStatsService that cell is no longer the underlying
|
// Confirm that we never tell NetworkStatsService that cell is no longer the underlying
|
||||||
// network for the VPN...
|
// network for the VPN...
|
||||||
@@ -6009,26 +5996,26 @@ public class ConnectivityServiceTest {
|
|||||||
// Also, for the same reason as above, the active interface passed in is null.
|
// Also, for the same reason as above, the active interface passed in is null.
|
||||||
mMockVpn.setUnderlyingNetworks(new Network[0]);
|
mMockVpn.setUnderlyingNetworks(new Network[0]);
|
||||||
waitForIdle();
|
waitForIdle();
|
||||||
expectNetworkStatus(wifiAndVpn, null);
|
expectNotifyNetworkStatus(wifiAndVpn, null);
|
||||||
reset(mStatsManager);
|
reset(mStatsManager);
|
||||||
|
|
||||||
// Specifying only a null underlying network is the same as no networks.
|
// Specifying only a null underlying network is the same as no networks.
|
||||||
mMockVpn.setUnderlyingNetworks(onlyNull);
|
mMockVpn.setUnderlyingNetworks(onlyNull);
|
||||||
waitForIdle();
|
waitForIdle();
|
||||||
expectNetworkStatus(wifiAndVpn, null);
|
expectNotifyNetworkStatus(wifiAndVpn, null);
|
||||||
reset(mStatsManager);
|
reset(mStatsManager);
|
||||||
|
|
||||||
// Specifying networks that are all disconnected is the same as specifying no networks.
|
// Specifying networks that are all disconnected is the same as specifying no networks.
|
||||||
mMockVpn.setUnderlyingNetworks(onlyCell);
|
mMockVpn.setUnderlyingNetworks(onlyCell.toArray(new Network[0]));
|
||||||
waitForIdle();
|
waitForIdle();
|
||||||
expectNetworkStatus(wifiAndVpn, null);
|
expectNotifyNetworkStatus(wifiAndVpn, null);
|
||||||
reset(mStatsManager);
|
reset(mStatsManager);
|
||||||
|
|
||||||
// Passing in null again means follow the default network again.
|
// Passing in null again means follow the default network again.
|
||||||
mMockVpn.setUnderlyingNetworks(null);
|
mMockVpn.setUnderlyingNetworks(null);
|
||||||
waitForIdle();
|
waitForIdle();
|
||||||
expectNetworkStatus(wifiAndVpn, WIFI_IFNAME, Process.myUid(), VPN_IFNAME,
|
expectNotifyNetworkStatus(wifiAndVpn, WIFI_IFNAME, Process.myUid(), VPN_IFNAME,
|
||||||
new String[]{WIFI_IFNAME});
|
List.of(WIFI_IFNAME));
|
||||||
reset(mStatsManager);
|
reset(mStatsManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user