diff options
3 files changed, 71 insertions, 30 deletions
diff --git a/service/java/com/android/server/wifi/WifiNetworkFactory.java b/service/java/com/android/server/wifi/WifiNetworkFactory.java index ee43845df..38d36f82f 100644 --- a/service/java/com/android/server/wifi/WifiNetworkFactory.java +++ b/service/java/com/android/server/wifi/WifiNetworkFactory.java @@ -701,22 +701,26 @@ public class WifiNetworkFactory extends NetworkFactory { // Disable Auto-join so that NetworkFactory can take control of the network connection. mWifiConnectivityManager.setSpecificNetworkRequestInProgress(true); + // Copy over the credentials from the app's request and then copy the ssid from user + // selection. + WifiConfiguration networkToConnect = + new WifiConfiguration(mActiveSpecificNetworkRequestSpecifier.wifiConfiguration); + networkToConnect.SSID = network.SSID; // If the request is for a specific SSID and BSSID, then set WifiConfiguration.BSSID field // to prevent roaming. if (isActiveRequestForSingleAccessPoint()) { - network.BSSID = + networkToConnect.BSSID = mActiveSpecificNetworkRequestSpecifier.bssidPatternMatcher.first.toString(); } - // Mark the network ephemeral so that it's automatically removed at the end of connection. - network.ephemeral = true; - network.fromWifiNetworkSpecifier = true; + networkToConnect.ephemeral = true; + networkToConnect.fromWifiNetworkSpecifier = true; // Store the user selected network. - mUserSelectedNetwork = network; + mUserSelectedNetwork = networkToConnect; // Trigger connection to the network. - connectToNetwork(network); + connectToNetwork(networkToConnect); } private void handleConnectToNetworkUserSelection(WifiConfiguration network) { @@ -729,7 +733,7 @@ public class WifiNetworkFactory extends NetworkFactory { handleConnectToNetworkUserSelectionInternal(network); // Add the network to the approved access point map for the app. - addNetworkToUserApprovedAccessPointMap(network); + addNetworkToUserApprovedAccessPointMap(mUserSelectedNetwork); } private void handleRejectUserSelection() { diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConfigurationTestUtil.java b/tests/wifitests/src/com/android/server/wifi/WifiConfigurationTestUtil.java index aeaba182d..c114aa10d 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConfigurationTestUtil.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConfigurationTestUtil.java @@ -645,6 +645,8 @@ public class WifiConfigurationTestUtil { assertCommonConfigurationElementsEqual(expected, actual); assertEquals(expected.networkId, actual.networkId); assertEquals(expected.ephemeral, actual.ephemeral); + assertEquals(expected.fromWifiNetworkSuggestion, actual.fromWifiNetworkSuggestion); + assertEquals(expected.fromWifiNetworkSpecifier, actual.fromWifiNetworkSpecifier); assertEquals(expected.trusted, actual.trusted); } diff --git a/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java b/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java index a745ca116..975b4307b 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java @@ -69,6 +69,7 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; +import org.mockito.ArgumentMatcher; import org.mockito.InOrder; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -101,6 +102,7 @@ public class WifiNetworkFactoryTest { private static final String TEST_BSSID_4 = "45:34:34:56:ee:ff"; private static final String TEST_BSSID_1_2_OUI = "12:34:23:00:00:00"; private static final String TEST_BSSID_OUI_MASK = "ff:ff:ff:00:00:00"; + private static final String TEST_WPA_PRESHARED_KEY = "\"password123\""; @Mock Context mContext; @Mock ActivityManager mActivityManager; @@ -971,8 +973,9 @@ public class WifiNetworkFactoryTest { assertNotNull(networkRequestUserSelectionCallback); // Now trigger user selection to one of the network. - WifiConfiguration selectedNetwork = WifiConfigurationTestUtil.createOpenNetwork(); - networkRequestUserSelectionCallback.select(selectedNetwork); + mSelectedNetwork = WifiConfigurationTestUtil.createPskNetwork(); + mSelectedNetwork.SSID = "\"" + mTestScanDatas[0].getResults()[0].SSID + "\""; + networkRequestUserSelectionCallback.select(mSelectedNetwork); mLooper.dispatchAll(); // Cancel periodic scans. @@ -986,9 +989,14 @@ public class WifiNetworkFactoryTest { wifiConfigurationCaptor.capture(), eq(TEST_UID_1), eq(TEST_PACKAGE_NAME_1)); WifiConfiguration network = wifiConfigurationCaptor.getValue(); assertNotNull(network); - WifiConfigurationTestUtil.assertConfigurationEqual(selectedNetwork, network); - assertTrue(network.ephemeral); - assertTrue(network.fromWifiNetworkSpecifier); + WifiConfiguration expectedWifiConfiguration = + new WifiConfiguration(((WifiNetworkSpecifier) mNetworkRequest.networkCapabilities + .getNetworkSpecifier()).wifiConfiguration); + expectedWifiConfiguration.SSID = mSelectedNetwork.SSID; + expectedWifiConfiguration.preSharedKey = TEST_WPA_PRESHARED_KEY; + expectedWifiConfiguration.ephemeral = true; + expectedWifiConfiguration.fromWifiNetworkSpecifier = true; + WifiConfigurationTestUtil.assertConfigurationEqual(expectedWifiConfiguration, network); ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class); verify(mClientModeImpl).sendMessage(messageCaptor.capture()); @@ -1012,16 +1020,17 @@ public class WifiNetworkFactoryTest { mNetworkRequestUserSelectionCallback.getValue(); assertNotNull(networkRequestUserSelectionCallback); - WifiConfiguration selectedNetwork = WifiConfigurationTestUtil.createOpenNetwork(); + mSelectedNetwork = WifiConfigurationTestUtil.createPskNetwork(); + mSelectedNetwork.SSID = "\"" + mTestScanDatas[0].getResults()[0].SSID + "\""; // Have a saved network with the same configuration. - WifiConfiguration matchingSavedNetwork = new WifiConfiguration(selectedNetwork); + WifiConfiguration matchingSavedNetwork = new WifiConfiguration(mSelectedNetwork); matchingSavedNetwork.networkId = TEST_NETWORK_ID_1; - when(mWifiConfigManager.getConfiguredNetwork(selectedNetwork.configKey())) + when(mWifiConfigManager.getConfiguredNetwork(mSelectedNetwork.configKey())) .thenReturn(matchingSavedNetwork); // Now trigger user selection to one of the network. - networkRequestUserSelectionCallback.select(selectedNetwork); + networkRequestUserSelectionCallback.select(mSelectedNetwork); mLooper.dispatchAll(); // Cancel periodic scans. @@ -1062,6 +1071,7 @@ public class WifiNetworkFactoryTest { MacAddress.BROADCAST_ADDRESS); WifiConfiguration wifiConfiguration = new WifiConfiguration(); wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); + wifiConfiguration.preSharedKey = TEST_WPA_PRESHARED_KEY; WifiNetworkSpecifier specifier = new WifiNetworkSpecifier( ssidPatternMatch, bssidPatternMatch, wifiConfiguration, TEST_UID_1, TEST_PACKAGE_NAME_1); @@ -1075,6 +1085,7 @@ public class WifiNetworkFactoryTest { // Now trigger user selection to the network. mSelectedNetwork = ScanResultUtil.createNetworkFromScanResult(matchingScanResult); + mSelectedNetwork.SSID = "\"" + matchingScanResult.SSID + "\""; INetworkRequestUserSelectionCallback networkRequestUserSelectionCallback = mNetworkRequestUserSelectionCallback.getValue(); assertNotNull(networkRequestUserSelectionCallback); @@ -1088,11 +1099,15 @@ public class WifiNetworkFactoryTest { wifiConfigurationCaptor.capture(), eq(TEST_UID_1), eq(TEST_PACKAGE_NAME_1)); WifiConfiguration network = wifiConfigurationCaptor.getValue(); assertNotNull(network); - WifiConfiguration expectedWifiConfiguration = new WifiConfiguration(mSelectedNetwork); + WifiConfiguration expectedWifiConfiguration = + new WifiConfiguration(((WifiNetworkSpecifier) mNetworkRequest.networkCapabilities + .getNetworkSpecifier()).wifiConfiguration); + expectedWifiConfiguration.SSID = mSelectedNetwork.SSID; + expectedWifiConfiguration.preSharedKey = TEST_WPA_PRESHARED_KEY; expectedWifiConfiguration.BSSID = matchingScanResult.BSSID; + expectedWifiConfiguration.ephemeral = true; + expectedWifiConfiguration.fromWifiNetworkSpecifier = true; WifiConfigurationTestUtil.assertConfigurationEqual(expectedWifiConfiguration, network); - assertTrue(network.ephemeral); - assertTrue(network.fromWifiNetworkSpecifier); // Verify connection message. ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class); @@ -1149,7 +1164,8 @@ public class WifiNetworkFactoryTest { // Fail the request after all the retries are exhausted. verify(mNetworkRequestMatchCallback).onAbort(); // Verify that we sent the connection failure callback. - verify(mNetworkRequestMatchCallback).onUserSelectionConnectFailure(mSelectedNetwork); + verify(mNetworkRequestMatchCallback).onUserSelectionConnectFailure( + argThat(new WifiConfigMatcher(mSelectedNetwork))); // Verify we reset the network request handling. verify(mWifiConnectivityManager).setSpecificNetworkRequestInProgress(false); } @@ -1176,7 +1192,8 @@ public class WifiNetworkFactoryTest { // Fail the request after all the retries are exhausted. verify(mNetworkRequestMatchCallback).onAbort(); // Verify that we sent the connection failure callback. - verify(mNetworkRequestMatchCallback).onUserSelectionConnectFailure(mSelectedNetwork); + verify(mNetworkRequestMatchCallback).onUserSelectionConnectFailure( + argThat(new WifiConfigMatcher(mSelectedNetwork))); // verify we canceled the timeout alarm. mInOrder.verify(mAlarmManager).cancel( mConnectionTimeoutAlarmListenerArgumentCaptor.getValue()); @@ -1207,7 +1224,8 @@ public class WifiNetworkFactoryTest { verify(mNetworkRequestMatchCallback).onAbort(); // Verify that we sent the connection failure callback. - verify(mNetworkRequestMatchCallback).onUserSelectionConnectFailure(mSelectedNetwork); + verify(mNetworkRequestMatchCallback).onUserSelectionConnectFailure( + argThat(new WifiConfigMatcher(mSelectedNetwork))); // verify we canceled the timeout alarm. mInOrder.verify(mAlarmManager).cancel( mConnectionTimeoutAlarmListenerArgumentCaptor.getValue()); @@ -1230,8 +1248,7 @@ public class WifiNetworkFactoryTest { WifiMetrics.ConnectionEvent.FAILURE_DHCP, connectedNetwork); // Verify that we did not send the connection failure callback. - verify(mNetworkRequestMatchCallback, never()) - .onUserSelectionConnectFailure(mSelectedNetwork); + verify(mNetworkRequestMatchCallback, never()).onUserSelectionConnectFailure(any()); // verify we canceled the timeout alarm. verify(mAlarmManager, never()) .cancel(mConnectionTimeoutAlarmListenerArgumentCaptor.getValue()); @@ -1251,7 +1268,8 @@ public class WifiNetworkFactoryTest { validateConnectionRetryAttempts(); // Verify that we sent the connection failure callback. - verify(mNetworkRequestMatchCallback).onUserSelectionConnectFailure(mSelectedNetwork); + verify(mNetworkRequestMatchCallback).onUserSelectionConnectFailure( + argThat(new WifiConfigMatcher(mSelectedNetwork))); // verify we canceled the timeout alarm. mInOrder.verify(mAlarmManager).cancel( mConnectionTimeoutAlarmListenerArgumentCaptor.getValue()); @@ -1272,7 +1290,8 @@ public class WifiNetworkFactoryTest { WifiMetrics.ConnectionEvent.FAILURE_NONE, mSelectedNetwork); // Verify that we sent the connection success callback. - verify(mNetworkRequestMatchCallback).onUserSelectionConnectSuccess(mSelectedNetwork); + verify(mNetworkRequestMatchCallback).onUserSelectionConnectSuccess( + argThat(new WifiConfigMatcher(mSelectedNetwork))); // verify we canceled the timeout alarm. verify(mAlarmManager).cancel(mConnectionTimeoutAlarmListenerArgumentCaptor.getValue()); } @@ -1292,8 +1311,7 @@ public class WifiNetworkFactoryTest { WifiMetrics.ConnectionEvent.FAILURE_NONE, connectedNetwork); // verify that we did not send out the success callback and did not stop the alarm timeout. - verify(mNetworkRequestMatchCallback, never()) - .onUserSelectionConnectSuccess(mSelectedNetwork); + verify(mNetworkRequestMatchCallback, never()).onUserSelectionConnectSuccess(any()); verify(mAlarmManager, never()) .cancel(mConnectionTimeoutAlarmListenerArgumentCaptor.getValue()); @@ -1302,7 +1320,8 @@ public class WifiNetworkFactoryTest { WifiMetrics.ConnectionEvent.FAILURE_NONE, mSelectedNetwork); // Verify that we sent the connection success callback. - verify(mNetworkRequestMatchCallback).onUserSelectionConnectSuccess(mSelectedNetwork); + verify(mNetworkRequestMatchCallback).onUserSelectionConnectSuccess( + argThat(new WifiConfigMatcher(mSelectedNetwork))); // verify we canceled the timeout alarm. verify(mAlarmManager).cancel(mConnectionTimeoutAlarmListenerArgumentCaptor.getValue()); } @@ -1320,7 +1339,8 @@ public class WifiNetworkFactoryTest { WifiMetrics.ConnectionEvent.FAILURE_NONE, mSelectedNetwork); // Verify that we sent the connection success callback. - verify(mNetworkRequestMatchCallback).onUserSelectionConnectSuccess(mSelectedNetwork); + verify(mNetworkRequestMatchCallback).onUserSelectionConnectSuccess( + argThat(new WifiConfigMatcher(mSelectedNetwork))); // verify we canceled the timeout alarm. verify(mAlarmManager).cancel(mConnectionTimeoutAlarmListenerArgumentCaptor.getValue()); @@ -2163,6 +2183,7 @@ public class WifiNetworkFactoryTest { Pair<MacAddress, MacAddress> bssidPatternMatch = Pair.create(MacAddress.ALL_ZEROS_ADDRESS, MacAddress.ALL_ZEROS_ADDRESS); WifiConfiguration wifiConfiguration = WifiConfigurationTestUtil.createPskNetwork(); + wifiConfiguration.preSharedKey = TEST_WPA_PRESHARED_KEY; WifiNetworkSpecifier specifier = new WifiNetworkSpecifier( ssidPatternMatch, bssidPatternMatch, wifiConfiguration, TEST_UID_1, TEST_PACKAGE_NAME_1); @@ -2336,6 +2357,20 @@ public class WifiNetworkFactoryTest { WorkSource workSource = mWorkSourceArgumentCaptor.getValue(); assertNotNull(workSource); assertEquals(TEST_UID_1, workSource.get(0)); + } + + class WifiConfigMatcher implements ArgumentMatcher<WifiConfiguration> { + private final WifiConfiguration mConfig; + WifiConfigMatcher(WifiConfiguration config) { + assertNotNull(config); + mConfig = config; + } + + @Override + public boolean matches(WifiConfiguration otherConfig) { + if (otherConfig == null) return false; + return mConfig.configKey().equals(otherConfig.configKey()); + } } } |