blob: 04f8009a40d53cc1e5e1e75daa5a49df03bf8ee7 [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
Kenny Root7e921a12012-09-08 22:02:21 -070019import static libcore.io.OsConstants.*;
20
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
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -080024import android.content.BroadcastReceiver;
25import android.content.Context;
26import android.content.Intent;
27import android.content.IntentFilter;
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +020028import android.content.pm.PackageInfo;
Kenny Root1683afa2011-01-07 14:27:50 -080029import android.content.pm.PackageManager;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -080030import android.content.pm.PackageManager.NameNotFoundException;
31import android.content.res.Resources;
32import android.content.res.Resources.NotFoundException;
Brett Chabota3de7452010-03-25 13:49:26 -070033import android.net.Uri;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -080034import android.os.Environment;
Brett Chabota3de7452010-03-25 13:49:26 -070035import android.os.FileUtils;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080036import android.os.IBinder;
Kenny Root7e921a12012-09-08 22:02:21 -070037import android.os.Process;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -080038import android.os.RemoteException;
39import android.os.ServiceManager;
40import android.os.StatFs;
Kenny Root7e921a12012-09-08 22:02:21 -070041import android.os.SystemClock;
Kenny Roote15bdc22012-09-16 15:45:38 -070042import android.os.UserManager;
Brett Chabota3de7452010-03-25 13:49:26 -070043import android.os.storage.IMountService;
44import android.os.storage.StorageListener;
45import android.os.storage.StorageManager;
46import android.os.storage.StorageResultCode;
Suchi Amalapurapu117818e2010-02-09 03:45:40 -080047import android.provider.Settings;
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -080048import android.provider.Settings.SettingNotFoundException;
Brett Chabota3de7452010-03-25 13:49:26 -070049import android.test.AndroidTestCase;
Brett Chabotf76c56b2010-07-26 17:28:17 -070050import android.test.suitebuilder.annotation.LargeTest;
Kenny Root0aaa0d92011-09-12 16:42:55 -070051import android.test.suitebuilder.annotation.SmallTest;
Brett Chabota3de7452010-03-25 13:49:26 -070052import android.util.DisplayMetrics;
53import android.util.Log;
54
55import java.io.File;
Kenny Root6a6b0072010-10-07 16:46:10 -070056import java.io.IOException;
Brett Chabota3de7452010-03-25 13:49:26 -070057import java.io.InputStream;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -080058
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +020059import java.util.List;
Kenny Root7e921a12012-09-08 22:02:21 -070060import java.util.concurrent.CountDownLatch;
61import java.util.concurrent.TimeUnit;
62
63import libcore.io.ErrnoException;
64import libcore.io.Libcore;
65import libcore.io.StructStat;
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +020066
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -080067public class PackageManagerTests extends AndroidTestCase {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080068 private static final boolean localLOGV = true;
Kenny Root25c925e2012-09-08 22:02:21 -070069
70 public static final String TAG = "PackageManagerTests";
71
72 public final long MAX_WAIT_TIME = 25 * 1000;
73
74 public final long WAIT_TIME_INCR = 5 * 1000;
75
Kenny Rootddbe50d2012-09-06 13:18:37 -070076 private static final String APP_LIB_DIR_PREFIX = "/data/app-lib/";
77
Kenny Root25c925e2012-09-08 22:02:21 -070078 private static final String SECURE_CONTAINERS_PREFIX = "/mnt/asec/";
79
Suchi Amalapurapu089262d2010-03-10 14:19:21 -080080 private static final int APP_INSTALL_AUTO = PackageHelper.APP_INSTALL_AUTO;
Kenny Root25c925e2012-09-08 22:02:21 -070081
Suchi Amalapurapu089262d2010-03-10 14:19:21 -080082 private static final int APP_INSTALL_DEVICE = PackageHelper.APP_INSTALL_INTERNAL;
Kenny Root25c925e2012-09-08 22:02:21 -070083
Suchi Amalapurapu089262d2010-03-10 14:19:21 -080084 private static final int APP_INSTALL_SDCARD = PackageHelper.APP_INSTALL_EXTERNAL;
Kenny Root25c925e2012-09-08 22:02:21 -070085
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -070086 private boolean mOrigState;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080087
88 void failStr(String errMsg) {
Kenny Root25c925e2012-09-08 22:02:21 -070089 Log.w(TAG, "errMsg=" + errMsg);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080090 fail(errMsg);
91 }
Kenny Root25c925e2012-09-08 22:02:21 -070092
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080093 void failStr(Exception e) {
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -070094 failStr(e.getMessage());
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080095 }
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -080096
97 @Override
98 protected void setUp() throws Exception {
99 super.setUp();
Kenny Rootd7b421b2010-08-05 08:40:00 -0700100 mOrigState = checkMediaState(Environment.MEDIA_MOUNTED);
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700101 if (!mountMedia()) {
102 Log.i(TAG, "sdcard not mounted? Some of these tests might fail");
103 }
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700104 }
105
106 @Override
107 protected void tearDown() throws Exception {
108 // Restore media state.
Kenny Rootd7b421b2010-08-05 08:40:00 -0700109 boolean newState = checkMediaState(Environment.MEDIA_MOUNTED);
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700110 if (newState != mOrigState) {
111 if (mOrigState) {
Kenny Rootd7b421b2010-08-05 08:40:00 -0700112 mountMedia();
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700113 } else {
Kenny Rootd7b421b2010-08-05 08:40:00 -0700114 unmountMedia();
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700115 }
116 }
117 super.tearDown();
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800118 }
119
120 private class PackageInstallObserver extends IPackageInstallObserver.Stub {
121 public int returnCode;
Kenny Root25c925e2012-09-08 22:02:21 -0700122
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800123 private boolean doneFlag = false;
124
125 public void packageInstalled(String packageName, int returnCode) {
Kenny Root25c925e2012-09-08 22:02:21 -0700126 synchronized (this) {
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800127 this.returnCode = returnCode;
128 doneFlag = true;
129 notifyAll();
130 }
131 }
132
133 public boolean isDone() {
134 return doneFlag;
135 }
136 }
137
138 abstract class GenericReceiver extends BroadcastReceiver {
139 private boolean doneFlag = false;
Kenny Root25c925e2012-09-08 22:02:21 -0700140
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800141 boolean received = false;
Kenny Root25c925e2012-09-08 22:02:21 -0700142
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800143 Intent intent;
Kenny Root25c925e2012-09-08 22:02:21 -0700144
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800145 IntentFilter filter;
Kenny Root25c925e2012-09-08 22:02:21 -0700146
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800147 abstract boolean notifyNow(Intent intent);
Kenny Root25c925e2012-09-08 22:02:21 -0700148
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800149 @Override
150 public void onReceive(Context context, Intent intent) {
151 if (notifyNow(intent)) {
152 synchronized (this) {
153 received = true;
154 doneFlag = true;
155 this.intent = intent;
156 notifyAll();
157 }
158 }
159 }
160
161 public boolean isDone() {
162 return doneFlag;
163 }
164
165 public void setFilter(IntentFilter filter) {
166 this.filter = filter;
167 }
168 }
169
170 class InstallReceiver extends GenericReceiver {
171 String pkgName;
172
173 InstallReceiver(String pkgName) {
174 this.pkgName = pkgName;
175 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
176 filter.addDataScheme("package");
177 super.setFilter(filter);
178 }
179
180 public boolean notifyNow(Intent intent) {
181 String action = intent.getAction();
182 if (!Intent.ACTION_PACKAGE_ADDED.equals(action)) {
183 return false;
184 }
185 Uri data = intent.getData();
186 String installedPkg = data.getEncodedSchemeSpecificPart();
187 if (pkgName.equals(installedPkg)) {
188 return true;
189 }
190 return false;
191 }
192 }
193
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700194 private PackageManager getPm() {
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800195 return mContext.getPackageManager();
196 }
197
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700198 private IPackageManager getIPm() {
199 IPackageManager ipm = IPackageManager.Stub.asInterface(
200 ServiceManager.getService("package"));
201 return ipm;
202 }
203
Kenny Rootb7c24702012-04-14 19:46:09 -0700204 public void invokeInstallPackage(Uri packageURI, int flags, GenericReceiver receiver,
205 boolean shouldSucceed) {
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800206 PackageInstallObserver observer = new PackageInstallObserver();
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800207 mContext.registerReceiver(receiver, receiver.filter);
208 try {
209 // Wait on observer
Kenny Root25c925e2012-09-08 22:02:21 -0700210 synchronized (observer) {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800211 synchronized (receiver) {
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800212 getPm().installPackage(packageURI, observer, flags, null);
213 long waitTime = 0;
Kenny Root25c925e2012-09-08 22:02:21 -0700214 while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) {
Kenny Root94e0acb2010-09-21 16:30:56 -0700215 try {
216 observer.wait(WAIT_TIME_INCR);
217 waitTime += WAIT_TIME_INCR;
218 } catch (InterruptedException e) {
219 Log.i(TAG, "Interrupted during sleep", e);
220 }
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800221 }
Kenny Root25c925e2012-09-08 22:02:21 -0700222 if (!observer.isDone()) {
Kenny Root94e0acb2010-09-21 16:30:56 -0700223 fail("Timed out waiting for packageInstalled callback");
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800224 }
Kenny Rootb7c24702012-04-14 19:46:09 -0700225
226 if (shouldSucceed) {
227 if (observer.returnCode != PackageManager.INSTALL_SUCCEEDED) {
228 fail("Package installation should have succeeded, but got code "
229 + observer.returnCode);
230 }
231 } else {
232 if (observer.returnCode == PackageManager.INSTALL_SUCCEEDED) {
233 fail("Package installation should fail");
234 }
235
236 /*
237 * We'll never expect get a notification since we
238 * shouldn't succeed.
239 */
240 return;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800241 }
Kenny Rootb7c24702012-04-14 19:46:09 -0700242
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800243 // Verify we received the broadcast
244 waitTime = 0;
Kenny Root25c925e2012-09-08 22:02:21 -0700245 while ((!receiver.isDone()) && (waitTime < MAX_WAIT_TIME)) {
Kenny Root94e0acb2010-09-21 16:30:56 -0700246 try {
247 receiver.wait(WAIT_TIME_INCR);
248 waitTime += WAIT_TIME_INCR;
249 } catch (InterruptedException e) {
250 Log.i(TAG, "Interrupted during sleep", e);
251 }
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800252 }
Kenny Root25c925e2012-09-08 22:02:21 -0700253 if (!receiver.isDone()) {
Kenny Root94e0acb2010-09-21 16:30:56 -0700254 fail("Timed out waiting for PACKAGE_ADDED notification");
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800255 }
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800256 }
257 }
258 } finally {
259 mContext.unregisterReceiver(receiver);
260 }
261 }
262
Kenny Root1683afa2011-01-07 14:27:50 -0800263 public void invokeInstallPackageFail(Uri packageURI, int flags, int expectedResult) {
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800264 PackageInstallObserver observer = new PackageInstallObserver();
265 try {
266 // Wait on observer
Kenny Root25c925e2012-09-08 22:02:21 -0700267 synchronized (observer) {
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800268 getPm().installPackage(packageURI, observer, flags, null);
269 long waitTime = 0;
Kenny Root25c925e2012-09-08 22:02:21 -0700270 while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) {
Kenny Root94e0acb2010-09-21 16:30:56 -0700271 try {
272 observer.wait(WAIT_TIME_INCR);
273 waitTime += WAIT_TIME_INCR;
274 } catch (InterruptedException e) {
275 Log.i(TAG, "Interrupted during sleep", e);
276 }
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800277 }
Kenny Root25c925e2012-09-08 22:02:21 -0700278 if (!observer.isDone()) {
Kenny Root94e0acb2010-09-21 16:30:56 -0700279 fail("Timed out waiting for packageInstalled callback");
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800280 }
Kenny Root1683afa2011-01-07 14:27:50 -0800281 assertEquals(expectedResult, observer.returnCode);
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800282 }
283 } finally {
284 }
285 }
286
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800287 Uri getInstallablePackage(int fileResId, File outFile) {
288 Resources res = mContext.getResources();
289 InputStream is = null;
290 try {
291 is = res.openRawResource(fileResId);
292 } catch (NotFoundException e) {
293 failStr("Failed to load resource with id: " + fileResId);
294 }
295 FileUtils.setPermissions(outFile.getPath(),
296 FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IRWXO,
297 -1, -1);
298 assertTrue(FileUtils.copyToFile(is, outFile));
299 FileUtils.setPermissions(outFile.getPath(),
300 FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IRWXO,
301 -1, -1);
302 return Uri.fromFile(outFile);
303 }
304
305 private PackageParser.Package parsePackage(Uri packageURI) {
306 final String archiveFilePath = packageURI.getPath();
307 PackageParser packageParser = new PackageParser(archiveFilePath);
308 File sourceFile = new File(archiveFilePath);
309 DisplayMetrics metrics = new DisplayMetrics();
310 metrics.setToDefaults();
Kenny Root25c925e2012-09-08 22:02:21 -0700311 PackageParser.Package pkg = packageParser.parsePackage(sourceFile, archiveFilePath,
312 metrics, 0);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -0800313 packageParser = null;
314 return pkg;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800315 }
Kenny Root25c925e2012-09-08 22:02:21 -0700316
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700317 private boolean checkSd(long pkgLen) {
318 String status = Environment.getExternalStorageState();
319 if (!status.equals(Environment.MEDIA_MOUNTED)) {
320 return false;
321 }
322 long sdSize = -1;
Kenny Root25c925e2012-09-08 22:02:21 -0700323 StatFs sdStats = new StatFs(Environment.getExternalStorageDirectory().getPath());
324 sdSize = (long) sdStats.getAvailableBlocks() * (long) sdStats.getBlockSize();
Kenny Root94e0acb2010-09-21 16:30:56 -0700325 // TODO check for thresholds here
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700326 return pkgLen <= sdSize;
Neal Nguyenedb979a2010-04-22 13:54:32 -0700327
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700328 }
Kenny Root25c925e2012-09-08 22:02:21 -0700329
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700330 private boolean checkInt(long pkgLen) {
331 StatFs intStats = new StatFs(Environment.getDataDirectory().getPath());
Kenny Root25c925e2012-09-08 22:02:21 -0700332 long intSize = (long) intStats.getBlockCount() * (long) intStats.getBlockSize();
333 long iSize = (long) intStats.getAvailableBlocks() * (long) intStats.getBlockSize();
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700334 // TODO check for thresholds here?
335 return pkgLen <= iSize;
336 }
Kenny Root25c925e2012-09-08 22:02:21 -0700337
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700338 private static final int INSTALL_LOC_INT = 1;
Kenny Root25c925e2012-09-08 22:02:21 -0700339
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700340 private static final int INSTALL_LOC_SD = 2;
Kenny Root25c925e2012-09-08 22:02:21 -0700341
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700342 private static final int INSTALL_LOC_ERR = -1;
Kenny Root25c925e2012-09-08 22:02:21 -0700343
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700344 private int getInstallLoc(int flags, int expInstallLocation, long pkgLen) {
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800345 // Flags explicitly over ride everything else.
Kenny Root25c925e2012-09-08 22:02:21 -0700346 if ((flags & PackageManager.INSTALL_EXTERNAL) != 0) {
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700347 return INSTALL_LOC_SD;
348 } else if ((flags & PackageManager.INSTALL_INTERNAL) != 0) {
349 return INSTALL_LOC_INT;
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800350 }
351 // Manifest option takes precedence next
352 if (expInstallLocation == PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL) {
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700353 if (checkSd(pkgLen)) {
Kenny Root25c925e2012-09-08 22:02:21 -0700354 return INSTALL_LOC_SD;
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700355 }
356 if (checkInt(pkgLen)) {
357 return INSTALL_LOC_INT;
358 }
359 return INSTALL_LOC_ERR;
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800360 }
Suchi Amalapurapu9b10ef52010-03-03 09:45:24 -0800361 if (expInstallLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY) {
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700362 if (checkInt(pkgLen)) {
363 return INSTALL_LOC_INT;
364 }
365 return INSTALL_LOC_ERR;
Suchi Amalapurapu9b10ef52010-03-03 09:45:24 -0800366 }
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700367 if (expInstallLocation == PackageInfo.INSTALL_LOCATION_AUTO) {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700368 // Check for free memory internally
369 if (checkInt(pkgLen)) {
370 return INSTALL_LOC_INT;
371 }
372 // Check for free memory externally
373 if (checkSd(pkgLen)) {
374 return INSTALL_LOC_SD;
375 }
376 return INSTALL_LOC_ERR;
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700377 }
378 // Check for settings preference.
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800379 boolean checkSd = false;
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700380 int userPref = getDefaultInstallLoc();
381 if (userPref == APP_INSTALL_DEVICE) {
382 if (checkInt(pkgLen)) {
383 return INSTALL_LOC_INT;
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800384 }
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700385 return INSTALL_LOC_ERR;
386 } else if (userPref == APP_INSTALL_SDCARD) {
387 if (checkSd(pkgLen)) {
388 return INSTALL_LOC_SD;
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800389 }
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700390 return INSTALL_LOC_ERR;
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -0700391 }
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700392 // Default system policy for apps with no manifest option specified.
393 // Check for free memory internally
394 if (checkInt(pkgLen)) {
395 return INSTALL_LOC_INT;
396 }
397 return INSTALL_LOC_ERR;
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800398 }
Neal Nguyenedb979a2010-04-22 13:54:32 -0700399
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800400 private void assertInstall(PackageParser.Package pkg, int flags, int expInstallLocation) {
401 try {
402 String pkgName = pkg.packageName;
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700403 ApplicationInfo info = getPm().getApplicationInfo(pkgName, 0);
404 assertNotNull(info);
405 assertEquals(pkgName, info.packageName);
406 File dataDir = Environment.getDataDirectory();
407 String appInstallPath = new File(dataDir, "app").getPath();
408 String drmInstallPath = new File(dataDir, "app-private").getPath();
409 File srcDir = new File(info.sourceDir);
410 String srcPath = srcDir.getParent();
411 File publicSrcDir = new File(info.publicSourceDir);
412 String publicSrcPath = publicSrcDir.getParent();
413 long pkgLen = new File(info.sourceDir).length();
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800414
Kenny Root6dceb882012-04-12 14:23:49 -0700415 int rLoc = getInstallLoc(flags, expInstallLocation, pkgLen);
416 if (rLoc == INSTALL_LOC_INT) {
417 if ((flags & PackageManager.INSTALL_FORWARD_LOCK) != 0) {
418 assertTrue("The application should be installed forward locked",
419 (info.flags & ApplicationInfo.FLAG_FORWARD_LOCK) != 0);
Kenny Root7e921a12012-09-08 22:02:21 -0700420 assertStartsWith("The APK path should point to the ASEC",
421 SECURE_CONTAINERS_PREFIX, srcPath);
422 assertStartsWith("The public APK path should point to the ASEC",
423 SECURE_CONTAINERS_PREFIX, publicSrcPath);
424 assertStartsWith("The native library path should point to the ASEC",
425 SECURE_CONTAINERS_PREFIX, info.nativeLibraryDir);
Nick Kralevich7de350a2012-09-07 15:48:11 -0700426 try {
427 String compatLib = new File(info.dataDir + "/lib").getCanonicalPath();
428 assertEquals("The compatibility lib directory should be a symbolic link to "
429 + info.nativeLibraryDir,
430 info.nativeLibraryDir, compatLib);
431 } catch (IOException e) {
432 fail("compat check: Can't read " + info.dataDir + "/lib");
433 }
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700434 } else {
Kenny Root6dceb882012-04-12 14:23:49 -0700435 assertFalse((info.flags & ApplicationInfo.FLAG_FORWARD_LOCK) != 0);
436 assertEquals(srcPath, appInstallPath);
437 assertEquals(publicSrcPath, appInstallPath);
Kenny Root7e921a12012-09-08 22:02:21 -0700438 assertStartsWith("Native library should point to shared lib directory",
Kenny Rootddbe50d2012-09-06 13:18:37 -0700439 new File(APP_LIB_DIR_PREFIX, info.packageName).getPath(),
Kenny Root7e921a12012-09-08 22:02:21 -0700440 info.nativeLibraryDir);
441 assertDirOwnerGroupPerms(
442 "Native library directory should be owned by system:system and 0755",
443 Process.SYSTEM_UID, Process.SYSTEM_UID,
444 S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH,
445 info.nativeLibraryDir);
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700446 }
Kenny Root6dceb882012-04-12 14:23:49 -0700447 assertFalse((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0);
448
449 // Make sure the native library dir is not a symlink
450 final File nativeLibDir = new File(info.nativeLibraryDir);
451 assertTrue("Native library dir should exist at " + info.nativeLibraryDir,
452 nativeLibDir.exists());
453 try {
454 assertEquals("Native library dir should not be a symlink",
Kenny Root7e921a12012-09-08 22:02:21 -0700455 info.nativeLibraryDir, nativeLibDir.getCanonicalPath());
Kenny Root6dceb882012-04-12 14:23:49 -0700456 } catch (IOException e) {
457 fail("Can't read " + nativeLibDir.getPath());
458 }
Kenny Root25c925e2012-09-08 22:02:21 -0700459 } else if (rLoc == INSTALL_LOC_SD) {
Kenny Root6dceb882012-04-12 14:23:49 -0700460 if ((flags & PackageManager.INSTALL_FORWARD_LOCK) != 0) {
461 assertTrue("The application should be installed forward locked",
462 (info.flags & ApplicationInfo.FLAG_FORWARD_LOCK) != 0);
463 } else {
464 assertFalse("The application should not be installed forward locked",
465 (info.flags & ApplicationInfo.FLAG_FORWARD_LOCK) != 0);
466 }
467 assertTrue("Application flags (" + info.flags
468 + ") should contain FLAG_EXTERNAL_STORAGE",
469 (info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0);
470 // Might need to check:
471 // ((info.flags & ApplicationInfo.FLAG_FORWARD_LOCK) != 0)
Kenny Root7e921a12012-09-08 22:02:21 -0700472 assertStartsWith("The APK path should point to the ASEC",
473 SECURE_CONTAINERS_PREFIX, srcPath);
474 assertStartsWith("The public APK path should point to the ASEC",
475 SECURE_CONTAINERS_PREFIX, publicSrcPath);
476 assertStartsWith("The native library path should point to the ASEC",
477 SECURE_CONTAINERS_PREFIX, info.nativeLibraryDir);
Kenny Root6dceb882012-04-12 14:23:49 -0700478
479 // Make sure the native library in /data/data/<app>/lib is a
480 // symlink to the ASEC
481 final File nativeLibSymLink = new File(info.dataDir, "lib");
482 assertTrue("Native library symlink should exist at " + nativeLibSymLink.getPath(),
483 nativeLibSymLink.exists());
484 try {
485 assertEquals(nativeLibSymLink.getPath() + " should be a symlink to "
Kenny Root25c925e2012-09-08 22:02:21 -0700486 + info.nativeLibraryDir, info.nativeLibraryDir,
487 nativeLibSymLink.getCanonicalPath());
Kenny Root6dceb882012-04-12 14:23:49 -0700488 } catch (IOException e) {
489 fail("Can't read " + nativeLibSymLink.getPath());
490 }
491 } else {
492 // TODO handle error. Install should have failed.
493 fail("Install should have failed");
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800494 }
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800495 } catch (NameNotFoundException e) {
496 failStr("failed with exception : " + e);
497 }
498 }
Neal Nguyenedb979a2010-04-22 13:54:32 -0700499
Kenny Root7e921a12012-09-08 22:02:21 -0700500 private void assertDirOwnerGroupPerms(String reason, int uid, int gid, int perms, String path) {
501 final StructStat stat;
502
503 try {
504 stat = Libcore.os.lstat(path);
505 } catch (ErrnoException e) {
506 throw new AssertionError(reason + "\n" + "Got: " + path + " does not exist");
507 }
508
509 StringBuilder sb = new StringBuilder();
510
511 if (!S_ISDIR(stat.st_mode)) {
512 sb.append("\nExpected type: ");
513 sb.append(S_IFDIR);
514 sb.append("\ngot type: ");
515 sb.append((stat.st_mode & S_IFMT));
516 }
517
518 if (stat.st_uid != uid) {
519 sb.append("\nExpected owner: ");
520 sb.append(uid);
521 sb.append("\nGot owner: ");
522 sb.append(stat.st_uid);
523 }
524
525 if (stat.st_gid != gid) {
526 sb.append("\nExpected group: ");
527 sb.append(gid);
528 sb.append("\nGot group: ");
529 sb.append(stat.st_gid);
530 }
531
532 if ((stat.st_mode & ~S_IFMT) != perms) {
533 sb.append("\nExpected permissions: ");
534 sb.append(Integer.toOctalString(perms));
535 sb.append("\nGot permissions: ");
536 sb.append(Integer.toOctalString(stat.st_mode & ~S_IFMT));
537 }
538
539 if (sb.length() > 0) {
540 throw new AssertionError(reason + sb.toString());
541 }
542 }
543
544 private static void assertStartsWith(String prefix, String actual) {
545 assertStartsWith("", prefix, actual);
546 }
547
548 private static void assertStartsWith(String description, String prefix, String actual) {
549 if (!actual.startsWith(prefix)) {
550 StringBuilder sb = new StringBuilder(description);
551 sb.append("\nExpected prefix: ");
552 sb.append(prefix);
553 sb.append("\n got: ");
554 sb.append(actual);
555 sb.append('\n');
556 throw new AssertionError(sb.toString());
557 }
558 }
559
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800560 private void assertNotInstalled(String pkgName) {
561 try {
562 ApplicationInfo info = getPm().getApplicationInfo(pkgName, 0);
563 fail(pkgName + " shouldnt be installed");
564 } catch (NameNotFoundException e) {
565 }
Kenny Roote15bdc22012-09-16 15:45:38 -0700566
567 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
568 List<UserInfo> users = um.getUsers();
569 for (UserInfo user : users) {
570 String dataDir = PackageManager.getDataDirForUser(user.id, pkgName);
571 assertFalse("Application data directory should not exist: " + dataDir,
572 new File(dataDir).exists());
573 }
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800574 }
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800575
576 class InstallParams {
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800577 Uri packageURI;
Kenny Root25c925e2012-09-08 22:02:21 -0700578
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800579 PackageParser.Package pkg;
Kenny Root25c925e2012-09-08 22:02:21 -0700580
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700581 InstallParams(String outFileName, int rawResId) {
582 this.pkg = getParsedPackage(outFileName, rawResId);
583 this.packageURI = Uri.fromFile(new File(pkg.mScanPath));
584 }
Kenny Root25c925e2012-09-08 22:02:21 -0700585
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700586 InstallParams(PackageParser.Package pkg) {
587 this.packageURI = Uri.fromFile(new File(pkg.mScanPath));
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800588 this.pkg = pkg;
589 }
Kenny Root25c925e2012-09-08 22:02:21 -0700590
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700591 long getApkSize() {
592 File file = new File(pkg.mScanPath);
593 return file.length();
594 }
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800595 }
596
Kenny Root25c925e2012-09-08 22:02:21 -0700597 private InstallParams sampleInstallFromRawResource(int flags, boolean cleanUp) throws Exception {
598 return installFromRawResource("install.apk", R.raw.install, flags, cleanUp, false, -1,
599 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800600 }
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -0800601
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700602 static final String PERM_PACKAGE = "package";
Kenny Root25c925e2012-09-08 22:02:21 -0700603
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700604 static final String PERM_DEFINED = "defined";
Kenny Root25c925e2012-09-08 22:02:21 -0700605
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700606 static final String PERM_UNDEFINED = "undefined";
Kenny Root25c925e2012-09-08 22:02:21 -0700607
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700608 static final String PERM_USED = "used";
Kenny Root25c925e2012-09-08 22:02:21 -0700609
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700610 static final String PERM_NOTUSED = "notused";
Neal Nguyenedb979a2010-04-22 13:54:32 -0700611
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700612 private void assertPermissions(String[] cmds) {
613 final PackageManager pm = getPm();
614 String pkg = null;
615 PackageInfo pkgInfo = null;
616 String mode = PERM_DEFINED;
617 int i = 0;
618 while (i < cmds.length) {
619 String cmd = cmds[i++];
620 if (cmd == PERM_PACKAGE) {
621 pkg = cmds[i++];
622 try {
623 pkgInfo = pm.getPackageInfo(pkg,
624 PackageManager.GET_PERMISSIONS
625 | PackageManager.GET_UNINSTALLED_PACKAGES);
626 } catch (NameNotFoundException e) {
627 pkgInfo = null;
628 }
629 } else if (cmd == PERM_DEFINED || cmd == PERM_UNDEFINED
630 || cmd == PERM_USED || cmd == PERM_NOTUSED) {
631 mode = cmds[i++];
632 } else {
633 if (mode == PERM_DEFINED) {
634 try {
635 PermissionInfo pi = pm.getPermissionInfo(cmd, 0);
636 assertNotNull(pi);
637 assertEquals(pi.packageName, pkg);
638 assertEquals(pi.name, cmd);
639 assertNotNull(pkgInfo);
640 boolean found = false;
Kenny Root25c925e2012-09-08 22:02:21 -0700641 for (int j = 0; j < pkgInfo.permissions.length && !found; j++) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700642 if (pkgInfo.permissions[j].name.equals(cmd)) {
643 found = true;
644 }
645 }
646 if (!found) {
647 fail("Permission not found: " + cmd);
648 }
649 } catch (NameNotFoundException e) {
650 throw new RuntimeException(e);
651 }
652 } else if (mode == PERM_UNDEFINED) {
653 try {
654 pm.getPermissionInfo(cmd, 0);
655 throw new RuntimeException("Permission exists: " + cmd);
656 } catch (NameNotFoundException e) {
657 }
658 if (pkgInfo != null) {
659 boolean found = false;
Kenny Root25c925e2012-09-08 22:02:21 -0700660 for (int j = 0; j < pkgInfo.permissions.length && !found; j++) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700661 if (pkgInfo.permissions[j].name.equals(cmd)) {
662 found = true;
663 }
664 }
665 if (found) {
666 fail("Permission still exists: " + cmd);
667 }
668 }
669 } else if (mode == PERM_USED || mode == PERM_NOTUSED) {
670 boolean found = false;
Kenny Root25c925e2012-09-08 22:02:21 -0700671 for (int j = 0; j < pkgInfo.requestedPermissions.length && !found; j++) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700672 if (pkgInfo.requestedPermissions[j].equals(cmd)) {
673 found = true;
674 }
675 }
676 if (!found) {
677 fail("Permission not requested: " + cmd);
678 }
679 if (mode == PERM_USED) {
Kenny Root25c925e2012-09-08 22:02:21 -0700680 if (pm.checkPermission(cmd, pkg) != PackageManager.PERMISSION_GRANTED) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700681 fail("Permission not granted: " + cmd);
682 }
683 } else {
Kenny Root25c925e2012-09-08 22:02:21 -0700684 if (pm.checkPermission(cmd, pkg) != PackageManager.PERMISSION_DENIED) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700685 fail("Permission granted: " + cmd);
686 }
687 }
688 }
689 }
690 }
691 }
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -0800692
Suchi Amalapurapuae181712010-03-30 14:01:02 -0700693 private PackageParser.Package getParsedPackage(String outFileName, int rawResId) {
694 PackageManager pm = mContext.getPackageManager();
695 File filesDir = mContext.getFilesDir();
696 File outFile = new File(filesDir, outFileName);
697 Uri packageURI = getInstallablePackage(rawResId, outFile);
698 PackageParser.Package pkg = parsePackage(packageURI);
699 return pkg;
700 }
701
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800702 /*
703 * Utility function that reads a apk bundled as a raw resource
704 * copies it into own data directory and invokes
705 * PackageManager api to install it.
706 */
Kenny Root25c925e2012-09-08 22:02:21 -0700707 private void installFromRawResource(InstallParams ip, int flags, boolean cleanUp, boolean fail,
708 int result, int expInstallLocation) throws Exception {
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800709 PackageManager pm = mContext.getPackageManager();
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700710 PackageParser.Package pkg = ip.pkg;
711 Uri packageURI = ip.packageURI;
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800712 if ((flags & PackageManager.INSTALL_REPLACE_EXISTING) == 0) {
713 // Make sure the package doesn't exist
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800714 try {
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800715 ApplicationInfo appInfo = pm.getApplicationInfo(pkg.packageName,
716 PackageManager.GET_UNINSTALLED_PACKAGES);
717 GenericReceiver receiver = new DeleteReceiver(pkg.packageName);
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700718 invokeDeletePackage(pkg.packageName, 0, receiver);
Kenny Roote15bdc22012-09-16 15:45:38 -0700719 } catch (NameNotFoundException e) {
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800720 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800721 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800722 try {
723 if (fail) {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700724 invokeInstallPackageFail(packageURI, flags, result);
Suchi Amalapurapuae181712010-03-30 14:01:02 -0700725 if ((flags & PackageManager.INSTALL_REPLACE_EXISTING) == 0) {
726 assertNotInstalled(pkg.packageName);
727 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800728 } else {
729 InstallReceiver receiver = new InstallReceiver(pkg.packageName);
Kenny Rootb7c24702012-04-14 19:46:09 -0700730 invokeInstallPackage(packageURI, flags, receiver, true);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800731 // Verify installed information
732 assertInstall(pkg, flags, expInstallLocation);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800733 }
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800734 } finally {
735 if (cleanUp) {
Suchi Amalapurapu9b10ef52010-03-03 09:45:24 -0800736 cleanUpInstall(ip);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800737 }
738 }
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700739 }
740
741 /*
742 * Utility function that reads a apk bundled as a raw resource
743 * copies it into own data directory and invokes
744 * PackageManager api to install it.
745 */
Kenny Root25c925e2012-09-08 22:02:21 -0700746 private InstallParams installFromRawResource(String outFileName, int rawResId, int flags,
747 boolean cleanUp, boolean fail, int result, int expInstallLocation) throws Exception {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700748 InstallParams ip = new InstallParams(outFileName, rawResId);
749 installFromRawResource(ip, flags, cleanUp, fail, result, expInstallLocation);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800750 return ip;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800751 }
752
Brett Chabotf76c56b2010-07-26 17:28:17 -0700753 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -0700754 public void testInstallNormalInternal() throws Exception {
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800755 sampleInstallFromRawResource(0, true);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800756 }
757
Brett Chabotf76c56b2010-07-26 17:28:17 -0700758 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -0700759 public void testInstallFwdLockedInternal() throws Exception {
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800760 sampleInstallFromRawResource(PackageManager.INSTALL_FORWARD_LOCK, true);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800761 }
762
Brett Chabotf76c56b2010-07-26 17:28:17 -0700763 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -0700764 public void testInstallSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -0800765 // Do not run on devices with emulated external storage.
766 if (Environment.isExternalStorageEmulated()) {
767 return;
768 }
769
Kenny Rootd7b421b2010-08-05 08:40:00 -0700770 mountMedia();
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800771 sampleInstallFromRawResource(PackageManager.INSTALL_EXTERNAL, true);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800772 }
773
Kenny Root25c925e2012-09-08 22:02:21 -0700774 /* ------------------------- Test replacing packages -------------- */
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800775 class ReplaceReceiver extends GenericReceiver {
776 String pkgName;
Kenny Root25c925e2012-09-08 22:02:21 -0700777
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800778 final static int INVALID = -1;
Kenny Root25c925e2012-09-08 22:02:21 -0700779
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800780 final static int REMOVED = 1;
Kenny Root25c925e2012-09-08 22:02:21 -0700781
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800782 final static int ADDED = 2;
Kenny Root25c925e2012-09-08 22:02:21 -0700783
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800784 final static int REPLACED = 3;
Kenny Root25c925e2012-09-08 22:02:21 -0700785
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800786 int removed = INVALID;
Kenny Root25c925e2012-09-08 22:02:21 -0700787
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800788 // for updated system apps only
789 boolean update = false;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800790
791 ReplaceReceiver(String pkgName) {
792 this.pkgName = pkgName;
793 filter = new IntentFilter(Intent.ACTION_PACKAGE_REMOVED);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800794 filter.addAction(Intent.ACTION_PACKAGE_ADDED);
795 if (update) {
796 filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
797 }
798 filter.addDataScheme("package");
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800799 super.setFilter(filter);
800 }
801
802 public boolean notifyNow(Intent intent) {
803 String action = intent.getAction();
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800804 Uri data = intent.getData();
805 String installedPkg = data.getEncodedSchemeSpecificPart();
806 if (pkgName == null || !pkgName.equals(installedPkg)) {
807 return false;
808 }
809 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
810 removed = REMOVED;
811 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
812 if (removed != REMOVED) {
813 return false;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800814 }
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800815 boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
816 if (!replacing) {
817 return false;
818 }
819 removed = ADDED;
820 if (!update) {
821 return true;
822 }
823 } else if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
824 if (removed != ADDED) {
825 return false;
826 }
827 removed = REPLACED;
828 return true;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800829 }
830 return false;
831 }
832 }
833
834 /*
835 * Utility function that reads a apk bundled as a raw resource
836 * copies it into own data directory and invokes
837 * PackageManager api to install first and then replace it
838 * again.
839 */
Kenny Root25c925e2012-09-08 22:02:21 -0700840 private void sampleReplaceFromRawResource(int flags) throws Exception {
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800841 InstallParams ip = sampleInstallFromRawResource(flags, false);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800842 boolean replace = ((flags & PackageManager.INSTALL_REPLACE_EXISTING) != 0);
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800843 Log.i(TAG, "replace=" + replace);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800844 GenericReceiver receiver;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800845 if (replace) {
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800846 receiver = new ReplaceReceiver(ip.pkg.packageName);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800847 Log.i(TAG, "Creating replaceReceiver");
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800848 } else {
849 receiver = new InstallReceiver(ip.pkg.packageName);
850 }
851 try {
Kenny Rootb7c24702012-04-14 19:46:09 -0700852 invokeInstallPackage(ip.packageURI, flags, receiver, replace);
853 if (replace) {
854 assertInstall(ip.pkg, flags, ip.pkg.installLocation);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800855 }
856 } finally {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800857 cleanUpInstall(ip);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800858 }
859 }
860
Brett Chabotf76c56b2010-07-26 17:28:17 -0700861 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -0700862 public void testReplaceFailNormalInternal() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -0700863 sampleReplaceFromRawResource(0);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800864 }
865
Brett Chabotf76c56b2010-07-26 17:28:17 -0700866 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -0700867 public void testReplaceFailFwdLockedInternal() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -0700868 sampleReplaceFromRawResource(PackageManager.INSTALL_FORWARD_LOCK);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800869 }
870
Brett Chabotf76c56b2010-07-26 17:28:17 -0700871 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -0700872 public void testReplaceFailSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -0800873 // Do not run on devices with emulated external storage.
874 if (Environment.isExternalStorageEmulated()) {
875 return;
876 }
877
Suchi Amalapurapuae181712010-03-30 14:01:02 -0700878 sampleReplaceFromRawResource(PackageManager.INSTALL_EXTERNAL);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800879 }
880
Brett Chabotf76c56b2010-07-26 17:28:17 -0700881 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -0700882 public void testReplaceNormalInternal() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -0700883 sampleReplaceFromRawResource(PackageManager.INSTALL_REPLACE_EXISTING);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800884 }
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800885
Brett Chabotf76c56b2010-07-26 17:28:17 -0700886 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -0700887 public void testReplaceFwdLockedInternal() throws Exception {
888 sampleReplaceFromRawResource(PackageManager.INSTALL_REPLACE_EXISTING
889 | PackageManager.INSTALL_FORWARD_LOCK);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800890 }
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800891
Brett Chabotf76c56b2010-07-26 17:28:17 -0700892 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -0700893 public void testReplaceSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -0800894 // Do not run on devices with emulated external storage.
895 if (Environment.isExternalStorageEmulated()) {
896 return;
897 }
898
Kenny Root25c925e2012-09-08 22:02:21 -0700899 sampleReplaceFromRawResource(PackageManager.INSTALL_REPLACE_EXISTING
900 | PackageManager.INSTALL_EXTERNAL);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800901 }
902
Kenny Root7e921a12012-09-08 22:02:21 -0700903 /* -------------- Delete tests --- */
Kenny Root25c925e2012-09-08 22:02:21 -0700904 private static class DeleteObserver extends IPackageDeleteObserver.Stub {
Kenny Root7e921a12012-09-08 22:02:21 -0700905 private CountDownLatch mLatch = new CountDownLatch(1);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800906
Kenny Root7e921a12012-09-08 22:02:21 -0700907 private int mReturnCode;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800908
Kenny Root7e921a12012-09-08 22:02:21 -0700909 private final String mPackageName;
910
911 private String mObservedPackage;
912
913 public DeleteObserver(String packageName) {
914 mPackageName = packageName;
915 }
916
917 public boolean isSuccessful() {
918 return mReturnCode == PackageManager.DELETE_SUCCEEDED;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800919 }
920
Kenny Rootc39bb4a2011-02-28 13:27:19 -0800921 public void packageDeleted(String packageName, int returnCode) throws RemoteException {
Kenny Root7e921a12012-09-08 22:02:21 -0700922 mObservedPackage = packageName;
923
924 mReturnCode = returnCode;
925
926 mLatch.countDown();
927 }
928
929 public void waitForCompletion(long timeoutMillis) {
930 final long deadline = SystemClock.uptimeMillis() + timeoutMillis;
931
932 long waitTime = timeoutMillis;
933 while (waitTime > 0) {
934 try {
935 boolean done = mLatch.await(waitTime, TimeUnit.MILLISECONDS);
936 if (done) {
937 assertEquals(mPackageName, mObservedPackage);
938 return;
939 }
940 } catch (InterruptedException e) {
941 // TODO Auto-generated catch block
942 e.printStackTrace();
943 }
944 waitTime = deadline - SystemClock.uptimeMillis();
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800945 }
Kenny Root7e921a12012-09-08 22:02:21 -0700946
947 throw new AssertionError("Timeout waiting for package deletion");
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800948 }
949 }
950
951 class DeleteReceiver extends GenericReceiver {
952 String pkgName;
953
954 DeleteReceiver(String pkgName) {
955 this.pkgName = pkgName;
956 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_REMOVED);
957 filter.addDataScheme("package");
958 super.setFilter(filter);
959 }
960
961 public boolean notifyNow(Intent intent) {
962 String action = intent.getAction();
963 if (!Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
964 return false;
965 }
966 Uri data = intent.getData();
967 String installedPkg = data.getEncodedSchemeSpecificPart();
968 if (pkgName.equals(installedPkg)) {
969 return true;
970 }
971 return false;
972 }
973 }
974
Kenny Root7e921a12012-09-08 22:02:21 -0700975 public boolean invokeDeletePackage(final String pkgName, int flags, GenericReceiver receiver)
976 throws Exception {
977 ApplicationInfo info = getPm().getApplicationInfo(pkgName,
978 PackageManager.GET_UNINSTALLED_PACKAGES);
979
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800980 mContext.registerReceiver(receiver, receiver.filter);
981 try {
Kenny Root7e921a12012-09-08 22:02:21 -0700982 DeleteObserver observer = new DeleteObserver(pkgName);
983
Kenny Roota3e90792012-10-18 10:58:36 -0700984 getPm().deletePackage(pkgName, observer, flags | PackageManager.DELETE_ALL_USERS);
Kenny Root7e921a12012-09-08 22:02:21 -0700985 observer.waitForCompletion(MAX_WAIT_TIME);
986
987 assertUninstalled(info);
988
989 // Verify we received the broadcast
Kenny Roota3e90792012-10-18 10:58:36 -0700990 // TODO replace this with a CountDownLatch
991 synchronized (receiver) {
992 long waitTime = 0;
993 while ((!receiver.isDone()) && (waitTime < MAX_WAIT_TIME)) {
994 receiver.wait(WAIT_TIME_INCR);
995 waitTime += WAIT_TIME_INCR;
996 }
997 if (!receiver.isDone()) {
998 throw new Exception("Timed out waiting for PACKAGE_REMOVED notification");
999 }
Kenny Root7e921a12012-09-08 22:02:21 -07001000 }
1001 return receiver.received;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001002 } finally {
1003 mContext.unregisterReceiver(receiver);
1004 }
1005 }
1006
Kenny Root7e921a12012-09-08 22:02:21 -07001007 private static void assertUninstalled(ApplicationInfo info) throws Exception {
1008 File nativeLibraryFile = new File(info.nativeLibraryDir);
1009 assertFalse("Native library directory should be erased", nativeLibraryFile.exists());
1010 }
1011
Kenny Root25c925e2012-09-08 22:02:21 -07001012 public void deleteFromRawResource(int iFlags, int dFlags) throws Exception {
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -08001013 InstallParams ip = sampleInstallFromRawResource(iFlags, false);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001014 boolean retainData = ((dFlags & PackageManager.DELETE_KEEP_DATA) != 0);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001015 GenericReceiver receiver = new DeleteReceiver(ip.pkg.packageName);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001016 try {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07001017 assertTrue(invokeDeletePackage(ip.pkg.packageName, dFlags, receiver));
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001018 ApplicationInfo info = null;
1019 Log.i(TAG, "okay4");
1020 try {
Kenny Root25c925e2012-09-08 22:02:21 -07001021 info = getPm().getApplicationInfo(ip.pkg.packageName,
1022 PackageManager.GET_UNINSTALLED_PACKAGES);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001023 } catch (NameNotFoundException e) {
1024 info = null;
1025 }
1026 if (retainData) {
1027 assertNotNull(info);
1028 assertEquals(info.packageName, ip.pkg.packageName);
1029 File file = new File(info.dataDir);
1030 assertTrue(file.exists());
1031 } else {
1032 assertNull(info);
1033 }
1034 } catch (Exception e) {
1035 failStr(e);
1036 } finally {
1037 cleanUpInstall(ip);
1038 }
1039 }
1040
Brett Chabotf76c56b2010-07-26 17:28:17 -07001041 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001042 public void testDeleteNormalInternal() throws Exception {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001043 deleteFromRawResource(0, 0);
1044 }
1045
Brett Chabotf76c56b2010-07-26 17:28:17 -07001046 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001047 public void testDeleteFwdLockedInternal() throws Exception {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001048 deleteFromRawResource(PackageManager.INSTALL_FORWARD_LOCK, 0);
1049 }
1050
Brett Chabotf76c56b2010-07-26 17:28:17 -07001051 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001052 public void testDeleteSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001053 // Do not run on devices with emulated external storage.
1054 if (Environment.isExternalStorageEmulated()) {
1055 return;
1056 }
1057
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -08001058 deleteFromRawResource(PackageManager.INSTALL_EXTERNAL, 0);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001059 }
1060
Brett Chabotf76c56b2010-07-26 17:28:17 -07001061 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001062 public void testDeleteNormalInternalRetainData() throws Exception {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001063 deleteFromRawResource(0, PackageManager.DELETE_KEEP_DATA);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001064 }
1065
Brett Chabotf76c56b2010-07-26 17:28:17 -07001066 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001067 public void testDeleteFwdLockedInternalRetainData() throws Exception {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001068 deleteFromRawResource(PackageManager.INSTALL_FORWARD_LOCK, PackageManager.DELETE_KEEP_DATA);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001069 }
1070
Brett Chabotf76c56b2010-07-26 17:28:17 -07001071 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001072 public void testDeleteSdcardRetainData() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001073 // Do not run on devices with emulated external storage.
1074 if (Environment.isExternalStorageEmulated()) {
1075 return;
1076 }
1077
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001078 deleteFromRawResource(PackageManager.INSTALL_EXTERNAL, PackageManager.DELETE_KEEP_DATA);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001079 }
1080
Kenny Root25c925e2012-09-08 22:02:21 -07001081 /* sdcard mount/unmount tests ***** */
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001082
1083 class SdMountReceiver extends GenericReceiver {
1084 String pkgNames[];
Kenny Root25c925e2012-09-08 22:02:21 -07001085
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001086 boolean status = true;
1087
1088 SdMountReceiver(String[] pkgNames) {
1089 this.pkgNames = pkgNames;
1090 IntentFilter filter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
1091 super.setFilter(filter);
1092 }
1093
1094 public boolean notifyNow(Intent intent) {
1095 Log.i(TAG, "okay 1");
1096 String action = intent.getAction();
1097 if (!Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
1098 return false;
1099 }
1100 String rpkgList[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1101 for (String pkg : pkgNames) {
1102 boolean found = false;
1103 for (String rpkg : rpkgList) {
1104 if (rpkg.equals(pkg)) {
1105 found = true;
1106 break;
1107 }
1108 }
1109 if (!found) {
1110 status = false;
1111 return true;
1112 }
1113 }
1114 return true;
1115 }
1116 }
1117
1118 class SdUnMountReceiver extends GenericReceiver {
1119 String pkgNames[];
Kenny Root25c925e2012-09-08 22:02:21 -07001120
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001121 boolean status = true;
1122
1123 SdUnMountReceiver(String[] pkgNames) {
1124 this.pkgNames = pkgNames;
1125 IntentFilter filter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
1126 super.setFilter(filter);
1127 }
1128
1129 public boolean notifyNow(Intent intent) {
1130 String action = intent.getAction();
1131 if (!Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1132 return false;
1133 }
1134 String rpkgList[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1135 for (String pkg : pkgNames) {
1136 boolean found = false;
1137 for (String rpkg : rpkgList) {
1138 if (rpkg.equals(pkg)) {
1139 found = true;
1140 break;
1141 }
1142 }
1143 if (!found) {
1144 status = false;
1145 return true;
1146 }
1147 }
1148 return true;
1149 }
1150 }
1151
1152 IMountService getMs() {
1153 IBinder service = ServiceManager.getService("mount");
1154 if (service != null) {
1155 return IMountService.Stub.asInterface(service);
1156 } else {
1157 Log.e(TAG, "Can't get mount service");
1158 }
1159 return null;
1160 }
1161
Kenny Rootd7b421b2010-08-05 08:40:00 -07001162 boolean checkMediaState(String desired) {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001163 try {
Kenny Rootd7b421b2010-08-05 08:40:00 -07001164 String mPath = Environment.getExternalStorageDirectory().getPath();
1165 String actual = getMs().getVolumeState(mPath);
1166 if (desired.equals(actual)) {
1167 return true;
1168 } else {
1169 return false;
1170 }
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001171 } catch (RemoteException e) {
Kenny Rootd7b421b2010-08-05 08:40:00 -07001172 Log.e(TAG, "Exception while checking media state", e);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001173 return false;
1174 }
1175 }
1176
1177 boolean mountMedia() {
Kenny Root3c676892011-01-12 10:58:40 -08001178 // We can't mount emulated storage.
1179 if (Environment.isExternalStorageEmulated()) {
1180 return true;
1181 }
1182
Kenny Rootd7b421b2010-08-05 08:40:00 -07001183 if (checkMediaState(Environment.MEDIA_MOUNTED)) {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001184 return true;
1185 }
Kenny Rootd7b421b2010-08-05 08:40:00 -07001186
1187 final String path = Environment.getExternalStorageDirectory().toString();
1188 StorageListener observer = new StorageListener(Environment.MEDIA_MOUNTED);
1189 StorageManager sm = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
1190 sm.registerListener(observer);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001191 try {
Kenny Rootd7b421b2010-08-05 08:40:00 -07001192 // Wait on observer
1193 synchronized (observer) {
1194 int ret = getMs().mountVolume(path);
1195 if (ret != StorageResultCode.OperationSucceeded) {
1196 throw new Exception("Could not mount the media");
1197 }
1198 long waitTime = 0;
1199 while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) {
1200 observer.wait(WAIT_TIME_INCR);
1201 waitTime += WAIT_TIME_INCR;
1202 }
1203 if (!observer.isDone()) {
1204 throw new Exception("Timed out waiting for unmount media notification");
1205 }
1206 return true;
1207 }
1208 } catch (Exception e) {
1209 Log.e(TAG, "Exception : " + e);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001210 return false;
Kenny Rootd7b421b2010-08-05 08:40:00 -07001211 } finally {
1212 sm.unregisterListener(observer);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001213 }
1214 }
1215
1216 private boolean unmountMedia() {
Kenny Root3c676892011-01-12 10:58:40 -08001217 // We can't unmount emulated storage.
1218 if (Environment.isExternalStorageEmulated()) {
1219 return true;
1220 }
1221
Kenny Rootd7b421b2010-08-05 08:40:00 -07001222 if (checkMediaState(Environment.MEDIA_UNMOUNTED)) {
1223 return true;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001224 }
Neal Nguyenedb979a2010-04-22 13:54:32 -07001225
Kenny Rootd7b421b2010-08-05 08:40:00 -07001226 final String path = Environment.getExternalStorageDirectory().getPath();
1227 StorageListener observer = new StorageListener(Environment.MEDIA_UNMOUNTED);
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001228 StorageManager sm = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
1229 sm.registerListener(observer);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001230 try {
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001231 // Wait on observer
Kenny Root25c925e2012-09-08 22:02:21 -07001232 synchronized (observer) {
Ben Komalo13c71972011-09-07 16:35:56 -07001233 getMs().unmountVolume(path, true, false);
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001234 long waitTime = 0;
Kenny Root25c925e2012-09-08 22:02:21 -07001235 while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) {
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001236 observer.wait(WAIT_TIME_INCR);
1237 waitTime += WAIT_TIME_INCR;
1238 }
Kenny Root25c925e2012-09-08 22:02:21 -07001239 if (!observer.isDone()) {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07001240 throw new Exception("Timed out waiting for unmount media notification");
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001241 }
1242 return true;
1243 }
1244 } catch (Exception e) {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07001245 Log.e(TAG, "Exception : " + e);
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001246 return false;
1247 } finally {
1248 sm.unregisterListener(observer);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001249 }
1250 }
1251
Kenny Root25c925e2012-09-08 22:02:21 -07001252 private boolean mountFromRawResource() throws Exception {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001253 // Install pkg on sdcard
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001254 InstallParams ip = sampleInstallFromRawResource(PackageManager.INSTALL_EXTERNAL, false);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001255 if (localLOGV) Log.i(TAG, "Installed pkg on sdcard");
Kenny Rootd7b421b2010-08-05 08:40:00 -07001256 boolean origState = checkMediaState(Environment.MEDIA_MOUNTED);
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001257 boolean registeredReceiver = false;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001258 SdMountReceiver receiver = new SdMountReceiver(new String[]{ip.pkg.packageName});
1259 try {
1260 if (localLOGV) Log.i(TAG, "Unmounting media");
1261 // Unmount media
1262 assertTrue(unmountMedia());
1263 if (localLOGV) Log.i(TAG, "Unmounted media");
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001264 // Register receiver here
1265 PackageManager pm = getPm();
1266 mContext.registerReceiver(receiver, receiver.filter);
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001267 registeredReceiver = true;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001268
1269 // Wait on receiver
1270 synchronized (receiver) {
1271 if (localLOGV) Log.i(TAG, "Mounting media");
1272 // Mount media again
1273 assertTrue(mountMedia());
1274 if (localLOGV) Log.i(TAG, "Mounted media");
1275 if (localLOGV) Log.i(TAG, "Waiting for notification");
1276 long waitTime = 0;
1277 // Verify we received the broadcast
1278 waitTime = 0;
Kenny Root25c925e2012-09-08 22:02:21 -07001279 while ((!receiver.isDone()) && (waitTime < MAX_WAIT_TIME)) {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001280 receiver.wait(WAIT_TIME_INCR);
1281 waitTime += WAIT_TIME_INCR;
1282 }
1283 if(!receiver.isDone()) {
1284 failStr("Timed out waiting for EXTERNAL_APPLICATIONS notification");
1285 }
1286 return receiver.received;
1287 }
1288 } catch (InterruptedException e) {
1289 failStr(e);
1290 return false;
1291 } finally {
Kenny Root25c925e2012-09-08 22:02:21 -07001292 if (registeredReceiver) {
1293 mContext.unregisterReceiver(receiver);
1294 }
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001295 // Restore original media state
1296 if (origState) {
1297 mountMedia();
1298 } else {
1299 unmountMedia();
1300 }
1301 if (localLOGV) Log.i(TAG, "Cleaning up install");
1302 cleanUpInstall(ip);
1303 }
1304 }
1305
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001306 /*
1307 * Install package on sdcard. Unmount and then mount the media.
1308 * (Use PackageManagerService private api for now)
1309 * Make sure the installed package is available.
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001310 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07001311 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001312 public void testMountSdNormalInternal() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001313 // Do not run on devices with emulated external storage.
1314 if (Environment.isExternalStorageEmulated()) {
1315 return;
1316 }
1317
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001318 assertTrue(mountFromRawResource());
1319 }
1320
Kenny Root25c925e2012-09-08 22:02:21 -07001321 void cleanUpInstall(InstallParams ip) throws Exception {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001322 if (ip == null) {
1323 return;
1324 }
1325 Runtime.getRuntime().gc();
Kenny Root7e921a12012-09-08 22:02:21 -07001326
1327 final String packageName = ip.pkg.packageName;
1328 Log.i(TAG, "Deleting package : " + packageName);
1329
1330 ApplicationInfo info = null;
1331 try {
1332 info = getPm().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
1333 } catch (NameNotFoundException ignored) {
1334 }
1335
1336 DeleteObserver observer = new DeleteObserver(packageName);
Kenny Roota3e90792012-10-18 10:58:36 -07001337 getPm().deletePackage(packageName, observer, PackageManager.DELETE_ALL_USERS);
Kenny Root7e921a12012-09-08 22:02:21 -07001338 observer.waitForCompletion(MAX_WAIT_TIME);
1339
1340 try {
1341 if (info != null) {
1342 assertUninstalled(info);
1343 }
1344 } finally {
1345 File outFile = new File(ip.pkg.mScanPath);
1346 if (outFile != null && outFile.exists()) {
1347 outFile.delete();
1348 }
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001349 }
1350 }
Kenny Root25c925e2012-09-08 22:02:21 -07001351
1352 private void cleanUpInstall(String pkgName) throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07001353 if (pkgName == null) {
1354 return;
1355 }
1356 Log.i(TAG, "Deleting package : " + pkgName);
1357 try {
1358 ApplicationInfo info = getPm().getApplicationInfo(pkgName,
1359 PackageManager.GET_UNINSTALLED_PACKAGES);
Kenny Root25c925e2012-09-08 22:02:21 -07001360
Suchi Amalapurapuae181712010-03-30 14:01:02 -07001361 if (info != null) {
Kenny Root7e921a12012-09-08 22:02:21 -07001362 DeleteObserver observer = new DeleteObserver(pkgName);
Kenny Roota3e90792012-10-18 10:58:36 -07001363 getPm().deletePackage(pkgName, observer, PackageManager.DELETE_ALL_USERS);
Kenny Root7e921a12012-09-08 22:02:21 -07001364 observer.waitForCompletion(MAX_WAIT_TIME);
1365 assertUninstalled(info);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07001366 }
Kenny Root25c925e2012-09-08 22:02:21 -07001367 } catch (NameNotFoundException e) {
1368 }
Suchi Amalapurapuae181712010-03-30 14:01:02 -07001369 }
Suchi Amalapurapu117818e2010-02-09 03:45:40 -08001370
Brett Chabotf76c56b2010-07-26 17:28:17 -07001371 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001372 public void testManifestInstallLocationInternal() throws Exception {
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -08001373 installFromRawResource("install.apk", R.raw.install_loc_internal,
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001374 0, true, false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
Suchi Amalapurapu117818e2010-02-09 03:45:40 -08001375 }
1376
Brett Chabotf76c56b2010-07-26 17:28:17 -07001377 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001378 public void testManifestInstallLocationSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001379 // Do not run on devices with emulated external storage.
1380 if (Environment.isExternalStorageEmulated()) {
1381 return;
1382 }
1383
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -08001384 installFromRawResource("install.apk", R.raw.install_loc_sdcard,
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001385 0, true, false, -1, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
Suchi Amalapurapu117818e2010-02-09 03:45:40 -08001386 }
1387
Brett Chabotf76c56b2010-07-26 17:28:17 -07001388 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001389 public void testManifestInstallLocationAuto() throws Exception {
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -08001390 installFromRawResource("install.apk", R.raw.install_loc_auto,
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001391 0, true, false, -1, PackageInfo.INSTALL_LOCATION_AUTO);
Suchi Amalapurapu117818e2010-02-09 03:45:40 -08001392 }
1393
Brett Chabotf76c56b2010-07-26 17:28:17 -07001394 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001395 public void testManifestInstallLocationUnspecified() throws Exception {
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -08001396 installFromRawResource("install.apk", R.raw.install_loc_unspecified,
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -07001397 0, true, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
Suchi Amalapurapu117818e2010-02-09 03:45:40 -08001398 }
1399
Brett Chabotf76c56b2010-07-26 17:28:17 -07001400 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001401 public void testManifestInstallLocationFwdLockedFlagSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001402 // Do not run on devices with emulated external storage.
1403 if (Environment.isExternalStorageEmulated()) {
1404 return;
1405 }
1406
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001407 installFromRawResource("install.apk", R.raw.install_loc_unspecified,
1408 PackageManager.INSTALL_FORWARD_LOCK |
Kenny Rootbf023582012-05-02 16:56:15 -07001409 PackageManager.INSTALL_EXTERNAL, true, false, -1,
1410 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001411 }
1412
Brett Chabotf76c56b2010-07-26 17:28:17 -07001413 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001414 public void testManifestInstallLocationFwdLockedSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001415 // Do not run on devices with emulated external storage.
1416 if (Environment.isExternalStorageEmulated()) {
1417 return;
1418 }
1419
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -08001420 installFromRawResource("install.apk", R.raw.install_loc_sdcard,
Kenny Root25c925e2012-09-08 22:02:21 -07001421 PackageManager.INSTALL_FORWARD_LOCK, true, false, -1,
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001422 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
1423 }
1424
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001425 /*
1426 * Install a package on internal flash via PackageManager install flag. Replace
1427 * the package via flag to install on sdcard. Make sure the new flag overrides
1428 * the old install location.
1429 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07001430 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001431 public void testReplaceFlagInternalSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001432 // Do not run on devices with emulated external storage.
1433 if (Environment.isExternalStorageEmulated()) {
1434 return;
1435 }
1436
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001437 int iFlags = 0;
1438 int rFlags = PackageManager.INSTALL_EXTERNAL;
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001439 InstallParams ip = sampleInstallFromRawResource(iFlags, false);
1440 GenericReceiver receiver = new ReplaceReceiver(ip.pkg.packageName);
1441 int replaceFlags = rFlags | PackageManager.INSTALL_REPLACE_EXISTING;
1442 try {
Kenny Rootb7c24702012-04-14 19:46:09 -07001443 invokeInstallPackage(ip.packageURI, replaceFlags, receiver, true);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001444 assertInstall(ip.pkg, rFlags, ip.pkg.installLocation);
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001445 } catch (Exception e) {
1446 failStr("Failed with exception : " + e);
1447 } finally {
1448 cleanUpInstall(ip);
1449 }
1450 }
1451
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001452 /*
1453 * Install a package on sdcard via PackageManager install flag. Replace
1454 * the package with no flags or manifest option and make sure the old
1455 * install location is retained.
1456 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07001457 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001458 public void testReplaceFlagSdcardInternal() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001459 // Do not run on devices with emulated external storage.
1460 if (Environment.isExternalStorageEmulated()) {
1461 return;
1462 }
1463
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001464 int iFlags = PackageManager.INSTALL_EXTERNAL;
1465 int rFlags = 0;
1466 InstallParams ip = sampleInstallFromRawResource(iFlags, false);
1467 GenericReceiver receiver = new ReplaceReceiver(ip.pkg.packageName);
1468 int replaceFlags = rFlags | PackageManager.INSTALL_REPLACE_EXISTING;
1469 try {
Kenny Rootb7c24702012-04-14 19:46:09 -07001470 invokeInstallPackage(ip.packageURI, replaceFlags, receiver, true);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001471 assertInstall(ip.pkg, iFlags, ip.pkg.installLocation);
1472 } catch (Exception e) {
1473 failStr("Failed with exception : " + e);
1474 } finally {
1475 cleanUpInstall(ip);
1476 }
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001477 }
1478
Brett Chabotf76c56b2010-07-26 17:28:17 -07001479 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001480 public void testManifestInstallLocationReplaceInternalSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001481 // Do not run on devices with emulated external storage.
1482 if (Environment.isExternalStorageEmulated()) {
1483 return;
1484 }
1485
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001486 int iFlags = 0;
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -07001487 int iApk = R.raw.install_loc_internal;
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001488 int rFlags = 0;
1489 int rApk = R.raw.install_loc_sdcard;
1490 InstallParams ip = installFromRawResource("install.apk", iApk,
1491 iFlags, false,
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -07001492 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001493 GenericReceiver receiver = new ReplaceReceiver(ip.pkg.packageName);
1494 int replaceFlags = rFlags | PackageManager.INSTALL_REPLACE_EXISTING;
1495 try {
1496 InstallParams rp = installFromRawResource("install.apk", rApk,
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001497 replaceFlags, false,
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001498 false, -1, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
1499 assertInstall(rp.pkg, replaceFlags, rp.pkg.installLocation);
1500 } catch (Exception e) {
1501 failStr("Failed with exception : " + e);
1502 } finally {
1503 cleanUpInstall(ip);
1504 }
1505 }
1506
Brett Chabotf76c56b2010-07-26 17:28:17 -07001507 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001508 public void testManifestInstallLocationReplaceSdcardInternal() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001509 // Do not run on devices with emulated external storage.
1510 if (Environment.isExternalStorageEmulated()) {
1511 return;
1512 }
1513
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001514 int iFlags = 0;
1515 int iApk = R.raw.install_loc_sdcard;
1516 int rFlags = 0;
1517 int rApk = R.raw.install_loc_unspecified;
1518 InstallParams ip = installFromRawResource("install.apk", iApk,
1519 iFlags, false,
1520 false, -1, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001521 int replaceFlags = rFlags | PackageManager.INSTALL_REPLACE_EXISTING;
1522 try {
1523 InstallParams rp = installFromRawResource("install.apk", rApk,
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001524 replaceFlags, false,
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001525 false, -1, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
1526 assertInstall(rp.pkg, replaceFlags, ip.pkg.installLocation);
1527 } catch (Exception e) {
1528 failStr("Failed with exception : " + e);
1529 } finally {
1530 cleanUpInstall(ip);
1531 }
Suchi Amalapurapu117818e2010-02-09 03:45:40 -08001532 }
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001533
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001534 class MoveReceiver extends GenericReceiver {
1535 String pkgName;
Kenny Root25c925e2012-09-08 22:02:21 -07001536
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001537 final static int INVALID = -1;
Kenny Root25c925e2012-09-08 22:02:21 -07001538
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001539 final static int REMOVED = 1;
Kenny Root25c925e2012-09-08 22:02:21 -07001540
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001541 final static int ADDED = 2;
Kenny Root25c925e2012-09-08 22:02:21 -07001542
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001543 int removed = INVALID;
1544
1545 MoveReceiver(String pkgName) {
1546 this.pkgName = pkgName;
1547 filter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
1548 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
1549 super.setFilter(filter);
1550 }
1551
1552 public boolean notifyNow(Intent intent) {
1553 String action = intent.getAction();
1554 Log.i(TAG, "MoveReceiver::" + action);
1555 if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1556 String[] list = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1557 if (list != null) {
1558 for (String pkg : list) {
1559 if (pkg.equals(pkgName)) {
1560 removed = REMOVED;
1561 break;
1562 }
1563 }
1564 }
1565 removed = REMOVED;
1566 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
1567 if (removed != REMOVED) {
1568 return false;
1569 }
1570 String[] list = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1571 if (list != null) {
1572 for (String pkg : list) {
1573 if (pkg.equals(pkgName)) {
1574 removed = ADDED;
1575 return true;
1576 }
1577 }
1578 }
1579 }
1580 return false;
1581 }
1582 }
1583
1584 private class PackageMoveObserver extends IPackageMoveObserver.Stub {
1585 public int returnCode;
Kenny Root25c925e2012-09-08 22:02:21 -07001586
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001587 private boolean doneFlag = false;
Kenny Root25c925e2012-09-08 22:02:21 -07001588
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001589 public String packageName;
Kenny Root25c925e2012-09-08 22:02:21 -07001590
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001591 public PackageMoveObserver(String pkgName) {
1592 packageName = pkgName;
1593 }
Kenny Root25c925e2012-09-08 22:02:21 -07001594
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001595 public void packageMoved(String packageName, int returnCode) {
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001596 Log.i("DEBUG_MOVE::", "pkg = " + packageName + ", " + "ret = " + returnCode);
1597 if (!packageName.equals(this.packageName)) {
1598 return;
1599 }
Kenny Root25c925e2012-09-08 22:02:21 -07001600 synchronized (this) {
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001601 this.returnCode = returnCode;
1602 doneFlag = true;
1603 notifyAll();
1604 }
1605 }
1606
1607 public boolean isDone() {
1608 return doneFlag;
1609 }
1610 }
1611
Kenny Root25c925e2012-09-08 22:02:21 -07001612 public boolean invokeMovePackage(String pkgName, int flags, GenericReceiver receiver)
1613 throws Exception {
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001614 PackageMoveObserver observer = new PackageMoveObserver(pkgName);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001615 final boolean received = false;
1616 mContext.registerReceiver(receiver, receiver.filter);
1617 try {
1618 // Wait on observer
Kenny Root25c925e2012-09-08 22:02:21 -07001619 synchronized (observer) {
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001620 synchronized (receiver) {
1621 getPm().movePackage(pkgName, observer, flags);
1622 long waitTime = 0;
Kenny Root25c925e2012-09-08 22:02:21 -07001623 while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) {
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001624 observer.wait(WAIT_TIME_INCR);
1625 waitTime += WAIT_TIME_INCR;
1626 }
Kenny Root25c925e2012-09-08 22:02:21 -07001627 if (!observer.isDone()) {
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001628 throw new Exception("Timed out waiting for pkgmove callback");
1629 }
1630 if (observer.returnCode != PackageManager.MOVE_SUCCEEDED) {
1631 return false;
1632 }
1633 // Verify we received the broadcast
1634 waitTime = 0;
Kenny Root25c925e2012-09-08 22:02:21 -07001635 while ((!receiver.isDone()) && (waitTime < MAX_WAIT_TIME)) {
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001636 receiver.wait(WAIT_TIME_INCR);
1637 waitTime += WAIT_TIME_INCR;
1638 }
Kenny Root25c925e2012-09-08 22:02:21 -07001639 if (!receiver.isDone()) {
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001640 throw new Exception("Timed out waiting for MOVE notifications");
1641 }
1642 return receiver.received;
1643 }
1644 }
1645 } finally {
1646 mContext.unregisterReceiver(receiver);
1647 }
1648 }
Kenny Root25c925e2012-09-08 22:02:21 -07001649
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001650 private boolean invokeMovePackageFail(String pkgName, int flags, int errCode) throws Exception {
1651 PackageMoveObserver observer = new PackageMoveObserver(pkgName);
1652 try {
1653 // Wait on observer
Kenny Root25c925e2012-09-08 22:02:21 -07001654 synchronized (observer) {
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001655 getPm().movePackage(pkgName, observer, flags);
1656 long waitTime = 0;
Kenny Root25c925e2012-09-08 22:02:21 -07001657 while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) {
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001658 observer.wait(WAIT_TIME_INCR);
1659 waitTime += WAIT_TIME_INCR;
1660 }
Kenny Root25c925e2012-09-08 22:02:21 -07001661 if (!observer.isDone()) {
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001662 throw new Exception("Timed out waiting for pkgmove callback");
1663 }
1664 assertEquals(errCode, observer.returnCode);
1665 }
1666 } finally {
1667 }
1668 return true;
1669 }
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001670
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07001671 private int getDefaultInstallLoc() {
Suchi Amalapurapu9b10ef52010-03-03 09:45:24 -08001672 int origDefaultLoc = PackageInfo.INSTALL_LOCATION_AUTO;
1673 try {
Jeff Sharkey625239a2012-09-26 22:03:49 -07001674 origDefaultLoc = Settings.Global.getInt(mContext.getContentResolver(),
1675 Settings.Global.DEFAULT_INSTALL_LOCATION);
Suchi Amalapurapu9b10ef52010-03-03 09:45:24 -08001676 } catch (SettingNotFoundException e1) {
1677 }
1678 return origDefaultLoc;
1679 }
1680
1681 private void setInstallLoc(int loc) {
Jeff Sharkey625239a2012-09-26 22:03:49 -07001682 Settings.Global.putInt(mContext.getContentResolver(),
1683 Settings.Global.DEFAULT_INSTALL_LOCATION, loc);
Suchi Amalapurapu9b10ef52010-03-03 09:45:24 -08001684 }
Kenny Root25c925e2012-09-08 22:02:21 -07001685
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001686 /*
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001687 * Tests for moving apps between internal and external storage
1688 */
1689 /*
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001690 * Utility function that reads a apk bundled as a raw resource
1691 * copies it into own data directory and invokes
1692 * PackageManager api to install first and then replace it
1693 * again.
1694 */
Neal Nguyenedb979a2010-04-22 13:54:32 -07001695
Kenny Root25c925e2012-09-08 22:02:21 -07001696 private void moveFromRawResource(String outFileName, int rawResId, int installFlags,
1697 int moveFlags, boolean cleanUp, boolean fail, int result) throws Exception {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07001698 int origDefaultLoc = getDefaultInstallLoc();
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001699 InstallParams ip = null;
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001700 try {
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001701 setInstallLoc(PackageHelper.APP_INSTALL_AUTO);
1702 // Install first
1703 ip = installFromRawResource("install.apk", rawResId, installFlags, false,
1704 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
1705 ApplicationInfo oldAppInfo = getPm().getApplicationInfo(ip.pkg.packageName, 0);
1706 if (fail) {
1707 assertTrue(invokeMovePackageFail(ip.pkg.packageName, moveFlags, result));
1708 ApplicationInfo info = getPm().getApplicationInfo(ip.pkg.packageName, 0);
1709 assertNotNull(info);
1710 assertEquals(oldAppInfo.flags, info.flags);
1711 } else {
1712 // Create receiver based on expRetCode
1713 MoveReceiver receiver = new MoveReceiver(ip.pkg.packageName);
Kenny Root25c925e2012-09-08 22:02:21 -07001714 boolean retCode = invokeMovePackage(ip.pkg.packageName, moveFlags, receiver);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001715 assertTrue(retCode);
1716 ApplicationInfo info = getPm().getApplicationInfo(ip.pkg.packageName, 0);
Kenny Root6a6b0072010-10-07 16:46:10 -07001717 assertNotNull("ApplicationInfo for recently installed application should exist",
1718 info);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001719 if ((moveFlags & PackageManager.MOVE_INTERNAL) != 0) {
Kenny Root6a6b0072010-10-07 16:46:10 -07001720 assertTrue("ApplicationInfo.FLAG_EXTERNAL_STORAGE flag should NOT be set",
1721 (info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == 0);
Kenny Root7e921a12012-09-08 22:02:21 -07001722 assertStartsWith("Native library dir should be in dataDir",
1723 info.dataDir, info.nativeLibraryDir);
1724 } else if ((moveFlags & PackageManager.MOVE_EXTERNAL_MEDIA) != 0) {
Kenny Root6a6b0072010-10-07 16:46:10 -07001725 assertTrue("ApplicationInfo.FLAG_EXTERNAL_STORAGE flag should be set",
1726 (info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0);
Kenny Root7e921a12012-09-08 22:02:21 -07001727 assertStartsWith("Native library dir should point to ASEC",
1728 SECURE_CONTAINERS_PREFIX, info.nativeLibraryDir);
Kenny Root6a6b0072010-10-07 16:46:10 -07001729 final File nativeLibSymLink = new File(info.dataDir, "lib");
Kenny Root7e921a12012-09-08 22:02:21 -07001730 assertStartsWith("The data directory should have a 'lib' symlink that points to the ASEC container",
1731 SECURE_CONTAINERS_PREFIX, nativeLibSymLink.getCanonicalPath());
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001732 }
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001733 }
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001734 } catch (NameNotFoundException e) {
1735 failStr("Pkg hasnt been installed correctly");
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001736 } catch (Exception e) {
1737 failStr("Failed with exception : " + e);
1738 } finally {
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001739 if (ip != null) {
1740 cleanUpInstall(ip);
1741 }
Suchi Amalapurapu9b10ef52010-03-03 09:45:24 -08001742 // Restore default install location
1743 setInstallLoc(origDefaultLoc);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001744 }
1745 }
Kenny Root25c925e2012-09-08 22:02:21 -07001746
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001747 private void sampleMoveFromRawResource(int installFlags, int moveFlags, boolean fail,
Kenny Root25c925e2012-09-08 22:02:21 -07001748 int result) throws Exception {
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001749 moveFromRawResource("install.apk",
1750 R.raw.install, installFlags, moveFlags, true,
1751 fail, result);
1752 }
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001753
Brett Chabotf76c56b2010-07-26 17:28:17 -07001754 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001755 public void testMoveAppInternalToExternal() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001756 // Do not run on devices with emulated external storage.
1757 if (Environment.isExternalStorageEmulated()) {
1758 return;
1759 }
1760
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001761 int installFlags = PackageManager.INSTALL_INTERNAL;
1762 int moveFlags = PackageManager.MOVE_EXTERNAL_MEDIA;
1763 boolean fail = false;
1764 int result = PackageManager.MOVE_SUCCEEDED;
1765 sampleMoveFromRawResource(installFlags, moveFlags, fail, result);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001766 }
1767
Brett Chabotf76c56b2010-07-26 17:28:17 -07001768 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001769 public void testMoveAppInternalToInternal() throws Exception {
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001770 int installFlags = PackageManager.INSTALL_INTERNAL;
1771 int moveFlags = PackageManager.MOVE_INTERNAL;
1772 boolean fail = true;
1773 int result = PackageManager.MOVE_FAILED_INVALID_LOCATION;
1774 sampleMoveFromRawResource(installFlags, moveFlags, fail, result);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001775 }
1776
Brett Chabotf76c56b2010-07-26 17:28:17 -07001777 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001778 public void testMoveAppExternalToExternal() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001779 // Do not run on devices with emulated external storage.
1780 if (Environment.isExternalStorageEmulated()) {
1781 return;
1782 }
1783
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001784 int installFlags = PackageManager.INSTALL_EXTERNAL;
1785 int moveFlags = PackageManager.MOVE_EXTERNAL_MEDIA;
1786 boolean fail = true;
1787 int result = PackageManager.MOVE_FAILED_INVALID_LOCATION;
1788 sampleMoveFromRawResource(installFlags, moveFlags, fail, result);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001789 }
Kenny Root3c676892011-01-12 10:58:40 -08001790
Brett Chabotf76c56b2010-07-26 17:28:17 -07001791 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001792 public void testMoveAppExternalToInternal() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001793 // Do not run on devices with emulated external storage.
1794 if (Environment.isExternalStorageEmulated()) {
1795 return;
1796 }
1797
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001798 int installFlags = PackageManager.INSTALL_EXTERNAL;
1799 int moveFlags = PackageManager.MOVE_INTERNAL;
1800 boolean fail = false;
1801 int result = PackageManager.MOVE_SUCCEEDED;
1802 sampleMoveFromRawResource(installFlags, moveFlags, fail, result);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001803 }
Kenny Root3c676892011-01-12 10:58:40 -08001804
Brett Chabotf76c56b2010-07-26 17:28:17 -07001805 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001806 public void testMoveAppForwardLocked() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001807 // Do not run on devices with emulated external storage.
1808 if (Environment.isExternalStorageEmulated()) {
1809 return;
1810 }
1811
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001812 int installFlags = PackageManager.INSTALL_FORWARD_LOCK;
1813 int moveFlags = PackageManager.MOVE_EXTERNAL_MEDIA;
Kenny Rootbf023582012-05-02 16:56:15 -07001814 boolean fail = false;
1815 int result = PackageManager.MOVE_SUCCEEDED;
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001816 sampleMoveFromRawResource(installFlags, moveFlags, fail, result);
1817 }
Kenny Root3c676892011-01-12 10:58:40 -08001818
Brett Chabotf76c56b2010-07-26 17:28:17 -07001819 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001820 public void testMoveAppFailInternalToExternalDelete() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001821 // Do not run on devices with emulated external storage.
1822 if (Environment.isExternalStorageEmulated()) {
1823 return;
1824 }
1825
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001826 int installFlags = 0;
1827 int moveFlags = PackageManager.MOVE_EXTERNAL_MEDIA;
1828 boolean fail = true;
1829 final int result = PackageManager.MOVE_FAILED_DOESNT_EXIST;
Neal Nguyenedb979a2010-04-22 13:54:32 -07001830
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001831 int rawResId = R.raw.install;
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07001832 int origDefaultLoc = getDefaultInstallLoc();
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001833 InstallParams ip = null;
1834 try {
1835 PackageManager pm = getPm();
1836 setInstallLoc(PackageHelper.APP_INSTALL_AUTO);
1837 // Install first
1838 ip = installFromRawResource("install.apk", R.raw.install, installFlags, false,
1839 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
1840 // Delete the package now retaining data.
Suchi Amalapurapu9a212ad2010-05-18 11:06:53 -07001841 GenericReceiver receiver = new DeleteReceiver(ip.pkg.packageName);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001842 invokeDeletePackage(ip.pkg.packageName, PackageManager.DELETE_KEEP_DATA, receiver);
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001843 assertTrue(invokeMovePackageFail(ip.pkg.packageName, moveFlags, result));
1844 } catch (Exception e) {
1845 failStr(e);
1846 } finally {
1847 if (ip != null) {
1848 cleanUpInstall(ip);
1849 }
1850 // Restore default install location
1851 setInstallLoc(origDefaultLoc);
1852 }
1853 }
Kenny Root3c676892011-01-12 10:58:40 -08001854
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001855 /*
1856 * Test that an install error code is returned when media is unmounted
1857 * and package installed on sdcard via package manager flag.
1858 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07001859 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001860 public void testInstallSdcardUnmount() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001861 // Do not run on devices with emulated external storage.
1862 if (Environment.isExternalStorageEmulated()) {
1863 return;
1864 }
1865
Kenny Rootd7b421b2010-08-05 08:40:00 -07001866 boolean origState = checkMediaState(Environment.MEDIA_MOUNTED);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001867 try {
1868 // Unmount sdcard
1869 assertTrue(unmountMedia());
1870 // Try to install and make sure an error code is returned.
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07001871 installFromRawResource("install.apk", R.raw.install,
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001872 PackageManager.INSTALL_EXTERNAL, false,
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -07001873 true, PackageManager.INSTALL_FAILED_MEDIA_UNAVAILABLE,
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07001874 PackageInfo.INSTALL_LOCATION_AUTO);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001875 } finally {
1876 // Restore original media state
1877 if (origState) {
1878 mountMedia();
1879 } else {
1880 unmountMedia();
1881 }
1882 }
1883 }
1884
1885 /*
Neal Nguyenedb979a2010-04-22 13:54:32 -07001886 * Unmount sdcard. Try installing an app with manifest option to install
1887 * on sdcard. Make sure it gets installed on internal flash.
1888 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07001889 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001890 public void testInstallManifestSdcardUnmount() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001891 // Do not run on devices with emulated external storage.
1892 if (Environment.isExternalStorageEmulated()) {
1893 return;
1894 }
1895
Kenny Rootd7b421b2010-08-05 08:40:00 -07001896 boolean origState = checkMediaState(Environment.MEDIA_MOUNTED);
1897 try {
1898 // Unmount sdcard
1899 assertTrue(unmountMedia());
1900 InstallParams ip = new InstallParams("install.apk", R.raw.install_loc_sdcard);
1901 installFromRawResource(ip, 0, true, false, -1,
1902 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
1903 } finally {
1904 // Restore original media state
1905 if (origState) {
1906 mountMedia();
1907 } else {
1908 unmountMedia();
1909 }
1910 }
1911 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001912
Kenny Root25c925e2012-09-08 22:02:21 -07001913 /*---------- Recommended install location tests ----*/
1914 /*
1915 * PrecedenceSuffixes:
1916 * Flag : FlagI, FlagE, FlagF
1917 * I - internal, E - external, F - forward locked, Flag suffix absent if not using any option.
1918 * Manifest: ManifestI, ManifestE, ManifestA, Manifest suffix absent if not using any option.
1919 * Existing: Existing suffix absent if not existing.
1920 * User: UserI, UserE, UserA, User suffix absent if not existing.
1921 *
1922 */
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -07001923
Kenny Root3c676892011-01-12 10:58:40 -08001924 /*
1925 * Install an app on internal flash
1926 */
1927 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001928 public void testFlagI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001929 sampleInstallFromRawResource(PackageManager.INSTALL_INTERNAL, true);
1930 }
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -07001931
Kenny Root3c676892011-01-12 10:58:40 -08001932 /*
1933 * Install an app on sdcard.
1934 */
1935 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001936 public void testFlagE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001937 // Do not run on devices with emulated external storage.
1938 if (Environment.isExternalStorageEmulated()) {
1939 return;
1940 }
1941
1942 sampleInstallFromRawResource(PackageManager.INSTALL_EXTERNAL, true);
1943 }
1944
1945 /*
1946 * Install an app forward-locked.
1947 */
1948 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001949 public void testFlagF() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001950 sampleInstallFromRawResource(PackageManager.INSTALL_FORWARD_LOCK, true);
1951 }
1952
1953 /*
1954 * Install an app with both internal and external flags set. should fail
1955 */
1956 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001957 public void testFlagIE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001958 installFromRawResource("install.apk", R.raw.install,
1959 PackageManager.INSTALL_EXTERNAL | PackageManager.INSTALL_INTERNAL,
1960 false,
1961 true, PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION,
1962 PackageInfo.INSTALL_LOCATION_AUTO);
1963 }
1964
1965 /*
1966 * Install an app with both internal and forward-lock flags set.
1967 */
1968 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001969 public void testFlagIF() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001970 sampleInstallFromRawResource(PackageManager.INSTALL_FORWARD_LOCK
1971 | PackageManager.INSTALL_INTERNAL, true);
1972 }
1973
1974 /*
Kenny Root6dceb882012-04-12 14:23:49 -07001975 * Install an app with both external and forward-lock flags set.
Kenny Root3c676892011-01-12 10:58:40 -08001976 */
1977 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001978 public void testFlagEF() throws Exception {
Kenny Root6dceb882012-04-12 14:23:49 -07001979 // Do not run on devices with emulated external storage.
1980 if (Environment.isExternalStorageEmulated()) {
1981 return;
1982 }
1983
1984 sampleInstallFromRawResource(PackageManager.INSTALL_FORWARD_LOCK
1985 | PackageManager.INSTALL_EXTERNAL, true);
Kenny Root3c676892011-01-12 10:58:40 -08001986 }
1987
1988 /*
1989 * Install an app with both internal and external flags set with forward
1990 * lock. Should fail.
1991 */
1992 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001993 public void testFlagIEF() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001994 installFromRawResource("install.apk", R.raw.install,
1995 PackageManager.INSTALL_FORWARD_LOCK | PackageManager.INSTALL_INTERNAL |
1996 PackageManager.INSTALL_EXTERNAL,
1997 false,
1998 true, PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION,
1999 PackageInfo.INSTALL_LOCATION_AUTO);
2000 }
2001
Kenny Root25c925e2012-09-08 22:02:21 -07002002 /*
2003 * Install an app with both internal and manifest option set.
2004 * should install on internal.
2005 */
2006 @LargeTest
2007 public void testFlagIManifestI() throws Exception {
2008 installFromRawResource("install.apk", R.raw.install_loc_internal,
2009 PackageManager.INSTALL_INTERNAL,
2010 true,
2011 false, -1,
2012 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2013 }
2014 /*
2015 * Install an app with both internal and manifest preference for
2016 * preferExternal. Should install on internal.
2017 */
2018 @LargeTest
2019 public void testFlagIManifestE() throws Exception {
2020 installFromRawResource("install.apk", R.raw.install_loc_sdcard,
2021 PackageManager.INSTALL_INTERNAL,
2022 true,
2023 false, -1,
2024 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2025 }
2026 /*
2027 * Install an app with both internal and manifest preference for
2028 * auto. should install internal.
2029 */
2030 @LargeTest
2031 public void testFlagIManifestA() throws Exception {
2032 installFromRawResource("install.apk", R.raw.install_loc_auto,
2033 PackageManager.INSTALL_INTERNAL,
2034 true,
2035 false, -1,
2036 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2037 }
2038 /*
2039 * Install an app with both external and manifest option set.
2040 * should install externally.
2041 */
2042 @LargeTest
2043 public void testFlagEManifestI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002044 // Do not run on devices with emulated external storage.
2045 if (Environment.isExternalStorageEmulated()) {
2046 return;
2047 }
2048
Kenny Root25c925e2012-09-08 22:02:21 -07002049 installFromRawResource("install.apk", R.raw.install_loc_internal,
2050 PackageManager.INSTALL_EXTERNAL,
2051 true,
2052 false, -1,
2053 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2054 }
Kenny Root3c676892011-01-12 10:58:40 -08002055
Kenny Root25c925e2012-09-08 22:02:21 -07002056 /*
2057 * Install an app with both external and manifest preference for
2058 * preferExternal. Should install externally.
2059 */
2060 @LargeTest
2061 public void testFlagEManifestE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002062 // Do not run on devices with emulated external storage.
2063 if (Environment.isExternalStorageEmulated()) {
2064 return;
2065 }
2066
2067 installFromRawResource("install.apk", R.raw.install_loc_sdcard,
2068 PackageManager.INSTALL_EXTERNAL,
2069 true,
2070 false, -1,
2071 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2072 }
2073
2074 /*
2075 * Install an app with both external and manifest preference for
2076 * auto. should install on external media.
2077 */
2078 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002079 public void testFlagEManifestA() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002080 // Do not run on devices with emulated external storage.
2081 if (Environment.isExternalStorageEmulated()) {
2082 return;
2083 }
2084
2085 installFromRawResource("install.apk", R.raw.install_loc_auto,
2086 PackageManager.INSTALL_EXTERNAL,
2087 true,
2088 false, -1,
2089 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2090 }
2091
2092 /*
2093 * Install an app with fwd locked flag set and install location set to
2094 * internal. should install internally.
2095 */
2096 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002097 public void testFlagFManifestI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002098 installFromRawResource("install.apk", R.raw.install_loc_internal,
2099 PackageManager.INSTALL_FORWARD_LOCK,
2100 true,
2101 false, -1,
Kenny Root6dceb882012-04-12 14:23:49 -07002102 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
Kenny Root3c676892011-01-12 10:58:40 -08002103 }
2104
2105 /*
2106 * Install an app with fwd locked flag set and install location set to
Kenny Root6dceb882012-04-12 14:23:49 -07002107 * preferExternal. Should install externally.
Kenny Root3c676892011-01-12 10:58:40 -08002108 */
2109 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002110 public void testFlagFManifestE() throws Exception {
Kenny Root6dceb882012-04-12 14:23:49 -07002111 // Do not run on devices with emulated external storage.
2112 if (Environment.isExternalStorageEmulated()) {
2113 return;
2114 }
2115
Kenny Root3c676892011-01-12 10:58:40 -08002116 installFromRawResource("install.apk", R.raw.install_loc_sdcard,
2117 PackageManager.INSTALL_FORWARD_LOCK,
2118 true,
2119 false, -1,
2120 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2121 }
2122
2123 /*
Kenny Root6dceb882012-04-12 14:23:49 -07002124 * Install an app with fwd locked flag set and install location set to auto.
2125 * should install externally.
Kenny Root3c676892011-01-12 10:58:40 -08002126 */
2127 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002128 public void testFlagFManifestA() throws Exception {
Kenny Root6dceb882012-04-12 14:23:49 -07002129 // Do not run on devices with emulated external storage.
2130 if (Environment.isExternalStorageEmulated()) {
2131 return;
2132 }
2133
Kenny Root3c676892011-01-12 10:58:40 -08002134 installFromRawResource("install.apk", R.raw.install_loc_auto,
2135 PackageManager.INSTALL_FORWARD_LOCK,
2136 true,
2137 false, -1,
Kenny Rootbf023582012-05-02 16:56:15 -07002138 PackageInfo.INSTALL_LOCATION_AUTO);
Kenny Root3c676892011-01-12 10:58:40 -08002139 }
2140
Kenny Root25c925e2012-09-08 22:02:21 -07002141 /*
2142 * The following test functions verify install location for existing apps.
Kenny Root3c676892011-01-12 10:58:40 -08002143 * ie existing app can be installed internally or externally. If install
2144 * flag is explicitly set it should override current location. If manifest location
2145 * is set, that should over ride current location too. if not the existing install
2146 * location should be honoured.
2147 * testFlagI/E/F/ExistingI/E -
2148 */
2149 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002150 public void testFlagIExistingI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002151 int iFlags = PackageManager.INSTALL_INTERNAL;
2152 int rFlags = PackageManager.INSTALL_INTERNAL | PackageManager.INSTALL_REPLACE_EXISTING;
2153 // First install.
2154 installFromRawResource("install.apk", R.raw.install,
2155 iFlags,
2156 false,
2157 false, -1,
2158 -1);
2159 // Replace now
2160 installFromRawResource("install.apk", R.raw.install,
2161 rFlags,
2162 true,
2163 false, -1,
2164 -1);
2165 }
2166
2167 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002168 public void testFlagIExistingE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002169 // Do not run on devices with emulated external storage.
2170 if (Environment.isExternalStorageEmulated()) {
2171 return;
2172 }
2173
2174 int iFlags = PackageManager.INSTALL_EXTERNAL;
2175 int rFlags = PackageManager.INSTALL_INTERNAL | PackageManager.INSTALL_REPLACE_EXISTING;
2176 // First install.
2177 installFromRawResource("install.apk", R.raw.install,
2178 iFlags,
2179 false,
2180 false, -1,
2181 -1);
2182 // Replace now
2183 installFromRawResource("install.apk", R.raw.install,
2184 rFlags,
2185 true,
2186 false, -1,
2187 -1);
2188 }
2189
2190 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002191 public void testFlagEExistingI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002192 // Do not run on devices with emulated external storage.
2193 if (Environment.isExternalStorageEmulated()) {
2194 return;
2195 }
2196
2197 int iFlags = PackageManager.INSTALL_INTERNAL;
2198 int rFlags = PackageManager.INSTALL_EXTERNAL | PackageManager.INSTALL_REPLACE_EXISTING;
2199 // First install.
2200 installFromRawResource("install.apk", R.raw.install,
2201 iFlags,
2202 false,
2203 false, -1,
2204 -1);
2205 // Replace now
2206 installFromRawResource("install.apk", R.raw.install,
2207 rFlags,
2208 true,
2209 false, -1,
2210 -1);
2211 }
2212
2213 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002214 public void testFlagEExistingE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002215 // Do not run on devices with emulated external storage.
2216 if (Environment.isExternalStorageEmulated()) {
2217 return;
2218 }
2219
2220 int iFlags = PackageManager.INSTALL_EXTERNAL;
2221 int rFlags = PackageManager.INSTALL_EXTERNAL | PackageManager.INSTALL_REPLACE_EXISTING;
2222 // First install.
2223 installFromRawResource("install.apk", R.raw.install,
2224 iFlags,
2225 false,
2226 false, -1,
2227 -1);
2228 // Replace now
2229 installFromRawResource("install.apk", R.raw.install,
2230 rFlags,
2231 true,
2232 false, -1,
2233 -1);
2234 }
2235
2236 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002237 public void testFlagFExistingI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002238 int iFlags = PackageManager.INSTALL_INTERNAL;
2239 int rFlags = PackageManager.INSTALL_FORWARD_LOCK | PackageManager.INSTALL_REPLACE_EXISTING;
2240 // First install.
2241 installFromRawResource("install.apk", R.raw.install,
2242 iFlags,
2243 false,
2244 false, -1,
2245 -1);
2246 // Replace now
2247 installFromRawResource("install.apk", R.raw.install,
2248 rFlags,
2249 true,
2250 false, -1,
2251 -1);
2252 }
2253
2254 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002255 public void testFlagFExistingE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002256 // Do not run on devices with emulated external storage.
2257 if (Environment.isExternalStorageEmulated()) {
2258 return;
2259 }
2260
2261 int iFlags = PackageManager.INSTALL_EXTERNAL;
2262 int rFlags = PackageManager.INSTALL_FORWARD_LOCK | PackageManager.INSTALL_REPLACE_EXISTING;
2263 // First install.
2264 installFromRawResource("install.apk", R.raw.install,
2265 iFlags,
2266 false,
2267 false, -1,
2268 -1);
2269 // Replace now
2270 installFromRawResource("install.apk", R.raw.install,
2271 rFlags,
2272 true,
2273 false, -1,
2274 -1);
2275 }
2276
2277 /*
2278 * The following set of tests verify the installation of apps with
2279 * install location attribute set to internalOnly, preferExternal and auto.
2280 * The manifest option should dictate the install location.
2281 * public void testManifestI/E/A
2282 * TODO out of memory fall back behaviour.
2283 */
2284 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002285 public void testManifestI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002286 installFromRawResource("install.apk", R.raw.install_loc_internal,
2287 0,
2288 true,
2289 false, -1,
2290 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2291 }
2292
2293 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002294 public void testManifestE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002295 // Do not run on devices with emulated external storage.
2296 if (Environment.isExternalStorageEmulated()) {
2297 return;
2298 }
2299
2300 installFromRawResource("install.apk", R.raw.install_loc_sdcard,
2301 0,
2302 true,
2303 false, -1,
2304 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2305 }
2306
2307 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002308 public void testManifestA() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002309 installFromRawResource("install.apk", R.raw.install_loc_auto,
2310 0,
2311 true,
2312 false, -1,
2313 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2314 }
2315
2316 /*
2317 * The following set of tests verify the installation of apps
2318 * with install location attribute set to internalOnly, preferExternal and auto
2319 * for already existing apps. The manifest option should take precedence.
2320 * TODO add out of memory fall back behaviour.
2321 * testManifestI/E/AExistingI/E
2322 */
2323 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002324 public void testManifestIExistingI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002325 int iFlags = PackageManager.INSTALL_INTERNAL;
2326 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
2327 // First install.
2328 installFromRawResource("install.apk", R.raw.install,
2329 iFlags,
2330 false,
2331 false, -1,
2332 -1);
2333 // Replace now
2334 installFromRawResource("install.apk", R.raw.install_loc_internal,
2335 rFlags,
2336 true,
2337 false, -1,
2338 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2339 }
2340
2341 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002342 public void testManifestIExistingE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002343 // Do not run on devices with emulated external storage.
2344 if (Environment.isExternalStorageEmulated()) {
2345 return;
2346 }
2347
2348 int iFlags = PackageManager.INSTALL_EXTERNAL;
2349 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
2350 // First install.
2351 installFromRawResource("install.apk", R.raw.install,
2352 iFlags,
2353 false,
2354 false, -1,
2355 -1);
2356 // Replace now
2357 installFromRawResource("install.apk", R.raw.install_loc_internal,
2358 rFlags,
2359 true,
2360 false, -1,
2361 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2362 }
2363
2364 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002365 public void testManifestEExistingI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002366 // Do not run on devices with emulated external storage.
2367 if (Environment.isExternalStorageEmulated()) {
2368 return;
2369 }
2370
2371 int iFlags = PackageManager.INSTALL_INTERNAL;
2372 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
2373 // First install.
2374 installFromRawResource("install.apk", R.raw.install,
2375 iFlags,
2376 false,
2377 false, -1,
2378 -1);
2379 // Replace now
2380 installFromRawResource("install.apk", R.raw.install_loc_sdcard,
2381 rFlags,
2382 true,
2383 false, -1,
2384 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2385 }
2386
2387 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002388 public void testManifestEExistingE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002389 // Do not run on devices with emulated external storage.
2390 if (Environment.isExternalStorageEmulated()) {
2391 return;
2392 }
2393
2394 int iFlags = PackageManager.INSTALL_EXTERNAL;
2395 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
2396 // First install.
2397 installFromRawResource("install.apk", R.raw.install,
2398 iFlags,
2399 false,
2400 false, -1,
2401 -1);
2402 // Replace now
2403 installFromRawResource("install.apk", R.raw.install_loc_sdcard,
2404 rFlags,
2405 true,
2406 false, -1,
2407 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2408 }
2409
2410 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002411 public void testManifestAExistingI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002412 int iFlags = PackageManager.INSTALL_INTERNAL;
2413 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
2414 // First install.
2415 installFromRawResource("install.apk", R.raw.install,
2416 iFlags,
2417 false,
2418 false, -1,
2419 -1);
2420 // Replace now
2421 installFromRawResource("install.apk", R.raw.install_loc_auto,
2422 rFlags,
2423 true,
2424 false, -1,
2425 PackageInfo.INSTALL_LOCATION_AUTO);
2426 }
2427
2428 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002429 public void testManifestAExistingE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002430 // Do not run on devices with emulated external storage.
2431 if (Environment.isExternalStorageEmulated()) {
2432 return;
2433 }
2434
2435 int iFlags = PackageManager.INSTALL_EXTERNAL;
2436 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
2437 // First install.
2438 installFromRawResource("install.apk", R.raw.install,
2439 iFlags,
2440 false,
2441 false, -1,
2442 -1);
2443 // Replace now
2444 installFromRawResource("install.apk", R.raw.install_loc_auto,
2445 rFlags,
2446 true,
2447 false, -1,
2448 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2449 }
2450
Kenny Root25c925e2012-09-08 22:02:21 -07002451 /*
2452 * The following set of tests check install location for existing
2453 * application based on user setting.
2454 */
2455 private int getExpectedInstallLocation(int userSetting) {
2456 int iloc = PackageInfo.INSTALL_LOCATION_UNSPECIFIED;
2457 boolean enable = getUserSettingSetInstallLocation();
2458 if (enable) {
2459 if (userSetting == PackageHelper.APP_INSTALL_AUTO) {
2460 iloc = PackageInfo.INSTALL_LOCATION_AUTO;
2461 } else if (userSetting == PackageHelper.APP_INSTALL_EXTERNAL) {
2462 iloc = PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL;
2463 } else if (userSetting == PackageHelper.APP_INSTALL_INTERNAL) {
2464 iloc = PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY;
2465 }
2466 }
2467 return iloc;
2468 }
2469
2470 private void setExistingXUserX(int userSetting, int iFlags, int iloc) throws Exception {
2471 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
2472 // First install.
2473 installFromRawResource("install.apk", R.raw.install,
2474 iFlags,
2475 false,
2476 false, -1,
2477 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
2478 int origSetting = getDefaultInstallLoc();
2479 try {
2480 // Set user setting
2481 setInstallLoc(userSetting);
2482 // Replace now
2483 installFromRawResource("install.apk", R.raw.install,
2484 rFlags,
2485 true,
2486 false, -1,
2487 iloc);
2488 } finally {
2489 setInstallLoc(origSetting);
2490 }
2491 }
2492 @LargeTest
2493 public void testExistingIUserI() throws Exception {
2494 int userSetting = PackageHelper.APP_INSTALL_INTERNAL;
2495 int iFlags = PackageManager.INSTALL_INTERNAL;
2496 setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2497 }
Kenny Root3c676892011-01-12 10:58:40 -08002498
2499 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002500 public void testExistingIUserE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002501 // Do not run on devices with emulated external storage.
2502 if (Environment.isExternalStorageEmulated()) {
2503 return;
2504 }
2505
2506 int userSetting = PackageHelper.APP_INSTALL_EXTERNAL;
2507 int iFlags = PackageManager.INSTALL_INTERNAL;
2508 setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2509 }
2510
Kenny Root25c925e2012-09-08 22:02:21 -07002511 @LargeTest
2512 public void testExistingIUserA() throws Exception {
2513 int userSetting = PackageHelper.APP_INSTALL_AUTO;
2514 int iFlags = PackageManager.INSTALL_INTERNAL;
2515 setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2516 }
Kenny Root3c676892011-01-12 10:58:40 -08002517
2518 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002519 public void testExistingEUserI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002520 // Do not run on devices with emulated external storage.
2521 if (Environment.isExternalStorageEmulated()) {
2522 return;
2523 }
2524
2525 int userSetting = PackageHelper.APP_INSTALL_INTERNAL;
2526 int iFlags = PackageManager.INSTALL_EXTERNAL;
2527 setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2528 }
2529
2530 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002531 public void testExistingEUserE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002532 // Do not run on devices with emulated external storage.
2533 if (Environment.isExternalStorageEmulated()) {
2534 return;
2535 }
2536
2537 int userSetting = PackageHelper.APP_INSTALL_EXTERNAL;
2538 int iFlags = PackageManager.INSTALL_EXTERNAL;
2539 setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2540 }
2541
2542 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002543 public void testExistingEUserA() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002544 // Do not run on devices with emulated external storage.
2545 if (Environment.isExternalStorageEmulated()) {
2546 return;
2547 }
2548
2549 int userSetting = PackageHelper.APP_INSTALL_AUTO;
2550 int iFlags = PackageManager.INSTALL_EXTERNAL;
2551 setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2552 }
2553
Kenny Root25c925e2012-09-08 22:02:21 -07002554 /*
2555 * The following set of tests verify that the user setting defines
2556 * the install location.
2557 *
2558 */
2559 private boolean getUserSettingSetInstallLocation() {
2560 try {
Jeff Sharkey625239a2012-09-26 22:03:49 -07002561 return Settings.Global.getInt(
2562 mContext.getContentResolver(), Settings.Global.SET_INSTALL_LOCATION) != 0;
Kenny Root25c925e2012-09-08 22:02:21 -07002563 } catch (SettingNotFoundException e1) {
2564 }
2565 return false;
2566 }
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -07002567
Kenny Root25c925e2012-09-08 22:02:21 -07002568 private void setUserSettingSetInstallLocation(boolean value) {
Jeff Sharkey625239a2012-09-26 22:03:49 -07002569 Settings.Global.putInt(mContext.getContentResolver(),
2570 Settings.Global.SET_INSTALL_LOCATION, value ? 1 : 0);
Kenny Root25c925e2012-09-08 22:02:21 -07002571 }
2572
2573 private void setUserX(boolean enable, int userSetting, int iloc) throws Exception {
2574 boolean origUserSetting = getUserSettingSetInstallLocation();
2575 int origSetting = getDefaultInstallLoc();
2576 try {
2577 setUserSettingSetInstallLocation(enable);
2578 // Set user setting
2579 setInstallLoc(userSetting);
2580 // Replace now
2581 installFromRawResource("install.apk", R.raw.install,
2582 0,
2583 true,
2584 false, -1,
2585 iloc);
2586 } finally {
2587 // Restore original setting
2588 setUserSettingSetInstallLocation(origUserSetting);
2589 setInstallLoc(origSetting);
2590 }
2591 }
2592 @LargeTest
2593 public void testUserI() throws Exception {
2594 int userSetting = PackageHelper.APP_INSTALL_INTERNAL;
2595 int iloc = getExpectedInstallLocation(userSetting);
2596 setUserX(true, userSetting, iloc);
2597 }
Kenny Root3c676892011-01-12 10:58:40 -08002598
2599 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002600 public void testUserE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002601 // Do not run on devices with emulated external storage.
2602 if (Environment.isExternalStorageEmulated()) {
2603 return;
2604 }
2605
2606 int userSetting = PackageHelper.APP_INSTALL_EXTERNAL;
2607 int iloc = getExpectedInstallLocation(userSetting);
2608 setUserX(true, userSetting, iloc);
2609 }
2610
Kenny Root25c925e2012-09-08 22:02:21 -07002611 @LargeTest
2612 public void testUserA() throws Exception {
2613 int userSetting = PackageHelper.APP_INSTALL_AUTO;
2614 int iloc = getExpectedInstallLocation(userSetting);
2615 setUserX(true, userSetting, iloc);
2616 }
2617
2618 /*
2619 * The following set of tests turn on/off the basic
2620 * user setting for turning on install location.
2621 */
2622 @LargeTest
2623 public void testUserPrefOffUserI() throws Exception {
2624 int userSetting = PackageHelper.APP_INSTALL_INTERNAL;
2625 int iloc = PackageInfo.INSTALL_LOCATION_UNSPECIFIED;
2626 setUserX(false, userSetting, iloc);
2627 }
Kenny Root3c676892011-01-12 10:58:40 -08002628
2629 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002630 public void testUserPrefOffUserE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002631 // Do not run on devices with emulated external storage.
2632 if (Environment.isExternalStorageEmulated()) {
2633 return;
2634 }
2635
2636 int userSetting = PackageHelper.APP_INSTALL_EXTERNAL;
2637 int iloc = PackageInfo.INSTALL_LOCATION_UNSPECIFIED;
2638 setUserX(false, userSetting, iloc);
2639 }
2640
Kenny Root25c925e2012-09-08 22:02:21 -07002641 @LargeTest
2642 public void testUserPrefOffA() throws Exception {
2643 int userSetting = PackageHelper.APP_INSTALL_AUTO;
2644 int iloc = PackageInfo.INSTALL_LOCATION_UNSPECIFIED;
2645 setUserX(false, userSetting, iloc);
2646 }
Neal Nguyenedb979a2010-04-22 13:54:32 -07002647
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002648 static final String BASE_PERMISSIONS_DEFINED[] = new String[] {
2649 PERM_PACKAGE, "com.android.unit_tests.install_decl_perm",
2650 PERM_DEFINED,
Brett Chabot0dc59e72010-04-01 18:21:38 -07002651 "com.android.frameworks.coretests.NORMAL",
2652 "com.android.frameworks.coretests.DANGEROUS",
2653 "com.android.frameworks.coretests.SIGNATURE",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002654 };
Neal Nguyenedb979a2010-04-22 13:54:32 -07002655
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002656 static final String BASE_PERMISSIONS_UNDEFINED[] = new String[] {
Brett Chabot0dc59e72010-04-01 18:21:38 -07002657 PERM_PACKAGE, "com.android.frameworks.coretests.install_decl_perm",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002658 PERM_UNDEFINED,
Brett Chabot0dc59e72010-04-01 18:21:38 -07002659 "com.android.frameworks.coretests.NORMAL",
2660 "com.android.frameworks.coretests.DANGEROUS",
2661 "com.android.frameworks.coretests.SIGNATURE",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002662 };
Neal Nguyenedb979a2010-04-22 13:54:32 -07002663
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002664 static final String BASE_PERMISSIONS_USED[] = new String[] {
Brett Chabot0dc59e72010-04-01 18:21:38 -07002665 PERM_PACKAGE, "com.android.frameworks.coretests.install_use_perm_good",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002666 PERM_USED,
Brett Chabot0dc59e72010-04-01 18:21:38 -07002667 "com.android.frameworks.coretests.NORMAL",
2668 "com.android.frameworks.coretests.DANGEROUS",
2669 "com.android.frameworks.coretests.SIGNATURE",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002670 };
Neal Nguyenedb979a2010-04-22 13:54:32 -07002671
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002672 static final String BASE_PERMISSIONS_NOTUSED[] = new String[] {
Brett Chabot0dc59e72010-04-01 18:21:38 -07002673 PERM_PACKAGE, "com.android.frameworks.coretests.install_use_perm_good",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002674 PERM_NOTUSED,
Brett Chabot0dc59e72010-04-01 18:21:38 -07002675 "com.android.frameworks.coretests.NORMAL",
2676 "com.android.frameworks.coretests.DANGEROUS",
2677 "com.android.frameworks.coretests.SIGNATURE",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002678 };
Neal Nguyenedb979a2010-04-22 13:54:32 -07002679
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002680 static final String BASE_PERMISSIONS_SIGUSED[] = new String[] {
Brett Chabot0dc59e72010-04-01 18:21:38 -07002681 PERM_PACKAGE, "com.android.frameworks.coretests.install_use_perm_good",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002682 PERM_USED,
Brett Chabot0dc59e72010-04-01 18:21:38 -07002683 "com.android.frameworks.coretests.SIGNATURE",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002684 PERM_NOTUSED,
Brett Chabot0dc59e72010-04-01 18:21:38 -07002685 "com.android.frameworks.coretests.NORMAL",
2686 "com.android.frameworks.coretests.DANGEROUS",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002687 };
Neal Nguyenedb979a2010-04-22 13:54:32 -07002688
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002689 /*
2690 * Ensure that permissions are properly declared.
2691 */
Neal Nguyenedb979a2010-04-22 13:54:32 -07002692 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002693 public void testInstallDeclaresPermissions() throws Exception {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002694 InstallParams ip = null;
2695 InstallParams ip2 = null;
2696 try {
2697 // **: Upon installing a package, are its declared permissions published?
Neal Nguyenedb979a2010-04-22 13:54:32 -07002698
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002699 int iFlags = PackageManager.INSTALL_INTERNAL;
2700 int iApk = R.raw.install_decl_perm;
2701 ip = installFromRawResource("install.apk", iApk,
2702 iFlags, false,
2703 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2704 assertInstall(ip.pkg, iFlags, ip.pkg.installLocation);
2705 assertPermissions(BASE_PERMISSIONS_DEFINED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002706
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002707 // **: Upon installing package, are its permissions granted?
Neal Nguyenedb979a2010-04-22 13:54:32 -07002708
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002709 int i2Flags = PackageManager.INSTALL_INTERNAL;
2710 int i2Apk = R.raw.install_use_perm_good;
2711 ip2 = installFromRawResource("install2.apk", i2Apk,
2712 i2Flags, false,
2713 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2714 assertInstall(ip2.pkg, i2Flags, ip2.pkg.installLocation);
2715 assertPermissions(BASE_PERMISSIONS_USED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002716
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002717 // **: Upon removing but not deleting, are permissions retained?
Neal Nguyenedb979a2010-04-22 13:54:32 -07002718
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002719 GenericReceiver receiver = new DeleteReceiver(ip.pkg.packageName);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002720
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002721 try {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07002722 invokeDeletePackage(ip.pkg.packageName, PackageManager.DELETE_KEEP_DATA, receiver);
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002723 } catch (Exception e) {
2724 failStr(e);
2725 }
2726 assertPermissions(BASE_PERMISSIONS_DEFINED);
2727 assertPermissions(BASE_PERMISSIONS_USED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002728
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002729 // **: Upon re-installing, are permissions retained?
Neal Nguyenedb979a2010-04-22 13:54:32 -07002730
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002731 ip = installFromRawResource("install.apk", iApk,
2732 iFlags | PackageManager.INSTALL_REPLACE_EXISTING, false,
2733 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2734 assertInstall(ip.pkg, iFlags, ip.pkg.installLocation);
2735 assertPermissions(BASE_PERMISSIONS_DEFINED);
2736 assertPermissions(BASE_PERMISSIONS_USED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002737
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002738 // **: Upon deleting package, are all permissions removed?
Neal Nguyenedb979a2010-04-22 13:54:32 -07002739
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002740 try {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07002741 invokeDeletePackage(ip.pkg.packageName, 0, receiver);
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002742 ip = null;
2743 } catch (Exception e) {
2744 failStr(e);
2745 }
2746 assertPermissions(BASE_PERMISSIONS_UNDEFINED);
2747 assertPermissions(BASE_PERMISSIONS_NOTUSED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002748
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002749 // **: Delete package using permissions; nothing to check here.
Neal Nguyenedb979a2010-04-22 13:54:32 -07002750
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002751 GenericReceiver receiver2 = new DeleteReceiver(ip2.pkg.packageName);
2752 try {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07002753 invokeDeletePackage(ip2.pkg.packageName, 0, receiver);
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002754 ip2 = null;
2755 } catch (Exception e) {
2756 failStr(e);
2757 }
Neal Nguyenedb979a2010-04-22 13:54:32 -07002758
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002759 // **: Re-install package using permissions; no permissions can be granted.
Neal Nguyenedb979a2010-04-22 13:54:32 -07002760
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002761 ip2 = installFromRawResource("install2.apk", i2Apk,
2762 i2Flags, false,
2763 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2764 assertInstall(ip2.pkg, i2Flags, ip2.pkg.installLocation);
2765 assertPermissions(BASE_PERMISSIONS_NOTUSED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002766
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002767 // **: Upon installing declaring package, are sig permissions granted
2768 // to other apps (but not other perms)?
Neal Nguyenedb979a2010-04-22 13:54:32 -07002769
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002770 ip = installFromRawResource("install.apk", iApk,
2771 iFlags, false,
2772 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2773 assertInstall(ip.pkg, iFlags, ip.pkg.installLocation);
2774 assertPermissions(BASE_PERMISSIONS_DEFINED);
2775 assertPermissions(BASE_PERMISSIONS_SIGUSED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002776
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002777 // **: Re-install package using permissions; are all permissions granted?
Neal Nguyenedb979a2010-04-22 13:54:32 -07002778
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002779 ip2 = installFromRawResource("install2.apk", i2Apk,
2780 i2Flags | PackageManager.INSTALL_REPLACE_EXISTING, false,
2781 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2782 assertInstall(ip2.pkg, i2Flags, ip2.pkg.installLocation);
2783 assertPermissions(BASE_PERMISSIONS_NOTUSED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002784
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002785 // **: Upon deleting package, are all permissions removed?
Neal Nguyenedb979a2010-04-22 13:54:32 -07002786
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002787 try {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07002788 invokeDeletePackage(ip.pkg.packageName, 0, receiver);
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002789 ip = null;
2790 } catch (Exception e) {
2791 failStr(e);
2792 }
2793 assertPermissions(BASE_PERMISSIONS_UNDEFINED);
2794 assertPermissions(BASE_PERMISSIONS_NOTUSED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002795
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002796 // **: Delete package using permissions; nothing to check here.
Neal Nguyenedb979a2010-04-22 13:54:32 -07002797
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002798 try {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07002799 invokeDeletePackage(ip2.pkg.packageName, 0, receiver);
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002800 ip2 = null;
2801 } catch (Exception e) {
2802 failStr(e);
2803 }
Neal Nguyenedb979a2010-04-22 13:54:32 -07002804
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002805 } finally {
2806 if (ip2 != null) {
2807 cleanUpInstall(ip2);
2808 }
2809 if (ip != null) {
2810 cleanUpInstall(ip);
2811 }
2812 }
2813 }
2814
Suchi Amalapurapuebb83ad2010-03-19 11:55:29 -07002815 /*
2816 * Ensure that permissions are properly declared.
2817 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07002818 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002819 public void testInstallOnSdPermissionsUnmount() throws Exception {
Suchi Amalapurapuebb83ad2010-03-19 11:55:29 -07002820 InstallParams ip = null;
Kenny Rootd7b421b2010-08-05 08:40:00 -07002821 boolean origMediaState = checkMediaState(Environment.MEDIA_MOUNTED);
Suchi Amalapurapuebb83ad2010-03-19 11:55:29 -07002822 try {
2823 // **: Upon installing a package, are its declared permissions published?
2824 int iFlags = PackageManager.INSTALL_INTERNAL;
2825 int iApk = R.raw.install_decl_perm;
2826 ip = installFromRawResource("install.apk", iApk,
2827 iFlags, false,
2828 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2829 assertInstall(ip.pkg, iFlags, ip.pkg.installLocation);
2830 assertPermissions(BASE_PERMISSIONS_DEFINED);
2831 // Unmount media here
2832 assertTrue(unmountMedia());
2833 // Mount media again
2834 mountMedia();
2835 //Check permissions now
2836 assertPermissions(BASE_PERMISSIONS_DEFINED);
2837 } finally {
2838 if (ip != null) {
2839 cleanUpInstall(ip);
2840 }
2841 }
2842 }
Suchi Amalapurapuc7537ee2010-03-24 09:27:19 -07002843
2844 /* This test creates a stale container via MountService and then installs
2845 * a package and verifies that the stale container is cleaned up and install
2846 * is successful.
2847 * Please note that this test is very closely tied to the framework's
2848 * naming convention for secure containers.
2849 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07002850 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002851 public void testInstallSdcardStaleContainer() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002852 // Do not run on devices with emulated external storage.
2853 if (Environment.isExternalStorageEmulated()) {
2854 return;
2855 }
2856
Kenny Rootd7b421b2010-08-05 08:40:00 -07002857 boolean origMediaState = checkMediaState(Environment.MEDIA_MOUNTED);
Suchi Amalapurapuc7537ee2010-03-24 09:27:19 -07002858 try {
Kenny Rootd7b421b2010-08-05 08:40:00 -07002859 // Mount media first
2860 mountMedia();
Suchi Amalapurapuc7537ee2010-03-24 09:27:19 -07002861 String outFileName = "install.apk";
2862 int rawResId = R.raw.install;
2863 PackageManager pm = mContext.getPackageManager();
2864 File filesDir = mContext.getFilesDir();
2865 File outFile = new File(filesDir, outFileName);
2866 Uri packageURI = getInstallablePackage(rawResId, outFile);
2867 PackageParser.Package pkg = parsePackage(packageURI);
2868 assertNotNull(pkg);
2869 // Install an app on sdcard.
2870 installFromRawResource(outFileName, rawResId,
2871 PackageManager.INSTALL_EXTERNAL, false,
2872 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
2873 // Unmount sdcard
2874 unmountMedia();
2875 // Delete the app on sdcard to leave a stale container on sdcard.
2876 GenericReceiver receiver = new DeleteReceiver(pkg.packageName);
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07002877 assertTrue(invokeDeletePackage(pkg.packageName, 0, receiver));
Suchi Amalapurapuc7537ee2010-03-24 09:27:19 -07002878 mountMedia();
2879 // Reinstall the app and make sure it gets installed.
2880 installFromRawResource(outFileName, rawResId,
2881 PackageManager.INSTALL_EXTERNAL, true,
2882 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002883 } catch (Exception e) {
Suchi Amalapurapuc7537ee2010-03-24 09:27:19 -07002884 failStr(e.getMessage());
2885 } finally {
2886 if (origMediaState) {
2887 mountMedia();
2888 } else {
2889 unmountMedia();
2890 }
2891
2892 }
2893 }
Suchi Amalapurapu1ace5bc2010-05-13 12:05:53 -07002894
2895 /* This test installs an application on sdcard and unmounts media.
2896 * The app is then re-installed on internal storage. The sdcard is mounted
2897 * and verified that the re-installation on internal storage takes precedence.
2898 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07002899 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002900 public void testInstallSdcardStaleContainerReinstall() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002901 // Do not run on devices with emulated external storage.
2902 if (Environment.isExternalStorageEmulated()) {
2903 return;
2904 }
2905
Kenny Rootd7b421b2010-08-05 08:40:00 -07002906 boolean origMediaState = checkMediaState(Environment.MEDIA_MOUNTED);
Suchi Amalapurapu1ace5bc2010-05-13 12:05:53 -07002907 try {
2908 // Mount media first
2909 mountMedia();
2910 String outFileName = "install.apk";
2911 int rawResId = R.raw.install;
2912 PackageManager pm = mContext.getPackageManager();
2913 File filesDir = mContext.getFilesDir();
2914 File outFile = new File(filesDir, outFileName);
2915 Uri packageURI = getInstallablePackage(rawResId, outFile);
2916 PackageParser.Package pkg = parsePackage(packageURI);
2917 assertNotNull(pkg);
2918 // Install an app on sdcard.
2919 installFromRawResource(outFileName, rawResId,
2920 PackageManager.INSTALL_EXTERNAL, false,
2921 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
2922 // Unmount sdcard
2923 unmountMedia();
2924 // Reinstall the app and make sure it gets installed on internal storage.
2925 installFromRawResource(outFileName, rawResId,
2926 PackageManager.INSTALL_REPLACE_EXISTING, false,
2927 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
2928 mountMedia();
2929 // Verify that the app installed is on internal storage.
Kenny Root25c925e2012-09-08 22:02:21 -07002930 assertInstall(pkg, 0, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
Suchi Amalapurapu1ace5bc2010-05-13 12:05:53 -07002931 } catch (Exception e) {
2932 failStr(e.getMessage());
2933 } finally {
2934 if (origMediaState) {
2935 mountMedia();
2936 } else {
2937 unmountMedia();
2938 }
Suchi Amalapurapu1ace5bc2010-05-13 12:05:53 -07002939 }
2940 }
Kenny Root3c676892011-01-12 10:58:40 -08002941
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002942 /*
2943 * The following series of tests are related to upgrading apps with
Neal Nguyenedb979a2010-04-22 13:54:32 -07002944 * different certificates.
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002945 */
2946 private int APP1_UNSIGNED = R.raw.install_app1_unsigned;
Kenny Root25c925e2012-09-08 22:02:21 -07002947
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002948 private int APP1_CERT1 = R.raw.install_app1_cert1;
Kenny Root25c925e2012-09-08 22:02:21 -07002949
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002950 private int APP1_CERT2 = R.raw.install_app1_cert2;
Kenny Root25c925e2012-09-08 22:02:21 -07002951
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002952 private int APP1_CERT1_CERT2 = R.raw.install_app1_cert1_cert2;
Kenny Root25c925e2012-09-08 22:02:21 -07002953
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002954 private int APP1_CERT3_CERT4 = R.raw.install_app1_cert3_cert4;
Kenny Root25c925e2012-09-08 22:02:21 -07002955
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002956 private int APP1_CERT3 = R.raw.install_app1_cert3;
Kenny Root25c925e2012-09-08 22:02:21 -07002957
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002958 private int APP2_UNSIGNED = R.raw.install_app2_unsigned;
Kenny Root25c925e2012-09-08 22:02:21 -07002959
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002960 private int APP2_CERT1 = R.raw.install_app2_cert1;
Kenny Root25c925e2012-09-08 22:02:21 -07002961
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002962 private int APP2_CERT2 = R.raw.install_app2_cert2;
Kenny Root25c925e2012-09-08 22:02:21 -07002963
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002964 private int APP2_CERT1_CERT2 = R.raw.install_app2_cert1_cert2;
Kenny Root25c925e2012-09-08 22:02:21 -07002965
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002966 private int APP2_CERT3 = R.raw.install_app2_cert3;
2967
Kenny Root25c925e2012-09-08 22:02:21 -07002968 private InstallParams replaceCerts(int apk1, int apk2, boolean cleanUp, boolean fail,
2969 int retCode) throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002970 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
2971 String apk1Name = "install1.apk";
2972 String apk2Name = "install2.apk";
2973 PackageParser.Package pkg1 = getParsedPackage(apk1Name, apk1);
2974 try {
2975 InstallParams ip = installFromRawResource(apk1Name, apk1, 0, false,
2976 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
2977 installFromRawResource(apk2Name, apk2, rFlags, false,
2978 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
2979 return ip;
2980 } catch (Exception e) {
2981 failStr(e.getMessage());
2982 } finally {
2983 if (cleanUp) {
2984 cleanUpInstall(pkg1.packageName);
2985 }
2986 }
2987 return null;
2988 }
Kenny Root25c925e2012-09-08 22:02:21 -07002989
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002990 /*
2991 * Test that an app signed with two certificates can be upgraded by the
2992 * same app signed with two certificates.
2993 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07002994 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002995 public void testReplaceMatchAllCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002996 replaceCerts(APP1_CERT1_CERT2, APP1_CERT1_CERT2, true, false, -1);
2997 }
2998
2999 /*
3000 * Test that an app signed with two certificates cannot be upgraded
3001 * by an app signed with a different certificate.
3002 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07003003 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003004 public void testReplaceMatchNoCerts1() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003005 replaceCerts(APP1_CERT1_CERT2, APP1_CERT3, true, true,
3006 PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES);
3007 }
Kenny Root25c925e2012-09-08 22:02:21 -07003008
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003009 /*
3010 * Test that an app signed with two certificates cannot be upgraded
3011 * by an app signed with a different certificate.
3012 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07003013 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003014 public void testReplaceMatchNoCerts2() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003015 replaceCerts(APP1_CERT1_CERT2, APP1_CERT3_CERT4, true, true,
3016 PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES);
3017 }
Kenny Root25c925e2012-09-08 22:02:21 -07003018
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003019 /*
3020 * Test that an app signed with two certificates cannot be upgraded by
3021 * an app signed with a subset of initial certificates.
3022 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07003023 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003024 public void testReplaceMatchSomeCerts1() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003025 replaceCerts(APP1_CERT1_CERT2, APP1_CERT1, true, true,
3026 PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES);
3027 }
Kenny Root25c925e2012-09-08 22:02:21 -07003028
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003029 /*
3030 * Test that an app signed with two certificates cannot be upgraded by
3031 * an app signed with the last certificate.
3032 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07003033 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003034 public void testReplaceMatchSomeCerts2() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003035 replaceCerts(APP1_CERT1_CERT2, APP1_CERT2, true, true,
3036 PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES);
3037 }
Kenny Root25c925e2012-09-08 22:02:21 -07003038
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003039 /*
3040 * Test that an app signed with a certificate can be upgraded by app
3041 * signed with a superset of certificates.
3042 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07003043 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003044 public void testReplaceMatchMoreCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003045 replaceCerts(APP1_CERT1, APP1_CERT1_CERT2, true, true,
3046 PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES);
3047 }
Kenny Root25c925e2012-09-08 22:02:21 -07003048
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003049 /*
3050 * Test that an app signed with a certificate can be upgraded by app
3051 * signed with a superset of certificates. Then verify that the an app
3052 * signed with the original set of certs cannot upgrade the new one.
3053 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07003054 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003055 public void testReplaceMatchMoreCertsReplaceSomeCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003056 InstallParams ip = replaceCerts(APP1_CERT1, APP1_CERT1_CERT2, false, true,
3057 PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES);
3058 try {
3059 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
3060 installFromRawResource("install.apk", APP1_CERT1, rFlags, false,
3061 false, -1,
3062 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3063 } catch (Exception e) {
3064 failStr(e.getMessage());
3065 } finally {
3066 if (ip != null) {
3067 cleanUpInstall(ip);
3068 }
3069 }
3070 }
Kenny Root25c925e2012-09-08 22:02:21 -07003071
3072 /**
3073 * The following tests are related to testing the checkSignatures api.
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003074 */
Kenny Root25c925e2012-09-08 22:02:21 -07003075 private void checkSignatures(int apk1, int apk2, int expMatchResult) throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003076 checkSharedSignatures(apk1, apk2, true, false, -1, expMatchResult);
3077 }
Kenny Root25c925e2012-09-08 22:02:21 -07003078
Brett Chabotf76c56b2010-07-26 17:28:17 -07003079 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003080 public void testCheckSignaturesAllMatch() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003081 int apk1 = APP1_CERT1_CERT2;
3082 int apk2 = APP2_CERT1_CERT2;
3083 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_MATCH);
3084 }
Kenny Root25c925e2012-09-08 22:02:21 -07003085
Brett Chabotf76c56b2010-07-26 17:28:17 -07003086 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003087 public void testCheckSignaturesNoMatch() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003088 int apk1 = APP1_CERT1;
3089 int apk2 = APP2_CERT2;
3090 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_NO_MATCH);
3091 }
Kenny Root25c925e2012-09-08 22:02:21 -07003092
Brett Chabotf76c56b2010-07-26 17:28:17 -07003093 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003094 public void testCheckSignaturesSomeMatch1() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003095 int apk1 = APP1_CERT1_CERT2;
3096 int apk2 = APP2_CERT1;
3097 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_NO_MATCH);
3098 }
Kenny Root25c925e2012-09-08 22:02:21 -07003099
Brett Chabotf76c56b2010-07-26 17:28:17 -07003100 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003101 public void testCheckSignaturesSomeMatch2() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003102 int apk1 = APP1_CERT1_CERT2;
3103 int apk2 = APP2_CERT2;
3104 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_NO_MATCH);
3105 }
Kenny Root25c925e2012-09-08 22:02:21 -07003106
Brett Chabotf76c56b2010-07-26 17:28:17 -07003107 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003108 public void testCheckSignaturesMoreMatch() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003109 int apk1 = APP1_CERT1;
3110 int apk2 = APP2_CERT1_CERT2;
3111 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_NO_MATCH);
3112 }
Kenny Root25c925e2012-09-08 22:02:21 -07003113
Brett Chabotf76c56b2010-07-26 17:28:17 -07003114 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003115 public void testCheckSignaturesUnknown() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003116 int apk1 = APP1_CERT1_CERT2;
3117 int apk2 = APP2_CERT1_CERT2;
3118 String apk1Name = "install1.apk";
3119 String apk2Name = "install2.apk";
3120 InstallParams ip1 = null;
3121
3122 try {
3123 ip1 = installFromRawResource(apk1Name, apk1, 0, false,
3124 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3125 PackageManager pm = mContext.getPackageManager();
3126 // Delete app2
3127 File filesDir = mContext.getFilesDir();
3128 File outFile = new File(filesDir, apk2Name);
3129 int rawResId = apk2;
3130 Uri packageURI = getInstallablePackage(rawResId, outFile);
3131 PackageParser.Package pkg = parsePackage(packageURI);
Kenny Roota3e90792012-10-18 10:58:36 -07003132 getPm().deletePackage(pkg.packageName, null, PackageManager.DELETE_ALL_USERS);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003133 // Check signatures now
3134 int match = mContext.getPackageManager().checkSignatures(
3135 ip1.pkg.packageName, pkg.packageName);
3136 assertEquals(PackageManager.SIGNATURE_UNKNOWN_PACKAGE, match);
3137 } finally {
3138 if (ip1 != null) {
3139 cleanUpInstall(ip1);
3140 }
3141 }
3142 }
Kenny Root25c925e2012-09-08 22:02:21 -07003143
Brett Chabotf76c56b2010-07-26 17:28:17 -07003144 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003145 public void testInstallNoCertificates() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003146 int apk1 = APP1_UNSIGNED;
3147 String apk1Name = "install1.apk";
3148 InstallParams ip1 = null;
3149
3150 try {
3151 installFromRawResource(apk1Name, apk1, 0, false,
3152 true, PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES,
3153 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3154 } finally {
3155 }
3156 }
Kenny Root25c925e2012-09-08 22:02:21 -07003157
3158 /*
3159 * The following tests are related to apps using shared uids signed with
3160 * different certs.
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003161 */
3162 private int SHARED1_UNSIGNED = R.raw.install_shared1_unsigned;
Kenny Root25c925e2012-09-08 22:02:21 -07003163
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003164 private int SHARED1_CERT1 = R.raw.install_shared1_cert1;
Kenny Root25c925e2012-09-08 22:02:21 -07003165
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003166 private int SHARED1_CERT2 = R.raw.install_shared1_cert2;
Kenny Root25c925e2012-09-08 22:02:21 -07003167
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003168 private int SHARED1_CERT1_CERT2 = R.raw.install_shared1_cert1_cert2;
Kenny Root25c925e2012-09-08 22:02:21 -07003169
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003170 private int SHARED2_UNSIGNED = R.raw.install_shared2_unsigned;
Kenny Root25c925e2012-09-08 22:02:21 -07003171
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003172 private int SHARED2_CERT1 = R.raw.install_shared2_cert1;
Kenny Root25c925e2012-09-08 22:02:21 -07003173
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003174 private int SHARED2_CERT2 = R.raw.install_shared2_cert2;
Kenny Root25c925e2012-09-08 22:02:21 -07003175
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003176 private int SHARED2_CERT1_CERT2 = R.raw.install_shared2_cert1_cert2;
Kenny Root25c925e2012-09-08 22:02:21 -07003177
3178 private void checkSharedSignatures(int apk1, int apk2, boolean cleanUp, boolean fail,
3179 int retCode, int expMatchResult) throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003180 String apk1Name = "install1.apk";
3181 String apk2Name = "install2.apk";
3182 PackageParser.Package pkg1 = getParsedPackage(apk1Name, apk1);
3183 PackageParser.Package pkg2 = getParsedPackage(apk2Name, apk2);
3184
3185 try {
3186 // Clean up before testing first.
3187 cleanUpInstall(pkg1.packageName);
3188 cleanUpInstall(pkg2.packageName);
Kenny Root25c925e2012-09-08 22:02:21 -07003189 installFromRawResource(apk1Name, apk1, 0, false, false, -1,
3190 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003191 if (fail) {
Kenny Root25c925e2012-09-08 22:02:21 -07003192 installFromRawResource(apk2Name, apk2, 0, false, true, retCode,
3193 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003194 } else {
Kenny Root25c925e2012-09-08 22:02:21 -07003195 installFromRawResource(apk2Name, apk2, 0, false, false, -1,
3196 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3197 int match = mContext.getPackageManager().checkSignatures(pkg1.packageName,
3198 pkg2.packageName);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003199 assertEquals(expMatchResult, match);
3200 }
3201 } finally {
3202 if (cleanUp) {
3203 cleanUpInstall(pkg1.packageName);
3204 cleanUpInstall(pkg2.packageName);
3205 }
3206 }
3207 }
Kenny Root25c925e2012-09-08 22:02:21 -07003208
Brett Chabotf76c56b2010-07-26 17:28:17 -07003209 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003210 public void testCheckSignaturesSharedAllMatch() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003211 int apk1 = SHARED1_CERT1_CERT2;
3212 int apk2 = SHARED2_CERT1_CERT2;
3213 boolean fail = false;
3214 int retCode = -1;
3215 int expMatchResult = PackageManager.SIGNATURE_MATCH;
3216 checkSharedSignatures(apk1, apk2, true, fail, retCode, expMatchResult);
3217 }
Kenny Root25c925e2012-09-08 22:02:21 -07003218
Brett Chabotf76c56b2010-07-26 17:28:17 -07003219 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003220 public void testCheckSignaturesSharedNoMatch() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003221 int apk1 = SHARED1_CERT1;
3222 int apk2 = SHARED2_CERT2;
3223 boolean fail = true;
3224 int retCode = PackageManager.INSTALL_FAILED_SHARED_USER_INCOMPATIBLE;
3225 int expMatchResult = -1;
3226 checkSharedSignatures(apk1, apk2, true, fail, retCode, expMatchResult);
3227 }
Kenny Root25c925e2012-09-08 22:02:21 -07003228
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003229 /*
Kenny Root25c925e2012-09-08 22:02:21 -07003230 * Test that an app signed with cert1 and cert2 cannot be replaced when
3231 * signed with cert1 alone.
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003232 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07003233 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003234 public void testCheckSignaturesSharedSomeMatch1() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003235 int apk1 = SHARED1_CERT1_CERT2;
3236 int apk2 = SHARED2_CERT1;
3237 boolean fail = true;
3238 int retCode = PackageManager.INSTALL_FAILED_SHARED_USER_INCOMPATIBLE;
3239 int expMatchResult = -1;
3240 checkSharedSignatures(apk1, apk2, true, fail, retCode, expMatchResult);
3241 }
Kenny Root25c925e2012-09-08 22:02:21 -07003242
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003243 /*
Kenny Root25c925e2012-09-08 22:02:21 -07003244 * Test that an app signed with cert1 and cert2 cannot be replaced when
3245 * signed with cert2 alone.
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003246 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07003247 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003248 public void testCheckSignaturesSharedSomeMatch2() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003249 int apk1 = SHARED1_CERT1_CERT2;
3250 int apk2 = SHARED2_CERT2;
3251 boolean fail = true;
3252 int retCode = PackageManager.INSTALL_FAILED_SHARED_USER_INCOMPATIBLE;
3253 int expMatchResult = -1;
3254 checkSharedSignatures(apk1, apk2, true, fail, retCode, expMatchResult);
3255 }
Kenny Root25c925e2012-09-08 22:02:21 -07003256
Brett Chabotf76c56b2010-07-26 17:28:17 -07003257 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003258 public void testCheckSignaturesSharedUnknown() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003259 int apk1 = SHARED1_CERT1_CERT2;
3260 int apk2 = SHARED2_CERT1_CERT2;
3261 String apk1Name = "install1.apk";
3262 String apk2Name = "install2.apk";
3263 InstallParams ip1 = null;
3264
3265 try {
3266 ip1 = installFromRawResource(apk1Name, apk1, 0, false,
3267 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3268 PackageManager pm = mContext.getPackageManager();
3269 // Delete app2
3270 PackageParser.Package pkg = getParsedPackage(apk2Name, apk2);
Kenny Roota3e90792012-10-18 10:58:36 -07003271 getPm().deletePackage(pkg.packageName, null, PackageManager.DELETE_ALL_USERS);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003272 // Check signatures now
3273 int match = mContext.getPackageManager().checkSignatures(
3274 ip1.pkg.packageName, pkg.packageName);
3275 assertEquals(PackageManager.SIGNATURE_UNKNOWN_PACKAGE, match);
3276 } finally {
3277 if (ip1 != null) {
3278 cleanUpInstall(ip1);
3279 }
3280 }
3281 }
Neal Nguyenedb979a2010-04-22 13:54:32 -07003282
Brett Chabotf76c56b2010-07-26 17:28:17 -07003283 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003284 public void testReplaceFirstSharedMatchAllCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003285 int apk1 = SHARED1_CERT1;
3286 int apk2 = SHARED2_CERT1;
3287 int rapk1 = SHARED1_CERT1;
3288 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_MATCH);
3289 replaceCerts(apk1, rapk1, true, false, -1);
3290 }
Kenny Root25c925e2012-09-08 22:02:21 -07003291
Brett Chabotf76c56b2010-07-26 17:28:17 -07003292 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003293 public void testReplaceSecondSharedMatchAllCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003294 int apk1 = SHARED1_CERT1;
3295 int apk2 = SHARED2_CERT1;
3296 int rapk2 = SHARED2_CERT1;
3297 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_MATCH);
3298 replaceCerts(apk2, rapk2, true, false, -1);
3299 }
Kenny Root25c925e2012-09-08 22:02:21 -07003300
Brett Chabotf76c56b2010-07-26 17:28:17 -07003301 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003302 public void testReplaceFirstSharedMatchSomeCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003303 int apk1 = SHARED1_CERT1_CERT2;
3304 int apk2 = SHARED2_CERT1_CERT2;
3305 int rapk1 = SHARED1_CERT1;
3306 boolean fail = true;
3307 int retCode = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES;
3308 checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH);
3309 installFromRawResource("install.apk", rapk1, PackageManager.INSTALL_REPLACE_EXISTING, true,
3310 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3311 }
Kenny Root25c925e2012-09-08 22:02:21 -07003312
Brett Chabotf76c56b2010-07-26 17:28:17 -07003313 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003314 public void testReplaceSecondSharedMatchSomeCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003315 int apk1 = SHARED1_CERT1_CERT2;
3316 int apk2 = SHARED2_CERT1_CERT2;
3317 int rapk2 = SHARED2_CERT1;
3318 boolean fail = true;
3319 int retCode = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES;
3320 checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH);
3321 installFromRawResource("install.apk", rapk2, PackageManager.INSTALL_REPLACE_EXISTING, true,
3322 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3323 }
Kenny Root25c925e2012-09-08 22:02:21 -07003324
Brett Chabotf76c56b2010-07-26 17:28:17 -07003325 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003326 public void testReplaceFirstSharedMatchNoCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003327 int apk1 = SHARED1_CERT1;
3328 int apk2 = SHARED2_CERT1;
3329 int rapk1 = SHARED1_CERT2;
3330 boolean fail = true;
3331 int retCode = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES;
3332 checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH);
3333 installFromRawResource("install.apk", rapk1, PackageManager.INSTALL_REPLACE_EXISTING, true,
3334 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
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 testReplaceSecondSharedMatchNoCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003339 int apk1 = SHARED1_CERT1;
3340 int apk2 = SHARED2_CERT1;
3341 int rapk2 = SHARED2_CERT2;
3342 boolean fail = true;
3343 int retCode = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES;
3344 checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH);
3345 installFromRawResource("install.apk", rapk2, PackageManager.INSTALL_REPLACE_EXISTING, true,
3346 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3347 }
Kenny Root25c925e2012-09-08 22:02:21 -07003348
Brett Chabotf76c56b2010-07-26 17:28:17 -07003349 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003350 public void testReplaceFirstSharedMatchMoreCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003351 int apk1 = SHARED1_CERT1;
3352 int apk2 = SHARED2_CERT1;
3353 int rapk1 = SHARED1_CERT1_CERT2;
3354 boolean fail = true;
3355 int retCode = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES;
3356 checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH);
3357 installFromRawResource("install.apk", rapk1, PackageManager.INSTALL_REPLACE_EXISTING, true,
3358 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3359 }
Kenny Root25c925e2012-09-08 22:02:21 -07003360
Brett Chabotf76c56b2010-07-26 17:28:17 -07003361 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003362 public void testReplaceSecondSharedMatchMoreCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003363 int apk1 = SHARED1_CERT1;
3364 int apk2 = SHARED2_CERT1;
3365 int rapk2 = SHARED2_CERT1_CERT2;
3366 boolean fail = true;
3367 int retCode = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES;
3368 checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH);
3369 installFromRawResource("install.apk", rapk2, PackageManager.INSTALL_REPLACE_EXISTING, true,
3370 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3371 }
Kenny Root1683afa2011-01-07 14:27:50 -08003372
Kenny Root4c3915a2011-01-31 16:36:32 -08003373 /**
3374 * Unknown features should be allowed to install. This prevents older phones
3375 * from rejecting new packages that specify features that didn't exist when
3376 * an older phone existed. All older phones are assumed to have those
3377 * features.
3378 * <p>
3379 * Right now we allow all packages to be installed regardless of their
3380 * features.
3381 */
Kenny Root1683afa2011-01-07 14:27:50 -08003382 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003383 public void testUsesFeatureUnknownFeature() throws Exception {
Kenny Root4c3915a2011-01-31 16:36:32 -08003384 int retCode = PackageManager.INSTALL_SUCCEEDED;
3385 installFromRawResource("install.apk", R.raw.install_uses_feature, 0, true, false, retCode,
Kenny Root1683afa2011-01-07 14:27:50 -08003386 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3387 }
Kenny Root3c676892011-01-12 10:58:40 -08003388
Kenny Root1ebd74a2011-08-03 15:09:44 -07003389 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003390 public void testInstallNonexistentFile() throws Exception {
Kenny Root1ebd74a2011-08-03 15:09:44 -07003391 int retCode = PackageManager.INSTALL_FAILED_INVALID_URI;
3392 File invalidFile = new File("/nonexistent-file.apk");
3393 invokeInstallPackageFail(Uri.fromFile(invalidFile), 0, retCode);
3394 }
3395
Kenny Root0aaa0d92011-09-12 16:42:55 -07003396 @SmallTest
Kenny Root25c925e2012-09-08 22:02:21 -07003397 public void testGetVerifierDeviceIdentity() throws Exception {
Kenny Root0aaa0d92011-09-12 16:42:55 -07003398 PackageManager pm = getPm();
3399 VerifierDeviceIdentity id = pm.getVerifierDeviceIdentity();
3400
3401 assertNotNull("Verifier device identity should not be null", id);
3402 }
3403
Kenny Root25c925e2012-09-08 22:02:21 -07003404 public void testGetInstalledPackages() throws Exception {
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003405 List<PackageInfo> packages = getPm().getInstalledPackages(0);
3406 assertNotNull("installed packages cannot be null", packages);
3407 assertTrue("installed packages cannot be empty", packages.size() > 0);
3408 }
3409
Kenny Root25c925e2012-09-08 22:02:21 -07003410 public void testGetUnInstalledPackages() throws Exception {
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003411 List<PackageInfo> packages = getPm().getInstalledPackages(
3412 PackageManager.GET_UNINSTALLED_PACKAGES);
3413 assertNotNull("installed packages cannot be null", packages);
3414 assertTrue("installed packages cannot be empty", packages.size() > 0);
3415 }
3416
3417 /**
Kenny Root25c925e2012-09-08 22:02:21 -07003418 * Test that getInstalledPackages returns all the data specified in flags.
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003419 */
Kenny Root25c925e2012-09-08 22:02:21 -07003420 public void testGetInstalledPackagesAll() throws Exception {
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003421 int flags = PackageManager.GET_ACTIVITIES | PackageManager.GET_GIDS
3422 | PackageManager.GET_CONFIGURATIONS | PackageManager.GET_INSTRUMENTATION
3423 | PackageManager.GET_PERMISSIONS | PackageManager.GET_PROVIDERS
3424 | PackageManager.GET_RECEIVERS | PackageManager.GET_SERVICES
3425 | PackageManager.GET_SIGNATURES | PackageManager.GET_UNINSTALLED_PACKAGES;
3426
3427 List<PackageInfo> packages = getPm().getInstalledPackages(flags);
3428 assertNotNull("installed packages cannot be null", packages);
3429 assertTrue("installed packages cannot be empty", packages.size() > 0);
3430
3431 PackageInfo packageInfo = null;
3432
3433 // Find the package with all components specified in the AndroidManifest
3434 // to ensure no null values
3435 for (PackageInfo pi : packages) {
3436 if ("com.android.frameworks.coretests.install_complete_package_info"
3437 .equals(pi.packageName)) {
3438 packageInfo = pi;
3439 break;
3440 }
3441 }
3442 assertNotNull("activities should not be null", packageInfo.activities);
3443 assertNotNull("configPreferences should not be null", packageInfo.configPreferences);
3444 assertNotNull("instrumentation should not be null", packageInfo.instrumentation);
3445 assertNotNull("permissions should not be null", packageInfo.permissions);
3446 assertNotNull("providers should not be null", packageInfo.providers);
3447 assertNotNull("receivers should not be null", packageInfo.receivers);
3448 assertNotNull("services should not be null", packageInfo.services);
3449 assertNotNull("signatures should not be null", packageInfo.signatures);
3450 }
3451
3452 /**
3453 * Test that getInstalledPackages returns all the data specified in
3454 * flags when the GET_UNINSTALLED_PACKAGES flag is set.
3455 */
Kenny Root25c925e2012-09-08 22:02:21 -07003456 public void testGetUnInstalledPackagesAll() throws Exception {
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003457 int flags = PackageManager.GET_UNINSTALLED_PACKAGES
3458 | PackageManager.GET_ACTIVITIES | PackageManager.GET_GIDS
3459 | PackageManager.GET_CONFIGURATIONS | PackageManager.GET_INSTRUMENTATION
3460 | PackageManager.GET_PERMISSIONS | PackageManager.GET_PROVIDERS
3461 | PackageManager.GET_RECEIVERS | PackageManager.GET_SERVICES
3462 | PackageManager.GET_SIGNATURES | PackageManager.GET_UNINSTALLED_PACKAGES;
3463
3464 List<PackageInfo> packages = getPm().getInstalledPackages(flags);
3465 assertNotNull("installed packages cannot be null", packages);
3466 assertTrue("installed packages cannot be empty", packages.size() > 0);
3467
3468 PackageInfo packageInfo = null;
3469
3470 // Find the package with all components specified in the AndroidManifest
3471 // to ensure no null values
3472 for (PackageInfo pi : packages) {
3473 if ("com.android.frameworks.coretests.install_complete_package_info"
3474 .equals(pi.packageName)) {
3475 packageInfo = pi;
3476 break;
3477 }
3478 }
3479 assertNotNull("activities should not be null", packageInfo.activities);
3480 assertNotNull("configPreferences should not be null", packageInfo.configPreferences);
3481 assertNotNull("instrumentation should not be null", packageInfo.instrumentation);
3482 assertNotNull("permissions should not be null", packageInfo.permissions);
3483 assertNotNull("providers should not be null", packageInfo.providers);
3484 assertNotNull("receivers should not be null", packageInfo.receivers);
3485 assertNotNull("services should not be null", packageInfo.services);
3486 assertNotNull("signatures should not be null", packageInfo.signatures);
3487 }
3488
Kenny Roote15bdc22012-09-16 15:45:38 -07003489 public void testInstall_BadDex_CleanUp() throws Exception {
3490 int retCode = PackageManager.INSTALL_FAILED_DEXOPT;
3491 installFromRawResource("install.apk", R.raw.install_bad_dex, 0, true, true, retCode,
3492 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3493 }
3494
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07003495 /*---------- Recommended install location tests ----*/
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08003496 /*
3497 * TODO's
3498 * check version numbers for upgrades
3499 * check permissions of installed packages
3500 * how to do tests on updated system apps?
3501 * verify updates to system apps cannot be installed on the sdcard.
3502 */
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -08003503}