diff options
author | Roshan Pius <rpius@google.com> | 2019-10-03 19:17:03 -0700 |
---|---|---|
committer | Roshan Pius <rpius@google.com> | 2019-10-07 17:51:47 +0000 |
commit | 75657ca9b0138a129be7e91e577e96d7872eddac (patch) | |
tree | b3dabd066d68299b43fabda98757c9225acc9fe6 /tests | |
parent | b516752a8ec54818247e4400f0a9518c4eb347d1 (diff) |
Wifi: Migrate DO/PO check to public APIs
Since wifi stack is now running out of system_server, we cannot use
LocalServices/DevicePolicyManager internal. Migrate to using public
APIs available for retrieving the same info.
Note:
a) This also addresses a @hide API usage within the wifi stack.
b) There are some privileged (settings/setupwizard) remove/forget/enable
APIs which passes in "null" as package name to
|WifiConfigManager.canModifyNetwork|. For all these calls, we will
bypass DO/PO checks (which should be fine since these APIs can never
be called by DO/PO apps)
Bug: 141943671
Test: atest com.android.server.wifi
Test: ACTS presubmit tests
Change-Id: I1ba528b6db0d752c683c0912af9cf34c483338e5
Diffstat (limited to 'tests')
7 files changed, 245 insertions, 127 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/CarrierNetworkEvaluatorTest.java b/tests/wifitests/src/com/android/server/wifi/CarrierNetworkEvaluatorTest.java index 56de59845..01b14a612 100644 --- a/tests/wifitests/src/com/android/server/wifi/CarrierNetworkEvaluatorTest.java +++ b/tests/wifitests/src/com/android/server/wifi/CarrierNetworkEvaluatorTest.java @@ -142,7 +142,8 @@ public class CarrierNetworkEvaluatorTest extends WifiBaseTest { mGetConfiguredNetworkForScanDetailsAnswer.addConfig(scanDetail, newConfig); } - when(mWifiConfigManager.enableNetwork(networkId, false, Process.WIFI_UID)).thenReturn(true); + when(mWifiConfigManager.enableNetwork( + networkId, false, Process.WIFI_UID, null)).thenReturn(true); when(mWifiConfigManager.setNetworkCandidateScanResult(eq(networkId), any(), anyInt())).thenReturn(true); when(mWifiConfigManager.getConfiguredNetwork(networkId)).thenReturn(newConfig); diff --git a/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java b/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java index 46610736c..70570d8b1 100644 --- a/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java @@ -690,12 +690,12 @@ public class ClientModeImplTest extends WifiBaseTest { } private void canForgetNetwork() throws Exception { - when(mWifiConfigManager.removeNetwork(eq(0), anyInt())).thenReturn(true); + when(mWifiConfigManager.removeNetwork(eq(0), anyInt(), any())).thenReturn(true); IActionListener connectActionListener = mock(IActionListener.class); mCmi.forget(0, mock(Binder.class), connectActionListener, 0, Binder.getCallingUid()); mLooper.dispatchAll(); verify(connectActionListener).onSuccess(); - verify(mWifiConfigManager).removeNetwork(anyInt(), anyInt()); + verify(mWifiConfigManager).removeNetwork(anyInt(), anyInt(), any()); } /** @@ -721,7 +721,7 @@ public class ClientModeImplTest extends WifiBaseTest { int networkId = TEST_NETWORK_ID; when(mWifiConfigManager.addOrUpdateNetwork(any(WifiConfiguration.class), anyInt())) .thenReturn(new NetworkUpdateResult(networkId)); - when(mWifiConfigManager.enableNetwork(eq(networkId), eq(false), anyInt())) + when(mWifiConfigManager.enableNetwork(eq(networkId), eq(false), anyInt(), any())) .thenReturn(true); IActionListener connectActionListener = mock(IActionListener.class); @@ -730,7 +730,7 @@ public class ClientModeImplTest extends WifiBaseTest { verify(connectActionListener).onSuccess(); verify(mWifiConfigManager).addOrUpdateNetwork(any(WifiConfiguration.class), anyInt()); - verify(mWifiConfigManager).enableNetwork(eq(networkId), eq(false), anyInt()); + verify(mWifiConfigManager).enableNetwork(eq(networkId), eq(false), anyInt(), any()); } /** @@ -763,7 +763,7 @@ public class ClientModeImplTest extends WifiBaseTest { verify(mWifiConfigManager, never()) .addOrUpdateNetwork(any(WifiConfiguration.class), anyInt()); verify(mWifiConfigManager, never()) - .enableNetwork(anyInt(), anyBoolean(), anyInt()); + .enableNetwork(anyInt(), anyBoolean(), anyInt(), any()); } /** @@ -783,7 +783,7 @@ public class ClientModeImplTest extends WifiBaseTest { verify(mWifiConfigManager).addOrUpdateNetwork(any(WifiConfiguration.class), anyInt()); verify(mWifiConfigManager, never()) - .enableNetwork(anyInt(), anyBoolean(), anyInt()); + .enableNetwork(anyInt(), anyBoolean(), anyInt(), any()); } /** @@ -796,7 +796,7 @@ public class ClientModeImplTest extends WifiBaseTest { int networkId = 5; when(mWifiConfigManager.addOrUpdateNetwork(any(WifiConfiguration.class), anyInt())) .thenReturn(new NetworkUpdateResult(networkId)); - when(mWifiConfigManager.enableNetwork(eq(networkId), eq(false), anyInt())) + when(mWifiConfigManager.enableNetwork(eq(networkId), eq(false), anyInt(), any())) .thenReturn(false); IActionListener connectActionListener = mock(IActionListener.class); @@ -805,7 +805,7 @@ public class ClientModeImplTest extends WifiBaseTest { verify(connectActionListener).onFailure(WifiManager.ERROR); verify(mWifiConfigManager).addOrUpdateNetwork(any(WifiConfiguration.class), anyInt()); - verify(mWifiConfigManager).enableNetwork(eq(networkId), eq(false), anyInt()); + verify(mWifiConfigManager).enableNetwork(eq(networkId), eq(false), anyInt(), any()); } /** @@ -844,7 +844,8 @@ public class ClientModeImplTest extends WifiBaseTest { } private void setupAndStartConnectSequence(WifiConfiguration config) throws Exception { - when(mWifiConfigManager.enableNetwork(eq(config.networkId), eq(true), anyInt())) + when(mWifiConfigManager.enableNetwork( + eq(config.networkId), eq(true), anyInt(), any())) .thenReturn(true); when(mWifiConfigManager.updateLastConnectUid(eq(config.networkId), anyInt())) .thenReturn(true); @@ -863,7 +864,8 @@ public class ClientModeImplTest extends WifiBaseTest { } private void validateSuccessfulConnectSequence(WifiConfiguration config) { - verify(mWifiConfigManager).enableNetwork(eq(config.networkId), eq(true), anyInt()); + verify(mWifiConfigManager).enableNetwork( + eq(config.networkId), eq(true), anyInt(), any()); verify(mWifiConnectivityManager).setUserConnectChoice(eq(config.networkId)); verify(mWifiConnectivityManager).prepareForForcedConnection(eq(config.networkId)); verify(mWifiConfigManager).getConfiguredNetworkWithoutMasking(eq(config.networkId)); @@ -871,7 +873,8 @@ public class ClientModeImplTest extends WifiBaseTest { } private void validateFailureConnectSequence(WifiConfiguration config) { - verify(mWifiConfigManager).enableNetwork(eq(config.networkId), eq(true), anyInt()); + verify(mWifiConfigManager).enableNetwork( + eq(config.networkId), eq(true), anyInt(), any()); verify(mWifiConnectivityManager).setUserConnectChoice(eq(config.networkId)); verify(mWifiConnectivityManager).prepareForForcedConnection(eq(config.networkId)); verify(mWifiConfigManager, never()) @@ -912,7 +915,8 @@ public class ClientModeImplTest extends WifiBaseTest { when(mWifiPermissionsUtil.checkNetworkSettingsPermission(Process.myUid())) .thenReturn(false); setupAndStartConnectSequence(config); - verify(mWifiConfigManager).enableNetwork(eq(config.networkId), eq(true), anyInt()); + verify(mWifiConfigManager).enableNetwork( + eq(config.networkId), eq(true), anyInt(), any()); verify(mWifiConnectivityManager, never()).setUserConnectChoice(eq(config.networkId)); verify(mWifiConnectivityManager).prepareForForcedConnection(eq(config.networkId)); verify(mWifiConfigManager).getConfiguredNetworkWithoutMasking(eq(config.networkId)); @@ -1311,7 +1315,8 @@ public class ClientModeImplTest extends WifiBaseTest { WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); config.networkId = FRAMEWORK_NETWORK_ID + 1; setupAndStartConnectSequence(config); - verify(mWifiConfigManager).enableNetwork(eq(config.networkId), eq(true), anyInt()); + verify(mWifiConfigManager).enableNetwork( + eq(config.networkId), eq(true), anyInt(), any()); verify(mWifiConnectivityManager, never()).setUserConnectChoice(eq(config.networkId)); verify(mWifiConnectivityManager).prepareForForcedConnection(eq(config.networkId)); verify(mWifiConfigManager, never()) @@ -1332,7 +1337,7 @@ public class ClientModeImplTest extends WifiBaseTest { mLooper.dispatchAll(); verify(connectActionListener).onFailure(anyInt()); - verify(mWifiConfigManager, never()).enableNetwork(eq(0), eq(true), anyInt()); + verify(mWifiConfigManager, never()).enableNetwork(eq(0), eq(true), anyInt(), any()); verify(mWifiConfigManager, never()).updateLastConnectUid(eq(0), anyInt()); } @@ -1354,7 +1359,7 @@ public class ClientModeImplTest extends WifiBaseTest { mLooper.dispatchAll(); verify(connectActionListener).onSuccess(); - verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt()); + verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt(), any()); mCmi.sendMessage(WifiMonitor.NETWORK_CONNECTION_EVENT, 0, 0, sBSSID); mLooper.dispatchAll(); @@ -1387,7 +1392,7 @@ public class ClientModeImplTest extends WifiBaseTest { mLooper.dispatchAll(); verify(connectActionListener).onSuccess(); - verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt()); + verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt(), any()); mCmi.sendMessage(WifiMonitor.NETWORK_CONNECTION_EVENT, 0, 0, sBSSID); mLooper.dispatchAll(); @@ -1447,7 +1452,7 @@ public class ClientModeImplTest extends WifiBaseTest { mLooper.dispatchAll(); verify(connectActionListener).onSuccess(); - verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt()); + verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt(), any()); mCmi.sendMessage(WifiMonitor.NETWORK_CONNECTION_EVENT, 0, 0, sBSSID); mLooper.dispatchAll(); @@ -1482,7 +1487,7 @@ public class ClientModeImplTest extends WifiBaseTest { mLooper.dispatchAll(); verify(connectActionListener).onSuccess(); - verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt()); + verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt(), any()); WifiConfiguration config = new WifiConfiguration(); config.getNetworkSelectionStatus().setHasEverConnected(true); @@ -1513,7 +1518,7 @@ public class ClientModeImplTest extends WifiBaseTest { mLooper.dispatchAll(); verify(connectActionListener).onSuccess(); - verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt()); + verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt(), any()); WifiConfiguration config = new WifiConfiguration(); config.SSID = sSSID; @@ -1544,7 +1549,7 @@ public class ClientModeImplTest extends WifiBaseTest { mLooper.dispatchAll(); verify(connectActionListener).onSuccess(); - verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt()); + verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt(), any()); when(mWifiConfigManager.getConfiguredNetwork(anyInt())).thenReturn(null); @@ -1573,7 +1578,7 @@ public class ClientModeImplTest extends WifiBaseTest { mLooper.dispatchAll(); verify(connectActionListener).onSuccess(); - verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt()); + verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt(), any()); WifiConfiguration config = new WifiConfiguration(); config.getNetworkSelectionStatus().setHasEverConnected(true); @@ -1602,7 +1607,7 @@ public class ClientModeImplTest extends WifiBaseTest { mLooper.dispatchAll(); verify(connectActionListener).onSuccess(); - verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt()); + verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt(), any()); WifiConfiguration config = new WifiConfiguration(); config.getNetworkSelectionStatus().setHasEverConnected(true); @@ -1630,7 +1635,7 @@ public class ClientModeImplTest extends WifiBaseTest { mLooper.dispatchAll(); verify(connectActionListener).onSuccess(); - verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt()); + verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt(), any()); when(mWifiConfigManager.getConfiguredNetwork(anyInt())).thenReturn(null); @@ -1653,7 +1658,7 @@ public class ClientModeImplTest extends WifiBaseTest { mLooper.dispatchAll(); verify(connectActionListener).onSuccess(); - verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt()); + verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt(), any()); mCmi.sendMessage(WifiMonitor.NETWORK_DISCONNECTION_EVENT, 0, 0, sBSSID); mLooper.dispatchAll(); @@ -1930,7 +1935,8 @@ public class ClientModeImplTest extends WifiBaseTest { public void disconnectFromNetworkWhenRemovedWhileObtainingIpAddr() throws Exception { initializeAndAddNetworkAndVerifySuccess(); - when(mWifiConfigManager.enableNetwork(eq(0), eq(true), anyInt())).thenReturn(true); + when(mWifiConfigManager.enableNetwork(eq(0), eq(true), anyInt(), any())) + .thenReturn(true); when(mWifiConfigManager.updateLastConnectUid(eq(0), anyInt())).thenReturn(true); verify(mWifiNative).removeAllNetworks(WIFI_IFACE_NAME); @@ -1940,7 +1946,7 @@ public class ClientModeImplTest extends WifiBaseTest { mLooper.dispatchAll(); verify(connectActionListener).onSuccess(); - verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt()); + verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt(), any()); verify(mWifiConnectivityManager).setUserConnectChoice(eq(0)); when(mWifiConfigManager.getScanDetailCacheForNetwork(FRAMEWORK_NETWORK_ID)) .thenReturn(mScanDetailCache); @@ -1961,13 +1967,13 @@ public class ClientModeImplTest extends WifiBaseTest { // now remove the config reset(connectActionListener); - when(mWifiConfigManager.removeNetwork(eq(FRAMEWORK_NETWORK_ID), anyInt())) + when(mWifiConfigManager.removeNetwork(eq(FRAMEWORK_NETWORK_ID), anyInt(), any())) .thenReturn(true); mCmi.forget(FRAMEWORK_NETWORK_ID, mock(Binder.class), connectActionListener, 0, Binder.getCallingUid()); mLooper.dispatchAll(); verify(connectActionListener).onSuccess(); - verify(mWifiConfigManager).removeNetwork(eq(FRAMEWORK_NETWORK_ID), anyInt()); + verify(mWifiConfigManager).removeNetwork(eq(FRAMEWORK_NETWORK_ID), anyInt(), any()); // trigger removal callback to trigger disconnect. WifiConfiguration removedConfig = new WifiConfiguration(); removedConfig.networkId = FRAMEWORK_NETWORK_ID; @@ -3623,7 +3629,7 @@ public class ClientModeImplTest extends WifiBaseTest { mLooper.dispatchAll(); verify(connectActionListener).onSuccess(); - verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt()); + verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt(), any()); mCmi.sendMessage(WifiMonitor.NETWORK_CONNECTION_EVENT, 0, 0, sBSSID); mLooper.dispatchAll(); diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java index 3fd8f2ce4..b98ed544e 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java @@ -20,8 +20,6 @@ import static org.junit.Assert.*; import static org.mockito.Mockito.*; import android.annotation.Nullable; -import android.app.admin.DeviceAdminInfo; -import android.app.admin.DevicePolicyManagerInternal; import android.app.test.MockAnswerUtil.AnswerWithArguments; import android.content.Context; import android.content.Intent; @@ -121,7 +119,6 @@ public class WifiConfigManagerTest extends WifiBaseTest { @Mock private WifiKeyStore mWifiKeyStore; @Mock private WifiConfigStore mWifiConfigStore; @Mock private PackageManager mPackageManager; - @Mock private DevicePolicyManagerInternal mDevicePolicyManagerInternal; @Mock private WifiPermissionsUtil mWifiPermissionsUtil; @Mock private WifiPermissionsWrapper mWifiPermissionsWrapper; @Mock private WifiInjector mWifiInjector; @@ -206,11 +203,9 @@ public class WifiConfigManagerTest extends WifiBaseTest { when(mWifiConfigStore.areStoresPresent()).thenReturn(true); setupStoreDataForRead(new ArrayList<>(), new ArrayList<>(), new HashMap<>()); - when(mDevicePolicyManagerInternal.isActiveAdminWithPolicy(anyInt(), anyInt())) - .thenReturn(false); when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(true); - when(mWifiPermissionsWrapper.getDevicePolicyManagerInternal()) - .thenReturn(mDevicePolicyManagerInternal); + when(mWifiPermissionsUtil.isDeviceOwner(anyInt(), any())).thenReturn(false); + when(mWifiPermissionsUtil.isProfileOwner(anyInt(), any())).thenReturn(false); when(mWifiInjector.getWifiLastResortWatchdog()).thenReturn(mWifiLastResortWatchdog); when(mWifiInjector.getWifiLastResortWatchdog().shouldIgnoreSsidUpdate()) .thenReturn(false); @@ -440,7 +435,8 @@ public class WifiConfigManagerTest extends WifiBaseTest { // This MAC should be different from the default, uninitialized randomized MAC. assertNotEquals(defaultMac, randMac); - assertTrue(mWifiConfigManager.removeNetwork(openNetwork.networkId, TEST_CREATOR_UID)); + assertTrue(mWifiConfigManager.removeNetwork( + openNetwork.networkId, TEST_CREATOR_UID, TEST_CREATOR_NAME)); assertTrue(mWifiConfigManager.getConfiguredNetworks().isEmpty()); // Adds the network back again and verify randomized MAC address stays the same. @@ -489,8 +485,7 @@ public class WifiConfigManagerTest extends WifiBaseTest { ArgumentCaptor.forClass(WifiConfiguration.class); when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(false); when(mWifiPermissionsUtil.checkNetworkSetupWizardPermission(anyInt())).thenReturn(false); - when(mDevicePolicyManagerInternal.isActiveAdminWithPolicy(anyInt(), anyInt())) - .thenReturn(true); + when(mWifiPermissionsUtil.isDeviceOwner(anyInt(), any())).thenReturn(true); WifiConfiguration openNetwork = WifiConfigurationTestUtil.createOpenNetwork(); openNetwork.macRandomizationSetting = WifiConfiguration.RANDOMIZATION_PERSISTENT; @@ -550,8 +545,7 @@ public class WifiConfigManagerTest extends WifiBaseTest { ArgumentCaptor.forClass(WifiConfiguration.class); when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(false); when(mWifiPermissionsUtil.checkNetworkSetupWizardPermission(anyInt())).thenReturn(true); - when(mDevicePolicyManagerInternal.isActiveAdminWithPolicy(anyInt(), anyInt())) - .thenReturn(true); + when(mWifiPermissionsUtil.isDeviceOwner(anyInt(), any())).thenReturn(true); WifiConfiguration openNetwork = WifiConfigurationTestUtil.createOpenNetwork(); openNetwork.macRandomizationSetting = WifiConfiguration.RANDOMIZATION_PERSISTENT; List<WifiConfiguration> networks = new ArrayList<>(); @@ -824,8 +818,7 @@ public class WifiConfigManagerTest extends WifiBaseTest { // Configure mock DevicePolicyManager to give Profile Owner permission so that we can modify // proxy settings on a configuration - when(mDevicePolicyManagerInternal.isActiveAdminWithPolicy(anyInt(), - eq(DeviceAdminInfo.USES_POLICY_PROFILE_OWNER))).thenReturn(true); + when(mWifiPermissionsUtil.isProfileOwner(anyInt(), any())).thenReturn(true); // Change the IpConfiguration now and ensure that the IP configuration flags are set now. assertAndSetNetworkIpConfiguration( @@ -926,7 +919,8 @@ public class WifiConfigManagerTest extends WifiBaseTest { // Ensure that configured network list is not empty. assertFalse(mWifiConfigManager.getConfiguredNetworks().isEmpty()); - assertTrue(mWifiConfigManager.removeNetwork(passpointNetwork.networkId, Process.WIFI_UID)); + assertTrue(mWifiConfigManager.removeNetwork( + passpointNetwork.networkId, Process.WIFI_UID, null)); // Verify keys are not being removed. verify(mWifiKeyStore, never()).removeKeys(any(WifiEnterpriseConfig.class)); @@ -970,8 +964,7 @@ public class WifiConfigManagerTest extends WifiBaseTest { // Configure mock DevicePolicyManager to give Profile Owner permission so that we can modify // proxy settings on a configuration - when(mDevicePolicyManagerInternal.isActiveAdminWithPolicy(anyInt(), - eq(DeviceAdminInfo.USES_POLICY_PROFILE_OWNER))).thenReturn(true); + when(mWifiPermissionsUtil.isProfileOwner(anyInt(), any())).thenReturn(true); verifyUpdateNetworkToWifiConfigManagerWithoutIpChange(openNetwork); verifyUpdateNetworkToWifiConfigManagerWithoutIpChange(pskNetwork); @@ -1190,7 +1183,7 @@ public class WifiConfigManagerTest extends WifiBaseTest { NetworkUpdateResult result = verifyAddNetworkToWifiConfigManager(openNetwork); assertTrue(mWifiConfigManager.enableNetwork( - result.getNetworkId(), false, TEST_CREATOR_UID)); + result.getNetworkId(), false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); WifiConfiguration retrievedNetwork = mWifiConfigManager.getConfiguredNetwork(result.getNetworkId()); NetworkSelectionStatus retrievedStatus = retrievedNetwork.getNetworkSelectionStatus(); @@ -1199,7 +1192,8 @@ public class WifiConfigManagerTest extends WifiBaseTest { mContextConfigStoreMockOrder.verify(mWifiConfigStore).write(eq(true)); // Now set it disabled. - assertTrue(mWifiConfigManager.disableNetwork(result.getNetworkId(), TEST_CREATOR_UID)); + assertTrue(mWifiConfigManager.disableNetwork( + result.getNetworkId(), TEST_CREATOR_UID, TEST_CREATOR_NAME)); retrievedNetwork = mWifiConfigManager.getConfiguredNetwork(result.getNetworkId()); retrievedStatus = retrievedNetwork.getNetworkSelectionStatus(); assertTrue(retrievedStatus.isNetworkPermanentlyDisabled()); @@ -1225,7 +1219,7 @@ public class WifiConfigManagerTest extends WifiBaseTest { // Now try to set it enable with |TEST_UPDATE_UID|, it should fail and the network // should remain disabled. assertFalse(mWifiConfigManager.enableNetwork( - result.getNetworkId(), true, TEST_UPDATE_UID)); + result.getNetworkId(), true, TEST_UPDATE_UID, TEST_UPDATE_NAME)); WifiConfiguration retrievedNetwork = mWifiConfigManager.getConfiguredNetwork(result.getNetworkId()); NetworkSelectionStatus retrievedStatus = retrievedNetwork.getNetworkSelectionStatus(); @@ -1248,13 +1242,14 @@ public class WifiConfigManagerTest extends WifiBaseTest { WifiConfiguration openNetwork = WifiConfigurationTestUtil.createOpenNetwork(); NetworkUpdateResult result = verifyAddNetworkToWifiConfigManager(openNetwork); assertTrue(mWifiConfigManager.enableNetwork( - result.getNetworkId(), true, TEST_CREATOR_UID)); + result.getNetworkId(), true, TEST_CREATOR_UID, TEST_CREATOR_NAME)); assertEquals(result.getNetworkId(), mWifiConfigManager.getLastSelectedNetwork()); // Now try to set it disabled with |TEST_UPDATE_UID|, it should fail and the network // should remain enabled. - assertFalse(mWifiConfigManager.disableNetwork(result.getNetworkId(), TEST_UPDATE_UID)); + assertFalse(mWifiConfigManager.disableNetwork( + result.getNetworkId(), TEST_UPDATE_UID, TEST_CREATOR_NAME)); WifiConfiguration retrievedNetwork = mWifiConfigManager.getConfiguredNetwork(result.getNetworkId()); NetworkSelectionStatus retrievedStatus = retrievedNetwork.getNetworkSelectionStatus(); @@ -1304,7 +1299,8 @@ public class WifiConfigManagerTest extends WifiBaseTest { @Test public void testRemoveNetworkWithEmptyConfigStore() { int networkId = new Random().nextInt(); - assertFalse(mWifiConfigManager.removeNetwork(networkId, TEST_CREATOR_UID)); + assertFalse(mWifiConfigManager.removeNetwork( + networkId, TEST_CREATOR_UID, TEST_CREATOR_NAME)); } /** @@ -1320,7 +1316,8 @@ public class WifiConfigManagerTest extends WifiBaseTest { // Change the networkID to an invalid one. openNetwork.networkId++; - assertFalse(mWifiConfigManager.removeNetwork(openNetwork.networkId, TEST_CREATOR_UID)); + assertFalse(mWifiConfigManager.removeNetwork( + openNetwork.networkId, TEST_CREATOR_UID, TEST_CREATOR_NAME)); } /** @@ -1338,8 +1335,9 @@ public class WifiConfigManagerTest extends WifiBaseTest { NetworkUpdateResult result = verifyAddNetworkToWifiConfigManager(openNetwork); assertFalse(mWifiConfigManager.enableNetwork( - result.getNetworkId() + 1, false, TEST_CREATOR_UID)); - assertFalse(mWifiConfigManager.disableNetwork(result.getNetworkId() + 1, TEST_CREATOR_UID)); + result.getNetworkId() + 1, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); + assertFalse(mWifiConfigManager.disableNetwork( + result.getNetworkId() + 1, TEST_CREATOR_UID, TEST_CREATOR_NAME)); assertFalse(mWifiConfigManager.updateNetworkSelectionStatus( result.getNetworkId() + 1, NetworkSelectionStatus.DISABLED_BY_WIFI_MANAGER)); assertFalse(mWifiConfigManager.updateLastConnectUid( @@ -2198,9 +2196,12 @@ public class WifiConfigManagerTest extends WifiBaseTest { verifyAddNetworkToWifiConfigManager(network3); // Enable all of them. - assertTrue(mWifiConfigManager.enableNetwork(network1.networkId, false, TEST_CREATOR_UID)); - assertTrue(mWifiConfigManager.enableNetwork(network2.networkId, false, TEST_CREATOR_UID)); - assertTrue(mWifiConfigManager.enableNetwork(network3.networkId, false, TEST_CREATOR_UID)); + assertTrue(mWifiConfigManager.enableNetwork( + network1.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); + assertTrue(mWifiConfigManager.enableNetwork( + network2.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); + assertTrue(mWifiConfigManager.enableNetwork( + network3.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); // Now set scan results in 2 of them to set the corresponding // {@link NetworkSelectionStatus#mSeenInLastQualifiedNetworkSelection} field. @@ -2222,7 +2223,8 @@ public class WifiConfigManagerTest extends WifiBaseTest { assertEquals(network2.SSID, pnoNetworks.get(2).ssid); // Now permanently disable |network3|. This should remove network 3 from the list. - assertTrue(mWifiConfigManager.disableNetwork(network3.networkId, TEST_CREATOR_UID)); + assertTrue(mWifiConfigManager.disableNetwork( + network3.networkId, TEST_CREATOR_UID, TEST_CREATOR_NAME)); // Retrieve the Pno network list again & verify the order of the networks returned. pnoNetworks = mWifiConfigManager.retrievePnoNetworkList(); @@ -2248,8 +2250,10 @@ public class WifiConfigManagerTest extends WifiBaseTest { verifyAddNetworkToWifiConfigManager(network2); // Enable all of them. - assertTrue(mWifiConfigManager.enableNetwork(network1.networkId, false, TEST_CREATOR_UID)); - assertTrue(mWifiConfigManager.enableNetwork(network2.networkId, false, TEST_CREATOR_UID)); + assertTrue(mWifiConfigManager.enableNetwork( + network1.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); + assertTrue(mWifiConfigManager.enableNetwork( + network2.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); assertTrue(mWifiConfigManager.updateNetworkAfterConnect(network1.networkId)); // Retrieve the Pno network list & verify the order of the networks returned. @@ -2308,7 +2312,8 @@ public class WifiConfigManagerTest extends WifiBaseTest { mContentObserverPnoChannelCulling.onChange(false); WifiConfiguration network1 = WifiConfigurationTestUtil.createEapNetwork(); verifyAddNetworkToWifiConfigManager(network1); - assertTrue(mWifiConfigManager.enableNetwork(network1.networkId, false, TEST_CREATOR_UID)); + assertTrue(mWifiConfigManager.enableNetwork( + network1.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); assertTrue(mWifiConfigManager.updateNetworkAfterConnect(network1.networkId)); ScanDetail scanDetail1 = createScanDetailForNetwork(network1, TEST_BSSID + "1", TEST_RSSI, TEST_FREQUENCY_1); @@ -2339,9 +2344,12 @@ public class WifiConfigManagerTest extends WifiBaseTest { verifyAddNetworkToWifiConfigManager(network3); // Enable all of them. - assertTrue(mWifiConfigManager.enableNetwork(network1.networkId, false, TEST_CREATOR_UID)); - assertTrue(mWifiConfigManager.enableNetwork(network2.networkId, false, TEST_CREATOR_UID)); - assertTrue(mWifiConfigManager.enableNetwork(network3.networkId, false, TEST_CREATOR_UID)); + assertTrue(mWifiConfigManager.enableNetwork( + network1.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); + assertTrue(mWifiConfigManager.enableNetwork( + network2.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); + assertTrue(mWifiConfigManager.enableNetwork( + network3.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); long firstConnectionTimeMillis = 45677; long secondConnectionTimeMillis = firstConnectionTimeMillis + 45; @@ -2382,9 +2390,12 @@ public class WifiConfigManagerTest extends WifiBaseTest { verifyAddNetworkToWifiConfigManager(network3); // Enable all of them. - assertTrue(mWifiConfigManager.enableNetwork(network1.networkId, false, TEST_CREATOR_UID)); - assertTrue(mWifiConfigManager.enableNetwork(network2.networkId, false, TEST_CREATOR_UID)); - assertTrue(mWifiConfigManager.enableNetwork(network3.networkId, false, TEST_CREATOR_UID)); + assertTrue(mWifiConfigManager.enableNetwork( + network1.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); + assertTrue(mWifiConfigManager.enableNetwork( + network2.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); + assertTrue(mWifiConfigManager.enableNetwork( + network3.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); long firstConnectionTimeMillis = 45677; long secondConnectionTimeMillis = firstConnectionTimeMillis + 45; @@ -2439,9 +2450,12 @@ public class WifiConfigManagerTest extends WifiBaseTest { verifyAddNetworkToWifiConfigManager(network3); // Enable all of them. - assertTrue(mWifiConfigManager.enableNetwork(network1.networkId, false, TEST_CREATOR_UID)); - assertTrue(mWifiConfigManager.enableNetwork(network2.networkId, false, TEST_CREATOR_UID)); - assertTrue(mWifiConfigManager.enableNetwork(network3.networkId, false, TEST_CREATOR_UID)); + assertTrue(mWifiConfigManager.enableNetwork( + network1.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); + assertTrue(mWifiConfigManager.enableNetwork( + network2.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); + assertTrue(mWifiConfigManager.enableNetwork( + network3.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); long firstConnectionTimeMillis = 45677; long secondConnectionTimeMillis = firstConnectionTimeMillis + 45; @@ -2482,11 +2496,11 @@ public class WifiConfigManagerTest extends WifiBaseTest { // Enable all of them. assertTrue(mWifiConfigManager.enableNetwork( - savedOpenNetwork.networkId, false, TEST_CREATOR_UID)); + savedOpenNetwork.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); assertTrue(mWifiConfigManager.enableNetwork( - ephemeralNetwork.networkId, false, TEST_CREATOR_UID)); + ephemeralNetwork.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); assertTrue(mWifiConfigManager.enableNetwork( - passpointNetwork.networkId, false, TEST_CREATOR_UID)); + passpointNetwork.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); // Retrieve the Pno network list & verify the order of the networks returned. List<WifiScanner.PnoSettings.PnoNetwork> pnoNetworks = @@ -3750,22 +3764,24 @@ public class WifiConfigManagerTest extends WifiBaseTest { when(mClock.getElapsedSinceBootMillis()).thenReturn(67L); assertTrue(mWifiConfigManager.enableNetwork( - result.getNetworkId(), true, TEST_CREATOR_UID)); + result.getNetworkId(), true, TEST_CREATOR_UID, TEST_CREATOR_NAME)); assertEquals(result.getNetworkId(), mWifiConfigManager.getLastSelectedNetwork()); assertEquals(67, mWifiConfigManager.getLastSelectedTimeStamp()); // Now disable the network and ensure that the last selected flag is cleared. - assertTrue(mWifiConfigManager.disableNetwork(result.getNetworkId(), TEST_CREATOR_UID)); + assertTrue(mWifiConfigManager.disableNetwork( + result.getNetworkId(), TEST_CREATOR_UID, TEST_CREATOR_NAME)); assertEquals( WifiConfiguration.INVALID_NETWORK_ID, mWifiConfigManager.getLastSelectedNetwork()); // Enable it again and remove the network to ensure that the last selected flag was cleared. assertTrue(mWifiConfigManager.enableNetwork( - result.getNetworkId(), true, TEST_CREATOR_UID)); + result.getNetworkId(), true, TEST_CREATOR_UID, TEST_CREATOR_NAME)); assertEquals(result.getNetworkId(), mWifiConfigManager.getLastSelectedNetwork()); assertEquals(openNetwork.configKey(), mWifiConfigManager.getLastSelectedNetworkConfigKey()); - assertTrue(mWifiConfigManager.removeNetwork(result.getNetworkId(), TEST_CREATOR_UID)); + assertTrue(mWifiConfigManager.removeNetwork( + result.getNetworkId(), TEST_CREATOR_UID, TEST_CREATOR_NAME)); assertEquals( WifiConfiguration.INVALID_NETWORK_ID, mWifiConfigManager.getLastSelectedNetwork()); } @@ -3836,14 +3852,16 @@ public class WifiConfigManagerTest extends WifiBaseTest { retrievedNetwork.getNetworkSelectionStatus().getConnectChoice()); // Remove network 3 and ensure that the connect choice on network 1 is not removed. - assertTrue(mWifiConfigManager.removeNetwork(network3.networkId, TEST_CREATOR_UID)); + assertTrue(mWifiConfigManager.removeNetwork( + network3.networkId, TEST_CREATOR_UID, TEST_CREATOR_NAME)); retrievedNetwork = mWifiConfigManager.getConfiguredNetwork(network1.networkId); assertEquals( network2.configKey(), retrievedNetwork.getNetworkSelectionStatus().getConnectChoice()); // Now remove network 2 and ensure that the connect choice on network 1 is removed.. - assertTrue(mWifiConfigManager.removeNetwork(network2.networkId, TEST_CREATOR_UID)); + assertTrue(mWifiConfigManager.removeNetwork( + network2.networkId, TEST_CREATOR_UID, TEST_CREATOR_NAME)); retrievedNetwork = mWifiConfigManager.getConfiguredNetwork(network1.networkId); assertNotEquals( network2.configKey(), @@ -4742,12 +4760,10 @@ public class WifiConfigManagerTest extends WifiBaseTest { network = mWifiConfigManager.getConfiguredNetwork(networkId); } network.setIpConfiguration(ipConfiguration); - when(mDevicePolicyManagerInternal.isActiveAdminWithPolicy(anyInt(), - eq(DeviceAdminInfo.USES_POLICY_PROFILE_OWNER))) - .thenReturn(withProfileOwnerPolicy); - when(mDevicePolicyManagerInternal.isActiveAdminWithPolicy(anyInt(), - eq(DeviceAdminInfo.USES_POLICY_DEVICE_OWNER))) + when(mWifiPermissionsUtil.isDeviceOwner(anyInt(), any())) .thenReturn(withDeviceOwnerPolicy); + when(mWifiPermissionsUtil.isProfileOwner(anyInt(), any())) + .thenReturn(withProfileOwnerPolicy); when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())) .thenReturn(withNetworkSettings); when(mWifiPermissionsUtil.checkNetworkSetupWizardPermission(anyInt())) @@ -5237,7 +5253,8 @@ public class WifiConfigManagerTest extends WifiBaseTest { */ private void verifyRemoveNetworkFromWifiConfigManager( WifiConfiguration configuration) { - assertTrue(mWifiConfigManager.removeNetwork(configuration.networkId, TEST_CREATOR_UID)); + assertTrue(mWifiConfigManager.removeNetwork( + configuration.networkId, TEST_CREATOR_UID, TEST_CREATOR_NAME)); verifyNetworkRemoveBroadcast(configuration); // Verify if the config store write was triggered without this new configuration. @@ -5249,7 +5266,8 @@ public class WifiConfigManagerTest extends WifiBaseTest { */ private void verifyRemoveEphemeralNetworkFromWifiConfigManager( WifiConfiguration configuration) throws Exception { - assertTrue(mWifiConfigManager.removeNetwork(configuration.networkId, TEST_CREATOR_UID)); + assertTrue(mWifiConfigManager.removeNetwork( + configuration.networkId, TEST_CREATOR_UID, TEST_CREATOR_NAME)); verifyNetworkRemoveBroadcast(configuration); // Ensure that the write was not invoked for ephemeral network remove. @@ -5261,7 +5279,8 @@ public class WifiConfigManagerTest extends WifiBaseTest { */ private void verifyRemovePasspointNetworkFromWifiConfigManager( WifiConfiguration configuration) throws Exception { - assertTrue(mWifiConfigManager.removeNetwork(configuration.networkId, TEST_CREATOR_UID)); + assertTrue(mWifiConfigManager.removeNetwork( + configuration.networkId, TEST_CREATOR_UID, TEST_CREATOR_NAME)); // Verify keys are not being removed. verify(mWifiKeyStore, never()).removeKeys(any(WifiEnterpriseConfig.class)); diff --git a/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java b/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java index a0c965a54..fe75f5bc5 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java @@ -1638,6 +1638,7 @@ public class WifiNetworkFactoryTest extends WifiBaseTest { WifiConfiguration wcmNetwork = new WifiConfiguration(mSelectedNetwork); wcmNetwork.networkId = TEST_NETWORK_ID_1; wcmNetwork.creatorUid = TEST_UID_1; + wcmNetwork.creatorName = TEST_PACKAGE_NAME_1; wcmNetwork.fromWifiNetworkSpecifier = true; wcmNetwork.ephemeral = true; when(mWifiConfigManager.getConfiguredNetwork(mSelectedNetwork.configKey())) @@ -1645,7 +1646,8 @@ public class WifiNetworkFactoryTest extends WifiBaseTest { mWifiNetworkFactory.releaseNetworkFor(mNetworkRequest); // Verify that we triggered a disconnect. verify(mClientModeImpl, times(2)).disconnectCommand(); - verify(mWifiConfigManager).removeNetwork(TEST_NETWORK_ID_1, TEST_UID_1); + verify(mWifiConfigManager).removeNetwork( + TEST_NETWORK_ID_1, TEST_UID_1, TEST_PACKAGE_NAME_1); // Re-enable connectivity manager . verify(mWifiConnectivityManager).setSpecificNetworkRequestInProgress(false); } diff --git a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java index 6f33a45f4..bc9d65b74 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java @@ -72,8 +72,6 @@ import static org.mockito.Mockito.when; import android.Manifest; import android.app.ActivityManager; import android.app.AppOpsManager; -import android.app.admin.DeviceAdminInfo; -import android.app.admin.DevicePolicyManagerInternal; import android.app.test.MockAnswerUtil; import android.content.BroadcastReceiver; import android.content.ContentResolver; @@ -252,7 +250,6 @@ public class WifiServiceImplTest extends WifiBaseTest { @Mock ITrafficStateCallback mTrafficStateCallback; @Mock INetworkRequestMatchCallback mNetworkRequestMatchCallback; @Mock WifiNetworkSuggestionsManager mWifiNetworkSuggestionsManager; - @Mock DevicePolicyManagerInternal mDevicePolicyManagerInternal; @Mock TelephonyManager mTelephonyManager; @Mock IOnWifiUsabilityStatsListener mOnWifiUsabilityStatsListener; @Mock WifiConfigManager mWifiConfigManager; @@ -304,8 +301,6 @@ public class WifiServiceImplTest extends WifiBaseTest { WifiAsyncChannel wifiAsyncChannel = new WifiAsyncChannel("WifiServiceImplTest"); wifiAsyncChannel.setWifiLog(mLog); when(mFrameworkFacade.makeWifiAsyncChannel(anyString())).thenReturn(wifiAsyncChannel); - when(mWifiPermissionsWrapper.getDevicePolicyManagerInternal()) - .thenReturn(mDevicePolicyManagerInternal); when(mWifiInjector.getFrameworkFacade()).thenReturn(mFrameworkFacade); when(mWifiInjector.getWifiLockManager()).thenReturn(mLockManager); when(mWifiInjector.getWifiMulticastLockManager()).thenReturn(mWifiMulticastLockManager); @@ -421,7 +416,7 @@ public class WifiServiceImplTest extends WifiBaseTest { .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); - when(mWifiConfigManager.removeNetwork(anyInt(), anyInt())).thenReturn(false); + when(mWifiConfigManager.removeNetwork(anyInt(), anyInt(), anyString())).thenReturn(false); mLooper.startAutoDispatch(); boolean succeeded = mWifiServiceImpl.removeNetwork(0, TEST_PACKAGE_NAME); @@ -568,8 +563,7 @@ public class WifiServiceImplTest extends WifiBaseTest { .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(false); - when(mDevicePolicyManagerInternal.isActiveAdminWithPolicy( - Process.myUid(), DeviceAdminInfo.USES_POLICY_DEVICE_OWNER)) + when(mWifiPermissionsUtil.isDeviceOwner(Binder.getCallingUid(), TEST_PACKAGE_NAME)) .thenReturn(true); when(mSettingsStore.handleWifiToggled(eq(true))).thenReturn(true); @@ -828,8 +822,7 @@ public class WifiServiceImplTest extends WifiBaseTest { .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(false); - when(mDevicePolicyManagerInternal.isActiveAdminWithPolicy( - Process.myUid(), DeviceAdminInfo.USES_POLICY_PROFILE_OWNER)) + when(mWifiPermissionsUtil.isProfileOwner(Binder.getCallingUid(), TEST_PACKAGE_NAME)) .thenReturn(true); when(mSettingsStore.handleWifiToggled(eq(false))).thenReturn(true); @@ -3287,7 +3280,8 @@ public class WifiServiceImplTest extends WifiBaseTest { // Let the final post inside the |factoryReset| method run to completion. mLooper.dispatchAll(); - verify(mWifiConfigManager).removeNetwork(network.networkId, Binder.getCallingUid()); + verify(mWifiConfigManager).removeNetwork( + network.networkId, Binder.getCallingUid(), TEST_PACKAGE_NAME); verify(mPasspointManager).removeProvider(fqdn); verify(mWifiConfigManager).clearDeletedEphemeralNetworks(); verify(mClientModeImpl).clearNetworkRequestUserApprovedAccessPoints(); @@ -3321,7 +3315,7 @@ public class WifiServiceImplTest extends WifiBaseTest { public void testAddOrUpdateNetworkIsNotAllowedForAppsTargetingQSdk() throws Exception { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); - when(mWifiConfigManager.addOrUpdateNetwork(any(), anyInt())).thenReturn( + when(mWifiConfigManager.addOrUpdateNetwork(any(), anyInt(), any())).thenReturn( new NetworkUpdateResult(0)); WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); @@ -3330,7 +3324,7 @@ public class WifiServiceImplTest extends WifiBaseTest { mLooper.stopAutoDispatchAndIgnoreExceptions(); verifyCheckChangePermission(TEST_PACKAGE_NAME); - verify(mWifiConfigManager, never()).addOrUpdateNetwork(any(), anyInt()); + verify(mWifiConfigManager, never()).addOrUpdateNetwork(any(), anyInt(), any()); verify(mWifiMetrics, never()).incrementNumAddOrUpdateNetworkCalls(); } @@ -3341,7 +3335,7 @@ public class WifiServiceImplTest extends WifiBaseTest { public void testAddOrUpdateNetworkIsAllowedForAppsTargetingBelowQSdk() throws Exception { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); - when(mWifiConfigManager.addOrUpdateNetwork(any(), anyInt())).thenReturn( + when(mWifiConfigManager.addOrUpdateNetwork(any(), anyInt(), any())).thenReturn( new NetworkUpdateResult(0)); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); @@ -3352,7 +3346,7 @@ public class WifiServiceImplTest extends WifiBaseTest { mLooper.stopAutoDispatch(); verifyCheckChangePermission(TEST_PACKAGE_NAME); - verify(mWifiConfigManager).addOrUpdateNetwork(any(), anyInt()); + verify(mWifiConfigManager).addOrUpdateNetwork(any(), anyInt(), any()); verify(mWifiMetrics).incrementNumAddOrUpdateNetworkCalls(); } @@ -3364,7 +3358,7 @@ public class WifiServiceImplTest extends WifiBaseTest { when(mContext.checkPermission(eq(android.Manifest.permission.NETWORK_SETTINGS), anyInt(), anyInt())).thenReturn(PackageManager.PERMISSION_GRANTED); mApplicationInfo.targetSdkVersion = Build.VERSION_CODES.P; - when(mWifiConfigManager.addOrUpdateNetwork(any(), anyInt())).thenReturn( + when(mWifiConfigManager.addOrUpdateNetwork(any(), anyInt(), any())).thenReturn( new NetworkUpdateResult(0)); WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); @@ -3377,7 +3371,7 @@ public class WifiServiceImplTest extends WifiBaseTest { android.Manifest.permission.CHANGE_WIFI_STATE, "WifiService"); verify(mAppOpsManager, never()).noteOp( AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); - verify(mWifiConfigManager).addOrUpdateNetwork(any(), anyInt()); + verify(mWifiConfigManager).addOrUpdateNetwork(any(), anyInt(), any()); verify(mWifiMetrics).incrementNumAddOrUpdateNetworkCalls(); } @@ -3389,7 +3383,7 @@ public class WifiServiceImplTest extends WifiBaseTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); mApplicationInfo.flags = ApplicationInfo.FLAG_SYSTEM; - when(mWifiConfigManager.addOrUpdateNetwork(any(), anyInt())).thenReturn( + when(mWifiConfigManager.addOrUpdateNetwork(any(), anyInt(), any())).thenReturn( new NetworkUpdateResult(0)); WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); @@ -3398,7 +3392,7 @@ public class WifiServiceImplTest extends WifiBaseTest { mLooper.stopAutoDispatch(); verifyCheckChangePermission(TEST_PACKAGE_NAME); - verify(mWifiConfigManager).addOrUpdateNetwork(any(), anyInt()); + verify(mWifiConfigManager).addOrUpdateNetwork(any(), anyInt(), any()); verify(mWifiMetrics).incrementNumAddOrUpdateNetworkCalls(); } @@ -3412,7 +3406,7 @@ public class WifiServiceImplTest extends WifiBaseTest { when(mWifiPermissionsUtil.checkSystemAlertWindowPermission( Process.myUid(), TEST_PACKAGE_NAME)).thenReturn(true); - when(mWifiConfigManager.addOrUpdateNetwork(any(), anyInt())).thenReturn( + when(mWifiConfigManager.addOrUpdateNetwork(any(), anyInt(), any())).thenReturn( new NetworkUpdateResult(0)); WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); @@ -3422,7 +3416,7 @@ public class WifiServiceImplTest extends WifiBaseTest { verifyCheckChangePermission(TEST_PACKAGE_NAME); verify(mWifiPermissionsUtil).checkSystemAlertWindowPermission(anyInt(), anyString()); - verify(mWifiConfigManager).addOrUpdateNetwork(any(), anyInt()); + verify(mWifiConfigManager).addOrUpdateNetwork(any(), anyInt(), any()); verify(mWifiMetrics).incrementNumAddOrUpdateNetworkCalls(); } @@ -3433,10 +3427,9 @@ public class WifiServiceImplTest extends WifiBaseTest { public void testAddOrUpdateNetworkIsAllowedForDOApp() throws Exception { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); - when(mDevicePolicyManagerInternal.isActiveAdminWithPolicy( - Process.myUid(), DeviceAdminInfo.USES_POLICY_DEVICE_OWNER)) + when(mWifiPermissionsUtil.isDeviceOwner(Binder.getCallingUid(), TEST_PACKAGE_NAME)) .thenReturn(true); - when(mWifiConfigManager.addOrUpdateNetwork(any(), anyInt())).thenReturn( + when(mWifiConfigManager.addOrUpdateNetwork(any(), anyInt(), any())).thenReturn( new NetworkUpdateResult(0)); WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); @@ -3445,7 +3438,7 @@ public class WifiServiceImplTest extends WifiBaseTest { mLooper.stopAutoDispatch(); verifyCheckChangePermission(TEST_PACKAGE_NAME); - verify(mWifiConfigManager).addOrUpdateNetwork(any(), anyInt()); + verify(mWifiConfigManager).addOrUpdateNetwork(any(), anyInt(), any()); verify(mWifiMetrics).incrementNumAddOrUpdateNetworkCalls(); } @@ -3456,10 +3449,9 @@ public class WifiServiceImplTest extends WifiBaseTest { public void testAddOrUpdateNetworkIsAllowedForPOApp() throws Exception { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); - when(mDevicePolicyManagerInternal.isActiveAdminWithPolicy( - Process.myUid(), DeviceAdminInfo.USES_POLICY_PROFILE_OWNER)) + when(mWifiPermissionsUtil.isProfileOwner(Binder.getCallingUid(), TEST_PACKAGE_NAME)) .thenReturn(true); - when(mWifiConfigManager.addOrUpdateNetwork(any(), anyInt())).thenReturn( + when(mWifiConfigManager.addOrUpdateNetwork(any(), anyInt(), any())).thenReturn( new NetworkUpdateResult(0)); WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); @@ -3468,7 +3460,7 @@ public class WifiServiceImplTest extends WifiBaseTest { mLooper.stopAutoDispatch(); verifyCheckChangePermission(TEST_PACKAGE_NAME); - verify(mWifiConfigManager).addOrUpdateNetwork(any(), anyInt()); + verify(mWifiConfigManager).addOrUpdateNetwork(any(), anyInt(), any()); verify(mWifiMetrics).incrementNumAddOrUpdateNetworkCalls(); } @@ -3542,12 +3534,13 @@ public class WifiServiceImplTest extends WifiBaseTest { when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); - when(mWifiConfigManager.enableNetwork(anyInt(), anyBoolean(), anyInt())).thenReturn(true); + when(mWifiConfigManager.enableNetwork(anyInt(), anyBoolean(), anyInt(), anyString())) + .thenReturn(true); mLooper.startAutoDispatch(); assertTrue(mWifiServiceImpl.enableNetwork(TEST_NETWORK_ID, false, TEST_PACKAGE_NAME)); mLooper.stopAutoDispatch(); verify(mWifiConfigManager).enableNetwork(eq(TEST_NETWORK_ID), eq(false), - eq(Binder.getCallingUid())); + eq(Binder.getCallingUid()), eq(TEST_PACKAGE_NAME)); verify(mWifiMetrics).incrementNumEnableNetworkCalls(); } @@ -3982,7 +3975,7 @@ public class WifiServiceImplTest extends WifiBaseTest { public void testRemoveNetworkIsAllowedForAppsTargetingBelowQSdk() { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); - when(mWifiConfigManager.removeNetwork(eq(0), anyInt())).thenReturn(true); + when(mWifiConfigManager.removeNetwork(eq(0), anyInt(), anyString())).thenReturn(true); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); @@ -3991,7 +3984,7 @@ public class WifiServiceImplTest extends WifiBaseTest { mLooper.stopAutoDispatch(); assertTrue(result); - verify(mWifiConfigManager).removeNetwork(anyInt(), anyInt()); + verify(mWifiConfigManager).removeNetwork(anyInt(), anyInt(), anyString()); } /** @@ -4051,7 +4044,7 @@ public class WifiServiceImplTest extends WifiBaseTest { .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); - when(mWifiConfigManager.disableNetwork(anyInt(), anyInt())).thenReturn(false); + when(mWifiConfigManager.disableNetwork(anyInt(), anyInt(), anyString())).thenReturn(false); mLooper.startAutoDispatch(); boolean succeeded = mWifiServiceImpl.disableNetwork(0, TEST_PACKAGE_NAME); diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointNetworkEvaluatorTest.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointNetworkEvaluatorTest.java index b55e9e7cc..f9e659bab 100644 --- a/tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointNetworkEvaluatorTest.java +++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointNetworkEvaluatorTest.java @@ -235,7 +235,8 @@ public class PasspointNetworkEvaluatorTest { assertNotNull(addedConfig.getValue().enterpriseConfig); assertEquals("", addedConfig.getValue().enterpriseConfig.getAnonymousIdentity()); assertTrue(addedConfig.getValue().isHomeProviderNetwork); - verify(mWifiConfigManager).enableNetwork(eq(TEST_NETWORK_ID), eq(false), anyInt()); + verify(mWifiConfigManager).enableNetwork( + eq(TEST_NETWORK_ID), eq(false), anyInt(), any()); verify(mWifiConfigManager).setNetworkCandidateScanResult( eq(TEST_NETWORK_ID), any(ScanResult.class), anyInt()); verify(mWifiConfigManager).updateScanDetailForNetwork( @@ -278,7 +279,8 @@ public class PasspointNetworkEvaluatorTest { assertNotNull(addedConfig.getValue().enterpriseConfig); assertEquals("", addedConfig.getValue().enterpriseConfig.getAnonymousIdentity()); assertFalse(addedConfig.getValue().isHomeProviderNetwork); - verify(mWifiConfigManager).enableNetwork(eq(TEST_NETWORK_ID), eq(false), anyInt()); + verify(mWifiConfigManager).enableNetwork( + eq(TEST_NETWORK_ID), eq(false), anyInt(), any()); verify(mWifiConfigManager).setNetworkCandidateScanResult( eq(TEST_NETWORK_ID), any(ScanResult.class), anyInt()); verify(mWifiConfigManager).updateScanDetailForNetwork( @@ -323,7 +325,8 @@ public class PasspointNetworkEvaluatorTest { assertNotNull(addedConfig.getValue().enterpriseConfig); assertEquals("", addedConfig.getValue().enterpriseConfig.getAnonymousIdentity()); assertTrue(addedConfig.getValue().isHomeProviderNetwork); - verify(mWifiConfigManager).enableNetwork(eq(TEST_NETWORK_ID), eq(false), anyInt()); + verify(mWifiConfigManager).enableNetwork( + eq(TEST_NETWORK_ID), eq(false), anyInt(), any()); verify(mWifiConfigManager).setNetworkCandidateScanResult( eq(TEST_NETWORK_ID), any(ScanResult.class), anyInt()); verify(mWifiConfigManager).updateScanDetailForNetwork( 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 fd486a883..5422ed3d3 100644 --- a/tests/wifitests/src/com/android/server/wifi/util/WifiPermissionsUtilTest.java +++ b/tests/wifitests/src/com/android/server/wifi/util/WifiPermissionsUtilTest.java @@ -31,6 +31,8 @@ import static org.mockito.Mockito.when; import android.Manifest; import android.app.AppOpsManager; +import android.app.admin.DevicePolicyManager; +import android.content.ComponentName; import android.content.ContentResolver; import android.content.Context; import android.content.pm.ApplicationInfo; @@ -79,8 +81,10 @@ public class WifiPermissionsUtilTest extends WifiBaseTest { @Mock private ContentResolver mMockContentResolver; @Mock private WifiInjector mWifiInjector; @Mock private LocationManager mLocationManager; + @Mock private DevicePolicyManager mDevicePolicyManager; @Spy private FakeWifiLog mWifiLog; + private static final String TEST_WIFI_STACK_APK_NAME = "com.android.server.wifistack"; private static final String TEST_PACKAGE_NAME = "com.google.somePackage"; private static final String INVALID_PACKAGE = "BAD_PACKAGE"; private static final int MANAGED_PROFILE_UID = 1100000; @@ -847,6 +851,95 @@ public class WifiPermissionsUtilTest extends WifiBaseTest { } /** + * Verifies the helper method exposed for checking if the app is a DeviceOwner. + */ + @Test + public void testIsDeviceOwnerApp() throws Exception { + setupMocks(); + WifiPermissionsUtil wifiPermissionsUtil = new WifiPermissionsUtil(mMockPermissionsWrapper, + mMockContext, mMockUserManager, mWifiInjector); + + when(mMockContext.getSystemService(DevicePolicyManager.class)) + .thenReturn(mDevicePolicyManager); + + when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()) + .thenReturn(new ComponentName(TEST_PACKAGE_NAME, new String())); + when(mDevicePolicyManager.getDeviceOwnerUser()) + .thenReturn(UserHandle.getUserHandleForUid(MANAGED_PROFILE_UID)); + assertTrue(wifiPermissionsUtil.isDeviceOwner( + MANAGED_PROFILE_UID, TEST_PACKAGE_NAME)); + + + // userId does not match + when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()) + .thenReturn(new ComponentName(TEST_PACKAGE_NAME, new String())); + when(mDevicePolicyManager.getDeviceOwnerUser()) + .thenReturn(UserHandle.getUserHandleForUid(OTHER_USER_UID)); + assertFalse(wifiPermissionsUtil.isDeviceOwner( + MANAGED_PROFILE_UID, TEST_PACKAGE_NAME)); + + // Package Name does not match + when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()) + .thenReturn(new ComponentName(INVALID_PACKAGE, new String())); + when(mDevicePolicyManager.getDeviceOwnerUser()) + .thenReturn(UserHandle.getUserHandleForUid(MANAGED_PROFILE_UID)); + assertFalse(wifiPermissionsUtil.isDeviceOwner( + MANAGED_PROFILE_UID, TEST_PACKAGE_NAME)); + + // No device owner. + when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()) + .thenReturn(null); + assertFalse(wifiPermissionsUtil.isDeviceOwner( + MANAGED_PROFILE_UID, TEST_PACKAGE_NAME)); + + // DevicePolicyManager does not exist. + when(mMockContext.getSystemService(Context.DEVICE_POLICY_SERVICE)) + .thenReturn(null); + assertFalse(wifiPermissionsUtil.isDeviceOwner( + MANAGED_PROFILE_UID, TEST_PACKAGE_NAME)); + } + + /** + * Verifies the helper method exposed for checking if the app is a ProfileOwner. + */ + @Test + public void testIsProfileOwnerApp() throws Exception { + setupMocks(); + WifiPermissionsUtil wifiPermissionsUtil = new WifiPermissionsUtil(mMockPermissionsWrapper, + mMockContext, mMockUserManager, mWifiInjector); + + when(mMockContext.createPackageContextAsUser( + TEST_WIFI_STACK_APK_NAME, 0, UserHandle.getUserHandleForUid(MANAGED_PROFILE_UID))) + .thenReturn(mMockContext); + when(mMockContext.getSystemService(DevicePolicyManager.class)) + .thenReturn(mDevicePolicyManager); + + when(mDevicePolicyManager.isProfileOwnerApp(TEST_PACKAGE_NAME)) + .thenReturn(true); + assertTrue(wifiPermissionsUtil.isProfileOwner( + MANAGED_PROFILE_UID, TEST_PACKAGE_NAME)); + + when(mDevicePolicyManager.isProfileOwnerApp(TEST_PACKAGE_NAME)) + .thenReturn(false); + assertFalse(wifiPermissionsUtil.isProfileOwner( + MANAGED_PROFILE_UID, TEST_PACKAGE_NAME)); + + // DevicePolicyManager does not exist. + when(mMockContext.getSystemService(Context.DEVICE_POLICY_SERVICE)) + .thenReturn(null); + assertFalse(wifiPermissionsUtil.isProfileOwner( + MANAGED_PROFILE_UID, TEST_PACKAGE_NAME)); + + // Invalid package name. + doThrow(new PackageManager.NameNotFoundException()) + .when(mMockContext).createPackageContextAsUser( + TEST_WIFI_STACK_APK_NAME, 0, + UserHandle.getUserHandleForUid(MANAGED_PROFILE_UID)); + assertFalse(wifiPermissionsUtil.isProfileOwner( + MANAGED_PROFILE_UID, TEST_PACKAGE_NAME)); + } + + /** * Test case setting: caller does not have Location permission. * Expect a SecurityException */ @@ -1157,6 +1250,7 @@ public class WifiPermissionsUtilTest extends WifiBaseTest { .thenReturn(mMockUserManager); when(mWifiInjector.makeLog(anyString())).thenReturn(mWifiLog); when(mMockContext.getSystemService(Context.LOCATION_SERVICE)).thenReturn(mLocationManager); + when(mMockContext.getPackageName()).thenReturn(TEST_WIFI_STACK_APK_NAME); } private void initTestVars() { |