diff options
author | Nate(Qiang) Jiang <qiangjiang@google.com> | 2020-01-31 09:50:14 -0800 |
---|---|---|
committer | Nate(Qiang) Jiang <qiangjiang@google.com> | 2020-02-05 11:13:17 -0800 |
commit | 80efbfc0aa8d811d751836ea1b08e468c068f299 (patch) | |
tree | 688e53bf5cc6d52634b5c954cb5ae0f1ca4eae47 /libs | |
parent | d69891faa94eaac126c58c64608259b60a945519 (diff) |
Add helper method to check if IMSI protection is required.
Bug: 147696774
Test: atest com.android.wifitrackerlib
Change-Id: Ibcd1f32d953ec1d58f036332a9a91794e5ab8256
Diffstat (limited to 'libs')
-rw-r--r-- | libs/WifiTrackerLib/src/com/android/wifitrackerlib/Utils.java | 93 | ||||
-rw-r--r-- | libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/UtilsTest.java | 95 |
2 files changed, 187 insertions, 1 deletions
diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/Utils.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/Utils.java index b5aad1d91..28456efa1 100644 --- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/Utils.java +++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/Utils.java @@ -35,8 +35,13 @@ import android.content.pm.IPackageManager; import android.content.pm.PackageManager; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; +import android.os.PersistableBundle; import android.os.RemoteException; import android.os.UserHandle; +import android.telephony.CarrierConfigManager; +import android.telephony.SubscriptionInfo; +import android.telephony.SubscriptionManager; +import android.telephony.TelephonyManager; import android.text.TextUtils; import androidx.annotation.NonNull; @@ -375,4 +380,92 @@ class Utils { return sj.toString(); } + + /** + * Check if the SIM is present for target carrier Id. + */ + static boolean isSimPresent(@NonNull Context context, int carrierId) { + SubscriptionManager subscriptionManager = + (SubscriptionManager) context.getSystemService( + Context.TELEPHONY_SUBSCRIPTION_SERVICE); + if (subscriptionManager == null) return false; + List<SubscriptionInfo> subInfoList = subscriptionManager.getActiveSubscriptionInfoList(); + if (subInfoList == null || subInfoList.isEmpty()) { + return false; + } + return subInfoList.stream() + .anyMatch(info -> info.getCarrierId() == carrierId); + } + + /** + * Get the SIM carrier name for target subscription Id. + */ + static @Nullable String getCarrierNameForSubId(@NonNull Context context, int subId) { + TelephonyManager telephonyManager = + (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); + if (telephonyManager == null) return null; + TelephonyManager specifiedTm = telephonyManager.createForSubscriptionId(subId); + if (specifiedTm == null) { + return null; + } + CharSequence name = specifiedTm.getSimCarrierIdName(); + if (name == null) { + return null; + } + return name.toString(); + } + + static boolean isSimCredential(@NonNull WifiConfiguration config) { + return config.enterpriseConfig != null + && config.enterpriseConfig.isAuthenticationSimBased(); + } + + /** + * Get the best match subscription Id for target WifiConfiguration. + */ + static int getSubIdForConfig(@NonNull Context context, @NonNull WifiConfiguration config) { + int dataSubId = SubscriptionManager.getDefaultDataSubscriptionId(); + if (config.carrierId == TelephonyManager.UNKNOWN_CARRIER_ID) { + return dataSubId; + } + SubscriptionManager subscriptionManager = + (SubscriptionManager) context.getSystemService( + Context.TELEPHONY_SUBSCRIPTION_SERVICE); + if (subscriptionManager == null) { + return SubscriptionManager.INVALID_SUBSCRIPTION_ID; + } + List<SubscriptionInfo> subInfoList = subscriptionManager.getActiveSubscriptionInfoList(); + if (subInfoList == null || subInfoList.isEmpty()) { + return SubscriptionManager.INVALID_SUBSCRIPTION_ID; + } + + 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; + } + } + } + return matchSubId; + } + + /** + * Check if target subscription Id requires IMSI privacy protection. + */ + static boolean isImsiPrivacyProtectionProvided(@NonNull Context context, int subId) { + CarrierConfigManager carrierConfigManager = + (CarrierConfigManager) context.getSystemService(Context.CARRIER_CONFIG_SERVICE); + if (carrierConfigManager == null) { + return false; + } + PersistableBundle bundle = carrierConfigManager.getConfigForSubId(subId); + if (bundle == null) { + return false; + } + return (bundle.getInt(CarrierConfigManager.IMSI_KEY_AVAILABILITY_INT) + & TelephonyManager.KEY_TYPE_WLAN) != 0; + } } diff --git a/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/UtilsTest.java b/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/UtilsTest.java index b8b101823..c88ca2343 100644 --- a/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/UtilsTest.java +++ b/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/UtilsTest.java @@ -22,13 +22,19 @@ import static com.android.wifitrackerlib.TestUtils.buildScanResult; import static com.android.wifitrackerlib.Utils.getAppLabelForSavedNetwork; import static com.android.wifitrackerlib.Utils.getAutoConnectDescription; import static com.android.wifitrackerlib.Utils.getBestScanResultByLevel; +import static com.android.wifitrackerlib.Utils.getCarrierNameForSubId; import static com.android.wifitrackerlib.Utils.getMeteredDescription; +import static com.android.wifitrackerlib.Utils.getSubIdForConfig; +import static com.android.wifitrackerlib.Utils.isImsiPrivacyProtectionProvided; +import static com.android.wifitrackerlib.Utils.isSimPresent; import static com.android.wifitrackerlib.Utils.mapScanResultsToKey; import static com.android.wifitrackerlib.WifiEntry.SECURITY_NONE; import static com.android.wifitrackerlib.WifiEntry.SECURITY_PSK; import static com.google.common.truth.Truth.assertThat; + +import static org.junit.Assert.*; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -43,7 +49,12 @@ import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Handler; +import android.os.PersistableBundle; import android.os.test.TestLooper; +import android.telephony.CarrierConfigManager; +import android.telephony.SubscriptionInfo; +import android.telephony.SubscriptionManager; +import android.telephony.TelephonyManager; import org.junit.Before; import org.junit.Ignore; @@ -57,7 +68,6 @@ import java.util.List; import java.util.Map; public class UtilsTest { - private static final String LABEL_AUTO_CONNECTION_DISABLED = "Auto-Connection disabled"; private static final String LABEL_METERED = "Metered"; private static final String LABEL_UNMETERED = "Unmetered"; @@ -65,10 +75,18 @@ public class UtilsTest { private static final String SYSTEM_UID_APP_NAME = "systemUidAppName"; private static final String APP_LABEL = "appLabel"; private static final String SETTINGS_APP_NAME = "com.android.settings"; + private static final int TEST_CARRIER_ID = 1191; + private static final int TEST_SUB_ID = 1111; + + private static final String TEST_CARRIER_NAME = "carrierName"; @Mock private Context mMockContext; @Mock private Resources mMockResources; @Mock private NetworkScoreManager mMockNetworkScoreManager; + @Mock private SubscriptionManager mSubscriptionManager; + @Mock private TelephonyManager mTelephonyManager; + @Mock private CarrierConfigManager mCarrierConfigManager; + @Mock private TelephonyManager mSpecifiedTm; private Handler mTestHandler; @@ -81,6 +99,13 @@ public class UtilsTest { when(mMockContext.getResources()).thenReturn(mMockResources); when(mMockContext.getSystemService(Context.NETWORK_SCORE_SERVICE)) .thenReturn(mMockNetworkScoreManager); + when(mMockContext.getSystemService(Context.CARRIER_CONFIG_SERVICE)) + .thenReturn(mCarrierConfigManager); + when(mMockContext.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE)) + .thenReturn(mSubscriptionManager); + when(mMockContext.getSystemService(Context.TELEPHONY_SERVICE)) + .thenReturn(mTelephonyManager); + when(mTelephonyManager.createForSubscriptionId(TEST_CARRIER_ID)).thenReturn(mSpecifiedTm); } @Test @@ -269,6 +294,74 @@ public class UtilsTest { assertThat(meteredDescription).isEqualTo(LABEL_UNMETERED); } + @Test + public void testCheckSimPresentWithNoSubscription() { + when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn(new ArrayList<>()); + assertFalse(isSimPresent(mMockContext, TEST_CARRIER_ID)); + } + + @Test + public void testCheckSimPresentWithNoMatchingSubscription() { + List<SubscriptionInfo> subscriptionInfoList = new ArrayList<>(); + SubscriptionInfo subscriptionInfo = mock(SubscriptionInfo.class); + when(subscriptionInfo.getCarrierId()).thenReturn(TEST_CARRIER_ID + 1); + subscriptionInfoList.add(subscriptionInfo); + when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn(subscriptionInfoList); + assertFalse(isSimPresent(mMockContext, TEST_CARRIER_ID)); + } + + @Test + public void testCheckSimPresentWithMatchingSubscription() { + List<SubscriptionInfo> subscriptionInfoList = new ArrayList<>(); + SubscriptionInfo subscriptionInfo = mock(SubscriptionInfo.class); + when(subscriptionInfo.getCarrierId()).thenReturn(TEST_CARRIER_ID); + subscriptionInfoList.add(subscriptionInfo); + when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn(subscriptionInfoList); + assertTrue(isSimPresent(mMockContext, TEST_CARRIER_ID)); + } + + @Test + public void testGetCarrierName() { + when(mSpecifiedTm.getSimCarrierIdName()).thenReturn(TEST_CARRIER_NAME); + assertEquals(TEST_CARRIER_NAME, getCarrierNameForSubId(mMockContext, TEST_CARRIER_ID)); + } + + @Test + public void testCheckRequireImsiPrivacyProtectionWithNoCarrierConfig() { + assertFalse(isImsiPrivacyProtectionProvided(mMockContext, TEST_SUB_ID)); + } + + @Test + public void testCheckRequireImsiPrivacyProtectionWithCarrierConfigKeyAvailable() { + PersistableBundle bundle = new PersistableBundle(); + bundle.putInt(CarrierConfigManager.IMSI_KEY_AVAILABILITY_INT, + TelephonyManager.KEY_TYPE_WLAN); + when(mCarrierConfigManager.getConfigForSubId(TEST_SUB_ID)).thenReturn(bundle); + assertTrue(isImsiPrivacyProtectionProvided(mMockContext, TEST_SUB_ID)); + } + + @Test + public void testGetSubIdForWifiConfigurationWithNoSubscription() { + WifiConfiguration config = new WifiConfiguration(); + config.carrierId = TEST_CARRIER_ID; + assertEquals(SubscriptionManager.INVALID_SUBSCRIPTION_ID, + getSubIdForConfig(mMockContext, config)); + } + + @Test + public void testGetSubIdForWifiConfigurationWithMatchingSubscription() { + WifiConfiguration config = new WifiConfiguration(); + config.carrierId = TEST_CARRIER_ID; + List<SubscriptionInfo> subscriptionInfoList = new ArrayList<>(); + SubscriptionInfo subscriptionInfo = mock(SubscriptionInfo.class); + when(subscriptionInfo.getCarrierId()).thenReturn(TEST_CARRIER_ID); + when(subscriptionInfo.getSubscriptionId()).thenReturn(TEST_SUB_ID); + subscriptionInfoList.add(subscriptionInfo); + when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn(subscriptionInfoList); + assertEquals(TEST_SUB_ID, getSubIdForConfig(mMockContext, config)); + } + + private StandardWifiEntry getStandardWifiEntry(WifiConfiguration config) { final WifiManager mockWifiManager = mock(WifiManager.class); final StandardWifiEntry entry = new StandardWifiEntry(mMockContext, mTestHandler, |