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