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 <vector>
17 #include <memory>
18 
19 #include <unistd.h>
20 
21 #include "device_manager.h"
22 #include <gtest/gtest.h>
23 #include "monitor.h"
24 
25 #include "devicestatus_define.h"
26 #include "devicestatus_errors.h"
27 
28 #undef LOG_TAG
29 #define LOG_TAG "MonitorTest"
30 
31 namespace OHOS {
32 namespace Msdp {
33 namespace DeviceStatus {
34 using namespace testing::ext;
35 namespace {
36 constexpr int32_t TIME_WAIT_FOR_OP_MS { 20 };
37 const std::string TEST_DEV_NODE {"TestDeviceNode"};
38 } // namespace
39 
40 class MonitorTest : public testing::Test {
41 public:
42     void SetUp();
43     void TearDown();
44     static void SetUpTestCase();
45     static void TearDownTestCase(void);
46 };
SetUpTestCase()47 void MonitorTest::SetUpTestCase() {}
48 
TearDownTestCase()49 void MonitorTest::TearDownTestCase() {}
50 
SetUp()51 void MonitorTest::SetUp() {}
52 
TearDown()53 void MonitorTest::TearDown()
54 {
55     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
56 }
57 
58 class TestDeviceMgr : public IDeviceMgr {
59 public:
60     TestDeviceMgr() = default;
61     ~TestDeviceMgr() = default;
AddDevice(const std::string & devNode)62     void AddDevice(const std::string &devNode) override
63     {
64         devMgr_.DeviceManager::AddDevice(devNode);
65     }
RemoveDevice(const std::string & devNode)66     void RemoveDevice(const std::string &devNode) override
67     {
68         devMgr_.DeviceManager::RemoveDevice(devNode);
69     }
70 private:
71     DeviceManager devMgr_;
72 };
73 
74 /**
75  * @tc.name: MonitorTest01
76  * @tc.desc: test Dispatch event
77  * @tc.type: FUNC
78  * @tc.require:
79  */
80 HWTEST_F(MonitorTest, MonitorTest01, TestSize.Level1)
81 {
82     Monitor monitor;
83     struct epoll_event ev;
84     ev.events = EPOLLIN;
85     ASSERT_NO_FATAL_FAILURE(monitor.Dispatch(ev));
86     ev.events = EPOLLHUP;
87     ASSERT_NO_FATAL_FAILURE(monitor.Dispatch(ev));
88     ev.events = EPOLLERR;
89     ASSERT_NO_FATAL_FAILURE(monitor.Dispatch(ev));
90 }
91 
92 /**
93  * @tc.name: MonitorTest02
94  * @tc.desc: test Enable and Disable
95  * @tc.type: FUNC
96  * @tc.require:
97  */
98 HWTEST_F(MonitorTest, MonitorTest02, TestSize.Level1)
99 {
100     Monitor monitor;
101     int32_t ret = monitor.Enable();
102     EXPECT_EQ(ret, RET_OK);
103     ASSERT_NO_FATAL_FAILURE(monitor.Disable());
104 }
105 
106 /**
107  * @tc.name: MonitorTest03
108  * @tc.desc: test OpenConnection and EnableReceiving
109  * @tc.type: FUNC
110  * @tc.require:
111  */
112 HWTEST_F(MonitorTest, MonitorTest03, TestSize.Level1)
113 {
114     Monitor monitor;
115     int32_t ret = monitor.OpenConnection();
116     EXPECT_EQ(ret, RET_OK);
117     ret = monitor.EnableReceiving();
118     EXPECT_EQ(ret, RET_OK);
119 }
120 
121 /**
122  * @tc.name: MonitorTest04
123  * @tc.desc: test AddDevice and RemoveDevice
124  * @tc.type: FUNC
125  * @tc.require:
126  */
127 HWTEST_F(MonitorTest, MonitorTest04, TestSize.Level1)
128 {
129     Monitor monitor;
130     ASSERT_NO_FATAL_FAILURE(monitor.AddDevice(TEST_DEV_NODE));
131     ASSERT_NO_FATAL_FAILURE(monitor.RemoveDevice(TEST_DEV_NODE));
132 }
133 
134 /**
135  * @tc.name: MonitorTest05
136  * @tc.desc: test SetDeviceMgr
137  * @tc.type: FUNC
138  * @tc.require:
139  */
140 HWTEST_F(MonitorTest, MonitorTest05, TestSize.Level1)
141 {
142     Monitor monitor;
143     std::shared_ptr<TestDeviceMgr> testDevMgr = std::make_shared<TestDeviceMgr>();
144     IDeviceMgr *deviceMgr = testDevMgr.get();
145     ASSERT_NO_FATAL_FAILURE(monitor.SetDeviceMgr(deviceMgr));
146 }
147 
148 /**
149  * @tc.name: MonitorTest06
150  * @tc.desc: test HandleInotifyEvent
151  * @tc.type: FUNC
152  * @tc.require:
153  */
154 HWTEST_F(MonitorTest, MonitorTest06, TestSize.Level1)
155 {
156     Monitor monitor;
157     char buf[sizeof(struct inotify_event) + NAME_MAX + 1];
158     struct inotify_event *event = reinterpret_cast<struct inotify_event *>(buf);
159     event->name[0] = '\0';
160     ASSERT_NO_FATAL_FAILURE(monitor.HandleInotifyEvent(event));
161 }
162 
163 /**
164  * @tc.name: MonitorTest07
165  * @tc.desc: test HandleInotifyEvent
166  * @tc.type: FUNC
167  * @tc.require:
168  */
169 HWTEST_F(MonitorTest, MonitorTest07, TestSize.Level1)
170 {
171     Monitor monitor;
172     char buf[sizeof(struct inotify_event) + NAME_MAX + 1];
173     struct inotify_event *event = reinterpret_cast<struct inotify_event *>(buf);
174     const char* name = "test_device";
175     size_t nameLen = strlen(name) + 1;
176     memcpy_s(event->name, nameLen, name, nameLen);
177     event->mask = IN_CREATE;
178     ASSERT_NO_FATAL_FAILURE(monitor.HandleInotifyEvent(event));
179 }
180 
181 /**
182  * @tc.name: MonitorTest08
183  * @tc.desc: test HandleInotifyEvent
184  * @tc.type: FUNC
185  * @tc.require:
186  */
187 HWTEST_F(MonitorTest, MonitorTest08, TestSize.Level1)
188 {
189     Monitor monitor;
190     char buf[sizeof(struct inotify_event) + NAME_MAX + 1];
191     struct inotify_event *event = reinterpret_cast<struct inotify_event *>(buf);
192     const char* name = "test_device";
193     size_t nameLen = strlen(name) + 1;
194     memcpy_s(event->name, nameLen, name, nameLen);
195     event->mask = IN_DELETE;
196     ASSERT_NO_FATAL_FAILURE(monitor.HandleInotifyEvent(event));
197 }
198 } // namespace DeviceStatus
199 } // namespace Msdp
200 } // namespace OHOS