From 40a6a0e1fb3efb62b81e24b30820c9ffa778b206 Mon Sep 17 00:00:00 2001 From: Roshan Pius Date: Tue, 2 Jun 2020 10:14:00 -0700 Subject: WifiConnectivityManager: Reset connection limiter on force scan Bug: 156708458 Test: atest com.android.server.wifi Change-Id: Ia7f82e382e3764892287f4358eda280de2bea3cb --- .../server/wifi/WifiConnectivityManager.java | 1 + .../server/wifi/WifiConnectivityManagerTest.java | 44 ++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/service/java/com/android/server/wifi/WifiConnectivityManager.java b/service/java/com/android/server/wifi/WifiConnectivityManager.java index ad5a8f26c..2ce0bc024 100644 --- a/service/java/com/android/server/wifi/WifiConnectivityManager.java +++ b/service/java/com/android/server/wifi/WifiConnectivityManager.java @@ -1847,6 +1847,7 @@ public class WifiConnectivityManager { if (!mWifiEnabled) return; localLog("forceConnectivityScan in request of " + workSource); + clearConnectionAttemptTimeStamps(); mWaitForFullBandScanResults = true; startForcedSingleScan(true, workSource); } diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java index 7bf21fb27..ba1c4f976 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java @@ -606,6 +606,50 @@ public class WifiConnectivityManagerTest extends WifiBaseTest { CANDIDATE_NETWORK_ID, Process.WIFI_UID, CANDIDATE_BSSID); } + /** + * Multiple back to back connection attempts after a force connectivity scan should not be rate + * limited. + * + * Expected behavior: WifiConnectivityManager calls ClientModeImpl.startConnectToNetwork() + * with the expected candidate network ID and BSSID for only the expected number of times within + * the given interval. + */ + @Test + public void connectionAttemptNotRateLimitedWhenScreenOffForceConnectivityScan() { + int maxAttemptRate = WifiConnectivityManager.MAX_CONNECTION_ATTEMPTS_RATE; + int timeInterval = WifiConnectivityManager.MAX_CONNECTION_ATTEMPTS_TIME_INTERVAL_MS; + int numAttempts = 0; + int connectionAttemptIntervals = timeInterval / maxAttemptRate; + + mWifiConnectivityManager.handleScreenStateChanged(false); + + // First attempt the max rate number of connections within the rate interval. + long currentTimeStamp = 0; + for (int attempt = 0; attempt < maxAttemptRate; attempt++) { + currentTimeStamp += connectionAttemptIntervals; + when(mClock.getElapsedSinceBootMillis()).thenReturn(currentTimeStamp); + // Set WiFi to disconnected state to trigger PNO scan + mWifiConnectivityManager.handleConnectionStateChanged( + WifiConnectivityManager.WIFI_STATE_DISCONNECTED); + numAttempts++; + } + + mWifiConnectivityManager.forceConnectivityScan(new WorkSource()); + + for (int attempt = 0; attempt < maxAttemptRate; attempt++) { + currentTimeStamp += connectionAttemptIntervals; + when(mClock.getElapsedSinceBootMillis()).thenReturn(currentTimeStamp); + // Set WiFi to disconnected state to trigger PNO scan + mWifiConnectivityManager.handleConnectionStateChanged( + WifiConnectivityManager.WIFI_STATE_DISCONNECTED); + numAttempts++; + } + + // Verify that all the connection attempts went through + verify(mClientModeImpl, times(numAttempts)).startConnectToNetwork( + CANDIDATE_NETWORK_ID, Process.WIFI_UID, CANDIDATE_BSSID); + } + /** * Multiple back to back connection attempts after a user selection should not be rate limited. * -- cgit v1.2.3