blob: fea45e72e8199143e1596685cfd3538951595009 [file] [log] [blame]
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.wifi.p2p;
import static android.net.NetworkInfo.DetailedState.FAILED;
import static android.net.NetworkInfo.DetailedState.IDLE;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession;
import static com.android.server.wifi.WifiSettingsConfigStore.WIFI_P2P_DEVICE_NAME;
import static com.android.server.wifi.WifiSettingsConfigStore.WIFI_P2P_PENDING_FACTORY_RESET;
import static com.android.server.wifi.WifiSettingsConfigStore.WIFI_VERBOSE_LOGGING_ENABLED;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isNull;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.annotation.Nullable;
import android.app.AlarmManager;
import android.app.test.MockAnswerUtil.AnswerWithArguments;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.InetAddresses;
import android.net.NetworkInfo;
import android.net.TetheringManager;
import android.net.wifi.CoexUnsafeChannel;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiScanner;
import android.net.wifi.WpsInfo;
import android.net.wifi.p2p.WifiP2pConfig;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pDeviceList;
import android.net.wifi.p2p.WifiP2pGroup;
import android.net.wifi.p2p.WifiP2pGroupList;
import android.net.wifi.p2p.WifiP2pInfo;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pProvDiscEvent;
import android.net.wifi.p2p.WifiP2pWfdInfo;
import android.net.wifi.p2p.nsd.WifiP2pServiceInfo;
import android.net.wifi.p2p.nsd.WifiP2pServiceRequest;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.os.Messenger;
import android.os.UserHandle;
import android.os.UserManager;
import android.os.WorkSource;
import android.os.test.TestLooper;
import android.provider.Settings;
import androidx.test.filters.SmallTest;
import com.android.modules.utils.build.SdkLevel;
import com.android.server.wifi.FakeWifiLog;
import com.android.server.wifi.FrameworkFacade;
import com.android.server.wifi.WifiBaseTest;
import com.android.server.wifi.WifiGlobals;
import com.android.server.wifi.WifiInjector;
import com.android.server.wifi.WifiSettingsConfigStore;
import com.android.server.wifi.coex.CoexManager;
import com.android.server.wifi.proto.nano.WifiMetricsProto.P2pConnectionEvent;
import com.android.server.wifi.util.NetdWrapper;
import com.android.server.wifi.util.StringUtil;
import com.android.server.wifi.util.WifiPermissionsUtil;
import com.android.server.wifi.util.WifiPermissionsWrapper;
import com.android.wifi.resources.R;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatcher;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.MockitoSession;
import org.mockito.Spy;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* Unit test harness for WifiP2pServiceImpl.
*/
@SmallTest
public class WifiP2pServiceImplTest extends WifiBaseTest {
private static final String TAG = "WifiP2pServiceImplTest";
private static final String IFACE_NAME_P2P = "mockP2p0";
private static final String P2P_GO_IP = "192.168.49.1";
private static final long STATE_CHANGE_WAITING_TIME = 1000;
private static final String thisDeviceMac = "11:22:33:44:55:66";
private static final String thisDeviceName = "thisDeviceName";
private static final String ANONYMIZED_DEVICE_ADDRESS = "02:00:00:00:00:00";
private static final String TEST_PACKAGE_NAME = "com.p2p.test";
private static final String TEST_ANDROID_ID = "314Deadbeef";
private ArgumentCaptor<BroadcastReceiver> mBcastRxCaptor = ArgumentCaptor.forClass(
BroadcastReceiver.class);
private Binder mClient1;
private Binder mClient2;
private BroadcastReceiver mLocationModeReceiver;
private BroadcastReceiver mWifiStateChangedReceiver;
private BroadcastReceiver mTetherStateReceiver;
private Handler mClientHandler;
private Messenger mP2pStateMachineMessenger;
private Messenger mClientMessenger;
private WifiP2pServiceImpl mWifiP2pServiceImpl;
private TestLooper mClientHanderLooper;
private TestLooper mLooper;
private WifiP2pConfig mTestWifiP2pPeerConfig;
private WifiP2pConfig mTestWifiP2pFastConnectionConfig;
private WifiP2pGroup mTestWifiP2pNewPersistentGoGroup;
private WifiP2pGroup mTestWifiP2pGroup;
private WifiP2pDevice mTestWifiP2pDevice;
private WifiP2pGroupList mGroups = new WifiP2pGroupList(null, null);
private WifiP2pDevice mTestThisDevice;
private ArgumentCaptor<Message> mMessageCaptor = ArgumentCaptor.forClass(Message.class);
private MockitoSession mStaticMockSession = null;
@Mock Context mContext;
@Mock FrameworkFacade mFrameworkFacade;
@Mock HandlerThread mHandlerThread;
@Mock NetdWrapper mNetdWrapper;
@Mock PackageManager mPackageManager;
@Mock Resources mResources;
@Mock NetworkInterface mP2pNetworkInterface;
@Mock WifiInjector mWifiInjector;
@Mock WifiManager mMockWifiManager;
@Mock WifiPermissionsUtil mWifiPermissionsUtil;
@Mock WifiSettingsConfigStore mWifiSettingsConfigStore;
@Mock WifiPermissionsWrapper mWifiPermissionsWrapper;
@Mock WifiP2pNative mWifiNative;
@Mock WifiP2pServiceInfo mTestWifiP2pServiceInfo;
@Mock WifiP2pServiceRequest mTestWifiP2pServiceRequest;
@Mock UserManager mUserManager;
@Mock WifiP2pMetrics mWifiP2pMetrics;
@Mock WifiManager mWifiManager;
@Mock WifiInfo mWifiInfo;
@Mock CoexManager mCoexManager;
@Spy FakeWifiLog mLog;
@Spy MockWifiP2pMonitor mWifiMonitor;
@Mock WifiGlobals mWifiGlobals;
@Mock AlarmManager mAlarmManager;
CoexManager.CoexListener mCoexListener;
private void generatorTestData() {
mTestWifiP2pGroup = new WifiP2pGroup();
mTestWifiP2pGroup.setNetworkName("TestGroupName");
mTestWifiP2pDevice = spy(new WifiP2pDevice());
mTestWifiP2pDevice.deviceName = "TestDeviceName";
mTestWifiP2pDevice.deviceAddress = "aa:bb:cc:dd:ee:ff";
// for general connect command
mTestWifiP2pPeerConfig = new WifiP2pConfig();
mTestWifiP2pPeerConfig.deviceAddress = mTestWifiP2pDevice.deviceAddress;
// for fast-connection connect command
mTestWifiP2pFastConnectionConfig = new WifiP2pConfig.Builder()
.setNetworkName("DIRECT-XY-HELLO")
.setPassphrase("DEADBEEF")
.build();
// for general group started event
mTestWifiP2pNewPersistentGoGroup = new WifiP2pGroup();
mTestWifiP2pNewPersistentGoGroup.setNetworkId(WifiP2pGroup.NETWORK_ID_PERSISTENT);
mTestWifiP2pNewPersistentGoGroup.setNetworkName("DIRECT-xy-NEW");
mTestWifiP2pNewPersistentGoGroup.setOwner(new WifiP2pDevice(thisDeviceMac));
mTestWifiP2pNewPersistentGoGroup.setIsGroupOwner(true);
mTestWifiP2pNewPersistentGoGroup.setInterface(IFACE_NAME_P2P);
mGroups.clear();
WifiP2pGroup group1 = new WifiP2pGroup();
group1.setNetworkId(0);
group1.setNetworkName(mTestWifiP2pGroup.getNetworkName());
group1.setOwner(mTestWifiP2pDevice);
group1.setIsGroupOwner(false);
mGroups.add(group1);
WifiP2pGroup group2 = new WifiP2pGroup();
group2.setNetworkId(1);
group2.setNetworkName("DIRECT-ab-Hello");
group2.setOwner(new WifiP2pDevice("12:34:56:78:90:ab"));
group2.setIsGroupOwner(false);
mGroups.add(group2);
WifiP2pGroup group3 = new WifiP2pGroup();
group3.setNetworkId(2);
group3.setNetworkName("DIRECT-cd-OWNER");
group3.setOwner(new WifiP2pDevice(thisDeviceMac));
group3.setIsGroupOwner(true);
mGroups.add(group3);
mTestThisDevice = new WifiP2pDevice();
mTestThisDevice.deviceName = thisDeviceName;
mTestThisDevice.deviceAddress = thisDeviceMac;
mTestThisDevice.primaryDeviceType = "10-0050F204-5";
}
/**
* Simulate Location Mode change: Changes the location manager return values and dispatches a
* broadcast.
*
* @param isLocationModeEnabled whether the location mode is enabled.,
*/
private void simulateLocationModeChange(boolean isLocationModeEnabled) {
when(mWifiPermissionsUtil.isLocationModeEnabled()).thenReturn(isLocationModeEnabled);
Intent intent = new Intent(LocationManager.MODE_CHANGED_ACTION);
mLocationModeReceiver.onReceive(mContext, intent);
}
/**
* Simulate Wi-Fi state change: broadcast state change and modify the API return value.
*
* @param isWifiOn whether the wifi mode is enabled.
*/
private void simulateWifiStateChange(boolean isWifiOn) {
when(mMockWifiManager.getWifiState()).thenReturn(
isWifiOn ? WifiManager.WIFI_STATE_ENABLED : WifiManager.WIFI_STATE_DISABLED);
Intent intent = new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION);
intent.putExtra(WifiManager.EXTRA_WIFI_STATE,
isWifiOn ? WifiManager.WIFI_STATE_ENABLED : WifiManager.WIFI_STATE_DISABLED);
mWifiStateChangedReceiver.onReceive(mContext, intent);
}
/**
* Simulate tethering flow is completed
*/
private void simulateTetherReady() {
ArrayList<String> availableList = new ArrayList<>();
ArrayList<String> localOnlyList = new ArrayList<>();
localOnlyList.add(IFACE_NAME_P2P);
ArrayList<String> tetherList = new ArrayList<>();
ArrayList<String> erroredList = new ArrayList<>();
Intent intent = new Intent(TetheringManager.ACTION_TETHER_STATE_CHANGED);
intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
intent.putStringArrayListExtra(TetheringManager.EXTRA_AVAILABLE_TETHER, availableList);
intent.putStringArrayListExtra(TetheringManager.EXTRA_ACTIVE_LOCAL_ONLY, localOnlyList);
intent.putStringArrayListExtra(TetheringManager.EXTRA_ACTIVE_TETHER, tetherList);
intent.putStringArrayListExtra(TetheringManager.EXTRA_ERRORED_TETHER, erroredList);
mTetherStateReceiver.onReceive(mContext, intent);
mLooper.dispatchAll();
}
/**
* Mock send WifiP2pManager.UPDATE_CHANNEL_INFO
*
* @param pkgName package name used for p2p channel init
* @param featureId The feature in the package
* @param binder client binder used for p2p channel init
* @param replyMessenger for checking replied message.
*/
private void sendChannelInfoUpdateMsg(String pkgName, @Nullable String featureId,
Binder binder, Messenger replyMessenger) throws Exception {
Message msg = Message.obtain();
msg.what = WifiP2pManager.UPDATE_CHANNEL_INFO;
Bundle bundle = new Bundle();
bundle.putString(WifiP2pManager.CALLING_PACKAGE, pkgName);
bundle.putString(WifiP2pManager.CALLING_FEATURE_ID, featureId);
bundle.putBinder(WifiP2pManager.CALLING_BINDER, binder);
msg.obj = bundle;
msg.replyTo = replyMessenger;
mP2pStateMachineMessenger.send(Message.obtain(msg));
mLooper.dispatchAll();
}
/**
* Mock send WifiP2pManager.ADD_LOCAL_SERVICE with mTestWifiP2pServiceInfo
*
* @param replyMessenger for checking replied message.
*/
private void sendAddLocalServiceMsg(Messenger replyMessenger) throws Exception {
Message msg = Message.obtain();
msg.what = WifiP2pManager.ADD_LOCAL_SERVICE;
msg.obj = mTestWifiP2pServiceInfo;
msg.replyTo = replyMessenger;
mP2pStateMachineMessenger.send(Message.obtain(msg));
mLooper.dispatchAll();
}
/**
* Mock send WifiP2pManager.CONNECT with ConfigValidAsGroup
*
* @param replyMessenger for checking replied message.
*/
private void sendConnectMsgWithConfigValidAsGroup(Messenger replyMessenger) throws Exception {
sendConnectMsg(replyMessenger, mTestWifiP2pFastConnectionConfig);
}
/**
* Mock send WifiP2pManager.CREATE_GROUP with ConfigValidAsGroup
*
* @param replyMessenger for checking replied message.
*/
private void sendCreateGroupMsgWithConfigValidAsGroup(Messenger replyMessenger)
throws Exception {
sendCreateGroupMsg(replyMessenger, 0, mTestWifiP2pFastConnectionConfig);
}
/**
* Mock send WifiP2pManager.DISCOVER_PEERS
*
* @param replyMessenger for checking replied message.
*/
private void sendDiscoverPeersMsg(Messenger replyMessenger) throws Exception {
Message msg = Message.obtain();
msg.what = WifiP2pManager.DISCOVER_PEERS;
msg.replyTo = replyMessenger;
mP2pStateMachineMessenger.send(Message.obtain(msg));
mLooper.dispatchAll();
}
/**
* Mock send WifiP2pManager.ADD_SERVICE_REQUEST with mocked mTestWifiP2pServiceRequest
*
* @param replyMessenger for checking replied message.
*/
private void sendAddServiceRequestMsg(Messenger replyMessenger) throws Exception {
Message msg = Message.obtain();
msg.what = WifiP2pManager.ADD_SERVICE_REQUEST;
msg.replyTo = replyMessenger;
msg.obj = mTestWifiP2pServiceRequest;
mP2pStateMachineMessenger.send(Message.obtain(msg));
mLooper.dispatchAll();
}
/**
* Mock send WifiP2pManager.DISCOVER_SERVICES
*
* @param replyMessenger for checking replied message.
*/
private void sendDiscoverServiceMsg(Messenger replyMessenger) throws Exception {
Message msg = Message.obtain();
msg.what = WifiP2pManager.DISCOVER_SERVICES;
msg.replyTo = replyMessenger;
mP2pStateMachineMessenger.send(Message.obtain(msg));
mLooper.dispatchAll();
}
/**
* Mock send WifiP2pManager.REQUEST_PEERS
*
* @param replyMessenger for checking replied message.
*/
private void sendRequestPeersMsg(Messenger replyMessenger) throws Exception {
Message msg = Message.obtain();
msg.what = WifiP2pManager.REQUEST_PEERS;
msg.replyTo = replyMessenger;
mP2pStateMachineMessenger.send(Message.obtain(msg));
mLooper.dispatchAll();
}
/**
* Mock send WifiP2pManager.REQUEST_GROUP_INFO
*
* @param replyMessenger for checking replied message.
*/
private void sendRequestGroupInfoMsg(Messenger replyMessenger) throws Exception {
Message msg = Message.obtain();
msg.what = WifiP2pManager.REQUEST_GROUP_INFO;
msg.replyTo = replyMessenger;
mP2pStateMachineMessenger.send(Message.obtain(msg));
mLooper.dispatchAll();
}
/**
* Mock send WifiP2pManager.DELETE_PERSISTENT_GROUP.
*
* @param replyMessenger for checking replied message.
* @param netId is the network id of the p2p group.
*/
private void sendDeletePersistentGroupMsg(Messenger replyMessenger,
int netId) throws Exception {
Message msg = Message.obtain();
msg.what = WifiP2pManager.DELETE_PERSISTENT_GROUP;
msg.arg1 = netId;
msg.replyTo = replyMessenger;
mP2pStateMachineMessenger.send(Message.obtain(msg));
mLooper.dispatchAll();
}
/**
* Send WifiP2pMonitor.P2P_GROUP_STARTED_EVENT.
*
* @param group the started group.
*/
private void sendGroupStartedMsg(WifiP2pGroup group) throws Exception {
if (group.getNetworkId() == WifiP2pGroup.NETWORK_ID_PERSISTENT) {
mGroups.add(group);
}
Message msg = Message.obtain();
msg.what = WifiP2pMonitor.P2P_GROUP_STARTED_EVENT;
msg.obj = group;
mP2pStateMachineMessenger.send(Message.obtain(msg));
mLooper.dispatchAll();
}
/**
* Send WifiP2pMonitor.P2P_GROUP_REMOVED_EVENT.
*/
private void sendGroupRemovedMsg() throws Exception {
Message msg = Message.obtain();
msg.what = WifiP2pMonitor.P2P_GROUP_REMOVED_EVENT;
mP2pStateMachineMessenger.send(Message.obtain(msg));
mLooper.dispatchAll();
}
/**
* Send WifiP2pMonitor.P2P_DEVICE_FOUND_EVENT.
*
* @param device the found device.
*/
private void sendDeviceFoundEventMsg(WifiP2pDevice device) throws Exception {
Message msg = Message.obtain();
msg.what = WifiP2pMonitor.P2P_DEVICE_FOUND_EVENT;
msg.obj = device;
mP2pStateMachineMessenger.send(Message.obtain(msg));
mLooper.dispatchAll();
}
/**
* Send WifiP2pMonitor.P2P_INVITATION_RESULT_EVENT.
*
* @param status invitation result.
*/
private void sendInvitationResultMsg(
WifiP2pServiceImpl.P2pStatus status) throws Exception {
Message msg = Message.obtain();
msg.what = WifiP2pMonitor.P2P_INVITATION_RESULT_EVENT;
msg.obj = status;
mP2pStateMachineMessenger.send(Message.obtain(msg));
mLooper.dispatchAll();
}
/**
* Mock send WifiP2pManager.SET_CHANNEL
*
* @param replyMessenger for checking replied message.
* @param p2pChannels stores the listen and operating channels.
*/
private void sendSetChannelMsg(Messenger replyMessenger,
Bundle p2pChannels) throws Exception {
Message msg = Message.obtain();
msg.what = WifiP2pManager.SET_CHANNEL;
msg.replyTo = replyMessenger;
msg.obj = p2pChannels;
mP2pStateMachineMessenger.send(Message.obtain(msg));
mLooper.dispatchAll();
}
/**
* Mock send WifiP2pManager.START_WPS
*
* @param replyMessenger for checking replied message.
* @param wps is the WPS configuration.
*/
private void sendStartWpsMsg(Messenger replyMessenger, WpsInfo wps) throws Exception {
Message msg = Message.obtain();
msg.what = WifiP2pManager.START_WPS;
msg.replyTo = replyMessenger;
msg.obj = wps;
mP2pStateMachineMessenger.send(Message.obtain(msg));
mLooper.dispatchAll();
}
/**
* Mock send WifiP2pManager.SET_DEVICE_NAME
*
* @param replyMessenger for checking replied message.
* @param dev is the P2p device configuration.
*/
private void sendSetDeviceNameMsg(
Messenger replyMessenger, WifiP2pDevice dev) throws Exception {
Message msg = Message.obtain();
msg.what = WifiP2pManager.SET_DEVICE_NAME;
msg.replyTo = replyMessenger;
msg.obj = dev;
mP2pStateMachineMessenger.send(Message.obtain(msg));
mLooper.dispatchAll();
}
/**
* Mock send WifiP2pManager.SET_WFD_INFO
*
* @param replyMessenger for checking replied message.
* @param wfdInfo is the P2p device's wfd information.
*/
private void sendSetWfdInfoMsg(
Messenger replyMessenger, WifiP2pWfdInfo wfdInfo) throws Exception {
Message msg = Message.obtain();
msg.what = WifiP2pManager.SET_WFD_INFO;
msg.replyTo = replyMessenger;
msg.obj = wfdInfo;
mP2pStateMachineMessenger.send(Message.obtain(msg));
mLooper.dispatchAll();
}
/**
* Mock send WifiP2pManager.SET_ONGOING_PEER_CONFIG
*
* @param replyMessenger for checking replied message.
* @param config used for change an ongoing peer connection.
*/
private void sendSetOngoingPeerConfigMsg(
Messenger replyMessenger, WifiP2pConfig config) throws Exception {
Message msg = Message.obtain();
msg.what = WifiP2pManager.SET_ONGOING_PEER_CONFIG;
msg.replyTo = replyMessenger;
msg.obj = config;
mP2pStateMachineMessenger.send(Message.obtain(msg));
mLooper.dispatchAll();
}
/**
* Mock send WifiP2pManager.REMOVE_LOCAL_SERVICE.
*
* @param replyMessenger for checking replied message.
* @param servInfo is the local service information.
*/
private void sendRemoveLocalServiceMsg(Messenger replyMessenger,
WifiP2pServiceInfo servInfo) throws Exception {
Message msg = Message.obtain();
msg.what = WifiP2pManager.REMOVE_LOCAL_SERVICE;
msg.obj = servInfo;
msg.replyTo = replyMessenger;
mP2pStateMachineMessenger.send(Message.obtain(msg));
mLooper.dispatchAll();
}
/**
* Mock send WifiP2pManager.REMOVE_SERVICE_REQUEST.
*
* @param replyMessenger for checking replied message.
* @param req is the service discovery request.
*/
private void sendRemoveServiceRequestMsg(Messenger replyMessenger,
WifiP2pServiceRequest req) throws Exception {
Message msg = Message.obtain();
msg.what = WifiP2pManager.REMOVE_SERVICE_REQUEST;
msg.obj = req;
msg.replyTo = replyMessenger;
mP2pStateMachineMessenger.send(Message.obtain(msg));
mLooper.dispatchAll();
}
/**
* Send Connect API msg.
*
* @param replyMessenger for checking replied message.
* @param config options as described in {@link WifiP2pConfig} class.
*/
private void sendConnectMsg(Messenger replyMessenger,
WifiP2pConfig config) throws Exception {
Message msg = Message.obtain();
msg.what = WifiP2pManager.CONNECT;
msg.obj = config;
msg.replyTo = replyMessenger;
mP2pStateMachineMessenger.send(Message.obtain(msg));
mLooper.dispatchAll();
}
/**
* Send CreateGroup API msg.
*
* @param replyMessenger for checking replied message.
* @param config options as described in {@link WifiP2pConfig} class.
*/
private void sendCreateGroupMsg(Messenger replyMessenger,
int netId,
WifiP2pConfig config) throws Exception {
Message msg = Message.obtain();
msg.what = WifiP2pManager.CREATE_GROUP;
msg.arg1 = netId;
msg.obj = config;
msg.replyTo = replyMessenger;
mP2pStateMachineMessenger.send(Message.obtain(msg));
mLooper.dispatchAll();
}
/**
* Send simple API msg.
*
* Mock the API msg without arguments.
*
* @param replyMessenger for checking replied message.
*/
private void sendSimpleMsg(Messenger replyMessenger,
int what) throws Exception {
Message msg = Message.obtain();
msg.what = what;
if (replyMessenger != null) {
msg.replyTo = replyMessenger;
}
mP2pStateMachineMessenger.send(Message.obtain(msg));
mLooper.dispatchAll();
}
/**
* Send simple API msg.
*
* Mock the API msg with objects.
*
* @param replyMessenger for checking replied message.
*/
private void sendSimpleMsg(Messenger replyMessenger,
int what, Object obj) throws Exception {
Message msg = Message.obtain();
msg.what = what;
msg.obj = obj;
if (replyMessenger != null) {
msg.replyTo = replyMessenger;
}
mP2pStateMachineMessenger.send(Message.obtain(msg));
mLooper.dispatchAll();
}
/**
* force P2p State enter InactiveState to start others unit test
*
* @param clientBinder client binder to use for p2p channel init
*/
private void forceP2pEnabled(Binder clientBinder) throws Exception {
simulateWifiStateChange(true);
simulateLocationModeChange(true);
checkIsP2pInitWhenClientConnected(true, false, clientBinder,
new WorkSource(clientBinder.getCallingUid(), TEST_PACKAGE_NAME));
verify(mContext).sendBroadcastWithMultiplePermissions(
argThat(new WifiP2pServiceImplTest
.P2pConnectionChangedIntentMatcherForNetworkState(IDLE)), any());
verify(mContext, never()).sendBroadcastWithMultiplePermissions(
argThat(new WifiP2pServiceImplTest
.P2pConnectionChangedIntentMatcherForNetworkState(FAILED)), any());
}
/**
* Check is P2p init as expected when client connected
*
* @param expectInit set true if p2p init should succeed as expected, set false when
* expected init should not happen
* @param expectReplace set true if p2p worksource replace should succeed as expected, set false
* when replace should not happen
* @param clientBinder client binder to use for p2p channel init
* @param expectedRequestorWs Expected merged requestorWs
*/
private void checkIsP2pInitWhenClientConnected(boolean expectInit, boolean expectReplace,
Binder clientBinder, WorkSource expectedRequestorWs)
throws Exception {
mWifiP2pServiceImpl.getMessenger(clientBinder, TEST_PACKAGE_NAME);
if (expectInit) {
// send a command to force P2P enabled.
sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_P2P_STATE);
}
mLooper.dispatchAll();
reset(mClientHandler);
if (expectInit) {
verify(mWifiNative).setupInterface(any(), any(), eq(expectedRequestorWs));
verify(mNetdWrapper).setInterfaceUp(anyString());
verify(mWifiMonitor, atLeastOnce()).registerHandler(anyString(), anyInt(), any());
// Verify timer is scheduled
verify(mAlarmManager, times(2)).setExact(anyInt(), anyLong(),
eq(mWifiP2pServiceImpl.P2P_IDLE_SHUTDOWN_MESSAGE_TIMEOUT_TAG), any(), any());
} else if (expectReplace) {
verify(mWifiNative).replaceRequestorWs(expectedRequestorWs);
} else {
verify(mWifiNative, never()).setupInterface(any(), any(), any());
verify(mNetdWrapper, never()).setInterfaceUp(anyString());
verify(mWifiMonitor, never()).registerHandler(anyString(), anyInt(), any());
}
}
/**
* Check is P2p teardown as expected when client disconnected
*
* @param expectTearDown set true if p2p teardown should succeed as expected,
* set false when expected teardown should not happen
* @param expectReplace set true if p2p worksource replace should succeed as expected, set false
* when replace should not happen
* @param clientBinder client binder to use for p2p channel init
* @param expectedRequestorWs Expected merged requestorWs
*/
private void checkIsP2pTearDownWhenClientDisconnected(
boolean expectTearDown, boolean expectReplace,
Binder clientBinder, WorkSource expectedRequestorWs) throws Exception {
mWifiP2pServiceImpl.close(clientBinder);
mLooper.dispatchAll();
if (expectTearDown) {
verify(mWifiNative).teardownInterface();
verify(mWifiMonitor).stopMonitoring(anyString());
} else if (expectReplace) {
verify(mWifiNative).replaceRequestorWs(expectedRequestorWs);
} else {
verify(mWifiNative, never()).teardownInterface();
verify(mWifiMonitor, never()).stopMonitoring(anyString());
}
}
/**
* Check the broadcast of WIFI_P2P_THIS_DEVICE_CHANGED_ACTION is sent as expected.
*/
private void checkSendThisDeviceChangedBroadcast() {
ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
String[] permission_gold = new String[] {android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.ACCESS_WIFI_STATE};
ArgumentCaptor<String []> permissionCaptor = ArgumentCaptor.forClass(String[].class);
verify(mContext, atLeastOnce()).sendBroadcastWithMultiplePermissions(
intentCaptor.capture(), permissionCaptor.capture());
String [] permission = permissionCaptor.getValue();
Arrays.sort(permission);
Arrays.sort(permission_gold);
assertEquals(permission_gold, permission);
Intent intent = intentCaptor.getValue();
WifiP2pDevice device = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
assertEquals(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION, intent.getAction());
assertEquals(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT, intent.getFlags());
assertEquals(mTestThisDevice.deviceName, device.deviceName);
assertEquals(ANONYMIZED_DEVICE_ADDRESS, device.deviceAddress);
assertEquals(mTestThisDevice.primaryDeviceType, device.primaryDeviceType);
assertEquals(mTestThisDevice.secondaryDeviceType, device.secondaryDeviceType);
assertEquals(mTestThisDevice.wpsConfigMethodsSupported, device.wpsConfigMethodsSupported);
assertEquals(mTestThisDevice.deviceCapability, device.deviceCapability);
assertEquals(mTestThisDevice.groupCapability, device.groupCapability);
assertEquals(mTestThisDevice.status, device.status);
if (mTestThisDevice.wfdInfo != null) {
assertEquals(mTestThisDevice.wfdInfo.isEnabled(),
device.wfdInfo.isEnabled());
assertEquals(mTestThisDevice.wfdInfo.getDeviceInfoHex(),
device.wfdInfo.getDeviceInfoHex());
assertEquals(mTestThisDevice.wfdInfo.getControlPort(),
device.wfdInfo.getControlPort());
assertEquals(mTestThisDevice.wfdInfo.getMaxThroughput(),
device.wfdInfo.getMaxThroughput());
} else {
assertEquals(mTestThisDevice.wfdInfo, device.wfdInfo);
}
}
/**
* Check the broadcast of ACTION_WIFI_P2P_PERSISTENT_GROUPS_CHANGED is sent as expected.
*/
private void checkSendP2pPersistentGroupsChangedBroadcast() {
ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
verify(mContext, atLeastOnce()).sendStickyBroadcastAsUser(
intentCaptor.capture(), eq(UserHandle.ALL));
Intent intent = intentCaptor.getValue();
assertEquals(WifiP2pManager.ACTION_WIFI_P2P_PERSISTENT_GROUPS_CHANGED, intent.getAction());
assertEquals(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT, intent.getFlags());
}
private class P2pConnectionChangedIntentMatcherForNetworkState
implements ArgumentMatcher<Intent> {
private final NetworkInfo.DetailedState mState;
P2pConnectionChangedIntentMatcherForNetworkState(NetworkInfo.DetailedState state) {
mState = state;
}
@Override
public boolean matches(Intent intent) {
if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION != intent.getAction()) {
return false;
}
NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
return networkInfo.getDetailedState() == mState;
}
}
/**
* Set up the instance of WifiP2pServiceImpl for testing.
*
* @param supported defines the p2p is supported or not in this instance.
*/
private void setUpWifiP2pServiceImpl(boolean supported) throws Exception {
reset(mContext, mFrameworkFacade, mHandlerThread, mPackageManager, mResources,
mWifiInjector, mWifiNative);
generatorTestData();
mClientHanderLooper = new TestLooper();
mClientHandler = spy(new Handler(mClientHanderLooper.getLooper()));
mClientMessenger = new Messenger(mClientHandler);
mLooper = new TestLooper();
when(mContext.getSystemService(Context.ALARM_SERVICE))
.thenReturn(mAlarmManager);
when(mContext.getPackageManager()).thenReturn(mPackageManager);
when(mContext.getResources()).thenReturn(mResources);
when(mContext.getSystemService(WifiManager.class)).thenReturn(mWifiManager);
when(mWifiManager.getConnectionInfo()).thenReturn(mWifiInfo);
when(mWifiSettingsConfigStore.get(eq(WIFI_P2P_DEVICE_NAME))).thenReturn(thisDeviceName);
when(mWifiSettingsConfigStore.get(eq(WIFI_P2P_PENDING_FACTORY_RESET))).thenReturn(false);
when(mHandlerThread.getLooper()).thenReturn(mLooper.getLooper());
if (supported) {
when(mPackageManager.hasSystemFeature(eq(PackageManager.FEATURE_WIFI_DIRECT)))
.thenReturn(true);
} else {
when(mPackageManager.hasSystemFeature(eq(PackageManager.FEATURE_WIFI_DIRECT)))
.thenReturn(false);
}
when(mResources.getString(R.string.config_wifi_p2p_device_type))
.thenReturn("10-0050F204-5");
when(mWifiInjector.getFrameworkFacade()).thenReturn(mFrameworkFacade);
when(mWifiInjector.getUserManager()).thenReturn(mUserManager);
when(mWifiInjector.getWifiP2pMetrics()).thenReturn(mWifiP2pMetrics);
when(mWifiInjector.getWifiP2pMonitor()).thenReturn(mWifiMonitor);
when(mWifiInjector.getWifiP2pNative()).thenReturn(mWifiNative);
when(mWifiInjector.getWifiP2pServiceHandlerThread()).thenReturn(mHandlerThread);
when(mWifiInjector.getWifiPermissionsUtil()).thenReturn(mWifiPermissionsUtil);
when(mWifiInjector.getSettingsConfigStore()).thenReturn(mWifiSettingsConfigStore);
when(mWifiInjector.getCoexManager()).thenReturn(mCoexManager);
when(mWifiInjector.getWifiGlobals()).thenReturn(mWifiGlobals);
// enable all permissions, disable specific permissions in tests
when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(true);
when(mWifiPermissionsUtil.checkNetworkStackPermission(anyInt())).thenReturn(true);
when(mWifiPermissionsUtil.checkReadWifiCredentialPermission(anyInt())).thenReturn(true);
when(mWifiPermissionsUtil.checkConfigOverridePermission(anyInt())).thenReturn(true);
when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(),
anyBoolean())).thenReturn(true);
when(mWifiNative.setupInterface(any(), any(), any())).thenReturn(IFACE_NAME_P2P);
when(mWifiNative.p2pGetDeviceAddress()).thenReturn(thisDeviceMac);
doAnswer(new AnswerWithArguments() {
public boolean answer(WifiP2pGroupList groups) {
groups.clear();
for (WifiP2pGroup group : mGroups.getGroupList()) {
groups.add(group);
}
return true;
}
}).when(mWifiNative).p2pListNetworks(any(WifiP2pGroupList.class));
doAnswer(new AnswerWithArguments() {
public boolean answer(int netId) {
mGroups.remove(netId);
return true;
}
}).when(mWifiNative).removeP2pNetwork(anyInt());
when(mWifiSettingsConfigStore.get(eq(WIFI_VERBOSE_LOGGING_ENABLED))).thenReturn(true);
doAnswer(new AnswerWithArguments() {
public void answer(CoexManager.CoexListener listener) {
mCoexListener = listener;
}
}).when(mCoexManager).registerCoexListener(any(CoexManager.CoexListener.class));
when(mCoexManager.getCoexRestrictions()).thenReturn(0);
when(mCoexManager.getCoexUnsafeChannels()).thenReturn(Collections.emptyList());
mWifiP2pServiceImpl = new WifiP2pServiceImpl(mContext, mWifiInjector);
if (supported) {
// register these event:
// * WifiManager.WIFI_STATE_CHANGED_ACTION
// * LocationManager.MODE_CHANGED_ACTION
// * TetheringManager.ACTION_TETHER_STATE_CHANGED
verify(mContext, times(3)).registerReceiver(mBcastRxCaptor.capture(),
any(IntentFilter.class));
mWifiStateChangedReceiver = mBcastRxCaptor.getAllValues().get(0);
mLocationModeReceiver = mBcastRxCaptor.getAllValues().get(1);
mTetherStateReceiver = mBcastRxCaptor.getAllValues().get(2);
}
mWifiP2pServiceImpl.mNetdWrapper = mNetdWrapper;
mP2pStateMachineMessenger = mWifiP2pServiceImpl.getP2pStateMachineMessenger();
mWifiP2pServiceImpl.setWifiHandlerLogForTest(mLog);
mWifiP2pServiceImpl.setWifiLogForReplyChannel(mLog);
}
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mStaticMockSession = mockitoSession()
.mockStatic(NetworkInterface.class)
.startMocking();
lenient().when(NetworkInterface.getByName(eq(IFACE_NAME_P2P)))
.thenReturn(mP2pNetworkInterface);
ArrayList<InetAddress> p2pInetAddresses = new ArrayList<>();
p2pInetAddresses.add(InetAddresses.parseNumericAddress(P2P_GO_IP));
when(mP2pNetworkInterface.getInetAddresses())
.thenReturn(Collections.enumeration(p2pInetAddresses));
setUpWifiP2pServiceImpl(true);
mClient1 = new Binder();
mClient2 = new Binder();
when(mContext.createContextAsUser(any(), anyInt())).thenReturn(mContext);
}
@After
public void cleanUp() throws Exception {
mStaticMockSession.finishMocking();
}
/**
* Mock enter Disabled state.
*/
private void mockEnterDisabledState() throws Exception {
Message msg = Message.obtain();
msg.what = WifiP2pMonitor.SUP_DISCONNECTION_EVENT;
mP2pStateMachineMessenger.send(Message.obtain(msg));
mLooper.dispatchAll();
}
/**
* Mock enter GroupNegotiation state.
*/
private void mockEnterGroupNegotiationState() throws Exception {
sendCreateGroupMsg(mClientMessenger, WifiP2pGroup.NETWORK_ID_TEMPORARY, null);
}
/**
* Mock enter ProvisionDiscovery state.
*/
private void mockEnterProvisionDiscoveryState() throws Exception {
mockPeersList();
sendConnectMsg(mClientMessenger, mTestWifiP2pPeerConfig);
}
/**
* Mock WifiP2pServiceImpl.mPeers.
*/
private void mockPeersList() throws Exception {
sendDeviceFoundEventMsg(mTestWifiP2pDevice);
}
/**
* Verify that p2p init / teardown whn a client connects / disconnects
* with wifi enabled
*/
@Test
public void testP2pInitWhenClientConnectWithWifiEnabled() throws Exception {
simulateWifiStateChange(true);
checkIsP2pInitWhenClientConnected(true, false, mClient1,
new WorkSource(mClient1.getCallingUid(), TEST_PACKAGE_NAME));
checkIsP2pTearDownWhenClientDisconnected(true, false, mClient1, null);
}
/**
* Verify that p2p doesn't init when a client connects / disconnects
* with wifi disabled
*/
@Test
public void testP2pDoesntInitWhenClientConnectWithWifiDisabled()
throws Exception {
simulateWifiStateChange(false);
checkIsP2pInitWhenClientConnected(false, false, mClient1,
new WorkSource(mClient1.getCallingUid(), TEST_PACKAGE_NAME));
checkIsP2pTearDownWhenClientDisconnected(false, false, mClient1, null);
}
/**
* Verify that p2p init / teardown when wifi off / on
* with a client connected
*/
@Test
public void checkIsP2pInitForWifiChanges() throws Exception {
forceP2pEnabled(mClient1);
simulateWifiStateChange(false);
mLooper.dispatchAll();
verify(mWifiNative).teardownInterface();
verify(mWifiMonitor).stopMonitoring(anyString());
// Force to back disable state for next test
mockEnterDisabledState();
// wifi off / on won't initialize the p2p interface.
simulateWifiStateChange(true);
mLooper.dispatchAll();
verify(mWifiNative, times(1)).setupInterface(any(), any(), any());
verify(mNetdWrapper, times(1)).setInterfaceUp(anyString());
verify(mWifiMonitor, atLeastOnce()).registerHandler(anyString(), anyInt(), any());
// Lazy initialization is done once receiving a command.
sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_P2P_STATE);
verify(mWifiNative, times(2)).setupInterface(any(), any(), any());
verify(mNetdWrapper, times(2)).setInterfaceUp(anyString());
}
/**
* Verify p2p init / teardown when two clients connect / disconnect
*/
@Test
public void checkIsP2pInitForTwoClientsConnection() throws Exception {
forceP2pEnabled(mClient1);
WorkSource expectedRequestorWs =
new WorkSource(mClient1.getCallingUid(), TEST_PACKAGE_NAME);
expectedRequestorWs.add(mClient2.getCallingUid(), TEST_PACKAGE_NAME);
// P2pInit check count should keep in once, same as one client connected case.
checkIsP2pInitWhenClientConnected(false, true, mClient2, expectedRequestorWs);
reset(mWifiNative);
checkIsP2pTearDownWhenClientDisconnected(false, true, mClient2,
new WorkSource(mClient1.getCallingUid(), TEST_PACKAGE_NAME));
checkIsP2pTearDownWhenClientDisconnected(true, false, mClient1, null);
}
/**
* Verify WifiP2pManager.ADD_LOCAL_SERVICE_FAILED is returned when a caller
* uses abnormal way to send WifiP2pManager.ADD_LOCAL_SERVICE (i.e no channel info updated).
*/
@Test
public void testAddLocalServiceFailureWhenNoChannelUpdated() throws Exception {
forceP2pEnabled(mClient1);
sendAddLocalServiceMsg(mClientMessenger);
verify(mWifiNative, never()).p2pServiceAdd(any());
assertTrue(mClientHandler.hasMessages(WifiP2pManager.ADD_LOCAL_SERVICE_FAILED));
}
/**
* Verify WifiP2pManager.ADD_LOCAL_SERVICE_FAILED is returned when a caller
* uses wrong package name to initialize a channel.
*/
@Test
public void testAddLocalServiceFailureWhenChannelUpdateWrongPkgName() throws Exception {
forceP2pEnabled(mClient1);
doThrow(new SecurityException("P2p unit test"))
.when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString());
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendAddLocalServiceMsg(mClientMessenger);
assertTrue(mClientHandler.hasMessages(WifiP2pManager.ADD_LOCAL_SERVICE_FAILED));
verify(mWifiNative, never()).p2pServiceAdd(any());
}
/**
* Verify WifiP2pManager.ADD_LOCAL_SERVICE_FAILED is returned when a caller
* without proper permission attmepts to send WifiP2pManager.ADD_LOCAL_SERVICE.
*/
@Test
public void testAddLocalServiceFailureWhenCallerPermissionDenied() throws Exception {
forceP2pEnabled(mClient1);
doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString());
when(mWifiPermissionsUtil.checkCanAccessWifiDirect(
anyString(), anyString(), anyInt(), anyBoolean())).thenReturn(false);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendAddLocalServiceMsg(mClientMessenger);
assertTrue(mClientHandler.hasMessages(WifiP2pManager.ADD_LOCAL_SERVICE_FAILED));
verify(mWifiNative, never()).p2pServiceAdd(any());
verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"),
anyInt(), eq(false));
}
private void verifyAddLocalService() throws Exception {
doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString());
when(mWifiNative.p2pServiceAdd(any())).thenReturn(true);
sendAddLocalServiceMsg(mClientMessenger);
verify(mWifiNative).p2pServiceAdd(any());
verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"),
anyInt(), eq(false));
assertTrue(mClientHandler.hasMessages(WifiP2pManager.ADD_LOCAL_SERVICE_SUCCEEDED));
}
/**
* Verify the caller with proper permission sends WifiP2pManager.ADD_LOCAL_SERVICE.
*/
@Test
public void testAddLocalServiceSuccess() throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
verifyAddLocalService();
}
/**
* Verify WifiP2pManager.ADD_LOCAL_SERVICE_FAILED is returned when native call failure.
*/
@Test
public void testAddLocalServiceFailureWhenNativeCallFailure() throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString());
when(mWifiNative.p2pServiceAdd(any())).thenReturn(false);
sendAddLocalServiceMsg(mClientMessenger);
verify(mWifiNative).p2pServiceAdd(any());
verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"),
anyInt(), eq(false));
assertTrue(mClientHandler.hasMessages(WifiP2pManager.ADD_LOCAL_SERVICE_FAILED));
}
/**
* Verify WifiP2pManager.CONNECT_FAILED is returned when a caller
* uses abnormal way to send WifiP2pManager.CONNECT (i.e no channel info updated).
*/
@Test
public void testConnectWithConfigValidAsGroupFailureWhenNoChannelUpdated() throws Exception {
forceP2pEnabled(mClient1);
sendConnectMsgWithConfigValidAsGroup(mClientMessenger);
assertTrue(mClientHandler.hasMessages(WifiP2pManager.CONNECT_FAILED));
verify(mWifiNative, never()).p2pGroupAdd(any(), anyBoolean());
}
/**
* Verify WifiP2pManager.CONNECT_FAILED is returned when a caller
* uses wrong package name to initialize a channel.
*/
@Test
public void testConnectWithConfigValidAsGroupFailureWhenChannelUpdateWrongPkgName()
throws Exception {
forceP2pEnabled(mClient1);
doThrow(new SecurityException("P2p unit test"))
.when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString());
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendConnectMsgWithConfigValidAsGroup(mClientMessenger);
assertTrue(mClientHandler.hasMessages(WifiP2pManager.CONNECT_FAILED));
verify(mWifiNative, never()).p2pGroupAdd(any(), anyBoolean());
}
/**
* Verify WifiP2pManager.CONNECT_FAILED is returned when a caller
* without proper permission attmepts to send WifiP2pManager.CONNECT.
*/
@Test
public void testConnectWithConfigValidAsGroupFailureWhenPermissionDenied() throws Exception {
forceP2pEnabled(mClient1);
doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString());
when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(),
anyBoolean())).thenReturn(false);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendConnectMsgWithConfigValidAsGroup(mClientMessenger);
assertTrue(mClientHandler.hasMessages(WifiP2pManager.CONNECT_FAILED));
verify(mWifiNative, never()).p2pGroupAdd(any(), anyBoolean());
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(false));
}
/**
* Verify the caller with proper permission sends WifiP2pManager.CONNECT.
*/
@Test
public void testConnectWithConfigValidAsGroupSuccess() throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
when(mWifiNative.p2pGroupAdd(any(), eq(true))).thenReturn(true);
sendConnectMsgWithConfigValidAsGroup(mClientMessenger);
verify(mWifiNative).p2pGroupAdd(any(), eq(true));
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(false));
assertTrue(mClientHandler.hasMessages(WifiP2pManager.CONNECT_SUCCEEDED));
}
/**
* Verify WifiP2pManager.CONNECT_FAILED is returned when native call failure.
*/
@Test
public void testConnectWithConfigValidAsGroupFailureWhenNativeCallFailure() throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
when(mWifiNative.p2pGroupAdd(any(), eq(true))).thenReturn(false);
sendConnectMsgWithConfigValidAsGroup(mClientMessenger);
verify(mWifiNative).p2pGroupAdd(any(), eq(true));
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(false));
assertTrue(mClientHandler.hasMessages(WifiP2pManager.CONNECT_FAILED));
}
/**
* Verify WifiP2pManager.CREATE_GROUP_FAILED is returned when a caller
* uses abnormal way to send WifiP2pManager.CREATE_GROUP (i.e no channel info updated).
*/
@Test
public void testCreateGroupWithConfigValidAsGroupFailureWhenNoChannelUpdated()
throws Exception {
forceP2pEnabled(mClient1);
sendCreateGroupMsgWithConfigValidAsGroup(mClientMessenger);
assertTrue(mClientHandler.hasMessages(WifiP2pManager.CREATE_GROUP_FAILED));
verify(mWifiNative, never()).p2pGroupAdd(anyBoolean());
verify(mWifiNative, never()).p2pGroupAdd(any(), anyBoolean());
}
/**
* Verify WifiP2pManager.CREATE_GROUP_FAILED is returned with null object when a caller
* uses wrong package name to initialize a channel.
*/
@Test
public void testCreateGroupWithConfigValidAsGroupFailureWhenChannelUpdateWrongPkgName()
throws Exception {
forceP2pEnabled(mClient1);
doThrow(new SecurityException("P2p unit test"))
.when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString());
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendCreateGroupMsgWithConfigValidAsGroup(mClientMessenger);
assertTrue(mClientHandler.hasMessages(WifiP2pManager.CREATE_GROUP_FAILED));
verify(mWifiNative, never()).p2pGroupAdd(anyBoolean());
verify(mWifiNative, never()).p2pGroupAdd(any(), anyBoolean());
}
/**
* Verify WifiP2pManager.CREATE_GROUP_FAILED is returned when a caller
* without proper permission attmepts to send WifiP2pManager.CREATE_GROUP.
*/
@Test
public void testCreateGroupWithConfigValidAsGroupFailureWhenPermissionDenied()
throws Exception {
forceP2pEnabled(mClient1);
doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString());
when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(),
anyBoolean())).thenReturn(false);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendCreateGroupMsgWithConfigValidAsGroup(mClientMessenger);
assertTrue(mClientHandler.hasMessages(WifiP2pManager.CREATE_GROUP_FAILED));
verify(mWifiNative, never()).p2pGroupAdd(anyBoolean());
verify(mWifiNative, never()).p2pGroupAdd(any(), anyBoolean());
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(false));
}
/**
* Verify the caller with proper permission sends WifiP2pManager.CREATE_GROUP.
*/
@Test
public void testCreateGroupWithConfigValidAsGroupSuccess() throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
when(mWifiNative.p2pGroupAdd(any(), eq(false))).thenReturn(true);
sendCreateGroupMsgWithConfigValidAsGroup(mClientMessenger);
verify(mWifiNative).p2pGroupAdd(any(), eq(false));
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(false));
assertTrue(mClientHandler.hasMessages(WifiP2pManager.CREATE_GROUP_SUCCEEDED));
}
/**
* Verify WifiP2pManager.CREATE_GROUP_FAILED is returned when native call failure.
*/
@Test
public void testCreateGroupWithConfigValidAsGroupFailureWhenNativeCallFailure()
throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
when(mWifiNative.p2pGroupAdd(any(), eq(false))).thenReturn(false);
sendCreateGroupMsgWithConfigValidAsGroup(mClientMessenger);
verify(mWifiNative).p2pGroupAdd(any(), eq(false));
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(false));
assertTrue(mClientHandler.hasMessages(WifiP2pManager.CREATE_GROUP_FAILED));
}
/**
* Verify WifiP2pManager.DISCOVER_PEERS_FAILED is returned when a caller
* uses abnormal way to send WifiP2pManager.REQUEST_PEERS (i.e no channel info updated).
*/
@Test
public void testDiscoverPeersFailureWhenNoChannelUpdated() throws Exception {
forceP2pEnabled(mClient1);
sendDiscoverPeersMsg(mClientMessenger);
verify(mWifiNative, never()).p2pFind(anyInt());
assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_PEERS_FAILED));
}
/**
* Verify WifiP2pManager.DISCOVER_PEERS_FAILED is returned when a caller
* uses wrong package name to initialize a channel.
*/
@Test
public void testDiscoverPeersFailureWhenChannelUpdateWrongPkgName() throws Exception {
forceP2pEnabled(mClient1);
doThrow(new SecurityException("P2p unit test"))
.when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString());
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendDiscoverPeersMsg(mClientMessenger);
verify(mWifiNative, never()).p2pFind(anyInt());
assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_PEERS_FAILED));
}
/**
* Verify WifiP2pManager.DISCOVER_PEERS_FAILED is returned with null object when a caller
* without proper permission attmepts to send WifiP2pManager.DISCOVER_PEERS.
*/
@Test
public void testDiscoverPeersFailureWhenPermissionDenied() throws Exception {
forceP2pEnabled(mClient1);
doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString());
when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(),
anyBoolean())).thenReturn(false);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendDiscoverPeersMsg(mClientMessenger);
verify(mWifiNative, never()).p2pFind(anyInt());
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true));
assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_PEERS_FAILED));
}
/**
* Verify WifiP2pManager.DISCOVER_PEERS_FAILED is returned with null object when a caller
* attmepts to send WifiP2pManager.DISCOVER_PEERS and location mode is disabled.
*/
@Test
public void testDiscoverPeersFailureWhenLocationModeDisabled() throws Exception {
forceP2pEnabled(mClient1);
doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString());
when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(),
eq(false))).thenReturn(true);
when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(),
eq(true))).thenReturn(false);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendDiscoverPeersMsg(mClientMessenger);
verify(mWifiNative, never()).p2pFind(anyInt());
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true));
assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_PEERS_FAILED));
}
/**
* Verify the caller with proper permission sends WifiP2pManager.DISCOVER_PEERS.
*/
@Test
public void testDiscoverPeersSuccess() throws Exception {
forceP2pEnabled(mClient1);
when(mWifiNative.p2pFind(anyInt())).thenReturn(true);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendDiscoverPeersMsg(mClientMessenger);
verify(mWifiNative).p2pFind(anyInt());
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true));
assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_PEERS_SUCCEEDED));
}
/**
* Verify WifiP2pManager.DISCOVER_PEERS_FAILED is returned when native call failure.
*/
@Test
public void testDiscoverPeersFailureWhenNativeCallFailure() throws Exception {
forceP2pEnabled(mClient1);
when(mWifiNative.p2pFind(anyInt())).thenReturn(false);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendDiscoverPeersMsg(mClientMessenger);
verify(mWifiNative).p2pFind(anyInt());
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true));
assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_PEERS_FAILED));
}
/**
* Verify WifiP2pManager.DISCOVER_SERVICES_FAILED is returned when a caller
* uses abnormal way to send WifiP2pManager.DISCOVER_SERVICES (i.e no channel info updated).
*/
@Test
public void testDiscoverServicesFailureWhenNoChannelUpdated() throws Exception {
when(mWifiNative.p2pServDiscReq(anyString(), anyString())).thenReturn("mServiceDiscReqId");
forceP2pEnabled(mClient1);
sendAddServiceRequestMsg(mClientMessenger);
sendDiscoverServiceMsg(mClientMessenger);
verify(mWifiNative, never()).p2pServDiscReq(anyString(), anyString());
verify(mWifiNative, never()).p2pFind(anyInt());
assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_SERVICES_FAILED));
}
/**
* Verify WifiP2pManager.DISCOVER_SERVICES_FAILED is returned when a caller
* uses wrong package name to initialize a channel.
*/
@Test
public void testDiscoverServicesFailureWhenChannelUpdateWrongPkgName() throws Exception {
when(mWifiNative.p2pServDiscReq(anyString(), anyString())).thenReturn("mServiceDiscReqId");
forceP2pEnabled(mClient1);
doThrow(new SecurityException("P2p unit test"))
.when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString());
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendAddServiceRequestMsg(mClientMessenger);
sendDiscoverServiceMsg(mClientMessenger);
verify(mWifiNative, never()).p2pServDiscReq(anyString(), anyString());
verify(mWifiNative, never()).p2pFind(anyInt());
assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_SERVICES_FAILED));
}
/**
* Verify WifiP2pManager.DISCOVER_SERVICES_FAILED is returned when a caller
* without proper permission attmepts to send WifiP2pManager.DISCOVER_SERVICES.
*/
@Test
public void testDiscoverServicesFailureWhenPermissionDenied() throws Exception {
when(mWifiNative.p2pServDiscReq(anyString(), anyString()))
.thenReturn("mServiceDiscReqId");
forceP2pEnabled(mClient1);
doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString());
when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(),
anyBoolean())).thenReturn(false);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendAddServiceRequestMsg(mClientMessenger);
sendDiscoverServiceMsg(mClientMessenger);
verify(mWifiNative, never()).p2pServDiscReq(anyString(), anyString());
verify(mWifiNative, never()).p2pFind(anyInt());
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true));
assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_SERVICES_FAILED));
}
/**
* Verify WifiP2pManager.DISCOVER_SERVICES_FAILED is returned when a caller
* attmepts to send WifiP2pManager.DISCOVER_SERVICES and location mode is disabled.
*/
@Test
public void testDiscoverServicesFailureWhenLocationModeDisabled() throws Exception {
when(mWifiNative.p2pServDiscReq(anyString(), anyString()))
.thenReturn("mServiceDiscReqId");
forceP2pEnabled(mClient1);
doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString());
when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(),
eq(false))).thenReturn(true);
when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(),
eq(true))).thenReturn(false);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendAddServiceRequestMsg(mClientMessenger);
sendDiscoverServiceMsg(mClientMessenger);
verify(mWifiNative, never()).p2pServDiscReq(anyString(), anyString());
verify(mWifiNative, never()).p2pFind(anyInt());
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true));
assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_SERVICES_FAILED));
}
/**
* Verify the caller with proper permission sends WifiP2pManager.DISCOVER_SERVICES.
*/
@Test
public void testDiscoverServicesSuccess() throws Exception {
when(mWifiNative.p2pServDiscReq(anyString(), anyString()))
.thenReturn("mServiceDiscReqId");
when(mWifiNative.p2pFind(anyInt())).thenReturn(true);
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendAddServiceRequestMsg(mClientMessenger);
sendDiscoverServiceMsg(mClientMessenger);
verify(mWifiNative).p2pServDiscReq(anyString(), anyString());
verify(mWifiNative).p2pFind(anyInt());
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true));
assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_SERVICES_SUCCEEDED));
}
/**
* Verify WifiP2pManager.DISCOVER_SERVICES_FAILED is returned when add service failure.
*/
@Test
public void testDiscoverServicesFailureWhenAddServiceRequestFailure() throws Exception {
when(mWifiNative.p2pServDiscReq(anyString(), anyString())).thenReturn(null);
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendAddServiceRequestMsg(mClientMessenger);
sendDiscoverServiceMsg(mClientMessenger);
verify(mWifiNative).p2pServDiscReq(anyString(), anyString());
verify(mWifiNative, never()).p2pFind(anyInt());
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true));
assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_SERVICES_FAILED));
}
/**
* Verify WifiP2pManager.DISCOVER_SERVICES_FAILED is returned when native call failure.
*/
@Test
public void testDiscoverServicesFailureWhenNativeCallFailure() throws Exception {
when(mWifiNative.p2pServDiscReq(anyString(), anyString()))
.thenReturn("mServiceDiscReqId");
when(mWifiNative.p2pFind(anyInt())).thenReturn(false);
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendAddServiceRequestMsg(mClientMessenger);
sendDiscoverServiceMsg(mClientMessenger);
verify(mWifiNative).p2pServDiscReq(anyString(), anyString());
verify(mWifiNative).p2pFind(anyInt());
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true));
assertTrue(mClientHandler.hasMessages(WifiP2pManager.DISCOVER_SERVICES_FAILED));
}
/**
* Verify WifiP2pManager.RESPONSE_PEERS is returned with null object when a caller
* uses abnormal way to send WifiP2pManager.REQUEST_PEERS (i.e no channel info updated).
*/
@Test
public void testRequestPeersFailureWhenNoChannelUpdated() throws Exception {
forceP2pEnabled(mClient1);
mockPeersList();
sendRequestPeersMsg(mClientMessenger);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
WifiP2pDeviceList peers = (WifiP2pDeviceList) mMessageCaptor.getValue().obj;
assertEquals(WifiP2pManager.RESPONSE_PEERS, mMessageCaptor.getValue().what);
assertNull(peers.get(mTestWifiP2pDevice.deviceAddress));
}
/**
* Verify WifiP2pManager.RESPONSE_PEERS is returned with null object when a caller
* uses wrong package name to initialize a channel.
*/
@Test
public void testRequestPeersFailureWhenChannelUpdateWrongPkgName() throws Exception {
forceP2pEnabled(mClient1);
mockPeersList();
doThrow(new SecurityException("P2p unit test"))
.when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString());
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendRequestPeersMsg(mClientMessenger);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
WifiP2pDeviceList peers = (WifiP2pDeviceList) mMessageCaptor.getValue().obj;
assertEquals(WifiP2pManager.RESPONSE_PEERS, mMessageCaptor.getValue().what);
assertNull(peers.get(mTestWifiP2pDevice.deviceAddress));
}
/**
* Verify WifiP2pManager.RESPONSE_PEERS is returned with null object when a caller
* without proper permission attmepts to send WifiP2pManager.REQUEST_PEERS.
*/
@Test
public void testRequestPeersFailureWhenPermissionDenied() throws Exception {
forceP2pEnabled(mClient1);
mockPeersList();
doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString());
when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(),
anyBoolean())).thenReturn(false);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendRequestPeersMsg(mClientMessenger);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true));
WifiP2pDeviceList peers = (WifiP2pDeviceList) mMessageCaptor.getValue().obj;
assertEquals(WifiP2pManager.RESPONSE_PEERS, mMessageCaptor.getValue().what);
assertNull(peers.get(mTestWifiP2pDevice.deviceAddress));
}
/**
* Verify WifiP2pManager.RESPONSE_PEERS is returned with null object when a caller
* attmepts to send WifiP2pManager.REQUEST_PEERS and location mode is disabled.
*/
@Test
public void testRequestPeersFailureWhenLocationModeDisabled() throws Exception {
forceP2pEnabled(mClient1);
mockPeersList();
doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString());
when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(),
eq(false))).thenReturn(true);
when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(),
eq(true))).thenReturn(false);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendRequestPeersMsg(mClientMessenger);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true));
WifiP2pDeviceList peers = (WifiP2pDeviceList) mMessageCaptor.getValue().obj;
assertEquals(WifiP2pManager.RESPONSE_PEERS, mMessageCaptor.getValue().what);
assertNull(peers.get(mTestWifiP2pDevice.deviceAddress));
}
/**
* Verify WifiP2pManager.RESPONSE_PEERS is returned with expect object when a caller
* with proper permission to send WifiP2pManager.REQUEST_PEERS.
*/
@Test
public void testRequestPeersSuccess() throws Exception {
forceP2pEnabled(mClient1);
mockPeersList();
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendRequestPeersMsg(mClientMessenger);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true));
WifiP2pDeviceList peers = (WifiP2pDeviceList) mMessageCaptor.getValue().obj;
assertEquals(WifiP2pManager.RESPONSE_PEERS, mMessageCaptor.getValue().what);
assertNotEquals(null, peers.get(mTestWifiP2pDevice.deviceAddress));
}
/**
* Verify WifiP2pManager.RESPONSE_GROUP_INFO is returned with null object when a caller
* uses abnormal way to send WifiP2pManager.REQUEST_GROUP_INFO (i.e no channel info updated).
*/
@Test
public void testRequestGroupInfoFailureWhenNoChannelUpdated() throws Exception {
forceP2pEnabled(mClient1);
sendGroupStartedMsg(mTestWifiP2pGroup);
simulateTetherReady();
sendRequestGroupInfoMsg(mClientMessenger);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
assertEquals(WifiP2pManager.RESPONSE_GROUP_INFO, mMessageCaptor.getValue().what);
assertNull(mMessageCaptor.getValue().obj);
}
/**
* Verify WifiP2pManager.RESPONSE_GROUP_INFO is returned with null object when a caller
* uses wrong package name to initialize a channel.
*/
@Test
public void testRequestGroupInfoFailureWhenChannelUpdateWrongPkgName() throws Exception {
forceP2pEnabled(mClient1);
sendGroupStartedMsg(mTestWifiP2pGroup);
simulateTetherReady();
doThrow(new SecurityException("P2p unit test"))
.when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString());
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendRequestGroupInfoMsg(mClientMessenger);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
assertEquals(WifiP2pManager.RESPONSE_GROUP_INFO, mMessageCaptor.getValue().what);
assertNull(mMessageCaptor.getValue().obj);
}
/**
* Verify WifiP2pManager.RESPONSE_GROUP_INFO is returned with null object when a caller
* without proper permission attempts to send WifiP2pManager.REQUEST_GROUP_INFO.
*/
@Test
public void testRequestGroupInfoFailureWhenPermissionDenied() throws Exception {
forceP2pEnabled(mClient1);
sendGroupStartedMsg(mTestWifiP2pGroup);
simulateTetherReady();
doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString());
when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(),
anyBoolean())).thenReturn(false);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendRequestGroupInfoMsg(mClientMessenger);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(false));
assertEquals(WifiP2pManager.RESPONSE_GROUP_INFO, mMessageCaptor.getValue().what);
assertNull(mMessageCaptor.getValue().obj);
}
/**
* Verify WifiP2pManager.RESPONSE_GROUP_INFO is returned with expect object when a caller
* with proper permission.
*/
@Test
public void testRequestGroupInfoSuccess() throws Exception {
mTestWifiP2pGroup.setOwner(mTestThisDevice);
forceP2pEnabled(mClient1);
sendGroupStartedMsg(mTestWifiP2pGroup);
simulateTetherReady();
when(mWifiPermissionsUtil.checkLocalMacAddressPermission(anyInt())).thenReturn(false);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendRequestGroupInfoMsg(mClientMessenger);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(false));
assertEquals(WifiP2pManager.RESPONSE_GROUP_INFO, mMessageCaptor.getValue().what);
WifiP2pGroup wifiP2pGroup = (WifiP2pGroup) mMessageCaptor.getValue().obj;
assertEquals(mTestWifiP2pGroup.getNetworkName(), wifiP2pGroup.getNetworkName());
// Ensure that our own MAC address is anonymized if we're the group owner.
assertEquals(ANONYMIZED_DEVICE_ADDRESS, wifiP2pGroup.getOwner().deviceAddress);
}
/**
* Verify WifiP2pManager.RESPONSE_GROUP_INFO does not anonymize this device's MAC address when
* requested by an app with the LOCAL_MAC_ADDRESS permission.
*/
@Test
public void testRequestGroupInfoIncludesMacForNetworkSettingsApp() throws Exception {
mTestWifiP2pGroup.setOwner(mTestThisDevice);
forceP2pEnabled(mClient1);
sendGroupStartedMsg(mTestWifiP2pGroup);
simulateTetherReady();
when(mWifiPermissionsUtil.checkLocalMacAddressPermission(anyInt())).thenReturn(true);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendRequestGroupInfoMsg(mClientMessenger);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(false));
assertEquals(WifiP2pManager.RESPONSE_GROUP_INFO, mMessageCaptor.getValue().what);
WifiP2pGroup wifiP2pGroup = (WifiP2pGroup) mMessageCaptor.getValue().obj;
assertEquals(thisDeviceMac, wifiP2pGroup.getOwner().deviceAddress);
}
/**
* Verify WifiP2pManager.START_LISTEN_FAILED is returned when a caller
* without proper permission attempts to send WifiP2pManager.START_LISTEN.
*/
@Test
public void testStartListenFailureWhenPermissionDenied() throws Exception {
when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(false);
forceP2pEnabled(mClient1);
sendSimpleMsg(mClientMessenger, WifiP2pManager.START_LISTEN);
assertTrue(mClientHandler.hasMessages(WifiP2pManager.START_LISTEN_FAILED));
// p2pFlush should be invoked once in forceP2pEnabled.
verify(mWifiNative).p2pFlush();
verify(mWifiNative, never()).p2pExtListen(anyBoolean(), anyInt(), anyInt());
}
/**
* Verify WifiP2pManager.START_LISTEN_FAILED is returned when native call failure.
*/
@Test
public void testStartListenFailureWhenNativeCallFailure() throws Exception {
when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(true);
when(mWifiNative.p2pExtListen(eq(true), anyInt(), anyInt())).thenReturn(false);
forceP2pEnabled(mClient1);
sendSimpleMsg(mClientMessenger, WifiP2pManager.START_LISTEN);
// p2pFlush would be invoked in forceP2pEnabled and startListen both.
verify(mWifiNative, times(2)).p2pFlush();
verify(mWifiNative).p2pExtListen(eq(true), anyInt(), anyInt());
assertTrue(mClientHandler.hasMessages(WifiP2pManager.START_LISTEN_FAILED));
}
/**
* Verify the caller with proper permission sends WifiP2pManager.START_LISTEN.
*/
@Test
public void testStartListenSuccess() throws Exception {
when(mWifiNative.p2pExtListen(eq(true), anyInt(), anyInt())).thenReturn(true);
when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(true);
forceP2pEnabled(mClient1);
sendSimpleMsg(mClientMessenger, WifiP2pManager.START_LISTEN);
// p2pFlush would be invoked in forceP2pEnabled and startListen both.
verify(mWifiNative, times(2)).p2pFlush();
verify(mWifiNative).p2pExtListen(eq(true), anyInt(), anyInt());
assertTrue(mClientHandler.hasMessages(WifiP2pManager.START_LISTEN_SUCCEEDED));
}
/**
* Verify WifiP2pManager.STOP_LISTEN_FAILED is returned when a caller
* without proper permission attempts to sends WifiP2pManager.STOP_LISTEN.
*/
@Test
public void testStopListenFailureWhenPermissionDenied() throws Exception {
when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(false);
forceP2pEnabled(mClient1);
sendSimpleMsg(mClientMessenger, WifiP2pManager.STOP_LISTEN);
assertTrue(mClientHandler.hasMessages(WifiP2pManager.STOP_LISTEN_FAILED));
verify(mWifiNative, never()).p2pExtListen(anyBoolean(), anyInt(), anyInt());
}
/**
* Verify WifiP2pManager.STOP_LISTEN_FAILED is returned when native call failure.
*/
@Test
public void testStopListenFailureWhenNativeCallFailure() throws Exception {
when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(true);
when(mWifiNative.p2pExtListen(eq(false), anyInt(), anyInt())).thenReturn(false);
forceP2pEnabled(mClient1);
sendSimpleMsg(mClientMessenger, WifiP2pManager.STOP_LISTEN);
verify(mWifiNative).p2pExtListen(eq(false), anyInt(), anyInt());
assertTrue(mClientHandler.hasMessages(WifiP2pManager.STOP_LISTEN_FAILED));
}
/**
* Verify the caller with proper permission sends WifiP2pManager.STOP_LISTEN.
*/
@Test
public void testStopListenSuccess() throws Exception {
when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(true);
when(mWifiNative.p2pExtListen(eq(false), anyInt(), anyInt())).thenReturn(true);
forceP2pEnabled(mClient1);
sendSimpleMsg(mClientMessenger, WifiP2pManager.STOP_LISTEN);
verify(mWifiNative).p2pExtListen(eq(false), anyInt(), anyInt());
assertTrue(mClientHandler.hasMessages(WifiP2pManager.STOP_LISTEN_SUCCEEDED));
}
/** Verify the p2p randomized MAC feature is enabled if OEM supports it. */
@Test
public void testP2pRandomMacWithOemSupport() throws Exception {
when(mResources.getBoolean(R.bool.config_wifi_p2p_mac_randomization_supported))
.thenReturn(true);
forceP2pEnabled(mClient1);
verify(mWifiNative, never()).setMacRandomization(eq(false));
verify(mWifiNative).setMacRandomization(eq(true));
}
/** Verify the p2p randomized MAC feature is disabled if OEM does not support it. */
@Test
public void testP2pRandomMacWithoutOemSupport() throws Exception {
when(mResources.getBoolean(R.bool.config_wifi_p2p_mac_randomization_supported))
.thenReturn(false);
forceP2pEnabled(mClient1);
verify(mWifiNative, never()).setMacRandomization(eq(true));
verify(mWifiNative).setMacRandomization(eq(false));
}
/**
* Verify the caller sends WifiP2pManager.DELETE_PERSISTENT_GROUP.
*/
@Test
public void testDeletePersistentGroupSuccess() throws Exception {
// Move to enabled state
forceP2pEnabled(mClient1);
sendDeletePersistentGroupMsg(mClientMessenger, WifiP2pGroup.NETWORK_ID_PERSISTENT);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.DELETE_PERSISTENT_GROUP_SUCCEEDED, message.what);
}
/**
* Verify that respond with DELETE_PERSISTENT_GROUP_FAILED
* when caller sends DELETE_PERSISTENT_GROUP and p2p is disabled.
*/
@Test
public void testDeletePersistentGroupFailureWhenP2pDisabled() throws Exception {
sendDeletePersistentGroupMsg(mClientMessenger, WifiP2pGroup.NETWORK_ID_PERSISTENT);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.DELETE_PERSISTENT_GROUP_FAILED, message.what);
assertEquals(WifiP2pManager.BUSY, message.arg1);
}
/**
* Verify that respond with DELETE_PERSISTENT_GROUP_FAILED
* when caller sends DELETE_PERSISTENT_GROUP and p2p is unsupported.
*/
@Test
public void testDeletePersistentGroupFailureWhenP2pUnsupported() throws Exception {
setUpWifiP2pServiceImpl(false);
sendDeletePersistentGroupMsg(mClientMessenger, WifiP2pGroup.NETWORK_ID_PERSISTENT);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.DELETE_PERSISTENT_GROUP_FAILED, message.what);
assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1);
}
/**
* Verify that respond with DELETE_PERSISTENT_GROUP_FAILED
* when caller sends DELETE_PERSISTENT_GROUP and doesn't have the necessary permissions.
*/
@Test
public void testDeletePersistentGroupFailureWhenNoPermissions() throws Exception {
// Move to enabled state
forceP2pEnabled(mClient1);
// no permissions held
when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(false);
when(mWifiPermissionsUtil.checkNetworkStackPermission(anyInt())).thenReturn(false);
when(mWifiPermissionsUtil.checkConfigOverridePermission(anyInt())).thenReturn(false);
sendDeletePersistentGroupMsg(mClientMessenger, WifiP2pGroup.NETWORK_ID_PERSISTENT);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.DELETE_PERSISTENT_GROUP_FAILED, message.what);
assertEquals(WifiP2pManager.ERROR, message.arg1);
}
/**
* Verify the peer scan counter is increased while receiving WifiP2pManager.DISCOVER_PEERS at
* P2pEnabledState.
*/
@Test
public void testPeerScanMetricWhenSendDiscoverPeers() throws Exception {
forceP2pEnabled(mClient1);
when(mWifiNative.p2pFind(anyInt())).thenReturn(true);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendDiscoverPeersMsg(mClientMessenger);
verify(mWifiP2pMetrics).incrementPeerScans();
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true));
}
/**
* Verify the service scan counter is increased while receiving
* WifiP2pManager.DISCOVER_SERVICES at P2pEnabledState.
*/
@Test
public void testServiceScanMetricWhenSendDiscoverServices() throws Exception {
when(mWifiNative.p2pServDiscReq(anyString(), anyString()))
.thenReturn("mServiceDiscReqId");
when(mWifiNative.p2pFind(anyInt())).thenReturn(true);
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendAddServiceRequestMsg(mClientMessenger);
sendDiscoverServiceMsg(mClientMessenger);
verify(mWifiP2pMetrics).incrementServiceScans();
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(true));
}
/**
* Verify the persistent group counter is updated while receiving
* WifiP2pManager.FACTORY_RESET.
*/
@Test
public void testPersistentGroupMetricWhenSendFactoryReset() throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
// permissions for factory reset
when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt()))
.thenReturn(true);
when(mUserManager.hasUserRestrictionForUser(eq(UserManager.DISALLOW_NETWORK_RESET), any()))
.thenReturn(false);
when(mUserManager.hasUserRestrictionForUser(eq(UserManager.DISALLOW_CONFIG_WIFI), any()))
.thenReturn(false);
ArgumentCaptor<WifiP2pGroupList> groupsCaptor =
ArgumentCaptor.forClass(WifiP2pGroupList.class);
verify(mWifiP2pMetrics).updatePersistentGroup(groupsCaptor.capture());
assertEquals(3, groupsCaptor.getValue().getGroupList().size());
sendSimpleMsg(mClientMessenger, WifiP2pManager.FACTORY_RESET);
verify(mWifiP2pMetrics, times(2)).updatePersistentGroup(groupsCaptor.capture());
// the captured object is the same object, just get the latest one is ok.
assertEquals(0, groupsCaptor.getValue().getGroupList().size());
}
/**
* Verify the persistent group counter is updated while receiving
* WifiP2pMonitor.P2P_GROUP_STARTED_EVENT.
*/
@Test
public void testPersistentGroupMetricWhenSendP2pGroupStartedEvent() throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
ArgumentCaptor<WifiP2pGroupList> groupsCaptor =
ArgumentCaptor.forClass(WifiP2pGroupList.class);
verify(mWifiP2pMetrics).updatePersistentGroup(groupsCaptor.capture());
assertEquals(3, groupsCaptor.getValue().getGroupList().size());
sendGroupStartedMsg(mTestWifiP2pNewPersistentGoGroup);
simulateTetherReady();
verify(mWifiP2pMetrics, times(2)).updatePersistentGroup(groupsCaptor.capture());
// the captured object is the same object, just get the latest one is ok.
assertEquals(4, groupsCaptor.getValue().getGroupList().size());
}
/**
* Verify the persistent group counter is updated while receiving
* WifiP2pManager.DELETE_PERSISTENT_GROUP.
*/
@Test
public void testPersistentGroupMetricWhenSendDeletePersistentGroup() throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
ArgumentCaptor<WifiP2pGroupList> groupsCaptor =
ArgumentCaptor.forClass(WifiP2pGroupList.class);
verify(mWifiP2pMetrics).updatePersistentGroup(groupsCaptor.capture());
assertEquals(3, groupsCaptor.getValue().getGroupList().size());
sendDeletePersistentGroupMsg(mClientMessenger, 0);
verify(mWifiP2pMetrics, times(2)).updatePersistentGroup(groupsCaptor.capture());
// the captured object is the same object, just get the latest one is ok.
assertEquals(2, groupsCaptor.getValue().getGroupList().size());
}
/**
* Verify the group event.
*/
@Test
public void testGroupEventMetric() throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendGroupStartedMsg(mTestWifiP2pNewPersistentGoGroup);
simulateTetherReady();
ArgumentCaptor<WifiP2pGroup> groupCaptor =
ArgumentCaptor.forClass(WifiP2pGroup.class);
verify(mWifiP2pMetrics).startGroupEvent(groupCaptor.capture());
WifiP2pGroup groupCaptured = groupCaptor.getValue();
assertEquals(mTestWifiP2pNewPersistentGoGroup.toString(), groupCaptured.toString());
sendGroupRemovedMsg();
verify(mWifiP2pMetrics).endGroupEvent();
}
/**
* Verify the connection event for a fresh connection.
*/
@Test
public void testStartFreshConnectionEventWhenSendConnect() throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
mockPeersList();
sendConnectMsg(mClientMessenger, mTestWifiP2pPeerConfig);
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(false));
ArgumentCaptor<WifiP2pConfig> configCaptor =
ArgumentCaptor.forClass(WifiP2pConfig.class);
verify(mWifiP2pMetrics).startConnectionEvent(
eq(P2pConnectionEvent.CONNECTION_FRESH),
configCaptor.capture());
assertEquals(mTestWifiP2pPeerConfig.toString(), configCaptor.getValue().toString());
}
/**
* Verify the connection event for a reinvoked connection.
*/
@Test
public void testStartReinvokeConnectionEventWhenSendConnect() throws Exception {
forceP2pEnabled(mClient1);
when(mWifiNative.p2pGroupAdd(anyInt()))
.thenReturn(true);
when(mTestWifiP2pDevice.isGroupOwner()).thenReturn(true);
when(mWifiNative.p2pGetSsid(eq(mTestWifiP2pDevice.deviceAddress)))
.thenReturn(mTestWifiP2pGroup.getNetworkName());
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
mockPeersList();
sendConnectMsg(mClientMessenger, mTestWifiP2pPeerConfig);
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(false));
ArgumentCaptor<WifiP2pConfig> configCaptor =
ArgumentCaptor.forClass(WifiP2pConfig.class);
verify(mWifiP2pMetrics).startConnectionEvent(
eq(P2pConnectionEvent.CONNECTION_REINVOKE),
configCaptor.capture());
assertEquals(mTestWifiP2pPeerConfig.toString(), configCaptor.getValue().toString());
}
/**
* Verify the connection event for a reinvoked connection via
* createGroup API.
*
* If there is a persistent group whose owner is this deivce, this would be
* a reinvoked group.
*/
@Test
public void testStartReinvokeConnectionEventWhenCreateGroup()
throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendCreateGroupMsg(mClientMessenger, WifiP2pGroup.NETWORK_ID_PERSISTENT, null);
verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"),
anyInt(), eq(false));
verify(mWifiP2pMetrics).startConnectionEvent(
eq(P2pConnectionEvent.CONNECTION_REINVOKE),
eq(null));
}
/**
* Verify the connection event for a local connection while setting
* netId to {@link WifiP2pGroup#NETWORK_ID_PERSISTENT}.
*/
@Test
public void testStartLocalConnectionWhenCreateGroup() throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
// permissions for factory reset
when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt()))
.thenReturn(true);
when(mUserManager.hasUserRestrictionForUser(eq(UserManager.DISALLOW_NETWORK_RESET), any()))
.thenReturn(false);
when(mUserManager.hasUserRestrictionForUser(eq(UserManager.DISALLOW_CONFIG_WIFI), any()))
.thenReturn(false);
// There is one group hosted by this device in mGroups.
// clear all groups to avoid re-invoking a group.
sendSimpleMsg(mClientMessenger, WifiP2pManager.FACTORY_RESET);
sendCreateGroupMsg(mClientMessenger, WifiP2pGroup.NETWORK_ID_PERSISTENT, null);
verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"),
anyInt(), eq(false));
verify(mWifiP2pMetrics).startConnectionEvent(
eq(P2pConnectionEvent.CONNECTION_LOCAL),
eq(null));
}
/**
* Verify the connection event for a local connection while setting the
* netId to {@link WifiP2pGroup#NETWORK_ID_TEMPORARY}.
*/
@Test
public void testStartLocalConnectionEventWhenCreateTemporaryGroup() throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendCreateGroupMsg(mClientMessenger, WifiP2pGroup.NETWORK_ID_TEMPORARY, null);
verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"),
anyInt(), eq(false));
verify(mWifiP2pMetrics).startConnectionEvent(
eq(P2pConnectionEvent.CONNECTION_LOCAL),
eq(null));
}
/**
* Verify the connection event for a fast connection via
* connect with config.
*/
@Test
public void testStartFastConnectionEventWhenSendConnectWithConfig()
throws Exception {
forceP2pEnabled(mClient1);
when(mWifiNative.p2pGroupAdd(any(), eq(true))).thenReturn(true);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendConnectMsg(mClientMessenger, mTestWifiP2pFastConnectionConfig);
verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"),
anyInt(), eq(false));
ArgumentCaptor<WifiP2pConfig> configCaptor =
ArgumentCaptor.forClass(WifiP2pConfig.class);
verify(mWifiP2pMetrics).startConnectionEvent(
eq(P2pConnectionEvent.CONNECTION_FAST),
configCaptor.capture());
assertEquals(mTestWifiP2pFastConnectionConfig.toString(),
configCaptor.getValue().toString());
}
/**
* Verify the connection event for a fast connection via
* createGroup API with config.
*/
@Test
public void testStartFastConnectionEventWhenCreateGroupWithConfig()
throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendCreateGroupMsg(mClientMessenger, 0, mTestWifiP2pFastConnectionConfig);
verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"),
anyInt(), eq(false));
ArgumentCaptor<WifiP2pConfig> configCaptor =
ArgumentCaptor.forClass(WifiP2pConfig.class);
verify(mWifiP2pMetrics).startConnectionEvent(
eq(P2pConnectionEvent.CONNECTION_FAST),
configCaptor.capture());
assertEquals(mTestWifiP2pFastConnectionConfig.toString(),
configCaptor.getValue().toString());
}
/**
* Verify the connection event ends while the group is formed.
*/
@Test
public void testEndConnectionEventWhenGroupFormed() throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
WifiP2pGroup group = new WifiP2pGroup();
group.setNetworkId(WifiP2pGroup.NETWORK_ID_PERSISTENT);
group.setNetworkName("DIRECT-xy-NEW");
group.setOwner(new WifiP2pDevice("thisDeviceMac"));
group.setIsGroupOwner(true);
group.setInterface(IFACE_NAME_P2P);
sendGroupStartedMsg(group);
simulateTetherReady();
verify(mWifiP2pMetrics).endConnectionEvent(
eq(P2pConnectionEvent.CLF_NONE));
}
/**
* Verify the connection event ends due to timeout.
*/
@Test
public void testEndConnectionEventWhenTimeout() throws Exception {
forceP2pEnabled(mClient1);
when(mWifiNative.p2pGroupAdd(anyBoolean())).thenReturn(true);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
mockEnterGroupNegotiationState();
verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"),
anyInt(), eq(false));
mLooper.moveTimeForward(120 * 1000 * 2);
mLooper.dispatchAll();
verify(mWifiP2pMetrics).endConnectionEvent(
eq(P2pConnectionEvent.CLF_TIMEOUT));
}
/**
* Verify the connection event ends due to the cancellation.
*/
@Test
public void testEndConnectionEventWhenCancel() throws Exception {
forceP2pEnabled(mClient1);
when(mWifiNative.p2pGroupAdd(anyBoolean())).thenReturn(true);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
mockEnterGroupNegotiationState();
verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"),
anyInt(), eq(false));
sendSimpleMsg(mClientMessenger, WifiP2pManager.CANCEL_CONNECT);
verify(mWifiP2pMetrics).endConnectionEvent(
eq(P2pConnectionEvent.CLF_CANCEL));
}
/**
* Verify the connection event ends due to the provision discovery failure.
*/
@Test
public void testEndConnectionEventWhenProvDiscFailure() throws Exception {
forceP2pEnabled(mClient1);
when(mWifiNative.p2pGroupAdd(anyBoolean())).thenReturn(true);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
mockEnterProvisionDiscoveryState();
verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"),
anyInt(), eq(false));
sendSimpleMsg(null, WifiP2pMonitor.P2P_PROV_DISC_FAILURE_EVENT);
verify(mWifiP2pMetrics).endConnectionEvent(
eq(P2pConnectionEvent.CLF_PROV_DISC_FAIL));
}
/**
* Verify the connection event ends due to the group removal.
*/
@Test
public void testEndConnectionEventWhenGroupRemoval() throws Exception {
forceP2pEnabled(mClient1);
when(mWifiNative.p2pGroupAdd(anyBoolean())).thenReturn(true);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
mockEnterGroupNegotiationState();
verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"),
anyInt(), eq(false));
sendSimpleMsg(null, WifiP2pMonitor.P2P_GROUP_REMOVED_EVENT);
verify(mContext).sendBroadcastWithMultiplePermissions(
argThat(new WifiP2pServiceImplTest
.P2pConnectionChangedIntentMatcherForNetworkState(FAILED)), any());
verify(mWifiP2pMetrics).endConnectionEvent(
eq(P2pConnectionEvent.CLF_UNKNOWN));
}
/**
* Verify the connection event ends due to the invitation failure.
*/
@Test
public void testEndConnectionEventWhenInvitationFailure() throws Exception {
forceP2pEnabled(mClient1);
when(mWifiNative.p2pGroupAdd(anyBoolean())).thenReturn(true);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
mockEnterGroupNegotiationState();
verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"),
anyInt(), eq(false));
sendInvitationResultMsg(WifiP2pServiceImpl.P2pStatus.UNKNOWN);
verify(mWifiP2pMetrics).endConnectionEvent(
eq(P2pConnectionEvent.CLF_INVITATION_FAIL));
}
/**
* Verify WifiP2pManager.RESPONSE_DEVICE_INFO is returned with null object when a caller
* without proper permission attempts.
*/
@Test
public void testRequestDeviceInfoFailureWhenPermissionDenied() throws Exception {
forceP2pEnabled(mClient1);
doNothing().when(mWifiPermissionsUtil).checkPackage(anyInt(), anyString());
when(mWifiPermissionsUtil.checkCanAccessWifiDirect(anyString(), anyString(), anyInt(),
anyBoolean())).thenReturn(false);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_DEVICE_INFO);
verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"),
anyInt(), eq(false));
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
assertEquals(WifiP2pManager.RESPONSE_DEVICE_INFO, mMessageCaptor.getValue().what);
assertNull(mMessageCaptor.getValue().obj);
}
/**
* Verify WifiP2pManager.RESPONSE_DEVICE_INFO is returned with expect object when a caller
* with proper permission attempts in p2p enabled state.
*/
@Test
public void testRequestDeviceInfoSuccessWhenP2pEnabled() throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_DEVICE_INFO);
verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"),
anyInt(), eq(false));
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
assertEquals(WifiP2pManager.RESPONSE_DEVICE_INFO, mMessageCaptor.getValue().what);
WifiP2pDevice wifiP2pDevice = (WifiP2pDevice) mMessageCaptor.getValue().obj;
assertEquals(ANONYMIZED_DEVICE_ADDRESS, wifiP2pDevice.deviceAddress);
assertEquals(thisDeviceName, wifiP2pDevice.deviceName);
}
/**
* Verify WifiP2pManager.RESPONSE_DEVICE_INFO is returned with empty object when a caller
* with proper permission attempts in p2p disabled state.
*/
@Test
public void testRequestDeviceInfoReturnEmptyWifiP2pDeviceWhenP2pDisabled() throws Exception {
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_DEVICE_INFO);
verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"),
anyInt(), eq(false));
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
assertEquals(WifiP2pManager.RESPONSE_DEVICE_INFO, mMessageCaptor.getValue().what);
WifiP2pDevice wifiP2pDevice = (WifiP2pDevice) mMessageCaptor.getValue().obj;
assertEquals("", wifiP2pDevice.deviceAddress);
assertEquals("", wifiP2pDevice.deviceName);
}
/**
* Verify WifiP2pManager.RESPONSE_DEVICE_INFO returns an object with the actual device MAC when
* the caller holds the LOCAL_MAC_ADDRESS permission.
*/
@Test
public void testRequestDeviceInfoReturnsActualMacForNetworkSettingsApp() throws Exception {
forceP2pEnabled(mClient1);
when(mWifiPermissionsUtil.checkLocalMacAddressPermission(anyInt())).thenReturn(true);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_DEVICE_INFO);
verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"),
anyInt(), eq(false));
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
assertEquals(WifiP2pManager.RESPONSE_DEVICE_INFO, mMessageCaptor.getValue().what);
WifiP2pDevice wifiP2pDevice = (WifiP2pDevice) mMessageCaptor.getValue().obj;
assertEquals(thisDeviceMac, wifiP2pDevice.deviceAddress);
assertEquals(thisDeviceName, wifiP2pDevice.deviceName);
}
private void verifyCustomizeDefaultDeviceName(String expectedName, boolean isRandomPostfix)
throws Exception {
forceP2pEnabled(mClient1);
when(mWifiPermissionsUtil.checkLocalMacAddressPermission(anyInt())).thenReturn(true);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_DEVICE_INFO);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
assertEquals(WifiP2pManager.RESPONSE_DEVICE_INFO, mMessageCaptor.getValue().what);
WifiP2pDevice wifiP2pDevice = (WifiP2pDevice) mMessageCaptor.getValue().obj;
if (isRandomPostfix) {
assertEquals(expectedName,
wifiP2pDevice.deviceName.substring(0, expectedName.length()));
} else {
assertEquals(expectedName, wifiP2pDevice.deviceName);
}
}
private void setupDefaultDeviceNameCustomization(
String prefix, int postfixDigit) {
when(mWifiSettingsConfigStore.get(eq(WIFI_P2P_DEVICE_NAME))).thenReturn(null);
when(mFrameworkFacade.getSecureStringSetting(any(), eq(Settings.Secure.ANDROID_ID)))
.thenReturn(TEST_ANDROID_ID);
when(mWifiGlobals.getWifiP2pDeviceNamePrefix()).thenReturn(prefix);
when(mWifiGlobals.getWifiP2pDeviceNamePostfixNumDigits()).thenReturn(postfixDigit);
}
/** Verify that the default device name is customized by overlay. */
@Test
public void testCustomizeDefaultDeviceName() throws Exception {
setupDefaultDeviceNameCustomization("Niceboat-", -1);
verifyCustomizeDefaultDeviceName("Niceboat-" + TEST_ANDROID_ID.substring(0, 4), false);
}
/** Verify that the prefix fallback to Android_ if the prefix is too long. */
@Test
public void testCustomizeDefaultDeviceNameTooLongPrefix() throws Exception {
setupDefaultDeviceNameCustomization(
StringUtil.generateRandomNumberString(
WifiP2pServiceImpl.DEVICE_NAME_PREFIX_LENGTH_MAX + 1), 4);
verifyCustomizeDefaultDeviceName(WifiP2pServiceImpl.DEFAULT_DEVICE_NAME_PREFIX, true);
}
/** Verify that the prefix fallback to Android_ if the prefix is empty. */
@Test
public void testCustomizeDefaultDeviceNameEmptyPrefix() throws Exception {
setupDefaultDeviceNameCustomization("", 6);
verifyCustomizeDefaultDeviceName(WifiP2pServiceImpl.DEFAULT_DEVICE_NAME_PREFIX, true);
}
/** Verify that the postfix fallbacks to 4-digit ANDROID_ID if the length is smaller than 4. */
@Test
public void testCustomizeDefaultDeviceNamePostfixTooShort() throws Exception {
setupDefaultDeviceNameCustomization("Prefix",
WifiP2pServiceImpl.DEVICE_NAME_POSTFIX_LENGTH_MIN - 1);
verifyCustomizeDefaultDeviceName("Prefix" + TEST_ANDROID_ID.substring(0, 4), true);
}
/** Verify that the postfix fallbacks to 4-digit ANDROID_ID if the length is 0.*/
@Test
public void testCustomizeDefaultDeviceNamePostfixIsZeroLength() throws Exception {
setupDefaultDeviceNameCustomization("Prefix", 0);
verifyCustomizeDefaultDeviceName("Prefix" + TEST_ANDROID_ID.substring(0, 4), true);
}
/** Verify that the digit length exceeds the remaining bytes. */
@Test
public void testCustomizeDefaultDeviceNameWithFewerRemainingBytes() throws Exception {
int postfixLength = 6;
String prefix = StringUtil.generateRandomNumberString(
WifiP2pServiceImpl.DEVICE_NAME_LENGTH_MAX - postfixLength + 1);
setupDefaultDeviceNameCustomization(prefix, postfixLength);
verifyCustomizeDefaultDeviceName(prefix, true);
}
/**
* Verify the caller sends WifiP2pManager.STOP_DISCOVERY.
*/
@Test
public void testStopDiscoverySuccess() throws Exception {
when(mWifiNative.p2pStopFind()).thenReturn(true);
forceP2pEnabled(mClient1);
sendSimpleMsg(mClientMessenger, WifiP2pManager.STOP_DISCOVERY);
verify(mWifiNative).p2pStopFind();
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.STOP_DISCOVERY_SUCCEEDED, message.what);
}
/**
* Verify WifiP2pManager.STOP_DISCOVERY_FAILED is returned when native call failure.
*/
@Test
public void testStopDiscoveryFailureWhenNativeCallFailure() throws Exception {
when(mWifiNative.p2pStopFind()).thenReturn(false);
forceP2pEnabled(mClient1);
sendSimpleMsg(mClientMessenger, WifiP2pManager.STOP_DISCOVERY);
verify(mWifiNative).p2pStopFind();
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.STOP_DISCOVERY_FAILED, message.what);
assertEquals(WifiP2pManager.ERROR, message.arg1);
}
/**
* Verify WifiP2pManager.STOP_DISCOVERY_FAILED is returned when p2p is disabled.
*/
@Test
public void testStopDiscoveryFailureWhenP2pDisabled() throws Exception {
sendSimpleMsg(mClientMessenger, WifiP2pManager.STOP_DISCOVERY);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.STOP_DISCOVERY_FAILED, message.what);
assertEquals(WifiP2pManager.BUSY, message.arg1);
}
/**
* Verify WifiP2pManager.STOP_DISCOVERY_FAILED is returned when p2p is unsupported.
*/
@Test
public void testStopDiscoveryFailureWhenP2pUnsupported() throws Exception {
setUpWifiP2pServiceImpl(false);
sendSimpleMsg(mClientMessenger, WifiP2pManager.STOP_DISCOVERY);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.STOP_DISCOVERY_FAILED, message.what);
assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1);
}
/**
* Verify the caller sends WifiP2pManager.CANCEL_CONNECT.
*/
@Test
public void testCancelConnectSuccess() throws Exception {
// Move to group creating state
testConnectWithConfigValidAsGroupSuccess();
sendSimpleMsg(mClientMessenger, WifiP2pManager.CANCEL_CONNECT);
verify(mClientHandler, atLeastOnce()).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.CANCEL_CONNECT_SUCCEEDED, message.what);
}
/**
* Verify WifiP2pManager.CANCEL_CONNECT_FAILED is returned when p2p is inactive.
*/
@Test
public void testCancelConnectFailureWhenP2pInactive() throws Exception {
// Move to inactive state
forceP2pEnabled(mClient1);
sendSimpleMsg(mClientMessenger, WifiP2pManager.CANCEL_CONNECT);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.CANCEL_CONNECT_FAILED, message.what);
assertEquals(WifiP2pManager.BUSY, message.arg1);
}
/**
* Verify WifiP2pManager.CANCEL_CONNECT_FAILED is returned when p2p is disabled.
*/
@Test
public void testCancelConnectFailureWhenP2pDisabled() throws Exception {
sendSimpleMsg(mClientMessenger, WifiP2pManager.CANCEL_CONNECT);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.CANCEL_CONNECT_FAILED, message.what);
assertEquals(WifiP2pManager.BUSY, message.arg1);
}
/**
* Verify WifiP2pManager.CANCEL_CONNECT_FAILED is returned when p2p is unsupported.
*/
@Test
public void testCancelConnectFailureWhenP2pUnsupported() throws Exception {
setUpWifiP2pServiceImpl(false);
sendSimpleMsg(mClientMessenger, WifiP2pManager.CANCEL_CONNECT);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.CANCEL_CONNECT_FAILED, message.what);
assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1);
}
/**
* Verify the caller sends WifiP2pManager.REMOVE_GROUP.
*/
@Test
public void testRemoveGroupSuccess() throws Exception {
// Move to group created state
forceP2pEnabled(mClient1);
WifiP2pGroup group = new WifiP2pGroup();
group.setNetworkId(WifiP2pGroup.NETWORK_ID_PERSISTENT);
group.setNetworkName("DIRECT-xy-NEW");
group.setOwner(new WifiP2pDevice("thisDeviceMac"));
group.setIsGroupOwner(true);
group.setInterface(IFACE_NAME_P2P);
sendGroupStartedMsg(group);
simulateTetherReady();
when(mWifiNative.p2pGroupRemove(eq(IFACE_NAME_P2P))).thenReturn(true);
sendSimpleMsg(mClientMessenger, WifiP2pManager.REMOVE_GROUP);
verify(mWifiNative).p2pGroupRemove(eq(IFACE_NAME_P2P));
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.REMOVE_GROUP_SUCCEEDED, message.what);
}
/**
* Verify WifiP2pManager.REMOVE_GROUP_FAILED is returned when native call failure.
*/
@Test
public void testRemoveGroupFailureWhenNativeCallFailure() throws Exception {
// Move to group created state
forceP2pEnabled(mClient1);
WifiP2pGroup group = new WifiP2pGroup();
group.setNetworkId(WifiP2pGroup.NETWORK_ID_PERSISTENT);
group.setNetworkName("DIRECT-xy-NEW");
group.setOwner(new WifiP2pDevice("thisDeviceMac"));
group.setIsGroupOwner(true);
group.setInterface(IFACE_NAME_P2P);
sendGroupStartedMsg(group);
simulateTetherReady();
when(mWifiNative.p2pGroupRemove(eq(IFACE_NAME_P2P))).thenReturn(false);
sendSimpleMsg(mClientMessenger, WifiP2pManager.REMOVE_GROUP);
verify(mWifiNative).p2pGroupRemove(eq(IFACE_NAME_P2P));
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.REMOVE_GROUP_FAILED, message.what);
assertEquals(WifiP2pManager.ERROR, message.arg1);
}
/**
* Verify WifiP2pManager.REMOVE_GROUP_FAILED is returned when p2p is creating group.
*/
@Test
public void testRemoveGroupFailureWhenP2pCreatingGroup() throws Exception {
// Move to group creating state
testConnectWithConfigValidAsGroupSuccess();
sendSimpleMsg(mClientMessenger, WifiP2pManager.REMOVE_GROUP);
verify(mClientHandler, atLeastOnce()).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.REMOVE_GROUP_FAILED, message.what);
assertEquals(WifiP2pManager.BUSY, message.arg1);
}
/**
* Verify WifiP2pManager.REMOVE_GROUP_FAILED is returned when p2p is inactive.
*/
@Test
public void testRemoveGroupFailureWhenP2pInactive() throws Exception {
// Move to inactive state
forceP2pEnabled(mClient1);
sendSimpleMsg(mClientMessenger, WifiP2pManager.REMOVE_GROUP);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.REMOVE_GROUP_FAILED, message.what);
assertEquals(WifiP2pManager.BUSY, message.arg1);
}
/**
* Verify WifiP2pManager.REMOVE_GROUP_FAILED is returned when p2p is disabled.
*/
@Test
public void testRemoveGroupFailureWhenP2pDisabled() throws Exception {
sendSimpleMsg(mClientMessenger, WifiP2pManager.REMOVE_GROUP);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.REMOVE_GROUP_FAILED, message.what);
assertEquals(WifiP2pManager.BUSY, message.arg1);
}
/**
* Verify WifiP2pManager.REMOVE_GROUP_FAILED is returned when p2p is unsupported.
*/
@Test
public void testRemoveGroupFailureWhenP2pUnsupported() throws Exception {
setUpWifiP2pServiceImpl(false);
sendSimpleMsg(mClientMessenger, WifiP2pManager.REMOVE_GROUP);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.REMOVE_GROUP_FAILED, message.what);
assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1);
}
/**
* Verify the caller sends WifiP2pManager.SET_CHANNEL.
*/
@Test
public void testSetChannelSuccess() throws Exception {
// Move to enabled state
forceP2pEnabled(mClient1);
Bundle p2pChannels = new Bundle();
p2pChannels.putInt("lc", 1);
p2pChannels.putInt("oc", 2);
when(mWifiNative.p2pSetListenChannel(anyInt())).thenReturn(true);
when(mWifiNative.p2pSetOperatingChannel(anyInt(), any())).thenReturn(true);
sendSetChannelMsg(mClientMessenger, p2pChannels);
verify(mWifiNative).p2pSetListenChannel(eq(1));
verify(mWifiNative).p2pSetOperatingChannel(eq(2), any());
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_CHANNEL_SUCCEEDED, message.what);
}
/**
* Verify WifiP2pManager.SET_CHANNEL_FAILED is returned when native call failure.
*/
@Test
public void testSetChannelFailureWhenNativeCallSetListenChannelFailure() throws Exception {
// Move to enabled state
forceP2pEnabled(mClient1);
Bundle p2pChannels = new Bundle();
p2pChannels.putInt("lc", 1);
p2pChannels.putInt("oc", 2);
when(mWifiNative.p2pSetListenChannel(anyInt())).thenReturn(false);
when(mWifiNative.p2pSetOperatingChannel(anyInt(), any())).thenReturn(true);
sendSetChannelMsg(mClientMessenger, p2pChannels);
verify(mWifiNative).p2pSetListenChannel(eq(1));
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_CHANNEL_FAILED, message.what);
}
/**
* Verify WifiP2pManager.SET_CHANNEL_FAILED is returned when native call failure.
*/
@Test
public void testSetChannelFailureWhenNativeCallSetOperatingChannelFailure() throws Exception {
// Move to enabled state
forceP2pEnabled(mClient1);
Bundle p2pChannels = new Bundle();
p2pChannels.putInt("lc", 1);
p2pChannels.putInt("oc", 2);
when(mWifiNative.p2pSetListenChannel(anyInt())).thenReturn(true);
when(mWifiNative.p2pSetOperatingChannel(anyInt(), any())).thenReturn(false);
sendSetChannelMsg(mClientMessenger, p2pChannels);
verify(mWifiNative).p2pSetListenChannel(eq(1));
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_CHANNEL_FAILED, message.what);
}
/**
* Verify WifiP2pManager.SET_CHANNEL_FAILED is returned when no permissions are held.
*/
@Test
public void testSetChannelFailureWhenNoPermissions() throws Exception {
// Move to enabled state
forceP2pEnabled(mClient1);
// no permissions held
when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(false);
when(mWifiPermissionsUtil.checkNetworkStackPermission(anyInt())).thenReturn(false);
when(mWifiPermissionsUtil.checkConfigOverridePermission(anyInt())).thenReturn(false);
Bundle p2pChannels = new Bundle();
p2pChannels.putInt("lc", 1);
p2pChannels.putInt("oc", 2);
sendSetChannelMsg(mClientMessenger, p2pChannels);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_CHANNEL_FAILED, message.what);
assertEquals(WifiP2pManager.ERROR, message.arg1);
}
/**
* Verify p2pSetListenChannel doesn't been called when message contain null object.
*/
@Test
public void testSetChannelFailureWhenObjectIsNull() throws Exception {
when(mWifiNative.p2pSetListenChannel(anyInt())).thenReturn(true);
when(mWifiNative.p2pSetOperatingChannel(anyInt(), any())).thenReturn(true);
// Move to enabled state
forceP2pEnabled(mClient1);
when(mWifiNative.p2pSetListenChannel(anyInt())).thenReturn(false);
when(mWifiNative.p2pSetOperatingChannel(anyInt(), any())).thenReturn(true);
sendSetChannelMsg(mClientMessenger, null);
// Should be called only once on entering P2pEnabledState.
verify(mWifiNative, times(1)).p2pSetListenChannel(anyInt());
verify(mWifiNative, times(1)).p2pSetOperatingChannel(anyInt(), any());
}
/**
* Verify the caller sends WifiP2pManager.START_WPS with push button configuration.
*/
@Test
public void testStartWpsWithPbcSuccess() throws Exception {
// Move to group created state
forceP2pEnabled(mClient1);
WifiP2pGroup group = new WifiP2pGroup();
group.setNetworkId(WifiP2pGroup.NETWORK_ID_PERSISTENT);
group.setNetworkName("DIRECT-xy-NEW");
group.setOwner(new WifiP2pDevice("thisDeviceMac"));
group.setIsGroupOwner(true);
group.setInterface(IFACE_NAME_P2P);
sendGroupStartedMsg(group);
simulateTetherReady();
when(mWifiNative.startWpsPbc(anyString(), any())).thenReturn(true);
WpsInfo wps = new WpsInfo();
wps.setup = WpsInfo.PBC;
sendStartWpsMsg(mClientMessenger, wps);
verify(mWifiNative).startWpsPbc(eq(IFACE_NAME_P2P), isNull());
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.START_WPS_SUCCEEDED, message.what);
}
/**
* Verify the caller sends WifiP2pManager.START_WPS with pin display.
*/
@Test
public void testStartWpsWithPinDisplaySuccess() throws Exception {
// TODO(hsiuchangchen): This test item is related to UI.
}
/**
* Verify the caller sends WifiP2pManager.START_WPS with pin keypad.
*/
@Test
public void testStartWpsWithPinKeypadSuccess() throws Exception {
// Move to group created state
forceP2pEnabled(mClient1);
WifiP2pGroup group = new WifiP2pGroup();
group.setNetworkId(WifiP2pGroup.NETWORK_ID_PERSISTENT);
group.setNetworkName("DIRECT-xy-NEW");
group.setOwner(new WifiP2pDevice("thisDeviceMac"));
group.setIsGroupOwner(true);
group.setInterface(IFACE_NAME_P2P);
sendGroupStartedMsg(group);
simulateTetherReady();
when(mWifiNative.startWpsPinKeypad(anyString(), anyString())).thenReturn(true);
WpsInfo wps = new WpsInfo();
wps.setup = WpsInfo.KEYPAD;
wps.pin = "1234";
sendStartWpsMsg(mClientMessenger, wps);
verify(mWifiNative).startWpsPinKeypad(eq(IFACE_NAME_P2P), eq("1234"));
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.START_WPS_SUCCEEDED, message.what);
}
/**
* Verify WifiP2pManager.START_WPS_FAILED is returned when message contain null object.
*/
@Test
public void testStartWpsFailureWhenObjectIsNull() throws Exception {
// Move to group created state
forceP2pEnabled(mClient1);
WifiP2pGroup group = new WifiP2pGroup();
group.setNetworkId(WifiP2pGroup.NETWORK_ID_PERSISTENT);
group.setNetworkName("DIRECT-xy-NEW");
group.setOwner(new WifiP2pDevice("thisDeviceMac"));
group.setIsGroupOwner(true);
group.setInterface(IFACE_NAME_P2P);
sendGroupStartedMsg(group);
simulateTetherReady();
WpsInfo wps = null;
sendStartWpsMsg(mClientMessenger, wps);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.START_WPS_FAILED, message.what);
}
/**
* Verify WifiP2pManager.START_WPS_FAILED is returned when native call failure with
* push button configuration.
*/
@Test
public void testStartWpsWithPbcFailureWhenNativeCallFailure() throws Exception {
// Move to group created state
forceP2pEnabled(mClient1);
WifiP2pGroup group = new WifiP2pGroup();
group.setNetworkId(WifiP2pGroup.NETWORK_ID_PERSISTENT);
group.setNetworkName("DIRECT-xy-NEW");
group.setOwner(new WifiP2pDevice("thisDeviceMac"));
group.setIsGroupOwner(true);
group.setInterface(IFACE_NAME_P2P);
sendGroupStartedMsg(group);
simulateTetherReady();
when(mWifiNative.startWpsPbc(anyString(), any())).thenReturn(false);
WpsInfo wps = new WpsInfo();
wps.setup = WpsInfo.PBC;
sendStartWpsMsg(mClientMessenger, wps);
verify(mWifiNative).startWpsPbc(eq(IFACE_NAME_P2P), isNull());
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.START_WPS_FAILED, message.what);
}
/**
* Verify WifiP2pManager.START_WPS_FAILED is returned when native call failure with
* pin display.
*/
@Test
public void testStartWpsWithPinDisplayFailureWhenNativeCallFailure() throws Exception {
// Move to group created state
forceP2pEnabled(mClient1);
WifiP2pGroup group = new WifiP2pGroup();
group.setNetworkId(WifiP2pGroup.NETWORK_ID_PERSISTENT);
group.setNetworkName("DIRECT-xy-NEW");
group.setOwner(new WifiP2pDevice("thisDeviceMac"));
group.setIsGroupOwner(true);
group.setInterface(IFACE_NAME_P2P);
sendGroupStartedMsg(group);
simulateTetherReady();
when(mWifiNative.startWpsPinDisplay(anyString(), any())).thenReturn("abcd");
WpsInfo wps = new WpsInfo();
wps.setup = WpsInfo.DISPLAY;
sendStartWpsMsg(mClientMessenger, wps);
verify(mWifiNative).startWpsPinDisplay(eq(IFACE_NAME_P2P), isNull());
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.START_WPS_FAILED, message.what);
}
/**
* Verify WifiP2pManager.START_WPS_FAILED is returned when native call failure with
* pin keypad.
*/
@Test
public void testStartWpsWithPinKeypadFailureWhenNativeCallFailure() throws Exception {
// Move to group created state
forceP2pEnabled(mClient1);
WifiP2pGroup group = new WifiP2pGroup();
group.setNetworkId(WifiP2pGroup.NETWORK_ID_PERSISTENT);
group.setNetworkName("DIRECT-xy-NEW");
group.setOwner(new WifiP2pDevice("thisDeviceMac"));
group.setIsGroupOwner(true);
group.setInterface(IFACE_NAME_P2P);
sendGroupStartedMsg(group);
simulateTetherReady();
when(mWifiNative.startWpsPinKeypad(anyString(), anyString())).thenReturn(false);
WpsInfo wps = new WpsInfo();
wps.setup = WpsInfo.KEYPAD;
wps.pin = "1234";
sendStartWpsMsg(mClientMessenger, wps);
verify(mWifiNative).startWpsPinKeypad(eq(IFACE_NAME_P2P), eq("1234"));
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.START_WPS_FAILED, message.what);
}
/**
* Verify WifiP2pManager.START_WPS_FAILED is returned when p2p is inactive.
*/
@Test
public void testStartWpsFailureWhenP2pInactive() throws Exception {
// Move to inactive state
forceP2pEnabled(mClient1);
WpsInfo wps = new WpsInfo();
sendStartWpsMsg(mClientMessenger, wps);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.START_WPS_FAILED, message.what);
assertEquals(WifiP2pManager.BUSY, message.arg1);
}
/**
* Verify WifiP2pManager.START_WPS_FAILED is returned when p2p is disabled.
*/
@Test
public void testStartWpsFailureWhenP2pDisabled() throws Exception {
WpsInfo wps = new WpsInfo();
sendStartWpsMsg(mClientMessenger, wps);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.START_WPS_FAILED, message.what);
assertEquals(WifiP2pManager.BUSY, message.arg1);
}
/**
* Verify WifiP2pManager.START_WPS_FAILED is returned when p2p is unsupported.
*/
@Test
public void testStartWpsFailureWhenP2pUnsupported() throws Exception {
setUpWifiP2pServiceImpl(false);
WpsInfo wps = new WpsInfo();
sendStartWpsMsg(mClientMessenger, wps);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.START_WPS_FAILED, message.what);
assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1);
}
/**
* Verify the caller sends WifiP2pManager.SET_DEVICE_NAME.
*/
@Test
public void testSetDeviceNameSuccess() throws Exception {
// Move to enabled state
forceP2pEnabled(mClient1);
mTestThisDevice.status = mTestThisDevice.AVAILABLE;
mTestThisDevice.deviceName = "another-name";
when(mWifiNative.setDeviceName(anyString())).thenReturn(true);
sendSetDeviceNameMsg(mClientMessenger, mTestThisDevice);
verify(mWifiNative).setDeviceName(eq(mTestThisDevice.deviceName));
verify(mWifiNative).setP2pSsidPostfix(eq("-" + mTestThisDevice.deviceName));
verify(mWifiSettingsConfigStore).put(
eq(WIFI_P2P_DEVICE_NAME), eq(mTestThisDevice.deviceName));
checkSendThisDeviceChangedBroadcast();
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_DEVICE_NAME_SUCCEEDED, message.what);
}
/**
* Verify WifiP2pManager.SET_DEVICE_NAME_FAILED is returned when native call failed.
*/
@Test
public void testSetDeviceNameFailureWhenNativeCallFailure() throws Exception {
// Move to enabled state
forceP2pEnabled(mClient1);
when(mWifiNative.setDeviceName(anyString())).thenReturn(false);
sendSetDeviceNameMsg(mClientMessenger, mTestThisDevice);
verify(mWifiNative).setDeviceName(eq(mTestThisDevice.deviceName));
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_DEVICE_NAME_FAILED, message.what);
assertEquals(WifiP2pManager.ERROR, message.arg1);
}
/**
* Verify WifiP2pManager.SET_DEVICE_NAME_FAILED is returned when p2p device is null.
*/
@Test
public void testSetDeviceNameFailureWhenDeviceIsNull() throws Exception {
// Move to enabled state
forceP2pEnabled(mClient1);
sendSetDeviceNameMsg(mClientMessenger, null);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_DEVICE_NAME_FAILED, message.what);
assertEquals(WifiP2pManager.ERROR, message.arg1);
}
/**
* Verify WifiP2pManager.SET_DEVICE_NAME_FAILED is returned when p2p device's name is null.
*/
@Test
public void testSetDeviceNameFailureWhenDeviceNameIsNull() throws Exception {
// Move to enabled state
forceP2pEnabled(mClient1);
mTestThisDevice.deviceName = null;
sendSetDeviceNameMsg(mClientMessenger, mTestThisDevice);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_DEVICE_NAME_FAILED, message.what);
assertEquals(WifiP2pManager.ERROR, message.arg1);
}
/**
* Verify WifiP2pManager.SET_DEVICE_NAME_FAILED is returned when p2p is disabled.
*/
@Test
public void testSetDeviceNameFailureWhenP2pDisabled() throws Exception {
sendSetDeviceNameMsg(mClientMessenger, mTestThisDevice);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_DEVICE_NAME_FAILED, message.what);
assertEquals(WifiP2pManager.BUSY, message.arg1);
}
/**
* Verify WifiP2pManager.SET_DEVICE_NAME_FAILED is returned when p2p is unsupported.
*/
@Test
public void testSetDeviceNameFailureWhenP2pUnsupported() throws Exception {
setUpWifiP2pServiceImpl(false);
sendSetDeviceNameMsg(mClientMessenger, mTestThisDevice);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_DEVICE_NAME_FAILED, message.what);
assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1);
}
/**
* Verify WifiP2pManager.SET_DEVICE_NAME_FAILED is returned when no permissions are held.
*/
@Test
public void testSetDeviceNameFailureWhenNoPermissions() throws Exception {
// Move to enabled state
forceP2pEnabled(mClient1);
// no permissions held
when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(false);
when(mWifiPermissionsUtil.checkNetworkStackPermission(anyInt())).thenReturn(false);
when(mWifiPermissionsUtil.checkConfigOverridePermission(anyInt())).thenReturn(false);
sendSetDeviceNameMsg(mClientMessenger, null);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_DEVICE_NAME_FAILED, message.what);
assertEquals(WifiP2pManager.ERROR, message.arg1);
}
/**
* Verify the caller sends WifiP2pManager.SET_WFD_INFO with wfd enabled.
*/
@Test
public void testSetWfdInfoSuccessWithWfdEnabled() throws Exception {
// Move to enabled state
forceP2pEnabled(mClient1);
mTestThisDevice.status = mTestThisDevice.AVAILABLE;
mTestThisDevice.wfdInfo = new WifiP2pWfdInfo();
mTestThisDevice.wfdInfo.setEnabled(true);
when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper);
when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt()))
.thenReturn(PackageManager.PERMISSION_GRANTED);
when(mWifiNative.setWfdEnable(anyBoolean())).thenReturn(true);
when(mWifiNative.setWfdDeviceInfo(anyString())).thenReturn(true);
sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo);
verify(mWifiInjector).getWifiPermissionsWrapper();
verify(mWifiPermissionsWrapper).getUidPermission(
eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt());
verify(mWifiNative).setWfdEnable(eq(true));
verify(mWifiNative).setWfdDeviceInfo(eq(mTestThisDevice.wfdInfo.getDeviceInfoHex()));
checkSendThisDeviceChangedBroadcast();
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_WFD_INFO_SUCCEEDED, message.what);
}
/**
* Verify the caller sends WifiP2pManager.SET_WFD_INFO with wfd is disabled.
*/
@Test
public void testSetWfdInfoSuccessWithWfdDisabled() throws Exception {
// Move to enabled state
forceP2pEnabled(mClient1);
mTestThisDevice.status = mTestThisDevice.AVAILABLE;
mTestThisDevice.wfdInfo = new WifiP2pWfdInfo();
mTestThisDevice.wfdInfo.setEnabled(false);
when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper);
when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt()))
.thenReturn(PackageManager.PERMISSION_GRANTED);
when(mWifiNative.setWfdEnable(anyBoolean())).thenReturn(true);
sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo);
verify(mWifiInjector).getWifiPermissionsWrapper();
verify(mWifiPermissionsWrapper).getUidPermission(
eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt());
verify(mWifiNative).setWfdEnable(eq(false));
checkSendThisDeviceChangedBroadcast();
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_WFD_INFO_SUCCEEDED, message.what);
}
/**
* Verify WifiP2pManager.SET_WFD_INFO_FAILED is returned when wfd permission denied.
*/
@Test
public void testSetWfdInfoFailureWhenWfdPermissionDenied() throws Exception {
// Move to enabled state
forceP2pEnabled(mClient1);
mTestThisDevice.status = mTestThisDevice.AVAILABLE;
mTestThisDevice.wfdInfo = new WifiP2pWfdInfo();
when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper);
when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt()))
.thenReturn(PackageManager.PERMISSION_DENIED);
sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo);
verify(mWifiInjector).getWifiPermissionsWrapper();
verify(mWifiPermissionsWrapper).getUidPermission(
eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt());
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_WFD_INFO_FAILED, message.what);
assertEquals(WifiP2pManager.ERROR, message.arg1);
}
/**
* Verify WifiP2pManager.SET_WFD_INFO_FAILED is returned when wfdInfo is null.
*/
@Test
public void testSetWfdInfoFailureWhenWfdInfoIsNull() throws Exception {
// Move to enabled state
forceP2pEnabled(mClient1);
mTestThisDevice.status = mTestThisDevice.AVAILABLE;
mTestThisDevice.wfdInfo = null;
when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper);
when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt()))
.thenReturn(PackageManager.PERMISSION_GRANTED);
sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo);
verify(mWifiInjector).getWifiPermissionsWrapper();
verify(mWifiPermissionsWrapper).getUidPermission(
eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt());
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_WFD_INFO_FAILED, message.what);
assertEquals(WifiP2pManager.ERROR, message.arg1);
}
/**
* Verify WifiP2pManager.SET_WFD_INFO_FAILED is returned when wfd is enabled
* and native call "setWfdEnable" failure.
*/
@Test
public void testSetWfdInfoFailureWithWfdEnabledWhenNativeCallFailure1() throws Exception {
// Move to enabled state
forceP2pEnabled(mClient1);
mTestThisDevice.status = mTestThisDevice.AVAILABLE;
mTestThisDevice.wfdInfo = new WifiP2pWfdInfo();
mTestThisDevice.wfdInfo.setEnabled(true);
when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper);
when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt()))
.thenReturn(PackageManager.PERMISSION_GRANTED);
when(mWifiNative.setWfdEnable(anyBoolean())).thenReturn(false);
sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo);
verify(mWifiInjector).getWifiPermissionsWrapper();
verify(mWifiPermissionsWrapper).getUidPermission(
eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt());
verify(mWifiNative).setWfdEnable(eq(true));
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_WFD_INFO_FAILED, message.what);
assertEquals(WifiP2pManager.ERROR, message.arg1);
}
/**
* Verify WifiP2pManager.SET_WFD_INFO_FAILED is returned when wfd is enabled
* and native call "setWfdDeviceInfo" failure.
*/
@Test
public void testSetWfdInfoFailureWithWfdEnabledWhenNativeCallFailure2() throws Exception {
// Move to enabled state
forceP2pEnabled(mClient1);
mTestThisDevice.status = mTestThisDevice.AVAILABLE;
mTestThisDevice.wfdInfo = new WifiP2pWfdInfo();
mTestThisDevice.wfdInfo.setEnabled(true);
when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper);
when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt()))
.thenReturn(PackageManager.PERMISSION_GRANTED);
when(mWifiNative.setWfdEnable(anyBoolean())).thenReturn(true);
when(mWifiNative.setWfdDeviceInfo(anyString())).thenReturn(false);
sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo);
verify(mWifiInjector).getWifiPermissionsWrapper();
verify(mWifiPermissionsWrapper).getUidPermission(
eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt());
verify(mWifiNative).setWfdEnable(eq(true));
verify(mWifiNative).setWfdDeviceInfo(eq(mTestThisDevice.wfdInfo.getDeviceInfoHex()));
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_WFD_INFO_FAILED, message.what);
assertEquals(WifiP2pManager.ERROR, message.arg1);
}
/**
* Verify WifiP2pManager.SET_WFD_INFO_FAILED is returned when wfd is disabled
* and native call failure.
*/
@Test
public void testSetWfdInfoFailureWithWfdDisabledWhenNativeCallFailure() throws Exception {
// Move to enabled state
forceP2pEnabled(mClient1);
mTestThisDevice.status = mTestThisDevice.AVAILABLE;
mTestThisDevice.wfdInfo = new WifiP2pWfdInfo();
mTestThisDevice.wfdInfo.setEnabled(false);
when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper);
when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt()))
.thenReturn(PackageManager.PERMISSION_GRANTED);
when(mWifiNative.setWfdEnable(anyBoolean())).thenReturn(false);
sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo);
verify(mWifiInjector).getWifiPermissionsWrapper();
verify(mWifiPermissionsWrapper).getUidPermission(
eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt());
verify(mWifiNative).setWfdEnable(eq(false));
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_WFD_INFO_FAILED, message.what);
assertEquals(WifiP2pManager.ERROR, message.arg1);
}
/**
* Verify WifiP2pManager.SET_WFD_INFO_FAILED is returned when p2p is disabled.
*/
@Test
public void testSetWfdInfoFailureWhenP2pDisabled() throws Exception {
mTestThisDevice.wfdInfo = new WifiP2pWfdInfo();
when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper);
when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt()))
.thenReturn(PackageManager.PERMISSION_GRANTED);
sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo);
verify(mWifiInjector).getWifiPermissionsWrapper();
verify(mWifiPermissionsWrapper).getUidPermission(
eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt());
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_WFD_INFO_FAILED, message.what);
assertEquals(WifiP2pManager.BUSY, message.arg1);
}
/**
* Verify WifiP2pManager.SET_WFD_INFO_FAILED is returned when wfd permission denied
* and p2p is disabled.
*/
@Test
public void testSetWfdInfoFailureWhenWfdPermissionDeniedAndP2pDisabled() throws Exception {
mTestThisDevice.wfdInfo = new WifiP2pWfdInfo();
when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper);
when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt()))
.thenReturn(PackageManager.PERMISSION_DENIED);
sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo);
verify(mWifiInjector).getWifiPermissionsWrapper();
verify(mWifiPermissionsWrapper).getUidPermission(
eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt());
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_WFD_INFO_FAILED, message.what);
assertEquals(WifiP2pManager.ERROR, message.arg1);
}
/**
* Verify WifiP2pManager.SET_WFD_INFO_FAILED is returned when p2p is unsupported.
*/
@Test
public void testSetWfdInfoFailureWhenP2pUnsupported() throws Exception {
setUpWifiP2pServiceImpl(false);
mTestThisDevice.wfdInfo = new WifiP2pWfdInfo();
when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper);
when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt()))
.thenReturn(PackageManager.PERMISSION_GRANTED);
sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo);
verify(mWifiInjector).getWifiPermissionsWrapper();
verify(mWifiPermissionsWrapper).getUidPermission(
eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt());
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_WFD_INFO_FAILED, message.what);
assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1);
}
/**
* Verify WifiP2pManager.SET_WFD_INFO_FAILED is returned when wfd permission denied
* and p2p is unsupported.
*/
@Test
public void testSetWfdInfoFailureWhenWfdPermissionDeniedAndP2pUnsupported() throws Exception {
setUpWifiP2pServiceImpl(false);
mTestThisDevice.wfdInfo = new WifiP2pWfdInfo();
when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper);
when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt()))
.thenReturn(PackageManager.PERMISSION_DENIED);
sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo);
verify(mWifiInjector).getWifiPermissionsWrapper();
verify(mWifiPermissionsWrapper).getUidPermission(
eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt());
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_WFD_INFO_FAILED, message.what);
assertEquals(WifiP2pManager.ERROR, message.arg1);
}
/**
* Verify the call setMiracastMode when p2p is enabled.
*/
@Test
public void testSetMiracastModeWhenP2pEnabled() throws Exception {
// Move to enabled state
forceP2pEnabled(mClient1);
when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper);
when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt()))
.thenReturn(PackageManager.PERMISSION_GRANTED);
mWifiP2pServiceImpl.setMiracastMode(0);
mLooper.dispatchAll();
verify(mWifiInjector).getWifiPermissionsWrapper();
verify(mWifiPermissionsWrapper).getUidPermission(
eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt());
verify(mWifiNative).setMiracastMode(eq(0));
}
/**
* Verify the call setMiracastMode when p2p is disable.
*/
@Test
public void testSetMiracastModeWhenP2pDisabled() throws Exception {
when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper);
when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt()))
.thenReturn(PackageManager.PERMISSION_GRANTED);
mWifiP2pServiceImpl.setMiracastMode(0);
mLooper.dispatchAll();
verify(mWifiInjector).getWifiPermissionsWrapper();
verify(mWifiPermissionsWrapper).getUidPermission(
eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt());
verify(mWifiNative, never()).setMiracastMode(anyInt());
}
/**
* Verify the call setMiracastMode when CONFIGURE_WIFI_DISPLAY permission denied.
*/
@Test(expected = SecurityException.class)
public void testSetMiracastModeWhenPermissionDeined() throws Exception {
when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper);
when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt()))
.thenReturn(PackageManager.PERMISSION_DENIED);
mWifiP2pServiceImpl.setMiracastMode(0);
verify(mWifiInjector).getWifiPermissionsWrapper();
verify(mWifiPermissionsWrapper).getUidPermission(
eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt());
verify(mWifiNative, never()).setMiracastMode(anyInt());
}
/**
* Verify the caller sends WifiP2pManager.FACTORY_RESET when p2p is enabled.
*/
@Test
public void testFactoryResetSuccessWhenP2pEnabled() throws Exception {
// Move to enabled state
forceP2pEnabled(mClient1);
when(mWifiInjector.getUserManager()).thenReturn(mUserManager);
when(mPackageManager.getNameForUid(anyInt())).thenReturn("testPkg");
when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(true);
when(mUserManager.hasUserRestrictionForUser(eq(UserManager.DISALLOW_NETWORK_RESET), any()))
.thenReturn(false);
when(mUserManager.hasUserRestrictionForUser(eq(UserManager.DISALLOW_CONFIG_WIFI), any()))
.thenReturn(false);
when(mWifiNative.p2pListNetworks(any())).thenReturn(true);
sendSimpleMsg(mClientMessenger, WifiP2pManager.FACTORY_RESET);
checkSendP2pPersistentGroupsChangedBroadcast();
verify(mWifiInjector).getUserManager();
verify(mPackageManager).getNameForUid(anyInt());
verify(mWifiPermissionsUtil).checkNetworkSettingsPermission(anyInt());
verify(mUserManager).hasUserRestrictionForUser(
eq(UserManager.DISALLOW_NETWORK_RESET), any());
verify(mUserManager).hasUserRestrictionForUser(eq(UserManager.DISALLOW_CONFIG_WIFI), any());
verify(mWifiNative, atLeastOnce()).p2pListNetworks(any());
verify(mWifiSettingsConfigStore).put(eq(WIFI_P2P_PENDING_FACTORY_RESET), eq(false));
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.FACTORY_RESET_SUCCEEDED, message.what);
}
/**
* Verify the caller sends WifiP2pManager.FACTORY_RESET when p2p is disabled at first
* and changes to enabled.
*/
@Test
public void testFactoryResetSuccessWhenP2pFromDisabledToEnabled() throws Exception {
when(mWifiInjector.getUserManager()).thenReturn(mUserManager);
when(mPackageManager.getNameForUid(anyInt())).thenReturn("testPkg");
when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(true);
when(mUserManager.hasUserRestrictionForUser(eq(UserManager.DISALLOW_NETWORK_RESET), any()))
.thenReturn(false);
when(mUserManager.hasUserRestrictionForUser(eq(UserManager.DISALLOW_CONFIG_WIFI), any()))
.thenReturn(false);
when(mWifiNative.p2pListNetworks(any())).thenReturn(true);
sendSimpleMsg(mClientMessenger, WifiP2pManager.FACTORY_RESET);
verify(mWifiInjector).getUserManager();
verify(mPackageManager).getNameForUid(anyInt());
verify(mWifiPermissionsUtil).checkNetworkSettingsPermission(anyInt());
verify(mUserManager).hasUserRestrictionForUser(
eq(UserManager.DISALLOW_NETWORK_RESET), any());
verify(mUserManager).hasUserRestrictionForUser(eq(UserManager.DISALLOW_CONFIG_WIFI), any());
verify(mWifiNative, never()).p2pListNetworks(any());
verify(mWifiSettingsConfigStore).put(eq(WIFI_P2P_PENDING_FACTORY_RESET), eq(true));
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.FACTORY_RESET_SUCCEEDED, message.what);
// Move to enabled state
when(mWifiSettingsConfigStore.get(eq(WIFI_P2P_PENDING_FACTORY_RESET))).thenReturn(true);
forceP2pEnabled(mClient1);
verify(mWifiInjector, times(2)).getUserManager();
verify(mPackageManager, times(2)).getNameForUid(anyInt());
verify(mWifiPermissionsUtil, times(2)).checkNetworkSettingsPermission(anyInt());
verify(mUserManager, times(2)).hasUserRestrictionForUser(
eq(UserManager.DISALLOW_NETWORK_RESET), any());
verify(mUserManager, times(2)).hasUserRestrictionForUser(
eq(UserManager.DISALLOW_CONFIG_WIFI), any());
verify(mWifiNative, atLeastOnce()).p2pListNetworks(any());
verify(mWifiSettingsConfigStore).get(eq(WIFI_P2P_PENDING_FACTORY_RESET));
verify(mWifiSettingsConfigStore).put(eq(WIFI_P2P_PENDING_FACTORY_RESET), eq(false));
checkSendP2pPersistentGroupsChangedBroadcast();
}
/**
* Verify WifiP2pManager.FACTORY_RESET_FAILED is returned without network setting permission.
*/
@Test
public void testFactoryResetFailureWithoutNetworkSettingPermission() throws Exception {
// Move to enabled state
forceP2pEnabled(mClient1);
when(mWifiInjector.getUserManager()).thenReturn(mUserManager);
when(mPackageManager.getNameForUid(anyInt())).thenReturn("testPkg");
when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(false);
sendSimpleMsg(mClientMessenger, WifiP2pManager.FACTORY_RESET);
verify(mWifiInjector).getUserManager();
verify(mPackageManager).getNameForUid(anyInt());
verify(mWifiPermissionsUtil).checkNetworkSettingsPermission(anyInt());
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.FACTORY_RESET_FAILED, message.what);
assertEquals(WifiP2pManager.ERROR, message.arg1);
}
/**
* Verify WifiP2pManager.FACTORY_RESET_FAILED is returned when network reset disallow.
*/
@Test
public void testFactoryResetFailureWhenNetworkResetDisallow() throws Exception {
// Move to enabled state
forceP2pEnabled(mClient1);
when(mWifiInjector.getUserManager()).thenReturn(mUserManager);
when(mPackageManager.getNameForUid(anyInt())).thenReturn("testPkg");
when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(true);
when(mUserManager.hasUserRestrictionForUser(eq(UserManager.DISALLOW_NETWORK_RESET), any()))
.thenReturn(true);
sendSimpleMsg(mClientMessenger, WifiP2pManager.FACTORY_RESET);
verify(mWifiInjector).getUserManager();
verify(mPackageManager).getNameForUid(anyInt());
verify(mWifiPermissionsUtil).checkNetworkSettingsPermission(anyInt());
verify(mUserManager).hasUserRestrictionForUser(
eq(UserManager.DISALLOW_NETWORK_RESET), any());
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.FACTORY_RESET_FAILED, message.what);
assertEquals(WifiP2pManager.ERROR, message.arg1);
}
/**
* Verify WifiP2pManager.FACTORY_RESET_FAILED is returned when config wifi disallow.
*/
@Test
public void testFactoryResetFailureWhenConfigWifiDisallow() throws Exception {
// Move to enabled state
forceP2pEnabled(mClient1);
when(mWifiInjector.getUserManager()).thenReturn(mUserManager);
when(mPackageManager.getNameForUid(anyInt())).thenReturn("testPkg");
when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(true);
when(mUserManager.hasUserRestrictionForUser(eq(UserManager.DISALLOW_NETWORK_RESET), any()))
.thenReturn(false);
when(mUserManager.hasUserRestrictionForUser(eq(UserManager.DISALLOW_CONFIG_WIFI), any()))
.thenReturn(true);
sendSimpleMsg(mClientMessenger, WifiP2pManager.FACTORY_RESET);
verify(mWifiInjector).getUserManager();
verify(mPackageManager).getNameForUid(anyInt());
verify(mWifiPermissionsUtil).checkNetworkSettingsPermission(anyInt());
verify(mUserManager).hasUserRestrictionForUser(
eq(UserManager.DISALLOW_NETWORK_RESET), any());
verify(mUserManager).hasUserRestrictionForUser(eq(UserManager.DISALLOW_CONFIG_WIFI), any());
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.FACTORY_RESET_FAILED, message.what);
assertEquals(WifiP2pManager.ERROR, message.arg1);
}
/**
* Verify WifiP2pManager.FACTORY_RESET_FAILED is returned when p2p is unsupported.
*/
@Test
public void testFactoryResetFailureWhenP2pUnsupported() throws Exception {
setUpWifiP2pServiceImpl(false);
sendSimpleMsg(mClientMessenger, WifiP2pManager.FACTORY_RESET);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.FACTORY_RESET_FAILED, message.what);
assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1);
}
/**
* Verify the caller sends WifiP2pManager.SET_ONGOING_PEER_CONFIG.
*/
@Test
public void testSetOngingPeerConfigSuccess() throws Exception {
forceP2pEnabled(mClient1);
mockPeersList();
WifiP2pConfig config = new WifiP2pConfig();
config.deviceAddress = mTestWifiP2pDevice.deviceAddress;
when(mWifiPermissionsUtil.checkNetworkStackPermission(anyInt())).thenReturn(true);
sendSetOngoingPeerConfigMsg(mClientMessenger, config);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_ONGOING_PEER_CONFIG_SUCCEEDED, message.what);
}
/**
* Verify WifiP2pManager.SET_ONGOING_PEER_CONFIG_FAILED is returned without NETWORK_STACK
* permission.
*/
@Test
public void testSetOngingPeerConfigFailureWithoutPermission() throws Exception {
forceP2pEnabled(mClient1);
mockPeersList();
WifiP2pConfig config = new WifiP2pConfig();
when(mWifiPermissionsUtil.checkNetworkStackPermission(anyInt())).thenReturn(false);
sendSetOngoingPeerConfigMsg(mClientMessenger, config);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_ONGOING_PEER_CONFIG_FAILED, message.what);
}
/**
* Verify WifiP2pManager.SET_ONGOING_PEER_CONFIG_FAILED is returned with invalid peer config.
*/
@Test
public void testSetOngoingPeerConfigFailureWithInvalidPeerConfig() throws Exception {
forceP2pEnabled(mClient1);
mockPeersList();
WifiP2pConfig config = new WifiP2pConfig();
when(mWifiPermissionsUtil.checkNetworkStackPermission(anyInt())).thenReturn(true);
sendSetOngoingPeerConfigMsg(mClientMessenger, config);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_ONGOING_PEER_CONFIG_FAILED, message.what);
}
/**
* Verify that respond with RESPONSE_ONGOING_PEER_CONFIG
* when caller sends REQUEST_ONGOING_PEER_CONFIG and permission is granted.
*/
@Test
public void testRequestOngoingPeerConfigSuccess() throws Exception {
forceP2pEnabled(mClient1);
when(mWifiPermissionsUtil.checkNetworkStackPermission(anyInt())).thenReturn(true);
sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_ONGOING_PEER_CONFIG);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
WifiP2pConfig config = (WifiP2pConfig) message.obj;
assertEquals(WifiP2pManager.RESPONSE_ONGOING_PEER_CONFIG, message.what);
assertNotNull(config);
}
/**
* Verify that respond with RESPONSE_ONGOING_PEER_CONFIG
* when caller sends REQUEST_ONGOING_PEER_CONFIG and has no NETWORK_STACK permission.
*/
@Test
public void testRequestOngoingPeerConfigFailureWithoutPermission() throws Exception {
forceP2pEnabled(mClient1);
when(mWifiPermissionsUtil.checkNetworkStackPermission(anyInt())).thenReturn(false);
sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_ONGOING_PEER_CONFIG);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
WifiP2pConfig config = (WifiP2pConfig) message.obj;
assertEquals(WifiP2pManager.RESPONSE_ONGOING_PEER_CONFIG, message.what);
assertNull(config);
}
/**
* Verify that respond with RESPONSE_PERSISTENT_GROUP_INFO
* when caller sends REQUEST_PERSISTENT_GROUP_INFO.
*/
@Test
public void testRequestPersistentGroupInfoSuccess() throws Exception {
// Ensure our own MAC address is not anonymized in the result
when(mWifiPermissionsUtil.checkLocalMacAddressPermission(anyInt())).thenReturn(true);
forceP2pEnabled(mClient1);
sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_PERSISTENT_GROUP_INFO);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
WifiP2pGroupList groups = (WifiP2pGroupList) message.obj;
assertEquals(WifiP2pManager.RESPONSE_PERSISTENT_GROUP_INFO, message.what);
// WifiP2pGroupList does not implement equals operator,
// use toString to compare two lists.
assertEquals(mGroups.toString(), groups.toString());
}
/**
* Verify that when no permissions are held, an empty {@link WifiP2pGroupList} is returned.
*/
@Test
public void testRequestPersistentGroupInfoNoPermissionFailure() throws Exception {
// Ensure our own MAC address is not anonymized in the result
when(mWifiPermissionsUtil.checkLocalMacAddressPermission(anyInt())).thenReturn(true);
forceP2pEnabled(mClient1);
// no permissions held
when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(false);
when(mWifiPermissionsUtil.checkNetworkStackPermission(anyInt())).thenReturn(false);
when(mWifiPermissionsUtil.checkReadWifiCredentialPermission(anyInt())).thenReturn(false);
sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_PERSISTENT_GROUP_INFO);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
WifiP2pGroupList groups = (WifiP2pGroupList) message.obj;
assertEquals(WifiP2pManager.RESPONSE_PERSISTENT_GROUP_INFO, message.what);
// WifiP2pGroupList does not implement equals operator,
// use toString to compare two lists.
// Expect empty WifiP2pGroupList()
assertEquals(new WifiP2pGroupList().toString(), groups.toString());
}
/**
* Verify that respond with RESPONSE_CONNECTION_INFO
* when caller sends REQUEST_CONNECTION_INFO.
*/
@Test
public void testRequestConnectionInfoSuccess() throws Exception {
forceP2pEnabled(mClient1);
sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_CONNECTION_INFO);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
WifiP2pInfo info = (WifiP2pInfo) message.obj;
assertEquals(WifiP2pManager.RESPONSE_CONNECTION_INFO, message.what);
// WifiP2pInfo does not implement equals operator,
// use toString to compare two objects.
assertEquals((new WifiP2pInfo()).toString(), info.toString());
}
/**
* Verify that respond with RESPONSE_P2P_STATE
* when caller sends REQUEST_P2P_STATE and p2p is enabled.
*/
@Test
public void testRequestP2pStateEnabled() throws Exception {
forceP2pEnabled(mClient1);
sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_P2P_STATE);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.RESPONSE_P2P_STATE, message.what);
assertEquals(WifiP2pManager.WIFI_P2P_STATE_ENABLED, message.arg1);
}
/**
* Verify that respond with RESPONSE_P2P_STATE
* when caller sends REQUEST_P2P_STATE and p2p is disabled.
*/
@Test
public void testRequestP2pStateDisabled() throws Exception {
sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_P2P_STATE);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.RESPONSE_P2P_STATE, message.what);
assertEquals(WifiP2pManager.WIFI_P2P_STATE_DISABLED, message.arg1);
}
/**
* Verify that respond with RESPONSE_DISCOVERY_STATE
* when caller sends REQUEST_DISCOVERY_STATE and discovery is started.
*/
@Test
public void testRequestDiscoveryStateWhenStarted() throws Exception {
forceP2pEnabled(mClient1);
when(mWifiPermissionsUtil.checkCanAccessWifiDirect(any(), any(),
anyInt(), anyBoolean())).thenReturn(false);
when(mWifiPermissionsUtil.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"),
anyInt(), anyBoolean())).thenReturn(true);
when(mWifiNative.p2pFind(anyInt())).thenReturn(true);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
sendDiscoverPeersMsg(mClientMessenger);
verify(mWifiNative).p2pFind(anyInt());
verify(mWifiPermissionsUtil).checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"),
anyInt(), eq(true));
sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_DISCOVERY_STATE);
// there are 2 responses:
// * WifiP2pManager.DISCOVER_PEERS_SUCCEEDED
// * WifiP2pManager.RESPONSE_DISCOVERY_STATE
verify(mClientHandler, times(2)).sendMessage(mMessageCaptor.capture());
List<Message> messages = mMessageCaptor.getAllValues();
assertEquals(WifiP2pManager.DISCOVER_PEERS_SUCCEEDED, messages.get(0).what);
assertEquals(WifiP2pManager.RESPONSE_DISCOVERY_STATE, messages.get(1).what);
assertEquals(WifiP2pManager.WIFI_P2P_DISCOVERY_STARTED, messages.get(1).arg1);
}
/**
* Verify that respond with RESPONSE_DISCOVERY_STATE
* when caller sends REQUEST_DISCOVERY_STATE and discovery is stopped.
*/
@Test
public void testRequestDiscoveryStateWhenStopped() throws Exception {
forceP2pEnabled(mClient1);
sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_DISCOVERY_STATE);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.RESPONSE_DISCOVERY_STATE, message.what);
assertEquals(WifiP2pManager.WIFI_P2P_DISCOVERY_STOPPED, message.arg1);
}
/**
* Verify that respond with RESPONSE_NETWORK_INFO
* when caller sends REQUEST_NETWORK_INFO.
*/
@Test
public void testRequestNetworkInfoSuccess() throws Exception {
NetworkInfo info_gold =
new NetworkInfo(ConnectivityManager.TYPE_WIFI_P2P, 0, "WIFI_P2P", "");
sendSimpleMsg(mClientMessenger, WifiP2pManager.REQUEST_NETWORK_INFO);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
NetworkInfo info = (NetworkInfo) message.obj;
assertEquals(WifiP2pManager.RESPONSE_NETWORK_INFO, message.what);
assertEquals(info_gold.toString(), info.toString());
}
/**
* Verify the caller sends WifiP2pManager.REMOVE_LOCAL_SERVICE.
*/
@Test
public void testRemoveLocalServiceSuccess() throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
verifyAddLocalService();
sendRemoveLocalServiceMsg(mClientMessenger, mTestWifiP2pServiceInfo);
verify(mWifiNative).p2pServiceDel(any(WifiP2pServiceInfo.class));
assertTrue(mClientHandler.hasMessages(WifiP2pManager.REMOVE_LOCAL_SERVICE_SUCCEEDED));
}
/**
* Verify the caller sends WifiP2pManager.REMOVE_LOCAL_SERVICE without client info.
*/
@Test
public void testRemoveLocalServiceSuccessWithoutClientInfo() throws Exception {
forceP2pEnabled(mClient1);
sendRemoveLocalServiceMsg(mClientMessenger, mTestWifiP2pServiceInfo);
verify(mWifiNative, never()).p2pServiceDel(any(WifiP2pServiceInfo.class));
assertTrue(mClientHandler.hasMessages(WifiP2pManager.REMOVE_LOCAL_SERVICE_SUCCEEDED));
}
/**
* Verify the caller sends WifiP2pManager.REMOVE_LOCAL_SERVICE when service info is null.
*/
@Test
public void testRemoveLocalServiceSuccessWithNullServiceInfo() throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
verifyAddLocalService();
sendRemoveLocalServiceMsg(mClientMessenger, null);
verify(mWifiNative, never()).p2pServiceDel(any(WifiP2pServiceInfo.class));
assertTrue(mClientHandler.hasMessages(WifiP2pManager.REMOVE_LOCAL_SERVICE_SUCCEEDED));
}
/**
* Verify that respond with REMOVE_LOCAL_SERVICE_FAILED
* when caller sends REMOVE_LOCAL_SERVICE and p2p is disabled.
*/
@Test
public void testRemoveLocalServiceFailureWhenP2pDisabled() throws Exception {
sendRemoveLocalServiceMsg(mClientMessenger, null);
verify(mWifiNative, never()).p2pServiceDel(any(WifiP2pServiceInfo.class));
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.REMOVE_LOCAL_SERVICE_FAILED, message.what);
assertEquals(WifiP2pManager.BUSY, message.arg1);
}
/**
* Verify that respond with REMOVE_LOCAL_SERVICE_FAILED
* when caller sends REMOVE_LOCAL_SERVICE and p2p is unsupported.
*/
@Test
public void testRemoveLocalServiceFailureWhenP2pUnsupported() throws Exception {
setUpWifiP2pServiceImpl(false);
sendRemoveLocalServiceMsg(mClientMessenger, null);
verify(mWifiNative, never()).p2pServiceDel(any(WifiP2pServiceInfo.class));
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.REMOVE_LOCAL_SERVICE_FAILED, message.what);
assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1);
}
/**
* Verify the caller sends WifiP2pManager.CLEAR_LOCAL_SERVICES.
*/
@Test
public void testClearLocalServiceSuccess() throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
verifyAddLocalService();
sendSimpleMsg(mClientMessenger, WifiP2pManager.CLEAR_LOCAL_SERVICES);
verify(mWifiNative, atLeastOnce()).p2pServiceDel(any(WifiP2pServiceInfo.class));
assertTrue(mClientHandler.hasMessages(WifiP2pManager.CLEAR_LOCAL_SERVICES_SUCCEEDED));
}
/**
* Verify the caller sends WifiP2pManager.CLEAR_LOCAL_SERVICES without client info.
*/
@Test
public void testClearLocalServiceSuccessWithoutClientInfo() throws Exception {
forceP2pEnabled(mClient1);
sendSimpleMsg(mClientMessenger, WifiP2pManager.CLEAR_LOCAL_SERVICES);
verify(mWifiNative, never()).p2pServiceDel(any(WifiP2pServiceInfo.class));
assertTrue(mClientHandler.hasMessages(WifiP2pManager.CLEAR_LOCAL_SERVICES_SUCCEEDED));
}
/**
* Verify that respond with CLEAR_LOCAL_SERVICES_FAILED
* when caller sends CLEAR_LOCAL_SERVICES and p2p is disabled.
*/
@Test
public void testClearLocalServiceFailureWhenP2pDisabled() throws Exception {
sendSimpleMsg(mClientMessenger, WifiP2pManager.CLEAR_LOCAL_SERVICES);
verify(mWifiNative, never()).p2pServiceDel(any(WifiP2pServiceInfo.class));
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.CLEAR_LOCAL_SERVICES_FAILED, message.what);
assertEquals(WifiP2pManager.BUSY, message.arg1);
}
/**
* Verify that respond with CLEAR_LOCAL_SERVICES_FAILED
* when caller sends CLEAR_LOCAL_SERVICES and p2p is unsupported.
*/
@Test
public void testClearLocalServiceFailureWhenP2pUnsupported() throws Exception {
setUpWifiP2pServiceImpl(false);
sendSimpleMsg(mClientMessenger, WifiP2pManager.CLEAR_LOCAL_SERVICES);
verify(mWifiNative, never()).p2pServiceDel(any(WifiP2pServiceInfo.class));
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.CLEAR_LOCAL_SERVICES_FAILED, message.what);
assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1);
}
/**
* Verify the caller sends WifiP2pManager.ADD_SERVICE_REQUEST without services discover.
*/
@Test
public void testAddServiceRequestNoOverflow() throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
for (int i = 0; i < 256; i++) {
reset(mTestWifiP2pServiceRequest);
sendAddServiceRequestMsg(mClientMessenger);
ArgumentCaptor<Integer> idCaptor = ArgumentCaptor.forClass(int.class);
verify(mTestWifiP2pServiceRequest).setTransactionId(idCaptor.capture());
assertTrue(idCaptor.getValue().intValue() > 0);
}
}
private void verifyAddServiceRequest() throws Exception {
sendAddServiceRequestMsg(mClientMessenger);
assertTrue(mClientHandler.hasMessages(WifiP2pManager.ADD_SERVICE_REQUEST_SUCCEEDED));
}
/**
* Verify the caller sends WifiP2pManager.ADD_SERVICE_REQUEST without services discover.
*/
@Test
public void testAddServiceRequestSuccessWithoutServiceDiscover() throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
verifyAddServiceRequest();
}
/**
* Verify the caller sends WifiP2pManager.ADD_SERVICE_REQUEST with services discover.
*/
@Test
public void testAddServiceRequestSuccessWithServiceDiscover() throws Exception {
testDiscoverServicesSuccess();
sendAddServiceRequestMsg(mClientMessenger);
verify(mWifiNative, atLeastOnce()).p2pServDiscReq(eq("00:00:00:00:00:00"), anyString());
assertTrue(mClientHandler.hasMessages(WifiP2pManager.ADD_SERVICE_REQUEST_SUCCEEDED));
}
/**
* Verify WifiP2pManager.ADD_SERVICE_REQUEST_FAILED is returned with null request.
*/
@Test
public void testAddServiceRequestFailureWithNullRequest() throws Exception {
forceP2pEnabled(mClient1);
sendSimpleMsg(mClientMessenger, WifiP2pManager.ADD_SERVICE_REQUEST);
assertTrue(mClientHandler.hasMessages(WifiP2pManager.ADD_SERVICE_REQUEST_FAILED));
}
/**
* Verify WifiP2pManager.ADD_SERVICE_REQUEST_FAILED is returned without client info.
*/
@Test
public void testAddServiceRequestFailureWithoutClientInfo() throws Exception {
forceP2pEnabled(mClient1);
sendAddServiceRequestMsg(mClientMessenger);
assertTrue(mClientHandler.hasMessages(WifiP2pManager.ADD_SERVICE_REQUEST_FAILED));
}
/**
* Verify that respond with ADD_SERVICE_REQUEST_FAILED
* when caller sends ADD_SERVICE_REQUEST and p2p is disabled.
*/
@Test
public void testAddServiceRequestFailureWhenP2pDisabled() throws Exception {
sendAddServiceRequestMsg(mClientMessenger);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.ADD_SERVICE_REQUEST_FAILED, message.what);
assertEquals(WifiP2pManager.BUSY, message.arg1);
}
/**
* Verify that respond with ADD_SERVICE_REQUEST_FAILED
* when caller sends ADD_SERVICE_REQUEST and p2p is unsupported.
*/
@Test
public void testAddServiceRequestFailureWhenP2pUnsupported() throws Exception {
setUpWifiP2pServiceImpl(false);
sendAddServiceRequestMsg(mClientMessenger);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.ADD_SERVICE_REQUEST_FAILED, message.what);
assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1);
}
/**
* Verify the caller sends WifiP2pManager.REMOVE_SERVICE_REQUEST.
*/
@Test
public void testRemoveServiceRequestSuccess() throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
verifyAddServiceRequest();
sendRemoveServiceRequestMsg(mClientMessenger, mTestWifiP2pServiceRequest);
assertTrue(mClientHandler.hasMessages(WifiP2pManager.REMOVE_SERVICE_REQUEST_SUCCEEDED));
}
/**
* Verify the caller sends WifiP2pManager.REMOVE_SERVICE_REQUEST without client info.
*/
@Test
public void testRemoveServiceRequestSuccessWithoutClientInfo() throws Exception {
forceP2pEnabled(mClient1);
sendRemoveServiceRequestMsg(mClientMessenger, mTestWifiP2pServiceRequest);
assertTrue(mClientHandler.hasMessages(WifiP2pManager.REMOVE_SERVICE_REQUEST_SUCCEEDED));
}
/**
* Verify the caller sends WifiP2pManager.REMOVE_SERVICE_REQUEST when service info is null.
*/
@Test
public void testRemoveServiceRequestSuccessWithNullServiceInfo() throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
verifyAddLocalService();
sendRemoveServiceRequestMsg(mClientMessenger, null);
assertTrue(mClientHandler.hasMessages(WifiP2pManager.REMOVE_SERVICE_REQUEST_SUCCEEDED));
}
/**
* Verify that respond with REMOVE_SERVICE_REQUEST_FAILED
* when caller sends REMOVE_SERVICE_REQUEST and p2p is disabled.
*/
@Test
public void testRemoveServiceRequestFailureWhenP2pDisabled() throws Exception {
sendRemoveServiceRequestMsg(mClientMessenger, null);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.REMOVE_SERVICE_REQUEST_FAILED, message.what);
assertEquals(WifiP2pManager.BUSY, message.arg1);
}
/**
* Verify that respond with REMOVE_SERVICE_REQUEST_FAILED
* when caller sends REMOVE_SERVICE_REQUEST and p2p is unsupported.
*/
@Test
public void testRemoveServiceRequestFailureWhenP2pUnsupported() throws Exception {
setUpWifiP2pServiceImpl(false);
sendRemoveServiceRequestMsg(mClientMessenger, null);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.REMOVE_SERVICE_REQUEST_FAILED, message.what);
assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1);
}
/**
* Verify the caller sends WifiP2pManager.CLEAR_SERVICE_REQUESTS.
*/
@Test
public void testClearServiceRequestsSuccess() throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
verifyAddServiceRequest();
sendSimpleMsg(mClientMessenger, WifiP2pManager.CLEAR_SERVICE_REQUESTS);
assertTrue(mClientHandler.hasMessages(WifiP2pManager.CLEAR_SERVICE_REQUESTS_SUCCEEDED));
}
/**
* Verify the caller sends WifiP2pManager.CLEAR_SERVICE_REQUESTS without client info.
*/
@Test
public void testClearServiceRequestsSuccessWithoutClientInfo() throws Exception {
forceP2pEnabled(mClient1);
sendSimpleMsg(mClientMessenger, WifiP2pManager.CLEAR_SERVICE_REQUESTS);
assertTrue(mClientHandler.hasMessages(WifiP2pManager.CLEAR_SERVICE_REQUESTS_SUCCEEDED));
}
/**
* Verify that respond with CLEAR_SERVICE_REQUESTS_FAILED
* when caller sends CLEAR_SERVICE_REQUEST and p2p is disabled.
*/
@Test
public void testClearServiceRequestsFailureWhenP2pDisabled() throws Exception {
sendSimpleMsg(mClientMessenger, WifiP2pManager.CLEAR_SERVICE_REQUESTS);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.CLEAR_SERVICE_REQUESTS_FAILED, message.what);
assertEquals(WifiP2pManager.BUSY, message.arg1);
}
/**
* Verify that respond with CLEAR_SERVICE_REQUESTS_FAILED
* when caller sends CLEAR_SERVICE_REQUEST and p2p is unsupported.
*/
@Test
public void testClearServiceRequestsFailureWhenP2pUnsupported() throws Exception {
setUpWifiP2pServiceImpl(false);
sendSimpleMsg(mClientMessenger, WifiP2pManager.CLEAR_SERVICE_REQUESTS);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.CLEAR_SERVICE_REQUESTS_FAILED, message.what);
assertEquals(WifiP2pManager.P2P_UNSUPPORTED, message.arg1);
}
/**
* Verify stopping discovery is executed when location mode is turned off.
*/
@Test
public void testStopDiscoveryWhenLocationModeIsDisabled() throws Exception {
forceP2pEnabled(mClient1);
simulateLocationModeChange(false);
mLooper.dispatchAll();
verify(mWifiNative).p2pStopFind();
}
/**
* Verify a network name which is too long is rejected.
*/
@Test
public void testSendConnectMsgWithTooLongNetworkName() throws Exception {
mTestWifiP2pFastConnectionConfig.networkName = "DIRECT-xy-abcdefghijklmnopqrstuvw";
sendConnectMsg(mClientMessenger, mTestWifiP2pFastConnectionConfig);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.CONNECT_FAILED, message.what);
}
/**
* Verify a network name which is too short is rejected.
*/
@Test
public void testSendConnectMsgWithTooShortNetworkName() throws Exception {
mTestWifiP2pFastConnectionConfig.networkName = "DIRECT-x";
sendConnectMsg(mClientMessenger, mTestWifiP2pFastConnectionConfig);
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.CONNECT_FAILED, message.what);
}
/**
* Verify the group owner intent value is selected correctly when no STA connection.
*/
@Test
public void testGroupOwnerIntentSelectionWithoutStaConnection() throws Exception {
when(mWifiInfo.getNetworkId()).thenReturn(WifiConfiguration.INVALID_NETWORK_ID);
when(mWifiInfo.getFrequency()).thenReturn(2412);
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
mockEnterProvisionDiscoveryState();
WifiP2pProvDiscEvent pdEvent = new WifiP2pProvDiscEvent();
pdEvent.device = mTestWifiP2pDevice;
sendSimpleMsg(null,
WifiP2pMonitor.P2P_PROV_DISC_PBC_RSP_EVENT,
pdEvent);
ArgumentCaptor<WifiP2pConfig> configCaptor =
ArgumentCaptor.forClass(WifiP2pConfig.class);
verify(mWifiNative).p2pConnect(configCaptor.capture(), anyBoolean());
WifiP2pConfig config = configCaptor.getValue();
assertEquals(WifiP2pServiceImpl.DEFAULT_GROUP_OWNER_INTENT + 1,
config.groupOwnerIntent);
}
/**
* Verify the group owner intent value is selected correctly when 2.4GHz STA connection.
*/
@Test
public void testGroupOwnerIntentSelectionWith24GStaConnection() throws Exception {
when(mWifiInfo.getNetworkId()).thenReturn(1);
when(mWifiInfo.getFrequency()).thenReturn(2412);
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
mockEnterProvisionDiscoveryState();
WifiP2pProvDiscEvent pdEvent = new WifiP2pProvDiscEvent();
pdEvent.device = mTestWifiP2pDevice;
sendSimpleMsg(null,
WifiP2pMonitor.P2P_PROV_DISC_PBC_RSP_EVENT,
pdEvent);
ArgumentCaptor<WifiP2pConfig> configCaptor =
ArgumentCaptor.forClass(WifiP2pConfig.class);
verify(mWifiNative).p2pConnect(configCaptor.capture(), anyBoolean());
WifiP2pConfig config = configCaptor.getValue();
assertEquals(WifiP2pConfig.GROUP_OWNER_INTENT_MIN,
config.groupOwnerIntent);
}
/**
* Verify the group owner intent value is selected correctly when 5GHz STA connection.
*/
@Test
public void testGroupOwnerIntentSelectionWith5GHzStaConnection() throws Exception {
when(mWifiInfo.getNetworkId()).thenReturn(1);
when(mWifiInfo.getFrequency()).thenReturn(5200);
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
mockEnterProvisionDiscoveryState();
WifiP2pProvDiscEvent pdEvent = new WifiP2pProvDiscEvent();
pdEvent.device = mTestWifiP2pDevice;
sendSimpleMsg(null,
WifiP2pMonitor.P2P_PROV_DISC_PBC_RSP_EVENT,
pdEvent);
ArgumentCaptor<WifiP2pConfig> configCaptor =
ArgumentCaptor.forClass(WifiP2pConfig.class);
verify(mWifiNative).p2pConnect(configCaptor.capture(), anyBoolean());
WifiP2pConfig config = configCaptor.getValue();
assertEquals(WifiP2pConfig.GROUP_OWNER_INTENT_MAX - 1,
config.groupOwnerIntent);
}
/**
* Verify the group owner intent value is selected correctly when 6GHz STA connection.
*/
@Test
public void testGroupOwnerIntentSelectionWith6GHzStaConnection() throws Exception {
when(mWifiInfo.getNetworkId()).thenReturn(1);
when(mWifiInfo.getFrequency()).thenReturn(6000);
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
mockEnterProvisionDiscoveryState();
WifiP2pProvDiscEvent pdEvent = new WifiP2pProvDiscEvent();
pdEvent.device = mTestWifiP2pDevice;
sendSimpleMsg(null,
WifiP2pMonitor.P2P_PROV_DISC_PBC_RSP_EVENT,
pdEvent);
ArgumentCaptor<WifiP2pConfig> configCaptor =
ArgumentCaptor.forClass(WifiP2pConfig.class);
verify(mWifiNative).p2pConnect(configCaptor.capture(), anyBoolean());
WifiP2pConfig config = configCaptor.getValue();
assertEquals(WifiP2pServiceImpl.DEFAULT_GROUP_OWNER_INTENT,
config.groupOwnerIntent);
}
private List<CoexUnsafeChannel> setupCoexMock(int restrictionBits) {
assumeTrue(SdkLevel.isAtLeastS());
List<CoexUnsafeChannel> unsafeChannels = new ArrayList<>();
unsafeChannels.add(new CoexUnsafeChannel(WifiScanner.WIFI_BAND_24_GHZ, 1));
unsafeChannels.add(new CoexUnsafeChannel(WifiScanner.WIFI_BAND_24_GHZ, 2));
unsafeChannels.add(new CoexUnsafeChannel(WifiScanner.WIFI_BAND_24_GHZ, 3));
unsafeChannels.add(new CoexUnsafeChannel(WifiScanner.WIFI_BAND_5_GHZ, 36));
unsafeChannels.add(new CoexUnsafeChannel(WifiScanner.WIFI_BAND_5_GHZ, 40));
unsafeChannels.add(new CoexUnsafeChannel(WifiScanner.WIFI_BAND_5_GHZ, 165));
when(mCoexManager.getCoexRestrictions()).thenReturn(restrictionBits);
when(mCoexManager.getCoexUnsafeChannels()).thenReturn(unsafeChannels);
when(mWifiNative.p2pSetListenChannel(anyInt())).thenReturn(true);
when(mWifiNative.p2pSetOperatingChannel(anyInt(), any())).thenReturn(true);
return unsafeChannels;
}
/** Verify P2P unsafe channels are set if P2P bit presents in restriction bits. */
@Test
public void testCoexCallbackWithWifiP2pUnsafeChannels() throws Exception {
assumeTrue(SdkLevel.isAtLeastS());
setupCoexMock(0);
assertNotNull(mCoexListener);
forceP2pEnabled(mClient1);
mLooper.dispatchAll();
List<CoexUnsafeChannel> unsafeChannels =
setupCoexMock(WifiManager.COEX_RESTRICTION_WIFI_DIRECT);
mCoexListener.onCoexUnsafeChannelsChanged();
mLooper.dispatchAll();
// On entering P2pEnabledState, these are called once first.
verify(mWifiNative, times(2)).p2pSetListenChannel(eq(0));
ArgumentCaptor<List<CoexUnsafeChannel>> unsafeChannelsCaptor =
ArgumentCaptor.forClass(List.class);
verify(mWifiNative, times(2)).p2pSetOperatingChannel(eq(0), unsafeChannelsCaptor.capture());
List<List<CoexUnsafeChannel>> capturedUnsafeChannelsList =
unsafeChannelsCaptor.getAllValues();
// The second one is what we sent.
assertEquals(unsafeChannels, capturedUnsafeChannelsList.get(1));
}
/** Verify P2P unsafe channels are cleared if P2P bit does not present in restriction bits. */
@Test
public void testCoexCallbackWithoutWifiP2pInRestrictionBits() throws Exception {
assumeTrue(SdkLevel.isAtLeastS());
setupCoexMock(0);
assertNotNull(mCoexListener);
forceP2pEnabled(mClient1);
mLooper.dispatchAll();
mCoexListener.onCoexUnsafeChannelsChanged();
mLooper.dispatchAll();
// On entering P2pEnabledState, these are called once first.
verify(mWifiNative, times(2)).p2pSetListenChannel(eq(0));
ArgumentCaptor<List<CoexUnsafeChannel>> unsafeChannelsCaptor =
ArgumentCaptor.forClass(List.class);
verify(mWifiNative, times(2)).p2pSetOperatingChannel(eq(0), unsafeChannelsCaptor.capture());
List<List<CoexUnsafeChannel>> capturedUnsafeChannelsList =
unsafeChannelsCaptor.getAllValues();
// The second one is what we sent.
assertEquals(0, capturedUnsafeChannelsList.get(1).size());
}
/**
* Verify the caller sends WifiP2pManager.SET_WFD_INFO with wfd enabled
* and WFD R2 device info.
*/
@Test
public void testSetWfdR2InfoSuccessWithWfdEnabled() throws Exception {
assumeTrue(SdkLevel.isAtLeastS());
// Move to enabled state
forceP2pEnabled(mClient1);
mTestThisDevice.status = mTestThisDevice.AVAILABLE;
mTestThisDevice.wfdInfo = new WifiP2pWfdInfo();
mTestThisDevice.wfdInfo.setEnabled(true);
mTestThisDevice.wfdInfo.setR2DeviceType(WifiP2pWfdInfo.DEVICE_TYPE_WFD_SOURCE);
when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper);
when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt()))
.thenReturn(PackageManager.PERMISSION_GRANTED);
when(mWifiNative.setWfdEnable(anyBoolean())).thenReturn(true);
when(mWifiNative.setWfdDeviceInfo(anyString())).thenReturn(true);
when(mWifiNative.setWfdR2DeviceInfo(anyString())).thenReturn(true);
sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo);
verify(mWifiInjector).getWifiPermissionsWrapper();
verify(mWifiPermissionsWrapper).getUidPermission(
eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt());
verify(mWifiNative).setWfdEnable(eq(true));
verify(mWifiNative).setWfdDeviceInfo(eq(mTestThisDevice.wfdInfo.getDeviceInfoHex()));
verify(mWifiNative).setWfdR2DeviceInfo(eq(mTestThisDevice.wfdInfo.getR2DeviceInfoHex()));
checkSendThisDeviceChangedBroadcast();
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_WFD_INFO_SUCCEEDED, message.what);
}
/**
* Verify WifiP2pManager.SET_WFD_INFO_FAILED is returned when wfd is enabled,
* WFD R2 device, and native call "setWfdR2DeviceInfo" failure.
*/
@Test
public void testSetWfdR2InfoFailureWithWfdEnabledWhenNativeCallFailure2() throws Exception {
assumeTrue(SdkLevel.isAtLeastS());
// Move to enabled state
forceP2pEnabled(mClient1);
mTestThisDevice.status = mTestThisDevice.AVAILABLE;
mTestThisDevice.wfdInfo = new WifiP2pWfdInfo();
mTestThisDevice.wfdInfo.setEnabled(true);
mTestThisDevice.wfdInfo.setR2DeviceType(WifiP2pWfdInfo.DEVICE_TYPE_WFD_SOURCE);
when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper);
when(mWifiPermissionsWrapper.getUidPermission(anyString(), anyInt()))
.thenReturn(PackageManager.PERMISSION_GRANTED);
when(mWifiNative.setWfdEnable(anyBoolean())).thenReturn(true);
when(mWifiNative.setWfdDeviceInfo(anyString())).thenReturn(true);
when(mWifiNative.setWfdR2DeviceInfo(anyString())).thenReturn(false);
sendSetWfdInfoMsg(mClientMessenger, mTestThisDevice.wfdInfo);
verify(mWifiInjector).getWifiPermissionsWrapper();
verify(mWifiPermissionsWrapper).getUidPermission(
eq(android.Manifest.permission.CONFIGURE_WIFI_DISPLAY), anyInt());
verify(mWifiNative).setWfdEnable(eq(true));
verify(mWifiNative).setWfdDeviceInfo(eq(mTestThisDevice.wfdInfo.getDeviceInfoHex()));
verify(mWifiNative).setWfdR2DeviceInfo(eq(mTestThisDevice.wfdInfo.getR2DeviceInfoHex()));
verify(mClientHandler).sendMessage(mMessageCaptor.capture());
Message message = mMessageCaptor.getValue();
assertEquals(WifiP2pManager.SET_WFD_INFO_FAILED, message.what);
assertEquals(WifiP2pManager.ERROR, message.arg1);
}
/**
* Verify that P2P group is removed during group creating failure.
*/
@Test
public void testGroupCreatingFailureDueToTethering() throws Exception {
when(mWifiNative.p2pGroupAdd(anyBoolean())).thenReturn(true);
when(mWifiNative.p2pGroupRemove(eq(IFACE_NAME_P2P))).thenReturn(true);
when(mWifiPermissionsUtil.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"),
anyInt(), anyBoolean())).thenReturn(true);
WifiP2pGroup group = new WifiP2pGroup();
group.setNetworkId(WifiP2pGroup.NETWORK_ID_PERSISTENT);
group.setNetworkName("DIRECT-xy-NEW");
group.setOwner(new WifiP2pDevice("thisDeviceMac"));
group.setIsGroupOwner(true);
group.setInterface(IFACE_NAME_P2P);
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
mLooper.dispatchAll();
sendCreateGroupMsg(mClientMessenger, WifiP2pGroup.NETWORK_ID_TEMPORARY, null);
mLooper.dispatchAll();
sendGroupStartedMsg(group);
mLooper.dispatchAll();
mLooper.moveTimeForward(120 * 1000 * 2);
mLooper.dispatchAll();
verify(mWifiNative).p2pGroupRemove(group.getInterface());
}
/**
* Verify the idle timer is cancelled after leaving inactive state.
*/
@Test
public void testIdleTimeoutCancelledAfterLeavingInactiveState() throws Exception {
forceP2pEnabled(mClient1);
sendChannelInfoUpdateMsg("testPkg1", "testFeature", mClient1, mClientMessenger);
mockPeersList();
sendConnectMsg(mClientMessenger, mTestWifiP2pPeerConfig);
verify(mWifiPermissionsUtil)
.checkCanAccessWifiDirect(eq("testPkg1"), eq("testFeature"), anyInt(), eq(false));
ArgumentCaptor<WifiP2pConfig> configCaptor =
ArgumentCaptor.forClass(WifiP2pConfig.class);
verify(mWifiP2pMetrics).startConnectionEvent(
eq(P2pConnectionEvent.CONNECTION_FRESH),
configCaptor.capture());
assertEquals(mTestWifiP2pPeerConfig.toString(), configCaptor.getValue().toString());
// Verify timer is cannelled
// Includes re-schedule 4 times:
// 1. forceP2pEnabled(): enter InactiveState
// 2. forceP2pEnabled: REQUEST_P2P_STATE
// 3. CONNECT
// 4. leave InactiveState
verify(mAlarmManager, times(4)).setExact(anyInt(), anyLong(),
eq(mWifiP2pServiceImpl.P2P_IDLE_SHUTDOWN_MESSAGE_TIMEOUT_TAG), any(), any());
verify(mAlarmManager, times(4)).cancel(eq(mWifiP2pServiceImpl.mP2pIdleShutdownMessage));
}
/**
* Verify the interface down after idle timer is triggered.
*/
@Test
public void testIdleTimeoutTriggered() throws Exception {
forceP2pEnabled(mClient1);
mWifiP2pServiceImpl.mP2pIdleShutdownMessage.onAlarm();
mLooper.dispatchAll();
verify(mWifiNative).teardownInterface();
verify(mWifiMonitor).stopMonitoring(anyString());
}
}