• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 <ctime>
17 #include <thread>
18 #include <vector>
19 
20 #include <gtest/gtest.h>
21 
22 #include "netmanager_base_test_security.h"
23 
24 #ifdef GTEST_API_
25 #define private public
26 #endif
27 #include "net_manager_center.h"
28 #include "net_mgr_log_wrapper.h"
29 #include "net_stats_callback_test.h"
30 #include "net_stats_client.h"
31 #include "net_stats_constants.h"
32 
33 namespace OHOS {
34 namespace NetManagerStandard {
35 namespace {
36 #define DTEST_LOG std::cout << __func__ << ":" << __LINE__ << ":"
37 using namespace testing::ext;
38 constexpr const char *ETH_IFACE_NAME = "lo";
39 constexpr int64_t TEST_UID = 1010;
40 constexpr int32_t TEST_SOCKETFD = 1;
41 
GetIfaceNamesFromManager(std::list<std::string> & ifaceNames)42 void GetIfaceNamesFromManager(std::list<std::string> &ifaceNames)
43 {
44     NetManagerCenter::GetInstance().GetIfaceNames(BEARER_CELLULAR, ifaceNames);
45 }
46 constexpr const char *MOCK_IFACE = "wlan0";
47 constexpr uint32_t MOCK_UID = 1234;
48 constexpr uint64_t MOCK_DATE = 115200;
49 constexpr uint64_t MOCK_RXBYTES = 10000;
50 constexpr uint64_t MOCK_TXBYTES = 11000;
51 constexpr uint64_t MOCK_RXPACKETS = 1000;
52 constexpr uint64_t MOCK_TXPACKETS = 1100;
53 } // namespace
54 
55 class NetStatsClientTest : public testing::Test {
56 public:
57     static void SetUpTestCase();
58     static void TearDownTestCase();
59     void SetUp();
60     void TearDown();
61     uint32_t GetTestTime();
62     static inline sptr<NetStatsCallbackTest> callback_ = nullptr;
63 };
64 
SetUpTestCase()65 void NetStatsClientTest::SetUpTestCase()
66 {
67     callback_ = new (std::nothrow) NetStatsCallbackTest();
68 }
69 
TearDownTestCase()70 void NetStatsClientTest::TearDownTestCase() {}
71 
SetUp()72 void NetStatsClientTest::SetUp() {}
73 
TearDown()74 void NetStatsClientTest::TearDown() {}
75 
76 /**
77  * @tc.name: RegisterNetStatsCallbackTest001
78  * @tc.desc: Test NetStatsClient RegisterNetStatsCallback.
79  * @tc.type: FUNC
80  */
81 HWTEST_F(NetStatsClientTest, RegisterNetStatsCallbackTest001, TestSize.Level1)
82 {
83     int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->RegisterNetStatsCallback(callback_);
84     EXPECT_GE(ret, 0);
85 }
86 
87 /**
88  * @tc.name: UnregisterNetStatsCallbackTest001
89  * @tc.desc: Test NetStatsClient UnregisterNetStatsCallback.
90  * @tc.type: FUNC
91  */
92 HWTEST_F(NetStatsClientTest, UnregisterNetStatsCallbackTest001, TestSize.Level1)
93 {
94     int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->UnregisterNetStatsCallback(callback_);
95     EXPECT_GE(ret, 0);
96 }
97 
98 /**
99  * @tc.name: GetIfaceRxBytesTest001
100  * @tc.desc: Test NetStatsClient GetIfaceRxBytes.
101  * @tc.type: FUNC
102  */
103 HWTEST_F(NetStatsClientTest, GetIfaceRxBytesTest001, TestSize.Level1)
104 {
105     uint64_t stats = 0;
106     int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->GetIfaceRxBytes(stats, ETH_IFACE_NAME);
107     EXPECT_GE(stats, static_cast<uint64_t>(0));
108     DTEST_LOG << "Ret" << ret << std::endl;
109 }
110 
111 /**
112  * @tc.name: GetIfaceTxBytesTest001
113  * @tc.desc: Test NetStatsClient GetIfaceTxBytes.
114  * @tc.type: FUNC
115  */
116 HWTEST_F(NetStatsClientTest, GetIfaceTxBytesTest001, TestSize.Level1)
117 {
118     uint64_t stats = 0;
119     int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->GetIfaceTxBytes(stats, ETH_IFACE_NAME);
120     EXPECT_GE(stats, static_cast<uint64_t>(0));
121     DTEST_LOG << "Ret" << ret << std::endl;
122 }
123 
124 /**
125  * @tc.name: GetCellularRxBytesTest001
126  * @tc.desc: Test NetStatsClient GetCellularRxBytes.
127  * @tc.type: FUNC
128  */
129 HWTEST_F(NetStatsClientTest, GetCellularRxBytesTest001, TestSize.Level1)
130 {
131     std::list<std::string> ifaceNames;
132     uint64_t stats = 0;
133     int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->GetCellularRxBytes(stats);
134     GetIfaceNamesFromManager(ifaceNames);
135     if (ifaceNames.empty()) {
136         EXPECT_GE(ret, -1);
137         return;
138     }
139     EXPECT_GE(stats, static_cast<uint64_t>(0));
140 }
141 
142 /**
143  * @tc.name: GetCellularTxBytesTest001
144  * @tc.desc: Test NetStatsClient GetCellularTxBytes.
145  * @tc.type: FUNC
146  */
147 HWTEST_F(NetStatsClientTest, GetCellularTxBytesTest001, TestSize.Level1)
148 {
149     std::list<std::string> ifaceNames;
150     uint64_t stats = 0;
151     int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->GetCellularTxBytes(stats);
152     GetIfaceNamesFromManager(ifaceNames);
153     if (ifaceNames.empty()) {
154         EXPECT_GE(ret, -1);
155         return;
156     }
157     EXPECT_GE(stats, static_cast<uint64_t>(0));
158 }
159 
160 /**
161  * @tc.name: GetAllRxBytesTest001
162  * @tc.desc: Test NetStatsClient GetAllRxBytes.
163  * @tc.type: FUNC
164  */
165 HWTEST_F(NetStatsClientTest, GetAllRxBytesTest001, TestSize.Level1)
166 {
167     uint64_t stats = 0;
168     int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->GetAllRxBytes(stats);
169     EXPECT_GE(stats, static_cast<uint64_t>(0));
170     DTEST_LOG << "Ret" << ret << std::endl;
171 }
172 
173 /**
174  * @tc.name: GetAllTxBytesTest001
175  * @tc.desc: Test NetStatsClient GetAllTxBytes.
176  * @tc.type: FUNC
177  */
178 HWTEST_F(NetStatsClientTest, GetAllTxBytesTest001, TestSize.Level1)
179 {
180     uint64_t stats = 0;
181     int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->GetAllTxBytes(stats);
182     EXPECT_GE(stats, static_cast<uint64_t>(0));
183     DTEST_LOG << "Ret" << ret << std::endl;
184 }
185 
186 /**
187  * @tc.name: GetUidRxBytesTest001
188  * @tc.desc: Test NetStatsClient GetUidRxBytes.
189  * @tc.type: FUNC
190  */
191 HWTEST_F(NetStatsClientTest, GetUidRxBytesTest001, TestSize.Level1)
192 {
193     uint64_t stats = 0;
194     int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->GetUidRxBytes(stats, TEST_UID);
195     EXPECT_GE(stats, static_cast<uint64_t>(0));
196     DTEST_LOG << "Ret" << ret << std::endl;
197 }
198 
199 /**
200  * @tc.name: GetUidTxBytesTest001
201  * @tc.desc: Test NetStatsClient GetUidTxBytes.
202  * @tc.type: FUNC
203  */
204 HWTEST_F(NetStatsClientTest, GetUidTxBytesTest001, TestSize.Level1)
205 {
206     uint64_t stats = 0;
207     int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->GetUidTxBytes(stats, TEST_UID);
208     EXPECT_GE(stats, static_cast<uint64_t>(0));
209     DTEST_LOG << "Ret" << ret << std::endl;
210 }
211 
212 HWTEST_F(NetStatsClientTest, NetStatsClient001, TestSize.Level1)
213 {
214     NetManagerBaseAccessToken token;
215     NetStatsInfo info;
216     int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->GetIfaceStatsDetail(MOCK_IFACE, 0, UINT32_MAX, info);
217     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
218     std::cout << info.IfaceData() << std::endl;
219 }
220 
221 HWTEST_F(NetStatsClientTest, NetStatsClient002, TestSize.Level1)
222 {
223     NetManagerBaseAccessToken token;
224     NetStatsInfo info;
225     int32_t ret =
226         DelayedSingleton<NetStatsClient>::GetInstance()->GetUidStatsDetail(MOCK_IFACE, MOCK_UID, 0, UINT32_MAX, info);
227     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
228     std::cout << info.UidData() << std::endl;
229 }
230 
231 HWTEST_F(NetStatsClientTest, NetStatsClient003, TestSize.Level1)
232 {
233     NETMGR_LOG_I("NetStatsClientTest::NetStatsClient003 enter");
234     std::string iface = "test_iface";
235     NetManagerBaseAccessToken token;
236     NetStatsInfo info;
237     info.iface_ = iface;
238     info.date_ = MOCK_DATE;
239     info.rxBytes_ = MOCK_RXBYTES;
240     info.txBytes_ = MOCK_TXBYTES;
241     info.rxPackets_ = MOCK_RXPACKETS;
242     info.txPackets_ = MOCK_TXPACKETS;
243     NETMGR_LOG_I("UpdateIfacesStats enter");
244     int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->UpdateIfacesStats(iface, 0, UINT32_MAX, info);
245     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
246     NETMGR_LOG_I("GetIfaceStatsDetail enter");
247     DelayedSingleton<NetStatsClient>::GetInstance()->GetIfaceStatsDetail(iface, 0, UINT32_MAX, info);
248     EXPECT_EQ(info.iface_, iface);
249     EXPECT_EQ(info.date_, UINT32_MAX);
250     EXPECT_EQ(info.rxBytes_, MOCK_RXBYTES);
251     EXPECT_EQ(info.txBytes_, MOCK_TXBYTES);
252     EXPECT_EQ(info.rxPackets_, MOCK_RXPACKETS);
253     EXPECT_EQ(info.txPackets_, MOCK_TXPACKETS);
254 }
255 
256 HWTEST_F(NetStatsClientTest, NetStatsClient004, TestSize.Level1)
257 {
258     NetStatsInfo info;
259     info.iface_ = MOCK_IFACE;
260     info.date_ = MOCK_DATE;
261     info.rxBytes_ = MOCK_RXBYTES;
262     info.txBytes_ = MOCK_TXBYTES;
263     info.rxPackets_ = MOCK_RXPACKETS;
264     info.txPackets_ = MOCK_TXPACKETS;
265 
266     int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->ResetFactory();
267     EXPECT_EQ(ret, NETMANAGER_ERR_PERMISSION_DENIED);
268 }
269 
270 HWTEST_F(NetStatsClientTest, NetStatsClient005, TestSize.Level1)
271 {
272     NetManagerBaseAccessToken token;
273     NetStatsInfo info;
274     info.iface_ = MOCK_IFACE;
275     info.date_ = MOCK_DATE;
276     info.rxBytes_ = MOCK_RXBYTES;
277     info.txBytes_ = MOCK_TXBYTES;
278     info.rxPackets_ = MOCK_RXPACKETS;
279     info.txPackets_ = MOCK_TXPACKETS;
280 
281     int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->ResetFactory();
282     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
283 }
284 
285 HWTEST_F(NetStatsClientTest, NetStatsClient006, TestSize.Level1)
286 {
287     int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->UpdateStatsData();
288     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
289 }
290 
291 HWTEST_F(NetStatsClientTest, NetStatsClient007, TestSize.Level1)
292 {
293     sptr<IRemoteObject::DeathRecipient> deathRecipient =
294         new (std::nothrow) NetStatsClient::NetStatsDeathRecipient(*DelayedSingleton<NetStatsClient>::GetInstance());
295     sptr<IRemoteObject> remote = nullptr;
296     deathRecipient->OnRemoteDied(remote);
297     std::vector<NetStatsInfo> infos;
298     int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->GetAllStatsInfo(infos);
299     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
300 }
301 
302 HWTEST_F(NetStatsClientTest, NetStatsClient008, TestSize.Level1)
303 {
304     sptr<IRemoteObject::DeathRecipient> deathRecipient =
305         new (std::nothrow) NetStatsClient::NetStatsDeathRecipient(*DelayedSingleton<NetStatsClient>::GetInstance());
306     sptr<IRemoteObject> remote = nullptr;
307     deathRecipient->OnRemoteDied(remote);
308     deathRecipient->OnRemoteDied(remote);
309     uint64_t stats = 0;
310     int32_t sockfd = 0;
311     int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->GetSockfdRxBytes(stats, sockfd);
312     EXPECT_EQ(ret, NETMANAGER_ERR_INVALID_PARAMETER);
313     ret = DelayedSingleton<NetStatsClient>::GetInstance()->GetSockfdTxBytes(stats, sockfd);
314     EXPECT_EQ(ret, NETMANAGER_ERR_INVALID_PARAMETER);
315 
316     ret = DelayedSingleton<NetStatsClient>::GetInstance()->GetSockfdRxBytes(stats, TEST_SOCKETFD);
317     EXPECT_EQ(ret, NETMANAGER_ERR_OPERATION_FAILED);
318     ret = DelayedSingleton<NetStatsClient>::GetInstance()->GetSockfdTxBytes(stats, TEST_SOCKETFD);
319     EXPECT_EQ(ret, NETMANAGER_ERR_OPERATION_FAILED);
320 }
321 } // namespace NetManagerStandard
322 } // namespace OHOS
323