diff options
author | Roshan Pius <rpius@google.com> | 2018-10-15 23:30:15 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2018-10-15 23:30:15 +0000 |
commit | b6b5b0d3859fec5ef0af9018a3334553f0a19ccf (patch) | |
tree | d79fdb455f52e937201a1859fcddd416eb792046 /tests | |
parent | 5e35dc1f0ab42b7ee081d8ae94c5fc41e0a3cadd (diff) | |
parent | 29f2a62b0e6d38d5cc8a6b5ed5b096d6f3654340 (diff) |
Merge changes I5b1baad8,I24038fe5
* changes:
ClientModeImpl: Construct network factories in constructor
WifiNetworkFactory: Refactor to a new class
Diffstat (limited to 'tests')
4 files changed, 244 insertions, 39 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java b/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java index 3664a7b42..d413e5bb4 100644 --- a/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java @@ -42,10 +42,8 @@ import android.net.LinkProperties; import android.net.MacAddress; import android.net.NetworkAgent; import android.net.NetworkCapabilities; -import android.net.NetworkFactory; import android.net.NetworkInfo; import android.net.NetworkMisc; -import android.net.NetworkRequest; import android.net.dhcp.DhcpClient; import android.net.ip.IpClient; import android.net.wifi.ScanResult; @@ -136,8 +134,6 @@ public class ClientModeImplTest { private static final int FRAMEWORK_NETWORK_ID = 0; private static final int TEST_RSSI = -54; private static final int TEST_NETWORK_ID = 54; - private static final int TEST_VALID_NETWORK_SCORE = 54; - private static final int TEST_OUTSCORED_NETWORK_SCORE = 100; private static final int WPS_SUPPLICANT_NETWORK_ID = 5; private static final int WPS_FRAMEWORK_NETWORK_ID = 10; private static final String DEFAULT_TEST_SSID = "\"GoogleGuest\""; @@ -325,7 +321,6 @@ public class ClientModeImplTest { HandlerThread mP2pThread; HandlerThread mSyncThread; AsyncChannel mCmiAsyncChannel; - AsyncChannel mNetworkFactoryChannel; TestAlarmManager mAlarmManager; MockWifiMonitor mWifiMonitor; TestLooper mLooper; @@ -334,7 +329,6 @@ public class ClientModeImplTest { FrameworkFacade mFrameworkFacade; IpClient.Callback mIpClientCallback; PhoneStateListener mPhoneStateListener; - NetworkRequest mDefaultNetworkRequest; OsuProvider mOsuProvider; ContentObserver mContentObserver; @@ -369,6 +363,8 @@ public class ClientModeImplTest { @Mock WifiPermissionsWrapper mWifiPermissionsWrapper; @Mock WakeupController mWakeupController; @Mock WifiDataStall mWifiDataStall; + @Mock WifiNetworkFactory mWifiNetworkFactory; + @Mock UntrustedWifiNetworkFactory mUntrustedWifiNetworkFactory; final ArgumentCaptor<WifiNative.InterfaceCallback> mInterfaceCallbackCaptor = ArgumentCaptor.forClass(WifiNative.InterfaceCallback.class); @@ -415,8 +411,14 @@ public class ClientModeImplTest { when(mWifiInjector.getWakeupController()).thenReturn(mWakeupController); when(mWifiInjector.getScoringParams()).thenReturn(new ScoringParams()); when(mWifiInjector.getWifiDataStall()).thenReturn(mWifiDataStall); + when(mWifiInjector.makeWifiNetworkFactory(any(), any())).thenReturn(mWifiNetworkFactory); + when(mWifiInjector.makeUntrustedWifiNetworkFactory(any(), any())) + .thenReturn(mUntrustedWifiNetworkFactory); when(mWifiNative.initialize()).thenReturn(true); + when(mWifiNetworkFactory.hasConnectionRequests()).thenReturn(true); + when(mUntrustedWifiNetworkFactory.hasConnectionRequests()).thenReturn(true); + mFrameworkFacade = getFrameworkFacade(); mContext = getContext(); @@ -505,36 +507,14 @@ public class ClientModeImplTest { mCmi.sendMessage(ClientModeImpl.CMD_BOOT_COMPLETED); mLooper.dispatchAll(); - /* Simulate the initial NetworkRequest sent in by ConnectivityService. */ - ArgumentCaptor<Messenger> captor = ArgumentCaptor.forClass(Messenger.class); - verify(mConnectivityManager, atLeast(2)).registerNetworkFactory( - captor.capture(), anyString()); - Messenger networkFactoryMessenger = captor.getAllValues().get(0); - registerAsyncChannel((x) -> { - mNetworkFactoryChannel = x; - }, networkFactoryMessenger); - - mDefaultNetworkRequest = new NetworkRequest.Builder() - .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) - .build(); - sendDefaultNetworkRequest(TEST_VALID_NETWORK_SCORE); + verify(mWifiNetworkFactory).register(); + verify(mUntrustedWifiNetworkFactory).register(); mLooper.startAutoDispatch(); mCmi.syncInitialize(mCmiAsyncChannel); mLooper.stopAutoDispatch(); } - /** - * Helper function to resend the cached network request (id == 0) with the specified score. - * Note: If you need to add a separate network request, don't use the builder to create one - * since the new request object will again default to id == 0. - */ - private void sendDefaultNetworkRequest(int score) { - mNetworkFactoryChannel.sendMessage( - NetworkFactory.CMD_REQUEST_NETWORK, score, 0, mDefaultNetworkRequest); - mLooper.dispatchAll(); - } - @After public void cleanUp() throws Exception { Binder.restoreCallingIdentity(mBinderToken); @@ -548,7 +528,6 @@ public class ClientModeImplTest { mSyncThread = null; mCmiAsyncChannel = null; mCmi = null; - mNetworkFactoryChannel = null; } @Test @@ -934,8 +913,10 @@ public class ClientModeImplTest { @Test public void triggerConnectWithNoNetworkRequest() throws Exception { loadComponentsInStaMode(); - // Change the network score to remove the network request. - sendDefaultNetworkRequest(TEST_OUTSCORED_NETWORK_SCORE); + // Remove the network requests. + when(mWifiNetworkFactory.hasConnectionRequests()).thenReturn(false); + when(mUntrustedWifiNetworkFactory.hasConnectionRequests()).thenReturn(false); + WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); config.networkId = FRAMEWORK_NETWORK_ID; setupAndStartConnectSequence(config); @@ -1010,8 +991,9 @@ public class ClientModeImplTest { // Simulate the first connection. connect(); - // Change the network score to remove the network request. - sendDefaultNetworkRequest(TEST_OUTSCORED_NETWORK_SCORE); + // Remove the network requests. + when(mWifiNetworkFactory.hasConnectionRequests()).thenReturn(false); + when(mUntrustedWifiNetworkFactory.hasConnectionRequests()).thenReturn(false); WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); config.networkId = FRAMEWORK_NETWORK_ID + 1; @@ -1034,8 +1016,9 @@ public class ClientModeImplTest { // Simulate the first connection. connect(); - // Change the network score to remove the network request. - sendDefaultNetworkRequest(TEST_OUTSCORED_NETWORK_SCORE); + // Remove the network requests. + when(mWifiNetworkFactory.hasConnectionRequests()).thenReturn(false); + when(mUntrustedWifiNetworkFactory.hasConnectionRequests()).thenReturn(false); when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(false); 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..5ef629254 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/UntrustedWifiNetworkFactoryTest.java @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2018 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..46519e519 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2018 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); + } +} |