Remove only the calling user's alarms in cancelAll

When calling cancelAll, the system should not also remove alarms
set by another instance of the app running in another user.

Test: FrameworksMockingServicesTests:AlarmManagerServiceTest

Bug: 182859584
Change-Id: Id554beb6dbb77f5128b7a34c5b0cf9b9ee5dab7d
This commit is contained in:
Suprabh Shukla
2023-03-20 18:29:56 -07:00
parent 1b5a3d7c52
commit c9fd9d3769
2 changed files with 39 additions and 18 deletions

View File

@@ -3088,7 +3088,9 @@ public class AlarmManagerService extends SystemService {
+ " does not belong to the calling uid " + callingUid);
}
synchronized (mLock) {
removeLocked(callingPackage, REMOVE_REASON_ALARM_CANCELLED);
removeAlarmsInternalLocked(
a -> (a.matches(callingPackage) && a.creatorUid == callingUid),
REMOVE_REASON_ALARM_CANCELLED);
}
}

View File

@@ -2778,51 +2778,70 @@ public final class AlarmManagerServiceTest {
setTestAlarm(ELAPSED_REALTIME, mNowElapsedTest + i + 1, getNewMockPendingIntent());
}
final String otherUidPackage1 = "other.uid.package1";
final String otherUidPackage2 = "other.uid.package2";
final int otherUid = 1243;
final String otherPackage1 = "other.package1";
final String otherPackage2 = "other.package2";
final int otherAppId = 1243;
final int otherUser1 = 31;
final int otherUser2 = 8;
final int otherUid1 = UserHandle.getUid(otherUser1, otherAppId);
final int otherUid2 = UserHandle.getUid(otherUser2, otherAppId);
registerAppIds(
new String[]{TEST_CALLING_PACKAGE, otherUidPackage1, otherUidPackage2},
new Integer[]{TEST_CALLING_UID, otherUid, otherUid}
new String[]{TEST_CALLING_PACKAGE, otherPackage1, otherPackage2},
new Integer[]{UserHandle.getAppId(TEST_CALLING_UID), otherAppId, otherAppId}
);
for (int i = 0; i < 9; i++) {
setTestAlarm(ELAPSED_REALTIME, mNowElapsedTest + i + 11, 0,
getNewMockPendingIntent(otherUid, otherUidPackage1), 0, 0, otherUid,
otherUidPackage1, null);
getNewMockPendingIntent(otherUid1, otherPackage1), 0, 0, otherUid1,
otherPackage1, null);
}
for (int i = 0; i < 8; i++) {
setTestAlarm(ELAPSED_REALTIME, mNowElapsedTest + i + 20, 0,
getNewMockPendingIntent(otherUid, otherUidPackage2), 0, 0, otherUid,
otherUidPackage2, null);
getNewMockPendingIntent(otherUid1, otherPackage2), 0, 0, otherUid1,
otherPackage2, null);
}
assertEquals(27, mService.mAlarmStore.size());
for (int i = 0; i < 7; i++) {
setTestAlarm(ELAPSED_REALTIME, mNowElapsedTest + i + 28, 0,
getNewMockPendingIntent(otherUid2, otherPackage2), 0, 0, otherUid2,
otherPackage2, null);
}
assertEquals(34, mService.mAlarmStore.size());
try {
mBinder.removeAll(otherUidPackage1);
mBinder.removeAll(otherPackage1);
fail("removeAll() for wrong package did not throw SecurityException");
} catch (SecurityException se) {
// Expected
}
try {
mBinder.removeAll(otherUidPackage2);
mBinder.removeAll(otherPackage2);
fail("removeAll() for wrong package did not throw SecurityException");
} catch (SecurityException se) {
// Expected
}
mBinder.removeAll(TEST_CALLING_PACKAGE);
assertEquals(17, mService.mAlarmStore.size());
assertEquals(24, mService.mAlarmStore.size());
assertEquals(0, mService.mAlarmStore.getCount(a -> a.matches(TEST_CALLING_PACKAGE)));
mTestCallingUid = otherUid;
mBinder.removeAll(otherUidPackage1);
assertEquals(0, mService.mAlarmStore.getCount(a -> a.matches(otherUidPackage1)));
assertEquals(8, mService.mAlarmStore.getCount(a -> a.matches(otherUidPackage2)));
mTestCallingUid = otherUid1;
mBinder.removeAll(otherPackage1);
assertEquals(15, mService.mAlarmStore.size());
assertEquals(15, mService.mAlarmStore.getCount(a -> a.matches(otherPackage2)));
assertEquals(0, mService.mAlarmStore.getCount(a -> a.matches(otherPackage1)));
mBinder.removeAll(otherPackage2);
assertEquals(7, mService.mAlarmStore.size());
assertEquals(7, mService.mAlarmStore.getCount(a -> a.matches(otherPackage2)));
mTestCallingUid = otherUid2;
mBinder.removeAll(otherPackage2);
assertEquals(0, mService.mAlarmStore.size());
}
@Test