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 | |
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
12 files changed, 795 insertions, 292 deletions
diff --git a/service/java/com/android/server/wifi/ClientModeImpl.java b/service/java/com/android/server/wifi/ClientModeImpl.java index 720b3dccb..68dfe083e 100644 --- a/service/java/com/android/server/wifi/ClientModeImpl.java +++ b/service/java/com/android/server/wifi/ClientModeImpl.java @@ -89,7 +89,6 @@ import android.os.WorkSource; import android.provider.Settings; import android.system.OsConstants; import android.telephony.SubscriptionManager; -import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Log; import android.util.Pair; @@ -233,6 +232,8 @@ public class ClientModeImpl extends StateMachine { private int mLastSignalLevel = -1; private String mLastBssid; private int mLastNetworkId; // The network Id we successfully joined + // The subId used by WifiConfiguration with SIM credential which was connected successfully + private int mLastSubId; private boolean mIpReachabilityDisconnectEnabled = true; @@ -676,16 +677,10 @@ public class ClientModeImpl extends StateMachine { */ public static final WorkSource WIFI_WORK_SOURCE = new WorkSource(Process.WIFI_UID); - private TelephonyManager mTelephonyManager; - private TelephonyManager getTelephonyManager() { - if (mTelephonyManager == null) { - mTelephonyManager = mWifiInjector.makeTelephonyManager(); - } - return mTelephonyManager; - } - private final BatteryStatsManager mBatteryStatsManager; + private final TelephonyUtil mTelephonyUtil; + private final String mTcpBufferSizes; // Used for debug and stats gathering @@ -711,7 +706,8 @@ public class ClientModeImpl extends StateMachine { LinkProbeManager linkProbeManager, BatteryStatsManager batteryStatsManager, SupplicantStateTracker supplicantStateTracker, - MboOceController mboOceController) { + MboOceController mboOceController, + TelephonyUtil telephonyUtil) { super(TAG, looper); mWifiInjector = wifiInjector; mWifiMetrics = mWifiInjector.getWifiMetrics(); @@ -728,6 +724,7 @@ public class ClientModeImpl extends StateMachine { mWifiTrafficPoller = wifiTrafficPoller; mLinkProbeManager = linkProbeManager; mMboOceController = mboOceController; + mTelephonyUtil = telephonyUtil; mNetworkInfo = new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0, NETWORKTYPE, ""); mBatteryStatsManager = batteryStatsManager; @@ -759,6 +756,7 @@ public class ClientModeImpl extends StateMachine { mNetworkInfo.setIsAvailable(false); mLastBssid = null; mLastNetworkId = WifiConfiguration.INVALID_NETWORK_ID; + mLastSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; mLastSignalLevel = -1; mCountryCode = countryCode; @@ -1726,6 +1724,7 @@ public class ClientModeImpl extends StateMachine { pw.println("mLastSignalLevel " + mLastSignalLevel); pw.println("mLastBssid " + mLastBssid); pw.println("mLastNetworkId " + mLastNetworkId); + pw.println("mLastSubId " + mLastSubId); pw.println("mOperationalMode " + mOperationalMode); pw.println("mUserWantsSuspendOpt " + mUserWantsSuspendOpt); pw.println("mSuspendOptNeedsDisabled " + mSuspendOptNeedsDisabled); @@ -2597,6 +2596,7 @@ public class ClientModeImpl extends StateMachine { mLastLinkLayerStats = null; registerDisconnected(); mLastNetworkId = WifiConfiguration.INVALID_NETWORK_ID; + mLastSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; mWifiScoreCard.resetConnectionState(); updateL2KeyAndGroupHint(); } @@ -3294,6 +3294,7 @@ public class ClientModeImpl extends StateMachine { // Initialize data structures mLastBssid = null; mLastNetworkId = WifiConfiguration.INVALID_NETWORK_ID; + mLastSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; mLastSignalLevel = -1; mWifiInfo.setMacAddress(mWifiNative.getMacAddress(mInterfaceName)); // TODO: b/79504296 This broadcast has been deprecated and should be removed @@ -3635,12 +3636,12 @@ public class ClientModeImpl extends StateMachine { // For SIM & AKA/AKA' EAP method Only, get identity from ICC if (mTargetWifiConfiguration != null && mTargetWifiConfiguration.networkId == netId - && TelephonyUtil.isSimConfig(mTargetWifiConfiguration)) { + && mTargetWifiConfiguration.enterpriseConfig != null + && mTargetWifiConfiguration.enterpriseConfig + .requireSimCredential()) { // Pair<identity, encrypted identity> - Pair<String, String> identityPair = - TelephonyUtil.getSimIdentity(getTelephonyManager(), - new TelephonyUtil(), mTargetWifiConfiguration, - mWifiInjector.getCarrierNetworkConfig()); + Pair<String, String> identityPair = mTelephonyUtil + .getSimIdentity(mTargetWifiConfiguration); Log.i(TAG, "SUP_REQUEST_IDENTITY: identityPair=" + identityPair); if (identityPair != null && identityPair.first != null) { mWifiNative.simIdentityResponse(mInterfaceName, identityPair.first, @@ -3747,12 +3748,12 @@ public class ClientModeImpl extends StateMachine { Log.i(TAG, "Connecting with " + currentMacAddress + " as the mac address"); if (config.enterpriseConfig != null - && TelephonyUtil.isSimEapMethod(config.enterpriseConfig.getEapMethod()) + && config.enterpriseConfig.requireSimCredential() && mWifiInjector.getCarrierNetworkConfig() .isCarrierEncryptionInfoAvailable() && TextUtils.isEmpty(config.enterpriseConfig.getAnonymousIdentity())) { - String anonAtRealm = TelephonyUtil.getAnonymousIdentityWith3GppRealm( - getTelephonyManager()); + String anonAtRealm = mTelephonyUtil + .getAnonymousIdentityWith3GppRealm(config); // Use anonymous@<realm> when pseudonym is not available config.enterpriseConfig.setAnonymousIdentity(anonAtRealm); } @@ -3873,15 +3874,15 @@ public class ClientModeImpl extends StateMachine { // We need to get the updated pseudonym from supplicant for EAP-SIM/AKA/AKA' if (config.enterpriseConfig != null - && TelephonyUtil.isSimEapMethod( - config.enterpriseConfig.getEapMethod())) { + && config.enterpriseConfig.requireSimCredential()) { + mLastSubId = mTelephonyUtil.getBestMatchSubscriptionId(config); String anonymousIdentity = mWifiNative.getEapAnonymousIdentity(mInterfaceName); if (!TextUtils.isEmpty(anonymousIdentity) && !TelephonyUtil .isAnonymousAtRealmIdentity(anonymousIdentity)) { - String decoratedPseudonym = TelephonyUtil - .decoratePseudonymWith3GppRealm(getTelephonyManager(), + String decoratedPseudonym = mTelephonyUtil + .decoratePseudonymWith3GppRealm(config, anonymousIdentity); if (decoratedPseudonym != null) { anonymousIdentity = decoratedPseudonym; @@ -4098,10 +4099,7 @@ public class ClientModeImpl extends StateMachine { case WifiEnterpriseConfig.Eap.AKA: case WifiEnterpriseConfig.Eap.AKA_PRIME: if (errorCode == WifiNative.EAP_SIM_VENDOR_SPECIFIC_CERT_EXPIRED) { - getTelephonyManager() - .createForSubscriptionId( - SubscriptionManager.getDefaultDataSubscriptionId()) - .resetCarrierKeysForImsiEncryption(); + mTelephonyUtil.resetCarrierKeysForImsiEncryption(targetedNetwork); } break; @@ -4527,11 +4525,14 @@ public class ClientModeImpl extends StateMachine { && mLastNetworkId != WifiConfiguration.INVALID_NETWORK_ID) { WifiConfiguration config = mWifiConfigManager.getConfiguredNetwork(mLastNetworkId); - if (TelephonyUtil.isSimConfig(config)) { + if (config.enterpriseConfig != null + && config.enterpriseConfig.requireSimCredential() + && !mTelephonyUtil.isSimPresent(mLastSubId)) { + // check if the removed sim card is associated with current config mWifiMetrics.logStaEvent(StaEvent.TYPE_FRAMEWORK_DISCONNECT, StaEvent.DISCONNECT_RESET_SIM_NETWORKS); - // TODO(b/132385576): STA may immediately connect back to the network - // that we just disconnected from + // TODO(b/132385576): STA may immediately connect back to the + // network that we just disconnected from mWifiNative.disconnect(mInterfaceName); transitionTo(mDisconnectingState); } @@ -5358,8 +5359,8 @@ public class ClientModeImpl extends StateMachine { } void handleGsmAuthRequest(SimAuthRequestData requestData) { - if (mTargetWifiConfiguration == null - || mTargetWifiConfiguration.networkId + if (mTargetWifiConfiguration != null + && mTargetWifiConfiguration.networkId == requestData.networkId) { logd("id matches targetWifiConfiguration"); } else { @@ -5379,16 +5380,16 @@ public class ClientModeImpl extends StateMachine { * 3. 3GPP TS 11.11 2G_authentication [RAND] * [SRES][Cipher Key Kc] */ - String response = - TelephonyUtil.getGsmSimAuthResponse(requestData.data, getTelephonyManager()); + String response = mTelephonyUtil + .getGsmSimAuthResponse(requestData.data, mTargetWifiConfiguration); if (response == null) { // In case of failure, issue may be due to sim type, retry as No.2 case - response = TelephonyUtil.getGsmSimpleSimAuthResponse(requestData.data, - getTelephonyManager()); + response = mTelephonyUtil + .getGsmSimpleSimAuthResponse(requestData.data, mTargetWifiConfiguration); if (response == null) { // In case of failure, issue may be due to sim type, retry as No.3 case - response = TelephonyUtil.getGsmSimpleSimNoLengthAuthResponse(requestData.data, - getTelephonyManager()); + response = mTelephonyUtil.getGsmSimpleSimNoLengthAuthResponse( + requestData.data, mTargetWifiConfiguration); } } if (response == null || response.length() == 0) { @@ -5401,8 +5402,8 @@ public class ClientModeImpl extends StateMachine { } void handle3GAuthRequest(SimAuthRequestData requestData) { - if (mTargetWifiConfiguration == null - || mTargetWifiConfiguration.networkId + if (mTargetWifiConfiguration != null + && mTargetWifiConfiguration.networkId == requestData.networkId) { logd("id matches targetWifiConfiguration"); } else { @@ -5410,8 +5411,8 @@ public class ClientModeImpl extends StateMachine { return; } - SimAuthResponseData response = - TelephonyUtil.get3GAuthResponse(requestData, getTelephonyManager()); + SimAuthResponseData response = mTelephonyUtil + .get3GAuthResponse(requestData, mTargetWifiConfiguration); if (response != null) { mWifiNative.simAuthResponse( mInterfaceName, response.type, response.response); diff --git a/service/java/com/android/server/wifi/SavedNetworkEvaluator.java b/service/java/com/android/server/wifi/SavedNetworkEvaluator.java index f4635ab68..0437dc49a 100644 --- a/service/java/com/android/server/wifi/SavedNetworkEvaluator.java +++ b/service/java/com/android/server/wifi/SavedNetworkEvaluator.java @@ -20,7 +20,6 @@ import android.annotation.NonNull; import android.content.Context; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; -import android.telephony.SubscriptionManager; import android.util.LocalLog; import com.android.internal.annotations.VisibleForTesting; @@ -39,7 +38,7 @@ public class SavedNetworkEvaluator implements WifiNetworkSelector.NetworkEvaluat private final Clock mClock; private final LocalLog mLocalLog; private final WifiConnectivityHelper mConnectivityHelper; - private final SubscriptionManager mSubscriptionManager; + private final TelephonyUtil mTelephonyUtil; private final int mRssiScoreSlope; private final int mRssiScoreOffset; private final int mSameBssidAward; @@ -59,13 +58,13 @@ public class SavedNetworkEvaluator implements WifiNetworkSelector.NetworkEvaluat SavedNetworkEvaluator(final Context context, ScoringParams scoringParams, WifiConfigManager configManager, Clock clock, LocalLog localLog, WifiConnectivityHelper connectivityHelper, - SubscriptionManager subscriptionManager) { + TelephonyUtil telephonyUtil) { mScoringParams = scoringParams; mWifiConfigManager = configManager; mClock = clock; mLocalLog = localLog; mConnectivityHelper = connectivityHelper; - mSubscriptionManager = subscriptionManager; + mTelephonyUtil = telephonyUtil; mRssiScoreSlope = context.getResources().getInteger( R.integer.config_wifi_framework_RSSI_SCORE_SLOPE); @@ -235,10 +234,15 @@ public class SavedNetworkEvaluator implements WifiNetworkSelector.NetworkEvaluat + " has specified BSSID " + network.BSSID + ". Skip " + scanResult.BSSID); continue; - } else if (TelephonyUtil.isSimConfig(network) - && !TelephonyUtil.isSimPresent(mSubscriptionManager)) { - // Don't select if security type is EAP SIM/AKA/AKA' when SIM is not present. - continue; + } else if (network.enterpriseConfig != null + && network.enterpriseConfig.requireSimCredential()) { + int subId = mTelephonyUtil.getBestMatchSubscriptionId(network); + if (!mTelephonyUtil.isSimPresent(subId)) { + // Don't select if security type is EAP SIM/AKA/AKA' when SIM is not present. + localLog("No SIM card is good for Network " + + WifiNetworkSelector.toNetworkString(network)); + continue; + } } int score = calculateBssidScore(scanResult, network, currentNetwork, currentBssid, diff --git a/service/java/com/android/server/wifi/WifiConfigManager.java b/service/java/com/android/server/wifi/WifiConfigManager.java index 657e05708..958a9f1b0 100644 --- a/service/java/com/android/server/wifi/WifiConfigManager.java +++ b/service/java/com/android/server/wifi/WifiConfigManager.java @@ -41,7 +41,6 @@ import android.os.Process; import android.os.UserHandle; import android.os.UserManager; import android.provider.Settings; -import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.ArraySet; import android.util.LocalLog; @@ -275,7 +274,6 @@ public class WifiConfigManager { private final Clock mClock; private final UserManager mUserManager; private final BackupManagerProxy mBackupManagerProxy; - private final TelephonyManager mTelephonyManager; private final WifiKeyStore mWifiKeyStore; private final WifiConfigStore mWifiConfigStore; private final WifiPermissionsUtil mWifiPermissionsUtil; @@ -284,6 +282,7 @@ public class WifiConfigManager { private final MacAddressUtil mMacAddressUtil; private boolean mConnectedMacRandomzationSupported; private final Mac mMac; + private final TelephonyUtil mTelephonyUtil; /** * Local log used for debugging any WifiConfigManager issues. @@ -394,7 +393,7 @@ public class WifiConfigManager { */ WifiConfigManager( Context context, Clock clock, UserManager userManager, - TelephonyManager telephonyManager, WifiKeyStore wifiKeyStore, + TelephonyUtil telephonyUtil, WifiKeyStore wifiKeyStore, WifiConfigStore wifiConfigStore, WifiPermissionsUtil wifiPermissionsUtil, WifiPermissionsWrapper wifiPermissionsWrapper, @@ -409,7 +408,7 @@ public class WifiConfigManager { mClock = clock; mUserManager = userManager; mBackupManagerProxy = new BackupManagerProxy(); - mTelephonyManager = telephonyManager; + mTelephonyUtil = telephonyUtil; mWifiKeyStore = wifiKeyStore; mWifiConfigStore = wifiConfigStore; mWifiPermissionsUtil = wifiPermissionsUtil; @@ -1148,6 +1147,7 @@ public class WifiConfigManager { // Copy over macRandomizationSetting internalConfig.macRandomizationSetting = externalConfig.macRandomizationSetting; + internalConfig.carrierId = externalConfig.carrierId; } /** @@ -2978,13 +2978,13 @@ public class WifiConfigManager { public void resetSimNetworks() { if (mVerboseLoggingEnabled) localLog("resetSimNetworks"); for (WifiConfiguration config : getInternalConfiguredNetworks()) { - if (!TelephonyUtil.isSimConfig(config)) { + if (config.enterpriseConfig == null + || !config.enterpriseConfig.requireSimCredential()) { continue; } if (config.enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.PEAP) { - Pair<String, String> currentIdentity = TelephonyUtil.getSimIdentity( - mTelephonyManager, new TelephonyUtil(), config, - mWifiInjector.getCarrierNetworkConfig()); + Pair<String, String> currentIdentity = + mTelephonyUtil.getSimIdentity(config); if (mVerboseLoggingEnabled) { Log.d(TAG, "New identity for config " + config + ": " + currentIdentity); } diff --git a/service/java/com/android/server/wifi/WifiInjector.java b/service/java/com/android/server/wifi/WifiInjector.java index 13f6a8267..89151ea3e 100644 --- a/service/java/com/android/server/wifi/WifiInjector.java +++ b/service/java/com/android/server/wifi/WifiInjector.java @@ -56,6 +56,7 @@ import com.android.server.wifi.p2p.WifiP2pMetrics; import com.android.server.wifi.p2p.WifiP2pMonitor; import com.android.server.wifi.p2p.WifiP2pNative; import com.android.server.wifi.rtt.RttMetrics; +import com.android.server.wifi.util.TelephonyUtil; import com.android.server.wifi.util.WifiPermissionsUtil; import com.android.server.wifi.util.WifiPermissionsWrapper; import com.android.server.wifi.wificond.IWificond; @@ -154,6 +155,7 @@ public class WifiInjector { private BssidBlocklistMonitor mBssidBlocklistMonitor; private final MacAddressUtil mMacAddressUtil; private final MboOceController mMboOceController; + private final TelephonyUtil mTelephonyUtil; public WifiInjector(Context context) { if (context == null) { @@ -245,9 +247,10 @@ public class WifiInjector { WifiConfigStore.createSharedFile(mFrameworkFacade.isNiapModeOn(mContext))); SubscriptionManager subscriptionManager = mContext.getSystemService(SubscriptionManager.class); + mTelephonyUtil = new TelephonyUtil(makeTelephonyManager(), subscriptionManager); // Config Manager mWifiConfigManager = new WifiConfigManager(mContext, mClock, - mUserManager, makeTelephonyManager(), + mUserManager, mTelephonyUtil, mWifiKeyStore, mWifiConfigStore, mWifiPermissionsUtil, mWifiPermissionsWrapper, this, new NetworkListSharedStoreData(mContext), new NetworkListUserStoreData(mContext), @@ -270,9 +273,10 @@ public class WifiInjector { mWifiMetrics.setWifiNetworkSelector(mWifiNetworkSelector); mSavedNetworkEvaluator = new SavedNetworkEvaluator(mContext, mScoringParams, mWifiConfigManager, mClock, mConnectivityLocalLog, mWifiConnectivityHelper, - subscriptionManager); + mTelephonyUtil); mWifiNetworkSuggestionsManager = new WifiNetworkSuggestionsManager(mContext, wifiHandler, - this, mWifiPermissionsUtil, mWifiConfigManager, mWifiConfigStore, mWifiMetrics); + this, mWifiPermissionsUtil, mWifiConfigManager, mWifiConfigStore, mWifiMetrics, + mTelephonyUtil); mNetworkSuggestionEvaluator = new NetworkSuggestionEvaluator(mWifiNetworkSuggestionsManager, mWifiConfigManager, mConnectivityLocalLog); mScoredNetworkEvaluator = new ScoredNetworkEvaluator(context, wifiHandler, @@ -313,7 +317,7 @@ public class WifiInjector { this, mBackupManagerProxy, mCountryCode, mWifiNative, new WrongPasswordNotifier(mContext, mFrameworkFacade), mSarManager, mWifiTrafficPoller, mLinkProbeManager, mBatteryStats, - supplicantStateTracker, mMboOceController); + supplicantStateTracker, mMboOceController, mTelephonyUtil); mActiveModeWarden = new ActiveModeWarden(this, wifiLooper, mWifiNative, new DefaultModeManager(mContext), mBatteryStats, mWifiDiagnostics, mContext, mClientModeImpl, mSettingsStore, mFrameworkFacade, mWifiPermissionsUtil); @@ -502,6 +506,10 @@ public class WifiInjector { return (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); } + public TelephonyUtil getTelephonyUtil() { + return mTelephonyUtil; + } + public WifiStateTracker getWifiStateTracker() { return mWifiStateTracker; } diff --git a/service/java/com/android/server/wifi/WifiNetworkSuggestionsManager.java b/service/java/com/android/server/wifi/WifiNetworkSuggestionsManager.java index b94f91da4..0d400c576 100644 --- a/service/java/com/android/server/wifi/WifiNetworkSuggestionsManager.java +++ b/service/java/com/android/server/wifi/WifiNetworkSuggestionsManager.java @@ -52,6 +52,7 @@ import com.android.internal.annotations.VisibleForTesting; import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; import com.android.internal.notification.SystemNotificationChannels; import com.android.server.wifi.util.ExternalCallbackTracker; +import com.android.server.wifi.util.TelephonyUtil; import com.android.server.wifi.util.WifiPermissionsUtil; import com.android.wifi.R; @@ -113,6 +114,7 @@ public class WifiNetworkSuggestionsManager { private final WifiMetrics mWifiMetrics; private final WifiInjector mWifiInjector; private final FrameworkFacade mFrameworkFacade; + private final TelephonyUtil mTelephonyUtil; /** * Per app meta data to store network suggestions, status, etc for each app providing network @@ -406,7 +408,8 @@ public class WifiNetworkSuggestionsManager { WifiPermissionsUtil wifiPermissionsUtil, WifiConfigManager wifiConfigManager, WifiConfigStore wifiConfigStore, - WifiMetrics wifiMetrics) { + WifiMetrics wifiMetrics, + TelephonyUtil telephonyUtil) { mContext = context; mResources = context.getResources(); mHandler = handler; @@ -419,6 +422,7 @@ public class WifiNetworkSuggestionsManager { mWifiPermissionsUtil = wifiPermissionsUtil; mWifiConfigManager = wifiConfigManager; mWifiMetrics = wifiMetrics; + mTelephonyUtil = telephonyUtil; // register the data store for serializing/deserializing data. wifiConfigStore.registerStoreData( @@ -1000,7 +1004,23 @@ public class WifiNetworkSuggestionsManager { Set<ExtendedWifiNetworkSuggestion> approvedExtNetworkSuggestions = extNetworkSuggestions .stream() - .filter(n -> n.perAppInfo.hasUserApproved) + .filter(n -> { + if (!n.perAppInfo.hasUserApproved) { + return false; + } + WifiConfiguration config = n.wns.wifiConfiguration; + if (config != null && config.enterpriseConfig != null + && config.enterpriseConfig.requireSimCredential()) { + int subId = mTelephonyUtil.getBestMatchSubscriptionId(config); + if (!mTelephonyUtil.isSimPresent(subId)) { + if (mVerboseLoggingEnabled) { + Log.v(TAG, "No SIM is matched, ignore the config."); + } + return false; + } + } + return true; + }) .collect(Collectors.toSet()); // If there is no active notification, check if we need to get approval for any of the apps // & send a notification for one of them. If there are multiple packages awaiting approval, diff --git a/service/java/com/android/server/wifi/util/TelephonyUtil.java b/service/java/com/android/server/wifi/util/TelephonyUtil.java index a80c18cf6..60921725e 100644 --- a/service/java/com/android/server/wifi/util/TelephonyUtil.java +++ b/service/java/com/android/server/wifi/util/TelephonyUtil.java @@ -20,6 +20,7 @@ import android.annotation.NonNull; 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.text.TextUtils; @@ -28,13 +29,14 @@ import android.util.Log; import android.util.Pair; import com.android.internal.annotations.VisibleForTesting; -import com.android.server.wifi.CarrierNetworkConfig; import com.android.server.wifi.WifiNative; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.PublicKey; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; import javax.annotation.Nonnull; import javax.crypto.BadPaddingException; @@ -78,34 +80,83 @@ public class TelephonyUtil { private static final int START_KC_POS = START_SRES_POS + SRES_LEN; private static final int KC_LEN = 8; + private final TelephonyManager mTelephonyManager; + private final SubscriptionManager mSubscriptionManager; + + /** + * Gets the instance of TelephonyUtil. + * @param telephonyManager Instance of {@link TelephonyManager} + * @param subscriptionManager Instance of {@link SubscriptionManager} + * @return The instance of TelephonyUtil + */ + public TelephonyUtil(@NonNull TelephonyManager telephonyManager, + @NonNull SubscriptionManager subscriptionManager) { + mTelephonyManager = telephonyManager; + mSubscriptionManager = subscriptionManager; + } + + /** + * Gets the SubscriptionId of SIM card which is from the carrier specified in config. + * @param config the instance of {@link WifiConfiguration} + * @return the best match SubscriptionId + */ + public int getBestMatchSubscriptionId(WifiConfiguration config) { + List<SubscriptionInfo> subInfoList = mSubscriptionManager.getActiveSubscriptionInfoList(); + if (subInfoList == null || subInfoList.isEmpty()) { + return SubscriptionManager.INVALID_SUBSCRIPTION_ID; + } + // Legacy WifiConfiguration without carrier ID + if (config.carrierId == TelephonyManager.UNKNOWN_CARRIER_ID + && config.enterpriseConfig != null + && config.enterpriseConfig.requireSimCredential()) { + Log.d(TAG, "carrierId is not assigned, using the default data sub."); + return SubscriptionManager.getDefaultDataSubscriptionId(); + } + + int dataSubId = SubscriptionManager.getDefaultDataSubscriptionId(); + int matchSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; + for (SubscriptionInfo subInfo : subInfoList) { + if (subInfo.getCarrierId() == config.carrierId) { + matchSubId = subInfo.getSubscriptionId(); + if (matchSubId == dataSubId) { + // Priority of Data sub is higher than non data sub. + break; + } + } + } + Log.d(TAG, "best match subscription id: " + matchSubId); + return matchSubId; + } + + /** + * Check if the specified SIM card is in the device. + * + * @param subId subscription ID of SIM card in the device. + * @return true if the subId is active, otherwise false. + */ + public boolean isSimPresent(int subId) { + return Arrays.stream(mSubscriptionManager.getActiveSubscriptionIdList()) + .anyMatch(id -> id == subId); + } /** * Get the identity for the current SIM or null if the SIM is not available * - * @param tm TelephonyManager instance * @param config WifiConfiguration that indicates what sort of authentication is necessary - * @param telephonyUtil TelephonyUtil instance - * @param carrierNetworkConfig CarrierNetworkConfig instance * @return Pair<identify, encrypted identity> or null if the SIM is not available * or config is invalid */ - public static Pair<String, String> getSimIdentity(TelephonyManager tm, - TelephonyUtil telephonyUtil, - WifiConfiguration config, CarrierNetworkConfig carrierNetworkConfig) { - if (tm == null) { - Log.e(TAG, "No valid TelephonyManager"); - return null; - } - TelephonyManager defaultDataTm = tm.createForSubscriptionId( - SubscriptionManager.getDefaultDataSubscriptionId()); - if (carrierNetworkConfig == null) { - Log.e(TAG, "No valid CarrierNetworkConfig"); + public Pair<String, String> getSimIdentity(WifiConfiguration config) { + int subId = getBestMatchSubscriptionId(config); + if (!SubscriptionManager.isValidSubscriptionId(subId)) { return null; } - String imsi = defaultDataTm.getSubscriberId(); + + TelephonyManager specifiedTm = mTelephonyManager.createForSubscriptionId(subId); + String imsi = specifiedTm.getSubscriberId(); String mccMnc = ""; - if (defaultDataTm.getSimState() == TelephonyManager.SIM_STATE_READY) { - mccMnc = defaultDataTm.getSimOperator(); + if (specifiedTm.getSimState() == TelephonyManager.SIM_STATE_READY) { + mccMnc = specifiedTm.getSimOperator(); } String identity = buildIdentity(getSimMethodForConfig(config), imsi, mccMnc, false); @@ -116,7 +167,7 @@ public class TelephonyUtil { ImsiEncryptionInfo imsiEncryptionInfo; try { - imsiEncryptionInfo = defaultDataTm.getCarrierInfoForImsiEncryption( + imsiEncryptionInfo = specifiedTm.getCarrierInfoForImsiEncryption( TelephonyManager.KEY_TYPE_WLAN); } catch (RuntimeException e) { Log.e(TAG, "Failed to get imsi encryption info: " + e.getMessage()); @@ -127,7 +178,7 @@ public class TelephonyUtil { return Pair.create(identity, ""); } - String encryptedIdentity = buildEncryptedIdentity(telephonyUtil, identity, + String encryptedIdentity = buildEncryptedIdentity(identity, imsiEncryptionInfo); // In case of failure for encryption, abort current EAP authentication. @@ -141,20 +192,17 @@ public class TelephonyUtil { /** * Gets Anonymous identity for current active SIM. * - * @param tm TelephonyManager instance + * @param config the instance of WifiConfiguration. * @return anonymous identity@realm which is based on current MCC/MNC, {@code null} if SIM is * not ready or absent. */ - public static String getAnonymousIdentityWith3GppRealm(@Nonnull TelephonyManager tm) { - if (tm == null) { + public String getAnonymousIdentityWith3GppRealm(@NonNull WifiConfiguration config) { + int subId = getBestMatchSubscriptionId(config); + TelephonyManager specifiedTm = mTelephonyManager.createForSubscriptionId(subId); + if (specifiedTm.getSimState() != TelephonyManager.SIM_STATE_READY) { return null; } - TelephonyManager defaultDataTm = tm.createForSubscriptionId( - SubscriptionManager.getDefaultDataSubscriptionId()); - if (defaultDataTm.getSimState() != TelephonyManager.SIM_STATE_READY) { - return null; - } - String mccMnc = defaultDataTm.getSimOperator(); + String mccMnc = specifiedTm.getSimOperator(); if (mccMnc == null || mccMnc.isEmpty()) { return null; } @@ -176,11 +224,12 @@ public class TelephonyUtil { * a Base64 encoded string. * * @param key The public key to use for encryption + * @param data The data need to be encrypted * @param encodingFlag base64 encoding flag * @return Base64 encoded string, or null if encryption failed */ @VisibleForTesting - public String encryptDataUsingPublicKey(PublicKey key, byte[] data, int encodingFlag) { + public static String encryptDataUsingPublicKey(PublicKey key, byte[] data, int encodingFlag) { try { Cipher cipher = Cipher.getInstance(IMSI_CIPHER_TRANSFORMATION); cipher.init(Cipher.ENCRYPT_MODE, key); @@ -202,13 +251,12 @@ public class TelephonyUtil { * "1" - EAP-SIM Identity * "6" - EAP-AKA' Identity * Encrypted identity format: prefix|IMSI@<NAIRealm> - * @param telephonyUtil TelephonyUtil instance * @param identity permanent identity with format based on section 4.1.1.6 of RFC 4187 * and 4.2.1.6 of RFC 4186. * @param imsiEncryptionInfo The IMSI encryption info retrieved from the SIM * @return "\0" + encryptedIdentity + "{, Key Identifier AVP}" */ - private static String buildEncryptedIdentity(TelephonyUtil telephonyUtil, String identity, + private static String buildEncryptedIdentity(String identity, ImsiEncryptionInfo imsiEncryptionInfo) { if (imsiEncryptionInfo == null) { Log.e(TAG, "imsiEncryptionInfo is not valid"); @@ -220,7 +268,7 @@ public class TelephonyUtil { } // Build and return the encrypted identity. - String encryptedIdentity = telephonyUtil.encryptDataUsingPublicKey( + String encryptedIdentity = encryptDataUsingPublicKey( imsiEncryptionInfo.getPublicKey(), identity.getBytes(), Base64.NO_WRAP); if (encryptedIdentity == null) { Log.e(TAG, "Failed to encrypt IMSI"); @@ -324,16 +372,6 @@ public class TelephonyUtil { } /** - * Checks if the network is a SIM config. - * - * @param config Config corresponding to the network. - * @return true if it is a SIM config, false otherwise. - */ - public static boolean isSimConfig(WifiConfiguration config) { - return getSimMethodForConfig(config) != WifiEnterpriseConfig.Eap.NONE; - } - - /** * Returns true if {@code identity} contains an anonymous@realm identity, false otherwise. */ public static boolean isAnonymousAtRealmIdentity(String identity) { @@ -448,12 +486,12 @@ public class TelephonyUtil { * [Length][RES][Length][CK][Length][IK] and more * * @param requestData RAND data from server. - * @param tm the instance of TelephonyManager. + * @param config The instance of WifiConfiguration. * @return the response data processed by SIM. If all request data is malformed, then returns * empty string. If request data is invalid, then returns null. */ - public static String getGsmSimAuthResponse(String[] requestData, TelephonyManager tm) { - return getGsmAuthResponseWithLength(requestData, tm, TelephonyManager.APPTYPE_USIM); + public String getGsmSimAuthResponse(String[] requestData, WifiConfiguration config) { + return getGsmAuthResponseWithLength(requestData, config, TelephonyManager.APPTYPE_USIM); } /** @@ -465,22 +503,22 @@ public class TelephonyUtil { * [Length][SRES][Length][Cipher Key Kc] * * @param requestData RAND data from server. - * @param tm the instance of TelephonyManager. + * @param config The instance of WifiConfiguration. * @return the response data processed by SIM. If all request data is malformed, then returns * empty string. If request data is invalid, then returns null. */ - public static String getGsmSimpleSimAuthResponse(String[] requestData, TelephonyManager tm) { - return getGsmAuthResponseWithLength(requestData, tm, TelephonyManager.APPTYPE_SIM); + public String getGsmSimpleSimAuthResponse(String[] requestData, + WifiConfiguration config) { + return getGsmAuthResponseWithLength(requestData, config, TelephonyManager.APPTYPE_SIM); } - private static String getGsmAuthResponseWithLength(String[] requestData, TelephonyManager tm, - int appType) { - if (tm == null) { - Log.e(TAG, "No valid TelephonyManager"); + private String getGsmAuthResponseWithLength(String[] requestData, + WifiConfiguration config, int appType) { + int subId = getBestMatchSubscriptionId(config); + if (!SubscriptionManager.isValidSubscriptionId(subId)) { return null; } - TelephonyManager defaultDataTm = tm.createForSubscriptionId( - SubscriptionManager.getDefaultDataSubscriptionId()); + StringBuilder sb = new StringBuilder(); for (String challenge : requestData) { if (challenge == null || challenge.isEmpty()) { @@ -497,8 +535,8 @@ public class TelephonyUtil { } String base64Challenge = Base64.encodeToString(rand, Base64.NO_WRAP); - - String tmResponse = defaultDataTm.getIccAuthentication( + TelephonyManager specifiedTm = mTelephonyManager.createForSubscriptionId(subId); + String tmResponse = specifiedTm.getIccAuthentication( appType, TelephonyManager.AUTHTYPE_EAP_SIM, base64Challenge); Log.v(TAG, "Raw Response - " + tmResponse); @@ -542,18 +580,18 @@ public class TelephonyUtil { * [SRES][Cipher Key Kc] * * @param requestData RAND data from server. - * @param tm the instance of TelephonyManager. + * @param config the instance of WifiConfiguration. * @return the response data processed by SIM. If all request data is malformed, then returns * empty string. If request data is invalid, then returns null. */ - public static String getGsmSimpleSimNoLengthAuthResponse(String[] requestData, - TelephonyManager tm) { - if (tm == null) { - Log.e(TAG, "No valid TelephonyManager"); + public String getGsmSimpleSimNoLengthAuthResponse(String[] requestData, + @NonNull WifiConfiguration config) { + + int subId = getBestMatchSubscriptionId(config); + if (!SubscriptionManager.isValidSubscriptionId(subId)) { return null; } - TelephonyManager defaultDataTm = tm.createForSubscriptionId( - SubscriptionManager.getDefaultDataSubscriptionId()); + StringBuilder sb = new StringBuilder(); for (String challenge : requestData) { if (challenge == null || challenge.isEmpty()) { @@ -570,8 +608,8 @@ public class TelephonyUtil { } String base64Challenge = Base64.encodeToString(rand, Base64.NO_WRAP); - - String tmResponse = defaultDataTm.getIccAuthentication(TelephonyManager.APPTYPE_SIM, + TelephonyManager specifiedTm = mTelephonyManager.createForSubscriptionId(subId); + String tmResponse = specifiedTm.getIccAuthentication(TelephonyManager.APPTYPE_SIM, TelephonyManager.AUTHTYPE_EAP_SIM, base64Challenge); Log.v(TAG, "Raw Response - " + tmResponse); @@ -628,8 +666,15 @@ public class TelephonyUtil { public String response; } - public static SimAuthResponseData get3GAuthResponse(SimAuthRequestData requestData, - TelephonyManager tm) { + /** + * Get the response data for 3G authentication. + * + * @param requestData authentication request data from server. + * @param config the instance of WifiConfiguration. + * @return the response data processed by SIM. If request data is invalid, then returns null. + */ + public SimAuthResponseData get3GAuthResponse(SimAuthRequestData requestData, + WifiConfiguration config) { StringBuilder sb = new StringBuilder(); byte[] rand = null; byte[] authn = null; @@ -649,15 +694,15 @@ public class TelephonyUtil { String tmResponse = ""; if (rand != null && authn != null) { String base64Challenge = Base64.encodeToString(concatHex(rand, authn), Base64.NO_WRAP); - if (tm != null) { - tmResponse = tm - .createForSubscriptionId(SubscriptionManager.getDefaultDataSubscriptionId()) - .getIccAuthentication(TelephonyManager.APPTYPE_USIM, - TelephonyManager.AUTHTYPE_EAP_AKA, base64Challenge); - Log.v(TAG, "Raw Response - " + tmResponse); - } else { - Log.e(TAG, "No valid TelephonyManager"); + int subId = getBestMatchSubscriptionId(config); + if (!SubscriptionManager.isValidSubscriptionId(subId)) { + return null; } + tmResponse = mTelephonyManager + .createForSubscriptionId(subId) + .getIccAuthentication(TelephonyManager.APPTYPE_USIM, + TelephonyManager.AUTHTYPE_EAP_AKA, base64Challenge); + Log.v(TAG, "Raw Response - " + tmResponse); } boolean goodReponse = false; @@ -735,26 +780,27 @@ public class TelephonyUtil { /** * Decorates a pseudonym with the NAI realm, in case it wasn't provided by the server * - * @param tm TelephonyManager instance + * @param config The instance of WifiConfiguration * @param pseudonym The pseudonym (temporary identity) provided by the server * @return pseudonym@realm which is based on current MCC/MNC, {@code null} if SIM is * not ready or absent. */ - public static String decoratePseudonymWith3GppRealm(@NonNull TelephonyManager tm, + public String decoratePseudonymWith3GppRealm(@NonNull WifiConfiguration config, String pseudonym) { - if (tm == null || TextUtils.isEmpty(pseudonym)) { + if (TextUtils.isEmpty(pseudonym)) { return null; } if (pseudonym.contains("@")) { // Pseudonym is already decorated return pseudonym; } - TelephonyManager defaultDataTm = tm.createForSubscriptionId( - SubscriptionManager.getDefaultDataSubscriptionId()); - if (defaultDataTm.getSimState() != TelephonyManager.SIM_STATE_READY) { + int subId = getBestMatchSubscriptionId(config); + + TelephonyManager specifiedTm = mTelephonyManager.createForSubscriptionId(subId); + if (specifiedTm.getSimState() != TelephonyManager.SIM_STATE_READY) { return null; } - String mccMnc = defaultDataTm.getSimOperator(); + String mccMnc = specifiedTm.getSimOperator(); if (mccMnc == null || mccMnc.isEmpty()) { return null; } @@ -770,4 +816,17 @@ public class TelephonyUtil { String realm = String.format(THREE_GPP_NAI_REALM_FORMAT, mnc, mcc); return String.format("%s@%s", pseudonym, realm); } + + /** + * Reset the downloaded IMSI encryption key. + * @param config Instance of WifiConfiguration + */ + public void resetCarrierKeysForImsiEncryption(@NonNull WifiConfiguration config) { + int subId = getBestMatchSubscriptionId(config); + if (!SubscriptionManager.isValidSubscriptionId(subId)) { + return; + } + TelephonyManager specifiedTm = mTelephonyManager.createForSubscriptionId(subId); + specifiedTm.resetCarrierKeysForImsiEncryption(); + } } 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)); } } |