PackageManagerTests.java revision 3a084af2e90849aaa8beb3a610189e3399c63ea0
1/* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.content.pm; 18 19import com.android.frameworks.coretests.R; 20import com.android.internal.content.PackageHelper; 21 22import android.content.BroadcastReceiver; 23import android.content.Context; 24import android.content.Intent; 25import android.content.IntentFilter; 26import android.content.pm.PackageInfo; 27import android.content.pm.PackageManager; 28import android.content.pm.PackageManager.NameNotFoundException; 29import android.content.res.Resources; 30import android.content.res.Resources.NotFoundException; 31import android.net.Uri; 32import android.os.Environment; 33import android.os.FileUtils; 34import android.os.IBinder; 35import android.os.RemoteException; 36import android.os.ServiceManager; 37import android.os.StatFs; 38import android.os.storage.IMountService; 39import android.os.storage.StorageListener; 40import android.os.storage.StorageManager; 41import android.os.storage.StorageResultCode; 42import android.provider.Settings; 43import android.provider.Settings.SettingNotFoundException; 44import android.test.AndroidTestCase; 45import android.test.suitebuilder.annotation.LargeTest; 46import android.test.suitebuilder.annotation.SmallTest; 47import android.util.DisplayMetrics; 48import android.util.Log; 49 50import java.io.File; 51import java.io.IOException; 52import java.io.InputStream; 53 54import java.util.List; 55 56public class PackageManagerTests extends AndroidTestCase { 57 private static final boolean localLOGV = true; 58 public static final String TAG="PackageManagerTests"; 59 public final long MAX_WAIT_TIME = 25*1000; 60 public final long WAIT_TIME_INCR = 5*1000; 61 private static final String SECURE_CONTAINERS_PREFIX = "/mnt/asec"; 62 private static final int APP_INSTALL_AUTO = PackageHelper.APP_INSTALL_AUTO; 63 private static final int APP_INSTALL_DEVICE = PackageHelper.APP_INSTALL_INTERNAL; 64 private static final int APP_INSTALL_SDCARD = PackageHelper.APP_INSTALL_EXTERNAL; 65 private boolean mOrigState; 66 67 void failStr(String errMsg) { 68 Log.w(TAG, "errMsg="+errMsg); 69 fail(errMsg); 70 } 71 void failStr(Exception e) { 72 failStr(e.getMessage()); 73 } 74 75 @Override 76 protected void setUp() throws Exception { 77 super.setUp(); 78 mOrigState = checkMediaState(Environment.MEDIA_MOUNTED); 79 if (!mountMedia()) { 80 Log.i(TAG, "sdcard not mounted? Some of these tests might fail"); 81 } 82 } 83 84 @Override 85 protected void tearDown() throws Exception { 86 // Restore media state. 87 boolean newState = checkMediaState(Environment.MEDIA_MOUNTED); 88 if (newState != mOrigState) { 89 if (mOrigState) { 90 mountMedia(); 91 } else { 92 unmountMedia(); 93 } 94 } 95 super.tearDown(); 96 } 97 98 private class PackageInstallObserver extends IPackageInstallObserver.Stub { 99 public int returnCode; 100 private boolean doneFlag = false; 101 102 public void packageInstalled(String packageName, int returnCode) { 103 synchronized(this) { 104 this.returnCode = returnCode; 105 doneFlag = true; 106 notifyAll(); 107 } 108 } 109 110 public boolean isDone() { 111 return doneFlag; 112 } 113 } 114 115 abstract class GenericReceiver extends BroadcastReceiver { 116 private boolean doneFlag = false; 117 boolean received = false; 118 Intent intent; 119 IntentFilter filter; 120 abstract boolean notifyNow(Intent intent); 121 @Override 122 public void onReceive(Context context, Intent intent) { 123 if (notifyNow(intent)) { 124 synchronized (this) { 125 received = true; 126 doneFlag = true; 127 this.intent = intent; 128 notifyAll(); 129 } 130 } 131 } 132 133 public boolean isDone() { 134 return doneFlag; 135 } 136 137 public void setFilter(IntentFilter filter) { 138 this.filter = filter; 139 } 140 } 141 142 class InstallReceiver extends GenericReceiver { 143 String pkgName; 144 145 InstallReceiver(String pkgName) { 146 this.pkgName = pkgName; 147 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); 148 filter.addDataScheme("package"); 149 super.setFilter(filter); 150 } 151 152 public boolean notifyNow(Intent intent) { 153 String action = intent.getAction(); 154 if (!Intent.ACTION_PACKAGE_ADDED.equals(action)) { 155 return false; 156 } 157 Uri data = intent.getData(); 158 String installedPkg = data.getEncodedSchemeSpecificPart(); 159 if (pkgName.equals(installedPkg)) { 160 return true; 161 } 162 return false; 163 } 164 } 165 166 private PackageManager getPm() { 167 return mContext.getPackageManager(); 168 } 169 170 private IPackageManager getIPm() { 171 IPackageManager ipm = IPackageManager.Stub.asInterface( 172 ServiceManager.getService("package")); 173 return ipm; 174 } 175 176 public void invokeInstallPackage(Uri packageURI, int flags, GenericReceiver receiver, 177 boolean shouldSucceed) { 178 PackageInstallObserver observer = new PackageInstallObserver(); 179 mContext.registerReceiver(receiver, receiver.filter); 180 try { 181 // Wait on observer 182 synchronized(observer) { 183 synchronized (receiver) { 184 getPm().installPackage(packageURI, observer, flags, null); 185 long waitTime = 0; 186 while((!observer.isDone()) && (waitTime < MAX_WAIT_TIME) ) { 187 try { 188 observer.wait(WAIT_TIME_INCR); 189 waitTime += WAIT_TIME_INCR; 190 } catch (InterruptedException e) { 191 Log.i(TAG, "Interrupted during sleep", e); 192 } 193 } 194 if(!observer.isDone()) { 195 fail("Timed out waiting for packageInstalled callback"); 196 } 197 198 if (shouldSucceed) { 199 if (observer.returnCode != PackageManager.INSTALL_SUCCEEDED) { 200 fail("Package installation should have succeeded, but got code " 201 + observer.returnCode); 202 } 203 } else { 204 if (observer.returnCode == PackageManager.INSTALL_SUCCEEDED) { 205 fail("Package installation should fail"); 206 } 207 208 /* 209 * We'll never expect get a notification since we 210 * shouldn't succeed. 211 */ 212 return; 213 } 214 215 // Verify we received the broadcast 216 waitTime = 0; 217 while((!receiver.isDone()) && (waitTime < MAX_WAIT_TIME) ) { 218 try { 219 receiver.wait(WAIT_TIME_INCR); 220 waitTime += WAIT_TIME_INCR; 221 } catch (InterruptedException e) { 222 Log.i(TAG, "Interrupted during sleep", e); 223 } 224 } 225 if(!receiver.isDone()) { 226 fail("Timed out waiting for PACKAGE_ADDED notification"); 227 } 228 } 229 } 230 } finally { 231 mContext.unregisterReceiver(receiver); 232 } 233 } 234 235 public void invokeInstallPackageFail(Uri packageURI, int flags, int expectedResult) { 236 PackageInstallObserver observer = new PackageInstallObserver(); 237 try { 238 // Wait on observer 239 synchronized(observer) { 240 getPm().installPackage(packageURI, observer, flags, null); 241 long waitTime = 0; 242 while((!observer.isDone()) && (waitTime < MAX_WAIT_TIME) ) { 243 try { 244 observer.wait(WAIT_TIME_INCR); 245 waitTime += WAIT_TIME_INCR; 246 } catch (InterruptedException e) { 247 Log.i(TAG, "Interrupted during sleep", e); 248 } 249 } 250 if(!observer.isDone()) { 251 fail("Timed out waiting for packageInstalled callback"); 252 } 253 assertEquals(expectedResult, observer.returnCode); 254 } 255 } finally { 256 } 257 } 258 259 Uri getInstallablePackage(int fileResId, File outFile) { 260 Resources res = mContext.getResources(); 261 InputStream is = null; 262 try { 263 is = res.openRawResource(fileResId); 264 } catch (NotFoundException e) { 265 failStr("Failed to load resource with id: " + fileResId); 266 } 267 FileUtils.setPermissions(outFile.getPath(), 268 FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IRWXO, 269 -1, -1); 270 assertTrue(FileUtils.copyToFile(is, outFile)); 271 FileUtils.setPermissions(outFile.getPath(), 272 FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IRWXO, 273 -1, -1); 274 return Uri.fromFile(outFile); 275 } 276 277 private PackageParser.Package parsePackage(Uri packageURI) { 278 final String archiveFilePath = packageURI.getPath(); 279 PackageParser packageParser = new PackageParser(archiveFilePath); 280 File sourceFile = new File(archiveFilePath); 281 DisplayMetrics metrics = new DisplayMetrics(); 282 metrics.setToDefaults(); 283 PackageParser.Package pkg = packageParser.parsePackage(sourceFile, archiveFilePath, metrics, 0); 284 packageParser = null; 285 return pkg; 286 } 287 private boolean checkSd(long pkgLen) { 288 String status = Environment.getExternalStorageState(); 289 if (!status.equals(Environment.MEDIA_MOUNTED)) { 290 return false; 291 } 292 long sdSize = -1; 293 StatFs sdStats = new StatFs( 294 Environment.getExternalStorageDirectory().getPath()); 295 sdSize = (long)sdStats.getAvailableBlocks() * 296 (long)sdStats.getBlockSize(); 297 // TODO check for thresholds here 298 return pkgLen <= sdSize; 299 300 } 301 private boolean checkInt(long pkgLen) { 302 StatFs intStats = new StatFs(Environment.getDataDirectory().getPath()); 303 long intSize = (long)intStats.getBlockCount() * 304 (long)intStats.getBlockSize(); 305 long iSize = (long)intStats.getAvailableBlocks() * 306 (long)intStats.getBlockSize(); 307 // TODO check for thresholds here? 308 return pkgLen <= iSize; 309 } 310 private static final int INSTALL_LOC_INT = 1; 311 private static final int INSTALL_LOC_SD = 2; 312 private static final int INSTALL_LOC_ERR = -1; 313 private int getInstallLoc(int flags, int expInstallLocation, long pkgLen) { 314 // Flags explicitly over ride everything else. 315 if ((flags & PackageManager.INSTALL_EXTERNAL) != 0 ) { 316 return INSTALL_LOC_SD; 317 } else if ((flags & PackageManager.INSTALL_INTERNAL) != 0) { 318 return INSTALL_LOC_INT; 319 } 320 // Manifest option takes precedence next 321 if (expInstallLocation == PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL) { 322 if (checkSd(pkgLen)) { 323 return INSTALL_LOC_SD; 324 } 325 if (checkInt(pkgLen)) { 326 return INSTALL_LOC_INT; 327 } 328 return INSTALL_LOC_ERR; 329 } 330 if (expInstallLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY) { 331 if (checkInt(pkgLen)) { 332 return INSTALL_LOC_INT; 333 } 334 return INSTALL_LOC_ERR; 335 } 336 if (expInstallLocation == PackageInfo.INSTALL_LOCATION_AUTO) { 337 // Check for free memory internally 338 if (checkInt(pkgLen)) { 339 return INSTALL_LOC_INT; 340 } 341 // Check for free memory externally 342 if (checkSd(pkgLen)) { 343 return INSTALL_LOC_SD; 344 } 345 return INSTALL_LOC_ERR; 346 } 347 // Check for settings preference. 348 boolean checkSd = false; 349 int userPref = getDefaultInstallLoc(); 350 if (userPref == APP_INSTALL_DEVICE) { 351 if (checkInt(pkgLen)) { 352 return INSTALL_LOC_INT; 353 } 354 return INSTALL_LOC_ERR; 355 } else if (userPref == APP_INSTALL_SDCARD) { 356 if (checkSd(pkgLen)) { 357 return INSTALL_LOC_SD; 358 } 359 return INSTALL_LOC_ERR; 360 } 361 // Default system policy for apps with no manifest option specified. 362 // Check for free memory internally 363 if (checkInt(pkgLen)) { 364 return INSTALL_LOC_INT; 365 } 366 return INSTALL_LOC_ERR; 367 } 368 369 private void assertInstall(PackageParser.Package pkg, int flags, int expInstallLocation) { 370 try { 371 String pkgName = pkg.packageName; 372 ApplicationInfo info = getPm().getApplicationInfo(pkgName, 0); 373 assertNotNull(info); 374 assertEquals(pkgName, info.packageName); 375 File dataDir = Environment.getDataDirectory(); 376 String appInstallPath = new File(dataDir, "app").getPath(); 377 String drmInstallPath = new File(dataDir, "app-private").getPath(); 378 File srcDir = new File(info.sourceDir); 379 String srcPath = srcDir.getParent(); 380 File publicSrcDir = new File(info.publicSourceDir); 381 String publicSrcPath = publicSrcDir.getParent(); 382 long pkgLen = new File(info.sourceDir).length(); 383 384 int rLoc = getInstallLoc(flags, expInstallLocation, pkgLen); 385 if (rLoc == INSTALL_LOC_INT) { 386 if ((flags & PackageManager.INSTALL_FORWARD_LOCK) != 0) { 387 assertTrue("The application should be installed forward locked", 388 (info.flags & ApplicationInfo.FLAG_FORWARD_LOCK) != 0); 389 assertTrue("The APK path (" + srcPath + ") should start with " 390 + SECURE_CONTAINERS_PREFIX, 391 srcPath.startsWith(SECURE_CONTAINERS_PREFIX)); 392 assertTrue("The public APK path (" + publicSrcPath + ") should start with " 393 + SECURE_CONTAINERS_PREFIX, 394 publicSrcPath.startsWith(SECURE_CONTAINERS_PREFIX)); 395 assertTrue("The native library path (" + info.nativeLibraryDir 396 + ") should start with " + SECURE_CONTAINERS_PREFIX, 397 info.nativeLibraryDir.startsWith(SECURE_CONTAINERS_PREFIX)); 398 } else { 399 assertFalse((info.flags & ApplicationInfo.FLAG_FORWARD_LOCK) != 0); 400 assertEquals(srcPath, appInstallPath); 401 assertEquals(publicSrcPath, appInstallPath); 402 assertTrue(info.nativeLibraryDir.startsWith(dataDir.getPath())); 403 } 404 assertFalse((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0); 405 406 // Make sure the native library dir is not a symlink 407 final File nativeLibDir = new File(info.nativeLibraryDir); 408 assertTrue("Native library dir should exist at " + info.nativeLibraryDir, 409 nativeLibDir.exists()); 410 try { 411 assertEquals("Native library dir should not be a symlink", 412 info.nativeLibraryDir, 413 nativeLibDir.getCanonicalPath()); 414 } catch (IOException e) { 415 fail("Can't read " + nativeLibDir.getPath()); 416 } 417 } else if (rLoc == INSTALL_LOC_SD){ 418 if ((flags & PackageManager.INSTALL_FORWARD_LOCK) != 0) { 419 assertTrue("The application should be installed forward locked", 420 (info.flags & ApplicationInfo.FLAG_FORWARD_LOCK) != 0); 421 } else { 422 assertFalse("The application should not be installed forward locked", 423 (info.flags & ApplicationInfo.FLAG_FORWARD_LOCK) != 0); 424 } 425 assertTrue("Application flags (" + info.flags 426 + ") should contain FLAG_EXTERNAL_STORAGE", 427 (info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0); 428 // Might need to check: 429 // ((info.flags & ApplicationInfo.FLAG_FORWARD_LOCK) != 0) 430 assertTrue("The APK path (" + srcPath + ") should start with " 431 + SECURE_CONTAINERS_PREFIX, srcPath.startsWith(SECURE_CONTAINERS_PREFIX)); 432 assertTrue("The public APK path (" + publicSrcPath + ") should start with " 433 + SECURE_CONTAINERS_PREFIX, 434 publicSrcPath.startsWith(SECURE_CONTAINERS_PREFIX)); 435 assertTrue("The native library path (" + info.nativeLibraryDir 436 + ") should start with " + SECURE_CONTAINERS_PREFIX, 437 info.nativeLibraryDir.startsWith(SECURE_CONTAINERS_PREFIX)); 438 439 // Make sure the native library in /data/data/<app>/lib is a 440 // symlink to the ASEC 441 final File nativeLibSymLink = new File(info.dataDir, "lib"); 442 assertTrue("Native library symlink should exist at " + nativeLibSymLink.getPath(), 443 nativeLibSymLink.exists()); 444 try { 445 assertEquals(nativeLibSymLink.getPath() + " should be a symlink to " 446 + info.nativeLibraryDir, info.nativeLibraryDir, nativeLibSymLink 447 .getCanonicalPath()); 448 } catch (IOException e) { 449 fail("Can't read " + nativeLibSymLink.getPath()); 450 } 451 } else { 452 // TODO handle error. Install should have failed. 453 fail("Install should have failed"); 454 } 455 } catch (NameNotFoundException e) { 456 failStr("failed with exception : " + e); 457 } 458 } 459 460 private void assertNotInstalled(String pkgName) { 461 try { 462 ApplicationInfo info = getPm().getApplicationInfo(pkgName, 0); 463 fail(pkgName + " shouldnt be installed"); 464 } catch (NameNotFoundException e) { 465 } 466 } 467 468 class InstallParams { 469 Uri packageURI; 470 PackageParser.Package pkg; 471 InstallParams(String outFileName, int rawResId) { 472 this.pkg = getParsedPackage(outFileName, rawResId); 473 this.packageURI = Uri.fromFile(new File(pkg.mScanPath)); 474 } 475 InstallParams(PackageParser.Package pkg) { 476 this.packageURI = Uri.fromFile(new File(pkg.mScanPath)); 477 this.pkg = pkg; 478 } 479 long getApkSize() { 480 File file = new File(pkg.mScanPath); 481 return file.length(); 482 } 483 } 484 485 private InstallParams sampleInstallFromRawResource(int flags, boolean cleanUp) { 486 return installFromRawResource("install.apk", R.raw.install, flags, cleanUp, 487 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); 488 } 489 490 static final String PERM_PACKAGE = "package"; 491 static final String PERM_DEFINED = "defined"; 492 static final String PERM_UNDEFINED = "undefined"; 493 static final String PERM_USED = "used"; 494 static final String PERM_NOTUSED = "notused"; 495 496 private void assertPermissions(String[] cmds) { 497 final PackageManager pm = getPm(); 498 String pkg = null; 499 PackageInfo pkgInfo = null; 500 String mode = PERM_DEFINED; 501 int i = 0; 502 while (i < cmds.length) { 503 String cmd = cmds[i++]; 504 if (cmd == PERM_PACKAGE) { 505 pkg = cmds[i++]; 506 try { 507 pkgInfo = pm.getPackageInfo(pkg, 508 PackageManager.GET_PERMISSIONS 509 | PackageManager.GET_UNINSTALLED_PACKAGES); 510 } catch (NameNotFoundException e) { 511 pkgInfo = null; 512 } 513 } else if (cmd == PERM_DEFINED || cmd == PERM_UNDEFINED 514 || cmd == PERM_USED || cmd == PERM_NOTUSED) { 515 mode = cmds[i++]; 516 } else { 517 if (mode == PERM_DEFINED) { 518 try { 519 PermissionInfo pi = pm.getPermissionInfo(cmd, 0); 520 assertNotNull(pi); 521 assertEquals(pi.packageName, pkg); 522 assertEquals(pi.name, cmd); 523 assertNotNull(pkgInfo); 524 boolean found = false; 525 for (int j=0; j<pkgInfo.permissions.length && !found; j++) { 526 if (pkgInfo.permissions[j].name.equals(cmd)) { 527 found = true; 528 } 529 } 530 if (!found) { 531 fail("Permission not found: " + cmd); 532 } 533 } catch (NameNotFoundException e) { 534 throw new RuntimeException(e); 535 } 536 } else if (mode == PERM_UNDEFINED) { 537 try { 538 pm.getPermissionInfo(cmd, 0); 539 throw new RuntimeException("Permission exists: " + cmd); 540 } catch (NameNotFoundException e) { 541 } 542 if (pkgInfo != null) { 543 boolean found = false; 544 for (int j=0; j<pkgInfo.permissions.length && !found; j++) { 545 if (pkgInfo.permissions[j].name.equals(cmd)) { 546 found = true; 547 } 548 } 549 if (found) { 550 fail("Permission still exists: " + cmd); 551 } 552 } 553 } else if (mode == PERM_USED || mode == PERM_NOTUSED) { 554 boolean found = false; 555 for (int j=0; j<pkgInfo.requestedPermissions.length && !found; j++) { 556 if (pkgInfo.requestedPermissions[j].equals(cmd)) { 557 found = true; 558 } 559 } 560 if (!found) { 561 fail("Permission not requested: " + cmd); 562 } 563 if (mode == PERM_USED) { 564 if (pm.checkPermission(cmd, pkg) 565 != PackageManager.PERMISSION_GRANTED) { 566 fail("Permission not granted: " + cmd); 567 } 568 } else { 569 if (pm.checkPermission(cmd, pkg) 570 != PackageManager.PERMISSION_DENIED) { 571 fail("Permission granted: " + cmd); 572 } 573 } 574 } 575 } 576 } 577 } 578 579 private PackageParser.Package getParsedPackage(String outFileName, int rawResId) { 580 PackageManager pm = mContext.getPackageManager(); 581 File filesDir = mContext.getFilesDir(); 582 File outFile = new File(filesDir, outFileName); 583 Uri packageURI = getInstallablePackage(rawResId, outFile); 584 PackageParser.Package pkg = parsePackage(packageURI); 585 return pkg; 586 } 587 588 /* 589 * Utility function that reads a apk bundled as a raw resource 590 * copies it into own data directory and invokes 591 * PackageManager api to install it. 592 */ 593 private void installFromRawResource(InstallParams ip, 594 int flags, boolean cleanUp, boolean fail, int result, 595 int expInstallLocation) { 596 PackageManager pm = mContext.getPackageManager(); 597 PackageParser.Package pkg = ip.pkg; 598 Uri packageURI = ip.packageURI; 599 if ((flags & PackageManager.INSTALL_REPLACE_EXISTING) == 0) { 600 // Make sure the package doesn't exist 601 try { 602 ApplicationInfo appInfo = pm.getApplicationInfo(pkg.packageName, 603 PackageManager.GET_UNINSTALLED_PACKAGES); 604 GenericReceiver receiver = new DeleteReceiver(pkg.packageName); 605 invokeDeletePackage(pkg.packageName, 0, receiver); 606 } catch (NameNotFoundException e1) { 607 } catch (Exception e) { 608 failStr(e); 609 } 610 } 611 try { 612 if (fail) { 613 invokeInstallPackageFail(packageURI, flags, result); 614 if ((flags & PackageManager.INSTALL_REPLACE_EXISTING) == 0) { 615 assertNotInstalled(pkg.packageName); 616 } 617 } else { 618 InstallReceiver receiver = new InstallReceiver(pkg.packageName); 619 invokeInstallPackage(packageURI, flags, receiver, true); 620 // Verify installed information 621 assertInstall(pkg, flags, expInstallLocation); 622 } 623 } finally { 624 if (cleanUp) { 625 cleanUpInstall(ip); 626 } 627 } 628 } 629 630 /* 631 * Utility function that reads a apk bundled as a raw resource 632 * copies it into own data directory and invokes 633 * PackageManager api to install it. 634 */ 635 private InstallParams installFromRawResource(String outFileName, 636 int rawResId, int flags, boolean cleanUp, boolean fail, int result, 637 int expInstallLocation) { 638 InstallParams ip = new InstallParams(outFileName, rawResId); 639 installFromRawResource(ip, flags, cleanUp, fail, result, expInstallLocation); 640 return ip; 641 } 642 643 @LargeTest 644 public void testInstallNormalInternal() { 645 sampleInstallFromRawResource(0, true); 646 } 647 648 @LargeTest 649 public void testInstallFwdLockedInternal() { 650 sampleInstallFromRawResource(PackageManager.INSTALL_FORWARD_LOCK, true); 651 } 652 653 @LargeTest 654 public void testInstallSdcard() { 655 // Do not run on devices with emulated external storage. 656 if (Environment.isExternalStorageEmulated()) { 657 return; 658 } 659 660 mountMedia(); 661 sampleInstallFromRawResource(PackageManager.INSTALL_EXTERNAL, true); 662 } 663 664 /* ------------------------- Test replacing packages --------------*/ 665 class ReplaceReceiver extends GenericReceiver { 666 String pkgName; 667 final static int INVALID = -1; 668 final static int REMOVED = 1; 669 final static int ADDED = 2; 670 final static int REPLACED = 3; 671 int removed = INVALID; 672 // for updated system apps only 673 boolean update = false; 674 675 ReplaceReceiver(String pkgName) { 676 this.pkgName = pkgName; 677 filter = new IntentFilter(Intent.ACTION_PACKAGE_REMOVED); 678 filter.addAction(Intent.ACTION_PACKAGE_ADDED); 679 if (update) { 680 filter.addAction(Intent.ACTION_PACKAGE_REPLACED); 681 } 682 filter.addDataScheme("package"); 683 super.setFilter(filter); 684 } 685 686 public boolean notifyNow(Intent intent) { 687 String action = intent.getAction(); 688 Uri data = intent.getData(); 689 String installedPkg = data.getEncodedSchemeSpecificPart(); 690 if (pkgName == null || !pkgName.equals(installedPkg)) { 691 return false; 692 } 693 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) { 694 removed = REMOVED; 695 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) { 696 if (removed != REMOVED) { 697 return false; 698 } 699 boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); 700 if (!replacing) { 701 return false; 702 } 703 removed = ADDED; 704 if (!update) { 705 return true; 706 } 707 } else if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) { 708 if (removed != ADDED) { 709 return false; 710 } 711 removed = REPLACED; 712 return true; 713 } 714 return false; 715 } 716 } 717 718 /* 719 * Utility function that reads a apk bundled as a raw resource 720 * copies it into own data directory and invokes 721 * PackageManager api to install first and then replace it 722 * again. 723 */ 724 private void sampleReplaceFromRawResource(int flags) { 725 InstallParams ip = sampleInstallFromRawResource(flags, false); 726 boolean replace = ((flags & PackageManager.INSTALL_REPLACE_EXISTING) != 0); 727 Log.i(TAG, "replace=" + replace); 728 GenericReceiver receiver; 729 if (replace) { 730 receiver = new ReplaceReceiver(ip.pkg.packageName); 731 Log.i(TAG, "Creating replaceReceiver"); 732 } else { 733 receiver = new InstallReceiver(ip.pkg.packageName); 734 } 735 try { 736 invokeInstallPackage(ip.packageURI, flags, receiver, replace); 737 if (replace) { 738 assertInstall(ip.pkg, flags, ip.pkg.installLocation); 739 } 740 } finally { 741 cleanUpInstall(ip); 742 } 743 } 744 745 @LargeTest 746 public void testReplaceFailNormalInternal() { 747 sampleReplaceFromRawResource(0); 748 } 749 750 @LargeTest 751 public void testReplaceFailFwdLockedInternal() { 752 sampleReplaceFromRawResource(PackageManager.INSTALL_FORWARD_LOCK); 753 } 754 755 @LargeTest 756 public void testReplaceFailSdcard() { 757 // Do not run on devices with emulated external storage. 758 if (Environment.isExternalStorageEmulated()) { 759 return; 760 } 761 762 sampleReplaceFromRawResource(PackageManager.INSTALL_EXTERNAL); 763 } 764 765 @LargeTest 766 public void testReplaceNormalInternal() { 767 sampleReplaceFromRawResource(PackageManager.INSTALL_REPLACE_EXISTING); 768 } 769 770 @LargeTest 771 public void testReplaceFwdLockedInternal() { 772 sampleReplaceFromRawResource(PackageManager.INSTALL_REPLACE_EXISTING | 773 PackageManager.INSTALL_FORWARD_LOCK); 774 } 775 776 @LargeTest 777 public void testReplaceSdcard() { 778 // Do not run on devices with emulated external storage. 779 if (Environment.isExternalStorageEmulated()) { 780 return; 781 } 782 783 sampleReplaceFromRawResource(PackageManager.INSTALL_REPLACE_EXISTING | 784 PackageManager.INSTALL_EXTERNAL); 785 } 786 787 /* -------------- Delete tests ---*/ 788 class DeleteObserver extends IPackageDeleteObserver.Stub { 789 790 public boolean succeeded; 791 private boolean doneFlag = false; 792 793 public boolean isDone() { 794 return doneFlag; 795 } 796 797 public void packageDeleted(String packageName, int returnCode) throws RemoteException { 798 synchronized(this) { 799 this.succeeded = returnCode == PackageManager.DELETE_SUCCEEDED; 800 doneFlag = true; 801 notifyAll(); 802 } 803 } 804 } 805 806 class DeleteReceiver extends GenericReceiver { 807 String pkgName; 808 809 DeleteReceiver(String pkgName) { 810 this.pkgName = pkgName; 811 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_REMOVED); 812 filter.addDataScheme("package"); 813 super.setFilter(filter); 814 } 815 816 public boolean notifyNow(Intent intent) { 817 String action = intent.getAction(); 818 if (!Intent.ACTION_PACKAGE_REMOVED.equals(action)) { 819 return false; 820 } 821 Uri data = intent.getData(); 822 String installedPkg = data.getEncodedSchemeSpecificPart(); 823 if (pkgName.equals(installedPkg)) { 824 return true; 825 } 826 return false; 827 } 828 } 829 830 public boolean invokeDeletePackage(final String pkgName, int flags, 831 GenericReceiver receiver) throws Exception { 832 DeleteObserver observer = new DeleteObserver(); 833 final boolean received = false; 834 mContext.registerReceiver(receiver, receiver.filter); 835 try { 836 // Wait on observer 837 synchronized(observer) { 838 synchronized (receiver) { 839 getPm().deletePackage(pkgName, observer, flags); 840 long waitTime = 0; 841 while((!observer.isDone()) && (waitTime < MAX_WAIT_TIME) ) { 842 observer.wait(WAIT_TIME_INCR); 843 waitTime += WAIT_TIME_INCR; 844 } 845 if(!observer.isDone()) { 846 throw new Exception("Timed out waiting for packageInstalled callback"); 847 } 848 // Verify we received the broadcast 849 waitTime = 0; 850 while((!receiver.isDone()) && (waitTime < MAX_WAIT_TIME) ) { 851 receiver.wait(WAIT_TIME_INCR); 852 waitTime += WAIT_TIME_INCR; 853 } 854 if(!receiver.isDone()) { 855 throw new Exception("Timed out waiting for PACKAGE_REMOVED notification"); 856 } 857 return receiver.received; 858 } 859 } 860 } finally { 861 mContext.unregisterReceiver(receiver); 862 } 863 } 864 865 public void deleteFromRawResource(int iFlags, int dFlags) { 866 InstallParams ip = sampleInstallFromRawResource(iFlags, false); 867 boolean retainData = ((dFlags & PackageManager.DONT_DELETE_DATA) != 0); 868 GenericReceiver receiver = new DeleteReceiver(ip.pkg.packageName); 869 DeleteObserver observer = new DeleteObserver(); 870 try { 871 assertTrue(invokeDeletePackage(ip.pkg.packageName, dFlags, receiver)); 872 ApplicationInfo info = null; 873 Log.i(TAG, "okay4"); 874 try { 875 info = getPm().getApplicationInfo(ip.pkg.packageName, 876 PackageManager.GET_UNINSTALLED_PACKAGES); 877 } catch (NameNotFoundException e) { 878 info = null; 879 } 880 if (retainData) { 881 assertNotNull(info); 882 assertEquals(info.packageName, ip.pkg.packageName); 883 File file = new File(info.dataDir); 884 assertTrue(file.exists()); 885 } else { 886 assertNull(info); 887 } 888 } catch (Exception e) { 889 failStr(e); 890 } finally { 891 cleanUpInstall(ip); 892 } 893 } 894 895 @LargeTest 896 public void testDeleteNormalInternal() { 897 deleteFromRawResource(0, 0); 898 } 899 900 @LargeTest 901 public void testDeleteFwdLockedInternal() { 902 deleteFromRawResource(PackageManager.INSTALL_FORWARD_LOCK, 0); 903 } 904 905 @LargeTest 906 public void testDeleteSdcard() { 907 // Do not run on devices with emulated external storage. 908 if (Environment.isExternalStorageEmulated()) { 909 return; 910 } 911 912 deleteFromRawResource(PackageManager.INSTALL_EXTERNAL, 0); 913 } 914 915 @LargeTest 916 public void testDeleteNormalInternalRetainData() { 917 deleteFromRawResource(0, PackageManager.DONT_DELETE_DATA); 918 } 919 920 @LargeTest 921 public void testDeleteFwdLockedInternalRetainData() { 922 deleteFromRawResource(PackageManager.INSTALL_FORWARD_LOCK, PackageManager.DONT_DELETE_DATA); 923 } 924 925 @LargeTest 926 public void testDeleteSdcardRetainData() { 927 // Do not run on devices with emulated external storage. 928 if (Environment.isExternalStorageEmulated()) { 929 return; 930 } 931 932 deleteFromRawResource(PackageManager.INSTALL_EXTERNAL, PackageManager.DONT_DELETE_DATA); 933 } 934 935 /* sdcard mount/unmount tests ******/ 936 937 class SdMountReceiver extends GenericReceiver { 938 String pkgNames[]; 939 boolean status = true; 940 941 SdMountReceiver(String[] pkgNames) { 942 this.pkgNames = pkgNames; 943 IntentFilter filter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); 944 super.setFilter(filter); 945 } 946 947 public boolean notifyNow(Intent intent) { 948 Log.i(TAG, "okay 1"); 949 String action = intent.getAction(); 950 if (!Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) { 951 return false; 952 } 953 String rpkgList[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); 954 for (String pkg : pkgNames) { 955 boolean found = false; 956 for (String rpkg : rpkgList) { 957 if (rpkg.equals(pkg)) { 958 found = true; 959 break; 960 } 961 } 962 if (!found) { 963 status = false; 964 return true; 965 } 966 } 967 return true; 968 } 969 } 970 971 class SdUnMountReceiver extends GenericReceiver { 972 String pkgNames[]; 973 boolean status = true; 974 975 SdUnMountReceiver(String[] pkgNames) { 976 this.pkgNames = pkgNames; 977 IntentFilter filter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); 978 super.setFilter(filter); 979 } 980 981 public boolean notifyNow(Intent intent) { 982 String action = intent.getAction(); 983 if (!Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) { 984 return false; 985 } 986 String rpkgList[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); 987 for (String pkg : pkgNames) { 988 boolean found = false; 989 for (String rpkg : rpkgList) { 990 if (rpkg.equals(pkg)) { 991 found = true; 992 break; 993 } 994 } 995 if (!found) { 996 status = false; 997 return true; 998 } 999 } 1000 return true; 1001 } 1002 } 1003 1004 IMountService getMs() { 1005 IBinder service = ServiceManager.getService("mount"); 1006 if (service != null) { 1007 return IMountService.Stub.asInterface(service); 1008 } else { 1009 Log.e(TAG, "Can't get mount service"); 1010 } 1011 return null; 1012 } 1013 1014 boolean checkMediaState(String desired) { 1015 try { 1016 String mPath = Environment.getExternalStorageDirectory().getPath(); 1017 String actual = getMs().getVolumeState(mPath); 1018 if (desired.equals(actual)) { 1019 return true; 1020 } else { 1021 return false; 1022 } 1023 } catch (RemoteException e) { 1024 Log.e(TAG, "Exception while checking media state", e); 1025 return false; 1026 } 1027 } 1028 1029 boolean mountMedia() { 1030 // We can't mount emulated storage. 1031 if (Environment.isExternalStorageEmulated()) { 1032 return true; 1033 } 1034 1035 if (checkMediaState(Environment.MEDIA_MOUNTED)) { 1036 return true; 1037 } 1038 1039 final String path = Environment.getExternalStorageDirectory().toString(); 1040 StorageListener observer = new StorageListener(Environment.MEDIA_MOUNTED); 1041 StorageManager sm = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE); 1042 sm.registerListener(observer); 1043 try { 1044 // Wait on observer 1045 synchronized (observer) { 1046 int ret = getMs().mountVolume(path); 1047 if (ret != StorageResultCode.OperationSucceeded) { 1048 throw new Exception("Could not mount the media"); 1049 } 1050 long waitTime = 0; 1051 while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) { 1052 observer.wait(WAIT_TIME_INCR); 1053 waitTime += WAIT_TIME_INCR; 1054 } 1055 if (!observer.isDone()) { 1056 throw new Exception("Timed out waiting for unmount media notification"); 1057 } 1058 return true; 1059 } 1060 } catch (Exception e) { 1061 Log.e(TAG, "Exception : " + e); 1062 return false; 1063 } finally { 1064 sm.unregisterListener(observer); 1065 } 1066 } 1067 1068 private boolean unmountMedia() { 1069 // We can't unmount emulated storage. 1070 if (Environment.isExternalStorageEmulated()) { 1071 return true; 1072 } 1073 1074 if (checkMediaState(Environment.MEDIA_UNMOUNTED)) { 1075 return true; 1076 } 1077 1078 final String path = Environment.getExternalStorageDirectory().getPath(); 1079 StorageListener observer = new StorageListener(Environment.MEDIA_UNMOUNTED); 1080 StorageManager sm = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE); 1081 sm.registerListener(observer); 1082 try { 1083 // Wait on observer 1084 synchronized(observer) { 1085 getMs().unmountVolume(path, true, false); 1086 long waitTime = 0; 1087 while((!observer.isDone()) && (waitTime < MAX_WAIT_TIME) ) { 1088 observer.wait(WAIT_TIME_INCR); 1089 waitTime += WAIT_TIME_INCR; 1090 } 1091 if(!observer.isDone()) { 1092 throw new Exception("Timed out waiting for unmount media notification"); 1093 } 1094 return true; 1095 } 1096 } catch (Exception e) { 1097 Log.e(TAG, "Exception : " + e); 1098 return false; 1099 } finally { 1100 sm.unregisterListener(observer); 1101 } 1102 } 1103 1104 private boolean mountFromRawResource() { 1105 // Install pkg on sdcard 1106 InstallParams ip = sampleInstallFromRawResource(PackageManager.INSTALL_EXTERNAL, false); 1107 if (localLOGV) Log.i(TAG, "Installed pkg on sdcard"); 1108 boolean origState = checkMediaState(Environment.MEDIA_MOUNTED); 1109 boolean registeredReceiver = false; 1110 SdMountReceiver receiver = new SdMountReceiver(new String[]{ip.pkg.packageName}); 1111 try { 1112 if (localLOGV) Log.i(TAG, "Unmounting media"); 1113 // Unmount media 1114 assertTrue(unmountMedia()); 1115 if (localLOGV) Log.i(TAG, "Unmounted media"); 1116 // Register receiver here 1117 PackageManager pm = getPm(); 1118 mContext.registerReceiver(receiver, receiver.filter); 1119 registeredReceiver = true; 1120 1121 // Wait on receiver 1122 synchronized (receiver) { 1123 if (localLOGV) Log.i(TAG, "Mounting media"); 1124 // Mount media again 1125 assertTrue(mountMedia()); 1126 if (localLOGV) Log.i(TAG, "Mounted media"); 1127 if (localLOGV) Log.i(TAG, "Waiting for notification"); 1128 long waitTime = 0; 1129 // Verify we received the broadcast 1130 waitTime = 0; 1131 while((!receiver.isDone()) && (waitTime < MAX_WAIT_TIME) ) { 1132 receiver.wait(WAIT_TIME_INCR); 1133 waitTime += WAIT_TIME_INCR; 1134 } 1135 if(!receiver.isDone()) { 1136 failStr("Timed out waiting for EXTERNAL_APPLICATIONS notification"); 1137 } 1138 return receiver.received; 1139 } 1140 } catch (InterruptedException e) { 1141 failStr(e); 1142 return false; 1143 } finally { 1144 if (registeredReceiver) mContext.unregisterReceiver(receiver); 1145 // Restore original media state 1146 if (origState) { 1147 mountMedia(); 1148 } else { 1149 unmountMedia(); 1150 } 1151 if (localLOGV) Log.i(TAG, "Cleaning up install"); 1152 cleanUpInstall(ip); 1153 } 1154 } 1155 1156 /* 1157 * Install package on sdcard. Unmount and then mount the media. 1158 * (Use PackageManagerService private api for now) 1159 * Make sure the installed package is available. 1160 */ 1161 @LargeTest 1162 public void testMountSdNormalInternal() { 1163 // Do not run on devices with emulated external storage. 1164 if (Environment.isExternalStorageEmulated()) { 1165 return; 1166 } 1167 1168 assertTrue(mountFromRawResource()); 1169 } 1170 1171 void cleanUpInstall(InstallParams ip) { 1172 if (ip == null) { 1173 return; 1174 } 1175 Runtime.getRuntime().gc(); 1176 Log.i(TAG, "Deleting package : " + ip.pkg.packageName); 1177 getPm().deletePackage(ip.pkg.packageName, null, 0); 1178 File outFile = new File(ip.pkg.mScanPath); 1179 if (outFile != null && outFile.exists()) { 1180 outFile.delete(); 1181 } 1182 } 1183 void cleanUpInstall(String pkgName) { 1184 if (pkgName == null) { 1185 return; 1186 } 1187 Log.i(TAG, "Deleting package : " + pkgName); 1188 try { 1189 ApplicationInfo info = getPm().getApplicationInfo(pkgName, 1190 PackageManager.GET_UNINSTALLED_PACKAGES); 1191 if (info != null) { 1192 getPm().deletePackage(pkgName, null, 0); 1193 } 1194 } catch (NameNotFoundException e) {} 1195 } 1196 1197 @LargeTest 1198 public void testManifestInstallLocationInternal() { 1199 installFromRawResource("install.apk", R.raw.install_loc_internal, 1200 0, true, false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); 1201 } 1202 1203 @LargeTest 1204 public void testManifestInstallLocationSdcard() { 1205 // Do not run on devices with emulated external storage. 1206 if (Environment.isExternalStorageEmulated()) { 1207 return; 1208 } 1209 1210 installFromRawResource("install.apk", R.raw.install_loc_sdcard, 1211 0, true, false, -1, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); 1212 } 1213 1214 @LargeTest 1215 public void testManifestInstallLocationAuto() { 1216 installFromRawResource("install.apk", R.raw.install_loc_auto, 1217 0, true, false, -1, PackageInfo.INSTALL_LOCATION_AUTO); 1218 } 1219 1220 @LargeTest 1221 public void testManifestInstallLocationUnspecified() { 1222 installFromRawResource("install.apk", R.raw.install_loc_unspecified, 1223 0, true, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); 1224 } 1225 1226 @LargeTest 1227 public void testManifestInstallLocationFwdLockedFlagSdcard() { 1228 // Do not run on devices with emulated external storage. 1229 if (Environment.isExternalStorageEmulated()) { 1230 return; 1231 } 1232 1233 installFromRawResource("install.apk", R.raw.install_loc_unspecified, 1234 PackageManager.INSTALL_FORWARD_LOCK | 1235 PackageManager.INSTALL_EXTERNAL, true, false, -1, 1236 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); 1237 } 1238 1239 @LargeTest 1240 public void testManifestInstallLocationFwdLockedSdcard() { 1241 // Do not run on devices with emulated external storage. 1242 if (Environment.isExternalStorageEmulated()) { 1243 return; 1244 } 1245 1246 installFromRawResource("install.apk", R.raw.install_loc_sdcard, 1247 PackageManager.INSTALL_FORWARD_LOCK, true, false, 1248 -1, 1249 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); 1250 } 1251 1252 /* 1253 * Install a package on internal flash via PackageManager install flag. Replace 1254 * the package via flag to install on sdcard. Make sure the new flag overrides 1255 * the old install location. 1256 */ 1257 @LargeTest 1258 public void testReplaceFlagInternalSdcard() { 1259 // Do not run on devices with emulated external storage. 1260 if (Environment.isExternalStorageEmulated()) { 1261 return; 1262 } 1263 1264 int iFlags = 0; 1265 int rFlags = PackageManager.INSTALL_EXTERNAL; 1266 InstallParams ip = sampleInstallFromRawResource(iFlags, false); 1267 GenericReceiver receiver = new ReplaceReceiver(ip.pkg.packageName); 1268 int replaceFlags = rFlags | PackageManager.INSTALL_REPLACE_EXISTING; 1269 try { 1270 invokeInstallPackage(ip.packageURI, replaceFlags, receiver, true); 1271 assertInstall(ip.pkg, rFlags, ip.pkg.installLocation); 1272 } catch (Exception e) { 1273 failStr("Failed with exception : " + e); 1274 } finally { 1275 cleanUpInstall(ip); 1276 } 1277 } 1278 1279 /* 1280 * Install a package on sdcard via PackageManager install flag. Replace 1281 * the package with no flags or manifest option and make sure the old 1282 * install location is retained. 1283 */ 1284 @LargeTest 1285 public void testReplaceFlagSdcardInternal() { 1286 // Do not run on devices with emulated external storage. 1287 if (Environment.isExternalStorageEmulated()) { 1288 return; 1289 } 1290 1291 int iFlags = PackageManager.INSTALL_EXTERNAL; 1292 int rFlags = 0; 1293 InstallParams ip = sampleInstallFromRawResource(iFlags, false); 1294 GenericReceiver receiver = new ReplaceReceiver(ip.pkg.packageName); 1295 int replaceFlags = rFlags | PackageManager.INSTALL_REPLACE_EXISTING; 1296 try { 1297 invokeInstallPackage(ip.packageURI, replaceFlags, receiver, true); 1298 assertInstall(ip.pkg, iFlags, ip.pkg.installLocation); 1299 } catch (Exception e) { 1300 failStr("Failed with exception : " + e); 1301 } finally { 1302 cleanUpInstall(ip); 1303 } 1304 } 1305 1306 @LargeTest 1307 public void testManifestInstallLocationReplaceInternalSdcard() { 1308 // Do not run on devices with emulated external storage. 1309 if (Environment.isExternalStorageEmulated()) { 1310 return; 1311 } 1312 1313 int iFlags = 0; 1314 int iApk = R.raw.install_loc_internal; 1315 int rFlags = 0; 1316 int rApk = R.raw.install_loc_sdcard; 1317 InstallParams ip = installFromRawResource("install.apk", iApk, 1318 iFlags, false, 1319 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); 1320 GenericReceiver receiver = new ReplaceReceiver(ip.pkg.packageName); 1321 int replaceFlags = rFlags | PackageManager.INSTALL_REPLACE_EXISTING; 1322 try { 1323 InstallParams rp = installFromRawResource("install.apk", rApk, 1324 replaceFlags, false, 1325 false, -1, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); 1326 assertInstall(rp.pkg, replaceFlags, rp.pkg.installLocation); 1327 } catch (Exception e) { 1328 failStr("Failed with exception : " + e); 1329 } finally { 1330 cleanUpInstall(ip); 1331 } 1332 } 1333 1334 @LargeTest 1335 public void testManifestInstallLocationReplaceSdcardInternal() { 1336 // Do not run on devices with emulated external storage. 1337 if (Environment.isExternalStorageEmulated()) { 1338 return; 1339 } 1340 1341 int iFlags = 0; 1342 int iApk = R.raw.install_loc_sdcard; 1343 int rFlags = 0; 1344 int rApk = R.raw.install_loc_unspecified; 1345 InstallParams ip = installFromRawResource("install.apk", iApk, 1346 iFlags, false, 1347 false, -1, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); 1348 int replaceFlags = rFlags | PackageManager.INSTALL_REPLACE_EXISTING; 1349 try { 1350 InstallParams rp = installFromRawResource("install.apk", rApk, 1351 replaceFlags, false, 1352 false, -1, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); 1353 assertInstall(rp.pkg, replaceFlags, ip.pkg.installLocation); 1354 } catch (Exception e) { 1355 failStr("Failed with exception : " + e); 1356 } finally { 1357 cleanUpInstall(ip); 1358 } 1359 } 1360 1361 class MoveReceiver extends GenericReceiver { 1362 String pkgName; 1363 final static int INVALID = -1; 1364 final static int REMOVED = 1; 1365 final static int ADDED = 2; 1366 int removed = INVALID; 1367 1368 MoveReceiver(String pkgName) { 1369 this.pkgName = pkgName; 1370 filter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); 1371 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); 1372 super.setFilter(filter); 1373 } 1374 1375 public boolean notifyNow(Intent intent) { 1376 String action = intent.getAction(); 1377 Log.i(TAG, "MoveReceiver::" + action); 1378 if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) { 1379 String[] list = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); 1380 if (list != null) { 1381 for (String pkg : list) { 1382 if (pkg.equals(pkgName)) { 1383 removed = REMOVED; 1384 break; 1385 } 1386 } 1387 } 1388 removed = REMOVED; 1389 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) { 1390 if (removed != REMOVED) { 1391 return false; 1392 } 1393 String[] list = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); 1394 if (list != null) { 1395 for (String pkg : list) { 1396 if (pkg.equals(pkgName)) { 1397 removed = ADDED; 1398 return true; 1399 } 1400 } 1401 } 1402 } 1403 return false; 1404 } 1405 } 1406 1407 private class PackageMoveObserver extends IPackageMoveObserver.Stub { 1408 public int returnCode; 1409 private boolean doneFlag = false; 1410 public String packageName; 1411 public PackageMoveObserver(String pkgName) { 1412 packageName = pkgName; 1413 } 1414 public void packageMoved(String packageName, int returnCode) { 1415 Log.i("DEBUG_MOVE::", "pkg = " + packageName + ", " + "ret = " + returnCode); 1416 if (!packageName.equals(this.packageName)) { 1417 return; 1418 } 1419 synchronized(this) { 1420 this.returnCode = returnCode; 1421 doneFlag = true; 1422 notifyAll(); 1423 } 1424 } 1425 1426 public boolean isDone() { 1427 return doneFlag; 1428 } 1429 } 1430 1431 public boolean invokeMovePackage(String pkgName, int flags, 1432 GenericReceiver receiver) throws Exception { 1433 PackageMoveObserver observer = new PackageMoveObserver(pkgName); 1434 final boolean received = false; 1435 mContext.registerReceiver(receiver, receiver.filter); 1436 try { 1437 // Wait on observer 1438 synchronized(observer) { 1439 synchronized (receiver) { 1440 getPm().movePackage(pkgName, observer, flags); 1441 long waitTime = 0; 1442 while((!observer.isDone()) && (waitTime < MAX_WAIT_TIME) ) { 1443 observer.wait(WAIT_TIME_INCR); 1444 waitTime += WAIT_TIME_INCR; 1445 } 1446 if(!observer.isDone()) { 1447 throw new Exception("Timed out waiting for pkgmove callback"); 1448 } 1449 if (observer.returnCode != PackageManager.MOVE_SUCCEEDED) { 1450 return false; 1451 } 1452 // Verify we received the broadcast 1453 waitTime = 0; 1454 while((!receiver.isDone()) && (waitTime < MAX_WAIT_TIME) ) { 1455 receiver.wait(WAIT_TIME_INCR); 1456 waitTime += WAIT_TIME_INCR; 1457 } 1458 if(!receiver.isDone()) { 1459 throw new Exception("Timed out waiting for MOVE notifications"); 1460 } 1461 return receiver.received; 1462 } 1463 } 1464 } finally { 1465 mContext.unregisterReceiver(receiver); 1466 } 1467 } 1468 private boolean invokeMovePackageFail(String pkgName, int flags, int errCode) throws Exception { 1469 PackageMoveObserver observer = new PackageMoveObserver(pkgName); 1470 try { 1471 // Wait on observer 1472 synchronized(observer) { 1473 getPm().movePackage(pkgName, observer, flags); 1474 long waitTime = 0; 1475 while((!observer.isDone()) && (waitTime < MAX_WAIT_TIME) ) { 1476 observer.wait(WAIT_TIME_INCR); 1477 waitTime += WAIT_TIME_INCR; 1478 } 1479 if(!observer.isDone()) { 1480 throw new Exception("Timed out waiting for pkgmove callback"); 1481 } 1482 assertEquals(errCode, observer.returnCode); 1483 } 1484 } finally { 1485 } 1486 return true; 1487 } 1488 1489 private int getDefaultInstallLoc() { 1490 int origDefaultLoc = PackageInfo.INSTALL_LOCATION_AUTO; 1491 try { 1492 origDefaultLoc = Settings.System.getInt(mContext.getContentResolver(), Settings.Secure.DEFAULT_INSTALL_LOCATION); 1493 } catch (SettingNotFoundException e1) { 1494 } 1495 return origDefaultLoc; 1496 } 1497 1498 private void setInstallLoc(int loc) { 1499 Settings.System.putInt(mContext.getContentResolver(), 1500 Settings.Secure.DEFAULT_INSTALL_LOCATION, loc); 1501 } 1502 /* 1503 * Tests for moving apps between internal and external storage 1504 */ 1505 /* 1506 * Utility function that reads a apk bundled as a raw resource 1507 * copies it into own data directory and invokes 1508 * PackageManager api to install first and then replace it 1509 * again. 1510 */ 1511 1512 private void moveFromRawResource(String outFileName, 1513 int rawResId, int installFlags, int moveFlags, boolean cleanUp, 1514 boolean fail, int result) { 1515 int origDefaultLoc = getDefaultInstallLoc(); 1516 InstallParams ip = null; 1517 try { 1518 setInstallLoc(PackageHelper.APP_INSTALL_AUTO); 1519 // Install first 1520 ip = installFromRawResource("install.apk", rawResId, installFlags, false, 1521 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); 1522 ApplicationInfo oldAppInfo = getPm().getApplicationInfo(ip.pkg.packageName, 0); 1523 if (fail) { 1524 assertTrue(invokeMovePackageFail(ip.pkg.packageName, moveFlags, result)); 1525 ApplicationInfo info = getPm().getApplicationInfo(ip.pkg.packageName, 0); 1526 assertNotNull(info); 1527 assertEquals(oldAppInfo.flags, info.flags); 1528 } else { 1529 // Create receiver based on expRetCode 1530 MoveReceiver receiver = new MoveReceiver(ip.pkg.packageName); 1531 boolean retCode = invokeMovePackage(ip.pkg.packageName, moveFlags, 1532 receiver); 1533 assertTrue(retCode); 1534 ApplicationInfo info = getPm().getApplicationInfo(ip.pkg.packageName, 0); 1535 assertNotNull("ApplicationInfo for recently installed application should exist", 1536 info); 1537 if ((moveFlags & PackageManager.MOVE_INTERNAL) != 0) { 1538 assertTrue("ApplicationInfo.FLAG_EXTERNAL_STORAGE flag should NOT be set", 1539 (info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == 0); 1540 assertTrue("ApplicationInfo.nativeLibraryDir should start with " + info.dataDir, 1541 info.nativeLibraryDir.startsWith(info.dataDir)); 1542 } else if ((moveFlags & PackageManager.MOVE_EXTERNAL_MEDIA) != 0){ 1543 assertTrue("ApplicationInfo.FLAG_EXTERNAL_STORAGE flag should be set", 1544 (info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0); 1545 assertTrue("ApplicationInfo.nativeLibraryDir should start with " + SECURE_CONTAINERS_PREFIX, 1546 info.nativeLibraryDir.startsWith(SECURE_CONTAINERS_PREFIX)); 1547 final File nativeLibSymLink = new File(info.dataDir, "lib"); 1548 assertTrue("The data directory should have a 'lib' symlink that points to the ASEC container", 1549 nativeLibSymLink.getCanonicalPath().startsWith(SECURE_CONTAINERS_PREFIX)); 1550 } 1551 } 1552 } catch (NameNotFoundException e) { 1553 failStr("Pkg hasnt been installed correctly"); 1554 } catch (Exception e) { 1555 failStr("Failed with exception : " + e); 1556 } finally { 1557 if (ip != null) { 1558 cleanUpInstall(ip); 1559 } 1560 // Restore default install location 1561 setInstallLoc(origDefaultLoc); 1562 } 1563 } 1564 private void sampleMoveFromRawResource(int installFlags, int moveFlags, boolean fail, 1565 int result) { 1566 moveFromRawResource("install.apk", 1567 R.raw.install, installFlags, moveFlags, true, 1568 fail, result); 1569 } 1570 1571 @LargeTest 1572 public void testMoveAppInternalToExternal() { 1573 // Do not run on devices with emulated external storage. 1574 if (Environment.isExternalStorageEmulated()) { 1575 return; 1576 } 1577 1578 int installFlags = PackageManager.INSTALL_INTERNAL; 1579 int moveFlags = PackageManager.MOVE_EXTERNAL_MEDIA; 1580 boolean fail = false; 1581 int result = PackageManager.MOVE_SUCCEEDED; 1582 sampleMoveFromRawResource(installFlags, moveFlags, fail, result); 1583 } 1584 1585 @LargeTest 1586 public void testMoveAppInternalToInternal() { 1587 int installFlags = PackageManager.INSTALL_INTERNAL; 1588 int moveFlags = PackageManager.MOVE_INTERNAL; 1589 boolean fail = true; 1590 int result = PackageManager.MOVE_FAILED_INVALID_LOCATION; 1591 sampleMoveFromRawResource(installFlags, moveFlags, fail, result); 1592 } 1593 1594 @LargeTest 1595 public void testMoveAppExternalToExternal() { 1596 // Do not run on devices with emulated external storage. 1597 if (Environment.isExternalStorageEmulated()) { 1598 return; 1599 } 1600 1601 int installFlags = PackageManager.INSTALL_EXTERNAL; 1602 int moveFlags = PackageManager.MOVE_EXTERNAL_MEDIA; 1603 boolean fail = true; 1604 int result = PackageManager.MOVE_FAILED_INVALID_LOCATION; 1605 sampleMoveFromRawResource(installFlags, moveFlags, fail, result); 1606 } 1607 1608 @LargeTest 1609 public void testMoveAppExternalToInternal() { 1610 // Do not run on devices with emulated external storage. 1611 if (Environment.isExternalStorageEmulated()) { 1612 return; 1613 } 1614 1615 int installFlags = PackageManager.INSTALL_EXTERNAL; 1616 int moveFlags = PackageManager.MOVE_INTERNAL; 1617 boolean fail = false; 1618 int result = PackageManager.MOVE_SUCCEEDED; 1619 sampleMoveFromRawResource(installFlags, moveFlags, fail, result); 1620 } 1621 1622 @LargeTest 1623 public void testMoveAppForwardLocked() { 1624 // Do not run on devices with emulated external storage. 1625 if (Environment.isExternalStorageEmulated()) { 1626 return; 1627 } 1628 1629 int installFlags = PackageManager.INSTALL_FORWARD_LOCK; 1630 int moveFlags = PackageManager.MOVE_EXTERNAL_MEDIA; 1631 boolean fail = false; 1632 int result = PackageManager.MOVE_SUCCEEDED; 1633 sampleMoveFromRawResource(installFlags, moveFlags, fail, result); 1634 } 1635 1636 @LargeTest 1637 public void testMoveAppFailInternalToExternalDelete() { 1638 // Do not run on devices with emulated external storage. 1639 if (Environment.isExternalStorageEmulated()) { 1640 return; 1641 } 1642 1643 int installFlags = 0; 1644 int moveFlags = PackageManager.MOVE_EXTERNAL_MEDIA; 1645 boolean fail = true; 1646 final int result = PackageManager.MOVE_FAILED_DOESNT_EXIST; 1647 1648 int rawResId = R.raw.install; 1649 int origDefaultLoc = getDefaultInstallLoc(); 1650 InstallParams ip = null; 1651 try { 1652 PackageManager pm = getPm(); 1653 setInstallLoc(PackageHelper.APP_INSTALL_AUTO); 1654 // Install first 1655 ip = installFromRawResource("install.apk", R.raw.install, installFlags, false, 1656 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); 1657 // Delete the package now retaining data. 1658 GenericReceiver receiver = new DeleteReceiver(ip.pkg.packageName); 1659 invokeDeletePackage(ip.pkg.packageName, PackageManager.DONT_DELETE_DATA, receiver); 1660 assertTrue(invokeMovePackageFail(ip.pkg.packageName, moveFlags, result)); 1661 } catch (Exception e) { 1662 failStr(e); 1663 } finally { 1664 if (ip != null) { 1665 cleanUpInstall(ip); 1666 } 1667 // Restore default install location 1668 setInstallLoc(origDefaultLoc); 1669 } 1670 } 1671 1672 /* 1673 * Test that an install error code is returned when media is unmounted 1674 * and package installed on sdcard via package manager flag. 1675 */ 1676 @LargeTest 1677 public void testInstallSdcardUnmount() { 1678 // Do not run on devices with emulated external storage. 1679 if (Environment.isExternalStorageEmulated()) { 1680 return; 1681 } 1682 1683 boolean origState = checkMediaState(Environment.MEDIA_MOUNTED); 1684 try { 1685 // Unmount sdcard 1686 assertTrue(unmountMedia()); 1687 // Try to install and make sure an error code is returned. 1688 installFromRawResource("install.apk", R.raw.install, 1689 PackageManager.INSTALL_EXTERNAL, false, 1690 true, PackageManager.INSTALL_FAILED_MEDIA_UNAVAILABLE, 1691 PackageInfo.INSTALL_LOCATION_AUTO); 1692 } finally { 1693 // Restore original media state 1694 if (origState) { 1695 mountMedia(); 1696 } else { 1697 unmountMedia(); 1698 } 1699 } 1700 } 1701 1702 /* 1703 * Unmount sdcard. Try installing an app with manifest option to install 1704 * on sdcard. Make sure it gets installed on internal flash. 1705 */ 1706 @LargeTest 1707 public void testInstallManifestSdcardUnmount() { 1708 // Do not run on devices with emulated external storage. 1709 if (Environment.isExternalStorageEmulated()) { 1710 return; 1711 } 1712 1713 boolean origState = checkMediaState(Environment.MEDIA_MOUNTED); 1714 try { 1715 // Unmount sdcard 1716 assertTrue(unmountMedia()); 1717 InstallParams ip = new InstallParams("install.apk", R.raw.install_loc_sdcard); 1718 installFromRawResource(ip, 0, true, false, -1, 1719 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); 1720 } finally { 1721 // Restore original media state 1722 if (origState) { 1723 mountMedia(); 1724 } else { 1725 unmountMedia(); 1726 } 1727 } 1728 } 1729 1730 /*---------- Recommended install location tests ----*/ 1731 /* Precedence: FlagManifestExistingUser 1732 * PrecedenceSuffixes: 1733 * Flag : FlagI, FlagE, FlagF 1734 * I - internal, E - external, F - forward locked, Flag suffix absent if not using any option. 1735 * Manifest: ManifestI, ManifestE, ManifestA, Manifest suffix absent if not using any option. 1736 * Existing: Existing suffix absent if not existing. 1737 * User: UserI, UserE, UserA, User suffix absent if not existing. 1738 * 1739 */ 1740 1741 /* 1742 * Install an app on internal flash 1743 */ 1744 @LargeTest 1745 public void testFlagI() { 1746 sampleInstallFromRawResource(PackageManager.INSTALL_INTERNAL, true); 1747 } 1748 1749 /* 1750 * Install an app on sdcard. 1751 */ 1752 @LargeTest 1753 public void testFlagE() { 1754 // Do not run on devices with emulated external storage. 1755 if (Environment.isExternalStorageEmulated()) { 1756 return; 1757 } 1758 1759 sampleInstallFromRawResource(PackageManager.INSTALL_EXTERNAL, true); 1760 } 1761 1762 /* 1763 * Install an app forward-locked. 1764 */ 1765 @LargeTest 1766 public void testFlagF() { 1767 sampleInstallFromRawResource(PackageManager.INSTALL_FORWARD_LOCK, true); 1768 } 1769 1770 /* 1771 * Install an app with both internal and external flags set. should fail 1772 */ 1773 @LargeTest 1774 public void testFlagIE() { 1775 installFromRawResource("install.apk", R.raw.install, 1776 PackageManager.INSTALL_EXTERNAL | PackageManager.INSTALL_INTERNAL, 1777 false, 1778 true, PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION, 1779 PackageInfo.INSTALL_LOCATION_AUTO); 1780 } 1781 1782 /* 1783 * Install an app with both internal and forward-lock flags set. 1784 */ 1785 @LargeTest 1786 public void testFlagIF() { 1787 sampleInstallFromRawResource(PackageManager.INSTALL_FORWARD_LOCK 1788 | PackageManager.INSTALL_INTERNAL, true); 1789 } 1790 1791 /* 1792 * Install an app with both external and forward-lock flags set. 1793 */ 1794 @LargeTest 1795 public void testFlagEF() { 1796 // Do not run on devices with emulated external storage. 1797 if (Environment.isExternalStorageEmulated()) { 1798 return; 1799 } 1800 1801 sampleInstallFromRawResource(PackageManager.INSTALL_FORWARD_LOCK 1802 | PackageManager.INSTALL_EXTERNAL, true); 1803 } 1804 1805 /* 1806 * Install an app with both internal and external flags set with forward 1807 * lock. Should fail. 1808 */ 1809 @LargeTest 1810 public void testFlagIEF() { 1811 installFromRawResource("install.apk", R.raw.install, 1812 PackageManager.INSTALL_FORWARD_LOCK | PackageManager.INSTALL_INTERNAL | 1813 PackageManager.INSTALL_EXTERNAL, 1814 false, 1815 true, PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION, 1816 PackageInfo.INSTALL_LOCATION_AUTO); 1817 } 1818 1819 /* 1820 * Install an app with both internal and manifest option set. 1821 * should install on internal. 1822 */ 1823 @LargeTest 1824 public void testFlagIManifestI() { 1825 installFromRawResource("install.apk", R.raw.install_loc_internal, 1826 PackageManager.INSTALL_INTERNAL, 1827 true, 1828 false, -1, 1829 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); 1830 } 1831 /* 1832 * Install an app with both internal and manifest preference for 1833 * preferExternal. Should install on internal. 1834 */ 1835 @LargeTest 1836 public void testFlagIManifestE() { 1837 installFromRawResource("install.apk", R.raw.install_loc_sdcard, 1838 PackageManager.INSTALL_INTERNAL, 1839 true, 1840 false, -1, 1841 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); 1842 } 1843 /* 1844 * Install an app with both internal and manifest preference for 1845 * auto. should install internal. 1846 */ 1847 @LargeTest 1848 public void testFlagIManifestA() { 1849 installFromRawResource("install.apk", R.raw.install_loc_auto, 1850 PackageManager.INSTALL_INTERNAL, 1851 true, 1852 false, -1, 1853 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); 1854 } 1855 /* 1856 * Install an app with both external and manifest option set. 1857 * should install externally. 1858 */ 1859 @LargeTest 1860 public void testFlagEManifestI() { 1861 // Do not run on devices with emulated external storage. 1862 if (Environment.isExternalStorageEmulated()) { 1863 return; 1864 } 1865 1866 installFromRawResource("install.apk", R.raw.install_loc_internal, 1867 PackageManager.INSTALL_EXTERNAL, 1868 true, 1869 false, -1, 1870 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); 1871 } 1872 1873 /* 1874 * Install an app with both external and manifest preference for 1875 * preferExternal. Should install externally. 1876 */ 1877 @LargeTest 1878 public void testFlagEManifestE() { 1879 // Do not run on devices with emulated external storage. 1880 if (Environment.isExternalStorageEmulated()) { 1881 return; 1882 } 1883 1884 installFromRawResource("install.apk", R.raw.install_loc_sdcard, 1885 PackageManager.INSTALL_EXTERNAL, 1886 true, 1887 false, -1, 1888 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); 1889 } 1890 1891 /* 1892 * Install an app with both external and manifest preference for 1893 * auto. should install on external media. 1894 */ 1895 @LargeTest 1896 public void testFlagEManifestA() { 1897 // Do not run on devices with emulated external storage. 1898 if (Environment.isExternalStorageEmulated()) { 1899 return; 1900 } 1901 1902 installFromRawResource("install.apk", R.raw.install_loc_auto, 1903 PackageManager.INSTALL_EXTERNAL, 1904 true, 1905 false, -1, 1906 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); 1907 } 1908 1909 /* 1910 * Install an app with fwd locked flag set and install location set to 1911 * internal. should install internally. 1912 */ 1913 @LargeTest 1914 public void testFlagFManifestI() { 1915 installFromRawResource("install.apk", R.raw.install_loc_internal, 1916 PackageManager.INSTALL_FORWARD_LOCK, 1917 true, 1918 false, -1, 1919 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); 1920 } 1921 1922 /* 1923 * Install an app with fwd locked flag set and install location set to 1924 * preferExternal. Should install externally. 1925 */ 1926 @LargeTest 1927 public void testFlagFManifestE() { 1928 // Do not run on devices with emulated external storage. 1929 if (Environment.isExternalStorageEmulated()) { 1930 return; 1931 } 1932 1933 installFromRawResource("install.apk", R.raw.install_loc_sdcard, 1934 PackageManager.INSTALL_FORWARD_LOCK, 1935 true, 1936 false, -1, 1937 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); 1938 } 1939 1940 /* 1941 * Install an app with fwd locked flag set and install location set to auto. 1942 * should install externally. 1943 */ 1944 @LargeTest 1945 public void testFlagFManifestA() { 1946 // Do not run on devices with emulated external storage. 1947 if (Environment.isExternalStorageEmulated()) { 1948 return; 1949 } 1950 1951 installFromRawResource("install.apk", R.raw.install_loc_auto, 1952 PackageManager.INSTALL_FORWARD_LOCK, 1953 true, 1954 false, -1, 1955 PackageInfo.INSTALL_LOCATION_AUTO); 1956 } 1957 1958 /* The following test functions verify install location for existing apps. 1959 * ie existing app can be installed internally or externally. If install 1960 * flag is explicitly set it should override current location. If manifest location 1961 * is set, that should over ride current location too. if not the existing install 1962 * location should be honoured. 1963 * testFlagI/E/F/ExistingI/E - 1964 */ 1965 @LargeTest 1966 public void testFlagIExistingI() { 1967 int iFlags = PackageManager.INSTALL_INTERNAL; 1968 int rFlags = PackageManager.INSTALL_INTERNAL | PackageManager.INSTALL_REPLACE_EXISTING; 1969 // First install. 1970 installFromRawResource("install.apk", R.raw.install, 1971 iFlags, 1972 false, 1973 false, -1, 1974 -1); 1975 // Replace now 1976 installFromRawResource("install.apk", R.raw.install, 1977 rFlags, 1978 true, 1979 false, -1, 1980 -1); 1981 } 1982 1983 @LargeTest 1984 public void testFlagIExistingE() { 1985 // Do not run on devices with emulated external storage. 1986 if (Environment.isExternalStorageEmulated()) { 1987 return; 1988 } 1989 1990 int iFlags = PackageManager.INSTALL_EXTERNAL; 1991 int rFlags = PackageManager.INSTALL_INTERNAL | PackageManager.INSTALL_REPLACE_EXISTING; 1992 // First install. 1993 installFromRawResource("install.apk", R.raw.install, 1994 iFlags, 1995 false, 1996 false, -1, 1997 -1); 1998 // Replace now 1999 installFromRawResource("install.apk", R.raw.install, 2000 rFlags, 2001 true, 2002 false, -1, 2003 -1); 2004 } 2005 2006 @LargeTest 2007 public void testFlagEExistingI() { 2008 // Do not run on devices with emulated external storage. 2009 if (Environment.isExternalStorageEmulated()) { 2010 return; 2011 } 2012 2013 int iFlags = PackageManager.INSTALL_INTERNAL; 2014 int rFlags = PackageManager.INSTALL_EXTERNAL | PackageManager.INSTALL_REPLACE_EXISTING; 2015 // First install. 2016 installFromRawResource("install.apk", R.raw.install, 2017 iFlags, 2018 false, 2019 false, -1, 2020 -1); 2021 // Replace now 2022 installFromRawResource("install.apk", R.raw.install, 2023 rFlags, 2024 true, 2025 false, -1, 2026 -1); 2027 } 2028 2029 @LargeTest 2030 public void testFlagEExistingE() { 2031 // Do not run on devices with emulated external storage. 2032 if (Environment.isExternalStorageEmulated()) { 2033 return; 2034 } 2035 2036 int iFlags = PackageManager.INSTALL_EXTERNAL; 2037 int rFlags = PackageManager.INSTALL_EXTERNAL | PackageManager.INSTALL_REPLACE_EXISTING; 2038 // First install. 2039 installFromRawResource("install.apk", R.raw.install, 2040 iFlags, 2041 false, 2042 false, -1, 2043 -1); 2044 // Replace now 2045 installFromRawResource("install.apk", R.raw.install, 2046 rFlags, 2047 true, 2048 false, -1, 2049 -1); 2050 } 2051 2052 @LargeTest 2053 public void testFlagFExistingI() { 2054 int iFlags = PackageManager.INSTALL_INTERNAL; 2055 int rFlags = PackageManager.INSTALL_FORWARD_LOCK | PackageManager.INSTALL_REPLACE_EXISTING; 2056 // First install. 2057 installFromRawResource("install.apk", R.raw.install, 2058 iFlags, 2059 false, 2060 false, -1, 2061 -1); 2062 // Replace now 2063 installFromRawResource("install.apk", R.raw.install, 2064 rFlags, 2065 true, 2066 false, -1, 2067 -1); 2068 } 2069 2070 @LargeTest 2071 public void testFlagFExistingE() { 2072 // Do not run on devices with emulated external storage. 2073 if (Environment.isExternalStorageEmulated()) { 2074 return; 2075 } 2076 2077 int iFlags = PackageManager.INSTALL_EXTERNAL; 2078 int rFlags = PackageManager.INSTALL_FORWARD_LOCK | PackageManager.INSTALL_REPLACE_EXISTING; 2079 // First install. 2080 installFromRawResource("install.apk", R.raw.install, 2081 iFlags, 2082 false, 2083 false, -1, 2084 -1); 2085 // Replace now 2086 installFromRawResource("install.apk", R.raw.install, 2087 rFlags, 2088 true, 2089 false, -1, 2090 -1); 2091 } 2092 2093 /* 2094 * The following set of tests verify the installation of apps with 2095 * install location attribute set to internalOnly, preferExternal and auto. 2096 * The manifest option should dictate the install location. 2097 * public void testManifestI/E/A 2098 * TODO out of memory fall back behaviour. 2099 */ 2100 @LargeTest 2101 public void testManifestI() { 2102 installFromRawResource("install.apk", R.raw.install_loc_internal, 2103 0, 2104 true, 2105 false, -1, 2106 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); 2107 } 2108 2109 @LargeTest 2110 public void testManifestE() { 2111 // Do not run on devices with emulated external storage. 2112 if (Environment.isExternalStorageEmulated()) { 2113 return; 2114 } 2115 2116 installFromRawResource("install.apk", R.raw.install_loc_sdcard, 2117 0, 2118 true, 2119 false, -1, 2120 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); 2121 } 2122 2123 @LargeTest 2124 public void testManifestA() { 2125 installFromRawResource("install.apk", R.raw.install_loc_auto, 2126 0, 2127 true, 2128 false, -1, 2129 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); 2130 } 2131 2132 /* 2133 * The following set of tests verify the installation of apps 2134 * with install location attribute set to internalOnly, preferExternal and auto 2135 * for already existing apps. The manifest option should take precedence. 2136 * TODO add out of memory fall back behaviour. 2137 * testManifestI/E/AExistingI/E 2138 */ 2139 @LargeTest 2140 public void testManifestIExistingI() { 2141 int iFlags = PackageManager.INSTALL_INTERNAL; 2142 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING; 2143 // First install. 2144 installFromRawResource("install.apk", R.raw.install, 2145 iFlags, 2146 false, 2147 false, -1, 2148 -1); 2149 // Replace now 2150 installFromRawResource("install.apk", R.raw.install_loc_internal, 2151 rFlags, 2152 true, 2153 false, -1, 2154 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); 2155 } 2156 2157 @LargeTest 2158 public void testManifestIExistingE() { 2159 // Do not run on devices with emulated external storage. 2160 if (Environment.isExternalStorageEmulated()) { 2161 return; 2162 } 2163 2164 int iFlags = PackageManager.INSTALL_EXTERNAL; 2165 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING; 2166 // First install. 2167 installFromRawResource("install.apk", R.raw.install, 2168 iFlags, 2169 false, 2170 false, -1, 2171 -1); 2172 // Replace now 2173 installFromRawResource("install.apk", R.raw.install_loc_internal, 2174 rFlags, 2175 true, 2176 false, -1, 2177 PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); 2178 } 2179 2180 @LargeTest 2181 public void testManifestEExistingI() { 2182 // Do not run on devices with emulated external storage. 2183 if (Environment.isExternalStorageEmulated()) { 2184 return; 2185 } 2186 2187 int iFlags = PackageManager.INSTALL_INTERNAL; 2188 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING; 2189 // First install. 2190 installFromRawResource("install.apk", R.raw.install, 2191 iFlags, 2192 false, 2193 false, -1, 2194 -1); 2195 // Replace now 2196 installFromRawResource("install.apk", R.raw.install_loc_sdcard, 2197 rFlags, 2198 true, 2199 false, -1, 2200 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); 2201 } 2202 2203 @LargeTest 2204 public void testManifestEExistingE() { 2205 // Do not run on devices with emulated external storage. 2206 if (Environment.isExternalStorageEmulated()) { 2207 return; 2208 } 2209 2210 int iFlags = PackageManager.INSTALL_EXTERNAL; 2211 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING; 2212 // First install. 2213 installFromRawResource("install.apk", R.raw.install, 2214 iFlags, 2215 false, 2216 false, -1, 2217 -1); 2218 // Replace now 2219 installFromRawResource("install.apk", R.raw.install_loc_sdcard, 2220 rFlags, 2221 true, 2222 false, -1, 2223 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); 2224 } 2225 2226 @LargeTest 2227 public void testManifestAExistingI() { 2228 int iFlags = PackageManager.INSTALL_INTERNAL; 2229 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING; 2230 // First install. 2231 installFromRawResource("install.apk", R.raw.install, 2232 iFlags, 2233 false, 2234 false, -1, 2235 -1); 2236 // Replace now 2237 installFromRawResource("install.apk", R.raw.install_loc_auto, 2238 rFlags, 2239 true, 2240 false, -1, 2241 PackageInfo.INSTALL_LOCATION_AUTO); 2242 } 2243 2244 @LargeTest 2245 public void testManifestAExistingE() { 2246 // Do not run on devices with emulated external storage. 2247 if (Environment.isExternalStorageEmulated()) { 2248 return; 2249 } 2250 2251 int iFlags = PackageManager.INSTALL_EXTERNAL; 2252 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING; 2253 // First install. 2254 installFromRawResource("install.apk", R.raw.install, 2255 iFlags, 2256 false, 2257 false, -1, 2258 -1); 2259 // Replace now 2260 installFromRawResource("install.apk", R.raw.install_loc_auto, 2261 rFlags, 2262 true, 2263 false, -1, 2264 PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); 2265 } 2266 2267 /* 2268 * The following set of tests check install location for existing 2269 * application based on user setting. 2270 */ 2271 private int getExpectedInstallLocation(int userSetting) { 2272 int iloc = PackageInfo.INSTALL_LOCATION_UNSPECIFIED; 2273 boolean enable = getUserSettingSetInstallLocation(); 2274 if (enable) { 2275 if (userSetting == PackageHelper.APP_INSTALL_AUTO) { 2276 iloc = PackageInfo.INSTALL_LOCATION_AUTO; 2277 } else if (userSetting == PackageHelper.APP_INSTALL_EXTERNAL) { 2278 iloc = PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL; 2279 } else if (userSetting == PackageHelper.APP_INSTALL_INTERNAL) { 2280 iloc = PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY; 2281 } 2282 } 2283 return iloc; 2284 } 2285 private void setExistingXUserX(int userSetting, int iFlags, int iloc) { 2286 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING; 2287 // First install. 2288 installFromRawResource("install.apk", R.raw.install, 2289 iFlags, 2290 false, 2291 false, -1, 2292 PackageInfo.INSTALL_LOCATION_UNSPECIFIED); 2293 int origSetting = getDefaultInstallLoc(); 2294 try { 2295 // Set user setting 2296 setInstallLoc(userSetting); 2297 // Replace now 2298 installFromRawResource("install.apk", R.raw.install, 2299 rFlags, 2300 true, 2301 false, -1, 2302 iloc); 2303 } finally { 2304 setInstallLoc(origSetting); 2305 } 2306 } 2307 @LargeTest 2308 public void testExistingIUserI() { 2309 int userSetting = PackageHelper.APP_INSTALL_INTERNAL; 2310 int iFlags = PackageManager.INSTALL_INTERNAL; 2311 setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); 2312 } 2313 2314 @LargeTest 2315 public void testExistingIUserE() { 2316 // Do not run on devices with emulated external storage. 2317 if (Environment.isExternalStorageEmulated()) { 2318 return; 2319 } 2320 2321 int userSetting = PackageHelper.APP_INSTALL_EXTERNAL; 2322 int iFlags = PackageManager.INSTALL_INTERNAL; 2323 setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); 2324 } 2325 2326 @LargeTest 2327 public void testExistingIUserA() { 2328 int userSetting = PackageHelper.APP_INSTALL_AUTO; 2329 int iFlags = PackageManager.INSTALL_INTERNAL; 2330 setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); 2331 } 2332 2333 @LargeTest 2334 public void testExistingEUserI() { 2335 // Do not run on devices with emulated external storage. 2336 if (Environment.isExternalStorageEmulated()) { 2337 return; 2338 } 2339 2340 int userSetting = PackageHelper.APP_INSTALL_INTERNAL; 2341 int iFlags = PackageManager.INSTALL_EXTERNAL; 2342 setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); 2343 } 2344 2345 @LargeTest 2346 public void testExistingEUserE() { 2347 // Do not run on devices with emulated external storage. 2348 if (Environment.isExternalStorageEmulated()) { 2349 return; 2350 } 2351 2352 int userSetting = PackageHelper.APP_INSTALL_EXTERNAL; 2353 int iFlags = PackageManager.INSTALL_EXTERNAL; 2354 setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); 2355 } 2356 2357 @LargeTest 2358 public void testExistingEUserA() { 2359 // Do not run on devices with emulated external storage. 2360 if (Environment.isExternalStorageEmulated()) { 2361 return; 2362 } 2363 2364 int userSetting = PackageHelper.APP_INSTALL_AUTO; 2365 int iFlags = PackageManager.INSTALL_EXTERNAL; 2366 setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); 2367 } 2368 2369 /* 2370 * The following set of tests verify that the user setting defines 2371 * the install location. 2372 * 2373 */ 2374 private boolean getUserSettingSetInstallLocation() { 2375 try { 2376 return Settings.System.getInt(mContext.getContentResolver(), Settings.Secure.SET_INSTALL_LOCATION) != 0; 2377 2378 } catch (SettingNotFoundException e1) { 2379 } 2380 return false; 2381 } 2382 2383 private void setUserSettingSetInstallLocation(boolean value) { 2384 Settings.System.putInt(mContext.getContentResolver(), 2385 Settings.Secure.SET_INSTALL_LOCATION, value ? 1 : 0); 2386 } 2387 private void setUserX(boolean enable, int userSetting, int iloc) { 2388 boolean origUserSetting = getUserSettingSetInstallLocation(); 2389 int origSetting = getDefaultInstallLoc(); 2390 try { 2391 setUserSettingSetInstallLocation(enable); 2392 // Set user setting 2393 setInstallLoc(userSetting); 2394 // Replace now 2395 installFromRawResource("install.apk", R.raw.install, 2396 0, 2397 true, 2398 false, -1, 2399 iloc); 2400 } finally { 2401 // Restore original setting 2402 setUserSettingSetInstallLocation(origUserSetting); 2403 setInstallLoc(origSetting); 2404 } 2405 } 2406 @LargeTest 2407 public void testUserI() { 2408 int userSetting = PackageHelper.APP_INSTALL_INTERNAL; 2409 int iloc = getExpectedInstallLocation(userSetting); 2410 setUserX(true, userSetting, iloc); 2411 } 2412 2413 @LargeTest 2414 public void testUserE() { 2415 // Do not run on devices with emulated external storage. 2416 if (Environment.isExternalStorageEmulated()) { 2417 return; 2418 } 2419 2420 int userSetting = PackageHelper.APP_INSTALL_EXTERNAL; 2421 int iloc = getExpectedInstallLocation(userSetting); 2422 setUserX(true, userSetting, iloc); 2423 } 2424 2425 @LargeTest 2426 public void testUserA() { 2427 int userSetting = PackageHelper.APP_INSTALL_AUTO; 2428 int iloc = getExpectedInstallLocation(userSetting); 2429 setUserX(true, userSetting, iloc); 2430 } 2431 /* 2432 * The following set of tests turn on/off the basic 2433 * user setting for turning on install location. 2434 */ 2435 @LargeTest 2436 public void testUserPrefOffUserI() { 2437 int userSetting = PackageHelper.APP_INSTALL_INTERNAL; 2438 int iloc = PackageInfo.INSTALL_LOCATION_UNSPECIFIED; 2439 setUserX(false, userSetting, iloc); 2440 } 2441 2442 @LargeTest 2443 public void testUserPrefOffUserE() { 2444 // Do not run on devices with emulated external storage. 2445 if (Environment.isExternalStorageEmulated()) { 2446 return; 2447 } 2448 2449 int userSetting = PackageHelper.APP_INSTALL_EXTERNAL; 2450 int iloc = PackageInfo.INSTALL_LOCATION_UNSPECIFIED; 2451 setUserX(false, userSetting, iloc); 2452 } 2453 2454 @LargeTest 2455 public void testUserPrefOffA() { 2456 int userSetting = PackageHelper.APP_INSTALL_AUTO; 2457 int iloc = PackageInfo.INSTALL_LOCATION_UNSPECIFIED; 2458 setUserX(false, userSetting, iloc); 2459 } 2460 2461 static final String BASE_PERMISSIONS_DEFINED[] = new String[] { 2462 PERM_PACKAGE, "com.android.unit_tests.install_decl_perm", 2463 PERM_DEFINED, 2464 "com.android.frameworks.coretests.NORMAL", 2465 "com.android.frameworks.coretests.DANGEROUS", 2466 "com.android.frameworks.coretests.SIGNATURE", 2467 }; 2468 2469 static final String BASE_PERMISSIONS_UNDEFINED[] = new String[] { 2470 PERM_PACKAGE, "com.android.frameworks.coretests.install_decl_perm", 2471 PERM_UNDEFINED, 2472 "com.android.frameworks.coretests.NORMAL", 2473 "com.android.frameworks.coretests.DANGEROUS", 2474 "com.android.frameworks.coretests.SIGNATURE", 2475 }; 2476 2477 static final String BASE_PERMISSIONS_USED[] = new String[] { 2478 PERM_PACKAGE, "com.android.frameworks.coretests.install_use_perm_good", 2479 PERM_USED, 2480 "com.android.frameworks.coretests.NORMAL", 2481 "com.android.frameworks.coretests.DANGEROUS", 2482 "com.android.frameworks.coretests.SIGNATURE", 2483 }; 2484 2485 static final String BASE_PERMISSIONS_NOTUSED[] = new String[] { 2486 PERM_PACKAGE, "com.android.frameworks.coretests.install_use_perm_good", 2487 PERM_NOTUSED, 2488 "com.android.frameworks.coretests.NORMAL", 2489 "com.android.frameworks.coretests.DANGEROUS", 2490 "com.android.frameworks.coretests.SIGNATURE", 2491 }; 2492 2493 static final String BASE_PERMISSIONS_SIGUSED[] = new String[] { 2494 PERM_PACKAGE, "com.android.frameworks.coretests.install_use_perm_good", 2495 PERM_USED, 2496 "com.android.frameworks.coretests.SIGNATURE", 2497 PERM_NOTUSED, 2498 "com.android.frameworks.coretests.NORMAL", 2499 "com.android.frameworks.coretests.DANGEROUS", 2500 }; 2501 2502 /* 2503 * Ensure that permissions are properly declared. 2504 */ 2505 @LargeTest 2506 public void testInstallDeclaresPermissions() { 2507 InstallParams ip = null; 2508 InstallParams ip2 = null; 2509 try { 2510 // **: Upon installing a package, are its declared permissions published? 2511 2512 int iFlags = PackageManager.INSTALL_INTERNAL; 2513 int iApk = R.raw.install_decl_perm; 2514 ip = installFromRawResource("install.apk", iApk, 2515 iFlags, false, 2516 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); 2517 assertInstall(ip.pkg, iFlags, ip.pkg.installLocation); 2518 assertPermissions(BASE_PERMISSIONS_DEFINED); 2519 2520 // **: Upon installing package, are its permissions granted? 2521 2522 int i2Flags = PackageManager.INSTALL_INTERNAL; 2523 int i2Apk = R.raw.install_use_perm_good; 2524 ip2 = installFromRawResource("install2.apk", i2Apk, 2525 i2Flags, false, 2526 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); 2527 assertInstall(ip2.pkg, i2Flags, ip2.pkg.installLocation); 2528 assertPermissions(BASE_PERMISSIONS_USED); 2529 2530 // **: Upon removing but not deleting, are permissions retained? 2531 2532 GenericReceiver receiver = new DeleteReceiver(ip.pkg.packageName); 2533 2534 try { 2535 invokeDeletePackage(ip.pkg.packageName, PackageManager.DONT_DELETE_DATA, receiver); 2536 } catch (Exception e) { 2537 failStr(e); 2538 } 2539 assertPermissions(BASE_PERMISSIONS_DEFINED); 2540 assertPermissions(BASE_PERMISSIONS_USED); 2541 2542 // **: Upon re-installing, are permissions retained? 2543 2544 ip = installFromRawResource("install.apk", iApk, 2545 iFlags | PackageManager.INSTALL_REPLACE_EXISTING, false, 2546 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); 2547 assertInstall(ip.pkg, iFlags, ip.pkg.installLocation); 2548 assertPermissions(BASE_PERMISSIONS_DEFINED); 2549 assertPermissions(BASE_PERMISSIONS_USED); 2550 2551 // **: Upon deleting package, are all permissions removed? 2552 2553 try { 2554 invokeDeletePackage(ip.pkg.packageName, 0, receiver); 2555 ip = null; 2556 } catch (Exception e) { 2557 failStr(e); 2558 } 2559 assertPermissions(BASE_PERMISSIONS_UNDEFINED); 2560 assertPermissions(BASE_PERMISSIONS_NOTUSED); 2561 2562 // **: Delete package using permissions; nothing to check here. 2563 2564 GenericReceiver receiver2 = new DeleteReceiver(ip2.pkg.packageName); 2565 try { 2566 invokeDeletePackage(ip2.pkg.packageName, 0, receiver); 2567 ip2 = null; 2568 } catch (Exception e) { 2569 failStr(e); 2570 } 2571 2572 // **: Re-install package using permissions; no permissions can be granted. 2573 2574 ip2 = installFromRawResource("install2.apk", i2Apk, 2575 i2Flags, false, 2576 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); 2577 assertInstall(ip2.pkg, i2Flags, ip2.pkg.installLocation); 2578 assertPermissions(BASE_PERMISSIONS_NOTUSED); 2579 2580 // **: Upon installing declaring package, are sig permissions granted 2581 // to other apps (but not other perms)? 2582 2583 ip = installFromRawResource("install.apk", iApk, 2584 iFlags, false, 2585 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); 2586 assertInstall(ip.pkg, iFlags, ip.pkg.installLocation); 2587 assertPermissions(BASE_PERMISSIONS_DEFINED); 2588 assertPermissions(BASE_PERMISSIONS_SIGUSED); 2589 2590 // **: Re-install package using permissions; are all permissions granted? 2591 2592 ip2 = installFromRawResource("install2.apk", i2Apk, 2593 i2Flags | PackageManager.INSTALL_REPLACE_EXISTING, false, 2594 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); 2595 assertInstall(ip2.pkg, i2Flags, ip2.pkg.installLocation); 2596 assertPermissions(BASE_PERMISSIONS_NOTUSED); 2597 2598 // **: Upon deleting package, are all permissions removed? 2599 2600 try { 2601 invokeDeletePackage(ip.pkg.packageName, 0, receiver); 2602 ip = null; 2603 } catch (Exception e) { 2604 failStr(e); 2605 } 2606 assertPermissions(BASE_PERMISSIONS_UNDEFINED); 2607 assertPermissions(BASE_PERMISSIONS_NOTUSED); 2608 2609 // **: Delete package using permissions; nothing to check here. 2610 2611 try { 2612 invokeDeletePackage(ip2.pkg.packageName, 0, receiver); 2613 ip2 = null; 2614 } catch (Exception e) { 2615 failStr(e); 2616 } 2617 2618 } finally { 2619 if (ip2 != null) { 2620 cleanUpInstall(ip2); 2621 } 2622 if (ip != null) { 2623 cleanUpInstall(ip); 2624 } 2625 } 2626 } 2627 2628 /* 2629 * Ensure that permissions are properly declared. 2630 */ 2631 @LargeTest 2632 public void testInstallOnSdPermissionsUnmount() { 2633 InstallParams ip = null; 2634 boolean origMediaState = checkMediaState(Environment.MEDIA_MOUNTED); 2635 try { 2636 // **: Upon installing a package, are its declared permissions published? 2637 int iFlags = PackageManager.INSTALL_INTERNAL; 2638 int iApk = R.raw.install_decl_perm; 2639 ip = installFromRawResource("install.apk", iApk, 2640 iFlags, false, 2641 false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); 2642 assertInstall(ip.pkg, iFlags, ip.pkg.installLocation); 2643 assertPermissions(BASE_PERMISSIONS_DEFINED); 2644 // Unmount media here 2645 assertTrue(unmountMedia()); 2646 // Mount media again 2647 mountMedia(); 2648 //Check permissions now 2649 assertPermissions(BASE_PERMISSIONS_DEFINED); 2650 } finally { 2651 if (ip != null) { 2652 cleanUpInstall(ip); 2653 } 2654 } 2655 } 2656 2657 /* This test creates a stale container via MountService and then installs 2658 * a package and verifies that the stale container is cleaned up and install 2659 * is successful. 2660 * Please note that this test is very closely tied to the framework's 2661 * naming convention for secure containers. 2662 */ 2663 @LargeTest 2664 public void testInstallSdcardStaleContainer() { 2665 // Do not run on devices with emulated external storage. 2666 if (Environment.isExternalStorageEmulated()) { 2667 return; 2668 } 2669 2670 boolean origMediaState = checkMediaState(Environment.MEDIA_MOUNTED); 2671 try { 2672 // Mount media first 2673 mountMedia(); 2674 String outFileName = "install.apk"; 2675 int rawResId = R.raw.install; 2676 PackageManager pm = mContext.getPackageManager(); 2677 File filesDir = mContext.getFilesDir(); 2678 File outFile = new File(filesDir, outFileName); 2679 Uri packageURI = getInstallablePackage(rawResId, outFile); 2680 PackageParser.Package pkg = parsePackage(packageURI); 2681 assertNotNull(pkg); 2682 // Install an app on sdcard. 2683 installFromRawResource(outFileName, rawResId, 2684 PackageManager.INSTALL_EXTERNAL, false, 2685 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); 2686 // Unmount sdcard 2687 unmountMedia(); 2688 // Delete the app on sdcard to leave a stale container on sdcard. 2689 GenericReceiver receiver = new DeleteReceiver(pkg.packageName); 2690 assertTrue(invokeDeletePackage(pkg.packageName, 0, receiver)); 2691 mountMedia(); 2692 // Reinstall the app and make sure it gets installed. 2693 installFromRawResource(outFileName, rawResId, 2694 PackageManager.INSTALL_EXTERNAL, true, 2695 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); 2696 } catch (Exception e) { 2697 failStr(e.getMessage()); 2698 } finally { 2699 if (origMediaState) { 2700 mountMedia(); 2701 } else { 2702 unmountMedia(); 2703 } 2704 2705 } 2706 } 2707 2708 /* This test installs an application on sdcard and unmounts media. 2709 * The app is then re-installed on internal storage. The sdcard is mounted 2710 * and verified that the re-installation on internal storage takes precedence. 2711 */ 2712 @LargeTest 2713 public void testInstallSdcardStaleContainerReinstall() { 2714 // Do not run on devices with emulated external storage. 2715 if (Environment.isExternalStorageEmulated()) { 2716 return; 2717 } 2718 2719 boolean origMediaState = checkMediaState(Environment.MEDIA_MOUNTED); 2720 try { 2721 // Mount media first 2722 mountMedia(); 2723 String outFileName = "install.apk"; 2724 int rawResId = R.raw.install; 2725 PackageManager pm = mContext.getPackageManager(); 2726 File filesDir = mContext.getFilesDir(); 2727 File outFile = new File(filesDir, outFileName); 2728 Uri packageURI = getInstallablePackage(rawResId, outFile); 2729 PackageParser.Package pkg = parsePackage(packageURI); 2730 assertNotNull(pkg); 2731 // Install an app on sdcard. 2732 installFromRawResource(outFileName, rawResId, 2733 PackageManager.INSTALL_EXTERNAL, false, 2734 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); 2735 // Unmount sdcard 2736 unmountMedia(); 2737 // Reinstall the app and make sure it gets installed on internal storage. 2738 installFromRawResource(outFileName, rawResId, 2739 PackageManager.INSTALL_REPLACE_EXISTING, false, 2740 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); 2741 mountMedia(); 2742 // Verify that the app installed is on internal storage. 2743 assertInstall(pkg, 0, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); 2744 } catch (Exception e) { 2745 failStr(e.getMessage()); 2746 } finally { 2747 if (origMediaState) { 2748 mountMedia(); 2749 } else { 2750 unmountMedia(); 2751 } 2752 } 2753 } 2754 2755 /* 2756 * The following series of tests are related to upgrading apps with 2757 * different certificates. 2758 */ 2759 private int APP1_UNSIGNED = R.raw.install_app1_unsigned; 2760 private int APP1_CERT1 = R.raw.install_app1_cert1; 2761 private int APP1_CERT2 = R.raw.install_app1_cert2; 2762 private int APP1_CERT1_CERT2 = R.raw.install_app1_cert1_cert2; 2763 private int APP1_CERT3_CERT4 = R.raw.install_app1_cert3_cert4; 2764 private int APP1_CERT3 = R.raw.install_app1_cert3; 2765 private int APP2_UNSIGNED = R.raw.install_app2_unsigned; 2766 private int APP2_CERT1 = R.raw.install_app2_cert1; 2767 private int APP2_CERT2 = R.raw.install_app2_cert2; 2768 private int APP2_CERT1_CERT2 = R.raw.install_app2_cert1_cert2; 2769 private int APP2_CERT3 = R.raw.install_app2_cert3; 2770 2771 private InstallParams replaceCerts(int apk1, int apk2, boolean cleanUp, boolean fail, int retCode) { 2772 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING; 2773 String apk1Name = "install1.apk"; 2774 String apk2Name = "install2.apk"; 2775 PackageParser.Package pkg1 = getParsedPackage(apk1Name, apk1); 2776 try { 2777 InstallParams ip = installFromRawResource(apk1Name, apk1, 0, false, 2778 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); 2779 installFromRawResource(apk2Name, apk2, rFlags, false, 2780 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); 2781 return ip; 2782 } catch (Exception e) { 2783 failStr(e.getMessage()); 2784 } finally { 2785 if (cleanUp) { 2786 cleanUpInstall(pkg1.packageName); 2787 } 2788 } 2789 return null; 2790 } 2791 /* 2792 * Test that an app signed with two certificates can be upgraded by the 2793 * same app signed with two certificates. 2794 */ 2795 @LargeTest 2796 public void testReplaceMatchAllCerts() { 2797 replaceCerts(APP1_CERT1_CERT2, APP1_CERT1_CERT2, true, false, -1); 2798 } 2799 2800 /* 2801 * Test that an app signed with two certificates cannot be upgraded 2802 * by an app signed with a different certificate. 2803 */ 2804 @LargeTest 2805 public void testReplaceMatchNoCerts1() { 2806 replaceCerts(APP1_CERT1_CERT2, APP1_CERT3, true, true, 2807 PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES); 2808 } 2809 /* 2810 * Test that an app signed with two certificates cannot be upgraded 2811 * by an app signed with a different certificate. 2812 */ 2813 @LargeTest 2814 public void testReplaceMatchNoCerts2() { 2815 replaceCerts(APP1_CERT1_CERT2, APP1_CERT3_CERT4, true, true, 2816 PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES); 2817 } 2818 /* 2819 * Test that an app signed with two certificates cannot be upgraded by 2820 * an app signed with a subset of initial certificates. 2821 */ 2822 @LargeTest 2823 public void testReplaceMatchSomeCerts1() { 2824 replaceCerts(APP1_CERT1_CERT2, APP1_CERT1, true, true, 2825 PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES); 2826 } 2827 /* 2828 * Test that an app signed with two certificates cannot be upgraded by 2829 * an app signed with the last certificate. 2830 */ 2831 @LargeTest 2832 public void testReplaceMatchSomeCerts2() { 2833 replaceCerts(APP1_CERT1_CERT2, APP1_CERT2, true, true, 2834 PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES); 2835 } 2836 /* 2837 * Test that an app signed with a certificate can be upgraded by app 2838 * signed with a superset of certificates. 2839 */ 2840 @LargeTest 2841 public void testReplaceMatchMoreCerts() { 2842 replaceCerts(APP1_CERT1, APP1_CERT1_CERT2, true, true, 2843 PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES); 2844 } 2845 /* 2846 * Test that an app signed with a certificate can be upgraded by app 2847 * signed with a superset of certificates. Then verify that the an app 2848 * signed with the original set of certs cannot upgrade the new one. 2849 */ 2850 @LargeTest 2851 public void testReplaceMatchMoreCertsReplaceSomeCerts() { 2852 InstallParams ip = replaceCerts(APP1_CERT1, APP1_CERT1_CERT2, false, true, 2853 PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES); 2854 try { 2855 int rFlags = PackageManager.INSTALL_REPLACE_EXISTING; 2856 installFromRawResource("install.apk", APP1_CERT1, rFlags, false, 2857 false, -1, 2858 PackageInfo.INSTALL_LOCATION_UNSPECIFIED); 2859 } catch (Exception e) { 2860 failStr(e.getMessage()); 2861 } finally { 2862 if (ip != null) { 2863 cleanUpInstall(ip); 2864 } 2865 } 2866 } 2867 /* 2868 * The following tests are related to testing the checkSignatures 2869 * api. 2870 */ 2871 private void checkSignatures(int apk1, int apk2, int expMatchResult) { 2872 checkSharedSignatures(apk1, apk2, true, false, -1, expMatchResult); 2873 } 2874 @LargeTest 2875 public void testCheckSignaturesAllMatch() { 2876 int apk1 = APP1_CERT1_CERT2; 2877 int apk2 = APP2_CERT1_CERT2; 2878 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_MATCH); 2879 } 2880 @LargeTest 2881 public void testCheckSignaturesNoMatch() { 2882 int apk1 = APP1_CERT1; 2883 int apk2 = APP2_CERT2; 2884 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_NO_MATCH); 2885 } 2886 @LargeTest 2887 public void testCheckSignaturesSomeMatch1() { 2888 int apk1 = APP1_CERT1_CERT2; 2889 int apk2 = APP2_CERT1; 2890 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_NO_MATCH); 2891 } 2892 @LargeTest 2893 public void testCheckSignaturesSomeMatch2() { 2894 int apk1 = APP1_CERT1_CERT2; 2895 int apk2 = APP2_CERT2; 2896 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_NO_MATCH); 2897 } 2898 @LargeTest 2899 public void testCheckSignaturesMoreMatch() { 2900 int apk1 = APP1_CERT1; 2901 int apk2 = APP2_CERT1_CERT2; 2902 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_NO_MATCH); 2903 } 2904 @LargeTest 2905 public void testCheckSignaturesUnknown() { 2906 int apk1 = APP1_CERT1_CERT2; 2907 int apk2 = APP2_CERT1_CERT2; 2908 String apk1Name = "install1.apk"; 2909 String apk2Name = "install2.apk"; 2910 InstallParams ip1 = null; 2911 2912 try { 2913 ip1 = installFromRawResource(apk1Name, apk1, 0, false, 2914 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); 2915 PackageManager pm = mContext.getPackageManager(); 2916 // Delete app2 2917 File filesDir = mContext.getFilesDir(); 2918 File outFile = new File(filesDir, apk2Name); 2919 int rawResId = apk2; 2920 Uri packageURI = getInstallablePackage(rawResId, outFile); 2921 PackageParser.Package pkg = parsePackage(packageURI); 2922 getPm().deletePackage(pkg.packageName, null, 0); 2923 // Check signatures now 2924 int match = mContext.getPackageManager().checkSignatures( 2925 ip1.pkg.packageName, pkg.packageName); 2926 assertEquals(PackageManager.SIGNATURE_UNKNOWN_PACKAGE, match); 2927 } finally { 2928 if (ip1 != null) { 2929 cleanUpInstall(ip1); 2930 } 2931 } 2932 } 2933 @LargeTest 2934 public void testInstallNoCertificates() { 2935 int apk1 = APP1_UNSIGNED; 2936 String apk1Name = "install1.apk"; 2937 InstallParams ip1 = null; 2938 2939 try { 2940 installFromRawResource(apk1Name, apk1, 0, false, 2941 true, PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES, 2942 PackageInfo.INSTALL_LOCATION_UNSPECIFIED); 2943 } finally { 2944 } 2945 } 2946 /* The following tests are related to apps using shared uids signed 2947 * with different certs. 2948 */ 2949 private int SHARED1_UNSIGNED = R.raw.install_shared1_unsigned; 2950 private int SHARED1_CERT1 = R.raw.install_shared1_cert1; 2951 private int SHARED1_CERT2 = R.raw.install_shared1_cert2; 2952 private int SHARED1_CERT1_CERT2 = R.raw.install_shared1_cert1_cert2; 2953 private int SHARED2_UNSIGNED = R.raw.install_shared2_unsigned; 2954 private int SHARED2_CERT1 = R.raw.install_shared2_cert1; 2955 private int SHARED2_CERT2 = R.raw.install_shared2_cert2; 2956 private int SHARED2_CERT1_CERT2 = R.raw.install_shared2_cert1_cert2; 2957 private void checkSharedSignatures(int apk1, int apk2, boolean cleanUp, boolean fail, int retCode, int expMatchResult) { 2958 String apk1Name = "install1.apk"; 2959 String apk2Name = "install2.apk"; 2960 PackageParser.Package pkg1 = getParsedPackage(apk1Name, apk1); 2961 PackageParser.Package pkg2 = getParsedPackage(apk2Name, apk2); 2962 2963 try { 2964 // Clean up before testing first. 2965 cleanUpInstall(pkg1.packageName); 2966 cleanUpInstall(pkg2.packageName); 2967 installFromRawResource(apk1Name, apk1, 0, false, 2968 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); 2969 if (fail) { 2970 installFromRawResource(apk2Name, apk2, 0, false, 2971 true, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); 2972 } else { 2973 installFromRawResource(apk2Name, apk2, 0, false, 2974 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); 2975 int match = mContext.getPackageManager().checkSignatures( 2976 pkg1.packageName, pkg2.packageName); 2977 assertEquals(expMatchResult, match); 2978 } 2979 } finally { 2980 if (cleanUp) { 2981 cleanUpInstall(pkg1.packageName); 2982 cleanUpInstall(pkg2.packageName); 2983 } 2984 } 2985 } 2986 @LargeTest 2987 public void testCheckSignaturesSharedAllMatch() { 2988 int apk1 = SHARED1_CERT1_CERT2; 2989 int apk2 = SHARED2_CERT1_CERT2; 2990 boolean fail = false; 2991 int retCode = -1; 2992 int expMatchResult = PackageManager.SIGNATURE_MATCH; 2993 checkSharedSignatures(apk1, apk2, true, fail, retCode, expMatchResult); 2994 } 2995 @LargeTest 2996 public void testCheckSignaturesSharedNoMatch() { 2997 int apk1 = SHARED1_CERT1; 2998 int apk2 = SHARED2_CERT2; 2999 boolean fail = true; 3000 int retCode = PackageManager.INSTALL_FAILED_SHARED_USER_INCOMPATIBLE; 3001 int expMatchResult = -1; 3002 checkSharedSignatures(apk1, apk2, true, fail, retCode, expMatchResult); 3003 } 3004 /* 3005 * Test that an app signed with cert1 and cert2 cannot be replaced when signed with cert1 alone. 3006 */ 3007 @LargeTest 3008 public void testCheckSignaturesSharedSomeMatch1() { 3009 int apk1 = SHARED1_CERT1_CERT2; 3010 int apk2 = SHARED2_CERT1; 3011 boolean fail = true; 3012 int retCode = PackageManager.INSTALL_FAILED_SHARED_USER_INCOMPATIBLE; 3013 int expMatchResult = -1; 3014 checkSharedSignatures(apk1, apk2, true, fail, retCode, expMatchResult); 3015 } 3016 /* 3017 * Test that an app signed with cert1 and cert2 cannot be replaced when signed with cert2 alone. 3018 */ 3019 @LargeTest 3020 public void testCheckSignaturesSharedSomeMatch2() { 3021 int apk1 = SHARED1_CERT1_CERT2; 3022 int apk2 = SHARED2_CERT2; 3023 boolean fail = true; 3024 int retCode = PackageManager.INSTALL_FAILED_SHARED_USER_INCOMPATIBLE; 3025 int expMatchResult = -1; 3026 checkSharedSignatures(apk1, apk2, true, fail, retCode, expMatchResult); 3027 } 3028 @LargeTest 3029 public void testCheckSignaturesSharedUnknown() { 3030 int apk1 = SHARED1_CERT1_CERT2; 3031 int apk2 = SHARED2_CERT1_CERT2; 3032 String apk1Name = "install1.apk"; 3033 String apk2Name = "install2.apk"; 3034 InstallParams ip1 = null; 3035 3036 try { 3037 ip1 = installFromRawResource(apk1Name, apk1, 0, false, 3038 false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); 3039 PackageManager pm = mContext.getPackageManager(); 3040 // Delete app2 3041 PackageParser.Package pkg = getParsedPackage(apk2Name, apk2); 3042 getPm().deletePackage(pkg.packageName, null, 0); 3043 // Check signatures now 3044 int match = mContext.getPackageManager().checkSignatures( 3045 ip1.pkg.packageName, pkg.packageName); 3046 assertEquals(PackageManager.SIGNATURE_UNKNOWN_PACKAGE, match); 3047 } finally { 3048 if (ip1 != null) { 3049 cleanUpInstall(ip1); 3050 } 3051 } 3052 } 3053 3054 @LargeTest 3055 public void testReplaceFirstSharedMatchAllCerts() { 3056 int apk1 = SHARED1_CERT1; 3057 int apk2 = SHARED2_CERT1; 3058 int rapk1 = SHARED1_CERT1; 3059 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_MATCH); 3060 replaceCerts(apk1, rapk1, true, false, -1); 3061 } 3062 @LargeTest 3063 public void testReplaceSecondSharedMatchAllCerts() { 3064 int apk1 = SHARED1_CERT1; 3065 int apk2 = SHARED2_CERT1; 3066 int rapk2 = SHARED2_CERT1; 3067 checkSignatures(apk1, apk2, PackageManager.SIGNATURE_MATCH); 3068 replaceCerts(apk2, rapk2, true, false, -1); 3069 } 3070 @LargeTest 3071 public void testReplaceFirstSharedMatchSomeCerts() { 3072 int apk1 = SHARED1_CERT1_CERT2; 3073 int apk2 = SHARED2_CERT1_CERT2; 3074 int rapk1 = SHARED1_CERT1; 3075 boolean fail = true; 3076 int retCode = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES; 3077 checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH); 3078 installFromRawResource("install.apk", rapk1, PackageManager.INSTALL_REPLACE_EXISTING, true, 3079 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); 3080 } 3081 @LargeTest 3082 public void testReplaceSecondSharedMatchSomeCerts() { 3083 int apk1 = SHARED1_CERT1_CERT2; 3084 int apk2 = SHARED2_CERT1_CERT2; 3085 int rapk2 = SHARED2_CERT1; 3086 boolean fail = true; 3087 int retCode = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES; 3088 checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH); 3089 installFromRawResource("install.apk", rapk2, PackageManager.INSTALL_REPLACE_EXISTING, true, 3090 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); 3091 } 3092 @LargeTest 3093 public void testReplaceFirstSharedMatchNoCerts() { 3094 int apk1 = SHARED1_CERT1; 3095 int apk2 = SHARED2_CERT1; 3096 int rapk1 = SHARED1_CERT2; 3097 boolean fail = true; 3098 int retCode = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES; 3099 checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH); 3100 installFromRawResource("install.apk", rapk1, PackageManager.INSTALL_REPLACE_EXISTING, true, 3101 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); 3102 } 3103 @LargeTest 3104 public void testReplaceSecondSharedMatchNoCerts() { 3105 int apk1 = SHARED1_CERT1; 3106 int apk2 = SHARED2_CERT1; 3107 int rapk2 = SHARED2_CERT2; 3108 boolean fail = true; 3109 int retCode = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES; 3110 checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH); 3111 installFromRawResource("install.apk", rapk2, PackageManager.INSTALL_REPLACE_EXISTING, true, 3112 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); 3113 } 3114 @LargeTest 3115 public void testReplaceFirstSharedMatchMoreCerts() { 3116 int apk1 = SHARED1_CERT1; 3117 int apk2 = SHARED2_CERT1; 3118 int rapk1 = SHARED1_CERT1_CERT2; 3119 boolean fail = true; 3120 int retCode = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES; 3121 checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH); 3122 installFromRawResource("install.apk", rapk1, PackageManager.INSTALL_REPLACE_EXISTING, true, 3123 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); 3124 } 3125 @LargeTest 3126 public void testReplaceSecondSharedMatchMoreCerts() { 3127 int apk1 = SHARED1_CERT1; 3128 int apk2 = SHARED2_CERT1; 3129 int rapk2 = SHARED2_CERT1_CERT2; 3130 boolean fail = true; 3131 int retCode = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES; 3132 checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH); 3133 installFromRawResource("install.apk", rapk2, PackageManager.INSTALL_REPLACE_EXISTING, true, 3134 fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); 3135 } 3136 3137 /** 3138 * Unknown features should be allowed to install. This prevents older phones 3139 * from rejecting new packages that specify features that didn't exist when 3140 * an older phone existed. All older phones are assumed to have those 3141 * features. 3142 * <p> 3143 * Right now we allow all packages to be installed regardless of their 3144 * features. 3145 */ 3146 @LargeTest 3147 public void testUsesFeatureUnknownFeature() { 3148 int retCode = PackageManager.INSTALL_SUCCEEDED; 3149 installFromRawResource("install.apk", R.raw.install_uses_feature, 0, true, false, retCode, 3150 PackageInfo.INSTALL_LOCATION_UNSPECIFIED); 3151 } 3152 3153 @LargeTest 3154 public void testInstallNonexistentFile() { 3155 int retCode = PackageManager.INSTALL_FAILED_INVALID_URI; 3156 File invalidFile = new File("/nonexistent-file.apk"); 3157 invokeInstallPackageFail(Uri.fromFile(invalidFile), 0, retCode); 3158 } 3159 3160 @SmallTest 3161 public void testGetVerifierDeviceIdentity() { 3162 PackageManager pm = getPm(); 3163 VerifierDeviceIdentity id = pm.getVerifierDeviceIdentity(); 3164 3165 assertNotNull("Verifier device identity should not be null", id); 3166 } 3167 3168 public void testGetInstalledPackages() { 3169 List<PackageInfo> packages = getPm().getInstalledPackages(0); 3170 assertNotNull("installed packages cannot be null", packages); 3171 assertTrue("installed packages cannot be empty", packages.size() > 0); 3172 } 3173 3174 public void testGetUnInstalledPackages() { 3175 List<PackageInfo> packages = getPm().getInstalledPackages( 3176 PackageManager.GET_UNINSTALLED_PACKAGES); 3177 assertNotNull("installed packages cannot be null", packages); 3178 assertTrue("installed packages cannot be empty", packages.size() > 0); 3179 } 3180 3181 /** 3182 * Test that getInstalledPackages returns all the data specified in 3183 * flags. 3184 */ 3185 public void testGetInstalledPackagesAll() { 3186 int flags = PackageManager.GET_ACTIVITIES | PackageManager.GET_GIDS 3187 | PackageManager.GET_CONFIGURATIONS | PackageManager.GET_INSTRUMENTATION 3188 | PackageManager.GET_PERMISSIONS | PackageManager.GET_PROVIDERS 3189 | PackageManager.GET_RECEIVERS | PackageManager.GET_SERVICES 3190 | PackageManager.GET_SIGNATURES | PackageManager.GET_UNINSTALLED_PACKAGES; 3191 3192 List<PackageInfo> packages = getPm().getInstalledPackages(flags); 3193 assertNotNull("installed packages cannot be null", packages); 3194 assertTrue("installed packages cannot be empty", packages.size() > 0); 3195 3196 PackageInfo packageInfo = null; 3197 3198 // Find the package with all components specified in the AndroidManifest 3199 // to ensure no null values 3200 for (PackageInfo pi : packages) { 3201 if ("com.android.frameworks.coretests.install_complete_package_info" 3202 .equals(pi.packageName)) { 3203 packageInfo = pi; 3204 break; 3205 } 3206 } 3207 assertNotNull("activities should not be null", packageInfo.activities); 3208 assertNotNull("configPreferences should not be null", packageInfo.configPreferences); 3209 assertNotNull("instrumentation should not be null", packageInfo.instrumentation); 3210 assertNotNull("permissions should not be null", packageInfo.permissions); 3211 assertNotNull("providers should not be null", packageInfo.providers); 3212 assertNotNull("receivers should not be null", packageInfo.receivers); 3213 assertNotNull("services should not be null", packageInfo.services); 3214 assertNotNull("signatures should not be null", packageInfo.signatures); 3215 } 3216 3217 /** 3218 * Test that getInstalledPackages returns all the data specified in 3219 * flags when the GET_UNINSTALLED_PACKAGES flag is set. 3220 */ 3221 public void testGetUnInstalledPackagesAll() { 3222 int flags = PackageManager.GET_UNINSTALLED_PACKAGES 3223 | PackageManager.GET_ACTIVITIES | PackageManager.GET_GIDS 3224 | PackageManager.GET_CONFIGURATIONS | PackageManager.GET_INSTRUMENTATION 3225 | PackageManager.GET_PERMISSIONS | PackageManager.GET_PROVIDERS 3226 | PackageManager.GET_RECEIVERS | PackageManager.GET_SERVICES 3227 | PackageManager.GET_SIGNATURES | PackageManager.GET_UNINSTALLED_PACKAGES; 3228 3229 List<PackageInfo> packages = getPm().getInstalledPackages(flags); 3230 assertNotNull("installed packages cannot be null", packages); 3231 assertTrue("installed packages cannot be empty", packages.size() > 0); 3232 3233 PackageInfo packageInfo = null; 3234 3235 // Find the package with all components specified in the AndroidManifest 3236 // to ensure no null values 3237 for (PackageInfo pi : packages) { 3238 if ("com.android.frameworks.coretests.install_complete_package_info" 3239 .equals(pi.packageName)) { 3240 packageInfo = pi; 3241 break; 3242 } 3243 } 3244 assertNotNull("activities should not be null", packageInfo.activities); 3245 assertNotNull("configPreferences should not be null", packageInfo.configPreferences); 3246 assertNotNull("instrumentation should not be null", packageInfo.instrumentation); 3247 assertNotNull("permissions should not be null", packageInfo.permissions); 3248 assertNotNull("providers should not be null", packageInfo.providers); 3249 assertNotNull("receivers should not be null", packageInfo.receivers); 3250 assertNotNull("services should not be null", packageInfo.services); 3251 assertNotNull("signatures should not be null", packageInfo.signatures); 3252 } 3253 3254 /*---------- Recommended install location tests ----*/ 3255 /* 3256 * TODO's 3257 * check version numbers for upgrades 3258 * check permissions of installed packages 3259 * how to do tests on updated system apps? 3260 * verify updates to system apps cannot be installed on the sdcard. 3261 */ 3262} 3263