1 /* 2 * Copyright (C) 2023 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.contentprotection; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.ArgumentMatchers.eq; 23 import static org.mockito.Mockito.when; 24 25 import android.Manifest.permission; 26 import android.content.pm.ApplicationInfo; 27 import android.content.pm.PackageInfo; 28 import android.content.pm.PackageManager; 29 import android.content.pm.PackageManager.NameNotFoundException; 30 import android.content.pm.PackageManager.PackageInfoFlags; 31 import android.testing.TestableContext; 32 33 import androidx.test.core.app.ApplicationProvider; 34 import androidx.test.ext.junit.runners.AndroidJUnit4; 35 import androidx.test.filters.SmallTest; 36 37 import org.junit.Before; 38 import org.junit.Rule; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.Mock; 42 import org.mockito.junit.MockitoJUnit; 43 import org.mockito.junit.MockitoRule; 44 45 /** 46 * Test for {@link ContentProtectionPackageManager}. 47 * 48 * <p>Run with: {@code atest 49 * FrameworksServicesTests:com.android.server.contentprotection.ContentProtectionPackageManagerTest} 50 */ 51 @RunWith(AndroidJUnit4.class) 52 @SmallTest 53 public class ContentProtectionPackageManagerTest { 54 private static final String PACKAGE_NAME = "PACKAGE_NAME"; 55 56 private static final PackageInfo EMPTY_PACKAGE_INFO = new PackageInfo(); 57 58 private static final PackageInfo SYSTEM_APP_PACKAGE_INFO = createSystemAppPackageInfo(); 59 60 private static final PackageInfo UPDATED_SYSTEM_APP_PACKAGE_INFO = 61 createUpdatedSystemAppPackageInfo(); 62 63 @Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule(); 64 65 @Rule 66 public final TestableContext mContext = 67 new TestableContext(ApplicationProvider.getApplicationContext()); 68 69 @Mock private PackageManager mMockPackageManager; 70 71 private ContentProtectionPackageManager mContentProtectionPackageManager; 72 73 @Before setup()74 public void setup() { 75 mContext.setMockPackageManager(mMockPackageManager); 76 mContentProtectionPackageManager = new ContentProtectionPackageManager(mContext); 77 } 78 79 @Test getPackageInfo_found()80 public void getPackageInfo_found() throws Exception { 81 PackageInfo expected = createPackageInfo(/* flags= */ 0); 82 when(mMockPackageManager.getPackageInfo(eq(PACKAGE_NAME), any(PackageInfoFlags.class))) 83 .thenReturn(expected); 84 85 PackageInfo actual = mContentProtectionPackageManager.getPackageInfo(PACKAGE_NAME); 86 87 assertThat(actual).isEqualTo(expected); 88 } 89 90 @Test getPackageInfo_notFound()91 public void getPackageInfo_notFound() throws Exception { 92 when(mMockPackageManager.getPackageInfo(eq(PACKAGE_NAME), any(PackageInfoFlags.class))) 93 .thenThrow(new NameNotFoundException()); 94 95 PackageInfo actual = mContentProtectionPackageManager.getPackageInfo(PACKAGE_NAME); 96 97 assertThat(actual).isNull(); 98 } 99 100 @Test getPackageInfo_null()101 public void getPackageInfo_null() { 102 PackageInfo actual = mContentProtectionPackageManager.getPackageInfo(PACKAGE_NAME); 103 104 assertThat(actual).isNull(); 105 } 106 107 @Test isSystemApp_true()108 public void isSystemApp_true() { 109 boolean actual = mContentProtectionPackageManager.isSystemApp(SYSTEM_APP_PACKAGE_INFO); 110 111 assertThat(actual).isTrue(); 112 } 113 114 @Test isSystemApp_false()115 public void isSystemApp_false() { 116 boolean actual = 117 mContentProtectionPackageManager.isSystemApp(UPDATED_SYSTEM_APP_PACKAGE_INFO); 118 119 assertThat(actual).isFalse(); 120 } 121 122 @Test isSystemApp_noApplicationInfo()123 public void isSystemApp_noApplicationInfo() { 124 boolean actual = mContentProtectionPackageManager.isSystemApp(EMPTY_PACKAGE_INFO); 125 126 assertThat(actual).isFalse(); 127 } 128 129 @Test isUpdatedSystemApp_true()130 public void isUpdatedSystemApp_true() { 131 boolean actual = 132 mContentProtectionPackageManager.isUpdatedSystemApp( 133 UPDATED_SYSTEM_APP_PACKAGE_INFO); 134 135 assertThat(actual).isTrue(); 136 } 137 138 @Test isUpdatedSystemApp_false()139 public void isUpdatedSystemApp_false() { 140 boolean actual = 141 mContentProtectionPackageManager.isUpdatedSystemApp(SYSTEM_APP_PACKAGE_INFO); 142 143 assertThat(actual).isFalse(); 144 } 145 146 @Test isUpdatedSystemApp_noApplicationInfo()147 public void isUpdatedSystemApp_noApplicationInfo() { 148 boolean actual = mContentProtectionPackageManager.isUpdatedSystemApp(EMPTY_PACKAGE_INFO); 149 150 assertThat(actual).isFalse(); 151 } 152 153 @Test hasRequestedInternetPermissions_true()154 public void hasRequestedInternetPermissions_true() { 155 PackageInfo packageInfo = createPackageInfo(new String[] {permission.INTERNET}); 156 157 boolean actual = 158 mContentProtectionPackageManager.hasRequestedInternetPermissions(packageInfo); 159 160 assertThat(actual).isTrue(); 161 } 162 163 @Test hasRequestedInternetPermissions_false()164 public void hasRequestedInternetPermissions_false() { 165 PackageInfo packageInfo = createPackageInfo(new String[] {permission.ACCESS_FINE_LOCATION}); 166 167 boolean actual = 168 mContentProtectionPackageManager.hasRequestedInternetPermissions(packageInfo); 169 170 assertThat(actual).isFalse(); 171 } 172 173 @Test hasRequestedInternetPermissions_noRequestedPermissions()174 public void hasRequestedInternetPermissions_noRequestedPermissions() { 175 boolean actual = 176 mContentProtectionPackageManager.hasRequestedInternetPermissions( 177 EMPTY_PACKAGE_INFO); 178 179 assertThat(actual).isFalse(); 180 } 181 createSystemAppPackageInfo()182 private static PackageInfo createSystemAppPackageInfo() { 183 return createPackageInfo(ApplicationInfo.FLAG_SYSTEM); 184 } 185 createUpdatedSystemAppPackageInfo()186 private static PackageInfo createUpdatedSystemAppPackageInfo() { 187 return createPackageInfo(ApplicationInfo.FLAG_UPDATED_SYSTEM_APP); 188 } 189 createPackageInfo(int flags)190 private static PackageInfo createPackageInfo(int flags) { 191 return createPackageInfo(flags, /* requestedPermissions= */ new String[0]); 192 } 193 createPackageInfo(String[] requestedPermissions)194 private static PackageInfo createPackageInfo(String[] requestedPermissions) { 195 return createPackageInfo(/* flags= */ 0, requestedPermissions); 196 } 197 createPackageInfo(int flags, String[] requestedPermissions)198 private static PackageInfo createPackageInfo(int flags, String[] requestedPermissions) { 199 PackageInfo packageInfo = new PackageInfo(); 200 packageInfo.packageName = PACKAGE_NAME; 201 packageInfo.applicationInfo = new ApplicationInfo(); 202 packageInfo.applicationInfo.packageName = PACKAGE_NAME; 203 packageInfo.applicationInfo.flags = flags; 204 packageInfo.requestedPermissions = requestedPermissions; 205 return packageInfo; 206 } 207 } 208