summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRebecca Silberstein <silberst@google.com>2017-04-15 22:43:53 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2017-04-15 22:43:54 +0000
commitff62ee923988e6226af808aeedebbc9174c74f64 (patch)
treeb0450b959db7f4aff1911091d54feadca99e0140 /tests
parent1e5c1abdeb4e3e180e5344c777f7a7db2095c246 (diff)
parentc90564bcec223b69abe7e31d9471aa20479b69e7 (diff)
Merge changes I11475732,I8de27f2b
* changes: WifiServiceImpl: check calls to enable wifi WifiServiceImpl: test setWifiEnabled
Diffstat (limited to 'tests')
-rw-r--r--tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java114
1 files changed, 114 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 5684ce6de..f18aabe71 100644
--- a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java
@@ -22,6 +22,7 @@ import static com.android.server.wifi.WifiController.CMD_WIFI_TOGGLED;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
@@ -31,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;
@@ -65,6 +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;
@@ -210,6 +216,112 @@ public class WifiServiceImplTest {
.dump(any(FileDescriptor.class), any(PrintWriter.class), any(String[].class));
}
+
+ /**
+ * Verify that wifi can be enabled by a caller with WIFI_STATE_CHANGE permission when wifi is
+ * off (no hotspot, no airplane mode).
+ */
+ @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));
+ }
+
+ /**
+ * Verify that the CMD_TOGGLE_WIFI message won't be sent if wifi is already on.
+ */
+ @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));
+ }
+
+ /**
+ * Verify a SecurityException is thrown if a caller does not have the correct permission to
+ * toggle wifi.
+ */
+ @Test(expected = SecurityException.class)
+ public void testSetWifiEnableWithoutPermission() throws Exception {
+ doThrow(new SecurityException()).when(mContext)
+ .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));
+ }
+
+ /**
+ * Verify that wifi can be disabled by a caller with WIFI_STATE_CHANGE permission when wifi is
+ * on.
+ */
+ @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));
+ }
+
+ /**
+ * Verify that CMD_TOGGLE_WIFI message won't be sent if wifi is already off.
+ */
+ @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));
+ }
+
+ /**
+ * Verify a SecurityException is thrown if a caller does not have the correct permission to
+ * toggle wifi.
+ */
+ @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"));
+ mWifiServiceImpl.setWifiEnabled(TEST_PACKAGE_NAME, false);
+ }
+
/**
* Ensure unpermitted callers cannot write the SoftApConfiguration.
*
@@ -299,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);