• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <ctime>
17 #include <thread>
18 #include <vector>
19 
20 #include <gtest/gtest.h>
21 
22 #include "net_manager_center.h"
23 #include "net_stats_callback_test.h"
24 #include "net_stats_constants.h"
25 #include "net_stats_service.h"
26 
27 namespace OHOS {
28 namespace NetManagerStandard {
29 namespace {
30 #define DTEST_LOG std::cout << __func__ << ":" << __LINE__ << ":"
31 constexpr const char *ETH_IFACE_NAME = "lo";
32 constexpr int64_t TEST_UID = 1010;
33 constexpr int32_t TEST_FD = 2;
34 static constexpr uint64_t TEST_COOKIE = 1;
GetIfaceNamesFromManager(std::list<std::string> & ifaceNames)35 void GetIfaceNamesFromManager(std::list<std::string> &ifaceNames)
36 {
37     NetManagerCenter::GetInstance().GetIfaceNames(BEARER_CELLULAR, ifaceNames);
38 }
39 } // namespace
40 
41 using namespace testing::ext;
42 class NetStatsServiceTest : public testing::Test {
43 public:
44     static void SetUpTestCase();
45     static void TearDownTestCase();
46     void SetUp();
47     void TearDown();
48     uint32_t GetTestTime();
49     static inline sptr<NetStatsCallbackTest> callback_ = nullptr;
50 };
51 
SetUpTestCase()52 void NetStatsServiceTest::SetUpTestCase()
53 {
54     callback_ = new (std::nothrow) NetStatsCallbackTest();
55     DelayedSingleton<NetStatsService>::GetInstance()->OnStart();
56 }
57 
TearDownTestCase()58 void NetStatsServiceTest::TearDownTestCase()
59 {
60     DelayedSingleton<NetStatsService>::GetInstance()->OnStop();
61 }
62 
SetUp()63 void NetStatsServiceTest::SetUp() {}
64 
TearDown()65 void NetStatsServiceTest::TearDown() {}
66 
67 /**
68  * @tc.name: DumpTest001
69  * @tc.desc: Test NetStatsService RegisterNetStatsCallback.
70  * @tc.type: FUNC
71  */
72 HWTEST_F(NetStatsServiceTest, DumpTest001, TestSize.Level1)
73 {
74     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->Dump(TEST_FD, {});
75     EXPECT_GE(ret, -1);
76 }
77 
78 /**
79  * @tc.name: RegisterNetStatsCallbackTest001
80  * @tc.desc: Test NetStatsService RegisterNetStatsCallback.
81  * @tc.type: FUNC
82  */
83 HWTEST_F(NetStatsServiceTest, RegisterNetStatsCallbackTest001, TestSize.Level1)
84 {
85     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->RegisterNetStatsCallback(callback_);
86     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
87 }
88 
89 /**
90  * @tc.name: RegisterNetStatsCallbackTest002
91  * @tc.desc: Test NetStatsService RegisterNetStatsCallback.
92  * @tc.type: FUNC
93  */
94 HWTEST_F(NetStatsServiceTest, RegisterNetStatsCallbackTest002, TestSize.Level1)
95 {
96     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->RegisterNetStatsCallback(nullptr);
97     EXPECT_NE(ret, NETMANAGER_SUCCESS);
98 }
99 
100 /**
101  * @tc.name: UnregisterNetStatsCallbackTest001
102  * @tc.desc: Test NetStatsService UnregisterNetStatsCallback.
103  * @tc.type: FUNC
104  */
105 HWTEST_F(NetStatsServiceTest, UnregisterNetStatsCallbackTest001, TestSize.Level1)
106 {
107     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->UnregisterNetStatsCallback(callback_);
108     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
109 }
110 
111 /**
112  * @tc.name: UnregisterNetStatsCallbackTest002
113  * @tc.desc: Test NetStatsService UnregisterNetStatsCallback.
114  * @tc.type: FUNC
115  */
116 HWTEST_F(NetStatsServiceTest, UnregisterNetStatsCallbackTest002, TestSize.Level1)
117 {
118     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->UnregisterNetStatsCallback(nullptr);
119     EXPECT_NE(ret, NETMANAGER_SUCCESS);
120 }
121 
122 /**
123  * @tc.name: GetIfaceRxBytesTest001
124  * @tc.desc: Test NetStatsService GetIfaceRxBytes.
125  * @tc.type: FUNC
126  */
127 HWTEST_F(NetStatsServiceTest, GetIfaceRxBytesTest001, TestSize.Level1)
128 {
129     uint64_t stats = 0;
130     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->GetIfaceRxBytes(stats, ETH_IFACE_NAME);
131     EXPECT_GE(stats, static_cast<uint64_t>(0));
132     DTEST_LOG << "Ret" << ret << std::endl;
133 }
134 
135 /**
136  * @tc.name: GetIfaceTxBytesTest001
137  * @tc.desc: Test NetStatsService GetIfaceTxBytes.
138  * @tc.type: FUNC
139  */
140 HWTEST_F(NetStatsServiceTest, GetIfaceTxBytesTest001, TestSize.Level1)
141 {
142     uint64_t stats = 0;
143     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->GetIfaceTxBytes(stats, ETH_IFACE_NAME);
144     EXPECT_GE(stats, static_cast<uint64_t>(0));
145     DTEST_LOG << "Ret" << ret << std::endl;
146 }
147 
148 /**
149  * @tc.name: GetCellularRxBytesTest001
150  * @tc.desc: Test NetStatsService GetCellularRxBytes.
151  * @tc.type: FUNC
152  */
153 HWTEST_F(NetStatsServiceTest, GetCellularRxBytesTest001, TestSize.Level1)
154 {
155     std::list<std::string> ifaceNames;
156     uint64_t stats = 0;
157     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->GetCellularRxBytes(stats);
158     GetIfaceNamesFromManager(ifaceNames);
159     if (ifaceNames.empty()) {
160         EXPECT_GE(ret, -1);
161         return;
162     }
163     EXPECT_GE(stats, static_cast<uint64_t>(0));
164 }
165 
166 /**
167  * @tc.name: GetCellularTxBytesTest001
168  * @tc.desc: Test NetStatsService GetCellularTxBytes.
169  * @tc.type: FUNC
170  */
171 HWTEST_F(NetStatsServiceTest, GetCellularTxBytesTest001, TestSize.Level1)
172 {
173     std::list<std::string> ifaceNames;
174     uint64_t stats = 0;
175     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->GetCellularTxBytes(stats);
176     GetIfaceNamesFromManager(ifaceNames);
177     if (ifaceNames.empty()) {
178         EXPECT_GE(ret, -1);
179         return;
180     }
181     EXPECT_GE(stats, static_cast<uint64_t>(0));
182 }
183 
184 /**
185  * @tc.name: GetAllRxBytesTest001
186  * @tc.desc: Test NetStatsService GetAllRxBytes.
187  * @tc.type: FUNC
188  */
189 HWTEST_F(NetStatsServiceTest, GetAllRxBytesTest001, TestSize.Level1)
190 {
191     uint64_t stats = 0;
192     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->GetAllRxBytes(stats);
193     EXPECT_GE(stats, static_cast<uint64_t>(0));
194     DTEST_LOG << "Ret" << ret << std::endl;
195 }
196 
197 /**
198  * @tc.name: GetAllTxBytesTest001
199  * @tc.desc: Test NetStatsService GetAllTxBytes.
200  * @tc.type: FUNC
201  */
202 HWTEST_F(NetStatsServiceTest, GetAllTxBytesTest001, TestSize.Level1)
203 {
204     uint64_t stats = 0;
205     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->GetAllTxBytes(stats);
206     EXPECT_GE(stats, static_cast<uint64_t>(0));
207     DTEST_LOG << "Ret" << ret << std::endl;
208 }
209 
210 /**
211  * @tc.name: GetUidRxBytesTest001
212  * @tc.desc: Test NetStatsService GetUidRxBytes.
213  * @tc.type: FUNC
214  */
215 HWTEST_F(NetStatsServiceTest, GetUidRxBytesTest001, TestSize.Level1)
216 {
217     uint64_t stats = 0;
218     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->GetUidRxBytes(stats, TEST_UID);
219     EXPECT_GE(stats, static_cast<uint64_t>(0));
220     DTEST_LOG << "Ret" << ret << std::endl;
221 }
222 
223 /**
224  * @tc.name: GetUidTxBytesTest001
225  * @tc.desc: Test NetStatsService GetUidTxBytes.
226  * @tc.type: FUNC
227  */
228 HWTEST_F(NetStatsServiceTest, GetUidTxBytesTest001, TestSize.Level1)
229 {
230     uint64_t stats = 0;
231     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->GetUidTxBytes(stats, TEST_UID);
232     EXPECT_GE(stats, static_cast<uint64_t>(0));
233     DTEST_LOG << "Ret" << ret << std::endl;
234 }
235 
236 /**
237  * @tc.name: GetIfaceStatsDetail001
238  * @tc.desc: Test NetStatsService GetIfaceStatsDetail.
239  * @tc.type: FUNC
240  */
241 HWTEST_F(NetStatsServiceTest, GetIfaceStatsDetail001, TestSize.Level1)
242 {
243     NetStatsInfo info;
244     std::string iface = "wlan0";
245     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->GetIfaceStatsDetail(iface, 0, UINT32_MAX, info);
246     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
247 }
248 
249 /**
250  * @tc.name: GetUidStatsDetail001
251  * @tc.desc: Test NetStatsService GetUidStatsDetail.
252  * @tc.type: FUNC
253  */
254 HWTEST_F(NetStatsServiceTest, GetUidStatsDetail001, TestSize.Level1)
255 {
256     NetStatsInfo info;
257     std::string iface = "wlan0";
258     uint32_t uid = 1234;
259     int32_t ret =
260         DelayedSingleton<NetStatsService>::GetInstance()->GetUidStatsDetail(iface, uid, 0, UINT32_MAX, info);
261     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
262 }
263 
264 /**
265  * @tc.name: UpdateIfacesStats
266  * @tc.desc: Test NetStatsService UpdateIfacesStats.
267  * @tc.type: FUNC
268  */
269 HWTEST_F(NetStatsServiceTest, UpdateIfacesStats, TestSize.Level1)
270 {
271     NetStatsInfo info;
272     std::string iface = "wlan0";
273     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->UpdateIfacesStats(iface, 0, UINT32_MAX, info);
274     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
275 }
276 
277 /**
278  * @tc.name: ResetFactory001
279  * @tc.desc: Test NetStatsService ResetFactory.
280  * @tc.type: FUNC
281  */
282 HWTEST_F(NetStatsServiceTest, ResetFactory001, TestSize.Level1)
283 {
284     NetStatsInfo info;
285     info.iface_ = "wlan0";
286     info.date_ = 115200;
287     info.rxBytes_ = 10000;
288     info.txBytes_ = 11000;
289     info.rxPackets_ = 1000;
290     info.txPackets_ = 1100;
291 
292     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->ResetFactory();
293     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
294 }
295 
296 /**
297  * @tc.name: UpdateStatsData001
298  * @tc.desc: Test NetStatsService UpdateStatsData.
299  * @tc.type: FUNC
300  */
301 HWTEST_F(NetStatsServiceTest, UpdateStatsData001, TestSize.Level1)
302 {
303     NetStatsInfo info;
304     info.iface_ = "wlan0";
305     info.date_ = 115200;
306     info.rxBytes_ = 10000;
307     info.txBytes_ = 11000;
308     info.rxPackets_ = 1000;
309     info.txPackets_ = 1100;
310 
311     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->UpdateStatsData();
312     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
313 }
314 
315 HWTEST_F(NetStatsServiceTest, GetCookieRxBytesTest001, TestSize.Level1)
316 {
317     uint64_t stats = 0;
318     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->GetCookieRxBytes(stats, TEST_COOKIE);
319     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_ERR_INTERNAL);
320 }
321 
322 HWTEST_F(NetStatsServiceTest, GetCookieTxBytesTest001, TestSize.Level1)
323 {
324     uint64_t stats = 0;
325     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->GetCookieTxBytes(stats, TEST_COOKIE);
326     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_ERR_INTERNAL);
327 }
328 
329 HWTEST_F(NetStatsServiceTest, GetAllSimStatsInfoTest001, TestSize.Level1)
330 {
331     std::vector<NetStatsInfo> infos;
332     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->GetAllSimStatsInfo(infos);
333     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
334 }
335 
336 HWTEST_F(NetStatsServiceTest, GetTrafficStatsByNetworkTest001, TestSize.Level1)
337 {
338     std::unordered_map<uint32_t, NetStatsInfo> infos;
339     sptr<NetStatsNetwork> network = new (std::nothrow) NetStatsNetwork();
340     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->GetTrafficStatsByNetwork(infos, network);
341     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
342 }
343 
344 HWTEST_F(NetStatsServiceTest, GetTrafficStatsByUidNetworkTest001, TestSize.Level1)
345 {
346     std::vector<NetStatsInfoSequence> infos;
347     uint32_t uid = 1;
348     sptr<NetStatsNetwork> network = new (std::nothrow) NetStatsNetwork();
349     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->GetTrafficStatsByUidNetwork(infos, uid, network);
350     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
351 }
352 
353 HWTEST_F(NetStatsServiceTest, SetAppStats001, TestSize.Level1)
354 {
355     PushStatsInfo info;
356     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->SetAppStats(info);
357     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
358 }
359 } // namespace NetManagerStandard
360 } // namespace OHOS
361