diff options
author | TreeHugger Robot <treehugger-gerrit@google.com> | 2017-07-28 15:23:33 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2017-07-28 15:23:33 +0000 |
commit | 069e700d31025487dd8a43e90cb02044824f708d (patch) | |
tree | 4382e3e91d7a724a7c483e374470b17a6cb7e623 | |
parent | 3673d53dea03d18e5b28ce248b9911f81e3f8638 (diff) | |
parent | 0753c5118317683fe5f4864e74b3ba9c015a6e4f (diff) |
Merge "Prevent apps from toggling Wifi when in Airplane mode" into oc-dr1-dev
-rw-r--r-- | service/java/com/android/server/wifi/WifiServiceImpl.java | 12 | ||||
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java | 30 |
2 files changed, 40 insertions, 2 deletions
diff --git a/service/java/com/android/server/wifi/WifiServiceImpl.java b/service/java/com/android/server/wifi/WifiServiceImpl.java index d222727b5..d6faf9b5d 100644 --- a/service/java/com/android/server/wifi/WifiServiceImpl.java +++ b/service/java/com/android/server/wifi/WifiServiceImpl.java @@ -780,11 +780,19 @@ public class WifiServiceImpl extends IWifiManager.Stub { mLog.trace("setWifiEnabled package=% uid=% enable=%").c(packageName) .c(Binder.getCallingUid()).c(enable).flush(); + boolean isFromSettings = + mWifiPermissionsUtil.checkNetworkSettingsPermission(Binder.getCallingUid()); + + // If Airplane mode is enabled, only Settings is allowed to toggle Wifi + if (mSettingsStore.isAirplaneModeOn() && !isFromSettings) { + mLog.trace("setWifiEnabled in Airplane mode: only Settings can enable wifi").flush(); + return false; + } + // If SoftAp is enabled, only Settings is allowed to toggle wifi boolean apEnabled = mWifiStateMachine.syncGetWifiApState() != WifiManager.WIFI_AP_STATE_DISABLED; - boolean isFromSettings = - mWifiPermissionsUtil.checkNetworkSettingsPermission(Binder.getCallingUid()); + if (apEnabled && !isFromSettings) { mLog.trace("setWifiEnabled SoftAp not disabled: only Settings can enable wifi").flush(); return false; diff --git a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java index 3b11d28c9..055050deb 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java @@ -362,6 +362,7 @@ public class WifiServiceImplTest { public void testSetWifiEnabledSuccess() throws Exception { when(mWifiStateMachine.syncGetWifiApState()).thenReturn(WifiManager.WIFI_AP_STATE_DISABLED); when(mSettingsStore.handleWifiToggled(eq(true))).thenReturn(true); + when(mSettingsStore.isAirplaneModeOn()).thenReturn(false); assertTrue(mWifiServiceImpl.setWifiEnabled(TEST_PACKAGE_NAME, true)); verify(mWifiController).sendMessage(eq(CMD_WIFI_TOGGLED)); } @@ -386,12 +387,39 @@ public class WifiServiceImplTest { doThrow(new SecurityException()).when(mContext) .enforceCallingOrSelfPermission(eq(android.Manifest.permission.CHANGE_WIFI_STATE), eq("WifiService")); + when(mSettingsStore.isAirplaneModeOn()).thenReturn(false); mWifiServiceImpl.setWifiEnabled(TEST_PACKAGE_NAME, true); verify(mWifiStateMachine, never()).syncGetWifiApState(); } /** * Verify that a call from an app with the NETWORK_SETTINGS permission can enable wifi if we + * are in airplane mode. + */ + @Test + public void testSetWifiEnabledFromNetworkSettingsHolderWhenInAirplaneMode() throws Exception { + when(mSettingsStore.handleWifiToggled(eq(true))).thenReturn(true); + when(mSettingsStore.isAirplaneModeOn()).thenReturn(true); + when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(true); + assertTrue(mWifiServiceImpl.setWifiEnabled(SYSUI_PACKAGE_NAME, true)); + verify(mWifiController).sendMessage(eq(CMD_WIFI_TOGGLED)); + } + + /** + * Verify that a caller without the NETWORK_SETTINGS permission can't enable wifi + * if we are in airplane mode. + */ + @Test + public void testSetWifiEnabledFromAppFailsWhenInAirplaneMode() throws Exception { + when(mSettingsStore.handleWifiToggled(eq(true))).thenReturn(true); + when(mSettingsStore.isAirplaneModeOn()).thenReturn(true); + when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(false); + assertFalse(mWifiServiceImpl.setWifiEnabled(TEST_PACKAGE_NAME, true)); + verify(mWifiController, never()).sendMessage(eq(CMD_WIFI_TOGGLED)); + } + + /** + * Verify that a call from an app with the NETWORK_SETTINGS permission can enable wifi if we * are in softap mode. */ @Test @@ -399,6 +427,7 @@ public class WifiServiceImplTest { when(mWifiStateMachine.syncGetWifiApState()).thenReturn(WifiManager.WIFI_AP_STATE_ENABLED); when(mSettingsStore.handleWifiToggled(eq(true))).thenReturn(true); when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(true); + when(mSettingsStore.isAirplaneModeOn()).thenReturn(false); assertTrue(mWifiServiceImpl.setWifiEnabled(SYSUI_PACKAGE_NAME, true)); verify(mWifiController).sendMessage(eq(CMD_WIFI_TOGGLED)); } @@ -410,6 +439,7 @@ public class WifiServiceImplTest { public void testSetWifiEnabledFromAppFailsWhenApEnabled() throws Exception { when(mWifiStateMachine.syncGetWifiApState()).thenReturn(WifiManager.WIFI_AP_STATE_ENABLED); when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(false); + when(mSettingsStore.isAirplaneModeOn()).thenReturn(false); assertFalse(mWifiServiceImpl.setWifiEnabled(TEST_PACKAGE_NAME, true)); verify(mSettingsStore, never()).handleWifiToggled(anyBoolean()); verify(mWifiController, never()).sendMessage(eq(CMD_WIFI_TOGGLED)); |