Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 1 | /* |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 2 | * Copyright (C) 2020 The Android Open Source Project |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 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 | |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 17 | package com.android.server.pm.parsing; |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.junit.Assert.assertNotNull; |
| 21 | import static org.junit.Assert.assertNull; |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 22 | import static org.junit.Assert.assertTrue; |
Anton Hansson | b2f709d | 2020-01-09 10:25:23 +0000 | [diff] [blame] | 23 | import static org.junit.Assert.fail; |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 24 | |
Gavin Corkery | ef44172 | 2019-05-09 17:02:10 +0100 | [diff] [blame] | 25 | import android.apex.ApexInfo; |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 27 | import android.content.pm.ApplicationInfo; |
| 28 | import android.content.pm.PackageInfo; |
| 29 | import android.content.pm.PackageManager; |
| 30 | import android.content.pm.PackageParser; |
| 31 | import android.content.pm.PermissionInfo; |
Winson | f00c755 | 2020-01-28 12:52:01 -0800 | [diff] [blame] | 32 | import android.content.pm.parsing.component.ParsedComponent; |
| 33 | import android.content.pm.parsing.component.ParsedPermission; |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 34 | import android.os.Build; |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 35 | import android.os.Bundle; |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 36 | import android.os.FileUtils; |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 37 | import android.platform.test.annotations.Presubmit; |
Oli Lan | 72ae447 | 2020-04-14 16:50:41 +0100 | [diff] [blame] | 38 | import android.util.SparseIntArray; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 39 | |
| 40 | import androidx.test.InstrumentationRegistry; |
| 41 | import androidx.test.filters.SmallTest; |
| 42 | import androidx.test.runner.AndroidJUnit4; |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 43 | |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 44 | import com.android.frameworks.servicestests.R; |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 45 | import com.android.internal.util.ArrayUtils; |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 46 | import com.android.server.pm.parsing.pkg.AndroidPackage; |
| 47 | import com.android.server.pm.parsing.pkg.ParsedPackage; |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 48 | |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 49 | import org.junit.Test; |
| 50 | import org.junit.runner.RunWith; |
| 51 | |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 52 | import java.io.File; |
| 53 | import java.io.InputStream; |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 54 | import java.util.function.Function; |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 55 | |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 56 | /** |
| 57 | * {@link ParsedPackage} was moved to the server, so this test moved along with it. |
| 58 | * |
| 59 | * This should be eventually refactored to a comprehensive parsing test, combined with its |
| 60 | * server variant in the parent package. |
| 61 | * |
| 62 | * TODO(b/135203078): Remove this test and replicate the cases in the actual com.android.server |
| 63 | * variant. |
| 64 | */ |
| 65 | @Presubmit |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 66 | @SmallTest |
| 67 | @RunWith(AndroidJUnit4.class) |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 68 | public class PackageParserLegacyCoreTest { |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 69 | private static final String RELEASED = null; |
| 70 | private static final String OLDER_PRE_RELEASE = "A"; |
| 71 | private static final String PRE_RELEASE = "B"; |
| 72 | private static final String NEWER_PRE_RELEASE = "C"; |
| 73 | |
Narayan Kamath | 71fcc14 | 2019-01-15 18:20:39 +0000 | [diff] [blame] | 74 | // Codenames with a fingerprint attached to them. These may only be present in the apps |
| 75 | // declared min SDK and not as platform codenames. |
| 76 | private static final String OLDER_PRE_RELEASE_WITH_FINGERPRINT = "A.fingerprint"; |
| 77 | private static final String PRE_RELEASE_WITH_FINGERPRINT = "B.fingerprint"; |
| 78 | private static final String NEWER_PRE_RELEASE_WITH_FINGERPRINT = "C.fingerprint"; |
| 79 | |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 80 | private static final String[] CODENAMES_RELEASED = { /* empty */}; |
| 81 | private static final String[] CODENAMES_PRE_RELEASE = {PRE_RELEASE}; |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 82 | |
| 83 | private static final int OLDER_VERSION = 10; |
| 84 | private static final int PLATFORM_VERSION = 20; |
| 85 | private static final int NEWER_VERSION = 30; |
| 86 | |
| 87 | private void verifyComputeMinSdkVersion(int minSdkVersion, String minSdkCodename, |
| 88 | boolean isPlatformReleased, int expectedMinSdk) { |
| 89 | final String[] outError = new String[1]; |
| 90 | final int result = PackageParser.computeMinSdkVersion( |
| 91 | minSdkVersion, |
| 92 | minSdkCodename, |
| 93 | PLATFORM_VERSION, |
| 94 | isPlatformReleased ? CODENAMES_RELEASED : CODENAMES_PRE_RELEASE, |
| 95 | outError); |
| 96 | |
Narayan Kamath | 71fcc14 | 2019-01-15 18:20:39 +0000 | [diff] [blame] | 97 | assertEquals("Error msg: " + outError[0], expectedMinSdk, result); |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 98 | |
| 99 | if (expectedMinSdk == -1) { |
| 100 | assertNotNull(outError[0]); |
| 101 | } else { |
| 102 | assertNull(outError[0]); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void testComputeMinSdkVersion_preReleasePlatform() { |
| 108 | // Do allow older release minSdkVersion on pre-release platform. |
| 109 | // APP: Released API 10 |
| 110 | // DEV: Pre-release API 20 |
| 111 | verifyComputeMinSdkVersion(OLDER_VERSION, RELEASED, false, OLDER_VERSION); |
| 112 | |
| 113 | // Do allow same release minSdkVersion on pre-release platform. |
| 114 | // APP: Released API 20 |
| 115 | // DEV: Pre-release API 20 |
| 116 | verifyComputeMinSdkVersion(PLATFORM_VERSION, RELEASED, false, PLATFORM_VERSION); |
| 117 | |
| 118 | // Don't allow newer release minSdkVersion on pre-release platform. |
| 119 | // APP: Released API 30 |
| 120 | // DEV: Pre-release API 20 |
| 121 | verifyComputeMinSdkVersion(NEWER_VERSION, RELEASED, false, -1); |
| 122 | |
| 123 | // Don't allow older pre-release minSdkVersion on pre-release platform. |
| 124 | // APP: Pre-release API 10 |
| 125 | // DEV: Pre-release API 20 |
| 126 | verifyComputeMinSdkVersion(OLDER_VERSION, OLDER_PRE_RELEASE, false, -1); |
Narayan Kamath | 71fcc14 | 2019-01-15 18:20:39 +0000 | [diff] [blame] | 127 | verifyComputeMinSdkVersion(OLDER_VERSION, OLDER_PRE_RELEASE_WITH_FINGERPRINT, false, -1); |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 128 | |
| 129 | // Do allow same pre-release minSdkVersion on pre-release platform, |
| 130 | // but overwrite the specified version with CUR_DEVELOPMENT. |
| 131 | // APP: Pre-release API 20 |
| 132 | // DEV: Pre-release API 20 |
| 133 | verifyComputeMinSdkVersion(PLATFORM_VERSION, PRE_RELEASE, false, |
| 134 | Build.VERSION_CODES.CUR_DEVELOPMENT); |
Narayan Kamath | 71fcc14 | 2019-01-15 18:20:39 +0000 | [diff] [blame] | 135 | verifyComputeMinSdkVersion(PLATFORM_VERSION, PRE_RELEASE_WITH_FINGERPRINT, false, |
| 136 | Build.VERSION_CODES.CUR_DEVELOPMENT); |
| 137 | |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 138 | |
| 139 | // Don't allow newer pre-release minSdkVersion on pre-release platform. |
| 140 | // APP: Pre-release API 30 |
| 141 | // DEV: Pre-release API 20 |
| 142 | verifyComputeMinSdkVersion(NEWER_VERSION, NEWER_PRE_RELEASE, false, -1); |
Narayan Kamath | 71fcc14 | 2019-01-15 18:20:39 +0000 | [diff] [blame] | 143 | verifyComputeMinSdkVersion(NEWER_VERSION, NEWER_PRE_RELEASE_WITH_FINGERPRINT, false, -1); |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Test |
| 147 | public void testComputeMinSdkVersion_releasedPlatform() { |
| 148 | // Do allow older release minSdkVersion on released platform. |
| 149 | // APP: Released API 10 |
| 150 | // DEV: Released API 20 |
| 151 | verifyComputeMinSdkVersion(OLDER_VERSION, RELEASED, true, OLDER_VERSION); |
| 152 | |
| 153 | // Do allow same release minSdkVersion on released platform. |
| 154 | // APP: Released API 20 |
| 155 | // DEV: Released API 20 |
| 156 | verifyComputeMinSdkVersion(PLATFORM_VERSION, RELEASED, true, PLATFORM_VERSION); |
| 157 | |
| 158 | // Don't allow newer release minSdkVersion on released platform. |
| 159 | // APP: Released API 30 |
| 160 | // DEV: Released API 20 |
| 161 | verifyComputeMinSdkVersion(NEWER_VERSION, RELEASED, true, -1); |
| 162 | |
| 163 | // Don't allow older pre-release minSdkVersion on released platform. |
| 164 | // APP: Pre-release API 10 |
| 165 | // DEV: Released API 20 |
| 166 | verifyComputeMinSdkVersion(OLDER_VERSION, OLDER_PRE_RELEASE, true, -1); |
Narayan Kamath | 71fcc14 | 2019-01-15 18:20:39 +0000 | [diff] [blame] | 167 | verifyComputeMinSdkVersion(OLDER_VERSION, OLDER_PRE_RELEASE_WITH_FINGERPRINT, true, -1); |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 168 | |
| 169 | // Don't allow same pre-release minSdkVersion on released platform. |
| 170 | // APP: Pre-release API 20 |
| 171 | // DEV: Released API 20 |
| 172 | verifyComputeMinSdkVersion(PLATFORM_VERSION, PRE_RELEASE, true, -1); |
Narayan Kamath | 71fcc14 | 2019-01-15 18:20:39 +0000 | [diff] [blame] | 173 | verifyComputeMinSdkVersion(PLATFORM_VERSION, PRE_RELEASE_WITH_FINGERPRINT, true, -1); |
| 174 | |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 175 | |
| 176 | // Don't allow newer pre-release minSdkVersion on released platform. |
| 177 | // APP: Pre-release API 30 |
| 178 | // DEV: Released API 20 |
| 179 | verifyComputeMinSdkVersion(NEWER_VERSION, NEWER_PRE_RELEASE, true, -1); |
Narayan Kamath | 71fcc14 | 2019-01-15 18:20:39 +0000 | [diff] [blame] | 180 | verifyComputeMinSdkVersion(NEWER_VERSION, NEWER_PRE_RELEASE_WITH_FINGERPRINT, true, -1); |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | private void verifyComputeTargetSdkVersion(int targetSdkVersion, String targetSdkCodename, |
Nicholas Lativy | eb23e4d | 2019-02-01 13:39:28 +0000 | [diff] [blame] | 184 | boolean isPlatformReleased, int expectedTargetSdk) { |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 185 | final String[] outError = new String[1]; |
| 186 | final int result = PackageParser.computeTargetSdkVersion( |
| 187 | targetSdkVersion, |
| 188 | targetSdkCodename, |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 189 | isPlatformReleased ? CODENAMES_RELEASED : CODENAMES_PRE_RELEASE, |
Nicholas Lativy | eb23e4d | 2019-02-01 13:39:28 +0000 | [diff] [blame] | 190 | outError); |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 191 | |
| 192 | assertEquals(result, expectedTargetSdk); |
| 193 | |
| 194 | if (expectedTargetSdk == -1) { |
| 195 | assertNotNull(outError[0]); |
| 196 | } else { |
| 197 | assertNull(outError[0]); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | @Test |
| 202 | public void testComputeTargetSdkVersion_preReleasePlatform() { |
| 203 | // Do allow older release targetSdkVersion on pre-release platform. |
| 204 | // APP: Released API 10 |
| 205 | // DEV: Pre-release API 20 |
Nicholas Lativy | eb23e4d | 2019-02-01 13:39:28 +0000 | [diff] [blame] | 206 | verifyComputeTargetSdkVersion(OLDER_VERSION, RELEASED, false, OLDER_VERSION); |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 207 | |
| 208 | // Do allow same release targetSdkVersion on pre-release platform. |
| 209 | // APP: Released API 20 |
| 210 | // DEV: Pre-release API 20 |
Nicholas Lativy | eb23e4d | 2019-02-01 13:39:28 +0000 | [diff] [blame] | 211 | verifyComputeTargetSdkVersion(PLATFORM_VERSION, RELEASED, false, PLATFORM_VERSION); |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 212 | |
| 213 | // Do allow newer release targetSdkVersion on pre-release platform. |
| 214 | // APP: Released API 30 |
| 215 | // DEV: Pre-release API 20 |
Nicholas Lativy | eb23e4d | 2019-02-01 13:39:28 +0000 | [diff] [blame] | 216 | verifyComputeTargetSdkVersion(NEWER_VERSION, RELEASED, false, NEWER_VERSION); |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 217 | |
| 218 | // Don't allow older pre-release targetSdkVersion on pre-release platform. |
| 219 | // APP: Pre-release API 10 |
| 220 | // DEV: Pre-release API 20 |
Nicholas Lativy | eb23e4d | 2019-02-01 13:39:28 +0000 | [diff] [blame] | 221 | verifyComputeTargetSdkVersion(OLDER_VERSION, OLDER_PRE_RELEASE, false, -1); |
| 222 | verifyComputeTargetSdkVersion(OLDER_VERSION, OLDER_PRE_RELEASE_WITH_FINGERPRINT, false, -1); |
Narayan Kamath | 71fcc14 | 2019-01-15 18:20:39 +0000 | [diff] [blame] | 223 | |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 224 | |
| 225 | // Do allow same pre-release targetSdkVersion on pre-release platform, |
| 226 | // but overwrite the specified version with CUR_DEVELOPMENT. |
| 227 | // APP: Pre-release API 20 |
| 228 | // DEV: Pre-release API 20 |
| 229 | verifyComputeTargetSdkVersion(PLATFORM_VERSION, PRE_RELEASE, false, |
Nicholas Lativy | eb23e4d | 2019-02-01 13:39:28 +0000 | [diff] [blame] | 230 | Build.VERSION_CODES.CUR_DEVELOPMENT); |
Narayan Kamath | 71fcc14 | 2019-01-15 18:20:39 +0000 | [diff] [blame] | 231 | verifyComputeTargetSdkVersion(PLATFORM_VERSION, PRE_RELEASE_WITH_FINGERPRINT, false, |
Nicholas Lativy | eb23e4d | 2019-02-01 13:39:28 +0000 | [diff] [blame] | 232 | Build.VERSION_CODES.CUR_DEVELOPMENT); |
Narayan Kamath | 71fcc14 | 2019-01-15 18:20:39 +0000 | [diff] [blame] | 233 | |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 234 | |
| 235 | // Don't allow newer pre-release targetSdkVersion on pre-release platform. |
| 236 | // APP: Pre-release API 30 |
| 237 | // DEV: Pre-release API 20 |
Nicholas Lativy | eb23e4d | 2019-02-01 13:39:28 +0000 | [diff] [blame] | 238 | verifyComputeTargetSdkVersion(NEWER_VERSION, NEWER_PRE_RELEASE, false, -1); |
| 239 | verifyComputeTargetSdkVersion(NEWER_VERSION, NEWER_PRE_RELEASE_WITH_FINGERPRINT, false, -1); |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | @Test |
| 243 | public void testComputeTargetSdkVersion_releasedPlatform() { |
| 244 | // Do allow older release targetSdkVersion on released platform. |
| 245 | // APP: Released API 10 |
| 246 | // DEV: Released API 20 |
Nicholas Lativy | eb23e4d | 2019-02-01 13:39:28 +0000 | [diff] [blame] | 247 | verifyComputeTargetSdkVersion(OLDER_VERSION, RELEASED, true, OLDER_VERSION); |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 248 | |
| 249 | // Do allow same release targetSdkVersion on released platform. |
| 250 | // APP: Released API 20 |
| 251 | // DEV: Released API 20 |
Nicholas Lativy | eb23e4d | 2019-02-01 13:39:28 +0000 | [diff] [blame] | 252 | verifyComputeTargetSdkVersion(PLATFORM_VERSION, RELEASED, true, PLATFORM_VERSION); |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 253 | |
| 254 | // Do allow newer release targetSdkVersion on released platform. |
| 255 | // APP: Released API 30 |
| 256 | // DEV: Released API 20 |
Nicholas Lativy | eb23e4d | 2019-02-01 13:39:28 +0000 | [diff] [blame] | 257 | verifyComputeTargetSdkVersion(NEWER_VERSION, RELEASED, true, NEWER_VERSION); |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 258 | |
| 259 | // Don't allow older pre-release targetSdkVersion on released platform. |
| 260 | // APP: Pre-release API 10 |
| 261 | // DEV: Released API 20 |
Nicholas Lativy | eb23e4d | 2019-02-01 13:39:28 +0000 | [diff] [blame] | 262 | verifyComputeTargetSdkVersion(OLDER_VERSION, OLDER_PRE_RELEASE, true, -1); |
| 263 | verifyComputeTargetSdkVersion(OLDER_VERSION, OLDER_PRE_RELEASE_WITH_FINGERPRINT, true, -1); |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 264 | |
| 265 | // Don't allow same pre-release targetSdkVersion on released platform. |
| 266 | // APP: Pre-release API 20 |
| 267 | // DEV: Released API 20 |
Nicholas Lativy | eb23e4d | 2019-02-01 13:39:28 +0000 | [diff] [blame] | 268 | verifyComputeTargetSdkVersion(PLATFORM_VERSION, PRE_RELEASE, true, -1); |
| 269 | verifyComputeTargetSdkVersion(PLATFORM_VERSION, PRE_RELEASE_WITH_FINGERPRINT, true, -1); |
Narayan Kamath | 71fcc14 | 2019-01-15 18:20:39 +0000 | [diff] [blame] | 270 | |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 271 | |
| 272 | // Don't allow newer pre-release targetSdkVersion on released platform. |
| 273 | // APP: Pre-release API 30 |
| 274 | // DEV: Released API 20 |
Nicholas Lativy | eb23e4d | 2019-02-01 13:39:28 +0000 | [diff] [blame] | 275 | verifyComputeTargetSdkVersion(NEWER_VERSION, NEWER_PRE_RELEASE, true, -1); |
| 276 | verifyComputeTargetSdkVersion(NEWER_VERSION, NEWER_PRE_RELEASE_WITH_FINGERPRINT, true, -1); |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 277 | } |
Holly Jiuyu Sun | b6d7695 | 2017-01-11 17:34:03 -0800 | [diff] [blame] | 278 | |
| 279 | /** |
| 280 | * Unit test for PackageParser.getActivityConfigChanges(). |
| 281 | * If the bit is 1 in the original configChanges, it is still 1 in the final configChanges. |
| 282 | * If the bit is 0 in the original configChanges and the bit is not set to 1 in |
Holly Jiuyu Sun | c93c3b9 | 2017-04-28 15:34:44 -0700 | [diff] [blame] | 283 | * recreateOnConfigChanges, the bit is changed to 1 in the final configChanges by default. |
Holly Jiuyu Sun | b6d7695 | 2017-01-11 17:34:03 -0800 | [diff] [blame] | 284 | */ |
| 285 | @Test |
| 286 | public void testGetActivityConfigChanges() { |
Holly Jiuyu Sun | c93c3b9 | 2017-04-28 15:34:44 -0700 | [diff] [blame] | 287 | // Not set in either configChanges or recreateOnConfigChanges. |
Holly Jiuyu Sun | b6d7695 | 2017-01-11 17:34:03 -0800 | [diff] [blame] | 288 | int configChanges = 0x0000; // 00000000. |
Holly Jiuyu Sun | c93c3b9 | 2017-04-28 15:34:44 -0700 | [diff] [blame] | 289 | int recreateOnConfigChanges = 0x0000; // 00000000. |
Holly Jiuyu Sun | b6d7695 | 2017-01-11 17:34:03 -0800 | [diff] [blame] | 290 | int finalConfigChanges = |
Holly Jiuyu Sun | c93c3b9 | 2017-04-28 15:34:44 -0700 | [diff] [blame] | 291 | PackageParser.getActivityConfigChanges(configChanges, recreateOnConfigChanges); |
Holly Jiuyu Sun | b6d7695 | 2017-01-11 17:34:03 -0800 | [diff] [blame] | 292 | assertEquals(0x0003, finalConfigChanges); // Should be 00000011. |
| 293 | |
Holly Jiuyu Sun | c93c3b9 | 2017-04-28 15:34:44 -0700 | [diff] [blame] | 294 | // Not set in configChanges, but set in recreateOnConfigChanges. |
Holly Jiuyu Sun | b6d7695 | 2017-01-11 17:34:03 -0800 | [diff] [blame] | 295 | configChanges = 0x0000; // 00000000. |
Holly Jiuyu Sun | c93c3b9 | 2017-04-28 15:34:44 -0700 | [diff] [blame] | 296 | recreateOnConfigChanges = 0x0003; // 00000011. |
Holly Jiuyu Sun | b6d7695 | 2017-01-11 17:34:03 -0800 | [diff] [blame] | 297 | finalConfigChanges = |
Holly Jiuyu Sun | c93c3b9 | 2017-04-28 15:34:44 -0700 | [diff] [blame] | 298 | PackageParser.getActivityConfigChanges(configChanges, recreateOnConfigChanges); |
Holly Jiuyu Sun | b6d7695 | 2017-01-11 17:34:03 -0800 | [diff] [blame] | 299 | assertEquals(0x0000, finalConfigChanges); // Should be 00000000. |
| 300 | |
| 301 | // Set in configChanges. |
| 302 | configChanges = 0x0003; // 00000011. |
Holly Jiuyu Sun | c93c3b9 | 2017-04-28 15:34:44 -0700 | [diff] [blame] | 303 | recreateOnConfigChanges = 0X0000; // 00000000. |
Holly Jiuyu Sun | b6d7695 | 2017-01-11 17:34:03 -0800 | [diff] [blame] | 304 | finalConfigChanges = |
Holly Jiuyu Sun | c93c3b9 | 2017-04-28 15:34:44 -0700 | [diff] [blame] | 305 | PackageParser.getActivityConfigChanges(configChanges, recreateOnConfigChanges); |
Holly Jiuyu Sun | b6d7695 | 2017-01-11 17:34:03 -0800 | [diff] [blame] | 306 | assertEquals(0x0003, finalConfigChanges); // Should be 00000011. |
| 307 | |
Holly Jiuyu Sun | c93c3b9 | 2017-04-28 15:34:44 -0700 | [diff] [blame] | 308 | recreateOnConfigChanges = 0x0003; // 00000011. |
Holly Jiuyu Sun | b6d7695 | 2017-01-11 17:34:03 -0800 | [diff] [blame] | 309 | finalConfigChanges = |
Holly Jiuyu Sun | c93c3b9 | 2017-04-28 15:34:44 -0700 | [diff] [blame] | 310 | PackageParser.getActivityConfigChanges(configChanges, recreateOnConfigChanges); |
Holly Jiuyu Sun | b6d7695 | 2017-01-11 17:34:03 -0800 | [diff] [blame] | 311 | assertEquals(0x0003, finalConfigChanges); // Should still be 00000011. |
| 312 | |
| 313 | // Other bit set in configChanges. |
| 314 | configChanges = 0x0080; // 10000000, orientation. |
Holly Jiuyu Sun | c93c3b9 | 2017-04-28 15:34:44 -0700 | [diff] [blame] | 315 | recreateOnConfigChanges = 0x0000; // 00000000. |
Holly Jiuyu Sun | b6d7695 | 2017-01-11 17:34:03 -0800 | [diff] [blame] | 316 | finalConfigChanges = |
Holly Jiuyu Sun | c93c3b9 | 2017-04-28 15:34:44 -0700 | [diff] [blame] | 317 | PackageParser.getActivityConfigChanges(configChanges, recreateOnConfigChanges); |
Holly Jiuyu Sun | b6d7695 | 2017-01-11 17:34:03 -0800 | [diff] [blame] | 318 | assertEquals(0x0083, finalConfigChanges); // Should be 10000011. |
| 319 | } |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 320 | |
| 321 | /** |
Jiyong Park | 5b1b734 | 2019-01-18 09:45:17 +0900 | [diff] [blame] | 322 | * Copies a specified {@code resourceId} to a file. Returns a non-null file if the copy |
| 323 | * succeeded, or {@code null} otherwise. |
| 324 | */ |
| 325 | File copyRawResourceToFile(String baseName, int resourceId) throws Exception { |
| 326 | // Copy the resource to a file. |
| 327 | Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); |
| 328 | InputStream is = context.getResources().openRawResource(resourceId); |
| 329 | File outFile = null; |
| 330 | try { |
| 331 | outFile = new File(context.getFilesDir(), baseName); |
| 332 | assertTrue(FileUtils.copyToFile(is, outFile)); |
| 333 | return outFile; |
| 334 | } catch (Exception e) { |
| 335 | if (outFile != null) { |
| 336 | outFile.delete(); |
| 337 | } |
| 338 | |
| 339 | return null; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | /** |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 344 | * Attempts to parse a package. |
| 345 | * |
| 346 | * APKs are put into coretests/apks/packageparser_*. |
| 347 | * |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 348 | * @param apkFileName temporary file name to store apk extracted from resources |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 349 | * @param apkResourceId identifier of the apk as a resource |
| 350 | */ |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 351 | ParsedPackage parsePackage(String apkFileName, int apkResourceId, |
| 352 | Function<ParsedPackage, ParsedPackage> converter) throws Exception { |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 353 | // Copy the resource to a file. |
Jiyong Park | 5b1b734 | 2019-01-18 09:45:17 +0900 | [diff] [blame] | 354 | File outFile = null; |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 355 | try { |
Jiyong Park | 5b1b734 | 2019-01-18 09:45:17 +0900 | [diff] [blame] | 356 | outFile = copyRawResourceToFile(apkFileName, apkResourceId); |
Winson | 727da64 | 2020-03-10 15:25:32 -0700 | [diff] [blame] | 357 | return converter.apply(new TestPackageParser2() |
| 358 | .parsePackage(outFile, 0 /* flags */, false)); |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 359 | } finally { |
Jiyong Park | 5b1b734 | 2019-01-18 09:45:17 +0900 | [diff] [blame] | 360 | if (outFile != null) { |
| 361 | outFile.delete(); |
| 362 | } |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Asserts basic properties about a component. |
| 368 | */ |
Winson | f00c755 | 2020-01-28 12:52:01 -0800 | [diff] [blame] | 369 | private void assertComponent(String className, int numIntents, ParsedComponent component) { |
| 370 | assertEquals(className, component.getName()); |
| 371 | assertEquals(numIntents, component.getIntents().size()); |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Asserts four regularly-named components of each type: one Activity, one Service, one |
| 376 | * Provider, and one Receiver. |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 377 | * |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 378 | * @param template templated string with %s subbed with Activity, Service, Provider, Receiver |
| 379 | */ |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 380 | private void assertOneComponentOfEachType(String template, AndroidPackage p) { |
Winson | 6571c8a | 2019-10-23 17:00:42 -0700 | [diff] [blame] | 381 | assertEquals(1, p.getActivities().size()); |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 382 | assertComponent(String.format(template, "Activity"), |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 383 | 0 /* intents */, p.getActivities().get(0)); |
| 384 | assertEquals(1, p.getServices().size()); |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 385 | assertComponent(String.format(template, "Service"), |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 386 | 0 /* intents */, p.getServices().get(0)); |
| 387 | assertEquals(1, p.getProviders().size()); |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 388 | assertComponent(String.format(template, "Provider"), |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 389 | 0 /* intents */, p.getProviders().get(0)); |
| 390 | assertEquals(1, p.getReceivers().size()); |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 391 | assertComponent(String.format(template, "Receiver"), |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 392 | 0 /* intents */, p.getReceivers().get(0)); |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 395 | private void assertPermission(String name, int protectionLevel, ParsedPermission permission) { |
| 396 | assertEquals(name, permission.getName()); |
| 397 | assertEquals(protectionLevel, permission.getProtection()); |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 400 | private void assertMetadata(Bundle b, String... keysAndValues) { |
| 401 | assertTrue("Odd number of elements in keysAndValues", (keysAndValues.length % 2) == 0); |
| 402 | |
| 403 | assertNotNull(b); |
| 404 | assertEquals(keysAndValues.length / 2, b.size()); |
| 405 | |
| 406 | for (int i = 0; i < keysAndValues.length; i += 2) { |
| 407 | final String key = keysAndValues[i]; |
| 408 | final String value = keysAndValues[i + 1]; |
| 409 | |
| 410 | assertEquals(value, b.getString(key)); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | // TODO Add a "_cached" test for testMultiPackageComponents() too, after fixing b/64295061. |
| 415 | // Package.writeToParcel can't handle circular package references. |
| 416 | |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 417 | @Test |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 418 | public void testPackageWithComponents_no_cache() throws Exception { |
| 419 | checkPackageWithComponents(p -> p); |
| 420 | } |
| 421 | |
| 422 | @Test |
| 423 | public void testPackageWithComponents_cached() throws Exception { |
| 424 | checkPackageWithComponents(p -> |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 425 | PackageCacher.fromCacheEntryStatic(PackageCacher.toCacheEntryStatic(p))); |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | private void checkPackageWithComponents( |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 429 | Function<ParsedPackage, ParsedPackage> converter) throws Exception { |
| 430 | ParsedPackage p = parsePackage( |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 431 | "install_complete_package_info.apk", R.raw.install_complete_package_info, |
| 432 | converter); |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 433 | String packageName = "com.android.frameworks.coretests.install_complete_package_info"; |
| 434 | |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 435 | assertEquals(packageName, p.getPackageName()); |
| 436 | assertEquals(1, p.getPermissions().size()); |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 437 | assertPermission( |
| 438 | "com.android.frameworks.coretests.install_complete_package_info.test_permission", |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 439 | PermissionInfo.PROTECTION_NORMAL, p.getPermissions().get(0)); |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 440 | |
phweiss | 26d3496 | 2019-10-29 13:37:54 +0100 | [diff] [blame] | 441 | findAndRemoveAppDetailsActivity(p); |
Oli Lan | ae2f5f3 | 2019-07-26 14:31:59 +0100 | [diff] [blame] | 442 | |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 443 | assertOneComponentOfEachType("com.android.frameworks.coretests.Test%s", p); |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 444 | |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 445 | assertMetadata(p.getMetaData(), |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 446 | "key1", "value1", |
| 447 | "key2", "this_is_app"); |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 448 | assertMetadata(p.getActivities().get(0).getMetaData(), |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 449 | "key1", "value1", |
| 450 | "key2", "this_is_activity"); |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 451 | assertMetadata(p.getServices().get(0).getMetaData(), |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 452 | "key1", "value1", |
| 453 | "key2", "this_is_service"); |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 454 | assertMetadata(p.getReceivers().get(0).getMetaData(), |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 455 | "key1", "value1", |
| 456 | "key2", "this_is_receiver"); |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 457 | assertMetadata(p.getProviders().get(0).getMetaData(), |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 458 | "key1", "value1", |
| 459 | "key2", "this_is_provider"); |
phweiss | 26d3496 | 2019-10-29 13:37:54 +0100 | [diff] [blame] | 460 | |
| 461 | } |
| 462 | |
| 463 | private void findAndRemoveAppDetailsActivity(ParsedPackage p) { |
| 464 | // Hidden "app details" activity is added to every package. |
| 465 | boolean foundAppDetailsActivity = false; |
| 466 | for (int i = 0; i < ArrayUtils.size(p.getActivities()); i++) { |
Winson | f00c755 | 2020-01-28 12:52:01 -0800 | [diff] [blame] | 467 | if (p.getActivities().get(i).getClassName().equals( |
phweiss | 26d3496 | 2019-10-29 13:37:54 +0100 | [diff] [blame] | 468 | PackageManager.APP_DETAILS_ACTIVITY_CLASS_NAME)) { |
| 469 | foundAppDetailsActivity = true; |
| 470 | p.getActivities().remove(i); |
| 471 | break; |
| 472 | } |
| 473 | } |
| 474 | assertTrue("Did not find app details activity", foundAppDetailsActivity); |
| 475 | } |
| 476 | |
| 477 | @Test |
| 478 | public void testPackageWithIntentFilters_no_cache() throws Exception { |
| 479 | checkPackageWithIntentFilters(p -> p); |
| 480 | } |
| 481 | |
| 482 | @Test |
| 483 | public void testPackageWithIntentFilters_cached() throws Exception { |
| 484 | checkPackageWithIntentFilters(p -> |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 485 | PackageCacher.fromCacheEntryStatic(PackageCacher.toCacheEntryStatic(p))); |
phweiss | 26d3496 | 2019-10-29 13:37:54 +0100 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | private void checkPackageWithIntentFilters( |
| 489 | Function<ParsedPackage, ParsedPackage> converter) throws Exception { |
| 490 | ParsedPackage p = parsePackage( |
| 491 | "install_intent_filters.apk", R.raw.install_intent_filters, |
| 492 | converter); |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 493 | String packageName = "com.android.frameworks.servicestests.install_intent_filters"; |
phweiss | 26d3496 | 2019-10-29 13:37:54 +0100 | [diff] [blame] | 494 | |
| 495 | assertEquals(packageName, p.getPackageName()); |
| 496 | |
| 497 | findAndRemoveAppDetailsActivity(p); |
| 498 | |
phweiss | 26d3496 | 2019-10-29 13:37:54 +0100 | [diff] [blame] | 499 | assertEquals("Expected exactly one activity", 1, p.getActivities().size()); |
| 500 | assertEquals("Expected exactly one intent filter", |
Winson | f00c755 | 2020-01-28 12:52:01 -0800 | [diff] [blame] | 501 | 1, p.getActivities().get(0).getIntents().size()); |
phweiss | 26d3496 | 2019-10-29 13:37:54 +0100 | [diff] [blame] | 502 | assertEquals("Expected exactly one mime group in intent filter", |
Winson | f00c755 | 2020-01-28 12:52:01 -0800 | [diff] [blame] | 503 | 1, p.getActivities().get(0).getIntents().get(0).countMimeGroups()); |
phweiss | 26d3496 | 2019-10-29 13:37:54 +0100 | [diff] [blame] | 504 | assertTrue("Did not find expected mime group 'mime_group_1'", |
Winson | f00c755 | 2020-01-28 12:52:01 -0800 | [diff] [blame] | 505 | p.getActivities().get(0).getIntents().get(0).hasMimeGroup("mime_group_1")); |
Garrett Boyer | 5213666 | 2017-05-23 13:47:58 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Jiyong Park | 5b1b734 | 2019-01-18 09:45:17 +0900 | [diff] [blame] | 508 | @Test |
| 509 | public void testApexPackageInfoGeneration() throws Exception { |
Nikita Ioffe | ec67395 | 2019-06-28 15:46:04 +0100 | [diff] [blame] | 510 | String apexModuleName = "com.android.tzdata.apex"; |
| 511 | File apexFile = copyRawResourceToFile(apexModuleName, |
Jiyong Park | 5b1b734 | 2019-01-18 09:45:17 +0900 | [diff] [blame] | 512 | R.raw.com_android_tzdata); |
Gavin Corkery | ef44172 | 2019-05-09 17:02:10 +0100 | [diff] [blame] | 513 | ApexInfo apexInfo = new ApexInfo(); |
| 514 | apexInfo.isActive = true; |
| 515 | apexInfo.isFactory = false; |
Nikita Ioffe | ec67395 | 2019-06-28 15:46:04 +0100 | [diff] [blame] | 516 | apexInfo.moduleName = apexModuleName; |
| 517 | apexInfo.modulePath = apexFile.getPath(); |
Gavin Corkery | ef44172 | 2019-05-09 17:02:10 +0100 | [diff] [blame] | 518 | apexInfo.versionCode = 191000070; |
Mohammad Samiul Islam | 7aa7d2e | 2019-03-27 12:23:47 +0000 | [diff] [blame] | 519 | int flags = PackageManager.GET_META_DATA | PackageManager.GET_SIGNING_CERTIFICATES; |
Oli Lan | c2c7a22 | 2019-07-31 15:27:22 +0100 | [diff] [blame] | 520 | |
| 521 | PackageParser pp = new PackageParser(); |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 522 | PackageParser.Package p = pp.parsePackage(apexFile, flags, false); |
Oli Lan | c2c7a22 | 2019-07-31 15:27:22 +0100 | [diff] [blame] | 523 | PackageParser.collectCertificates(p, false); |
| 524 | PackageInfo pi = PackageParser.generatePackageInfo(p, apexInfo, flags); |
| 525 | |
Winson Chiu | e479015 | 2020-01-24 00:57:20 +0000 | [diff] [blame] | 526 | assertEquals("com.google.android.tzdata", pi.applicationInfo.packageName); |
Mohammad Samiul Islam | 935a156 | 2019-03-12 16:52:56 +0000 | [diff] [blame] | 527 | assertTrue(pi.applicationInfo.enabled); |
| 528 | assertEquals(28, pi.applicationInfo.targetSdkVersion); |
Mohammad Samiul Islam | 7aa7d2e | 2019-03-27 12:23:47 +0000 | [diff] [blame] | 529 | assertEquals(191000070, pi.applicationInfo.longVersionCode); |
| 530 | assertNotNull(pi.applicationInfo.metaData); |
| 531 | assertEquals(apexFile.getPath(), pi.applicationInfo.sourceDir); |
| 532 | assertEquals("Bundle[{com.android.vending.derived.apk.id=1}]", |
| 533 | pi.applicationInfo.metaData.toString()); |
Mohammad Samiul Islam | 935a156 | 2019-03-12 16:52:56 +0000 | [diff] [blame] | 534 | |
Winson Chiu | e479015 | 2020-01-24 00:57:20 +0000 | [diff] [blame] | 535 | assertEquals("com.google.android.tzdata", pi.packageName); |
Mohammad Samiul Islam | 7aa7d2e | 2019-03-27 12:23:47 +0000 | [diff] [blame] | 536 | assertEquals(191000070, pi.getLongVersionCode()); |
Jiyong Park | 5b1b734 | 2019-01-18 09:45:17 +0900 | [diff] [blame] | 537 | assertNotNull(pi.signingInfo); |
| 538 | assertTrue(pi.signingInfo.getApkContentsSigners().length > 0); |
Mohammad Samiul Islam | 935a156 | 2019-03-12 16:52:56 +0000 | [diff] [blame] | 539 | assertTrue(pi.isApex); |
Gavin Corkery | ef44172 | 2019-05-09 17:02:10 +0100 | [diff] [blame] | 540 | assertTrue((pi.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0); |
| 541 | assertTrue((pi.applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0); |
Jiyong Park | 5b1b734 | 2019-01-18 09:45:17 +0900 | [diff] [blame] | 542 | } |
Anton Hansson | b2f709d | 2020-01-09 10:25:23 +0000 | [diff] [blame] | 543 | |
| 544 | @Test |
| 545 | public void testUsesSdk() throws Exception { |
Oli Lan | 72ae447 | 2020-04-14 16:50:41 +0100 | [diff] [blame] | 546 | ParsedPackage pkg = |
| 547 | parsePackage("install_uses_sdk.apk_r0", R.raw.install_uses_sdk_r0, x -> x); |
| 548 | SparseIntArray minExtVers = pkg.getMinExtensionVersions(); |
| 549 | assertEquals(1, minExtVers.size()); |
| 550 | assertEquals(0, minExtVers.get(10000, -1)); |
| 551 | |
Anton Hansson | b2f709d | 2020-01-09 10:25:23 +0000 | [diff] [blame] | 552 | try { |
| 553 | parsePackage("install_uses_sdk.apk_r5", R.raw.install_uses_sdk_r5, x -> x); |
| 554 | fail("Expected parsing exception due to incompatible extension SDK version"); |
| 555 | } catch (PackageParser.PackageParserException expected) { |
| 556 | assertEquals(PackageManager.INSTALL_FAILED_OLDER_SDK, expected.error); |
| 557 | } |
| 558 | try { |
| 559 | parsePackage("install_uses_sdk.apk_q0", R.raw.install_uses_sdk_q0, x -> x); |
| 560 | fail("Expected parsing exception due to non-existent extension SDK"); |
| 561 | } catch (PackageParser.PackageParserException expected) { |
| 562 | assertEquals(PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED, expected.error); |
| 563 | } |
| 564 | try { |
| 565 | parsePackage("install_uses_sdk.apk_r", R.raw.install_uses_sdk_r, x -> x); |
| 566 | fail("Expected parsing exception due to unspecified extension SDK version"); |
| 567 | } catch (PackageParser.PackageParserException expected) { |
| 568 | assertEquals(PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED, expected.error); |
| 569 | } |
| 570 | try { |
| 571 | parsePackage("install_uses_sdk.apk_0", R.raw.install_uses_sdk_0, x -> x); |
| 572 | fail("Expected parsing exception due to unspecified extension SDK"); |
| 573 | } catch (PackageParser.PackageParserException expected) { |
| 574 | assertEquals(PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED, expected.error); |
| 575 | } |
| 576 | |
| 577 | } |
Alan Viverette | 82f4b263a | 2016-12-19 15:40:05 -0500 | [diff] [blame] | 578 | } |