From a61a3c5c15c6564d60af0e8c08b879cdfa7c509d Mon Sep 17 00:00:00 2001 From: Roshan Pius Date: Tue, 17 Sep 2019 09:45:49 -0700 Subject: WifiPermissionsUtil: Use getApplicationInfoAsUser for target SDK checks To handle multi-user scenarios, pass in the corresponding app's user id to fetch app info. Bug: 140895783 Test: atest com.android.server.wifi Change-Id: I0ff22e39513778b13648b226e91d8a27effc7596 --- .../android/server/wifi/WifiServiceImplTest.java | 38 +++++++++++----------- .../aware/WifiAwareDataPathStateManagerTest.java | 17 ++++++---- .../server/wifi/util/WifiPermissionsUtilTest.java | 2 +- 3 files changed, 31 insertions(+), 26 deletions(-) (limited to 'tests') diff --git a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java index 4f0ddc036..9702d2ba6 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java @@ -544,7 +544,7 @@ public class WifiServiceImplTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(true); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); when(mSettingsStore.handleWifiToggled(anyBoolean())).thenReturn(true); when(mSettingsStore.isAirplaneModeOn()).thenReturn(false); @@ -563,7 +563,7 @@ public class WifiServiceImplTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(false); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(false); when(mSettingsStore.handleWifiToggled(eq(true))).thenReturn(true); when(mSettingsStore.isAirplaneModeOn()).thenReturn(false); @@ -607,7 +607,7 @@ public class WifiServiceImplTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(false); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(false); when(mDevicePolicyManagerInternal.isActiveAdminWithPolicy( Process.myUid(), DeviceAdminInfo.USES_POLICY_DEVICE_OWNER)) .thenReturn(true); @@ -627,7 +627,7 @@ public class WifiServiceImplTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(false); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(false); mApplicationInfo.flags = ApplicationInfo.FLAG_SYSTEM; when(mSettingsStore.handleWifiToggled(eq(true))).thenReturn(true); @@ -645,7 +645,7 @@ public class WifiServiceImplTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(true); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); when(mSettingsStore.handleWifiToggled(eq(true))).thenReturn(true); when(mSettingsStore.isAirplaneModeOn()).thenReturn(false); @@ -676,7 +676,7 @@ public class WifiServiceImplTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(false); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(false); when(mSettingsStore.handleWifiToggled(eq(true))).thenReturn(true); when(mSettingsStore.isAirplaneModeOn()).thenReturn(false); @@ -693,7 +693,7 @@ public class WifiServiceImplTest { doThrow(new SecurityException()).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(true); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); when(mSettingsStore.handleWifiToggled(eq(true))).thenReturn(true); try { mWifiServiceImpl.setWifiEnabled(TEST_PACKAGE_NAME, true); @@ -713,7 +713,7 @@ public class WifiServiceImplTest { doReturn(AppOpsManager.MODE_IGNORED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(true); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); when(mSettingsStore.handleWifiToggled(eq(true))).thenReturn(true); mWifiServiceImpl.setWifiEnabled(TEST_PACKAGE_NAME, true); @@ -745,7 +745,7 @@ public class WifiServiceImplTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(true); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); when(mSettingsStore.handleWifiToggled(eq(true))).thenReturn(true); when(mSettingsStore.isAirplaneModeOn()).thenReturn(true); when(mContext.checkPermission( @@ -789,7 +789,7 @@ public class WifiServiceImplTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(true); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); when(mSettingsStore.isWifiToggleEnabled()).thenReturn(false); mWifiServiceImpl.checkAndStartWifi(); @@ -871,7 +871,7 @@ public class WifiServiceImplTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(false); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(false); when(mDevicePolicyManagerInternal.isActiveAdminWithPolicy( Process.myUid(), DeviceAdminInfo.USES_POLICY_PROFILE_OWNER)) .thenReturn(true); @@ -891,7 +891,7 @@ public class WifiServiceImplTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(false); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(false); mApplicationInfo.flags = ApplicationInfo.FLAG_SYSTEM; when(mSettingsStore.handleWifiToggled(eq(false))).thenReturn(true); @@ -910,7 +910,7 @@ public class WifiServiceImplTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(true); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); when(mSettingsStore.handleWifiToggled(eq(false))).thenReturn(true); when(mSettingsStore.isAirplaneModeOn()).thenReturn(false); @@ -941,7 +941,7 @@ public class WifiServiceImplTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(false); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(false); when(mSettingsStore.handleWifiToggled(eq(false))).thenReturn(true); when(mSettingsStore.isAirplaneModeOn()).thenReturn(false); @@ -2660,7 +2660,7 @@ public class WifiServiceImplTest { when(mContext.getPackageManager()).thenReturn(pm); when(pm.getApplicationInfo(any(), anyInt())).thenReturn(mApplicationInfo); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(true); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); when(mClientModeImpl.syncAddOrUpdatePasspointConfig(any(), any(PasspointConfiguration.class), anyInt(), eq(TEST_PACKAGE_NAME))).thenReturn( @@ -2822,7 +2822,7 @@ public class WifiServiceImplTest { when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(false); when(mWifiPermissionsUtil.checkNetworkSetupWizardPermission(anyInt())).thenReturn(false); when(mWifiPermissionsUtil.isTargetSdkLessThan(eq(TEST_PACKAGE_NAME), - eq(Build.VERSION_CODES.Q))).thenReturn(true); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); List result = mWifiServiceImpl.getPasspointConfigurations( TEST_PACKAGE_NAME); @@ -2855,7 +2855,7 @@ public class WifiServiceImplTest { when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(anyInt())).thenReturn( false); when(mWifiPermissionsUtil.isTargetSdkLessThan(isNull(), - eq(Build.VERSION_CODES.Q))).thenReturn(true); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); assertFalse(mWifiServiceImpl.removePasspointConfiguration(null, null)); } @@ -3733,7 +3733,7 @@ public class WifiServiceImplTest { .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mClientModeImpl.syncAddOrUpdateNetwork(any(), any())).thenReturn(0); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(true); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); assertEquals(0, mWifiServiceImpl.addOrUpdateNetwork(config, TEST_PACKAGE_NAME)); @@ -3876,7 +3876,7 @@ public class WifiServiceImplTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(true); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); mWifiServiceImpl.enableNetwork(TEST_NETWORK_ID, true, TEST_PACKAGE_NAME); diff --git a/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareDataPathStateManagerTest.java b/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareDataPathStateManagerTest.java index f2cc45fef..35916d2f6 100644 --- a/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareDataPathStateManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareDataPathStateManagerTest.java @@ -161,7 +161,8 @@ public class WifiAwareDataPathStateManagerTest { // by default pretend to be an old API: i.e. allow Responders configured as *ANY*. This // allows older (more extrensive) tests to run. - when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), anyInt())).thenReturn(true); + when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), anyInt(), anyInt())) + .thenReturn(true); when(mWifiPermissionsUtil.isLocationModeEnabled()).thenReturn(true); mDut = new WifiAwareStateManager(); @@ -1017,7 +1018,8 @@ public class WifiAwareDataPathStateManagerTest { */ @Test public void testDataPathResonderMacPassphraseNoPeerIdSuccessNonLegacy() throws Exception { - when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), anyInt())).thenReturn(false); + when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), anyInt(), anyInt())) + .thenReturn(false); testDataPathResponderUtility(false, false, false, true, true); } @@ -1028,7 +1030,8 @@ public class WifiAwareDataPathStateManagerTest { @Test public void testDataPathResonderMacOpenNoPeerIdNoPmkPassphraseSuccessNonLegacy() throws Exception { - when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), anyInt())).thenReturn(false); + when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), anyInt(), anyInt())) + .thenReturn(false); testDataPathResponderUtility(false, false, false, false, true); } @@ -1074,7 +1077,8 @@ public class WifiAwareDataPathStateManagerTest { */ @Test public void testDataPathResonderDirectNoMacPassphraseSuccessNonLegacy() throws Exception { - when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), anyInt())).thenReturn(false); + when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), anyInt(), anyInt())) + .thenReturn(false); testDataPathResponderUtility(true, false, false, true, true); } @@ -1084,7 +1088,8 @@ public class WifiAwareDataPathStateManagerTest { */ @Test public void testDataPathResonderDirectNoMacNoPmkPassphraseSuccessNonLegacy() throws Exception { - when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), anyInt())).thenReturn(false); + when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), anyInt(), anyInt())) + .thenReturn(false); testDataPathResponderUtility(true, false, false, false, true); } @@ -1473,7 +1478,7 @@ public class WifiAwareDataPathStateManagerTest { InOrder inOrderM = inOrder(mAwareMetricsMock); boolean isLegacy = mWifiPermissionsUtil.isTargetSdkLessThan("anything", - Build.VERSION_CODES.P); + Build.VERSION_CODES.P, 0); if (providePmk) { when(mPermissionsWrapperMock.getUidPermission( diff --git a/tests/wifitests/src/com/android/server/wifi/util/WifiPermissionsUtilTest.java b/tests/wifitests/src/com/android/server/wifi/util/WifiPermissionsUtilTest.java index 8baacf615..0c9ed26b7 100644 --- a/tests/wifitests/src/com/android/server/wifi/util/WifiPermissionsUtilTest.java +++ b/tests/wifitests/src/com/android/server/wifi/util/WifiPermissionsUtilTest.java @@ -1148,7 +1148,7 @@ public class WifiPermissionsUtilTest { } private void setupMocks() throws Exception { - when(mMockPkgMgr.getApplicationInfo(TEST_PACKAGE_NAME, 0)) + when(mMockPkgMgr.getApplicationInfoAsUser(eq(TEST_PACKAGE_NAME), eq(0), anyInt())) .thenReturn(mMockApplInfo); when(mMockContext.getPackageManager()).thenReturn(mMockPkgMgr); when(mMockAppOps.noteOp(AppOpsManager.OP_WIFI_SCAN, mUid, TEST_PACKAGE_NAME)) -- cgit v1.2.3