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

Commit fc901157 authored by Lorenzo Colitti's avatar Lorenzo Colitti
Browse files

Properly test TrafficController permissions on package updates.

The current test we have for a package changing TrafficController
permissions (i.e., INTERNET or UPDATE_DEVICE_STATS permissions)
is not realistic.

This is because the test simulates the update by removing and
adding the package. But TrafficController permissions are not
driven by broadcasts, they are driven by PackageListObserver,
and when an app is updated, PackageListObserver does not see a
remove and and add, but a single change event.

Add a new test for this.

Bug: 137864893
Test: new unit test added
Change-Id: I691b928f57c79c19dfb6ddb01983d55ef305c472
parent e6901bc8
Loading
Loading
Loading
Loading
+22 −3
Original line number Diff line number Diff line
@@ -561,11 +561,17 @@ public class PermissionMonitorTest {
        mNetdServiceMonitor.expectPermission(INetd.PERMISSION_NONE, new int[]{SYSTEM_UID1});
    }

    private PackageInfo addPackage(String packageName, int uid, String[] permissions)
    private PackageInfo setPackagePermissions(String packageName, int uid, String[] permissions)
            throws Exception {
        PackageInfo packageInfo = packageInfoWithPermissions(permissions, PARTITION_SYSTEM);
        when(mPackageManager.getPackageInfo(eq(packageName), anyInt())).thenReturn(packageInfo);
        when(mPackageManager.getPackagesForUid(eq(uid))).thenReturn(new String[]{packageName});
        return packageInfo;
    }

    private PackageInfo addPackage(String packageName, int uid, String[] permissions)
            throws Exception {
        PackageInfo packageInfo = setPackagePermissions(packageName, uid, permissions);
        mObserver.onPackageAdded(packageName, uid);
        return packageInfo;
    }
@@ -616,14 +622,13 @@ public class PermissionMonitorTest {
    }

    @Test
    public void testPackageUpdate() throws Exception {
    public void testPackageRemoveThenAdd() throws Exception {
        final NetdServiceMonitor mNetdServiceMonitor = new NetdServiceMonitor(mNetdService);

        addPackage(MOCK_PACKAGE1, MOCK_UID1, new String[] {INTERNET, UPDATE_DEVICE_STATS});
        mNetdServiceMonitor.expectPermission(INetd.PERMISSION_INTERNET
                | INetd.PERMISSION_UPDATE_DEVICE_STATS, new int[]{MOCK_UID1});

        // Remove and install the same package to simulate the update action
        when(mPackageManager.getPackagesForUid(MOCK_UID1)).thenReturn(new String[]{});
        mObserver.onPackageRemoved(MOCK_PACKAGE1, MOCK_UID1);
        mNetdServiceMonitor.expectPermission(INetd.PERMISSION_UNINSTALLED, new int[]{MOCK_UID1});
@@ -632,6 +637,20 @@ public class PermissionMonitorTest {
        mNetdServiceMonitor.expectPermission(INetd.PERMISSION_INTERNET, new int[]{MOCK_UID1});
    }

    @Test
    public void testPackageUpdate() throws Exception {
        final NetdServiceMonitor mNetdServiceMonitor = new NetdServiceMonitor(mNetdService);

        addPackage(MOCK_PACKAGE1, MOCK_UID1, new String[] {});
        mNetdServiceMonitor.expectPermission(INetd.PERMISSION_NONE, new int[]{MOCK_UID1});

        // When updating a package, the broadcast receiver gets two broadcasts (a remove and then an
        // add), but the observer sees only one callback (an update).
        setPackagePermissions(MOCK_PACKAGE1, MOCK_UID1, new String[] {INTERNET});
        mObserver.onPackageChanged(MOCK_PACKAGE1, MOCK_UID1);
        mNetdServiceMonitor.expectPermission(INetd.PERMISSION_INTERNET, new int[]{MOCK_UID1});
    }

    @Test
    public void testPackageUninstallWithMultiplePackages() throws Exception {
        final NetdServiceMonitor mNetdServiceMonitor = new NetdServiceMonitor(mNetdService);