Home | History | Annotate | Download | only in usespermissiondiffcertapp
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.cts.usespermissiondiffcertapp;
     18 
     19 import static com.android.cts.permissiondeclareapp.UtilsProvider.ACTION_SET_INSTALLER_PACKAGE_NAME;
     20 import static com.android.cts.permissiondeclareapp.UtilsProvider.EXTRA_INSTALLER_PACKAGE_NAME;
     21 import static com.android.cts.permissiondeclareapp.UtilsProvider.EXTRA_PACKAGE_NAME;
     22 
     23 import android.content.Intent;
     24 import android.content.pm.PackageManager;
     25 import android.os.Bundle;
     26 import android.test.AndroidTestCase;
     27 
     28 import com.android.cts.permissiondeclareapp.UtilsProvider;
     29 
     30 /**
     31  * Tests that one application can and can not modify the installer package
     32  * of another application is appropriate.
     33  *
     34  * Accesses app cts/tests/appsecurity-tests/test-apps/PermissionDeclareApp/...
     35  */
     36 public class ModifyInstallerPackageTest extends AndroidTestCase {
     37     static final String OTHER_PACKAGE = "com.android.cts.permissiondeclareapp";
     38     static final String MY_PACKAGE = "com.android.cts.usespermissiondiffcertapp";
     39 
     40     PackageManager getPackageManager() {
     41         return getContext().getPackageManager();
     42     }
     43 
     44     private void call(Intent intent) {
     45         final Bundle extras = new Bundle();
     46         extras.putParcelable(Intent.EXTRA_INTENT, intent);
     47         getContext().getContentResolver().call(UtilsProvider.URI, "", "", extras);
     48     }
     49 
     50     /**
     51      * Test that we can set the installer package name.
     52      */
     53     public void testSetInstallPackage() {
     54         // Pre-condition.
     55         assertEquals(null, getPackageManager().getInstallerPackageName(OTHER_PACKAGE));
     56 
     57         getPackageManager().setInstallerPackageName(OTHER_PACKAGE, MY_PACKAGE);
     58         assertEquals(MY_PACKAGE, getPackageManager().getInstallerPackageName(OTHER_PACKAGE));
     59 
     60         // Clean up.
     61         getPackageManager().setInstallerPackageName(OTHER_PACKAGE, null);
     62         assertEquals(null, getPackageManager().getInstallerPackageName(OTHER_PACKAGE));
     63     }
     64 
     65     /**
     66      * Test that we fail if trying to set an installer package with an unknown
     67      * target package name.
     68      */
     69     public void testSetInstallPackageBadTarget() {
     70         try {
     71             getPackageManager().setInstallerPackageName("thisdoesnotexistihope!", MY_PACKAGE);
     72             fail("setInstallerPackageName did not throw IllegalArgumentException");
     73         } catch (IllegalArgumentException e) {
     74             // That's what we want!
     75         }
     76     }
     77 
     78     /**
     79      * Test that we fail if trying to set an installer package with an unknown
     80      * installer package name.
     81      */
     82     public void testSetInstallPackageBadInstaller() {
     83         try {
     84             getPackageManager().setInstallerPackageName(OTHER_PACKAGE, "thisdoesnotexistihope!");
     85             fail("setInstallerPackageName did not throw IllegalArgumentException");
     86         } catch (IllegalArgumentException e) {
     87             // That's what we want!
     88         }
     89         assertEquals(null, getPackageManager().getInstallerPackageName(OTHER_PACKAGE));
     90     }
     91 
     92     /**
     93      * Test that we fail if trying to set an installer package that is not
     94      * signed with our cert.
     95      */
     96     public void testSetInstallPackageWrongCertificate() {
     97         // Pre-condition.
     98         assertEquals(null, getPackageManager().getInstallerPackageName(OTHER_PACKAGE));
     99 
    100         try {
    101             getPackageManager().setInstallerPackageName(OTHER_PACKAGE, OTHER_PACKAGE);
    102             fail("setInstallerPackageName did not throw SecurityException");
    103         } catch (SecurityException e) {
    104             // That's what we want!
    105         }
    106 
    107         assertEquals(null, getPackageManager().getInstallerPackageName(OTHER_PACKAGE));
    108     }
    109 
    110     /**
    111      * Test that we fail if trying to set an installer package that is not
    112      * signed with the same cert as the currently set installer.
    113      */
    114     public void testSetInstallPackageConflictingInstaller() {
    115         // Pre-condition.
    116         assertEquals(null, getPackageManager().getInstallerPackageName(OTHER_PACKAGE));
    117 
    118         // Have the other package set the installer, under its cert.
    119         Intent intent = new Intent();
    120         intent.setAction(ACTION_SET_INSTALLER_PACKAGE_NAME);
    121         intent.putExtra(EXTRA_PACKAGE_NAME, OTHER_PACKAGE);
    122         intent.putExtra(EXTRA_INSTALLER_PACKAGE_NAME, OTHER_PACKAGE);
    123         call(intent);
    124 
    125         assertEquals(OTHER_PACKAGE, getPackageManager().getInstallerPackageName(OTHER_PACKAGE));
    126 
    127         try {
    128             getPackageManager().setInstallerPackageName(OTHER_PACKAGE, MY_PACKAGE);
    129             fail("setInstallerPackageName did not throw SecurityException");
    130         } catch (SecurityException e) {
    131             // That's what we want!
    132         }
    133 
    134         assertEquals(OTHER_PACKAGE, getPackageManager().getInstallerPackageName(OTHER_PACKAGE));
    135 
    136         // Now clear the installer
    137         intent.setAction(ACTION_SET_INSTALLER_PACKAGE_NAME);
    138         intent.putExtra(EXTRA_PACKAGE_NAME, OTHER_PACKAGE);
    139         intent.putExtra(EXTRA_INSTALLER_PACKAGE_NAME, (String)null);
    140         call(intent);
    141 
    142         assertEquals(null, getPackageManager().getInstallerPackageName(OTHER_PACKAGE));
    143     }
    144 }
    145