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