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 "risk_collect_test.h"
17 
18 #include "gmock/gmock.h"
19 
20 #include "security_guard_define.h"
21 #include "security_guard_log.h"
22 #include "security_guard_utils.h"
23 #define private public
24 #define protected public
25 #include "config_data_manager.h"
26 #include "database_manager.h"
27 #include "uevent_listener_impl.h"
28 #include "uevent_listener.h"
29 #include "uevent_notify.h"
30 #include "data_format.h"
31 #include "syspara/parameters.h"
32 #undef private
33 #undef protected
34 
35 using namespace testing;
36 using namespace testing::ext;
37 using namespace OHOS::Security::SecurityGuard;
38 using namespace OHOS::Security::SecurityGuardTest;
39 
40 namespace OHOS {
41     constexpr uint32_t MAX_CONTENT_SIZE = 900;
42 }
43 
44 namespace OHOS::Security::SecurityGuardTest {
SetUpTestCase()45 void RiskCollectTest::SetUpTestCase()
46 {
47 }
48 
TearDownTestCase()49 void RiskCollectTest::TearDownTestCase()
50 {
51 }
52 
SetUp()53 void RiskCollectTest::SetUp()
54 {
55 }
56 
TearDown()57 void RiskCollectTest::TearDown()
58 {
59 }
60 
61 class MockUeventListenerImpl : public UeventListenerImpl {
62 public:
MockUeventListenerImpl(KernelInterfaceAdapter adapter)63     explicit MockUeventListenerImpl(KernelInterfaceAdapter adapter) : UeventListenerImpl(adapter) {}
64     ~MockUeventListenerImpl() override = default;
65     MOCK_METHOD0(InitUevent, bool());
66     MOCK_METHOD2(UeventListen, int(char *buffer, size_t length));
67     MOCK_METHOD2(ParseEvent, void(char *buffer, size_t length));
68 };
69 
70 class MockKernelInterfaceAdapter : public KernelInterfaceAdapter {
71 public:
72     MockKernelInterfaceAdapter() = default;
73     ~MockKernelInterfaceAdapter() override = default;
74     MOCK_METHOD3(Socket, int(int af, int type, int protocol));
75     MOCK_METHOD3(Bind, int(int fd, const struct sockaddr* addr, socklen_t addrLength));
76     MOCK_METHOD3(Poll, int(struct pollfd* const fds, nfds_t fdCount, int timeout));
77     MOCK_METHOD4(Recv, ssize_t(int socket, void* const buf, size_t len, int flags));
78     MOCK_METHOD2(Open, int(const char* const pathName, int flags));
79     MOCK_METHOD3(Write, ssize_t(int fd, const void* const buf, size_t count));
80 };
81 
82 /**
83  * @tc.name: TestUeventListener001
84  * @tc.desc: Test Start with mock
85  * @tc.type: FUNC
86  * @tc.require: SR000H96L5
87  */
88 HWTEST_F(RiskCollectTest, TestUeventListener001, TestSize.Level1)
89 {
90     std::string deviceType = OHOS::system::GetDeviceType();
91     if (deviceType == "2in1") {
92         KernelInterfaceAdapter adapter;
93         MockUeventListenerImpl mockObj(adapter);
94         EXPECT_CALL(mockObj, InitUevent).Times(AtLeast(1)).WillRepeatedly(Return(false));
95         UeventListener listener(mockObj);
96         listener.Start();
97     }
98 }
99 
100 /**
101  * @tc.name: TestUeventListener002
102  * @tc.desc: Test InitUevent with mock
103  * @tc.type: FUNC
104  * @tc.require: SR000H96L5
105  */
106 HWTEST_F(RiskCollectTest, TestUeventListener002, TestSize.Level1)
107 {
108     MockKernelInterfaceAdapter mockObj;
109     UeventListenerImpl impl(mockObj);
110     EXPECT_CALL(mockObj, Socket).Times(AtLeast(1)).WillOnce(Return(-1)).WillRepeatedly(Return(0));
111     EXPECT_CALL(mockObj, Bind).Times(AtLeast(1)).WillOnce(Return(-1)).WillRepeatedly(Return(0));
112     bool isSuccess = impl.InitUevent();
113     EXPECT_FALSE(isSuccess);
114     isSuccess = impl.InitUevent();
115     EXPECT_FALSE(isSuccess);
116     isSuccess = impl.InitUevent();
117     EXPECT_FALSE(isSuccess);
118     isSuccess = impl.InitUevent();
119     EXPECT_FALSE(isSuccess);
120 }
121 
122 /**
123  * @tc.name: TestUeventListener003
124  * @tc.desc: Test UeventListen with mock
125  * @tc.type: FUNC
126  * @tc.require: SR000H96L5
127  */
128 HWTEST_F(RiskCollectTest, TestUeventListener003, TestSize.Level1)
129 {
130     MockKernelInterfaceAdapter mockObj;
131     UeventListenerImpl impl(mockObj);
132     char buffer[1024] = { 0 };
133     EXPECT_CALL(mockObj, Poll).Times(AtLeast(1)).WillOnce(Return(0)).WillOnce(
__anon8dea7afb0102(struct pollfd* const fds, nfds_t fdCount, int timeout) 134         [] (struct pollfd* const fds, nfds_t fdCount, int timeout) -> int {
135             fds->revents = -1;
136             return 1;
137         }).WillOnce(
__anon8dea7afb0202(struct pollfd* const fds, nfds_t fdCount, int timeout) 138             [] (struct pollfd* const fds, nfds_t fdCount, int timeout) -> int {
139                 fds->revents = 0;
140                 return 1;
141             }).WillRepeatedly(
__anon8dea7afb0302(struct pollfd* const fds, nfds_t fdCount, int timeout) 142                 [] (struct pollfd* const fds, nfds_t fdCount, int timeout) -> int {
143                     fds->revents = 1;
144                     return 1;
145                 });
146     EXPECT_CALL(mockObj, Recv).Times(AtLeast(1)).WillOnce(Return(0)).WillRepeatedly(Return(1));
147     int32_t count = impl.UeventListen(nullptr, 0);
148     EXPECT_EQ(count, 0);
149     count = impl.UeventListen(buffer, sizeof(buffer));
150     EXPECT_EQ(count, 0);
151     count = impl.UeventListen(buffer, sizeof(buffer) - 1);
152     EXPECT_EQ(count, 1);
153 }
154 
155 /**
156  * @tc.name: TestUeventListener004
157  * @tc.desc: Test ParseEvent with different content
158  * @tc.type: FUNC
159  * @tc.require: SR000H96L5
160  */
161 HWTEST_F(RiskCollectTest, TestUeventListener004, TestSize.Level1)
162 {
163     KernelInterfaceAdapter obj;
164     UeventListenerImpl impl(obj);
165     char buffer[1024] = { 0 };
166     impl.ParseEvent(nullptr, 0);
167     impl.ParseEvent(buffer, sizeof(buffer) + 1);
168     impl.ParseEvent(buffer, sizeof(buffer) - 1);
169     EXPECT_CALL(DatabaseManager::GetInstance(), InsertEvent).WillOnce(Return(FAILED)).WillOnce(Return(SUCCESS));
170     const char* content = "SG_KERNEL_COLLECT_DATA_CMD=1-0-34-{\"status\":1, \"cred\":1,\"extra\":\"\"}";
171     (void) memset_s(buffer, sizeof(buffer), 0, sizeof(buffer));
172     errno_t rc = memcpy_s(buffer, sizeof(buffer), content, strlen(content));
173     EXPECT_TRUE(rc == EOK);
174     impl.ParseEvent(buffer, strlen(content));
175 
176     const char* content1 = "SG_KERNEL_COLLECT_DATA_CMD=1-0-38-{\"status\":\"1\", \"cred\":\"1\",\"extra\":\"\"}";
177     (void) memset_s(buffer, sizeof(buffer), 0, sizeof(buffer));
178     rc = memcpy_s(buffer, sizeof(buffer), content1, strlen(content1));
179     EXPECT_TRUE(rc == EOK);
180     impl.ParseEvent(buffer, strlen(content1));
181 
182     const char* content2 = "SG_KERNEL_COLLECT_DATA_CMD=1-0-39-{\"status\":\"1\", \"cred\":\"1\",\"extra\":\"\"}";
183     (void) memset_s(buffer, sizeof(buffer), 0, sizeof(buffer));
184     rc = memcpy_s(buffer, sizeof(buffer), content2, strlen(content2));
185     EXPECT_TRUE(rc == EOK);
186     impl.ParseEvent(buffer, strlen(content2));
187 
188     const char* content3 = "SG_KERNEL_COLLECT_DATA_CMD=1-0-34-{\"status\":1, \"cred\":1,\"extra\":\"\"}-0";
189     (void) memset_s(buffer, sizeof(buffer), 0, sizeof(buffer));
190     rc = memcpy_s(buffer, sizeof(buffer), content3, strlen(content3));
191     EXPECT_TRUE(rc == EOK);
192     impl.ParseEvent(buffer, strlen(content3));
193 }
194 
195 /**
196  * @tc.name: TestUeventNotify001
197  * @tc.desc: Test NotifyScan with mock
198  * @tc.type: FUNC
199  * @tc.require: SR000H9A70
200  */
201 HWTEST_F(RiskCollectTest, TestUeventNotify001, TestSize.Level1)
202 {
203     MockKernelInterfaceAdapter mockObj;
204     UeventNotify notify(mockObj);
205     EXPECT_CALL(mockObj, Open).Times(AtLeast(1)).WillOnce(Return(-1)).WillRepeatedly(Return(0));
206     EXPECT_CALL(mockObj, Write).Times(AtLeast(1)).WillOnce(Return(0)).WillRepeatedly(Return(1));
207     notify.NotifyScan();
208     notify.NotifyScan();
209     notify.NotifyScan();
210 }
211 
212 /**
213  * @tc.name: TestUeventNotify002
214  * @tc.desc: Test AddWhiteList with mock
215  * @tc.type: FUNC
216  * @tc.require: SR000H9A70
217  */
218 HWTEST_F(RiskCollectTest, TestUeventNotify002, TestSize.Level1)
219 {
220     std::vector<int64_t> whitelist;
221     MockKernelInterfaceAdapter mockObj;
222     UeventNotify notify(mockObj);
223     EXPECT_CALL(mockObj, Open).Times(AtLeast(1)).WillOnce(Return(-1)).WillRepeatedly(Return(0));
224     EXPECT_CALL(mockObj, Write).Times(AtLeast(1)).WillOnce(Return(0)).WillRepeatedly(Return(5));
225     notify.AddWhiteList(whitelist);
226     whitelist.emplace_back(0);
227     notify.AddWhiteList(whitelist);
228     notify.AddWhiteList(whitelist);
229     notify.AddWhiteList(whitelist);
230 }
231 
232 /**
233  * @tc.name: TestKernelInterfaceAdapter001
234  * @tc.desc: Test KernelInterfaceAdapter bind interface
235  * @tc.type: FUNC
236  * @tc.require: SR000H9A70
237  */
238 HWTEST_F(RiskCollectTest, TestKernelInterfaceAdapter001, TestSize.Level1)
239 {
240     KernelInterfaceAdapter adapter;
241     struct sockaddr_nl addr = {};
242     int ret = adapter.Bind(0, reinterpret_cast<const struct sockaddr *>(&addr), sizeof(addr));
243     EXPECT_FALSE(ret == 0);
244     ret = adapter.Bind(0, nullptr, 0);
245     EXPECT_TRUE(ret == -1);
246 }
247 
248 /**
249  * @tc.name: TestKernelInterfaceAdapter002
250  * @tc.desc: Test KernelInterfaceAdapter poll interface
251  * @tc.type: FUNC
252  * @tc.require: SR000H9A70
253  */
254 HWTEST_F(RiskCollectTest, TestKernelInterfaceAdapter002, TestSize.Level1)
255 {
256     KernelInterfaceAdapter adapter;
257     struct pollfd fds = {};
258     int ret = adapter.Poll(&fds, 1, -1);
259     EXPECT_FALSE(ret == 0);
260     ret = adapter.Poll(nullptr, 0, -1);
261     EXPECT_TRUE(ret == 0);
262 }
263 
264 /**
265  * @tc.name: TestKernelInterfaceAdapter003
266  * @tc.desc: Test KernelInterfaceAdapter recv interface
267  * @tc.type: FUNC
268  * @tc.require: SR000H9A70
269  */
270 HWTEST_F(RiskCollectTest, TestKernelInterfaceAdapter003, TestSize.Level1)
271 {
272     KernelInterfaceAdapter adapter;
273     char buffer[1] = {};
274     int ret = adapter.Recv(0, buffer, sizeof(buffer), 0);
275     EXPECT_FALSE(ret == 0);
276     ret = adapter.Recv(0, nullptr, 0, 0);
277     EXPECT_TRUE(ret == 0);
278 }
279 
280 /**
281  * @tc.name: TestKernelInterfaceAdapter004
282  * @tc.desc: Test KernelInterfaceAdapter open interface
283  * @tc.type: FUNC
284  * @tc.require: SR000H9A70
285  */
286 HWTEST_F(RiskCollectTest, TestKernelInterfaceAdapter004, TestSize.Level1)
287 {
288     KernelInterfaceAdapter adapter;
289     int ret = adapter.Open("/proc/kernel_sg", 0);
290     EXPECT_TRUE(ret != 1);
291     ret = adapter.Open("test", 0);
292     EXPECT_TRUE(ret == -1);
293 }
294 
295 /**
296  * @tc.name: TestKernelInterfaceAdapter005
297  * @tc.desc: Test KernelInterfaceAdapter write interface
298  * @tc.type: FUNC
299  * @tc.require: SR000H9A70
300  */
301 HWTEST_F(RiskCollectTest, TestKernelInterfaceAdapter005, TestSize.Level1)
302 {
303     KernelInterfaceAdapter adapter;
304     char buffer[1] = {};
305     int ret = adapter.Write(0, buffer, sizeof(buffer));
306     EXPECT_FALSE(ret == 0);
307     ret = adapter.Write(0, nullptr, 0);
308     EXPECT_TRUE(ret == 0);
309 }
310 
311 HWTEST_F(RiskCollectTest, CheckRiskContent001, TestSize.Level1)
312 {
313     std::string content(MAX_CONTENT_SIZE, 'c');
314     bool isSuccess = DataFormat::CheckRiskContent(content);
315     EXPECT_FALSE(isSuccess);
316 }
317 
318 HWTEST_F(RiskCollectTest, ParseConditions001, TestSize.Level1)
319 {
320     std::string conditions;
321     RequestCondition reqCondition;
322     DataFormat::ParseConditions(conditions, reqCondition);
323     EXPECT_TRUE(reqCondition.riskEvent.empty());
324 }
325 
326 HWTEST_F(RiskCollectTest, ParseConditions002, TestSize.Level1)
327 {
328     std::string conditions = "{\"eventId\":0}";
329     RequestCondition reqCondition;
330     DataFormat::ParseConditions(conditions, reqCondition);
331     EXPECT_TRUE(reqCondition.riskEvent.empty());
332 }
333 
334 HWTEST_F(RiskCollectTest, ParseConditions003, TestSize.Level1)
335 {
336     std::string conditions = "{\"eventId\":[\"t\", \"e\", \"s\", \"t\"]}";
337     RequestCondition reqCondition;
338     DataFormat::ParseConditions(conditions, reqCondition);
339     EXPECT_TRUE(reqCondition.riskEvent.empty());
340 }
341 
342 HWTEST_F(RiskCollectTest, ParseConditions004, TestSize.Level1)
343 {
344     std::string conditions = "{\"eventId\":[1, 2, 3, 4]}";
345     RequestCondition reqCondition;
346     EXPECT_CALL(ConfigDataManager::GetInstance(), GetTableFromEventId).WillOnce(Return("risk_event"))
347         .WillRepeatedly(Return("audit_event"));
348     DataFormat::ParseConditions(conditions, reqCondition);
349     EXPECT_FALSE(reqCondition.riskEvent.empty());
350 }
351 
352 HWTEST_F(RiskCollectTest, ParseConditions005, TestSize.Level1)
353 {
354     std::string conditions = "{\"beginTime\":1}";
355     RequestCondition reqCondition;
356     DataFormat::ParseConditions(conditions, reqCondition);
357     EXPECT_TRUE(reqCondition.beginTime.empty());
358 }
359 
360 HWTEST_F(RiskCollectTest, ParseConditions006, TestSize.Level1)
361 {
362     std::string conditions = "{\"beginTime\":\"0001\"}";
363     RequestCondition reqCondition;
364     DataFormat::ParseConditions(conditions, reqCondition);
365     EXPECT_TRUE(reqCondition.beginTime == "0001");
366 }
367 
368 HWTEST_F(RiskCollectTest, ParseConditions007, TestSize.Level1)
369 {
370     std::string conditions = "{\"endTime\":1}";
371     RequestCondition reqCondition;
372     DataFormat::ParseConditions(conditions, reqCondition);
373     EXPECT_TRUE(reqCondition.endTime.empty());
374 }
375 
376 HWTEST_F(RiskCollectTest, ParseConditions008, TestSize.Level1)
377 {
378     std::string conditions = "{\"endTime\":\"0001\"}";
379     RequestCondition reqCondition;
380     DataFormat::ParseConditions(conditions, reqCondition);
381     EXPECT_TRUE(reqCondition.endTime == "0001");
382 }
383 }