blob: 4f18da7056efae24166de5f105da9a981cb3d997 [file] [log] [blame]
Robin Lee4d03abc2016-05-09 12:32:27 +01001/*
2 * Copyright (C) 2016 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 */
16
17package com.android.server.connectivity;
18
19import static android.content.pm.UserInfo.FLAG_ADMIN;
20import static android.content.pm.UserInfo.FLAG_MANAGED_PROFILE;
21import static android.content.pm.UserInfo.FLAG_PRIMARY;
22import static android.content.pm.UserInfo.FLAG_RESTRICTED;
Robin Lee17e61832016-05-09 13:46:28 +010023import static org.mockito.AdditionalMatchers.*;
Robin Lee4d03abc2016-05-09 12:32:27 +010024import static org.mockito.Mockito.*;
25
26import android.annotation.UserIdInt;
Robin Lee17e61832016-05-09 13:46:28 +010027import android.app.AppOpsManager;
Tony Makde7f7d12016-06-30 11:19:20 +010028import android.app.NotificationManager;
Robin Lee4d03abc2016-05-09 12:32:27 +010029import android.content.Context;
Robin Leeb8c2a2b2017-03-10 16:17:06 +000030import android.content.pm.ApplicationInfo;
Robin Lee4d03abc2016-05-09 12:32:27 +010031import android.content.pm.PackageManager;
Charles He36738632017-05-15 17:07:18 +010032import android.content.pm.ResolveInfo;
33import android.content.pm.ServiceInfo;
Robin Lee4d03abc2016-05-09 12:32:27 +010034import android.content.pm.UserInfo;
Charles He3da6a1f2017-08-16 13:14:13 +010035import android.content.res.Resources;
Tony Makde7f7d12016-06-30 11:19:20 +010036import android.net.NetworkInfo.DetailedState;
Robin Lee4d03abc2016-05-09 12:32:27 +010037import android.net.UidRange;
Charles He36738632017-05-15 17:07:18 +010038import android.net.VpnService;
39import android.os.Build.VERSION_CODES;
40import android.os.Bundle;
Robin Lee4d03abc2016-05-09 12:32:27 +010041import android.os.INetworkManagementService;
42import android.os.Looper;
43import android.os.UserHandle;
44import android.os.UserManager;
45import android.test.AndroidTestCase;
46import android.test.suitebuilder.annotation.SmallTest;
47import android.util.ArrayMap;
48import android.util.ArraySet;
49
Charles He3da6a1f2017-08-16 13:14:13 +010050import com.android.internal.R;
Robin Leec3736bc2017-03-10 16:19:54 +000051import com.android.internal.net.VpnConfig;
52
Robin Leeb8c2a2b2017-03-10 16:17:06 +000053import org.mockito.Answers;
Tony Makde7f7d12016-06-30 11:19:20 +010054import org.mockito.InOrder;
Robin Lee4d03abc2016-05-09 12:32:27 +010055import org.mockito.Mock;
56import org.mockito.MockitoAnnotations;
57
Charles He36738632017-05-15 17:07:18 +010058import java.util.ArrayList;
59import java.util.Arrays;
60import java.util.Collections;
61import java.util.Map;
62import java.util.Set;
63
Robin Lee4d03abc2016-05-09 12:32:27 +010064/**
65 * Tests for {@link Vpn}.
66 *
67 * Build, install and run with:
Charles He36738632017-05-15 17:07:18 +010068 * runtest --path java/com/android/server/connectivity/VpnTest.java
Robin Lee4d03abc2016-05-09 12:32:27 +010069 */
70public class VpnTest extends AndroidTestCase {
71 private static final String TAG = "VpnTest";
72
73 // Mock users
74 static final UserInfo primaryUser = new UserInfo(27, "Primary", FLAG_ADMIN | FLAG_PRIMARY);
75 static final UserInfo secondaryUser = new UserInfo(15, "Secondary", FLAG_ADMIN);
76 static final UserInfo restrictedProfileA = new UserInfo(40, "RestrictedA", FLAG_RESTRICTED);
77 static final UserInfo restrictedProfileB = new UserInfo(42, "RestrictedB", FLAG_RESTRICTED);
78 static final UserInfo managedProfileA = new UserInfo(45, "ManagedA", FLAG_MANAGED_PROFILE);
79 static {
80 restrictedProfileA.restrictedProfileParentId = primaryUser.id;
81 restrictedProfileB.restrictedProfileParentId = secondaryUser.id;
82 managedProfileA.profileGroupId = primaryUser.id;
83 }
84
Robin Lee17e61832016-05-09 13:46:28 +010085 /**
86 * Names and UIDs for some fake packages. Important points:
87 * - UID is ordered increasing.
88 * - One pair of packages have consecutive UIDs.
89 */
90 static final String[] PKGS = {"com.example", "org.example", "net.example", "web.vpn"};
91 static final int[] PKG_UIDS = {66, 77, 78, 400};
92
93 // Mock packages
94 static final Map<String, Integer> mPackages = new ArrayMap<>();
95 static {
96 for (int i = 0; i < PKGS.length; i++) {
97 mPackages.put(PKGS[i], PKG_UIDS[i]);
98 }
99 }
100
Robin Leeb8c2a2b2017-03-10 16:17:06 +0000101 @Mock(answer = Answers.RETURNS_DEEP_STUBS) private Context mContext;
Robin Lee4d03abc2016-05-09 12:32:27 +0100102 @Mock private UserManager mUserManager;
103 @Mock private PackageManager mPackageManager;
104 @Mock private INetworkManagementService mNetService;
Robin Lee17e61832016-05-09 13:46:28 +0100105 @Mock private AppOpsManager mAppOps;
Tony Makde7f7d12016-06-30 11:19:20 +0100106 @Mock private NotificationManager mNotificationManager;
Robin Leeb8c2a2b2017-03-10 16:17:06 +0000107 @Mock private Vpn.SystemServices mSystemServices;
Robin Lee4d03abc2016-05-09 12:32:27 +0100108
109 @Override
110 public void setUp() throws Exception {
111 MockitoAnnotations.initMocks(this);
Robin Leec3736bc2017-03-10 16:19:54 +0000112
Robin Lee4d03abc2016-05-09 12:32:27 +0100113 when(mContext.getPackageManager()).thenReturn(mPackageManager);
Robin Lee17e61832016-05-09 13:46:28 +0100114 setMockedPackages(mPackages);
Robin Leec3736bc2017-03-10 16:19:54 +0000115
Tony Makde7f7d12016-06-30 11:19:20 +0100116 when(mContext.getPackageName()).thenReturn(Vpn.class.getPackage().getName());
Robin Lee4d03abc2016-05-09 12:32:27 +0100117 when(mContext.getSystemService(eq(Context.USER_SERVICE))).thenReturn(mUserManager);
Robin Lee17e61832016-05-09 13:46:28 +0100118 when(mContext.getSystemService(eq(Context.APP_OPS_SERVICE))).thenReturn(mAppOps);
Tony Makde7f7d12016-06-30 11:19:20 +0100119 when(mContext.getSystemService(eq(Context.NOTIFICATION_SERVICE)))
120 .thenReturn(mNotificationManager);
Charles He3da6a1f2017-08-16 13:14:13 +0100121 when(mContext.getString(R.string.config_customVpnAlwaysOnDisconnectedDialogComponent))
122 .thenReturn(Resources.getSystem().getString(
123 R.string.config_customVpnAlwaysOnDisconnectedDialogComponent));
Robin Leeb8c2a2b2017-03-10 16:17:06 +0000124
125 // Used by {@link Notification.Builder}
126 ApplicationInfo applicationInfo = new ApplicationInfo();
Charles He36738632017-05-15 17:07:18 +0100127 applicationInfo.targetSdkVersion = VERSION_CODES.CUR_DEVELOPMENT;
Robin Leeb8c2a2b2017-03-10 16:17:06 +0000128 when(mContext.getApplicationInfo()).thenReturn(applicationInfo);
129
Robin Lee4d03abc2016-05-09 12:32:27 +0100130 doNothing().when(mNetService).registerObserver(any());
131 }
132
133 @SmallTest
134 public void testRestrictedProfilesAreAddedToVpn() {
135 setMockedUsers(primaryUser, secondaryUser, restrictedProfileA, restrictedProfileB);
136
Robin Leeb8c2a2b2017-03-10 16:17:06 +0000137 final Vpn vpn = createVpn(primaryUser.id);
Robin Lee4d03abc2016-05-09 12:32:27 +0100138 final Set<UidRange> ranges = vpn.createUserAndRestrictedProfilesRanges(primaryUser.id,
139 null, null);
140
141 assertEquals(new ArraySet<>(Arrays.asList(new UidRange[] {
142 UidRange.createForUser(primaryUser.id),
143 UidRange.createForUser(restrictedProfileA.id)
144 })), ranges);
145 }
146
147 @SmallTest
148 public void testManagedProfilesAreNotAddedToVpn() {
149 setMockedUsers(primaryUser, managedProfileA);
150
Robin Leeb8c2a2b2017-03-10 16:17:06 +0000151 final Vpn vpn = createVpn(primaryUser.id);
Robin Lee4d03abc2016-05-09 12:32:27 +0100152 final Set<UidRange> ranges = vpn.createUserAndRestrictedProfilesRanges(primaryUser.id,
153 null, null);
154
155 assertEquals(new ArraySet<>(Arrays.asList(new UidRange[] {
156 UidRange.createForUser(primaryUser.id)
157 })), ranges);
158 }
159
160 @SmallTest
161 public void testAddUserToVpnOnlyAddsOneUser() {
162 setMockedUsers(primaryUser, restrictedProfileA, managedProfileA);
163
Robin Leeb8c2a2b2017-03-10 16:17:06 +0000164 final Vpn vpn = createVpn(primaryUser.id);
Robin Lee4d03abc2016-05-09 12:32:27 +0100165 final Set<UidRange> ranges = new ArraySet<>();
166 vpn.addUserToRanges(ranges, primaryUser.id, null, null);
167
168 assertEquals(new ArraySet<>(Arrays.asList(new UidRange[] {
169 UidRange.createForUser(primaryUser.id)
170 })), ranges);
171 }
172
173 @SmallTest
174 public void testUidWhiteAndBlacklist() throws Exception {
Robin Leeb8c2a2b2017-03-10 16:17:06 +0000175 final Vpn vpn = createVpn(primaryUser.id);
Robin Lee4d03abc2016-05-09 12:32:27 +0100176 final UidRange user = UidRange.createForUser(primaryUser.id);
Robin Lee17e61832016-05-09 13:46:28 +0100177 final String[] packages = {PKGS[0], PKGS[1], PKGS[2]};
Robin Lee4d03abc2016-05-09 12:32:27 +0100178
179 // Whitelist
180 final Set<UidRange> allow = vpn.createUserAndRestrictedProfilesRanges(primaryUser.id,
Robin Lee17e61832016-05-09 13:46:28 +0100181 Arrays.asList(packages), null);
Robin Lee4d03abc2016-05-09 12:32:27 +0100182 assertEquals(new ArraySet<>(Arrays.asList(new UidRange[] {
Robin Lee17e61832016-05-09 13:46:28 +0100183 new UidRange(user.start + PKG_UIDS[0], user.start + PKG_UIDS[0]),
184 new UidRange(user.start + PKG_UIDS[1], user.start + PKG_UIDS[2])
Robin Lee4d03abc2016-05-09 12:32:27 +0100185 })), allow);
186
187 // Blacklist
188 final Set<UidRange> disallow = vpn.createUserAndRestrictedProfilesRanges(primaryUser.id,
Robin Lee17e61832016-05-09 13:46:28 +0100189 null, Arrays.asList(packages));
Robin Lee4d03abc2016-05-09 12:32:27 +0100190 assertEquals(new ArraySet<>(Arrays.asList(new UidRange[] {
Robin Lee17e61832016-05-09 13:46:28 +0100191 new UidRange(user.start, user.start + PKG_UIDS[0] - 1),
192 new UidRange(user.start + PKG_UIDS[0] + 1, user.start + PKG_UIDS[1] - 1),
193 /* Empty range between UIDS[1] and UIDS[2], should be excluded, */
194 new UidRange(user.start + PKG_UIDS[2] + 1, user.stop)
Robin Lee4d03abc2016-05-09 12:32:27 +0100195 })), disallow);
196 }
197
Robin Lee17e61832016-05-09 13:46:28 +0100198 @SmallTest
199 public void testLockdownChangingPackage() throws Exception {
Robin Leeb8c2a2b2017-03-10 16:17:06 +0000200 final Vpn vpn = createVpn(primaryUser.id);
Robin Lee17e61832016-05-09 13:46:28 +0100201 final UidRange user = UidRange.createForUser(primaryUser.id);
202
203 // Default state.
Tony Makde7f7d12016-06-30 11:19:20 +0100204 assertUnblocked(vpn, user.start + PKG_UIDS[0], user.start + PKG_UIDS[1], user.start + PKG_UIDS[2], user.start + PKG_UIDS[3]);
Robin Lee17e61832016-05-09 13:46:28 +0100205
206 // Set always-on without lockdown.
207 assertTrue(vpn.setAlwaysOnPackage(PKGS[1], false));
Tony Makde7f7d12016-06-30 11:19:20 +0100208 assertUnblocked(vpn, user.start + PKG_UIDS[0], user.start + PKG_UIDS[1], user.start + PKG_UIDS[2], user.start + PKG_UIDS[3]);
Robin Lee17e61832016-05-09 13:46:28 +0100209
210 // Set always-on with lockdown.
211 assertTrue(vpn.setAlwaysOnPackage(PKGS[1], true));
212 verify(mNetService).setAllowOnlyVpnForUids(eq(true), aryEq(new UidRange[] {
213 new UidRange(user.start, user.start + PKG_UIDS[1] - 1),
214 new UidRange(user.start + PKG_UIDS[1] + 1, user.stop)
215 }));
Tony Makde7f7d12016-06-30 11:19:20 +0100216 assertBlocked(vpn, user.start + PKG_UIDS[0], user.start + PKG_UIDS[2], user.start + PKG_UIDS[3]);
217 assertUnblocked(vpn, user.start + PKG_UIDS[1]);
Robin Lee17e61832016-05-09 13:46:28 +0100218
219 // Switch to another app.
220 assertTrue(vpn.setAlwaysOnPackage(PKGS[3], true));
221 verify(mNetService).setAllowOnlyVpnForUids(eq(false), aryEq(new UidRange[] {
222 new UidRange(user.start, user.start + PKG_UIDS[1] - 1),
223 new UidRange(user.start + PKG_UIDS[1] + 1, user.stop)
224 }));
225 verify(mNetService).setAllowOnlyVpnForUids(eq(true), aryEq(new UidRange[] {
226 new UidRange(user.start, user.start + PKG_UIDS[3] - 1),
227 new UidRange(user.start + PKG_UIDS[3] + 1, user.stop)
228 }));
Tony Makde7f7d12016-06-30 11:19:20 +0100229 assertBlocked(vpn, user.start + PKG_UIDS[0], user.start + PKG_UIDS[1], user.start + PKG_UIDS[2]);
230 assertUnblocked(vpn, user.start + PKG_UIDS[3]);
Robin Lee17e61832016-05-09 13:46:28 +0100231 }
232
233 @SmallTest
234 public void testLockdownAddingAProfile() throws Exception {
Robin Leeb8c2a2b2017-03-10 16:17:06 +0000235 final Vpn vpn = createVpn(primaryUser.id);
Robin Lee17e61832016-05-09 13:46:28 +0100236 setMockedUsers(primaryUser);
237
238 // Make a copy of the restricted profile, as we're going to mark it deleted halfway through.
239 final UserInfo tempProfile = new UserInfo(restrictedProfileA.id, restrictedProfileA.name,
240 restrictedProfileA.flags);
241 tempProfile.restrictedProfileParentId = primaryUser.id;
242
243 final UidRange user = UidRange.createForUser(primaryUser.id);
244 final UidRange profile = UidRange.createForUser(tempProfile.id);
245
246 // Set lockdown.
247 assertTrue(vpn.setAlwaysOnPackage(PKGS[3], true));
248 verify(mNetService).setAllowOnlyVpnForUids(eq(true), aryEq(new UidRange[] {
249 new UidRange(user.start, user.start + PKG_UIDS[3] - 1),
250 new UidRange(user.start + PKG_UIDS[3] + 1, user.stop)
251 }));
252
253 // Verify restricted user isn't affected at first.
Tony Makde7f7d12016-06-30 11:19:20 +0100254 assertUnblocked(vpn, profile.start + PKG_UIDS[0]);
Robin Lee17e61832016-05-09 13:46:28 +0100255
256 // Add the restricted user.
257 setMockedUsers(primaryUser, tempProfile);
258 vpn.onUserAdded(tempProfile.id);
259 verify(mNetService).setAllowOnlyVpnForUids(eq(true), aryEq(new UidRange[] {
260 new UidRange(profile.start, profile.start + PKG_UIDS[3] - 1),
261 new UidRange(profile.start + PKG_UIDS[3] + 1, profile.stop)
262 }));
263
264 // Remove the restricted user.
265 tempProfile.partial = true;
266 vpn.onUserRemoved(tempProfile.id);
267 verify(mNetService).setAllowOnlyVpnForUids(eq(false), aryEq(new UidRange[] {
268 new UidRange(profile.start, profile.start + PKG_UIDS[3] - 1),
269 new UidRange(profile.start + PKG_UIDS[3] + 1, profile.stop)
270 }));
271 }
272
Tony Makde7f7d12016-06-30 11:19:20 +0100273 @SmallTest
Robin Leec3736bc2017-03-10 16:19:54 +0000274 public void testLockdownRuleRepeatability() throws Exception {
275 final Vpn vpn = createVpn(primaryUser.id);
276
277 // Given legacy lockdown is already enabled,
278 vpn.setLockdown(true);
279 verify(mNetService, times(1)).setAllowOnlyVpnForUids(
280 eq(true), aryEq(new UidRange[] {UidRange.createForUser(primaryUser.id)}));
281
282 // Enabling legacy lockdown twice should do nothing.
283 vpn.setLockdown(true);
284 verify(mNetService, times(1)).setAllowOnlyVpnForUids(anyBoolean(), any(UidRange[].class));
285
286 // And disabling should remove the rules exactly once.
287 vpn.setLockdown(false);
288 verify(mNetService, times(1)).setAllowOnlyVpnForUids(
289 eq(false), aryEq(new UidRange[] {UidRange.createForUser(primaryUser.id)}));
290
291 // Removing the lockdown again should have no effect.
292 vpn.setLockdown(false);
293 verify(mNetService, times(2)).setAllowOnlyVpnForUids(anyBoolean(), any(UidRange[].class));
294 }
295
296 @SmallTest
297 public void testLockdownRuleReversibility() throws Exception {
298 final Vpn vpn = createVpn(primaryUser.id);
299
300 final UidRange[] entireUser = {
301 UidRange.createForUser(primaryUser.id)
302 };
303 final UidRange[] exceptPkg0 = {
304 new UidRange(entireUser[0].start, entireUser[0].start + PKG_UIDS[0] - 1),
305 new UidRange(entireUser[0].start + PKG_UIDS[0] + 1, entireUser[0].stop)
306 };
307
308 final InOrder order = inOrder(mNetService);
309
310 // Given lockdown is enabled with no package (legacy VPN),
311 vpn.setLockdown(true);
312 order.verify(mNetService).setAllowOnlyVpnForUids(eq(true), aryEq(entireUser));
313
314 // When a new VPN package is set the rules should change to cover that package.
315 vpn.prepare(null, PKGS[0]);
316 order.verify(mNetService).setAllowOnlyVpnForUids(eq(false), aryEq(entireUser));
317 order.verify(mNetService).setAllowOnlyVpnForUids(eq(true), aryEq(exceptPkg0));
318
319 // When that VPN package is unset, everything should be undone again in reverse.
320 vpn.prepare(null, VpnConfig.LEGACY_VPN);
321 order.verify(mNetService).setAllowOnlyVpnForUids(eq(false), aryEq(exceptPkg0));
322 order.verify(mNetService).setAllowOnlyVpnForUids(eq(true), aryEq(entireUser));
323 }
324
325 @SmallTest
Charles He36738632017-05-15 17:07:18 +0100326 public void testIsAlwaysOnPackageSupported() throws Exception {
327 final Vpn vpn = createVpn(primaryUser.id);
328
329 ApplicationInfo appInfo = new ApplicationInfo();
330 when(mPackageManager.getApplicationInfoAsUser(eq(PKGS[0]), anyInt(), eq(primaryUser.id)))
331 .thenReturn(appInfo);
332
333 ServiceInfo svcInfo = new ServiceInfo();
334 ResolveInfo resInfo = new ResolveInfo();
335 resInfo.serviceInfo = svcInfo;
336 when(mPackageManager.queryIntentServicesAsUser(any(), eq(PackageManager.GET_META_DATA),
337 eq(primaryUser.id)))
338 .thenReturn(Collections.singletonList(resInfo));
339
340 // null package name should return false
341 assertFalse(vpn.isAlwaysOnPackageSupported(null));
342
343 // Pre-N apps are not supported
344 appInfo.targetSdkVersion = VERSION_CODES.M;
345 assertFalse(vpn.isAlwaysOnPackageSupported(PKGS[0]));
346
347 // N+ apps are supported by default
348 appInfo.targetSdkVersion = VERSION_CODES.N;
349 assertTrue(vpn.isAlwaysOnPackageSupported(PKGS[0]));
350
351 // Apps that opt out explicitly are not supported
352 appInfo.targetSdkVersion = VERSION_CODES.CUR_DEVELOPMENT;
353 Bundle metaData = new Bundle();
Charles Hec57a01c2017-08-15 15:30:22 +0100354 metaData.putBoolean(VpnService.SERVICE_META_DATA_SUPPORTS_ALWAYS_ON, false);
Charles He36738632017-05-15 17:07:18 +0100355 svcInfo.metaData = metaData;
356 assertFalse(vpn.isAlwaysOnPackageSupported(PKGS[0]));
357 }
358
359 @SmallTest
Tony Makde7f7d12016-06-30 11:19:20 +0100360 public void testNotificationShownForAlwaysOnApp() {
Robin Leeb8c2a2b2017-03-10 16:17:06 +0000361 final UserHandle userHandle = UserHandle.of(primaryUser.id);
362 final Vpn vpn = createVpn(primaryUser.id);
Tony Makde7f7d12016-06-30 11:19:20 +0100363 setMockedUsers(primaryUser);
364
Robin Leeb8c2a2b2017-03-10 16:17:06 +0000365 final InOrder order = inOrder(mNotificationManager);
366
Tony Makde7f7d12016-06-30 11:19:20 +0100367 // Don't show a notification for regular disconnected states.
368 vpn.updateState(DetailedState.DISCONNECTED, TAG);
Robin Leeb8c2a2b2017-03-10 16:17:06 +0000369 order.verify(mNotificationManager, atLeastOnce())
370 .cancelAsUser(anyString(), anyInt(), eq(userHandle));
Tony Makde7f7d12016-06-30 11:19:20 +0100371
372 // Start showing a notification for disconnected once always-on.
373 vpn.setAlwaysOnPackage(PKGS[0], false);
Robin Leeb8c2a2b2017-03-10 16:17:06 +0000374 order.verify(mNotificationManager)
375 .notifyAsUser(anyString(), anyInt(), any(), eq(userHandle));
Tony Makde7f7d12016-06-30 11:19:20 +0100376
377 // Stop showing the notification once connected.
378 vpn.updateState(DetailedState.CONNECTED, TAG);
Robin Leeb8c2a2b2017-03-10 16:17:06 +0000379 order.verify(mNotificationManager).cancelAsUser(anyString(), anyInt(), eq(userHandle));
Tony Makde7f7d12016-06-30 11:19:20 +0100380
381 // Show the notification if we disconnect again.
382 vpn.updateState(DetailedState.DISCONNECTED, TAG);
Robin Leeb8c2a2b2017-03-10 16:17:06 +0000383 order.verify(mNotificationManager)
384 .notifyAsUser(anyString(), anyInt(), any(), eq(userHandle));
Tony Makde7f7d12016-06-30 11:19:20 +0100385
386 // Notification should be cleared after unsetting always-on package.
387 vpn.setAlwaysOnPackage(null, false);
Robin Leeb8c2a2b2017-03-10 16:17:06 +0000388 order.verify(mNotificationManager).cancelAsUser(anyString(), anyInt(), eq(userHandle));
Tony Makde7f7d12016-06-30 11:19:20 +0100389 }
390
Robin Lee4d03abc2016-05-09 12:32:27 +0100391 /**
Tony Makde7f7d12016-06-30 11:19:20 +0100392 * Mock some methods of vpn object.
Robin Lee4d03abc2016-05-09 12:32:27 +0100393 */
Robin Leeb8c2a2b2017-03-10 16:17:06 +0000394 private Vpn createVpn(@UserIdInt int userId) {
395 return new Vpn(Looper.myLooper(), mContext, mNetService, userId, mSystemServices);
Tony Makde7f7d12016-06-30 11:19:20 +0100396 }
Robin Lee17e61832016-05-09 13:46:28 +0100397
Tony Makde7f7d12016-06-30 11:19:20 +0100398 private static void assertBlocked(Vpn vpn, int... uids) {
399 for (int uid : uids) {
400 assertTrue("Uid " + uid + " should be blocked", vpn.isBlockingUid(uid));
401 }
402 }
403
404 private static void assertUnblocked(Vpn vpn, int... uids) {
405 for (int uid : uids) {
406 assertFalse("Uid " + uid + " should not be blocked", vpn.isBlockingUid(uid));
Robin Lee17e61832016-05-09 13:46:28 +0100407 }
Robin Lee4d03abc2016-05-09 12:32:27 +0100408 }
409
410 /**
411 * Populate {@link #mUserManager} with a list of fake users.
412 */
413 private void setMockedUsers(UserInfo... users) {
414 final Map<Integer, UserInfo> userMap = new ArrayMap<>();
415 for (UserInfo user : users) {
416 userMap.put(user.id, user);
417 }
418
Robin Lee17e61832016-05-09 13:46:28 +0100419 /**
420 * @see UserManagerService#getUsers(boolean)
421 */
Robin Lee4d03abc2016-05-09 12:32:27 +0100422 doAnswer(invocation -> {
Robin Lee17e61832016-05-09 13:46:28 +0100423 final boolean excludeDying = (boolean) invocation.getArguments()[0];
424 final ArrayList<UserInfo> result = new ArrayList<>(users.length);
425 for (UserInfo ui : users) {
426 if (!excludeDying || (ui.isEnabled() && !ui.partial)) {
427 result.add(ui);
428 }
429 }
430 return result;
431 }).when(mUserManager).getUsers(anyBoolean());
Robin Lee4d03abc2016-05-09 12:32:27 +0100432
433 doAnswer(invocation -> {
434 final int id = (int) invocation.getArguments()[0];
435 return userMap.get(id);
436 }).when(mUserManager).getUserInfo(anyInt());
437
438 doAnswer(invocation -> {
439 final int id = (int) invocation.getArguments()[0];
440 return (userMap.get(id).flags & UserInfo.FLAG_ADMIN) != 0;
441 }).when(mUserManager).canHaveRestrictedProfile(anyInt());
442 }
443
444 /**
445 * Populate {@link #mPackageManager} with a fake packageName-to-UID mapping.
446 */
447 private void setMockedPackages(final Map<String, Integer> packages) {
448 try {
449 doAnswer(invocation -> {
450 final String appName = (String) invocation.getArguments()[0];
451 final int userId = (int) invocation.getArguments()[1];
452 return UserHandle.getUid(userId, packages.get(appName));
453 }).when(mPackageManager).getPackageUidAsUser(anyString(), anyInt());
454 } catch (Exception e) {
455 }
456 }
457}