• 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.sensorprivacy;
18 
19 import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
20 import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn;
21 
22 import android.app.ActivityManager;
23 import android.app.ActivityTaskManager;
24 import android.app.AppOpsManager;
25 import android.content.Context;
26 import android.content.pm.UserInfo;
27 import android.os.Environment;
28 import android.telephony.TelephonyManager;
29 import android.testing.AndroidTestingRunner;
30 
31 import androidx.test.platform.app.InstrumentationRegistry;
32 
33 import com.android.dx.mockito.inline.extended.ExtendedMockito;
34 import com.android.server.LocalServices;
35 import com.android.server.SensorPrivacyService;
36 import com.android.server.pm.UserManagerInternal;
37 
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.MockitoSession;
42 import org.mockito.quality.Strictness;
43 
44 import java.io.File;
45 import java.io.IOException;
46 import java.nio.file.Files;
47 
48 @RunWith(AndroidTestingRunner.class)
49 public class SensorPrivacyServiceMockingTest {
50 
51     private static final String PERSISTENCE_FILE_PATHS_TEMPLATE =
52             "SensorPrivacyServiceMockingTest/persisted_file%d.xml";
53     public static final String PERSISTENCE_FILE1 =
54             String.format(PERSISTENCE_FILE_PATHS_TEMPLATE, 1);
55     public static final String PERSISTENCE_FILE2 =
56             String.format(PERSISTENCE_FILE_PATHS_TEMPLATE, 2);
57     public static final String PERSISTENCE_FILE3 =
58             String.format(PERSISTENCE_FILE_PATHS_TEMPLATE, 3);
59     public static final String PERSISTENCE_FILE4 =
60             String.format(PERSISTENCE_FILE_PATHS_TEMPLATE, 4);
61     public static final String PERSISTENCE_FILE5 =
62             String.format(PERSISTENCE_FILE_PATHS_TEMPLATE, 5);
63     public static final String PERSISTENCE_FILE6 =
64             String.format(PERSISTENCE_FILE_PATHS_TEMPLATE, 6);
65 
66     private Context mContext;
67     @Mock
68     private AppOpsManager mMockedAppOpsManager;
69     @Mock
70     private UserManagerInternal mMockedUserManagerInternal;
71     @Mock
72     private ActivityManager mMockedActivityManager;
73     @Mock
74     private ActivityTaskManager mMockedActivityTaskManager;
75     @Mock
76     private TelephonyManager mMockedTelephonyManager;
77 
78     @Test
testServiceInit()79     public void testServiceInit() throws IOException {
80         MockitoSession mockitoSession = ExtendedMockito.mockitoSession()
81                 .initMocks(this)
82                 .strictness(Strictness.WARN)
83                 .spyStatic(LocalServices.class)
84                 .spyStatic(Environment.class)
85                 .startMocking();
86 
87         try {
88             mContext = InstrumentationRegistry.getInstrumentation().getContext();
89             spyOn(mContext);
90 
91             doReturn(mMockedAppOpsManager).when(mContext).getSystemService(AppOpsManager.class);
92             doReturn(mMockedUserManagerInternal)
93                     .when(() -> LocalServices.getService(UserManagerInternal.class));
94             doReturn(mMockedActivityManager).when(mContext).getSystemService(ActivityManager.class);
95             doReturn(mMockedActivityTaskManager)
96                     .when(mContext).getSystemService(ActivityTaskManager.class);
97             doReturn(mMockedTelephonyManager).when(mContext).getSystemService(
98                     TelephonyManager.class);
99 
100             String dataDir = mContext.getApplicationInfo().dataDir;
101             doReturn(new File(dataDir)).when(() -> Environment.getDataSystemDirectory());
102 
103             File onDeviceFile = new File(dataDir, "sensor_privacy.xml");
104             onDeviceFile.delete();
105 
106             // Try all files with one known user
107             doReturn(new int[]{0}).when(mMockedUserManagerInternal).getUserIds();
108             doReturn(ExtendedMockito.mock(UserInfo.class)).when(mMockedUserManagerInternal)
109                     .getUserInfo(0);
110             initServiceWithPersistenceFile(onDeviceFile, null);
111             initServiceWithPersistenceFile(onDeviceFile, PERSISTENCE_FILE1);
112             initServiceWithPersistenceFile(onDeviceFile, PERSISTENCE_FILE2);
113             initServiceWithPersistenceFile(onDeviceFile, PERSISTENCE_FILE3);
114             initServiceWithPersistenceFile(onDeviceFile, PERSISTENCE_FILE4);
115             initServiceWithPersistenceFile(onDeviceFile, PERSISTENCE_FILE5);
116             initServiceWithPersistenceFile(onDeviceFile, PERSISTENCE_FILE6);
117 
118             // Try all files with two known users
119             doReturn(new int[]{0, 10}).when(mMockedUserManagerInternal).getUserIds();
120             doReturn(ExtendedMockito.mock(UserInfo.class)).when(mMockedUserManagerInternal)
121                     .getUserInfo(0);
122             doReturn(ExtendedMockito.mock(UserInfo.class)).when(mMockedUserManagerInternal)
123                     .getUserInfo(10);
124             initServiceWithPersistenceFile(onDeviceFile, null);
125             initServiceWithPersistenceFile(onDeviceFile, PERSISTENCE_FILE1);
126             initServiceWithPersistenceFile(onDeviceFile, PERSISTENCE_FILE2);
127             initServiceWithPersistenceFile(onDeviceFile, PERSISTENCE_FILE3);
128             initServiceWithPersistenceFile(onDeviceFile, PERSISTENCE_FILE4);
129             initServiceWithPersistenceFile(onDeviceFile, PERSISTENCE_FILE5);
130             initServiceWithPersistenceFile(onDeviceFile, PERSISTENCE_FILE6);
131 
132         } finally {
133             mockitoSession.finishMocking();
134         }
135     }
136 
initServiceWithPersistenceFile(File onDeviceFile, String persistenceFilePath)137     private void initServiceWithPersistenceFile(File onDeviceFile,
138             String persistenceFilePath) throws IOException {
139         if (persistenceFilePath != null) {
140             Files.copy(mContext.getAssets().open(persistenceFilePath),
141                     onDeviceFile.toPath());
142         }
143         new SensorPrivacyService(mContext);
144         onDeviceFile.delete();
145     }
146 }
147