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 junit.framework.Assert.assertFalse;
20 import static junit.framework.Assert.assertTrue;
21 import static junit.framework.Assert.fail;
22 
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.Mockito.doReturn;
25 
26 import android.net.Uri;
27 import android.os.PersistableBundle;
28 import android.telephony.CarrierConfigManager;
29 import android.telephony.ServiceState;
30 import android.telephony.TelephonyManager;
31 import android.telephony.data.ApnSetting;
32 import android.test.suitebuilder.annotation.SmallTest;
33 
34 import com.android.internal.telephony.TelephonyTest;
35 
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 
40 import java.lang.reflect.Field;
41 import java.lang.reflect.Modifier;
42 import java.net.InetAddress;
43 import java.util.ArrayList;
44 import java.util.List;
45 
46 public class ApnSettingTest extends TelephonyTest {
47 
48     private PersistableBundle mBundle;
49 
50     @Before
setUp()51     public void setUp() throws Exception {
52         super.setUp(getClass().getSimpleName());
53         mBundle = mContextFixture.getCarrierConfigBundle();
54     }
55 
56     @After
tearDown()57     public void tearDown() throws Exception {
58         super.tearDown();
59     }
60 
createApnSetting(int apnTypesBitmask)61     static ApnSetting createApnSetting(int apnTypesBitmask) {
62         return createApnSettingInternal(apnTypesBitmask, true);
63     }
64 
createDisabledApnSetting(int apnTypesBitmask)65     private static ApnSetting createDisabledApnSetting(int apnTypesBitmask) {
66         return createApnSettingInternal(apnTypesBitmask, false);
67     }
68 
createApnSettingInternal(int apnTypeBitmask, boolean carrierEnabled)69     private static ApnSetting createApnSettingInternal(int apnTypeBitmask, boolean carrierEnabled) {
70         return ApnSetting.makeApnSetting(
71                 2163,                   // id
72                 "44010",                // numeric
73                 "sp-mode",              // name
74                 "spmode.ne.jp",         // apn
75                 null,                   // proxy
76                 -1,                     // port
77                 null,                   // mmsc
78                 null,                   // mmsproxy
79                 -1,                     // mmsport
80                 "",                     // user
81                 "",                     // password
82                 -1,                     // authtype
83                 apnTypeBitmask,         // types
84                 ApnSetting.PROTOCOL_IP, // protocol
85                 ApnSetting.PROTOCOL_IP, // roaming_protocol
86                 carrierEnabled,         // carrier_enabled
87                 0,                      // networktype_bitmask
88                 0,                      // profile_id
89                 false,                  // modem_cognitive
90                 0,                      // max_conns
91                 0,                      // wait_time
92                 0,                      // max_conns_time
93                 0,                      // mtu
94                 -1,                     // mvno_type
95                 "");                    // mnvo_match_data
96     }
97 
assertApnSettingsEqual(List<ApnSetting> a1, List<ApnSetting> a2)98     private static void assertApnSettingsEqual(List<ApnSetting> a1, List<ApnSetting> a2) {
99         assertEquals(a1.size(), a2.size());
100         for (int i = 0; i < a1.size(); ++i) {
101             assertApnSettingEqual(a1.get(i), a2.get(i));
102         }
103     }
104 
assertApnSettingEqual(ApnSetting a1, ApnSetting a2)105     private static void assertApnSettingEqual(ApnSetting a1, ApnSetting a2) {
106         assertEquals(a1.getEntryName(), a2.getEntryName());
107         assertEquals(a1.getApnName(), a2.getApnName());
108         assertEquals(a1.getProxyAddressAsString(), a2.getProxyAddressAsString());
109         assertEquals(a1.getProxyPort(), a2.getProxyPort());
110         assertEquals(a1.getMmsc(), a2.getMmsc());
111         assertEquals(a1.getMmsProxyAddressAsString(), a2.getMmsProxyAddressAsString());
112         assertEquals(a1.getMmsProxyPort(), a2.getMmsProxyPort());
113         assertEquals(a1.getUser(), a2.getUser());
114         assertEquals(a1.getPassword(), a2.getPassword());
115         assertEquals(a1.getAuthType(), a2.getAuthType());
116         assertEquals(a1.getId(), a2.getId());
117         assertEquals(a1.getOperatorNumeric(), a2.getOperatorNumeric());
118         assertEquals(a1.getProtocol(), a2.getProtocol());
119         assertEquals(a1.getRoamingProtocol(), a2.getRoamingProtocol());
120         assertEquals(a1.getApnTypeBitmask(), a2.getApnTypeBitmask());
121         assertEquals(a1.isEnabled(), a2.isEnabled());
122         assertEquals(a1.getProfileId(), a2.getProfileId());
123         assertEquals(a1.isPersistent(), a2.isPersistent());
124         assertEquals(a1.getMaxConns(), a2.getMaxConns());
125         assertEquals(a1.getWaitTime(), a2.getWaitTime());
126         assertEquals(a1.getMaxConnsTime(), a2.getMaxConnsTime());
127         assertEquals(a1.getMtu(), a2.getMtu());
128         assertEquals(a1.getMvnoType(), a2.getMvnoType());
129         assertEquals(a1.getMvnoMatchData(), a2.getMvnoMatchData());
130         assertEquals(a1.getNetworkTypeBitmask(), a2.getNetworkTypeBitmask());
131         assertEquals(a1.getApnSetId(), a2.getApnSetId());
132         assertEquals(a1.getSkip464Xlat(), a2.getSkip464Xlat());
133     }
134 
135     @Test
136     @SmallTest
testFromString()137     public void testFromString() {
138         final int dunTypesBitmask = ApnSetting.TYPE_DUN;
139         final int mmsTypesBitmask = ApnSetting.TYPE_MMS | ApnSetting.TYPE_ALL;
140 
141         ApnSetting expectedApn;
142         String testString;
143 
144         // A real-world v1 example string.
145         testString = "Vodafone IT,web.omnitel.it,,,,,,,,,222,10,,DUN";
146         expectedApn = ApnSetting.makeApnSetting(
147                 -1, "22210", "Vodafone IT", "web.omnitel.it", "", -1, null, "", -1, "", "", 0,
148                 dunTypesBitmask, ApnSetting.PROTOCOL_IP, ApnSetting.PROTOCOL_IP, true,
149                 0, 0, false, 0, 0, 0, 0, -1, "");
150         assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
151 
152         // A v2 string.
153         testString = "[ApnSettingV2] Name,apn,,,,,,,,,123,45,,mms|*,IPV6,IP,true,14";
154         int networkTypeBitmask = 1 << (13 - 1);
155         expectedApn = ApnSetting.makeApnSetting(
156                 -1, "12345", "Name", "apn", "", -1, null, "", -1, "", "", 0,
157                 mmsTypesBitmask, ApnSetting.PROTOCOL_IPV6, ApnSetting.PROTOCOL_IP, true,
158                 networkTypeBitmask, 0, false, 0, 0, 0, 0, -1, "");
159         assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
160 
161         // A v2 string with spaces.
162         testString = "[ApnSettingV2] Name,apn, ,,,,,,,,123,45,,mms|*,IPV6, IP,true,14";
163         expectedApn = ApnSetting.makeApnSetting(
164                 -1, "12345", "Name", "apn", "", -1, null, "", -1, "", "", 0,
165                 mmsTypesBitmask, ApnSetting.PROTOCOL_IPV6, ApnSetting.PROTOCOL_IP, true,
166                 networkTypeBitmask, 0, false, 0, 0, 0, 0, -1, "");
167         assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
168 
169         // A v3 string.
170         testString = "[ApnSettingV3] Name,apn,,,,,,,,,123,45,,mms|*,IPV6,IP,true,14,,,,,,,spn,testspn";
171         expectedApn = ApnSetting.makeApnSetting(
172                 -1, "12345", "Name", "apn", "", -1, null, "", -1, "", "", 0,
173                 mmsTypesBitmask, ApnSetting.PROTOCOL_IPV6, ApnSetting.PROTOCOL_IP, true,
174                 networkTypeBitmask, 0, false, 0, 0, 0, 0, ApnSetting.MVNO_TYPE_SPN, "testspn");
175         assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
176 
177         // A v4 string with network type bitmask.
178         testString =
179                 "[ApnSettingV4] Name,apn,,,,,,,,,123,45,,mms|*,IPV6,IP,true,0,,,,,,,spn,testspn,6";
180         networkTypeBitmask = 1 << (6 - 1);
181         expectedApn = ApnSetting.makeApnSetting(
182                 -1, "12345", "Name", "apn", "", -1, null, "", -1, "", "", 0,
183                 mmsTypesBitmask, ApnSetting.PROTOCOL_IPV6, ApnSetting.PROTOCOL_IP, true,
184                 networkTypeBitmask, 0, false, 0, 0, 0, 0, ApnSetting.MVNO_TYPE_SPN, "testspn");
185         assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
186 
187         testString =
188                 "[ApnSettingV4] Name,apn,,,,,,,,,123,45,,mms|*,IPV6,IP,true,0,,,,,,,spn,testspn,"
189                         + "4|5|6|7|8|12|13|14|19";
190         // The value was calculated by adding "4|5|6|7|8|12|13|14|19".
191         networkTypeBitmask = 276728;
192         expectedApn = ApnSetting.makeApnSetting(
193                 -1, "12345", "Name", "apn", "", -1, null, "", -1, "", "", 0,
194                 mmsTypesBitmask, ApnSetting.PROTOCOL_IPV6, ApnSetting.PROTOCOL_IP, true,
195                 networkTypeBitmask, 0, false, 0, 0, 0, 0, ApnSetting.MVNO_TYPE_SPN, "testspn");
196         assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
197 
198         // A v4 string with network type bitmask and compatible bearer bitmask.
199         testString =
200                 "[ApnSettingV4] Name,apn,,,,,,,,,123,45,,mms|*,IPV6,IP,true,8,,,,,,,spn,testspn, 6";
201         networkTypeBitmask = 1 << (6 - 1);
202         expectedApn = ApnSetting.makeApnSetting(
203                 -1, "12345", "Name", "apn", "", -1, null, "", -1, "", "", 0,
204                 mmsTypesBitmask, ApnSetting.PROTOCOL_IPV6, ApnSetting.PROTOCOL_IP, true,
205                 networkTypeBitmask, 0, false, 0, 0, 0, 0, ApnSetting.MVNO_TYPE_SPN, "testspn");
206         assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
207 
208         // A v4 string with network type bitmask and incompatible bearer bitmask.
209         testString =
210                 "[ApnSettingV4] Name,apn,,,,,,,,,123,45,,mms|*,IPV6,IP,true,9,,,,,,,spn,testspn, 6";
211         expectedApn = ApnSetting.makeApnSetting(
212                 -1, "12345", "Name", "apn", "", -1, null, "", -1, "", "", 0,
213                 mmsTypesBitmask, ApnSetting.PROTOCOL_IPV6, ApnSetting.PROTOCOL_IP, true,
214                 networkTypeBitmask, 0, false, 0,
215                 0, 0, 0, ApnSetting.MVNO_TYPE_SPN, "testspn");
216         assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
217 
218         // A v5 string with apnSetId=0
219         testString =
220                 "[ApnSettingV5] Name,apn,,,,,,,,,123,45,,mms|*,IPV6,IP,true,0,,,,,,,spn,testspn,0,0";
221         expectedApn = ApnSetting.makeApnSetting(
222                 -1, "12345", "Name", "apn", "", -1, null, "", -1, "", "", 0,
223                 mmsTypesBitmask, ApnSetting.PROTOCOL_IPV6, ApnSetting.PROTOCOL_IP, true,
224                 0, 0, false, 0, 0, 0, 0, ApnSetting.MVNO_TYPE_SPN, "testspn");
225         assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
226 
227         // A v5 string with apnSetId=3
228         testString =
229                 "[ApnSettingV5] Name,apn,,,,,,,,,123,45,,mms|*,IPV6,IP,true,0,,,,,,,spn,testspn,0,3";
230         expectedApn = ApnSetting.makeApnSetting(
231                 -1, "12345", "Name", "apn", "", -1, null, "", -1, "", "", 0,
232                 mmsTypesBitmask, ApnSetting.PROTOCOL_IPV6, ApnSetting.PROTOCOL_IP, true,
233                 0, 0, false, 0, 0, 0, 0, ApnSetting.MVNO_TYPE_SPN, "testspn", 3, -1, -1);
234         assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
235 
236         // A v6 string with carrierId=100
237         testString =
238             "[ApnSettingV5] Name,apn,,,,,,,,,123,45,,mms|*,IPV6,IP,true,0,,,,,,,spn,testspn,0,3,"
239                 + "100";
240         expectedApn = ApnSetting.makeApnSetting(
241             -1, "12345", "Name", "apn", "", -1, null, "", -1, "", "", 0,
242             mmsTypesBitmask, ApnSetting.PROTOCOL_IPV6, ApnSetting.PROTOCOL_IP, true,
243             0, 0, false, 0, 0, 0, 0, ApnSetting.MVNO_TYPE_SPN, "testspn", 3, 100, -1);
244         assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
245 
246         // A v7 string with skip_464xlat=1
247         testString =
248             "[ApnSettingV7] Name,apn,,,,,,,,,123,45,,mms|*,IPV6,IP,true,0,,,,,,,spn,testspn,0,3,"
249                 + "-1, 1";
250         expectedApn = ApnSetting.makeApnSetting(
251             -1, "12345", "Name", "apn", "", -1, null, "", -1, "", "", 0,
252             mmsTypesBitmask, ApnSetting.PROTOCOL_IPV6, ApnSetting.PROTOCOL_IP, true,
253             0, 0, false, 0, 0, 0, 0, ApnSetting.MVNO_TYPE_SPN, "testspn", 3, -1, 1);
254         assertApnSettingEqual(expectedApn, ApnSetting.fromString(testString));
255 
256         // Return no apn if insufficient fields given.
257         testString = "[ApnSettingV3] Name,apn,,,,,,,,,123, 45,,mms|*";
258         assertEquals(null, ApnSetting.fromString(testString));
259 
260         testString = "Name,apn,,,,,,,,,123, 45,";
261         assertEquals(null, ApnSetting.fromString(testString));
262     }
263 
264     @Test
265     @SmallTest
testArrayFromString()266     public void testArrayFromString() {
267         final int mmsTypesBitmask = ApnSetting.TYPE_MMS;
268         // Test a multiple v3 string.
269         String testString =
270                 "[ApnSettingV3] Name,apn,,,,,,,,,123,45,,mms,IPV6,IP,true,14,,,,,,,spn,testspn";
271         testString +=
272                 " ;[ApnSettingV3] Name1,apn1,,,,,,,,,123,46,,mms,IPV6,IP,true,12,,,,,,,gid,testGid";
273         testString +=
274                 " ;[ApnSettingV3] Name1,apn2,,,,,,,,,123,46,,mms,IPV6,IP,true,12,,,,,,,,";
275         testString +=
276                 " ;[ApnSettingV5] Name1,apn2,,,,,,,,,123,46,,mms,IPV6,IP,true,0,,,,,,,,,,3";
277         List<ApnSetting> expectedApns = new ArrayList<ApnSetting>();
278         expectedApns.add(ApnSetting.makeApnSetting(
279                 -1, "12345", "Name", "apn", "", -1, null, "", -1, "", "", 0,
280                 mmsTypesBitmask, ApnSetting.PROTOCOL_IPV6, ApnSetting.PROTOCOL_IP, true,
281                 1 << (13 - 1), 0, false, 0, 0, 0, 0, ApnSetting.MVNO_TYPE_SPN, "testspn"));
282         expectedApns.add(ApnSetting.makeApnSetting(
283                 -1, "12346", "Name1", "apn1", "", -1, null, "", -1, "", "", 0,
284                 mmsTypesBitmask, ApnSetting.PROTOCOL_IPV6, ApnSetting.PROTOCOL_IP, true,
285                 1 << (12 - 1), 0, false, 0, 0, 0, 0, ApnSetting.MVNO_TYPE_GID, "testGid"));
286         expectedApns.add(ApnSetting.makeApnSetting(
287                 -1, "12346", "Name1", "apn2", "", -1, null, "", -1, "", "", 0,
288                 mmsTypesBitmask, ApnSetting.PROTOCOL_IPV6, ApnSetting.PROTOCOL_IP, true,
289                 1 << (12 - 1), 0, false, 0, 0, 0, 0, -1, ""));
290         expectedApns.add(ApnSetting.makeApnSetting(
291                 -1, "12346", "Name1", "apn2", "", -1, null, "", -1, "", "", 0,
292                 mmsTypesBitmask, ApnSetting.PROTOCOL_IPV6, ApnSetting.PROTOCOL_IP, true,
293                 0, 0, false, 0, 0, 0, 0, -1, "", 3, -1, -1));
294         assertApnSettingsEqual(expectedApns, ApnSetting.arrayFromString(testString));
295     }
296 
297     @Test
298     @SmallTest
testToString()299     public void testToString() {
300         // Use default apn_set_id constructor.
301         ApnSetting apn = ApnSetting.makeApnSetting(
302                 99, "12345", "Name", "apn", null, 10,
303                 null, null, -1, "user", "password", 0,
304                 ApnSetting.TYPE_DEFAULT, ApnSetting.PROTOCOL_IPV6, ApnSetting.PROTOCOL_IP, true,
305                 4096, 0, false, 0, 0, 0, 0, ApnSetting.MVNO_TYPE_SPN, "");
306         String expected = "[ApnSettingV7] Name, 99, 12345, apn, null, "
307                 + "null, null, null, 10, 0, hipri | default, "
308                 + "IPV6, IP, true, 0, false, 0, 0, 0, 0, spn, , false, 4096, 0, -1, -1";
309         assertEquals(expected, apn.toString());
310 
311         final int networkTypeBitmask = 1 << (14 - 1);
312         apn = ApnSetting.makeApnSetting(
313                 99, "12345", "Name", "apn", null, 10,
314                 null, null, -1, "user", "password", 0,
315                 ApnSetting.TYPE_DEFAULT, ApnSetting.PROTOCOL_IPV6, ApnSetting.PROTOCOL_IP, true,
316                 networkTypeBitmask, 0, false, 0, 0, 0, 0, ApnSetting.MVNO_TYPE_SPN, "", 3, -1, 1);
317         expected = "[ApnSettingV7] Name, 99, 12345, apn, null, "
318                 + "null, null, null, 10, 0, hipri | default, "
319                 + "IPV6, IP, true, 0, false, 0, 0, 0, 0, spn, , false, 8192, 3, -1, 1";
320         assertEquals(expected, apn.toString());
321     }
322 
323     @Test
324     @SmallTest
testIsMetered()325     public void testIsMetered() {
326         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
327                 new String[]{ApnSetting.TYPE_DEFAULT_STRING, ApnSetting.TYPE_MMS_STRING});
328 
329         doReturn(false).when(mServiceState).getDataRoaming();
330         doReturn(1).when(mPhone).getSubId();
331 
332         assertTrue(ApnSettingUtils.isMetered(createApnSetting(ApnSetting.TYPE_DEFAULT), mPhone));
333 
334         assertTrue(ApnSettingUtils.isMetered(
335                 createApnSetting(ApnSetting.TYPE_DEFAULT | ApnSetting.TYPE_MMS), mPhone));
336 
337         assertTrue(ApnSettingUtils.isMetered(createApnSetting(ApnSetting.TYPE_MMS), mPhone));
338 
339         assertTrue(ApnSettingUtils.isMetered(
340                 createApnSetting(ApnSetting.TYPE_SUPL | ApnSetting.TYPE_MMS), mPhone));
341 
342         assertTrue(ApnSettingUtils.isMetered(
343                 createApnSetting(ApnSetting.TYPE_DEFAULT | ApnSetting.TYPE_DUN), mPhone));
344 
345         assertTrue(ApnSettingUtils.isMetered(createApnSetting(ApnSetting.TYPE_ALL), mPhone));
346 
347         assertFalse(ApnSettingUtils.isMetered(
348                 createApnSetting(ApnSetting.TYPE_SUPL | ApnSetting.TYPE_FOTA), mPhone));
349 
350         assertFalse(ApnSettingUtils.isMetered(
351                 createApnSetting(ApnSetting.TYPE_IA | ApnSetting.TYPE_CBS), mPhone));
352 
353         assertTrue(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_DEFAULT, mPhone));
354         assertTrue(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_MMS, mPhone));
355         assertFalse(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_SUPL, mPhone));
356         assertFalse(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_CBS, mPhone));
357         assertFalse(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_DUN, mPhone));
358         assertFalse(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_FOTA, mPhone));
359         assertFalse(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_IA, mPhone));
360         assertFalse(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_HIPRI, mPhone));
361         assertFalse(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_XCAP, mPhone));
362         assertFalse(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_ENTERPRISE, mPhone));
363 
364         // Carrier config settings changes.
365         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
366                 new String[]{ApnSetting.TYPE_DEFAULT_STRING});
367 
368         assertTrue(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_DEFAULT, mPhone));
369         assertFalse(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_MMS, mPhone));
370     }
371 
372     @Test
373     @SmallTest
testIsRoamingMetered()374     public void testIsRoamingMetered() {
375         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_ROAMING_APN_TYPES_STRINGS,
376                 new String[]{ApnSetting.TYPE_DEFAULT_STRING, ApnSetting.TYPE_MMS_STRING});
377         doReturn(true).when(mServiceState).getDataRoaming();
378         doReturn(1).when(mPhone).getSubId();
379 
380         assertTrue(ApnSettingUtils.isMetered(createApnSetting(ApnSetting.TYPE_DEFAULT), mPhone));
381 
382         assertTrue(ApnSettingUtils.isMetered(
383                 createApnSetting(ApnSetting.TYPE_DEFAULT | ApnSetting.TYPE_MMS), mPhone));
384 
385         assertTrue(ApnSettingUtils.isMetered(createApnSetting(ApnSetting.TYPE_MMS), mPhone));
386 
387         assertTrue(ApnSettingUtils.isMetered(
388                 createApnSetting(ApnSetting.TYPE_SUPL | ApnSetting.TYPE_MMS), mPhone));
389 
390         assertTrue(ApnSettingUtils.isMetered(
391                 createApnSetting(ApnSetting.TYPE_DEFAULT | ApnSetting.TYPE_DUN), mPhone));
392 
393         assertTrue(ApnSettingUtils.isMetered(createApnSetting(ApnSetting.TYPE_ALL), mPhone));
394 
395         assertFalse(ApnSettingUtils.isMetered(
396                 createApnSetting(ApnSetting.TYPE_SUPL | ApnSetting.TYPE_FOTA), mPhone));
397 
398         assertFalse(ApnSettingUtils.isMetered(
399                 createApnSetting(ApnSetting.TYPE_IA | ApnSetting.TYPE_CBS), mPhone));
400 
401         // Carrier config settings changes.
402         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_ROAMING_APN_TYPES_STRINGS,
403                 new String[]{ApnSetting.TYPE_FOTA_STRING});
404 
405         assertFalse(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_DEFAULT, mPhone));
406         assertFalse(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_MMS, mPhone));
407         assertTrue(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_FOTA, mPhone));
408         assertFalse(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_XCAP, mPhone));
409         assertFalse(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_ENTERPRISE, mPhone));
410     }
411 
412     @Test
413     @SmallTest
testIsMeteredAnother()414     public void testIsMeteredAnother() {
415         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
416                 new String[]{ApnSetting.TYPE_SUPL_STRING, ApnSetting.TYPE_CBS_STRING});
417 
418         doReturn(false).when(mServiceState).getDataRoaming();
419         doReturn(1).when(mPhone).getSubId();
420 
421         assertTrue(ApnSettingUtils.isMetered(
422                 createApnSetting(ApnSetting.TYPE_SUPL | ApnSetting.TYPE_CBS), mPhone));
423 
424         assertTrue(ApnSettingUtils.isMetered(createApnSetting(ApnSetting.TYPE_SUPL), mPhone));
425 
426         assertTrue(ApnSettingUtils.isMetered(createApnSetting(ApnSetting.TYPE_CBS), mPhone));
427 
428         assertTrue(ApnSettingUtils.isMetered(
429                 createApnSetting(ApnSetting.TYPE_FOTA | ApnSetting.TYPE_CBS), mPhone));
430 
431         assertTrue(ApnSettingUtils.isMetered(
432                 createApnSetting(ApnSetting.TYPE_SUPL | ApnSetting.TYPE_IA), mPhone));
433 
434         assertTrue(ApnSettingUtils.isMetered(createApnSetting(ApnSetting.TYPE_ALL), mPhone));
435 
436         assertFalse(ApnSettingUtils.isMetered(
437                 createApnSetting(ApnSetting.TYPE_DEFAULT | ApnSetting.TYPE_IMS), mPhone));
438 
439         assertFalse(ApnSettingUtils.isMetered(createApnSetting(ApnSetting.TYPE_IMS), mPhone));
440         assertFalse(ApnSettingUtils.isMetered(createApnSetting(ApnSetting.TYPE_XCAP), mPhone));
441         assertFalse(ApnSettingUtils.isMetered(
442                 createApnSetting(ApnSetting.TYPE_ENTERPRISE), mPhone));
443     }
444 
445     @Test
446     @SmallTest
testIsRoamingMeteredAnother()447     public void testIsRoamingMeteredAnother() {
448         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_ROAMING_APN_TYPES_STRINGS,
449                 new String[]{ApnSetting.TYPE_SUPL_STRING, ApnSetting.TYPE_CBS_STRING});
450         doReturn(true).when(mServiceState).getDataRoaming();
451         doReturn(2).when(mPhone).getSubId();
452 
453         assertTrue(ApnSettingUtils.isMetered(
454                 createApnSetting(ApnSetting.TYPE_SUPL | ApnSetting.TYPE_CBS), mPhone));
455 
456         assertTrue(ApnSettingUtils.isMetered(createApnSetting(ApnSetting.TYPE_SUPL), mPhone));
457 
458         assertTrue(ApnSettingUtils.isMetered(createApnSetting(ApnSetting.TYPE_CBS), mPhone));
459 
460         assertTrue(ApnSettingUtils.isMetered(
461                 createApnSetting(ApnSetting.TYPE_FOTA | ApnSetting.TYPE_CBS), mPhone));
462 
463         assertTrue(ApnSettingUtils.isMetered(
464                 createApnSetting(ApnSetting.TYPE_SUPL | ApnSetting.TYPE_IA), mPhone));
465 
466         assertTrue(ApnSettingUtils.isMetered(createApnSetting(ApnSetting.TYPE_ALL), mPhone));
467 
468         assertFalse(ApnSettingUtils.isMetered(
469                 createApnSetting(ApnSetting.TYPE_DEFAULT | ApnSetting.TYPE_IMS), mPhone));
470 
471         assertFalse(ApnSettingUtils.isMetered(createApnSetting(ApnSetting.TYPE_IMS), mPhone));
472 
473         assertTrue(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_SUPL, mPhone));
474         assertTrue(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_CBS, mPhone));
475         assertFalse(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_DEFAULT, mPhone));
476         assertFalse(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_MMS, mPhone));
477         assertFalse(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_DUN, mPhone));
478         assertFalse(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_FOTA, mPhone));
479         assertFalse(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_IA, mPhone));
480         assertFalse(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_HIPRI, mPhone));
481         assertFalse(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_XCAP, mPhone));
482         assertFalse(ApnSettingUtils.isMeteredApnType(ApnSetting.TYPE_ENTERPRISE, mPhone));
483     }
484 
485     @Test
486     @SmallTest
testIsMeteredNothingCharged()487     public void testIsMeteredNothingCharged() {
488         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
489                 new String[]{});
490 
491         doReturn(false).when(mServiceState).getDataRoaming();
492         doReturn(3).when(mPhone).getSubId();
493 
494         assertFalse(ApnSettingUtils.isMetered(createApnSetting(ApnSetting.TYPE_IMS), mPhone));
495 
496         assertFalse(ApnSettingUtils.isMetered(
497                 createApnSetting(ApnSetting.TYPE_IMS | ApnSetting.TYPE_MMS), mPhone));
498 
499         assertFalse(ApnSettingUtils.isMetered(
500                 createApnSetting(ApnSetting.TYPE_DEFAULT | ApnSetting.TYPE_FOTA), mPhone));
501 
502         assertFalse(ApnSettingUtils.isMetered(
503                 createApnSetting(ApnSetting.TYPE_ALL), mPhone));
504     }
505 
506     @Test
507     @SmallTest
testIsRoamingMeteredNothingCharged()508     public void testIsRoamingMeteredNothingCharged() {
509         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_ROAMING_APN_TYPES_STRINGS,
510                 new String[]{});
511         doReturn(true).when(mServiceState).getDataRoaming();
512         doReturn(3).when(mPhone).getSubId();
513 
514         assertFalse(ApnSettingUtils.isMetered(createApnSetting(ApnSetting.TYPE_IMS), mPhone));
515 
516         assertFalse(ApnSettingUtils.isMetered(
517                 createApnSetting(ApnSetting.TYPE_IMS | ApnSetting.TYPE_MMS), mPhone));
518 
519         assertFalse(ApnSettingUtils.isMetered(
520                 createApnSetting(ApnSetting.TYPE_DEFAULT | ApnSetting.TYPE_FOTA), mPhone));
521 
522         assertFalse(ApnSettingUtils.isMetered(
523                 createApnSetting(ApnSetting.TYPE_ALL), mPhone));
524     }
525 
526     @Test
527     @SmallTest
testCanHandleType()528     public void testCanHandleType() {
529         String types[] = {"mms"};
530 
531         assertTrue(createApnSetting(ApnSetting.TYPE_ALL)
532                 .canHandleType(ApnSetting.TYPE_MMS));
533 
534         assertFalse(createApnSetting(ApnSetting.TYPE_DEFAULT)
535                 .canHandleType(ApnSetting.TYPE_MMS));
536 
537         assertTrue(createApnSetting(ApnSetting.TYPE_DEFAULT)
538                 .canHandleType(ApnSetting.TYPE_DEFAULT));
539 
540         // Hipri is asymmetric
541         assertTrue(createApnSetting(ApnSetting.TYPE_DEFAULT)
542                 .canHandleType(ApnSetting.TYPE_HIPRI));
543         assertFalse(createApnSetting(ApnSetting.TYPE_HIPRI)
544                 .canHandleType(ApnSetting.TYPE_DEFAULT));
545 
546 
547         assertTrue(createApnSetting(ApnSetting.TYPE_DEFAULT | ApnSetting.TYPE_MMS)
548                 .canHandleType(ApnSetting.TYPE_DEFAULT));
549 
550         assertTrue(createApnSetting(ApnSetting.TYPE_DEFAULT | ApnSetting.TYPE_MMS)
551                 .canHandleType(ApnSetting.TYPE_MMS));
552 
553         assertFalse(createApnSetting(ApnSetting.TYPE_DEFAULT | ApnSetting.TYPE_MMS)
554                 .canHandleType(ApnSetting.TYPE_SUPL));
555 
556         // special IA case - doesn't match wildcards
557         assertFalse(createApnSetting(ApnSetting.TYPE_ALL)
558                 .canHandleType(ApnSetting.TYPE_IA));
559         assertTrue(createApnSetting(
560                 ApnSetting.TYPE_DEFAULT | ApnSetting.TYPE_MMS | ApnSetting.TYPE_IA)
561                 .canHandleType(ApnSetting.TYPE_IA));
562 
563         // same for emergency, mcx, xcap, and enterprise
564         assertFalse(createApnSetting(ApnSetting.TYPE_ALL)
565                 .canHandleType(ApnSetting.TYPE_EMERGENCY));
566         assertTrue(createApnSetting(
567                 ApnSetting.TYPE_DEFAULT | ApnSetting.TYPE_MMS | ApnSetting.TYPE_EMERGENCY)
568                 .canHandleType(ApnSetting.TYPE_EMERGENCY));
569         assertFalse(createApnSetting(ApnSetting.TYPE_ALL)
570                 .canHandleType(ApnSetting.TYPE_MCX));
571         assertTrue(createApnSetting(
572                 ApnSetting.TYPE_DEFAULT | ApnSetting.TYPE_MMS | ApnSetting.TYPE_MCX)
573                 .canHandleType(ApnSetting.TYPE_MCX));
574         assertFalse(createApnSetting(ApnSetting.TYPE_ALL)
575                 .canHandleType(ApnSetting.TYPE_XCAP));
576         assertTrue(createApnSetting(
577                 ApnSetting.TYPE_DEFAULT | ApnSetting.TYPE_MMS | ApnSetting.TYPE_XCAP)
578                 .canHandleType(ApnSetting.TYPE_XCAP));
579         assertFalse(createApnSetting(ApnSetting.TYPE_ALL)
580                 .canHandleType(ApnSetting.TYPE_ENTERPRISE));
581         assertTrue(createApnSetting(
582                 ApnSetting.TYPE_DEFAULT | ApnSetting.TYPE_MMS | ApnSetting.TYPE_ENTERPRISE)
583                 .canHandleType(ApnSetting.TYPE_ENTERPRISE));
584 
585         // check carrier disabled
586         assertFalse(createDisabledApnSetting(ApnSetting.TYPE_ALL)
587                 .canHandleType(ApnSetting.TYPE_MMS));
588         assertFalse(createDisabledApnSetting(ApnSetting.TYPE_DEFAULT)
589                 .canHandleType(ApnSetting.TYPE_DEFAULT));
590         assertFalse(createDisabledApnSetting(ApnSetting.TYPE_DEFAULT)
591                 .canHandleType(ApnSetting.TYPE_HIPRI));
592         assertFalse(createDisabledApnSetting(ApnSetting.TYPE_DEFAULT | ApnSetting.TYPE_MMS)
593                 .canHandleType(ApnSetting.TYPE_DEFAULT));
594         assertFalse(createDisabledApnSetting(ApnSetting.TYPE_DEFAULT | ApnSetting.TYPE_MMS)
595                 .canHandleType(ApnSetting.TYPE_MMS));
596         assertFalse(createDisabledApnSetting(
597                 ApnSetting.TYPE_DEFAULT | ApnSetting.TYPE_MMS | ApnSetting.TYPE_IA)
598                 .canHandleType(ApnSetting.TYPE_IA));
599         assertFalse(createDisabledApnSetting(
600                 ApnSetting.TYPE_DEFAULT | ApnSetting.TYPE_MMS | ApnSetting.TYPE_XCAP)
601                 .canHandleType(ApnSetting.TYPE_XCAP));
602         assertFalse(createDisabledApnSetting(
603                 ApnSetting.TYPE_DEFAULT | ApnSetting.TYPE_MMS | ApnSetting.TYPE_ENTERPRISE)
604                 .canHandleType(ApnSetting.TYPE_ENTERPRISE));
605     }
606 
607     @Test
608     @SmallTest
testEquals()609     public void testEquals() throws Exception {
610         final int dummyInt = 1;
611         final String dummyString = "dummy";
612         final String[] dummyStringArr = new String[] {"dummy"};
613         final InetAddress dummyProxyAddress = InetAddress.getByAddress(new byte[]{0, 0, 0, 0});
614         final Uri dummyUri = Uri.parse("www.google.com");
615         // base apn
616         ApnSetting baseApn = createApnSetting(ApnSetting.TYPE_MMS | ApnSetting.TYPE_DEFAULT);
617         Field[] fields = ApnSetting.class.getDeclaredFields();
618         for (Field f : fields) {
619             int modifiers = f.getModifiers();
620             if (Modifier.isStatic(modifiers) || !Modifier.isFinal(modifiers)) {
621                 continue;
622             }
623             f.setAccessible(true);
624             ApnSetting testApn = null;
625             if (int.class.equals(f.getType())) {
626                 testApn = ApnSetting.makeApnSetting(baseApn);
627                 f.setInt(testApn, dummyInt + f.getInt(testApn));
628             } else if (boolean.class.equals(f.getType())) {
629                 testApn = ApnSetting.makeApnSetting(baseApn);
630                 f.setBoolean(testApn, !f.getBoolean(testApn));
631             } else if (String.class.equals(f.getType())) {
632                 testApn = ApnSetting.makeApnSetting(baseApn);
633                 f.set(testApn, dummyString);
634             } else if (String[].class.equals(f.getType())) {
635                 testApn = ApnSetting.makeApnSetting(baseApn);
636                 f.set(testApn, dummyStringArr);
637             } else if (InetAddress.class.equals(f.getType())) {
638                 testApn = ApnSetting.makeApnSetting(baseApn);
639                 f.set(testApn, dummyProxyAddress);
640             } else if (Uri.class.equals(f.getType())) {
641                 testApn = ApnSetting.makeApnSetting(baseApn);
642                 f.set(testApn, dummyUri);
643             } else {
644                 fail("Unsupported field:" + f.getName());
645             }
646             if (testApn != null) {
647                 assertFalse(f.getName() + " is NOT checked", testApn.equals(baseApn));
648             }
649         }
650     }
651 
652     @Test
653     @SmallTest
testEqualsRoamingProtocol()654     public void testEqualsRoamingProtocol() {
655         ApnSetting apn1 = ApnSetting.makeApnSetting(
656                 1234,
657                 "310260",
658                 "",
659                 "ims",
660                 null,
661                 -1,
662                 null,
663                 null,
664                 -1,
665                 "",
666                 "",
667                 -1,
668                 ApnSetting.TYPE_IMS,
669                 ApnSetting.PROTOCOL_IPV6,
670                 -1,
671                 true,
672                 ServiceState.convertBearerBitmaskToNetworkTypeBitmask(131071),
673                 0,
674                 false,
675                 0,
676                 0,
677                 0,
678                 1440,
679                 -1,
680                 "");
681 
682         ApnSetting apn2 = ApnSetting.makeApnSetting(
683                 1235,
684                 "310260",
685                 "",
686                 "ims",
687                 null,
688                 -1,
689                 null,
690                 null,
691                 -1,
692                 "",
693                 "",
694                 -1,
695                 ApnSetting.TYPE_IMS,
696                 ApnSetting.PROTOCOL_IPV6,
697                 ApnSetting.PROTOCOL_IPV6,
698                 true,
699                 ServiceState.convertBearerBitmaskToNetworkTypeBitmask(131072),
700                 0,
701                 false,
702                 0,
703                 0,
704                 0,
705                 1440,
706                 -1,
707                 "");
708 
709         assertTrue(apn1.equals(apn2, false));
710         assertFalse(apn1.equals(apn2, true));
711     }
712 
713     @Test
714     @SmallTest
testCanHandleNetwork()715     public void testCanHandleNetwork() {
716         ApnSetting apn1 = ApnSetting.makeApnSetting(
717                 1234,
718                 "310260",
719                 "",
720                 "ims",
721                 null,
722                 -1,
723                 null,
724                 null,
725                 -1,
726                 "",
727                 "",
728                 -1,
729                 ApnSetting.TYPE_IMS,
730                 ApnSetting.PROTOCOL_IPV6,
731                 -1,
732                 true,
733                 (int) (TelephonyManager.NETWORK_TYPE_BITMASK_LTE
734                         | TelephonyManager.NETWORK_TYPE_BITMASK_UMTS),
735                 0,
736                 false,
737                 0,
738                 0,
739                 0,
740                 1440,
741                 -1,
742                 "");
743 
744         ApnSetting apn2 = ApnSetting.makeApnSetting(
745                 1235,
746                 "310260",
747                 "",
748                 "ims",
749                 null,
750                 -1,
751                 null,
752                 null,
753                 -1,
754                 "",
755                 "",
756                 -1,
757                 ApnSetting.TYPE_IMS,
758                 ApnSetting.PROTOCOL_IPV6,
759                 ApnSetting.PROTOCOL_IPV6,
760                 true,
761                 (int) (TelephonyManager.NETWORK_TYPE_BITMASK_EDGE
762                         | TelephonyManager.NETWORK_TYPE_BITMASK_GPRS),
763                 0,
764                 false,
765                 0,
766                 0,
767                 0,
768                 1440,
769                 -1,
770                 "");
771 
772         assertFalse(apn1.canSupportNetworkType(TelephonyManager.NETWORK_TYPE_1xRTT));
773         assertTrue(apn1.canSupportNetworkType(TelephonyManager.NETWORK_TYPE_LTE));
774         assertTrue(apn1.canSupportNetworkType(TelephonyManager.NETWORK_TYPE_UMTS));
775 
776         assertFalse(apn2.canSupportNetworkType(TelephonyManager.NETWORK_TYPE_1xRTT));
777         assertFalse(apn2.canSupportNetworkType(TelephonyManager.NETWORK_TYPE_LTE));
778         assertTrue(apn2.canSupportNetworkType(TelephonyManager.NETWORK_TYPE_GPRS));
779         assertTrue(apn2.canSupportNetworkType(TelephonyManager.NETWORK_TYPE_EDGE));
780 
781         assertTrue(apn2.canSupportNetworkType(TelephonyManager.NETWORK_TYPE_GSM));
782     }
783 }
784