1 /*
2  * Copyright (C) 2020 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.systemui.car.sideloaded;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.ArgumentMatchers.anyInt;
23 import static org.mockito.ArgumentMatchers.eq;
24 import static org.mockito.Mockito.when;
25 
26 import android.app.ActivityTaskManager.RootTaskInfo;
27 import android.content.ComponentName;
28 import android.content.pm.ApplicationInfo;
29 import android.content.pm.InstallSourceInfo;
30 import android.content.pm.PackageManager;
31 import android.testing.AndroidTestingRunner;
32 import android.testing.TestableLooper;
33 import android.testing.TestableResources;
34 
35 import androidx.test.filters.SmallTest;
36 
37 import com.android.systemui.R;
38 import com.android.systemui.SysuiTestCase;
39 import com.android.systemui.car.CarDeviceProvisionedController;
40 import com.android.systemui.car.CarSystemUiTest;
41 
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.Mock;
46 import org.mockito.MockitoAnnotations;
47 
48 @CarSystemUiTest
49 @RunWith(AndroidTestingRunner.class)
50 @TestableLooper.RunWithLooper
51 @SmallTest
52 public class SideLoadedAppDetectorTest extends SysuiTestCase {
53 
54     private static final String SAFE_VENDOR = "com.safe.vendor";
55     private static final String UNSAFE_VENDOR = "com.unsafe.vendor";
56     private static final String APP_PACKAGE_NAME = "com.test";
57     private static final String APP_CLASS_NAME = ".TestClass";
58 
59     private SideLoadedAppDetector mSideLoadedAppDetector;
60 
61     @Mock
62     private PackageManager mPackageManager;
63     @Mock
64     private CarDeviceProvisionedController mCarDeviceProvisionedController;
65 
66     @Before
setUp()67     public void setUp() throws Exception {
68         MockitoAnnotations.initMocks(this);
69 
70         TestableResources testableResources = mContext.getOrCreateTestableResources();
71         String[] allowedAppInstallSources = new String[]{SAFE_VENDOR};
72         testableResources.addOverride(R.array.config_allowedAppInstallSources,
73                 allowedAppInstallSources);
74 
75         mSideLoadedAppDetector = new SideLoadedAppDetector(testableResources.getResources(),
76                 mPackageManager,
77                 mCarDeviceProvisionedController);
78     }
79 
80     @Test
isSafe_systemApp_returnsTrue()81     public void isSafe_systemApp_returnsTrue() throws Exception {
82         RootTaskInfo taskInfo = new RootTaskInfo();
83         taskInfo.topActivity = new ComponentName(APP_PACKAGE_NAME, APP_CLASS_NAME);
84 
85         ApplicationInfo applicationInfo = new ApplicationInfo();
86         applicationInfo.packageName = APP_PACKAGE_NAME;
87         applicationInfo.flags = ApplicationInfo.FLAG_SYSTEM;
88 
89         when(mPackageManager.getApplicationInfoAsUser(eq(APP_PACKAGE_NAME), anyInt(), any()))
90                 .thenReturn(applicationInfo);
91 
92         assertThat(mSideLoadedAppDetector.isSafe(taskInfo)).isTrue();
93     }
94 
95     @Test
isSafe_updatedSystemApp_returnsTrue()96     public void isSafe_updatedSystemApp_returnsTrue() throws Exception {
97         RootTaskInfo taskInfo = new RootTaskInfo();
98         taskInfo.topActivity = new ComponentName(APP_PACKAGE_NAME, APP_CLASS_NAME);
99 
100         ApplicationInfo applicationInfo = new ApplicationInfo();
101         applicationInfo.packageName = APP_PACKAGE_NAME;
102         applicationInfo.flags = ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
103 
104         when(mPackageManager.getApplicationInfoAsUser(eq(APP_PACKAGE_NAME), anyInt(), any()))
105                 .thenReturn(applicationInfo);
106 
107         assertThat(mSideLoadedAppDetector.isSafe(taskInfo)).isTrue();
108     }
109 
110     @Test
isSafe_nonSystemApp_withSafeSource_returnsTrue()111     public void isSafe_nonSystemApp_withSafeSource_returnsTrue() throws Exception {
112         InstallSourceInfo sourceInfo = new InstallSourceInfo(SAFE_VENDOR,
113                 /* initiatingPackageSigningInfo= */null,
114                 /* originatingPackageName= */ null,
115                 /* installingPackageName= */ null);
116         RootTaskInfo taskInfo = new RootTaskInfo();
117         taskInfo.topActivity = new ComponentName(APP_PACKAGE_NAME, APP_CLASS_NAME);
118 
119         ApplicationInfo applicationInfo = new ApplicationInfo();
120         applicationInfo.packageName = APP_PACKAGE_NAME;
121 
122         when(mPackageManager.getApplicationInfoAsUser(eq(APP_PACKAGE_NAME), anyInt(), any()))
123                 .thenReturn(applicationInfo);
124         when(mPackageManager.getInstallSourceInfo(APP_PACKAGE_NAME)).thenReturn(sourceInfo);
125 
126         assertThat(mSideLoadedAppDetector.isSafe(taskInfo)).isTrue();
127     }
128 
129     @Test
isSafe_nonSystemApp_withUnsafeSource_returnsFalse()130     public void isSafe_nonSystemApp_withUnsafeSource_returnsFalse() throws Exception {
131         InstallSourceInfo sourceInfo = new InstallSourceInfo(UNSAFE_VENDOR,
132                 /* initiatingPackageSigningInfo= */null,
133                 /* originatingPackageName= */ null,
134                 /* installingPackageName= */ null);
135         RootTaskInfo taskInfo = new RootTaskInfo();
136         taskInfo.topActivity = new ComponentName(APP_PACKAGE_NAME, APP_CLASS_NAME);
137 
138         ApplicationInfo applicationInfo = new ApplicationInfo();
139         applicationInfo.packageName = APP_PACKAGE_NAME;
140 
141         when(mPackageManager.getApplicationInfoAsUser(eq(APP_PACKAGE_NAME), anyInt(), any()))
142                 .thenReturn(applicationInfo);
143         when(mPackageManager.getInstallSourceInfo(APP_PACKAGE_NAME)).thenReturn(sourceInfo);
144 
145         assertThat(mSideLoadedAppDetector.isSafe(taskInfo)).isFalse();
146     }
147 
148     @Test
isSafe_nonSystemApp_withoutSource_returnsFalse()149     public void isSafe_nonSystemApp_withoutSource_returnsFalse() throws Exception {
150         InstallSourceInfo sourceInfo = new InstallSourceInfo(null,
151                 /* initiatingPackageSigningInfo= */null,
152                 /* originatingPackageName= */ null,
153                 /* installingPackageName= */ null);
154         RootTaskInfo taskInfo = new RootTaskInfo();
155         taskInfo.topActivity = new ComponentName(APP_PACKAGE_NAME, APP_CLASS_NAME);
156 
157         ApplicationInfo applicationInfo = new ApplicationInfo();
158         applicationInfo.packageName = APP_PACKAGE_NAME;
159 
160         when(mPackageManager.getApplicationInfoAsUser(eq(APP_PACKAGE_NAME), anyInt(), any()))
161                 .thenReturn(applicationInfo);
162         when(mPackageManager.getInstallSourceInfo(APP_PACKAGE_NAME)).thenReturn(sourceInfo);
163 
164         assertThat(mSideLoadedAppDetector.isSafe(taskInfo)).isFalse();
165     }
166 }
167