1 /*
2 * Copyright (c) 2022-2023 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 <gtest/gtest.h>
17
18 #ifdef GTEST_API_
19 #define private public
20 #define protected public
21 #endif
22 #include "net_stats_database_defines.h"
23 #include "net_manager_constants.h"
24 #include "net_stats_cached.h"
25 #include "net_stats_history.h"
26 #include "net_stats_constants.h"
27 #include "net_stats_database_helper.h"
28 namespace OHOS {
29 namespace NetManagerStandard {
30 namespace {
31 using namespace testing::ext;
32 #define DTEST_LOG std::cout << __func__ << ":" << __LINE__ << ":"
33 } // namespace
34
35 class NetStatsCachedTest : public testing::Test {
36 public:
37 static void SetUpTestCase();
38 static void TearDownTestCase();
39 void SetUp();
40 void TearDown();
41 static inline auto instance_ = std::make_shared<NetStatsCached>();
42 };
43
SetUpTestCase()44 void NetStatsCachedTest::SetUpTestCase() {}
45
TearDownTestCase()46 void NetStatsCachedTest::TearDownTestCase() {}
47
SetUp()48 void NetStatsCachedTest::SetUp() {}
49
TearDown()50 void NetStatsCachedTest::TearDown() {}
51
52 HWTEST_F(NetStatsCachedTest, CacheUidStatsTest001, TestSize.Level1)
53 {
54 auto ret = instance_->CheckUidStor();
55 EXPECT_FALSE(ret);
56 ret = instance_->CheckIfaceStor();
57 EXPECT_FALSE(ret);
58 instance_->CacheUidStats();
59 instance_->CacheIfaceStats();
60 NetStatsInfo info;
61 instance_->stats_.PushUidStats(info);
62 instance_->stats_.PushIfaceStats(info);
63 if (instance_->stats_.GetUidStatsInfo().empty()) {
64 EXPECT_EQ(instance_->stats_.GetCurrentUidStats(), static_cast<uint64_t>(0));
65 } else {
66 EXPECT_GT(instance_->stats_.GetCurrentUidStats(), static_cast<uint64_t>(0));
67 }
68 if (instance_->stats_.GetIfaceStatsInfo().empty()) {
69 EXPECT_EQ(instance_->stats_.GetCurrentIfaceStats(), static_cast<uint64_t>(0));
70 } else {
71 EXPECT_GT(instance_->stats_.GetCurrentIfaceStats(), static_cast<uint64_t>(0));
72 }
73
74 instance_->stats_.ResetUidStats();
75 instance_->stats_.ResetIfaceStats();
76 EXPECT_TRUE(instance_->stats_.GetUidStatsInfo().empty());
77 EXPECT_TRUE(instance_->stats_.GetIfaceStatsInfo().empty());
78 EXPECT_EQ(instance_->stats_.GetCurrentUidStats(), static_cast<uint64_t>(0));
79 EXPECT_EQ(instance_->stats_.GetCurrentIfaceStats(), static_cast<uint64_t>(0));
80 instance_->CacheStats();
81 }
82
83 HWTEST_F(NetStatsCachedTest, StartCachedTest001, TestSize.Level1)
84 {
85 instance_->StartCached();
86 std::vector<NetStatsInfo> allInfo;
87 int32_t uid = 4800;
88 int32_t start = 1;
89 int32_t end = 10;
90 auto history = std::make_unique<NetStatsHistory>();
91 int32_t ret = history->GetHistory(allInfo, uid, start, end);
92 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
93 instance_->GetUidStatsCached(allInfo);
94 instance_->GetIfaceStatsCached(allInfo);
95 instance_->CacheIfaceStats();
96 instance_->WriteStats();
97 uint32_t threshold = 1000;
98 instance_->SetCycleThreshold(threshold);
99 instance_->ForceUpdateStats();
100 instance_->Reset();
101 }
102
103 HWTEST_F(NetStatsCachedTest, WriteIfaceStatsTest001, TestSize.Level1)
104 {
105 instance_->isForce_ = true;
106 instance_->stats_.currentIfaceStats_ = 0;
107 instance_->WriteIfaceStats();
108 instance_->stats_.currentIfaceStats_ = NetStatsCached::DEFAULT_TRAFFIC_STATISTICS_THRESHOLD_BYTES + 1;
109 instance_->WriteIfaceStats();
110 instance_->isForce_ = false;
111 instance_->stats_.currentIfaceStats_ = 0;
112 instance_->WriteIfaceStats();
113 instance_->stats_.currentIfaceStats_ = NetStatsCached::DEFAULT_TRAFFIC_STATISTICS_THRESHOLD_BYTES + 1;
114 instance_->WriteIfaceStats();
115 EXPECT_FALSE(instance_->CheckIfaceStor());
116 }
117
118 HWTEST_F(NetStatsCachedTest, WriteUidStatsTest001, TestSize.Level1)
119 {
120 instance_->isForce_ = true;
121 instance_->stats_.currentUidStats_ = 0;
122 instance_->WriteUidStats();
123 instance_->stats_.currentUidStats_ = NetStatsCached::DEFAULT_TRAFFIC_STATISTICS_THRESHOLD_BYTES + 1;
124 instance_->WriteUidStats();
125 instance_->isForce_ = false;
126 instance_->stats_.currentUidStats_ = 0;
127 instance_->WriteUidStats();
128 instance_->stats_.currentUidStats_ = NetStatsCached::DEFAULT_TRAFFIC_STATISTICS_THRESHOLD_BYTES + 1;
129 instance_->WriteUidStats();
130 EXPECT_FALSE(instance_->CheckUidStor());
131 }
132 } // namespace NetManagerStandard
133 } // namespace OHOS
134