From 194a506c9a5de993caee8888442db7bdb45de3e1 Mon Sep 17 00:00:00 2001 From: "Nate(Qiang) Jiang" Date: Tue, 22 Oct 2019 13:08:35 -0700 Subject: [NetworkSuggestion] add network callback Add a network callback for WifiNetworkSuggestion. Will send event for both connection success and failure. Bug: 142062781 Test: atest android.net.wifi Test: atest com.android.server.wifi Change-Id: I46d403b784d469538118c9b03556b8236de35fe7 --- .../wifi/WifiNetworkSuggestionsManagerTest.java | 80 ++++++++++++++++++++-- .../android/server/wifi/WifiServiceImplTest.java | 52 ++++++++++++++ 2 files changed, 127 insertions(+), 5 deletions(-) (limited to 'tests') diff --git a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java index 5755c5858..19dd3c9e9 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java @@ -44,6 +44,7 @@ import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.res.Resources; import android.net.MacAddress; +import android.net.wifi.ISuggestionConnectionStatusListener; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiManager; import android.net.wifi.WifiNetworkSuggestion; @@ -51,6 +52,7 @@ import android.net.wifi.WifiScanner; import android.net.wifi.hotspot2.PasspointConfiguration; import android.net.wifi.hotspot2.pps.HomeSp; import android.os.Handler; +import android.os.IBinder; import android.os.UserHandle; import android.os.test.TestLooper; import android.test.suitebuilder.annotation.SmallTest; @@ -91,6 +93,7 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { private static final String TEST_FQDN = "FQDN"; private static final int TEST_UID_1 = 5667; private static final int TEST_UID_2 = 4537; + private static final int NETWORK_CALLBACK_ID = 1100; private static final int VALID_CARRIER_ID = 1; private @Mock Context mContext; @@ -106,6 +109,8 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { private @Mock NetworkSuggestionStoreData mNetworkSuggestionStoreData; private @Mock WifiMetrics mWifiMetrics; private @Mock PasspointManager mPasspointManager; + private @Mock ISuggestionConnectionStatusListener mListener; + private @Mock IBinder mBinder; private TestLooper mLooper; private ArgumentCaptor mAppOpChangedListenerCaptor = ArgumentCaptor.forClass(AppOpsManager.OnOpChangedListener.class); @@ -845,9 +850,13 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { * {@link WifiNetworkSuggestion#isAppInteractionRequired} flag set. * b) The app holds location permission. * This should trigger a broadcast to the app. + * This should trigger not a connection failure callback to the app. */ @Test - public void testOnNetworkConnectionSuccessWithOneMatch() { + public void testOnNetworkConnectionSuccessWithOneMatch() throws Exception { + assertTrue(mWifiNetworkSuggestionsManager + .registerSuggestionConnectionStatusListener(mBinder, mListener, + NETWORK_CALLBACK_ID, TEST_PACKAGE_1)); WifiNetworkSuggestion networkSuggestion = new WifiNetworkSuggestion( WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, TEST_UID_1, TEST_PACKAGE_1); @@ -880,12 +889,64 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { mInorder.verifyNoMoreInteractions(); } + @Test + public void testOnNetworkConnectionFailureWithOneMatchButCallbackOnBinderDied() + throws Exception { + ArgumentCaptor drCaptor = + ArgumentCaptor.forClass(IBinder.DeathRecipient.class); + assertTrue(mWifiNetworkSuggestionsManager + .registerSuggestionConnectionStatusListener(mBinder, mListener, + NETWORK_CALLBACK_ID, TEST_PACKAGE_1)); + verify(mBinder).linkToDeath(drCaptor.capture(), anyInt()); + WifiNetworkSuggestion networkSuggestion = new WifiNetworkSuggestion( + WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, TEST_UID_1, + TEST_PACKAGE_1); + List networkSuggestionList = + new ArrayList() {{ + add(networkSuggestion); + }}; + assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, + mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, + TEST_PACKAGE_1)); + mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_PACKAGE_1); + // Simulate binder was died. + drCaptor.getValue().binderDied(); + mLooper.dispatchAll(); + verify(mBinder).unlinkToDeath(any(), anyInt()); + // Simulate connecting to the network. + WifiConfiguration connectNetwork = + new WifiConfiguration(networkSuggestion.wifiConfiguration); + connectNetwork.fromWifiNetworkSuggestion = true; + connectNetwork.ephemeral = true; + connectNetwork.creatorName = TEST_APP_NAME_1; + mWifiNetworkSuggestionsManager.handleConnectionAttemptEnded( + WifiMetrics.ConnectionEvent.FAILURE_AUTHENTICATION_FAILURE, + connectNetwork, TEST_BSSID); + + verify(mWifiMetrics).incrementNetworkSuggestionApiNumConnectFailure(); + // Verify no connection failure event was sent out. + mInorder.verify(mWifiPermissionsUtil, never()) + .enforceCanAccessScanResults(TEST_PACKAGE_1, TEST_UID_1); + verify(mListener, never()).onConnectionStatus(any(), anyInt()); + + // Verify no more broadcast were sent out. + mInorder.verify(mContext, never()).sendBroadcastAsUser( + any(), any()); + } + /** * Verify a successful lookup of a single network suggestion matching the current network * connection failure. + * a) The corresponding network suggestion has the + * {@link WifiNetworkSuggestion#isAppInteractionRequired} flag set. + * b) The app holds location permission. + * This should trigger a connection failure callback to the app */ @Test - public void testOnNetworkConnectionFailureWithOneMatch() { + public void testOnNetworkConnectionFailureWithOneMatch() throws Exception { + assertTrue(mWifiNetworkSuggestionsManager + .registerSuggestionConnectionStatusListener(mBinder, mListener, + NETWORK_CALLBACK_ID, TEST_PACKAGE_1)); WifiNetworkSuggestion networkSuggestion = new WifiNetworkSuggestion( WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, TEST_UID_1, TEST_PACKAGE_1); @@ -906,14 +967,17 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { mWifiNetworkSuggestionsManager.handleConnectionAttemptEnded( WifiMetrics.ConnectionEvent.FAILURE_DHCP, connectNetwork, TEST_BSSID); + // Verify right callback were sent out. + mInorder.verify(mWifiPermissionsUtil) + .enforceCanAccessScanResults(TEST_PACKAGE_1, TEST_UID_1); + verify(mListener) + .onConnectionStatus(networkSuggestion, + WifiManager.STATUS_SUGGESTION_CONNECTION_FAILURE_IP_PROVISIONING); verify(mWifiMetrics).incrementNetworkSuggestionApiNumConnectFailure(); // Verify no more broadcast were sent out. - mInorder.verify(mWifiPermissionsUtil, never()).enforceCanAccessScanResults( - anyString(), anyInt()); mInorder.verify(mContext, never()).sendBroadcastAsUser( any(), any()); - } /** @@ -1138,6 +1202,8 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, TEST_PACKAGE_1)); + verify(mWifiPermissionsUtil, times(2)) + .checkNetworkCarrierProvisioningPermission(TEST_UID_1); assertFalse(mWifiNetworkSuggestionsManager.hasUserApprovedForApp(TEST_PACKAGE_1)); WifiConfiguration connectNetwork = @@ -1176,6 +1242,8 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, TEST_PACKAGE_1)); + verify(mWifiPermissionsUtil, times(2)) + .checkNetworkCarrierProvisioningPermission(TEST_UID_1); mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_PACKAGE_1); WifiConfiguration connectNetwork = @@ -1214,6 +1282,8 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, TEST_PACKAGE_1)); + verify(mWifiPermissionsUtil, times(2)) + .checkNetworkCarrierProvisioningPermission(TEST_UID_1); mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_PACKAGE_1); doThrow(new SecurityException()) diff --git a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java index a436d97c2..2d994259a 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java @@ -95,6 +95,7 @@ import android.net.wifi.INetworkRequestMatchCallback; import android.net.wifi.IOnWifiUsabilityStatsListener; import android.net.wifi.IScanResultsListener; import android.net.wifi.ISoftApCallback; +import android.net.wifi.ISuggestionConnectionStatusListener; import android.net.wifi.ITrafficStateCallback; import android.net.wifi.ITxPacketCountListener; import android.net.wifi.ScanResult; @@ -201,6 +202,7 @@ public class WifiServiceImplTest extends WifiBaseTest { 5, 1100002, "\"magenta\"", false, false, null, null)); private static final int TEST_AP_FREQUENCY = 2412; private static final int TEST_AP_BANDWIDTH = SoftApInfo.CHANNEL_WIDTH_20MHZ; + private static final int NETWORK_CALLBACK_ID = 1100; private SoftApInfo mTestSoftApInfo; private AsyncChannel mAsyncChannel; @@ -274,6 +276,7 @@ public class WifiServiceImplTest extends WifiBaseTest { @Mock SarManager mSarManager; @Mock ILocalOnlyHotspotCallback mLohsCallback; @Mock IScanResultsListener mClientScanResultsListener; + @Mock ISuggestionConnectionStatusListener mSuggestionConnectionStatusListener; WifiLog mLog = new LogcatLog(TAG); @@ -4390,6 +4393,55 @@ public class WifiServiceImplTest extends WifiBaseTest { verify(mScanRequestProxy).unregisterScanResultsListener(eq(listenerIdentifier)); } + /** + * Test register callback without permission. + */ + @Test(expected = SecurityException.class) + public void testRegisterSuggestionNetworkCallbackWithMissingPermission() { + doThrow(new SecurityException()).when(mContext).enforceCallingOrSelfPermission( + eq(android.Manifest.permission.ACCESS_WIFI_STATE), eq("WifiService")); + mWifiServiceImpl.registerSuggestionConnectionStatusListener(mAppBinder, + mSuggestionConnectionStatusListener, NETWORK_CALLBACK_ID, TEST_PACKAGE_NAME); + } + + /** + * Test register callback without callback + */ + @Test(expected = IllegalArgumentException.class) + public void testRegisterSuggestionNetworkCallbackWithIllegalArgument() { + mWifiServiceImpl.registerSuggestionConnectionStatusListener(mAppBinder, null, + NETWORK_CALLBACK_ID, TEST_PACKAGE_NAME); + } + + /** + * Test unregister callback without permission. + */ + @Test(expected = SecurityException.class) + public void testUnregisterSuggestionNetworkCallbackWithMissingPermission() { + doThrow(new SecurityException()).when(mContext).enforceCallingOrSelfPermission( + eq(android.Manifest.permission.ACCESS_WIFI_STATE), eq("WifiService")); + mWifiServiceImpl.unregisterSuggestionConnectionStatusListener( + NETWORK_CALLBACK_ID, TEST_PACKAGE_NAME); + } + + /** + * Test register nad unregister callback will go to WifiNetworkSuggestionManager + */ + @Test + public void testRegisterUnregisterSuggestionNetworkCallback() throws Exception { + mWifiServiceImpl.registerSuggestionConnectionStatusListener(mAppBinder, + mSuggestionConnectionStatusListener, NETWORK_CALLBACK_ID, TEST_PACKAGE_NAME); + mLooper.dispatchAll(); + verify(mWifiNetworkSuggestionsManager).registerSuggestionConnectionStatusListener( + eq(mAppBinder), eq(mSuggestionConnectionStatusListener), eq(NETWORK_CALLBACK_ID), + eq(TEST_PACKAGE_NAME)); + mWifiServiceImpl.unregisterSuggestionConnectionStatusListener(NETWORK_CALLBACK_ID, + TEST_PACKAGE_NAME); + mLooper.dispatchAll(); + verify(mWifiNetworkSuggestionsManager).unregisterSuggestionConnectionStatusListener( + eq(NETWORK_CALLBACK_ID), eq(TEST_PACKAGE_NAME)); + } + /** * Test to verify that the lock mode is verified before dispatching the operation -- cgit v1.2.3