• 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 constexpr const char *ETH_IFACE_NAME = "lo";
31 constexpr int64_t TEST_UID = 1010;
32 constexpr int32_t TEST_FD = 2;
GetIfaceNamesFromManager(std::list<std::string> & ifaceNames)33 void GetIfaceNamesFromManager(std::list<std::string> &ifaceNames)
34 {
35     NetManagerCenter::GetInstance().GetIfaceNames(BEARER_CELLULAR, ifaceNames);
36 }
37 } // namespace
38 
39 using namespace testing::ext;
40 class NetStatsServiceTest : public testing::Test {
41 public:
42     static void SetUpTestCase();
43     static void TearDownTestCase();
44     void SetUp();
45     void TearDown();
46     uint32_t GetTestTime();
47     static inline sptr<NetStatsCallbackTest> callback_ = nullptr;
48 };
49 
SetUpTestCase()50 void NetStatsServiceTest::SetUpTestCase()
51 {
52     callback_ = new (std::nothrow) NetStatsCallbackTest();
53     DelayedSingleton<NetStatsService>::GetInstance()->OnStart();
54 }
55 
TearDownTestCase()56 void NetStatsServiceTest::TearDownTestCase()
57 {
58     DelayedSingleton<NetStatsService>::GetInstance()->OnStop();
59 }
60 
SetUp()61 void NetStatsServiceTest::SetUp() {}
62 
TearDown()63 void NetStatsServiceTest::TearDown() {}
64 
65 /**
66  * @tc.name: DumpTest001
67  * @tc.desc: Test NetStatsService RegisterNetStatsCallback.
68  * @tc.type: FUNC
69  */
70 HWTEST_F(NetStatsServiceTest, DumpTest001, TestSize.Level1)
71 {
72     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->Dump(TEST_FD, {});
73     EXPECT_GE(ret, -1);
74 }
75 
76 /**
77  * @tc.name: RegisterNetStatsCallbackTest001
78  * @tc.desc: Test NetStatsService RegisterNetStatsCallback.
79  * @tc.type: FUNC
80  */
81 HWTEST_F(NetStatsServiceTest, RegisterNetStatsCallbackTest001, TestSize.Level1)
82 {
83     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->RegisterNetStatsCallback(callback_);
84     EXPECT_EQ(ret, 0);
85 }
86 
87 /**
88  * @tc.name: RegisterNetStatsCallbackTest002
89  * @tc.desc: Test NetStatsService RegisterNetStatsCallback.
90  * @tc.type: FUNC
91  */
92 HWTEST_F(NetStatsServiceTest, RegisterNetStatsCallbackTest002, TestSize.Level1)
93 {
94     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->RegisterNetStatsCallback(nullptr);
95     EXPECT_NE(ret, 0);
96 }
97 
98 /**
99  * @tc.name: UnregisterNetStatsCallbackTest001
100  * @tc.desc: Test NetStatsService UnregisterNetStatsCallback.
101  * @tc.type: FUNC
102  */
103 HWTEST_F(NetStatsServiceTest, UnregisterNetStatsCallbackTest001, TestSize.Level1)
104 {
105     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->UnregisterNetStatsCallback(callback_);
106     EXPECT_EQ(ret, 0);
107 }
108 
109 /**
110  * @tc.name: UnregisterNetStatsCallbackTest002
111  * @tc.desc: Test NetStatsService UnregisterNetStatsCallback.
112  * @tc.type: FUNC
113  */
114 HWTEST_F(NetStatsServiceTest, UnregisterNetStatsCallbackTest002, TestSize.Level1)
115 {
116     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->UnregisterNetStatsCallback(nullptr);
117     EXPECT_NE(ret, 0);
118 }
119 
120 /**
121  * @tc.name: GetIfaceRxBytesTest001
122  * @tc.desc: Test NetStatsService GetIfaceRxBytes.
123  * @tc.type: FUNC
124  */
125 HWTEST_F(NetStatsServiceTest, GetIfaceRxBytesTest001, TestSize.Level1)
126 {
127     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->GetIfaceRxBytes(ETH_IFACE_NAME);
128     EXPECT_GE(ret, 0);
129 }
130 
131 /**
132  * @tc.name: GetIfaceTxBytesTest001
133  * @tc.desc: Test NetStatsService GetIfaceTxBytes.
134  * @tc.type: FUNC
135  */
136 HWTEST_F(NetStatsServiceTest, GetIfaceTxBytesTest001, TestSize.Level1)
137 {
138     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->GetIfaceTxBytes(ETH_IFACE_NAME);
139     EXPECT_GE(ret, 0);
140 }
141 
142 /**
143  * @tc.name: GetCellularRxBytesTest001
144  * @tc.desc: Test NetStatsService GetCellularRxBytes.
145  * @tc.type: FUNC
146  */
147 HWTEST_F(NetStatsServiceTest, GetCellularRxBytesTest001, TestSize.Level1)
148 {
149     std::list<std::string> ifaceNames;
150     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->GetCellularRxBytes();
151     GetIfaceNamesFromManager(ifaceNames);
152     if (ifaceNames.empty()) {
153         EXPECT_GE(ret, -1);
154         return;
155     }
156     EXPECT_GE(ret, 0);
157 }
158 
159 /**
160  * @tc.name: GetCellularTxBytesTest001
161  * @tc.desc: Test NetStatsService GetCellularTxBytes.
162  * @tc.type: FUNC
163  */
164 HWTEST_F(NetStatsServiceTest, GetCellularTxBytesTest001, TestSize.Level1)
165 {
166     std::list<std::string> ifaceNames;
167     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->GetCellularTxBytes();
168     GetIfaceNamesFromManager(ifaceNames);
169     if (ifaceNames.empty()) {
170         EXPECT_GE(ret, -1);
171         return;
172     }
173     EXPECT_GE(ret, 0);
174 }
175 
176 /**
177  * @tc.name: GetAllRxBytesTest001
178  * @tc.desc: Test NetStatsService GetAllRxBytes.
179  * @tc.type: FUNC
180  */
181 HWTEST_F(NetStatsServiceTest, GetAllRxBytesTest001, TestSize.Level1)
182 {
183     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->GetAllRxBytes();
184     EXPECT_GE(ret, 0);
185 }
186 
187 /**
188  * @tc.name: GetAllTxBytesTest001
189  * @tc.desc: Test NetStatsService GetAllTxBytes.
190  * @tc.type: FUNC
191  */
192 HWTEST_F(NetStatsServiceTest, GetAllTxBytesTest001, TestSize.Level1)
193 {
194     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->GetAllTxBytes();
195     EXPECT_GE(ret, 0);
196 }
197 
198 /**
199  * @tc.name: GetUidRxBytesTest001
200  * @tc.desc: Test NetStatsService GetUidRxBytes.
201  * @tc.type: FUNC
202  */
203 HWTEST_F(NetStatsServiceTest, GetUidRxBytesTest001, TestSize.Level1)
204 {
205     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->GetUidRxBytes(TEST_UID);
206     EXPECT_GE(ret, 0);
207 }
208 
209 /**
210  * @tc.name: GetUidTxBytesTest001
211  * @tc.desc: Test NetStatsService GetUidTxBytes.
212  * @tc.type: FUNC
213  */
214 HWTEST_F(NetStatsServiceTest, GetUidTxBytesTest001, TestSize.Level1)
215 {
216     int32_t ret = DelayedSingleton<NetStatsService>::GetInstance()->GetUidTxBytes(TEST_UID);
217     EXPECT_GE(ret, 0);
218 }
219 } // namespace NetManagerStandard
220 } // namespace OHOS
221