Remove the need for permission during tests
A call to ActivityManager was not faked for tests leading to a real
permission check. There's no need for this, so it have been fixed in the
service. Some other calls that can be faked have also been switched
over to the "injector" object.
Test: atest FrameworksTimeServicesTests
Bug: 182461754
Change-Id: Ic757932fee972d5ee360023a10f6616f36da271e
diff --git a/services/core/java/com/android/server/timedetector/TimeDetectorService.java b/services/core/java/com/android/server/timedetector/TimeDetectorService.java
index d88f426..83270f6 100644
--- a/services/core/java/com/android/server/timedetector/TimeDetectorService.java
+++ b/services/core/java/com/android/server/timedetector/TimeDetectorService.java
@@ -19,7 +19,6 @@
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
-import android.app.ActivityManager;
import android.app.time.ExternalTimeSuggestion;
import android.app.time.ITimeDetectorListener;
import android.app.time.TimeCapabilitiesAndConfig;
@@ -30,7 +29,6 @@
import android.app.timedetector.ManualTimeSuggestion;
import android.app.timedetector.TelephonyTimeSuggestion;
import android.content.Context;
-import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.ParcelableException;
@@ -166,8 +164,7 @@
*/
boolean updateConfiguration(@UserIdInt int userId, @NonNull TimeConfiguration configuration) {
// Resolve constants like USER_CURRENT to the true user ID as needed.
- int resolvedUserId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
- Binder.getCallingUid(), userId, false, false, "updateConfiguration", null);
+ int resolvedUserId = mCallerIdentityInjector.resolveUserId(userId, "updateConfiguration");
enforceManageTimeDetectorPermission();
@@ -280,11 +277,11 @@
public TimeState getTimeState() {
enforceManageTimeDetectorPermission();
- final long token = Binder.clearCallingIdentity();
+ final long token = mCallerIdentityInjector.clearCallingIdentity();
try {
return mTimeDetectorStrategy.getTimeState();
} finally {
- Binder.restoreCallingIdentity(token);
+ mCallerIdentityInjector.restoreCallingIdentity(token);
}
}
@@ -295,11 +292,11 @@
void setTimeState(@NonNull TimeState timeState) {
enforceManageTimeDetectorPermission();
- final long token = Binder.clearCallingIdentity();
+ final long token = mCallerIdentityInjector.clearCallingIdentity();
try {
mTimeDetectorStrategy.setTimeState(timeState);
} finally {
- Binder.restoreCallingIdentity(token);
+ mCallerIdentityInjector.restoreCallingIdentity(token);
}
}
@@ -308,11 +305,11 @@
enforceManageTimeDetectorPermission();
Objects.requireNonNull(time);
- final long token = Binder.clearCallingIdentity();
+ final long token = mCallerIdentityInjector.clearCallingIdentity();
try {
return mTimeDetectorStrategy.confirmTime(time);
} finally {
- Binder.restoreCallingIdentity(token);
+ mCallerIdentityInjector.restoreCallingIdentity(token);
}
}
@@ -324,13 +321,13 @@
// This calls suggestManualTime() as the logic is identical, it only differs in the
// permission required, which is handled on the line above.
int userId = mCallerIdentityInjector.getCallingUserId();
- final long token = Binder.clearCallingIdentity();
+ final long token = mCallerIdentityInjector.clearCallingIdentity();
try {
final boolean bypassUserPolicyChecks = false;
return mTimeDetectorStrategy.suggestManualTime(
userId, suggestion, bypassUserPolicyChecks);
} finally {
- Binder.restoreCallingIdentity(token);
+ mCallerIdentityInjector.restoreCallingIdentity(token);
}
}
@@ -377,11 +374,11 @@
void clearLatestNetworkTime() {
enforceSuggestNetworkTimePermission();
- final long token = Binder.clearCallingIdentity();
+ final long token = mCallerIdentityInjector.clearCallingIdentity();
try {
mTimeDetectorStrategy.clearLatestNetworkSuggestion();
} finally {
- Binder.restoreCallingIdentity(token);
+ mCallerIdentityInjector.restoreCallingIdentity(token);
}
}
@@ -473,7 +470,7 @@
void clearNetworkTimeForSystemClockForTests() {
enforceSuggestNetworkTimePermission();
- final long token = Binder.clearCallingIdentity();
+ final long token = mCallerIdentityInjector.clearCallingIdentity();
try {
// TODO(b/222295093): Remove this condition once we can be sure that all uses of
// NtpTrustedTime result in a suggestion being made to the time detector.
@@ -485,7 +482,7 @@
mNtpTrustedTime.clearCachedTimeResult();
}
} finally {
- Binder.restoreCallingIdentity(token);
+ mCallerIdentityInjector.restoreCallingIdentity(token);
}
}
diff --git a/services/core/java/com/android/server/timezonedetector/CallerIdentityInjector.java b/services/core/java/com/android/server/timezonedetector/CallerIdentityInjector.java
index 1500cfa..1f1d83f 100644
--- a/services/core/java/com/android/server/timezonedetector/CallerIdentityInjector.java
+++ b/services/core/java/com/android/server/timezonedetector/CallerIdentityInjector.java
@@ -17,6 +17,7 @@
package com.android.server.timezonedetector;
import android.annotation.UserIdInt;
+import android.app.ActivityManager;
import android.os.Binder;
import android.os.UserHandle;
@@ -29,6 +30,12 @@
/** A singleton for the real implementation of {@link CallerIdentityInjector}. */
CallerIdentityInjector REAL = new Real();
+ /**
+ * A {@link ActivityManager#handleIncomingUser} call. This can be used to map the abstract
+ * user ID value USER_CURRENT to the actual user ID.
+ */
+ @UserIdInt int resolveUserId(@UserIdInt int userId, String debugInfo);
+
/** A {@link UserHandle#getCallingUserId()} call. */
@UserIdInt int getCallingUserId();
@@ -45,6 +52,12 @@
}
@Override
+ public int resolveUserId(@UserIdInt int userId, String debugName) {
+ return ActivityManager.handleIncomingUser(Binder.getCallingPid(),
+ Binder.getCallingUid(), userId, false, false, debugName, null);
+ }
+
+ @Override
public int getCallingUserId() {
return UserHandle.getCallingUserId();
}
diff --git a/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorService.java b/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorService.java
index dac4bf8..d914544 100644
--- a/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorService.java
+++ b/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorService.java
@@ -19,7 +19,6 @@
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
-import android.app.ActivityManager;
import android.app.time.ITimeZoneDetectorListener;
import android.app.time.TimeZoneCapabilitiesAndConfig;
import android.app.time.TimeZoneConfiguration;
@@ -28,7 +27,6 @@
import android.app.timezonedetector.ManualTimeZoneSuggestion;
import android.app.timezonedetector.TelephonyTimeZoneSuggestion;
import android.content.Context;
-import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
@@ -168,8 +166,8 @@
enforceManageTimeZoneDetectorPermission();
// Resolve constants like USER_CURRENT to the true user ID as needed.
- int resolvedUserId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
- Binder.getCallingUid(), userId, false, false, "getCapabilitiesAndConfig", null);
+ int resolvedUserId =
+ mCallerIdentityInjector.resolveUserId(userId, "getCapabilitiesAndConfig");
final long token = mCallerIdentityInjector.clearCallingIdentity();
try {
@@ -190,8 +188,7 @@
boolean updateConfiguration(
@UserIdInt int userId, @NonNull TimeZoneConfiguration configuration) {
// Resolve constants like USER_CURRENT to the true user ID as needed.
- int resolvedUserId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
- Binder.getCallingUid(), userId, false, false, "updateConfiguration", null);
+ int resolvedUserId = mCallerIdentityInjector.resolveUserId(userId, "updateConfiguration");
enforceManageTimeZoneDetectorPermission();
diff --git a/services/tests/timetests/AndroidManifest.xml b/services/tests/timetests/AndroidManifest.xml
index 62fbb05..a21d383 100644
--- a/services/tests/timetests/AndroidManifest.xml
+++ b/services/tests/timetests/AndroidManifest.xml
@@ -17,9 +17,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.framework.services.tests.time">
- <!-- Required for user checks -->
- <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />
-
<application>
<uses-library android:name="android.test.runner" />
</application>
diff --git a/services/tests/timetests/src/com/android/server/timezonedetector/TestCallerIdentityInjector.java b/services/tests/timetests/src/com/android/server/timezonedetector/TestCallerIdentityInjector.java
index 67b8cc2..56db9cc 100644
--- a/services/tests/timetests/src/com/android/server/timezonedetector/TestCallerIdentityInjector.java
+++ b/services/tests/timetests/src/com/android/server/timezonedetector/TestCallerIdentityInjector.java
@@ -35,6 +35,11 @@
}
@Override
+ public int resolveUserId(int userId, String debugInfo) {
+ return userId;
+ }
+
+ @Override
public int getCallingUserId() {
assertNotNull("callingUserId has been cleared", mCurrentCallingUserId);
return mCurrentCallingUserId;