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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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: GetEarliestEventTime
245 * @tc.desc: Verify uds session function GetEarliestEventTime
246 * @tc.type: FUNC
247 * @tc.require:
248 */
249 HWTEST_F(UDSSessionTest, GetEarliestEventTime, TestSize.Level1)
250 {
251 UDSSession sesObj(PROGRAM_NAME, moduleType_, writeFd_, UID_ROOT, pid_);
252
253 int32_t type = ANR_DISPATCH;
254 int32_t timerId = 0;
255 const int32_t idLen = 5;
256 int64_t currentTime = GetSysClockTime();
257 int64_t earliestEventTime = currentTime;
258 sesObj.SaveANREvent(type, 0, currentTime, timerId);
259 for (int32_t i = 1; i < idLen; ++i) {
260 currentTime = GetSysClockTime();
261 sesObj.SaveANREvent(type, i, currentTime, timerId + i);
262 }
263
264 int64_t eventTime = sesObj.GetEarliestEventTime(type);
265 EXPECT_EQ(eventTime, earliestEventTime);
266 }
267
268 } // namespace MMI
269 } // namespace OHOS
270