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