• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 <cstdio>
17 #include <dlfcn.h>
18 #include <gtest/gtest.h>
19 
20 #include "accesstoken_kit.h"
21 #include "devicestatus_data_define.h"
22 #include "devicestatus_define.h"
23 #define private public
24 #include "devicestatus_data_parse.h"
25 #include "devicestatus_msdp_mock.h"
26 #undef private
27 #include "devicestatus_msdp_interface.h"
28 #include "devicestatus_msdp_mock.h"
29 #include "devicestatus_msdp_client_impl.h"
30 #include "sensor_data_callback.h"
31 
32 namespace OHOS {
33 namespace Msdp {
34 namespace DeviceStatus {
35 using namespace testing::ext;
36 namespace {
37 std::shared_ptr<DeviceStatusMsdpMock> g_testMock;
38 constexpr ::OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "DeviceStatusMsdpMocKTest" };
39 #ifdef __aarch64__
40 const std::string DEVICESTATUS_MOCK_LIB_PATH { "/system/lib64/libdevicestatus_mock.z.so" };
41 #else
42 const std::string DEVICESTATUS_MOCK_LIB_PATH { "/system/lib/libdevicestatus_mock.z.so" };
43 #endif
44 } // namespace
45 
46 class DeviceStatusMsdpMocKTest : public testing::Test {
47 public:
48     static void SetUpTestCase();
49     static void TearDownTestCase();
50     void SetUp();
51     void TearDown();
52     int32_t LoadMockLibrary(const std::shared_ptr<MsdpAlgoHandle> &mockHandler);
53     int32_t UnloadMockLibrary(const std::shared_ptr<MsdpAlgoHandle> &mockHandler);
54 };
55 
SetUpTestCase()56 void DeviceStatusMsdpMocKTest::SetUpTestCase()
57 {
58     g_testMock = std::make_shared<DeviceStatusMsdpMock>();
59 }
60 
TearDownTestCase()61 void DeviceStatusMsdpMocKTest::TearDownTestCase()
62 {
63     g_testMock = nullptr;
64 }
65 
SetUp()66 void DeviceStatusMsdpMocKTest::SetUp() {}
67 
TearDown()68 void DeviceStatusMsdpMocKTest::TearDown() {}
69 
LoadMockLibrary(const std::shared_ptr<MsdpAlgoHandle> & mockHandler)70 int32_t DeviceStatusMsdpMocKTest::LoadMockLibrary(const std::shared_ptr<MsdpAlgoHandle> &mockHandler)
71 {
72     FI_HILOGI("Enter");
73     if (mockHandler == nullptr) {
74         FI_HILOGE("mockHandler is nullptr");
75         return RET_ERR;
76     }
77     if (mockHandler->handle != nullptr) {
78         FI_HILOGE("handle has exists");
79         return RET_OK;
80     }
81 
82     std::string dlName = DEVICESTATUS_MOCK_LIB_PATH;
83     char libRealPath[PATH_MAX] = { 0 };
84     if (realpath(dlName.c_str(), libRealPath) == nullptr) {
85         FI_HILOGE("Get absolute algoPath is error, errno:%{public}d", errno);
86         return RET_ERR;
87     }
88 
89     mockHandler->handle = dlopen(libRealPath, RTLD_LAZY);
90     if (mockHandler->handle == nullptr) {
91         FI_HILOGE("Cannot load library error:%{public}s", dlerror());
92         return RET_ERR;
93     }
94 
95     mockHandler->create = reinterpret_cast<IMsdp* (*)()>(dlsym(mockHandler->handle, "Create"));
96     mockHandler->destroy = reinterpret_cast<void *(*)(IMsdp*)>(dlsym(mockHandler->handle, "Destroy"));
97     if (mockHandler->create == nullptr || mockHandler->destroy == nullptr) {
98         FI_HILOGE("%{public}s dlsym create or destroy failed", dlName.c_str());
99         dlclose(mockHandler->handle);
100         mockHandler->Clear();
101         return RET_ERR;
102     }
103     return RET_OK;
104 }
105 
UnloadMockLibrary(const std::shared_ptr<MsdpAlgoHandle> & mockHandler)106 int32_t DeviceStatusMsdpMocKTest::UnloadMockLibrary(const std::shared_ptr<MsdpAlgoHandle> &mockHandler)
107 {
108     FI_HILOGI("Enter");
109     if (mockHandler == nullptr) {
110         FI_HILOGE("mockHandler is nullptr");
111         return RET_ERR;
112     }
113     if (mockHandler->handle == nullptr) {
114         FI_HILOGE("handle is nullptr");
115         return RET_ERR;
116     }
117 
118     if (mockHandler->pAlgorithm != nullptr) {
119         mockHandler->destroy(mockHandler->pAlgorithm);
120         mockHandler->pAlgorithm = nullptr;
121     }
122     dlclose(mockHandler->handle);
123     mockHandler->Clear();
124     return RET_OK;
125 }
126 
127 /**
128  * @tc.name: DeviceStatusMsdpMocKTest001
129  * @tc.desc: test devicestatus Mock in Algorithm
130  * @tc.type: FUNC
131  */
132 HWTEST_F(DeviceStatusMsdpMocKTest, DeviceStatusMsdpMocKTest001, TestSize.Level1)
133 {
134     CALL_TEST_DEBUG;
135     EXPECT_TRUE(g_testMock->Init());
136     auto callback = std::make_shared<DeviceStatusMsdpClientImpl>();
137     EXPECT_TRUE(g_testMock->RegisterCallback(callback) == ERR_OK);
138     EXPECT_TRUE(g_testMock->UnregisterCallback() == ERR_OK);
139 }
140 
141 /**
142  * @tc.name: DeviceStatusMsdpMocKTest002
143  * @tc.desc: test devicestatus Mock in Algorithm
144  * @tc.type: FUNC
145  */
146 HWTEST_F(DeviceStatusMsdpMocKTest, DeviceStatusMsdpMocKTest002, TestSize.Level1)
147 {
148     CALL_TEST_DEBUG;
149     EXPECT_TRUE(g_testMock->Init());
150     auto callback = std::make_shared<DeviceStatusMsdpClientImpl>();
151     EXPECT_TRUE(g_testMock->RegisterCallback(callback) == ERR_OK);
152     EXPECT_TRUE(g_testMock->Enable(Type::TYPE_INVALID) == ERR_OK);
153     EXPECT_TRUE(g_testMock->Disable(Type::TYPE_INVALID) == ERR_OK);
154     EXPECT_TRUE(g_testMock->UnregisterCallback() == ERR_OK);
155 }
156 
157 /**
158  * @tc.name: DeviceStatusMsdpMocKTest003
159  * @tc.desc: test devicestatus Mock in Algorithm
160  * @tc.type: FUNC
161  */
162 HWTEST_F(DeviceStatusMsdpMocKTest, DeviceStatusMsdpMocKTest003, TestSize.Level1)
163 {
164     CALL_TEST_DEBUG;
165     EXPECT_TRUE(g_testMock->Init());
166     auto callback = std::make_shared<DeviceStatusMsdpClientImpl>();
167     EXPECT_TRUE(g_testMock->RegisterCallback(callback) == ERR_OK);
168     EXPECT_TRUE(g_testMock->Enable(Type::TYPE_ABSOLUTE_STILL) == ERR_OK);
169     EXPECT_TRUE(g_testMock->Disable(Type::TYPE_ABSOLUTE_STILL) == ERR_OK);
170     EXPECT_TRUE(g_testMock->UnregisterCallback() == ERR_OK);
171 }
172 
173 /**
174  * @tc.name: DeviceStatusMsdpMocKTest004
175  * @tc.desc: test devicestatus Mock in Algorithm
176  * @tc.type: FUNC
177  */
178 HWTEST_F(DeviceStatusMsdpMocKTest, DeviceStatusMsdpMocKTest004, TestSize.Level1)
179 {
180     CALL_TEST_DEBUG;
181     EXPECT_TRUE(g_testMock->Init());
182     auto callback = std::make_shared<DeviceStatusMsdpClientImpl>();
183     EXPECT_TRUE(g_testMock->RegisterCallback(callback) == ERR_OK);
184     EXPECT_TRUE(g_testMock->Enable(Type::TYPE_HORIZONTAL_POSITION) == ERR_OK);
185     EXPECT_TRUE(g_testMock->Disable(Type::TYPE_HORIZONTAL_POSITION) == ERR_OK);
186     EXPECT_TRUE(g_testMock->UnregisterCallback() == ERR_OK);
187 }
188 
189 /**
190  * @tc.name: DeviceStatusMsdpMocKTest005
191  * @tc.desc: test devicestatus Mock in Algorithm
192  * @tc.type: FUNC
193  */
194 HWTEST_F(DeviceStatusMsdpMocKTest, DeviceStatusMsdpMocKTest005, TestSize.Level1)
195 {
196     CALL_TEST_DEBUG;
197     EXPECT_TRUE(g_testMock->Init());
198     auto callback = std::make_shared<DeviceStatusMsdpClientImpl>();
199     EXPECT_TRUE(g_testMock->RegisterCallback(callback) == ERR_OK);
200     EXPECT_TRUE(g_testMock->Enable(Type::TYPE_VERTICAL_POSITION) == ERR_OK);
201     EXPECT_TRUE(g_testMock->Disable(Type::TYPE_VERTICAL_POSITION) == ERR_OK);
202     EXPECT_TRUE(g_testMock->UnregisterCallback() == ERR_OK);
203 }
204 
205 /**
206  * @tc.name: DeviceStatusMsdpMocKTest006
207  * @tc.desc: test devicestatus Mock in Algorithm
208  * @tc.type: FUNC
209  */
210 HWTEST_F(DeviceStatusMsdpMocKTest, DeviceStatusMsdpMocKTest006, TestSize.Level1)
211 {
212     CALL_TEST_DEBUG;
213     EXPECT_TRUE(g_testMock->Init());
214     auto callback = std::make_shared<DeviceStatusMsdpClientImpl>();
215     EXPECT_TRUE(g_testMock->RegisterCallback(callback) == ERR_OK);
216     EXPECT_TRUE(g_testMock->Enable(Type::TYPE_LID_OPEN) == ERR_OK);
217     EXPECT_TRUE(g_testMock->Disable(Type::TYPE_LID_OPEN) == ERR_OK);
218     EXPECT_TRUE(g_testMock->UnregisterCallback() == ERR_OK);
219 }
220 
221 /**
222  * @tc.name: DeviceStatusMsdpMocKTest007
223  * @tc.desc: test devicestatus Mock in Algorithm
224  * @tc.type: FUNC
225  */
226 HWTEST_F(DeviceStatusMsdpMocKTest, DeviceStatusMsdpMocKTest007, TestSize.Level1)
227 {
228     CALL_TEST_DEBUG;
229     EXPECT_TRUE(g_testMock->Init());
230     auto callback = std::make_shared<DeviceStatusMsdpClientImpl>();
231     EXPECT_TRUE(g_testMock->RegisterCallback(callback) == ERR_OK);
232     EXPECT_TRUE(g_testMock->Enable(Type::TYPE_MAX) == ERR_OK);
233     EXPECT_TRUE(g_testMock->Disable(Type::TYPE_MAX) == ERR_OK);
234     EXPECT_TRUE(g_testMock->UnregisterCallback() == ERR_OK);
235 }
236 
237 /**
238  * @tc.name: DeviceStatusMsdpMocKTest008
239  * @tc.desc: test devicestatus DisableCount
240  * @tc.type: FUNC
241  */
242 HWTEST_F(DeviceStatusMsdpMocKTest, DeviceStatusMsdpMocKTest008, TestSize.Level1)
243 {
244     CALL_TEST_DEBUG;
245     EXPECT_TRUE(g_testMock->Init());
246     auto callback = std::make_shared<DeviceStatusMsdpClientImpl>();
247     EXPECT_TRUE(g_testMock->RegisterCallback(callback) == ERR_OK);
248     EXPECT_TRUE(g_testMock->Enable(Type::TYPE_HORIZONTAL_POSITION) == ERR_OK);
249     EXPECT_TRUE(g_testMock->Disable(Type::TYPE_HORIZONTAL_POSITION) == ERR_OK);
250     EXPECT_TRUE(g_testMock->UnregisterCallback() == ERR_OK);
251     EXPECT_TRUE(g_testMock->DisableCount(Type::TYPE_HORIZONTAL_POSITION) == ERR_OK);
252 }
253 
254 /**
255  * @tc.name: DeviceStatusMsdpMocKTest009
256  * @tc.desc: test devicestatus NotifyMsdpImpl
257  * @tc.type: FUNC
258  */
259 HWTEST_F(DeviceStatusMsdpMocKTest, DeviceStatusMsdpMocKTest009, TestSize.Level1)
260 {
261     CALL_TEST_DEBUG;
262     EXPECT_FALSE(g_testMock->NotifyMsdpImpl({}) == ERR_OK);
263 }
264 
265 /**
266  * @tc.name: DeviceStatusMsdpMocKTest010
267  * @tc.desc: test devicestatus Mock in Algorithm
268  * @tc.type: FUNC
269  */
270 HWTEST_F(DeviceStatusMsdpMocKTest, DeviceStatusMsdpMocKTest010, TestSize.Level1)
271 {
272     CALL_TEST_DEBUG;
273     g_testMock->InitTimer();
274     g_testMock->StartThread();
275     std::make_unique<std::thread>(&DeviceStatusMsdpMock::LoopingThreadEntry, g_testMock)->detach();
276     constexpr int32_t TIMER_INTERVAL = 3;
277     int32_t ret = g_testMock->SetTimerInterval(TIMER_INTERVAL);
278     g_testMock->CloseTimer();
279     EXPECT_TRUE(ret == ERR_OK);
280 }
281 
282 /**
283  * @tc.name: DeviceStatusMsdpMocKTest011
284  * @tc.desc: test devicestatus Mock in Algorithm
285  * @tc.type: FUNC
286  */
287 HWTEST_F(DeviceStatusMsdpMocKTest, DeviceStatusMsdpMocKTest011, TestSize.Level1)
288 {
289     CALL_TEST_DEBUG;
290     g_testMock->InitTimer();
291     g_testMock->StartThread();
292     std::make_unique<std::thread>(&DeviceStatusMsdpMock::LoopingThreadEntry, g_testMock)->detach();
293     constexpr int32_t TIMER_INTERVAL = -1;
294     int32_t ret = g_testMock->SetTimerInterval(TIMER_INTERVAL);
295     g_testMock->CloseTimer();
296     EXPECT_TRUE(ret == RET_ERR);
297 }
298 
299 /**
300  * @tc.name: DeviceStatusMsdpMocKTest012
301  * @tc.desc: test devicestatus Mock in Algorithm
302  * @tc.type: FUNC
303  */
304 HWTEST_F(DeviceStatusMsdpMocKTest, DeviceStatusMsdpMocKTest012, TestSize.Level1)
305 {
306     CALL_TEST_DEBUG;
307     g_testMock->InitTimer();
308     g_testMock->StartThread();
309     std::make_unique<std::thread>(&DeviceStatusMsdpMock::LoopingThreadEntry, g_testMock)->detach();
310     constexpr int32_t TIMER_INTERVAL = 0;
311     int32_t ret = g_testMock->SetTimerInterval(TIMER_INTERVAL);
312     g_testMock->CloseTimer();
313     EXPECT_TRUE(ret == ERR_OK);
314 }
315 
316 /**
317  * @tc.name: DeviceStatusMsdpMocKTest013
318  * @tc.desc: test devicestatus Mock in Algorithm
319  * @tc.type: FUNC
320  */
321 HWTEST_F(DeviceStatusMsdpMocKTest, DeviceStatusMsdpMocKTest013, TestSize.Level1)
322 {
323     CALL_TEST_DEBUG;
324     g_testMock->InitTimer();
325     g_testMock->StartThread();
326     std::make_unique<std::thread>(&DeviceStatusMsdpMock::LoopingThreadEntry, g_testMock)->detach();
327     constexpr int32_t TIMER_INTERVAL = 0;
328     int32_t ret = g_testMock->SetTimerInterval(TIMER_INTERVAL);
329     EXPECT_TRUE(ret == ERR_OK);
330     g_testMock->TimerCallback();
331     ret = g_testMock->GetDeviceStatusData();
332     g_testMock->CloseTimer();
333     EXPECT_TRUE(ret == ERR_OK);
334 }
335 
336 /**
337  * @tc.name: DeviceStatusMsdpMocKTest014
338  * @tc.desc: test devicestatus NotifyMsdpImpl
339  * @tc.type: FUNC
340  */
341 HWTEST_F(DeviceStatusMsdpMocKTest, DeviceStatusMsdpMocKTest014, TestSize.Level1)
342 {
343     CALL_TEST_DEBUG;
344     g_testMock->GetCallbackImpl() = nullptr;
345     EXPECT_FALSE(g_testMock->NotifyMsdpImpl({}) == ERR_OK);
346 }
347 
348 /**
349  * @tc.name: DeviceStatusMsdpMocKTest015
350  * @tc.desc: test devicestatus NotifyMsdpImpl
351  * @tc.type: FUNC
352  */
353 HWTEST_F(DeviceStatusMsdpMocKTest, DeviceStatusMsdpMocKTest015, TestSize.Level1)
354 {
355     CALL_TEST_DEBUG;
356     auto callback = std::make_shared<DeviceStatusMsdpClientImpl>();
357     EXPECT_TRUE(g_testMock->RegisterCallback(callback) == ERR_OK);
358     EXPECT_FALSE(g_testMock->NotifyMsdpImpl({TYPE_INVALID, VALUE_INVALID}) == ERR_OK);
359 }
360 
361 /**
362  * @tc.name: DeviceStatusMsdpMocKTest016
363  * @tc.desc: test devicestatus NotifyMsdpImpl
364  * @tc.type: FUNC
365  */
366 HWTEST_F(DeviceStatusMsdpMocKTest, DeviceStatusMsdpMocKTest016, TestSize.Level1)
367 {
368     CALL_TEST_DEBUG;
369     g_testMock->dataParse_ = nullptr;
370     int32_t ret = g_testMock->GetDeviceStatusData();
371     EXPECT_TRUE(ret == RET_ERR);
372 }
373 
374 /**
375  * @tc.name: DeviceStatusMsdpMocKTest017
376  * @tc.desc: test devicestatus Mock in Algorithm
377  * @tc.type: FUNC
378  */
379 HWTEST_F(DeviceStatusMsdpMocKTest, DeviceStatusMsdpMocKTest017, TestSize.Level1)
380 {
381     CALL_TEST_DEBUG;
382     constexpr int32_t TIMER_INTERVAL = 0;
383     int32_t ret = g_testMock->SetTimerInterval(TIMER_INTERVAL);
384     EXPECT_TRUE(ret == RET_ERR);
385 }
386 
387 /**
388  * @tc.name: DeviceStatusMsdpMocKTest018
389  * @tc.desc: test devicestatus RegisterCallback
390  * @tc.type: FUNC
391  */
392 HWTEST_F(DeviceStatusMsdpMocKTest, DeviceStatusMsdpMocKTest018, TestSize.Level1)
393 {
394     CALL_TEST_DEBUG;
395     std::shared_ptr<MsdpAlgoHandle> mock = std::make_shared<MsdpAlgoHandle>();
396     int32_t ret = LoadMockLibrary(mock);
397     ASSERT_EQ(ret, RET_OK);
398     ASSERT_NE(mock->handle, nullptr);
399     mock->pAlgorithm = mock->create();
400 
401     std::shared_ptr<DeviceStatusMsdpClientImpl> callback = std::make_shared<DeviceStatusMsdpClientImpl>();
402     EXPECT_TRUE(mock->pAlgorithm->RegisterCallback(callback) == ERR_OK);
403     EXPECT_TRUE(mock->pAlgorithm->UnregisterCallback() == ERR_OK);
404 
405     ret = UnloadMockLibrary(mock);
406     ASSERT_EQ(ret, RET_OK);
407 }
408 
409 /**
410  * @tc.name: DeviceStatusMsdpMocKTest019
411  * @tc.desc: test devicestatus Mock in Algorithm
412  * @tc.type: FUNC
413  */
414 HWTEST_F(DeviceStatusMsdpMocKTest, DeviceStatusMsdpMocKTest019, TestSize.Level1)
415 {
416     CALL_TEST_DEBUG;
417     g_testMock->TimerCallback();
418     constexpr int32_t TIMER_INTERVAL = 0;
419     FI_HILOGI("Test the abnormal branch.");
420     int32_t ret = g_testMock->SetTimerInterval(TIMER_INTERVAL);
421     g_testMock->CloseTimer();
422     EXPECT_TRUE(ret == RET_ERR);
423 }
424 } // namespace DeviceStatus
425 } // namespace Msdp
426 } // namespace OHOS
427