1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wifi;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertFalse;
21import static org.junit.Assert.assertTrue;
22
23import android.test.suitebuilder.annotation.SmallTest;
24
25import org.junit.Test;
26
27import java.util.HashMap;
28import java.util.Map;
29
30/**
31 * Unit tests for {@link com.android.server.wifi.IMSIParameter}.
32 */
33@SmallTest
34public class IMSIParameterTest {
35    /**
36     * Data points for testing function {@link IMSIParameter#build}.
37     */
38    private static final Map<String, IMSIParameter> BUILD_PARAM_TEST_MAP = new HashMap<>();
39    static {
40        BUILD_PARAM_TEST_MAP.put(null, null);
41        BUILD_PARAM_TEST_MAP.put("", null);
42        BUILD_PARAM_TEST_MAP.put("1234a123", null);    // IMSI contained invalid character.
43        BUILD_PARAM_TEST_MAP.put("1234567890123451", null);   // IMSI exceeding max length.
44        BUILD_PARAM_TEST_MAP.put("123456789012345", new IMSIParameter("123456789012345", false));
45        BUILD_PARAM_TEST_MAP.put("1234*", new IMSIParameter("1234", true));
46    }
47
48    /**
49     * Verify the expectations of {@link IMSIParameter#build} function using the predefined
50     * test data {@link #BUILD_PARAM_TEST_MAP}.
51     *
52     * @throws Exception
53     */
54    @Test
55    public void verifyBuildIMSIParameter() throws Exception {
56        for (Map.Entry<String, IMSIParameter> entry : BUILD_PARAM_TEST_MAP.entrySet()) {
57            assertEquals(entry.getValue(), IMSIParameter.build(entry.getKey()));
58        }
59    }
60
61    /**
62     * Verify that attempt to match a null IMSI will not cause any crash and should return false.
63     *
64     * @throws Exception
65     */
66    @Test
67    public void matchesNullImsi() throws Exception {
68        IMSIParameter param = new IMSIParameter("1234", false);
69        assertFalse(param.matchesImsi(null));
70    }
71
72    /**
73     * Verify that an IMSIParameter containing a full IMSI will only match against an IMSI of the
74     * same value.
75     *
76     * @throws Exception
77     */
78    @Test
79    public void matchesImsiWithFullImsi() throws Exception {
80        IMSIParameter param = new IMSIParameter("1234", false);
81
82        // Full IMSI requires exact matching.
83        assertFalse(param.matchesImsi("123"));
84        assertFalse(param.matchesImsi("12345"));
85        assertTrue(param.matchesImsi("1234"));
86    }
87
88    /**
89     * Verify that an IMSIParameter containing a prefix IMSI will match against any IMSI that
90     * starts with the same prefix.
91     *
92     * @throws Exception
93     */
94    @Test
95    public void matchesImsiWithPrefixImsi() throws Exception {
96        IMSIParameter param = new IMSIParameter("1234", true);
97
98        // Prefix IMSI will match any IMSI that starts with the same prefix.
99        assertFalse(param.matchesImsi("123"));
100        assertTrue(param.matchesImsi("12345"));
101        assertTrue(param.matchesImsi("1234"));
102    }
103
104    /**
105     * Verify that attempt to match a null MCC-MNC will not cause any crash and should return
106     * false.
107     *
108     * @throws Exception
109     */
110    @Test
111    public void matchesNullMccMnc() throws Exception {
112        IMSIParameter param = new IMSIParameter("1234", false);
113        assertFalse(param.matchesMccMnc(null));
114    }
115
116    /**
117     * Verify that an IMSIParameter containing a full IMSI will only match against a 6 digit
118     * MCC-MNC that is a prefix of the IMSI.
119     *
120     * @throws Exception
121     */
122    @Test
123    public void matchesMccMncFullImsi() throws Exception {
124        IMSIParameter param = new IMSIParameter("1234567890", false);
125
126        assertFalse(param.matchesMccMnc("1234567"));    // Invalid length for MCC-MNC
127        assertFalse(param.matchesMccMnc("12345"));      // Invalid length for MCC-MNC
128        assertTrue(param.matchesMccMnc("123456"));
129    }
130
131    /**
132     * Verify that an IMSIParameter containing an IMSI prefix that's less than 6 digits
133     * will match against any 6-digit MCC-MNC that starts with the same prefix.
134     *
135     * @throws Exception
136     */
137    @Test
138    public void matchesMccMncWithPrefixImsiLessThanMccMncLength() throws Exception {
139        IMSIParameter param = new IMSIParameter("12345", true);
140
141        assertFalse(param.matchesMccMnc("123448"));     // Prefix mismatch
142        assertFalse(param.matchesMccMnc("12345"));      // Invalid length for MCC-MNC
143        assertFalse(param.matchesMccMnc("1234567"));    // Invalid length for MCC-MNC
144        assertTrue(param.matchesMccMnc("123457"));
145        assertTrue(param.matchesMccMnc("123456"));
146    }
147
148    /**
149     * Verify that an IMSIParameter containing an IMSI prefix that's more than 6 digits
150     * will only match against a 6-digit MCC-MNC that matches the first 6-digit of the prefix.
151     *
152     * @throws Exception
153     */
154    @Test
155    public void matchesMccMncWithPrefixImsiMoreThanMccMncLength() throws Exception {
156        IMSIParameter param = new IMSIParameter("1234567890", true);
157        assertFalse(param.matchesMccMnc("12345"));      // Invalid length for MCC-MNC
158        assertFalse(param.matchesMccMnc("1234567"));    // Invalid length for MCC-MNC
159        assertTrue(param.matchesMccMnc("123456"));
160    }
161}
162