1 /*
2  * Copyright (c) 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 
23 #include "net_manager_constants.h"
24 #include "net_manager_native.h"
25 
26 namespace OHOS {
27 namespace nmd {
28 namespace {
29 using namespace testing::ext;
30 }
31 
32 class NetManagerNativeTest : public testing::Test {
33 public:
34     static void SetUpTestCase();
35     static void TearDownTestCase();
36     void SetUp();
37     void TearDown();
38     static inline std::shared_ptr<NetManagerNative> instance_ = nullptr;
39 };
40 
SetUpTestCase()41 void NetManagerNativeTest::SetUpTestCase()
42 {
43     instance_ = std::make_shared<NetManagerNative>();
44 }
45 
TearDownTestCase()46 void NetManagerNativeTest::TearDownTestCase() {}
47 
SetUp()48 void NetManagerNativeTest::SetUp() {}
49 
TearDown()50 void NetManagerNativeTest::TearDown() {}
51 
52 HWTEST_F(NetManagerNativeTest, UpdateInterfaceIndex001, TestSize.Level1)
53 {
54     uint32_t interfaceIndex = 10090;
55     instance_->UpdateInterfaceIndex(interfaceIndex);
56     auto result = instance_->GetCurrentInterfaceIndex();
57     auto findResult = std::find(result.begin(), result.end(), interfaceIndex);
58     EXPECT_NE(findResult, result.end());
59 }
60 
61 HWTEST_F(NetManagerNativeTest, SetInternetPermission001, TestSize.Level1)
62 {
63     uint32_t uid = 0;
64     uint8_t allow = 1;
65     uint8_t isBroker = 0;
66     auto ret = instance_->SetInternetPermission(uid, allow, isBroker);
67     EXPECT_EQ(ret, NETMANAGER_ERROR);
68 }
69 
70 HWTEST_F(NetManagerNativeTest, NetworkCreateVirtual001, TestSize.Level1)
71 {
72     int32_t netid = 12235;
73     bool hasDns = false;
74     auto ret = instance_->NetworkCreateVirtual(netid, hasDns);
75     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
76 }
77 
78 HWTEST_F(NetManagerNativeTest, NetworkAddUids001, TestSize.Level1)
79 {
80     int32_t netId = 12235;
81     std::vector<UidRange> uidRanges;
82     auto ret = instance_->NetworkAddUids(netId, uidRanges);
83     EXPECT_EQ(ret, 0);
84 }
85 
86 HWTEST_F(NetManagerNativeTest, NetworkDelUids001, TestSize.Level1)
87 {
88     int32_t netId = 12235;
89     std::vector<UidRange> uidRanges;
90     auto ret = instance_->NetworkDelUids(netId, uidRanges);
91     EXPECT_EQ(ret, 0);
92 }
93 
94 HWTEST_F(NetManagerNativeTest, NetworkSetPermissionForNetwork001, TestSize.Level1)
95 {
96     int32_t netId = 12235;
97     auto ret = instance_->NetworkSetPermissionForNetwork(netId, NetworkPermission::PERMISSION_NONE);
98     EXPECT_EQ(ret, NETMANAGER_ERROR);
99 }
100 
101 HWTEST_F(NetManagerNativeTest, GetCellularRxBytes001, TestSize.Level1)
102 {
103     auto ret = instance_->GetCellularRxBytes();
104     EXPECT_EQ(ret, 0);
105 }
106 
107 HWTEST_F(NetManagerNativeTest, GetCellularTxBytes001, TestSize.Level1)
108 {
109     auto ret = instance_->GetCellularTxBytes();
110     EXPECT_EQ(ret, 0);
111 }
112 
113 HWTEST_F(NetManagerNativeTest, GetAllRxBytes001, TestSize.Level1)
114 {
115     auto ret = instance_->GetAllRxBytes();
116     EXPECT_GE(ret, 0);
117 }
118 
119 HWTEST_F(NetManagerNativeTest, GetAllTxBytes001, TestSize.Level1)
120 {
121     auto ret = instance_->GetAllTxBytes();
122     EXPECT_GE(ret, 0);
123 }
124 
125 HWTEST_F(NetManagerNativeTest, GetUidTxBytes001, TestSize.Level1)
126 {
127     int32_t uid = 10012;
128     auto ret = instance_->GetUidTxBytes(uid);
129     EXPECT_EQ(ret, 0);
130 }
131 
132 HWTEST_F(NetManagerNativeTest, GetUidRxBytes001, TestSize.Level1)
133 {
134     int32_t uid = 10012;
135     auto ret = instance_->GetUidRxBytes(uid);
136     EXPECT_EQ(ret, 0);
137 }
138 
139 HWTEST_F(NetManagerNativeTest, GetIfaceRxBytes001, TestSize.Level1)
140 {
141     std::string testIface = "testIface";
142     auto ret = instance_->GetIfaceRxBytes(testIface);
143     EXPECT_EQ(ret, 0);
144 }
145 
146 HWTEST_F(NetManagerNativeTest, GetIfaceTxBytes001, TestSize.Level1)
147 {
148     std::string testIface = "testIface";
149     auto ret = instance_->GetIfaceTxBytes(testIface);
150     EXPECT_EQ(ret, 0);
151 }
152 
153 HWTEST_F(NetManagerNativeTest, FirewallSetUidRule001, TestSize.Level1)
154 {
155     uint32_t chain = NetManagerStandard::ChainType::CHAIN_NONE;
156     std::vector<uint32_t> uids;
157     uint32_t firewallRule = NetManagerStandard::FirewallRule::RULE_ALLOW;
158     auto ret = instance_->FirewallSetUidRule(chain, uids, firewallRule);
159     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
160 }
161 
162 HWTEST_F(NetManagerNativeTest, DnsGetAddrInfo001, TestSize.Level1)
163 {
164     std::string hostName;
165     std::string serverName;
166     AddrInfo hints;
167     uint16_t netId = 0;
168     std::vector<AddrInfo> res;
169     auto ret = instance_->DnsGetAddrInfo(hostName, serverName, hints, netId, res);
170     EXPECT_EQ(ret, NETMANAGER_ERROR);
171 }
172 
173 HWTEST_F(NetManagerNativeTest, AddStaticArpTest001, TestSize.Level1)
174 {
175     std::string ipAddr = "192.168.1.100";
176     std::string macAddr = "aa:bb:cc:dd:ee:ff";
177     std::string ifName = "wlan0";
178     auto ret = instance_->AddStaticArp(ipAddr, macAddr, ifName);
179     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
180 }
181 
182 HWTEST_F(NetManagerNativeTest, DelStaticArpTest001, TestSize.Level1)
183 {
184     std::string ipAddr = "192.168.1.100";
185     std::string macAddr = "aa:bb:cc:dd:ee:ff";
186     std::string ifName = "wlan0";
187     auto ret = instance_->DelStaticArp(ipAddr, macAddr, ifName);
188     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
189 }
190 
191 HWTEST_F(NetManagerNativeTest, CreateVnic001, TestSize.Level1)
192 {
193     uint16_t mtu = 1500;
194     std::string tunAddr = "192.168.1.100";
195     int32_t prefix = 24;
196     std::set<int32_t> uids;
197     auto ret = instance_->CreateVnic(mtu, tunAddr, prefix, uids);
198     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
199 }
200 
201 HWTEST_F(NetManagerNativeTest, DestroyVnic001, TestSize.Level1)
202 {
203     auto ret = instance_->DestroyVnic();
204     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
205 }
206 } // namespace nmd
207 } // namespace OHOS
208