ClientModeImpl#connect, save: move code into helper methods
Moved code into helper methods to prepare for the next
stage of the refactor.
Bug: 138403307
Test: atest FrameworksWifiTests
Change-Id: I0d16c3825a8bfda375f840a4e93d0623a9c67dee
diff --git a/service/java/com/android/server/wifi/ClientModeImpl.java b/service/java/com/android/server/wifi/ClientModeImpl.java
index d4826b4..9e0b7c6 100644
--- a/service/java/com/android/server/wifi/ClientModeImpl.java
+++ b/service/java/com/android/server/wifi/ClientModeImpl.java
@@ -6029,16 +6029,13 @@
/**
* Trigger network connection and provide status via the provided callback.
+ *
+ * For a new network, a config is passed to create and connect, and netId is ignored.
+ * For an existing network, a network id is passed along with a null config.
*/
public void connect(WifiConfiguration config, int netId,
@Nullable IActionListener callback, int callingUid) {
mWifiThreadRunner.post(() -> {
- /*
- * The connect message can contain a network id passed as arg1 on message or
- * or a config passed as obj on message.
- * For a new network, a config is passed to create and connect.
- * For an existing network, a network id is passed
- */
ActionListenerWrapper wrapper = new ActionListenerWrapper(callback);
NetworkUpdateResult result = null;
if (config != null) {
@@ -6058,23 +6055,19 @@
result = new NetworkUpdateResult(netId);
}
final int networkId = result.getNetworkId();
- mWifiConfigManager.userEnabledNetwork(networkId);
- if (!mWifiConfigManager.enableNetwork(networkId, true, callingUid, null)
- || !mWifiConfigManager.updateLastConnectUid(networkId, callingUid)) {
- logi("connect Allowing uid " + callingUid
- + " with insufficient permissions to connect=" + networkId);
- } else if (mWifiPermissionsUtil.checkNetworkSettingsPermission(callingUid)) {
- // Note user connect choice here, so that it will be considered in the
- // next network selection.
- mWifiConfigManager.setUserConnectChoice(networkId);
- }
- Message message =
- obtainMessage(CMD_CONNECT_NETWORK, new ConnectNetworkMessage(result, wrapper));
- message.sendingUid = callingUid;
- sendMessage(message);
+ mWifiConfigManager.updateBeforeConnectNetwork(networkId, callingUid);
+ sendConnectMessage(result, wrapper, callingUid);
});
}
+ private void sendConnectMessage(NetworkUpdateResult result, ActionListenerWrapper wrapper,
+ int callingUid) {
+ Message message =
+ obtainMessage(CMD_CONNECT_NETWORK, new ConnectNetworkMessage(result, wrapper));
+ message.sendingUid = callingUid;
+ sendMessage(message);
+ }
+
/**
* Trigger network save and provide status via the provided callback.
*/
@@ -6101,13 +6094,20 @@
return;
}
broadcastWifiCredentialChanged(WifiManager.WIFI_CREDENTIAL_SAVED, config);
- Message message =
- obtainMessage(CMD_SAVE_NETWORK, new ConnectNetworkMessage(result, wrapper));
- message.sendingUid = callingUid;
- sendMessage(message);
+ sendSaveMessage(result, wrapper, callingUid);
});
}
+
+
+ private void sendSaveMessage(NetworkUpdateResult result, ActionListenerWrapper wrapper,
+ int callingUid) {
+ Message message =
+ obtainMessage(CMD_SAVE_NETWORK, new ConnectNetworkMessage(result, wrapper));
+ message.sendingUid = callingUid;
+ sendMessage(message);
+ }
+
/**
* Trigger network forget and provide status via the provided callback.
*/
diff --git a/service/java/com/android/server/wifi/WifiConfigManager.java b/service/java/com/android/server/wifi/WifiConfigManager.java
index 829dd77..20a78ff 100644
--- a/service/java/com/android/server/wifi/WifiConfigManager.java
+++ b/service/java/com/android/server/wifi/WifiConfigManager.java
@@ -1968,7 +1968,7 @@
* @param uid uid of the app requesting the connection.
* @return true if the network was found, false otherwise.
*/
- public boolean updateLastConnectUid(int networkId, int uid) {
+ private boolean updateLastConnectUid(int networkId, int uid) {
if (mVerboseLoggingEnabled) {
Log.v(TAG, "Update network last connect UID for " + networkId);
}
@@ -2685,13 +2685,14 @@
/**
* User enabled network manually, maybe trigger by user select to connect network.
* @param networkId enabled network id.
+ * @return true if the operation succeeded, false otherwise.
*/
- public void userEnabledNetwork(int networkId) {
+ public boolean userEnabledNetwork(int networkId) {
WifiConfiguration configuration = getInternalConfiguredNetwork(networkId);
if (configuration == null) {
- return;
+ return false;
}
- String network;
+ final String network;
if (configuration.isPasspoint()) {
network = configuration.FQDN;
} else {
@@ -2701,6 +2702,7 @@
mBssidBlocklistMonitor.clearBssidBlocklistForSsid(configuration.SSID);
Log.d(TAG, "Enable disabled network: " + network + " num="
+ mUserTemporarilyDisabledList.size());
+ return true;
}
/**
@@ -3344,4 +3346,27 @@
}
return change;
}
+
+ /** Update WifiConfigManager before connecting to a network. */
+ public boolean updateBeforeConnectNetwork(int networkId, int callingUid) {
+ if (!userEnabledNetwork(networkId)) {
+ return false;
+ }
+ if (!enableNetwork(networkId, true, callingUid, null)) {
+ Log.i(TAG, "connect Allowing uid " + callingUid
+ + " with insufficient permissions to connect=" + networkId);
+ return false;
+ }
+ if (!updateLastConnectUid(networkId, callingUid)) {
+ Log.i(TAG, "connect Allowing uid " + callingUid
+ + " with insufficient permissions to connect=" + networkId);
+ return false;
+ }
+ if (mWifiPermissionsUtil.checkNetworkSettingsPermission(callingUid)) {
+ // Note user connect choice here, so that it will be considered in the
+ // next network selection.
+ setUserConnectChoice(networkId);
+ }
+ return true;
+ }
}
diff --git a/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java b/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java
index 0b284ef..c634a69 100644
--- a/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java
@@ -849,11 +849,6 @@
}
private void setupAndStartConnectSequence(WifiConfiguration config) throws Exception {
- when(mWifiConfigManager.enableNetwork(
- eq(config.networkId), eq(true), anyInt(), any()))
- .thenReturn(true);
- when(mWifiConfigManager.updateLastConnectUid(eq(config.networkId), anyInt()))
- .thenReturn(true);
when(mWifiConfigManager.getConfiguredNetwork(eq(config.networkId)))
.thenReturn(config);
when(mWifiConfigManager.getConfiguredNetworkWithoutMasking(
@@ -869,10 +864,7 @@
}
private void validateSuccessfulConnectSequence(WifiConfiguration config) {
- verify(mWifiConfigManager).userEnabledNetwork(config.networkId);
- verify(mWifiConfigManager).enableNetwork(
- eq(config.networkId), eq(true), anyInt(), any());
- verify(mWifiConfigManager).setUserConnectChoice(config.networkId);
+ verify(mWifiConfigManager).updateBeforeConnectNetwork(eq(config.networkId), anyInt());
verify(mWifiConnectivityManager).prepareForForcedConnection(eq(config.networkId));
verify(mWifiConfigManager).getConfiguredNetworkWithoutMasking(eq(config.networkId));
verify(mWifiNative).connectToNetwork(eq(WIFI_IFACE_NAME), eq(config));
@@ -880,9 +872,7 @@
}
private void validateFailureConnectSequence(WifiConfiguration config) {
- verify(mWifiConfigManager).enableNetwork(
- eq(config.networkId), eq(true), anyInt(), any());
- verify(mWifiConfigManager).setUserConnectChoice(config.networkId);
+ verify(mWifiConfigManager).updateBeforeConnectNetwork(eq(config.networkId), anyInt());
verify(mWifiConnectivityManager).prepareForForcedConnection(eq(config.networkId));
verify(mWifiConfigManager, never())
.getConfiguredNetworkWithoutMasking(eq(config.networkId));
@@ -922,9 +912,7 @@
when(mWifiPermissionsUtil.checkNetworkSettingsPermission(Process.myUid()))
.thenReturn(false);
setupAndStartConnectSequence(config);
- verify(mWifiConfigManager).enableNetwork(
- eq(config.networkId), eq(true), anyInt(), any());
- verify(mWifiConfigManager, never()).setUserConnectChoice(config.networkId);
+ verify(mWifiConfigManager).updateBeforeConnectNetwork(eq(config.networkId), anyInt());
verify(mWifiConnectivityManager).prepareForForcedConnection(eq(config.networkId));
verify(mWifiConfigManager).getConfiguredNetworkWithoutMasking(eq(config.networkId));
verify(mWifiNative).connectToNetwork(eq(WIFI_IFACE_NAME), eq(config));
@@ -1419,14 +1407,12 @@
WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork();
config.networkId = FRAMEWORK_NETWORK_ID + 1;
setupAndStartConnectSequence(config);
- verify(mWifiConfigManager).enableNetwork(
- eq(config.networkId), eq(true), anyInt(), any());
- verify(mWifiConfigManager, never()).setUserConnectChoice(config.networkId);
+ verify(mWifiConfigManager).updateBeforeConnectNetwork(eq(config.networkId), anyInt());
verify(mWifiConnectivityManager).prepareForForcedConnection(eq(config.networkId));
verify(mWifiConfigManager, never())
.getConfiguredNetworkWithoutMasking(eq(config.networkId));
verify(mWifiNative, never()).connectToNetwork(eq(WIFI_IFACE_NAME), eq(config));
- verify(mWifiPermissionsUtil, times(4)).checkNetworkSettingsPermission(anyInt());
+ verify(mWifiPermissionsUtil, times(2)).checkNetworkSettingsPermission(anyInt());
}
@Test
@@ -1441,8 +1427,7 @@
mLooper.dispatchAll();
verify(connectActionListener).onFailure(anyInt());
- verify(mWifiConfigManager, never()).enableNetwork(eq(0), eq(true), anyInt(), any());
- verify(mWifiConfigManager, never()).updateLastConnectUid(eq(0), anyInt());
+ verify(mWifiConfigManager, never()).updateBeforeConnectNetwork(anyInt(), anyInt());
}
/**
@@ -1507,7 +1492,7 @@
mLooper.dispatchAll();
verify(connectActionListener).onSuccess();
- verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt(), any());
+ verify(mWifiConfigManager).updateBeforeConnectNetwork(eq(0), anyInt());
mCmi.sendMessage(WifiMonitor.NETWORK_CONNECTION_EVENT, 0, 0, sBSSID);
mLooper.dispatchAll();
@@ -1565,7 +1550,7 @@
mLooper.dispatchAll();
verify(connectActionListener).onSuccess();
- verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt(), any());
+ verify(mWifiConfigManager).updateBeforeConnectNetwork(eq(0), anyInt());
mCmi.sendMessage(WifiMonitor.SUPPLICANT_STATE_CHANGE_EVENT, 0, 0,
new StateChangeResult(0, sWifiSsid, sBSSID, SupplicantState.ASSOCIATED));
@@ -1625,7 +1610,7 @@
mLooper.dispatchAll();
verify(connectActionListener).onSuccess();
- verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt(), any());
+ verify(mWifiConfigManager).updateBeforeConnectNetwork(eq(0), anyInt());
WifiConfiguration config = createTestNetwork(false);
config.getNetworkSelectionStatus().setHasEverConnected(true);
@@ -1659,7 +1644,7 @@
mLooper.dispatchAll();
verify(connectActionListener).onSuccess();
- verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt(), any());
+ verify(mWifiConfigManager).updateBeforeConnectNetwork(eq(0), anyInt());
WifiConfiguration config = new WifiConfiguration();
config.SSID = sSSID;
@@ -1696,7 +1681,7 @@
mLooper.dispatchAll();
verify(connectActionListener).onSuccess();
- verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt(), any());
+ verify(mWifiConfigManager).updateBeforeConnectNetwork(eq(0), anyInt());
WifiConfiguration config = new WifiConfiguration();
config.SSID = sSSID;
@@ -1740,7 +1725,7 @@
mLooper.dispatchAll();
verify(connectActionListener).onSuccess();
- verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt(), any());
+ verify(mWifiConfigManager).updateBeforeConnectNetwork(eq(0), anyInt());
WifiConfiguration config = new WifiConfiguration();
config.getNetworkSelectionStatus().setHasEverConnected(true);
@@ -1769,7 +1754,7 @@
mLooper.dispatchAll();
verify(connectActionListener).onSuccess();
- verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt(), any());
+ verify(mWifiConfigManager).updateBeforeConnectNetwork(eq(0), anyInt());
when(mWifiConfigManager.getConfiguredNetwork(anyInt())).thenReturn(null);
@@ -1792,7 +1777,7 @@
mLooper.dispatchAll();
verify(connectActionListener).onSuccess();
- verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt(), any());
+ verify(mWifiConfigManager).updateBeforeConnectNetwork(eq(0), anyInt());
DisconnectEventInfo disconnectEventInfo =
new DisconnectEventInfo(sSSID, sBSSID, 0, false);
@@ -2002,10 +1987,6 @@
public void disconnectFromNetworkWhenRemovedWhileObtainingIpAddr() throws Exception {
initializeAndAddNetworkAndVerifySuccess();
- when(mWifiConfigManager.enableNetwork(eq(0), eq(true), anyInt(), any()))
- .thenReturn(true);
- when(mWifiConfigManager.updateLastConnectUid(eq(0), anyInt())).thenReturn(true);
-
verify(mWifiNative).removeAllNetworks(WIFI_IFACE_NAME);
IActionListener connectActionListener = mock(IActionListener.class);
@@ -2013,8 +1994,6 @@
mLooper.dispatchAll();
verify(connectActionListener).onSuccess();
- verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt(), any());
- verify(mWifiConfigManager).setUserConnectChoice(0);
when(mWifiConfigManager.getScanDetailCacheForNetwork(FRAMEWORK_NETWORK_ID))
.thenReturn(mScanDetailCache);
@@ -2278,17 +2257,6 @@
}
/**
- * Adds the network without putting ClientModeImpl into ConnectableState.
- */
- @Test
- public void addNetworkInDefaultState() throws Exception {
- // We should not be in initial state now.
- assertTrue("DefaultState".equals(getCurrentState().getName()));
- initializeMocksForAddedNetwork(false);
- verify(mWifiConfigManager, never()).setUserConnectChoice(0);
- }
-
- /**
* Verifies that ClientModeImpl sets and unsets appropriate 'RecentFailureReason' values
* on a WifiConfiguration when it fails association, authentication, or successfully connects
*/
@@ -4022,7 +3990,7 @@
mLooper.dispatchAll();
verify(connectActionListener).onSuccess();
- verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt(), any());
+ verify(mWifiConfigManager).updateBeforeConnectNetwork(eq(0), anyInt());
mCmi.sendMessage(WifiMonitor.NETWORK_CONNECTION_EVENT, 0, 0, sBSSID);
mLooper.dispatchAll();
@@ -4972,7 +4940,7 @@
mLooper.dispatchAll();
verify(connectActionListener).onSuccess();
- verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt(), any());
+ verify(mWifiConfigManager).updateBeforeConnectNetwork(eq(0), anyInt());
mCmi.sendMessage(WifiMonitor.NETWORK_CONNECTION_EVENT, 0, 0, sBSSID);
mLooper.dispatchAll();
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java
index 7b06f69..69dfc0f 100644
--- a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java
@@ -1528,14 +1528,13 @@
* {@link WifiConfigManager#updateLastConnectUid(int, int)}.
*/
@Test
- public void testUpdateLastConnectUid() throws Exception {
+ public void testConnectNetworkUpdatesLastConnectUid() throws Exception {
WifiConfiguration openNetwork = WifiConfigurationTestUtil.createOpenNetwork();
NetworkUpdateResult result = verifyAddNetworkToWifiConfigManager(openNetwork);
- assertTrue(
- mWifiConfigManager.updateLastConnectUid(
- result.getNetworkId(), TEST_CREATOR_UID));
+ mWifiConfigManager.updateBeforeConnectNetwork(result.getNetworkId(), TEST_CREATOR_UID);
+
WifiConfiguration retrievedNetwork =
mWifiConfigManager.getConfiguredNetwork(result.getNetworkId());
assertEquals(TEST_CREATOR_UID, retrievedNetwork.lastConnectUid);
@@ -1602,7 +1601,7 @@
result.getNetworkId() + 1, TEST_CREATOR_UID, TEST_CREATOR_NAME));
assertFalse(mWifiConfigManager.updateNetworkSelectionStatus(
result.getNetworkId() + 1, NetworkSelectionStatus.DISABLED_BY_WIFI_MANAGER));
- assertFalse(mWifiConfigManager.updateLastConnectUid(
+ assertFalse(mWifiConfigManager.updateBeforeConnectNetwork(
result.getNetworkId() + 1, TEST_CREATOR_UID));
}
@@ -4531,7 +4530,7 @@
}
@Test
- public void testUserEnableDisabledNetwork() {
+ public void testUserEnableDisabledNetwork() {
when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(true);
WifiConfiguration openNetwork = WifiConfigurationTestUtil.createOpenNetwork();
List<WifiConfiguration> networks = new ArrayList<>();
@@ -5564,4 +5563,104 @@
configNotInLastNetworkSelection.networkId);
assertNull(configNotInLastNetworkSelection.getNetworkSelectionStatus().getConnectChoice());
}
+
+ @Test
+ public void testConnectNetworkSuccess() throws Exception {
+ WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork();
+ config.getNetworkSelectionStatus().setNetworkSelectionStatus(
+ NetworkSelectionStatus.NETWORK_SELECTION_TEMPORARY_DISABLED);
+ config.getNetworkSelectionStatus().setConnectChoice("bogusKey");
+
+ List<WifiConfiguration> sharedConfigs = Arrays.asList(config);
+ // Setup xml storage
+ setupStoreDataForRead(sharedConfigs, Arrays.asList());
+ assertTrue(mWifiConfigManager.loadFromStore());
+ verify(mWifiConfigStore).read();
+
+ config = mWifiConfigManager.getConfiguredNetwork(config.networkId);
+ assertFalse(config.getNetworkSelectionStatus().isNetworkEnabled());
+ assertNotEquals(TEST_CREATOR_UID, config.lastConnectUid);
+ assertEquals("bogusKey", config.getNetworkSelectionStatus().getConnectChoice());
+
+ when(mWifiPermissionsUtil.checkNetworkSettingsPermission(TEST_CREATOR_UID))
+ .thenReturn(true);
+
+ assertTrue(mWifiConfigManager.updateBeforeConnectNetwork(
+ config.networkId, TEST_CREATOR_UID));
+
+ config = mWifiConfigManager.getConfiguredNetwork(config.networkId);
+ // network became enabled
+ assertTrue(config.getNetworkSelectionStatus().isNetworkEnabled());
+ // lastConnectUid updated
+ assertEquals(TEST_CREATOR_UID, config.lastConnectUid);
+ // connect choice was cleared
+ assertNull(config.getNetworkSelectionStatus().getConnectChoice());
+ }
+
+ @Test
+ public void testConnectNetworkSuccessNoNetworkSettingsPermission() throws Exception {
+ WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork();
+ config.getNetworkSelectionStatus().setNetworkSelectionStatus(
+ NetworkSelectionStatus.NETWORK_SELECTION_TEMPORARY_DISABLED);
+ config.getNetworkSelectionStatus().setConnectChoice("bogusKey");
+
+ List<WifiConfiguration> sharedConfigs = Arrays.asList(config);
+ // Setup xml storage
+ setupStoreDataForRead(sharedConfigs, Arrays.asList());
+ assertTrue(mWifiConfigManager.loadFromStore());
+ verify(mWifiConfigStore).read();
+
+ config = mWifiConfigManager.getConfiguredNetwork(config.networkId);
+ assertFalse(config.getNetworkSelectionStatus().isNetworkEnabled());
+ assertNotEquals(TEST_CREATOR_UID, config.lastConnectUid);
+ assertEquals("bogusKey", config.getNetworkSelectionStatus().getConnectChoice());
+
+ when(mWifiPermissionsUtil.checkNetworkSettingsPermission(TEST_CREATOR_UID))
+ .thenReturn(false);
+
+ assertTrue(mWifiConfigManager.updateBeforeConnectNetwork(
+ config.networkId, TEST_CREATOR_UID));
+
+ config = mWifiConfigManager.getConfiguredNetwork(config.networkId);
+ // network became enabled
+ assertTrue(config.getNetworkSelectionStatus().isNetworkEnabled());
+ // lastConnectUid updated
+ assertEquals(TEST_CREATOR_UID, config.lastConnectUid);
+ // connect choice not cleared
+ assertEquals("bogusKey", config.getNetworkSelectionStatus().getConnectChoice());
+ }
+
+ @Test
+ public void testConnectNetworkFailure_UidDoesNotBelongToCurrentUser() throws Exception {
+ WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork();
+ config.getNetworkSelectionStatus().setNetworkSelectionStatus(
+ NetworkSelectionStatus.NETWORK_SELECTION_TEMPORARY_DISABLED);
+ config.getNetworkSelectionStatus().setConnectChoice("bogusKey");
+
+ List<WifiConfiguration> sharedConfigs = Arrays.asList(config);
+ // Setup xml storage
+ setupStoreDataForRead(sharedConfigs, Arrays.asList());
+ assertTrue(mWifiConfigManager.loadFromStore());
+ verify(mWifiConfigStore).read();
+
+ int creatorUid = 10000000;
+
+ config = mWifiConfigManager.getConfiguredNetwork(config.networkId);
+ assertFalse(config.getNetworkSelectionStatus().isNetworkEnabled());
+ assertNotEquals(creatorUid, config.lastConnectUid);
+ assertEquals("bogusKey", config.getNetworkSelectionStatus().getConnectChoice());
+
+ when(mWifiPermissionsUtil.checkNetworkSettingsPermission(creatorUid))
+ .thenReturn(false);
+ when(mUserManager.isSameProfileGroup(any(), any())).thenReturn(false);
+
+ assertFalse(mWifiConfigManager.updateBeforeConnectNetwork(config.networkId, creatorUid));
+
+ // network still disabled
+ assertFalse(config.getNetworkSelectionStatus().isNetworkEnabled());
+ // lastConnectUid wasn't changed
+ assertNotEquals(creatorUid, config.lastConnectUid);
+ // connect choice wasn't changed
+ assertEquals("bogusKey", config.getNetworkSelectionStatus().getConnectChoice());
+ }
}