Fix CarPowerManagenentService crash in car service test
- add ICarImpl.asserCallingFromSystemProcessOrSelf() to prevent
throwing exception when running car service code in car service
unit testing.
Bug: 131928300
Test: Run car service tests, atest CarServiceTest
Change-Id: I68ce1978b3f5696cc643f44c00c0d94a760808c0
diff --git a/service/src/com/android/car/ICarImpl.java b/service/src/com/android/car/ICarImpl.java
index 116919d..7728e53 100644
--- a/service/src/com/android/car/ICarImpl.java
+++ b/service/src/com/android/car/ICarImpl.java
@@ -266,6 +266,19 @@
}
}
+ /**
+ * Assert if binder call is coming from system process like system server or if it is called
+ * from its own process even if it is not system. The latter can happen in test environment.
+ * Note that car service runs as system user but test like car service test will not.
+ */
+ static void assertCallingFromSystemProcessOrSelf() {
+ int uid = Binder.getCallingUid();
+ int pid = Binder.getCallingPid();
+ if (uid != Process.SYSTEM_UID && pid != Process.myPid()) {
+ throw new SecurityException("Only allowed from system or self");
+ }
+ }
+
@Override
public IBinder getCarService(String serviceName) {
switch (serviceName) {