Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f6dff3a3 authored by Neil Fuller's avatar Neil Fuller
Browse files

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
parent 8ea631ac
Loading
Loading
Loading
Loading
+13 −16
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ package com.android.server.timedetector;
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.ITimeDetectorService;
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 @@ public final class TimeDetectorService extends ITimeDetectorService.Stub
     */
    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 final class TimeDetectorService extends ITimeDetectorService.Stub
    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 @@ public final class TimeDetectorService extends ITimeDetectorService.Stub
    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 @@ public final class TimeDetectorService extends ITimeDetectorService.Stub
        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 @@ public final class TimeDetectorService extends ITimeDetectorService.Stub
        // 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 @@ public final class TimeDetectorService extends ITimeDetectorService.Stub
    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 @@ public final class TimeDetectorService extends ITimeDetectorService.Stub
    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 @@ public final class TimeDetectorService extends ITimeDetectorService.Stub
                mNtpTrustedTime.clearCachedTimeResult();
            }
        } finally {
            Binder.restoreCallingIdentity(token);
            mCallerIdentityInjector.restoreCallingIdentity(token);
        }
    }

+13 −0
Original line number Diff line number Diff line
@@ -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 @@ public interface CallerIdentityInjector {
    /** 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();

@@ -44,6 +51,12 @@ public interface CallerIdentityInjector {
        protected Real() {
        }

        @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();
+3 −6
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ package com.android.server.timezonedetector;
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.ITimeZoneDetectorService;
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 @@ public final class TimeZoneDetectorService extends ITimeZoneDetectorService.Stub
        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 @@ public final class TimeZoneDetectorService extends ITimeZoneDetectorService.Stub
    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();

+0 −3
Original line number Diff line number Diff line
@@ -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>
+5 −0
Original line number Diff line number Diff line
@@ -34,6 +34,11 @@ public class TestCallerIdentityInjector implements CallerIdentityInjector {
        mCurrentCallingUserId = userId;
    }

    @Override
    public int resolveUserId(int userId, String debugInfo) {
        return userId;
    }

    @Override
    public int getCallingUserId() {
        assertNotNull("callingUserId has been cleared", mCurrentCallingUserId);