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
17package com.android.internal.telephony.dataconnection;
18
19import static com.android.internal.telephony.PhoneConstants.APN_TYPE_ALL;
20import static com.android.internal.telephony.PhoneConstants.APN_TYPE_DEFAULT;
21import static com.android.internal.telephony.PhoneConstants.APN_TYPE_HIPRI;
22import static com.android.internal.telephony.PhoneConstants.APN_TYPE_IA;
23import static com.android.internal.telephony.PhoneConstants.APN_TYPE_MMS;
24import static com.android.internal.telephony.PhoneConstants.APN_TYPE_SUPL;
25
26import static junit.framework.Assert.assertFalse;
27import static junit.framework.Assert.assertTrue;
28import static junit.framework.Assert.fail;
29
30import static org.junit.Assert.assertEquals;
31import static org.mockito.Mockito.doReturn;
32
33import android.os.PersistableBundle;
34import android.telephony.CarrierConfigManager;
35import android.telephony.ServiceState;
36import android.test.suitebuilder.annotation.SmallTest;
37
38import com.android.internal.telephony.PhoneConstants;
39import com.android.internal.telephony.TelephonyTest;
40
41import org.junit.After;
42import org.junit.Before;
43import org.junit.Test;
44
45import java.lang.reflect.Field;
46import java.lang.reflect.Modifier;
47import java.util.ArrayList;
48import java.util.List;
49
50public 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    }
141
142    @Test
143    @SmallTest
144    public void testFromString() throws Exception {
145        String[] dunTypes = {"DUN"};
146        String[] mmsTypes = {"mms", "*"};
147
148        ApnSetting expectedApn;
149        String testString;
150
151        // A real-world v1 example string.
152        testString = "Vodafone IT,web.omnitel.it,,,,,,,,,222,10,,DUN";
153        expectedApn = new ApnSetting(
154                -1, "22210", "Vodafone IT", "web.omnitel.it", "", "",
155                "", "", "", "", "", 0, dunTypes, "IP", "IP", true, 0, 0,
156                0, false, 0, 0, 0, 0, "", "");
157        assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
158
159        // A v2 string.
160        testString = "[ApnSettingV2] Name,apn,,,,,,,,,123,45,,mms|*,IPV6,IP,true,14";
161        expectedApn = new ApnSetting(
162                -1, "12345", "Name", "apn", "", "",
163                "", "", "", "", "", 0, mmsTypes, "IPV6", "IP", true, 14, 0,
164                0, false, 0, 0, 0, 0, "", "");
165        assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
166
167        // A v2 string with spaces.
168        testString = "[ApnSettingV2] Name,apn, ,,,,,,,,123,45,,mms|*,IPV6, IP,true,14";
169        expectedApn = new ApnSetting(
170                -1, "12345", "Name", "apn", "", "",
171                "", "", "", "", "", 0, mmsTypes, "IPV6", "IP", true, 14, 0,
172                0, false, 0, 0, 0, 0, "", "");
173        assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
174
175        // A v3 string.
176        testString = "[ApnSettingV3] Name,apn,,,,,,,,,123,45,,mms|*,IPV6,IP,true,14,,,,,,,spn,testspn";
177        expectedApn = new ApnSetting(
178                -1, "12345", "Name", "apn", "", "", "", "", "", "", "", 0, mmsTypes, "IPV6",
179                "IP", true, 14, 0, 0, false, 0, 0, 0, 0, "spn", "testspn");
180        assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
181
182        // Return no apn if insufficient fields given.
183        testString = "[ApnSettingV3] Name,apn,,,,,,,,,123, 45,,mms|*";
184        assertEquals(null, ApnSetting.fromString(testString));
185
186        testString = "Name,apn,,,,,,,,,123, 45,";
187        assertEquals(null, ApnSetting.fromString(testString));
188    }
189
190    @Test
191    @SmallTest
192    public void testArrayFromString() throws Exception {
193        // Test a multiple v3 string.
194        String testString =
195                "[ApnSettingV3] Name,apn,,,,,,,,,123,45,,mms,IPV6,IP,true,14,,,,,,,spn,testspn";
196        testString +=
197                " ;[ApnSettingV3] Name1,apn1,,,,,,,,,123,46,,mms,IPV6,IP,true,12,,,,,,,gid,testGid";
198        testString +=
199                " ;[ApnSettingV3] Name1,apn2,,,,,,,,,123,46,,mms,IPV6,IP,true,12,,,,,,,,";
200        List<ApnSetting> expectedApns = new ArrayList<ApnSetting>();
201        expectedApns.add(new ApnSetting(
202                -1, "12345", "Name", "apn", "", "", "", "", "", "", "", 0, new String[]{"mms"}, "IPV6",
203                "IP", true, 14, 0, 0, false, 0, 0, 0, 0, "spn", "testspn"));
204        expectedApns.add(new ApnSetting(
205                -1, "12346", "Name1", "apn1", "", "", "", "", "", "", "", 0, new String[]{"mms"}, "IPV6",
206                "IP", true, 12, 0, 0, false, 0, 0, 0, 0, "gid", "testGid"));
207        expectedApns.add(new ApnSetting(
208                -1, "12346", "Name1", "apn2", "", "", "", "", "", "", "", 0, new String[]{"mms"}, "IPV6",
209                "IP", true, 12, 0, 0, false, 0, 0, 0, 0, "", ""));
210        assertApnSettingsEqual(expectedApns, ApnSetting.arrayFromString(testString));
211    }
212
213    @Test
214    @SmallTest
215    public void testToString() throws Exception {
216        String[] types = {"default", "*"};
217        ApnSetting apn = new ApnSetting(
218                99, "12345", "Name", "apn", "proxy", "port",
219                "mmsc", "mmsproxy", "mmsport", "user", "password", 0,
220                types, "IPV6", "IP", true, 14, 0, 0, false, 0, 0, 0, 0, "", "");
221        String expected = "[ApnSettingV3] Name, 99, 12345, apn, proxy, " +
222                "mmsc, mmsproxy, mmsport, port, 0, default | *, " +
223                "IPV6, IP, true, 14, 8192, 0, false, 0, 0, 0, 0, , , false";
224        assertEquals(expected, apn.toString());
225    }
226
227    @Test
228    @SmallTest
229    public void testIsMetered() throws Exception {
230        mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
231                new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS});
232
233        doReturn(false).when(mServiceState).getDataRoaming();
234        doReturn(1).when(mPhone).getSubId();
235        assertTrue(createApnSetting(
236                new String[]{PhoneConstants.APN_TYPE_DEFAULT}).isMetered(mPhone));
237
238        assertTrue(createApnSetting(
239                new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS}).
240                isMetered(mPhone));
241
242        assertTrue(createApnSetting(
243                new String[]{PhoneConstants.APN_TYPE_MMS}).isMetered(mPhone));
244
245        assertTrue(createApnSetting(
246                new String[]{PhoneConstants.APN_TYPE_MMS, PhoneConstants.APN_TYPE_SUPL}).
247                isMetered(mPhone));
248
249        assertTrue(createApnSetting(
250                new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_DUN}).
251                isMetered(mPhone));
252
253        assertTrue(createApnSetting(
254                new String[]{PhoneConstants.APN_TYPE_ALL}).isMetered(mPhone));
255
256        assertFalse(createApnSetting(
257                new String[]{PhoneConstants.APN_TYPE_FOTA, PhoneConstants.APN_TYPE_SUPL}).
258                isMetered(mPhone));
259
260        assertFalse(createApnSetting(
261                new String[]{PhoneConstants.APN_TYPE_IA, PhoneConstants.APN_TYPE_CBS}).
262                isMetered(mPhone));
263
264        assertTrue(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_DEFAULT, mPhone));
265        assertTrue(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_MMS, mPhone));
266        assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_SUPL, mPhone));
267        assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_CBS, mPhone));
268        assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_DUN, mPhone));
269        assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_FOTA, mPhone));
270        assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_IA, mPhone));
271        assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_HIPRI, mPhone));
272
273        // Carrier config settings changes.
274        mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
275                new String[]{PhoneConstants.APN_TYPE_DEFAULT});
276
277        assertTrue(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_DEFAULT, mPhone));
278        assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_MMS, mPhone));
279    }
280
281    @Test
282    @SmallTest
283    public void testIsRoamingMetered() throws Exception {
284        mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_ROAMING_APN_TYPES_STRINGS,
285                new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS});
286        doReturn(true).when(mServiceState).getDataRoaming();
287        doReturn(1).when(mPhone).getSubId();
288
289        assertTrue(createApnSetting(
290                new String[]{PhoneConstants.APN_TYPE_DEFAULT}).isMetered(mPhone));
291
292        assertTrue(createApnSetting(
293                new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS}).
294                isMetered(mPhone));
295
296        assertTrue(createApnSetting(
297                new String[]{PhoneConstants.APN_TYPE_MMS}).isMetered(mPhone));
298
299        assertTrue(createApnSetting(
300                new String[]{PhoneConstants.APN_TYPE_MMS, PhoneConstants.APN_TYPE_SUPL}).
301                isMetered(mPhone));
302
303        assertTrue(createApnSetting(
304                new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_DUN}).
305                isMetered(mPhone));
306
307        assertTrue(createApnSetting(
308                new String[]{PhoneConstants.APN_TYPE_ALL}).isMetered(mPhone));
309
310        assertFalse(createApnSetting(
311                new String[]{PhoneConstants.APN_TYPE_FOTA, PhoneConstants.APN_TYPE_SUPL}).
312                isMetered(mPhone));
313
314        assertFalse(createApnSetting(
315                new String[]{PhoneConstants.APN_TYPE_IA, PhoneConstants.APN_TYPE_CBS}).
316                isMetered(mPhone));
317
318        // Carrier config settings changes.
319        mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_ROAMING_APN_TYPES_STRINGS,
320                new String[]{PhoneConstants.APN_TYPE_FOTA});
321
322        assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_DEFAULT, mPhone));
323        assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_MMS, mPhone));
324        assertTrue(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_FOTA, mPhone));
325    }
326
327    @Test
328    @SmallTest
329    public void testIsIwlanMetered() throws Exception {
330        mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_IWLAN_APN_TYPES_STRINGS,
331                new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS});
332        doReturn(false).when(mServiceState).getDataRoaming();
333        doReturn(ServiceState.RIL_RADIO_TECHNOLOGY_IWLAN).when(mServiceState)
334                .getRilDataRadioTechnology();
335        doReturn(1).when(mPhone).getSubId();
336
337        assertTrue(createApnSetting(
338                new String[]{PhoneConstants.APN_TYPE_DEFAULT}).isMetered(mPhone));
339
340        assertTrue(createApnSetting(
341                new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS})
342                .isMetered(mPhone));
343
344        assertTrue(createApnSetting(
345                new String[]{PhoneConstants.APN_TYPE_MMS}).isMetered(mPhone));
346
347        assertTrue(createApnSetting(
348                new String[]{PhoneConstants.APN_TYPE_MMS, PhoneConstants.APN_TYPE_SUPL})
349                .isMetered(mPhone));
350
351        assertTrue(createApnSetting(
352                new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_DUN})
353                .isMetered(mPhone));
354
355        assertTrue(createApnSetting(
356                new String[]{PhoneConstants.APN_TYPE_ALL}).isMetered(mPhone));
357
358        assertFalse(createApnSetting(
359                new String[]{PhoneConstants.APN_TYPE_FOTA, PhoneConstants.APN_TYPE_SUPL})
360                .isMetered(mPhone));
361
362        assertFalse(createApnSetting(
363                new String[]{PhoneConstants.APN_TYPE_IA, PhoneConstants.APN_TYPE_CBS})
364                .isMetered(mPhone));
365
366        // Carrier config settings changes.
367        mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_IWLAN_APN_TYPES_STRINGS,
368                new String[]{PhoneConstants.APN_TYPE_FOTA});
369
370        assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_DEFAULT, mPhone));
371        assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_MMS, mPhone));
372        assertTrue(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_FOTA, mPhone));
373    }
374
375    @Test
376    @SmallTest
377    public void testIsMeteredAnother() throws Exception {
378        mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
379                new String[]{PhoneConstants.APN_TYPE_SUPL, PhoneConstants.APN_TYPE_CBS});
380
381        doReturn(false).when(mServiceState).getDataRoaming();
382        doReturn(1).when(mPhone).getSubId();
383        assertTrue(createApnSetting(
384                new String[]{PhoneConstants.APN_TYPE_SUPL, PhoneConstants.APN_TYPE_CBS}).
385                isMetered(mPhone));
386
387        assertTrue(createApnSetting(
388                new String[]{PhoneConstants.APN_TYPE_SUPL}).isMetered(mPhone));
389
390        assertTrue(createApnSetting(
391                new String[]{PhoneConstants.APN_TYPE_CBS}).isMetered(mPhone));
392
393        assertTrue(createApnSetting(
394                new String[]{PhoneConstants.APN_TYPE_FOTA, PhoneConstants.APN_TYPE_CBS}).
395                isMetered(mPhone));
396
397        assertTrue(createApnSetting(
398                new String[]{PhoneConstants.APN_TYPE_SUPL, PhoneConstants.APN_TYPE_IA}).
399                isMetered(mPhone));
400
401        assertTrue(createApnSetting(
402                new String[]{PhoneConstants.APN_TYPE_ALL}).isMetered(mPhone));
403
404        assertFalse(createApnSetting(
405                new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_IMS}).
406                isMetered(mPhone));
407
408        assertFalse(createApnSetting(
409                new String[]{PhoneConstants.APN_TYPE_IMS}).isMetered(mPhone));
410    }
411
412    @Test
413    @SmallTest
414    public void testIsRoamingMeteredAnother() throws Exception {
415        mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_ROAMING_APN_TYPES_STRINGS,
416                new String[]{PhoneConstants.APN_TYPE_SUPL, PhoneConstants.APN_TYPE_CBS});
417        doReturn(true).when(mServiceState).getDataRoaming();
418        doReturn(2).when(mPhone).getSubId();
419        assertTrue(createApnSetting(
420                new String[]{PhoneConstants.APN_TYPE_SUPL, PhoneConstants.APN_TYPE_CBS}).
421                isMetered(mPhone));
422
423        assertTrue(createApnSetting(
424                new String[]{PhoneConstants.APN_TYPE_SUPL}).isMetered(mPhone));
425
426        assertTrue(createApnSetting(
427                new String[]{PhoneConstants.APN_TYPE_CBS}).isMetered(mPhone));
428
429        assertTrue(createApnSetting(
430                new String[]{PhoneConstants.APN_TYPE_FOTA, PhoneConstants.APN_TYPE_CBS}).
431                isMetered(mPhone));
432
433        assertTrue(createApnSetting(
434                new String[]{PhoneConstants.APN_TYPE_SUPL, PhoneConstants.APN_TYPE_IA}).
435                isMetered(mPhone));
436
437        assertTrue(createApnSetting(
438                new String[]{PhoneConstants.APN_TYPE_ALL}).isMetered(mPhone));
439
440        assertFalse(createApnSetting(
441                new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_IMS}).
442                isMetered(mPhone));
443
444        assertFalse(createApnSetting(
445                new String[]{PhoneConstants.APN_TYPE_IMS}).isMetered(mPhone));
446
447        assertTrue(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_SUPL, mPhone));
448        assertTrue(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_CBS, mPhone));
449        assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_DEFAULT, mPhone));
450        assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_MMS, mPhone));
451        assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_DUN, mPhone));
452        assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_FOTA, mPhone));
453        assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_IA, mPhone));
454        assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_HIPRI, mPhone));
455    }
456
457    @Test
458    @SmallTest
459    public void testIsIwlanMeteredAnother() throws Exception {
460        mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_IWLAN_APN_TYPES_STRINGS,
461                new String[]{PhoneConstants.APN_TYPE_SUPL, PhoneConstants.APN_TYPE_CBS});
462        doReturn(true).when(mServiceState).getDataRoaming();
463        doReturn(ServiceState.RIL_RADIO_TECHNOLOGY_IWLAN).when(mServiceState)
464                .getRilDataRadioTechnology();
465        doReturn(2).when(mPhone).getSubId();
466        assertTrue(createApnSetting(
467                new String[]{PhoneConstants.APN_TYPE_SUPL, PhoneConstants.APN_TYPE_CBS})
468                .isMetered(mPhone));
469
470        assertTrue(createApnSetting(
471                new String[]{PhoneConstants.APN_TYPE_SUPL}).isMetered(mPhone));
472
473        assertTrue(createApnSetting(
474                new String[]{PhoneConstants.APN_TYPE_CBS}).isMetered(mPhone));
475
476        assertTrue(createApnSetting(
477                new String[]{PhoneConstants.APN_TYPE_FOTA, PhoneConstants.APN_TYPE_CBS})
478                .isMetered(mPhone));
479
480        assertTrue(createApnSetting(
481                new String[]{PhoneConstants.APN_TYPE_SUPL, PhoneConstants.APN_TYPE_IA})
482                .isMetered(mPhone));
483
484        assertTrue(createApnSetting(
485                new String[]{PhoneConstants.APN_TYPE_ALL}).isMetered(mPhone));
486
487        assertFalse(createApnSetting(
488                new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_IMS})
489                .isMetered(mPhone));
490
491        assertFalse(createApnSetting(
492                new String[]{PhoneConstants.APN_TYPE_IMS}).isMetered(mPhone));
493
494        assertTrue(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_SUPL, mPhone));
495        assertTrue(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_CBS, mPhone));
496        assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_DEFAULT, mPhone));
497        assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_MMS, mPhone));
498        assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_DUN, mPhone));
499        assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_FOTA, mPhone));
500        assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_IA, mPhone));
501        assertFalse(ApnSetting.isMeteredApnType(PhoneConstants.APN_TYPE_HIPRI, mPhone));
502    }
503
504    @Test
505    @SmallTest
506    public void testIsMeteredNothingCharged() throws Exception {
507        mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
508                new String[]{});
509
510        doReturn(false).when(mServiceState).getDataRoaming();
511        doReturn(3).when(mPhone).getSubId();
512
513        assertFalse(createApnSetting(
514                new String[]{PhoneConstants.APN_TYPE_IMS}).isMetered(mPhone));
515
516        assertFalse(createApnSetting(
517                new String[]{PhoneConstants.APN_TYPE_IMS, PhoneConstants.APN_TYPE_MMS})
518                .isMetered(mPhone));
519
520        assertFalse(createApnSetting(
521                new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_FOTA})
522                .isMetered(mPhone));
523
524        assertFalse(createApnSetting(
525                new String[]{PhoneConstants.APN_TYPE_ALL}).isMetered(mPhone));
526    }
527
528    @Test
529    @SmallTest
530    public void testIsRoamingMeteredNothingCharged() throws Exception {
531        mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_ROAMING_APN_TYPES_STRINGS,
532                new String[]{});
533        doReturn(true).when(mServiceState).getDataRoaming();
534        doReturn(3).when(mPhone).getSubId();
535
536        assertFalse(createApnSetting(
537                new String[]{PhoneConstants.APN_TYPE_IMS}).isMetered(mPhone));
538
539        assertFalse(createApnSetting(
540                new String[]{PhoneConstants.APN_TYPE_IMS, PhoneConstants.APN_TYPE_MMS}).
541                isMetered(mPhone));
542
543        assertFalse(createApnSetting(
544                new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_FOTA}).
545                isMetered(mPhone));
546
547        assertFalse(createApnSetting(
548                new String[]{PhoneConstants.APN_TYPE_ALL}).
549                isMetered(mPhone));
550    }
551
552    @Test
553    @SmallTest
554    public void testIsIwlanMeteredNothingCharged() throws Exception {
555        mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_IWLAN_APN_TYPES_STRINGS,
556                new String[]{});
557        doReturn(true).when(mServiceState).getDataRoaming();
558        doReturn(ServiceState.RIL_RADIO_TECHNOLOGY_IWLAN).when(mServiceState)
559                .getRilDataRadioTechnology();
560        doReturn(3).when(mPhone).getSubId();
561
562        assertFalse(createApnSetting(
563                new String[]{PhoneConstants.APN_TYPE_IMS}).isMetered(mPhone));
564
565        assertFalse(createApnSetting(
566                new String[]{PhoneConstants.APN_TYPE_IMS, PhoneConstants.APN_TYPE_MMS})
567                .isMetered(mPhone));
568
569        assertFalse(createApnSetting(
570                new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_FOTA})
571                .isMetered(mPhone));
572
573        assertFalse(createApnSetting(
574                new String[]{PhoneConstants.APN_TYPE_ALL}).isMetered(mPhone));
575    }
576
577    @Test
578    @SmallTest
579    public void testIsMeteredNothingFree() throws Exception {
580        mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
581                new String[]{PhoneConstants.APN_TYPE_ALL});
582
583        doReturn(false).when(mServiceState).getDataRoaming();
584        doReturn(4).when(mPhone).getSubId();
585
586        assertTrue(createApnSetting(
587                new String[]{PhoneConstants.APN_TYPE_ALL}).
588                isMetered(mPhone));
589
590        assertTrue(createApnSetting(
591                new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS}).
592                isMetered(mPhone));
593
594        assertTrue(createApnSetting(
595                new String[]{PhoneConstants.APN_TYPE_FOTA, PhoneConstants.APN_TYPE_CBS}).
596                isMetered(mPhone));
597
598        assertTrue(createApnSetting(
599                new String[]{PhoneConstants.APN_TYPE_IA, PhoneConstants.APN_TYPE_DUN}).
600                isMetered(mPhone));
601
602    }
603
604    @Test
605    @SmallTest
606    public void testIsRoamingMeteredNothingFree() throws Exception {
607        mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_ROAMING_APN_TYPES_STRINGS,
608                new String[]{PhoneConstants.APN_TYPE_ALL});
609
610        doReturn(true).when(mServiceState).getDataRoaming();
611        doReturn(4).when(mPhone).getSubId();
612
613        assertTrue(createApnSetting(
614                new String[]{PhoneConstants.APN_TYPE_ALL}).isMetered(mPhone));
615
616        assertTrue(createApnSetting(
617                new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS})
618                .isMetered(mPhone));
619
620        assertTrue(createApnSetting(
621                new String[]{PhoneConstants.APN_TYPE_FOTA, PhoneConstants.APN_TYPE_CBS})
622                .isMetered(mPhone));
623
624        assertTrue(createApnSetting(
625                new String[]{PhoneConstants.APN_TYPE_IA, PhoneConstants.APN_TYPE_DUN})
626                .isMetered(mPhone));
627    }
628
629    @Test
630    @SmallTest
631    public void testIsIwlanMeteredNothingFree() throws Exception {
632        mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_IWLAN_APN_TYPES_STRINGS,
633                new String[]{PhoneConstants.APN_TYPE_ALL});
634
635        doReturn(false).when(mServiceState).getDataRoaming();
636        doReturn(ServiceState.RIL_RADIO_TECHNOLOGY_IWLAN).when(mServiceState)
637                .getRilDataRadioTechnology();
638        doReturn(4).when(mPhone).getSubId();
639
640        assertTrue(createApnSetting(
641                new String[]{PhoneConstants.APN_TYPE_ALL}).isMetered(mPhone));
642
643        assertTrue(createApnSetting(
644                new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS}).
645                isMetered(mPhone));
646
647        assertTrue(createApnSetting(
648                new String[]{PhoneConstants.APN_TYPE_FOTA, PhoneConstants.APN_TYPE_CBS}).
649                isMetered(mPhone));
650
651        assertTrue(createApnSetting(
652                new String[]{PhoneConstants.APN_TYPE_IA, PhoneConstants.APN_TYPE_DUN}).
653                isMetered(mPhone));
654    }
655
656    @Test
657    @SmallTest
658    public void testCanHandleType() throws Exception {
659        String types[] = {"mms"};
660
661        // empty string replaced with ALL ('*') when loaded to db
662        assertFalse(createApnSetting(new String[]{}).
663                canHandleType(APN_TYPE_MMS));
664
665        assertTrue(createApnSetting(new String[]{APN_TYPE_ALL}).
666                canHandleType(APN_TYPE_MMS));
667
668        assertFalse(createApnSetting(new String[]{APN_TYPE_DEFAULT}).
669                canHandleType(APN_TYPE_MMS));
670
671        assertTrue(createApnSetting(new String[]{"DEfAULT"}).
672                canHandleType("defAult"));
673
674        // Hipri is asymmetric
675        assertTrue(createApnSetting(new String[]{APN_TYPE_DEFAULT}).
676                canHandleType(APN_TYPE_HIPRI));
677        assertFalse(createApnSetting(new String[]{APN_TYPE_HIPRI}).
678                canHandleType(APN_TYPE_DEFAULT));
679
680
681        assertTrue(createApnSetting(new String[]{APN_TYPE_DEFAULT, APN_TYPE_MMS}).
682                canHandleType(APN_TYPE_DEFAULT));
683
684        assertTrue(createApnSetting(new String[]{APN_TYPE_DEFAULT, APN_TYPE_MMS}).
685                canHandleType(APN_TYPE_MMS));
686
687        assertFalse(createApnSetting(new String[]{APN_TYPE_DEFAULT, APN_TYPE_MMS}).
688                canHandleType(APN_TYPE_SUPL));
689
690        // special IA case - doesn't match wildcards
691        assertFalse(createApnSetting(new String[]{APN_TYPE_DEFAULT, APN_TYPE_MMS}).
692                canHandleType(APN_TYPE_IA));
693        assertFalse(createApnSetting(new String[]{APN_TYPE_ALL}).
694                canHandleType(APN_TYPE_IA));
695        assertFalse(createApnSetting(new String[]{APN_TYPE_ALL}).
696                canHandleType("iA"));
697        assertTrue(createApnSetting(new String[]{APN_TYPE_DEFAULT, APN_TYPE_MMS, APN_TYPE_IA}).
698                canHandleType(APN_TYPE_IA));
699
700        // check carrier disabled
701        assertFalse(createDisabledApnSetting(new String[]{APN_TYPE_ALL}).
702                canHandleType(APN_TYPE_MMS));
703        assertFalse(createDisabledApnSetting(new String[]{"DEfAULT"}).
704                canHandleType("defAult"));
705        assertFalse(createDisabledApnSetting(new String[]{APN_TYPE_DEFAULT}).
706                canHandleType(APN_TYPE_HIPRI));
707        assertFalse(createDisabledApnSetting(new String[]{APN_TYPE_DEFAULT, APN_TYPE_MMS}).
708                canHandleType(APN_TYPE_DEFAULT));
709        assertFalse(createDisabledApnSetting(new String[]{APN_TYPE_DEFAULT, APN_TYPE_MMS}).
710                canHandleType(APN_TYPE_MMS));
711        assertFalse(createDisabledApnSetting(new String[]
712                {APN_TYPE_DEFAULT, APN_TYPE_MMS, APN_TYPE_IA}).
713                canHandleType(APN_TYPE_IA));
714    }
715
716    @Test
717    @SmallTest
718    public void testEquals() throws Exception {
719        final int dummyInt = 1;
720        final String dummyString = "dummy";
721        final String[] dummyStringArr = new String[] {"dummy"};
722        // base apn
723        ApnSetting baseApn = createApnSetting(new String[] {"mms", "default"});
724        Field[] fields = ApnSetting.class.getDeclaredFields();
725        for (Field f : fields) {
726            int modifiers = f.getModifiers();
727            if (Modifier.isStatic(modifiers) || !Modifier.isFinal(modifiers)) {
728                continue;
729            }
730            f.setAccessible(true);
731            ApnSetting testApn = null;
732            if (int.class.equals(f.getType())) {
733                testApn = new ApnSetting(baseApn);
734                f.setInt(testApn, dummyInt + f.getInt(testApn));
735            } else if (boolean.class.equals(f.getType())) {
736                testApn = new ApnSetting(baseApn);
737                f.setBoolean(testApn, !f.getBoolean(testApn));
738            } else if (String.class.equals(f.getType())) {
739                testApn = new ApnSetting(baseApn);
740                f.set(testApn, dummyString);
741            } else if (String[].class.equals(f.getType())) {
742                testApn = new ApnSetting(baseApn);
743                f.set(testApn, dummyStringArr);
744            } else {
745                fail("Unsupported field:" + f.getName());
746            }
747            if (testApn != null) {
748                assertFalse(f.getName() + " is NOT checked", testApn.equals(baseApn));
749            }
750        }
751    }
752
753    @Test
754    @SmallTest
755    public void testEqualsRoamingProtocol() throws Exception {
756        ApnSetting apn1 = new ApnSetting(
757                1234,
758                "310260",
759                "",
760                "ims",
761                "",
762                "",
763                "",
764                "",
765                "",
766                "",
767                "",
768                -1,
769                 new String[]{"ims"},
770                "IPV6",
771                "",
772                true,
773                0,
774                131071,
775                0,
776                false,
777                0,
778                0,
779                0,
780                1440,
781                "",
782                "");
783
784        ApnSetting apn2 = new ApnSetting(
785                1235,
786                "310260",
787                "",
788                "ims",
789                "",
790                "",
791                "",
792                "",
793                "",
794                "",
795                "",
796                -1,
797                new String[]{"ims"},
798                "IPV6",
799                "IPV6",
800                true,
801                0,
802                131072,
803                0,
804                false,
805                0,
806                0,
807                0,
808                1440,
809                "",
810                "");
811
812        assertTrue(apn1.equals(apn2, false));
813        assertFalse(apn1.equals(apn2, true));
814    }
815}