1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #define private public
17 #define protected public
18
19 #include "coordination_sm_test.h"
20
21 #include "accesstoken_kit.h"
22 #include <gtest/gtest.h>
23 #include "nativetoken_kit.h"
24 #include "nocopyable.h"
25 #include "token_setproc.h"
26 #include "pointer_event.h"
27 #include "coordination_device_manager.h"
28 #include "coordination_event_handler.h"
29 #include "coordination_message.h"
30 #include "coordination_sm.h"
31 #include "coordination_softbus_adapter.h"
32 #include "coordination_state_in.h"
33 #include "coordination_util.h"
34 #include "device.h"
35 #include "fi_log.h"
36
37 using namespace ::OHOS;
38 using namespace ::OHOS::Security::AccessToken;
39
40 namespace OHOS {
41 namespace Msdp {
42 namespace DeviceStatus {
43 using namespace testing::ext;
44 namespace {
45 constexpr ::OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "CoordinationSMTest" };
46 const std::string REMOTE_NETWORKID { "Test_Remote_NetworkId" };
47 const std::string ORIGIN_NETWORKID { "Test_Origin_NetworkId" };
48 constexpr int32_t DEVICE_ID { 0 };
49 constexpr int32_t ERR_CODE { 20900001 };
50 constexpr int32_t UNKNOWN_STATE { 3 };
51 } // namespace
52
ClearCoordiantionSM()53 void ClearCoordiantionSM()
54 {
55 COOR_SM->preparedNetworkId_ = { "", "" };
56 COOR_SM->startDeviceDhid_ = "";
57 COOR_SM->remoteNetworkId_ = "";
58 COOR_SM->sinkNetworkId_ = "";
59 COOR_SM->isUnchained_ = false;
60 COOR_SM->currentState_ = CoordinationState::STATE_FREE;
61 COOR_SM->initCallback_ = nullptr;
62 COOR_SM->stateCallback_ = nullptr;
63 COOR_SM->isStarting_ = false;
64 COOR_SM->isStopping_ = false;
65 COOR_SM->mouseLocation_ = std::make_pair(0, 0);
66 COOR_SM->lastPointerEvent_ = nullptr;
67 COOR_SM->displayX_ = -1;
68 COOR_SM->displayY_ = -1;
69 COOR_SM->monitorId_ = -1;
70 COOR_SM->interceptorId_ = -1;
71 COOR_SM->filterId_ = -1;
72 COOR_SM->remoteNetworkIdCallback_ = nullptr;
73 COOR_SM->mouseLocationCallback_ = nullptr;
74 COOR_SM->notifyDragCancelCallback_ = nullptr;
75 COOR_SM->runner_ = nullptr;
76 COOR_SM->onlineDevice_.clear();
77 COOR_SM->stateChangedCallbacks_.clear();
78 COOR_SM->coordinationStates_.clear();
79 }
80
ClearCoordinationSoftbusAdapter()81 void ClearCoordinationSoftbusAdapter()
82 {
83 COOR_SOFTBUS_ADAPTER->socketFd_ = -1;
84 COOR_SOFTBUS_ADAPTER->localSessionName_ = "";
85 COOR_SOFTBUS_ADAPTER->onRecvDataCallback_ = nullptr;
86 COOR_SOFTBUS_ADAPTER->sessionDevs_.clear();
87 }
88
89 class CoordinationSMTest : public testing::Test {
90 public:
SetUpTestCase()91 static void SetUpTestCase() {}
TearDownTestCase()92 static void TearDownTestCase() {}
93 void SetUp();
TearDown()94 void TearDown() {}
95 void AddPermission();
96 void SetAceessTokenPermission(const char** perms, size_t permAmount);
97 };
98
SetUp()99 void CoordinationSMTest::SetUp()
100 {
101 AddPermission();
102 }
103
AddPermission()104 void CoordinationSMTest::AddPermission()
105 {
106 const char *perms[] = { "ohos.permission.DISTRIBUTED_DATASYNC" };
107 SetAceessTokenPermission(perms, sizeof(perms) / sizeof(perms[0]));
108 }
109
SetAceessTokenPermission(const char ** perms,size_t permAmount)110 void CoordinationSMTest::SetAceessTokenPermission(const char** perms, size_t permAmount)
111 {
112 if (perms == nullptr || permAmount == 0) {
113 FI_HILOGE("The perms is empty");
114 return;
115 }
116 NativeTokenInfoParams infoInstance = {
117 .dcapsNum = 0,
118 .permsNum = permAmount,
119 .aclsNum = 0,
120 .dcaps = nullptr,
121 .perms = perms,
122 .acls = nullptr,
123 .processName = "CoordinationSMTest",
124 .aplStr = "system_basic",
125 };
126 uint64_t tokenId = GetAccessTokenId(&infoInstance);
127 SetSelfTokenID(tokenId);
128 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
129 }
130
SendMsg(int32_t sessionId,const std::string & message)131 int32_t CoordinationSoftbusAdapter::SendMsg(int32_t sessionId, const std::string &message)
132 {
133 CALL_DEBUG_ENTER;
134 return sessionId == 1 ? RET_OK : RET_ERR;
135 }
136
Device(int32_t deviceId)137 Device::Device(int32_t deviceId) :deviceId_(deviceId) {}
138
~Device()139 Device::~Device() {}
140
Open()141 int32_t Device::Open()
142 {
143 return 0;
144 }
145
Close()146 void Device::Close() {}
147
Dispatch(const struct epoll_event & ev)148 void Device::Dispatch(const struct epoll_event &ev) {}
149
150 /**
151 * @tc.name: CoordinationSMTest001
152 * @tc.desc: test IsNeedFilterOut state == CoordinationState::STATE_OUT
153 * @tc.type: FUNC
154 */
155 HWTEST_F(CoordinationSMTest, CoordinationSMTest001, TestSize.Level0)
156 {
157 CALL_TEST_DEBUG;
158 std::string localNetworkId = COORDINATION::GetLocalNetworkId();
159 std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
160 ASSERT_NE(keyEvent, nullptr);
161 keyEvent->SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_BACK);
162 keyEvent->SetActionTime(1);
163 keyEvent->SetKeyAction(OHOS::MMI::KeyEvent::KEY_ACTION_DOWN);
164 OHOS::MMI::KeyEvent::KeyItem item;
165 item.SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_BACK);
166 item.SetDownTime(1);
167 item.SetPressed(true);
168 keyEvent->AddKeyItem(item);
169 bool ret = COOR_SM->IsNeedFilterOut(localNetworkId, keyEvent);
170 EXPECT_EQ(false, ret);
171 ClearCoordiantionSM();
172 ClearCoordinationSoftbusAdapter();
173 }
174
175 /**
176 * @tc.name: CoordinationSMTest002
177 * @tc.desc: test abnormal GetCoordinationState when local network id is empty
178 * @tc.type: FUNC
179 */
180 HWTEST_F(CoordinationSMTest, CoordinationSMTest002, TestSize.Level0)
181 {
182 CALL_TEST_DEBUG;
183 int32_t ret = COOR_SM->GetCoordinationState("");
184 EXPECT_TRUE(ret == COMMON_PARAMETER_ERROR);
185 ClearCoordiantionSM();
186 ClearCoordinationSoftbusAdapter();
187 }
188
189 /**
190 * @tc.name: CoordinationSMTest003
191 * @tc.desc: test normal GetCoordinationState when local network id is correct
192 * @tc.type: FUNC
193 */
194 HWTEST_F(CoordinationSMTest, CoordinationSMTest003, TestSize.Level0)
195 {
196 CALL_TEST_DEBUG;
197 std::string localNetworkId = COORDINATION::GetLocalNetworkId();
198 int32_t ret = COOR_SM->GetCoordinationState(localNetworkId);
199 EXPECT_TRUE(ret == 0);
200 ClearCoordiantionSM();
201 ClearCoordinationSoftbusAdapter();
202 }
203
204 /**
205 * @tc.name: CoordinationSMTest004
206 * @tc.desc: Interface (GetDeviceCoordinationState) testing
207 * @tc.type: FUNC
208 */
209 HWTEST_F(CoordinationSMTest, CoordinationSMTest004, TestSize.Level0)
210 {
211 CALL_TEST_DEBUG;
212 std::string state = COOR_SM->GetDeviceCoordinationState(CoordinationState::STATE_FREE);
213 EXPECT_TRUE(state == "free");
214 state = COOR_SM->GetDeviceCoordinationState(CoordinationState::STATE_IN);
215 EXPECT_TRUE(state == "in");
216 state = COOR_SM->GetDeviceCoordinationState(CoordinationState::STATE_OUT);
217 EXPECT_TRUE(state == "out");
218 state = COOR_SM->GetDeviceCoordinationState(static_cast<CoordinationState>(UNKNOWN_STATE));
219 EXPECT_TRUE(state == "unknown");
220 }
221
222 /**
223 * @tc.name: CoordinationSMTest005
224 * @tc.desc: Interface (UpdateLastPointerEventCallback) testing
225 * @tc.type: FUNC
226 */
227 HWTEST_F(CoordinationSMTest, CoordinationSMTest005, TestSize.Level0)
228 {
229 CALL_TEST_DEBUG;
230 auto pointerEvent = MMI::PointerEvent::Create();
231 ASSERT_NE(pointerEvent, nullptr);
232 pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_CANCEL);
233 COOR_SM->UpdateLastPointerEventCallback(pointerEvent);
234 EXPECT_TRUE(COOR_SM->lastPointerEvent_ == pointerEvent);
235 }
236
237 /**
238 * @tc.name: CoordinationSMTest006
239 * @tc.desc: Interface (GetLastPointerEvent) testing
240 * @tc.type: FUNC
241 */
242 HWTEST_F(CoordinationSMTest, CoordinationSMTest006, TestSize.Level0)
243 {
244 CALL_TEST_DEBUG;
245 auto pointerEvent = MMI::PointerEvent::Create();
246 ASSERT_NE(pointerEvent, nullptr);
247 pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_CANCEL);
248 COOR_SM->UpdateLastPointerEventCallback(pointerEvent);
249 auto lastPointerEvent = COOR_SM->GetLastPointerEvent();
250 ASSERT_NE(lastPointerEvent, nullptr);
251 EXPECT_TRUE(lastPointerEvent->GetPointerAction() == MMI::PointerEvent::POINTER_ACTION_CANCEL);
252 }
253
254 /**
255 * @tc.name: CoordinationSMTest007
256 * @tc.desc: Interface (SetSinkNetworkId) testing
257 * @tc.type: FUNC
258 */
259 HWTEST_F(CoordinationSMTest, CoordinationSMTest007, TestSize.Level0)
260 {
261 CALL_TEST_DEBUG;
262 std::string remoteNetworkId("cde2b5b4453a5b3ec566f836ffa7a4aab52c4b9c8a0b34f3d6aaca4566db24f0");
263 COOR_SM->SetSinkNetworkId(remoteNetworkId);
264 EXPECT_TRUE(COOR_SM->sinkNetworkId_ == remoteNetworkId);
265 COOR_SM->sinkNetworkId_.clear();
266 }
267
268 /**
269 * @tc.name: CoordinationSMTest008
270 * @tc.desc: Interface (Reset) testing
271 * @tc.type: FUNC
272 */
273 HWTEST_F(CoordinationSMTest, CoordinationSMTest008, TestSize.Level0)
274 {
275 CALL_TEST_DEBUG;
276 COOR_SM->isStarting_ = true;
277 std::string networkId("cde2b5b4453a5b3ec566f836ffa7a4aab52c4b9c8a0b34f3d6aaca4566db24f0");
278 COOR_SM->Reset(networkId);
279 EXPECT_TRUE(!COOR_SM->isStarting_);
280 COOR_SM->remoteNetworkId_.clear();
281 COOR_SM->sinkNetworkId_.clear();
282 }
283
284 /**
285 * @tc.name: CoordinationSMTest009
286 * @tc.desc: test normal ActivateCoordination return the correct value
287 * @tc.type: FUNC
288 */
289 HWTEST_F(CoordinationSMTest, CoordinationSMTest009, TestSize.Level0)
290 {
291 CALL_TEST_DEBUG;
292 int32_t startDeviceId = 1;
293 COOR_SOFTBUS_ADAPTER->sessionDevs_[REMOTE_NETWORKID] = 0;
294 COOR_SM->currentState_ = CoordinationState::STATE_IN;
295 COOR_SM->coordinationStates_[CoordinationState::STATE_IN] = std::make_shared<CoordinationStateIn>();
296 int32_t ret = COOR_SM->ActivateCoordination(REMOTE_NETWORKID, startDeviceId);
297 EXPECT_EQ(ret, ERR_CODE);
298 COOR_SOFTBUS_ADAPTER->sessionDevs_[REMOTE_NETWORKID] = 1;
299 ret = COOR_SM->ActivateCoordination(REMOTE_NETWORKID, startDeviceId);
300 EXPECT_EQ(ret, RET_OK);
301 ClearCoordiantionSM();
302 ClearCoordinationSoftbusAdapter();
303 }
304
305 /**
306 * @tc.name: CoordinationSMTest010
307 * @tc.desc: test normal DeactivateCoordination return the correct value
308 * @tc.type: FUNC
309 */
310 HWTEST_F(CoordinationSMTest, CoordinationSMTest010, TestSize.Level0)
311 {
312 CALL_TEST_DEBUG;
313 COOR_SM->currentState_ = CoordinationState::STATE_IN;
314 COOR_SM->coordinationStates_[CoordinationState::STATE_IN] = std::make_shared<CoordinationStateIn>();
315 COOR_SM->startDeviceDhid_ = "teststartDeviceDhid";
316 auto curdevice= std::make_shared<Device>(DEVICE_ID);
317 curdevice->name_ = "DistributedInput ";
318 auto dev = std::make_shared<CoordinationDeviceManager::Device>(curdevice);
319 dev->dhid_ = COOR_SM->startDeviceDhid_;
320 dev->networkId_ = "testNetworkId";
__anona3d752af0202(void) 321 std::function<void(void)> mycallback = [&](void) {
322 GTEST_LOG_(INFO) << "notifyDragCancelCallback_ callback test";
323 };
324 COOR_SM->notifyDragCancelCallback_ = mycallback;
325 COOR_DEV_MGR->devices_[0] = dev;
326 COOR_SOFTBUS_ADAPTER->sessionDevs_["testNetworkId"] = 0;
327 int32_t ret = COOR_SM->DeactivateCoordination(false);
328 EXPECT_EQ(ret, RET_ERR);
329 COOR_SOFTBUS_ADAPTER->sessionDevs_["testNetworkId"] = 1;
330 ret = COOR_SM->DeactivateCoordination(false);
331 EXPECT_EQ(ret, RET_OK);
332 ClearCoordiantionSM();
333 ClearCoordinationSoftbusAdapter();
334 }
335
336 /**
337 * @tc.name: CoordinationSMTest011
338 * @tc.desc: test normal UpdateState
339 * @tc.type: FUNC
340 */
341 HWTEST_F(CoordinationSMTest, CoordinationSMTest011, TestSize.Level0)
342 {
343 CALL_TEST_DEBUG;
344 COOR_SM->UpdateState(CoordinationState::STATE_IN);
345 auto curstate = COOR_SM->GetCurrentCoordinationState();
346 EXPECT_EQ(curstate, CoordinationState::STATE_IN);
347 std::pair<std::string, std::string> devicelist = COOR_SM->GetPreparedDevices();
348 EXPECT_TRUE(devicelist.first.empty() && devicelist.second.empty());
349 ClearCoordiantionSM();
350 ClearCoordinationSoftbusAdapter();
351 }
352
353 /**
354 * @tc.name: CoordinationSMTest012
355 * @tc.desc: test normal UpdatePreparedDevices and obtain correct value
356 * @tc.type: FUNC
357 */
358 HWTEST_F(CoordinationSMTest, CoordinationSMTest012, TestSize.Level0)
359 {
360 CALL_TEST_DEBUG;
361 COOR_SM->UpdatePreparedDevices(REMOTE_NETWORKID, ORIGIN_NETWORKID);
362 std::pair<std::string, std::string> devicelist = COOR_SM->GetPreparedDevices();
363 EXPECT_TRUE((devicelist.first == REMOTE_NETWORKID) && (devicelist.second == ORIGIN_NETWORKID));
364 ClearCoordiantionSM();
365 ClearCoordinationSoftbusAdapter();
366 }
367 } // namespace DeviceStatus
368 } // namespace Msdp
369 } // namespace OHOS
370