• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "net_stats_info.h"
19 
20 namespace OHOS {
21 namespace NetManagerStandard {
22 namespace {
23 constexpr int64_t TEST_RXBYTES = 15453;
24 constexpr int64_t TEST_TXBYTES = 45115;
25 constexpr int64_t TEST_RXPACKETS = 5646894;
26 constexpr int64_t TEST_TXPACKETS = 7894;
27 constexpr const char *TEST_IDENT = "2";
GetNetStatsInfoData()28 NetStatsInfo GetNetStatsInfoData()
29 {
30     NetStatsInfo info;
31     info.ident_ = TEST_IDENT;
32     info.rxBytes_ = TEST_RXBYTES;
33     info.rxPackets_ = TEST_RXPACKETS;
34     info.txBytes_ = TEST_TXBYTES;
35     info.txPackets_ = TEST_TXPACKETS;
36     return info;
37 }
38 } // namespace
39 
40 using namespace testing::ext;
41 class NetStatsInfoTest : public testing::Test {
42 public:
43     static void SetUpTestCase();
44     static void TearDownTestCase();
45     void SetUp();
46     void TearDown();
47     uint32_t GetTestTime();
48 };
49 
SetUpTestCase()50 void NetStatsInfoTest::SetUpTestCase() {}
51 
TearDownTestCase()52 void NetStatsInfoTest::TearDownTestCase() {}
53 
SetUp()54 void NetStatsInfoTest::SetUp() {}
55 
TearDown()56 void NetStatsInfoTest::TearDown() {}
57 
58 /**
59  * @tc.name: MarshallingTest001
60  * @tc.desc: Test NetStatsInfo Marshalling.
61  * @tc.type: FUNC
62  */
63 HWTEST_F(NetStatsInfoTest, MarshallingTest001, TestSize.Level1)
64 {
65     Parcel parcel;
66     NetStatsInfo info = GetNetStatsInfoData();
67     EXPECT_TRUE(info.Marshalling(parcel));
68     NetStatsInfo result;
69     EXPECT_TRUE(NetStatsInfo::Unmarshalling(parcel, result));
70     EXPECT_EQ(result.ident_, info.ident_);
71     EXPECT_EQ(result.rxBytes_, info.rxBytes_);
72     EXPECT_EQ(result.txBytes_, info.txBytes_);
73     EXPECT_EQ(result.rxPackets_, info.rxPackets_);
74     EXPECT_EQ(result.txPackets_, info.txPackets_);
75 }
76 
77 /**
78  * @tc.name: MarshallingTest002
79  * @tc.desc: Test NetStatsInfo Marshalling.
80  * @tc.type: FUNC
81  */
82 HWTEST_F(NetStatsInfoTest, MarshallingUnmarshallingTest002, TestSize.Level1)
83 {
84     Parcel parcel;
85     NetStatsInfo info = GetNetStatsInfoData();
86     EXPECT_TRUE(NetStatsInfo::Marshalling(parcel, info));
87     NetStatsInfo result;
88     EXPECT_TRUE(NetStatsInfo::Unmarshalling(parcel, result));
89     EXPECT_EQ(result.ident_, info.ident_);
90     EXPECT_EQ(result.rxBytes_, info.rxBytes_);
91     EXPECT_EQ(result.txBytes_, info.txBytes_);
92     EXPECT_EQ(result.rxPackets_, info.rxPackets_);
93     EXPECT_EQ(result.txPackets_, info.txPackets_);
94 }
95 
96 /**
97  * @tc.name: MarshallingUnmarshallingTest003
98  * @tc.desc: Test NetStatsInfo Marshalling.
99  * @tc.type: FUNC
100  */
101 HWTEST_F(NetStatsInfoTest, MarshallingUnmarshallingTest003, TestSize.Level1)
102 {
103     Parcel parcel;
104     std::vector<NetStatsInfo> statsInfos;
105     NetStatsInfo infoa = GetNetStatsInfoData();
106     statsInfos.push_back(infoa);
107     NetStatsInfo infob = GetNetStatsInfoData();
108     statsInfos.push_back(infob);
109 
110     EXPECT_TRUE(NetStatsInfo::Marshalling(parcel, statsInfos));
111     std::vector<NetStatsInfo> results;
112     EXPECT_TRUE(NetStatsInfo::Unmarshalling(parcel, results));
113 }
114 
115 /**
116  * @tc.name: MarshallingUnmarshallingTest004
117  * @tc.desc: Test NetStatsInfo Marshalling.
118  * @tc.type: FUNC
119  */
120 HWTEST_F(NetStatsInfoTest, MarshallingUnmarshallingTest004, TestSize.Level1)
121 {
122     Parcel parcel;
123     std::unordered_map<uint32_t, NetStatsInfo> statsInfos;
124     NetStatsInfo infoa = GetNetStatsInfoData();
125     statsInfos.emplace(0, infoa);
126     NetStatsInfo infob = GetNetStatsInfoData();
127     statsInfos.emplace(1, infob);
128 
129     EXPECT_TRUE(NetStatsInfo::Marshalling(parcel, statsInfos));
130     std::unordered_map<uint32_t, NetStatsInfo> results;
131     EXPECT_TRUE(NetStatsInfo::Unmarshalling(parcel, results));
132 }
133 
134 /**
135  * @tc.name: NetStatsInfoOperator
136  * @tc.desc: Test NetStatsInfo Operator.
137  * @tc.type: FUNC
138  */
139 HWTEST_F(NetStatsInfoTest, NetStatsInfoOperator, TestSize.Level1)
140 {
141     NetStatsInfo infoa = GetNetStatsInfoData();
142     NetStatsInfo infob = GetNetStatsInfoData();
143     NetStatsInfo infoc = infoa - infob;
144     EXPECT_EQ(infoc.rxBytes_, 0);
145     infoc += infoa;
146     EXPECT_EQ(infoc.rxBytes_, infoa.rxBytes_);
147 }
148 } // namespace NetManagerStandard
149 } // namespace OHOS
150