Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 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 | |
Brett Chabot | a3de745 | 2010-03-25 13:49:26 -0700 | [diff] [blame] | 17 | package android.content.pm; |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 18 | |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 19 | import static android.system.OsConstants.S_IFDIR; |
| 20 | import static android.system.OsConstants.S_IFMT; |
| 21 | import static android.system.OsConstants.S_IRGRP; |
| 22 | import static android.system.OsConstants.S_IROTH; |
| 23 | import static android.system.OsConstants.S_IRWXU; |
| 24 | import static android.system.OsConstants.S_ISDIR; |
| 25 | import static android.system.OsConstants.S_IXGRP; |
| 26 | import static android.system.OsConstants.S_IXOTH; |
Suchi Amalapurapu | 089262d | 2010-03-10 14:19:21 -0800 | [diff] [blame] | 27 | |
Christopher Tate | f1977b4 | 2014-03-24 16:25:51 -0700 | [diff] [blame] | 28 | import android.app.PackageInstallObserver; |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 29 | import android.content.BroadcastReceiver; |
| 30 | import android.content.Context; |
| 31 | import android.content.Intent; |
| 32 | import android.content.IntentFilter; |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 33 | import android.content.pm.PackageManager.NameNotFoundException; |
Jeff Sharkey | c4858a2 | 2014-06-16 10:51:20 -0700 | [diff] [blame] | 34 | import android.content.pm.PackageParser.PackageParserException; |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 35 | import android.content.res.Resources; |
| 36 | import android.content.res.Resources.NotFoundException; |
Brett Chabot | a3de745 | 2010-03-25 13:49:26 -0700 | [diff] [blame] | 37 | import android.net.Uri; |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 38 | import android.os.Binder; |
Calin Juravle | 3d2af7f | 2017-04-19 19:56:21 -0700 | [diff] [blame] | 39 | import android.os.Build; |
Christopher Tate | f1977b4 | 2014-03-24 16:25:51 -0700 | [diff] [blame] | 40 | import android.os.Bundle; |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 41 | import android.os.Environment; |
Brett Chabot | a3de745 | 2010-03-25 13:49:26 -0700 | [diff] [blame] | 42 | import android.os.FileUtils; |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 43 | import android.os.IBinder; |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 44 | import android.os.Process; |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 45 | import android.os.RemoteException; |
| 46 | import android.os.ServiceManager; |
| 47 | import android.os.StatFs; |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 48 | import android.os.SystemClock; |
Kenny Root | e15bdc2 | 2012-09-16 15:45:38 -0700 | [diff] [blame] | 49 | import android.os.UserManager; |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 50 | import android.os.storage.IStorageManager; |
Brett Chabot | a3de745 | 2010-03-25 13:49:26 -0700 | [diff] [blame] | 51 | import android.os.storage.StorageListener; |
| 52 | import android.os.storage.StorageManager; |
| 53 | import android.os.storage.StorageResultCode; |
Suchi Amalapurapu | 117818e | 2010-02-09 03:45:40 -0800 | [diff] [blame] | 54 | import android.provider.Settings; |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 55 | import android.provider.Settings.SettingNotFoundException; |
Elliott Hughes | 34385d3 | 2014-04-28 11:11:32 -0700 | [diff] [blame] | 56 | import android.system.ErrnoException; |
Elliott Hughes | 9790190 | 2014-04-28 12:40:12 -0700 | [diff] [blame] | 57 | import android.system.Os; |
Elliott Hughes | 34385d3 | 2014-04-28 11:11:32 -0700 | [diff] [blame] | 58 | import android.system.StructStat; |
Brett Chabot | a3de745 | 2010-03-25 13:49:26 -0700 | [diff] [blame] | 59 | import android.test.AndroidTestCase; |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 60 | import android.test.suitebuilder.annotation.LargeTest; |
Kenny Root | 0aaa0d9 | 2011-09-12 16:42:55 -0700 | [diff] [blame] | 61 | import android.test.suitebuilder.annotation.SmallTest; |
Abodunrinwa Toki | f901776 | 2015-07-31 18:13:30 -0700 | [diff] [blame] | 62 | import android.test.suitebuilder.annotation.Suppress; |
Brett Chabot | a3de745 | 2010-03-25 13:49:26 -0700 | [diff] [blame] | 63 | import android.util.Log; |
| 64 | |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 65 | import com.android.frameworks.coretests.R; |
| 66 | import com.android.internal.content.PackageHelper; |
| 67 | |
Calin Juravle | 3d2af7f | 2017-04-19 19:56:21 -0700 | [diff] [blame] | 68 | import dalvik.system.VMRuntime; |
| 69 | |
Brett Chabot | a3de745 | 2010-03-25 13:49:26 -0700 | [diff] [blame] | 70 | import java.io.File; |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 71 | import java.io.IOException; |
Brett Chabot | a3de745 | 2010-03-25 13:49:26 -0700 | [diff] [blame] | 72 | import java.io.InputStream; |
Calin Juravle | 3d2af7f | 2017-04-19 19:56:21 -0700 | [diff] [blame] | 73 | import java.nio.file.Files; |
| 74 | import java.nio.file.Paths; |
| 75 | import java.nio.file.StandardCopyOption; |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 76 | import java.util.HashSet; |
Martin Wallgren | f2f1b6c | 2011-05-26 15:03:52 +0200 | [diff] [blame] | 77 | import java.util.List; |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 78 | import java.util.Set; |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 79 | import java.util.concurrent.CountDownLatch; |
| 80 | import java.util.concurrent.TimeUnit; |
| 81 | |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 82 | public class PackageManagerTests extends AndroidTestCase { |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 83 | private static final boolean localLOGV = true; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 84 | |
| 85 | public static final String TAG = "PackageManagerTests"; |
| 86 | |
Calin Juravle | 3d2af7f | 2017-04-19 19:56:21 -0700 | [diff] [blame] | 87 | public static final long MAX_WAIT_TIME = 25 * 1000; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 88 | |
Calin Juravle | 3d2af7f | 2017-04-19 19:56:21 -0700 | [diff] [blame] | 89 | public static final long WAIT_TIME_INCR = 5 * 1000; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 90 | |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 91 | private static final String SECURE_CONTAINERS_PREFIX = "/mnt/asec"; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 92 | |
Suchi Amalapurapu | 089262d | 2010-03-10 14:19:21 -0800 | [diff] [blame] | 93 | private static final int APP_INSTALL_AUTO = PackageHelper.APP_INSTALL_AUTO; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 94 | |
Suchi Amalapurapu | 089262d | 2010-03-10 14:19:21 -0800 | [diff] [blame] | 95 | private static final int APP_INSTALL_DEVICE = PackageHelper.APP_INSTALL_INTERNAL; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 96 | |
Suchi Amalapurapu | 089262d | 2010-03-10 14:19:21 -0800 | [diff] [blame] | 97 | private static final int APP_INSTALL_SDCARD = PackageHelper.APP_INSTALL_EXTERNAL; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 98 | |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 99 | private boolean mOrigState; |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 100 | |
| 101 | void failStr(String errMsg) { |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 102 | Log.w(TAG, "errMsg=" + errMsg); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 103 | fail(errMsg); |
| 104 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 105 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 106 | void failStr(Exception e) { |
Suchi Amalapurapu | 90d8ee6 | 2010-03-18 11:38:35 -0700 | [diff] [blame] | 107 | failStr(e.getMessage()); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 108 | } |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 109 | |
| 110 | @Override |
| 111 | protected void setUp() throws Exception { |
| 112 | super.setUp(); |
Kenny Root | d7b421b | 2010-08-05 08:40:00 -0700 | [diff] [blame] | 113 | mOrigState = checkMediaState(Environment.MEDIA_MOUNTED); |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 114 | if (!mountMedia()) { |
| 115 | Log.i(TAG, "sdcard not mounted? Some of these tests might fail"); |
| 116 | } |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | protected void tearDown() throws Exception { |
| 121 | // Restore media state. |
Kenny Root | d7b421b | 2010-08-05 08:40:00 -0700 | [diff] [blame] | 122 | boolean newState = checkMediaState(Environment.MEDIA_MOUNTED); |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 123 | if (newState != mOrigState) { |
| 124 | if (mOrigState) { |
Kenny Root | d7b421b | 2010-08-05 08:40:00 -0700 | [diff] [blame] | 125 | mountMedia(); |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 126 | } else { |
Kenny Root | d7b421b | 2010-08-05 08:40:00 -0700 | [diff] [blame] | 127 | unmountMedia(); |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | super.tearDown(); |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Christopher Tate | f1977b4 | 2014-03-24 16:25:51 -0700 | [diff] [blame] | 133 | private class TestInstallObserver extends PackageInstallObserver { |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 134 | public int returnCode; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 135 | |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 136 | private boolean doneFlag = false; |
| 137 | |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 138 | @Override |
| 139 | public void onPackageInstalled(String basePackageName, int returnCode, String msg, |
| 140 | Bundle extras) { |
| 141 | Log.d(TAG, "onPackageInstalled: code=" + returnCode + ", msg=" + msg + ", extras=" |
| 142 | + extras); |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 143 | synchronized (this) { |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 144 | this.returnCode = returnCode; |
| 145 | doneFlag = true; |
| 146 | notifyAll(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | public boolean isDone() { |
| 151 | return doneFlag; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | abstract class GenericReceiver extends BroadcastReceiver { |
| 156 | private boolean doneFlag = false; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 157 | |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 158 | boolean received = false; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 159 | |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 160 | Intent intent; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 161 | |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 162 | IntentFilter filter; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 163 | |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 164 | abstract boolean notifyNow(Intent intent); |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 165 | |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 166 | @Override |
| 167 | public void onReceive(Context context, Intent intent) { |
| 168 | if (notifyNow(intent)) { |
| 169 | synchronized (this) { |
| 170 | received = true; |
| 171 | doneFlag = true; |
| 172 | this.intent = intent; |
| 173 | notifyAll(); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | public boolean isDone() { |
| 179 | return doneFlag; |
| 180 | } |
| 181 | |
| 182 | public void setFilter(IntentFilter filter) { |
| 183 | this.filter = filter; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | class InstallReceiver extends GenericReceiver { |
| 188 | String pkgName; |
| 189 | |
| 190 | InstallReceiver(String pkgName) { |
| 191 | this.pkgName = pkgName; |
| 192 | IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); |
| 193 | filter.addDataScheme("package"); |
| 194 | super.setFilter(filter); |
| 195 | } |
| 196 | |
| 197 | public boolean notifyNow(Intent intent) { |
| 198 | String action = intent.getAction(); |
| 199 | if (!Intent.ACTION_PACKAGE_ADDED.equals(action)) { |
| 200 | return false; |
| 201 | } |
| 202 | Uri data = intent.getData(); |
| 203 | String installedPkg = data.getEncodedSchemeSpecificPart(); |
| 204 | if (pkgName.equals(installedPkg)) { |
| 205 | return true; |
| 206 | } |
| 207 | return false; |
| 208 | } |
| 209 | } |
| 210 | |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 211 | private PackageManager getPm() { |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 212 | return mContext.getPackageManager(); |
| 213 | } |
| 214 | |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 215 | private IPackageManager getIPm() { |
| 216 | IPackageManager ipm = IPackageManager.Stub.asInterface( |
| 217 | ServiceManager.getService("package")); |
| 218 | return ipm; |
| 219 | } |
| 220 | |
Kenny Root | b7c2470 | 2012-04-14 19:46:09 -0700 | [diff] [blame] | 221 | public void invokeInstallPackage(Uri packageURI, int flags, GenericReceiver receiver, |
| 222 | boolean shouldSucceed) { |
Christopher Tate | f1977b4 | 2014-03-24 16:25:51 -0700 | [diff] [blame] | 223 | TestInstallObserver observer = new TestInstallObserver(); |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 224 | mContext.registerReceiver(receiver, receiver.filter); |
| 225 | try { |
| 226 | // Wait on observer |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 227 | synchronized (observer) { |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 228 | synchronized (receiver) { |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 229 | getPm().installPackage(packageURI, observer, flags, null); |
| 230 | long waitTime = 0; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 231 | while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) { |
Kenny Root | 94e0acb | 2010-09-21 16:30:56 -0700 | [diff] [blame] | 232 | try { |
| 233 | observer.wait(WAIT_TIME_INCR); |
| 234 | waitTime += WAIT_TIME_INCR; |
| 235 | } catch (InterruptedException e) { |
| 236 | Log.i(TAG, "Interrupted during sleep", e); |
| 237 | } |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 238 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 239 | if (!observer.isDone()) { |
Kenny Root | 94e0acb | 2010-09-21 16:30:56 -0700 | [diff] [blame] | 240 | fail("Timed out waiting for packageInstalled callback"); |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 241 | } |
Kenny Root | b7c2470 | 2012-04-14 19:46:09 -0700 | [diff] [blame] | 242 | |
| 243 | if (shouldSucceed) { |
| 244 | if (observer.returnCode != PackageManager.INSTALL_SUCCEEDED) { |
| 245 | fail("Package installation should have succeeded, but got code " |
| 246 | + observer.returnCode); |
| 247 | } |
| 248 | } else { |
| 249 | if (observer.returnCode == PackageManager.INSTALL_SUCCEEDED) { |
| 250 | fail("Package installation should fail"); |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * We'll never expect get a notification since we |
| 255 | * shouldn't succeed. |
| 256 | */ |
| 257 | return; |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 258 | } |
Kenny Root | b7c2470 | 2012-04-14 19:46:09 -0700 | [diff] [blame] | 259 | |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 260 | // Verify we received the broadcast |
| 261 | waitTime = 0; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 262 | while ((!receiver.isDone()) && (waitTime < MAX_WAIT_TIME)) { |
Kenny Root | 94e0acb | 2010-09-21 16:30:56 -0700 | [diff] [blame] | 263 | try { |
| 264 | receiver.wait(WAIT_TIME_INCR); |
| 265 | waitTime += WAIT_TIME_INCR; |
| 266 | } catch (InterruptedException e) { |
| 267 | Log.i(TAG, "Interrupted during sleep", e); |
| 268 | } |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 269 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 270 | if (!receiver.isDone()) { |
Kenny Root | 94e0acb | 2010-09-21 16:30:56 -0700 | [diff] [blame] | 271 | fail("Timed out waiting for PACKAGE_ADDED notification"); |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 272 | } |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | } finally { |
| 276 | mContext.unregisterReceiver(receiver); |
| 277 | } |
| 278 | } |
| 279 | |
Kenny Root | 1683afa | 2011-01-07 14:27:50 -0800 | [diff] [blame] | 280 | public void invokeInstallPackageFail(Uri packageURI, int flags, int expectedResult) { |
Christopher Tate | f1977b4 | 2014-03-24 16:25:51 -0700 | [diff] [blame] | 281 | TestInstallObserver observer = new TestInstallObserver(); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 282 | try { |
| 283 | // Wait on observer |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 284 | synchronized (observer) { |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 285 | getPm().installPackage(packageURI, observer, flags, null); |
| 286 | long waitTime = 0; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 287 | while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) { |
Kenny Root | 94e0acb | 2010-09-21 16:30:56 -0700 | [diff] [blame] | 288 | try { |
| 289 | observer.wait(WAIT_TIME_INCR); |
| 290 | waitTime += WAIT_TIME_INCR; |
| 291 | } catch (InterruptedException e) { |
| 292 | Log.i(TAG, "Interrupted during sleep", e); |
| 293 | } |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 294 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 295 | if (!observer.isDone()) { |
Kenny Root | 94e0acb | 2010-09-21 16:30:56 -0700 | [diff] [blame] | 296 | fail("Timed out waiting for packageInstalled callback"); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 297 | } |
Kenny Root | 1683afa | 2011-01-07 14:27:50 -0800 | [diff] [blame] | 298 | assertEquals(expectedResult, observer.returnCode); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 299 | } |
| 300 | } finally { |
| 301 | } |
| 302 | } |
| 303 | |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 304 | Uri getInstallablePackage(int fileResId, File outFile) { |
| 305 | Resources res = mContext.getResources(); |
| 306 | InputStream is = null; |
| 307 | try { |
| 308 | is = res.openRawResource(fileResId); |
| 309 | } catch (NotFoundException e) { |
| 310 | failStr("Failed to load resource with id: " + fileResId); |
| 311 | } |
| 312 | FileUtils.setPermissions(outFile.getPath(), |
| 313 | FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IRWXO, |
| 314 | -1, -1); |
| 315 | assertTrue(FileUtils.copyToFile(is, outFile)); |
| 316 | FileUtils.setPermissions(outFile.getPath(), |
| 317 | FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IRWXO, |
| 318 | -1, -1); |
| 319 | return Uri.fromFile(outFile); |
| 320 | } |
| 321 | |
Jeff Sharkey | c4858a2 | 2014-06-16 10:51:20 -0700 | [diff] [blame] | 322 | private PackageParser.Package parsePackage(Uri packageURI) throws PackageParserException { |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 323 | final String archiveFilePath = packageURI.getPath(); |
Jeff Sharkey | 275e085 | 2014-06-17 18:18:49 -0700 | [diff] [blame] | 324 | PackageParser packageParser = new PackageParser(); |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 325 | File sourceFile = new File(archiveFilePath); |
Jeff Sharkey | 275e085 | 2014-06-17 18:18:49 -0700 | [diff] [blame] | 326 | PackageParser.Package pkg = packageParser.parseMonolithicPackage(sourceFile, 0); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 327 | packageParser = null; |
| 328 | return pkg; |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 329 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 330 | |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 331 | private boolean checkSd(long pkgLen) { |
| 332 | String status = Environment.getExternalStorageState(); |
| 333 | if (!status.equals(Environment.MEDIA_MOUNTED)) { |
| 334 | return false; |
| 335 | } |
| 336 | long sdSize = -1; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 337 | StatFs sdStats = new StatFs(Environment.getExternalStorageDirectory().getPath()); |
| 338 | sdSize = (long) sdStats.getAvailableBlocks() * (long) sdStats.getBlockSize(); |
Kenny Root | 94e0acb | 2010-09-21 16:30:56 -0700 | [diff] [blame] | 339 | // TODO check for thresholds here |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 340 | return pkgLen <= sdSize; |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 341 | |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 342 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 343 | |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 344 | private boolean checkInt(long pkgLen) { |
| 345 | StatFs intStats = new StatFs(Environment.getDataDirectory().getPath()); |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 346 | long intSize = (long) intStats.getBlockCount() * (long) intStats.getBlockSize(); |
| 347 | long iSize = (long) intStats.getAvailableBlocks() * (long) intStats.getBlockSize(); |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 348 | // TODO check for thresholds here? |
| 349 | return pkgLen <= iSize; |
| 350 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 351 | |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 352 | private static final int INSTALL_LOC_INT = 1; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 353 | |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 354 | private static final int INSTALL_LOC_SD = 2; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 355 | |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 356 | private static final int INSTALL_LOC_ERR = -1; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 357 | |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 358 | private int getInstallLoc(int flags, int expInstallLocation, long pkgLen) { |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 359 | // Flags explicitly over ride everything else. |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 360 | if ((flags & PackageManager.INSTALL_EXTERNAL) != 0) { |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 361 | return INSTALL_LOC_SD; |
| 362 | } else if ((flags & PackageManager.INSTALL_INTERNAL) != 0) { |
| 363 | return INSTALL_LOC_INT; |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 364 | } |
| 365 | // Manifest option takes precedence next |
| 366 | if (expInstallLocation == PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL) { |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 367 | if (checkSd(pkgLen)) { |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 368 | return INSTALL_LOC_SD; |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 369 | } |
| 370 | if (checkInt(pkgLen)) { |
| 371 | return INSTALL_LOC_INT; |
| 372 | } |
| 373 | return INSTALL_LOC_ERR; |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 374 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 375 | if (expInstallLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY) { |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 376 | if (checkInt(pkgLen)) { |
| 377 | return INSTALL_LOC_INT; |
| 378 | } |
| 379 | return INSTALL_LOC_ERR; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 380 | } |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 381 | if (expInstallLocation == PackageInfo.INSTALL_LOCATION_AUTO) { |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 382 | // Check for free memory internally |
| 383 | if (checkInt(pkgLen)) { |
| 384 | return INSTALL_LOC_INT; |
| 385 | } |
| 386 | // Check for free memory externally |
| 387 | if (checkSd(pkgLen)) { |
| 388 | return INSTALL_LOC_SD; |
| 389 | } |
| 390 | return INSTALL_LOC_ERR; |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 391 | } |
| 392 | // Check for settings preference. |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 393 | boolean checkSd = false; |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 394 | int userPref = getDefaultInstallLoc(); |
| 395 | if (userPref == APP_INSTALL_DEVICE) { |
| 396 | if (checkInt(pkgLen)) { |
| 397 | return INSTALL_LOC_INT; |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 398 | } |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 399 | return INSTALL_LOC_ERR; |
| 400 | } else if (userPref == APP_INSTALL_SDCARD) { |
| 401 | if (checkSd(pkgLen)) { |
| 402 | return INSTALL_LOC_SD; |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 403 | } |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 404 | return INSTALL_LOC_ERR; |
Suchi Amalapurapu | 90d8ee6 | 2010-03-18 11:38:35 -0700 | [diff] [blame] | 405 | } |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 406 | // Default system policy for apps with no manifest option specified. |
| 407 | // Check for free memory internally |
| 408 | if (checkInt(pkgLen)) { |
| 409 | return INSTALL_LOC_INT; |
| 410 | } |
| 411 | return INSTALL_LOC_ERR; |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 412 | } |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 413 | |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 414 | private void assertInstall(PackageParser.Package pkg, int flags, int expInstallLocation) { |
| 415 | try { |
| 416 | String pkgName = pkg.packageName; |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 417 | ApplicationInfo info = getPm().getApplicationInfo(pkgName, 0); |
| 418 | assertNotNull(info); |
| 419 | assertEquals(pkgName, info.packageName); |
| 420 | File dataDir = Environment.getDataDirectory(); |
| 421 | String appInstallPath = new File(dataDir, "app").getPath(); |
| 422 | String drmInstallPath = new File(dataDir, "app-private").getPath(); |
| 423 | File srcDir = new File(info.sourceDir); |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 424 | String srcPath = srcDir.getParentFile().getParent(); |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 425 | File publicSrcDir = new File(info.publicSourceDir); |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 426 | String publicSrcPath = publicSrcDir.getParentFile().getParent(); |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 427 | long pkgLen = new File(info.sourceDir).length(); |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 428 | String expectedLibPath = new File(new File(info.sourceDir).getParentFile(), "lib") |
| 429 | .getPath(); |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 430 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 431 | int rLoc = getInstallLoc(flags, expInstallLocation, pkgLen); |
| 432 | if (rLoc == INSTALL_LOC_INT) { |
| 433 | if ((flags & PackageManager.INSTALL_FORWARD_LOCK) != 0) { |
| 434 | assertTrue("The application should be installed forward locked", |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 435 | (info.privateFlags & ApplicationInfo.PRIVATE_FLAG_FORWARD_LOCK) != 0); |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 436 | assertStartsWith("The APK path should point to the ASEC", |
| 437 | SECURE_CONTAINERS_PREFIX, srcPath); |
| 438 | assertStartsWith("The public APK path should point to the ASEC", |
| 439 | SECURE_CONTAINERS_PREFIX, publicSrcPath); |
| 440 | assertStartsWith("The native library path should point to the ASEC", |
| 441 | SECURE_CONTAINERS_PREFIX, info.nativeLibraryDir); |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 442 | } else { |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 443 | assertFalse( |
| 444 | (info.privateFlags & ApplicationInfo.PRIVATE_FLAG_FORWARD_LOCK) != 0); |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 445 | assertEquals(appInstallPath, srcPath); |
| 446 | assertEquals(appInstallPath, publicSrcPath); |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 447 | assertStartsWith("Native library should point to shared lib directory", |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 448 | expectedLibPath, info.nativeLibraryDir); |
| 449 | assertDirOwnerGroupPermsIfExists( |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 450 | "Native library directory should be owned by system:system and 0755", |
| 451 | Process.SYSTEM_UID, Process.SYSTEM_UID, |
| 452 | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH, |
| 453 | info.nativeLibraryDir); |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 454 | } |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 455 | assertFalse((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0); |
| 456 | |
| 457 | // Make sure the native library dir is not a symlink |
| 458 | final File nativeLibDir = new File(info.nativeLibraryDir); |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 459 | if (nativeLibDir.exists()) { |
| 460 | try { |
| 461 | assertEquals("Native library dir should not be a symlink", |
| 462 | info.nativeLibraryDir, nativeLibDir.getCanonicalPath()); |
| 463 | } catch (IOException e) { |
| 464 | fail("Can't read " + nativeLibDir.getPath()); |
| 465 | } |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 466 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 467 | } else if (rLoc == INSTALL_LOC_SD) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 468 | if ((flags & PackageManager.INSTALL_FORWARD_LOCK) != 0) { |
| 469 | assertTrue("The application should be installed forward locked", |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 470 | (info.privateFlags & ApplicationInfo.PRIVATE_FLAG_FORWARD_LOCK) != 0); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 471 | } else { |
| 472 | assertFalse("The application should not be installed forward locked", |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 473 | (info.privateFlags & ApplicationInfo.PRIVATE_FLAG_FORWARD_LOCK) != 0); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 474 | } |
| 475 | assertTrue("Application flags (" + info.flags |
| 476 | + ") should contain FLAG_EXTERNAL_STORAGE", |
| 477 | (info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0); |
| 478 | // Might need to check: |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 479 | // ((info.privateFlags & ApplicationInfo.PRIVATE_FLAG_FORWARD_LOCK) != 0) |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 480 | assertStartsWith("The APK path should point to the ASEC", |
| 481 | SECURE_CONTAINERS_PREFIX, srcPath); |
| 482 | assertStartsWith("The public APK path should point to the ASEC", |
| 483 | SECURE_CONTAINERS_PREFIX, publicSrcPath); |
| 484 | assertStartsWith("The native library path should point to the ASEC", |
| 485 | SECURE_CONTAINERS_PREFIX, info.nativeLibraryDir); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 486 | |
| 487 | // Make sure the native library in /data/data/<app>/lib is a |
| 488 | // symlink to the ASEC |
| 489 | final File nativeLibSymLink = new File(info.dataDir, "lib"); |
| 490 | assertTrue("Native library symlink should exist at " + nativeLibSymLink.getPath(), |
| 491 | nativeLibSymLink.exists()); |
| 492 | try { |
| 493 | assertEquals(nativeLibSymLink.getPath() + " should be a symlink to " |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 494 | + info.nativeLibraryDir, info.nativeLibraryDir, |
| 495 | nativeLibSymLink.getCanonicalPath()); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 496 | } catch (IOException e) { |
| 497 | fail("Can't read " + nativeLibSymLink.getPath()); |
| 498 | } |
| 499 | } else { |
| 500 | // TODO handle error. Install should have failed. |
| 501 | fail("Install should have failed"); |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 502 | } |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 503 | } catch (NameNotFoundException e) { |
| 504 | failStr("failed with exception : " + e); |
| 505 | } |
| 506 | } |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 507 | |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 508 | private void assertDirOwnerGroupPermsIfExists(String reason, int uid, int gid, int perms, |
| 509 | String path) { |
| 510 | if (!new File(path).exists()) { |
| 511 | return; |
| 512 | } |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 513 | |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 514 | final StructStat stat; |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 515 | try { |
Elliott Hughes | 34385d3 | 2014-04-28 11:11:32 -0700 | [diff] [blame] | 516 | stat = Os.lstat(path); |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 517 | } catch (ErrnoException e) { |
| 518 | throw new AssertionError(reason + "\n" + "Got: " + path + " does not exist"); |
| 519 | } |
| 520 | |
| 521 | StringBuilder sb = new StringBuilder(); |
| 522 | |
| 523 | if (!S_ISDIR(stat.st_mode)) { |
| 524 | sb.append("\nExpected type: "); |
| 525 | sb.append(S_IFDIR); |
| 526 | sb.append("\ngot type: "); |
| 527 | sb.append((stat.st_mode & S_IFMT)); |
| 528 | } |
| 529 | |
| 530 | if (stat.st_uid != uid) { |
| 531 | sb.append("\nExpected owner: "); |
| 532 | sb.append(uid); |
| 533 | sb.append("\nGot owner: "); |
| 534 | sb.append(stat.st_uid); |
| 535 | } |
| 536 | |
| 537 | if (stat.st_gid != gid) { |
| 538 | sb.append("\nExpected group: "); |
| 539 | sb.append(gid); |
| 540 | sb.append("\nGot group: "); |
| 541 | sb.append(stat.st_gid); |
| 542 | } |
| 543 | |
| 544 | if ((stat.st_mode & ~S_IFMT) != perms) { |
| 545 | sb.append("\nExpected permissions: "); |
| 546 | sb.append(Integer.toOctalString(perms)); |
| 547 | sb.append("\nGot permissions: "); |
| 548 | sb.append(Integer.toOctalString(stat.st_mode & ~S_IFMT)); |
| 549 | } |
| 550 | |
| 551 | if (sb.length() > 0) { |
| 552 | throw new AssertionError(reason + sb.toString()); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | private static void assertStartsWith(String prefix, String actual) { |
| 557 | assertStartsWith("", prefix, actual); |
| 558 | } |
| 559 | |
| 560 | private static void assertStartsWith(String description, String prefix, String actual) { |
| 561 | if (!actual.startsWith(prefix)) { |
| 562 | StringBuilder sb = new StringBuilder(description); |
| 563 | sb.append("\nExpected prefix: "); |
| 564 | sb.append(prefix); |
| 565 | sb.append("\n got: "); |
| 566 | sb.append(actual); |
| 567 | sb.append('\n'); |
| 568 | throw new AssertionError(sb.toString()); |
| 569 | } |
| 570 | } |
| 571 | |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 572 | private void assertNotInstalled(String pkgName) { |
| 573 | try { |
| 574 | ApplicationInfo info = getPm().getApplicationInfo(pkgName, 0); |
| 575 | fail(pkgName + " shouldnt be installed"); |
| 576 | } catch (NameNotFoundException e) { |
| 577 | } |
| 578 | } |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 579 | |
| 580 | class InstallParams { |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 581 | Uri packageURI; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 582 | |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 583 | PackageParser.Package pkg; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 584 | |
Jeff Sharkey | c4858a2 | 2014-06-16 10:51:20 -0700 | [diff] [blame] | 585 | InstallParams(String outFileName, int rawResId) throws PackageParserException { |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 586 | this.pkg = getParsedPackage(outFileName, rawResId); |
Jeff Sharkey | c4858a2 | 2014-06-16 10:51:20 -0700 | [diff] [blame] | 587 | this.packageURI = Uri.fromFile(new File(pkg.codePath)); |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 588 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 589 | |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 590 | InstallParams(PackageParser.Package pkg) { |
Jeff Sharkey | c4858a2 | 2014-06-16 10:51:20 -0700 | [diff] [blame] | 591 | this.packageURI = Uri.fromFile(new File(pkg.codePath)); |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 592 | this.pkg = pkg; |
| 593 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 594 | |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 595 | long getApkSize() { |
Jeff Sharkey | c4858a2 | 2014-06-16 10:51:20 -0700 | [diff] [blame] | 596 | File file = new File(pkg.codePath); |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 597 | return file.length(); |
| 598 | } |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 599 | } |
| 600 | |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 601 | private InstallParams sampleInstallFromRawResource(int flags, boolean cleanUp) throws Exception { |
| 602 | return installFromRawResource("install.apk", R.raw.install, flags, cleanUp, false, -1, |
| 603 | PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 604 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 605 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 606 | static final String PERM_PACKAGE = "package"; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 607 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 608 | static final String PERM_DEFINED = "defined"; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 609 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 610 | static final String PERM_UNDEFINED = "undefined"; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 611 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 612 | static final String PERM_USED = "used"; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 613 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 614 | static final String PERM_NOTUSED = "notused"; |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 615 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 616 | private void assertPermissions(String[] cmds) { |
| 617 | final PackageManager pm = getPm(); |
| 618 | String pkg = null; |
| 619 | PackageInfo pkgInfo = null; |
| 620 | String mode = PERM_DEFINED; |
| 621 | int i = 0; |
| 622 | while (i < cmds.length) { |
| 623 | String cmd = cmds[i++]; |
| 624 | if (cmd == PERM_PACKAGE) { |
| 625 | pkg = cmds[i++]; |
| 626 | try { |
| 627 | pkgInfo = pm.getPackageInfo(pkg, |
| 628 | PackageManager.GET_PERMISSIONS |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 629 | | PackageManager.MATCH_UNINSTALLED_PACKAGES); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 630 | } catch (NameNotFoundException e) { |
| 631 | pkgInfo = null; |
| 632 | } |
| 633 | } else if (cmd == PERM_DEFINED || cmd == PERM_UNDEFINED |
| 634 | || cmd == PERM_USED || cmd == PERM_NOTUSED) { |
| 635 | mode = cmds[i++]; |
| 636 | } else { |
| 637 | if (mode == PERM_DEFINED) { |
| 638 | try { |
| 639 | PermissionInfo pi = pm.getPermissionInfo(cmd, 0); |
| 640 | assertNotNull(pi); |
| 641 | assertEquals(pi.packageName, pkg); |
| 642 | assertEquals(pi.name, cmd); |
| 643 | assertNotNull(pkgInfo); |
| 644 | boolean found = false; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 645 | for (int j = 0; j < pkgInfo.permissions.length && !found; j++) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 646 | if (pkgInfo.permissions[j].name.equals(cmd)) { |
| 647 | found = true; |
| 648 | } |
| 649 | } |
| 650 | if (!found) { |
| 651 | fail("Permission not found: " + cmd); |
| 652 | } |
| 653 | } catch (NameNotFoundException e) { |
| 654 | throw new RuntimeException(e); |
| 655 | } |
| 656 | } else if (mode == PERM_UNDEFINED) { |
| 657 | try { |
| 658 | pm.getPermissionInfo(cmd, 0); |
| 659 | throw new RuntimeException("Permission exists: " + cmd); |
| 660 | } catch (NameNotFoundException e) { |
| 661 | } |
| 662 | if (pkgInfo != null) { |
| 663 | boolean found = false; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 664 | for (int j = 0; j < pkgInfo.permissions.length && !found; j++) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 665 | if (pkgInfo.permissions[j].name.equals(cmd)) { |
| 666 | found = true; |
| 667 | } |
| 668 | } |
| 669 | if (found) { |
| 670 | fail("Permission still exists: " + cmd); |
| 671 | } |
| 672 | } |
| 673 | } else if (mode == PERM_USED || mode == PERM_NOTUSED) { |
| 674 | boolean found = false; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 675 | for (int j = 0; j < pkgInfo.requestedPermissions.length && !found; j++) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 676 | if (pkgInfo.requestedPermissions[j].equals(cmd)) { |
| 677 | found = true; |
| 678 | } |
| 679 | } |
| 680 | if (!found) { |
| 681 | fail("Permission not requested: " + cmd); |
| 682 | } |
| 683 | if (mode == PERM_USED) { |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 684 | if (pm.checkPermission(cmd, pkg) != PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 685 | fail("Permission not granted: " + cmd); |
| 686 | } |
| 687 | } else { |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 688 | if (pm.checkPermission(cmd, pkg) != PackageManager.PERMISSION_DENIED) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 689 | fail("Permission granted: " + cmd); |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 696 | |
Jeff Sharkey | c4858a2 | 2014-06-16 10:51:20 -0700 | [diff] [blame] | 697 | private PackageParser.Package getParsedPackage(String outFileName, int rawResId) |
| 698 | throws PackageParserException { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 699 | PackageManager pm = mContext.getPackageManager(); |
| 700 | File filesDir = mContext.getFilesDir(); |
| 701 | File outFile = new File(filesDir, outFileName); |
| 702 | Uri packageURI = getInstallablePackage(rawResId, outFile); |
| 703 | PackageParser.Package pkg = parsePackage(packageURI); |
| 704 | return pkg; |
| 705 | } |
| 706 | |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 707 | /* |
| 708 | * Utility function that reads a apk bundled as a raw resource |
| 709 | * copies it into own data directory and invokes |
| 710 | * PackageManager api to install it. |
| 711 | */ |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 712 | private void installFromRawResource(InstallParams ip, int flags, boolean cleanUp, boolean fail, |
| 713 | int result, int expInstallLocation) throws Exception { |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 714 | PackageManager pm = mContext.getPackageManager(); |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 715 | PackageParser.Package pkg = ip.pkg; |
| 716 | Uri packageURI = ip.packageURI; |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 717 | if ((flags & PackageManager.INSTALL_REPLACE_EXISTING) == 0) { |
| 718 | // Make sure the package doesn't exist |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 719 | try { |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 720 | ApplicationInfo appInfo = pm.getApplicationInfo(pkg.packageName, |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 721 | PackageManager.MATCH_UNINSTALLED_PACKAGES); |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 722 | GenericReceiver receiver = new DeleteReceiver(pkg.packageName); |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 723 | invokeDeletePackage(pkg.packageName, 0, receiver); |
Kenny Root | e15bdc2 | 2012-09-16 15:45:38 -0700 | [diff] [blame] | 724 | } catch (NameNotFoundException e) { |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 725 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 726 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 727 | try { |
| 728 | if (fail) { |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 729 | invokeInstallPackageFail(packageURI, flags, result); |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 730 | if ((flags & PackageManager.INSTALL_REPLACE_EXISTING) == 0) { |
| 731 | assertNotInstalled(pkg.packageName); |
| 732 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 733 | } else { |
| 734 | InstallReceiver receiver = new InstallReceiver(pkg.packageName); |
Kenny Root | b7c2470 | 2012-04-14 19:46:09 -0700 | [diff] [blame] | 735 | invokeInstallPackage(packageURI, flags, receiver, true); |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 736 | // Verify installed information |
| 737 | assertInstall(pkg, flags, expInstallLocation); |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 738 | } |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 739 | } finally { |
| 740 | if (cleanUp) { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 741 | cleanUpInstall(ip); |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 742 | } |
| 743 | } |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | /* |
| 747 | * Utility function that reads a apk bundled as a raw resource |
| 748 | * copies it into own data directory and invokes |
| 749 | * PackageManager api to install it. |
| 750 | */ |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 751 | private InstallParams installFromRawResource(String outFileName, int rawResId, int flags, |
| 752 | boolean cleanUp, boolean fail, int result, int expInstallLocation) throws Exception { |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 753 | InstallParams ip = new InstallParams(outFileName, rawResId); |
| 754 | installFromRawResource(ip, flags, cleanUp, fail, result, expInstallLocation); |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 755 | return ip; |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 756 | } |
| 757 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 758 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 759 | public void testInstallNormalInternal() throws Exception { |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 760 | sampleInstallFromRawResource(0, true); |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 761 | } |
| 762 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 763 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 764 | public void testInstallFwdLockedInternal() throws Exception { |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 765 | sampleInstallFromRawResource(PackageManager.INSTALL_FORWARD_LOCK, true); |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 766 | } |
| 767 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 768 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 769 | public void testInstallSdcard() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 770 | // Do not run on devices with emulated external storage. |
| 771 | if (Environment.isExternalStorageEmulated()) { |
| 772 | return; |
| 773 | } |
| 774 | |
Kenny Root | d7b421b | 2010-08-05 08:40:00 -0700 | [diff] [blame] | 775 | mountMedia(); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 776 | sampleInstallFromRawResource(PackageManager.INSTALL_EXTERNAL, true); |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 777 | } |
| 778 | |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 779 | /* ------------------------- Test replacing packages -------------- */ |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 780 | class ReplaceReceiver extends GenericReceiver { |
| 781 | String pkgName; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 782 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 783 | final static int INVALID = -1; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 784 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 785 | final static int REMOVED = 1; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 786 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 787 | final static int ADDED = 2; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 788 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 789 | final static int REPLACED = 3; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 790 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 791 | int removed = INVALID; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 792 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 793 | // for updated system apps only |
| 794 | boolean update = false; |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 795 | |
| 796 | ReplaceReceiver(String pkgName) { |
| 797 | this.pkgName = pkgName; |
| 798 | filter = new IntentFilter(Intent.ACTION_PACKAGE_REMOVED); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 799 | filter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 800 | if (update) { |
| 801 | filter.addAction(Intent.ACTION_PACKAGE_REPLACED); |
| 802 | } |
| 803 | filter.addDataScheme("package"); |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 804 | super.setFilter(filter); |
| 805 | } |
| 806 | |
| 807 | public boolean notifyNow(Intent intent) { |
| 808 | String action = intent.getAction(); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 809 | Uri data = intent.getData(); |
| 810 | String installedPkg = data.getEncodedSchemeSpecificPart(); |
| 811 | if (pkgName == null || !pkgName.equals(installedPkg)) { |
| 812 | return false; |
| 813 | } |
| 814 | if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) { |
| 815 | removed = REMOVED; |
| 816 | } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) { |
| 817 | if (removed != REMOVED) { |
| 818 | return false; |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 819 | } |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 820 | boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); |
| 821 | if (!replacing) { |
| 822 | return false; |
| 823 | } |
| 824 | removed = ADDED; |
| 825 | if (!update) { |
| 826 | return true; |
| 827 | } |
| 828 | } else if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) { |
| 829 | if (removed != ADDED) { |
| 830 | return false; |
| 831 | } |
| 832 | removed = REPLACED; |
| 833 | return true; |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 834 | } |
| 835 | return false; |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | /* |
| 840 | * Utility function that reads a apk bundled as a raw resource |
| 841 | * copies it into own data directory and invokes |
| 842 | * PackageManager api to install first and then replace it |
| 843 | * again. |
| 844 | */ |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 845 | private void sampleReplaceFromRawResource(int flags) throws Exception { |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 846 | InstallParams ip = sampleInstallFromRawResource(flags, false); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 847 | boolean replace = ((flags & PackageManager.INSTALL_REPLACE_EXISTING) != 0); |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 848 | Log.i(TAG, "replace=" + replace); |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 849 | GenericReceiver receiver; |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 850 | if (replace) { |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 851 | receiver = new ReplaceReceiver(ip.pkg.packageName); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 852 | Log.i(TAG, "Creating replaceReceiver"); |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 853 | } else { |
| 854 | receiver = new InstallReceiver(ip.pkg.packageName); |
| 855 | } |
| 856 | try { |
Kenny Root | b7c2470 | 2012-04-14 19:46:09 -0700 | [diff] [blame] | 857 | invokeInstallPackage(ip.packageURI, flags, receiver, replace); |
| 858 | if (replace) { |
| 859 | assertInstall(ip.pkg, flags, ip.pkg.installLocation); |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 860 | } |
| 861 | } finally { |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 862 | cleanUpInstall(ip); |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 863 | } |
| 864 | } |
| 865 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 866 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 867 | public void testReplaceFailNormalInternal() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 868 | sampleReplaceFromRawResource(0); |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 869 | } |
| 870 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 871 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 872 | public void testReplaceFailFwdLockedInternal() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 873 | sampleReplaceFromRawResource(PackageManager.INSTALL_FORWARD_LOCK); |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 874 | } |
| 875 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 876 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 877 | public void testReplaceFailSdcard() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 878 | // Do not run on devices with emulated external storage. |
| 879 | if (Environment.isExternalStorageEmulated()) { |
| 880 | return; |
| 881 | } |
| 882 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 883 | sampleReplaceFromRawResource(PackageManager.INSTALL_EXTERNAL); |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 884 | } |
| 885 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 886 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 887 | public void testReplaceNormalInternal() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 888 | sampleReplaceFromRawResource(PackageManager.INSTALL_REPLACE_EXISTING); |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 889 | } |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 890 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 891 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 892 | public void testReplaceFwdLockedInternal() throws Exception { |
| 893 | sampleReplaceFromRawResource(PackageManager.INSTALL_REPLACE_EXISTING |
| 894 | | PackageManager.INSTALL_FORWARD_LOCK); |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 895 | } |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 896 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 897 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 898 | public void testReplaceSdcard() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 899 | // Do not run on devices with emulated external storage. |
| 900 | if (Environment.isExternalStorageEmulated()) { |
| 901 | return; |
| 902 | } |
| 903 | |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 904 | sampleReplaceFromRawResource(PackageManager.INSTALL_REPLACE_EXISTING |
| 905 | | PackageManager.INSTALL_EXTERNAL); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 906 | } |
| 907 | |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 908 | /* -------------- Delete tests --- */ |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 909 | private static class DeleteObserver extends IPackageDeleteObserver.Stub { |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 910 | private CountDownLatch mLatch = new CountDownLatch(1); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 911 | |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 912 | private int mReturnCode; |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 913 | |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 914 | private final String mPackageName; |
| 915 | |
| 916 | private String mObservedPackage; |
| 917 | |
| 918 | public DeleteObserver(String packageName) { |
| 919 | mPackageName = packageName; |
| 920 | } |
| 921 | |
| 922 | public boolean isSuccessful() { |
| 923 | return mReturnCode == PackageManager.DELETE_SUCCEEDED; |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 924 | } |
| 925 | |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 926 | public void packageDeleted(String packageName, int returnCode) throws RemoteException { |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 927 | mObservedPackage = packageName; |
| 928 | |
| 929 | mReturnCode = returnCode; |
| 930 | |
| 931 | mLatch.countDown(); |
| 932 | } |
| 933 | |
| 934 | public void waitForCompletion(long timeoutMillis) { |
| 935 | final long deadline = SystemClock.uptimeMillis() + timeoutMillis; |
| 936 | |
| 937 | long waitTime = timeoutMillis; |
| 938 | while (waitTime > 0) { |
| 939 | try { |
| 940 | boolean done = mLatch.await(waitTime, TimeUnit.MILLISECONDS); |
| 941 | if (done) { |
| 942 | assertEquals(mPackageName, mObservedPackage); |
| 943 | return; |
| 944 | } |
| 945 | } catch (InterruptedException e) { |
| 946 | // TODO Auto-generated catch block |
| 947 | e.printStackTrace(); |
| 948 | } |
| 949 | waitTime = deadline - SystemClock.uptimeMillis(); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 950 | } |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 951 | |
| 952 | throw new AssertionError("Timeout waiting for package deletion"); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 953 | } |
| 954 | } |
| 955 | |
| 956 | class DeleteReceiver extends GenericReceiver { |
| 957 | String pkgName; |
| 958 | |
| 959 | DeleteReceiver(String pkgName) { |
| 960 | this.pkgName = pkgName; |
| 961 | IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_REMOVED); |
| 962 | filter.addDataScheme("package"); |
| 963 | super.setFilter(filter); |
| 964 | } |
| 965 | |
| 966 | public boolean notifyNow(Intent intent) { |
| 967 | String action = intent.getAction(); |
| 968 | if (!Intent.ACTION_PACKAGE_REMOVED.equals(action)) { |
| 969 | return false; |
| 970 | } |
| 971 | Uri data = intent.getData(); |
| 972 | String installedPkg = data.getEncodedSchemeSpecificPart(); |
| 973 | if (pkgName.equals(installedPkg)) { |
| 974 | return true; |
| 975 | } |
| 976 | return false; |
| 977 | } |
| 978 | } |
| 979 | |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 980 | public boolean invokeDeletePackage(final String pkgName, int flags, GenericReceiver receiver) |
| 981 | throws Exception { |
| 982 | ApplicationInfo info = getPm().getApplicationInfo(pkgName, |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 983 | PackageManager.MATCH_UNINSTALLED_PACKAGES); |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 984 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 985 | mContext.registerReceiver(receiver, receiver.filter); |
| 986 | try { |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 987 | DeleteObserver observer = new DeleteObserver(pkgName); |
| 988 | |
Kenny Root | a3e9079 | 2012-10-18 10:58:36 -0700 | [diff] [blame] | 989 | getPm().deletePackage(pkgName, observer, flags | PackageManager.DELETE_ALL_USERS); |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 990 | observer.waitForCompletion(MAX_WAIT_TIME); |
| 991 | |
| 992 | assertUninstalled(info); |
| 993 | |
| 994 | // Verify we received the broadcast |
Kenny Root | a3e9079 | 2012-10-18 10:58:36 -0700 | [diff] [blame] | 995 | // TODO replace this with a CountDownLatch |
| 996 | synchronized (receiver) { |
| 997 | long waitTime = 0; |
| 998 | while ((!receiver.isDone()) && (waitTime < MAX_WAIT_TIME)) { |
| 999 | receiver.wait(WAIT_TIME_INCR); |
| 1000 | waitTime += WAIT_TIME_INCR; |
| 1001 | } |
| 1002 | if (!receiver.isDone()) { |
| 1003 | throw new Exception("Timed out waiting for PACKAGE_REMOVED notification"); |
| 1004 | } |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1005 | } |
| 1006 | return receiver.received; |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1007 | } finally { |
| 1008 | mContext.unregisterReceiver(receiver); |
| 1009 | } |
| 1010 | } |
| 1011 | |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1012 | private static void assertUninstalled(ApplicationInfo info) throws Exception { |
| 1013 | File nativeLibraryFile = new File(info.nativeLibraryDir); |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 1014 | assertFalse("Native library directory " + info.nativeLibraryDir |
| 1015 | + " should be erased", nativeLibraryFile.exists()); |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1018 | public void deleteFromRawResource(int iFlags, int dFlags) throws Exception { |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 1019 | InstallParams ip = sampleInstallFromRawResource(iFlags, false); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1020 | boolean retainData = ((dFlags & PackageManager.DELETE_KEEP_DATA) != 0); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1021 | GenericReceiver receiver = new DeleteReceiver(ip.pkg.packageName); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1022 | try { |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 1023 | assertTrue(invokeDeletePackage(ip.pkg.packageName, dFlags, receiver)); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1024 | ApplicationInfo info = null; |
| 1025 | Log.i(TAG, "okay4"); |
| 1026 | try { |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1027 | info = getPm().getApplicationInfo(ip.pkg.packageName, |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 1028 | PackageManager.MATCH_UNINSTALLED_PACKAGES); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1029 | } catch (NameNotFoundException e) { |
| 1030 | info = null; |
| 1031 | } |
| 1032 | if (retainData) { |
| 1033 | assertNotNull(info); |
| 1034 | assertEquals(info.packageName, ip.pkg.packageName); |
| 1035 | File file = new File(info.dataDir); |
| 1036 | assertTrue(file.exists()); |
| 1037 | } else { |
| 1038 | assertNull(info); |
| 1039 | } |
| 1040 | } catch (Exception e) { |
| 1041 | failStr(e); |
| 1042 | } finally { |
| 1043 | cleanUpInstall(ip); |
| 1044 | } |
| 1045 | } |
| 1046 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 1047 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1048 | public void testDeleteNormalInternal() throws Exception { |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1049 | deleteFromRawResource(0, 0); |
| 1050 | } |
| 1051 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 1052 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1053 | public void testDeleteFwdLockedInternal() throws Exception { |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1054 | deleteFromRawResource(PackageManager.INSTALL_FORWARD_LOCK, 0); |
| 1055 | } |
| 1056 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 1057 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1058 | public void testDeleteSdcard() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1059 | // Do not run on devices with emulated external storage. |
| 1060 | if (Environment.isExternalStorageEmulated()) { |
| 1061 | return; |
| 1062 | } |
| 1063 | |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 1064 | deleteFromRawResource(PackageManager.INSTALL_EXTERNAL, 0); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1065 | } |
| 1066 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 1067 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1068 | public void testDeleteNormalInternalRetainData() throws Exception { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1069 | deleteFromRawResource(0, PackageManager.DELETE_KEEP_DATA); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1070 | } |
| 1071 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 1072 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1073 | public void testDeleteFwdLockedInternalRetainData() throws Exception { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1074 | deleteFromRawResource(PackageManager.INSTALL_FORWARD_LOCK, PackageManager.DELETE_KEEP_DATA); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1075 | } |
| 1076 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 1077 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1078 | public void testDeleteSdcardRetainData() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1079 | // Do not run on devices with emulated external storage. |
| 1080 | if (Environment.isExternalStorageEmulated()) { |
| 1081 | return; |
| 1082 | } |
| 1083 | |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1084 | deleteFromRawResource(PackageManager.INSTALL_EXTERNAL, PackageManager.DELETE_KEEP_DATA); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1085 | } |
| 1086 | |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1087 | /* sdcard mount/unmount tests ***** */ |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1088 | |
| 1089 | class SdMountReceiver extends GenericReceiver { |
| 1090 | String pkgNames[]; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1091 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1092 | boolean status = true; |
| 1093 | |
| 1094 | SdMountReceiver(String[] pkgNames) { |
| 1095 | this.pkgNames = pkgNames; |
| 1096 | IntentFilter filter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); |
| 1097 | super.setFilter(filter); |
| 1098 | } |
| 1099 | |
| 1100 | public boolean notifyNow(Intent intent) { |
| 1101 | Log.i(TAG, "okay 1"); |
| 1102 | String action = intent.getAction(); |
| 1103 | if (!Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) { |
| 1104 | return false; |
| 1105 | } |
| 1106 | String rpkgList[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 1107 | for (String pkg : pkgNames) { |
| 1108 | boolean found = false; |
| 1109 | for (String rpkg : rpkgList) { |
| 1110 | if (rpkg.equals(pkg)) { |
| 1111 | found = true; |
| 1112 | break; |
| 1113 | } |
| 1114 | } |
| 1115 | if (!found) { |
| 1116 | status = false; |
| 1117 | return true; |
| 1118 | } |
| 1119 | } |
| 1120 | return true; |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | class SdUnMountReceiver extends GenericReceiver { |
| 1125 | String pkgNames[]; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1126 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1127 | boolean status = true; |
| 1128 | |
| 1129 | SdUnMountReceiver(String[] pkgNames) { |
| 1130 | this.pkgNames = pkgNames; |
| 1131 | IntentFilter filter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
| 1132 | super.setFilter(filter); |
| 1133 | } |
| 1134 | |
| 1135 | public boolean notifyNow(Intent intent) { |
| 1136 | String action = intent.getAction(); |
| 1137 | if (!Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) { |
| 1138 | return false; |
| 1139 | } |
| 1140 | String rpkgList[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 1141 | for (String pkg : pkgNames) { |
| 1142 | boolean found = false; |
| 1143 | for (String rpkg : rpkgList) { |
| 1144 | if (rpkg.equals(pkg)) { |
| 1145 | found = true; |
| 1146 | break; |
| 1147 | } |
| 1148 | } |
| 1149 | if (!found) { |
| 1150 | status = false; |
| 1151 | return true; |
| 1152 | } |
| 1153 | } |
| 1154 | return true; |
| 1155 | } |
| 1156 | } |
| 1157 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 1158 | IStorageManager getSm() { |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1159 | IBinder service = ServiceManager.getService("mount"); |
| 1160 | if (service != null) { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 1161 | return IStorageManager.Stub.asInterface(service); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1162 | } else { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 1163 | Log.e(TAG, "Can't get storagemanager service"); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1164 | } |
| 1165 | return null; |
| 1166 | } |
| 1167 | |
Kenny Root | d7b421b | 2010-08-05 08:40:00 -0700 | [diff] [blame] | 1168 | boolean checkMediaState(String desired) { |
Jeff Sharkey | 8452d94 | 2015-09-17 10:16:43 -0700 | [diff] [blame] | 1169 | String actual = Environment.getExternalStorageState(); |
| 1170 | if (desired.equals(actual)) { |
| 1171 | return true; |
| 1172 | } else { |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1173 | return false; |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | boolean mountMedia() { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1178 | // We can't mount emulated storage. |
| 1179 | if (Environment.isExternalStorageEmulated()) { |
| 1180 | return true; |
| 1181 | } |
| 1182 | |
Kenny Root | d7b421b | 2010-08-05 08:40:00 -0700 | [diff] [blame] | 1183 | if (checkMediaState(Environment.MEDIA_MOUNTED)) { |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1184 | return true; |
| 1185 | } |
Kenny Root | d7b421b | 2010-08-05 08:40:00 -0700 | [diff] [blame] | 1186 | |
| 1187 | final String path = Environment.getExternalStorageDirectory().toString(); |
| 1188 | StorageListener observer = new StorageListener(Environment.MEDIA_MOUNTED); |
| 1189 | StorageManager sm = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE); |
| 1190 | sm.registerListener(observer); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1191 | try { |
Kenny Root | d7b421b | 2010-08-05 08:40:00 -0700 | [diff] [blame] | 1192 | // Wait on observer |
| 1193 | synchronized (observer) { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 1194 | int ret = getSm().mountVolume(path); |
Kenny Root | d7b421b | 2010-08-05 08:40:00 -0700 | [diff] [blame] | 1195 | if (ret != StorageResultCode.OperationSucceeded) { |
| 1196 | throw new Exception("Could not mount the media"); |
| 1197 | } |
| 1198 | long waitTime = 0; |
| 1199 | while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) { |
| 1200 | observer.wait(WAIT_TIME_INCR); |
| 1201 | waitTime += WAIT_TIME_INCR; |
| 1202 | } |
| 1203 | if (!observer.isDone()) { |
| 1204 | throw new Exception("Timed out waiting for unmount media notification"); |
| 1205 | } |
| 1206 | return true; |
| 1207 | } |
| 1208 | } catch (Exception e) { |
| 1209 | Log.e(TAG, "Exception : " + e); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1210 | return false; |
Kenny Root | d7b421b | 2010-08-05 08:40:00 -0700 | [diff] [blame] | 1211 | } finally { |
| 1212 | sm.unregisterListener(observer); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | private boolean unmountMedia() { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1217 | // We can't unmount emulated storage. |
| 1218 | if (Environment.isExternalStorageEmulated()) { |
| 1219 | return true; |
| 1220 | } |
| 1221 | |
Kenny Root | d7b421b | 2010-08-05 08:40:00 -0700 | [diff] [blame] | 1222 | if (checkMediaState(Environment.MEDIA_UNMOUNTED)) { |
| 1223 | return true; |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1224 | } |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 1225 | |
Kenny Root | d7b421b | 2010-08-05 08:40:00 -0700 | [diff] [blame] | 1226 | final String path = Environment.getExternalStorageDirectory().getPath(); |
| 1227 | StorageListener observer = new StorageListener(Environment.MEDIA_UNMOUNTED); |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 1228 | StorageManager sm = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE); |
| 1229 | sm.registerListener(observer); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1230 | try { |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 1231 | // Wait on observer |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1232 | synchronized (observer) { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 1233 | getSm().unmountVolume(path, true, false); |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 1234 | long waitTime = 0; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1235 | while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) { |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 1236 | observer.wait(WAIT_TIME_INCR); |
| 1237 | waitTime += WAIT_TIME_INCR; |
| 1238 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1239 | if (!observer.isDone()) { |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 1240 | throw new Exception("Timed out waiting for unmount media notification"); |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 1241 | } |
| 1242 | return true; |
| 1243 | } |
| 1244 | } catch (Exception e) { |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 1245 | Log.e(TAG, "Exception : " + e); |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 1246 | return false; |
| 1247 | } finally { |
| 1248 | sm.unregisterListener(observer); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1249 | } |
| 1250 | } |
| 1251 | |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1252 | private boolean mountFromRawResource() throws Exception { |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1253 | // Install pkg on sdcard |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 1254 | InstallParams ip = sampleInstallFromRawResource(PackageManager.INSTALL_EXTERNAL, false); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1255 | if (localLOGV) Log.i(TAG, "Installed pkg on sdcard"); |
Kenny Root | d7b421b | 2010-08-05 08:40:00 -0700 | [diff] [blame] | 1256 | boolean origState = checkMediaState(Environment.MEDIA_MOUNTED); |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 1257 | boolean registeredReceiver = false; |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1258 | SdMountReceiver receiver = new SdMountReceiver(new String[]{ip.pkg.packageName}); |
| 1259 | try { |
| 1260 | if (localLOGV) Log.i(TAG, "Unmounting media"); |
| 1261 | // Unmount media |
| 1262 | assertTrue(unmountMedia()); |
| 1263 | if (localLOGV) Log.i(TAG, "Unmounted media"); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1264 | // Register receiver here |
| 1265 | PackageManager pm = getPm(); |
| 1266 | mContext.registerReceiver(receiver, receiver.filter); |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 1267 | registeredReceiver = true; |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1268 | |
| 1269 | // Wait on receiver |
| 1270 | synchronized (receiver) { |
| 1271 | if (localLOGV) Log.i(TAG, "Mounting media"); |
| 1272 | // Mount media again |
| 1273 | assertTrue(mountMedia()); |
| 1274 | if (localLOGV) Log.i(TAG, "Mounted media"); |
| 1275 | if (localLOGV) Log.i(TAG, "Waiting for notification"); |
| 1276 | long waitTime = 0; |
| 1277 | // Verify we received the broadcast |
| 1278 | waitTime = 0; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1279 | while ((!receiver.isDone()) && (waitTime < MAX_WAIT_TIME)) { |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1280 | receiver.wait(WAIT_TIME_INCR); |
| 1281 | waitTime += WAIT_TIME_INCR; |
| 1282 | } |
| 1283 | if(!receiver.isDone()) { |
| 1284 | failStr("Timed out waiting for EXTERNAL_APPLICATIONS notification"); |
| 1285 | } |
| 1286 | return receiver.received; |
| 1287 | } |
| 1288 | } catch (InterruptedException e) { |
| 1289 | failStr(e); |
| 1290 | return false; |
| 1291 | } finally { |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1292 | if (registeredReceiver) { |
| 1293 | mContext.unregisterReceiver(receiver); |
| 1294 | } |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1295 | // Restore original media state |
| 1296 | if (origState) { |
| 1297 | mountMedia(); |
| 1298 | } else { |
| 1299 | unmountMedia(); |
| 1300 | } |
| 1301 | if (localLOGV) Log.i(TAG, "Cleaning up install"); |
| 1302 | cleanUpInstall(ip); |
| 1303 | } |
| 1304 | } |
| 1305 | |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 1306 | /* |
| 1307 | * Install package on sdcard. Unmount and then mount the media. |
| 1308 | * (Use PackageManagerService private api for now) |
| 1309 | * Make sure the installed package is available. |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 1310 | */ |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 1311 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1312 | public void testMountSdNormalInternal() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1313 | // Do not run on devices with emulated external storage. |
| 1314 | if (Environment.isExternalStorageEmulated()) { |
| 1315 | return; |
| 1316 | } |
| 1317 | |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 1318 | assertTrue(mountFromRawResource()); |
| 1319 | } |
| 1320 | |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1321 | void cleanUpInstall(InstallParams ip) throws Exception { |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1322 | if (ip == null) { |
| 1323 | return; |
| 1324 | } |
| 1325 | Runtime.getRuntime().gc(); |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1326 | try { |
Todd Kennedy | cf901d7 | 2017-05-16 09:10:07 -0700 | [diff] [blame] | 1327 | cleanUpInstall(ip.pkg.packageName); |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1328 | } finally { |
Jeff Sharkey | c4858a2 | 2014-06-16 10:51:20 -0700 | [diff] [blame] | 1329 | File outFile = new File(ip.pkg.codePath); |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1330 | if (outFile != null && outFile.exists()) { |
| 1331 | outFile.delete(); |
| 1332 | } |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1333 | } |
| 1334 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1335 | |
| 1336 | private void cleanUpInstall(String pkgName) throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 1337 | if (pkgName == null) { |
| 1338 | return; |
| 1339 | } |
| 1340 | Log.i(TAG, "Deleting package : " + pkgName); |
| 1341 | try { |
Todd Kennedy | cf901d7 | 2017-05-16 09:10:07 -0700 | [diff] [blame] | 1342 | final ApplicationInfo info = getPm().getApplicationInfo(pkgName, |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 1343 | PackageManager.MATCH_UNINSTALLED_PACKAGES); |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 1344 | if (info != null) { |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1345 | DeleteObserver observer = new DeleteObserver(pkgName); |
Kenny Root | a3e9079 | 2012-10-18 10:58:36 -0700 | [diff] [blame] | 1346 | getPm().deletePackage(pkgName, observer, PackageManager.DELETE_ALL_USERS); |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1347 | observer.waitForCompletion(MAX_WAIT_TIME); |
| 1348 | assertUninstalled(info); |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 1349 | } |
Todd Kennedy | cf901d7 | 2017-05-16 09:10:07 -0700 | [diff] [blame] | 1350 | } catch (IllegalArgumentException | NameNotFoundException e) { |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1351 | } |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 1352 | } |
Suchi Amalapurapu | 117818e | 2010-02-09 03:45:40 -0800 | [diff] [blame] | 1353 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 1354 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1355 | public void testManifestInstallLocationInternal() throws Exception { |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 1356 | installFromRawResource("install.apk", R.raw.install_loc_internal, |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 1357 | 0, true, false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); |
Suchi Amalapurapu | 117818e | 2010-02-09 03:45:40 -0800 | [diff] [blame] | 1358 | } |
| 1359 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 1360 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1361 | public void testManifestInstallLocationSdcard() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1362 | // Do not run on devices with emulated external storage. |
| 1363 | if (Environment.isExternalStorageEmulated()) { |
| 1364 | return; |
| 1365 | } |
| 1366 | |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 1367 | installFromRawResource("install.apk", R.raw.install_loc_sdcard, |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 1368 | 0, true, false, -1, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); |
Suchi Amalapurapu | 117818e | 2010-02-09 03:45:40 -0800 | [diff] [blame] | 1369 | } |
| 1370 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 1371 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1372 | public void testManifestInstallLocationAuto() throws Exception { |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 1373 | installFromRawResource("install.apk", R.raw.install_loc_auto, |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 1374 | 0, true, false, -1, PackageInfo.INSTALL_LOCATION_AUTO); |
Suchi Amalapurapu | 117818e | 2010-02-09 03:45:40 -0800 | [diff] [blame] | 1375 | } |
| 1376 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 1377 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1378 | public void testManifestInstallLocationUnspecified() throws Exception { |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 1379 | installFromRawResource("install.apk", R.raw.install_loc_unspecified, |
Suchi Amalapurapu | 90d8ee6 | 2010-03-18 11:38:35 -0700 | [diff] [blame] | 1380 | 0, true, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
Suchi Amalapurapu | 117818e | 2010-02-09 03:45:40 -0800 | [diff] [blame] | 1381 | } |
| 1382 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 1383 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1384 | public void testManifestInstallLocationFwdLockedFlagSdcard() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1385 | // Do not run on devices with emulated external storage. |
| 1386 | if (Environment.isExternalStorageEmulated()) { |
| 1387 | return; |
| 1388 | } |
| 1389 | |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 1390 | installFromRawResource("install.apk", R.raw.install_loc_unspecified, |
| 1391 | PackageManager.INSTALL_FORWARD_LOCK | |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 1392 | PackageManager.INSTALL_EXTERNAL, true, false, -1, |
| 1393 | PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 1394 | } |
| 1395 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 1396 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1397 | public void testManifestInstallLocationFwdLockedSdcard() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1398 | // Do not run on devices with emulated external storage. |
| 1399 | if (Environment.isExternalStorageEmulated()) { |
| 1400 | return; |
| 1401 | } |
| 1402 | |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 1403 | installFromRawResource("install.apk", R.raw.install_loc_sdcard, |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1404 | PackageManager.INSTALL_FORWARD_LOCK, true, false, -1, |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 1405 | PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); |
| 1406 | } |
| 1407 | |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 1408 | /* |
| 1409 | * Install a package on internal flash via PackageManager install flag. Replace |
| 1410 | * the package via flag to install on sdcard. Make sure the new flag overrides |
| 1411 | * the old install location. |
| 1412 | */ |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 1413 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1414 | public void testReplaceFlagInternalSdcard() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1415 | // Do not run on devices with emulated external storage. |
| 1416 | if (Environment.isExternalStorageEmulated()) { |
| 1417 | return; |
| 1418 | } |
| 1419 | |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 1420 | int iFlags = 0; |
| 1421 | int rFlags = PackageManager.INSTALL_EXTERNAL; |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 1422 | InstallParams ip = sampleInstallFromRawResource(iFlags, false); |
| 1423 | GenericReceiver receiver = new ReplaceReceiver(ip.pkg.packageName); |
| 1424 | int replaceFlags = rFlags | PackageManager.INSTALL_REPLACE_EXISTING; |
| 1425 | try { |
Kenny Root | b7c2470 | 2012-04-14 19:46:09 -0700 | [diff] [blame] | 1426 | invokeInstallPackage(ip.packageURI, replaceFlags, receiver, true); |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 1427 | assertInstall(ip.pkg, rFlags, ip.pkg.installLocation); |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 1428 | } catch (Exception e) { |
| 1429 | failStr("Failed with exception : " + e); |
| 1430 | } finally { |
| 1431 | cleanUpInstall(ip); |
| 1432 | } |
| 1433 | } |
| 1434 | |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 1435 | /* |
| 1436 | * Install a package on sdcard via PackageManager install flag. Replace |
| 1437 | * the package with no flags or manifest option and make sure the old |
| 1438 | * install location is retained. |
| 1439 | */ |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 1440 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1441 | public void testReplaceFlagSdcardInternal() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1442 | // Do not run on devices with emulated external storage. |
| 1443 | if (Environment.isExternalStorageEmulated()) { |
| 1444 | return; |
| 1445 | } |
| 1446 | |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 1447 | int iFlags = PackageManager.INSTALL_EXTERNAL; |
| 1448 | int rFlags = 0; |
| 1449 | InstallParams ip = sampleInstallFromRawResource(iFlags, false); |
| 1450 | GenericReceiver receiver = new ReplaceReceiver(ip.pkg.packageName); |
| 1451 | int replaceFlags = rFlags | PackageManager.INSTALL_REPLACE_EXISTING; |
| 1452 | try { |
Kenny Root | b7c2470 | 2012-04-14 19:46:09 -0700 | [diff] [blame] | 1453 | invokeInstallPackage(ip.packageURI, replaceFlags, receiver, true); |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 1454 | assertInstall(ip.pkg, iFlags, ip.pkg.installLocation); |
| 1455 | } catch (Exception e) { |
| 1456 | failStr("Failed with exception : " + e); |
| 1457 | } finally { |
| 1458 | cleanUpInstall(ip); |
| 1459 | } |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 1460 | } |
| 1461 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 1462 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1463 | public void testManifestInstallLocationReplaceInternalSdcard() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1464 | // Do not run on devices with emulated external storage. |
| 1465 | if (Environment.isExternalStorageEmulated()) { |
| 1466 | return; |
| 1467 | } |
| 1468 | |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 1469 | int iFlags = 0; |
Suchi Amalapurapu | 90d8ee6 | 2010-03-18 11:38:35 -0700 | [diff] [blame] | 1470 | int iApk = R.raw.install_loc_internal; |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 1471 | int rFlags = 0; |
| 1472 | int rApk = R.raw.install_loc_sdcard; |
| 1473 | InstallParams ip = installFromRawResource("install.apk", iApk, |
| 1474 | iFlags, false, |
Suchi Amalapurapu | 90d8ee6 | 2010-03-18 11:38:35 -0700 | [diff] [blame] | 1475 | false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 1476 | GenericReceiver receiver = new ReplaceReceiver(ip.pkg.packageName); |
| 1477 | int replaceFlags = rFlags | PackageManager.INSTALL_REPLACE_EXISTING; |
| 1478 | try { |
| 1479 | InstallParams rp = installFromRawResource("install.apk", rApk, |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 1480 | replaceFlags, false, |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 1481 | false, -1, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); |
| 1482 | assertInstall(rp.pkg, replaceFlags, rp.pkg.installLocation); |
| 1483 | } catch (Exception e) { |
| 1484 | failStr("Failed with exception : " + e); |
| 1485 | } finally { |
| 1486 | cleanUpInstall(ip); |
| 1487 | } |
| 1488 | } |
| 1489 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 1490 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1491 | public void testManifestInstallLocationReplaceSdcardInternal() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1492 | // Do not run on devices with emulated external storage. |
| 1493 | if (Environment.isExternalStorageEmulated()) { |
| 1494 | return; |
| 1495 | } |
| 1496 | |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 1497 | int iFlags = 0; |
| 1498 | int iApk = R.raw.install_loc_sdcard; |
| 1499 | int rFlags = 0; |
| 1500 | int rApk = R.raw.install_loc_unspecified; |
| 1501 | InstallParams ip = installFromRawResource("install.apk", iApk, |
| 1502 | iFlags, false, |
| 1503 | false, -1, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 1504 | int replaceFlags = rFlags | PackageManager.INSTALL_REPLACE_EXISTING; |
| 1505 | try { |
| 1506 | InstallParams rp = installFromRawResource("install.apk", rApk, |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 1507 | replaceFlags, false, |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 1508 | false, -1, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); |
| 1509 | assertInstall(rp.pkg, replaceFlags, ip.pkg.installLocation); |
| 1510 | } catch (Exception e) { |
| 1511 | failStr("Failed with exception : " + e); |
| 1512 | } finally { |
| 1513 | cleanUpInstall(ip); |
| 1514 | } |
Suchi Amalapurapu | 117818e | 2010-02-09 03:45:40 -0800 | [diff] [blame] | 1515 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 1516 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 1517 | class MoveReceiver extends GenericReceiver { |
| 1518 | String pkgName; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1519 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 1520 | final static int INVALID = -1; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1521 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 1522 | final static int REMOVED = 1; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1523 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 1524 | final static int ADDED = 2; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1525 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 1526 | int removed = INVALID; |
| 1527 | |
| 1528 | MoveReceiver(String pkgName) { |
| 1529 | this.pkgName = pkgName; |
| 1530 | filter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); |
| 1531 | filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
| 1532 | super.setFilter(filter); |
| 1533 | } |
| 1534 | |
| 1535 | public boolean notifyNow(Intent intent) { |
| 1536 | String action = intent.getAction(); |
| 1537 | Log.i(TAG, "MoveReceiver::" + action); |
| 1538 | if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) { |
| 1539 | String[] list = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 1540 | if (list != null) { |
| 1541 | for (String pkg : list) { |
| 1542 | if (pkg.equals(pkgName)) { |
| 1543 | removed = REMOVED; |
| 1544 | break; |
| 1545 | } |
| 1546 | } |
| 1547 | } |
| 1548 | removed = REMOVED; |
| 1549 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) { |
| 1550 | if (removed != REMOVED) { |
| 1551 | return false; |
| 1552 | } |
| 1553 | String[] list = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 1554 | if (list != null) { |
| 1555 | for (String pkg : list) { |
| 1556 | if (pkg.equals(pkgName)) { |
| 1557 | removed = ADDED; |
| 1558 | return true; |
| 1559 | } |
| 1560 | } |
| 1561 | } |
| 1562 | } |
| 1563 | return false; |
| 1564 | } |
| 1565 | } |
| 1566 | |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1567 | public boolean invokeMovePackage(String pkgName, int flags, GenericReceiver receiver) |
| 1568 | throws Exception { |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 1569 | throw new UnsupportedOperationException(); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 1570 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1571 | |
Suchi Amalapurapu | 30f775b | 2010-04-06 11:41:23 -0700 | [diff] [blame] | 1572 | private boolean invokeMovePackageFail(String pkgName, int flags, int errCode) throws Exception { |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 1573 | throw new UnsupportedOperationException(); |
Suchi Amalapurapu | 30f775b | 2010-04-06 11:41:23 -0700 | [diff] [blame] | 1574 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 1575 | |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 1576 | private int getDefaultInstallLoc() { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 1577 | int origDefaultLoc = PackageInfo.INSTALL_LOCATION_AUTO; |
| 1578 | try { |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 1579 | origDefaultLoc = Settings.Global.getInt(mContext.getContentResolver(), |
| 1580 | Settings.Global.DEFAULT_INSTALL_LOCATION); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 1581 | } catch (SettingNotFoundException e1) { |
| 1582 | } |
| 1583 | return origDefaultLoc; |
| 1584 | } |
| 1585 | |
| 1586 | private void setInstallLoc(int loc) { |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 1587 | Settings.Global.putInt(mContext.getContentResolver(), |
| 1588 | Settings.Global.DEFAULT_INSTALL_LOCATION, loc); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 1589 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1590 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 1591 | /* |
Suchi Amalapurapu | 30f775b | 2010-04-06 11:41:23 -0700 | [diff] [blame] | 1592 | * Tests for moving apps between internal and external storage |
| 1593 | */ |
| 1594 | /* |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 1595 | * Utility function that reads a apk bundled as a raw resource |
| 1596 | * copies it into own data directory and invokes |
| 1597 | * PackageManager api to install first and then replace it |
| 1598 | * again. |
| 1599 | */ |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 1600 | |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1601 | private void moveFromRawResource(String outFileName, int rawResId, int installFlags, |
| 1602 | int moveFlags, boolean cleanUp, boolean fail, int result) throws Exception { |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 1603 | int origDefaultLoc = getDefaultInstallLoc(); |
Suchi Amalapurapu | 30f775b | 2010-04-06 11:41:23 -0700 | [diff] [blame] | 1604 | InstallParams ip = null; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 1605 | try { |
Suchi Amalapurapu | 30f775b | 2010-04-06 11:41:23 -0700 | [diff] [blame] | 1606 | setInstallLoc(PackageHelper.APP_INSTALL_AUTO); |
| 1607 | // Install first |
| 1608 | ip = installFromRawResource("install.apk", rawResId, installFlags, false, |
| 1609 | false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 1610 | ApplicationInfo oldAppInfo = getPm().getApplicationInfo(ip.pkg.packageName, 0); |
| 1611 | if (fail) { |
| 1612 | assertTrue(invokeMovePackageFail(ip.pkg.packageName, moveFlags, result)); |
| 1613 | ApplicationInfo info = getPm().getApplicationInfo(ip.pkg.packageName, 0); |
| 1614 | assertNotNull(info); |
| 1615 | assertEquals(oldAppInfo.flags, info.flags); |
| 1616 | } else { |
| 1617 | // Create receiver based on expRetCode |
| 1618 | MoveReceiver receiver = new MoveReceiver(ip.pkg.packageName); |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1619 | boolean retCode = invokeMovePackage(ip.pkg.packageName, moveFlags, receiver); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 1620 | assertTrue(retCode); |
| 1621 | ApplicationInfo info = getPm().getApplicationInfo(ip.pkg.packageName, 0); |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 1622 | assertNotNull("ApplicationInfo for recently installed application should exist", |
| 1623 | info); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 1624 | if ((moveFlags & PackageManager.MOVE_INTERNAL) != 0) { |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 1625 | assertTrue("ApplicationInfo.FLAG_EXTERNAL_STORAGE flag should NOT be set", |
| 1626 | (info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == 0); |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1627 | assertStartsWith("Native library dir should be in dataDir", |
| 1628 | info.dataDir, info.nativeLibraryDir); |
| 1629 | } else if ((moveFlags & PackageManager.MOVE_EXTERNAL_MEDIA) != 0) { |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 1630 | assertTrue("ApplicationInfo.FLAG_EXTERNAL_STORAGE flag should be set", |
| 1631 | (info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0); |
Kenny Root | 7e921a1 | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1632 | assertStartsWith("Native library dir should point to ASEC", |
| 1633 | SECURE_CONTAINERS_PREFIX, info.nativeLibraryDir); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 1634 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 1635 | } |
Suchi Amalapurapu | 30f775b | 2010-04-06 11:41:23 -0700 | [diff] [blame] | 1636 | } catch (NameNotFoundException e) { |
| 1637 | failStr("Pkg hasnt been installed correctly"); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 1638 | } finally { |
Suchi Amalapurapu | 30f775b | 2010-04-06 11:41:23 -0700 | [diff] [blame] | 1639 | if (ip != null) { |
| 1640 | cleanUpInstall(ip); |
| 1641 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 1642 | // Restore default install location |
| 1643 | setInstallLoc(origDefaultLoc); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 1644 | } |
| 1645 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1646 | |
Suchi Amalapurapu | 30f775b | 2010-04-06 11:41:23 -0700 | [diff] [blame] | 1647 | private void sampleMoveFromRawResource(int installFlags, int moveFlags, boolean fail, |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1648 | int result) throws Exception { |
Suchi Amalapurapu | 30f775b | 2010-04-06 11:41:23 -0700 | [diff] [blame] | 1649 | moveFromRawResource("install.apk", |
| 1650 | R.raw.install, installFlags, moveFlags, true, |
| 1651 | fail, result); |
| 1652 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 1653 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 1654 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1655 | public void testMoveAppInternalToExternal() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1656 | // Do not run on devices with emulated external storage. |
| 1657 | if (Environment.isExternalStorageEmulated()) { |
| 1658 | return; |
| 1659 | } |
| 1660 | |
Suchi Amalapurapu | 30f775b | 2010-04-06 11:41:23 -0700 | [diff] [blame] | 1661 | int installFlags = PackageManager.INSTALL_INTERNAL; |
| 1662 | int moveFlags = PackageManager.MOVE_EXTERNAL_MEDIA; |
| 1663 | boolean fail = false; |
| 1664 | int result = PackageManager.MOVE_SUCCEEDED; |
| 1665 | sampleMoveFromRawResource(installFlags, moveFlags, fail, result); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 1666 | } |
| 1667 | |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 1668 | @Suppress |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 1669 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1670 | public void testMoveAppInternalToInternal() throws Exception { |
Suchi Amalapurapu | 30f775b | 2010-04-06 11:41:23 -0700 | [diff] [blame] | 1671 | int installFlags = PackageManager.INSTALL_INTERNAL; |
| 1672 | int moveFlags = PackageManager.MOVE_INTERNAL; |
| 1673 | boolean fail = true; |
| 1674 | int result = PackageManager.MOVE_FAILED_INVALID_LOCATION; |
| 1675 | sampleMoveFromRawResource(installFlags, moveFlags, fail, result); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 1676 | } |
| 1677 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 1678 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1679 | public void testMoveAppExternalToExternal() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1680 | // Do not run on devices with emulated external storage. |
| 1681 | if (Environment.isExternalStorageEmulated()) { |
| 1682 | return; |
| 1683 | } |
| 1684 | |
Suchi Amalapurapu | 30f775b | 2010-04-06 11:41:23 -0700 | [diff] [blame] | 1685 | int installFlags = PackageManager.INSTALL_EXTERNAL; |
| 1686 | int moveFlags = PackageManager.MOVE_EXTERNAL_MEDIA; |
| 1687 | boolean fail = true; |
| 1688 | int result = PackageManager.MOVE_FAILED_INVALID_LOCATION; |
| 1689 | sampleMoveFromRawResource(installFlags, moveFlags, fail, result); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 1690 | } |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1691 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 1692 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1693 | public void testMoveAppExternalToInternal() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1694 | // Do not run on devices with emulated external storage. |
| 1695 | if (Environment.isExternalStorageEmulated()) { |
| 1696 | return; |
| 1697 | } |
| 1698 | |
Suchi Amalapurapu | 30f775b | 2010-04-06 11:41:23 -0700 | [diff] [blame] | 1699 | int installFlags = PackageManager.INSTALL_EXTERNAL; |
| 1700 | int moveFlags = PackageManager.MOVE_INTERNAL; |
| 1701 | boolean fail = false; |
| 1702 | int result = PackageManager.MOVE_SUCCEEDED; |
| 1703 | sampleMoveFromRawResource(installFlags, moveFlags, fail, result); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 1704 | } |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1705 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 1706 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1707 | public void testMoveAppForwardLocked() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1708 | // Do not run on devices with emulated external storage. |
| 1709 | if (Environment.isExternalStorageEmulated()) { |
| 1710 | return; |
| 1711 | } |
| 1712 | |
Suchi Amalapurapu | 30f775b | 2010-04-06 11:41:23 -0700 | [diff] [blame] | 1713 | int installFlags = PackageManager.INSTALL_FORWARD_LOCK; |
| 1714 | int moveFlags = PackageManager.MOVE_EXTERNAL_MEDIA; |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 1715 | boolean fail = false; |
| 1716 | int result = PackageManager.MOVE_SUCCEEDED; |
Suchi Amalapurapu | 30f775b | 2010-04-06 11:41:23 -0700 | [diff] [blame] | 1717 | sampleMoveFromRawResource(installFlags, moveFlags, fail, result); |
| 1718 | } |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1719 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 1720 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1721 | public void testMoveAppFailInternalToExternalDelete() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1722 | // Do not run on devices with emulated external storage. |
| 1723 | if (Environment.isExternalStorageEmulated()) { |
| 1724 | return; |
| 1725 | } |
| 1726 | |
Suchi Amalapurapu | 30f775b | 2010-04-06 11:41:23 -0700 | [diff] [blame] | 1727 | int installFlags = 0; |
| 1728 | int moveFlags = PackageManager.MOVE_EXTERNAL_MEDIA; |
| 1729 | boolean fail = true; |
| 1730 | final int result = PackageManager.MOVE_FAILED_DOESNT_EXIST; |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 1731 | |
Suchi Amalapurapu | 30f775b | 2010-04-06 11:41:23 -0700 | [diff] [blame] | 1732 | int rawResId = R.raw.install; |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 1733 | int origDefaultLoc = getDefaultInstallLoc(); |
Suchi Amalapurapu | 30f775b | 2010-04-06 11:41:23 -0700 | [diff] [blame] | 1734 | InstallParams ip = null; |
| 1735 | try { |
| 1736 | PackageManager pm = getPm(); |
| 1737 | setInstallLoc(PackageHelper.APP_INSTALL_AUTO); |
| 1738 | // Install first |
| 1739 | ip = installFromRawResource("install.apk", R.raw.install, installFlags, false, |
| 1740 | false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 1741 | // Delete the package now retaining data. |
Suchi Amalapurapu | 9a212ad | 2010-05-18 11:06:53 -0700 | [diff] [blame] | 1742 | GenericReceiver receiver = new DeleteReceiver(ip.pkg.packageName); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1743 | invokeDeletePackage(ip.pkg.packageName, PackageManager.DELETE_KEEP_DATA, receiver); |
Suchi Amalapurapu | 30f775b | 2010-04-06 11:41:23 -0700 | [diff] [blame] | 1744 | assertTrue(invokeMovePackageFail(ip.pkg.packageName, moveFlags, result)); |
| 1745 | } catch (Exception e) { |
| 1746 | failStr(e); |
| 1747 | } finally { |
| 1748 | if (ip != null) { |
| 1749 | cleanUpInstall(ip); |
| 1750 | } |
| 1751 | // Restore default install location |
| 1752 | setInstallLoc(origDefaultLoc); |
| 1753 | } |
| 1754 | } |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1755 | |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 1756 | /* |
| 1757 | * Test that an install error code is returned when media is unmounted |
| 1758 | * and package installed on sdcard via package manager flag. |
| 1759 | */ |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 1760 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1761 | public void testInstallSdcardUnmount() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1762 | // Do not run on devices with emulated external storage. |
| 1763 | if (Environment.isExternalStorageEmulated()) { |
| 1764 | return; |
| 1765 | } |
| 1766 | |
Kenny Root | d7b421b | 2010-08-05 08:40:00 -0700 | [diff] [blame] | 1767 | boolean origState = checkMediaState(Environment.MEDIA_MOUNTED); |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 1768 | try { |
| 1769 | // Unmount sdcard |
| 1770 | assertTrue(unmountMedia()); |
| 1771 | // Try to install and make sure an error code is returned. |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 1772 | installFromRawResource("install.apk", R.raw.install, |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 1773 | PackageManager.INSTALL_EXTERNAL, false, |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 1774 | true, PackageManager.INSTALL_FAILED_MEDIA_UNAVAILABLE, |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 1775 | PackageInfo.INSTALL_LOCATION_AUTO); |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 1776 | } finally { |
| 1777 | // Restore original media state |
| 1778 | if (origState) { |
| 1779 | mountMedia(); |
| 1780 | } else { |
| 1781 | unmountMedia(); |
| 1782 | } |
| 1783 | } |
| 1784 | } |
| 1785 | |
| 1786 | /* |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 1787 | * Unmount sdcard. Try installing an app with manifest option to install |
| 1788 | * on sdcard. Make sure it gets installed on internal flash. |
| 1789 | */ |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 1790 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1791 | public void testInstallManifestSdcardUnmount() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1792 | // Do not run on devices with emulated external storage. |
| 1793 | if (Environment.isExternalStorageEmulated()) { |
| 1794 | return; |
| 1795 | } |
| 1796 | |
Kenny Root | d7b421b | 2010-08-05 08:40:00 -0700 | [diff] [blame] | 1797 | boolean origState = checkMediaState(Environment.MEDIA_MOUNTED); |
| 1798 | try { |
| 1799 | // Unmount sdcard |
| 1800 | assertTrue(unmountMedia()); |
| 1801 | InstallParams ip = new InstallParams("install.apk", R.raw.install_loc_sdcard); |
| 1802 | installFromRawResource(ip, 0, true, false, -1, |
| 1803 | PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); |
| 1804 | } finally { |
| 1805 | // Restore original media state |
| 1806 | if (origState) { |
| 1807 | mountMedia(); |
| 1808 | } else { |
| 1809 | unmountMedia(); |
| 1810 | } |
| 1811 | } |
| 1812 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 1813 | |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1814 | /*---------- Recommended install location tests ----*/ |
| 1815 | /* |
| 1816 | * PrecedenceSuffixes: |
| 1817 | * Flag : FlagI, FlagE, FlagF |
| 1818 | * I - internal, E - external, F - forward locked, Flag suffix absent if not using any option. |
| 1819 | * Manifest: ManifestI, ManifestE, ManifestA, Manifest suffix absent if not using any option. |
| 1820 | * Existing: Existing suffix absent if not existing. |
| 1821 | * User: UserI, UserE, UserA, User suffix absent if not existing. |
| 1822 | * |
| 1823 | */ |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 1824 | |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1825 | /* |
| 1826 | * Install an app on internal flash |
| 1827 | */ |
| 1828 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1829 | public void testFlagI() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1830 | sampleInstallFromRawResource(PackageManager.INSTALL_INTERNAL, true); |
| 1831 | } |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 1832 | |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1833 | /* |
| 1834 | * Install an app on sdcard. |
| 1835 | */ |
| 1836 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1837 | public void testFlagE() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1838 | // Do not run on devices with emulated external storage. |
| 1839 | if (Environment.isExternalStorageEmulated()) { |
| 1840 | return; |
| 1841 | } |
| 1842 | |
| 1843 | sampleInstallFromRawResource(PackageManager.INSTALL_EXTERNAL, true); |
| 1844 | } |
| 1845 | |
| 1846 | /* |
| 1847 | * Install an app forward-locked. |
| 1848 | */ |
| 1849 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1850 | public void testFlagF() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1851 | sampleInstallFromRawResource(PackageManager.INSTALL_FORWARD_LOCK, true); |
| 1852 | } |
| 1853 | |
| 1854 | /* |
| 1855 | * Install an app with both internal and external flags set. should fail |
| 1856 | */ |
| 1857 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1858 | public void testFlagIE() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1859 | installFromRawResource("install.apk", R.raw.install, |
| 1860 | PackageManager.INSTALL_EXTERNAL | PackageManager.INSTALL_INTERNAL, |
| 1861 | false, |
| 1862 | true, PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION, |
| 1863 | PackageInfo.INSTALL_LOCATION_AUTO); |
| 1864 | } |
| 1865 | |
| 1866 | /* |
| 1867 | * Install an app with both internal and forward-lock flags set. |
| 1868 | */ |
| 1869 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1870 | public void testFlagIF() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1871 | sampleInstallFromRawResource(PackageManager.INSTALL_FORWARD_LOCK |
| 1872 | | PackageManager.INSTALL_INTERNAL, true); |
| 1873 | } |
| 1874 | |
| 1875 | /* |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 1876 | * Install an app with both external and forward-lock flags set. |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1877 | */ |
| 1878 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1879 | public void testFlagEF() throws Exception { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 1880 | // Do not run on devices with emulated external storage. |
| 1881 | if (Environment.isExternalStorageEmulated()) { |
| 1882 | return; |
| 1883 | } |
| 1884 | |
| 1885 | sampleInstallFromRawResource(PackageManager.INSTALL_FORWARD_LOCK |
| 1886 | | PackageManager.INSTALL_EXTERNAL, true); |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | /* |
| 1890 | * Install an app with both internal and external flags set with forward |
| 1891 | * lock. Should fail. |
| 1892 | */ |
| 1893 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1894 | public void testFlagIEF() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1895 | installFromRawResource("install.apk", R.raw.install, |
| 1896 | PackageManager.INSTALL_FORWARD_LOCK | PackageManager.INSTALL_INTERNAL | |
| 1897 | PackageManager.INSTALL_EXTERNAL, |
| 1898 | false, |
| 1899 | true, PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION, |
| 1900 | PackageInfo.INSTALL_LOCATION_AUTO); |
| 1901 | } |
| 1902 | |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1903 | /* |
| 1904 | * Install an app with both internal and manifest option set. |
| 1905 | * should install on internal. |
| 1906 | */ |
| 1907 | @LargeTest |
| 1908 | public void testFlagIManifestI() throws Exception { |
| 1909 | installFromRawResource("install.apk", R.raw.install_loc_internal, |
| 1910 | PackageManager.INSTALL_INTERNAL, |
| 1911 | true, |
| 1912 | false, -1, |
| 1913 | PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); |
| 1914 | } |
| 1915 | /* |
| 1916 | * Install an app with both internal and manifest preference for |
| 1917 | * preferExternal. Should install on internal. |
| 1918 | */ |
| 1919 | @LargeTest |
| 1920 | public void testFlagIManifestE() throws Exception { |
| 1921 | installFromRawResource("install.apk", R.raw.install_loc_sdcard, |
| 1922 | PackageManager.INSTALL_INTERNAL, |
| 1923 | true, |
| 1924 | false, -1, |
| 1925 | PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); |
| 1926 | } |
| 1927 | /* |
| 1928 | * Install an app with both internal and manifest preference for |
| 1929 | * auto. should install internal. |
| 1930 | */ |
| 1931 | @LargeTest |
| 1932 | public void testFlagIManifestA() throws Exception { |
| 1933 | installFromRawResource("install.apk", R.raw.install_loc_auto, |
| 1934 | PackageManager.INSTALL_INTERNAL, |
| 1935 | true, |
| 1936 | false, -1, |
| 1937 | PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); |
| 1938 | } |
| 1939 | /* |
| 1940 | * Install an app with both external and manifest option set. |
| 1941 | * should install externally. |
| 1942 | */ |
| 1943 | @LargeTest |
| 1944 | public void testFlagEManifestI() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1945 | // Do not run on devices with emulated external storage. |
| 1946 | if (Environment.isExternalStorageEmulated()) { |
| 1947 | return; |
| 1948 | } |
| 1949 | |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1950 | installFromRawResource("install.apk", R.raw.install_loc_internal, |
| 1951 | PackageManager.INSTALL_EXTERNAL, |
| 1952 | true, |
| 1953 | false, -1, |
| 1954 | PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); |
| 1955 | } |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1956 | |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1957 | /* |
| 1958 | * Install an app with both external and manifest preference for |
| 1959 | * preferExternal. Should install externally. |
| 1960 | */ |
| 1961 | @LargeTest |
| 1962 | public void testFlagEManifestE() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1963 | // Do not run on devices with emulated external storage. |
| 1964 | if (Environment.isExternalStorageEmulated()) { |
| 1965 | return; |
| 1966 | } |
| 1967 | |
| 1968 | installFromRawResource("install.apk", R.raw.install_loc_sdcard, |
| 1969 | PackageManager.INSTALL_EXTERNAL, |
| 1970 | true, |
| 1971 | false, -1, |
| 1972 | PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); |
| 1973 | } |
| 1974 | |
| 1975 | /* |
| 1976 | * Install an app with both external and manifest preference for |
| 1977 | * auto. should install on external media. |
| 1978 | */ |
| 1979 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1980 | public void testFlagEManifestA() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1981 | // Do not run on devices with emulated external storage. |
| 1982 | if (Environment.isExternalStorageEmulated()) { |
| 1983 | return; |
| 1984 | } |
| 1985 | |
| 1986 | installFromRawResource("install.apk", R.raw.install_loc_auto, |
| 1987 | PackageManager.INSTALL_EXTERNAL, |
| 1988 | true, |
| 1989 | false, -1, |
| 1990 | PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); |
| 1991 | } |
| 1992 | |
| 1993 | /* |
| 1994 | * Install an app with fwd locked flag set and install location set to |
| 1995 | * internal. should install internally. |
| 1996 | */ |
| 1997 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 1998 | public void testFlagFManifestI() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 1999 | installFromRawResource("install.apk", R.raw.install_loc_internal, |
| 2000 | PackageManager.INSTALL_FORWARD_LOCK, |
| 2001 | true, |
| 2002 | false, -1, |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 2003 | PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2004 | } |
| 2005 | |
| 2006 | /* |
| 2007 | * Install an app with fwd locked flag set and install location set to |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 2008 | * preferExternal. Should install externally. |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2009 | */ |
| 2010 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2011 | public void testFlagFManifestE() throws Exception { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 2012 | // Do not run on devices with emulated external storage. |
| 2013 | if (Environment.isExternalStorageEmulated()) { |
| 2014 | return; |
| 2015 | } |
| 2016 | |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2017 | installFromRawResource("install.apk", R.raw.install_loc_sdcard, |
| 2018 | PackageManager.INSTALL_FORWARD_LOCK, |
| 2019 | true, |
| 2020 | false, -1, |
| 2021 | PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); |
| 2022 | } |
| 2023 | |
| 2024 | /* |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 2025 | * Install an app with fwd locked flag set and install location set to auto. |
| 2026 | * should install externally. |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2027 | */ |
| 2028 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2029 | public void testFlagFManifestA() throws Exception { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 2030 | // Do not run on devices with emulated external storage. |
| 2031 | if (Environment.isExternalStorageEmulated()) { |
| 2032 | return; |
| 2033 | } |
| 2034 | |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2035 | installFromRawResource("install.apk", R.raw.install_loc_auto, |
| 2036 | PackageManager.INSTALL_FORWARD_LOCK, |
| 2037 | true, |
| 2038 | false, -1, |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 2039 | PackageInfo.INSTALL_LOCATION_AUTO); |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2040 | } |
| 2041 | |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2042 | /* |
| 2043 | * The following test functions verify install location for existing apps. |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2044 | * ie existing app can be installed internally or externally. If install |
| 2045 | * flag is explicitly set it should override current location. If manifest location |
| 2046 | * is set, that should over ride current location too. if not the existing install |
| 2047 | * location should be honoured. |
| 2048 | * testFlagI/E/F/ExistingI/E - |
| 2049 | */ |
| 2050 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2051 | public void testFlagIExistingI() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2052 | int iFlags = PackageManager.INSTALL_INTERNAL; |
| 2053 | int rFlags = PackageManager.INSTALL_INTERNAL | PackageManager.INSTALL_REPLACE_EXISTING; |
| 2054 | // First install. |
| 2055 | installFromRawResource("install.apk", R.raw.install, |
| 2056 | iFlags, |
| 2057 | false, |
| 2058 | false, -1, |
| 2059 | -1); |
| 2060 | // Replace now |
| 2061 | installFromRawResource("install.apk", R.raw.install, |
| 2062 | rFlags, |
| 2063 | true, |
| 2064 | false, -1, |
| 2065 | -1); |
| 2066 | } |
| 2067 | |
| 2068 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2069 | public void testFlagIExistingE() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2070 | // Do not run on devices with emulated external storage. |
| 2071 | if (Environment.isExternalStorageEmulated()) { |
| 2072 | return; |
| 2073 | } |
| 2074 | |
| 2075 | int iFlags = PackageManager.INSTALL_EXTERNAL; |
| 2076 | int rFlags = PackageManager.INSTALL_INTERNAL | PackageManager.INSTALL_REPLACE_EXISTING; |
| 2077 | // First install. |
| 2078 | installFromRawResource("install.apk", R.raw.install, |
| 2079 | iFlags, |
| 2080 | false, |
| 2081 | false, -1, |
| 2082 | -1); |
| 2083 | // Replace now |
| 2084 | installFromRawResource("install.apk", R.raw.install, |
| 2085 | rFlags, |
| 2086 | true, |
| 2087 | false, -1, |
| 2088 | -1); |
| 2089 | } |
| 2090 | |
| 2091 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2092 | public void testFlagEExistingI() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2093 | // Do not run on devices with emulated external storage. |
| 2094 | if (Environment.isExternalStorageEmulated()) { |
| 2095 | return; |
| 2096 | } |
| 2097 | |
| 2098 | int iFlags = PackageManager.INSTALL_INTERNAL; |
| 2099 | int rFlags = PackageManager.INSTALL_EXTERNAL | PackageManager.INSTALL_REPLACE_EXISTING; |
| 2100 | // First install. |
| 2101 | installFromRawResource("install.apk", R.raw.install, |
| 2102 | iFlags, |
| 2103 | false, |
| 2104 | false, -1, |
| 2105 | -1); |
| 2106 | // Replace now |
| 2107 | installFromRawResource("install.apk", R.raw.install, |
| 2108 | rFlags, |
| 2109 | true, |
| 2110 | false, -1, |
| 2111 | -1); |
| 2112 | } |
| 2113 | |
| 2114 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2115 | public void testFlagEExistingE() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2116 | // Do not run on devices with emulated external storage. |
| 2117 | if (Environment.isExternalStorageEmulated()) { |
| 2118 | return; |
| 2119 | } |
| 2120 | |
| 2121 | int iFlags = PackageManager.INSTALL_EXTERNAL; |
| 2122 | int rFlags = PackageManager.INSTALL_EXTERNAL | PackageManager.INSTALL_REPLACE_EXISTING; |
| 2123 | // First install. |
| 2124 | installFromRawResource("install.apk", R.raw.install, |
| 2125 | iFlags, |
| 2126 | false, |
| 2127 | false, -1, |
| 2128 | -1); |
| 2129 | // Replace now |
| 2130 | installFromRawResource("install.apk", R.raw.install, |
| 2131 | rFlags, |
| 2132 | true, |
| 2133 | false, -1, |
| 2134 | -1); |
| 2135 | } |
| 2136 | |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 2137 | @Suppress |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2138 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2139 | public void testFlagFExistingI() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2140 | int iFlags = PackageManager.INSTALL_INTERNAL; |
| 2141 | int rFlags = PackageManager.INSTALL_FORWARD_LOCK | PackageManager.INSTALL_REPLACE_EXISTING; |
| 2142 | // First install. |
| 2143 | installFromRawResource("install.apk", R.raw.install, |
| 2144 | iFlags, |
| 2145 | false, |
| 2146 | false, -1, |
| 2147 | -1); |
| 2148 | // Replace now |
| 2149 | installFromRawResource("install.apk", R.raw.install, |
| 2150 | rFlags, |
| 2151 | true, |
| 2152 | false, -1, |
| 2153 | -1); |
| 2154 | } |
| 2155 | |
| 2156 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2157 | public void testFlagFExistingE() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2158 | // Do not run on devices with emulated external storage. |
| 2159 | if (Environment.isExternalStorageEmulated()) { |
| 2160 | return; |
| 2161 | } |
| 2162 | |
| 2163 | int iFlags = PackageManager.INSTALL_EXTERNAL; |
| 2164 | int rFlags = PackageManager.INSTALL_FORWARD_LOCK | PackageManager.INSTALL_REPLACE_EXISTING; |
| 2165 | // First install. |
| 2166 | installFromRawResource("install.apk", R.raw.install, |
| 2167 | iFlags, |
| 2168 | false, |
| 2169 | false, -1, |
| 2170 | -1); |
| 2171 | // Replace now |
| 2172 | installFromRawResource("install.apk", R.raw.install, |
| 2173 | rFlags, |
| 2174 | true, |
| 2175 | false, -1, |
| 2176 | -1); |
| 2177 | } |
| 2178 | |
| 2179 | /* |
| 2180 | * The following set of tests verify the installation of apps with |
| 2181 | * install location attribute set to internalOnly, preferExternal and auto. |
| 2182 | * The manifest option should dictate the install location. |
| 2183 | * public void testManifestI/E/A |
| 2184 | * TODO out of memory fall back behaviour. |
| 2185 | */ |
| 2186 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2187 | public void testManifestI() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2188 | installFromRawResource("install.apk", R.raw.install_loc_internal, |
| 2189 | 0, |
| 2190 | true, |
| 2191 | false, -1, |
| 2192 | PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); |
| 2193 | } |
| 2194 | |
| 2195 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2196 | public void testManifestE() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2197 | // Do not run on devices with emulated external storage. |
| 2198 | if (Environment.isExternalStorageEmulated()) { |
| 2199 | return; |
| 2200 | } |
| 2201 | |
| 2202 | installFromRawResource("install.apk", R.raw.install_loc_sdcard, |
| 2203 | 0, |
| 2204 | true, |
| 2205 | false, -1, |
| 2206 | PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); |
| 2207 | } |
| 2208 | |
| 2209 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2210 | public void testManifestA() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2211 | installFromRawResource("install.apk", R.raw.install_loc_auto, |
| 2212 | 0, |
| 2213 | true, |
| 2214 | false, -1, |
| 2215 | PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); |
| 2216 | } |
| 2217 | |
| 2218 | /* |
| 2219 | * The following set of tests verify the installation of apps |
| 2220 | * with install location attribute set to internalOnly, preferExternal and auto |
| 2221 | * for already existing apps. The manifest option should take precedence. |
| 2222 | * TODO add out of memory fall back behaviour. |
| 2223 | * testManifestI/E/AExistingI/E |
| 2224 | */ |
| 2225 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2226 | public void testManifestIExistingI() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2227 | int iFlags = PackageManager.INSTALL_INTERNAL; |
| 2228 | int rFlags = PackageManager.INSTALL_REPLACE_EXISTING; |
| 2229 | // First install. |
| 2230 | installFromRawResource("install.apk", R.raw.install, |
| 2231 | iFlags, |
| 2232 | false, |
| 2233 | false, -1, |
| 2234 | -1); |
| 2235 | // Replace now |
| 2236 | installFromRawResource("install.apk", R.raw.install_loc_internal, |
| 2237 | rFlags, |
| 2238 | true, |
| 2239 | false, -1, |
| 2240 | PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); |
| 2241 | } |
| 2242 | |
| 2243 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2244 | public void testManifestIExistingE() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2245 | // Do not run on devices with emulated external storage. |
| 2246 | if (Environment.isExternalStorageEmulated()) { |
| 2247 | return; |
| 2248 | } |
| 2249 | |
| 2250 | int iFlags = PackageManager.INSTALL_EXTERNAL; |
| 2251 | int rFlags = PackageManager.INSTALL_REPLACE_EXISTING; |
| 2252 | // First install. |
| 2253 | installFromRawResource("install.apk", R.raw.install, |
| 2254 | iFlags, |
| 2255 | false, |
| 2256 | false, -1, |
| 2257 | -1); |
| 2258 | // Replace now |
| 2259 | installFromRawResource("install.apk", R.raw.install_loc_internal, |
| 2260 | rFlags, |
| 2261 | true, |
| 2262 | false, -1, |
| 2263 | PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); |
| 2264 | } |
| 2265 | |
| 2266 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2267 | public void testManifestEExistingI() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2268 | // Do not run on devices with emulated external storage. |
| 2269 | if (Environment.isExternalStorageEmulated()) { |
| 2270 | return; |
| 2271 | } |
| 2272 | |
| 2273 | int iFlags = PackageManager.INSTALL_INTERNAL; |
| 2274 | int rFlags = PackageManager.INSTALL_REPLACE_EXISTING; |
| 2275 | // First install. |
| 2276 | installFromRawResource("install.apk", R.raw.install, |
| 2277 | iFlags, |
| 2278 | false, |
| 2279 | false, -1, |
| 2280 | -1); |
| 2281 | // Replace now |
| 2282 | installFromRawResource("install.apk", R.raw.install_loc_sdcard, |
| 2283 | rFlags, |
| 2284 | true, |
| 2285 | false, -1, |
| 2286 | PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); |
| 2287 | } |
| 2288 | |
| 2289 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2290 | public void testManifestEExistingE() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2291 | // Do not run on devices with emulated external storage. |
| 2292 | if (Environment.isExternalStorageEmulated()) { |
| 2293 | return; |
| 2294 | } |
| 2295 | |
| 2296 | int iFlags = PackageManager.INSTALL_EXTERNAL; |
| 2297 | int rFlags = PackageManager.INSTALL_REPLACE_EXISTING; |
| 2298 | // First install. |
| 2299 | installFromRawResource("install.apk", R.raw.install, |
| 2300 | iFlags, |
| 2301 | false, |
| 2302 | false, -1, |
| 2303 | -1); |
| 2304 | // Replace now |
| 2305 | installFromRawResource("install.apk", R.raw.install_loc_sdcard, |
| 2306 | rFlags, |
| 2307 | true, |
| 2308 | false, -1, |
| 2309 | PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); |
| 2310 | } |
| 2311 | |
| 2312 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2313 | public void testManifestAExistingI() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2314 | int iFlags = PackageManager.INSTALL_INTERNAL; |
| 2315 | int rFlags = PackageManager.INSTALL_REPLACE_EXISTING; |
| 2316 | // First install. |
| 2317 | installFromRawResource("install.apk", R.raw.install, |
| 2318 | iFlags, |
| 2319 | false, |
| 2320 | false, -1, |
| 2321 | -1); |
| 2322 | // Replace now |
| 2323 | installFromRawResource("install.apk", R.raw.install_loc_auto, |
| 2324 | rFlags, |
| 2325 | true, |
| 2326 | false, -1, |
| 2327 | PackageInfo.INSTALL_LOCATION_AUTO); |
| 2328 | } |
| 2329 | |
| 2330 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2331 | public void testManifestAExistingE() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2332 | // Do not run on devices with emulated external storage. |
| 2333 | if (Environment.isExternalStorageEmulated()) { |
| 2334 | return; |
| 2335 | } |
| 2336 | |
| 2337 | int iFlags = PackageManager.INSTALL_EXTERNAL; |
| 2338 | int rFlags = PackageManager.INSTALL_REPLACE_EXISTING; |
| 2339 | // First install. |
| 2340 | installFromRawResource("install.apk", R.raw.install, |
| 2341 | iFlags, |
| 2342 | false, |
| 2343 | false, -1, |
| 2344 | -1); |
| 2345 | // Replace now |
| 2346 | installFromRawResource("install.apk", R.raw.install_loc_auto, |
| 2347 | rFlags, |
| 2348 | true, |
| 2349 | false, -1, |
| 2350 | PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); |
| 2351 | } |
| 2352 | |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2353 | /* |
| 2354 | * The following set of tests check install location for existing |
| 2355 | * application based on user setting. |
| 2356 | */ |
| 2357 | private int getExpectedInstallLocation(int userSetting) { |
| 2358 | int iloc = PackageInfo.INSTALL_LOCATION_UNSPECIFIED; |
| 2359 | boolean enable = getUserSettingSetInstallLocation(); |
| 2360 | if (enable) { |
| 2361 | if (userSetting == PackageHelper.APP_INSTALL_AUTO) { |
| 2362 | iloc = PackageInfo.INSTALL_LOCATION_AUTO; |
| 2363 | } else if (userSetting == PackageHelper.APP_INSTALL_EXTERNAL) { |
| 2364 | iloc = PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL; |
| 2365 | } else if (userSetting == PackageHelper.APP_INSTALL_INTERNAL) { |
| 2366 | iloc = PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY; |
| 2367 | } |
| 2368 | } |
| 2369 | return iloc; |
| 2370 | } |
| 2371 | |
| 2372 | private void setExistingXUserX(int userSetting, int iFlags, int iloc) throws Exception { |
| 2373 | int rFlags = PackageManager.INSTALL_REPLACE_EXISTING; |
| 2374 | // First install. |
| 2375 | installFromRawResource("install.apk", R.raw.install, |
| 2376 | iFlags, |
| 2377 | false, |
| 2378 | false, -1, |
| 2379 | PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 2380 | int origSetting = getDefaultInstallLoc(); |
| 2381 | try { |
| 2382 | // Set user setting |
| 2383 | setInstallLoc(userSetting); |
| 2384 | // Replace now |
| 2385 | installFromRawResource("install.apk", R.raw.install, |
| 2386 | rFlags, |
| 2387 | true, |
| 2388 | false, -1, |
| 2389 | iloc); |
| 2390 | } finally { |
| 2391 | setInstallLoc(origSetting); |
| 2392 | } |
| 2393 | } |
| 2394 | @LargeTest |
| 2395 | public void testExistingIUserI() throws Exception { |
| 2396 | int userSetting = PackageHelper.APP_INSTALL_INTERNAL; |
| 2397 | int iFlags = PackageManager.INSTALL_INTERNAL; |
| 2398 | setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); |
| 2399 | } |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2400 | |
| 2401 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2402 | public void testExistingIUserE() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2403 | // Do not run on devices with emulated external storage. |
| 2404 | if (Environment.isExternalStorageEmulated()) { |
| 2405 | return; |
| 2406 | } |
| 2407 | |
| 2408 | int userSetting = PackageHelper.APP_INSTALL_EXTERNAL; |
| 2409 | int iFlags = PackageManager.INSTALL_INTERNAL; |
| 2410 | setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); |
| 2411 | } |
| 2412 | |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2413 | @LargeTest |
| 2414 | public void testExistingIUserA() throws Exception { |
| 2415 | int userSetting = PackageHelper.APP_INSTALL_AUTO; |
| 2416 | int iFlags = PackageManager.INSTALL_INTERNAL; |
| 2417 | setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); |
| 2418 | } |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2419 | |
| 2420 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2421 | public void testExistingEUserI() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2422 | // Do not run on devices with emulated external storage. |
| 2423 | if (Environment.isExternalStorageEmulated()) { |
| 2424 | return; |
| 2425 | } |
| 2426 | |
| 2427 | int userSetting = PackageHelper.APP_INSTALL_INTERNAL; |
| 2428 | int iFlags = PackageManager.INSTALL_EXTERNAL; |
| 2429 | setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); |
| 2430 | } |
| 2431 | |
| 2432 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2433 | public void testExistingEUserE() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2434 | // Do not run on devices with emulated external storage. |
| 2435 | if (Environment.isExternalStorageEmulated()) { |
| 2436 | return; |
| 2437 | } |
| 2438 | |
| 2439 | int userSetting = PackageHelper.APP_INSTALL_EXTERNAL; |
| 2440 | int iFlags = PackageManager.INSTALL_EXTERNAL; |
| 2441 | setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); |
| 2442 | } |
| 2443 | |
| 2444 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2445 | public void testExistingEUserA() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2446 | // Do not run on devices with emulated external storage. |
| 2447 | if (Environment.isExternalStorageEmulated()) { |
| 2448 | return; |
| 2449 | } |
| 2450 | |
| 2451 | int userSetting = PackageHelper.APP_INSTALL_AUTO; |
| 2452 | int iFlags = PackageManager.INSTALL_EXTERNAL; |
| 2453 | setExistingXUserX(userSetting, iFlags, PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL); |
| 2454 | } |
| 2455 | |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2456 | /* |
| 2457 | * The following set of tests verify that the user setting defines |
| 2458 | * the install location. |
| 2459 | * |
| 2460 | */ |
| 2461 | private boolean getUserSettingSetInstallLocation() { |
| 2462 | try { |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 2463 | return Settings.Global.getInt( |
| 2464 | mContext.getContentResolver(), Settings.Global.SET_INSTALL_LOCATION) != 0; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2465 | } catch (SettingNotFoundException e1) { |
| 2466 | } |
| 2467 | return false; |
| 2468 | } |
Suchi Amalapurapu | 90d8ee6 | 2010-03-18 11:38:35 -0700 | [diff] [blame] | 2469 | |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2470 | private void setUserSettingSetInstallLocation(boolean value) { |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 2471 | Settings.Global.putInt(mContext.getContentResolver(), |
| 2472 | Settings.Global.SET_INSTALL_LOCATION, value ? 1 : 0); |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2473 | } |
| 2474 | |
| 2475 | private void setUserX(boolean enable, int userSetting, int iloc) throws Exception { |
| 2476 | boolean origUserSetting = getUserSettingSetInstallLocation(); |
| 2477 | int origSetting = getDefaultInstallLoc(); |
| 2478 | try { |
| 2479 | setUserSettingSetInstallLocation(enable); |
| 2480 | // Set user setting |
| 2481 | setInstallLoc(userSetting); |
| 2482 | // Replace now |
| 2483 | installFromRawResource("install.apk", R.raw.install, |
| 2484 | 0, |
| 2485 | true, |
| 2486 | false, -1, |
| 2487 | iloc); |
| 2488 | } finally { |
| 2489 | // Restore original setting |
| 2490 | setUserSettingSetInstallLocation(origUserSetting); |
| 2491 | setInstallLoc(origSetting); |
| 2492 | } |
| 2493 | } |
| 2494 | @LargeTest |
| 2495 | public void testUserI() throws Exception { |
| 2496 | int userSetting = PackageHelper.APP_INSTALL_INTERNAL; |
| 2497 | int iloc = getExpectedInstallLocation(userSetting); |
| 2498 | setUserX(true, userSetting, iloc); |
| 2499 | } |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2500 | |
| 2501 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2502 | public void testUserE() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2503 | // Do not run on devices with emulated external storage. |
| 2504 | if (Environment.isExternalStorageEmulated()) { |
| 2505 | return; |
| 2506 | } |
| 2507 | |
| 2508 | int userSetting = PackageHelper.APP_INSTALL_EXTERNAL; |
| 2509 | int iloc = getExpectedInstallLocation(userSetting); |
| 2510 | setUserX(true, userSetting, iloc); |
| 2511 | } |
| 2512 | |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2513 | @LargeTest |
| 2514 | public void testUserA() throws Exception { |
| 2515 | int userSetting = PackageHelper.APP_INSTALL_AUTO; |
| 2516 | int iloc = getExpectedInstallLocation(userSetting); |
| 2517 | setUserX(true, userSetting, iloc); |
| 2518 | } |
| 2519 | |
| 2520 | /* |
| 2521 | * The following set of tests turn on/off the basic |
| 2522 | * user setting for turning on install location. |
| 2523 | */ |
| 2524 | @LargeTest |
| 2525 | public void testUserPrefOffUserI() throws Exception { |
| 2526 | int userSetting = PackageHelper.APP_INSTALL_INTERNAL; |
| 2527 | int iloc = PackageInfo.INSTALL_LOCATION_UNSPECIFIED; |
| 2528 | setUserX(false, userSetting, iloc); |
| 2529 | } |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2530 | |
| 2531 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2532 | public void testUserPrefOffUserE() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2533 | // Do not run on devices with emulated external storage. |
| 2534 | if (Environment.isExternalStorageEmulated()) { |
| 2535 | return; |
| 2536 | } |
| 2537 | |
| 2538 | int userSetting = PackageHelper.APP_INSTALL_EXTERNAL; |
| 2539 | int iloc = PackageInfo.INSTALL_LOCATION_UNSPECIFIED; |
| 2540 | setUserX(false, userSetting, iloc); |
| 2541 | } |
| 2542 | |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2543 | @LargeTest |
| 2544 | public void testUserPrefOffA() throws Exception { |
| 2545 | int userSetting = PackageHelper.APP_INSTALL_AUTO; |
| 2546 | int iloc = PackageInfo.INSTALL_LOCATION_UNSPECIFIED; |
| 2547 | setUserX(false, userSetting, iloc); |
| 2548 | } |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2549 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2550 | static final String BASE_PERMISSIONS_DEFINED[] = new String[] { |
| 2551 | PERM_PACKAGE, "com.android.unit_tests.install_decl_perm", |
| 2552 | PERM_DEFINED, |
Brett Chabot | 0dc59e7 | 2010-04-01 18:21:38 -0700 | [diff] [blame] | 2553 | "com.android.frameworks.coretests.NORMAL", |
| 2554 | "com.android.frameworks.coretests.DANGEROUS", |
| 2555 | "com.android.frameworks.coretests.SIGNATURE", |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2556 | }; |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2557 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2558 | static final String BASE_PERMISSIONS_UNDEFINED[] = new String[] { |
Brett Chabot | 0dc59e7 | 2010-04-01 18:21:38 -0700 | [diff] [blame] | 2559 | PERM_PACKAGE, "com.android.frameworks.coretests.install_decl_perm", |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2560 | PERM_UNDEFINED, |
Brett Chabot | 0dc59e7 | 2010-04-01 18:21:38 -0700 | [diff] [blame] | 2561 | "com.android.frameworks.coretests.NORMAL", |
| 2562 | "com.android.frameworks.coretests.DANGEROUS", |
| 2563 | "com.android.frameworks.coretests.SIGNATURE", |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2564 | }; |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2565 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2566 | static final String BASE_PERMISSIONS_USED[] = new String[] { |
Brett Chabot | 0dc59e7 | 2010-04-01 18:21:38 -0700 | [diff] [blame] | 2567 | PERM_PACKAGE, "com.android.frameworks.coretests.install_use_perm_good", |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2568 | PERM_USED, |
Brett Chabot | 0dc59e7 | 2010-04-01 18:21:38 -0700 | [diff] [blame] | 2569 | "com.android.frameworks.coretests.NORMAL", |
| 2570 | "com.android.frameworks.coretests.DANGEROUS", |
| 2571 | "com.android.frameworks.coretests.SIGNATURE", |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2572 | }; |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2573 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2574 | static final String BASE_PERMISSIONS_NOTUSED[] = new String[] { |
Brett Chabot | 0dc59e7 | 2010-04-01 18:21:38 -0700 | [diff] [blame] | 2575 | PERM_PACKAGE, "com.android.frameworks.coretests.install_use_perm_good", |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2576 | PERM_NOTUSED, |
Brett Chabot | 0dc59e7 | 2010-04-01 18:21:38 -0700 | [diff] [blame] | 2577 | "com.android.frameworks.coretests.NORMAL", |
| 2578 | "com.android.frameworks.coretests.DANGEROUS", |
| 2579 | "com.android.frameworks.coretests.SIGNATURE", |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2580 | }; |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2581 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2582 | static final String BASE_PERMISSIONS_SIGUSED[] = new String[] { |
Brett Chabot | 0dc59e7 | 2010-04-01 18:21:38 -0700 | [diff] [blame] | 2583 | PERM_PACKAGE, "com.android.frameworks.coretests.install_use_perm_good", |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2584 | PERM_USED, |
Brett Chabot | 0dc59e7 | 2010-04-01 18:21:38 -0700 | [diff] [blame] | 2585 | "com.android.frameworks.coretests.SIGNATURE", |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2586 | PERM_NOTUSED, |
Brett Chabot | 0dc59e7 | 2010-04-01 18:21:38 -0700 | [diff] [blame] | 2587 | "com.android.frameworks.coretests.NORMAL", |
| 2588 | "com.android.frameworks.coretests.DANGEROUS", |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2589 | }; |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2590 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2591 | /* |
| 2592 | * Ensure that permissions are properly declared. |
| 2593 | */ |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2594 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2595 | public void testInstallDeclaresPermissions() throws Exception { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2596 | InstallParams ip = null; |
| 2597 | InstallParams ip2 = null; |
| 2598 | try { |
| 2599 | // **: Upon installing a package, are its declared permissions published? |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2600 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2601 | int iFlags = PackageManager.INSTALL_INTERNAL; |
| 2602 | int iApk = R.raw.install_decl_perm; |
| 2603 | ip = installFromRawResource("install.apk", iApk, |
| 2604 | iFlags, false, |
| 2605 | false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); |
| 2606 | assertInstall(ip.pkg, iFlags, ip.pkg.installLocation); |
| 2607 | assertPermissions(BASE_PERMISSIONS_DEFINED); |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2608 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2609 | // **: Upon installing package, are its permissions granted? |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2610 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2611 | int i2Flags = PackageManager.INSTALL_INTERNAL; |
| 2612 | int i2Apk = R.raw.install_use_perm_good; |
| 2613 | ip2 = installFromRawResource("install2.apk", i2Apk, |
| 2614 | i2Flags, false, |
| 2615 | false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); |
| 2616 | assertInstall(ip2.pkg, i2Flags, ip2.pkg.installLocation); |
| 2617 | assertPermissions(BASE_PERMISSIONS_USED); |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2618 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2619 | // **: Upon removing but not deleting, are permissions retained? |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2620 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2621 | GenericReceiver receiver = new DeleteReceiver(ip.pkg.packageName); |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2622 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2623 | try { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 2624 | invokeDeletePackage(ip.pkg.packageName, PackageManager.DELETE_KEEP_DATA, receiver); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2625 | } catch (Exception e) { |
| 2626 | failStr(e); |
| 2627 | } |
| 2628 | assertPermissions(BASE_PERMISSIONS_DEFINED); |
| 2629 | assertPermissions(BASE_PERMISSIONS_USED); |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2630 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2631 | // **: Upon re-installing, are permissions retained? |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2632 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2633 | ip = installFromRawResource("install.apk", iApk, |
| 2634 | iFlags | PackageManager.INSTALL_REPLACE_EXISTING, false, |
| 2635 | false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); |
| 2636 | assertInstall(ip.pkg, iFlags, ip.pkg.installLocation); |
| 2637 | assertPermissions(BASE_PERMISSIONS_DEFINED); |
| 2638 | assertPermissions(BASE_PERMISSIONS_USED); |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2639 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2640 | // **: Upon deleting package, are all permissions removed? |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2641 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2642 | try { |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 2643 | invokeDeletePackage(ip.pkg.packageName, 0, receiver); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2644 | ip = null; |
| 2645 | } catch (Exception e) { |
| 2646 | failStr(e); |
| 2647 | } |
| 2648 | assertPermissions(BASE_PERMISSIONS_UNDEFINED); |
| 2649 | assertPermissions(BASE_PERMISSIONS_NOTUSED); |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2650 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2651 | // **: Delete package using permissions; nothing to check here. |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2652 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2653 | GenericReceiver receiver2 = new DeleteReceiver(ip2.pkg.packageName); |
| 2654 | try { |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 2655 | invokeDeletePackage(ip2.pkg.packageName, 0, receiver); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2656 | ip2 = null; |
| 2657 | } catch (Exception e) { |
| 2658 | failStr(e); |
| 2659 | } |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2660 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2661 | // **: Re-install package using permissions; no permissions can be granted. |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2662 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2663 | ip2 = installFromRawResource("install2.apk", i2Apk, |
| 2664 | i2Flags, false, |
| 2665 | false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); |
| 2666 | assertInstall(ip2.pkg, i2Flags, ip2.pkg.installLocation); |
| 2667 | assertPermissions(BASE_PERMISSIONS_NOTUSED); |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2668 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2669 | // **: Upon installing declaring package, are sig permissions granted |
| 2670 | // to other apps (but not other perms)? |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2671 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2672 | ip = installFromRawResource("install.apk", iApk, |
| 2673 | iFlags, false, |
| 2674 | false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); |
| 2675 | assertInstall(ip.pkg, iFlags, ip.pkg.installLocation); |
| 2676 | assertPermissions(BASE_PERMISSIONS_DEFINED); |
| 2677 | assertPermissions(BASE_PERMISSIONS_SIGUSED); |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2678 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2679 | // **: Re-install package using permissions; are all permissions granted? |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2680 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2681 | ip2 = installFromRawResource("install2.apk", i2Apk, |
| 2682 | i2Flags | PackageManager.INSTALL_REPLACE_EXISTING, false, |
| 2683 | false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); |
| 2684 | assertInstall(ip2.pkg, i2Flags, ip2.pkg.installLocation); |
| 2685 | assertPermissions(BASE_PERMISSIONS_NOTUSED); |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2686 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2687 | // **: Upon deleting package, are all permissions removed? |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2688 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2689 | try { |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 2690 | invokeDeletePackage(ip.pkg.packageName, 0, receiver); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2691 | ip = null; |
| 2692 | } catch (Exception e) { |
| 2693 | failStr(e); |
| 2694 | } |
| 2695 | assertPermissions(BASE_PERMISSIONS_UNDEFINED); |
| 2696 | assertPermissions(BASE_PERMISSIONS_NOTUSED); |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2697 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2698 | // **: Delete package using permissions; nothing to check here. |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2699 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2700 | try { |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 2701 | invokeDeletePackage(ip2.pkg.packageName, 0, receiver); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2702 | ip2 = null; |
| 2703 | } catch (Exception e) { |
| 2704 | failStr(e); |
| 2705 | } |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2706 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 2707 | } finally { |
| 2708 | if (ip2 != null) { |
| 2709 | cleanUpInstall(ip2); |
| 2710 | } |
| 2711 | if (ip != null) { |
| 2712 | cleanUpInstall(ip); |
| 2713 | } |
| 2714 | } |
| 2715 | } |
| 2716 | |
Suchi Amalapurapu | ebb83ad | 2010-03-19 11:55:29 -0700 | [diff] [blame] | 2717 | /* |
| 2718 | * Ensure that permissions are properly declared. |
| 2719 | */ |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 2720 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2721 | public void testInstallOnSdPermissionsUnmount() throws Exception { |
Suchi Amalapurapu | ebb83ad | 2010-03-19 11:55:29 -0700 | [diff] [blame] | 2722 | InstallParams ip = null; |
Kenny Root | d7b421b | 2010-08-05 08:40:00 -0700 | [diff] [blame] | 2723 | boolean origMediaState = checkMediaState(Environment.MEDIA_MOUNTED); |
Suchi Amalapurapu | ebb83ad | 2010-03-19 11:55:29 -0700 | [diff] [blame] | 2724 | try { |
| 2725 | // **: Upon installing a package, are its declared permissions published? |
| 2726 | int iFlags = PackageManager.INSTALL_INTERNAL; |
| 2727 | int iApk = R.raw.install_decl_perm; |
| 2728 | ip = installFromRawResource("install.apk", iApk, |
| 2729 | iFlags, false, |
| 2730 | false, -1, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); |
| 2731 | assertInstall(ip.pkg, iFlags, ip.pkg.installLocation); |
| 2732 | assertPermissions(BASE_PERMISSIONS_DEFINED); |
| 2733 | // Unmount media here |
| 2734 | assertTrue(unmountMedia()); |
| 2735 | // Mount media again |
| 2736 | mountMedia(); |
| 2737 | //Check permissions now |
| 2738 | assertPermissions(BASE_PERMISSIONS_DEFINED); |
| 2739 | } finally { |
| 2740 | if (ip != null) { |
| 2741 | cleanUpInstall(ip); |
| 2742 | } |
| 2743 | } |
| 2744 | } |
Suchi Amalapurapu | c7537ee | 2010-03-24 09:27:19 -0700 | [diff] [blame] | 2745 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 2746 | /* This test creates a stale container via StorageManagerService and then installs |
Suchi Amalapurapu | c7537ee | 2010-03-24 09:27:19 -0700 | [diff] [blame] | 2747 | * a package and verifies that the stale container is cleaned up and install |
| 2748 | * is successful. |
| 2749 | * Please note that this test is very closely tied to the framework's |
| 2750 | * naming convention for secure containers. |
| 2751 | */ |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 2752 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2753 | public void testInstallSdcardStaleContainer() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2754 | // Do not run on devices with emulated external storage. |
| 2755 | if (Environment.isExternalStorageEmulated()) { |
| 2756 | return; |
| 2757 | } |
| 2758 | |
Kenny Root | d7b421b | 2010-08-05 08:40:00 -0700 | [diff] [blame] | 2759 | boolean origMediaState = checkMediaState(Environment.MEDIA_MOUNTED); |
Suchi Amalapurapu | c7537ee | 2010-03-24 09:27:19 -0700 | [diff] [blame] | 2760 | try { |
Kenny Root | d7b421b | 2010-08-05 08:40:00 -0700 | [diff] [blame] | 2761 | // Mount media first |
| 2762 | mountMedia(); |
Suchi Amalapurapu | c7537ee | 2010-03-24 09:27:19 -0700 | [diff] [blame] | 2763 | String outFileName = "install.apk"; |
| 2764 | int rawResId = R.raw.install; |
| 2765 | PackageManager pm = mContext.getPackageManager(); |
| 2766 | File filesDir = mContext.getFilesDir(); |
| 2767 | File outFile = new File(filesDir, outFileName); |
| 2768 | Uri packageURI = getInstallablePackage(rawResId, outFile); |
| 2769 | PackageParser.Package pkg = parsePackage(packageURI); |
| 2770 | assertNotNull(pkg); |
| 2771 | // Install an app on sdcard. |
| 2772 | installFromRawResource(outFileName, rawResId, |
| 2773 | PackageManager.INSTALL_EXTERNAL, false, |
| 2774 | false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 2775 | // Unmount sdcard |
| 2776 | unmountMedia(); |
| 2777 | // Delete the app on sdcard to leave a stale container on sdcard. |
| 2778 | GenericReceiver receiver = new DeleteReceiver(pkg.packageName); |
Suchi Amalapurapu | 315a5fb | 2010-04-13 14:32:16 -0700 | [diff] [blame] | 2779 | assertTrue(invokeDeletePackage(pkg.packageName, 0, receiver)); |
Suchi Amalapurapu | c7537ee | 2010-03-24 09:27:19 -0700 | [diff] [blame] | 2780 | mountMedia(); |
| 2781 | // Reinstall the app and make sure it gets installed. |
| 2782 | installFromRawResource(outFileName, rawResId, |
| 2783 | PackageManager.INSTALL_EXTERNAL, true, |
| 2784 | false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2785 | } catch (Exception e) { |
Suchi Amalapurapu | c7537ee | 2010-03-24 09:27:19 -0700 | [diff] [blame] | 2786 | failStr(e.getMessage()); |
| 2787 | } finally { |
| 2788 | if (origMediaState) { |
| 2789 | mountMedia(); |
| 2790 | } else { |
| 2791 | unmountMedia(); |
| 2792 | } |
| 2793 | |
| 2794 | } |
| 2795 | } |
Suchi Amalapurapu | 1ace5bc | 2010-05-13 12:05:53 -0700 | [diff] [blame] | 2796 | |
| 2797 | /* This test installs an application on sdcard and unmounts media. |
| 2798 | * The app is then re-installed on internal storage. The sdcard is mounted |
| 2799 | * and verified that the re-installation on internal storage takes precedence. |
| 2800 | */ |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 2801 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2802 | public void testInstallSdcardStaleContainerReinstall() throws Exception { |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2803 | // Do not run on devices with emulated external storage. |
| 2804 | if (Environment.isExternalStorageEmulated()) { |
| 2805 | return; |
| 2806 | } |
| 2807 | |
Kenny Root | d7b421b | 2010-08-05 08:40:00 -0700 | [diff] [blame] | 2808 | boolean origMediaState = checkMediaState(Environment.MEDIA_MOUNTED); |
Suchi Amalapurapu | 1ace5bc | 2010-05-13 12:05:53 -0700 | [diff] [blame] | 2809 | try { |
| 2810 | // Mount media first |
| 2811 | mountMedia(); |
| 2812 | String outFileName = "install.apk"; |
| 2813 | int rawResId = R.raw.install; |
| 2814 | PackageManager pm = mContext.getPackageManager(); |
| 2815 | File filesDir = mContext.getFilesDir(); |
| 2816 | File outFile = new File(filesDir, outFileName); |
| 2817 | Uri packageURI = getInstallablePackage(rawResId, outFile); |
| 2818 | PackageParser.Package pkg = parsePackage(packageURI); |
| 2819 | assertNotNull(pkg); |
| 2820 | // Install an app on sdcard. |
| 2821 | installFromRawResource(outFileName, rawResId, |
| 2822 | PackageManager.INSTALL_EXTERNAL, false, |
| 2823 | false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 2824 | // Unmount sdcard |
| 2825 | unmountMedia(); |
| 2826 | // Reinstall the app and make sure it gets installed on internal storage. |
| 2827 | installFromRawResource(outFileName, rawResId, |
| 2828 | PackageManager.INSTALL_REPLACE_EXISTING, false, |
| 2829 | false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 2830 | mountMedia(); |
| 2831 | // Verify that the app installed is on internal storage. |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2832 | assertInstall(pkg, 0, PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); |
Suchi Amalapurapu | 1ace5bc | 2010-05-13 12:05:53 -0700 | [diff] [blame] | 2833 | } catch (Exception e) { |
| 2834 | failStr(e.getMessage()); |
| 2835 | } finally { |
| 2836 | if (origMediaState) { |
| 2837 | mountMedia(); |
| 2838 | } else { |
| 2839 | unmountMedia(); |
| 2840 | } |
Suchi Amalapurapu | 1ace5bc | 2010-05-13 12:05:53 -0700 | [diff] [blame] | 2841 | } |
| 2842 | } |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 2843 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2844 | /* |
| 2845 | * The following series of tests are related to upgrading apps with |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 2846 | * different certificates. |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2847 | */ |
| 2848 | private int APP1_UNSIGNED = R.raw.install_app1_unsigned; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2849 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2850 | private int APP1_CERT1 = R.raw.install_app1_cert1; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2851 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2852 | private int APP1_CERT2 = R.raw.install_app1_cert2; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2853 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2854 | private int APP1_CERT1_CERT2 = R.raw.install_app1_cert1_cert2; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2855 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2856 | private int APP1_CERT3_CERT4 = R.raw.install_app1_cert3_cert4; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2857 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2858 | private int APP1_CERT3 = R.raw.install_app1_cert3; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2859 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2860 | private int APP2_UNSIGNED = R.raw.install_app2_unsigned; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2861 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2862 | private int APP2_CERT1 = R.raw.install_app2_cert1; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2863 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2864 | private int APP2_CERT2 = R.raw.install_app2_cert2; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2865 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2866 | private int APP2_CERT1_CERT2 = R.raw.install_app2_cert1_cert2; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2867 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2868 | private int APP2_CERT3 = R.raw.install_app2_cert3; |
| 2869 | |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2870 | private InstallParams replaceCerts(int apk1, int apk2, boolean cleanUp, boolean fail, |
| 2871 | int retCode) throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2872 | int rFlags = PackageManager.INSTALL_REPLACE_EXISTING; |
| 2873 | String apk1Name = "install1.apk"; |
| 2874 | String apk2Name = "install2.apk"; |
| 2875 | PackageParser.Package pkg1 = getParsedPackage(apk1Name, apk1); |
| 2876 | try { |
| 2877 | InstallParams ip = installFromRawResource(apk1Name, apk1, 0, false, |
| 2878 | false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 2879 | installFromRawResource(apk2Name, apk2, rFlags, false, |
| 2880 | fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 2881 | return ip; |
| 2882 | } catch (Exception e) { |
| 2883 | failStr(e.getMessage()); |
| 2884 | } finally { |
| 2885 | if (cleanUp) { |
| 2886 | cleanUpInstall(pkg1.packageName); |
| 2887 | } |
| 2888 | } |
| 2889 | return null; |
| 2890 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2891 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2892 | /* |
| 2893 | * Test that an app signed with two certificates can be upgraded by the |
| 2894 | * same app signed with two certificates. |
| 2895 | */ |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 2896 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2897 | public void testReplaceMatchAllCerts() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2898 | replaceCerts(APP1_CERT1_CERT2, APP1_CERT1_CERT2, true, false, -1); |
| 2899 | } |
| 2900 | |
| 2901 | /* |
| 2902 | * Test that an app signed with two certificates cannot be upgraded |
| 2903 | * by an app signed with a different certificate. |
| 2904 | */ |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 2905 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2906 | public void testReplaceMatchNoCerts1() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2907 | replaceCerts(APP1_CERT1_CERT2, APP1_CERT3, true, true, |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 2908 | PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE); |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2909 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2910 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2911 | /* |
| 2912 | * Test that an app signed with two certificates cannot be upgraded |
| 2913 | * by an app signed with a different certificate. |
| 2914 | */ |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 2915 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2916 | public void testReplaceMatchNoCerts2() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2917 | replaceCerts(APP1_CERT1_CERT2, APP1_CERT3_CERT4, true, true, |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 2918 | PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE); |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2919 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2920 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2921 | /* |
| 2922 | * Test that an app signed with two certificates cannot be upgraded by |
| 2923 | * an app signed with a subset of initial certificates. |
| 2924 | */ |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 2925 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2926 | public void testReplaceMatchSomeCerts1() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2927 | replaceCerts(APP1_CERT1_CERT2, APP1_CERT1, true, true, |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 2928 | PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE); |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2929 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2930 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2931 | /* |
| 2932 | * Test that an app signed with two certificates cannot be upgraded by |
| 2933 | * an app signed with the last certificate. |
| 2934 | */ |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 2935 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2936 | public void testReplaceMatchSomeCerts2() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2937 | replaceCerts(APP1_CERT1_CERT2, APP1_CERT2, true, true, |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 2938 | PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE); |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2939 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2940 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2941 | /* |
| 2942 | * Test that an app signed with a certificate can be upgraded by app |
| 2943 | * signed with a superset of certificates. |
| 2944 | */ |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 2945 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2946 | public void testReplaceMatchMoreCerts() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2947 | replaceCerts(APP1_CERT1, APP1_CERT1_CERT2, true, true, |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 2948 | PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE); |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2949 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2950 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2951 | /* |
| 2952 | * Test that an app signed with a certificate can be upgraded by app |
| 2953 | * signed with a superset of certificates. Then verify that the an app |
| 2954 | * signed with the original set of certs cannot upgrade the new one. |
| 2955 | */ |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 2956 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2957 | public void testReplaceMatchMoreCertsReplaceSomeCerts() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2958 | InstallParams ip = replaceCerts(APP1_CERT1, APP1_CERT1_CERT2, false, true, |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 2959 | PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE); |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2960 | try { |
| 2961 | int rFlags = PackageManager.INSTALL_REPLACE_EXISTING; |
| 2962 | installFromRawResource("install.apk", APP1_CERT1, rFlags, false, |
| 2963 | false, -1, |
| 2964 | PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 2965 | } catch (Exception e) { |
| 2966 | failStr(e.getMessage()); |
| 2967 | } finally { |
| 2968 | if (ip != null) { |
| 2969 | cleanUpInstall(ip); |
| 2970 | } |
| 2971 | } |
| 2972 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 2973 | |
| 2974 | /** |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 2975 | * The following tests are related to testing KeySets-based key rotation |
| 2976 | */ |
| 2977 | /* |
| 2978 | * Check if an apk which does not specify an upgrade-keyset may be upgraded |
| 2979 | * by an apk which does |
| 2980 | */ |
| 2981 | public void testNoKSToUpgradeKS() throws Exception { |
| 2982 | replaceCerts(R.raw.keyset_sa_unone, R.raw.keyset_sa_ua, true, false, -1); |
| 2983 | } |
| 2984 | |
| 2985 | /* |
| 2986 | * Check if an apk which does specify an upgrade-keyset may be downgraded to |
| 2987 | * an apk which does not |
| 2988 | */ |
| 2989 | public void testUpgradeKSToNoKS() throws Exception { |
| 2990 | replaceCerts(R.raw.keyset_sa_ua, R.raw.keyset_sa_unone, true, false, -1); |
| 2991 | } |
| 2992 | |
| 2993 | /* |
| 2994 | * Check if an apk signed by a key other than the upgrade keyset can update |
| 2995 | * an app |
| 2996 | */ |
| 2997 | public void testUpgradeKSWithWrongKey() throws Exception { |
| 2998 | replaceCerts(R.raw.keyset_sa_ua, R.raw.keyset_sb_ua, true, true, |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 2999 | PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE); |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 3000 | } |
| 3001 | |
| 3002 | /* |
| 3003 | * Check if an apk signed by its signing key, which is not an upgrade key, |
| 3004 | * can upgrade an app. |
| 3005 | */ |
| 3006 | public void testUpgradeKSWithWrongSigningKey() throws Exception { |
| 3007 | replaceCerts(R.raw.keyset_sa_ub, R.raw.keyset_sa_ub, true, true, |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 3008 | PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE); |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 3009 | } |
| 3010 | |
| 3011 | /* |
| 3012 | * Check if an apk signed by its upgrade key, which is not its signing key, |
| 3013 | * can upgrade an app. |
| 3014 | */ |
| 3015 | public void testUpgradeKSWithUpgradeKey() throws Exception { |
| 3016 | replaceCerts(R.raw.keyset_sa_ub, R.raw.keyset_sb_ub, true, false, -1); |
| 3017 | } |
| 3018 | /* |
| 3019 | * Check if an apk signed by its upgrade key, which is its signing key, can |
| 3020 | * upgrade an app. |
| 3021 | */ |
| 3022 | public void testUpgradeKSWithSigningUpgradeKey() throws Exception { |
| 3023 | replaceCerts(R.raw.keyset_sa_ua, R.raw.keyset_sa_ua, true, false, -1); |
| 3024 | } |
| 3025 | |
| 3026 | /* |
| 3027 | * Check if an apk signed by multiple keys, one of which is its upgrade key, |
| 3028 | * can upgrade an app. |
| 3029 | */ |
| 3030 | public void testMultipleUpgradeKSWithUpgradeKey() throws Exception { |
| 3031 | replaceCerts(R.raw.keyset_sa_ua, R.raw.keyset_sab_ua, true, false, -1); |
| 3032 | } |
| 3033 | |
| 3034 | /* |
| 3035 | * Check if an apk signed by multiple keys, one of which is its signing key, |
| 3036 | * but none of which is an upgrade key, can upgrade an app. |
| 3037 | */ |
| 3038 | public void testMultipleUpgradeKSWithSigningKey() throws Exception { |
| 3039 | replaceCerts(R.raw.keyset_sau_ub, R.raw.keyset_sa_ua, true, true, |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 3040 | PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE); |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 3041 | } |
| 3042 | |
| 3043 | /* |
| 3044 | * Check if an apk which defines multiple (two) upgrade keysets is |
| 3045 | * upgrade-able by either. |
| 3046 | */ |
| 3047 | public void testUpgradeKSWithMultipleUpgradeKeySets() throws Exception { |
| 3048 | replaceCerts(R.raw.keyset_sa_ua_ub, R.raw.keyset_sa_ua, true, false, -1); |
| 3049 | replaceCerts(R.raw.keyset_sa_ua_ub, R.raw.keyset_sb_ub, true, false, -1); |
| 3050 | } |
| 3051 | |
| 3052 | /* |
| 3053 | * Check if an apk's sigs are changed after upgrading with a non-signing |
| 3054 | * key. |
| 3055 | * |
| 3056 | * TODO: consider checking against hard-coded Signatures in the Sig-tests |
| 3057 | */ |
| 3058 | public void testSigChangeAfterUpgrade() throws Exception { |
| 3059 | // install original apk and grab sigs |
| 3060 | installFromRawResource("tmp.apk", R.raw.keyset_sa_ub, |
| 3061 | 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3062 | PackageManager pm = getPm(); |
| 3063 | String pkgName = "com.android.frameworks.coretests.keysets"; |
| 3064 | PackageInfo pi = pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES); |
| 3065 | assertTrue("Package should only have one signature, sig A", |
| 3066 | pi.signatures.length == 1); |
| 3067 | String sigBefore = pi.signatures[0].toCharsString(); |
| 3068 | // install apk signed by different upgrade KeySet |
| 3069 | installFromRawResource("tmp2.apk", R.raw.keyset_sb_ub, |
| 3070 | PackageManager.INSTALL_REPLACE_EXISTING, false, false, -1, |
| 3071 | PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3072 | pi = pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES); |
| 3073 | assertTrue("Package should only have one signature, sig B", |
| 3074 | pi.signatures.length == 1); |
| 3075 | String sigAfter = pi.signatures[0].toCharsString(); |
| 3076 | assertFalse("Package signatures did not change after upgrade!", |
| 3077 | sigBefore.equals(sigAfter)); |
| 3078 | cleanUpInstall(pkgName); |
| 3079 | } |
| 3080 | |
| 3081 | /* |
| 3082 | * Check if an apk's sig is the same after upgrading with a signing |
| 3083 | * key. |
| 3084 | */ |
| 3085 | public void testSigSameAfterUpgrade() throws Exception { |
| 3086 | // install original apk and grab sigs |
| 3087 | installFromRawResource("tmp.apk", R.raw.keyset_sa_ua, |
| 3088 | 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3089 | PackageManager pm = getPm(); |
| 3090 | String pkgName = "com.android.frameworks.coretests.keysets"; |
| 3091 | PackageInfo pi = pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES); |
| 3092 | assertTrue("Package should only have one signature, sig A", |
| 3093 | pi.signatures.length == 1); |
| 3094 | String sigBefore = pi.signatures[0].toCharsString(); |
| 3095 | // install apk signed by same upgrade KeySet |
| 3096 | installFromRawResource("tmp2.apk", R.raw.keyset_sa_ua, |
| 3097 | PackageManager.INSTALL_REPLACE_EXISTING, false, false, -1, |
| 3098 | PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3099 | pi = pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES); |
| 3100 | assertTrue("Package should only have one signature, sig A", |
| 3101 | pi.signatures.length == 1); |
| 3102 | String sigAfter = pi.signatures[0].toCharsString(); |
| 3103 | assertTrue("Package signatures changed after upgrade!", |
| 3104 | sigBefore.equals(sigAfter)); |
| 3105 | cleanUpInstall(pkgName); |
| 3106 | } |
| 3107 | |
| 3108 | /* |
| 3109 | * Check if an apk's sigs are the same after upgrading with an app with |
| 3110 | * a subset of the original signing keys. |
| 3111 | */ |
| 3112 | public void testSigRemovedAfterUpgrade() throws Exception { |
| 3113 | // install original apk and grab sigs |
| 3114 | installFromRawResource("tmp.apk", R.raw.keyset_sab_ua, |
| 3115 | 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3116 | PackageManager pm = getPm(); |
| 3117 | String pkgName = "com.android.frameworks.coretests.keysets"; |
| 3118 | PackageInfo pi = pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES); |
| 3119 | assertTrue("Package should have two signatures, sig A and sig B", |
| 3120 | pi.signatures.length == 2); |
| 3121 | Set<String> sigsBefore = new HashSet<String>(); |
| 3122 | for (int i = 0; i < pi.signatures.length; i++) { |
| 3123 | sigsBefore.add(pi.signatures[i].toCharsString()); |
| 3124 | } |
| 3125 | // install apk signed subset upgrade KeySet |
| 3126 | installFromRawResource("tmp2.apk", R.raw.keyset_sa_ua, |
| 3127 | PackageManager.INSTALL_REPLACE_EXISTING, false, false, -1, |
| 3128 | PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3129 | pi = pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES); |
| 3130 | assertTrue("Package should only have one signature, sig A", |
| 3131 | pi.signatures.length == 1); |
| 3132 | String sigAfter = pi.signatures[0].toCharsString(); |
| 3133 | assertTrue("Original package signatures did not contain new sig", |
| 3134 | sigsBefore.contains(sigAfter)); |
| 3135 | cleanUpInstall(pkgName); |
| 3136 | } |
| 3137 | |
| 3138 | /* |
| 3139 | * Check if an apk's sigs are added to after upgrading with an app with |
| 3140 | * a superset of the original signing keys. |
| 3141 | */ |
| 3142 | public void testSigAddedAfterUpgrade() throws Exception { |
| 3143 | // install original apk and grab sigs |
| 3144 | installFromRawResource("tmp.apk", R.raw.keyset_sa_ua, |
| 3145 | 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3146 | PackageManager pm = getPm(); |
| 3147 | String pkgName = "com.android.frameworks.coretests.keysets"; |
| 3148 | PackageInfo pi = pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES); |
| 3149 | assertTrue("Package should only have one signature, sig A", |
| 3150 | pi.signatures.length == 1); |
| 3151 | String sigBefore = pi.signatures[0].toCharsString(); |
| 3152 | // install apk signed subset upgrade KeySet |
| 3153 | installFromRawResource("tmp2.apk", R.raw.keyset_sab_ua, |
| 3154 | PackageManager.INSTALL_REPLACE_EXISTING, false, false, -1, |
| 3155 | PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3156 | pi = pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES); |
| 3157 | assertTrue("Package should have two signatures, sig A and sig B", |
| 3158 | pi.signatures.length == 2); |
| 3159 | Set<String> sigsAfter = new HashSet<String>(); |
| 3160 | for (int i = 0; i < pi.signatures.length; i++) { |
| 3161 | sigsAfter.add(pi.signatures[i].toCharsString()); |
| 3162 | } |
| 3163 | assertTrue("Package signatures did not change after upgrade!", |
| 3164 | sigsAfter.contains(sigBefore)); |
| 3165 | cleanUpInstall(pkgName); |
| 3166 | } |
| 3167 | |
| 3168 | /* |
| 3169 | * Check if an apk gains signature-level permission after changing to the a |
| 3170 | * new signature, for which a permission should be granted. |
| 3171 | */ |
| 3172 | public void testUpgradeSigPermGained() throws Exception { |
| 3173 | // install apk which defines permission |
| 3174 | installFromRawResource("permDef.apk", R.raw.keyset_permdef_sa_unone, |
| 3175 | 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3176 | // install apk which uses permission but does not have sig |
| 3177 | installFromRawResource("permUse.apk", R.raw.keyset_permuse_sb_ua_ub, |
| 3178 | 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3179 | // verify that package does not have perm before |
| 3180 | PackageManager pm = getPm(); |
| 3181 | String permPkgName = "com.android.frameworks.coretests.keysets_permdef"; |
| 3182 | String pkgName = "com.android.frameworks.coretests.keysets"; |
| 3183 | String permName = "com.android.frameworks.coretests.keysets_permdef.keyset_perm"; |
| 3184 | assertFalse("keyset permission granted to app without same signature!", |
| 3185 | pm.checkPermission(permName, pkgName) |
| 3186 | == PackageManager.PERMISSION_GRANTED); |
| 3187 | // upgrade to apk with perm signature |
| 3188 | installFromRawResource("permUse2.apk", R.raw.keyset_permuse_sa_ua_ub, |
| 3189 | PackageManager.INSTALL_REPLACE_EXISTING, false, false, -1, |
| 3190 | PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3191 | assertTrue("keyset permission not granted to app after upgrade to same sig", |
| 3192 | pm.checkPermission(permName, pkgName) |
| 3193 | == PackageManager.PERMISSION_GRANTED); |
| 3194 | cleanUpInstall(permPkgName); |
| 3195 | cleanUpInstall(pkgName); |
| 3196 | } |
| 3197 | |
| 3198 | /* |
| 3199 | * Check if an apk loses signature-level permission after changing to the a |
| 3200 | * new signature, from one which a permission should be granted. |
| 3201 | */ |
| 3202 | public void testUpgradeSigPermLost() throws Exception { |
| 3203 | // install apk which defines permission |
| 3204 | installFromRawResource("permDef.apk", R.raw.keyset_permdef_sa_unone, |
| 3205 | 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3206 | // install apk which uses permission, signed by same sig |
| 3207 | installFromRawResource("permUse.apk", R.raw.keyset_permuse_sa_ua_ub, |
| 3208 | 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3209 | // verify that package does not have perm before |
| 3210 | PackageManager pm = getPm(); |
| 3211 | String permPkgName = "com.android.frameworks.coretests.keysets_permdef"; |
| 3212 | String pkgName = "com.android.frameworks.coretests.keysets"; |
| 3213 | String permName = "com.android.frameworks.coretests.keysets_permdef.keyset_perm"; |
| 3214 | assertTrue("keyset permission not granted to app with same sig", |
| 3215 | pm.checkPermission(permName, pkgName) |
| 3216 | == PackageManager.PERMISSION_GRANTED); |
| 3217 | // upgrade to apk without perm signature |
| 3218 | installFromRawResource("permUse2.apk", R.raw.keyset_permuse_sb_ua_ub, |
| 3219 | PackageManager.INSTALL_REPLACE_EXISTING, false, false, -1, |
| 3220 | PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3221 | |
| 3222 | assertFalse("keyset permission not revoked from app which upgraded to a " |
| 3223 | + "different signature", |
| 3224 | pm.checkPermission(permName, pkgName) |
| 3225 | == PackageManager.PERMISSION_GRANTED); |
| 3226 | cleanUpInstall(permPkgName); |
| 3227 | cleanUpInstall(pkgName); |
| 3228 | } |
| 3229 | |
| 3230 | /** |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 3231 | * The following tests are related to testing KeySets-based API |
| 3232 | */ |
| 3233 | |
| 3234 | /* |
| 3235 | * testGetSigningKeySetNull - ensure getSigningKeySet() returns null on null |
| 3236 | * input and when calling a package other than that which made the call. |
| 3237 | */ |
| 3238 | public void testGetSigningKeySet() throws Exception { |
| 3239 | PackageManager pm = getPm(); |
| 3240 | String mPkgName = mContext.getPackageName(); |
| 3241 | String otherPkgName = "com.android.frameworks.coretests.keysets_api"; |
| 3242 | KeySet ks; |
| 3243 | try { |
| 3244 | ks = pm.getSigningKeySet(null); |
| 3245 | assertTrue(false); // should have thrown |
| 3246 | } catch (NullPointerException e) { |
| 3247 | } |
| 3248 | try { |
| 3249 | ks = pm.getSigningKeySet("keysets.test.bogus.package"); |
| 3250 | assertTrue(false); // should have thrown |
| 3251 | } catch (IllegalArgumentException e) { |
| 3252 | } |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3253 | final InstallParams ip = installFromRawResource("keysetApi.apk", R.raw.keyset_splat_api, |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 3254 | 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3255 | try { |
| 3256 | ks = pm.getSigningKeySet(otherPkgName); |
| 3257 | assertTrue(false); // should have thrown |
| 3258 | } catch (SecurityException e) { |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3259 | } finally { |
| 3260 | cleanUpInstall(ip); |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 3261 | } |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 3262 | ks = pm.getSigningKeySet(mContext.getPackageName()); |
| 3263 | assertNotNull(ks); |
| 3264 | } |
| 3265 | |
| 3266 | /* |
| 3267 | * testGetKeySetByAlias - same as getSigningKeySet, but for keysets defined |
| 3268 | * by this package. |
| 3269 | */ |
| 3270 | public void testGetKeySetByAlias() throws Exception { |
| 3271 | PackageManager pm = getPm(); |
| 3272 | String mPkgName = mContext.getPackageName(); |
| 3273 | String otherPkgName = "com.android.frameworks.coretests.keysets_api"; |
| 3274 | KeySet ks; |
| 3275 | try { |
| 3276 | ks = pm.getKeySetByAlias(null, null); |
| 3277 | assertTrue(false); // should have thrown |
| 3278 | } catch (NullPointerException e) { |
| 3279 | } |
| 3280 | try { |
| 3281 | ks = pm.getKeySetByAlias(null, "keysetBogus"); |
| 3282 | assertTrue(false); // should have thrown |
| 3283 | } catch (NullPointerException e) { |
| 3284 | } |
| 3285 | try { |
| 3286 | ks = pm.getKeySetByAlias("keysets.test.bogus.package", null); |
| 3287 | assertTrue(false); // should have thrown |
| 3288 | } catch (NullPointerException e) { |
| 3289 | } |
| 3290 | try { |
| 3291 | ks = pm.getKeySetByAlias("keysets.test.bogus.package", "A"); |
| 3292 | assertTrue(false); // should have thrown |
| 3293 | } catch(IllegalArgumentException e) { |
| 3294 | } |
| 3295 | try { |
| 3296 | ks = pm.getKeySetByAlias(mPkgName, "keysetBogus"); |
| 3297 | assertTrue(false); // should have thrown |
| 3298 | } catch(IllegalArgumentException e) { |
| 3299 | } |
dcashman | c4d372f | 2015-12-01 17:04:58 -0800 | [diff] [blame] | 3300 | |
| 3301 | // make sure we can get a KeySet from our pkg |
| 3302 | ks = pm.getKeySetByAlias(mPkgName, "A"); |
| 3303 | assertNotNull(ks); |
| 3304 | |
| 3305 | // and another |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3306 | final InstallParams ip = installFromRawResource("keysetApi.apk", R.raw.keyset_splat_api, |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 3307 | 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3308 | try { |
| 3309 | ks = pm.getKeySetByAlias(otherPkgName, "A"); |
dcashman | c4d372f | 2015-12-01 17:04:58 -0800 | [diff] [blame] | 3310 | assertNotNull(ks); |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3311 | } finally { |
| 3312 | cleanUpInstall(ip); |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 3313 | } |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 3314 | } |
| 3315 | |
| 3316 | public void testIsSignedBy() throws Exception { |
| 3317 | PackageManager pm = getPm(); |
| 3318 | String mPkgName = mContext.getPackageName(); |
| 3319 | String otherPkgName = "com.android.frameworks.coretests.keysets_api"; |
| 3320 | KeySet mSigningKS = pm.getSigningKeySet(mPkgName); |
| 3321 | KeySet mDefinedKS = pm.getKeySetByAlias(mPkgName, "A"); |
| 3322 | |
| 3323 | try { |
| 3324 | assertFalse(pm.isSignedBy(null, null)); |
| 3325 | assertTrue(false); // should have thrown |
| 3326 | } catch (NullPointerException e) { |
| 3327 | } |
| 3328 | try { |
| 3329 | assertFalse(pm.isSignedBy(null, mSigningKS)); |
| 3330 | assertTrue(false); // should have thrown |
| 3331 | } catch (NullPointerException e) { |
| 3332 | } |
| 3333 | try { |
| 3334 | assertFalse(pm.isSignedBy(mPkgName, null)); |
| 3335 | assertTrue(false); // should have thrown |
| 3336 | } catch (NullPointerException e) { |
| 3337 | } |
| 3338 | try { |
| 3339 | assertFalse(pm.isSignedBy("keysets.test.bogus.package", mDefinedKS)); |
| 3340 | } catch(IllegalArgumentException e) { |
| 3341 | } |
| 3342 | assertFalse(pm.isSignedBy(mPkgName, mDefinedKS)); |
| 3343 | assertFalse(pm.isSignedBy(mPkgName, new KeySet(new Binder()))); |
| 3344 | assertTrue(pm.isSignedBy(mPkgName, mSigningKS)); |
| 3345 | |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3346 | final InstallParams ip1 = installFromRawResource("keysetApi.apk", R.raw.keyset_splat_api, |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 3347 | 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3348 | try { |
| 3349 | assertFalse(pm.isSignedBy(otherPkgName, mDefinedKS)); |
| 3350 | assertTrue(pm.isSignedBy(otherPkgName, mSigningKS)); |
| 3351 | } finally { |
| 3352 | cleanUpInstall(ip1); |
| 3353 | } |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 3354 | |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3355 | final InstallParams ip2 = installFromRawResource("keysetApi.apk", R.raw.keyset_splata_api, |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 3356 | 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3357 | try { |
| 3358 | assertTrue(pm.isSignedBy(otherPkgName, mDefinedKS)); |
| 3359 | assertTrue(pm.isSignedBy(otherPkgName, mSigningKS)); |
| 3360 | } finally { |
| 3361 | cleanUpInstall(ip2); |
| 3362 | } |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 3363 | } |
| 3364 | |
| 3365 | public void testIsSignedByExactly() throws Exception { |
| 3366 | PackageManager pm = getPm(); |
| 3367 | String mPkgName = mContext.getPackageName(); |
| 3368 | String otherPkgName = "com.android.frameworks.coretests.keysets_api"; |
| 3369 | KeySet mSigningKS = pm.getSigningKeySet(mPkgName); |
| 3370 | KeySet mDefinedKS = pm.getKeySetByAlias(mPkgName, "A"); |
| 3371 | try { |
| 3372 | assertFalse(pm.isSignedBy(null, null)); |
| 3373 | assertTrue(false); // should have thrown |
| 3374 | } catch (NullPointerException e) { |
| 3375 | } |
| 3376 | try { |
| 3377 | assertFalse(pm.isSignedBy(null, mSigningKS)); |
| 3378 | assertTrue(false); // should have thrown |
| 3379 | } catch (NullPointerException e) { |
| 3380 | } |
| 3381 | try { |
| 3382 | assertFalse(pm.isSignedBy(mPkgName, null)); |
| 3383 | assertTrue(false); // should have thrown |
| 3384 | } catch (NullPointerException e) { |
| 3385 | } |
| 3386 | try { |
| 3387 | assertFalse(pm.isSignedByExactly("keysets.test.bogus.package", mDefinedKS)); |
| 3388 | } catch(IllegalArgumentException e) { |
| 3389 | } |
| 3390 | assertFalse(pm.isSignedByExactly(mPkgName, mDefinedKS)); |
| 3391 | assertFalse(pm.isSignedByExactly(mPkgName, new KeySet(new Binder()))); |
| 3392 | assertTrue(pm.isSignedByExactly(mPkgName, mSigningKS)); |
| 3393 | |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3394 | final InstallParams ip1 = installFromRawResource("keysetApi.apk", R.raw.keyset_splat_api, |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 3395 | 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3396 | try { |
| 3397 | assertFalse(pm.isSignedByExactly(otherPkgName, mDefinedKS)); |
| 3398 | assertTrue(pm.isSignedByExactly(otherPkgName, mSigningKS)); |
| 3399 | } finally { |
| 3400 | cleanUpInstall(ip1); |
| 3401 | } |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 3402 | |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3403 | final InstallParams ip2 = installFromRawResource("keysetApi.apk", R.raw.keyset_splata_api, |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 3404 | 0, false, false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3405 | try { |
| 3406 | assertFalse(pm.isSignedByExactly(otherPkgName, mDefinedKS)); |
| 3407 | assertFalse(pm.isSignedByExactly(otherPkgName, mSigningKS)); |
| 3408 | } finally { |
| 3409 | cleanUpInstall(ip2); |
| 3410 | } |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 3411 | } |
| 3412 | |
| 3413 | |
| 3414 | |
| 3415 | /** |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3416 | * The following tests are related to testing the checkSignatures api. |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3417 | */ |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3418 | private void checkSignatures(int apk1, int apk2, int expMatchResult) throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3419 | checkSharedSignatures(apk1, apk2, true, false, -1, expMatchResult); |
| 3420 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3421 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 3422 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3423 | public void testCheckSignaturesAllMatch() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3424 | int apk1 = APP1_CERT1_CERT2; |
| 3425 | int apk2 = APP2_CERT1_CERT2; |
| 3426 | checkSignatures(apk1, apk2, PackageManager.SIGNATURE_MATCH); |
| 3427 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3428 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 3429 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3430 | public void testCheckSignaturesNoMatch() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3431 | int apk1 = APP1_CERT1; |
| 3432 | int apk2 = APP2_CERT2; |
| 3433 | checkSignatures(apk1, apk2, PackageManager.SIGNATURE_NO_MATCH); |
| 3434 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3435 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 3436 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3437 | public void testCheckSignaturesSomeMatch1() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3438 | int apk1 = APP1_CERT1_CERT2; |
| 3439 | int apk2 = APP2_CERT1; |
| 3440 | checkSignatures(apk1, apk2, PackageManager.SIGNATURE_NO_MATCH); |
| 3441 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3442 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 3443 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3444 | public void testCheckSignaturesSomeMatch2() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3445 | int apk1 = APP1_CERT1_CERT2; |
| 3446 | int apk2 = APP2_CERT2; |
| 3447 | checkSignatures(apk1, apk2, PackageManager.SIGNATURE_NO_MATCH); |
| 3448 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3449 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 3450 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3451 | public void testCheckSignaturesMoreMatch() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3452 | int apk1 = APP1_CERT1; |
| 3453 | int apk2 = APP2_CERT1_CERT2; |
| 3454 | checkSignatures(apk1, apk2, PackageManager.SIGNATURE_NO_MATCH); |
| 3455 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3456 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 3457 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3458 | public void testCheckSignaturesUnknown() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3459 | int apk1 = APP1_CERT1_CERT2; |
| 3460 | int apk2 = APP2_CERT1_CERT2; |
| 3461 | String apk1Name = "install1.apk"; |
| 3462 | String apk2Name = "install2.apk"; |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3463 | |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3464 | final InstallParams ip = installFromRawResource(apk1Name, apk1, 0, false, |
| 3465 | false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3466 | try { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3467 | PackageManager pm = mContext.getPackageManager(); |
| 3468 | // Delete app2 |
| 3469 | File filesDir = mContext.getFilesDir(); |
| 3470 | File outFile = new File(filesDir, apk2Name); |
| 3471 | int rawResId = apk2; |
| 3472 | Uri packageURI = getInstallablePackage(rawResId, outFile); |
| 3473 | PackageParser.Package pkg = parsePackage(packageURI); |
Kenny Root | a3e9079 | 2012-10-18 10:58:36 -0700 | [diff] [blame] | 3474 | getPm().deletePackage(pkg.packageName, null, PackageManager.DELETE_ALL_USERS); |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3475 | // Check signatures now |
| 3476 | int match = mContext.getPackageManager().checkSignatures( |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3477 | ip.pkg.packageName, pkg.packageName); |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3478 | assertEquals(PackageManager.SIGNATURE_UNKNOWN_PACKAGE, match); |
| 3479 | } finally { |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3480 | cleanUpInstall(ip); |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3481 | } |
| 3482 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3483 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 3484 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3485 | public void testInstallNoCertificates() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3486 | int apk1 = APP1_UNSIGNED; |
| 3487 | String apk1Name = "install1.apk"; |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3488 | |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3489 | installFromRawResource(apk1Name, apk1, 0, false, |
| 3490 | true, PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES, |
| 3491 | PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3492 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3493 | |
| 3494 | /* |
| 3495 | * The following tests are related to apps using shared uids signed with |
| 3496 | * different certs. |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3497 | */ |
| 3498 | private int SHARED1_UNSIGNED = R.raw.install_shared1_unsigned; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3499 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3500 | private int SHARED1_CERT1 = R.raw.install_shared1_cert1; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3501 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3502 | private int SHARED1_CERT2 = R.raw.install_shared1_cert2; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3503 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3504 | private int SHARED1_CERT1_CERT2 = R.raw.install_shared1_cert1_cert2; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3505 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3506 | private int SHARED2_UNSIGNED = R.raw.install_shared2_unsigned; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3507 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3508 | private int SHARED2_CERT1 = R.raw.install_shared2_cert1; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3509 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3510 | private int SHARED2_CERT2 = R.raw.install_shared2_cert2; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3511 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3512 | private int SHARED2_CERT1_CERT2 = R.raw.install_shared2_cert1_cert2; |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3513 | |
| 3514 | private void checkSharedSignatures(int apk1, int apk2, boolean cleanUp, boolean fail, |
| 3515 | int retCode, int expMatchResult) throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3516 | String apk1Name = "install1.apk"; |
| 3517 | String apk2Name = "install2.apk"; |
| 3518 | PackageParser.Package pkg1 = getParsedPackage(apk1Name, apk1); |
| 3519 | PackageParser.Package pkg2 = getParsedPackage(apk2Name, apk2); |
| 3520 | |
| 3521 | try { |
| 3522 | // Clean up before testing first. |
| 3523 | cleanUpInstall(pkg1.packageName); |
| 3524 | cleanUpInstall(pkg2.packageName); |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3525 | installFromRawResource(apk1Name, apk1, 0, false, false, -1, |
| 3526 | PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3527 | if (fail) { |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3528 | installFromRawResource(apk2Name, apk2, 0, false, true, retCode, |
| 3529 | PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3530 | } else { |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3531 | installFromRawResource(apk2Name, apk2, 0, false, false, -1, |
| 3532 | PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3533 | int match = mContext.getPackageManager().checkSignatures(pkg1.packageName, |
| 3534 | pkg2.packageName); |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3535 | assertEquals(expMatchResult, match); |
| 3536 | } |
| 3537 | } finally { |
| 3538 | if (cleanUp) { |
| 3539 | cleanUpInstall(pkg1.packageName); |
| 3540 | cleanUpInstall(pkg2.packageName); |
| 3541 | } |
| 3542 | } |
| 3543 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3544 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 3545 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3546 | public void testCheckSignaturesSharedAllMatch() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3547 | int apk1 = SHARED1_CERT1_CERT2; |
| 3548 | int apk2 = SHARED2_CERT1_CERT2; |
| 3549 | boolean fail = false; |
| 3550 | int retCode = -1; |
| 3551 | int expMatchResult = PackageManager.SIGNATURE_MATCH; |
| 3552 | checkSharedSignatures(apk1, apk2, true, fail, retCode, expMatchResult); |
| 3553 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3554 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 3555 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3556 | public void testCheckSignaturesSharedNoMatch() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3557 | int apk1 = SHARED1_CERT1; |
| 3558 | int apk2 = SHARED2_CERT2; |
| 3559 | boolean fail = true; |
| 3560 | int retCode = PackageManager.INSTALL_FAILED_SHARED_USER_INCOMPATIBLE; |
| 3561 | int expMatchResult = -1; |
| 3562 | checkSharedSignatures(apk1, apk2, true, fail, retCode, expMatchResult); |
| 3563 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3564 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3565 | /* |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3566 | * Test that an app signed with cert1 and cert2 cannot be replaced when |
| 3567 | * signed with cert1 alone. |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3568 | */ |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 3569 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3570 | public void testCheckSignaturesSharedSomeMatch1() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3571 | int apk1 = SHARED1_CERT1_CERT2; |
| 3572 | int apk2 = SHARED2_CERT1; |
| 3573 | boolean fail = true; |
| 3574 | int retCode = PackageManager.INSTALL_FAILED_SHARED_USER_INCOMPATIBLE; |
| 3575 | int expMatchResult = -1; |
| 3576 | checkSharedSignatures(apk1, apk2, true, fail, retCode, expMatchResult); |
| 3577 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3578 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3579 | /* |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3580 | * Test that an app signed with cert1 and cert2 cannot be replaced when |
| 3581 | * signed with cert2 alone. |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3582 | */ |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 3583 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3584 | public void testCheckSignaturesSharedSomeMatch2() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3585 | int apk1 = SHARED1_CERT1_CERT2; |
| 3586 | int apk2 = SHARED2_CERT2; |
| 3587 | boolean fail = true; |
| 3588 | int retCode = PackageManager.INSTALL_FAILED_SHARED_USER_INCOMPATIBLE; |
| 3589 | int expMatchResult = -1; |
| 3590 | checkSharedSignatures(apk1, apk2, true, fail, retCode, expMatchResult); |
| 3591 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3592 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 3593 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3594 | public void testCheckSignaturesSharedUnknown() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3595 | int apk1 = SHARED1_CERT1_CERT2; |
| 3596 | int apk2 = SHARED2_CERT1_CERT2; |
| 3597 | String apk1Name = "install1.apk"; |
| 3598 | String apk2Name = "install2.apk"; |
| 3599 | InstallParams ip1 = null; |
| 3600 | |
| 3601 | try { |
| 3602 | ip1 = installFromRawResource(apk1Name, apk1, 0, false, |
| 3603 | false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3604 | PackageManager pm = mContext.getPackageManager(); |
| 3605 | // Delete app2 |
| 3606 | PackageParser.Package pkg = getParsedPackage(apk2Name, apk2); |
Kenny Root | a3e9079 | 2012-10-18 10:58:36 -0700 | [diff] [blame] | 3607 | getPm().deletePackage(pkg.packageName, null, PackageManager.DELETE_ALL_USERS); |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3608 | // Check signatures now |
| 3609 | int match = mContext.getPackageManager().checkSignatures( |
| 3610 | ip1.pkg.packageName, pkg.packageName); |
| 3611 | assertEquals(PackageManager.SIGNATURE_UNKNOWN_PACKAGE, match); |
| 3612 | } finally { |
| 3613 | if (ip1 != null) { |
| 3614 | cleanUpInstall(ip1); |
| 3615 | } |
| 3616 | } |
| 3617 | } |
Neal Nguyen | edb979a | 2010-04-22 13:54:32 -0700 | [diff] [blame] | 3618 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 3619 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3620 | public void testReplaceFirstSharedMatchAllCerts() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3621 | int apk1 = SHARED1_CERT1; |
| 3622 | int apk2 = SHARED2_CERT1; |
| 3623 | int rapk1 = SHARED1_CERT1; |
| 3624 | checkSignatures(apk1, apk2, PackageManager.SIGNATURE_MATCH); |
| 3625 | replaceCerts(apk1, rapk1, true, false, -1); |
| 3626 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3627 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 3628 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3629 | public void testReplaceSecondSharedMatchAllCerts() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3630 | int apk1 = SHARED1_CERT1; |
| 3631 | int apk2 = SHARED2_CERT1; |
| 3632 | int rapk2 = SHARED2_CERT1; |
| 3633 | checkSignatures(apk1, apk2, PackageManager.SIGNATURE_MATCH); |
| 3634 | replaceCerts(apk2, rapk2, true, false, -1); |
| 3635 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3636 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 3637 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3638 | public void testReplaceFirstSharedMatchSomeCerts() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3639 | int apk1 = SHARED1_CERT1_CERT2; |
| 3640 | int apk2 = SHARED2_CERT1_CERT2; |
| 3641 | int rapk1 = SHARED1_CERT1; |
| 3642 | boolean fail = true; |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 3643 | int retCode = PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE; |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3644 | checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH); |
| 3645 | installFromRawResource("install.apk", rapk1, PackageManager.INSTALL_REPLACE_EXISTING, true, |
| 3646 | fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3647 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3648 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 3649 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3650 | public void testReplaceSecondSharedMatchSomeCerts() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3651 | int apk1 = SHARED1_CERT1_CERT2; |
| 3652 | int apk2 = SHARED2_CERT1_CERT2; |
| 3653 | int rapk2 = SHARED2_CERT1; |
| 3654 | boolean fail = true; |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 3655 | int retCode = PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE; |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3656 | checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH); |
| 3657 | installFromRawResource("install.apk", rapk2, PackageManager.INSTALL_REPLACE_EXISTING, true, |
| 3658 | fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3659 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3660 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 3661 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3662 | public void testReplaceFirstSharedMatchNoCerts() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3663 | int apk1 = SHARED1_CERT1; |
| 3664 | int apk2 = SHARED2_CERT1; |
| 3665 | int rapk1 = SHARED1_CERT2; |
| 3666 | boolean fail = true; |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 3667 | int retCode = PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE; |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3668 | checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH); |
| 3669 | installFromRawResource("install.apk", rapk1, PackageManager.INSTALL_REPLACE_EXISTING, true, |
| 3670 | fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3671 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3672 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 3673 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3674 | public void testReplaceSecondSharedMatchNoCerts() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3675 | int apk1 = SHARED1_CERT1; |
| 3676 | int apk2 = SHARED2_CERT1; |
| 3677 | int rapk2 = SHARED2_CERT2; |
| 3678 | boolean fail = true; |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 3679 | int retCode = PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE; |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3680 | checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH); |
| 3681 | installFromRawResource("install.apk", rapk2, PackageManager.INSTALL_REPLACE_EXISTING, true, |
| 3682 | fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3683 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3684 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 3685 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3686 | public void testReplaceFirstSharedMatchMoreCerts() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3687 | int apk1 = SHARED1_CERT1; |
| 3688 | int apk2 = SHARED2_CERT1; |
| 3689 | int rapk1 = SHARED1_CERT1_CERT2; |
| 3690 | boolean fail = true; |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 3691 | int retCode = PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE; |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3692 | checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH); |
| 3693 | installFromRawResource("install.apk", rapk1, PackageManager.INSTALL_REPLACE_EXISTING, true, |
| 3694 | fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3695 | } |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3696 | |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 3697 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3698 | public void testReplaceSecondSharedMatchMoreCerts() throws Exception { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3699 | int apk1 = SHARED1_CERT1; |
| 3700 | int apk2 = SHARED2_CERT1; |
| 3701 | int rapk2 = SHARED2_CERT1_CERT2; |
| 3702 | boolean fail = true; |
Jeff Sharkey | 2a53332 | 2014-11-07 16:24:48 -0800 | [diff] [blame] | 3703 | int retCode = PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE; |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3704 | checkSharedSignatures(apk1, apk2, false, false, -1, PackageManager.SIGNATURE_MATCH); |
| 3705 | installFromRawResource("install.apk", rapk2, PackageManager.INSTALL_REPLACE_EXISTING, true, |
| 3706 | fail, retCode, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3707 | } |
Kenny Root | 1683afa | 2011-01-07 14:27:50 -0800 | [diff] [blame] | 3708 | |
Kenny Root | 4c3915a | 2011-01-31 16:36:32 -0800 | [diff] [blame] | 3709 | /** |
| 3710 | * Unknown features should be allowed to install. This prevents older phones |
| 3711 | * from rejecting new packages that specify features that didn't exist when |
| 3712 | * an older phone existed. All older phones are assumed to have those |
| 3713 | * features. |
| 3714 | * <p> |
| 3715 | * Right now we allow all packages to be installed regardless of their |
| 3716 | * features. |
| 3717 | */ |
Kenny Root | 1683afa | 2011-01-07 14:27:50 -0800 | [diff] [blame] | 3718 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3719 | public void testUsesFeatureUnknownFeature() throws Exception { |
Kenny Root | 4c3915a | 2011-01-31 16:36:32 -0800 | [diff] [blame] | 3720 | int retCode = PackageManager.INSTALL_SUCCEEDED; |
| 3721 | installFromRawResource("install.apk", R.raw.install_uses_feature, 0, true, false, retCode, |
Kenny Root | 1683afa | 2011-01-07 14:27:50 -0800 | [diff] [blame] | 3722 | PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3723 | } |
Kenny Root | 3c67689 | 2011-01-12 10:58:40 -0800 | [diff] [blame] | 3724 | |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 3725 | @LargeTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3726 | public void testInstallNonexistentFile() throws Exception { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 3727 | int retCode = PackageManager.INSTALL_FAILED_INVALID_URI; |
| 3728 | File invalidFile = new File("/nonexistent-file.apk"); |
| 3729 | invokeInstallPackageFail(Uri.fromFile(invalidFile), 0, retCode); |
| 3730 | } |
| 3731 | |
Kenny Root | 0aaa0d9 | 2011-09-12 16:42:55 -0700 | [diff] [blame] | 3732 | @SmallTest |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3733 | public void testGetVerifierDeviceIdentity() throws Exception { |
Kenny Root | 0aaa0d9 | 2011-09-12 16:42:55 -0700 | [diff] [blame] | 3734 | PackageManager pm = getPm(); |
| 3735 | VerifierDeviceIdentity id = pm.getVerifierDeviceIdentity(); |
| 3736 | |
| 3737 | assertNotNull("Verifier device identity should not be null", id); |
| 3738 | } |
| 3739 | |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3740 | public void testGetInstalledPackages() throws Exception { |
Martin Wallgren | f2f1b6c | 2011-05-26 15:03:52 +0200 | [diff] [blame] | 3741 | List<PackageInfo> packages = getPm().getInstalledPackages(0); |
| 3742 | assertNotNull("installed packages cannot be null", packages); |
| 3743 | assertTrue("installed packages cannot be empty", packages.size() > 0); |
| 3744 | } |
| 3745 | |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3746 | public void testGetUnInstalledPackages() throws Exception { |
Martin Wallgren | f2f1b6c | 2011-05-26 15:03:52 +0200 | [diff] [blame] | 3747 | List<PackageInfo> packages = getPm().getInstalledPackages( |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 3748 | PackageManager.MATCH_UNINSTALLED_PACKAGES); |
Martin Wallgren | f2f1b6c | 2011-05-26 15:03:52 +0200 | [diff] [blame] | 3749 | assertNotNull("installed packages cannot be null", packages); |
| 3750 | assertTrue("installed packages cannot be empty", packages.size() > 0); |
| 3751 | } |
| 3752 | |
| 3753 | /** |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3754 | * Test that getInstalledPackages returns all the data specified in flags. |
Martin Wallgren | f2f1b6c | 2011-05-26 15:03:52 +0200 | [diff] [blame] | 3755 | */ |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3756 | public void testGetInstalledPackagesAll() throws Exception { |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3757 | final int flags = PackageManager.GET_ACTIVITIES | PackageManager.GET_GIDS |
Martin Wallgren | f2f1b6c | 2011-05-26 15:03:52 +0200 | [diff] [blame] | 3758 | | PackageManager.GET_CONFIGURATIONS | PackageManager.GET_INSTRUMENTATION |
| 3759 | | PackageManager.GET_PERMISSIONS | PackageManager.GET_PROVIDERS |
| 3760 | | PackageManager.GET_RECEIVERS | PackageManager.GET_SERVICES |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 3761 | | PackageManager.GET_SIGNATURES | PackageManager.MATCH_UNINSTALLED_PACKAGES; |
Martin Wallgren | f2f1b6c | 2011-05-26 15:03:52 +0200 | [diff] [blame] | 3762 | |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3763 | final InstallParams ip = |
| 3764 | installFromRawResource("install.apk", R.raw.install_complete_package_info, |
| 3765 | 0 /*flags*/, false /*cleanUp*/, false /*fail*/, -1 /*result*/, |
| 3766 | PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); |
| 3767 | try { |
| 3768 | final List<PackageInfo> packages = getPm().getInstalledPackages(flags); |
| 3769 | assertNotNull("installed packages cannot be null", packages); |
| 3770 | assertTrue("installed packages cannot be empty", packages.size() > 0); |
Martin Wallgren | f2f1b6c | 2011-05-26 15:03:52 +0200 | [diff] [blame] | 3771 | |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3772 | PackageInfo packageInfo = null; |
Martin Wallgren | f2f1b6c | 2011-05-26 15:03:52 +0200 | [diff] [blame] | 3773 | |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3774 | // Find the package with all components specified in the AndroidManifest |
| 3775 | // to ensure no null values |
| 3776 | for (PackageInfo pi : packages) { |
| 3777 | if ("com.android.frameworks.coretests.install_complete_package_info" |
| 3778 | .equals(pi.packageName)) { |
| 3779 | packageInfo = pi; |
| 3780 | break; |
| 3781 | } |
Martin Wallgren | f2f1b6c | 2011-05-26 15:03:52 +0200 | [diff] [blame] | 3782 | } |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3783 | assertNotNull("activities should not be null", packageInfo.activities); |
| 3784 | assertNotNull("configPreferences should not be null", packageInfo.configPreferences); |
| 3785 | assertNotNull("instrumentation should not be null", packageInfo.instrumentation); |
| 3786 | assertNotNull("permissions should not be null", packageInfo.permissions); |
| 3787 | assertNotNull("providers should not be null", packageInfo.providers); |
| 3788 | assertNotNull("receivers should not be null", packageInfo.receivers); |
| 3789 | assertNotNull("services should not be null", packageInfo.services); |
| 3790 | assertNotNull("signatures should not be null", packageInfo.signatures); |
| 3791 | } finally { |
| 3792 | cleanUpInstall(ip); |
Martin Wallgren | f2f1b6c | 2011-05-26 15:03:52 +0200 | [diff] [blame] | 3793 | } |
Martin Wallgren | f2f1b6c | 2011-05-26 15:03:52 +0200 | [diff] [blame] | 3794 | } |
| 3795 | |
| 3796 | /** |
| 3797 | * Test that getInstalledPackages returns all the data specified in |
| 3798 | * flags when the GET_UNINSTALLED_PACKAGES flag is set. |
| 3799 | */ |
Kenny Root | 25c925e | 2012-09-08 22:02:21 -0700 | [diff] [blame] | 3800 | public void testGetUnInstalledPackagesAll() throws Exception { |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 3801 | final int flags = PackageManager.MATCH_UNINSTALLED_PACKAGES |
Martin Wallgren | f2f1b6c | 2011-05-26 15:03:52 +0200 | [diff] [blame] | 3802 | | PackageManager.GET_ACTIVITIES | PackageManager.GET_GIDS |
| 3803 | | PackageManager.GET_CONFIGURATIONS | PackageManager.GET_INSTRUMENTATION |
| 3804 | | PackageManager.GET_PERMISSIONS | PackageManager.GET_PROVIDERS |
| 3805 | | PackageManager.GET_RECEIVERS | PackageManager.GET_SERVICES |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 3806 | | PackageManager.GET_SIGNATURES; |
Martin Wallgren | f2f1b6c | 2011-05-26 15:03:52 +0200 | [diff] [blame] | 3807 | |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3808 | // first, install the package |
| 3809 | final InstallParams ip = |
| 3810 | installFromRawResource("install.apk", R.raw.install_complete_package_info, |
| 3811 | 0 /*flags*/, false /*cleanUp*/, false /*fail*/, -1 /*result*/, |
| 3812 | PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); |
| 3813 | try { |
| 3814 | // then, remove it, keeping it's data around |
| 3815 | final GenericReceiver receiver = new DeleteReceiver(ip.pkg.packageName); |
| 3816 | invokeDeletePackage(ip.pkg.packageName, PackageManager.DELETE_KEEP_DATA, receiver); |
Martin Wallgren | f2f1b6c | 2011-05-26 15:03:52 +0200 | [diff] [blame] | 3817 | |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3818 | final List<PackageInfo> packages = getPm().getInstalledPackages(flags); |
| 3819 | assertNotNull("installed packages cannot be null", packages); |
| 3820 | assertTrue("installed packages cannot be empty", packages.size() > 0); |
Martin Wallgren | f2f1b6c | 2011-05-26 15:03:52 +0200 | [diff] [blame] | 3821 | |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3822 | PackageInfo packageInfo = null; |
| 3823 | |
| 3824 | // Find the package with all components specified in the AndroidManifest |
| 3825 | // to ensure no null values |
| 3826 | for (PackageInfo pi : packages) { |
| 3827 | if ("com.android.frameworks.coretests.install_complete_package_info" |
| 3828 | .equals(pi.packageName)) { |
| 3829 | packageInfo = pi; |
| 3830 | break; |
| 3831 | } |
Martin Wallgren | f2f1b6c | 2011-05-26 15:03:52 +0200 | [diff] [blame] | 3832 | } |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3833 | assertNotNull("activities should not be null", packageInfo.activities); |
| 3834 | assertNotNull("configPreferences should not be null", packageInfo.configPreferences); |
| 3835 | assertNotNull("instrumentation should not be null", packageInfo.instrumentation); |
| 3836 | assertNotNull("permissions should not be null", packageInfo.permissions); |
| 3837 | assertNotNull("providers should not be null", packageInfo.providers); |
| 3838 | assertNotNull("receivers should not be null", packageInfo.receivers); |
| 3839 | assertNotNull("services should not be null", packageInfo.services); |
| 3840 | assertNotNull("signatures should not be null", packageInfo.signatures); |
| 3841 | } finally { |
| 3842 | cleanUpInstall(ip); |
Martin Wallgren | f2f1b6c | 2011-05-26 15:03:52 +0200 | [diff] [blame] | 3843 | } |
Martin Wallgren | f2f1b6c | 2011-05-26 15:03:52 +0200 | [diff] [blame] | 3844 | } |
| 3845 | |
Todd Kennedy | 9f3a78c | 2015-11-30 16:55:04 -0800 | [diff] [blame] | 3846 | @Suppress |
Kenny Root | e15bdc2 | 2012-09-16 15:45:38 -0700 | [diff] [blame] | 3847 | public void testInstall_BadDex_CleanUp() throws Exception { |
| 3848 | int retCode = PackageManager.INSTALL_FAILED_DEXOPT; |
| 3849 | installFromRawResource("install.apk", R.raw.install_bad_dex, 0, true, true, retCode, |
| 3850 | PackageInfo.INSTALL_LOCATION_UNSPECIFIED); |
| 3851 | } |
| 3852 | |
Calin Juravle | 3d2af7f | 2017-04-19 19:56:21 -0700 | [diff] [blame] | 3853 | private static class TestDexModuleRegisterCallback |
| 3854 | extends PackageManager.DexModuleRegisterCallback { |
| 3855 | private String mDexModulePath = null; |
| 3856 | private boolean mSuccess = false; |
| 3857 | private String mMessage = null; |
| 3858 | CountDownLatch doneSignal = new CountDownLatch(1); |
| 3859 | |
| 3860 | @Override |
| 3861 | public void onDexModuleRegistered(String dexModulePath, boolean success, String message) { |
| 3862 | mDexModulePath = dexModulePath; |
| 3863 | mSuccess = success; |
| 3864 | mMessage = message; |
| 3865 | doneSignal.countDown(); |
| 3866 | } |
| 3867 | |
| 3868 | boolean waitTillDone() { |
| 3869 | long startTime = System.currentTimeMillis(); |
| 3870 | while (System.currentTimeMillis() - startTime < MAX_WAIT_TIME) { |
| 3871 | try { |
| 3872 | return doneSignal.await(MAX_WAIT_TIME, TimeUnit.MILLISECONDS); |
| 3873 | } catch (InterruptedException e) { |
| 3874 | Log.i(TAG, "Interrupted during sleep", e); |
| 3875 | } |
| 3876 | } |
| 3877 | return false; |
| 3878 | } |
| 3879 | |
| 3880 | } |
| 3881 | |
| 3882 | // Verify that the base code path cannot be registered. |
| 3883 | public void testRegisterDexModuleBaseCode() throws Exception { |
| 3884 | PackageManager pm = getPm(); |
| 3885 | ApplicationInfo info = getContext().getApplicationInfo(); |
| 3886 | TestDexModuleRegisterCallback callback = new TestDexModuleRegisterCallback(); |
| 3887 | pm.registerDexModule(info.getBaseCodePath(), callback); |
| 3888 | assertTrue(callback.waitTillDone()); |
| 3889 | assertEquals(info.getBaseCodePath(), callback.mDexModulePath); |
| 3890 | assertFalse("BaseCodePath should not be registered", callback.mSuccess); |
| 3891 | } |
| 3892 | |
| 3893 | // Verify thatmodules which are not own by the calling package are not registered. |
| 3894 | public void testRegisterDexModuleNotOwningModule() throws Exception { |
| 3895 | TestDexModuleRegisterCallback callback = new TestDexModuleRegisterCallback(); |
| 3896 | String moduleBelongingToOtherPackage = "/data/user/0/com.google.android.gms/module.apk"; |
| 3897 | getPm().registerDexModule(moduleBelongingToOtherPackage, callback); |
| 3898 | assertTrue(callback.waitTillDone()); |
| 3899 | assertEquals(moduleBelongingToOtherPackage, callback.mDexModulePath); |
| 3900 | assertTrue(callback.waitTillDone()); |
| 3901 | assertFalse("Only modules belonging to the calling package can be registered", |
| 3902 | callback.mSuccess); |
| 3903 | } |
| 3904 | |
| 3905 | // Verify that modules owned by the package are successfully registered. |
| 3906 | public void testRegisterDexModuleSuccessfully() throws Exception { |
| 3907 | ApplicationInfo info = getContext().getApplicationInfo(); |
| 3908 | // Copy the main apk into the data folder and use it as a "module". |
| 3909 | File dexModuleDir = new File(info.dataDir, "module-dir"); |
| 3910 | File dexModule = new File(dexModuleDir, "module.apk"); |
| 3911 | try { |
| 3912 | assertNotNull(FileUtils.createDir( |
| 3913 | dexModuleDir.getParentFile(), dexModuleDir.getName())); |
| 3914 | Files.copy(Paths.get(info.getBaseCodePath()), dexModule.toPath(), |
| 3915 | StandardCopyOption.REPLACE_EXISTING); |
| 3916 | TestDexModuleRegisterCallback callback = new TestDexModuleRegisterCallback(); |
| 3917 | getPm().registerDexModule(dexModule.toString(), callback); |
| 3918 | assertTrue(callback.waitTillDone()); |
| 3919 | assertEquals(dexModule.toString(), callback.mDexModulePath); |
| 3920 | assertTrue(callback.waitTillDone()); |
| 3921 | assertTrue(callback.mMessage, callback.mSuccess); |
| 3922 | |
| 3923 | // NOTE: |
| 3924 | // This actually verifies internal behaviour which might change. It's not |
| 3925 | // ideal but it's the best we can do since there's no other place we can currently |
| 3926 | // write a better test. |
| 3927 | for(String isa : getAppDexInstructionSets(info)) { |
| 3928 | Files.exists(Paths.get(dexModuleDir.toString(), "oat", isa, "module.odex")); |
| 3929 | Files.exists(Paths.get(dexModuleDir.toString(), "oat", isa, "module.vdex")); |
| 3930 | } |
| 3931 | } finally { |
| 3932 | FileUtils.deleteContentsAndDir(dexModuleDir); |
| 3933 | } |
| 3934 | } |
| 3935 | |
| 3936 | // If the module does not exist on disk we should get a failure. |
| 3937 | public void testRegisterDexModuleNotExists() throws Exception { |
| 3938 | ApplicationInfo info = getContext().getApplicationInfo(); |
| 3939 | String nonExistentApk = Paths.get(info.dataDir, "non-existent.apk").toString(); |
| 3940 | TestDexModuleRegisterCallback callback = new TestDexModuleRegisterCallback(); |
| 3941 | getPm().registerDexModule(nonExistentApk, callback); |
| 3942 | assertTrue(callback.waitTillDone()); |
| 3943 | assertEquals(nonExistentApk, callback.mDexModulePath); |
| 3944 | assertTrue(callback.waitTillDone()); |
| 3945 | assertFalse("DexModule registration should fail", callback.mSuccess); |
| 3946 | } |
| 3947 | |
| 3948 | // Copied from com.android.server.pm.InstructionSets because we don't have access to it here. |
| 3949 | private static String[] getAppDexInstructionSets(ApplicationInfo info) { |
| 3950 | if (info.primaryCpuAbi != null) { |
| 3951 | if (info.secondaryCpuAbi != null) { |
| 3952 | return new String[] { |
| 3953 | VMRuntime.getInstructionSet(info.primaryCpuAbi), |
| 3954 | VMRuntime.getInstructionSet(info.secondaryCpuAbi) }; |
| 3955 | } else { |
| 3956 | return new String[] { |
| 3957 | VMRuntime.getInstructionSet(info.primaryCpuAbi) }; |
| 3958 | } |
| 3959 | } |
| 3960 | |
| 3961 | return new String[] { VMRuntime.getInstructionSet(Build.SUPPORTED_ABIS[0]) }; |
| 3962 | } |
| 3963 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 3964 | /*---------- Recommended install location tests ----*/ |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 3965 | /* |
| 3966 | * TODO's |
| 3967 | * check version numbers for upgrades |
| 3968 | * check permissions of installed packages |
| 3969 | * how to do tests on updated system apps? |
| 3970 | * verify updates to system apps cannot be installed on the sdcard. |
| 3971 | */ |
Suchi Amalapurapu | afbaaa1 | 2010-02-03 11:24:49 -0800 | [diff] [blame] | 3972 | } |