SupplicantP2pIfaceHalTest.java revision cb90e9415a66b9328d5e091317262151e83be823
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 */ 16package com.android.server.wifi.p2p; 17 18import static org.junit.Assert.*; 19import static org.mockito.Matchers.*; 20import static org.mockito.Mockito.doAnswer; 21import static org.mockito.Mockito.doThrow; 22import static org.mockito.Mockito.inOrder; 23import static org.mockito.Mockito.never; 24import static org.mockito.Mockito.verify; 25import static org.mockito.Mockito.when; 26 27import android.app.test.MockAnswerUtil.AnswerWithArguments; 28import android.hardware.wifi.supplicant.V1_0.ISupplicant; 29import android.hardware.wifi.supplicant.V1_0.ISupplicantIface; 30import android.hardware.wifi.supplicant.V1_0.ISupplicantNetwork; 31import android.hardware.wifi.supplicant.V1_0.ISupplicantP2pIface; 32import android.hardware.wifi.supplicant.V1_0.ISupplicantP2pNetwork; 33import android.hardware.wifi.supplicant.V1_0.IfaceType; 34import android.hardware.wifi.supplicant.V1_0.SupplicantStatus; 35import android.hardware.wifi.supplicant.V1_0.SupplicantStatusCode; 36import android.hidl.manager.V1_0.IServiceManager; 37import android.hidl.manager.V1_0.IServiceNotification; 38import android.net.wifi.WpsInfo; 39import android.net.wifi.p2p.WifiP2pConfig; 40import android.net.wifi.p2p.WifiP2pDevice; 41import android.net.wifi.p2p.WifiP2pGroup; 42import android.net.wifi.p2p.WifiP2pGroupList; 43import android.net.wifi.p2p.WifiP2pManager; 44import android.net.wifi.p2p.nsd.WifiP2pServiceInfo; 45import android.os.IHwBinder; 46import android.os.RemoteException; 47import android.text.TextUtils; 48 49import com.android.server.wifi.util.NativeUtil; 50 51import org.junit.Assert.*; 52import org.junit.Before; 53import org.junit.Test; 54import org.mockito.ArgumentCaptor; 55import org.mockito.InOrder; 56import org.mockito.Mock; 57import org.mockito.MockitoAnnotations; 58 59import java.util.ArrayList; 60import java.util.Arrays; 61import java.util.HashMap; 62import java.util.HashSet; 63import java.util.Map; 64 65/** 66 * Unit tests for SupplicantP2pIfaceHal 67 */ 68public class SupplicantP2pIfaceHalTest { 69 private static final String TAG = "SupplicantP2pIfaceHalTest"; 70 private SupplicantP2pIfaceHal mDut; 71 @Mock IServiceManager mServiceManagerMock; 72 @Mock ISupplicant mISupplicantMock; 73 @Mock ISupplicantIface mISupplicantIfaceMock; 74 @Mock ISupplicantP2pIface mISupplicantP2pIfaceMock; 75 @Mock ISupplicantP2pNetwork mISupplicantP2pNetworkMock; 76 77 SupplicantStatus mStatusSuccess; 78 SupplicantStatus mStatusFailure; 79 RemoteException mRemoteException; 80 ISupplicant.IfaceInfo mStaIface; 81 ISupplicant.IfaceInfo mP2pIface; 82 ArrayList<ISupplicant.IfaceInfo> mIfaceInfoList; 83 84 final String mIfaceName = "virtual_interface_name"; 85 final String mSsid = "\"SSID\""; 86 final ArrayList<Byte> mSsidBytes = new ArrayList<Byte>() {{ 87 add((byte)'S'); add((byte)'S'); add((byte)'I'); add((byte)'D'); 88 }}; 89 final String mPeerMacAddress = "00:11:22:33:44:55"; 90 final byte mPeerMacAddressBytes[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 }; 91 final String mGroupOwnerMacAddress = "01:12:23:34:45:56"; 92 final byte mGroupOwnerMacAddressBytes[] = { 0x01, 0x12, 0x23, 0x34, 0x45, 0x56 }; 93 final String mInvalidMacAddress1 = "00:11:22:33:44"; 94 final String mInvalidMacAddress2 = ":::::"; 95 final String mInvalidMacAddress3 = "invalid"; 96 final byte mInvalidMacAddressBytes1[] = null; 97 final byte mInvalidMacAddressBytes2[] = {}; 98 final byte mInvalidMacAddressBytes3[] = { 0x00, 0x01, 0x02, 0x03, 0x04 }; 99 final byte mInvalidMacAddressBytes4[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; 100 HashSet<String> mInvalidMacAddresses = new HashSet<String>(Arrays.asList( 101 mInvalidMacAddress1, mInvalidMacAddress2, 102 mInvalidMacAddress3)); 103 HashSet<byte[]> mInvalidMacAddressesBytes = new HashSet<byte[]>(Arrays.asList( 104 mInvalidMacAddressBytes1, mInvalidMacAddressBytes2, 105 mInvalidMacAddressBytes3, mInvalidMacAddressBytes4)); 106 107 final String mInvalidService1 = null; 108 final String mInvalidService2 = "service"; 109 final String mValidServiceRequestString = "30313233"; 110 final byte[] mValidServiceRequestBytes = { 0x30, 0x31, 0x32, 0x33 }; 111 final String mInvalidServiceRequestString = "not a hex string"; 112 final String mInvalidUpnpService1 = "upnp"; 113 final String mInvalidUpnpService2 = "upnp 1"; 114 final String mInvalidUpnpService3 = "upnp invalid_number name"; 115 final String mInvalidBonjourService1 = "bonjour"; 116 final String mInvalidBonjourService2 = "bonjour 123456"; 117 final String mInvalidBonjourService3 = "bonjour invalid_hex 123456"; 118 final String mInvalidBonjourService4 = "bonjour 123456 invalid_hex"; 119 final String mValidUpnpService = "upnp 1 serviceName"; 120 final int mValidUpnpServiceVersion = 1; 121 final String mValidUpnpServiceName = "serviceName"; 122 final String mValidBonjourService = "bonjour 30313233 34353637"; 123 final ArrayList<Byte> mValidBonjourServiceRequest = new ArrayList<Byte>() {{ 124 add((byte)'0'); add((byte)'1'); add((byte)'2'); add((byte)'3'); 125 }}; 126 final ArrayList<Byte> mValidBonjourServiceResponse = new ArrayList<Byte>() {{ 127 add((byte)'4'); add((byte)'5'); add((byte)'6'); add((byte)'7'); 128 }}; 129 130 131 private ArgumentCaptor<IHwBinder.DeathRecipient> mDeathRecipientCaptor = 132 ArgumentCaptor.forClass(IHwBinder.DeathRecipient.class); 133 private ArgumentCaptor<IServiceNotification.Stub> mServiceNotificationCaptor = 134 ArgumentCaptor.forClass(IServiceNotification.Stub.class); 135 private InOrder mInOrder; 136 137 private class SupplicantP2pIfaceHalSpy extends SupplicantP2pIfaceHal { 138 SupplicantP2pIfaceHalSpy() { 139 super(null); 140 } 141 142 @Override 143 protected IServiceManager getServiceManagerMockable() throws RemoteException { 144 return mServiceManagerMock; 145 } 146 147 @Override 148 protected ISupplicant getSupplicantMockable() throws RemoteException { 149 return mISupplicantMock; 150 } 151 152 @Override 153 protected ISupplicantP2pIface getP2pIfaceMockable(ISupplicantIface iface) { 154 return mISupplicantP2pIfaceMock; 155 } 156 157 @Override 158 protected ISupplicantP2pNetwork getP2pNetworkMockable(ISupplicantNetwork network) { 159 return mISupplicantP2pNetworkMock; 160 } 161 } 162 163 @Before 164 public void setUp() throws Exception { 165 MockitoAnnotations.initMocks(this); 166 mStatusSuccess = createSupplicantStatus(SupplicantStatusCode.SUCCESS); 167 mStatusFailure = createSupplicantStatus(SupplicantStatusCode.FAILURE_UNKNOWN); 168 mRemoteException = new RemoteException("Test Remote Exception"); 169 mStaIface = createIfaceInfo(IfaceType.STA, "wlan0"); 170 mP2pIface = createIfaceInfo(IfaceType.P2P, "p2p0"); 171 172 mIfaceInfoList = new ArrayList<ISupplicant.IfaceInfo>(); 173 mIfaceInfoList.add(mStaIface); 174 mIfaceInfoList.add(mP2pIface); 175 176 when(mServiceManagerMock.linkToDeath(any(IHwBinder.DeathRecipient.class), 177 anyLong())).thenReturn(true); 178 when(mServiceManagerMock.registerForNotifications(anyString(), anyString(), 179 any(IServiceNotification.Stub.class))).thenReturn(true); 180 when(mISupplicantMock.linkToDeath(any(IHwBinder.DeathRecipient.class), 181 anyLong())).thenReturn(true); 182 when(mISupplicantP2pIfaceMock.linkToDeath(any(IHwBinder.DeathRecipient.class), 183 anyLong())).thenReturn(true); 184 mDut = new SupplicantP2pIfaceHalSpy(); 185 } 186 187 /** 188 * Sunny day scenario for SupplicantP2pIfaceHal initialization 189 * Asserts successful initialization 190 */ 191 @Test 192 public void testInitialize_success() throws Exception { 193 executeAndValidateInitializationSequence(false, false, false); 194 } 195 196 /** 197 * Tests the initialization flow, with a RemoteException occurring when 'getInterface' is called 198 * Ensures initialization fails. 199 */ 200 @Test 201 public void testInitialize_remoteExceptionFailure() throws Exception { 202 executeAndValidateInitializationSequence(true, false, false); 203 } 204 205 /** 206 * Tests the initialization flow, with listInterfaces returning 0 interfaces. 207 * Ensures failure 208 */ 209 @Test 210 public void testInitialize_zeroInterfacesFailure() throws Exception { 211 executeAndValidateInitializationSequence(false, true, false); 212 } 213 214 /** 215 * Tests the initialization flow, with a null interface being returned by getInterface. 216 * Ensures initialization fails. 217 */ 218 @Test 219 public void testInitialize_nullInterfaceFailure() throws Exception { 220 executeAndValidateInitializationSequence(false, false, true); 221 } 222 223 /** 224 * Sunny day scenario for getName() 225 */ 226 @Test 227 public void testGetName_success() throws Exception { 228 229 doAnswer(new AnswerWithArguments() { 230 public void answer(ISupplicantIface.getNameCallback cb) throws RemoteException { 231 cb.onValues(mStatusSuccess, mIfaceName); 232 } 233 }) 234 .when(mISupplicantP2pIfaceMock).getName(any(ISupplicantIface.getNameCallback.class)); 235 236 // Default value when service is not initialized. 237 assertNull(mDut.getName()); 238 executeAndValidateInitializationSequence(false, false, false); 239 assertEquals(mIfaceName, mDut.getName()); 240 } 241 242 /** 243 * Verify that getName returns null, if status is not SUCCESS. 244 */ 245 @Test 246 public void testGetName_failure() throws Exception { 247 executeAndValidateInitializationSequence(false, false, false); 248 doAnswer(new AnswerWithArguments() { 249 public void answer(ISupplicantIface.getNameCallback cb) throws RemoteException { 250 cb.onValues(mStatusFailure, "none"); 251 } 252 }) 253 .when(mISupplicantP2pIfaceMock).getName(any(ISupplicantIface.getNameCallback.class)); 254 assertNull(mDut.getName()); 255 // Check that service is still alive. 256 assertTrue(mDut.isInitializationComplete()); 257 } 258 259 /** 260 * Verify that getName disconnects and returns null, if HAL throws exception. 261 */ 262 @Test 263 public void testGetName_exception() throws Exception { 264 executeAndValidateInitializationSequence(false, false, false); 265 doAnswer(new AnswerWithArguments() { 266 public void answer(ISupplicantIface.getNameCallback cb) throws RemoteException { 267 throw new RemoteException("Test"); 268 } 269 }) 270 .when(mISupplicantP2pIfaceMock).getName(any(ISupplicantIface.getNameCallback.class)); 271 assertNull(mDut.getName()); 272 // Check service is dead. 273 assertFalse(mDut.isInitializationComplete()); 274 } 275 276 277 /** 278 * Sunny day scenario for find() 279 */ 280 @Test 281 public void testFind_success() throws Exception { 282 when(mISupplicantP2pIfaceMock.find(anyInt())).thenReturn(mStatusSuccess); 283 // Default value when service is not yet initialized. 284 assertFalse(mDut.find(1)); 285 286 executeAndValidateInitializationSequence(false, false, false); 287 assertTrue(mDut.find(1)); 288 assertFalse(mDut.find(-1)); 289 } 290 291 /** 292 * Verify that find returns false, if status is not SUCCESS. 293 */ 294 @Test 295 public void testFind_failure() throws Exception { 296 executeAndValidateInitializationSequence(false, false, false); 297 when(mISupplicantP2pIfaceMock.find(anyInt())).thenReturn(mStatusFailure); 298 assertFalse(mDut.find(1)); 299 // Check that service is still alive. 300 assertTrue(mDut.isInitializationComplete()); 301 } 302 303 /** 304 * Verify that find disconnects and returns false, if HAL throws exception. 305 */ 306 @Test 307 public void testFind_exception() throws Exception { 308 executeAndValidateInitializationSequence(false, false, false); 309 when(mISupplicantP2pIfaceMock.find(anyInt())).thenThrow(mRemoteException); 310 assertFalse(mDut.find(0)); 311 // Check service is dead. 312 assertFalse(mDut.isInitializationComplete()); 313 } 314 315 316 /** 317 * Sunny day scenario for stopFind() 318 */ 319 @Test 320 public void testStopFind_success() throws Exception { 321 when(mISupplicantP2pIfaceMock.stopFind()).thenReturn(mStatusSuccess); 322 // Default value when service is not yet initialized. 323 assertFalse(mDut.stopFind()); 324 executeAndValidateInitializationSequence(false, false, false); 325 assertTrue(mDut.stopFind()); 326 } 327 328 /** 329 * Verify that stopFind returns false, if status is not SUCCESS. 330 */ 331 @Test 332 public void testStopFind_failure() throws Exception { 333 executeAndValidateInitializationSequence(false, false, false); 334 when(mISupplicantP2pIfaceMock.stopFind()).thenReturn(mStatusFailure); 335 assertFalse(mDut.stopFind()); 336 // Check that service is still alive. 337 assertTrue(mDut.isInitializationComplete()); 338 } 339 340 /** 341 * Verify that stopFind disconnects and returns false, if HAL throws exception. 342 */ 343 @Test 344 public void testStopFind_exception() throws Exception { 345 executeAndValidateInitializationSequence(false, false, false); 346 when(mISupplicantP2pIfaceMock.stopFind()).thenThrow(mRemoteException); 347 assertFalse(mDut.stopFind()); 348 // Check service is dead. 349 assertFalse(mDut.isInitializationComplete()); 350 } 351 352 353 /** 354 * Sunny day scenario for flush() 355 */ 356 @Test 357 public void testFlush_success() throws Exception { 358 when(mISupplicantP2pIfaceMock.flush()).thenReturn(mStatusSuccess); 359 // Default value when service is not yet initialized. 360 assertFalse(mDut.flush()); 361 executeAndValidateInitializationSequence(false, false, false); 362 assertTrue(mDut.flush()); 363 } 364 365 /** 366 * Verify that flush returns false, if status is not SUCCESS. 367 */ 368 @Test 369 public void testFlush_failure() throws Exception { 370 executeAndValidateInitializationSequence(false, false, false); 371 when(mISupplicantP2pIfaceMock.flush()).thenReturn(mStatusFailure); 372 assertFalse(mDut.flush()); 373 // Check that service is still alive. 374 assertTrue(mDut.isInitializationComplete()); 375 } 376 377 /** 378 * Verify that flush disconnects and returns false, if HAL throws exception. 379 */ 380 @Test 381 public void testFlush_exception() throws Exception { 382 executeAndValidateInitializationSequence(false, false, false); 383 when(mISupplicantP2pIfaceMock.flush()).thenThrow(mRemoteException); 384 assertFalse(mDut.flush()); 385 // Check service is dead. 386 assertFalse(mDut.isInitializationComplete()); 387 } 388 389 390 /** 391 * Sunny day scenario for serviceFlush() 392 */ 393 @Test 394 public void testServiceFlush_success() throws Exception { 395 when(mISupplicantP2pIfaceMock.flushServices()).thenReturn(mStatusSuccess); 396 // Default value when service is not initialized. 397 assertFalse(mDut.serviceFlush()); 398 executeAndValidateInitializationSequence(false, false, false); 399 assertTrue(mDut.serviceFlush()); 400 } 401 402 /** 403 * Verify that serviceFlush returns false, if status is not SUCCESS. 404 */ 405 @Test 406 public void testServiceFlush_failure() throws Exception { 407 executeAndValidateInitializationSequence(false, false, false); 408 when(mISupplicantP2pIfaceMock.flushServices()).thenReturn(mStatusFailure); 409 assertFalse(mDut.serviceFlush()); 410 // Check that service is still alive. 411 assertTrue(mDut.isInitializationComplete()); 412 } 413 414 /** 415 * Verify that serviceFlush disconnects and returns false, if HAL throws exception. 416 */ 417 @Test 418 public void testServiceFlush_exception() throws Exception { 419 executeAndValidateInitializationSequence(false, false, false); 420 when(mISupplicantP2pIfaceMock.flushServices()).thenThrow(mRemoteException); 421 assertFalse(mDut.serviceFlush()); 422 // Check service is dead. 423 assertFalse(mDut.isInitializationComplete()); 424 } 425 426 427 /** 428 * Sunny day scenario for setPowerSave() 429 */ 430 @Test 431 public void testSetPowerSave_success() throws Exception { 432 when(mISupplicantP2pIfaceMock.setPowerSave(eq(mIfaceName), anyBoolean())) 433 .thenReturn(mStatusSuccess); 434 // Default value when service is not initialized. 435 assertFalse(mDut.setPowerSave(mIfaceName, true)); 436 executeAndValidateInitializationSequence(false, false, false); 437 assertTrue(mDut.setPowerSave(mIfaceName, true)); 438 } 439 440 /** 441 * Verify that setPowerSave returns false, if status is not SUCCESS. 442 */ 443 @Test 444 public void testSetPowerSave_failure() throws Exception { 445 executeAndValidateInitializationSequence(false, false, false); 446 when(mISupplicantP2pIfaceMock.setPowerSave(eq(mIfaceName), anyBoolean())) 447 .thenReturn(mStatusFailure); 448 assertFalse(mDut.setPowerSave(mIfaceName, true)); 449 // Check that service is still alive. 450 assertTrue(mDut.isInitializationComplete()); 451 } 452 453 /** 454 * Verify that setPowerSave disconnects and returns false, if HAL throws exception. 455 */ 456 @Test 457 public void testSetPowerSave_exception() throws Exception { 458 executeAndValidateInitializationSequence(false, false, false); 459 when(mISupplicantP2pIfaceMock.setPowerSave(eq(mIfaceName), anyBoolean())) 460 .thenThrow(mRemoteException); 461 assertFalse(mDut.setPowerSave(mIfaceName, true)); 462 // Check service is dead. 463 assertFalse(mDut.isInitializationComplete()); 464 } 465 466 467 /** 468 * Sunny day scenario for setGroupIdle() 469 */ 470 @Test 471 public void testSetGroupIdle_success() throws Exception { 472 when(mISupplicantP2pIfaceMock.setGroupIdle(eq(mIfaceName), anyInt())) 473 .thenReturn(mStatusSuccess); 474 // Default value when service is not initialized. 475 assertFalse(mDut.setGroupIdle(mIfaceName, 1)); 476 executeAndValidateInitializationSequence(false, false, false); 477 assertTrue(mDut.setGroupIdle(mIfaceName, 1)); 478 assertFalse(mDut.setGroupIdle(mIfaceName, -1)); 479 } 480 481 /** 482 * Verify that setGroupIdle returns false, if status is not SUCCESS. 483 */ 484 @Test 485 public void testSetGroupIdle_failure() throws Exception { 486 executeAndValidateInitializationSequence(false, false, false); 487 when(mISupplicantP2pIfaceMock.setGroupIdle(eq(mIfaceName), anyInt())) 488 .thenReturn(mStatusFailure); 489 assertFalse(mDut.setGroupIdle(mIfaceName, 1)); 490 // Check that service is still alive. 491 assertTrue(mDut.isInitializationComplete()); 492 } 493 494 /** 495 * Verify that setGroupIdle disconnects and returns false, if HAL throws exception. 496 */ 497 @Test 498 public void testSetGroupIdle_exception() throws Exception { 499 executeAndValidateInitializationSequence(false, false, false); 500 when(mISupplicantP2pIfaceMock.setGroupIdle(eq(mIfaceName), anyInt())) 501 .thenThrow(mRemoteException); 502 assertFalse(mDut.setGroupIdle(mIfaceName, 1)); 503 // Check service is dead. 504 assertFalse(mDut.isInitializationComplete()); 505 } 506 507 508 /** 509 * Sunny day scenario for setSsidPostfix() 510 */ 511 @Test 512 public void testSetSsidPostfix_success() throws Exception { 513 String ssid = "SSID POSTFIX"; 514 when(mISupplicantP2pIfaceMock.setSsidPostfix(eq(NativeUtil.decodeSsid("\"" + ssid + "\"")))) 515 .thenReturn(mStatusSuccess); 516 // Default value when service is not initialized. 517 assertFalse(mDut.setSsidPostfix(ssid)); 518 executeAndValidateInitializationSequence(false, false, false); 519 assertTrue(mDut.setSsidPostfix(ssid)); 520 assertFalse(mDut.setSsidPostfix(null)); 521 } 522 523 /** 524 * Verify that setSsidPostfix returns false, if status is not SUCCESS. 525 */ 526 @Test 527 public void testSetSsidPostfix_failure() throws Exception { 528 String ssid = "SSID POSTFIX"; 529 executeAndValidateInitializationSequence(false, false, false); 530 when(mISupplicantP2pIfaceMock.setSsidPostfix(eq(NativeUtil.decodeSsid("\"" + ssid + "\"")))) 531 .thenReturn(mStatusFailure); 532 assertFalse(mDut.setSsidPostfix(ssid)); 533 // Check that service is still alive. 534 assertTrue(mDut.isInitializationComplete()); 535 } 536 537 /** 538 * Verify that setSsidPostfix disconnects and returns false, if HAL throws exception. 539 */ 540 @Test 541 public void testSetSsidPostfix_exception() throws Exception { 542 String ssid = "SSID POSTFIX"; 543 executeAndValidateInitializationSequence(false, false, false); 544 when(mISupplicantP2pIfaceMock.setSsidPostfix(eq(NativeUtil.decodeSsid("\"" + ssid + "\"")))) 545 .thenThrow(mRemoteException); 546 assertFalse(mDut.setSsidPostfix(ssid)); 547 // Check service is dead. 548 assertFalse(mDut.isInitializationComplete()); 549 } 550 551 552 /** 553 * Sunny day scenario for connect() 554 */ 555 @Test 556 public void testConnect_success() throws Exception { 557 final String configPin = "12345"; 558 final HashSet<Integer> methods = new HashSet<>(); 559 560 doAnswer(new AnswerWithArguments() { 561 public void answer(byte[] peer, int method, String pin, boolean joinExisting, 562 boolean persistent, int goIntent, 563 ISupplicantP2pIface.connectCallback cb) throws RemoteException { 564 methods.add(method); 565 566 if (method == ISupplicantP2pIface.WpsProvisionMethod.DISPLAY 567 && TextUtils.isEmpty(pin)) { 568 // Return the configPin for DISPLAY method if the pin was not provided. 569 cb.onValues(mStatusSuccess, configPin); 570 } else { 571 if (method != ISupplicantP2pIface.WpsProvisionMethod.PBC) { 572 // PIN is only required for PIN methods. 573 assertEquals(pin, configPin); 574 } 575 // For all the other cases, there is no generated pin. 576 cb.onValues(mStatusSuccess, ""); 577 } 578 } 579 }) 580 .when(mISupplicantP2pIfaceMock).connect( 581 eq(mPeerMacAddressBytes), anyInt(), anyString(), anyBoolean(), anyBoolean(), 582 anyInt(), any(ISupplicantP2pIface.connectCallback.class)); 583 584 WifiP2pConfig config = createDummyP2pConfig(mPeerMacAddress, WpsInfo.DISPLAY, ""); 585 586 // Default value when service is not initialized. 587 assertNull(mDut.connect(config, false)); 588 589 executeAndValidateInitializationSequence(false, false, false); 590 591 assertEquals(configPin, mDut.connect(config, false)); 592 assertTrue(methods.contains(ISupplicantP2pIface.WpsProvisionMethod.DISPLAY)); 593 methods.clear(); 594 595 config = createDummyP2pConfig(mPeerMacAddress, WpsInfo.DISPLAY, configPin); 596 assertTrue(mDut.connect(config, false).isEmpty()); 597 assertTrue(methods.contains(ISupplicantP2pIface.WpsProvisionMethod.DISPLAY)); 598 methods.clear(); 599 600 config = createDummyP2pConfig(mPeerMacAddress, WpsInfo.PBC, ""); 601 assertTrue(mDut.connect(config, false).isEmpty()); 602 assertTrue(methods.contains(ISupplicantP2pIface.WpsProvisionMethod.PBC)); 603 methods.clear(); 604 605 config = createDummyP2pConfig(mPeerMacAddress, WpsInfo.KEYPAD, configPin); 606 assertTrue(mDut.connect(config, false).isEmpty()); 607 assertTrue(methods.contains(ISupplicantP2pIface.WpsProvisionMethod.KEYPAD)); 608 } 609 610 /** 611 * Test connect with invalid arguments. 612 */ 613 @Test 614 public void testConnect_invalidArguments() throws Exception { 615 executeAndValidateInitializationSequence(false, false, false); 616 doAnswer(new AnswerWithArguments() { 617 public void answer(byte[] peer, int method, String pin, boolean joinExisting, 618 boolean persistent, int goIntent, 619 ISupplicantP2pIface.connectCallback cb) throws RemoteException { 620 cb.onValues(mStatusSuccess, pin); 621 } 622 }) 623 .when(mISupplicantP2pIfaceMock).connect( 624 any(byte[].class), anyInt(), anyString(), anyBoolean(), anyBoolean(), 625 anyInt(), any(ISupplicantP2pIface.connectCallback.class)); 626 627 WifiP2pConfig config = createDummyP2pConfig(mPeerMacAddress, WpsInfo.DISPLAY, ""); 628 629 // unsupported. 630 config.wps.setup = -1; 631 assertNull(mDut.connect(config, false)); 632 633 // Invalid peer address. 634 config.wps.setup = WpsInfo.DISPLAY; 635 for (String address : mInvalidMacAddresses) { 636 config.deviceAddress = address; 637 assertNull(mDut.connect(config, false)); 638 } 639 640 // null pin not valid. 641 config.wps.setup = WpsInfo.DISPLAY; 642 config.wps.pin = null; 643 assertNull(mDut.connect(config, false)); 644 645 // Pin should be empty for PBC. 646 config.wps.setup = WpsInfo.PBC; 647 config.wps.pin = "03455323"; 648 assertNull(mDut.connect(config, false)); 649 } 650 651 /** 652 * Verify that connect returns null, if status is not SUCCESS. 653 */ 654 @Test 655 public void testConnect_failure() throws Exception { 656 final String configPin = "12345"; 657 WifiP2pConfig config = createDummyP2pConfig(mPeerMacAddress, WpsInfo.DISPLAY, configPin); 658 659 executeAndValidateInitializationSequence(false, false, false); 660 doAnswer(new AnswerWithArguments() { 661 public void answer(byte[] peer, int method, String pin, boolean joinExisting, 662 boolean persistent, int goIntent, 663 ISupplicantP2pIface.connectCallback cb) throws RemoteException { 664 cb.onValues(mStatusFailure, null); 665 } 666 }) 667 .when(mISupplicantP2pIfaceMock).connect( 668 eq(mPeerMacAddressBytes), anyInt(), anyString(), anyBoolean(), anyBoolean(), 669 anyInt(), any(ISupplicantP2pIface.connectCallback.class)); 670 671 assertNull(mDut.connect(config, false)); 672 // Check that service is still alive. 673 assertTrue(mDut.isInitializationComplete()); 674 } 675 676 /** 677 * Verify that connect disconnects and returns null, if HAL throws exception. 678 */ 679 @Test 680 public void testConnect_exception() throws Exception { 681 final String configPin = "12345"; 682 WifiP2pConfig config = createDummyP2pConfig(mPeerMacAddress, WpsInfo.DISPLAY, configPin); 683 684 doThrow(mRemoteException) 685 .when(mISupplicantP2pIfaceMock).connect( 686 eq(mPeerMacAddressBytes), anyInt(), anyString(), anyBoolean(), anyBoolean(), 687 anyInt(), any(ISupplicantP2pIface.connectCallback.class)); 688 689 assertNull(mDut.connect(config, false)); 690 // Check service is dead. 691 assertFalse(mDut.isInitializationComplete()); 692 } 693 694 695 /** 696 * Sunny day scenario for cancelConnect() 697 */ 698 @Test 699 public void testCancelConnect_success() throws Exception { 700 when(mISupplicantP2pIfaceMock.cancelConnect()) 701 .thenReturn(mStatusSuccess); 702 // Default value when service is not initialized. 703 assertFalse(mDut.cancelConnect()); 704 executeAndValidateInitializationSequence(false, false, false); 705 assertTrue(mDut.cancelConnect()); 706 } 707 708 /** 709 * Verify that cancelConnect returns false, if status is not SUCCESS. 710 */ 711 @Test 712 public void testCancelConnect_failure() throws Exception { 713 executeAndValidateInitializationSequence(false, false, false); 714 when(mISupplicantP2pIfaceMock.cancelConnect()) 715 .thenReturn(mStatusFailure); 716 assertFalse(mDut.cancelConnect()); 717 // Check that service is still alive. 718 assertTrue(mDut.isInitializationComplete()); 719 } 720 721 /** 722 * Verify that cancelConnect disconnects and returns false, if HAL throws exception. 723 */ 724 @Test 725 public void testCancelConnect_exception() throws Exception { 726 String ssid = "\"SSID POSTFIX\""; 727 executeAndValidateInitializationSequence(false, false, false); 728 when(mISupplicantP2pIfaceMock.cancelConnect()) 729 .thenThrow(mRemoteException); 730 assertFalse(mDut.cancelConnect()); 731 // Check service is dead. 732 assertFalse(mDut.isInitializationComplete()); 733 } 734 735 736 /** 737 * Sunny day scenario for provisionDiscovery() 738 */ 739 @Test 740 public void testProvisionDiscovery_success() throws Exception { 741 WifiP2pConfig config = createDummyP2pConfig(mPeerMacAddress, WpsInfo.PBC, ""); 742 743 when(mISupplicantP2pIfaceMock.provisionDiscovery( 744 eq(mPeerMacAddressBytes), anyInt())) 745 .thenReturn(mStatusSuccess); 746 // Default value when service is not initialized. 747 assertFalse(mDut.provisionDiscovery(config)); 748 executeAndValidateInitializationSequence(false, false, false); 749 assertTrue(mDut.provisionDiscovery(config)); 750 } 751 752 /** 753 * Test provisionDiscovery with invalid arguments. 754 */ 755 @Test 756 public void testProvisionDiscovery_invalidArguments() throws Exception { 757 when(mISupplicantP2pIfaceMock.provisionDiscovery( 758 eq(mPeerMacAddressBytes), anyInt())) 759 .thenReturn(mStatusSuccess); 760 executeAndValidateInitializationSequence(false, false, false); 761 762 WifiP2pConfig config = createDummyP2pConfig(mPeerMacAddress, WpsInfo.PBC, ""); 763 764 // Unsupported method. 765 config.wps.setup = -1; 766 assertFalse(mDut.provisionDiscovery(config)); 767 768 config.wps.setup = WpsInfo.PBC; 769 for (String address : mInvalidMacAddresses) { 770 config.deviceAddress = address; 771 assertFalse(mDut.provisionDiscovery(config)); 772 } 773 } 774 775 /** 776 * Verify that provisionDiscovery returns false, if status is not SUCCESS. 777 */ 778 @Test 779 public void testProvisionDiscovery_failure() throws Exception { 780 WifiP2pConfig config = createDummyP2pConfig(mPeerMacAddress, WpsInfo.PBC, ""); 781 782 executeAndValidateInitializationSequence(false, false, false); 783 when(mISupplicantP2pIfaceMock.provisionDiscovery( 784 eq(mPeerMacAddressBytes), anyInt())) 785 .thenReturn(mStatusFailure); 786 assertFalse(mDut.provisionDiscovery(config)); 787 // Check that service is still alive. 788 assertTrue(mDut.isInitializationComplete()); 789 } 790 791 /** 792 * Verify that provisionDiscovery disconnects and returns false, if HAL throws exception. 793 */ 794 @Test 795 public void testProvisionDiscovery_exception() throws Exception { 796 WifiP2pConfig config = createDummyP2pConfig(mPeerMacAddress, WpsInfo.PBC, ""); 797 798 executeAndValidateInitializationSequence(false, false, false); 799 when(mISupplicantP2pIfaceMock.provisionDiscovery( 800 eq(mPeerMacAddressBytes), anyInt())) 801 .thenThrow(mRemoteException); 802 assertFalse(mDut.provisionDiscovery(config)); 803 // Check service is dead. 804 assertFalse(mDut.isInitializationComplete()); 805 } 806 807 808 /** 809 * Sunny day scenario for invite() 810 */ 811 @Test 812 public void testInvite_success() throws Exception { 813 WifiP2pGroup group = createDummyP2pGroup(); 814 815 when(mISupplicantP2pIfaceMock.invite( 816 eq(mIfaceName), eq(mGroupOwnerMacAddressBytes), eq(mPeerMacAddressBytes))) 817 .thenReturn(mStatusSuccess); 818 // Default value when service is not initialized. 819 assertFalse(mDut.invite(group, mPeerMacAddress)); 820 executeAndValidateInitializationSequence(false, false, false); 821 assertTrue(mDut.invite(group, mPeerMacAddress)); 822 } 823 824 /** 825 * Invite with invalid arguments. 826 */ 827 @Test 828 public void testInvite_invalidArguments() throws Exception { 829 WifiP2pGroup group = createDummyP2pGroup(); 830 831 executeAndValidateInitializationSequence(false, false, false); 832 when(mISupplicantP2pIfaceMock.invite( 833 anyString(), any(byte[].class), any(byte[].class))) 834 .thenReturn(mStatusSuccess); 835 836 for (String address : mInvalidMacAddresses) { 837 assertFalse(mDut.invite(group, address)); 838 } 839 840 for (String address : mInvalidMacAddresses) { 841 group.getOwner().deviceAddress = address; 842 assertFalse(mDut.invite(group, mPeerMacAddress)); 843 } 844 845 group.setOwner(null); 846 assertFalse(mDut.invite(group, mPeerMacAddress)); 847 assertFalse(mDut.invite(null, mPeerMacAddress)); 848 } 849 850 /** 851 * Verify that invite returns false, if status is not SUCCESS. 852 */ 853 @Test 854 public void testInvite_failure() throws Exception { 855 WifiP2pGroup group = createDummyP2pGroup(); 856 857 executeAndValidateInitializationSequence(false, false, false); 858 when(mISupplicantP2pIfaceMock.invite( 859 anyString(), any(byte[].class), any(byte[].class))) 860 .thenReturn(mStatusFailure); 861 assertFalse(mDut.invite(group, mPeerMacAddress)); 862 // Check that service is still alive. 863 assertTrue(mDut.isInitializationComplete()); 864 } 865 866 /** 867 * Verify that invite disconnects and returns false, if HAL throws exception. 868 */ 869 @Test 870 public void testInvite_exception() throws Exception { 871 WifiP2pGroup group = createDummyP2pGroup(); 872 873 executeAndValidateInitializationSequence(false, false, false); 874 when(mISupplicantP2pIfaceMock.invite( 875 anyString(), any(byte[].class), any(byte[].class))) 876 .thenThrow(mRemoteException); 877 assertFalse(mDut.invite(group, mPeerMacAddress)); 878 // Check service is dead. 879 assertFalse(mDut.isInitializationComplete()); 880 } 881 882 883 /** 884 * Sunny day scenario for reject() 885 */ 886 @Test 887 public void testReject_success() throws Exception { 888 when(mISupplicantP2pIfaceMock.reject(eq(mPeerMacAddressBytes))) 889 .thenReturn(mStatusSuccess); 890 // Default value when service is not initialized. 891 assertFalse(mDut.reject(mPeerMacAddress)); 892 executeAndValidateInitializationSequence(false, false, false); 893 assertTrue(mDut.reject(mPeerMacAddress)); 894 } 895 896 /** 897 * Reject with invalid arguments. 898 */ 899 @Test 900 public void testReject_invalidArguments() throws Exception { 901 executeAndValidateInitializationSequence(false, false, false); 902 when(mISupplicantP2pIfaceMock.reject(any(byte[].class))) 903 .thenReturn(mStatusSuccess); 904 905 for (String address : mInvalidMacAddresses) { 906 assertFalse(mDut.reject(address)); 907 } 908 } 909 910 /** 911 * Verify that reject returns false, if status is not SUCCESS. 912 */ 913 @Test 914 public void testReject_failure() throws Exception { 915 executeAndValidateInitializationSequence(false, false, false); 916 when(mISupplicantP2pIfaceMock.reject(any(byte[].class))) 917 .thenReturn(mStatusFailure); 918 assertFalse(mDut.reject(mPeerMacAddress)); 919 // Check that service is still alive. 920 assertTrue(mDut.isInitializationComplete()); 921 } 922 923 /** 924 * Verify that reject disconnects and returns false, if HAL throws exception. 925 */ 926 @Test 927 public void testReject_exception() throws Exception { 928 executeAndValidateInitializationSequence(false, false, false); 929 when(mISupplicantP2pIfaceMock.reject(any(byte[].class))) 930 .thenThrow(mRemoteException); 931 assertFalse(mDut.reject(mPeerMacAddress)); 932 // Check service is dead. 933 assertFalse(mDut.isInitializationComplete()); 934 } 935 936 937 /** 938 * Sunny day scenario for getDeviceAddress() 939 */ 940 @Test 941 public void testGetDeviceAddress_success() throws Exception { 942 doAnswer(new AnswerWithArguments() { 943 public void answer(ISupplicantP2pIface.getDeviceAddressCallback cb) { 944 cb.onValues(mStatusSuccess, mPeerMacAddressBytes); 945 } 946 }) 947 .when(mISupplicantP2pIfaceMock).getDeviceAddress( 948 any(ISupplicantP2pIface.getDeviceAddressCallback.class)); 949 950 // Default value when service is not initialized. 951 assertNull(mDut.getDeviceAddress()); 952 executeAndValidateInitializationSequence(false, false, false); 953 assertEquals(mPeerMacAddress, mDut.getDeviceAddress()); 954 } 955 956 /** 957 * Test getDeviceAddress() when invalid mac address is being reported. 958 */ 959 @Test 960 public void testGetDeviceAddress_invalidResult() throws Exception { 961 executeAndValidateInitializationSequence(false, false, false); 962 HashSet<byte[]> addresses = new HashSet<byte[]>(Arrays.asList( 963 mInvalidMacAddressBytes1, mInvalidMacAddressBytes2, 964 mInvalidMacAddressBytes3, mInvalidMacAddressBytes4)); 965 966 doAnswer(new AnswerWithArguments() { 967 public void answer(ISupplicantP2pIface.getDeviceAddressCallback cb) { 968 byte[] address = addresses.iterator().next(); 969 cb.onValues(mStatusSuccess, address); 970 addresses.remove(address); 971 } 972 }) 973 .when(mISupplicantP2pIfaceMock).getDeviceAddress( 974 any(ISupplicantP2pIface.getDeviceAddressCallback.class)); 975 976 // Default value when service is not initialized. 977 while (!addresses.isEmpty()) { 978 assertNull(mDut.getDeviceAddress()); 979 } 980 } 981 982 /** 983 * Verify that getDeviceAddress returns false, if status is not SUCCESS. 984 */ 985 @Test 986 public void testGetDeviceAddress_failure() throws Exception { 987 executeAndValidateInitializationSequence(false, false, false); 988 doAnswer(new AnswerWithArguments() { 989 public void answer(ISupplicantP2pIface.getDeviceAddressCallback cb) { 990 cb.onValues(mStatusFailure, null); 991 } 992 }) 993 .when(mISupplicantP2pIfaceMock).getDeviceAddress( 994 any(ISupplicantP2pIface.getDeviceAddressCallback.class)); 995 996 assertNull(mDut.getDeviceAddress()); 997 // Check that service is still alive. 998 assertTrue(mDut.isInitializationComplete()); 999 } 1000 1001 /** 1002 * Verify that getDeviceAddress disconnects and returns false, if HAL throws exception. 1003 */ 1004 @Test 1005 public void testGetDeviceAddress_exception() throws Exception { 1006 executeAndValidateInitializationSequence(false, false, false); 1007 doThrow(mRemoteException).when(mISupplicantP2pIfaceMock).getDeviceAddress( 1008 any(ISupplicantP2pIface.getDeviceAddressCallback.class)); 1009 1010 assertNull(mDut.getDeviceAddress()); 1011 // Check service is dead. 1012 assertFalse(mDut.isInitializationComplete()); 1013 } 1014 1015 1016 /** 1017 * Sunny day scenario for getSsid() 1018 */ 1019 @Test 1020 public void testGetSsid_success() throws Exception { 1021 doAnswer(new AnswerWithArguments() { 1022 public void answer(byte[] address, ISupplicantP2pIface.getSsidCallback cb) { 1023 cb.onValues(mStatusSuccess, mSsidBytes); 1024 } 1025 }) 1026 .when(mISupplicantP2pIfaceMock).getSsid( 1027 eq(mPeerMacAddressBytes), 1028 any(ISupplicantP2pIface.getSsidCallback.class)); 1029 1030 // Default value when service is not initialized. 1031 assertNull(mDut.getSsid(mPeerMacAddress)); 1032 executeAndValidateInitializationSequence(false, false, false); 1033 assertEquals(mSsid, mDut.getSsid(mPeerMacAddress)); 1034 } 1035 1036 /** 1037 * Test getSsid() with invalid argument and response. 1038 */ 1039 @Test 1040 public void testGetSsid_invalidArguments() throws Exception { 1041 executeAndValidateInitializationSequence(false, false, false); 1042 1043 doAnswer(new AnswerWithArguments() { 1044 public void answer(byte[] address, ISupplicantP2pIface.getSsidCallback cb) { 1045 cb.onValues(mStatusSuccess, mSsidBytes); 1046 } 1047 }) 1048 .when(mISupplicantP2pIfaceMock).getSsid( 1049 any(byte[].class), any(ISupplicantP2pIface.getSsidCallback.class)); 1050 1051 for (String address : mInvalidMacAddresses) { 1052 assertNull(mDut.getSsid(address)); 1053 } 1054 1055 // Simulate null response from HAL. 1056 doAnswer(new AnswerWithArguments() { 1057 public void answer(byte[] address, ISupplicantP2pIface.getSsidCallback cb) { 1058 cb.onValues(mStatusSuccess, null); 1059 } 1060 }) 1061 .when(mISupplicantP2pIfaceMock).getSsid( 1062 any(byte[].class), any(ISupplicantP2pIface.getSsidCallback.class)); 1063 1064 assertNull(mDut.getSsid(mPeerMacAddress)); 1065 } 1066 1067 /** 1068 * Verify that getSsid returns false, if status is not SUCCESS. 1069 */ 1070 @Test 1071 public void testGetSsid_failure() throws Exception { 1072 executeAndValidateInitializationSequence(false, false, false); 1073 1074 doAnswer(new AnswerWithArguments() { 1075 public void answer(byte[] address, ISupplicantP2pIface.getSsidCallback cb) { 1076 cb.onValues(mStatusFailure, null); 1077 } 1078 }) 1079 .when(mISupplicantP2pIfaceMock).getSsid( 1080 any(byte[].class), any(ISupplicantP2pIface.getSsidCallback.class)); 1081 1082 assertNull(mDut.getSsid(mPeerMacAddress)); 1083 // Check that service is still alive. 1084 assertTrue(mDut.isInitializationComplete()); 1085 } 1086 1087 /** 1088 * Verify that getSsid disconnects and returns false, if HAL throws exception. 1089 */ 1090 @Test 1091 public void testGetSsid_exception() throws Exception { 1092 executeAndValidateInitializationSequence(false, false, false); 1093 doThrow(mRemoteException) 1094 .when(mISupplicantP2pIfaceMock).getSsid( 1095 any(byte[].class), any(ISupplicantP2pIface.getSsidCallback.class)); 1096 1097 assertNull(mDut.getSsid(mPeerMacAddress)); 1098 // Check service is dead. 1099 assertFalse(mDut.isInitializationComplete()); 1100 } 1101 1102 1103 /** 1104 * Sunny day scenario for reinvoke() 1105 */ 1106 @Test 1107 public void testReinvoke_success() throws Exception { 1108 when(mISupplicantP2pIfaceMock.reinvoke(anyInt(), eq(mPeerMacAddressBytes))) 1109 .thenReturn(mStatusSuccess); 1110 // Default value when service is not initialized. 1111 assertFalse(mDut.reinvoke(0, mPeerMacAddress)); 1112 executeAndValidateInitializationSequence(false, false, false); 1113 assertTrue(mDut.reinvoke(0, mPeerMacAddress)); 1114 } 1115 1116 /** 1117 * Reinvoke with invalid arguments. 1118 */ 1119 @Test 1120 public void testReinvoke_invalidArguments() throws Exception { 1121 executeAndValidateInitializationSequence(false, false, false); 1122 when(mISupplicantP2pIfaceMock.reinvoke(anyInt(), any(byte[].class))) 1123 .thenReturn(mStatusSuccess); 1124 1125 for (String address : mInvalidMacAddresses) { 1126 assertFalse(mDut.reinvoke(0, address)); 1127 } 1128 } 1129 1130 /** 1131 * Verify that reinvoke returns false, if status is not SUCCESS. 1132 */ 1133 @Test 1134 public void testReinvoke_failure() throws Exception { 1135 executeAndValidateInitializationSequence(false, false, false); 1136 when(mISupplicantP2pIfaceMock.reinvoke(anyInt(), any(byte[].class))) 1137 .thenReturn(mStatusFailure); 1138 assertFalse(mDut.reinvoke(0, mPeerMacAddress)); 1139 // Check that service is still alive. 1140 assertTrue(mDut.isInitializationComplete()); 1141 } 1142 1143 /** 1144 * Verify that reinvoke disconnects and returns false, if HAL throws exception. 1145 */ 1146 @Test 1147 public void testReinvoke_exception() throws Exception { 1148 executeAndValidateInitializationSequence(false, false, false); 1149 when(mISupplicantP2pIfaceMock.reinvoke(anyInt(), any(byte[].class))) 1150 .thenThrow(mRemoteException); 1151 assertFalse(mDut.reinvoke(0, mPeerMacAddress)); 1152 // Check service is dead. 1153 assertFalse(mDut.isInitializationComplete()); 1154 } 1155 1156 1157 /** 1158 * Sunny day scenario for groupAdd() 1159 */ 1160 @Test 1161 public void testGroupAdd_success() throws Exception { 1162 when(mISupplicantP2pIfaceMock.addGroup(eq(true), eq(3))) 1163 .thenReturn(mStatusSuccess); 1164 // Default value when service is not initialized. 1165 assertFalse(mDut.groupAdd(3, true)); 1166 executeAndValidateInitializationSequence(false, false, false); 1167 assertTrue(mDut.groupAdd(3, true)); 1168 } 1169 1170 /** 1171 * Verify that groupAdd returns false, if status is not SUCCESS. 1172 */ 1173 @Test 1174 public void testGroupAdd_failure() throws Exception { 1175 executeAndValidateInitializationSequence(false, false, false); 1176 when(mISupplicantP2pIfaceMock.addGroup(anyBoolean(), anyInt())) 1177 .thenReturn(mStatusFailure); 1178 assertFalse(mDut.groupAdd(0, true)); 1179 // Check that service is still alive. 1180 assertTrue(mDut.isInitializationComplete()); 1181 } 1182 1183 /** 1184 * Verify that groupAdd disconnects and returns false, if HAL throws exception. 1185 */ 1186 @Test 1187 public void testGroupAdd_exception() throws Exception { 1188 executeAndValidateInitializationSequence(false, false, false); 1189 when(mISupplicantP2pIfaceMock.addGroup(anyBoolean(), anyInt())) 1190 .thenThrow(mRemoteException); 1191 assertFalse(mDut.groupAdd(0, true)); 1192 // Check service is dead. 1193 assertFalse(mDut.isInitializationComplete()); 1194 } 1195 1196 1197 /** 1198 * Sunny day scenario for groupRemove() 1199 */ 1200 @Test 1201 public void testGroupRemove_success() throws Exception { 1202 when(mISupplicantP2pIfaceMock.removeGroup(eq(mIfaceName))) 1203 .thenReturn(mStatusSuccess); 1204 // Default value when service is not initialized. 1205 assertFalse(mDut.groupRemove(mIfaceName)); 1206 executeAndValidateInitializationSequence(false, false, false); 1207 assertTrue(mDut.groupRemove(mIfaceName)); 1208 } 1209 1210 /** 1211 * Verify that groupRemove returns false, if status is not SUCCESS. 1212 */ 1213 @Test 1214 public void testGroupRemove_failure() throws Exception { 1215 executeAndValidateInitializationSequence(false, false, false); 1216 when(mISupplicantP2pIfaceMock.removeGroup(anyString())) 1217 .thenReturn(mStatusFailure); 1218 assertFalse(mDut.groupRemove(mIfaceName)); 1219 // Check that service is still alive. 1220 assertTrue(mDut.isInitializationComplete()); 1221 } 1222 1223 /** 1224 * Verify that groupRemove disconnects and returns false, if HAL throws exception. 1225 */ 1226 @Test 1227 public void testGroupRemove_exception() throws Exception { 1228 executeAndValidateInitializationSequence(false, false, false); 1229 when(mISupplicantP2pIfaceMock.removeGroup(anyString())) 1230 .thenThrow(mRemoteException); 1231 assertFalse(mDut.groupRemove(mIfaceName)); 1232 // Check service is dead. 1233 assertFalse(mDut.isInitializationComplete()); 1234 } 1235 1236 1237 /** 1238 * Sunny day scenario for getGroupCapability() 1239 */ 1240 @Test 1241 public void testGetGroupCapability_success() throws Exception { 1242 final int caps = 123; 1243 1244 doAnswer(new AnswerWithArguments() { 1245 public void answer(byte[] address, ISupplicantP2pIface.getGroupCapabilityCallback cb) { 1246 cb.onValues(mStatusSuccess, caps); 1247 } 1248 }) 1249 .when(mISupplicantP2pIfaceMock) 1250 .getGroupCapability( 1251 eq(mPeerMacAddressBytes), 1252 any(ISupplicantP2pIface.getGroupCapabilityCallback.class)); 1253 1254 // Default value when service is not initialized. 1255 assertEquals(-1, mDut.getGroupCapability(mPeerMacAddress)); 1256 executeAndValidateInitializationSequence(false, false, false); 1257 assertEquals(caps, mDut.getGroupCapability(mPeerMacAddress)); 1258 } 1259 1260 /** 1261 * GetGroupCapability with invalid arguments. 1262 */ 1263 @Test 1264 public void testGetGroupCapability_invalidArguments() throws Exception { 1265 executeAndValidateInitializationSequence(false, false, false); 1266 1267 doAnswer(new AnswerWithArguments() { 1268 public void answer(byte[] address, ISupplicantP2pIface.getGroupCapabilityCallback cb) { 1269 cb.onValues(mStatusSuccess, 0); 1270 } 1271 }) 1272 .when(mISupplicantP2pIfaceMock) 1273 .getGroupCapability( 1274 eq(mPeerMacAddressBytes), 1275 any(ISupplicantP2pIface.getGroupCapabilityCallback.class)); 1276 1277 for (String address : mInvalidMacAddresses) { 1278 assertEquals(-1, mDut.getGroupCapability(address)); 1279 } 1280 } 1281 1282 /** 1283 * Verify that getGroupCapability returns false, if status is not SUCCESS. 1284 */ 1285 @Test 1286 public void testGetGroupCapability_failure() throws Exception { 1287 executeAndValidateInitializationSequence(false, false, false); 1288 1289 doAnswer(new AnswerWithArguments() { 1290 public void answer(byte[] address, ISupplicantP2pIface.getGroupCapabilityCallback cb) { 1291 cb.onValues(mStatusFailure, 0); 1292 } 1293 }) 1294 .when(mISupplicantP2pIfaceMock) 1295 .getGroupCapability( 1296 eq(mPeerMacAddressBytes), 1297 any(ISupplicantP2pIface.getGroupCapabilityCallback.class)); 1298 1299 assertEquals(-1, mDut.getGroupCapability(mPeerMacAddress)); 1300 // Check that service is still alive. 1301 assertTrue(mDut.isInitializationComplete()); 1302 } 1303 1304 /** 1305 * Verify that getGroupCapability disconnects and returns false, if HAL throws exception. 1306 */ 1307 @Test 1308 public void testGetGroupCapability_exception() throws Exception { 1309 executeAndValidateInitializationSequence(false, false, false); 1310 doThrow(mRemoteException) 1311 .when(mISupplicantP2pIfaceMock) 1312 .getGroupCapability( 1313 eq(mPeerMacAddressBytes), 1314 any(ISupplicantP2pIface.getGroupCapabilityCallback.class)); 1315 assertEquals(-1, mDut.getGroupCapability(mPeerMacAddress)); 1316 // Check service is dead. 1317 assertFalse(mDut.isInitializationComplete()); 1318 } 1319 1320 1321 /** 1322 * Sunny day scenario for configureExtListen() 1323 */ 1324 @Test 1325 public void testConfigureExtListen_success() throws Exception { 1326 when(mISupplicantP2pIfaceMock.configureExtListen(eq(123), eq(456))) 1327 .thenReturn(mStatusSuccess); 1328 when(mISupplicantP2pIfaceMock.configureExtListen(eq(0), eq(0))) 1329 .thenReturn(mStatusSuccess); 1330 // Default value when service is not initialized. 1331 assertFalse(mDut.configureExtListen(true, 123, 456)); 1332 executeAndValidateInitializationSequence(false, false, false); 1333 assertTrue(mDut.configureExtListen(true, 123, 456)); 1334 // Turning listening off should reset intervals to 0s. 1335 assertTrue(mDut.configureExtListen(false, 999, 999)); 1336 // Disable listening. 1337 assertTrue(mDut.configureExtListen(false, -1, -1)); 1338 } 1339 1340 /** 1341 * Test configureExtListen with invalid parameters. 1342 */ 1343 @Test 1344 public void testConfigureExtListen_invalidArguments() throws Exception { 1345 executeAndValidateInitializationSequence(false, false, false); 1346 when(mISupplicantP2pIfaceMock.configureExtListen(anyInt(), anyInt())) 1347 .thenReturn(mStatusFailure); 1348 assertFalse(mDut.configureExtListen(true, -1, 1)); 1349 assertFalse(mDut.configureExtListen(true, 1, -1)); 1350 } 1351 1352 /** 1353 * Verify that configureExtListen returns false, if status is not SUCCESS. 1354 */ 1355 @Test 1356 public void testConfigureExtListen_failure() throws Exception { 1357 executeAndValidateInitializationSequence(false, false, false); 1358 when(mISupplicantP2pIfaceMock.configureExtListen(anyInt(), anyInt())) 1359 .thenReturn(mStatusFailure); 1360 assertFalse(mDut.configureExtListen(true, 1, 1)); 1361 // Check that service is still alive. 1362 assertTrue(mDut.isInitializationComplete()); 1363 } 1364 1365 /** 1366 * Verify that configureExtListen disconnects and returns false, if HAL throws exception. 1367 */ 1368 @Test 1369 public void testConfigureExtListen_exception() throws Exception { 1370 executeAndValidateInitializationSequence(false, false, false); 1371 when(mISupplicantP2pIfaceMock.configureExtListen(anyInt(), anyInt())) 1372 .thenThrow(mRemoteException); 1373 assertFalse(mDut.configureExtListen(true, 1, 1)); 1374 // Check service is dead. 1375 assertFalse(mDut.isInitializationComplete()); 1376 } 1377 1378 1379 /** 1380 * Sunny day scenario for setListenChannel() 1381 */ 1382 @Test 1383 public void testSetListenChannel_success() throws Exception { 1384 when(mISupplicantP2pIfaceMock.setListenChannel(eq(123), eq(456))) 1385 .thenReturn(mStatusSuccess); 1386 // Default value when service is not initialized. 1387 assertFalse(mDut.setListenChannel(123, 456)); 1388 executeAndValidateInitializationSequence(false, false, false); 1389 assertTrue(mDut.setListenChannel(123, 456)); 1390 } 1391 1392 /** 1393 * Test setListenChannel with invalid parameters. 1394 */ 1395 @Test 1396 public void testSetListenChannel_invalidArguments() throws Exception { 1397 executeAndValidateInitializationSequence(false, false, false); 1398 when(mISupplicantP2pIfaceMock.setListenChannel(anyInt(), anyInt())) 1399 .thenReturn(mStatusSuccess); 1400 assertFalse(mDut.setListenChannel(-1, 1)); 1401 assertFalse(mDut.setListenChannel(1, -1)); 1402 } 1403 1404 /** 1405 * Verify that setListenChannel returns false, if status is not SUCCESS. 1406 */ 1407 @Test 1408 public void testSetListenChannel_failure() throws Exception { 1409 executeAndValidateInitializationSequence(false, false, false); 1410 when(mISupplicantP2pIfaceMock.setListenChannel(anyInt(), anyInt())) 1411 .thenReturn(mStatusFailure); 1412 assertFalse(mDut.setListenChannel(1, 1)); 1413 // Check that service is still alive. 1414 assertTrue(mDut.isInitializationComplete()); 1415 } 1416 1417 /** 1418 * Verify that setListenChannel disconnects and returns false, if HAL throws exception. 1419 */ 1420 @Test 1421 public void testSetListenChannel_exception() throws Exception { 1422 executeAndValidateInitializationSequence(false, false, false); 1423 when(mISupplicantP2pIfaceMock.setListenChannel(anyInt(), anyInt())) 1424 .thenThrow(mRemoteException); 1425 assertFalse(mDut.setListenChannel(1, 1)); 1426 // Check service is dead. 1427 assertFalse(mDut.isInitializationComplete()); 1428 } 1429 1430 1431 /** 1432 * Sunny day scenario for serviceAdd() 1433 */ 1434 @Test 1435 public void testServiceAdd_success() throws Exception { 1436 WifiP2pServiceInfo info = createDummyP2pServiceInfo( 1437 mValidUpnpService, mValidBonjourService); 1438 final HashSet<String> services = new HashSet<String>(); 1439 1440 doAnswer(new AnswerWithArguments() { 1441 public SupplicantStatus answer(int version, String name) { 1442 services.add("upnp"); 1443 assertEquals(mValidUpnpServiceVersion, version); 1444 assertEquals(mValidUpnpServiceName, name); 1445 return mStatusSuccess; 1446 } 1447 }) 1448 .when(mISupplicantP2pIfaceMock).addUpnpService(anyInt(), anyString()); 1449 1450 doAnswer(new AnswerWithArguments() { 1451 public SupplicantStatus answer(ArrayList<Byte> request, ArrayList<Byte> response) { 1452 services.add("bonjour"); 1453 assertEquals(mValidBonjourServiceRequest, request); 1454 assertEquals(mValidBonjourServiceResponse, response); 1455 return mStatusSuccess; 1456 } 1457 }) 1458 .when(mISupplicantP2pIfaceMock).addBonjourService( 1459 any(ArrayList.class), any(ArrayList.class)); 1460 1461 // Default value when service is not initialized. 1462 assertFalse(mDut.serviceAdd(info)); 1463 executeAndValidateInitializationSequence(false, false, false); 1464 assertTrue(mDut.serviceAdd(info)); 1465 // Confirm that both services have been added. 1466 assertTrue(services.contains("upnp")); 1467 assertTrue(services.contains("bonjour")); 1468 1469 // Empty services should cause no trouble. 1470 assertTrue(mDut.serviceAdd(createDummyP2pServiceInfo())); 1471 } 1472 1473 /** 1474 * Test serviceAdd with invalid parameters. 1475 */ 1476 @Test 1477 public void testServiceAdd_invalidArguments() throws Exception { 1478 executeAndValidateInitializationSequence(false, false, false); 1479 1480 when(mISupplicantP2pIfaceMock.addUpnpService(anyInt(), anyString())) 1481 .thenReturn(mStatusSuccess); 1482 when(mISupplicantP2pIfaceMock.addBonjourService( 1483 any(ArrayList.class), any(ArrayList.class))) 1484 .thenReturn(mStatusSuccess); 1485 1486 assertFalse(mDut.serviceAdd(null)); 1487 assertFalse(mDut.serviceAdd(createDummyP2pServiceInfo(mInvalidService1))); 1488 assertFalse(mDut.serviceAdd(createDummyP2pServiceInfo(mInvalidService2))); 1489 assertFalse(mDut.serviceAdd(createDummyP2pServiceInfo(mInvalidUpnpService1))); 1490 assertFalse(mDut.serviceAdd(createDummyP2pServiceInfo(mInvalidUpnpService2))); 1491 assertFalse(mDut.serviceAdd(createDummyP2pServiceInfo(mInvalidUpnpService3))); 1492 assertFalse(mDut.serviceAdd(createDummyP2pServiceInfo(mInvalidBonjourService1))); 1493 assertFalse(mDut.serviceAdd(createDummyP2pServiceInfo(mInvalidBonjourService2))); 1494 assertFalse(mDut.serviceAdd(createDummyP2pServiceInfo(mInvalidBonjourService3))); 1495 assertFalse(mDut.serviceAdd(createDummyP2pServiceInfo(mInvalidBonjourService4))); 1496 } 1497 1498 /** 1499 * Verify that serviceAdd returns false, if status is not SUCCESS. 1500 */ 1501 @Test 1502 public void testServiceAdd_failure() throws Exception { 1503 executeAndValidateInitializationSequence(false, false, false); 1504 1505 when(mISupplicantP2pIfaceMock.addUpnpService(anyInt(), anyString())) 1506 .thenReturn(mStatusFailure); 1507 when(mISupplicantP2pIfaceMock.addBonjourService( 1508 any(ArrayList.class), any(ArrayList.class))) 1509 .thenReturn(mStatusFailure); 1510 1511 assertFalse(mDut.serviceAdd(createDummyP2pServiceInfo(mValidUpnpService))); 1512 assertFalse(mDut.serviceAdd(createDummyP2pServiceInfo(mValidBonjourService))); 1513 1514 // Check that service is still alive. 1515 assertTrue(mDut.isInitializationComplete()); 1516 } 1517 1518 /** 1519 * Verify that serviceAdd disconnects and returns false, if HAL throws exception. 1520 */ 1521 @Test 1522 public void testServiceAdd_exception() throws Exception { 1523 executeAndValidateInitializationSequence(false, false, false); 1524 1525 when(mISupplicantP2pIfaceMock.addUpnpService(anyInt(), anyString())) 1526 .thenThrow(mRemoteException); 1527 assertFalse(mDut.serviceAdd(createDummyP2pServiceInfo(mValidUpnpService))); 1528 // Check service is dead. 1529 assertFalse(mDut.isInitializationComplete()); 1530 1531 executeAndValidateInitializationSequence(false, false, false); 1532 when(mISupplicantP2pIfaceMock.addBonjourService( 1533 any(ArrayList.class), any(ArrayList.class))) 1534 .thenThrow(mRemoteException); 1535 assertFalse(mDut.serviceAdd(createDummyP2pServiceInfo(mValidBonjourService))); 1536 // Check service is dead. 1537 assertFalse(mDut.isInitializationComplete()); 1538 } 1539 1540 1541 /** 1542 * Sunny day scenario for serviceRemove() 1543 */ 1544 @Test 1545 public void testServiceRemove_success() throws Exception { 1546 WifiP2pServiceInfo info = createDummyP2pServiceInfo( 1547 mValidUpnpService, mValidBonjourService); 1548 final HashSet<String> services = new HashSet<String>(); 1549 1550 doAnswer(new AnswerWithArguments() { 1551 public SupplicantStatus answer(int version, String name) { 1552 services.add("upnp"); 1553 assertEquals(mValidUpnpServiceVersion, version); 1554 assertEquals(mValidUpnpServiceName, name); 1555 return mStatusSuccess; 1556 } 1557 }) 1558 .when(mISupplicantP2pIfaceMock).removeUpnpService(anyInt(), anyString()); 1559 1560 doAnswer(new AnswerWithArguments() { 1561 public SupplicantStatus answer(ArrayList<Byte> request) { 1562 services.add("bonjour"); 1563 assertEquals(mValidBonjourServiceRequest, request); 1564 return mStatusSuccess; 1565 } 1566 }) 1567 .when(mISupplicantP2pIfaceMock).removeBonjourService(any(ArrayList.class)); 1568 1569 // Default value when service is not initialized. 1570 assertFalse(mDut.serviceRemove(info)); 1571 executeAndValidateInitializationSequence(false, false, false); 1572 assertTrue(mDut.serviceRemove(info)); 1573 // Confirm that both services have been removed. 1574 assertTrue(services.contains("upnp")); 1575 assertTrue(services.contains("bonjour")); 1576 1577 // Empty services should cause no trouble. 1578 assertTrue(mDut.serviceRemove(createDummyP2pServiceInfo())); 1579 } 1580 1581 /** 1582 * Test serviceRemove with invalid parameters. 1583 */ 1584 @Test 1585 public void testServiceRemove_invalidArguments() throws Exception { 1586 executeAndValidateInitializationSequence(false, false, false); 1587 1588 when(mISupplicantP2pIfaceMock.removeUpnpService(anyInt(), anyString())) 1589 .thenReturn(mStatusSuccess); 1590 when(mISupplicantP2pIfaceMock.removeBonjourService(any(ArrayList.class))) 1591 .thenReturn(mStatusSuccess); 1592 1593 assertFalse(mDut.serviceRemove(null)); 1594 assertFalse(mDut.serviceRemove(createDummyP2pServiceInfo(mInvalidService1))); 1595 assertFalse(mDut.serviceRemove(createDummyP2pServiceInfo(mInvalidService2))); 1596 assertFalse(mDut.serviceRemove(createDummyP2pServiceInfo(mInvalidUpnpService1))); 1597 assertFalse(mDut.serviceRemove(createDummyP2pServiceInfo(mInvalidUpnpService2))); 1598 assertFalse(mDut.serviceRemove(createDummyP2pServiceInfo(mInvalidUpnpService3))); 1599 assertFalse(mDut.serviceRemove(createDummyP2pServiceInfo(mInvalidBonjourService1))); 1600 assertFalse(mDut.serviceRemove(createDummyP2pServiceInfo(mInvalidBonjourService2))); 1601 assertFalse(mDut.serviceRemove(createDummyP2pServiceInfo(mInvalidBonjourService3))); 1602 // Response parameter is ignored by serviceRemove call, hence the following would pass. 1603 // The production code would need to parse otherwise redundant parameter to fail on this 1604 // one. 1605 // 1606 // assertFalse(mDut.serviceRemove(createDummyP2pServiceInfo(mInvalidBonjourService4))); 1607 } 1608 1609 /** 1610 * Verify that serviceRemove returns false, if status is not SUCCESS. 1611 */ 1612 @Test 1613 public void testServiceRemove_failure() throws Exception { 1614 executeAndValidateInitializationSequence(false, false, false); 1615 1616 when(mISupplicantP2pIfaceMock.removeUpnpService(anyInt(), anyString())) 1617 .thenReturn(mStatusFailure); 1618 when(mISupplicantP2pIfaceMock.removeBonjourService(any(ArrayList.class))) 1619 .thenReturn(mStatusFailure); 1620 1621 assertFalse(mDut.serviceRemove(createDummyP2pServiceInfo(mValidUpnpService))); 1622 assertFalse(mDut.serviceRemove(createDummyP2pServiceInfo(mValidBonjourService))); 1623 1624 // Check that service is still alive. 1625 assertTrue(mDut.isInitializationComplete()); 1626 } 1627 1628 /** 1629 * Verify that serviceRemove disconnects and returns false, if HAL throws exception. 1630 */ 1631 @Test 1632 public void testServiceRemove_exception() throws Exception { 1633 executeAndValidateInitializationSequence(false, false, false); 1634 1635 when(mISupplicantP2pIfaceMock.removeUpnpService(anyInt(), anyString())) 1636 .thenThrow(mRemoteException); 1637 assertFalse(mDut.serviceRemove(createDummyP2pServiceInfo(mValidUpnpService))); 1638 // Check service is dead. 1639 assertFalse(mDut.isInitializationComplete()); 1640 1641 executeAndValidateInitializationSequence(false, false, false); 1642 when(mISupplicantP2pIfaceMock.removeBonjourService(any(ArrayList.class))) 1643 .thenThrow(mRemoteException); 1644 assertFalse(mDut.serviceRemove(createDummyP2pServiceInfo(mValidBonjourService))); 1645 // Check service is dead. 1646 assertFalse(mDut.isInitializationComplete()); 1647 } 1648 1649 1650 /** 1651 * Sunny day scenario for requestServiceDiscovery() 1652 */ 1653 @Test 1654 public void testRequestServiceDiscovery_success() throws Exception { 1655 final int caps = 123; 1656 1657 doAnswer(new AnswerWithArguments() { 1658 public void answer(byte[] address, ArrayList<Byte> query, 1659 ISupplicantP2pIface.requestServiceDiscoveryCallback cb) { 1660 cb.onValues(mStatusSuccess, 1234); 1661 } 1662 }) 1663 .when(mISupplicantP2pIfaceMock) 1664 .requestServiceDiscovery( 1665 eq(mPeerMacAddressBytes), 1666 eq(mValidBonjourServiceRequest), 1667 any(ISupplicantP2pIface.requestServiceDiscoveryCallback.class)); 1668 1669 // Default value when service is not initialized. 1670 assertNull(mDut.requestServiceDiscovery(mPeerMacAddress, mValidServiceRequestString)); 1671 1672 executeAndValidateInitializationSequence(false, false, false); 1673 assertEquals("1234", mDut.requestServiceDiscovery( 1674 mPeerMacAddress, mValidServiceRequestString)); 1675 } 1676 1677 /** 1678 * RequestServiceDiscovery with invalid arguments. 1679 */ 1680 @Test 1681 public void testRequestServiceDiscovery_invalidArguments() throws Exception { 1682 executeAndValidateInitializationSequence(false, false, false); 1683 1684 doAnswer(new AnswerWithArguments() { 1685 public void answer(byte[] address, ArrayList<Byte> query, 1686 ISupplicantP2pIface.requestServiceDiscoveryCallback cb) { 1687 cb.onValues(mStatusSuccess, 0); 1688 } 1689 }) 1690 .when(mISupplicantP2pIfaceMock) 1691 .requestServiceDiscovery( 1692 any(byte[].class), any(ArrayList.class), 1693 any(ISupplicantP2pIface.requestServiceDiscoveryCallback.class)); 1694 1695 for (String address : mInvalidMacAddresses) { 1696 assertNull(mDut.requestServiceDiscovery( 1697 address, mValidServiceRequestString)); 1698 } 1699 assertNull(mDut.requestServiceDiscovery(mPeerMacAddress, null)); 1700 assertNull(mDut.requestServiceDiscovery(mPeerMacAddress, mInvalidServiceRequestString)); 1701 } 1702 1703 /** 1704 * Verify that requestServiceDiscovery returns false, if status is not SUCCESS. 1705 */ 1706 @Test 1707 public void testRequestServiceDiscovery_failure() throws Exception { 1708 executeAndValidateInitializationSequence(false, false, false); 1709 1710 doAnswer(new AnswerWithArguments() { 1711 public void answer( 1712 byte[] address, ArrayList<Byte> query, 1713 ISupplicantP2pIface.requestServiceDiscoveryCallback cb) { 1714 cb.onValues(mStatusFailure, 0); 1715 } 1716 }) 1717 .when(mISupplicantP2pIfaceMock) 1718 .requestServiceDiscovery( 1719 any(byte[].class), any(ArrayList.class), 1720 any(ISupplicantP2pIface.requestServiceDiscoveryCallback.class)); 1721 1722 assertNull(mDut.requestServiceDiscovery(mPeerMacAddress, mValidServiceRequestString)); 1723 // Check that service is still alive. 1724 assertTrue(mDut.isInitializationComplete()); 1725 } 1726 1727 /** 1728 * Verify that requestServiceDiscovery disconnects and returns false, if HAL throws exception. 1729 */ 1730 @Test 1731 public void testRequestServiceDiscovery_exception() throws Exception { 1732 executeAndValidateInitializationSequence(false, false, false); 1733 doThrow(mRemoteException) 1734 .when(mISupplicantP2pIfaceMock) 1735 .requestServiceDiscovery( 1736 any(byte[].class), any(ArrayList.class), 1737 any(ISupplicantP2pIface.requestServiceDiscoveryCallback.class)); 1738 assertNull(mDut.requestServiceDiscovery(mPeerMacAddress, mValidServiceRequestString)); 1739 // Check service is dead. 1740 assertFalse(mDut.isInitializationComplete()); 1741 } 1742 1743 // Test constant used in cancelServiceDiscovery tests 1744 static final String SERVICE_IDENTIFIER_STR = "521918410304"; 1745 static final long SERVICE_IDENTIFIER_LONG = 521918410304L; 1746 1747 /** 1748 * Sunny day scenario for cancelServiceDiscovery() 1749 */ 1750 @Test 1751 public void testCancelServiceDiscovery_success() throws Exception { 1752 when(mISupplicantP2pIfaceMock.cancelServiceDiscovery(SERVICE_IDENTIFIER_LONG)) 1753 .thenReturn(mStatusSuccess); 1754 // Default value when service is not initialized. 1755 assertFalse(mDut.cancelServiceDiscovery(SERVICE_IDENTIFIER_STR)); 1756 executeAndValidateInitializationSequence(false, false, false); 1757 assertTrue(mDut.cancelServiceDiscovery(SERVICE_IDENTIFIER_STR)); 1758 } 1759 1760 /** 1761 * Test cancelServiceDiscovery with invalid parameters. 1762 */ 1763 @Test 1764 public void testCancelServiceDiscovery_invalidArguments() throws Exception { 1765 executeAndValidateInitializationSequence(false, false, false); 1766 when(mISupplicantP2pIfaceMock.cancelServiceDiscovery(anyLong())) 1767 .thenReturn(mStatusFailure); 1768 assertFalse(mDut.cancelServiceDiscovery(null)); 1769 assertFalse(mDut.cancelServiceDiscovery("not a number")); 1770 } 1771 1772 /** 1773 * Verify that cancelServiceDiscovery returns false, if status is not SUCCESS. 1774 */ 1775 @Test 1776 public void testCancelServiceDiscovery_failure() throws Exception { 1777 executeAndValidateInitializationSequence(false, false, false); 1778 when(mISupplicantP2pIfaceMock.cancelServiceDiscovery(anyLong())) 1779 .thenReturn(mStatusFailure); 1780 assertFalse(mDut.cancelServiceDiscovery(SERVICE_IDENTIFIER_STR)); 1781 // Check that service is still alive. 1782 assertTrue(mDut.isInitializationComplete()); 1783 } 1784 1785 /** 1786 * Verify that cancelServiceDiscovery disconnects and returns false, if HAL throws exception. 1787 */ 1788 @Test 1789 public void testCancelServiceDiscovery_exception() throws Exception { 1790 executeAndValidateInitializationSequence(false, false, false); 1791 when(mISupplicantP2pIfaceMock.cancelServiceDiscovery(anyLong())) 1792 .thenThrow(mRemoteException); 1793 assertFalse(mDut.cancelServiceDiscovery(SERVICE_IDENTIFIER_STR)); 1794 // Check service is dead. 1795 assertFalse(mDut.isInitializationComplete()); 1796 } 1797 1798 1799 /** 1800 * Sunny day scenario for setMiracastMode() 1801 */ 1802 @Test 1803 public void testSetMiracastMode_success() throws Exception { 1804 HashSet<Byte> modes = new HashSet<Byte>(); 1805 1806 when(mISupplicantP2pIfaceMock.setMiracastMode(anyByte())) 1807 .thenAnswer(new AnswerWithArguments() { 1808 public SupplicantStatus answer(byte mode) { 1809 modes.add(mode); 1810 return mStatusSuccess; 1811 } 1812 }); 1813 // Default value when service is not initialized. 1814 assertFalse(mDut.setMiracastMode(WifiP2pManager.MIRACAST_SOURCE)); 1815 executeAndValidateInitializationSequence(false, false, false); 1816 assertTrue(mDut.setMiracastMode(WifiP2pManager.MIRACAST_SOURCE)); 1817 assertTrue(modes.contains(ISupplicantP2pIface.MiracastMode.SOURCE)); 1818 1819 assertTrue(mDut.setMiracastMode(WifiP2pManager.MIRACAST_SINK)); 1820 assertTrue(modes.contains(ISupplicantP2pIface.MiracastMode.SINK)); 1821 1822 // Any invalid number yields disabled miracast mode. 1823 assertTrue(mDut.setMiracastMode(-1)); 1824 assertTrue(modes.contains(ISupplicantP2pIface.MiracastMode.DISABLED)); 1825 } 1826 1827 /** 1828 * Verify that setMiracastMode returns false, if status is not SUCCESS. 1829 */ 1830 @Test 1831 public void testSetMiracastMode_failure() throws Exception { 1832 executeAndValidateInitializationSequence(false, false, false); 1833 when(mISupplicantP2pIfaceMock.setMiracastMode(anyByte())) 1834 .thenReturn(mStatusFailure); 1835 assertFalse(mDut.setMiracastMode(WifiP2pManager.MIRACAST_SOURCE)); 1836 // Check that service is still alive. 1837 assertTrue(mDut.isInitializationComplete()); 1838 } 1839 1840 /** 1841 * Verify that setMiracastMode disconnects and returns false, if HAL throws exception. 1842 */ 1843 @Test 1844 public void testSetMiracastMode_exception() throws Exception { 1845 executeAndValidateInitializationSequence(false, false, false); 1846 when(mISupplicantP2pIfaceMock.setMiracastMode(anyByte())) 1847 .thenThrow(mRemoteException); 1848 assertFalse(mDut.setMiracastMode(WifiP2pManager.MIRACAST_SOURCE)); 1849 // Check service is dead. 1850 assertFalse(mDut.isInitializationComplete()); 1851 } 1852 1853 1854 /** 1855 * Sunny day scenario for startWpsPbc() 1856 */ 1857 @Test 1858 public void testStartWpsPbc_success() throws Exception { 1859 when(mISupplicantP2pIfaceMock.startWpsPbc(eq(mIfaceName), eq(mPeerMacAddressBytes))) 1860 .thenReturn(mStatusSuccess); 1861 // Default value when service is not initialized. 1862 assertFalse(mDut.startWpsPbc(mIfaceName, mPeerMacAddress)); 1863 executeAndValidateInitializationSequence(false, false, false); 1864 assertTrue(mDut.startWpsPbc(mIfaceName, mPeerMacAddress)); 1865 } 1866 1867 /** 1868 * StartWpsPbc with invalid arguments. 1869 */ 1870 @Test 1871 public void testStartWpsPbc_invalidArguments() throws Exception { 1872 executeAndValidateInitializationSequence(false, false, false); 1873 when(mISupplicantP2pIfaceMock.startWpsPbc(anyString(), any(byte[].class))) 1874 .thenReturn(mStatusSuccess); 1875 1876 for (String address : mInvalidMacAddresses) { 1877 assertFalse(mDut.startWpsPbc(mIfaceName, address)); 1878 } 1879 1880 assertFalse(mDut.startWpsPbc(null, mPeerMacAddress)); 1881 } 1882 1883 /** 1884 * Verify that startWpsPbc returns false, if status is not SUCCESS. 1885 */ 1886 @Test 1887 public void testStartWpsPbc_failure() throws Exception { 1888 executeAndValidateInitializationSequence(false, false, false); 1889 when(mISupplicantP2pIfaceMock.startWpsPbc(anyString(), any(byte[].class))) 1890 .thenReturn(mStatusFailure); 1891 assertFalse(mDut.startWpsPbc(mIfaceName, mPeerMacAddress)); 1892 // Check that service is still alive. 1893 assertTrue(mDut.isInitializationComplete()); 1894 } 1895 1896 /** 1897 * Verify that startWpsPbc disconnects and returns false, if HAL throws exception. 1898 */ 1899 @Test 1900 public void testStartWpsPbc_exception() throws Exception { 1901 executeAndValidateInitializationSequence(false, false, false); 1902 when(mISupplicantP2pIfaceMock.startWpsPbc(anyString(), any(byte[].class))) 1903 .thenThrow(mRemoteException); 1904 assertFalse(mDut.startWpsPbc(mIfaceName, mPeerMacAddress)); 1905 // Check service is dead. 1906 assertFalse(mDut.isInitializationComplete()); 1907 } 1908 1909 1910 /** 1911 * Sunny day scenario for startWpsPinKeypad() 1912 */ 1913 @Test 1914 public void testStartWpsPinKeypad_success() throws Exception { 1915 when(mISupplicantP2pIfaceMock.startWpsPinKeypad(eq(mIfaceName), eq("1234"))) 1916 .thenReturn(mStatusSuccess); 1917 // Default value when service is not initialized. 1918 assertFalse(mDut.startWpsPinKeypad(mIfaceName, "1234")); 1919 executeAndValidateInitializationSequence(false, false, false); 1920 assertTrue(mDut.startWpsPinKeypad(mIfaceName, "1234")); 1921 } 1922 1923 /** 1924 * StartWpsPinKeypad with invalid arguments. 1925 */ 1926 @Test 1927 public void testStartWpsPinKeypad_invalidArguments() throws Exception { 1928 executeAndValidateInitializationSequence(false, false, false); 1929 when(mISupplicantP2pIfaceMock.startWpsPinKeypad(anyString(), anyString())) 1930 .thenReturn(mStatusSuccess); 1931 1932 assertFalse(mDut.startWpsPinKeypad(null, "1234")); 1933 assertFalse(mDut.startWpsPinKeypad(mIfaceName, null)); 1934 // StartWpsPinPinKeypad does not validate, that PIN indeed holds an integer encoded in a 1935 // string. This code would be redundant, as HAL requires string to be passed. 1936 } 1937 1938 /** 1939 * Verify that startWpsPinKeypad returns false, if status is not SUCCESS. 1940 */ 1941 @Test 1942 public void testStartWpsPinKeypad_failure() throws Exception { 1943 executeAndValidateInitializationSequence(false, false, false); 1944 when(mISupplicantP2pIfaceMock.startWpsPinKeypad(anyString(), anyString())) 1945 .thenReturn(mStatusFailure); 1946 assertFalse(mDut.startWpsPinKeypad(mIfaceName, "1234")); 1947 // Check that service is still alive. 1948 assertTrue(mDut.isInitializationComplete()); 1949 } 1950 1951 /** 1952 * Verify that startWpsPinKeypad disconnects and returns false, if HAL throws exception. 1953 */ 1954 @Test 1955 public void testStartWpsPinKeypad_exception() throws Exception { 1956 executeAndValidateInitializationSequence(false, false, false); 1957 when(mISupplicantP2pIfaceMock.startWpsPinKeypad(anyString(), anyString())) 1958 .thenThrow(mRemoteException); 1959 assertFalse(mDut.startWpsPinKeypad(mIfaceName, "1234")); 1960 // Check service is dead. 1961 assertFalse(mDut.isInitializationComplete()); 1962 } 1963 1964 1965 /** 1966 * Sunny day scenario for startWpsPinDisplay() 1967 */ 1968 @Test 1969 public void testStartWpsPinDisplay_success() throws Exception { 1970 doAnswer(new AnswerWithArguments() { 1971 public void answer(String ifName, byte[] bssid, 1972 ISupplicantP2pIface.startWpsPinDisplayCallback cb) { 1973 cb.onValues(mStatusSuccess, "1234"); 1974 } 1975 }) 1976 .when(mISupplicantP2pIfaceMock).startWpsPinDisplay( 1977 eq(mIfaceName), eq(mPeerMacAddressBytes), 1978 any(ISupplicantP2pIface.startWpsPinDisplayCallback.class)); 1979 1980 // Default value when service is not initialized. 1981 assertNull(mDut.startWpsPinDisplay(mIfaceName, mPeerMacAddress)); 1982 executeAndValidateInitializationSequence(false, false, false); 1983 assertEquals("1234", mDut.startWpsPinDisplay(mIfaceName, mPeerMacAddress)); 1984 } 1985 1986 /** 1987 * StartWpsPinDisplay with invalid arguments. 1988 */ 1989 @Test 1990 public void testStartWpsPinDisplay_invalidArguments() throws Exception { 1991 executeAndValidateInitializationSequence(false, false, false); 1992 doAnswer(new AnswerWithArguments() { 1993 public void answer(String ifName, byte[] bssid, 1994 ISupplicantP2pIface.startWpsPinDisplayCallback cb) { 1995 cb.onValues(mStatusSuccess, "1234"); 1996 } 1997 }) 1998 .when(mISupplicantP2pIfaceMock).startWpsPinDisplay( 1999 anyString(), any(byte[].class), 2000 any(ISupplicantP2pIface.startWpsPinDisplayCallback.class)); 2001 2002 for (String address : mInvalidMacAddresses) { 2003 assertNull(mDut.startWpsPinDisplay(mIfaceName, address)); 2004 } 2005 2006 assertNull(mDut.startWpsPinDisplay(null, mPeerMacAddress)); 2007 } 2008 2009 /** 2010 * Verify that startWpsPinDisplay returns false, if status is not SUCCESS. 2011 */ 2012 @Test 2013 public void testStartWpsPinDisplay_failure() throws Exception { 2014 executeAndValidateInitializationSequence(false, false, false); 2015 doAnswer(new AnswerWithArguments() { 2016 public void answer(String ifName, byte[] bssid, 2017 ISupplicantP2pIface.startWpsPinDisplayCallback cb) { 2018 cb.onValues(mStatusFailure, "1234"); 2019 } 2020 }) 2021 .when(mISupplicantP2pIfaceMock).startWpsPinDisplay( 2022 anyString(), any(byte[].class), 2023 any(ISupplicantP2pIface.startWpsPinDisplayCallback.class)); 2024 2025 assertNull(mDut.startWpsPinDisplay(mIfaceName, mPeerMacAddress)); 2026 // Check that service is still alive. 2027 assertTrue(mDut.isInitializationComplete()); 2028 } 2029 2030 /** 2031 * Verify that startWpsPinDisplay disconnects and returns false, if HAL throws exception. 2032 */ 2033 @Test 2034 public void testStartWpsPinDisplay_exception() throws Exception { 2035 executeAndValidateInitializationSequence(false, false, false); 2036 doThrow(mRemoteException) 2037 .when(mISupplicantP2pIfaceMock).startWpsPinDisplay( 2038 anyString(), any(byte[].class), 2039 any(ISupplicantP2pIface.startWpsPinDisplayCallback.class)); 2040 assertNull(mDut.startWpsPinDisplay(mIfaceName, mPeerMacAddress)); 2041 // Check service is dead. 2042 assertFalse(mDut.isInitializationComplete()); 2043 } 2044 2045 2046 /** 2047 * Sunny day scenario for cancelWps() 2048 */ 2049 @Test 2050 public void testCancelWps_success() throws Exception { 2051 when(mISupplicantP2pIfaceMock.cancelWps(eq(mIfaceName))) 2052 .thenReturn(mStatusSuccess); 2053 // Default value when service is not initialized. 2054 assertFalse(mDut.cancelWps(mIfaceName)); 2055 executeAndValidateInitializationSequence(false, false, false); 2056 assertTrue(mDut.cancelWps(mIfaceName)); 2057 } 2058 2059 /** 2060 * CancelWps with invalid arguments. 2061 */ 2062 @Test 2063 public void testCancelWps_invalidArguments() throws Exception { 2064 executeAndValidateInitializationSequence(false, false, false); 2065 when(mISupplicantP2pIfaceMock.cancelWps(anyString())) 2066 .thenReturn(mStatusSuccess); 2067 2068 assertFalse(mDut.cancelWps(null)); 2069 } 2070 2071 /** 2072 * Verify that cancelWps returns false, if status is not SUCCESS. 2073 */ 2074 @Test 2075 public void testCancelWps_failure() throws Exception { 2076 executeAndValidateInitializationSequence(false, false, false); 2077 when(mISupplicantP2pIfaceMock.cancelWps(anyString())) 2078 .thenReturn(mStatusFailure); 2079 assertFalse(mDut.cancelWps(mIfaceName)); 2080 // Check that service is still alive. 2081 assertTrue(mDut.isInitializationComplete()); 2082 } 2083 2084 /** 2085 * Verify that cancelWps disconnects and returns false, if HAL throws exception. 2086 */ 2087 @Test 2088 public void testCancelWps_exception() throws Exception { 2089 executeAndValidateInitializationSequence(false, false, false); 2090 when(mISupplicantP2pIfaceMock.cancelWps(anyString())) 2091 .thenThrow(mRemoteException); 2092 assertFalse(mDut.cancelWps(mIfaceName)); 2093 // Check service is dead. 2094 assertFalse(mDut.isInitializationComplete()); 2095 } 2096 2097 2098 /** 2099 * Sunny day scenario for enableWfd() 2100 */ 2101 @Test 2102 public void testEnableWfd_success() throws Exception { 2103 when(mISupplicantP2pIfaceMock.enableWfd(eq(true))) 2104 .thenReturn(mStatusSuccess); 2105 // Default value when service is not initialized. 2106 assertFalse(mDut.enableWfd(true)); 2107 executeAndValidateInitializationSequence(false, false, false); 2108 assertTrue(mDut.enableWfd(true)); 2109 } 2110 2111 /** 2112 * Verify that enableWfd returns false, if status is not SUCCESS. 2113 */ 2114 @Test 2115 public void testEnableWfd_failure() throws Exception { 2116 executeAndValidateInitializationSequence(false, false, false); 2117 when(mISupplicantP2pIfaceMock.enableWfd(anyBoolean())) 2118 .thenReturn(mStatusFailure); 2119 assertFalse(mDut.enableWfd(true)); 2120 // Check that service is still alive. 2121 assertTrue(mDut.isInitializationComplete()); 2122 } 2123 2124 /** 2125 * Verify that enableWfd disconnects and returns false, if HAL throws exception. 2126 */ 2127 @Test 2128 public void testEnableWfd_exception() throws Exception { 2129 executeAndValidateInitializationSequence(false, false, false); 2130 when(mISupplicantP2pIfaceMock.enableWfd(anyBoolean())) 2131 .thenThrow(mRemoteException); 2132 assertFalse(mDut.enableWfd(false)); 2133 // Check service is dead. 2134 assertFalse(mDut.isInitializationComplete()); 2135 } 2136 2137 2138 /** 2139 * Sunny day scenario for setWfdDeviceInfo() 2140 */ 2141 @Test 2142 public void testSetWfdDeviceInfo_success() throws Exception { 2143 when(mISupplicantP2pIfaceMock.setWfdDeviceInfo(eq(mValidServiceRequestBytes))) 2144 .thenReturn(mStatusSuccess); 2145 // Default value when service is not initialized. 2146 assertFalse(mDut.setWfdDeviceInfo(mValidServiceRequestString)); 2147 executeAndValidateInitializationSequence(false, false, false); 2148 assertTrue(mDut.setWfdDeviceInfo(mValidServiceRequestString)); 2149 } 2150 2151 /** 2152 * SetWfdDeviceInfo with invalid arguments. 2153 */ 2154 @Test 2155 public void testSetWfdDeviceInfo_invalidArguments() throws Exception { 2156 executeAndValidateInitializationSequence(false, false, false); 2157 when(mISupplicantP2pIfaceMock.setWfdDeviceInfo(any(byte[].class))) 2158 .thenReturn(mStatusSuccess); 2159 2160 assertFalse(mDut.setWfdDeviceInfo(null)); 2161 assertFalse(mDut.setWfdDeviceInfo(mInvalidServiceRequestString)); 2162 } 2163 2164 /** 2165 * Verify that setWfdDeviceInfo returns false, if status is not SUCCESS. 2166 */ 2167 @Test 2168 public void testSetWfdDeviceInfo_failure() throws Exception { 2169 executeAndValidateInitializationSequence(false, false, false); 2170 when(mISupplicantP2pIfaceMock.setWfdDeviceInfo(any(byte[].class))) 2171 .thenReturn(mStatusFailure); 2172 assertFalse(mDut.setWfdDeviceInfo(mValidServiceRequestString)); 2173 // Check that service is still alive. 2174 assertTrue(mDut.isInitializationComplete()); 2175 } 2176 2177 /** 2178 * Verify that setWfdDeviceInfo disconnects and returns false, if HAL throws exception. 2179 */ 2180 @Test 2181 public void testSetWfdDeviceInfo_exception() throws Exception { 2182 executeAndValidateInitializationSequence(false, false, false); 2183 when(mISupplicantP2pIfaceMock.setWfdDeviceInfo(any(byte[].class))) 2184 .thenThrow(mRemoteException); 2185 assertFalse(mDut.setWfdDeviceInfo(mValidServiceRequestString)); 2186 // Check service is dead. 2187 assertFalse(mDut.isInitializationComplete()); 2188 } 2189 2190 /** 2191 * Verify the loading of group info. 2192 * Specifically, all groups returned by listNetworks are added as a persistent group, so long as 2193 * they are NOT current. 2194 */ 2195 @Test 2196 public void testLoadGroups() throws Exception { 2197 executeAndValidateInitializationSequence(false, false, false); 2198 2199 // Class to hold the P2p group info returned from the HIDL interface. 2200 class P2pGroupInfo { 2201 public String ssid; 2202 public byte[] bssid; 2203 public boolean isGo; 2204 public boolean isCurrent; 2205 P2pGroupInfo(String ssid, byte[] bssid, boolean isGo, boolean isCurrent) { 2206 this.ssid = ssid; 2207 this.bssid = bssid; 2208 this.isGo = isGo; 2209 this.isCurrent = isCurrent; 2210 } 2211 } 2212 2213 Map<Integer, P2pGroupInfo> groups = new HashMap<>(); 2214 groups.put(0, new P2pGroupInfo( 2215 "test_34", 2216 NativeUtil.macAddressToByteArray("56:34:ab:12:12:34"), 2217 false, false)); 2218 groups.put(1, new P2pGroupInfo( 2219 "test_1234", 2220 NativeUtil.macAddressToByteArray("16:ed:ab:12:45:34"), 2221 true, false)); 2222 groups.put(2, new P2pGroupInfo( 2223 "test_4545", 2224 NativeUtil.macAddressToByteArray("32:89:23:56:45:34"), 2225 true, false)); 2226 groups.put(3, new P2pGroupInfo( 2227 "iShouldntBeHere", 2228 NativeUtil.macAddressToByteArray("aa:bb:cc:56:45:34"), 2229 true, true)); 2230 2231 doAnswer(new AnswerWithArguments() { 2232 public void answer(ISupplicantP2pIface.listNetworksCallback cb) { 2233 cb.onValues(mStatusSuccess, new ArrayList<Integer>(groups.keySet())); 2234 } 2235 }).when(mISupplicantP2pIfaceMock) 2236 .listNetworks(any(ISupplicantP2pIface.listNetworksCallback.class)); 2237 2238 doAnswer(new AnswerWithArguments() { 2239 public void answer(final int networkId, ISupplicantP2pIface.getNetworkCallback cb) { 2240 try { 2241 doAnswer(new AnswerWithArguments() { 2242 public void answer(ISupplicantP2pNetwork.getSsidCallback cb) { 2243 cb.onValues(mStatusSuccess, 2244 NativeUtil.stringToByteArrayList(groups.get(networkId).ssid)); 2245 return; 2246 } 2247 }).when(mISupplicantP2pNetworkMock) 2248 .getSsid(any(ISupplicantP2pNetwork.getSsidCallback.class)); 2249 doAnswer(new AnswerWithArguments() { 2250 public void answer(ISupplicantP2pNetwork.getBssidCallback cb) { 2251 cb.onValues(mStatusSuccess, groups.get(networkId).bssid); 2252 return; 2253 } 2254 }).when(mISupplicantP2pNetworkMock) 2255 .getBssid(any(ISupplicantP2pNetwork.getBssidCallback.class)); 2256 doAnswer(new AnswerWithArguments() { 2257 public void answer(ISupplicantP2pNetwork.isCurrentCallback cb) { 2258 cb.onValues(mStatusSuccess, groups.get(networkId).isCurrent); 2259 return; 2260 } 2261 }).when(mISupplicantP2pNetworkMock) 2262 .isCurrent(any(ISupplicantP2pNetwork.isCurrentCallback.class)); 2263 doAnswer(new AnswerWithArguments() { 2264 public void answer(ISupplicantP2pNetwork.isGoCallback cb) { 2265 cb.onValues(mStatusSuccess, groups.get(networkId).isGo); 2266 return; 2267 } 2268 }).when(mISupplicantP2pNetworkMock) 2269 .isGo(any(ISupplicantP2pNetwork.isGoCallback.class)); 2270 } catch (RemoteException e) { 2271 } 2272 cb.onValues(mStatusSuccess, mISupplicantP2pNetworkMock); 2273 return; 2274 } 2275 }).when(mISupplicantP2pIfaceMock) 2276 .getNetwork(anyInt(), any(ISupplicantP2pIface.getNetworkCallback.class)); 2277 2278 WifiP2pGroupList p2pGroups = new WifiP2pGroupList(); 2279 assertTrue(mDut.loadGroups(p2pGroups)); 2280 2281 assertEquals(3, p2pGroups.getGroupList().size()); 2282 for (WifiP2pGroup group : p2pGroups.getGroupList()) { 2283 int networkId = group.getNetworkId(); 2284 assertEquals(groups.get(networkId).ssid, group.getNetworkName()); 2285 assertEquals( 2286 NativeUtil.macAddressFromByteArray(groups.get(networkId).bssid), 2287 group.getOwner().deviceAddress); 2288 assertEquals(groups.get(networkId).isGo, group.isGroupOwner()); 2289 } 2290 } 2291 2292 /** 2293 * Sunny day scenario for setClientList() 2294 */ 2295 @Test 2296 public void testSetClientList() throws Exception { 2297 int testNetworkId = 5; 2298 final String client1 = mGroupOwnerMacAddress; 2299 final String client2 = mPeerMacAddress; 2300 2301 executeAndValidateInitializationSequence(false, false, false); 2302 doAnswer(new AnswerWithArguments() { 2303 public void answer(final int networkId, ISupplicantP2pIface.getNetworkCallback cb) { 2304 if (networkId == testNetworkId) { 2305 cb.onValues(mStatusSuccess, mISupplicantP2pNetworkMock); 2306 } else { 2307 cb.onValues(mStatusFailure, null); 2308 } 2309 return; 2310 } 2311 }).when(mISupplicantP2pIfaceMock) 2312 .getNetwork(anyInt(), any(ISupplicantP2pIface.getNetworkCallback.class)); 2313 when(mISupplicantP2pNetworkMock.setClientList(any(ArrayList.class))) 2314 .thenReturn(mStatusSuccess); 2315 2316 String clientList = client1 + " " + client2; 2317 assertTrue(mDut.setClientList(testNetworkId, clientList)); 2318 verify(mISupplicantP2pIfaceMock) 2319 .getNetwork(anyInt(), any(ISupplicantP2pIface.getNetworkCallback.class)); 2320 ArgumentCaptor<ArrayList> capturedClients = ArgumentCaptor.forClass(ArrayList.class); 2321 verify(mISupplicantP2pNetworkMock).setClientList(capturedClients.capture()); 2322 2323 // Convert these to long to help with comparisons. 2324 ArrayList<byte[]> clients = capturedClients.getValue(); 2325 ArrayList<Long> expectedClients = new ArrayList<Long>() {{ 2326 add(NativeUtil.macAddressToLong(mGroupOwnerMacAddressBytes)); 2327 add(NativeUtil.macAddressToLong(mPeerMacAddressBytes)); 2328 }}; 2329 ArrayList<Long> receivedClients = new ArrayList<Long>(); 2330 for (byte[] client : clients) { 2331 receivedClients.add(NativeUtil.macAddressToLong(client)); 2332 } 2333 assertEquals(expectedClients, receivedClients); 2334 } 2335 2336 /** 2337 * Failure scenario for setClientList() when getNetwork returns null. 2338 */ 2339 @Test 2340 public void testSetClientListFailureDueToGetNetwork() throws Exception { 2341 int testNetworkId = 5; 2342 final String client1 = mGroupOwnerMacAddress; 2343 final String client2 = mPeerMacAddress; 2344 2345 executeAndValidateInitializationSequence(false, false, false); 2346 doAnswer(new AnswerWithArguments() { 2347 public void answer(final int networkId, ISupplicantP2pIface.getNetworkCallback cb) { 2348 cb.onValues(mStatusFailure, null); 2349 return; 2350 } 2351 }).when(mISupplicantP2pIfaceMock) 2352 .getNetwork(anyInt(), any(ISupplicantP2pIface.getNetworkCallback.class)); 2353 when(mISupplicantP2pNetworkMock.setClientList(any(ArrayList.class))) 2354 .thenReturn(mStatusSuccess); 2355 2356 String clientList = client1 + " " + client2; 2357 assertFalse(mDut.setClientList(testNetworkId, clientList)); 2358 verify(mISupplicantP2pIfaceMock) 2359 .getNetwork(anyInt(), any(ISupplicantP2pIface.getNetworkCallback.class)); 2360 verify(mISupplicantP2pNetworkMock, never()).setClientList(any(ArrayList.class)); 2361 } 2362 2363 /** 2364 * Sunny day scenario for getClientList() 2365 */ 2366 @Test 2367 public void testGetClientList() throws Exception { 2368 int testNetworkId = 5; 2369 final String client1 = mGroupOwnerMacAddress; 2370 final String client2 = mPeerMacAddress; 2371 2372 executeAndValidateInitializationSequence(false, false, false); 2373 doAnswer(new AnswerWithArguments() { 2374 public void answer(final int networkId, ISupplicantP2pIface.getNetworkCallback cb) { 2375 if (networkId == testNetworkId) { 2376 cb.onValues(mStatusSuccess, mISupplicantP2pNetworkMock); 2377 } else { 2378 cb.onValues(mStatusFailure, null); 2379 } 2380 return; 2381 } 2382 }).when(mISupplicantP2pIfaceMock) 2383 .getNetwork(anyInt(), any(ISupplicantP2pIface.getNetworkCallback.class)); 2384 doAnswer(new AnswerWithArguments() { 2385 public void answer(ISupplicantP2pNetwork.getClientListCallback cb) { 2386 ArrayList<byte[]> clients = new ArrayList<byte[]>() {{ 2387 add(mGroupOwnerMacAddressBytes); 2388 add(mPeerMacAddressBytes); 2389 }}; 2390 cb.onValues(mStatusSuccess, clients); 2391 return; 2392 } 2393 }).when(mISupplicantP2pNetworkMock) 2394 .getClientList(any(ISupplicantP2pNetwork.getClientListCallback.class)); 2395 2396 String clientList = client1 + " " + client2; 2397 assertEquals(clientList, mDut.getClientList(testNetworkId)); 2398 verify(mISupplicantP2pIfaceMock) 2399 .getNetwork(anyInt(), any(ISupplicantP2pIface.getNetworkCallback.class)); 2400 verify(mISupplicantP2pNetworkMock) 2401 .getClientList(any(ISupplicantP2pNetwork.getClientListCallback.class)); 2402 } 2403 2404 /** 2405 * Failure scenario for getClientList() when getNetwork returns null. 2406 */ 2407 @Test 2408 public void testGetClientListFailureDueToGetNetwork() throws Exception { 2409 int testNetworkId = 5; 2410 final String client1 = mGroupOwnerMacAddress; 2411 final String client2 = mPeerMacAddress; 2412 2413 executeAndValidateInitializationSequence(false, false, false); 2414 doAnswer(new AnswerWithArguments() { 2415 public void answer(final int networkId, ISupplicantP2pIface.getNetworkCallback cb) { 2416 cb.onValues(mStatusFailure, null); 2417 return; 2418 } 2419 }).when(mISupplicantP2pIfaceMock) 2420 .getNetwork(anyInt(), any(ISupplicantP2pIface.getNetworkCallback.class)); 2421 doAnswer(new AnswerWithArguments() { 2422 public void answer(ISupplicantP2pNetwork.getClientListCallback cb) { 2423 ArrayList<byte[]> clients = new ArrayList<byte[]>() {{ 2424 add(mGroupOwnerMacAddressBytes); 2425 add(mPeerMacAddressBytes); 2426 }}; 2427 cb.onValues(mStatusSuccess, clients); 2428 return; 2429 } 2430 }).when(mISupplicantP2pNetworkMock) 2431 .getClientList(any(ISupplicantP2pNetwork.getClientListCallback.class)); 2432 2433 assertEquals(null, mDut.getClientList(testNetworkId)); 2434 verify(mISupplicantP2pIfaceMock) 2435 .getNetwork(anyInt(), any(ISupplicantP2pIface.getNetworkCallback.class)); 2436 verify(mISupplicantP2pNetworkMock, never()) 2437 .getClientList(any(ISupplicantP2pNetwork.getClientListCallback.class)); 2438 } 2439 2440 /** 2441 * Sunny day scenario for saveConfig() 2442 */ 2443 @Test 2444 public void testSaveConfig() throws Exception { 2445 when(mISupplicantP2pIfaceMock.saveConfig()).thenReturn(mStatusSuccess); 2446 2447 // Should fail before initialization. 2448 assertFalse(mDut.saveConfig()); 2449 executeAndValidateInitializationSequence(false, false, false); 2450 assertTrue(mDut.saveConfig()); 2451 verify(mISupplicantP2pIfaceMock).saveConfig(); 2452 } 2453 2454 /** 2455 * Calls.initialize(), mocking various call back answers and verifying flow, asserting for the 2456 * expected result. Verifies if ISupplicantP2pIface manager is initialized or reset. 2457 * Each of the arguments will cause a different failure mode when set true. 2458 */ 2459 private void executeAndValidateInitializationSequence(boolean causeRemoteException, 2460 boolean getZeroInterfaces, boolean getNullInterface) throws Exception { 2461 boolean shouldSucceed = !causeRemoteException && !getZeroInterfaces && !getNullInterface; 2462 // Setup callback mock answers 2463 ArrayList<ISupplicant.IfaceInfo> interfaces; 2464 if (getZeroInterfaces) { 2465 interfaces = new ArrayList<ISupplicant.IfaceInfo>(); 2466 } else { 2467 interfaces = mIfaceInfoList; 2468 } 2469 2470 doAnswer(new AnswerWithArguments() { 2471 public void answer(ISupplicant.listInterfacesCallback cb) throws RemoteException { 2472 cb.onValues(mStatusSuccess, interfaces); 2473 } 2474 }) 2475 .when(mISupplicantMock).listInterfaces(any(ISupplicant.listInterfacesCallback.class)); 2476 2477 if (causeRemoteException) { 2478 doThrow(new RemoteException("Some error!!!")) 2479 .when(mISupplicantMock).getInterface(any(ISupplicant.IfaceInfo.class), 2480 any(ISupplicant.getInterfaceCallback.class)); 2481 } else { 2482 doAnswer(new GetGetInterfaceAnswer(getNullInterface)) 2483 .when(mISupplicantMock).getInterface(any(ISupplicant.IfaceInfo.class), 2484 any(ISupplicant.getInterfaceCallback.class)); 2485 } 2486 2487 mInOrder = inOrder(mServiceManagerMock, mISupplicantMock); 2488 // Initialize SupplicantP2pIfaceHal, should call serviceManager.registerForNotifications 2489 assertTrue(mDut.initialize()); 2490 // verify: service manager initialization sequence 2491 mInOrder.verify(mServiceManagerMock).linkToDeath(any(IHwBinder.DeathRecipient.class), 2492 anyLong()); 2493 mInOrder.verify(mServiceManagerMock).registerForNotifications( 2494 eq(ISupplicant.kInterfaceName), eq(""), mServiceNotificationCaptor.capture()); 2495 // act: cause the onRegistration(...) callback to execute 2496 mServiceNotificationCaptor.getValue().onRegistration(ISupplicant.kInterfaceName, "", true); 2497 2498 assertEquals(shouldSucceed, mDut.isInitializationComplete()); 2499 // verify: listInterfaces is called 2500 mInOrder.verify(mISupplicantMock).listInterfaces( 2501 any(ISupplicant.listInterfacesCallback.class)); 2502 if (!getZeroInterfaces) { 2503 mInOrder.verify(mISupplicantMock) 2504 .getInterface(any(ISupplicant.IfaceInfo.class), 2505 any(ISupplicant.getInterfaceCallback.class)); 2506 } 2507 } 2508 2509 2510 private SupplicantStatus createSupplicantStatus(int code) { 2511 SupplicantStatus status = new SupplicantStatus(); 2512 status.code = code; 2513 return status; 2514 } 2515 2516 /** 2517 * Create an IfaceInfo with given type and name 2518 */ 2519 private ISupplicant.IfaceInfo createIfaceInfo(int type, String name) { 2520 ISupplicant.IfaceInfo info = new ISupplicant.IfaceInfo(); 2521 info.type = type; 2522 info.name = name; 2523 return info; 2524 } 2525 2526 /** 2527 * Create new dummy WifiP2pConfig instance. 2528 */ 2529 private WifiP2pConfig createDummyP2pConfig(String peerAddress, int wpsProvMethod, String pin) { 2530 WifiP2pConfig config = new WifiP2pConfig(); 2531 config.wps = new WpsInfo(); 2532 config.deviceAddress = peerAddress; 2533 2534 config.wps.setup = wpsProvMethod; 2535 config.wps.pin = pin; 2536 2537 return config; 2538 } 2539 2540 /** 2541 * Create new dummy WifiP2pGroup instance. 2542 */ 2543 private WifiP2pGroup createDummyP2pGroup() { 2544 WifiP2pGroup group = new WifiP2pGroup(); 2545 group.setInterface(mIfaceName); 2546 2547 WifiP2pDevice owner = new WifiP2pDevice(); 2548 owner.deviceAddress = mGroupOwnerMacAddress; 2549 group.setOwner(owner); 2550 2551 return group; 2552 } 2553 2554 /** 2555 * Create new dummy WifiP2pServiceInfo instance. 2556 */ 2557 private WifiP2pServiceInfo createDummyP2pServiceInfo(String... services) { 2558 class TestP2pServiceInfo extends WifiP2pServiceInfo { 2559 TestP2pServiceInfo(String[] services) { 2560 super(Arrays.asList(services)); 2561 } 2562 } 2563 return new TestP2pServiceInfo(services); 2564 } 2565 2566 private class GetGetInterfaceAnswer extends AnswerWithArguments { 2567 boolean mGetNullInterface; 2568 2569 GetGetInterfaceAnswer(boolean getNullInterface) { 2570 mGetNullInterface = getNullInterface; 2571 } 2572 2573 public void answer(ISupplicant.IfaceInfo iface, ISupplicant.getInterfaceCallback cb) { 2574 if (mGetNullInterface) { 2575 cb.onValues(mStatusSuccess, null); 2576 } else { 2577 cb.onValues(mStatusSuccess, mISupplicantIfaceMock); 2578 } 2579 } 2580 } 2581} 2582