blob: 423d45ea520a49299838a5c4920c8eb435840c5b [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
Jeff Sharkey2a533322014-11-07 16:24:48 -080019import static android.system.OsConstants.S_IFDIR;
20import static android.system.OsConstants.S_IFMT;
21import static android.system.OsConstants.S_IRGRP;
22import static android.system.OsConstants.S_IROTH;
23import static android.system.OsConstants.S_IRWXU;
24import static android.system.OsConstants.S_ISDIR;
25import static android.system.OsConstants.S_IXGRP;
26import static android.system.OsConstants.S_IXOTH;
Suchi Amalapurapu089262d2010-03-10 14:19:21 -080027
Christopher Tatef1977b42014-03-24 16:25:51 -070028import android.app.PackageInstallObserver;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -080029import android.content.BroadcastReceiver;
30import android.content.Context;
31import android.content.Intent;
32import android.content.IntentFilter;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -080033import android.content.pm.PackageManager.NameNotFoundException;
Jeff Sharkeyc4858a22014-06-16 10:51:20 -070034import android.content.pm.PackageParser.PackageParserException;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -080035import android.content.res.Resources;
36import android.content.res.Resources.NotFoundException;
Brett Chabota3de7452010-03-25 13:49:26 -070037import android.net.Uri;
dcashman9d2f4412014-06-09 09:27:54 -070038import android.os.Binder;
Calin Juravle3d2af7f2017-04-19 19:56:21 -070039import android.os.Build;
Christopher Tatef1977b42014-03-24 16:25:51 -070040import android.os.Bundle;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -080041import android.os.Environment;
Brett Chabota3de7452010-03-25 13:49:26 -070042import android.os.FileUtils;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080043import android.os.IBinder;
Kenny Root7e921a12012-09-08 22:02:21 -070044import android.os.Process;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -080045import android.os.RemoteException;
46import android.os.ServiceManager;
47import android.os.StatFs;
Kenny Root7e921a12012-09-08 22:02:21 -070048import android.os.SystemClock;
Kenny Roote15bdc22012-09-16 15:45:38 -070049import android.os.UserManager;
Sudheer Shanka2250d562016-11-07 15:41:02 -080050import android.os.storage.IStorageManager;
Brett Chabota3de7452010-03-25 13:49:26 -070051import android.os.storage.StorageListener;
52import android.os.storage.StorageManager;
53import android.os.storage.StorageResultCode;
Suchi Amalapurapu117818e2010-02-09 03:45:40 -080054import android.provider.Settings;
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -080055import android.provider.Settings.SettingNotFoundException;
Elliott Hughes34385d32014-04-28 11:11:32 -070056import android.system.ErrnoException;
Elliott Hughes97901902014-04-28 12:40:12 -070057import android.system.Os;
Elliott Hughes34385d32014-04-28 11:11:32 -070058import android.system.StructStat;
Brett Chabota3de7452010-03-25 13:49:26 -070059import android.test.AndroidTestCase;
Brett Chabotf76c56b2010-07-26 17:28:17 -070060import android.test.suitebuilder.annotation.LargeTest;
Kenny Root0aaa0d92011-09-12 16:42:55 -070061import android.test.suitebuilder.annotation.SmallTest;
Abodunrinwa Tokif9017762015-07-31 18:13:30 -070062import android.test.suitebuilder.annotation.Suppress;
Brett Chabota3de7452010-03-25 13:49:26 -070063import android.util.Log;
64
Jeff Sharkey2a533322014-11-07 16:24:48 -080065import com.android.frameworks.coretests.R;
66import com.android.internal.content.PackageHelper;
67
Calin Juravle3d2af7f2017-04-19 19:56:21 -070068import dalvik.system.VMRuntime;
69
Brett Chabota3de7452010-03-25 13:49:26 -070070import java.io.File;
Kenny Root6a6b0072010-10-07 16:46:10 -070071import java.io.IOException;
Brett Chabota3de7452010-03-25 13:49:26 -070072import java.io.InputStream;
Calin Juravle3d2af7f2017-04-19 19:56:21 -070073import java.nio.file.Files;
74import java.nio.file.Paths;
75import java.nio.file.StandardCopyOption;
dcashman55b10782014-04-09 14:20:38 -070076import java.util.HashSet;
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +020077import java.util.List;
dcashman55b10782014-04-09 14:20:38 -070078import java.util.Set;
Kenny Root7e921a12012-09-08 22:02:21 -070079import java.util.concurrent.CountDownLatch;
80import java.util.concurrent.TimeUnit;
81
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -080082public class PackageManagerTests extends AndroidTestCase {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080083 private static final boolean localLOGV = true;
Kenny Root25c925e2012-09-08 22:02:21 -070084
85 public static final String TAG = "PackageManagerTests";
86
Calin Juravle3d2af7f2017-04-19 19:56:21 -070087 public static final long MAX_WAIT_TIME = 25 * 1000;
Kenny Root25c925e2012-09-08 22:02:21 -070088
Calin Juravle3d2af7f2017-04-19 19:56:21 -070089 public static final long WAIT_TIME_INCR = 5 * 1000;
Kenny Root25c925e2012-09-08 22:02:21 -070090
Jeff Sharkey2a533322014-11-07 16:24:48 -080091 private static final String SECURE_CONTAINERS_PREFIX = "/mnt/asec";
Kenny Root25c925e2012-09-08 22:02:21 -070092
Suchi Amalapurapu089262d2010-03-10 14:19:21 -080093 private static final int APP_INSTALL_AUTO = PackageHelper.APP_INSTALL_AUTO;
Kenny Root25c925e2012-09-08 22:02:21 -070094
Suchi Amalapurapu089262d2010-03-10 14:19:21 -080095 private static final int APP_INSTALL_DEVICE = PackageHelper.APP_INSTALL_INTERNAL;
Kenny Root25c925e2012-09-08 22:02:21 -070096
Suchi Amalapurapu089262d2010-03-10 14:19:21 -080097 private static final int APP_INSTALL_SDCARD = PackageHelper.APP_INSTALL_EXTERNAL;
Kenny Root25c925e2012-09-08 22:02:21 -070098
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -070099 private boolean mOrigState;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800100
101 void failStr(String errMsg) {
Kenny Root25c925e2012-09-08 22:02:21 -0700102 Log.w(TAG, "errMsg=" + errMsg);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800103 fail(errMsg);
104 }
Kenny Root25c925e2012-09-08 22:02:21 -0700105
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800106 void failStr(Exception e) {
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -0700107 failStr(e.getMessage());
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800108 }
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800109
110 @Override
111 protected void setUp() throws Exception {
112 super.setUp();
Kenny Rootd7b421b2010-08-05 08:40:00 -0700113 mOrigState = checkMediaState(Environment.MEDIA_MOUNTED);
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700114 if (!mountMedia()) {
115 Log.i(TAG, "sdcard not mounted? Some of these tests might fail");
116 }
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700117 }
118
119 @Override
120 protected void tearDown() throws Exception {
121 // Restore media state.
Kenny Rootd7b421b2010-08-05 08:40:00 -0700122 boolean newState = checkMediaState(Environment.MEDIA_MOUNTED);
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700123 if (newState != mOrigState) {
124 if (mOrigState) {
Kenny Rootd7b421b2010-08-05 08:40:00 -0700125 mountMedia();
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700126 } else {
Kenny Rootd7b421b2010-08-05 08:40:00 -0700127 unmountMedia();
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700128 }
129 }
130 super.tearDown();
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800131 }
132
Christopher Tatef1977b42014-03-24 16:25:51 -0700133 private class TestInstallObserver extends PackageInstallObserver {
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800134 public int returnCode;
Kenny Root25c925e2012-09-08 22:02:21 -0700135
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800136 private boolean doneFlag = false;
137
Jeff Sharkey2a533322014-11-07 16:24:48 -0800138 @Override
139 public void onPackageInstalled(String basePackageName, int returnCode, String msg,
140 Bundle extras) {
141 Log.d(TAG, "onPackageInstalled: code=" + returnCode + ", msg=" + msg + ", extras="
142 + extras);
Kenny Root25c925e2012-09-08 22:02:21 -0700143 synchronized (this) {
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800144 this.returnCode = returnCode;
145 doneFlag = true;
146 notifyAll();
147 }
148 }
149
150 public boolean isDone() {
151 return doneFlag;
152 }
153 }
154
155 abstract class GenericReceiver extends BroadcastReceiver {
156 private boolean doneFlag = false;
Kenny Root25c925e2012-09-08 22:02:21 -0700157
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800158 boolean received = false;
Kenny Root25c925e2012-09-08 22:02:21 -0700159
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800160 Intent intent;
Kenny Root25c925e2012-09-08 22:02:21 -0700161
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800162 IntentFilter filter;
Kenny Root25c925e2012-09-08 22:02:21 -0700163
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800164 abstract boolean notifyNow(Intent intent);
Kenny Root25c925e2012-09-08 22:02:21 -0700165
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800166 @Override
167 public void onReceive(Context context, Intent intent) {
168 if (notifyNow(intent)) {
169 synchronized (this) {
170 received = true;
171 doneFlag = true;
172 this.intent = intent;
173 notifyAll();
174 }
175 }
176 }
177
178 public boolean isDone() {
179 return doneFlag;
180 }
181
182 public void setFilter(IntentFilter filter) {
183 this.filter = filter;
184 }
185 }
186
187 class InstallReceiver extends GenericReceiver {
188 String pkgName;
189
190 InstallReceiver(String pkgName) {
191 this.pkgName = pkgName;
192 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
193 filter.addDataScheme("package");
194 super.setFilter(filter);
195 }
196
197 public boolean notifyNow(Intent intent) {
198 String action = intent.getAction();
199 if (!Intent.ACTION_PACKAGE_ADDED.equals(action)) {
200 return false;
201 }
202 Uri data = intent.getData();
203 String installedPkg = data.getEncodedSchemeSpecificPart();
204 if (pkgName.equals(installedPkg)) {
205 return true;
206 }
207 return false;
208 }
209 }
210
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700211 private PackageManager getPm() {
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800212 return mContext.getPackageManager();
213 }
214
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700215 private IPackageManager getIPm() {
216 IPackageManager ipm = IPackageManager.Stub.asInterface(
217 ServiceManager.getService("package"));
218 return ipm;
219 }
220
Kenny Rootb7c24702012-04-14 19:46:09 -0700221 public void invokeInstallPackage(Uri packageURI, int flags, GenericReceiver receiver,
222 boolean shouldSucceed) {
Christopher Tatef1977b42014-03-24 16:25:51 -0700223 TestInstallObserver observer = new TestInstallObserver();
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800224 mContext.registerReceiver(receiver, receiver.filter);
225 try {
226 // Wait on observer
Kenny Root25c925e2012-09-08 22:02:21 -0700227 synchronized (observer) {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800228 synchronized (receiver) {
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800229 getPm().installPackage(packageURI, observer, flags, null);
230 long waitTime = 0;
Kenny Root25c925e2012-09-08 22:02:21 -0700231 while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) {
Kenny Root94e0acb2010-09-21 16:30:56 -0700232 try {
233 observer.wait(WAIT_TIME_INCR);
234 waitTime += WAIT_TIME_INCR;
235 } catch (InterruptedException e) {
236 Log.i(TAG, "Interrupted during sleep", e);
237 }
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800238 }
Kenny Root25c925e2012-09-08 22:02:21 -0700239 if (!observer.isDone()) {
Kenny Root94e0acb2010-09-21 16:30:56 -0700240 fail("Timed out waiting for packageInstalled callback");
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800241 }
Kenny Rootb7c24702012-04-14 19:46:09 -0700242
243 if (shouldSucceed) {
244 if (observer.returnCode != PackageManager.INSTALL_SUCCEEDED) {
245 fail("Package installation should have succeeded, but got code "
246 + observer.returnCode);
247 }
248 } else {
249 if (observer.returnCode == PackageManager.INSTALL_SUCCEEDED) {
250 fail("Package installation should fail");
251 }
252
253 /*
254 * We'll never expect get a notification since we
255 * shouldn't succeed.
256 */
257 return;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800258 }
Kenny Rootb7c24702012-04-14 19:46:09 -0700259
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800260 // Verify we received the broadcast
261 waitTime = 0;
Kenny Root25c925e2012-09-08 22:02:21 -0700262 while ((!receiver.isDone()) && (waitTime < MAX_WAIT_TIME)) {
Kenny Root94e0acb2010-09-21 16:30:56 -0700263 try {
264 receiver.wait(WAIT_TIME_INCR);
265 waitTime += WAIT_TIME_INCR;
266 } catch (InterruptedException e) {
267 Log.i(TAG, "Interrupted during sleep", e);
268 }
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800269 }
Kenny Root25c925e2012-09-08 22:02:21 -0700270 if (!receiver.isDone()) {
Kenny Root94e0acb2010-09-21 16:30:56 -0700271 fail("Timed out waiting for PACKAGE_ADDED notification");
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800272 }
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800273 }
274 }
275 } finally {
276 mContext.unregisterReceiver(receiver);
277 }
278 }
279
Kenny Root1683afa2011-01-07 14:27:50 -0800280 public void invokeInstallPackageFail(Uri packageURI, int flags, int expectedResult) {
Christopher Tatef1977b42014-03-24 16:25:51 -0700281 TestInstallObserver observer = new TestInstallObserver();
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800282 try {
283 // Wait on observer
Kenny Root25c925e2012-09-08 22:02:21 -0700284 synchronized (observer) {
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800285 getPm().installPackage(packageURI, observer, flags, null);
286 long waitTime = 0;
Kenny Root25c925e2012-09-08 22:02:21 -0700287 while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) {
Kenny Root94e0acb2010-09-21 16:30:56 -0700288 try {
289 observer.wait(WAIT_TIME_INCR);
290 waitTime += WAIT_TIME_INCR;
291 } catch (InterruptedException e) {
292 Log.i(TAG, "Interrupted during sleep", e);
293 }
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800294 }
Kenny Root25c925e2012-09-08 22:02:21 -0700295 if (!observer.isDone()) {
Kenny Root94e0acb2010-09-21 16:30:56 -0700296 fail("Timed out waiting for packageInstalled callback");
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800297 }
Kenny Root1683afa2011-01-07 14:27:50 -0800298 assertEquals(expectedResult, observer.returnCode);
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800299 }
300 } finally {
301 }
302 }
303
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800304 Uri getInstallablePackage(int fileResId, File outFile) {
305 Resources res = mContext.getResources();
306 InputStream is = null;
307 try {
308 is = res.openRawResource(fileResId);
309 } catch (NotFoundException e) {
310 failStr("Failed to load resource with id: " + fileResId);
311 }
312 FileUtils.setPermissions(outFile.getPath(),
313 FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IRWXO,
314 -1, -1);
315 assertTrue(FileUtils.copyToFile(is, outFile));
316 FileUtils.setPermissions(outFile.getPath(),
317 FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IRWXO,
318 -1, -1);
319 return Uri.fromFile(outFile);
320 }
321
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700322 private PackageParser.Package parsePackage(Uri packageURI) throws PackageParserException {
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800323 final String archiveFilePath = packageURI.getPath();
Jeff Sharkey275e0852014-06-17 18:18:49 -0700324 PackageParser packageParser = new PackageParser();
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800325 File sourceFile = new File(archiveFilePath);
Jeff Sharkey275e0852014-06-17 18:18:49 -0700326 PackageParser.Package pkg = packageParser.parseMonolithicPackage(sourceFile, 0);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -0800327 packageParser = null;
328 return pkg;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800329 }
Kenny Root25c925e2012-09-08 22:02:21 -0700330
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700331 private boolean checkSd(long pkgLen) {
332 String status = Environment.getExternalStorageState();
333 if (!status.equals(Environment.MEDIA_MOUNTED)) {
334 return false;
335 }
336 long sdSize = -1;
Kenny Root25c925e2012-09-08 22:02:21 -0700337 StatFs sdStats = new StatFs(Environment.getExternalStorageDirectory().getPath());
338 sdSize = (long) sdStats.getAvailableBlocks() * (long) sdStats.getBlockSize();
Kenny Root94e0acb2010-09-21 16:30:56 -0700339 // TODO check for thresholds here
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700340 return pkgLen <= sdSize;
Neal Nguyenedb979a2010-04-22 13:54:32 -0700341
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700342 }
Kenny Root25c925e2012-09-08 22:02:21 -0700343
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700344 private boolean checkInt(long pkgLen) {
345 StatFs intStats = new StatFs(Environment.getDataDirectory().getPath());
Kenny Root25c925e2012-09-08 22:02:21 -0700346 long intSize = (long) intStats.getBlockCount() * (long) intStats.getBlockSize();
347 long iSize = (long) intStats.getAvailableBlocks() * (long) intStats.getBlockSize();
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700348 // TODO check for thresholds here?
349 return pkgLen <= iSize;
350 }
Kenny Root25c925e2012-09-08 22:02:21 -0700351
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700352 private static final int INSTALL_LOC_INT = 1;
Kenny Root25c925e2012-09-08 22:02:21 -0700353
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700354 private static final int INSTALL_LOC_SD = 2;
Kenny Root25c925e2012-09-08 22:02:21 -0700355
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700356 private static final int INSTALL_LOC_ERR = -1;
Kenny Root25c925e2012-09-08 22:02:21 -0700357
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700358 private int getInstallLoc(int flags, int expInstallLocation, long pkgLen) {
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800359 // Flags explicitly over ride everything else.
Kenny Root25c925e2012-09-08 22:02:21 -0700360 if ((flags & PackageManager.INSTALL_EXTERNAL) != 0) {
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700361 return INSTALL_LOC_SD;
362 } else if ((flags & PackageManager.INSTALL_INTERNAL) != 0) {
363 return INSTALL_LOC_INT;
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800364 }
365 // Manifest option takes precedence next
366 if (expInstallLocation == PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL) {
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700367 if (checkSd(pkgLen)) {
Kenny Root25c925e2012-09-08 22:02:21 -0700368 return INSTALL_LOC_SD;
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700369 }
370 if (checkInt(pkgLen)) {
371 return INSTALL_LOC_INT;
372 }
373 return INSTALL_LOC_ERR;
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800374 }
Suchi Amalapurapu9b10ef52010-03-03 09:45:24 -0800375 if (expInstallLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY) {
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700376 if (checkInt(pkgLen)) {
377 return INSTALL_LOC_INT;
378 }
379 return INSTALL_LOC_ERR;
Suchi Amalapurapu9b10ef52010-03-03 09:45:24 -0800380 }
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700381 if (expInstallLocation == PackageInfo.INSTALL_LOCATION_AUTO) {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700382 // Check for free memory internally
383 if (checkInt(pkgLen)) {
384 return INSTALL_LOC_INT;
385 }
386 // Check for free memory externally
387 if (checkSd(pkgLen)) {
388 return INSTALL_LOC_SD;
389 }
390 return INSTALL_LOC_ERR;
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700391 }
392 // Check for settings preference.
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800393 boolean checkSd = false;
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700394 int userPref = getDefaultInstallLoc();
395 if (userPref == APP_INSTALL_DEVICE) {
396 if (checkInt(pkgLen)) {
397 return INSTALL_LOC_INT;
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800398 }
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700399 return INSTALL_LOC_ERR;
400 } else if (userPref == APP_INSTALL_SDCARD) {
401 if (checkSd(pkgLen)) {
402 return INSTALL_LOC_SD;
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800403 }
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700404 return INSTALL_LOC_ERR;
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -0700405 }
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700406 // Default system policy for apps with no manifest option specified.
407 // Check for free memory internally
408 if (checkInt(pkgLen)) {
409 return INSTALL_LOC_INT;
410 }
411 return INSTALL_LOC_ERR;
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800412 }
Neal Nguyenedb979a2010-04-22 13:54:32 -0700413
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800414 private void assertInstall(PackageParser.Package pkg, int flags, int expInstallLocation) {
415 try {
416 String pkgName = pkg.packageName;
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700417 ApplicationInfo info = getPm().getApplicationInfo(pkgName, 0);
418 assertNotNull(info);
419 assertEquals(pkgName, info.packageName);
420 File dataDir = Environment.getDataDirectory();
421 String appInstallPath = new File(dataDir, "app").getPath();
422 String drmInstallPath = new File(dataDir, "app-private").getPath();
423 File srcDir = new File(info.sourceDir);
Jeff Sharkey2a533322014-11-07 16:24:48 -0800424 String srcPath = srcDir.getParentFile().getParent();
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700425 File publicSrcDir = new File(info.publicSourceDir);
Jeff Sharkey2a533322014-11-07 16:24:48 -0800426 String publicSrcPath = publicSrcDir.getParentFile().getParent();
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700427 long pkgLen = new File(info.sourceDir).length();
Jeff Sharkey2a533322014-11-07 16:24:48 -0800428 String expectedLibPath = new File(new File(info.sourceDir).getParentFile(), "lib")
429 .getPath();
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800430
Kenny Root6dceb882012-04-12 14:23:49 -0700431 int rLoc = getInstallLoc(flags, expInstallLocation, pkgLen);
432 if (rLoc == INSTALL_LOC_INT) {
433 if ((flags & PackageManager.INSTALL_FORWARD_LOCK) != 0) {
434 assertTrue("The application should be installed forward locked",
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800435 (info.privateFlags & ApplicationInfo.PRIVATE_FLAG_FORWARD_LOCK) != 0);
Kenny Root7e921a12012-09-08 22:02:21 -0700436 assertStartsWith("The APK path should point to the ASEC",
437 SECURE_CONTAINERS_PREFIX, srcPath);
438 assertStartsWith("The public APK path should point to the ASEC",
439 SECURE_CONTAINERS_PREFIX, publicSrcPath);
440 assertStartsWith("The native library path should point to the ASEC",
441 SECURE_CONTAINERS_PREFIX, info.nativeLibraryDir);
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700442 } else {
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800443 assertFalse(
444 (info.privateFlags & ApplicationInfo.PRIVATE_FLAG_FORWARD_LOCK) != 0);
Jeff Sharkey2a533322014-11-07 16:24:48 -0800445 assertEquals(appInstallPath, srcPath);
446 assertEquals(appInstallPath, publicSrcPath);
Kenny Root7e921a12012-09-08 22:02:21 -0700447 assertStartsWith("Native library should point to shared lib directory",
Jeff Sharkey2a533322014-11-07 16:24:48 -0800448 expectedLibPath, info.nativeLibraryDir);
449 assertDirOwnerGroupPermsIfExists(
Kenny Root7e921a12012-09-08 22:02:21 -0700450 "Native library directory should be owned by system:system and 0755",
451 Process.SYSTEM_UID, Process.SYSTEM_UID,
452 S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH,
453 info.nativeLibraryDir);
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -0700454 }
Kenny Root6dceb882012-04-12 14:23:49 -0700455 assertFalse((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0);
456
457 // Make sure the native library dir is not a symlink
458 final File nativeLibDir = new File(info.nativeLibraryDir);
Jeff Sharkey2a533322014-11-07 16:24:48 -0800459 if (nativeLibDir.exists()) {
460 try {
461 assertEquals("Native library dir should not be a symlink",
462 info.nativeLibraryDir, nativeLibDir.getCanonicalPath());
463 } catch (IOException e) {
464 fail("Can't read " + nativeLibDir.getPath());
465 }
Kenny Root6dceb882012-04-12 14:23:49 -0700466 }
Kenny Root25c925e2012-09-08 22:02:21 -0700467 } else if (rLoc == INSTALL_LOC_SD) {
Kenny Root6dceb882012-04-12 14:23:49 -0700468 if ((flags & PackageManager.INSTALL_FORWARD_LOCK) != 0) {
469 assertTrue("The application should be installed forward locked",
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800470 (info.privateFlags & ApplicationInfo.PRIVATE_FLAG_FORWARD_LOCK) != 0);
Kenny Root6dceb882012-04-12 14:23:49 -0700471 } else {
472 assertFalse("The application should not be installed forward locked",
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800473 (info.privateFlags & ApplicationInfo.PRIVATE_FLAG_FORWARD_LOCK) != 0);
Kenny Root6dceb882012-04-12 14:23:49 -0700474 }
475 assertTrue("Application flags (" + info.flags
476 + ") should contain FLAG_EXTERNAL_STORAGE",
477 (info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0);
478 // Might need to check:
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800479 // ((info.privateFlags & ApplicationInfo.PRIVATE_FLAG_FORWARD_LOCK) != 0)
Kenny Root7e921a12012-09-08 22:02:21 -0700480 assertStartsWith("The APK path should point to the ASEC",
481 SECURE_CONTAINERS_PREFIX, srcPath);
482 assertStartsWith("The public APK path should point to the ASEC",
483 SECURE_CONTAINERS_PREFIX, publicSrcPath);
484 assertStartsWith("The native library path should point to the ASEC",
485 SECURE_CONTAINERS_PREFIX, info.nativeLibraryDir);
Kenny Root6dceb882012-04-12 14:23:49 -0700486
487 // Make sure the native library in /data/data/<app>/lib is a
488 // symlink to the ASEC
489 final File nativeLibSymLink = new File(info.dataDir, "lib");
490 assertTrue("Native library symlink should exist at " + nativeLibSymLink.getPath(),
491 nativeLibSymLink.exists());
492 try {
493 assertEquals(nativeLibSymLink.getPath() + " should be a symlink to "
Kenny Root25c925e2012-09-08 22:02:21 -0700494 + info.nativeLibraryDir, info.nativeLibraryDir,
495 nativeLibSymLink.getCanonicalPath());
Kenny Root6dceb882012-04-12 14:23:49 -0700496 } catch (IOException e) {
497 fail("Can't read " + nativeLibSymLink.getPath());
498 }
499 } else {
500 // TODO handle error. Install should have failed.
501 fail("Install should have failed");
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800502 }
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800503 } catch (NameNotFoundException e) {
504 failStr("failed with exception : " + e);
505 }
506 }
Neal Nguyenedb979a2010-04-22 13:54:32 -0700507
Jeff Sharkey2a533322014-11-07 16:24:48 -0800508 private void assertDirOwnerGroupPermsIfExists(String reason, int uid, int gid, int perms,
509 String path) {
510 if (!new File(path).exists()) {
511 return;
512 }
Kenny Root7e921a12012-09-08 22:02:21 -0700513
Jeff Sharkey2a533322014-11-07 16:24:48 -0800514 final StructStat stat;
Kenny Root7e921a12012-09-08 22:02:21 -0700515 try {
Elliott Hughes34385d32014-04-28 11:11:32 -0700516 stat = Os.lstat(path);
Kenny Root7e921a12012-09-08 22:02:21 -0700517 } catch (ErrnoException e) {
518 throw new AssertionError(reason + "\n" + "Got: " + path + " does not exist");
519 }
520
521 StringBuilder sb = new StringBuilder();
522
523 if (!S_ISDIR(stat.st_mode)) {
524 sb.append("\nExpected type: ");
525 sb.append(S_IFDIR);
526 sb.append("\ngot type: ");
527 sb.append((stat.st_mode & S_IFMT));
528 }
529
530 if (stat.st_uid != uid) {
531 sb.append("\nExpected owner: ");
532 sb.append(uid);
533 sb.append("\nGot owner: ");
534 sb.append(stat.st_uid);
535 }
536
537 if (stat.st_gid != gid) {
538 sb.append("\nExpected group: ");
539 sb.append(gid);
540 sb.append("\nGot group: ");
541 sb.append(stat.st_gid);
542 }
543
544 if ((stat.st_mode & ~S_IFMT) != perms) {
545 sb.append("\nExpected permissions: ");
546 sb.append(Integer.toOctalString(perms));
547 sb.append("\nGot permissions: ");
548 sb.append(Integer.toOctalString(stat.st_mode & ~S_IFMT));
549 }
550
551 if (sb.length() > 0) {
552 throw new AssertionError(reason + sb.toString());
553 }
554 }
555
556 private static void assertStartsWith(String prefix, String actual) {
557 assertStartsWith("", prefix, actual);
558 }
559
560 private static void assertStartsWith(String description, String prefix, String actual) {
561 if (!actual.startsWith(prefix)) {
562 StringBuilder sb = new StringBuilder(description);
563 sb.append("\nExpected prefix: ");
564 sb.append(prefix);
565 sb.append("\n got: ");
566 sb.append(actual);
567 sb.append('\n');
568 throw new AssertionError(sb.toString());
569 }
570 }
571
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800572 private void assertNotInstalled(String pkgName) {
573 try {
574 ApplicationInfo info = getPm().getApplicationInfo(pkgName, 0);
575 fail(pkgName + " shouldnt be installed");
576 } catch (NameNotFoundException e) {
577 }
578 }
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800579
580 class InstallParams {
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800581 Uri packageURI;
Kenny Root25c925e2012-09-08 22:02:21 -0700582
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800583 PackageParser.Package pkg;
Kenny Root25c925e2012-09-08 22:02:21 -0700584
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700585 InstallParams(String outFileName, int rawResId) throws PackageParserException {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700586 this.pkg = getParsedPackage(outFileName, rawResId);
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700587 this.packageURI = Uri.fromFile(new File(pkg.codePath));
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700588 }
Kenny Root25c925e2012-09-08 22:02:21 -0700589
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700590 InstallParams(PackageParser.Package pkg) {
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700591 this.packageURI = Uri.fromFile(new File(pkg.codePath));
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800592 this.pkg = pkg;
593 }
Kenny Root25c925e2012-09-08 22:02:21 -0700594
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700595 long getApkSize() {
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700596 File file = new File(pkg.codePath);
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700597 return file.length();
598 }
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800599 }
600
Kenny Root25c925e2012-09-08 22:02:21 -0700601 private InstallParams sampleInstallFromRawResource(int flags, boolean cleanUp) throws Exception {
602 return installFromRawResource("install.apk", R.raw.install, flags, cleanUp, false, -1,
603 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800604 }
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -0800605
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700606 static final String PERM_PACKAGE = "package";
Kenny Root25c925e2012-09-08 22:02:21 -0700607
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700608 static final String PERM_DEFINED = "defined";
Kenny Root25c925e2012-09-08 22:02:21 -0700609
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700610 static final String PERM_UNDEFINED = "undefined";
Kenny Root25c925e2012-09-08 22:02:21 -0700611
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700612 static final String PERM_USED = "used";
Kenny Root25c925e2012-09-08 22:02:21 -0700613
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700614 static final String PERM_NOTUSED = "notused";
Neal Nguyenedb979a2010-04-22 13:54:32 -0700615
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700616 private void assertPermissions(String[] cmds) {
617 final PackageManager pm = getPm();
618 String pkg = null;
619 PackageInfo pkgInfo = null;
620 String mode = PERM_DEFINED;
621 int i = 0;
622 while (i < cmds.length) {
623 String cmd = cmds[i++];
624 if (cmd == PERM_PACKAGE) {
625 pkg = cmds[i++];
626 try {
627 pkgInfo = pm.getPackageInfo(pkg,
628 PackageManager.GET_PERMISSIONS
Amith Yamasani0d1fd8d2016-10-12 14:21:51 -0700629 | PackageManager.MATCH_UNINSTALLED_PACKAGES);
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700630 } catch (NameNotFoundException e) {
631 pkgInfo = null;
632 }
633 } else if (cmd == PERM_DEFINED || cmd == PERM_UNDEFINED
634 || cmd == PERM_USED || cmd == PERM_NOTUSED) {
635 mode = cmds[i++];
636 } else {
637 if (mode == PERM_DEFINED) {
638 try {
639 PermissionInfo pi = pm.getPermissionInfo(cmd, 0);
640 assertNotNull(pi);
641 assertEquals(pi.packageName, pkg);
642 assertEquals(pi.name, cmd);
643 assertNotNull(pkgInfo);
644 boolean found = false;
Kenny Root25c925e2012-09-08 22:02:21 -0700645 for (int j = 0; j < pkgInfo.permissions.length && !found; j++) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700646 if (pkgInfo.permissions[j].name.equals(cmd)) {
647 found = true;
648 }
649 }
650 if (!found) {
651 fail("Permission not found: " + cmd);
652 }
653 } catch (NameNotFoundException e) {
654 throw new RuntimeException(e);
655 }
656 } else if (mode == PERM_UNDEFINED) {
657 try {
658 pm.getPermissionInfo(cmd, 0);
659 throw new RuntimeException("Permission exists: " + cmd);
660 } catch (NameNotFoundException e) {
661 }
662 if (pkgInfo != null) {
663 boolean found = false;
Kenny Root25c925e2012-09-08 22:02:21 -0700664 for (int j = 0; j < pkgInfo.permissions.length && !found; j++) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700665 if (pkgInfo.permissions[j].name.equals(cmd)) {
666 found = true;
667 }
668 }
669 if (found) {
670 fail("Permission still exists: " + cmd);
671 }
672 }
673 } else if (mode == PERM_USED || mode == PERM_NOTUSED) {
674 boolean found = false;
Kenny Root25c925e2012-09-08 22:02:21 -0700675 for (int j = 0; j < pkgInfo.requestedPermissions.length && !found; j++) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700676 if (pkgInfo.requestedPermissions[j].equals(cmd)) {
677 found = true;
678 }
679 }
680 if (!found) {
681 fail("Permission not requested: " + cmd);
682 }
683 if (mode == PERM_USED) {
Kenny Root25c925e2012-09-08 22:02:21 -0700684 if (pm.checkPermission(cmd, pkg) != PackageManager.PERMISSION_GRANTED) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700685 fail("Permission not granted: " + cmd);
686 }
687 } else {
Kenny Root25c925e2012-09-08 22:02:21 -0700688 if (pm.checkPermission(cmd, pkg) != PackageManager.PERMISSION_DENIED) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700689 fail("Permission granted: " + cmd);
690 }
691 }
692 }
693 }
694 }
695 }
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -0800696
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700697 private PackageParser.Package getParsedPackage(String outFileName, int rawResId)
698 throws PackageParserException {
Suchi Amalapurapuae181712010-03-30 14:01:02 -0700699 PackageManager pm = mContext.getPackageManager();
700 File filesDir = mContext.getFilesDir();
701 File outFile = new File(filesDir, outFileName);
702 Uri packageURI = getInstallablePackage(rawResId, outFile);
703 PackageParser.Package pkg = parsePackage(packageURI);
704 return pkg;
705 }
706
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800707 /*
708 * Utility function that reads a apk bundled as a raw resource
709 * copies it into own data directory and invokes
710 * PackageManager api to install it.
711 */
Kenny Root25c925e2012-09-08 22:02:21 -0700712 private void installFromRawResource(InstallParams ip, int flags, boolean cleanUp, boolean fail,
713 int result, int expInstallLocation) throws Exception {
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800714 PackageManager pm = mContext.getPackageManager();
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700715 PackageParser.Package pkg = ip.pkg;
716 Uri packageURI = ip.packageURI;
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800717 if ((flags & PackageManager.INSTALL_REPLACE_EXISTING) == 0) {
718 // Make sure the package doesn't exist
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800719 try {
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800720 ApplicationInfo appInfo = pm.getApplicationInfo(pkg.packageName,
Amith Yamasani0d1fd8d2016-10-12 14:21:51 -0700721 PackageManager.MATCH_UNINSTALLED_PACKAGES);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800722 GenericReceiver receiver = new DeleteReceiver(pkg.packageName);
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700723 invokeDeletePackage(pkg.packageName, 0, receiver);
Kenny Roote15bdc22012-09-16 15:45:38 -0700724 } catch (NameNotFoundException e) {
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800725 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800726 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800727 try {
728 if (fail) {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700729 invokeInstallPackageFail(packageURI, flags, result);
Suchi Amalapurapuae181712010-03-30 14:01:02 -0700730 if ((flags & PackageManager.INSTALL_REPLACE_EXISTING) == 0) {
731 assertNotInstalled(pkg.packageName);
732 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800733 } else {
734 InstallReceiver receiver = new InstallReceiver(pkg.packageName);
Kenny Rootb7c24702012-04-14 19:46:09 -0700735 invokeInstallPackage(packageURI, flags, receiver, true);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800736 // Verify installed information
737 assertInstall(pkg, flags, expInstallLocation);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800738 }
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800739 } finally {
740 if (cleanUp) {
Suchi Amalapurapu9b10ef52010-03-03 09:45:24 -0800741 cleanUpInstall(ip);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800742 }
743 }
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700744 }
745
746 /*
747 * Utility function that reads a apk bundled as a raw resource
748 * copies it into own data directory and invokes
749 * PackageManager api to install it.
750 */
Kenny Root25c925e2012-09-08 22:02:21 -0700751 private InstallParams installFromRawResource(String outFileName, int rawResId, int flags,
752 boolean cleanUp, boolean fail, int result, int expInstallLocation) throws Exception {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -0700753 InstallParams ip = new InstallParams(outFileName, rawResId);
754 installFromRawResource(ip, flags, cleanUp, fail, result, expInstallLocation);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800755 return ip;
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 testInstallNormalInternal() throws Exception {
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800760 sampleInstallFromRawResource(0, 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 testInstallFwdLockedInternal() throws Exception {
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800765 sampleInstallFromRawResource(PackageManager.INSTALL_FORWARD_LOCK, true);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800766 }
767
Brett Chabotf76c56b2010-07-26 17:28:17 -0700768 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -0700769 public void testInstallSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -0800770 // Do not run on devices with emulated external storage.
771 if (Environment.isExternalStorageEmulated()) {
772 return;
773 }
774
Kenny Rootd7b421b2010-08-05 08:40:00 -0700775 mountMedia();
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800776 sampleInstallFromRawResource(PackageManager.INSTALL_EXTERNAL, true);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800777 }
778
Kenny Root25c925e2012-09-08 22:02:21 -0700779 /* ------------------------- Test replacing packages -------------- */
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800780 class ReplaceReceiver extends GenericReceiver {
781 String pkgName;
Kenny Root25c925e2012-09-08 22:02:21 -0700782
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800783 final static int INVALID = -1;
Kenny Root25c925e2012-09-08 22:02:21 -0700784
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800785 final static int REMOVED = 1;
Kenny Root25c925e2012-09-08 22:02:21 -0700786
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800787 final static int ADDED = 2;
Kenny Root25c925e2012-09-08 22:02:21 -0700788
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800789 final static int REPLACED = 3;
Kenny Root25c925e2012-09-08 22:02:21 -0700790
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800791 int removed = INVALID;
Kenny Root25c925e2012-09-08 22:02:21 -0700792
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800793 // for updated system apps only
794 boolean update = false;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800795
796 ReplaceReceiver(String pkgName) {
797 this.pkgName = pkgName;
798 filter = new IntentFilter(Intent.ACTION_PACKAGE_REMOVED);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800799 filter.addAction(Intent.ACTION_PACKAGE_ADDED);
800 if (update) {
801 filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
802 }
803 filter.addDataScheme("package");
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800804 super.setFilter(filter);
805 }
806
807 public boolean notifyNow(Intent intent) {
808 String action = intent.getAction();
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800809 Uri data = intent.getData();
810 String installedPkg = data.getEncodedSchemeSpecificPart();
811 if (pkgName == null || !pkgName.equals(installedPkg)) {
812 return false;
813 }
814 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
815 removed = REMOVED;
816 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
817 if (removed != REMOVED) {
818 return false;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800819 }
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800820 boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
821 if (!replacing) {
822 return false;
823 }
824 removed = ADDED;
825 if (!update) {
826 return true;
827 }
828 } else if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
829 if (removed != ADDED) {
830 return false;
831 }
832 removed = REPLACED;
833 return true;
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800834 }
835 return false;
836 }
837 }
838
839 /*
840 * Utility function that reads a apk bundled as a raw resource
841 * copies it into own data directory and invokes
842 * PackageManager api to install first and then replace it
843 * again.
844 */
Kenny Root25c925e2012-09-08 22:02:21 -0700845 private void sampleReplaceFromRawResource(int flags) throws Exception {
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -0800846 InstallParams ip = sampleInstallFromRawResource(flags, false);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800847 boolean replace = ((flags & PackageManager.INSTALL_REPLACE_EXISTING) != 0);
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -0800848 Log.i(TAG, "replace=" + replace);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800849 GenericReceiver receiver;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800850 if (replace) {
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800851 receiver = new ReplaceReceiver(ip.pkg.packageName);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800852 Log.i(TAG, "Creating replaceReceiver");
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800853 } else {
854 receiver = new InstallReceiver(ip.pkg.packageName);
855 }
856 try {
Kenny Rootb7c24702012-04-14 19:46:09 -0700857 invokeInstallPackage(ip.packageURI, flags, receiver, replace);
858 if (replace) {
859 assertInstall(ip.pkg, flags, ip.pkg.installLocation);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800860 }
861 } finally {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800862 cleanUpInstall(ip);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800863 }
864 }
865
Brett Chabotf76c56b2010-07-26 17:28:17 -0700866 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -0700867 public void testReplaceFailNormalInternal() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -0700868 sampleReplaceFromRawResource(0);
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 testReplaceFailFwdLockedInternal() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -0700873 sampleReplaceFromRawResource(PackageManager.INSTALL_FORWARD_LOCK);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800874 }
875
Brett Chabotf76c56b2010-07-26 17:28:17 -0700876 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -0700877 public void testReplaceFailSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -0800878 // Do not run on devices with emulated external storage.
879 if (Environment.isExternalStorageEmulated()) {
880 return;
881 }
882
Suchi Amalapurapuae181712010-03-30 14:01:02 -0700883 sampleReplaceFromRawResource(PackageManager.INSTALL_EXTERNAL);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800884 }
885
Brett Chabotf76c56b2010-07-26 17:28:17 -0700886 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -0700887 public void testReplaceNormalInternal() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -0700888 sampleReplaceFromRawResource(PackageManager.INSTALL_REPLACE_EXISTING);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800889 }
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800890
Brett Chabotf76c56b2010-07-26 17:28:17 -0700891 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -0700892 public void testReplaceFwdLockedInternal() throws Exception {
893 sampleReplaceFromRawResource(PackageManager.INSTALL_REPLACE_EXISTING
894 | PackageManager.INSTALL_FORWARD_LOCK);
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -0800895 }
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800896
Brett Chabotf76c56b2010-07-26 17:28:17 -0700897 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -0700898 public void testReplaceSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -0800899 // Do not run on devices with emulated external storage.
900 if (Environment.isExternalStorageEmulated()) {
901 return;
902 }
903
Kenny Root25c925e2012-09-08 22:02:21 -0700904 sampleReplaceFromRawResource(PackageManager.INSTALL_REPLACE_EXISTING
905 | PackageManager.INSTALL_EXTERNAL);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800906 }
907
Kenny Root7e921a12012-09-08 22:02:21 -0700908 /* -------------- Delete tests --- */
Kenny Root25c925e2012-09-08 22:02:21 -0700909 private static class DeleteObserver extends IPackageDeleteObserver.Stub {
Kenny Root7e921a12012-09-08 22:02:21 -0700910 private CountDownLatch mLatch = new CountDownLatch(1);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800911
Kenny Root7e921a12012-09-08 22:02:21 -0700912 private int mReturnCode;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800913
Kenny Root7e921a12012-09-08 22:02:21 -0700914 private final String mPackageName;
915
916 private String mObservedPackage;
917
918 public DeleteObserver(String packageName) {
919 mPackageName = packageName;
920 }
921
922 public boolean isSuccessful() {
923 return mReturnCode == PackageManager.DELETE_SUCCEEDED;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800924 }
925
Kenny Rootc39bb4a2011-02-28 13:27:19 -0800926 public void packageDeleted(String packageName, int returnCode) throws RemoteException {
Kenny Root7e921a12012-09-08 22:02:21 -0700927 mObservedPackage = packageName;
928
929 mReturnCode = returnCode;
930
931 mLatch.countDown();
932 }
933
934 public void waitForCompletion(long timeoutMillis) {
935 final long deadline = SystemClock.uptimeMillis() + timeoutMillis;
936
937 long waitTime = timeoutMillis;
938 while (waitTime > 0) {
939 try {
940 boolean done = mLatch.await(waitTime, TimeUnit.MILLISECONDS);
941 if (done) {
942 assertEquals(mPackageName, mObservedPackage);
943 return;
944 }
945 } catch (InterruptedException e) {
946 // TODO Auto-generated catch block
947 e.printStackTrace();
948 }
949 waitTime = deadline - SystemClock.uptimeMillis();
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800950 }
Kenny Root7e921a12012-09-08 22:02:21 -0700951
952 throw new AssertionError("Timeout waiting for package deletion");
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800953 }
954 }
955
956 class DeleteReceiver extends GenericReceiver {
957 String pkgName;
958
959 DeleteReceiver(String pkgName) {
960 this.pkgName = pkgName;
961 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_REMOVED);
962 filter.addDataScheme("package");
963 super.setFilter(filter);
964 }
965
966 public boolean notifyNow(Intent intent) {
967 String action = intent.getAction();
968 if (!Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
969 return false;
970 }
971 Uri data = intent.getData();
972 String installedPkg = data.getEncodedSchemeSpecificPart();
973 if (pkgName.equals(installedPkg)) {
974 return true;
975 }
976 return false;
977 }
978 }
979
Kenny Root7e921a12012-09-08 22:02:21 -0700980 public boolean invokeDeletePackage(final String pkgName, int flags, GenericReceiver receiver)
981 throws Exception {
982 ApplicationInfo info = getPm().getApplicationInfo(pkgName,
Amith Yamasani0d1fd8d2016-10-12 14:21:51 -0700983 PackageManager.MATCH_UNINSTALLED_PACKAGES);
Kenny Root7e921a12012-09-08 22:02:21 -0700984
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800985 mContext.registerReceiver(receiver, receiver.filter);
986 try {
Kenny Root7e921a12012-09-08 22:02:21 -0700987 DeleteObserver observer = new DeleteObserver(pkgName);
988
Kenny Roota3e90792012-10-18 10:58:36 -0700989 getPm().deletePackage(pkgName, observer, flags | PackageManager.DELETE_ALL_USERS);
Kenny Root7e921a12012-09-08 22:02:21 -0700990 observer.waitForCompletion(MAX_WAIT_TIME);
991
992 assertUninstalled(info);
993
994 // Verify we received the broadcast
Kenny Roota3e90792012-10-18 10:58:36 -0700995 // TODO replace this with a CountDownLatch
996 synchronized (receiver) {
997 long waitTime = 0;
998 while ((!receiver.isDone()) && (waitTime < MAX_WAIT_TIME)) {
999 receiver.wait(WAIT_TIME_INCR);
1000 waitTime += WAIT_TIME_INCR;
1001 }
1002 if (!receiver.isDone()) {
1003 throw new Exception("Timed out waiting for PACKAGE_REMOVED notification");
1004 }
Kenny Root7e921a12012-09-08 22:02:21 -07001005 }
1006 return receiver.received;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001007 } finally {
1008 mContext.unregisterReceiver(receiver);
1009 }
1010 }
1011
Kenny Root7e921a12012-09-08 22:02:21 -07001012 private static void assertUninstalled(ApplicationInfo info) throws Exception {
1013 File nativeLibraryFile = new File(info.nativeLibraryDir);
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08001014 assertFalse("Native library directory " + info.nativeLibraryDir
1015 + " should be erased", nativeLibraryFile.exists());
Kenny Root7e921a12012-09-08 22:02:21 -07001016 }
1017
Kenny Root25c925e2012-09-08 22:02:21 -07001018 public void deleteFromRawResource(int iFlags, int dFlags) throws Exception {
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -08001019 InstallParams ip = sampleInstallFromRawResource(iFlags, false);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001020 boolean retainData = ((dFlags & PackageManager.DELETE_KEEP_DATA) != 0);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001021 GenericReceiver receiver = new DeleteReceiver(ip.pkg.packageName);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001022 try {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07001023 assertTrue(invokeDeletePackage(ip.pkg.packageName, dFlags, receiver));
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001024 ApplicationInfo info = null;
1025 Log.i(TAG, "okay4");
1026 try {
Kenny Root25c925e2012-09-08 22:02:21 -07001027 info = getPm().getApplicationInfo(ip.pkg.packageName,
Amith Yamasani0d1fd8d2016-10-12 14:21:51 -07001028 PackageManager.MATCH_UNINSTALLED_PACKAGES);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001029 } catch (NameNotFoundException e) {
1030 info = null;
1031 }
1032 if (retainData) {
1033 assertNotNull(info);
1034 assertEquals(info.packageName, ip.pkg.packageName);
1035 File file = new File(info.dataDir);
1036 assertTrue(file.exists());
1037 } else {
1038 assertNull(info);
1039 }
1040 } catch (Exception e) {
1041 failStr(e);
1042 } finally {
1043 cleanUpInstall(ip);
1044 }
1045 }
1046
Brett Chabotf76c56b2010-07-26 17:28:17 -07001047 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001048 public void testDeleteNormalInternal() throws Exception {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001049 deleteFromRawResource(0, 0);
1050 }
1051
Brett Chabotf76c56b2010-07-26 17:28:17 -07001052 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001053 public void testDeleteFwdLockedInternal() throws Exception {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001054 deleteFromRawResource(PackageManager.INSTALL_FORWARD_LOCK, 0);
1055 }
1056
Brett Chabotf76c56b2010-07-26 17:28:17 -07001057 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001058 public void testDeleteSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001059 // Do not run on devices with emulated external storage.
1060 if (Environment.isExternalStorageEmulated()) {
1061 return;
1062 }
1063
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -08001064 deleteFromRawResource(PackageManager.INSTALL_EXTERNAL, 0);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001065 }
1066
Brett Chabotf76c56b2010-07-26 17:28:17 -07001067 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001068 public void testDeleteNormalInternalRetainData() throws Exception {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001069 deleteFromRawResource(0, PackageManager.DELETE_KEEP_DATA);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001070 }
1071
Brett Chabotf76c56b2010-07-26 17:28:17 -07001072 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001073 public void testDeleteFwdLockedInternalRetainData() throws Exception {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001074 deleteFromRawResource(PackageManager.INSTALL_FORWARD_LOCK, PackageManager.DELETE_KEEP_DATA);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001075 }
1076
Brett Chabotf76c56b2010-07-26 17:28:17 -07001077 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001078 public void testDeleteSdcardRetainData() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001079 // Do not run on devices with emulated external storage.
1080 if (Environment.isExternalStorageEmulated()) {
1081 return;
1082 }
1083
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001084 deleteFromRawResource(PackageManager.INSTALL_EXTERNAL, PackageManager.DELETE_KEEP_DATA);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001085 }
1086
Kenny Root25c925e2012-09-08 22:02:21 -07001087 /* sdcard mount/unmount tests ***** */
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001088
1089 class SdMountReceiver extends GenericReceiver {
1090 String pkgNames[];
Kenny Root25c925e2012-09-08 22:02:21 -07001091
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001092 boolean status = true;
1093
1094 SdMountReceiver(String[] pkgNames) {
1095 this.pkgNames = pkgNames;
1096 IntentFilter filter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
1097 super.setFilter(filter);
1098 }
1099
1100 public boolean notifyNow(Intent intent) {
1101 Log.i(TAG, "okay 1");
1102 String action = intent.getAction();
1103 if (!Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
1104 return false;
1105 }
1106 String rpkgList[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1107 for (String pkg : pkgNames) {
1108 boolean found = false;
1109 for (String rpkg : rpkgList) {
1110 if (rpkg.equals(pkg)) {
1111 found = true;
1112 break;
1113 }
1114 }
1115 if (!found) {
1116 status = false;
1117 return true;
1118 }
1119 }
1120 return true;
1121 }
1122 }
1123
1124 class SdUnMountReceiver extends GenericReceiver {
1125 String pkgNames[];
Kenny Root25c925e2012-09-08 22:02:21 -07001126
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001127 boolean status = true;
1128
1129 SdUnMountReceiver(String[] pkgNames) {
1130 this.pkgNames = pkgNames;
1131 IntentFilter filter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
1132 super.setFilter(filter);
1133 }
1134
1135 public boolean notifyNow(Intent intent) {
1136 String action = intent.getAction();
1137 if (!Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1138 return false;
1139 }
1140 String rpkgList[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1141 for (String pkg : pkgNames) {
1142 boolean found = false;
1143 for (String rpkg : rpkgList) {
1144 if (rpkg.equals(pkg)) {
1145 found = true;
1146 break;
1147 }
1148 }
1149 if (!found) {
1150 status = false;
1151 return true;
1152 }
1153 }
1154 return true;
1155 }
1156 }
1157
Sudheer Shanka2250d562016-11-07 15:41:02 -08001158 IStorageManager getSm() {
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001159 IBinder service = ServiceManager.getService("mount");
1160 if (service != null) {
Sudheer Shanka2250d562016-11-07 15:41:02 -08001161 return IStorageManager.Stub.asInterface(service);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001162 } else {
Sudheer Shanka2250d562016-11-07 15:41:02 -08001163 Log.e(TAG, "Can't get storagemanager service");
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001164 }
1165 return null;
1166 }
1167
Kenny Rootd7b421b2010-08-05 08:40:00 -07001168 boolean checkMediaState(String desired) {
Jeff Sharkey8452d942015-09-17 10:16:43 -07001169 String actual = Environment.getExternalStorageState();
1170 if (desired.equals(actual)) {
1171 return true;
1172 } else {
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) {
Sudheer Shanka2250d562016-11-07 15:41:02 -08001194 int ret = getSm().mountVolume(path);
Kenny Rootd7b421b2010-08-05 08:40:00 -07001195 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) {
Sudheer Shanka2250d562016-11-07 15:41:02 -08001233 getSm().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 try {
Todd Kennedycf901d72017-05-16 09:10:07 -07001327 cleanUpInstall(ip.pkg.packageName);
Kenny Root7e921a12012-09-08 22:02:21 -07001328 } finally {
Jeff Sharkeyc4858a22014-06-16 10:51:20 -07001329 File outFile = new File(ip.pkg.codePath);
Kenny Root7e921a12012-09-08 22:02:21 -07001330 if (outFile != null && outFile.exists()) {
1331 outFile.delete();
1332 }
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001333 }
1334 }
Kenny Root25c925e2012-09-08 22:02:21 -07001335
1336 private void cleanUpInstall(String pkgName) throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07001337 if (pkgName == null) {
1338 return;
1339 }
1340 Log.i(TAG, "Deleting package : " + pkgName);
1341 try {
Todd Kennedycf901d72017-05-16 09:10:07 -07001342 final ApplicationInfo info = getPm().getApplicationInfo(pkgName,
Amith Yamasani0d1fd8d2016-10-12 14:21:51 -07001343 PackageManager.MATCH_UNINSTALLED_PACKAGES);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07001344 if (info != null) {
Kenny Root7e921a12012-09-08 22:02:21 -07001345 DeleteObserver observer = new DeleteObserver(pkgName);
Kenny Roota3e90792012-10-18 10:58:36 -07001346 getPm().deletePackage(pkgName, observer, PackageManager.DELETE_ALL_USERS);
Kenny Root7e921a12012-09-08 22:02:21 -07001347 observer.waitForCompletion(MAX_WAIT_TIME);
1348 assertUninstalled(info);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07001349 }
Todd Kennedycf901d72017-05-16 09:10:07 -07001350 } catch (IllegalArgumentException | NameNotFoundException e) {
Kenny Root25c925e2012-09-08 22:02:21 -07001351 }
Suchi Amalapurapuae181712010-03-30 14:01:02 -07001352 }
Suchi Amalapurapu117818e2010-02-09 03:45:40 -08001353
Brett Chabotf76c56b2010-07-26 17:28:17 -07001354 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001355 public void testManifestInstallLocationInternal() throws Exception {
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -08001356 installFromRawResource("install.apk", R.raw.install_loc_internal,
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001357 0, true, false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
Suchi Amalapurapu117818e2010-02-09 03:45:40 -08001358 }
1359
Brett Chabotf76c56b2010-07-26 17:28:17 -07001360 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001361 public void testManifestInstallLocationSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001362 // Do not run on devices with emulated external storage.
1363 if (Environment.isExternalStorageEmulated()) {
1364 return;
1365 }
1366
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -08001367 installFromRawResource("install.apk", R.raw.install_loc_sdcard,
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001368 0, true, false, -1, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
Suchi Amalapurapu117818e2010-02-09 03:45:40 -08001369 }
1370
Brett Chabotf76c56b2010-07-26 17:28:17 -07001371 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001372 public void testManifestInstallLocationAuto() throws Exception {
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -08001373 installFromRawResource("install.apk", R.raw.install_loc_auto,
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -08001374 0, true, false, -1, PackageInfo.INSTALL_LOCATION_AUTO);
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 testManifestInstallLocationUnspecified() throws Exception {
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -08001379 installFromRawResource("install.apk", R.raw.install_loc_unspecified,
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -07001380 0, true, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
Suchi Amalapurapu117818e2010-02-09 03:45:40 -08001381 }
1382
Brett Chabotf76c56b2010-07-26 17:28:17 -07001383 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001384 public void testManifestInstallLocationFwdLockedFlagSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001385 // Do not run on devices with emulated external storage.
1386 if (Environment.isExternalStorageEmulated()) {
1387 return;
1388 }
1389
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001390 installFromRawResource("install.apk", R.raw.install_loc_unspecified,
1391 PackageManager.INSTALL_FORWARD_LOCK |
Kenny Rootbf023582012-05-02 16:56:15 -07001392 PackageManager.INSTALL_EXTERNAL, true, false, -1,
1393 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001394 }
1395
Brett Chabotf76c56b2010-07-26 17:28:17 -07001396 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001397 public void testManifestInstallLocationFwdLockedSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001398 // Do not run on devices with emulated external storage.
1399 if (Environment.isExternalStorageEmulated()) {
1400 return;
1401 }
1402
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -08001403 installFromRawResource("install.apk", R.raw.install_loc_sdcard,
Kenny Root25c925e2012-09-08 22:02:21 -07001404 PackageManager.INSTALL_FORWARD_LOCK, true, false, -1,
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001405 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
1406 }
1407
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001408 /*
1409 * Install a package on internal flash via PackageManager install flag. Replace
1410 * the package via flag to install on sdcard. Make sure the new flag overrides
1411 * the old install location.
1412 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07001413 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001414 public void testReplaceFlagInternalSdcard() 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 Amalapurapu8a9ab242010-03-11 16:49:16 -08001420 int iFlags = 0;
1421 int rFlags = PackageManager.INSTALL_EXTERNAL;
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001422 InstallParams ip = sampleInstallFromRawResource(iFlags, false);
1423 GenericReceiver receiver = new ReplaceReceiver(ip.pkg.packageName);
1424 int replaceFlags = rFlags | PackageManager.INSTALL_REPLACE_EXISTING;
1425 try {
Kenny Rootb7c24702012-04-14 19:46:09 -07001426 invokeInstallPackage(ip.packageURI, replaceFlags, receiver, true);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001427 assertInstall(ip.pkg, rFlags, ip.pkg.installLocation);
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001428 } catch (Exception e) {
1429 failStr("Failed with exception : " + e);
1430 } finally {
1431 cleanUpInstall(ip);
1432 }
1433 }
1434
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001435 /*
1436 * Install a package on sdcard via PackageManager install flag. Replace
1437 * the package with no flags or manifest option and make sure the old
1438 * install location is retained.
1439 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07001440 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001441 public void testReplaceFlagSdcardInternal() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001442 // Do not run on devices with emulated external storage.
1443 if (Environment.isExternalStorageEmulated()) {
1444 return;
1445 }
1446
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001447 int iFlags = PackageManager.INSTALL_EXTERNAL;
1448 int rFlags = 0;
1449 InstallParams ip = sampleInstallFromRawResource(iFlags, false);
1450 GenericReceiver receiver = new ReplaceReceiver(ip.pkg.packageName);
1451 int replaceFlags = rFlags | PackageManager.INSTALL_REPLACE_EXISTING;
1452 try {
Kenny Rootb7c24702012-04-14 19:46:09 -07001453 invokeInstallPackage(ip.packageURI, replaceFlags, receiver, true);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001454 assertInstall(ip.pkg, iFlags, ip.pkg.installLocation);
1455 } catch (Exception e) {
1456 failStr("Failed with exception : " + e);
1457 } finally {
1458 cleanUpInstall(ip);
1459 }
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001460 }
1461
Brett Chabotf76c56b2010-07-26 17:28:17 -07001462 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001463 public void testManifestInstallLocationReplaceInternalSdcard() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001464 // Do not run on devices with emulated external storage.
1465 if (Environment.isExternalStorageEmulated()) {
1466 return;
1467 }
1468
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001469 int iFlags = 0;
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -07001470 int iApk = R.raw.install_loc_internal;
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001471 int rFlags = 0;
1472 int rApk = R.raw.install_loc_sdcard;
1473 InstallParams ip = installFromRawResource("install.apk", iApk,
1474 iFlags, false,
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -07001475 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001476 GenericReceiver receiver = new ReplaceReceiver(ip.pkg.packageName);
1477 int replaceFlags = rFlags | PackageManager.INSTALL_REPLACE_EXISTING;
1478 try {
1479 InstallParams rp = installFromRawResource("install.apk", rApk,
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001480 replaceFlags, false,
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001481 false, -1, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
1482 assertInstall(rp.pkg, replaceFlags, rp.pkg.installLocation);
1483 } catch (Exception e) {
1484 failStr("Failed with exception : " + e);
1485 } finally {
1486 cleanUpInstall(ip);
1487 }
1488 }
1489
Brett Chabotf76c56b2010-07-26 17:28:17 -07001490 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001491 public void testManifestInstallLocationReplaceSdcardInternal() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001492 // Do not run on devices with emulated external storage.
1493 if (Environment.isExternalStorageEmulated()) {
1494 return;
1495 }
1496
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001497 int iFlags = 0;
1498 int iApk = R.raw.install_loc_sdcard;
1499 int rFlags = 0;
1500 int rApk = R.raw.install_loc_unspecified;
1501 InstallParams ip = installFromRawResource("install.apk", iApk,
1502 iFlags, false,
1503 false, -1, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001504 int replaceFlags = rFlags | PackageManager.INSTALL_REPLACE_EXISTING;
1505 try {
1506 InstallParams rp = installFromRawResource("install.apk", rApk,
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001507 replaceFlags, false,
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001508 false, -1, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
1509 assertInstall(rp.pkg, replaceFlags, ip.pkg.installLocation);
1510 } catch (Exception e) {
1511 failStr("Failed with exception : " + e);
1512 } finally {
1513 cleanUpInstall(ip);
1514 }
Suchi Amalapurapu117818e2010-02-09 03:45:40 -08001515 }
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001516
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001517 class MoveReceiver extends GenericReceiver {
1518 String pkgName;
Kenny Root25c925e2012-09-08 22:02:21 -07001519
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001520 final static int INVALID = -1;
Kenny Root25c925e2012-09-08 22:02:21 -07001521
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001522 final static int REMOVED = 1;
Kenny Root25c925e2012-09-08 22:02:21 -07001523
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001524 final static int ADDED = 2;
Kenny Root25c925e2012-09-08 22:02:21 -07001525
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001526 int removed = INVALID;
1527
1528 MoveReceiver(String pkgName) {
1529 this.pkgName = pkgName;
1530 filter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
1531 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
1532 super.setFilter(filter);
1533 }
1534
1535 public boolean notifyNow(Intent intent) {
1536 String action = intent.getAction();
1537 Log.i(TAG, "MoveReceiver::" + action);
1538 if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1539 String[] list = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1540 if (list != null) {
1541 for (String pkg : list) {
1542 if (pkg.equals(pkgName)) {
1543 removed = REMOVED;
1544 break;
1545 }
1546 }
1547 }
1548 removed = REMOVED;
1549 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
1550 if (removed != REMOVED) {
1551 return false;
1552 }
1553 String[] list = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1554 if (list != null) {
1555 for (String pkg : list) {
1556 if (pkg.equals(pkgName)) {
1557 removed = ADDED;
1558 return true;
1559 }
1560 }
1561 }
1562 }
1563 return false;
1564 }
1565 }
1566
Kenny Root25c925e2012-09-08 22:02:21 -07001567 public boolean invokeMovePackage(String pkgName, int flags, GenericReceiver receiver)
1568 throws Exception {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001569 throw new UnsupportedOperationException();
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001570 }
Kenny Root25c925e2012-09-08 22:02:21 -07001571
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001572 private boolean invokeMovePackageFail(String pkgName, int flags, int errCode) throws Exception {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001573 throw new UnsupportedOperationException();
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001574 }
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001575
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07001576 private int getDefaultInstallLoc() {
Suchi Amalapurapu9b10ef52010-03-03 09:45:24 -08001577 int origDefaultLoc = PackageInfo.INSTALL_LOCATION_AUTO;
1578 try {
Jeff Sharkey625239a2012-09-26 22:03:49 -07001579 origDefaultLoc = Settings.Global.getInt(mContext.getContentResolver(),
1580 Settings.Global.DEFAULT_INSTALL_LOCATION);
Suchi Amalapurapu9b10ef52010-03-03 09:45:24 -08001581 } catch (SettingNotFoundException e1) {
1582 }
1583 return origDefaultLoc;
1584 }
1585
1586 private void setInstallLoc(int loc) {
Jeff Sharkey625239a2012-09-26 22:03:49 -07001587 Settings.Global.putInt(mContext.getContentResolver(),
1588 Settings.Global.DEFAULT_INSTALL_LOCATION, loc);
Suchi Amalapurapu9b10ef52010-03-03 09:45:24 -08001589 }
Kenny Root25c925e2012-09-08 22:02:21 -07001590
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001591 /*
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001592 * Tests for moving apps between internal and external storage
1593 */
1594 /*
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001595 * Utility function that reads a apk bundled as a raw resource
1596 * copies it into own data directory and invokes
1597 * PackageManager api to install first and then replace it
1598 * again.
1599 */
Neal Nguyenedb979a2010-04-22 13:54:32 -07001600
Kenny Root25c925e2012-09-08 22:02:21 -07001601 private void moveFromRawResource(String outFileName, int rawResId, int installFlags,
1602 int moveFlags, boolean cleanUp, boolean fail, int result) throws Exception {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07001603 int origDefaultLoc = getDefaultInstallLoc();
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001604 InstallParams ip = null;
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001605 try {
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001606 setInstallLoc(PackageHelper.APP_INSTALL_AUTO);
1607 // Install first
1608 ip = installFromRawResource("install.apk", rawResId, installFlags, false,
1609 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
1610 ApplicationInfo oldAppInfo = getPm().getApplicationInfo(ip.pkg.packageName, 0);
1611 if (fail) {
1612 assertTrue(invokeMovePackageFail(ip.pkg.packageName, moveFlags, result));
1613 ApplicationInfo info = getPm().getApplicationInfo(ip.pkg.packageName, 0);
1614 assertNotNull(info);
1615 assertEquals(oldAppInfo.flags, info.flags);
1616 } else {
1617 // Create receiver based on expRetCode
1618 MoveReceiver receiver = new MoveReceiver(ip.pkg.packageName);
Kenny Root25c925e2012-09-08 22:02:21 -07001619 boolean retCode = invokeMovePackage(ip.pkg.packageName, moveFlags, receiver);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001620 assertTrue(retCode);
1621 ApplicationInfo info = getPm().getApplicationInfo(ip.pkg.packageName, 0);
Kenny Root6a6b0072010-10-07 16:46:10 -07001622 assertNotNull("ApplicationInfo for recently installed application should exist",
1623 info);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001624 if ((moveFlags & PackageManager.MOVE_INTERNAL) != 0) {
Kenny Root6a6b0072010-10-07 16:46:10 -07001625 assertTrue("ApplicationInfo.FLAG_EXTERNAL_STORAGE flag should NOT be set",
1626 (info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == 0);
Kenny Root7e921a12012-09-08 22:02:21 -07001627 assertStartsWith("Native library dir should be in dataDir",
1628 info.dataDir, info.nativeLibraryDir);
1629 } else if ((moveFlags & PackageManager.MOVE_EXTERNAL_MEDIA) != 0) {
Kenny Root6a6b0072010-10-07 16:46:10 -07001630 assertTrue("ApplicationInfo.FLAG_EXTERNAL_STORAGE flag should be set",
1631 (info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0);
Kenny Root7e921a12012-09-08 22:02:21 -07001632 assertStartsWith("Native library dir should point to ASEC",
1633 SECURE_CONTAINERS_PREFIX, info.nativeLibraryDir);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001634 }
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001635 }
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001636 } catch (NameNotFoundException e) {
1637 failStr("Pkg hasnt been installed correctly");
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001638 } finally {
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001639 if (ip != null) {
1640 cleanUpInstall(ip);
1641 }
Suchi Amalapurapu9b10ef52010-03-03 09:45:24 -08001642 // Restore default install location
1643 setInstallLoc(origDefaultLoc);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001644 }
1645 }
Kenny Root25c925e2012-09-08 22:02:21 -07001646
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001647 private void sampleMoveFromRawResource(int installFlags, int moveFlags, boolean fail,
Kenny Root25c925e2012-09-08 22:02:21 -07001648 int result) throws Exception {
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001649 moveFromRawResource("install.apk",
1650 R.raw.install, installFlags, moveFlags, true,
1651 fail, result);
1652 }
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001653
Brett Chabotf76c56b2010-07-26 17:28:17 -07001654 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001655 public void testMoveAppInternalToExternal() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001656 // Do not run on devices with emulated external storage.
1657 if (Environment.isExternalStorageEmulated()) {
1658 return;
1659 }
1660
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001661 int installFlags = PackageManager.INSTALL_INTERNAL;
1662 int moveFlags = PackageManager.MOVE_EXTERNAL_MEDIA;
1663 boolean fail = false;
1664 int result = PackageManager.MOVE_SUCCEEDED;
1665 sampleMoveFromRawResource(installFlags, moveFlags, fail, result);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001666 }
1667
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08001668 @Suppress
Brett Chabotf76c56b2010-07-26 17:28:17 -07001669 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001670 public void testMoveAppInternalToInternal() throws Exception {
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001671 int installFlags = PackageManager.INSTALL_INTERNAL;
1672 int moveFlags = PackageManager.MOVE_INTERNAL;
1673 boolean fail = true;
1674 int result = PackageManager.MOVE_FAILED_INVALID_LOCATION;
1675 sampleMoveFromRawResource(installFlags, moveFlags, fail, result);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001676 }
1677
Brett Chabotf76c56b2010-07-26 17:28:17 -07001678 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001679 public void testMoveAppExternalToExternal() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001680 // Do not run on devices with emulated external storage.
1681 if (Environment.isExternalStorageEmulated()) {
1682 return;
1683 }
1684
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001685 int installFlags = PackageManager.INSTALL_EXTERNAL;
1686 int moveFlags = PackageManager.MOVE_EXTERNAL_MEDIA;
1687 boolean fail = true;
1688 int result = PackageManager.MOVE_FAILED_INVALID_LOCATION;
1689 sampleMoveFromRawResource(installFlags, moveFlags, fail, result);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001690 }
Kenny Root3c676892011-01-12 10:58:40 -08001691
Brett Chabotf76c56b2010-07-26 17:28:17 -07001692 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001693 public void testMoveAppExternalToInternal() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001694 // Do not run on devices with emulated external storage.
1695 if (Environment.isExternalStorageEmulated()) {
1696 return;
1697 }
1698
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001699 int installFlags = PackageManager.INSTALL_EXTERNAL;
1700 int moveFlags = PackageManager.MOVE_INTERNAL;
1701 boolean fail = false;
1702 int result = PackageManager.MOVE_SUCCEEDED;
1703 sampleMoveFromRawResource(installFlags, moveFlags, fail, result);
Suchi Amalapurapu8946dd32010-02-19 09:19:34 -08001704 }
Kenny Root3c676892011-01-12 10:58:40 -08001705
Brett Chabotf76c56b2010-07-26 17:28:17 -07001706 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001707 public void testMoveAppForwardLocked() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001708 // Do not run on devices with emulated external storage.
1709 if (Environment.isExternalStorageEmulated()) {
1710 return;
1711 }
1712
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001713 int installFlags = PackageManager.INSTALL_FORWARD_LOCK;
1714 int moveFlags = PackageManager.MOVE_EXTERNAL_MEDIA;
Kenny Rootbf023582012-05-02 16:56:15 -07001715 boolean fail = false;
1716 int result = PackageManager.MOVE_SUCCEEDED;
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001717 sampleMoveFromRawResource(installFlags, moveFlags, fail, result);
1718 }
Kenny Root3c676892011-01-12 10:58:40 -08001719
Brett Chabotf76c56b2010-07-26 17:28:17 -07001720 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001721 public void testMoveAppFailInternalToExternalDelete() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001722 // Do not run on devices with emulated external storage.
1723 if (Environment.isExternalStorageEmulated()) {
1724 return;
1725 }
1726
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001727 int installFlags = 0;
1728 int moveFlags = PackageManager.MOVE_EXTERNAL_MEDIA;
1729 boolean fail = true;
1730 final int result = PackageManager.MOVE_FAILED_DOESNT_EXIST;
Neal Nguyenedb979a2010-04-22 13:54:32 -07001731
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001732 int rawResId = R.raw.install;
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07001733 int origDefaultLoc = getDefaultInstallLoc();
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001734 InstallParams ip = null;
1735 try {
1736 PackageManager pm = getPm();
1737 setInstallLoc(PackageHelper.APP_INSTALL_AUTO);
1738 // Install first
1739 ip = installFromRawResource("install.apk", R.raw.install, installFlags, false,
1740 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
1741 // Delete the package now retaining data.
Suchi Amalapurapu9a212ad2010-05-18 11:06:53 -07001742 GenericReceiver receiver = new DeleteReceiver(ip.pkg.packageName);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001743 invokeDeletePackage(ip.pkg.packageName, PackageManager.DELETE_KEEP_DATA, receiver);
Suchi Amalapurapu30f775b2010-04-06 11:41:23 -07001744 assertTrue(invokeMovePackageFail(ip.pkg.packageName, moveFlags, result));
1745 } catch (Exception e) {
1746 failStr(e);
1747 } finally {
1748 if (ip != null) {
1749 cleanUpInstall(ip);
1750 }
1751 // Restore default install location
1752 setInstallLoc(origDefaultLoc);
1753 }
1754 }
Kenny Root3c676892011-01-12 10:58:40 -08001755
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001756 /*
1757 * Test that an install error code is returned when media is unmounted
1758 * and package installed on sdcard via package manager flag.
1759 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07001760 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001761 public void testInstallSdcardUnmount() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001762 // Do not run on devices with emulated external storage.
1763 if (Environment.isExternalStorageEmulated()) {
1764 return;
1765 }
1766
Kenny Rootd7b421b2010-08-05 08:40:00 -07001767 boolean origState = checkMediaState(Environment.MEDIA_MOUNTED);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001768 try {
1769 // Unmount sdcard
1770 assertTrue(unmountMedia());
1771 // Try to install and make sure an error code is returned.
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07001772 installFromRawResource("install.apk", R.raw.install,
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001773 PackageManager.INSTALL_EXTERNAL, false,
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -07001774 true, PackageManager.INSTALL_FAILED_MEDIA_UNAVAILABLE,
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07001775 PackageInfo.INSTALL_LOCATION_AUTO);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001776 } finally {
1777 // Restore original media state
1778 if (origState) {
1779 mountMedia();
1780 } else {
1781 unmountMedia();
1782 }
1783 }
1784 }
1785
1786 /*
Neal Nguyenedb979a2010-04-22 13:54:32 -07001787 * Unmount sdcard. Try installing an app with manifest option to install
1788 * on sdcard. Make sure it gets installed on internal flash.
1789 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07001790 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001791 public void testInstallManifestSdcardUnmount() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001792 // Do not run on devices with emulated external storage.
1793 if (Environment.isExternalStorageEmulated()) {
1794 return;
1795 }
1796
Kenny Rootd7b421b2010-08-05 08:40:00 -07001797 boolean origState = checkMediaState(Environment.MEDIA_MOUNTED);
1798 try {
1799 // Unmount sdcard
1800 assertTrue(unmountMedia());
1801 InstallParams ip = new InstallParams("install.apk", R.raw.install_loc_sdcard);
1802 installFromRawResource(ip, 0, true, false, -1,
1803 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
1804 } finally {
1805 // Restore original media state
1806 if (origState) {
1807 mountMedia();
1808 } else {
1809 unmountMedia();
1810 }
1811 }
1812 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001813
Kenny Root25c925e2012-09-08 22:02:21 -07001814 /*---------- Recommended install location tests ----*/
1815 /*
1816 * PrecedenceSuffixes:
1817 * Flag : FlagI, FlagE, FlagF
1818 * I - internal, E - external, F - forward locked, Flag suffix absent if not using any option.
1819 * Manifest: ManifestI, ManifestE, ManifestA, Manifest suffix absent if not using any option.
1820 * Existing: Existing suffix absent if not existing.
1821 * User: UserI, UserE, UserA, User suffix absent if not existing.
1822 *
1823 */
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -07001824
Kenny Root3c676892011-01-12 10:58:40 -08001825 /*
1826 * Install an app on internal flash
1827 */
1828 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001829 public void testFlagI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001830 sampleInstallFromRawResource(PackageManager.INSTALL_INTERNAL, true);
1831 }
Suchi Amalapurapu14b6abd2010-03-17 08:37:04 -07001832
Kenny Root3c676892011-01-12 10:58:40 -08001833 /*
1834 * Install an app on sdcard.
1835 */
1836 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001837 public void testFlagE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001838 // Do not run on devices with emulated external storage.
1839 if (Environment.isExternalStorageEmulated()) {
1840 return;
1841 }
1842
1843 sampleInstallFromRawResource(PackageManager.INSTALL_EXTERNAL, true);
1844 }
1845
1846 /*
1847 * Install an app forward-locked.
1848 */
1849 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001850 public void testFlagF() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001851 sampleInstallFromRawResource(PackageManager.INSTALL_FORWARD_LOCK, true);
1852 }
1853
1854 /*
1855 * Install an app with both internal and external flags set. should fail
1856 */
1857 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001858 public void testFlagIE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001859 installFromRawResource("install.apk", R.raw.install,
1860 PackageManager.INSTALL_EXTERNAL | PackageManager.INSTALL_INTERNAL,
1861 false,
1862 true, PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION,
1863 PackageInfo.INSTALL_LOCATION_AUTO);
1864 }
1865
1866 /*
1867 * Install an app with both internal and forward-lock flags set.
1868 */
1869 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001870 public void testFlagIF() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001871 sampleInstallFromRawResource(PackageManager.INSTALL_FORWARD_LOCK
1872 | PackageManager.INSTALL_INTERNAL, true);
1873 }
1874
1875 /*
Kenny Root6dceb882012-04-12 14:23:49 -07001876 * Install an app with both external and forward-lock flags set.
Kenny Root3c676892011-01-12 10:58:40 -08001877 */
1878 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001879 public void testFlagEF() throws Exception {
Kenny Root6dceb882012-04-12 14:23:49 -07001880 // Do not run on devices with emulated external storage.
1881 if (Environment.isExternalStorageEmulated()) {
1882 return;
1883 }
1884
1885 sampleInstallFromRawResource(PackageManager.INSTALL_FORWARD_LOCK
1886 | PackageManager.INSTALL_EXTERNAL, true);
Kenny Root3c676892011-01-12 10:58:40 -08001887 }
1888
1889 /*
1890 * Install an app with both internal and external flags set with forward
1891 * lock. Should fail.
1892 */
1893 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001894 public void testFlagIEF() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001895 installFromRawResource("install.apk", R.raw.install,
1896 PackageManager.INSTALL_FORWARD_LOCK | PackageManager.INSTALL_INTERNAL |
1897 PackageManager.INSTALL_EXTERNAL,
1898 false,
1899 true, PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION,
1900 PackageInfo.INSTALL_LOCATION_AUTO);
1901 }
1902
Kenny Root25c925e2012-09-08 22:02:21 -07001903 /*
1904 * Install an app with both internal and manifest option set.
1905 * should install on internal.
1906 */
1907 @LargeTest
1908 public void testFlagIManifestI() throws Exception {
1909 installFromRawResource("install.apk", R.raw.install_loc_internal,
1910 PackageManager.INSTALL_INTERNAL,
1911 true,
1912 false, -1,
1913 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
1914 }
1915 /*
1916 * Install an app with both internal and manifest preference for
1917 * preferExternal. Should install on internal.
1918 */
1919 @LargeTest
1920 public void testFlagIManifestE() throws Exception {
1921 installFromRawResource("install.apk", R.raw.install_loc_sdcard,
1922 PackageManager.INSTALL_INTERNAL,
1923 true,
1924 false, -1,
1925 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
1926 }
1927 /*
1928 * Install an app with both internal and manifest preference for
1929 * auto. should install internal.
1930 */
1931 @LargeTest
1932 public void testFlagIManifestA() throws Exception {
1933 installFromRawResource("install.apk", R.raw.install_loc_auto,
1934 PackageManager.INSTALL_INTERNAL,
1935 true,
1936 false, -1,
1937 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
1938 }
1939 /*
1940 * Install an app with both external and manifest option set.
1941 * should install externally.
1942 */
1943 @LargeTest
1944 public void testFlagEManifestI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001945 // Do not run on devices with emulated external storage.
1946 if (Environment.isExternalStorageEmulated()) {
1947 return;
1948 }
1949
Kenny Root25c925e2012-09-08 22:02:21 -07001950 installFromRawResource("install.apk", R.raw.install_loc_internal,
1951 PackageManager.INSTALL_EXTERNAL,
1952 true,
1953 false, -1,
1954 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
1955 }
Kenny Root3c676892011-01-12 10:58:40 -08001956
Kenny Root25c925e2012-09-08 22:02:21 -07001957 /*
1958 * Install an app with both external and manifest preference for
1959 * preferExternal. Should install externally.
1960 */
1961 @LargeTest
1962 public void testFlagEManifestE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001963 // Do not run on devices with emulated external storage.
1964 if (Environment.isExternalStorageEmulated()) {
1965 return;
1966 }
1967
1968 installFromRawResource("install.apk", R.raw.install_loc_sdcard,
1969 PackageManager.INSTALL_EXTERNAL,
1970 true,
1971 false, -1,
1972 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
1973 }
1974
1975 /*
1976 * Install an app with both external and manifest preference for
1977 * auto. should install on external media.
1978 */
1979 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001980 public void testFlagEManifestA() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001981 // Do not run on devices with emulated external storage.
1982 if (Environment.isExternalStorageEmulated()) {
1983 return;
1984 }
1985
1986 installFromRawResource("install.apk", R.raw.install_loc_auto,
1987 PackageManager.INSTALL_EXTERNAL,
1988 true,
1989 false, -1,
1990 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
1991 }
1992
1993 /*
1994 * Install an app with fwd locked flag set and install location set to
1995 * internal. should install internally.
1996 */
1997 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07001998 public void testFlagFManifestI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08001999 installFromRawResource("install.apk", R.raw.install_loc_internal,
2000 PackageManager.INSTALL_FORWARD_LOCK,
2001 true,
2002 false, -1,
Kenny Root6dceb882012-04-12 14:23:49 -07002003 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
Kenny Root3c676892011-01-12 10:58:40 -08002004 }
2005
2006 /*
2007 * Install an app with fwd locked flag set and install location set to
Kenny Root6dceb882012-04-12 14:23:49 -07002008 * preferExternal. Should install externally.
Kenny Root3c676892011-01-12 10:58:40 -08002009 */
2010 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002011 public void testFlagFManifestE() throws Exception {
Kenny Root6dceb882012-04-12 14:23:49 -07002012 // Do not run on devices with emulated external storage.
2013 if (Environment.isExternalStorageEmulated()) {
2014 return;
2015 }
2016
Kenny Root3c676892011-01-12 10:58:40 -08002017 installFromRawResource("install.apk", R.raw.install_loc_sdcard,
2018 PackageManager.INSTALL_FORWARD_LOCK,
2019 true,
2020 false, -1,
2021 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2022 }
2023
2024 /*
Kenny Root6dceb882012-04-12 14:23:49 -07002025 * Install an app with fwd locked flag set and install location set to auto.
2026 * should install externally.
Kenny Root3c676892011-01-12 10:58:40 -08002027 */
2028 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002029 public void testFlagFManifestA() throws Exception {
Kenny Root6dceb882012-04-12 14:23:49 -07002030 // Do not run on devices with emulated external storage.
2031 if (Environment.isExternalStorageEmulated()) {
2032 return;
2033 }
2034
Kenny Root3c676892011-01-12 10:58:40 -08002035 installFromRawResource("install.apk", R.raw.install_loc_auto,
2036 PackageManager.INSTALL_FORWARD_LOCK,
2037 true,
2038 false, -1,
Kenny Rootbf023582012-05-02 16:56:15 -07002039 PackageInfo.INSTALL_LOCATION_AUTO);
Kenny Root3c676892011-01-12 10:58:40 -08002040 }
2041
Kenny Root25c925e2012-09-08 22:02:21 -07002042 /*
2043 * The following test functions verify install location for existing apps.
Kenny Root3c676892011-01-12 10:58:40 -08002044 * ie existing app can be installed internally or externally. If install
2045 * flag is explicitly set it should override current location. If manifest location
2046 * is set, that should over ride current location too. if not the existing install
2047 * location should be honoured.
2048 * testFlagI/E/F/ExistingI/E -
2049 */
2050 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002051 public void testFlagIExistingI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002052 int iFlags = PackageManager.INSTALL_INTERNAL;
2053 int rFlags = PackageManager.INSTALL_INTERNAL | PackageManager.INSTALL_REPLACE_EXISTING;
2054 // First install.
2055 installFromRawResource("install.apk", R.raw.install,
2056 iFlags,
2057 false,
2058 false, -1,
2059 -1);
2060 // Replace now
2061 installFromRawResource("install.apk", R.raw.install,
2062 rFlags,
2063 true,
2064 false, -1,
2065 -1);
2066 }
2067
2068 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002069 public void testFlagIExistingE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002070 // Do not run on devices with emulated external storage.
2071 if (Environment.isExternalStorageEmulated()) {
2072 return;
2073 }
2074
2075 int iFlags = PackageManager.INSTALL_EXTERNAL;
2076 int rFlags = PackageManager.INSTALL_INTERNAL | PackageManager.INSTALL_REPLACE_EXISTING;
2077 // First install.
2078 installFromRawResource("install.apk", R.raw.install,
2079 iFlags,
2080 false,
2081 false, -1,
2082 -1);
2083 // Replace now
2084 installFromRawResource("install.apk", R.raw.install,
2085 rFlags,
2086 true,
2087 false, -1,
2088 -1);
2089 }
2090
2091 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002092 public void testFlagEExistingI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002093 // Do not run on devices with emulated external storage.
2094 if (Environment.isExternalStorageEmulated()) {
2095 return;
2096 }
2097
2098 int iFlags = PackageManager.INSTALL_INTERNAL;
2099 int rFlags = PackageManager.INSTALL_EXTERNAL | PackageManager.INSTALL_REPLACE_EXISTING;
2100 // First install.
2101 installFromRawResource("install.apk", R.raw.install,
2102 iFlags,
2103 false,
2104 false, -1,
2105 -1);
2106 // Replace now
2107 installFromRawResource("install.apk", R.raw.install,
2108 rFlags,
2109 true,
2110 false, -1,
2111 -1);
2112 }
2113
2114 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002115 public void testFlagEExistingE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002116 // Do not run on devices with emulated external storage.
2117 if (Environment.isExternalStorageEmulated()) {
2118 return;
2119 }
2120
2121 int iFlags = PackageManager.INSTALL_EXTERNAL;
2122 int rFlags = PackageManager.INSTALL_EXTERNAL | PackageManager.INSTALL_REPLACE_EXISTING;
2123 // First install.
2124 installFromRawResource("install.apk", R.raw.install,
2125 iFlags,
2126 false,
2127 false, -1,
2128 -1);
2129 // Replace now
2130 installFromRawResource("install.apk", R.raw.install,
2131 rFlags,
2132 true,
2133 false, -1,
2134 -1);
2135 }
2136
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08002137 @Suppress
Kenny Root3c676892011-01-12 10:58:40 -08002138 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002139 public void testFlagFExistingI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002140 int iFlags = PackageManager.INSTALL_INTERNAL;
2141 int rFlags = PackageManager.INSTALL_FORWARD_LOCK | PackageManager.INSTALL_REPLACE_EXISTING;
2142 // First install.
2143 installFromRawResource("install.apk", R.raw.install,
2144 iFlags,
2145 false,
2146 false, -1,
2147 -1);
2148 // Replace now
2149 installFromRawResource("install.apk", R.raw.install,
2150 rFlags,
2151 true,
2152 false, -1,
2153 -1);
2154 }
2155
2156 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002157 public void testFlagFExistingE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002158 // Do not run on devices with emulated external storage.
2159 if (Environment.isExternalStorageEmulated()) {
2160 return;
2161 }
2162
2163 int iFlags = PackageManager.INSTALL_EXTERNAL;
2164 int rFlags = PackageManager.INSTALL_FORWARD_LOCK | PackageManager.INSTALL_REPLACE_EXISTING;
2165 // First install.
2166 installFromRawResource("install.apk", R.raw.install,
2167 iFlags,
2168 false,
2169 false, -1,
2170 -1);
2171 // Replace now
2172 installFromRawResource("install.apk", R.raw.install,
2173 rFlags,
2174 true,
2175 false, -1,
2176 -1);
2177 }
2178
2179 /*
2180 * The following set of tests verify the installation of apps with
2181 * install location attribute set to internalOnly, preferExternal and auto.
2182 * The manifest option should dictate the install location.
2183 * public void testManifestI/E/A
2184 * TODO out of memory fall back behaviour.
2185 */
2186 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002187 public void testManifestI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002188 installFromRawResource("install.apk", R.raw.install_loc_internal,
2189 0,
2190 true,
2191 false, -1,
2192 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2193 }
2194
2195 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002196 public void testManifestE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002197 // Do not run on devices with emulated external storage.
2198 if (Environment.isExternalStorageEmulated()) {
2199 return;
2200 }
2201
2202 installFromRawResource("install.apk", R.raw.install_loc_sdcard,
2203 0,
2204 true,
2205 false, -1,
2206 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2207 }
2208
2209 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002210 public void testManifestA() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002211 installFromRawResource("install.apk", R.raw.install_loc_auto,
2212 0,
2213 true,
2214 false, -1,
2215 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2216 }
2217
2218 /*
2219 * The following set of tests verify the installation of apps
2220 * with install location attribute set to internalOnly, preferExternal and auto
2221 * for already existing apps. The manifest option should take precedence.
2222 * TODO add out of memory fall back behaviour.
2223 * testManifestI/E/AExistingI/E
2224 */
2225 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002226 public void testManifestIExistingI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002227 int iFlags = PackageManager.INSTALL_INTERNAL;
2228 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
2229 // First install.
2230 installFromRawResource("install.apk", R.raw.install,
2231 iFlags,
2232 false,
2233 false, -1,
2234 -1);
2235 // Replace now
2236 installFromRawResource("install.apk", R.raw.install_loc_internal,
2237 rFlags,
2238 true,
2239 false, -1,
2240 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2241 }
2242
2243 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002244 public void testManifestIExistingE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002245 // Do not run on devices with emulated external storage.
2246 if (Environment.isExternalStorageEmulated()) {
2247 return;
2248 }
2249
2250 int iFlags = PackageManager.INSTALL_EXTERNAL;
2251 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
2252 // First install.
2253 installFromRawResource("install.apk", R.raw.install,
2254 iFlags,
2255 false,
2256 false, -1,
2257 -1);
2258 // Replace now
2259 installFromRawResource("install.apk", R.raw.install_loc_internal,
2260 rFlags,
2261 true,
2262 false, -1,
2263 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2264 }
2265
2266 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002267 public void testManifestEExistingI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002268 // Do not run on devices with emulated external storage.
2269 if (Environment.isExternalStorageEmulated()) {
2270 return;
2271 }
2272
2273 int iFlags = PackageManager.INSTALL_INTERNAL;
2274 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
2275 // First install.
2276 installFromRawResource("install.apk", R.raw.install,
2277 iFlags,
2278 false,
2279 false, -1,
2280 -1);
2281 // Replace now
2282 installFromRawResource("install.apk", R.raw.install_loc_sdcard,
2283 rFlags,
2284 true,
2285 false, -1,
2286 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2287 }
2288
2289 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002290 public void testManifestEExistingE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002291 // Do not run on devices with emulated external storage.
2292 if (Environment.isExternalStorageEmulated()) {
2293 return;
2294 }
2295
2296 int iFlags = PackageManager.INSTALL_EXTERNAL;
2297 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
2298 // First install.
2299 installFromRawResource("install.apk", R.raw.install,
2300 iFlags,
2301 false,
2302 false, -1,
2303 -1);
2304 // Replace now
2305 installFromRawResource("install.apk", R.raw.install_loc_sdcard,
2306 rFlags,
2307 true,
2308 false, -1,
2309 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2310 }
2311
2312 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002313 public void testManifestAExistingI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002314 int iFlags = PackageManager.INSTALL_INTERNAL;
2315 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
2316 // First install.
2317 installFromRawResource("install.apk", R.raw.install,
2318 iFlags,
2319 false,
2320 false, -1,
2321 -1);
2322 // Replace now
2323 installFromRawResource("install.apk", R.raw.install_loc_auto,
2324 rFlags,
2325 true,
2326 false, -1,
2327 PackageInfo.INSTALL_LOCATION_AUTO);
2328 }
2329
2330 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002331 public void testManifestAExistingE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002332 // Do not run on devices with emulated external storage.
2333 if (Environment.isExternalStorageEmulated()) {
2334 return;
2335 }
2336
2337 int iFlags = PackageManager.INSTALL_EXTERNAL;
2338 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
2339 // First install.
2340 installFromRawResource("install.apk", R.raw.install,
2341 iFlags,
2342 false,
2343 false, -1,
2344 -1);
2345 // Replace now
2346 installFromRawResource("install.apk", R.raw.install_loc_auto,
2347 rFlags,
2348 true,
2349 false, -1,
2350 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2351 }
2352
Kenny Root25c925e2012-09-08 22:02:21 -07002353 /*
2354 * The following set of tests check install location for existing
2355 * application based on user setting.
2356 */
2357 private int getExpectedInstallLocation(int userSetting) {
2358 int iloc = PackageInfo.INSTALL_LOCATION_UNSPECIFIED;
2359 boolean enable = getUserSettingSetInstallLocation();
2360 if (enable) {
2361 if (userSetting == PackageHelper.APP_INSTALL_AUTO) {
2362 iloc = PackageInfo.INSTALL_LOCATION_AUTO;
2363 } else if (userSetting == PackageHelper.APP_INSTALL_EXTERNAL) {
2364 iloc = PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL;
2365 } else if (userSetting == PackageHelper.APP_INSTALL_INTERNAL) {
2366 iloc = PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY;
2367 }
2368 }
2369 return iloc;
2370 }
2371
2372 private void setExistingXUserX(int userSetting, int iFlags, int iloc) throws Exception {
2373 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
2374 // First install.
2375 installFromRawResource("install.apk", R.raw.install,
2376 iFlags,
2377 false,
2378 false, -1,
2379 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
2380 int origSetting = getDefaultInstallLoc();
2381 try {
2382 // Set user setting
2383 setInstallLoc(userSetting);
2384 // Replace now
2385 installFromRawResource("install.apk", R.raw.install,
2386 rFlags,
2387 true,
2388 false, -1,
2389 iloc);
2390 } finally {
2391 setInstallLoc(origSetting);
2392 }
2393 }
2394 @LargeTest
2395 public void testExistingIUserI() throws Exception {
2396 int userSetting = PackageHelper.APP_INSTALL_INTERNAL;
2397 int iFlags = PackageManager.INSTALL_INTERNAL;
2398 setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2399 }
Kenny Root3c676892011-01-12 10:58:40 -08002400
2401 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002402 public void testExistingIUserE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002403 // Do not run on devices with emulated external storage.
2404 if (Environment.isExternalStorageEmulated()) {
2405 return;
2406 }
2407
2408 int userSetting = PackageHelper.APP_INSTALL_EXTERNAL;
2409 int iFlags = PackageManager.INSTALL_INTERNAL;
2410 setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2411 }
2412
Kenny Root25c925e2012-09-08 22:02:21 -07002413 @LargeTest
2414 public void testExistingIUserA() throws Exception {
2415 int userSetting = PackageHelper.APP_INSTALL_AUTO;
2416 int iFlags = PackageManager.INSTALL_INTERNAL;
2417 setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2418 }
Kenny Root3c676892011-01-12 10:58:40 -08002419
2420 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002421 public void testExistingEUserI() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002422 // Do not run on devices with emulated external storage.
2423 if (Environment.isExternalStorageEmulated()) {
2424 return;
2425 }
2426
2427 int userSetting = PackageHelper.APP_INSTALL_INTERNAL;
2428 int iFlags = PackageManager.INSTALL_EXTERNAL;
2429 setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2430 }
2431
2432 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002433 public void testExistingEUserE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002434 // Do not run on devices with emulated external storage.
2435 if (Environment.isExternalStorageEmulated()) {
2436 return;
2437 }
2438
2439 int userSetting = PackageHelper.APP_INSTALL_EXTERNAL;
2440 int iFlags = PackageManager.INSTALL_EXTERNAL;
2441 setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2442 }
2443
2444 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002445 public void testExistingEUserA() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002446 // Do not run on devices with emulated external storage.
2447 if (Environment.isExternalStorageEmulated()) {
2448 return;
2449 }
2450
2451 int userSetting = PackageHelper.APP_INSTALL_AUTO;
2452 int iFlags = PackageManager.INSTALL_EXTERNAL;
2453 setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL);
2454 }
2455
Kenny Root25c925e2012-09-08 22:02:21 -07002456 /*
2457 * The following set of tests verify that the user setting defines
2458 * the install location.
2459 *
2460 */
2461 private boolean getUserSettingSetInstallLocation() {
2462 try {
Jeff Sharkey625239a2012-09-26 22:03:49 -07002463 return Settings.Global.getInt(
2464 mContext.getContentResolver(), Settings.Global.SET_INSTALL_LOCATION) != 0;
Kenny Root25c925e2012-09-08 22:02:21 -07002465 } catch (SettingNotFoundException e1) {
2466 }
2467 return false;
2468 }
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -07002469
Kenny Root25c925e2012-09-08 22:02:21 -07002470 private void setUserSettingSetInstallLocation(boolean value) {
Jeff Sharkey625239a2012-09-26 22:03:49 -07002471 Settings.Global.putInt(mContext.getContentResolver(),
2472 Settings.Global.SET_INSTALL_LOCATION, value ? 1 : 0);
Kenny Root25c925e2012-09-08 22:02:21 -07002473 }
2474
2475 private void setUserX(boolean enable, int userSetting, int iloc) throws Exception {
2476 boolean origUserSetting = getUserSettingSetInstallLocation();
2477 int origSetting = getDefaultInstallLoc();
2478 try {
2479 setUserSettingSetInstallLocation(enable);
2480 // Set user setting
2481 setInstallLoc(userSetting);
2482 // Replace now
2483 installFromRawResource("install.apk", R.raw.install,
2484 0,
2485 true,
2486 false, -1,
2487 iloc);
2488 } finally {
2489 // Restore original setting
2490 setUserSettingSetInstallLocation(origUserSetting);
2491 setInstallLoc(origSetting);
2492 }
2493 }
2494 @LargeTest
2495 public void testUserI() throws Exception {
2496 int userSetting = PackageHelper.APP_INSTALL_INTERNAL;
2497 int iloc = getExpectedInstallLocation(userSetting);
2498 setUserX(true, userSetting, iloc);
2499 }
Kenny Root3c676892011-01-12 10:58:40 -08002500
2501 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002502 public void testUserE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002503 // Do not run on devices with emulated external storage.
2504 if (Environment.isExternalStorageEmulated()) {
2505 return;
2506 }
2507
2508 int userSetting = PackageHelper.APP_INSTALL_EXTERNAL;
2509 int iloc = getExpectedInstallLocation(userSetting);
2510 setUserX(true, userSetting, iloc);
2511 }
2512
Kenny Root25c925e2012-09-08 22:02:21 -07002513 @LargeTest
2514 public void testUserA() throws Exception {
2515 int userSetting = PackageHelper.APP_INSTALL_AUTO;
2516 int iloc = getExpectedInstallLocation(userSetting);
2517 setUserX(true, userSetting, iloc);
2518 }
2519
2520 /*
2521 * The following set of tests turn on/off the basic
2522 * user setting for turning on install location.
2523 */
2524 @LargeTest
2525 public void testUserPrefOffUserI() throws Exception {
2526 int userSetting = PackageHelper.APP_INSTALL_INTERNAL;
2527 int iloc = PackageInfo.INSTALL_LOCATION_UNSPECIFIED;
2528 setUserX(false, userSetting, iloc);
2529 }
Kenny Root3c676892011-01-12 10:58:40 -08002530
2531 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002532 public void testUserPrefOffUserE() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002533 // Do not run on devices with emulated external storage.
2534 if (Environment.isExternalStorageEmulated()) {
2535 return;
2536 }
2537
2538 int userSetting = PackageHelper.APP_INSTALL_EXTERNAL;
2539 int iloc = PackageInfo.INSTALL_LOCATION_UNSPECIFIED;
2540 setUserX(false, userSetting, iloc);
2541 }
2542
Kenny Root25c925e2012-09-08 22:02:21 -07002543 @LargeTest
2544 public void testUserPrefOffA() throws Exception {
2545 int userSetting = PackageHelper.APP_INSTALL_AUTO;
2546 int iloc = PackageInfo.INSTALL_LOCATION_UNSPECIFIED;
2547 setUserX(false, userSetting, iloc);
2548 }
Neal Nguyenedb979a2010-04-22 13:54:32 -07002549
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002550 static final String BASE_PERMISSIONS_DEFINED[] = new String[] {
2551 PERM_PACKAGE, "com.android.unit_tests.install_decl_perm",
2552 PERM_DEFINED,
Brett Chabot0dc59e72010-04-01 18:21:38 -07002553 "com.android.frameworks.coretests.NORMAL",
2554 "com.android.frameworks.coretests.DANGEROUS",
2555 "com.android.frameworks.coretests.SIGNATURE",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002556 };
Neal Nguyenedb979a2010-04-22 13:54:32 -07002557
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002558 static final String BASE_PERMISSIONS_UNDEFINED[] = new String[] {
Brett Chabot0dc59e72010-04-01 18:21:38 -07002559 PERM_PACKAGE, "com.android.frameworks.coretests.install_decl_perm",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002560 PERM_UNDEFINED,
Brett Chabot0dc59e72010-04-01 18:21:38 -07002561 "com.android.frameworks.coretests.NORMAL",
2562 "com.android.frameworks.coretests.DANGEROUS",
2563 "com.android.frameworks.coretests.SIGNATURE",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002564 };
Neal Nguyenedb979a2010-04-22 13:54:32 -07002565
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002566 static final String BASE_PERMISSIONS_USED[] = new String[] {
Brett Chabot0dc59e72010-04-01 18:21:38 -07002567 PERM_PACKAGE, "com.android.frameworks.coretests.install_use_perm_good",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002568 PERM_USED,
Brett Chabot0dc59e72010-04-01 18:21:38 -07002569 "com.android.frameworks.coretests.NORMAL",
2570 "com.android.frameworks.coretests.DANGEROUS",
2571 "com.android.frameworks.coretests.SIGNATURE",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002572 };
Neal Nguyenedb979a2010-04-22 13:54:32 -07002573
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002574 static final String BASE_PERMISSIONS_NOTUSED[] = new String[] {
Brett Chabot0dc59e72010-04-01 18:21:38 -07002575 PERM_PACKAGE, "com.android.frameworks.coretests.install_use_perm_good",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002576 PERM_NOTUSED,
Brett Chabot0dc59e72010-04-01 18:21:38 -07002577 "com.android.frameworks.coretests.NORMAL",
2578 "com.android.frameworks.coretests.DANGEROUS",
2579 "com.android.frameworks.coretests.SIGNATURE",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002580 };
Neal Nguyenedb979a2010-04-22 13:54:32 -07002581
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002582 static final String BASE_PERMISSIONS_SIGUSED[] = new String[] {
Brett Chabot0dc59e72010-04-01 18:21:38 -07002583 PERM_PACKAGE, "com.android.frameworks.coretests.install_use_perm_good",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002584 PERM_USED,
Brett Chabot0dc59e72010-04-01 18:21:38 -07002585 "com.android.frameworks.coretests.SIGNATURE",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002586 PERM_NOTUSED,
Brett Chabot0dc59e72010-04-01 18:21:38 -07002587 "com.android.frameworks.coretests.NORMAL",
2588 "com.android.frameworks.coretests.DANGEROUS",
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002589 };
Neal Nguyenedb979a2010-04-22 13:54:32 -07002590
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002591 /*
2592 * Ensure that permissions are properly declared.
2593 */
Neal Nguyenedb979a2010-04-22 13:54:32 -07002594 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002595 public void testInstallDeclaresPermissions() throws Exception {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002596 InstallParams ip = null;
2597 InstallParams ip2 = null;
2598 try {
2599 // **: Upon installing a package, are its declared permissions published?
Neal Nguyenedb979a2010-04-22 13:54:32 -07002600
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002601 int iFlags = PackageManager.INSTALL_INTERNAL;
2602 int iApk = R.raw.install_decl_perm;
2603 ip = installFromRawResource("install.apk", iApk,
2604 iFlags, false,
2605 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2606 assertInstall(ip.pkg, iFlags, ip.pkg.installLocation);
2607 assertPermissions(BASE_PERMISSIONS_DEFINED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002608
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002609 // **: Upon installing package, are its permissions granted?
Neal Nguyenedb979a2010-04-22 13:54:32 -07002610
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002611 int i2Flags = PackageManager.INSTALL_INTERNAL;
2612 int i2Apk = R.raw.install_use_perm_good;
2613 ip2 = installFromRawResource("install2.apk", i2Apk,
2614 i2Flags, false,
2615 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2616 assertInstall(ip2.pkg, i2Flags, ip2.pkg.installLocation);
2617 assertPermissions(BASE_PERMISSIONS_USED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002618
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002619 // **: Upon removing but not deleting, are permissions retained?
Neal Nguyenedb979a2010-04-22 13:54:32 -07002620
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002621 GenericReceiver receiver = new DeleteReceiver(ip.pkg.packageName);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002622
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002623 try {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07002624 invokeDeletePackage(ip.pkg.packageName, PackageManager.DELETE_KEEP_DATA, receiver);
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002625 } catch (Exception e) {
2626 failStr(e);
2627 }
2628 assertPermissions(BASE_PERMISSIONS_DEFINED);
2629 assertPermissions(BASE_PERMISSIONS_USED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002630
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002631 // **: Upon re-installing, are permissions retained?
Neal Nguyenedb979a2010-04-22 13:54:32 -07002632
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002633 ip = installFromRawResource("install.apk", iApk,
2634 iFlags | PackageManager.INSTALL_REPLACE_EXISTING, false,
2635 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2636 assertInstall(ip.pkg, iFlags, ip.pkg.installLocation);
2637 assertPermissions(BASE_PERMISSIONS_DEFINED);
2638 assertPermissions(BASE_PERMISSIONS_USED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002639
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002640 // **: Upon deleting package, are all permissions removed?
Neal Nguyenedb979a2010-04-22 13:54:32 -07002641
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002642 try {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07002643 invokeDeletePackage(ip.pkg.packageName, 0, receiver);
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002644 ip = null;
2645 } catch (Exception e) {
2646 failStr(e);
2647 }
2648 assertPermissions(BASE_PERMISSIONS_UNDEFINED);
2649 assertPermissions(BASE_PERMISSIONS_NOTUSED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002650
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002651 // **: Delete package using permissions; nothing to check here.
Neal Nguyenedb979a2010-04-22 13:54:32 -07002652
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002653 GenericReceiver receiver2 = new DeleteReceiver(ip2.pkg.packageName);
2654 try {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07002655 invokeDeletePackage(ip2.pkg.packageName, 0, receiver);
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002656 ip2 = null;
2657 } catch (Exception e) {
2658 failStr(e);
2659 }
Neal Nguyenedb979a2010-04-22 13:54:32 -07002660
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002661 // **: Re-install package using permissions; no permissions can be granted.
Neal Nguyenedb979a2010-04-22 13:54:32 -07002662
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002663 ip2 = installFromRawResource("install2.apk", i2Apk,
2664 i2Flags, false,
2665 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2666 assertInstall(ip2.pkg, i2Flags, ip2.pkg.installLocation);
2667 assertPermissions(BASE_PERMISSIONS_NOTUSED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002668
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002669 // **: Upon installing declaring package, are sig permissions granted
2670 // to other apps (but not other perms)?
Neal Nguyenedb979a2010-04-22 13:54:32 -07002671
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002672 ip = installFromRawResource("install.apk", iApk,
2673 iFlags, false,
2674 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2675 assertInstall(ip.pkg, iFlags, ip.pkg.installLocation);
2676 assertPermissions(BASE_PERMISSIONS_DEFINED);
2677 assertPermissions(BASE_PERMISSIONS_SIGUSED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002678
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002679 // **: Re-install package using permissions; are all permissions granted?
Neal Nguyenedb979a2010-04-22 13:54:32 -07002680
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002681 ip2 = installFromRawResource("install2.apk", i2Apk,
2682 i2Flags | PackageManager.INSTALL_REPLACE_EXISTING, false,
2683 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2684 assertInstall(ip2.pkg, i2Flags, ip2.pkg.installLocation);
2685 assertPermissions(BASE_PERMISSIONS_NOTUSED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002686
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002687 // **: Upon deleting package, are all permissions removed?
Neal Nguyenedb979a2010-04-22 13:54:32 -07002688
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002689 try {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07002690 invokeDeletePackage(ip.pkg.packageName, 0, receiver);
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002691 ip = null;
2692 } catch (Exception e) {
2693 failStr(e);
2694 }
2695 assertPermissions(BASE_PERMISSIONS_UNDEFINED);
2696 assertPermissions(BASE_PERMISSIONS_NOTUSED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002697
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002698 // **: Delete package using permissions; nothing to check here.
Neal Nguyenedb979a2010-04-22 13:54:32 -07002699
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002700 try {
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07002701 invokeDeletePackage(ip2.pkg.packageName, 0, receiver);
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002702 ip2 = null;
2703 } catch (Exception e) {
2704 failStr(e);
2705 }
Neal Nguyenedb979a2010-04-22 13:54:32 -07002706
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07002707 } finally {
2708 if (ip2 != null) {
2709 cleanUpInstall(ip2);
2710 }
2711 if (ip != null) {
2712 cleanUpInstall(ip);
2713 }
2714 }
2715 }
2716
Suchi Amalapurapuebb83ad2010-03-19 11:55:29 -07002717 /*
2718 * Ensure that permissions are properly declared.
2719 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07002720 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002721 public void testInstallOnSdPermissionsUnmount() throws Exception {
Suchi Amalapurapuebb83ad2010-03-19 11:55:29 -07002722 InstallParams ip = null;
Kenny Rootd7b421b2010-08-05 08:40:00 -07002723 boolean origMediaState = checkMediaState(Environment.MEDIA_MOUNTED);
Suchi Amalapurapuebb83ad2010-03-19 11:55:29 -07002724 try {
2725 // **: Upon installing a package, are its declared permissions published?
2726 int iFlags = PackageManager.INSTALL_INTERNAL;
2727 int iApk = R.raw.install_decl_perm;
2728 ip = installFromRawResource("install.apk", iApk,
2729 iFlags, false,
2730 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
2731 assertInstall(ip.pkg, iFlags, ip.pkg.installLocation);
2732 assertPermissions(BASE_PERMISSIONS_DEFINED);
2733 // Unmount media here
2734 assertTrue(unmountMedia());
2735 // Mount media again
2736 mountMedia();
2737 //Check permissions now
2738 assertPermissions(BASE_PERMISSIONS_DEFINED);
2739 } finally {
2740 if (ip != null) {
2741 cleanUpInstall(ip);
2742 }
2743 }
2744 }
Suchi Amalapurapuc7537ee2010-03-24 09:27:19 -07002745
Sudheer Shanka2250d562016-11-07 15:41:02 -08002746 /* This test creates a stale container via StorageManagerService and then installs
Suchi Amalapurapuc7537ee2010-03-24 09:27:19 -07002747 * a package and verifies that the stale container is cleaned up and install
2748 * is successful.
2749 * Please note that this test is very closely tied to the framework's
2750 * naming convention for secure containers.
2751 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07002752 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002753 public void testInstallSdcardStaleContainer() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002754 // Do not run on devices with emulated external storage.
2755 if (Environment.isExternalStorageEmulated()) {
2756 return;
2757 }
2758
Kenny Rootd7b421b2010-08-05 08:40:00 -07002759 boolean origMediaState = checkMediaState(Environment.MEDIA_MOUNTED);
Suchi Amalapurapuc7537ee2010-03-24 09:27:19 -07002760 try {
Kenny Rootd7b421b2010-08-05 08:40:00 -07002761 // Mount media first
2762 mountMedia();
Suchi Amalapurapuc7537ee2010-03-24 09:27:19 -07002763 String outFileName = "install.apk";
2764 int rawResId = R.raw.install;
2765 PackageManager pm = mContext.getPackageManager();
2766 File filesDir = mContext.getFilesDir();
2767 File outFile = new File(filesDir, outFileName);
2768 Uri packageURI = getInstallablePackage(rawResId, outFile);
2769 PackageParser.Package pkg = parsePackage(packageURI);
2770 assertNotNull(pkg);
2771 // Install an app on sdcard.
2772 installFromRawResource(outFileName, rawResId,
2773 PackageManager.INSTALL_EXTERNAL, false,
2774 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
2775 // Unmount sdcard
2776 unmountMedia();
2777 // Delete the app on sdcard to leave a stale container on sdcard.
2778 GenericReceiver receiver = new DeleteReceiver(pkg.packageName);
Suchi Amalapurapu315a5fb2010-04-13 14:32:16 -07002779 assertTrue(invokeDeletePackage(pkg.packageName, 0, receiver));
Suchi Amalapurapuc7537ee2010-03-24 09:27:19 -07002780 mountMedia();
2781 // Reinstall the app and make sure it gets installed.
2782 installFromRawResource(outFileName, rawResId,
2783 PackageManager.INSTALL_EXTERNAL, true,
2784 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
Neal Nguyenedb979a2010-04-22 13:54:32 -07002785 } catch (Exception e) {
Suchi Amalapurapuc7537ee2010-03-24 09:27:19 -07002786 failStr(e.getMessage());
2787 } finally {
2788 if (origMediaState) {
2789 mountMedia();
2790 } else {
2791 unmountMedia();
2792 }
2793
2794 }
2795 }
Suchi Amalapurapu1ace5bc2010-05-13 12:05:53 -07002796
2797 /* This test installs an application on sdcard and unmounts media.
2798 * The app is then re-installed on internal storage. The sdcard is mounted
2799 * and verified that the re-installation on internal storage takes precedence.
2800 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07002801 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002802 public void testInstallSdcardStaleContainerReinstall() throws Exception {
Kenny Root3c676892011-01-12 10:58:40 -08002803 // Do not run on devices with emulated external storage.
2804 if (Environment.isExternalStorageEmulated()) {
2805 return;
2806 }
2807
Kenny Rootd7b421b2010-08-05 08:40:00 -07002808 boolean origMediaState = checkMediaState(Environment.MEDIA_MOUNTED);
Suchi Amalapurapu1ace5bc2010-05-13 12:05:53 -07002809 try {
2810 // Mount media first
2811 mountMedia();
2812 String outFileName = "install.apk";
2813 int rawResId = R.raw.install;
2814 PackageManager pm = mContext.getPackageManager();
2815 File filesDir = mContext.getFilesDir();
2816 File outFile = new File(filesDir, outFileName);
2817 Uri packageURI = getInstallablePackage(rawResId, outFile);
2818 PackageParser.Package pkg = parsePackage(packageURI);
2819 assertNotNull(pkg);
2820 // Install an app on sdcard.
2821 installFromRawResource(outFileName, rawResId,
2822 PackageManager.INSTALL_EXTERNAL, false,
2823 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
2824 // Unmount sdcard
2825 unmountMedia();
2826 // Reinstall the app and make sure it gets installed on internal storage.
2827 installFromRawResource(outFileName, rawResId,
2828 PackageManager.INSTALL_REPLACE_EXISTING, false,
2829 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
2830 mountMedia();
2831 // Verify that the app installed is on internal storage.
Kenny Root25c925e2012-09-08 22:02:21 -07002832 assertInstall(pkg, 0, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
Suchi Amalapurapu1ace5bc2010-05-13 12:05:53 -07002833 } catch (Exception e) {
2834 failStr(e.getMessage());
2835 } finally {
2836 if (origMediaState) {
2837 mountMedia();
2838 } else {
2839 unmountMedia();
2840 }
Suchi Amalapurapu1ace5bc2010-05-13 12:05:53 -07002841 }
2842 }
Kenny Root3c676892011-01-12 10:58:40 -08002843
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002844 /*
2845 * The following series of tests are related to upgrading apps with
Neal Nguyenedb979a2010-04-22 13:54:32 -07002846 * different certificates.
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002847 */
2848 private int APP1_UNSIGNED = R.raw.install_app1_unsigned;
Kenny Root25c925e2012-09-08 22:02:21 -07002849
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002850 private int APP1_CERT1 = R.raw.install_app1_cert1;
Kenny Root25c925e2012-09-08 22:02:21 -07002851
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002852 private int APP1_CERT2 = R.raw.install_app1_cert2;
Kenny Root25c925e2012-09-08 22:02:21 -07002853
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002854 private int APP1_CERT1_CERT2 = R.raw.install_app1_cert1_cert2;
Kenny Root25c925e2012-09-08 22:02:21 -07002855
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002856 private int APP1_CERT3_CERT4 = R.raw.install_app1_cert3_cert4;
Kenny Root25c925e2012-09-08 22:02:21 -07002857
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002858 private int APP1_CERT3 = R.raw.install_app1_cert3;
Kenny Root25c925e2012-09-08 22:02:21 -07002859
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002860 private int APP2_UNSIGNED = R.raw.install_app2_unsigned;
Kenny Root25c925e2012-09-08 22:02:21 -07002861
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002862 private int APP2_CERT1 = R.raw.install_app2_cert1;
Kenny Root25c925e2012-09-08 22:02:21 -07002863
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002864 private int APP2_CERT2 = R.raw.install_app2_cert2;
Kenny Root25c925e2012-09-08 22:02:21 -07002865
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002866 private int APP2_CERT1_CERT2 = R.raw.install_app2_cert1_cert2;
Kenny Root25c925e2012-09-08 22:02:21 -07002867
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002868 private int APP2_CERT3 = R.raw.install_app2_cert3;
2869
Kenny Root25c925e2012-09-08 22:02:21 -07002870 private InstallParams replaceCerts(int apk1, int apk2, boolean cleanUp, boolean fail,
2871 int retCode) throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002872 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
2873 String apk1Name = "install1.apk";
2874 String apk2Name = "install2.apk";
2875 PackageParser.Package pkg1 = getParsedPackage(apk1Name, apk1);
2876 try {
2877 InstallParams ip = installFromRawResource(apk1Name, apk1, 0, false,
2878 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
2879 installFromRawResource(apk2Name, apk2, rFlags, false,
2880 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
2881 return ip;
2882 } catch (Exception e) {
2883 failStr(e.getMessage());
2884 } finally {
2885 if (cleanUp) {
2886 cleanUpInstall(pkg1.packageName);
2887 }
2888 }
2889 return null;
2890 }
Kenny Root25c925e2012-09-08 22:02:21 -07002891
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002892 /*
2893 * Test that an app signed with two certificates can be upgraded by the
2894 * same app signed with two certificates.
2895 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07002896 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002897 public void testReplaceMatchAllCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002898 replaceCerts(APP1_CERT1_CERT2, APP1_CERT1_CERT2, true, false, -1);
2899 }
2900
2901 /*
2902 * Test that an app signed with two certificates cannot be upgraded
2903 * by an app signed with a different certificate.
2904 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07002905 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002906 public void testReplaceMatchNoCerts1() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002907 replaceCerts(APP1_CERT1_CERT2, APP1_CERT3, true, true,
Jeff Sharkey2a533322014-11-07 16:24:48 -08002908 PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002909 }
Kenny Root25c925e2012-09-08 22:02:21 -07002910
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002911 /*
2912 * Test that an app signed with two certificates cannot be upgraded
2913 * by an app signed with a different certificate.
2914 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07002915 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002916 public void testReplaceMatchNoCerts2() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002917 replaceCerts(APP1_CERT1_CERT2, APP1_CERT3_CERT4, true, true,
Jeff Sharkey2a533322014-11-07 16:24:48 -08002918 PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002919 }
Kenny Root25c925e2012-09-08 22:02:21 -07002920
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002921 /*
2922 * Test that an app signed with two certificates cannot be upgraded by
2923 * an app signed with a subset of initial certificates.
2924 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07002925 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002926 public void testReplaceMatchSomeCerts1() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002927 replaceCerts(APP1_CERT1_CERT2, APP1_CERT1, true, true,
Jeff Sharkey2a533322014-11-07 16:24:48 -08002928 PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002929 }
Kenny Root25c925e2012-09-08 22:02:21 -07002930
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002931 /*
2932 * Test that an app signed with two certificates cannot be upgraded by
2933 * an app signed with the last certificate.
2934 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07002935 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002936 public void testReplaceMatchSomeCerts2() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002937 replaceCerts(APP1_CERT1_CERT2, APP1_CERT2, true, true,
Jeff Sharkey2a533322014-11-07 16:24:48 -08002938 PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002939 }
Kenny Root25c925e2012-09-08 22:02:21 -07002940
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002941 /*
2942 * Test that an app signed with a certificate can be upgraded by app
2943 * signed with a superset of certificates.
2944 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07002945 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002946 public void testReplaceMatchMoreCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002947 replaceCerts(APP1_CERT1, APP1_CERT1_CERT2, true, true,
Jeff Sharkey2a533322014-11-07 16:24:48 -08002948 PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002949 }
Kenny Root25c925e2012-09-08 22:02:21 -07002950
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002951 /*
2952 * Test that an app signed with a certificate can be upgraded by app
2953 * signed with a superset of certificates. Then verify that the an app
2954 * signed with the original set of certs cannot upgrade the new one.
2955 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07002956 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07002957 public void testReplaceMatchMoreCertsReplaceSomeCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002958 InstallParams ip = replaceCerts(APP1_CERT1, APP1_CERT1_CERT2, false, true,
Jeff Sharkey2a533322014-11-07 16:24:48 -08002959 PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07002960 try {
2961 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING;
2962 installFromRawResource("install.apk", APP1_CERT1, rFlags, false,
2963 false, -1,
2964 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
2965 } catch (Exception e) {
2966 failStr(e.getMessage());
2967 } finally {
2968 if (ip != null) {
2969 cleanUpInstall(ip);
2970 }
2971 }
2972 }
Kenny Root25c925e2012-09-08 22:02:21 -07002973
2974 /**
dcashman55b10782014-04-09 14:20:38 -07002975 * The following tests are related to testing KeySets-based key rotation
2976 */
2977 /*
2978 * Check if an apk which does not specify an upgrade-keyset may be upgraded
2979 * by an apk which does
2980 */
2981 public void testNoKSToUpgradeKS() throws Exception {
2982 replaceCerts(R.raw.keyset_sa_unone, R.raw.keyset_sa_ua, true, false, -1);
2983 }
2984
2985 /*
2986 * Check if an apk which does specify an upgrade-keyset may be downgraded to
2987 * an apk which does not
2988 */
2989 public void testUpgradeKSToNoKS() throws Exception {
2990 replaceCerts(R.raw.keyset_sa_ua, R.raw.keyset_sa_unone, true, false, -1);
2991 }
2992
2993 /*
2994 * Check if an apk signed by a key other than the upgrade keyset can update
2995 * an app
2996 */
2997 public void testUpgradeKSWithWrongKey() throws Exception {
2998 replaceCerts(R.raw.keyset_sa_ua, R.raw.keyset_sb_ua, true, true,
Jeff Sharkey2a533322014-11-07 16:24:48 -08002999 PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE);
dcashman55b10782014-04-09 14:20:38 -07003000 }
3001
3002 /*
3003 * Check if an apk signed by its signing key, which is not an upgrade key,
3004 * can upgrade an app.
3005 */
3006 public void testUpgradeKSWithWrongSigningKey() throws Exception {
3007 replaceCerts(R.raw.keyset_sa_ub, R.raw.keyset_sa_ub, true, true,
Jeff Sharkey2a533322014-11-07 16:24:48 -08003008 PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE);
dcashman55b10782014-04-09 14:20:38 -07003009 }
3010
3011 /*
3012 * Check if an apk signed by its upgrade key, which is not its signing key,
3013 * can upgrade an app.
3014 */
3015 public void testUpgradeKSWithUpgradeKey() throws Exception {
3016 replaceCerts(R.raw.keyset_sa_ub, R.raw.keyset_sb_ub, true, false, -1);
3017 }
3018 /*
3019 * Check if an apk signed by its upgrade key, which is its signing key, can
3020 * upgrade an app.
3021 */
3022 public void testUpgradeKSWithSigningUpgradeKey() throws Exception {
3023 replaceCerts(R.raw.keyset_sa_ua, R.raw.keyset_sa_ua, true, false, -1);
3024 }
3025
3026 /*
3027 * Check if an apk signed by multiple keys, one of which is its upgrade key,
3028 * can upgrade an app.
3029 */
3030 public void testMultipleUpgradeKSWithUpgradeKey() throws Exception {
3031 replaceCerts(R.raw.keyset_sa_ua, R.raw.keyset_sab_ua, true, false, -1);
3032 }
3033
3034 /*
3035 * Check if an apk signed by multiple keys, one of which is its signing key,
3036 * but none of which is an upgrade key, can upgrade an app.
3037 */
3038 public void testMultipleUpgradeKSWithSigningKey() throws Exception {
3039 replaceCerts(R.raw.keyset_sau_ub, R.raw.keyset_sa_ua, true, true,
Jeff Sharkey2a533322014-11-07 16:24:48 -08003040 PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE);
dcashman55b10782014-04-09 14:20:38 -07003041 }
3042
3043 /*
3044 * Check if an apk which defines multiple (two) upgrade keysets is
3045 * upgrade-able by either.
3046 */
3047 public void testUpgradeKSWithMultipleUpgradeKeySets() throws Exception {
3048 replaceCerts(R.raw.keyset_sa_ua_ub, R.raw.keyset_sa_ua, true, false, -1);
3049 replaceCerts(R.raw.keyset_sa_ua_ub, R.raw.keyset_sb_ub, true, false, -1);
3050 }
3051
3052 /*
3053 * Check if an apk's sigs are changed after upgrading with a non-signing
3054 * key.
3055 *
3056 * TODO: consider checking against hard-coded Signatures in the Sig-tests
3057 */
3058 public void testSigChangeAfterUpgrade() throws Exception {
3059 // install original apk and grab sigs
3060 installFromRawResource("tmp.apk", R.raw.keyset_sa_ub,
3061 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3062 PackageManager pm = getPm();
3063 String pkgName = "com.android.frameworks.coretests.keysets";
3064 PackageInfo pi = pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);
3065 assertTrue("Package should only have one signature, sig A",
3066 pi.signatures.length == 1);
3067 String sigBefore = pi.signatures[0].toCharsString();
3068 // install apk signed by different upgrade KeySet
3069 installFromRawResource("tmp2.apk", R.raw.keyset_sb_ub,
3070 PackageManager.INSTALL_REPLACE_EXISTING, false, false, -1,
3071 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3072 pi = pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);
3073 assertTrue("Package should only have one signature, sig B",
3074 pi.signatures.length == 1);
3075 String sigAfter = pi.signatures[0].toCharsString();
3076 assertFalse("Package signatures did not change after upgrade!",
3077 sigBefore.equals(sigAfter));
3078 cleanUpInstall(pkgName);
3079 }
3080
3081 /*
3082 * Check if an apk's sig is the same after upgrading with a signing
3083 * key.
3084 */
3085 public void testSigSameAfterUpgrade() throws Exception {
3086 // install original apk and grab sigs
3087 installFromRawResource("tmp.apk", R.raw.keyset_sa_ua,
3088 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3089 PackageManager pm = getPm();
3090 String pkgName = "com.android.frameworks.coretests.keysets";
3091 PackageInfo pi = pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);
3092 assertTrue("Package should only have one signature, sig A",
3093 pi.signatures.length == 1);
3094 String sigBefore = pi.signatures[0].toCharsString();
3095 // install apk signed by same upgrade KeySet
3096 installFromRawResource("tmp2.apk", R.raw.keyset_sa_ua,
3097 PackageManager.INSTALL_REPLACE_EXISTING, false, false, -1,
3098 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3099 pi = pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);
3100 assertTrue("Package should only have one signature, sig A",
3101 pi.signatures.length == 1);
3102 String sigAfter = pi.signatures[0].toCharsString();
3103 assertTrue("Package signatures changed after upgrade!",
3104 sigBefore.equals(sigAfter));
3105 cleanUpInstall(pkgName);
3106 }
3107
3108 /*
3109 * Check if an apk's sigs are the same after upgrading with an app with
3110 * a subset of the original signing keys.
3111 */
3112 public void testSigRemovedAfterUpgrade() throws Exception {
3113 // install original apk and grab sigs
3114 installFromRawResource("tmp.apk", R.raw.keyset_sab_ua,
3115 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3116 PackageManager pm = getPm();
3117 String pkgName = "com.android.frameworks.coretests.keysets";
3118 PackageInfo pi = pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);
3119 assertTrue("Package should have two signatures, sig A and sig B",
3120 pi.signatures.length == 2);
3121 Set<String> sigsBefore = new HashSet<String>();
3122 for (int i = 0; i < pi.signatures.length; i++) {
3123 sigsBefore.add(pi.signatures[i].toCharsString());
3124 }
3125 // install apk signed subset upgrade KeySet
3126 installFromRawResource("tmp2.apk", R.raw.keyset_sa_ua,
3127 PackageManager.INSTALL_REPLACE_EXISTING, false, false, -1,
3128 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3129 pi = pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);
3130 assertTrue("Package should only have one signature, sig A",
3131 pi.signatures.length == 1);
3132 String sigAfter = pi.signatures[0].toCharsString();
3133 assertTrue("Original package signatures did not contain new sig",
3134 sigsBefore.contains(sigAfter));
3135 cleanUpInstall(pkgName);
3136 }
3137
3138 /*
3139 * Check if an apk's sigs are added to after upgrading with an app with
3140 * a superset of the original signing keys.
3141 */
3142 public void testSigAddedAfterUpgrade() throws Exception {
3143 // install original apk and grab sigs
3144 installFromRawResource("tmp.apk", R.raw.keyset_sa_ua,
3145 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3146 PackageManager pm = getPm();
3147 String pkgName = "com.android.frameworks.coretests.keysets";
3148 PackageInfo pi = pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);
3149 assertTrue("Package should only have one signature, sig A",
3150 pi.signatures.length == 1);
3151 String sigBefore = pi.signatures[0].toCharsString();
3152 // install apk signed subset upgrade KeySet
3153 installFromRawResource("tmp2.apk", R.raw.keyset_sab_ua,
3154 PackageManager.INSTALL_REPLACE_EXISTING, false, false, -1,
3155 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3156 pi = pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);
3157 assertTrue("Package should have two signatures, sig A and sig B",
3158 pi.signatures.length == 2);
3159 Set<String> sigsAfter = new HashSet<String>();
3160 for (int i = 0; i < pi.signatures.length; i++) {
3161 sigsAfter.add(pi.signatures[i].toCharsString());
3162 }
3163 assertTrue("Package signatures did not change after upgrade!",
3164 sigsAfter.contains(sigBefore));
3165 cleanUpInstall(pkgName);
3166 }
3167
3168 /*
3169 * Check if an apk gains signature-level permission after changing to the a
3170 * new signature, for which a permission should be granted.
3171 */
3172 public void testUpgradeSigPermGained() throws Exception {
3173 // install apk which defines permission
3174 installFromRawResource("permDef.apk", R.raw.keyset_permdef_sa_unone,
3175 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3176 // install apk which uses permission but does not have sig
3177 installFromRawResource("permUse.apk", R.raw.keyset_permuse_sb_ua_ub,
3178 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3179 // verify that package does not have perm before
3180 PackageManager pm = getPm();
3181 String permPkgName = "com.android.frameworks.coretests.keysets_permdef";
3182 String pkgName = "com.android.frameworks.coretests.keysets";
3183 String permName = "com.android.frameworks.coretests.keysets_permdef.keyset_perm";
3184 assertFalse("keyset permission granted to app without same signature!",
3185 pm.checkPermission(permName, pkgName)
3186 == PackageManager.PERMISSION_GRANTED);
3187 // upgrade to apk with perm signature
3188 installFromRawResource("permUse2.apk", R.raw.keyset_permuse_sa_ua_ub,
3189 PackageManager.INSTALL_REPLACE_EXISTING, false, false, -1,
3190 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3191 assertTrue("keyset permission not granted to app after upgrade to same sig",
3192 pm.checkPermission(permName, pkgName)
3193 == PackageManager.PERMISSION_GRANTED);
3194 cleanUpInstall(permPkgName);
3195 cleanUpInstall(pkgName);
3196 }
3197
3198 /*
3199 * Check if an apk loses signature-level permission after changing to the a
3200 * new signature, from one which a permission should be granted.
3201 */
3202 public void testUpgradeSigPermLost() throws Exception {
3203 // install apk which defines permission
3204 installFromRawResource("permDef.apk", R.raw.keyset_permdef_sa_unone,
3205 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3206 // install apk which uses permission, signed by same sig
3207 installFromRawResource("permUse.apk", R.raw.keyset_permuse_sa_ua_ub,
3208 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3209 // verify that package does not have perm before
3210 PackageManager pm = getPm();
3211 String permPkgName = "com.android.frameworks.coretests.keysets_permdef";
3212 String pkgName = "com.android.frameworks.coretests.keysets";
3213 String permName = "com.android.frameworks.coretests.keysets_permdef.keyset_perm";
3214 assertTrue("keyset permission not granted to app with same sig",
3215 pm.checkPermission(permName, pkgName)
3216 == PackageManager.PERMISSION_GRANTED);
3217 // upgrade to apk without perm signature
3218 installFromRawResource("permUse2.apk", R.raw.keyset_permuse_sb_ua_ub,
3219 PackageManager.INSTALL_REPLACE_EXISTING, false, false, -1,
3220 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3221
3222 assertFalse("keyset permission not revoked from app which upgraded to a "
3223 + "different signature",
3224 pm.checkPermission(permName, pkgName)
3225 == PackageManager.PERMISSION_GRANTED);
3226 cleanUpInstall(permPkgName);
3227 cleanUpInstall(pkgName);
3228 }
3229
3230 /**
dcashman9d2f4412014-06-09 09:27:54 -07003231 * The following tests are related to testing KeySets-based API
3232 */
3233
3234 /*
3235 * testGetSigningKeySetNull - ensure getSigningKeySet() returns null on null
3236 * input and when calling a package other than that which made the call.
3237 */
3238 public void testGetSigningKeySet() throws Exception {
3239 PackageManager pm = getPm();
3240 String mPkgName = mContext.getPackageName();
3241 String otherPkgName = "com.android.frameworks.coretests.keysets_api";
3242 KeySet ks;
3243 try {
3244 ks = pm.getSigningKeySet(null);
3245 assertTrue(false); // should have thrown
3246 } catch (NullPointerException e) {
3247 }
3248 try {
3249 ks = pm.getSigningKeySet("keysets.test.bogus.package");
3250 assertTrue(false); // should have thrown
3251 } catch (IllegalArgumentException e) {
3252 }
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003253 final InstallParams ip = installFromRawResource("keysetApi.apk", R.raw.keyset_splat_api,
dcashman9d2f4412014-06-09 09:27:54 -07003254 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3255 try {
3256 ks = pm.getSigningKeySet(otherPkgName);
3257 assertTrue(false); // should have thrown
3258 } catch (SecurityException e) {
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003259 } finally {
3260 cleanUpInstall(ip);
dcashman9d2f4412014-06-09 09:27:54 -07003261 }
dcashman9d2f4412014-06-09 09:27:54 -07003262 ks = pm.getSigningKeySet(mContext.getPackageName());
3263 assertNotNull(ks);
3264 }
3265
3266 /*
3267 * testGetKeySetByAlias - same as getSigningKeySet, but for keysets defined
3268 * by this package.
3269 */
3270 public void testGetKeySetByAlias() throws Exception {
3271 PackageManager pm = getPm();
3272 String mPkgName = mContext.getPackageName();
3273 String otherPkgName = "com.android.frameworks.coretests.keysets_api";
3274 KeySet ks;
3275 try {
3276 ks = pm.getKeySetByAlias(null, null);
3277 assertTrue(false); // should have thrown
3278 } catch (NullPointerException e) {
3279 }
3280 try {
3281 ks = pm.getKeySetByAlias(null, "keysetBogus");
3282 assertTrue(false); // should have thrown
3283 } catch (NullPointerException e) {
3284 }
3285 try {
3286 ks = pm.getKeySetByAlias("keysets.test.bogus.package", null);
3287 assertTrue(false); // should have thrown
3288 } catch (NullPointerException e) {
3289 }
3290 try {
3291 ks = pm.getKeySetByAlias("keysets.test.bogus.package", "A");
3292 assertTrue(false); // should have thrown
3293 } catch(IllegalArgumentException e) {
3294 }
3295 try {
3296 ks = pm.getKeySetByAlias(mPkgName, "keysetBogus");
3297 assertTrue(false); // should have thrown
3298 } catch(IllegalArgumentException e) {
3299 }
dcashmanc4d372f2015-12-01 17:04:58 -08003300
3301 // make sure we can get a KeySet from our pkg
3302 ks = pm.getKeySetByAlias(mPkgName, "A");
3303 assertNotNull(ks);
3304
3305 // and another
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003306 final InstallParams ip = installFromRawResource("keysetApi.apk", R.raw.keyset_splat_api,
dcashman9d2f4412014-06-09 09:27:54 -07003307 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3308 try {
3309 ks = pm.getKeySetByAlias(otherPkgName, "A");
dcashmanc4d372f2015-12-01 17:04:58 -08003310 assertNotNull(ks);
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003311 } finally {
3312 cleanUpInstall(ip);
dcashman9d2f4412014-06-09 09:27:54 -07003313 }
dcashman9d2f4412014-06-09 09:27:54 -07003314 }
3315
3316 public void testIsSignedBy() throws Exception {
3317 PackageManager pm = getPm();
3318 String mPkgName = mContext.getPackageName();
3319 String otherPkgName = "com.android.frameworks.coretests.keysets_api";
3320 KeySet mSigningKS = pm.getSigningKeySet(mPkgName);
3321 KeySet mDefinedKS = pm.getKeySetByAlias(mPkgName, "A");
3322
3323 try {
3324 assertFalse(pm.isSignedBy(null, null));
3325 assertTrue(false); // should have thrown
3326 } catch (NullPointerException e) {
3327 }
3328 try {
3329 assertFalse(pm.isSignedBy(null, mSigningKS));
3330 assertTrue(false); // should have thrown
3331 } catch (NullPointerException e) {
3332 }
3333 try {
3334 assertFalse(pm.isSignedBy(mPkgName, null));
3335 assertTrue(false); // should have thrown
3336 } catch (NullPointerException e) {
3337 }
3338 try {
3339 assertFalse(pm.isSignedBy("keysets.test.bogus.package", mDefinedKS));
3340 } catch(IllegalArgumentException e) {
3341 }
3342 assertFalse(pm.isSignedBy(mPkgName, mDefinedKS));
3343 assertFalse(pm.isSignedBy(mPkgName, new KeySet(new Binder())));
3344 assertTrue(pm.isSignedBy(mPkgName, mSigningKS));
3345
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003346 final InstallParams ip1 = installFromRawResource("keysetApi.apk", R.raw.keyset_splat_api,
dcashman9d2f4412014-06-09 09:27:54 -07003347 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003348 try {
3349 assertFalse(pm.isSignedBy(otherPkgName, mDefinedKS));
3350 assertTrue(pm.isSignedBy(otherPkgName, mSigningKS));
3351 } finally {
3352 cleanUpInstall(ip1);
3353 }
dcashman9d2f4412014-06-09 09:27:54 -07003354
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003355 final InstallParams ip2 = installFromRawResource("keysetApi.apk", R.raw.keyset_splata_api,
dcashman9d2f4412014-06-09 09:27:54 -07003356 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003357 try {
3358 assertTrue(pm.isSignedBy(otherPkgName, mDefinedKS));
3359 assertTrue(pm.isSignedBy(otherPkgName, mSigningKS));
3360 } finally {
3361 cleanUpInstall(ip2);
3362 }
dcashman9d2f4412014-06-09 09:27:54 -07003363 }
3364
3365 public void testIsSignedByExactly() throws Exception {
3366 PackageManager pm = getPm();
3367 String mPkgName = mContext.getPackageName();
3368 String otherPkgName = "com.android.frameworks.coretests.keysets_api";
3369 KeySet mSigningKS = pm.getSigningKeySet(mPkgName);
3370 KeySet mDefinedKS = pm.getKeySetByAlias(mPkgName, "A");
3371 try {
3372 assertFalse(pm.isSignedBy(null, null));
3373 assertTrue(false); // should have thrown
3374 } catch (NullPointerException e) {
3375 }
3376 try {
3377 assertFalse(pm.isSignedBy(null, mSigningKS));
3378 assertTrue(false); // should have thrown
3379 } catch (NullPointerException e) {
3380 }
3381 try {
3382 assertFalse(pm.isSignedBy(mPkgName, null));
3383 assertTrue(false); // should have thrown
3384 } catch (NullPointerException e) {
3385 }
3386 try {
3387 assertFalse(pm.isSignedByExactly("keysets.test.bogus.package", mDefinedKS));
3388 } catch(IllegalArgumentException e) {
3389 }
3390 assertFalse(pm.isSignedByExactly(mPkgName, mDefinedKS));
3391 assertFalse(pm.isSignedByExactly(mPkgName, new KeySet(new Binder())));
3392 assertTrue(pm.isSignedByExactly(mPkgName, mSigningKS));
3393
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003394 final InstallParams ip1 = installFromRawResource("keysetApi.apk", R.raw.keyset_splat_api,
dcashman9d2f4412014-06-09 09:27:54 -07003395 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003396 try {
3397 assertFalse(pm.isSignedByExactly(otherPkgName, mDefinedKS));
3398 assertTrue(pm.isSignedByExactly(otherPkgName, mSigningKS));
3399 } finally {
3400 cleanUpInstall(ip1);
3401 }
dcashman9d2f4412014-06-09 09:27:54 -07003402
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003403 final InstallParams ip2 = installFromRawResource("keysetApi.apk", R.raw.keyset_splata_api,
dcashman9d2f4412014-06-09 09:27:54 -07003404 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003405 try {
3406 assertFalse(pm.isSignedByExactly(otherPkgName, mDefinedKS));
3407 assertFalse(pm.isSignedByExactly(otherPkgName, mSigningKS));
3408 } finally {
3409 cleanUpInstall(ip2);
3410 }
dcashman9d2f4412014-06-09 09:27:54 -07003411 }
3412
3413
3414
3415 /**
Kenny Root25c925e2012-09-08 22:02:21 -07003416 * The following tests are related to testing the checkSignatures api.
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003417 */
Kenny Root25c925e2012-09-08 22:02:21 -07003418 private void checkSignatures(int apk1, int apk2, int expMatchResult) throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003419 checkSharedSignatures(apk1, apk2, true, false, -1, expMatchResult);
3420 }
Kenny Root25c925e2012-09-08 22:02:21 -07003421
Brett Chabotf76c56b2010-07-26 17:28:17 -07003422 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003423 public void testCheckSignaturesAllMatch() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003424 int apk1 = APP1_CERT1_CERT2;
3425 int apk2 = APP2_CERT1_CERT2;
3426 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_MATCH);
3427 }
Kenny Root25c925e2012-09-08 22:02:21 -07003428
Brett Chabotf76c56b2010-07-26 17:28:17 -07003429 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003430 public void testCheckSignaturesNoMatch() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003431 int apk1 = APP1_CERT1;
3432 int apk2 = APP2_CERT2;
3433 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_NO_MATCH);
3434 }
Kenny Root25c925e2012-09-08 22:02:21 -07003435
Brett Chabotf76c56b2010-07-26 17:28:17 -07003436 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003437 public void testCheckSignaturesSomeMatch1() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003438 int apk1 = APP1_CERT1_CERT2;
3439 int apk2 = APP2_CERT1;
3440 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_NO_MATCH);
3441 }
Kenny Root25c925e2012-09-08 22:02:21 -07003442
Brett Chabotf76c56b2010-07-26 17:28:17 -07003443 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003444 public void testCheckSignaturesSomeMatch2() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003445 int apk1 = APP1_CERT1_CERT2;
3446 int apk2 = APP2_CERT2;
3447 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_NO_MATCH);
3448 }
Kenny Root25c925e2012-09-08 22:02:21 -07003449
Brett Chabotf76c56b2010-07-26 17:28:17 -07003450 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003451 public void testCheckSignaturesMoreMatch() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003452 int apk1 = APP1_CERT1;
3453 int apk2 = APP2_CERT1_CERT2;
3454 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_NO_MATCH);
3455 }
Kenny Root25c925e2012-09-08 22:02:21 -07003456
Brett Chabotf76c56b2010-07-26 17:28:17 -07003457 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003458 public void testCheckSignaturesUnknown() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003459 int apk1 = APP1_CERT1_CERT2;
3460 int apk2 = APP2_CERT1_CERT2;
3461 String apk1Name = "install1.apk";
3462 String apk2Name = "install2.apk";
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003463
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003464 final InstallParams ip = installFromRawResource(apk1Name, apk1, 0, false,
3465 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003466 try {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003467 PackageManager pm = mContext.getPackageManager();
3468 // Delete app2
3469 File filesDir = mContext.getFilesDir();
3470 File outFile = new File(filesDir, apk2Name);
3471 int rawResId = apk2;
3472 Uri packageURI = getInstallablePackage(rawResId, outFile);
3473 PackageParser.Package pkg = parsePackage(packageURI);
Kenny Roota3e90792012-10-18 10:58:36 -07003474 getPm().deletePackage(pkg.packageName, null, PackageManager.DELETE_ALL_USERS);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003475 // Check signatures now
3476 int match = mContext.getPackageManager().checkSignatures(
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003477 ip.pkg.packageName, pkg.packageName);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003478 assertEquals(PackageManager.SIGNATURE_UNKNOWN_PACKAGE, match);
3479 } finally {
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003480 cleanUpInstall(ip);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003481 }
3482 }
Kenny Root25c925e2012-09-08 22:02:21 -07003483
Brett Chabotf76c56b2010-07-26 17:28:17 -07003484 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003485 public void testInstallNoCertificates() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003486 int apk1 = APP1_UNSIGNED;
3487 String apk1Name = "install1.apk";
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003488
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003489 installFromRawResource(apk1Name, apk1, 0, false,
3490 true, PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES,
3491 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003492 }
Kenny Root25c925e2012-09-08 22:02:21 -07003493
3494 /*
3495 * The following tests are related to apps using shared uids signed with
3496 * different certs.
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003497 */
3498 private int SHARED1_UNSIGNED = R.raw.install_shared1_unsigned;
Kenny Root25c925e2012-09-08 22:02:21 -07003499
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003500 private int SHARED1_CERT1 = R.raw.install_shared1_cert1;
Kenny Root25c925e2012-09-08 22:02:21 -07003501
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003502 private int SHARED1_CERT2 = R.raw.install_shared1_cert2;
Kenny Root25c925e2012-09-08 22:02:21 -07003503
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003504 private int SHARED1_CERT1_CERT2 = R.raw.install_shared1_cert1_cert2;
Kenny Root25c925e2012-09-08 22:02:21 -07003505
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003506 private int SHARED2_UNSIGNED = R.raw.install_shared2_unsigned;
Kenny Root25c925e2012-09-08 22:02:21 -07003507
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003508 private int SHARED2_CERT1 = R.raw.install_shared2_cert1;
Kenny Root25c925e2012-09-08 22:02:21 -07003509
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003510 private int SHARED2_CERT2 = R.raw.install_shared2_cert2;
Kenny Root25c925e2012-09-08 22:02:21 -07003511
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003512 private int SHARED2_CERT1_CERT2 = R.raw.install_shared2_cert1_cert2;
Kenny Root25c925e2012-09-08 22:02:21 -07003513
3514 private void checkSharedSignatures(int apk1, int apk2, boolean cleanUp, boolean fail,
3515 int retCode, int expMatchResult) throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003516 String apk1Name = "install1.apk";
3517 String apk2Name = "install2.apk";
3518 PackageParser.Package pkg1 = getParsedPackage(apk1Name, apk1);
3519 PackageParser.Package pkg2 = getParsedPackage(apk2Name, apk2);
3520
3521 try {
3522 // Clean up before testing first.
3523 cleanUpInstall(pkg1.packageName);
3524 cleanUpInstall(pkg2.packageName);
Kenny Root25c925e2012-09-08 22:02:21 -07003525 installFromRawResource(apk1Name, apk1, 0, false, false, -1,
3526 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003527 if (fail) {
Kenny Root25c925e2012-09-08 22:02:21 -07003528 installFromRawResource(apk2Name, apk2, 0, false, true, retCode,
3529 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003530 } else {
Kenny Root25c925e2012-09-08 22:02:21 -07003531 installFromRawResource(apk2Name, apk2, 0, false, false, -1,
3532 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3533 int match = mContext.getPackageManager().checkSignatures(pkg1.packageName,
3534 pkg2.packageName);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003535 assertEquals(expMatchResult, match);
3536 }
3537 } finally {
3538 if (cleanUp) {
3539 cleanUpInstall(pkg1.packageName);
3540 cleanUpInstall(pkg2.packageName);
3541 }
3542 }
3543 }
Kenny Root25c925e2012-09-08 22:02:21 -07003544
Brett Chabotf76c56b2010-07-26 17:28:17 -07003545 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003546 public void testCheckSignaturesSharedAllMatch() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003547 int apk1 = SHARED1_CERT1_CERT2;
3548 int apk2 = SHARED2_CERT1_CERT2;
3549 boolean fail = false;
3550 int retCode = -1;
3551 int expMatchResult = PackageManager.SIGNATURE_MATCH;
3552 checkSharedSignatures(apk1, apk2, true, fail, retCode, expMatchResult);
3553 }
Kenny Root25c925e2012-09-08 22:02:21 -07003554
Brett Chabotf76c56b2010-07-26 17:28:17 -07003555 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003556 public void testCheckSignaturesSharedNoMatch() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003557 int apk1 = SHARED1_CERT1;
3558 int apk2 = SHARED2_CERT2;
3559 boolean fail = true;
3560 int retCode = PackageManager.INSTALL_FAILED_SHARED_USER_INCOMPATIBLE;
3561 int expMatchResult = -1;
3562 checkSharedSignatures(apk1, apk2, true, fail, retCode, expMatchResult);
3563 }
Kenny Root25c925e2012-09-08 22:02:21 -07003564
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003565 /*
Kenny Root25c925e2012-09-08 22:02:21 -07003566 * Test that an app signed with cert1 and cert2 cannot be replaced when
3567 * signed with cert1 alone.
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003568 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07003569 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003570 public void testCheckSignaturesSharedSomeMatch1() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003571 int apk1 = SHARED1_CERT1_CERT2;
3572 int apk2 = SHARED2_CERT1;
3573 boolean fail = true;
3574 int retCode = PackageManager.INSTALL_FAILED_SHARED_USER_INCOMPATIBLE;
3575 int expMatchResult = -1;
3576 checkSharedSignatures(apk1, apk2, true, fail, retCode, expMatchResult);
3577 }
Kenny Root25c925e2012-09-08 22:02:21 -07003578
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003579 /*
Kenny Root25c925e2012-09-08 22:02:21 -07003580 * Test that an app signed with cert1 and cert2 cannot be replaced when
3581 * signed with cert2 alone.
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003582 */
Brett Chabotf76c56b2010-07-26 17:28:17 -07003583 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003584 public void testCheckSignaturesSharedSomeMatch2() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003585 int apk1 = SHARED1_CERT1_CERT2;
3586 int apk2 = SHARED2_CERT2;
3587 boolean fail = true;
3588 int retCode = PackageManager.INSTALL_FAILED_SHARED_USER_INCOMPATIBLE;
3589 int expMatchResult = -1;
3590 checkSharedSignatures(apk1, apk2, true, fail, retCode, expMatchResult);
3591 }
Kenny Root25c925e2012-09-08 22:02:21 -07003592
Brett Chabotf76c56b2010-07-26 17:28:17 -07003593 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003594 public void testCheckSignaturesSharedUnknown() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003595 int apk1 = SHARED1_CERT1_CERT2;
3596 int apk2 = SHARED2_CERT1_CERT2;
3597 String apk1Name = "install1.apk";
3598 String apk2Name = "install2.apk";
3599 InstallParams ip1 = null;
3600
3601 try {
3602 ip1 = installFromRawResource(apk1Name, apk1, 0, false,
3603 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3604 PackageManager pm = mContext.getPackageManager();
3605 // Delete app2
3606 PackageParser.Package pkg = getParsedPackage(apk2Name, apk2);
Kenny Roota3e90792012-10-18 10:58:36 -07003607 getPm().deletePackage(pkg.packageName, null, PackageManager.DELETE_ALL_USERS);
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003608 // Check signatures now
3609 int match = mContext.getPackageManager().checkSignatures(
3610 ip1.pkg.packageName, pkg.packageName);
3611 assertEquals(PackageManager.SIGNATURE_UNKNOWN_PACKAGE, match);
3612 } finally {
3613 if (ip1 != null) {
3614 cleanUpInstall(ip1);
3615 }
3616 }
3617 }
Neal Nguyenedb979a2010-04-22 13:54:32 -07003618
Brett Chabotf76c56b2010-07-26 17:28:17 -07003619 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003620 public void testReplaceFirstSharedMatchAllCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003621 int apk1 = SHARED1_CERT1;
3622 int apk2 = SHARED2_CERT1;
3623 int rapk1 = SHARED1_CERT1;
3624 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_MATCH);
3625 replaceCerts(apk1, rapk1, true, false, -1);
3626 }
Kenny Root25c925e2012-09-08 22:02:21 -07003627
Brett Chabotf76c56b2010-07-26 17:28:17 -07003628 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003629 public void testReplaceSecondSharedMatchAllCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003630 int apk1 = SHARED1_CERT1;
3631 int apk2 = SHARED2_CERT1;
3632 int rapk2 = SHARED2_CERT1;
3633 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_MATCH);
3634 replaceCerts(apk2, rapk2, true, false, -1);
3635 }
Kenny Root25c925e2012-09-08 22:02:21 -07003636
Brett Chabotf76c56b2010-07-26 17:28:17 -07003637 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003638 public void testReplaceFirstSharedMatchSomeCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003639 int apk1 = SHARED1_CERT1_CERT2;
3640 int apk2 = SHARED2_CERT1_CERT2;
3641 int rapk1 = SHARED1_CERT1;
3642 boolean fail = true;
Jeff Sharkey2a533322014-11-07 16:24:48 -08003643 int retCode = PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE;
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003644 checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH);
3645 installFromRawResource("install.apk", rapk1, PackageManager.INSTALL_REPLACE_EXISTING, true,
3646 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3647 }
Kenny Root25c925e2012-09-08 22:02:21 -07003648
Brett Chabotf76c56b2010-07-26 17:28:17 -07003649 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003650 public void testReplaceSecondSharedMatchSomeCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003651 int apk1 = SHARED1_CERT1_CERT2;
3652 int apk2 = SHARED2_CERT1_CERT2;
3653 int rapk2 = SHARED2_CERT1;
3654 boolean fail = true;
Jeff Sharkey2a533322014-11-07 16:24:48 -08003655 int retCode = PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE;
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003656 checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH);
3657 installFromRawResource("install.apk", rapk2, PackageManager.INSTALL_REPLACE_EXISTING, true,
3658 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3659 }
Kenny Root25c925e2012-09-08 22:02:21 -07003660
Brett Chabotf76c56b2010-07-26 17:28:17 -07003661 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003662 public void testReplaceFirstSharedMatchNoCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003663 int apk1 = SHARED1_CERT1;
3664 int apk2 = SHARED2_CERT1;
3665 int rapk1 = SHARED1_CERT2;
3666 boolean fail = true;
Jeff Sharkey2a533322014-11-07 16:24:48 -08003667 int retCode = PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE;
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003668 checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH);
3669 installFromRawResource("install.apk", rapk1, PackageManager.INSTALL_REPLACE_EXISTING, true,
3670 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3671 }
Kenny Root25c925e2012-09-08 22:02:21 -07003672
Brett Chabotf76c56b2010-07-26 17:28:17 -07003673 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003674 public void testReplaceSecondSharedMatchNoCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003675 int apk1 = SHARED1_CERT1;
3676 int apk2 = SHARED2_CERT1;
3677 int rapk2 = SHARED2_CERT2;
3678 boolean fail = true;
Jeff Sharkey2a533322014-11-07 16:24:48 -08003679 int retCode = PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE;
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003680 checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH);
3681 installFromRawResource("install.apk", rapk2, PackageManager.INSTALL_REPLACE_EXISTING, true,
3682 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3683 }
Kenny Root25c925e2012-09-08 22:02:21 -07003684
Brett Chabotf76c56b2010-07-26 17:28:17 -07003685 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003686 public void testReplaceFirstSharedMatchMoreCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003687 int apk1 = SHARED1_CERT1;
3688 int apk2 = SHARED2_CERT1;
3689 int rapk1 = SHARED1_CERT1_CERT2;
3690 boolean fail = true;
Jeff Sharkey2a533322014-11-07 16:24:48 -08003691 int retCode = PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE;
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003692 checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH);
3693 installFromRawResource("install.apk", rapk1, PackageManager.INSTALL_REPLACE_EXISTING, true,
3694 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3695 }
Kenny Root25c925e2012-09-08 22:02:21 -07003696
Brett Chabotf76c56b2010-07-26 17:28:17 -07003697 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003698 public void testReplaceSecondSharedMatchMoreCerts() throws Exception {
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003699 int apk1 = SHARED1_CERT1;
3700 int apk2 = SHARED2_CERT1;
3701 int rapk2 = SHARED2_CERT1_CERT2;
3702 boolean fail = true;
Jeff Sharkey2a533322014-11-07 16:24:48 -08003703 int retCode = PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE;
Suchi Amalapurapuae181712010-03-30 14:01:02 -07003704 checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH);
3705 installFromRawResource("install.apk", rapk2, PackageManager.INSTALL_REPLACE_EXISTING, true,
3706 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3707 }
Kenny Root1683afa2011-01-07 14:27:50 -08003708
Kenny Root4c3915a2011-01-31 16:36:32 -08003709 /**
3710 * Unknown features should be allowed to install. This prevents older phones
3711 * from rejecting new packages that specify features that didn't exist when
3712 * an older phone existed. All older phones are assumed to have those
3713 * features.
3714 * <p>
3715 * Right now we allow all packages to be installed regardless of their
3716 * features.
3717 */
Kenny Root1683afa2011-01-07 14:27:50 -08003718 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003719 public void testUsesFeatureUnknownFeature() throws Exception {
Kenny Root4c3915a2011-01-31 16:36:32 -08003720 int retCode = PackageManager.INSTALL_SUCCEEDED;
3721 installFromRawResource("install.apk", R.raw.install_uses_feature, 0, true, false, retCode,
Kenny Root1683afa2011-01-07 14:27:50 -08003722 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3723 }
Kenny Root3c676892011-01-12 10:58:40 -08003724
Kenny Root1ebd74a2011-08-03 15:09:44 -07003725 @LargeTest
Kenny Root25c925e2012-09-08 22:02:21 -07003726 public void testInstallNonexistentFile() throws Exception {
Kenny Root1ebd74a2011-08-03 15:09:44 -07003727 int retCode = PackageManager.INSTALL_FAILED_INVALID_URI;
3728 File invalidFile = new File("/nonexistent-file.apk");
3729 invokeInstallPackageFail(Uri.fromFile(invalidFile), 0, retCode);
3730 }
3731
Kenny Root0aaa0d92011-09-12 16:42:55 -07003732 @SmallTest
Kenny Root25c925e2012-09-08 22:02:21 -07003733 public void testGetVerifierDeviceIdentity() throws Exception {
Kenny Root0aaa0d92011-09-12 16:42:55 -07003734 PackageManager pm = getPm();
3735 VerifierDeviceIdentity id = pm.getVerifierDeviceIdentity();
3736
3737 assertNotNull("Verifier device identity should not be null", id);
3738 }
3739
Kenny Root25c925e2012-09-08 22:02:21 -07003740 public void testGetInstalledPackages() throws Exception {
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003741 List<PackageInfo> packages = getPm().getInstalledPackages(0);
3742 assertNotNull("installed packages cannot be null", packages);
3743 assertTrue("installed packages cannot be empty", packages.size() > 0);
3744 }
3745
Kenny Root25c925e2012-09-08 22:02:21 -07003746 public void testGetUnInstalledPackages() throws Exception {
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003747 List<PackageInfo> packages = getPm().getInstalledPackages(
Amith Yamasani0d1fd8d2016-10-12 14:21:51 -07003748 PackageManager.MATCH_UNINSTALLED_PACKAGES);
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003749 assertNotNull("installed packages cannot be null", packages);
3750 assertTrue("installed packages cannot be empty", packages.size() > 0);
3751 }
3752
3753 /**
Kenny Root25c925e2012-09-08 22:02:21 -07003754 * Test that getInstalledPackages returns all the data specified in flags.
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003755 */
Kenny Root25c925e2012-09-08 22:02:21 -07003756 public void testGetInstalledPackagesAll() throws Exception {
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003757 final int flags = PackageManager.GET_ACTIVITIES | PackageManager.GET_GIDS
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003758 | PackageManager.GET_CONFIGURATIONS | PackageManager.GET_INSTRUMENTATION
3759 | PackageManager.GET_PERMISSIONS | PackageManager.GET_PROVIDERS
3760 | PackageManager.GET_RECEIVERS | PackageManager.GET_SERVICES
Amith Yamasani0d1fd8d2016-10-12 14:21:51 -07003761 | PackageManager.GET_SIGNATURES | PackageManager.MATCH_UNINSTALLED_PACKAGES;
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003762
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003763 final InstallParams ip =
3764 installFromRawResource("install.apk", R.raw.install_complete_package_info,
3765 0 /*flags*/, false /*cleanUp*/, false /*fail*/, -1 /*result*/,
3766 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
3767 try {
3768 final List<PackageInfo> packages = getPm().getInstalledPackages(flags);
3769 assertNotNull("installed packages cannot be null", packages);
3770 assertTrue("installed packages cannot be empty", packages.size() > 0);
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003771
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003772 PackageInfo packageInfo = null;
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003773
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003774 // Find the package with all components specified in the AndroidManifest
3775 // to ensure no null values
3776 for (PackageInfo pi : packages) {
3777 if ("com.android.frameworks.coretests.install_complete_package_info"
3778 .equals(pi.packageName)) {
3779 packageInfo = pi;
3780 break;
3781 }
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003782 }
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003783 assertNotNull("activities should not be null", packageInfo.activities);
3784 assertNotNull("configPreferences should not be null", packageInfo.configPreferences);
3785 assertNotNull("instrumentation should not be null", packageInfo.instrumentation);
3786 assertNotNull("permissions should not be null", packageInfo.permissions);
3787 assertNotNull("providers should not be null", packageInfo.providers);
3788 assertNotNull("receivers should not be null", packageInfo.receivers);
3789 assertNotNull("services should not be null", packageInfo.services);
3790 assertNotNull("signatures should not be null", packageInfo.signatures);
3791 } finally {
3792 cleanUpInstall(ip);
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003793 }
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003794 }
3795
3796 /**
3797 * Test that getInstalledPackages returns all the data specified in
3798 * flags when the GET_UNINSTALLED_PACKAGES flag is set.
3799 */
Kenny Root25c925e2012-09-08 22:02:21 -07003800 public void testGetUnInstalledPackagesAll() throws Exception {
Amith Yamasani0d1fd8d2016-10-12 14:21:51 -07003801 final int flags = PackageManager.MATCH_UNINSTALLED_PACKAGES
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003802 | PackageManager.GET_ACTIVITIES | PackageManager.GET_GIDS
3803 | PackageManager.GET_CONFIGURATIONS | PackageManager.GET_INSTRUMENTATION
3804 | PackageManager.GET_PERMISSIONS | PackageManager.GET_PROVIDERS
3805 | PackageManager.GET_RECEIVERS | PackageManager.GET_SERVICES
Amith Yamasani0d1fd8d2016-10-12 14:21:51 -07003806 | PackageManager.GET_SIGNATURES;
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003807
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003808 // first, install the package
3809 final InstallParams ip =
3810 installFromRawResource("install.apk", R.raw.install_complete_package_info,
3811 0 /*flags*/, false /*cleanUp*/, false /*fail*/, -1 /*result*/,
3812 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
3813 try {
3814 // then, remove it, keeping it's data around
3815 final GenericReceiver receiver = new DeleteReceiver(ip.pkg.packageName);
3816 invokeDeletePackage(ip.pkg.packageName, PackageManager.DELETE_KEEP_DATA, receiver);
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003817
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003818 final List<PackageInfo> packages = getPm().getInstalledPackages(flags);
3819 assertNotNull("installed packages cannot be null", packages);
3820 assertTrue("installed packages cannot be empty", packages.size() > 0);
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003821
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003822 PackageInfo packageInfo = null;
3823
3824 // Find the package with all components specified in the AndroidManifest
3825 // to ensure no null values
3826 for (PackageInfo pi : packages) {
3827 if ("com.android.frameworks.coretests.install_complete_package_info"
3828 .equals(pi.packageName)) {
3829 packageInfo = pi;
3830 break;
3831 }
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003832 }
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003833 assertNotNull("activities should not be null", packageInfo.activities);
3834 assertNotNull("configPreferences should not be null", packageInfo.configPreferences);
3835 assertNotNull("instrumentation should not be null", packageInfo.instrumentation);
3836 assertNotNull("permissions should not be null", packageInfo.permissions);
3837 assertNotNull("providers should not be null", packageInfo.providers);
3838 assertNotNull("receivers should not be null", packageInfo.receivers);
3839 assertNotNull("services should not be null", packageInfo.services);
3840 assertNotNull("signatures should not be null", packageInfo.signatures);
3841 } finally {
3842 cleanUpInstall(ip);
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003843 }
Martin Wallgrenf2f1b6c2011-05-26 15:03:52 +02003844 }
3845
Todd Kennedy9f3a78c2015-11-30 16:55:04 -08003846 @Suppress
Kenny Roote15bdc22012-09-16 15:45:38 -07003847 public void testInstall_BadDex_CleanUp() throws Exception {
3848 int retCode = PackageManager.INSTALL_FAILED_DEXOPT;
3849 installFromRawResource("install.apk", R.raw.install_bad_dex, 0, true, true, retCode,
3850 PackageInfo.INSTALL_LOCATION_UNSPECIFIED);
3851 }
3852
Calin Juravle3d2af7f2017-04-19 19:56:21 -07003853 private static class TestDexModuleRegisterCallback
3854 extends PackageManager.DexModuleRegisterCallback {
3855 private String mDexModulePath = null;
3856 private boolean mSuccess = false;
3857 private String mMessage = null;
3858 CountDownLatch doneSignal = new CountDownLatch(1);
3859
3860 @Override
3861 public void onDexModuleRegistered(String dexModulePath, boolean success, String message) {
3862 mDexModulePath = dexModulePath;
3863 mSuccess = success;
3864 mMessage = message;
3865 doneSignal.countDown();
3866 }
3867
3868 boolean waitTillDone() {
3869 long startTime = System.currentTimeMillis();
3870 while (System.currentTimeMillis() - startTime < MAX_WAIT_TIME) {
3871 try {
3872 return doneSignal.await(MAX_WAIT_TIME, TimeUnit.MILLISECONDS);
3873 } catch (InterruptedException e) {
3874 Log.i(TAG, "Interrupted during sleep", e);
3875 }
3876 }
3877 return false;
3878 }
3879
3880 }
3881
3882 // Verify that the base code path cannot be registered.
3883 public void testRegisterDexModuleBaseCode() throws Exception {
3884 PackageManager pm = getPm();
3885 ApplicationInfo info = getContext().getApplicationInfo();
3886 TestDexModuleRegisterCallback callback = new TestDexModuleRegisterCallback();
3887 pm.registerDexModule(info.getBaseCodePath(), callback);
3888 assertTrue(callback.waitTillDone());
3889 assertEquals(info.getBaseCodePath(), callback.mDexModulePath);
3890 assertFalse("BaseCodePath should not be registered", callback.mSuccess);
3891 }
3892
3893 // Verify thatmodules which are not own by the calling package are not registered.
3894 public void testRegisterDexModuleNotOwningModule() throws Exception {
3895 TestDexModuleRegisterCallback callback = new TestDexModuleRegisterCallback();
3896 String moduleBelongingToOtherPackage = "/data/user/0/com.google.android.gms/module.apk";
3897 getPm().registerDexModule(moduleBelongingToOtherPackage, callback);
3898 assertTrue(callback.waitTillDone());
3899 assertEquals(moduleBelongingToOtherPackage, callback.mDexModulePath);
3900 assertTrue(callback.waitTillDone());
3901 assertFalse("Only modules belonging to the calling package can be registered",
3902 callback.mSuccess);
3903 }
3904
3905 // Verify that modules owned by the package are successfully registered.
3906 public void testRegisterDexModuleSuccessfully() throws Exception {
3907 ApplicationInfo info = getContext().getApplicationInfo();
3908 // Copy the main apk into the data folder and use it as a "module".
3909 File dexModuleDir = new File(info.dataDir, "module-dir");
3910 File dexModule = new File(dexModuleDir, "module.apk");
3911 try {
3912 assertNotNull(FileUtils.createDir(
3913 dexModuleDir.getParentFile(), dexModuleDir.getName()));
3914 Files.copy(Paths.get(info.getBaseCodePath()), dexModule.toPath(),
3915 StandardCopyOption.REPLACE_EXISTING);
3916 TestDexModuleRegisterCallback callback = new TestDexModuleRegisterCallback();
3917 getPm().registerDexModule(dexModule.toString(), callback);
3918 assertTrue(callback.waitTillDone());
3919 assertEquals(dexModule.toString(), callback.mDexModulePath);
3920 assertTrue(callback.waitTillDone());
3921 assertTrue(callback.mMessage, callback.mSuccess);
3922
3923 // NOTE:
3924 // This actually verifies internal behaviour which might change. It's not
3925 // ideal but it's the best we can do since there's no other place we can currently
3926 // write a better test.
3927 for(String isa : getAppDexInstructionSets(info)) {
3928 Files.exists(Paths.get(dexModuleDir.toString(), "oat", isa, "module.odex"));
3929 Files.exists(Paths.get(dexModuleDir.toString(), "oat", isa, "module.vdex"));
3930 }
3931 } finally {
3932 FileUtils.deleteContentsAndDir(dexModuleDir);
3933 }
3934 }
3935
3936 // If the module does not exist on disk we should get a failure.
3937 public void testRegisterDexModuleNotExists() throws Exception {
3938 ApplicationInfo info = getContext().getApplicationInfo();
3939 String nonExistentApk = Paths.get(info.dataDir, "non-existent.apk").toString();
3940 TestDexModuleRegisterCallback callback = new TestDexModuleRegisterCallback();
3941 getPm().registerDexModule(nonExistentApk, callback);
3942 assertTrue(callback.waitTillDone());
3943 assertEquals(nonExistentApk, callback.mDexModulePath);
3944 assertTrue(callback.waitTillDone());
3945 assertFalse("DexModule registration should fail", callback.mSuccess);
3946 }
3947
3948 // Copied from com.android.server.pm.InstructionSets because we don't have access to it here.
3949 private static String[] getAppDexInstructionSets(ApplicationInfo info) {
3950 if (info.primaryCpuAbi != null) {
3951 if (info.secondaryCpuAbi != null) {
3952 return new String[] {
3953 VMRuntime.getInstructionSet(info.primaryCpuAbi),
3954 VMRuntime.getInstructionSet(info.secondaryCpuAbi) };
3955 } else {
3956 return new String[] {
3957 VMRuntime.getInstructionSet(info.primaryCpuAbi) };
3958 }
3959 }
3960
3961 return new String[] { VMRuntime.getInstructionSet(Build.SUPPORTED_ABIS[0]) };
3962 }
3963
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07003964 /*---------- Recommended install location tests ----*/
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08003965 /*
3966 * TODO's
3967 * check version numbers for upgrades
3968 * check permissions of installed packages
3969 * how to do tests on updated system apps?
3970 * verify updates to system apps cannot be installed on the sdcard.
3971 */
Suchi Amalapurapuafbaaa12010-02-03 11:24:49 -08003972}