1 /*
2 * Copyright (c) 2021-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 "gtest/gtest.h"
17
18 #include "access_manager.h"
19 #include "dh_context.h"
20 #include "distributed_hardware_manager_factory.h"
21 #include "dm_device_info.h"
22 #include "distributed_hardware_errno.h"
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace DistributedHardware {
27 namespace {
28 enum class Status : uint32_t {
29 DEVICE_ONLINE = 0,
30 DEVICE_OFFLINE = 1,
31 };
32
33 constexpr uint16_t TEST_DEV_TYPE_PAD = 0x11;
34 const std::string TEST_NETWORKID = "111111";
35 const std::string TEST_UUID = "222222";
36 const std::string TEST_UDID = "333333";
37 }
38
39 class AccessManagerTest : public testing::Test {
40 public:
41 static void SetUpTestCase(void);
42 static void TearDownTestCase(void);
43 void SetUp();
44 void TearDown();
45 };
46
SetUp()47 void AccessManagerTest::SetUp()
48 {
49 DistributedHardwareManagerFactory::GetInstance().isInit_.store(true);
50 }
51
TearDown()52 void AccessManagerTest::TearDown() {}
53
SetUpTestCase()54 void AccessManagerTest::SetUpTestCase() {}
55
TearDownTestCase()56 void AccessManagerTest::TearDownTestCase() {}
57
58 /**
59 * @tc.name: SendOnLineEvent_001
60 * @tc.desc: Verify the online success
61 * @tc.type: FUNC
62 * @tc.require: AR000GHSJK
63 */
64 HWTEST_F(AccessManagerTest, SendOnLineEvent_001, TestSize.Level1)
65 {
66 DHContext::GetInstance().AddOnlineDevice(TEST_UDID, TEST_UUID, TEST_NETWORKID);
67 auto ret = DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent(TEST_NETWORKID, TEST_UUID, TEST_UDID,
68 TEST_DEV_TYPE_PAD);
69 EXPECT_EQ(ERR_DH_FWK_HARDWARE_MANAGER_DEVICE_REPEAT_ONLINE, ret);
70
71 DHContext::GetInstance().devIdEntrySet_.clear();
72 ret = DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent(TEST_NETWORKID, TEST_UUID, TEST_UDID,
73 TEST_DEV_TYPE_PAD);
74 EXPECT_EQ(DH_FWK_SUCCESS, ret);
75 }
76
77 /**
78 * @tc.name: SendOffLineEvent_001
79 * @tc.desc: Verify the offline success
80 * @tc.type: FUNC
81 * @tc.require: AR000GHSJM
82 */
83 HWTEST_F(AccessManagerTest, SendOffLineEvent_001, TestSize.Level1)
84 {
85 DHContext::GetInstance().devIdEntrySet_.clear();
86 auto ret = DistributedHardwareManagerFactory::GetInstance().SendOffLineEvent("", TEST_UUID, TEST_UDID,
87 TEST_DEV_TYPE_PAD);
88 EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret);
89 ret = DistributedHardwareManagerFactory::GetInstance().SendOffLineEvent(TEST_NETWORKID, "", TEST_UDID,
90 TEST_DEV_TYPE_PAD);
91 EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret);
92 ret = DistributedHardwareManagerFactory::GetInstance().SendOffLineEvent(TEST_NETWORKID, TEST_UUID, "",
93 TEST_DEV_TYPE_PAD);
94 EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret);
95
96 DHContext::GetInstance().AddOnlineDevice(TEST_UDID, TEST_UUID, TEST_NETWORKID);
97 ret = DistributedHardwareManagerFactory::GetInstance().SendOffLineEvent(TEST_NETWORKID, TEST_UUID, TEST_UDID,
98 TEST_DEV_TYPE_PAD);
99 EXPECT_EQ(DH_FWK_SUCCESS, ret);
100 DHContext::GetInstance().devIdEntrySet_.clear();
101 }
102
103 /**
104 * @tc.name: dhFactoryInit_001
105 * @tc.desc: Verify the Init success
106 * @tc.type: FUNC
107 * @tc.require: AR000GHSJM
108 */
109 HWTEST_F(AccessManagerTest, dhFactoryInit_001, TestSize.Level1)
110 {
111 auto ret = DistributedHardwareManagerFactory::GetInstance().Init();
112 EXPECT_EQ(true, ret);
113 }
114
115 /**
116 * @tc.name: Init_001
117 * @tc.desc: Verify the Init function
118 * @tc.type: FUNC
119 * @tc.require: AR000GHSJM
120 */
121 HWTEST_F(AccessManagerTest, Init_001, TestSize.Level0)
122 {
123 EXPECT_EQ(DH_FWK_SUCCESS, AccessManager::GetInstance()->Init());
124 }
125
126 /**
127 * @tc.name: OnRemoteDied_001
128 * @tc.desc: Verify the OnRemoteDied function
129 * @tc.type: FUNC
130 * @tc.require: AR000GHSJM
131 */
132 HWTEST_F(AccessManagerTest, OnRemoteDied_001, TestSize.Level0)
133 {
134 AccessManager::GetInstance()->OnRemoteDied();
135 EXPECT_EQ(DH_FWK_SUCCESS, AccessManager::GetInstance()->Init());
136 }
137
138 /**
139 * @tc.name: OnDeviceOnline_001
140 * @tc.desc: Verify the OnDeviceOnline function
141 * @tc.type: FUNC
142 * @tc.require: AR000GHSJM
143 */
144 HWTEST_F(AccessManagerTest, OnDeviceOnline_001, TestSize.Level0)
145 {
146 DmDeviceInfo deviceInfo;
147 AccessManager::GetInstance()->OnDeviceOnline(deviceInfo);
148 EXPECT_EQ(DH_FWK_SUCCESS, AccessManager::GetInstance()->Init());
149 }
150
151 /**
152 * @tc.name: OnDeviceOffline_001
153 * @tc.desc: Verify the OnDeviceOffline function
154 * @tc.type: FUNC
155 * @tc.require: AR000GHSJM
156 */
157 HWTEST_F(AccessManagerTest, OnDeviceOffline_001, TestSize.Level0)
158 {
159 DmDeviceInfo deviceInfo;
160 AccessManager::GetInstance()->OnDeviceOffline(deviceInfo);
161 EXPECT_EQ(DH_FWK_SUCCESS, AccessManager::GetInstance()->Init());
162 }
163
164 /**
165 * @tc.name: OnDeviceOffline_002
166 * @tc.desc: Verify the OnDeviceOffline function
167 * @tc.type: FUNC
168 * @tc.require: AR000GHSJM
169 */
170 HWTEST_F(AccessManagerTest, OnDeviceOffline_002, TestSize.Level0)
171 {
172 DmDeviceInfo deviceInfo;
173 std::string devId = "000001";
174 int32_t ret = memcpy_s(deviceInfo.deviceId, DM_MAX_DEVICE_ID_LEN, devId.c_str(), devId.length());
175 if (ret != EOK) {
176 return;
177 }
178 AccessManager::GetInstance()->OnDeviceOffline(deviceInfo);
179 EXPECT_EQ(DH_FWK_SUCCESS, AccessManager::GetInstance()->Init());
180 }
181
182 /**
183 * @tc.name: OnDeviceOffline_003
184 * @tc.desc: Verify the OnDeviceOffline function
185 * @tc.type: FUNC
186 * @tc.require: AR000GHSJM
187 */
188 HWTEST_F(AccessManagerTest, OnDeviceOffline_003, TestSize.Level0)
189 {
190 DmDeviceInfo deviceInfo;
191 std::string devId = "000001";
192 int32_t ret = memcpy_s(deviceInfo.deviceId, DM_MAX_DEVICE_ID_LEN, devId.c_str(), devId.length());
193 if (ret != EOK) {
194 return;
195 }
196 std::string netId = "000002";
197 ret = memcpy_s(deviceInfo.networkId, DM_MAX_DEVICE_ID_LEN, netId.c_str(), netId.length());
198 if (ret != EOK) {
199 return;
200 }
201 AccessManager::GetInstance()->OnDeviceOffline(deviceInfo);
202 EXPECT_EQ(DH_FWK_SUCCESS, AccessManager::GetInstance()->Init());
203 }
204
205 /**
206 * @tc.name: OnDeviceReady_001
207 * @tc.desc: Verify the OnDeviceReady function
208 * @tc.type: FUNC
209 * @tc.require: AR000GHSJM
210 */
211 HWTEST_F(AccessManagerTest, OnDeviceReady_001, TestSize.Level0)
212 {
213 DmDeviceInfo deviceInfo;
214 AccessManager::GetInstance()->OnDeviceReady(deviceInfo);
215 EXPECT_EQ(DH_FWK_SUCCESS, AccessManager::GetInstance()->Init());
216 }
217
218 /**
219 * @tc.name: OnDeviceReady_002
220 * @tc.desc: Verify the OnDeviceReady function
221 * @tc.type: FUNC
222 * @tc.require: AR000GHSJM
223 */
224 HWTEST_F(AccessManagerTest, OnDeviceReady_002, TestSize.Level0)
225 {
226 DmDeviceInfo deviceInfo;
227 std::string devId = "000001";
228 int32_t ret = memcpy_s(deviceInfo.deviceId, DM_MAX_DEVICE_ID_LEN, devId.c_str(), devId.length());
229 if (ret != EOK) {
230 return;
231 }
232 AccessManager::GetInstance()->OnDeviceReady(deviceInfo);
233 EXPECT_EQ(DH_FWK_SUCCESS, AccessManager::GetInstance()->Init());
234 }
235
236 /**
237 * @tc.name: OnDeviceReady_003
238 * @tc.desc: Verify the OnDeviceReady function
239 * @tc.type: FUNC
240 * @tc.require: AR000GHSJM
241 */
242 HWTEST_F(AccessManagerTest, OnDeviceReady_003, TestSize.Level0)
243 {
244 DmDeviceInfo deviceInfo;
245 std::string devId = "000001";
246 int32_t ret = memcpy_s(deviceInfo.deviceId, DM_MAX_DEVICE_ID_LEN, devId.c_str(), devId.length());
247 if (ret != EOK) {
248 return;
249 }
250 std::string netId = "000002";
251 ret = memcpy_s(deviceInfo.networkId, DM_MAX_DEVICE_ID_LEN, netId.c_str(), netId.length());
252 if (ret != EOK) {
253 return;
254 }
255 AccessManager::GetInstance()->OnDeviceReady(deviceInfo);
256 EXPECT_EQ(DH_FWK_SUCCESS, AccessManager::GetInstance()->Init());
257 }
258
259 /**
260 * @tc.name: OnDeviceChanged_001
261 * @tc.desc: Verify the OnDeviceChanged function
262 * @tc.type: FUNC
263 * @tc.require: AR000GHSJM
264 */
265 HWTEST_F(AccessManagerTest, OnDeviceChanged_001, TestSize.Level0)
266 {
267 DmDeviceInfo deviceInfo;
268 AccessManager::GetInstance()->OnDeviceChanged(deviceInfo);
269 EXPECT_EQ(DH_FWK_SUCCESS, AccessManager::GetInstance()->Init());
270 }
271
272 /**
273 * @tc.name: UnInit_001
274 * @tc.desc: Verify the UnInit function
275 * @tc.type: FUNC
276 * @tc.require: AR000GHSJM
277 */
278 HWTEST_F(AccessManagerTest, UnInit_001, TestSize.Level0)
279 {
280 EXPECT_EQ(DH_FWK_SUCCESS, AccessManager::GetInstance()->UnInit());
281 }
282
283 /**
284 * @tc.name: CheckExitSAOrNot_001
285 * @tc.desc: Verify the CheckExitSAOrNot function
286 * @tc.type: FUNC
287 * @tc.require: AR000GHSJM
288 */
289 HWTEST_F(AccessManagerTest, CheckExitSAOrNot_001, TestSize.Level0)
290 {
291 DistributedHardwareManagerFactory::GetInstance().CheckExitSAOrNot();
292 ASSERT_TRUE(DistributedHardwareManagerFactory::GetInstance().IsInit());
293 }
294
295 /**
296 * @tc.name: InitLocalDevInfo_001
297 * @tc.desc: Verify the InitLocalDevInfo function
298 * @tc.type: FUNC
299 * @tc.require: AR000GHSJM
300 */
301 HWTEST_F(AccessManagerTest, InitLocalDevInfo_001, TestSize.Level0)
302 {
303 ASSERT_TRUE(DistributedHardwareManagerFactory::GetInstance().InitLocalDevInfo());
304 }
305 } // namespace DistributedHardware
306 } // namespace OHOS
307