Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | */ |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 16 | package com.android.server.devicepolicy; |
| 17 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 18 | import static android.app.admin.DevicePolicyManager.DELEGATION_APP_RESTRICTIONS; |
| 19 | import static android.app.admin.DevicePolicyManager.DELEGATION_CERT_INSTALL; |
yinxu | f4f9cec | 2017-06-19 10:28:19 -0700 | [diff] [blame] | 20 | import static android.app.admin.DevicePolicyManager.WIPE_EUICC; |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 21 | import static android.os.UserManagerInternal.CAMERA_DISABLED_GLOBALLY; |
| 22 | import static android.os.UserManagerInternal.CAMERA_DISABLED_LOCALLY; |
| 23 | import static android.os.UserManagerInternal.CAMERA_NOT_DISABLED; |
| 24 | |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 25 | import static com.android.server.testutis.TestUtils.assertExpectException; |
| 26 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 27 | import static org.mockito.Matchers.any; |
| 28 | import static org.mockito.Matchers.anyInt; |
| 29 | import static org.mockito.Matchers.anyLong; |
| 30 | import static org.mockito.Matchers.anyObject; |
| 31 | import static org.mockito.Matchers.anyString; |
| 32 | import static org.mockito.Matchers.eq; |
| 33 | import static org.mockito.Matchers.isNull; |
| 34 | import static org.mockito.Mockito.atLeast; |
| 35 | import static org.mockito.Mockito.doAnswer; |
| 36 | import static org.mockito.Mockito.doReturn; |
Christine Franks | 361b825 | 2017-06-23 18:12:46 -0700 | [diff] [blame] | 37 | import static org.mockito.Mockito.mock; |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 38 | import static org.mockito.Mockito.never; |
| 39 | import static org.mockito.Mockito.nullable; |
| 40 | import static org.mockito.Mockito.reset; |
| 41 | import static org.mockito.Mockito.timeout; |
| 42 | import static org.mockito.Mockito.times; |
| 43 | import static org.mockito.Mockito.verify; |
| 44 | import static org.mockito.Mockito.verifyZeroInteractions; |
| 45 | import static org.mockito.Mockito.when; |
| 46 | import static org.mockito.hamcrest.MockitoHamcrest.argThat; |
| 47 | |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 48 | import android.Manifest.permission; |
| 49 | import android.app.Activity; |
Robin Lee | 7f5c91c | 2017-02-08 21:27:02 +0000 | [diff] [blame] | 50 | import android.app.Notification; |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 51 | import android.app.admin.DeviceAdminReceiver; |
| 52 | import android.app.admin.DevicePolicyManager; |
| 53 | import android.app.admin.DevicePolicyManagerInternal; |
Eric Sandness | fabfcb0 | 2017-05-03 18:28:56 +0100 | [diff] [blame] | 54 | import android.app.admin.PasswordMetrics; |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 55 | import android.content.BroadcastReceiver; |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 56 | import android.content.ComponentName; |
Christine Franks | 361b825 | 2017-06-23 18:12:46 -0700 | [diff] [blame] | 57 | import android.content.Context; |
Tony Mak | 2f26b79 | 2016-11-28 17:54:51 +0000 | [diff] [blame] | 58 | import android.content.Intent; |
Rubin Xu | ed1928a | 2016-02-11 17:23:06 +0000 | [diff] [blame] | 59 | import android.content.pm.ApplicationInfo; |
| 60 | import android.content.pm.PackageInfo; |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 61 | import android.content.pm.PackageManager; |
Robin Lee | abaa069 | 2017-02-20 20:54:22 +0000 | [diff] [blame] | 62 | import android.content.pm.StringParceledListSlice; |
Tony Mak | 2f26b79 | 2016-11-28 17:54:51 +0000 | [diff] [blame] | 63 | import android.content.pm.UserInfo; |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 64 | import android.graphics.Color; |
| 65 | import android.net.Uri; |
Makoto Onuki | a31ebbc | 2015-11-23 17:15:21 -0800 | [diff] [blame] | 66 | import android.net.wifi.WifiInfo; |
Makoto Onuki | 3ab6f2e | 2015-11-05 13:55:37 -0800 | [diff] [blame] | 67 | import android.os.Build.VERSION_CODES; |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 68 | import android.os.Bundle; |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 69 | import android.os.Process; |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 70 | import android.os.UserHandle; |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 71 | import android.os.UserManager; |
Pavel Grafov | c14b317 | 2017-07-03 13:15:11 +0100 | [diff] [blame] | 72 | import android.platform.test.annotations.Presubmit; |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 73 | import android.provider.Settings; |
Bartosz Fabianowski | 05dc9f7 | 2017-02-22 23:41:14 +0100 | [diff] [blame] | 74 | import android.security.KeyChain; |
Mahaver Chopra | 1216ae5 | 2016-03-11 15:39:48 +0000 | [diff] [blame] | 75 | import android.telephony.TelephonyManager; |
Makoto Onuki | a31ebbc | 2015-11-23 17:15:21 -0800 | [diff] [blame] | 76 | import android.test.MoreAsserts; |
Benjamin Franz | 6d00903 | 2016-01-25 18:56:38 +0000 | [diff] [blame] | 77 | import android.test.suitebuilder.annotation.SmallTest; |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 78 | import android.util.ArraySet; |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 79 | import android.util.Pair; |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 80 | |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 81 | import com.android.internal.R; |
Rubin Xu | aab7a41 | 2016-12-30 21:13:29 +0000 | [diff] [blame] | 82 | import com.android.internal.widget.LockPatternUtils; |
Alan Treadway | afad878 | 2016-01-19 15:15:08 +0000 | [diff] [blame] | 83 | import com.android.server.LocalServices; |
| 84 | import com.android.server.SystemService; |
Esteban Talavera | 6c9116a | 2016-11-24 16:12:44 +0000 | [diff] [blame] | 85 | import com.android.server.pm.UserRestrictionsUtils; |
Alan Treadway | afad878 | 2016-01-19 15:15:08 +0000 | [diff] [blame] | 86 | |
Robin Lee | 7f5c91c | 2017-02-08 21:27:02 +0000 | [diff] [blame] | 87 | import org.hamcrest.BaseMatcher; |
| 88 | import org.hamcrest.Description; |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 89 | import org.mockito.invocation.InvocationOnMock; |
| 90 | import org.mockito.stubbing.Answer; |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 91 | |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 92 | import java.util.ArrayList; |
Alan Treadway | afad878 | 2016-01-19 15:15:08 +0000 | [diff] [blame] | 93 | import java.util.Arrays; |
Esteban Talavera | c9bb378 | 2016-11-11 15:41:14 +0000 | [diff] [blame] | 94 | import java.util.Collections; |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 95 | import java.util.HashMap; |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 96 | import java.util.List; |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 97 | import java.util.Map; |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 98 | import java.util.Set; |
Michal Karpinski | d084ca5 | 2017-01-18 15:54:18 +0000 | [diff] [blame] | 99 | import java.util.concurrent.TimeUnit; |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 100 | |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 101 | /** |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 102 | * Tests for DevicePolicyManager( and DevicePolicyManagerService). |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 103 | * You can run them via: |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 104 | m FrameworksServicesTests && |
| 105 | adb install \ |
Alan Treadway | afad878 | 2016-01-19 15:15:08 +0000 | [diff] [blame] | 106 | -r ${ANDROID_PRODUCT_OUT}/data/app/FrameworksServicesTests/FrameworksServicesTests.apk && |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 107 | adb shell am instrument -e class com.android.server.devicepolicy.DevicePolicyManagerTest \ |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 108 | -w com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 109 | |
| 110 | (mmma frameworks/base/services/tests/servicestests/ for non-ninja build) |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 111 | * |
| 112 | * , or: |
| 113 | * runtest -c com.android.server.devicepolicy.DevicePolicyManagerTest frameworks-services |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 114 | */ |
Benjamin Franz | 6d00903 | 2016-01-25 18:56:38 +0000 | [diff] [blame] | 115 | @SmallTest |
Pavel Grafov | c14b317 | 2017-07-03 13:15:11 +0100 | [diff] [blame] | 116 | @Presubmit |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 117 | public class DevicePolicyManagerTest extends DpmTestBase { |
Alan Treadway | afad878 | 2016-01-19 15:15:08 +0000 | [diff] [blame] | 118 | private static final List<String> OWNER_SETUP_PERMISSIONS = Arrays.asList( |
| 119 | permission.MANAGE_DEVICE_ADMINS, permission.MANAGE_PROFILE_AND_DEVICE_OWNERS, |
| 120 | permission.MANAGE_USERS, permission.INTERACT_ACROSS_USERS_FULL); |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 121 | public static final String NOT_DEVICE_OWNER_MSG = "does not own the device"; |
| 122 | public static final String ONGOING_CALL_MSG = "ongoing call on the device"; |
Alan Treadway | afad878 | 2016-01-19 15:15:08 +0000 | [diff] [blame] | 123 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 124 | // TODO replace all instances of this with explicit {@link #mServiceContext}. |
| 125 | @Deprecated |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 126 | private DpmMockContext mContext; |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 127 | |
| 128 | private DpmMockContext mServiceContext; |
| 129 | private DpmMockContext mAdmin1Context; |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 130 | public DevicePolicyManager dpm; |
| 131 | public DevicePolicyManagerServiceTestable dpms; |
| 132 | |
Bartosz Fabianowski | 05dc9f7 | 2017-02-22 23:41:14 +0100 | [diff] [blame] | 133 | /* |
| 134 | * The CA cert below is the content of cacert.pem as generated by: |
| 135 | * |
| 136 | * openssl req -new -x509 -days 3650 -extensions v3_ca -keyout cakey.pem -out cacert.pem |
| 137 | */ |
| 138 | private static final String TEST_CA = |
| 139 | "-----BEGIN CERTIFICATE-----\n" + |
| 140 | "MIIDXTCCAkWgAwIBAgIJAK9Tl/F9V8kSMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV\n" + |
| 141 | "BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX\n" + |
| 142 | "aWRnaXRzIFB0eSBMdGQwHhcNMTUwMzA2MTczMjExWhcNMjUwMzAzMTczMjExWjBF\n" + |
| 143 | "MQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50\n" + |
| 144 | "ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB\n" + |
| 145 | "CgKCAQEAvItOutsE75WBTgTyNAHt4JXQ3JoseaGqcC3WQij6vhrleWi5KJ0jh1/M\n" + |
| 146 | "Rpry7Fajtwwb4t8VZa0NuM2h2YALv52w1xivql88zce/HU1y7XzbXhxis9o6SCI+\n" + |
| 147 | "oVQSbPeXRgBPppFzBEh3ZqYTVhAqw451XhwdA4Aqs3wts7ddjwlUzyMdU44osCUg\n" + |
| 148 | "kVg7lfPf9sTm5IoHVcfLSCWH5n6Nr9sH3o2ksyTwxuOAvsN11F/a0mmUoPciYPp+\n" + |
| 149 | "q7DzQzdi7akRG601DZ4YVOwo6UITGvDyuAAdxl5isovUXqe6Jmz2/myTSpAKxGFs\n" + |
| 150 | "jk9oRoG6WXWB1kni490GIPjJ1OceyQIDAQABo1AwTjAdBgNVHQ4EFgQUH1QIlPKL\n" + |
| 151 | "p2OQ/AoLOjKvBW4zK3AwHwYDVR0jBBgwFoAUH1QIlPKLp2OQ/AoLOjKvBW4zK3Aw\n" + |
| 152 | "DAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAcMi4voMMJHeQLjtq8Oky\n" + |
| 153 | "Azpyk8moDwgCd4llcGj7izOkIIFqq/lyqKdtykVKUWz2bSHO5cLrtaOCiBWVlaCV\n" + |
| 154 | "DYAnnVLM8aqaA6hJDIfaGs4zmwz0dY8hVMFCuCBiLWuPfiYtbEmjHGSmpQTG6Qxn\n" + |
| 155 | "ZJlaK5CZyt5pgh5EdNdvQmDEbKGmu0wpCq9qjZImwdyAul1t/B0DrsWApZMgZpeI\n" + |
| 156 | "d2od0VBrCICB1K4p+C51D93xyQiva7xQcCne+TAnGNy9+gjQ/MyR8MRpwRLv5ikD\n" + |
| 157 | "u0anJCN8pXo6IMglfMAsoton1J6o5/ae5uhC6caQU8bNUsCK570gpNfjkzo6rbP0\n" + |
| 158 | "wQ==\n" + |
| 159 | "-----END CERTIFICATE-----\n"; |
| 160 | |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 161 | @Override |
| 162 | protected void setUp() throws Exception { |
| 163 | super.setUp(); |
| 164 | |
| 165 | mContext = getContext(); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 166 | mServiceContext = mContext; |
| 167 | mServiceContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 168 | when(getServices().packageManager.hasSystemFeature(eq(PackageManager.FEATURE_DEVICE_ADMIN))) |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 169 | .thenReturn(true); |
| 170 | |
Makoto Onuki | 1a5ee77 | 2016-02-12 15:34:57 -0800 | [diff] [blame] | 171 | // By default, pretend all users are running and unlocked. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 172 | when(getServices().userManager.isUserUnlocked(anyInt())).thenReturn(true); |
Makoto Onuki | 1a5ee77 | 2016-02-12 15:34:57 -0800 | [diff] [blame] | 173 | |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 174 | initializeDpms(); |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 175 | |
Makoto Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 176 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_UID); |
| 177 | setUpPackageManagerForAdmin(admin2, DpmMockContext.CALLER_UID); |
| 178 | setUpPackageManagerForAdmin(admin3, DpmMockContext.CALLER_UID); |
Makoto Onuki | 3ab6f2e | 2015-11-05 13:55:37 -0800 | [diff] [blame] | 179 | setUpPackageManagerForAdmin(adminNoPerm, DpmMockContext.CALLER_UID); |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 180 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 181 | mAdmin1Context = new DpmMockContext(getServices(), mRealTestContext); |
| 182 | mAdmin1Context.packageName = admin1.getPackageName(); |
| 183 | mAdmin1Context.applicationInfo = new ApplicationInfo(); |
| 184 | mAdmin1Context.binder.callingUid = DpmMockContext.CALLER_UID; |
| 185 | |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 186 | setUpUserManager(); |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 189 | @Override |
| 190 | protected void tearDown() throws Exception { |
| 191 | flushTasks(); |
| 192 | super.tearDown(); |
| 193 | } |
| 194 | |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 195 | private void initializeDpms() { |
| 196 | // Need clearCallingIdentity() to pass permission checks. |
| 197 | final long ident = mContext.binder.clearCallingIdentity(); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 198 | LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 199 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 200 | dpms = new DevicePolicyManagerServiceTestable(getServices(), mContext); |
| 201 | dpms.systemReady(SystemService.PHASE_LOCK_SETTINGS_READY); |
| 202 | dpms.systemReady(SystemService.PHASE_BOOT_COMPLETED); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 203 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 204 | dpm = new DevicePolicyManagerTestable(mContext, dpms); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 205 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 206 | mContext.binder.restoreCallingIdentity(ident); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 209 | private void setUpUserManager() { |
| 210 | // Emulate UserManager.set/getApplicationRestriction(). |
| 211 | final Map<Pair<String, UserHandle>, Bundle> appRestrictions = new HashMap<>(); |
| 212 | |
| 213 | // UM.setApplicationRestrictions() will save to appRestrictions. |
| 214 | doAnswer(new Answer<Void>() { |
| 215 | @Override |
| 216 | public Void answer(InvocationOnMock invocation) throws Throwable { |
| 217 | String pkg = (String) invocation.getArguments()[0]; |
| 218 | Bundle bundle = (Bundle) invocation.getArguments()[1]; |
| 219 | UserHandle user = (UserHandle) invocation.getArguments()[2]; |
| 220 | |
| 221 | appRestrictions.put(Pair.create(pkg, user), bundle); |
| 222 | |
| 223 | return null; |
| 224 | } |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 225 | }).when(getServices().userManager).setApplicationRestrictions( |
Eric Sandness | a9b8253 | 2017-04-07 18:17:12 +0100 | [diff] [blame] | 226 | anyString(), nullable(Bundle.class), any(UserHandle.class)); |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 227 | |
| 228 | // UM.getApplicationRestrictions() will read from appRestrictions. |
| 229 | doAnswer(new Answer<Bundle>() { |
| 230 | @Override |
| 231 | public Bundle answer(InvocationOnMock invocation) throws Throwable { |
| 232 | String pkg = (String) invocation.getArguments()[0]; |
| 233 | UserHandle user = (UserHandle) invocation.getArguments()[1]; |
| 234 | |
| 235 | return appRestrictions.get(Pair.create(pkg, user)); |
| 236 | } |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 237 | }).when(getServices().userManager).getApplicationRestrictions( |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 238 | anyString(), any(UserHandle.class)); |
| 239 | |
Makoto Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 240 | // Add the first secondary user. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 241 | getServices().addUser(DpmMockContext.CALLER_USER_HANDLE, 0); |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | private void setAsProfileOwner(ComponentName admin) { |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 245 | final long ident = mServiceContext.binder.clearCallingIdentity(); |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 246 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 247 | mServiceContext.binder.callingUid = |
| 248 | UserHandle.getUid(DpmMockContext.CALLER_USER_HANDLE, DpmMockContext.SYSTEM_UID); |
| 249 | runAsCaller(mServiceContext, dpms, dpm -> { |
| 250 | // PO needs to be a DA. |
| 251 | dpm.setActiveAdmin(admin, /*replace=*/ false); |
| 252 | // Fire! |
| 253 | assertTrue(dpm.setProfileOwner(admin, "owner-name", DpmMockContext.CALLER_USER_HANDLE)); |
| 254 | // Check |
| 255 | assertEquals(admin, dpm.getProfileOwnerAsUser(DpmMockContext.CALLER_USER_HANDLE)); |
| 256 | }); |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 257 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 258 | mServiceContext.binder.restoreCallingIdentity(ident); |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | public void testHasNoFeature() throws Exception { |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 262 | when(getServices().packageManager.hasSystemFeature(eq(PackageManager.FEATURE_DEVICE_ADMIN))) |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 263 | .thenReturn(false); |
| 264 | |
| 265 | LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 266 | new DevicePolicyManagerServiceTestable(getServices(), mContext); |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 267 | |
| 268 | // If the device has no DPMS feature, it shouldn't register the local service. |
| 269 | assertNull(LocalServices.getService(DevicePolicyManagerInternal.class)); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Caller doesn't have proper permissions. |
| 274 | */ |
| 275 | public void testSetActiveAdmin_SecurityException() { |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 276 | // 1. Failure cases. |
| 277 | |
| 278 | // Caller doesn't have MANAGE_DEVICE_ADMINS. |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 279 | assertExpectException(SecurityException.class, /* messageRegex= */ null, |
| 280 | () -> dpm.setActiveAdmin(admin1, false)); |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 281 | |
| 282 | // Caller has MANAGE_DEVICE_ADMINS, but for different user. |
| 283 | mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 284 | |
| 285 | assertExpectException(SecurityException.class, /* messageRegex= */ null, |
| 286 | () -> dpm.setActiveAdmin(admin1, false, DpmMockContext.CALLER_USER_HANDLE + 1)); |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 289 | /** |
| 290 | * Test for: |
| 291 | * {@link DevicePolicyManager#setActiveAdmin} |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 292 | * with replace=false and replace=true |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 293 | * {@link DevicePolicyManager#isAdminActive} |
| 294 | * {@link DevicePolicyManager#isAdminActiveAsUser} |
| 295 | * {@link DevicePolicyManager#getActiveAdmins} |
| 296 | * {@link DevicePolicyManager#getActiveAdminsAsUser} |
| 297 | */ |
| 298 | public void testSetActiveAdmin() throws Exception { |
| 299 | // 1. Make sure the caller has proper permissions. |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 300 | mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); |
| 301 | |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 302 | // 2. Call the API. |
| 303 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 304 | |
| 305 | // 3. Verify internal calls. |
| 306 | |
| 307 | // Check if the boradcast is sent. |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 308 | verify(mContext.spiedContext).sendBroadcastAsUser( |
| 309 | MockUtils.checkIntentAction( |
| 310 | DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED), |
| 311 | MockUtils.checkUserHandle(DpmMockContext.CALLER_USER_HANDLE)); |
| 312 | verify(mContext.spiedContext).sendBroadcastAsUser( |
| 313 | MockUtils.checkIntentAction( |
| 314 | DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED), |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 315 | MockUtils.checkUserHandle(DpmMockContext.CALLER_USER_HANDLE)); |
| 316 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 317 | verify(getServices().ipackageManager, times(1)).setApplicationEnabledSetting( |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 318 | eq(admin1.getPackageName()), |
| 319 | eq(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT), |
| 320 | eq(PackageManager.DONT_KILL_APP), |
| 321 | eq(DpmMockContext.CALLER_USER_HANDLE), |
| 322 | anyString()); |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 323 | |
| 324 | // TODO Verify other calls too. |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 325 | |
| 326 | // Make sure it's active admin1. |
| 327 | assertTrue(dpm.isAdminActive(admin1)); |
| 328 | assertFalse(dpm.isAdminActive(admin2)); |
| 329 | assertFalse(dpm.isAdminActive(admin3)); |
| 330 | |
| 331 | // But not admin1 for a different user. |
| 332 | |
| 333 | // For this to work, caller needs android.permission.INTERACT_ACROSS_USERS_FULL. |
| 334 | // (Because we're checking a different user's status from CALLER_USER_HANDLE.) |
| 335 | mContext.callerPermissions.add("android.permission.INTERACT_ACROSS_USERS_FULL"); |
| 336 | |
| 337 | assertFalse(dpm.isAdminActiveAsUser(admin1, DpmMockContext.CALLER_USER_HANDLE + 1)); |
| 338 | assertFalse(dpm.isAdminActiveAsUser(admin2, DpmMockContext.CALLER_USER_HANDLE + 1)); |
| 339 | |
| 340 | mContext.callerPermissions.remove("android.permission.INTERACT_ACROSS_USERS_FULL"); |
| 341 | |
| 342 | // Next, add one more admin. |
| 343 | // Before doing so, update the application info, now it's enabled. |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 344 | setUpPackageManagerForAdmin(admin2, DpmMockContext.CALLER_UID, |
Christine Franks | 361b825 | 2017-06-23 18:12:46 -0700 | [diff] [blame] | 345 | PackageManager.COMPONENT_ENABLED_STATE_DEFAULT); |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 346 | |
| 347 | dpm.setActiveAdmin(admin2, /* replace =*/ false); |
| 348 | |
| 349 | // Now we have two admins. |
| 350 | assertTrue(dpm.isAdminActive(admin1)); |
| 351 | assertTrue(dpm.isAdminActive(admin2)); |
| 352 | assertFalse(dpm.isAdminActive(admin3)); |
| 353 | |
| 354 | // Admin2 was already enabled, so setApplicationEnabledSetting() shouldn't have called |
| 355 | // again. (times(1) because it was previously called for admin1) |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 356 | verify(getServices().ipackageManager, times(1)).setApplicationEnabledSetting( |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 357 | eq(admin1.getPackageName()), |
| 358 | eq(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT), |
| 359 | eq(PackageManager.DONT_KILL_APP), |
| 360 | eq(DpmMockContext.CALLER_USER_HANDLE), |
| 361 | anyString()); |
| 362 | |
| 363 | // 4. Add the same admin1 again without replace, which should throw. |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 364 | assertExpectException(IllegalArgumentException.class, /* messageRegex= */ null, |
| 365 | () -> dpm.setActiveAdmin(admin1, /* replace =*/ false)); |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 366 | |
| 367 | // 5. Add the same admin1 again with replace, which should succeed. |
| 368 | dpm.setActiveAdmin(admin1, /* replace =*/ true); |
| 369 | |
| 370 | // TODO make sure it's replaced. |
| 371 | |
| 372 | // 6. Test getActiveAdmins() |
| 373 | List<ComponentName> admins = dpm.getActiveAdmins(); |
| 374 | assertEquals(2, admins.size()); |
| 375 | assertEquals(admin1, admins.get(0)); |
| 376 | assertEquals(admin2, admins.get(1)); |
| 377 | |
| 378 | // Another user has no admins. |
| 379 | mContext.callerPermissions.add("android.permission.INTERACT_ACROSS_USERS_FULL"); |
| 380 | |
| 381 | assertEquals(0, DpmTestUtils.getListSizeAllowingNull( |
| 382 | dpm.getActiveAdminsAsUser(DpmMockContext.CALLER_USER_HANDLE + 1))); |
| 383 | |
| 384 | mContext.callerPermissions.remove("android.permission.INTERACT_ACROSS_USERS_FULL"); |
| 385 | } |
| 386 | |
Makoto Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 387 | public void testSetActiveAdmin_multiUsers() throws Exception { |
| 388 | |
| 389 | final int ANOTHER_USER_ID = 100; |
| 390 | final int ANOTHER_ADMIN_UID = UserHandle.getUid(ANOTHER_USER_ID, 20456); |
| 391 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 392 | getServices().addUser(ANOTHER_USER_ID, 0); // Add one more user. |
Makoto Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 393 | |
| 394 | // Set up pacakge manager for the other user. |
| 395 | setUpPackageManagerForAdmin(admin2, ANOTHER_ADMIN_UID); |
Makoto Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 396 | |
| 397 | mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); |
| 398 | |
| 399 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 400 | |
| 401 | mMockContext.binder.callingUid = ANOTHER_ADMIN_UID; |
| 402 | dpm.setActiveAdmin(admin2, /* replace =*/ false); |
| 403 | |
| 404 | |
| 405 | mMockContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 406 | assertTrue(dpm.isAdminActive(admin1)); |
| 407 | assertFalse(dpm.isAdminActive(admin2)); |
| 408 | |
| 409 | mMockContext.binder.callingUid = ANOTHER_ADMIN_UID; |
| 410 | assertFalse(dpm.isAdminActive(admin1)); |
| 411 | assertTrue(dpm.isAdminActive(admin2)); |
| 412 | } |
| 413 | |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 414 | /** |
| 415 | * Test for: |
| 416 | * {@link DevicePolicyManager#setActiveAdmin} |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 417 | * with replace=false |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 418 | */ |
| 419 | public void testSetActiveAdmin_twiceWithoutReplace() throws Exception { |
| 420 | // 1. Make sure the caller has proper permissions. |
| 421 | mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); |
| 422 | |
| 423 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 424 | assertTrue(dpm.isAdminActive(admin1)); |
| 425 | |
| 426 | // Add the same admin1 again without replace, which should throw. |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 427 | assertExpectException(IllegalArgumentException.class, /* messageRegex= */ null, |
| 428 | () -> dpm.setActiveAdmin(admin1, /* replace =*/ false)); |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Test for: |
Makoto Onuki | 3ab6f2e | 2015-11-05 13:55:37 -0800 | [diff] [blame] | 433 | * {@link DevicePolicyManager#setActiveAdmin} when the admin isn't protected with |
| 434 | * BIND_DEVICE_ADMIN. |
| 435 | */ |
| 436 | public void testSetActiveAdmin_permissionCheck() throws Exception { |
| 437 | // 1. Make sure the caller has proper permissions. |
| 438 | mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); |
| 439 | |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 440 | assertExpectException(IllegalArgumentException.class, |
| 441 | /* messageRegex= */ permission.BIND_DEVICE_ADMIN, |
| 442 | () -> dpm.setActiveAdmin(adminNoPerm, /* replace =*/ false)); |
Makoto Onuki | 3ab6f2e | 2015-11-05 13:55:37 -0800 | [diff] [blame] | 443 | assertFalse(dpm.isAdminActive(adminNoPerm)); |
| 444 | |
| 445 | // Change the target API level to MNC. Now it can be set as DA. |
| 446 | setUpPackageManagerForAdmin(adminNoPerm, DpmMockContext.CALLER_UID, null, |
| 447 | VERSION_CODES.M); |
| 448 | dpm.setActiveAdmin(adminNoPerm, /* replace =*/ false); |
| 449 | assertTrue(dpm.isAdminActive(adminNoPerm)); |
| 450 | |
| 451 | // TODO Test the "load from the file" case where DA will still be loaded even without |
| 452 | // BIND_DEVICE_ADMIN and target API is N. |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Test for: |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 457 | * {@link DevicePolicyManager#removeActiveAdmin} |
| 458 | */ |
| 459 | public void testRemoveActiveAdmin_SecurityException() { |
| 460 | mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); |
| 461 | |
| 462 | // Add admin. |
| 463 | |
| 464 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 465 | |
| 466 | assertTrue(dpm.isAdminActive(admin1)); |
| 467 | |
| 468 | assertFalse(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE)); |
| 469 | |
| 470 | // Directly call the DPMS method with a different userid, which should fail. |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 471 | assertExpectException(SecurityException.class, /* messageRegex =*/ null, |
| 472 | () -> dpms.removeActiveAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE + 1)); |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 473 | |
| 474 | // Try to remove active admin with a different caller userid should fail too, without |
| 475 | // having MANAGE_DEVICE_ADMINS. |
| 476 | mContext.callerPermissions.clear(); |
| 477 | |
Makoto Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 478 | // Change the caller, and call into DPMS directly with a different user-id. |
| 479 | |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 480 | mContext.binder.callingUid = 1234567; |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 481 | assertExpectException(SecurityException.class, /* messageRegex =*/ null, |
| 482 | () -> dpms.removeActiveAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE)); |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | /** |
Makoto Onuki | 1a5ee77 | 2016-02-12 15:34:57 -0800 | [diff] [blame] | 486 | * {@link DevicePolicyManager#removeActiveAdmin} should fail with the user is not unlocked |
| 487 | * (because we can't send the remove broadcast). |
| 488 | */ |
| 489 | public void testRemoveActiveAdmin_userNotRunningOrLocked() { |
| 490 | mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); |
| 491 | |
| 492 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 493 | |
| 494 | // Add admin. |
| 495 | |
| 496 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 497 | |
| 498 | assertTrue(dpm.isAdminActive(admin1)); |
| 499 | |
| 500 | assertFalse(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE)); |
| 501 | |
| 502 | // 1. User not unlocked. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 503 | when(getServices().userManager.isUserUnlocked(eq(DpmMockContext.CALLER_USER_HANDLE))) |
Makoto Onuki | 1a5ee77 | 2016-02-12 15:34:57 -0800 | [diff] [blame] | 504 | .thenReturn(false); |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 505 | assertExpectException(IllegalStateException.class, |
| 506 | /* messageRegex= */ "User must be running and unlocked", |
| 507 | () -> dpm.removeActiveAdmin(admin1)); |
Makoto Onuki | 1a5ee77 | 2016-02-12 15:34:57 -0800 | [diff] [blame] | 508 | |
| 509 | assertFalse(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE)); |
| 510 | |
| 511 | // 2. User unlocked. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 512 | when(getServices().userManager.isUserUnlocked(eq(DpmMockContext.CALLER_USER_HANDLE))) |
Makoto Onuki | 1a5ee77 | 2016-02-12 15:34:57 -0800 | [diff] [blame] | 513 | .thenReturn(true); |
| 514 | |
| 515 | dpm.removeActiveAdmin(admin1); |
Suprabh Shukla | 3cb2b49 | 2016-08-09 17:20:57 -0700 | [diff] [blame] | 516 | assertFalse(dpm.isAdminActiveAsUser(admin1, DpmMockContext.CALLER_USER_HANDLE)); |
Makoto Onuki | 1a5ee77 | 2016-02-12 15:34:57 -0800 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /** |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 520 | * Test for: |
| 521 | * {@link DevicePolicyManager#removeActiveAdmin} |
| 522 | */ |
Makoto Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 523 | public void testRemoveActiveAdmin_fromDifferentUserWithINTERACT_ACROSS_USERS_FULL() { |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 524 | mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); |
| 525 | |
| 526 | // Add admin1. |
| 527 | |
| 528 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 529 | |
| 530 | assertTrue(dpm.isAdminActive(admin1)); |
| 531 | assertFalse(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE)); |
| 532 | |
| 533 | // Different user, but should work, because caller has proper permissions. |
| 534 | mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); |
Makoto Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 535 | |
| 536 | // Change the caller, and call into DPMS directly with a different user-id. |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 537 | mContext.binder.callingUid = 1234567; |
Makoto Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 538 | |
| 539 | dpms.removeActiveAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE); |
Suprabh Shukla | 3cb2b49 | 2016-08-09 17:20:57 -0700 | [diff] [blame] | 540 | assertFalse(dpm.isAdminActiveAsUser(admin1, DpmMockContext.CALLER_USER_HANDLE)); |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 541 | |
| 542 | // TODO DO Still can't be removed in this case. |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Test for: |
| 547 | * {@link DevicePolicyManager#removeActiveAdmin} |
| 548 | */ |
| 549 | public void testRemoveActiveAdmin_sameUserNoMANAGE_DEVICE_ADMINS() { |
| 550 | // Need MANAGE_DEVICE_ADMINS for setActiveAdmin. We'll remove it later. |
| 551 | mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); |
| 552 | |
| 553 | // Add admin1. |
| 554 | |
| 555 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 556 | |
| 557 | assertTrue(dpm.isAdminActive(admin1)); |
| 558 | assertFalse(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE)); |
| 559 | |
| 560 | // Broadcast from saveSettingsLocked(). |
| 561 | verify(mContext.spiedContext, times(1)).sendBroadcastAsUser( |
| 562 | MockUtils.checkIntentAction( |
| 563 | DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED), |
| 564 | MockUtils.checkUserHandle(DpmMockContext.CALLER_USER_HANDLE)); |
| 565 | |
| 566 | // Remove. No permissions, but same user, so it'll work. |
| 567 | mContext.callerPermissions.clear(); |
| 568 | dpm.removeActiveAdmin(admin1); |
| 569 | |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 570 | verify(mContext.spiedContext).sendOrderedBroadcastAsUser( |
| 571 | MockUtils.checkIntentAction( |
| 572 | DeviceAdminReceiver.ACTION_DEVICE_ADMIN_DISABLED), |
| 573 | MockUtils.checkUserHandle(DpmMockContext.CALLER_USER_HANDLE), |
| 574 | isNull(String.class), |
Suprabh Shukla | 3cb2b49 | 2016-08-09 17:20:57 -0700 | [diff] [blame] | 575 | any(BroadcastReceiver.class), |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 576 | eq(dpms.mHandler), |
| 577 | eq(Activity.RESULT_OK), |
| 578 | isNull(String.class), |
| 579 | isNull(Bundle.class)); |
| 580 | |
Suprabh Shukla | 3cb2b49 | 2016-08-09 17:20:57 -0700 | [diff] [blame] | 581 | assertFalse(dpm.isAdminActiveAsUser(admin1, DpmMockContext.CALLER_USER_HANDLE)); |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 582 | |
| 583 | // Again broadcast from saveSettingsLocked(). |
| 584 | verify(mContext.spiedContext, times(2)).sendBroadcastAsUser( |
| 585 | MockUtils.checkIntentAction( |
| 586 | DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED), |
| 587 | MockUtils.checkUserHandle(DpmMockContext.CALLER_USER_HANDLE)); |
| 588 | |
| 589 | // TODO Check other internal calls. |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 590 | } |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 591 | |
| 592 | /** |
Robin Lee | d2a73ed | 2016-12-19 09:07:16 +0000 | [diff] [blame] | 593 | * Test for: @{link DevicePolicyManager#setActivePasswordState} |
| 594 | * |
| 595 | * Validates that when the password for a user changes, the notification broadcast intent |
| 596 | * {@link DeviceAdminReceiver#ACTION_PASSWORD_CHANGED} is sent to managed profile owners, in |
| 597 | * addition to ones in the original user. |
| 598 | */ |
| 599 | public void testSetActivePasswordState_sendToProfiles() throws Exception { |
| 600 | mContext.callerPermissions.add(permission.BIND_DEVICE_ADMIN); |
| 601 | |
| 602 | final int MANAGED_PROFILE_USER_ID = 78; |
| 603 | final int MANAGED_PROFILE_ADMIN_UID = |
| 604 | UserHandle.getUid(MANAGED_PROFILE_USER_ID, DpmMockContext.SYSTEM_UID); |
| 605 | |
| 606 | // Setup device owner. |
| 607 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 608 | mContext.packageName = admin1.getPackageName(); |
| 609 | setupDeviceOwner(); |
| 610 | |
| 611 | // Add a managed profile belonging to the system user. |
| 612 | addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); |
| 613 | |
| 614 | // Change the parent user's password. |
| 615 | dpm.reportPasswordChanged(UserHandle.USER_SYSTEM); |
| 616 | |
| 617 | // Both the device owner and the managed profile owner should receive this broadcast. |
| 618 | final Intent intent = new Intent(DeviceAdminReceiver.ACTION_PASSWORD_CHANGED); |
| 619 | intent.setComponent(admin1); |
| 620 | intent.putExtra(Intent.EXTRA_USER, UserHandle.of(UserHandle.USER_SYSTEM)); |
| 621 | |
| 622 | verify(mContext.spiedContext, times(1)).sendBroadcastAsUser( |
| 623 | MockUtils.checkIntent(intent), |
| 624 | MockUtils.checkUserHandle(UserHandle.USER_SYSTEM)); |
| 625 | verify(mContext.spiedContext, times(1)).sendBroadcastAsUser( |
| 626 | MockUtils.checkIntent(intent), |
| 627 | MockUtils.checkUserHandle(MANAGED_PROFILE_USER_ID)); |
| 628 | } |
| 629 | |
| 630 | /** |
| 631 | * Test for: @{link DevicePolicyManager#setActivePasswordState} |
| 632 | * |
| 633 | * Validates that when the password for a managed profile changes, the notification broadcast |
| 634 | * intent {@link DeviceAdminReceiver#ACTION_PASSWORD_CHANGED} is only sent to the profile, not |
| 635 | * its parent. |
| 636 | */ |
| 637 | public void testSetActivePasswordState_notSentToParent() throws Exception { |
| 638 | mContext.callerPermissions.add(permission.BIND_DEVICE_ADMIN); |
| 639 | |
| 640 | final int MANAGED_PROFILE_USER_ID = 78; |
| 641 | final int MANAGED_PROFILE_ADMIN_UID = |
| 642 | UserHandle.getUid(MANAGED_PROFILE_USER_ID, DpmMockContext.SYSTEM_UID); |
| 643 | |
| 644 | // Setup device owner. |
| 645 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 646 | mContext.packageName = admin1.getPackageName(); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 647 | doReturn(true).when(getServices().lockPatternUtils) |
Robin Lee | d2a73ed | 2016-12-19 09:07:16 +0000 | [diff] [blame] | 648 | .isSeparateProfileChallengeEnabled(MANAGED_PROFILE_USER_ID); |
| 649 | setupDeviceOwner(); |
| 650 | |
| 651 | // Add a managed profile belonging to the system user. |
| 652 | addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); |
| 653 | |
| 654 | // Change the profile's password. |
| 655 | dpm.reportPasswordChanged(MANAGED_PROFILE_USER_ID); |
| 656 | |
| 657 | // Both the device owner and the managed profile owner should receive this broadcast. |
| 658 | final Intent intent = new Intent(DeviceAdminReceiver.ACTION_PASSWORD_CHANGED); |
| 659 | intent.setComponent(admin1); |
| 660 | intent.putExtra(Intent.EXTRA_USER, UserHandle.of(MANAGED_PROFILE_USER_ID)); |
| 661 | |
| 662 | verify(mContext.spiedContext, never()).sendBroadcastAsUser( |
| 663 | MockUtils.checkIntent(intent), |
| 664 | MockUtils.checkUserHandle(UserHandle.USER_SYSTEM)); |
| 665 | verify(mContext.spiedContext, times(1)).sendBroadcastAsUser( |
| 666 | MockUtils.checkIntent(intent), |
| 667 | MockUtils.checkUserHandle(MANAGED_PROFILE_USER_ID)); |
| 668 | } |
| 669 | /** |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 670 | * Test for: {@link DevicePolicyManager#setDeviceOwner} DO on system user installs successfully. |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 671 | */ |
| 672 | public void testSetDeviceOwner() throws Exception { |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 673 | setDeviceOwner(); |
| 674 | |
| 675 | // Try to set a profile owner on the same user, which should fail. |
| 676 | setUpPackageManagerForAdmin(admin2, DpmMockContext.CALLER_SYSTEM_USER_UID); |
| 677 | dpm.setActiveAdmin(admin2, /* refreshing= */ true, UserHandle.USER_SYSTEM); |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 678 | assertExpectException(IllegalStateException.class, |
| 679 | /* messageRegex= */ "already has a device owner", |
| 680 | () -> dpm.setProfileOwner(admin2, "owner-name", UserHandle.USER_SYSTEM)); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 681 | |
| 682 | // DO admin can't be deactivated. |
| 683 | dpm.removeActiveAdmin(admin1); |
| 684 | assertTrue(dpm.isAdminActive(admin1)); |
| 685 | |
| 686 | // TODO Test getDeviceOwnerName() too. To do so, we need to change |
| 687 | // DPMS.getApplicationLabel() because Context.createPackageContextAsUser() is not mockable. |
| 688 | } |
| 689 | |
| 690 | private void setDeviceOwner() throws Exception { |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 691 | mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 692 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 693 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 694 | mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); |
| 695 | |
Makoto Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 696 | // In this test, change the caller user to "system". |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 697 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 698 | |
Makoto Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 699 | // Make sure admin1 is installed on system user. |
| 700 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); |
Makoto Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 701 | |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 702 | // Check various get APIs. |
| 703 | checkGetDeviceOwnerInfoApi(dpm, /* hasDeviceOwner =*/ false); |
| 704 | |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 705 | // DO needs to be an DA. |
| 706 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 707 | |
| 708 | // Fire! |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 709 | assertTrue(dpm.setDeviceOwner(admin1, "owner-name")); |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 710 | |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 711 | // getDeviceOwnerComponent should return the admin1 component. |
| 712 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnCallingUser()); |
| 713 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser()); |
| 714 | |
| 715 | // Check various get APIs. |
| 716 | checkGetDeviceOwnerInfoApi(dpm, /* hasDeviceOwner =*/ true); |
| 717 | |
| 718 | // getDeviceOwnerComponent should *NOT* return the admin1 component for other users. |
| 719 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 720 | assertEquals(null, dpm.getDeviceOwnerComponentOnCallingUser()); |
| 721 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser()); |
| 722 | |
| 723 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 724 | |
Makoto Onuki | 219bbaf | 2015-11-12 01:38:47 +0000 | [diff] [blame] | 725 | // Verify internal calls. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 726 | verify(getServices().iactivityManager, times(1)).updateDeviceOwner( |
Makoto Onuki | 219bbaf | 2015-11-12 01:38:47 +0000 | [diff] [blame] | 727 | eq(admin1.getPackageName())); |
| 728 | |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 729 | // TODO We should check if the caller has called clearCallerIdentity(). |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 730 | verify(getServices().ibackupManager, times(1)).setBackupServiceActive( |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 731 | eq(UserHandle.USER_SYSTEM), eq(false)); |
| 732 | |
| 733 | verify(mContext.spiedContext, times(1)).sendBroadcastAsUser( |
| 734 | MockUtils.checkIntentAction(DevicePolicyManager.ACTION_DEVICE_OWNER_CHANGED), |
| 735 | MockUtils.checkUserHandle(UserHandle.USER_SYSTEM)); |
| 736 | |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 737 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser()); |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 740 | private void checkGetDeviceOwnerInfoApi(DevicePolicyManager dpm, boolean hasDeviceOwner) { |
| 741 | final int origCallingUser = mContext.binder.callingUid; |
| 742 | final List origPermissions = new ArrayList(mContext.callerPermissions); |
| 743 | mContext.callerPermissions.clear(); |
| 744 | |
| 745 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
| 746 | |
| 747 | mContext.binder.callingUid = Process.SYSTEM_UID; |
| 748 | |
| 749 | // TODO Test getDeviceOwnerName() too. To do so, we need to change |
| 750 | // DPMS.getApplicationLabel() because Context.createPackageContextAsUser() is not mockable. |
| 751 | if (hasDeviceOwner) { |
| 752 | assertTrue(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 753 | assertTrue(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())); |
| 754 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnCallingUser()); |
| 755 | |
| 756 | assertTrue(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())); |
| 757 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser()); |
| 758 | assertEquals(UserHandle.USER_SYSTEM, dpm.getDeviceOwnerUserId()); |
| 759 | } else { |
| 760 | assertFalse(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 761 | assertFalse(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())); |
| 762 | assertEquals(null, dpm.getDeviceOwnerComponentOnCallingUser()); |
| 763 | |
| 764 | assertFalse(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())); |
| 765 | assertEquals(null, dpm.getDeviceOwnerComponentOnAnyUser()); |
| 766 | assertEquals(UserHandle.USER_NULL, dpm.getDeviceOwnerUserId()); |
| 767 | } |
| 768 | |
| 769 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 770 | if (hasDeviceOwner) { |
| 771 | assertTrue(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 772 | assertTrue(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())); |
| 773 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnCallingUser()); |
| 774 | |
| 775 | assertTrue(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())); |
| 776 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser()); |
| 777 | assertEquals(UserHandle.USER_SYSTEM, dpm.getDeviceOwnerUserId()); |
| 778 | } else { |
| 779 | assertFalse(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 780 | assertFalse(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())); |
| 781 | assertEquals(null, dpm.getDeviceOwnerComponentOnCallingUser()); |
| 782 | |
| 783 | assertFalse(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())); |
| 784 | assertEquals(null, dpm.getDeviceOwnerComponentOnAnyUser()); |
| 785 | assertEquals(UserHandle.USER_NULL, dpm.getDeviceOwnerUserId()); |
| 786 | } |
| 787 | |
| 788 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 789 | // Still with MANAGE_USERS. |
| 790 | assertFalse(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 791 | assertFalse(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())); |
| 792 | assertEquals(null, dpm.getDeviceOwnerComponentOnCallingUser()); |
| 793 | |
| 794 | if (hasDeviceOwner) { |
| 795 | assertTrue(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())); |
| 796 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser()); |
| 797 | assertEquals(UserHandle.USER_SYSTEM, dpm.getDeviceOwnerUserId()); |
| 798 | } else { |
| 799 | assertFalse(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())); |
| 800 | assertEquals(null, dpm.getDeviceOwnerComponentOnAnyUser()); |
| 801 | assertEquals(UserHandle.USER_NULL, dpm.getDeviceOwnerUserId()); |
| 802 | } |
| 803 | |
| 804 | mContext.binder.callingUid = Process.SYSTEM_UID; |
| 805 | mContext.callerPermissions.remove(permission.MANAGE_USERS); |
| 806 | // System can still call "OnAnyUser" without MANAGE_USERS. |
| 807 | if (hasDeviceOwner) { |
| 808 | assertTrue(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 809 | assertTrue(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())); |
| 810 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnCallingUser()); |
| 811 | |
| 812 | assertTrue(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())); |
| 813 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser()); |
| 814 | assertEquals(UserHandle.USER_SYSTEM, dpm.getDeviceOwnerUserId()); |
| 815 | } else { |
| 816 | assertFalse(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 817 | assertFalse(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())); |
| 818 | assertEquals(null, dpm.getDeviceOwnerComponentOnCallingUser()); |
| 819 | |
| 820 | assertFalse(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())); |
| 821 | assertEquals(null, dpm.getDeviceOwnerComponentOnAnyUser()); |
| 822 | assertEquals(UserHandle.USER_NULL, dpm.getDeviceOwnerUserId()); |
| 823 | } |
| 824 | |
| 825 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 826 | // Still no MANAGE_USERS. |
| 827 | if (hasDeviceOwner) { |
| 828 | assertTrue(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 829 | assertTrue(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())); |
| 830 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnCallingUser()); |
| 831 | } else { |
| 832 | assertFalse(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 833 | assertFalse(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())); |
| 834 | assertEquals(null, dpm.getDeviceOwnerComponentOnCallingUser()); |
| 835 | } |
| 836 | |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 837 | assertExpectException(SecurityException.class, /* messageRegex =*/ null, |
| 838 | () -> dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())); |
| 839 | assertExpectException(SecurityException.class, /* messageRegex =*/ null, |
| 840 | dpm::getDeviceOwnerComponentOnAnyUser); |
| 841 | assertExpectException(SecurityException.class, /* messageRegex =*/ null, |
| 842 | dpm::getDeviceOwnerUserId); |
| 843 | assertExpectException(SecurityException.class, /* messageRegex =*/ null, |
| 844 | dpm::getDeviceOwnerNameOnAnyUser); |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 845 | |
| 846 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 847 | // Still no MANAGE_USERS. |
| 848 | assertFalse(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 849 | assertFalse(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())); |
| 850 | assertEquals(null, dpm.getDeviceOwnerComponentOnCallingUser()); |
| 851 | |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 852 | assertExpectException(SecurityException.class, /* messageRegex =*/ null, |
| 853 | () -> dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())); |
| 854 | assertExpectException(SecurityException.class, /* messageRegex =*/ null, |
| 855 | dpm::getDeviceOwnerComponentOnAnyUser); |
| 856 | assertExpectException(SecurityException.class, /* messageRegex =*/ null, |
| 857 | dpm::getDeviceOwnerUserId); |
| 858 | assertExpectException(SecurityException.class, /* messageRegex =*/ null, |
| 859 | dpm::getDeviceOwnerNameOnAnyUser); |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 860 | |
| 861 | // Restore. |
| 862 | mContext.binder.callingUid = origCallingUser; |
| 863 | mContext.callerPermissions.addAll(origPermissions); |
| 864 | } |
| 865 | |
| 866 | |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 867 | /** |
| 868 | * Test for: {@link DevicePolicyManager#setDeviceOwner} Package doesn't exist. |
| 869 | */ |
| 870 | public void testSetDeviceOwner_noSuchPackage() { |
| 871 | mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 872 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 873 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 874 | mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); |
| 875 | |
| 876 | // Call from a process on the system user. |
| 877 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 878 | |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 879 | assertExpectException(IllegalArgumentException.class, |
| 880 | /* messageRegex= */ "Invalid component", |
| 881 | () -> dpm.setDeviceOwner(new ComponentName("a.b.c", ".def"))); |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | public void testSetDeviceOwner_failures() throws Exception { |
| 885 | // TODO Test more failure cases. Basically test all chacks in enforceCanSetDeviceOwner(). |
| 886 | } |
| 887 | |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 888 | public void testClearDeviceOwner() throws Exception { |
| 889 | mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 890 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 891 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 892 | mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); |
| 893 | |
| 894 | // Set admin1 as a DA to the secondary user. |
| 895 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_UID); |
| 896 | |
| 897 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 898 | |
| 899 | // Set admin 1 as the DO to the system user. |
| 900 | |
| 901 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 902 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); |
| 903 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 904 | assertTrue(dpm.setDeviceOwner(admin1, "owner-name")); |
| 905 | |
Makoto Onuki | 219bbaf | 2015-11-12 01:38:47 +0000 | [diff] [blame] | 906 | // Verify internal calls. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 907 | verify(getServices().iactivityManager, times(1)).updateDeviceOwner( |
Makoto Onuki | 219bbaf | 2015-11-12 01:38:47 +0000 | [diff] [blame] | 908 | eq(admin1.getPackageName())); |
| 909 | |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 910 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser()); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 911 | |
Makoto Onuki | 90b8965 | 2016-01-28 14:44:18 -0800 | [diff] [blame] | 912 | dpm.addUserRestriction(admin1, UserManager.DISALLOW_ADD_USER); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 913 | when(getServices().userManager.hasUserRestriction(eq(UserManager.DISALLOW_ADD_USER), |
Victor Chang | 348f696 | 2017-01-30 16:19:13 +0000 | [diff] [blame] | 914 | MockUtils.checkUserHandle(UserHandle.USER_SYSTEM))).thenReturn(true); |
Makoto Onuki | 90b8965 | 2016-01-28 14:44:18 -0800 | [diff] [blame] | 915 | |
| 916 | assertTrue(dpm.isAdminActive(admin1)); |
| 917 | assertFalse(dpm.isRemovingAdmin(admin1, UserHandle.USER_SYSTEM)); |
| 918 | |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 919 | // Set up other mocks. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 920 | when(getServices().userManager.getUserRestrictions()).thenReturn(new Bundle()); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 921 | |
| 922 | // Now call clear. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 923 | doReturn(DpmMockContext.CALLER_SYSTEM_USER_UID).when(getServices().packageManager). |
| 924 | getPackageUidAsUser(eq(admin1.getPackageName()), anyInt()); |
Makoto Onuki | 1a5ee77 | 2016-02-12 15:34:57 -0800 | [diff] [blame] | 925 | |
| 926 | // But first pretend the user is locked. Then it should fail. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 927 | when(getServices().userManager.isUserUnlocked(anyInt())).thenReturn(false); |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 928 | assertExpectException(IllegalStateException.class, |
| 929 | /* messageRegex= */ "User must be running and unlocked", |
| 930 | () -> dpm.clearDeviceOwnerApp(admin1.getPackageName())); |
Makoto Onuki | 1a5ee77 | 2016-02-12 15:34:57 -0800 | [diff] [blame] | 931 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 932 | when(getServices().userManager.isUserUnlocked(anyInt())).thenReturn(true); |
| 933 | reset(getServices().userManagerInternal); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 934 | dpm.clearDeviceOwnerApp(admin1.getPackageName()); |
| 935 | |
| 936 | // Now DO shouldn't be set. |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 937 | assertNull(dpm.getDeviceOwnerComponentOnAnyUser()); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 938 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 939 | verify(getServices().userManager).setUserRestriction(eq(UserManager.DISALLOW_ADD_USER), |
Victor Chang | 348f696 | 2017-01-30 16:19:13 +0000 | [diff] [blame] | 940 | eq(false), |
| 941 | MockUtils.checkUserHandle(UserHandle.USER_SYSTEM)); |
| 942 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 943 | verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( |
Makoto Onuki | 90b8965 | 2016-01-28 14:44:18 -0800 | [diff] [blame] | 944 | eq(UserHandle.USER_SYSTEM), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 945 | eq(null), |
| 946 | eq(true), eq(CAMERA_NOT_DISABLED)); |
Makoto Onuki | 90b8965 | 2016-01-28 14:44:18 -0800 | [diff] [blame] | 947 | |
Suprabh Shukla | 3cb2b49 | 2016-08-09 17:20:57 -0700 | [diff] [blame] | 948 | assertFalse(dpm.isAdminActiveAsUser(admin1, UserHandle.USER_SYSTEM)); |
Tony Mak | 1970f97 | 2016-08-30 17:41:48 +0100 | [diff] [blame] | 949 | |
| 950 | // ACTION_DEVICE_OWNER_CHANGED should be sent twice, once for setting the device owner |
| 951 | // and once for clearing it. |
| 952 | verify(mContext.spiedContext, times(2)).sendBroadcastAsUser( |
| 953 | MockUtils.checkIntentAction(DevicePolicyManager.ACTION_DEVICE_OWNER_CHANGED), |
| 954 | MockUtils.checkUserHandle(UserHandle.USER_SYSTEM)); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 955 | // TODO Check other calls. |
| 956 | } |
| 957 | |
| 958 | public void testClearDeviceOwner_fromDifferentUser() throws Exception { |
| 959 | mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 960 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 961 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 962 | mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); |
| 963 | |
| 964 | // Set admin1 as a DA to the secondary user. |
| 965 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_UID); |
| 966 | |
| 967 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 968 | |
| 969 | // Set admin 1 as the DO to the system user. |
| 970 | |
| 971 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 972 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); |
| 973 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 974 | assertTrue(dpm.setDeviceOwner(admin1, "owner-name")); |
| 975 | |
Makoto Onuki | 219bbaf | 2015-11-12 01:38:47 +0000 | [diff] [blame] | 976 | // Verify internal calls. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 977 | verify(getServices().iactivityManager, times(1)).updateDeviceOwner( |
Makoto Onuki | 219bbaf | 2015-11-12 01:38:47 +0000 | [diff] [blame] | 978 | eq(admin1.getPackageName())); |
| 979 | |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 980 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser()); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 981 | |
| 982 | // Now call clear from the secondary user, which should throw. |
| 983 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 984 | |
| 985 | // Now call clear. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 986 | doReturn(DpmMockContext.CALLER_UID).when(getServices().packageManager).getPackageUidAsUser( |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 987 | eq(admin1.getPackageName()), |
| 988 | anyInt()); |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 989 | assertExpectException(SecurityException.class, |
| 990 | /* messageRegex =*/ "clearDeviceOwner can only be called by the device owner", |
| 991 | () -> dpm.clearDeviceOwnerApp(admin1.getPackageName())); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 992 | |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 993 | // DO shouldn't be removed. |
| 994 | assertTrue(dpm.isDeviceManaged()); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 995 | } |
| 996 | |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 997 | public void testSetProfileOwner() throws Exception { |
| 998 | setAsProfileOwner(admin1); |
Makoto Onuki | 803d675 | 2015-10-30 12:58:39 -0700 | [diff] [blame] | 999 | |
Makoto Onuki | 90b8965 | 2016-01-28 14:44:18 -0800 | [diff] [blame] | 1000 | // PO admin can't be deactivated. |
| 1001 | dpm.removeActiveAdmin(admin1); |
| 1002 | assertTrue(dpm.isAdminActive(admin1)); |
| 1003 | |
Makoto Onuki | 803d675 | 2015-10-30 12:58:39 -0700 | [diff] [blame] | 1004 | // Try setting DO on the same user, which should fail. |
| 1005 | setUpPackageManagerForAdmin(admin2, DpmMockContext.CALLER_UID); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1006 | mServiceContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 1007 | runAsCaller(mServiceContext, dpms, dpm -> { |
| 1008 | dpm.setActiveAdmin(admin2, /* refreshing= */ true, DpmMockContext.CALLER_USER_HANDLE); |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 1009 | assertExpectException(IllegalStateException.class, |
| 1010 | /* messageRegex= */ "already has a profile owner", |
| 1011 | () -> dpm.setDeviceOwner(admin2, "owner-name", |
| 1012 | DpmMockContext.CALLER_USER_HANDLE)); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1013 | }); |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
Makoto Onuki | 90b8965 | 2016-01-28 14:44:18 -0800 | [diff] [blame] | 1016 | public void testClearProfileOwner() throws Exception { |
| 1017 | setAsProfileOwner(admin1); |
| 1018 | |
| 1019 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 1020 | |
| 1021 | assertTrue(dpm.isProfileOwnerApp(admin1.getPackageName())); |
| 1022 | assertFalse(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE)); |
| 1023 | |
Makoto Onuki | 1a5ee77 | 2016-02-12 15:34:57 -0800 | [diff] [blame] | 1024 | // First try when the user is locked, which should fail. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1025 | when(getServices().userManager.isUserUnlocked(anyInt())) |
Makoto Onuki | 1a5ee77 | 2016-02-12 15:34:57 -0800 | [diff] [blame] | 1026 | .thenReturn(false); |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 1027 | assertExpectException(IllegalStateException.class, |
| 1028 | /* messageRegex= */ "User must be running and unlocked", |
| 1029 | () -> dpm.clearProfileOwner(admin1)); |
| 1030 | |
Makoto Onuki | 1a5ee77 | 2016-02-12 15:34:57 -0800 | [diff] [blame] | 1031 | // Clear, really. |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 1032 | when(getServices().userManager.isUserUnlocked(anyInt())).thenReturn(true); |
Makoto Onuki | 90b8965 | 2016-01-28 14:44:18 -0800 | [diff] [blame] | 1033 | dpm.clearProfileOwner(admin1); |
| 1034 | |
| 1035 | // Check |
| 1036 | assertFalse(dpm.isProfileOwnerApp(admin1.getPackageName())); |
Suprabh Shukla | 3cb2b49 | 2016-08-09 17:20:57 -0700 | [diff] [blame] | 1037 | assertFalse(dpm.isAdminActiveAsUser(admin1, DpmMockContext.CALLER_USER_HANDLE)); |
Makoto Onuki | 90b8965 | 2016-01-28 14:44:18 -0800 | [diff] [blame] | 1038 | } |
| 1039 | |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 1040 | public void testSetProfileOwner_failures() throws Exception { |
| 1041 | // TODO Test more failure cases. Basically test all chacks in enforceCanSetProfileOwner(). |
| 1042 | } |
| 1043 | |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 1044 | public void testGetDeviceOwnerAdminLocked() throws Exception { |
| 1045 | checkDeviceOwnerWithMultipleDeviceAdmins(); |
| 1046 | } |
| 1047 | |
| 1048 | private void checkDeviceOwnerWithMultipleDeviceAdmins() throws Exception { |
| 1049 | // In ths test, we use 3 users (system + 2 secondary users), set some device admins to them, |
| 1050 | // set admin2 on CALLER_USER_HANDLE as DO, then call getDeviceOwnerAdminLocked() to |
| 1051 | // make sure it gets the right component from the right user. |
| 1052 | |
| 1053 | final int ANOTHER_USER_ID = 100; |
| 1054 | final int ANOTHER_ADMIN_UID = UserHandle.getUid(ANOTHER_USER_ID, 456); |
| 1055 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1056 | getServices().addUser(ANOTHER_USER_ID, 0); // Add one more user. |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 1057 | |
| 1058 | mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 1059 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 1060 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 1061 | mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); |
| 1062 | |
| 1063 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 1064 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1065 | when(getServices().userManagerForMock.isSplitSystemUser()).thenReturn(true); |
Victor Chang | e29cd47 | 2016-03-02 20:57:42 +0000 | [diff] [blame] | 1066 | |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 1067 | // Make sure the admin packge is installed to each user. |
| 1068 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); |
| 1069 | setUpPackageManagerForAdmin(admin3, DpmMockContext.CALLER_SYSTEM_USER_UID); |
| 1070 | |
| 1071 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_UID); |
| 1072 | setUpPackageManagerForAdmin(admin2, DpmMockContext.CALLER_UID); |
| 1073 | |
| 1074 | setUpPackageManagerForAdmin(admin2, ANOTHER_ADMIN_UID); |
| 1075 | |
| 1076 | |
| 1077 | // Set active admins to the users. |
| 1078 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 1079 | dpm.setActiveAdmin(admin3, /* replace =*/ false); |
| 1080 | |
| 1081 | dpm.setActiveAdmin(admin1, /* replace =*/ false, DpmMockContext.CALLER_USER_HANDLE); |
| 1082 | dpm.setActiveAdmin(admin2, /* replace =*/ false, DpmMockContext.CALLER_USER_HANDLE); |
| 1083 | |
| 1084 | dpm.setActiveAdmin(admin2, /* replace =*/ false, ANOTHER_USER_ID); |
| 1085 | |
| 1086 | // Set DO on the first non-system user. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1087 | getServices().setUserRunning(DpmMockContext.CALLER_USER_HANDLE, true); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 1088 | assertTrue(dpm.setDeviceOwner(admin2, "owner-name", DpmMockContext.CALLER_USER_HANDLE)); |
| 1089 | |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 1090 | assertEquals(admin2, dpms.getDeviceOwnerComponent(/* callingUserOnly =*/ false)); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 1091 | |
| 1092 | // Then check getDeviceOwnerAdminLocked(). |
| 1093 | assertEquals(admin2, dpms.getDeviceOwnerAdminLocked().info.getComponent()); |
| 1094 | assertEquals(DpmMockContext.CALLER_UID, dpms.getDeviceOwnerAdminLocked().getUid()); |
| 1095 | } |
| 1096 | |
| 1097 | /** |
| 1098 | * This essentially tests |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 1099 | * {@code DevicePolicyManagerService.findOwnerComponentIfNecessaryLocked()}. (which is |
| 1100 | * private.) |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 1101 | * |
| 1102 | * We didn't use to persist the DO component class name, but now we do, and the above method |
| 1103 | * finds the right component from a package name upon migration. |
| 1104 | */ |
| 1105 | public void testDeviceOwnerMigration() throws Exception { |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1106 | when(getServices().userManagerForMock.isSplitSystemUser()).thenReturn(true); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 1107 | checkDeviceOwnerWithMultipleDeviceAdmins(); |
| 1108 | |
| 1109 | // Overwrite the device owner setting and clears the clas name. |
| 1110 | dpms.mOwners.setDeviceOwner( |
| 1111 | new ComponentName(admin2.getPackageName(), ""), |
| 1112 | "owner-name", DpmMockContext.CALLER_USER_HANDLE); |
| 1113 | dpms.mOwners.writeDeviceOwner(); |
| 1114 | |
| 1115 | // Make sure the DO component name doesn't have a class name. |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 1116 | assertEquals("", dpms.getDeviceOwnerComponent(/* callingUserOnly =*/ false).getClassName()); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 1117 | |
| 1118 | // Then create a new DPMS to have it load the settings from files. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1119 | when(getServices().userManager.getUserRestrictions(any(UserHandle.class))) |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1120 | .thenReturn(new Bundle()); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 1121 | initializeDpms(); |
| 1122 | |
| 1123 | // Now the DO component name is a full name. |
| 1124 | // *BUT* because both admin1 and admin2 belong to the same package, we think admin1 is the |
| 1125 | // DO. |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 1126 | assertEquals(admin1, dpms.getDeviceOwnerComponent(/* callingUserOnly =*/ false)); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 1127 | } |
| 1128 | |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 1129 | public void testSetGetApplicationRestriction() { |
| 1130 | setAsProfileOwner(admin1); |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1131 | mContext.packageName = admin1.getPackageName(); |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 1132 | |
| 1133 | { |
| 1134 | Bundle rest = new Bundle(); |
| 1135 | rest.putString("KEY_STRING", "Foo1"); |
| 1136 | dpm.setApplicationRestrictions(admin1, "pkg1", rest); |
| 1137 | } |
| 1138 | |
| 1139 | { |
| 1140 | Bundle rest = new Bundle(); |
| 1141 | rest.putString("KEY_STRING", "Foo2"); |
| 1142 | dpm.setApplicationRestrictions(admin1, "pkg2", rest); |
| 1143 | } |
| 1144 | |
| 1145 | { |
| 1146 | Bundle returned = dpm.getApplicationRestrictions(admin1, "pkg1"); |
| 1147 | assertNotNull(returned); |
| 1148 | assertEquals(returned.size(), 1); |
| 1149 | assertEquals(returned.get("KEY_STRING"), "Foo1"); |
| 1150 | } |
| 1151 | |
| 1152 | { |
| 1153 | Bundle returned = dpm.getApplicationRestrictions(admin1, "pkg2"); |
| 1154 | assertNotNull(returned); |
| 1155 | assertEquals(returned.size(), 1); |
| 1156 | assertEquals(returned.get("KEY_STRING"), "Foo2"); |
| 1157 | } |
| 1158 | |
| 1159 | dpm.setApplicationRestrictions(admin1, "pkg2", new Bundle()); |
| 1160 | assertEquals(0, dpm.getApplicationRestrictions(admin1, "pkg2").size()); |
| 1161 | } |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1162 | |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1163 | /** |
Esteban Talavera | 7e4cbad | 2017-03-30 17:59:50 +0100 | [diff] [blame] | 1164 | * Setup a package in the package manager mock for {@link DpmMockContext#CALLER_USER_HANDLE}. |
| 1165 | * Useful for faking installed applications. |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1166 | * |
| 1167 | * @param packageName the name of the package to be setup |
| 1168 | * @param appId the application ID to be given to the package |
| 1169 | * @return the UID of the package as known by the mock package manager |
| 1170 | */ |
| 1171 | private int setupPackageInPackageManager(final String packageName, final int appId) |
| 1172 | throws Exception { |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1173 | return setupPackageInPackageManager(packageName, DpmMockContext.CALLER_USER_HANDLE, appId, |
Esteban Talavera | 7e4cbad | 2017-03-30 17:59:50 +0100 | [diff] [blame] | 1174 | ApplicationInfo.FLAG_HAS_CODE); |
| 1175 | } |
| 1176 | |
| 1177 | /** |
| 1178 | * Setup a package in the package manager mock. Useful for faking installed applications. |
| 1179 | * |
| 1180 | * @param packageName the name of the package to be setup |
| 1181 | * @param userId the user id where the package will be "installed" |
| 1182 | * @param appId the application ID to be given to the package |
| 1183 | * @param flags flags to set in the ApplicationInfo for this package |
| 1184 | * @return the UID of the package as known by the mock package manager |
| 1185 | */ |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1186 | private int setupPackageInPackageManager(final String packageName, int userId, final int appId, |
| 1187 | int flags) throws Exception { |
| 1188 | final int uid = UserHandle.getUid(userId, appId); |
| 1189 | // Make the PackageManager return the package instead of throwing NameNotFoundException |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1190 | final PackageInfo pi = new PackageInfo(); |
| 1191 | pi.applicationInfo = new ApplicationInfo(); |
Esteban Talavera | 7e4cbad | 2017-03-30 17:59:50 +0100 | [diff] [blame] | 1192 | pi.applicationInfo.flags = flags; |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1193 | doReturn(pi).when(getServices().ipackageManager).getPackageInfo( |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1194 | eq(packageName), |
| 1195 | anyInt(), |
Esteban Talavera | 7e4cbad | 2017-03-30 17:59:50 +0100 | [diff] [blame] | 1196 | eq(userId)); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1197 | doReturn(pi.applicationInfo).when(getServices().ipackageManager).getApplicationInfo( |
Esteban Talavera | 7e4cbad | 2017-03-30 17:59:50 +0100 | [diff] [blame] | 1198 | eq(packageName), |
| 1199 | anyInt(), |
| 1200 | eq(userId)); |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1201 | // Setup application UID with the PackageManager |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1202 | doReturn(uid).when(getServices().packageManager).getPackageUidAsUser( |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1203 | eq(packageName), |
Esteban Talavera | 7e4cbad | 2017-03-30 17:59:50 +0100 | [diff] [blame] | 1204 | eq(userId)); |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1205 | // Associate packageName to uid |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1206 | doReturn(packageName).when(getServices().ipackageManager).getNameForUid(eq(uid)); |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1207 | doReturn(new String[]{packageName}) |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1208 | .when(getServices().ipackageManager).getPackagesForUid(eq(uid)); |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1209 | return uid; |
| 1210 | } |
| 1211 | |
Robin Lee | 7f5c91c | 2017-02-08 21:27:02 +0000 | [diff] [blame] | 1212 | public void testCertificateDisclosure() throws Exception { |
| 1213 | final int userId = DpmMockContext.CALLER_USER_HANDLE; |
| 1214 | final UserHandle user = UserHandle.of(userId); |
| 1215 | |
| 1216 | mContext.applicationInfo = new ApplicationInfo(); |
| 1217 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
| 1218 | mContext.packageName = "com.android.frameworks.servicestests"; |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1219 | getServices().addPackageContext(user, mContext); |
Robin Lee | 7f5c91c | 2017-02-08 21:27:02 +0000 | [diff] [blame] | 1220 | when(mContext.resources.getColor(anyInt(), anyObject())).thenReturn(Color.WHITE); |
| 1221 | |
Robin Lee | abaa069 | 2017-02-20 20:54:22 +0000 | [diff] [blame] | 1222 | StringParceledListSlice oneCert = asSlice(new String[] {"1"}); |
| 1223 | StringParceledListSlice fourCerts = asSlice(new String[] {"1", "2", "3", "4"}); |
Robin Lee | 7f5c91c | 2017-02-08 21:27:02 +0000 | [diff] [blame] | 1224 | |
| 1225 | final String TEST_STRING = "Test for exactly 2 certs out of 4"; |
| 1226 | doReturn(TEST_STRING).when(mContext.resources).getQuantityText(anyInt(), eq(2)); |
| 1227 | |
| 1228 | // Given that we have exactly one certificate installed, |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1229 | when(getServices().keyChainConnection.getService().getUserCaAliases()).thenReturn(oneCert); |
Robin Lee | 7f5c91c | 2017-02-08 21:27:02 +0000 | [diff] [blame] | 1230 | // when that certificate is approved, |
Robin Lee | abaa069 | 2017-02-20 20:54:22 +0000 | [diff] [blame] | 1231 | dpms.approveCaCert(oneCert.getList().get(0), userId, true); |
Robin Lee | 7f5c91c | 2017-02-08 21:27:02 +0000 | [diff] [blame] | 1232 | // a notification should not be shown. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1233 | verify(getServices().notificationManager, timeout(1000)) |
Robin Lee | 7f5c91c | 2017-02-08 21:27:02 +0000 | [diff] [blame] | 1234 | .cancelAsUser(anyString(), anyInt(), eq(user)); |
| 1235 | |
| 1236 | // Given that we have four certificates installed, |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1237 | when(getServices().keyChainConnection.getService().getUserCaAliases()).thenReturn(fourCerts); |
Robin Lee | 7f5c91c | 2017-02-08 21:27:02 +0000 | [diff] [blame] | 1238 | // when two of them are approved (one of them approved twice hence no action), |
Robin Lee | abaa069 | 2017-02-20 20:54:22 +0000 | [diff] [blame] | 1239 | dpms.approveCaCert(fourCerts.getList().get(0), userId, true); |
| 1240 | dpms.approveCaCert(fourCerts.getList().get(1), userId, true); |
Robin Lee | 7f5c91c | 2017-02-08 21:27:02 +0000 | [diff] [blame] | 1241 | // a notification should be shown saying that there are two certificates left to approve. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1242 | verify(getServices().notificationManager, timeout(1000)) |
Robin Lee | 7f5c91c | 2017-02-08 21:27:02 +0000 | [diff] [blame] | 1243 | .notifyAsUser(anyString(), anyInt(), argThat( |
| 1244 | new BaseMatcher<Notification>() { |
| 1245 | @Override |
| 1246 | public boolean matches(Object item) { |
| 1247 | final Notification noti = (Notification) item; |
| 1248 | return TEST_STRING.equals( |
| 1249 | noti.extras.getString(Notification.EXTRA_TITLE)); |
| 1250 | } |
| 1251 | @Override |
| 1252 | public void describeTo(Description description) { |
| 1253 | description.appendText( |
| 1254 | "Notification{title=\"" + TEST_STRING + "\"}"); |
| 1255 | } |
| 1256 | }), eq(user)); |
| 1257 | } |
| 1258 | |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1259 | /** |
| 1260 | * Simple test for delegate set/get and general delegation. Tests verifying that delegated |
| 1261 | * privileges can acually be exercised by a delegate are not covered here. |
| 1262 | */ |
| 1263 | public void testDelegation() throws Exception { |
| 1264 | setAsProfileOwner(admin1); |
| 1265 | |
| 1266 | final int userHandle = DpmMockContext.CALLER_USER_HANDLE; |
| 1267 | |
| 1268 | // Given two packages |
| 1269 | final String CERT_DELEGATE = "com.delegate.certs"; |
| 1270 | final String RESTRICTIONS_DELEGATE = "com.delegate.apprestrictions"; |
| 1271 | final int CERT_DELEGATE_UID = setupPackageInPackageManager(CERT_DELEGATE, 20988); |
| 1272 | final int RESTRICTIONS_DELEGATE_UID = setupPackageInPackageManager(RESTRICTIONS_DELEGATE, |
| 1273 | 20989); |
| 1274 | |
| 1275 | // On delegation |
| 1276 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 1277 | mContext.packageName = admin1.getPackageName(); |
| 1278 | dpm.setCertInstallerPackage(admin1, CERT_DELEGATE); |
| 1279 | dpm.setApplicationRestrictionsManagingPackage(admin1, RESTRICTIONS_DELEGATE); |
| 1280 | |
| 1281 | // DPMS correctly stores and retrieves the delegates |
| 1282 | DevicePolicyManagerService.DevicePolicyData policy = dpms.mUserData.get(userHandle); |
| 1283 | assertEquals(2, policy.mDelegationMap.size()); |
| 1284 | MoreAsserts.assertContentsInAnyOrder(policy.mDelegationMap.get(CERT_DELEGATE), |
| 1285 | DELEGATION_CERT_INSTALL); |
| 1286 | MoreAsserts.assertContentsInAnyOrder(dpm.getDelegatedScopes(admin1, CERT_DELEGATE), |
| 1287 | DELEGATION_CERT_INSTALL); |
| 1288 | assertEquals(CERT_DELEGATE, dpm.getCertInstallerPackage(admin1)); |
| 1289 | MoreAsserts.assertContentsInAnyOrder(policy.mDelegationMap.get(RESTRICTIONS_DELEGATE), |
| 1290 | DELEGATION_APP_RESTRICTIONS); |
| 1291 | MoreAsserts.assertContentsInAnyOrder(dpm.getDelegatedScopes(admin1, RESTRICTIONS_DELEGATE), |
| 1292 | DELEGATION_APP_RESTRICTIONS); |
| 1293 | assertEquals(RESTRICTIONS_DELEGATE, dpm.getApplicationRestrictionsManagingPackage(admin1)); |
| 1294 | |
| 1295 | // On calling install certificate APIs from an unauthorized process |
| 1296 | mContext.binder.callingUid = RESTRICTIONS_DELEGATE_UID; |
| 1297 | mContext.packageName = RESTRICTIONS_DELEGATE; |
| 1298 | |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 1299 | assertExpectException(SecurityException.class, /* messageRegex =*/ null, |
| 1300 | () -> dpm.installCaCert(null, null)); |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1301 | |
| 1302 | // On calling install certificate APIs from an authorized process |
| 1303 | mContext.binder.callingUid = CERT_DELEGATE_UID; |
| 1304 | mContext.packageName = CERT_DELEGATE; |
| 1305 | |
| 1306 | // DPMS executes without a SecurityException |
| 1307 | try { |
| 1308 | dpm.installCaCert(null, null); |
| 1309 | } catch (SecurityException unexpected) { |
| 1310 | fail("Threw SecurityException on authorized access"); |
| 1311 | } catch (NullPointerException expected) { |
| 1312 | } |
| 1313 | |
| 1314 | // On removing a delegate |
| 1315 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 1316 | mContext.packageName = admin1.getPackageName(); |
| 1317 | dpm.setCertInstallerPackage(admin1, null); |
| 1318 | |
| 1319 | // DPMS does not allow access to ex-delegate |
| 1320 | mContext.binder.callingUid = CERT_DELEGATE_UID; |
| 1321 | mContext.packageName = CERT_DELEGATE; |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 1322 | assertExpectException(SecurityException.class, /* messageRegex =*/ null, |
| 1323 | () -> dpm.installCaCert(null, null)); |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1324 | |
| 1325 | // But still allows access to other existing delegates |
| 1326 | mContext.binder.callingUid = RESTRICTIONS_DELEGATE_UID; |
| 1327 | mContext.packageName = RESTRICTIONS_DELEGATE; |
| 1328 | try { |
| 1329 | dpm.getApplicationRestrictions(null, "pkg"); |
| 1330 | } catch (SecurityException expected) { |
| 1331 | fail("Threw SecurityException on authorized access"); |
| 1332 | } |
| 1333 | } |
| 1334 | |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1335 | public void testApplicationRestrictionsManagingApp() throws Exception { |
| 1336 | setAsProfileOwner(admin1); |
| 1337 | |
Rubin Xu | ed1928a | 2016-02-11 17:23:06 +0000 | [diff] [blame] | 1338 | final String nonExistAppRestrictionsManagerPackage = "com.google.app.restrictions.manager2"; |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1339 | final String appRestrictionsManagerPackage = "com.google.app.restrictions.manager"; |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 1340 | final String nonDelegateExceptionMessageRegex = |
| 1341 | "Caller with uid \\d+ is not a delegate of scope delegation-app-restrictions."; |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1342 | final int appRestrictionsManagerAppId = 20987; |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1343 | final int appRestrictionsManagerUid = setupPackageInPackageManager( |
| 1344 | appRestrictionsManagerPackage, appRestrictionsManagerAppId); |
Rubin Xu | ed1928a | 2016-02-11 17:23:06 +0000 | [diff] [blame] | 1345 | |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1346 | // appRestrictionsManager package shouldn't be able to manage restrictions as the PO hasn't |
| 1347 | // delegated that permission yet. |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1348 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 1349 | mContext.packageName = admin1.getPackageName(); |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1350 | assertFalse(dpm.isCallerApplicationRestrictionsManagingPackage()); |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 1351 | final Bundle rest = new Bundle(); |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1352 | rest.putString("KEY_STRING", "Foo1"); |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 1353 | assertExpectException(SecurityException.class, nonDelegateExceptionMessageRegex, |
| 1354 | () -> dpm.setApplicationRestrictions(null, "pkg1", rest)); |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1355 | |
| 1356 | // Check via the profile owner that no restrictions were set. |
| 1357 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1358 | mContext.packageName = admin1.getPackageName(); |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1359 | assertEquals(0, dpm.getApplicationRestrictions(admin1, "pkg1").size()); |
| 1360 | |
Rubin Xu | ed1928a | 2016-02-11 17:23:06 +0000 | [diff] [blame] | 1361 | // Check the API does not allow setting a non-existent package |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 1362 | assertExpectException(PackageManager.NameNotFoundException.class, |
| 1363 | /* messageRegex= */ nonExistAppRestrictionsManagerPackage, |
| 1364 | () -> dpm.setApplicationRestrictionsManagingPackage( |
| 1365 | admin1, nonExistAppRestrictionsManagerPackage)); |
Rubin Xu | ed1928a | 2016-02-11 17:23:06 +0000 | [diff] [blame] | 1366 | |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1367 | // Let appRestrictionsManagerPackage manage app restrictions |
| 1368 | dpm.setApplicationRestrictionsManagingPackage(admin1, appRestrictionsManagerPackage); |
| 1369 | assertEquals(appRestrictionsManagerPackage, |
| 1370 | dpm.getApplicationRestrictionsManagingPackage(admin1)); |
| 1371 | |
| 1372 | // Now that package should be able to set and retrieve app restrictions. |
| 1373 | mContext.binder.callingUid = appRestrictionsManagerUid; |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1374 | mContext.packageName = appRestrictionsManagerPackage; |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1375 | assertTrue(dpm.isCallerApplicationRestrictionsManagingPackage()); |
| 1376 | dpm.setApplicationRestrictions(null, "pkg1", rest); |
| 1377 | Bundle returned = dpm.getApplicationRestrictions(null, "pkg1"); |
| 1378 | assertEquals(1, returned.size(), 1); |
| 1379 | assertEquals("Foo1", returned.get("KEY_STRING")); |
| 1380 | |
| 1381 | // The same app running on a separate user shouldn't be able to manage app restrictions. |
| 1382 | mContext.binder.callingUid = UserHandle.getUid( |
| 1383 | UserHandle.USER_SYSTEM, appRestrictionsManagerAppId); |
| 1384 | assertFalse(dpm.isCallerApplicationRestrictionsManagingPackage()); |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 1385 | assertExpectException(SecurityException.class, nonDelegateExceptionMessageRegex, |
| 1386 | () -> dpm.setApplicationRestrictions(null, "pkg1", rest)); |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1387 | |
| 1388 | // The DPM is still able to manage app restrictions, even if it allowed another app to do it |
| 1389 | // too. |
| 1390 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1391 | mContext.packageName = admin1.getPackageName(); |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1392 | assertEquals(returned, dpm.getApplicationRestrictions(admin1, "pkg1")); |
| 1393 | dpm.setApplicationRestrictions(admin1, "pkg1", null); |
| 1394 | assertEquals(0, dpm.getApplicationRestrictions(admin1, "pkg1").size()); |
| 1395 | |
| 1396 | // Removing the ability for the package to manage app restrictions. |
| 1397 | dpm.setApplicationRestrictionsManagingPackage(admin1, null); |
| 1398 | assertNull(dpm.getApplicationRestrictionsManagingPackage(admin1)); |
| 1399 | mContext.binder.callingUid = appRestrictionsManagerUid; |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1400 | mContext.packageName = appRestrictionsManagerPackage; |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1401 | assertFalse(dpm.isCallerApplicationRestrictionsManagingPackage()); |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 1402 | assertExpectException(SecurityException.class, nonDelegateExceptionMessageRegex, |
| 1403 | () -> dpm.setApplicationRestrictions(null, "pkg1", null)); |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1406 | public void testSetUserRestriction_asDo() throws Exception { |
| 1407 | mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 1408 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1409 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 1410 | mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); |
| 1411 | |
| 1412 | // First, set DO. |
| 1413 | |
| 1414 | // Call from a process on the system user. |
| 1415 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 1416 | |
| 1417 | // Make sure admin1 is installed on system user. |
| 1418 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1419 | |
| 1420 | // Call. |
| 1421 | dpm.setActiveAdmin(admin1, /* replace =*/ false, UserHandle.USER_SYSTEM); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 1422 | assertTrue(dpm.setDeviceOwner(admin1, "owner-name", |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1423 | UserHandle.USER_SYSTEM)); |
| 1424 | |
Esteban Talavera | 6c9116a | 2016-11-24 16:12:44 +0000 | [diff] [blame] | 1425 | // Check that the user restrictions that are enabled by default are set. Then unset them. |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 1426 | final String[] defaultRestrictions = UserRestrictionsUtils |
Esteban Talavera | 548a04b | 2016-12-20 15:22:30 +0000 | [diff] [blame] | 1427 | .getDefaultEnabledForDeviceOwner().toArray(new String[0]); |
Esteban Talavera | 6c9116a | 2016-11-24 16:12:44 +0000 | [diff] [blame] | 1428 | DpmTestUtils.assertRestrictions( |
| 1429 | DpmTestUtils.newRestrictions(defaultRestrictions), |
| 1430 | dpms.getDeviceOwnerAdminLocked().ensureUserRestrictions() |
| 1431 | ); |
| 1432 | DpmTestUtils.assertRestrictions( |
| 1433 | DpmTestUtils.newRestrictions(defaultRestrictions), |
| 1434 | dpm.getUserRestrictions(admin1) |
| 1435 | ); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1436 | verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( |
Esteban Talavera | 548a04b | 2016-12-20 15:22:30 +0000 | [diff] [blame] | 1437 | eq(UserHandle.USER_SYSTEM), |
Nicolas Prevot | 2ea46fe | 2017-01-05 10:29:34 +0000 | [diff] [blame] | 1438 | MockUtils.checkUserRestrictions(defaultRestrictions), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1439 | eq(true) /* isDeviceOwner */, |
| 1440 | eq(CAMERA_NOT_DISABLED) |
Esteban Talavera | 548a04b | 2016-12-20 15:22:30 +0000 | [diff] [blame] | 1441 | ); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1442 | reset(getServices().userManagerInternal); |
Esteban Talavera | 6c9116a | 2016-11-24 16:12:44 +0000 | [diff] [blame] | 1443 | |
| 1444 | for (String restriction : defaultRestrictions) { |
| 1445 | dpm.clearUserRestriction(admin1, restriction); |
| 1446 | } |
| 1447 | |
Esteban Talavera | 548a04b | 2016-12-20 15:22:30 +0000 | [diff] [blame] | 1448 | assertNoDeviceOwnerRestrictions(); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1449 | reset(getServices().userManagerInternal); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1450 | |
| 1451 | dpm.addUserRestriction(admin1, UserManager.DISALLOW_ADD_USER); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1452 | verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1453 | eq(UserHandle.USER_SYSTEM), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1454 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_ADD_USER), |
| 1455 | eq(true), eq(CAMERA_NOT_DISABLED)); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1456 | reset(getServices().userManagerInternal); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1457 | |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1458 | dpm.addUserRestriction(admin1, UserManager.DISALLOW_OUTGOING_CALLS); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1459 | verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1460 | eq(UserHandle.USER_SYSTEM), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1461 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_OUTGOING_CALLS, |
| 1462 | UserManager.DISALLOW_ADD_USER), |
| 1463 | eq(true), eq(CAMERA_NOT_DISABLED)); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1464 | reset(getServices().userManagerInternal); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1465 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1466 | DpmTestUtils.assertRestrictions( |
| 1467 | DpmTestUtils.newRestrictions( |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1468 | UserManager.DISALLOW_ADD_USER, UserManager.DISALLOW_OUTGOING_CALLS), |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1469 | dpms.getDeviceOwnerAdminLocked().ensureUserRestrictions() |
| 1470 | ); |
Makoto Onuki | 3a3092f | 2015-10-30 11:07:51 -0700 | [diff] [blame] | 1471 | DpmTestUtils.assertRestrictions( |
| 1472 | DpmTestUtils.newRestrictions( |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1473 | UserManager.DISALLOW_ADD_USER, UserManager.DISALLOW_OUTGOING_CALLS), |
Makoto Onuki | 3a3092f | 2015-10-30 11:07:51 -0700 | [diff] [blame] | 1474 | dpm.getUserRestrictions(admin1) |
| 1475 | ); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1476 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1477 | dpm.clearUserRestriction(admin1, UserManager.DISALLOW_ADD_USER); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1478 | verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1479 | eq(UserHandle.USER_SYSTEM), |
| 1480 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_OUTGOING_CALLS), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1481 | eq(true), eq(CAMERA_NOT_DISABLED)); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1482 | reset(getServices().userManagerInternal); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1483 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1484 | DpmTestUtils.assertRestrictions( |
| 1485 | DpmTestUtils.newRestrictions(UserManager.DISALLOW_OUTGOING_CALLS), |
| 1486 | dpms.getDeviceOwnerAdminLocked().ensureUserRestrictions() |
| 1487 | ); |
Makoto Onuki | 3a3092f | 2015-10-30 11:07:51 -0700 | [diff] [blame] | 1488 | DpmTestUtils.assertRestrictions( |
| 1489 | DpmTestUtils.newRestrictions(UserManager.DISALLOW_OUTGOING_CALLS), |
| 1490 | dpm.getUserRestrictions(admin1) |
| 1491 | ); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1492 | |
| 1493 | dpm.clearUserRestriction(admin1, UserManager.DISALLOW_OUTGOING_CALLS); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1494 | verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1495 | eq(UserHandle.USER_SYSTEM), |
| 1496 | MockUtils.checkUserRestrictions(), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1497 | eq(true), eq(CAMERA_NOT_DISABLED)); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1498 | reset(getServices().userManagerInternal); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1499 | |
Esteban Talavera | 548a04b | 2016-12-20 15:22:30 +0000 | [diff] [blame] | 1500 | assertNoDeviceOwnerRestrictions(); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1501 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1502 | // DISALLOW_ADJUST_VOLUME and DISALLOW_UNMUTE_MICROPHONE are PO restrictions, but when |
| 1503 | // DO sets them, the scope is global. |
| 1504 | dpm.addUserRestriction(admin1, UserManager.DISALLOW_ADJUST_VOLUME); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1505 | reset(getServices().userManagerInternal); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1506 | dpm.addUserRestriction(admin1, UserManager.DISALLOW_UNMUTE_MICROPHONE); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1507 | verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1508 | eq(UserHandle.USER_SYSTEM), |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1509 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_ADJUST_VOLUME, |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1510 | UserManager.DISALLOW_UNMUTE_MICROPHONE), |
| 1511 | eq(true), eq(CAMERA_NOT_DISABLED)); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1512 | reset(getServices().userManagerInternal); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1513 | |
| 1514 | dpm.clearUserRestriction(admin1, UserManager.DISALLOW_ADJUST_VOLUME); |
| 1515 | dpm.clearUserRestriction(admin1, UserManager.DISALLOW_UNMUTE_MICROPHONE); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1516 | reset(getServices().userManagerInternal); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1517 | |
| 1518 | // More tests. |
| 1519 | dpm.addUserRestriction(admin1, UserManager.DISALLOW_ADD_USER); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1520 | verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1521 | eq(UserHandle.USER_SYSTEM), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1522 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_ADD_USER), |
| 1523 | eq(true), eq(CAMERA_NOT_DISABLED)); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1524 | reset(getServices().userManagerInternal); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1525 | |
| 1526 | dpm.addUserRestriction(admin1, UserManager.DISALLOW_FUN); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1527 | verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1528 | eq(UserHandle.USER_SYSTEM), |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1529 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_FUN, |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1530 | UserManager.DISALLOW_ADD_USER), |
| 1531 | eq(true), eq(CAMERA_NOT_DISABLED)); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1532 | reset(getServices().userManagerInternal); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1533 | |
| 1534 | dpm.setCameraDisabled(admin1, true); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1535 | verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1536 | eq(UserHandle.USER_SYSTEM), |
| 1537 | // DISALLOW_CAMERA will be applied to both local and global. |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1538 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_FUN, |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1539 | UserManager.DISALLOW_ADD_USER), |
| 1540 | eq(true), eq(CAMERA_DISABLED_GLOBALLY)); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1541 | reset(getServices().userManagerInternal); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1542 | |
| 1543 | // Set up another DA and let it disable camera. Now DISALLOW_CAMERA will only be applied |
| 1544 | // locally. |
| 1545 | dpm.setCameraDisabled(admin1, false); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1546 | reset(getServices().userManagerInternal); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1547 | |
| 1548 | setUpPackageManagerForAdmin(admin2, DpmMockContext.CALLER_SYSTEM_USER_UID); |
| 1549 | dpm.setActiveAdmin(admin2, /* replace =*/ false, UserHandle.USER_SYSTEM); |
| 1550 | dpm.setCameraDisabled(admin2, true); |
| 1551 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1552 | verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1553 | eq(UserHandle.USER_SYSTEM), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1554 | // DISALLOW_CAMERA will be applied to both local and global. <- TODO: fix this |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1555 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_FUN, |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1556 | UserManager.DISALLOW_ADD_USER), |
| 1557 | eq(true), eq(CAMERA_DISABLED_LOCALLY)); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1558 | reset(getServices().userManagerInternal); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1559 | // TODO Make sure restrictions are written to the file. |
| 1560 | } |
| 1561 | |
| 1562 | public void testSetUserRestriction_asPo() { |
| 1563 | setAsProfileOwner(admin1); |
| 1564 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1565 | DpmTestUtils.assertRestrictions( |
| 1566 | DpmTestUtils.newRestrictions(), |
| 1567 | dpms.getProfileOwnerAdminLocked(DpmMockContext.CALLER_USER_HANDLE) |
| 1568 | .ensureUserRestrictions() |
| 1569 | ); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1570 | |
| 1571 | dpm.addUserRestriction(admin1, UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1572 | verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1573 | eq(DpmMockContext.CALLER_USER_HANDLE), |
| 1574 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1575 | eq(false), eq(CAMERA_NOT_DISABLED)); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1576 | reset(getServices().userManagerInternal); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1577 | |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1578 | dpm.addUserRestriction(admin1, UserManager.DISALLOW_OUTGOING_CALLS); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1579 | verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1580 | eq(DpmMockContext.CALLER_USER_HANDLE), |
| 1581 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, |
| 1582 | UserManager.DISALLOW_OUTGOING_CALLS), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1583 | eq(false), eq(CAMERA_NOT_DISABLED)); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1584 | reset(getServices().userManagerInternal); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1585 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1586 | DpmTestUtils.assertRestrictions( |
| 1587 | DpmTestUtils.newRestrictions( |
| 1588 | UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, |
| 1589 | UserManager.DISALLOW_OUTGOING_CALLS |
| 1590 | ), |
| 1591 | dpms.getProfileOwnerAdminLocked(DpmMockContext.CALLER_USER_HANDLE) |
| 1592 | .ensureUserRestrictions() |
| 1593 | ); |
Makoto Onuki | 3a3092f | 2015-10-30 11:07:51 -0700 | [diff] [blame] | 1594 | DpmTestUtils.assertRestrictions( |
| 1595 | DpmTestUtils.newRestrictions( |
| 1596 | UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, |
| 1597 | UserManager.DISALLOW_OUTGOING_CALLS |
| 1598 | ), |
| 1599 | dpm.getUserRestrictions(admin1) |
| 1600 | ); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1601 | |
| 1602 | dpm.clearUserRestriction(admin1, UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1603 | verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1604 | eq(DpmMockContext.CALLER_USER_HANDLE), |
| 1605 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_OUTGOING_CALLS), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1606 | eq(false), eq(CAMERA_NOT_DISABLED)); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1607 | reset(getServices().userManagerInternal); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1608 | |
| 1609 | DpmTestUtils.assertRestrictions( |
| 1610 | DpmTestUtils.newRestrictions( |
| 1611 | UserManager.DISALLOW_OUTGOING_CALLS |
| 1612 | ), |
| 1613 | dpms.getProfileOwnerAdminLocked(DpmMockContext.CALLER_USER_HANDLE) |
| 1614 | .ensureUserRestrictions() |
| 1615 | ); |
Makoto Onuki | 3a3092f | 2015-10-30 11:07:51 -0700 | [diff] [blame] | 1616 | DpmTestUtils.assertRestrictions( |
| 1617 | DpmTestUtils.newRestrictions( |
| 1618 | UserManager.DISALLOW_OUTGOING_CALLS |
| 1619 | ), |
| 1620 | dpm.getUserRestrictions(admin1) |
| 1621 | ); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1622 | |
| 1623 | dpm.clearUserRestriction(admin1, UserManager.DISALLOW_OUTGOING_CALLS); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1624 | verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1625 | eq(DpmMockContext.CALLER_USER_HANDLE), |
| 1626 | MockUtils.checkUserRestrictions(), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1627 | eq(false), eq(CAMERA_NOT_DISABLED)); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1628 | reset(getServices().userManagerInternal); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1629 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1630 | DpmTestUtils.assertRestrictions( |
| 1631 | DpmTestUtils.newRestrictions(), |
| 1632 | dpms.getProfileOwnerAdminLocked(DpmMockContext.CALLER_USER_HANDLE) |
| 1633 | .ensureUserRestrictions() |
| 1634 | ); |
Makoto Onuki | 3a3092f | 2015-10-30 11:07:51 -0700 | [diff] [blame] | 1635 | DpmTestUtils.assertRestrictions( |
| 1636 | DpmTestUtils.newRestrictions(), |
| 1637 | dpm.getUserRestrictions(admin1) |
| 1638 | ); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1639 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1640 | // DISALLOW_ADJUST_VOLUME and DISALLOW_UNMUTE_MICROPHONE can be set by PO too, even |
| 1641 | // though when DO sets them they'll be applied globally. |
| 1642 | dpm.addUserRestriction(admin1, UserManager.DISALLOW_ADJUST_VOLUME); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1643 | reset(getServices().userManagerInternal); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1644 | dpm.addUserRestriction(admin1, UserManager.DISALLOW_UNMUTE_MICROPHONE); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1645 | verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1646 | eq(DpmMockContext.CALLER_USER_HANDLE), |
| 1647 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_ADJUST_VOLUME, |
| 1648 | UserManager.DISALLOW_UNMUTE_MICROPHONE), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1649 | eq(false), eq(CAMERA_NOT_DISABLED)); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1650 | reset(getServices().userManagerInternal); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1651 | |
| 1652 | dpm.setCameraDisabled(admin1, true); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1653 | verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1654 | eq(DpmMockContext.CALLER_USER_HANDLE), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1655 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_ADJUST_VOLUME, |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1656 | UserManager.DISALLOW_UNMUTE_MICROPHONE), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1657 | eq(false), eq(CAMERA_DISABLED_LOCALLY)); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1658 | reset(getServices().userManagerInternal); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1659 | |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1660 | // TODO Make sure restrictions are written to the file. |
| 1661 | } |
Makoto Onuki | a31ebbc | 2015-11-23 17:15:21 -0800 | [diff] [blame] | 1662 | |
Esteban Talavera | 548a04b | 2016-12-20 15:22:30 +0000 | [diff] [blame] | 1663 | |
| 1664 | public void testDefaultEnabledUserRestrictions() throws Exception { |
| 1665 | mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); |
| 1666 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
| 1667 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 1668 | mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); |
| 1669 | |
| 1670 | // First, set DO. |
| 1671 | |
| 1672 | // Call from a process on the system user. |
| 1673 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 1674 | |
| 1675 | // Make sure admin1 is installed on system user. |
| 1676 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); |
| 1677 | |
| 1678 | dpm.setActiveAdmin(admin1, /* replace =*/ false, UserHandle.USER_SYSTEM); |
| 1679 | assertTrue(dpm.setDeviceOwner(admin1, "owner-name", |
| 1680 | UserHandle.USER_SYSTEM)); |
| 1681 | |
| 1682 | // Check that the user restrictions that are enabled by default are set. Then unset them. |
| 1683 | String[] defaultRestrictions = UserRestrictionsUtils |
| 1684 | .getDefaultEnabledForDeviceOwner().toArray(new String[0]); |
| 1685 | assertTrue(defaultRestrictions.length > 0); |
| 1686 | DpmTestUtils.assertRestrictions( |
| 1687 | DpmTestUtils.newRestrictions(defaultRestrictions), |
| 1688 | dpms.getDeviceOwnerAdminLocked().ensureUserRestrictions() |
| 1689 | ); |
| 1690 | DpmTestUtils.assertRestrictions( |
| 1691 | DpmTestUtils.newRestrictions(defaultRestrictions), |
| 1692 | dpm.getUserRestrictions(admin1) |
| 1693 | ); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1694 | verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( |
Esteban Talavera | 548a04b | 2016-12-20 15:22:30 +0000 | [diff] [blame] | 1695 | eq(UserHandle.USER_SYSTEM), |
Nicolas Prevot | 2ea46fe | 2017-01-05 10:29:34 +0000 | [diff] [blame] | 1696 | MockUtils.checkUserRestrictions(defaultRestrictions), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1697 | eq(true) /* isDeviceOwner */, |
| 1698 | eq(CAMERA_NOT_DISABLED) |
Esteban Talavera | 548a04b | 2016-12-20 15:22:30 +0000 | [diff] [blame] | 1699 | ); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1700 | reset(getServices().userManagerInternal); |
Esteban Talavera | 548a04b | 2016-12-20 15:22:30 +0000 | [diff] [blame] | 1701 | |
| 1702 | for (String restriction : defaultRestrictions) { |
| 1703 | dpm.clearUserRestriction(admin1, restriction); |
| 1704 | } |
| 1705 | |
| 1706 | assertNoDeviceOwnerRestrictions(); |
| 1707 | |
| 1708 | // Initialize DPMS again and check that the user restriction wasn't enabled again. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1709 | reset(getServices().userManagerInternal); |
Esteban Talavera | 548a04b | 2016-12-20 15:22:30 +0000 | [diff] [blame] | 1710 | initializeDpms(); |
| 1711 | assertTrue(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 1712 | assertNotNull(dpms.getDeviceOwnerAdminLocked()); |
| 1713 | |
| 1714 | assertNoDeviceOwnerRestrictions(); |
| 1715 | |
| 1716 | // Add a new restriction to the default set, initialize DPMS, and check that the restriction |
| 1717 | // is set as it wasn't enabled during setDeviceOwner. |
| 1718 | final String newDefaultEnabledRestriction = UserManager.DISALLOW_REMOVE_MANAGED_PROFILE; |
| 1719 | assertFalse(UserRestrictionsUtils |
| 1720 | .getDefaultEnabledForDeviceOwner().contains(newDefaultEnabledRestriction)); |
| 1721 | UserRestrictionsUtils |
| 1722 | .getDefaultEnabledForDeviceOwner().add(newDefaultEnabledRestriction); |
| 1723 | try { |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1724 | reset(getServices().userManagerInternal); |
Esteban Talavera | 548a04b | 2016-12-20 15:22:30 +0000 | [diff] [blame] | 1725 | initializeDpms(); |
| 1726 | assertTrue(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 1727 | assertNotNull(dpms.getDeviceOwnerAdminLocked()); |
| 1728 | |
| 1729 | DpmTestUtils.assertRestrictions( |
| 1730 | DpmTestUtils.newRestrictions(newDefaultEnabledRestriction), |
| 1731 | dpms.getDeviceOwnerAdminLocked().ensureUserRestrictions() |
| 1732 | ); |
| 1733 | DpmTestUtils.assertRestrictions( |
| 1734 | DpmTestUtils.newRestrictions(newDefaultEnabledRestriction), |
| 1735 | dpm.getUserRestrictions(admin1) |
| 1736 | ); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1737 | verify(getServices().userManagerInternal, atLeast(1)).setDevicePolicyUserRestrictions( |
Esteban Talavera | 548a04b | 2016-12-20 15:22:30 +0000 | [diff] [blame] | 1738 | eq(UserHandle.USER_SYSTEM), |
Nicolas Prevot | 2ea46fe | 2017-01-05 10:29:34 +0000 | [diff] [blame] | 1739 | MockUtils.checkUserRestrictions(newDefaultEnabledRestriction), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1740 | eq(true) /* isDeviceOwner */, |
| 1741 | eq(CAMERA_NOT_DISABLED) |
Esteban Talavera | 548a04b | 2016-12-20 15:22:30 +0000 | [diff] [blame] | 1742 | ); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1743 | reset(getServices().userManagerInternal); |
Esteban Talavera | 548a04b | 2016-12-20 15:22:30 +0000 | [diff] [blame] | 1744 | |
| 1745 | // Remove the restriction. |
| 1746 | dpm.clearUserRestriction(admin1, newDefaultEnabledRestriction); |
| 1747 | |
| 1748 | // Initialize DPMS again. The restriction shouldn't be enabled for a second time. |
| 1749 | initializeDpms(); |
| 1750 | assertTrue(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 1751 | assertNotNull(dpms.getDeviceOwnerAdminLocked()); |
| 1752 | assertNoDeviceOwnerRestrictions(); |
| 1753 | } finally { |
| 1754 | UserRestrictionsUtils |
| 1755 | .getDefaultEnabledForDeviceOwner().remove(newDefaultEnabledRestriction); |
| 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | private void assertNoDeviceOwnerRestrictions() { |
| 1760 | DpmTestUtils.assertRestrictions( |
| 1761 | DpmTestUtils.newRestrictions(), |
| 1762 | dpms.getDeviceOwnerAdminLocked().ensureUserRestrictions() |
| 1763 | ); |
| 1764 | DpmTestUtils.assertRestrictions( |
| 1765 | DpmTestUtils.newRestrictions(), |
| 1766 | dpm.getUserRestrictions(admin1) |
| 1767 | ); |
| 1768 | } |
| 1769 | |
Makoto Onuki | a31ebbc | 2015-11-23 17:15:21 -0800 | [diff] [blame] | 1770 | public void testGetMacAddress() throws Exception { |
| 1771 | mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); |
| 1772 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 1773 | mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); |
| 1774 | |
| 1775 | // In this test, change the caller user to "system". |
| 1776 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 1777 | |
| 1778 | // Make sure admin1 is installed on system user. |
| 1779 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); |
| 1780 | |
| 1781 | // Test 1. Caller doesn't have DO or DA. |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 1782 | assertExpectException(SecurityException.class, /* messageRegex= */ "No active admin", |
| 1783 | () -> dpm.getWifiMacAddress(admin1)); |
Makoto Onuki | a31ebbc | 2015-11-23 17:15:21 -0800 | [diff] [blame] | 1784 | |
| 1785 | // DO needs to be an DA. |
| 1786 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 1787 | assertTrue(dpm.isAdminActive(admin1)); |
| 1788 | |
| 1789 | // Test 2. Caller has DA, but not DO. |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 1790 | assertExpectException(SecurityException.class, /* messageRegex= */ NOT_DEVICE_OWNER_MSG, |
| 1791 | () -> dpm.getWifiMacAddress(admin1)); |
Makoto Onuki | a31ebbc | 2015-11-23 17:15:21 -0800 | [diff] [blame] | 1792 | |
| 1793 | // Test 3. Caller has PO, but not DO. |
| 1794 | assertTrue(dpm.setProfileOwner(admin1, null, UserHandle.USER_SYSTEM)); |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 1795 | assertExpectException(SecurityException.class, /* messageRegex= */ NOT_DEVICE_OWNER_MSG, |
| 1796 | () -> dpm.getWifiMacAddress(admin1)); |
Makoto Onuki | a31ebbc | 2015-11-23 17:15:21 -0800 | [diff] [blame] | 1797 | |
| 1798 | // Remove PO. |
| 1799 | dpm.clearProfileOwner(admin1); |
Suprabh Shukla | 3cb2b49 | 2016-08-09 17:20:57 -0700 | [diff] [blame] | 1800 | dpm.setActiveAdmin(admin1, false); |
Makoto Onuki | a31ebbc | 2015-11-23 17:15:21 -0800 | [diff] [blame] | 1801 | // Test 4, Caller is DO now. |
| 1802 | assertTrue(dpm.setDeviceOwner(admin1, null, UserHandle.USER_SYSTEM)); |
| 1803 | |
| 1804 | // 4-1. But no WifiInfo. |
Sudheer Shanka | 3cb4da1 | 2016-03-07 18:51:49 -0800 | [diff] [blame] | 1805 | assertNull(dpm.getWifiMacAddress(admin1)); |
Makoto Onuki | a31ebbc | 2015-11-23 17:15:21 -0800 | [diff] [blame] | 1806 | |
| 1807 | // 4-2. Returns WifiInfo, but with the default MAC. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1808 | when(getServices().wifiManager.getConnectionInfo()).thenReturn(new WifiInfo()); |
Sudheer Shanka | 3cb4da1 | 2016-03-07 18:51:49 -0800 | [diff] [blame] | 1809 | assertNull(dpm.getWifiMacAddress(admin1)); |
Makoto Onuki | a31ebbc | 2015-11-23 17:15:21 -0800 | [diff] [blame] | 1810 | |
| 1811 | // 4-3. With a real MAC address. |
| 1812 | final WifiInfo wi = new WifiInfo(); |
| 1813 | wi.setMacAddress("11:22:33:44:55:66"); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1814 | when(getServices().wifiManager.getConnectionInfo()).thenReturn(wi); |
Sudheer Shanka | 3cb4da1 | 2016-03-07 18:51:49 -0800 | [diff] [blame] | 1815 | assertEquals("11:22:33:44:55:66", dpm.getWifiMacAddress(admin1)); |
Makoto Onuki | a31ebbc | 2015-11-23 17:15:21 -0800 | [diff] [blame] | 1816 | } |
Mahaver Chopra | f8373b5 | 2015-12-23 14:42:18 +0000 | [diff] [blame] | 1817 | |
Mahaver Chopra | 1216ae5 | 2016-03-11 15:39:48 +0000 | [diff] [blame] | 1818 | public void testReboot() throws Exception { |
Mahaver Chopra | f8373b5 | 2015-12-23 14:42:18 +0000 | [diff] [blame] | 1819 | mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); |
| 1820 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 1821 | |
| 1822 | // In this test, change the caller user to "system". |
| 1823 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 1824 | |
| 1825 | // Make sure admin1 is installed on system user. |
| 1826 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); |
| 1827 | |
| 1828 | // Set admin1 as DA. |
| 1829 | dpm.setActiveAdmin(admin1, false); |
| 1830 | assertTrue(dpm.isAdminActive(admin1)); |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 1831 | assertExpectException(SecurityException.class, /* messageRegex= */ NOT_DEVICE_OWNER_MSG, |
| 1832 | () -> dpm.reboot(admin1)); |
Mahaver Chopra | f8373b5 | 2015-12-23 14:42:18 +0000 | [diff] [blame] | 1833 | |
| 1834 | // Set admin1 as PO. |
| 1835 | assertTrue(dpm.setProfileOwner(admin1, null, UserHandle.USER_SYSTEM)); |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 1836 | assertExpectException(SecurityException.class, /* messageRegex= */ NOT_DEVICE_OWNER_MSG, |
| 1837 | () -> dpm.reboot(admin1)); |
Mahaver Chopra | f8373b5 | 2015-12-23 14:42:18 +0000 | [diff] [blame] | 1838 | |
| 1839 | // Remove PO and add DO. |
| 1840 | dpm.clearProfileOwner(admin1); |
Suprabh Shukla | 3cb2b49 | 2016-08-09 17:20:57 -0700 | [diff] [blame] | 1841 | dpm.setActiveAdmin(admin1, false); |
Mahaver Chopra | f8373b5 | 2015-12-23 14:42:18 +0000 | [diff] [blame] | 1842 | assertTrue(dpm.setDeviceOwner(admin1, null, UserHandle.USER_SYSTEM)); |
| 1843 | |
Mahaver Chopra | 1216ae5 | 2016-03-11 15:39:48 +0000 | [diff] [blame] | 1844 | // admin1 is DO. |
| 1845 | // Set current call state of device to ringing. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1846 | when(getServices().telephonyManager.getCallState()) |
Mahaver Chopra | 1216ae5 | 2016-03-11 15:39:48 +0000 | [diff] [blame] | 1847 | .thenReturn(TelephonyManager.CALL_STATE_RINGING); |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 1848 | assertExpectException(IllegalStateException.class, /* messageRegex= */ ONGOING_CALL_MSG, |
| 1849 | () -> dpm.reboot(admin1)); |
Mahaver Chopra | 1216ae5 | 2016-03-11 15:39:48 +0000 | [diff] [blame] | 1850 | |
| 1851 | // Set current call state of device to dialing/active. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1852 | when(getServices().telephonyManager.getCallState()) |
Mahaver Chopra | 1216ae5 | 2016-03-11 15:39:48 +0000 | [diff] [blame] | 1853 | .thenReturn(TelephonyManager.CALL_STATE_OFFHOOK); |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 1854 | assertExpectException(IllegalStateException.class, /* messageRegex= */ ONGOING_CALL_MSG, |
| 1855 | () -> dpm.reboot(admin1)); |
Mahaver Chopra | 1216ae5 | 2016-03-11 15:39:48 +0000 | [diff] [blame] | 1856 | |
| 1857 | // Set current call state of device to idle. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1858 | when(getServices().telephonyManager.getCallState()).thenReturn(TelephonyManager.CALL_STATE_IDLE); |
Mahaver Chopra | f8373b5 | 2015-12-23 14:42:18 +0000 | [diff] [blame] | 1859 | dpm.reboot(admin1); |
| 1860 | } |
Kenny Guy | 06de4e7 | 2015-12-22 12:07:39 +0000 | [diff] [blame] | 1861 | |
| 1862 | public void testSetGetSupportText() { |
| 1863 | mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); |
| 1864 | dpm.setActiveAdmin(admin1, true); |
| 1865 | dpm.setActiveAdmin(admin2, true); |
| 1866 | mContext.callerPermissions.remove(permission.MANAGE_DEVICE_ADMINS); |
| 1867 | |
| 1868 | // Null default support messages. |
| 1869 | { |
| 1870 | assertNull(dpm.getLongSupportMessage(admin1)); |
| 1871 | assertNull(dpm.getShortSupportMessage(admin1)); |
| 1872 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 1873 | assertNull(dpm.getShortSupportMessageForUser(admin1, |
| 1874 | DpmMockContext.CALLER_USER_HANDLE)); |
| 1875 | assertNull(dpm.getLongSupportMessageForUser(admin1, |
| 1876 | DpmMockContext.CALLER_USER_HANDLE)); |
| 1877 | mMockContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 1878 | } |
| 1879 | |
| 1880 | // Only system can call the per user versions. |
| 1881 | { |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 1882 | assertExpectException(SecurityException.class, /* messageRegex= */ "message for user", |
| 1883 | () -> dpm.getShortSupportMessageForUser(admin1, |
| 1884 | DpmMockContext.CALLER_USER_HANDLE)); |
| 1885 | assertExpectException(SecurityException.class, /* messageRegex= */ "message for user", |
| 1886 | () -> dpm.getLongSupportMessageForUser(admin1, |
| 1887 | DpmMockContext.CALLER_USER_HANDLE)); |
Kenny Guy | 06de4e7 | 2015-12-22 12:07:39 +0000 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | // Can't set message for admin in another uid. |
| 1891 | { |
| 1892 | mContext.binder.callingUid = DpmMockContext.CALLER_UID + 1; |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 1893 | assertExpectException(SecurityException.class, |
| 1894 | /* messageRegex= */ "is not owned by uid", |
| 1895 | () -> dpm.setShortSupportMessage(admin1, "Some text")); |
Kenny Guy | 06de4e7 | 2015-12-22 12:07:39 +0000 | [diff] [blame] | 1896 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 1897 | } |
| 1898 | |
| 1899 | // Set/Get short returns what it sets and other admins text isn't changed. |
| 1900 | { |
| 1901 | final String supportText = "Some text to test with."; |
| 1902 | dpm.setShortSupportMessage(admin1, supportText); |
| 1903 | assertEquals(supportText, dpm.getShortSupportMessage(admin1)); |
| 1904 | assertNull(dpm.getLongSupportMessage(admin1)); |
| 1905 | assertNull(dpm.getShortSupportMessage(admin2)); |
| 1906 | |
| 1907 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 1908 | assertEquals(supportText, dpm.getShortSupportMessageForUser(admin1, |
| 1909 | DpmMockContext.CALLER_USER_HANDLE)); |
| 1910 | assertNull(dpm.getShortSupportMessageForUser(admin2, |
| 1911 | DpmMockContext.CALLER_USER_HANDLE)); |
| 1912 | assertNull(dpm.getLongSupportMessageForUser(admin1, |
| 1913 | DpmMockContext.CALLER_USER_HANDLE)); |
| 1914 | mMockContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 1915 | |
| 1916 | dpm.setShortSupportMessage(admin1, null); |
| 1917 | assertNull(dpm.getShortSupportMessage(admin1)); |
| 1918 | } |
| 1919 | |
| 1920 | // Set/Get long returns what it sets and other admins text isn't changed. |
| 1921 | { |
| 1922 | final String supportText = "Some text to test with.\nWith more text."; |
| 1923 | dpm.setLongSupportMessage(admin1, supportText); |
| 1924 | assertEquals(supportText, dpm.getLongSupportMessage(admin1)); |
| 1925 | assertNull(dpm.getShortSupportMessage(admin1)); |
| 1926 | assertNull(dpm.getLongSupportMessage(admin2)); |
| 1927 | |
| 1928 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 1929 | assertEquals(supportText, dpm.getLongSupportMessageForUser(admin1, |
| 1930 | DpmMockContext.CALLER_USER_HANDLE)); |
| 1931 | assertNull(dpm.getLongSupportMessageForUser(admin2, |
| 1932 | DpmMockContext.CALLER_USER_HANDLE)); |
| 1933 | assertNull(dpm.getShortSupportMessageForUser(admin1, |
| 1934 | DpmMockContext.CALLER_USER_HANDLE)); |
| 1935 | mMockContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 1936 | |
| 1937 | dpm.setLongSupportMessage(admin1, null); |
| 1938 | assertNull(dpm.getLongSupportMessage(admin1)); |
| 1939 | } |
| 1940 | } |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 1941 | |
phweiss | 73145f4 | 2017-01-17 19:06:38 +0100 | [diff] [blame] | 1942 | public void testCreateAdminSupportIntent() throws Exception { |
| 1943 | // Setup device owner. |
| 1944 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 1945 | setupDeviceOwner(); |
| 1946 | |
| 1947 | // Nonexisting permission returns null |
| 1948 | Intent intent = dpm.createAdminSupportIntent("disallow_nothing"); |
| 1949 | assertNull(intent); |
| 1950 | |
| 1951 | // Existing permission that is not set returns null |
| 1952 | intent = dpm.createAdminSupportIntent(UserManager.DISALLOW_ADJUST_VOLUME); |
| 1953 | assertNull(intent); |
| 1954 | |
| 1955 | // Existing permission that is not set by device/profile owner returns null |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1956 | when(getServices().userManager.hasUserRestriction( |
phweiss | 73145f4 | 2017-01-17 19:06:38 +0100 | [diff] [blame] | 1957 | eq(UserManager.DISALLOW_ADJUST_VOLUME), |
| 1958 | eq(UserHandle.getUserHandleForUid(mContext.binder.callingUid)))) |
| 1959 | .thenReturn(true); |
| 1960 | intent = dpm.createAdminSupportIntent(UserManager.DISALLOW_ADJUST_VOLUME); |
| 1961 | assertNull(intent); |
| 1962 | |
| 1963 | // Permission that is set by device owner returns correct intent |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 1964 | when(getServices().userManager.getUserRestrictionSource( |
phweiss | 73145f4 | 2017-01-17 19:06:38 +0100 | [diff] [blame] | 1965 | eq(UserManager.DISALLOW_ADJUST_VOLUME), |
| 1966 | eq(UserHandle.getUserHandleForUid(mContext.binder.callingUid)))) |
| 1967 | .thenReturn(UserManager.RESTRICTION_SOURCE_DEVICE_OWNER); |
| 1968 | intent = dpm.createAdminSupportIntent(UserManager.DISALLOW_ADJUST_VOLUME); |
| 1969 | assertNotNull(intent); |
| 1970 | assertEquals(Settings.ACTION_SHOW_ADMIN_SUPPORT_DETAILS, intent.getAction()); |
| 1971 | assertEquals(UserHandle.getUserId(DpmMockContext.CALLER_SYSTEM_USER_UID), |
| 1972 | intent.getIntExtra(Intent.EXTRA_USER_ID, -1)); |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 1973 | assertEquals(admin1, intent.getParcelableExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN)); |
phweiss | 73145f4 | 2017-01-17 19:06:38 +0100 | [diff] [blame] | 1974 | assertEquals(UserManager.DISALLOW_ADJUST_VOLUME, |
| 1975 | intent.getStringExtra(DevicePolicyManager.EXTRA_RESTRICTION)); |
| 1976 | |
| 1977 | // Try with POLICY_DISABLE_CAMERA and POLICY_DISABLE_SCREEN_CAPTURE, which are not |
| 1978 | // user restrictions |
| 1979 | |
| 1980 | // Camera is not disabled |
| 1981 | intent = dpm.createAdminSupportIntent(DevicePolicyManager.POLICY_DISABLE_CAMERA); |
| 1982 | assertNull(intent); |
| 1983 | |
| 1984 | // Camera is disabled |
| 1985 | dpm.setCameraDisabled(admin1, true); |
| 1986 | intent = dpm.createAdminSupportIntent(DevicePolicyManager.POLICY_DISABLE_CAMERA); |
| 1987 | assertNotNull(intent); |
| 1988 | assertEquals(DevicePolicyManager.POLICY_DISABLE_CAMERA, |
| 1989 | intent.getStringExtra(DevicePolicyManager.EXTRA_RESTRICTION)); |
| 1990 | |
| 1991 | // Screen capture is not disabled |
| 1992 | intent = dpm.createAdminSupportIntent(DevicePolicyManager.POLICY_DISABLE_SCREEN_CAPTURE); |
| 1993 | assertNull(intent); |
| 1994 | |
| 1995 | // Screen capture is disabled |
| 1996 | dpm.setScreenCaptureDisabled(admin1, true); |
| 1997 | intent = dpm.createAdminSupportIntent(DevicePolicyManager.POLICY_DISABLE_SCREEN_CAPTURE); |
| 1998 | assertNotNull(intent); |
| 1999 | assertEquals(DevicePolicyManager.POLICY_DISABLE_SCREEN_CAPTURE, |
| 2000 | intent.getStringExtra(DevicePolicyManager.EXTRA_RESTRICTION)); |
| 2001 | |
| 2002 | // Same checks for different user |
| 2003 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 2004 | // Camera should be disabled by device owner |
| 2005 | intent = dpm.createAdminSupportIntent(DevicePolicyManager.POLICY_DISABLE_CAMERA); |
| 2006 | assertNotNull(intent); |
| 2007 | assertEquals(DevicePolicyManager.POLICY_DISABLE_CAMERA, |
| 2008 | intent.getStringExtra(DevicePolicyManager.EXTRA_RESTRICTION)); |
| 2009 | assertEquals(UserHandle.getUserId(DpmMockContext.CALLER_SYSTEM_USER_UID), |
| 2010 | intent.getIntExtra(Intent.EXTRA_USER_ID, -1)); |
| 2011 | // ScreenCapture should not be disabled by device owner |
| 2012 | intent = dpm.createAdminSupportIntent(DevicePolicyManager.POLICY_DISABLE_SCREEN_CAPTURE); |
| 2013 | assertNull(intent); |
| 2014 | } |
| 2015 | |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2016 | /** |
| 2017 | * Test for: |
| 2018 | * {@link DevicePolicyManager#setAffiliationIds} |
Esteban Talavera | c9bb378 | 2016-11-11 15:41:14 +0000 | [diff] [blame] | 2019 | * {@link DevicePolicyManager#getAffiliationIds} |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2020 | * {@link DevicePolicyManager#isAffiliatedUser} |
| 2021 | */ |
| 2022 | public void testUserAffiliation() throws Exception { |
| 2023 | mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); |
| 2024 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2025 | mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); |
| 2026 | |
| 2027 | // Check that the system user is unaffiliated. |
| 2028 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 2029 | assertFalse(dpm.isAffiliatedUser()); |
| 2030 | |
| 2031 | // Set a device owner on the system user. Check that the system user becomes affiliated. |
| 2032 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); |
| 2033 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 2034 | assertTrue(dpm.setDeviceOwner(admin1, "owner-name")); |
| 2035 | assertTrue(dpm.isAffiliatedUser()); |
Esteban Talavera | c9bb378 | 2016-11-11 15:41:14 +0000 | [diff] [blame] | 2036 | assertTrue(dpm.getAffiliationIds(admin1).isEmpty()); |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2037 | |
Esteban Talavera | c9bb378 | 2016-11-11 15:41:14 +0000 | [diff] [blame] | 2038 | // Install a profile owner. Check that the test user is unaffiliated. |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2039 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 2040 | setAsProfileOwner(admin2); |
| 2041 | assertFalse(dpm.isAffiliatedUser()); |
Esteban Talavera | c9bb378 | 2016-11-11 15:41:14 +0000 | [diff] [blame] | 2042 | assertTrue(dpm.getAffiliationIds(admin2).isEmpty()); |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2043 | |
| 2044 | // Have the profile owner specify a set of affiliation ids. Check that the test user remains |
| 2045 | // unaffiliated. |
Tony Mak | 3165743 | 2017-04-25 09:29:55 +0100 | [diff] [blame] | 2046 | final Set<String> userAffiliationIds = new ArraySet<>(); |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2047 | userAffiliationIds.add("red"); |
| 2048 | userAffiliationIds.add("green"); |
| 2049 | userAffiliationIds.add("blue"); |
| 2050 | dpm.setAffiliationIds(admin2, userAffiliationIds); |
Esteban Talavera | c9bb378 | 2016-11-11 15:41:14 +0000 | [diff] [blame] | 2051 | MoreAsserts.assertContentsInAnyOrder(dpm.getAffiliationIds(admin2), "red", "green", "blue"); |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2052 | assertFalse(dpm.isAffiliatedUser()); |
| 2053 | |
| 2054 | // Have the device owner specify a set of affiliation ids that do not intersect with those |
| 2055 | // specified by the profile owner. Check that the test user remains unaffiliated. |
Tony Mak | 3165743 | 2017-04-25 09:29:55 +0100 | [diff] [blame] | 2056 | final Set<String> deviceAffiliationIds = new ArraySet<>(); |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2057 | deviceAffiliationIds.add("cyan"); |
| 2058 | deviceAffiliationIds.add("yellow"); |
| 2059 | deviceAffiliationIds.add("magenta"); |
| 2060 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 2061 | dpm.setAffiliationIds(admin1, deviceAffiliationIds); |
Esteban Talavera | c9bb378 | 2016-11-11 15:41:14 +0000 | [diff] [blame] | 2062 | MoreAsserts.assertContentsInAnyOrder( |
| 2063 | dpm.getAffiliationIds(admin1), "cyan", "yellow", "magenta"); |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2064 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 2065 | assertFalse(dpm.isAffiliatedUser()); |
| 2066 | |
| 2067 | // Have the profile owner specify a set of affiliation ids that intersect with those |
| 2068 | // specified by the device owner. Check that the test user becomes affiliated. |
| 2069 | userAffiliationIds.add("yellow"); |
| 2070 | dpm.setAffiliationIds(admin2, userAffiliationIds); |
Esteban Talavera | c9bb378 | 2016-11-11 15:41:14 +0000 | [diff] [blame] | 2071 | MoreAsserts.assertContentsInAnyOrder( |
| 2072 | dpm.getAffiliationIds(admin2), "red", "green", "blue", "yellow"); |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2073 | assertTrue(dpm.isAffiliatedUser()); |
| 2074 | |
Esteban Talavera | c9bb378 | 2016-11-11 15:41:14 +0000 | [diff] [blame] | 2075 | // Clear affiliation ids for the profile owner. The user becomes unaffiliated. |
Tony Mak | 3165743 | 2017-04-25 09:29:55 +0100 | [diff] [blame] | 2076 | dpm.setAffiliationIds(admin2, Collections.emptySet()); |
Esteban Talavera | c9bb378 | 2016-11-11 15:41:14 +0000 | [diff] [blame] | 2077 | assertTrue(dpm.getAffiliationIds(admin2).isEmpty()); |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2078 | assertFalse(dpm.isAffiliatedUser()); |
| 2079 | |
Esteban Talavera | a7bd04e | 2017-02-02 18:28:04 +0000 | [diff] [blame] | 2080 | // Set affiliation ids again, then clear PO to check that the user becomes unaffiliated |
| 2081 | dpm.setAffiliationIds(admin2, userAffiliationIds); |
| 2082 | assertTrue(dpm.isAffiliatedUser()); |
| 2083 | dpm.clearProfileOwner(admin2); |
| 2084 | assertFalse(dpm.isAffiliatedUser()); |
| 2085 | |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2086 | // Check that the system user remains affiliated. |
| 2087 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 2088 | assertTrue(dpm.isAffiliatedUser()); |
Esteban Talavera | a7bd04e | 2017-02-02 18:28:04 +0000 | [diff] [blame] | 2089 | |
| 2090 | // Clear the device owner - the user becomes unaffiliated. |
| 2091 | clearDeviceOwner(); |
| 2092 | assertFalse(dpm.isAffiliatedUser()); |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2093 | } |
Alan Treadway | afad878 | 2016-01-19 15:15:08 +0000 | [diff] [blame] | 2094 | |
| 2095 | public void testGetUserProvisioningState_defaultResult() { |
| 2096 | assertEquals(DevicePolicyManager.STATE_USER_UNMANAGED, dpm.getUserProvisioningState()); |
| 2097 | } |
| 2098 | |
| 2099 | public void testSetUserProvisioningState_permission() throws Exception { |
| 2100 | setupProfileOwner(); |
| 2101 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2102 | |
| 2103 | exerciseUserProvisioningTransitions(DpmMockContext.CALLER_USER_HANDLE, |
| 2104 | DevicePolicyManager.STATE_USER_SETUP_FINALIZED); |
| 2105 | } |
| 2106 | |
| 2107 | public void testSetUserProvisioningState_unprivileged() throws Exception { |
| 2108 | setupProfileOwner(); |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 2109 | assertExpectException(SecurityException.class, /* messageRegex =*/ null, |
| 2110 | () -> dpm.setUserProvisioningState(DevicePolicyManager.STATE_USER_SETUP_FINALIZED, |
| 2111 | DpmMockContext.CALLER_USER_HANDLE)); |
Alan Treadway | afad878 | 2016-01-19 15:15:08 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
| 2114 | public void testSetUserProvisioningState_noManagement() { |
| 2115 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 2116 | assertExpectException(IllegalStateException.class, |
| 2117 | /* messageRegex= */ "change provisioning state unless a .* owner is set", |
| 2118 | () -> dpm.setUserProvisioningState(DevicePolicyManager.STATE_USER_SETUP_FINALIZED, |
| 2119 | DpmMockContext.CALLER_USER_HANDLE)); |
Alan Treadway | afad878 | 2016-01-19 15:15:08 +0000 | [diff] [blame] | 2120 | assertEquals(DevicePolicyManager.STATE_USER_UNMANAGED, dpm.getUserProvisioningState()); |
| 2121 | } |
| 2122 | |
| 2123 | public void testSetUserProvisioningState_deviceOwnerFromSetupWizard() throws Exception { |
| 2124 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 2125 | setupDeviceOwner(); |
| 2126 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2127 | |
| 2128 | exerciseUserProvisioningTransitions(UserHandle.USER_SYSTEM, |
| 2129 | DevicePolicyManager.STATE_USER_SETUP_COMPLETE, |
| 2130 | DevicePolicyManager.STATE_USER_SETUP_FINALIZED); |
| 2131 | } |
| 2132 | |
| 2133 | public void testSetUserProvisioningState_deviceOwnerFromSetupWizardAlternative() |
| 2134 | throws Exception { |
| 2135 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 2136 | setupDeviceOwner(); |
| 2137 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2138 | |
| 2139 | exerciseUserProvisioningTransitions(UserHandle.USER_SYSTEM, |
| 2140 | DevicePolicyManager.STATE_USER_SETUP_INCOMPLETE, |
| 2141 | DevicePolicyManager.STATE_USER_SETUP_FINALIZED); |
| 2142 | } |
| 2143 | |
| 2144 | public void testSetUserProvisioningState_deviceOwnerWithoutSetupWizard() throws Exception { |
| 2145 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 2146 | setupDeviceOwner(); |
| 2147 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2148 | |
| 2149 | exerciseUserProvisioningTransitions(UserHandle.USER_SYSTEM, |
| 2150 | DevicePolicyManager.STATE_USER_SETUP_FINALIZED); |
| 2151 | } |
| 2152 | |
| 2153 | public void testSetUserProvisioningState_managedProfileFromSetupWizard_primaryUser() |
| 2154 | throws Exception { |
| 2155 | setupProfileOwner(); |
| 2156 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2157 | |
| 2158 | exerciseUserProvisioningTransitions(DpmMockContext.CALLER_USER_HANDLE, |
| 2159 | DevicePolicyManager.STATE_USER_PROFILE_COMPLETE, |
| 2160 | DevicePolicyManager.STATE_USER_UNMANAGED); |
| 2161 | } |
| 2162 | |
| 2163 | public void testSetUserProvisioningState_managedProfileFromSetupWizard_managedProfile() |
| 2164 | throws Exception { |
| 2165 | setupProfileOwner(); |
| 2166 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2167 | |
| 2168 | exerciseUserProvisioningTransitions(DpmMockContext.CALLER_USER_HANDLE, |
| 2169 | DevicePolicyManager.STATE_USER_SETUP_COMPLETE, |
| 2170 | DevicePolicyManager.STATE_USER_SETUP_FINALIZED); |
| 2171 | } |
| 2172 | |
| 2173 | public void testSetUserProvisioningState_managedProfileWithoutSetupWizard() throws Exception { |
| 2174 | setupProfileOwner(); |
| 2175 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2176 | |
| 2177 | exerciseUserProvisioningTransitions(DpmMockContext.CALLER_USER_HANDLE, |
| 2178 | DevicePolicyManager.STATE_USER_SETUP_FINALIZED); |
| 2179 | } |
| 2180 | |
| 2181 | public void testSetUserProvisioningState_illegalTransitionOutOfFinalized1() throws Exception { |
| 2182 | setupProfileOwner(); |
| 2183 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2184 | |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 2185 | assertExpectException(IllegalStateException.class, |
| 2186 | /* messageRegex= */ "Cannot move to user provisioning state", |
| 2187 | () -> exerciseUserProvisioningTransitions(DpmMockContext.CALLER_USER_HANDLE, |
| 2188 | DevicePolicyManager.STATE_USER_SETUP_FINALIZED, |
| 2189 | DevicePolicyManager.STATE_USER_UNMANAGED)); |
Alan Treadway | afad878 | 2016-01-19 15:15:08 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
| 2192 | public void testSetUserProvisioningState_illegalTransitionToAnotherInProgressState() |
| 2193 | throws Exception { |
| 2194 | setupProfileOwner(); |
| 2195 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2196 | |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 2197 | assertExpectException(IllegalStateException.class, |
| 2198 | /* messageRegex= */ "Cannot move to user provisioning state", |
| 2199 | () -> exerciseUserProvisioningTransitions(DpmMockContext.CALLER_USER_HANDLE, |
| 2200 | DevicePolicyManager.STATE_USER_SETUP_INCOMPLETE, |
| 2201 | DevicePolicyManager.STATE_USER_SETUP_COMPLETE)); |
Alan Treadway | afad878 | 2016-01-19 15:15:08 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | private void exerciseUserProvisioningTransitions(int userId, int... states) { |
| 2205 | assertEquals(DevicePolicyManager.STATE_USER_UNMANAGED, dpm.getUserProvisioningState()); |
| 2206 | for (int state : states) { |
| 2207 | dpm.setUserProvisioningState(state, userId); |
| 2208 | assertEquals(state, dpm.getUserProvisioningState()); |
| 2209 | } |
| 2210 | } |
| 2211 | |
| 2212 | private void setupProfileOwner() throws Exception { |
| 2213 | mContext.callerPermissions.addAll(OWNER_SETUP_PERMISSIONS); |
| 2214 | |
| 2215 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_UID); |
| 2216 | dpm.setActiveAdmin(admin1, false); |
| 2217 | assertTrue(dpm.setProfileOwner(admin1, null, DpmMockContext.CALLER_USER_HANDLE)); |
| 2218 | |
| 2219 | mContext.callerPermissions.removeAll(OWNER_SETUP_PERMISSIONS); |
| 2220 | } |
| 2221 | |
| 2222 | private void setupDeviceOwner() throws Exception { |
| 2223 | mContext.callerPermissions.addAll(OWNER_SETUP_PERMISSIONS); |
| 2224 | |
| 2225 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); |
| 2226 | dpm.setActiveAdmin(admin1, false); |
| 2227 | assertTrue(dpm.setDeviceOwner(admin1, null, UserHandle.USER_SYSTEM)); |
| 2228 | |
| 2229 | mContext.callerPermissions.removeAll(OWNER_SETUP_PERMISSIONS); |
| 2230 | } |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 2231 | |
| 2232 | public void testSetMaximumTimeToLock() { |
| 2233 | mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); |
| 2234 | |
| 2235 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 2236 | dpm.setActiveAdmin(admin2, /* replace =*/ false); |
| 2237 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2238 | reset(getServices().powerManagerInternal); |
| 2239 | reset(getServices().settings); |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 2240 | |
| 2241 | dpm.setMaximumTimeToLock(admin1, 0); |
| 2242 | verifyScreenTimeoutCall(null, false); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2243 | reset(getServices().powerManagerInternal); |
| 2244 | reset(getServices().settings); |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 2245 | |
| 2246 | dpm.setMaximumTimeToLock(admin1, 1); |
| 2247 | verifyScreenTimeoutCall(1, true); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2248 | reset(getServices().powerManagerInternal); |
| 2249 | reset(getServices().settings); |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 2250 | |
| 2251 | dpm.setMaximumTimeToLock(admin2, 10); |
| 2252 | verifyScreenTimeoutCall(null, false); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2253 | reset(getServices().powerManagerInternal); |
| 2254 | reset(getServices().settings); |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 2255 | |
| 2256 | dpm.setMaximumTimeToLock(admin1, 5); |
| 2257 | verifyScreenTimeoutCall(5, true); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2258 | reset(getServices().powerManagerInternal); |
| 2259 | reset(getServices().settings); |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 2260 | |
| 2261 | dpm.setMaximumTimeToLock(admin2, 4); |
| 2262 | verifyScreenTimeoutCall(4, true); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2263 | reset(getServices().powerManagerInternal); |
| 2264 | reset(getServices().settings); |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 2265 | |
| 2266 | dpm.setMaximumTimeToLock(admin1, 0); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2267 | reset(getServices().powerManagerInternal); |
| 2268 | reset(getServices().settings); |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 2269 | |
| 2270 | dpm.setMaximumTimeToLock(admin2, Integer.MAX_VALUE); |
| 2271 | verifyScreenTimeoutCall(Integer.MAX_VALUE, true); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2272 | reset(getServices().powerManagerInternal); |
| 2273 | reset(getServices().settings); |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 2274 | |
| 2275 | dpm.setMaximumTimeToLock(admin2, Integer.MAX_VALUE + 1); |
| 2276 | verifyScreenTimeoutCall(Integer.MAX_VALUE, true); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2277 | reset(getServices().powerManagerInternal); |
| 2278 | reset(getServices().settings); |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 2279 | |
| 2280 | dpm.setMaximumTimeToLock(admin2, 10); |
| 2281 | verifyScreenTimeoutCall(10, true); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2282 | reset(getServices().powerManagerInternal); |
| 2283 | reset(getServices().settings); |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 2284 | |
| 2285 | // There's no restriction; shold be set to MAX. |
| 2286 | dpm.setMaximumTimeToLock(admin2, 0); |
| 2287 | verifyScreenTimeoutCall(Integer.MAX_VALUE, false); |
| 2288 | } |
| 2289 | |
Michal Karpinski | 943aabd | 2016-10-06 11:09:25 +0100 | [diff] [blame] | 2290 | public void testSetRequiredStrongAuthTimeout_DeviceOwner() throws Exception { |
| 2291 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 2292 | setupDeviceOwner(); |
| 2293 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2294 | |
Michal Karpinski | d084ca5 | 2017-01-18 15:54:18 +0000 | [diff] [blame] | 2295 | final long MINIMUM_STRONG_AUTH_TIMEOUT_MS = TimeUnit.HOURS.toMillis(1); |
| 2296 | final long ONE_MINUTE = TimeUnit.MINUTES.toMillis(1); |
| 2297 | final long MIN_PLUS_ONE_MINUTE = MINIMUM_STRONG_AUTH_TIMEOUT_MS + ONE_MINUTE; |
| 2298 | final long MAX_MINUS_ONE_MINUTE = DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS |
| 2299 | - ONE_MINUTE; |
| 2300 | |
| 2301 | // verify that the minimum timeout cannot be modified on user builds (system property is |
| 2302 | // not being read) |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2303 | getServices().buildMock.isDebuggable = false; |
Michal Karpinski | d084ca5 | 2017-01-18 15:54:18 +0000 | [diff] [blame] | 2304 | |
| 2305 | dpm.setRequiredStrongAuthTimeout(admin1, MAX_MINUS_ONE_MINUTE); |
| 2306 | assertEquals(dpm.getRequiredStrongAuthTimeout(admin1), MAX_MINUS_ONE_MINUTE); |
| 2307 | assertEquals(dpm.getRequiredStrongAuthTimeout(null), MAX_MINUS_ONE_MINUTE); |
| 2308 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2309 | verify(getServices().systemProperties, never()).getLong(anyString(), anyLong()); |
Michal Karpinski | d084ca5 | 2017-01-18 15:54:18 +0000 | [diff] [blame] | 2310 | |
| 2311 | // restore to the debuggable build state |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2312 | getServices().buildMock.isDebuggable = true; |
Michal Karpinski | d084ca5 | 2017-01-18 15:54:18 +0000 | [diff] [blame] | 2313 | |
Michal Karpinski | d084ca5 | 2017-01-18 15:54:18 +0000 | [diff] [blame] | 2314 | // reset to default (0 means the admin is not participating, so default should be returned) |
| 2315 | dpm.setRequiredStrongAuthTimeout(admin1, 0); |
Michal Karpinski | 943aabd | 2016-10-06 11:09:25 +0100 | [diff] [blame] | 2316 | |
| 2317 | // aggregation should be the default if unset by any admin |
| 2318 | assertEquals(dpm.getRequiredStrongAuthTimeout(null), |
| 2319 | DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS); |
| 2320 | |
| 2321 | // admin not participating by default |
| 2322 | assertEquals(dpm.getRequiredStrongAuthTimeout(admin1), 0); |
| 2323 | |
| 2324 | //clamping from the top |
| 2325 | dpm.setRequiredStrongAuthTimeout(admin1, |
| 2326 | DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS + ONE_MINUTE); |
| 2327 | assertEquals(dpm.getRequiredStrongAuthTimeout(admin1), |
| 2328 | DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS); |
| 2329 | assertEquals(dpm.getRequiredStrongAuthTimeout(null), |
| 2330 | DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS); |
| 2331 | |
Michal Karpinski | d084ca5 | 2017-01-18 15:54:18 +0000 | [diff] [blame] | 2332 | // 0 means the admin is not participating, so default should be returned |
Michal Karpinski | 943aabd | 2016-10-06 11:09:25 +0100 | [diff] [blame] | 2333 | dpm.setRequiredStrongAuthTimeout(admin1, 0); |
| 2334 | assertEquals(dpm.getRequiredStrongAuthTimeout(admin1), 0); |
| 2335 | assertEquals(dpm.getRequiredStrongAuthTimeout(null), |
| 2336 | DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS); |
| 2337 | |
| 2338 | // clamping from the bottom |
| 2339 | dpm.setRequiredStrongAuthTimeout(admin1, MINIMUM_STRONG_AUTH_TIMEOUT_MS - ONE_MINUTE); |
| 2340 | assertEquals(dpm.getRequiredStrongAuthTimeout(admin1), MINIMUM_STRONG_AUTH_TIMEOUT_MS); |
| 2341 | assertEquals(dpm.getRequiredStrongAuthTimeout(null), MINIMUM_STRONG_AUTH_TIMEOUT_MS); |
| 2342 | |
Michal Karpinski | d084ca5 | 2017-01-18 15:54:18 +0000 | [diff] [blame] | 2343 | // values within range |
| 2344 | dpm.setRequiredStrongAuthTimeout(admin1, MIN_PLUS_ONE_MINUTE); |
| 2345 | assertEquals(dpm.getRequiredStrongAuthTimeout(admin1), MIN_PLUS_ONE_MINUTE); |
| 2346 | assertEquals(dpm.getRequiredStrongAuthTimeout(null), MIN_PLUS_ONE_MINUTE); |
| 2347 | |
| 2348 | dpm.setRequiredStrongAuthTimeout(admin1, MAX_MINUS_ONE_MINUTE); |
| 2349 | assertEquals(dpm.getRequiredStrongAuthTimeout(admin1), MAX_MINUS_ONE_MINUTE); |
| 2350 | assertEquals(dpm.getRequiredStrongAuthTimeout(null), MAX_MINUS_ONE_MINUTE); |
Michal Karpinski | 943aabd | 2016-10-06 11:09:25 +0100 | [diff] [blame] | 2351 | |
| 2352 | // reset to default |
| 2353 | dpm.setRequiredStrongAuthTimeout(admin1, 0); |
| 2354 | assertEquals(dpm.getRequiredStrongAuthTimeout(admin1), 0); |
| 2355 | assertEquals(dpm.getRequiredStrongAuthTimeout(null), |
| 2356 | DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS); |
| 2357 | |
| 2358 | // negative value |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 2359 | assertExpectException(IllegalArgumentException.class, /* messageRegex= */ null, |
| 2360 | () -> dpm.setRequiredStrongAuthTimeout(admin1, -ONE_MINUTE)); |
Michal Karpinski | 943aabd | 2016-10-06 11:09:25 +0100 | [diff] [blame] | 2361 | } |
| 2362 | |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 2363 | private void verifyScreenTimeoutCall(Integer expectedTimeout, |
| 2364 | boolean shouldStayOnWhilePluggedInBeCleared) { |
| 2365 | if (expectedTimeout == null) { |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2366 | verify(getServices().powerManagerInternal, times(0)) |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 2367 | .setMaximumScreenOffTimeoutFromDeviceAdmin(anyInt()); |
| 2368 | } else { |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2369 | verify(getServices().powerManagerInternal, times(1)) |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 2370 | .setMaximumScreenOffTimeoutFromDeviceAdmin(eq(expectedTimeout)); |
| 2371 | } |
| 2372 | // TODO Verify calls to settingsGlobalPutInt. Tried but somehow mockito threw |
| 2373 | // UnfinishedVerificationException. |
| 2374 | } |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 2375 | |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2376 | private void setup_DeviceAdminFeatureOff() throws Exception { |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2377 | when(getServices().packageManager.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)) |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2378 | .thenReturn(false); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2379 | when(getServices().ipackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)) |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2380 | .thenReturn(false); |
| 2381 | initializeDpms(); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2382 | when(getServices().userManagerForMock.isSplitSystemUser()).thenReturn(false); |
| 2383 | when(getServices().userManager.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, true)) |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2384 | .thenReturn(true); |
| 2385 | setUserSetupCompleteForUser(false, UserHandle.USER_SYSTEM); |
| 2386 | |
| 2387 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2388 | } |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2389 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2390 | public void testIsProvisioningAllowed_DeviceAdminFeatureOff() throws Exception { |
| 2391 | setup_DeviceAdminFeatureOff(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2392 | mContext.packageName = admin1.getPackageName(); |
| 2393 | setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2394 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, false); |
| 2395 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false); |
| 2396 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2397 | false); |
| 2398 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, false); |
| 2399 | } |
| 2400 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2401 | public void testCheckProvisioningPreCondition_DeviceAdminFeatureOff() throws Exception { |
| 2402 | setup_DeviceAdminFeatureOff(); |
| 2403 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2404 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2405 | DevicePolicyManager.CODE_DEVICE_ADMIN_NOT_SUPPORTED); |
| 2406 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2407 | DevicePolicyManager.CODE_DEVICE_ADMIN_NOT_SUPPORTED); |
| 2408 | assertCheckProvisioningPreCondition( |
| 2409 | DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2410 | DevicePolicyManager.CODE_DEVICE_ADMIN_NOT_SUPPORTED); |
| 2411 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2412 | DevicePolicyManager.CODE_DEVICE_ADMIN_NOT_SUPPORTED); |
| 2413 | } |
| 2414 | |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2415 | private void setup_ManagedProfileFeatureOff() throws Exception { |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2416 | when(getServices().ipackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)) |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2417 | .thenReturn(false); |
| 2418 | initializeDpms(); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2419 | when(getServices().userManagerForMock.isSplitSystemUser()).thenReturn(false); |
| 2420 | when(getServices().userManager.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, true)) |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2421 | .thenReturn(true); |
| 2422 | setUserSetupCompleteForUser(false, UserHandle.USER_SYSTEM); |
| 2423 | |
| 2424 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2425 | } |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2426 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2427 | public void testIsProvisioningAllowed_ManagedProfileFeatureOff() throws Exception { |
| 2428 | setup_ManagedProfileFeatureOff(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2429 | mContext.packageName = admin1.getPackageName(); |
| 2430 | setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2431 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, true); |
| 2432 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false); |
| 2433 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2434 | false); |
| 2435 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, false); |
| 2436 | |
| 2437 | // Test again when split user is on |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2438 | when(getServices().userManagerForMock.isSplitSystemUser()).thenReturn(true); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2439 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, true); |
| 2440 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false); |
| 2441 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2442 | true); |
| 2443 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, false); |
| 2444 | } |
| 2445 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2446 | public void testCheckProvisioningPreCondition_ManagedProfileFeatureOff() throws Exception { |
| 2447 | setup_ManagedProfileFeatureOff(); |
| 2448 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2449 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2450 | DevicePolicyManager.CODE_OK); |
| 2451 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2452 | DevicePolicyManager.CODE_MANAGED_USERS_NOT_SUPPORTED); |
| 2453 | assertCheckProvisioningPreCondition( |
| 2454 | DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2455 | DevicePolicyManager.CODE_NOT_SYSTEM_USER_SPLIT); |
| 2456 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2457 | DevicePolicyManager.CODE_MANAGED_USERS_NOT_SUPPORTED); |
| 2458 | |
| 2459 | // Test again when split user is on |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2460 | when(getServices().userManagerForMock.isSplitSystemUser()).thenReturn(true); |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2461 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2462 | DevicePolicyManager.CODE_OK); |
| 2463 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2464 | DevicePolicyManager.CODE_MANAGED_USERS_NOT_SUPPORTED); |
| 2465 | assertCheckProvisioningPreCondition( |
| 2466 | DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2467 | DevicePolicyManager.CODE_OK); |
| 2468 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2469 | DevicePolicyManager.CODE_MANAGED_USERS_NOT_SUPPORTED); |
| 2470 | } |
| 2471 | |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2472 | private void setup_nonSplitUser_firstBoot_primaryUser() throws Exception { |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2473 | when(getServices().ipackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)) |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2474 | .thenReturn(true); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2475 | when(getServices().userManagerForMock.isSplitSystemUser()).thenReturn(false); |
| 2476 | when(getServices().userManager.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, true)) |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2477 | .thenReturn(true); |
| 2478 | setUserSetupCompleteForUser(false, UserHandle.USER_SYSTEM); |
| 2479 | |
| 2480 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2481 | } |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2482 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2483 | public void testIsProvisioningAllowed_nonSplitUser_firstBoot_primaryUser() throws Exception { |
| 2484 | setup_nonSplitUser_firstBoot_primaryUser(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2485 | mContext.packageName = admin1.getPackageName(); |
| 2486 | setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2487 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, true); |
| 2488 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, true); |
| 2489 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2490 | false /* because of non-split user */); |
| 2491 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2492 | false /* because of non-split user */); |
| 2493 | } |
| 2494 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2495 | public void testCheckProvisioningPreCondition_nonSplitUser_firstBoot_primaryUser() |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2496 | throws Exception { |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2497 | setup_nonSplitUser_firstBoot_primaryUser(); |
| 2498 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2499 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2500 | DevicePolicyManager.CODE_OK); |
| 2501 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2502 | DevicePolicyManager.CODE_OK); |
| 2503 | assertCheckProvisioningPreCondition( |
| 2504 | DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2505 | DevicePolicyManager.CODE_NOT_SYSTEM_USER_SPLIT); |
| 2506 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2507 | DevicePolicyManager.CODE_NOT_SYSTEM_USER_SPLIT); |
| 2508 | } |
| 2509 | |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2510 | private void setup_nonSplitUser_afterDeviceSetup_primaryUser() throws Exception { |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2511 | when(getServices().ipackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)) |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2512 | .thenReturn(true); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2513 | when(getServices().userManagerForMock.isSplitSystemUser()).thenReturn(false); |
| 2514 | when(getServices().userManager.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, true)) |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2515 | .thenReturn(true); |
| 2516 | setUserSetupCompleteForUser(true, UserHandle.USER_SYSTEM); |
| 2517 | |
| 2518 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2519 | } |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2520 | |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2521 | private void setup_nonSplitUser_withDo_primaryUser() throws Exception { |
| 2522 | setDeviceOwner(); |
| 2523 | setup_nonSplitUser_afterDeviceSetup_primaryUser(); |
| 2524 | setUpPackageManagerForFakeAdmin(adminAnotherPackage, DpmMockContext.ANOTHER_UID, admin2); |
| 2525 | } |
| 2526 | |
| 2527 | private void setup_nonSplitUser_withDo_primaryUser_ManagedProfile() throws Exception { |
| 2528 | setup_nonSplitUser_withDo_primaryUser(); |
| 2529 | final int MANAGED_PROFILE_USER_ID = 18; |
| 2530 | final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 1308); |
| 2531 | addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2532 | when(getServices().userManager.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2533 | false /* we can't remove a managed profile */)).thenReturn(false); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2534 | when(getServices().userManager.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2535 | true)).thenReturn(true); |
| 2536 | } |
| 2537 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2538 | public void testIsProvisioningAllowed_nonSplitUser_afterDeviceSetup_primaryUser() |
| 2539 | throws Exception { |
| 2540 | setup_nonSplitUser_afterDeviceSetup_primaryUser(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2541 | mContext.packageName = admin1.getPackageName(); |
| 2542 | setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2543 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2544 | false/* because of completed device setup */); |
| 2545 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, true); |
| 2546 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2547 | false/* because of non-split user */); |
| 2548 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2549 | false/* because of non-split user */); |
| 2550 | } |
| 2551 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2552 | public void testCheckProvisioningPreCondition_nonSplitUser_afterDeviceSetup_primaryUser() |
| 2553 | throws Exception { |
| 2554 | setup_nonSplitUser_afterDeviceSetup_primaryUser(); |
| 2555 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2556 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2557 | DevicePolicyManager.CODE_USER_SETUP_COMPLETED); |
| 2558 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2559 | DevicePolicyManager.CODE_OK); |
| 2560 | assertCheckProvisioningPreCondition( |
| 2561 | DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2562 | DevicePolicyManager.CODE_NOT_SYSTEM_USER_SPLIT); |
| 2563 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2564 | DevicePolicyManager.CODE_NOT_SYSTEM_USER_SPLIT); |
| 2565 | } |
| 2566 | |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2567 | public void testProvisioning_nonSplitUser_withDo_primaryUser() throws Exception { |
| 2568 | setup_nonSplitUser_withDo_primaryUser(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2569 | mContext.packageName = admin1.getPackageName(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2570 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2571 | |
| 2572 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2573 | DevicePolicyManager.CODE_HAS_DEVICE_OWNER); |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2574 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, false); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2575 | |
| 2576 | // COMP mode is allowed. |
| 2577 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2578 | DevicePolicyManager.CODE_OK); |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2579 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, true); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2580 | |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2581 | // And other DPCs can also provision a managed profile (DO + BYOD case). |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2582 | assertCheckProvisioningPreCondition( |
| 2583 | DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2584 | DpmMockContext.ANOTHER_PACKAGE_NAME, |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2585 | DevicePolicyManager.CODE_OK); |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2586 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, true, |
| 2587 | DpmMockContext.ANOTHER_PACKAGE_NAME, DpmMockContext.ANOTHER_UID); |
| 2588 | } |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2589 | |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2590 | public void testProvisioning_nonSplitUser_withDo_primaryUser_restrictedByDo() throws Exception { |
| 2591 | setup_nonSplitUser_withDo_primaryUser(); |
| 2592 | mContext.packageName = admin1.getPackageName(); |
| 2593 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2594 | // The DO should be allowed to initiate provisioning if it set the restriction itself, but |
| 2595 | // other packages should be forbidden. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2596 | when(getServices().userManager.hasUserRestriction( |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2597 | eq(UserManager.DISALLOW_ADD_MANAGED_PROFILE), |
| 2598 | eq(UserHandle.getUserHandleForUid(mContext.binder.callingUid)))) |
| 2599 | .thenReturn(true); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2600 | when(getServices().userManager.getUserRestrictionSource( |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2601 | eq(UserManager.DISALLOW_ADD_MANAGED_PROFILE), |
| 2602 | eq(UserHandle.getUserHandleForUid(mContext.binder.callingUid)))) |
| 2603 | .thenReturn(UserManager.RESTRICTION_SOURCE_DEVICE_OWNER); |
| 2604 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2605 | DevicePolicyManager.CODE_OK); |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2606 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, true); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2607 | assertCheckProvisioningPreCondition( |
| 2608 | DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2609 | DpmMockContext.ANOTHER_PACKAGE_NAME, |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2610 | DevicePolicyManager.CODE_ADD_MANAGED_PROFILE_DISALLOWED); |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2611 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false, |
| 2612 | DpmMockContext.ANOTHER_PACKAGE_NAME, DpmMockContext.ANOTHER_UID); |
| 2613 | } |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2614 | |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2615 | public void testProvisioning_nonSplitUser_withDo_primaryUser_restrictedBySystem() |
| 2616 | throws Exception { |
| 2617 | setup_nonSplitUser_withDo_primaryUser(); |
| 2618 | mContext.packageName = admin1.getPackageName(); |
| 2619 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2620 | // The DO should not be allowed to initiate provisioning if the restriction is set by |
| 2621 | // another entity. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2622 | when(getServices().userManager.hasUserRestriction( |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2623 | eq(UserManager.DISALLOW_ADD_MANAGED_PROFILE), |
| 2624 | eq(UserHandle.getUserHandleForUid(mContext.binder.callingUid)))) |
| 2625 | .thenReturn(true); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2626 | when(getServices().userManager.getUserRestrictionSource( |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2627 | eq(UserManager.DISALLOW_ADD_MANAGED_PROFILE), |
| 2628 | eq(UserHandle.getUserHandleForUid(mContext.binder.callingUid)))) |
| 2629 | .thenReturn(UserManager.RESTRICTION_SOURCE_SYSTEM); |
| 2630 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2631 | DevicePolicyManager.CODE_ADD_MANAGED_PROFILE_DISALLOWED); |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2632 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false); |
| 2633 | |
| 2634 | assertCheckProvisioningPreCondition( |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2635 | DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2636 | DpmMockContext.ANOTHER_PACKAGE_NAME, |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2637 | DevicePolicyManager.CODE_ADD_MANAGED_PROFILE_DISALLOWED); |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2638 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false, |
| 2639 | DpmMockContext.ANOTHER_PACKAGE_NAME, DpmMockContext.ANOTHER_UID); |
| 2640 | } |
| 2641 | |
| 2642 | public void testCheckProvisioningPreCondition_nonSplitUser_comp() throws Exception { |
| 2643 | setup_nonSplitUser_withDo_primaryUser_ManagedProfile(); |
| 2644 | mContext.packageName = admin1.getPackageName(); |
| 2645 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2646 | |
| 2647 | // We can delete the managed profile to create a new one, so provisioning is allowed. |
| 2648 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2649 | DevicePolicyManager.CODE_OK); |
| 2650 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, true); |
| 2651 | assertCheckProvisioningPreCondition( |
| 2652 | DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2653 | DpmMockContext.ANOTHER_PACKAGE_NAME, |
| 2654 | DevicePolicyManager.CODE_OK); |
| 2655 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, true, |
| 2656 | DpmMockContext.ANOTHER_PACKAGE_NAME, DpmMockContext.ANOTHER_UID); |
| 2657 | } |
| 2658 | |
| 2659 | public void testCheckProvisioningPreCondition_nonSplitUser_comp_cannot_remove_profile() |
| 2660 | throws Exception { |
| 2661 | setup_nonSplitUser_withDo_primaryUser_ManagedProfile(); |
| 2662 | mContext.packageName = admin1.getPackageName(); |
| 2663 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2664 | when(getServices().userManager.hasUserRestriction( |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2665 | eq(UserManager.DISALLOW_REMOVE_MANAGED_PROFILE), |
| 2666 | eq(UserHandle.SYSTEM))) |
| 2667 | .thenReturn(true); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2668 | when(getServices().userManager.getUserRestrictionSource( |
Nicolas Prevot | d37c4a9 | 2017-01-23 11:56:00 +0000 | [diff] [blame] | 2669 | eq(UserManager.DISALLOW_REMOVE_MANAGED_PROFILE), |
| 2670 | eq(UserHandle.SYSTEM))) |
| 2671 | .thenReturn(UserManager.RESTRICTION_SOURCE_DEVICE_OWNER); |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2672 | |
| 2673 | // We can't remove the profile to create a new one. |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2674 | assertCheckProvisioningPreCondition( |
| 2675 | DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2676 | DpmMockContext.ANOTHER_PACKAGE_NAME, |
| 2677 | DevicePolicyManager.CODE_CANNOT_ADD_MANAGED_PROFILE); |
| 2678 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false, |
| 2679 | DpmMockContext.ANOTHER_PACKAGE_NAME, DpmMockContext.ANOTHER_UID); |
Nicolas Prevot | d37c4a9 | 2017-01-23 11:56:00 +0000 | [diff] [blame] | 2680 | |
| 2681 | // But the device owner can still do it because it has set the restriction itself. |
| 2682 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2683 | DevicePolicyManager.CODE_OK); |
| 2684 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, true); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2685 | } |
| 2686 | |
| 2687 | private void setup_splitUser_firstBoot_systemUser() throws Exception { |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2688 | when(getServices().ipackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)) |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2689 | .thenReturn(true); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2690 | when(getServices().userManagerForMock.isSplitSystemUser()).thenReturn(true); |
| 2691 | when(getServices().userManager.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, true)) |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2692 | .thenReturn(false); |
| 2693 | setUserSetupCompleteForUser(false, UserHandle.USER_SYSTEM); |
| 2694 | |
| 2695 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2696 | } |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2697 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2698 | public void testIsProvisioningAllowed_splitUser_firstBoot_systemUser() throws Exception { |
| 2699 | setup_splitUser_firstBoot_systemUser(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2700 | mContext.packageName = admin1.getPackageName(); |
| 2701 | setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2702 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, true); |
| 2703 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2704 | false /* because canAddMoreManagedProfiles returns false */); |
| 2705 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2706 | true); |
| 2707 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2708 | false/* because calling uid is system user */); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2709 | } |
| 2710 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2711 | public void testCheckProvisioningPreCondition_splitUser_firstBoot_systemUser() |
| 2712 | throws Exception { |
| 2713 | setup_splitUser_firstBoot_systemUser(); |
| 2714 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2715 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2716 | DevicePolicyManager.CODE_OK); |
| 2717 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
Esteban Talavera | 6c9116a | 2016-11-24 16:12:44 +0000 | [diff] [blame] | 2718 | DevicePolicyManager.CODE_SPLIT_SYSTEM_USER_DEVICE_SYSTEM_USER); |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2719 | assertCheckProvisioningPreCondition( |
| 2720 | DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2721 | DevicePolicyManager.CODE_OK); |
| 2722 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2723 | DevicePolicyManager.CODE_SYSTEM_USER); |
| 2724 | } |
| 2725 | |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2726 | private void setup_splitUser_afterDeviceSetup_systemUser() throws Exception { |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2727 | when(getServices().ipackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)) |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2728 | .thenReturn(true); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2729 | when(getServices().userManagerForMock.isSplitSystemUser()).thenReturn(true); |
| 2730 | when(getServices().userManager.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, true)) |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2731 | .thenReturn(false); |
| 2732 | setUserSetupCompleteForUser(true, UserHandle.USER_SYSTEM); |
| 2733 | |
| 2734 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2735 | } |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2736 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2737 | public void testIsProvisioningAllowed_splitUser_afterDeviceSetup_systemUser() throws Exception { |
| 2738 | setup_splitUser_afterDeviceSetup_systemUser(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2739 | mContext.packageName = admin1.getPackageName(); |
| 2740 | setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2741 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2742 | true/* it's undefined behavior. Can be changed into false in the future */); |
| 2743 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2744 | false /* because canAddMoreManagedProfiles returns false */); |
| 2745 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2746 | true/* it's undefined behavior. Can be changed into false in the future */); |
| 2747 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2748 | false/* because calling uid is system user */); |
| 2749 | } |
| 2750 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2751 | public void testCheckProvisioningPreCondition_splitUser_afterDeviceSetup_systemUser() |
| 2752 | throws Exception { |
| 2753 | setup_splitUser_afterDeviceSetup_systemUser(); |
| 2754 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2755 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2756 | DevicePolicyManager.CODE_OK); |
| 2757 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
Esteban Talavera | 6c9116a | 2016-11-24 16:12:44 +0000 | [diff] [blame] | 2758 | DevicePolicyManager.CODE_SPLIT_SYSTEM_USER_DEVICE_SYSTEM_USER); |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2759 | assertCheckProvisioningPreCondition( |
| 2760 | DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2761 | DevicePolicyManager.CODE_OK); |
| 2762 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2763 | DevicePolicyManager.CODE_SYSTEM_USER); |
| 2764 | } |
| 2765 | |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2766 | private void setup_splitUser_firstBoot_primaryUser() throws Exception { |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2767 | when(getServices().ipackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)) |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2768 | .thenReturn(true); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2769 | when(getServices().userManagerForMock.isSplitSystemUser()).thenReturn(true); |
| 2770 | when(getServices().userManager.canAddMoreManagedProfiles(DpmMockContext.CALLER_USER_HANDLE, |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2771 | true)).thenReturn(true); |
| 2772 | setUserSetupCompleteForUser(false, DpmMockContext.CALLER_USER_HANDLE); |
| 2773 | |
| 2774 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2775 | } |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2776 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2777 | public void testIsProvisioningAllowed_splitUser_firstBoot_primaryUser() throws Exception { |
| 2778 | setup_splitUser_firstBoot_primaryUser(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2779 | mContext.packageName = admin1.getPackageName(); |
| 2780 | setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2781 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, true); |
| 2782 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, true); |
| 2783 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2784 | true); |
| 2785 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, true); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2788 | public void testCheckProvisioningPreCondition_splitUser_firstBoot_primaryUser() |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2789 | throws Exception { |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2790 | setup_splitUser_firstBoot_primaryUser(); |
| 2791 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2792 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2793 | DevicePolicyManager.CODE_OK); |
| 2794 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2795 | DevicePolicyManager.CODE_OK); |
| 2796 | assertCheckProvisioningPreCondition( |
| 2797 | DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2798 | DevicePolicyManager.CODE_OK); |
| 2799 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2800 | DevicePolicyManager.CODE_OK); |
| 2801 | } |
| 2802 | |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2803 | private void setup_splitUser_afterDeviceSetup_primaryUser() throws Exception { |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2804 | when(getServices().ipackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)) |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2805 | .thenReturn(true); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2806 | when(getServices().userManagerForMock.isSplitSystemUser()).thenReturn(true); |
| 2807 | when(getServices().userManager.canAddMoreManagedProfiles(DpmMockContext.CALLER_USER_HANDLE, |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2808 | true)).thenReturn(true); |
| 2809 | setUserSetupCompleteForUser(true, DpmMockContext.CALLER_USER_HANDLE); |
| 2810 | |
| 2811 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2812 | } |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2813 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2814 | public void testIsProvisioningAllowed_splitUser_afterDeviceSetup_primaryUser() |
| 2815 | throws Exception { |
| 2816 | setup_splitUser_afterDeviceSetup_primaryUser(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2817 | mContext.packageName = admin1.getPackageName(); |
| 2818 | setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2819 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2820 | true/* it's undefined behavior. Can be changed into false in the future */); |
| 2821 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, true); |
| 2822 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2823 | true/* it's undefined behavior. Can be changed into false in the future */); |
| 2824 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2825 | false/* because user setup completed */); |
| 2826 | } |
| 2827 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2828 | public void testCheckProvisioningPreCondition_splitUser_afterDeviceSetup_primaryUser() |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2829 | throws Exception { |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2830 | setup_splitUser_afterDeviceSetup_primaryUser(); |
| 2831 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2832 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2833 | DevicePolicyManager.CODE_OK); |
| 2834 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2835 | DevicePolicyManager.CODE_OK); |
| 2836 | assertCheckProvisioningPreCondition( |
| 2837 | DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2838 | DevicePolicyManager.CODE_OK); |
| 2839 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2840 | DevicePolicyManager.CODE_USER_SETUP_COMPLETED); |
| 2841 | } |
| 2842 | |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2843 | private void setup_provisionManagedProfileWithDeviceOwner_systemUser() throws Exception { |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2844 | setDeviceOwner(); |
| 2845 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2846 | when(getServices().ipackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)) |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2847 | .thenReturn(true); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2848 | when(getServices().userManagerForMock.isSplitSystemUser()).thenReturn(true); |
| 2849 | when(getServices().userManager.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, true)) |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2850 | .thenReturn(false); |
| 2851 | setUserSetupCompleteForUser(true, UserHandle.USER_SYSTEM); |
| 2852 | |
| 2853 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2854 | } |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2855 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2856 | public void testIsProvisioningAllowed_provisionManagedProfileWithDeviceOwner_systemUser() |
| 2857 | throws Exception { |
| 2858 | setup_provisionManagedProfileWithDeviceOwner_systemUser(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2859 | mContext.packageName = admin1.getPackageName(); |
| 2860 | setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2861 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2862 | false /* can't provision managed profile on system user */); |
| 2863 | } |
| 2864 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2865 | public void testCheckProvisioningPreCondition_provisionManagedProfileWithDeviceOwner_systemUser() |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2866 | throws Exception { |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2867 | setup_provisionManagedProfileWithDeviceOwner_systemUser(); |
| 2868 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2869 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2870 | DevicePolicyManager.CODE_SPLIT_SYSTEM_USER_DEVICE_SYSTEM_USER); |
| 2871 | } |
| 2872 | |
| 2873 | private void setup_provisionManagedProfileWithDeviceOwner_primaryUser() throws Exception { |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2874 | setDeviceOwner(); |
| 2875 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2876 | when(getServices().ipackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)) |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2877 | .thenReturn(true); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2878 | when(getServices().userManagerForMock.isSplitSystemUser()).thenReturn(true); |
| 2879 | when(getServices().userManager.canAddMoreManagedProfiles(DpmMockContext.CALLER_USER_HANDLE, |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2880 | true)).thenReturn(true); |
| 2881 | setUserSetupCompleteForUser(false, DpmMockContext.CALLER_USER_HANDLE); |
| 2882 | |
| 2883 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2884 | } |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2885 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2886 | public void testIsProvisioningAllowed_provisionManagedProfileWithDeviceOwner_primaryUser() |
| 2887 | throws Exception { |
| 2888 | setup_provisionManagedProfileWithDeviceOwner_primaryUser(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2889 | setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); |
| 2890 | mContext.packageName = admin1.getPackageName(); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2891 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, true); |
| 2892 | } |
| 2893 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2894 | public void testCheckProvisioningPreCondition_provisionManagedProfileWithDeviceOwner_primaryUser() |
Nicolas Prevot | 56400a4 | 2016-11-10 12:57:54 +0000 | [diff] [blame] | 2895 | throws Exception { |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2896 | setup_provisionManagedProfileWithDeviceOwner_primaryUser(); |
| 2897 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2898 | |
| 2899 | // COMP mode is allowed. |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2900 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2901 | DevicePolicyManager.CODE_OK); |
| 2902 | } |
| 2903 | |
| 2904 | private void setup_provisionManagedProfileCantRemoveUser_primaryUser() throws Exception { |
Nicolas Prevot | 56400a4 | 2016-11-10 12:57:54 +0000 | [diff] [blame] | 2905 | setDeviceOwner(); |
| 2906 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2907 | when(getServices().ipackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)) |
Nicolas Prevot | 56400a4 | 2016-11-10 12:57:54 +0000 | [diff] [blame] | 2908 | .thenReturn(true); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2909 | when(getServices().userManagerForMock.isSplitSystemUser()).thenReturn(true); |
| 2910 | when(getServices().userManager.hasUserRestriction( |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2911 | eq(UserManager.DISALLOW_REMOVE_MANAGED_PROFILE), |
| 2912 | eq(UserHandle.of(DpmMockContext.CALLER_USER_HANDLE)))) |
Nicolas Prevot | 56400a4 | 2016-11-10 12:57:54 +0000 | [diff] [blame] | 2913 | .thenReturn(true); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2914 | when(getServices().userManager.canAddMoreManagedProfiles(DpmMockContext.CALLER_USER_HANDLE, |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2915 | false /* we can't remove a managed profile */)).thenReturn(false); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2916 | when(getServices().userManager.canAddMoreManagedProfiles(DpmMockContext.CALLER_USER_HANDLE, |
Nicolas Prevot | 56400a4 | 2016-11-10 12:57:54 +0000 | [diff] [blame] | 2917 | true)).thenReturn(true); |
| 2918 | setUserSetupCompleteForUser(false, DpmMockContext.CALLER_USER_HANDLE); |
| 2919 | |
| 2920 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2921 | } |
Nicolas Prevot | 56400a4 | 2016-11-10 12:57:54 +0000 | [diff] [blame] | 2922 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2923 | public void testIsProvisioningAllowed_provisionManagedProfileCantRemoveUser_primaryUser() |
| 2924 | throws Exception { |
| 2925 | setup_provisionManagedProfileCantRemoveUser_primaryUser(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2926 | mContext.packageName = admin1.getPackageName(); |
| 2927 | setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); |
Nicolas Prevot | 56400a4 | 2016-11-10 12:57:54 +0000 | [diff] [blame] | 2928 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false); |
| 2929 | } |
| 2930 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2931 | public void testCheckProvisioningPreCondition_provisionManagedProfileCantRemoveUser_primaryUser() |
| 2932 | throws Exception { |
| 2933 | setup_provisionManagedProfileCantRemoveUser_primaryUser(); |
| 2934 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2935 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2936 | DevicePolicyManager.CODE_CANNOT_ADD_MANAGED_PROFILE); |
| 2937 | } |
| 2938 | |
| 2939 | public void testCheckProvisioningPreCondition_permission() { |
| 2940 | // GIVEN the permission MANAGE_PROFILE_AND_DEVICE_OWNERS is not granted |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 2941 | assertExpectException(SecurityException.class, /* messageRegex =*/ null, |
| 2942 | () -> dpm.checkProvisioningPreCondition( |
| 2943 | DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, "some.package")); |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2944 | } |
| 2945 | |
Victor Chang | 3577ed2 | 2016-08-25 18:49:26 +0100 | [diff] [blame] | 2946 | public void testForceUpdateUserSetupComplete_permission() { |
| 2947 | // GIVEN the permission MANAGE_PROFILE_AND_DEVICE_OWNERS is not granted |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 2948 | assertExpectException(SecurityException.class, /* messageRegex =*/ null, |
| 2949 | () -> dpm.forceUpdateUserSetupComplete()); |
Victor Chang | 3577ed2 | 2016-08-25 18:49:26 +0100 | [diff] [blame] | 2950 | } |
| 2951 | |
| 2952 | public void testForceUpdateUserSetupComplete_systemUser() { |
| 2953 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2954 | // GIVEN calling from user 20 |
| 2955 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 2956 | assertExpectException(SecurityException.class, /* messageRegex =*/ null, |
| 2957 | () -> dpm.forceUpdateUserSetupComplete()); |
Victor Chang | 3577ed2 | 2016-08-25 18:49:26 +0100 | [diff] [blame] | 2958 | } |
| 2959 | |
| 2960 | public void testForceUpdateUserSetupComplete_userbuild() { |
| 2961 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2962 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 2963 | |
| 2964 | final int userId = UserHandle.USER_SYSTEM; |
| 2965 | // GIVEN userComplete is false in SettingsProvider |
| 2966 | setUserSetupCompleteForUser(false, userId); |
| 2967 | |
| 2968 | // GIVEN userComplete is true in DPM |
| 2969 | DevicePolicyManagerService.DevicePolicyData userData = |
| 2970 | new DevicePolicyManagerService.DevicePolicyData(userId); |
| 2971 | userData.mUserSetupComplete = true; |
| 2972 | dpms.mUserData.put(UserHandle.USER_SYSTEM, userData); |
| 2973 | |
| 2974 | // GIVEN it's user build |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 2975 | getServices().buildMock.isDebuggable = false; |
Victor Chang | 3577ed2 | 2016-08-25 18:49:26 +0100 | [diff] [blame] | 2976 | |
| 2977 | assertTrue(dpms.hasUserSetupCompleted()); |
| 2978 | |
| 2979 | dpm.forceUpdateUserSetupComplete(); |
| 2980 | |
| 2981 | // THEN the state in dpms is not changed |
| 2982 | assertTrue(dpms.hasUserSetupCompleted()); |
| 2983 | } |
| 2984 | |
| 2985 | public void testForceUpdateUserSetupComplete_userDebugbuild() { |
| 2986 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2987 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 2988 | |
| 2989 | final int userId = UserHandle.USER_SYSTEM; |
| 2990 | // GIVEN userComplete is false in SettingsProvider |
| 2991 | setUserSetupCompleteForUser(false, userId); |
| 2992 | |
| 2993 | // GIVEN userComplete is true in DPM |
| 2994 | DevicePolicyManagerService.DevicePolicyData userData = |
| 2995 | new DevicePolicyManagerService.DevicePolicyData(userId); |
| 2996 | userData.mUserSetupComplete = true; |
| 2997 | dpms.mUserData.put(UserHandle.USER_SYSTEM, userData); |
| 2998 | |
| 2999 | // GIVEN it's userdebug build |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3000 | getServices().buildMock.isDebuggable = true; |
Victor Chang | 3577ed2 | 2016-08-25 18:49:26 +0100 | [diff] [blame] | 3001 | |
| 3002 | assertTrue(dpms.hasUserSetupCompleted()); |
| 3003 | |
| 3004 | dpm.forceUpdateUserSetupComplete(); |
| 3005 | |
| 3006 | // THEN the state in dpms is not changed |
| 3007 | assertFalse(dpms.hasUserSetupCompleted()); |
| 3008 | } |
| 3009 | |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3010 | private void clearDeviceOwner() throws Exception { |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3011 | doReturn(DpmMockContext.CALLER_SYSTEM_USER_UID).when(getServices().packageManager) |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3012 | .getPackageUidAsUser(eq(admin1.getPackageName()), anyInt()); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3013 | |
| 3014 | mAdmin1Context.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3015 | runAsCaller(mAdmin1Context, dpms, dpm -> { |
| 3016 | dpm.clearDeviceOwnerApp(admin1.getPackageName()); |
| 3017 | }); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3018 | } |
| 3019 | |
| 3020 | public void testGetLastSecurityLogRetrievalTime() throws Exception { |
| 3021 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3022 | setupDeviceOwner(); |
Esteban Talavera | d36dd15 | 2016-12-15 08:51:45 +0000 | [diff] [blame] | 3023 | |
| 3024 | // setUp() adds a secondary user for CALLER_USER_HANDLE. Remove it as otherwise the |
| 3025 | // feature is disabled because there are non-affiliated secondary users. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3026 | getServices().removeUser(DpmMockContext.CALLER_USER_HANDLE); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3027 | when(mContext.resources.getBoolean(R.bool.config_supportPreRebootSecurityLogs)) |
| 3028 | .thenReturn(true); |
| 3029 | |
| 3030 | // No logs were retrieved so far. |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3031 | assertEquals(-1, dpm.getLastSecurityLogRetrievalTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3032 | |
| 3033 | // Enabling logging should not change the timestamp. |
| 3034 | dpm.setSecurityLoggingEnabled(admin1, true); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3035 | verify(getServices().settings) |
Esteban Talavera | d36dd15 | 2016-12-15 08:51:45 +0000 | [diff] [blame] | 3036 | .securityLogSetLoggingEnabledProperty(true); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3037 | when(getServices().settings.securityLogGetLoggingEnabledProperty()) |
Esteban Talavera | d36dd15 | 2016-12-15 08:51:45 +0000 | [diff] [blame] | 3038 | .thenReturn(true); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3039 | assertEquals(-1, dpm.getLastSecurityLogRetrievalTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3040 | |
| 3041 | // Retrieving the logs should update the timestamp. |
| 3042 | final long beforeRetrieval = System.currentTimeMillis(); |
| 3043 | dpm.retrieveSecurityLogs(admin1); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3044 | final long firstSecurityLogRetrievalTime = dpm.getLastSecurityLogRetrievalTime(); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3045 | final long afterRetrieval = System.currentTimeMillis(); |
| 3046 | assertTrue(firstSecurityLogRetrievalTime >= beforeRetrieval); |
| 3047 | assertTrue(firstSecurityLogRetrievalTime <= afterRetrieval); |
| 3048 | |
| 3049 | // Retrieving the pre-boot logs should update the timestamp. |
| 3050 | Thread.sleep(2); |
| 3051 | dpm.retrievePreRebootSecurityLogs(admin1); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3052 | final long secondSecurityLogRetrievalTime = dpm.getLastSecurityLogRetrievalTime(); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3053 | assertTrue(secondSecurityLogRetrievalTime > firstSecurityLogRetrievalTime); |
| 3054 | |
| 3055 | // Checking the timestamp again should not change it. |
| 3056 | Thread.sleep(2); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3057 | assertEquals(secondSecurityLogRetrievalTime, dpm.getLastSecurityLogRetrievalTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3058 | |
| 3059 | // Retrieving the logs again should update the timestamp. |
| 3060 | dpm.retrieveSecurityLogs(admin1); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3061 | final long thirdSecurityLogRetrievalTime = dpm.getLastSecurityLogRetrievalTime(); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3062 | assertTrue(thirdSecurityLogRetrievalTime > secondSecurityLogRetrievalTime); |
| 3063 | |
| 3064 | // Disabling logging should not change the timestamp. |
| 3065 | Thread.sleep(2); |
| 3066 | dpm.setSecurityLoggingEnabled(admin1, false); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3067 | assertEquals(thirdSecurityLogRetrievalTime, dpm.getLastSecurityLogRetrievalTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3068 | |
| 3069 | // Restarting the DPMS should not lose the timestamp. |
| 3070 | initializeDpms(); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3071 | assertEquals(thirdSecurityLogRetrievalTime, dpm.getLastSecurityLogRetrievalTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3072 | |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3073 | // Any uid holding MANAGE_USERS permission can retrieve the timestamp. |
| 3074 | mContext.binder.callingUid = 1234567; |
| 3075 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
| 3076 | assertEquals(thirdSecurityLogRetrievalTime, dpm.getLastSecurityLogRetrievalTime()); |
| 3077 | mContext.callerPermissions.remove(permission.MANAGE_USERS); |
| 3078 | |
| 3079 | // System can retrieve the timestamp. |
| 3080 | mContext.binder.clearCallingIdentity(); |
| 3081 | assertEquals(thirdSecurityLogRetrievalTime, dpm.getLastSecurityLogRetrievalTime()); |
| 3082 | |
| 3083 | // Removing the device owner should clear the timestamp. |
| 3084 | clearDeviceOwner(); |
| 3085 | assertEquals(-1, dpm.getLastSecurityLogRetrievalTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3086 | } |
| 3087 | |
| 3088 | public void testGetLastBugReportRequestTime() throws Exception { |
| 3089 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3090 | setupDeviceOwner(); |
Esteban Talavera | d36dd15 | 2016-12-15 08:51:45 +0000 | [diff] [blame] | 3091 | |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3092 | mContext.packageName = admin1.getPackageName(); |
| 3093 | mContext.applicationInfo = new ApplicationInfo(); |
| 3094 | when(mContext.resources.getColor(eq(R.color.notification_action_list), anyObject())) |
| 3095 | .thenReturn(Color.WHITE); |
| 3096 | when(mContext.resources.getColor(eq(R.color.notification_material_background_color), |
| 3097 | anyObject())).thenReturn(Color.WHITE); |
| 3098 | |
Esteban Talavera | d36dd15 | 2016-12-15 08:51:45 +0000 | [diff] [blame] | 3099 | // setUp() adds a secondary user for CALLER_USER_HANDLE. Remove it as otherwise the |
| 3100 | // feature is disabled because there are non-affiliated secondary users. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3101 | getServices().removeUser(DpmMockContext.CALLER_USER_HANDLE); |
Esteban Talavera | d36dd15 | 2016-12-15 08:51:45 +0000 | [diff] [blame] | 3102 | |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3103 | // No bug reports were requested so far. |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3104 | assertEquals(-1, dpm.getLastBugReportRequestTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3105 | |
| 3106 | // Requesting a bug report should update the timestamp. |
| 3107 | final long beforeRequest = System.currentTimeMillis(); |
| 3108 | dpm.requestBugreport(admin1); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3109 | final long bugReportRequestTime = dpm.getLastBugReportRequestTime(); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3110 | final long afterRequest = System.currentTimeMillis(); |
| 3111 | assertTrue(bugReportRequestTime >= beforeRequest); |
| 3112 | assertTrue(bugReportRequestTime <= afterRequest); |
| 3113 | |
| 3114 | // Checking the timestamp again should not change it. |
| 3115 | Thread.sleep(2); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3116 | assertEquals(bugReportRequestTime, dpm.getLastBugReportRequestTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3117 | |
| 3118 | // Restarting the DPMS should not lose the timestamp. |
| 3119 | initializeDpms(); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3120 | assertEquals(bugReportRequestTime, dpm.getLastBugReportRequestTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3121 | |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3122 | // Any uid holding MANAGE_USERS permission can retrieve the timestamp. |
| 3123 | mContext.binder.callingUid = 1234567; |
| 3124 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
| 3125 | assertEquals(bugReportRequestTime, dpm.getLastBugReportRequestTime()); |
| 3126 | mContext.callerPermissions.remove(permission.MANAGE_USERS); |
| 3127 | |
| 3128 | // System can retrieve the timestamp. |
| 3129 | mContext.binder.clearCallingIdentity(); |
| 3130 | assertEquals(bugReportRequestTime, dpm.getLastBugReportRequestTime()); |
| 3131 | |
| 3132 | // Removing the device owner should clear the timestamp. |
| 3133 | clearDeviceOwner(); |
| 3134 | assertEquals(-1, dpm.getLastBugReportRequestTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3135 | } |
| 3136 | |
| 3137 | public void testGetLastNetworkLogRetrievalTime() throws Exception { |
| 3138 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3139 | setupDeviceOwner(); |
Esteban Talavera | d36dd15 | 2016-12-15 08:51:45 +0000 | [diff] [blame] | 3140 | mContext.packageName = admin1.getPackageName(); |
| 3141 | mContext.applicationInfo = new ApplicationInfo(); |
| 3142 | when(mContext.resources.getColor(eq(R.color.notification_action_list), anyObject())) |
| 3143 | .thenReturn(Color.WHITE); |
| 3144 | when(mContext.resources.getColor(eq(R.color.notification_material_background_color), |
| 3145 | anyObject())).thenReturn(Color.WHITE); |
| 3146 | |
| 3147 | // setUp() adds a secondary user for CALLER_USER_HANDLE. Remove it as otherwise the |
| 3148 | // feature is disabled because there are non-affiliated secondary users. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3149 | getServices().removeUser(DpmMockContext.CALLER_USER_HANDLE); |
| 3150 | when(getServices().iipConnectivityMetrics.registerNetdEventCallback(anyObject())) |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3151 | .thenReturn(true); |
| 3152 | |
| 3153 | // No logs were retrieved so far. |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3154 | assertEquals(-1, dpm.getLastNetworkLogRetrievalTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3155 | |
| 3156 | // Attempting to retrieve logs without enabling logging should not change the timestamp. |
| 3157 | dpm.retrieveNetworkLogs(admin1, 0 /* batchToken */); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3158 | assertEquals(-1, dpm.getLastNetworkLogRetrievalTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3159 | |
| 3160 | // Enabling logging should not change the timestamp. |
| 3161 | dpm.setNetworkLoggingEnabled(admin1, true); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3162 | assertEquals(-1, dpm.getLastNetworkLogRetrievalTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3163 | |
| 3164 | // Retrieving the logs should update the timestamp. |
| 3165 | final long beforeRetrieval = System.currentTimeMillis(); |
| 3166 | dpm.retrieveNetworkLogs(admin1, 0 /* batchToken */); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3167 | final long firstNetworkLogRetrievalTime = dpm.getLastNetworkLogRetrievalTime(); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3168 | final long afterRetrieval = System.currentTimeMillis(); |
| 3169 | assertTrue(firstNetworkLogRetrievalTime >= beforeRetrieval); |
| 3170 | assertTrue(firstNetworkLogRetrievalTime <= afterRetrieval); |
| 3171 | |
| 3172 | // Checking the timestamp again should not change it. |
| 3173 | Thread.sleep(2); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3174 | assertEquals(firstNetworkLogRetrievalTime, dpm.getLastNetworkLogRetrievalTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3175 | |
| 3176 | // Retrieving the logs again should update the timestamp. |
| 3177 | dpm.retrieveNetworkLogs(admin1, 0 /* batchToken */); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3178 | final long secondNetworkLogRetrievalTime = dpm.getLastNetworkLogRetrievalTime(); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3179 | assertTrue(secondNetworkLogRetrievalTime > firstNetworkLogRetrievalTime); |
| 3180 | |
| 3181 | // Disabling logging should not change the timestamp. |
| 3182 | Thread.sleep(2); |
| 3183 | dpm.setNetworkLoggingEnabled(admin1, false); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3184 | assertEquals(secondNetworkLogRetrievalTime, dpm.getLastNetworkLogRetrievalTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3185 | |
| 3186 | // Restarting the DPMS should not lose the timestamp. |
| 3187 | initializeDpms(); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3188 | assertEquals(secondNetworkLogRetrievalTime, dpm.getLastNetworkLogRetrievalTime()); |
| 3189 | |
| 3190 | // Any uid holding MANAGE_USERS permission can retrieve the timestamp. |
| 3191 | mContext.binder.callingUid = 1234567; |
| 3192 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
| 3193 | assertEquals(secondNetworkLogRetrievalTime, dpm.getLastNetworkLogRetrievalTime()); |
| 3194 | mContext.callerPermissions.remove(permission.MANAGE_USERS); |
| 3195 | |
| 3196 | // System can retrieve the timestamp. |
| 3197 | mContext.binder.clearCallingIdentity(); |
| 3198 | assertEquals(secondNetworkLogRetrievalTime, dpm.getLastNetworkLogRetrievalTime()); |
| 3199 | |
| 3200 | // Removing the device owner should clear the timestamp. |
| 3201 | clearDeviceOwner(); |
| 3202 | assertEquals(-1, dpm.getLastNetworkLogRetrievalTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3203 | } |
| 3204 | |
Tony Mak | 2f26b79 | 2016-11-28 17:54:51 +0000 | [diff] [blame] | 3205 | public void testGetBindDeviceAdminTargetUsers() throws Exception { |
| 3206 | // Setup device owner. |
| 3207 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3208 | setupDeviceOwner(); |
| 3209 | |
| 3210 | // Only device owner is setup, the result list should be empty. |
| 3211 | List<UserHandle> targetUsers = dpm.getBindDeviceAdminTargetUsers(admin1); |
| 3212 | MoreAsserts.assertEmpty(targetUsers); |
| 3213 | |
| 3214 | // Setup a managed profile managed by the same admin. |
| 3215 | final int MANAGED_PROFILE_USER_ID = 15; |
| 3216 | final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 20456); |
| 3217 | addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); |
| 3218 | |
| 3219 | // Add a secondary user, it should never talk with. |
| 3220 | final int ANOTHER_USER_ID = 36; |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3221 | getServices().addUser(ANOTHER_USER_ID, 0); |
Tony Mak | 2f26b79 | 2016-11-28 17:54:51 +0000 | [diff] [blame] | 3222 | |
Nicolas Prevot | d5b0360 | 2016-12-06 13:32:22 +0000 | [diff] [blame] | 3223 | // Since the managed profile is not affiliated, they should not be allowed to talk to each |
| 3224 | // other. |
| 3225 | targetUsers = dpm.getBindDeviceAdminTargetUsers(admin1); |
| 3226 | MoreAsserts.assertEmpty(targetUsers); |
| 3227 | |
| 3228 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 3229 | targetUsers = dpm.getBindDeviceAdminTargetUsers(admin1); |
| 3230 | MoreAsserts.assertEmpty(targetUsers); |
| 3231 | |
| 3232 | // Setting affiliation ids |
Tony Mak | 3165743 | 2017-04-25 09:29:55 +0100 | [diff] [blame] | 3233 | final Set<String> userAffiliationIds = Collections.singleton("some.affiliation-id"); |
Nicolas Prevot | d5b0360 | 2016-12-06 13:32:22 +0000 | [diff] [blame] | 3234 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3235 | dpm.setAffiliationIds(admin1, userAffiliationIds); |
| 3236 | |
| 3237 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 3238 | dpm.setAffiliationIds(admin1, userAffiliationIds); |
| 3239 | |
Tony Mak | 2f26b79 | 2016-11-28 17:54:51 +0000 | [diff] [blame] | 3240 | // Calling from device owner admin, the result list should just contain the managed |
| 3241 | // profile user id. |
Nicolas Prevot | d5b0360 | 2016-12-06 13:32:22 +0000 | [diff] [blame] | 3242 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
Tony Mak | 2f26b79 | 2016-11-28 17:54:51 +0000 | [diff] [blame] | 3243 | targetUsers = dpm.getBindDeviceAdminTargetUsers(admin1); |
| 3244 | MoreAsserts.assertContentsInAnyOrder(targetUsers, UserHandle.of(MANAGED_PROFILE_USER_ID)); |
| 3245 | |
| 3246 | // Calling from managed profile admin, the result list should just contain the system |
| 3247 | // user id. |
| 3248 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 3249 | targetUsers = dpm.getBindDeviceAdminTargetUsers(admin1); |
| 3250 | MoreAsserts.assertContentsInAnyOrder(targetUsers, UserHandle.SYSTEM); |
Nicolas Prevot | d5b0360 | 2016-12-06 13:32:22 +0000 | [diff] [blame] | 3251 | |
| 3252 | // Changing affiliation ids in one |
Tony Mak | 3165743 | 2017-04-25 09:29:55 +0100 | [diff] [blame] | 3253 | dpm.setAffiliationIds(admin1, Collections.singleton("some-different-affiliation-id")); |
Nicolas Prevot | d5b0360 | 2016-12-06 13:32:22 +0000 | [diff] [blame] | 3254 | |
| 3255 | // Since the managed profile is not affiliated any more, they should not be allowed to talk |
| 3256 | // to each other. |
| 3257 | targetUsers = dpm.getBindDeviceAdminTargetUsers(admin1); |
| 3258 | MoreAsserts.assertEmpty(targetUsers); |
| 3259 | |
| 3260 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3261 | targetUsers = dpm.getBindDeviceAdminTargetUsers(admin1); |
| 3262 | MoreAsserts.assertEmpty(targetUsers); |
Tony Mak | 2f26b79 | 2016-11-28 17:54:51 +0000 | [diff] [blame] | 3263 | } |
| 3264 | |
| 3265 | public void testGetBindDeviceAdminTargetUsers_differentPackage() throws Exception { |
| 3266 | // Setup a device owner. |
| 3267 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3268 | setupDeviceOwner(); |
| 3269 | |
| 3270 | // Set up a managed profile managed by different package. |
| 3271 | final int MANAGED_PROFILE_USER_ID = 15; |
| 3272 | final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 20456); |
| 3273 | final ComponentName adminDifferentPackage = |
| 3274 | new ComponentName("another.package", "whatever.class"); |
| 3275 | addManagedProfile(adminDifferentPackage, MANAGED_PROFILE_ADMIN_UID, admin2); |
| 3276 | |
Nicolas Prevot | d5b0360 | 2016-12-06 13:32:22 +0000 | [diff] [blame] | 3277 | // Setting affiliation ids |
Tony Mak | 3165743 | 2017-04-25 09:29:55 +0100 | [diff] [blame] | 3278 | final Set<String> userAffiliationIds = Collections.singleton("some-affiliation-id"); |
Nicolas Prevot | d5b0360 | 2016-12-06 13:32:22 +0000 | [diff] [blame] | 3279 | dpm.setAffiliationIds(admin1, userAffiliationIds); |
| 3280 | |
| 3281 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 3282 | dpm.setAffiliationIds(adminDifferentPackage, userAffiliationIds); |
| 3283 | |
Tony Mak | 2f26b79 | 2016-11-28 17:54:51 +0000 | [diff] [blame] | 3284 | // Calling from device owner admin, we should get zero bind device admin target users as |
| 3285 | // their packages are different. |
Nicolas Prevot | d5b0360 | 2016-12-06 13:32:22 +0000 | [diff] [blame] | 3286 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
Tony Mak | 2f26b79 | 2016-11-28 17:54:51 +0000 | [diff] [blame] | 3287 | List<UserHandle> targetUsers = dpm.getBindDeviceAdminTargetUsers(admin1); |
| 3288 | MoreAsserts.assertEmpty(targetUsers); |
| 3289 | |
| 3290 | // Calling from managed profile admin, we should still get zero target users for the same |
| 3291 | // reason. |
| 3292 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 3293 | targetUsers = dpm.getBindDeviceAdminTargetUsers(adminDifferentPackage); |
| 3294 | MoreAsserts.assertEmpty(targetUsers); |
| 3295 | } |
| 3296 | |
Esteban Talavera | bdcada9 | 2017-02-01 14:20:06 +0000 | [diff] [blame] | 3297 | public void testLockTaskPackagesAllowedForAffiliatedUsers() throws Exception { |
| 3298 | // Setup a device owner. |
| 3299 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3300 | setupDeviceOwner(); |
| 3301 | // Lock task packages are updated when loading user data. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3302 | verify(getServices().iactivityManager) |
Esteban Talavera | bdcada9 | 2017-02-01 14:20:06 +0000 | [diff] [blame] | 3303 | .updateLockTaskPackages(eq(UserHandle.USER_SYSTEM), eq(new String[0])); |
| 3304 | |
| 3305 | // Set up a managed profile managed by different package (package name shouldn't matter) |
| 3306 | final int MANAGED_PROFILE_USER_ID = 15; |
| 3307 | final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 20456); |
| 3308 | final ComponentName adminDifferentPackage = |
| 3309 | new ComponentName("another.package", "whatever.class"); |
| 3310 | addManagedProfile(adminDifferentPackage, MANAGED_PROFILE_ADMIN_UID, admin2); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3311 | verify(getServices().iactivityManager) |
Esteban Talavera | bdcada9 | 2017-02-01 14:20:06 +0000 | [diff] [blame] | 3312 | .updateLockTaskPackages(eq(MANAGED_PROFILE_USER_ID), eq(new String[0])); |
| 3313 | |
| 3314 | // The DO can still set lock task packages |
| 3315 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3316 | final String[] doPackages = {"doPackage1", "doPackage2"}; |
| 3317 | dpm.setLockTaskPackages(admin1, doPackages); |
| 3318 | MoreAsserts.assertEquals(doPackages, dpm.getLockTaskPackages(admin1)); |
| 3319 | assertTrue(dpm.isLockTaskPermitted("doPackage1")); |
| 3320 | assertFalse(dpm.isLockTaskPermitted("anotherPackage")); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3321 | verify(getServices().iactivityManager) |
Esteban Talavera | bdcada9 | 2017-02-01 14:20:06 +0000 | [diff] [blame] | 3322 | .updateLockTaskPackages(eq(UserHandle.USER_SYSTEM), eq(doPackages)); |
| 3323 | |
| 3324 | // Managed profile is unaffiliated - shouldn't be able to setLockTaskPackages. |
| 3325 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 3326 | final String[] poPackages = {"poPackage1", "poPackage2"}; |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 3327 | assertExpectException(SecurityException.class, /* messageRegex =*/ null, |
| 3328 | () -> dpm.setLockTaskPackages(adminDifferentPackage, poPackages)); |
| 3329 | assertExpectException(SecurityException.class, /* messageRegex =*/ null, |
| 3330 | () -> dpm.getLockTaskPackages(adminDifferentPackage)); |
Esteban Talavera | bdcada9 | 2017-02-01 14:20:06 +0000 | [diff] [blame] | 3331 | assertFalse(dpm.isLockTaskPermitted("doPackage1")); |
| 3332 | |
| 3333 | // Setting same affiliation ids |
Tony Mak | 3165743 | 2017-04-25 09:29:55 +0100 | [diff] [blame] | 3334 | final Set<String> userAffiliationIds = Collections.singleton("some-affiliation-id"); |
Esteban Talavera | bdcada9 | 2017-02-01 14:20:06 +0000 | [diff] [blame] | 3335 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3336 | dpm.setAffiliationIds(admin1, userAffiliationIds); |
| 3337 | |
| 3338 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 3339 | dpm.setAffiliationIds(adminDifferentPackage, userAffiliationIds); |
| 3340 | |
| 3341 | // Now the managed profile can set lock task packages. |
| 3342 | dpm.setLockTaskPackages(adminDifferentPackage, poPackages); |
| 3343 | MoreAsserts.assertEquals(poPackages, dpm.getLockTaskPackages(adminDifferentPackage)); |
| 3344 | assertTrue(dpm.isLockTaskPermitted("poPackage1")); |
| 3345 | assertFalse(dpm.isLockTaskPermitted("doPackage2")); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3346 | verify(getServices().iactivityManager) |
Esteban Talavera | bdcada9 | 2017-02-01 14:20:06 +0000 | [diff] [blame] | 3347 | .updateLockTaskPackages(eq(MANAGED_PROFILE_USER_ID), eq(poPackages)); |
| 3348 | |
| 3349 | // Unaffiliate the profile, lock task mode no longer available on the profile. |
Tony Mak | 3165743 | 2017-04-25 09:29:55 +0100 | [diff] [blame] | 3350 | dpm.setAffiliationIds(adminDifferentPackage, Collections.emptySet()); |
Esteban Talavera | bdcada9 | 2017-02-01 14:20:06 +0000 | [diff] [blame] | 3351 | assertFalse(dpm.isLockTaskPermitted("poPackage1")); |
| 3352 | // Lock task packages cleared when loading user data and when the user becomes unaffiliated. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3353 | verify(getServices().iactivityManager, times(2)) |
Esteban Talavera | bdcada9 | 2017-02-01 14:20:06 +0000 | [diff] [blame] | 3354 | .updateLockTaskPackages(eq(MANAGED_PROFILE_USER_ID), eq(new String[0])); |
| 3355 | |
| 3356 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3357 | assertTrue(dpm.isLockTaskPermitted("doPackage1")); |
| 3358 | } |
| 3359 | |
Bartosz Fabianowski | dd7f8da | 2016-11-30 11:09:22 +0100 | [diff] [blame] | 3360 | public void testIsDeviceManaged() throws Exception { |
| 3361 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3362 | setupDeviceOwner(); |
| 3363 | |
| 3364 | // The device owner itself, any uid holding MANAGE_USERS permission and the system can |
| 3365 | // find out that the device has a device owner. |
| 3366 | assertTrue(dpm.isDeviceManaged()); |
| 3367 | mContext.binder.callingUid = 1234567; |
| 3368 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
| 3369 | assertTrue(dpm.isDeviceManaged()); |
| 3370 | mContext.callerPermissions.remove(permission.MANAGE_USERS); |
| 3371 | mContext.binder.clearCallingIdentity(); |
| 3372 | assertTrue(dpm.isDeviceManaged()); |
| 3373 | |
| 3374 | clearDeviceOwner(); |
| 3375 | |
| 3376 | // Any uid holding MANAGE_USERS permission and the system can find out that the device does |
| 3377 | // not have a device owner. |
| 3378 | mContext.binder.callingUid = 1234567; |
| 3379 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
| 3380 | assertFalse(dpm.isDeviceManaged()); |
| 3381 | mContext.callerPermissions.remove(permission.MANAGE_USERS); |
| 3382 | mContext.binder.clearCallingIdentity(); |
| 3383 | assertFalse(dpm.isDeviceManaged()); |
| 3384 | } |
| 3385 | |
Bartosz Fabianowski | 365a3db | 2016-11-30 18:28:10 +0100 | [diff] [blame] | 3386 | public void testDeviceOwnerOrganizationName() throws Exception { |
| 3387 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3388 | setupDeviceOwner(); |
| 3389 | |
| 3390 | dpm.setOrganizationName(admin1, "organization"); |
| 3391 | |
| 3392 | // Device owner can retrieve organization managing the device. |
| 3393 | assertEquals("organization", dpm.getDeviceOwnerOrganizationName()); |
| 3394 | |
| 3395 | // Any uid holding MANAGE_USERS permission can retrieve organization managing the device. |
| 3396 | mContext.binder.callingUid = 1234567; |
| 3397 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
| 3398 | assertEquals("organization", dpm.getDeviceOwnerOrganizationName()); |
| 3399 | mContext.callerPermissions.remove(permission.MANAGE_USERS); |
| 3400 | |
| 3401 | // System can retrieve organization managing the device. |
| 3402 | mContext.binder.clearCallingIdentity(); |
| 3403 | assertEquals("organization", dpm.getDeviceOwnerOrganizationName()); |
| 3404 | |
| 3405 | // Removing the device owner clears the organization managing the device. |
| 3406 | clearDeviceOwner(); |
| 3407 | assertNull(dpm.getDeviceOwnerOrganizationName()); |
| 3408 | } |
| 3409 | |
Nicolas Prevot | c9c1b2f | 2017-01-12 10:52:50 +0000 | [diff] [blame] | 3410 | public void testWipeDataManagedProfile() throws Exception { |
| 3411 | final int MANAGED_PROFILE_USER_ID = 15; |
| 3412 | final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 19436); |
| 3413 | addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); |
| 3414 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 3415 | |
| 3416 | // Even if the caller is the managed profile, the current user is the user 0 |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3417 | when(getServices().iactivityManager.getCurrentUser()) |
Nicolas Prevot | c9c1b2f | 2017-01-12 10:52:50 +0000 | [diff] [blame] | 3418 | .thenReturn(new UserInfo(UserHandle.USER_SYSTEM, "user system", 0)); |
| 3419 | |
| 3420 | dpm.wipeData(0); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3421 | verify(getServices().userManagerInternal).removeUserEvenWhenDisallowed( |
Nicolas Prevot | c9c1b2f | 2017-01-12 10:52:50 +0000 | [diff] [blame] | 3422 | MANAGED_PROFILE_USER_ID); |
| 3423 | } |
| 3424 | |
| 3425 | public void testWipeDataManagedProfileDisallowed() throws Exception { |
| 3426 | final int MANAGED_PROFILE_USER_ID = 15; |
| 3427 | final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 19436); |
| 3428 | addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); |
| 3429 | |
| 3430 | // Even if the caller is the managed profile, the current user is the user 0 |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3431 | when(getServices().iactivityManager.getCurrentUser()) |
Nicolas Prevot | c9c1b2f | 2017-01-12 10:52:50 +0000 | [diff] [blame] | 3432 | .thenReturn(new UserInfo(UserHandle.USER_SYSTEM, "user system", 0)); |
| 3433 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3434 | when(getServices().userManager.getUserRestrictionSource( |
Nicolas Prevot | c9c1b2f | 2017-01-12 10:52:50 +0000 | [diff] [blame] | 3435 | UserManager.DISALLOW_REMOVE_MANAGED_PROFILE, |
| 3436 | UserHandle.of(MANAGED_PROFILE_USER_ID))) |
| 3437 | .thenReturn(UserManager.RESTRICTION_SOURCE_SYSTEM); |
| 3438 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 3439 | // The PO is not allowed to remove the profile if the user restriction was set on the |
| 3440 | // profile by the system |
| 3441 | assertExpectException(SecurityException.class, /* messageRegex= */ null, |
| 3442 | () -> dpm.wipeData(0)); |
Nicolas Prevot | c9c1b2f | 2017-01-12 10:52:50 +0000 | [diff] [blame] | 3443 | } |
| 3444 | |
Esteban Talavera | b88f42b | 2017-01-24 16:47:16 +0000 | [diff] [blame] | 3445 | public void testWipeDataDeviceOwner() throws Exception { |
| 3446 | setDeviceOwner(); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3447 | when(getServices().userManager.getUserRestrictionSource( |
Esteban Talavera | b88f42b | 2017-01-24 16:47:16 +0000 | [diff] [blame] | 3448 | UserManager.DISALLOW_FACTORY_RESET, |
| 3449 | UserHandle.SYSTEM)) |
| 3450 | .thenReturn(UserManager.RESTRICTION_SOURCE_DEVICE_OWNER); |
| 3451 | |
| 3452 | dpm.wipeData(0); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3453 | verify(getServices().recoverySystem).rebootWipeUserData( |
yinxu | f4f9cec | 2017-06-19 10:28:19 -0700 | [diff] [blame] | 3454 | /*shutdown=*/ eq(false), anyString(), /*force=*/ eq(true), |
| 3455 | /*wipeEuicc=*/ eq(false)); |
| 3456 | } |
| 3457 | |
| 3458 | public void testWipeEuiccDataEnabled() throws Exception { |
| 3459 | setDeviceOwner(); |
| 3460 | when(getServices().userManager.getUserRestrictionSource( |
| 3461 | UserManager.DISALLOW_FACTORY_RESET, |
| 3462 | UserHandle.SYSTEM)) |
| 3463 | .thenReturn(UserManager.RESTRICTION_SOURCE_DEVICE_OWNER); |
| 3464 | |
| 3465 | dpm.wipeData(WIPE_EUICC); |
| 3466 | verify(getServices().recoverySystem).rebootWipeUserData( |
| 3467 | /*shutdown=*/ eq(false), anyString(), /*force=*/ eq(true), |
| 3468 | /*wipeEuicc=*/ eq(true)); |
Esteban Talavera | b88f42b | 2017-01-24 16:47:16 +0000 | [diff] [blame] | 3469 | } |
| 3470 | |
| 3471 | public void testWipeDataDeviceOwnerDisallowed() throws Exception { |
| 3472 | setDeviceOwner(); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3473 | when(getServices().userManager.getUserRestrictionSource( |
Esteban Talavera | b88f42b | 2017-01-24 16:47:16 +0000 | [diff] [blame] | 3474 | UserManager.DISALLOW_FACTORY_RESET, |
| 3475 | UserHandle.SYSTEM)) |
| 3476 | .thenReturn(UserManager.RESTRICTION_SOURCE_SYSTEM); |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 3477 | // The DO is not allowed to wipe the device if the user restriction was set |
| 3478 | // by the system |
| 3479 | assertExpectException(SecurityException.class, /* messageRegex= */ null, |
| 3480 | () -> dpm.wipeData(0)); |
Esteban Talavera | b88f42b | 2017-01-24 16:47:16 +0000 | [diff] [blame] | 3481 | } |
| 3482 | |
| 3483 | public void testMaximumFailedPasswordAttemptsReachedManagedProfile() throws Exception { |
| 3484 | final int MANAGED_PROFILE_USER_ID = 15; |
| 3485 | final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 19436); |
| 3486 | addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); |
| 3487 | |
| 3488 | // Even if the caller is the managed profile, the current user is the user 0 |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3489 | when(getServices().iactivityManager.getCurrentUser()) |
Esteban Talavera | b88f42b | 2017-01-24 16:47:16 +0000 | [diff] [blame] | 3490 | .thenReturn(new UserInfo(UserHandle.USER_SYSTEM, "user system", 0)); |
| 3491 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3492 | when(getServices().userManager.getUserRestrictionSource( |
Esteban Talavera | b88f42b | 2017-01-24 16:47:16 +0000 | [diff] [blame] | 3493 | UserManager.DISALLOW_REMOVE_MANAGED_PROFILE, |
| 3494 | UserHandle.of(MANAGED_PROFILE_USER_ID))) |
| 3495 | .thenReturn(UserManager.RESTRICTION_SOURCE_PROFILE_OWNER); |
| 3496 | |
| 3497 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 3498 | dpm.setMaximumFailedPasswordsForWipe(admin1, 3); |
| 3499 | |
| 3500 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 3501 | mContext.callerPermissions.add(permission.BIND_DEVICE_ADMIN); |
| 3502 | // Failed password attempts on the parent user are taken into account, as there isn't a |
| 3503 | // separate work challenge. |
| 3504 | dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); |
| 3505 | dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); |
| 3506 | dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); |
| 3507 | |
| 3508 | // The profile should be wiped even if DISALLOW_REMOVE_MANAGED_PROFILE is enabled, because |
| 3509 | // both the user restriction and the policy were set by the PO. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3510 | verify(getServices().userManagerInternal).removeUserEvenWhenDisallowed( |
Esteban Talavera | b88f42b | 2017-01-24 16:47:16 +0000 | [diff] [blame] | 3511 | MANAGED_PROFILE_USER_ID); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3512 | verifyZeroInteractions(getServices().recoverySystem); |
Esteban Talavera | b88f42b | 2017-01-24 16:47:16 +0000 | [diff] [blame] | 3513 | } |
| 3514 | |
| 3515 | public void testMaximumFailedPasswordAttemptsReachedManagedProfileDisallowed() |
| 3516 | throws Exception { |
| 3517 | final int MANAGED_PROFILE_USER_ID = 15; |
| 3518 | final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 19436); |
| 3519 | addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); |
| 3520 | |
| 3521 | // Even if the caller is the managed profile, the current user is the user 0 |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3522 | when(getServices().iactivityManager.getCurrentUser()) |
Esteban Talavera | b88f42b | 2017-01-24 16:47:16 +0000 | [diff] [blame] | 3523 | .thenReturn(new UserInfo(UserHandle.USER_SYSTEM, "user system", 0)); |
| 3524 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3525 | when(getServices().userManager.getUserRestrictionSource( |
Esteban Talavera | b88f42b | 2017-01-24 16:47:16 +0000 | [diff] [blame] | 3526 | UserManager.DISALLOW_REMOVE_MANAGED_PROFILE, |
| 3527 | UserHandle.of(MANAGED_PROFILE_USER_ID))) |
| 3528 | .thenReturn(UserManager.RESTRICTION_SOURCE_SYSTEM); |
| 3529 | |
| 3530 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 3531 | dpm.setMaximumFailedPasswordsForWipe(admin1, 3); |
| 3532 | |
| 3533 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 3534 | mContext.callerPermissions.add(permission.BIND_DEVICE_ADMIN); |
| 3535 | // Failed password attempts on the parent user are taken into account, as there isn't a |
| 3536 | // separate work challenge. |
| 3537 | dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); |
| 3538 | dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); |
| 3539 | dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); |
| 3540 | |
| 3541 | // DISALLOW_REMOVE_MANAGED_PROFILE was set by the system, not the PO, so the profile is |
| 3542 | // not wiped. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3543 | verify(getServices().userManagerInternal, never()) |
Esteban Talavera | b88f42b | 2017-01-24 16:47:16 +0000 | [diff] [blame] | 3544 | .removeUserEvenWhenDisallowed(anyInt()); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3545 | verifyZeroInteractions(getServices().recoverySystem); |
Esteban Talavera | b88f42b | 2017-01-24 16:47:16 +0000 | [diff] [blame] | 3546 | } |
| 3547 | |
| 3548 | public void testMaximumFailedPasswordAttemptsReachedDeviceOwner() throws Exception { |
| 3549 | setDeviceOwner(); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3550 | when(getServices().userManager.getUserRestrictionSource( |
Esteban Talavera | b88f42b | 2017-01-24 16:47:16 +0000 | [diff] [blame] | 3551 | UserManager.DISALLOW_FACTORY_RESET, |
| 3552 | UserHandle.SYSTEM)) |
| 3553 | .thenReturn(UserManager.RESTRICTION_SOURCE_DEVICE_OWNER); |
| 3554 | |
| 3555 | dpm.setMaximumFailedPasswordsForWipe(admin1, 3); |
| 3556 | |
| 3557 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 3558 | mContext.callerPermissions.add(permission.BIND_DEVICE_ADMIN); |
| 3559 | dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); |
| 3560 | dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); |
| 3561 | dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); |
| 3562 | |
| 3563 | // The device should be wiped even if DISALLOW_FACTORY_RESET is enabled, because both the |
| 3564 | // user restriction and the policy were set by the DO. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3565 | verify(getServices().recoverySystem).rebootWipeUserData( |
yinxu | f4f9cec | 2017-06-19 10:28:19 -0700 | [diff] [blame] | 3566 | /*shutdown=*/ eq(false), anyString(), /*force=*/ eq(true), |
| 3567 | /*wipeEuicc=*/ eq(false)); |
Esteban Talavera | b88f42b | 2017-01-24 16:47:16 +0000 | [diff] [blame] | 3568 | } |
| 3569 | |
| 3570 | public void testMaximumFailedPasswordAttemptsReachedDeviceOwnerDisallowed() throws Exception { |
| 3571 | setDeviceOwner(); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3572 | when(getServices().userManager.getUserRestrictionSource( |
Esteban Talavera | b88f42b | 2017-01-24 16:47:16 +0000 | [diff] [blame] | 3573 | UserManager.DISALLOW_FACTORY_RESET, |
| 3574 | UserHandle.SYSTEM)) |
| 3575 | .thenReturn(UserManager.RESTRICTION_SOURCE_SYSTEM); |
| 3576 | |
| 3577 | dpm.setMaximumFailedPasswordsForWipe(admin1, 3); |
| 3578 | |
| 3579 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 3580 | mContext.callerPermissions.add(permission.BIND_DEVICE_ADMIN); |
| 3581 | dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); |
| 3582 | dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); |
| 3583 | dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); |
| 3584 | |
| 3585 | // DISALLOW_FACTORY_RESET was set by the system, not the DO, so the device is not wiped. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3586 | verifyZeroInteractions(getServices().recoverySystem); |
| 3587 | verify(getServices().userManagerInternal, never()) |
Esteban Talavera | b88f42b | 2017-01-24 16:47:16 +0000 | [diff] [blame] | 3588 | .removeUserEvenWhenDisallowed(anyInt()); |
| 3589 | } |
| 3590 | |
Bartosz Fabianowski | 4b0624f | 2017-01-16 11:47:28 +0100 | [diff] [blame] | 3591 | public void testGetPermissionGrantState() throws Exception { |
| 3592 | final String permission = "some.permission"; |
| 3593 | final String app1 = "com.example.app1"; |
| 3594 | final String app2 = "com.example.app2"; |
| 3595 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3596 | when(getServices().ipackageManager.checkPermission(eq(permission), eq(app1), anyInt())) |
Bartosz Fabianowski | 4b0624f | 2017-01-16 11:47:28 +0100 | [diff] [blame] | 3597 | .thenReturn(PackageManager.PERMISSION_GRANTED); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3598 | doReturn(PackageManager.FLAG_PERMISSION_POLICY_FIXED).when(getServices().packageManager) |
Bartosz Fabianowski | 4b0624f | 2017-01-16 11:47:28 +0100 | [diff] [blame] | 3599 | .getPermissionFlags(permission, app1, UserHandle.SYSTEM); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3600 | when(getServices().packageManager.getPermissionFlags(permission, app1, |
Bartosz Fabianowski | 4b0624f | 2017-01-16 11:47:28 +0100 | [diff] [blame] | 3601 | UserHandle.of(DpmMockContext.CALLER_USER_HANDLE))) |
| 3602 | .thenReturn(PackageManager.FLAG_PERMISSION_POLICY_FIXED); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3603 | when(getServices().ipackageManager.checkPermission(eq(permission), eq(app2), anyInt())) |
Bartosz Fabianowski | 4b0624f | 2017-01-16 11:47:28 +0100 | [diff] [blame] | 3604 | .thenReturn(PackageManager.PERMISSION_DENIED); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3605 | doReturn(0).when(getServices().packageManager).getPermissionFlags(permission, app2, |
Bartosz Fabianowski | 4b0624f | 2017-01-16 11:47:28 +0100 | [diff] [blame] | 3606 | UserHandle.SYSTEM); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3607 | when(getServices().packageManager.getPermissionFlags(permission, app2, |
Bartosz Fabianowski | 4b0624f | 2017-01-16 11:47:28 +0100 | [diff] [blame] | 3608 | UserHandle.of(DpmMockContext.CALLER_USER_HANDLE))).thenReturn(0); |
| 3609 | |
| 3610 | // System can retrieve permission grant state. |
| 3611 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
Bartosz Fabianowski | 2dbf9ae | 2017-01-27 14:47:17 +0100 | [diff] [blame] | 3612 | mContext.packageName = "com.example.system"; |
Bartosz Fabianowski | 4b0624f | 2017-01-16 11:47:28 +0100 | [diff] [blame] | 3613 | assertEquals(DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED, |
| 3614 | dpm.getPermissionGrantState(null, app1, permission)); |
| 3615 | assertEquals(DevicePolicyManager.PERMISSION_GRANT_STATE_DEFAULT, |
| 3616 | dpm.getPermissionGrantState(null, app2, permission)); |
| 3617 | |
| 3618 | // A regular app cannot retrieve permission grant state. |
Bartosz Fabianowski | 2dbf9ae | 2017-01-27 14:47:17 +0100 | [diff] [blame] | 3619 | mContext.binder.callingUid = setupPackageInPackageManager(app1, 1); |
| 3620 | mContext.packageName = app1; |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 3621 | assertExpectException(SecurityException.class, /* messageRegex= */ null, |
| 3622 | () -> dpm.getPermissionGrantState(null, app1, permission)); |
Bartosz Fabianowski | 4b0624f | 2017-01-16 11:47:28 +0100 | [diff] [blame] | 3623 | |
| 3624 | // Profile owner can retrieve permission grant state. |
Bartosz Fabianowski | 2dbf9ae | 2017-01-27 14:47:17 +0100 | [diff] [blame] | 3625 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 3626 | mContext.packageName = admin1.getPackageName(); |
Bartosz Fabianowski | 4b0624f | 2017-01-16 11:47:28 +0100 | [diff] [blame] | 3627 | setAsProfileOwner(admin1); |
| 3628 | assertEquals(DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED, |
| 3629 | dpm.getPermissionGrantState(admin1, app1, permission)); |
| 3630 | assertEquals(DevicePolicyManager.PERMISSION_GRANT_STATE_DEFAULT, |
| 3631 | dpm.getPermissionGrantState(admin1, app2, permission)); |
| 3632 | } |
| 3633 | |
Rubin Xu | aab7a41 | 2016-12-30 21:13:29 +0000 | [diff] [blame] | 3634 | public void testResetPasswordWithToken() throws Exception { |
| 3635 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3636 | setupDeviceOwner(); |
| 3637 | // test token validation |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 3638 | assertExpectException(IllegalArgumentException.class, /* messageRegex= */ null, |
| 3639 | () -> dpm.setResetPasswordToken(admin1, new byte[31])); |
| 3640 | |
Rubin Xu | aab7a41 | 2016-12-30 21:13:29 +0000 | [diff] [blame] | 3641 | // test adding a token |
| 3642 | final byte[] token = new byte[32]; |
| 3643 | final long handle = 123456; |
| 3644 | final String password = "password"; |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3645 | when(getServices().lockPatternUtils.addEscrowToken(eq(token), eq(UserHandle.USER_SYSTEM))) |
Rubin Xu | aab7a41 | 2016-12-30 21:13:29 +0000 | [diff] [blame] | 3646 | .thenReturn(handle); |
| 3647 | assertTrue(dpm.setResetPasswordToken(admin1, token)); |
| 3648 | |
| 3649 | // test password activation |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3650 | when(getServices().lockPatternUtils.isEscrowTokenActive(eq(handle), eq(UserHandle.USER_SYSTEM))) |
Rubin Xu | aab7a41 | 2016-12-30 21:13:29 +0000 | [diff] [blame] | 3651 | .thenReturn(true); |
| 3652 | assertTrue(dpm.isResetPasswordTokenActive(admin1)); |
| 3653 | |
| 3654 | // test reset password with token |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3655 | when(getServices().lockPatternUtils.setLockCredentialWithToken(eq(password), |
Rubin Xu | 7cf4509 | 2017-08-28 11:47:35 +0100 | [diff] [blame^] | 3656 | eq(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD), |
| 3657 | eq(DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED), eq(handle), eq(token), |
Rubin Xu | aab7a41 | 2016-12-30 21:13:29 +0000 | [diff] [blame] | 3658 | eq(UserHandle.USER_SYSTEM))) |
| 3659 | .thenReturn(true); |
| 3660 | assertTrue(dpm.resetPasswordWithToken(admin1, password, token, 0)); |
| 3661 | |
| 3662 | // test removing a token |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3663 | when(getServices().lockPatternUtils.removeEscrowToken(eq(handle), eq(UserHandle.USER_SYSTEM))) |
Rubin Xu | aab7a41 | 2016-12-30 21:13:29 +0000 | [diff] [blame] | 3664 | .thenReturn(true); |
| 3665 | assertTrue(dpm.clearResetPasswordToken(admin1)); |
| 3666 | } |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3667 | |
Eric Sandness | fabfcb0 | 2017-05-03 18:28:56 +0100 | [diff] [blame] | 3668 | public void testIsActivePasswordSufficient() throws Exception { |
| 3669 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3670 | mContext.packageName = admin1.getPackageName(); |
| 3671 | setupDeviceOwner(); |
| 3672 | |
| 3673 | dpm.setPasswordQuality(admin1, DevicePolicyManager.PASSWORD_QUALITY_COMPLEX); |
| 3674 | dpm.setPasswordMinimumLength(admin1, 8); |
| 3675 | dpm.setPasswordMinimumLetters(admin1, 6); |
| 3676 | dpm.setPasswordMinimumLowerCase(admin1, 3); |
| 3677 | dpm.setPasswordMinimumUpperCase(admin1, 1); |
| 3678 | dpm.setPasswordMinimumNonLetter(admin1, 1); |
| 3679 | dpm.setPasswordMinimumNumeric(admin1, 1); |
| 3680 | dpm.setPasswordMinimumSymbols(admin1, 0); |
| 3681 | |
| 3682 | PasswordMetrics passwordMetricsNoSymbols = new PasswordMetrics( |
| 3683 | DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, 9, |
| 3684 | 8, 2, |
| 3685 | 6, 1, |
| 3686 | 0, 1); |
| 3687 | |
| 3688 | setActivePasswordState(passwordMetricsNoSymbols); |
| 3689 | assertTrue(dpm.isActivePasswordSufficient()); |
| 3690 | |
| 3691 | initializeDpms(); |
| 3692 | reset(mContext.spiedContext); |
| 3693 | assertTrue(dpm.isActivePasswordSufficient()); |
| 3694 | |
| 3695 | // This call simulates the user entering the password for the first time after a reboot. |
| 3696 | // This causes password metrics to be reloaded into memory. Until this happens, |
| 3697 | // dpm.isActivePasswordSufficient() will continue to return its last checkpointed value, |
| 3698 | // even if the DPC changes password requirements so that the password no longer meets the |
| 3699 | // requirements. This is a known limitation of the current implementation of |
| 3700 | // isActivePasswordSufficient() - see b/34218769. |
| 3701 | setActivePasswordState(passwordMetricsNoSymbols); |
| 3702 | assertTrue(dpm.isActivePasswordSufficient()); |
| 3703 | |
| 3704 | dpm.setPasswordMinimumSymbols(admin1, 1); |
| 3705 | // This assertion would fail if we had not called setActivePasswordState() again after |
| 3706 | // initializeDpms() - see previous comment. |
| 3707 | assertFalse(dpm.isActivePasswordSufficient()); |
| 3708 | |
| 3709 | initializeDpms(); |
| 3710 | reset(mContext.spiedContext); |
| 3711 | assertFalse(dpm.isActivePasswordSufficient()); |
| 3712 | |
| 3713 | PasswordMetrics passwordMetricsWithSymbols = new PasswordMetrics( |
| 3714 | DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, 9, |
| 3715 | 7, 2, |
| 3716 | 5, 1, |
| 3717 | 1, 2); |
| 3718 | |
| 3719 | setActivePasswordState(passwordMetricsWithSymbols); |
| 3720 | assertTrue(dpm.isActivePasswordSufficient()); |
| 3721 | } |
| 3722 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3723 | private void setActivePasswordState(PasswordMetrics passwordMetrics) |
| 3724 | throws Exception { |
| 3725 | final int userHandle = UserHandle.getUserId(mContext.binder.callingUid); |
Eric Sandness | fabfcb0 | 2017-05-03 18:28:56 +0100 | [diff] [blame] | 3726 | final long ident = mContext.binder.clearCallingIdentity(); |
Eric Sandness | fabfcb0 | 2017-05-03 18:28:56 +0100 | [diff] [blame] | 3727 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3728 | dpm.setActivePasswordState(passwordMetrics, userHandle); |
| 3729 | dpm.reportPasswordChanged(userHandle); |
Eric Sandness | fabfcb0 | 2017-05-03 18:28:56 +0100 | [diff] [blame] | 3730 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3731 | final Intent intent = new Intent(DeviceAdminReceiver.ACTION_PASSWORD_CHANGED); |
| 3732 | intent.setComponent(admin1); |
| 3733 | intent.putExtra(Intent.EXTRA_USER, UserHandle.of(mContext.binder.callingUid)); |
| 3734 | |
| 3735 | verify(mContext.spiedContext, times(1)).sendBroadcastAsUser( |
| 3736 | MockUtils.checkIntent(intent), |
| 3737 | MockUtils.checkUserHandle(userHandle)); |
| 3738 | |
| 3739 | // CertificateMonitor.updateInstalledCertificates is called on the background thread, |
| 3740 | // let it finish with system uid, otherwise it will throw and crash. |
| 3741 | flushTasks(); |
| 3742 | |
| 3743 | mContext.binder.restoreCallingIdentity(ident); |
Eric Sandness | fabfcb0 | 2017-05-03 18:28:56 +0100 | [diff] [blame] | 3744 | } |
| 3745 | |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3746 | public void testIsCurrentInputMethodSetByOwnerForDeviceOwner() throws Exception { |
| 3747 | final String currentIme = Settings.Secure.DEFAULT_INPUT_METHOD; |
| 3748 | final Uri currentImeUri = Settings.Secure.getUriFor(currentIme); |
| 3749 | final int deviceOwnerUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3750 | final int firstUserSystemUid = UserHandle.getUid(UserHandle.USER_SYSTEM, |
| 3751 | DpmMockContext.SYSTEM_UID); |
| 3752 | final int secondUserSystemUid = UserHandle.getUid(DpmMockContext.CALLER_USER_HANDLE, |
| 3753 | DpmMockContext.SYSTEM_UID); |
| 3754 | |
| 3755 | // Set up a device owner. |
| 3756 | mContext.binder.callingUid = deviceOwnerUid; |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3757 | setupDeviceOwner(); |
| 3758 | |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3759 | // First and second user set IMEs manually. |
| 3760 | mContext.binder.callingUid = firstUserSystemUid; |
| 3761 | assertFalse(dpm.isCurrentInputMethodSetByOwner()); |
| 3762 | mContext.binder.callingUid = secondUserSystemUid; |
| 3763 | assertFalse(dpm.isCurrentInputMethodSetByOwner()); |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3764 | |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3765 | // Device owner changes IME for first user. |
| 3766 | mContext.binder.callingUid = deviceOwnerUid; |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3767 | when(getServices().settings.settingsSecureGetStringForUser(currentIme, UserHandle.USER_SYSTEM)) |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3768 | .thenReturn("ime1"); |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3769 | dpm.setSecureSetting(admin1, currentIme, "ime2"); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3770 | verify(getServices().settings).settingsSecurePutStringForUser(currentIme, "ime2", |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3771 | UserHandle.USER_SYSTEM); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3772 | reset(getServices().settings); |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3773 | dpms.notifyChangeToContentObserver(currentImeUri, UserHandle.USER_SYSTEM); |
| 3774 | mContext.binder.callingUid = firstUserSystemUid; |
| 3775 | assertTrue(dpm.isCurrentInputMethodSetByOwner()); |
| 3776 | mContext.binder.callingUid = secondUserSystemUid; |
| 3777 | assertFalse(dpm.isCurrentInputMethodSetByOwner()); |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3778 | |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3779 | // Second user changes IME manually. |
| 3780 | dpms.notifyChangeToContentObserver(currentImeUri, DpmMockContext.CALLER_USER_HANDLE); |
| 3781 | mContext.binder.callingUid = firstUserSystemUid; |
| 3782 | assertTrue(dpm.isCurrentInputMethodSetByOwner()); |
| 3783 | mContext.binder.callingUid = secondUserSystemUid; |
| 3784 | assertFalse(dpm.isCurrentInputMethodSetByOwner()); |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3785 | |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3786 | // First user changes IME manually. |
| 3787 | dpms.notifyChangeToContentObserver(currentImeUri, UserHandle.USER_SYSTEM); |
| 3788 | mContext.binder.callingUid = firstUserSystemUid; |
| 3789 | assertFalse(dpm.isCurrentInputMethodSetByOwner()); |
| 3790 | mContext.binder.callingUid = secondUserSystemUid; |
| 3791 | assertFalse(dpm.isCurrentInputMethodSetByOwner()); |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3792 | |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3793 | // Device owner changes IME for first user again. |
| 3794 | mContext.binder.callingUid = deviceOwnerUid; |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3795 | when(getServices().settings.settingsSecureGetStringForUser(currentIme, UserHandle.USER_SYSTEM)) |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3796 | .thenReturn("ime2"); |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3797 | dpm.setSecureSetting(admin1, currentIme, "ime3"); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3798 | verify(getServices().settings).settingsSecurePutStringForUser(currentIme, "ime3", |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3799 | UserHandle.USER_SYSTEM); |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3800 | dpms.notifyChangeToContentObserver(currentImeUri, UserHandle.USER_SYSTEM); |
| 3801 | mContext.binder.callingUid = firstUserSystemUid; |
| 3802 | assertTrue(dpm.isCurrentInputMethodSetByOwner()); |
| 3803 | mContext.binder.callingUid = secondUserSystemUid; |
| 3804 | assertFalse(dpm.isCurrentInputMethodSetByOwner()); |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3805 | |
| 3806 | // Restarting the DPMS should not lose information. |
| 3807 | initializeDpms(); |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3808 | mContext.binder.callingUid = firstUserSystemUid; |
| 3809 | assertTrue(dpm.isCurrentInputMethodSetByOwner()); |
| 3810 | mContext.binder.callingUid = secondUserSystemUid; |
| 3811 | assertFalse(dpm.isCurrentInputMethodSetByOwner()); |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3812 | |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3813 | // Device owner can find out whether it set the current IME itself. |
| 3814 | mContext.binder.callingUid = deviceOwnerUid; |
| 3815 | assertTrue(dpm.isCurrentInputMethodSetByOwner()); |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3816 | |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3817 | // Removing the device owner should clear the information that it set the current IME. |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3818 | clearDeviceOwner(); |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3819 | mContext.binder.callingUid = firstUserSystemUid; |
| 3820 | assertFalse(dpm.isCurrentInputMethodSetByOwner()); |
| 3821 | mContext.binder.callingUid = secondUserSystemUid; |
| 3822 | assertFalse(dpm.isCurrentInputMethodSetByOwner()); |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3823 | } |
| 3824 | |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3825 | public void testIsCurrentInputMethodSetByOwnerForProfileOwner() throws Exception { |
| 3826 | final String currentIme = Settings.Secure.DEFAULT_INPUT_METHOD; |
| 3827 | final Uri currentImeUri = Settings.Secure.getUriFor(currentIme); |
| 3828 | final int profileOwnerUid = DpmMockContext.CALLER_UID; |
| 3829 | final int firstUserSystemUid = UserHandle.getUid(UserHandle.USER_SYSTEM, |
| 3830 | DpmMockContext.SYSTEM_UID); |
| 3831 | final int secondUserSystemUid = UserHandle.getUid(DpmMockContext.CALLER_USER_HANDLE, |
| 3832 | DpmMockContext.SYSTEM_UID); |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3833 | |
| 3834 | // Set up a profile owner. |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3835 | mContext.binder.callingUid = profileOwnerUid; |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3836 | setupProfileOwner(); |
| 3837 | |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3838 | // First and second user set IMEs manually. |
| 3839 | mContext.binder.callingUid = firstUserSystemUid; |
| 3840 | assertFalse(dpm.isCurrentInputMethodSetByOwner()); |
| 3841 | mContext.binder.callingUid = secondUserSystemUid; |
| 3842 | assertFalse(dpm.isCurrentInputMethodSetByOwner()); |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3843 | |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3844 | // Profile owner changes IME for second user. |
| 3845 | mContext.binder.callingUid = profileOwnerUid; |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3846 | when(getServices().settings.settingsSecureGetStringForUser(currentIme, |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3847 | DpmMockContext.CALLER_USER_HANDLE)).thenReturn("ime1"); |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3848 | dpm.setSecureSetting(admin1, currentIme, "ime2"); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3849 | verify(getServices().settings).settingsSecurePutStringForUser(currentIme, "ime2", |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3850 | DpmMockContext.CALLER_USER_HANDLE); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3851 | reset(getServices().settings); |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3852 | dpms.notifyChangeToContentObserver(currentImeUri, DpmMockContext.CALLER_USER_HANDLE); |
| 3853 | mContext.binder.callingUid = firstUserSystemUid; |
| 3854 | assertFalse(dpm.isCurrentInputMethodSetByOwner()); |
| 3855 | mContext.binder.callingUid = secondUserSystemUid; |
| 3856 | assertTrue(dpm.isCurrentInputMethodSetByOwner()); |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3857 | |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3858 | // First user changes IME manually. |
| 3859 | dpms.notifyChangeToContentObserver(currentImeUri, UserHandle.USER_SYSTEM); |
| 3860 | mContext.binder.callingUid = firstUserSystemUid; |
| 3861 | assertFalse(dpm.isCurrentInputMethodSetByOwner()); |
| 3862 | mContext.binder.callingUid = secondUserSystemUid; |
| 3863 | assertTrue(dpm.isCurrentInputMethodSetByOwner()); |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3864 | |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3865 | // Second user changes IME manually. |
| 3866 | dpms.notifyChangeToContentObserver(currentImeUri, DpmMockContext.CALLER_USER_HANDLE); |
| 3867 | mContext.binder.callingUid = firstUserSystemUid; |
| 3868 | assertFalse(dpm.isCurrentInputMethodSetByOwner()); |
| 3869 | mContext.binder.callingUid = secondUserSystemUid; |
| 3870 | assertFalse(dpm.isCurrentInputMethodSetByOwner()); |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3871 | |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3872 | // Profile owner changes IME for second user again. |
| 3873 | mContext.binder.callingUid = profileOwnerUid; |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3874 | when(getServices().settings.settingsSecureGetStringForUser(currentIme, |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3875 | DpmMockContext.CALLER_USER_HANDLE)).thenReturn("ime2"); |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3876 | dpm.setSecureSetting(admin1, currentIme, "ime3"); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3877 | verify(getServices().settings).settingsSecurePutStringForUser(currentIme, "ime3", |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3878 | DpmMockContext.CALLER_USER_HANDLE); |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3879 | dpms.notifyChangeToContentObserver(currentImeUri, DpmMockContext.CALLER_USER_HANDLE); |
| 3880 | mContext.binder.callingUid = firstUserSystemUid; |
| 3881 | assertFalse(dpm.isCurrentInputMethodSetByOwner()); |
| 3882 | mContext.binder.callingUid = secondUserSystemUid; |
| 3883 | assertTrue(dpm.isCurrentInputMethodSetByOwner()); |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3884 | |
| 3885 | // Restarting the DPMS should not lose information. |
| 3886 | initializeDpms(); |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3887 | mContext.binder.callingUid = firstUserSystemUid; |
| 3888 | assertFalse(dpm.isCurrentInputMethodSetByOwner()); |
| 3889 | mContext.binder.callingUid = secondUserSystemUid; |
| 3890 | assertTrue(dpm.isCurrentInputMethodSetByOwner()); |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3891 | |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3892 | // Profile owner can find out whether it set the current IME itself. |
| 3893 | mContext.binder.callingUid = profileOwnerUid; |
| 3894 | assertTrue(dpm.isCurrentInputMethodSetByOwner()); |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3895 | |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3896 | // Removing the profile owner should clear the information that it set the current IME. |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3897 | dpm.clearProfileOwner(admin1); |
Bartosz Fabianowski | 172f780 | 2017-03-06 12:07:57 +0100 | [diff] [blame] | 3898 | mContext.binder.callingUid = firstUserSystemUid; |
| 3899 | assertFalse(dpm.isCurrentInputMethodSetByOwner()); |
| 3900 | mContext.binder.callingUid = secondUserSystemUid; |
| 3901 | assertFalse(dpm.isCurrentInputMethodSetByOwner()); |
Bartosz Fabianowski | aff5e9c | 2017-01-25 06:09:13 +0100 | [diff] [blame] | 3902 | } |
Rubin Xu | aab7a41 | 2016-12-30 21:13:29 +0000 | [diff] [blame] | 3903 | |
Esteban Talavera | 7e4cbad | 2017-03-30 17:59:50 +0100 | [diff] [blame] | 3904 | public void testSetPermittedCrossProfileNotificationListeners_unavailableForDo() |
| 3905 | throws Exception { |
| 3906 | // Set up a device owner. |
| 3907 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3908 | setupDeviceOwner(); |
| 3909 | assertSetPermittedCrossProfileNotificationListenersUnavailable(mContext.binder.callingUid); |
| 3910 | } |
| 3911 | |
| 3912 | public void testSetPermittedCrossProfileNotificationListeners_unavailableForPoOnUser() |
| 3913 | throws Exception { |
| 3914 | // Set up a profile owner. |
| 3915 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 3916 | setupProfileOwner(); |
| 3917 | assertSetPermittedCrossProfileNotificationListenersUnavailable(mContext.binder.callingUid); |
| 3918 | } |
| 3919 | |
| 3920 | private void assertSetPermittedCrossProfileNotificationListenersUnavailable( |
| 3921 | int adminUid) throws Exception { |
| 3922 | mContext.binder.callingUid = adminUid; |
| 3923 | final int userId = UserHandle.getUserId(adminUid); |
| 3924 | |
| 3925 | final String packageName = "some.package"; |
| 3926 | assertFalse(dpms.setPermittedCrossProfileNotificationListeners( |
| 3927 | admin1, Collections.singletonList(packageName))); |
| 3928 | assertNull(dpms.getPermittedCrossProfileNotificationListeners(admin1)); |
| 3929 | |
| 3930 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 3931 | assertTrue(dpms.isNotificationListenerServicePermitted(packageName, userId)); |
| 3932 | |
| 3933 | // Attempt to set to empty list (which means no listener is whitelisted) |
| 3934 | mContext.binder.callingUid = adminUid; |
| 3935 | assertFalse(dpms.setPermittedCrossProfileNotificationListeners( |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 3936 | admin1, Collections.emptyList())); |
Esteban Talavera | 7e4cbad | 2017-03-30 17:59:50 +0100 | [diff] [blame] | 3937 | assertNull(dpms.getPermittedCrossProfileNotificationListeners(admin1)); |
| 3938 | |
| 3939 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 3940 | assertTrue(dpms.isNotificationListenerServicePermitted(packageName, userId)); |
| 3941 | } |
| 3942 | |
| 3943 | public void testIsNotificationListenerServicePermitted_onlySystemCanCall() throws Exception { |
| 3944 | // Set up a managed profile |
| 3945 | final int MANAGED_PROFILE_USER_ID = 15; |
| 3946 | final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 19436); |
| 3947 | addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); |
| 3948 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 3949 | |
| 3950 | final String permittedListener = "some.package"; |
| 3951 | setupPackageInPackageManager( |
| 3952 | permittedListener, |
| 3953 | UserHandle.USER_SYSTEM, // We check the packageInfo from the primary user. |
| 3954 | /*appId=*/ 12345, /*flags=*/ 0); |
| 3955 | |
| 3956 | assertTrue(dpms.setPermittedCrossProfileNotificationListeners( |
| 3957 | admin1, Collections.singletonList(permittedListener))); |
| 3958 | |
Pavel Grafov | a1ea8d9 | 2017-05-25 21:55:24 +0100 | [diff] [blame] | 3959 | // isNotificationListenerServicePermitted should throw if not called from System. |
| 3960 | assertExpectException(SecurityException.class, /* messageRegex= */ null, |
| 3961 | () -> dpms.isNotificationListenerServicePermitted( |
| 3962 | permittedListener, MANAGED_PROFILE_USER_ID)); |
Esteban Talavera | 7e4cbad | 2017-03-30 17:59:50 +0100 | [diff] [blame] | 3963 | |
| 3964 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 3965 | assertTrue(dpms.isNotificationListenerServicePermitted( |
| 3966 | permittedListener, MANAGED_PROFILE_USER_ID)); |
| 3967 | } |
| 3968 | |
| 3969 | public void testSetPermittedCrossProfileNotificationListeners_managedProfile() |
| 3970 | throws Exception { |
| 3971 | // Set up a managed profile |
| 3972 | final int MANAGED_PROFILE_USER_ID = 15; |
| 3973 | final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 19436); |
| 3974 | addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); |
| 3975 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 3976 | |
| 3977 | final String permittedListener = "permitted.package"; |
| 3978 | int appId = 12345; |
| 3979 | setupPackageInPackageManager( |
| 3980 | permittedListener, |
| 3981 | UserHandle.USER_SYSTEM, // We check the packageInfo from the primary user. |
| 3982 | appId, /*flags=*/ 0); |
| 3983 | |
| 3984 | final String notPermittedListener = "not.permitted.package"; |
| 3985 | setupPackageInPackageManager( |
| 3986 | notPermittedListener, |
| 3987 | UserHandle.USER_SYSTEM, // We check the packageInfo from the primary user. |
| 3988 | ++appId, /*flags=*/ 0); |
| 3989 | |
| 3990 | final String systemListener = "system.package"; |
| 3991 | setupPackageInPackageManager( |
| 3992 | systemListener, |
| 3993 | UserHandle.USER_SYSTEM, // We check the packageInfo from the primary user. |
| 3994 | ++appId, ApplicationInfo.FLAG_SYSTEM); |
| 3995 | |
| 3996 | // By default all packages are allowed |
| 3997 | assertNull(dpms.getPermittedCrossProfileNotificationListeners(admin1)); |
| 3998 | |
| 3999 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 4000 | assertTrue(dpms.isNotificationListenerServicePermitted( |
| 4001 | permittedListener, MANAGED_PROFILE_USER_ID)); |
| 4002 | assertTrue(dpms.isNotificationListenerServicePermitted( |
| 4003 | notPermittedListener, MANAGED_PROFILE_USER_ID)); |
| 4004 | assertTrue(dpms.isNotificationListenerServicePermitted( |
| 4005 | systemListener, MANAGED_PROFILE_USER_ID)); |
| 4006 | |
| 4007 | // Setting only one package in the whitelist |
| 4008 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 4009 | assertTrue(dpms.setPermittedCrossProfileNotificationListeners( |
| 4010 | admin1, Collections.singletonList(permittedListener))); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4011 | final List<String> permittedListeners = |
Esteban Talavera | 7e4cbad | 2017-03-30 17:59:50 +0100 | [diff] [blame] | 4012 | dpms.getPermittedCrossProfileNotificationListeners(admin1); |
| 4013 | assertEquals(1, permittedListeners.size()); |
| 4014 | assertEquals(permittedListener, permittedListeners.get(0)); |
| 4015 | |
| 4016 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 4017 | assertTrue(dpms.isNotificationListenerServicePermitted( |
| 4018 | permittedListener, MANAGED_PROFILE_USER_ID)); |
| 4019 | assertFalse(dpms.isNotificationListenerServicePermitted( |
| 4020 | notPermittedListener, MANAGED_PROFILE_USER_ID)); |
| 4021 | // System packages are always allowed (even if not in the whitelist) |
| 4022 | assertTrue(dpms.isNotificationListenerServicePermitted( |
| 4023 | systemListener, MANAGED_PROFILE_USER_ID)); |
| 4024 | |
| 4025 | // Setting an empty whitelist - only system listeners allowed |
| 4026 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 4027 | assertTrue(dpms.setPermittedCrossProfileNotificationListeners( |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4028 | admin1, Collections.emptyList())); |
Esteban Talavera | 7e4cbad | 2017-03-30 17:59:50 +0100 | [diff] [blame] | 4029 | assertEquals(0, dpms.getPermittedCrossProfileNotificationListeners(admin1).size()); |
| 4030 | |
| 4031 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 4032 | assertFalse(dpms.isNotificationListenerServicePermitted( |
| 4033 | permittedListener, MANAGED_PROFILE_USER_ID)); |
| 4034 | assertFalse(dpms.isNotificationListenerServicePermitted( |
| 4035 | notPermittedListener, MANAGED_PROFILE_USER_ID)); |
| 4036 | // System packages are always allowed (even if not in the whitelist) |
| 4037 | assertTrue(dpms.isNotificationListenerServicePermitted( |
| 4038 | systemListener, MANAGED_PROFILE_USER_ID)); |
| 4039 | |
| 4040 | // Setting a null whitelist - all listeners allowed |
| 4041 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 4042 | assertTrue(dpms.setPermittedCrossProfileNotificationListeners(admin1, null)); |
| 4043 | assertNull(dpms.getPermittedCrossProfileNotificationListeners(admin1)); |
| 4044 | |
| 4045 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 4046 | assertTrue(dpms.isNotificationListenerServicePermitted( |
| 4047 | permittedListener, MANAGED_PROFILE_USER_ID)); |
| 4048 | assertTrue(dpms.isNotificationListenerServicePermitted( |
| 4049 | notPermittedListener, MANAGED_PROFILE_USER_ID)); |
| 4050 | assertTrue(dpms.isNotificationListenerServicePermitted( |
| 4051 | systemListener, MANAGED_PROFILE_USER_ID)); |
| 4052 | } |
| 4053 | |
| 4054 | public void testSetPermittedCrossProfileNotificationListeners_doesNotAffectPrimaryProfile() |
| 4055 | throws Exception { |
| 4056 | // Set up a managed profile |
| 4057 | final int MANAGED_PROFILE_USER_ID = 15; |
| 4058 | final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 19436); |
| 4059 | addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); |
| 4060 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 4061 | |
| 4062 | final String nonSystemPackage = "non.system.package"; |
| 4063 | int appId = 12345; |
| 4064 | setupPackageInPackageManager( |
| 4065 | nonSystemPackage, |
| 4066 | UserHandle.USER_SYSTEM, // We check the packageInfo from the primary user. |
| 4067 | appId, /*flags=*/ 0); |
| 4068 | |
| 4069 | final String systemListener = "system.package"; |
| 4070 | setupPackageInPackageManager( |
| 4071 | systemListener, |
| 4072 | UserHandle.USER_SYSTEM, // We check the packageInfo from the primary user. |
| 4073 | ++appId, ApplicationInfo.FLAG_SYSTEM); |
| 4074 | |
| 4075 | // By default all packages are allowed (for all profiles) |
| 4076 | assertNull(dpms.getPermittedCrossProfileNotificationListeners(admin1)); |
| 4077 | |
| 4078 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 4079 | assertTrue(dpms.isNotificationListenerServicePermitted( |
| 4080 | nonSystemPackage, MANAGED_PROFILE_USER_ID)); |
| 4081 | assertTrue(dpms.isNotificationListenerServicePermitted( |
| 4082 | systemListener, MANAGED_PROFILE_USER_ID)); |
| 4083 | assertTrue(dpms.isNotificationListenerServicePermitted( |
| 4084 | nonSystemPackage, UserHandle.USER_SYSTEM)); |
| 4085 | assertTrue(dpms.isNotificationListenerServicePermitted( |
| 4086 | systemListener, UserHandle.USER_SYSTEM)); |
| 4087 | |
| 4088 | // Setting an empty whitelist - only system listeners allowed in managed profile, but |
| 4089 | // all allowed in primary profile |
| 4090 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 4091 | assertTrue(dpms.setPermittedCrossProfileNotificationListeners( |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4092 | admin1, Collections.emptyList())); |
Esteban Talavera | 7e4cbad | 2017-03-30 17:59:50 +0100 | [diff] [blame] | 4093 | assertEquals(0, dpms.getPermittedCrossProfileNotificationListeners(admin1).size()); |
| 4094 | |
| 4095 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 4096 | assertFalse(dpms.isNotificationListenerServicePermitted( |
| 4097 | nonSystemPackage, MANAGED_PROFILE_USER_ID)); |
| 4098 | assertTrue(dpms.isNotificationListenerServicePermitted( |
| 4099 | systemListener, MANAGED_PROFILE_USER_ID)); |
| 4100 | assertTrue(dpms.isNotificationListenerServicePermitted( |
| 4101 | nonSystemPackage, UserHandle.USER_SYSTEM)); |
| 4102 | assertTrue(dpms.isNotificationListenerServicePermitted( |
| 4103 | systemListener, UserHandle.USER_SYSTEM)); |
| 4104 | } |
| 4105 | |
Bartosz Fabianowski | 05dc9f7 | 2017-02-22 23:41:14 +0100 | [diff] [blame] | 4106 | public void testGetOwnerInstalledCaCertsForDeviceOwner() throws Exception { |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4107 | mServiceContext.packageName = mRealTestContext.getPackageName(); |
| 4108 | mServiceContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 4109 | mAdmin1Context.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
Bartosz Fabianowski | 05dc9f7 | 2017-02-22 23:41:14 +0100 | [diff] [blame] | 4110 | setDeviceOwner(); |
| 4111 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4112 | verifyCanGetOwnerInstalledCaCerts(admin1, mAdmin1Context); |
Bartosz Fabianowski | 05dc9f7 | 2017-02-22 23:41:14 +0100 | [diff] [blame] | 4113 | } |
| 4114 | |
| 4115 | public void testGetOwnerInstalledCaCertsForProfileOwner() throws Exception { |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4116 | mServiceContext.packageName = mRealTestContext.getPackageName(); |
| 4117 | mServiceContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 4118 | mAdmin1Context.binder.callingUid = DpmMockContext.CALLER_UID; |
Bartosz Fabianowski | 05dc9f7 | 2017-02-22 23:41:14 +0100 | [diff] [blame] | 4119 | setAsProfileOwner(admin1); |
| 4120 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4121 | verifyCanGetOwnerInstalledCaCerts(admin1, mAdmin1Context); |
| 4122 | verifyCantGetOwnerInstalledCaCertsProfileOwnerRemoval(admin1, mAdmin1Context); |
Bartosz Fabianowski | 05dc9f7 | 2017-02-22 23:41:14 +0100 | [diff] [blame] | 4123 | } |
| 4124 | |
| 4125 | public void testGetOwnerInstalledCaCertsForDelegate() throws Exception { |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4126 | mServiceContext.packageName = mRealTestContext.getPackageName(); |
| 4127 | mServiceContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 4128 | mAdmin1Context.binder.callingUid = DpmMockContext.CALLER_UID; |
Bartosz Fabianowski | 05dc9f7 | 2017-02-22 23:41:14 +0100 | [diff] [blame] | 4129 | setAsProfileOwner(admin1); |
| 4130 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4131 | final DpmMockContext caller = new DpmMockContext(getServices(), mRealTestContext); |
| 4132 | caller.packageName = "com.example.delegate"; |
| 4133 | caller.binder.callingUid = setupPackageInPackageManager(caller.packageName, |
| 4134 | DpmMockContext.CALLER_USER_HANDLE, 20988, ApplicationInfo.FLAG_HAS_CODE); |
Bartosz Fabianowski | 05dc9f7 | 2017-02-22 23:41:14 +0100 | [diff] [blame] | 4135 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4136 | // Make caller a delegated cert installer. |
| 4137 | runAsCaller(mAdmin1Context, dpms, |
| 4138 | dpm -> dpm.setCertInstallerPackage(admin1, caller.packageName)); |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4139 | |
| 4140 | verifyCanGetOwnerInstalledCaCerts(null, caller); |
| 4141 | verifyCantGetOwnerInstalledCaCertsProfileOwnerRemoval(null, caller); |
Bartosz Fabianowski | 05dc9f7 | 2017-02-22 23:41:14 +0100 | [diff] [blame] | 4142 | } |
| 4143 | |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4144 | private void verifyCanGetOwnerInstalledCaCerts( |
| 4145 | final ComponentName caller, final DpmMockContext callerContext) throws Exception { |
Bartosz Fabianowski | 05dc9f7 | 2017-02-22 23:41:14 +0100 | [diff] [blame] | 4146 | final String alias = "cert"; |
| 4147 | final byte[] caCert = TEST_CA.getBytes(); |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4148 | |
| 4149 | // device admin (used for posting the tls notification) |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4150 | DpmMockContext admin1Context = mAdmin1Context; |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4151 | if (admin1.getPackageName().equals(callerContext.getPackageName())) { |
| 4152 | admin1Context = callerContext; |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4153 | } |
| 4154 | when(admin1Context.resources.getColor(anyInt(), anyObject())).thenReturn(Color.WHITE); |
| 4155 | |
| 4156 | // caller: device admin or delegated certificate installer |
| 4157 | callerContext.applicationInfo = new ApplicationInfo(); |
| 4158 | final UserHandle callerUser = callerContext.binder.getCallingUserHandle(); |
| 4159 | |
| 4160 | // system_server |
| 4161 | final DpmMockContext serviceContext = mContext; |
| 4162 | serviceContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4163 | getServices().addPackageContext(callerUser, admin1Context); |
| 4164 | getServices().addPackageContext(callerUser, callerContext); |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4165 | |
| 4166 | // Install a CA cert. |
| 4167 | runAsCaller(callerContext, dpms, (dpm) -> { |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4168 | when(getServices().keyChainConnection.getService().installCaCertificate(caCert)) |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4169 | .thenReturn(alias); |
| 4170 | assertTrue(dpm.installCaCert(caller, caCert)); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4171 | when(getServices().keyChainConnection.getService().getUserCaAliases()) |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4172 | .thenReturn(asSlice(new String[] {alias})); |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4173 | }); |
| 4174 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4175 | getServices().injectBroadcast(mServiceContext, new Intent(KeyChain.ACTION_TRUST_STORE_CHANGED) |
| 4176 | .putExtra(Intent.EXTRA_USER_HANDLE, callerUser.getIdentifier()), |
| 4177 | callerUser.getIdentifier()); |
Bartosz Fabianowski | 05dc9f7 | 2017-02-22 23:41:14 +0100 | [diff] [blame] | 4178 | flushTasks(); |
| 4179 | |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4180 | final List<String> ownerInstalledCaCerts = new ArrayList<>(); |
| 4181 | |
Bartosz Fabianowski | 05dc9f7 | 2017-02-22 23:41:14 +0100 | [diff] [blame] | 4182 | // Device Owner / Profile Owner can find out which CA certs were installed by itself. |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4183 | runAsCaller(admin1Context, dpms, (dpm) -> { |
| 4184 | final List<String> installedCaCerts = dpm.getOwnerInstalledCaCerts(callerUser); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4185 | assertEquals(Collections.singletonList(alias), installedCaCerts); |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4186 | ownerInstalledCaCerts.addAll(installedCaCerts); |
| 4187 | }); |
Bartosz Fabianowski | 05dc9f7 | 2017-02-22 23:41:14 +0100 | [diff] [blame] | 4188 | |
| 4189 | // Restarting the DPMS should not lose information. |
| 4190 | initializeDpms(); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4191 | runAsCaller(admin1Context, dpms, (dpm) -> |
| 4192 | assertEquals(ownerInstalledCaCerts, dpm.getOwnerInstalledCaCerts(callerUser))); |
Bartosz Fabianowski | 05dc9f7 | 2017-02-22 23:41:14 +0100 | [diff] [blame] | 4193 | |
| 4194 | // System can find out which CA certs were installed by the Device Owner / Profile Owner. |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4195 | runAsCaller(serviceContext, dpms, (dpm) -> { |
| 4196 | assertEquals(ownerInstalledCaCerts, dpm.getOwnerInstalledCaCerts(callerUser)); |
Bartosz Fabianowski | 05dc9f7 | 2017-02-22 23:41:14 +0100 | [diff] [blame] | 4197 | |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4198 | // Remove the CA cert. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4199 | reset(getServices().keyChainConnection.getService()); |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4200 | }); |
| 4201 | |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4202 | getServices().injectBroadcast(mServiceContext, new Intent(KeyChain.ACTION_TRUST_STORE_CHANGED) |
| 4203 | .putExtra(Intent.EXTRA_USER_HANDLE, callerUser.getIdentifier()), |
| 4204 | callerUser.getIdentifier()); |
Bartosz Fabianowski | 05dc9f7 | 2017-02-22 23:41:14 +0100 | [diff] [blame] | 4205 | flushTasks(); |
| 4206 | |
| 4207 | // Verify that the CA cert is no longer reported as installed by the Device Owner / Profile |
| 4208 | // Owner. |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4209 | runAsCaller(admin1Context, dpms, (dpm) -> { |
| 4210 | MoreAsserts.assertEmpty(dpm.getOwnerInstalledCaCerts(callerUser)); |
| 4211 | }); |
Bartosz Fabianowski | 05dc9f7 | 2017-02-22 23:41:14 +0100 | [diff] [blame] | 4212 | } |
| 4213 | |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4214 | private void verifyCantGetOwnerInstalledCaCertsProfileOwnerRemoval( |
| 4215 | final ComponentName callerName, final DpmMockContext callerContext) throws Exception { |
Bartosz Fabianowski | 05dc9f7 | 2017-02-22 23:41:14 +0100 | [diff] [blame] | 4216 | final String alias = "cert"; |
| 4217 | final byte[] caCert = TEST_CA.getBytes(); |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4218 | |
| 4219 | // device admin (used for posting the tls notification) |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4220 | DpmMockContext admin1Context = mAdmin1Context; |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4221 | if (admin1.getPackageName().equals(callerContext.getPackageName())) { |
| 4222 | admin1Context = callerContext; |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4223 | } |
| 4224 | when(admin1Context.resources.getColor(anyInt(), anyObject())).thenReturn(Color.WHITE); |
| 4225 | |
| 4226 | // caller: device admin or delegated certificate installer |
| 4227 | callerContext.applicationInfo = new ApplicationInfo(); |
| 4228 | final UserHandle callerUser = callerContext.binder.getCallingUserHandle(); |
| 4229 | |
| 4230 | // system_server |
| 4231 | final DpmMockContext serviceContext = mContext; |
| 4232 | serviceContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4233 | getServices().addPackageContext(callerUser, admin1Context); |
| 4234 | getServices().addPackageContext(callerUser, callerContext); |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4235 | |
| 4236 | // Install a CA cert as caller |
| 4237 | runAsCaller(callerContext, dpms, (dpm) -> { |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4238 | when(getServices().keyChainConnection.getService().installCaCertificate(caCert)) |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4239 | .thenReturn(alias); |
| 4240 | assertTrue(dpm.installCaCert(callerName, caCert)); |
| 4241 | }); |
| 4242 | |
| 4243 | // Fake the CA cert as having been installed |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4244 | when(getServices().keyChainConnection.getService().getUserCaAliases()) |
Bartosz Fabianowski | 05dc9f7 | 2017-02-22 23:41:14 +0100 | [diff] [blame] | 4245 | .thenReturn(asSlice(new String[] {alias})); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4246 | getServices().injectBroadcast(mServiceContext, new Intent(KeyChain.ACTION_TRUST_STORE_CHANGED) |
| 4247 | .putExtra(Intent.EXTRA_USER_HANDLE, callerUser.getIdentifier()), |
| 4248 | callerUser.getIdentifier()); |
Bartosz Fabianowski | 05dc9f7 | 2017-02-22 23:41:14 +0100 | [diff] [blame] | 4249 | flushTasks(); |
| 4250 | |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4251 | // Removing the Profile Owner should clear the information on which CA certs were installed |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4252 | runAsCaller(admin1Context, dpms, dpm -> dpm.clearProfileOwner(admin1)); |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4253 | |
| 4254 | runAsCaller(serviceContext, dpms, (dpm) -> { |
| 4255 | final List<String> ownerInstalledCaCerts = dpm.getOwnerInstalledCaCerts(callerUser); |
| 4256 | assertNotNull(ownerInstalledCaCerts); |
| 4257 | assertTrue(ownerInstalledCaCerts.isEmpty()); |
| 4258 | }); |
Bartosz Fabianowski | 05dc9f7 | 2017-02-22 23:41:14 +0100 | [diff] [blame] | 4259 | } |
| 4260 | |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 4261 | private void setUserSetupCompleteForUser(boolean isUserSetupComplete, int userhandle) { |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4262 | when(getServices().settings.settingsSecureGetIntForUser(Settings.Secure.USER_SETUP_COMPLETE, 0, |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 4263 | userhandle)).thenReturn(isUserSetupComplete ? 1 : 0); |
| 4264 | dpms.notifyChangeToContentObserver( |
| 4265 | Settings.Secure.getUriFor(Settings.Secure.USER_SETUP_COMPLETE), userhandle); |
| 4266 | } |
| 4267 | |
| 4268 | private void assertProvisioningAllowed(String action, boolean expected) { |
| 4269 | assertEquals("isProvisioningAllowed(" + action + ") returning unexpected result", expected, |
| 4270 | dpm.isProvisioningAllowed(action)); |
| 4271 | } |
Tony Mak | 2f26b79 | 2016-11-28 17:54:51 +0000 | [diff] [blame] | 4272 | |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 4273 | private void assertProvisioningAllowed(String action, boolean expected, String packageName, |
| 4274 | int uid) { |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4275 | final String previousPackageName = mContext.packageName; |
| 4276 | final int previousUid = mMockContext.binder.callingUid; |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 4277 | |
| 4278 | // Call assertProvisioningAllowed with the packageName / uid passed as arguments. |
| 4279 | mContext.packageName = packageName; |
| 4280 | mMockContext.binder.callingUid = uid; |
| 4281 | assertProvisioningAllowed(action, expected); |
| 4282 | |
| 4283 | // Set the previous package name / calling uid to go back to the initial state. |
| 4284 | mContext.packageName = previousPackageName; |
| 4285 | mMockContext.binder.callingUid = previousUid; |
| 4286 | } |
| 4287 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 4288 | private void assertCheckProvisioningPreCondition(String action, int provisioningCondition) { |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 4289 | assertCheckProvisioningPreCondition(action, admin1.getPackageName(), provisioningCondition); |
| 4290 | } |
| 4291 | |
| 4292 | private void assertCheckProvisioningPreCondition( |
| 4293 | String action, String packageName, int provisioningCondition) { |
| 4294 | assertEquals("checkProvisioningPreCondition(" |
| 4295 | + action + ", " + packageName + ") returning unexpected result", |
| 4296 | provisioningCondition, dpm.checkProvisioningPreCondition(action, packageName)); |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 4297 | } |
| 4298 | |
Tony Mak | 2f26b79 | 2016-11-28 17:54:51 +0000 | [diff] [blame] | 4299 | /** |
| 4300 | * Setup a managed profile with the specified admin and its uid. |
| 4301 | * @param admin ComponentName that's visible to the test code, which doesn't have to exist. |
| 4302 | * @param adminUid uid of the admin package. |
| 4303 | * @param copyFromAdmin package information for {@code admin} will be built based on this |
| 4304 | * component's information. |
| 4305 | */ |
| 4306 | private void addManagedProfile( |
| 4307 | ComponentName admin, int adminUid, ComponentName copyFromAdmin) throws Exception { |
| 4308 | final int userId = UserHandle.getUserId(adminUid); |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4309 | getServices().addUser(userId, UserInfo.FLAG_MANAGED_PROFILE, UserHandle.USER_SYSTEM); |
Tony Mak | 2f26b79 | 2016-11-28 17:54:51 +0000 | [diff] [blame] | 4310 | mContext.callerPermissions.addAll(OWNER_SETUP_PERMISSIONS); |
| 4311 | setUpPackageManagerForFakeAdmin(admin, adminUid, copyFromAdmin); |
| 4312 | dpm.setActiveAdmin(admin, false, userId); |
| 4313 | assertTrue(dpm.setProfileOwner(admin, null, userId)); |
| 4314 | mContext.callerPermissions.removeAll(OWNER_SETUP_PERMISSIONS); |
| 4315 | } |
Robin Lee | 7f5c91c | 2017-02-08 21:27:02 +0000 | [diff] [blame] | 4316 | |
| 4317 | /** |
Robin Lee | abaa069 | 2017-02-20 20:54:22 +0000 | [diff] [blame] | 4318 | * Convert String[] to StringParceledListSlice. |
Robin Lee | 7f5c91c | 2017-02-08 21:27:02 +0000 | [diff] [blame] | 4319 | */ |
Robin Lee | abaa069 | 2017-02-20 20:54:22 +0000 | [diff] [blame] | 4320 | private static StringParceledListSlice asSlice(String[] s) { |
| 4321 | return new StringParceledListSlice(Arrays.asList(s)); |
Robin Lee | 7f5c91c | 2017-02-08 21:27:02 +0000 | [diff] [blame] | 4322 | } |
Bartosz Fabianowski | 05dc9f7 | 2017-02-22 23:41:14 +0100 | [diff] [blame] | 4323 | |
| 4324 | private void flushTasks() throws Exception { |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4325 | dpms.mHandler.runWithScissors(() -> {}, 0 /*now*/); |
| 4326 | dpms.mBackgroundHandler.runWithScissors(() -> {}, 0 /*now*/); |
Bartosz Fabianowski | 05dc9f7 | 2017-02-22 23:41:14 +0100 | [diff] [blame] | 4327 | |
Robin Lee | 2c68dad | 2017-03-17 12:50:24 +0000 | [diff] [blame] | 4328 | // We can't let exceptions happen on the background thread. Throw them here if they happen |
| 4329 | // so they still cause the test to fail despite being suppressed. |
Pavel Grafov | 75c0a89 | 2017-05-18 17:28:27 +0100 | [diff] [blame] | 4330 | getServices().rethrowBackgroundBroadcastExceptions(); |
Bartosz Fabianowski | 05dc9f7 | 2017-02-22 23:41:14 +0100 | [diff] [blame] | 4331 | } |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 4332 | } |