Loading src/java/com/android/internal/telephony/CarrierKeyDownloadManager.java +55 −72 Original line number Diff line number Diff line Loading @@ -16,7 +16,6 @@ package com.android.internal.telephony; import static android.preference.PreferenceManager.getDefaultSharedPreferences; import static android.telephony.CarrierConfigManager.KEY_ALLOW_METERED_NETWORK_FOR_CERT_DOWNLOAD_BOOL; import static java.nio.charset.StandardCharsets.UTF_8; Loading @@ -28,7 +27,6 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.database.Cursor; import android.net.Uri; import android.os.Handler; Loading Loading @@ -69,8 +67,6 @@ import java.util.zip.ZipException; public class CarrierKeyDownloadManager extends Handler { private static final String LOG_TAG = "CarrierKeyDownloadManager"; private static final String MCC_MNC_PREF_TAG = "CARRIER_KEY_DM_MCC_MNC"; private static final String CERT_BEGIN_STRING = "-----BEGIN CERTIFICATE-----"; private static final String CERT_END_STRING = "-----END CERTIFICATE-----"; Loading @@ -94,10 +90,6 @@ public class CarrierKeyDownloadManager extends Handler { @VisibleForTesting public int mKeyAvailability = 0; public static final String MNC = "MNC"; public static final String MCC = "MCC"; private static final String SEPARATOR = ":"; private static final String JSON_CERTIFICATE = "certificate"; private static final String JSON_CERTIFICATE_ALTERNATE = "public-key"; private static final String JSON_TYPE = "key-type"; Loading @@ -119,18 +111,34 @@ public class CarrierKeyDownloadManager extends Handler { private String mURL; private boolean mAllowedOverMeteredNetwork = false; @VisibleForTesting public String mMccMncForDownload; @VisibleForTesting public long mDownloadId; public CarrierKeyDownloadManager(Phone phone) { mPhone = phone; mContext = phone.getContext(); IntentFilter filter = new IntentFilter(); filter.addAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED); filter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE); filter.addAction(INTENT_KEY_RENEWAL_ALARM_PREFIX + mPhone.getPhoneId()); filter.addAction(TelephonyIntents.ACTION_CARRIER_CERTIFICATE_DOWNLOAD); mContext.registerReceiver(mBroadcastReceiver, filter, null, phone); mDownloadManager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE); } private final BroadcastReceiver mDownloadReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) { Log.d(LOG_TAG, "Download Complete"); sendMessage(obtainMessage(EVENT_DOWNLOAD_COMPLETE, intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0))); } } }; private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Loading @@ -151,10 +159,6 @@ public class CarrierKeyDownloadManager extends Handler { Log.d(LOG_TAG, "Carrier Config changed: " + action); sendEmptyMessage(EVENT_ALARM_OR_CONFIG_CHANGE); } } else if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) { Log.d(LOG_TAG, "Download Complete"); sendMessage(obtainMessage(EVENT_DOWNLOAD_COMPLETE, intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0))); } } }; Loading @@ -167,9 +171,9 @@ public class CarrierKeyDownloadManager extends Handler { break; case EVENT_DOWNLOAD_COMPLETE: long carrierKeyDownloadIdentifier = (long) msg.obj; String mccMnc = getMccMncSetFromPref(); if (isValidDownload(mccMnc)) { onDownloadComplete(carrierKeyDownloadIdentifier, mccMnc); String currentMccMnc = getSimOperator(); if (isValidDownload(currentMccMnc, carrierKeyDownloadIdentifier)) { onDownloadComplete(carrierKeyDownloadIdentifier, currentMccMnc); onPostDownloadProcessing(carrierKeyDownloadIdentifier); } break; Loading @@ -178,7 +182,10 @@ public class CarrierKeyDownloadManager extends Handler { private void onPostDownloadProcessing(long carrierKeyDownloadIdentifier) { resetRenewalAlarm(); cleanupDownloadPreferences(carrierKeyDownloadIdentifier); cleanupDownloadInfo(); // unregister from DOWNLOAD_COMPLETE mContext.unregisterReceiver(mDownloadReceiver); } private void handleAlarmOrConfigChange() { Loading @@ -201,11 +208,11 @@ public class CarrierKeyDownloadManager extends Handler { } } private void cleanupDownloadPreferences(long carrierKeyDownloadIdentifier) { Log.d(LOG_TAG, "Cleaning up download preferences: " + carrierKeyDownloadIdentifier); SharedPreferences.Editor editor = getDefaultSharedPreferences(mContext).edit(); editor.remove(String.valueOf(carrierKeyDownloadIdentifier)); editor.commit(); private void cleanupDownloadInfo() { Log.d(LOG_TAG, "Cleaning up download info"); mDownloadId = -1; mMccMncForDownload = null; } private void cleanupRenewalAlarms() { Loading Loading @@ -279,14 +286,6 @@ public class CarrierKeyDownloadManager extends Handler { + new Date(minExpirationDate)); } private String getMccMncSetFromPref() { // check if this is a download that we had created. We do this by checking if the // downloadId is stored in the shared prefs. int slotId = mPhone.getPhoneId(); SharedPreferences preferences = getDefaultSharedPreferences(mContext); return preferences.getString(MCC_MNC_PREF_TAG + slotId, null); } /** * Returns the sim operator. **/ Loading @@ -303,32 +302,22 @@ public class CarrierKeyDownloadManager extends Handler { * instance of the phone. **/ @VisibleForTesting public boolean isValidDownload(String mccMnc) { String mccCurrent = ""; String mncCurrent = ""; String mccSource = ""; String mncSource = ""; String simOperator = getSimOperator(); if (TextUtils.isEmpty(simOperator) || TextUtils.isEmpty(mccMnc)) { Log.e(LOG_TAG, "simOperator or mcc/mnc is empty"); public boolean isValidDownload(String currentMccMnc, long currentDownloadId) { if (currentDownloadId != mDownloadId) { Log.e(LOG_TAG, "download ID=" + currentDownloadId + " for completed download does not match stored id=" + mDownloadId); return false; } String[] splitValue = mccMnc.split(SEPARATOR); mccSource = splitValue[0]; mncSource = splitValue[1]; Log.d(LOG_TAG, "values from sharedPrefs mcc, mnc: " + mccSource + "," + mncSource); mccCurrent = simOperator.substring(0, 3); mncCurrent = simOperator.substring(3); Log.d(LOG_TAG, "using values for mcc, mnc: " + mccCurrent + "," + mncCurrent); if (TextUtils.isEmpty(currentMccMnc) || TextUtils.isEmpty(mMccMncForDownload) || !TextUtils.equals(currentMccMnc, mMccMncForDownload)) { Log.e(LOG_TAG, "currentMccMnc=" + currentMccMnc + " stored=" + mMccMncForDownload); return false; } if (TextUtils.equals(mncSource, mncCurrent) && TextUtils.equals(mccSource, mccCurrent)) { Log.d(LOG_TAG, "Matched MccMnc, downloadId: " + currentMccMnc + "," + currentDownloadId); return true; } return false; } /** * This method will try to parse the downloaded information, and persist it in the database. Loading Loading @@ -461,11 +450,8 @@ public class CarrierKeyDownloadManager extends Handler { return; } try { String mcc = ""; String mnc = ""; String[] splitValue = mccMnc.split(SEPARATOR); mcc = splitValue[0]; mnc = splitValue[1]; String mcc = mccMnc.substring(0, 3); String mnc = mccMnc.substring(3); JSONObject jsonObj = new JSONObject(jsonStr); JSONArray keys = jsonObj.getJSONArray(JSON_CARRIER_KEYS); for (int i = 0; i < keys.length(); i++) { Loading Loading @@ -548,19 +534,19 @@ public class CarrierKeyDownloadManager extends Handler { private boolean downloadKey() { Log.d(LOG_TAG, "starting download from: " + mURL); String mcc = ""; String mnc = ""; String simOperator = getSimOperator(); if (!TextUtils.isEmpty(simOperator)) { mcc = simOperator.substring(0, 3); mnc = simOperator.substring(3); Log.d(LOG_TAG, "using values for mcc, mnc: " + mcc + "," + mnc); String mccMnc = getSimOperator(); if (!TextUtils.isEmpty(mccMnc)) { Log.d(LOG_TAG, "downloading key for mccmnc: " + mccMnc); } else { Log.e(LOG_TAG, "mcc, mnc: is empty"); Log.e(LOG_TAG, "mccmnc: is empty"); return false; } try { // register the broadcast receiver to listen for download complete IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE); mContext.registerReceiver(mDownloadReceiver, filter, null, mPhone); DownloadManager.Request request = new DownloadManager.Request(Uri.parse(mURL)); // TODO(b/128550341): Implement the logic to minimize using metered network such as Loading @@ -569,14 +555,11 @@ public class CarrierKeyDownloadManager extends Handler { request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN); request.addRequestHeader("Accept-Encoding", "gzip"); Long carrierKeyDownloadRequestId = mDownloadManager.enqueue(request); SharedPreferences.Editor editor = getDefaultSharedPreferences(mContext).edit(); String mccMnc = mcc + SEPARATOR + mnc; int slotId = mPhone.getPhoneId(); Log.d(LOG_TAG, "storing values in sharedpref mcc, mnc, days: " + mcc + "," + mnc Log.d(LOG_TAG, "saving values mccmnc, downloadId: " + mccMnc + ", " + carrierKeyDownloadRequestId); editor.putString(MCC_MNC_PREF_TAG + slotId, mccMnc); editor.commit(); mMccMncForDownload = mccMnc; mDownloadId = carrierKeyDownloadRequestId; } catch (Exception e) { Log.e(LOG_TAG, "exception trying to download key from url: " + mURL); return false; Loading tests/telephonytests/src/com/android/internal/telephony/CarrierKeyDownloadMgrTest.java +33 −26 Original line number Diff line number Diff line Loading @@ -15,7 +15,7 @@ */ package com.android.internal.telephony; import static android.preference.PreferenceManager.getDefaultSharedPreferences; import static junit.framework.Assert.assertNull; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; Loading @@ -31,7 +31,6 @@ import static org.mockito.Mockito.when; import android.app.DownloadManager; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.PersistableBundle; import android.telephony.CarrierConfigManager; import android.telephony.ImsiEncryptionInfo; Loading Loading @@ -154,7 +153,7 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest { } ImsiEncryptionInfo imsiEncryptionInfo = new ImsiEncryptionInfo("310", "270", 2, "key1=value", keyInfo.first, new Date(keyInfo.second)); String mccMnc = "310:270"; String mccMnc = "310270"; mCarrierKeyDM.parseJsonAndPersistKey(mJsonStr, mccMnc); verify(mPhone, times(2)).setCarrierInfoForImsiEncryption( (Matchers.refEq(imsiEncryptionInfo))); Loading @@ -175,7 +174,7 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest { } ImsiEncryptionInfo imsiEncryptionInfo = new ImsiEncryptionInfo("310", "270", 2, "key1=value", keyInfo.first, new Date(keyInfo.second)); String mccMnc = "310:270"; String mccMnc = "310270"; mCarrierKeyDM.parseJsonAndPersistKey(mJsonStr1, mccMnc); verify(mPhone, times(2)).setCarrierInfoForImsiEncryption( (Matchers.refEq(imsiEncryptionInfo))); Loading @@ -188,7 +187,7 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest { @Test @SmallTest public void testParseBadJsonFail() { String mccMnc = "310:290"; String mccMnc = "310290"; String badJsonStr = "{badJsonString}"; mCarrierKeyDM.parseJsonAndPersistKey(badJsonStr, mccMnc); verify(mPhone, times(0)).setCarrierInfoForImsiEncryption(any()); Loading @@ -201,9 +200,13 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest { @Test @SmallTest public void testIsValidDownload() { String mccMnc = "310:260"; when(mTelephonyManager.getSimOperator(anyInt())).thenReturn("310260"); assertTrue(mCarrierKeyDM.isValidDownload(mccMnc)); String currentMccMnc = "310260"; long currentDownloadId = 1; // mock downloadId to match mCarrierKeyDM.mMccMncForDownload = currentMccMnc; mCarrierKeyDM.mDownloadId = currentDownloadId; assertTrue(mCarrierKeyDM.isValidDownload(currentMccMnc, currentDownloadId)); } /** Loading @@ -213,9 +216,18 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest { @Test @SmallTest public void testIsValidDownloadFail() { String mccMnc = "310:290"; when(mTelephonyManager.getSimOperator(anyInt())).thenReturn("310260"); assertFalse(mCarrierKeyDM.isValidDownload(mccMnc)); String currentMccMnc = "310260"; long currentDownloadId = 1; // mock downloadId to match, mccmnc so it doesn't match mCarrierKeyDM.mMccMncForDownload = "310290"; mCarrierKeyDM.mDownloadId = currentDownloadId; assertFalse(mCarrierKeyDM.isValidDownload(currentMccMnc, currentDownloadId)); // pass in mccmnc to match, and mock shared pref downloadId so it doesn't match currentMccMnc = "310290"; mCarrierKeyDM.mDownloadId = currentDownloadId + 1; assertFalse(mCarrierKeyDM.isValidDownload(currentMccMnc, currentDownloadId)); } /** Loading Loading @@ -243,11 +255,10 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest { @Test @SmallTest public void testDownloadComplete() { SharedPreferences.Editor editor = getDefaultSharedPreferences(mContext).edit(); String mccMnc = "310:260"; int slotId = mPhone.getPhoneId(); editor.putString("CARRIER_KEY_DM_MCC_MNC" + slotId, mccMnc); editor.commit(); String mccMnc = "310260"; long downloadId = 1; mCarrierKeyDM.mMccMncForDownload = mccMnc; mCarrierKeyDM.mDownloadId = downloadId; SimpleDateFormat dt = new SimpleDateFormat("yyyy-mm-dd"); Calendar expectedCal = new GregorianCalendar(); Loading @@ -256,6 +267,7 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest { when(mTelephonyManager.getSimOperator(anyInt())).thenReturn("310260"); Intent mIntent = new Intent(DownloadManager.ACTION_DOWNLOAD_COMPLETE); mIntent.putExtra(DownloadManager.EXTRA_DOWNLOAD_ID, downloadId); mContext.sendBroadcast(mIntent); processAllMessages(); Date expirationDate = new Date(mCarrierKeyDM.getExpirationDate()); Loading @@ -281,9 +293,7 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest { mIntent.putExtra(PhoneConstants.PHONE_KEY, 0); mContext.sendBroadcast(mIntent); processAllMessages(); SharedPreferences preferences = getDefaultSharedPreferences(mContext); String mccMnc = preferences.getString("CARRIER_KEY_DM_MCC_MNC" + slotId, null); assertTrue(mccMnc.equals("310:260")); assertEquals("310260", mCarrierKeyDM.mMccMncForDownload); } /** Loading @@ -304,9 +314,8 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest { mIntent.putExtra(PhoneConstants.PHONE_KEY, 0); mContext.sendBroadcast(mIntent); processAllMessages(); SharedPreferences preferences = getDefaultSharedPreferences(mContext); String mccMnc = preferences.getString("CARRIER_KEY_DM_MCC_MNC" + slotId, null); assertEquals(null, mccMnc); assertNull(mCarrierKeyDM.mMccMncForDownload); verify(mPhone).deleteCarrierInfoForImsiEncryption(); } Loading @@ -329,9 +338,7 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest { + slotId); mContext.sendBroadcast(mIntent); processAllMessages(); SharedPreferences preferences = getDefaultSharedPreferences(mContext); String mccMnc = preferences.getString("CARRIER_KEY_DM_MCC_MNC" + slotId, null); assertTrue(mccMnc.equals("310:260")); assertEquals("310260", mCarrierKeyDM.mMccMncForDownload); } /** Loading @@ -349,7 +356,7 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest { ImsiEncryptionInfo imsiEncryptionInfo = new ImsiEncryptionInfo("310", "270", TelephonyManager.KEY_TYPE_WLAN, "key1=value", keyInfo.first, new Date(CERT_EXPIRATION)); String mccMnc = "310:270"; String mccMnc = "310270"; mCarrierKeyDM.parseJsonAndPersistKey(mJsonStr3GppSpec, mccMnc); verify(mPhone).setCarrierInfoForImsiEncryption( (Matchers.refEq(imsiEncryptionInfo))); Loading Loading
src/java/com/android/internal/telephony/CarrierKeyDownloadManager.java +55 −72 Original line number Diff line number Diff line Loading @@ -16,7 +16,6 @@ package com.android.internal.telephony; import static android.preference.PreferenceManager.getDefaultSharedPreferences; import static android.telephony.CarrierConfigManager.KEY_ALLOW_METERED_NETWORK_FOR_CERT_DOWNLOAD_BOOL; import static java.nio.charset.StandardCharsets.UTF_8; Loading @@ -28,7 +27,6 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.database.Cursor; import android.net.Uri; import android.os.Handler; Loading Loading @@ -69,8 +67,6 @@ import java.util.zip.ZipException; public class CarrierKeyDownloadManager extends Handler { private static final String LOG_TAG = "CarrierKeyDownloadManager"; private static final String MCC_MNC_PREF_TAG = "CARRIER_KEY_DM_MCC_MNC"; private static final String CERT_BEGIN_STRING = "-----BEGIN CERTIFICATE-----"; private static final String CERT_END_STRING = "-----END CERTIFICATE-----"; Loading @@ -94,10 +90,6 @@ public class CarrierKeyDownloadManager extends Handler { @VisibleForTesting public int mKeyAvailability = 0; public static final String MNC = "MNC"; public static final String MCC = "MCC"; private static final String SEPARATOR = ":"; private static final String JSON_CERTIFICATE = "certificate"; private static final String JSON_CERTIFICATE_ALTERNATE = "public-key"; private static final String JSON_TYPE = "key-type"; Loading @@ -119,18 +111,34 @@ public class CarrierKeyDownloadManager extends Handler { private String mURL; private boolean mAllowedOverMeteredNetwork = false; @VisibleForTesting public String mMccMncForDownload; @VisibleForTesting public long mDownloadId; public CarrierKeyDownloadManager(Phone phone) { mPhone = phone; mContext = phone.getContext(); IntentFilter filter = new IntentFilter(); filter.addAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED); filter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE); filter.addAction(INTENT_KEY_RENEWAL_ALARM_PREFIX + mPhone.getPhoneId()); filter.addAction(TelephonyIntents.ACTION_CARRIER_CERTIFICATE_DOWNLOAD); mContext.registerReceiver(mBroadcastReceiver, filter, null, phone); mDownloadManager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE); } private final BroadcastReceiver mDownloadReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) { Log.d(LOG_TAG, "Download Complete"); sendMessage(obtainMessage(EVENT_DOWNLOAD_COMPLETE, intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0))); } } }; private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Loading @@ -151,10 +159,6 @@ public class CarrierKeyDownloadManager extends Handler { Log.d(LOG_TAG, "Carrier Config changed: " + action); sendEmptyMessage(EVENT_ALARM_OR_CONFIG_CHANGE); } } else if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) { Log.d(LOG_TAG, "Download Complete"); sendMessage(obtainMessage(EVENT_DOWNLOAD_COMPLETE, intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0))); } } }; Loading @@ -167,9 +171,9 @@ public class CarrierKeyDownloadManager extends Handler { break; case EVENT_DOWNLOAD_COMPLETE: long carrierKeyDownloadIdentifier = (long) msg.obj; String mccMnc = getMccMncSetFromPref(); if (isValidDownload(mccMnc)) { onDownloadComplete(carrierKeyDownloadIdentifier, mccMnc); String currentMccMnc = getSimOperator(); if (isValidDownload(currentMccMnc, carrierKeyDownloadIdentifier)) { onDownloadComplete(carrierKeyDownloadIdentifier, currentMccMnc); onPostDownloadProcessing(carrierKeyDownloadIdentifier); } break; Loading @@ -178,7 +182,10 @@ public class CarrierKeyDownloadManager extends Handler { private void onPostDownloadProcessing(long carrierKeyDownloadIdentifier) { resetRenewalAlarm(); cleanupDownloadPreferences(carrierKeyDownloadIdentifier); cleanupDownloadInfo(); // unregister from DOWNLOAD_COMPLETE mContext.unregisterReceiver(mDownloadReceiver); } private void handleAlarmOrConfigChange() { Loading @@ -201,11 +208,11 @@ public class CarrierKeyDownloadManager extends Handler { } } private void cleanupDownloadPreferences(long carrierKeyDownloadIdentifier) { Log.d(LOG_TAG, "Cleaning up download preferences: " + carrierKeyDownloadIdentifier); SharedPreferences.Editor editor = getDefaultSharedPreferences(mContext).edit(); editor.remove(String.valueOf(carrierKeyDownloadIdentifier)); editor.commit(); private void cleanupDownloadInfo() { Log.d(LOG_TAG, "Cleaning up download info"); mDownloadId = -1; mMccMncForDownload = null; } private void cleanupRenewalAlarms() { Loading Loading @@ -279,14 +286,6 @@ public class CarrierKeyDownloadManager extends Handler { + new Date(minExpirationDate)); } private String getMccMncSetFromPref() { // check if this is a download that we had created. We do this by checking if the // downloadId is stored in the shared prefs. int slotId = mPhone.getPhoneId(); SharedPreferences preferences = getDefaultSharedPreferences(mContext); return preferences.getString(MCC_MNC_PREF_TAG + slotId, null); } /** * Returns the sim operator. **/ Loading @@ -303,32 +302,22 @@ public class CarrierKeyDownloadManager extends Handler { * instance of the phone. **/ @VisibleForTesting public boolean isValidDownload(String mccMnc) { String mccCurrent = ""; String mncCurrent = ""; String mccSource = ""; String mncSource = ""; String simOperator = getSimOperator(); if (TextUtils.isEmpty(simOperator) || TextUtils.isEmpty(mccMnc)) { Log.e(LOG_TAG, "simOperator or mcc/mnc is empty"); public boolean isValidDownload(String currentMccMnc, long currentDownloadId) { if (currentDownloadId != mDownloadId) { Log.e(LOG_TAG, "download ID=" + currentDownloadId + " for completed download does not match stored id=" + mDownloadId); return false; } String[] splitValue = mccMnc.split(SEPARATOR); mccSource = splitValue[0]; mncSource = splitValue[1]; Log.d(LOG_TAG, "values from sharedPrefs mcc, mnc: " + mccSource + "," + mncSource); mccCurrent = simOperator.substring(0, 3); mncCurrent = simOperator.substring(3); Log.d(LOG_TAG, "using values for mcc, mnc: " + mccCurrent + "," + mncCurrent); if (TextUtils.isEmpty(currentMccMnc) || TextUtils.isEmpty(mMccMncForDownload) || !TextUtils.equals(currentMccMnc, mMccMncForDownload)) { Log.e(LOG_TAG, "currentMccMnc=" + currentMccMnc + " stored=" + mMccMncForDownload); return false; } if (TextUtils.equals(mncSource, mncCurrent) && TextUtils.equals(mccSource, mccCurrent)) { Log.d(LOG_TAG, "Matched MccMnc, downloadId: " + currentMccMnc + "," + currentDownloadId); return true; } return false; } /** * This method will try to parse the downloaded information, and persist it in the database. Loading Loading @@ -461,11 +450,8 @@ public class CarrierKeyDownloadManager extends Handler { return; } try { String mcc = ""; String mnc = ""; String[] splitValue = mccMnc.split(SEPARATOR); mcc = splitValue[0]; mnc = splitValue[1]; String mcc = mccMnc.substring(0, 3); String mnc = mccMnc.substring(3); JSONObject jsonObj = new JSONObject(jsonStr); JSONArray keys = jsonObj.getJSONArray(JSON_CARRIER_KEYS); for (int i = 0; i < keys.length(); i++) { Loading Loading @@ -548,19 +534,19 @@ public class CarrierKeyDownloadManager extends Handler { private boolean downloadKey() { Log.d(LOG_TAG, "starting download from: " + mURL); String mcc = ""; String mnc = ""; String simOperator = getSimOperator(); if (!TextUtils.isEmpty(simOperator)) { mcc = simOperator.substring(0, 3); mnc = simOperator.substring(3); Log.d(LOG_TAG, "using values for mcc, mnc: " + mcc + "," + mnc); String mccMnc = getSimOperator(); if (!TextUtils.isEmpty(mccMnc)) { Log.d(LOG_TAG, "downloading key for mccmnc: " + mccMnc); } else { Log.e(LOG_TAG, "mcc, mnc: is empty"); Log.e(LOG_TAG, "mccmnc: is empty"); return false; } try { // register the broadcast receiver to listen for download complete IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE); mContext.registerReceiver(mDownloadReceiver, filter, null, mPhone); DownloadManager.Request request = new DownloadManager.Request(Uri.parse(mURL)); // TODO(b/128550341): Implement the logic to minimize using metered network such as Loading @@ -569,14 +555,11 @@ public class CarrierKeyDownloadManager extends Handler { request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN); request.addRequestHeader("Accept-Encoding", "gzip"); Long carrierKeyDownloadRequestId = mDownloadManager.enqueue(request); SharedPreferences.Editor editor = getDefaultSharedPreferences(mContext).edit(); String mccMnc = mcc + SEPARATOR + mnc; int slotId = mPhone.getPhoneId(); Log.d(LOG_TAG, "storing values in sharedpref mcc, mnc, days: " + mcc + "," + mnc Log.d(LOG_TAG, "saving values mccmnc, downloadId: " + mccMnc + ", " + carrierKeyDownloadRequestId); editor.putString(MCC_MNC_PREF_TAG + slotId, mccMnc); editor.commit(); mMccMncForDownload = mccMnc; mDownloadId = carrierKeyDownloadRequestId; } catch (Exception e) { Log.e(LOG_TAG, "exception trying to download key from url: " + mURL); return false; Loading
tests/telephonytests/src/com/android/internal/telephony/CarrierKeyDownloadMgrTest.java +33 −26 Original line number Diff line number Diff line Loading @@ -15,7 +15,7 @@ */ package com.android.internal.telephony; import static android.preference.PreferenceManager.getDefaultSharedPreferences; import static junit.framework.Assert.assertNull; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; Loading @@ -31,7 +31,6 @@ import static org.mockito.Mockito.when; import android.app.DownloadManager; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.PersistableBundle; import android.telephony.CarrierConfigManager; import android.telephony.ImsiEncryptionInfo; Loading Loading @@ -154,7 +153,7 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest { } ImsiEncryptionInfo imsiEncryptionInfo = new ImsiEncryptionInfo("310", "270", 2, "key1=value", keyInfo.first, new Date(keyInfo.second)); String mccMnc = "310:270"; String mccMnc = "310270"; mCarrierKeyDM.parseJsonAndPersistKey(mJsonStr, mccMnc); verify(mPhone, times(2)).setCarrierInfoForImsiEncryption( (Matchers.refEq(imsiEncryptionInfo))); Loading @@ -175,7 +174,7 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest { } ImsiEncryptionInfo imsiEncryptionInfo = new ImsiEncryptionInfo("310", "270", 2, "key1=value", keyInfo.first, new Date(keyInfo.second)); String mccMnc = "310:270"; String mccMnc = "310270"; mCarrierKeyDM.parseJsonAndPersistKey(mJsonStr1, mccMnc); verify(mPhone, times(2)).setCarrierInfoForImsiEncryption( (Matchers.refEq(imsiEncryptionInfo))); Loading @@ -188,7 +187,7 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest { @Test @SmallTest public void testParseBadJsonFail() { String mccMnc = "310:290"; String mccMnc = "310290"; String badJsonStr = "{badJsonString}"; mCarrierKeyDM.parseJsonAndPersistKey(badJsonStr, mccMnc); verify(mPhone, times(0)).setCarrierInfoForImsiEncryption(any()); Loading @@ -201,9 +200,13 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest { @Test @SmallTest public void testIsValidDownload() { String mccMnc = "310:260"; when(mTelephonyManager.getSimOperator(anyInt())).thenReturn("310260"); assertTrue(mCarrierKeyDM.isValidDownload(mccMnc)); String currentMccMnc = "310260"; long currentDownloadId = 1; // mock downloadId to match mCarrierKeyDM.mMccMncForDownload = currentMccMnc; mCarrierKeyDM.mDownloadId = currentDownloadId; assertTrue(mCarrierKeyDM.isValidDownload(currentMccMnc, currentDownloadId)); } /** Loading @@ -213,9 +216,18 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest { @Test @SmallTest public void testIsValidDownloadFail() { String mccMnc = "310:290"; when(mTelephonyManager.getSimOperator(anyInt())).thenReturn("310260"); assertFalse(mCarrierKeyDM.isValidDownload(mccMnc)); String currentMccMnc = "310260"; long currentDownloadId = 1; // mock downloadId to match, mccmnc so it doesn't match mCarrierKeyDM.mMccMncForDownload = "310290"; mCarrierKeyDM.mDownloadId = currentDownloadId; assertFalse(mCarrierKeyDM.isValidDownload(currentMccMnc, currentDownloadId)); // pass in mccmnc to match, and mock shared pref downloadId so it doesn't match currentMccMnc = "310290"; mCarrierKeyDM.mDownloadId = currentDownloadId + 1; assertFalse(mCarrierKeyDM.isValidDownload(currentMccMnc, currentDownloadId)); } /** Loading Loading @@ -243,11 +255,10 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest { @Test @SmallTest public void testDownloadComplete() { SharedPreferences.Editor editor = getDefaultSharedPreferences(mContext).edit(); String mccMnc = "310:260"; int slotId = mPhone.getPhoneId(); editor.putString("CARRIER_KEY_DM_MCC_MNC" + slotId, mccMnc); editor.commit(); String mccMnc = "310260"; long downloadId = 1; mCarrierKeyDM.mMccMncForDownload = mccMnc; mCarrierKeyDM.mDownloadId = downloadId; SimpleDateFormat dt = new SimpleDateFormat("yyyy-mm-dd"); Calendar expectedCal = new GregorianCalendar(); Loading @@ -256,6 +267,7 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest { when(mTelephonyManager.getSimOperator(anyInt())).thenReturn("310260"); Intent mIntent = new Intent(DownloadManager.ACTION_DOWNLOAD_COMPLETE); mIntent.putExtra(DownloadManager.EXTRA_DOWNLOAD_ID, downloadId); mContext.sendBroadcast(mIntent); processAllMessages(); Date expirationDate = new Date(mCarrierKeyDM.getExpirationDate()); Loading @@ -281,9 +293,7 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest { mIntent.putExtra(PhoneConstants.PHONE_KEY, 0); mContext.sendBroadcast(mIntent); processAllMessages(); SharedPreferences preferences = getDefaultSharedPreferences(mContext); String mccMnc = preferences.getString("CARRIER_KEY_DM_MCC_MNC" + slotId, null); assertTrue(mccMnc.equals("310:260")); assertEquals("310260", mCarrierKeyDM.mMccMncForDownload); } /** Loading @@ -304,9 +314,8 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest { mIntent.putExtra(PhoneConstants.PHONE_KEY, 0); mContext.sendBroadcast(mIntent); processAllMessages(); SharedPreferences preferences = getDefaultSharedPreferences(mContext); String mccMnc = preferences.getString("CARRIER_KEY_DM_MCC_MNC" + slotId, null); assertEquals(null, mccMnc); assertNull(mCarrierKeyDM.mMccMncForDownload); verify(mPhone).deleteCarrierInfoForImsiEncryption(); } Loading @@ -329,9 +338,7 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest { + slotId); mContext.sendBroadcast(mIntent); processAllMessages(); SharedPreferences preferences = getDefaultSharedPreferences(mContext); String mccMnc = preferences.getString("CARRIER_KEY_DM_MCC_MNC" + slotId, null); assertTrue(mccMnc.equals("310:260")); assertEquals("310260", mCarrierKeyDM.mMccMncForDownload); } /** Loading @@ -349,7 +356,7 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest { ImsiEncryptionInfo imsiEncryptionInfo = new ImsiEncryptionInfo("310", "270", TelephonyManager.KEY_TYPE_WLAN, "key1=value", keyInfo.first, new Date(CERT_EXPIRATION)); String mccMnc = "310:270"; String mccMnc = "310270"; mCarrierKeyDM.parseJsonAndPersistKey(mJsonStr3GppSpec, mccMnc); verify(mPhone).setCarrierInfoForImsiEncryption( (Matchers.refEq(imsiEncryptionInfo))); Loading