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_stats_callback_test.h"
23 #include "net_stats_client.h"
24 #include "net_stats_constants.h"
25 #include "net_manager_center.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;
GetIfaceNamesFromManager(std::list<std::string> & ifaceNames)32 void GetIfaceNamesFromManager(std::list<std::string> &ifaceNames)
33 {
34 NetManagerCenter::GetInstance().GetIfaceNames(BEARER_CELLULAR, ifaceNames);
35 }
36 } // namespace
37
38 using namespace testing::ext;
39 class NetStatsClientTest : public testing::Test {
40 public:
41 static void SetUpTestCase();
42 static void TearDownTestCase();
43 void SetUp();
44 void TearDown();
45 uint32_t GetTestTime();
46 static inline sptr<NetStatsCallbackTest> callback_ = nullptr;
47 };
48
SetUpTestCase()49 void NetStatsClientTest::SetUpTestCase()
50 {
51 callback_ = new (std::nothrow) NetStatsCallbackTest();
52 }
53
TearDownTestCase()54 void NetStatsClientTest::TearDownTestCase() {}
55
SetUp()56 void NetStatsClientTest::SetUp() {}
57
TearDown()58 void NetStatsClientTest::TearDown() {}
59
60 /**
61 * @tc.name: RegisterNetStatsCallbackTest001
62 * @tc.desc: Test NetStatsClient RegisterNetStatsCallback.
63 * @tc.type: FUNC
64 */
65 HWTEST_F(NetStatsClientTest, RegisterNetStatsCallbackTest001, TestSize.Level1)
66 {
67 int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->RegisterNetStatsCallback(callback_);
68 EXPECT_GE(ret, 0);
69 }
70
71 /**
72 * @tc.name: UnregisterNetStatsCallbackTest001
73 * @tc.desc: Test NetStatsClient UnregisterNetStatsCallback.
74 * @tc.type: FUNC
75 */
76 HWTEST_F(NetStatsClientTest, UnregisterNetStatsCallbackTest001, TestSize.Level1)
77 {
78 int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->UnregisterNetStatsCallback(callback_);
79 EXPECT_GE(ret, 0);
80 }
81
82 /**
83 * @tc.name: GetIfaceRxBytesTest001
84 * @tc.desc: Test NetStatsClient GetIfaceRxBytes.
85 * @tc.type: FUNC
86 */
87 HWTEST_F(NetStatsClientTest, GetIfaceRxBytesTest001, TestSize.Level1)
88 {
89 int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->GetIfaceRxBytes(ETH_IFACE_NAME);
90 EXPECT_GE(ret, 0);
91 }
92
93 /**
94 * @tc.name: GetIfaceTxBytesTest001
95 * @tc.desc: Test NetStatsClient GetIfaceTxBytes.
96 * @tc.type: FUNC
97 */
98 HWTEST_F(NetStatsClientTest, GetIfaceTxBytesTest001, TestSize.Level1)
99 {
100 int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->GetIfaceTxBytes(ETH_IFACE_NAME);
101 EXPECT_GE(ret, 0);
102 }
103
104 /**
105 * @tc.name: GetCellularRxBytesTest001
106 * @tc.desc: Test NetStatsClient GetCellularRxBytes.
107 * @tc.type: FUNC
108 */
109 HWTEST_F(NetStatsClientTest, GetCellularRxBytesTest001, TestSize.Level1)
110 {
111 std::list<std::string> ifaceNames;
112 int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->GetCellularRxBytes();
113 GetIfaceNamesFromManager(ifaceNames);
114 if (ifaceNames.empty()) {
115 EXPECT_GE(ret, -1);
116 return;
117 }
118 EXPECT_GE(ret, 0);
119 }
120
121 /**
122 * @tc.name: GetCellularTxBytesTest001
123 * @tc.desc: Test NetStatsClient GetCellularTxBytes.
124 * @tc.type: FUNC
125 */
126 HWTEST_F(NetStatsClientTest, GetCellularTxBytesTest001, TestSize.Level1)
127 {
128 std::list<std::string> ifaceNames;
129 int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->GetCellularTxBytes();
130 GetIfaceNamesFromManager(ifaceNames);
131 if (ifaceNames.empty()) {
132 EXPECT_GE(ret, -1);
133 return;
134 }
135 EXPECT_GE(ret, 0);
136 }
137
138 /**
139 * @tc.name: GetAllRxBytesTest001
140 * @tc.desc: Test NetStatsClient GetAllRxBytes.
141 * @tc.type: FUNC
142 */
143 HWTEST_F(NetStatsClientTest, GetAllRxBytesTest001, TestSize.Level1)
144 {
145 int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->GetAllRxBytes();
146 EXPECT_GE(ret, 0);
147 }
148
149 /**
150 * @tc.name: GetAllTxBytesTest001
151 * @tc.desc: Test NetStatsClient GetAllTxBytes.
152 * @tc.type: FUNC
153 */
154 HWTEST_F(NetStatsClientTest, GetAllTxBytesTest001, TestSize.Level1)
155 {
156 int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->GetAllTxBytes();
157 EXPECT_GE(ret, 0);
158 }
159
160 /**
161 * @tc.name: GetUidRxBytesTest001
162 * @tc.desc: Test NetStatsClient GetUidRxBytes.
163 * @tc.type: FUNC
164 */
165 HWTEST_F(NetStatsClientTest, GetUidRxBytesTest001, TestSize.Level1)
166 {
167 int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->GetUidRxBytes(TEST_UID);
168 EXPECT_GE(ret, 0);
169 }
170
171 /**
172 * @tc.name: GetUidTxBytesTest001
173 * @tc.desc: Test NetStatsClient GetUidTxBytes.
174 * @tc.type: FUNC
175 */
176 HWTEST_F(NetStatsClientTest, GetUidTxBytesTest001, TestSize.Level1)
177 {
178 int32_t ret = DelayedSingleton<NetStatsClient>::GetInstance()->GetUidTxBytes(TEST_UID);
179 EXPECT_GE(ret, 0);
180 }
181 } // namespace NetManagerStandard
182 } // namespace OHOS
183