Address comments at aosp/2361702

Test: 1. Patch local built module on T device
      2. atest ConnectivityCoverageTests:android.net.connectivity.com.android.server.ConnectivityServiceTest
Fix: 267968887
Change-Id: I86e5be9a0cee0621ff9ef0996d56a51fc5f408c2
This commit is contained in:
Junyu Lai
2023-02-20 15:36:54 +08:00
parent 05600c129e
commit c53a169a90
2 changed files with 88 additions and 27 deletions

View File

@@ -8802,6 +8802,9 @@ public class ConnectivityService extends IConnectivityManager.Stub
}
private void updateProfileAllowedNetworks() {
// Netd command is not implemented before U.
if (!SdkLevel.isAtLeastU()) return;
ensureRunningOnConnectivityServiceThread();
final ArrayList<NativeUidRangeConfig> configs = new ArrayList<>();
final List<UserHandle> users = mContext.getSystemService(UserManager.class)
@@ -8832,8 +8835,10 @@ public class ConnectivityService extends IConnectivityManager.Stub
mNetd.setNetworkAllowlist(configs.toArray(new NativeUidRangeConfig[0]));
} catch (ServiceSpecificException e) {
// Has the interface disappeared since the network was built?
Log.wtf(TAG, "Unexpected ServiceSpecificException", e);
} catch (RemoteException e) {
// Netd died. This usually causes a runtime restart anyway.
// Netd died. This will cause a runtime restart anyway.
Log.wtf(TAG, "Unexpected RemoteException", e);
}
}

View File

@@ -10420,7 +10420,11 @@ public class ConnectivityServiceTest {
verify(mMockNetd, times(1)).idletimerRemoveInterface(eq(MOBILE_IFNAME), anyInt(),
eq(Integer.toString(TRANSPORT_CELLULAR)));
verify(mMockNetd).networkDestroy(cellNetId);
if (SdkLevel.isAtLeastU()) {
verify(mMockNetd).setNetworkAllowlist(any());
} else {
verify(mMockNetd, never()).setNetworkAllowlist(any());
}
verifyNoMoreInteractions(mMockNetd);
verifyNoMoreInteractions(mClatCoordinator);
reset(mMockNetd);
@@ -10461,7 +10465,11 @@ public class ConnectivityServiceTest {
verify(mMockNetd).idletimerRemoveInterface(eq(MOBILE_IFNAME), anyInt(),
eq(Integer.toString(TRANSPORT_CELLULAR)));
verify(mMockNetd).networkDestroy(cellNetId);
if (SdkLevel.isAtLeastU()) {
verify(mMockNetd).setNetworkAllowlist(any());
} else {
verify(mMockNetd, never()).setNetworkAllowlist(any());
}
verifyNoMoreInteractions(mMockNetd);
verifyNoMoreInteractions(mClatCoordinator);
@@ -15771,7 +15779,11 @@ public class ConnectivityServiceTest {
mCellAgent.getNetwork().netId,
toUidRangeStableParcels(allowedRanges),
0 /* subPriority */);
if (SdkLevel.isAtLeastU()) {
inOrder.verify(mMockNetd).setNetworkAllowlist(new NativeUidRangeConfig[]{config1User});
} else {
inOrder.verify(mMockNetd, never()).setNetworkAllowlist(any());
}
doReturn(asList(PRIMARY_USER_HANDLE, SECONDARY_USER_HANDLE))
.when(mUserManager).getUserHandles(anyBoolean());
@@ -15785,7 +15797,11 @@ public class ConnectivityServiceTest {
mCellAgent.getNetwork().netId,
toUidRangeStableParcels(allowedRanges),
0 /* subPriority */);
if (SdkLevel.isAtLeastU()) {
inOrder.verify(mMockNetd).setNetworkAllowlist(new NativeUidRangeConfig[]{config2Users});
} else {
inOrder.verify(mMockNetd, never()).setNetworkAllowlist(any());
}
}
@Test
@@ -15812,8 +15828,12 @@ public class ConnectivityServiceTest {
mCellAgent.getNetwork().netId,
allowAllUidRangesParcel,
0 /* subPriority */);
if (SdkLevel.isAtLeastU()) {
inOrder.verify(mMockNetd).setNetworkAllowlist(
new NativeUidRangeConfig[]{cellAllAllowedConfig});
} else {
inOrder.verify(mMockNetd, never()).setNetworkAllowlist(any());
}
// Verify the same uid ranges are also applied for enterprise network.
final TestNetworkAgentWrapper enterpriseAgent = makeEnterpriseNetworkAgent(
@@ -15827,9 +15847,13 @@ public class ConnectivityServiceTest {
// making the order of the list undeterministic. Thus, verify this in order insensitive way.
final ArgumentCaptor<NativeUidRangeConfig[]> configsCaptor = ArgumentCaptor.forClass(
NativeUidRangeConfig[].class);
if (SdkLevel.isAtLeastU()) {
inOrder.verify(mMockNetd).setNetworkAllowlist(configsCaptor.capture());
assertContainsAll(List.of(configsCaptor.getValue()),
List.of(cellAllAllowedConfig, enterpriseAllAllowedConfig));
} else {
inOrder.verify(mMockNetd, never()).setNetworkAllowlist(any());
}
// Setup profile preference which only applies to test app uid on the managed profile.
ProfileNetworkPreference.Builder prefBuilder = new ProfileNetworkPreference.Builder();
@@ -15857,24 +15881,36 @@ public class ConnectivityServiceTest {
mCellAgent.getNetwork().netId,
excludeAppRangesParcel,
0 /* subPriority */);
if (SdkLevel.isAtLeastU()) {
inOrder.verify(mMockNetd).setNetworkAllowlist(configsCaptor.capture());
assertContainsAll(List.of(configsCaptor.getValue()),
List.of(cellExcludeAppConfig, enterpriseAllAllowedConfig));
} else {
inOrder.verify(mMockNetd, never()).setNetworkAllowlist(any());
}
// Verify unset by giving all allowed set for all users when the preference got removed.
mCm.setProfileNetworkPreference(testHandle, PROFILE_NETWORK_PREFERENCE_ENTERPRISE,
r -> r.run(), listener);
listener.expectOnComplete();
if (SdkLevel.isAtLeastU()) {
inOrder.verify(mMockNetd).setNetworkAllowlist(configsCaptor.capture());
assertContainsAll(List.of(configsCaptor.getValue()),
List.of(cellAllAllowedConfig, enterpriseAllAllowedConfig));
} else {
inOrder.verify(mMockNetd, never()).setNetworkAllowlist(any());
}
// Verify issuing with cellular set only when a network with enterprise capability
// disconnects.
enterpriseAgent.disconnect();
waitForIdle();
if (SdkLevel.isAtLeastU()) {
inOrder.verify(mMockNetd).setNetworkAllowlist(
new NativeUidRangeConfig[]{cellAllAllowedConfig});
} else {
inOrder.verify(mMockNetd, never()).setNetworkAllowlist(any());
}
}
@Test
@@ -15894,7 +15930,11 @@ public class ConnectivityServiceTest {
List.of(prefBuilder.build()),
r -> r.run(), listener);
listener.expectOnComplete();
if (SdkLevel.isAtLeastU()) {
inOrder.verify(mMockNetd).setNetworkAllowlist(new NativeUidRangeConfig[]{});
} else {
inOrder.verify(mMockNetd, never()).setNetworkAllowlist(any());
}
// Start with 1 default network, which should be restricted since the blocking
// preference is already set.
@@ -15918,8 +15958,12 @@ public class ConnectivityServiceTest {
mCellAgent.getNetwork().netId,
excludeAppRangesParcel,
0 /* subPriority */);
if (SdkLevel.isAtLeastU()) {
inOrder.verify(mMockNetd).setNetworkAllowlist(
new NativeUidRangeConfig[]{cellExcludeAppConfig});
} else {
inOrder.verify(mMockNetd, never()).setNetworkAllowlist(any());
}
// Verify enterprise network is not blocked for test app.
final TestNetworkAgentWrapper enterpriseAgent = makeEnterpriseNetworkAgent(
@@ -15938,19 +15982,31 @@ public class ConnectivityServiceTest {
// making the order of the list undeterministic. Thus, verify this in order insensitive way.
final ArgumentCaptor<NativeUidRangeConfig[]> configsCaptor = ArgumentCaptor.forClass(
NativeUidRangeConfig[].class);
if (SdkLevel.isAtLeastU()) {
inOrder.verify(mMockNetd).setNetworkAllowlist(configsCaptor.capture());
assertContainsAll(List.of(configsCaptor.getValue()),
List.of(enterpriseAllAllowedConfig, cellExcludeAppConfig));
} else {
inOrder.verify(mMockNetd, never()).setNetworkAllowlist(any());
}
// Verify issuing with cellular set only when enterprise network disconnects.
enterpriseAgent.disconnect();
waitForIdle();
if (SdkLevel.isAtLeastU()) {
inOrder.verify(mMockNetd).setNetworkAllowlist(
new NativeUidRangeConfig[]{cellExcludeAppConfig});
} else {
inOrder.verify(mMockNetd, never()).setNetworkAllowlist(any());
}
mCellAgent.disconnect();
waitForIdle();
if (SdkLevel.isAtLeastU()) {
inOrder.verify(mMockNetd).setNetworkAllowlist(new NativeUidRangeConfig[]{});
} else {
inOrder.verify(mMockNetd, never()).setNetworkAllowlist(any());
}
}
/**