summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java228
1 files changed, 215 insertions, 13 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java b/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java
index 1a7cce780..da4c32994 100644
--- a/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java
@@ -19,10 +19,15 @@ package com.android.server.wifi;
import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE;
+import static com.android.server.wifi.WifiNetworkFactory.PERIODIC_SCAN_INTERVAL_MS;
+import static com.android.server.wifi.util.NativeUtil.addEnclosingQuotes;
+
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import android.app.ActivityManager;
+import android.app.AlarmManager;
+import android.app.AlarmManager.OnAlarmListener;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.MacAddress;
@@ -30,7 +35,11 @@ import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiNetworkSpecifier;
+import android.net.wifi.WifiScanner;
+import android.net.wifi.WifiScanner.ScanListener;
+import android.net.wifi.WifiScanner.ScanSettings;
import android.os.PatternMatcher;
+import android.os.WorkSource;
import android.os.test.TestLooper;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Pair;
@@ -38,6 +47,8 @@ import android.util.Pair;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@@ -55,10 +66,19 @@ public class WifiNetworkFactoryTest {
@Mock WifiConnectivityManager mWifiConnectivityManager;
@Mock Context mContext;
@Mock ActivityManager mActivityManager;
+ @Mock AlarmManager mAlarmManager;
+ @Mock Clock mClock;
+ @Mock WifiInjector mWifiInjector;
+ @Mock WifiScanner mWifiScanner;
@Mock PackageManager mPackageManager;
NetworkCapabilities mNetworkCapabilities;
TestLooper mLooper;
NetworkRequest mNetworkRequest;
+ WifiScanner.ScanData[] mTestScanDatas;
+ ArgumentCaptor<ScanSettings> mScanSettingsArgumentCaptor =
+ ArgumentCaptor.forClass(ScanSettings.class);
+ ArgumentCaptor<WorkSource> mWorkSourceArgumentCaptor =
+ ArgumentCaptor.forClass(WorkSource.class);
private WifiNetworkFactory mWifiNetworkFactory;
@@ -72,6 +92,7 @@ public class WifiNetworkFactoryTest {
mLooper = new TestLooper();
mNetworkCapabilities = new NetworkCapabilities();
mNetworkCapabilities.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
+ mTestScanDatas = ScanTestUtil.createScanDatas(new int[][]{ { 2417, 2427, 5180, 5170 } });
when(mContext.getPackageManager()).thenReturn(mPackageManager);
when(mPackageManager.getNameForUid(TEST_UID_1)).thenReturn(TEST_PACKAGE_NAME_1);
@@ -80,9 +101,11 @@ public class WifiNetworkFactoryTest {
.thenReturn(IMPORTANCE_FOREGROUND_SERVICE);
when(mActivityManager.getPackageImportance(TEST_PACKAGE_NAME_2))
.thenReturn(IMPORTANCE_FOREGROUND_SERVICE);
+ when(mWifiInjector.getWifiScanner()).thenReturn(mWifiScanner);
mWifiNetworkFactory = new WifiNetworkFactory(mLooper.getLooper(), mContext,
- mNetworkCapabilities, mActivityManager, mWifiConnectivityManager);
+ mNetworkCapabilities, mActivityManager, mAlarmManager, mClock, mWifiInjector,
+ mWifiConnectivityManager);
mNetworkRequest = new NetworkRequest.Builder()
.setCapabilities(mNetworkCapabilities)
@@ -149,7 +172,7 @@ public class WifiNetworkFactoryTest {
when(mActivityManager.getPackageImportance(TEST_PACKAGE_NAME_1))
.thenReturn(IMPORTANCE_FOREGROUND_SERVICE + 1);
- WifiNetworkSpecifier specifier = createWifiNetworkSpecifier(TEST_UID_1);
+ WifiNetworkSpecifier specifier = createWifiNetworkSpecifier(TEST_UID_1, false);
mNetworkRequest.networkCapabilities.setNetworkSpecifier(specifier);
assertFalse(mWifiNetworkFactory.acceptRequest(mNetworkRequest, 0));
@@ -164,7 +187,7 @@ public class WifiNetworkFactoryTest {
when(mActivityManager.getPackageImportance(TEST_PACKAGE_NAME_1))
.thenReturn(IMPORTANCE_FOREGROUND);
- WifiNetworkSpecifier specifier = createWifiNetworkSpecifier(TEST_UID_1);
+ WifiNetworkSpecifier specifier = createWifiNetworkSpecifier(TEST_UID_1, false);
mNetworkRequest.networkCapabilities.setNetworkSpecifier(specifier);
assertTrue(mWifiNetworkFactory.acceptRequest(mNetworkRequest, 0));
@@ -182,13 +205,13 @@ public class WifiNetworkFactoryTest {
.thenReturn(IMPORTANCE_FOREGROUND);
// Handle request 1.
- WifiNetworkSpecifier specifier1 = createWifiNetworkSpecifier(TEST_UID_1);
+ WifiNetworkSpecifier specifier1 = createWifiNetworkSpecifier(TEST_UID_1, false);
mNetworkRequest.networkCapabilities.setNetworkSpecifier(specifier1);
mWifiNetworkFactory.needNetworkFor(mNetworkRequest, 0);
// Make request 2 which will be accepted because a fg app request can
// override a fg service request.
- WifiNetworkSpecifier specifier2 = createWifiNetworkSpecifier(TEST_UID_2);
+ WifiNetworkSpecifier specifier2 = createWifiNetworkSpecifier(TEST_UID_2, false);
mNetworkRequest.networkCapabilities.setNetworkSpecifier(specifier2);
assertTrue(mWifiNetworkFactory.acceptRequest(mNetworkRequest, 0));
}
@@ -205,13 +228,13 @@ public class WifiNetworkFactoryTest {
.thenReturn(IMPORTANCE_FOREGROUND_SERVICE);
// Handle request 1.
- WifiNetworkSpecifier specifier1 = createWifiNetworkSpecifier(TEST_UID_1);
+ WifiNetworkSpecifier specifier1 = createWifiNetworkSpecifier(TEST_UID_1, false);
mNetworkRequest.networkCapabilities.setNetworkSpecifier(specifier1);
mWifiNetworkFactory.needNetworkFor(mNetworkRequest, 0);
// Make request 2 which will be accepted because a fg service request can
// override an existing fg service request.
- WifiNetworkSpecifier specifier2 = createWifiNetworkSpecifier(TEST_UID_2);
+ WifiNetworkSpecifier specifier2 = createWifiNetworkSpecifier(TEST_UID_2, false);
mNetworkRequest.networkCapabilities.setNetworkSpecifier(specifier2);
assertTrue(mWifiNetworkFactory.acceptRequest(mNetworkRequest, 0));
}
@@ -228,13 +251,13 @@ public class WifiNetworkFactoryTest {
.thenReturn(IMPORTANCE_FOREGROUND);
// Handle request 1.
- WifiNetworkSpecifier specifier1 = createWifiNetworkSpecifier(TEST_UID_1);
+ WifiNetworkSpecifier specifier1 = createWifiNetworkSpecifier(TEST_UID_1, false);
mNetworkRequest.networkCapabilities.setNetworkSpecifier(specifier1);
mWifiNetworkFactory.needNetworkFor(mNetworkRequest, 0);
// Make request 2 which will be accepted because a fg app request can
// override an existing fg app request.
- WifiNetworkSpecifier specifier2 = createWifiNetworkSpecifier(TEST_UID_2);
+ WifiNetworkSpecifier specifier2 = createWifiNetworkSpecifier(TEST_UID_2, false);
mNetworkRequest.networkCapabilities.setNetworkSpecifier(specifier2);
assertTrue(mWifiNetworkFactory.acceptRequest(mNetworkRequest, 0));
}
@@ -251,23 +274,202 @@ public class WifiNetworkFactoryTest {
.thenReturn(IMPORTANCE_FOREGROUND_SERVICE);
// Handle request 1.
- WifiNetworkSpecifier specifier1 = createWifiNetworkSpecifier(TEST_UID_1);
+ WifiNetworkSpecifier specifier1 = createWifiNetworkSpecifier(TEST_UID_1, false);
mNetworkRequest.networkCapabilities.setNetworkSpecifier(specifier1);
mWifiNetworkFactory.needNetworkFor(mNetworkRequest, 0);
// Make request 2 which will be rejected because a fg service request cannot
// override a fg app request.
- WifiNetworkSpecifier specifier2 = createWifiNetworkSpecifier(TEST_UID_2);
+ WifiNetworkSpecifier specifier2 = createWifiNetworkSpecifier(TEST_UID_2, false);
mNetworkRequest.networkCapabilities.setNetworkSpecifier(specifier2);
assertFalse(mWifiNetworkFactory.acceptRequest(mNetworkRequest, 0));
}
- private WifiNetworkSpecifier createWifiNetworkSpecifier(int uid) {
+ /**
+ * Verify handling of new network request with network specifier.
+ */
+ @Test
+ public void testHandleNetworkRequestWithSpecifier() {
+ WifiNetworkSpecifier specifier = createWifiNetworkSpecifier(TEST_UID_1, false);
+ mNetworkRequest.networkCapabilities.setNetworkSpecifier(specifier);
+ mWifiNetworkFactory.needNetworkFor(mNetworkRequest, 0);
+
+ // Disable connectivity manager .
+ verify(mWifiConnectivityManager).enable(false);
+ verify(mWifiScanner).startScan(mScanSettingsArgumentCaptor.capture(), any(),
+ mWorkSourceArgumentCaptor.capture());
+
+ // Verify scan settings.
+ ScanSettings scanSettings = mScanSettingsArgumentCaptor.getValue();
+ assertNotNull(scanSettings);
+ assertEquals(WifiScanner.WIFI_BAND_BOTH_WITH_DFS, scanSettings.band);
+ assertEquals(WifiScanner.TYPE_HIGH_ACCURACY, scanSettings.type);
+ assertEquals(WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN, scanSettings.reportEvents);
+ assertNull(scanSettings.hiddenNetworks);
+ WorkSource workSource = mWorkSourceArgumentCaptor.getValue();
+ assertNotNull(workSource);
+ assertEquals(TEST_UID_1, workSource.get(0));
+ }
+
+ /**
+ * Verify handling of new network request with network specifier for a hidden network.
+ */
+ @Test
+ public void testHandleNetworkRequestWithSpecifierForHiddenNetwork() {
+ WifiNetworkSpecifier specifier = createWifiNetworkSpecifier(TEST_UID_1, true);
+ mNetworkRequest.networkCapabilities.setNetworkSpecifier(specifier);
+ mWifiNetworkFactory.needNetworkFor(mNetworkRequest, 0);
+
+ // Disable connectivity manager .
+ verify(mWifiConnectivityManager).enable(false);
+ verify(mWifiScanner).startScan(mScanSettingsArgumentCaptor.capture(), any(),
+ mWorkSourceArgumentCaptor.capture());
+
+ // Verify scan settings.
+ ScanSettings scanSettings = mScanSettingsArgumentCaptor.getValue();
+ assertNotNull(scanSettings);
+ assertEquals(WifiScanner.WIFI_BAND_BOTH_WITH_DFS, scanSettings.band);
+ assertEquals(WifiScanner.TYPE_HIGH_ACCURACY, scanSettings.type);
+ assertEquals(WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN, scanSettings.reportEvents);
+ assertNotNull(scanSettings.hiddenNetworks);
+ assertNotNull(scanSettings.hiddenNetworks[0]);
+ assertEquals(scanSettings.hiddenNetworks[0].ssid,
+ addEnclosingQuotes(specifier.ssidPatternMatcher.getPath()));
+ WorkSource workSource = mWorkSourceArgumentCaptor.getValue();
+ assertNotNull(workSource);
+ assertEquals(TEST_UID_1, workSource.get(0));
+ }
+
+ /**
+ * Verify handling of new network request with network specifier for a non-hidden network
+ * after processing a previous hidden network requst.
+ * Validates that the scan settings was properly reset between the 2 request
+ * {@link ScanSettings#hiddenNetworks}
+ */
+ @Test
+ public void testHandleNetworkRequestWithSpecifierAfterPreviousHiddenNetworkRequest() {
+ testHandleNetworkRequestWithSpecifierForHiddenNetwork();
+ mWifiNetworkFactory.releaseNetworkFor(mNetworkRequest);
+ reset(mWifiScanner, mWifiConnectivityManager);
+ testHandleNetworkRequestWithSpecifier();
+ }
+
+ /**
+ * Verify handling of release of the active network request with network specifier.
+ */
+ @Test
+ public void testHandleNetworkReleaseWithSpecifier() {
+ // Make a generic request first to ensure that we re-enable auto-join after release.
+ mWifiNetworkFactory.needNetworkFor(mNetworkRequest, 0);
+
+ WifiNetworkSpecifier specifier = createWifiNetworkSpecifier(TEST_UID_1, false);
+ mNetworkRequest.networkCapabilities.setNetworkSpecifier(specifier);
+
+ // Make the network request with specifier.
+ mWifiNetworkFactory.needNetworkFor(mNetworkRequest, 0);
+ // Disable connectivity manager .
+ verify(mWifiConnectivityManager).enable(false);
+ verify(mWifiScanner).startScan(any(), any(), any());
+
+ // Release the network request.
+ mWifiNetworkFactory.releaseNetworkFor(mNetworkRequest);
+ // Re-enable connectivity manager .
+ verify(mWifiConnectivityManager).enable(true);
+ }
+
+ /**
+ * Verify the periodic scan to find a network matching the network specifier.
+ * Simulates the case where the network is not found in any of the scan results.
+ */
+ @Test
+ public void testPeriodicScanNetworkRequestWithSpecifier() {
+ WifiNetworkSpecifier specifier = createWifiNetworkSpecifier(TEST_UID_1, false);
+ mNetworkRequest.networkCapabilities.setNetworkSpecifier(specifier);
+ mWifiNetworkFactory.needNetworkFor(mNetworkRequest, 0);
+
+ verify(mWifiConnectivityManager).enable(false);
+ verifyPeriodicScans(0,
+ PERIODIC_SCAN_INTERVAL_MS, // 10s
+ PERIODIC_SCAN_INTERVAL_MS, // 10s
+ PERIODIC_SCAN_INTERVAL_MS, // 10s
+ PERIODIC_SCAN_INTERVAL_MS); // 10s
+ }
+
+ /**
+ * Verify the periodic scan back off to find a network matching the network specifier
+ * is cancelled when the active network request is released.
+ */
+ @Test
+ public void testPeriodicScanCancelOnReleaseNetworkRequestWithSpecifier() {
+ WifiNetworkSpecifier specifier = createWifiNetworkSpecifier(TEST_UID_1, false);
+ mNetworkRequest.networkCapabilities.setNetworkSpecifier(specifier);
+ mWifiNetworkFactory.needNetworkFor(mNetworkRequest, 0);
+
+ verify(mWifiConnectivityManager).enable(false);
+ verifyPeriodicScans(0,
+ PERIODIC_SCAN_INTERVAL_MS, // 10s
+ PERIODIC_SCAN_INTERVAL_MS); // 10s
+
+ mWifiNetworkFactory.releaseNetworkFor(mNetworkRequest);
+ // Cancel the alarm set for the next scan.
+ verify(mAlarmManager).cancel(any(OnAlarmListener.class));
+ }
+
+ // Simulates the periodic scans performed to find a matching network.
+ // a) Start scan
+ // b) Scan results received.
+ // c) Set alarm for next scan at the expected interval.
+ // d) Alarm fires, go to step a) again and repeat.
+ private void verifyPeriodicScans(long...expectedIntervalsInSeconds) {
+ when(mClock.getElapsedSinceBootMillis()).thenReturn(0L);
+
+ ArgumentCaptor<OnAlarmListener> alarmListenerArgumentCaptor =
+ ArgumentCaptor.forClass(OnAlarmListener.class);
+ OnAlarmListener alarmListener = null;
+ ArgumentCaptor<ScanListener> scanListenerArgumentCaptor =
+ ArgumentCaptor.forClass(ScanListener.class);
+ ScanListener scanListener = null;
+
+ InOrder inOrder = inOrder(mWifiScanner, mAlarmManager);
+
+ for (int i = 0; i < expectedIntervalsInSeconds.length - 1; i++) {
+ long expectedCurrentIntervalInMs = expectedIntervalsInSeconds[i];
+ long expectedNextIntervalInMs = expectedIntervalsInSeconds[i + 1];
+
+ // First scan is immediately fired, so need for the alarm to fire.
+ if (expectedCurrentIntervalInMs != 0) {
+ // Fire the alarm and ensure that we started the next scan.
+ alarmListener.onAlarm();
+ }
+ inOrder.verify(mWifiScanner).startScan(
+ any(), scanListenerArgumentCaptor.capture(), any());
+ scanListener = scanListenerArgumentCaptor.getValue();
+ assertNotNull(scanListener);
+
+ // Now trigger the scan results callback and verify the alarm set for the next scan.
+ scanListener.onResults(mTestScanDatas);
+
+ inOrder.verify(mAlarmManager).set(eq(AlarmManager.ELAPSED_REALTIME_WAKEUP),
+ eq(expectedNextIntervalInMs), any(),
+ alarmListenerArgumentCaptor.capture(), any());
+ alarmListener = alarmListenerArgumentCaptor.getValue();
+ assertNotNull(alarmListener);
+ }
+
+ verifyNoMoreInteractions(mWifiScanner, mAlarmManager);
+ }
+
+ private WifiNetworkSpecifier createWifiNetworkSpecifier(int uid, boolean isHidden) {
PatternMatcher ssidPatternMatch =
new PatternMatcher(TEST_SSID, PatternMatcher.PATTERN_LITERAL);
Pair<MacAddress, MacAddress> bssidPatternMatch =
Pair.create(MacAddress.ALL_ZEROS_ADDRESS, MacAddress.ALL_ZEROS_ADDRESS);
- WifiConfiguration wifiConfiguration = WifiConfigurationTestUtil.createPskNetwork();
+ WifiConfiguration wifiConfiguration;
+ if (isHidden) {
+ wifiConfiguration = WifiConfigurationTestUtil.createPskHiddenNetwork();
+ } else {
+ wifiConfiguration = WifiConfigurationTestUtil.createPskNetwork();
+ }
return new WifiNetworkSpecifier(
ssidPatternMatch, bssidPatternMatch, wifiConfiguration, uid);
}