1 /*
2 * Copyright (c) 2022-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 "UTTest_dm_device_state_manager.h"
17
18 #include <iostream>
19
20 #include "dm_log.h"
21 #include "dm_constants.h"
22 #include "dm_adapter_manager.h"
23 #include "ipc_notify_device_state_req.h"
24 #include "ipc_notify_auth_result_req.h"
25 #include "dm_device_state_manager.h"
26 #include "ipc_notify_device_found_req.h"
27 #include "ipc_notify_discover_result_req.h"
28 #include "hichain_connector.h"
29 #include "device_manager_service_listener.h"
30
31 namespace OHOS {
32 namespace DistributedHardware {
SetUp()33 void DmDeviceStateManagerTest::SetUp()
34 {
35 }
36
TearDown()37 void DmDeviceStateManagerTest::TearDown()
38 {
39 }
40
SetUpTestCase()41 void DmDeviceStateManagerTest::SetUpTestCase()
42 {
43 }
44
TearDownTestCase()45 void DmDeviceStateManagerTest::TearDownTestCase()
46 {
47 }
48 namespace {
49 std::shared_ptr<HiChainConnector> hiChainConnector_ = std::make_shared<HiChainConnector>();
50 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
51 std::shared_ptr<DeviceManagerServiceListener> listener_ = std::make_shared<DeviceManagerServiceListener>();
52 std::shared_ptr<HiChainAuthConnector> hiChainAuthConnector = std::make_shared<HiChainAuthConnector>();
53 std::shared_ptr<DmDeviceStateManager> dmDeviceStateManager =
54 std::make_shared<DmDeviceStateManager>(softbusConnector, listener_, hiChainConnector_, hiChainAuthConnector);
55
56 /**
57 * @tc.name: DmDeviceStateManager_001
58 * @tc.desc: set DmDeviceStateManager to tne new pointer, and it's not nullptr
59 * @tc.type: FUNC
60 * @tc.require: AR000GHSJK
61 */
62 HWTEST_F(DmDeviceStateManagerTest, DmDeviceStateManager_001, testing::ext::TestSize.Level0)
63 {
64 std::shared_ptr<DmDeviceStateManager> p = std::make_shared<DmDeviceStateManager>(softbusConnector, listener_,
65 hiChainConnector_, hiChainAuthConnector);
66 ASSERT_NE(p, nullptr);
67 }
68
69 /**
70 * @tc.name: DmDeviceStateManager_002
71 * @tc.desc: set DmDeviceStateManager to tne new pointer,it's not nullptr and delete it
72 * @tc.type: FUNC
73 * @tc.require: AR000GHSJK
74 */
75 HWTEST_F(DmDeviceStateManagerTest, DmDeviceStateManager_002, testing::ext::TestSize.Level0)
76 {
77 std::shared_ptr<DmDeviceStateManager> p = std::make_shared<DmDeviceStateManager>(softbusConnector, listener_,
78 hiChainConnector_, hiChainAuthConnector);
79 p.reset();
80 EXPECT_EQ(p, nullptr);
81 }
82
83 /**
84 * @tc.name: OnDbReady_001
85 * @tc.desc: set info.deviceId to some para, and return it
86 * @tc.type: FUNC
87 * @tc.require: AR000GHSJK
88 */
89 HWTEST_F(DmDeviceStateManagerTest, OnDbReady_001, testing::ext::TestSize.Level0)
90 {
91 std::string pkgName;
92 std::string deviceId;
93 DmDeviceInfo info;
94 strcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
95 dmDeviceStateManager->OnDbReady(pkgName, deviceId);
96 std::shared_ptr<IpcNotifyDeviceStateReq> pReq =
97 std::static_pointer_cast<IpcNotifyDeviceStateReq>(listener_->ipcServerListener_.req_);
98 DmDeviceInfo dminfo;
99 if (pReq == nullptr) {
100 strcpy_s(dminfo.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
101 } else {
102 dminfo = pReq->GetDeviceInfo();
103 }
104 int result = strcmp(info.deviceId, dminfo.deviceId);
105 EXPECT_EQ(result, 0);
106 }
107
108 /**
109 * @tc.name: RegisterOffLineTimer_001
110 * @tc.desc: call RegisterOffLineTimer
111 * @tc.type: FUNC
112 * @tc.require: AR000GHSJK
113 */
114 HWTEST_F(DmDeviceStateManagerTest, RegisterOffLineTimer_001, testing::ext::TestSize.Level0)
115 {
116 DmDeviceInfo info = {
117 .deviceId = "12345678901234567890987654321234",
118 .deviceName = "asda",
119 .deviceTypeId = 1,
120 };
121 dmDeviceStateManager->RegisterOffLineTimer(info);
122 std::string deviceId;
123 SoftbusConnector::GetUdidByNetworkId(info.deviceId, deviceId);
124 EXPECT_EQ(dmDeviceStateManager->stateTimerInfoMap_.count(deviceId), 0);
125 }
126
127 /**
128 * @tc.name: SaveOnlineDeviceInfo_001
129 * @tc.desc: call SaveOnlineDeviceInfo
130 * @tc.type: FUNC
131 */
132 HWTEST_F(DmDeviceStateManagerTest, SaveOnlineDeviceInfo_001, testing::ext::TestSize.Level0)
133 {
134 DmDeviceInfo info = {
135 .deviceId = "123",
136 .deviceName = "asda",
137 .deviceTypeId = 1,
138 };
139 dmDeviceStateManager->SaveOnlineDeviceInfo(info);
140 std::string uuid;
141 SoftbusConnector::GetUuidByNetworkId(info.networkId, uuid);
142 EXPECT_EQ(dmDeviceStateManager->remoteDeviceInfos_.count(uuid), 0);
143 }
144
145 /**
146 * @tc.name: DeleteOfflineDeviceInfo_001
147 * @tc.desc: call DeleteOfflineDeviceInfo
148 * @tc.type: FUNC
149 */
150 HWTEST_F(DmDeviceStateManagerTest, DeleteOfflineDeviceInfo_001, testing::ext::TestSize.Level0)
151 {
152 DmDeviceInfo info = {
153 .deviceId = "123",
154 .deviceName = "asda",
155 .deviceTypeId = 1,
156 };
157 dmDeviceStateManager->DeleteOfflineDeviceInfo(info);
158 for (auto iter: dmDeviceStateManager->remoteDeviceInfos_) {
159 if (iter.second.deviceId == info.deviceId) {
160 EXPECT_EQ(dmDeviceStateManager->remoteDeviceInfos_.count(iter.first), 0);
161 break;
162 }
163 }
164 }
165
166 HWTEST_F(DmDeviceStateManagerTest, OnDeviceOnline_001, testing::ext::TestSize.Level0)
167 {
168 std::string deviceId = "deviceId";
169 dmDeviceStateManager->OnDeviceOnline(deviceId, DmAuthForm::ACROSS_ACCOUNT);
170 EXPECT_NE(dmDeviceStateManager->softbusConnector_, nullptr);
171 }
172
173 HWTEST_F(DmDeviceStateManagerTest, OnDeviceOffline_001, testing::ext::TestSize.Level0)
174 {
175 std::string deviceId = "deviceId";
176 dmDeviceStateManager->OnDeviceOffline(deviceId);
177 EXPECT_NE(dmDeviceStateManager->softbusConnector_, nullptr);
178 }
179
180 HWTEST_F(DmDeviceStateManagerTest, StartOffLineTimer_001, testing::ext::TestSize.Level0)
181 {
182 DmDeviceInfo deviceInfo;
183 dmDeviceStateManager->timer_ = nullptr;
184 dmDeviceStateManager->StartOffLineTimer(deviceInfo);
185 EXPECT_NE(dmDeviceStateManager->softbusConnector_, nullptr);
186 }
187
188 HWTEST_F(DmDeviceStateManagerTest, DeleteTimeOutGroup_001, testing::ext::TestSize.Level0)
189 {
190 std::string name = "name";
191 dmDeviceStateManager->DeleteTimeOutGroup(name);
192 EXPECT_NE(dmDeviceStateManager->softbusConnector_, nullptr);
193 }
194
195 HWTEST_F(DmDeviceStateManagerTest, AddTask_001, testing::ext::TestSize.Level0)
196 {
197 int32_t eventId = 0;
198 std::string deviceId = "deviceId";
199 std::shared_ptr<NotifyEvent> task = std::make_shared<NotifyEvent>(eventId, deviceId);
200 int32_t ret = dmDeviceStateManager->AddTask(task);
201 EXPECT_EQ(ret, DM_OK);
202 }
203
204 HWTEST_F(DmDeviceStateManagerTest, RunTask_001, testing::ext::TestSize.Level0)
205 {
206 int32_t eventId = 0;
207 std::string deviceId = "deviceId";
208 std::shared_ptr<NotifyEvent> task = std::make_shared<NotifyEvent>(eventId, deviceId);
209 dmDeviceStateManager->RunTask(task);
210 EXPECT_NE(dmDeviceStateManager->softbusConnector_, nullptr);
211 }
212
213 HWTEST_F(DmDeviceStateManagerTest, GetAuthForm_001, testing::ext::TestSize.Level0)
214 {
215 std::string networkId;
216 dmDeviceStateManager->hiChainConnector_ = nullptr;
217 int32_t ret = dmDeviceStateManager->GetAuthForm(networkId);
218 EXPECT_EQ(ret, DmAuthForm::INVALID_TYPE);
219 }
220
221 HWTEST_F(DmDeviceStateManagerTest, GetAuthForm_002, testing::ext::TestSize.Level0)
222 {
223 std::string networkId;
224 dmDeviceStateManager->hiChainConnector_ = std::make_shared<HiChainConnector>();
225 int32_t ret = dmDeviceStateManager->GetAuthForm(networkId);
226 EXPECT_EQ(ret, DmAuthForm::INVALID_TYPE);
227 }
228
229 HWTEST_F(DmDeviceStateManagerTest, GetAuthForm_003, testing::ext::TestSize.Level0)
230 {
231 std::string networkId = "networkId";
232 int32_t ret = dmDeviceStateManager->GetAuthForm(networkId);
233 EXPECT_EQ(ret, DmAuthForm::INVALID_TYPE);
234 }
235
236 HWTEST_F(DmDeviceStateManagerTest, ProcNotifyEvent_001, testing::ext::TestSize.Level0)
237 {
238 int32_t eventId = 0;
239 std::string deviceId;
240 int32_t ret = dmDeviceStateManager->ProcNotifyEvent(eventId, deviceId);
241 DmDeviceInfo info = {
242 .deviceId = "123",
243 .deviceName = "asda",
244 .deviceTypeId = 1,
245 .networkId = "14569",
246 };
247 std::string udid = "udidTest";
248 DmDeviceInfo remoteInfo = {
249 .deviceId = "123",
250 };
251 dmDeviceStateManager->remoteDeviceInfos_[udid] = remoteInfo;
252 dmDeviceStateManager->ChangeDeviceInfo(info);
253 EXPECT_EQ(ret, DM_OK);
254 }
255
256 HWTEST_F(DmDeviceStateManagerTest, HandleCandidateRestrictStatus_001, testing::ext::TestSize.Level0)
257 {
258 std::string proofInfo;
259 uint16_t deviceTypeId = 0x00;
260 int32_t errcode = -1;
261 dmDeviceStateManager->HandleCredentialAuthStatus(proofInfo, deviceTypeId, errcode);
262 EXPECT_NE(dmDeviceStateManager->softbusConnector_, nullptr);
263 }
264 } // namespace
265 } // namespace DistributedHardware
266 } // namespace OHOS
267