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