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