• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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 #endif
21 #include "mock_netsys_native_client.h"
22 #include "net_manager_constants.h"
23 
24 namespace OHOS {
25 namespace NetManagerStandard {
26 namespace {
27 using namespace testing::ext;
28 } // namespace
29 
30 class MockNetsysNativeClientTest : public testing::Test {
31 public:
32     static void SetUpTestCase();
33 
34     static void TearDownTestCase();
35 
36     void SetUp();
37 
38     void TearDown();
39     static inline MockNetsysNativeClient nativeClient_;
40 };
41 
SetUpTestCase()42 void MockNetsysNativeClientTest::SetUpTestCase() {}
43 
TearDownTestCase()44 void MockNetsysNativeClientTest::TearDownTestCase() {}
45 
SetUp()46 void MockNetsysNativeClientTest::SetUp() {}
47 
TearDown()48 void MockNetsysNativeClientTest::TearDown() {}
49 
50 HWTEST_F(MockNetsysNativeClientTest, MockNetsysNativeClientBranchTest001, TestSize.Level1)
51 {
52     nativeClient_.Init();
53     nativeClient_.RegisterMockApi();
54     EXPECT_TRUE(nativeClient_.CheckMockApi(MOCK_INTERFACECLEARADDRS_API));
55 
56     int32_t netId = 0;
57     int32_t ret = nativeClient_.NetworkDestroy(netId);
58     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
59 
60     std::string iface = "";
61     ret = nativeClient_.NetworkAddInterface(netId, iface, BEARER_DEFAULT);
62     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
63 
64     ret = nativeClient_.NetworkRemoveInterface(netId, iface);
65     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
66 
67     ret = nativeClient_.NetworkAddRoute(netId, iface, "", "");
68     EXPECT_NE(ret, NetManagerStandard::NETMANAGER_SUCCESS);
69 
70     ret = nativeClient_.SetIpv6PrivacyExtensions(iface, 1);
71     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
72 
73     ret = nativeClient_.SetEnableIpv6(iface, 1);
74     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
75 
76     ret = nativeClient_.NetworkRemoveRoute(netId, iface, "", "");
77     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
78 
79     ret = nativeClient_.SetInterfaceDown(iface);
80     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
81 
82     ret = nativeClient_.SetInterfaceUp(iface);
83     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
84 
85     nativeClient_.ClearInterfaceAddrs(iface);
86 
87     ret = nativeClient_.GetInterfaceMtu(iface);
88     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
89 
90     ret = nativeClient_.SetInterfaceMtu(iface, 0);
91     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
92 
93     ret = nativeClient_.AddInterfaceAddress(iface, "", 0);
94     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
95 
96     ret = nativeClient_.DelInterfaceAddress(iface, "", 0);
97     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
98 }
99 
100 HWTEST_F(MockNetsysNativeClientTest, MockNetsysNativeClientBranchTest002, TestSize.Level1)
101 {
102     nativeClient_.Init();
103     nativeClient_.RegisterMockApi();
104 
105     int32_t netId = 0;
106     std::string iface = "";
107     std::vector<std::string> testInput = {};
108     int32_t ret = nativeClient_.SetResolverConfig(netId, 0, 0, testInput, testInput);
109     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
110 
111     uint16_t baseTimeoutMsec = 0;
112     uint8_t retryCount = 0;
113     ret = nativeClient_.GetResolverConfig(netId, testInput, testInput, baseTimeoutMsec, retryCount);
114     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
115 
116     ret = nativeClient_.CreateNetworkCache(netId);
117     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
118 
119     ret = nativeClient_.DestroyNetworkCache(netId);
120     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
121 
122     ret = nativeClient_.GetCellularRxBytes();
123     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
124 
125     ret = nativeClient_.GetCellularTxBytes();
126     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
127 
128     ret = nativeClient_.GetAllBytes(iface);
129     EXPECT_NE(ret, NetManagerStandard::NETMANAGER_SUCCESS);
130 
131     ret = nativeClient_.GetAllRxBytes();
132     EXPECT_GE(ret, NetManagerStandard::NETMANAGER_SUCCESS);
133 
134     ret = nativeClient_.GetAllTxBytes();
135     EXPECT_GE(ret, NetManagerStandard::NETMANAGER_SUCCESS);
136 }
137 
138 HWTEST_F(MockNetsysNativeClientTest, MockNetsysNativeClientBranchTest003, TestSize.Level1)
139 {
140     nativeClient_.Init();
141     nativeClient_.RegisterMockApi();
142 
143     uint32_t uid = 0;
144     int32_t ret = nativeClient_.GetUidTxBytes(uid);
145     EXPECT_NE(ret, NetManagerStandard::NETMANAGER_SUCCESS);
146 
147     ret = nativeClient_.GetUidRxBytes(uid);
148     EXPECT_NE(ret, NetManagerStandard::NETMANAGER_SUCCESS);
149 
150     std::string iface = "";
151     ret = nativeClient_.GetUidOnIfaceTxBytes(uid, iface);
152     EXPECT_NE(ret, NetManagerStandard::NETMANAGER_SUCCESS);
153 
154     ret = nativeClient_.GetUidOnIfaceRxBytes(uid, iface);
155     EXPECT_NE(ret, NetManagerStandard::NETMANAGER_SUCCESS);
156 
157     std::string filename = "";
158     ret = nativeClient_.GetIfaceBytes(iface, filename);
159     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
160 
161     ret = nativeClient_.GetIfaceRxBytes(iface);
162     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
163 
164     ret = nativeClient_.GetIfaceTxBytes(iface);
165     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
166 
167     ret = nativeClient_.GetIfaceRxPackets(iface);
168     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
169 }
170 
171 HWTEST_F(MockNetsysNativeClientTest, MockNetsysNativeClientBranchTest004, TestSize.Level1)
172 {
173     nativeClient_.Init();
174     nativeClient_.RegisterMockApi();
175 
176     std::string iface = "";
177     std::string filename = "";
178     int32_t ret = nativeClient_.GetIfaceTxPackets(iface);
179     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
180 
181     std::vector<std::string> ifList = {};
182     EXPECT_TRUE(nativeClient_.InterfaceGetList() != ifList);
183 
184     EXPECT_FALSE(nativeClient_.UidGetList() != ifList);
185 
186     ret = nativeClient_.AddRoute("", "", "", "");
187     EXPECT_NE(ret, NetManagerStandard::NETMANAGER_SUCCESS);
188 
189     int32_t netId = 0;
190     ret = nativeClient_.SetDefaultNetWork(netId);
191     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
192 
193     ret = nativeClient_.ClearDefaultNetWorkNetId();
194     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
195 
196     int32_t socketFd = 0;
197     ret = nativeClient_.BindSocket(socketFd, netId);
198     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
199 
200     uint16_t id = 0;
201     ret = nativeClient_.ShareDnsSet(id);
202     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
203 
204     NetsysNotifyCallback callback;
205     ret = nativeClient_.RegisterNetsysNotifyCallback(callback);
206     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
207 
208     ret = nativeClient_.BindNetworkServiceVpn(socketFd);
209     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
210 
211     struct ifreq ifRequest = {};
212     int32_t ifaceFd = 0;
213     ret = nativeClient_.EnableVirtualNetIfaceCard(socketFd, ifRequest, ifaceFd);
214     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
215 }
216 
217 HWTEST_F(MockNetsysNativeClientTest, MockNetsysNativeClientBranchTest005, TestSize.Level1)
218 {
219     nativeClient_.Init();
220     nativeClient_.RegisterMockApi();
221 
222     int32_t ifaceFd = 0;
223     std::string iface = "";
224     int32_t socketFd = 0;
225     struct ifreq ifRequest = {};
226     int32_t ret = nativeClient_.SetIpAddress(socketFd, "", ifaceFd, ifRequest);
227     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
228 
229     ret = nativeClient_.SetBlocking(ifaceFd, false);
230     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
231 
232     ret = nativeClient_.StartDhcpClient(iface, false);
233     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
234 
235     ret = nativeClient_.StopDhcpClient(iface, false);
236     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
237 
238     ret = nativeClient_.RegisterCallback(nullptr);
239     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
240 
241     ret = nativeClient_.StartDhcpService(iface, "");
242     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
243 
244     ret = nativeClient_.StopDhcpService(iface);
245     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
246 
247     std::string ipAddr = "";
248     uint32_t uid = 1000;
249     ret = nativeClient_.CloseSocketsUid(ipAddr, uid);
250     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
251 }
252 } // namespace NetManagerStandard
253 } // namespace OHOS
254