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