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