• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.sdksandbox.helpers;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.junit.Assert.assertThrows;
22 
23 import android.Manifest;
24 import android.content.Context;
25 import android.content.pm.ApplicationInfo;
26 import android.content.pm.PackageManager;
27 import android.content.pm.SharedLibraryInfo;
28 import android.os.Process;
29 
30 import androidx.test.platform.app.InstrumentationRegistry;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.List;
38 import java.util.stream.Collectors;
39 
40 public class PackageManagerHelperUnitTest {
41     private static final String TEST_PACKAGE = "com.android.server.sdksandbox.tests";
42     private static final ArrayList<String> SDK_NAMES =
43             new ArrayList<>(
44                     Arrays.asList(
45                             "com.android.codeprovider",
46                             "com.android.codeproviderresources",
47                             "com.android.property_sdkprovider_classname_not_present"));
48     private static final ArrayList<String> SDK_PACKAGE_NAMES =
49             new ArrayList<>(
50                     Arrays.asList(
51                             "com.android.codeprovider_1",
52                             "com.android.codeproviderresources_1",
53                             "com.android.property_sdkprovider_classname_not_present_1"));
54     private PackageManagerHelper mPackageManagerHelper;
55     private int mClientAppUid;
56 
57     @Before
setUp()58     public void setUp() {
59         Context context = InstrumentationRegistry.getInstrumentation().getContext();
60         mClientAppUid = Process.myUid();
61         mPackageManagerHelper = new PackageManagerHelper(context, mClientAppUid);
62     }
63 
64     @Test
testSdkSharedLibraryInfo()65     public void testSdkSharedLibraryInfo() throws Exception {
66         List<SharedLibraryInfo> sharedLibraryInfos =
67                 mPackageManagerHelper.getSdkSharedLibraryInfo(TEST_PACKAGE);
68         assertThat(sharedLibraryInfos.size()).isEqualTo(3);
69 
70         List<String> sdks =
71                 sharedLibraryInfos.stream()
72                         .map(sharedLibrary -> sharedLibrary.getName())
73                         .collect(Collectors.toList());
74 
75         assertThat(sdks).containsExactlyElementsIn(SDK_NAMES);
76     }
77 
78     @Test
testGetSdkSharedLibraryInfoForSdk()79     public void testGetSdkSharedLibraryInfoForSdk() throws Exception {
80         SharedLibraryInfo sharedLibraryInfo =
81                 mPackageManagerHelper.getSdkSharedLibraryInfoForSdk(TEST_PACKAGE, SDK_NAMES.get(0));
82         assertThat(sharedLibraryInfo.getDeclaringPackage().getPackageName())
83                 .isEqualTo(SDK_PACKAGE_NAMES.get(0));
84     }
85 
86     @Test
testGetProperty()87     public void testGetProperty() throws Exception {
88         String propertyName = "android.sdksandbox.PROPERTY_SDK_PROVIDER_CLASS_NAME";
89         assertThat(
90                         mPackageManagerHelper
91                                 .getProperty(
92                                         propertyName, /* packageName= */ SDK_PACKAGE_NAMES.get(0))
93                                 .getString())
94                 .isEqualTo("test.class.name");
95     }
96 
97     @Test
testGetApplicationInfoForSharedLibrary()98     public void testGetApplicationInfoForSharedLibrary() throws Exception {
99 
100         try {
101             InstrumentationRegistry.getInstrumentation()
102                     .getUiAutomation()
103                     .adoptShellPermissionIdentity(Manifest.permission.INTERACT_ACROSS_USERS_FULL);
104 
105             SharedLibraryInfo sharedLibraryInfo =
106                     mPackageManagerHelper.getSdkSharedLibraryInfoForSdk(
107                             TEST_PACKAGE, SDK_NAMES.get(0));
108             ApplicationInfo applicationInfo =
109                     mPackageManagerHelper.getApplicationInfoForSharedLibrary(
110                             sharedLibraryInfo,
111                             /* flags= */ PackageManager.MATCH_STATIC_SHARED_AND_SDK_LIBRARIES
112                                     | PackageManager.MATCH_ANY_USER);
113             assertThat(applicationInfo.packageName).isEqualTo(SDK_PACKAGE_NAMES.get(0));
114         } finally {
115             InstrumentationRegistry.getInstrumentation()
116                     .getUiAutomation()
117                     .dropShellPermissionIdentity();
118         }
119     }
120 
121     @Test
testGetPackageNamesForUid()122     public void testGetPackageNamesForUid() throws Exception {
123         List<String> packageNames = mPackageManagerHelper.getPackageNamesForUid(mClientAppUid);
124         assertThat(packageNames.size()).isEqualTo(1);
125         assertThat(packageNames.get(0)).isEqualTo(TEST_PACKAGE);
126     }
127 
128     @Test
testGetPackageNamesForUid_invalidUid()129     public void testGetPackageNamesForUid_invalidUid() throws Exception {
130         PackageManager.NameNotFoundException thrown =
131                 assertThrows(
132                         PackageManager.NameNotFoundException.class,
133                         () -> mPackageManagerHelper.getPackageNamesForUid(/* callingUid= */ -1));
134         assertThat(thrown).hasMessageThat().contains("Could not find package for -1");
135     }
136 }
137