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
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 @@
             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,64 +98,68 @@
         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 {
-                            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");
-                                }
-                            }
-                        }
-                        return new ApnSettings(
-                                mmscUrl, proxyAddress, proxyPort, getDebugText(cursor));
-                    }
-                }
 
-            }
-        } finally {
-            if (cursor != null) {
-                cursor.close();
+        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 {
+                            proxyPort = Integer.parseInt(portString);
+                        } catch (NumberFormatException e) {
+                            LogUtil.e(requestId, "Invalid port " + portString + ", use 80");
+                        }
+                    }
+                }
+                return new ApnSettings(
+                        mmscUrl, proxyAddress, proxyPort, getDebugText(cursor));
+            }
+        }
+        return null;
+    }
+
     private static String getDebugText(Cursor cursor) {
         final StringBuilder sb = new StringBuilder();
         sb.append("APN [");
@@ -183,7 +187,7 @@
         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 @@
 
     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;
+        }
+    }
+
+}