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 <gtest/gtest.h>
17 #include <securec.h>
18
19 #include "client_trans_statistics.c"
20 #include "softbus_adapter_mem.h"
21 #include "trans_network_statistics.h"
22
23 using namespace testing::ext;
24
25 namespace OHOS {
26
27 ConnectionAddr g_addrInfo;
28
29 #define INVALID_VALUE (-1)
30 #define SESSIONKEY_LEN 46
31 #define SESSION_KEY_LEN 46
32 static const char *g_sessionName = "ohos.distributedschedule.dms.test";
33 char g_peerSessionName[SESSIONKEY_LEN] = "ohos.distributedschedule.dms.test";
34 char g_peerSessionKey[SESSION_KEY_LEN] = "clientkey";
35 static int32_t g_fd = 0;
36 std::string g_testData = "TransSessionTest_GetSessionKeyTestData";
37
38 #define TEST_FILE_NAME "test.filename.01"
39 #define TEST_PKG_NAME_LEN (64)
40 #define TEST_SESSION_NAME_LEN (64)
41 #define TEST_NETWORK_ID_LEN (64)
42 #define TEST_GROUP_ID_LEN (64)
43 #define TRANS_TEST_ID 1
44
45 class ClientTransStatisticsTest : public testing::Test {
46 public:
ClientTransStatisticsTest()47 ClientTransStatisticsTest()
48 {}
~ClientTransStatisticsTest()49 ~ClientTransStatisticsTest()
50 {}
51 static void SetUpTestCase(void);
52 static void TearDownTestCase(void);
SetUp()53 void SetUp() override
54 {}
TearDown()55 void TearDown() override
56 {}
57 };
58
SetUpTestCase(void)59 void ClientTransStatisticsTest::SetUpTestCase(void)
60 {
61 }
62
TearDownTestCase(void)63 void ClientTransStatisticsTest::TearDownTestCase(void)
64 {
65 }
66
TestGetErrorChannelInfo(void)67 ChannelInfo *TestGetErrorChannelInfo(void)
68 {
69 ChannelInfo *info = (ChannelInfo *)SoftBusMalloc(sizeof(ChannelInfo));
70 (void)memset_s(info, sizeof(ChannelInfo), 0, sizeof(ChannelInfo));
71 info->peerSessionName = g_peerSessionName;
72 info->channelId = TRANS_TEST_ID;
73 info->channelType = CHANNEL_TYPE_TCP_DIRECT;
74 info->sessionKey = g_peerSessionKey;
75 info->fd = g_fd;
76 return info;
77 }
78
TestGetServerChannelInfo(void)79 ChannelInfo *TestGetServerChannelInfo(void)
80 {
81 ChannelInfo *info = (ChannelInfo *)SoftBusMalloc(sizeof(ChannelInfo));
82 (void)memset_s(info, sizeof(ChannelInfo), 0, sizeof(ChannelInfo));
83 info->peerSessionName = g_peerSessionName;
84 info->channelId = TRANS_TEST_ID;
85 info->channelType = CHANNEL_TYPE_TCP_DIRECT;
86 info->sessionKey = g_peerSessionKey;
87 info->fd = g_fd;
88 info->connectType = CONNECT_BR;
89 info->isServer = true;
90 return info;
91 }
92
TestGetRightChannelInfo(void)93 ChannelInfo *TestGetRightChannelInfo(void)
94 {
95 ChannelInfo *info = (ChannelInfo *)SoftBusMalloc(sizeof(ChannelInfo));
96 (void)memset_s(info, sizeof(ChannelInfo), 0, sizeof(ChannelInfo));
97 info->peerSessionName = g_peerSessionName;
98 info->channelId = TRANS_TEST_ID;
99 info->channelType = CHANNEL_TYPE_TCP_DIRECT;
100 info->sessionKey = g_peerSessionKey;
101 info->fd = g_fd;
102 info->connectType = CONNECT_BR;
103 info->isServer = false;
104 return info;
105 }
106
107 /**
108 * @tc.name: AddSocketResourceTest001
109 * @tc.desc: AddSocketResource, use the wrong or normal parameter.
110 * @tc.type: FUNC
111 * @tc.require: I5HZ6N
112 */
113 HWTEST_F(ClientTransStatisticsTest, AddSocketResourceTest001, TestSize.Level1)
114 {
115 EXPECT_NO_THROW(AddSocketResource(g_sessionName, nullptr));
116
117 ChannelInfo *errChannel = TestGetErrorChannelInfo();
118 EXPECT_NO_THROW(AddSocketResource(nullptr, errChannel));
119
120 EXPECT_NO_THROW(AddSocketResource(g_sessionName, errChannel));
121 SoftBusFree(errChannel);
122
123 ChannelInfo *serverChannel = TestGetServerChannelInfo();
124 EXPECT_NO_THROW(AddSocketResource(g_sessionName, serverChannel));
125 SoftBusFree(serverChannel);
126
127 ChannelInfo *rightChannel = TestGetRightChannelInfo();
128 EXPECT_NO_THROW(AddSocketResource(g_sessionName, rightChannel));
129 SoftBusFree(rightChannel);
130 }
131
132 /**
133 * @tc.name: UpdateChannelStatisticsTest001
134 * @tc.desc: UpdateChannelStatistics, use the wrong or normal parameter.
135 * @tc.type: FUNC
136 * @tc.require: I5HZ6N
137 */
138 HWTEST_F(ClientTransStatisticsTest, UpdateChannelStatisticsTest001, TestSize.Level1)
139 {
140 int32_t ret = ClientTransStatisticsInit();
141 EXPECT_EQ(ret, SOFTBUS_OK);
142
143 int32_t socketId = TRANS_TEST_ID;
144 int64_t len = TRANS_TEST_ID;
145 UpdateChannelStatistics(socketId, len);
146
147 int32_t channelId = TRANS_TEST_ID;
148 int32_t channelType = CHANNEL_TYPE_UDP;
149 DeleteSocketResourceByChannelId(channelId, channelType);
150
151 ClientTransStatisticsDeinit();
152 }
153
154 /**
155 * @tc.name: CreateSocketResourceTest001
156 * @tc.desc: CreateSocketResource, use the wrong or normal parameter.
157 * @tc.type: FUNC
158 * @tc.require: I5HZ6N
159 */
160 HWTEST_F(ClientTransStatisticsTest, CreateSocketResourceTest001, TestSize.Level1)
161 {
162 SocketResource *item = nullptr;
163 const char *sessionName = "sessionName";
164 ChannelInfo *channel = TestGetRightChannelInfo();
165 CreateSocketResource(item, sessionName, channel);
166 EXPECT_EQ(item, nullptr);
167 item = reinterpret_cast<SocketResource *>(SoftBusCalloc(sizeof(SocketResource)));
168 uint64_t laneId = TRANS_TEST_ID;
169 int32_t channelId = TRANS_TEST_ID;
170 int32_t channelType = CHANNEL_TYPE_UDP;
171 channel->laneId = laneId;
172 channel->channelId = channelId;
173 channel->channelType = channelType;
174 CreateSocketResource(item, sessionName, channel);
175 EXPECT_NE(item, nullptr);
176 SoftBusFree(item);
177 }
178
179 /**
180 * @tc.name: AddSocketResourceTest002
181 * @tc.desc: AddSocketResource, use the wrong or normal parameter.
182 * @tc.type: FUNC
183 * @tc.require: I5HZ6N
184 */
185 HWTEST_F(ClientTransStatisticsTest, AddSocketResourceTest002, TestSize.Level1)
186 {
187 int32_t ret = ClientTransStatisticsInit();
188 EXPECT_EQ(ret, SOFTBUS_OK);
189 SocketResource *newItem = reinterpret_cast<SocketResource *>(SoftBusCalloc(sizeof(SocketResource)));
190 ASSERT_NE(newItem, nullptr);
191 newItem->socketId = TRANS_TEST_ID;
192 ListInit(&newItem->node);
193 ListAdd(&g_channelStatisticsList->list, &newItem->node);
194
195 g_channelStatisticsList->cnt = static_cast<int32_t>(MAX_SOCKET_RESOURCE_NUM);
196 ChannelInfo *rightChannel = TestGetRightChannelInfo();
197 AddSocketResource(g_sessionName, rightChannel);
198
199 g_channelStatisticsList->cnt = static_cast<int32_t>(MAX_SOCKET_RESOURCE_NUM - TRANS_TEST_ID);
200 rightChannel->channelId = INVALID_VALUE;
201 AddSocketResource(g_sessionName, rightChannel);
202 EXPECT_NE(g_channelStatisticsList, nullptr);
203 SoftBusFree(rightChannel);
204 ClientTransStatisticsDeinit();
205 }
206
207 /**
208 * @tc.name: UpdateChannelStatisticsTest002
209 * @tc.desc: UpdateChannelStatistics, use the wrong or normal parameter.
210 * @tc.type: FUNC
211 * @tc.require: I5HZ6N
212 */
213 HWTEST_F(ClientTransStatisticsTest, UpdateChannelStatisticsTest002, TestSize.Level1)
214 {
215 int32_t socketId = TRANS_TEST_ID;
216 int64_t len = TRANS_TEST_ID;
217 UpdateChannelStatistics(socketId, len);
218 EXPECT_EQ(g_channelStatisticsList, nullptr);
219
220 int32_t ret = ClientTransStatisticsInit();
221 EXPECT_EQ(ret, SOFTBUS_OK);
222 SocketResource *newItem = reinterpret_cast<SocketResource *>(SoftBusCalloc(sizeof(SocketResource)));
223 ASSERT_NE(newItem, nullptr);
224 newItem->socketId = socketId;
225 newItem->traffic = 0;
226 ListInit(&newItem->node);
227 ListAdd(&g_channelStatisticsList->list, &newItem->node);
228
229 UpdateChannelStatistics(socketId, len);
230 EXPECT_EQ(newItem->traffic, len);
231
232 ClientTransStatisticsDeinit();
233 }
234
235 /**
236 * @tc.name: PackStatisticsTest001
237 * @tc.desc: PackStatistics, use the wrong or normal parameter.
238 * @tc.type: FUNC
239 * @tc.require: I5HZ6N
240 */
241 HWTEST_F(ClientTransStatisticsTest, PackStatisticsTest001, TestSize.Level1)
242 {
243 int32_t ret = PackStatistics(nullptr, nullptr);
244 EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
245 cJSON *json = cJSON_CreateObject();
246 SocketResource *resource = reinterpret_cast<SocketResource *>(SoftBusCalloc(sizeof(SocketResource)));
247 ret = PackStatistics(json, resource);
248 EXPECT_EQ(ret, SOFTBUS_OK);
249 cJSON_Delete(json);
250 SoftBusFree(resource);
251 }
252
253 /**
254 * @tc.name: CloseChannelAndSendStatisticsTest001
255 * @tc.desc: CloseChannelAndSendStatistics, use the wrong or normal parameter.
256 * @tc.type: FUNC
257 * @tc.require: I5HZ6N
258 */
259 HWTEST_F(ClientTransStatisticsTest, CloseChannelAndSendStatisticsTest001, TestSize.Level1)
260 {
261 CloseChannelAndSendStatistics(nullptr);
262 SocketResource *resource = reinterpret_cast<SocketResource *>(SoftBusCalloc(sizeof(SocketResource)));
263 CloseChannelAndSendStatistics(resource);
264 EXPECT_NE(resource, nullptr);
265 SoftBusFree(resource);
266 }
267
268 /**
269 * @tc.name: DeleteSocketResourceByChannelIdTest002
270 * @tc.desc: DeleteSocketResourceByChannelId, use the wrong or normal parameter.
271 * @tc.type: FUNC
272 * @tc.require: I5HZ6N
273 */
274 HWTEST_F(ClientTransStatisticsTest, DeleteSocketResourceByChannelIdTest002, TestSize.Level1)
275 {
276 int32_t channelId = INVALID_VALUE;
277 int32_t channelType = CHANNEL_TYPE_UDP;
278 int32_t socketId = TRANS_TEST_ID;
279 int64_t len = TRANS_TEST_ID;
280 DeleteSocketResourceByChannelId(channelId, channelType);
281 channelId = TRANS_TEST_ID;
282 DeleteSocketResourceByChannelId(channelId, channelType);
283 EXPECT_EQ(g_channelStatisticsList, nullptr);
284
285 int32_t ret = ClientTransStatisticsInit();
286 EXPECT_EQ(ret, SOFTBUS_OK);
287 SocketResource *newItem = reinterpret_cast<SocketResource *>(SoftBusCalloc(sizeof(SocketResource)));
288 ASSERT_NE(newItem, nullptr);
289 newItem->channelId = channelId;
290 newItem->channelType = channelType;
291 newItem->socketId = socketId;
292 ListInit(&newItem->node);
293 ListAdd(&g_channelStatisticsList->list, &newItem->node);
294 g_channelStatisticsList->cnt = TRANS_TEST_ID;
295
296 UpdateChannelStatistics(socketId, len);
297 DeleteSocketResourceByChannelId(channelId, channelType);
298 EXPECT_EQ(g_channelStatisticsList->cnt, TRANS_TEST_ID);
299
300 ClientTransStatisticsDeinit();
301 }
302
303 /**
304 * @tc.name: ClientTransStatisticsDeinitTest001
305 * @tc.desc: ClientTransStatisticsDeinit, use the wrong or normal parameter.
306 * @tc.type: FUNC
307 * @tc.require: I5HZ6N
308 */
309 HWTEST_F(ClientTransStatisticsTest, ClientTransStatisticsDeinitTest001, TestSize.Level1)
310 {
311 g_channelStatisticsList = nullptr;
312 ClientTransStatisticsDeinit();
313
314 int32_t ret = ClientTransStatisticsInit();
315 EXPECT_EQ(ret, SOFTBUS_OK);
316 SocketResource *newItem = reinterpret_cast<SocketResource *>(SoftBusCalloc(sizeof(SocketResource)));
317 ASSERT_NE(newItem, nullptr);
318 newItem->channelId = TRANS_TEST_ID;
319 ListInit(&newItem->node);
320 ListAdd(&g_channelStatisticsList->list, &newItem->node);
321
322 ClientTransStatisticsDeinit();
323 EXPECT_EQ(g_channelStatisticsList, nullptr);
324 }
325
326 /**
327 * @tc.name: DeleteSocketResourceBySocketIdTest002
328 * @tc.desc: DeleteSocketResourceBySocketId, use the wrong or normal parameter.
329 * @tc.type: FUNC
330 * @tc.require: I5HZ6N
331 */
332 HWTEST_F(ClientTransStatisticsTest, DeleteSocketResourceBySocketIdTest002, TestSize.Level1)
333 {
334 int32_t socketId = TRANS_TEST_ID;
335 int64_t len = TRANS_TEST_ID;
336 DeleteSocketResourceBySocketId(socketId);
337 EXPECT_EQ(g_channelStatisticsList, nullptr);
338
339 int32_t ret = ClientTransStatisticsInit();
340 EXPECT_EQ(ret, SOFTBUS_OK);
341 SocketResource *newItem = reinterpret_cast<SocketResource *>(SoftBusCalloc(sizeof(SocketResource)));
342 ASSERT_NE(newItem, nullptr);
343 newItem->socketId = socketId;
344 ListInit(&newItem->node);
345 ListAdd(&g_channelStatisticsList->list, &newItem->node);
346 g_channelStatisticsList->cnt = TRANS_TEST_ID;
347
348 UpdateChannelStatistics(socketId, len);
349 DeleteSocketResourceBySocketId(socketId);
350 EXPECT_EQ(g_channelStatisticsList->cnt, 0);
351
352 ClientTransStatisticsDeinit();
353 }
354 } // namespace OHOS
355