summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRebecca Silberstein <silberst@google.com>2017-04-02 22:12:48 -0700
committerRebecca Silberstein <silberst@google.com>2017-04-12 11:22:04 -0700
commitc90564bcec223b69abe7e31d9471aa20479b69e7 (patch)
tree1c1e8c1e28307fc842c3326ad0329e6d24fc6896 /tests
parent7df23a4c0aee6166602225af8e7220395b349e52 (diff)
WifiServiceImpl: check calls to enable wifi
When the user has entered softap mode, apps should not be able to toggle wifi on. This leaves the user with unexpected behavior. This CL adds checks for softap mode to make sure only Settings and SysUI (Quick and QuickQuick settings) can toggle wifi on. Bug: 36358238 Test: manually Test: frameworks/opt/net/wifi/tests/wifitests/runtests.sh Test: frameworks/base/wifi/tests/runtests.sh Test: wifi integration tests Change-Id: I114757324b529a43c411dc8a01afcb05a23489ee
Diffstat (limited to 'tests')
-rw-r--r--tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java45
1 files changed, 45 insertions, 0 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java
index 5a2116242..f18aabe71 100644
--- a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java
@@ -32,6 +32,7 @@ import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.net.wifi.WifiConfiguration;
+import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IPowerManager;
@@ -66,7 +67,10 @@ public class WifiServiceImplTest {
private static final String TAG = "WifiServiceImplTest";
private static final int DEFAULT_VERBOSE_LOGGING = 0;
+ private static final String ANDROID_SYSTEM_PACKAGE = "android";
private static final String TEST_PACKAGE_NAME = "TestPackage";
+ private static final String SETTINGS_PACKAGE_NAME = "com.android.settings";
+ private static final String SYSUI_PACKAGE_NAME = "com.android.systemui";
@Mock Context mContext;
@Mock WifiInjector mWifiInjector;
@@ -219,6 +223,7 @@ public class WifiServiceImplTest {
*/
@Test
public void testSetWifiEnabledSuccess() throws Exception {
+ when(mWifiStateMachine.syncGetWifiApState()).thenReturn(WifiManager.WIFI_AP_STATE_DISABLED);
when(mSettingsStore.handleWifiToggled(eq(true))).thenReturn(true);
assertTrue(mWifiServiceImpl.setWifiEnabled(TEST_PACKAGE_NAME, true));
verify(mWifiController).sendMessage(eq(CMD_WIFI_TOGGLED));
@@ -229,6 +234,7 @@ public class WifiServiceImplTest {
*/
@Test
public void testSetWifiEnabledNoToggle() throws Exception {
+ when(mWifiStateMachine.syncGetWifiApState()).thenReturn(WifiManager.WIFI_AP_STATE_DISABLED);
when(mSettingsStore.handleWifiToggled(eq(true))).thenReturn(false);
assertTrue(mWifiServiceImpl.setWifiEnabled(TEST_PACKAGE_NAME, true));
verify(mWifiController, never()).sendMessage(eq(CMD_WIFI_TOGGLED));
@@ -244,6 +250,40 @@ public class WifiServiceImplTest {
.enforceCallingOrSelfPermission(eq(android.Manifest.permission.CHANGE_WIFI_STATE),
eq("WifiService"));
mWifiServiceImpl.setWifiEnabled(TEST_PACKAGE_NAME, true);
+ verify(mWifiStateMachine, never()).syncGetWifiApState();
+ }
+
+ /**
+ * Verify that a call from Settings can enable wifi if we are in softap mode.
+ */
+ @Test
+ public void testSetWifiEnabledFromSettingsWhenApEnabled() throws Exception {
+ when(mWifiStateMachine.syncGetWifiApState()).thenReturn(WifiManager.WIFI_AP_STATE_ENABLED);
+ when(mSettingsStore.handleWifiToggled(eq(true))).thenReturn(true);
+ assertTrue(mWifiServiceImpl.setWifiEnabled(SETTINGS_PACKAGE_NAME, true));
+ verify(mWifiController).sendMessage(eq(CMD_WIFI_TOGGLED));
+ }
+
+ /**
+ * Verify that a call from SysUI can enable wifi if we are in softap mode.
+ */
+ @Test
+ public void testSetWifiEnabledFromSysUiWhenApEnabled() throws Exception {
+ when(mWifiStateMachine.syncGetWifiApState()).thenReturn(WifiManager.WIFI_AP_STATE_ENABLED);
+ when(mSettingsStore.handleWifiToggled(eq(true))).thenReturn(true);
+ assertTrue(mWifiServiceImpl.setWifiEnabled(SYSUI_PACKAGE_NAME, true));
+ verify(mWifiController).sendMessage(eq(CMD_WIFI_TOGGLED));
+ }
+
+ /**
+ * Verify that a call from an app cannot enable wifi if we are in softap mode.
+ */
+ @Test
+ public void testSetWifiEnabledFromAppFailsWhenApEnabled() throws Exception {
+ when(mWifiStateMachine.syncGetWifiApState()).thenReturn(WifiManager.WIFI_AP_STATE_ENABLED);
+ assertFalse(mWifiServiceImpl.setWifiEnabled(TEST_PACKAGE_NAME, true));
+ verify(mSettingsStore, never()).handleWifiToggled(anyBoolean());
+ verify(mWifiController, never()).sendMessage(eq(CMD_WIFI_TOGGLED));
}
/**
@@ -252,6 +292,7 @@ public class WifiServiceImplTest {
*/
@Test
public void testSetWifiDisabledSuccess() throws Exception {
+ when(mWifiStateMachine.syncGetWifiApState()).thenReturn(WifiManager.WIFI_AP_STATE_DISABLED);
when(mSettingsStore.handleWifiToggled(eq(false))).thenReturn(true);
assertTrue(mWifiServiceImpl.setWifiEnabled(TEST_PACKAGE_NAME, false));
verify(mWifiController).sendMessage(eq(CMD_WIFI_TOGGLED));
@@ -262,6 +303,7 @@ public class WifiServiceImplTest {
*/
@Test
public void testSetWifiDisabledNoToggle() throws Exception {
+ when(mWifiStateMachine.syncGetWifiApState()).thenReturn(WifiManager.WIFI_AP_STATE_DISABLED);
when(mSettingsStore.handleWifiToggled(eq(false))).thenReturn(false);
assertTrue(mWifiServiceImpl.setWifiEnabled(TEST_PACKAGE_NAME, false));
verify(mWifiController, never()).sendMessage(eq(CMD_WIFI_TOGGLED));
@@ -273,6 +315,7 @@ public class WifiServiceImplTest {
*/
@Test(expected = SecurityException.class)
public void testSetWifiDisabledWithoutPermission() throws Exception {
+ when(mWifiStateMachine.syncGetWifiApState()).thenReturn(WifiManager.WIFI_AP_STATE_DISABLED);
doThrow(new SecurityException()).when(mContext)
.enforceCallingOrSelfPermission(eq(android.Manifest.permission.CHANGE_WIFI_STATE),
eq("WifiService"));
@@ -368,6 +411,8 @@ public class WifiServiceImplTest {
when(mSettingsStore.handleWifiToggled(true)).thenReturn(true);
when(mSettingsStore.isWifiToggleEnabled()).thenReturn(true);
when(mWifiStateMachine.syncGetWifiState()).thenReturn(WIFI_STATE_DISABLED);
+ when(mWifiStateMachine.syncGetWifiApState()).thenReturn(WifiManager.WIFI_AP_STATE_DISABLED);
+ when(mContext.getPackageName()).thenReturn(ANDROID_SYSTEM_PACKAGE);
mWifiServiceImpl.checkAndStartWifi();
verify(mWifiController).start();
verify(mWifiController).sendMessage(CMD_WIFI_TOGGLED);