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