• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.pm.test.appenumeration;
18 
19 import static com.android.compatibility.common.util.ShellUtils.runShellCommand;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import android.app.AppGlobals;
24 import android.content.pm.IPackageManager;
25 import android.content.pm.ProviderInfo;
26 
27 import androidx.test.runner.AndroidJUnit4;
28 
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.stream.Collectors;
37 
38 /**
39  * Application enumeration tests for the internal apis of package manager service.
40  */
41 @RunWith(AndroidJUnit4.class)
42 public class AppEnumerationInternalTests {
43     private static final String TEST_DATA_PATH = "/data/local/tmp/appenumerationtests/";
44     private static final String SYNC_PROVIDER_APK_PATH =
45             TEST_DATA_PATH + "AppEnumerationSyncProviderTestApp.apk";
46     private static final String SYNC_PROVIDER_PKG_NAME = "com.android.appenumeration.syncprovider";
47     private static final String SYNC_PROVIDER_AUTHORITY = SYNC_PROVIDER_PKG_NAME;
48 
49     private IPackageManager mIPackageManager;
50 
51     @Before
setup()52     public void setup() {
53         mIPackageManager = AppGlobals.getPackageManager();
54     }
55 
56     @After
tearDown()57     public void tearDown() throws Exception {
58         uninstallPackage(SYNC_PROVIDER_PKG_NAME);
59     }
60 
61     @Test
querySyncProviders_canSeeForceQueryable()62     public void querySyncProviders_canSeeForceQueryable() throws Exception {
63         final List<String> names = new ArrayList<>();
64         final List<ProviderInfo> infos = new ArrayList<>();
65         installPackage(SYNC_PROVIDER_APK_PATH, true /* forceQueryable */);
66         mIPackageManager.querySyncProviders(names, infos);
67 
68         assertThat(names).contains(SYNC_PROVIDER_AUTHORITY);
69         assertThat(infos.stream().map(info -> info.packageName).collect(Collectors.toList()))
70                 .contains(SYNC_PROVIDER_PKG_NAME);
71     }
72 
73     @Test
querySyncProviders_cannotSeeSyncProvider()74     public void querySyncProviders_cannotSeeSyncProvider() throws Exception {
75         final List<String> names = new ArrayList<>();
76         final List<ProviderInfo> infos = new ArrayList<>();
77         installPackage(SYNC_PROVIDER_APK_PATH, false /* forceQueryable */);
78         mIPackageManager.querySyncProviders(names, infos);
79 
80         assertThat(names).doesNotContain(SYNC_PROVIDER_AUTHORITY);
81         assertThat(infos.stream().map(info -> info.packageName).collect(Collectors.toList()))
82                 .doesNotContain(SYNC_PROVIDER_PKG_NAME);
83     }
84 
installPackage(String apkPath, boolean forceQueryable)85     private static void installPackage(String apkPath, boolean forceQueryable) {
86         final StringBuilder cmd = new StringBuilder("pm install ");
87         if (forceQueryable) {
88             cmd.append("--force-queryable ");
89         }
90         cmd.append(apkPath);
91         final String result = runShellCommand(cmd.toString());
92         assertThat(result.trim()).contains("Success");
93     }
94 
uninstallPackage(String packageName)95     private static void uninstallPackage(String packageName) {
96         runShellCommand("pm uninstall " + packageName);
97     }
98 }
99