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