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.AdditionalMatchers.aryEq;
21import static org.mockito.Mockito.*;
22
23import android.net.wifi.WifiConfiguration;
24import android.net.wifi.WifiEnterpriseConfig;
25import android.support.test.filters.SmallTest;
26import android.telephony.ImsiEncryptionInfo;
27import android.telephony.TelephonyManager;
28import android.util.Base64;
29import android.util.Pair;
30
31import com.android.server.wifi.WifiConfigurationTestUtil;
32import com.android.server.wifi.util.TelephonyUtil.SimAuthRequestData;
33import com.android.server.wifi.util.TelephonyUtil.SimAuthResponseData;
34
35import org.junit.Test;
36
37import java.security.PublicKey;
38
39/**
40 * Unit tests for {@link com.android.server.wifi.util.TelephonyUtil}.
41 */
42@SmallTest
43public class TelephonyUtilTest {
44    @Test
45    public void getSimIdentityEapSim() {
46        TelephonyManager tm = mock(TelephonyManager.class);
47        TelephonyUtil telephonyUtil = mock(TelephonyUtil.class);
48        final Pair<String, String> expectedIdentity = Pair.create(
49                "13214561234567890@wlan.mnc456.mcc321.3gppnetwork.org", "");
50
51        when(tm.getSubscriberId()).thenReturn("3214561234567890");
52        when(tm.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY);
53        when(tm.getSimOperator()).thenReturn("321456");
54        when(tm.getCarrierInfoForImsiEncryption(anyInt())).thenReturn(null);
55
56        assertEquals(expectedIdentity, TelephonyUtil.getSimIdentity(tm, telephonyUtil,
57                WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.SIM,
58                        WifiEnterpriseConfig.Phase2.NONE)));
59        assertEquals(expectedIdentity, TelephonyUtil.getSimIdentity(tm, telephonyUtil,
60                WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.PEAP,
61                        WifiEnterpriseConfig.Phase2.SIM)));
62    }
63
64    @Test
65    public void getSimIdentityEapAka() {
66        TelephonyManager tm = mock(TelephonyManager.class);
67        TelephonyUtil telephonyUtil = mock(TelephonyUtil.class);
68        final Pair<String, String> expectedIdentity = Pair.create(
69                "03214561234567890@wlan.mnc456.mcc321.3gppnetwork.org", "");
70        when(tm.getSubscriberId()).thenReturn("3214561234567890");
71
72        when(tm.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY);
73        when(tm.getSimOperator()).thenReturn("321456");
74        when(tm.getCarrierInfoForImsiEncryption(anyInt())).thenReturn(null);
75
76        assertEquals(expectedIdentity, TelephonyUtil.getSimIdentity(tm, telephonyUtil,
77                WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.AKA,
78                        WifiEnterpriseConfig.Phase2.NONE)));
79        assertEquals(expectedIdentity, TelephonyUtil.getSimIdentity(tm, telephonyUtil,
80                WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.PEAP,
81                        WifiEnterpriseConfig.Phase2.AKA)));
82    }
83
84    @Test
85    public void getSimIdentityEapAkaPrime() {
86        TelephonyManager tm = mock(TelephonyManager.class);
87        TelephonyUtil telephonyUtil = mock(TelephonyUtil.class);
88        final Pair<String, String> expectedIdentity = Pair.create(
89                "63214561234567890@wlan.mnc456.mcc321.3gppnetwork.org", "");
90
91        when(tm.getSubscriberId()).thenReturn("3214561234567890");
92        when(tm.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY);
93        when(tm.getSimOperator()).thenReturn("321456");
94        when(tm.getCarrierInfoForImsiEncryption(anyInt())).thenReturn(null);
95
96        assertEquals(expectedIdentity, TelephonyUtil.getSimIdentity(tm, telephonyUtil,
97                WifiConfigurationTestUtil.createEapNetwork(
98                        WifiEnterpriseConfig.Eap.AKA_PRIME,
99                        WifiEnterpriseConfig.Phase2.NONE)));
100        assertEquals(expectedIdentity, TelephonyUtil.getSimIdentity(tm, telephonyUtil,
101                WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.PEAP,
102                        WifiEnterpriseConfig.Phase2.AKA_PRIME)));
103    }
104
105    /**
106     * Verify that an expected identity is returned when using the encrypted IMSI.
107     *
108     * @throws Exception
109     */
110    @Test
111    public void getEncryptedIdentityImsi() throws Exception {
112        TelephonyManager tm = mock(TelephonyManager.class);
113        TelephonyUtil telephonyUtil = mock(TelephonyUtil.class);
114        String encryptedImsi = "EncryptedIMSI";
115        String encryptedIdentity = "\0" + encryptedImsi + "@wlan.mnc456.mcc321.3gppnetwork.org";
116        final Pair<String, String> expectedIdentity = Pair.create(
117                "03214561234567890@wlan.mnc456.mcc321.3gppnetwork.org", encryptedIdentity);
118        PublicKey key = null;
119
120        when(tm.getSubscriberId()).thenReturn("3214561234567890");
121        when(tm.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY);
122        when(tm.getSimOperator()).thenReturn("321456");
123        ImsiEncryptionInfo info = new ImsiEncryptionInfo("321", "456",
124                TelephonyManager.KEY_TYPE_WLAN, null, key, null);
125        when(tm.getCarrierInfoForImsiEncryption(eq(TelephonyManager.KEY_TYPE_WLAN)))
126                .thenReturn(info);
127
128        when(telephonyUtil.encryptDataUsingPublicKey(any(), any())).thenReturn(encryptedImsi);
129
130        assertEquals(expectedIdentity, TelephonyUtil.getSimIdentity(tm, telephonyUtil,
131                WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.AKA,
132                        WifiEnterpriseConfig.Phase2.NONE)));
133    }
134
135    /**
136     * Verify that an expected identity is returned when using the encrypted IMSI with key
137     * identifier.
138     *
139     * @throws Exception
140     */
141    @Test
142    public void getEncryptedIdentityKeyIdentifier() throws Exception {
143        TelephonyManager tm = mock(TelephonyManager.class);
144        TelephonyUtil telephonyUtil = mock(TelephonyUtil.class);
145        PublicKey key = null;
146        String keyIdentifier = "key=testKey";
147        String encryptedImsi = "EncryptedIMSI";
148        String encryptedIdentity = "\0" + encryptedImsi + "@wlan.mnc456.mcc321.3gppnetwork.org,"
149                + keyIdentifier;
150        final Pair<String, String> expectedIdentity = Pair.create(
151                "03214561234567890@wlan.mnc456.mcc321.3gppnetwork.org", encryptedIdentity);
152
153        when(tm.getSubscriberId()).thenReturn("3214561234567890");
154        when(tm.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY);
155        when(tm.getSimOperator()).thenReturn("321456");
156        ImsiEncryptionInfo info = new ImsiEncryptionInfo("321", "456",
157                TelephonyManager.KEY_TYPE_WLAN, keyIdentifier, key, null);
158        when(tm.getCarrierInfoForImsiEncryption(eq(TelephonyManager.KEY_TYPE_WLAN)))
159                .thenReturn(info);
160
161        when(telephonyUtil.encryptDataUsingPublicKey(any(), any())).thenReturn(encryptedImsi);
162
163        assertEquals(expectedIdentity, TelephonyUtil.getSimIdentity(tm, telephonyUtil,
164                WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.AKA,
165                        WifiEnterpriseConfig.Phase2.NONE)));
166    }
167
168    /**
169     * Verify that a null identity will be returned when IMSI encryption failed.
170     *
171     * @throws Exception
172     */
173    @Test
174    public void getEncryptedIdentityFailed() throws Exception {
175        TelephonyManager tm = mock(TelephonyManager.class);
176        TelephonyUtil telephonyUtil = mock(TelephonyUtil.class);
177        PublicKey key = null;
178        String imsi = "3214561234567890";
179        final Pair<String, String> expectedIdentity = Pair.create(
180                "03214561234567890@wlan.mnc456.mcc321.3gppnetwork.org", "");
181
182        when(tm.getSubscriberId()).thenReturn("3214561234567890");
183        when(tm.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY);
184        when(tm.getSimOperator()).thenReturn("321456");
185        ImsiEncryptionInfo info = new ImsiEncryptionInfo("321", "456",
186                TelephonyManager.KEY_TYPE_WLAN, null, key, null);
187        when(tm.getCarrierInfoForImsiEncryption(eq(TelephonyManager.KEY_TYPE_WLAN)))
188                .thenReturn(info);
189        when(telephonyUtil.encryptDataUsingPublicKey(any(), aryEq(imsi.getBytes())))
190                .thenReturn(null);
191
192        assertEquals(expectedIdentity, TelephonyUtil.getSimIdentity(tm, telephonyUtil,
193                WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.AKA,
194                        WifiEnterpriseConfig.Phase2.NONE)));
195    }
196
197    @Test
198    public void getSimIdentity2DigitMnc() {
199        TelephonyManager tm = mock(TelephonyManager.class);
200        TelephonyUtil telephonyUtil = mock(TelephonyUtil.class);
201        final Pair<String, String> expectedIdentity = Pair.create(
202                "1321560123456789@wlan.mnc056.mcc321.3gppnetwork.org", "");
203
204        when(tm.getSubscriberId()).thenReturn("321560123456789");
205        when(tm.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY);
206        when(tm.getSimOperator()).thenReturn("32156");
207        when(tm.getCarrierInfoForImsiEncryption(anyInt())).thenReturn(null);
208
209        assertEquals(expectedIdentity, TelephonyUtil.getSimIdentity(tm, telephonyUtil,
210                WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.SIM,
211                        WifiEnterpriseConfig.Phase2.NONE)));
212    }
213
214    @Test
215    public void getSimIdentityUnknownMccMnc() {
216        TelephonyManager tm = mock(TelephonyManager.class);
217        TelephonyUtil telephonyUtil = mock(TelephonyUtil.class);
218        final Pair<String, String> expectedIdentity = Pair.create(
219                "13214560123456789@wlan.mnc456.mcc321.3gppnetwork.org", "");
220
221        when(tm.getSubscriberId()).thenReturn("3214560123456789");
222        when(tm.getSimState()).thenReturn(TelephonyManager.SIM_STATE_UNKNOWN);
223        when(tm.getSimOperator()).thenReturn(null);
224        when(tm.getCarrierInfoForImsiEncryption(anyInt())).thenReturn(null);
225
226        assertEquals(expectedIdentity, TelephonyUtil.getSimIdentity(tm, telephonyUtil,
227                WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.SIM,
228                        WifiEnterpriseConfig.Phase2.NONE)));
229    }
230
231    @Test
232    public void getSimIdentityWithNoTelephonyManager() {
233        assertEquals(null, TelephonyUtil.getSimIdentity(null, null,
234                WifiConfigurationTestUtil.createEapNetwork(
235                        WifiEnterpriseConfig.Eap.SIM, WifiEnterpriseConfig.Phase2.NONE)));
236    }
237
238    @Test
239    public void getSimIdentityNonTelephonyConfig() {
240        TelephonyManager tm = mock(TelephonyManager.class);
241        TelephonyUtil telephonyUtil = mock(TelephonyUtil.class);
242        when(tm.getSubscriberId()).thenReturn("321560123456789");
243        when(tm.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY);
244        when(tm.getSimOperator()).thenReturn("32156");
245        assertEquals(null, TelephonyUtil.getSimIdentity(tm, telephonyUtil,
246                WifiConfigurationTestUtil.createEapNetwork(
247                        WifiEnterpriseConfig.Eap.TTLS, WifiEnterpriseConfig.Phase2.SIM)));
248        assertEquals(null, TelephonyUtil.getSimIdentity(tm, telephonyUtil,
249                WifiConfigurationTestUtil.createEapNetwork(
250                        WifiEnterpriseConfig.Eap.PEAP, WifiEnterpriseConfig.Phase2.MSCHAPV2)));
251        assertEquals(null, TelephonyUtil.getSimIdentity(tm, telephonyUtil,
252                WifiConfigurationTestUtil.createEapNetwork(
253                        WifiEnterpriseConfig.Eap.TLS, WifiEnterpriseConfig.Phase2.NONE)));
254        assertEquals(null, TelephonyUtil.getSimIdentity(
255                tm, telephonyUtil, new WifiConfiguration()));
256    }
257
258    @Test
259    public void isSimConfig() {
260        assertFalse(TelephonyUtil.isSimConfig(null));
261        assertFalse(TelephonyUtil.isSimConfig(new WifiConfiguration()));
262        assertFalse(TelephonyUtil.isSimConfig(WifiConfigurationTestUtil.createOpenNetwork()));
263        assertFalse(TelephonyUtil.isSimConfig(WifiConfigurationTestUtil.createWepNetwork()));
264        assertFalse(TelephonyUtil.isSimConfig(WifiConfigurationTestUtil.createPskNetwork()));
265        assertFalse(TelephonyUtil.isSimConfig(WifiConfigurationTestUtil.createEapNetwork(
266                WifiEnterpriseConfig.Eap.TTLS, WifiEnterpriseConfig.Phase2.SIM)));
267        assertFalse(TelephonyUtil.isSimConfig(WifiConfigurationTestUtil.createEapNetwork(
268                WifiEnterpriseConfig.Eap.TLS, WifiEnterpriseConfig.Phase2.NONE)));
269        assertFalse(TelephonyUtil.isSimConfig(WifiConfigurationTestUtil.createEapNetwork(
270                WifiEnterpriseConfig.Eap.PEAP, WifiEnterpriseConfig.Phase2.MSCHAPV2)));
271        assertTrue(TelephonyUtil.isSimConfig(WifiConfigurationTestUtil.createEapNetwork(
272                WifiEnterpriseConfig.Eap.SIM, WifiEnterpriseConfig.Phase2.NONE)));
273        assertTrue(TelephonyUtil.isSimConfig(WifiConfigurationTestUtil.createEapNetwork(
274                WifiEnterpriseConfig.Eap.AKA, WifiEnterpriseConfig.Phase2.NONE)));
275        assertTrue(TelephonyUtil.isSimConfig(WifiConfigurationTestUtil.createEapNetwork(
276                WifiEnterpriseConfig.Eap.AKA_PRIME, WifiEnterpriseConfig.Phase2.NONE)));
277        assertTrue(TelephonyUtil.isSimConfig(WifiConfigurationTestUtil.createEapNetwork(
278                WifiEnterpriseConfig.Eap.PEAP, WifiEnterpriseConfig.Phase2.SIM)));
279        assertTrue(TelephonyUtil.isSimConfig(WifiConfigurationTestUtil.createEapNetwork(
280                WifiEnterpriseConfig.Eap.PEAP, WifiEnterpriseConfig.Phase2.AKA)));
281        assertTrue(TelephonyUtil.isSimConfig(WifiConfigurationTestUtil.createEapNetwork(
282                WifiEnterpriseConfig.Eap.PEAP, WifiEnterpriseConfig.Phase2.AKA_PRIME)));
283    }
284
285    /**
286     * Produce a base64 encoded length byte + data.
287     */
288    private static String createSimChallengeRequest(byte[] challengeValue) {
289        byte[] challengeLengthAndValue = new byte[challengeValue.length + 1];
290        challengeLengthAndValue[0] = (byte) challengeValue.length;
291        for (int i = 0; i < challengeValue.length; ++i) {
292            challengeLengthAndValue[i + 1] = challengeValue[i];
293        }
294        return Base64.encodeToString(challengeLengthAndValue, android.util.Base64.NO_WRAP);
295    }
296
297    /**
298     * Produce a base64 encoded sres length byte + sres + kc length byte + kc.
299     */
300    private static String createGsmSimAuthResponse(byte[] sresValue, byte[] kcValue) {
301        int overallLength = sresValue.length + kcValue.length + 2;
302        byte[] result = new byte[sresValue.length + kcValue.length + 2];
303        int idx = 0;
304        result[idx++] = (byte) sresValue.length;
305        for (int i = 0; i < sresValue.length; ++i) {
306            result[idx++] = sresValue[i];
307        }
308        result[idx++] = (byte) kcValue.length;
309        for (int i = 0; i < kcValue.length; ++i) {
310            result[idx++] = kcValue[i];
311        }
312        return Base64.encodeToString(result, Base64.NO_WRAP);
313    }
314
315    @Test
316    public void getGsmSimAuthResponseInvalidRequest() {
317        TelephonyManager tm = mock(TelephonyManager.class);
318        final String[] invalidRequests = { null, "", "XXXX" };
319        assertEquals("", TelephonyUtil.getGsmSimAuthResponse(invalidRequests, tm));
320    }
321
322    @Test
323    public void getGsmSimAuthResponseFailedSimResponse() {
324        TelephonyManager tm = mock(TelephonyManager.class);
325        final String[] failedRequests = { "5E5F" };
326        when(tm.getIccAuthentication(anyInt(), anyInt(),
327                eq(createSimChallengeRequest(new byte[] { 0x5e, 0x5f })))).thenReturn(null);
328
329        assertEquals(null, TelephonyUtil.getGsmSimAuthResponse(failedRequests, tm));
330    }
331
332    @Test
333    public void getGsmSimAuthResponseUsim() {
334        TelephonyManager tm = mock(TelephonyManager.class);
335        when(tm.getIccAuthentication(TelephonyManager.APPTYPE_USIM,
336                        TelephonyManager.AUTHTYPE_EAP_SIM,
337                        createSimChallengeRequest(new byte[] { 0x1b, 0x2b })))
338                .thenReturn(createGsmSimAuthResponse(new byte[] { 0x1D, 0x2C },
339                                new byte[] { 0x3B, 0x4A }));
340        when(tm.getIccAuthentication(TelephonyManager.APPTYPE_USIM,
341                        TelephonyManager.AUTHTYPE_EAP_SIM,
342                        createSimChallengeRequest(new byte[] { 0x01, 0x22 })))
343                .thenReturn(createGsmSimAuthResponse(new byte[] { 0x11, 0x11 },
344                                new byte[] { 0x12, 0x34 }));
345
346        assertEquals(":3b4a:1d2c:1234:1111", TelephonyUtil.getGsmSimAuthResponse(
347                        new String[] { "1B2B", "0122" }, tm));
348    }
349
350    @Test
351    public void getGsmSimAuthResponseSimpleSim() {
352        TelephonyManager tm = mock(TelephonyManager.class);
353        when(tm.getIccAuthentication(TelephonyManager.APPTYPE_USIM,
354                        TelephonyManager.AUTHTYPE_EAP_SIM,
355                        createSimChallengeRequest(new byte[] { 0x1a, 0x2b })))
356                .thenReturn(null);
357        when(tm.getIccAuthentication(TelephonyManager.APPTYPE_SIM,
358                        TelephonyManager.AUTHTYPE_EAP_SIM,
359                        createSimChallengeRequest(new byte[] { 0x1a, 0x2b })))
360                .thenReturn(createGsmSimAuthResponse(new byte[] { 0x1D, 0x2C },
361                                new byte[] { 0x3B, 0x4A }));
362        when(tm.getIccAuthentication(TelephonyManager.APPTYPE_USIM,
363                        TelephonyManager.AUTHTYPE_EAP_SIM,
364                        createSimChallengeRequest(new byte[] { 0x01, 0x23 })))
365                .thenReturn(null);
366        when(tm.getIccAuthentication(TelephonyManager.APPTYPE_SIM,
367                        TelephonyManager.AUTHTYPE_EAP_SIM,
368                        createSimChallengeRequest(new byte[] { 0x01, 0x23 })))
369                .thenReturn(createGsmSimAuthResponse(new byte[] { 0x33, 0x22 },
370                                new byte[] { 0x11, 0x00 }));
371
372        assertEquals(":3b4a:1d2c:1100:3322", TelephonyUtil.getGsmSimAuthResponse(
373                        new String[] { "1A2B", "0123" }, tm));
374    }
375
376    /**
377     * Produce a base64 encoded tag + res length byte + res + ck length byte + ck + ik length byte +
378     * ik.
379     */
380    private static String create3GSimAuthUmtsAuthResponse(byte[] res, byte[] ck, byte[] ik) {
381        byte[] result = new byte[res.length + ck.length + ik.length + 4];
382        int idx = 0;
383        result[idx++] = (byte) 0xdb;
384        result[idx++] = (byte) res.length;
385        for (int i = 0; i < res.length; ++i) {
386            result[idx++] = res[i];
387        }
388        result[idx++] = (byte) ck.length;
389        for (int i = 0; i < ck.length; ++i) {
390            result[idx++] = ck[i];
391        }
392        result[idx++] = (byte) ik.length;
393        for (int i = 0; i < ik.length; ++i) {
394            result[idx++] = ik[i];
395        }
396        return Base64.encodeToString(result, Base64.NO_WRAP);
397    }
398
399    private static String create3GSimAuthUmtsAutsResponse(byte[] auts) {
400        byte[] result = new byte[auts.length + 2];
401        int idx = 0;
402        result[idx++] = (byte) 0xdc;
403        result[idx++] = (byte) auts.length;
404        for (int i = 0; i < auts.length; ++i) {
405            result[idx++] = auts[i];
406        }
407        return Base64.encodeToString(result, Base64.NO_WRAP);
408    }
409
410    @Test
411    public void get3GAuthResponseInvalidRequest() {
412        TelephonyManager tm = mock(TelephonyManager.class);
413        assertEquals(null, TelephonyUtil.get3GAuthResponse(
414                        new SimAuthRequestData(0, 0, "SSID", new String[] {"0123"}), tm));
415        assertEquals(null, TelephonyUtil.get3GAuthResponse(
416                        new SimAuthRequestData(0, 0, "SSID", new String[] {"xyz2", "1234"}), tm));
417        verifyNoMoreInteractions(tm);
418    }
419
420    @Test
421    public void get3GAuthResponseNullIccAuthentication() {
422        TelephonyManager tm = mock(TelephonyManager.class);
423
424        when(tm.getIccAuthentication(TelephonyManager.APPTYPE_USIM,
425                        TelephonyManager.AUTHTYPE_EAP_AKA, "AgEjAkVn")).thenReturn(null);
426
427        SimAuthResponseData response = TelephonyUtil.get3GAuthResponse(
428                new SimAuthRequestData(0, 0, "SSID", new String[] {"0123", "4567"}), tm);
429        assertNull(response);
430    }
431
432    @Test
433    public void get3GAuthResponseIccAuthenticationTooShort() {
434        TelephonyManager tm = mock(TelephonyManager.class);
435
436        when(tm.getIccAuthentication(TelephonyManager.APPTYPE_USIM,
437                        TelephonyManager.AUTHTYPE_EAP_AKA, "AgEjAkVn"))
438                .thenReturn(Base64.encodeToString(new byte[] {(byte) 0xdc}, Base64.NO_WRAP));
439
440        SimAuthResponseData response = TelephonyUtil.get3GAuthResponse(
441                new SimAuthRequestData(0, 0, "SSID", new String[] {"0123", "4567"}), tm);
442        assertNull(response);
443    }
444
445    @Test
446    public void get3GAuthResponseBadTag() {
447        TelephonyManager tm = mock(TelephonyManager.class);
448
449        when(tm.getIccAuthentication(TelephonyManager.APPTYPE_USIM,
450                        TelephonyManager.AUTHTYPE_EAP_AKA, "AgEjAkVn"))
451                .thenReturn(Base64.encodeToString(new byte[] {0x31, 0x1, 0x2, 0x3, 0x4},
452                                Base64.NO_WRAP));
453
454        SimAuthResponseData response = TelephonyUtil.get3GAuthResponse(
455                new SimAuthRequestData(0, 0, "SSID", new String[] {"0123", "4567"}), tm);
456        assertNull(response);
457    }
458
459    @Test
460    public void get3GAuthResponseUmtsAuth() {
461        TelephonyManager tm = mock(TelephonyManager.class);
462
463        when(tm.getIccAuthentication(TelephonyManager.APPTYPE_USIM,
464                        TelephonyManager.AUTHTYPE_EAP_AKA, "AgEjAkVn"))
465                .thenReturn(create3GSimAuthUmtsAuthResponse(new byte[] {0x11, 0x12},
466                                new byte[] {0x21, 0x22, 0x23}, new byte[] {0x31}));
467
468        SimAuthResponseData response = TelephonyUtil.get3GAuthResponse(
469                new SimAuthRequestData(0, 0, "SSID", new String[] {"0123", "4567"}), tm);
470        assertNotNull(response);
471        assertEquals("UMTS-AUTH", response.type);
472        assertEquals(":31:212223:1112", response.response);
473    }
474
475    @Test
476    public void get3GAuthResponseUmtsAuts() {
477        TelephonyManager tm = mock(TelephonyManager.class);
478
479        when(tm.getIccAuthentication(TelephonyManager.APPTYPE_USIM,
480                        TelephonyManager.AUTHTYPE_EAP_AKA, "AgEjAkVn"))
481                .thenReturn(create3GSimAuthUmtsAutsResponse(new byte[] {0x22, 0x33}));
482
483        SimAuthResponseData response = TelephonyUtil.get3GAuthResponse(
484                new SimAuthRequestData(0, 0, "SSID", new String[] {"0123", "4567"}), tm);
485        assertNotNull(response);
486        assertEquals("UMTS-AUTS", response.type);
487        assertEquals(":2233", response.response);
488    }
489}
490