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 #include "soft_bus_channel.h"
29 #undef private
30
31 #include "gtest/gtest.h"
32 #include "accesstoken_kit.h"
33 #include "accesstoken_common_log.h"
34 #include "access_token_error.h"
35 #include "base_remote_command.h"
36 #include "constant_common.h"
37 #include "delete_remote_token_command.h"
38 #include "device_info_manager.h"
39 #include "device_info_repository.h"
40 #include "device_info.h"
41 #include "device_manager_callback.h"
42 #include "dm_device_info.h"
43 #include "i_token_sync_manager.h"
44 #include "remote_command_manager.h"
45 #include "socket.h"
46 #include "soft_bus_device_connection_listener.h"
47 #include "soft_bus_socket_listener.h"
48 #include "test_common.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 namespace {
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 = TestCommon::GetNativeTokenIdFromProcess("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 TestCommon::SetTestEvironment(g_selfTokenId);
107 NativeTokenGet();
108 }
TearDownTestCase()109 void TokenSyncServiceTest::TearDownTestCase()
110 {
111 SetSelfTokenID(g_selfTokenId);
112 TestCommon::ResetTestEvironment();
113 }
SetUp()114 void TokenSyncServiceTest::SetUp()
115 {
116 tokenSyncManagerService_ = DelayedSingleton<TokenSyncManagerService>::GetInstance();
117 EXPECT_NE(nullptr, tokenSyncManagerService_);
118 }
TearDown()119 void TokenSyncServiceTest::TearDown()
120 {
121 LOGI(ATM_DOMAIN, ATM_TAG, "TearDown start.");
122 tokenSyncManagerService_ = nullptr;
123 for (auto it = threads_.begin(); it != threads_.end(); it++) {
124 it->join();
125 }
126 threads_.clear();
127
128 if (g_ptrDeviceStateCallback != nullptr) {
129 OnDeviceOffline(g_devInfo);
130 sleep(1);
131 }
132 }
133
OnDeviceOffline(const DistributedHardware::DmDeviceInfo & info)134 void TokenSyncServiceTest::OnDeviceOffline(const DistributedHardware::DmDeviceInfo &info)
135 {
136 std::string networkId = info.networkId;
137 std::string uuid = DeviceInfoManager::GetInstance().ConvertToUniversallyUniqueIdOrFetch(networkId);
138 if (uuid.empty()) {
139 uuid = SoftBusManager::GetInstance().GetUniversallyUniqueIdByNodeId(networkId);
140 }
141 std::string udid = DeviceInfoManager::GetInstance().ConvertToUniqueDeviceIdOrFetch(networkId);
142
143 LOGI(ATM_DOMAIN, ATM_TAG,
144 "networkId: %{public}s, uuid: %{public}s, udid: %{public}s",
145 networkId.c_str(),
146 uuid.c_str(),
147 ConstantCommon::EncryptDevId(udid).c_str());
148
149 if (uuid != "" && udid != "") {
150 RemoteCommandManager::GetInstance().NotifyDeviceOffline(uuid);
151 RemoteCommandManager::GetInstance().NotifyDeviceOffline(udid);
152 DeviceInfoManager::GetInstance().RemoveRemoteDeviceInfo(networkId, DeviceIdType::NETWORK_ID);
153 } else {
154 LOGE(ATM_DOMAIN, ATM_TAG, "uuid or udid is empty, offline failed.");
155 }
156 }
157
158 /**
159 * @tc.name: CheckAndCopyStr001
160 * @tc.desc: destlen not equal to src
161 * @tc.type: FUNC
162 * @tc.require:
163 */
164 HWTEST_F(TokenSyncServiceTest, CheckAndCopyStr001, TestSize.Level4)
165 {
166 std::string test_src = "testSrc";
167 ASSERT_FALSE(SoftBusManager::GetInstance().CheckAndCopyStr(nullptr, test_src.length(), test_src));
168 }
169
170 /**
171 * @tc.name: CloseSocket001
172 * @tc.desc: invalid socketFd
173 * @tc.type: FUNC
174 * @tc.require:
175 */
176 HWTEST_F(TokenSyncServiceTest, CloseSocket001, TestSize.Level4)
177 {
178 ASSERT_EQ(Constant::FAILURE, SoftBusManager::GetInstance().CloseSocket(-1));
179 ASSERT_EQ(Constant::SUCCESS, SoftBusManager::GetInstance().CloseSocket(OUT_OF_MAP_SOCKET));
180 std::string networkId;
181 ASSERT_FALSE(SoftBusManager::GetInstance().GetNetworkIdBySocket(OUT_OF_MAP_SOCKET, networkId));
182 }
183
184 /**
185 * @tc.name: GetUniversallyUniqueIdByNodeId001
186 * @tc.desc: invalid nodeId
187 * @tc.type: FUNC
188 * @tc.require:
189 */
190 HWTEST_F(TokenSyncServiceTest, GetUniversallyUniqueIdByNodeId001, TestSize.Level4)
191 {
192 SoftBusManager::GetInstance().Initialize();
193 SoftBusManager::GetInstance().SetDefaultConfigValue();
194 ASSERT_EQ("", SoftBusManager::GetInstance().GetUniversallyUniqueIdByNodeId(""));
195 ASSERT_EQ("", SoftBusManager::GetInstance().GetUniqueDeviceIdByNodeId(""));
196 }
197
198 /**
199 * @tc.name: InsertCallbackAndExcute001
200 * @tc.desc: Ond
201 * @tc.type: FUNC
202 * @tc.require:
203 */
204 HWTEST_F(TokenSyncServiceTest, InsertCallbackAndExcute001, TestSize.Level4)
205 {
206 SoftBusDeviceConnectionListener listener;
207 listener.OnDeviceOffline(g_devInfo);
208 DeviceInfoRepository::GetInstance().SaveDeviceInfo(g_devInfo.networkId, "123", g_devInfo.deviceId,
209 g_devInfo.deviceName, std::to_string(g_devInfo.deviceTypeId));
210 listener.OnDeviceOffline(g_devInfo);
211 SoftBusChannel channel("test");
212 std::string test("test");
213 channel.InsertCallback(0, test);
214 ASSERT_EQ(true, channel.isSocketUsing_);
215 ASSERT_EQ("", channel.ExecuteCommand("test", "test"));
216 }
217 } // namespace AccessToken
218 } // namespace Security
219 } // namespace OHOS
220