diff options
author | Stephanie Shi <stephshi@google.com> | 2019-11-18 01:13:58 -0800 |
---|---|---|
committer | Stephanie Shi <stephshi@google.com> | 2019-12-09 11:34:50 -0800 |
commit | ba686f4e57e874860bd380c918d9b7a602fbb733 (patch) | |
tree | cf7fcdaa08ce6d86b74e72b3bcaad24039600d39 | |
parent | 79f796b61ae0a1f0fcce67359ae7dca1ba5f79d6 (diff) | |
download | Mms-ba686f4e57e874860bd380c918d9b7a602fbb733.tar.gz |
Add ApnSettingsTest
Adding this test to cover the regression in b/24002396 so that we no
longer have to rely on a manual test. The tests under
MmsServiceRoboTests still need to be added to a presubmit.
Test: atest ApnSettingsTest
Change-Id: Ic112a3edf750597bc5f23790d7258175988d871e
Bug: 143818216
-rw-r--r-- | src/com/android/mms/service/ApnSettings.java | 138 | ||||
-rw-r--r-- | tests/robotests/Android.bp | 10 | ||||
-rw-r--r-- | tests/robotests/src/com/android/mms/service/ApnSettingsTest.java | 137 |
3 files changed, 213 insertions, 72 deletions
diff --git a/src/com/android/mms/service/ApnSettings.java b/src/com/android/mms/service/ApnSettings.java index 48383c1..9ecdd70 100644 --- a/src/com/android/mms/service/ApnSettings.java +++ b/src/com/android/mms/service/ApnSettings.java @@ -62,29 +62,29 @@ public class ApnSettings { Telephony.Carriers.USER, Telephony.Carriers.PASSWORD, }; - private static final int COLUMN_TYPE = 0; - private static final int COLUMN_MMSC = 1; - private static final int COLUMN_MMSPROXY = 2; - private static final int COLUMN_MMSPORT = 3; - private static final int COLUMN_NAME = 4; - private static final int COLUMN_APN = 5; - private static final int COLUMN_BEARER = 6; - private static final int COLUMN_PROTOCOL = 7; + private static final int COLUMN_TYPE = 0; + private static final int COLUMN_MMSC = 1; + private static final int COLUMN_MMSPROXY = 2; + private static final int COLUMN_MMSPORT = 3; + private static final int COLUMN_NAME = 4; + private static final int COLUMN_APN = 5; + private static final int COLUMN_BEARER = 6; + private static final int COLUMN_PROTOCOL = 7; private static final int COLUMN_ROAMING_PROTOCOL = 8; - private static final int COLUMN_AUTH_TYPE = 9; - private static final int COLUMN_MVNO_TYPE = 10; + private static final int COLUMN_AUTH_TYPE = 9; + private static final int COLUMN_MVNO_TYPE = 10; private static final int COLUMN_MVNO_MATCH_DATA = 11; - private static final int COLUMN_PROXY = 12; - private static final int COLUMN_PORT = 13; - private static final int COLUMN_SERVER = 14; - private static final int COLUMN_USER = 15; - private static final int COLUMN_PASSWORD = 16; + private static final int COLUMN_PROXY = 12; + private static final int COLUMN_PORT = 13; + private static final int COLUMN_SERVER = 14; + private static final int COLUMN_USER = 15; + private static final int COLUMN_PASSWORD = 16; /** * Load APN settings from system - * @param context - * @param apnName the optional APN name to match + * + * @param apnName the optional APN name to match * @param requestId the request ID for logging */ public static ApnSettings load(Context context, String apnName, int subId, String requestId) @@ -98,62 +98,66 @@ public class ApnSettings { if (!TextUtils.isEmpty(apnName)) { //selection += " AND " + Telephony.Carriers.APN + "=?"; selection = Telephony.Carriers.APN + "=?"; - selectionArgs = new String[]{ apnName }; + selectionArgs = new String[]{apnName}; } - Cursor cursor = null; - try { - cursor = SqliteWrapper.query( - context, - context.getContentResolver(), - Uri.withAppendedPath(Telephony.Carriers.CONTENT_URI, "/subId/" + subId), - APN_PROJECTION, - selection, - selectionArgs, - null/*sortOrder*/); - if (cursor != null) { - String mmscUrl = null; - String proxyAddress = null; - // Default proxy port to 80 - int proxyPort = 80; - while (cursor.moveToNext()) { - // Read values from APN settings - if (isValidApnType( - cursor.getString(COLUMN_TYPE), PhoneConstants.APN_TYPE_MMS)) { - mmscUrl = trimWithNullCheck(cursor.getString(COLUMN_MMSC)); - if (TextUtils.isEmpty(mmscUrl)) { - continue; - } - mmscUrl = NetworkUtils.trimV4AddrZeros(mmscUrl); + + try (Cursor cursor = SqliteWrapper.query( + context, + context.getContentResolver(), + Uri.withAppendedPath(Telephony.Carriers.CONTENT_URI, "/subId/" + subId), + APN_PROJECTION, + selection, + selectionArgs, + null/*sortOrder*/)) { + + ApnSettings settings = getApnSettingsFromCursor(cursor, requestId); + if (settings != null) { + return settings; + } + } + throw new ApnException("Can not find valid APN"); + } + + private static ApnSettings getApnSettingsFromCursor(Cursor cursor, String requestId) + throws ApnException { + if (cursor == null) { + return null; + } + + // Default proxy port to 80 + int proxyPort = 80; + while (cursor.moveToNext()) { + // Read values from APN settings + if (isValidApnType( + cursor.getString(COLUMN_TYPE), PhoneConstants.APN_TYPE_MMS)) { + String mmscUrl = trimWithNullCheck(cursor.getString(COLUMN_MMSC)); + if (TextUtils.isEmpty(mmscUrl)) { + continue; + } + mmscUrl = NetworkUtils.trimV4AddrZeros(mmscUrl); + try { + new URI(mmscUrl); + } catch (URISyntaxException e) { + throw new ApnException("Invalid MMSC url " + mmscUrl); + } + String proxyAddress = trimWithNullCheck(cursor.getString(COLUMN_MMSPROXY)); + if (!TextUtils.isEmpty(proxyAddress)) { + proxyAddress = NetworkUtils.trimV4AddrZeros(proxyAddress); + final String portString = + trimWithNullCheck(cursor.getString(COLUMN_MMSPORT)); + if (!TextUtils.isEmpty(portString)) { try { - new URI(mmscUrl); - } catch (URISyntaxException e) { - throw new ApnException("Invalid MMSC url " + mmscUrl); - } - proxyAddress = trimWithNullCheck(cursor.getString(COLUMN_MMSPROXY)); - if (!TextUtils.isEmpty(proxyAddress)) { - proxyAddress = NetworkUtils.trimV4AddrZeros(proxyAddress); - final String portString = - trimWithNullCheck(cursor.getString(COLUMN_MMSPORT)); - if (!TextUtils.isEmpty(portString)) { - try { - proxyPort = Integer.parseInt(portString); - } catch (NumberFormatException e) { - LogUtil.e(requestId, "Invalid port " + portString + ", use 80"); - } - } + proxyPort = Integer.parseInt(portString); + } catch (NumberFormatException e) { + LogUtil.e(requestId, "Invalid port " + portString + ", use 80"); } - return new ApnSettings( - mmscUrl, proxyAddress, proxyPort, getDebugText(cursor)); } } - - } - } finally { - if (cursor != null) { - cursor.close(); + return new ApnSettings( + mmscUrl, proxyAddress, proxyPort, getDebugText(cursor)); } } - throw new ApnException("Can not find valid APN"); + return null; } private static String getDebugText(Cursor cursor) { @@ -183,7 +187,7 @@ public class ApnSettings { mProxyAddress = proxyAddr; mProxyPort = proxyPort; mDebugText = debugText; - } + } public String getMmscUrl() { return mServiceCenter; diff --git a/tests/robotests/Android.bp b/tests/robotests/Android.bp index e983542..54b361b 100644 --- a/tests/robotests/Android.bp +++ b/tests/robotests/Android.bp @@ -9,10 +9,10 @@ android_robolectric_test { java_resource_dirs: ["config"], - static_libs: ["testng"], + static_libs: [ + "androidx.test.core", + "testng", + ], instrumentation_for: "MmsService", -} - - - +}
\ No newline at end of file diff --git a/tests/robotests/src/com/android/mms/service/ApnSettingsTest.java b/tests/robotests/src/com/android/mms/service/ApnSettingsTest.java new file mode 100644 index 0000000..6428b1e --- /dev/null +++ b/tests/robotests/src/com/android/mms/service/ApnSettingsTest.java @@ -0,0 +1,137 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.mms.service; + +import static com.google.common.truth.Truth.assertThat; + +import static org.testng.Assert.assertThrows; + +import android.content.ContentProvider; +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.net.Uri; +import android.provider.Telephony; + +import androidx.test.core.app.ApplicationProvider; + +import com.android.internal.telephony.PhoneConstants; +import com.android.mms.service.exception.ApnException; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.fakes.RoboCursor; +import org.robolectric.shadows.ShadowContentResolver; + +import java.util.Arrays; + +@RunWith(RobolectricTestRunner.class) +public final class ApnSettingsTest { + + private Context context; + + @Before + public void setUp() { + context = ApplicationProvider.getApplicationContext(); + } + + @Test + public void load_noApnSettings_throwsApnException() { + assertThrows(ApnException.class, + () -> ApnSettings.load(context, "apnName", /* subId= */ 0, "requestId")); + } + + @Test + public void getApnSettingsFromCursor_validSettings_correctApnSettingsLoaded() throws Exception { + createApnSettingsCursor("mmscUrl", "mmsProxy", /* proxyPort= */ "123"); + ApnSettings apnSettings = ApnSettings.load(context, "apnName", /* subId= */ 0, "requestId"); + + assertThat(apnSettings.getProxyPort()).isEqualTo(123); + assertThat(apnSettings.getMmscUrl()).isEqualTo("mmscUrl"); + assertThat(apnSettings.getProxyAddress()).isEqualTo("mmsProxy"); + } + + @Test + public void getApnSettingsFromCursor_nullMmsPort_defaultProxyPortUsed() throws Exception { + createApnSettingsCursor("mmscUrl", "mmsProxy", /* proxyPort= */ null); + ApnSettings apnSettings = ApnSettings.load(context, "apnName", /* subId= */ 0, "requestId"); + assertThat(apnSettings.getProxyPort()).isEqualTo(80); + } + + @Test + public void getApnSettingsFromCursor_emptyMmsPort_defaultProxyPortUsed() throws Exception { + createApnSettingsCursor("mmscUrl", "mmsProxy", + /* proxyPort= */ ""); + ApnSettings apnSettings = ApnSettings.load(context, "apnName", /* subId= */ 0, "requestId"); + assertThat(apnSettings.getProxyPort()).isEqualTo(80); + } + + private void createApnSettingsCursor(String mmscUrl, String mmsProxy, String proxyPort) { + Object[][] apnValues = + {new Object[]{PhoneConstants.APN_TYPE_MMS, mmscUrl, mmsProxy, proxyPort}}; + RoboCursor cursor = new RoboCursor(); + cursor.setResults(apnValues); + cursor.setColumnNames(Arrays.asList(Telephony.Carriers.TYPE, Telephony.Carriers.MMSC, + Telephony.Carriers.MMSPROXY, Telephony.Carriers.MMSPORT)); + + ShadowContentResolver.registerProviderInternal( + Telephony.Carriers.CONTENT_URI.getAuthority(), new FakeApnSettingsProvider(cursor)); + } + + private final class FakeApnSettingsProvider extends ContentProvider { + + private final Cursor cursor; + + FakeApnSettingsProvider(Cursor cursor) { + this.cursor = cursor; + } + + @Override + public boolean onCreate() { + return false; + } + + @Override + public Cursor query(Uri uri, String[] projection, + String selection, String[] selectionArgs, String sortOrder) { + return cursor; + } + + @Override + public String getType(Uri uri) { + return null; + } + + @Override + public Uri insert(Uri uri, ContentValues values) { + return null; + } + + @Override + public int delete(Uri uri, String selection, String[] selectionArgs) { + return 0; + } + + @Override + public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { + return 0; + } + } + +} |