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 "socket_session_test.h"
17
18 #include "ipc_skeleton.h"
19 #include "message_parcel.h"
20
21 #include "devicestatus_define.h"
22 #include "i_context.h"
23 #include "socket_client.h"
24 #include "socket_session_manager.h"
25 #include "socket_server.h"
26
27
28 namespace OHOS {
29 namespace Msdp {
30 namespace DeviceStatus {
31 using namespace testing::ext;
32 namespace {
33 constexpr size_t BUF_CMD_SIZE { 512 };
34 std::unique_ptr<SocketClient> g_client {nullptr};
35 std::shared_ptr<SocketConnection> g_socket { nullptr };
36 std::shared_ptr<SocketServer> g_socketServer { nullptr };
37 std::shared_ptr<SocketSession> g_session { nullptr };
38 std::shared_ptr<SocketSession> g_sessionOne { nullptr };
39 std::shared_ptr<SocketSessionManager> g_socketSessionManager { nullptr };
40 IContext *g_context { nullptr };
41 Intention g_intention { Intention::UNKNOWN_INTENTION };
42 constexpr int32_t TIME_WAIT_FOR_OP_MS { 20 };
43 constexpr int32_t NUM_ONE { 1 };
44 } // namespace
45
SetUpTestCase()46 void SocketSessionTest::SetUpTestCase() {}
47
SetUp()48 void SocketSessionTest::SetUp()
49 {
50 g_client = std::make_unique<SocketClient>();
51 g_socketServer = std::make_unique<SocketServer>(g_context);
52 g_socketSessionManager = std::make_shared<SocketSessionManager>();
53 int32_t moduleType = 1;
54 int32_t tokenType = 1;
55 int32_t uid = IPCSkeleton::GetCallingUid();
56 int32_t pid = IPCSkeleton::GetCallingPid();
57 int32_t sockFds[2] { -1, -1 };
58 g_session = std::make_shared<SocketSession>("test", moduleType, tokenType, sockFds[0], uid, pid);
59 g_sessionOne = std::make_shared<SocketSession>("test1", moduleType, tokenType, sockFds[1], uid, pid);
60 }
TearDown()61 void SocketSessionTest::TearDown()
62 {
63 g_client = nullptr;
64 g_socket = nullptr;
65 g_socketSessionManager = nullptr;
66 g_session = nullptr;
67 g_socketServer = nullptr;
68 g_sessionOne = nullptr;
69 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
70 }
71
72 /**
73 * @tc.name: SocketSessionTest1
74 * @tc.desc: Drag Drawing
75 * @tc.type: FUNC
76 * @tc.require:
77 */
78 HWTEST_F(SocketSessionTest, SocketSessionTest1, TestSize.Level0)
79 {
80 CALL_TEST_DEBUG;
81 int32_t ret = g_client->Connect();
82 EXPECT_TRUE(ret);
83 ret = g_client->Connect();
84 EXPECT_TRUE(ret);
85 g_client->Stop();
86 g_client->OnDisconnected();
87 }
88
89 /**
90 * @tc.name: SocketSessionTest2
91 * @tc.desc: Drag Drawing
92 * @tc.type: FUNC
93 * @tc.require:
94 */
95 HWTEST_F(SocketSessionTest, SocketSessionTest2, TestSize.Level0)
96 {
97 CALL_TEST_DEBUG;
98 int32_t ret = g_client->Socket();
99 MessageId msgId { MessageId::INVALID };
100 NetPacket pkt(msgId);
101 g_client->OnPacket(pkt);
102 EXPECT_GT(ret, -1);
103 }
104
105 /**
106 * @tc.name: SocketSessionTest3
107 * @tc.desc: Drag Drawing
108 * @tc.type: FUNC
109 * @tc.require:
110 */
111 HWTEST_F(SocketSessionTest, SocketSessionTest3, TestSize.Level0)
112 {
113 CALL_TEST_DEBUG;
114 g_client->Reconnect();
115 int32_t ret = g_client->Connect();
116 EXPECT_TRUE(ret);
117 g_client->Reconnect();
118 g_client->OnDisconnected();
119 }
120
121 /**
122 * @tc.name: SocketSessionTest8
123 * @tc.desc: Drag Drawing
124 * @tc.type: FUNC
125 * @tc.require:
126 */
127 HWTEST_F(SocketSessionTest, SocketSessionTest8, TestSize.Level0)
128 {
129 CALL_TEST_DEBUG;
130 CallingContext context {
131 .intention = g_intention,
132 .tokenId = IPCSkeleton::GetCallingTokenID(),
133 .uid = IPCSkeleton::GetCallingUid(),
134 .pid = IPCSkeleton::GetCallingPid(),
135 };
136 MessageParcel datas;
137 MessageParcel reply;
138 auto programName = GetProgramName();
139 int32_t socketFd { -1 };
140 int32_t tokenType { -1 };
141 int32_t ret = g_socketServer->Socket(context, programName, CONNECT_MODULE_TYPE_FI_CLIENT, socketFd, tokenType);
142 EXPECT_EQ(ret, RET_ERR);
143 }
144
145 /**
146 * @tc.name: SocketSessionTest18
147 * @tc.desc: Drag Drawing
148 * @tc.type: FUNC
149 * @tc.require:
150 */
151 HWTEST_F(SocketSessionTest, SocketSessionTest18, TestSize.Level0)
152 {
153 CALL_TEST_DEBUG;
154 g_socketSessionManager->GetAppMgr();
155 int32_t ret = g_socketSessionManager->Enable();
156 EXPECT_EQ(ret, RET_OK);
157 }
158
159 /**
160 * @tc.name: SocketSessionTest19
161 * @tc.desc: Drag Drawing
162 * @tc.type: FUNC
163 * @tc.require:
164 */
165 HWTEST_F(SocketSessionTest, SocketSessionTest19, TestSize.Level0)
166 {
167 CALL_TEST_DEBUG;
168 int32_t clientFd { -1 };
169 int32_t ret = g_socketSessionManager->AllocSocketFd("", NUM_ONE, NUM_ONE, NUM_ONE, NUM_ONE, clientFd);
170 EXPECT_EQ(ret, RET_ERR);
171 }
172
173 /**
174 * @tc.name: SocketSessionTest20
175 * @tc.desc: Drag Drawing
176 * @tc.type: FUNC
177 * @tc.require:
178 */
179 HWTEST_F(SocketSessionTest, SocketSessionTest20, TestSize.Level0)
180 {
181 CALL_TEST_DEBUG;
182 int32_t pid { 1 };
183 g_socketSessionManager->FindSessionByPid(pid);
184 g_socketSessionManager->DispatchOne();
185 int32_t fd { 1 };
186 g_socketSessionManager->ReleaseSession(fd);
187 g_socketSessionManager->FindSession(fd);
188 g_socketSessionManager->DumpSession("");
189 g_socketSessionManager->RemoveSessionDeletedCallback(pid);
190 int32_t sockFd { -1 };
191 int32_t bufSize { -1 };
192 int32_t ret = g_socketSessionManager->SetBufferSize(sockFd, bufSize);
193 EXPECT_EQ(ret, RET_OK);
194 }
195
196 /**
197 * @tc.name: SocketSessionTest21
198 * @tc.desc: Drag Drawing
199 * @tc.type: FUNC
200 * @tc.require:
201 */
202 HWTEST_F(SocketSessionTest, SocketSessionTest21, TestSize.Level0)
203 {
204 CALL_TEST_DEBUG;
205 int32_t fd { 1 };
206 bool ret = g_socketSessionManager->AddSession(g_session);
207 g_socketSessionManager->NotifySessionDeleted(g_session);
208 EXPECT_FALSE(ret);
209 g_socketSessionManager->ReleaseSession(fd);
210 }
211
212 /**
213 * @tc.name: SocketSessionTest22
214 * @tc.desc: Drag Drawing
215 * @tc.type: FUNC
216 * @tc.require:
217 */
218 HWTEST_F(SocketSessionTest, SocketSessionTest22, TestSize.Level0)
219 {
220 CALL_TEST_DEBUG;
221 MessageId msgId { MessageId::INVALID };
222 NetPacket pkt(msgId);
223 bool ret = g_session->SendMsg(pkt);
224 EXPECT_FALSE(ret);
225 }
226
227 /**
228 * @tc.name: SocketSessionTest23
229 * @tc.desc: Drag Drawing
230 * @tc.type: FUNC
231 * @tc.require:
232 */
233 HWTEST_F(SocketSessionTest, SocketSessionTest23, TestSize.Level0)
234 {
235 CALL_TEST_DEBUG;
236 char buf[BUF_CMD_SIZE] = { 0 };
237 size_t size = 1;
238 ASSERT_NO_FATAL_FAILURE(g_session->SendMsg(buf, size));
239 }
240
241 /**
242 * @tc.name: SocketSessionTest24
243 * @tc.desc: Drag Drawing
244 * @tc.type: FUNC
245 * @tc.require:
246 */
247 HWTEST_F(SocketSessionTest, SocketSessionTest24, TestSize.Level0)
248 {
249 CALL_TEST_DEBUG;
250 char buf[BUF_CMD_SIZE] = { 0 };
251 size_t size = 1;
252 struct epoll_event ev {};
253 ev.events = 0;
254 ev.events |= EPOLLIN;
255 g_session->Dispatch(ev);
256 g_socketSessionManager->Dispatch(ev);
257
258 ev.events = 0;
259 ev.events |= EPOLLHUP;
260 g_session->Dispatch(ev);
261 g_socketSessionManager->Dispatch(ev);
262 bool ret = g_session->SendMsg(buf, size);
263 g_session->ToString();
264 EXPECT_FALSE(ret);
265 }
266
267 /**
268 * @tc.name: SocketSessionTest25
269 * @tc.desc: Drag Drawing
270 * @tc.type: FUNC
271 * @tc.require:
272 */
273 HWTEST_F(SocketSessionTest, SocketSessionTest25, TestSize.Level0)
274 {
275 CALL_TEST_DEBUG;
276 g_socketSessionManager->AddSessionDeletedCallback(1, nullptr);
__anonb5fcd45d0202(SocketSessionPtr ptr)277 g_socketSessionManager->AddSessionDeletedCallback(1, [](SocketSessionPtr ptr){});
278 bool ret = g_socketSessionManager->AddSession(nullptr);
279 EXPECT_FALSE(ret);
280 }
281
282 /**
283 * @tc.name: SocketSessionTest27
284 * @tc.desc: Drag Drawing
285 * @tc.type: FUNC
286 * @tc.require:
287 */
288 HWTEST_F(SocketSessionTest, SocketSessionTest27, TestSize.Level0)
289 {
290 CALL_TEST_DEBUG;
291 char buf[BUF_CMD_SIZE] = { 0 };
292 size_t size = 0;
293 bool ret = g_session->SendMsg(buf, size);
294 EXPECT_FALSE(ret);
295 }
296
297 /**
298 * @tc.name: SocketSessionTest28
299 * @tc.desc: Drag Drawing
300 * @tc.type: FUNC
301 * @tc.require:
302 */
303 HWTEST_F(SocketSessionTest, SocketSessionTest28, TestSize.Level0)
304 {
305 CALL_TEST_DEBUG;
306 int32_t fd = g_session->GetFd();
307 bool ret = g_socketSessionManager->AddSession(g_session);
308 EXPECT_FALSE(ret);
309 g_socketSessionManager->ReleaseSession(fd);
310 }
311
312 /**
313 * @tc.name: SocketSessionTest29
314 * @tc.desc: Drag Drawing
315 * @tc.type: FUNC
316 * @tc.require:
317 */
318 HWTEST_F(SocketSessionTest, SocketSessionTest29, TestSize.Level0)
319 {
320 CALL_TEST_DEBUG;
__anonb5fcd45d0302(SocketSessionPtr ptr)321 g_socketSessionManager->callbacks_.emplace(g_session->GetPid(), [](SocketSessionPtr ptr){});
322 g_socketSessionManager->NotifySessionDeleted(g_session);
323 int32_t ARG_101 = 101;
324 for (size_t i = 0; i < ARG_101; i++) {
325 g_socketSessionManager->sessions_.emplace(i, nullptr);
326 }
327 bool ret = g_socketSessionManager->AddSession(g_session);
328 EXPECT_FALSE(ret);
329 g_socketSessionManager->sessions_.clear();
330 g_socketSessionManager->callbacks_.clear();
331 }
332
333 /**
334 * @tc.name: SocketSessionTest30
335 * @tc.desc: Drag Drawing
336 * @tc.type: FUNC
337 * @tc.require:
338 */
339 HWTEST_F(SocketSessionTest, SocketSessionTest30, TestSize.Level0)
340 {
341 CALL_TEST_DEBUG;
342 char buf[BUF_CMD_SIZE] = { 0 };
343 size_t size = 1;
344 bool ret = g_sessionOne->SendMsg(buf, size);
345 EXPECT_FALSE(ret);
346 }
347
348 /**
349 * @tc.name: SocketSessionTest31
350 * @tc.desc: Drag Drawing
351 * @tc.type: FUNC
352 * @tc.require:
353 */
354 HWTEST_F(SocketSessionTest, SocketSessionTest31, TestSize.Level0)
355 {
356 CALL_TEST_DEBUG;
357 int32_t pid = IPCSkeleton::GetCallingPid();
358 ASSERT_NO_FATAL_FAILURE(g_socketSessionManager->ReleaseSessionByPid(pid));
359 }
360
361 /**
362 * @tc.name: SocketSessionTest32
363 * @tc.desc: Drag Drawing
364 * @tc.type: FUNC
365 * @tc.require:
366 */
367 HWTEST_F(SocketSessionTest, SocketSessionTest32, TestSize.Level0)
368 {
369 CALL_TEST_DEBUG;
370 IEpollEventSource *epollEventSource = g_session.get();
371 ASSERT_NO_FATAL_FAILURE(g_socketSessionManager->OnEpollIn(*epollEventSource));
372 ASSERT_NO_FATAL_FAILURE(g_socketSessionManager->DeleteCollaborationServiceByName());
373 }
374
375 /**
376 * @tc.name: SocketSessionTest33
377 * @tc.desc: Drag Drawing
378 * @tc.type: FUNC
379 * @tc.require:
380 */
381 HWTEST_F(SocketSessionTest, SocketSessionTest33, TestSize.Level0)
382 {
383 CALL_TEST_DEBUG;
__anonb5fcd45d0402() 384 auto onDisconnected = []() {
385 return;
386 };
387 int32_t fd = g_session->GetFd();;
__anonb5fcd45d0502(const NetPacket &pkt) 388 auto recv = [](const NetPacket &pkt) {
389 return;
390 };
391 SocketConnection socketConnection(fd, recv, onDisconnected);
392 ASSERT_NO_FATAL_FAILURE(socketConnection.OnReadable(fd));
393 ASSERT_NO_FATAL_FAILURE(socketConnection.OnShutdown(fd));
394 }
395 } // namespace DeviceStatus
396 } // namespace Msdp
397 } // namespace OHOS
398