WifiServiceImpl: test setWifiEnabled
Before adding mode checks, add unit tests to check current behavior.
Bug: 36358238
Test: frameworks/opt/net/wifi/tests/wifitests/runtests.sh
Change-Id: I8de27f2bceebb589b173d91d79dfa3a5afe6a675
diff --git a/service/java/com/android/server/wifi/WifiServiceImpl.java b/service/java/com/android/server/wifi/WifiServiceImpl.java
index f915a6d..61d74fe 100644
--- a/service/java/com/android/server/wifi/WifiServiceImpl.java
+++ b/service/java/com/android/server/wifi/WifiServiceImpl.java
@@ -660,7 +660,7 @@
}
// null wifiConfig is a meaningful input for CMD_SET_AP
if (wifiConfig == null || isValid(wifiConfig)) {
- mWifiController.obtainMessage(CMD_SET_AP, enabled ? 1 : 0, 0, wifiConfig).sendToTarget();
+ mWifiController.sendMessage(CMD_SET_AP, enabled ? 1 : 0, 0, wifiConfig);
} else {
Slog.e(TAG, "Invalid WifiConfiguration");
}
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java
index 5684ce6..5a21162 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 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;
@@ -65,6 +66,7 @@
private static final String TAG = "WifiServiceImplTest";
private static final int DEFAULT_VERBOSE_LOGGING = 0;
+ private static final String TEST_PACKAGE_NAME = "TestPackage";
@Mock Context mContext;
@Mock WifiInjector mWifiInjector;
@@ -210,6 +212,73 @@
.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(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(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 that wifi can be disabled by a caller with WIFI_STATE_CHANGE permission when wifi is
+ * on.
+ */
+ @Test
+ public void testSetWifiDisabledSuccess() throws Exception {
+ 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(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 {
+ 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.
*