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 "data_flow_statistics.h"
23 #include "net_mgr_log_wrapper.h"
24 #include "net_stats_callback_test.h"
25 #include "net_stats_client.h"
26 #include "net_stats_constants.h"
27 #include "netmanager_base_test_security.h"
28
29 namespace OHOS {
30 namespace NetManagerStandard {
31 namespace {
32 using namespace testing::ext;
33 constexpr uint32_t TEST_UID = 1001;
34 const std::string ETH_IFACE_NAME = "eth0";
35 } // namespace
36 class DataFlowStatisticsTest : public testing::Test {
37 public:
38 static void SetUpTestCase();
39 static void TearDownTestCase();
40 void SetUp();
41 void TearDown();
42 sptr<NetStatsCallbackTest> GetINetStatsCallbackSample() const;
43 };
44
SetUpTestCase()45 void DataFlowStatisticsTest::SetUpTestCase() {}
46
TearDownTestCase()47 void DataFlowStatisticsTest::TearDownTestCase() {}
48
SetUp()49 void DataFlowStatisticsTest::SetUp() {}
50
TearDown()51 void DataFlowStatisticsTest::TearDown() {}
52
GetINetStatsCallbackSample() const53 sptr<NetStatsCallbackTest> DataFlowStatisticsTest::GetINetStatsCallbackSample() const
54 {
55 sptr<NetStatsCallbackTest> callback = new (std::nothrow) NetStatsCallbackTest();
56 return callback;
57 }
58
59 /**
60 * @tc.name: NetStatsManager001
61 * @tc.desc: Test DataFlowStatisticsTest GetCellularRxBytes.
62 * @tc.type: FUNC
63 */
64 HWTEST_F(DataFlowStatisticsTest, NetStatsManager001, TestSize.Level1)
65 {
66 std::unique_ptr<DataFlowStatistics> flow = std::make_unique<DataFlowStatistics>();
67 int64_t ret = flow->GetCellularRxBytes();
68 ASSERT_GE(ret, 0);
69 }
70
71 /**
72 * @tc.name: NetStatsManager002
73 * @tc.desc: Test DataFlowStatisticsTest GetCellularTxBytes.
74 * @tc.type: FUNC
75 */
76 HWTEST_F(DataFlowStatisticsTest, NetStatsManager002, TestSize.Level1)
77 {
78 std::unique_ptr<DataFlowStatistics> flow = std::make_unique<DataFlowStatistics>();
79 int64_t ret = flow->GetCellularTxBytes();
80 ASSERT_GE(ret, 0);
81 }
82
83 /**
84 * @tc.name: NetStatsManager003
85 * @tc.desc: Test DataFlowStatisticsTest GetAllRxBytes.
86 * @tc.type: FUNC
87 */
88 HWTEST_F(DataFlowStatisticsTest, NetStatsManager003, TestSize.Level1)
89 {
90 std::unique_ptr<DataFlowStatistics> flow = std::make_unique<DataFlowStatistics>();
91 int64_t ret = flow->GetAllRxBytes();
92 ASSERT_GE(ret, 0);
93 }
94
95 /**
96 * @tc.name: NetStatsManager004
97 * @tc.desc: Test DataFlowStatisticsTest GetAllTxBytes.
98 * @tc.type: FUNC
99 */
100 HWTEST_F(DataFlowStatisticsTest, NetStatsManager004, TestSize.Level1)
101 {
102 std::unique_ptr<DataFlowStatistics> flow = std::make_unique<DataFlowStatistics>();
103 int64_t ret = flow->GetAllTxBytes();
104 ASSERT_GE(ret, 0);
105 }
106
107 HWTEST_F(DataFlowStatisticsTest, NetStatsManager005, TestSize.Level1)
108 {
109 std::unique_ptr<DataFlowStatistics> flow = std::make_unique<DataFlowStatistics>();
110 int64_t ret = flow->GetUidTxBytes(TEST_UID);
111 ASSERT_GE(ret, -1);
112 ret = flow->GetUidRxBytes(TEST_UID);
113 ASSERT_GE(ret, -1);
114 }
115
116 /**
117 * @tc.name: NetStatsManager007
118 * @tc.desc: Test DataFlowStatisticsTest GetIfaceRxBytes.
119 * @tc.type: FUNC
120 */
121 HWTEST_F(DataFlowStatisticsTest, NetStatsManager007, TestSize.Level1)
122 {
123 std::unique_ptr<DataFlowStatistics> flow = std::make_unique<DataFlowStatistics>();
124 std::string iface = ETH_IFACE_NAME;
125 int64_t ret = flow->GetIfaceRxBytes(iface);
126 ASSERT_GE(ret, 0);
127 }
128
129 /**
130 * @tc.name: NetStatsManager008
131 * @tc.desc: Test DataFlowStatisticsTest GetIfaceTxBytes.
132 * @tc.type: FUNC
133 */
134 HWTEST_F(DataFlowStatisticsTest, NetStatsManager008, TestSize.Level1)
135 {
136 std::unique_ptr<DataFlowStatistics> flow = std::make_unique<DataFlowStatistics>();
137 std::string iface = ETH_IFACE_NAME;
138 int64_t ret = flow->GetIfaceTxBytes(iface);
139 ASSERT_GE(ret, 0);
140 }
141
142 /**
143 * @tc.name: NetStatsManager009
144 * @tc.desc: Test DataFlowStatisticsTest GetIfaceRxPackets.
145 * @tc.type: FUNC
146 */
147 HWTEST_F(DataFlowStatisticsTest, NetStatsManager009, TestSize.Level1)
148 {
149 std::unique_ptr<DataFlowStatistics> flow = std::make_unique<DataFlowStatistics>();
150 std::string iface = ETH_IFACE_NAME;
151 int64_t ret = flow->GetIfaceRxPackets(iface);
152 ASSERT_GE(ret, 0);
153 }
154
155 /**
156 * @tc.name: NetStatsManager010
157 * @tc.desc: Test DataFlowStatisticsTest GetIfaceTxPackets.
158 * @tc.type: FUNC
159 */
160 HWTEST_F(DataFlowStatisticsTest, NetStatsManager010, TestSize.Level1)
161 {
162 std::unique_ptr<DataFlowStatistics> flow = std::make_unique<DataFlowStatistics>();
163 std::string iface = ETH_IFACE_NAME;
164 int64_t ret = flow->GetIfaceTxPackets(iface);
165 ASSERT_GE(ret, 0);
166 }
167
168 /**
169 * @tc.name: NetStatsManager011
170 * @tc.desc: Test DataFlowStatisticsTest RegisterNetStatsCallback.
171 * @tc.type: FUNC
172 */
173 HWTEST_F(DataFlowStatisticsTest, NetStatsManager011, TestSize.Level1)
174 {
175 NetManagerBaseAccessToken token;
176 sptr<NetStatsCallbackTest> callback = GetINetStatsCallbackSample();
177 int32_t result = DelayedSingleton<NetStatsClient>::GetInstance()->RegisterNetStatsCallback(callback);
178 ASSERT_EQ(result, NETMANAGER_SUCCESS);
179 result = DelayedSingleton<NetStatsClient>::GetInstance()->UnregisterNetStatsCallback(callback);
180 ASSERT_EQ(result, NETMANAGER_SUCCESS);
181 }
182 } // namespace NetManagerStandard
183 } // namespace OHOS
184