1 /*
2  * Copyright (C) 2019 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 android.net.eap;
18 
19 import static android.net.eap.EapSessionConfig.DEFAULT_IDENTITY;
20 import static android.telephony.TelephonyManager.APPTYPE_USIM;
21 
22 import static com.android.internal.net.eap.message.EapData.EAP_TYPE_AKA;
23 import static com.android.internal.net.eap.message.EapData.EAP_TYPE_AKA_PRIME;
24 import static com.android.internal.net.eap.message.EapData.EAP_TYPE_MSCHAP_V2;
25 import static com.android.internal.net.eap.message.EapData.EAP_TYPE_SIM;
26 
27 import static org.junit.Assert.assertArrayEquals;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertTrue;
30 import static org.junit.Assert.fail;
31 
32 import android.net.eap.EapSessionConfig.EapAkaConfig;
33 import android.net.eap.EapSessionConfig.EapAkaPrimeConfig;
34 import android.net.eap.EapSessionConfig.EapMethodConfig;
35 import android.net.eap.EapSessionConfig.EapMsChapV2Config;
36 import android.net.eap.EapSessionConfig.EapSimConfig;
37 
38 import org.junit.Test;
39 
40 import java.nio.charset.StandardCharsets;
41 
42 public class EapSessionConfigTest {
43     private static final byte[] EAP_IDENTITY =
44             "test@android.net".getBytes(StandardCharsets.US_ASCII);
45     private static final int SUB_ID = 1;
46     private static final String NETWORK_NAME = "android.net";
47     private static final boolean ALLOW_MISMATCHED_NETWORK_NAMES = true;
48     private static final String USERNAME = "username";
49     private static final String PASSWORD = "password";
50 
51     @Test
testBuildEapSim()52     public void testBuildEapSim() {
53         EapSessionConfig result = new EapSessionConfig.Builder()
54                 .setEapIdentity(EAP_IDENTITY)
55                 .setEapSimConfig(SUB_ID, APPTYPE_USIM)
56                 .build();
57 
58         assertArrayEquals(EAP_IDENTITY, result.eapIdentity);
59 
60         EapMethodConfig eapMethodConfig = result.eapConfigs.get(EAP_TYPE_SIM);
61         assertEquals(EAP_TYPE_SIM, eapMethodConfig.methodType);
62         EapSimConfig eapSimConfig = (EapSimConfig) eapMethodConfig;
63         assertEquals(SUB_ID, eapSimConfig.subId);
64         assertEquals(APPTYPE_USIM, eapSimConfig.apptype);
65     }
66 
67     @Test
testBuildEapAka()68     public void testBuildEapAka() {
69         EapSessionConfig result = new EapSessionConfig.Builder()
70                 .setEapAkaConfig(SUB_ID, APPTYPE_USIM)
71                 .build();
72 
73         assertArrayEquals(DEFAULT_IDENTITY, result.eapIdentity);
74         EapMethodConfig eapMethodConfig = result.eapConfigs.get(EAP_TYPE_AKA);
75         assertEquals(EAP_TYPE_AKA, eapMethodConfig.methodType);
76         EapAkaConfig eapAkaConfig = (EapAkaConfig) eapMethodConfig;
77         assertEquals(SUB_ID, eapAkaConfig.subId);
78         assertEquals(APPTYPE_USIM, eapAkaConfig.apptype);
79     }
80 
81     @Test
testBuildEapAkaPrime()82     public void testBuildEapAkaPrime() {
83         EapSessionConfig result =
84                 new EapSessionConfig.Builder()
85                         .setEapAkaPrimeConfig(
86                                 SUB_ID, APPTYPE_USIM, NETWORK_NAME, ALLOW_MISMATCHED_NETWORK_NAMES)
87                         .build();
88 
89         assertEquals(DEFAULT_IDENTITY, result.eapIdentity);
90         EapMethodConfig eapMethodConfig = result.eapConfigs.get(EAP_TYPE_AKA_PRIME);
91         assertEquals(EAP_TYPE_AKA_PRIME, eapMethodConfig.methodType);
92         EapAkaPrimeConfig eapAkaPrimeConfig = (EapAkaPrimeConfig) eapMethodConfig;
93         assertEquals(SUB_ID, eapAkaPrimeConfig.subId);
94         assertEquals(APPTYPE_USIM, eapAkaPrimeConfig.apptype);
95         assertEquals(NETWORK_NAME, eapAkaPrimeConfig.networkName);
96         assertTrue(eapAkaPrimeConfig.allowMismatchedNetworkNames);
97     }
98 
99     @Test
testBuildEapMsChapV2()100     public void testBuildEapMsChapV2() {
101         EapSessionConfig result =
102                 new EapSessionConfig.Builder().setEapMsChapV2Config(USERNAME, PASSWORD).build();
103 
104         EapMsChapV2Config config = (EapMsChapV2Config) result.eapConfigs.get(EAP_TYPE_MSCHAP_V2);
105         assertEquals(EAP_TYPE_MSCHAP_V2, config.methodType);
106         assertEquals(USERNAME, config.username);
107         assertEquals(PASSWORD, config.password);
108     }
109 
110     @Test
testBuildWithoutConfigs()111     public void testBuildWithoutConfigs() {
112         try {
113             new EapSessionConfig.Builder().build();
114             fail("build() should throw an IllegalStateException if no EAP methods are configured");
115         } catch (IllegalStateException expected) {
116         }
117     }
118 }
119