diff --git a/service/src/com/android/server/ConnectivityService.java b/service/src/com/android/server/ConnectivityService.java index cd4421b56a..32504653a9 100755 --- a/service/src/com/android/server/ConnectivityService.java +++ b/service/src/com/android/server/ConnectivityService.java @@ -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 configs = new ArrayList<>(); final List 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); } } diff --git a/tests/unit/java/com/android/server/ConnectivityServiceTest.java b/tests/unit/java/com/android/server/ConnectivityServiceTest.java index a2d284b1c5..8f0a33cddd 100755 --- a/tests/unit/java/com/android/server/ConnectivityServiceTest.java +++ b/tests/unit/java/com/android/server/ConnectivityServiceTest.java @@ -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); - verify(mMockNetd).setNetworkAllowlist(any()); + 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); - verify(mMockNetd).setNetworkAllowlist(any()); + 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 */); - inOrder.verify(mMockNetd).setNetworkAllowlist(new NativeUidRangeConfig[] { config1User }); + 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 */); - inOrder.verify(mMockNetd).setNetworkAllowlist(new NativeUidRangeConfig[] { config2Users }); + 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 */); - inOrder.verify(mMockNetd).setNetworkAllowlist( - new NativeUidRangeConfig[]{cellAllAllowedConfig}); + 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 configsCaptor = ArgumentCaptor.forClass( NativeUidRangeConfig[].class); - inOrder.verify(mMockNetd).setNetworkAllowlist(configsCaptor.capture()); - assertContainsAll(List.of(configsCaptor.getValue()), - List.of(cellAllAllowedConfig, enterpriseAllAllowedConfig)); + 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 */); - inOrder.verify(mMockNetd).setNetworkAllowlist(configsCaptor.capture()); - assertContainsAll(List.of(configsCaptor.getValue()), - List.of(cellExcludeAppConfig, enterpriseAllAllowedConfig)); + 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(); - inOrder.verify(mMockNetd).setNetworkAllowlist(configsCaptor.capture()); - assertContainsAll(List.of(configsCaptor.getValue()), - List.of(cellAllAllowedConfig, enterpriseAllAllowedConfig)); + 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(); - inOrder.verify(mMockNetd).setNetworkAllowlist( - new NativeUidRangeConfig[]{cellAllAllowedConfig}); + 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(); - inOrder.verify(mMockNetd).setNetworkAllowlist(new NativeUidRangeConfig[]{}); + 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 */); - inOrder.verify(mMockNetd).setNetworkAllowlist( - new NativeUidRangeConfig[]{cellExcludeAppConfig}); + 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 configsCaptor = ArgumentCaptor.forClass( NativeUidRangeConfig[].class); - inOrder.verify(mMockNetd).setNetworkAllowlist(configsCaptor.capture()); - assertContainsAll(List.of(configsCaptor.getValue()), - List.of(enterpriseAllAllowedConfig, cellExcludeAppConfig)); + 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(); - inOrder.verify(mMockNetd).setNetworkAllowlist( - new NativeUidRangeConfig[]{cellExcludeAppConfig}); + if (SdkLevel.isAtLeastU()) { + inOrder.verify(mMockNetd).setNetworkAllowlist( + new NativeUidRangeConfig[]{cellExcludeAppConfig}); + } else { + inOrder.verify(mMockNetd, never()).setNetworkAllowlist(any()); + } mCellAgent.disconnect(); waitForIdle(); - inOrder.verify(mMockNetd).setNetworkAllowlist(new NativeUidRangeConfig[]{}); + if (SdkLevel.isAtLeastU()) { + inOrder.verify(mMockNetd).setNetworkAllowlist(new NativeUidRangeConfig[]{}); + } else { + inOrder.verify(mMockNetd, never()).setNetworkAllowlist(any()); + } } /**