Home | History | Annotate | Download | only in dataconnection
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.internal.telephony.dataconnection;
     18 
     19 import static com.android.internal.telephony.PhoneConstants.APN_TYPE_ALL;
     20 import static com.android.internal.telephony.PhoneConstants.APN_TYPE_DEFAULT;
     21 import static com.android.internal.telephony.PhoneConstants.APN_TYPE_HIPRI;
     22 import static com.android.internal.telephony.PhoneConstants.APN_TYPE_IA;
     23 import static com.android.internal.telephony.PhoneConstants.APN_TYPE_MMS;
     24 import static com.android.internal.telephony.PhoneConstants.APN_TYPE_SUPL;
     25 
     26 import static junit.framework.Assert.assertFalse;
     27 import static junit.framework.Assert.assertTrue;
     28 import static junit.framework.Assert.fail;
     29 
     30 import static org.junit.Assert.assertEquals;
     31 import static org.mockito.Mockito.doReturn;
     32 
     33 import android.os.PersistableBundle;
     34 import android.telephony.CarrierConfigManager;
     35 import android.telephony.ServiceState;
     36 import android.test.suitebuilder.annotation.SmallTest;
     37 
     38 import com.android.internal.telephony.PhoneConstants;
     39 import com.android.internal.telephony.TelephonyTest;
     40 
     41 import org.junit.After;
     42 import org.junit.Before;
     43 import org.junit.Test;
     44 
     45 import java.lang.reflect.Field;
     46 import java.lang.reflect.Modifier;
     47 import java.util.ArrayList;
     48 import java.util.List;
     49 
     50 public class ApnSettingTest extends TelephonyTest {
     51 
     52     private PersistableBundle mBundle;
     53 
     54     @Before
     55     public void setUp() throws Exception {
     56         super.setUp(getClass().getSimpleName());
     57         mBundle = mContextFixture.getCarrierConfigBundle();
     58     }
     59 
     60     @After
     61     public void tearDown() throws Exception {
     62         super.tearDown();
     63     }
     64 
     65     static ApnSetting createApnSetting(String[] apnTypes) {
     66         return createApnSettingInternal(apnTypes, true);
     67     }
     68 
     69     private static ApnSetting createDisabledApnSetting(String[] apnTypes) {
     70         return createApnSettingInternal(apnTypes, false);
     71     }
     72 
     73     private static ApnSetting createApnSettingInternal(String[] apnTypes, boolean carrierEnabled) {
     74         return new ApnSetting(
     75                 2163,                   // id
     76                 "44010",                // numeric
     77                 "sp-mode",              // name
     78                 "spmode.ne.jp",         // apn
     79                 "",                     // proxy
     80                 "",                     // port
     81                 "",                     // mmsc
     82                 "",                     // mmsproxy
     83                 "",                     // mmsport
     84                 "",                     // user
     85                 "",                     // password
     86                 -1,                     // authtype
     87                 apnTypes,               // types
     88                 "IP",                   // protocol
     89                 "IP",                   // roaming_protocol
     90                 carrierEnabled,         // carrier_enabled
     91                 0,                      // bearer
     92                 0,                      // bearer_bitmask
     93                 0,                      // profile_id
     94                 false,                  // modem_cognitive
     95                 0,                      // max_conns
     96                 0,                      // wait_time
     97                 0,                      // max_conns_time
     98                 0,                      // mtu
     99                 "",                     // mvno_type
    100                 "");                    // mnvo_match_data
    101     }
    102 
    103     private static void assertApnSettingsEqual(List<ApnSetting> a1, List<ApnSetting> a2) {
    104         assertEquals(a1.size(), a2.size());
    105         for (int i = 0; i < a1.size(); ++i) {
    106             assertApnSettingEqual(a1.get(i), a2.get(i));
    107         }
    108     }
    109 
    110     private static void assertApnSettingEqual(ApnSetting a1, ApnSetting a2) {
    111         assertEquals(a1.carrier, a2.carrier);
    112         assertEquals(a1.apn, a2.apn);
    113         assertEquals(a1.proxy, a2.proxy);
    114         assertEquals(a1.port, a2.port);
    115         assertEquals(a1.mmsc, a2.mmsc);
    116         assertEquals(a1.mmsProxy, a2.mmsProxy);
    117         assertEquals(a1.mmsPort, a2.mmsPort);
    118         assertEquals(a1.user, a2.user);
    119         assertEquals(a1.password, a2.password);
    120         assertEquals(a1.authType, a2.authType);
    121         assertEquals(a1.id, a2.id);
    122         assertEquals(a1.numeric, a2.numeric);
    123         assertEquals(a1.protocol, a2.protocol);
    124         assertEquals(a1.roamingProtocol, a2.roamingProtocol);
    125         assertEquals(a1.types.length, a2.types.length);
    126         int i;
    127         for (i = 0; i < a1.types.length; i++) {
    128             assertEquals(a1.types[i], a2.types[i]);
    129         }
    130         assertEquals(a1.carrierEnabled, a2.carrierEnabled);
    131         assertEquals(a1.bearerBitmask, a2.bearerBitmask);
    132         assertEquals(a1.profileId, a2.profileId);
    133         assertEquals(a1.modemCognitive, a2.modemCognitive);
    134         assertEquals(a1.maxConns, a2.maxConns);
    135         assertEquals(a1.waitTime, a2.waitTime);
    136         assertEquals(a1.maxConnsTime, a2.maxConnsTime);
    137         assertEquals(a1.mtu, a2.mtu);
    138         assertEquals(a1.mvnoType, a2.mvnoType);
    139         assertEquals(a1.mvnoMatchData, a2.mvnoMatchData);
    140         assertEquals(a1.networkTypeBitmask, a2.networkTypeBitmask);
    141         assertEquals(a1.apnSetId, a2.apnSetId);
    142     }
    143 
    144     @Test
    145     @SmallTest
    146     public void testFromString() throws Exception {
    147         String[] dunTypes = {"DUN"};
    148         String[] mmsTypes = {"mms", "*"};
    149 
    150         ApnSetting expectedApn;
    151         String testString;
    152 
    153         // A real-world v1 example string.
    154         testString = "Vodafone IT,web.omnitel.it,,,,,,,,,222,10,,DUN";
    155         expectedApn = new ApnSetting(
    156                 -1, "22210", "Vodafone IT", "web.omnitel.it", "", "",
    157                 "", "", "", "", "", 0, dunTypes, "IP", "IP", true, 0, 0,
    158                 0, false, 0, 0, 0, 0, "", "");
    159         assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
    160 
    161         // A v2 string.
    162         testString = "[ApnSettingV2] Name,apn,,,,,,,,,123,45,,mms|*,IPV6,IP,true,14";
    163         expectedApn = new ApnSetting(
    164                 -1, "12345", "Name", "apn", "", "",
    165                 "", "", "", "", "", 0, mmsTypes, "IPV6", "IP", true, 14, 0,
    166                 0, false, 0, 0, 0, 0, "", "");
    167         assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
    168 
    169         // A v2 string with spaces.
    170         testString = "[ApnSettingV2] Name,apn, ,,,,,,,,123,45,,mms|*,IPV6, IP,true,14";
    171         expectedApn = new ApnSetting(
    172                 -1, "12345", "Name", "apn", "", "",
    173                 "", "", "", "", "", 0, mmsTypes, "IPV6", "IP", true, 14, 0,
    174                 0, false, 0, 0, 0, 0, "", "");
    175         assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
    176         int networkTypeBitmask = 1 << (13 - 1);
    177         expectedApn = new ApnSetting(
    178                 -1, "12345", "Name", "apn", "", "", "", "", "", "", "", 0, mmsTypes, "IPV6",
    179                 "IP", true, networkTypeBitmask, 0, false, 0, 0, 0, 0, "", "");
    180         assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
    181 
    182         // A v3 string.
    183         testString = "[ApnSettingV3] Name,apn,,,,,,,,,123,45,,mms|*,IPV6,IP,true,14,,,,,,,spn,testspn";
    184         expectedApn = new ApnSetting(
    185                 -1, "12345", "Name", "apn", "", "", "", "", "", "", "", 0, mmsTypes, "IPV6",
    186                 "IP", true, 14, 0, 0, false, 0, 0, 0, 0, "spn", "testspn");
    187         assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
    188 
    189         // A v4 string with network type bitmask.
    190         testString =
    191                 "[ApnSettingV4] Name,apn,,,,,,,,,123,45,,mms|*,IPV6,IP,true,0,,,,,,,spn,testspn,6";
    192         networkTypeBitmask = 1 << (6 - 1);
    193         expectedApn = new ApnSetting(
    194                 -1, "12345", "Name", "apn", "", "", "", "", "", "", "", 0, mmsTypes, "IPV6",
    195                 "IP", true, networkTypeBitmask, 0, false, 0, 0, 0, 0, "spn", "testspn");
    196         assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
    197 
    198         testString =
    199                 "[ApnSettingV4] Name,apn,,,,,,,,,123,45,,mms|*,IPV6,IP,true,0,,,,,,,spn,testspn,"
    200                         + "4|5|6|7|8|12|13|14|19";
    201         // The value was calculated by adding "4|5|6|7|8|12|13|14|19".
    202         networkTypeBitmask = 276728;
    203         expectedApn = new ApnSetting(
    204                 -1, "12345", "Name", "apn", "", "", "", "", "", "", "", 0, mmsTypes, "IPV6",
    205                 "IP", true, networkTypeBitmask, 0, false, 0, 0, 0, 0, "spn", "testspn");
    206         assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
    207 
    208         // A v4 string with network type bitmask and compatible bearer bitmask.
    209         testString =
    210                 "[ApnSettingV4] Name,apn,,,,,,,,,123,45,,mms|*,IPV6,IP,true,8,,,,,,,spn,testspn, 6";
    211         networkTypeBitmask = 1 << (6 - 1);
    212         int bearerBitmask = 1 << (8 - 1);
    213         expectedApn = new ApnSetting(
    214                 -1, "12345", "Name", "apn", "", "", "", "", "", "", "", 0, mmsTypes, "IPV6",
    215                 "IP", true, 0, bearerBitmask, 0, false, 0, 0, 0, 0, "spn",
    216                 "testspn");
    217         assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
    218         expectedApn = new ApnSetting(
    219                 -1, "12345", "Name", "apn", "", "", "", "", "", "", "", 0, mmsTypes, "IPV6",
    220                 "IP", true, networkTypeBitmask, 0, false, 0, 0, 0, 0, "spn",
    221                 "testspn");
    222         assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
    223 
    224         // A v4 string with network type bitmask and incompatible bearer bitmask.
    225         testString =
    226                 "[ApnSettingV4] Name,apn,,,,,,,,,123,45,,mms|*,IPV6,IP,true,9,,,,,,,spn,testspn, 6";
    227         bearerBitmask = 1 << (8 - 1);
    228         expectedApn = new ApnSetting(
    229                 -1, "12345", "Name", "apn", "", "", "", "", "", "", "", 0, mmsTypes, "IPV6",
    230                 "IP", true, 0, bearerBitmask, 0, false, 0, 0, 0, 0, "spn",
    231                 "testspn");
    232         assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
    233 
    234         // A v5 string with apnSetId=0
    235         testString =
    236                 "[ApnSettingV5] Name,apn,,,,,,,,,123,45,,mms|*,IPV6,IP,true,0,,,,,,,spn,testspn,0,0";
    237         expectedApn = new ApnSetting(
    238                 -1, "12345", "Name", "apn", "", "", "", "", "", "", "", 0, mmsTypes, "IPV6",
    239                 "IP", true, 0, 0, 0, false, 0, 0, 0, 0, "spn", "testspn");
    240         assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
    241 
    242         // A v5 string with apnSetId=3
    243         testString =
    244                 "[ApnSettingV5] Name,apn,,,,,,,,,123,45,,mms|*,IPV6,IP,true,0,,,,,,,spn,testspn,0,3";
    245         expectedApn = new ApnSetting(
    246                 -1, "12345", "Name", "apn", "", "", "", "", "", "", "", 0, mmsTypes, "IPV6",
    247                 "IP", true, 0, 0, false, 0, 0, 0, 0, "spn", "testspn", 3);
    248         assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
    249 
    250         // Return no apn if insufficient fields given.
    251         testString = "[ApnSettingV3] Name,apn,,,,,,,,,123, 45,,mms|*";
    252         assertEquals(null, ApnSetting.fromString(testString));
    253 
    254         testString = "Name,apn,,,,,,,,,123, 45,";
    255         assertEquals(null, ApnSetting.fromString(testString));
    256     }
    257 
    258     @Test
    259     @SmallTest
    260     public void testArrayFromString() throws Exception {
    261         // Test a multiple v3 string.
    262         String testString =
    263                 "[ApnSettingV3] Name,apn,,,,,,,,,123,45,,mms,IPV6,IP,true,14,,,,,,,spn,testspn";
    264         testString +=
    265                 " ;[ApnSettingV3] Name1,apn1,,,,,,,,,123,46,,mms,IPV6,IP,true,12,,,,,,,gid,testGid";
    266         testString +=
    267                 " ;[ApnSettingV3] Name1,apn2,,,,,,,,,123,46,,mms,IPV6,IP,true,12,,,,,,,,";
    268         testString +=
    269                 " ;[ApnSettingV5] Name1,apn2,,,,,,,,,123,46,,mms,IPV6,IP,true,0,,,,,,,,,,3";
    270         List<ApnSetting> expectedApns = new ArrayList<ApnSetting>();
    271         expectedApns.add(new ApnSetting(
    272                 -1, "12345", "Name", "apn", "", "", "", "", "", "", "", 0, new String[]{"mms"}, "IPV6",
    273                 "IP", true, 14, 0, 0, false, 0, 0, 0, 0, "spn", "testspn"));
    274         expectedApns.add(new ApnSetting(
    275                 -1, "12346", "Name1", "apn1", "", "", "", "", "", "", "", 0, new String[]{"mms"}, "IPV6",
    276                 "IP", true, 12, 0, 0, false, 0, 0, 0, 0, "gid", "testGid"));
    277         expectedApns.add(new ApnSetting(
    278                 -1, "12346", "Name1", "apn2", "", "", "", "", "", "", "", 0, new String[]{"mms"}, "IPV6",
    279                 "IP", true, 12, 0, 0, false, 0, 0, 0, 0, "", ""));
    280         expectedApns.add(new ApnSetting(
    281                 -1, "12346", "Name1", "apn2", "", "", "", "", "", "", "", 0, new String[]{"mms"}, "IPV6",
    282                 "IP", true, 0, 0, false, 0, 0, 0, 0, "", "", 3));
    283         assertApnSettingsEqual(expectedApns, ApnSetting.arrayFromString(testString));
    284     }
    285 
    286     @Test
    287     @SmallTest
    288     public void testToString() throws Exception {
    289         String[] types = {"default", "*"};
    290         // use default apn_set_id constructor
    291         ApnSetting apn = new ApnSetting(
    292                 99, "12345", "Name", "apn", "proxy", "port",
    293                 "mmsc", "mmsproxy", "mmsport", "user", "password", 0,
    294                 types, "IPV6", "IP", true, 14, 0, 0, false, 0, 0, 0, 0, "", "");
    295         String expected = "[ApnSettingV5] Name, 99, 12345, apn, proxy, "
    296                 + "mmsc, mmsproxy, mmsport, port, 0, default | *, "
    297                 + "IPV6, IP, true, 14, 8192, 0, false, 0, 0, 0, 0, , , false, 4096, 0";
    298         assertEquals(expected, apn.toString());
    299 
    300         int networkTypeBitmask = 1 << (14 - 1);
    301         int bearerBitmask =
    302                 ServiceState.convertNetworkTypeBitmaskToBearerBitmask(networkTypeBitmask);
    303         apn = new ApnSetting(99, "12345", "Name", "apn", "proxy", "port",
    304                 "mmsc", "mmsproxy", "mmsport", "user", "password", 0,
    305                 types, "IPV6", "IP", true, networkTypeBitmask, 0, false, 0, 0, 0, 0, "", "", 3);
    306         expected = "[ApnSettingV5] Name, 99, 12345, apn, proxy, "
    307                 + "mmsc, mmsproxy, mmsport, port, 0, default | *, IPV6, IP, true, 0, "
    308                 + bearerBitmask + ", 0, false, 0, 0, 0, 0, , , false, 8192, 3";
    309         assertEquals(expected, apn.toString());
    310     }
    311 
    312     @Test
    313     @SmallTest
    314     public void testIsMetered() throws Exception {
    315         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
    316                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS});
    317 
    318         doReturn(false).when(mServiceState).getDataRoaming();
    319         doReturn(1).when(mPhone).getSubId();
    320         assertTrue(createApnSetting(
    321                 new String[]{PhoneConstants.APN_TYPE_DEFAULT}).isMetered(mPhone));
    322 
    323         assertTrue(createApnSetting(
    324                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS}).
    325                 isMetered(mPhone));
    326 
    327         assertTrue(createApnSetting(
    328                 new String[]{PhoneConstants.APN_TYPE_MMS}).isMetered(mPhone));
    329 
    330         assertTrue(createApnSetting(
    331                 new String[]{PhoneConstants.APN_TYPE_MMS, PhoneConstants.APN_TYPE_SUPL}).
    332                 isMetered(mPhone));
    333 
    334         assertTrue(createApnSetting(
    335                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_DUN}).
    336                 isMetered(mPhone));
    337 
    338         assertTrue(createApnSetting(
    339                 new String[]{PhoneConstants.APN_TYPE_ALL}).isMetered(mPhone));
    340 
    341         assertFalse(createApnSetting(
    342                 new String[]{PhoneConstants.APN_TYPE_FOTA, PhoneConstants.APN_TYPE_SUPL}).
    343                 isMetered(mPhone));
    344 
    345         assertFalse(createApnSetting(
    346                 new String[]{PhoneConstants.APN_TYPE_IA, PhoneConstants.APN_TYPE_CBS}).
    347                 isMetered(mPhone));
    348 
    349         assertTrue(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_DEFAULT, mPhone));
    350         assertTrue(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_MMS, mPhone));
    351         assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_SUPL, mPhone));
    352         assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_CBS, mPhone));
    353         assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_DUN, mPhone));
    354         assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_FOTA, mPhone));
    355         assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_IA, mPhone));
    356         assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_HIPRI, mPhone));
    357 
    358         // Carrier config settings changes.
    359         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
    360                 new String[]{PhoneConstants.APN_TYPE_DEFAULT});
    361 
    362         assertTrue(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_DEFAULT, mPhone));
    363         assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_MMS, mPhone));
    364     }
    365 
    366     @Test
    367     @SmallTest
    368     public void testIsRoamingMetered() throws Exception {
    369         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_ROAMING_APN_TYPES_STRINGS,
    370                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS});
    371         doReturn(true).when(mServiceState).getDataRoaming();
    372         doReturn(1).when(mPhone).getSubId();
    373 
    374         assertTrue(createApnSetting(
    375                 new String[]{PhoneConstants.APN_TYPE_DEFAULT}).isMetered(mPhone));
    376 
    377         assertTrue(createApnSetting(
    378                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS}).
    379                 isMetered(mPhone));
    380 
    381         assertTrue(createApnSetting(
    382                 new String[]{PhoneConstants.APN_TYPE_MMS}).isMetered(mPhone));
    383 
    384         assertTrue(createApnSetting(
    385                 new String[]{PhoneConstants.APN_TYPE_MMS, PhoneConstants.APN_TYPE_SUPL}).
    386                 isMetered(mPhone));
    387 
    388         assertTrue(createApnSetting(
    389                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_DUN}).
    390                 isMetered(mPhone));
    391 
    392         assertTrue(createApnSetting(
    393                 new String[]{PhoneConstants.APN_TYPE_ALL}).isMetered(mPhone));
    394 
    395         assertFalse(createApnSetting(
    396                 new String[]{PhoneConstants.APN_TYPE_FOTA, PhoneConstants.APN_TYPE_SUPL}).
    397                 isMetered(mPhone));
    398 
    399         assertFalse(createApnSetting(
    400                 new String[]{PhoneConstants.APN_TYPE_IA, PhoneConstants.APN_TYPE_CBS}).
    401                 isMetered(mPhone));
    402 
    403         // Carrier config settings changes.
    404         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_ROAMING_APN_TYPES_STRINGS,
    405                 new String[]{PhoneConstants.APN_TYPE_FOTA});
    406 
    407         assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_DEFAULT, mPhone));
    408         assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_MMS, mPhone));
    409         assertTrue(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_FOTA, mPhone));
    410     }
    411 
    412     @Test
    413     @SmallTest
    414     public void testIsIwlanMetered() throws Exception {
    415         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_IWLAN_APN_TYPES_STRINGS,
    416                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS});
    417         doReturn(false).when(mServiceState).getDataRoaming();
    418         doReturn(ServiceState.RIL_RADIO_TECHNOLOGY_IWLAN).when(mServiceState)
    419                 .getRilDataRadioTechnology();
    420         doReturn(1).when(mPhone).getSubId();
    421 
    422         assertTrue(createApnSetting(
    423                 new String[]{PhoneConstants.APN_TYPE_DEFAULT}).isMetered(mPhone));
    424 
    425         assertTrue(createApnSetting(
    426                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS})
    427                 .isMetered(mPhone));
    428 
    429         assertTrue(createApnSetting(
    430                 new String[]{PhoneConstants.APN_TYPE_MMS}).isMetered(mPhone));
    431 
    432         assertTrue(createApnSetting(
    433                 new String[]{PhoneConstants.APN_TYPE_MMS, PhoneConstants.APN_TYPE_SUPL})
    434                 .isMetered(mPhone));
    435 
    436         assertTrue(createApnSetting(
    437                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_DUN})
    438                 .isMetered(mPhone));
    439 
    440         assertTrue(createApnSetting(
    441                 new String[]{PhoneConstants.APN_TYPE_ALL}).isMetered(mPhone));
    442 
    443         assertFalse(createApnSetting(
    444                 new String[]{PhoneConstants.APN_TYPE_FOTA, PhoneConstants.APN_TYPE_SUPL})
    445                 .isMetered(mPhone));
    446 
    447         assertFalse(createApnSetting(
    448                 new String[]{PhoneConstants.APN_TYPE_IA, PhoneConstants.APN_TYPE_CBS})
    449                 .isMetered(mPhone));
    450 
    451         // Carrier config settings changes.
    452         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_IWLAN_APN_TYPES_STRINGS,
    453                 new String[]{PhoneConstants.APN_TYPE_FOTA});
    454 
    455         assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_DEFAULT, mPhone));
    456         assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_MMS, mPhone));
    457         assertTrue(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_FOTA, mPhone));
    458     }
    459 
    460     @Test
    461     @SmallTest
    462     public void testIsMeteredAnother() throws Exception {
    463         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
    464                 new String[]{PhoneConstants.APN_TYPE_SUPL, PhoneConstants.APN_TYPE_CBS});
    465 
    466         doReturn(false).when(mServiceState).getDataRoaming();
    467         doReturn(1).when(mPhone).getSubId();
    468         assertTrue(createApnSetting(
    469                 new String[]{PhoneConstants.APN_TYPE_SUPL, PhoneConstants.APN_TYPE_CBS}).
    470                 isMetered(mPhone));
    471 
    472         assertTrue(createApnSetting(
    473                 new String[]{PhoneConstants.APN_TYPE_SUPL}).isMetered(mPhone));
    474 
    475         assertTrue(createApnSetting(
    476                 new String[]{PhoneConstants.APN_TYPE_CBS}).isMetered(mPhone));
    477 
    478         assertTrue(createApnSetting(
    479                 new String[]{PhoneConstants.APN_TYPE_FOTA, PhoneConstants.APN_TYPE_CBS}).
    480                 isMetered(mPhone));
    481 
    482         assertTrue(createApnSetting(
    483                 new String[]{PhoneConstants.APN_TYPE_SUPL, PhoneConstants.APN_TYPE_IA}).
    484                 isMetered(mPhone));
    485 
    486         assertTrue(createApnSetting(
    487                 new String[]{PhoneConstants.APN_TYPE_ALL}).isMetered(mPhone));
    488 
    489         assertFalse(createApnSetting(
    490                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_IMS}).
    491                 isMetered(mPhone));
    492 
    493         assertFalse(createApnSetting(
    494                 new String[]{PhoneConstants.APN_TYPE_IMS}).isMetered(mPhone));
    495     }
    496 
    497     @Test
    498     @SmallTest
    499     public void testIsRoamingMeteredAnother() throws Exception {
    500         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_ROAMING_APN_TYPES_STRINGS,
    501                 new String[]{PhoneConstants.APN_TYPE_SUPL, PhoneConstants.APN_TYPE_CBS});
    502         doReturn(true).when(mServiceState).getDataRoaming();
    503         doReturn(2).when(mPhone).getSubId();
    504         assertTrue(createApnSetting(
    505                 new String[]{PhoneConstants.APN_TYPE_SUPL, PhoneConstants.APN_TYPE_CBS}).
    506                 isMetered(mPhone));
    507 
    508         assertTrue(createApnSetting(
    509                 new String[]{PhoneConstants.APN_TYPE_SUPL}).isMetered(mPhone));
    510 
    511         assertTrue(createApnSetting(
    512                 new String[]{PhoneConstants.APN_TYPE_CBS}).isMetered(mPhone));
    513 
    514         assertTrue(createApnSetting(
    515                 new String[]{PhoneConstants.APN_TYPE_FOTA, PhoneConstants.APN_TYPE_CBS}).
    516                 isMetered(mPhone));
    517 
    518         assertTrue(createApnSetting(
    519                 new String[]{PhoneConstants.APN_TYPE_SUPL, PhoneConstants.APN_TYPE_IA}).
    520                 isMetered(mPhone));
    521 
    522         assertTrue(createApnSetting(
    523                 new String[]{PhoneConstants.APN_TYPE_ALL}).isMetered(mPhone));
    524 
    525         assertFalse(createApnSetting(
    526                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_IMS}).
    527                 isMetered(mPhone));
    528 
    529         assertFalse(createApnSetting(
    530                 new String[]{PhoneConstants.APN_TYPE_IMS}).isMetered(mPhone));
    531 
    532         assertTrue(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_SUPL, mPhone));
    533         assertTrue(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_CBS, mPhone));
    534         assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_DEFAULT, mPhone));
    535         assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_MMS, mPhone));
    536         assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_DUN, mPhone));
    537         assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_FOTA, mPhone));
    538         assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_IA, mPhone));
    539         assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_HIPRI, mPhone));
    540     }
    541 
    542     @Test
    543     @SmallTest
    544     public void testIsIwlanMeteredAnother() throws Exception {
    545         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_IWLAN_APN_TYPES_STRINGS,
    546                 new String[]{PhoneConstants.APN_TYPE_SUPL, PhoneConstants.APN_TYPE_CBS});
    547         doReturn(true).when(mServiceState).getDataRoaming();
    548         doReturn(ServiceState.RIL_RADIO_TECHNOLOGY_IWLAN).when(mServiceState)
    549                 .getRilDataRadioTechnology();
    550         doReturn(2).when(mPhone).getSubId();
    551         assertTrue(createApnSetting(
    552                 new String[]{PhoneConstants.APN_TYPE_SUPL, PhoneConstants.APN_TYPE_CBS})
    553                 .isMetered(mPhone));
    554 
    555         assertTrue(createApnSetting(
    556                 new String[]{PhoneConstants.APN_TYPE_SUPL}).isMetered(mPhone));
    557 
    558         assertTrue(createApnSetting(
    559                 new String[]{PhoneConstants.APN_TYPE_CBS}).isMetered(mPhone));
    560 
    561         assertTrue(createApnSetting(
    562                 new String[]{PhoneConstants.APN_TYPE_FOTA, PhoneConstants.APN_TYPE_CBS})
    563                 .isMetered(mPhone));
    564 
    565         assertTrue(createApnSetting(
    566                 new String[]{PhoneConstants.APN_TYPE_SUPL, PhoneConstants.APN_TYPE_IA})
    567                 .isMetered(mPhone));
    568 
    569         assertTrue(createApnSetting(
    570                 new String[]{PhoneConstants.APN_TYPE_ALL}).isMetered(mPhone));
    571 
    572         assertFalse(createApnSetting(
    573                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_IMS})
    574                 .isMetered(mPhone));
    575 
    576         assertFalse(createApnSetting(
    577                 new String[]{PhoneConstants.APN_TYPE_IMS}).isMetered(mPhone));
    578 
    579         assertTrue(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_SUPL, mPhone));
    580         assertTrue(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_CBS, mPhone));
    581         assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_DEFAULT, mPhone));
    582         assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_MMS, mPhone));
    583         assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_DUN, mPhone));
    584         assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_FOTA, mPhone));
    585         assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_IA, mPhone));
    586         assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_HIPRI, mPhone));
    587     }
    588 
    589     @Test
    590     @SmallTest
    591     public void testIsMeteredNothingCharged() throws Exception {
    592         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
    593                 new String[]{});
    594 
    595         doReturn(false).when(mServiceState).getDataRoaming();
    596         doReturn(3).when(mPhone).getSubId();
    597 
    598         assertFalse(createApnSetting(
    599                 new String[]{PhoneConstants.APN_TYPE_IMS}).isMetered(mPhone));
    600 
    601         assertFalse(createApnSetting(
    602                 new String[]{PhoneConstants.APN_TYPE_IMS, PhoneConstants.APN_TYPE_MMS})
    603                 .isMetered(mPhone));
    604 
    605         assertFalse(createApnSetting(
    606                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_FOTA})
    607                 .isMetered(mPhone));
    608 
    609         assertFalse(createApnSetting(
    610                 new String[]{PhoneConstants.APN_TYPE_ALL}).isMetered(mPhone));
    611     }
    612 
    613     @Test
    614     @SmallTest
    615     public void testIsRoamingMeteredNothingCharged() throws Exception {
    616         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_ROAMING_APN_TYPES_STRINGS,
    617                 new String[]{});
    618         doReturn(true).when(mServiceState).getDataRoaming();
    619         doReturn(3).when(mPhone).getSubId();
    620 
    621         assertFalse(createApnSetting(
    622                 new String[]{PhoneConstants.APN_TYPE_IMS}).isMetered(mPhone));
    623 
    624         assertFalse(createApnSetting(
    625                 new String[]{PhoneConstants.APN_TYPE_IMS, PhoneConstants.APN_TYPE_MMS}).
    626                 isMetered(mPhone));
    627 
    628         assertFalse(createApnSetting(
    629                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_FOTA}).
    630                 isMetered(mPhone));
    631 
    632         assertFalse(createApnSetting(
    633                 new String[]{PhoneConstants.APN_TYPE_ALL}).
    634                 isMetered(mPhone));
    635     }
    636 
    637     @Test
    638     @SmallTest
    639     public void testIsIwlanMeteredNothingCharged() throws Exception {
    640         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_IWLAN_APN_TYPES_STRINGS,
    641                 new String[]{});
    642         doReturn(true).when(mServiceState).getDataRoaming();
    643         doReturn(ServiceState.RIL_RADIO_TECHNOLOGY_IWLAN).when(mServiceState)
    644                 .getRilDataRadioTechnology();
    645         doReturn(3).when(mPhone).getSubId();
    646 
    647         assertFalse(createApnSetting(
    648                 new String[]{PhoneConstants.APN_TYPE_IMS}).isMetered(mPhone));
    649 
    650         assertFalse(createApnSetting(
    651                 new String[]{PhoneConstants.APN_TYPE_IMS, PhoneConstants.APN_TYPE_MMS})
    652                 .isMetered(mPhone));
    653 
    654         assertFalse(createApnSetting(
    655                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_FOTA})
    656                 .isMetered(mPhone));
    657 
    658         assertFalse(createApnSetting(
    659                 new String[]{PhoneConstants.APN_TYPE_ALL}).isMetered(mPhone));
    660     }
    661 
    662     @Test
    663     @SmallTest
    664     public void testIsMeteredNothingFree() throws Exception {
    665         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
    666                 new String[]{PhoneConstants.APN_TYPE_ALL});
    667 
    668         doReturn(false).when(mServiceState).getDataRoaming();
    669         doReturn(4).when(mPhone).getSubId();
    670 
    671         assertTrue(createApnSetting(
    672                 new String[]{PhoneConstants.APN_TYPE_ALL}).
    673                 isMetered(mPhone));
    674 
    675         assertTrue(createApnSetting(
    676                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS}).
    677                 isMetered(mPhone));
    678 
    679         assertTrue(createApnSetting(
    680                 new String[]{PhoneConstants.APN_TYPE_FOTA, PhoneConstants.APN_TYPE_CBS}).
    681                 isMetered(mPhone));
    682 
    683         assertTrue(createApnSetting(
    684                 new String[]{PhoneConstants.APN_TYPE_IA, PhoneConstants.APN_TYPE_DUN}).
    685                 isMetered(mPhone));
    686 
    687     }
    688 
    689     @Test
    690     @SmallTest
    691     public void testIsRoamingMeteredNothingFree() throws Exception {
    692         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_ROAMING_APN_TYPES_STRINGS,
    693                 new String[]{PhoneConstants.APN_TYPE_ALL});
    694 
    695         doReturn(true).when(mServiceState).getDataRoaming();
    696         doReturn(4).when(mPhone).getSubId();
    697 
    698         assertTrue(createApnSetting(
    699                 new String[]{PhoneConstants.APN_TYPE_ALL}).isMetered(mPhone));
    700 
    701         assertTrue(createApnSetting(
    702                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS})
    703                 .isMetered(mPhone));
    704 
    705         assertTrue(createApnSetting(
    706                 new String[]{PhoneConstants.APN_TYPE_FOTA, PhoneConstants.APN_TYPE_CBS})
    707                 .isMetered(mPhone));
    708 
    709         assertTrue(createApnSetting(
    710                 new String[]{PhoneConstants.APN_TYPE_IA, PhoneConstants.APN_TYPE_DUN})
    711                 .isMetered(mPhone));
    712     }
    713 
    714     @Test
    715     @SmallTest
    716     public void testIsIwlanMeteredNothingFree() throws Exception {
    717         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_IWLAN_APN_TYPES_STRINGS,
    718                 new String[]{PhoneConstants.APN_TYPE_ALL});
    719 
    720         doReturn(false).when(mServiceState).getDataRoaming();
    721         doReturn(ServiceState.RIL_RADIO_TECHNOLOGY_IWLAN).when(mServiceState)
    722                 .getRilDataRadioTechnology();
    723         doReturn(4).when(mPhone).getSubId();
    724 
    725         assertTrue(createApnSetting(
    726                 new String[]{PhoneConstants.APN_TYPE_ALL}).isMetered(mPhone));
    727 
    728         assertTrue(createApnSetting(
    729                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS}).
    730                 isMetered(mPhone));
    731 
    732         assertTrue(createApnSetting(
    733                 new String[]{PhoneConstants.APN_TYPE_FOTA, PhoneConstants.APN_TYPE_CBS}).
    734                 isMetered(mPhone));
    735 
    736         assertTrue(createApnSetting(
    737                 new String[]{PhoneConstants.APN_TYPE_IA, PhoneConstants.APN_TYPE_DUN}).
    738                 isMetered(mPhone));
    739     }
    740 
    741     @Test
    742     @SmallTest
    743     public void testCanHandleType() throws Exception {
    744         String types[] = {"mms"};
    745 
    746         // empty string replaced with ALL ('*') when loaded to db
    747         assertFalse(createApnSetting(new String[]{}).
    748                 canHandleType(APN_TYPE_MMS));
    749 
    750         assertTrue(createApnSetting(new String[]{APN_TYPE_ALL}).
    751                 canHandleType(APN_TYPE_MMS));
    752 
    753         assertFalse(createApnSetting(new String[]{APN_TYPE_DEFAULT}).
    754                 canHandleType(APN_TYPE_MMS));
    755 
    756         assertTrue(createApnSetting(new String[]{"DEfAULT"}).
    757                 canHandleType("defAult"));
    758 
    759         // Hipri is asymmetric
    760         assertTrue(createApnSetting(new String[]{APN_TYPE_DEFAULT}).
    761                 canHandleType(APN_TYPE_HIPRI));
    762         assertFalse(createApnSetting(new String[]{APN_TYPE_HIPRI}).
    763                 canHandleType(APN_TYPE_DEFAULT));
    764 
    765 
    766         assertTrue(createApnSetting(new String[]{APN_TYPE_DEFAULT, APN_TYPE_MMS}).
    767                 canHandleType(APN_TYPE_DEFAULT));
    768 
    769         assertTrue(createApnSetting(new String[]{APN_TYPE_DEFAULT, APN_TYPE_MMS}).
    770                 canHandleType(APN_TYPE_MMS));
    771 
    772         assertFalse(createApnSetting(new String[]{APN_TYPE_DEFAULT, APN_TYPE_MMS}).
    773                 canHandleType(APN_TYPE_SUPL));
    774 
    775         // special IA case - doesn't match wildcards
    776         assertFalse(createApnSetting(new String[]{APN_TYPE_DEFAULT, APN_TYPE_MMS}).
    777                 canHandleType(APN_TYPE_IA));
    778         assertFalse(createApnSetting(new String[]{APN_TYPE_ALL}).
    779                 canHandleType(APN_TYPE_IA));
    780         assertFalse(createApnSetting(new String[]{APN_TYPE_ALL}).
    781                 canHandleType("iA"));
    782         assertTrue(createApnSetting(new String[]{APN_TYPE_DEFAULT, APN_TYPE_MMS, APN_TYPE_IA}).
    783                 canHandleType(APN_TYPE_IA));
    784 
    785         // check carrier disabled
    786         assertFalse(createDisabledApnSetting(new String[]{APN_TYPE_ALL}).
    787                 canHandleType(APN_TYPE_MMS));
    788         assertFalse(createDisabledApnSetting(new String[]{"DEfAULT"}).
    789                 canHandleType("defAult"));
    790         assertFalse(createDisabledApnSetting(new String[]{APN_TYPE_DEFAULT}).
    791                 canHandleType(APN_TYPE_HIPRI));
    792         assertFalse(createDisabledApnSetting(new String[]{APN_TYPE_DEFAULT, APN_TYPE_MMS}).
    793                 canHandleType(APN_TYPE_DEFAULT));
    794         assertFalse(createDisabledApnSetting(new String[]{APN_TYPE_DEFAULT, APN_TYPE_MMS}).
    795                 canHandleType(APN_TYPE_MMS));
    796         assertFalse(createDisabledApnSetting(new String[]
    797                 {APN_TYPE_DEFAULT, APN_TYPE_MMS, APN_TYPE_IA}).
    798                 canHandleType(APN_TYPE_IA));
    799     }
    800 
    801     @Test
    802     @SmallTest
    803     public void testEquals() throws Exception {
    804         final int dummyInt = 1;
    805         final String dummyString = "dummy";
    806         final String[] dummyStringArr = new String[] {"dummy"};
    807         // base apn
    808         ApnSetting baseApn = createApnSetting(new String[] {"mms", "default"});
    809         Field[] fields = ApnSetting.class.getDeclaredFields();
    810         for (Field f : fields) {
    811             int modifiers = f.getModifiers();
    812             if (Modifier.isStatic(modifiers) || !Modifier.isFinal(modifiers)) {
    813                 continue;
    814             }
    815             f.setAccessible(true);
    816             ApnSetting testApn = null;
    817             if (int.class.equals(f.getType())) {
    818                 testApn = new ApnSetting(baseApn);
    819                 f.setInt(testApn, dummyInt + f.getInt(testApn));
    820             } else if (boolean.class.equals(f.getType())) {
    821                 testApn = new ApnSetting(baseApn);
    822                 f.setBoolean(testApn, !f.getBoolean(testApn));
    823             } else if (String.class.equals(f.getType())) {
    824                 testApn = new ApnSetting(baseApn);
    825                 f.set(testApn, dummyString);
    826             } else if (String[].class.equals(f.getType())) {
    827                 testApn = new ApnSetting(baseApn);
    828                 f.set(testApn, dummyStringArr);
    829             } else {
    830                 fail("Unsupported field:" + f.getName());
    831             }
    832             if (testApn != null) {
    833                 assertFalse(f.getName() + " is NOT checked", testApn.equals(baseApn));
    834             }
    835         }
    836     }
    837 
    838     @Test
    839     @SmallTest
    840     public void testEqualsRoamingProtocol() throws Exception {
    841         ApnSetting apn1 = new ApnSetting(
    842                 1234,
    843                 "310260",
    844                 "",
    845                 "ims",
    846                 "",
    847                 "",
    848                 "",
    849                 "",
    850                 "",
    851                 "",
    852                 "",
    853                 -1,
    854                  new String[]{"ims"},
    855                 "IPV6",
    856                 "",
    857                 true,
    858                 0,
    859                 131071,
    860                 0,
    861                 false,
    862                 0,
    863                 0,
    864                 0,
    865                 1440,
    866                 "",
    867                 "");
    868 
    869         ApnSetting apn2 = new ApnSetting(
    870                 1235,
    871                 "310260",
    872                 "",
    873                 "ims",
    874                 "",
    875                 "",
    876                 "",
    877                 "",
    878                 "",
    879                 "",
    880                 "",
    881                 -1,
    882                 new String[]{"ims"},
    883                 "IPV6",
    884                 "IPV6",
    885                 true,
    886                 0,
    887                 131072,
    888                 0,
    889                 false,
    890                 0,
    891                 0,
    892                 0,
    893                 1440,
    894                 "",
    895                 "");
    896 
    897         assertTrue(apn1.equals(apn2, false));
    898         assertFalse(apn1.equals(apn2, true));
    899     }
    900 }
    901