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