Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | package com.android.server.notification; |
| 17 | |
| 18 | import static com.android.server.notification.ManagedServices.APPROVAL_BY_COMPONENT; |
| 19 | import static com.android.server.notification.ManagedServices.APPROVAL_BY_PACKAGE; |
| 20 | |
| 21 | import static junit.framework.Assert.assertEquals; |
| 22 | import static junit.framework.Assert.assertFalse; |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 23 | import static junit.framework.Assert.assertNotNull; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 24 | import static junit.framework.Assert.assertTrue; |
| 25 | |
Julia Reynolds | 8a4f2ab | 2019-04-10 10:08:21 -0400 | [diff] [blame] | 26 | import static org.mockito.ArgumentMatchers.anyString; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 27 | import static org.mockito.Matchers.any; |
| 28 | import static org.mockito.Matchers.anyInt; |
| 29 | import static org.mockito.Matchers.eq; |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 30 | import static org.mockito.Mockito.mock; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 31 | import static org.mockito.Mockito.never; |
| 32 | import static org.mockito.Mockito.times; |
| 33 | import static org.mockito.Mockito.verify; |
| 34 | import static org.mockito.Mockito.when; |
| 35 | |
| 36 | import android.content.ComponentName; |
| 37 | import android.content.Context; |
| 38 | import android.content.Intent; |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 39 | import android.content.ServiceConnection; |
Julia Reynolds | 8a4f2ab | 2019-04-10 10:08:21 -0400 | [diff] [blame] | 40 | import android.content.pm.ApplicationInfo; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 41 | import android.content.pm.IPackageManager; |
| 42 | import android.content.pm.PackageManager; |
| 43 | import android.content.pm.ResolveInfo; |
| 44 | import android.content.pm.ServiceInfo; |
| 45 | import android.content.pm.UserInfo; |
Julia Reynolds | fa206a4 | 2017-08-14 13:22:23 -0400 | [diff] [blame] | 46 | import android.os.Build; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 47 | import android.os.IBinder; |
| 48 | import android.os.IInterface; |
| 49 | import android.os.UserHandle; |
| 50 | import android.os.UserManager; |
| 51 | import android.provider.Settings; |
| 52 | import android.text.TextUtils; |
| 53 | import android.util.ArrayMap; |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 54 | import android.util.ArraySet; |
| 55 | import android.util.IntArray; |
| 56 | import android.util.SparseArray; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 57 | import android.util.Xml; |
| 58 | |
| 59 | import com.android.internal.util.FastXmlSerializer; |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 60 | import com.android.internal.util.XmlUtils; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 61 | import com.android.server.UiServiceTestCase; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 62 | |
| 63 | import com.google.android.collect.Lists; |
| 64 | |
| 65 | import org.junit.Before; |
| 66 | import org.junit.Test; |
| 67 | import org.mockito.Mock; |
| 68 | import org.mockito.MockitoAnnotations; |
| 69 | import org.mockito.invocation.InvocationOnMock; |
| 70 | import org.mockito.stubbing.Answer; |
| 71 | import org.xmlpull.v1.XmlPullParser; |
| 72 | import org.xmlpull.v1.XmlSerializer; |
| 73 | |
| 74 | import java.io.BufferedInputStream; |
| 75 | import java.io.BufferedOutputStream; |
| 76 | import java.io.ByteArrayInputStream; |
| 77 | import java.io.ByteArrayOutputStream; |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 78 | import java.nio.charset.StandardCharsets; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 79 | import java.util.ArrayList; |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 80 | import java.util.Arrays; |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 81 | import java.util.Collection; |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 82 | import java.util.Collections; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 83 | import java.util.List; |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 84 | import java.util.Set; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 85 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 86 | public class ManagedServicesTest extends UiServiceTestCase { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 87 | |
| 88 | @Mock |
| 89 | private IPackageManager mIpm; |
| 90 | @Mock |
| 91 | private PackageManager mPm; |
| 92 | @Mock |
| 93 | private UserManager mUm; |
| 94 | @Mock |
| 95 | private ManagedServices.UserProfiles mUserProfiles; |
| 96 | Object mLock = new Object(); |
| 97 | |
| 98 | UserInfo mZero = new UserInfo(0, "zero", 0); |
| 99 | UserInfo mTen = new UserInfo(10, "ten", 0); |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 100 | private String mDefaultsString; |
| 101 | private String mVersionString; |
| 102 | private final Set<ComponentName> mDefaults = new ArraySet(); |
| 103 | private ManagedServices mService; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 104 | |
| 105 | private static final String SETTING = "setting"; |
| 106 | private static final String SECONDARY_SETTING = "secondary_setting"; |
| 107 | |
| 108 | private ArrayMap<Integer, String> mExpectedPrimaryPackages; |
| 109 | private ArrayMap<Integer, String> mExpectedPrimaryComponentNames; |
| 110 | private ArrayMap<Integer, String> mExpectedSecondaryPackages; |
| 111 | private ArrayMap<Integer, String> mExpectedSecondaryComponentNames; |
| 112 | |
Julia Reynolds | 5aa13a4 | 2017-08-24 09:10:23 -0400 | [diff] [blame] | 113 | // type : user : list of approved |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 114 | private ArrayMap<Integer, ArrayMap<Integer, String>> mExpectedPrimary; |
| 115 | private ArrayMap<Integer, ArrayMap<Integer, String>> mExpectedSecondary; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 116 | |
| 117 | @Before |
| 118 | public void setUp() throws Exception { |
| 119 | MockitoAnnotations.initMocks(this); |
| 120 | |
| 121 | getContext().setMockPackageManager(mPm); |
| 122 | getContext().addMockSystemService(Context.USER_SERVICE, mUm); |
| 123 | |
| 124 | List<UserInfo> users = new ArrayList<>(); |
| 125 | users.add(mZero); |
| 126 | users.add(mTen); |
| 127 | users.add(new UserInfo(11, "11", 0)); |
| 128 | users.add(new UserInfo(12, "12", 0)); |
| 129 | for (UserInfo user : users) { |
| 130 | when(mUm.getUserInfo(eq(user.id))).thenReturn(user); |
| 131 | } |
| 132 | when(mUm.getUsers()).thenReturn(users); |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 133 | IntArray profileIds = new IntArray(); |
| 134 | profileIds.add(0); |
| 135 | profileIds.add(11); |
| 136 | profileIds.add(10); |
| 137 | profileIds.add(12); |
| 138 | when(mUserProfiles.getCurrentProfileIds()).thenReturn(profileIds); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 139 | |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 140 | mVersionString = "2"; |
| 141 | mExpectedPrimary = new ArrayMap<>(); |
| 142 | mExpectedSecondary = new ArrayMap<>(); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 143 | mExpectedPrimaryPackages = new ArrayMap<>(); |
| 144 | mExpectedPrimaryPackages.put(0, "this.is.a.package.name:another.package"); |
| 145 | mExpectedPrimaryPackages.put(10, "this.is.another.package"); |
| 146 | mExpectedPrimaryPackages.put(11, ""); |
| 147 | mExpectedPrimaryPackages.put(12, "bananas!"); |
| 148 | mExpectedPrimaryComponentNames = new ArrayMap<>(); |
| 149 | mExpectedPrimaryComponentNames.put(0, "this.is.a.package.name/Ba:another.package/B1"); |
| 150 | mExpectedPrimaryComponentNames.put(10, "this.is.another.package/M1"); |
| 151 | mExpectedPrimaryComponentNames.put(11, ""); |
| 152 | mExpectedPrimaryComponentNames.put(12, "bananas!/Bananas!"); |
| 153 | mExpectedPrimary.put(APPROVAL_BY_PACKAGE, mExpectedPrimaryPackages); |
| 154 | mExpectedPrimary.put(APPROVAL_BY_COMPONENT, mExpectedPrimaryComponentNames); |
| 155 | |
| 156 | mExpectedSecondaryComponentNames = new ArrayMap<>(); |
| 157 | mExpectedSecondaryComponentNames.put(0, "secondary/component.Name"); |
| 158 | mExpectedSecondaryComponentNames.put(10, |
| 159 | "this.is.another.package/with.Component:component/2:package/component2"); |
| 160 | mExpectedSecondaryPackages = new ArrayMap<>(); |
| 161 | mExpectedSecondaryPackages.put(0, "secondary"); |
| 162 | mExpectedSecondaryPackages.put(10, |
| 163 | "this.is.another.package:component:package"); |
| 164 | mExpectedSecondary.put(APPROVAL_BY_PACKAGE, mExpectedSecondaryPackages); |
| 165 | mExpectedSecondary.put(APPROVAL_BY_COMPONENT, mExpectedSecondaryComponentNames); |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 166 | mService = new TestManagedServices(getContext(), mLock, mUserProfiles, |
| 167 | mIpm, APPROVAL_BY_COMPONENT); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | @Test |
| 171 | public void testBackupAndRestore_migration() throws Exception { |
| 172 | for (int approvalLevel : new int[] {APPROVAL_BY_COMPONENT, APPROVAL_BY_PACKAGE}) { |
| 173 | ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, |
| 174 | mIpm, approvalLevel); |
| 175 | |
| 176 | for (int userId : mExpectedPrimary.get(approvalLevel).keySet()) { |
| 177 | service.onSettingRestored( |
| 178 | service.getConfig().secureSettingName, |
| 179 | mExpectedPrimary.get(approvalLevel).get(userId), |
Julia Reynolds | fa206a4 | 2017-08-14 13:22:23 -0400 | [diff] [blame] | 180 | Build.VERSION_CODES.O, userId); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 181 | } |
| 182 | verifyExpectedApprovedEntries(service, true); |
| 183 | |
| 184 | for (int userId : mExpectedSecondary.get(approvalLevel).keySet()) { |
| 185 | service.onSettingRestored(service.getConfig().secondarySettingName, |
Julia Reynolds | fa206a4 | 2017-08-14 13:22:23 -0400 | [diff] [blame] | 186 | mExpectedSecondary.get(approvalLevel).get(userId), Build.VERSION_CODES.O, |
| 187 | userId); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 188 | } |
| 189 | verifyExpectedApprovedEntries(service); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | @Test |
Julia Reynolds | fa206a4 | 2017-08-14 13:22:23 -0400 | [diff] [blame] | 194 | public void testBackupAndRestore_migration_preO() throws Exception { |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 195 | ArrayMap<Integer, String> backupPrimaryPackages = new ArrayMap<>(); |
Julia Reynolds | fa206a4 | 2017-08-14 13:22:23 -0400 | [diff] [blame] | 196 | backupPrimaryPackages.put(0, "backup.0:backup:0a"); |
| 197 | backupPrimaryPackages.put(10, "10.backup"); |
| 198 | backupPrimaryPackages.put(11, "eleven"); |
| 199 | backupPrimaryPackages.put(12, ""); |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 200 | ArrayMap<Integer, String> backupPrimaryComponentNames = new ArrayMap<>(); |
Julia Reynolds | fa206a4 | 2017-08-14 13:22:23 -0400 | [diff] [blame] | 201 | backupPrimaryComponentNames.put(0, "backup.first/whatever:a/b"); |
| 202 | backupPrimaryComponentNames.put(10, "again/M1"); |
| 203 | backupPrimaryComponentNames.put(11, "orange/youglad:itisnot/banana"); |
| 204 | backupPrimaryComponentNames.put(12, ""); |
| 205 | ArrayMap<Integer, ArrayMap<Integer, String>> backupPrimary = new ArrayMap<>(); |
| 206 | backupPrimary.put(APPROVAL_BY_PACKAGE, backupPrimaryPackages); |
| 207 | backupPrimary.put(APPROVAL_BY_COMPONENT, backupPrimaryComponentNames); |
| 208 | |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 209 | ArrayMap<Integer, String> backupSecondaryComponentNames = new ArrayMap<>(); |
Julia Reynolds | fa206a4 | 2017-08-14 13:22:23 -0400 | [diff] [blame] | 210 | backupSecondaryComponentNames.put(0, "secondary.1/component.Name"); |
| 211 | backupSecondaryComponentNames.put(10, |
| 212 | "this.is.another.package.backup/with.Component:component.backup/2"); |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 213 | ArrayMap<Integer, String> backupSecondaryPackages = new ArrayMap<>(); |
Julia Reynolds | fa206a4 | 2017-08-14 13:22:23 -0400 | [diff] [blame] | 214 | backupSecondaryPackages.put(0, ""); |
| 215 | backupSecondaryPackages.put(10, |
| 216 | "this.is.another.package.backup:package.backup"); |
| 217 | ArrayMap<Integer, ArrayMap<Integer, String>> backupSecondary = new ArrayMap<>(); |
| 218 | backupSecondary.put(APPROVAL_BY_PACKAGE, backupSecondaryPackages); |
| 219 | backupSecondary.put(APPROVAL_BY_COMPONENT, backupSecondaryComponentNames); |
| 220 | |
| 221 | for (int approvalLevel : new int[] {APPROVAL_BY_COMPONENT, APPROVAL_BY_PACKAGE}) { |
| 222 | ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, |
| 223 | mIpm, approvalLevel); |
| 224 | |
| 225 | // not an expected flow but a way to get data into the settings |
| 226 | for (int userId : mExpectedPrimary.get(approvalLevel).keySet()) { |
| 227 | service.onSettingRestored( |
| 228 | service.getConfig().secureSettingName, |
| 229 | mExpectedPrimary.get(approvalLevel).get(userId), |
| 230 | Build.VERSION_CODES.O, userId); |
| 231 | } |
| 232 | |
| 233 | for (int userId : mExpectedSecondary.get(approvalLevel).keySet()) { |
| 234 | service.onSettingRestored(service.getConfig().secondarySettingName, |
| 235 | mExpectedSecondary.get(approvalLevel).get(userId), Build.VERSION_CODES.O, |
| 236 | userId); |
| 237 | } |
| 238 | |
| 239 | // actual test |
| 240 | for (int userId : backupPrimary.get(approvalLevel).keySet()) { |
| 241 | service.onSettingRestored( |
| 242 | service.getConfig().secureSettingName, |
| 243 | backupPrimary.get(approvalLevel).get(userId), |
| 244 | Build.VERSION_CODES.N_MR1, userId); |
| 245 | } |
| 246 | verifyExpectedApprovedEntries(service, true); |
| 247 | |
| 248 | for (int userId : backupSecondary.get(approvalLevel).keySet()) { |
| 249 | service.onSettingRestored(service.getConfig().secondarySettingName, |
| 250 | backupSecondary.get(approvalLevel).get(userId), |
| 251 | Build.VERSION_CODES.N_MR1, userId); |
| 252 | } |
| 253 | verifyExpectedApprovedEntries(service); |
| 254 | verifyExpectedApprovedEntries(service, backupPrimary.get(approvalLevel)); |
| 255 | verifyExpectedApprovedEntries(service, backupSecondary.get(approvalLevel)); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | @Test |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 260 | public void testReadXml_migrationFromSettings() throws Exception { |
| 261 | for (int approvalLevel : new int[] {APPROVAL_BY_COMPONENT, APPROVAL_BY_PACKAGE}) { |
| 262 | ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, |
| 263 | mIpm, approvalLevel); |
| 264 | |
| 265 | // approved services aren't in xml |
| 266 | XmlPullParser parser = Xml.newPullParser(); |
| 267 | parser.setInput(new BufferedInputStream(new ByteArrayInputStream(new byte[]{})), |
| 268 | null); |
| 269 | writeExpectedValuesToSettings(approvalLevel); |
| 270 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 271 | service.migrateToXml(); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 272 | |
| 273 | verifyExpectedApprovedEntries(service); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | @Test |
| 278 | public void testReadXml() throws Exception { |
| 279 | for (int approvalLevel : new int[] {APPROVAL_BY_COMPONENT, APPROVAL_BY_PACKAGE}) { |
| 280 | ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, |
| 281 | mIpm, approvalLevel); |
| 282 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 283 | loadXml(service); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 284 | |
| 285 | verifyExpectedApprovedEntries(service); |
Julia Reynolds | 4552300 | 2017-09-12 09:43:57 -0400 | [diff] [blame] | 286 | |
| 287 | int[] invalidUsers = new int[] {98, 99}; |
| 288 | for (int invalidUser : invalidUsers) { |
| 289 | assertFalse("service type " + service.mApprovalLevel + ":" |
| 290 | + invalidUser + " is allowed for user " + invalidUser, |
| 291 | service.isPackageOrComponentAllowed( |
| 292 | String.valueOf(invalidUser), invalidUser)); |
| 293 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
| 297 | @Test |
Artem Iglikov | bec9617 | 2018-04-24 12:46:44 +0100 | [diff] [blame] | 298 | public void testReadXml_appendsListOfApprovedComponents() throws Exception { |
| 299 | for (int approvalLevel : new int[] {APPROVAL_BY_COMPONENT, APPROVAL_BY_PACKAGE}) { |
| 300 | ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, |
| 301 | mIpm, approvalLevel); |
| 302 | |
| 303 | String preApprovedPackage = "some.random.package"; |
| 304 | String preApprovedComponent = "some.random.package/C1"; |
| 305 | |
| 306 | List<String> packages = new ArrayList<>(); |
| 307 | packages.add(preApprovedPackage); |
| 308 | addExpectedServices(service, packages, 0); |
| 309 | |
| 310 | service.setPackageOrComponentEnabled(preApprovedComponent, 0, true, true); |
| 311 | |
| 312 | loadXml(service); |
| 313 | |
| 314 | verifyExpectedApprovedEntries(service); |
| 315 | |
| 316 | String verifyValue = (approvalLevel == APPROVAL_BY_COMPONENT) |
| 317 | ? preApprovedComponent |
| 318 | : preApprovedPackage; |
| 319 | assertTrue(service.isPackageOrComponentAllowed(verifyValue, 0)); |
| 320 | } |
| 321 | } |
| 322 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 323 | /** Test that restore ignores the user id attribute and applies the data to the target user. */ |
| 324 | @Test |
| 325 | public void testReadXml_onlyRestoresForTargetUser() throws Exception { |
| 326 | for (int approvalLevel : new int[] {APPROVAL_BY_COMPONENT, APPROVAL_BY_PACKAGE}) { |
| 327 | ManagedServices service = |
| 328 | new TestManagedServices( |
| 329 | getContext(), mLock, mUserProfiles, mIpm, approvalLevel); |
| 330 | String testPackage = "user.test.package"; |
| 331 | String testComponent = "user.test.component/C1"; |
| 332 | String resolvedValue = |
| 333 | (approvalLevel == APPROVAL_BY_COMPONENT) ? testComponent : testPackage; |
| 334 | XmlPullParser parser = |
| 335 | getParserWithEntries(service, getXmlEntry(resolvedValue, 0, true)); |
| 336 | |
| 337 | service.readXml(parser, null, true, 10); |
| 338 | |
| 339 | assertFalse(service.isPackageOrComponentAllowed(resolvedValue, 0)); |
| 340 | assertTrue(service.isPackageOrComponentAllowed(resolvedValue, 10)); |
| 341 | } |
| 342 | } |
| 343 | |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 344 | /** Test that restore ignores the user id attribute and applies the data to the target user. */ |
| 345 | @Test |
| 346 | public void testWriteReadXml_writeReadDefaults() throws Exception { |
| 347 | // setup |
| 348 | ManagedServices service1 = |
| 349 | new TestManagedServices( |
| 350 | getContext(), mLock, mUserProfiles, mIpm, APPROVAL_BY_COMPONENT); |
| 351 | ManagedServices service2 = |
| 352 | new TestManagedServices( |
| 353 | getContext(), mLock, mUserProfiles, mIpm, APPROVAL_BY_COMPONENT); |
| 354 | XmlSerializer serializer = new FastXmlSerializer(); |
| 355 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 356 | BufferedOutputStream outStream = new BufferedOutputStream(baos); |
| 357 | serializer.setOutput(outStream, "utf-8"); |
| 358 | |
| 359 | //data setup |
| 360 | service1.addDefaultComponentOrPackage("package/class"); |
| 361 | serializer.startDocument(null, true); |
| 362 | service1.writeXml(serializer, false, 0); |
| 363 | serializer.endDocument(); |
| 364 | outStream.flush(); |
| 365 | |
| 366 | final XmlPullParser parser = Xml.newPullParser(); |
| 367 | BufferedInputStream input = new BufferedInputStream( |
| 368 | new ByteArrayInputStream(baos.toByteArray())); |
| 369 | |
| 370 | parser.setInput(input, StandardCharsets.UTF_8.name()); |
| 371 | XmlUtils.beginDocument(parser, "test"); |
| 372 | service2.readXml(parser, null, false, 0); |
| 373 | ArraySet<ComponentName> defaults = service2.getDefaultComponents(); |
| 374 | |
| 375 | assertEquals(1, defaults.size()); |
| 376 | assertEquals(new ComponentName("package", "class"), defaults.valueAt(0)); |
| 377 | |
| 378 | } |
| 379 | |
| 380 | @Test |
| 381 | public void resetPackage_enableDefaultsOnly() { |
| 382 | // setup |
| 383 | ManagedServices service = |
| 384 | new TestManagedServices( |
| 385 | getContext(), mLock, mUserProfiles, mIpm, APPROVAL_BY_COMPONENT); |
| 386 | service.addApprovedList( |
| 387 | "package/not-default:another-package/not-default:package2/default", |
| 388 | 0, true); |
| 389 | service.addDefaultComponentOrPackage("package/default"); |
| 390 | service.addDefaultComponentOrPackage("package2/default"); |
| 391 | |
| 392 | ArrayMap<Boolean, ArrayList<ComponentName>> componentsToActivate = |
| 393 | service.resetComponents("package", 0); |
| 394 | |
| 395 | assertEquals(1, componentsToActivate.get(true).size()); |
| 396 | assertEquals(new ComponentName("package", "default"), |
| 397 | componentsToActivate.get(true).get(0)); |
| 398 | } |
| 399 | |
| 400 | |
| 401 | @Test |
| 402 | public void resetPackage_nonDefaultsRemoved() { |
| 403 | // setup |
| 404 | ManagedServices service = |
| 405 | new TestManagedServices( |
| 406 | getContext(), mLock, mUserProfiles, mIpm, APPROVAL_BY_COMPONENT); |
| 407 | service.addApprovedList( |
| 408 | "package/not-default:another-package/not-default:package2/default", |
| 409 | 0, true); |
| 410 | service.addDefaultComponentOrPackage("package/default"); |
| 411 | service.addDefaultComponentOrPackage("package2/default"); |
| 412 | |
| 413 | ArrayMap<Boolean, ArrayList<ComponentName>> componentsToActivate = |
| 414 | service.resetComponents("package", 0); |
| 415 | |
| 416 | assertEquals(1, componentsToActivate.get(true).size()); |
| 417 | assertEquals(new ComponentName("package", "not-default"), |
| 418 | componentsToActivate.get(false).get(0)); |
| 419 | } |
| 420 | |
| 421 | @Test |
| 422 | public void resetPackage_onlyDefaultsOnly() { |
| 423 | // setup |
| 424 | ManagedServices service = |
| 425 | new TestManagedServices( |
| 426 | getContext(), mLock, mUserProfiles, mIpm, APPROVAL_BY_COMPONENT); |
| 427 | service.addApprovedList( |
| 428 | "package/not-default:another-package/not-default:package2/default", |
| 429 | 0, true); |
| 430 | service.addDefaultComponentOrPackage("package/default"); |
| 431 | service.addDefaultComponentOrPackage("package2/default"); |
| 432 | |
| 433 | assertEquals(3, service.getAllowedComponents(0).size()); |
| 434 | |
| 435 | service.resetComponents("package", 0); |
| 436 | |
| 437 | List<ComponentName> components = service.getAllowedComponents(0); |
| 438 | assertEquals(3, components.size()); |
| 439 | assertTrue(components.contains(new ComponentName("package", "default"))); |
| 440 | } |
| 441 | |
| 442 | @Test |
| 443 | public void resetPackage_affectCurrentUserOnly() { |
| 444 | // setup |
| 445 | ManagedServices service = |
| 446 | new TestManagedServices( |
| 447 | getContext(), mLock, mUserProfiles, mIpm, APPROVAL_BY_COMPONENT); |
| 448 | service.addApprovedList( |
| 449 | "package/not-default:another-package/not-default:package2/default", |
| 450 | 0, true); |
| 451 | service.addApprovedList( |
| 452 | "package/not-default:another-package/not-default:package2/default", |
| 453 | 1, true); |
| 454 | service.addDefaultComponentOrPackage("package/default"); |
| 455 | service.addDefaultComponentOrPackage("package2/default"); |
| 456 | |
| 457 | service.resetComponents("package", 0); |
| 458 | |
| 459 | List<ComponentName> components = service.getAllowedComponents(1); |
| 460 | assertEquals(3, components.size()); |
| 461 | } |
| 462 | |
| 463 | @Test |
| 464 | public void resetPackage_samePackageMultipleClasses() { |
| 465 | // setup |
| 466 | ManagedServices service = |
| 467 | new TestManagedServices( |
| 468 | getContext(), mLock, mUserProfiles, mIpm, APPROVAL_BY_COMPONENT); |
| 469 | service.addApprovedList( |
| 470 | "package/not-default:another-package/not-default:package2/default", |
| 471 | 0, true); |
| 472 | service.addApprovedList( |
| 473 | "package/class:another-package/class:package2/class", |
| 474 | 0, true); |
| 475 | service.addDefaultComponentOrPackage("package/default"); |
| 476 | service.addDefaultComponentOrPackage("package2/default"); |
| 477 | |
| 478 | service.resetComponents("package", 0); |
| 479 | |
| 480 | List<ComponentName> components = service.getAllowedComponents(0); |
| 481 | assertEquals(5, components.size()); |
| 482 | assertTrue(components.contains(new ComponentName("package", "default"))); |
| 483 | } |
| 484 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 485 | /** Test that backup only writes packages/components that belong to the target user. */ |
| 486 | @Test |
| 487 | public void testWriteXml_onlyBackupsForTargetUser() throws Exception { |
| 488 | for (int approvalLevel : new int[] {APPROVAL_BY_COMPONENT, APPROVAL_BY_PACKAGE}) { |
| 489 | ManagedServices service = |
| 490 | new TestManagedServices( |
| 491 | getContext(), mLock, mUserProfiles, mIpm, approvalLevel); |
| 492 | // Set up components. |
| 493 | String testPackage0 = "user0.test.package"; |
| 494 | String testComponent0 = "user0.test.component/C1"; |
| 495 | String testPackage10 = "user10.test.package"; |
| 496 | String testComponent10 = "user10.test.component/C1"; |
| 497 | String resolvedValue0 = |
| 498 | (approvalLevel == APPROVAL_BY_COMPONENT) ? testComponent0 : testPackage0; |
| 499 | String resolvedValue10 = |
| 500 | (approvalLevel == APPROVAL_BY_COMPONENT) ? testComponent10 : testPackage10; |
| 501 | addExpectedServices( |
| 502 | service, Collections.singletonList(service.getPackageName(resolvedValue0)), 0); |
| 503 | addExpectedServices( |
| 504 | service, |
| 505 | Collections.singletonList(service.getPackageName(resolvedValue10)), |
| 506 | 10); |
| 507 | XmlPullParser parser = |
| 508 | getParserWithEntries( |
| 509 | service, |
| 510 | getXmlEntry(resolvedValue0, 0, true), |
| 511 | getXmlEntry(resolvedValue10, 10, true)); |
| 512 | service.readXml(parser, null, false, UserHandle.USER_ALL); |
| 513 | |
| 514 | // Write backup. |
| 515 | XmlSerializer serializer = new FastXmlSerializer(); |
| 516 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 517 | serializer.setOutput(new BufferedOutputStream(baos), "utf-8"); |
| 518 | serializer.startDocument(null, true); |
| 519 | service.writeXml(serializer, true, 10); |
| 520 | serializer.endDocument(); |
| 521 | serializer.flush(); |
| 522 | |
| 523 | // Reset values. |
| 524 | service.setPackageOrComponentEnabled(resolvedValue0, 0, true, false); |
| 525 | service.setPackageOrComponentEnabled(resolvedValue10, 10, true, false); |
| 526 | |
| 527 | // Parse backup via restore. |
| 528 | XmlPullParser restoreParser = Xml.newPullParser(); |
| 529 | restoreParser.setInput( |
| 530 | new BufferedInputStream(new ByteArrayInputStream(baos.toByteArray())), null); |
| 531 | restoreParser.nextTag(); |
| 532 | service.readXml(restoreParser, null, true, 10); |
| 533 | |
| 534 | assertFalse(service.isPackageOrComponentAllowed(resolvedValue0, 0)); |
| 535 | assertFalse(service.isPackageOrComponentAllowed(resolvedValue0, 10)); |
| 536 | assertTrue(service.isPackageOrComponentAllowed(resolvedValue10, 10)); |
| 537 | } |
| 538 | } |
| 539 | |
Artem Iglikov | bec9617 | 2018-04-24 12:46:44 +0100 | [diff] [blame] | 540 | @Test |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 541 | public void testWriteXml_trimsMissingServices() throws Exception { |
| 542 | for (int approvalLevel : new int[] {APPROVAL_BY_COMPONENT, APPROVAL_BY_PACKAGE}) { |
| 543 | ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, |
| 544 | mIpm, approvalLevel); |
| 545 | loadXml(service); |
| 546 | |
| 547 | // remove missing |
| 548 | mExpectedPrimaryPackages.put(0, "another.package"); |
| 549 | mExpectedPrimaryPackages.remove(12); |
| 550 | mExpectedPrimaryComponentNames.put(0, "another.package/B1"); |
| 551 | mExpectedPrimaryComponentNames.remove(12); |
| 552 | mExpectedSecondaryPackages.put(10, "this.is.another.package:component"); |
| 553 | mExpectedSecondaryComponentNames.put( |
| 554 | 10, "this.is.another.package/with.Component:component/2"); |
| 555 | |
| 556 | for (UserInfo userInfo : mUm.getUsers()) { |
| 557 | List<String> entriesExpectedToHaveServices = new ArrayList<>(); |
| 558 | if (mExpectedPrimary.get(approvalLevel).containsKey(userInfo.id)) { |
| 559 | for (String packageOrComponent : |
| 560 | mExpectedPrimary.get(approvalLevel).get(userInfo.id).split(":")) { |
| 561 | if (!TextUtils.isEmpty(packageOrComponent)) { |
| 562 | entriesExpectedToHaveServices.add( |
| 563 | service.getPackageName(packageOrComponent)); |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | if (mExpectedSecondary.get(approvalLevel).containsKey(userInfo.id)) { |
| 568 | for (String packageOrComponent : |
| 569 | mExpectedSecondary.get(approvalLevel).get(userInfo.id).split(":")) { |
| 570 | if (!TextUtils.isEmpty(packageOrComponent)) { |
| 571 | entriesExpectedToHaveServices.add( |
| 572 | service.getPackageName(packageOrComponent)); |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | addExpectedServices(service, entriesExpectedToHaveServices, userInfo.id); |
| 577 | } |
| 578 | |
| 579 | XmlSerializer serializer = new FastXmlSerializer(); |
| 580 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 581 | serializer.setOutput(new BufferedOutputStream(baos), "utf-8"); |
| 582 | serializer.startDocument(null, true); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 583 | for (UserInfo userInfo : mUm.getUsers()) { |
| 584 | service.writeXml(serializer, true, userInfo.id); |
| 585 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 586 | serializer.endDocument(); |
| 587 | serializer.flush(); |
| 588 | |
| 589 | XmlPullParser parser = Xml.newPullParser(); |
| 590 | parser.setInput(new BufferedInputStream( |
| 591 | new ByteArrayInputStream(baos.toByteArray())), null); |
| 592 | parser.nextTag(); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 593 | for (UserInfo userInfo : mUm.getUsers()) { |
| 594 | service.readXml(parser, null, true, userInfo.id); |
| 595 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 596 | |
| 597 | verifyExpectedApprovedEntries(service); |
| 598 | assertFalse(service.isPackageOrComponentAllowed("this.is.a.package.name", 0)); |
| 599 | assertFalse(service.isPackageOrComponentAllowed("bananas!", 12)); |
| 600 | assertFalse(service.isPackageOrComponentAllowed("package/component2", 10)); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | @Test |
| 605 | public void testWriteXml_writesSetting() throws Exception { |
| 606 | for (int approvalLevel : new int[] {APPROVAL_BY_COMPONENT, APPROVAL_BY_PACKAGE}) { |
| 607 | ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, |
| 608 | mIpm, approvalLevel); |
| 609 | loadXml(service); |
| 610 | |
| 611 | XmlSerializer serializer = new FastXmlSerializer(); |
| 612 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 613 | serializer.setOutput(new BufferedOutputStream(baos), "utf-8"); |
| 614 | serializer.startDocument(null, true); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 615 | service.writeXml(serializer, false, UserHandle.USER_ALL); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 616 | serializer.endDocument(); |
| 617 | serializer.flush(); |
| 618 | |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 619 | for (int userId : mUserProfiles.getCurrentProfileIds().toArray()) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 620 | List<String> expected = |
| 621 | stringToList(mExpectedPrimary.get(approvalLevel).get(userId)); |
| 622 | List<String> actual = stringToList(Settings.Secure.getStringForUser( |
| 623 | getContext().getContentResolver(), |
| 624 | service.getConfig().secureSettingName, userId)); |
| 625 | assertContentsInAnyOrder(actual, expected); |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | @Test |
| 631 | public void rebindServices_onlyBindsExactMatchesIfComponent() throws Exception { |
| 632 | // If the primary and secondary lists contain component names, only those components within |
| 633 | // the package should be matched |
| 634 | ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, |
| 635 | mIpm, |
| 636 | ManagedServices.APPROVAL_BY_COMPONENT); |
| 637 | |
| 638 | List<String> packages = new ArrayList<>(); |
| 639 | packages.add("package"); |
| 640 | packages.add("anotherPackage"); |
| 641 | addExpectedServices(service, packages, 0); |
| 642 | |
| 643 | // only 2 components are approved per package |
| 644 | mExpectedPrimaryComponentNames.clear(); |
| 645 | mExpectedPrimaryComponentNames.put(0, "package/C1:package/C2"); |
| 646 | mExpectedSecondaryComponentNames.clear(); |
| 647 | mExpectedSecondaryComponentNames.put(0, "anotherPackage/C1:anotherPackage/C2"); |
| 648 | |
| 649 | loadXml(service); |
| 650 | |
| 651 | // verify the 2 components per package are enabled (bound) |
| 652 | verifyExpectedBoundEntries(service, true); |
| 653 | verifyExpectedBoundEntries(service, false); |
| 654 | |
| 655 | // verify the last component per package is not enabled/we don't try to bind to it |
| 656 | for (String pkg : packages) { |
| 657 | ComponentName unapprovedAdditionalComponent = |
| 658 | ComponentName.unflattenFromString(pkg + "/C3"); |
| 659 | assertFalse( |
| 660 | service.isComponentEnabledForCurrentProfiles( |
| 661 | unapprovedAdditionalComponent)); |
| 662 | verify(mIpm, never()).getServiceInfo( |
| 663 | eq(unapprovedAdditionalComponent), anyInt(), anyInt()); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | @Test |
| 668 | public void rebindServices_bindsEverythingInAPackage() throws Exception { |
| 669 | // If the primary and secondary lists contain packages, all components within those packages |
| 670 | // should be bound |
| 671 | ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, mIpm, |
| 672 | APPROVAL_BY_PACKAGE); |
| 673 | |
| 674 | List<String> packages = new ArrayList<>(); |
| 675 | packages.add("package"); |
| 676 | packages.add("packagea"); |
| 677 | addExpectedServices(service, packages, 0); |
| 678 | |
| 679 | // 2 approved packages |
| 680 | mExpectedPrimaryPackages.clear(); |
| 681 | mExpectedPrimaryPackages.put(0, "package"); |
| 682 | mExpectedSecondaryPackages.clear(); |
| 683 | mExpectedSecondaryPackages.put(0, "packagea"); |
| 684 | |
| 685 | loadXml(service); |
| 686 | |
| 687 | // verify the 3 components per package are enabled (bound) |
| 688 | verifyExpectedBoundEntries(service, true); |
| 689 | verifyExpectedBoundEntries(service, false); |
| 690 | } |
| 691 | |
| 692 | @Test |
| 693 | public void testPackageUninstall_packageNoLongerInApprovedList() throws Exception { |
| 694 | for (int approvalLevel : new int[] {APPROVAL_BY_COMPONENT, APPROVAL_BY_PACKAGE}) { |
| 695 | ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, |
| 696 | mIpm, approvalLevel); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 697 | writeExpectedValuesToSettings(approvalLevel); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 698 | service.migrateToXml(); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 699 | |
| 700 | mExpectedPrimaryPackages.put(0, "another.package"); |
| 701 | mExpectedPrimaryComponentNames.put(0, "another.package/B1"); |
| 702 | service.onPackagesChanged(true, new String[]{"this.is.a.package.name"}, new int[]{103}); |
| 703 | |
| 704 | verifyExpectedApprovedEntries(service); |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | @Test |
| 709 | public void testPackageUninstall_componentNoLongerInApprovedList() throws Exception { |
| 710 | for (int approvalLevel : new int[] {APPROVAL_BY_COMPONENT, APPROVAL_BY_PACKAGE}) { |
| 711 | ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, |
| 712 | mIpm, approvalLevel); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 713 | writeExpectedValuesToSettings(approvalLevel); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 714 | service.migrateToXml(); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 715 | |
| 716 | mExpectedSecondaryComponentNames.put(10, "component/2"); |
| 717 | mExpectedSecondaryPackages.put(10, "component"); |
| 718 | service.onPackagesChanged(true, new String[]{"this.is.another.package"}, new int[]{ |
| 719 | UserHandle.PER_USER_RANGE + 1}); |
| 720 | |
| 721 | verifyExpectedApprovedEntries(service); |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | @Test |
Julia Reynolds | 1459074 | 2018-07-30 09:25:35 -0400 | [diff] [blame] | 726 | public void testIsPackageAllowed() { |
| 727 | for (int approvalLevel : new int[] {APPROVAL_BY_COMPONENT, APPROVAL_BY_PACKAGE}) { |
| 728 | ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, |
| 729 | mIpm, approvalLevel); |
| 730 | writeExpectedValuesToSettings(approvalLevel); |
| 731 | service.migrateToXml(); |
| 732 | |
| 733 | verifyExpectedApprovedPackages(service); |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | @Test |
| 738 | public void testUpgradeAppBindsNewServices() throws Exception { |
| 739 | // If the primary and secondary lists contain component names, only those components within |
| 740 | // the package should be matched |
| 741 | ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, |
| 742 | mIpm, |
| 743 | ManagedServices.APPROVAL_BY_PACKAGE); |
| 744 | |
| 745 | List<String> packages = new ArrayList<>(); |
| 746 | packages.add("package"); |
| 747 | addExpectedServices(service, packages, 0); |
| 748 | |
| 749 | // only 2 components are approved per package |
| 750 | mExpectedPrimaryComponentNames.clear(); |
| 751 | mExpectedPrimaryPackages.clear(); |
| 752 | mExpectedPrimaryComponentNames.put(0, "package/C1:package/C2"); |
| 753 | mExpectedSecondaryComponentNames.clear(); |
| 754 | mExpectedSecondaryPackages.clear(); |
| 755 | |
| 756 | loadXml(service); |
| 757 | |
| 758 | // new component expected |
| 759 | mExpectedPrimaryComponentNames.put(0, "package/C1:package/C2:package/C3"); |
| 760 | |
| 761 | service.onPackagesChanged(false, new String[]{"package"}, new int[]{0}); |
| 762 | |
| 763 | // verify the 3 components per package are enabled (bound) |
| 764 | verifyExpectedBoundEntries(service, true); |
| 765 | |
| 766 | // verify the last component per package is not enabled/we don't try to bind to it |
| 767 | for (String pkg : packages) { |
| 768 | ComponentName unapprovedAdditionalComponent = |
| 769 | ComponentName.unflattenFromString(pkg + "/C3"); |
| 770 | assertFalse( |
| 771 | service.isComponentEnabledForCurrentProfiles( |
| 772 | unapprovedAdditionalComponent)); |
| 773 | verify(mIpm, never()).getServiceInfo( |
| 774 | eq(unapprovedAdditionalComponent), anyInt(), anyInt()); |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | @Test |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 779 | public void testSetPackageOrComponentEnabled() throws Exception { |
| 780 | for (int approvalLevel : new int[] {APPROVAL_BY_COMPONENT, APPROVAL_BY_PACKAGE}) { |
| 781 | ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, |
| 782 | mIpm, approvalLevel); |
| 783 | ArrayMap<Integer, ArrayList<String>> expectedEnabled = new ArrayMap<>(); |
| 784 | expectedEnabled.put(0, |
| 785 | Lists.newArrayList(new String[]{"package/Comp", "package/C2", "again/M4"})); |
| 786 | expectedEnabled.put(10, |
| 787 | Lists.newArrayList(new String[]{"user10package/B", "user10/Component", |
| 788 | "user10package1/K", "user10.3/Component", "user10package2/L", |
| 789 | "user10.4/Component"})); |
| 790 | |
| 791 | for (int userId : expectedEnabled.keySet()) { |
| 792 | ArrayList<String> expectedForUser = expectedEnabled.get(userId); |
| 793 | for (int i = 0; i < expectedForUser.size(); i++) { |
| 794 | boolean primary = i % 2 == 0; |
| 795 | service.setPackageOrComponentEnabled(expectedForUser.get(i), userId, primary, |
| 796 | true); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | // verify everything added is approved |
| 801 | for (int userId : expectedEnabled.keySet()) { |
| 802 | ArrayList<String> expectedForUser = expectedEnabled.get(userId); |
| 803 | for (int i = 0; i < expectedForUser.size(); i++) { |
| 804 | String verifyValue = (approvalLevel == APPROVAL_BY_COMPONENT) |
| 805 | ? expectedForUser.get(i) |
| 806 | : service.getPackageName(expectedForUser.get(i)); |
| 807 | assertTrue("Not allowed: user: " + userId + " entry: " + verifyValue |
| 808 | + " for approval level " + approvalLevel, |
| 809 | service.isPackageOrComponentAllowed(verifyValue, userId)); |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | ArrayMap<Integer, ArrayList<String>> expectedNoAccess = new ArrayMap<>(); |
| 814 | for (int userId : expectedEnabled.keySet()) { |
| 815 | ArrayList<String> expectedForUser = expectedEnabled.get(userId); |
| 816 | for (int i = expectedForUser.size() - 1; i >= 0; i--) { |
| 817 | ArrayList<String> removed = new ArrayList<>(); |
| 818 | if (i % 3 == 0) { |
| 819 | String revokeAccessFor = expectedForUser.remove(i); |
| 820 | removed.add(revokeAccessFor); |
| 821 | service.setPackageOrComponentEnabled( |
| 822 | revokeAccessFor, userId, i % 2 == 0, false); |
| 823 | } |
| 824 | expectedNoAccess.put(userId, removed); |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | // verify everything still there is approved |
| 829 | for (int userId : expectedEnabled.keySet()) { |
| 830 | ArrayList<String> expectedForUser = expectedEnabled.get(userId); |
| 831 | for (int i = 0; i < expectedForUser.size(); i++) { |
| 832 | String verifyValue = (approvalLevel == APPROVAL_BY_COMPONENT) |
| 833 | ? expectedForUser.get(i) |
| 834 | : service.getPackageName(expectedForUser.get(i)); |
| 835 | assertTrue("Not allowed: user: " + userId + " entry: " + verifyValue, |
| 836 | service.isPackageOrComponentAllowed(verifyValue, userId)); |
| 837 | } |
| 838 | } |
| 839 | // verify everything removed isn't |
| 840 | for (int userId : expectedNoAccess.keySet()) { |
| 841 | ArrayList<String> notExpectedForUser = expectedNoAccess.get(userId); |
| 842 | for (int i = 0; i < notExpectedForUser.size(); i++) { |
| 843 | assertFalse( |
| 844 | "Is allowed: user: " + userId + " entry: " + notExpectedForUser.get(i), |
| 845 | service.isPackageOrComponentAllowed(notExpectedForUser.get(i), userId)); |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | @Test |
Julia Reynolds | 4c456dd | 2019-01-07 12:22:00 -0500 | [diff] [blame] | 852 | public void testGetAllowedPackages_byUser() throws Exception { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 853 | for (int approvalLevel : new int[] {APPROVAL_BY_COMPONENT, APPROVAL_BY_PACKAGE}) { |
| 854 | ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, |
| 855 | mIpm, approvalLevel); |
| 856 | loadXml(service); |
| 857 | |
| 858 | List<String> allowedPackagesForUser0 = new ArrayList<>(); |
| 859 | allowedPackagesForUser0.add("this.is.a.package.name"); |
| 860 | allowedPackagesForUser0.add("another.package"); |
| 861 | allowedPackagesForUser0.add("secondary"); |
| 862 | |
| 863 | List<String> actual = service.getAllowedPackages(0); |
| 864 | assertEquals(3, actual.size()); |
| 865 | for (String pkg : allowedPackagesForUser0) { |
| 866 | assertTrue(actual.contains(pkg)); |
| 867 | } |
| 868 | |
| 869 | List<String> allowedPackagesForUser10 = new ArrayList<>(); |
| 870 | allowedPackagesForUser10.add("this.is.another.package"); |
| 871 | allowedPackagesForUser10.add("package"); |
| 872 | allowedPackagesForUser10.add("this.is.another.package"); |
| 873 | allowedPackagesForUser10.add("component"); |
| 874 | |
| 875 | actual = service.getAllowedPackages(10); |
| 876 | assertEquals(4, actual.size()); |
| 877 | for (String pkg : allowedPackagesForUser10) { |
| 878 | assertTrue(actual.contains(pkg)); |
| 879 | } |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | @Test |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 884 | public void testGetAllowedComponentsByUser() throws Exception { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 885 | ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, mIpm, |
| 886 | APPROVAL_BY_COMPONENT); |
| 887 | loadXml(service); |
| 888 | |
| 889 | List<ComponentName> expected = new ArrayList<>(); |
| 890 | expected.add(ComponentName.unflattenFromString("this.is.another.package/M1")); |
| 891 | expected.add(ComponentName.unflattenFromString("this.is.another.package/with.Component")); |
| 892 | expected.add(ComponentName.unflattenFromString("component/2")); |
| 893 | expected.add(ComponentName.unflattenFromString("package/component2")); |
| 894 | |
| 895 | List<ComponentName> actual = service.getAllowedComponents(10); |
| 896 | |
| 897 | assertContentsInAnyOrder(expected, actual); |
| 898 | |
| 899 | assertEquals(expected.size(), actual.size()); |
| 900 | |
| 901 | for (ComponentName cn : expected) { |
| 902 | assertTrue("Actual missing " + cn, actual.contains(cn)); |
| 903 | } |
| 904 | |
| 905 | for (ComponentName cn : actual) { |
| 906 | assertTrue("Actual contains extra " + cn, expected.contains(cn)); |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | @Test |
| 911 | public void testGetAllowedComponents_approvalByPackage() throws Exception { |
| 912 | ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, mIpm, |
| 913 | APPROVAL_BY_PACKAGE); |
| 914 | loadXml(service); |
| 915 | |
| 916 | assertEquals(0, service.getAllowedComponents(10).size()); |
| 917 | } |
| 918 | |
Julia Reynolds | 5aa13a4 | 2017-08-24 09:10:23 -0400 | [diff] [blame] | 919 | @Test |
Julia Reynolds | 4c456dd | 2019-01-07 12:22:00 -0500 | [diff] [blame] | 920 | public void testGetAllowedPackages() throws Exception { |
| 921 | ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, |
| 922 | mIpm, APPROVAL_BY_COMPONENT); |
| 923 | loadXml(service); |
| 924 | service.mApprovalLevel = APPROVAL_BY_PACKAGE; |
| 925 | loadXml(service); |
| 926 | |
| 927 | List<String> allowedPackages = new ArrayList<>(); |
| 928 | allowedPackages.add("this.is.a.package.name"); |
| 929 | allowedPackages.add("another.package"); |
| 930 | allowedPackages.add("secondary"); |
| 931 | allowedPackages.add("this.is.another.package"); |
| 932 | allowedPackages.add("package"); |
| 933 | allowedPackages.add("component"); |
| 934 | allowedPackages.add("bananas!"); |
| 935 | |
| 936 | Set<String> actual = service.getAllowedPackages(); |
| 937 | assertEquals(allowedPackages.size(), actual.size()); |
| 938 | for (String pkg : allowedPackages) { |
| 939 | assertTrue(actual.contains(pkg)); |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | @Test |
Julia Reynolds | 5aa13a4 | 2017-08-24 09:10:23 -0400 | [diff] [blame] | 944 | public void testOnUserRemoved() throws Exception { |
| 945 | for (int approvalLevel : new int[] {APPROVAL_BY_COMPONENT, APPROVAL_BY_PACKAGE}) { |
| 946 | ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, |
| 947 | mIpm, approvalLevel); |
| 948 | loadXml(service); |
| 949 | |
| 950 | ArrayMap<Integer, String> verifyMap = mExpectedPrimary.get(service.mApprovalLevel); |
| 951 | String user0 = verifyMap.remove(0); |
| 952 | verifyMap = mExpectedSecondary.get(service.mApprovalLevel); |
| 953 | user0 = user0 + ":" + verifyMap.remove(0); |
| 954 | |
| 955 | service.onUserRemoved(0); |
| 956 | |
| 957 | for (String verifyValue : user0.split(":")) { |
| 958 | if (!TextUtils.isEmpty(verifyValue)) { |
| 959 | assertFalse("service type " + service.mApprovalLevel + ":" + verifyValue |
| 960 | + " is still allowed", |
| 961 | service.isPackageOrComponentAllowed(verifyValue, 0)); |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | verifyExpectedApprovedEntries(service); |
| 966 | } |
| 967 | } |
| 968 | |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 969 | @Test |
| 970 | public void testIsSameUser() { |
| 971 | IInterface service = mock(IInterface.class); |
| 972 | when(service.asBinder()).thenReturn(mock(IBinder.class)); |
| 973 | ManagedServices services = new TestManagedServices(getContext(), mLock, mUserProfiles, |
| 974 | mIpm, APPROVAL_BY_PACKAGE); |
| 975 | services.registerService(service, null, 10); |
| 976 | ManagedServices.ManagedServiceInfo info = services.checkServiceTokenLocked(service); |
| 977 | info.isSystem = true; |
| 978 | |
| 979 | assertFalse(services.isSameUser(service, 0)); |
| 980 | assertTrue(services.isSameUser(service, 10)); |
| 981 | } |
| 982 | |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 983 | @Test |
| 984 | public void testGetAllowedComponents() throws Exception { |
| 985 | ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, mIpm, |
| 986 | APPROVAL_BY_COMPONENT); |
| 987 | loadXml(service); |
| 988 | |
| 989 | SparseArray<ArraySet<ComponentName>> expected = new SparseArray<>(); |
| 990 | |
| 991 | ArraySet<ComponentName> expected10 = new ArraySet<>(); |
| 992 | expected10.add(ComponentName.unflattenFromString("this.is.another.package/M1")); |
| 993 | expected10.add(ComponentName.unflattenFromString("this.is.another.package/with.Component")); |
| 994 | expected10.add(ComponentName.unflattenFromString("component/2")); |
| 995 | expected10.add(ComponentName.unflattenFromString("package/component2")); |
| 996 | expected.put(10, expected10); |
| 997 | ArraySet<ComponentName> expected0 = new ArraySet<>(); |
| 998 | expected0.add(ComponentName.unflattenFromString("secondary/component.Name")); |
| 999 | expected0.add(ComponentName.unflattenFromString("this.is.a.package.name/Ba")); |
| 1000 | expected0.add(ComponentName.unflattenFromString("another.package/B1")); |
| 1001 | expected.put(0, expected0); |
| 1002 | ArraySet<ComponentName> expected12 = new ArraySet<>(); |
| 1003 | expected12.add(ComponentName.unflattenFromString("bananas!/Bananas!")); |
| 1004 | expected.put(12, expected12); |
| 1005 | expected.put(11, new ArraySet<>()); |
| 1006 | |
| 1007 | SparseArray<ArraySet<ComponentName>> actual = |
| 1008 | service.getAllowedComponents(mUserProfiles.getCurrentProfileIds()); |
| 1009 | |
| 1010 | assertContentsInAnyOrder(expected, actual); |
| 1011 | } |
| 1012 | |
| 1013 | @Test |
| 1014 | public void testPopulateComponentsToUnbind_forceRebind() { |
| 1015 | ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, mIpm, |
| 1016 | APPROVAL_BY_COMPONENT); |
| 1017 | |
| 1018 | IInterface iInterface = mock(IInterface.class); |
| 1019 | when(iInterface.asBinder()).thenReturn(mock(IBinder.class)); |
| 1020 | |
| 1021 | ManagedServices.ManagedServiceInfo service0 = service.new ManagedServiceInfo( |
| 1022 | iInterface, ComponentName.unflattenFromString("a/a"), 0, false, |
| 1023 | mock(ServiceConnection.class), 26); |
| 1024 | ManagedServices.ManagedServiceInfo service10 = service.new ManagedServiceInfo( |
| 1025 | iInterface, ComponentName.unflattenFromString("b/b"), 10, false, |
| 1026 | mock(ServiceConnection.class), 26); |
| 1027 | Set<ManagedServices.ManagedServiceInfo> removableBoundServices = new ArraySet<>(); |
| 1028 | removableBoundServices.add(service0); |
| 1029 | removableBoundServices.add(service10); |
| 1030 | |
| 1031 | SparseArray<Set<ComponentName>> allowedComponentsToBind = new SparseArray<>(); |
| 1032 | Set<ComponentName> allowed0 = new ArraySet<>(); |
| 1033 | allowed0.add(ComponentName.unflattenFromString("a/a")); |
| 1034 | allowedComponentsToBind.put(0, allowed0); |
| 1035 | Set<ComponentName> allowed10 = new ArraySet<>(); |
| 1036 | allowed10.add(ComponentName.unflattenFromString("b/b")); |
| 1037 | allowedComponentsToBind.put(10, allowed10); |
| 1038 | |
| 1039 | SparseArray<Set<ComponentName>> componentsToUnbind = new SparseArray<>(); |
| 1040 | |
| 1041 | service.populateComponentsToUnbind(true, removableBoundServices, allowedComponentsToBind, |
| 1042 | componentsToUnbind); |
| 1043 | |
| 1044 | assertEquals(2, componentsToUnbind.size()); |
| 1045 | assertTrue(componentsToUnbind.get(0).contains(ComponentName.unflattenFromString("a/a"))); |
| 1046 | assertTrue(componentsToUnbind.get(10).contains(ComponentName.unflattenFromString("b/b"))); |
| 1047 | } |
| 1048 | |
| 1049 | @Test |
| 1050 | public void testPopulateComponentsToUnbind() { |
| 1051 | ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, mIpm, |
| 1052 | APPROVAL_BY_COMPONENT); |
| 1053 | |
| 1054 | IInterface iInterface = mock(IInterface.class); |
| 1055 | when(iInterface.asBinder()).thenReturn(mock(IBinder.class)); |
| 1056 | |
| 1057 | ManagedServices.ManagedServiceInfo service0 = service.new ManagedServiceInfo( |
| 1058 | iInterface, ComponentName.unflattenFromString("a/a"), 0, false, |
| 1059 | mock(ServiceConnection.class), 26); |
| 1060 | ManagedServices.ManagedServiceInfo service0a = service.new ManagedServiceInfo( |
| 1061 | iInterface, ComponentName.unflattenFromString("c/c"), 0, false, |
| 1062 | mock(ServiceConnection.class), 26); |
| 1063 | ManagedServices.ManagedServiceInfo service10 = service.new ManagedServiceInfo( |
| 1064 | iInterface, ComponentName.unflattenFromString("b/b"), 10, false, |
| 1065 | mock(ServiceConnection.class), 26); |
| 1066 | Set<ManagedServices.ManagedServiceInfo> removableBoundServices = new ArraySet<>(); |
| 1067 | removableBoundServices.add(service0); |
| 1068 | removableBoundServices.add(service0a); |
| 1069 | removableBoundServices.add(service10); |
| 1070 | |
| 1071 | SparseArray<Set<ComponentName>> allowedComponentsToBind = new SparseArray<>(); |
| 1072 | Set<ComponentName> allowed0 = new ArraySet<>(); |
| 1073 | allowed0.add(ComponentName.unflattenFromString("a/a")); |
| 1074 | allowedComponentsToBind.put(0, allowed0); |
| 1075 | Set<ComponentName> allowed10 = new ArraySet<>(); |
| 1076 | allowed10.add(ComponentName.unflattenFromString("b/b")); |
| 1077 | allowedComponentsToBind.put(10, allowed10); |
| 1078 | |
| 1079 | SparseArray<Set<ComponentName>> componentsToUnbind = new SparseArray<>(); |
| 1080 | |
| 1081 | service.populateComponentsToUnbind(false, removableBoundServices, allowedComponentsToBind, |
| 1082 | componentsToUnbind); |
| 1083 | |
| 1084 | assertEquals(1, componentsToUnbind.size()); |
| 1085 | assertEquals(1, componentsToUnbind.get(0).size()); |
| 1086 | assertTrue(componentsToUnbind.get(0).contains(ComponentName.unflattenFromString("c/c"))); |
| 1087 | } |
| 1088 | |
| 1089 | @Test |
| 1090 | public void populateComponentsToBind() { |
| 1091 | ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, mIpm, |
| 1092 | APPROVAL_BY_COMPONENT); |
| 1093 | |
| 1094 | SparseArray<ArraySet<ComponentName>> approvedComponentsByUser = new SparseArray<>(); |
| 1095 | ArraySet<ComponentName> allowed0 = new ArraySet<>(); |
| 1096 | allowed0.add(ComponentName.unflattenFromString("a/a")); |
| 1097 | approvedComponentsByUser.put(0, allowed0); |
| 1098 | ArraySet<ComponentName> allowed10 = new ArraySet<>(); |
| 1099 | allowed10.add(ComponentName.unflattenFromString("b/b")); |
| 1100 | allowed10.add(ComponentName.unflattenFromString("c/c")); |
| 1101 | approvedComponentsByUser.put(10, allowed10); |
| 1102 | ArraySet<ComponentName> allowed15 = new ArraySet<>(); |
| 1103 | allowed15.add(ComponentName.unflattenFromString("d/d")); |
| 1104 | approvedComponentsByUser.put(15, allowed15); |
| 1105 | |
| 1106 | IntArray users = new IntArray(); |
| 1107 | users.add(10); |
| 1108 | users.add(0); |
| 1109 | |
| 1110 | SparseArray<Set<ComponentName>> componentsToBind = new SparseArray<>(); |
| 1111 | |
| 1112 | service.populateComponentsToBind(componentsToBind, users, approvedComponentsByUser); |
| 1113 | |
| 1114 | assertEquals(2, componentsToBind.size()); |
| 1115 | assertEquals(1, componentsToBind.get(0).size()); |
| 1116 | assertTrue(componentsToBind.get(0).contains(ComponentName.unflattenFromString("a/a"))); |
| 1117 | assertEquals(2, componentsToBind.get(10).size()); |
| 1118 | assertTrue(componentsToBind.get(10).contains(ComponentName.unflattenFromString("b/b"))); |
| 1119 | assertTrue(componentsToBind.get(10).contains(ComponentName.unflattenFromString("c/c"))); |
| 1120 | } |
| 1121 | |
Beverly | 42d0190 | 2019-01-03 12:47:57 -0500 | [diff] [blame] | 1122 | @Test |
Julia Reynolds | 8a4f2ab | 2019-04-10 10:08:21 -0400 | [diff] [blame] | 1123 | public void testOnNullBinding() throws Exception { |
| 1124 | Context context = mock(Context.class); |
| 1125 | PackageManager pm = mock(PackageManager.class); |
| 1126 | ApplicationInfo ai = new ApplicationInfo(); |
| 1127 | ai.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT; |
| 1128 | |
| 1129 | when(context.getPackageName()).thenReturn(mContext.getPackageName()); |
| 1130 | when(context.getUserId()).thenReturn(mContext.getUserId()); |
| 1131 | when(context.getPackageManager()).thenReturn(pm); |
| 1132 | when(pm.getApplicationInfo(anyString(), anyInt())).thenReturn(ai); |
| 1133 | |
| 1134 | ManagedServices service = new TestManagedServices(context, mLock, mUserProfiles, mIpm, |
| 1135 | APPROVAL_BY_COMPONENT); |
| 1136 | ComponentName cn = ComponentName.unflattenFromString("a/a"); |
| 1137 | |
| 1138 | service.registerSystemService(cn, 0); |
| 1139 | when(context.bindServiceAsUser(any(), any(), anyInt(), any())).thenAnswer(invocation -> { |
| 1140 | Object[] args = invocation.getArguments(); |
| 1141 | ServiceConnection sc = (ServiceConnection) args[1]; |
| 1142 | sc.onNullBinding(cn); |
| 1143 | return true; |
| 1144 | }); |
| 1145 | |
| 1146 | service.registerSystemService(cn, 0); |
| 1147 | assertFalse(service.isBound(cn, 0)); |
| 1148 | } |
| 1149 | |
| 1150 | @Test |
| 1151 | public void testOnServiceConnected() throws Exception { |
| 1152 | Context context = mock(Context.class); |
| 1153 | PackageManager pm = mock(PackageManager.class); |
| 1154 | ApplicationInfo ai = new ApplicationInfo(); |
| 1155 | ai.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT; |
| 1156 | |
| 1157 | when(context.getPackageName()).thenReturn(mContext.getPackageName()); |
| 1158 | when(context.getUserId()).thenReturn(mContext.getUserId()); |
| 1159 | when(context.getPackageManager()).thenReturn(pm); |
| 1160 | when(pm.getApplicationInfo(anyString(), anyInt())).thenReturn(ai); |
| 1161 | |
| 1162 | ManagedServices service = new TestManagedServices(context, mLock, mUserProfiles, mIpm, |
| 1163 | APPROVAL_BY_COMPONENT); |
| 1164 | ComponentName cn = ComponentName.unflattenFromString("a/a"); |
| 1165 | |
| 1166 | service.registerSystemService(cn, 0); |
| 1167 | when(context.bindServiceAsUser(any(), any(), anyInt(), any())).thenAnswer(invocation -> { |
| 1168 | Object[] args = invocation.getArguments(); |
| 1169 | ServiceConnection sc = (ServiceConnection) args[1]; |
| 1170 | sc.onServiceConnected(cn, mock(IBinder.class)); |
| 1171 | return true; |
| 1172 | }); |
| 1173 | |
| 1174 | service.registerSystemService(cn, 0); |
| 1175 | assertTrue(service.isBound(cn, 0)); |
| 1176 | } |
| 1177 | |
| 1178 | @Test |
Beverly | 42d0190 | 2019-01-03 12:47:57 -0500 | [diff] [blame] | 1179 | public void testOnPackagesChanged_nullValuesPassed_noNullPointers() { |
| 1180 | for (int approvalLevel : new int[] {APPROVAL_BY_COMPONENT, APPROVAL_BY_PACKAGE}) { |
| 1181 | ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, |
| 1182 | mIpm, approvalLevel); |
| 1183 | // null uid list |
| 1184 | service.onPackagesChanged(true, new String[]{"this.is.a.package.name"}, null); |
| 1185 | |
| 1186 | // null package list |
| 1187 | service.onPackagesChanged(true, null, new int[]{103}); |
| 1188 | } |
| 1189 | } |
| 1190 | |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 1191 | @Test |
| 1192 | public void loadDefaults_noVersionNoDefaults() throws Exception { |
| 1193 | resetComponentsAndPackages(); |
| 1194 | loadXml(mService); |
| 1195 | assertEquals(mService.getDefaultComponents().size(), 0); |
| 1196 | } |
| 1197 | |
| 1198 | @Test |
| 1199 | public void loadDefaults_noVersionNoDefaultsOneActive() throws Exception { |
| 1200 | resetComponentsAndPackages(); |
| 1201 | mService.addDefaultComponentOrPackage("package/class"); |
| 1202 | loadXml(mService); |
| 1203 | assertEquals(1, mService.getDefaultComponents().size()); |
| 1204 | assertTrue(mService.getDefaultComponents() |
| 1205 | .contains(ComponentName.unflattenFromString("package/class"))); |
| 1206 | } |
| 1207 | |
| 1208 | @Test |
| 1209 | public void loadDefaults_noVersionWithDefaults() throws Exception { |
| 1210 | resetComponentsAndPackages(); |
| 1211 | mDefaults.add(new ComponentName("default", "class")); |
| 1212 | loadXml(mService); |
| 1213 | assertEquals(mService.getDefaultComponents(), mDefaults); |
| 1214 | } |
| 1215 | |
| 1216 | @Test |
| 1217 | public void loadDefaults_versionOneWithDefaultsWithActive() throws Exception { |
| 1218 | resetComponentsAndPackages(); |
| 1219 | mDefaults.add(new ComponentName("default", "class")); |
| 1220 | mExpectedPrimaryComponentNames.put(0, "package/class"); |
| 1221 | mVersionString = "1"; |
| 1222 | loadXml(mService); |
| 1223 | assertEquals(mService.getDefaultComponents(), |
| 1224 | new ArraySet(Arrays.asList(new ComponentName("package", "class")))); |
| 1225 | } |
| 1226 | |
| 1227 | @Test |
| 1228 | public void loadDefaults_versionTwoWithDefaultsWithActive() throws Exception { |
| 1229 | resetComponentsAndPackages(); |
| 1230 | mDefaults.add(new ComponentName("default", "class")); |
| 1231 | mDefaultsString = "default/class"; |
| 1232 | mExpectedPrimaryComponentNames.put(0, "package/class"); |
| 1233 | mVersionString = "2"; |
| 1234 | loadXml(mService); |
| 1235 | assertEquals(1, mService.getDefaultComponents().size()); |
| 1236 | mDefaults.forEach(pkg -> { |
| 1237 | assertTrue(mService.getDefaultComponents().contains(pkg)); |
| 1238 | }); |
| 1239 | } |
| 1240 | |
| 1241 | @Test |
| 1242 | public void loadDefaults_versionOneWithXMLDefaultsWithActive() throws Exception { |
| 1243 | resetComponentsAndPackages(); |
| 1244 | mDefaults.add(new ComponentName("default", "class")); |
| 1245 | mDefaultsString = "xml/class"; |
| 1246 | mExpectedPrimaryComponentNames.put(0, "package/class"); |
| 1247 | mVersionString = "1"; |
| 1248 | loadXml(mService); |
| 1249 | assertEquals(mService.getDefaultComponents(), |
| 1250 | new ArraySet(Arrays.asList(new ComponentName("xml", "class")))); |
| 1251 | } |
| 1252 | |
| 1253 | @Test |
| 1254 | public void loadDefaults_versionTwoWithXMLDefaultsWithActive() throws Exception { |
| 1255 | resetComponentsAndPackages(); |
| 1256 | mDefaults.add(new ComponentName("default", "class")); |
| 1257 | mDefaultsString = "xml/class"; |
| 1258 | mExpectedPrimaryComponentNames.put(0, "package/class"); |
| 1259 | mVersionString = "2"; |
| 1260 | loadXml(mService); |
| 1261 | assertEquals(mService.getDefaultComponents(), |
| 1262 | new ArraySet(Arrays.asList(new ComponentName("xml", "class")))); |
| 1263 | } |
| 1264 | |
| 1265 | private void resetComponentsAndPackages() { |
| 1266 | ArrayMap<Integer, ArrayMap<Integer, String>> empty = new ArrayMap(1); |
| 1267 | ArrayMap<Integer, String> emptyPkgs = new ArrayMap(0); |
| 1268 | empty.append(mService.mApprovalLevel, emptyPkgs); |
| 1269 | mExpectedPrimary = empty; |
| 1270 | mExpectedPrimaryComponentNames = emptyPkgs; |
| 1271 | mExpectedPrimaryPackages = emptyPkgs; |
| 1272 | mExpectedSecondary = empty; |
| 1273 | mExpectedSecondaryComponentNames = emptyPkgs; |
| 1274 | mExpectedSecondaryPackages = emptyPkgs; |
| 1275 | } |
| 1276 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1277 | private void loadXml(ManagedServices service) throws Exception { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1278 | final StringBuffer xml = new StringBuffer(); |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 1279 | String xmlTag = service.getConfig().xmlTag; |
| 1280 | xml.append("<" + xmlTag |
| 1281 | + (mDefaultsString != null ? " defaults=\"" + mDefaultsString + "\" " : "") |
| 1282 | + (mVersionString != null ? " version=\"" + mVersionString + "\" " : "") |
| 1283 | + ">\n"); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1284 | for (int userId : mExpectedPrimary.get(service.mApprovalLevel).keySet()) { |
| 1285 | xml.append(getXmlEntry( |
| 1286 | mExpectedPrimary.get(service.mApprovalLevel).get(userId), userId, true)); |
| 1287 | } |
| 1288 | for (int userId : mExpectedSecondary.get(service.mApprovalLevel).keySet()) { |
| 1289 | xml.append(getXmlEntry( |
| 1290 | mExpectedSecondary.get(service.mApprovalLevel).get(userId), userId, false)); |
| 1291 | } |
Julia Reynolds | 4552300 | 2017-09-12 09:43:57 -0400 | [diff] [blame] | 1292 | xml.append("<" + ManagedServices.TAG_MANAGED_SERVICES + " " |
| 1293 | + ManagedServices.ATT_USER_ID + "=\"99\" " |
| 1294 | + ManagedServices.ATT_IS_PRIMARY + "=\"true\" " |
| 1295 | + ManagedServices.ATT_APPROVED_LIST + "=\"99\" />\n"); |
| 1296 | xml.append("<" + ManagedServices.TAG_MANAGED_SERVICES + " " |
| 1297 | + ManagedServices.ATT_USER_ID + "=\"98\" " |
| 1298 | + ManagedServices.ATT_IS_PRIMARY + "=\"false\" " |
| 1299 | + ManagedServices.ATT_APPROVED_LIST + "=\"98\" />\n"); |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 1300 | xml.append("</" + xmlTag + ">"); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1301 | |
| 1302 | XmlPullParser parser = Xml.newPullParser(); |
| 1303 | parser.setInput(new BufferedInputStream( |
| 1304 | new ByteArrayInputStream(xml.toString().getBytes())), null); |
| 1305 | parser.nextTag(); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 1306 | service.readXml(parser, null, false, UserHandle.USER_ALL); |
| 1307 | } |
| 1308 | |
| 1309 | private XmlPullParser getParserWithEntries(ManagedServices service, String... xmlEntries) |
| 1310 | throws Exception { |
| 1311 | final StringBuffer xml = new StringBuffer(); |
| 1312 | xml.append("<" + service.getConfig().xmlTag + ">\n"); |
| 1313 | for (String xmlEntry : xmlEntries) { |
| 1314 | xml.append(xmlEntry); |
| 1315 | } |
| 1316 | xml.append("</" + service.getConfig().xmlTag + ">"); |
| 1317 | |
| 1318 | XmlPullParser parser = Xml.newPullParser(); |
| 1319 | parser.setInput(new BufferedInputStream( |
| 1320 | new ByteArrayInputStream(xml.toString().getBytes())), null); |
| 1321 | parser.nextTag(); |
| 1322 | return parser; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | private void addExpectedServices(final ManagedServices service, final List<String> packages, |
| 1326 | int userId) { |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 1327 | ManagedServices.Config config = service.getConfig(); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1328 | when(mPm.queryIntentServicesAsUser(any(), anyInt(), eq(userId))). |
| 1329 | thenAnswer(new Answer<List<ResolveInfo>>() { |
| 1330 | @Override |
| 1331 | public List<ResolveInfo> answer(InvocationOnMock invocationOnMock) |
| 1332 | throws Throwable { |
| 1333 | Object[] args = invocationOnMock.getArguments(); |
| 1334 | Intent invocationIntent = (Intent) args[0]; |
| 1335 | if (invocationIntent != null) { |
| 1336 | if (invocationIntent.getAction().equals( |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 1337 | config.serviceInterface) |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1338 | && packages.contains(invocationIntent.getPackage())) { |
| 1339 | List<ResolveInfo> dummyServices = new ArrayList<>(); |
| 1340 | for (int i = 1; i <= 3; i ++) { |
| 1341 | ResolveInfo resolveInfo = new ResolveInfo(); |
| 1342 | ServiceInfo serviceInfo = new ServiceInfo(); |
| 1343 | serviceInfo.packageName = invocationIntent.getPackage(); |
| 1344 | serviceInfo.name = "C"+i; |
| 1345 | serviceInfo.permission = service.getConfig().bindPermission; |
| 1346 | resolveInfo.serviceInfo = serviceInfo; |
| 1347 | dummyServices.add(resolveInfo); |
| 1348 | } |
| 1349 | return dummyServices; |
| 1350 | } |
| 1351 | } |
| 1352 | return new ArrayList<>(); |
| 1353 | } |
| 1354 | }); |
| 1355 | } |
| 1356 | |
| 1357 | private List<String> stringToList(String list) { |
| 1358 | if (list == null) { |
| 1359 | list = ""; |
| 1360 | } |
| 1361 | return new ArrayList<>(Lists.newArrayList(list.split( |
| 1362 | ManagedServices.ENABLED_SERVICES_SEPARATOR))); |
| 1363 | } |
| 1364 | |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1365 | private void assertContentsInAnyOrder(Collection<?> expected, Collection<?> actual) { |
| 1366 | assertNotNull(actual); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1367 | assertEquals(expected.size(), actual.size()); |
| 1368 | |
| 1369 | for (Object o : expected) { |
| 1370 | assertTrue("Actual missing " + o, actual.contains(o)); |
| 1371 | } |
| 1372 | |
| 1373 | for (Object o : actual) { |
| 1374 | assertTrue("Actual contains extra " + o, expected.contains(o)); |
| 1375 | } |
| 1376 | } |
| 1377 | |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1378 | private void assertContentsInAnyOrder(SparseArray<ArraySet<ComponentName>> expected, |
| 1379 | SparseArray<ArraySet<ComponentName>> actual) throws Exception { |
| 1380 | assertEquals(expected.size(), actual.size()); |
| 1381 | |
| 1382 | for (int i = 0; i < expected.size(); i++) { |
| 1383 | int key = expected.keyAt(i); |
| 1384 | assertTrue(actual.indexOfKey(key) >= 0); |
| 1385 | try { |
| 1386 | assertContentsInAnyOrder(expected.valueAt(i), actual.get(key)); |
| 1387 | } catch (Throwable t) { |
| 1388 | throw new Exception("Error validating " + key, t); |
| 1389 | } |
| 1390 | } |
| 1391 | } |
| 1392 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1393 | private void verifyExpectedBoundEntries(ManagedServices service, boolean primary) |
| 1394 | throws Exception { |
| 1395 | ArrayMap<Integer, String> verifyMap = primary ? mExpectedPrimary.get(service.mApprovalLevel) |
| 1396 | : mExpectedSecondary.get(service.mApprovalLevel); |
| 1397 | for (int userId : verifyMap.keySet()) { |
| 1398 | for (String packageOrComponent : verifyMap.get(userId).split(":")) { |
| 1399 | if (!TextUtils.isEmpty(packageOrComponent)) { |
| 1400 | if (service.mApprovalLevel == APPROVAL_BY_PACKAGE) { |
Julia Reynolds | 5aa13a4 | 2017-08-24 09:10:23 -0400 | [diff] [blame] | 1401 | assertTrue(packageOrComponent, |
| 1402 | service.isComponentEnabledForPackage(packageOrComponent)); |
Julia Reynolds | 1459074 | 2018-07-30 09:25:35 -0400 | [diff] [blame] | 1403 | for (int i = 1; i <= 3; i++) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1404 | ComponentName componentName = ComponentName.unflattenFromString( |
| 1405 | packageOrComponent +"/C" + i); |
| 1406 | assertTrue(service.isComponentEnabledForCurrentProfiles( |
| 1407 | componentName)); |
| 1408 | verify(mIpm, times(1)).getServiceInfo( |
| 1409 | eq(componentName), anyInt(), anyInt()); |
| 1410 | } |
| 1411 | } else { |
| 1412 | ComponentName componentName = |
| 1413 | ComponentName.unflattenFromString(packageOrComponent); |
| 1414 | assertTrue(service.isComponentEnabledForCurrentProfiles(componentName)); |
| 1415 | verify(mIpm, times(1)).getServiceInfo( |
| 1416 | eq(componentName), anyInt(), anyInt()); |
| 1417 | } |
| 1418 | } |
| 1419 | } |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | private void verifyExpectedApprovedEntries(ManagedServices service) { |
| 1424 | verifyExpectedApprovedEntries(service, true); |
| 1425 | verifyExpectedApprovedEntries(service, false); |
| 1426 | } |
| 1427 | |
| 1428 | private void verifyExpectedApprovedEntries(ManagedServices service, boolean primary) { |
| 1429 | ArrayMap<Integer, String> verifyMap = primary |
| 1430 | ? mExpectedPrimary.get(service.mApprovalLevel) |
| 1431 | : mExpectedSecondary.get(service.mApprovalLevel); |
Julia Reynolds | fa206a4 | 2017-08-14 13:22:23 -0400 | [diff] [blame] | 1432 | verifyExpectedApprovedEntries(service, verifyMap); |
| 1433 | } |
| 1434 | |
| 1435 | private void verifyExpectedApprovedEntries(ManagedServices service, |
| 1436 | ArrayMap<Integer, String> verifyMap) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1437 | for (int userId : verifyMap.keySet()) { |
| 1438 | for (String verifyValue : verifyMap.get(userId).split(":")) { |
| 1439 | if (!TextUtils.isEmpty(verifyValue)) { |
| 1440 | assertTrue("service type " + service.mApprovalLevel + ":" |
Julia Reynolds | fa206a4 | 2017-08-14 13:22:23 -0400 | [diff] [blame] | 1441 | + verifyValue + " is not allowed for user " + userId, |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1442 | service.isPackageOrComponentAllowed(verifyValue, userId)); |
| 1443 | } |
| 1444 | } |
| 1445 | } |
| 1446 | } |
| 1447 | |
Julia Reynolds | 1459074 | 2018-07-30 09:25:35 -0400 | [diff] [blame] | 1448 | |
| 1449 | private void verifyExpectedApprovedPackages(ManagedServices service) { |
| 1450 | verifyExpectedApprovedPackages(service, true); |
| 1451 | verifyExpectedApprovedPackages(service, false); |
| 1452 | } |
| 1453 | |
| 1454 | private void verifyExpectedApprovedPackages(ManagedServices service, boolean primary) { |
| 1455 | ArrayMap<Integer, String> verifyMap = primary |
| 1456 | ? mExpectedPrimary.get(service.mApprovalLevel) |
| 1457 | : mExpectedSecondary.get(service.mApprovalLevel); |
| 1458 | verifyExpectedApprovedPackages(service, verifyMap); |
| 1459 | } |
| 1460 | |
| 1461 | private void verifyExpectedApprovedPackages(ManagedServices service, |
| 1462 | ArrayMap<Integer, String> verifyMap) { |
| 1463 | for (int userId : verifyMap.keySet()) { |
| 1464 | for (String verifyValue : verifyMap.get(userId).split(":")) { |
| 1465 | if (!TextUtils.isEmpty(verifyValue)) { |
| 1466 | ComponentName component = ComponentName.unflattenFromString(verifyValue); |
| 1467 | if (component != null ) { |
| 1468 | assertTrue("service type " + service.mApprovalLevel + ":" |
| 1469 | + verifyValue + " is not allowed for user " + userId, |
| 1470 | service.isPackageAllowed(component.getPackageName(), userId)); |
| 1471 | } else { |
| 1472 | assertTrue("service type " + service.mApprovalLevel + ":" |
| 1473 | + verifyValue + " is not allowed for user " + userId, |
| 1474 | service.isPackageAllowed(verifyValue, userId)); |
| 1475 | } |
| 1476 | } |
| 1477 | } |
| 1478 | } |
| 1479 | } |
| 1480 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1481 | private void writeExpectedValuesToSettings(int approvalLevel) { |
| 1482 | for (int userId : mExpectedPrimary.get(approvalLevel).keySet()) { |
| 1483 | Settings.Secure.putStringForUser(getContext().getContentResolver(), SETTING, |
| 1484 | mExpectedPrimary.get(approvalLevel).get(userId), userId); |
| 1485 | } |
| 1486 | for (int userId : mExpectedSecondary.get(approvalLevel).keySet()) { |
| 1487 | Settings.Secure.putStringForUser(getContext().getContentResolver(), SECONDARY_SETTING, |
| 1488 | mExpectedSecondary.get(approvalLevel).get(userId), userId); |
| 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | private String getXmlEntry(String approved, int userId, boolean isPrimary) { |
| 1493 | return "<" + ManagedServices.TAG_MANAGED_SERVICES + " " |
| 1494 | + ManagedServices.ATT_USER_ID + "=\"" + userId +"\" " |
| 1495 | + ManagedServices.ATT_IS_PRIMARY + "=\"" + isPrimary +"\" " |
| 1496 | + ManagedServices.ATT_APPROVED_LIST + "=\"" + approved +"\" " |
| 1497 | + "/>\n"; |
| 1498 | } |
| 1499 | |
| 1500 | class TestManagedServices extends ManagedServices { |
| 1501 | |
| 1502 | public TestManagedServices(Context context, Object mutex, UserProfiles userProfiles, |
| 1503 | IPackageManager pm, int approvedServiceType) { |
| 1504 | super(context, mutex, userProfiles, pm); |
| 1505 | mApprovalLevel = approvedServiceType; |
| 1506 | } |
| 1507 | |
| 1508 | @Override |
| 1509 | protected Config getConfig() { |
| 1510 | final Config c = new Config(); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1511 | c.xmlTag = "test"; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1512 | c.secureSettingName = SETTING; |
| 1513 | c.secondarySettingName = SECONDARY_SETTING; |
| 1514 | c.bindPermission = "permission"; |
| 1515 | c.serviceInterface = "serviceInterface"; |
| 1516 | return c; |
| 1517 | } |
| 1518 | |
| 1519 | @Override |
| 1520 | protected IInterface asInterface(IBinder binder) { |
| 1521 | return null; |
| 1522 | } |
| 1523 | |
| 1524 | @Override |
| 1525 | protected boolean checkType(IInterface service) { |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1526 | return true; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | @Override |
| 1530 | protected void onServiceAdded(ManagedServiceInfo info) { |
| 1531 | |
| 1532 | } |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 1533 | |
| 1534 | @Override |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 1535 | protected void loadDefaultsFromConfig() { |
| 1536 | mDefaultComponents.addAll(mDefaults); |
| 1537 | } |
| 1538 | |
| 1539 | @Override |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 1540 | protected String getRequiredPermission() { |
| 1541 | return null; |
| 1542 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1543 | } |
| 1544 | } |