1 /*
2 * Copyright (c) 2022-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 #include <gtest/gtest.h>
16
17 #include "comm_log.h"
18 #include "securec.h"
19 #include "softbus_adapter_mem.h"
20 #include "softbus_app_info.h"
21 #include "softbus_common.h"
22 #include "softbus_error_code.h"
23 #include "legacy/softbus_hidumper_alarm.h"
24 #include "legacy/softbus_hidumper_broadcast.h"
25 #include "legacy/softbus_hidumper_buscenter.h"
26 #include "legacy/softbus_hidumper_conn.h"
27 #include "legacy/softbus_hidumper_disc.h"
28 #include "legacy/softbus_hidumper_interface.h"
29 #include "legacy/softbus_hidumper_nstack.h"
30 #include "legacy/softbus_hidumper_stats.h"
31 #include "legacy/softbus_hidumper_trans.h"
32 #include "legacy/softbus_hidumper_util.h"
33 #include "legacy/softbus_hidumper.h"
34 #include "trans_network_statistics.c"
35 #include "trans_network_statistics.h"
36
37 using namespace std;
38 using namespace testing::ext;
39
40 namespace OHOS {
41 class TransNetworkStatisticsTest : public testing::Test {
42 public:
43 static void SetUpTestCase();
44 static void TearDownTestCase();
45 void SetUp();
46 void TearDown();
47 };
48
SetUpTestCase(void)49 void TransNetworkStatisticsTest::SetUpTestCase(void) {}
50
TearDownTestCase(void)51 void TransNetworkStatisticsTest::TearDownTestCase(void) {}
52
SetUp(void)53 void TransNetworkStatisticsTest::SetUp(void) {}
54
TearDown(void)55 void TransNetworkStatisticsTest::TearDown(void) {}
56
57 /* *
58 * @tc.name: IsChannelDfxInfoValid001
59 * @tc.desc: Test IsChannelDfxInfoValid when channelId is less than 0.
60 * @tc.type: FUNC
61 * @tc.require:
62 */
63 HWTEST_F(TransNetworkStatisticsTest, IsChannelDfxInfoValid001, TestSize.Level1)
64 {
65 int32_t channelId = -1;
66 int32_t channelType = 1;
67 EXPECT_FALSE(IsChannelDfxInfoValid(channelId, channelType));
68 }
69
70 /* *
71 * @tc.name: IsChannelDfxInfoValid002
72 * @tc.desc: Test IsChannelDfxInfoValid when g_channelDfxInfoList is nullptr.
73 * @tc.type: FUNC
74 * @tc.require:
75 */
76 HWTEST_F(TransNetworkStatisticsTest, IsChannelDfxInfoValid002, TestSize.Level1)
77 {
78 int32_t channelId = 1;
79 int32_t channelType = 1;
80 TransNetworkStatisticsDeinit();
81 EXPECT_FALSE(IsChannelDfxInfoValid(channelId, channelType));
82 }
83
84 /* *
85 * @tc.name: IsChannelDfxInfoValid003
86 * @tc.desc: Test IsChannelDfxInfoValid when channelType is invalid
87 * @tc.type: FUNC
88 * @tc.require:
89 */
90 HWTEST_F(TransNetworkStatisticsTest, IsChannelDfxInfoValid003, TestSize.Level1)
91 {
92 int32_t channelId = 1;
93 int32_t channelType = -1;
94 EXPECT_EQ(TransNetworkStatisticsInit(), SOFTBUS_OK);
95 EXPECT_FALSE(IsChannelDfxInfoValid(channelId, channelType));
96 TransNetworkStatisticsDeinit();
97 }
98
99 /* *
100 * @tc.name: ChannelStatisticsInfoInit001
101 * @tc.desc: Test ChannelStatisticsInfoInit when info is nullptr.
102 * @tc.type: FUNC
103 * @tc.require:
104 */
105 HWTEST_F(TransNetworkStatisticsTest, ChannelStatisticsInfoInit001, TestSize.Level1)
106 {
107 int32_t channelId = 1;
108 const void *dataInfo = "testData";
109 uint32_t len = 8;
110 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ChannelStatisticsInfoInit(nullptr, channelId, dataInfo, len));
111 }
112
113 /* *
114 * @tc.name: ChannelStatisticsInfoInit002
115 * @tc.desc: Test ChannelStatisticsInfoInit when dataInfo is nullptr.
116 * @tc.type: FUNC
117 * @tc.require:
118 */
119 HWTEST_F(TransNetworkStatisticsTest, ChannelStatisticsInfoInit002, TestSize.Level1)
120 {
121 int32_t channelId = 1;
122 ChannelStatisticsInfo info;
123 uint32_t len = 8;
124 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ChannelStatisticsInfoInit(&info, channelId, nullptr, len));
125 }
126
127 /* *
128 * @tc.name: ChannelStatisticsInfoInit003
129 * @tc.desc: Test ChannelStatisticsInfoInit when len is greater than MAX_SOCKET_RESOURCE_LEN.
130 * @tc.type: FUNC
131 * @tc.require:
132 */
133 HWTEST_F(TransNetworkStatisticsTest, ChannelStatisticsInfoInit003, TestSize.Level1)
134 {
135 int32_t channelId = 1;
136 ChannelStatisticsInfo info;
137 const void *dataInfo = "testData";
138 uint32_t len = MAX_SOCKET_RESOURCE_LEN + 1;
139 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ChannelStatisticsInfoInit(&info, channelId, dataInfo, len));
140 }
141
142 /* *
143 * @tc.name: ChannelStatisticsInfoInit004
144 * @tc.desc: Test ChannelStatisticsInfoInit when all parameters are valid.
145 * @tc.type: FUNC
146 * @tc.require:
147 */
148 HWTEST_F(TransNetworkStatisticsTest, ChannelStatisticsInfoInit004, TestSize.Level1)
149 {
150 int32_t channelId = 1;
151 ChannelStatisticsInfo info;
152 const void *dataInfo = "testData";
153 uint32_t len = 8;
154 EXPECT_EQ(SOFTBUS_OK, ChannelStatisticsInfoInit(&info, channelId, dataInfo, len));
155 SoftBusFree(info.channelInfo);
156 }
157
158 /* *
159 * @tc.name: PackNetworkStatistics001
160 * @tc.desc: Test PackNetworkStatistics when json is nullptr.
161 * @tc.type: FUNC
162 * @tc.require:
163 */
164 HWTEST_F(TransNetworkStatisticsTest, PackNetworkStatistics001, TestSize.Level1)
165 {
166 NetworkStatisticsInfo info;
167 EXPECT_EQ(SOFTBUS_INVALID_PARAM, PackNetworkStatistics(nullptr, &info));
168 }
169
170 /* *
171 * @tc.name: PackNetworkStatistics002
172 * @tc.desc: Test PackNetworkStatistics when info is nullptr.
173 * @tc.type: FUNC
174 * @tc.require:
175 */
176 HWTEST_F(TransNetworkStatisticsTest, PackNetworkStatistics002, TestSize.Level1)
177 {
178 cJSON *json = cJSON_CreateObject();
179 EXPECT_EQ(SOFTBUS_INVALID_PARAM, PackNetworkStatistics(json, nullptr));
180 cJSON_Delete(json);
181 }
182
183 /* *
184 * @tc.name: PackNetworkStatistics003
185 * @tc.desc: Test PackNetworkStatistics when all parameters are valid.
186 * @tc.type: FUNC
187 * @tc.require:
188 */
189 HWTEST_F(TransNetworkStatisticsTest, PackNetworkStatistics003, TestSize.Level1)
190 {
191 cJSON *json = cJSON_CreateObject();
192 NetworkStatisticsInfo info;
193 info.resource.laneId = 1234567890;
194 EXPECT_EQ(EOK, strcpy_s(info.resource.localUdid, sizeof(info.resource.localUdid), "localUdid"));
195 EXPECT_EQ(EOK, strcpy_s(info.resource.peerUdid, sizeof(info.resource.peerUdid), "peerUdid"));
196 info.resource.laneLinkType = 1;
197 info.startTime = 1234567890;
198 info.endTime = 1234567890;
199
200 EXPECT_EQ(SOFTBUS_OK, PackNetworkStatistics(json, &info));
201 cJSON_Delete(json);
202 }
203
204 /* *
205 * @tc.name: AddChannelStatisticsInfo001
206 * @tc.desc: Test AddChannelStatisticsInfo when channelId is less than 0.
207 * @tc.type: FUNC
208 * @tc.require:
209 */
210 HWTEST_F(TransNetworkStatisticsTest, AddChannelStatisticsInfo001, TestSize.Level1)
211 {
212 int32_t channelId = -1;
213 int32_t channelType = 1;
214 AddChannelStatisticsInfo(channelId, channelType);
215 EXPECT_NO_FATAL_FAILURE(AddChannelStatisticsInfo(channelId, channelType));
216 }
217
218 /* *
219 * @tc.name: AddChannelStatisticsInfo002
220 * @tc.desc: Test AddChannelStatisticsInfo when g_channelDfxInfoList is nullptr.
221 * @tc.type: FUNC
222 * @tc.require:
223 */
224 HWTEST_F(TransNetworkStatisticsTest, AddChannelStatisticsInfo002, TestSize.Level1)
225 {
226 int32_t channelId = 1;
227 int32_t channelType = 1;
228 TransNetworkStatisticsDeinit();
229 AddChannelStatisticsInfo(channelId, channelType);
230 EXPECT_NO_FATAL_FAILURE(AddChannelStatisticsInfo(channelId, channelType));
231 }
232
233 /* *
234 * @tc.name: AddNetworkResource001
235 * @tc.desc: Test AddNetworkResource when networkResource is nullptr.
236 * @tc.type: FUNC
237 * @tc.require:
238 */
239 HWTEST_F(TransNetworkStatisticsTest, AddNetworkResource001, TestSize.Level1)
240 {
241 NetworkResource *networkResource = nullptr;
242 AddNetworkResource(networkResource);
243 EXPECT_NO_FATAL_FAILURE(AddNetworkResource(networkResource));
244 }
245
246 /* *
247 * @tc.name: AddNetworkResource002
248 * @tc.desc: Test AddNetworkResource when g_networkResourceList is nullptr.
249 * @tc.type: FUNC
250 * @tc.require:
251 */
252 HWTEST_F(TransNetworkStatisticsTest, AddNetworkResource002, TestSize.Level1)
253 {
254 NetworkResource networkResource;
255 TransNetworkStatisticsDeinit();
256 AddNetworkResource(&networkResource);
257 EXPECT_NO_FATAL_FAILURE(AddNetworkResource(&networkResource));
258 }
259
260 /* *
261 * @tc.name: UpdateNetworkResourceByLaneId001
262 * @tc.desc: Test UpdateNetworkResourceByLaneId when dataInfo is nullptr.
263 * @tc.type: FUNC
264 * @tc.require:
265 */
266 HWTEST_F(TransNetworkStatisticsTest, UpdateNetworkResourceByLaneId001, TestSize.Level1)
267 {
268 int32_t channelId = 1;
269 int32_t channelType = 1;
270 uint64_t laneId = 1234567890;
271 const void *dataInfo = nullptr;
272 uint32_t len = 8;
273 UpdateNetworkResourceByLaneId(channelId, channelType, laneId, dataInfo, len);
274 EXPECT_NO_FATAL_FAILURE(UpdateNetworkResourceByLaneId(channelId, channelType, laneId, dataInfo, len));
275 }
276 } // namespace OHOS