From 198804eb25f071f6a5a33a0bf442b49cc87a8484 Mon Sep 17 00:00:00 2001 From: Rebecca Silberstein Date: Wed, 12 Apr 2017 22:39:16 -0700 Subject: 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 --- .../android/server/wifi/WifiServiceImplTest.java | 85 ++++++++++++++++++++++ 1 file changed, 85 insertions(+) (limited to 'tests') diff --git a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java index f18aabe71..ed0e9c486 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; @@ -40,6 +41,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; @@ -93,6 +95,8 @@ public class WifiServiceImplTest { @Mock WifiPermissionsUtil mWifiPermissionsUtil; @Mock WifiSettingsStore mSettingsStore; @Mock ContentResolver mContentResolver; + @Mock UserManager mUserManager; + @Mock WifiConfiguration mApConfig; PowerManager mPowerManager; private class WifiAsyncChannelTester { @@ -152,6 +156,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); @@ -417,4 +422,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); + } } -- cgit v1.2.3