1 /* 2 * Copyright (C) 2022 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 17 package com.android.server; 18 19 import static android.content.pm.UserInfo.FLAG_ADMIN; 20 import static android.content.pm.UserInfo.FLAG_MANAGED_PROFILE; 21 import static android.content.pm.UserInfo.FLAG_PRIMARY; 22 import static android.content.pm.UserInfo.FLAG_RESTRICTED; 23 24 import static org.mockito.ArgumentMatchers.anyInt; 25 import static org.mockito.ArgumentMatchers.anyString; 26 import static org.mockito.Mockito.doAnswer; 27 28 import android.content.pm.PackageManager; 29 import android.content.pm.UserInfo; 30 import android.os.Process; 31 import android.os.UserHandle; 32 import android.util.ArrayMap; 33 34 import java.util.Arrays; 35 import java.util.List; 36 import java.util.Map; 37 import java.util.stream.Collectors; 38 39 /** Common variables or methods shared between VpnTest and VpnManagerServiceTest. */ 40 public class VpnTestBase { 41 protected static final String TEST_VPN_PKG = "com.testvpn.vpn"; 42 /** 43 * Names and UIDs for some fake packages. Important points: 44 * - UID is ordered increasing. 45 * - One pair of packages have consecutive UIDs. 46 */ 47 protected static final String[] PKGS = {"com.example", "org.example", "net.example", "web.vpn"}; 48 protected static final int[] PKG_UIDS = {10066, 10077, 10078, 10400}; 49 // Mock packages 50 protected static final Map<String, Integer> sPackages = new ArrayMap<>(); 51 static { 52 for (int i = 0; i < PKGS.length; i++) { sPackages.put(PKGS[i], PKG_UIDS[i])53 sPackages.put(PKGS[i], PKG_UIDS[i]); 54 } sPackages.put(TEST_VPN_PKG, Process.myUid())55 sPackages.put(TEST_VPN_PKG, Process.myUid()); 56 } 57 58 // Mock users 59 protected static final int SYSTEM_USER_ID = 0; 60 protected static final UserInfo SYSTEM_USER = new UserInfo(0, "system", UserInfo.FLAG_PRIMARY); 61 protected static final UserInfo PRIMARY_USER = new UserInfo(27, "Primary", 62 FLAG_ADMIN | FLAG_PRIMARY); 63 protected static final UserInfo SECONDARY_USER = new UserInfo(15, "Secondary", FLAG_ADMIN); 64 protected static final UserInfo RESTRICTED_PROFILE_A = new UserInfo(40, "RestrictedA", 65 FLAG_RESTRICTED); 66 protected static final UserInfo RESTRICTED_PROFILE_B = new UserInfo(42, "RestrictedB", 67 FLAG_RESTRICTED); 68 protected static final UserInfo MANAGED_PROFILE_A = new UserInfo(45, "ManagedA", 69 FLAG_MANAGED_PROFILE); 70 static { 71 RESTRICTED_PROFILE_A.restrictedProfileParentId = PRIMARY_USER.id; 72 RESTRICTED_PROFILE_B.restrictedProfileParentId = SECONDARY_USER.id; 73 MANAGED_PROFILE_A.profileGroupId = PRIMARY_USER.id; 74 } 75 76 // Populate a fake packageName-to-UID mapping. setMockedPackages(PackageManager mockPm, final Map<String, Integer> packages)77 protected void setMockedPackages(PackageManager mockPm, final Map<String, Integer> packages) { 78 try { 79 doAnswer(invocation -> { 80 final String appName = (String) invocation.getArguments()[0]; 81 final int userId = (int) invocation.getArguments()[1]; 82 83 final Integer appId = packages.get(appName); 84 if (appId == null) { 85 throw new PackageManager.NameNotFoundException(appName); 86 } 87 88 return UserHandle.getUid(userId, appId); 89 }).when(mockPm).getPackageUidAsUser(anyString(), anyInt()); 90 } catch (Exception e) { 91 } 92 } 93 toList(int[] arr)94 protected List<Integer> toList(int[] arr) { 95 return Arrays.stream(arr).boxed().collect(Collectors.toList()); 96 } 97 } 98