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 android.ext.services.watchdog; 18 19 import static android.service.watchdog.ExplicitHealthCheckService.PackageConfig; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.mockito.ArgumentMatchers.any; 24 import static org.mockito.ArgumentMatchers.anyInt; 25 import static org.mockito.Mockito.when; 26 27 import android.Manifest; 28 import android.content.Context; 29 import android.content.ContextWrapper; 30 import android.content.Intent; 31 import android.content.pm.ApplicationInfo; 32 import android.content.pm.PackageManager; 33 import android.content.pm.ResolveInfo; 34 import android.content.pm.ServiceInfo; 35 import android.net.ConnectivityManager; 36 import android.test.ServiceTestCase; 37 38 import androidx.test.InstrumentationRegistry; 39 40 import org.junit.After; 41 import org.junit.Before; 42 import org.junit.Test; 43 import org.mockito.Mock; 44 import org.mockito.Mockito; 45 import org.mockito.MockitoAnnotations; 46 47 import java.util.Arrays; 48 import java.util.List; 49 50 /** 51 * Contains the base unit tests for ExplicitHealthCheckServiceImpl service. 52 */ 53 public class ExplicitHealthCheckServiceImplUnitTest 54 extends ServiceTestCase<ExplicitHealthCheckServiceImpl> { 55 private Context mContext; 56 @Mock 57 private ConnectivityManager mConnectivityManager; 58 @Mock 59 private PackageManager mPackageManager; 60 ExplicitHealthCheckServiceImplUnitTest()61 public ExplicitHealthCheckServiceImplUnitTest() { 62 super(ExplicitHealthCheckServiceImpl.class); 63 } 64 65 @Before setUp()66 public void setUp() throws Exception { 67 super.setUp(); 68 MockitoAnnotations.initMocks(this); 69 mContext = Mockito.spy(new ContextWrapper(getSystemContext())); 70 setContext(mContext); 71 72 when(mContext.getSystemService(ConnectivityManager.class)).thenReturn(mConnectivityManager); 73 when(mContext.getPackageManager()).thenReturn(mPackageManager); 74 75 InstrumentationRegistry 76 .getInstrumentation() 77 .getUiAutomation() 78 .adoptShellPermissionIdentity(Manifest.permission.READ_DEVICE_CONFIG); 79 80 Intent intent = new Intent(getContext(), ExplicitHealthCheckServiceImpl.class); 81 startService(intent); 82 } 83 84 @After tearDown()85 public void tearDown() { 86 getService().mSupportedCheckers.clear(); 87 88 InstrumentationRegistry 89 .getInstrumentation() 90 .getUiAutomation() 91 .dropShellPermissionIdentity(); 92 } 93 94 @Test testInitHealthCheckersWhileNoResolveInfo()95 public void testInitHealthCheckersWhileNoResolveInfo() throws Exception { 96 when(mPackageManager.queryIntentServices(any(), anyInt())).thenReturn(null); 97 98 final ExplicitHealthCheckServiceImpl service = getService(); 99 service.onCreate(); 100 101 assertThat(service.mSupportedCheckers).hasSize(0); 102 } 103 104 @Test testInitHealthCheckersWhileApplicationInfoFlagNotSystem()105 public void testInitHealthCheckersWhileApplicationInfoFlagNotSystem() throws Exception { 106 when(mPackageManager.queryIntentServices(any(), anyInt())) 107 .thenReturn(createFakeResolveInfo(ApplicationInfo.FLAG_DEBUGGABLE)); 108 109 final ExplicitHealthCheckServiceImpl service = getService(); 110 service.onCreate(); 111 112 assertThat(service.mSupportedCheckers).hasSize(0); 113 } 114 115 @Test testInitHealthCheckersWhileHasResolveInfoAndFlagIsSystem()116 public void testInitHealthCheckersWhileHasResolveInfoAndFlagIsSystem() throws Exception { 117 when(mPackageManager.queryIntentServices(any(), anyInt())) 118 .thenReturn(createFakeResolveInfo(ApplicationInfo.FLAG_SYSTEM)); 119 120 final ExplicitHealthCheckServiceImpl service = getService(); 121 service.onCreate(); 122 123 assertThat(service.mSupportedCheckers).hasSize(1); 124 assertThat(service.mSupportedCheckers.get("pkg").getSupportedPackageName()) 125 .isEqualTo("pkg"); 126 } 127 128 @Test testOnRequestHealthCheck()129 public void testOnRequestHealthCheck() throws Exception { 130 when(mPackageManager.queryIntentServices(any(), anyInt())) 131 .thenReturn(createFakeResolveInfo(ApplicationInfo.FLAG_SYSTEM)); 132 final ExplicitHealthCheckServiceImpl service = getService(); 133 service.onCreate(); 134 135 assertThat(service.mSupportedCheckers.get("pkg").isPending()).isFalse(); 136 137 service.onRequestHealthCheck(/* packageName */ "pkg"); 138 139 assertThat(service.mSupportedCheckers.get("pkg").isPending()).isTrue(); 140 } 141 142 @Test testOnCancelHealthCheck()143 public void testOnCancelHealthCheck() throws Exception { 144 when(mPackageManager.queryIntentServices(any(), anyInt())) 145 .thenReturn(createFakeResolveInfo(ApplicationInfo.FLAG_SYSTEM)); 146 final ExplicitHealthCheckServiceImpl service = getService(); 147 service.onCreate(); 148 service.onRequestHealthCheck(/* packageName */ "pkg"); 149 150 service.onCancelHealthCheck(/* packageName */ "pkg"); 151 152 assertThat(service.mSupportedCheckers.get("pkg").isPending()).isFalse(); 153 } 154 155 @Test testOnGetSupportedPackages()156 public void testOnGetSupportedPackages() throws Exception { 157 when(mPackageManager.queryIntentServices(any(), anyInt())) 158 .thenReturn(createFakeResolveInfo(ApplicationInfo.FLAG_SYSTEM)); 159 final ExplicitHealthCheckServiceImpl service = getService(); 160 service.onCreate(); 161 162 final List<PackageConfig> packageConfigs = service.onGetSupportedPackages(); 163 164 assertThat(packageConfigs).hasSize(1); 165 assertThat(packageConfigs.get(0).getPackageName()).isEqualTo("pkg"); 166 } 167 168 @Test testOnGetRequestedPackagesWhileNoRequest()169 public void testOnGetRequestedPackagesWhileNoRequest() throws Exception { 170 when(mPackageManager.queryIntentServices(any(), anyInt())) 171 .thenReturn(createFakeResolveInfo(ApplicationInfo.FLAG_SYSTEM)); 172 final ExplicitHealthCheckServiceImpl service = getService(); 173 service.onCreate(); 174 175 final List<String> packages = service.onGetRequestedPackages(); 176 177 assertThat(packages).hasSize(0); 178 } 179 180 @Test testOnGetRequestedPackagesWhileHasRequest()181 public void testOnGetRequestedPackagesWhileHasRequest() throws Exception { 182 when(mPackageManager.queryIntentServices(any(), anyInt())) 183 .thenReturn(createFakeResolveInfo(ApplicationInfo.FLAG_SYSTEM)); 184 final ExplicitHealthCheckServiceImpl service = getService(); 185 service.onCreate(); 186 service.onRequestHealthCheck(/* packageName */ "pkg"); 187 188 final List<String> packages = service.onGetRequestedPackages(); 189 190 assertThat(packages).hasSize(1); 191 assertThat(packages.get(0)).isEqualTo("pkg"); 192 } 193 createFakeResolveInfo(int flags)194 private List<ResolveInfo> createFakeResolveInfo(int flags) { 195 return Arrays.asList( 196 makeNewResolveInfo(/* name */ "service", /* packageName */ "pkg", flags)); 197 } 198 makeNewResolveInfo(String name, String packageName, int flags)199 private ResolveInfo makeNewResolveInfo(String name, String packageName, int flags) { 200 final ApplicationInfo applicationInfo = new ApplicationInfo(); 201 applicationInfo.packageName = packageName; 202 applicationInfo.flags = flags; 203 final ServiceInfo serviceInfo = new ServiceInfo(); 204 serviceInfo.name = name; 205 serviceInfo.applicationInfo = applicationInfo; 206 final ResolveInfo resolveInfo = new ResolveInfo(); 207 resolveInfo.serviceInfo = serviceInfo; 208 return resolveInfo; 209 } 210 } 211