diff options
author | Steven Liu <steveliu@google.com> | 2019-08-19 14:32:09 -0700 |
---|---|---|
committer | Steven Liu <steveliu@google.com> | 2019-11-11 09:25:42 -0800 |
commit | f3e89a12abba2ab8ab4ac22d59f676306c8bd03c (patch) | |
tree | 98485428486d0a1d9ebc06a4f9b65f762bc21238 /tests | |
parent | 2b0e30c0866f583418f6b54b7dd932aa6328b6d2 (diff) |
Using the carrier ID for EAP-SIM/AKA/AKA' enterprise wifi configuration.
Bug: 130875004
Test: unit test, manual test
Change-Id: I8dfd3fde6007d00da0eb93a5bd1cd80fdae7db98
Diffstat (limited to 'tests')
6 files changed, 557 insertions, 146 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java b/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java index 7f1c778ba..f26cf939d 100644 --- a/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java @@ -78,6 +78,8 @@ import android.os.UserManager; import android.os.test.TestLooper; import android.provider.Settings; import android.security.KeyStore; +import android.telephony.SubscriptionInfo; +import android.telephony.SubscriptionManager; import android.telephony.TelephonyManager; import android.test.mock.MockContentProvider; import android.test.mock.MockContentResolver; @@ -98,6 +100,7 @@ import com.android.server.wifi.proto.nano.WifiMetricsProto; import com.android.server.wifi.proto.nano.WifiMetricsProto.StaEvent; import com.android.server.wifi.proto.nano.WifiMetricsProto.WifiIsUnusableEvent; import com.android.server.wifi.proto.nano.WifiMetricsProto.WifiUsabilityStats; +import com.android.server.wifi.util.TelephonyUtil; import com.android.server.wifi.util.WifiPermissionsUtil; import com.android.server.wifi.util.WifiPermissionsWrapper; import com.android.wifi.R; @@ -120,6 +123,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import java.util.Objects; import java.util.concurrent.CountDownLatch; import java.util.function.Consumer; @@ -160,6 +164,9 @@ public class ClientModeImplTest extends WifiBaseTest { private static final byte RSSI_THRESHOLD_BREACH_MIN = -80; private static final byte RSSI_THRESHOLD_BREACH_MAX = -20; + private static final int DATA_SUBID = 1; + private static final int CARRIER_ID_1 = 100; + private long mBinderToken; private MockitoSession mSession; @@ -341,6 +348,7 @@ public class ClientModeImplTest extends WifiBaseTest { IpClientCallbacks mIpClientCallback; OsuProvider mOsuProvider; WifiConfiguration mConnectedNetwork; + TelephonyUtil mTelephonyUtil; @Mock WifiScanner mWifiScanner; @Mock SupplicantStateTracker mSupplicantStateTracker; @@ -388,6 +396,7 @@ public class ClientModeImplTest extends WifiBaseTest { @Mock Handler mNetworkAgentHandler; @Mock BatteryStatsManager mBatteryStatsManager; @Mock MboOceController mMboOceController; + @Mock SubscriptionManager mSubscriptionManager; final ArgumentCaptor<WifiConfigManager.OnNetworkUpdateListener> mConfigUpdateListenerCaptor = ArgumentCaptor.forClass(WifiConfigManager.OnNetworkUpdateListener.class); @@ -490,7 +499,13 @@ public class ClientModeImplTest extends WifiBaseTest { mIpClientCallback.onQuit(); return null; }).when(mIpClient).shutdown(); + List<SubscriptionInfo> subList = new ArrayList<>() {{ + add(mock(SubscriptionInfo.class)); + }}; + when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn(subList); + TelephonyUtil tu = new TelephonyUtil(mTelephonyManager, mSubscriptionManager); + mTelephonyUtil = spy(tu); // static mocking mSession = ExtendedMockito.mockitoSession().strictness(Strictness.LENIENT) .spyStatic(MacAddress.class) @@ -545,7 +560,7 @@ public class ClientModeImplTest extends WifiBaseTest { mUserManager, mWifiInjector, mBackupManagerProxy, mCountryCode, mWifiNative, mWrongPasswordNotifier, mSarManager, mWifiTrafficPoller, mLinkProbeManager, mBatteryStatsManager, mSupplicantStateTracker, - mMboOceController); + mMboOceController, mTelephonyUtil); mCmi.start(); mWifiCoreThread = getCmiHandlerThread(mCmi); @@ -979,6 +994,78 @@ public class ClientModeImplTest extends WifiBaseTest { } /** + * When the SIM card was removed, if the current wifi connection is notusing it, the connection + * should be kept. + */ + @Test + public void testResetSimWhenNonConnectedSimRemoved() throws Exception { + mConnectedNetwork = spy(WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.SIM, WifiEnterpriseConfig.Phase2.NONE)); + mConnectedNetwork.carrierId = CARRIER_ID_1; + doReturn(DATA_SUBID).when(mTelephonyUtil) + .getBestMatchSubscriptionId(any(WifiConfiguration.class)); + when(mDataTelephonyManager.getSimOperator()).thenReturn("123456"); + when(mDataTelephonyManager.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY); + mConnectedNetwork.enterpriseConfig.setAnonymousIdentity(""); + + String expectedAnonymousIdentity = "anonymous@wlan.mnc456.mcc123.3gppnetwork.org"; + + triggerConnect(); + + when(mWifiConfigManager.getScanDetailCacheForNetwork(FRAMEWORK_NETWORK_ID)) + .thenReturn(mScanDetailCache); + when(mScanDetailCache.getScanDetail(sBSSID)).thenReturn( + getGoogleGuestScanDetail(TEST_RSSI, sBSSID, sFreq)); + when(mScanDetailCache.getScanResult(sBSSID)).thenReturn( + getGoogleGuestScanDetail(TEST_RSSI, sBSSID, sFreq).getScanResult()); + + mCmi.sendMessage(WifiMonitor.NETWORK_CONNECTION_EVENT, 0, 0, sBSSID); + mLooper.dispatchAll(); + + doReturn(true).when(mTelephonyUtil).isSimPresent(eq(DATA_SUBID)); + mCmi.sendMessage(ClientModeImpl.CMD_RESET_SIM_NETWORKS, false); + mLooper.dispatchAll(); + + assertEquals("ObtainingIpState", getCurrentState().getName()); + } + + /** + * When the SIM card was removed, if the current wifi connection is using it, the connection + * should be disconnected. + */ + @Test + public void testResetSimWhenConnectedSimRemoved() throws Exception { + mConnectedNetwork = spy(WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.SIM, WifiEnterpriseConfig.Phase2.NONE)); + mConnectedNetwork.carrierId = CARRIER_ID_1; + doReturn(DATA_SUBID).when(mTelephonyUtil) + .getBestMatchSubscriptionId(any(WifiConfiguration.class)); + when(mDataTelephonyManager.getSimOperator()).thenReturn("123456"); + when(mDataTelephonyManager.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY); + mConnectedNetwork.enterpriseConfig.setAnonymousIdentity(""); + + String expectedAnonymousIdentity = "anonymous@wlan.mnc456.mcc123.3gppnetwork.org"; + + triggerConnect(); + + when(mWifiConfigManager.getScanDetailCacheForNetwork(FRAMEWORK_NETWORK_ID)) + .thenReturn(mScanDetailCache); + when(mScanDetailCache.getScanDetail(sBSSID)).thenReturn( + getGoogleGuestScanDetail(TEST_RSSI, sBSSID, sFreq)); + when(mScanDetailCache.getScanResult(sBSSID)).thenReturn( + getGoogleGuestScanDetail(TEST_RSSI, sBSSID, sFreq).getScanResult()); + + mCmi.sendMessage(WifiMonitor.NETWORK_CONNECTION_EVENT, 0, 0, sBSSID); + mLooper.dispatchAll(); + + doReturn(false).when(mTelephonyUtil).isSimPresent(eq(DATA_SUBID)); + mCmi.sendMessage(ClientModeImpl.CMD_RESET_SIM_NETWORKS, false); + mLooper.dispatchAll(); + + assertEquals("DisconnectingState", getCurrentState().getName()); + } + + /** * Tests anonymous identity is set again whenever a connection is established for the carrier * that supports encrypted IMSI and anonymous identity and no real pseudonym was provided. */ @@ -991,7 +1078,10 @@ public class ClientModeImplTest extends WifiBaseTest { mConnectedNetwork.enterpriseConfig.setAnonymousIdentity(""); String expectedAnonymousIdentity = "anonymous@wlan.mnc456.mcc123.3gppnetwork.org"; - + MockitoSession mockSession = ExtendedMockito.mockitoSession() + .mockStatic(SubscriptionManager.class) + .startMocking(); + when(SubscriptionManager.getDefaultDataSubscriptionId()).thenReturn(DATA_SUBID); when(mCarrierNetworkConfig.isCarrierEncryptionInfoAvailable()).thenReturn(true); // Initial value should be "not set" @@ -1025,6 +1115,7 @@ public class ClientModeImplTest extends WifiBaseTest { // trigger "add or update network" operation. The test needs to be updated to account for // this change. verify(mWifiConfigManager).addOrUpdateNetwork(any(), anyInt()); + mockSession.finishMocking(); } /** @@ -1043,7 +1134,10 @@ public class ClientModeImplTest extends WifiBaseTest { String expectedAnonymousIdentity = "anonymous@wlan.mnc456.mcc123.3gppnetwork.org"; String pseudonym = "83bcca9384fca@wlan.mnc456.mcc123.3gppnetwork.org"; - + MockitoSession mockSession = ExtendedMockito.mockitoSession() + .mockStatic(SubscriptionManager.class) + .startMocking(); + when(SubscriptionManager.getDefaultDataSubscriptionId()).thenReturn(DATA_SUBID); when(mCarrierNetworkConfig.isCarrierEncryptionInfoAvailable()).thenReturn(true); triggerConnect(); @@ -1074,6 +1168,7 @@ public class ClientModeImplTest extends WifiBaseTest { // trigger "add or update network" operation. The test needs to be updated to account for // this change. verify(mWifiConfigManager).addOrUpdateNetwork(any(), anyInt()); + mockSession.finishMocking(); } /** @@ -1093,7 +1188,10 @@ public class ClientModeImplTest extends WifiBaseTest { String realm = "wlan.mnc456.mcc123.3gppnetwork.org"; String expectedAnonymousIdentity = "anonymous"; String pseudonym = "83bcca9384fca"; - + MockitoSession mockSession = ExtendedMockito.mockitoSession() + .mockStatic(SubscriptionManager.class) + .startMocking(); + when(SubscriptionManager.getDefaultDataSubscriptionId()).thenReturn(DATA_SUBID); when(mCarrierNetworkConfig.isCarrierEncryptionInfoAvailable()).thenReturn(true); triggerConnect(); @@ -1124,6 +1222,7 @@ public class ClientModeImplTest extends WifiBaseTest { // trigger "add or update network" operation. The test needs to be updated to account for // this change. verify(mWifiConfigManager).addOrUpdateNetwork(any(), anyInt()); + mockSession.finishMocking(); } /** * Tests the Passpoint information is set in WifiInfo for Passpoint AP connection. @@ -1587,6 +1686,11 @@ public class ClientModeImplTest extends WifiBaseTest { config.getNetworkSelectionStatus().setHasEverConnected(true); config.enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.SIM); when(mWifiConfigManager.getConfiguredNetwork(anyInt())).thenReturn(config); + MockitoSession mockSession = ExtendedMockito.mockitoSession() + .mockStatic(SubscriptionManager.class) + .startMocking(); + when(SubscriptionManager.getDefaultDataSubscriptionId()).thenReturn(DATA_SUBID); + when(SubscriptionManager.isValidSubscriptionId(anyInt())).thenReturn(true); mCmi.sendMessage(WifiMonitor.AUTHENTICATION_FAILURE_EVENT, WifiManager.ERROR_AUTH_FAILURE_EAP_FAILURE, @@ -1594,6 +1698,7 @@ public class ClientModeImplTest extends WifiBaseTest { mLooper.dispatchAll(); verify(mDataTelephonyManager).resetCarrierKeysForImsiEncryption(); + mockSession.finishMocking(); } /** @@ -3713,6 +3818,11 @@ public class ClientModeImplTest extends WifiBaseTest { when(mDataTelephonyManager.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY); when(mDataTelephonyManager.getSimOperator()).thenReturn("321456"); when(mDataTelephonyManager.getCarrierInfoForImsiEncryption(anyInt())).thenReturn(null); + MockitoSession mockSession = ExtendedMockito.mockitoSession() + .mockStatic(SubscriptionManager.class) + .startMocking(); + when(SubscriptionManager.getDefaultDataSubscriptionId()).thenReturn(DATA_SUBID); + when(SubscriptionManager.isValidSubscriptionId(anyInt())).thenReturn(true); triggerConnect(); @@ -3722,6 +3832,7 @@ public class ClientModeImplTest extends WifiBaseTest { verify(mWifiNative).simIdentityResponse(WIFI_IFACE_NAME, "13214561234567890@wlan.mnc456.mcc321.3gppnetwork.org", ""); + mockSession.finishMocking(); } /** diff --git a/tests/wifitests/src/com/android/server/wifi/SavedNetworkEvaluatorTest.java b/tests/wifitests/src/com/android/server/wifi/SavedNetworkEvaluatorTest.java index b14f210f6..3a76eda50 100644 --- a/tests/wifitests/src/com/android/server/wifi/SavedNetworkEvaluatorTest.java +++ b/tests/wifitests/src/com/android/server/wifi/SavedNetworkEvaluatorTest.java @@ -27,12 +27,12 @@ import android.content.res.Resources; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; import android.os.SystemClock; -import android.telephony.SubscriptionManager; import android.test.suitebuilder.annotation.SmallTest; import android.util.LocalLog; import com.android.server.wifi.WifiNetworkSelector.NetworkEvaluator.OnConnectableListener; import com.android.server.wifi.WifiNetworkSelectorTestUtil.ScanDetailsAndWifiConfigs; +import com.android.server.wifi.util.TelephonyUtil; import com.android.wifi.R; import org.junit.After; @@ -41,7 +41,6 @@ import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import java.util.Collections; import java.util.List; /** @@ -77,10 +76,7 @@ public class SavedNetworkEvaluatorTest extends WifiBaseTest { mSavedNetworkEvaluator = new SavedNetworkEvaluator(mContext, new ScoringParams(mContext), mWifiConfigManager, - mClock, mLocalLog, mWifiConnectivityHelper, mSubscriptionManager); - // SIM is absent - when(mSubscriptionManager.getActiveSubscriptionInfoList()) - .thenReturn(Collections.emptyList()); + mClock, mLocalLog, mWifiConnectivityHelper, mTelephonyUtil); } /** Cleans up test. */ @@ -89,6 +85,9 @@ public class SavedNetworkEvaluatorTest extends WifiBaseTest { validateMockitoUsage(); } + private static final int INVALID_SUBID = 1; + private static final int TEST_CARRIER_ID = 100; + private SavedNetworkEvaluator mSavedNetworkEvaluator; @Mock private WifiConfigManager mWifiConfigManager; @Mock private WifiConnectivityHelper mWifiConnectivityHelper; @@ -96,7 +95,7 @@ public class SavedNetworkEvaluatorTest extends WifiBaseTest { @Mock private Resources mResource; @Mock private Clock mClock; @Mock private OnConnectableListener mOnConnectableListener; - @Mock private SubscriptionManager mSubscriptionManager; + @Mock private TelephonyUtil mTelephonyUtil; private LocalLog mLocalLog; private int mThresholdMinimumRssi2G; private int mThresholdMinimumRssi5G; @@ -181,6 +180,33 @@ public class SavedNetworkEvaluatorTest extends WifiBaseTest { } /** + * Do not evaluate networks which require SIM card when the SIM card is absent. + */ + @Test + public void ignoreNetworkIfSimIsAbsentForEapSimNetwork() { + String[] ssids = {"\"test1\""}; + String[] bssids = {"6c:f3:7f:ae:8c:f3"}; + int[] freqs = {2470}; + int[] levels = {mThresholdQualifiedRssi2G + 8}; + + ScanDetailsAndWifiConfigs scanDetailsAndConfigs = + WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigForEapSimNetwork(ssids, bssids, + freqs, levels, mWifiConfigManager, mClock); + List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); + WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); + savedConfigs[0].carrierId = TEST_CARRIER_ID; + // SIM is absent + when(mTelephonyUtil.getBestMatchSubscriptionId(any(WifiConfiguration.class))) + .thenReturn(INVALID_SUBID); + when(mTelephonyUtil.isSimPresent(eq(INVALID_SUBID))).thenReturn(false); + + WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, + null, null, true, false, mOnConnectableListener); + + assertNull(candidate); + } + + /** * Do not evaluate networks that {@link WifiConfiguration#isEphemeral}. */ @Test diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java index e327c1d61..e99224512 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java @@ -42,6 +42,8 @@ import android.os.UserHandle; import android.os.UserManager; import android.os.test.TestLooper; import android.provider.Settings; +import android.telephony.SubscriptionInfo; +import android.telephony.SubscriptionManager; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Pair; @@ -113,10 +115,12 @@ public class WifiConfigManagerTest extends WifiBaseTest { private static final int TEST_FREQUENCY_3 = 5240; private static final MacAddress TEST_RANDOMIZED_MAC = MacAddress.fromString("d2:11:19:34:a5:20"); + private static final int DATA_SUBID = 1; @Mock private Context mContext; @Mock private Clock mClock; @Mock private UserManager mUserManager; + @Mock private SubscriptionManager mSubscriptionManager; @Mock private TelephonyManager mTelephonyManager; @Mock private TelephonyManager mDataTelephonyManager; @Mock private WifiKeyStore mWifiKeyStore; @@ -145,6 +149,7 @@ public class WifiConfigManagerTest extends WifiBaseTest { private ContentObserver mContentObserverPnoChannelCulling; private ContentObserver mContentObserverPnoRecencySorting; private MockitoSession mSession; + private TelephonyUtil mTelephonyUtil; /** * Setup the mocks and an instance of WifiConfigManager before each test. @@ -220,6 +225,7 @@ public class WifiConfigManagerTest extends WifiBaseTest { when(mMacAddressUtil.calculatePersistentMacForConfiguration(any(), any())) .thenReturn(TEST_RANDOMIZED_MAC); + mTelephonyUtil = new TelephonyUtil(mTelephonyManager, mSubscriptionManager); createWifiConfigManager(); mWifiConfigManager.addOnNetworkUpdateListener(mWcmListener); ArgumentCaptor<ContentObserver> observerCaptor = @@ -4563,6 +4569,10 @@ public class WifiConfigManagerTest extends WifiBaseTest { when(mDataTelephonyManager.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY); when(mDataTelephonyManager.getSimOperator()).thenReturn("321456"); when(mDataTelephonyManager.getCarrierInfoForImsiEncryption(anyInt())).thenReturn(null); + List<SubscriptionInfo> subList = new ArrayList<>() {{ + add(mock(SubscriptionInfo.class)); + }}; + when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn(subList); WifiConfiguration network = WifiConfigurationTestUtil.createEapNetwork(); WifiConfiguration simNetwork = WifiConfigurationTestUtil.createEapNetwork( @@ -4578,6 +4588,11 @@ public class WifiConfigManagerTest extends WifiBaseTest { verifyAddNetworkToWifiConfigManager(network); verifyAddNetworkToWifiConfigManager(simNetwork); verifyAddNetworkToWifiConfigManager(peapSimNetwork); + MockitoSession mockSession = ExtendedMockito.mockitoSession() + .mockStatic(SubscriptionManager.class) + .startMocking(); + when(SubscriptionManager.getDefaultDataSubscriptionId()).thenReturn(DATA_SUBID); + when(SubscriptionManager.isValidSubscriptionId(anyInt())).thenReturn(true); // SIM was removed, resetting SIM Networks mWifiConfigManager.resetSimNetworks(); @@ -4594,6 +4609,8 @@ public class WifiConfigManagerTest extends WifiBaseTest { mWifiConfigManager.getConfiguredNetwork(peapSimNetwork.networkId); assertEquals(expectedIdentity, retrievedPeapSimNetwork.enterpriseConfig.getIdentity()); assertNotEquals("", retrievedPeapSimNetwork.enterpriseConfig.getAnonymousIdentity()); + + mockSession.finishMocking(); } /** @@ -4604,12 +4621,14 @@ public class WifiConfigManagerTest extends WifiBaseTest { */ @Test public void testResetSimNetworks_getSimIdentityNull_shouldResetAllNonPeapSimIdentities() { - when(mDataTelephonyManager.getSubscriberId()).thenReturn("3214561234567890"); + when(mDataTelephonyManager.getSubscriberId()).thenReturn(""); when(mDataTelephonyManager.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY); - when(mDataTelephonyManager.getSimOperator()).thenReturn("321456"); + when(mDataTelephonyManager.getSimOperator()).thenReturn(""); when(mDataTelephonyManager.getCarrierInfoForImsiEncryption(anyInt())).thenReturn(null); - // null CarrierNetworkConfig => getSimIdentity returns null => PEAP identity unchanged - when(mWifiInjector.getCarrierNetworkConfig()).thenReturn(null); + List<SubscriptionInfo> subList = new ArrayList<>() {{ + add(mock(SubscriptionInfo.class)); + }}; + when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn(subList); WifiConfiguration peapSimNetwork = WifiConfigurationTestUtil.createEapNetwork( WifiEnterpriseConfig.Eap.PEAP, WifiEnterpriseConfig.Phase2.SIM); @@ -4627,6 +4646,11 @@ public class WifiConfigManagerTest extends WifiBaseTest { simNetwork.enterpriseConfig.setIdentity("identity_eap_sim"); simNetwork.enterpriseConfig.setAnonymousIdentity("anonymous_identity_eap_sim"); verifyAddNetworkToWifiConfigManager(simNetwork); + MockitoSession mockSession = ExtendedMockito.mockitoSession() + .mockStatic(SubscriptionManager.class) + .startMocking(); + when(SubscriptionManager.getDefaultDataSubscriptionId()).thenReturn(DATA_SUBID); + when(SubscriptionManager.isValidSubscriptionId(anyInt())).thenReturn(true); // SIM was removed, resetting SIM Networks mWifiConfigManager.resetSimNetworks(); @@ -4645,6 +4669,8 @@ public class WifiConfigManagerTest extends WifiBaseTest { assertEquals("identity_peap", retrievedPeapSimNetwork.enterpriseConfig.getIdentity()); assertEquals("anonymous_identity_peap", retrievedPeapSimNetwork.enterpriseConfig.getAnonymousIdentity()); + + mockSession.finishMocking(); } /** @@ -4832,7 +4858,7 @@ public class WifiConfigManagerTest extends WifiBaseTest { private void createWifiConfigManager() { mWifiConfigManager = new WifiConfigManager( - mContext, mClock, mUserManager, mTelephonyManager, + mContext, mClock, mUserManager, mTelephonyUtil, mWifiKeyStore, mWifiConfigStore, mWifiPermissionsUtil, mWifiPermissionsWrapper, mWifiInjector, mNetworkListSharedStoreData, mNetworkListUserStoreData, diff --git a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTestUtil.java b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTestUtil.java index 74d6cb747..287d0b0d8 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTestUtil.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTestUtil.java @@ -29,6 +29,7 @@ import android.net.WifiKey; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiConfiguration.NetworkSelectionStatus; +import android.net.wifi.WifiEnterpriseConfig; import android.net.wifi.WifiNetworkScoreCache; import android.net.wifi.WifiSsid; import android.text.TextUtils; @@ -38,8 +39,10 @@ import com.android.server.wifi.util.ScanResultUtil; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; /** * Helper for WifiNetworkSelector unit tests. @@ -93,6 +96,47 @@ public class WifiNetworkSelectorTestUtil { return new ScanDetailsAndWifiConfigs(scanDetails, savedConfigs); } + /** + * Build a list of ScanDetail based on the caller supplied network SSID, BSSID, + * frequency and RSSI level information. Create the EAP-SIM authticated + * WifiConfiguration for these networks and set up the mocked WifiConfigManager. + * + * @param ssids an array of SSIDs + * @param bssids an array of BSSIDs + * @param freqs an array of the network's frequency + * @param levels an array of the network's RSSI levels + * @param wifiConfigManager the mocked WifiConfigManager + * @return the constructed ScanDetail list and WifiConfiguration array + */ + public static ScanDetailsAndWifiConfigs setupScanDetailsAndConfigForEapSimNetwork( + String[] ssids, + String[] bssids, int[] freqs, int[] levels, + WifiConfigManager wifiConfigManager, Clock clock) { + assertNotNull(ssids); + String[] caps = new String[ssids.length]; + for (int i = 0; i < ssids.length; i++) { + caps[i] = "[EAP][ESS]"; + } + List<ScanDetail> scanDetails = buildScanDetails(ssids, bssids, freqs, caps, levels, clock); + WifiConfiguration[] savedConfigs = new WifiConfiguration[ssids.length]; + Set<String> ssidSet = new HashSet<>(); + for (int i = 0; i < ssids.length; i++) { + // do not allow duplicated ssid + assertFalse(ssidSet.contains(ssids[i])); + ssidSet.add(ssids[i]); + savedConfigs[i] = WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.SIM, WifiEnterpriseConfig.Phase2.NONE); + savedConfigs[i].SSID = ssids[i]; + savedConfigs[i].networkId = i; + } + + checkConsistencyOfScanDetailsAndWifiConfigs(scanDetails, savedConfigs); + prepareConfigStore(wifiConfigManager, savedConfigs); + scanResultLinkConfiguration(wifiConfigManager, savedConfigs, scanDetails); + + return new ScanDetailsAndWifiConfigs(scanDetails, savedConfigs); + } + private static void checkConsistencyOfScanDetailsAndWifiConfigs( List<ScanDetail> scanDetails, WifiConfiguration[] savedConfigs) { diff --git a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java index 02ea0e3c5..6dc87aaf7 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java @@ -46,6 +46,7 @@ import android.content.res.Resources; import android.net.MacAddress; import android.net.wifi.ISuggestionConnectionStatusListener; import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiEnterpriseConfig; import android.net.wifi.WifiManager; import android.net.wifi.WifiNetworkSuggestion; import android.net.wifi.WifiScanner; @@ -61,6 +62,7 @@ import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; import com.android.server.wifi.WifiNetworkSuggestionsManager.ExtendedWifiNetworkSuggestion; import com.android.server.wifi.WifiNetworkSuggestionsManager.PerAppInfo; import com.android.server.wifi.hotspot2.PasspointManager; +import com.android.server.wifi.util.TelephonyUtil; import com.android.server.wifi.util.WifiPermissionsUtil; import com.android.wifi.R; @@ -95,7 +97,8 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { 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 static final int VALID_CARRIER_ID = 100; + private static final int TEST_SUBID = 1; private @Mock Context mContext; private @Mock Resources mResources; @@ -109,6 +112,7 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { private @Mock WifiConfigManager mWifiConfigManager; private @Mock NetworkSuggestionStoreData mNetworkSuggestionStoreData; private @Mock WifiMetrics mWifiMetrics; + private @Mock TelephonyUtil mTelephonyUtil; private @Mock PasspointManager mPasspointManager; private @Mock ISuggestionConnectionStatusListener mListener; private @Mock IBinder mBinder; @@ -171,7 +175,7 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { mWifiNetworkSuggestionsManager = new WifiNetworkSuggestionsManager(mContext, new Handler(mLooper.getLooper()), mWifiInjector, mWifiPermissionsUtil, mWifiConfigManager, mWifiConfigStore, - mWifiMetrics); + mWifiMetrics, mTelephonyUtil); verify(mContext).getResources(); verify(mContext).getSystemService(Context.APP_OPS_SERVICE); verify(mContext).getSystemService(Context.NOTIFICATION_SERVICE); @@ -523,6 +527,40 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { } /** + * Do not evaluate the suggested network which requires SIM card, but the SIM is absent. + */ + @Test + public void testGetNetworkSuggestionsForScanDtailIgnoreEapSimNetworkForAbsentSim() { + WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.SIM, WifiEnterpriseConfig.Phase2.NONE); + config.carrierId = VALID_CARRIER_ID; + WifiNetworkSuggestion networkSuggestion = new WifiNetworkSuggestion( + config, null, false, false, TEST_UID_1, + TEST_PACKAGE_1); + List<WifiNetworkSuggestion> networkSuggestionList1 = + new ArrayList<WifiNetworkSuggestion>() {{ + add(networkSuggestion); + }}; + when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(eq(TEST_UID_1))) + .thenReturn(true); + assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, + mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, + TEST_PACKAGE_1, TEST_FEATURE)); + mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_PACKAGE_1); + + ScanDetail scanDetail = createScanDetailForNetwork(networkSuggestion.wifiConfiguration); + + when(mTelephonyUtil.getBestMatchSubscriptionId(any(WifiConfiguration.class))) + .thenReturn(TEST_SUBID); + when(mTelephonyUtil.isSimPresent(eq(TEST_SUBID))).thenReturn(false); + + Set<WifiNetworkSuggestion> matchingNetworkSuggestions = + mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail(scanDetail); + + assertNull(matchingNetworkSuggestions); + } + + /** * Verify a successful lookup of a single network suggestion matching the provided scan detail. */ @Test diff --git a/tests/wifitests/src/com/android/server/wifi/util/TelephonyUtilTest.java b/tests/wifitests/src/com/android/server/wifi/util/TelephonyUtilTest.java index 483855a16..5cff6d3d1 100644 --- a/tests/wifitests/src/com/android/server/wifi/util/TelephonyUtilTest.java +++ b/tests/wifitests/src/com/android/server/wifi/util/TelephonyUtilTest.java @@ -16,12 +16,17 @@ package com.android.server.wifi.util; +import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; +import static com.android.dx.mockito.inline.extended.ExtendedMockito.when; + import static org.junit.Assert.*; import static org.mockito.Mockito.*; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiEnterpriseConfig; import android.telephony.ImsiEncryptionInfo; +import android.telephony.SubscriptionInfo; +import android.telephony.SubscriptionManager; import android.telephony.TelephonyManager; import android.util.Base64; import android.util.Pair; @@ -29,19 +34,23 @@ import android.util.Pair; import androidx.test.filters.SmallTest; import com.android.dx.mockito.inline.extended.ExtendedMockito; -import com.android.server.wifi.CarrierNetworkConfig; import com.android.server.wifi.WifiBaseTest; import com.android.server.wifi.WifiConfigurationTestUtil; import com.android.server.wifi.util.TelephonyUtil.SimAuthRequestData; import com.android.server.wifi.util.TelephonyUtil.SimAuthResponseData; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.MockitoSession; +import org.mockito.quality.Strictness; import java.security.PublicKey; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; @@ -53,18 +62,63 @@ import javax.crypto.Cipher; public class TelephonyUtilTest extends WifiBaseTest { private TelephonyUtil mTelephonyUtil; - @Mock - CarrierNetworkConfig mCarrierNetworkConfig; + private static final int DATA_SUBID = 1; + private static final int NON_DATA_SUBID = 2; + private static final int INVALID_SUBID = -1; + private static final int DATA_CARRIER_ID = 10; + private static final int NON_DATA_CARRIER_ID = 20; + private static final int DEACTIVE_CARRIER_ID = 30; + + private List<SubscriptionInfo> mSubInfoList; + + MockitoSession mMockingSession = null; + @Mock TelephonyManager mTelephonyManager; @Mock TelephonyManager mDataTelephonyManager; + @Mock + TelephonyManager mNonDataTelephonyManager; + @Mock + SubscriptionManager mSubscriptionManager; + @Mock + SubscriptionInfo mDataSubscriptionInfo; + @Mock + SubscriptionInfo mNonDataSubscriptionInfo; @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); - mTelephonyUtil = new TelephonyUtil(); - when(mTelephonyManager.createForSubscriptionId(anyInt())).thenReturn(mDataTelephonyManager); + mTelephonyUtil = new TelephonyUtil(mTelephonyManager, mSubscriptionManager); + mSubInfoList = new ArrayList<>(); + mSubInfoList.add(mDataSubscriptionInfo); + mSubInfoList.add(mNonDataSubscriptionInfo); + when(mTelephonyManager.createForSubscriptionId(eq(DATA_SUBID))) + .thenReturn(mDataTelephonyManager); + when(mTelephonyManager.createForSubscriptionId(eq(NON_DATA_SUBID))) + .thenReturn(mNonDataTelephonyManager); + when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn(mSubInfoList); + mMockingSession = ExtendedMockito.mockitoSession().strictness(Strictness.LENIENT) + .mockStatic(SubscriptionManager.class).startMocking(); + + doReturn(DATA_SUBID).when( + () -> SubscriptionManager.getDefaultDataSubscriptionId()); + doReturn(true).when( + () -> SubscriptionManager.isValidSubscriptionId(DATA_SUBID)); + doReturn(true).when( + () -> SubscriptionManager.isValidSubscriptionId(NON_DATA_SUBID)); + + when(mDataSubscriptionInfo.getCarrierId()).thenReturn(DATA_CARRIER_ID); + when(mDataSubscriptionInfo.getSubscriptionId()).thenReturn(DATA_SUBID); + when(mNonDataSubscriptionInfo.getCarrierId()).thenReturn(NON_DATA_CARRIER_ID); + when(mNonDataSubscriptionInfo.getSubscriptionId()).thenReturn(NON_DATA_SUBID); + } + + @After + public void cleanUp() throws Exception { + if (mMockingSession != null) { + mMockingSession.finishMocking(); + } } @Test @@ -76,15 +130,19 @@ public class TelephonyUtilTest extends WifiBaseTest { when(mDataTelephonyManager.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY); when(mDataTelephonyManager.getSimOperator()).thenReturn("321456"); when(mDataTelephonyManager.getCarrierInfoForImsiEncryption(anyInt())).thenReturn(null); - - assertEquals(expectedIdentity, TelephonyUtil.getSimIdentity(mTelephonyManager, - mTelephonyUtil, + WifiConfiguration simConfig = WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.SIM, - WifiEnterpriseConfig.Phase2.NONE), mCarrierNetworkConfig)); - assertEquals(expectedIdentity, - TelephonyUtil.getSimIdentity(mTelephonyManager, mTelephonyUtil, - WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.PEAP, - WifiEnterpriseConfig.Phase2.SIM), mCarrierNetworkConfig)); + WifiEnterpriseConfig.Phase2.NONE); + simConfig.carrierId = DATA_CARRIER_ID; + + assertEquals(expectedIdentity, mTelephonyUtil.getSimIdentity(simConfig)); + + WifiConfiguration peapSimConfig = + WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.PEAP, + WifiEnterpriseConfig.Phase2.SIM); + peapSimConfig.carrierId = DATA_CARRIER_ID; + + assertEquals(expectedIdentity, mTelephonyUtil.getSimIdentity(peapSimConfig)); } @Test @@ -96,15 +154,19 @@ public class TelephonyUtilTest extends WifiBaseTest { when(mDataTelephonyManager.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY); when(mDataTelephonyManager.getSimOperator()).thenReturn("321456"); when(mDataTelephonyManager.getCarrierInfoForImsiEncryption(anyInt())).thenReturn(null); + WifiConfiguration akaConfig = + WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.AKA, + WifiEnterpriseConfig.Phase2.NONE); + akaConfig.carrierId = DATA_CARRIER_ID; - assertEquals(expectedIdentity, - TelephonyUtil.getSimIdentity(mTelephonyManager, mTelephonyUtil, - WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.AKA, - WifiEnterpriseConfig.Phase2.NONE), mCarrierNetworkConfig)); - assertEquals(expectedIdentity, - TelephonyUtil.getSimIdentity(mTelephonyManager, mTelephonyUtil, - WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.PEAP, - WifiEnterpriseConfig.Phase2.AKA), mCarrierNetworkConfig)); + assertEquals(expectedIdentity, mTelephonyUtil.getSimIdentity(akaConfig)); + + WifiConfiguration peapAkaConfig = + WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.PEAP, + WifiEnterpriseConfig.Phase2.AKA); + peapAkaConfig.carrierId = DATA_CARRIER_ID; + + assertEquals(expectedIdentity, mTelephonyUtil.getSimIdentity(peapAkaConfig)); } @Test @@ -116,16 +178,19 @@ public class TelephonyUtilTest extends WifiBaseTest { when(mDataTelephonyManager.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY); when(mDataTelephonyManager.getSimOperator()).thenReturn("321456"); when(mDataTelephonyManager.getCarrierInfoForImsiEncryption(anyInt())).thenReturn(null); + WifiConfiguration akaPConfig = + WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.AKA_PRIME, + WifiEnterpriseConfig.Phase2.NONE); + akaPConfig.carrierId = DATA_CARRIER_ID; - assertEquals(expectedIdentity, - TelephonyUtil.getSimIdentity(mTelephonyManager, mTelephonyUtil, - WifiConfigurationTestUtil.createEapNetwork( - WifiEnterpriseConfig.Eap.AKA_PRIME, - WifiEnterpriseConfig.Phase2.NONE), mCarrierNetworkConfig)); - assertEquals(expectedIdentity, - TelephonyUtil.getSimIdentity(mTelephonyManager, mTelephonyUtil, - WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.PEAP, - WifiEnterpriseConfig.Phase2.AKA_PRIME), mCarrierNetworkConfig)); + assertEquals(expectedIdentity, mTelephonyUtil.getSimIdentity(akaPConfig)); + + WifiConfiguration peapAkaPConfig = + WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.PEAP, + WifiEnterpriseConfig.Phase2.AKA_PRIME); + peapAkaPConfig.carrierId = DATA_CARRIER_ID; + + assertEquals(expectedIdentity, mTelephonyUtil.getSimIdentity(peapAkaPConfig)); } /** @@ -158,11 +223,12 @@ public class TelephonyUtilTest extends WifiBaseTest { when(mDataTelephonyManager.getCarrierInfoForImsiEncryption( eq(TelephonyManager.KEY_TYPE_WLAN))) .thenReturn(info); + WifiConfiguration config = + WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.AKA, + WifiEnterpriseConfig.Phase2.NONE); + config.carrierId = DATA_CARRIER_ID; - assertEquals(expectedIdentity, - TelephonyUtil.getSimIdentity(mTelephonyManager, mTelephonyUtil, - WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.AKA, - WifiEnterpriseConfig.Phase2.NONE), mCarrierNetworkConfig)); + assertEquals(expectedIdentity, mTelephonyUtil.getSimIdentity(config)); } finally { session.finishMocking(); } @@ -193,9 +259,12 @@ public class TelephonyUtilTest extends WifiBaseTest { eq(TelephonyManager.KEY_TYPE_WLAN))) .thenReturn(info); - assertNull(TelephonyUtil.getSimIdentity(mTelephonyManager, mTelephonyUtil, + WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.AKA, - WifiEnterpriseConfig.Phase2.NONE), mCarrierNetworkConfig)); + WifiEnterpriseConfig.Phase2.NONE); + config.carrierId = DATA_CARRIER_ID; + + assertNull(mTelephonyUtil.getSimIdentity(config)); } finally { session.finishMocking(); } @@ -210,11 +279,12 @@ public class TelephonyUtilTest extends WifiBaseTest { when(mDataTelephonyManager.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY); when(mDataTelephonyManager.getSimOperator()).thenReturn("32156"); when(mDataTelephonyManager.getCarrierInfoForImsiEncryption(anyInt())).thenReturn(null); + WifiConfiguration config = + WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.SIM, + WifiEnterpriseConfig.Phase2.NONE); + config.carrierId = DATA_CARRIER_ID; - assertEquals(expectedIdentity, - TelephonyUtil.getSimIdentity(mTelephonyManager, mTelephonyUtil, - WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.SIM, - WifiEnterpriseConfig.Phase2.NONE), mCarrierNetworkConfig)); + assertEquals(expectedIdentity, mTelephonyUtil.getSimIdentity(config)); } @Test @@ -226,19 +296,12 @@ public class TelephonyUtilTest extends WifiBaseTest { when(mDataTelephonyManager.getSimState()).thenReturn(TelephonyManager.SIM_STATE_UNKNOWN); when(mDataTelephonyManager.getSimOperator()).thenReturn(null); when(mDataTelephonyManager.getCarrierInfoForImsiEncryption(anyInt())).thenReturn(null); + WifiConfiguration config = + WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.SIM, + WifiEnterpriseConfig.Phase2.NONE); + config.carrierId = DATA_CARRIER_ID; - assertEquals(expectedIdentity, - TelephonyUtil.getSimIdentity(mTelephonyManager, mTelephonyUtil, - WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.SIM, - WifiEnterpriseConfig.Phase2.NONE), mCarrierNetworkConfig)); - } - - @Test - public void getSimIdentityWithNoTelephonyManager() { - assertEquals(null, TelephonyUtil.getSimIdentity(null, null, - WifiConfigurationTestUtil.createEapNetwork( - WifiEnterpriseConfig.Eap.SIM, WifiEnterpriseConfig.Phase2.NONE), - mCarrierNetworkConfig)); + assertEquals(expectedIdentity, mTelephonyUtil.getSimIdentity(config)); } @Test @@ -246,47 +309,18 @@ public class TelephonyUtilTest extends WifiBaseTest { when(mDataTelephonyManager.getSubscriberId()).thenReturn("321560123456789"); when(mDataTelephonyManager.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY); when(mDataTelephonyManager.getSimOperator()).thenReturn("32156"); - assertEquals(null, TelephonyUtil.getSimIdentity(mTelephonyManager, mTelephonyUtil, - WifiConfigurationTestUtil.createEapNetwork( - WifiEnterpriseConfig.Eap.TTLS, WifiEnterpriseConfig.Phase2.SIM), - mCarrierNetworkConfig)); - assertEquals(null, TelephonyUtil.getSimIdentity(mTelephonyManager, mTelephonyUtil, - WifiConfigurationTestUtil.createEapNetwork( - WifiEnterpriseConfig.Eap.PEAP, WifiEnterpriseConfig.Phase2.MSCHAPV2), - mCarrierNetworkConfig)); - assertEquals(null, TelephonyUtil.getSimIdentity(mTelephonyManager, mTelephonyUtil, - WifiConfigurationTestUtil.createEapNetwork( - WifiEnterpriseConfig.Eap.TLS, WifiEnterpriseConfig.Phase2.NONE), - mCarrierNetworkConfig)); - assertEquals(null, TelephonyUtil.getSimIdentity( - mTelephonyManager, mTelephonyUtil, new WifiConfiguration(), mCarrierNetworkConfig)); - } - - @Test - public void isSimConfig() { - assertFalse(TelephonyUtil.isSimConfig(null)); - assertFalse(TelephonyUtil.isSimConfig(new WifiConfiguration())); - assertFalse(TelephonyUtil.isSimConfig(WifiConfigurationTestUtil.createOpenNetwork())); - assertFalse(TelephonyUtil.isSimConfig(WifiConfigurationTestUtil.createWepNetwork())); - assertFalse(TelephonyUtil.isSimConfig(WifiConfigurationTestUtil.createPskNetwork())); - assertFalse(TelephonyUtil.isSimConfig(WifiConfigurationTestUtil.createEapNetwork( - WifiEnterpriseConfig.Eap.TTLS, WifiEnterpriseConfig.Phase2.SIM))); - assertFalse(TelephonyUtil.isSimConfig(WifiConfigurationTestUtil.createEapNetwork( - WifiEnterpriseConfig.Eap.TLS, WifiEnterpriseConfig.Phase2.NONE))); - assertFalse(TelephonyUtil.isSimConfig(WifiConfigurationTestUtil.createEapNetwork( - WifiEnterpriseConfig.Eap.PEAP, WifiEnterpriseConfig.Phase2.MSCHAPV2))); - assertTrue(TelephonyUtil.isSimConfig(WifiConfigurationTestUtil.createEapNetwork( - WifiEnterpriseConfig.Eap.SIM, WifiEnterpriseConfig.Phase2.NONE))); - assertTrue(TelephonyUtil.isSimConfig(WifiConfigurationTestUtil.createEapNetwork( - WifiEnterpriseConfig.Eap.AKA, WifiEnterpriseConfig.Phase2.NONE))); - assertTrue(TelephonyUtil.isSimConfig(WifiConfigurationTestUtil.createEapNetwork( - WifiEnterpriseConfig.Eap.AKA_PRIME, WifiEnterpriseConfig.Phase2.NONE))); - assertTrue(TelephonyUtil.isSimConfig(WifiConfigurationTestUtil.createEapNetwork( - WifiEnterpriseConfig.Eap.PEAP, WifiEnterpriseConfig.Phase2.SIM))); - assertTrue(TelephonyUtil.isSimConfig(WifiConfigurationTestUtil.createEapNetwork( - WifiEnterpriseConfig.Eap.PEAP, WifiEnterpriseConfig.Phase2.AKA))); - assertTrue(TelephonyUtil.isSimConfig(WifiConfigurationTestUtil.createEapNetwork( - WifiEnterpriseConfig.Eap.PEAP, WifiEnterpriseConfig.Phase2.AKA_PRIME))); + + assertEquals(null, + mTelephonyUtil.getSimIdentity(WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.TTLS, WifiEnterpriseConfig.Phase2.SIM))); + assertEquals(null, + mTelephonyUtil.getSimIdentity(WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.PEAP, WifiEnterpriseConfig.Phase2.MSCHAPV2))); + assertEquals(null, + mTelephonyUtil.getSimIdentity(WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.TLS, WifiEnterpriseConfig.Phase2.NONE))); + assertEquals(null, + mTelephonyUtil.getSimIdentity(new WifiConfiguration())); } /** @@ -345,7 +379,10 @@ public class TelephonyUtilTest extends WifiBaseTest { @Test public void getGsmSimAuthResponseInvalidRequest() { final String[] invalidRequests = { null, "", "XXXX" }; - assertEquals("", TelephonyUtil.getGsmSimAuthResponse(invalidRequests, mTelephonyManager)); + WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.SIM, WifiEnterpriseConfig.Phase2.NONE); + + assertEquals("", mTelephonyUtil.getGsmSimAuthResponse(invalidRequests, config)); } @Test @@ -353,8 +390,10 @@ public class TelephonyUtilTest extends WifiBaseTest { final String[] failedRequests = { "5E5F" }; when(mDataTelephonyManager.getIccAuthentication(anyInt(), anyInt(), eq(createSimChallengeRequest(new byte[] { 0x5e, 0x5f })))).thenReturn(null); + WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.SIM, WifiEnterpriseConfig.Phase2.NONE); - assertEquals(null, TelephonyUtil.getGsmSimAuthResponse(failedRequests, mTelephonyManager)); + assertEquals(null, mTelephonyUtil.getGsmSimAuthResponse(failedRequests, config)); } @Test @@ -369,16 +408,21 @@ public class TelephonyUtilTest extends WifiBaseTest { createSimChallengeRequest(new byte[] { 0x01, 0x22 }))) .thenReturn(createGsmSimAuthResponse(new byte[] { 0x11, 0x11 }, new byte[] { 0x12, 0x34 })); + WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.SIM, WifiEnterpriseConfig.Phase2.NONE); - assertEquals(":3b4a:1d2c:1234:1111", TelephonyUtil.getGsmSimAuthResponse( - new String[] { "1B2B", "0122" }, mTelephonyManager)); + assertEquals(":3b4a:1d2c:1234:1111", mTelephonyUtil.getGsmSimAuthResponse( + new String[] { "1B2B", "0122" }, config)); } @Test public void getGsmSimpleSimAuthResponseInvalidRequest() { final String[] invalidRequests = { null, "", "XXXX" }; + WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.SIM, WifiEnterpriseConfig.Phase2.NONE); + assertEquals("", - TelephonyUtil.getGsmSimpleSimAuthResponse(invalidRequests, mTelephonyManager)); + mTelephonyUtil.getGsmSimpleSimAuthResponse(invalidRequests, config)); } @Test @@ -386,9 +430,11 @@ public class TelephonyUtilTest extends WifiBaseTest { final String[] failedRequests = { "5E5F" }; when(mDataTelephonyManager.getIccAuthentication(anyInt(), anyInt(), eq(createSimChallengeRequest(new byte[] { 0x5e, 0x5f })))).thenReturn(null); + WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.SIM, WifiEnterpriseConfig.Phase2.NONE); assertEquals(null, - TelephonyUtil.getGsmSimpleSimAuthResponse(failedRequests, mTelephonyManager)); + mTelephonyUtil.getGsmSimpleSimAuthResponse(failedRequests, config)); } @Test @@ -403,16 +449,21 @@ public class TelephonyUtilTest extends WifiBaseTest { createSimChallengeRequest(new byte[] { 0x01, 0x23 }))) .thenReturn(createGsmSimAuthResponse(new byte[] { 0x33, 0x22 }, new byte[] { 0x11, 0x00 })); + WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.SIM, WifiEnterpriseConfig.Phase2.NONE); - assertEquals(":3b4a:1d2c:1100:3322", TelephonyUtil.getGsmSimpleSimAuthResponse( - new String[] { "1A2B", "0123" }, mTelephonyManager)); + assertEquals(":3b4a:1d2c:1100:3322", mTelephonyUtil.getGsmSimpleSimAuthResponse( + new String[] { "1A2B", "0123" }, config)); } @Test public void getGsmSimpleSimNoLengthAuthResponseInvalidRequest() { final String[] invalidRequests = { null, "", "XXXX" }; - assertEquals("", TelephonyUtil.getGsmSimpleSimNoLengthAuthResponse(invalidRequests, - mTelephonyManager)); + WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.SIM, WifiEnterpriseConfig.Phase2.NONE); + + assertEquals("", mTelephonyUtil.getGsmSimpleSimNoLengthAuthResponse(invalidRequests, + config)); } @Test @@ -420,9 +471,11 @@ public class TelephonyUtilTest extends WifiBaseTest { final String[] failedRequests = { "5E5F" }; when(mDataTelephonyManager.getIccAuthentication(anyInt(), anyInt(), eq(create2gUsimChallengeRequest(new byte[] { 0x5e, 0x5f })))).thenReturn(null); + WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.SIM, WifiEnterpriseConfig.Phase2.NONE); - assertEquals(null, TelephonyUtil.getGsmSimpleSimNoLengthAuthResponse(failedRequests, - mTelephonyManager)); + assertEquals(null, mTelephonyUtil.getGsmSimpleSimNoLengthAuthResponse(failedRequests, + config)); } @Test @@ -437,10 +490,12 @@ public class TelephonyUtilTest extends WifiBaseTest { create2gUsimChallengeRequest(new byte[] { 0x01, 0x23 }))) .thenReturn(create2gUsimAuthResponse(new byte[] { 0x12, 0x34, 0x56, 0x78 }, new byte[] { 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78 })); + WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.SIM, WifiEnterpriseConfig.Phase2.NONE); assertEquals(":1a2b3c4d5e6f7a1a:1a2b3c4d:1234567812345678:12345678", - TelephonyUtil.getGsmSimpleSimNoLengthAuthResponse( - new String[] { "1A2B", "0123" }, mTelephonyManager)); + mTelephonyUtil.getGsmSimpleSimNoLengthAuthResponse( + new String[] { "1A2B", "0123" }, config)); } /** @@ -479,11 +534,14 @@ public class TelephonyUtilTest extends WifiBaseTest { @Test public void get3GAuthResponseInvalidRequest() { - assertEquals(null, TelephonyUtil.get3GAuthResponse( - new SimAuthRequestData(0, 0, "SSID", new String[]{"0123"}), mTelephonyManager)); - assertEquals(null, TelephonyUtil.get3GAuthResponse( + WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.AKA, WifiEnterpriseConfig.Phase2.NONE); + + assertEquals(null, mTelephonyUtil.get3GAuthResponse( + new SimAuthRequestData(0, 0, "SSID", new String[]{"0123"}), config)); + assertEquals(null, mTelephonyUtil.get3GAuthResponse( new SimAuthRequestData(0, 0, "SSID", new String[]{"xyz2", "1234"}), - mTelephonyManager)); + config)); verifyNoMoreInteractions(mDataTelephonyManager); } @@ -491,10 +549,12 @@ public class TelephonyUtilTest extends WifiBaseTest { public void get3GAuthResponseNullIccAuthentication() { when(mDataTelephonyManager.getIccAuthentication(TelephonyManager.APPTYPE_USIM, TelephonyManager.AUTHTYPE_EAP_AKA, "AgEjAkVn")).thenReturn(null); - - SimAuthResponseData response = TelephonyUtil.get3GAuthResponse( + WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.AKA, WifiEnterpriseConfig.Phase2.NONE); + SimAuthResponseData response = mTelephonyUtil.get3GAuthResponse( new SimAuthRequestData(0, 0, "SSID", new String[]{"0123", "4567"}), - mTelephonyManager); + config); + assertNull(response); } @@ -503,10 +563,12 @@ public class TelephonyUtilTest extends WifiBaseTest { when(mDataTelephonyManager.getIccAuthentication(TelephonyManager.APPTYPE_USIM, TelephonyManager.AUTHTYPE_EAP_AKA, "AgEjAkVn")) .thenReturn(Base64.encodeToString(new byte[] {(byte) 0xdc}, Base64.NO_WRAP)); - - SimAuthResponseData response = TelephonyUtil.get3GAuthResponse( + WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.AKA, WifiEnterpriseConfig.Phase2.NONE); + SimAuthResponseData response = mTelephonyUtil.get3GAuthResponse( new SimAuthRequestData(0, 0, "SSID", new String[]{"0123", "4567"}), - mTelephonyManager); + config); + assertNull(response); } @@ -516,10 +578,12 @@ public class TelephonyUtilTest extends WifiBaseTest { TelephonyManager.AUTHTYPE_EAP_AKA, "AgEjAkVn")) .thenReturn(Base64.encodeToString(new byte[] {0x31, 0x1, 0x2, 0x3, 0x4}, Base64.NO_WRAP)); - - SimAuthResponseData response = TelephonyUtil.get3GAuthResponse( + WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.AKA, WifiEnterpriseConfig.Phase2.NONE); + SimAuthResponseData response = mTelephonyUtil.get3GAuthResponse( new SimAuthRequestData(0, 0, "SSID", new String[]{"0123", "4567"}), - mTelephonyManager); + config); + assertNull(response); } @@ -529,10 +593,12 @@ public class TelephonyUtilTest extends WifiBaseTest { TelephonyManager.AUTHTYPE_EAP_AKA, "AgEjAkVn")) .thenReturn(create3GSimAuthUmtsAuthResponse(new byte[] {0x11, 0x12}, new byte[] {0x21, 0x22, 0x23}, new byte[] {0x31})); - - SimAuthResponseData response = TelephonyUtil.get3GAuthResponse( + WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.AKA, WifiEnterpriseConfig.Phase2.NONE); + SimAuthResponseData response = mTelephonyUtil.get3GAuthResponse( new SimAuthRequestData(0, 0, "SSID", new String[]{"0123", "4567"}), - mTelephonyManager); + config); + assertNotNull(response); assertEquals("UMTS-AUTH", response.type); assertEquals(":31:212223:1112", response.response); @@ -543,10 +609,11 @@ public class TelephonyUtilTest extends WifiBaseTest { when(mDataTelephonyManager.getIccAuthentication(TelephonyManager.APPTYPE_USIM, TelephonyManager.AUTHTYPE_EAP_AKA, "AgEjAkVn")) .thenReturn(create3GSimAuthUmtsAutsResponse(new byte[] {0x22, 0x33})); - - SimAuthResponseData response = TelephonyUtil.get3GAuthResponse( + WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.AKA, WifiEnterpriseConfig.Phase2.NONE); + SimAuthResponseData response = mTelephonyUtil.get3GAuthResponse( new SimAuthRequestData(0, 0, "SSID", new String[]{"0123", "4567"}), - mTelephonyManager); + config); assertNotNull(response); assertEquals("UMTS-AUTS", response.type); assertEquals(":2233", response.response); @@ -561,9 +628,11 @@ public class TelephonyUtilTest extends WifiBaseTest { String expectedIdentity = "anonymous@wlan.mnc456.mcc123.3gppnetwork.org"; when(mDataTelephonyManager.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY); when(mDataTelephonyManager.getSimOperator()).thenReturn(mccmnc); + WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.AKA, WifiEnterpriseConfig.Phase2.NONE); assertEquals(expectedIdentity, - TelephonyUtil.getAnonymousIdentityWith3GppRealm(mTelephonyManager)); + mTelephonyUtil.getAnonymousIdentityWith3GppRealm(config)); } /** @@ -572,7 +641,104 @@ public class TelephonyUtilTest extends WifiBaseTest { @Test public void getAnonymousIdentityWithoutSim() { when(mDataTelephonyManager.getSimState()).thenReturn(TelephonyManager.SIM_STATE_ABSENT); + WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.AKA, WifiEnterpriseConfig.Phase2.NONE); + + assertNull(mTelephonyUtil.getAnonymousIdentityWith3GppRealm(config)); + } + + /** + * Verify SIM is present. + */ + @Test + public void isSimPresentWithValidSubscriptionIdList() { + when(mSubscriptionManager.getActiveSubscriptionIdList()) + .thenReturn(new int[] {DATA_SUBID, NON_DATA_SUBID}); + + assertTrue(mTelephonyUtil.isSimPresent(DATA_SUBID)); + } + + /** + * Verify SIM is not present. + */ + @Test + public void isSimPresentWithInvalidOrEmptySubscriptionIdList() { + when(mSubscriptionManager.getActiveSubscriptionIdList()) + .thenReturn(new int[]{}); + + assertFalse(mTelephonyUtil.isSimPresent(DATA_SUBID)); + + when(mSubscriptionManager.getActiveSubscriptionIdList()) + .thenReturn(new int[]{NON_DATA_SUBID}); + + assertFalse(mTelephonyUtil.isSimPresent(DATA_SUBID)); + } + + /** + * The active SubscriptionInfo List may be null or empty from Telephony. + */ + @Test + public void getBestMatchSubscriptionIdWithEmptyActiveSubscriptionInfoList() { + WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.AKA, WifiEnterpriseConfig.Phase2.NONE); + when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn(null); + + assertEquals(INVALID_SUBID, mTelephonyUtil.getBestMatchSubscriptionId(config)); + + when(mSubscriptionManager.getActiveSubscriptionInfoList()) + .thenReturn(Collections.emptyList()); + + assertEquals(INVALID_SUBID, mTelephonyUtil.getBestMatchSubscriptionId(config)); + } + + /** + * The matched Subscription ID should be that of data SIM when carrier ID is not specified. + */ + @Test + public void getBestMatchSubscriptionIdWithoutCarrierIdFieldForSimConfig() { + WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.AKA, WifiEnterpriseConfig.Phase2.NONE); + + assertEquals(DATA_SUBID, mTelephonyUtil.getBestMatchSubscriptionId(config)); + } + + /** + * The matched Subscription ID should be invalid if the configuration does not require + * SIM card and the carrier ID is not specified. + */ + @Test + public void getBestMatchSubscriptionIdWithoutCarrierIdFieldForNonSimConfig() { + WifiConfiguration config = new WifiConfiguration(); + + assertEquals(INVALID_SUBID, mTelephonyUtil.getBestMatchSubscriptionId(config)); + } + + /** + * If the carrier ID is specifed for EAP-SIM configuration, the corresponding Subscription ID + * should be returned. + */ + @Test + public void getBestMatchSubscriptionIdWithNonDataCarrierId() { + WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.AKA, WifiEnterpriseConfig.Phase2.NONE); + config.carrierId = NON_DATA_CARRIER_ID; + + assertEquals(NON_DATA_SUBID, mTelephonyUtil.getBestMatchSubscriptionId(config)); + + config.carrierId = DATA_CARRIER_ID; + assertEquals(DATA_SUBID, mTelephonyUtil.getBestMatchSubscriptionId(config)); + } + + /** + * The matched Subscription ID should be invalid if the SIM card for the specified carrier ID + * is absent. + */ + @Test + public void getBestMatchSubscriptionIdWithDeactiveCarrierId() { + WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork( + WifiEnterpriseConfig.Eap.AKA, WifiEnterpriseConfig.Phase2.NONE); + config.carrierId = DEACTIVE_CARRIER_ID; - assertNull(TelephonyUtil.getAnonymousIdentityWith3GppRealm(mTelephonyManager)); + assertEquals(INVALID_SUBID, mTelephonyUtil.getBestMatchSubscriptionId(config)); } } |