1 /*
2 * Copyright (c) 2021-2022 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 #include <algorithm>
17 #include <fstream>
18 #include <functional>
19 #include <iostream>
20 #include <map>
21 #include <memory>
22 #include <thread>
23
24 #define private public
25 #include "remote_command_executor.h"
26 #include "token_sync_manager_service.h"
27 #undef private
28
29 #include "gtest/gtest.h"
30 #include "accesstoken_kit.h"
31 #include "accesstoken_common_log.h"
32 #include "access_token_error.h"
33 #include "base_remote_command.h"
34 #include "constant_common.h"
35 #include "delete_remote_token_command.h"
36 #include "device_info_manager.h"
37 #include "device_info_repository.h"
38 #include "device_info.h"
39 #include "device_manager.h"
40 #include "device_manager_callback.h"
41 #include "dm_device_info.h"
42 #include "i_token_sync_manager.h"
43 #define private public
44 #include "remote_command_manager.h"
45 #undef private
46 #include "socket.h"
47 #include "soft_bus_device_connection_listener.h"
48 #include "soft_bus_socket_listener.h"
49 #include "test_common.h"
50 #include "token_setproc.h"
51 #include "token_sync_manager_stub.h"
52
53 using namespace std;
54 using namespace testing::ext;
55
56 namespace OHOS {
57 namespace Security {
58 namespace AccessToken {
59 static std::vector<std::thread> threads_;
60 static std::shared_ptr<SoftBusDeviceConnectionListener> g_ptrDeviceStateCallback =
61 std::make_shared<SoftBusDeviceConnectionListener>();
62 static std::string g_udid = "deviceid-1:udid-001";
63 static int32_t g_selfUid;
64 static AccessTokenID g_selfTokenId = 0;
65 static const int32_t OUT_OF_MAP_SOCKET = 2;
66 static const std::string TOKEN_SYNC_PACKAGE_NAME = "ohos.security.distributed_access_token";
67 static const std::string TOKEN_SYNC_SOCKET_NAME = "ohos.security.atm_channel.";
68 static const uint32_t SOCKET_NAME_MAX_LEN = 256;
69
70 class TokenSyncServiceTest : public testing::Test {
71 public:
72 TokenSyncServiceTest();
73 ~TokenSyncServiceTest();
74 static void SetUpTestCase();
75 static void TearDownTestCase();
76 void OnDeviceOffline(const DistributedHardware::DmDeviceInfo &info);
77 void SetUp();
78 void TearDown();
79 std::shared_ptr<TokenSyncManagerService> tokenSyncManagerService_;
80 };
81
82 static DistributedHardware::DmDeviceInfo g_devInfo = {
83 // udid = deviceid-1:udid-001 uuid = deviceid-1:uuid-001
84 .deviceId = "deviceid-1",
85 .deviceName = "remote_mock",
86 .deviceTypeId = 1,
87 .networkId = "deviceid-1"
88 };
89
90 namespace {
91 static constexpr int MAX_RETRY_TIMES = 10;
92 static constexpr int32_t DEVICEID_MAX_LEN = 256;
93 }
94
TokenSyncServiceTest()95 TokenSyncServiceTest::TokenSyncServiceTest()
96 {
97 DelayedSingleton<TokenSyncManagerService>::GetInstance()->Initialize();
98 }
~TokenSyncServiceTest()99 TokenSyncServiceTest::~TokenSyncServiceTest()
100 {}
101
NativeTokenGet()102 void NativeTokenGet()
103 {
104 uint64_t tokenId = 0;
105 tokenId = TestCommon::GetNativeTokenIdFromProcess("token_sync_service");
106 ASSERT_NE(tokenId, static_cast<AccessTokenID>(0));
107 EXPECT_EQ(0, SetSelfTokenID(tokenId));
108 }
109
SetUpTestCase()110 void TokenSyncServiceTest::SetUpTestCase()
111 {
112 g_selfUid = getuid();
113 g_selfTokenId = GetSelfTokenID();
114 TestCommon::SetTestEvironment(g_selfTokenId);
115
116 NativeTokenGet();
117 }
TearDownTestCase()118 void TokenSyncServiceTest::TearDownTestCase()
119 {
120 SetSelfTokenID(g_selfTokenId);
121 TestCommon::ResetTestEvironment();
122 }
SetUp()123 void TokenSyncServiceTest::SetUp()
124 {
125 tokenSyncManagerService_ = DelayedSingleton<TokenSyncManagerService>::GetInstance();
126 EXPECT_NE(nullptr, tokenSyncManagerService_);
127 }
TearDown()128 void TokenSyncServiceTest::TearDown()
129 {
130 LOGI(ATM_DOMAIN, ATM_TAG, "TearDown start.");
131 tokenSyncManagerService_ = nullptr;
132 for (auto it = threads_.begin(); it != threads_.end(); it++) {
133 it->join();
134 }
135 threads_.clear();
136
137 if (g_ptrDeviceStateCallback != nullptr) {
138 OnDeviceOffline(g_devInfo);
139 sleep(1);
140 }
141 }
142
OnDeviceOffline(const DistributedHardware::DmDeviceInfo & info)143 void TokenSyncServiceTest::OnDeviceOffline(const DistributedHardware::DmDeviceInfo &info)
144 {
145 std::string networkId = info.networkId;
146 std::string uuid = DeviceInfoManager::GetInstance().ConvertToUniversallyUniqueIdOrFetch(networkId);
147 if (uuid.empty()) {
148 uuid = SoftBusManager::GetInstance().GetUniversallyUniqueIdByNodeId(networkId);
149 }
150 std::string udid = DeviceInfoManager::GetInstance().ConvertToUniqueDeviceIdOrFetch(networkId);
151
152 LOGI(ATM_DOMAIN, ATM_TAG,
153 "networkId: %{public}s, uuid: %{public}s, udid: %{public}s",
154 networkId.c_str(),
155 uuid.c_str(),
156 ConstantCommon::EncryptDevId(udid).c_str());
157
158 if (uuid != "" && udid != "") {
159 RemoteCommandManager::GetInstance().NotifyDeviceOffline(uuid);
160 RemoteCommandManager::GetInstance().NotifyDeviceOffline(udid);
161 DeviceInfoManager::GetInstance().RemoveRemoteDeviceInfo(networkId, DeviceIdType::NETWORK_ID);
162 } else {
163 LOGE(ATM_DOMAIN, ATM_TAG, "uuid or udid is empty, offline failed.");
164 }
165 }
166
167 namespace {
168 std::string g_jsonBefore;
169 std::string g_jsonAfter;
170 }
171
SendTaskThread()172 void SendTaskThread()
173 {
174 int count = 0;
175 while (!GetSendMessFlagMock() && count < MAX_RETRY_TIMES) {
176 sleep(1);
177 count++;
178 }
179
180 ResetSendMessFlagMock();
181
182 std::string uuidMessage = GetUuidMock();
183 std::string sendJson = g_jsonBefore + uuidMessage + g_jsonAfter;
184
185 unsigned char *sendBuffer = (unsigned char *)malloc(0x1000);
186 if (sendBuffer == nullptr) {
187 return;
188 }
189 int sendLen = 0x1000;
190 CompressMock(sendJson, sendBuffer, sendLen);
191
192 SoftBusSocketListener::OnClientBytes(1, sendBuffer, sendLen);
193 free(sendBuffer);
194 }
195
196 static PermissionDef g_infoManagerTestPermDef1 = {
197 .permissionName = "ohos.permission.test1",
198 .bundleName = "accesstoken_test",
199 .grantMode = 1,
200 .availableLevel = APL_NORMAL,
201 .label = "label",
202 .labelId = 1,
203 .description = "open the door",
204 .descriptionId = 1
205 };
206
207 static PermissionDef g_infoManagerTestPermDef2 = {
208 .permissionName = "ohos.permission.test2",
209 .bundleName = "accesstoken_test",
210 .grantMode = 1,
211 .availableLevel = APL_NORMAL,
212 .label = "label",
213 .labelId = 1,
214 .description = "break the door",
215 .descriptionId = 1
216 };
217
218 static PermissionStateFull g_infoManagerTestState1 = {
219 .permissionName = "ohos.permission.test1",
220 .isGeneral = true,
221 .resDeviceID = {"local"},
222 .grantStatus = {PermissionState::PERMISSION_GRANTED},
223 .grantFlags = {1}
224 };
225
226 static PermissionStateFull g_infoManagerTestState2 = {
227 .permissionName = "ohos.permission.test2",
228 .isGeneral = false,
229 .resDeviceID = {"device 1", "device 2"},
230 .grantStatus = {PermissionState::PERMISSION_GRANTED, PermissionState::PERMISSION_GRANTED},
231 .grantFlags = {1, 2}
232 };
233
234 static HapInfoParams g_infoManagerTestInfoParms = {
235 .userID = 1,
236 .bundleName = "accesstoken_test",
237 .instIndex = 0,
238 .appIDDesc = "testtesttesttest"
239 };
240
241 static HapPolicyParams g_infoManagerTestPolicyPrams = {
242 .apl = APL_NORMAL,
243 .domain = "test.domain",
244 .permList = {g_infoManagerTestPermDef1, g_infoManagerTestPermDef2},
245 .permStateList = {g_infoManagerTestState1, g_infoManagerTestState2}
246 };
247
248 class TestBaseRemoteCommand : public BaseRemoteCommand {
249 public:
Prepare()250 void Prepare() override {}
251
Execute()252 void Execute() override {}
253
Finish()254 void Finish() override {}
255
ToJsonPayload()256 std::string ToJsonPayload() override
257 {
258 return std::string();
259 }
260
TestBaseRemoteCommand()261 TestBaseRemoteCommand() {}
262 virtual ~TestBaseRemoteCommand() = default;
263 };
264
DeleteAndAllocToken(AccessTokenID & tokenId)265 static void DeleteAndAllocToken(AccessTokenID& tokenId)
266 {
267 // create local token
268 AccessTokenIDEx tokenIdEx = TestCommon::GetHapTokenIdFromBundle(g_infoManagerTestInfoParms.userID,
269 g_infoManagerTestInfoParms.bundleName, g_infoManagerTestInfoParms.instIndex);
270 TestCommon::DeleteTestHapToken(tokenIdEx.tokenIdExStruct.tokenID);
271
272 AccessTokenIDEx tokenIdEx1 = {0};
273 TestCommon::AllocTestHapToken(g_infoManagerTestInfoParms, g_infoManagerTestPolicyPrams, tokenIdEx1);
274 ASSERT_NE(static_cast<AccessTokenID>(0), tokenIdEx1.tokenIdExStruct.tokenID);
275 }
276
277 /**
278 * @tc.name: ProcessOneCommand001
279 * @tc.desc: RemoteCommandExecutor::ProcessOneCommand function test with nullptr
280 * @tc.type: FUNC
281 * @tc.require:
282 */
283 HWTEST_F(TokenSyncServiceTest, ProcessOneCommand001, TestSize.Level0)
284 {
285 std::string nodeId = "test_nodeId";
286 auto executor = std::make_shared<RemoteCommandExecutor>(nodeId);
287 EXPECT_EQ(Constant::SUCCESS, executor->ProcessOneCommand(nullptr));
288 }
289
290 /**
291 * @tc.name: ProcessOneCommand002
292 * @tc.desc: RemoteCommandExecutor::ProcessOneCommand function test
293 * @tc.type: FUNC
294 * @tc.require:
295 */
296 HWTEST_F(TokenSyncServiceTest, ProcessOneCommand002, TestSize.Level0)
297 {
298 std::string nodeId = "test_nodeId";
299 auto executor = std::make_shared<RemoteCommandExecutor>(nodeId);
300 auto cmd = std::make_shared<TestBaseRemoteCommand>();
301 cmd->remoteProtocol_.statusCode = Constant::FAILURE;
302 EXPECT_EQ(Constant::FAILURE, executor->ProcessOneCommand(cmd));
303 }
304
305 /**
306 * @tc.name: ProcessOneCommand003
307 * @tc.desc: RemoteCommandExecutor::ProcessOneCommand function test with status code 0
308 * @tc.type: FUNC
309 * @tc.require:
310 */
311 HWTEST_F(TokenSyncServiceTest, ProcessOneCommand003, TestSize.Level0)
312 {
313 std::string nodeId = ConstantCommon::GetLocalDeviceId();
314 auto executor = std::make_shared<RemoteCommandExecutor>(nodeId);
315 auto cmd = std::make_shared<TestBaseRemoteCommand>();
316 cmd->remoteProtocol_.statusCode = Constant::SUCCESS;
317 EXPECT_EQ(Constant::FAILURE, executor->ProcessOneCommand(cmd));
318 }
319
320 /**
321 * @tc.name: AddCommand001
322 * @tc.desc: RemoteCommandExecutor::AddCommand function test with nullptr
323 * @tc.type: FUNC
324 * @tc.require:
325 */
326 HWTEST_F(TokenSyncServiceTest, AddCommand001, TestSize.Level0)
327 {
328 std::string nodeId = "test_nodeId";
329 auto executor = std::make_shared<RemoteCommandExecutor>(nodeId);
330 EXPECT_EQ(Constant::INVALID_COMMAND, executor->AddCommand(nullptr));
331 }
332
333 /**
334 * @tc.name: AddCommand002
335 * @tc.desc: RemoteCommandExecutor::AddCommand function test
336 * @tc.type: FUNC
337 * @tc.require:
338 */
339 HWTEST_F(TokenSyncServiceTest, AddCommand002, TestSize.Level0)
340 {
341 std::string nodeId = "test_nodeId";
342 auto executor = std::make_shared<RemoteCommandExecutor>(nodeId);
343 auto cmd = std::make_shared<TestBaseRemoteCommand>();
344 EXPECT_EQ(Constant::SUCCESS, executor->AddCommand(cmd));
345 }
346
347 /**
348 * @tc.name: ProcessBufferedCommands001
349 * @tc.desc: RemoteCommandExecutor::ProcessBufferedCommands function test
350 * @tc.type: FUNC
351 * @tc.require:
352 */
353 HWTEST_F(TokenSyncServiceTest, ProcessBufferedCommands001, TestSize.Level0)
354 {
355 std::string nodeId = "test_nodeId";
356 auto executor = std::make_shared<RemoteCommandExecutor>(nodeId);
357 executor->commands_.clear();
358 EXPECT_EQ(Constant::SUCCESS, executor->ProcessBufferedCommands());
359 }
360
361 /**
362 * @tc.name: ProcessBufferedCommands002
363 * @tc.desc: RemoteCommandExecutor::ProcessBufferedCommands function test
364 * @tc.type: FUNC
365 * @tc.require:
366 */
367 HWTEST_F(TokenSyncServiceTest, ProcessBufferedCommands002, TestSize.Level0)
368 {
369 std::string nodeId = "test_nodeId";
370 auto executor = std::make_shared<RemoteCommandExecutor>(nodeId);
371 auto cmd = std::make_shared<TestBaseRemoteCommand>();
372 executor->commands_.emplace_back(cmd);
373 EXPECT_EQ(Constant::SUCCESS, executor->ProcessBufferedCommands());
374 }
375
376 /**
377 * @tc.name: ProcessBufferedCommands003
378 * @tc.desc: RemoteCommandExecutor::ProcessBufferedCommands function test
379 * @tc.type: FUNC
380 * @tc.require:
381 */
382 HWTEST_F(TokenSyncServiceTest, ProcessBufferedCommands003, TestSize.Level0)
383 {
384 std::string nodeId = "test_nodeId";
385 auto executor = std::make_shared<RemoteCommandExecutor>(nodeId);
386 auto cmd = std::make_shared<TestBaseRemoteCommand>();
387 cmd->remoteProtocol_.statusCode = Constant::FAILURE_BUT_CAN_RETRY;
388 executor->commands_.emplace_back(cmd);
389 EXPECT_EQ(Constant::FAILURE, executor->ProcessBufferedCommands());
390 }
391
392 /**
393 * @tc.name: ProcessBufferedCommands004
394 * @tc.desc: RemoteCommandExecutor::ProcessBufferedCommands function test
395 * @tc.type: FUNC
396 * @tc.require:
397 */
398 HWTEST_F(TokenSyncServiceTest, ProcessBufferedCommands004, TestSize.Level0)
399 {
400 std::string nodeId = "test_nodeId";
401 auto executor = std::make_shared<RemoteCommandExecutor>(nodeId);
402 auto cmd = std::make_shared<TestBaseRemoteCommand>();
403 cmd->remoteProtocol_.statusCode = -3; // other error code
404 executor->commands_.emplace_back(cmd);
405 EXPECT_EQ(Constant::SUCCESS, executor->ProcessBufferedCommands());
406 }
407
408 /**
409 * @tc.name: ClientProcessResult001
410 * @tc.desc: RemoteCommandExecutor::ClientProcessResult function test
411 * @tc.type: FUNC
412 * @tc.require:
413 */
414 HWTEST_F(TokenSyncServiceTest, ClientProcessResult001, TestSize.Level0)
415 {
416 std::string nodeId = "test_nodeId";
417 auto executor = std::make_shared<RemoteCommandExecutor>(nodeId);
418 auto cmd = std::make_shared<TestBaseRemoteCommand>();
419 cmd->remoteProtocol_.statusCode = Constant::STATUS_CODE_BEFORE_RPC;
420 EXPECT_EQ(Constant::FAILURE, executor->ClientProcessResult(cmd));
421 }
422
423 /**
424 * @tc.name: ClientProcessResult002
425 * @tc.desc: RemoteCommandExecutor::ClientProcessResult function test
426 * @tc.type: FUNC
427 * @tc.require:
428 */
429 HWTEST_F(TokenSyncServiceTest, ClientProcessResult002, TestSize.Level0)
430 {
431 std::string nodeId = ConstantCommon::GetLocalDeviceId();
432 auto executor = std::make_shared<RemoteCommandExecutor>(nodeId);
433 auto cmd = std::make_shared<TestBaseRemoteCommand>();
434 cmd->remoteProtocol_.statusCode = Constant::SUCCESS;
435 EXPECT_EQ(Constant::SUCCESS, executor->ClientProcessResult(cmd));
436 cmd->remoteProtocol_.statusCode = Constant::FAILURE;
437 EXPECT_EQ(Constant::FAILURE, executor->ClientProcessResult(cmd));
438 }
439
440 /**
441 * @tc.name: ToNativeTokenInfoJson001
442 * @tc.desc: ToNativeTokenInfoJson function test
443 * @tc.type: FUNC
444 * @tc.require:
445 */
446 HWTEST_F(TokenSyncServiceTest, ToNativeTokenInfoJson001, TestSize.Level0)
447 {
448 NativeTokenInfoBase native1 = {
449 .ver = 1,
450 .processName = "token_sync_test",
451 .dcap = {"AT_CAP"},
452 .tokenID = 1,
453 .tokenAttr = 0,
454 .nativeAcls = {},
455 };
456 auto cmd = std::make_shared<TestBaseRemoteCommand>();
457 EXPECT_NE(nullptr, cmd->ToNativeTokenInfoJson(native1));
458 }
459
460 /**
461 * @tc.name: FromPermStateListJson001
462 * @tc.desc: FromPermStateListJson function test
463 * @tc.type: FUNC
464 * @tc.require:
465 */
466 HWTEST_F(TokenSyncServiceTest, FromPermStateListJson001, TestSize.Level0)
467 {
468 HapTokenInfo baseInfo = {
469 .ver = 1,
470 .userID = 1,
471 .bundleName = "com.ohos.access_token",
472 .instIndex = 1,
473 .tokenID = 0x20100000,
474 .tokenAttr = 0
475 };
476
477 PermissionStatus infoManagerTestState = {
478 .permissionName = "ohos.permission.test1",
479 .grantStatus = PermissionState::PERMISSION_GRANTED,
480 .grantFlag = PermissionFlag::PERMISSION_SYSTEM_FIXED};
481 std::vector<PermissionStatus> permStateList;
482 permStateList.emplace_back(infoManagerTestState);
483
484 HapTokenInfoForSync remoteTokenInfo = {
485 .baseInfo = baseInfo,
486 .permStateList = permStateList
487 };
488 CJsonUnique hapTokenJson;
489 auto cmd = std::make_shared<TestBaseRemoteCommand>();
490 hapTokenJson = cmd->ToHapTokenInfosJson(remoteTokenInfo);
491
492 HapTokenInfoForSync hap;
493 cmd->FromHapTokenBasicInfoJson(hapTokenJson.get(), hap.baseInfo);
494 cmd->FromPermStateListJson(hapTokenJson.get(), hap.permStateList);
495
496 PermissionStatus state1 = {
497 .permissionName = "ohos.permission.test1",
498 .grantStatus = PermissionState::PERMISSION_GRANTED,
499 .grantFlag = PermissionFlag::PERMISSION_SYSTEM_FIXED};
500 CJsonUnique permStateJson = CreateJson();
501 cmd->ToPermStateJson(permStateJson.get(), state1);
502
503 PermissionStatus state2 = {
504 .permissionName = "ohos.permission.test1",
505 .grantStatus = PermissionState::PERMISSION_GRANTED,
506 .grantFlag = PermissionFlag::PERMISSION_SYSTEM_FIXED};
507 cmd->ToPermStateJson(permStateJson.get(), state2);
508
509 EXPECT_EQ(hap.baseInfo.tokenID, remoteTokenInfo.baseInfo.tokenID);
510 }
511
512 /**
513 * @tc.name: FromNativeTokenInfoJson001
514 * @tc.desc: FromNativeTokenInfoJson function test
515 * @tc.type: FUNC
516 * @tc.require:
517 */
518 HWTEST_F(TokenSyncServiceTest, FromNativeTokenInfoJson001, TestSize.Level0)
519 {
520 auto cmd = std::make_shared<TestBaseRemoteCommand>();
521
522 CJsonUnique nativeTokenListJsonNull = CreateJson();
523 NativeTokenInfoBase tokenNull;
524 cmd->FromNativeTokenInfoJson(nativeTokenListJsonNull.get(), tokenNull);
525
526 CJsonUnique hapTokenJsonNull = CreateJson();
527 HapTokenInfo hapTokenBasicInfoNull;
528 cmd->FromHapTokenBasicInfoJson(hapTokenJsonNull.get(), hapTokenBasicInfoNull);
529
530 NativeTokenInfoBase native1 = {
531 .apl = APL_NORMAL,
532 .ver = 2,
533 .processName = "token_sync_test",
534 .dcap = {"AT_CAP"},
535 .tokenID = 1,
536 .tokenAttr = 0,
537 .nativeAcls = {},
538 };
539
540 CJsonUnique nativeTokenListJson = cmd->ToNativeTokenInfoJson(native1);
541 NativeTokenInfoBase token;
542 cmd->FromNativeTokenInfoJson(nativeTokenListJson.get(), token);
543 EXPECT_EQ(token.processName, "token_sync_test");
544 EXPECT_EQ(token.apl, ATokenAplEnum::APL_NORMAL);
545 }
546
547 /**
548 * @tc.name: FromPermStateListJson002
549 * @tc.desc: FromPermStateListJson function test
550 * @tc.type: FUNC
551 * @tc.require:
552 */
553 HWTEST_F(TokenSyncServiceTest, FromPermStateListJson002, TestSize.Level0)
554 {
555 auto cmd = std::make_shared<TestBaseRemoteCommand>();
556
557 CJsonUnique hapTokenJsonNull = CreateJsonFromString("{\\\"bundleName\\\":\\\"\\\","
558 "\\\"instIndex\\\":0,\\\"permState\\\":[{\\\"permissionName\\\":\\\"TEST\\\", "
559 "\\\"grantStatus\\\":0, \\\"grantFlag\\\":0}],\\\"tokenAttr\\\":0,"
560 "\\\"tokenID\\\":111,\\\"userID\\\":0,\\\"version\\\":1}");
561
562 std::vector<PermissionStatus> permStateListNull;
563 cmd->FromPermStateListJson(hapTokenJsonNull.get(), permStateListNull);
564 EXPECT_EQ(permStateListNull.size(), 0);
565
566 hapTokenJsonNull = CreateJsonFromString("{\\\"bundleName\\\":\\\"\\\","
567 "\\\"instIndex\\\":0,\\\"permState\\\":[{\\\"permissionName\\\":\\\"TEST\\\"}],"
568 "\\\"tokenAttr\\\":0,\\\"tokenID\\\":111,\\\"userID\\\":0,\\\"version\\\":1}");
569 cmd->FromPermStateListJson(hapTokenJsonNull.get(), permStateListNull);
570 EXPECT_EQ(permStateListNull.size(), 0);
571
572 hapTokenJsonNull = CreateJsonFromString("{\\\"bundleName\\\":\\\"\\\","
573 "\\\"instIndex\\\":0,\\\"permState\\\":[{\\\"permissionName\\\":\\\"TEST\\\"}],"
574 "\\\"tokenAttr\\\":0,\\\"tokenID\\\":111,\\\"userID\\\":0,\\\"version\\\":1}");
575 cmd->FromPermStateListJson(hapTokenJsonNull.get(), permStateListNull);
576 EXPECT_EQ(permStateListNull.size(), 0);
577
578 hapTokenJsonNull = CreateJsonFromString("{\\\"bundleName\\\":\\\"\\\","
579 "\\\"instIndex\\\":0,\\\"permState\\\":[{\\\"permissionName\\\":\\\"TEST\\\", "
580 "\\\"grantStatus\\\":0, \\\"grantFlag\\\":0}],\\\"tokenAttr\\\":0,"
581 "\\\"tokenID\\\":111,\\\"userID\\\":0,\\\"version\\\":1}");
582 cmd->FromPermStateListJson(hapTokenJsonNull.get(), permStateListNull);
583 EXPECT_EQ(permStateListNull.size(), 0);
584 }
585
586 /**
587 * @tc.name: GetRemoteHapTokenInfo002
588 * @tc.desc: test remote hap recv func
589 * @tc.type: FUNC
590 * @tc.require:AR000GK6T5 AR000GK6T9
591 */
592 HWTEST_F(TokenSyncServiceTest, GetRemoteHapTokenInfo002, TestSize.Level0)
593 {
594 LOGI(ATM_DOMAIN, ATM_TAG, "GetRemoteHapTokenInfo002 start.");
595
596 ResetUuidMock();
597
598 AccessTokenID tokenId = 0;
599 DeleteAndAllocToken(tokenId);
600
601 std::string jsonBefore =
602 "{\"commandName\":\"SyncRemoteHapTokenCommand\",\"id\":\"0065e65f-\",\"jsonPayload\":"
603 "\"{\\\"HapTokenInfo\\\":{\\\"bundleName\\\":\\\"\\\","
604 "\\\"instIndex\\\":0,\\\"permState\\\":null,\\\"tokenAttr\\\":0,"
605 "\\\"tokenID\\\":0,\\\"userID\\\":0,\\\"version\\\":1},\\\"commandName\\\":\\\"SyncRemoteHapTokenCommand\\\","
606 "\\\"dstDeviceId\\\":\\\"local:udid-001\\\",\\\"dstDeviceLevel\\\":\\\"\\\",\\\"message\\\":\\\"success\\\","
607 "\\\"requestTokenId\\\":";
608 std::string jsonAfter = ",\\\"requestVersion\\\":2,\\\"responseDeviceId\\\":\\\"\\\",\\\"responseVersion\\\":2,"
609 "\\\"srcDeviceId\\\":\\\"deviceid-1:udid-001\\\",\\\"srcDeviceLevel\\\":\\\"\\\",\\\"statusCode\\\":100001,"
610 "\\\"uniqueId\\\":\\\"SyncRemoteHapTokenCommand\\\"}\",\"type\":\"request\"}";
611 std::string recvJson = jsonBefore + std::to_string(tokenId) + jsonAfter;
612
613 unsigned char *recvBuffer = (unsigned char *)malloc(0x1000);
614 int recvLen = 0x1000;
615 CompressMock(recvJson, recvBuffer, recvLen);
616
617 ResetSendMessFlagMock();
618
619 g_ptrDeviceStateCallback->OnDeviceOnline(g_devInfo); // create channel
620
621 char networkId[DEVICEID_MAX_LEN + 1];
622 char pkgName[SOCKET_NAME_MAX_LEN + 1];
623 char peerName[SOCKET_NAME_MAX_LEN + 1];
624 strcpy_s(networkId, DEVICEID_MAX_LEN, "deviceid-1:udid-001");
625 strcpy_s(pkgName, SOCKET_NAME_MAX_LEN, TOKEN_SYNC_PACKAGE_NAME.c_str());
626 strcpy_s(peerName, SOCKET_NAME_MAX_LEN, TOKEN_SYNC_SOCKET_NAME.c_str());
627 PeerSocketInfo info = {
628 .name = peerName,
629 .networkId = networkId,
630 .pkgName = pkgName
631 };
632 SoftBusSocketListener::OnBind(1, info);
633 SoftBusSocketListener::OnClientBytes(1, recvBuffer, recvLen);
634 int count = 0;
635 while (!GetSendMessFlagMock() && count < MAX_RETRY_TIMES) {
636 sleep(1);
637 count++;
638 }
639 free(recvBuffer);
640
641 ResetSendMessFlagMock();
642 std::string uuidMessage = GetUuidMock();
643 ASSERT_EQ(uuidMessage, "0065e65f-");
644 }
645
646 /**
647 * @tc.name: GetRemoteHapTokenInfo003
648 * @tc.desc: test remote hap send func, but get tokenInfo is wrong
649 * @tc.type: FUNC
650 * @tc.require:AR000GK6T5 AR000GK6T9
651 */
652 HWTEST_F(TokenSyncServiceTest, GetRemoteHapTokenInfo003, TestSize.Level0)
653 {
654 LOGI(ATM_DOMAIN, ATM_TAG, "GetRemoteHapTokenInfo003 start.");
655 g_jsonBefore = "{\"commandName\":\"SyncRemoteHapTokenCommand\", \"id\":\"";
656 // apl is error
657 g_jsonAfter =
658 "\",\"jsonPayload\":\"{\\\"HapTokenInfo\\\":{\\\"bundleName\\\":\\\"mock_token_sync\\\","
659 "\\\"instIndex\\\":0,\\\"permState\\\":null,\\\"tokenAttr\\\":0,\\\"tokenID\\\":537919488,"
660 "\\\"userID\\\":0,\\\"version\\\":1},\\\"commandName\\\":\\\"SyncRemoteHapTokenCommand\\\","
661 "\\\"dstDeviceId\\\":\\\"deviceid-1:udid-001\\\","
662 "\\\"dstDeviceLevel\\\":\\\"\\\",\\\"message\\\":\\\"success\\\","
663 "\\\"requestTokenId\\\":537919488,\\\"requestVersion\\\":2,\\\"responseDeviceId\\\":\\\"deviceid-1:udid-001\\\""
664 ",\\\"responseVersion\\\":2,\\\"srcDeviceId\\\":\\\"local:udid-001\\\",\\\"srcDeviceLevel\\\":\\\"\\\","
665 "\\\"statusCode\\\":0,\\\"uniqueId\\\":\\\"SyncRemoteHapTokenCommand\\\"}\",\"type\":\"response\"}";
666
667 g_ptrDeviceStateCallback->OnDeviceOnline(g_devInfo);
668 sleep(3);
669 threads_.emplace_back(std::thread(SendTaskThread));
670
671 OHOS::DelayedSingleton<TokenSyncManagerService>::GetInstance()->GetRemoteHapTokenInfo(
672 g_udid, 0x20100000);
673
674 AccessTokenID mapID = AccessTokenKit::AllocLocalTokenID(g_udid, 0x20100000);
675 ASSERT_EQ(mapID, static_cast<AccessTokenID>(0));
676 }
677
678 /**
679 * @tc.name: GetRemoteHapTokenInfo004
680 * @tc.desc: test remote hap send func, but json payload lost parameter
681 * @tc.type: FUNC
682 * @tc.require:AR000GK6T5 AR000GK6T9
683 */
684 HWTEST_F(TokenSyncServiceTest, GetRemoteHapTokenInfo004, TestSize.Level0)
685 {
686 LOGI(ATM_DOMAIN, ATM_TAG, "GetRemoteHapTokenInfo004 start.");
687 g_jsonBefore = "{\"commandName\":\"SyncRemoteHapTokenCommand\", \"id\":\"";
688 // lost tokenID
689 g_jsonAfter =
690 "\",\"jsonPayload\":\"{\\\"HapTokenInfo\\\":{\\\"bundleName\\\":\\\"mock_token_sync\\\","
691 "\\\"permState\\\":null,\\\"tokenAttr\\\":0,"
692 "\\\"userID\\\":0,\\\"version\\\":1},\\\"commandName\\\":\\\"SyncRemoteHapTokenCommand\\\","
693 "\\\"dstDeviceId\\\":\\\"deviceid-1:udid-001\\\","
694 "\\\"dstDeviceLevel\\\":\\\"\\\",\\\"message\\\":\\\"success\\\","
695 "\\\"requestTokenId\\\":537919488,\\\"requestVersion\\\":2,\\\"responseDeviceId\\\":\\\"deviceid-1:udid-001\\\""
696 ",\\\"responseVersion\\\":2,\\\"srcDeviceId\\\":\\\"local:udid-001\\\",\\\"srcDeviceLevel\\\":\\\"\\\","
697 "\\\"statusCode\\\":0,\\\"uniqueId\\\":\\\"SyncRemoteHapTokenCommand\\\"}\",\"type\":\"response\"}";
698
699 g_ptrDeviceStateCallback->OnDeviceOnline(g_devInfo);
700 sleep(3);
701 threads_.emplace_back(std::thread(SendTaskThread));
702
703 OHOS::DelayedSingleton<TokenSyncManagerService>::GetInstance()->GetRemoteHapTokenInfo(
704 g_udid, 0x20100000);
705
706 AccessTokenID mapID = AccessTokenKit::AllocLocalTokenID(g_udid, 0x20100000);
707 ASSERT_EQ(mapID, static_cast<AccessTokenID>(0));
708 }
709
710 /**
711 * @tc.name: GetRemoteHapTokenInfo005
712 * @tc.desc: test remote hap send func, but json payload parameter type is wrong
713 * @tc.type: FUNC
714 * @tc.require:AR000GK6T5 AR000GK6T9
715 */
716 HWTEST_F(TokenSyncServiceTest, GetRemoteHapTokenInfo005, TestSize.Level0)
717 {
718 LOGI(ATM_DOMAIN, ATM_TAG, "GetRemoteHapTokenInfo005 start.");
719 g_jsonBefore = "{\"commandName\":\"SyncRemoteHapTokenCommand\", \"id\":\"";
720 // instIndex is not number
721 g_jsonAfter =
722 "\",\"jsonPayload\":\"{\\\"HapTokenInfo\\\":{\\\"bundleName\\\":\\\"mock_token_sync\\\","
723 "\\\"instIndex\\\":1,\\\"permState\\\":null,\\\"tokenAttr\\\":0,"
724 "\\\"tokenID\\\":\\\"aaa\\\","
725 "\\\"userID\\\":0,\\\"version\\\":1},\\\"commandName\\\":\\\"SyncRemoteHapTokenCommand\\\","
726 "\\\"dstDeviceId\\\":\\\"deviceid-1:udid-001\\\","
727 "\\\"dstDeviceLevel\\\":\\\"\\\",\\\"message\\\":\\\"success\\\","
728 "\\\"requestTokenId\\\":537919488,\\\"requestVersion\\\":2,\\\"responseDeviceId\\\":\\\"deviceid-1:udid-001\\\""
729 ",\\\"responseVersion\\\":2,\\\"srcDeviceId\\\":\\\"local:udid-001\\\",\\\"srcDeviceLevel\\\":\\\"\\\","
730 "\\\"statusCode\\\":0,\\\"uniqueId\\\":\\\"SyncRemoteHapTokenCommand\\\"}\",\"type\":\"response\"}";
731
732 g_ptrDeviceStateCallback->OnDeviceOnline(g_devInfo);
733 sleep(3);
734
735 threads_.emplace_back(std::thread(SendTaskThread));
736 OHOS::DelayedSingleton<TokenSyncManagerService>::GetInstance()->GetRemoteHapTokenInfo(
737 g_udid, 0x20100000);
738
739 AccessTokenID mapID = AccessTokenKit::AllocLocalTokenID(g_udid, 0x20100000);
740 ASSERT_EQ(mapID, static_cast<AccessTokenID>(0));
741 }
742
743 /**
744 * @tc.name: GetRemoteHapTokenInfo006
745 * @tc.desc: test remote hap send func, but json payload parameter format is wrong
746 * @tc.type: FUNC
747 * @tc.require:AR000GK6T5 AR000GK6T9
748 */
749 HWTEST_F(TokenSyncServiceTest, GetRemoteHapTokenInfo006, TestSize.Level0)
750 {
751 LOGI(ATM_DOMAIN, ATM_TAG, "GetRemoteHapTokenInfo006 start.");
752 g_jsonBefore = "{\"commandName\":\"SyncRemoteHapTokenCommand\", \"id\":\"";
753 // mock_token_sync lost \\\"
754 g_jsonAfter =
755 "\",\"jsonPayload\":\"{\\\"HapTokenInfo\\\":{\\\"bundleName\\\":\\\"mock_token_sync,"
756 "\\\"111111\\\",\\\"instIndex\\\":1,\\\"permState\\\":null,\\\"tokenAttr\\\":0,"
757 "\\\"tokenID\\\":537919488,"
758 "\\\"userID\\\":0,\\\"version\\\":1},\\\"commandName\\\":\\\"SyncRemoteHapTokenCommand\\\","
759 "\\\"dstDeviceId\\\":\\\"deviceid-1:udid-001\\\","
760 "\\\"dstDeviceLevel\\\":\\\"\\\",\\\"message\\\":\\\"success\\\","
761 "\\\"requestTokenId\\\":537919488,\\\"requestVersion\\\":2,\\\"responseDeviceId\\\":\\\"deviceid-1:udid-001\\\""
762 ",\\\"responseVersion\\\":2,\\\"srcDeviceId\\\":\\\"local:udid-001\\\",\\\"srcDeviceLevel\\\":\\\"\\\","
763 "\\\"statusCode\\\":0,\\\"uniqueId\\\":\\\"SyncRemoteHapTokenCommand\\\"}\",\"type\":\"response\"}";
764
765 g_ptrDeviceStateCallback->OnDeviceOnline(g_devInfo);
766 sleep(3);
767
768 threads_.emplace_back(std::thread(SendTaskThread));
769
770 OHOS::DelayedSingleton<TokenSyncManagerService>::GetInstance()->GetRemoteHapTokenInfo(
771 g_udid, 0x20100000);
772
773 AccessTokenID mapID = AccessTokenKit::AllocLocalTokenID(g_udid, 0x20100000);
774 ASSERT_EQ(mapID, static_cast<AccessTokenID>(0));
775 }
776
777 /**
778 * @tc.name: GetRemoteHapTokenInfo007
779 * @tc.desc: test remote hap send func, statusCode is wrong
780 * @tc.type: FUNC
781 * @tc.require:AR000GK6T5 AR000GK6T9
782 */
783 HWTEST_F(TokenSyncServiceTest, GetRemoteHapTokenInfo007, TestSize.Level0)
784 {
785 LOGI(ATM_DOMAIN, ATM_TAG, "GetRemoteHapTokenInfo007 start.");
786 g_jsonBefore = "{\"commandName\":\"SyncRemoteHapTokenCommand\", \"id\":\"";
787 // statusCode error
788 g_jsonAfter =
789 "\",\"jsonPayload\":\"{\\\"HapTokenInfo\\\":{\\\"bundleName\\\":\\\"mock_token_sync\\\","
790 "\\\"instIndex\\\":1,\\\"permState\\\":null,\\\"tokenAttr\\\":0,"
791 "\\\"tokenID\\\":537919488,"
792 "\\\"userID\\\":0,\\\"version\\\":1},\\\"commandName\\\":\\\"SyncRemoteHapTokenCommand\\\","
793 "\\\"dstDeviceId\\\":\\\"deviceid-1\\\",\\\"dstDeviceLevel\\\":\\\"\\\",\\\"message\\\":\\\"success\\\","
794 "\\\"requestTokenId\\\":537919488,\\\"requestVersion\\\":2,\\\"responseDeviceId\\\":\\\"deviceid-1:udid-001\\\""
795 ",\\\"responseVersion\\\":2,\\\"srcDeviceId\\\":\\\"local:udid-001\\\",\\\"srcDeviceLevel\\\":\\\"\\\","
796 "\\\"statusCode\\\":-2,\\\"uniqueId\\\":\\\"SyncRemoteHapTokenCommand\\\"}\","
797 "\"type\":\"response\"}";
798
799 g_ptrDeviceStateCallback->OnDeviceOnline(g_devInfo);
800 sleep(3);
801 threads_.emplace_back(std::thread(SendTaskThread));
802
803 OHOS::DelayedSingleton<TokenSyncManagerService>::GetInstance()->GetRemoteHapTokenInfo(
804 g_udid, 0x20100000);
805
806 AccessTokenID mapID = AccessTokenKit::AllocLocalTokenID(g_udid, 0x20100000);
807 ASSERT_EQ(mapID, static_cast<AccessTokenID>(0));
808 }
809
810 /**
811 * @tc.name: GetRemoteHapTokenInfo008
812 * @tc.desc: test remote hap recv func, tokenID is not exist
813 * @tc.type: FUNC
814 * @tc.require:AR000GK6T5 AR000GK6T9
815 */
816 HWTEST_F(TokenSyncServiceTest, GetRemoteHapTokenInfo008, TestSize.Level0)
817 {
818 LOGI(ATM_DOMAIN, ATM_TAG, "GetRemoteHapTokenInfo008 start.");
819 // create local token
820 AccessTokenIDEx tokenIdEx = TestCommon::GetHapTokenIdFromBundle(g_infoManagerTestInfoParms.userID,
821 g_infoManagerTestInfoParms.bundleName, g_infoManagerTestInfoParms.instIndex);
822 AccessTokenID tokenID = tokenIdEx.tokenIdExStruct.tokenID;
823 TestCommon::DeleteTestHapToken(tokenID);
824
825 // tokenID is not exist
826 std::string jsonBefore =
827 "{\"commandName\":\"SyncRemoteHapTokenCommand\",\"id\":\"0065e65f-\",\"jsonPayload\":"
828 "\"{\\\"HapTokenInfo\\\":{\\\"bundleName\\\":\\\"\\\","
829 "\\\"instIndex\\\":0,\\\"permState\\\":null,\\\"tokenAttr\\\":0,"
830 "\\\"tokenID\\\":0,\\\"userID\\\":0,\\\"version\\\":1},\\\"commandName\\\":\\\"SyncRemoteHapTokenCommand\\\","
831 "\\\"dstDeviceId\\\":\\\"local:udid-001\\\",\\\"dstDeviceLevel\\\":\\\"\\\",\\\"message\\\":\\\"success\\\","
832 "\\\"requestTokenId\\\":";
833 std::string tokenJsonStr = std::to_string(tokenID);
834 std::string jsonAfter = ",\\\"requestVersion\\\":2,\\\"responseDeviceId\\\":\\\"\\\",\\\"responseVersion\\\":2,"
835 "\\\"srcDeviceId\\\":\\\"deviceid-1:udid-001\\\",\\\"srcDeviceLevel\\\":\\\"\\\",\\\"statusCode\\\":100001,"
836 "\\\"uniqueId\\\":\\\"SyncRemoteHapTokenCommand\\\"}\",\"type\":\"request\"}";
837
838 // create recv message
839 std::string recvJson = jsonBefore + tokenJsonStr + jsonAfter;
840 unsigned char *recvBuffer = (unsigned char *)malloc(0x1000);
841 int recvLen = 0x1000;
842 CompressMock(recvJson, recvBuffer, recvLen);
843
844 g_ptrDeviceStateCallback->OnDeviceOnline(g_devInfo);
845
846 ResetSendMessFlagMock();
847 SoftBusSocketListener::OnClientBytes(1, recvBuffer, recvLen);
848
849 int count = 0;
850 while (!GetSendMessFlagMock() && count < MAX_RETRY_TIMES) {
851 sleep(1);
852 count++;
853 }
854 free(recvBuffer);
855 AccessTokenID mapID = AccessTokenKit::AllocLocalTokenID(g_udid, 0);
856 ASSERT_EQ(mapID, static_cast<AccessTokenID>(0));
857 }
858
859 /**
860 * @tc.name: DeleteRemoteTokenCommand001
861 * @tc.desc: test delete remote token command
862 * @tc.type: FUNC
863 * @tc.require: Issue Number
864 */
865 HWTEST_F(TokenSyncServiceTest, DeleteRemoteTokenCommand001, TestSize.Level0)
866 {
867 std::string srcDeviceId = "001";
868 std::string dstDeviceId = "002";
869 AccessTokenID tokenID = 1;
870 std::shared_ptr<DeleteRemoteTokenCommand> deleteRemoteTokenCommand =
871 RemoteCommandFactory::GetInstance().NewDeleteRemoteTokenCommand(srcDeviceId, dstDeviceId, tokenID);
872 ASSERT_EQ(deleteRemoteTokenCommand->remoteProtocol_.commandName, "DeleteRemoteTokenCommand");
873 ASSERT_EQ(deleteRemoteTokenCommand->remoteProtocol_.uniqueId, "DeleteRemoteTokenCommand");
874 ASSERT_EQ(deleteRemoteTokenCommand->remoteProtocol_.srcDeviceId, srcDeviceId);
875 ASSERT_EQ(deleteRemoteTokenCommand->remoteProtocol_.dstDeviceId, dstDeviceId);
876 ASSERT_EQ(
877 // 2 is DISTRIBUTED_ACCESS_TOKEN_SERVICE_VERSION
878 deleteRemoteTokenCommand->remoteProtocol_.responseVersion, 2);
879 ASSERT_EQ(
880 // 2 is DISTRIBUTED_ACCESS_TOKEN_SERVICE_VERSION
881 deleteRemoteTokenCommand->remoteProtocol_.requestVersion, 2);
882
883 deleteRemoteTokenCommand->Execute();
884 deleteRemoteTokenCommand->Finish();
885 ASSERT_EQ(deleteRemoteTokenCommand->remoteProtocol_.statusCode, Constant::SUCCESS);
886 }
887
888 /**
889 * @tc.name: NewUpdateRemoteHapTokenCommand001
890 * @tc.desc: test delete remote token command
891 * @tc.type: FUNC
892 * @tc.require: Issue Number
893 */
894 HWTEST_F(TokenSyncServiceTest, NewUpdateRemoteHapTokenCommand001, TestSize.Level0)
895 {
896 std::string srcDeviceId = "001";
897 std::string dstDeviceId = "002";
898 HapTokenInfoForSync tokenInfo;
899 std::shared_ptr<UpdateRemoteHapTokenCommand> command =
900 RemoteCommandFactory::GetInstance().NewUpdateRemoteHapTokenCommand(srcDeviceId, dstDeviceId, tokenInfo);
901 ASSERT_EQ(command->remoteProtocol_.commandName, "UpdateRemoteHapTokenCommand");
902 ASSERT_EQ(command->remoteProtocol_.uniqueId, "UpdateRemoteHapTokenCommand");
903 command->Execute();
904 command->Finish();
905 ASSERT_EQ(command->remoteProtocol_.statusCode, Constant::SUCCESS);
906 }
907
908 /**
909 * @tc.name: AddDeviceInfo001
910 * @tc.desc: DeviceInfoManager::AddDeviceInfo function test
911 * @tc.type: FUNC
912 * @tc.require:
913 */
914 HWTEST_F(TokenSyncServiceTest, AddDeviceInfo001, TestSize.Level0)
915 {
916 std::string networkId;
917 std::string universallyUniqueId;
918 std::string uniqueDeviceId;
919 std::string deviceName;
920 std::string deviceType;
921 ASSERT_EQ("", networkId);
922 ASSERT_EQ("", universallyUniqueId);
923 ASSERT_EQ("", uniqueDeviceId);
924 ASSERT_EQ("", deviceName);
925 ASSERT_EQ("", deviceType);
926 DeviceInfoManager::GetInstance().AddDeviceInfo(networkId, universallyUniqueId, uniqueDeviceId, deviceName,
927 deviceType); // all empty
928
929 networkId = "123";
930 universallyUniqueId = "123";
931 uniqueDeviceId = "123";
932 deviceName = "123";
933 deviceType = "123";
934 ASSERT_NE("", networkId);
935 ASSERT_NE("", universallyUniqueId);
936 ASSERT_NE("", uniqueDeviceId);
937 ASSERT_NE("", deviceName);
938 ASSERT_NE("", deviceType);
939 DeviceInfoManager::GetInstance().AddDeviceInfo(networkId, universallyUniqueId, uniqueDeviceId, deviceName,
940 deviceType); // all valued
941
942 std::string nodeId = uniqueDeviceId;
943 DeviceIdType type = DeviceIdType::UNIQUE_DISABILITY_ID;
944 DeviceInfoRepository::GetInstance().DeleteDeviceInfo(nodeId, type); // delete 123
945 }
946
947 /**
948 * @tc.name: RemoveAllRemoteDeviceInfo001
949 * @tc.desc: DeviceInfoManager::RemoveAllRemoteDeviceInfo function test
950 * @tc.type: FUNC
951 * @tc.require:
952 */
953 HWTEST_F(TokenSyncServiceTest, RemoveAllRemoteDeviceInfo001, TestSize.Level0)
954 {
955 DeviceInfoManager::GetInstance().RemoveAllRemoteDeviceInfo(); // FindDeviceInfo false
956
957 std::string networkId = "123";
958 std::string universallyUniqueId = "123";
959 std::string uniqueDeviceId;
960 std::string deviceName = "123";
961 std::string deviceType = "123";
962 uniqueDeviceId = ConstantCommon::GetLocalDeviceId();
963 ASSERT_EQ("local:udid-001", uniqueDeviceId);
964
965 DeviceInfoManager::GetInstance().AddDeviceInfo(networkId, universallyUniqueId, uniqueDeviceId, deviceName,
966 deviceType);
967 DeviceInfoManager::GetInstance().RemoveAllRemoteDeviceInfo(); // FindDeviceInfo true
968
969 std::string nodeId = uniqueDeviceId;
970 DeviceIdType type = DeviceIdType::UNIQUE_DISABILITY_ID;
971 DeviceInfoRepository::GetInstance().DeleteDeviceInfo(nodeId, type); // delete 123
972 }
973
974 /**
975 * @tc.name: RemoveRemoteDeviceInfo001
976 * @tc.desc: DeviceInfoManager::RemoveRemoteDeviceInfo function test
977 * @tc.type: FUNC
978 * @tc.require:
979 */
980 HWTEST_F(TokenSyncServiceTest, RemoveRemoteDeviceInfo001, TestSize.Level0)
981 {
982 std::string nodeId;
983 DeviceIdType deviceIdType = DeviceIdType::UNKNOWN;
984 DeviceInfoManager::GetInstance().RemoveRemoteDeviceInfo(nodeId, deviceIdType); // nodeId invalid
985 ASSERT_EQ("", nodeId);
986
987 nodeId = "123";
988 DeviceInfoManager::GetInstance().RemoveRemoteDeviceInfo(nodeId, deviceIdType); // FindDeviceInfo false
989
990 std::string networkId = "123";
991 std::string universallyUniqueId = "123";
992 std::string uniqueDeviceId = "123";
993 std::string deviceName = "123";
994 std::string deviceType = "123";
995 DeviceInfoManager::GetInstance().AddDeviceInfo(networkId, universallyUniqueId, uniqueDeviceId, deviceName,
996 deviceType); // add 123 nodeid
997
998 networkId = "456";
999 universallyUniqueId = "456";
1000 uniqueDeviceId = ConstantCommon::GetLocalDeviceId();
1001 ASSERT_EQ("local:udid-001", uniqueDeviceId);
1002 deviceName = "456";
1003 deviceType = "456";
1004 DeviceInfoManager::GetInstance().AddDeviceInfo(networkId, universallyUniqueId, uniqueDeviceId, deviceName,
1005 deviceType); // add local unique deviceid
1006
1007 nodeId = "123";
1008 deviceIdType = DeviceIdType::UNIQUE_DISABILITY_ID;
1009 // FindDeviceInfo true + uniqueDeviceId != localDevice true
1010 DeviceInfoManager::GetInstance().RemoveRemoteDeviceInfo(nodeId, deviceIdType); // delete 123
1011
1012 nodeId = uniqueDeviceId;
1013 // FindDeviceInfo true + uniqueDeviceId != localDevice false
1014 DeviceInfoManager::GetInstance().RemoveRemoteDeviceInfo(nodeId, deviceIdType);
1015
1016 nodeId = uniqueDeviceId;
1017 DeviceIdType type = DeviceIdType::UNIQUE_DISABILITY_ID;
1018 DeviceInfoRepository::GetInstance().DeleteDeviceInfo(nodeId, type); // delete local unique deviceid
1019 }
1020
1021 /**
1022 * @tc.name: ConvertToUniversallyUniqueIdOrFetch001
1023 * @tc.desc: DeviceInfoManager::ConvertToUniversallyUniqueIdOrFetch function test
1024 * @tc.type: FUNC
1025 * @tc.require:
1026 */
1027 HWTEST_F(TokenSyncServiceTest, ConvertToUniversallyUniqueIdOrFetch001, TestSize.Level0)
1028 {
1029 std::string nodeId;
1030 ASSERT_EQ("", DeviceInfoManager::GetInstance().ConvertToUniversallyUniqueIdOrFetch(nodeId)); // nodeId invalid
1031
1032 nodeId = "123";
1033 // FindDeviceInfo false
1034 ASSERT_EQ("", DeviceInfoManager::GetInstance().ConvertToUniversallyUniqueIdOrFetch(nodeId));
1035
1036 std::string networkId = "123";
1037 std::string universallyUniqueId = "123";
1038 std::string uniqueDeviceId = "123";
1039 std::string deviceName = "123";
1040 std::string deviceType = "123";
1041 DeviceInfoManager::GetInstance().AddDeviceInfo(networkId, universallyUniqueId, uniqueDeviceId, deviceName,
1042 deviceType); // add 123 nodeid
1043
1044 nodeId = "123";
1045 // FindDeviceInfo true + universallyUniqueId is not empty
1046 DeviceInfoManager::GetInstance().ConvertToUniversallyUniqueIdOrFetch(nodeId);
1047
1048 nodeId = uniqueDeviceId;
1049 // FindDeviceInfo true + uniqueDeviceId != localDevice false
1050 DeviceIdType deviceIdType = DeviceIdType::UNIQUE_DISABILITY_ID;
1051 DeviceInfoManager::GetInstance().RemoveRemoteDeviceInfo(nodeId, deviceIdType);
1052
1053 nodeId = uniqueDeviceId;
1054 DeviceIdType type = DeviceIdType::UNIQUE_DISABILITY_ID;
1055 DeviceInfoRepository::GetInstance().DeleteDeviceInfo(nodeId, type); // delete 123
1056 }
1057
1058 /**
1059 * @tc.name: ConvertToUniqueDeviceIdOrFetch001
1060 * @tc.desc: DeviceInfoManager::ConvertToUniqueDeviceIdOrFetch function test
1061 * @tc.type: FUNC
1062 * @tc.require:
1063 */
1064 HWTEST_F(TokenSyncServiceTest, ConvertToUniqueDeviceIdOrFetch001, TestSize.Level0)
1065 {
1066 std::string nodeId;
1067 ASSERT_EQ("", DeviceInfoManager::GetInstance().ConvertToUniqueDeviceIdOrFetch(nodeId)); // nodeId invalid
1068
1069 nodeId = "123";
1070 // FindDeviceInfo false
1071 ASSERT_EQ("", DeviceInfoManager::GetInstance().ConvertToUniqueDeviceIdOrFetch(nodeId));
1072
1073 std::string networkId = "123";
1074 std::string universallyUniqueId = "123";
1075 std::string uniqueDeviceId = "123";
1076 std::string deviceName = "123";
1077 std::string deviceType = "123";
1078 DeviceInfoManager::GetInstance().AddDeviceInfo(networkId, universallyUniqueId, uniqueDeviceId, deviceName,
1079 deviceType); // add 123 nodeid
1080
1081 nodeId = "123";
1082 // FindDeviceInfo true + universallyUniqueId is not empty
1083 DeviceInfoManager::GetInstance().ConvertToUniqueDeviceIdOrFetch(nodeId);
1084
1085 nodeId = uniqueDeviceId;
1086 // FindDeviceInfo true + uniqueDeviceId != localDevice false
1087 DeviceIdType deviceIdType = DeviceIdType::UNIQUE_DISABILITY_ID;
1088 DeviceInfoManager::GetInstance().RemoveRemoteDeviceInfo(nodeId, deviceIdType);
1089
1090 nodeId = uniqueDeviceId;
1091 DeviceIdType type = DeviceIdType::UNIQUE_DISABILITY_ID;
1092 DeviceInfoRepository::GetInstance().DeleteDeviceInfo(nodeId, type); // delete 123
1093 }
1094
1095 /**
1096 * @tc.name: IsDeviceUniversallyUniqueId001
1097 * @tc.desc: DeviceInfoManager::IsDeviceUniversallyUniqueId function test
1098 * @tc.type: FUNC
1099 * @tc.require:
1100 */
1101 HWTEST_F(TokenSyncServiceTest, IsDeviceUniversallyUniqueId001, TestSize.Level0)
1102 {
1103 std::string nodeId;
1104 ASSERT_EQ(false, DeviceInfoManager::GetInstance().IsDeviceUniversallyUniqueId(nodeId)); // nodeId invalid
1105
1106 nodeId = "123";
1107 ASSERT_EQ(false, DeviceInfoManager::GetInstance().IsDeviceUniversallyUniqueId(nodeId)); // FindDeviceInfo false
1108
1109 std::string networkId = "123";
1110 std::string universallyUniqueId = "123";
1111 std::string uniqueDeviceId = "123";
1112 std::string deviceName = "123";
1113 std::string deviceType = "123";
1114 DeviceInfoManager::GetInstance().AddDeviceInfo(networkId, universallyUniqueId, uniqueDeviceId, deviceName,
1115 deviceType); // add 123 nodeid
1116
1117 ASSERT_EQ(true, DeviceInfoManager::GetInstance().IsDeviceUniversallyUniqueId(nodeId)); // FindDeviceInfo true
1118
1119 nodeId = uniqueDeviceId;
1120 DeviceIdType type = DeviceIdType::UNIQUE_DISABILITY_ID;
1121 DeviceInfoRepository::GetInstance().DeleteDeviceInfo(nodeId, type); // delete 123
1122 }
1123
1124 /**
1125 * @tc.name: FindDeviceInfo001
1126 * @tc.desc: DeviceInfoRepository::FindDeviceInfo function test
1127 * @tc.type: FUNC
1128 * @tc.require:
1129 */
1130 HWTEST_F(TokenSyncServiceTest, FindDeviceInfo001, TestSize.Level0)
1131 {
1132 std::string networkId = "123";
1133 std::string universallyUniqueId = "123";
1134 std::string uniqueDeviceId = "123";
1135 std::string deviceName = "123";
1136 std::string deviceType = "123";
1137
1138 DeviceId deviceId;
1139 deviceId.networkId = networkId;
1140 deviceId.universallyUniqueId = universallyUniqueId;
1141 deviceId.uniqueDeviceId = uniqueDeviceId;
1142 DeviceInfo deviceInfo;
1143 // count > 0 false
1144 DeviceIdType type = DeviceIdType::UNKNOWN;
1145 ASSERT_EQ(false, DeviceInfoRepository::GetInstance().FindDeviceInfo("456", type, deviceInfo));
1146
1147 DeviceInfoRepository::GetInstance().SaveDeviceInfo(networkId, universallyUniqueId, uniqueDeviceId, deviceName,
1148 deviceType); // add 123 nodeid
1149
1150 type = DeviceIdType::NETWORK_ID;
1151 // count > 0 true
1152 ASSERT_EQ(true, DeviceInfoRepository::GetInstance().FindDeviceInfo(networkId, type, deviceInfo));
1153
1154 type = DeviceIdType::UNIVERSALLY_UNIQUE_ID;
1155 // count > 0 true
1156 ASSERT_EQ(true, DeviceInfoRepository::GetInstance().FindDeviceInfo(universallyUniqueId, type, deviceInfo));
1157
1158 type = DeviceIdType::UNIQUE_DISABILITY_ID;
1159 // count > 0 true
1160 ASSERT_EQ(true, DeviceInfoRepository::GetInstance().FindDeviceInfo(uniqueDeviceId, type, deviceInfo));
1161
1162 type = DeviceIdType::UNKNOWN;
1163 // count > 0 true
1164 ASSERT_EQ(true, DeviceInfoRepository::GetInstance().FindDeviceInfo(networkId, type, deviceInfo));
1165
1166 std::string nodeId = uniqueDeviceId;
1167 type = DeviceIdType::UNIQUE_DISABILITY_ID;
1168 DeviceInfoRepository::GetInstance().DeleteDeviceInfo(nodeId, type); // delete 123
1169 }
1170
1171 /**
1172 * @tc.name: GetRemoteHapTokenInfo001
1173 * @tc.desc: TokenSyncManagerService::GetRemoteHapTokenInfo function test
1174 * @tc.type: FUNC
1175 * @tc.require:
1176 */
1177 HWTEST_F(TokenSyncServiceTest, GetRemoteHapTokenInfo001, TestSize.Level0)
1178 {
1179 std::string deviceID = "dev-001";
1180 AccessTokenID tokenID = 123; // 123 is random input
1181
1182 // FindDeviceInfo failed
1183 ASSERT_EQ(TokenSyncError::TOKEN_SYNC_REMOTE_DEVICE_INVALID,
1184 tokenSyncManagerService_->GetRemoteHapTokenInfo(deviceID, tokenID));
1185 }
1186
1187 /**
1188 * @tc.name: DeleteRemoteHapTokenInfo001
1189 * @tc.desc: TokenSyncManagerService::DeleteRemoteHapTokenInfo function test
1190 * @tc.type: FUNC
1191 * @tc.require:
1192 */
1193 HWTEST_F(TokenSyncServiceTest, DeleteRemoteHapTokenInfo001, TestSize.Level0)
1194 {
1195 AccessTokenID tokenId;
1196
1197 tokenId = 0;
1198 // Params is wrong, token id is invalid
1199 ASSERT_EQ(TokenSyncError::TOKEN_SYNC_PARAMS_INVALID,
1200 tokenSyncManagerService_->DeleteRemoteHapTokenInfo(tokenId));
1201
1202 std::string networkId = "123";
1203 std::string universallyUniqueId = "123";
1204 std::string uniqueDeviceId = "123";
1205 std::string deviceName = "123";
1206 std::string deviceType = "123";
1207 DeviceInfoManager::GetInstance().AddDeviceInfo(networkId, universallyUniqueId, uniqueDeviceId, deviceName,
1208 deviceType); // add nodeId 123
1209 networkId = "456";
1210 universallyUniqueId = "456";
1211 std::string localUdid = ConstantCommon::GetLocalDeviceId();
1212 deviceName = "456";
1213 deviceType = "456";
1214 DeviceInfoManager::GetInstance().AddDeviceInfo(networkId, universallyUniqueId, localUdid, deviceName,
1215 deviceType); // add nodeId 456
1216 tokenId = 123; // 123 is random input
1217 // no need notify local device
1218 ASSERT_EQ(TokenSyncError::TOKEN_SYNC_SUCCESS, tokenSyncManagerService_->DeleteRemoteHapTokenInfo(tokenId));
1219
1220 HapTokenInfoForSync tokenInfo;
1221 ASSERT_EQ(TokenSyncError::TOKEN_SYNC_SUCCESS, tokenSyncManagerService_->UpdateRemoteHapTokenInfo(tokenInfo));
1222 }
1223
1224 /**
1225 * @tc.name: ExistDeviceInfo001
1226 * @tc.desc: TokenSyncManagerService::ExistDeviceInfo function test
1227 * @tc.type: FUNC
1228 * @tc.require:
1229 */
1230 HWTEST_F(TokenSyncServiceTest, ExistDeviceInfo001, TestSize.Level0)
1231 {
1232 std::string nodeId = "111";
1233 DeviceIdType type = DeviceIdType::NETWORK_ID;
1234 EXPECT_FALSE(DeviceInfoManager::GetInstance().ExistDeviceInfo(nodeId, type));
1235 }
1236
1237 class TestStub : public TokenSyncManagerStub {
1238 public:
1239 TestStub() = default;
1240 virtual ~TestStub() = default;
1241
GetRemoteHapTokenInfo(const std::string & deviceID,AccessTokenID tokenID)1242 int GetRemoteHapTokenInfo(const std::string& deviceID, AccessTokenID tokenID)
1243 {
1244 return 0;
1245 }
1246
DeleteRemoteHapTokenInfo(AccessTokenID tokenID)1247 int DeleteRemoteHapTokenInfo(AccessTokenID tokenID)
1248 {
1249 return 0;
1250 }
1251
UpdateRemoteHapTokenInfo(const HapTokenInfoForSync & tokenInfo)1252 int UpdateRemoteHapTokenInfo(const HapTokenInfoForSync& tokenInfo)
1253 {
1254 return 0;
1255 }
1256 };
1257
1258 /**
1259 * @tc.name: OnRemoteRequest001
1260 * @tc.desc: TokenSyncManagerStub::OnRemoteRequest function test
1261 * @tc.type: FUNC
1262 * @tc.require:
1263 */
1264 HWTEST_F(TokenSyncServiceTest, OnRemoteRequest001, TestSize.Level0)
1265 {
1266 OHOS::MessageParcel data;
1267 OHOS::MessageParcel reply;
1268 OHOS::MessageOption option(OHOS::MessageOption::TF_SYNC);
1269 TestStub sub;
1270
1271 ASSERT_EQ(true, data.WriteInterfaceToken(ITokenSyncManager::GetDescriptor()));
1272 uint32_t code = 10;
1273 ASSERT_NE(0, sub.OnRemoteRequest(code, data, reply, option)); // msgCode default
1274
1275 ASSERT_EQ(true, data.WriteInterfaceToken(ITokenSyncManager::GetDescriptor()));
1276 // msgCode GET_REMOTE_HAP_TOKEN_INFO + type != TOKEN_NATIVE
1277 ASSERT_EQ(NO_ERROR, sub.OnRemoteRequest(static_cast<uint32_t>(
1278 TokenSyncInterfaceCode::GET_REMOTE_HAP_TOKEN_INFO), data, reply, option));
1279
1280 ASSERT_EQ(true, data.WriteInterfaceToken(ITokenSyncManager::GetDescriptor()));
1281 // msgCode DELETE_REMOTE_HAP_TOKEN_INFO + type != TOKEN_NATIVE
1282 ASSERT_EQ(NO_ERROR, sub.OnRemoteRequest(static_cast<uint32_t>(
1283 TokenSyncInterfaceCode::DELETE_REMOTE_HAP_TOKEN_INFO), data, reply, option));
1284
1285 ASSERT_EQ(true, data.WriteInterfaceToken(ITokenSyncManager::GetDescriptor()));
1286 // msgCode UPDATE_REMOTE_HAP_TOKEN_INFO + type != TOKEN_NATIVE
1287 ASSERT_EQ(NO_ERROR, sub.OnRemoteRequest(static_cast<uint32_t>(
1288 TokenSyncInterfaceCode::UPDATE_REMOTE_HAP_TOKEN_INFO), data, reply, option));
1289 }
1290
1291 /**
1292 * @tc.name: OnRemoteRequest002
1293 * @tc.desc: TokenSyncManagerStub::OnRemoteRequest function test
1294 * @tc.type: FUNC
1295 * @tc.require:
1296 */
1297 HWTEST_F(TokenSyncServiceTest, OnRemoteRequest002, TestSize.Level0)
1298 {
1299 OHOS::MessageParcel data;
1300 OHOS::MessageParcel reply;
1301 OHOS::MessageOption option(OHOS::MessageOption::TF_SYNC);
1302 TestStub sub;
1303 auto tokenId = GetSelfTokenID();
1304 EXPECT_EQ(0, SetSelfTokenID(g_selfTokenId));
1305 setuid(1234);
1306 ASSERT_EQ(true, data.WriteInterfaceToken(ITokenSyncManager::GetDescriptor()));
1307
1308 ASSERT_EQ(NO_ERROR, sub.OnRemoteRequest(static_cast<uint32_t>(
1309 TokenSyncInterfaceCode::GET_REMOTE_HAP_TOKEN_INFO), data, reply, option));
1310
1311 ASSERT_EQ(true, data.WriteInterfaceToken(ITokenSyncManager::GetDescriptor()));
1312
1313 ASSERT_EQ(NO_ERROR, sub.OnRemoteRequest(static_cast<uint32_t>(
1314 TokenSyncInterfaceCode::DELETE_REMOTE_HAP_TOKEN_INFO), data, reply, option));
1315
1316 ASSERT_EQ(true, data.WriteInterfaceToken(ITokenSyncManager::GetDescriptor()));
1317
1318 ASSERT_EQ(NO_ERROR, sub.OnRemoteRequest(static_cast<uint32_t>(
1319 TokenSyncInterfaceCode::UPDATE_REMOTE_HAP_TOKEN_INFO), data, reply, option));
1320
1321 ASSERT_EQ(ERR_IDENTITY_CHECK_FAILED, reply.ReadInt32());
1322
1323 setuid(g_selfUid);
1324 EXPECT_EQ(0, SetSelfTokenID(tokenId));
1325 }
1326
1327 /**
1328 * @tc.name: OnStart001
1329 * @tc.desc: TokenSyncManagerStub::OnStart function test
1330 * @tc.type: FUNC
1331 * @tc.require:
1332 */
1333 HWTEST_F(TokenSyncServiceTest, OnStart001, TestSize.Level0)
1334 {
1335 tokenSyncManagerService_->OnStop();
1336 ASSERT_EQ(ServiceRunningState::STATE_NOT_START, tokenSyncManagerService_->state_);
1337 tokenSyncManagerService_->OnStart();
1338 ASSERT_EQ(ServiceRunningState::STATE_RUNNING, tokenSyncManagerService_->state_);
1339 tokenSyncManagerService_->OnStart();
1340 }
1341
1342 /**
1343 * @tc.name: RemoteCommandManager001
1344 * @tc.desc: RemoteCommandManager001 function test
1345 * @tc.type: FUNC
1346 * @tc.require:
1347 */
1348 HWTEST_F(TokenSyncServiceTest, RemoteCommandManager001, TestSize.Level0)
1349 {
1350 RemoteCommandManager::GetInstance().Init();
1351 std::string udid = "test_udId";
1352 auto cmd = std::make_shared<TestBaseRemoteCommand>();
1353 char networkId[DEVICEID_MAX_LEN + 1];
1354 char pkgName[SOCKET_NAME_MAX_LEN + 1];
1355 char peerName[SOCKET_NAME_MAX_LEN + 1];
1356 strcpy_s(networkId, DEVICEID_MAX_LEN, "deviceid-1:udid-001");
1357 strcpy_s(pkgName, SOCKET_NAME_MAX_LEN, TOKEN_SYNC_PACKAGE_NAME.c_str());
1358 strcpy_s(peerName, SOCKET_NAME_MAX_LEN, TOKEN_SYNC_SOCKET_NAME.c_str());
1359 PeerSocketInfo info = {
1360 .name = peerName,
1361 .networkId = networkId,
1362 .pkgName = pkgName
1363 };
1364 int recvLen = 0x1000;
1365 SoftBusSocketListener::OnBind(0, info);
1366 int32_t ret = RemoteCommandManager::GetInstance().AddCommand(udid, cmd);
1367 ASSERT_EQ(Constant::SUCCESS, ret);
1368 ret = RemoteCommandManager::GetInstance().AddCommand("", cmd);
1369 ASSERT_EQ(Constant::FAILURE, ret);
1370 SoftBusSocketListener::OnServiceBytes(0, nullptr, recvLen);
1371 ret = RemoteCommandManager::GetInstance().AddCommand(udid, nullptr);
1372 ASSERT_EQ(Constant::FAILURE, ret);
1373 SoftBusSocketListener::OnClientBytes(0, nullptr, recvLen);
1374 ret = RemoteCommandManager::GetInstance().AddCommand("", nullptr);
1375 ASSERT_EQ(Constant::FAILURE, ret);
1376 SoftBusSocketListener::OnShutdown(0, SHUTDOWN_REASON_UNKNOWN);
1377 }
1378
1379 /**
1380 * @tc.name: RemoteCommandManager002
1381 * @tc.desc: RemoteCommandManager002 function test
1382 * @tc.type: FUNC
1383 * @tc.require:
1384 */
1385 HWTEST_F(TokenSyncServiceTest, RemoteCommandManager002, TestSize.Level0)
1386 {
1387 RemoteCommandManager::GetInstance().Init();
1388 std::string udid = "test_udId_1";
1389 int32_t ret = RemoteCommandManager::GetInstance().ProcessDeviceCommandImmediately(udid);
1390 ASSERT_EQ(Constant::FAILURE, ret);
1391 ret = RemoteCommandManager::GetInstance().ProcessDeviceCommandImmediately("");
1392 ASSERT_EQ(Constant::FAILURE, ret);
1393 SoftBusSocketListener::OnShutdown(1, SHUTDOWN_REASON_UNKNOWN);
1394 }
1395
1396 /**
1397 * @tc.name: RemoteCommandManager003
1398 * @tc.desc: RemoteCommandManager003 function test
1399 * @tc.type: FUNC
1400 * @tc.require:
1401 */
1402 HWTEST_F(TokenSyncServiceTest, RemoteCommandManager003, TestSize.Level0)
1403 {
1404 RemoteCommandManager::GetInstance().Init();
1405 std::string nodeId = "test_udId";
1406 int32_t ret = RemoteCommandManager::GetInstance().NotifyDeviceOnline("");
1407 ASSERT_EQ(Constant::FAILURE, ret);
1408 ret = RemoteCommandManager::GetInstance().NotifyDeviceOnline(nodeId);
1409 ASSERT_EQ(Constant::SUCCESS, ret);
1410 SoftBusSocketListener::OnShutdown(OUT_OF_MAP_SOCKET, SHUTDOWN_REASON_UNKNOWN);
1411 }
1412
1413 /**
1414 * @tc.name: RemoteCommandManager004
1415 * @tc.desc: RemoteCommandManager004 function test
1416 * @tc.type: FUNC
1417 * @tc.require:
1418 */
1419 HWTEST_F(TokenSyncServiceTest, RemoteCommandManager004, TestSize.Level0)
1420 {
1421 RemoteCommandManager::GetInstance().Init();
1422 std::string udid = "test_udId";
1423 auto cmd = std::make_shared<TestBaseRemoteCommand>();
1424 char networkId[DEVICEID_MAX_LEN + 1];
1425 char pkgName[SOCKET_NAME_MAX_LEN + 1];
1426 char peerName[SOCKET_NAME_MAX_LEN + 1];
1427 strcpy_s(networkId, DEVICEID_MAX_LEN, "deviceid-1:udid-001");
1428 strcpy_s(pkgName, SOCKET_NAME_MAX_LEN, TOKEN_SYNC_PACKAGE_NAME.c_str());
1429 strcpy_s(peerName, SOCKET_NAME_MAX_LEN, "invalid");
1430 PeerSocketInfo info = {
1431 .name = peerName,
1432 .networkId = networkId,
1433 .pkgName = pkgName
1434 };
1435 SoftBusSocketListener::OnBind(1, info);
1436 int32_t ret = RemoteCommandManager::GetInstance().AddCommand(udid, cmd);
1437 ASSERT_EQ(Constant::SUCCESS, ret);
1438 }
1439
1440 /**
1441 * @tc.name: RemoteCommandManager005
1442 * @tc.desc: RemoteCommandManager005 function test
1443 * @tc.type: FUNC
1444 * @tc.require:
1445 */
1446 HWTEST_F(TokenSyncServiceTest, RemoteCommandManager005, TestSize.Level0)
1447 {
1448 RemoteCommandManager::GetInstance().Init();
1449 std::string udid = "test_udId";
1450 auto cmd = std::make_shared<TestBaseRemoteCommand>();
1451 char networkId[DEVICEID_MAX_LEN + 1];
1452 char pkgName[SOCKET_NAME_MAX_LEN + 1];
1453 char peerName[SOCKET_NAME_MAX_LEN + 1];
1454 strcpy_s(networkId, DEVICEID_MAX_LEN, "deviceid-1:udid-001");
1455 strcpy_s(pkgName, SOCKET_NAME_MAX_LEN, "invalid");
1456 strcpy_s(peerName, SOCKET_NAME_MAX_LEN, TOKEN_SYNC_SOCKET_NAME.c_str());
1457 PeerSocketInfo info = {
1458 .name = peerName,
1459 .networkId = networkId,
1460 .pkgName = pkgName
1461 };
1462 SoftBusSocketListener::OnBind(1, info);
1463 int32_t ret = RemoteCommandManager::GetInstance().AddCommand(udid, cmd);
1464 ASSERT_EQ(Constant::SUCCESS, ret);
1465 }
1466
1467 /**
1468 * @tc.name: ProcessDeviceCommandImmediately001
1469 * @tc.desc: ProcessDeviceCommandImmediately function test
1470 * @tc.type: FUNC
1471 * @tc.require:
1472 */
1473 HWTEST_F(TokenSyncServiceTest, ProcessDeviceCommandImmediately001, TestSize.Level0)
1474 {
1475 std::string udid = "test_udId_1";
1476 RemoteCommandManager::GetInstance().executors_[udid] = nullptr;
1477 int32_t ret = RemoteCommandManager::GetInstance().ProcessDeviceCommandImmediately(udid);
1478 ASSERT_EQ(Constant::FAILURE, ret);
1479 ASSERT_EQ(1, RemoteCommandManager::GetInstance().executors_.erase(udid));
1480 }
1481
1482 /**
1483 * @tc.name: ProcessBufferedCommandsWithThread001
1484 * @tc.desc: RemoteCommandExecutor::ProcessBufferedCommandsWithThread function test
1485 * @tc.type: FUNC
1486 * @tc.require:
1487 */
1488 HWTEST_F(TokenSyncServiceTest, ProcessBufferedCommandsWithThread001, TestSize.Level0)
1489 {
1490 std::string nodeId = "test_nodeId";
1491 auto executor = std::make_shared<RemoteCommandExecutor>(nodeId);
1492 executor->ProcessBufferedCommandsWithThread();
1493 EXPECT_FALSE(executor->running_);
1494 auto cmd = std::make_shared<TestBaseRemoteCommand>();
1495 cmd->remoteProtocol_.statusCode = Constant::FAILURE_BUT_CAN_RETRY;
1496 executor->commands_.emplace_back(cmd);
1497 executor->running_ = true;
1498 executor->ProcessBufferedCommandsWithThread();
1499 executor->running_ = false;
1500 executor->ProcessBufferedCommandsWithThread();
1501 EXPECT_TRUE(executor->running_);
1502 }
1503
1504 namespace {
1505 PermissionStatus g_infoManagerTestUpdateState1 = {
1506 .permissionName = "ohos.permission.CAMERA",
1507 .grantStatus = PermissionState::PERMISSION_DENIED,
1508 .grantFlag = 1
1509 };
1510
1511 PermissionStatus g_infoManagerTestUpdateState2 = {
1512 .permissionName = "ohos.permission.ANSWER_CALL",
1513 .grantStatus = PermissionState::PERMISSION_DENIED,
1514 .grantFlag = 1
1515 };
1516
1517 HapTokenInfo g_remoteHapInfoBasic = {
1518 .ver = 1,
1519 .userID = 1,
1520 .bundleName = "accesstoken_test",
1521 .instIndex = 1,
1522 .tokenID = 0x20000001,
1523 .tokenAttr = 0
1524 };
1525
1526 HapTokenInfoForSync g_remoteHapInfo = {
1527 .baseInfo = g_remoteHapInfoBasic,
1528 .permStateList = {g_infoManagerTestUpdateState1, g_infoManagerTestUpdateState2}
1529 };
1530 }
1531 } // namespace AccessToken
1532 } // namespace Security
1533 } // namespace OHOS
1534