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