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