summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRebecca Silberstein <silberst@google.com>2017-04-12 22:39:16 -0700
committerRebecca Silberstein <silberst@google.com>2017-04-19 16:52:21 +0000
commit3871ff67f4a6970f1831fc8951392746c9e2bfa2 (patch)
tree60e3508ad94acbf4ed930a9201179d20984703fc /tests
parentbd0447ff06c5719494960e2e3ec95b84941baf2a (diff)
WifiServiceImpl: setWifiApEnabled unit tests
Add unit tests to cover setWifiApEnabled. This did require a small change to check a tethering permission and add the user manager. Bug: 37294233 Test: frameworks/opt/net/wifi/tests/wifitests/runtests.sh Change-Id: I7bdfdcbdfa60605bc2e61f18aa8abeb4bcaf5c56 Merged-In: I77413789b1b2a19c9330f4c9b9978c4a0f5b5bf8 Merged-In: I33ae84d7282dea967a53a644018e20eb2902eca3
Diffstat (limited to 'tests')
-rw-r--r--tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java85
1 files changed, 85 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 93ee8ea9a..a09c9132a 100644
--- a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java
@@ -18,6 +18,7 @@ package com.android.server.wifi;
import static android.net.wifi.WifiManager.WIFI_STATE_DISABLED;
+import static com.android.server.wifi.WifiController.CMD_SET_AP;
import static com.android.server.wifi.WifiController.CMD_WIFI_TOGGLED;
import static org.junit.Assert.assertEquals;
@@ -41,6 +42,7 @@ import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.os.PowerManager;
+import android.os.UserManager;
import android.os.test.TestLooper;
import android.test.suitebuilder.annotation.SmallTest;
@@ -94,6 +96,8 @@ public class WifiServiceImplTest {
@Mock WifiPermissionsUtil mWifiPermissionsUtil;
@Mock WifiSettingsStore mSettingsStore;
@Mock ContentResolver mContentResolver;
+ @Mock UserManager mUserManager;
+ @Mock WifiConfiguration mApConfig;
PowerManager mPowerManager;
private class WifiAsyncChannelTester {
@@ -153,6 +157,7 @@ public class WifiServiceImplTest {
MockitoAnnotations.initMocks(this);
mLooper = new TestLooper();
+ when(mWifiInjector.getUserManager()).thenReturn(mUserManager);
when(mWifiInjector.getWifiController()).thenReturn(mWifiController);
when(mWifiInjector.getWifiMetrics()).thenReturn(mWifiMetrics);
when(mWifiInjector.getWifiStateMachine()).thenReturn(mWifiStateMachine);
@@ -436,4 +441,84 @@ public class WifiServiceImplTest {
verify(mWifiController).start();
verify(mWifiController).sendMessage(CMD_WIFI_TOGGLED);
}
+
+ /**
+ * Verify setWifiApEnabled works with the correct permissions and a null config.
+ */
+ @Test
+ public void testSetWifiApEnabledWithProperPermissionsWithNullConfig() {
+ when(mWifiPermissionsUtil.checkConfigOverridePermission(anyInt())).thenReturn(true);
+ when(mUserManager.hasUserRestriction(eq(UserManager.DISALLOW_CONFIG_TETHERING)))
+ .thenReturn(false);
+ mWifiServiceImpl.setWifiApEnabled(null, true);
+ verify(mWifiController).sendMessage(eq(CMD_SET_AP), eq(1), eq(0), eq(null));
+ }
+
+ /**
+ * Verify setWifiApEnabled works with correct permissions and a valid config.
+ *
+ * TODO: should really validate that ap configs have a set of basic config settings b/37280779
+ */
+ @Test
+ public void testSetWifiApEnabledWithProperPermissionsWithValidConfig() {
+ when(mWifiPermissionsUtil.checkConfigOverridePermission(anyInt())).thenReturn(true);
+ when(mUserManager.hasUserRestriction(eq(UserManager.DISALLOW_CONFIG_TETHERING)))
+ .thenReturn(false);
+ WifiConfiguration apConfig = new WifiConfiguration();
+ mWifiServiceImpl.setWifiApEnabled(apConfig, true);
+ verify(mWifiController).sendMessage(eq(CMD_SET_AP), eq(1), eq(0), eq(apConfig));
+ }
+
+ /**
+ * Verify setWifiApEnabled when disabling softap with correct permissions sends the correct
+ * message to WifiController.
+ */
+ @Test
+ public void testSetWifiApEnabledFalseWithProperPermissionsWithNullConfig() {
+ when(mWifiPermissionsUtil.checkConfigOverridePermission(anyInt())).thenReturn(true);
+ when(mUserManager.hasUserRestriction(eq(UserManager.DISALLOW_CONFIG_TETHERING)))
+ .thenReturn(false);
+ mWifiServiceImpl.setWifiApEnabled(null, false);
+ verify(mWifiController).sendMessage(eq(CMD_SET_AP), eq(0), eq(0), eq(null));
+ }
+
+ /**
+ * setWifiApEnabled should fail if the provided config is not valid.
+ */
+ @Test
+ public void testSetWIfiApEnabledWithProperPermissionInvalidConfigFails() {
+ when(mWifiPermissionsUtil.checkConfigOverridePermission(anyInt())).thenReturn(true);
+ when(mUserManager.hasUserRestriction(eq(UserManager.DISALLOW_CONFIG_TETHERING)))
+ .thenReturn(false);
+ // mApConfig is a mock and the values are not set - triggering the invalid config. Testing
+ // will be improved when we actually do test softap configs in b/37280779
+ mWifiServiceImpl.setWifiApEnabled(mApConfig, true);
+ verify(mWifiController, never()).sendMessage(eq(CMD_SET_AP), eq(1), eq(0), eq(mApConfig));
+ }
+
+ /**
+ * setWifiApEnabled should throw a security exception when the caller does not have the correct
+ * permissions.
+ */
+ @Test(expected = SecurityException.class)
+ public void testSetWifiApEnabledThrowsSecurityExceptionWithoutConfigOverridePermission()
+ throws Exception {
+ doThrow(new SecurityException()).when(mContext)
+ .enforceCallingOrSelfPermission(eq(android.Manifest.permission.CHANGE_WIFI_STATE),
+ eq("WifiService"));
+ mWifiServiceImpl.setWifiApEnabled(null, true);
+ }
+
+ /**
+ * setWifiApEnabled should throw a SecurityException when disallow tethering is set for the
+ * user.
+ */
+ @Test(expected = SecurityException.class)
+ public void testSetWifiApEnabledThrowsSecurityExceptionWithDisallowTethering()
+ throws Exception {
+ when(mWifiPermissionsUtil.checkConfigOverridePermission(anyInt())).thenReturn(true);
+ when(mUserManager.hasUserRestriction(eq(UserManager.DISALLOW_CONFIG_TETHERING)))
+ .thenReturn(true);
+ mWifiServiceImpl.setWifiApEnabled(null, true);
+ }
}