1 /*
2  * Copyright (c) 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 <memory>
17 
18 #include <gtest/gtest.h>
19 
20 #include "net_manager_constants.h"
21 #include "net_mgr_log_wrapper.h"
22 #include "netmanager_base_common_utils.h"
23 #include "net_stats_database_defines.h"
24 #include "net_stats_history.h"
25 
26 namespace OHOS {
27 namespace NetManagerStandard {
28 using namespace NetStatsDatabaseDefines;
29 namespace {
30 constexpr uint64_t TIME_CYCLE = 60;
31 constexpr uint32_t UID = 2000222;
32 } // namespace
33 
34 using namespace testing::ext;
35 class NetStatsHistoryTest : public testing::Test {
36 public:
37     static void SetUpTestCase();
38     static void TearDownTestCase();
39     void SetUp();
40     void TearDown();
41 };
42 
SetUpTestCase()43 void NetStatsHistoryTest::SetUpTestCase() {}
44 
TearDownTestCase()45 void NetStatsHistoryTest::TearDownTestCase() {}
46 
SetUp()47 void NetStatsHistoryTest::SetUp() {}
48 
TearDown()49 void NetStatsHistoryTest::TearDown() {}
50 
51 HWTEST_F(NetStatsHistoryTest, HistoryTest001, TestSize.Level1)
52 {
53     auto history = std::make_unique<NetStatsHistory>();
54     std::vector<NetStatsInfo> infos;
55     int32_t ret = history->GetHistory(infos);
__anonf12380a80202(const auto &info) 56     std::for_each(infos.begin(), infos.end(), [](const auto &info) { std::cout << info.UidData() << std::endl; });
57     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
58 }
59 
60 HWTEST_F(NetStatsHistoryTest, HistoryTest002, TestSize.Level1)
61 {
62     auto history = std::make_unique<NetStatsHistory>();
63     std::vector<NetStatsInfo> infos;
64     uint64_t currentTime = CommonUtils::GetCurrentSecond();
65     int32_t ret = history->GetHistory(infos, currentTime - TIME_CYCLE, currentTime + TIME_CYCLE);
__anonf12380a80302(const auto &info) 66     std::for_each(infos.begin(), infos.end(), [](const auto &info) { std::cout << info.UidData() << std::endl; });
67     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
68 }
69 
70 HWTEST_F(NetStatsHistoryTest, HistoryTest003, TestSize.Level1)
71 {
72     auto history = std::make_unique<NetStatsHistory>();
73     std::vector<NetStatsInfo> infos;
74     int32_t ret = history->GetHistory(infos, 1152, 0, LONG_MAX);
__anonf12380a80402(const auto &info) 75     std::for_each(infos.begin(), infos.end(), [](const auto &info) { std::cout << info.UidData() << std::endl; });
76     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
77 }
78 
79 HWTEST_F(NetStatsHistoryTest, HistoryTest004, TestSize.Level1)
80 {
81     auto history = std::make_unique<NetStatsHistory>();
82     std::vector<NetStatsInfo> infos;
83     uint64_t currentTime = CommonUtils::GetCurrentSecond();
84     int32_t ret = history->GetHistory(infos, 1152, currentTime - TIME_CYCLE, currentTime + TIME_CYCLE);
__anonf12380a80502(const auto &info) 85     std::for_each(infos.begin(), infos.end(), [](const auto &info) { std::cout << info.UidData() << std::endl; });
86     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
87 }
88 
89 HWTEST_F(NetStatsHistoryTest, HistoryTest005, TestSize.Level1)
90 {
91     auto history = std::make_unique<NetStatsHistory>();
92     std::vector<NetStatsInfo> infos;
93     int32_t ret = history->GetHistory(infos, "wlan0");
__anonf12380a80602(const auto &info) 94     std::for_each(infos.begin(), infos.end(), [](const auto &info) { std::cout << info.UidData() << std::endl; });
95     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
96 }
97 
98 HWTEST_F(NetStatsHistoryTest, HistoryTest006, TestSize.Level1)
99 {
100     auto history = std::make_unique<NetStatsHistory>();
101     std::vector<NetStatsInfo> infos;
102     uint64_t currentTime = CommonUtils::GetCurrentSecond();
103     int32_t ret = history->GetHistory(infos, "wlan0", currentTime - TIME_CYCLE, currentTime + TIME_CYCLE);
__anonf12380a80702(const auto &info) 104     std::for_each(infos.begin(), infos.end(), [](const auto &info) { std::cout << info.UidData() << std::endl; });
105     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
106 }
107 
108 HWTEST_F(NetStatsHistoryTest, HistoryTest007, TestSize.Level1)
109 {
110     auto history = std::make_unique<NetStatsHistory>();
111     std::vector<NetStatsInfo> infos;
112     int32_t ret = history->GetHistory(infos, "wlan0", 1152, 0, LONG_MAX);
__anonf12380a80802(const auto &info) 113     std::for_each(infos.begin(), infos.end(), [](const auto &info) { std::cout << info.UidData() << std::endl; });
114     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
115 }
116 
117 HWTEST_F(NetStatsHistoryTest, HistoryTest008, TestSize.Level1)
118 {
119     auto history = std::make_unique<NetStatsHistory>();
120     std::vector<NetStatsInfo> infos;
121     uint64_t currentTime = CommonUtils::GetCurrentSecond();
122     int32_t ret = history->GetHistory(infos, "wlan0", 1152, currentTime - TIME_CYCLE, currentTime + TIME_CYCLE);
__anonf12380a80902(const auto &info) 123     std::for_each(infos.begin(), infos.end(), [](const auto &info) { std::cout << info.UidData() << std::endl; });
124     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
125 }
126 
127 HWTEST_F(NetStatsHistoryTest, HistoryTest009, TestSize.Level1)
128 {
129     auto history = std::make_unique<NetStatsHistory>();
130     std::vector<NetStatsInfo> infos;
131     std::string ident = "2";
132     int32_t ret = history->GetHistoryByIdent(infos, ident, 0, LONG_MAX);
__anonf12380a80a02(const auto &info) 133     std::for_each(infos.begin(), infos.end(), [](const auto &info) { std::cout << info.UidData() << std::endl; });
134     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
135 }
136 
137 HWTEST_F(NetStatsHistoryTest, HistoryTest010, TestSize.Level1)
138 {
139     auto history = std::make_unique<NetStatsHistory>();
140     std::vector<NetStatsInfo> infos;
141     uint32_t uid = UID;
142     std::string ident = "2";
143     int32_t ret = history->GetHistory(infos, uid, ident, 0, LONG_MAX);
__anonf12380a80b02(const auto &info) 144     std::for_each(infos.begin(), infos.end(), [](const auto &info) { std::cout << info.UidData() << std::endl; });
145     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
146 }
147 } // namespace NetManagerStandard
148 } // namespace OHOS