diff options
author | Rebecca Silberstein <silberst@google.com> | 2017-04-16 01:20:40 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2017-04-16 01:20:40 +0000 |
commit | 536a9c2b67c5977d3bd567c44e4d16aa3c89e4fe (patch) | |
tree | 28e4d921095dde0be80ba47ca2c56075f758640d /tests | |
parent | 4f3b0d84dfc064b7cb40713007bf606c3969a743 (diff) | |
parent | d13cd95984bc71dae7989d64dde1b4f5269d029b (diff) |
WifiServiceImpl: setWifiApEnabled unit tests am: 198804eb25 am: eb00f87e10
am: d13cd95984
Change-Id: I77413789b1b2a19c9330f4c9b9978c4a0f5b5bf8
Diffstat (limited to 'tests')
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java | 85 |
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); + } } |