1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.wifi.p2p; 18 19 import static android.net.NetworkInfo.DetailedState.FAILED; 20 import static android.net.NetworkInfo.DetailedState.IDLE; 21 22 import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession; 23 import static com.android.server.wifi.WifiSettingsConfigStore.WIFI_P2P_DEVICE_NAME; 24 import static com.android.server.wifi.WifiSettingsConfigStore.WIFI_P2P_PENDING_FACTORY_RESET; 25 import static com.android.server.wifi.WifiSettingsConfigStore.WIFI_VERBOSE_LOGGING_ENABLED; 26 27 import static org.junit.Assert.assertEquals; 28 import static org.junit.Assert.assertNotEquals; 29 import static org.junit.Assert.assertNotNull; 30 import static org.junit.Assert.assertNull; 31 import static org.junit.Assert.assertTrue; 32 import static org.junit.Assume.assumeTrue; 33 import static org.mockito.ArgumentMatchers.argThat; 34 import static org.mockito.Matchers.any; 35 import static org.mockito.Matchers.anyBoolean; 36 import static org.mockito.Matchers.anyInt; 37 import static org.mockito.Matchers.anyLong; 38 import static org.mockito.Matchers.anyString; 39 import static org.mockito.Matchers.eq; 40 import static org.mockito.Matchers.isNull; 41 import static org.mockito.Mockito.atLeastOnce; 42 import static org.mockito.Mockito.doAnswer; 43 import static org.mockito.Mockito.doNothing; 44 import static org.mockito.Mockito.doThrow; 45 import static org.mockito.Mockito.lenient; 46 import static org.mockito.Mockito.never; 47 import static org.mockito.Mockito.reset; 48 import static org.mockito.Mockito.spy; 49 import static org.mockito.Mockito.times; 50 import static org.mockito.Mockito.verify; 51 import static org.mockito.Mockito.when; 52 53 import android.annotation.Nullable; 54 import android.app.AlarmManager; 55 import android.app.test.MockAnswerUtil.AnswerWithArguments; 56 import android.content.BroadcastReceiver; 57 import android.content.Context; 58 import android.content.Intent; 59 import android.content.IntentFilter; 60 import android.content.pm.PackageManager; 61 import android.content.res.Resources; 62 import android.location.LocationManager; 63 import android.net.ConnectivityManager; 64 import android.net.InetAddresses; 65 import android.net.NetworkInfo; 66 import android.net.TetheringManager; 67 import android.net.wifi.CoexUnsafeChannel; 68 import android.net.wifi.WifiConfiguration; 69 import android.net.wifi.WifiInfo; 70 import android.net.wifi.WifiManager; 71 import android.net.wifi.WifiScanner; 72 import android.net.wifi.WpsInfo; 73 import android.net.wifi.p2p.WifiP2pConfig; 74 import android.net.wifi.p2p.WifiP2pDevice; 75 import android.net.wifi.p2p.WifiP2pDeviceList; 76 import android.net.wifi.p2p.WifiP2pGroup; 77 import android.net.wifi.p2p.WifiP2pGroupList; 78 import android.net.wifi.p2p.WifiP2pInfo; 79 import android.net.wifi.p2p.WifiP2pManager; 80 import android.net.wifi.p2p.WifiP2pProvDiscEvent; 81 import android.net.wifi.p2p.WifiP2pWfdInfo; 82 import android.net.wifi.p2p.nsd.WifiP2pServiceInfo; 83 import android.net.wifi.p2p.nsd.WifiP2pServiceRequest; 84 import android.os.Binder; 85 import android.os.Bundle; 86 import android.os.Handler; 87 import android.os.HandlerThread; 88 import android.os.Message; 89 import android.os.Messenger; 90 import android.os.UserHandle; 91 import android.os.UserManager; 92 import android.os.WorkSource; 93 import android.os.test.TestLooper; 94 import android.provider.Settings; 95 96 import androidx.test.filters.SmallTest; 97 98 import com.android.modules.utils.build.SdkLevel; 99 import com.android.server.wifi.FakeWifiLog; 100 import com.android.server.wifi.FrameworkFacade; 101 import com.android.server.wifi.WifiBaseTest; 102 import com.android.server.wifi.WifiGlobals; 103 import com.android.server.wifi.WifiInjector; 104 import com.android.server.wifi.WifiSettingsConfigStore; 105 import com.android.server.wifi.coex.CoexManager; 106 import com.android.server.wifi.proto.nano.WifiMetricsProto.P2pConnectionEvent; 107 import com.android.server.wifi.util.NetdWrapper; 108 import com.android.server.wifi.util.StringUtil; 109 import com.android.server.wifi.util.WifiPermissionsUtil; 110 import com.android.server.wifi.util.WifiPermissionsWrapper; 111 import com.android.wifi.resources.R; 112 113 import org.junit.After; 114 import org.junit.Before; 115 import org.junit.Test; 116 import org.mockito.ArgumentCaptor; 117 import org.mockito.ArgumentMatcher; 118 import org.mockito.Mock; 119 import org.mockito.MockitoAnnotations; 120 import org.mockito.MockitoSession; 121 import org.mockito.Spy; 122 123 import java.net.InetAddress; 124 import java.net.NetworkInterface; 125 import java.util.ArrayList; 126 import java.util.Arrays; 127 import java.util.Collections; 128 import java.util.List; 129 130 /** 131 * Unit test harness for WifiP2pServiceImpl. 132 */ 133 @SmallTest 134 public class WifiP2pServiceImplTest extends WifiBaseTest { 135 private static final String TAG = "WifiP2pServiceImplTest"; 136 private static final String IFACE_NAME_P2P = "mockP2p0"; 137 private static final String P2P_GO_IP = "192.168.49.1"; 138 private static final long STATE_CHANGE_WAITING_TIME = 1000; 139 private static final String thisDeviceMac = "11:22:33:44:55:66"; 140 private static final String thisDeviceName = "thisDeviceName"; 141 private static final String ANONYMIZED_DEVICE_ADDRESS = "02:00:00:00:00:00"; 142 private static final String TEST_PACKAGE_NAME = "com.p2p.test"; 143 private static final String TEST_ANDROID_ID = "314Deadbeef"; 144 145 private ArgumentCaptor<BroadcastReceiver> mBcastRxCaptor = ArgumentCaptor.forClass( 146 BroadcastReceiver.class); 147 private Binder mClient1; 148 private Binder mClient2; 149 150 private BroadcastReceiver mLocationModeReceiver; 151 private BroadcastReceiver mWifiStateChangedReceiver; 152 private BroadcastReceiver mTetherStateReceiver; 153 private Handler mClientHandler; 154 private Messenger mP2pStateMachineMessenger; 155 private Messenger mClientMessenger; 156 private WifiP2pServiceImpl mWifiP2pServiceImpl; 157 private TestLooper mClientHanderLooper; 158 private TestLooper mLooper; 159 private WifiP2pConfig mTestWifiP2pPeerConfig; 160 private WifiP2pConfig mTestWifiP2pFastConnectionConfig; 161 private WifiP2pGroup mTestWifiP2pNewPersistentGoGroup; 162 private WifiP2pGroup mTestWifiP2pGroup; 163 private WifiP2pDevice mTestWifiP2pDevice; 164 private WifiP2pGroupList mGroups = new WifiP2pGroupList(null, null); 165 private WifiP2pDevice mTestThisDevice; 166 private ArgumentCaptor<Message> mMessageCaptor = ArgumentCaptor.forClass(Message.class); 167 private MockitoSession mStaticMockSession = null; 168 169 @Mock Context mContext; 170 @Mock FrameworkFacade mFrameworkFacade; 171 @Mock HandlerThread mHandlerThread; 172 @Mock NetdWrapper mNetdWrapper; 173 @Mock PackageManager mPackageManager; 174 @Mock Resources mResources; 175 @Mock NetworkInterface mP2pNetworkInterface; 176 @Mock WifiInjector mWifiInjector; 177 @Mock WifiManager mMockWifiManager; 178 @Mock WifiPermissionsUtil mWifiPermissionsUtil; 179 @Mock WifiSettingsConfigStore mWifiSettingsConfigStore; 180 @Mock WifiPermissionsWrapper mWifiPermissionsWrapper; 181 @Mock WifiP2pNative mWifiNative; 182 @Mock WifiP2pServiceInfo mTestWifiP2pServiceInfo; 183 @Mock WifiP2pServiceRequest mTestWifiP2pServiceRequest; 184 @Mock UserManager mUserManager; 185 @Mock WifiP2pMetrics mWifiP2pMetrics; 186 @Mock WifiManager mWifiManager; 187 @Mock WifiInfo mWifiInfo; 188 @Mock CoexManager mCoexManager; 189 @Spy FakeWifiLog mLog; 190 @Spy MockWifiP2pMonitor mWifiMonitor; 191 @Mock WifiGlobals mWifiGlobals; 192 @Mock AlarmManager mAlarmManager; 193 CoexManager.CoexListener mCoexListener; 194 generatorTestData()195 private void generatorTestData() { 196 mTestWifiP2pGroup = new WifiP2pGroup(); 197 mTestWifiP2pGroup.setNetworkName("TestGroupName"); 198 mTestWifiP2pDevice = spy(new WifiP2pDevice()); 199 mTestWifiP2pDevice.deviceName = "TestDeviceName"; 200 mTestWifiP2pDevice.deviceAddress = "aa:bb:cc:dd:ee:ff"; 201 202 // for general connect command 203 mTestWifiP2pPeerConfig = new WifiP2pConfig(); 204 mTestWifiP2pPeerConfig.deviceAddress = mTestWifiP2pDevice.deviceAddress; 205 206 // for fast-connection connect command 207 mTestWifiP2pFastConnectionConfig = new WifiP2pConfig.Builder() 208 .setNetworkName("DIRECT-XY-HELLO") 209 .setPassphrase("DEADBEEF") 210 .build(); 211 212 // for general group started event 213 mTestWifiP2pNewPersistentGoGroup = new WifiP2pGroup(); 214 mTestWifiP2pNewPersistentGoGroup.setNetworkId(WifiP2pGroup.NETWORK_ID_PERSISTENT); 215 mTestWifiP2pNewPersistentGoGroup.setNetworkName("DIRECT-xy-NEW"); 216 mTestWifiP2pNewPersistentGoGroup.setOwner(new WifiP2pDevice(thisDeviceMac)); 217 mTestWifiP2pNewPersistentGoGroup.setIsGroupOwner(true); 218 mTestWifiP2pNewPersistentGoGroup.setInterface(IFACE_NAME_P2P); 219 220 mGroups.clear(); 221 WifiP2pGroup group1 = new WifiP2pGroup(); 222 group1.setNetworkId(0); 223 group1.setNetworkName(mTestWifiP2pGroup.getNetworkName()); 224 group1.setOwner(mTestWifiP2pDevice); 225 group1.setIsGroupOwner(false); 226 mGroups.add(group1); 227 228 WifiP2pGroup group2 = new WifiP2pGroup(); 229 group2.setNetworkId(1); 230 group2.setNetworkName("DIRECT-ab-Hello"); 231 group2.setOwner(new WifiP2pDevice("12:34:56:78:90:ab")); 232 group2.setIsGroupOwner(false); 233 mGroups.add(group2); 234 235 WifiP2pGroup group3 = new WifiP2pGroup(); 236 group3.setNetworkId(2); 237 group3.setNetworkName("DIRECT-cd-OWNER"); 238 group3.setOwner(new WifiP2pDevice(thisDeviceMac)); 239 group3.setIsGroupOwner(true); 240 mGroups.add(group3); 241 242 mTestThisDevice = new WifiP2pDevice(); 243 mTestThisDevice.deviceName = thisDeviceName; 244 mTestThisDevice.deviceAddress = thisDeviceMac; 245 mTestThisDevice.primaryDeviceType = "10-0050F204-5"; 246 } 247 248 /** 249 * Simulate Location Mode change: Changes the location manager return values and dispatches a 250 * broadcast. 251 * 252 * @param isLocationModeEnabled whether the location mode is enabled., 253 */ simulateLocationModeChange(boolean isLocationModeEnabled)254 private void simulateLocationModeChange(boolean isLocationModeEnabled) { 255 when(mWifiPermissionsUtil.isLocationModeEnabled()).thenReturn(isLocationModeEnabled); 256 257 Intent intent = new Intent(LocationManager.MODE_CHANGED_ACTION); 258 mLocationModeReceiver.onReceive(mContext, intent); 259 } 260 261 /** 262 * Simulate Wi-Fi state change: broadcast state change and modify the API return value. 263 * 264 * @param isWifiOn whether the wifi mode is enabled. 265 */ simulateWifiStateChange(boolean isWifiOn)266 private void simulateWifiStateChange(boolean isWifiOn) { 267 when(mMockWifiManager.getWifiState()).thenReturn( 268 isWifiOn ? WifiManager.WIFI_STATE_ENABLED : WifiManager.WIFI_STATE_DISABLED); 269 270 Intent intent = new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION); 271 intent.putExtra(WifiManager.EXTRA_WIFI_STATE, 272 isWifiOn ? WifiManager.WIFI_STATE_ENABLED : WifiManager.WIFI_STATE_DISABLED); 273 mWifiStateChangedReceiver.onReceive(mContext, intent); 274 } 275 276 /** 277 * Simulate tethering flow is completed 278 */ simulateTetherReady()279 private void simulateTetherReady() { 280 ArrayList<String> availableList = new ArrayList<>(); 281 ArrayList<String> localOnlyList = new ArrayList<>(); 282 localOnlyList.add(IFACE_NAME_P2P); 283 ArrayList<String> tetherList = new ArrayList<>(); 284 ArrayList<String> erroredList = new ArrayList<>(); 285 286 Intent intent = new Intent(TetheringManager.ACTION_TETHER_STATE_CHANGED); 287 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); 288 intent.putStringArrayListExtra(TetheringManager.EXTRA_AVAILABLE_TETHER, availableList); 289 intent.putStringArrayListExtra(TetheringManager.EXTRA_ACTIVE_LOCAL_ONLY, localOnlyList); 290 intent.putStringArrayListExtra(TetheringManager.EXTRA_ACTIVE_TETHER, tetherList); 291 intent.putStringArrayListExtra(TetheringManager.EXTRA_ERRORED_TETHER, erroredList); 292 mTetherStateReceiver.onReceive(mContext, intent); 293 mLooper.dispatchAll(); 294 } 295 296 /** 297 * Mock send WifiP2pManager.UPDATE_CHANNEL_INFO 298 * 299 * @param pkgName package name used for p2p channel init 300 * @param featureId The feature in the package 301 * @param binder client binder used for p2p channel init 302 * @param replyMessenger for checking replied message. 303 */ sendChannelInfoUpdateMsg(String pkgName, @Nullable String featureId, Binder binder, Messenger replyMessenger)304 private void sendChannelInfoUpdateMsg(String pkgName, @Nullable String featureId, 305 Binder binder, Messenger replyMessenger) throws Exception { 306 Message msg = Message.obtain(); 307 msg.what = WifiP2pManager.UPDATE_CHANNEL_INFO; 308 Bundle bundle = new Bundle(); 309 bundle.putString(WifiP2pManager.CALLING_PACKAGE, pkgName); 310 bundle.putString(WifiP2pManager.CALLING_FEATURE_ID, featureId); 311 bundle.putBinder(WifiP2pManager.CALLING_BINDER, binder); 312 msg.obj = bundle; 313 msg.replyTo = replyMessenger; 314 mP2pStateMachineMessenger.send(Message.obtain(msg)); 315 mLooper.dispatchAll(); 316 } 317 318 /** 319 * Mock send WifiP2pManager.ADD_LOCAL_SERVICE with mTestWifiP2pServiceInfo 320 * 321 * @param replyMessenger for checking replied message. 322 */ sendAddLocalServiceMsg(Messenger replyMessenger)323 private void sendAddLocalServiceMsg(Messenger replyMessenger) throws Exception { 324 Message msg = Message.obtain(); 325 msg.what = WifiP2pManager.ADD_LOCAL_SERVICE; 326 msg.obj = mTestWifiP2pServiceInfo; 327 msg.replyTo = replyMessenger; 328 mP2pStateMachineMessenger.send(Message.obtain(msg)); 329 mLooper.dispatchAll(); 330 } 331 332 /** 333 * Mock send WifiP2pManager.CONNECT with ConfigValidAsGroup 334 * 335 * @param replyMessenger for checking replied message. 336 */ sendConnectMsgWithConfigValidAsGroup(Messenger replyMessenger)337 private void sendConnectMsgWithConfigValidAsGroup(Messenger replyMessenger) throws Exception { 338 sendConnectMsg(replyMessenger, mTestWifiP2pFastConnectionConfig); 339 } 340 341 /** 342 * Mock send WifiP2pManager.CREATE_GROUP with ConfigValidAsGroup 343 * 344 * @param replyMessenger for checking replied message. 345 */ sendCreateGroupMsgWithConfigValidAsGroup(Messenger replyMessenger)346 private void sendCreateGroupMsgWithConfigValidAsGroup(Messenger replyMessenger) 347 throws Exception { 348 sendCreateGroupMsg(replyMessenger, 0, mTestWifiP2pFastConnectionConfig); 349 } 350 351 /** 352 * Mock send WifiP2pManager.DISCOVER_PEERS 353 * 354 * @param replyMessenger for checking replied message. 355 */ sendDiscoverPeersMsg(Messenger replyMessenger)356 private void sendDiscoverPeersMsg(Messenger replyMessenger) throws Exception { 357 Message msg = Message.obtain(); 358 msg.what = WifiP2pManager.DISCOVER_PEERS; 359 msg.replyTo = replyMessenger; 360 mP2pStateMachineMessenger.send(Message.obtain(msg)); 361 mLooper.dispatchAll(); 362 } 363 364 /** 365 * Mock send WifiP2pManager.ADD_SERVICE_REQUEST with mocked mTestWifiP2pServiceRequest 366 * 367 * @param replyMessenger for checking replied message. 368 */ sendAddServiceRequestMsg(Messenger replyMessenger)369 private void sendAddServiceRequestMsg(Messenger replyMessenger) throws Exception { 370 Message msg = Message.obtain(); 371 msg.what = WifiP2pManager.ADD_SERVICE_REQUEST; 372 msg.replyTo = replyMessenger; 373 msg.obj = mTestWifiP2pServiceRequest; 374 mP2pStateMachineMessenger.send(Message.obtain(msg)); 375 mLooper.dispatchAll(); 376 } 377 378 /** 379 * Mock send WifiP2pManager.DISCOVER_SERVICES 380 * 381 * @param replyMessenger for checking replied message. 382 */ sendDiscoverServiceMsg(Messenger replyMessenger)383 private void sendDiscoverServiceMsg(Messenger replyMessenger) throws Exception { 384 Message msg = Message.obtain(); 385 msg.what = WifiP2pManager.DISCOVER_SERVICES; 386 msg.replyTo = replyMessenger; 387 mP2pStateMachineMessenger.send(Message.obtain(msg)); 388 mLooper.dispatchAll(); 389 } 390 391 /** 392 * Mock send WifiP2pManager.REQUEST_PEERS 393 * 394 * @param replyMessenger for checking replied message. 395 */ sendRequestPeersMsg(Messenger replyMessenger)396 private void sendRequestPeersMsg(Messenger replyMessenger) throws Exception { 397 Message msg = Message.obtain(); 398 msg.what = WifiP2pManager.REQUEST_PEERS; 399 msg.replyTo = replyMessenger; 400 mP2pStateMachineMessenger.send(Message.obtain(msg)); 401 mLooper.dispatchAll(); 402 } 403 404 /** 405 * Mock send WifiP2pManager.REQUEST_GROUP_INFO 406 * 407 * @param replyMessenger for checking replied message. 408 */ sendRequestGroupInfoMsg(Messenger replyMessenger)409 private void sendRequestGroupInfoMsg(Messenger replyMessenger) throws Exception { 410 Message msg = Message.obtain(); 411 msg.what = WifiP2pManager.REQUEST_GROUP_INFO; 412 msg.replyTo = replyMessenger; 413 mP2pStateMachineMessenger.send(Message.obtain(msg)); 414 mLooper.dispatchAll(); 415 } 416 417 /** 418 * Mock send WifiP2pManager.DELETE_PERSISTENT_GROUP. 419 * 420 * @param replyMessenger for checking replied message. 421 * @param netId is the network id of the p2p group. 422 */ sendDeletePersistentGroupMsg(Messenger replyMessenger, int netId)423 private void sendDeletePersistentGroupMsg(Messenger replyMessenger, 424 int netId) throws Exception { 425 Message msg = Message.obtain(); 426 msg.what = WifiP2pManager.DELETE_PERSISTENT_GROUP; 427 msg.arg1 = netId; 428 msg.replyTo = replyMessenger; 429 mP2pStateMachineMessenger.send(Message.obtain(msg)); 430 mLooper.dispatchAll(); 431 } 432 433 /** 434 * Send WifiP2pMonitor.P2P_GROUP_STARTED_EVENT. 435 * 436 * @param group the started group. 437 */ sendGroupStartedMsg(WifiP2pGroup group)438 private void sendGroupStartedMsg(WifiP2pGroup group) throws Exception { 439 if (group.getNetworkId() == WifiP2pGroup.NETWORK_ID_PERSISTENT) { 440 mGroups.add(group); 441 } 442 443 Message msg = Message.obtain(); 444 msg.what = WifiP2pMonitor.P2P_GROUP_STARTED_EVENT; 445 msg.obj = group; 446 mP2pStateMachineMessenger.send(Message.obtain(msg)); 447 mLooper.dispatchAll(); 448 } 449 450 /** 451 * Send WifiP2pMonitor.P2P_GROUP_REMOVED_EVENT. 452 */ sendGroupRemovedMsg()453 private void sendGroupRemovedMsg() throws Exception { 454 Message msg = Message.obtain(); 455 msg.what = WifiP2pMonitor.P2P_GROUP_REMOVED_EVENT; 456 mP2pStateMachineMessenger.send(Message.obtain(msg)); 457 mLooper.dispatchAll(); 458 } 459 460 /** 461 * Send WifiP2pMonitor.P2P_DEVICE_FOUND_EVENT. 462 * 463 * @param device the found device. 464 */ sendDeviceFoundEventMsg(WifiP2pDevice device)465 private void sendDeviceFoundEventMsg(WifiP2pDevice device) throws Exception { 466 Message msg = Message.obtain(); 467 msg.what = WifiP2pMonitor.P2P_DEVICE_FOUND_EVENT; 468 msg.obj = device; 469 mP2pStateMachineMessenger.send(Message.obtain(msg)); 470 mLooper.dispatchAll(); 471 } 472 473 /** 474 * Send WifiP2pMonitor.P2P_INVITATION_RESULT_EVENT. 475 * 476 * @param status invitation result. 477 */ sendInvitationResultMsg( WifiP2pServiceImpl.P2pStatus status)478 private void sendInvitationResultMsg( 479 WifiP2pServiceImpl.P2pStatus status) throws Exception { 480 Message msg = Message.obtain(); 481 msg.what = WifiP2pMonitor.P2P_INVITATION_RESULT_EVENT; 482 msg.obj = status; 483 mP2pStateMachineMessenger.send(Message.obtain(msg)); 484 mLooper.dispatchAll(); 485 } 486 487 /** 488 * Mock send WifiP2pManager.SET_CHANNEL 489 * 490 * @param replyMessenger for checking replied message. 491 * @param p2pChannels stores the listen and operating channels. 492 */ sendSetChannelMsg(Messenger replyMessenger, Bundle p2pChannels)493 private void sendSetChannelMsg(Messenger replyMessenger, 494 Bundle p2pChannels) throws Exception { 495 Message msg = Message.obtain(); 496 msg.what = WifiP2pManager.SET_CHANNEL; 497 msg.replyTo = replyMessenger; 498 msg.obj = p2pChannels; 499 mP2pStateMachineMessenger.send(Message.obtain(msg)); 500 mLooper.dispatchAll(); 501 } 502 503 /** 504 * Mock send WifiP2pManager.START_WPS 505 * 506 * @param replyMessenger for checking replied message. 507 * @param wps is the WPS configuration. 508 */ sendStartWpsMsg(Messenger replyMessenger, WpsInfo wps)509 private void sendStartWpsMsg(Messenger replyMessenger, WpsInfo wps) throws Exception { 510 Message msg = Message.obtain(); 511 msg.what = WifiP2pManager.START_WPS; 512 msg.replyTo = replyMessenger; 513 msg.obj = wps; 514 mP2pStateMachineMessenger.send(Message.obtain(msg)); 515 mLooper.dispatchAll(); 516 } 517 518 /** 519 * Mock send WifiP2pManager.SET_DEVICE_NAME 520 * 521 * @param replyMessenger for checking replied message. 522 * @param dev is the P2p device configuration. 523 */ sendSetDeviceNameMsg( Messenger replyMessenger, WifiP2pDevice dev)524 private void sendSetDeviceNameMsg( 525 Messenger replyMessenger, WifiP2pDevice dev) throws Exception { 526 Message msg = Message.obtain(); 527 msg.what = WifiP2pManager.SET_DEVICE_NAME; 528 msg.replyTo = replyMessenger; 529 msg.obj = dev; 530 mP2pStateMachineMessenger.send(Message.obtain(msg)); 531 mLooper.dispatchAll(); 532 } 533 534 /** 535 * Mock send WifiP2pManager.SET_WFD_INFO 536 * 537 * @param replyMessenger for checking replied message. 538 * @param wfdInfo is the P2p device's wfd information. 539 */ sendSetWfdInfoMsg( Messenger replyMessenger, WifiP2pWfdInfo wfdInfo)540 private void sendSetWfdInfoMsg( 541 Messenger replyMessenger, WifiP2pWfdInfo wfdInfo) throws Exception { 542 Message msg = Message.obtain(); 543 msg.what = WifiP2pManager.SET_WFD_INFO; 544 msg.replyTo = replyMessenger; 545 msg.obj = wfdInfo; 546 mP2pStateMachineMessenger.send(Message.obtain(msg)); 547 mLooper.dispatchAll(); 548 } 549 550 /** 551 * Mock send WifiP2pManager.SET_ONGOING_PEER_CONFIG 552 * 553 * @param replyMessenger for checking replied message. 554 * @param config used for change an ongoing peer connection. 555 */ sendSetOngoingPeerConfigMsg( Messenger replyMessenger, WifiP2pConfig config)556 private void sendSetOngoingPeerConfigMsg( 557 Messenger replyMessenger, WifiP2pConfig config) throws Exception { 558 Message msg = Message.obtain(); 559 msg.what = WifiP2pManager.SET_ONGOING_PEER_CONFIG; 560 msg.replyTo = replyMessenger; 561 msg.obj = config; 562 mP2pStateMachineMessenger.send(Message.obtain(msg)); 563 mLooper.dispatchAll(); 564 } 565 566 /** 567 * Mock send WifiP2pManager.REMOVE_LOCAL_SERVICE. 568 * 569 * @param replyMessenger for checking replied message. 570 * @param servInfo is the local service information. 571 */ sendRemoveLocalServiceMsg(Messenger replyMessenger, WifiP2pServiceInfo servInfo)572 private void sendRemoveLocalServiceMsg(Messenger replyMessenger, 573 WifiP2pServiceInfo servInfo) throws Exception { 574 Message msg = Message.obtain(); 575 msg.what = WifiP2pManager.REMOVE_LOCAL_SERVICE; 576 msg.obj = servInfo; 577 msg.replyTo = replyMessenger; 578 mP2pStateMachineMessenger.send(Message.obtain(msg)); 579 mLooper.dispatchAll(); 580 } 581 582 /** 583 * Mock send WifiP2pManager.REMOVE_SERVICE_REQUEST. 584 * 585 * @param replyMessenger for checking replied message. 586 * @param req is the service discovery request. 587 */ sendRemoveServiceRequestMsg(Messenger replyMessenger, WifiP2pServiceRequest req)588 private void sendRemoveServiceRequestMsg(Messenger replyMessenger, 589 WifiP2pServiceRequest req) throws Exception { 590 Message msg = Message.obtain(); 591 msg.what = WifiP2pManager.REMOVE_SERVICE_REQUEST; 592 msg.obj = req; 593 msg.replyTo = replyMessenger; 594 mP2pStateMachineMessenger.send(Message.obtain(msg)); 595 mLooper.dispatchAll(); 596 } 597 598 /** 599 * Send Connect API msg. 600 * 601 * @param replyMessenger for checking replied message. 602 * @param config options as described in {@link WifiP2pConfig} class. 603 */ sendConnectMsg(Messenger replyMessenger, WifiP2pConfig config)604 private void sendConnectMsg(Messenger replyMessenger, 605 WifiP2pConfig config) throws Exception { 606 Message msg = Message.obtain(); 607 msg.what = WifiP2pManager.CONNECT; 608 msg.obj = config; 609 msg.replyTo = replyMessenger; 610 mP2pStateMachineMessenger.send(Message.obtain(msg)); 611 mLooper.dispatchAll(); 612 } 613 614 /** 615 * Send CreateGroup API msg. 616 * 617 * @param replyMessenger for checking replied message. 618 * @param config options as described in {@link WifiP2pConfig} class. 619 */ sendCreateGroupMsg(Messenger replyMessenger, int netId, WifiP2pConfig config)620 private void sendCreateGroupMsg(Messenger replyMessenger, 621 int netId, 622 WifiP2pConfig config) throws Exception { 623 Message msg = Message.obtain(); 624 msg.what = WifiP2pManager.CREATE_GROUP; 625 msg.arg1 = netId; 626 msg.obj = config; 627 msg.replyTo = replyMessenger; 628 mP2pStateMachineMessenger.send(Message.obtain(msg)); 629 mLooper.dispatchAll(); 630 } 631 632 /** 633 * Send simple API msg. 634 * 635 * Mock the API msg without arguments. 636 * 637 * @param replyMessenger for checking replied message. 638 */ sendSimpleMsg(Messenger replyMessenger, int what)639 private void sendSimpleMsg(Messenger replyMessenger, 640 int what) throws Exception { 641 Message msg = Message.obtain(); 642 msg.what = what; 643 if (replyMessenger != null) { 644 msg.replyTo = replyMessenger; 645 } 646 mP2pStateMachineMessenger.send(Message.obtain(msg)); 647 mLooper.dispatchAll(); 648 } 649 650 /** 651 * Send simple API msg. 652 * 653 * Mock the API msg with objects. 654 * 655 * @param replyMessenger for checking replied message. 656 */ sendSimpleMsg(Messenger replyMessenger, int what, Object obj)657 private void sendSimpleMsg(Messenger replyMessenger, 658 int what, Object obj) throws Exception { 659 Message msg = Message.obtain(); 660 msg.what = what; 661 msg.obj = obj; 662 if (replyMessenger != null) { 663 msg.replyTo = replyMessenger; 664 } 665 mP2pStateMachineMessenger.send(Message.obtain(msg)); 666 mLooper.dispatchAll(); 667 } 668 669 /** 670 * force P2p State enter InactiveState to start others unit test 671 * 672 * @param clientBinder client binder to use for p2p channel init 673 */ forceP2pEnabled(Binder clientBinder)674 private void forceP2pEnabled(Binder clientBinder) throws Exception { 675 simulateWifiStateChange(true); 676 simulateLocationModeChange(true); 677 checkIsP2pInitWhenClientConnected(true, false, clientBinder, 678 new WorkSource(clientBinder.getCallingUid(), TEST_PACKAGE_NAME)); 679 verify(mContext).sendBroadcastWithMultiplePermissions( 680 argThat(new WifiP2pServiceImplTest 681 .P2pConnectionChangedIntentMatcherForNetworkState(IDLE)), any()); 682 verify(mContext, never()).sendBroadcastWithMultiplePermissions( 683 argThat(new WifiP2pServiceImplTest 684 .P2pConnectionChangedIntentMatcherForNetworkState(FAILED)), any()); 685 } 686 687 /** 688 * Check is P2p init as expected when client connected 689 * 690 * @param expectInit set true if p2p init should succeed as expected, set false when 691 * expected init should not happen 692 * @param expectReplace set true if p2p worksource replace should succeed as expected, set false 693 * when replace should not happen 694 * @param clientBinder client binder to use for p2p channel init 695 * @param expectedRequestorWs Expected merged requestorWs 696 */ checkIsP2pInitWhenClientConnected(boolean expectInit, boolean expectReplace, Binder clientBinder, WorkSource expectedRequestorWs)697 private void checkIsP2pInitWhenClientConnected(boolean expectInit, boolean expectReplace, 698 Binder clientBinder, WorkSource expectedRequestorWs) 699 throws Exception { 700 mWifiP2pServiceImpl.getMessenger(clientBinder, TEST_PACKAGE_NAME); 701 if (expectInit) { 702 // send a command to force P2P enabled. 703 sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_P2P_STATE); 704 } 705 mLooper.dispatchAll(); 706 reset(mClientHandler); 707 if (expectInit) { 708 verify(mWifiNative).setupInterface(any(), any(), eq(expectedRequestorWs)); 709 verify(mNetdWrapper).setInterfaceUp(anyString()); 710 verify(mWifiMonitor, atLeastOnce()).registerHandler(anyString(), anyInt(), any()); 711 // Verify timer is scheduled 712 verify(mAlarmManager, times(2)).setExact(anyInt(), anyLong(), 713 eq(mWifiP2pServiceImpl.P2P_IDLE_SHUTDOWN_MESSAGE_TIMEOUT_TAG), any(), any()); 714 } else if (expectReplace) { 715 verify(mWifiNative).replaceRequestorWs(expectedRequestorWs); 716 } else { 717 verify(mWifiNative, never()).setupInterface(any(), any(), any()); 718 verify(mNetdWrapper, never()).setInterfaceUp(anyString()); 719 verify(mWifiMonitor, never()).registerHandler(anyString(), anyInt(), any()); 720 } 721 } 722 723 /** 724 * Check is P2p teardown as expected when client disconnected 725 * 726 * @param expectTearDown set true if p2p teardown should succeed as expected, 727 * set false when expected teardown should not happen 728 * @param expectReplace set true if p2p worksource replace should succeed as expected, set false 729 * when replace should not happen 730 * @param clientBinder client binder to use for p2p channel init 731 * @param expectedRequestorWs Expected merged requestorWs 732 */ checkIsP2pTearDownWhenClientDisconnected( boolean expectTearDown, boolean expectReplace, Binder clientBinder, WorkSource expectedRequestorWs)733 private void checkIsP2pTearDownWhenClientDisconnected( 734 boolean expectTearDown, boolean expectReplace, 735 Binder clientBinder, WorkSource expectedRequestorWs) throws Exception { 736 mWifiP2pServiceImpl.close(clientBinder); 737 mLooper.dispatchAll(); 738 if (expectTearDown) { 739 verify(mWifiNative).teardownInterface(); 740 verify(mWifiMonitor).stopMonitoring(anyString()); 741 } else if (expectReplace) { 742 verify(mWifiNative).replaceRequestorWs(expectedRequestorWs); 743 } else { 744 verify(mWifiNative, never()).teardownInterface(); 745 verify(mWifiMonitor, never()).stopMonitoring(anyString()); 746 } 747 } 748 749 /** 750 * Check the broadcast of WIFI_P2P_THIS_DEVICE_CHANGED_ACTION is sent as expected. 751 */ checkSendThisDeviceChangedBroadcast()752 private void checkSendThisDeviceChangedBroadcast() { 753 ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); 754 String[] permission_gold = new String[] {android.Manifest.permission.ACCESS_FINE_LOCATION, 755 android.Manifest.permission.ACCESS_WIFI_STATE}; 756 ArgumentCaptor<String []> permissionCaptor = ArgumentCaptor.forClass(String[].class); 757 verify(mContext, atLeastOnce()).sendBroadcastWithMultiplePermissions( 758 intentCaptor.capture(), permissionCaptor.capture()); 759 String [] permission = permissionCaptor.getValue(); 760 Arrays.sort(permission); 761 Arrays.sort(permission_gold); 762 assertEquals(permission_gold, permission); 763 Intent intent = intentCaptor.getValue(); 764 WifiP2pDevice device = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE); 765 assertEquals(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION, intent.getAction()); 766 assertEquals(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT, intent.getFlags()); 767 assertEquals(mTestThisDevice.deviceName, device.deviceName); 768 assertEquals(ANONYMIZED_DEVICE_ADDRESS, device.deviceAddress); 769 assertEquals(mTestThisDevice.primaryDeviceType, device.primaryDeviceType); 770 assertEquals(mTestThisDevice.secondaryDeviceType, device.secondaryDeviceType); 771 assertEquals(mTestThisDevice.wpsConfigMethodsSupported, device.wpsConfigMethodsSupported); 772 assertEquals(mTestThisDevice.deviceCapability, device.deviceCapability); 773 assertEquals(mTestThisDevice.groupCapability, device.groupCapability); 774 assertEquals(mTestThisDevice.status, device.status); 775 if (mTestThisDevice.wfdInfo != null) { 776 assertEquals(mTestThisDevice.wfdInfo.isEnabled(), 777 device.wfdInfo.isEnabled()); 778 assertEquals(mTestThisDevice.wfdInfo.getDeviceInfoHex(), 779 device.wfdInfo.getDeviceInfoHex()); 780 assertEquals(mTestThisDevice.wfdInfo.getControlPort(), 781 device.wfdInfo.getControlPort()); 782 assertEquals(mTestThisDevice.wfdInfo.getMaxThroughput(), 783 device.wfdInfo.getMaxThroughput()); 784 } else { 785 assertEquals(mTestThisDevice.wfdInfo, device.wfdInfo); 786 } 787 } 788 789 /** 790 * Check the broadcast of ACTION_WIFI_P2P_PERSISTENT_GROUPS_CHANGED is sent as expected. 791 */ checkSendP2pPersistentGroupsChangedBroadcast()792 private void checkSendP2pPersistentGroupsChangedBroadcast() { 793 ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); 794 verify(mContext, atLeastOnce()).sendStickyBroadcastAsUser( 795 intentCaptor.capture(), eq(UserHandle.ALL)); 796 Intent intent = intentCaptor.getValue(); 797 assertEquals(WifiP2pManager.ACTION_WIFI_P2P_PERSISTENT_GROUPS_CHANGED, intent.getAction()); 798 assertEquals(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT, intent.getFlags()); 799 } 800 801 private class P2pConnectionChangedIntentMatcherForNetworkState 802 implements ArgumentMatcher<Intent> { 803 private final NetworkInfo.DetailedState mState; P2pConnectionChangedIntentMatcherForNetworkState(NetworkInfo.DetailedState state)804 P2pConnectionChangedIntentMatcherForNetworkState(NetworkInfo.DetailedState state) { 805 mState = state; 806 } 807 @Override matches(Intent intent)808 public boolean matches(Intent intent) { 809 if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION != intent.getAction()) { 810 return false; 811 } 812 NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); 813 return networkInfo.getDetailedState() == mState; 814 } 815 } 816 817 /** 818 * Set up the instance of WifiP2pServiceImpl for testing. 819 * 820 * @param supported defines the p2p is supported or not in this instance. 821 */ setUpWifiP2pServiceImpl(boolean supported)822 private void setUpWifiP2pServiceImpl(boolean supported) throws Exception { 823 reset(mContext, mFrameworkFacade, mHandlerThread, mPackageManager, mResources, 824 mWifiInjector, mWifiNative); 825 826 generatorTestData(); 827 mClientHanderLooper = new TestLooper(); 828 mClientHandler = spy(new Handler(mClientHanderLooper.getLooper())); 829 mClientMessenger = new Messenger(mClientHandler); 830 mLooper = new TestLooper(); 831 832 when(mContext.getSystemService(Context.ALARM_SERVICE)) 833 .thenReturn(mAlarmManager); 834 when(mContext.getPackageManager()).thenReturn(mPackageManager); 835 when(mContext.getResources()).thenReturn(mResources); 836 when(mContext.getSystemService(WifiManager.class)).thenReturn(mWifiManager); 837 when(mWifiManager.getConnectionInfo()).thenReturn(mWifiInfo); 838 when(mWifiSettingsConfigStore.get(eq(WIFI_P2P_DEVICE_NAME))).thenReturn(thisDeviceName); 839 when(mWifiSettingsConfigStore.get(eq(WIFI_P2P_PENDING_FACTORY_RESET))).thenReturn(false); 840 when(mHandlerThread.getLooper()).thenReturn(mLooper.getLooper()); 841 if (supported) { 842 when(mPackageManager.hasSystemFeature(eq(PackageManager.FEATURE_WIFI_DIRECT))) 843 .thenReturn(true); 844 } else { 845 when(mPackageManager.hasSystemFeature(eq(PackageManager.FEATURE_WIFI_DIRECT))) 846 .thenReturn(false); 847 } 848 when(mResources.getString(R.string.config_wifi_p2p_device_type)) 849 .thenReturn("10-0050F204-5"); 850 when(mWifiInjector.getFrameworkFacade()).thenReturn(mFrameworkFacade); 851 when(mWifiInjector.getUserManager()).thenReturn(mUserManager); 852 when(mWifiInjector.getWifiP2pMetrics()).thenReturn(mWifiP2pMetrics); 853 when(mWifiInjector.getWifiP2pMonitor()).thenReturn(mWifiMonitor); 854 when(mWifiInjector.getWifiP2pNative()).thenReturn(mWifiNative); 855 when(mWifiInjector.getWifiP2pServiceHandlerThread()).thenReturn(mHandlerThread); 856 when(mWifiInjector.getWifiPermissionsUtil()).thenReturn(mWifiPermissionsUtil); 857 when(mWifiInjector.getSettingsConfigStore()).thenReturn(mWifiSettingsConfigStore); 858 when(mWifiInjector.getCoexManager()).thenReturn(mCoexManager); 859 when(mWifiInjector.getWifiGlobals()).thenReturn(mWifiGlobals); 860 // enable all permissions, disable specific permissions in tests 861 when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(true); 862 when(mWifiPermissionsUtil.checkNetworkStackPermission(anyInt())).thenReturn(true); 863 when(mWifiPermissionsUtil.checkReadWifiCredentialPermission(anyInt())).thenReturn(true); 864 when(mWifiPermissionsUtil.checkConfigOverridePermission(anyInt())).thenReturn(true); 865 when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(), 866 anyBoolean())).thenReturn(true); 867 when(mWifiNative.setupInterface(any(), any(), any())).thenReturn(IFACE_NAME_P2P); 868 when(mWifiNative.p2pGetDeviceAddress()).thenReturn(thisDeviceMac); 869 doAnswer(new AnswerWithArguments() { 870 public boolean answer(WifiP2pGroupList groups) { 871 groups.clear(); 872 for (WifiP2pGroup group : mGroups.getGroupList()) { 873 groups.add(group); 874 } 875 return true; 876 } 877 }).when(mWifiNative).p2pListNetworks(any(WifiP2pGroupList.class)); 878 doAnswer(new AnswerWithArguments() { 879 public boolean answer(int netId) { 880 mGroups.remove(netId); 881 return true; 882 } 883 }).when(mWifiNative).removeP2pNetwork(anyInt()); 884 when(mWifiSettingsConfigStore.get(eq(WIFI_VERBOSE_LOGGING_ENABLED))).thenReturn(true); 885 886 doAnswer(new AnswerWithArguments() { 887 public void answer(CoexManager.CoexListener listener) { 888 mCoexListener = listener; 889 } 890 }).when(mCoexManager).registerCoexListener(any(CoexManager.CoexListener.class)); 891 when(mCoexManager.getCoexRestrictions()).thenReturn(0); 892 when(mCoexManager.getCoexUnsafeChannels()).thenReturn(Collections.emptyList()); 893 894 mWifiP2pServiceImpl = new WifiP2pServiceImpl(mContext, mWifiInjector); 895 if (supported) { 896 // register these event: 897 // * WifiManager.WIFI_STATE_CHANGED_ACTION 898 // * LocationManager.MODE_CHANGED_ACTION 899 // * TetheringManager.ACTION_TETHER_STATE_CHANGED 900 verify(mContext, times(3)).registerReceiver(mBcastRxCaptor.capture(), 901 any(IntentFilter.class)); 902 mWifiStateChangedReceiver = mBcastRxCaptor.getAllValues().get(0); 903 mLocationModeReceiver = mBcastRxCaptor.getAllValues().get(1); 904 mTetherStateReceiver = mBcastRxCaptor.getAllValues().get(2); 905 } 906 907 mWifiP2pServiceImpl.mNetdWrapper = mNetdWrapper; 908 mP2pStateMachineMessenger = mWifiP2pServiceImpl.getP2pStateMachineMessenger(); 909 mWifiP2pServiceImpl.setWifiHandlerLogForTest(mLog); 910 mWifiP2pServiceImpl.setWifiLogForReplyChannel(mLog); 911 } 912 913 @Before setUp()914 public void setUp() throws Exception { 915 MockitoAnnotations.initMocks(this); 916 917 mStaticMockSession = mockitoSession() 918 .mockStatic(NetworkInterface.class) 919 .startMocking(); 920 lenient().when(NetworkInterface.getByName(eq(IFACE_NAME_P2P))) 921 .thenReturn(mP2pNetworkInterface); 922 ArrayList<InetAddress> p2pInetAddresses = new ArrayList<>(); 923 p2pInetAddresses.add(InetAddresses.parseNumericAddress(P2P_GO_IP)); 924 when(mP2pNetworkInterface.getInetAddresses()) 925 .thenReturn(Collections.enumeration(p2pInetAddresses)); 926 927 setUpWifiP2pServiceImpl(true); 928 mClient1 = new Binder(); 929 mClient2 = new Binder(); 930 931 when(mContext.createContextAsUser(any(), anyInt())).thenReturn(mContext); 932 } 933 934 @After cleanUp()935 public void cleanUp() throws Exception { 936 mStaticMockSession.finishMocking(); 937 } 938 939 /** 940 * Mock enter Disabled state. 941 */ mockEnterDisabledState()942 private void mockEnterDisabledState() throws Exception { 943 Message msg = Message.obtain(); 944 msg.what = WifiP2pMonitor.SUP_DISCONNECTION_EVENT; 945 mP2pStateMachineMessenger.send(Message.obtain(msg)); 946 mLooper.dispatchAll(); 947 } 948 949 /** 950 * Mock enter GroupNegotiation state. 951 */ mockEnterGroupNegotiationState()952 private void mockEnterGroupNegotiationState() throws Exception { 953 sendCreateGroupMsg(mClientMessenger, WifiP2pGroup.NETWORK_ID_TEMPORARY, null); 954 } 955 956 957 /** 958 * Mock enter ProvisionDiscovery state. 959 */ mockEnterProvisionDiscoveryState()960 private void mockEnterProvisionDiscoveryState() throws Exception { 961 mockPeersList(); 962 sendConnectMsg(mClientMessenger, mTestWifiP2pPeerConfig); 963 } 964 965 /** 966 * Mock WifiP2pServiceImpl.mPeers. 967 */ mockPeersList()968 private void mockPeersList() throws Exception { 969 sendDeviceFoundEventMsg(mTestWifiP2pDevice); 970 } 971 972 /** 973 * Verify that p2p init / teardown whn a client connects / disconnects 974 * with wifi enabled 975 */ 976 @Test testP2pInitWhenClientConnectWithWifiEnabled()977 public void testP2pInitWhenClientConnectWithWifiEnabled() throws Exception { 978 simulateWifiStateChange(true); 979 checkIsP2pInitWhenClientConnected(true, false, mClient1, 980 new WorkSource(mClient1.getCallingUid(), TEST_PACKAGE_NAME)); 981 checkIsP2pTearDownWhenClientDisconnected(true, false, mClient1, null); 982 } 983 984 /** 985 * Verify that p2p doesn't init when a client connects / disconnects 986 * with wifi disabled 987 */ 988 @Test testP2pDoesntInitWhenClientConnectWithWifiDisabled()989 public void testP2pDoesntInitWhenClientConnectWithWifiDisabled() 990 throws Exception { 991 simulateWifiStateChange(false); 992 checkIsP2pInitWhenClientConnected(false, false, mClient1, 993 new WorkSource(mClient1.getCallingUid(), TEST_PACKAGE_NAME)); 994 checkIsP2pTearDownWhenClientDisconnected(false, false, mClient1, null); 995 } 996 997 /** 998 * Verify that p2p init / teardown when wifi off / on 999 * with a client connected 1000 */ 1001 @Test checkIsP2pInitForWifiChanges()1002 public void checkIsP2pInitForWifiChanges() throws Exception { 1003 forceP2pEnabled(mClient1); 1004 1005 simulateWifiStateChange(false); 1006 mLooper.dispatchAll(); 1007 verify(mWifiNative).teardownInterface(); 1008 verify(mWifiMonitor).stopMonitoring(anyString()); 1009 // Force to back disable state for next test 1010 mockEnterDisabledState(); 1011 1012 // wifi off / on won't initialize the p2p interface. 1013 simulateWifiStateChange(true); 1014 mLooper.dispatchAll(); 1015 verify(mWifiNative, times(1)).setupInterface(any(), any(), any()); 1016 verify(mNetdWrapper, times(1)).setInterfaceUp(anyString()); 1017 verify(mWifiMonitor, atLeastOnce()).registerHandler(anyString(), anyInt(), any()); 1018 1019 // Lazy initialization is done once receiving a command. 1020 sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_P2P_STATE); 1021 verify(mWifiNative, times(2)).setupInterface(any(), any(), any()); 1022 verify(mNetdWrapper, times(2)).setInterfaceUp(anyString()); 1023 } 1024 1025 /** 1026 * Verify p2p init / teardown when two clients connect / disconnect 1027 */ 1028 @Test checkIsP2pInitForTwoClientsConnection()1029 public void checkIsP2pInitForTwoClientsConnection() throws Exception { 1030 forceP2pEnabled(mClient1); 1031 WorkSource expectedRequestorWs = 1032 new WorkSource(mClient1.getCallingUid(), TEST_PACKAGE_NAME); 1033 expectedRequestorWs.add(mClient2.getCallingUid(), TEST_PACKAGE_NAME); 1034 // P2pInit check count should keep in once, same as one client connected case. 1035 checkIsP2pInitWhenClientConnected(false, true, mClient2, expectedRequestorWs); 1036 reset(mWifiNative); 1037 checkIsP2pTearDownWhenClientDisconnected(false, true, mClient2, 1038 new WorkSource(mClient1.getCallingUid(), TEST_PACKAGE_NAME)); 1039 checkIsP2pTearDownWhenClientDisconnected(true, false, mClient1, null); 1040 } 1041 1042 /** 1043 * Verify WifiP2pManager.ADD_LOCAL_SERVICE_FAILED is returned when a caller 1044 * uses abnormal way to send WifiP2pManager.ADD_LOCAL_SERVICE (i.e no channel info updated). 1045 */ 1046 @Test testAddLocalServiceFailureWhenNoChannelUpdated()1047 public void testAddLocalServiceFailureWhenNoChannelUpdated() throws Exception { 1048 forceP2pEnabled(mClient1); 1049 sendAddLocalServiceMsg(mClientMessenger); 1050 verify(mWifiNative, never()).p2pServiceAdd(any()); 1051 assertTrue(mClientHandler.hasMessages(WifiP2pManager.ADD_LOCAL_SERVICE_FAILED)); 1052 } 1053 1054 /** 1055 * Verify WifiP2pManager.ADD_LOCAL_SERVICE_FAILED is returned when a caller 1056 * uses wrong package name to initialize a channel. 1057 */ 1058 @Test testAddLocalServiceFailureWhenChannelUpdateWrongPkgName()1059 public void testAddLocalServiceFailureWhenChannelUpdateWrongPkgName() throws Exception { 1060 forceP2pEnabled(mClient1); 1061 doThrow(new SecurityException("P2p unit test")) 1062 .when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString()); 1063 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1064 sendAddLocalServiceMsg(mClientMessenger); 1065 assertTrue(mClientHandler.hasMessages(WifiP2pManager.ADD_LOCAL_SERVICE_FAILED)); 1066 verify(mWifiNative, never()).p2pServiceAdd(any()); 1067 } 1068 1069 /** 1070 * Verify WifiP2pManager.ADD_LOCAL_SERVICE_FAILED is returned when a caller 1071 * without proper permission attmepts to send WifiP2pManager.ADD_LOCAL_SERVICE. 1072 */ 1073 @Test testAddLocalServiceFailureWhenCallerPermissionDenied()1074 public void testAddLocalServiceFailureWhenCallerPermissionDenied() throws Exception { 1075 forceP2pEnabled(mClient1); 1076 doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString()); 1077 when(mWifiPermissionsUtil.checkCanAccessWifiDirect( 1078 anyString(), anyString(), anyInt(), anyBoolean())).thenReturn(false); 1079 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1080 sendAddLocalServiceMsg(mClientMessenger); 1081 assertTrue(mClientHandler.hasMessages(WifiP2pManager.ADD_LOCAL_SERVICE_FAILED)); 1082 verify(mWifiNative, never()).p2pServiceAdd(any()); 1083 verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), 1084 anyInt(), eq(false)); 1085 } 1086 verifyAddLocalService()1087 private void verifyAddLocalService() throws Exception { 1088 doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString()); 1089 when(mWifiNative.p2pServiceAdd(any())).thenReturn(true); 1090 sendAddLocalServiceMsg(mClientMessenger); 1091 verify(mWifiNative).p2pServiceAdd(any()); 1092 verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), 1093 anyInt(), eq(false)); 1094 assertTrue(mClientHandler.hasMessages(WifiP2pManager.ADD_LOCAL_SERVICE_SUCCEEDED)); 1095 } 1096 1097 /** 1098 * Verify the caller with proper permission sends WifiP2pManager.ADD_LOCAL_SERVICE. 1099 */ 1100 @Test testAddLocalServiceSuccess()1101 public void testAddLocalServiceSuccess() throws Exception { 1102 forceP2pEnabled(mClient1); 1103 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1104 verifyAddLocalService(); 1105 } 1106 1107 /** 1108 * Verify WifiP2pManager.ADD_LOCAL_SERVICE_FAILED is returned when native call failure. 1109 */ 1110 @Test testAddLocalServiceFailureWhenNativeCallFailure()1111 public void testAddLocalServiceFailureWhenNativeCallFailure() throws Exception { 1112 forceP2pEnabled(mClient1); 1113 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1114 doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString()); 1115 when(mWifiNative.p2pServiceAdd(any())).thenReturn(false); 1116 sendAddLocalServiceMsg(mClientMessenger); 1117 verify(mWifiNative).p2pServiceAdd(any()); 1118 verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), 1119 anyInt(), eq(false)); 1120 assertTrue(mClientHandler.hasMessages(WifiP2pManager.ADD_LOCAL_SERVICE_FAILED)); 1121 } 1122 1123 /** 1124 * Verify WifiP2pManager.CONNECT_FAILED is returned when a caller 1125 * uses abnormal way to send WifiP2pManager.CONNECT (i.e no channel info updated). 1126 */ 1127 @Test testConnectWithConfigValidAsGroupFailureWhenNoChannelUpdated()1128 public void testConnectWithConfigValidAsGroupFailureWhenNoChannelUpdated() throws Exception { 1129 forceP2pEnabled(mClient1); 1130 sendConnectMsgWithConfigValidAsGroup(mClientMessenger); 1131 assertTrue(mClientHandler.hasMessages(WifiP2pManager.CONNECT_FAILED)); 1132 verify(mWifiNative, never()).p2pGroupAdd(any(), anyBoolean()); 1133 } 1134 1135 /** 1136 * Verify WifiP2pManager.CONNECT_FAILED is returned when a caller 1137 * uses wrong package name to initialize a channel. 1138 */ 1139 @Test testConnectWithConfigValidAsGroupFailureWhenChannelUpdateWrongPkgName()1140 public void testConnectWithConfigValidAsGroupFailureWhenChannelUpdateWrongPkgName() 1141 throws Exception { 1142 forceP2pEnabled(mClient1); 1143 doThrow(new SecurityException("P2p unit test")) 1144 .when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString()); 1145 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1146 sendConnectMsgWithConfigValidAsGroup(mClientMessenger); 1147 assertTrue(mClientHandler.hasMessages(WifiP2pManager.CONNECT_FAILED)); 1148 verify(mWifiNative, never()).p2pGroupAdd(any(), anyBoolean()); 1149 } 1150 1151 /** 1152 * Verify WifiP2pManager.CONNECT_FAILED is returned when a caller 1153 * without proper permission attmepts to send WifiP2pManager.CONNECT. 1154 */ 1155 @Test testConnectWithConfigValidAsGroupFailureWhenPermissionDenied()1156 public void testConnectWithConfigValidAsGroupFailureWhenPermissionDenied() throws Exception { 1157 forceP2pEnabled(mClient1); 1158 doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString()); 1159 when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(), 1160 anyBoolean())).thenReturn(false); 1161 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1162 sendConnectMsgWithConfigValidAsGroup(mClientMessenger); 1163 assertTrue(mClientHandler.hasMessages(WifiP2pManager.CONNECT_FAILED)); 1164 verify(mWifiNative, never()).p2pGroupAdd(any(), anyBoolean()); 1165 verify(mWifiPermissionsUtil) 1166 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(false)); 1167 } 1168 1169 /** 1170 * Verify the caller with proper permission sends WifiP2pManager.CONNECT. 1171 */ 1172 @Test testConnectWithConfigValidAsGroupSuccess()1173 public void testConnectWithConfigValidAsGroupSuccess() throws Exception { 1174 forceP2pEnabled(mClient1); 1175 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1176 when(mWifiNative.p2pGroupAdd(any(), eq(true))).thenReturn(true); 1177 sendConnectMsgWithConfigValidAsGroup(mClientMessenger); 1178 verify(mWifiNative).p2pGroupAdd(any(), eq(true)); 1179 verify(mWifiPermissionsUtil) 1180 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(false)); 1181 assertTrue(mClientHandler.hasMessages(WifiP2pManager.CONNECT_SUCCEEDED)); 1182 } 1183 1184 /** 1185 * Verify WifiP2pManager.CONNECT_FAILED is returned when native call failure. 1186 */ 1187 @Test testConnectWithConfigValidAsGroupFailureWhenNativeCallFailure()1188 public void testConnectWithConfigValidAsGroupFailureWhenNativeCallFailure() throws Exception { 1189 forceP2pEnabled(mClient1); 1190 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1191 when(mWifiNative.p2pGroupAdd(any(), eq(true))).thenReturn(false); 1192 sendConnectMsgWithConfigValidAsGroup(mClientMessenger); 1193 verify(mWifiNative).p2pGroupAdd(any(), eq(true)); 1194 verify(mWifiPermissionsUtil) 1195 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(false)); 1196 assertTrue(mClientHandler.hasMessages(WifiP2pManager.CONNECT_FAILED)); 1197 } 1198 1199 /** 1200 * Verify WifiP2pManager.CREATE_GROUP_FAILED is returned when a caller 1201 * uses abnormal way to send WifiP2pManager.CREATE_GROUP (i.e no channel info updated). 1202 */ 1203 @Test testCreateGroupWithConfigValidAsGroupFailureWhenNoChannelUpdated()1204 public void testCreateGroupWithConfigValidAsGroupFailureWhenNoChannelUpdated() 1205 throws Exception { 1206 forceP2pEnabled(mClient1); 1207 sendCreateGroupMsgWithConfigValidAsGroup(mClientMessenger); 1208 assertTrue(mClientHandler.hasMessages(WifiP2pManager.CREATE_GROUP_FAILED)); 1209 verify(mWifiNative, never()).p2pGroupAdd(anyBoolean()); 1210 verify(mWifiNative, never()).p2pGroupAdd(any(), anyBoolean()); 1211 } 1212 1213 /** 1214 * Verify WifiP2pManager.CREATE_GROUP_FAILED is returned with null object when a caller 1215 * uses wrong package name to initialize a channel. 1216 */ 1217 @Test testCreateGroupWithConfigValidAsGroupFailureWhenChannelUpdateWrongPkgName()1218 public void testCreateGroupWithConfigValidAsGroupFailureWhenChannelUpdateWrongPkgName() 1219 throws Exception { 1220 forceP2pEnabled(mClient1); 1221 doThrow(new SecurityException("P2p unit test")) 1222 .when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString()); 1223 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1224 sendCreateGroupMsgWithConfigValidAsGroup(mClientMessenger); 1225 assertTrue(mClientHandler.hasMessages(WifiP2pManager.CREATE_GROUP_FAILED)); 1226 verify(mWifiNative, never()).p2pGroupAdd(anyBoolean()); 1227 verify(mWifiNative, never()).p2pGroupAdd(any(), anyBoolean()); 1228 } 1229 1230 /** 1231 * Verify WifiP2pManager.CREATE_GROUP_FAILED is returned when a caller 1232 * without proper permission attmepts to send WifiP2pManager.CREATE_GROUP. 1233 */ 1234 @Test testCreateGroupWithConfigValidAsGroupFailureWhenPermissionDenied()1235 public void testCreateGroupWithConfigValidAsGroupFailureWhenPermissionDenied() 1236 throws Exception { 1237 forceP2pEnabled(mClient1); 1238 doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString()); 1239 when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(), 1240 anyBoolean())).thenReturn(false); 1241 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1242 sendCreateGroupMsgWithConfigValidAsGroup(mClientMessenger); 1243 assertTrue(mClientHandler.hasMessages(WifiP2pManager.CREATE_GROUP_FAILED)); 1244 verify(mWifiNative, never()).p2pGroupAdd(anyBoolean()); 1245 verify(mWifiNative, never()).p2pGroupAdd(any(), anyBoolean()); 1246 verify(mWifiPermissionsUtil) 1247 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(false)); 1248 } 1249 1250 /** 1251 * Verify the caller with proper permission sends WifiP2pManager.CREATE_GROUP. 1252 */ 1253 @Test testCreateGroupWithConfigValidAsGroupSuccess()1254 public void testCreateGroupWithConfigValidAsGroupSuccess() throws Exception { 1255 forceP2pEnabled(mClient1); 1256 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1257 when(mWifiNative.p2pGroupAdd(any(), eq(false))).thenReturn(true); 1258 sendCreateGroupMsgWithConfigValidAsGroup(mClientMessenger); 1259 verify(mWifiNative).p2pGroupAdd(any(), eq(false)); 1260 verify(mWifiPermissionsUtil) 1261 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(false)); 1262 assertTrue(mClientHandler.hasMessages(WifiP2pManager.CREATE_GROUP_SUCCEEDED)); 1263 } 1264 1265 /** 1266 * Verify WifiP2pManager.CREATE_GROUP_FAILED is returned when native call failure. 1267 */ 1268 @Test testCreateGroupWithConfigValidAsGroupFailureWhenNativeCallFailure()1269 public void testCreateGroupWithConfigValidAsGroupFailureWhenNativeCallFailure() 1270 throws Exception { 1271 forceP2pEnabled(mClient1); 1272 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1273 when(mWifiNative.p2pGroupAdd(any(), eq(false))).thenReturn(false); 1274 sendCreateGroupMsgWithConfigValidAsGroup(mClientMessenger); 1275 verify(mWifiNative).p2pGroupAdd(any(), eq(false)); 1276 verify(mWifiPermissionsUtil) 1277 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(false)); 1278 assertTrue(mClientHandler.hasMessages(WifiP2pManager.CREATE_GROUP_FAILED)); 1279 } 1280 1281 /** 1282 * Verify WifiP2pManager.DISCOVER_PEERS_FAILED is returned when a caller 1283 * uses abnormal way to send WifiP2pManager.REQUEST_PEERS (i.e no channel info updated). 1284 */ 1285 @Test testDiscoverPeersFailureWhenNoChannelUpdated()1286 public void testDiscoverPeersFailureWhenNoChannelUpdated() throws Exception { 1287 forceP2pEnabled(mClient1); 1288 sendDiscoverPeersMsg(mClientMessenger); 1289 verify(mWifiNative, never()).p2pFind(anyInt()); 1290 assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_PEERS_FAILED)); 1291 } 1292 1293 /** 1294 * Verify WifiP2pManager.DISCOVER_PEERS_FAILED is returned when a caller 1295 * uses wrong package name to initialize a channel. 1296 */ 1297 @Test testDiscoverPeersFailureWhenChannelUpdateWrongPkgName()1298 public void testDiscoverPeersFailureWhenChannelUpdateWrongPkgName() throws Exception { 1299 forceP2pEnabled(mClient1); 1300 doThrow(new SecurityException("P2p unit test")) 1301 .when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString()); 1302 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1303 sendDiscoverPeersMsg(mClientMessenger); 1304 verify(mWifiNative, never()).p2pFind(anyInt()); 1305 assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_PEERS_FAILED)); 1306 } 1307 1308 /** 1309 * Verify WifiP2pManager.DISCOVER_PEERS_FAILED is returned with null object when a caller 1310 * without proper permission attmepts to send WifiP2pManager.DISCOVER_PEERS. 1311 */ 1312 @Test testDiscoverPeersFailureWhenPermissionDenied()1313 public void testDiscoverPeersFailureWhenPermissionDenied() throws Exception { 1314 forceP2pEnabled(mClient1); 1315 doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString()); 1316 when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(), 1317 anyBoolean())).thenReturn(false); 1318 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1319 sendDiscoverPeersMsg(mClientMessenger); 1320 verify(mWifiNative, never()).p2pFind(anyInt()); 1321 verify(mWifiPermissionsUtil) 1322 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true)); 1323 assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_PEERS_FAILED)); 1324 } 1325 1326 /** 1327 * Verify WifiP2pManager.DISCOVER_PEERS_FAILED is returned with null object when a caller 1328 * attmepts to send WifiP2pManager.DISCOVER_PEERS and location mode is disabled. 1329 */ 1330 @Test testDiscoverPeersFailureWhenLocationModeDisabled()1331 public void testDiscoverPeersFailureWhenLocationModeDisabled() throws Exception { 1332 forceP2pEnabled(mClient1); 1333 doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString()); 1334 when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(), 1335 eq(false))).thenReturn(true); 1336 when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(), 1337 eq(true))).thenReturn(false); 1338 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1339 sendDiscoverPeersMsg(mClientMessenger); 1340 verify(mWifiNative, never()).p2pFind(anyInt()); 1341 verify(mWifiPermissionsUtil) 1342 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true)); 1343 assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_PEERS_FAILED)); 1344 } 1345 1346 /** 1347 * Verify the caller with proper permission sends WifiP2pManager.DISCOVER_PEERS. 1348 */ 1349 @Test testDiscoverPeersSuccess()1350 public void testDiscoverPeersSuccess() throws Exception { 1351 forceP2pEnabled(mClient1); 1352 when(mWifiNative.p2pFind(anyInt())).thenReturn(true); 1353 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1354 sendDiscoverPeersMsg(mClientMessenger); 1355 verify(mWifiNative).p2pFind(anyInt()); 1356 verify(mWifiPermissionsUtil) 1357 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true)); 1358 assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_PEERS_SUCCEEDED)); 1359 } 1360 1361 /** 1362 * Verify WifiP2pManager.DISCOVER_PEERS_FAILED is returned when native call failure. 1363 */ 1364 @Test testDiscoverPeersFailureWhenNativeCallFailure()1365 public void testDiscoverPeersFailureWhenNativeCallFailure() throws Exception { 1366 forceP2pEnabled(mClient1); 1367 when(mWifiNative.p2pFind(anyInt())).thenReturn(false); 1368 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1369 sendDiscoverPeersMsg(mClientMessenger); 1370 verify(mWifiNative).p2pFind(anyInt()); 1371 verify(mWifiPermissionsUtil) 1372 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true)); 1373 assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_PEERS_FAILED)); 1374 } 1375 1376 1377 /** 1378 * Verify WifiP2pManager.DISCOVER_SERVICES_FAILED is returned when a caller 1379 * uses abnormal way to send WifiP2pManager.DISCOVER_SERVICES (i.e no channel info updated). 1380 */ 1381 @Test testDiscoverServicesFailureWhenNoChannelUpdated()1382 public void testDiscoverServicesFailureWhenNoChannelUpdated() throws Exception { 1383 when(mWifiNative.p2pServDiscReq(anyString(), anyString())).thenReturn("mServiceDiscReqId"); 1384 forceP2pEnabled(mClient1); 1385 sendAddServiceRequestMsg(mClientMessenger); 1386 sendDiscoverServiceMsg(mClientMessenger); 1387 verify(mWifiNative, never()).p2pServDiscReq(anyString(), anyString()); 1388 verify(mWifiNative, never()).p2pFind(anyInt()); 1389 assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_SERVICES_FAILED)); 1390 } 1391 1392 /** 1393 * Verify WifiP2pManager.DISCOVER_SERVICES_FAILED is returned when a caller 1394 * uses wrong package name to initialize a channel. 1395 */ 1396 @Test testDiscoverServicesFailureWhenChannelUpdateWrongPkgName()1397 public void testDiscoverServicesFailureWhenChannelUpdateWrongPkgName() throws Exception { 1398 when(mWifiNative.p2pServDiscReq(anyString(), anyString())).thenReturn("mServiceDiscReqId"); 1399 forceP2pEnabled(mClient1); 1400 doThrow(new SecurityException("P2p unit test")) 1401 .when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString()); 1402 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1403 sendAddServiceRequestMsg(mClientMessenger); 1404 sendDiscoverServiceMsg(mClientMessenger); 1405 verify(mWifiNative, never()).p2pServDiscReq(anyString(), anyString()); 1406 verify(mWifiNative, never()).p2pFind(anyInt()); 1407 assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_SERVICES_FAILED)); 1408 } 1409 1410 /** 1411 * Verify WifiP2pManager.DISCOVER_SERVICES_FAILED is returned when a caller 1412 * without proper permission attmepts to send WifiP2pManager.DISCOVER_SERVICES. 1413 */ 1414 @Test testDiscoverServicesFailureWhenPermissionDenied()1415 public void testDiscoverServicesFailureWhenPermissionDenied() throws Exception { 1416 when(mWifiNative.p2pServDiscReq(anyString(), anyString())) 1417 .thenReturn("mServiceDiscReqId"); 1418 forceP2pEnabled(mClient1); 1419 doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString()); 1420 when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(), 1421 anyBoolean())).thenReturn(false); 1422 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1423 sendAddServiceRequestMsg(mClientMessenger); 1424 sendDiscoverServiceMsg(mClientMessenger); 1425 verify(mWifiNative, never()).p2pServDiscReq(anyString(), anyString()); 1426 verify(mWifiNative, never()).p2pFind(anyInt()); 1427 verify(mWifiPermissionsUtil) 1428 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true)); 1429 assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_SERVICES_FAILED)); 1430 } 1431 1432 /** 1433 * Verify WifiP2pManager.DISCOVER_SERVICES_FAILED is returned when a caller 1434 * attmepts to send WifiP2pManager.DISCOVER_SERVICES and location mode is disabled. 1435 */ 1436 @Test testDiscoverServicesFailureWhenLocationModeDisabled()1437 public void testDiscoverServicesFailureWhenLocationModeDisabled() throws Exception { 1438 when(mWifiNative.p2pServDiscReq(anyString(), anyString())) 1439 .thenReturn("mServiceDiscReqId"); 1440 forceP2pEnabled(mClient1); 1441 doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString()); 1442 when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(), 1443 eq(false))).thenReturn(true); 1444 when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(), 1445 eq(true))).thenReturn(false); 1446 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1447 sendAddServiceRequestMsg(mClientMessenger); 1448 sendDiscoverServiceMsg(mClientMessenger); 1449 verify(mWifiNative, never()).p2pServDiscReq(anyString(), anyString()); 1450 verify(mWifiNative, never()).p2pFind(anyInt()); 1451 verify(mWifiPermissionsUtil) 1452 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true)); 1453 assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_SERVICES_FAILED)); 1454 } 1455 1456 /** 1457 * Verify the caller with proper permission sends WifiP2pManager.DISCOVER_SERVICES. 1458 */ 1459 @Test testDiscoverServicesSuccess()1460 public void testDiscoverServicesSuccess() throws Exception { 1461 when(mWifiNative.p2pServDiscReq(anyString(), anyString())) 1462 .thenReturn("mServiceDiscReqId"); 1463 when(mWifiNative.p2pFind(anyInt())).thenReturn(true); 1464 forceP2pEnabled(mClient1); 1465 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1466 sendAddServiceRequestMsg(mClientMessenger); 1467 sendDiscoverServiceMsg(mClientMessenger); 1468 verify(mWifiNative).p2pServDiscReq(anyString(), anyString()); 1469 verify(mWifiNative).p2pFind(anyInt()); 1470 verify(mWifiPermissionsUtil) 1471 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true)); 1472 assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_SERVICES_SUCCEEDED)); 1473 } 1474 1475 /** 1476 * Verify WifiP2pManager.DISCOVER_SERVICES_FAILED is returned when add service failure. 1477 */ 1478 @Test testDiscoverServicesFailureWhenAddServiceRequestFailure()1479 public void testDiscoverServicesFailureWhenAddServiceRequestFailure() throws Exception { 1480 when(mWifiNative.p2pServDiscReq(anyString(), anyString())).thenReturn(null); 1481 forceP2pEnabled(mClient1); 1482 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1483 sendAddServiceRequestMsg(mClientMessenger); 1484 sendDiscoverServiceMsg(mClientMessenger); 1485 verify(mWifiNative).p2pServDiscReq(anyString(), anyString()); 1486 verify(mWifiNative, never()).p2pFind(anyInt()); 1487 verify(mWifiPermissionsUtil) 1488 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true)); 1489 assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_SERVICES_FAILED)); 1490 } 1491 1492 /** 1493 * Verify WifiP2pManager.DISCOVER_SERVICES_FAILED is returned when native call failure. 1494 */ 1495 @Test testDiscoverServicesFailureWhenNativeCallFailure()1496 public void testDiscoverServicesFailureWhenNativeCallFailure() throws Exception { 1497 when(mWifiNative.p2pServDiscReq(anyString(), anyString())) 1498 .thenReturn("mServiceDiscReqId"); 1499 when(mWifiNative.p2pFind(anyInt())).thenReturn(false); 1500 forceP2pEnabled(mClient1); 1501 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1502 sendAddServiceRequestMsg(mClientMessenger); 1503 sendDiscoverServiceMsg(mClientMessenger); 1504 verify(mWifiNative).p2pServDiscReq(anyString(), anyString()); 1505 verify(mWifiNative).p2pFind(anyInt()); 1506 verify(mWifiPermissionsUtil) 1507 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true)); 1508 assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_SERVICES_FAILED)); 1509 } 1510 1511 /** 1512 * Verify WifiP2pManager.RESPONSE_PEERS is returned with null object when a caller 1513 * uses abnormal way to send WifiP2pManager.REQUEST_PEERS (i.e no channel info updated). 1514 */ 1515 @Test testRequestPeersFailureWhenNoChannelUpdated()1516 public void testRequestPeersFailureWhenNoChannelUpdated() throws Exception { 1517 forceP2pEnabled(mClient1); 1518 mockPeersList(); 1519 sendRequestPeersMsg(mClientMessenger); 1520 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 1521 WifiP2pDeviceList peers = (WifiP2pDeviceList) mMessageCaptor.getValue().obj; 1522 assertEquals(WifiP2pManager.RESPONSE_PEERS, mMessageCaptor.getValue().what); 1523 assertNull(peers.get(mTestWifiP2pDevice.deviceAddress)); 1524 1525 } 1526 1527 /** 1528 * Verify WifiP2pManager.RESPONSE_PEERS is returned with null object when a caller 1529 * uses wrong package name to initialize a channel. 1530 */ 1531 @Test testRequestPeersFailureWhenChannelUpdateWrongPkgName()1532 public void testRequestPeersFailureWhenChannelUpdateWrongPkgName() throws Exception { 1533 forceP2pEnabled(mClient1); 1534 mockPeersList(); 1535 doThrow(new SecurityException("P2p unit test")) 1536 .when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString()); 1537 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1538 sendRequestPeersMsg(mClientMessenger); 1539 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 1540 WifiP2pDeviceList peers = (WifiP2pDeviceList) mMessageCaptor.getValue().obj; 1541 assertEquals(WifiP2pManager.RESPONSE_PEERS, mMessageCaptor.getValue().what); 1542 assertNull(peers.get(mTestWifiP2pDevice.deviceAddress)); 1543 } 1544 1545 /** 1546 * Verify WifiP2pManager.RESPONSE_PEERS is returned with null object when a caller 1547 * without proper permission attmepts to send WifiP2pManager.REQUEST_PEERS. 1548 */ 1549 @Test testRequestPeersFailureWhenPermissionDenied()1550 public void testRequestPeersFailureWhenPermissionDenied() throws Exception { 1551 forceP2pEnabled(mClient1); 1552 mockPeersList(); 1553 doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString()); 1554 when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(), 1555 anyBoolean())).thenReturn(false); 1556 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1557 sendRequestPeersMsg(mClientMessenger); 1558 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 1559 verify(mWifiPermissionsUtil) 1560 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true)); 1561 WifiP2pDeviceList peers = (WifiP2pDeviceList) mMessageCaptor.getValue().obj; 1562 assertEquals(WifiP2pManager.RESPONSE_PEERS, mMessageCaptor.getValue().what); 1563 assertNull(peers.get(mTestWifiP2pDevice.deviceAddress)); 1564 1565 } 1566 1567 /** 1568 * Verify WifiP2pManager.RESPONSE_PEERS is returned with null object when a caller 1569 * attmepts to send WifiP2pManager.REQUEST_PEERS and location mode is disabled. 1570 */ 1571 @Test testRequestPeersFailureWhenLocationModeDisabled()1572 public void testRequestPeersFailureWhenLocationModeDisabled() throws Exception { 1573 forceP2pEnabled(mClient1); 1574 mockPeersList(); 1575 doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString()); 1576 when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(), 1577 eq(false))).thenReturn(true); 1578 when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(), 1579 eq(true))).thenReturn(false); 1580 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1581 sendRequestPeersMsg(mClientMessenger); 1582 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 1583 verify(mWifiPermissionsUtil) 1584 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true)); 1585 WifiP2pDeviceList peers = (WifiP2pDeviceList) mMessageCaptor.getValue().obj; 1586 assertEquals(WifiP2pManager.RESPONSE_PEERS, mMessageCaptor.getValue().what); 1587 assertNull(peers.get(mTestWifiP2pDevice.deviceAddress)); 1588 1589 } 1590 1591 /** 1592 * Verify WifiP2pManager.RESPONSE_PEERS is returned with expect object when a caller 1593 * with proper permission to send WifiP2pManager.REQUEST_PEERS. 1594 */ 1595 @Test testRequestPeersSuccess()1596 public void testRequestPeersSuccess() throws Exception { 1597 forceP2pEnabled(mClient1); 1598 mockPeersList(); 1599 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1600 sendRequestPeersMsg(mClientMessenger); 1601 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 1602 verify(mWifiPermissionsUtil) 1603 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true)); 1604 WifiP2pDeviceList peers = (WifiP2pDeviceList) mMessageCaptor.getValue().obj; 1605 assertEquals(WifiP2pManager.RESPONSE_PEERS, mMessageCaptor.getValue().what); 1606 assertNotEquals(null, peers.get(mTestWifiP2pDevice.deviceAddress)); 1607 } 1608 1609 /** 1610 * Verify WifiP2pManager.RESPONSE_GROUP_INFO is returned with null object when a caller 1611 * uses abnormal way to send WifiP2pManager.REQUEST_GROUP_INFO (i.e no channel info updated). 1612 */ 1613 @Test testRequestGroupInfoFailureWhenNoChannelUpdated()1614 public void testRequestGroupInfoFailureWhenNoChannelUpdated() throws Exception { 1615 forceP2pEnabled(mClient1); 1616 sendGroupStartedMsg(mTestWifiP2pGroup); 1617 simulateTetherReady(); 1618 sendRequestGroupInfoMsg(mClientMessenger); 1619 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 1620 assertEquals(WifiP2pManager.RESPONSE_GROUP_INFO, mMessageCaptor.getValue().what); 1621 assertNull(mMessageCaptor.getValue().obj); 1622 } 1623 1624 /** 1625 * Verify WifiP2pManager.RESPONSE_GROUP_INFO is returned with null object when a caller 1626 * uses wrong package name to initialize a channel. 1627 */ 1628 @Test testRequestGroupInfoFailureWhenChannelUpdateWrongPkgName()1629 public void testRequestGroupInfoFailureWhenChannelUpdateWrongPkgName() throws Exception { 1630 forceP2pEnabled(mClient1); 1631 sendGroupStartedMsg(mTestWifiP2pGroup); 1632 simulateTetherReady(); 1633 doThrow(new SecurityException("P2p unit test")) 1634 .when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString()); 1635 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1636 sendRequestGroupInfoMsg(mClientMessenger); 1637 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 1638 assertEquals(WifiP2pManager.RESPONSE_GROUP_INFO, mMessageCaptor.getValue().what); 1639 assertNull(mMessageCaptor.getValue().obj); 1640 } 1641 1642 /** 1643 * Verify WifiP2pManager.RESPONSE_GROUP_INFO is returned with null object when a caller 1644 * without proper permission attempts to send WifiP2pManager.REQUEST_GROUP_INFO. 1645 */ 1646 @Test testRequestGroupInfoFailureWhenPermissionDenied()1647 public void testRequestGroupInfoFailureWhenPermissionDenied() throws Exception { 1648 forceP2pEnabled(mClient1); 1649 sendGroupStartedMsg(mTestWifiP2pGroup); 1650 simulateTetherReady(); 1651 doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString()); 1652 when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(), 1653 anyBoolean())).thenReturn(false); 1654 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1655 sendRequestGroupInfoMsg(mClientMessenger); 1656 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 1657 verify(mWifiPermissionsUtil) 1658 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(false)); 1659 assertEquals(WifiP2pManager.RESPONSE_GROUP_INFO, mMessageCaptor.getValue().what); 1660 assertNull(mMessageCaptor.getValue().obj); 1661 } 1662 1663 /** 1664 * Verify WifiP2pManager.RESPONSE_GROUP_INFO is returned with expect object when a caller 1665 * with proper permission. 1666 */ 1667 @Test testRequestGroupInfoSuccess()1668 public void testRequestGroupInfoSuccess() throws Exception { 1669 mTestWifiP2pGroup.setOwner(mTestThisDevice); 1670 forceP2pEnabled(mClient1); 1671 sendGroupStartedMsg(mTestWifiP2pGroup); 1672 simulateTetherReady(); 1673 when(mWifiPermissionsUtil.checkLocalMacAddressPermission(anyInt())).thenReturn(false); 1674 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1675 sendRequestGroupInfoMsg(mClientMessenger); 1676 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 1677 verify(mWifiPermissionsUtil) 1678 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(false)); 1679 assertEquals(WifiP2pManager.RESPONSE_GROUP_INFO, mMessageCaptor.getValue().what); 1680 WifiP2pGroup wifiP2pGroup = (WifiP2pGroup) mMessageCaptor.getValue().obj; 1681 assertEquals(mTestWifiP2pGroup.getNetworkName(), wifiP2pGroup.getNetworkName()); 1682 // Ensure that our own MAC address is anonymized if we're the group owner. 1683 assertEquals(ANONYMIZED_DEVICE_ADDRESS, wifiP2pGroup.getOwner().deviceAddress); 1684 } 1685 1686 /** 1687 * Verify WifiP2pManager.RESPONSE_GROUP_INFO does not anonymize this device's MAC address when 1688 * requested by an app with the LOCAL_MAC_ADDRESS permission. 1689 */ 1690 @Test testRequestGroupInfoIncludesMacForNetworkSettingsApp()1691 public void testRequestGroupInfoIncludesMacForNetworkSettingsApp() throws Exception { 1692 mTestWifiP2pGroup.setOwner(mTestThisDevice); 1693 forceP2pEnabled(mClient1); 1694 sendGroupStartedMsg(mTestWifiP2pGroup); 1695 simulateTetherReady(); 1696 when(mWifiPermissionsUtil.checkLocalMacAddressPermission(anyInt())).thenReturn(true); 1697 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1698 sendRequestGroupInfoMsg(mClientMessenger); 1699 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 1700 verify(mWifiPermissionsUtil) 1701 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(false)); 1702 assertEquals(WifiP2pManager.RESPONSE_GROUP_INFO, mMessageCaptor.getValue().what); 1703 WifiP2pGroup wifiP2pGroup = (WifiP2pGroup) mMessageCaptor.getValue().obj; 1704 assertEquals(thisDeviceMac, wifiP2pGroup.getOwner().deviceAddress); 1705 } 1706 1707 /** 1708 * Verify WifiP2pManager.START_LISTEN_FAILED is returned when a caller 1709 * without proper permission attempts to send WifiP2pManager.START_LISTEN. 1710 */ 1711 @Test testStartListenFailureWhenPermissionDenied()1712 public void testStartListenFailureWhenPermissionDenied() throws Exception { 1713 when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(false); 1714 forceP2pEnabled(mClient1); 1715 sendSimpleMsg(mClientMessenger, WifiP2pManager.START_LISTEN); 1716 assertTrue(mClientHandler.hasMessages(WifiP2pManager.START_LISTEN_FAILED)); 1717 // p2pFlush should be invoked once in forceP2pEnabled. 1718 verify(mWifiNative).p2pFlush(); 1719 verify(mWifiNative, never()).p2pExtListen(anyBoolean(), anyInt(), anyInt()); 1720 } 1721 1722 /** 1723 * Verify WifiP2pManager.START_LISTEN_FAILED is returned when native call failure. 1724 */ 1725 @Test testStartListenFailureWhenNativeCallFailure()1726 public void testStartListenFailureWhenNativeCallFailure() throws Exception { 1727 when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(true); 1728 when(mWifiNative.p2pExtListen(eq(true), anyInt(), anyInt())).thenReturn(false); 1729 forceP2pEnabled(mClient1); 1730 sendSimpleMsg(mClientMessenger, WifiP2pManager.START_LISTEN); 1731 // p2pFlush would be invoked in forceP2pEnabled and startListen both. 1732 verify(mWifiNative, times(2)).p2pFlush(); 1733 verify(mWifiNative).p2pExtListen(eq(true), anyInt(), anyInt()); 1734 assertTrue(mClientHandler.hasMessages(WifiP2pManager.START_LISTEN_FAILED)); 1735 } 1736 1737 /** 1738 * Verify the caller with proper permission sends WifiP2pManager.START_LISTEN. 1739 */ 1740 @Test testStartListenSuccess()1741 public void testStartListenSuccess() throws Exception { 1742 when(mWifiNative.p2pExtListen(eq(true), anyInt(), anyInt())).thenReturn(true); 1743 when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(true); 1744 forceP2pEnabled(mClient1); 1745 sendSimpleMsg(mClientMessenger, WifiP2pManager.START_LISTEN); 1746 // p2pFlush would be invoked in forceP2pEnabled and startListen both. 1747 verify(mWifiNative, times(2)).p2pFlush(); 1748 verify(mWifiNative).p2pExtListen(eq(true), anyInt(), anyInt()); 1749 assertTrue(mClientHandler.hasMessages(WifiP2pManager.START_LISTEN_SUCCEEDED)); 1750 } 1751 1752 /** 1753 * Verify WifiP2pManager.STOP_LISTEN_FAILED is returned when a caller 1754 * without proper permission attempts to sends WifiP2pManager.STOP_LISTEN. 1755 */ 1756 @Test testStopListenFailureWhenPermissionDenied()1757 public void testStopListenFailureWhenPermissionDenied() throws Exception { 1758 when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(false); 1759 forceP2pEnabled(mClient1); 1760 sendSimpleMsg(mClientMessenger, WifiP2pManager.STOP_LISTEN); 1761 assertTrue(mClientHandler.hasMessages(WifiP2pManager.STOP_LISTEN_FAILED)); 1762 verify(mWifiNative, never()).p2pExtListen(anyBoolean(), anyInt(), anyInt()); 1763 } 1764 1765 /** 1766 * Verify WifiP2pManager.STOP_LISTEN_FAILED is returned when native call failure. 1767 */ 1768 @Test testStopListenFailureWhenNativeCallFailure()1769 public void testStopListenFailureWhenNativeCallFailure() throws Exception { 1770 when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(true); 1771 when(mWifiNative.p2pExtListen(eq(false), anyInt(), anyInt())).thenReturn(false); 1772 forceP2pEnabled(mClient1); 1773 sendSimpleMsg(mClientMessenger, WifiP2pManager.STOP_LISTEN); 1774 verify(mWifiNative).p2pExtListen(eq(false), anyInt(), anyInt()); 1775 assertTrue(mClientHandler.hasMessages(WifiP2pManager.STOP_LISTEN_FAILED)); 1776 } 1777 1778 /** 1779 * Verify the caller with proper permission sends WifiP2pManager.STOP_LISTEN. 1780 */ 1781 @Test testStopListenSuccess()1782 public void testStopListenSuccess() throws Exception { 1783 when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(true); 1784 when(mWifiNative.p2pExtListen(eq(false), anyInt(), anyInt())).thenReturn(true); 1785 forceP2pEnabled(mClient1); 1786 sendSimpleMsg(mClientMessenger, WifiP2pManager.STOP_LISTEN); 1787 verify(mWifiNative).p2pExtListen(eq(false), anyInt(), anyInt()); 1788 assertTrue(mClientHandler.hasMessages(WifiP2pManager.STOP_LISTEN_SUCCEEDED)); 1789 } 1790 1791 /** Verify the p2p randomized MAC feature is enabled if OEM supports it. */ 1792 @Test testP2pRandomMacWithOemSupport()1793 public void testP2pRandomMacWithOemSupport() throws Exception { 1794 when(mResources.getBoolean(R.bool.config_wifi_p2p_mac_randomization_supported)) 1795 .thenReturn(true); 1796 forceP2pEnabled(mClient1); 1797 verify(mWifiNative, never()).setMacRandomization(eq(false)); 1798 verify(mWifiNative).setMacRandomization(eq(true)); 1799 } 1800 1801 /** Verify the p2p randomized MAC feature is disabled if OEM does not support it. */ 1802 @Test testP2pRandomMacWithoutOemSupport()1803 public void testP2pRandomMacWithoutOemSupport() throws Exception { 1804 when(mResources.getBoolean(R.bool.config_wifi_p2p_mac_randomization_supported)) 1805 .thenReturn(false); 1806 forceP2pEnabled(mClient1); 1807 verify(mWifiNative, never()).setMacRandomization(eq(true)); 1808 verify(mWifiNative).setMacRandomization(eq(false)); 1809 } 1810 1811 /** 1812 * Verify the caller sends WifiP2pManager.DELETE_PERSISTENT_GROUP. 1813 */ 1814 @Test testDeletePersistentGroupSuccess()1815 public void testDeletePersistentGroupSuccess() throws Exception { 1816 // Move to enabled state 1817 forceP2pEnabled(mClient1); 1818 1819 sendDeletePersistentGroupMsg(mClientMessenger, WifiP2pGroup.NETWORK_ID_PERSISTENT); 1820 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 1821 Message message = mMessageCaptor.getValue(); 1822 assertEquals(WifiP2pManager.DELETE_PERSISTENT_GROUP_SUCCEEDED, message.what); 1823 } 1824 1825 /** 1826 * Verify that respond with DELETE_PERSISTENT_GROUP_FAILED 1827 * when caller sends DELETE_PERSISTENT_GROUP and p2p is disabled. 1828 */ 1829 @Test testDeletePersistentGroupFailureWhenP2pDisabled()1830 public void testDeletePersistentGroupFailureWhenP2pDisabled() throws Exception { 1831 sendDeletePersistentGroupMsg(mClientMessenger, WifiP2pGroup.NETWORK_ID_PERSISTENT); 1832 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 1833 Message message = mMessageCaptor.getValue(); 1834 assertEquals(WifiP2pManager.DELETE_PERSISTENT_GROUP_FAILED, message.what); 1835 assertEquals(WifiP2pManager.BUSY, message.arg1); 1836 } 1837 1838 /** 1839 * Verify that respond with DELETE_PERSISTENT_GROUP_FAILED 1840 * when caller sends DELETE_PERSISTENT_GROUP and p2p is unsupported. 1841 */ 1842 @Test testDeletePersistentGroupFailureWhenP2pUnsupported()1843 public void testDeletePersistentGroupFailureWhenP2pUnsupported() throws Exception { 1844 setUpWifiP2pServiceImpl(false); 1845 sendDeletePersistentGroupMsg(mClientMessenger, WifiP2pGroup.NETWORK_ID_PERSISTENT); 1846 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 1847 Message message = mMessageCaptor.getValue(); 1848 assertEquals(WifiP2pManager.DELETE_PERSISTENT_GROUP_FAILED, message.what); 1849 assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1); 1850 } 1851 1852 /** 1853 * Verify that respond with DELETE_PERSISTENT_GROUP_FAILED 1854 * when caller sends DELETE_PERSISTENT_GROUP and doesn't have the necessary permissions. 1855 */ 1856 @Test testDeletePersistentGroupFailureWhenNoPermissions()1857 public void testDeletePersistentGroupFailureWhenNoPermissions() throws Exception { 1858 // Move to enabled state 1859 forceP2pEnabled(mClient1); 1860 1861 // no permissions held 1862 when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(false); 1863 when(mWifiPermissionsUtil.checkNetworkStackPermission(anyInt())).thenReturn(false); 1864 when(mWifiPermissionsUtil.checkConfigOverridePermission(anyInt())).thenReturn(false); 1865 1866 sendDeletePersistentGroupMsg(mClientMessenger, WifiP2pGroup.NETWORK_ID_PERSISTENT); 1867 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 1868 Message message = mMessageCaptor.getValue(); 1869 assertEquals(WifiP2pManager.DELETE_PERSISTENT_GROUP_FAILED, message.what); 1870 assertEquals(WifiP2pManager.ERROR, message.arg1); 1871 } 1872 1873 /** 1874 * Verify the peer scan counter is increased while receiving WifiP2pManager.DISCOVER_PEERS at 1875 * P2pEnabledState. 1876 */ 1877 @Test testPeerScanMetricWhenSendDiscoverPeers()1878 public void testPeerScanMetricWhenSendDiscoverPeers() throws Exception { 1879 forceP2pEnabled(mClient1); 1880 when(mWifiNative.p2pFind(anyInt())).thenReturn(true); 1881 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1882 sendDiscoverPeersMsg(mClientMessenger); 1883 verify(mWifiP2pMetrics).incrementPeerScans(); 1884 verify(mWifiPermissionsUtil) 1885 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true)); 1886 } 1887 1888 /** 1889 * Verify the service scan counter is increased while receiving 1890 * WifiP2pManager.DISCOVER_SERVICES at P2pEnabledState. 1891 */ 1892 @Test testServiceScanMetricWhenSendDiscoverServices()1893 public void testServiceScanMetricWhenSendDiscoverServices() throws Exception { 1894 when(mWifiNative.p2pServDiscReq(anyString(), anyString())) 1895 .thenReturn("mServiceDiscReqId"); 1896 when(mWifiNative.p2pFind(anyInt())).thenReturn(true); 1897 forceP2pEnabled(mClient1); 1898 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1899 sendAddServiceRequestMsg(mClientMessenger); 1900 sendDiscoverServiceMsg(mClientMessenger); 1901 verify(mWifiP2pMetrics).incrementServiceScans(); 1902 verify(mWifiPermissionsUtil) 1903 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true)); 1904 } 1905 1906 /** 1907 * Verify the persistent group counter is updated while receiving 1908 * WifiP2pManager.FACTORY_RESET. 1909 */ 1910 @Test testPersistentGroupMetricWhenSendFactoryReset()1911 public void testPersistentGroupMetricWhenSendFactoryReset() throws Exception { 1912 forceP2pEnabled(mClient1); 1913 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1914 1915 // permissions for factory reset 1916 when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())) 1917 .thenReturn(true); 1918 when(mUserManager.hasUserRestrictionForUser(eq(UserManager.DISALLOW_NETWORK_RESET), any())) 1919 .thenReturn(false); 1920 when(mUserManager.hasUserRestrictionForUser(eq(UserManager.DISALLOW_CONFIG_WIFI), any())) 1921 .thenReturn(false); 1922 1923 ArgumentCaptor<WifiP2pGroupList> groupsCaptor = 1924 ArgumentCaptor.forClass(WifiP2pGroupList.class); 1925 verify(mWifiP2pMetrics).updatePersistentGroup(groupsCaptor.capture()); 1926 assertEquals(3, groupsCaptor.getValue().getGroupList().size()); 1927 1928 sendSimpleMsg(mClientMessenger, WifiP2pManager.FACTORY_RESET); 1929 1930 verify(mWifiP2pMetrics, times(2)).updatePersistentGroup(groupsCaptor.capture()); 1931 // the captured object is the same object, just get the latest one is ok. 1932 assertEquals(0, groupsCaptor.getValue().getGroupList().size()); 1933 } 1934 1935 /** 1936 * Verify the persistent group counter is updated while receiving 1937 * WifiP2pMonitor.P2P_GROUP_STARTED_EVENT. 1938 */ 1939 @Test testPersistentGroupMetricWhenSendP2pGroupStartedEvent()1940 public void testPersistentGroupMetricWhenSendP2pGroupStartedEvent() throws Exception { 1941 forceP2pEnabled(mClient1); 1942 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1943 1944 ArgumentCaptor<WifiP2pGroupList> groupsCaptor = 1945 ArgumentCaptor.forClass(WifiP2pGroupList.class); 1946 verify(mWifiP2pMetrics).updatePersistentGroup(groupsCaptor.capture()); 1947 assertEquals(3, groupsCaptor.getValue().getGroupList().size()); 1948 1949 sendGroupStartedMsg(mTestWifiP2pNewPersistentGoGroup); 1950 simulateTetherReady(); 1951 1952 verify(mWifiP2pMetrics, times(2)).updatePersistentGroup(groupsCaptor.capture()); 1953 // the captured object is the same object, just get the latest one is ok. 1954 assertEquals(4, groupsCaptor.getValue().getGroupList().size()); 1955 } 1956 1957 /** 1958 * Verify the persistent group counter is updated while receiving 1959 * WifiP2pManager.DELETE_PERSISTENT_GROUP. 1960 */ 1961 @Test testPersistentGroupMetricWhenSendDeletePersistentGroup()1962 public void testPersistentGroupMetricWhenSendDeletePersistentGroup() throws Exception { 1963 forceP2pEnabled(mClient1); 1964 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1965 1966 ArgumentCaptor<WifiP2pGroupList> groupsCaptor = 1967 ArgumentCaptor.forClass(WifiP2pGroupList.class); 1968 verify(mWifiP2pMetrics).updatePersistentGroup(groupsCaptor.capture()); 1969 assertEquals(3, groupsCaptor.getValue().getGroupList().size()); 1970 1971 sendDeletePersistentGroupMsg(mClientMessenger, 0); 1972 1973 verify(mWifiP2pMetrics, times(2)).updatePersistentGroup(groupsCaptor.capture()); 1974 // the captured object is the same object, just get the latest one is ok. 1975 assertEquals(2, groupsCaptor.getValue().getGroupList().size()); 1976 } 1977 1978 /** 1979 * Verify the group event. 1980 */ 1981 @Test testGroupEventMetric()1982 public void testGroupEventMetric() throws Exception { 1983 forceP2pEnabled(mClient1); 1984 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 1985 1986 sendGroupStartedMsg(mTestWifiP2pNewPersistentGoGroup); 1987 simulateTetherReady(); 1988 1989 ArgumentCaptor<WifiP2pGroup> groupCaptor = 1990 ArgumentCaptor.forClass(WifiP2pGroup.class); 1991 verify(mWifiP2pMetrics).startGroupEvent(groupCaptor.capture()); 1992 WifiP2pGroup groupCaptured = groupCaptor.getValue(); 1993 assertEquals(mTestWifiP2pNewPersistentGoGroup.toString(), groupCaptured.toString()); 1994 1995 sendGroupRemovedMsg(); 1996 verify(mWifiP2pMetrics).endGroupEvent(); 1997 } 1998 1999 /** 2000 * Verify the connection event for a fresh connection. 2001 */ 2002 @Test testStartFreshConnectionEventWhenSendConnect()2003 public void testStartFreshConnectionEventWhenSendConnect() throws Exception { 2004 forceP2pEnabled(mClient1); 2005 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 2006 2007 mockPeersList(); 2008 sendConnectMsg(mClientMessenger, mTestWifiP2pPeerConfig); 2009 verify(mWifiPermissionsUtil) 2010 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(false)); 2011 2012 ArgumentCaptor<WifiP2pConfig> configCaptor = 2013 ArgumentCaptor.forClass(WifiP2pConfig.class); 2014 verify(mWifiP2pMetrics).startConnectionEvent( 2015 eq(P2pConnectionEvent.CONNECTION_FRESH), 2016 configCaptor.capture()); 2017 assertEquals(mTestWifiP2pPeerConfig.toString(), configCaptor.getValue().toString()); 2018 } 2019 2020 /** 2021 * Verify the connection event for a reinvoked connection. 2022 */ 2023 @Test testStartReinvokeConnectionEventWhenSendConnect()2024 public void testStartReinvokeConnectionEventWhenSendConnect() throws Exception { 2025 forceP2pEnabled(mClient1); 2026 when(mWifiNative.p2pGroupAdd(anyInt())) 2027 .thenReturn(true); 2028 when(mTestWifiP2pDevice.isGroupOwner()).thenReturn(true); 2029 when(mWifiNative.p2pGetSsid(eq(mTestWifiP2pDevice.deviceAddress))) 2030 .thenReturn(mTestWifiP2pGroup.getNetworkName()); 2031 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 2032 2033 mockPeersList(); 2034 sendConnectMsg(mClientMessenger, mTestWifiP2pPeerConfig); 2035 verify(mWifiPermissionsUtil) 2036 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(false)); 2037 2038 ArgumentCaptor<WifiP2pConfig> configCaptor = 2039 ArgumentCaptor.forClass(WifiP2pConfig.class); 2040 verify(mWifiP2pMetrics).startConnectionEvent( 2041 eq(P2pConnectionEvent.CONNECTION_REINVOKE), 2042 configCaptor.capture()); 2043 assertEquals(mTestWifiP2pPeerConfig.toString(), configCaptor.getValue().toString()); 2044 } 2045 2046 /** 2047 * Verify the connection event for a reinvoked connection via 2048 * createGroup API. 2049 * 2050 * If there is a persistent group whose owner is this deivce, this would be 2051 * a reinvoked group. 2052 */ 2053 @Test testStartReinvokeConnectionEventWhenCreateGroup()2054 public void testStartReinvokeConnectionEventWhenCreateGroup() 2055 throws Exception { 2056 forceP2pEnabled(mClient1); 2057 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 2058 2059 sendCreateGroupMsg(mClientMessenger, WifiP2pGroup.NETWORK_ID_PERSISTENT, null); 2060 verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), 2061 anyInt(), eq(false)); 2062 2063 verify(mWifiP2pMetrics).startConnectionEvent( 2064 eq(P2pConnectionEvent.CONNECTION_REINVOKE), 2065 eq(null)); 2066 } 2067 2068 /** 2069 * Verify the connection event for a local connection while setting 2070 * netId to {@link WifiP2pGroup#NETWORK_ID_PERSISTENT}. 2071 */ 2072 @Test testStartLocalConnectionWhenCreateGroup()2073 public void testStartLocalConnectionWhenCreateGroup() throws Exception { 2074 forceP2pEnabled(mClient1); 2075 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 2076 2077 // permissions for factory reset 2078 when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())) 2079 .thenReturn(true); 2080 when(mUserManager.hasUserRestrictionForUser(eq(UserManager.DISALLOW_NETWORK_RESET), any())) 2081 .thenReturn(false); 2082 when(mUserManager.hasUserRestrictionForUser(eq(UserManager.DISALLOW_CONFIG_WIFI), any())) 2083 .thenReturn(false); 2084 2085 // There is one group hosted by this device in mGroups. 2086 // clear all groups to avoid re-invoking a group. 2087 sendSimpleMsg(mClientMessenger, WifiP2pManager.FACTORY_RESET); 2088 2089 sendCreateGroupMsg(mClientMessenger, WifiP2pGroup.NETWORK_ID_PERSISTENT, null); 2090 verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), 2091 anyInt(), eq(false)); 2092 2093 verify(mWifiP2pMetrics).startConnectionEvent( 2094 eq(P2pConnectionEvent.CONNECTION_LOCAL), 2095 eq(null)); 2096 } 2097 2098 /** 2099 * Verify the connection event for a local connection while setting the 2100 * netId to {@link WifiP2pGroup#NETWORK_ID_TEMPORARY}. 2101 */ 2102 @Test testStartLocalConnectionEventWhenCreateTemporaryGroup()2103 public void testStartLocalConnectionEventWhenCreateTemporaryGroup() throws Exception { 2104 forceP2pEnabled(mClient1); 2105 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 2106 2107 sendCreateGroupMsg(mClientMessenger, WifiP2pGroup.NETWORK_ID_TEMPORARY, null); 2108 verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), 2109 anyInt(), eq(false)); 2110 2111 verify(mWifiP2pMetrics).startConnectionEvent( 2112 eq(P2pConnectionEvent.CONNECTION_LOCAL), 2113 eq(null)); 2114 } 2115 2116 /** 2117 * Verify the connection event for a fast connection via 2118 * connect with config. 2119 */ 2120 @Test testStartFastConnectionEventWhenSendConnectWithConfig()2121 public void testStartFastConnectionEventWhenSendConnectWithConfig() 2122 throws Exception { 2123 forceP2pEnabled(mClient1); 2124 when(mWifiNative.p2pGroupAdd(any(), eq(true))).thenReturn(true); 2125 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 2126 2127 sendConnectMsg(mClientMessenger, mTestWifiP2pFastConnectionConfig); 2128 verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), 2129 anyInt(), eq(false)); 2130 2131 ArgumentCaptor<WifiP2pConfig> configCaptor = 2132 ArgumentCaptor.forClass(WifiP2pConfig.class); 2133 verify(mWifiP2pMetrics).startConnectionEvent( 2134 eq(P2pConnectionEvent.CONNECTION_FAST), 2135 configCaptor.capture()); 2136 assertEquals(mTestWifiP2pFastConnectionConfig.toString(), 2137 configCaptor.getValue().toString()); 2138 } 2139 2140 /** 2141 * Verify the connection event for a fast connection via 2142 * createGroup API with config. 2143 */ 2144 @Test testStartFastConnectionEventWhenCreateGroupWithConfig()2145 public void testStartFastConnectionEventWhenCreateGroupWithConfig() 2146 throws Exception { 2147 forceP2pEnabled(mClient1); 2148 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 2149 2150 sendCreateGroupMsg(mClientMessenger, 0, mTestWifiP2pFastConnectionConfig); 2151 verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), 2152 anyInt(), eq(false)); 2153 2154 ArgumentCaptor<WifiP2pConfig> configCaptor = 2155 ArgumentCaptor.forClass(WifiP2pConfig.class); 2156 verify(mWifiP2pMetrics).startConnectionEvent( 2157 eq(P2pConnectionEvent.CONNECTION_FAST), 2158 configCaptor.capture()); 2159 assertEquals(mTestWifiP2pFastConnectionConfig.toString(), 2160 configCaptor.getValue().toString()); 2161 } 2162 2163 /** 2164 * Verify the connection event ends while the group is formed. 2165 */ 2166 @Test testEndConnectionEventWhenGroupFormed()2167 public void testEndConnectionEventWhenGroupFormed() throws Exception { 2168 forceP2pEnabled(mClient1); 2169 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 2170 2171 WifiP2pGroup group = new WifiP2pGroup(); 2172 group.setNetworkId(WifiP2pGroup.NETWORK_ID_PERSISTENT); 2173 group.setNetworkName("DIRECT-xy-NEW"); 2174 group.setOwner(new WifiP2pDevice("thisDeviceMac")); 2175 group.setIsGroupOwner(true); 2176 group.setInterface(IFACE_NAME_P2P); 2177 sendGroupStartedMsg(group); 2178 simulateTetherReady(); 2179 verify(mWifiP2pMetrics).endConnectionEvent( 2180 eq(P2pConnectionEvent.CLF_NONE)); 2181 } 2182 2183 /** 2184 * Verify the connection event ends due to timeout. 2185 */ 2186 @Test testEndConnectionEventWhenTimeout()2187 public void testEndConnectionEventWhenTimeout() throws Exception { 2188 forceP2pEnabled(mClient1); 2189 when(mWifiNative.p2pGroupAdd(anyBoolean())).thenReturn(true); 2190 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 2191 2192 mockEnterGroupNegotiationState(); 2193 verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), 2194 anyInt(), eq(false)); 2195 2196 mLooper.moveTimeForward(120 * 1000 * 2); 2197 mLooper.dispatchAll(); 2198 2199 verify(mWifiP2pMetrics).endConnectionEvent( 2200 eq(P2pConnectionEvent.CLF_TIMEOUT)); 2201 } 2202 2203 /** 2204 * Verify the connection event ends due to the cancellation. 2205 */ 2206 @Test testEndConnectionEventWhenCancel()2207 public void testEndConnectionEventWhenCancel() throws Exception { 2208 forceP2pEnabled(mClient1); 2209 when(mWifiNative.p2pGroupAdd(anyBoolean())).thenReturn(true); 2210 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 2211 2212 mockEnterGroupNegotiationState(); 2213 verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), 2214 anyInt(), eq(false)); 2215 2216 sendSimpleMsg(mClientMessenger, WifiP2pManager.CANCEL_CONNECT); 2217 2218 verify(mWifiP2pMetrics).endConnectionEvent( 2219 eq(P2pConnectionEvent.CLF_CANCEL)); 2220 } 2221 2222 /** 2223 * Verify the connection event ends due to the provision discovery failure. 2224 */ 2225 @Test testEndConnectionEventWhenProvDiscFailure()2226 public void testEndConnectionEventWhenProvDiscFailure() throws Exception { 2227 forceP2pEnabled(mClient1); 2228 when(mWifiNative.p2pGroupAdd(anyBoolean())).thenReturn(true); 2229 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 2230 2231 mockEnterProvisionDiscoveryState(); 2232 verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), 2233 anyInt(), eq(false)); 2234 2235 sendSimpleMsg(null, WifiP2pMonitor.P2P_PROV_DISC_FAILURE_EVENT); 2236 2237 verify(mWifiP2pMetrics).endConnectionEvent( 2238 eq(P2pConnectionEvent.CLF_PROV_DISC_FAIL)); 2239 } 2240 2241 /** 2242 * Verify the connection event ends due to the group removal. 2243 */ 2244 @Test testEndConnectionEventWhenGroupRemoval()2245 public void testEndConnectionEventWhenGroupRemoval() throws Exception { 2246 forceP2pEnabled(mClient1); 2247 when(mWifiNative.p2pGroupAdd(anyBoolean())).thenReturn(true); 2248 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 2249 2250 mockEnterGroupNegotiationState(); 2251 verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), 2252 anyInt(), eq(false)); 2253 2254 sendSimpleMsg(null, WifiP2pMonitor.P2P_GROUP_REMOVED_EVENT); 2255 verify(mContext).sendBroadcastWithMultiplePermissions( 2256 argThat(new WifiP2pServiceImplTest 2257 .P2pConnectionChangedIntentMatcherForNetworkState(FAILED)), any()); 2258 2259 verify(mWifiP2pMetrics).endConnectionEvent( 2260 eq(P2pConnectionEvent.CLF_UNKNOWN)); 2261 } 2262 2263 /** 2264 * Verify the connection event ends due to the invitation failure. 2265 */ 2266 @Test testEndConnectionEventWhenInvitationFailure()2267 public void testEndConnectionEventWhenInvitationFailure() throws Exception { 2268 forceP2pEnabled(mClient1); 2269 when(mWifiNative.p2pGroupAdd(anyBoolean())).thenReturn(true); 2270 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 2271 2272 mockEnterGroupNegotiationState(); 2273 verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), 2274 anyInt(), eq(false)); 2275 2276 sendInvitationResultMsg(WifiP2pServiceImpl.P2pStatus.UNKNOWN); 2277 2278 verify(mWifiP2pMetrics).endConnectionEvent( 2279 eq(P2pConnectionEvent.CLF_INVITATION_FAIL)); 2280 } 2281 2282 /** 2283 * Verify WifiP2pManager.RESPONSE_DEVICE_INFO is returned with null object when a caller 2284 * without proper permission attempts. 2285 */ 2286 @Test testRequestDeviceInfoFailureWhenPermissionDenied()2287 public void testRequestDeviceInfoFailureWhenPermissionDenied() throws Exception { 2288 forceP2pEnabled(mClient1); 2289 doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString()); 2290 when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(), 2291 anyBoolean())).thenReturn(false); 2292 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 2293 sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_DEVICE_INFO); 2294 verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), 2295 anyInt(), eq(false)); 2296 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2297 assertEquals(WifiP2pManager.RESPONSE_DEVICE_INFO, mMessageCaptor.getValue().what); 2298 assertNull(mMessageCaptor.getValue().obj); 2299 } 2300 2301 /** 2302 * Verify WifiP2pManager.RESPONSE_DEVICE_INFO is returned with expect object when a caller 2303 * with proper permission attempts in p2p enabled state. 2304 */ 2305 @Test testRequestDeviceInfoSuccessWhenP2pEnabled()2306 public void testRequestDeviceInfoSuccessWhenP2pEnabled() throws Exception { 2307 forceP2pEnabled(mClient1); 2308 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 2309 sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_DEVICE_INFO); 2310 verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), 2311 anyInt(), eq(false)); 2312 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2313 assertEquals(WifiP2pManager.RESPONSE_DEVICE_INFO, mMessageCaptor.getValue().what); 2314 WifiP2pDevice wifiP2pDevice = (WifiP2pDevice) mMessageCaptor.getValue().obj; 2315 assertEquals(ANONYMIZED_DEVICE_ADDRESS, wifiP2pDevice.deviceAddress); 2316 assertEquals(thisDeviceName, wifiP2pDevice.deviceName); 2317 } 2318 2319 /** 2320 * Verify WifiP2pManager.RESPONSE_DEVICE_INFO is returned with empty object when a caller 2321 * with proper permission attempts in p2p disabled state. 2322 */ 2323 @Test testRequestDeviceInfoReturnEmptyWifiP2pDeviceWhenP2pDisabled()2324 public void testRequestDeviceInfoReturnEmptyWifiP2pDeviceWhenP2pDisabled() throws Exception { 2325 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 2326 sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_DEVICE_INFO); 2327 verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), 2328 anyInt(), eq(false)); 2329 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2330 assertEquals(WifiP2pManager.RESPONSE_DEVICE_INFO, mMessageCaptor.getValue().what); 2331 WifiP2pDevice wifiP2pDevice = (WifiP2pDevice) mMessageCaptor.getValue().obj; 2332 assertEquals("", wifiP2pDevice.deviceAddress); 2333 assertEquals("", wifiP2pDevice.deviceName); 2334 } 2335 2336 /** 2337 * Verify WifiP2pManager.RESPONSE_DEVICE_INFO returns an object with the actual device MAC when 2338 * the caller holds the LOCAL_MAC_ADDRESS permission. 2339 */ 2340 @Test testRequestDeviceInfoReturnsActualMacForNetworkSettingsApp()2341 public void testRequestDeviceInfoReturnsActualMacForNetworkSettingsApp() throws Exception { 2342 forceP2pEnabled(mClient1); 2343 when(mWifiPermissionsUtil.checkLocalMacAddressPermission(anyInt())).thenReturn(true); 2344 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 2345 sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_DEVICE_INFO); 2346 verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), 2347 anyInt(), eq(false)); 2348 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2349 assertEquals(WifiP2pManager.RESPONSE_DEVICE_INFO, mMessageCaptor.getValue().what); 2350 WifiP2pDevice wifiP2pDevice = (WifiP2pDevice) mMessageCaptor.getValue().obj; 2351 assertEquals(thisDeviceMac, wifiP2pDevice.deviceAddress); 2352 assertEquals(thisDeviceName, wifiP2pDevice.deviceName); 2353 } 2354 verifyCustomizeDefaultDeviceName(String expectedName, boolean isRandomPostfix)2355 private void verifyCustomizeDefaultDeviceName(String expectedName, boolean isRandomPostfix) 2356 throws Exception { 2357 forceP2pEnabled(mClient1); 2358 when(mWifiPermissionsUtil.checkLocalMacAddressPermission(anyInt())).thenReturn(true); 2359 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 2360 2361 sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_DEVICE_INFO); 2362 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2363 assertEquals(WifiP2pManager.RESPONSE_DEVICE_INFO, mMessageCaptor.getValue().what); 2364 2365 WifiP2pDevice wifiP2pDevice = (WifiP2pDevice) mMessageCaptor.getValue().obj; 2366 if (isRandomPostfix) { 2367 assertEquals(expectedName, 2368 wifiP2pDevice.deviceName.substring(0, expectedName.length())); 2369 } else { 2370 assertEquals(expectedName, wifiP2pDevice.deviceName); 2371 } 2372 } 2373 setupDefaultDeviceNameCustomization( String prefix, int postfixDigit)2374 private void setupDefaultDeviceNameCustomization( 2375 String prefix, int postfixDigit) { 2376 when(mWifiSettingsConfigStore.get(eq(WIFI_P2P_DEVICE_NAME))).thenReturn(null); 2377 when(mFrameworkFacade.getSecureStringSetting(any(), eq(Settings.Secure.ANDROID_ID))) 2378 .thenReturn(TEST_ANDROID_ID); 2379 when(mWifiGlobals.getWifiP2pDeviceNamePrefix()).thenReturn(prefix); 2380 when(mWifiGlobals.getWifiP2pDeviceNamePostfixNumDigits()).thenReturn(postfixDigit); 2381 } 2382 2383 /** Verify that the default device name is customized by overlay. */ 2384 @Test testCustomizeDefaultDeviceName()2385 public void testCustomizeDefaultDeviceName() throws Exception { 2386 setupDefaultDeviceNameCustomization("Niceboat-", -1); 2387 verifyCustomizeDefaultDeviceName("Niceboat-" + TEST_ANDROID_ID.substring(0, 4), false); 2388 } 2389 2390 /** Verify that the prefix fallback to Android_ if the prefix is too long. */ 2391 @Test testCustomizeDefaultDeviceNameTooLongPrefix()2392 public void testCustomizeDefaultDeviceNameTooLongPrefix() throws Exception { 2393 setupDefaultDeviceNameCustomization( 2394 StringUtil.generateRandomNumberString( 2395 WifiP2pServiceImpl.DEVICE_NAME_PREFIX_LENGTH_MAX + 1), 4); 2396 verifyCustomizeDefaultDeviceName(WifiP2pServiceImpl.DEFAULT_DEVICE_NAME_PREFIX, true); 2397 } 2398 2399 /** Verify that the prefix fallback to Android_ if the prefix is empty. */ 2400 @Test testCustomizeDefaultDeviceNameEmptyPrefix()2401 public void testCustomizeDefaultDeviceNameEmptyPrefix() throws Exception { 2402 setupDefaultDeviceNameCustomization("", 6); 2403 verifyCustomizeDefaultDeviceName(WifiP2pServiceImpl.DEFAULT_DEVICE_NAME_PREFIX, true); 2404 } 2405 2406 /** Verify that the postfix fallbacks to 4-digit ANDROID_ID if the length is smaller than 4. */ 2407 @Test testCustomizeDefaultDeviceNamePostfixTooShort()2408 public void testCustomizeDefaultDeviceNamePostfixTooShort() throws Exception { 2409 setupDefaultDeviceNameCustomization("Prefix", 2410 WifiP2pServiceImpl.DEVICE_NAME_POSTFIX_LENGTH_MIN - 1); 2411 verifyCustomizeDefaultDeviceName("Prefix" + TEST_ANDROID_ID.substring(0, 4), true); 2412 } 2413 2414 /** Verify that the postfix fallbacks to 4-digit ANDROID_ID if the length is 0.*/ 2415 @Test testCustomizeDefaultDeviceNamePostfixIsZeroLength()2416 public void testCustomizeDefaultDeviceNamePostfixIsZeroLength() throws Exception { 2417 setupDefaultDeviceNameCustomization("Prefix", 0); 2418 verifyCustomizeDefaultDeviceName("Prefix" + TEST_ANDROID_ID.substring(0, 4), true); 2419 } 2420 2421 /** Verify that the digit length exceeds the remaining bytes. */ 2422 @Test testCustomizeDefaultDeviceNameWithFewerRemainingBytes()2423 public void testCustomizeDefaultDeviceNameWithFewerRemainingBytes() throws Exception { 2424 int postfixLength = 6; 2425 String prefix = StringUtil.generateRandomNumberString( 2426 WifiP2pServiceImpl.DEVICE_NAME_LENGTH_MAX - postfixLength + 1); 2427 setupDefaultDeviceNameCustomization(prefix, postfixLength); 2428 verifyCustomizeDefaultDeviceName(prefix, true); 2429 } 2430 2431 /** 2432 * Verify the caller sends WifiP2pManager.STOP_DISCOVERY. 2433 */ 2434 @Test testStopDiscoverySuccess()2435 public void testStopDiscoverySuccess() throws Exception { 2436 when(mWifiNative.p2pStopFind()).thenReturn(true); 2437 forceP2pEnabled(mClient1); 2438 sendSimpleMsg(mClientMessenger, WifiP2pManager.STOP_DISCOVERY); 2439 verify(mWifiNative).p2pStopFind(); 2440 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2441 Message message = mMessageCaptor.getValue(); 2442 assertEquals(WifiP2pManager.STOP_DISCOVERY_SUCCEEDED, message.what); 2443 } 2444 2445 /** 2446 * Verify WifiP2pManager.STOP_DISCOVERY_FAILED is returned when native call failure. 2447 */ 2448 @Test testStopDiscoveryFailureWhenNativeCallFailure()2449 public void testStopDiscoveryFailureWhenNativeCallFailure() throws Exception { 2450 when(mWifiNative.p2pStopFind()).thenReturn(false); 2451 forceP2pEnabled(mClient1); 2452 sendSimpleMsg(mClientMessenger, WifiP2pManager.STOP_DISCOVERY); 2453 verify(mWifiNative).p2pStopFind(); 2454 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2455 Message message = mMessageCaptor.getValue(); 2456 assertEquals(WifiP2pManager.STOP_DISCOVERY_FAILED, message.what); 2457 assertEquals(WifiP2pManager.ERROR, message.arg1); 2458 } 2459 2460 /** 2461 * Verify WifiP2pManager.STOP_DISCOVERY_FAILED is returned when p2p is disabled. 2462 */ 2463 @Test testStopDiscoveryFailureWhenP2pDisabled()2464 public void testStopDiscoveryFailureWhenP2pDisabled() throws Exception { 2465 sendSimpleMsg(mClientMessenger, WifiP2pManager.STOP_DISCOVERY); 2466 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2467 Message message = mMessageCaptor.getValue(); 2468 assertEquals(WifiP2pManager.STOP_DISCOVERY_FAILED, message.what); 2469 assertEquals(WifiP2pManager.BUSY, message.arg1); 2470 } 2471 2472 /** 2473 * Verify WifiP2pManager.STOP_DISCOVERY_FAILED is returned when p2p is unsupported. 2474 */ 2475 @Test testStopDiscoveryFailureWhenP2pUnsupported()2476 public void testStopDiscoveryFailureWhenP2pUnsupported() throws Exception { 2477 setUpWifiP2pServiceImpl(false); 2478 sendSimpleMsg(mClientMessenger, WifiP2pManager.STOP_DISCOVERY); 2479 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2480 Message message = mMessageCaptor.getValue(); 2481 assertEquals(WifiP2pManager.STOP_DISCOVERY_FAILED, message.what); 2482 assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1); 2483 } 2484 2485 /** 2486 * Verify the caller sends WifiP2pManager.CANCEL_CONNECT. 2487 */ 2488 @Test testCancelConnectSuccess()2489 public void testCancelConnectSuccess() throws Exception { 2490 // Move to group creating state 2491 testConnectWithConfigValidAsGroupSuccess(); 2492 2493 sendSimpleMsg(mClientMessenger, WifiP2pManager.CANCEL_CONNECT); 2494 verify(mClientHandler, atLeastOnce()).sendMessage(mMessageCaptor.capture()); 2495 Message message = mMessageCaptor.getValue(); 2496 assertEquals(WifiP2pManager.CANCEL_CONNECT_SUCCEEDED, message.what); 2497 } 2498 2499 /** 2500 * Verify WifiP2pManager.CANCEL_CONNECT_FAILED is returned when p2p is inactive. 2501 */ 2502 @Test testCancelConnectFailureWhenP2pInactive()2503 public void testCancelConnectFailureWhenP2pInactive() throws Exception { 2504 // Move to inactive state 2505 forceP2pEnabled(mClient1); 2506 2507 sendSimpleMsg(mClientMessenger, WifiP2pManager.CANCEL_CONNECT); 2508 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2509 Message message = mMessageCaptor.getValue(); 2510 assertEquals(WifiP2pManager.CANCEL_CONNECT_FAILED, message.what); 2511 assertEquals(WifiP2pManager.BUSY, message.arg1); 2512 } 2513 2514 /** 2515 * Verify WifiP2pManager.CANCEL_CONNECT_FAILED is returned when p2p is disabled. 2516 */ 2517 @Test testCancelConnectFailureWhenP2pDisabled()2518 public void testCancelConnectFailureWhenP2pDisabled() throws Exception { 2519 sendSimpleMsg(mClientMessenger, WifiP2pManager.CANCEL_CONNECT); 2520 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2521 Message message = mMessageCaptor.getValue(); 2522 assertEquals(WifiP2pManager.CANCEL_CONNECT_FAILED, message.what); 2523 assertEquals(WifiP2pManager.BUSY, message.arg1); 2524 } 2525 2526 /** 2527 * Verify WifiP2pManager.CANCEL_CONNECT_FAILED is returned when p2p is unsupported. 2528 */ 2529 @Test testCancelConnectFailureWhenP2pUnsupported()2530 public void testCancelConnectFailureWhenP2pUnsupported() throws Exception { 2531 setUpWifiP2pServiceImpl(false); 2532 sendSimpleMsg(mClientMessenger, WifiP2pManager.CANCEL_CONNECT); 2533 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2534 Message message = mMessageCaptor.getValue(); 2535 assertEquals(WifiP2pManager.CANCEL_CONNECT_FAILED, message.what); 2536 assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1); 2537 } 2538 2539 /** 2540 * Verify the caller sends WifiP2pManager.REMOVE_GROUP. 2541 */ 2542 @Test testRemoveGroupSuccess()2543 public void testRemoveGroupSuccess() throws Exception { 2544 // Move to group created state 2545 forceP2pEnabled(mClient1); 2546 WifiP2pGroup group = new WifiP2pGroup(); 2547 group.setNetworkId(WifiP2pGroup.NETWORK_ID_PERSISTENT); 2548 group.setNetworkName("DIRECT-xy-NEW"); 2549 group.setOwner(new WifiP2pDevice("thisDeviceMac")); 2550 group.setIsGroupOwner(true); 2551 group.setInterface(IFACE_NAME_P2P); 2552 sendGroupStartedMsg(group); 2553 simulateTetherReady(); 2554 2555 when(mWifiNative.p2pGroupRemove(eq(IFACE_NAME_P2P))).thenReturn(true); 2556 sendSimpleMsg(mClientMessenger, WifiP2pManager.REMOVE_GROUP); 2557 verify(mWifiNative).p2pGroupRemove(eq(IFACE_NAME_P2P)); 2558 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2559 Message message = mMessageCaptor.getValue(); 2560 assertEquals(WifiP2pManager.REMOVE_GROUP_SUCCEEDED, message.what); 2561 } 2562 2563 /** 2564 * Verify WifiP2pManager.REMOVE_GROUP_FAILED is returned when native call failure. 2565 */ 2566 @Test testRemoveGroupFailureWhenNativeCallFailure()2567 public void testRemoveGroupFailureWhenNativeCallFailure() throws Exception { 2568 // Move to group created state 2569 forceP2pEnabled(mClient1); 2570 WifiP2pGroup group = new WifiP2pGroup(); 2571 group.setNetworkId(WifiP2pGroup.NETWORK_ID_PERSISTENT); 2572 group.setNetworkName("DIRECT-xy-NEW"); 2573 group.setOwner(new WifiP2pDevice("thisDeviceMac")); 2574 group.setIsGroupOwner(true); 2575 group.setInterface(IFACE_NAME_P2P); 2576 sendGroupStartedMsg(group); 2577 simulateTetherReady(); 2578 2579 when(mWifiNative.p2pGroupRemove(eq(IFACE_NAME_P2P))).thenReturn(false); 2580 sendSimpleMsg(mClientMessenger, WifiP2pManager.REMOVE_GROUP); 2581 verify(mWifiNative).p2pGroupRemove(eq(IFACE_NAME_P2P)); 2582 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2583 Message message = mMessageCaptor.getValue(); 2584 assertEquals(WifiP2pManager.REMOVE_GROUP_FAILED, message.what); 2585 assertEquals(WifiP2pManager.ERROR, message.arg1); 2586 } 2587 2588 /** 2589 * Verify WifiP2pManager.REMOVE_GROUP_FAILED is returned when p2p is creating group. 2590 */ 2591 @Test testRemoveGroupFailureWhenP2pCreatingGroup()2592 public void testRemoveGroupFailureWhenP2pCreatingGroup() throws Exception { 2593 // Move to group creating state 2594 testConnectWithConfigValidAsGroupSuccess(); 2595 2596 sendSimpleMsg(mClientMessenger, WifiP2pManager.REMOVE_GROUP); 2597 verify(mClientHandler, atLeastOnce()).sendMessage(mMessageCaptor.capture()); 2598 Message message = mMessageCaptor.getValue(); 2599 assertEquals(WifiP2pManager.REMOVE_GROUP_FAILED, message.what); 2600 assertEquals(WifiP2pManager.BUSY, message.arg1); 2601 } 2602 2603 /** 2604 * Verify WifiP2pManager.REMOVE_GROUP_FAILED is returned when p2p is inactive. 2605 */ 2606 @Test testRemoveGroupFailureWhenP2pInactive()2607 public void testRemoveGroupFailureWhenP2pInactive() throws Exception { 2608 // Move to inactive state 2609 forceP2pEnabled(mClient1); 2610 2611 sendSimpleMsg(mClientMessenger, WifiP2pManager.REMOVE_GROUP); 2612 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2613 Message message = mMessageCaptor.getValue(); 2614 assertEquals(WifiP2pManager.REMOVE_GROUP_FAILED, message.what); 2615 assertEquals(WifiP2pManager.BUSY, message.arg1); 2616 } 2617 2618 /** 2619 * Verify WifiP2pManager.REMOVE_GROUP_FAILED is returned when p2p is disabled. 2620 */ 2621 @Test testRemoveGroupFailureWhenP2pDisabled()2622 public void testRemoveGroupFailureWhenP2pDisabled() throws Exception { 2623 sendSimpleMsg(mClientMessenger, WifiP2pManager.REMOVE_GROUP); 2624 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2625 Message message = mMessageCaptor.getValue(); 2626 assertEquals(WifiP2pManager.REMOVE_GROUP_FAILED, message.what); 2627 assertEquals(WifiP2pManager.BUSY, message.arg1); 2628 } 2629 2630 /** 2631 * Verify WifiP2pManager.REMOVE_GROUP_FAILED is returned when p2p is unsupported. 2632 */ 2633 @Test testRemoveGroupFailureWhenP2pUnsupported()2634 public void testRemoveGroupFailureWhenP2pUnsupported() throws Exception { 2635 setUpWifiP2pServiceImpl(false); 2636 sendSimpleMsg(mClientMessenger, WifiP2pManager.REMOVE_GROUP); 2637 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2638 Message message = mMessageCaptor.getValue(); 2639 assertEquals(WifiP2pManager.REMOVE_GROUP_FAILED, message.what); 2640 assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1); 2641 } 2642 2643 /** 2644 * Verify the caller sends WifiP2pManager.SET_CHANNEL. 2645 */ 2646 @Test testSetChannelSuccess()2647 public void testSetChannelSuccess() throws Exception { 2648 // Move to enabled state 2649 forceP2pEnabled(mClient1); 2650 2651 Bundle p2pChannels = new Bundle(); 2652 p2pChannels.putInt("lc", 1); 2653 p2pChannels.putInt("oc", 2); 2654 when(mWifiNative.p2pSetListenChannel(anyInt())).thenReturn(true); 2655 when(mWifiNative.p2pSetOperatingChannel(anyInt(), any())).thenReturn(true); 2656 sendSetChannelMsg(mClientMessenger, p2pChannels); 2657 verify(mWifiNative).p2pSetListenChannel(eq(1)); 2658 verify(mWifiNative).p2pSetOperatingChannel(eq(2), any()); 2659 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2660 Message message = mMessageCaptor.getValue(); 2661 assertEquals(WifiP2pManager.SET_CHANNEL_SUCCEEDED, message.what); 2662 } 2663 2664 /** 2665 * Verify WifiP2pManager.SET_CHANNEL_FAILED is returned when native call failure. 2666 */ 2667 @Test testSetChannelFailureWhenNativeCallSetListenChannelFailure()2668 public void testSetChannelFailureWhenNativeCallSetListenChannelFailure() throws Exception { 2669 // Move to enabled state 2670 forceP2pEnabled(mClient1); 2671 2672 Bundle p2pChannels = new Bundle(); 2673 p2pChannels.putInt("lc", 1); 2674 p2pChannels.putInt("oc", 2); 2675 when(mWifiNative.p2pSetListenChannel(anyInt())).thenReturn(false); 2676 when(mWifiNative.p2pSetOperatingChannel(anyInt(), any())).thenReturn(true); 2677 sendSetChannelMsg(mClientMessenger, p2pChannels); 2678 verify(mWifiNative).p2pSetListenChannel(eq(1)); 2679 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2680 Message message = mMessageCaptor.getValue(); 2681 assertEquals(WifiP2pManager.SET_CHANNEL_FAILED, message.what); 2682 } 2683 2684 /** 2685 * Verify WifiP2pManager.SET_CHANNEL_FAILED is returned when native call failure. 2686 */ 2687 @Test testSetChannelFailureWhenNativeCallSetOperatingChannelFailure()2688 public void testSetChannelFailureWhenNativeCallSetOperatingChannelFailure() throws Exception { 2689 // Move to enabled state 2690 forceP2pEnabled(mClient1); 2691 2692 Bundle p2pChannels = new Bundle(); 2693 p2pChannels.putInt("lc", 1); 2694 p2pChannels.putInt("oc", 2); 2695 when(mWifiNative.p2pSetListenChannel(anyInt())).thenReturn(true); 2696 when(mWifiNative.p2pSetOperatingChannel(anyInt(), any())).thenReturn(false); 2697 sendSetChannelMsg(mClientMessenger, p2pChannels); 2698 verify(mWifiNative).p2pSetListenChannel(eq(1)); 2699 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2700 Message message = mMessageCaptor.getValue(); 2701 assertEquals(WifiP2pManager.SET_CHANNEL_FAILED, message.what); 2702 } 2703 2704 /** 2705 * Verify WifiP2pManager.SET_CHANNEL_FAILED is returned when no permissions are held. 2706 */ 2707 @Test testSetChannelFailureWhenNoPermissions()2708 public void testSetChannelFailureWhenNoPermissions() throws Exception { 2709 // Move to enabled state 2710 forceP2pEnabled(mClient1); 2711 2712 // no permissions held 2713 when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(false); 2714 when(mWifiPermissionsUtil.checkNetworkStackPermission(anyInt())).thenReturn(false); 2715 when(mWifiPermissionsUtil.checkConfigOverridePermission(anyInt())).thenReturn(false); 2716 2717 Bundle p2pChannels = new Bundle(); 2718 p2pChannels.putInt("lc", 1); 2719 p2pChannels.putInt("oc", 2); 2720 sendSetChannelMsg(mClientMessenger, p2pChannels); 2721 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2722 Message message = mMessageCaptor.getValue(); 2723 assertEquals(WifiP2pManager.SET_CHANNEL_FAILED, message.what); 2724 assertEquals(WifiP2pManager.ERROR, message.arg1); 2725 } 2726 2727 /** 2728 * Verify p2pSetListenChannel doesn't been called when message contain null object. 2729 */ 2730 @Test testSetChannelFailureWhenObjectIsNull()2731 public void testSetChannelFailureWhenObjectIsNull() throws Exception { 2732 when(mWifiNative.p2pSetListenChannel(anyInt())).thenReturn(true); 2733 when(mWifiNative.p2pSetOperatingChannel(anyInt(), any())).thenReturn(true); 2734 2735 // Move to enabled state 2736 forceP2pEnabled(mClient1); 2737 2738 when(mWifiNative.p2pSetListenChannel(anyInt())).thenReturn(false); 2739 when(mWifiNative.p2pSetOperatingChannel(anyInt(), any())).thenReturn(true); 2740 sendSetChannelMsg(mClientMessenger, null); 2741 // Should be called only once on entering P2pEnabledState. 2742 verify(mWifiNative, times(1)).p2pSetListenChannel(anyInt()); 2743 verify(mWifiNative, times(1)).p2pSetOperatingChannel(anyInt(), any()); 2744 } 2745 2746 /** 2747 * Verify the caller sends WifiP2pManager.START_WPS with push button configuration. 2748 */ 2749 @Test testStartWpsWithPbcSuccess()2750 public void testStartWpsWithPbcSuccess() throws Exception { 2751 // Move to group created state 2752 forceP2pEnabled(mClient1); 2753 WifiP2pGroup group = new WifiP2pGroup(); 2754 group.setNetworkId(WifiP2pGroup.NETWORK_ID_PERSISTENT); 2755 group.setNetworkName("DIRECT-xy-NEW"); 2756 group.setOwner(new WifiP2pDevice("thisDeviceMac")); 2757 group.setIsGroupOwner(true); 2758 group.setInterface(IFACE_NAME_P2P); 2759 sendGroupStartedMsg(group); 2760 simulateTetherReady(); 2761 2762 when(mWifiNative.startWpsPbc(anyString(), any())).thenReturn(true); 2763 WpsInfo wps = new WpsInfo(); 2764 wps.setup = WpsInfo.PBC; 2765 sendStartWpsMsg(mClientMessenger, wps); 2766 verify(mWifiNative).startWpsPbc(eq(IFACE_NAME_P2P), isNull()); 2767 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2768 Message message = mMessageCaptor.getValue(); 2769 assertEquals(WifiP2pManager.START_WPS_SUCCEEDED, message.what); 2770 } 2771 2772 /** 2773 * Verify the caller sends WifiP2pManager.START_WPS with pin display. 2774 */ 2775 @Test testStartWpsWithPinDisplaySuccess()2776 public void testStartWpsWithPinDisplaySuccess() throws Exception { 2777 // TODO(hsiuchangchen): This test item is related to UI. 2778 } 2779 2780 /** 2781 * Verify the caller sends WifiP2pManager.START_WPS with pin keypad. 2782 */ 2783 @Test testStartWpsWithPinKeypadSuccess()2784 public void testStartWpsWithPinKeypadSuccess() throws Exception { 2785 // Move to group created state 2786 forceP2pEnabled(mClient1); 2787 WifiP2pGroup group = new WifiP2pGroup(); 2788 group.setNetworkId(WifiP2pGroup.NETWORK_ID_PERSISTENT); 2789 group.setNetworkName("DIRECT-xy-NEW"); 2790 group.setOwner(new WifiP2pDevice("thisDeviceMac")); 2791 group.setIsGroupOwner(true); 2792 group.setInterface(IFACE_NAME_P2P); 2793 sendGroupStartedMsg(group); 2794 simulateTetherReady(); 2795 2796 when(mWifiNative.startWpsPinKeypad(anyString(), anyString())).thenReturn(true); 2797 WpsInfo wps = new WpsInfo(); 2798 wps.setup = WpsInfo.KEYPAD; 2799 wps.pin = "1234"; 2800 sendStartWpsMsg(mClientMessenger, wps); 2801 verify(mWifiNative).startWpsPinKeypad(eq(IFACE_NAME_P2P), eq("1234")); 2802 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2803 Message message = mMessageCaptor.getValue(); 2804 assertEquals(WifiP2pManager.START_WPS_SUCCEEDED, message.what); 2805 } 2806 2807 /** 2808 * Verify WifiP2pManager.START_WPS_FAILED is returned when message contain null object. 2809 */ 2810 @Test testStartWpsFailureWhenObjectIsNull()2811 public void testStartWpsFailureWhenObjectIsNull() throws Exception { 2812 // Move to group created state 2813 forceP2pEnabled(mClient1); 2814 WifiP2pGroup group = new WifiP2pGroup(); 2815 group.setNetworkId(WifiP2pGroup.NETWORK_ID_PERSISTENT); 2816 group.setNetworkName("DIRECT-xy-NEW"); 2817 group.setOwner(new WifiP2pDevice("thisDeviceMac")); 2818 group.setIsGroupOwner(true); 2819 group.setInterface(IFACE_NAME_P2P); 2820 sendGroupStartedMsg(group); 2821 simulateTetherReady(); 2822 2823 WpsInfo wps = null; 2824 sendStartWpsMsg(mClientMessenger, wps); 2825 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2826 Message message = mMessageCaptor.getValue(); 2827 assertEquals(WifiP2pManager.START_WPS_FAILED, message.what); 2828 } 2829 2830 /** 2831 * Verify WifiP2pManager.START_WPS_FAILED is returned when native call failure with 2832 * push button configuration. 2833 */ 2834 @Test testStartWpsWithPbcFailureWhenNativeCallFailure()2835 public void testStartWpsWithPbcFailureWhenNativeCallFailure() throws Exception { 2836 // Move to group created state 2837 forceP2pEnabled(mClient1); 2838 WifiP2pGroup group = new WifiP2pGroup(); 2839 group.setNetworkId(WifiP2pGroup.NETWORK_ID_PERSISTENT); 2840 group.setNetworkName("DIRECT-xy-NEW"); 2841 group.setOwner(new WifiP2pDevice("thisDeviceMac")); 2842 group.setIsGroupOwner(true); 2843 group.setInterface(IFACE_NAME_P2P); 2844 sendGroupStartedMsg(group); 2845 simulateTetherReady(); 2846 when(mWifiNative.startWpsPbc(anyString(), any())).thenReturn(false); 2847 WpsInfo wps = new WpsInfo(); 2848 wps.setup = WpsInfo.PBC; 2849 sendStartWpsMsg(mClientMessenger, wps); 2850 verify(mWifiNative).startWpsPbc(eq(IFACE_NAME_P2P), isNull()); 2851 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2852 Message message = mMessageCaptor.getValue(); 2853 assertEquals(WifiP2pManager.START_WPS_FAILED, message.what); 2854 } 2855 2856 /** 2857 * Verify WifiP2pManager.START_WPS_FAILED is returned when native call failure with 2858 * pin display. 2859 */ 2860 @Test testStartWpsWithPinDisplayFailureWhenNativeCallFailure()2861 public void testStartWpsWithPinDisplayFailureWhenNativeCallFailure() throws Exception { 2862 // Move to group created state 2863 forceP2pEnabled(mClient1); 2864 WifiP2pGroup group = new WifiP2pGroup(); 2865 group.setNetworkId(WifiP2pGroup.NETWORK_ID_PERSISTENT); 2866 group.setNetworkName("DIRECT-xy-NEW"); 2867 group.setOwner(new WifiP2pDevice("thisDeviceMac")); 2868 group.setIsGroupOwner(true); 2869 group.setInterface(IFACE_NAME_P2P); 2870 sendGroupStartedMsg(group); 2871 simulateTetherReady(); 2872 2873 when(mWifiNative.startWpsPinDisplay(anyString(), any())).thenReturn("abcd"); 2874 WpsInfo wps = new WpsInfo(); 2875 wps.setup = WpsInfo.DISPLAY; 2876 sendStartWpsMsg(mClientMessenger, wps); 2877 verify(mWifiNative).startWpsPinDisplay(eq(IFACE_NAME_P2P), isNull()); 2878 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2879 Message message = mMessageCaptor.getValue(); 2880 assertEquals(WifiP2pManager.START_WPS_FAILED, message.what); 2881 } 2882 2883 /** 2884 * Verify WifiP2pManager.START_WPS_FAILED is returned when native call failure with 2885 * pin keypad. 2886 */ 2887 @Test testStartWpsWithPinKeypadFailureWhenNativeCallFailure()2888 public void testStartWpsWithPinKeypadFailureWhenNativeCallFailure() throws Exception { 2889 // Move to group created state 2890 forceP2pEnabled(mClient1); 2891 WifiP2pGroup group = new WifiP2pGroup(); 2892 group.setNetworkId(WifiP2pGroup.NETWORK_ID_PERSISTENT); 2893 group.setNetworkName("DIRECT-xy-NEW"); 2894 group.setOwner(new WifiP2pDevice("thisDeviceMac")); 2895 group.setIsGroupOwner(true); 2896 group.setInterface(IFACE_NAME_P2P); 2897 sendGroupStartedMsg(group); 2898 simulateTetherReady(); 2899 2900 when(mWifiNative.startWpsPinKeypad(anyString(), anyString())).thenReturn(false); 2901 WpsInfo wps = new WpsInfo(); 2902 wps.setup = WpsInfo.KEYPAD; 2903 wps.pin = "1234"; 2904 sendStartWpsMsg(mClientMessenger, wps); 2905 verify(mWifiNative).startWpsPinKeypad(eq(IFACE_NAME_P2P), eq("1234")); 2906 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2907 Message message = mMessageCaptor.getValue(); 2908 assertEquals(WifiP2pManager.START_WPS_FAILED, message.what); 2909 } 2910 2911 /** 2912 * Verify WifiP2pManager.START_WPS_FAILED is returned when p2p is inactive. 2913 */ 2914 @Test testStartWpsFailureWhenP2pInactive()2915 public void testStartWpsFailureWhenP2pInactive() throws Exception { 2916 // Move to inactive state 2917 forceP2pEnabled(mClient1); 2918 2919 WpsInfo wps = new WpsInfo(); 2920 sendStartWpsMsg(mClientMessenger, wps); 2921 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2922 Message message = mMessageCaptor.getValue(); 2923 assertEquals(WifiP2pManager.START_WPS_FAILED, message.what); 2924 assertEquals(WifiP2pManager.BUSY, message.arg1); 2925 } 2926 2927 /** 2928 * Verify WifiP2pManager.START_WPS_FAILED is returned when p2p is disabled. 2929 */ 2930 @Test testStartWpsFailureWhenP2pDisabled()2931 public void testStartWpsFailureWhenP2pDisabled() throws Exception { 2932 WpsInfo wps = new WpsInfo(); 2933 sendStartWpsMsg(mClientMessenger, wps); 2934 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2935 Message message = mMessageCaptor.getValue(); 2936 assertEquals(WifiP2pManager.START_WPS_FAILED, message.what); 2937 assertEquals(WifiP2pManager.BUSY, message.arg1); 2938 } 2939 2940 /** 2941 * Verify WifiP2pManager.START_WPS_FAILED is returned when p2p is unsupported. 2942 */ 2943 @Test testStartWpsFailureWhenP2pUnsupported()2944 public void testStartWpsFailureWhenP2pUnsupported() throws Exception { 2945 setUpWifiP2pServiceImpl(false); 2946 WpsInfo wps = new WpsInfo(); 2947 sendStartWpsMsg(mClientMessenger, wps); 2948 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2949 Message message = mMessageCaptor.getValue(); 2950 assertEquals(WifiP2pManager.START_WPS_FAILED, message.what); 2951 assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1); 2952 } 2953 2954 /** 2955 * Verify the caller sends WifiP2pManager.SET_DEVICE_NAME. 2956 */ 2957 @Test testSetDeviceNameSuccess()2958 public void testSetDeviceNameSuccess() throws Exception { 2959 // Move to enabled state 2960 forceP2pEnabled(mClient1); 2961 mTestThisDevice.status = mTestThisDevice.AVAILABLE; 2962 2963 mTestThisDevice.deviceName = "another-name"; 2964 when(mWifiNative.setDeviceName(anyString())).thenReturn(true); 2965 sendSetDeviceNameMsg(mClientMessenger, mTestThisDevice); 2966 verify(mWifiNative).setDeviceName(eq(mTestThisDevice.deviceName)); 2967 verify(mWifiNative).setP2pSsidPostfix(eq("-" + mTestThisDevice.deviceName)); 2968 verify(mWifiSettingsConfigStore).put( 2969 eq(WIFI_P2P_DEVICE_NAME), eq(mTestThisDevice.deviceName)); 2970 checkSendThisDeviceChangedBroadcast(); 2971 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2972 Message message = mMessageCaptor.getValue(); 2973 assertEquals(WifiP2pManager.SET_DEVICE_NAME_SUCCEEDED, message.what); 2974 } 2975 2976 /** 2977 * Verify WifiP2pManager.SET_DEVICE_NAME_FAILED is returned when native call failed. 2978 */ 2979 @Test testSetDeviceNameFailureWhenNativeCallFailure()2980 public void testSetDeviceNameFailureWhenNativeCallFailure() throws Exception { 2981 // Move to enabled state 2982 forceP2pEnabled(mClient1); 2983 2984 when(mWifiNative.setDeviceName(anyString())).thenReturn(false); 2985 sendSetDeviceNameMsg(mClientMessenger, mTestThisDevice); 2986 verify(mWifiNative).setDeviceName(eq(mTestThisDevice.deviceName)); 2987 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 2988 Message message = mMessageCaptor.getValue(); 2989 assertEquals(WifiP2pManager.SET_DEVICE_NAME_FAILED, message.what); 2990 assertEquals(WifiP2pManager.ERROR, message.arg1); 2991 } 2992 2993 /** 2994 * Verify WifiP2pManager.SET_DEVICE_NAME_FAILED is returned when p2p device is null. 2995 */ 2996 @Test testSetDeviceNameFailureWhenDeviceIsNull()2997 public void testSetDeviceNameFailureWhenDeviceIsNull() throws Exception { 2998 // Move to enabled state 2999 forceP2pEnabled(mClient1); 3000 3001 sendSetDeviceNameMsg(mClientMessenger, null); 3002 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3003 Message message = mMessageCaptor.getValue(); 3004 assertEquals(WifiP2pManager.SET_DEVICE_NAME_FAILED, message.what); 3005 assertEquals(WifiP2pManager.ERROR, message.arg1); 3006 } 3007 3008 /** 3009 * Verify WifiP2pManager.SET_DEVICE_NAME_FAILED is returned when p2p device's name is null. 3010 */ 3011 @Test testSetDeviceNameFailureWhenDeviceNameIsNull()3012 public void testSetDeviceNameFailureWhenDeviceNameIsNull() throws Exception { 3013 // Move to enabled state 3014 forceP2pEnabled(mClient1); 3015 3016 mTestThisDevice.deviceName = null; 3017 sendSetDeviceNameMsg(mClientMessenger, mTestThisDevice); 3018 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3019 Message message = mMessageCaptor.getValue(); 3020 assertEquals(WifiP2pManager.SET_DEVICE_NAME_FAILED, message.what); 3021 assertEquals(WifiP2pManager.ERROR, message.arg1); 3022 } 3023 3024 /** 3025 * Verify WifiP2pManager.SET_DEVICE_NAME_FAILED is returned when p2p is disabled. 3026 */ 3027 @Test testSetDeviceNameFailureWhenP2pDisabled()3028 public void testSetDeviceNameFailureWhenP2pDisabled() throws Exception { 3029 sendSetDeviceNameMsg(mClientMessenger, mTestThisDevice); 3030 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3031 Message message = mMessageCaptor.getValue(); 3032 assertEquals(WifiP2pManager.SET_DEVICE_NAME_FAILED, message.what); 3033 assertEquals(WifiP2pManager.BUSY, message.arg1); 3034 } 3035 3036 /** 3037 * Verify WifiP2pManager.SET_DEVICE_NAME_FAILED is returned when p2p is unsupported. 3038 */ 3039 @Test testSetDeviceNameFailureWhenP2pUnsupported()3040 public void testSetDeviceNameFailureWhenP2pUnsupported() throws Exception { 3041 setUpWifiP2pServiceImpl(false); 3042 sendSetDeviceNameMsg(mClientMessenger, mTestThisDevice); 3043 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3044 Message message = mMessageCaptor.getValue(); 3045 assertEquals(WifiP2pManager.SET_DEVICE_NAME_FAILED, message.what); 3046 assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1); 3047 } 3048 3049 /** 3050 * Verify WifiP2pManager.SET_DEVICE_NAME_FAILED is returned when no permissions are held. 3051 */ 3052 @Test testSetDeviceNameFailureWhenNoPermissions()3053 public void testSetDeviceNameFailureWhenNoPermissions() throws Exception { 3054 // Move to enabled state 3055 forceP2pEnabled(mClient1); 3056 3057 // no permissions held 3058 when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(false); 3059 when(mWifiPermissionsUtil.checkNetworkStackPermission(anyInt())).thenReturn(false); 3060 when(mWifiPermissionsUtil.checkConfigOverridePermission(anyInt())).thenReturn(false); 3061 3062 sendSetDeviceNameMsg(mClientMessenger, null); 3063 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3064 Message message = mMessageCaptor.getValue(); 3065 assertEquals(WifiP2pManager.SET_DEVICE_NAME_FAILED, message.what); 3066 assertEquals(WifiP2pManager.ERROR, message.arg1); 3067 } 3068 3069 /** 3070 * Verify the caller sends WifiP2pManager.SET_WFD_INFO with wfd enabled. 3071 */ 3072 @Test testSetWfdInfoSuccessWithWfdEnabled()3073 public void testSetWfdInfoSuccessWithWfdEnabled() throws Exception { 3074 // Move to enabled state 3075 forceP2pEnabled(mClient1); 3076 mTestThisDevice.status = mTestThisDevice.AVAILABLE; 3077 3078 mTestThisDevice.wfdInfo = new WifiP2pWfdInfo(); 3079 mTestThisDevice.wfdInfo.setEnabled(true); 3080 when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper); 3081 when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt())) 3082 .thenReturn(PackageManager.PERMISSION_GRANTED); 3083 when(mWifiNative.setWfdEnable(anyBoolean())).thenReturn(true); 3084 when(mWifiNative.setWfdDeviceInfo(anyString())).thenReturn(true); 3085 sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo); 3086 3087 verify(mWifiInjector).getWifiPermissionsWrapper(); 3088 verify(mWifiPermissionsWrapper).getUidPermission( 3089 eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt()); 3090 verify(mWifiNative).setWfdEnable(eq(true)); 3091 verify(mWifiNative).setWfdDeviceInfo(eq(mTestThisDevice.wfdInfo.getDeviceInfoHex())); 3092 checkSendThisDeviceChangedBroadcast(); 3093 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3094 Message message = mMessageCaptor.getValue(); 3095 assertEquals(WifiP2pManager.SET_WFD_INFO_SUCCEEDED, message.what); 3096 } 3097 3098 /** 3099 * Verify the caller sends WifiP2pManager.SET_WFD_INFO with wfd is disabled. 3100 */ 3101 @Test testSetWfdInfoSuccessWithWfdDisabled()3102 public void testSetWfdInfoSuccessWithWfdDisabled() throws Exception { 3103 // Move to enabled state 3104 forceP2pEnabled(mClient1); 3105 mTestThisDevice.status = mTestThisDevice.AVAILABLE; 3106 3107 mTestThisDevice.wfdInfo = new WifiP2pWfdInfo(); 3108 mTestThisDevice.wfdInfo.setEnabled(false); 3109 when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper); 3110 when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt())) 3111 .thenReturn(PackageManager.PERMISSION_GRANTED); 3112 when(mWifiNative.setWfdEnable(anyBoolean())).thenReturn(true); 3113 sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo); 3114 3115 verify(mWifiInjector).getWifiPermissionsWrapper(); 3116 verify(mWifiPermissionsWrapper).getUidPermission( 3117 eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt()); 3118 verify(mWifiNative).setWfdEnable(eq(false)); 3119 checkSendThisDeviceChangedBroadcast(); 3120 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3121 Message message = mMessageCaptor.getValue(); 3122 assertEquals(WifiP2pManager.SET_WFD_INFO_SUCCEEDED, message.what); 3123 } 3124 3125 /** 3126 * Verify WifiP2pManager.SET_WFD_INFO_FAILED is returned when wfd permission denied. 3127 */ 3128 @Test testSetWfdInfoFailureWhenWfdPermissionDenied()3129 public void testSetWfdInfoFailureWhenWfdPermissionDenied() throws Exception { 3130 // Move to enabled state 3131 forceP2pEnabled(mClient1); 3132 mTestThisDevice.status = mTestThisDevice.AVAILABLE; 3133 3134 mTestThisDevice.wfdInfo = new WifiP2pWfdInfo(); 3135 when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper); 3136 when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt())) 3137 .thenReturn(PackageManager.PERMISSION_DENIED); 3138 sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo); 3139 verify(mWifiInjector).getWifiPermissionsWrapper(); 3140 verify(mWifiPermissionsWrapper).getUidPermission( 3141 eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt()); 3142 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3143 Message message = mMessageCaptor.getValue(); 3144 assertEquals(WifiP2pManager.SET_WFD_INFO_FAILED, message.what); 3145 assertEquals(WifiP2pManager.ERROR, message.arg1); 3146 } 3147 3148 /** 3149 * Verify WifiP2pManager.SET_WFD_INFO_FAILED is returned when wfdInfo is null. 3150 */ 3151 @Test testSetWfdInfoFailureWhenWfdInfoIsNull()3152 public void testSetWfdInfoFailureWhenWfdInfoIsNull() throws Exception { 3153 // Move to enabled state 3154 forceP2pEnabled(mClient1); 3155 mTestThisDevice.status = mTestThisDevice.AVAILABLE; 3156 3157 mTestThisDevice.wfdInfo = null; 3158 when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper); 3159 when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt())) 3160 .thenReturn(PackageManager.PERMISSION_GRANTED); 3161 sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo); 3162 verify(mWifiInjector).getWifiPermissionsWrapper(); 3163 verify(mWifiPermissionsWrapper).getUidPermission( 3164 eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt()); 3165 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3166 Message message = mMessageCaptor.getValue(); 3167 assertEquals(WifiP2pManager.SET_WFD_INFO_FAILED, message.what); 3168 assertEquals(WifiP2pManager.ERROR, message.arg1); 3169 } 3170 3171 /** 3172 * Verify WifiP2pManager.SET_WFD_INFO_FAILED is returned when wfd is enabled 3173 * and native call "setWfdEnable" failure. 3174 */ 3175 @Test testSetWfdInfoFailureWithWfdEnabledWhenNativeCallFailure1()3176 public void testSetWfdInfoFailureWithWfdEnabledWhenNativeCallFailure1() throws Exception { 3177 // Move to enabled state 3178 forceP2pEnabled(mClient1); 3179 mTestThisDevice.status = mTestThisDevice.AVAILABLE; 3180 3181 mTestThisDevice.wfdInfo = new WifiP2pWfdInfo(); 3182 mTestThisDevice.wfdInfo.setEnabled(true); 3183 when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper); 3184 when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt())) 3185 .thenReturn(PackageManager.PERMISSION_GRANTED); 3186 when(mWifiNative.setWfdEnable(anyBoolean())).thenReturn(false); 3187 sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo); 3188 3189 verify(mWifiInjector).getWifiPermissionsWrapper(); 3190 verify(mWifiPermissionsWrapper).getUidPermission( 3191 eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt()); 3192 verify(mWifiNative).setWfdEnable(eq(true)); 3193 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3194 Message message = mMessageCaptor.getValue(); 3195 assertEquals(WifiP2pManager.SET_WFD_INFO_FAILED, message.what); 3196 assertEquals(WifiP2pManager.ERROR, message.arg1); 3197 } 3198 3199 /** 3200 * Verify WifiP2pManager.SET_WFD_INFO_FAILED is returned when wfd is enabled 3201 * and native call "setWfdDeviceInfo" failure. 3202 */ 3203 @Test testSetWfdInfoFailureWithWfdEnabledWhenNativeCallFailure2()3204 public void testSetWfdInfoFailureWithWfdEnabledWhenNativeCallFailure2() throws Exception { 3205 // Move to enabled state 3206 forceP2pEnabled(mClient1); 3207 mTestThisDevice.status = mTestThisDevice.AVAILABLE; 3208 3209 mTestThisDevice.wfdInfo = new WifiP2pWfdInfo(); 3210 mTestThisDevice.wfdInfo.setEnabled(true); 3211 when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper); 3212 when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt())) 3213 .thenReturn(PackageManager.PERMISSION_GRANTED); 3214 when(mWifiNative.setWfdEnable(anyBoolean())).thenReturn(true); 3215 when(mWifiNative.setWfdDeviceInfo(anyString())).thenReturn(false); 3216 sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo); 3217 3218 verify(mWifiInjector).getWifiPermissionsWrapper(); 3219 verify(mWifiPermissionsWrapper).getUidPermission( 3220 eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt()); 3221 verify(mWifiNative).setWfdEnable(eq(true)); 3222 verify(mWifiNative).setWfdDeviceInfo(eq(mTestThisDevice.wfdInfo.getDeviceInfoHex())); 3223 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3224 Message message = mMessageCaptor.getValue(); 3225 assertEquals(WifiP2pManager.SET_WFD_INFO_FAILED, message.what); 3226 assertEquals(WifiP2pManager.ERROR, message.arg1); 3227 } 3228 3229 /** 3230 * Verify WifiP2pManager.SET_WFD_INFO_FAILED is returned when wfd is disabled 3231 * and native call failure. 3232 */ 3233 @Test testSetWfdInfoFailureWithWfdDisabledWhenNativeCallFailure()3234 public void testSetWfdInfoFailureWithWfdDisabledWhenNativeCallFailure() throws Exception { 3235 // Move to enabled state 3236 forceP2pEnabled(mClient1); 3237 mTestThisDevice.status = mTestThisDevice.AVAILABLE; 3238 3239 mTestThisDevice.wfdInfo = new WifiP2pWfdInfo(); 3240 mTestThisDevice.wfdInfo.setEnabled(false); 3241 when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper); 3242 when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt())) 3243 .thenReturn(PackageManager.PERMISSION_GRANTED); 3244 when(mWifiNative.setWfdEnable(anyBoolean())).thenReturn(false); 3245 sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo); 3246 3247 verify(mWifiInjector).getWifiPermissionsWrapper(); 3248 verify(mWifiPermissionsWrapper).getUidPermission( 3249 eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt()); 3250 verify(mWifiNative).setWfdEnable(eq(false)); 3251 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3252 Message message = mMessageCaptor.getValue(); 3253 assertEquals(WifiP2pManager.SET_WFD_INFO_FAILED, message.what); 3254 assertEquals(WifiP2pManager.ERROR, message.arg1); 3255 } 3256 3257 /** 3258 * Verify WifiP2pManager.SET_WFD_INFO_FAILED is returned when p2p is disabled. 3259 */ 3260 @Test testSetWfdInfoFailureWhenP2pDisabled()3261 public void testSetWfdInfoFailureWhenP2pDisabled() throws Exception { 3262 mTestThisDevice.wfdInfo = new WifiP2pWfdInfo(); 3263 when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper); 3264 when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt())) 3265 .thenReturn(PackageManager.PERMISSION_GRANTED); 3266 sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo); 3267 verify(mWifiInjector).getWifiPermissionsWrapper(); 3268 verify(mWifiPermissionsWrapper).getUidPermission( 3269 eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt()); 3270 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3271 Message message = mMessageCaptor.getValue(); 3272 assertEquals(WifiP2pManager.SET_WFD_INFO_FAILED, message.what); 3273 assertEquals(WifiP2pManager.BUSY, message.arg1); 3274 } 3275 3276 /** 3277 * Verify WifiP2pManager.SET_WFD_INFO_FAILED is returned when wfd permission denied 3278 * and p2p is disabled. 3279 */ 3280 @Test testSetWfdInfoFailureWhenWfdPermissionDeniedAndP2pDisabled()3281 public void testSetWfdInfoFailureWhenWfdPermissionDeniedAndP2pDisabled() throws Exception { 3282 mTestThisDevice.wfdInfo = new WifiP2pWfdInfo(); 3283 when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper); 3284 when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt())) 3285 .thenReturn(PackageManager.PERMISSION_DENIED); 3286 sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo); 3287 verify(mWifiInjector).getWifiPermissionsWrapper(); 3288 verify(mWifiPermissionsWrapper).getUidPermission( 3289 eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt()); 3290 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3291 Message message = mMessageCaptor.getValue(); 3292 assertEquals(WifiP2pManager.SET_WFD_INFO_FAILED, message.what); 3293 assertEquals(WifiP2pManager.ERROR, message.arg1); 3294 } 3295 3296 /** 3297 * Verify WifiP2pManager.SET_WFD_INFO_FAILED is returned when p2p is unsupported. 3298 */ 3299 @Test testSetWfdInfoFailureWhenP2pUnsupported()3300 public void testSetWfdInfoFailureWhenP2pUnsupported() throws Exception { 3301 setUpWifiP2pServiceImpl(false); 3302 mTestThisDevice.wfdInfo = new WifiP2pWfdInfo(); 3303 when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper); 3304 when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt())) 3305 .thenReturn(PackageManager.PERMISSION_GRANTED); 3306 sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo); 3307 verify(mWifiInjector).getWifiPermissionsWrapper(); 3308 verify(mWifiPermissionsWrapper).getUidPermission( 3309 eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt()); 3310 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3311 Message message = mMessageCaptor.getValue(); 3312 assertEquals(WifiP2pManager.SET_WFD_INFO_FAILED, message.what); 3313 assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1); 3314 } 3315 3316 /** 3317 * Verify WifiP2pManager.SET_WFD_INFO_FAILED is returned when wfd permission denied 3318 * and p2p is unsupported. 3319 */ 3320 @Test testSetWfdInfoFailureWhenWfdPermissionDeniedAndP2pUnsupported()3321 public void testSetWfdInfoFailureWhenWfdPermissionDeniedAndP2pUnsupported() throws Exception { 3322 setUpWifiP2pServiceImpl(false); 3323 mTestThisDevice.wfdInfo = new WifiP2pWfdInfo(); 3324 when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper); 3325 when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt())) 3326 .thenReturn(PackageManager.PERMISSION_DENIED); 3327 sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo); 3328 verify(mWifiInjector).getWifiPermissionsWrapper(); 3329 verify(mWifiPermissionsWrapper).getUidPermission( 3330 eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt()); 3331 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3332 Message message = mMessageCaptor.getValue(); 3333 assertEquals(WifiP2pManager.SET_WFD_INFO_FAILED, message.what); 3334 assertEquals(WifiP2pManager.ERROR, message.arg1); 3335 } 3336 3337 /** 3338 * Verify the call setMiracastMode when p2p is enabled. 3339 */ 3340 @Test testSetMiracastModeWhenP2pEnabled()3341 public void testSetMiracastModeWhenP2pEnabled() throws Exception { 3342 // Move to enabled state 3343 forceP2pEnabled(mClient1); 3344 3345 when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper); 3346 when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt())) 3347 .thenReturn(PackageManager.PERMISSION_GRANTED); 3348 mWifiP2pServiceImpl.setMiracastMode(0); 3349 mLooper.dispatchAll(); 3350 verify(mWifiInjector).getWifiPermissionsWrapper(); 3351 verify(mWifiPermissionsWrapper).getUidPermission( 3352 eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt()); 3353 verify(mWifiNative).setMiracastMode(eq(0)); 3354 } 3355 3356 /** 3357 * Verify the call setMiracastMode when p2p is disable. 3358 */ 3359 @Test testSetMiracastModeWhenP2pDisabled()3360 public void testSetMiracastModeWhenP2pDisabled() throws Exception { 3361 when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper); 3362 when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt())) 3363 .thenReturn(PackageManager.PERMISSION_GRANTED); 3364 mWifiP2pServiceImpl.setMiracastMode(0); 3365 mLooper.dispatchAll(); 3366 verify(mWifiInjector).getWifiPermissionsWrapper(); 3367 verify(mWifiPermissionsWrapper).getUidPermission( 3368 eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt()); 3369 verify(mWifiNative, never()).setMiracastMode(anyInt()); 3370 } 3371 3372 /** 3373 * Verify the call setMiracastMode when CONFIGURE_WIFI_DISPLAY permission denied. 3374 */ 3375 @Test(expected = SecurityException.class) testSetMiracastModeWhenPermissionDeined()3376 public void testSetMiracastModeWhenPermissionDeined() throws Exception { 3377 when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper); 3378 when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt())) 3379 .thenReturn(PackageManager.PERMISSION_DENIED); 3380 mWifiP2pServiceImpl.setMiracastMode(0); 3381 verify(mWifiInjector).getWifiPermissionsWrapper(); 3382 verify(mWifiPermissionsWrapper).getUidPermission( 3383 eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt()); 3384 verify(mWifiNative, never()).setMiracastMode(anyInt()); 3385 } 3386 3387 /** 3388 * Verify the caller sends WifiP2pManager.FACTORY_RESET when p2p is enabled. 3389 */ 3390 @Test testFactoryResetSuccessWhenP2pEnabled()3391 public void testFactoryResetSuccessWhenP2pEnabled() throws Exception { 3392 // Move to enabled state 3393 forceP2pEnabled(mClient1); 3394 3395 when(mWifiInjector.getUserManager()).thenReturn(mUserManager); 3396 when(mPackageManager.getNameForUid(anyInt())).thenReturn("testPkg"); 3397 when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(true); 3398 when(mUserManager.hasUserRestrictionForUser(eq(UserManager.DISALLOW_NETWORK_RESET), any())) 3399 .thenReturn(false); 3400 when(mUserManager.hasUserRestrictionForUser(eq(UserManager.DISALLOW_CONFIG_WIFI), any())) 3401 .thenReturn(false); 3402 when(mWifiNative.p2pListNetworks(any())).thenReturn(true); 3403 sendSimpleMsg(mClientMessenger, WifiP2pManager.FACTORY_RESET); 3404 checkSendP2pPersistentGroupsChangedBroadcast(); 3405 verify(mWifiInjector).getUserManager(); 3406 verify(mPackageManager).getNameForUid(anyInt()); 3407 verify(mWifiPermissionsUtil).checkNetworkSettingsPermission(anyInt()); 3408 verify(mUserManager).hasUserRestrictionForUser( 3409 eq(UserManager.DISALLOW_NETWORK_RESET), any()); 3410 verify(mUserManager).hasUserRestrictionForUser(eq(UserManager.DISALLOW_CONFIG_WIFI), any()); 3411 verify(mWifiNative, atLeastOnce()).p2pListNetworks(any()); 3412 verify(mWifiSettingsConfigStore).put(eq(WIFI_P2P_PENDING_FACTORY_RESET), eq(false)); 3413 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3414 Message message = mMessageCaptor.getValue(); 3415 assertEquals(WifiP2pManager.FACTORY_RESET_SUCCEEDED, message.what); 3416 } 3417 3418 /** 3419 * Verify the caller sends WifiP2pManager.FACTORY_RESET when p2p is disabled at first 3420 * and changes to enabled. 3421 */ 3422 @Test testFactoryResetSuccessWhenP2pFromDisabledToEnabled()3423 public void testFactoryResetSuccessWhenP2pFromDisabledToEnabled() throws Exception { 3424 when(mWifiInjector.getUserManager()).thenReturn(mUserManager); 3425 when(mPackageManager.getNameForUid(anyInt())).thenReturn("testPkg"); 3426 when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(true); 3427 when(mUserManager.hasUserRestrictionForUser(eq(UserManager.DISALLOW_NETWORK_RESET), any())) 3428 .thenReturn(false); 3429 when(mUserManager.hasUserRestrictionForUser(eq(UserManager.DISALLOW_CONFIG_WIFI), any())) 3430 .thenReturn(false); 3431 when(mWifiNative.p2pListNetworks(any())).thenReturn(true); 3432 sendSimpleMsg(mClientMessenger, WifiP2pManager.FACTORY_RESET); 3433 verify(mWifiInjector).getUserManager(); 3434 verify(mPackageManager).getNameForUid(anyInt()); 3435 verify(mWifiPermissionsUtil).checkNetworkSettingsPermission(anyInt()); 3436 verify(mUserManager).hasUserRestrictionForUser( 3437 eq(UserManager.DISALLOW_NETWORK_RESET), any()); 3438 verify(mUserManager).hasUserRestrictionForUser(eq(UserManager.DISALLOW_CONFIG_WIFI), any()); 3439 verify(mWifiNative, never()).p2pListNetworks(any()); 3440 verify(mWifiSettingsConfigStore).put(eq(WIFI_P2P_PENDING_FACTORY_RESET), eq(true)); 3441 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3442 Message message = mMessageCaptor.getValue(); 3443 assertEquals(WifiP2pManager.FACTORY_RESET_SUCCEEDED, message.what); 3444 3445 // Move to enabled state 3446 when(mWifiSettingsConfigStore.get(eq(WIFI_P2P_PENDING_FACTORY_RESET))).thenReturn(true); 3447 forceP2pEnabled(mClient1); 3448 verify(mWifiInjector, times(2)).getUserManager(); 3449 verify(mPackageManager, times(2)).getNameForUid(anyInt()); 3450 verify(mWifiPermissionsUtil, times(2)).checkNetworkSettingsPermission(anyInt()); 3451 verify(mUserManager, times(2)).hasUserRestrictionForUser( 3452 eq(UserManager.DISALLOW_NETWORK_RESET), any()); 3453 verify(mUserManager, times(2)).hasUserRestrictionForUser( 3454 eq(UserManager.DISALLOW_CONFIG_WIFI), any()); 3455 verify(mWifiNative, atLeastOnce()).p2pListNetworks(any()); 3456 verify(mWifiSettingsConfigStore).get(eq(WIFI_P2P_PENDING_FACTORY_RESET)); 3457 verify(mWifiSettingsConfigStore).put(eq(WIFI_P2P_PENDING_FACTORY_RESET), eq(false)); 3458 checkSendP2pPersistentGroupsChangedBroadcast(); 3459 } 3460 3461 /** 3462 * Verify WifiP2pManager.FACTORY_RESET_FAILED is returned without network setting permission. 3463 */ 3464 @Test testFactoryResetFailureWithoutNetworkSettingPermission()3465 public void testFactoryResetFailureWithoutNetworkSettingPermission() throws Exception { 3466 // Move to enabled state 3467 forceP2pEnabled(mClient1); 3468 3469 when(mWifiInjector.getUserManager()).thenReturn(mUserManager); 3470 when(mPackageManager.getNameForUid(anyInt())).thenReturn("testPkg"); 3471 when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(false); 3472 sendSimpleMsg(mClientMessenger, WifiP2pManager.FACTORY_RESET); 3473 verify(mWifiInjector).getUserManager(); 3474 verify(mPackageManager).getNameForUid(anyInt()); 3475 verify(mWifiPermissionsUtil).checkNetworkSettingsPermission(anyInt()); 3476 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3477 Message message = mMessageCaptor.getValue(); 3478 assertEquals(WifiP2pManager.FACTORY_RESET_FAILED, message.what); 3479 assertEquals(WifiP2pManager.ERROR, message.arg1); 3480 } 3481 3482 /** 3483 * Verify WifiP2pManager.FACTORY_RESET_FAILED is returned when network reset disallow. 3484 */ 3485 @Test testFactoryResetFailureWhenNetworkResetDisallow()3486 public void testFactoryResetFailureWhenNetworkResetDisallow() throws Exception { 3487 // Move to enabled state 3488 forceP2pEnabled(mClient1); 3489 3490 when(mWifiInjector.getUserManager()).thenReturn(mUserManager); 3491 when(mPackageManager.getNameForUid(anyInt())).thenReturn("testPkg"); 3492 when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(true); 3493 when(mUserManager.hasUserRestrictionForUser(eq(UserManager.DISALLOW_NETWORK_RESET), any())) 3494 .thenReturn(true); 3495 sendSimpleMsg(mClientMessenger, WifiP2pManager.FACTORY_RESET); 3496 verify(mWifiInjector).getUserManager(); 3497 verify(mPackageManager).getNameForUid(anyInt()); 3498 verify(mWifiPermissionsUtil).checkNetworkSettingsPermission(anyInt()); 3499 verify(mUserManager).hasUserRestrictionForUser( 3500 eq(UserManager.DISALLOW_NETWORK_RESET), any()); 3501 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3502 Message message = mMessageCaptor.getValue(); 3503 assertEquals(WifiP2pManager.FACTORY_RESET_FAILED, message.what); 3504 assertEquals(WifiP2pManager.ERROR, message.arg1); 3505 } 3506 3507 /** 3508 * Verify WifiP2pManager.FACTORY_RESET_FAILED is returned when config wifi disallow. 3509 */ 3510 @Test testFactoryResetFailureWhenConfigWifiDisallow()3511 public void testFactoryResetFailureWhenConfigWifiDisallow() throws Exception { 3512 // Move to enabled state 3513 forceP2pEnabled(mClient1); 3514 3515 when(mWifiInjector.getUserManager()).thenReturn(mUserManager); 3516 when(mPackageManager.getNameForUid(anyInt())).thenReturn("testPkg"); 3517 when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(true); 3518 when(mUserManager.hasUserRestrictionForUser(eq(UserManager.DISALLOW_NETWORK_RESET), any())) 3519 .thenReturn(false); 3520 when(mUserManager.hasUserRestrictionForUser(eq(UserManager.DISALLOW_CONFIG_WIFI), any())) 3521 .thenReturn(true); 3522 sendSimpleMsg(mClientMessenger, WifiP2pManager.FACTORY_RESET); 3523 verify(mWifiInjector).getUserManager(); 3524 verify(mPackageManager).getNameForUid(anyInt()); 3525 verify(mWifiPermissionsUtil).checkNetworkSettingsPermission(anyInt()); 3526 verify(mUserManager).hasUserRestrictionForUser( 3527 eq(UserManager.DISALLOW_NETWORK_RESET), any()); 3528 verify(mUserManager).hasUserRestrictionForUser(eq(UserManager.DISALLOW_CONFIG_WIFI), any()); 3529 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3530 Message message = mMessageCaptor.getValue(); 3531 assertEquals(WifiP2pManager.FACTORY_RESET_FAILED, message.what); 3532 assertEquals(WifiP2pManager.ERROR, message.arg1); 3533 } 3534 3535 /** 3536 * Verify WifiP2pManager.FACTORY_RESET_FAILED is returned when p2p is unsupported. 3537 */ 3538 @Test testFactoryResetFailureWhenP2pUnsupported()3539 public void testFactoryResetFailureWhenP2pUnsupported() throws Exception { 3540 setUpWifiP2pServiceImpl(false); 3541 3542 sendSimpleMsg(mClientMessenger, WifiP2pManager.FACTORY_RESET); 3543 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3544 Message message = mMessageCaptor.getValue(); 3545 assertEquals(WifiP2pManager.FACTORY_RESET_FAILED, message.what); 3546 assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1); 3547 } 3548 3549 /** 3550 * Verify the caller sends WifiP2pManager.SET_ONGOING_PEER_CONFIG. 3551 */ 3552 @Test testSetOngingPeerConfigSuccess()3553 public void testSetOngingPeerConfigSuccess() throws Exception { 3554 forceP2pEnabled(mClient1); 3555 mockPeersList(); 3556 WifiP2pConfig config = new WifiP2pConfig(); 3557 config.deviceAddress = mTestWifiP2pDevice.deviceAddress; 3558 3559 when(mWifiPermissionsUtil.checkNetworkStackPermission(anyInt())).thenReturn(true); 3560 sendSetOngoingPeerConfigMsg(mClientMessenger, config); 3561 3562 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3563 Message message = mMessageCaptor.getValue(); 3564 assertEquals(WifiP2pManager.SET_ONGOING_PEER_CONFIG_SUCCEEDED, message.what); 3565 } 3566 3567 /** 3568 * Verify WifiP2pManager.SET_ONGOING_PEER_CONFIG_FAILED is returned without NETWORK_STACK 3569 * permission. 3570 */ 3571 @Test testSetOngingPeerConfigFailureWithoutPermission()3572 public void testSetOngingPeerConfigFailureWithoutPermission() throws Exception { 3573 forceP2pEnabled(mClient1); 3574 mockPeersList(); 3575 WifiP2pConfig config = new WifiP2pConfig(); 3576 3577 when(mWifiPermissionsUtil.checkNetworkStackPermission(anyInt())).thenReturn(false); 3578 sendSetOngoingPeerConfigMsg(mClientMessenger, config); 3579 3580 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3581 Message message = mMessageCaptor.getValue(); 3582 assertEquals(WifiP2pManager.SET_ONGOING_PEER_CONFIG_FAILED, message.what); 3583 } 3584 3585 /** 3586 * Verify WifiP2pManager.SET_ONGOING_PEER_CONFIG_FAILED is returned with invalid peer config. 3587 */ 3588 @Test testSetOngoingPeerConfigFailureWithInvalidPeerConfig()3589 public void testSetOngoingPeerConfigFailureWithInvalidPeerConfig() throws Exception { 3590 forceP2pEnabled(mClient1); 3591 mockPeersList(); 3592 WifiP2pConfig config = new WifiP2pConfig(); 3593 3594 when(mWifiPermissionsUtil.checkNetworkStackPermission(anyInt())).thenReturn(true); 3595 sendSetOngoingPeerConfigMsg(mClientMessenger, config); 3596 3597 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3598 Message message = mMessageCaptor.getValue(); 3599 assertEquals(WifiP2pManager.SET_ONGOING_PEER_CONFIG_FAILED, message.what); 3600 } 3601 3602 /** 3603 * Verify that respond with RESPONSE_ONGOING_PEER_CONFIG 3604 * when caller sends REQUEST_ONGOING_PEER_CONFIG and permission is granted. 3605 */ 3606 @Test testRequestOngoingPeerConfigSuccess()3607 public void testRequestOngoingPeerConfigSuccess() throws Exception { 3608 forceP2pEnabled(mClient1); 3609 3610 when(mWifiPermissionsUtil.checkNetworkStackPermission(anyInt())).thenReturn(true); 3611 sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_ONGOING_PEER_CONFIG); 3612 3613 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3614 Message message = mMessageCaptor.getValue(); 3615 WifiP2pConfig config = (WifiP2pConfig) message.obj; 3616 assertEquals(WifiP2pManager.RESPONSE_ONGOING_PEER_CONFIG, message.what); 3617 assertNotNull(config); 3618 } 3619 3620 /** 3621 * Verify that respond with RESPONSE_ONGOING_PEER_CONFIG 3622 * when caller sends REQUEST_ONGOING_PEER_CONFIG and has no NETWORK_STACK permission. 3623 */ 3624 @Test testRequestOngoingPeerConfigFailureWithoutPermission()3625 public void testRequestOngoingPeerConfigFailureWithoutPermission() throws Exception { 3626 forceP2pEnabled(mClient1); 3627 3628 when(mWifiPermissionsUtil.checkNetworkStackPermission(anyInt())).thenReturn(false); 3629 sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_ONGOING_PEER_CONFIG); 3630 3631 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3632 Message message = mMessageCaptor.getValue(); 3633 WifiP2pConfig config = (WifiP2pConfig) message.obj; 3634 assertEquals(WifiP2pManager.RESPONSE_ONGOING_PEER_CONFIG, message.what); 3635 assertNull(config); 3636 } 3637 3638 /** 3639 * Verify that respond with RESPONSE_PERSISTENT_GROUP_INFO 3640 * when caller sends REQUEST_PERSISTENT_GROUP_INFO. 3641 */ 3642 @Test testRequestPersistentGroupInfoSuccess()3643 public void testRequestPersistentGroupInfoSuccess() throws Exception { 3644 // Ensure our own MAC address is not anonymized in the result 3645 when(mWifiPermissionsUtil.checkLocalMacAddressPermission(anyInt())).thenReturn(true); 3646 forceP2pEnabled(mClient1); 3647 3648 sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_PERSISTENT_GROUP_INFO); 3649 3650 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3651 Message message = mMessageCaptor.getValue(); 3652 WifiP2pGroupList groups = (WifiP2pGroupList) message.obj; 3653 assertEquals(WifiP2pManager.RESPONSE_PERSISTENT_GROUP_INFO, message.what); 3654 // WifiP2pGroupList does not implement equals operator, 3655 // use toString to compare two lists. 3656 assertEquals(mGroups.toString(), groups.toString()); 3657 } 3658 3659 /** 3660 * Verify that when no permissions are held, an empty {@link WifiP2pGroupList} is returned. 3661 */ 3662 @Test testRequestPersistentGroupInfoNoPermissionFailure()3663 public void testRequestPersistentGroupInfoNoPermissionFailure() throws Exception { 3664 // Ensure our own MAC address is not anonymized in the result 3665 when(mWifiPermissionsUtil.checkLocalMacAddressPermission(anyInt())).thenReturn(true); 3666 forceP2pEnabled(mClient1); 3667 3668 // no permissions held 3669 when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(false); 3670 when(mWifiPermissionsUtil.checkNetworkStackPermission(anyInt())).thenReturn(false); 3671 when(mWifiPermissionsUtil.checkReadWifiCredentialPermission(anyInt())).thenReturn(false); 3672 3673 sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_PERSISTENT_GROUP_INFO); 3674 3675 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3676 Message message = mMessageCaptor.getValue(); 3677 WifiP2pGroupList groups = (WifiP2pGroupList) message.obj; 3678 assertEquals(WifiP2pManager.RESPONSE_PERSISTENT_GROUP_INFO, message.what); 3679 // WifiP2pGroupList does not implement equals operator, 3680 // use toString to compare two lists. 3681 // Expect empty WifiP2pGroupList() 3682 assertEquals(new WifiP2pGroupList().toString(), groups.toString()); 3683 } 3684 3685 /** 3686 * Verify that respond with RESPONSE_CONNECTION_INFO 3687 * when caller sends REQUEST_CONNECTION_INFO. 3688 */ 3689 @Test testRequestConnectionInfoSuccess()3690 public void testRequestConnectionInfoSuccess() throws Exception { 3691 forceP2pEnabled(mClient1); 3692 3693 sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_CONNECTION_INFO); 3694 3695 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3696 Message message = mMessageCaptor.getValue(); 3697 WifiP2pInfo info = (WifiP2pInfo) message.obj; 3698 assertEquals(WifiP2pManager.RESPONSE_CONNECTION_INFO, message.what); 3699 // WifiP2pInfo does not implement equals operator, 3700 // use toString to compare two objects. 3701 assertEquals((new WifiP2pInfo()).toString(), info.toString()); 3702 } 3703 3704 /** 3705 * Verify that respond with RESPONSE_P2P_STATE 3706 * when caller sends REQUEST_P2P_STATE and p2p is enabled. 3707 */ 3708 @Test testRequestP2pStateEnabled()3709 public void testRequestP2pStateEnabled() throws Exception { 3710 forceP2pEnabled(mClient1); 3711 3712 sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_P2P_STATE); 3713 3714 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3715 Message message = mMessageCaptor.getValue(); 3716 assertEquals(WifiP2pManager.RESPONSE_P2P_STATE, message.what); 3717 assertEquals(WifiP2pManager.WIFI_P2P_STATE_ENABLED, message.arg1); 3718 } 3719 3720 /** 3721 * Verify that respond with RESPONSE_P2P_STATE 3722 * when caller sends REQUEST_P2P_STATE and p2p is disabled. 3723 */ 3724 @Test testRequestP2pStateDisabled()3725 public void testRequestP2pStateDisabled() throws Exception { 3726 sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_P2P_STATE); 3727 3728 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3729 Message message = mMessageCaptor.getValue(); 3730 assertEquals(WifiP2pManager.RESPONSE_P2P_STATE, message.what); 3731 assertEquals(WifiP2pManager.WIFI_P2P_STATE_DISABLED, message.arg1); 3732 } 3733 3734 /** 3735 * Verify that respond with RESPONSE_DISCOVERY_STATE 3736 * when caller sends REQUEST_DISCOVERY_STATE and discovery is started. 3737 */ 3738 @Test testRequestDiscoveryStateWhenStarted()3739 public void testRequestDiscoveryStateWhenStarted() throws Exception { 3740 forceP2pEnabled(mClient1); 3741 when(mWifiPermissionsUtil.checkCanAccessWifiDirect(any(), any(), 3742 anyInt(), anyBoolean())).thenReturn(false); 3743 when(mWifiPermissionsUtil.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), 3744 anyInt(), anyBoolean())).thenReturn(true); 3745 when(mWifiNative.p2pFind(anyInt())).thenReturn(true); 3746 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 3747 sendDiscoverPeersMsg(mClientMessenger); 3748 verify(mWifiNative).p2pFind(anyInt()); 3749 verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), 3750 anyInt(), eq(true)); 3751 3752 sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_DISCOVERY_STATE); 3753 3754 // there are 2 responses: 3755 // * WifiP2pManager.DISCOVER_PEERS_SUCCEEDED 3756 // * WifiP2pManager.RESPONSE_DISCOVERY_STATE 3757 verify(mClientHandler, times(2)).sendMessage(mMessageCaptor.capture()); 3758 List<Message> messages = mMessageCaptor.getAllValues(); 3759 assertEquals(WifiP2pManager.DISCOVER_PEERS_SUCCEEDED, messages.get(0).what); 3760 assertEquals(WifiP2pManager.RESPONSE_DISCOVERY_STATE, messages.get(1).what); 3761 assertEquals(WifiP2pManager.WIFI_P2P_DISCOVERY_STARTED, messages.get(1).arg1); 3762 } 3763 3764 /** 3765 * Verify that respond with RESPONSE_DISCOVERY_STATE 3766 * when caller sends REQUEST_DISCOVERY_STATE and discovery is stopped. 3767 */ 3768 @Test testRequestDiscoveryStateWhenStopped()3769 public void testRequestDiscoveryStateWhenStopped() throws Exception { 3770 forceP2pEnabled(mClient1); 3771 3772 sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_DISCOVERY_STATE); 3773 3774 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3775 Message message = mMessageCaptor.getValue(); 3776 assertEquals(WifiP2pManager.RESPONSE_DISCOVERY_STATE, message.what); 3777 assertEquals(WifiP2pManager.WIFI_P2P_DISCOVERY_STOPPED, message.arg1); 3778 } 3779 3780 /** 3781 * Verify that respond with RESPONSE_NETWORK_INFO 3782 * when caller sends REQUEST_NETWORK_INFO. 3783 */ 3784 @Test testRequestNetworkInfoSuccess()3785 public void testRequestNetworkInfoSuccess() throws Exception { 3786 NetworkInfo info_gold = 3787 new NetworkInfo(ConnectivityManager.TYPE_WIFI_P2P, 0, "WIFI_P2P", ""); 3788 3789 sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_NETWORK_INFO); 3790 3791 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3792 Message message = mMessageCaptor.getValue(); 3793 NetworkInfo info = (NetworkInfo) message.obj; 3794 assertEquals(WifiP2pManager.RESPONSE_NETWORK_INFO, message.what); 3795 assertEquals(info_gold.toString(), info.toString()); 3796 } 3797 3798 /** 3799 * Verify the caller sends WifiP2pManager.REMOVE_LOCAL_SERVICE. 3800 */ 3801 @Test testRemoveLocalServiceSuccess()3802 public void testRemoveLocalServiceSuccess() throws Exception { 3803 forceP2pEnabled(mClient1); 3804 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 3805 verifyAddLocalService(); 3806 3807 sendRemoveLocalServiceMsg(mClientMessenger, mTestWifiP2pServiceInfo); 3808 verify(mWifiNative).p2pServiceDel(any(WifiP2pServiceInfo.class)); 3809 3810 assertTrue(mClientHandler.hasMessages(WifiP2pManager.REMOVE_LOCAL_SERVICE_SUCCEEDED)); 3811 } 3812 3813 /** 3814 * Verify the caller sends WifiP2pManager.REMOVE_LOCAL_SERVICE without client info. 3815 */ 3816 @Test testRemoveLocalServiceSuccessWithoutClientInfo()3817 public void testRemoveLocalServiceSuccessWithoutClientInfo() throws Exception { 3818 forceP2pEnabled(mClient1); 3819 3820 sendRemoveLocalServiceMsg(mClientMessenger, mTestWifiP2pServiceInfo); 3821 verify(mWifiNative, never()).p2pServiceDel(any(WifiP2pServiceInfo.class)); 3822 3823 assertTrue(mClientHandler.hasMessages(WifiP2pManager.REMOVE_LOCAL_SERVICE_SUCCEEDED)); 3824 } 3825 3826 /** 3827 * Verify the caller sends WifiP2pManager.REMOVE_LOCAL_SERVICE when service info is null. 3828 */ 3829 @Test testRemoveLocalServiceSuccessWithNullServiceInfo()3830 public void testRemoveLocalServiceSuccessWithNullServiceInfo() throws Exception { 3831 forceP2pEnabled(mClient1); 3832 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 3833 verifyAddLocalService(); 3834 3835 sendRemoveLocalServiceMsg(mClientMessenger, null); 3836 verify(mWifiNative, never()).p2pServiceDel(any(WifiP2pServiceInfo.class)); 3837 3838 assertTrue(mClientHandler.hasMessages(WifiP2pManager.REMOVE_LOCAL_SERVICE_SUCCEEDED)); 3839 } 3840 3841 /** 3842 * Verify that respond with REMOVE_LOCAL_SERVICE_FAILED 3843 * when caller sends REMOVE_LOCAL_SERVICE and p2p is disabled. 3844 */ 3845 @Test testRemoveLocalServiceFailureWhenP2pDisabled()3846 public void testRemoveLocalServiceFailureWhenP2pDisabled() throws Exception { 3847 sendRemoveLocalServiceMsg(mClientMessenger, null); 3848 verify(mWifiNative, never()).p2pServiceDel(any(WifiP2pServiceInfo.class)); 3849 3850 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3851 Message message = mMessageCaptor.getValue(); 3852 assertEquals(WifiP2pManager.REMOVE_LOCAL_SERVICE_FAILED, message.what); 3853 assertEquals(WifiP2pManager.BUSY, message.arg1); 3854 } 3855 3856 /** 3857 * Verify that respond with REMOVE_LOCAL_SERVICE_FAILED 3858 * when caller sends REMOVE_LOCAL_SERVICE and p2p is unsupported. 3859 */ 3860 @Test testRemoveLocalServiceFailureWhenP2pUnsupported()3861 public void testRemoveLocalServiceFailureWhenP2pUnsupported() throws Exception { 3862 setUpWifiP2pServiceImpl(false); 3863 3864 sendRemoveLocalServiceMsg(mClientMessenger, null); 3865 verify(mWifiNative, never()).p2pServiceDel(any(WifiP2pServiceInfo.class)); 3866 3867 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3868 Message message = mMessageCaptor.getValue(); 3869 assertEquals(WifiP2pManager.REMOVE_LOCAL_SERVICE_FAILED, message.what); 3870 assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1); 3871 } 3872 3873 /** 3874 * Verify the caller sends WifiP2pManager.CLEAR_LOCAL_SERVICES. 3875 */ 3876 @Test testClearLocalServiceSuccess()3877 public void testClearLocalServiceSuccess() throws Exception { 3878 forceP2pEnabled(mClient1); 3879 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 3880 verifyAddLocalService(); 3881 3882 sendSimpleMsg(mClientMessenger, WifiP2pManager.CLEAR_LOCAL_SERVICES); 3883 verify(mWifiNative, atLeastOnce()).p2pServiceDel(any(WifiP2pServiceInfo.class)); 3884 3885 assertTrue(mClientHandler.hasMessages(WifiP2pManager.CLEAR_LOCAL_SERVICES_SUCCEEDED)); 3886 } 3887 3888 /** 3889 * Verify the caller sends WifiP2pManager.CLEAR_LOCAL_SERVICES without client info. 3890 */ 3891 @Test testClearLocalServiceSuccessWithoutClientInfo()3892 public void testClearLocalServiceSuccessWithoutClientInfo() throws Exception { 3893 forceP2pEnabled(mClient1); 3894 3895 sendSimpleMsg(mClientMessenger, WifiP2pManager.CLEAR_LOCAL_SERVICES); 3896 verify(mWifiNative, never()).p2pServiceDel(any(WifiP2pServiceInfo.class)); 3897 3898 assertTrue(mClientHandler.hasMessages(WifiP2pManager.CLEAR_LOCAL_SERVICES_SUCCEEDED)); 3899 } 3900 3901 /** 3902 * Verify that respond with CLEAR_LOCAL_SERVICES_FAILED 3903 * when caller sends CLEAR_LOCAL_SERVICES and p2p is disabled. 3904 */ 3905 @Test testClearLocalServiceFailureWhenP2pDisabled()3906 public void testClearLocalServiceFailureWhenP2pDisabled() throws Exception { 3907 sendSimpleMsg(mClientMessenger, WifiP2pManager.CLEAR_LOCAL_SERVICES); 3908 verify(mWifiNative, never()).p2pServiceDel(any(WifiP2pServiceInfo.class)); 3909 3910 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3911 Message message = mMessageCaptor.getValue(); 3912 assertEquals(WifiP2pManager.CLEAR_LOCAL_SERVICES_FAILED, message.what); 3913 assertEquals(WifiP2pManager.BUSY, message.arg1); 3914 } 3915 3916 /** 3917 * Verify that respond with CLEAR_LOCAL_SERVICES_FAILED 3918 * when caller sends CLEAR_LOCAL_SERVICES and p2p is unsupported. 3919 */ 3920 @Test testClearLocalServiceFailureWhenP2pUnsupported()3921 public void testClearLocalServiceFailureWhenP2pUnsupported() throws Exception { 3922 setUpWifiP2pServiceImpl(false); 3923 3924 sendSimpleMsg(mClientMessenger, WifiP2pManager.CLEAR_LOCAL_SERVICES); 3925 verify(mWifiNative, never()).p2pServiceDel(any(WifiP2pServiceInfo.class)); 3926 3927 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 3928 Message message = mMessageCaptor.getValue(); 3929 assertEquals(WifiP2pManager.CLEAR_LOCAL_SERVICES_FAILED, message.what); 3930 assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1); 3931 } 3932 3933 /** 3934 * Verify the caller sends WifiP2pManager.ADD_SERVICE_REQUEST without services discover. 3935 */ 3936 @Test testAddServiceRequestNoOverflow()3937 public void testAddServiceRequestNoOverflow() throws Exception { 3938 forceP2pEnabled(mClient1); 3939 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 3940 3941 for (int i = 0; i < 256; i++) { 3942 reset(mTestWifiP2pServiceRequest); 3943 sendAddServiceRequestMsg(mClientMessenger); 3944 ArgumentCaptor<Integer> idCaptor = ArgumentCaptor.forClass(int.class); 3945 verify(mTestWifiP2pServiceRequest).setTransactionId(idCaptor.capture()); 3946 assertTrue(idCaptor.getValue().intValue() > 0); 3947 } 3948 } 3949 verifyAddServiceRequest()3950 private void verifyAddServiceRequest() throws Exception { 3951 sendAddServiceRequestMsg(mClientMessenger); 3952 assertTrue(mClientHandler.hasMessages(WifiP2pManager.ADD_SERVICE_REQUEST_SUCCEEDED)); 3953 } 3954 3955 /** 3956 * Verify the caller sends WifiP2pManager.ADD_SERVICE_REQUEST without services discover. 3957 */ 3958 @Test testAddServiceRequestSuccessWithoutServiceDiscover()3959 public void testAddServiceRequestSuccessWithoutServiceDiscover() throws Exception { 3960 forceP2pEnabled(mClient1); 3961 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 3962 verifyAddServiceRequest(); 3963 } 3964 3965 /** 3966 * Verify the caller sends WifiP2pManager.ADD_SERVICE_REQUEST with services discover. 3967 */ 3968 @Test testAddServiceRequestSuccessWithServiceDiscover()3969 public void testAddServiceRequestSuccessWithServiceDiscover() throws Exception { 3970 testDiscoverServicesSuccess(); 3971 3972 sendAddServiceRequestMsg(mClientMessenger); 3973 verify(mWifiNative, atLeastOnce()).p2pServDiscReq(eq("00:00:00:00:00:00"), anyString()); 3974 3975 assertTrue(mClientHandler.hasMessages(WifiP2pManager.ADD_SERVICE_REQUEST_SUCCEEDED)); 3976 } 3977 3978 /** 3979 * Verify WifiP2pManager.ADD_SERVICE_REQUEST_FAILED is returned with null request. 3980 */ 3981 @Test testAddServiceRequestFailureWithNullRequest()3982 public void testAddServiceRequestFailureWithNullRequest() throws Exception { 3983 forceP2pEnabled(mClient1); 3984 3985 sendSimpleMsg(mClientMessenger, WifiP2pManager.ADD_SERVICE_REQUEST); 3986 3987 assertTrue(mClientHandler.hasMessages(WifiP2pManager.ADD_SERVICE_REQUEST_FAILED)); 3988 } 3989 3990 /** 3991 * Verify WifiP2pManager.ADD_SERVICE_REQUEST_FAILED is returned without client info. 3992 */ 3993 @Test testAddServiceRequestFailureWithoutClientInfo()3994 public void testAddServiceRequestFailureWithoutClientInfo() throws Exception { 3995 forceP2pEnabled(mClient1); 3996 3997 sendAddServiceRequestMsg(mClientMessenger); 3998 3999 assertTrue(mClientHandler.hasMessages(WifiP2pManager.ADD_SERVICE_REQUEST_FAILED)); 4000 } 4001 4002 /** 4003 * Verify that respond with ADD_SERVICE_REQUEST_FAILED 4004 * when caller sends ADD_SERVICE_REQUEST and p2p is disabled. 4005 */ 4006 @Test testAddServiceRequestFailureWhenP2pDisabled()4007 public void testAddServiceRequestFailureWhenP2pDisabled() throws Exception { 4008 sendAddServiceRequestMsg(mClientMessenger); 4009 4010 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 4011 Message message = mMessageCaptor.getValue(); 4012 assertEquals(WifiP2pManager.ADD_SERVICE_REQUEST_FAILED, message.what); 4013 assertEquals(WifiP2pManager.BUSY, message.arg1); 4014 } 4015 4016 /** 4017 * Verify that respond with ADD_SERVICE_REQUEST_FAILED 4018 * when caller sends ADD_SERVICE_REQUEST and p2p is unsupported. 4019 */ 4020 @Test testAddServiceRequestFailureWhenP2pUnsupported()4021 public void testAddServiceRequestFailureWhenP2pUnsupported() throws Exception { 4022 setUpWifiP2pServiceImpl(false); 4023 4024 sendAddServiceRequestMsg(mClientMessenger); 4025 4026 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 4027 Message message = mMessageCaptor.getValue(); 4028 assertEquals(WifiP2pManager.ADD_SERVICE_REQUEST_FAILED, message.what); 4029 assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1); 4030 } 4031 4032 /** 4033 * Verify the caller sends WifiP2pManager.REMOVE_SERVICE_REQUEST. 4034 */ 4035 @Test testRemoveServiceRequestSuccess()4036 public void testRemoveServiceRequestSuccess() throws Exception { 4037 forceP2pEnabled(mClient1); 4038 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 4039 verifyAddServiceRequest(); 4040 4041 sendRemoveServiceRequestMsg(mClientMessenger, mTestWifiP2pServiceRequest); 4042 4043 assertTrue(mClientHandler.hasMessages(WifiP2pManager.REMOVE_SERVICE_REQUEST_SUCCEEDED)); 4044 } 4045 4046 /** 4047 * Verify the caller sends WifiP2pManager.REMOVE_SERVICE_REQUEST without client info. 4048 */ 4049 @Test testRemoveServiceRequestSuccessWithoutClientInfo()4050 public void testRemoveServiceRequestSuccessWithoutClientInfo() throws Exception { 4051 forceP2pEnabled(mClient1); 4052 4053 sendRemoveServiceRequestMsg(mClientMessenger, mTestWifiP2pServiceRequest); 4054 4055 assertTrue(mClientHandler.hasMessages(WifiP2pManager.REMOVE_SERVICE_REQUEST_SUCCEEDED)); 4056 } 4057 4058 /** 4059 * Verify the caller sends WifiP2pManager.REMOVE_SERVICE_REQUEST when service info is null. 4060 */ 4061 @Test testRemoveServiceRequestSuccessWithNullServiceInfo()4062 public void testRemoveServiceRequestSuccessWithNullServiceInfo() throws Exception { 4063 forceP2pEnabled(mClient1); 4064 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 4065 verifyAddLocalService(); 4066 4067 sendRemoveServiceRequestMsg(mClientMessenger, null); 4068 4069 assertTrue(mClientHandler.hasMessages(WifiP2pManager.REMOVE_SERVICE_REQUEST_SUCCEEDED)); 4070 } 4071 4072 /** 4073 * Verify that respond with REMOVE_SERVICE_REQUEST_FAILED 4074 * when caller sends REMOVE_SERVICE_REQUEST and p2p is disabled. 4075 */ 4076 @Test testRemoveServiceRequestFailureWhenP2pDisabled()4077 public void testRemoveServiceRequestFailureWhenP2pDisabled() throws Exception { 4078 sendRemoveServiceRequestMsg(mClientMessenger, null); 4079 4080 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 4081 Message message = mMessageCaptor.getValue(); 4082 assertEquals(WifiP2pManager.REMOVE_SERVICE_REQUEST_FAILED, message.what); 4083 assertEquals(WifiP2pManager.BUSY, message.arg1); 4084 } 4085 4086 /** 4087 * Verify that respond with REMOVE_SERVICE_REQUEST_FAILED 4088 * when caller sends REMOVE_SERVICE_REQUEST and p2p is unsupported. 4089 */ 4090 @Test testRemoveServiceRequestFailureWhenP2pUnsupported()4091 public void testRemoveServiceRequestFailureWhenP2pUnsupported() throws Exception { 4092 setUpWifiP2pServiceImpl(false); 4093 4094 sendRemoveServiceRequestMsg(mClientMessenger, null); 4095 4096 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 4097 Message message = mMessageCaptor.getValue(); 4098 assertEquals(WifiP2pManager.REMOVE_SERVICE_REQUEST_FAILED, message.what); 4099 assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1); 4100 } 4101 4102 /** 4103 * Verify the caller sends WifiP2pManager.CLEAR_SERVICE_REQUESTS. 4104 */ 4105 @Test testClearServiceRequestsSuccess()4106 public void testClearServiceRequestsSuccess() throws Exception { 4107 forceP2pEnabled(mClient1); 4108 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 4109 verifyAddServiceRequest(); 4110 4111 sendSimpleMsg(mClientMessenger, WifiP2pManager.CLEAR_SERVICE_REQUESTS); 4112 4113 assertTrue(mClientHandler.hasMessages(WifiP2pManager.CLEAR_SERVICE_REQUESTS_SUCCEEDED)); 4114 } 4115 4116 /** 4117 * Verify the caller sends WifiP2pManager.CLEAR_SERVICE_REQUESTS without client info. 4118 */ 4119 @Test testClearServiceRequestsSuccessWithoutClientInfo()4120 public void testClearServiceRequestsSuccessWithoutClientInfo() throws Exception { 4121 forceP2pEnabled(mClient1); 4122 4123 sendSimpleMsg(mClientMessenger, WifiP2pManager.CLEAR_SERVICE_REQUESTS); 4124 4125 assertTrue(mClientHandler.hasMessages(WifiP2pManager.CLEAR_SERVICE_REQUESTS_SUCCEEDED)); 4126 } 4127 4128 /** 4129 * Verify that respond with CLEAR_SERVICE_REQUESTS_FAILED 4130 * when caller sends CLEAR_SERVICE_REQUEST and p2p is disabled. 4131 */ 4132 @Test testClearServiceRequestsFailureWhenP2pDisabled()4133 public void testClearServiceRequestsFailureWhenP2pDisabled() throws Exception { 4134 sendSimpleMsg(mClientMessenger, WifiP2pManager.CLEAR_SERVICE_REQUESTS); 4135 4136 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 4137 Message message = mMessageCaptor.getValue(); 4138 assertEquals(WifiP2pManager.CLEAR_SERVICE_REQUESTS_FAILED, message.what); 4139 assertEquals(WifiP2pManager.BUSY, message.arg1); 4140 } 4141 4142 /** 4143 * Verify that respond with CLEAR_SERVICE_REQUESTS_FAILED 4144 * when caller sends CLEAR_SERVICE_REQUEST and p2p is unsupported. 4145 */ 4146 @Test testClearServiceRequestsFailureWhenP2pUnsupported()4147 public void testClearServiceRequestsFailureWhenP2pUnsupported() throws Exception { 4148 setUpWifiP2pServiceImpl(false); 4149 4150 sendSimpleMsg(mClientMessenger, WifiP2pManager.CLEAR_SERVICE_REQUESTS); 4151 4152 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 4153 Message message = mMessageCaptor.getValue(); 4154 assertEquals(WifiP2pManager.CLEAR_SERVICE_REQUESTS_FAILED, message.what); 4155 assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1); 4156 } 4157 4158 /** 4159 * Verify stopping discovery is executed when location mode is turned off. 4160 */ 4161 @Test testStopDiscoveryWhenLocationModeIsDisabled()4162 public void testStopDiscoveryWhenLocationModeIsDisabled() throws Exception { 4163 forceP2pEnabled(mClient1); 4164 simulateLocationModeChange(false); 4165 mLooper.dispatchAll(); 4166 verify(mWifiNative).p2pStopFind(); 4167 } 4168 4169 /** 4170 * Verify a network name which is too long is rejected. 4171 */ 4172 @Test testSendConnectMsgWithTooLongNetworkName()4173 public void testSendConnectMsgWithTooLongNetworkName() throws Exception { 4174 mTestWifiP2pFastConnectionConfig.networkName = "DIRECT-xy-abcdefghijklmnopqrstuvw"; 4175 sendConnectMsg(mClientMessenger, mTestWifiP2pFastConnectionConfig); 4176 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 4177 Message message = mMessageCaptor.getValue(); 4178 assertEquals(WifiP2pManager.CONNECT_FAILED, message.what); 4179 } 4180 4181 /** 4182 * Verify a network name which is too short is rejected. 4183 */ 4184 @Test testSendConnectMsgWithTooShortNetworkName()4185 public void testSendConnectMsgWithTooShortNetworkName() throws Exception { 4186 mTestWifiP2pFastConnectionConfig.networkName = "DIRECT-x"; 4187 sendConnectMsg(mClientMessenger, mTestWifiP2pFastConnectionConfig); 4188 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 4189 Message message = mMessageCaptor.getValue(); 4190 assertEquals(WifiP2pManager.CONNECT_FAILED, message.what); 4191 } 4192 4193 /** 4194 * Verify the group owner intent value is selected correctly when no STA connection. 4195 */ 4196 @Test testGroupOwnerIntentSelectionWithoutStaConnection()4197 public void testGroupOwnerIntentSelectionWithoutStaConnection() throws Exception { 4198 when(mWifiInfo.getNetworkId()).thenReturn(WifiConfiguration.INVALID_NETWORK_ID); 4199 when(mWifiInfo.getFrequency()).thenReturn(2412); 4200 forceP2pEnabled(mClient1); 4201 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 4202 4203 mockEnterProvisionDiscoveryState(); 4204 4205 WifiP2pProvDiscEvent pdEvent = new WifiP2pProvDiscEvent(); 4206 pdEvent.device = mTestWifiP2pDevice; 4207 sendSimpleMsg(null, 4208 WifiP2pMonitor.P2P_PROV_DISC_PBC_RSP_EVENT, 4209 pdEvent); 4210 4211 ArgumentCaptor<WifiP2pConfig> configCaptor = 4212 ArgumentCaptor.forClass(WifiP2pConfig.class); 4213 verify(mWifiNative).p2pConnect(configCaptor.capture(), anyBoolean()); 4214 WifiP2pConfig config = configCaptor.getValue(); 4215 assertEquals(WifiP2pServiceImpl.DEFAULT_GROUP_OWNER_INTENT + 1, 4216 config.groupOwnerIntent); 4217 } 4218 4219 /** 4220 * Verify the group owner intent value is selected correctly when 2.4GHz STA connection. 4221 */ 4222 @Test testGroupOwnerIntentSelectionWith24GStaConnection()4223 public void testGroupOwnerIntentSelectionWith24GStaConnection() throws Exception { 4224 when(mWifiInfo.getNetworkId()).thenReturn(1); 4225 when(mWifiInfo.getFrequency()).thenReturn(2412); 4226 forceP2pEnabled(mClient1); 4227 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 4228 4229 mockEnterProvisionDiscoveryState(); 4230 4231 WifiP2pProvDiscEvent pdEvent = new WifiP2pProvDiscEvent(); 4232 pdEvent.device = mTestWifiP2pDevice; 4233 sendSimpleMsg(null, 4234 WifiP2pMonitor.P2P_PROV_DISC_PBC_RSP_EVENT, 4235 pdEvent); 4236 4237 ArgumentCaptor<WifiP2pConfig> configCaptor = 4238 ArgumentCaptor.forClass(WifiP2pConfig.class); 4239 verify(mWifiNative).p2pConnect(configCaptor.capture(), anyBoolean()); 4240 WifiP2pConfig config = configCaptor.getValue(); 4241 assertEquals(WifiP2pConfig.GROUP_OWNER_INTENT_MIN, 4242 config.groupOwnerIntent); 4243 } 4244 4245 /** 4246 * Verify the group owner intent value is selected correctly when 5GHz STA connection. 4247 */ 4248 @Test testGroupOwnerIntentSelectionWith5GHzStaConnection()4249 public void testGroupOwnerIntentSelectionWith5GHzStaConnection() throws Exception { 4250 when(mWifiInfo.getNetworkId()).thenReturn(1); 4251 when(mWifiInfo.getFrequency()).thenReturn(5200); 4252 forceP2pEnabled(mClient1); 4253 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 4254 4255 mockEnterProvisionDiscoveryState(); 4256 4257 WifiP2pProvDiscEvent pdEvent = new WifiP2pProvDiscEvent(); 4258 pdEvent.device = mTestWifiP2pDevice; 4259 sendSimpleMsg(null, 4260 WifiP2pMonitor.P2P_PROV_DISC_PBC_RSP_EVENT, 4261 pdEvent); 4262 4263 ArgumentCaptor<WifiP2pConfig> configCaptor = 4264 ArgumentCaptor.forClass(WifiP2pConfig.class); 4265 verify(mWifiNative).p2pConnect(configCaptor.capture(), anyBoolean()); 4266 WifiP2pConfig config = configCaptor.getValue(); 4267 assertEquals(WifiP2pConfig.GROUP_OWNER_INTENT_MAX - 1, 4268 config.groupOwnerIntent); 4269 } 4270 4271 /** 4272 * Verify the group owner intent value is selected correctly when 6GHz STA connection. 4273 */ 4274 @Test testGroupOwnerIntentSelectionWith6GHzStaConnection()4275 public void testGroupOwnerIntentSelectionWith6GHzStaConnection() throws Exception { 4276 when(mWifiInfo.getNetworkId()).thenReturn(1); 4277 when(mWifiInfo.getFrequency()).thenReturn(6000); 4278 forceP2pEnabled(mClient1); 4279 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 4280 4281 mockEnterProvisionDiscoveryState(); 4282 4283 WifiP2pProvDiscEvent pdEvent = new WifiP2pProvDiscEvent(); 4284 pdEvent.device = mTestWifiP2pDevice; 4285 sendSimpleMsg(null, 4286 WifiP2pMonitor.P2P_PROV_DISC_PBC_RSP_EVENT, 4287 pdEvent); 4288 4289 ArgumentCaptor<WifiP2pConfig> configCaptor = 4290 ArgumentCaptor.forClass(WifiP2pConfig.class); 4291 verify(mWifiNative).p2pConnect(configCaptor.capture(), anyBoolean()); 4292 WifiP2pConfig config = configCaptor.getValue(); 4293 assertEquals(WifiP2pServiceImpl.DEFAULT_GROUP_OWNER_INTENT, 4294 config.groupOwnerIntent); 4295 } 4296 setupCoexMock(int restrictionBits)4297 private List<CoexUnsafeChannel> setupCoexMock(int restrictionBits) { 4298 assumeTrue(SdkLevel.isAtLeastS()); 4299 List<CoexUnsafeChannel> unsafeChannels = new ArrayList<>(); 4300 unsafeChannels.add(new CoexUnsafeChannel(WifiScanner.WIFI_BAND_24_GHZ, 1)); 4301 unsafeChannels.add(new CoexUnsafeChannel(WifiScanner.WIFI_BAND_24_GHZ, 2)); 4302 unsafeChannels.add(new CoexUnsafeChannel(WifiScanner.WIFI_BAND_24_GHZ, 3)); 4303 unsafeChannels.add(new CoexUnsafeChannel(WifiScanner.WIFI_BAND_5_GHZ, 36)); 4304 unsafeChannels.add(new CoexUnsafeChannel(WifiScanner.WIFI_BAND_5_GHZ, 40)); 4305 unsafeChannels.add(new CoexUnsafeChannel(WifiScanner.WIFI_BAND_5_GHZ, 165)); 4306 when(mCoexManager.getCoexRestrictions()).thenReturn(restrictionBits); 4307 when(mCoexManager.getCoexUnsafeChannels()).thenReturn(unsafeChannels); 4308 when(mWifiNative.p2pSetListenChannel(anyInt())).thenReturn(true); 4309 when(mWifiNative.p2pSetOperatingChannel(anyInt(), any())).thenReturn(true); 4310 return unsafeChannels; 4311 } 4312 4313 /** Verify P2P unsafe channels are set if P2P bit presents in restriction bits. */ 4314 @Test testCoexCallbackWithWifiP2pUnsafeChannels()4315 public void testCoexCallbackWithWifiP2pUnsafeChannels() throws Exception { 4316 assumeTrue(SdkLevel.isAtLeastS()); 4317 setupCoexMock(0); 4318 assertNotNull(mCoexListener); 4319 forceP2pEnabled(mClient1); 4320 mLooper.dispatchAll(); 4321 4322 List<CoexUnsafeChannel> unsafeChannels = 4323 setupCoexMock(WifiManager.COEX_RESTRICTION_WIFI_DIRECT); 4324 mCoexListener.onCoexUnsafeChannelsChanged(); 4325 mLooper.dispatchAll(); 4326 4327 // On entering P2pEnabledState, these are called once first. 4328 verify(mWifiNative, times(2)).p2pSetListenChannel(eq(0)); 4329 ArgumentCaptor<List<CoexUnsafeChannel>> unsafeChannelsCaptor = 4330 ArgumentCaptor.forClass(List.class); 4331 verify(mWifiNative, times(2)).p2pSetOperatingChannel(eq(0), unsafeChannelsCaptor.capture()); 4332 List<List<CoexUnsafeChannel>> capturedUnsafeChannelsList = 4333 unsafeChannelsCaptor.getAllValues(); 4334 // The second one is what we sent. 4335 assertEquals(unsafeChannels, capturedUnsafeChannelsList.get(1)); 4336 } 4337 4338 /** Verify P2P unsafe channels are cleared if P2P bit does not present in restriction bits. */ 4339 @Test testCoexCallbackWithoutWifiP2pInRestrictionBits()4340 public void testCoexCallbackWithoutWifiP2pInRestrictionBits() throws Exception { 4341 assumeTrue(SdkLevel.isAtLeastS()); 4342 setupCoexMock(0); 4343 assertNotNull(mCoexListener); 4344 forceP2pEnabled(mClient1); 4345 mLooper.dispatchAll(); 4346 4347 mCoexListener.onCoexUnsafeChannelsChanged(); 4348 mLooper.dispatchAll(); 4349 4350 // On entering P2pEnabledState, these are called once first. 4351 verify(mWifiNative, times(2)).p2pSetListenChannel(eq(0)); 4352 ArgumentCaptor<List<CoexUnsafeChannel>> unsafeChannelsCaptor = 4353 ArgumentCaptor.forClass(List.class); 4354 verify(mWifiNative, times(2)).p2pSetOperatingChannel(eq(0), unsafeChannelsCaptor.capture()); 4355 List<List<CoexUnsafeChannel>> capturedUnsafeChannelsList = 4356 unsafeChannelsCaptor.getAllValues(); 4357 // The second one is what we sent. 4358 assertEquals(0, capturedUnsafeChannelsList.get(1).size()); 4359 } 4360 4361 /** 4362 * Verify the caller sends WifiP2pManager.SET_WFD_INFO with wfd enabled 4363 * and WFD R2 device info. 4364 */ 4365 @Test testSetWfdR2InfoSuccessWithWfdEnabled()4366 public void testSetWfdR2InfoSuccessWithWfdEnabled() throws Exception { 4367 assumeTrue(SdkLevel.isAtLeastS()); 4368 // Move to enabled state 4369 forceP2pEnabled(mClient1); 4370 mTestThisDevice.status = mTestThisDevice.AVAILABLE; 4371 4372 mTestThisDevice.wfdInfo = new WifiP2pWfdInfo(); 4373 mTestThisDevice.wfdInfo.setEnabled(true); 4374 mTestThisDevice.wfdInfo.setR2DeviceType(WifiP2pWfdInfo.DEVICE_TYPE_WFD_SOURCE); 4375 when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper); 4376 when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt())) 4377 .thenReturn(PackageManager.PERMISSION_GRANTED); 4378 when(mWifiNative.setWfdEnable(anyBoolean())).thenReturn(true); 4379 when(mWifiNative.setWfdDeviceInfo(anyString())).thenReturn(true); 4380 when(mWifiNative.setWfdR2DeviceInfo(anyString())).thenReturn(true); 4381 sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo); 4382 4383 verify(mWifiInjector).getWifiPermissionsWrapper(); 4384 verify(mWifiPermissionsWrapper).getUidPermission( 4385 eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt()); 4386 verify(mWifiNative).setWfdEnable(eq(true)); 4387 verify(mWifiNative).setWfdDeviceInfo(eq(mTestThisDevice.wfdInfo.getDeviceInfoHex())); 4388 verify(mWifiNative).setWfdR2DeviceInfo(eq(mTestThisDevice.wfdInfo.getR2DeviceInfoHex())); 4389 checkSendThisDeviceChangedBroadcast(); 4390 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 4391 Message message = mMessageCaptor.getValue(); 4392 assertEquals(WifiP2pManager.SET_WFD_INFO_SUCCEEDED, message.what); 4393 } 4394 4395 /** 4396 * Verify WifiP2pManager.SET_WFD_INFO_FAILED is returned when wfd is enabled, 4397 * WFD R2 device, and native call "setWfdR2DeviceInfo" failure. 4398 */ 4399 @Test testSetWfdR2InfoFailureWithWfdEnabledWhenNativeCallFailure2()4400 public void testSetWfdR2InfoFailureWithWfdEnabledWhenNativeCallFailure2() throws Exception { 4401 assumeTrue(SdkLevel.isAtLeastS()); 4402 // Move to enabled state 4403 forceP2pEnabled(mClient1); 4404 mTestThisDevice.status = mTestThisDevice.AVAILABLE; 4405 4406 mTestThisDevice.wfdInfo = new WifiP2pWfdInfo(); 4407 mTestThisDevice.wfdInfo.setEnabled(true); 4408 mTestThisDevice.wfdInfo.setR2DeviceType(WifiP2pWfdInfo.DEVICE_TYPE_WFD_SOURCE); 4409 when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper); 4410 when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt())) 4411 .thenReturn(PackageManager.PERMISSION_GRANTED); 4412 when(mWifiNative.setWfdEnable(anyBoolean())).thenReturn(true); 4413 when(mWifiNative.setWfdDeviceInfo(anyString())).thenReturn(true); 4414 when(mWifiNative.setWfdR2DeviceInfo(anyString())).thenReturn(false); 4415 sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo); 4416 4417 verify(mWifiInjector).getWifiPermissionsWrapper(); 4418 verify(mWifiPermissionsWrapper).getUidPermission( 4419 eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt()); 4420 verify(mWifiNative).setWfdEnable(eq(true)); 4421 verify(mWifiNative).setWfdDeviceInfo(eq(mTestThisDevice.wfdInfo.getDeviceInfoHex())); 4422 verify(mWifiNative).setWfdR2DeviceInfo(eq(mTestThisDevice.wfdInfo.getR2DeviceInfoHex())); 4423 verify(mClientHandler).sendMessage(mMessageCaptor.capture()); 4424 Message message = mMessageCaptor.getValue(); 4425 assertEquals(WifiP2pManager.SET_WFD_INFO_FAILED, message.what); 4426 assertEquals(WifiP2pManager.ERROR, message.arg1); 4427 } 4428 4429 /** 4430 * Verify that P2P group is removed during group creating failure. 4431 */ 4432 @Test testGroupCreatingFailureDueToTethering()4433 public void testGroupCreatingFailureDueToTethering() throws Exception { 4434 when(mWifiNative.p2pGroupAdd(anyBoolean())).thenReturn(true); 4435 when(mWifiNative.p2pGroupRemove(eq(IFACE_NAME_P2P))).thenReturn(true); 4436 when(mWifiPermissionsUtil.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), 4437 anyInt(), anyBoolean())).thenReturn(true); 4438 4439 WifiP2pGroup group = new WifiP2pGroup(); 4440 group.setNetworkId(WifiP2pGroup.NETWORK_ID_PERSISTENT); 4441 group.setNetworkName("DIRECT-xy-NEW"); 4442 group.setOwner(new WifiP2pDevice("thisDeviceMac")); 4443 group.setIsGroupOwner(true); 4444 group.setInterface(IFACE_NAME_P2P); 4445 4446 forceP2pEnabled(mClient1); 4447 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 4448 mLooper.dispatchAll(); 4449 sendCreateGroupMsg(mClientMessenger, WifiP2pGroup.NETWORK_ID_TEMPORARY, null); 4450 mLooper.dispatchAll(); 4451 4452 sendGroupStartedMsg(group); 4453 mLooper.dispatchAll(); 4454 4455 mLooper.moveTimeForward(120 * 1000 * 2); 4456 mLooper.dispatchAll(); 4457 4458 verify(mWifiNative).p2pGroupRemove(group.getInterface()); 4459 } 4460 4461 /** 4462 * Verify the idle timer is cancelled after leaving inactive state. 4463 */ 4464 @Test testIdleTimeoutCancelledAfterLeavingInactiveState()4465 public void testIdleTimeoutCancelledAfterLeavingInactiveState() throws Exception { 4466 forceP2pEnabled(mClient1); 4467 sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger); 4468 4469 mockPeersList(); 4470 sendConnectMsg(mClientMessenger, mTestWifiP2pPeerConfig); 4471 verify(mWifiPermissionsUtil) 4472 .checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(false)); 4473 4474 ArgumentCaptor<WifiP2pConfig> configCaptor = 4475 ArgumentCaptor.forClass(WifiP2pConfig.class); 4476 verify(mWifiP2pMetrics).startConnectionEvent( 4477 eq(P2pConnectionEvent.CONNECTION_FRESH), 4478 configCaptor.capture()); 4479 assertEquals(mTestWifiP2pPeerConfig.toString(), configCaptor.getValue().toString()); 4480 // Verify timer is cannelled 4481 // Includes re-schedule 4 times: 4482 // 1. forceP2pEnabled(): enter InactiveState 4483 // 2. forceP2pEnabled: REQUEST_P2P_STATE 4484 // 3. CONNECT 4485 // 4. leave InactiveState 4486 verify(mAlarmManager, times(4)).setExact(anyInt(), anyLong(), 4487 eq(mWifiP2pServiceImpl.P2P_IDLE_SHUTDOWN_MESSAGE_TIMEOUT_TAG), any(), any()); 4488 verify(mAlarmManager, times(4)).cancel(eq(mWifiP2pServiceImpl.mP2pIdleShutdownMessage)); 4489 } 4490 4491 /** 4492 * Verify the interface down after idle timer is triggered. 4493 */ 4494 @Test testIdleTimeoutTriggered()4495 public void testIdleTimeoutTriggered() throws Exception { 4496 forceP2pEnabled(mClient1); 4497 mWifiP2pServiceImpl.mP2pIdleShutdownMessage.onAlarm(); 4498 mLooper.dispatchAll(); 4499 verify(mWifiNative).teardownInterface(); 4500 verify(mWifiMonitor).stopMonitoring(anyString()); 4501 } 4502 } 4503