blob: 0244425359314ab673017ee69cf2c8e7ffaf624c [file] [log] [blame]
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -08001/*
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
Brett Chabota3de7452010-03-25 13:49:26 -070017package android.content.pm;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -080018
Elliott Hughes34385d32014-04-28 11:11:32 -070019import static android.system.OsConstants.*;
Kenny Root7e921a12012-09-08 22:02:21 -070020
Brett Chabota3de7452010-03-25 13:49:26 -070021import com.android.frameworks.coretests.R;
Suchi Amalapurapu089262d2010-03-10 14:19:21 -080022import com.android.internal.content.PackageHelper;
23
Christopher Tatef1977b42014-03-24 16:25:51 -070024import android.app.PackageInstallObserver;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -080025import android.content.BroadcastReceiver;
26import android.content.Context;
27import android.content.Intent;
28import android.content.IntentFilter;
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +020029import android.content.pm.PackageInfo;
Kenny Root1683afa2011-01-07 14:27:50 -080030import android.content.pm.PackageManager;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -080031import android.content.pm.PackageManager.NameNotFoundException;
Jeff Sharkeyc4858a22014-06-16 10:51:20 -070032import android.content.pm.PackageParser.PackageParserException;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -080033import android.content.res.Resources;
34import android.content.res.Resources.NotFoundException;
Brett Chabota3de7452010-03-25 13:49:26 -070035import android.net.Uri;
Christopher Tatef1977b42014-03-24 16:25:51 -070036import android.os.Bundle;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -080037import android.os.Environment;
Brett Chabota3de7452010-03-25 13:49:26 -070038import android.os.FileUtils;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080039import android.os.IBinder;
Kenny Root7e921a12012-09-08 22:02:21 -070040import android.os.Process;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -080041import android.os.RemoteException;
42import android.os.ServiceManager;
43import android.os.StatFs;
Kenny Root7e921a12012-09-08 22:02:21 -070044import android.os.SystemClock;
Kenny Roote15bdc22012-09-16 15:45:38 -070045import android.os.UserManager;
Brett Chabota3de7452010-03-25 13:49:26 -070046import android.os.storage.IMountService;
47import android.os.storage.StorageListener;
48import android.os.storage.StorageManager;
49import android.os.storage.StorageResultCode;
Suchi Amalapurapu117818e2010-02-09 03:45:40 -080050import android.provider.Settings;
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -080051import android.provider.Settings.SettingNotFoundException;
Elliott Hughes34385d32014-04-28 11:11:32 -070052import android.system.ErrnoException;
Elliott Hughes97901902014-04-28 12:40:12 -070053import android.system.Os;
Elliott Hughes34385d32014-04-28 11:11:32 -070054import android.system.StructStat;
Brett Chabota3de7452010-03-25 13:49:26 -070055import android.test.AndroidTestCase;
Brett Chabotf76c56b2010-07-26 17:28:17 -070056import android.test.suitebuilder.annotation.LargeTest;
Kenny Root0aaa0d92011-09-12 16:42:55 -070057import android.test.suitebuilder.annotation.SmallTest;
Brett Chabota3de7452010-03-25 13:49:26 -070058import android.util.DisplayMetrics;
59import android.util.Log;
60
61import java.io.File;
Kenny Root6a6b0072010-10-07 16:46:10 -070062import java.io.IOException;
Brett Chabota3de7452010-03-25 13:49:26 -070063import java.io.InputStream;
dcashman55b10782014-04-09 14:20:38 -070064import java.util.HashSet;
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +020065import java.util.List;
dcashman55b10782014-04-09 14:20:38 -070066import java.util.Set;
67
Kenny Root7e921a12012-09-08 22:02:21 -070068import java.util.concurrent.CountDownLatch;
69import java.util.concurrent.TimeUnit;
70
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -080071public class PackageManagerTests extends AndroidTestCase {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080072 private static final boolean localLOGV = true;
Kenny Root25c925e2012-09-08 22:02:21 -070073
74 public static final String TAG = "PackageManagerTests";
75
76 public final long MAX_WAIT_TIME = 25 * 1000;
77
78 public final long WAIT_TIME_INCR = 5 * 1000;
79
Kenny Rootddbe50d2012-09-06 13:18:37 -070080 private static final String APP_LIB_DIR_PREFIX = "/data/app-lib/";
81
Kenny Root25c925e2012-09-08 22:02:21 -070082 private static final String SECURE_CONTAINERS_PREFIX = "/mnt/asec/";
83
Suchi Amalapurapu089262d2010-03-10 14:19:21 -080084 private static final int APP_INSTALL_AUTO = PackageHelper.APP_INSTALL_AUTO;
Kenny Root25c925e2012-09-08 22:02:21 -070085
Suchi Amalapurapu089262d2010-03-10 14:19:21 -080086 private static final int APP_INSTALL_DEVICE = PackageHelper.APP_INSTALL_INTERNAL;
Kenny Root25c925e2012-09-08 22:02:21 -070087
Suchi Amalapurapu089262d2010-03-10 14:19:21 -080088 private static final int APP_INSTALL_SDCARD = PackageHelper.APP_INSTALL_EXTERNAL;
Kenny Root25c925e2012-09-08 22:02:21 -070089
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -070090 private boolean mOrigState;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080091
92 void failStr(String errMsg) {
Kenny Root25c925e2012-09-08 22:02:21 -070093 Log.w(TAG, "errMsg=" + errMsg);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080094 fail(errMsg);
95 }
Kenny Root25c925e2012-09-08 22:02:21 -070096
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080097 void failStr(Exception e) {
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -070098 failStr(e.getMessage());
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080099 }
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800100
101 @Override
102 protected void setUp() throws Exception {
103 super.setUp();
Kenny Rootd7b421b2010-08-05 08:40:00 -0700104 mOrigState = checkMediaState(Environment.MEDIA_MOUNTED);
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700105 if (!mountMedia()) {
106 Log.i(TAG, "sdcard not mounted? Some of these tests might fail");
107 }
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700108 }
109
110 @Override
111 protected void tearDown() throws Exception {
112 // Restore media state.
Kenny Rootd7b421b2010-08-05 08:40:00 -0700113 boolean newState = checkMediaState(Environment.MEDIA_MOUNTED);
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700114 if (newState != mOrigState) {
115 if (mOrigState) {
Kenny Rootd7b421b2010-08-05 08:40:00 -0700116 mountMedia();
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700117 } else {
Kenny Rootd7b421b2010-08-05 08:40:00 -0700118 unmountMedia();
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700119 }
120 }
121 super.tearDown();
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800122 }
123
Christopher Tatef1977b42014-03-24 16:25:51 -0700124 private class TestInstallObserver extends PackageInstallObserver {
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800125 public int returnCode;
Kenny Root25c925e2012-09-08 22:02:21 -0700126
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800127 private boolean doneFlag = false;
128
Christopher Tatef1977b42014-03-24 16:25:51 -0700129 public void packageInstalled(String packageName, Bundle extras, int returnCode) {
Kenny Root25c925e2012-09-08 22:02:21 -0700130 synchronized (this) {
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800131 this.returnCode = returnCode;
132 doneFlag = true;
133 notifyAll();
134 }
135 }
136
137 public boolean isDone() {
138 return doneFlag;
139 }
140 }
141
142 abstract class GenericReceiver extends BroadcastReceiver {
143 private boolean doneFlag = false;
Kenny Root25c925e2012-09-08 22:02:21 -0700144
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800145 boolean received = false;
Kenny Root25c925e2012-09-08 22:02:21 -0700146
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800147 Intent intent;
Kenny Root25c925e2012-09-08 22:02:21 -0700148
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800149 IntentFilter filter;
Kenny Root25c925e2012-09-08 22:02:21 -0700150
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800151 abstract boolean notifyNow(Intent intent);
Kenny Root25c925e2012-09-08 22:02:21 -0700152
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800153 @Override
154 public void onReceive(Context context, Intent intent) {
155 if (notifyNow(intent)) {
156 synchronized (this) {
157 received = true;
158 doneFlag = true;
159 this.intent = intent;
160 notifyAll();
161 }
162 }
163 }
164
165 public boolean isDone() {
166 return doneFlag;
167 }
168
169 public void setFilter(IntentFilter filter) {
170 this.filter = filter;
171 }
172 }
173
174 class InstallReceiver extends GenericReceiver {
175 String pkgName;
176
177 InstallReceiver(String pkgName) {
178 this.pkgName = pkgName;
179 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
180 filter.addDataScheme("package");
181 super.setFilter(filter);
182 }
183
184 public boolean notifyNow(Intent intent) {
185 String action = intent.getAction();
186 if (!Intent.ACTION_PACKAGE_ADDED.equals(action)) {
187 return false;
188 }
189 Uri data = intent.getData();
190 String installedPkg = data.getEncodedSchemeSpecificPart();
191 if (pkgName.equals(installedPkg)) {
192 return true;
193 }
194 return false;
195 }
196 }
197
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700198 private PackageManager getPm() {
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800199 return mContext.getPackageManager();
200 }
201
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700202 private IPackageManager getIPm() {
203 IPackageManager ipm = IPackageManager.Stub.asInterface(
204 ServiceManager.getService("package"));
205 return ipm;
206 }
207
Kenny Rootb7c24702012-04-14 19:46:09 -0700208 public void invokeInstallPackage(Uri packageURI, int flags, GenericReceiver receiver,
209 boolean shouldSucceed) {
Christopher Tatef1977b42014-03-24 16:25:51 -0700210 TestInstallObserver observer = new TestInstallObserver();
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800211 mContext.registerReceiver(receiver, receiver.filter);
212 try {
213 // Wait on observer
Kenny Root25c925e2012-09-08 22:02:21 -0700214 synchronized (observer) {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800215 synchronized (receiver) {
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800216 getPm().installPackage(packageURI, observer, flags, null);
217 long waitTime = 0;
Kenny Root25c925e2012-09-08 22:02:21 -0700218 while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) {
Kenny Root94e0acb2010-09-21 16:30:56 -0700219 try {
220 observer.wait(WAIT_TIME_INCR);
221 waitTime += WAIT_TIME_INCR;
222 } catch (InterruptedException e) {
223 Log.i(TAG, "Interrupted during sleep", e);
224 }
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800225 }
Kenny Root25c925e2012-09-08 22:02:21 -0700226 if (!observer.isDone()) {
Kenny Root94e0acb2010-09-21 16:30:56 -0700227 fail("Timed out waiting for packageInstalled callback");
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800228 }
Kenny Rootb7c24702012-04-14 19:46:09 -0700229
230 if (shouldSucceed) {
231 if (observer.returnCode != PackageManager.INSTALL_SUCCEEDED) {
232 fail("Package installation should have succeeded, but got code "
233 + observer.returnCode);
234 }
235 } else {
236 if (observer.returnCode == PackageManager.INSTALL_SUCCEEDED) {
237 fail("Package installation should fail");
238 }
239
240 /*
241 * We'll never expect get a notification since we
242 * shouldn't succeed.
243 */
244 return;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800245 }
Kenny Rootb7c24702012-04-14 19:46:09 -0700246
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800247 // Verify we received the broadcast
248 waitTime = 0;
Kenny Root25c925e2012-09-08 22:02:21 -0700249 while ((!receiver.isDone()) && (waitTime < MAX_WAIT_TIME)) {
Kenny Root94e0acb2010-09-21 16:30:56 -0700250 try {
251 receiver.wait(WAIT_TIME_INCR);
252 waitTime += WAIT_TIME_INCR;
253 } catch (InterruptedException e) {
254 Log.i(TAG, "Interrupted during sleep", e);
255 }
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800256 }
Kenny Root25c925e2012-09-08 22:02:21 -0700257 if (!receiver.isDone()) {
Kenny Root94e0acb2010-09-21 16:30:56 -0700258 fail("Timed out waiting for PACKAGE_ADDED notification");
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800259 }
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800260 }
261 }
262 } finally {
263 mContext.unregisterReceiver(receiver);
264 }
265 }
266
Kenny Root1683afa2011-01-07 14:27:50 -0800267 public void invokeInstallPackageFail(Uri packageURI, int flags, int expectedResult) {
Christopher Tatef1977b42014-03-24 16:25:51 -0700268 TestInstallObserver observer = new TestInstallObserver();
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800269 try {
270 // Wait on observer
Kenny Root25c925e2012-09-08 22:02:21 -0700271 synchronized (observer) {
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800272 getPm().installPackage(packageURI, observer, flags, null);
273 long waitTime = 0;
Kenny Root25c925e2012-09-08 22:02:21 -0700274 while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) {
Kenny Root94e0acb2010-09-21 16:30:56 -0700275 try {
276 observer.wait(WAIT_TIME_INCR);
277 waitTime += WAIT_TIME_INCR;
278 } catch (InterruptedException e) {
279 Log.i(TAG, "Interrupted during sleep", e);
280 }
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800281 }
Kenny Root25c925e2012-09-08 22:02:21 -0700282 if (!observer.isDone()) {
Kenny Root94e0acb2010-09-21 16:30:56 -0700283 fail("Timed out waiting for packageInstalled callback");
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800284 }
Kenny Root1683afa2011-01-07 14:27:50 -0800285 assertEquals(expectedResult, observer.returnCode);
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800286 }
287 } finally {
288 }
289 }
290
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800291 Uri getInstallablePackage(int fileResId, File outFile) {
292 Resources res = mContext.getResources();
293 InputStream is = null;
294 try {
295 is = res.openRawResource(fileResId);
296 } catch (NotFoundException e) {
297 failStr("Failed to load resource with id: " + fileResId);
298 }
299 FileUtils.setPermissions(outFile.getPath(),
300 FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IRWXO,
301 -1, -1);
302 assertTrue(FileUtils.copyToFile(is, outFile));
303 FileUtils.setPermissions(outFile.getPath(),
304 FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IRWXO,
305 -1, -1);
306 return Uri.fromFile(outFile);
307 }
308
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700309 private PackageParser.Package parsePackage(Uri packageURI) throws PackageParserException {
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800310 final String archiveFilePath = packageURI.getPath();
Jeff Sharkey275e0852014-06-17 18:18:49 -0700311 PackageParser packageParser = new PackageParser();
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800312 File sourceFile = new File(archiveFilePath);
Jeff Sharkey275e0852014-06-17 18:18:49 -0700313 PackageParser.Package pkg = packageParser.parseMonolithicPackage(sourceFile, 0);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -0800314 packageParser = null;
315 return pkg;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800316 }
Kenny Root25c925e2012-09-08 22:02:21 -0700317
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700318 private boolean checkSd(long pkgLen) {
319 String status = Environment.getExternalStorageState();
320 if (!status.equals(Environment.MEDIA_MOUNTED)) {
321 return false;
322 }
323 long sdSize = -1;
Kenny Root25c925e2012-09-08 22:02:21 -0700324 StatFs sdStats = new StatFs(Environment.getExternalStorageDirectory().getPath());
325 sdSize = (long) sdStats.getAvailableBlocks() * (long) sdStats.getBlockSize();
Kenny Root94e0acb2010-09-21 16:30:56 -0700326 // TODO check for thresholds here
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700327 return pkgLen <= sdSize;
Neal Nguyenedb979a2010-04-22 13:54:32 -0700328
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700329 }
Kenny Root25c925e2012-09-08 22:02:21 -0700330
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700331 private boolean checkInt(long pkgLen) {
332 StatFs intStats = new StatFs(Environment.getDataDirectory().getPath());
Kenny Root25c925e2012-09-08 22:02:21 -0700333 long intSize = (long) intStats.getBlockCount() * (long) intStats.getBlockSize();
334 long iSize = (long) intStats.getAvailableBlocks() * (long) intStats.getBlockSize();
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700335 // TODO check for thresholds here?
336 return pkgLen <= iSize;
337 }
Kenny Root25c925e2012-09-08 22:02:21 -0700338
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700339 private static final int INSTALL_LOC_INT = 1;
Kenny Root25c925e2012-09-08 22:02:21 -0700340
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700341 private static final int INSTALL_LOC_SD = 2;
Kenny Root25c925e2012-09-08 22:02:21 -0700342
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700343 private static final int INSTALL_LOC_ERR = -1;
Kenny Root25c925e2012-09-08 22:02:21 -0700344
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700345 private int getInstallLoc(int flags, int expInstallLocation, long pkgLen) {
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800346 // Flags explicitly over ride everything else.
Kenny Root25c925e2012-09-08 22:02:21 -0700347 if ((flags & PackageManager.INSTALL_EXTERNAL) != 0) {
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700348 return INSTALL_LOC_SD;
349 } else if ((flags & PackageManager.INSTALL_INTERNAL) != 0) {
350 return INSTALL_LOC_INT;
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800351 }
352 // Manifest option takes precedence next
353 if (expInstallLocation == PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL) {
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700354 if (checkSd(pkgLen)) {
Kenny Root25c925e2012-09-08 22:02:21 -0700355 return INSTALL_LOC_SD;
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700356 }
357 if (checkInt(pkgLen)) {
358 return INSTALL_LOC_INT;
359 }
360 return INSTALL_LOC_ERR;
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800361 }
Suchi Amalapurapu9b10ef52010-03-03 09:45:24 -0800362 if (expInstallLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY) {
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700363 if (checkInt(pkgLen)) {
364 return INSTALL_LOC_INT;
365 }
366 return INSTALL_LOC_ERR;
Suchi Amalapurapu9b10ef52010-03-03 09:45:24 -0800367 }
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700368 if (expInstallLocation == PackageInfo.INSTALL_LOCATION_AUTO) {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700369 // Check for free memory internally
370 if (checkInt(pkgLen)) {
371 return INSTALL_LOC_INT;
372 }
373 // Check for free memory externally
374 if (checkSd(pkgLen)) {
375 return INSTALL_LOC_SD;
376 }
377 return INSTALL_LOC_ERR;
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700378 }
379 // Check for settings preference.
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800380 boolean checkSd = false;
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700381 int userPref = getDefaultInstallLoc();
382 if (userPref == APP_INSTALL_DEVICE) {
383 if (checkInt(pkgLen)) {
384 return INSTALL_LOC_INT;
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800385 }
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700386 return INSTALL_LOC_ERR;
387 } else if (userPref == APP_INSTALL_SDCARD) {
388 if (checkSd(pkgLen)) {
389 return INSTALL_LOC_SD;
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800390 }
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700391 return INSTALL_LOC_ERR;
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -0700392 }
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700393 // Default system policy for apps with no manifest option specified.
394 // Check for free memory internally
395 if (checkInt(pkgLen)) {
396 return INSTALL_LOC_INT;
397 }
398 return INSTALL_LOC_ERR;
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800399 }
Neal Nguyenedb979a2010-04-22 13:54:32 -0700400
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800401 private void assertInstall(PackageParser.Package pkg, int flags, int expInstallLocation) {
402 try {
403 String pkgName = pkg.packageName;
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700404 ApplicationInfo info = getPm().getApplicationInfo(pkgName, 0);
405 assertNotNull(info);
406 assertEquals(pkgName, info.packageName);
407 File dataDir = Environment.getDataDirectory();
408 String appInstallPath = new File(dataDir, "app").getPath();
409 String drmInstallPath = new File(dataDir, "app-private").getPath();
410 File srcDir = new File(info.sourceDir);
411 String srcPath = srcDir.getParent();
412 File publicSrcDir = new File(info.publicSourceDir);
413 String publicSrcPath = publicSrcDir.getParent();
414 long pkgLen = new File(info.sourceDir).length();
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800415
Kenny Root6dceb882012-04-12 14:23:49 -0700416 int rLoc = getInstallLoc(flags, expInstallLocation, pkgLen);
417 if (rLoc == INSTALL_LOC_INT) {
418 if ((flags & PackageManager.INSTALL_FORWARD_LOCK) != 0) {
419 assertTrue("The application should be installed forward locked",
420 (info.flags & ApplicationInfo.FLAG_FORWARD_LOCK) != 0);
Kenny Root7e921a12012-09-08 22:02:21 -0700421 assertStartsWith("The APK path should point to the ASEC",
422 SECURE_CONTAINERS_PREFIX, srcPath);
423 assertStartsWith("The public APK path should point to the ASEC",
424 SECURE_CONTAINERS_PREFIX, publicSrcPath);
425 assertStartsWith("The native library path should point to the ASEC",
426 SECURE_CONTAINERS_PREFIX, info.nativeLibraryDir);
Nick Kralevich7de350a2012-09-07 15:48:11 -0700427 try {
428 String compatLib = new File(info.dataDir + "/lib").getCanonicalPath();
429 assertEquals("The compatibility lib directory should be a symbolic link to "
430 + info.nativeLibraryDir,
431 info.nativeLibraryDir, compatLib);
432 } catch (IOException e) {
433 fail("compat check: Can't read " + info.dataDir + "/lib");
434 }
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700435 } else {
Kenny Root6dceb882012-04-12 14:23:49 -0700436 assertFalse((info.flags & ApplicationInfo.FLAG_FORWARD_LOCK) != 0);
437 assertEquals(srcPath, appInstallPath);
438 assertEquals(publicSrcPath, appInstallPath);
Kenny Root7e921a12012-09-08 22:02:21 -0700439 assertStartsWith("Native library should point to shared lib directory",
Kenny Rootddbe50d2012-09-06 13:18:37 -0700440 new File(APP_LIB_DIR_PREFIX, info.packageName).getPath(),
Kenny Root7e921a12012-09-08 22:02:21 -0700441 info.nativeLibraryDir);
442 assertDirOwnerGroupPerms(
443 "Native library directory should be owned by system:system and 0755",
444 Process.SYSTEM_UID, Process.SYSTEM_UID,
445 S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH,
446 info.nativeLibraryDir);
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700447 }
Kenny Root6dceb882012-04-12 14:23:49 -0700448 assertFalse((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0);
449
450 // Make sure the native library dir is not a symlink
451 final File nativeLibDir = new File(info.nativeLibraryDir);
452 assertTrue("Native library dir should exist at " + info.nativeLibraryDir,
453 nativeLibDir.exists());
454 try {
455 assertEquals("Native library dir should not be a symlink",
Kenny Root7e921a12012-09-08 22:02:21 -0700456 info.nativeLibraryDir, nativeLibDir.getCanonicalPath());
Kenny Root6dceb882012-04-12 14:23:49 -0700457 } catch (IOException e) {
458 fail("Can't read " + nativeLibDir.getPath());
459 }
Kenny Root25c925e2012-09-08 22:02:21 -0700460 } else if (rLoc == INSTALL_LOC_SD) {
Kenny Root6dceb882012-04-12 14:23:49 -0700461 if ((flags & PackageManager.INSTALL_FORWARD_LOCK) != 0) {
462 assertTrue("The application should be installed forward locked",
463 (info.flags & ApplicationInfo.FLAG_FORWARD_LOCK) != 0);
464 } else {
465 assertFalse("The application should not be installed forward locked",
466 (info.flags & ApplicationInfo.FLAG_FORWARD_LOCK) != 0);
467 }
468 assertTrue("Application flags (" + info.flags
469 + ") should contain FLAG_EXTERNAL_STORAGE",
470 (info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0);
471 // Might need to check:
472 // ((info.flags & ApplicationInfo.FLAG_FORWARD_LOCK) != 0)
Kenny Root7e921a12012-09-08 22:02:21 -0700473 assertStartsWith("The APK path should point to the ASEC",
474 SECURE_CONTAINERS_PREFIX, srcPath);
475 assertStartsWith("The public APK path should point to the ASEC",
476 SECURE_CONTAINERS_PREFIX, publicSrcPath);
477 assertStartsWith("The native library path should point to the ASEC",
478 SECURE_CONTAINERS_PREFIX, info.nativeLibraryDir);
Kenny Root6dceb882012-04-12 14:23:49 -0700479
480 // Make sure the native library in /data/data/<app>/lib is a
481 // symlink to the ASEC
482 final File nativeLibSymLink = new File(info.dataDir, "lib");
483 assertTrue("Native library symlink should exist at " + nativeLibSymLink.getPath(),
484 nativeLibSymLink.exists());
485 try {
486 assertEquals(nativeLibSymLink.getPath() + " should be a symlink to "
Kenny Root25c925e2012-09-08 22:02:21 -0700487 + info.nativeLibraryDir, info.nativeLibraryDir,
488 nativeLibSymLink.getCanonicalPath());
Kenny Root6dceb882012-04-12 14:23:49 -0700489 } catch (IOException e) {
490 fail("Can't read " + nativeLibSymLink.getPath());
491 }
492 } else {
493 // TODO handle error. Install should have failed.
494 fail("Install should have failed");
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800495 }
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800496 } catch (NameNotFoundException e) {
497 failStr("failed with exception : " + e);
498 }
499 }
Neal Nguyenedb979a2010-04-22 13:54:32 -0700500
Kenny Root7e921a12012-09-08 22:02:21 -0700501 private void assertDirOwnerGroupPerms(String reason, int uid, int gid, int perms, String path) {
502 final StructStat stat;
503
504 try {
Elliott Hughes34385d32014-04-28 11:11:32 -0700505 stat = Os.lstat(path);
Kenny Root7e921a12012-09-08 22:02:21 -0700506 } catch (ErrnoException e) {
507 throw new AssertionError(reason + "\n" + "Got: " + path + " does not exist");
508 }
509
510 StringBuilder sb = new StringBuilder();
511
512 if (!S_ISDIR(stat.st_mode)) {
513 sb.append("\nExpected type: ");
514 sb.append(S_IFDIR);
515 sb.append("\ngot type: ");
516 sb.append((stat.st_mode & S_IFMT));
517 }
518
519 if (stat.st_uid != uid) {
520 sb.append("\nExpected owner: ");
521 sb.append(uid);
522 sb.append("\nGot owner: ");
523 sb.append(stat.st_uid);
524 }
525
526 if (stat.st_gid != gid) {
527 sb.append("\nExpected group: ");
528 sb.append(gid);
529 sb.append("\nGot group: ");
530 sb.append(stat.st_gid);
531 }
532
533 if ((stat.st_mode & ~S_IFMT) != perms) {
534 sb.append("\nExpected permissions: ");
535 sb.append(Integer.toOctalString(perms));
536 sb.append("\nGot permissions: ");
537 sb.append(Integer.toOctalString(stat.st_mode & ~S_IFMT));
538 }
539
540 if (sb.length() > 0) {
541 throw new AssertionError(reason + sb.toString());
542 }
543 }
544
545 private static void assertStartsWith(String prefix, String actual) {
546 assertStartsWith("", prefix, actual);
547 }
548
549 private static void assertStartsWith(String description, String prefix, String actual) {
550 if (!actual.startsWith(prefix)) {
551 StringBuilder sb = new StringBuilder(description);
552 sb.append("\nExpected prefix: ");
553 sb.append(prefix);
554 sb.append("\n got: ");
555 sb.append(actual);
556 sb.append('\n');
557 throw new AssertionError(sb.toString());
558 }
559 }
560
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800561 private void assertNotInstalled(String pkgName) {
562 try {
563 ApplicationInfo info = getPm().getApplicationInfo(pkgName, 0);
564 fail(pkgName + " shouldnt be installed");
565 } catch (NameNotFoundException e) {
566 }
Kenny Roote15bdc22012-09-16 15:45:38 -0700567
568 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
569 List<UserInfo> users = um.getUsers();
570 for (UserInfo user : users) {
571 String dataDir = PackageManager.getDataDirForUser(user.id, pkgName);
572 assertFalse("Application data directory should not exist: " + dataDir,
573 new File(dataDir).exists());
574 }
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800575 }
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800576
577 class InstallParams {
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800578 Uri packageURI;
Kenny Root25c925e2012-09-08 22:02:21 -0700579
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800580 PackageParser.Package pkg;
Kenny Root25c925e2012-09-08 22:02:21 -0700581
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700582 InstallParams(String outFileName, int rawResId) throws PackageParserException {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700583 this.pkg = getParsedPackage(outFileName, rawResId);
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700584 this.packageURI = Uri.fromFile(new File(pkg.codePath));
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700585 }
Kenny Root25c925e2012-09-08 22:02:21 -0700586
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700587 InstallParams(PackageParser.Package pkg) {
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700588 this.packageURI = Uri.fromFile(new File(pkg.codePath));
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800589 this.pkg = pkg;
590 }
Kenny Root25c925e2012-09-08 22:02:21 -0700591
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700592 long getApkSize() {
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700593 File file = new File(pkg.codePath);
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700594 return file.length();
595 }
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800596 }
597
Kenny Root25c925e2012-09-08 22:02:21 -0700598 private InstallParams sampleInstallFromRawResource(int flags, boolean cleanUp) throws Exception {
599 return installFromRawResource("install.apk", R.raw.install, flags, cleanUp, false, -1,
600 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800601 }
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -0800602
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700603 static final String PERM_PACKAGE = "package";
Kenny Root25c925e2012-09-08 22:02:21 -0700604
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700605 static final String PERM_DEFINED = "defined";
Kenny Root25c925e2012-09-08 22:02:21 -0700606
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700607 static final String PERM_UNDEFINED = "undefined";
Kenny Root25c925e2012-09-08 22:02:21 -0700608
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700609 static final String PERM_USED = "used";
Kenny Root25c925e2012-09-08 22:02:21 -0700610
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700611 static final String PERM_NOTUSED = "notused";
Neal Nguyenedb979a2010-04-22 13:54:32 -0700612
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700613 private void assertPermissions(String[] cmds) {
614 final PackageManager pm = getPm();
615 String pkg = null;
616 PackageInfo pkgInfo = null;
617 String mode = PERM_DEFINED;
618 int i = 0;
619 while (i < cmds.length) {
620 String cmd = cmds[i++];
621 if (cmd == PERM_PACKAGE) {
622 pkg = cmds[i++];
623 try {
624 pkgInfo = pm.getPackageInfo(pkg,
625 PackageManager.GET_PERMISSIONS
626 | PackageManager.GET_UNINSTALLED_PACKAGES);
627 } catch (NameNotFoundException e) {
628 pkgInfo = null;
629 }
630 } else if (cmd == PERM_DEFINED || cmd == PERM_UNDEFINED
631 || cmd == PERM_USED || cmd == PERM_NOTUSED) {
632 mode = cmds[i++];
633 } else {
634 if (mode == PERM_DEFINED) {
635 try {
636 PermissionInfo pi = pm.getPermissionInfo(cmd, 0);
637 assertNotNull(pi);
638 assertEquals(pi.packageName, pkg);
639 assertEquals(pi.name, cmd);
640 assertNotNull(pkgInfo);
641 boolean found = false;
Kenny Root25c925e2012-09-08 22:02:21 -0700642 for (int j = 0; j < pkgInfo.permissions.length && !found; j++) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700643 if (pkgInfo.permissions[j].name.equals(cmd)) {
644 found = true;
645 }
646 }
647 if (!found) {
648 fail("Permission not found: " + cmd);
649 }
650 } catch (NameNotFoundException e) {
651 throw new RuntimeException(e);
652 }
653 } else if (mode == PERM_UNDEFINED) {
654 try {
655 pm.getPermissionInfo(cmd, 0);
656 throw new RuntimeException("Permission exists: " + cmd);
657 } catch (NameNotFoundException e) {
658 }
659 if (pkgInfo != null) {
660 boolean found = false;
Kenny Root25c925e2012-09-08 22:02:21 -0700661 for (int j = 0; j < pkgInfo.permissions.length && !found; j++) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700662 if (pkgInfo.permissions[j].name.equals(cmd)) {
663 found = true;
664 }
665 }
666 if (found) {
667 fail("Permission still exists: " + cmd);
668 }
669 }
670 } else if (mode == PERM_USED || mode == PERM_NOTUSED) {
671 boolean found = false;
Kenny Root25c925e2012-09-08 22:02:21 -0700672 for (int j = 0; j < pkgInfo.requestedPermissions.length && !found; j++) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700673 if (pkgInfo.requestedPermissions[j].equals(cmd)) {
674 found = true;
675 }
676 }
677 if (!found) {
678 fail("Permission not requested: " + cmd);
679 }
680 if (mode == PERM_USED) {
Kenny Root25c925e2012-09-08 22:02:21 -0700681 if (pm.checkPermission(cmd, pkg) != PackageManager.PERMISSION_GRANTED) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700682 fail("Permission not granted: " + cmd);
683 }
684 } else {
Kenny Root25c925e2012-09-08 22:02:21 -0700685 if (pm.checkPermission(cmd, pkg) != PackageManager.PERMISSION_DENIED) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700686 fail("Permission granted: " + cmd);
687 }
688 }
689 }
690 }
691 }
692 }
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -0800693
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700694 private PackageParser.Package getParsedPackage(String outFileName, int rawResId)
695 throws PackageParserException {
Suchi Amalapurapuae181712010-03-30 14:01:02 -0700696 PackageManager pm = mContext.getPackageManager();
697 File filesDir = mContext.getFilesDir();
698 File outFile = new File(filesDir, outFileName);
699 Uri packageURI = getInstallablePackage(rawResId, outFile);
700 PackageParser.Package pkg = parsePackage(packageURI);
701 return pkg;
702 }
703
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800704 /*
705 * Utility function that reads a apk bundled as a raw resource
706 * copies it into own data directory and invokes
707 * PackageManager api to install it.
708 */
Kenny Root25c925e2012-09-08 22:02:21 -0700709 private void installFromRawResource(InstallParams ip, int flags, boolean cleanUp, boolean fail,
710 int result, int expInstallLocation) throws Exception {
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800711 PackageManager pm = mContext.getPackageManager();
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700712 PackageParser.Package pkg = ip.pkg;
713 Uri packageURI = ip.packageURI;
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800714 if ((flags & PackageManager.INSTALL_REPLACE_EXISTING) == 0) {
715 // Make sure the package doesn't exist
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800716 try {
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800717 ApplicationInfo appInfo = pm.getApplicationInfo(pkg.packageName,
718 PackageManager.GET_UNINSTALLED_PACKAGES);
719 GenericReceiver receiver = new DeleteReceiver(pkg.packageName);
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700720 invokeDeletePackage(pkg.packageName, 0, receiver);
Kenny Roote15bdc22012-09-16 15:45:38 -0700721 } catch (NameNotFoundException e) {
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800722 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800723 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800724 try {
725 if (fail) {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700726 invokeInstallPackageFail(packageURI, flags, result);
Suchi Amalapurapuae181712010-03-30 14:01:02 -0700727 if ((flags & PackageManager.INSTALL_REPLACE_EXISTING) == 0) {
728 assertNotInstalled(pkg.packageName);
729 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800730 } else {
731 InstallReceiver receiver = new InstallReceiver(pkg.packageName);
Kenny Rootb7c24702012-04-14 19:46:09 -0700732 invokeInstallPackage(packageURI, flags, receiver, true);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800733 // Verify installed information
734 assertInstall(pkg, flags, expInstallLocation);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800735 }
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800736 } finally {
737 if (cleanUp) {
Suchi Amalapurapu9b10ef52010-03-03 09:45:24 -0800738 cleanUpInstall(ip);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800739 }
740 }
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700741 }
742
743 /*
744 * Utility function that reads a apk bundled as a raw resource
745 * copies it into own data directory and invokes
746 * PackageManager api to install it.
747 */
Kenny Root25c925e2012-09-08 22:02:21 -0700748 private InstallParams installFromRawResource(String outFileName, int rawResId, int flags,
749 boolean cleanUp, boolean fail, int result, int expInstallLocation) throws Exception {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700750 InstallParams ip = new InstallParams(outFileName, rawResId);
751 installFromRawResource(ip, flags, cleanUp, fail, result, expInstallLocation);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800752 return ip;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800753 }
754
Brett Chabotf76c56b2010-07-26 17:28:17 -0700755 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -0700756 public void testInstallNormalInternal() throws Exception {
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800757 sampleInstallFromRawResource(0, true);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800758 }
759
Brett Chabotf76c56b2010-07-26 17:28:17 -0700760 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -0700761 public void testInstallFwdLockedInternal() throws Exception {
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800762 sampleInstallFromRawResource(PackageManager.INSTALL_FORWARD_LOCK, true);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800763 }
764
Brett Chabotf76c56b2010-07-26 17:28:17 -0700765 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -0700766 public void testInstallSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -0800767 // Do not run on devices with emulated external storage.
768 if (Environment.isExternalStorageEmulated()) {
769 return;
770 }
771
Kenny Rootd7b421b2010-08-05 08:40:00 -0700772 mountMedia();
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800773 sampleInstallFromRawResource(PackageManager.INSTALL_EXTERNAL, true);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800774 }
775
Kenny Root25c925e2012-09-08 22:02:21 -0700776 /* ------------------------- Test replacing packages -------------- */
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800777 class ReplaceReceiver extends GenericReceiver {
778 String pkgName;
Kenny Root25c925e2012-09-08 22:02:21 -0700779
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800780 final static int INVALID = -1;
Kenny Root25c925e2012-09-08 22:02:21 -0700781
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800782 final static int REMOVED = 1;
Kenny Root25c925e2012-09-08 22:02:21 -0700783
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800784 final static int ADDED = 2;
Kenny Root25c925e2012-09-08 22:02:21 -0700785
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800786 final static int REPLACED = 3;
Kenny Root25c925e2012-09-08 22:02:21 -0700787
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800788 int removed = INVALID;
Kenny Root25c925e2012-09-08 22:02:21 -0700789
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800790 // for updated system apps only
791 boolean update = false;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800792
793 ReplaceReceiver(String pkgName) {
794 this.pkgName = pkgName;
795 filter = new IntentFilter(Intent.ACTION_PACKAGE_REMOVED);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800796 filter.addAction(Intent.ACTION_PACKAGE_ADDED);
797 if (update) {
798 filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
799 }
800 filter.addDataScheme("package");
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800801 super.setFilter(filter);
802 }
803
804 public boolean notifyNow(Intent intent) {
805 String action = intent.getAction();
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800806 Uri data = intent.getData();
807 String installedPkg = data.getEncodedSchemeSpecificPart();
808 if (pkgName == null || !pkgName.equals(installedPkg)) {
809 return false;
810 }
811 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
812 removed = REMOVED;
813 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
814 if (removed != REMOVED) {
815 return false;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800816 }
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800817 boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
818 if (!replacing) {
819 return false;
820 }
821 removed = ADDED;
822 if (!update) {
823 return true;
824 }
825 } else if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
826 if (removed != ADDED) {
827 return false;
828 }
829 removed = REPLACED;
830 return true;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800831 }
832 return false;
833 }
834 }
835
836 /*
837 * Utility function that reads a apk bundled as a raw resource
838 * copies it into own data directory and invokes
839 * PackageManager api to install first and then replace it
840 * again.
841 */
Kenny Root25c925e2012-09-08 22:02:21 -0700842 private void sampleReplaceFromRawResource(int flags) throws Exception {
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800843 InstallParams ip = sampleInstallFromRawResource(flags, false);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800844 boolean replace = ((flags & PackageManager.INSTALL_REPLACE_EXISTING) != 0);
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800845 Log.i(TAG, "replace=" + replace);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800846 GenericReceiver receiver;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800847 if (replace) {
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800848 receiver = new ReplaceReceiver(ip.pkg.packageName);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800849 Log.i(TAG, "Creating replaceReceiver");
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800850 } else {
851 receiver = new InstallReceiver(ip.pkg.packageName);
852 }
853 try {
Kenny Rootb7c24702012-04-14 19:46:09 -0700854 invokeInstallPackage(ip.packageURI, flags, receiver, replace);
855 if (replace) {
856 assertInstall(ip.pkg, flags, ip.pkg.installLocation);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800857 }
858 } finally {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800859 cleanUpInstall(ip);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800860 }
861 }
862
Brett Chabotf76c56b2010-07-26 17:28:17 -0700863 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -0700864 public void testReplaceFailNormalInternal() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -0700865 sampleReplaceFromRawResource(0);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800866 }
867
Brett Chabotf76c56b2010-07-26 17:28:17 -0700868 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -0700869 public void testReplaceFailFwdLockedInternal() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -0700870 sampleReplaceFromRawResource(PackageManager.INSTALL_FORWARD_LOCK);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800871 }
872
Brett Chabotf76c56b2010-07-26 17:28:17 -0700873 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -0700874 public void testReplaceFailSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -0800875 // Do not run on devices with emulated external storage.
876 if (Environment.isExternalStorageEmulated()) {
877 return;
878 }
879
Suchi Amalapurapuae181712010-03-30 14:01:02 -0700880 sampleReplaceFromRawResource(PackageManager.INSTALL_EXTERNAL);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800881 }
882
Brett Chabotf76c56b2010-07-26 17:28:17 -0700883 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -0700884 public void testReplaceNormalInternal() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -0700885 sampleReplaceFromRawResource(PackageManager.INSTALL_REPLACE_EXISTING);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800886 }
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800887
Brett Chabotf76c56b2010-07-26 17:28:17 -0700888 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -0700889 public void testReplaceFwdLockedInternal() throws Exception {
890 sampleReplaceFromRawResource(PackageManager.INSTALL_REPLACE_EXISTING
891 | PackageManager.INSTALL_FORWARD_LOCK);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800892 }
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800893
Brett Chabotf76c56b2010-07-26 17:28:17 -0700894 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -0700895 public void testReplaceSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -0800896 // Do not run on devices with emulated external storage.
897 if (Environment.isExternalStorageEmulated()) {
898 return;
899 }
900
Kenny Root25c925e2012-09-08 22:02:21 -0700901 sampleReplaceFromRawResource(PackageManager.INSTALL_REPLACE_EXISTING
902 | PackageManager.INSTALL_EXTERNAL);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800903 }
904
Kenny Root7e921a12012-09-08 22:02:21 -0700905 /* -------------- Delete tests --- */
Kenny Root25c925e2012-09-08 22:02:21 -0700906 private static class DeleteObserver extends IPackageDeleteObserver.Stub {
Kenny Root7e921a12012-09-08 22:02:21 -0700907 private CountDownLatch mLatch = new CountDownLatch(1);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800908
Kenny Root7e921a12012-09-08 22:02:21 -0700909 private int mReturnCode;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800910
Kenny Root7e921a12012-09-08 22:02:21 -0700911 private final String mPackageName;
912
913 private String mObservedPackage;
914
915 public DeleteObserver(String packageName) {
916 mPackageName = packageName;
917 }
918
919 public boolean isSuccessful() {
920 return mReturnCode == PackageManager.DELETE_SUCCEEDED;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800921 }
922
Kenny Rootc39bb4a2011-02-28 13:27:19 -0800923 public void packageDeleted(String packageName, int returnCode) throws RemoteException {
Kenny Root7e921a12012-09-08 22:02:21 -0700924 mObservedPackage = packageName;
925
926 mReturnCode = returnCode;
927
928 mLatch.countDown();
929 }
930
931 public void waitForCompletion(long timeoutMillis) {
932 final long deadline = SystemClock.uptimeMillis() + timeoutMillis;
933
934 long waitTime = timeoutMillis;
935 while (waitTime > 0) {
936 try {
937 boolean done = mLatch.await(waitTime, TimeUnit.MILLISECONDS);
938 if (done) {
939 assertEquals(mPackageName, mObservedPackage);
940 return;
941 }
942 } catch (InterruptedException e) {
943 // TODO Auto-generated catch block
944 e.printStackTrace();
945 }
946 waitTime = deadline - SystemClock.uptimeMillis();
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800947 }
Kenny Root7e921a12012-09-08 22:02:21 -0700948
949 throw new AssertionError("Timeout waiting for package deletion");
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800950 }
951 }
952
953 class DeleteReceiver extends GenericReceiver {
954 String pkgName;
955
956 DeleteReceiver(String pkgName) {
957 this.pkgName = pkgName;
958 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_REMOVED);
959 filter.addDataScheme("package");
960 super.setFilter(filter);
961 }
962
963 public boolean notifyNow(Intent intent) {
964 String action = intent.getAction();
965 if (!Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
966 return false;
967 }
968 Uri data = intent.getData();
969 String installedPkg = data.getEncodedSchemeSpecificPart();
970 if (pkgName.equals(installedPkg)) {
971 return true;
972 }
973 return false;
974 }
975 }
976
Kenny Root7e921a12012-09-08 22:02:21 -0700977 public boolean invokeDeletePackage(final String pkgName, int flags, GenericReceiver receiver)
978 throws Exception {
979 ApplicationInfo info = getPm().getApplicationInfo(pkgName,
980 PackageManager.GET_UNINSTALLED_PACKAGES);
981
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800982 mContext.registerReceiver(receiver, receiver.filter);
983 try {
Kenny Root7e921a12012-09-08 22:02:21 -0700984 DeleteObserver observer = new DeleteObserver(pkgName);
985
Kenny Roota3e90792012-10-18 10:58:36 -0700986 getPm().deletePackage(pkgName, observer, flags | PackageManager.DELETE_ALL_USERS);
Kenny Root7e921a12012-09-08 22:02:21 -0700987 observer.waitForCompletion(MAX_WAIT_TIME);
988
989 assertUninstalled(info);
990
991 // Verify we received the broadcast
Kenny Roota3e90792012-10-18 10:58:36 -0700992 // TODO replace this with a CountDownLatch
993 synchronized (receiver) {
994 long waitTime = 0;
995 while ((!receiver.isDone()) && (waitTime < MAX_WAIT_TIME)) {
996 receiver.wait(WAIT_TIME_INCR);
997 waitTime += WAIT_TIME_INCR;
998 }
999 if (!receiver.isDone()) {
1000 throw new Exception("Timed out waiting for PACKAGE_REMOVED notification");
1001 }
Kenny Root7e921a12012-09-08 22:02:21 -07001002 }
1003 return receiver.received;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001004 } finally {
1005 mContext.unregisterReceiver(receiver);
1006 }
1007 }
1008
Kenny Root7e921a12012-09-08 22:02:21 -07001009 private static void assertUninstalled(ApplicationInfo info) throws Exception {
1010 File nativeLibraryFile = new File(info.nativeLibraryDir);
1011 assertFalse("Native library directory should be erased", nativeLibraryFile.exists());
1012 }
1013
Kenny Root25c925e2012-09-08 22:02:21 -07001014 public void deleteFromRawResource(int iFlags, int dFlags) throws Exception {
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -08001015 InstallParams ip = sampleInstallFromRawResource(iFlags, false);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001016 boolean retainData = ((dFlags & PackageManager.DELETE_KEEP_DATA) != 0);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001017 GenericReceiver receiver = new DeleteReceiver(ip.pkg.packageName);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001018 try {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07001019 assertTrue(invokeDeletePackage(ip.pkg.packageName, dFlags, receiver));
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001020 ApplicationInfo info = null;
1021 Log.i(TAG, "okay4");
1022 try {
Kenny Root25c925e2012-09-08 22:02:21 -07001023 info = getPm().getApplicationInfo(ip.pkg.packageName,
1024 PackageManager.GET_UNINSTALLED_PACKAGES);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001025 } catch (NameNotFoundException e) {
1026 info = null;
1027 }
1028 if (retainData) {
1029 assertNotNull(info);
1030 assertEquals(info.packageName, ip.pkg.packageName);
1031 File file = new File(info.dataDir);
1032 assertTrue(file.exists());
1033 } else {
1034 assertNull(info);
1035 }
1036 } catch (Exception e) {
1037 failStr(e);
1038 } finally {
1039 cleanUpInstall(ip);
1040 }
1041 }
1042
Brett Chabotf76c56b2010-07-26 17:28:17 -07001043 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001044 public void testDeleteNormalInternal() throws Exception {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001045 deleteFromRawResource(0, 0);
1046 }
1047
Brett Chabotf76c56b2010-07-26 17:28:17 -07001048 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001049 public void testDeleteFwdLockedInternal() throws Exception {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001050 deleteFromRawResource(PackageManager.INSTALL_FORWARD_LOCK, 0);
1051 }
1052
Brett Chabotf76c56b2010-07-26 17:28:17 -07001053 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001054 public void testDeleteSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001055 // Do not run on devices with emulated external storage.
1056 if (Environment.isExternalStorageEmulated()) {
1057 return;
1058 }
1059
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -08001060 deleteFromRawResource(PackageManager.INSTALL_EXTERNAL, 0);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001061 }
1062
Brett Chabotf76c56b2010-07-26 17:28:17 -07001063 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001064 public void testDeleteNormalInternalRetainData() throws Exception {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001065 deleteFromRawResource(0, PackageManager.DELETE_KEEP_DATA);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001066 }
1067
Brett Chabotf76c56b2010-07-26 17:28:17 -07001068 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001069 public void testDeleteFwdLockedInternalRetainData() throws Exception {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001070 deleteFromRawResource(PackageManager.INSTALL_FORWARD_LOCK, PackageManager.DELETE_KEEP_DATA);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001071 }
1072
Brett Chabotf76c56b2010-07-26 17:28:17 -07001073 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001074 public void testDeleteSdcardRetainData() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001075 // Do not run on devices with emulated external storage.
1076 if (Environment.isExternalStorageEmulated()) {
1077 return;
1078 }
1079
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001080 deleteFromRawResource(PackageManager.INSTALL_EXTERNAL, PackageManager.DELETE_KEEP_DATA);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001081 }
1082
Kenny Root25c925e2012-09-08 22:02:21 -07001083 /* sdcard mount/unmount tests ***** */
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001084
1085 class SdMountReceiver extends GenericReceiver {
1086 String pkgNames[];
Kenny Root25c925e2012-09-08 22:02:21 -07001087
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001088 boolean status = true;
1089
1090 SdMountReceiver(String[] pkgNames) {
1091 this.pkgNames = pkgNames;
1092 IntentFilter filter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
1093 super.setFilter(filter);
1094 }
1095
1096 public boolean notifyNow(Intent intent) {
1097 Log.i(TAG, "okay 1");
1098 String action = intent.getAction();
1099 if (!Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
1100 return false;
1101 }
1102 String rpkgList[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1103 for (String pkg : pkgNames) {
1104 boolean found = false;
1105 for (String rpkg : rpkgList) {
1106 if (rpkg.equals(pkg)) {
1107 found = true;
1108 break;
1109 }
1110 }
1111 if (!found) {
1112 status = false;
1113 return true;
1114 }
1115 }
1116 return true;
1117 }
1118 }
1119
1120 class SdUnMountReceiver extends GenericReceiver {
1121 String pkgNames[];
Kenny Root25c925e2012-09-08 22:02:21 -07001122
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001123 boolean status = true;
1124
1125 SdUnMountReceiver(String[] pkgNames) {
1126 this.pkgNames = pkgNames;
1127 IntentFilter filter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
1128 super.setFilter(filter);
1129 }
1130
1131 public boolean notifyNow(Intent intent) {
1132 String action = intent.getAction();
1133 if (!Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1134 return false;
1135 }
1136 String rpkgList[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1137 for (String pkg : pkgNames) {
1138 boolean found = false;
1139 for (String rpkg : rpkgList) {
1140 if (rpkg.equals(pkg)) {
1141 found = true;
1142 break;
1143 }
1144 }
1145 if (!found) {
1146 status = false;
1147 return true;
1148 }
1149 }
1150 return true;
1151 }
1152 }
1153
1154 IMountService getMs() {
1155 IBinder service = ServiceManager.getService("mount");
1156 if (service != null) {
1157 return IMountService.Stub.asInterface(service);
1158 } else {
1159 Log.e(TAG, "Can't get mount service");
1160 }
1161 return null;
1162 }
1163
Kenny Rootd7b421b2010-08-05 08:40:00 -07001164 boolean checkMediaState(String desired) {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001165 try {
Kenny Rootd7b421b2010-08-05 08:40:00 -07001166 String mPath = Environment.getExternalStorageDirectory().getPath();
1167 String actual = getMs().getVolumeState(mPath);
1168 if (desired.equals(actual)) {
1169 return true;
1170 } else {
1171 return false;
1172 }
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001173 } catch (RemoteException e) {
Kenny Rootd7b421b2010-08-05 08:40:00 -07001174 Log.e(TAG, "Exception while checking media state", e);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001175 return false;
1176 }
1177 }
1178
1179 boolean mountMedia() {
Kenny Root3c676892011-01-12 10:58:40 -08001180 // We can't mount emulated storage.
1181 if (Environment.isExternalStorageEmulated()) {
1182 return true;
1183 }
1184
Kenny Rootd7b421b2010-08-05 08:40:00 -07001185 if (checkMediaState(Environment.MEDIA_MOUNTED)) {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001186 return true;
1187 }
Kenny Rootd7b421b2010-08-05 08:40:00 -07001188
1189 final String path = Environment.getExternalStorageDirectory().toString();
1190 StorageListener observer = new StorageListener(Environment.MEDIA_MOUNTED);
1191 StorageManager sm = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
1192 sm.registerListener(observer);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001193 try {
Kenny Rootd7b421b2010-08-05 08:40:00 -07001194 // Wait on observer
1195 synchronized (observer) {
1196 int ret = getMs().mountVolume(path);
1197 if (ret != StorageResultCode.OperationSucceeded) {
1198 throw new Exception("Could not mount the media");
1199 }
1200 long waitTime = 0;
1201 while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) {
1202 observer.wait(WAIT_TIME_INCR);
1203 waitTime += WAIT_TIME_INCR;
1204 }
1205 if (!observer.isDone()) {
1206 throw new Exception("Timed out waiting for unmount media notification");
1207 }
1208 return true;
1209 }
1210 } catch (Exception e) {
1211 Log.e(TAG, "Exception : " + e);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001212 return false;
Kenny Rootd7b421b2010-08-05 08:40:00 -07001213 } finally {
1214 sm.unregisterListener(observer);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001215 }
1216 }
1217
1218 private boolean unmountMedia() {
Kenny Root3c676892011-01-12 10:58:40 -08001219 // We can't unmount emulated storage.
1220 if (Environment.isExternalStorageEmulated()) {
1221 return true;
1222 }
1223
Kenny Rootd7b421b2010-08-05 08:40:00 -07001224 if (checkMediaState(Environment.MEDIA_UNMOUNTED)) {
1225 return true;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001226 }
Neal Nguyenedb979a2010-04-22 13:54:32 -07001227
Kenny Rootd7b421b2010-08-05 08:40:00 -07001228 final String path = Environment.getExternalStorageDirectory().getPath();
1229 StorageListener observer = new StorageListener(Environment.MEDIA_UNMOUNTED);
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001230 StorageManager sm = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
1231 sm.registerListener(observer);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001232 try {
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001233 // Wait on observer
Kenny Root25c925e2012-09-08 22:02:21 -07001234 synchronized (observer) {
Ben Komalo13c71972011-09-07 16:35:56 -07001235 getMs().unmountVolume(path, true, false);
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001236 long waitTime = 0;
Kenny Root25c925e2012-09-08 22:02:21 -07001237 while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) {
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001238 observer.wait(WAIT_TIME_INCR);
1239 waitTime += WAIT_TIME_INCR;
1240 }
Kenny Root25c925e2012-09-08 22:02:21 -07001241 if (!observer.isDone()) {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07001242 throw new Exception("Timed out waiting for unmount media notification");
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001243 }
1244 return true;
1245 }
1246 } catch (Exception e) {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07001247 Log.e(TAG, "Exception : " + e);
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001248 return false;
1249 } finally {
1250 sm.unregisterListener(observer);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001251 }
1252 }
1253
Kenny Root25c925e2012-09-08 22:02:21 -07001254 private boolean mountFromRawResource() throws Exception {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001255 // Install pkg on sdcard
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001256 InstallParams ip = sampleInstallFromRawResource(PackageManager.INSTALL_EXTERNAL, false);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001257 if (localLOGV) Log.i(TAG, "Installed pkg on sdcard");
Kenny Rootd7b421b2010-08-05 08:40:00 -07001258 boolean origState = checkMediaState(Environment.MEDIA_MOUNTED);
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001259 boolean registeredReceiver = false;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001260 SdMountReceiver receiver = new SdMountReceiver(new String[]{ip.pkg.packageName});
1261 try {
1262 if (localLOGV) Log.i(TAG, "Unmounting media");
1263 // Unmount media
1264 assertTrue(unmountMedia());
1265 if (localLOGV) Log.i(TAG, "Unmounted media");
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001266 // Register receiver here
1267 PackageManager pm = getPm();
1268 mContext.registerReceiver(receiver, receiver.filter);
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001269 registeredReceiver = true;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001270
1271 // Wait on receiver
1272 synchronized (receiver) {
1273 if (localLOGV) Log.i(TAG, "Mounting media");
1274 // Mount media again
1275 assertTrue(mountMedia());
1276 if (localLOGV) Log.i(TAG, "Mounted media");
1277 if (localLOGV) Log.i(TAG, "Waiting for notification");
1278 long waitTime = 0;
1279 // Verify we received the broadcast
1280 waitTime = 0;
Kenny Root25c925e2012-09-08 22:02:21 -07001281 while ((!receiver.isDone()) && (waitTime < MAX_WAIT_TIME)) {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001282 receiver.wait(WAIT_TIME_INCR);
1283 waitTime += WAIT_TIME_INCR;
1284 }
1285 if(!receiver.isDone()) {
1286 failStr("Timed out waiting for EXTERNAL_APPLICATIONS notification");
1287 }
1288 return receiver.received;
1289 }
1290 } catch (InterruptedException e) {
1291 failStr(e);
1292 return false;
1293 } finally {
Kenny Root25c925e2012-09-08 22:02:21 -07001294 if (registeredReceiver) {
1295 mContext.unregisterReceiver(receiver);
1296 }
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001297 // Restore original media state
1298 if (origState) {
1299 mountMedia();
1300 } else {
1301 unmountMedia();
1302 }
1303 if (localLOGV) Log.i(TAG, "Cleaning up install");
1304 cleanUpInstall(ip);
1305 }
1306 }
1307
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001308 /*
1309 * Install package on sdcard. Unmount and then mount the media.
1310 * (Use PackageManagerService private api for now)
1311 * Make sure the installed package is available.
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001312 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07001313 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001314 public void testMountSdNormalInternal() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001315 // Do not run on devices with emulated external storage.
1316 if (Environment.isExternalStorageEmulated()) {
1317 return;
1318 }
1319
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001320 assertTrue(mountFromRawResource());
1321 }
1322
Kenny Root25c925e2012-09-08 22:02:21 -07001323 void cleanUpInstall(InstallParams ip) throws Exception {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001324 if (ip == null) {
1325 return;
1326 }
1327 Runtime.getRuntime().gc();
Kenny Root7e921a12012-09-08 22:02:21 -07001328
1329 final String packageName = ip.pkg.packageName;
1330 Log.i(TAG, "Deleting package : " + packageName);
1331
1332 ApplicationInfo info = null;
1333 try {
1334 info = getPm().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
1335 } catch (NameNotFoundException ignored) {
1336 }
1337
1338 DeleteObserver observer = new DeleteObserver(packageName);
Kenny Roota3e90792012-10-18 10:58:36 -07001339 getPm().deletePackage(packageName, observer, PackageManager.DELETE_ALL_USERS);
Kenny Root7e921a12012-09-08 22:02:21 -07001340 observer.waitForCompletion(MAX_WAIT_TIME);
1341
1342 try {
1343 if (info != null) {
1344 assertUninstalled(info);
1345 }
1346 } finally {
Jeff Sharkeyc4858a22014-06-16 10:51:20 -07001347 File outFile = new File(ip.pkg.codePath);
Kenny Root7e921a12012-09-08 22:02:21 -07001348 if (outFile != null && outFile.exists()) {
1349 outFile.delete();
1350 }
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001351 }
1352 }
Kenny Root25c925e2012-09-08 22:02:21 -07001353
1354 private void cleanUpInstall(String pkgName) throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07001355 if (pkgName == null) {
1356 return;
1357 }
1358 Log.i(TAG, "Deleting package : " + pkgName);
1359 try {
1360 ApplicationInfo info = getPm().getApplicationInfo(pkgName,
1361 PackageManager.GET_UNINSTALLED_PACKAGES);
Kenny Root25c925e2012-09-08 22:02:21 -07001362
Suchi Amalapurapuae181712010-03-30 14:01:02 -07001363 if (info != null) {
Kenny Root7e921a12012-09-08 22:02:21 -07001364 DeleteObserver observer = new DeleteObserver(pkgName);
Kenny Roota3e90792012-10-18 10:58:36 -07001365 getPm().deletePackage(pkgName, observer, PackageManager.DELETE_ALL_USERS);
Kenny Root7e921a12012-09-08 22:02:21 -07001366 observer.waitForCompletion(MAX_WAIT_TIME);
1367 assertUninstalled(info);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07001368 }
Kenny Root25c925e2012-09-08 22:02:21 -07001369 } catch (NameNotFoundException e) {
1370 }
Suchi Amalapurapuae181712010-03-30 14:01:02 -07001371 }
Suchi Amalapurapu117818e2010-02-09 03:45:40 -08001372
Brett Chabotf76c56b2010-07-26 17:28:17 -07001373 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001374 public void testManifestInstallLocationInternal() throws Exception {
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -08001375 installFromRawResource("install.apk", R.raw.install_loc_internal,
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001376 0, true, false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
Suchi Amalapurapu117818e2010-02-09 03:45:40 -08001377 }
1378
Brett Chabotf76c56b2010-07-26 17:28:17 -07001379 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001380 public void testManifestInstallLocationSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001381 // Do not run on devices with emulated external storage.
1382 if (Environment.isExternalStorageEmulated()) {
1383 return;
1384 }
1385
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -08001386 installFromRawResource("install.apk", R.raw.install_loc_sdcard,
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001387 0, true, false, -1, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
Suchi Amalapurapu117818e2010-02-09 03:45:40 -08001388 }
1389
Brett Chabotf76c56b2010-07-26 17:28:17 -07001390 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001391 public void testManifestInstallLocationAuto() throws Exception {
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -08001392 installFromRawResource("install.apk", R.raw.install_loc_auto,
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001393 0, true, false, -1, PackageInfo.INSTALL_LOCATION_AUTO);
Suchi Amalapurapu117818e2010-02-09 03:45:40 -08001394 }
1395
Brett Chabotf76c56b2010-07-26 17:28:17 -07001396 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001397 public void testManifestInstallLocationUnspecified() throws Exception {
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -08001398 installFromRawResource("install.apk", R.raw.install_loc_unspecified,
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -07001399 0, true, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
Suchi Amalapurapu117818e2010-02-09 03:45:40 -08001400 }
1401
Brett Chabotf76c56b2010-07-26 17:28:17 -07001402 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001403 public void testManifestInstallLocationFwdLockedFlagSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001404 // Do not run on devices with emulated external storage.
1405 if (Environment.isExternalStorageEmulated()) {
1406 return;
1407 }
1408
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001409 installFromRawResource("install.apk", R.raw.install_loc_unspecified,
1410 PackageManager.INSTALL_FORWARD_LOCK |
Kenny Rootbf023582012-05-02 16:56:15 -07001411 PackageManager.INSTALL_EXTERNAL, true, false, -1,
1412 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001413 }
1414
Brett Chabotf76c56b2010-07-26 17:28:17 -07001415 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001416 public void testManifestInstallLocationFwdLockedSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001417 // Do not run on devices with emulated external storage.
1418 if (Environment.isExternalStorageEmulated()) {
1419 return;
1420 }
1421
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -08001422 installFromRawResource("install.apk", R.raw.install_loc_sdcard,
Kenny Root25c925e2012-09-08 22:02:21 -07001423 PackageManager.INSTALL_FORWARD_LOCK, true, false, -1,
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001424 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
1425 }
1426
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001427 /*
1428 * Install a package on internal flash via PackageManager install flag. Replace
1429 * the package via flag to install on sdcard. Make sure the new flag overrides
1430 * the old install location.
1431 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07001432 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001433 public void testReplaceFlagInternalSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001434 // Do not run on devices with emulated external storage.
1435 if (Environment.isExternalStorageEmulated()) {
1436 return;
1437 }
1438
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001439 int iFlags = 0;
1440 int rFlags = PackageManager.INSTALL_EXTERNAL;
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001441 InstallParams ip = sampleInstallFromRawResource(iFlags, false);
1442 GenericReceiver receiver = new ReplaceReceiver(ip.pkg.packageName);
1443 int replaceFlags = rFlags | PackageManager.INSTALL_REPLACE_EXISTING;
1444 try {
Kenny Rootb7c24702012-04-14 19:46:09 -07001445 invokeInstallPackage(ip.packageURI, replaceFlags, receiver, true);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001446 assertInstall(ip.pkg, rFlags, ip.pkg.installLocation);
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001447 } catch (Exception e) {
1448 failStr("Failed with exception : " + e);
1449 } finally {
1450 cleanUpInstall(ip);
1451 }
1452 }
1453
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001454 /*
1455 * Install a package on sdcard via PackageManager install flag. Replace
1456 * the package with no flags or manifest option and make sure the old
1457 * install location is retained.
1458 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07001459 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001460 public void testReplaceFlagSdcardInternal() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001461 // Do not run on devices with emulated external storage.
1462 if (Environment.isExternalStorageEmulated()) {
1463 return;
1464 }
1465
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001466 int iFlags = PackageManager.INSTALL_EXTERNAL;
1467 int rFlags = 0;
1468 InstallParams ip = sampleInstallFromRawResource(iFlags, false);
1469 GenericReceiver receiver = new ReplaceReceiver(ip.pkg.packageName);
1470 int replaceFlags = rFlags | PackageManager.INSTALL_REPLACE_EXISTING;
1471 try {
Kenny Rootb7c24702012-04-14 19:46:09 -07001472 invokeInstallPackage(ip.packageURI, replaceFlags, receiver, true);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001473 assertInstall(ip.pkg, iFlags, ip.pkg.installLocation);
1474 } catch (Exception e) {
1475 failStr("Failed with exception : " + e);
1476 } finally {
1477 cleanUpInstall(ip);
1478 }
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001479 }
1480
Brett Chabotf76c56b2010-07-26 17:28:17 -07001481 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001482 public void testManifestInstallLocationReplaceInternalSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001483 // Do not run on devices with emulated external storage.
1484 if (Environment.isExternalStorageEmulated()) {
1485 return;
1486 }
1487
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001488 int iFlags = 0;
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -07001489 int iApk = R.raw.install_loc_internal;
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001490 int rFlags = 0;
1491 int rApk = R.raw.install_loc_sdcard;
1492 InstallParams ip = installFromRawResource("install.apk", iApk,
1493 iFlags, false,
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -07001494 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001495 GenericReceiver receiver = new ReplaceReceiver(ip.pkg.packageName);
1496 int replaceFlags = rFlags | PackageManager.INSTALL_REPLACE_EXISTING;
1497 try {
1498 InstallParams rp = installFromRawResource("install.apk", rApk,
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001499 replaceFlags, false,
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001500 false, -1, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
1501 assertInstall(rp.pkg, replaceFlags, rp.pkg.installLocation);
1502 } catch (Exception e) {
1503 failStr("Failed with exception : " + e);
1504 } finally {
1505 cleanUpInstall(ip);
1506 }
1507 }
1508
Brett Chabotf76c56b2010-07-26 17:28:17 -07001509 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001510 public void testManifestInstallLocationReplaceSdcardInternal() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001511 // Do not run on devices with emulated external storage.
1512 if (Environment.isExternalStorageEmulated()) {
1513 return;
1514 }
1515
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001516 int iFlags = 0;
1517 int iApk = R.raw.install_loc_sdcard;
1518 int rFlags = 0;
1519 int rApk = R.raw.install_loc_unspecified;
1520 InstallParams ip = installFromRawResource("install.apk", iApk,
1521 iFlags, false,
1522 false, -1, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001523 int replaceFlags = rFlags | PackageManager.INSTALL_REPLACE_EXISTING;
1524 try {
1525 InstallParams rp = installFromRawResource("install.apk", rApk,
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001526 replaceFlags, false,
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001527 false, -1, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
1528 assertInstall(rp.pkg, replaceFlags, ip.pkg.installLocation);
1529 } catch (Exception e) {
1530 failStr("Failed with exception : " + e);
1531 } finally {
1532 cleanUpInstall(ip);
1533 }
Suchi Amalapurapu117818e2010-02-09 03:45:40 -08001534 }
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001535
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001536 class MoveReceiver extends GenericReceiver {
1537 String pkgName;
Kenny Root25c925e2012-09-08 22:02:21 -07001538
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001539 final static int INVALID = -1;
Kenny Root25c925e2012-09-08 22:02:21 -07001540
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001541 final static int REMOVED = 1;
Kenny Root25c925e2012-09-08 22:02:21 -07001542
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001543 final static int ADDED = 2;
Kenny Root25c925e2012-09-08 22:02:21 -07001544
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001545 int removed = INVALID;
1546
1547 MoveReceiver(String pkgName) {
1548 this.pkgName = pkgName;
1549 filter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
1550 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
1551 super.setFilter(filter);
1552 }
1553
1554 public boolean notifyNow(Intent intent) {
1555 String action = intent.getAction();
1556 Log.i(TAG, "MoveReceiver::" + action);
1557 if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1558 String[] list = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1559 if (list != null) {
1560 for (String pkg : list) {
1561 if (pkg.equals(pkgName)) {
1562 removed = REMOVED;
1563 break;
1564 }
1565 }
1566 }
1567 removed = REMOVED;
1568 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
1569 if (removed != REMOVED) {
1570 return false;
1571 }
1572 String[] list = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1573 if (list != null) {
1574 for (String pkg : list) {
1575 if (pkg.equals(pkgName)) {
1576 removed = ADDED;
1577 return true;
1578 }
1579 }
1580 }
1581 }
1582 return false;
1583 }
1584 }
1585
1586 private class PackageMoveObserver extends IPackageMoveObserver.Stub {
1587 public int returnCode;
Kenny Root25c925e2012-09-08 22:02:21 -07001588
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001589 private boolean doneFlag = false;
Kenny Root25c925e2012-09-08 22:02:21 -07001590
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001591 public String packageName;
Kenny Root25c925e2012-09-08 22:02:21 -07001592
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001593 public PackageMoveObserver(String pkgName) {
1594 packageName = pkgName;
1595 }
Kenny Root25c925e2012-09-08 22:02:21 -07001596
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001597 public void packageMoved(String packageName, int returnCode) {
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001598 Log.i("DEBUG_MOVE::", "pkg = " + packageName + ", " + "ret = " + returnCode);
1599 if (!packageName.equals(this.packageName)) {
1600 return;
1601 }
Kenny Root25c925e2012-09-08 22:02:21 -07001602 synchronized (this) {
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001603 this.returnCode = returnCode;
1604 doneFlag = true;
1605 notifyAll();
1606 }
1607 }
1608
1609 public boolean isDone() {
1610 return doneFlag;
1611 }
1612 }
1613
Kenny Root25c925e2012-09-08 22:02:21 -07001614 public boolean invokeMovePackage(String pkgName, int flags, GenericReceiver receiver)
1615 throws Exception {
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001616 PackageMoveObserver observer = new PackageMoveObserver(pkgName);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001617 final boolean received = false;
1618 mContext.registerReceiver(receiver, receiver.filter);
1619 try {
1620 // Wait on observer
Kenny Root25c925e2012-09-08 22:02:21 -07001621 synchronized (observer) {
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001622 synchronized (receiver) {
1623 getPm().movePackage(pkgName, observer, flags);
1624 long waitTime = 0;
Kenny Root25c925e2012-09-08 22:02:21 -07001625 while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) {
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001626 observer.wait(WAIT_TIME_INCR);
1627 waitTime += WAIT_TIME_INCR;
1628 }
Kenny Root25c925e2012-09-08 22:02:21 -07001629 if (!observer.isDone()) {
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001630 throw new Exception("Timed out waiting for pkgmove callback");
1631 }
1632 if (observer.returnCode != PackageManager.MOVE_SUCCEEDED) {
1633 return false;
1634 }
1635 // Verify we received the broadcast
1636 waitTime = 0;
Kenny Root25c925e2012-09-08 22:02:21 -07001637 while ((!receiver.isDone()) && (waitTime < MAX_WAIT_TIME)) {
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001638 receiver.wait(WAIT_TIME_INCR);
1639 waitTime += WAIT_TIME_INCR;
1640 }
Kenny Root25c925e2012-09-08 22:02:21 -07001641 if (!receiver.isDone()) {
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001642 throw new Exception("Timed out waiting for MOVE notifications");
1643 }
1644 return receiver.received;
1645 }
1646 }
1647 } finally {
1648 mContext.unregisterReceiver(receiver);
1649 }
1650 }
Kenny Root25c925e2012-09-08 22:02:21 -07001651
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001652 private boolean invokeMovePackageFail(String pkgName, int flags, int errCode) throws Exception {
1653 PackageMoveObserver observer = new PackageMoveObserver(pkgName);
1654 try {
1655 // Wait on observer
Kenny Root25c925e2012-09-08 22:02:21 -07001656 synchronized (observer) {
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001657 getPm().movePackage(pkgName, observer, flags);
1658 long waitTime = 0;
Kenny Root25c925e2012-09-08 22:02:21 -07001659 while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) {
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001660 observer.wait(WAIT_TIME_INCR);
1661 waitTime += WAIT_TIME_INCR;
1662 }
Kenny Root25c925e2012-09-08 22:02:21 -07001663 if (!observer.isDone()) {
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001664 throw new Exception("Timed out waiting for pkgmove callback");
1665 }
1666 assertEquals(errCode, observer.returnCode);
1667 }
1668 } finally {
1669 }
1670 return true;
1671 }
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001672
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07001673 private int getDefaultInstallLoc() {
Suchi Amalapurapu9b10ef52010-03-03 09:45:24 -08001674 int origDefaultLoc = PackageInfo.INSTALL_LOCATION_AUTO;
1675 try {
Jeff Sharkey625239a2012-09-26 22:03:49 -07001676 origDefaultLoc = Settings.Global.getInt(mContext.getContentResolver(),
1677 Settings.Global.DEFAULT_INSTALL_LOCATION);
Suchi Amalapurapu9b10ef52010-03-03 09:45:24 -08001678 } catch (SettingNotFoundException e1) {
1679 }
1680 return origDefaultLoc;
1681 }
1682
1683 private void setInstallLoc(int loc) {
Jeff Sharkey625239a2012-09-26 22:03:49 -07001684 Settings.Global.putInt(mContext.getContentResolver(),
1685 Settings.Global.DEFAULT_INSTALL_LOCATION, loc);
Suchi Amalapurapu9b10ef52010-03-03 09:45:24 -08001686 }
Kenny Root25c925e2012-09-08 22:02:21 -07001687
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001688 /*
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001689 * Tests for moving apps between internal and external storage
1690 */
1691 /*
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001692 * Utility function that reads a apk bundled as a raw resource
1693 * copies it into own data directory and invokes
1694 * PackageManager api to install first and then replace it
1695 * again.
1696 */
Neal Nguyenedb979a2010-04-22 13:54:32 -07001697
Kenny Root25c925e2012-09-08 22:02:21 -07001698 private void moveFromRawResource(String outFileName, int rawResId, int installFlags,
1699 int moveFlags, boolean cleanUp, boolean fail, int result) throws Exception {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07001700 int origDefaultLoc = getDefaultInstallLoc();
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001701 InstallParams ip = null;
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001702 try {
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001703 setInstallLoc(PackageHelper.APP_INSTALL_AUTO);
1704 // Install first
1705 ip = installFromRawResource("install.apk", rawResId, installFlags, false,
1706 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
1707 ApplicationInfo oldAppInfo = getPm().getApplicationInfo(ip.pkg.packageName, 0);
1708 if (fail) {
1709 assertTrue(invokeMovePackageFail(ip.pkg.packageName, moveFlags, result));
1710 ApplicationInfo info = getPm().getApplicationInfo(ip.pkg.packageName, 0);
1711 assertNotNull(info);
1712 assertEquals(oldAppInfo.flags, info.flags);
1713 } else {
1714 // Create receiver based on expRetCode
1715 MoveReceiver receiver = new MoveReceiver(ip.pkg.packageName);
Kenny Root25c925e2012-09-08 22:02:21 -07001716 boolean retCode = invokeMovePackage(ip.pkg.packageName, moveFlags, receiver);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001717 assertTrue(retCode);
1718 ApplicationInfo info = getPm().getApplicationInfo(ip.pkg.packageName, 0);
Kenny Root6a6b0072010-10-07 16:46:10 -07001719 assertNotNull("ApplicationInfo for recently installed application should exist",
1720 info);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001721 if ((moveFlags & PackageManager.MOVE_INTERNAL) != 0) {
Kenny Root6a6b0072010-10-07 16:46:10 -07001722 assertTrue("ApplicationInfo.FLAG_EXTERNAL_STORAGE flag should NOT be set",
1723 (info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == 0);
Kenny Root7e921a12012-09-08 22:02:21 -07001724 assertStartsWith("Native library dir should be in dataDir",
1725 info.dataDir, info.nativeLibraryDir);
1726 } else if ((moveFlags & PackageManager.MOVE_EXTERNAL_MEDIA) != 0) {
Kenny Root6a6b0072010-10-07 16:46:10 -07001727 assertTrue("ApplicationInfo.FLAG_EXTERNAL_STORAGE flag should be set",
1728 (info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0);
Kenny Root7e921a12012-09-08 22:02:21 -07001729 assertStartsWith("Native library dir should point to ASEC",
1730 SECURE_CONTAINERS_PREFIX, info.nativeLibraryDir);
Kenny Root6a6b0072010-10-07 16:46:10 -07001731 final File nativeLibSymLink = new File(info.dataDir, "lib");
Kenny Root7e921a12012-09-08 22:02:21 -07001732 assertStartsWith("The data directory should have a 'lib' symlink that points to the ASEC container",
1733 SECURE_CONTAINERS_PREFIX, nativeLibSymLink.getCanonicalPath());
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001734 }
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001735 }
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001736 } catch (NameNotFoundException e) {
1737 failStr("Pkg hasnt been installed correctly");
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001738 } catch (Exception e) {
1739 failStr("Failed with exception : " + e);
1740 } finally {
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001741 if (ip != null) {
1742 cleanUpInstall(ip);
1743 }
Suchi Amalapurapu9b10ef52010-03-03 09:45:24 -08001744 // Restore default install location
1745 setInstallLoc(origDefaultLoc);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001746 }
1747 }
Kenny Root25c925e2012-09-08 22:02:21 -07001748
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001749 private void sampleMoveFromRawResource(int installFlags, int moveFlags, boolean fail,
Kenny Root25c925e2012-09-08 22:02:21 -07001750 int result) throws Exception {
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001751 moveFromRawResource("install.apk",
1752 R.raw.install, installFlags, moveFlags, true,
1753 fail, result);
1754 }
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001755
Brett Chabotf76c56b2010-07-26 17:28:17 -07001756 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001757 public void testMoveAppInternalToExternal() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001758 // Do not run on devices with emulated external storage.
1759 if (Environment.isExternalStorageEmulated()) {
1760 return;
1761 }
1762
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001763 int installFlags = PackageManager.INSTALL_INTERNAL;
1764 int moveFlags = PackageManager.MOVE_EXTERNAL_MEDIA;
1765 boolean fail = false;
1766 int result = PackageManager.MOVE_SUCCEEDED;
1767 sampleMoveFromRawResource(installFlags, moveFlags, fail, result);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001768 }
1769
Brett Chabotf76c56b2010-07-26 17:28:17 -07001770 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001771 public void testMoveAppInternalToInternal() throws Exception {
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001772 int installFlags = PackageManager.INSTALL_INTERNAL;
1773 int moveFlags = PackageManager.MOVE_INTERNAL;
1774 boolean fail = true;
1775 int result = PackageManager.MOVE_FAILED_INVALID_LOCATION;
1776 sampleMoveFromRawResource(installFlags, moveFlags, fail, result);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001777 }
1778
Brett Chabotf76c56b2010-07-26 17:28:17 -07001779 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001780 public void testMoveAppExternalToExternal() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001781 // Do not run on devices with emulated external storage.
1782 if (Environment.isExternalStorageEmulated()) {
1783 return;
1784 }
1785
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001786 int installFlags = PackageManager.INSTALL_EXTERNAL;
1787 int moveFlags = PackageManager.MOVE_EXTERNAL_MEDIA;
1788 boolean fail = true;
1789 int result = PackageManager.MOVE_FAILED_INVALID_LOCATION;
1790 sampleMoveFromRawResource(installFlags, moveFlags, fail, result);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001791 }
Kenny Root3c676892011-01-12 10:58:40 -08001792
Brett Chabotf76c56b2010-07-26 17:28:17 -07001793 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001794 public void testMoveAppExternalToInternal() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001795 // Do not run on devices with emulated external storage.
1796 if (Environment.isExternalStorageEmulated()) {
1797 return;
1798 }
1799
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001800 int installFlags = PackageManager.INSTALL_EXTERNAL;
1801 int moveFlags = PackageManager.MOVE_INTERNAL;
1802 boolean fail = false;
1803 int result = PackageManager.MOVE_SUCCEEDED;
1804 sampleMoveFromRawResource(installFlags, moveFlags, fail, result);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001805 }
Kenny Root3c676892011-01-12 10:58:40 -08001806
Brett Chabotf76c56b2010-07-26 17:28:17 -07001807 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001808 public void testMoveAppForwardLocked() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001809 // Do not run on devices with emulated external storage.
1810 if (Environment.isExternalStorageEmulated()) {
1811 return;
1812 }
1813
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001814 int installFlags = PackageManager.INSTALL_FORWARD_LOCK;
1815 int moveFlags = PackageManager.MOVE_EXTERNAL_MEDIA;
Kenny Rootbf023582012-05-02 16:56:15 -07001816 boolean fail = false;
1817 int result = PackageManager.MOVE_SUCCEEDED;
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001818 sampleMoveFromRawResource(installFlags, moveFlags, fail, result);
1819 }
Kenny Root3c676892011-01-12 10:58:40 -08001820
Brett Chabotf76c56b2010-07-26 17:28:17 -07001821 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001822 public void testMoveAppFailInternalToExternalDelete() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001823 // Do not run on devices with emulated external storage.
1824 if (Environment.isExternalStorageEmulated()) {
1825 return;
1826 }
1827
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001828 int installFlags = 0;
1829 int moveFlags = PackageManager.MOVE_EXTERNAL_MEDIA;
1830 boolean fail = true;
1831 final int result = PackageManager.MOVE_FAILED_DOESNT_EXIST;
Neal Nguyenedb979a2010-04-22 13:54:32 -07001832
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001833 int rawResId = R.raw.install;
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07001834 int origDefaultLoc = getDefaultInstallLoc();
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001835 InstallParams ip = null;
1836 try {
1837 PackageManager pm = getPm();
1838 setInstallLoc(PackageHelper.APP_INSTALL_AUTO);
1839 // Install first
1840 ip = installFromRawResource("install.apk", R.raw.install, installFlags, false,
1841 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
1842 // Delete the package now retaining data.
Suchi Amalapurapu9a212ad2010-05-18 11:06:53 -07001843 GenericReceiver receiver = new DeleteReceiver(ip.pkg.packageName);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001844 invokeDeletePackage(ip.pkg.packageName, PackageManager.DELETE_KEEP_DATA, receiver);
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001845 assertTrue(invokeMovePackageFail(ip.pkg.packageName, moveFlags, result));
1846 } catch (Exception e) {
1847 failStr(e);
1848 } finally {
1849 if (ip != null) {
1850 cleanUpInstall(ip);
1851 }
1852 // Restore default install location
1853 setInstallLoc(origDefaultLoc);
1854 }
1855 }
Kenny Root3c676892011-01-12 10:58:40 -08001856
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001857 /*
1858 * Test that an install error code is returned when media is unmounted
1859 * and package installed on sdcard via package manager flag.
1860 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07001861 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001862 public void testInstallSdcardUnmount() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001863 // Do not run on devices with emulated external storage.
1864 if (Environment.isExternalStorageEmulated()) {
1865 return;
1866 }
1867
Kenny Rootd7b421b2010-08-05 08:40:00 -07001868 boolean origState = checkMediaState(Environment.MEDIA_MOUNTED);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001869 try {
1870 // Unmount sdcard
1871 assertTrue(unmountMedia());
1872 // Try to install and make sure an error code is returned.
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07001873 installFromRawResource("install.apk", R.raw.install,
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001874 PackageManager.INSTALL_EXTERNAL, false,
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -07001875 true, PackageManager.INSTALL_FAILED_MEDIA_UNAVAILABLE,
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07001876 PackageInfo.INSTALL_LOCATION_AUTO);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001877 } finally {
1878 // Restore original media state
1879 if (origState) {
1880 mountMedia();
1881 } else {
1882 unmountMedia();
1883 }
1884 }
1885 }
1886
1887 /*
Neal Nguyenedb979a2010-04-22 13:54:32 -07001888 * Unmount sdcard. Try installing an app with manifest option to install
1889 * on sdcard. Make sure it gets installed on internal flash.
1890 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07001891 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001892 public void testInstallManifestSdcardUnmount() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001893 // Do not run on devices with emulated external storage.
1894 if (Environment.isExternalStorageEmulated()) {
1895 return;
1896 }
1897
Kenny Rootd7b421b2010-08-05 08:40:00 -07001898 boolean origState = checkMediaState(Environment.MEDIA_MOUNTED);
1899 try {
1900 // Unmount sdcard
1901 assertTrue(unmountMedia());
1902 InstallParams ip = new InstallParams("install.apk", R.raw.install_loc_sdcard);
1903 installFromRawResource(ip, 0, true, false, -1,
1904 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
1905 } finally {
1906 // Restore original media state
1907 if (origState) {
1908 mountMedia();
1909 } else {
1910 unmountMedia();
1911 }
1912 }
1913 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001914
Kenny Root25c925e2012-09-08 22:02:21 -07001915 /*---------- Recommended install location tests ----*/
1916 /*
1917 * PrecedenceSuffixes:
1918 * Flag : FlagI, FlagE, FlagF
1919 * I - internal, E - external, F - forward locked, Flag suffix absent if not using any option.
1920 * Manifest: ManifestI, ManifestE, ManifestA, Manifest suffix absent if not using any option.
1921 * Existing: Existing suffix absent if not existing.
1922 * User: UserI, UserE, UserA, User suffix absent if not existing.
1923 *
1924 */
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -07001925
Kenny Root3c676892011-01-12 10:58:40 -08001926 /*
1927 * Install an app on internal flash
1928 */
1929 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001930 public void testFlagI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001931 sampleInstallFromRawResource(PackageManager.INSTALL_INTERNAL, true);
1932 }
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -07001933
Kenny Root3c676892011-01-12 10:58:40 -08001934 /*
1935 * Install an app on sdcard.
1936 */
1937 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001938 public void testFlagE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001939 // Do not run on devices with emulated external storage.
1940 if (Environment.isExternalStorageEmulated()) {
1941 return;
1942 }
1943
1944 sampleInstallFromRawResource(PackageManager.INSTALL_EXTERNAL, true);
1945 }
1946
1947 /*
1948 * Install an app forward-locked.
1949 */
1950 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001951 public void testFlagF() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001952 sampleInstallFromRawResource(PackageManager.INSTALL_FORWARD_LOCK, true);
1953 }
1954
1955 /*
1956 * Install an app with both internal and external flags set. should fail
1957 */
1958 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001959 public void testFlagIE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001960 installFromRawResource("install.apk", R.raw.install,
1961 PackageManager.INSTALL_EXTERNAL | PackageManager.INSTALL_INTERNAL,
1962 false,
1963 true, PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION,
1964 PackageInfo.INSTALL_LOCATION_AUTO);
1965 }
1966
1967 /*
1968 * Install an app with both internal and forward-lock flags set.
1969 */
1970 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001971 public void testFlagIF() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001972 sampleInstallFromRawResource(PackageManager.INSTALL_FORWARD_LOCK
1973 | PackageManager.INSTALL_INTERNAL, true);
1974 }
1975
1976 /*
Kenny Root6dceb882012-04-12 14:23:49 -07001977 * Install an app with both external and forward-lock flags set.
Kenny Root3c676892011-01-12 10:58:40 -08001978 */
1979 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001980 public void testFlagEF() throws Exception {
Kenny Root6dceb882012-04-12 14:23:49 -07001981 // Do not run on devices with emulated external storage.
1982 if (Environment.isExternalStorageEmulated()) {
1983 return;
1984 }
1985
1986 sampleInstallFromRawResource(PackageManager.INSTALL_FORWARD_LOCK
1987 | PackageManager.INSTALL_EXTERNAL, true);
Kenny Root3c676892011-01-12 10:58:40 -08001988 }
1989
1990 /*
1991 * Install an app with both internal and external flags set with forward
1992 * lock. Should fail.
1993 */
1994 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001995 public void testFlagIEF() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001996 installFromRawResource("install.apk", R.raw.install,
1997 PackageManager.INSTALL_FORWARD_LOCK | PackageManager.INSTALL_INTERNAL |
1998 PackageManager.INSTALL_EXTERNAL,
1999 false,
2000 true, PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION,
2001 PackageInfo.INSTALL_LOCATION_AUTO);
2002 }
2003
Kenny Root25c925e2012-09-08 22:02:21 -07002004 /*
2005 * Install an app with both internal and manifest option set.
2006 * should install on internal.
2007 */
2008 @LargeTest
2009 public void testFlagIManifestI() throws Exception {
2010 installFromRawResource("install.apk", R.raw.install_loc_internal,
2011 PackageManager.INSTALL_INTERNAL,
2012 true,
2013 false, -1,
2014 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2015 }
2016 /*
2017 * Install an app with both internal and manifest preference for
2018 * preferExternal. Should install on internal.
2019 */
2020 @LargeTest
2021 public void testFlagIManifestE() throws Exception {
2022 installFromRawResource("install.apk", R.raw.install_loc_sdcard,
2023 PackageManager.INSTALL_INTERNAL,
2024 true,
2025 false, -1,
2026 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2027 }
2028 /*
2029 * Install an app with both internal and manifest preference for
2030 * auto. should install internal.
2031 */
2032 @LargeTest
2033 public void testFlagIManifestA() throws Exception {
2034 installFromRawResource("install.apk", R.raw.install_loc_auto,
2035 PackageManager.INSTALL_INTERNAL,
2036 true,
2037 false, -1,
2038 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2039 }
2040 /*
2041 * Install an app with both external and manifest option set.
2042 * should install externally.
2043 */
2044 @LargeTest
2045 public void testFlagEManifestI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002046 // Do not run on devices with emulated external storage.
2047 if (Environment.isExternalStorageEmulated()) {
2048 return;
2049 }
2050
Kenny Root25c925e2012-09-08 22:02:21 -07002051 installFromRawResource("install.apk", R.raw.install_loc_internal,
2052 PackageManager.INSTALL_EXTERNAL,
2053 true,
2054 false, -1,
2055 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2056 }
Kenny Root3c676892011-01-12 10:58:40 -08002057
Kenny Root25c925e2012-09-08 22:02:21 -07002058 /*
2059 * Install an app with both external and manifest preference for
2060 * preferExternal. Should install externally.
2061 */
2062 @LargeTest
2063 public void testFlagEManifestE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002064 // Do not run on devices with emulated external storage.
2065 if (Environment.isExternalStorageEmulated()) {
2066 return;
2067 }
2068
2069 installFromRawResource("install.apk", R.raw.install_loc_sdcard,
2070 PackageManager.INSTALL_EXTERNAL,
2071 true,
2072 false, -1,
2073 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2074 }
2075
2076 /*
2077 * Install an app with both external and manifest preference for
2078 * auto. should install on external media.
2079 */
2080 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002081 public void testFlagEManifestA() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002082 // Do not run on devices with emulated external storage.
2083 if (Environment.isExternalStorageEmulated()) {
2084 return;
2085 }
2086
2087 installFromRawResource("install.apk", R.raw.install_loc_auto,
2088 PackageManager.INSTALL_EXTERNAL,
2089 true,
2090 false, -1,
2091 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2092 }
2093
2094 /*
2095 * Install an app with fwd locked flag set and install location set to
2096 * internal. should install internally.
2097 */
2098 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002099 public void testFlagFManifestI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002100 installFromRawResource("install.apk", R.raw.install_loc_internal,
2101 PackageManager.INSTALL_FORWARD_LOCK,
2102 true,
2103 false, -1,
Kenny Root6dceb882012-04-12 14:23:49 -07002104 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
Kenny Root3c676892011-01-12 10:58:40 -08002105 }
2106
2107 /*
2108 * Install an app with fwd locked flag set and install location set to
Kenny Root6dceb882012-04-12 14:23:49 -07002109 * preferExternal. Should install externally.
Kenny Root3c676892011-01-12 10:58:40 -08002110 */
2111 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002112 public void testFlagFManifestE() throws Exception {
Kenny Root6dceb882012-04-12 14:23:49 -07002113 // Do not run on devices with emulated external storage.
2114 if (Environment.isExternalStorageEmulated()) {
2115 return;
2116 }
2117
Kenny Root3c676892011-01-12 10:58:40 -08002118 installFromRawResource("install.apk", R.raw.install_loc_sdcard,
2119 PackageManager.INSTALL_FORWARD_LOCK,
2120 true,
2121 false, -1,
2122 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2123 }
2124
2125 /*
Kenny Root6dceb882012-04-12 14:23:49 -07002126 * Install an app with fwd locked flag set and install location set to auto.
2127 * should install externally.
Kenny Root3c676892011-01-12 10:58:40 -08002128 */
2129 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002130 public void testFlagFManifestA() throws Exception {
Kenny Root6dceb882012-04-12 14:23:49 -07002131 // Do not run on devices with emulated external storage.
2132 if (Environment.isExternalStorageEmulated()) {
2133 return;
2134 }
2135
Kenny Root3c676892011-01-12 10:58:40 -08002136 installFromRawResource("install.apk", R.raw.install_loc_auto,
2137 PackageManager.INSTALL_FORWARD_LOCK,
2138 true,
2139 false, -1,
Kenny Rootbf023582012-05-02 16:56:15 -07002140 PackageInfo.INSTALL_LOCATION_AUTO);
Kenny Root3c676892011-01-12 10:58:40 -08002141 }
2142
Kenny Root25c925e2012-09-08 22:02:21 -07002143 /*
2144 * The following test functions verify install location for existing apps.
Kenny Root3c676892011-01-12 10:58:40 -08002145 * ie existing app can be installed internally or externally. If install
2146 * flag is explicitly set it should override current location. If manifest location
2147 * is set, that should over ride current location too. if not the existing install
2148 * location should be honoured.
2149 * testFlagI/E/F/ExistingI/E -
2150 */
2151 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002152 public void testFlagIExistingI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002153 int iFlags = PackageManager.INSTALL_INTERNAL;
2154 int rFlags = PackageManager.INSTALL_INTERNAL | PackageManager.INSTALL_REPLACE_EXISTING;
2155 // First install.
2156 installFromRawResource("install.apk", R.raw.install,
2157 iFlags,
2158 false,
2159 false, -1,
2160 -1);
2161 // Replace now
2162 installFromRawResource("install.apk", R.raw.install,
2163 rFlags,
2164 true,
2165 false, -1,
2166 -1);
2167 }
2168
2169 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002170 public void testFlagIExistingE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002171 // Do not run on devices with emulated external storage.
2172 if (Environment.isExternalStorageEmulated()) {
2173 return;
2174 }
2175
2176 int iFlags = PackageManager.INSTALL_EXTERNAL;
2177 int rFlags = PackageManager.INSTALL_INTERNAL | PackageManager.INSTALL_REPLACE_EXISTING;
2178 // First install.
2179 installFromRawResource("install.apk", R.raw.install,
2180 iFlags,
2181 false,
2182 false, -1,
2183 -1);
2184 // Replace now
2185 installFromRawResource("install.apk", R.raw.install,
2186 rFlags,
2187 true,
2188 false, -1,
2189 -1);
2190 }
2191
2192 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002193 public void testFlagEExistingI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002194 // Do not run on devices with emulated external storage.
2195 if (Environment.isExternalStorageEmulated()) {
2196 return;
2197 }
2198
2199 int iFlags = PackageManager.INSTALL_INTERNAL;
2200 int rFlags = PackageManager.INSTALL_EXTERNAL | PackageManager.INSTALL_REPLACE_EXISTING;
2201 // First install.
2202 installFromRawResource("install.apk", R.raw.install,
2203 iFlags,
2204 false,
2205 false, -1,
2206 -1);
2207 // Replace now
2208 installFromRawResource("install.apk", R.raw.install,
2209 rFlags,
2210 true,
2211 false, -1,
2212 -1);
2213 }
2214
2215 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002216 public void testFlagEExistingE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002217 // Do not run on devices with emulated external storage.
2218 if (Environment.isExternalStorageEmulated()) {
2219 return;
2220 }
2221
2222 int iFlags = PackageManager.INSTALL_EXTERNAL;
2223 int rFlags = PackageManager.INSTALL_EXTERNAL | PackageManager.INSTALL_REPLACE_EXISTING;
2224 // First install.
2225 installFromRawResource("install.apk", R.raw.install,
2226 iFlags,
2227 false,
2228 false, -1,
2229 -1);
2230 // Replace now
2231 installFromRawResource("install.apk", R.raw.install,
2232 rFlags,
2233 true,
2234 false, -1,
2235 -1);
2236 }
2237
2238 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002239 public void testFlagFExistingI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002240 int iFlags = PackageManager.INSTALL_INTERNAL;
2241 int rFlags = PackageManager.INSTALL_FORWARD_LOCK | PackageManager.INSTALL_REPLACE_EXISTING;
2242 // First install.
2243 installFromRawResource("install.apk", R.raw.install,
2244 iFlags,
2245 false,
2246 false, -1,
2247 -1);
2248 // Replace now
2249 installFromRawResource("install.apk", R.raw.install,
2250 rFlags,
2251 true,
2252 false, -1,
2253 -1);
2254 }
2255
2256 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002257 public void testFlagFExistingE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002258 // Do not run on devices with emulated external storage.
2259 if (Environment.isExternalStorageEmulated()) {
2260 return;
2261 }
2262
2263 int iFlags = PackageManager.INSTALL_EXTERNAL;
2264 int rFlags = PackageManager.INSTALL_FORWARD_LOCK | PackageManager.INSTALL_REPLACE_EXISTING;
2265 // First install.
2266 installFromRawResource("install.apk", R.raw.install,
2267 iFlags,
2268 false,
2269 false, -1,
2270 -1);
2271 // Replace now
2272 installFromRawResource("install.apk", R.raw.install,
2273 rFlags,
2274 true,
2275 false, -1,
2276 -1);
2277 }
2278
2279 /*
2280 * The following set of tests verify the installation of apps with
2281 * install location attribute set to internalOnly, preferExternal and auto.
2282 * The manifest option should dictate the install location.
2283 * public void testManifestI/E/A
2284 * TODO out of memory fall back behaviour.
2285 */
2286 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002287 public void testManifestI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002288 installFromRawResource("install.apk", R.raw.install_loc_internal,
2289 0,
2290 true,
2291 false, -1,
2292 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2293 }
2294
2295 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002296 public void testManifestE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002297 // Do not run on devices with emulated external storage.
2298 if (Environment.isExternalStorageEmulated()) {
2299 return;
2300 }
2301
2302 installFromRawResource("install.apk", R.raw.install_loc_sdcard,
2303 0,
2304 true,
2305 false, -1,
2306 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2307 }
2308
2309 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002310 public void testManifestA() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002311 installFromRawResource("install.apk", R.raw.install_loc_auto,
2312 0,
2313 true,
2314 false, -1,
2315 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2316 }
2317
2318 /*
2319 * The following set of tests verify the installation of apps
2320 * with install location attribute set to internalOnly, preferExternal and auto
2321 * for already existing apps. The manifest option should take precedence.
2322 * TODO add out of memory fall back behaviour.
2323 * testManifestI/E/AExistingI/E
2324 */
2325 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002326 public void testManifestIExistingI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002327 int iFlags = PackageManager.INSTALL_INTERNAL;
2328 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
2329 // First install.
2330 installFromRawResource("install.apk", R.raw.install,
2331 iFlags,
2332 false,
2333 false, -1,
2334 -1);
2335 // Replace now
2336 installFromRawResource("install.apk", R.raw.install_loc_internal,
2337 rFlags,
2338 true,
2339 false, -1,
2340 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2341 }
2342
2343 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002344 public void testManifestIExistingE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002345 // Do not run on devices with emulated external storage.
2346 if (Environment.isExternalStorageEmulated()) {
2347 return;
2348 }
2349
2350 int iFlags = PackageManager.INSTALL_EXTERNAL;
2351 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
2352 // First install.
2353 installFromRawResource("install.apk", R.raw.install,
2354 iFlags,
2355 false,
2356 false, -1,
2357 -1);
2358 // Replace now
2359 installFromRawResource("install.apk", R.raw.install_loc_internal,
2360 rFlags,
2361 true,
2362 false, -1,
2363 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2364 }
2365
2366 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002367 public void testManifestEExistingI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002368 // Do not run on devices with emulated external storage.
2369 if (Environment.isExternalStorageEmulated()) {
2370 return;
2371 }
2372
2373 int iFlags = PackageManager.INSTALL_INTERNAL;
2374 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
2375 // First install.
2376 installFromRawResource("install.apk", R.raw.install,
2377 iFlags,
2378 false,
2379 false, -1,
2380 -1);
2381 // Replace now
2382 installFromRawResource("install.apk", R.raw.install_loc_sdcard,
2383 rFlags,
2384 true,
2385 false, -1,
2386 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2387 }
2388
2389 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002390 public void testManifestEExistingE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002391 // Do not run on devices with emulated external storage.
2392 if (Environment.isExternalStorageEmulated()) {
2393 return;
2394 }
2395
2396 int iFlags = PackageManager.INSTALL_EXTERNAL;
2397 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
2398 // First install.
2399 installFromRawResource("install.apk", R.raw.install,
2400 iFlags,
2401 false,
2402 false, -1,
2403 -1);
2404 // Replace now
2405 installFromRawResource("install.apk", R.raw.install_loc_sdcard,
2406 rFlags,
2407 true,
2408 false, -1,
2409 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2410 }
2411
2412 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002413 public void testManifestAExistingI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002414 int iFlags = PackageManager.INSTALL_INTERNAL;
2415 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
2416 // First install.
2417 installFromRawResource("install.apk", R.raw.install,
2418 iFlags,
2419 false,
2420 false, -1,
2421 -1);
2422 // Replace now
2423 installFromRawResource("install.apk", R.raw.install_loc_auto,
2424 rFlags,
2425 true,
2426 false, -1,
2427 PackageInfo.INSTALL_LOCATION_AUTO);
2428 }
2429
2430 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002431 public void testManifestAExistingE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002432 // Do not run on devices with emulated external storage.
2433 if (Environment.isExternalStorageEmulated()) {
2434 return;
2435 }
2436
2437 int iFlags = PackageManager.INSTALL_EXTERNAL;
2438 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
2439 // First install.
2440 installFromRawResource("install.apk", R.raw.install,
2441 iFlags,
2442 false,
2443 false, -1,
2444 -1);
2445 // Replace now
2446 installFromRawResource("install.apk", R.raw.install_loc_auto,
2447 rFlags,
2448 true,
2449 false, -1,
2450 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2451 }
2452
Kenny Root25c925e2012-09-08 22:02:21 -07002453 /*
2454 * The following set of tests check install location for existing
2455 * application based on user setting.
2456 */
2457 private int getExpectedInstallLocation(int userSetting) {
2458 int iloc = PackageInfo.INSTALL_LOCATION_UNSPECIFIED;
2459 boolean enable = getUserSettingSetInstallLocation();
2460 if (enable) {
2461 if (userSetting == PackageHelper.APP_INSTALL_AUTO) {
2462 iloc = PackageInfo.INSTALL_LOCATION_AUTO;
2463 } else if (userSetting == PackageHelper.APP_INSTALL_EXTERNAL) {
2464 iloc = PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL;
2465 } else if (userSetting == PackageHelper.APP_INSTALL_INTERNAL) {
2466 iloc = PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY;
2467 }
2468 }
2469 return iloc;
2470 }
2471
2472 private void setExistingXUserX(int userSetting, int iFlags, int iloc) throws Exception {
2473 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
2474 // First install.
2475 installFromRawResource("install.apk", R.raw.install,
2476 iFlags,
2477 false,
2478 false, -1,
2479 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
2480 int origSetting = getDefaultInstallLoc();
2481 try {
2482 // Set user setting
2483 setInstallLoc(userSetting);
2484 // Replace now
2485 installFromRawResource("install.apk", R.raw.install,
2486 rFlags,
2487 true,
2488 false, -1,
2489 iloc);
2490 } finally {
2491 setInstallLoc(origSetting);
2492 }
2493 }
2494 @LargeTest
2495 public void testExistingIUserI() throws Exception {
2496 int userSetting = PackageHelper.APP_INSTALL_INTERNAL;
2497 int iFlags = PackageManager.INSTALL_INTERNAL;
2498 setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2499 }
Kenny Root3c676892011-01-12 10:58:40 -08002500
2501 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002502 public void testExistingIUserE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002503 // Do not run on devices with emulated external storage.
2504 if (Environment.isExternalStorageEmulated()) {
2505 return;
2506 }
2507
2508 int userSetting = PackageHelper.APP_INSTALL_EXTERNAL;
2509 int iFlags = PackageManager.INSTALL_INTERNAL;
2510 setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2511 }
2512
Kenny Root25c925e2012-09-08 22:02:21 -07002513 @LargeTest
2514 public void testExistingIUserA() throws Exception {
2515 int userSetting = PackageHelper.APP_INSTALL_AUTO;
2516 int iFlags = PackageManager.INSTALL_INTERNAL;
2517 setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2518 }
Kenny Root3c676892011-01-12 10:58:40 -08002519
2520 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002521 public void testExistingEUserI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002522 // Do not run on devices with emulated external storage.
2523 if (Environment.isExternalStorageEmulated()) {
2524 return;
2525 }
2526
2527 int userSetting = PackageHelper.APP_INSTALL_INTERNAL;
2528 int iFlags = PackageManager.INSTALL_EXTERNAL;
2529 setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2530 }
2531
2532 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002533 public void testExistingEUserE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002534 // Do not run on devices with emulated external storage.
2535 if (Environment.isExternalStorageEmulated()) {
2536 return;
2537 }
2538
2539 int userSetting = PackageHelper.APP_INSTALL_EXTERNAL;
2540 int iFlags = PackageManager.INSTALL_EXTERNAL;
2541 setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2542 }
2543
2544 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002545 public void testExistingEUserA() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002546 // Do not run on devices with emulated external storage.
2547 if (Environment.isExternalStorageEmulated()) {
2548 return;
2549 }
2550
2551 int userSetting = PackageHelper.APP_INSTALL_AUTO;
2552 int iFlags = PackageManager.INSTALL_EXTERNAL;
2553 setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2554 }
2555
Kenny Root25c925e2012-09-08 22:02:21 -07002556 /*
2557 * The following set of tests verify that the user setting defines
2558 * the install location.
2559 *
2560 */
2561 private boolean getUserSettingSetInstallLocation() {
2562 try {
Jeff Sharkey625239a2012-09-26 22:03:49 -07002563 return Settings.Global.getInt(
2564 mContext.getContentResolver(), Settings.Global.SET_INSTALL_LOCATION) != 0;
Kenny Root25c925e2012-09-08 22:02:21 -07002565 } catch (SettingNotFoundException e1) {
2566 }
2567 return false;
2568 }
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -07002569
Kenny Root25c925e2012-09-08 22:02:21 -07002570 private void setUserSettingSetInstallLocation(boolean value) {
Jeff Sharkey625239a2012-09-26 22:03:49 -07002571 Settings.Global.putInt(mContext.getContentResolver(),
2572 Settings.Global.SET_INSTALL_LOCATION, value ? 1 : 0);
Kenny Root25c925e2012-09-08 22:02:21 -07002573 }
2574
2575 private void setUserX(boolean enable, int userSetting, int iloc) throws Exception {
2576 boolean origUserSetting = getUserSettingSetInstallLocation();
2577 int origSetting = getDefaultInstallLoc();
2578 try {
2579 setUserSettingSetInstallLocation(enable);
2580 // Set user setting
2581 setInstallLoc(userSetting);
2582 // Replace now
2583 installFromRawResource("install.apk", R.raw.install,
2584 0,
2585 true,
2586 false, -1,
2587 iloc);
2588 } finally {
2589 // Restore original setting
2590 setUserSettingSetInstallLocation(origUserSetting);
2591 setInstallLoc(origSetting);
2592 }
2593 }
2594 @LargeTest
2595 public void testUserI() throws Exception {
2596 int userSetting = PackageHelper.APP_INSTALL_INTERNAL;
2597 int iloc = getExpectedInstallLocation(userSetting);
2598 setUserX(true, userSetting, iloc);
2599 }
Kenny Root3c676892011-01-12 10:58:40 -08002600
2601 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002602 public void testUserE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002603 // Do not run on devices with emulated external storage.
2604 if (Environment.isExternalStorageEmulated()) {
2605 return;
2606 }
2607
2608 int userSetting = PackageHelper.APP_INSTALL_EXTERNAL;
2609 int iloc = getExpectedInstallLocation(userSetting);
2610 setUserX(true, userSetting, iloc);
2611 }
2612
Kenny Root25c925e2012-09-08 22:02:21 -07002613 @LargeTest
2614 public void testUserA() throws Exception {
2615 int userSetting = PackageHelper.APP_INSTALL_AUTO;
2616 int iloc = getExpectedInstallLocation(userSetting);
2617 setUserX(true, userSetting, iloc);
2618 }
2619
2620 /*
2621 * The following set of tests turn on/off the basic
2622 * user setting for turning on install location.
2623 */
2624 @LargeTest
2625 public void testUserPrefOffUserI() throws Exception {
2626 int userSetting = PackageHelper.APP_INSTALL_INTERNAL;
2627 int iloc = PackageInfo.INSTALL_LOCATION_UNSPECIFIED;
2628 setUserX(false, userSetting, iloc);
2629 }
Kenny Root3c676892011-01-12 10:58:40 -08002630
2631 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002632 public void testUserPrefOffUserE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002633 // Do not run on devices with emulated external storage.
2634 if (Environment.isExternalStorageEmulated()) {
2635 return;
2636 }
2637
2638 int userSetting = PackageHelper.APP_INSTALL_EXTERNAL;
2639 int iloc = PackageInfo.INSTALL_LOCATION_UNSPECIFIED;
2640 setUserX(false, userSetting, iloc);
2641 }
2642
Kenny Root25c925e2012-09-08 22:02:21 -07002643 @LargeTest
2644 public void testUserPrefOffA() throws Exception {
2645 int userSetting = PackageHelper.APP_INSTALL_AUTO;
2646 int iloc = PackageInfo.INSTALL_LOCATION_UNSPECIFIED;
2647 setUserX(false, userSetting, iloc);
2648 }
Neal Nguyenedb979a2010-04-22 13:54:32 -07002649
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002650 static final String BASE_PERMISSIONS_DEFINED[] = new String[] {
2651 PERM_PACKAGE, "com.android.unit_tests.install_decl_perm",
2652 PERM_DEFINED,
Brett Chabot0dc59e72010-04-01 18:21:38 -07002653 "com.android.frameworks.coretests.NORMAL",
2654 "com.android.frameworks.coretests.DANGEROUS",
2655 "com.android.frameworks.coretests.SIGNATURE",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002656 };
Neal Nguyenedb979a2010-04-22 13:54:32 -07002657
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002658 static final String BASE_PERMISSIONS_UNDEFINED[] = new String[] {
Brett Chabot0dc59e72010-04-01 18:21:38 -07002659 PERM_PACKAGE, "com.android.frameworks.coretests.install_decl_perm",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002660 PERM_UNDEFINED,
Brett Chabot0dc59e72010-04-01 18:21:38 -07002661 "com.android.frameworks.coretests.NORMAL",
2662 "com.android.frameworks.coretests.DANGEROUS",
2663 "com.android.frameworks.coretests.SIGNATURE",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002664 };
Neal Nguyenedb979a2010-04-22 13:54:32 -07002665
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002666 static final String BASE_PERMISSIONS_USED[] = new String[] {
Brett Chabot0dc59e72010-04-01 18:21:38 -07002667 PERM_PACKAGE, "com.android.frameworks.coretests.install_use_perm_good",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002668 PERM_USED,
Brett Chabot0dc59e72010-04-01 18:21:38 -07002669 "com.android.frameworks.coretests.NORMAL",
2670 "com.android.frameworks.coretests.DANGEROUS",
2671 "com.android.frameworks.coretests.SIGNATURE",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002672 };
Neal Nguyenedb979a2010-04-22 13:54:32 -07002673
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002674 static final String BASE_PERMISSIONS_NOTUSED[] = new String[] {
Brett Chabot0dc59e72010-04-01 18:21:38 -07002675 PERM_PACKAGE, "com.android.frameworks.coretests.install_use_perm_good",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002676 PERM_NOTUSED,
Brett Chabot0dc59e72010-04-01 18:21:38 -07002677 "com.android.frameworks.coretests.NORMAL",
2678 "com.android.frameworks.coretests.DANGEROUS",
2679 "com.android.frameworks.coretests.SIGNATURE",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002680 };
Neal Nguyenedb979a2010-04-22 13:54:32 -07002681
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002682 static final String BASE_PERMISSIONS_SIGUSED[] = new String[] {
Brett Chabot0dc59e72010-04-01 18:21:38 -07002683 PERM_PACKAGE, "com.android.frameworks.coretests.install_use_perm_good",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002684 PERM_USED,
Brett Chabot0dc59e72010-04-01 18:21:38 -07002685 "com.android.frameworks.coretests.SIGNATURE",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002686 PERM_NOTUSED,
Brett Chabot0dc59e72010-04-01 18:21:38 -07002687 "com.android.frameworks.coretests.NORMAL",
2688 "com.android.frameworks.coretests.DANGEROUS",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002689 };
Neal Nguyenedb979a2010-04-22 13:54:32 -07002690
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002691 /*
2692 * Ensure that permissions are properly declared.
2693 */
Neal Nguyenedb979a2010-04-22 13:54:32 -07002694 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002695 public void testInstallDeclaresPermissions() throws Exception {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002696 InstallParams ip = null;
2697 InstallParams ip2 = null;
2698 try {
2699 // **: Upon installing a package, are its declared permissions published?
Neal Nguyenedb979a2010-04-22 13:54:32 -07002700
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002701 int iFlags = PackageManager.INSTALL_INTERNAL;
2702 int iApk = R.raw.install_decl_perm;
2703 ip = installFromRawResource("install.apk", iApk,
2704 iFlags, false,
2705 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2706 assertInstall(ip.pkg, iFlags, ip.pkg.installLocation);
2707 assertPermissions(BASE_PERMISSIONS_DEFINED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002708
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002709 // **: Upon installing package, are its permissions granted?
Neal Nguyenedb979a2010-04-22 13:54:32 -07002710
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002711 int i2Flags = PackageManager.INSTALL_INTERNAL;
2712 int i2Apk = R.raw.install_use_perm_good;
2713 ip2 = installFromRawResource("install2.apk", i2Apk,
2714 i2Flags, false,
2715 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2716 assertInstall(ip2.pkg, i2Flags, ip2.pkg.installLocation);
2717 assertPermissions(BASE_PERMISSIONS_USED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002718
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002719 // **: Upon removing but not deleting, are permissions retained?
Neal Nguyenedb979a2010-04-22 13:54:32 -07002720
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002721 GenericReceiver receiver = new DeleteReceiver(ip.pkg.packageName);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002722
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002723 try {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07002724 invokeDeletePackage(ip.pkg.packageName, PackageManager.DELETE_KEEP_DATA, receiver);
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002725 } catch (Exception e) {
2726 failStr(e);
2727 }
2728 assertPermissions(BASE_PERMISSIONS_DEFINED);
2729 assertPermissions(BASE_PERMISSIONS_USED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002730
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002731 // **: Upon re-installing, are permissions retained?
Neal Nguyenedb979a2010-04-22 13:54:32 -07002732
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002733 ip = installFromRawResource("install.apk", iApk,
2734 iFlags | PackageManager.INSTALL_REPLACE_EXISTING, false,
2735 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2736 assertInstall(ip.pkg, iFlags, ip.pkg.installLocation);
2737 assertPermissions(BASE_PERMISSIONS_DEFINED);
2738 assertPermissions(BASE_PERMISSIONS_USED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002739
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002740 // **: Upon deleting package, are all permissions removed?
Neal Nguyenedb979a2010-04-22 13:54:32 -07002741
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002742 try {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07002743 invokeDeletePackage(ip.pkg.packageName, 0, receiver);
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002744 ip = null;
2745 } catch (Exception e) {
2746 failStr(e);
2747 }
2748 assertPermissions(BASE_PERMISSIONS_UNDEFINED);
2749 assertPermissions(BASE_PERMISSIONS_NOTUSED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002750
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002751 // **: Delete package using permissions; nothing to check here.
Neal Nguyenedb979a2010-04-22 13:54:32 -07002752
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002753 GenericReceiver receiver2 = new DeleteReceiver(ip2.pkg.packageName);
2754 try {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07002755 invokeDeletePackage(ip2.pkg.packageName, 0, receiver);
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002756 ip2 = null;
2757 } catch (Exception e) {
2758 failStr(e);
2759 }
Neal Nguyenedb979a2010-04-22 13:54:32 -07002760
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002761 // **: Re-install package using permissions; no permissions can be granted.
Neal Nguyenedb979a2010-04-22 13:54:32 -07002762
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002763 ip2 = installFromRawResource("install2.apk", i2Apk,
2764 i2Flags, false,
2765 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2766 assertInstall(ip2.pkg, i2Flags, ip2.pkg.installLocation);
2767 assertPermissions(BASE_PERMISSIONS_NOTUSED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002768
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002769 // **: Upon installing declaring package, are sig permissions granted
2770 // to other apps (but not other perms)?
Neal Nguyenedb979a2010-04-22 13:54:32 -07002771
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002772 ip = installFromRawResource("install.apk", iApk,
2773 iFlags, false,
2774 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2775 assertInstall(ip.pkg, iFlags, ip.pkg.installLocation);
2776 assertPermissions(BASE_PERMISSIONS_DEFINED);
2777 assertPermissions(BASE_PERMISSIONS_SIGUSED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002778
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002779 // **: Re-install package using permissions; are all permissions granted?
Neal Nguyenedb979a2010-04-22 13:54:32 -07002780
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002781 ip2 = installFromRawResource("install2.apk", i2Apk,
2782 i2Flags | PackageManager.INSTALL_REPLACE_EXISTING, false,
2783 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2784 assertInstall(ip2.pkg, i2Flags, ip2.pkg.installLocation);
2785 assertPermissions(BASE_PERMISSIONS_NOTUSED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002786
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002787 // **: Upon deleting package, are all permissions removed?
Neal Nguyenedb979a2010-04-22 13:54:32 -07002788
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002789 try {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07002790 invokeDeletePackage(ip.pkg.packageName, 0, receiver);
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002791 ip = null;
2792 } catch (Exception e) {
2793 failStr(e);
2794 }
2795 assertPermissions(BASE_PERMISSIONS_UNDEFINED);
2796 assertPermissions(BASE_PERMISSIONS_NOTUSED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002797
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002798 // **: Delete package using permissions; nothing to check here.
Neal Nguyenedb979a2010-04-22 13:54:32 -07002799
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002800 try {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07002801 invokeDeletePackage(ip2.pkg.packageName, 0, receiver);
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002802 ip2 = null;
2803 } catch (Exception e) {
2804 failStr(e);
2805 }
Neal Nguyenedb979a2010-04-22 13:54:32 -07002806
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002807 } finally {
2808 if (ip2 != null) {
2809 cleanUpInstall(ip2);
2810 }
2811 if (ip != null) {
2812 cleanUpInstall(ip);
2813 }
2814 }
2815 }
2816
Suchi Amalapurapuebb83ad2010-03-19 11:55:29 -07002817 /*
2818 * Ensure that permissions are properly declared.
2819 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07002820 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002821 public void testInstallOnSdPermissionsUnmount() throws Exception {
Suchi Amalapurapuebb83ad2010-03-19 11:55:29 -07002822 InstallParams ip = null;
Kenny Rootd7b421b2010-08-05 08:40:00 -07002823 boolean origMediaState = checkMediaState(Environment.MEDIA_MOUNTED);
Suchi Amalapurapuebb83ad2010-03-19 11:55:29 -07002824 try {
2825 // **: Upon installing a package, are its declared permissions published?
2826 int iFlags = PackageManager.INSTALL_INTERNAL;
2827 int iApk = R.raw.install_decl_perm;
2828 ip = installFromRawResource("install.apk", iApk,
2829 iFlags, false,
2830 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2831 assertInstall(ip.pkg, iFlags, ip.pkg.installLocation);
2832 assertPermissions(BASE_PERMISSIONS_DEFINED);
2833 // Unmount media here
2834 assertTrue(unmountMedia());
2835 // Mount media again
2836 mountMedia();
2837 //Check permissions now
2838 assertPermissions(BASE_PERMISSIONS_DEFINED);
2839 } finally {
2840 if (ip != null) {
2841 cleanUpInstall(ip);
2842 }
2843 }
2844 }
Suchi Amalapurapuc7537ee2010-03-24 09:27:19 -07002845
2846 /* This test creates a stale container via MountService and then installs
2847 * a package and verifies that the stale container is cleaned up and install
2848 * is successful.
2849 * Please note that this test is very closely tied to the framework's
2850 * naming convention for secure containers.
2851 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07002852 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002853 public void testInstallSdcardStaleContainer() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002854 // Do not run on devices with emulated external storage.
2855 if (Environment.isExternalStorageEmulated()) {
2856 return;
2857 }
2858
Kenny Rootd7b421b2010-08-05 08:40:00 -07002859 boolean origMediaState = checkMediaState(Environment.MEDIA_MOUNTED);
Suchi Amalapurapuc7537ee2010-03-24 09:27:19 -07002860 try {
Kenny Rootd7b421b2010-08-05 08:40:00 -07002861 // Mount media first
2862 mountMedia();
Suchi Amalapurapuc7537ee2010-03-24 09:27:19 -07002863 String outFileName = "install.apk";
2864 int rawResId = R.raw.install;
2865 PackageManager pm = mContext.getPackageManager();
2866 File filesDir = mContext.getFilesDir();
2867 File outFile = new File(filesDir, outFileName);
2868 Uri packageURI = getInstallablePackage(rawResId, outFile);
2869 PackageParser.Package pkg = parsePackage(packageURI);
2870 assertNotNull(pkg);
2871 // Install an app on sdcard.
2872 installFromRawResource(outFileName, rawResId,
2873 PackageManager.INSTALL_EXTERNAL, false,
2874 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
2875 // Unmount sdcard
2876 unmountMedia();
2877 // Delete the app on sdcard to leave a stale container on sdcard.
2878 GenericReceiver receiver = new DeleteReceiver(pkg.packageName);
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07002879 assertTrue(invokeDeletePackage(pkg.packageName, 0, receiver));
Suchi Amalapurapuc7537ee2010-03-24 09:27:19 -07002880 mountMedia();
2881 // Reinstall the app and make sure it gets installed.
2882 installFromRawResource(outFileName, rawResId,
2883 PackageManager.INSTALL_EXTERNAL, true,
2884 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002885 } catch (Exception e) {
Suchi Amalapurapuc7537ee2010-03-24 09:27:19 -07002886 failStr(e.getMessage());
2887 } finally {
2888 if (origMediaState) {
2889 mountMedia();
2890 } else {
2891 unmountMedia();
2892 }
2893
2894 }
2895 }
Suchi Amalapurapu1ace5bc2010-05-13 12:05:53 -07002896
2897 /* This test installs an application on sdcard and unmounts media.
2898 * The app is then re-installed on internal storage. The sdcard is mounted
2899 * and verified that the re-installation on internal storage takes precedence.
2900 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07002901 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002902 public void testInstallSdcardStaleContainerReinstall() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002903 // Do not run on devices with emulated external storage.
2904 if (Environment.isExternalStorageEmulated()) {
2905 return;
2906 }
2907
Kenny Rootd7b421b2010-08-05 08:40:00 -07002908 boolean origMediaState = checkMediaState(Environment.MEDIA_MOUNTED);
Suchi Amalapurapu1ace5bc2010-05-13 12:05:53 -07002909 try {
2910 // Mount media first
2911 mountMedia();
2912 String outFileName = "install.apk";
2913 int rawResId = R.raw.install;
2914 PackageManager pm = mContext.getPackageManager();
2915 File filesDir = mContext.getFilesDir();
2916 File outFile = new File(filesDir, outFileName);
2917 Uri packageURI = getInstallablePackage(rawResId, outFile);
2918 PackageParser.Package pkg = parsePackage(packageURI);
2919 assertNotNull(pkg);
2920 // Install an app on sdcard.
2921 installFromRawResource(outFileName, rawResId,
2922 PackageManager.INSTALL_EXTERNAL, false,
2923 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
2924 // Unmount sdcard
2925 unmountMedia();
2926 // Reinstall the app and make sure it gets installed on internal storage.
2927 installFromRawResource(outFileName, rawResId,
2928 PackageManager.INSTALL_REPLACE_EXISTING, false,
2929 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
2930 mountMedia();
2931 // Verify that the app installed is on internal storage.
Kenny Root25c925e2012-09-08 22:02:21 -07002932 assertInstall(pkg, 0, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
Suchi Amalapurapu1ace5bc2010-05-13 12:05:53 -07002933 } catch (Exception e) {
2934 failStr(e.getMessage());
2935 } finally {
2936 if (origMediaState) {
2937 mountMedia();
2938 } else {
2939 unmountMedia();
2940 }
Suchi Amalapurapu1ace5bc2010-05-13 12:05:53 -07002941 }
2942 }
Kenny Root3c676892011-01-12 10:58:40 -08002943
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002944 /*
2945 * The following series of tests are related to upgrading apps with
Neal Nguyenedb979a2010-04-22 13:54:32 -07002946 * different certificates.
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002947 */
2948 private int APP1_UNSIGNED = R.raw.install_app1_unsigned;
Kenny Root25c925e2012-09-08 22:02:21 -07002949
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002950 private int APP1_CERT1 = R.raw.install_app1_cert1;
Kenny Root25c925e2012-09-08 22:02:21 -07002951
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002952 private int APP1_CERT2 = R.raw.install_app1_cert2;
Kenny Root25c925e2012-09-08 22:02:21 -07002953
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002954 private int APP1_CERT1_CERT2 = R.raw.install_app1_cert1_cert2;
Kenny Root25c925e2012-09-08 22:02:21 -07002955
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002956 private int APP1_CERT3_CERT4 = R.raw.install_app1_cert3_cert4;
Kenny Root25c925e2012-09-08 22:02:21 -07002957
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002958 private int APP1_CERT3 = R.raw.install_app1_cert3;
Kenny Root25c925e2012-09-08 22:02:21 -07002959
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002960 private int APP2_UNSIGNED = R.raw.install_app2_unsigned;
Kenny Root25c925e2012-09-08 22:02:21 -07002961
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002962 private int APP2_CERT1 = R.raw.install_app2_cert1;
Kenny Root25c925e2012-09-08 22:02:21 -07002963
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002964 private int APP2_CERT2 = R.raw.install_app2_cert2;
Kenny Root25c925e2012-09-08 22:02:21 -07002965
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002966 private int APP2_CERT1_CERT2 = R.raw.install_app2_cert1_cert2;
Kenny Root25c925e2012-09-08 22:02:21 -07002967
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002968 private int APP2_CERT3 = R.raw.install_app2_cert3;
2969
Kenny Root25c925e2012-09-08 22:02:21 -07002970 private InstallParams replaceCerts(int apk1, int apk2, boolean cleanUp, boolean fail,
2971 int retCode) throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002972 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
2973 String apk1Name = "install1.apk";
2974 String apk2Name = "install2.apk";
2975 PackageParser.Package pkg1 = getParsedPackage(apk1Name, apk1);
2976 try {
2977 InstallParams ip = installFromRawResource(apk1Name, apk1, 0, false,
2978 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
2979 installFromRawResource(apk2Name, apk2, rFlags, false,
2980 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
2981 return ip;
2982 } catch (Exception e) {
2983 failStr(e.getMessage());
2984 } finally {
2985 if (cleanUp) {
2986 cleanUpInstall(pkg1.packageName);
2987 }
2988 }
2989 return null;
2990 }
Kenny Root25c925e2012-09-08 22:02:21 -07002991
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002992 /*
2993 * Test that an app signed with two certificates can be upgraded by the
2994 * same app signed with two certificates.
2995 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07002996 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002997 public void testReplaceMatchAllCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002998 replaceCerts(APP1_CERT1_CERT2, APP1_CERT1_CERT2, true, false, -1);
2999 }
3000
3001 /*
3002 * Test that an app signed with two certificates cannot be upgraded
3003 * by an app signed with a different certificate.
3004 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07003005 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003006 public void testReplaceMatchNoCerts1() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003007 replaceCerts(APP1_CERT1_CERT2, APP1_CERT3, true, true,
3008 PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES);
3009 }
Kenny Root25c925e2012-09-08 22:02:21 -07003010
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003011 /*
3012 * Test that an app signed with two certificates cannot be upgraded
3013 * by an app signed with a different certificate.
3014 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07003015 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003016 public void testReplaceMatchNoCerts2() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003017 replaceCerts(APP1_CERT1_CERT2, APP1_CERT3_CERT4, true, true,
3018 PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES);
3019 }
Kenny Root25c925e2012-09-08 22:02:21 -07003020
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003021 /*
3022 * Test that an app signed with two certificates cannot be upgraded by
3023 * an app signed with a subset of initial certificates.
3024 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07003025 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003026 public void testReplaceMatchSomeCerts1() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003027 replaceCerts(APP1_CERT1_CERT2, APP1_CERT1, true, true,
3028 PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES);
3029 }
Kenny Root25c925e2012-09-08 22:02:21 -07003030
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003031 /*
3032 * Test that an app signed with two certificates cannot be upgraded by
3033 * an app signed with the last certificate.
3034 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07003035 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003036 public void testReplaceMatchSomeCerts2() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003037 replaceCerts(APP1_CERT1_CERT2, APP1_CERT2, true, true,
3038 PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES);
3039 }
Kenny Root25c925e2012-09-08 22:02:21 -07003040
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003041 /*
3042 * Test that an app signed with a certificate can be upgraded by app
3043 * signed with a superset of certificates.
3044 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07003045 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003046 public void testReplaceMatchMoreCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003047 replaceCerts(APP1_CERT1, APP1_CERT1_CERT2, true, true,
3048 PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES);
3049 }
Kenny Root25c925e2012-09-08 22:02:21 -07003050
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003051 /*
3052 * Test that an app signed with a certificate can be upgraded by app
3053 * signed with a superset of certificates. Then verify that the an app
3054 * signed with the original set of certs cannot upgrade the new one.
3055 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07003056 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003057 public void testReplaceMatchMoreCertsReplaceSomeCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003058 InstallParams ip = replaceCerts(APP1_CERT1, APP1_CERT1_CERT2, false, true,
3059 PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES);
3060 try {
3061 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
3062 installFromRawResource("install.apk", APP1_CERT1, rFlags, false,
3063 false, -1,
3064 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3065 } catch (Exception e) {
3066 failStr(e.getMessage());
3067 } finally {
3068 if (ip != null) {
3069 cleanUpInstall(ip);
3070 }
3071 }
3072 }
Kenny Root25c925e2012-09-08 22:02:21 -07003073
3074 /**
dcashman55b10782014-04-09 14:20:38 -07003075 * The following tests are related to testing KeySets-based key rotation
3076 */
3077 /*
3078 * Check if an apk which does not specify an upgrade-keyset may be upgraded
3079 * by an apk which does
3080 */
3081 public void testNoKSToUpgradeKS() throws Exception {
3082 replaceCerts(R.raw.keyset_sa_unone, R.raw.keyset_sa_ua, true, false, -1);
3083 }
3084
3085 /*
3086 * Check if an apk which does specify an upgrade-keyset may be downgraded to
3087 * an apk which does not
3088 */
3089 public void testUpgradeKSToNoKS() throws Exception {
3090 replaceCerts(R.raw.keyset_sa_ua, R.raw.keyset_sa_unone, true, false, -1);
3091 }
3092
3093 /*
3094 * Check if an apk signed by a key other than the upgrade keyset can update
3095 * an app
3096 */
3097 public void testUpgradeKSWithWrongKey() throws Exception {
3098 replaceCerts(R.raw.keyset_sa_ua, R.raw.keyset_sb_ua, true, true,
3099 PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES);
3100 }
3101
3102 /*
3103 * Check if an apk signed by its signing key, which is not an upgrade key,
3104 * can upgrade an app.
3105 */
3106 public void testUpgradeKSWithWrongSigningKey() throws Exception {
3107 replaceCerts(R.raw.keyset_sa_ub, R.raw.keyset_sa_ub, true, true,
3108 PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES);
3109 }
3110
3111 /*
3112 * Check if an apk signed by its upgrade key, which is not its signing key,
3113 * can upgrade an app.
3114 */
3115 public void testUpgradeKSWithUpgradeKey() throws Exception {
3116 replaceCerts(R.raw.keyset_sa_ub, R.raw.keyset_sb_ub, true, false, -1);
3117 }
3118 /*
3119 * Check if an apk signed by its upgrade key, which is its signing key, can
3120 * upgrade an app.
3121 */
3122 public void testUpgradeKSWithSigningUpgradeKey() throws Exception {
3123 replaceCerts(R.raw.keyset_sa_ua, R.raw.keyset_sa_ua, true, false, -1);
3124 }
3125
3126 /*
3127 * Check if an apk signed by multiple keys, one of which is its upgrade key,
3128 * can upgrade an app.
3129 */
3130 public void testMultipleUpgradeKSWithUpgradeKey() throws Exception {
3131 replaceCerts(R.raw.keyset_sa_ua, R.raw.keyset_sab_ua, true, false, -1);
3132 }
3133
3134 /*
3135 * Check if an apk signed by multiple keys, one of which is its signing key,
3136 * but none of which is an upgrade key, can upgrade an app.
3137 */
3138 public void testMultipleUpgradeKSWithSigningKey() throws Exception {
3139 replaceCerts(R.raw.keyset_sau_ub, R.raw.keyset_sa_ua, true, true,
3140 PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES);
3141 }
3142
3143 /*
3144 * Check if an apk which defines multiple (two) upgrade keysets is
3145 * upgrade-able by either.
3146 */
3147 public void testUpgradeKSWithMultipleUpgradeKeySets() throws Exception {
3148 replaceCerts(R.raw.keyset_sa_ua_ub, R.raw.keyset_sa_ua, true, false, -1);
3149 replaceCerts(R.raw.keyset_sa_ua_ub, R.raw.keyset_sb_ub, true, false, -1);
3150 }
3151
3152 /*
3153 * Check if an apk's sigs are changed after upgrading with a non-signing
3154 * key.
3155 *
3156 * TODO: consider checking against hard-coded Signatures in the Sig-tests
3157 */
3158 public void testSigChangeAfterUpgrade() throws Exception {
3159 // install original apk and grab sigs
3160 installFromRawResource("tmp.apk", R.raw.keyset_sa_ub,
3161 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3162 PackageManager pm = getPm();
3163 String pkgName = "com.android.frameworks.coretests.keysets";
3164 PackageInfo pi = pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);
3165 assertTrue("Package should only have one signature, sig A",
3166 pi.signatures.length == 1);
3167 String sigBefore = pi.signatures[0].toCharsString();
3168 // install apk signed by different upgrade KeySet
3169 installFromRawResource("tmp2.apk", R.raw.keyset_sb_ub,
3170 PackageManager.INSTALL_REPLACE_EXISTING, false, false, -1,
3171 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3172 pi = pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);
3173 assertTrue("Package should only have one signature, sig B",
3174 pi.signatures.length == 1);
3175 String sigAfter = pi.signatures[0].toCharsString();
3176 assertFalse("Package signatures did not change after upgrade!",
3177 sigBefore.equals(sigAfter));
3178 cleanUpInstall(pkgName);
3179 }
3180
3181 /*
3182 * Check if an apk's sig is the same after upgrading with a signing
3183 * key.
3184 */
3185 public void testSigSameAfterUpgrade() throws Exception {
3186 // install original apk and grab sigs
3187 installFromRawResource("tmp.apk", R.raw.keyset_sa_ua,
3188 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3189 PackageManager pm = getPm();
3190 String pkgName = "com.android.frameworks.coretests.keysets";
3191 PackageInfo pi = pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);
3192 assertTrue("Package should only have one signature, sig A",
3193 pi.signatures.length == 1);
3194 String sigBefore = pi.signatures[0].toCharsString();
3195 // install apk signed by same upgrade KeySet
3196 installFromRawResource("tmp2.apk", R.raw.keyset_sa_ua,
3197 PackageManager.INSTALL_REPLACE_EXISTING, false, false, -1,
3198 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3199 pi = pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);
3200 assertTrue("Package should only have one signature, sig A",
3201 pi.signatures.length == 1);
3202 String sigAfter = pi.signatures[0].toCharsString();
3203 assertTrue("Package signatures changed after upgrade!",
3204 sigBefore.equals(sigAfter));
3205 cleanUpInstall(pkgName);
3206 }
3207
3208 /*
3209 * Check if an apk's sigs are the same after upgrading with an app with
3210 * a subset of the original signing keys.
3211 */
3212 public void testSigRemovedAfterUpgrade() throws Exception {
3213 // install original apk and grab sigs
3214 installFromRawResource("tmp.apk", R.raw.keyset_sab_ua,
3215 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3216 PackageManager pm = getPm();
3217 String pkgName = "com.android.frameworks.coretests.keysets";
3218 PackageInfo pi = pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);
3219 assertTrue("Package should have two signatures, sig A and sig B",
3220 pi.signatures.length == 2);
3221 Set<String> sigsBefore = new HashSet<String>();
3222 for (int i = 0; i < pi.signatures.length; i++) {
3223 sigsBefore.add(pi.signatures[i].toCharsString());
3224 }
3225 // install apk signed subset upgrade KeySet
3226 installFromRawResource("tmp2.apk", R.raw.keyset_sa_ua,
3227 PackageManager.INSTALL_REPLACE_EXISTING, false, false, -1,
3228 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3229 pi = pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);
3230 assertTrue("Package should only have one signature, sig A",
3231 pi.signatures.length == 1);
3232 String sigAfter = pi.signatures[0].toCharsString();
3233 assertTrue("Original package signatures did not contain new sig",
3234 sigsBefore.contains(sigAfter));
3235 cleanUpInstall(pkgName);
3236 }
3237
3238 /*
3239 * Check if an apk's sigs are added to after upgrading with an app with
3240 * a superset of the original signing keys.
3241 */
3242 public void testSigAddedAfterUpgrade() throws Exception {
3243 // install original apk and grab sigs
3244 installFromRawResource("tmp.apk", R.raw.keyset_sa_ua,
3245 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3246 PackageManager pm = getPm();
3247 String pkgName = "com.android.frameworks.coretests.keysets";
3248 PackageInfo pi = pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);
3249 assertTrue("Package should only have one signature, sig A",
3250 pi.signatures.length == 1);
3251 String sigBefore = pi.signatures[0].toCharsString();
3252 // install apk signed subset upgrade KeySet
3253 installFromRawResource("tmp2.apk", R.raw.keyset_sab_ua,
3254 PackageManager.INSTALL_REPLACE_EXISTING, false, false, -1,
3255 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3256 pi = pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);
3257 assertTrue("Package should have two signatures, sig A and sig B",
3258 pi.signatures.length == 2);
3259 Set<String> sigsAfter = new HashSet<String>();
3260 for (int i = 0; i < pi.signatures.length; i++) {
3261 sigsAfter.add(pi.signatures[i].toCharsString());
3262 }
3263 assertTrue("Package signatures did not change after upgrade!",
3264 sigsAfter.contains(sigBefore));
3265 cleanUpInstall(pkgName);
3266 }
3267
3268 /*
3269 * Check if an apk gains signature-level permission after changing to the a
3270 * new signature, for which a permission should be granted.
3271 */
3272 public void testUpgradeSigPermGained() throws Exception {
3273 // install apk which defines permission
3274 installFromRawResource("permDef.apk", R.raw.keyset_permdef_sa_unone,
3275 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3276 // install apk which uses permission but does not have sig
3277 installFromRawResource("permUse.apk", R.raw.keyset_permuse_sb_ua_ub,
3278 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3279 // verify that package does not have perm before
3280 PackageManager pm = getPm();
3281 String permPkgName = "com.android.frameworks.coretests.keysets_permdef";
3282 String pkgName = "com.android.frameworks.coretests.keysets";
3283 String permName = "com.android.frameworks.coretests.keysets_permdef.keyset_perm";
3284 assertFalse("keyset permission granted to app without same signature!",
3285 pm.checkPermission(permName, pkgName)
3286 == PackageManager.PERMISSION_GRANTED);
3287 // upgrade to apk with perm signature
3288 installFromRawResource("permUse2.apk", R.raw.keyset_permuse_sa_ua_ub,
3289 PackageManager.INSTALL_REPLACE_EXISTING, false, false, -1,
3290 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3291 assertTrue("keyset permission not granted to app after upgrade to same sig",
3292 pm.checkPermission(permName, pkgName)
3293 == PackageManager.PERMISSION_GRANTED);
3294 cleanUpInstall(permPkgName);
3295 cleanUpInstall(pkgName);
3296 }
3297
3298 /*
3299 * Check if an apk loses signature-level permission after changing to the a
3300 * new signature, from one which a permission should be granted.
3301 */
3302 public void testUpgradeSigPermLost() throws Exception {
3303 // install apk which defines permission
3304 installFromRawResource("permDef.apk", R.raw.keyset_permdef_sa_unone,
3305 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3306 // install apk which uses permission, signed by same sig
3307 installFromRawResource("permUse.apk", R.raw.keyset_permuse_sa_ua_ub,
3308 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3309 // verify that package does not have perm before
3310 PackageManager pm = getPm();
3311 String permPkgName = "com.android.frameworks.coretests.keysets_permdef";
3312 String pkgName = "com.android.frameworks.coretests.keysets";
3313 String permName = "com.android.frameworks.coretests.keysets_permdef.keyset_perm";
3314 assertTrue("keyset permission not granted to app with same sig",
3315 pm.checkPermission(permName, pkgName)
3316 == PackageManager.PERMISSION_GRANTED);
3317 // upgrade to apk without perm signature
3318 installFromRawResource("permUse2.apk", R.raw.keyset_permuse_sb_ua_ub,
3319 PackageManager.INSTALL_REPLACE_EXISTING, false, false, -1,
3320 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3321
3322 assertFalse("keyset permission not revoked from app which upgraded to a "
3323 + "different signature",
3324 pm.checkPermission(permName, pkgName)
3325 == PackageManager.PERMISSION_GRANTED);
3326 cleanUpInstall(permPkgName);
3327 cleanUpInstall(pkgName);
3328 }
3329
3330 /**
Kenny Root25c925e2012-09-08 22:02:21 -07003331 * The following tests are related to testing the checkSignatures api.
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003332 */
Kenny Root25c925e2012-09-08 22:02:21 -07003333 private void checkSignatures(int apk1, int apk2, int expMatchResult) throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003334 checkSharedSignatures(apk1, apk2, true, false, -1, expMatchResult);
3335 }
Kenny Root25c925e2012-09-08 22:02:21 -07003336
Brett Chabotf76c56b2010-07-26 17:28:17 -07003337 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003338 public void testCheckSignaturesAllMatch() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003339 int apk1 = APP1_CERT1_CERT2;
3340 int apk2 = APP2_CERT1_CERT2;
3341 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_MATCH);
3342 }
Kenny Root25c925e2012-09-08 22:02:21 -07003343
Brett Chabotf76c56b2010-07-26 17:28:17 -07003344 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003345 public void testCheckSignaturesNoMatch() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003346 int apk1 = APP1_CERT1;
3347 int apk2 = APP2_CERT2;
3348 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_NO_MATCH);
3349 }
Kenny Root25c925e2012-09-08 22:02:21 -07003350
Brett Chabotf76c56b2010-07-26 17:28:17 -07003351 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003352 public void testCheckSignaturesSomeMatch1() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003353 int apk1 = APP1_CERT1_CERT2;
3354 int apk2 = APP2_CERT1;
3355 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_NO_MATCH);
3356 }
Kenny Root25c925e2012-09-08 22:02:21 -07003357
Brett Chabotf76c56b2010-07-26 17:28:17 -07003358 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003359 public void testCheckSignaturesSomeMatch2() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003360 int apk1 = APP1_CERT1_CERT2;
3361 int apk2 = APP2_CERT2;
3362 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_NO_MATCH);
3363 }
Kenny Root25c925e2012-09-08 22:02:21 -07003364
Brett Chabotf76c56b2010-07-26 17:28:17 -07003365 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003366 public void testCheckSignaturesMoreMatch() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003367 int apk1 = APP1_CERT1;
3368 int apk2 = APP2_CERT1_CERT2;
3369 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_NO_MATCH);
3370 }
Kenny Root25c925e2012-09-08 22:02:21 -07003371
Brett Chabotf76c56b2010-07-26 17:28:17 -07003372 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003373 public void testCheckSignaturesUnknown() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003374 int apk1 = APP1_CERT1_CERT2;
3375 int apk2 = APP2_CERT1_CERT2;
3376 String apk1Name = "install1.apk";
3377 String apk2Name = "install2.apk";
3378 InstallParams ip1 = null;
3379
3380 try {
3381 ip1 = installFromRawResource(apk1Name, apk1, 0, false,
3382 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3383 PackageManager pm = mContext.getPackageManager();
3384 // Delete app2
3385 File filesDir = mContext.getFilesDir();
3386 File outFile = new File(filesDir, apk2Name);
3387 int rawResId = apk2;
3388 Uri packageURI = getInstallablePackage(rawResId, outFile);
3389 PackageParser.Package pkg = parsePackage(packageURI);
Kenny Roota3e90792012-10-18 10:58:36 -07003390 getPm().deletePackage(pkg.packageName, null, PackageManager.DELETE_ALL_USERS);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003391 // Check signatures now
3392 int match = mContext.getPackageManager().checkSignatures(
3393 ip1.pkg.packageName, pkg.packageName);
3394 assertEquals(PackageManager.SIGNATURE_UNKNOWN_PACKAGE, match);
3395 } finally {
3396 if (ip1 != null) {
3397 cleanUpInstall(ip1);
3398 }
3399 }
3400 }
Kenny Root25c925e2012-09-08 22:02:21 -07003401
Brett Chabotf76c56b2010-07-26 17:28:17 -07003402 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003403 public void testInstallNoCertificates() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003404 int apk1 = APP1_UNSIGNED;
3405 String apk1Name = "install1.apk";
3406 InstallParams ip1 = null;
3407
3408 try {
3409 installFromRawResource(apk1Name, apk1, 0, false,
3410 true, PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES,
3411 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3412 } finally {
3413 }
3414 }
Kenny Root25c925e2012-09-08 22:02:21 -07003415
3416 /*
3417 * The following tests are related to apps using shared uids signed with
3418 * different certs.
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003419 */
3420 private int SHARED1_UNSIGNED = R.raw.install_shared1_unsigned;
Kenny Root25c925e2012-09-08 22:02:21 -07003421
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003422 private int SHARED1_CERT1 = R.raw.install_shared1_cert1;
Kenny Root25c925e2012-09-08 22:02:21 -07003423
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003424 private int SHARED1_CERT2 = R.raw.install_shared1_cert2;
Kenny Root25c925e2012-09-08 22:02:21 -07003425
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003426 private int SHARED1_CERT1_CERT2 = R.raw.install_shared1_cert1_cert2;
Kenny Root25c925e2012-09-08 22:02:21 -07003427
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003428 private int SHARED2_UNSIGNED = R.raw.install_shared2_unsigned;
Kenny Root25c925e2012-09-08 22:02:21 -07003429
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003430 private int SHARED2_CERT1 = R.raw.install_shared2_cert1;
Kenny Root25c925e2012-09-08 22:02:21 -07003431
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003432 private int SHARED2_CERT2 = R.raw.install_shared2_cert2;
Kenny Root25c925e2012-09-08 22:02:21 -07003433
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003434 private int SHARED2_CERT1_CERT2 = R.raw.install_shared2_cert1_cert2;
Kenny Root25c925e2012-09-08 22:02:21 -07003435
3436 private void checkSharedSignatures(int apk1, int apk2, boolean cleanUp, boolean fail,
3437 int retCode, int expMatchResult) throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003438 String apk1Name = "install1.apk";
3439 String apk2Name = "install2.apk";
3440 PackageParser.Package pkg1 = getParsedPackage(apk1Name, apk1);
3441 PackageParser.Package pkg2 = getParsedPackage(apk2Name, apk2);
3442
3443 try {
3444 // Clean up before testing first.
3445 cleanUpInstall(pkg1.packageName);
3446 cleanUpInstall(pkg2.packageName);
Kenny Root25c925e2012-09-08 22:02:21 -07003447 installFromRawResource(apk1Name, apk1, 0, false, false, -1,
3448 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003449 if (fail) {
Kenny Root25c925e2012-09-08 22:02:21 -07003450 installFromRawResource(apk2Name, apk2, 0, false, true, retCode,
3451 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003452 } else {
Kenny Root25c925e2012-09-08 22:02:21 -07003453 installFromRawResource(apk2Name, apk2, 0, false, false, -1,
3454 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3455 int match = mContext.getPackageManager().checkSignatures(pkg1.packageName,
3456 pkg2.packageName);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003457 assertEquals(expMatchResult, match);
3458 }
3459 } finally {
3460 if (cleanUp) {
3461 cleanUpInstall(pkg1.packageName);
3462 cleanUpInstall(pkg2.packageName);
3463 }
3464 }
3465 }
Kenny Root25c925e2012-09-08 22:02:21 -07003466
Brett Chabotf76c56b2010-07-26 17:28:17 -07003467 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003468 public void testCheckSignaturesSharedAllMatch() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003469 int apk1 = SHARED1_CERT1_CERT2;
3470 int apk2 = SHARED2_CERT1_CERT2;
3471 boolean fail = false;
3472 int retCode = -1;
3473 int expMatchResult = PackageManager.SIGNATURE_MATCH;
3474 checkSharedSignatures(apk1, apk2, true, fail, retCode, expMatchResult);
3475 }
Kenny Root25c925e2012-09-08 22:02:21 -07003476
Brett Chabotf76c56b2010-07-26 17:28:17 -07003477 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003478 public void testCheckSignaturesSharedNoMatch() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003479 int apk1 = SHARED1_CERT1;
3480 int apk2 = SHARED2_CERT2;
3481 boolean fail = true;
3482 int retCode = PackageManager.INSTALL_FAILED_SHARED_USER_INCOMPATIBLE;
3483 int expMatchResult = -1;
3484 checkSharedSignatures(apk1, apk2, true, fail, retCode, expMatchResult);
3485 }
Kenny Root25c925e2012-09-08 22:02:21 -07003486
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003487 /*
Kenny Root25c925e2012-09-08 22:02:21 -07003488 * Test that an app signed with cert1 and cert2 cannot be replaced when
3489 * signed with cert1 alone.
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003490 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07003491 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003492 public void testCheckSignaturesSharedSomeMatch1() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003493 int apk1 = SHARED1_CERT1_CERT2;
3494 int apk2 = SHARED2_CERT1;
3495 boolean fail = true;
3496 int retCode = PackageManager.INSTALL_FAILED_SHARED_USER_INCOMPATIBLE;
3497 int expMatchResult = -1;
3498 checkSharedSignatures(apk1, apk2, true, fail, retCode, expMatchResult);
3499 }
Kenny Root25c925e2012-09-08 22:02:21 -07003500
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003501 /*
Kenny Root25c925e2012-09-08 22:02:21 -07003502 * Test that an app signed with cert1 and cert2 cannot be replaced when
3503 * signed with cert2 alone.
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003504 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07003505 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003506 public void testCheckSignaturesSharedSomeMatch2() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003507 int apk1 = SHARED1_CERT1_CERT2;
3508 int apk2 = SHARED2_CERT2;
3509 boolean fail = true;
3510 int retCode = PackageManager.INSTALL_FAILED_SHARED_USER_INCOMPATIBLE;
3511 int expMatchResult = -1;
3512 checkSharedSignatures(apk1, apk2, true, fail, retCode, expMatchResult);
3513 }
Kenny Root25c925e2012-09-08 22:02:21 -07003514
Brett Chabotf76c56b2010-07-26 17:28:17 -07003515 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003516 public void testCheckSignaturesSharedUnknown() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003517 int apk1 = SHARED1_CERT1_CERT2;
3518 int apk2 = SHARED2_CERT1_CERT2;
3519 String apk1Name = "install1.apk";
3520 String apk2Name = "install2.apk";
3521 InstallParams ip1 = null;
3522
3523 try {
3524 ip1 = installFromRawResource(apk1Name, apk1, 0, false,
3525 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3526 PackageManager pm = mContext.getPackageManager();
3527 // Delete app2
3528 PackageParser.Package pkg = getParsedPackage(apk2Name, apk2);
Kenny Roota3e90792012-10-18 10:58:36 -07003529 getPm().deletePackage(pkg.packageName, null, PackageManager.DELETE_ALL_USERS);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003530 // Check signatures now
3531 int match = mContext.getPackageManager().checkSignatures(
3532 ip1.pkg.packageName, pkg.packageName);
3533 assertEquals(PackageManager.SIGNATURE_UNKNOWN_PACKAGE, match);
3534 } finally {
3535 if (ip1 != null) {
3536 cleanUpInstall(ip1);
3537 }
3538 }
3539 }
Neal Nguyenedb979a2010-04-22 13:54:32 -07003540
Brett Chabotf76c56b2010-07-26 17:28:17 -07003541 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003542 public void testReplaceFirstSharedMatchAllCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003543 int apk1 = SHARED1_CERT1;
3544 int apk2 = SHARED2_CERT1;
3545 int rapk1 = SHARED1_CERT1;
3546 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_MATCH);
3547 replaceCerts(apk1, rapk1, true, false, -1);
3548 }
Kenny Root25c925e2012-09-08 22:02:21 -07003549
Brett Chabotf76c56b2010-07-26 17:28:17 -07003550 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003551 public void testReplaceSecondSharedMatchAllCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003552 int apk1 = SHARED1_CERT1;
3553 int apk2 = SHARED2_CERT1;
3554 int rapk2 = SHARED2_CERT1;
3555 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_MATCH);
3556 replaceCerts(apk2, rapk2, true, false, -1);
3557 }
Kenny Root25c925e2012-09-08 22:02:21 -07003558
Brett Chabotf76c56b2010-07-26 17:28:17 -07003559 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003560 public void testReplaceFirstSharedMatchSomeCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003561 int apk1 = SHARED1_CERT1_CERT2;
3562 int apk2 = SHARED2_CERT1_CERT2;
3563 int rapk1 = SHARED1_CERT1;
3564 boolean fail = true;
3565 int retCode = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES;
3566 checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH);
3567 installFromRawResource("install.apk", rapk1, PackageManager.INSTALL_REPLACE_EXISTING, true,
3568 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3569 }
Kenny Root25c925e2012-09-08 22:02:21 -07003570
Brett Chabotf76c56b2010-07-26 17:28:17 -07003571 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003572 public void testReplaceSecondSharedMatchSomeCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003573 int apk1 = SHARED1_CERT1_CERT2;
3574 int apk2 = SHARED2_CERT1_CERT2;
3575 int rapk2 = SHARED2_CERT1;
3576 boolean fail = true;
3577 int retCode = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES;
3578 checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH);
3579 installFromRawResource("install.apk", rapk2, PackageManager.INSTALL_REPLACE_EXISTING, true,
3580 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3581 }
Kenny Root25c925e2012-09-08 22:02:21 -07003582
Brett Chabotf76c56b2010-07-26 17:28:17 -07003583 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003584 public void testReplaceFirstSharedMatchNoCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003585 int apk1 = SHARED1_CERT1;
3586 int apk2 = SHARED2_CERT1;
3587 int rapk1 = SHARED1_CERT2;
3588 boolean fail = true;
3589 int retCode = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES;
3590 checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH);
3591 installFromRawResource("install.apk", rapk1, PackageManager.INSTALL_REPLACE_EXISTING, true,
3592 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3593 }
Kenny Root25c925e2012-09-08 22:02:21 -07003594
Brett Chabotf76c56b2010-07-26 17:28:17 -07003595 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003596 public void testReplaceSecondSharedMatchNoCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003597 int apk1 = SHARED1_CERT1;
3598 int apk2 = SHARED2_CERT1;
3599 int rapk2 = SHARED2_CERT2;
3600 boolean fail = true;
3601 int retCode = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES;
3602 checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH);
3603 installFromRawResource("install.apk", rapk2, PackageManager.INSTALL_REPLACE_EXISTING, true,
3604 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3605 }
Kenny Root25c925e2012-09-08 22:02:21 -07003606
Brett Chabotf76c56b2010-07-26 17:28:17 -07003607 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003608 public void testReplaceFirstSharedMatchMoreCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003609 int apk1 = SHARED1_CERT1;
3610 int apk2 = SHARED2_CERT1;
3611 int rapk1 = SHARED1_CERT1_CERT2;
3612 boolean fail = true;
3613 int retCode = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES;
3614 checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH);
3615 installFromRawResource("install.apk", rapk1, PackageManager.INSTALL_REPLACE_EXISTING, true,
3616 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3617 }
Kenny Root25c925e2012-09-08 22:02:21 -07003618
Brett Chabotf76c56b2010-07-26 17:28:17 -07003619 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003620 public void testReplaceSecondSharedMatchMoreCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003621 int apk1 = SHARED1_CERT1;
3622 int apk2 = SHARED2_CERT1;
3623 int rapk2 = SHARED2_CERT1_CERT2;
3624 boolean fail = true;
3625 int retCode = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES;
3626 checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH);
3627 installFromRawResource("install.apk", rapk2, PackageManager.INSTALL_REPLACE_EXISTING, true,
3628 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3629 }
Kenny Root1683afa2011-01-07 14:27:50 -08003630
Kenny Root4c3915a2011-01-31 16:36:32 -08003631 /**
3632 * Unknown features should be allowed to install. This prevents older phones
3633 * from rejecting new packages that specify features that didn't exist when
3634 * an older phone existed. All older phones are assumed to have those
3635 * features.
3636 * <p>
3637 * Right now we allow all packages to be installed regardless of their
3638 * features.
3639 */
Kenny Root1683afa2011-01-07 14:27:50 -08003640 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003641 public void testUsesFeatureUnknownFeature() throws Exception {
Kenny Root4c3915a2011-01-31 16:36:32 -08003642 int retCode = PackageManager.INSTALL_SUCCEEDED;
3643 installFromRawResource("install.apk", R.raw.install_uses_feature, 0, true, false, retCode,
Kenny Root1683afa2011-01-07 14:27:50 -08003644 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3645 }
Kenny Root3c676892011-01-12 10:58:40 -08003646
Kenny Root1ebd74a2011-08-03 15:09:44 -07003647 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003648 public void testInstallNonexistentFile() throws Exception {
Kenny Root1ebd74a2011-08-03 15:09:44 -07003649 int retCode = PackageManager.INSTALL_FAILED_INVALID_URI;
3650 File invalidFile = new File("/nonexistent-file.apk");
3651 invokeInstallPackageFail(Uri.fromFile(invalidFile), 0, retCode);
3652 }
3653
Kenny Root0aaa0d92011-09-12 16:42:55 -07003654 @SmallTest
Kenny Root25c925e2012-09-08 22:02:21 -07003655 public void testGetVerifierDeviceIdentity() throws Exception {
Kenny Root0aaa0d92011-09-12 16:42:55 -07003656 PackageManager pm = getPm();
3657 VerifierDeviceIdentity id = pm.getVerifierDeviceIdentity();
3658
3659 assertNotNull("Verifier device identity should not be null", id);
3660 }
3661
Kenny Root25c925e2012-09-08 22:02:21 -07003662 public void testGetInstalledPackages() throws Exception {
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003663 List<PackageInfo> packages = getPm().getInstalledPackages(0);
3664 assertNotNull("installed packages cannot be null", packages);
3665 assertTrue("installed packages cannot be empty", packages.size() > 0);
3666 }
3667
Kenny Root25c925e2012-09-08 22:02:21 -07003668 public void testGetUnInstalledPackages() throws Exception {
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003669 List<PackageInfo> packages = getPm().getInstalledPackages(
3670 PackageManager.GET_UNINSTALLED_PACKAGES);
3671 assertNotNull("installed packages cannot be null", packages);
3672 assertTrue("installed packages cannot be empty", packages.size() > 0);
3673 }
3674
3675 /**
Kenny Root25c925e2012-09-08 22:02:21 -07003676 * Test that getInstalledPackages returns all the data specified in flags.
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003677 */
Kenny Root25c925e2012-09-08 22:02:21 -07003678 public void testGetInstalledPackagesAll() throws Exception {
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003679 int flags = PackageManager.GET_ACTIVITIES | PackageManager.GET_GIDS
3680 | PackageManager.GET_CONFIGURATIONS | PackageManager.GET_INSTRUMENTATION
3681 | PackageManager.GET_PERMISSIONS | PackageManager.GET_PROVIDERS
3682 | PackageManager.GET_RECEIVERS | PackageManager.GET_SERVICES
3683 | PackageManager.GET_SIGNATURES | PackageManager.GET_UNINSTALLED_PACKAGES;
3684
3685 List<PackageInfo> packages = getPm().getInstalledPackages(flags);
3686 assertNotNull("installed packages cannot be null", packages);
3687 assertTrue("installed packages cannot be empty", packages.size() > 0);
3688
3689 PackageInfo packageInfo = null;
3690
3691 // Find the package with all components specified in the AndroidManifest
3692 // to ensure no null values
3693 for (PackageInfo pi : packages) {
3694 if ("com.android.frameworks.coretests.install_complete_package_info"
3695 .equals(pi.packageName)) {
3696 packageInfo = pi;
3697 break;
3698 }
3699 }
3700 assertNotNull("activities should not be null", packageInfo.activities);
3701 assertNotNull("configPreferences should not be null", packageInfo.configPreferences);
3702 assertNotNull("instrumentation should not be null", packageInfo.instrumentation);
3703 assertNotNull("permissions should not be null", packageInfo.permissions);
3704 assertNotNull("providers should not be null", packageInfo.providers);
3705 assertNotNull("receivers should not be null", packageInfo.receivers);
3706 assertNotNull("services should not be null", packageInfo.services);
3707 assertNotNull("signatures should not be null", packageInfo.signatures);
3708 }
3709
3710 /**
3711 * Test that getInstalledPackages returns all the data specified in
3712 * flags when the GET_UNINSTALLED_PACKAGES flag is set.
3713 */
Kenny Root25c925e2012-09-08 22:02:21 -07003714 public void testGetUnInstalledPackagesAll() throws Exception {
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003715 int flags = PackageManager.GET_UNINSTALLED_PACKAGES
3716 | PackageManager.GET_ACTIVITIES | PackageManager.GET_GIDS
3717 | PackageManager.GET_CONFIGURATIONS | PackageManager.GET_INSTRUMENTATION
3718 | PackageManager.GET_PERMISSIONS | PackageManager.GET_PROVIDERS
3719 | PackageManager.GET_RECEIVERS | PackageManager.GET_SERVICES
3720 | PackageManager.GET_SIGNATURES | PackageManager.GET_UNINSTALLED_PACKAGES;
3721
3722 List<PackageInfo> packages = getPm().getInstalledPackages(flags);
3723 assertNotNull("installed packages cannot be null", packages);
3724 assertTrue("installed packages cannot be empty", packages.size() > 0);
3725
3726 PackageInfo packageInfo = null;
3727
3728 // Find the package with all components specified in the AndroidManifest
3729 // to ensure no null values
3730 for (PackageInfo pi : packages) {
3731 if ("com.android.frameworks.coretests.install_complete_package_info"
3732 .equals(pi.packageName)) {
3733 packageInfo = pi;
3734 break;
3735 }
3736 }
3737 assertNotNull("activities should not be null", packageInfo.activities);
3738 assertNotNull("configPreferences should not be null", packageInfo.configPreferences);
3739 assertNotNull("instrumentation should not be null", packageInfo.instrumentation);
3740 assertNotNull("permissions should not be null", packageInfo.permissions);
3741 assertNotNull("providers should not be null", packageInfo.providers);
3742 assertNotNull("receivers should not be null", packageInfo.receivers);
3743 assertNotNull("services should not be null", packageInfo.services);
3744 assertNotNull("signatures should not be null", packageInfo.signatures);
3745 }
3746
Kenny Roote15bdc22012-09-16 15:45:38 -07003747 public void testInstall_BadDex_CleanUp() throws Exception {
3748 int retCode = PackageManager.INSTALL_FAILED_DEXOPT;
3749 installFromRawResource("install.apk", R.raw.install_bad_dex, 0, true, true, retCode,
3750 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3751 }
3752
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07003753 /*---------- Recommended install location tests ----*/
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08003754 /*
3755 * TODO's
3756 * check version numbers for upgrades
3757 * check permissions of installed packages
3758 * how to do tests on updated system apps?
3759 * verify updates to system apps cannot be installed on the sdcard.
3760 */
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -08003761}