1/*
2 * Copyright (C) 2006 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
17package com.android.framework.permission.tests;
18
19import android.app.PackageInstallObserver;
20import android.content.pm.PackageManager;
21import android.test.AndroidTestCase;
22import android.test.suitebuilder.annotation.SmallTest;
23
24/**
25 * Verify PackageManager api's that require specific permissions.
26 */
27public class PmPermissionsTests extends AndroidTestCase {
28    private PackageManager mPm;
29    private String mPkgName = "com.android.framework.permission.tests";
30
31    @Override
32    protected void setUp() throws Exception {
33        super.setUp();
34        mPm = getContext().getPackageManager();
35    }
36
37    /*
38     * This test verifies that PackageManger.getPackageSizeInfo enforces permission
39     * android.permission.GET_PACKAGE_SIZE
40     */
41    @SmallTest
42    public void testGetPackageSize() {
43        try {
44            mPm.getPackageSizeInfo(mPkgName, null);
45            fail("PackageManager.getPackageSizeInfo" +
46                    "did not throw SecurityException as expected");
47        } catch (SecurityException e) {
48            // expected
49        }
50    }
51
52    /*
53     * This test verifies that PackageManger.DeleteApplicationCacheFiles enforces permission
54     * android.permission.DELETE_CACHE_FILES
55     */
56    @SmallTest
57    public void testDeleteApplicationCacheFiles() {
58        try {
59            mPm.deleteApplicationCacheFiles(mPkgName, null);
60            fail("PackageManager.deleteApplicationCacheFiles" +
61                    "did not throw SecurityException as expected");
62        } catch (SecurityException e) {
63            // expected
64        }
65    }
66
67    /*
68     * This test verifies that PackageManger.installPackage enforces permission
69     * android.permission.INSTALL_PACKAGES
70     */
71    private class TestInstallObserver extends PackageInstallObserver {
72    }
73
74    @SmallTest
75    public void testInstallPackage() {
76        TestInstallObserver observer = new TestInstallObserver();
77        try {
78            mPm.installPackage(null, observer, 0, null);
79            fail("PackageManager.installPackage" +
80                    "did not throw SecurityException as expected");
81        } catch (SecurityException e) {
82            // expected
83        }
84    }
85
86    /*
87     * This test verifies that PackageManger.freeStorage
88     * enforces permission android.permission.CLEAR_APP_CACHE
89     */
90    @SmallTest
91    public void testFreeStorage1() {
92        try {
93            mPm.freeStorage(100000, null);
94            fail("PackageManager.freeStorage " +
95                   "did not throw SecurityException as expected");
96        } catch (SecurityException e) {
97            // expected
98        }
99    }
100
101    /*
102     * This test verifies that PackageManger.freeStorageAndNotify
103     * enforces permission android.permission.CLEAR_APP_CACHE
104     */
105    @SmallTest
106    public void testFreeStorage2() {
107        try {
108            mPm.freeStorageAndNotify(100000, null);
109            fail("PackageManager.freeStorageAndNotify" +
110                    " did not throw SecurityException as expected");
111        } catch (SecurityException e) {
112            // expected
113        }
114    }
115
116    /*
117     * This test verifies that PackageManger.clearApplicationUserData
118     * enforces permission android.permission.CLEAR_APP_USER_DATA
119     */
120    @SmallTest
121    public void testClearApplicationUserData() {
122        try {
123            mPm.clearApplicationUserData(mPkgName, null);
124            fail("PackageManager.clearApplicationUserData" +
125                    "did not throw SecurityException as expected");
126        } catch (SecurityException e) {
127            // expected
128        }
129    }
130
131    /*
132     * This test verifies that PackageManger.deletePackage
133     * enforces permission android.permission.DELETE_PACKAGES
134     */
135    @SmallTest
136    public void testDeletePackage() {
137        try {
138            mPm.deletePackage(mPkgName, null, 0);
139            fail("PackageManager.deletePackage" +
140                   "did not throw SecurityException as expected");
141        } catch (SecurityException e) {
142            // expected
143        }
144    }
145}