• 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.tests.apex.app;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.Manifest;
22 import android.content.Context;
23 import android.content.pm.PackageInfo;
24 import android.content.pm.PackageManager;
25 
26 import androidx.test.InstrumentationRegistry;
27 
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.junit.runners.JUnit4;
33 
34 @RunWith(JUnit4.class)
35 public class ApkInApexTests {
36     private final Context mContext = InstrumentationRegistry.getContext();
37     private final PackageManager mPm = mContext.getPackageManager();
38 
39 
40     @Before
adoptShellPermissions()41     public void adoptShellPermissions() {
42         InstrumentationRegistry
43                 .getInstrumentation()
44                 .getUiAutomation()
45                 .adoptShellPermissionIdentity(
46                         Manifest.permission.INSTALL_PACKAGES,
47                         Manifest.permission.DELETE_PACKAGES,
48                         Manifest.permission.TEST_MANAGE_ROLLBACKS);
49     }
50 
51     @After
dropShellPermissions()52     public void dropShellPermissions() {
53         InstrumentationRegistry
54                 .getInstrumentation()
55                 .getUiAutomation()
56                 .dropShellPermissionIdentity();
57     }
58 
59     @Test
testPrivPermissionIsGranted()60     public void testPrivPermissionIsGranted() throws Exception {
61         PackageInfo pi = mPm.getPackageInfo("com.android.apex.product.app.test",
62                 PackageManager.GET_PERMISSIONS);
63         assertThat(pi.requestedPermissions).asList()
64                 .contains("android.permission.PACKAGE_USAGE_STATS");
65 
66         pi = mPm.getPackageInfo("com.android.apex.system.app.test",
67                 PackageManager.GET_PERMISSIONS);
68         assertThat(pi.requestedPermissions).asList()
69                 .contains("android.permission.PACKAGE_USAGE_STATS");
70 
71         pi = mPm.getPackageInfo("com.android.apex.system_ext.app.test",
72                 PackageManager.GET_PERMISSIONS);
73         assertThat(pi.requestedPermissions).asList()
74                 .contains("android.permission.PACKAGE_USAGE_STATS");
75 
76         pi = mPm.getPackageInfo("com.android.apex.vendor.app.test",
77                 PackageManager.GET_PERMISSIONS);
78 
79         assertThat(pi.requestedPermissions).asList()
80                 .contains("android.permission.START_ACTIVITIES_FROM_BACKGROUND");
81     }
82 
83     @Test
testJniCalls()84     public void testJniCalls() throws Exception {
85         System.loadLibrary("ApkInApex_jni");
86         assertThat(nativeFakeMethod()).isTrue();
87     }
88 
nativeFakeMethod()89     private native boolean nativeFakeMethod();
90 }
91