blob: 2dce2a281087e87488f9b04a05c904c75fa3440f [file] [log] [blame]
Peter Qiueaa6ff22016-09-12 10:00:13 -07001/*
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.hotspot2;
18
Peter Qiueaa6ff22016-09-12 10:00:13 -070019import static org.junit.Assert.assertEquals;
Peter Qiuc0447962016-11-02 14:41:25 -070020import static org.junit.Assert.assertFalse;
Peter Qiub54f07e2017-03-07 14:56:15 -080021import static org.junit.Assert.assertNull;
Peter Qiueaa6ff22016-09-12 10:00:13 -070022import static org.junit.Assert.assertTrue;
Peter Qiuae791272016-11-17 15:17:34 -080023import static org.mockito.Mockito.any;
Peter Qiu33c46cd2016-12-19 13:12:13 -080024import static org.mockito.Mockito.anyBoolean;
Sohani Rao0ddc2ba2017-10-09 14:36:48 -070025import static org.mockito.Mockito.anyInt;
Peter Qiud28cfdd2016-12-13 14:17:20 -080026import static org.mockito.Mockito.anyLong;
Peter Qiu3d424022016-11-30 13:36:25 -080027import static org.mockito.Mockito.anyMap;
Peter Qiuf331a6b2017-03-20 14:55:45 -070028import static org.mockito.Mockito.doThrow;
Peter Qiueaa6ff22016-09-12 10:00:13 -070029import static org.mockito.Mockito.eq;
Peter Qiuae791272016-11-17 15:17:34 -080030import static org.mockito.Mockito.mock;
Peter Qiu33c46cd2016-12-19 13:12:13 -080031import static org.mockito.Mockito.never;
Peter Qiu99d4b812017-02-14 09:42:55 -080032import static org.mockito.Mockito.reset;
Peter Qiueaa6ff22016-09-12 10:00:13 -070033import static org.mockito.Mockito.verify;
Peter Qiuc0447962016-11-02 14:41:25 -070034import static org.mockito.Mockito.when;
Peter Qiueaa6ff22016-09-12 10:00:13 -070035import static org.mockito.MockitoAnnotations.initMocks;
36
37import android.content.Context;
38import android.content.Intent;
Peter Qiuac4ab1b2017-06-06 13:59:04 -070039import android.net.Uri;
Peter Qiuc0447962016-11-02 14:41:25 -070040import android.net.wifi.EAPConstants;
Peter Qiuec28f862017-03-07 14:25:01 -080041import android.net.wifi.ScanResult;
Peter Qiu5e4822d2017-02-23 11:32:33 -080042import android.net.wifi.WifiConfiguration;
43import android.net.wifi.WifiEnterpriseConfig;
Etan Cohend3fb3d82017-10-17 08:43:56 -070044import android.net.wifi.WifiManager;
Peter Qiuac4ab1b2017-06-06 13:59:04 -070045import android.net.wifi.WifiSsid;
Sohani Rao0ddc2ba2017-10-09 14:36:48 -070046import android.net.wifi.hotspot2.IProvisioningCallback;
Peter Qiuac4ab1b2017-06-06 13:59:04 -070047import android.net.wifi.hotspot2.OsuProvider;
Peter Qiuc0447962016-11-02 14:41:25 -070048import android.net.wifi.hotspot2.PasspointConfiguration;
49import android.net.wifi.hotspot2.pps.Credential;
Peter Qiu260f8f72017-02-02 10:56:48 -080050import android.net.wifi.hotspot2.pps.HomeSp;
Sohani Rao0ddc2ba2017-10-09 14:36:48 -070051import android.os.Looper;
Peter Qiueaa6ff22016-09-12 10:00:13 -070052import android.os.UserHandle;
Sohani Rao0ddc2ba2017-10-09 14:36:48 -070053import android.os.test.TestLooper;
Etan Cohen7f2a1e32018-01-23 07:11:24 -080054import android.support.test.filters.SmallTest;
Peter Qiu5e4822d2017-02-23 11:32:33 -080055import android.util.Base64;
Peter Qiu3d424022016-11-30 13:36:25 -080056import android.util.Pair;
Peter Qiueaa6ff22016-09-12 10:00:13 -070057
Peter Qiuae791272016-11-17 15:17:34 -080058import com.android.server.wifi.Clock;
Peter Qiuc0447962016-11-02 14:41:25 -070059import com.android.server.wifi.FakeKeys;
60import com.android.server.wifi.IMSIParameter;
61import com.android.server.wifi.SIMAccessor;
Peter Qiu99d4b812017-02-14 09:42:55 -080062import com.android.server.wifi.WifiConfigManager;
63import com.android.server.wifi.WifiConfigStore;
Peter Qiuae791272016-11-17 15:17:34 -080064import com.android.server.wifi.WifiKeyStore;
Peter Qiu26b97ef2017-06-22 14:49:02 -070065import com.android.server.wifi.WifiMetrics;
Peter Qiuae791272016-11-17 15:17:34 -080066import com.android.server.wifi.WifiNative;
Peter Qiu33c46cd2016-12-19 13:12:13 -080067import com.android.server.wifi.hotspot2.anqp.ANQPElement;
68import com.android.server.wifi.hotspot2.anqp.Constants.ANQPElementType;
69import com.android.server.wifi.hotspot2.anqp.DomainNameElement;
Peter Qiuac4ab1b2017-06-06 13:59:04 -070070import com.android.server.wifi.hotspot2.anqp.HSOsuProvidersElement;
71import com.android.server.wifi.hotspot2.anqp.I18Name;
72import com.android.server.wifi.hotspot2.anqp.OsuProviderInfo;
Masashi Honma2e889dc2018-04-02 17:09:51 +090073import com.android.server.wifi.util.InformationElementUtil.RoamingConsortium;
Peter Qiu48c93da2017-03-09 13:28:47 -080074import com.android.server.wifi.util.ScanResultUtil;
Nate Jiang078fb3e2020-12-03 14:31:30 -080075import com.android.server.wifi.util.WifiPermissionsUtil;
Peter Qiueaa6ff22016-09-12 10:00:13 -070076
77import org.junit.Before;
78import org.junit.Test;
79import org.mockito.ArgumentCaptor;
80import org.mockito.Mock;
81
Peter Qiu5e4822d2017-02-23 11:32:33 -080082import java.nio.charset.StandardCharsets;
Peter Qiuf331a6b2017-03-20 14:55:45 -070083import java.security.GeneralSecurityException;
Sohani Rao10cae962017-11-29 15:46:00 -080084import java.security.KeyStore;
Peter Qiuf331a6b2017-03-20 14:55:45 -070085import java.security.cert.X509Certificate;
Peter Qiuc0447962016-11-02 14:41:25 -070086import java.util.ArrayList;
Peter Qiu33c46cd2016-12-19 13:12:13 -080087import java.util.Arrays;
88import java.util.HashMap;
Peter Qiuc0447962016-11-02 14:41:25 -070089import java.util.List;
Peter Qiuac4ab1b2017-06-06 13:59:04 -070090import java.util.Locale;
Peter Qiu33c46cd2016-12-19 13:12:13 -080091import java.util.Map;
Peter Qiuc0447962016-11-02 14:41:25 -070092
Peter Qiueaa6ff22016-09-12 10:00:13 -070093/**
94 * Unit tests for {@link com.android.server.wifi.hotspot2.PasspointManager}.
95 */
96@SmallTest
97public class PasspointManagerTest {
98 private static final long BSSID = 0x112233445566L;
Etan Cohend3fb3d82017-10-17 08:43:56 -070099 private static final String TEST_FQDN = "test1.test.com";
Peter Qiuc0447962016-11-02 14:41:25 -0700100 private static final String TEST_FRIENDLY_NAME = "friendly name";
101 private static final String TEST_REALM = "realm.test.com";
102 private static final String TEST_IMSI = "1234*";
Peter Qiu87c6f1b2016-12-15 15:22:31 -0800103 private static final IMSIParameter TEST_IMSI_PARAM = IMSIParameter.build(TEST_IMSI);
Peter Qiueaa6ff22016-09-12 10:00:13 -0700104
Peter Qiu3d424022016-11-30 13:36:25 -0800105 private static final String TEST_SSID = "TestSSID";
Peter Qiuec28f862017-03-07 14:25:01 -0800106 private static final long TEST_BSSID = 0x112233445566L;
107 private static final String TEST_BSSID_STRING = "11:22:33:44:55:66";
Peter Qiu3d424022016-11-30 13:36:25 -0800108 private static final long TEST_HESSID = 0x5678L;
Peter Qiuec28f862017-03-07 14:25:01 -0800109 private static final int TEST_ANQP_DOMAIN_ID = 0;
110 private static final ANQPNetworkKey TEST_ANQP_KEY = ANQPNetworkKey.buildKey(
111 TEST_SSID, TEST_BSSID, TEST_HESSID, TEST_ANQP_DOMAIN_ID);
Peter Qiu07816a42017-04-03 17:08:33 +0000112 private static final int TEST_CREATOR_UID = 1234;
Sohani Rao0ddc2ba2017-10-09 14:36:48 -0700113 private static final int TEST_UID = 1500;
Peter Qiu3d424022016-11-30 13:36:25 -0800114
Peter Qiueaa6ff22016-09-12 10:00:13 -0700115 @Mock Context mContext;
Peter Qiuae791272016-11-17 15:17:34 -0800116 @Mock WifiNative mWifiNative;
117 @Mock WifiKeyStore mWifiKeyStore;
118 @Mock Clock mClock;
Peter Qiuc0447962016-11-02 14:41:25 -0700119 @Mock SIMAccessor mSimAccessor;
Peter Qiuae791272016-11-17 15:17:34 -0800120 @Mock PasspointObjectFactory mObjectFactory;
121 @Mock PasspointEventHandler.Callbacks mCallbacks;
Peter Qiu3d424022016-11-30 13:36:25 -0800122 @Mock AnqpCache mAnqpCache;
Peter Qiu33c46cd2016-12-19 13:12:13 -0800123 @Mock ANQPRequestManager mAnqpRequestManager;
Peter Qiuf331a6b2017-03-20 14:55:45 -0700124 @Mock CertificateVerifier mCertVerifier;
Peter Qiu99d4b812017-02-14 09:42:55 -0800125 @Mock WifiConfigManager mWifiConfigManager;
126 @Mock WifiConfigStore mWifiConfigStore;
127 @Mock PasspointConfigStoreData.DataSource mDataSource;
Peter Qiu26b97ef2017-06-22 14:49:02 -0700128 @Mock WifiMetrics mWifiMetrics;
Sohani Rao0ddc2ba2017-10-09 14:36:48 -0700129 @Mock OsuNetworkConnection mOsuNetworkConnection;
Sohani Rao7df7bb82017-11-15 17:07:24 -0800130 @Mock OsuServerConnection mOsuServerConnection;
Sohani Raob36b9852017-10-09 14:36:48 -0700131 @Mock PasspointProvisioner mPasspointProvisioner;
Sohani Rao0ddc2ba2017-10-09 14:36:48 -0700132 @Mock IProvisioningCallback mCallback;
Sohani Rao10cae962017-11-29 15:46:00 -0800133 @Mock WfaKeyStore mWfaKeyStore;
134 @Mock KeyStore mKeyStore;
Nate Jiang078fb3e2020-12-03 14:31:30 -0800135 @Mock WifiPermissionsUtil mWifiPermissionsUtil;
Sohani Rao0ddc2ba2017-10-09 14:36:48 -0700136 TestLooper mLooper;
Peter Qiueaa6ff22016-09-12 10:00:13 -0700137 PasspointManager mManager;
138
139 /** Sets up test. */
140 @Before
141 public void setUp() throws Exception {
142 initMocks(this);
Peter Qiu3d424022016-11-30 13:36:25 -0800143 when(mObjectFactory.makeAnqpCache(mClock)).thenReturn(mAnqpCache);
Peter Qiu99b966a2017-03-23 10:04:55 -0700144 when(mObjectFactory.makeANQPRequestManager(any(), eq(mClock)))
Peter Qiu33c46cd2016-12-19 13:12:13 -0800145 .thenReturn(mAnqpRequestManager);
Peter Qiuf331a6b2017-03-20 14:55:45 -0700146 when(mObjectFactory.makeCertificateVerifier()).thenReturn(mCertVerifier);
Sohani Rao0ddc2ba2017-10-09 14:36:48 -0700147 when(mObjectFactory.makeOsuNetworkConnection(any(Context.class)))
148 .thenReturn(mOsuNetworkConnection);
Sohani Rao7df7bb82017-11-15 17:07:24 -0800149 when(mObjectFactory.makeOsuServerConnection())
150 .thenReturn(mOsuServerConnection);
Sohani Rao10cae962017-11-29 15:46:00 -0800151 when(mObjectFactory.makeWfaKeyStore()).thenReturn(mWfaKeyStore);
152 when(mWfaKeyStore.get()).thenReturn(mKeyStore);
153 when(mObjectFactory.makePasspointProvisioner(any(Context.class)))
Sohani Rao7df7bb82017-11-15 17:07:24 -0800154 .thenReturn(mPasspointProvisioner);
Peter Qiuae791272016-11-17 15:17:34 -0800155 mManager = new PasspointManager(mContext, mWifiNative, mWifiKeyStore, mClock,
Nate Jiang078fb3e2020-12-03 14:31:30 -0800156 mSimAccessor, mObjectFactory, mWifiConfigManager, mWifiConfigStore, mWifiMetrics,
157 mWifiPermissionsUtil);
Peter Qiueaa6ff22016-09-12 10:00:13 -0700158 ArgumentCaptor<PasspointEventHandler.Callbacks> callbacks =
159 ArgumentCaptor.forClass(PasspointEventHandler.Callbacks.class);
Peter Qiuae791272016-11-17 15:17:34 -0800160 verify(mObjectFactory).makePasspointEventHandler(any(WifiNative.class),
161 callbacks.capture());
Peter Qiu99d4b812017-02-14 09:42:55 -0800162 ArgumentCaptor<PasspointConfigStoreData.DataSource> dataSource =
163 ArgumentCaptor.forClass(PasspointConfigStoreData.DataSource.class);
164 verify(mObjectFactory).makePasspointConfigStoreData(
165 any(WifiKeyStore.class), any(SIMAccessor.class), dataSource.capture());
Peter Qiueaa6ff22016-09-12 10:00:13 -0700166 mCallbacks = callbacks.getValue();
Peter Qiu99d4b812017-02-14 09:42:55 -0800167 mDataSource = dataSource.getValue();
Sohani Rao0ddc2ba2017-10-09 14:36:48 -0700168 mLooper = new TestLooper();
Peter Qiueaa6ff22016-09-12 10:00:13 -0700169 }
170
171 /**
Peter Qiuc0447962016-11-02 14:41:25 -0700172 * Verify that the given Passpoint configuration matches the one that's added to
173 * the PasspointManager.
174 *
175 * @param expectedConfig The expected installed Passpoint configuration
176 */
177 private void verifyInstalledConfig(PasspointConfiguration expectedConfig) {
178 List<PasspointConfiguration> installedConfigs = mManager.getProviderConfigs();
179 assertEquals(1, installedConfigs.size());
180 assertEquals(expectedConfig, installedConfigs.get(0));
181 }
182
183 /**
Peter Qiuae791272016-11-17 15:17:34 -0800184 * Create a mock PasspointProvider with default expectations.
185 *
186 * @param config The configuration associated with the provider
187 * @return {@link com.android.server.wifi.hotspot2.PasspointProvider}
188 */
189 private PasspointProvider createMockProvider(PasspointConfiguration config) {
190 PasspointProvider provider = mock(PasspointProvider.class);
191 when(provider.installCertsAndKeys()).thenReturn(true);
192 when(provider.getConfig()).thenReturn(config);
193 return provider;
194 }
195
196 /**
Peter Qiu4f4d7452017-01-24 15:51:38 -0800197 * Helper function for creating a test configuration with user credential.
198 *
199 * @return {@link PasspointConfiguration}
200 */
Etan Cohend3fb3d82017-10-17 08:43:56 -0700201 private PasspointConfiguration createTestConfigWithUserCredential(String fqdn) {
Peter Qiu4f4d7452017-01-24 15:51:38 -0800202 PasspointConfiguration config = new PasspointConfiguration();
Peter Qiu260f8f72017-02-02 10:56:48 -0800203 HomeSp homeSp = new HomeSp();
Etan Cohend3fb3d82017-10-17 08:43:56 -0700204 homeSp.setFqdn(fqdn);
Peter Qiu4f4d7452017-01-24 15:51:38 -0800205 homeSp.setFriendlyName(TEST_FRIENDLY_NAME);
206 config.setHomeSp(homeSp);
207 Credential credential = new Credential();
208 credential.setRealm(TEST_REALM);
209 credential.setCaCertificate(FakeKeys.CA_CERT0);
210 Credential.UserCredential userCredential = new Credential.UserCredential();
211 userCredential.setUsername("username");
212 userCredential.setPassword("password");
213 userCredential.setEapType(EAPConstants.EAP_TTLS);
Peter Qiu5e4822d2017-02-23 11:32:33 -0800214 userCredential.setNonEapInnerMethod(Credential.UserCredential.AUTH_METHOD_MSCHAP);
Peter Qiu4f4d7452017-01-24 15:51:38 -0800215 credential.setUserCredential(userCredential);
216 config.setCredential(credential);
217 return config;
218 }
219
220 /**
221 * Helper function for creating a test configuration with SIM credential.
222 *
223 * @return {@link PasspointConfiguration}
224 */
225 private PasspointConfiguration createTestConfigWithSimCredential() {
226 PasspointConfiguration config = new PasspointConfiguration();
Peter Qiu260f8f72017-02-02 10:56:48 -0800227 HomeSp homeSp = new HomeSp();
Peter Qiu4f4d7452017-01-24 15:51:38 -0800228 homeSp.setFqdn(TEST_FQDN);
229 homeSp.setFriendlyName(TEST_FRIENDLY_NAME);
230 config.setHomeSp(homeSp);
231 Credential credential = new Credential();
232 credential.setRealm(TEST_REALM);
233 Credential.SimCredential simCredential = new Credential.SimCredential();
234 simCredential.setImsi(TEST_IMSI);
235 simCredential.setEapType(EAPConstants.EAP_SIM);
236 credential.setSimCredential(simCredential);
237 config.setCredential(credential);
238 return config;
239 }
240
241 /**
Peter Qiu3d424022016-11-30 13:36:25 -0800242 * Helper function for adding a test provider to the manager. Return the mock
243 * provider that's added to the manager.
244 *
245 * @return {@link PasspointProvider}
246 */
Etan Cohend3fb3d82017-10-17 08:43:56 -0700247 private PasspointProvider addTestProvider(String fqdn) {
248 PasspointConfiguration config = createTestConfigWithUserCredential(fqdn);
Peter Qiu3d424022016-11-30 13:36:25 -0800249 PasspointProvider provider = createMockProvider(config);
Peter Qiu87c6f1b2016-12-15 15:22:31 -0800250 when(mObjectFactory.makePasspointProvider(eq(config), eq(mWifiKeyStore),
Peter Qiu07816a42017-04-03 17:08:33 +0000251 eq(mSimAccessor), anyLong(), eq(TEST_CREATOR_UID))).thenReturn(provider);
252 assertTrue(mManager.addOrUpdateProvider(config, TEST_CREATOR_UID));
Peter Qiu3d424022016-11-30 13:36:25 -0800253
254 return provider;
255 }
256
257 /**
Peter Qiuec28f862017-03-07 14:25:01 -0800258 * Helper function for creating a ScanResult for testing.
Peter Qiu3d424022016-11-30 13:36:25 -0800259 *
Peter Qiuec28f862017-03-07 14:25:01 -0800260 * @return {@link ScanResult}
Peter Qiu3d424022016-11-30 13:36:25 -0800261 */
Peter Qiuec28f862017-03-07 14:25:01 -0800262 private ScanResult createTestScanResult() {
263 ScanResult scanResult = new ScanResult();
264 scanResult.SSID = TEST_SSID;
265 scanResult.BSSID = TEST_BSSID_STRING;
266 scanResult.hessid = TEST_HESSID;
Peter Qiu33c3ae82017-06-02 11:35:06 -0700267 scanResult.flags = ScanResult.FLAG_PASSPOINT_NETWORK;
Peter Qiuec28f862017-03-07 14:25:01 -0800268 return scanResult;
Peter Qiu3d424022016-11-30 13:36:25 -0800269 }
270
271 /**
Peter Qiu33c46cd2016-12-19 13:12:13 -0800272 * Verify that the ANQP elements will be added to the ANQP cache on receiving a successful
273 * response.
274 *
275 * @throws Exception
276 */
277 @Test
278 public void anqpResponseSuccess() throws Exception {
279 Map<ANQPElementType, ANQPElement> anqpElementMap = new HashMap<>();
280 anqpElementMap.put(ANQPElementType.ANQPDomName,
281 new DomainNameElement(Arrays.asList(new String[] {"test.com"})));
282
Peter Qiuec28f862017-03-07 14:25:01 -0800283 when(mAnqpRequestManager.onRequestCompleted(TEST_BSSID, true)).thenReturn(TEST_ANQP_KEY);
Peter Qiu33c46cd2016-12-19 13:12:13 -0800284 mCallbacks.onANQPResponse(TEST_BSSID, anqpElementMap);
Peter Qiuec28f862017-03-07 14:25:01 -0800285 verify(mAnqpCache).addEntry(TEST_ANQP_KEY, anqpElementMap);
Peter Qiue8fd83b2017-03-20 13:55:52 -0700286 verify(mContext, never()).sendBroadcastAsUser(any(Intent.class), any(UserHandle.class),
287 any(String.class));
288 }
289
290 /**
Peter Qiu33c46cd2016-12-19 13:12:13 -0800291 * Verify that no ANQP elements will be added to the ANQP cache on receiving a successful
292 * response for a request that's not sent by us.
293 *
294 * @throws Exception
295 */
296 @Test
297 public void anqpResponseSuccessWithUnknownRequest() throws Exception {
298 Map<ANQPElementType, ANQPElement> anqpElementMap = new HashMap<>();
299 anqpElementMap.put(ANQPElementType.ANQPDomName,
300 new DomainNameElement(Arrays.asList(new String[] {"test.com"})));
301
302 when(mAnqpRequestManager.onRequestCompleted(TEST_BSSID, true)).thenReturn(null);
303 mCallbacks.onANQPResponse(TEST_BSSID, anqpElementMap);
304 verify(mAnqpCache, never()).addEntry(any(ANQPNetworkKey.class), anyMap());
305 }
306
307 /**
308 * Verify that no ANQP elements will be added to the ANQP cache on receiving a failure response.
309 *
310 * @throws Exception
311 */
312 @Test
313 public void anqpResponseFailure() throws Exception {
Peter Qiuec28f862017-03-07 14:25:01 -0800314 when(mAnqpRequestManager.onRequestCompleted(TEST_BSSID, false)).thenReturn(TEST_ANQP_KEY);
Peter Qiu33c46cd2016-12-19 13:12:13 -0800315 mCallbacks.onANQPResponse(TEST_BSSID, null);
316 verify(mAnqpCache, never()).addEntry(any(ANQPNetworkKey.class), anyMap());
317
318 }
319
320 /**
Peter Qiuc0447962016-11-02 14:41:25 -0700321 * Verify that adding a provider with a null configuration will fail.
322 *
323 * @throws Exception
324 */
325 @Test
326 public void addProviderWithNullConfig() throws Exception {
Peter Qiu07816a42017-04-03 17:08:33 +0000327 assertFalse(mManager.addOrUpdateProvider(null, TEST_CREATOR_UID));
Peter Qiu26b97ef2017-06-22 14:49:02 -0700328 verify(mWifiMetrics).incrementNumPasspointProviderInstallation();
329 verify(mWifiMetrics, never()).incrementNumPasspointProviderInstallSuccess();
Peter Qiuc0447962016-11-02 14:41:25 -0700330 }
331
332 /**
333 * Verify that adding a provider with a empty configuration will fail.
334 *
335 * @throws Exception
336 */
337 @Test
338 public void addProviderWithEmptyConfig() throws Exception {
Peter Qiu07816a42017-04-03 17:08:33 +0000339 assertFalse(mManager.addOrUpdateProvider(new PasspointConfiguration(), TEST_CREATOR_UID));
Peter Qiu26b97ef2017-06-22 14:49:02 -0700340 verify(mWifiMetrics).incrementNumPasspointProviderInstallation();
341 verify(mWifiMetrics, never()).incrementNumPasspointProviderInstallSuccess();
Peter Qiuc0447962016-11-02 14:41:25 -0700342 }
343
344 /**
345 * Verify taht adding a provider with an invalid credential will fail (using EAP-TLS
346 * for user credential).
347 *
348 * @throws Exception
349 */
350 @Test
351 public void addProviderWithInvalidCredential() throws Exception {
Etan Cohend3fb3d82017-10-17 08:43:56 -0700352 PasspointConfiguration config = createTestConfigWithUserCredential(TEST_FQDN);
Peter Qiuc0447962016-11-02 14:41:25 -0700353 // EAP-TLS not allowed for user credential.
Peter Qiu4f4d7452017-01-24 15:51:38 -0800354 config.getCredential().getUserCredential().setEapType(EAPConstants.EAP_TLS);
Peter Qiu07816a42017-04-03 17:08:33 +0000355 assertFalse(mManager.addOrUpdateProvider(config, TEST_CREATOR_UID));
Peter Qiu26b97ef2017-06-22 14:49:02 -0700356 verify(mWifiMetrics).incrementNumPasspointProviderInstallation();
357 verify(mWifiMetrics, never()).incrementNumPasspointProviderInstallSuccess();
Peter Qiuc0447962016-11-02 14:41:25 -0700358 }
359
360 /**
361 * Verify that adding a provider with a valid configuration and user credential will succeed.
362 *
363 * @throws Exception
364 */
365 @Test
366 public void addRemoveProviderWithValidUserCredential() throws Exception {
Etan Cohend3fb3d82017-10-17 08:43:56 -0700367 PasspointConfiguration config = createTestConfigWithUserCredential(TEST_FQDN);
Peter Qiuae791272016-11-17 15:17:34 -0800368 PasspointProvider provider = createMockProvider(config);
Peter Qiu87c6f1b2016-12-15 15:22:31 -0800369 when(mObjectFactory.makePasspointProvider(eq(config), eq(mWifiKeyStore),
Peter Qiu07816a42017-04-03 17:08:33 +0000370 eq(mSimAccessor), anyLong(), eq(TEST_CREATOR_UID))).thenReturn(provider);
371 assertTrue(mManager.addOrUpdateProvider(config, TEST_CREATOR_UID));
Peter Qiuc0447962016-11-02 14:41:25 -0700372 verifyInstalledConfig(config);
Peter Qiu99d4b812017-02-14 09:42:55 -0800373 verify(mWifiConfigManager).saveToStore(true);
Peter Qiu26b97ef2017-06-22 14:49:02 -0700374 verify(mWifiMetrics).incrementNumPasspointProviderInstallation();
375 verify(mWifiMetrics).incrementNumPasspointProviderInstallSuccess();
376 reset(mWifiMetrics);
Peter Qiu99d4b812017-02-14 09:42:55 -0800377 reset(mWifiConfigManager);
378
379 // Verify content in the data source.
380 List<PasspointProvider> providers = mDataSource.getProviders();
381 assertEquals(1, providers.size());
382 assertEquals(config, providers.get(0).getConfig());
383 // Provider index start with 0, should be 1 after adding a provider.
384 assertEquals(1, mDataSource.getProviderIndex());
Peter Qiuc0447962016-11-02 14:41:25 -0700385
386 // Remove the provider.
Nate Jiang078fb3e2020-12-03 14:31:30 -0800387 assertTrue(mManager.removeProvider(TEST_CREATOR_UID, TEST_FQDN));
Peter Qiuae791272016-11-17 15:17:34 -0800388 verify(provider).uninstallCertsAndKeys();
Peter Qiu99d4b812017-02-14 09:42:55 -0800389 verify(mWifiConfigManager).saveToStore(true);
Peter Qiu26b97ef2017-06-22 14:49:02 -0700390 verify(mWifiMetrics).incrementNumPasspointProviderUninstallation();
391 verify(mWifiMetrics).incrementNumPasspointProviderUninstallSuccess();
Peter Qiud28cfdd2016-12-13 14:17:20 -0800392 assertTrue(mManager.getProviderConfigs().isEmpty());
Peter Qiu99d4b812017-02-14 09:42:55 -0800393
394 // Verify content in the data source.
395 assertTrue(mDataSource.getProviders().isEmpty());
396 // Removing a provider should not change the provider index.
397 assertEquals(1, mDataSource.getProviderIndex());
Peter Qiuc0447962016-11-02 14:41:25 -0700398 }
399
400 /**
401 * Verify that adding a provider with a valid configuration and SIM credential will succeed.
402 *
403 * @throws Exception
404 */
405 @Test
406 public void addRemoveProviderWithValidSimCredential() throws Exception {
Peter Qiu4f4d7452017-01-24 15:51:38 -0800407 PasspointConfiguration config = createTestConfigWithSimCredential();
Peter Qiuae791272016-11-17 15:17:34 -0800408 PasspointProvider provider = createMockProvider(config);
Peter Qiu87c6f1b2016-12-15 15:22:31 -0800409 when(mObjectFactory.makePasspointProvider(eq(config), eq(mWifiKeyStore),
Peter Qiu07816a42017-04-03 17:08:33 +0000410 eq(mSimAccessor), anyLong(), eq(TEST_CREATOR_UID))).thenReturn(provider);
411 assertTrue(mManager.addOrUpdateProvider(config, TEST_CREATOR_UID));
Peter Qiuc0447962016-11-02 14:41:25 -0700412 verifyInstalledConfig(config);
Peter Qiu99d4b812017-02-14 09:42:55 -0800413 verify(mWifiConfigManager).saveToStore(true);
Peter Qiu26b97ef2017-06-22 14:49:02 -0700414 verify(mWifiMetrics).incrementNumPasspointProviderInstallation();
415 verify(mWifiMetrics).incrementNumPasspointProviderInstallSuccess();
416 reset(mWifiMetrics);
Peter Qiu99d4b812017-02-14 09:42:55 -0800417 reset(mWifiConfigManager);
418
419 // Verify content in the data source.
420 List<PasspointProvider> providers = mDataSource.getProviders();
421 assertEquals(1, providers.size());
422 assertEquals(config, providers.get(0).getConfig());
423 // Provider index start with 0, should be 1 after adding a provider.
424 assertEquals(1, mDataSource.getProviderIndex());
Peter Qiuc0447962016-11-02 14:41:25 -0700425
426 // Remove the provider.
Nate Jiang078fb3e2020-12-03 14:31:30 -0800427 assertTrue(mManager.removeProvider(TEST_CREATOR_UID, TEST_FQDN));
Peter Qiuae791272016-11-17 15:17:34 -0800428 verify(provider).uninstallCertsAndKeys();
Peter Qiu99d4b812017-02-14 09:42:55 -0800429 verify(mWifiConfigManager).saveToStore(true);
Peter Qiu26b97ef2017-06-22 14:49:02 -0700430 verify(mWifiMetrics).incrementNumPasspointProviderUninstallation();
431 verify(mWifiMetrics).incrementNumPasspointProviderUninstallSuccess();
Peter Qiud28cfdd2016-12-13 14:17:20 -0800432 assertTrue(mManager.getProviderConfigs().isEmpty());
Peter Qiu99d4b812017-02-14 09:42:55 -0800433
434 // Verify content in the data source.
435 assertTrue(mDataSource.getProviders().isEmpty());
436 // Removing a provider should not change the provider index.
437 assertEquals(1, mDataSource.getProviderIndex());
Peter Qiuc0447962016-11-02 14:41:25 -0700438 }
439
440 /**
Peter Qiuc0447962016-11-02 14:41:25 -0700441 * Verify that adding a provider with the same base domain as the existing provider will
442 * succeed, and verify that the existing provider is replaced by the new provider with
443 * the new configuration.
444 *
445 * @throws Exception
446 */
447 @Test
448 public void addProviderWithExistingConfig() throws Exception {
449 // Add a provider with the original configuration.
Peter Qiu4f4d7452017-01-24 15:51:38 -0800450 PasspointConfiguration origConfig = createTestConfigWithSimCredential();
Peter Qiuae791272016-11-17 15:17:34 -0800451 PasspointProvider origProvider = createMockProvider(origConfig);
Peter Qiu87c6f1b2016-12-15 15:22:31 -0800452 when(mObjectFactory.makePasspointProvider(eq(origConfig), eq(mWifiKeyStore),
Peter Qiu07816a42017-04-03 17:08:33 +0000453 eq(mSimAccessor), anyLong(), eq(TEST_CREATOR_UID))).thenReturn(origProvider);
454 assertTrue(mManager.addOrUpdateProvider(origConfig, TEST_CREATOR_UID));
Peter Qiuc0447962016-11-02 14:41:25 -0700455 verifyInstalledConfig(origConfig);
Peter Qiu99d4b812017-02-14 09:42:55 -0800456 verify(mWifiConfigManager).saveToStore(true);
Peter Qiu26b97ef2017-06-22 14:49:02 -0700457 verify(mWifiMetrics).incrementNumPasspointProviderInstallation();
458 verify(mWifiMetrics).incrementNumPasspointProviderInstallSuccess();
459 reset(mWifiMetrics);
Peter Qiu99d4b812017-02-14 09:42:55 -0800460 reset(mWifiConfigManager);
461
462 // Verify data source content.
463 List<PasspointProvider> origProviders = mDataSource.getProviders();
464 assertEquals(1, origProviders.size());
465 assertEquals(origConfig, origProviders.get(0).getConfig());
466 assertEquals(1, mDataSource.getProviderIndex());
Peter Qiuc0447962016-11-02 14:41:25 -0700467
468 // Add another provider with the same base domain as the existing provider.
469 // This should replace the existing provider with the new configuration.
Etan Cohend3fb3d82017-10-17 08:43:56 -0700470 PasspointConfiguration newConfig = createTestConfigWithUserCredential(TEST_FQDN);
Peter Qiuae791272016-11-17 15:17:34 -0800471 PasspointProvider newProvider = createMockProvider(newConfig);
Peter Qiu87c6f1b2016-12-15 15:22:31 -0800472 when(mObjectFactory.makePasspointProvider(eq(newConfig), eq(mWifiKeyStore),
Peter Qiu07816a42017-04-03 17:08:33 +0000473 eq(mSimAccessor), anyLong(), eq(TEST_CREATOR_UID))).thenReturn(newProvider);
474 assertTrue(mManager.addOrUpdateProvider(newConfig, TEST_CREATOR_UID));
Peter Qiuc0447962016-11-02 14:41:25 -0700475 verifyInstalledConfig(newConfig);
Peter Qiu99d4b812017-02-14 09:42:55 -0800476 verify(mWifiConfigManager).saveToStore(true);
Peter Qiu26b97ef2017-06-22 14:49:02 -0700477 verify(mWifiMetrics).incrementNumPasspointProviderInstallation();
478 verify(mWifiMetrics).incrementNumPasspointProviderInstallSuccess();
Peter Qiu99d4b812017-02-14 09:42:55 -0800479
480 // Verify data source content.
481 List<PasspointProvider> newProviders = mDataSource.getProviders();
482 assertEquals(1, newProviders.size());
483 assertEquals(newConfig, newProviders.get(0).getConfig());
484 assertEquals(2, mDataSource.getProviderIndex());
Peter Qiuc0447962016-11-02 14:41:25 -0700485 }
Peter Qiuae791272016-11-17 15:17:34 -0800486
487 /**
488 * Verify that adding a provider will fail when failing to install certificates and
489 * key to the keystore.
490 *
491 * @throws Exception
492 */
493 @Test
494 public void addProviderOnKeyInstallationFailiure() throws Exception {
Etan Cohend3fb3d82017-10-17 08:43:56 -0700495 PasspointConfiguration config = createTestConfigWithUserCredential(TEST_FQDN);
Peter Qiuae791272016-11-17 15:17:34 -0800496 PasspointProvider provider = mock(PasspointProvider.class);
497 when(provider.installCertsAndKeys()).thenReturn(false);
Peter Qiu87c6f1b2016-12-15 15:22:31 -0800498 when(mObjectFactory.makePasspointProvider(eq(config), eq(mWifiKeyStore),
Peter Qiu07816a42017-04-03 17:08:33 +0000499 eq(mSimAccessor), anyLong(), eq(TEST_CREATOR_UID))).thenReturn(provider);
500 assertFalse(mManager.addOrUpdateProvider(config, TEST_CREATOR_UID));
Peter Qiu26b97ef2017-06-22 14:49:02 -0700501 verify(mWifiMetrics).incrementNumPasspointProviderInstallation();
502 verify(mWifiMetrics, never()).incrementNumPasspointProviderInstallSuccess();
Peter Qiuae791272016-11-17 15:17:34 -0800503 }
504
505 /**
Peter Qiuf331a6b2017-03-20 14:55:45 -0700506 * Verify that adding a provider with an invalid CA certificate will fail.
507 *
508 * @throws Exception
509 */
510 @Test
511 public void addProviderWithInvalidCaCert() throws Exception {
Etan Cohend3fb3d82017-10-17 08:43:56 -0700512 PasspointConfiguration config = createTestConfigWithUserCredential(TEST_FQDN);
Peter Qiuf331a6b2017-03-20 14:55:45 -0700513 doThrow(new GeneralSecurityException())
514 .when(mCertVerifier).verifyCaCert(any(X509Certificate.class));
Peter Qiu07816a42017-04-03 17:08:33 +0000515 assertFalse(mManager.addOrUpdateProvider(config, TEST_CREATOR_UID));
Peter Qiu26b97ef2017-06-22 14:49:02 -0700516 verify(mWifiMetrics).incrementNumPasspointProviderInstallation();
517 verify(mWifiMetrics, never()).incrementNumPasspointProviderInstallSuccess();
Peter Qiuf331a6b2017-03-20 14:55:45 -0700518 }
519
520 /**
521 * Verify that adding a provider with R2 configuration will not perform CA certificate
522 * verification.
523 *
524 * @throws Exception
525 */
526 @Test
527 public void addProviderWithR2Config() throws Exception {
Etan Cohend3fb3d82017-10-17 08:43:56 -0700528 PasspointConfiguration config = createTestConfigWithUserCredential(TEST_FQDN);
Peter Qiuf331a6b2017-03-20 14:55:45 -0700529 config.setUpdateIdentifier(1);
530 PasspointProvider provider = createMockProvider(config);
531 when(mObjectFactory.makePasspointProvider(eq(config), eq(mWifiKeyStore),
Peter Qiu07816a42017-04-03 17:08:33 +0000532 eq(mSimAccessor), anyLong(), eq(TEST_CREATOR_UID))).thenReturn(provider);
533 assertTrue(mManager.addOrUpdateProvider(config, TEST_CREATOR_UID));
Peter Qiuf331a6b2017-03-20 14:55:45 -0700534 verify(mCertVerifier, never()).verifyCaCert(any(X509Certificate.class));
535 verifyInstalledConfig(config);
Peter Qiu26b97ef2017-06-22 14:49:02 -0700536 verify(mWifiMetrics).incrementNumPasspointProviderInstallation();
537 verify(mWifiMetrics).incrementNumPasspointProviderInstallSuccess();
Peter Qiuf331a6b2017-03-20 14:55:45 -0700538 }
539
540 /**
Peter Qiuae791272016-11-17 15:17:34 -0800541 * Verify that removing a non-existing provider will fail.
542 *
543 * @throws Exception
544 */
545 @Test
546 public void removeNonExistingProvider() throws Exception {
Nate Jiang078fb3e2020-12-03 14:31:30 -0800547 assertFalse(mManager.removeProvider(TEST_CREATOR_UID, TEST_FQDN));
Peter Qiu26b97ef2017-06-22 14:49:02 -0700548 verify(mWifiMetrics).incrementNumPasspointProviderUninstallation();
549 verify(mWifiMetrics, never()).incrementNumPasspointProviderUninstallSuccess();
Peter Qiuae791272016-11-17 15:17:34 -0800550 }
Peter Qiu3d424022016-11-30 13:36:25 -0800551
552 /**
Peter Qiub54f07e2017-03-07 14:56:15 -0800553 * Verify that a {code null} will be returned when no providers are installed.
Peter Qiu3d424022016-11-30 13:36:25 -0800554 *
555 * @throws Exception
556 */
557 @Test
558 public void matchProviderWithNoProvidersInstalled() throws Exception {
Peter Qiub54f07e2017-03-07 14:56:15 -0800559 assertNull(mManager.matchProvider(createTestScanResult()));
Peter Qiu3d424022016-11-30 13:36:25 -0800560 }
561
562 /**
Peter Qiub54f07e2017-03-07 14:56:15 -0800563 * Verify that a {code null} be returned when ANQP entry doesn't exist in the cache.
Peter Qiu3d424022016-11-30 13:36:25 -0800564 *
565 * @throws Exception
566 */
567 @Test
568 public void matchProviderWithAnqpCacheMissed() throws Exception {
Etan Cohend3fb3d82017-10-17 08:43:56 -0700569 addTestProvider(TEST_FQDN);
Peter Qiu3d424022016-11-30 13:36:25 -0800570
Peter Qiuec28f862017-03-07 14:25:01 -0800571 when(mAnqpCache.getEntry(TEST_ANQP_KEY)).thenReturn(null);
Peter Qiub54f07e2017-03-07 14:56:15 -0800572 assertNull(mManager.matchProvider(createTestScanResult()));
Peter Qiu33c46cd2016-12-19 13:12:13 -0800573 // Verify that a request for ANQP elements is initiated.
Peter Qiuec28f862017-03-07 14:25:01 -0800574 verify(mAnqpRequestManager).requestANQPElements(eq(TEST_BSSID), any(ANQPNetworkKey.class),
Peter Qiu33c46cd2016-12-19 13:12:13 -0800575 anyBoolean(), anyBoolean());
Peter Qiu3d424022016-11-30 13:36:25 -0800576 }
577
578 /**
Peter Qiub54f07e2017-03-07 14:56:15 -0800579 * Verify that the expected provider will be returned when a HomeProvider is matched.
Peter Qiu3d424022016-11-30 13:36:25 -0800580 *
581 * @throws Exception
582 */
583 @Test
584 public void matchProviderAsHomeProvider() throws Exception {
Etan Cohend3fb3d82017-10-17 08:43:56 -0700585 PasspointProvider provider = addTestProvider(TEST_FQDN);
Peter Qiu3d424022016-11-30 13:36:25 -0800586 ANQPData entry = new ANQPData(mClock, null);
Peter Qiu3d424022016-11-30 13:36:25 -0800587
Peter Qiuec28f862017-03-07 14:25:01 -0800588 when(mAnqpCache.getEntry(TEST_ANQP_KEY)).thenReturn(entry);
Masashi Honma2e889dc2018-04-02 17:09:51 +0900589 when(provider.match(anyMap(), any(RoamingConsortium.class)))
590 .thenReturn(PasspointMatch.HomeProvider);
Peter Qiub54f07e2017-03-07 14:56:15 -0800591 Pair<PasspointProvider, PasspointMatch> result =
Peter Qiuec28f862017-03-07 14:25:01 -0800592 mManager.matchProvider(createTestScanResult());
Peter Qiub54f07e2017-03-07 14:56:15 -0800593 assertEquals(PasspointMatch.HomeProvider, result.second);
594 assertEquals(TEST_FQDN, result.first.getConfig().getHomeSp().getFqdn());
Peter Qiu3d424022016-11-30 13:36:25 -0800595 }
596
597 /**
Peter Qiub54f07e2017-03-07 14:56:15 -0800598 * Verify that the expected provider will be returned when a RoamingProvider is matched.
Peter Qiu3d424022016-11-30 13:36:25 -0800599 *
600 * @throws Exception
601 */
602 @Test
603 public void matchProviderAsRoamingProvider() throws Exception {
Etan Cohend3fb3d82017-10-17 08:43:56 -0700604 PasspointProvider provider = addTestProvider(TEST_FQDN);
Peter Qiu3d424022016-11-30 13:36:25 -0800605 ANQPData entry = new ANQPData(mClock, null);
Peter Qiu3d424022016-11-30 13:36:25 -0800606
Peter Qiuec28f862017-03-07 14:25:01 -0800607 when(mAnqpCache.getEntry(TEST_ANQP_KEY)).thenReturn(entry);
Masashi Honma2e889dc2018-04-02 17:09:51 +0900608 when(provider.match(anyMap(), any(RoamingConsortium.class)))
609 .thenReturn(PasspointMatch.RoamingProvider);
Peter Qiub54f07e2017-03-07 14:56:15 -0800610 Pair<PasspointProvider, PasspointMatch> result =
Peter Qiuec28f862017-03-07 14:25:01 -0800611 mManager.matchProvider(createTestScanResult());
Peter Qiub54f07e2017-03-07 14:56:15 -0800612 assertEquals(PasspointMatch.RoamingProvider, result.second);
613 assertEquals(TEST_FQDN, result.first.getConfig().getHomeSp().getFqdn());
Peter Qiu3d424022016-11-30 13:36:25 -0800614 }
615
616 /**
Peter Qiub54f07e2017-03-07 14:56:15 -0800617 * Verify that a {code null} will be returned when there is no matching provider.
Peter Qiu3d424022016-11-30 13:36:25 -0800618 *
619 * @throws Exception
620 */
621 @Test
622 public void matchProviderWithNoMatch() throws Exception {
Etan Cohend3fb3d82017-10-17 08:43:56 -0700623 PasspointProvider provider = addTestProvider(TEST_FQDN);
Peter Qiu3d424022016-11-30 13:36:25 -0800624 ANQPData entry = new ANQPData(mClock, null);
Peter Qiu3d424022016-11-30 13:36:25 -0800625
Peter Qiuec28f862017-03-07 14:25:01 -0800626 when(mAnqpCache.getEntry(TEST_ANQP_KEY)).thenReturn(entry);
Masashi Honma2e889dc2018-04-02 17:09:51 +0900627 when(provider.match(anyMap(), any(RoamingConsortium.class)))
628 .thenReturn(PasspointMatch.None);
Peter Qiub54f07e2017-03-07 14:56:15 -0800629 assertNull(mManager.matchProvider(createTestScanResult()));
Peter Qiu3d424022016-11-30 13:36:25 -0800630 }
631
632 /**
633 * Verify the expectations for sweepCache.
634 *
635 * @throws Exception
636 */
637 @Test
638 public void sweepCache() throws Exception {
639 mManager.sweepCache();
640 verify(mAnqpCache).sweep();
641 }
Peter Qiu99d4b812017-02-14 09:42:55 -0800642
643 /**
Peter Qiuec28f862017-03-07 14:25:01 -0800644 * Verify that an empty map will be returned if ANQP elements are not cached for the given AP.
645 *
646 * @throws Exception
647 */
648 @Test
649 public void getANQPElementsWithNoMatchFound() throws Exception {
650 when(mAnqpCache.getEntry(TEST_ANQP_KEY)).thenReturn(null);
651 assertTrue(mManager.getANQPElements(createTestScanResult()).isEmpty());
652 }
653
654 /**
655 * Verify that an expected ANQP elements will be returned if ANQP elements are cached for the
656 * given AP.
657 *
658 * @throws Exception
659 */
660 @Test
661 public void getANQPElementsWithMatchFound() throws Exception {
662 Map<ANQPElementType, ANQPElement> anqpElementMap = new HashMap<>();
663 anqpElementMap.put(ANQPElementType.ANQPDomName,
664 new DomainNameElement(Arrays.asList(new String[] {"test.com"})));
665 ANQPData entry = new ANQPData(mClock, anqpElementMap);
666
667 when(mAnqpCache.getEntry(TEST_ANQP_KEY)).thenReturn(entry);
668 assertEquals(anqpElementMap, mManager.getANQPElements(createTestScanResult()));
669 }
670
671 /**
Peter Qiu48c93da2017-03-09 13:28:47 -0800672 * Verify that an expected {@link WifiConfiguration} will be returned when a {@link ScanResult}
673 * is matched to a home provider.
674 *
675 * @throws Exception
676 */
677 @Test
678 public void getMatchingWifiConfigForHomeProviderAP() throws Exception {
Etan Cohend3fb3d82017-10-17 08:43:56 -0700679 PasspointProvider provider = addTestProvider(TEST_FQDN);
Peter Qiu48c93da2017-03-09 13:28:47 -0800680 ANQPData entry = new ANQPData(mClock, null);
681
682 when(mAnqpCache.getEntry(TEST_ANQP_KEY)).thenReturn(entry);
Masashi Honma2e889dc2018-04-02 17:09:51 +0900683 when(provider.match(anyMap(), any(RoamingConsortium.class)))
684 .thenReturn(PasspointMatch.HomeProvider);
Peter Qiu48c93da2017-03-09 13:28:47 -0800685 when(provider.getWifiConfig()).thenReturn(new WifiConfiguration());
686 WifiConfiguration config = mManager.getMatchingWifiConfig(createTestScanResult());
687 assertEquals(ScanResultUtil.createQuotedSSID(TEST_SSID), config.SSID);
Peter Qiud6ba42b2017-03-27 09:26:34 -0700688 assertTrue(config.isHomeProviderNetwork);
Peter Qiu48c93da2017-03-09 13:28:47 -0800689 }
690
691 /**
692 * Verify that an expected {@link WifiConfiguration} will be returned when a {@link ScanResult}
693 * is matched to a roaming provider.
694 *
695 * @throws Exception
696 */
697 @Test
698 public void getMatchingWifiConfigForRoamingProviderAP() throws Exception {
Etan Cohend3fb3d82017-10-17 08:43:56 -0700699 PasspointProvider provider = addTestProvider(TEST_FQDN);
Peter Qiu48c93da2017-03-09 13:28:47 -0800700 ANQPData entry = new ANQPData(mClock, null);
701
702 when(mAnqpCache.getEntry(TEST_ANQP_KEY)).thenReturn(entry);
Masashi Honma2e889dc2018-04-02 17:09:51 +0900703 when(provider.match(anyMap(), any(RoamingConsortium.class)))
704 .thenReturn(PasspointMatch.RoamingProvider);
Peter Qiu48c93da2017-03-09 13:28:47 -0800705 when(provider.getWifiConfig()).thenReturn(new WifiConfiguration());
706 WifiConfiguration config = mManager.getMatchingWifiConfig(createTestScanResult());
707 assertEquals(ScanResultUtil.createQuotedSSID(TEST_SSID), config.SSID);
Peter Qiud6ba42b2017-03-27 09:26:34 -0700708 assertFalse(config.isHomeProviderNetwork);
Peter Qiu48c93da2017-03-09 13:28:47 -0800709 }
710
711 /**
712 * Verify that a {code null} will be returned when a {@link ScanResult} doesn't match any
713 * provider.
714 *
715 * @throws Exception
716 */
717 @Test
718 public void getMatchingWifiConfigWithNoMatchingProvider() throws Exception {
Etan Cohend3fb3d82017-10-17 08:43:56 -0700719 PasspointProvider provider = addTestProvider(TEST_FQDN);
Peter Qiu48c93da2017-03-09 13:28:47 -0800720 ANQPData entry = new ANQPData(mClock, null);
721
722 when(mAnqpCache.getEntry(TEST_ANQP_KEY)).thenReturn(entry);
Masashi Honma2e889dc2018-04-02 17:09:51 +0900723 when(provider.match(anyMap(), any(RoamingConsortium.class)))
724 .thenReturn(PasspointMatch.None);
Peter Qiu48c93da2017-03-09 13:28:47 -0800725 assertNull(mManager.getMatchingWifiConfig(createTestScanResult()));
726 verify(provider, never()).getWifiConfig();
727 }
728
729 /**
Peter Qiu33c3ae82017-06-02 11:35:06 -0700730 * Verify that a {@code null} will be returned when trying to get a matching
731 * {@link WifiConfiguration} for a {@code null} {@link ScanResult}.
Peter Qiu48c93da2017-03-09 13:28:47 -0800732 *
733 * @throws Exception
734 */
735 @Test
736 public void getMatchingWifiConfigWithNullScanResult() throws Exception {
737 assertNull(mManager.getMatchingWifiConfig(null));
738 }
739
740 /**
Peter Qiu33c3ae82017-06-02 11:35:06 -0700741 * Verify that a {@code null} will be returned when trying to get a matching
742 * {@link WifiConfiguration} for a {@link ScanResult} with a {@code null} BSSID.
743 *
744 * @throws Exception
745 */
746 @Test
747 public void getMatchingWifiConfigWithNullBSSID() throws Exception {
748 ScanResult scanResult = createTestScanResult();
749 scanResult.BSSID = null;
750 assertNull(mManager.getMatchingWifiConfig(scanResult));
751 }
752
753 /**
754 * Verify that a {@code null} will be returned when trying to get a matching
755 * {@link WifiConfiguration} for a {@link ScanResult} with an invalid BSSID.
756 *
757 * @throws Exception
758 */
759 @Test
760 public void getMatchingWifiConfigWithInvalidBSSID() throws Exception {
761 ScanResult scanResult = createTestScanResult();
762 scanResult.BSSID = "asdfdasfas";
763 assertNull(mManager.getMatchingWifiConfig(scanResult));
764 }
765
766 /**
767 * Verify that a {@code null} will be returned when trying to get a matching
768 * {@link WifiConfiguration} for a non-Passpoint AP.
769 *
770 * @throws Exception
771 */
772 @Test
773 public void getMatchingWifiConfigForNonPasspointAP() throws Exception {
774 ScanResult scanResult = createTestScanResult();
775 scanResult.flags = 0;
776 assertNull(mManager.getMatchingWifiConfig(scanResult));
777 }
778
779 /**
Etan Cohend3fb3d82017-10-17 08:43:56 -0700780 * Verify that an expected set of {@link WifiConfiguration} will be returned when a
781 * {@link ScanResult} is matched to a provider.
782 *
783 * @throws Exception
784 */
785 @Test
786 public void getAllMatchingWifiConfigsForProviderAP() throws Exception {
787 PasspointProvider providerHome = addTestProvider(TEST_FQDN + 0);
788 PasspointProvider providerRoaming = addTestProvider(TEST_FQDN + 1);
789 PasspointProvider providerNone = addTestProvider(TEST_FQDN + 2);
790 ANQPData entry = new ANQPData(mClock, null);
791
792 when(mAnqpCache.getEntry(TEST_ANQP_KEY)).thenReturn(entry);
793
Masashi Honma2e889dc2018-04-02 17:09:51 +0900794 when(providerHome.match(anyMap(), any(RoamingConsortium.class)))
795 .thenReturn(PasspointMatch.HomeProvider);
796 when(providerRoaming.match(anyMap(), any(RoamingConsortium.class)))
797 .thenReturn(PasspointMatch.RoamingProvider);
798 when(providerNone.match(anyMap(), any(RoamingConsortium.class)))
799 .thenReturn(PasspointMatch.None);
Etan Cohend3fb3d82017-10-17 08:43:56 -0700800
801 when(providerHome.getWifiConfig()).thenReturn(new WifiConfiguration());
802 when(providerRoaming.getWifiConfig()).thenReturn(new WifiConfiguration());
803 when(providerNone.getWifiConfig()).thenReturn(new WifiConfiguration());
804
805 List<WifiConfiguration> configs = mManager.getAllMatchingWifiConfigs(
806 createTestScanResult());
807
808 assertEquals(2, configs.size());
809 int observedHome = 0;
810 int observedRoaming = 0;
811 for (WifiConfiguration config : configs) {
812 assertEquals(ScanResultUtil.createQuotedSSID(TEST_SSID), config.SSID);
813 if (config.isHomeProviderNetwork) {
814 observedHome++;
815 } else {
816 observedRoaming++;
817 }
818 }
819 assertEquals(1, observedHome);
820 assertEquals(1, observedRoaming);
821 }
822
823 /**
824 * Verify that an empty list will be returned when trying to get all matching
825 * {@link WifiConfiguration} for a {@code null} {@link ScanResult}.
826 *
827 * @throws Exception
828 */
829 @Test
830 public void getAllMatchingWifiConfigsWithNullScanResult() throws Exception {
831 assertEquals(0, mManager.getAllMatchingWifiConfigs(null).size());
832 }
833
834 /**
835 * Verify that an empty list will be returned when trying to get a all matching
836 * {@link WifiConfiguration} for a {@link ScanResult} with a {@code null} BSSID.
837 *
838 * @throws Exception
839 */
840 @Test
841 public void getAllMatchingWifiConfigWithNullBSSID() throws Exception {
842 ScanResult scanResult = createTestScanResult();
843 scanResult.BSSID = null;
844 assertEquals(0, mManager.getAllMatchingWifiConfigs(scanResult).size());
845 }
846
847 /**
848 * Verify that an empty list will be returned when trying to get all matching
849 * {@link WifiConfiguration} for a {@link ScanResult} with an invalid BSSID.
850 *
851 * @throws Exception
852 */
853 @Test
854 public void getAllMatchingWifiConfigWithInvalidBSSID() throws Exception {
855 ScanResult scanResult = createTestScanResult();
856 scanResult.BSSID = "asdfdasfas";
857 assertEquals(0, mManager.getAllMatchingWifiConfigs(scanResult).size());
858 }
859
860 /**
861 * Verify that an empty list will be returned when trying to get all matching
862 * {@link WifiConfiguration} for a non-Passpoint AP.
863 *
864 * @throws Exception
865 */
866 @Test
867 public void getAllMatchingWifiConfigForNonPasspointAP() throws Exception {
868 ScanResult scanResult = createTestScanResult();
869 scanResult.flags = 0;
870 assertEquals(0, mManager.getAllMatchingWifiConfigs(scanResult).size());
871 }
872
873 /**
Peter Qiuac4ab1b2017-06-06 13:59:04 -0700874 * Verify that an empty list will be returned when retrieving OSU providers for an AP with
875 * null scan result.
876 *
877 * @throws Exception
878 */
879 @Test
880 public void getMatchingOsuProvidersForNullScanResult() throws Exception {
881 assertTrue(mManager.getMatchingOsuProviders(null).isEmpty());
882 }
883
884 /**
885 * Verify that an empty list will be returned when retrieving OSU providers for an AP with
886 * invalid BSSID.
887 *
888 * @throws Exception
889 */
890 @Test
891 public void getMatchingOsuProvidersForInvalidBSSID() throws Exception {
892 ScanResult scanResult = createTestScanResult();
893 scanResult.BSSID = "asdfdasfas";
894 assertTrue(mManager.getMatchingOsuProviders(scanResult).isEmpty());
895 }
896
897 /**
898 * Verify that an empty list will be returned when retrieving OSU providers for a
899 * non-Passpoint AP.
900 *
901 * @throws Exception
902 */
903 @Test
904 public void getMatchingOsuProvidersForNonPasspointAP() throws Exception {
905 ScanResult scanResult = createTestScanResult();
906 scanResult.flags = 0;
907 assertTrue(mManager.getMatchingOsuProviders(scanResult).isEmpty());
908 }
909
910 /**
911 * Verify that an empty list will be returned when no match is found from the ANQP cache.
912 *
913 * @throws Exception
914 */
915 @Test
916 public void getMatchingOsuProviderWithNoMatch() throws Exception {
917 when(mAnqpCache.getEntry(TEST_ANQP_KEY)).thenReturn(null);
918 assertTrue(mManager.getMatchingOsuProviders(createTestScanResult()).isEmpty());
919 }
920
921 /**
922 * Verify that an expected provider list will be returned when a match is found from
923 * the ANQP cache.
924 *
925 * @throws Exception
926 */
927 @Test
928 public void getMatchingOsuProvidersWithMatch() throws Exception {
929 // Test data.
930 WifiSsid osuSsid = WifiSsid.createFromAsciiEncoded("Test SSID");
931 String friendlyName = "Test Provider";
932 String serviceDescription = "Dummy Service";
933 Uri serverUri = Uri.parse("https://test.com");
934 String nai = "access.test.com";
935 List<Integer> methodList = Arrays.asList(1);
936 List<I18Name> friendlyNames = Arrays.asList(
937 new I18Name(Locale.ENGLISH.getLanguage(), Locale.ENGLISH, friendlyName));
938 List<I18Name> serviceDescriptions = Arrays.asList(
939 new I18Name(Locale.ENGLISH.getLanguage(), Locale.ENGLISH, serviceDescription));
940
941 // Setup OSU providers ANQP element.
942 List<OsuProviderInfo> providerInfoList = new ArrayList<>();
943 providerInfoList.add(new OsuProviderInfo(
944 friendlyNames, serverUri, methodList, null, nai, serviceDescriptions));
945 Map<ANQPElementType, ANQPElement> anqpElementMap = new HashMap<>();
946 anqpElementMap.put(ANQPElementType.HSOSUProviders,
947 new HSOsuProvidersElement(osuSsid, providerInfoList));
948 ANQPData entry = new ANQPData(mClock, anqpElementMap);
949
950 // Setup expectation.
951 OsuProvider provider = new OsuProvider(
952 osuSsid, friendlyName, serviceDescription, serverUri, nai, methodList, null);
953 List<OsuProvider> expectedList = new ArrayList<>();
954 expectedList.add(provider);
955
956 when(mAnqpCache.getEntry(TEST_ANQP_KEY)).thenReturn(entry);
957 assertEquals(expectedList, mManager.getMatchingOsuProviders(createTestScanResult()));
958 }
959
960 /**
Peter Qiu99d4b812017-02-14 09:42:55 -0800961 * Verify that the provider list maintained by the PasspointManager after the list is updated
962 * in the data source.
963 *
964 * @throws Exception
965 */
966 @Test
967 public void verifyProvidersAfterDataSourceUpdate() throws Exception {
968 // Update the provider list in the data source.
Etan Cohend3fb3d82017-10-17 08:43:56 -0700969 PasspointConfiguration config = createTestConfigWithUserCredential(TEST_FQDN);
Peter Qiu99d4b812017-02-14 09:42:55 -0800970 PasspointProvider provider = createMockProvider(config);
971 List<PasspointProvider> providers = new ArrayList<>();
972 providers.add(provider);
973 mDataSource.setProviders(providers);
974
975 // Verify the providers maintained by PasspointManager.
976 assertEquals(1, mManager.getProviderConfigs().size());
977 assertEquals(config, mManager.getProviderConfigs().get(0));
978 }
979
980 /**
981 * Verify that the provider index used by PasspointManager is updated after it is updated in
982 * the data source.
983 *
984 * @throws Exception
985 */
986 @Test
987 public void verifyProviderIndexAfterDataSourceUpdate() throws Exception {
988 long providerIndex = 9;
989 mDataSource.setProviderIndex(providerIndex);
990 assertEquals(providerIndex, mDataSource.getProviderIndex());
991
992 // Add a provider.
Etan Cohend3fb3d82017-10-17 08:43:56 -0700993 PasspointConfiguration config = createTestConfigWithUserCredential(TEST_FQDN);
Peter Qiu99d4b812017-02-14 09:42:55 -0800994 PasspointProvider provider = createMockProvider(config);
995 // Verify the provider ID used to create the new provider.
996 when(mObjectFactory.makePasspointProvider(eq(config), eq(mWifiKeyStore),
Peter Qiu07816a42017-04-03 17:08:33 +0000997 eq(mSimAccessor), eq(providerIndex), eq(TEST_CREATOR_UID))).thenReturn(provider);
998 assertTrue(mManager.addOrUpdateProvider(config, TEST_CREATOR_UID));
Peter Qiu99d4b812017-02-14 09:42:55 -0800999 verifyInstalledConfig(config);
1000 verify(mWifiConfigManager).saveToStore(true);
1001 reset(mWifiConfigManager);
1002 }
Peter Qiu5e4822d2017-02-23 11:32:33 -08001003
1004 /**
1005 * Verify that a PasspointProvider with expected PasspointConfiguration will be installed when
1006 * adding a legacy Passpoint configuration containing a valid user credential.
1007 *
1008 * @throws Exception
1009 */
1010 @Test
1011 public void addLegacyPasspointConfigWithUserCredential() throws Exception {
1012 // Test data.
1013 String fqdn = "test.com";
1014 String friendlyName = "Friendly Name";
1015 long[] rcOIs = new long[] {0x1234L, 0x2345L};
1016 String realm = "realm.com";
1017 String username = "username";
1018 String password = "password";
1019 byte[] base64EncodedPw =
1020 Base64.encode(password.getBytes(StandardCharsets.UTF_8), Base64.DEFAULT);
1021 String encodedPasswordStr = new String(base64EncodedPw, StandardCharsets.UTF_8);
1022 String caCertificateAlias = "CaCert";
1023
1024 // Setup WifiConfiguration for legacy Passpoint configuraiton.
1025 WifiConfiguration wifiConfig = new WifiConfiguration();
1026 wifiConfig.FQDN = fqdn;
1027 wifiConfig.providerFriendlyName = friendlyName;
1028 wifiConfig.roamingConsortiumIds = rcOIs;
1029 wifiConfig.enterpriseConfig.setIdentity(username);
1030 wifiConfig.enterpriseConfig.setPassword(password);
1031 wifiConfig.enterpriseConfig.setRealm(realm);
1032 wifiConfig.enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.TTLS);
1033 wifiConfig.enterpriseConfig.setPhase2Method(WifiEnterpriseConfig.Phase2.PAP);
1034 wifiConfig.enterpriseConfig.setCaCertificateAlias(caCertificateAlias);
1035
1036 // Setup expected {@link PasspointConfiguration}
1037 PasspointConfiguration passpointConfig = new PasspointConfiguration();
1038 HomeSp homeSp = new HomeSp();
1039 homeSp.setFqdn(fqdn);
1040 homeSp.setFriendlyName(friendlyName);
1041 homeSp.setRoamingConsortiumOis(rcOIs);
1042 passpointConfig.setHomeSp(homeSp);
1043 Credential credential = new Credential();
1044 Credential.UserCredential userCredential = new Credential.UserCredential();
1045 userCredential.setUsername(username);
1046 userCredential.setPassword(encodedPasswordStr);
1047 userCredential.setEapType(EAPConstants.EAP_TTLS);
1048 userCredential.setNonEapInnerMethod("PAP");
1049 credential.setUserCredential(userCredential);
1050 credential.setRealm(realm);
1051 passpointConfig.setCredential(credential);
1052
1053 assertTrue(PasspointManager.addLegacyPasspointConfig(wifiConfig));
1054 verifyInstalledConfig(passpointConfig);
1055 }
1056
1057 /**
1058 * Verify that adding a legacy Passpoint configuration containing user credential will
1059 * fail when client certificate is not provided.
1060 *
1061 * @throws Exception
1062 */
1063 @Test
1064 public void addLegacyPasspointConfigWithUserCredentialWithoutCaCert() throws Exception {
1065 // Test data.
1066 String fqdn = "test.com";
1067 String friendlyName = "Friendly Name";
1068 long[] rcOIs = new long[] {0x1234L, 0x2345L};
1069 String realm = "realm.com";
1070 String username = "username";
1071 String password = "password";
1072 byte[] base64EncodedPw =
1073 Base64.encode(password.getBytes(StandardCharsets.UTF_8), Base64.DEFAULT);
1074 String encodedPasswordStr = new String(base64EncodedPw, StandardCharsets.UTF_8);
1075
1076 // Setup WifiConfiguration for legacy Passpoint configuraiton.
1077 WifiConfiguration wifiConfig = new WifiConfiguration();
1078 wifiConfig.FQDN = fqdn;
1079 wifiConfig.providerFriendlyName = friendlyName;
1080 wifiConfig.roamingConsortiumIds = rcOIs;
1081 wifiConfig.enterpriseConfig.setIdentity(username);
1082 wifiConfig.enterpriseConfig.setPassword(password);
1083 wifiConfig.enterpriseConfig.setRealm(realm);
1084 wifiConfig.enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.TTLS);
1085 wifiConfig.enterpriseConfig.setPhase2Method(WifiEnterpriseConfig.Phase2.PAP);
1086
1087 assertFalse(PasspointManager.addLegacyPasspointConfig(wifiConfig));
1088 }
1089
1090 /**
1091 * Verify that a PasspointProvider with expected PasspointConfiguration will be installed when
1092 * adding a legacy Passpoint configuration containing a valid SIM credential.
1093 *
1094 * @throws Exception
1095 */
1096 @Test
1097 public void addLegacyPasspointConfigWithSimCredential() throws Exception {
1098 // Test data.
1099 String fqdn = "test.com";
1100 String friendlyName = "Friendly Name";
1101 long[] rcOIs = new long[] {0x1234L, 0x2345L};
1102 String realm = "realm.com";
1103 String imsi = "1234";
1104
1105 // Setup WifiConfiguration for legacy Passpoint configuraiton.
1106 WifiConfiguration wifiConfig = new WifiConfiguration();
1107 wifiConfig.FQDN = fqdn;
1108 wifiConfig.providerFriendlyName = friendlyName;
1109 wifiConfig.roamingConsortiumIds = rcOIs;
1110 wifiConfig.enterpriseConfig.setRealm(realm);
1111 wifiConfig.enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.SIM);
1112 wifiConfig.enterpriseConfig.setPlmn(imsi);
1113
1114 // Setup expected {@link PasspointConfiguration}
1115 PasspointConfiguration passpointConfig = new PasspointConfiguration();
1116 HomeSp homeSp = new HomeSp();
1117 homeSp.setFqdn(fqdn);
1118 homeSp.setFriendlyName(friendlyName);
1119 homeSp.setRoamingConsortiumOis(rcOIs);
1120 passpointConfig.setHomeSp(homeSp);
1121 Credential credential = new Credential();
1122 Credential.SimCredential simCredential = new Credential.SimCredential();
1123 simCredential.setEapType(EAPConstants.EAP_SIM);
1124 simCredential.setImsi(imsi);
1125 credential.setSimCredential(simCredential);
1126 credential.setRealm(realm);
1127 passpointConfig.setCredential(credential);
1128
1129 assertTrue(PasspointManager.addLegacyPasspointConfig(wifiConfig));
1130 verifyInstalledConfig(passpointConfig);
1131 }
1132
1133 /**
1134 * Verify that a PasspointProvider with expected PasspointConfiguration will be installed when
1135 * adding a legacy Passpoint configuration containing a valid certificate credential.
1136 *
1137 * @throws Exception
1138 */
1139 @Test
1140 public void addLegacyPasspointConfigWithCertCredential() throws Exception {
1141 // Test data.
1142 String fqdn = "test.com";
1143 String friendlyName = "Friendly Name";
1144 long[] rcOIs = new long[] {0x1234L, 0x2345L};
1145 String realm = "realm.com";
1146 String caCertificateAlias = "CaCert";
1147 String clientCertificateAlias = "ClientCert";
1148
1149 // Setup WifiConfiguration for legacy Passpoint configuraiton.
1150 WifiConfiguration wifiConfig = new WifiConfiguration();
1151 wifiConfig.FQDN = fqdn;
1152 wifiConfig.providerFriendlyName = friendlyName;
1153 wifiConfig.roamingConsortiumIds = rcOIs;
1154 wifiConfig.enterpriseConfig.setRealm(realm);
1155 wifiConfig.enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.TLS);
1156 wifiConfig.enterpriseConfig.setCaCertificateAlias(caCertificateAlias);
1157 wifiConfig.enterpriseConfig.setClientCertificateAlias(clientCertificateAlias);
1158
1159 // Setup expected {@link PasspointConfiguration}
1160 PasspointConfiguration passpointConfig = new PasspointConfiguration();
1161 HomeSp homeSp = new HomeSp();
1162 homeSp.setFqdn(fqdn);
1163 homeSp.setFriendlyName(friendlyName);
1164 homeSp.setRoamingConsortiumOis(rcOIs);
1165 passpointConfig.setHomeSp(homeSp);
1166 Credential credential = new Credential();
1167 Credential.CertificateCredential certCredential = new Credential.CertificateCredential();
1168 certCredential.setCertType(Credential.CertificateCredential.CERT_TYPE_X509V3);
1169 credential.setCertCredential(certCredential);
1170 credential.setRealm(realm);
1171 passpointConfig.setCredential(credential);
1172
1173 assertTrue(PasspointManager.addLegacyPasspointConfig(wifiConfig));
1174 verifyInstalledConfig(passpointConfig);
1175 }
1176
1177 /**
1178 * Verify that adding a legacy Passpoint configuration containing certificate credential will
1179 * fail when CA certificate is not provided.
1180 *
1181 * @throws Exception
1182 */
1183 @Test
1184 public void addLegacyPasspointConfigWithCertCredentialWithoutCaCert() throws Exception {
1185 // Test data.
1186 String fqdn = "test.com";
1187 String friendlyName = "Friendly Name";
1188 long[] rcOIs = new long[] {0x1234L, 0x2345L};
1189 String realm = "realm.com";
1190 String clientCertificateAlias = "ClientCert";
1191
1192 // Setup WifiConfiguration for legacy Passpoint configuraiton.
1193 WifiConfiguration wifiConfig = new WifiConfiguration();
1194 wifiConfig.FQDN = fqdn;
1195 wifiConfig.providerFriendlyName = friendlyName;
1196 wifiConfig.roamingConsortiumIds = rcOIs;
1197 wifiConfig.enterpriseConfig.setRealm(realm);
1198 wifiConfig.enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.TLS);
1199 wifiConfig.enterpriseConfig.setClientCertificateAlias(clientCertificateAlias);
1200
1201 assertFalse(PasspointManager.addLegacyPasspointConfig(wifiConfig));
1202 }
1203
1204 /**
1205 * Verify that adding a legacy Passpoint configuration containing certificate credential will
1206 * fail when client certificate is not provided.
1207 *
1208 * @throws Exception
1209 */
1210 @Test
1211 public void addLegacyPasspointConfigWithCertCredentialWithoutClientCert() throws Exception {
1212 // Test data.
1213 String fqdn = "test.com";
1214 String friendlyName = "Friendly Name";
1215 long[] rcOIs = new long[] {0x1234L, 0x2345L};
1216 String realm = "realm.com";
1217 String caCertificateAlias = "CaCert";
1218
1219 // Setup WifiConfiguration for legacy Passpoint configuraiton.
1220 WifiConfiguration wifiConfig = new WifiConfiguration();
1221 wifiConfig.FQDN = fqdn;
1222 wifiConfig.providerFriendlyName = friendlyName;
1223 wifiConfig.roamingConsortiumIds = rcOIs;
1224 wifiConfig.enterpriseConfig.setRealm(realm);
1225 wifiConfig.enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.TLS);
1226 wifiConfig.enterpriseConfig.setCaCertificateAlias(caCertificateAlias);
1227
1228 assertFalse(PasspointManager.addLegacyPasspointConfig(wifiConfig));
1229 }
Peter Qiu49357a32017-06-22 15:48:19 -07001230
1231 /**
1232 * Verify that the provider's "hasEverConnected" flag will be set to true and the associated
1233 * metric is updated after the provider was used to successfully connect to a Passpoint
1234 * network for the first time.
1235 *
1236 * @throws Exception
1237 */
1238 @Test
1239 public void providerNetworkConnectedFirstTime() throws Exception {
Etan Cohend3fb3d82017-10-17 08:43:56 -07001240 PasspointProvider provider = addTestProvider(TEST_FQDN);
Peter Qiu49357a32017-06-22 15:48:19 -07001241 when(provider.getHasEverConnected()).thenReturn(false);
1242 mManager.onPasspointNetworkConnected(TEST_FQDN);
1243 verify(provider).setHasEverConnected(eq(true));
1244 }
1245
1246 /**
1247 * Verify that the provider's "hasEverConnected" flag the associated metric is not updated
1248 * after the provider was used to successfully connect to a Passpoint network for non-first
1249 * time.
1250 *
1251 * @throws Exception
1252 */
1253 @Test
1254 public void providerNetworkConnectedNotFirstTime() throws Exception {
Etan Cohend3fb3d82017-10-17 08:43:56 -07001255 PasspointProvider provider = addTestProvider(TEST_FQDN);
Peter Qiu49357a32017-06-22 15:48:19 -07001256 when(provider.getHasEverConnected()).thenReturn(true);
1257 mManager.onPasspointNetworkConnected(TEST_FQDN);
1258 verify(provider, never()).setHasEverConnected(anyBoolean());
1259 }
1260
1261 /**
1262 * Verify that the expected Passpoint metrics are updated when
1263 * {@link PasspointManager#updateMetrics} is invoked.
1264 *
1265 * @throws Exception
1266 */
1267 @Test
1268 public void updateMetrics() throws Exception {
Etan Cohend3fb3d82017-10-17 08:43:56 -07001269 PasspointProvider provider = addTestProvider(TEST_FQDN);
Peter Qiu49357a32017-06-22 15:48:19 -07001270
1271 // Provider have not provided a successful network connection.
1272 int expectedInstalledProviders = 1;
1273 int expectedConnectedProviders = 0;
1274 when(provider.getHasEverConnected()).thenReturn(false);
1275 mManager.updateMetrics();
1276 verify(mWifiMetrics).updateSavedPasspointProfiles(
1277 eq(expectedInstalledProviders), eq(expectedConnectedProviders));
1278 reset(provider);
1279 reset(mWifiMetrics);
1280
1281 // Provider have provided a successful network connection.
1282 expectedConnectedProviders = 1;
1283 when(provider.getHasEverConnected()).thenReturn(true);
1284 mManager.updateMetrics();
1285 verify(mWifiMetrics).updateSavedPasspointProfiles(
1286 eq(expectedInstalledProviders), eq(expectedConnectedProviders));
1287 }
Sohani Rao0ddc2ba2017-10-09 14:36:48 -07001288 /**
1289 * Verify Passpoint Manager's provisioning APIs by invoking methods in PasspointProvisioner for
1290 * initiailization and provisioning a provider.
1291 */
1292 @Test
1293 public void verifyPasspointProvisioner() {
1294 mManager.initializeProvisioner(mLooper.getLooper());
1295 verify(mPasspointProvisioner).init(any(Looper.class));
1296 when(mPasspointProvisioner.startSubscriptionProvisioning(anyInt(), any(OsuProvider.class),
1297 any(IProvisioningCallback.class))).thenReturn(true);
1298 OsuProvider osuProvider = PasspointProvisioningTestUtil.generateOsuProvider(true);
1299 assertEquals(true,
1300 mManager.startSubscriptionProvisioning(TEST_UID, osuProvider, mCallback));
1301 }
Peter Qiueaa6ff22016-09-12 10:00:13 -07001302}