diff options
author | Roshan Pius <rpius@google.com> | 2017-09-25 14:51:37 -0700 |
---|---|---|
committer | Roshan Pius <rpius@google.com> | 2017-09-28 15:24:45 -0700 |
commit | 7cede48303b0faabb3b6861a1ac7229f76fca006 (patch) | |
tree | 7c15c871ebb75fee9e471489dc0f2123f596c607 | |
parent | b73e0a91dcf3fa04cf07c2d096fb20c229d6cd28 (diff) | |
download | android_frameworks_opt_net_wifi-7cede48303b0faabb3b6861a1ac7229f76fca006.tar.gz android_frameworks_opt_net_wifi-7cede48303b0faabb3b6861a1ac7229f76fca006.tar.bz2 android_frameworks_opt_net_wifi-7cede48303b0faabb3b6861a1ac7229f76fca006.zip |
WifiStateMachine: Handle WifiManager.save() when wifi is off
Bug: 66909738
Test: Verified that you can save networks even when wifi is off using
ag/2930541.
Test: Unit tests
Change-Id: I38c316456e74ce5ad1ec43750b075f0999c28327
-rw-r--r-- | service/java/com/android/server/wifi/WifiStateMachine.java | 75 | ||||
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java | 137 |
2 files changed, 158 insertions, 54 deletions
diff --git a/service/java/com/android/server/wifi/WifiStateMachine.java b/service/java/com/android/server/wifi/WifiStateMachine.java index faad0e2d1..ca9cdfdfd 100644 --- a/service/java/com/android/server/wifi/WifiStateMachine.java +++ b/service/java/com/android/server/wifi/WifiStateMachine.java @@ -3980,9 +3980,7 @@ public class WifiStateMachine extends StateMachine implements WifiNative.WifiRss deleteNetworkConfigAndSendReply(message, true); break; case WifiManager.SAVE_NETWORK: - messageHandlingStatus = MESSAGE_HANDLING_STATUS_FAIL; - replyToMessage(message, WifiManager.SAVE_NETWORK_FAILED, - WifiManager.BUSY); + saveNetworkConfigAndSendReply(message); break; case WifiManager.START_WPS: replyToMessage(message, WifiManager.WPS_FAILED, @@ -5270,36 +5268,12 @@ public class WifiStateMachine extends StateMachine implements WifiNative.WifiRss replyToMessage(message, WifiManager.CONNECT_NETWORK_SUCCEEDED); break; case WifiManager.SAVE_NETWORK: - config = (WifiConfiguration) message.obj; - mWifiConnectionStatistics.numWifiManagerJoinAttempt++; - if (config == null) { - loge("SAVE_NETWORK with null configuration" - + mSupplicantStateTracker.getSupplicantStateName() - + " my state " + getCurrentState().getName()); - messageHandlingStatus = MESSAGE_HANDLING_STATUS_FAIL; - replyToMessage(message, WifiManager.SAVE_NETWORK_FAILED, - WifiManager.ERROR); - break; - } - result = mWifiConfigManager.addOrUpdateNetwork(config, message.sendingUid); - if (!result.isSuccess()) { - loge("SAVE_NETWORK adding/updating config=" + config + " failed"); - messageHandlingStatus = MESSAGE_HANDLING_STATUS_FAIL; - replyToMessage(message, WifiManager.SAVE_NETWORK_FAILED, - WifiManager.ERROR); - break; - } - if (!mWifiConfigManager.enableNetwork( - result.getNetworkId(), false, message.sendingUid)) { - loge("SAVE_NETWORK enabling config=" + config + " failed"); - messageHandlingStatus = MESSAGE_HANDLING_STATUS_FAIL; - replyToMessage(message, WifiManager.SAVE_NETWORK_FAILED, - WifiManager.ERROR); - break; - } + result = saveNetworkConfigAndSendReply(message); netId = result.getNetworkId(); - if (mWifiInfo.getNetworkId() == netId) { + if (result.isSuccess() && mWifiInfo.getNetworkId() == netId) { + mWifiConnectionStatistics.numWifiManagerJoinAttempt++; if (result.hasCredentialChanged()) { + config = (WifiConfiguration) message.obj; // The network credentials changed and we're connected to this network, // start a new connection with the updated credentials. logi("SAVE_NETWORK credential changed for config=" + config.configKey() @@ -5322,8 +5296,6 @@ public class WifiStateMachine extends StateMachine implements WifiNative.WifiRss } } } - broadcastWifiCredentialChanged(WifiManager.WIFI_CREDENTIAL_SAVED, config); - replyToMessage(message, WifiManager.SAVE_NETWORK_SUCCEEDED); break; case WifiManager.FORGET_NETWORK: if (!deleteNetworkConfigAndSendReply(message, true)) { @@ -7210,6 +7182,43 @@ public class WifiStateMachine extends StateMachine implements WifiNative.WifiRss } } + /** + * Private method to handle calling WifiConfigManager to add & enable network configs and reply + * to the message from the sender of the outcome. + * + * @return NetworkUpdateResult with networkId of the added/updated configuration. Will return + * {@link WifiConfiguration#INVALID_NETWORK_ID} in case of error. + */ + private NetworkUpdateResult saveNetworkConfigAndSendReply(Message message) { + WifiConfiguration config = (WifiConfiguration) message.obj; + if (config == null) { + loge("SAVE_NETWORK with null configuration " + + mSupplicantStateTracker.getSupplicantStateName() + + " my state " + getCurrentState().getName()); + messageHandlingStatus = MESSAGE_HANDLING_STATUS_FAIL; + replyToMessage(message, WifiManager.SAVE_NETWORK_FAILED, WifiManager.ERROR); + return new NetworkUpdateResult(WifiConfiguration.INVALID_NETWORK_ID); + } + NetworkUpdateResult result = + mWifiConfigManager.addOrUpdateNetwork(config, message.sendingUid); + if (!result.isSuccess()) { + loge("SAVE_NETWORK adding/updating config=" + config + " failed"); + messageHandlingStatus = MESSAGE_HANDLING_STATUS_FAIL; + replyToMessage(message, WifiManager.SAVE_NETWORK_FAILED, WifiManager.ERROR); + return result; + } + if (!mWifiConfigManager.enableNetwork( + result.getNetworkId(), false, message.sendingUid)) { + loge("SAVE_NETWORK enabling config=" + config + " failed"); + messageHandlingStatus = MESSAGE_HANDLING_STATUS_FAIL; + replyToMessage(message, WifiManager.SAVE_NETWORK_FAILED, WifiManager.ERROR); + return new NetworkUpdateResult(WifiConfiguration.INVALID_NETWORK_ID); + } + broadcastWifiCredentialChanged(WifiManager.WIFI_CREDENTIAL_SAVED, config); + replyToMessage(message, WifiManager.SAVE_NETWORK_SUCCEEDED); + return result; + } + private static String getLinkPropertiesSummary(LinkProperties lp) { List<String> attributes = new ArrayList<>(6); if (lp.hasIPv4Address()) { diff --git a/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java b/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java index 07840fdac..d54e8f877 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java @@ -139,6 +139,7 @@ public class WifiStateMachineTest { : WifiStateMachine.NUM_LOG_RECS_VERBOSE); private static final int FRAMEWORK_NETWORK_ID = 7; private static final int TEST_RSSI = -54; + private static final int TEST_NETWORK_ID = 54; private static final int WPS_SUPPLICANT_NETWORK_ID = 5; private static final int WPS_FRAMEWORK_NETWORK_ID = 10; private static final String DEFAULT_TEST_SSID = "\"GoogleGuest\""; @@ -750,18 +751,15 @@ public class WifiStateMachineTest { (Intent) argThat(new WifiEnablingStateIntentMatcher()), any()); } - /** - * Verifies that configs can be removed when in client mode. - */ - @Test - public void canRemoveNetworkConfigInClientMode() throws Exception { + private void canRemoveNetwork() { boolean result; when(mWifiConfigManager.removeNetwork(eq(0), anyInt())).thenReturn(true); - initializeAndAddNetworkAndVerifySuccess(); mLooper.startAutoDispatch(); result = mWsm.syncRemoveNetwork(mWsmAsyncChannel, 0); mLooper.stopAutoDispatch(); + assertTrue(result); + verify(mWifiConfigManager).removeNetwork(anyInt(), anyInt()); } /** @@ -769,23 +767,21 @@ public class WifiStateMachineTest { */ @Test public void canRemoveNetworkConfigWhenWifiDisabled() { - boolean result; - when(mWifiConfigManager.removeNetwork(eq(0), anyInt())).thenReturn(true); - mLooper.startAutoDispatch(); - result = mWsm.syncRemoveNetwork(mWsmAsyncChannel, 0); - mLooper.stopAutoDispatch(); - - assertTrue(result); - verify(mWifiConfigManager).removeNetwork(anyInt(), anyInt()); + canRemoveNetwork(); } + /** - * Verifies that configs can be forgotten when in client mode. + * Verifies that configs can be removed when in client mode. */ @Test - public void canForgetNetworkConfigInClientMode() throws Exception { - when(mWifiConfigManager.removeNetwork(eq(0), anyInt())).thenReturn(true); + public void canRemoveNetworkConfigInClientMode() throws Exception { initializeAndAddNetworkAndVerifySuccess(); + canRemoveNetwork(); + } + + private void canForgetNetwork() { + when(mWifiConfigManager.removeNetwork(eq(0), anyInt())).thenReturn(true); mWsm.sendMessage(WifiManager.FORGET_NETWORK, 0, MANAGED_PROFILE_UID); mLooper.dispatchAll(); verify(mWifiConfigManager).removeNetwork(anyInt(), anyInt()); @@ -796,10 +792,109 @@ public class WifiStateMachineTest { */ @Test public void canForgetNetworkConfigWhenWifiDisabled() throws Exception { - when(mWifiConfigManager.removeNetwork(eq(0), anyInt())).thenReturn(true); - mWsm.sendMessage(WifiManager.FORGET_NETWORK, 0, MANAGED_PROFILE_UID); - mLooper.dispatchAll(); - verify(mWifiConfigManager).removeNetwork(anyInt(), anyInt()); + canForgetNetwork(); + } + + /** + * Verifies that configs can be forgotten when in client mode. + */ + @Test + public void canForgetNetworkConfigInClientMode() throws Exception { + initializeAndAddNetworkAndVerifySuccess(); + canForgetNetwork(); + } + + private void canSaveNetworkConfig() { + WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); + + int networkId = TEST_NETWORK_ID; + when(mWifiConfigManager.addOrUpdateNetwork(any(WifiConfiguration.class), anyInt())) + .thenReturn(new NetworkUpdateResult(networkId)); + when(mWifiConfigManager.enableNetwork(eq(networkId), eq(false), anyInt())) + .thenReturn(true); + + mLooper.startAutoDispatch(); + Message reply = mWsmAsyncChannel.sendMessageSynchronously(WifiManager.SAVE_NETWORK, config); + mLooper.stopAutoDispatch(); + assertEquals(WifiManager.SAVE_NETWORK_SUCCEEDED, reply.what); + + verify(mWifiConfigManager).addOrUpdateNetwork(any(WifiConfiguration.class), anyInt()); + verify(mWifiConfigManager).enableNetwork(eq(networkId), eq(false), anyInt()); + } + + /** + * Verifies that configs can be saved when not in client mode. + */ + @Test + public void canSaveNetworkConfigWhenWifiDisabled() throws Exception { + canSaveNetworkConfig(); + } + + /** + * Verifies that configs can be saved when in client mode. + */ + @Test + public void canSaveNetworkConfigInClientMode() throws Exception { + loadComponentsInStaMode(); + canSaveNetworkConfig(); + } + + /** + * Verifies that null configs are rejected in SAVE_NETWORK message. + */ + @Test + public void saveNetworkConfigFailsWithNullConfig() throws Exception { + mLooper.startAutoDispatch(); + Message reply = mWsmAsyncChannel.sendMessageSynchronously(WifiManager.SAVE_NETWORK, null); + mLooper.stopAutoDispatch(); + assertEquals(WifiManager.SAVE_NETWORK_FAILED, reply.what); + + verify(mWifiConfigManager, never()) + .addOrUpdateNetwork(any(WifiConfiguration.class), anyInt()); + verify(mWifiConfigManager, never()) + .enableNetwork(anyInt(), anyBoolean(), anyInt()); + } + + /** + * Verifies that configs save fails when the addition of network fails. + */ + @Test + public void saveNetworkConfigFailsWithConfigAddFailure() throws Exception { + WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); + + when(mWifiConfigManager.addOrUpdateNetwork(any(WifiConfiguration.class), anyInt())) + .thenReturn(new NetworkUpdateResult(WifiConfiguration.INVALID_NETWORK_ID)); + + mLooper.startAutoDispatch(); + Message reply = mWsmAsyncChannel.sendMessageSynchronously(WifiManager.SAVE_NETWORK, config); + mLooper.stopAutoDispatch(); + assertEquals(WifiManager.SAVE_NETWORK_FAILED, reply.what); + + verify(mWifiConfigManager).addOrUpdateNetwork(any(WifiConfiguration.class), anyInt()); + verify(mWifiConfigManager, never()) + .enableNetwork(anyInt(), anyBoolean(), anyInt()); + } + + /** + * Verifies that configs save fails when the enable of network fails. + */ + @Test + public void saveNetworkConfigFailsWithConfigEnableFailure() throws Exception { + WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); + + int networkId = 5; + when(mWifiConfigManager.addOrUpdateNetwork(any(WifiConfiguration.class), anyInt())) + .thenReturn(new NetworkUpdateResult(networkId)); + when(mWifiConfigManager.enableNetwork(eq(networkId), eq(false), anyInt())) + .thenReturn(false); + + mLooper.startAutoDispatch(); + Message reply = mWsmAsyncChannel.sendMessageSynchronously(WifiManager.SAVE_NETWORK, config); + mLooper.stopAutoDispatch(); + assertEquals(WifiManager.SAVE_NETWORK_FAILED, reply.what); + + verify(mWifiConfigManager).addOrUpdateNetwork(any(WifiConfiguration.class), anyInt()); + verify(mWifiConfigManager).enableNetwork(eq(networkId), eq(false), anyInt()); } /** |