Merge "Do not remove profile network preference for different uids"

This commit is contained in:
Lorenzo Colitti
2022-05-09 09:40:52 +00:00
committed by Gerrit Code Review
4 changed files with 38 additions and 20 deletions

View File

@@ -120,8 +120,8 @@ public final class ProfileNetworkPreference implements Parcelable {
public String toString() { public String toString() {
return "ProfileNetworkPreference{" return "ProfileNetworkPreference{"
+ "mPreference=" + getPreference() + "mPreference=" + getPreference()
+ "mIncludedUids=" + mIncludedUids.toString() + "mIncludedUids=" + Arrays.toString(mIncludedUids)
+ "mExcludedUids=" + mExcludedUids.toString() + "mExcludedUids=" + Arrays.toString(mExcludedUids)
+ "mPreferenceEnterpriseId=" + mPreferenceEnterpriseId + "mPreferenceEnterpriseId=" + mPreferenceEnterpriseId
+ '}'; + '}';
} }

View File

@@ -10833,10 +10833,20 @@ public class ConnectivityService extends IConnectivityManager.Stub
private void handleSetProfileNetworkPreference( private void handleSetProfileNetworkPreference(
@NonNull final List<ProfileNetworkPreferenceList.Preference> preferenceList, @NonNull final List<ProfileNetworkPreferenceList.Preference> preferenceList,
@Nullable final IOnCompleteListener listener) { @Nullable final IOnCompleteListener listener) {
/*
* handleSetProfileNetworkPreference is always called for single user.
* preferenceList only contains preferences for different uids within the same user
* (enforced by getUidListToBeAppliedForNetworkPreference).
* Clear all the existing preferences for the user before applying new preferences.
*
*/
mProfileNetworkPreferences = mProfileNetworkPreferences.clearUser(
preferenceList.get(0).user);
for (final ProfileNetworkPreferenceList.Preference preference : preferenceList) { for (final ProfileNetworkPreferenceList.Preference preference : preferenceList) {
validateNetworkCapabilitiesOfProfileNetworkPreference(preference.capabilities); validateNetworkCapabilitiesOfProfileNetworkPreference(preference.capabilities);
mProfileNetworkPreferences = mProfileNetworkPreferences.plus(preference); mProfileNetworkPreferences = mProfileNetworkPreferences.plus(preference);
} }
removeDefaultNetworkRequestsForPreference(PREFERENCE_ORDER_PROFILE); removeDefaultNetworkRequestsForPreference(PREFERENCE_ORDER_PROFILE);
addPerAppDefaultNetworkRequests( addPerAppDefaultNetworkRequests(
createNrisFromProfileNetworkPreferences(mProfileNetworkPreferences)); createNrisFromProfileNetworkPreferences(mProfileNetworkPreferences));

View File

@@ -70,23 +70,33 @@ public class ProfileNetworkPreferenceList {
/** /**
* Returns a new object consisting of this object plus the passed preference. * Returns a new object consisting of this object plus the passed preference.
* *
* If a preference already exists for the same user, it will be replaced by the passed * It is not expected that unwanted preference already exists for the same user.
* preference. Passing a Preference object containing a null capabilities object is equivalent * All preferences for the user that were previously configured should be cleared before
* to (and indeed, implemented as) removing the preference for this user. * adding a new preference.
* Passing a Preference object containing a null capabilities object is equivalent
* to removing the preference for this user.
*/ */
public ProfileNetworkPreferenceList plus(@NonNull final Preference pref) { public ProfileNetworkPreferenceList plus(@NonNull final Preference pref) {
final ArrayList<Preference> newPrefs = new ArrayList<>(); final ArrayList<Preference> newPrefs = new ArrayList<>(preferences);
for (final Preference existingPref : preferences) {
if (!existingPref.user.equals(pref.user)) {
newPrefs.add(existingPref);
}
}
if (null != pref.capabilities) { if (null != pref.capabilities) {
newPrefs.add(pref); newPrefs.add(pref);
} }
return new ProfileNetworkPreferenceList(newPrefs); return new ProfileNetworkPreferenceList(newPrefs);
} }
/**
* Remove all preferences corresponding to a user.
*/
public ProfileNetworkPreferenceList clearUser(UserHandle user) {
final ArrayList<Preference> newPrefs = new ArrayList<>();
for (final Preference existingPref : preferences) {
if (!existingPref.user.equals(user)) {
newPrefs.add(existingPref);
}
}
return new ProfileNetworkPreferenceList(newPrefs);
}
public boolean isEmpty() { public boolean isEmpty() {
return preferences.isEmpty(); return preferences.isEmpty();
} }

View File

@@ -14630,7 +14630,7 @@ public class ConnectivityServiceTest {
profileNetworkPreferenceBuilder.setPreference( profileNetworkPreferenceBuilder.setPreference(
PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK); PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK);
profileNetworkPreferenceBuilder.setPreferenceEnterpriseId( profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(
NetworkCapabilities.NET_ENTERPRISE_ID_2); NET_ENTERPRISE_ID_2);
registerDefaultNetworkCallbacks(); registerDefaultNetworkCallbacks();
testPreferenceForUserNetworkUpDownForGivenPreference( testPreferenceForUserNetworkUpDownForGivenPreference(
profileNetworkPreferenceBuilder.build(), true, profileNetworkPreferenceBuilder.build(), true,
@@ -14730,14 +14730,13 @@ public class ConnectivityServiceTest {
workAgent2.getNetwork().netId, workAgent2.getNetwork().netId,
uidRangeFor(testHandle, profileNetworkPreferenceBuilder2.build()), uidRangeFor(testHandle, profileNetworkPreferenceBuilder2.build()),
PREFERENCE_ORDER_PROFILE)); PREFERENCE_ORDER_PROFILE));
// BUG: the second preference silently replaces the first because they are verify(mMockNetd).networkAddUidRangesParcel(new NativeUidRangeConfig(
// both for testHandle.
verify(mMockNetd, never()).networkAddUidRangesParcel(new NativeUidRangeConfig(
workAgent1.getNetwork().netId, workAgent1.getNetwork().netId,
uidRangeFor(testHandle, profileNetworkPreferenceBuilder1.build()), uidRangeFor(testHandle, profileNetworkPreferenceBuilder1.build()),
PREFERENCE_ORDER_PROFILE)); PREFERENCE_ORDER_PROFILE));
assertNoCallbacks(mSystemDefaultNetworkCallback, mDefaultNetworkCallback, appCb1); assertNoCallbacks(mSystemDefaultNetworkCallback, mDefaultNetworkCallback);
appCb1.expectAvailableCallbacksValidated(workAgent1);
appCb2.expectAvailableCallbacksValidated(workAgent2); appCb2.expectAvailableCallbacksValidated(workAgent2);
// Set preferences for testHandle to map testWorkProfileAppUid3 to // Set preferences for testHandle to map testWorkProfileAppUid3 to
@@ -14760,16 +14759,15 @@ public class ConnectivityServiceTest {
workAgent2.getNetwork().netId, workAgent2.getNetwork().netId,
uidRangeFor(testHandle, profileNetworkPreferenceBuilder2.build()), uidRangeFor(testHandle, profileNetworkPreferenceBuilder2.build()),
PREFERENCE_ORDER_PROFILE)); PREFERENCE_ORDER_PROFILE));
// BUG: the second preference should also get removed here unless it was silently verify(mMockNetd).networkRemoveUidRangesParcel(new NativeUidRangeConfig(
// discarded
verify(mMockNetd, never()).networkRemoveUidRangesParcel(new NativeUidRangeConfig(
workAgent1.getNetwork().netId, workAgent1.getNetwork().netId,
uidRangeFor(testHandle, profileNetworkPreferenceBuilder1.build()), uidRangeFor(testHandle, profileNetworkPreferenceBuilder1.build()),
PREFERENCE_ORDER_PROFILE)); PREFERENCE_ORDER_PROFILE));
assertNoCallbacks(mSystemDefaultNetworkCallback, mDefaultNetworkCallback, appCb1); assertNoCallbacks(mSystemDefaultNetworkCallback, mDefaultNetworkCallback);
appCb3.expectAvailableCallbacksValidated(workAgent1); appCb3.expectAvailableCallbacksValidated(workAgent1);
appCb2.expectAvailableCallbacksValidated(mCellNetworkAgent); appCb2.expectAvailableCallbacksValidated(mCellNetworkAgent);
appCb1.expectAvailableCallbacksValidated(mCellNetworkAgent);
// Set the preferences for testHandle to default. // Set the preferences for testHandle to default.
ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder = ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder =