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 #include <random>
18
19 #include <gtest/gtest.h>
20
21 #include "net_manager_constants.h"
22 #include "net_stats_database_helper.h"
23 #include "net_stats_data_handler.h"
24 #include "net_stats_database_defines.h"
25
26 namespace OHOS {
27 namespace NetManagerStandard {
28 using namespace NetStatsDatabaseDefines;
29
30 using namespace testing::ext;
31 namespace {
32 const std::string CCH = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
33 constexpr uint32_t UID_MAX_TEST = 200;
34 constexpr uint32_t MOCK_DATA_SIZE = 1000;
35 const std::vector<std::string> MOCK_IFACE = {"wlan0", "eth0", "eth1", "usb0", "wlan1", "usb1"};
36 std::random_device g_rd;
37 std::mt19937 g_regn(g_rd());
GetUint32()38 uint32_t GetUint32()
39 {
40 return static_cast<uint32_t>(g_regn()) % UID_MAX_TEST;
41 }
42
GetUint64()43 uint64_t GetUint64()
44 {
45 return static_cast<uint64_t>(g_regn());
46 }
47
GetMockDate()48 uint64_t GetMockDate()
49 {
50 return static_cast<uint64_t>(g_regn() % LONG_MAX);
51 }
52
GetMockIface()53 std::string GetMockIface()
54 {
55 return MOCK_IFACE.at(g_regn() % MOCK_IFACE.size());
56 }
57 } // namespace
58 class NetStatsMockData : public testing::Test {
59 public:
60 static void SetUpTestCase();
61 static void TearDownTestCase();
62 void SetUp();
63 void TearDown();
64 };
65
SetUpTestCase()66 void NetStatsMockData::SetUpTestCase() {}
67
TearDownTestCase()68 void NetStatsMockData::TearDownTestCase() {}
69
SetUp()70 void NetStatsMockData::SetUp() {}
71
TearDown()72 void NetStatsMockData::TearDown() {}
73
74 HWTEST_F(NetStatsMockData, CreateTableTest001, TestSize.Level1)
75 {
76 auto handler = std::make_unique<NetStatsDataHandler>();
77 std::vector<NetStatsInfo> statsData;
78 for (uint32_t i = 0; i < MOCK_DATA_SIZE; i++) {
79 NetStatsInfo info;
80 info.uid_ = GetUint32();
81 info.date_ = GetMockDate();
82 info.iface_ = GetMockIface();
83 info.rxBytes_ = GetUint64();
84 info.rxPackets_ = GetUint64();
85 info.txBytes_ = GetUint64();
86 info.txPackets_ = GetUint64();
87 statsData.push_back(info);
88 }
89 int32_t ret = handler->WriteStatsData(statsData, UID_TABLE);
90 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
91 ret = handler->WriteStatsData(statsData, IFACE_TABLE);
92 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
93 }
94 } // namespace NetManagerStandard
95 } // namespace OHOS