• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 <iostream>
18 
19 #ifdef GTEST_API_
20 #define private public
21 #define protected public
22 #endif
23 
24 #include "i_net_stats_callback.h"
25 #include "net_all_capabilities.h"
26 
27 #include "net_stats_service_stub.h"
28 #include "common_net_stats_callback_test.h"
29 
30 namespace OHOS {
31 namespace NetManagerStandard {
32 using namespace testing::ext;
33 namespace {
34 constexpr uint64_t OUTOFFRANGECODE = 100;
35 constexpr uint32_t TEST_UINT32_VALUE = 100;
36 constexpr uint64_t TEST_UINT64_VALUE = 100;
37 constexpr const char *TEST_STRING = "test";
38 
39 class MockNetStatsServiceStub : public NetStatsServiceStub {
40 public:
41     MockNetStatsServiceStub() = default;
~MockNetStatsServiceStub()42     ~MockNetStatsServiceStub() {}
43 
GetIfaceRxBytes(uint64_t & stats,const std::string & interfaceName)44     int32_t GetIfaceRxBytes(uint64_t &stats, const std::string &interfaceName) override
45     {
46         return 0;
47     }
48 
GetIfaceTxBytes(uint64_t & stats,const std::string & interfaceName)49     int32_t GetIfaceTxBytes(uint64_t &stats, const std::string &interfaceName) override
50     {
51         return 0;
52     }
53 
GetCellularRxBytes(uint64_t & stats)54     int32_t GetCellularRxBytes(uint64_t &stats) override
55     {
56         return 0;
57     }
58 
GetCellularTxBytes(uint64_t & stats)59     int32_t GetCellularTxBytes(uint64_t &stats) override
60     {
61         return 0;
62     }
63 
GetAllRxBytes(uint64_t & stats)64     int32_t GetAllRxBytes(uint64_t &stats) override
65     {
66         return 0;
67     }
68 
GetAllTxBytes(uint64_t & stats)69     int32_t GetAllTxBytes(uint64_t &stats) override
70     {
71         return 0;
72     }
73 
GetUidRxBytes(uint64_t & stats,uint32_t uid)74     int32_t GetUidRxBytes(uint64_t &stats, uint32_t uid) override
75     {
76         return 0;
77     }
78 
GetUidTxBytes(uint64_t & stats,uint32_t uid)79     int32_t GetUidTxBytes(uint64_t &stats, uint32_t uid) override
80     {
81         return 0;
82     }
83 
GetAllStatsInfo(std::vector<NetStatsInfo> & info)84     int32_t GetAllStatsInfo(std::vector<NetStatsInfo> &info) override
85     {
86         return 0;
87     }
88 
RegisterNetStatsCallback(const sptr<INetStatsCallback> & callback)89     int32_t RegisterNetStatsCallback(const sptr<INetStatsCallback> &callback) override
90     {
91         return 0;
92     }
93 
UnregisterNetStatsCallback(const sptr<INetStatsCallback> & callback)94     int32_t UnregisterNetStatsCallback(const sptr<INetStatsCallback> &callback) override
95     {
96         return 0;
97     }
98 
GetIfaceStatsDetail(const std::string & iface,uint64_t start,uint64_t end,NetStatsInfo & statsInfo)99     int32_t GetIfaceStatsDetail(const std::string &iface, uint64_t start, uint64_t end,
100                                 NetStatsInfo &statsInfo) override
101     {
102         return 0;
103     }
104 
GetUidStatsDetail(const std::string & iface,uint32_t uid,uint64_t start,uint64_t end,NetStatsInfo & statsInfo)105     int32_t GetUidStatsDetail(const std::string &iface, uint32_t uid, uint64_t start, uint64_t end,
106                               NetStatsInfo &statsInfo) override
107     {
108         return 0;
109     }
110 
UpdateIfacesStats(const std::string & iface,uint64_t start,uint64_t end,const NetStatsInfo & stats)111     int32_t UpdateIfacesStats(const std::string &iface, uint64_t start, uint64_t end,
112                                       const NetStatsInfo &stats) override
113     {
114         return 0;
115     }
116 
UpdateStatsData()117     int32_t UpdateStatsData() override
118     {
119         return 0;
120     }
121 
ResetFactory()122     int32_t ResetFactory() override
123     {
124         return 0;
125     }
126 
GetCookieRxBytes(uint64_t & stats,uint64_t cookie)127     int32_t GetCookieRxBytes(uint64_t &stats, uint64_t cookie) override
128     {
129         return 0;
130     }
131 
GetCookieTxBytes(uint64_t & stats,uint64_t cookie)132     int32_t GetCookieTxBytes(uint64_t &stats, uint64_t cookie) override
133     {
134         return 0;
135     }
136 };
137 
138 class TestNetStatsServiceStub : public testing::Test {
139 public:
140     static void SetUpTestCase();
141     static void TearDownTestCase();
142     void SetUp();
143     void TearDown();
144 
145     static inline std::shared_ptr<NetStatsServiceStub> instance_ = std::make_shared<MockNetStatsServiceStub>();
146 };
147 
SetUpTestCase()148 void TestNetStatsServiceStub::SetUpTestCase() {}
149 
TearDownTestCase()150 void TestNetStatsServiceStub::TearDownTestCase() {}
151 
SetUp()152 void TestNetStatsServiceStub::SetUp() {}
153 
TearDown()154 void TestNetStatsServiceStub::TearDown() {}
155 
156 /**
157  * @tc.name: OnRemoteRequestTest001
158  * @tc.desc: Test NetConnCallbackStub OnRemoteRequest.
159  * @tc.type: FUNC
160  */
161 HWTEST_F(TestNetStatsServiceStub, OnRemoteRequestTest001, TestSize.Level1)
162 {
163     MessageParcel data;
164     MessageParcel reply;
165     MessageOption option;
166     int32_t ret = instance_->OnRemoteRequest(OUTOFFRANGECODE, data, reply, option);
167     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_ERR_DESCRIPTOR_MISMATCH);
168 }
169 
170 /**
171  * @tc.name: RegisterNetStatsCallbackTest001
172  * @tc.desc: Test NetConnCallbackStub RegisterNetStatsCallback.
173  * @tc.type: FUNC
174  */
175 HWTEST_F(TestNetStatsServiceStub, RegisterNetStatsCallbackTest001, TestSize.Level1)
176 {
177     MessageParcel data;
178     if (!data.WriteInterfaceToken(NetStatsServiceStub::GetDescriptor())) {
179         return;
180     }
181     sptr<INetStatsCallback> callback = new (std::nothrow) NetStatsCallbackTestCb();
182     if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
183         return;
184     }
185     MessageParcel reply;
186     MessageOption option;
187     int32_t ret = instance_->OnRemoteRequest(
188         static_cast<uint32_t>(StatsInterfaceCode::CMD_NSM_REGISTER_NET_STATS_CALLBACK), data, reply, option);
189     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
190 }
191 
192 /**
193  * @tc.name: UnregisterNetStatsCallbackTest001
194  * @tc.desc: Test NetConnCallbackStub UnregisterNetStatsCallback.
195  * @tc.type: FUNC
196  */
197 HWTEST_F(TestNetStatsServiceStub, UnregisterNetStatsCallbackTest001, TestSize.Level1)
198 {
199     MessageParcel data;
200     if (!data.WriteInterfaceToken(NetStatsServiceStub::GetDescriptor())) {
201         return;
202     }
203     sptr<INetStatsCallback> callback = new (std::nothrow) NetStatsCallbackTestCb();
204     if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
205         return;
206     }
207     MessageParcel reply;
208     MessageOption option;
209     int32_t ret = instance_->OnRemoteRequest(
210         static_cast<uint32_t>(StatsInterfaceCode::CMD_NSM_UNREGISTER_NET_STATS_CALLBACK), data, reply, option);
211     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
212 }
213 
214 /**
215  * @tc.name: GetIfaceRxBytesTest001
216  * @tc.desc: Test NetConnCallbackStub GetIfaceRxBytes.
217  * @tc.type: FUNC
218  */
219 HWTEST_F(TestNetStatsServiceStub, GetIfaceRxBytesTest001, TestSize.Level1)
220 {
221     MessageParcel data;
222     if (!data.WriteInterfaceToken(NetStatsServiceStub::GetDescriptor())) {
223         return;
224     }
225     if (!data.WriteString(TEST_STRING)) {
226         return;
227     }
228     MessageParcel reply;
229     MessageOption option;
230     int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_IFACE_RXBYTES), data,
231                                              reply, option);
232     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
233 }
234 
235 /**
236  * @tc.name: GetIfaceTxBytesTest001
237  * @tc.desc: Test NetConnCallbackStub GetIfaceTxBytes.
238  * @tc.type: FUNC
239  */
240 HWTEST_F(TestNetStatsServiceStub, GetIfaceTxBytesTest001, TestSize.Level1)
241 {
242     MessageParcel data;
243     if (!data.WriteInterfaceToken(NetStatsServiceStub::GetDescriptor())) {
244         return;
245     }
246     if (!data.WriteString(TEST_STRING)) {
247         return;
248     }
249     MessageParcel reply;
250     MessageOption option;
251     int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_IFACE_TXBYTES), data,
252                                              reply, option);
253     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
254 }
255 
256 /**
257  * @tc.name: GetCellularRxBytesTest001
258  * @tc.desc: Test NetConnCallbackStub GetCellularRxBytes.
259  * @tc.type: FUNC
260  */
261 HWTEST_F(TestNetStatsServiceStub, GetCellularRxBytesTest001, TestSize.Level1)
262 {
263     MessageParcel data;
264     if (!data.WriteInterfaceToken(NetStatsServiceStub::GetDescriptor())) {
265         return;
266     }
267     MessageParcel reply;
268     MessageOption option;
269     int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_CELLULAR_RXBYTES), data,
270                                              reply, option);
271     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
272 }
273 
274 /**
275  * @tc.name: GetCellularTxBytesTest001
276  * @tc.desc: Test NetConnCallbackStub GetCellularTxBytes.
277  * @tc.type: FUNC
278  */
279 HWTEST_F(TestNetStatsServiceStub, GetCellularTxBytesTest001, TestSize.Level1)
280 {
281     MessageParcel data;
282     if (!data.WriteInterfaceToken(NetStatsServiceStub::GetDescriptor())) {
283         return;
284     }
285     MessageParcel reply;
286     MessageOption option;
287     int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_CELLULAR_TXBYTES), data,
288                                              reply, option);
289     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
290 }
291 
292 /**
293  * @tc.name: GetAllRxBytesTest001
294  * @tc.desc: Test NetConnCallbackStub GetAllRxBytes.
295  * @tc.type: FUNC
296  */
297 HWTEST_F(TestNetStatsServiceStub, GetAllRxBytesTest001, TestSize.Level1)
298 {
299     MessageParcel data;
300     if (!data.WriteInterfaceToken(NetStatsServiceStub::GetDescriptor())) {
301         return;
302     }
303     MessageParcel reply;
304     MessageOption option;
305     int32_t ret =
306         instance_->OnRemoteRequest(static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_ALL_RXBYTES), data, reply, option);
307     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
308 }
309 
310 /**
311  * @tc.name: GetAllTxBytesTest001
312  * @tc.desc: Test NetConnCallbackStub GetAllTxBytes.
313  * @tc.type: FUNC
314  */
315 HWTEST_F(TestNetStatsServiceStub, GetAllTxBytesTest001, TestSize.Level1)
316 {
317     MessageParcel data;
318     if (!data.WriteInterfaceToken(NetStatsServiceStub::GetDescriptor())) {
319         return;
320     }
321     MessageParcel reply;
322     MessageOption option;
323     int32_t ret =
324         instance_->OnRemoteRequest(static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_ALL_TXBYTES), data, reply, option);
325     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
326 }
327 
328 /**
329  * @tc.name: GetUidRxBytesTest001
330  * @tc.desc: Test NetConnCallbackStub GetUidRxBytes.
331  * @tc.type: FUNC
332  */
333 HWTEST_F(TestNetStatsServiceStub, GetUidRxBytesTest001, TestSize.Level1)
334 {
335     MessageParcel data;
336     if (!data.WriteInterfaceToken(NetStatsServiceStub::GetDescriptor())) {
337         return;
338     }
339     if (!data.WriteUint32(TEST_UINT32_VALUE)) {
340         return;
341     }
342     MessageParcel reply;
343     MessageOption option;
344     int32_t ret =
345         instance_->OnRemoteRequest(static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_UID_RXBYTES), data, reply, option);
346     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
347 }
348 
349 /**
350  * @tc.name: GetUidTxBytesTest001
351  * @tc.desc: Test NetConnCallbackStub GetUidTxBytes.
352  * @tc.type: FUNC
353  */
354 HWTEST_F(TestNetStatsServiceStub, GetUidTxBytesTest001, TestSize.Level1)
355 {
356     MessageParcel data;
357     if (!data.WriteInterfaceToken(NetStatsServiceStub::GetDescriptor())) {
358         return;
359     }
360     if (!data.WriteUint32(TEST_UINT32_VALUE)) {
361         return;
362     }
363     MessageParcel reply;
364     MessageOption option;
365     int32_t ret =
366         instance_->OnRemoteRequest(static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_UID_TXBYTES), data, reply, option);
367     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
368 }
369 
370 /**
371  * @tc.name: GetIfaceStatsDetailTest001
372  * @tc.desc: Test NetConnCallbackStub GetIfaceStatsDetail.
373  * @tc.type: FUNC
374  */
375 HWTEST_F(TestNetStatsServiceStub, GetIfaceStatsDetailTest001, TestSize.Level1)
376 {
377     MessageParcel data;
378     if (!data.WriteInterfaceToken(NetStatsServiceStub::GetDescriptor())) {
379         return;
380     }
381     if (!data.WriteString(TEST_STRING)) {
382         return;
383     }
384     if (!data.WriteUint64(TEST_UINT64_VALUE)) {
385         return;
386     }
387     if (!data.WriteUint64(TEST_UINT64_VALUE)) {
388         return;
389     }
390     MessageParcel reply;
391     MessageOption option;
392     int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_IFACE_STATS_DETAIL),
393                                              data, reply, option);
394     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
395 }
396 
397 /**
398  * @tc.name: GetUidStatsDetailTest001
399  * @tc.desc: Test NetConnCallbackStub GetUidStatsDetail.
400  * @tc.type: FUNC
401  */
402 HWTEST_F(TestNetStatsServiceStub, GetUidStatsDetailTest001, TestSize.Level1)
403 {
404     MessageParcel data;
405     if (!data.WriteInterfaceToken(NetStatsServiceStub::GetDescriptor())) {
406         return;
407     }
408     if (!data.WriteString(TEST_STRING)) {
409         return;
410     }
411     if (!data.WriteUint32(TEST_UINT32_VALUE)) {
412         return;
413     }
414     if (!data.WriteUint64(TEST_UINT64_VALUE)) {
415         return;
416     }
417     if (!data.WriteUint64(TEST_UINT64_VALUE)) {
418         return;
419     }
420     MessageParcel reply;
421     MessageOption option;
422     int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_UID_STATS_DETAIL), data,
423                                              reply, option);
424     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
425 }
426 
427 /**
428  * @tc.name: UpdateIfacesStatsTest001
429  * @tc.desc: Test NetConnCallbackStub UpdateIfacesStats.
430  * @tc.type: FUNC
431  */
432 HWTEST_F(TestNetStatsServiceStub, UpdateIfacesStatsTest001, TestSize.Level1)
433 {
434     MessageParcel data;
435     if (!data.WriteInterfaceToken(NetStatsServiceStub::GetDescriptor())) {
436         return;
437     }
438     if (!data.WriteString(TEST_STRING)) {
439         return;
440     }
441     if (!data.WriteUint64(TEST_UINT64_VALUE)) {
442         return;
443     }
444     if (!data.WriteUint64(TEST_UINT64_VALUE)) {
445         return;
446     }
447     NetStatsInfo stats;
448     stats.Marshalling(data);
449     MessageParcel reply;
450     MessageOption option;
451     int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(StatsInterfaceCode::CMD_UPDATE_IFACES_STATS), data,
452                                              reply, option);
453     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
454 }
455 
456 /**
457  * @tc.name: UpdateStatsDataTest001
458  * @tc.desc: Test NetConnCallbackStub UpdateStatsData.
459  * @tc.type: FUNC
460  */
461 HWTEST_F(TestNetStatsServiceStub, UpdateStatsDataTest001, TestSize.Level1)
462 {
463     MessageParcel data;
464     if (!data.WriteInterfaceToken(NetStatsServiceStub::GetDescriptor())) {
465         return;
466     }
467     MessageParcel reply;
468     MessageOption option;
469     int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(StatsInterfaceCode::CMD_UPDATE_STATS_DATA), data,
470                                              reply, option);
471     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
472 }
473 
474 /**
475  * @tc.name: ResetFactoryTest001
476  * @tc.desc: Test NetConnCallbackStub ResetFactory.
477  * @tc.type: FUNC
478  */
479 HWTEST_F(TestNetStatsServiceStub, ResetFactoryTest001, TestSize.Level1)
480 {
481     MessageParcel data;
482     if (!data.WriteInterfaceToken(NetStatsServiceStub::GetDescriptor())) {
483         return;
484     }
485     MessageParcel reply;
486     MessageOption option;
487     int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(StatsInterfaceCode::CMD_NSM_RESET_FACTORY), data,
488                                              reply, option);
489     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
490 }
491 
492 /**
493  * @tc.name: GetAllStatsInfoTest001
494  * @tc.desc: Test NetConnCallbackStub GetAllStatsInfo.
495  * @tc.type: FUNC
496  */
497 HWTEST_F(TestNetStatsServiceStub, GetAllStatsInfoTest001, TestSize.Level1)
498 {
499     MessageParcel data;
500     if (!data.WriteInterfaceToken(NetStatsServiceStub::GetDescriptor())) {
501         return;
502     }
503     MessageParcel reply;
504     MessageOption option;
505     int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_ALL_STATS_INFO), data,
506                                              reply, option);
507     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
508 }
509 
510 /**
511  * @tc.name: GetCookieRxBytesTest001
512  * @tc.desc: Test NetConnCallbackStub GetCookieRxBytes.
513  * @tc.type: FUNC
514  */
515 HWTEST_F(TestNetStatsServiceStub, GetCookieRxBytesTest001, TestSize.Level1)
516 {
517     MessageParcel data;
518     if (!data.WriteInterfaceToken(NetStatsServiceStub::GetDescriptor())) {
519         return;
520     }
521     if (!data.WriteUint64(TEST_UINT64_VALUE)) {
522         return;
523     }
524     MessageParcel reply;
525     MessageOption option;
526     int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_COOKIE_RXBYTES), data,
527                                              reply, option);
528     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
529 }
530 
531 /**
532  * @tc.name: GetCookieTxBytesTest001
533  * @tc.desc: Test NetConnCallbackStub GetCookieTxBytes.
534  * @tc.type: FUNC
535  */
536 HWTEST_F(TestNetStatsServiceStub, GetCookieTxBytesTest001, TestSize.Level1)
537 {
538     MessageParcel data;
539     if (!data.WriteInterfaceToken(NetStatsServiceStub::GetDescriptor())) {
540         return;
541     }
542     if (!data.WriteUint64(TEST_UINT64_VALUE)) {
543         return;
544     }
545     MessageParcel reply;
546     MessageOption option;
547     int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_COOKIE_TXBYTES), data,
548                                              reply, option);
549     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
550 }
551 } // namespace
552 } // namespace NetManagerStandard
553 } // namespace OHOS