1 /* 2 * Copyright (C) 2017 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 package com.android.server.wifi; 17 18 import static org.junit.Assert.*; 19 import static org.mockito.Matchers.any; 20 import static org.mockito.Matchers.anyLong; 21 import static org.mockito.Matchers.anyString; 22 import static org.mockito.Matchers.eq; 23 import static org.mockito.Mockito.*; 24 25 import android.content.Context; 26 import android.hardware.wifi.hostapd.V1_0.HostapdStatus; 27 import android.hardware.wifi.hostapd.V1_0.HostapdStatusCode; 28 import android.hardware.wifi.hostapd.V1_0.IHostapd; 29 import android.hidl.manager.V1_0.IServiceManager; 30 import android.hidl.manager.V1_0.IServiceNotification; 31 import android.net.wifi.WifiConfiguration; 32 import android.os.IHwBinder; 33 import android.os.RemoteException; 34 35 import com.android.internal.R; 36 import com.android.server.wifi.util.NativeUtil; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.mockito.ArgumentCaptor; 41 import org.mockito.InOrder; 42 import org.mockito.Mock; 43 import org.mockito.MockitoAnnotations; 44 45 /** 46 * Unit tests for HostapdHal 47 */ 48 public class HostapdHalTest { 49 private static final String IFACE_NAME = "mock-wlan0"; 50 private static final String NETWORK_SSID = "test-ssid"; 51 private static final String NETWORK_PSK = "test-psk"; 52 53 private @Mock Context mContext; 54 private @Mock IServiceManager mServiceManagerMock; 55 private @Mock IHostapd mIHostapdMock; 56 private @Mock WifiNative.HostapdDeathEventHandler mHostapdHalDeathHandler; 57 private MockResources mResources; 58 HostapdStatus mStatusSuccess; 59 HostapdStatus mStatusFailure; 60 private HostapdHal mHostapdHal; 61 private ArgumentCaptor<IHwBinder.DeathRecipient> mServiceManagerDeathCaptor = 62 ArgumentCaptor.forClass(IHwBinder.DeathRecipient.class); 63 private ArgumentCaptor<IHwBinder.DeathRecipient> mHostapdDeathCaptor = 64 ArgumentCaptor.forClass(IHwBinder.DeathRecipient.class); 65 private ArgumentCaptor<IServiceNotification.Stub> mServiceNotificationCaptor = 66 ArgumentCaptor.forClass(IServiceNotification.Stub.class); 67 private ArgumentCaptor<IHostapd.IfaceParams> mIfaceParamsCaptor = 68 ArgumentCaptor.forClass(IHostapd.IfaceParams.class); 69 private ArgumentCaptor<IHostapd.NetworkParams> mNetworkParamsCaptor = 70 ArgumentCaptor.forClass(IHostapd.NetworkParams.class); 71 private InOrder mInOrder; 72 73 private class HostapdHalSpy extends HostapdHal { 74 HostapdHalSpy() { 75 super(mContext); 76 } 77 78 @Override 79 protected IServiceManager getServiceManagerMockable() throws RemoteException { 80 return mServiceManagerMock; 81 } 82 83 @Override 84 protected IHostapd getHostapdMockable() throws RemoteException { 85 return mIHostapdMock; 86 } 87 } 88 89 @Before 90 public void setUp() throws Exception { 91 MockitoAnnotations.initMocks(this); 92 mResources = new MockResources(); 93 mResources.setBoolean(R.bool.config_wifi_softap_acs_supported, false); 94 mResources.setBoolean(R.bool.config_wifi_softap_ieee80211ac_supported, false); 95 96 mStatusSuccess = createHostapdStatus(HostapdStatusCode.SUCCESS); 97 mStatusFailure = createHostapdStatus(HostapdStatusCode.FAILURE_UNKNOWN); 98 99 when(mContext.getResources()).thenReturn(mResources); 100 when(mServiceManagerMock.linkToDeath(any(IHwBinder.DeathRecipient.class), 101 anyLong())).thenReturn(true); 102 when(mServiceManagerMock.registerForNotifications(anyString(), anyString(), 103 any(IServiceNotification.Stub.class))).thenReturn(true); 104 when(mIHostapdMock.linkToDeath(any(IHwBinder.DeathRecipient.class), 105 anyLong())).thenReturn(true); 106 when(mIHostapdMock.linkToDeath(any(IHwBinder.DeathRecipient.class), 107 anyLong())).thenReturn(true); 108 when(mIHostapdMock.addAccessPoint( 109 mIfaceParamsCaptor.capture(), mNetworkParamsCaptor.capture())) 110 .thenReturn(mStatusSuccess); 111 when(mIHostapdMock.removeAccessPoint(any())).thenReturn(mStatusSuccess); 112 mHostapdHal = new HostapdHalSpy(); 113 } 114 115 /** 116 * Sunny day scenario for HostapdHal initialization 117 * Asserts successful initialization 118 */ 119 @Test 120 public void testInitialize_success() throws Exception { 121 executeAndValidateInitializationSequence(false, false); 122 } 123 124 /** 125 * Failure scenario for HostapdHal initialization 126 */ 127 @Test 128 public void testInitialize_registerException() throws Exception { 129 executeAndValidateInitializationSequence(true, false); 130 } 131 132 /** 133 * Failure scenario for HostapdHal initialization 134 */ 135 @Test 136 public void testInitialize_registerFailure() throws Exception { 137 executeAndValidateInitializationSequence(false, true); 138 } 139 140 /** 141 * Verifies the hostapd death handling. 142 */ 143 @Test 144 public void testDeathHandling() throws Exception { 145 executeAndValidateInitializationSequence(); 146 147 mHostapdHal.registerDeathHandler(mHostapdHalDeathHandler); 148 mHostapdDeathCaptor.getValue().serviceDied(0); 149 verify(mHostapdHalDeathHandler).onDeath(); 150 } 151 152 /** 153 * Verifies the successful addition of access point. 154 */ 155 @Test 156 public void testAddAccessPointSuccess_Psk_Band2G() throws Exception { 157 executeAndValidateInitializationSequence(); 158 final int apChannel = 6; 159 160 WifiConfiguration configuration = new WifiConfiguration(); 161 configuration.SSID = NETWORK_SSID; 162 configuration.hiddenSSID = false; 163 configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK); 164 configuration.preSharedKey = NETWORK_PSK; 165 configuration.apChannel = apChannel; 166 configuration.apBand = WifiConfiguration.AP_BAND_2GHZ; 167 168 assertTrue(mHostapdHal.addAccessPoint(IFACE_NAME, configuration)); 169 verify(mIHostapdMock).addAccessPoint(any(), any()); 170 171 assertEquals(IFACE_NAME, mIfaceParamsCaptor.getValue().ifaceName); 172 assertTrue(mIfaceParamsCaptor.getValue().hwModeParams.enable80211N); 173 assertFalse(mIfaceParamsCaptor.getValue().hwModeParams.enable80211AC); 174 assertEquals(IHostapd.Band.BAND_2_4_GHZ, mIfaceParamsCaptor.getValue().channelParams.band); 175 assertFalse(mIfaceParamsCaptor.getValue().channelParams.enableAcs); 176 assertFalse(mIfaceParamsCaptor.getValue().channelParams.acsShouldExcludeDfs); 177 assertEquals(apChannel, mIfaceParamsCaptor.getValue().channelParams.channel); 178 179 assertEquals(NativeUtil.stringToByteArrayList(NETWORK_SSID), 180 mNetworkParamsCaptor.getValue().ssid); 181 assertFalse(mNetworkParamsCaptor.getValue().isHidden); 182 assertEquals(IHostapd.EncryptionType.WPA2, mNetworkParamsCaptor.getValue().encryptionType); 183 assertEquals(NETWORK_PSK, mNetworkParamsCaptor.getValue().pskPassphrase); 184 } 185 186 /** 187 * Verifies the successful addition of access point. 188 */ 189 @Test 190 public void testAddAccessPointSuccess_Open_Band5G() throws Exception { 191 executeAndValidateInitializationSequence(); 192 final int apChannel = 18; 193 194 WifiConfiguration configuration = new WifiConfiguration(); 195 configuration.SSID = NETWORK_SSID; 196 configuration.hiddenSSID = true; 197 configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 198 configuration.apChannel = apChannel; 199 configuration.apBand = WifiConfiguration.AP_BAND_5GHZ; 200 201 assertTrue(mHostapdHal.addAccessPoint(IFACE_NAME, configuration)); 202 verify(mIHostapdMock).addAccessPoint(any(), any()); 203 204 assertEquals(IFACE_NAME, mIfaceParamsCaptor.getValue().ifaceName); 205 assertTrue(mIfaceParamsCaptor.getValue().hwModeParams.enable80211N); 206 assertFalse(mIfaceParamsCaptor.getValue().hwModeParams.enable80211AC); 207 assertEquals(IHostapd.Band.BAND_5_GHZ, mIfaceParamsCaptor.getValue().channelParams.band); 208 assertFalse(mIfaceParamsCaptor.getValue().channelParams.enableAcs); 209 assertFalse(mIfaceParamsCaptor.getValue().channelParams.acsShouldExcludeDfs); 210 assertEquals(apChannel, mIfaceParamsCaptor.getValue().channelParams.channel); 211 212 assertEquals(NativeUtil.stringToByteArrayList(NETWORK_SSID), 213 mNetworkParamsCaptor.getValue().ssid); 214 assertTrue(mNetworkParamsCaptor.getValue().isHidden); 215 assertEquals(IHostapd.EncryptionType.NONE, mNetworkParamsCaptor.getValue().encryptionType); 216 assertNotNull(mNetworkParamsCaptor.getValue().pskPassphrase); 217 } 218 219 /** 220 * Verifies the successful addition of access point. 221 */ 222 @Test 223 public void testAddAccessPointSuccess_Psk_Band5G_Hidden() throws Exception { 224 executeAndValidateInitializationSequence(); 225 final int apChannel = 18; 226 227 WifiConfiguration configuration = new WifiConfiguration(); 228 configuration.SSID = NETWORK_SSID; 229 configuration.hiddenSSID = true; 230 configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK); 231 configuration.preSharedKey = NETWORK_PSK; 232 configuration.apChannel = apChannel; 233 configuration.apBand = WifiConfiguration.AP_BAND_5GHZ; 234 235 assertTrue(mHostapdHal.addAccessPoint(IFACE_NAME, configuration)); 236 verify(mIHostapdMock).addAccessPoint(any(), any()); 237 238 assertEquals(IFACE_NAME, mIfaceParamsCaptor.getValue().ifaceName); 239 assertTrue(mIfaceParamsCaptor.getValue().hwModeParams.enable80211N); 240 assertFalse(mIfaceParamsCaptor.getValue().hwModeParams.enable80211AC); 241 assertEquals(IHostapd.Band.BAND_5_GHZ, mIfaceParamsCaptor.getValue().channelParams.band); 242 assertFalse(mIfaceParamsCaptor.getValue().channelParams.enableAcs); 243 assertFalse(mIfaceParamsCaptor.getValue().channelParams.acsShouldExcludeDfs); 244 assertEquals(apChannel, mIfaceParamsCaptor.getValue().channelParams.channel); 245 246 assertEquals(NativeUtil.stringToByteArrayList(NETWORK_SSID), 247 mNetworkParamsCaptor.getValue().ssid); 248 assertTrue(mNetworkParamsCaptor.getValue().isHidden); 249 assertEquals(IHostapd.EncryptionType.WPA2, mNetworkParamsCaptor.getValue().encryptionType); 250 assertEquals(NETWORK_PSK, mNetworkParamsCaptor.getValue().pskPassphrase); 251 } 252 253 /** 254 * Verifies the successful addition of access point. 255 */ 256 @Test 257 public void testAddAccessPointSuccess_Psk_Band2G_WithACS() throws Exception { 258 // Enable ACS in the config. 259 mResources.setBoolean(R.bool.config_wifi_softap_acs_supported, true); 260 mHostapdHal = new HostapdHalSpy(); 261 262 executeAndValidateInitializationSequence(); 263 final int apChannel = 6; 264 265 WifiConfiguration configuration = new WifiConfiguration(); 266 configuration.SSID = NETWORK_SSID; 267 configuration.hiddenSSID = false; 268 configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK); 269 configuration.preSharedKey = NETWORK_PSK; 270 configuration.apChannel = apChannel; 271 configuration.apBand = WifiConfiguration.AP_BAND_2GHZ; 272 273 assertTrue(mHostapdHal.addAccessPoint(IFACE_NAME, configuration)); 274 verify(mIHostapdMock).addAccessPoint(any(), any()); 275 276 assertEquals(IFACE_NAME, mIfaceParamsCaptor.getValue().ifaceName); 277 assertTrue(mIfaceParamsCaptor.getValue().hwModeParams.enable80211N); 278 assertFalse(mIfaceParamsCaptor.getValue().hwModeParams.enable80211AC); 279 assertEquals(IHostapd.Band.BAND_2_4_GHZ, mIfaceParamsCaptor.getValue().channelParams.band); 280 assertTrue(mIfaceParamsCaptor.getValue().channelParams.enableAcs); 281 assertTrue(mIfaceParamsCaptor.getValue().channelParams.acsShouldExcludeDfs); 282 283 assertEquals(NativeUtil.stringToByteArrayList(NETWORK_SSID), 284 mNetworkParamsCaptor.getValue().ssid); 285 assertFalse(mNetworkParamsCaptor.getValue().isHidden); 286 assertEquals(IHostapd.EncryptionType.WPA2, mNetworkParamsCaptor.getValue().encryptionType); 287 assertEquals(NETWORK_PSK, mNetworkParamsCaptor.getValue().pskPassphrase); 288 } 289 290 /** 291 * Verifies the successful addition of access point. 292 */ 293 @Test 294 public void testAddAccessPointSuccess_Psk_Band2G_WithIeee80211AC() throws Exception { 295 // Enable ACS & 80211AC in the config. 296 mResources.setBoolean(R.bool.config_wifi_softap_acs_supported, true); 297 mResources.setBoolean(R.bool.config_wifi_softap_ieee80211ac_supported, true); 298 mHostapdHal = new HostapdHalSpy(); 299 300 executeAndValidateInitializationSequence(); 301 final int apChannel = 6; 302 303 WifiConfiguration configuration = new WifiConfiguration(); 304 configuration.SSID = NETWORK_SSID; 305 configuration.hiddenSSID = false; 306 configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK); 307 configuration.preSharedKey = NETWORK_PSK; 308 configuration.apChannel = apChannel; 309 configuration.apBand = WifiConfiguration.AP_BAND_2GHZ; 310 311 assertTrue(mHostapdHal.addAccessPoint(IFACE_NAME, configuration)); 312 verify(mIHostapdMock).addAccessPoint(any(), any()); 313 314 assertEquals(IFACE_NAME, mIfaceParamsCaptor.getValue().ifaceName); 315 assertTrue(mIfaceParamsCaptor.getValue().hwModeParams.enable80211N); 316 assertTrue(mIfaceParamsCaptor.getValue().hwModeParams.enable80211AC); 317 assertEquals(IHostapd.Band.BAND_2_4_GHZ, mIfaceParamsCaptor.getValue().channelParams.band); 318 assertTrue(mIfaceParamsCaptor.getValue().channelParams.enableAcs); 319 assertTrue(mIfaceParamsCaptor.getValue().channelParams.acsShouldExcludeDfs); 320 321 assertEquals(NativeUtil.stringToByteArrayList(NETWORK_SSID), 322 mNetworkParamsCaptor.getValue().ssid); 323 assertFalse(mNetworkParamsCaptor.getValue().isHidden); 324 assertEquals(IHostapd.EncryptionType.WPA2, mNetworkParamsCaptor.getValue().encryptionType); 325 assertEquals(NETWORK_PSK, mNetworkParamsCaptor.getValue().pskPassphrase); 326 } 327 328 /** 329 * Verifies the successful addition of access point. 330 */ 331 @Test 332 public void testAddAccessPointSuccess_Psk_BandAny_WithACS() throws Exception { 333 // Enable ACS in the config. 334 mResources.setBoolean(R.bool.config_wifi_softap_acs_supported, true); 335 mHostapdHal = new HostapdHalSpy(); 336 337 executeAndValidateInitializationSequence(); 338 final int apChannel = 6; 339 340 WifiConfiguration configuration = new WifiConfiguration(); 341 configuration.SSID = NETWORK_SSID; 342 configuration.hiddenSSID = false; 343 configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK); 344 configuration.preSharedKey = NETWORK_PSK; 345 configuration.apChannel = apChannel; 346 configuration.apBand = WifiConfiguration.AP_BAND_ANY; 347 348 assertTrue(mHostapdHal.addAccessPoint(IFACE_NAME, configuration)); 349 verify(mIHostapdMock).addAccessPoint(any(), any()); 350 351 assertEquals(IFACE_NAME, mIfaceParamsCaptor.getValue().ifaceName); 352 assertTrue(mIfaceParamsCaptor.getValue().hwModeParams.enable80211N); 353 assertFalse(mIfaceParamsCaptor.getValue().hwModeParams.enable80211AC); 354 assertEquals(IHostapd.Band.BAND_ANY, mIfaceParamsCaptor.getValue().channelParams.band); 355 assertTrue(mIfaceParamsCaptor.getValue().channelParams.enableAcs); 356 assertTrue(mIfaceParamsCaptor.getValue().channelParams.acsShouldExcludeDfs); 357 358 assertEquals(NativeUtil.stringToByteArrayList(NETWORK_SSID), 359 mNetworkParamsCaptor.getValue().ssid); 360 assertFalse(mNetworkParamsCaptor.getValue().isHidden); 361 assertEquals(IHostapd.EncryptionType.WPA2, mNetworkParamsCaptor.getValue().encryptionType); 362 assertEquals(NETWORK_PSK, mNetworkParamsCaptor.getValue().pskPassphrase); 363 } 364 365 /** 366 * Verifies the successful addition of access point. 367 * Verifies that BAND_ANY is downgraded to 2.4GHz if ACS is disabled. 368 */ 369 @Test 370 public void testAddAccessPointSuccess_Psk_BandAny_Downgraded_WithoutACS() throws Exception { 371 // Disable ACS in the config. 372 mResources.setBoolean(R.bool.config_wifi_softap_acs_supported, false); 373 mHostapdHal = new HostapdHalSpy(); 374 375 executeAndValidateInitializationSequence(); 376 final int apChannel = 6; 377 378 WifiConfiguration configuration = new WifiConfiguration(); 379 configuration.SSID = NETWORK_SSID; 380 configuration.hiddenSSID = false; 381 configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK); 382 configuration.preSharedKey = NETWORK_PSK; 383 configuration.apChannel = apChannel; 384 configuration.apBand = WifiConfiguration.AP_BAND_ANY; 385 386 assertTrue(mHostapdHal.addAccessPoint(IFACE_NAME, configuration)); 387 verify(mIHostapdMock).addAccessPoint(any(), any()); 388 389 assertEquals(IFACE_NAME, mIfaceParamsCaptor.getValue().ifaceName); 390 assertTrue(mIfaceParamsCaptor.getValue().hwModeParams.enable80211N); 391 assertFalse(mIfaceParamsCaptor.getValue().hwModeParams.enable80211AC); 392 // Verify the band is downgraded to 2.4GHz. 393 assertEquals(IHostapd.Band.BAND_2_4_GHZ, 394 mIfaceParamsCaptor.getValue().channelParams.band); 395 assertFalse(mIfaceParamsCaptor.getValue().channelParams.enableAcs); 396 397 assertEquals(NativeUtil.stringToByteArrayList(NETWORK_SSID), 398 mNetworkParamsCaptor.getValue().ssid); 399 assertFalse(mNetworkParamsCaptor.getValue().isHidden); 400 assertEquals(IHostapd.EncryptionType.WPA2, mNetworkParamsCaptor.getValue().encryptionType); 401 assertEquals(NETWORK_PSK, mNetworkParamsCaptor.getValue().pskPassphrase); 402 } 403 404 /** 405 * Verifies the failure handling in addition of access point with an invalid band. 406 */ 407 @Test 408 public void testAddAccessPointInvalidBandFailure() throws Exception { 409 executeAndValidateInitializationSequence(); 410 411 WifiConfiguration configuration = new WifiConfiguration(); 412 configuration.SSID = NETWORK_SSID; 413 configuration.hiddenSSID = true; 414 configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 415 configuration.apBand = WifiConfiguration.AP_BAND_5GHZ + 1; 416 417 assertFalse(mHostapdHal.addAccessPoint(IFACE_NAME, configuration)); 418 verify(mIHostapdMock, never()).addAccessPoint(any(), any()); 419 } 420 421 /** 422 * Verifies the failure handling in addition of access point. 423 */ 424 @Test 425 public void testAddAccessPointFailure() throws Exception { 426 executeAndValidateInitializationSequence(); 427 when(mIHostapdMock.addAccessPoint(any(), any())).thenReturn(mStatusFailure); 428 429 WifiConfiguration configuration = new WifiConfiguration(); 430 configuration.SSID = NETWORK_SSID; 431 configuration.hiddenSSID = true; 432 configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 433 configuration.apChannel = 6; 434 configuration.apBand = WifiConfiguration.AP_BAND_2GHZ; 435 436 assertFalse(mHostapdHal.addAccessPoint(IFACE_NAME, configuration)); 437 verify(mIHostapdMock).addAccessPoint(any(), any()); 438 } 439 440 /** 441 * Verifies the failure handling in addition of access point. 442 */ 443 @Test 444 public void testAddAccessPointRemoteException() throws Exception { 445 executeAndValidateInitializationSequence(); 446 doThrow(new RemoteException()).when(mIHostapdMock).addAccessPoint(any(), any()); 447 448 WifiConfiguration configuration = new WifiConfiguration(); 449 configuration.SSID = NETWORK_SSID; 450 configuration.hiddenSSID = true; 451 configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 452 configuration.apChannel = 6; 453 454 assertFalse(mHostapdHal.addAccessPoint(IFACE_NAME, configuration)); 455 verify(mIHostapdMock).addAccessPoint(any(), any()); 456 } 457 458 /** 459 * Verifies the successful removal of access point. 460 */ 461 @Test 462 public void testRemoveAccessPointSuccess() throws Exception { 463 executeAndValidateInitializationSequence(); 464 465 assertTrue(mHostapdHal.removeAccessPoint(IFACE_NAME)); 466 verify(mIHostapdMock).removeAccessPoint(any()); 467 } 468 469 /** 470 * Verifies the failure handling in removal of access point. 471 */ 472 @Test 473 public void testRemoveAccessPointFailure() throws Exception { 474 executeAndValidateInitializationSequence(); 475 when(mIHostapdMock.removeAccessPoint(any())).thenReturn(mStatusFailure); 476 477 assertFalse(mHostapdHal.removeAccessPoint(IFACE_NAME)); 478 verify(mIHostapdMock).removeAccessPoint(any()); 479 } 480 481 /** 482 * Verifies the failure handling in addition of access point. 483 */ 484 @Test 485 public void testRemoveAccessPointRemoteException() throws Exception { 486 executeAndValidateInitializationSequence(); 487 doThrow(new RemoteException()).when(mIHostapdMock).removeAccessPoint(any()); 488 489 assertFalse(mHostapdHal.removeAccessPoint(IFACE_NAME)); 490 verify(mIHostapdMock).removeAccessPoint(any()); 491 } 492 493 private void executeAndValidateInitializationSequence() throws Exception { 494 executeAndValidateInitializationSequence(false, false); 495 } 496 497 /** 498 * Calls.initialize(), mocking various callback answers and verifying flow, asserting for the 499 * expected result. Verifies if IHostapd manager is initialized or reset. 500 */ 501 private void executeAndValidateInitializationSequence( 502 boolean causeRegisterRemoteException, boolean causeRegisterFailure) throws Exception { 503 boolean shouldSucceed = !causeRegisterRemoteException && !causeRegisterFailure; 504 mInOrder = inOrder(mServiceManagerMock, mIHostapdMock); 505 if (causeRegisterFailure) { 506 when(mServiceManagerMock.registerForNotifications(anyString(), anyString(), 507 any(IServiceNotification.Stub.class))).thenReturn(false); 508 } else if (causeRegisterRemoteException) { 509 doThrow(new RemoteException()).when(mServiceManagerMock) 510 .registerForNotifications( 511 anyString(), anyString(), any(IServiceNotification.Stub.class)); 512 } 513 // Initialize HostapdHal, should call serviceManager.registerForNotifications 514 assertEquals(shouldSucceed, mHostapdHal.initialize()); 515 // verify: service manager initialization sequence 516 mInOrder.verify(mServiceManagerMock).linkToDeath(mServiceManagerDeathCaptor.capture(), 517 anyLong()); 518 mInOrder.verify(mServiceManagerMock).registerForNotifications( 519 eq(IHostapd.kInterfaceName), eq(""), mServiceNotificationCaptor.capture()); 520 if (shouldSucceed) { 521 // act: cause the onRegistration(...) callback to execute 522 mServiceNotificationCaptor.getValue().onRegistration(IHostapd.kInterfaceName, "", true); 523 assertTrue(mHostapdHal.isInitializationComplete()); 524 mInOrder.verify(mIHostapdMock).linkToDeath(mHostapdDeathCaptor.capture(), anyLong()); 525 } else { 526 assertFalse(mHostapdHal.isInitializationComplete()); 527 mInOrder.verify(mIHostapdMock, never()).linkToDeath( 528 mHostapdDeathCaptor.capture(), anyLong()); 529 } 530 } 531 532 private HostapdStatus createHostapdStatus(int code) { 533 HostapdStatus status = new HostapdStatus(); 534 status.code = code; 535 return status; 536 } 537 } 538 539