diff options
author | Rebecca Silberstein <silberst@google.com> | 2017-04-17 17:20:46 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2017-04-17 17:20:46 +0000 |
commit | d5805c08f661113c04a0fa55bdea6c3b304427a3 (patch) | |
tree | afb2b0d2419fa09716f86c58b88c0ab1b845e4b2 | |
parent | eb00f87e100528f3c3e9c4aa51f2ff94a7693385 (diff) | |
parent | b7c333ba6d7480d7531a2db3e84ad934187204a5 (diff) |
WifiServiceImpl: add new methods to start softap
am: b7c333ba6d
Change-Id: I29c83ae15e3feb1aafef5136d37959c81e35ac2d
-rw-r--r-- | service/java/com/android/server/wifi/WifiServiceImpl.java | 66 | ||||
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java | 68 |
2 files changed, 133 insertions, 1 deletions
diff --git a/service/java/com/android/server/wifi/WifiServiceImpl.java b/service/java/com/android/server/wifi/WifiServiceImpl.java index e39420941..c5f367561 100644 --- a/service/java/com/android/server/wifi/WifiServiceImpl.java +++ b/service/java/com/android/server/wifi/WifiServiceImpl.java @@ -16,6 +16,7 @@ package com.android.server.wifi; +import static com.android.server.connectivity.tethering.IControlsTethering.STATE_TETHERED; import static com.android.server.wifi.WifiController.CMD_AIRPLANE_TOGGLED; import static com.android.server.wifi.WifiController.CMD_BATTERY_CHANGED; import static com.android.server.wifi.WifiController.CMD_EMERGENCY_CALL_STATE_CHANGED; @@ -541,6 +542,11 @@ public class WifiServiceImpl extends IWifiManager.Stub { } } + private void enforceNetworkStackPermission() { + mContext.enforceCallingOrSelfPermission(android.Manifest.permission.NETWORK_STACK, + "WifiService"); + } + private void enforceAccessPermission() { mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE, "WifiService"); @@ -697,6 +703,66 @@ public class WifiServiceImpl extends IWifiManager.Stub { } /** + * see {@link android.net.wifi.WifiManager#startSoftAp(WifiConfiguration)} + * @param wifiConfig SSID, security and channel details as + * part of WifiConfiguration + * @return {@code true} if softap start was triggered + * @throws SecurityException if the caller does not have permission to start softap + */ + @Override + public boolean startSoftAp(WifiConfiguration wifiConfig) { + // NETWORK_STACK is a signature only permission. + enforceNetworkStackPermission(); + + mLog.trace("startSoftAp uid=%").c(Binder.getCallingUid()).flush(); + + return startSoftApInternal(wifiConfig, STATE_TETHERED); + } + + private boolean startSoftApInternal(WifiConfiguration wifiConfig, int mode) { + mLog.trace("startSoftApInternal uid=% mode=%") + .c(Binder.getCallingUid()).c(mode).flush(); + + // null wifiConfig is a meaningful input for CMD_SET_AP + if (wifiConfig == null || isValid(wifiConfig)) { + // TODO: need a way to set the mode + mWifiController.sendMessage(CMD_SET_AP, 1, 0, wifiConfig); + return true; + } + Slog.e(TAG, "Invalid WifiConfiguration"); + return false; + } + + /** + * see {@link android.net.wifi.WifiManager#stopSoftAp()} + * @return {@code true} if softap stop was triggered + * @throws SecurityException if the caller does not have permission to stop softap + */ + @Override + public boolean stopSoftAp() { + // NETWORK_STACK is a signature only permission. + enforceNetworkStackPermission(); + + mLog.trace("stopSoftAp uid=%").c(Binder.getCallingUid()).flush(); + + // add checks here to make sure this is the proper caller - apps can't disable tethering or + // instances of local only hotspot that they didn't start. return false for those cases + + return stopSoftApInternal(); + } + + /** + * Internal method to stop softap mode. Callers of this method should have already checked + * proper permissions beyond the NetworkStack permission. + */ + private boolean stopSoftApInternal() { + mLog.trace("stopSoftApInternal uid=%").c(Binder.getCallingUid()).flush(); + + mWifiController.sendMessage(CMD_SET_AP, 0, 0); + return true; + } + + /** * see {@link WifiManager#getWifiApConfiguration()} * @return soft access point configuration * @throws SecurityException if the caller does not have permission to retrieve the softap diff --git a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java index ed0e9c486..be597b9f4 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java @@ -467,7 +467,7 @@ public class WifiServiceImplTest { * setWifiApEnabled should fail if the provided config is not valid. */ @Test - public void testSetWIfiApEnabledWithProperPermissionInvalidConfigFails() { + public void testSetWifiApEnabledWithProperPermissionInvalidConfigFails() { when(mWifiPermissionsUtil.checkConfigOverridePermission(anyInt())).thenReturn(true); when(mUserManager.hasUserRestriction(eq(UserManager.DISALLOW_CONFIG_TETHERING))) .thenReturn(false); @@ -501,5 +501,71 @@ public class WifiServiceImplTest { when(mUserManager.hasUserRestriction(eq(UserManager.DISALLOW_CONFIG_TETHERING))) .thenReturn(true); mWifiServiceImpl.setWifiApEnabled(null, true); + + } + + /** + * Verify caller with proper permission can call startSoftAp. + */ + @Test + public void testStartSoftApWithPermissionsAndNullConfig() { + boolean result = mWifiServiceImpl.startSoftAp(null); + assertTrue(result); + verify(mWifiController).sendMessage(eq(CMD_SET_AP), eq(1), eq(0), eq(null)); + } + + /** + * Verify caller with proper permissions but an invalid config does not start softap. + */ + @Test + public void testStartSoftApWithPermissionsAndInvalidConfig() { + boolean result = mWifiServiceImpl.startSoftAp(mApConfig); + assertFalse(result); + verifyZeroInteractions(mWifiController); + } + + /** + * Verify caller with proper permission and valid config does start softap. + */ + @Test + public void testStartSoftApWithPermissionsAndValidConfig() { + WifiConfiguration config = new WifiConfiguration(); + boolean result = mWifiServiceImpl.startSoftAp(config); + assertTrue(result); + verify(mWifiController).sendMessage(eq(CMD_SET_AP), eq(1), eq(0), eq(config)); + } + + /** + * Verify a SecurityException is thrown when a caller without the correct permission attempts to + * start softap. + */ + @Test(expected = SecurityException.class) + public void testStartSoftApWithoutPermissionThrowsException() throws Exception { + doThrow(new SecurityException()).when(mContext) + .enforceCallingOrSelfPermission(eq(android.Manifest.permission.NETWORK_STACK), + eq("WifiService")); + mWifiServiceImpl.startSoftAp(null); + } + + /** + * Verify caller with proper permission can call stopSoftAp. + */ + @Test + public void testStopSoftApWithPermissions() { + boolean result = mWifiServiceImpl.stopSoftAp(); + assertTrue(result); + verify(mWifiController).sendMessage(eq(CMD_SET_AP), eq(0), eq(0)); + } + + /** + * Verify SecurityException is thrown when a caller without the correct permission attempts to + * stop softap. + */ + @Test(expected = SecurityException.class) + public void testStopSoftApWithoutPermissionThrowsException() throws Exception { + doThrow(new SecurityException()).when(mContext) + .enforceCallingOrSelfPermission(eq(android.Manifest.permission.NETWORK_STACK), + eq("WifiService")); + mWifiServiceImpl.stopSoftAp(); } } |