diff options
author | Roshan Pius <rpius@google.com> | 2018-09-20 10:24:40 -0700 |
---|---|---|
committer | Roshan Pius <rpius@google.com> | 2018-10-08 16:05:08 -0700 |
commit | afa78c953b73d7024e3d8ef49a021f8dac4999f6 (patch) | |
tree | c17e9a93e9a1dfc93b066edf1be2682368e40ecd /tests | |
parent | 8a8f9588cbecb8236ac724dd604a9a0a0d546ac7 (diff) |
WifiNetworkFactory: Refactor to a new class
Move WifiNetworkFactory & UntrustedWifiNetworkFactory to separate
classes out of ClientModeImpl. Also, simplify the existing auto-join
enable mechanism (WifiConnectivityManager.enable()).
Also added a new |mRunning| flag in WifiConnectivityManager to avoid
multiple calls to start/stop being processed. There was no functional
problem without |mRunning| flag, but it makes the unit tests cleaner.
Bug: 116210817
Test: Unit tests
Test: Device boots up & auto-connects to wifi networks
Change-Id: I24038fe554ab7343e8884ded9ff60d26f2549ff7
Diffstat (limited to 'tests')
3 files changed, 224 insertions, 2 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/UntrustedWifiNetworkFactoryTest.java b/tests/wifitests/src/com/android/server/wifi/UntrustedWifiNetworkFactoryTest.java new file mode 100644 index 000000000..aa2577bd8 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/UntrustedWifiNetworkFactoryTest.java @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.wifi; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import android.content.Context; +import android.net.NetworkCapabilities; +import android.net.NetworkRequest; +import android.os.test.TestLooper; +import android.test.suitebuilder.annotation.SmallTest; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +/** + * Unit tests for {@link com.android.server.wifi.UntrustedWifiNetworkFactory}. + */ +@SmallTest +public class UntrustedWifiNetworkFactoryTest { + @Mock WifiConnectivityManager mWifiConnectivityManager; + @Mock Context mContext; + NetworkCapabilities mNetworkCapabilities; + TestLooper mLooper; + NetworkRequest mNetworkRequest; + + private UntrustedWifiNetworkFactory mUntrustedWifiNetworkFactory; + + /** + * Setup the mocks. + */ + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + mLooper = new TestLooper(); + mNetworkCapabilities = new NetworkCapabilities(); + mNetworkCapabilities.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); + mNetworkCapabilities.removeCapability(NetworkCapabilities.NET_CAPABILITY_TRUSTED); + + mUntrustedWifiNetworkFactory = new UntrustedWifiNetworkFactory( + mLooper.getLooper(), mContext, + mNetworkCapabilities, mWifiConnectivityManager); + + mNetworkRequest = new NetworkRequest.Builder() + .setCapabilities(mNetworkCapabilities) + .build(); + } + + /** + * Called after each test + */ + @After + public void cleanup() { + validateMockitoUsage(); + } + + /** + * Validates handling of needNetworkFor. + */ + @Test + public void testHandleNetworkRequest() { + assertFalse(mUntrustedWifiNetworkFactory.hasConnectionRequests()); + mUntrustedWifiNetworkFactory.needNetworkFor(mNetworkRequest, 0); + + // First network request should turn on auto-join. + verify(mWifiConnectivityManager).setUntrustedConnectionAllowed(true); + assertTrue(mUntrustedWifiNetworkFactory.hasConnectionRequests()); + + // Subsequent ones should do nothing. + mUntrustedWifiNetworkFactory.needNetworkFor(mNetworkRequest, 0); + verifyNoMoreInteractions(mWifiConnectivityManager); + } + + /** + * Validates handling of releaseNetwork. + */ + @Test + public void testHandleNetworkRelease() { + // Release network without a corresponding request should be ignored. + mUntrustedWifiNetworkFactory.releaseNetworkFor(mNetworkRequest); + assertFalse(mUntrustedWifiNetworkFactory.hasConnectionRequests()); + + // Now request & then release the network request + mUntrustedWifiNetworkFactory.needNetworkFor(mNetworkRequest, 0); + assertTrue(mUntrustedWifiNetworkFactory.hasConnectionRequests()); + verify(mWifiConnectivityManager).setUntrustedConnectionAllowed(true); + + mUntrustedWifiNetworkFactory.releaseNetworkFor(mNetworkRequest); + assertFalse(mUntrustedWifiNetworkFactory.hasConnectionRequests()); + verify(mWifiConnectivityManager).setUntrustedConnectionAllowed(false); + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java index 7d0922959..5432de2c7 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java @@ -95,6 +95,7 @@ public class WifiConnectivityManagerTest { when(mWifiInjector.getWifiScanner()).thenReturn(mWifiScanner); mWifiConnectivityManager = createConnectivityManager(); verify(mWifiConfigManager).setOnSavedNetworkUpdateListener(anyObject()); + mWifiConnectivityManager.setTrustedConnectionAllowed(true); mWifiConnectivityManager.setWifiEnabled(true); when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime()); mFullScanMaxTxPacketRate = mResource.getInteger( @@ -317,7 +318,7 @@ public class WifiConnectivityManagerTest { mClientModeImpl, mWifiInjector, mWifiConfigManager, mWifiInfo, mWifiNS, mWifiConnectivityHelper, mWifiLastResortWatchdog, mOpenNetworkNotifier, mCarrierNetworkNotifier, - mCarrierNetworkConfig, mWifiMetrics, mLooper.getLooper(), mClock, mLocalLog, true, + mCarrierNetworkConfig, mWifiMetrics, mLooper.getLooper(), mClock, mLocalLog, null, null, null); } @@ -416,6 +417,7 @@ public class WifiConnectivityManagerTest { R.bool.config_wifi_framework_enable_associated_network_selection)) .thenReturn(false); mWifiConnectivityManager = createConnectivityManager(); + mWifiConnectivityManager.setTrustedConnectionAllowed(true); // Set WiFi to connected state mWifiConnectivityManager.handleConnectionStateChanged( @@ -1616,7 +1618,6 @@ public class WifiConnectivityManagerTest { */ @Test public void verifyGetFirmwareRoamingInfoIsCalledWhenEnableWiFiAndWcmOn() { - reset(mWifiConnectivityHelper); // WifiConnectivityManager is on by default mWifiConnectivityManager.setWifiEnabled(true); verify(mWifiConnectivityHelper).getFirmwareRoamingInfo(); @@ -1975,6 +1976,7 @@ public class WifiConnectivityManagerTest { }}).when(mWifiNS).selectNetwork( any(), any(), any(), anyBoolean(), anyBoolean(), anyBoolean()); + mWifiConnectivityManager.setTrustedConnectionAllowed(true); // Set WiFi to disconnected state with screen on which triggers a scan immediately. mWifiConnectivityManager.setWifiEnabled(true); mWifiConnectivityManager.handleScreenStateChanged(true); @@ -2030,6 +2032,7 @@ public class WifiConnectivityManagerTest { }}).when(mWifiNS).selectNetwork( any(), any(), any(), anyBoolean(), anyBoolean(), anyBoolean()); + mWifiConnectivityManager.setTrustedConnectionAllowed(true); // Set WiFi to disconnected state with screen on which triggers a scan immediately. mWifiConnectivityManager.setWifiEnabled(true); mWifiConnectivityManager.handleScreenStateChanged(true); diff --git a/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java b/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java new file mode 100644 index 000000000..3b6d5cb69 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.wifi; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import android.content.Context; +import android.net.NetworkCapabilities; +import android.net.NetworkRequest; +import android.os.test.TestLooper; +import android.test.suitebuilder.annotation.SmallTest; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +/** + * Unit tests for {@link com.android.server.wifi.WifiNetworkFactory}. + */ +@SmallTest +public class WifiNetworkFactoryTest { + @Mock WifiConnectivityManager mWifiConnectivityManager; + @Mock Context mContext; + NetworkCapabilities mNetworkCapabilities; + TestLooper mLooper; + NetworkRequest mNetworkRequest; + + private WifiNetworkFactory mWifiNetworkFactory; + + /** + * Setup the mocks. + */ + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + mLooper = new TestLooper(); + mNetworkCapabilities = new NetworkCapabilities(); + mNetworkCapabilities.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); + + mWifiNetworkFactory = new WifiNetworkFactory(mLooper.getLooper(), mContext, + mNetworkCapabilities, mWifiConnectivityManager); + + mNetworkRequest = new NetworkRequest.Builder() + .setCapabilities(mNetworkCapabilities) + .build(); + } + + /** + * Called after each test + */ + @After + public void cleanup() { + validateMockitoUsage(); + } + + /** + * Validates handling of needNetworkFor. + */ + @Test + public void testHandleNetworkRequest() { + assertFalse(mWifiNetworkFactory.hasConnectionRequests()); + mWifiNetworkFactory.needNetworkFor(mNetworkRequest, 0); + + // First network request should turn on auto-join. + verify(mWifiConnectivityManager).setTrustedConnectionAllowed(true); + assertTrue(mWifiNetworkFactory.hasConnectionRequests()); + + // Subsequent ones should do nothing. + mWifiNetworkFactory.needNetworkFor(mNetworkRequest, 0); + verifyNoMoreInteractions(mWifiConnectivityManager); + } + + /** + * Validates handling of releaseNetwork. + */ + @Test + public void testHandleNetworkRelease() { + // Release network with out a corresponding request should be ignored. + mWifiNetworkFactory.releaseNetworkFor(mNetworkRequest); + assertFalse(mWifiNetworkFactory.hasConnectionRequests()); + + mWifiNetworkFactory.needNetworkFor(mNetworkRequest, 0); + assertTrue(mWifiNetworkFactory.hasConnectionRequests()); + verify(mWifiConnectivityManager).setTrustedConnectionAllowed(true); + + mWifiNetworkFactory.releaseNetworkFor(mNetworkRequest); + assertFalse(mWifiNetworkFactory.hasConnectionRequests()); + verify(mWifiConnectivityManager).setTrustedConnectionAllowed(false); + } +} |