1 /*
2 * Copyright (c) 2024 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 #include "soft_bus_manager.h"
28 #undef private
29
30 #include "gtest/gtest.h"
31 #include "accesstoken_kit.h"
32 #include "accesstoken_log.h"
33 #include "access_token_error.h"
34 #include "base_remote_command.h"
35 #include "constant_common.h"
36 #include "delete_remote_token_command.h"
37 #include "device_info_manager.h"
38 #include "device_info_repository.h"
39 #include "device_info.h"
40 #include "device_manager_callback.h"
41 #include "dm_device_info.h"
42 #include "i_token_sync_manager.h"
43 #include "remote_command_manager.h"
44 #include "socket.h"
45 #include "soft_bus_device_connection_listener.h"
46 #include "soft_bus_socket_listener.h"
47 #include "token_setproc.h"
48 #include "token_sync_manager_stub.h"
49
50 using namespace std;
51 using namespace testing::ext;
52
53 namespace OHOS {
54 namespace Security {
55 namespace AccessToken {
56 namespace {
57 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "TokenSyncServiceTest"};
58
59 static DistributedHardware::DmDeviceInfo g_devInfo = {
60 // udid = deviceid-1:udid-001 uuid = deviceid-1:uuid-001
61 .deviceId = "deviceid-1",
62 .deviceName = "remote_mock",
63 .deviceTypeId = 1,
64 .networkId = "deviceid-1"
65 };
66
67 static std::vector<std::thread> threads_;
68 static std::shared_ptr<SoftBusDeviceConnectionListener> g_ptrDeviceStateCallback =
69 std::make_shared<SoftBusDeviceConnectionListener>();
70 static int32_t g_selfUid;
71 static AccessTokenID g_selfTokenId = 0;
72 static const int32_t OUT_OF_MAP_SOCKET = 2;
73 }
74
75 class TokenSyncServiceTest : public testing::Test {
76 public:
77 TokenSyncServiceTest();
78 ~TokenSyncServiceTest();
79 static void SetUpTestCase();
80 static void TearDownTestCase();
81 void OnDeviceOffline(const DistributedHardware::DmDeviceInfo &info);
82 void SetUp();
83 void TearDown();
84 std::shared_ptr<TokenSyncManagerService> tokenSyncManagerService_;
85 };
86
TokenSyncServiceTest()87 TokenSyncServiceTest::TokenSyncServiceTest()
88 {
89 DelayedSingleton<TokenSyncManagerService>::GetInstance()->Initialize();
90 }
~TokenSyncServiceTest()91 TokenSyncServiceTest::~TokenSyncServiceTest()
92 {}
93
NativeTokenGet()94 void NativeTokenGet()
95 {
96 uint64_t tokenId = 0;
97 tokenId = AccessTokenKit::GetNativeTokenId("token_sync_service");
98 ASSERT_NE(tokenId, static_cast<AccessTokenID>(0));
99 EXPECT_EQ(0, SetSelfTokenID(tokenId));
100 }
101
SetUpTestCase()102 void TokenSyncServiceTest::SetUpTestCase()
103 {
104 g_selfUid = getuid();
105 g_selfTokenId = GetSelfTokenID();
106 NativeTokenGet();
107 }
TearDownTestCase()108 void TokenSyncServiceTest::TearDownTestCase()
109 {}
SetUp()110 void TokenSyncServiceTest::SetUp()
111 {
112 tokenSyncManagerService_ = DelayedSingleton<TokenSyncManagerService>::GetInstance();
113 EXPECT_NE(nullptr, tokenSyncManagerService_);
114 }
TearDown()115 void TokenSyncServiceTest::TearDown()
116 {
117 ACCESSTOKEN_LOG_INFO(LABEL, "TearDown start.");
118 tokenSyncManagerService_ = nullptr;
119 for (auto it = threads_.begin(); it != threads_.end(); it++) {
120 it->join();
121 }
122 threads_.clear();
123
124 if (g_ptrDeviceStateCallback != nullptr) {
125 OnDeviceOffline(g_devInfo);
126 sleep(1);
127 }
128 }
129
OnDeviceOffline(const DistributedHardware::DmDeviceInfo & info)130 void TokenSyncServiceTest::OnDeviceOffline(const DistributedHardware::DmDeviceInfo &info)
131 {
132 std::string networkId = info.networkId;
133 std::string uuid = DeviceInfoManager::GetInstance().ConvertToUniversallyUniqueIdOrFetch(networkId);
134 std::string udid = DeviceInfoManager::GetInstance().ConvertToUniqueDeviceIdOrFetch(networkId);
135
136 ACCESSTOKEN_LOG_INFO(LABEL,
137 "networkId: %{public}s, uuid: %{public}s, udid: %{public}s",
138 networkId.c_str(),
139 uuid.c_str(),
140 ConstantCommon::EncryptDevId(udid).c_str());
141
142 if (uuid != "" && udid != "") {
143 RemoteCommandManager::GetInstance().NotifyDeviceOffline(uuid);
144 RemoteCommandManager::GetInstance().NotifyDeviceOffline(udid);
145 DeviceInfoManager::GetInstance().RemoveRemoteDeviceInfo(networkId, DeviceIdType::NETWORK_ID);
146 } else {
147 ACCESSTOKEN_LOG_ERROR(LABEL, "uuid or udid is empty, offline failed.");
148 }
149 }
150
151 /**
152 * @tc.name: CheckAndCopyStr001
153 * @tc.desc: destlen not equal to src
154 * @tc.type: FUNC
155 * @tc.require:
156 */
157 HWTEST_F(TokenSyncServiceTest, CheckAndCopyStr001, TestSize.Level1)
158 {
159 std::string test_src = "testSrc";
160 ASSERT_FALSE(SoftBusManager::GetInstance().CheckAndCopyStr(nullptr, test_src.length(), test_src));
161 }
162
163 /**
164 * @tc.name: CloseSocket001
165 * @tc.desc: invalid socketFd
166 * @tc.type: FUNC
167 * @tc.require:
168 */
169 HWTEST_F(TokenSyncServiceTest, CloseSocket001, TestSize.Level1)
170 {
171 ASSERT_EQ(Constant::FAILURE, SoftBusManager::GetInstance().CloseSocket(-1));
172 ASSERT_EQ(Constant::SUCCESS, SoftBusManager::GetInstance().CloseSocket(OUT_OF_MAP_SOCKET));
173 std::string networkId;
174 ASSERT_FALSE(SoftBusManager::GetInstance().GetNetworkIdBySocket(OUT_OF_MAP_SOCKET, networkId));
175 }
176
177 /**
178 * @tc.name: GetUniversallyUniqueIdByNodeId001
179 * @tc.desc: invalid nodeId
180 * @tc.type: FUNC
181 * @tc.require:
182 */
183 HWTEST_F(TokenSyncServiceTest, GetUniversallyUniqueIdByNodeId001, TestSize.Level1)
184 {
185 ASSERT_EQ("", SoftBusManager::GetInstance().GetUniversallyUniqueIdByNodeId(""));
186 ASSERT_EQ("", SoftBusManager::GetInstance().GetUniqueDeviceIdByNodeId(""));
187 }
188 } // namespace AccessToken
189 } // namespace Security
190 } // namespace OHOS
191