1 /*
2  * Copyright (c) 2021-2022 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 #include <sys/socket.h>
18 
19 #include "proto.h"
20 #include "uds_session.h"
21 
22 namespace OHOS {
23 namespace MMI {
24 namespace {
25 using namespace testing::ext;
26 constexpr int32_t UID_ROOT { 0 };
27 } // namespace
28 
29 class UDSSessionTest : public testing::Test {
30 public:
SetUpTestCase(void)31     static void SetUpTestCase(void) {}
TearDownTestCase(void)32     static void TearDownTestCase(void) {}
33     static constexpr char PROGRAM_NAME[] = "uds_sesion_test";
34     const int32_t moduleType_ = 3;
35     static inline int32_t pid_ = 0;
36     int32_t writeFd_ = -1;
37     int32_t readFd_ = -1;
38     void SetUp() override;
39     void TearDown() override;
40 };
SetUp()41 void UDSSessionTest::SetUp()
42 {
43     UDSSessionTest::pid_ = getpid();
44     int32_t sockFds[2] = {};
45     auto ret = socketpair(AF_UNIX, SOCK_STREAM, 0, sockFds);
46     ASSERT_EQ(ret, 0);
47 
48     writeFd_ = sockFds[0];
49     readFd_ = sockFds[1];
50 }
51 
TearDown()52 void UDSSessionTest::TearDown()
53 {
54     close(writeFd_);
55     writeFd_ = -1;
56     close(readFd_);
57     readFd_ = -1;
58 }
59 
60 /**
61  * @tc.name: Construct
62  * @tc.desc: Verify uds session function EventsIsEmpty
63  * @tc.type: FUNC
64  * @tc.require:
65  */
HWTEST_F(UDSSessionTest, Construct, TestSize.Level1)66 HWTEST_F(UDSSessionTest, Construct, TestSize.Level1)
67 {
68     UDSSession udsSession(PROGRAM_NAME, moduleType_, writeFd_, UID_ROOT, pid_);
69     bool retResult = udsSession.IsEventQueueEmpty();
70     EXPECT_TRUE(retResult);
71 }
72 
73 /**
74  * @tc.name: SendMsg_type1_001
75  * @tc.desc: Verify uds session function sendMsg
76  * @tc.type: FUNC
77  * @tc.require:
78  */
HWTEST_F(UDSSessionTest, SendMsg_type1_001, TestSize.Level1)79 HWTEST_F(UDSSessionTest, SendMsg_type1_001, TestSize.Level1)
80 {
81     const char *buf = "1234";
82     size_t size = 4;
83     UDSSession sesObj(PROGRAM_NAME, moduleType_, writeFd_, UID_ROOT, pid_);
84     bool retResult = sesObj.SendMsg(buf, size);
85     EXPECT_TRUE(retResult);
86 }
87 
88 /**
89  * @tc.name: SendMsg_type1_002
90  * @tc.desc: Verify uds session function SendMsg
91  * @tc.type: FUNC
92  * @tc.require:
93  */
HWTEST_F(UDSSessionTest, SendMsg_type1_002, TestSize.Level1)94 HWTEST_F(UDSSessionTest, SendMsg_type1_002, TestSize.Level1)
95 {
96     const char *buf = nullptr;
97     size_t size = 4;
98 
99     UDSSession sesObj(PROGRAM_NAME, moduleType_, writeFd_, UID_ROOT, pid_);
100     bool retResult = sesObj.SendMsg(buf, size);
101     EXPECT_FALSE(retResult);
102 }
103 
104 /**
105  * @tc.name: SendMsg_type1_003
106  * @tc.desc: Verify uds session function SendMsg
107  * @tc.type: FUNC
108  * @tc.require:
109  */
HWTEST_F(UDSSessionTest, SendMsg_type1_003, TestSize.Level1)110 HWTEST_F(UDSSessionTest, SendMsg_type1_003, TestSize.Level1)
111 {
112     const char *buf = nullptr;
113     size_t size = 0;
114     UDSSession sesObj(PROGRAM_NAME, moduleType_, writeFd_, UID_ROOT, pid_);
115     bool retResult = sesObj.SendMsg(buf, size);
116     EXPECT_FALSE(retResult);
117 }
118 
119 /**
120  * @tc.name: SendMsg_type1_004
121  * @tc.desc: Verify uds session function SendMsg
122  * @tc.type: FUNC
123  * @tc.require:
124  */
HWTEST_F(UDSSessionTest, SendMsg_type1_004, TestSize.Level1)125 HWTEST_F(UDSSessionTest, SendMsg_type1_004, TestSize.Level1)
126 {
127     const char *buf = "this unit data";
128     size_t size = 14;
129 
130     UDSSession sesObj(PROGRAM_NAME, moduleType_, writeFd_, UID_ROOT, pid_);
131     bool retResult = sesObj.SendMsg(buf, size);
132     EXPECT_TRUE(retResult);
133 }
134 
135 /**
136  * @tc.name: SendMsg_type1_005
137  * @tc.desc: Verify uds session function SendMsg
138  * @tc.type: FUNC
139  * @tc.require:
140  */
HWTEST_F(UDSSessionTest, SendMsg_type1_005, TestSize.Level1)141 HWTEST_F(UDSSessionTest, SendMsg_type1_005, TestSize.Level1)
142 {
143     const char *buf = "this unit data";
144     size_t size = -1001;
145 
146     UDSSession sesObj(PROGRAM_NAME, moduleType_, writeFd_, UID_ROOT, pid_);
147     bool retResult = sesObj.SendMsg(buf, size);
148     EXPECT_FALSE(retResult);
149 }
150 
151 /**
152  * @tc.name: SendMsg_type2_001
153  * @tc.desc: Verify uds session function SendMsg
154  * @tc.type: FUNC
155  * @tc.require:
156  */
HWTEST_F(UDSSessionTest, SendMsg_type2_001, TestSize.Level1)157 HWTEST_F(UDSSessionTest, SendMsg_type2_001, TestSize.Level1)
158 {
159     int32_t fd = -1;
160     NetPacket pkt(MmiMessageId::INVALID);
161 
162     UDSSession sesObj(PROGRAM_NAME, moduleType_, fd, UID_ROOT, pid_);
163     bool retResult = sesObj.SendMsg(pkt);
164     EXPECT_FALSE(retResult);
165 }
166 
167 /**
168  * @tc.name: SendMsg_type2_002
169  * @tc.desc: Verify uds session function SendMsg
170  * @tc.type: FUNC
171  * @tc.require:
172  */
HWTEST_F(UDSSessionTest, SendMsg_type2_002, TestSize.Level1)173 HWTEST_F(UDSSessionTest, SendMsg_type2_002, TestSize.Level1)
174 {
175     NetPacket pkt(MmiMessageId::INVALID);
176 
177     UDSSession sesObj(PROGRAM_NAME, moduleType_, writeFd_, UID_ROOT, pid_);
178     bool retResult = sesObj.SendMsg(pkt);
179     EXPECT_TRUE(retResult);
180 }
181 
182 /**
183  * @tc.name: SendMsg_type2_003
184  * @tc.desc: Verify uds session function SendMsg
185  * @tc.type: FUNC
186  * @tc.require:
187  */
HWTEST_F(UDSSessionTest, SendMsg_type2_003, TestSize.Level1)188 HWTEST_F(UDSSessionTest, SendMsg_type2_003, TestSize.Level1)
189 {
190     int32_t fd = -65535;
191     NetPacket pkt(MmiMessageId::INVALID);
192 
193     UDSSession sesObj(PROGRAM_NAME, moduleType_, fd, UID_ROOT, pid_);
194     bool retResult = sesObj.SendMsg(pkt);
195     EXPECT_FALSE(retResult);
196 }
197 
198 /**
199  * @tc.name: GetTimerIds
200  * @tc.desc: Verify uds session function GetTimerIds
201  * @tc.type: FUNC
202  * @tc.require:
203  */
HWTEST_F(UDSSessionTest, GetTimerIds, TestSize.Level1)204 HWTEST_F(UDSSessionTest, GetTimerIds, TestSize.Level1)
205 {
206     UDSSession sesObj(PROGRAM_NAME, moduleType_, writeFd_, UID_ROOT, pid_);
207 
208     auto currentTime = GetSysClockTime();
209     int32_t type = ANR_DISPATCH;
210     int32_t timerId = 1;
211     sesObj.SaveANREvent(type, 1, currentTime, timerId);
212 
213     std::vector<int32_t> ids = sesObj.GetTimerIds(type);
214     if (ids.empty()) {
215         ASSERT_FALSE(true);
216     }
217     EXPECT_EQ(ids[0], timerId);
218 }
219 
220 /**
221  * @tc.name: DelEvents
222  * @tc.desc: Verify uds session function DelEvents
223  * @tc.type: FUNC
224  * @tc.require:
225  */
HWTEST_F(UDSSessionTest, DelEvents, TestSize.Level1)226 HWTEST_F(UDSSessionTest, DelEvents, TestSize.Level1)
227 {
228     UDSSession sesObj(PROGRAM_NAME, moduleType_, writeFd_, UID_ROOT, pid_);
229 
230     int32_t type = ANR_DISPATCH;
231     int32_t timerId = 0;
232     const int32_t idLen = 5;
233     int32_t i = 0;
234     for (; i < idLen; ++i) {
235         auto currentTime = GetSysClockTime();
236         sesObj.SaveANREvent(type, i, currentTime, timerId + i);
237     }
238 
239     std::list<int32_t> timerIds = sesObj.DelEvents(type, i - 2);
240     EXPECT_EQ(timerIds.size(), idLen - 1);
241 }
242 
243 /**
244  * @tc.name: UDSSessionTest_DelEvents_01
245  * @tc.desc: Verify uds session function DelEvents
246  * @tc.type: FUNC
247  * @tc.require:
248  */
HWTEST_F(UDSSessionTest, UDSSessionTest_DelEvents_01, TestSize.Level1)249 HWTEST_F(UDSSessionTest, UDSSessionTest_DelEvents_01, TestSize.Level1)
250 {
251     UDSSession sesObj(PROGRAM_NAME, moduleType_, writeFd_, UID_ROOT, pid_);
252     int32_t type = 3;
253     int32_t id = 2;
254     UDSSession::EventTime event;
255     event.id = 4;
256     event.eventTime = 1000;
257     event.timerId = 5;
258 
259     int32_t key = 1;
260     sesObj.events_[key].push_back(event);
261     ASSERT_NO_FATAL_FAILURE(sesObj.DelEvents(type, id));
262 }
263 
264 /**
265  * @tc.name: UDSSessionTest_DelEvents_02
266  * @tc.desc: Verify uds session function DelEvents
267  * @tc.type: FUNC
268  * @tc.require:
269  */
HWTEST_F(UDSSessionTest, UDSSessionTest_DelEvents_02, TestSize.Level1)270 HWTEST_F(UDSSessionTest, UDSSessionTest_DelEvents_02, TestSize.Level1)
271 {
272     UDSSession sesObj(PROGRAM_NAME, moduleType_, writeFd_, UID_ROOT, pid_);
273     int32_t type = 1;
274     int32_t id = 2;
275     UDSSession::EventTime event;
276     event.id = 4;
277     event.eventTime = 1000;
278     event.timerId = 5;
279 
280     int32_t key = 1;
281     sesObj.events_[key].push_back(event);
282     ASSERT_NO_FATAL_FAILURE(sesObj.DelEvents(type, id));
283 
284     id = 5;
285     ASSERT_NO_FATAL_FAILURE(sesObj.DelEvents(type, id));
286 }
287 
288 /**
289  * @tc.name: UDSSessionTest_GetEarliestEventTime_01
290  * @tc.desc: Verify uds session function DelEvents
291  * @tc.type: FUNC
292  * @tc.require:
293  */
HWTEST_F(UDSSessionTest, UDSSessionTest_GetEarliestEventTime_01, TestSize.Level1)294 HWTEST_F(UDSSessionTest, UDSSessionTest_GetEarliestEventTime_01, TestSize.Level1)
295 {
296     UDSSession sesObj(PROGRAM_NAME, moduleType_, writeFd_, UID_ROOT, pid_);
297     int32_t type = 1;
298     UDSSession::EventTime event;
299     event.id = 4;
300     event.eventTime = 1000;
301     event.timerId = 5;
302 
303     int32_t key = 1;
304     sesObj.events_[key].push_back(event);
305     int64_t ret = sesObj.GetEarliestEventTime(type);
306     EXPECT_EQ(ret, 1000);
307 
308     type = 2;
309     int64_t ret2 = sesObj.GetEarliestEventTime(type);
310     EXPECT_EQ(ret2, 0);
311 }
312 
313 /**
314  * @tc.name: UDSSessionTest_SaveANREvent_01
315  * @tc.desc: Verify uds session function SaveANREvent
316  * @tc.type: FUNC
317  * @tc.require:
318  */
HWTEST_F(UDSSessionTest, UDSSessionTest_SaveANREvent_01, TestSize.Level1)319 HWTEST_F(UDSSessionTest, UDSSessionTest_SaveANREvent_01, TestSize.Level1)
320 {
321     UDSSession sesObj(PROGRAM_NAME, moduleType_, writeFd_, UID_ROOT, pid_);
322     int32_t type = 1;
323     int32_t id = 2;
324     int64_t time = 5;
325     int32_t timerId = 6;
326     UDSSession::EventTime event;
327     event.id = 4;
328     event.eventTime = 1000;
329     event.timerId = 5;
330 
331     int32_t key = 1;
332     sesObj.events_[key].push_back(event);
333     ASSERT_NO_FATAL_FAILURE(sesObj.SaveANREvent(type, id, time, timerId));
334 
335     type = 3;
336     ASSERT_NO_FATAL_FAILURE(sesObj.SaveANREvent(type, id, time, timerId));
337 }
338 
339 /**
340  * @tc.name: UDSSessionTest_GetTimerIds_01
341  * @tc.desc: Verify uds session function GetTimerIds
342  * @tc.type: FUNC
343  * @tc.require:
344  */
HWTEST_F(UDSSessionTest, UDSSessionTest_GetTimerIds_01, TestSize.Level1)345 HWTEST_F(UDSSessionTest, UDSSessionTest_GetTimerIds_01, TestSize.Level1)
346 {
347     UDSSession sesObj(PROGRAM_NAME, moduleType_, writeFd_, UID_ROOT, pid_);
348     int32_t type = 1;
349     UDSSession::EventTime event;
350     event.id = 4;
351     event.eventTime = 1000;
352     event.timerId = 5;
353 
354     int32_t key = 1;
355     sesObj.events_[key].push_back(event);
356     ASSERT_NO_FATAL_FAILURE(sesObj.GetTimerIds(type));
357 
358     type = 2;
359     ASSERT_NO_FATAL_FAILURE(sesObj.GetTimerIds(type));
360 }
361 
362 /**
363  * @tc.name: UDSSessionTest_sendMsg_01
364  * @tc.desc: Verify uds session function sendMsg
365  * @tc.type: FUNC
366  * @tc.require:
367  */
HWTEST_F(UDSSessionTest, UDSSessionTest_sendMsg_01, TestSize.Level1)368 HWTEST_F(UDSSessionTest, UDSSessionTest_sendMsg_01, TestSize.Level1)
369 {
370     const char *buf = "123";
371     size_t size = 0;
372     UDSSession sesObj(PROGRAM_NAME, moduleType_, writeFd_, UID_ROOT, pid_);
373     bool retResult = sesObj.SendMsg(buf, size);
374     EXPECT_FALSE(retResult);
375 }
376 
377 /**
378  * @tc.name: UDSSessionTest_Close_01
379  * @tc.desc: Verify uds session function Close
380  * @tc.type: FUNC
381  * @tc.require:
382  */
HWTEST_F(UDSSessionTest, UDSSessionTest_Close_01, TestSize.Level1)383 HWTEST_F(UDSSessionTest, UDSSessionTest_Close_01, TestSize.Level1)
384 {
385     UDSSession sesObj(PROGRAM_NAME, moduleType_, writeFd_, UID_ROOT, pid_);
386     sesObj.fd_ = -1;
387     ASSERT_NO_FATAL_FAILURE(sesObj.Close());
388 }
389 
390 /**
391  * @tc.name: UDSSessionTest_ReportSocketBufferFull_01
392  * @tc.desc: Verify uds session function ReportSocketBufferFull
393  * @tc.type: FUNC
394  * @tc.require:
395  */
HWTEST_F(UDSSessionTest, UDSSessionTest_ReportSocketBufferFull_01, TestSize.Level1)396 HWTEST_F(UDSSessionTest, UDSSessionTest_ReportSocketBufferFull_01, TestSize.Level1)
397 {
398     UDSSession sesObj(PROGRAM_NAME, moduleType_, writeFd_, UID_ROOT, pid_);
399     ASSERT_NO_FATAL_FAILURE(sesObj.ReportSocketBufferFull());
400 }
401 
402 /**
403  * @tc.name: GetEarliestEventTime
404  * @tc.desc: Verify uds session function GetEarliestEventTime
405  * @tc.type: FUNC
406  * @tc.require:
407  */
HWTEST_F(UDSSessionTest, GetEarliestEventTime, TestSize.Level1)408 HWTEST_F(UDSSessionTest, GetEarliestEventTime, TestSize.Level1)
409 {
410     UDSSession sesObj(PROGRAM_NAME, moduleType_, writeFd_, UID_ROOT, pid_);
411 
412     int32_t type = ANR_DISPATCH;
413     int32_t timerId = 0;
414     const int32_t idLen = 5;
415     int64_t currentTime = GetSysClockTime();
416     int64_t earliestEventTime = currentTime;
417     sesObj.SaveANREvent(type, 0, currentTime, timerId);
418     for (int32_t i = 1; i < idLen; ++i) {
419         currentTime = GetSysClockTime();
420         sesObj.SaveANREvent(type, i, currentTime, timerId + i);
421     }
422 
423     int64_t eventTime = sesObj.GetEarliestEventTime(type);
424     EXPECT_EQ(eventTime, earliestEventTime);
425 }
426 } // namespace MMI
427 } // namespace OHOS
428