1 /*
2  * Copyright 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 
17 package com.android.managedprovisioning.task.wifi;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotEquals;
21 import static org.junit.Assert.assertTrue;
22 
23 import android.net.IpConfiguration;
24 import android.net.wifi.WifiConfiguration;
25 import android.net.wifi.WifiEnterpriseConfig;
26 
27 import androidx.test.filters.SmallTest;
28 
29 import com.android.managedprovisioning.model.WifiInfo;
30 
31 import org.junit.Test;
32 
33 import java.security.cert.CertificateEncodingException;
34 import java.security.cert.X509Certificate;
35 import java.util.Base64;
36 
37 /**
38  * Unit test for {@link WifiConfigurationProvider}.
39  */
40 @SmallTest
41 public class WifiConfigurationProviderTest {
42     private static final String TEST_SSID = "\"test_ssid\"";
43     private static final boolean TEST_HIDDEN = true;
44     private static final String TEST_PAC_URL = "test.pac.url";
45     private static final String TEST_PROXY_BYPASS_HOST = "testProxyBypassHost";
46     private static final String TEST_PROXY_HOST = "TestProxyHost";
47     private static final int TEST_PROXY_PORT = 1234;
48     private static final String TEST_PASSWORD = "testPassword";
49     private static final String TEST_PASSWORD_WEP = "0123456789"; // length needs to be 10
50 
51     /*
52      * Taken from:
53      * https://g3doc.corp.google.com/company/teams/clouddpc/documents/policy/sample_policy_jsons.md?cl=head#with-eap
54      */
55     private static final String TEST_CA_CERT = "MIIDKDCCAhCgAwIBAgIJAOM5SzKO2pzCMA0GCSqGSIb3DQEBCwUAMBIxEDAOBgNVBAMTB0VBUCBDQTAwHhcNMTYwMTEyMDAxMDQ3WhcNMjYwMTA5M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MBAAGjgZMwgZAwCQYDVR0TBAIwADAdBgNVHQ4EFgQUq+JLbJFNWLo/CTOjSdzzj8NfeIcwQgYDVR0jBDswOYAUBncDZLiollnC41jCQnqYB1zSBV2hFqQUMBIxEDAOBgNVBAMTB0VBUCBDQTCCCQDjOUsyjtqcwjATBgNVHSUEDDAKBggrBgEFBQcDAjALBgNVHQ8EBAMCB4AwDQYJKoZIhvcNAQELBQADggEBAEjDSRnJptUoNZooQJfjaxEH5uv16SChU/0sgk4HfM0YAR/LTTO0XhHGvueMk72gVc3KARIv6m2cLYDhEJ7xvyb/b6PkMchHFzXqk2cxJw01OpwYmouR58MZqRAJm5xicriBtLy11Mx7YEeSSZGKLlDEAT7B1UXw7dodHS0AFZbLT2N8w31PpYMopAPkdc/4DGwDuDvK/QGQ9Xjebn+m0muOPRTgKEyS0/jfkoVEML1K+a2wQA4udk+Si1DEvZETXkEEYxX7l9auUUXmRec7WWO9BY2VDvHyproMDbQndXxy3EC/RH/noS39IItdBeame9CQ5yvlaDBaoqN6tScetUc=";
59 
60     private static final String TEST_USER_CERT_NON_X509 = "/u3+7QAAAAIAAAABAAAAAQADYWJjAAABZjmySqMAAAK8MIICuDAOBgorBgEEASoCEQEBBQAEggKk9B93ViQJqk1DOwD/AFiljxR//AoKOEl0XPC6SFadj7x8j3M6FwbALo2lGWHMYnxHU7uVPJ8iSynGWiD+5WcaXA9+xlUmvpI7WgrQ7Dek+r8mLddkvGorqzI1CjJcDIHEmio3WhnM1X8MnBlrKggFP/FKsdE27oirq2YbcH7SEtvsAJNaqHZp7ZkG38AYcZH+V+4hhxDk/VJo3EZeJPcnLVASUyM69t6cTOo0wqOTZjeOytSAwVgKjM2FG/ZkqGgfmP2/BzPbUk0e7amTxCQDYRLsn/KkVdKOq8AoIESGGeaS7bt9p6/wRayp1t3ykAyQipIGlY7aiaMAsXxbdc//MTO4yrt45VEsLGzVaayRRzMD3pITvkz5ZWvuJGQLneKqCFdIzGJPC5tkWmiTWa9WA7+RKVmGgV1loqS86Uzur19N/wrLH56nVWlAVjKknBW8ly5xJF7AAgK3/u3bKSVA50XkO6i1lEsz9FCU4KU3Mx2MVv10NIysXcpecx8LpkZ2gLLm+2Y7exaIqUn239oLJEHhVGgmLAL2avQ3Vd+glKBfgyoScpbWqpFkWD1Qm4UeseZi+XG/77g0+dvniX5SXDbTdZH+uH/AcZnOVMxLGSY9+53jWsMXYvPsnlxEvMVRhsLgwsEdzrqI7uI2XFRj0wSDFthRO9zq0EU2K1BZtwvcILdmmwP2jCBoXDd8vm37K99oPSXjMwJC+XhkxY3K3BGyQp1vUNdtixma1m+ONApFwpgFkSr3vwJOfDlmN2heP4z9OeJWNp0KIBg/SbXeemiIYbzlWmVTmNIQBhzbWL7GQSVjQLmkcZ6oOL4eeBZIPvhfNqEHwEJYYzMf1GVMecJ8aQI5JQx2/ObZ7pUiJukW3PCBVfOzQ7CrpD6WXaLdwBlLmQAAAAEABHRlc3QAAAACAQILu3qeGs7aEjgBWSGbNW1kiGBc3g==";
61 
62     private static final String TEST_IDENTITY = "TestUser";
63     private static final String TEST_ANONYMOUS_IDENTITY = "TestAUser";
64     private static final String TEST_DOMAIN = "google.com";
65     private static final String TEST_SECURITY_TYPE = "EAP";
66 
67     private static final WifiInfo.Builder BASE_BUILDER = new WifiInfo.Builder()
68             .setSsid(TEST_SSID)
69             .setHidden(TEST_HIDDEN);
70 
71     private static final WifiInfo WIFI_INFO_WPA = BASE_BUILDER
72             .setSecurityType(WifiConfigurationProvider.WPA)
73             .setPassword(TEST_PASSWORD)
74             .build();
75 
76     private static final WifiInfo WIFI_INFO_WEP = BASE_BUILDER
77             .setSecurityType(WifiConfigurationProvider.WEP)
78             .setPassword(TEST_PASSWORD)
79             .build();
80 
81     private static final WifiInfo WIFI_INFO_WEP_2 = BASE_BUILDER
82             .setSecurityType(WifiConfigurationProvider.WEP)
83             .setPassword(TEST_PASSWORD_WEP)
84             .build();
85 
86     private static final WifiInfo WIFI_INFO_NONE = BASE_BUILDER
87             .setSecurityType(WifiConfigurationProvider.NONE)
88             .build();
89 
90     private static final WifiInfo WIFI_INFO_NULL = BASE_BUILDER
91             .build();
92 
93     private static final WifiInfo WIFI_INFO_PAC = BASE_BUILDER
94             .setPacUrl(TEST_PAC_URL)
95             .build();
96 
97     private static final WifiInfo WIFI_INFO_PROXY = BASE_BUILDER
98             .setProxyBypassHosts(TEST_PROXY_BYPASS_HOST)
99             .setProxyHost(TEST_PROXY_HOST)
100             .setProxyPort(TEST_PROXY_PORT)
101             .build();
102 
103     private static final WifiInfo.Builder BASE_EAP_BUILDER = new WifiInfo.Builder()
104             .setSsid(TEST_SSID)
105             .setHidden(TEST_HIDDEN)
106             .setSecurityType(TEST_SECURITY_TYPE);
107 
108     private final WifiConfigurationProvider mProvider = new WifiConfigurationProvider();
109 
110     @Test
testWpa()111     public void testWpa() {
112         WifiConfiguration wifiConf = mProvider.generateWifiConfiguration(WIFI_INFO_WPA);
113 
114         assertBase(wifiConf);
115         assertTrue(wifiConf.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK));
116         assertTrue(wifiConf.allowedProtocols.get(WifiConfiguration.Protocol.WPA));
117         assertEquals("\"" + TEST_PASSWORD + "\"", wifiConf.preSharedKey);
118         assertEquals(IpConfiguration.ProxySettings.UNASSIGNED,
119                 wifiConf.getIpConfiguration().getProxySettings());
120     }
121 
122     @Test
testWep()123     public void testWep() {
124         WifiConfiguration wifiConf = mProvider.generateWifiConfiguration(WIFI_INFO_WEP);
125 
126         assertBase(wifiConf);
127         assertTrue(wifiConf.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.NONE));
128         assertEquals("\"" + TEST_PASSWORD + "\"", wifiConf.wepKeys[0]);
129         assertEquals(IpConfiguration.ProxySettings.UNASSIGNED,
130                 wifiConf.getIpConfiguration().getProxySettings());
131     }
132 
133     @Test
testWep2()134     public void testWep2() {
135         WifiConfiguration wifiConf = mProvider.generateWifiConfiguration(WIFI_INFO_WEP_2);
136 
137         assertBase(wifiConf);
138         assertTrue(wifiConf.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.NONE));
139         assertEquals(TEST_PASSWORD_WEP, wifiConf.wepKeys[0]);
140         assertEquals(IpConfiguration.ProxySettings.UNASSIGNED,
141                 wifiConf.getIpConfiguration().getProxySettings());
142     }
143 
144     @Test
testNone()145     public void testNone() {
146         WifiConfiguration wifiConf = mProvider.generateWifiConfiguration(WIFI_INFO_NONE);
147 
148         assertBase(wifiConf);
149         assertTrue(wifiConf.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.NONE));
150         assertTrue(wifiConf.allowedAuthAlgorithms.get(WifiConfiguration.AuthAlgorithm.OPEN));
151         assertEquals(IpConfiguration.ProxySettings.UNASSIGNED,
152                 wifiConf.getIpConfiguration().getProxySettings());
153     }
154 
155     @Test
testNull()156     public void testNull() {
157         WifiConfiguration wifiConf = mProvider.generateWifiConfiguration(WIFI_INFO_NULL);
158 
159         assertBase(wifiConf);
160         assertTrue(wifiConf.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.NONE));
161         assertTrue(wifiConf.allowedAuthAlgorithms.get(WifiConfiguration.AuthAlgorithm.OPEN));
162         assertEquals(IpConfiguration.ProxySettings.UNASSIGNED,
163                 wifiConf.getIpConfiguration().getProxySettings());
164     }
165 
166     @Test
testPac()167     public void testPac() {
168         WifiConfiguration wifiConf = mProvider.generateWifiConfiguration(WIFI_INFO_PAC);
169 
170         assertBase(wifiConf);
171         assertEquals(IpConfiguration.ProxySettings.PAC,
172                 wifiConf.getIpConfiguration().getProxySettings());
173     }
174 
175     @Test
testStaticProxy()176     public void testStaticProxy() {
177         WifiConfiguration wifiConf = mProvider.generateWifiConfiguration(WIFI_INFO_PROXY);
178 
179         assertBase(wifiConf);
180         assertEquals(IpConfiguration.ProxySettings.STATIC,
181                 wifiConf.getIpConfiguration().getProxySettings());
182     }
183 
184     @Test
testEAP_returnsCorrectKeyManagement()185     public void testEAP_returnsCorrectKeyManagement() {
186         WifiConfiguration wifiConf = mProvider.generateWifiConfiguration(
187                 buildBaseTestWifiInfoForEAP());
188 
189         assertEAPAllowedKeyManagement(wifiConf);
190     }
191 
192     @Test
testEAP_returnsCorrectEnterpriseConfig()193     public void testEAP_returnsCorrectEnterpriseConfig() {
194         WifiConfiguration wifiConf = mProvider.generateWifiConfiguration(
195                 buildTestWifiInfoForEAPWithCertificates(
196                         TEST_PASSWORD, TEST_IDENTITY, TEST_ANONYMOUS_IDENTITY, TEST_DOMAIN,
197                         TEST_CA_CERT, TEST_USER_CERT_STRING_INPUT));
198 
199         assertEnterpriseConfig(
200                 wifiConf, TEST_PASSWORD, TEST_IDENTITY, TEST_ANONYMOUS_IDENTITY, TEST_DOMAIN,
201                 TEST_CA_CERT, TEST_USER_CERT_CHAIN_OUTPUT, TEST_USER_CERT_PRIVATE_KEY_OUTPUT);
202     }
203 
204     @Test
testEAP_noCertificates_returnsCorrectEnterpriseConfig()205     public void testEAP_noCertificates_returnsCorrectEnterpriseConfig() {
206         WifiConfiguration wifiConf = mProvider.generateWifiConfiguration(
207                 buildTestWifiInfoForEAPWithCertificates(
208                         TEST_PASSWORD, TEST_IDENTITY, TEST_ANONYMOUS_IDENTITY, TEST_DOMAIN,
209                         /* caCertificate= */null, /* userCertificate= */null));
210 
211         assertEnterpriseConfigWithoutCertificates(
212                 wifiConf, TEST_PASSWORD, TEST_IDENTITY, TEST_ANONYMOUS_IDENTITY, TEST_DOMAIN);
213     }
214 
215     @Test
testEAP_invalidCACertificate()216     public void testEAP_invalidCACertificate() {
217         WifiConfiguration wifiConf = mProvider.generateWifiConfiguration(
218                 buildTestWifiInfoForEAPWithCertificates(
219                         TEST_PASSWORD, TEST_IDENTITY, TEST_ANONYMOUS_IDENTITY, TEST_DOMAIN,
220                         /* caCertificate= */"random", /* userCertificate= */null));
221 
222         assertEmptyEnterpriseConfig(wifiConf);
223     }
224 
225     @Test
testEAP_invalidUserCertificate()226     public void testEAP_invalidUserCertificate() {
227         WifiConfiguration wifiConf = mProvider.generateWifiConfiguration(
228                 buildTestWifiInfoForEAPWithCertificates(
229                         TEST_PASSWORD, TEST_IDENTITY, TEST_ANONYMOUS_IDENTITY, TEST_DOMAIN,
230                         /* caCertificate= */null, /* userCertificate= */"random"));
231 
232         assertEmptyEnterpriseConfig(wifiConf);
233     }
234 
235     @Test
testEAP_nonX509Cert()236     public void testEAP_nonX509Cert() {
237         WifiConfiguration wifiConf = mProvider.generateWifiConfiguration(
238                 buildTestWifiInfoForEAPWithCertificates(
239                         TEST_PASSWORD, TEST_IDENTITY, TEST_ANONYMOUS_IDENTITY, TEST_DOMAIN,
240                         /* caCertificate= */null, TEST_USER_CERT_NON_X509));
241 
242         assertEmptyEnterpriseConfig(wifiConf);
243     }
244 
245     @Test
testEAP_PEAP_PAP()246     public void testEAP_PEAP_PAP() {
247         WifiConfiguration wifiConf = mProvider.generateWifiConfiguration(
248                 buildTestWifiInfoForEAPJustAuthMethods("PEAP", "PAP"));
249 
250         assertEAP_MethodAndPhase2Auth(
251                 wifiConf, WifiEnterpriseConfig.Eap.PEAP, WifiEnterpriseConfig.Phase2.PAP);
252     }
253 
254     @Test
testEAP_PEAP_NONE()255     public void testEAP_PEAP_NONE() {
256         WifiConfiguration wifiConf = mProvider.generateWifiConfiguration(
257                 buildTestWifiInfoForEAPJustAuthMethods("PEAP", "NONE"));
258 
259         assertEAP_MethodAndPhase2Auth(
260                 wifiConf, WifiEnterpriseConfig.Eap.PEAP, WifiEnterpriseConfig.Phase2.NONE);
261     }
262 
263     @Test
testEAP_Phase2_Empty()264     public void testEAP_Phase2_Empty() {
265         WifiConfiguration wifiConf = mProvider.generateWifiConfiguration(
266                 buildTestWifiInfoForEAPJustAuthMethods("PEAP", ""));
267 
268         assertEAP_MethodAndPhase2Auth(
269                 wifiConf, WifiEnterpriseConfig.Eap.PEAP, WifiEnterpriseConfig.Phase2.NONE);
270     }
271 
272     @Test
testEAP_Phase2_Null()273     public void testEAP_Phase2_Null() {
274         WifiConfiguration wifiConf = mProvider.generateWifiConfiguration(
275                 buildTestWifiInfoForEAPJustAuthMethods("PEAP", null));
276 
277         assertEAP_MethodAndPhase2Auth(
278                 wifiConf, WifiEnterpriseConfig.Eap.PEAP, WifiEnterpriseConfig.Phase2.NONE);
279     }
280 
281     @Test
testEAP_TLS_MSCHAP()282     public void testEAP_TLS_MSCHAP() {
283         WifiConfiguration wifiConf = mProvider.generateWifiConfiguration(
284                 buildTestWifiInfoForEAPJustAuthMethods("TLS", "MSCHAP"));
285 
286         assertEAP_MethodAndPhase2Auth(
287                 wifiConf, WifiEnterpriseConfig.Eap.TLS, WifiEnterpriseConfig.Phase2.MSCHAP);
288     }
289 
290     @Test
testEAP_TTLS_MSCHAPV2()291     public void testEAP_TTLS_MSCHAPV2() {
292         WifiConfiguration wifiConf = mProvider.generateWifiConfiguration(
293                 buildTestWifiInfoForEAPJustAuthMethods("TTLS", "MSCHAPV2"));
294 
295         assertEAP_MethodAndPhase2Auth(
296                 wifiConf, WifiEnterpriseConfig.Eap.TTLS, WifiEnterpriseConfig.Phase2.MSCHAPV2);
297     }
298 
299     @Test
testEAP_PWD_GTC()300     public void testEAP_PWD_GTC() {
301         WifiConfiguration wifiConf = mProvider.generateWifiConfiguration(
302                 buildTestWifiInfoForEAPJustAuthMethods("PWD", "GTC"));
303 
304         assertEAP_MethodAndPhase2Auth(
305                 wifiConf, WifiEnterpriseConfig.Eap.PWD, WifiEnterpriseConfig.Phase2.GTC);
306     }
307 
308     @Test
testEAP_SIM_SIM()309     public void testEAP_SIM_SIM() {
310         WifiConfiguration wifiConf = mProvider.generateWifiConfiguration(
311                 buildTestWifiInfoForEAPJustAuthMethods("SIM", "SIM"));
312 
313         assertEAP_MethodAndPhase2Auth(
314                 wifiConf, WifiEnterpriseConfig.Eap.SIM, WifiEnterpriseConfig.Phase2.SIM);
315     }
316 
317     @Test
testEAP_AKA_AKA()318     public void testEAP_AKA_AKA() {
319         WifiConfiguration wifiConf = mProvider.generateWifiConfiguration(
320                 buildTestWifiInfoForEAPJustAuthMethods("AKA", "AKA"));
321 
322         assertEAP_MethodAndPhase2Auth(
323                 wifiConf, WifiEnterpriseConfig.Eap.AKA, WifiEnterpriseConfig.Phase2.AKA);
324     }
325 
326     @Test
testEAP_AKA_PRIME_AKA_PRIME()327     public void testEAP_AKA_PRIME_AKA_PRIME() {
328         WifiConfiguration wifiConf = mProvider.generateWifiConfiguration(
329                 buildTestWifiInfoForEAPJustAuthMethods("AKA_PRIME", "AKA_PRIME"));
330 
331         assertEAP_MethodAndPhase2Auth(
332                 wifiConf, WifiEnterpriseConfig.Eap.AKA_PRIME,
333                 WifiEnterpriseConfig.Phase2.AKA_PRIME);
334     }
335 
336     @Test
testEAPWithInvalidEAPMethod()337     public void testEAPWithInvalidEAPMethod() {
338         WifiConfiguration wifiConf = mProvider.generateWifiConfiguration(
339                 buildTestWifiInfoForEAPJustAuthMethods("ABC", "PAP"));
340 
341         assertEAP_MethodAndPhase2Auth(wifiConf, WifiEnterpriseConfig.Eap.NONE,
342                 WifiEnterpriseConfig.Phase2.NONE);
343     }
344 
345     @Test
testEAPWithInvalidPhase2Auth()346     public void testEAPWithInvalidPhase2Auth() {
347         WifiConfiguration wifiConf = mProvider.generateWifiConfiguration(
348                 buildTestWifiInfoForEAPJustAuthMethods("PEAP", "ABC"));
349 
350         assertEAP_MethodAndPhase2Auth(wifiConf, WifiEnterpriseConfig.Eap.NONE,
351                 WifiEnterpriseConfig.Phase2.NONE);
352     }
353 
buildBaseTestWifiInfoForEAP()354     private WifiInfo buildBaseTestWifiInfoForEAP() {
355         return buildTestWifiInfoForEAPWithCertificates(
356                 TEST_PASSWORD, TEST_IDENTITY, TEST_ANONYMOUS_IDENTITY, TEST_DOMAIN,
357                 /* caCertificate= */null, /* userCertificate= */null);
358     }
359 
buildTestWifiInfoForEAPJustAuthMethods(String eapMethod, String phase2Auth)360     private WifiInfo buildTestWifiInfoForEAPJustAuthMethods(String eapMethod, String phase2Auth) {
361         return BASE_EAP_BUILDER
362                 .setEapMethod(eapMethod)
363                 .setPhase2Auth(phase2Auth)
364                 .setCaCertificate(null)
365                 .setUserCertificate(null)
366                 .build();
367     }
368 
buildTestWifiInfoForEAPWithCertificates( String password, String identity, String anonymousIdentity, String domain, String caCertificate, String userCertificate)369     private WifiInfo buildTestWifiInfoForEAPWithCertificates(
370             String password, String identity, String anonymousIdentity, String domain,
371             String caCertificate, String userCertificate) {
372         return BASE_EAP_BUILDER
373                 .setEapMethod("PEAP")
374                 .setPhase2Auth("")
375                 .setPassword(password)
376                 .setIdentity(identity)
377                 .setAnonymousIdentity(anonymousIdentity)
378                 .setDomain(domain)
379                 .setCaCertificate(caCertificate)
380                 .setUserCertificate(userCertificate)
381                 .build();
382     }
383 
assertBase(WifiConfiguration wifiConf)384     private void assertBase(WifiConfiguration wifiConf) {
385         assertEquals(TEST_SSID, wifiConf.SSID);
386         assertEquals(TEST_HIDDEN, wifiConf.hiddenSSID);
387         assertEquals(WifiConfiguration.Status.ENABLED, wifiConf.status);
388     }
389 
assertEnterpriseConfig(WifiConfiguration wifiConf, String password, String identity, String anonymousIdentity, String domain, String caCertificate, String userCertificate, String privateKey)390     private void assertEnterpriseConfig(WifiConfiguration wifiConf,
391             String password, String identity, String anonymousIdentity, String domain,
392             String caCertificate, String userCertificate, String privateKey) {
393         assertEnterpriseConfigBase(wifiConf, password, identity, anonymousIdentity, domain);
394         assertCertificateInformation(wifiConf, caCertificate, userCertificate, privateKey);
395     }
396 
assertEnterpriseConfigWithoutCertificates(WifiConfiguration wifiConf, String password, String identity, String anonymousIdentity, String domain)397     private void assertEnterpriseConfigWithoutCertificates(WifiConfiguration wifiConf,
398             String password, String identity, String anonymousIdentity, String domain) {
399         assertEnterpriseConfigBase(wifiConf, password, identity, anonymousIdentity, domain);
400         assertEmptyCertificates(wifiConf);
401     }
402 
assertEnterpriseConfigBase(WifiConfiguration wifiConf, String password, String identity, String anonymousIdentity, String domain)403     private void assertEnterpriseConfigBase(WifiConfiguration wifiConf, String password,
404             String identity, String anonymousIdentity, String domain) {
405         assertNotEquals(null, wifiConf.enterpriseConfig);
406         assertEquals(password, wifiConf.enterpriseConfig.getPassword());
407         assertEquals(identity, wifiConf.enterpriseConfig.getIdentity());
408         assertEquals(anonymousIdentity, wifiConf.enterpriseConfig.getAnonymousIdentity());
409         assertEquals(domain, wifiConf.enterpriseConfig.getDomainSuffixMatch());
410     }
411 
assertCertificateInformation( WifiConfiguration wifiConf, String caCertificate, String userCertificate, String privateKey)412     private void assertCertificateInformation(
413             WifiConfiguration wifiConf, String caCertificate, String userCertificate,
414             String privateKey) {
415         try {
416             assertEquals(caCertificate, Base64.getEncoder()
417                     .encodeToString(wifiConf.enterpriseConfig.getCaCertificate().getEncoded()));
418             assertEquals(userCertificate, buildEncodedCertificateChain(
419                     wifiConf.enterpriseConfig.getClientCertificateChain()));
420             assertEquals(privateKey, Base64.getEncoder()
421                     .encodeToString(wifiConf.enterpriseConfig.getClientPrivateKey().getEncoded()));
422         } catch (CertificateEncodingException e) {
423             throw new IllegalArgumentException("Certificate cannot be encoded!");
424         }
425     }
426 
assertEmptyEnterpriseConfig(WifiConfiguration wifiConf)427     private void assertEmptyEnterpriseConfig(WifiConfiguration wifiConf) {
428         assertEnterpriseConfigBase(wifiConf, "", "", "",  "");
429         assertEmptyCertificates(wifiConf);
430     }
431 
assertEmptyCertificates(WifiConfiguration wifiConf)432     private void assertEmptyCertificates(WifiConfiguration wifiConf) {
433         assertEquals(null, wifiConf.enterpriseConfig.getCaCertificate());
434         assertEquals(null, wifiConf.enterpriseConfig.getClientPrivateKey());
435         assertEquals(null, wifiConf.enterpriseConfig.getClientCertificateChain());
436     }
437 
buildEncodedCertificateChain(X509Certificate[] clientCertificateChain)438     private String buildEncodedCertificateChain(X509Certificate[] clientCertificateChain)
439             throws CertificateEncodingException {
440         StringBuilder encodedCertChain = new StringBuilder();
441         for (X509Certificate certificate: clientCertificateChain) {
442             encodedCertChain.append(Base64.getEncoder().encodeToString(certificate.getEncoded()));
443         }
444         return encodedCertChain.toString();
445     }
446 
assertEAP_MethodAndPhase2Auth( WifiConfiguration wifiConf, int eapMethod, int phase2Auth)447     private void assertEAP_MethodAndPhase2Auth(
448             WifiConfiguration wifiConf, int eapMethod, int phase2Auth) {
449         assertEquals(eapMethod, wifiConf.enterpriseConfig.getEapMethod());
450         assertEquals(phase2Auth, wifiConf.enterpriseConfig.getPhase2Method());
451     }
452 
assertEAPAllowedKeyManagement(WifiConfiguration wifiConf)453     private void assertEAPAllowedKeyManagement(WifiConfiguration wifiConf) {
454         assertTrue(wifiConf.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X));
455         assertTrue(wifiConf.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP));
456     }
457 }
458