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

Commit 3752c462 authored by Jorim Jaggi's avatar Jorim Jaggi Committed by Andreas Gampe
Browse files

Fix BackgroundDexOptServiceIntegrationTests

- Ensure that calling uid is shell.
- Clear calling identity such that we don't get permission
failures when calling getCurrentUser()

Test: self
Exempt-From-Owner-Approval: cherry-pick
Merged-In: Ifbaceb47edbbc4a6b002d49411ca4635ffc33a08
Change-Id: Ifbaceb47edbbc4a6b002d49411ca4635ffc33a08
Fixes: 111798412
parent 9cbb19d0
Loading
Loading
Loading
Loading
+21 −1
Original line number Diff line number Diff line
@@ -9004,6 +9004,20 @@ public class PackageManagerService extends IPackageManager.Stub
        }
    }
    /**
     * Enforces that only the system UID or shell's UID can call a method exposed
     * via Binder.
     *
     * @param message used as message if SecurityException is thrown
     * @throws SecurityException if the caller is not system or shell
     */
    private static void enforceSystemOrShell(String message) {
        final int uid = Binder.getCallingUid();
        if (uid != Process.SYSTEM_UID && uid != Process.SHELL_UID) {
            throw new SecurityException(message);
        }
    }
    @Override
    public void performFstrimIfNeeded() {
        enforceSystemOrRoot("Only the system can request fstrim");
@@ -9498,7 +9512,13 @@ public class PackageManagerService extends IPackageManager.Stub
        if (getInstantAppPackageName(Binder.getCallingUid()) != null) {
            return false;
        }
        enforceSystemOrShell("runBackgroundDexoptJob");
        final long identity = Binder.clearCallingIdentity();
        try {
            return BackgroundDexOptService.runIdleOptimizationsNow(this, mContext, packageNames);
        } finally {
            Binder.restoreCallingIdentity(identity);
        }
    }
    private static List<SharedLibraryInfo> findSharedLibraries(PackageParser.Package p) {
+13 −20
Original line number Diff line number Diff line
@@ -17,8 +17,10 @@
package com.android.server.pm;

import android.app.AlarmManager;
import android.app.UiAutomation;
import android.content.Context;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.os.SystemProperties;
import android.os.storage.StorageManager;
import android.support.test.InstrumentationRegistry;
@@ -34,6 +36,7 @@ import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -141,27 +144,17 @@ public final class BackgroundDexOptServiceIntegrationTests {
    // Run the command and return the stdout.
    private static String runShellCommand(String cmd) throws IOException {
        Log.i(TAG, String.format("running command: '%s'", cmd));
        long startTime = System.nanoTime();
        Process p = Runtime.getRuntime().exec(cmd);
        int res;
        try {
            res = p.waitFor();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        String stdout = inputStreamToString(p.getInputStream());
        String stderr = inputStreamToString(p.getErrorStream());
        long elapsedTime = System.nanoTime() - startTime;
        Log.i(TAG, String.format("ran command: '%s' in %d ms with return code %d", cmd,
                TimeUnit.NANOSECONDS.toMillis(elapsedTime), res));
        Log.i(TAG, "stdout");
        Log.i(TAG, stdout);
        Log.i(TAG, "stderr");
        Log.i(TAG, stderr);
        if (res != 0) {
            throw new RuntimeException(String.format("failed command: '%s'", cmd));
        }
        return stdout;
        ParcelFileDescriptor pfd = InstrumentationRegistry.getInstrumentation().getUiAutomation()
                .executeShellCommand(cmd);
        byte[] buf = new byte[512];
        int bytesRead;
        FileInputStream fis = new ParcelFileDescriptor.AutoCloseInputStream(pfd);
        StringBuilder stdout = new StringBuilder();
        while ((bytesRead = fis.read(buf)) != -1) {
            stdout.append(new String(buf, 0, bytesRead));
        }
        fis.close();
        return stdout.toString();
    }

    // Run the command and return the stdout split by lines.