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 "token_setproc.h"
49 #include "token_sync_manager_stub.h"
50
51 using namespace std;
52 using namespace testing::ext;
53
54 namespace OHOS {
55 namespace Security {
56 namespace AccessToken {
57 namespace {
58 static DistributedHardware::DmDeviceInfo g_devInfo = {
59 // udid = deviceid-1:udid-001 uuid = deviceid-1:uuid-001
60 .deviceId = "deviceid-1",
61 .deviceName = "remote_mock",
62 .deviceTypeId = 1,
63 .networkId = "deviceid-1"
64 };
65
66 static std::vector<std::thread> threads_;
67 static std::shared_ptr<SoftBusDeviceConnectionListener> g_ptrDeviceStateCallback =
68 std::make_shared<SoftBusDeviceConnectionListener>();
69 static int32_t g_selfUid;
70 static AccessTokenID g_selfTokenId = 0;
71 static const int32_t OUT_OF_MAP_SOCKET = 2;
72 }
73
74 class TokenSyncServiceTest : public testing::Test {
75 public:
76 TokenSyncServiceTest();
77 ~TokenSyncServiceTest();
78 static void SetUpTestCase();
79 static void TearDownTestCase();
80 void OnDeviceOffline(const DistributedHardware::DmDeviceInfo &info);
81 void SetUp();
82 void TearDown();
83 std::shared_ptr<TokenSyncManagerService> tokenSyncManagerService_;
84 };
85
TokenSyncServiceTest()86 TokenSyncServiceTest::TokenSyncServiceTest()
87 {
88 DelayedSingleton<TokenSyncManagerService>::GetInstance()->Initialize();
89 }
~TokenSyncServiceTest()90 TokenSyncServiceTest::~TokenSyncServiceTest()
91 {}
92
NativeTokenGet()93 void NativeTokenGet()
94 {
95 uint64_t tokenId = 0;
96 tokenId = AccessTokenKit::GetNativeTokenId("token_sync_service");
97 ASSERT_NE(tokenId, static_cast<AccessTokenID>(0));
98 EXPECT_EQ(0, SetSelfTokenID(tokenId));
99 }
100
SetUpTestCase()101 void TokenSyncServiceTest::SetUpTestCase()
102 {
103 g_selfUid = getuid();
104 g_selfTokenId = GetSelfTokenID();
105 NativeTokenGet();
106 }
TearDownTestCase()107 void TokenSyncServiceTest::TearDownTestCase()
108 {}
SetUp()109 void TokenSyncServiceTest::SetUp()
110 {
111 tokenSyncManagerService_ = DelayedSingleton<TokenSyncManagerService>::GetInstance();
112 EXPECT_NE(nullptr, tokenSyncManagerService_);
113 }
TearDown()114 void TokenSyncServiceTest::TearDown()
115 {
116 LOGI(ATM_DOMAIN, ATM_TAG, "TearDown start.");
117 tokenSyncManagerService_ = nullptr;
118 for (auto it = threads_.begin(); it != threads_.end(); it++) {
119 it->join();
120 }
121 threads_.clear();
122
123 if (g_ptrDeviceStateCallback != nullptr) {
124 OnDeviceOffline(g_devInfo);
125 sleep(1);
126 }
127 }
128
OnDeviceOffline(const DistributedHardware::DmDeviceInfo & info)129 void TokenSyncServiceTest::OnDeviceOffline(const DistributedHardware::DmDeviceInfo &info)
130 {
131 std::string networkId = info.networkId;
132 std::string uuid = DeviceInfoManager::GetInstance().ConvertToUniversallyUniqueIdOrFetch(networkId);
133 std::string udid = DeviceInfoManager::GetInstance().ConvertToUniqueDeviceIdOrFetch(networkId);
134
135 LOGI(ATM_DOMAIN, ATM_TAG,
136 "networkId: %{public}s, uuid: %{public}s, udid: %{public}s",
137 networkId.c_str(),
138 uuid.c_str(),
139 ConstantCommon::EncryptDevId(udid).c_str());
140
141 if (uuid != "" && udid != "") {
142 RemoteCommandManager::GetInstance().NotifyDeviceOffline(uuid);
143 RemoteCommandManager::GetInstance().NotifyDeviceOffline(udid);
144 DeviceInfoManager::GetInstance().RemoveRemoteDeviceInfo(networkId, DeviceIdType::NETWORK_ID);
145 } else {
146 LOGE(ATM_DOMAIN, ATM_TAG, "uuid or udid is empty, offline failed.");
147 }
148 }
149
150 /**
151 * @tc.name: CheckAndCopyStr001
152 * @tc.desc: destlen not equal to src
153 * @tc.type: FUNC
154 * @tc.require:
155 */
156 HWTEST_F(TokenSyncServiceTest, CheckAndCopyStr001, TestSize.Level1)
157 {
158 std::string test_src = "testSrc";
159 ASSERT_FALSE(SoftBusManager::GetInstance().CheckAndCopyStr(nullptr, test_src.length(), test_src));
160 }
161
162 /**
163 * @tc.name: CloseSocket001
164 * @tc.desc: invalid socketFd
165 * @tc.type: FUNC
166 * @tc.require:
167 */
168 HWTEST_F(TokenSyncServiceTest, CloseSocket001, TestSize.Level1)
169 {
170 ASSERT_EQ(Constant::FAILURE, SoftBusManager::GetInstance().CloseSocket(-1));
171 ASSERT_EQ(Constant::SUCCESS, SoftBusManager::GetInstance().CloseSocket(OUT_OF_MAP_SOCKET));
172 std::string networkId;
173 ASSERT_FALSE(SoftBusManager::GetInstance().GetNetworkIdBySocket(OUT_OF_MAP_SOCKET, networkId));
174 }
175
176 /**
177 * @tc.name: GetUniversallyUniqueIdByNodeId001
178 * @tc.desc: invalid nodeId
179 * @tc.type: FUNC
180 * @tc.require:
181 */
182 HWTEST_F(TokenSyncServiceTest, GetUniversallyUniqueIdByNodeId001, TestSize.Level1)
183 {
184 SoftBusManager::GetInstance().Initialize();
185 SoftBusManager::GetInstance().SetDefaultConfigValue();
186 ASSERT_EQ("", SoftBusManager::GetInstance().GetUniversallyUniqueIdByNodeId(""));
187 ASSERT_EQ("", SoftBusManager::GetInstance().GetUniqueDeviceIdByNodeId(""));
188 }
189
190 /**
191 * @tc.name: InsertCallbackAndExcute001
192 * @tc.desc: Ond
193 * @tc.type: FUNC
194 * @tc.require:
195 */
196 HWTEST_F(TokenSyncServiceTest, InsertCallbackAndExcute001, TestSize.Level1)
197 {
198 SoftBusDeviceConnectionListener listener;
199 listener.OnDeviceOffline(g_devInfo);
200 SoftBusChannel channel("test");
201 std::string test("test");
202 channel.InsertCallback(0, test);
203 ASSERT_EQ(true, channel.isSocketUsing_);
204 ASSERT_EQ("", channel.ExecuteCommand("test", "test"));
205 }
206 } // namespace AccessToken
207 } // namespace Security
208 } // namespace OHOS
209