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