TelephonyUtilTest.java revision 259fb5a5265e0a2aa8a851d5e694d28afe9a87f2
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.util;
18
19import static org.junit.Assert.*;
20import static org.mockito.Mockito.*;
21
22import android.net.wifi.WifiEnterpriseConfig;
23import android.telephony.TelephonyManager;
24import android.test.suitebuilder.annotation.SmallTest;
25import android.util.Base64;
26
27import com.android.server.wifi.util.TelephonyUtil.SimAuthRequestData;
28import com.android.server.wifi.util.TelephonyUtil.SimAuthResponseData;
29
30import org.junit.Test;
31
32/**
33 * Unit tests for {@link com.android.server.wifi.util.TelephonyUtil}.
34 */
35@SmallTest
36public class TelephonyUtilTest {
37
38    @Test
39    public void getSimIdentityEapSim() {
40        TelephonyManager tm = mock(TelephonyManager.class);
41        when(tm.getSubscriberId()).thenReturn("3214561234567890");
42        when(tm.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY);
43        when(tm.getSimOperator()).thenReturn("321456");
44        assertEquals("13214561234567890@wlan.mnc456.mcc321.3gppnetwork.org",
45                TelephonyUtil.getSimIdentity(tm, WifiEnterpriseConfig.Eap.SIM));
46    }
47
48    @Test
49    public void getSimIdentityEapAka() {
50        TelephonyManager tm = mock(TelephonyManager.class);
51        when(tm.getSubscriberId()).thenReturn("3214561234567890");
52        when(tm.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY);
53        when(tm.getSimOperator()).thenReturn("321456");
54        assertEquals("03214561234567890@wlan.mnc456.mcc321.3gppnetwork.org",
55                TelephonyUtil.getSimIdentity(tm, WifiEnterpriseConfig.Eap.AKA));
56    }
57
58    @Test
59    public void getSimIdentityEapAkaPrime() {
60        TelephonyManager tm = mock(TelephonyManager.class);
61        when(tm.getSubscriberId()).thenReturn("3214561234567890");
62        when(tm.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY);
63        when(tm.getSimOperator()).thenReturn("321456");
64        assertEquals("63214561234567890@wlan.mnc456.mcc321.3gppnetwork.org",
65                TelephonyUtil.getSimIdentity(tm, WifiEnterpriseConfig.Eap.AKA_PRIME));
66    }
67
68    @Test
69    public void getSimIdentity2DigitMnc() {
70        TelephonyManager tm = mock(TelephonyManager.class);
71        when(tm.getSubscriberId()).thenReturn("321560123456789");
72        when(tm.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY);
73        when(tm.getSimOperator()).thenReturn("32156");
74        assertEquals("1321560123456789@wlan.mnc056.mcc321.3gppnetwork.org",
75                TelephonyUtil.getSimIdentity(tm, WifiEnterpriseConfig.Eap.SIM));
76    }
77
78    @Test
79    public void getSimIdentityUnknownMccMnc() {
80        TelephonyManager tm = mock(TelephonyManager.class);
81        when(tm.getSubscriberId()).thenReturn("3214560123456789");
82        when(tm.getSimState()).thenReturn(TelephonyManager.SIM_STATE_UNKNOWN);
83        when(tm.getSimOperator()).thenReturn(null);
84        assertEquals("13214560123456789@wlan.mnc456.mcc321.3gppnetwork.org",
85                TelephonyUtil.getSimIdentity(tm, WifiEnterpriseConfig.Eap.SIM));
86    }
87
88
89
90
91    /**
92     * Produce a base64 encoded length byte + data.
93     */
94    private static String createSimChallengeRequest(byte[] challengeValue) {
95        byte[] challengeLengthAndValue = new byte[challengeValue.length + 1];
96        challengeLengthAndValue[0] = (byte) challengeValue.length;
97        for (int i = 0; i < challengeValue.length; ++i) {
98            challengeLengthAndValue[i + 1] = challengeValue[i];
99        }
100        return Base64.encodeToString(challengeLengthAndValue, android.util.Base64.NO_WRAP);
101    }
102
103    /**
104     * Produce a base64 encoded sres length byte + sres + kc length byte + kc.
105     */
106    private static String createGsmSimAuthResponse(byte[] sresValue, byte[] kcValue) {
107        int overallLength = sresValue.length + kcValue.length + 2;
108        byte[] result = new byte[sresValue.length + kcValue.length + 2];
109        int idx = 0;
110        result[idx++] = (byte) sresValue.length;
111        for (int i = 0; i < sresValue.length; ++i) {
112            result[idx++] = sresValue[i];
113        }
114        result[idx++] = (byte) kcValue.length;
115        for (int i = 0; i < kcValue.length; ++i) {
116            result[idx++] = kcValue[i];
117        }
118        return Base64.encodeToString(result, Base64.NO_WRAP);
119    }
120
121    @Test
122    public void getGsmSimAuthResponseInvalidRequest() {
123        TelephonyManager tm = mock(TelephonyManager.class);
124        final String[] invalidRequests = { null, "", "XXXX" };
125        assertEquals("", TelephonyUtil.getGsmSimAuthResponse(invalidRequests, tm));
126    }
127
128    @Test
129    public void getGsmSimAuthResponseFailedSimResponse() {
130        TelephonyManager tm = mock(TelephonyManager.class);
131        final String[] failedRequests = { "5E5F" };
132        when(tm.getIccAuthentication(anyInt(), anyInt(),
133                eq(createSimChallengeRequest(new byte[] { 0x5e, 0x5f })))).thenReturn(null);
134
135        assertEquals(null, TelephonyUtil.getGsmSimAuthResponse(failedRequests, tm));
136    }
137
138    @Test
139    public void getGsmSimAuthResponseUsim() {
140        TelephonyManager tm = mock(TelephonyManager.class);
141        when(tm.getIccAuthentication(TelephonyManager.APPTYPE_USIM,
142                        TelephonyManager.AUTHTYPE_EAP_SIM,
143                        createSimChallengeRequest(new byte[] { 0x1b, 0x2b })))
144                .thenReturn(createGsmSimAuthResponse(new byte[] { 0x1D, 0x2C },
145                                new byte[] { 0x3B, 0x4A }));
146        when(tm.getIccAuthentication(TelephonyManager.APPTYPE_USIM,
147                        TelephonyManager.AUTHTYPE_EAP_SIM,
148                        createSimChallengeRequest(new byte[] { 0x01, 0x22 })))
149                .thenReturn(createGsmSimAuthResponse(new byte[] { 0x11, 0x11 },
150                                new byte[] { 0x12, 0x34 }));
151
152        assertEquals(":3b4a:1d2c:1234:1111", TelephonyUtil.getGsmSimAuthResponse(
153                        new String[] { "1B2B", "0122" }, tm));
154    }
155
156    @Test
157    public void getGsmSimAuthResponseSimpleSim() {
158        TelephonyManager tm = mock(TelephonyManager.class);
159        when(tm.getIccAuthentication(TelephonyManager.APPTYPE_USIM,
160                        TelephonyManager.AUTHTYPE_EAP_SIM,
161                        createSimChallengeRequest(new byte[] { 0x1a, 0x2b })))
162                .thenReturn(null);
163        when(tm.getIccAuthentication(TelephonyManager.APPTYPE_SIM,
164                        TelephonyManager.AUTHTYPE_EAP_SIM,
165                        createSimChallengeRequest(new byte[] { 0x1a, 0x2b })))
166                .thenReturn(createGsmSimAuthResponse(new byte[] { 0x1D, 0x2C },
167                                new byte[] { 0x3B, 0x4A }));
168        when(tm.getIccAuthentication(TelephonyManager.APPTYPE_USIM,
169                        TelephonyManager.AUTHTYPE_EAP_SIM,
170                        createSimChallengeRequest(new byte[] { 0x01, 0x23 })))
171                .thenReturn(null);
172        when(tm.getIccAuthentication(TelephonyManager.APPTYPE_SIM,
173                        TelephonyManager.AUTHTYPE_EAP_SIM,
174                        createSimChallengeRequest(new byte[] { 0x01, 0x23 })))
175                .thenReturn(createGsmSimAuthResponse(new byte[] { 0x33, 0x22 },
176                                new byte[] { 0x11, 0x00 }));
177
178        assertEquals(":3b4a:1d2c:1100:3322", TelephonyUtil.getGsmSimAuthResponse(
179                        new String[] { "1A2B", "0123" }, tm));
180    }
181
182    /**
183     * Produce a base64 encoded tag + res length byte + res + ck length byte + ck + ik length byte +
184     * ik.
185     */
186    private static String create3GSimAuthUmtsAuthResponse(byte[] res, byte[] ck, byte[] ik) {
187        byte[] result = new byte[res.length + ck.length + ik.length + 4];
188        int idx = 0;
189        result[idx++] = (byte) 0xdb;
190        result[idx++] = (byte) res.length;
191        for (int i = 0; i < res.length; ++i) {
192            result[idx++] = res[i];
193        }
194        result[idx++] = (byte) ck.length;
195        for (int i = 0; i < ck.length; ++i) {
196            result[idx++] = ck[i];
197        }
198        result[idx++] = (byte) ik.length;
199        for (int i = 0; i < ik.length; ++i) {
200            result[idx++] = ik[i];
201        }
202        return Base64.encodeToString(result, Base64.NO_WRAP);
203    }
204
205    private static String create3GSimAuthUmtsAutsResponse(byte[] auts) {
206        byte[] result = new byte[auts.length + 2];
207        int idx = 0;
208        result[idx++] = (byte) 0xdc;
209        result[idx++] = (byte) auts.length;
210        for (int i = 0; i < auts.length; ++i) {
211            result[idx++] = auts[i];
212        }
213        return Base64.encodeToString(result, Base64.NO_WRAP);
214    }
215
216    @Test
217    public void get3GAuthResponseInvalidRequest() {
218        TelephonyManager tm = mock(TelephonyManager.class);
219        assertEquals(null, TelephonyUtil.get3GAuthResponse(
220                        new SimAuthRequestData(0, 0, "SSID", new String[] {"0123"}), tm));
221        assertEquals(null, TelephonyUtil.get3GAuthResponse(
222                        new SimAuthRequestData(0, 0, "SSID", new String[] {"xyz2", "1234"}), tm));
223        verifyNoMoreInteractions(tm);
224    }
225
226    @Test
227    public void get3GAuthResponseNullIccAuthentication() {
228        TelephonyManager tm = mock(TelephonyManager.class);
229
230        when(tm.getIccAuthentication(TelephonyManager.APPTYPE_USIM,
231                        TelephonyManager.AUTHTYPE_EAP_AKA, "AgEjAkVn")).thenReturn(null);
232
233        SimAuthResponseData response = TelephonyUtil.get3GAuthResponse(
234                new SimAuthRequestData(0, 0, "SSID", new String[] {"0123", "4567"}), tm);
235        assertNull(response);
236    }
237
238    @Test
239    public void get3GAuthResponseIccAuthenticationTooShort() {
240        TelephonyManager tm = mock(TelephonyManager.class);
241
242        when(tm.getIccAuthentication(TelephonyManager.APPTYPE_USIM,
243                        TelephonyManager.AUTHTYPE_EAP_AKA, "AgEjAkVn"))
244                .thenReturn(Base64.encodeToString(new byte[] {(byte) 0xdc}, Base64.NO_WRAP));
245
246        SimAuthResponseData response = TelephonyUtil.get3GAuthResponse(
247                new SimAuthRequestData(0, 0, "SSID", new String[] {"0123", "4567"}), tm);
248        assertNull(response);
249    }
250
251    @Test
252    public void get3GAuthResponseBadTag() {
253        TelephonyManager tm = mock(TelephonyManager.class);
254
255        when(tm.getIccAuthentication(TelephonyManager.APPTYPE_USIM,
256                        TelephonyManager.AUTHTYPE_EAP_AKA, "AgEjAkVn"))
257                .thenReturn(Base64.encodeToString(new byte[] {0x31, 0x1, 0x2, 0x3, 0x4},
258                                Base64.NO_WRAP));
259
260        SimAuthResponseData response = TelephonyUtil.get3GAuthResponse(
261                new SimAuthRequestData(0, 0, "SSID", new String[] {"0123", "4567"}), tm);
262        assertNull(response);
263    }
264
265    @Test
266    public void get3GAuthResponseUmtsAuth() {
267        TelephonyManager tm = mock(TelephonyManager.class);
268
269        when(tm.getIccAuthentication(TelephonyManager.APPTYPE_USIM,
270                        TelephonyManager.AUTHTYPE_EAP_AKA, "AgEjAkVn"))
271                .thenReturn(create3GSimAuthUmtsAuthResponse(new byte[] {0x11, 0x12},
272                                new byte[] {0x21, 0x22, 0x23}, new byte[] {0x31}));
273
274        SimAuthResponseData response = TelephonyUtil.get3GAuthResponse(
275                new SimAuthRequestData(0, 0, "SSID", new String[] {"0123", "4567"}), tm);
276        assertNotNull(response);
277        assertEquals("UMTS-AUTH", response.type);
278        assertEquals(":31:212223:1112", response.response);
279    }
280
281    @Test
282    public void get3GAuthResponseUmtsAuts() {
283        TelephonyManager tm = mock(TelephonyManager.class);
284
285        when(tm.getIccAuthentication(TelephonyManager.APPTYPE_USIM,
286                        TelephonyManager.AUTHTYPE_EAP_AKA, "AgEjAkVn"))
287                .thenReturn(create3GSimAuthUmtsAutsResponse(new byte[] {0x22, 0x33}));
288
289        SimAuthResponseData response = TelephonyUtil.get3GAuthResponse(
290                new SimAuthRequestData(0, 0, "SSID", new String[] {"0123", "4567"}), tm);
291        assertNotNull(response);
292        assertEquals("UMTS-AUTS", response.type);
293        assertEquals(":2233", response.response);
294    }
295}
296