summaryrefslogtreecommitdiffstats
path: root/tests/wifitests/src/com/android/server/wifi/hotspot2/ANQPMatcherTest.java
blob: df9c332a35baab5a190cbad0482f701d4fdfd1b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.wifi.hotspot2;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import android.net.wifi.EAPConstants;

import androidx.test.filters.SmallTest;

import com.android.server.wifi.IMSIParameter;
import com.android.server.wifi.hotspot2.anqp.CellularNetwork;
import com.android.server.wifi.hotspot2.anqp.DomainNameElement;
import com.android.server.wifi.hotspot2.anqp.NAIRealmData;
import com.android.server.wifi.hotspot2.anqp.NAIRealmElement;
import com.android.server.wifi.hotspot2.anqp.RoamingConsortiumElement;
import com.android.server.wifi.hotspot2.anqp.ThreeGPPNetworkElement;
import com.android.server.wifi.hotspot2.anqp.eap.AuthParam;
import com.android.server.wifi.hotspot2.anqp.eap.EAPMethod;
import com.android.server.wifi.hotspot2.anqp.eap.NonEAPInnerAuth;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Unit tests for {@link com.android.server.wifi.hotspot2.ANQPMatcher}.
 */
@SmallTest
public class ANQPMatcherTest {
    private static final String TEST_MCC_MNC = "123456";
    private static final String TEST_3GPP_FQDN = String.format("wlan.mnc%s.mcc%s.3gppnetwork.org",
            TEST_MCC_MNC.substring(3), TEST_MCC_MNC.substring(0, 3));
    /**
     * Verify that domain name match will fail when a null Domain Name ANQP element is provided.
     *
     * @throws Exception
     */
    @Test
    public void matchDomainNameWithNullElement() throws Exception {
        assertFalse(ANQPMatcher.matchDomainName(null, "test.com", null, null));
    }

    /**
     * Verify that domain name match will succeed when the specified FQDN matches a domain name
     * in the Domain Name ANQP element.
     *
     * @throws Exception
     */
    @Test
    public void matchDomainNameUsingFQDN() throws Exception {
        String fqdn = "test.com";
        String[] domains = new String[] {fqdn};
        DomainNameElement element = new DomainNameElement(Arrays.asList(domains));
        assertTrue(ANQPMatcher.matchDomainName(element, fqdn, null, null));
    }

    /**
     * Verify that domain name match will succeed when the specified IMSI parameter and IMSI list
     * matches a 3GPP network domain in the Domain Name ANQP element.
     *
     * @throws Exception
     */
    @Test
    public void matchDomainNameUsingIMSI() throws Exception {
        IMSIParameter imsiParam = new IMSIParameter("1234", true);
        List<String> simImsiList = Arrays.asList(new String[] {"123457890", "123498723"});
        // 3GPP network domain with MCC=123 and MNC=456.
        String[] domains = new String[] {"wlan.mnc457.mcc123.3gppnetwork.org"};
        DomainNameElement element = new DomainNameElement(Arrays.asList(domains));
        assertTrue(ANQPMatcher.matchDomainName(element, null, imsiParam, simImsiList));
    }

    /**
     * Verify that roaming consortium match will fail when a null Roaming Consortium ANQP
     * element is provided.
     *
     * @throws Exception
     */
    @Test
    public void matchRoamingConsortiumWithNullElement() throws Exception {
        assertFalse(ANQPMatcher.matchRoamingConsortium(null, new long[0], false));
    }

    /**
     * Verify that a roaming consortium match will succeed when the specified OI matches
     * an OI in the Roaming Consortium ANQP element.
     *
     * @throws Exception
     */
    @Test
    public void matchRoamingConsortium() throws Exception {
        long oi = 0x1234L;
        RoamingConsortiumElement element =
                new RoamingConsortiumElement(Arrays.asList(new Long[] {oi}));
        assertTrue(ANQPMatcher.matchRoamingConsortium(element, new long[] {oi}, false));
    }

    /**
     * Verify that no match will be returned when matching a null NAI Realm
     * ANQP element.
     *
     * @throws Exception
     */
    @Test
    public void matchNAIRealmWithNullElement() throws Exception {
        assertFalse(ANQPMatcher.matchNAIRealm(null, "test.com"));
    }

    /**
     * Verify that no match will be returned when matching a NAI Realm
     * ANQP element contained no NAI realm data.
     *
     * @throws Exception
     */
    @Test
    public void matchNAIRealmWithEmtpyRealmData() throws Exception {
        NAIRealmElement element = new NAIRealmElement(new ArrayList<NAIRealmData>());
        assertFalse(ANQPMatcher.matchNAIRealm(element, "test.com"));
    }

    /**
     * Verify that a realm match will be returned when the specified realm matches a realm
     * in the NAI Realm ANQP element with no EAP methods.
     *
     * @throws Exception
     */
    @Test
    public void matchNAIRealmWithRealmMatch() throws Exception {
        String realm = "test.com";
        NAIRealmData realmData = new NAIRealmData(
                Arrays.asList(new String[] {realm}), new ArrayList<EAPMethod>());
        NAIRealmElement element = new NAIRealmElement(
                Arrays.asList(new NAIRealmData[] {realmData}));
        assertTrue(ANQPMatcher.matchNAIRealm(element, realm));
    }

    /**
     * Verify that a realm match will be returned when the specified realm and EAP
     * method matches a realm in the NAI Realm ANQP element.
     *
     * @throws Exception
     */
    @Test
    public void matchNAIRealmWithRealmMethodMatch() throws Exception {
        // Test data.
        String realm = "test.com";
        int eapMethodID = EAPConstants.EAP_TLS;

        // Setup NAI Realm element.
        EAPMethod method = new EAPMethod(eapMethodID, new HashMap<Integer, Set<AuthParam>>());
        NAIRealmData realmData = new NAIRealmData(
                Arrays.asList(new String[] {realm}), Arrays.asList(new EAPMethod[] {method}));
        NAIRealmElement element = new NAIRealmElement(
                Arrays.asList(new NAIRealmData[] {realmData}));

        assertTrue(ANQPMatcher.matchNAIRealm(element, realm));
    }

    /**
     * Verify that a realm match will be returned when the specified realm, EAP
     * method, and the authentication parameter matches a realm with the associated EAP method and
     * authentication parameter in the NAI Realm ANQP element.
     *
     * @throws Exception
     */
    @Test
    public void matchNAIRealmWithExactMatch() throws Exception {
        // Test data.
        String realm = "test.com";
        int eapMethodID = EAPConstants.EAP_TTLS;
        NonEAPInnerAuth authParam = new NonEAPInnerAuth(NonEAPInnerAuth.AUTH_TYPE_MSCHAP);
        Set<AuthParam> authSet = new HashSet<>();
        authSet.add(authParam);
        Map<Integer, Set<AuthParam>> authMap = new HashMap<>();
        authMap.put(authParam.getAuthTypeID(), authSet);

        // Setup NAI Realm element.
        EAPMethod method = new EAPMethod(eapMethodID, authMap);
        NAIRealmData realmData = new NAIRealmData(
                Arrays.asList(new String[] {realm}), Arrays.asList(new EAPMethod[] {method}));
        NAIRealmElement element = new NAIRealmElement(
                Arrays.asList(new NAIRealmData[] {realmData}));

        assertTrue(ANQPMatcher.matchNAIRealm(element, realm));
    }

    /**
     * Verify that a realm match will be returned when the specified EAP method
     * doesn't match with the corresponding EAP method in the NAI Realm ANQP element.
     *
     * @throws Exception
     */
    @Test
    public void matchNAIRealmWithEAPMethodMismatch() throws Exception {
        // Test data.
        String realm = "test.com";
        int eapMethodID = EAPConstants.EAP_TTLS;
        NonEAPInnerAuth authParam = new NonEAPInnerAuth(NonEAPInnerAuth.AUTH_TYPE_MSCHAP);
        Set<AuthParam> authSet = new HashSet<>();
        authSet.add(authParam);
        Map<Integer, Set<AuthParam>> authMap = new HashMap<>();
        authMap.put(authParam.getAuthTypeID(), authSet);

        // Setup NAI Realm element.
        EAPMethod method = new EAPMethod(eapMethodID, authMap);
        NAIRealmData realmData = new NAIRealmData(
                Arrays.asList(new String[] {realm}), Arrays.asList(new EAPMethod[] {method}));
        NAIRealmElement element = new NAIRealmElement(
                Arrays.asList(new NAIRealmData[] {realmData}));

        assertTrue(ANQPMatcher.matchNAIRealm(element, realm));
    }

    /**
     * Verify that a realm match will be returned when the specified authentication
     * parameter doesn't match with the corresponding authentication parameter in the NAI Realm
     * ANQP element.
     *
     * @throws Exception
     */
    @Test
    public void matchNAIRealmWithAuthTypeMismatch() throws Exception {
        // Test data.
        String realm = "test.com";
        int eapMethodID = EAPConstants.EAP_TTLS;
        NonEAPInnerAuth authParam = new NonEAPInnerAuth(NonEAPInnerAuth.AUTH_TYPE_MSCHAP);
        Set<AuthParam> authSet = new HashSet<>();
        authSet.add(authParam);
        Map<Integer, Set<AuthParam>> authMap = new HashMap<>();
        authMap.put(authParam.getAuthTypeID(), authSet);

        // Setup NAI Realm element.
        EAPMethod method = new EAPMethod(eapMethodID, authMap);
        NAIRealmData realmData = new NAIRealmData(
                Arrays.asList(new String[] {realm}), Arrays.asList(new EAPMethod[] {method}));
        NAIRealmElement element = new NAIRealmElement(
                Arrays.asList(new NAIRealmData[] {realmData}));

        // Mismatch in authentication type which we ignore.
        assertTrue(ANQPMatcher.matchNAIRealm(element, realm));
    }

    /**
     * Verify that 3GPP Network match will fail when a null element is provided.
     *
     * @throws Exception
     */
    @Test
    public void matchThreeGPPNetworkWithNullElement() throws Exception {
        IMSIParameter imsiParam = new IMSIParameter("1234", true);
        List<String> simImsiList = Arrays.asList(new String[] {"123456789", "123498723"});
        assertFalse(ANQPMatcher.matchThreeGPPNetwork(null, imsiParam, simImsiList));
    }

    /**
     * Verify that 3GPP network will succeed when the given 3GPP Network ANQP element contained
     * a MCC-MNC that matches the both IMSI parameter and an IMSI from the IMSI list.
     *
     * @throws Exception
     */
    @Test
    public void matchThreeGPPNetwork() throws Exception {
        IMSIParameter imsiParam = new IMSIParameter("1234", true);
        List<String> simImsiList = Arrays.asList(new String[] {"123456789", "123498723"});

        CellularNetwork network = new CellularNetwork(Arrays.asList(new String[] {"123456"}));
        ThreeGPPNetworkElement element =
                new ThreeGPPNetworkElement(Arrays.asList(new CellularNetwork[] {network}));
        // The MCC-MNC provided in 3GPP Network ANQP element matches both IMSI parameter
        // and an IMSI from the installed SIM card.
        assertTrue(ANQPMatcher.matchThreeGPPNetwork(element, imsiParam, simImsiList));
    }

    /**
     * Verify that 3GPP network will failed when the given 3GPP Network ANQP element contained
     * a MCC-MNC that match the IMSI parameter but not the IMSI list.
     *
     * @throws Exception
     */
    @Test
    public void matchThreeGPPNetworkWithoutSimImsiMatch() throws Exception {
        IMSIParameter imsiParam = new IMSIParameter("1234", true);
        List<String> simImsiList = Arrays.asList(new String[] {"123457890", "123498723"});

        CellularNetwork network = new CellularNetwork(Arrays.asList(new String[] {"123456"}));
        ThreeGPPNetworkElement element =
                new ThreeGPPNetworkElement(Arrays.asList(new CellularNetwork[] {network}));
        // The MCC-MNC provided in 3GPP Network ANQP element doesn't match any of the IMSIs
        // from the installed SIM card.
        assertFalse(ANQPMatcher.matchThreeGPPNetwork(element, imsiParam, simImsiList));
    }

    /**
     * Verify that 3GPP network will failed when the given 3GPP Network ANQP element contained
     * a MCC-MNC that doesn't match with the IMSI parameter.
     *
     * @throws Exception
     */
    @Test
    public void matchThreeGPPNetworkWithImsiParamMismatch() throws Exception {
        IMSIParameter imsiParam = new IMSIParameter("1234", true);
        List<String> simImsiList = Arrays.asList(new String[] {"123457890", "123498723"});

        CellularNetwork network = new CellularNetwork(Arrays.asList(new String[] {"123356"}));
        ThreeGPPNetworkElement element =
                new ThreeGPPNetworkElement(Arrays.asList(new CellularNetwork[] {network}));
        // The MCC-MNC provided in 3GPP Network ANQP element doesn't match the IMSI parameter.
        assertFalse(ANQPMatcher.matchThreeGPPNetwork(element, imsiParam, simImsiList));
    }

    /**
     * Verify that it will return a EAP-Method from the NAI realm when there is a matched realm in
     * the NAIRealm element.
     */
    @Test
    public void getEapMethodForNAIRealmWithCarrierInMatch() {
        // Test data.
        String realm = TEST_3GPP_FQDN;
        int eapMethodID = EAPConstants.EAP_AKA;

        // Create a realm that has the EAP method
        EAPMethod method = new EAPMethod(eapMethodID, null);
        NAIRealmData realmData = new NAIRealmData(
                Arrays.asList(new String[]{realm}), Arrays.asList(new EAPMethod[]{method}));

        // Setup NAI Realm element.
        NAIRealmElement element = new NAIRealmElement(
                Arrays.asList(new NAIRealmData[]{realmData}));

        assertEquals(EAPConstants.EAP_AKA,
                ANQPMatcher.getCarrierEapMethodFromMatchingNAIRealm(TEST_3GPP_FQDN, element));
    }

    /**
     * Verify that it will return -1 when there is a matched realm in the NAIRealm element, but it
     * does not have a EAP Method.
     */
    @Test
    public void getEapMethodForNAIRealmWithCarrierInMatchButNotEapMethod() {
        // Test data.
        String realm = TEST_3GPP_FQDN;
        NAIRealmData realmData = new NAIRealmData(Arrays.asList(new String[]{realm}),
                new ArrayList<>());

        // Setup NAI Realm element.
        NAIRealmElement element = new NAIRealmElement(
                Arrays.asList(new NAIRealmData[]{realmData}));

        assertEquals(-1,
                ANQPMatcher.getCarrierEapMethodFromMatchingNAIRealm(TEST_3GPP_FQDN, element));
    }

    /**
     * Verify that it will return -1 when there is a matched realm in the NAIRealm element, but it
     * does have non-carrier EAP-method.
     */
    @Test
    public void getEapMethodForNAIRealmWithCarrierInMatchButNotCarrierEapMethod() {
        // Test data.
        String realm = TEST_3GPP_FQDN;
        int eapMethodID = EAPConstants.EAP_TTLS;
        NonEAPInnerAuth authParam = new NonEAPInnerAuth(NonEAPInnerAuth.AUTH_TYPE_MSCHAP);
        Set<AuthParam> authSet = new HashSet<>();
        authSet.add(authParam);
        Map<Integer, Set<AuthParam>> authMap = new HashMap<>();
        authMap.put(authParam.getAuthTypeID(), authSet);

        // Setup NAI Realm element.
        EAPMethod method = new EAPMethod(eapMethodID, authMap);
        NAIRealmData realmData = new NAIRealmData(
                Arrays.asList(new String[]{realm}), Arrays.asList(new EAPMethod[]{method}));
        NAIRealmElement element = new NAIRealmElement(
                Arrays.asList(new NAIRealmData[]{realmData}));

        assertEquals(-1,
                ANQPMatcher.getCarrierEapMethodFromMatchingNAIRealm(TEST_3GPP_FQDN, element));
    }

    /**
     * Verify that it will return -1 when there is no matched realm in the NAIRealm element.
     */
    @Test
    public void getEapMethodForNAIRealmWithCarrierInNoMatch() {
        // Test data.
        String realm = "test.com";
        int eapMethodID = EAPConstants.EAP_TTLS;
        NonEAPInnerAuth authParam = new NonEAPInnerAuth(NonEAPInnerAuth.AUTH_TYPE_MSCHAP);
        Set<AuthParam> authSet = new HashSet<>();
        authSet.add(authParam);
        Map<Integer, Set<AuthParam>> authMap = new HashMap<>();
        authMap.put(authParam.getAuthTypeID(), authSet);

        // Setup NAI Realm element.
        EAPMethod method = new EAPMethod(eapMethodID, authMap);
        NAIRealmData realmData = new NAIRealmData(
                Arrays.asList(new String[]{realm}), Arrays.asList(new EAPMethod[]{method}));
        NAIRealmElement element = new NAIRealmElement(
                Arrays.asList(new NAIRealmData[]{realmData}));

        assertEquals(-1,
                ANQPMatcher.getCarrierEapMethodFromMatchingNAIRealm(TEST_3GPP_FQDN, element));
    }

    /**
     * Verify that match is found when HomeOI contains some of the RCOIs advertised by an AP marked
     * as not required.
     *
     * @throws Exception
     */
    @Test
    public void matchAnyHomeOi() throws Exception {
        long[] providerOis = new long[] {0x1234L, 0x5678L, 0xabcdL};
        Long[] anqpOis = new Long[] {0x1234L, 0x5678L, 0xdeadL, 0xf0cdL};
        RoamingConsortiumElement element =
                new RoamingConsortiumElement(Arrays.asList(anqpOis));
        assertTrue(ANQPMatcher.matchRoamingConsortium(element, providerOis, false));
    }

    /**
     * Verify that no match is found when HomeOI does not contain any of the RCOIs advertised by an
     * AP marked as not required.
     *
     * @throws Exception
     */
    @Test
    public void matchAnyHomeOiNegative() throws Exception {
        long[] providerOis = new long[] {0x1234L, 0x5678L, 0xabcdL};
        Long[] anqpOis = new Long[] {0xabc2L, 0x1232L};
        RoamingConsortiumElement element =
                new RoamingConsortiumElement(Arrays.asList(anqpOis));
        assertFalse(ANQPMatcher.matchRoamingConsortium(element, providerOis, false));
    }

    /**
     * Verify that match is found when HomeOI contains all of the RCOIs advertised by an AP marked
     * as required.
     *
     * @throws Exception
     */
    @Test
    public void matchAllHomeOi() throws Exception {
        long[] providerOis = new long[] {0x1234L, 0x5678L, 0xabcdL};
        Long[] anqpOis = new Long[] {0x1234L, 0x5678L, 0xabcdL, 0xdeadL, 0xf0cdL};
        RoamingConsortiumElement element =
                new RoamingConsortiumElement(Arrays.asList(anqpOis));
        assertTrue(ANQPMatcher.matchRoamingConsortium(element, providerOis, true));
    }

    /**
     * Verify that match is not found when HomeOI does not contain all of the RCOIs advertised by an
     * AP marked as required.
     *
     * @throws Exception
     */
    @Test
    public void matchAllHomeOiNegative() throws Exception {
        long[] providerOis = new long[] {0x1234L, 0x5678L, 0xabcdL};
        Long[] anqpOis = new Long[] {0x1234L, 0x5678L, 0xdeadL, 0xf0cdL};
        RoamingConsortiumElement element =
                new RoamingConsortiumElement(Arrays.asList(anqpOis));
        assertFalse(ANQPMatcher.matchRoamingConsortium(element, providerOis, true));
    }
}