• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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);
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_.NetworkRemoveRoute(netId, iface, "", "");
71     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
72 
73     ret = nativeClient_.SetInterfaceDown(iface);
74     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
75 
76     ret = nativeClient_.SetInterfaceUp(iface);
77     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
78 
79     nativeClient_.ClearInterfaceAddrs(iface);
80 
81     ret = nativeClient_.GetInterfaceMtu(iface);
82     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
83 
84     ret = nativeClient_.SetInterfaceMtu(iface, 0);
85     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
86 
87     ret = nativeClient_.AddInterfaceAddress(iface, "", 0);
88     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
89 
90     ret = nativeClient_.DelInterfaceAddress(iface, "", 0);
91     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
92 }
93 
94 HWTEST_F(MockNetsysNativeClientTest, MockNetsysNativeClientBranchTest002, TestSize.Level1)
95 {
96     nativeClient_.Init();
97     nativeClient_.RegisterMockApi();
98 
99     int32_t netId = 0;
100     std::string iface = "";
101     std::vector<std::string> testInput = {};
102     int32_t ret = nativeClient_.SetResolverConfig(netId, 0, 0, testInput, testInput);
103     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
104 
105     uint16_t baseTimeoutMsec = 0;
106     uint8_t retryCount = 0;
107     ret = nativeClient_.GetResolverConfig(netId, testInput, testInput, baseTimeoutMsec, retryCount);
108     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
109 
110     ret = nativeClient_.CreateNetworkCache(netId);
111     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
112 
113     ret = nativeClient_.DestroyNetworkCache(netId);
114     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
115 
116     ret = nativeClient_.GetCellularRxBytes();
117     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
118 
119     ret = nativeClient_.GetCellularTxBytes();
120     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
121 
122     ret = nativeClient_.GetAllBytes(iface);
123     EXPECT_NE(ret, NetManagerStandard::NETMANAGER_SUCCESS);
124 
125     ret = nativeClient_.GetAllRxBytes();
126     EXPECT_GE(ret, NetManagerStandard::NETMANAGER_SUCCESS);
127 
128     ret = nativeClient_.GetAllTxBytes();
129     EXPECT_GE(ret, NetManagerStandard::NETMANAGER_SUCCESS);
130 }
131 
132 HWTEST_F(MockNetsysNativeClientTest, MockNetsysNativeClientBranchTest003, TestSize.Level1)
133 {
134     nativeClient_.Init();
135     nativeClient_.RegisterMockApi();
136 
137     uint32_t uid = 0;
138     int32_t ret = nativeClient_.GetUidTxBytes(uid);
139     EXPECT_NE(ret, NetManagerStandard::NETMANAGER_SUCCESS);
140 
141     ret = nativeClient_.GetUidRxBytes(uid);
142     EXPECT_NE(ret, NetManagerStandard::NETMANAGER_SUCCESS);
143 
144     std::string iface = "";
145     ret = nativeClient_.GetUidOnIfaceTxBytes(uid, iface);
146     EXPECT_NE(ret, NetManagerStandard::NETMANAGER_SUCCESS);
147 
148     ret = nativeClient_.GetUidOnIfaceRxBytes(uid, iface);
149     EXPECT_NE(ret, NetManagerStandard::NETMANAGER_SUCCESS);
150 
151     std::string filename = "";
152     ret = nativeClient_.GetIfaceBytes(iface, filename);
153     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
154 
155     ret = nativeClient_.GetIfaceRxBytes(iface);
156     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
157 
158     ret = nativeClient_.GetIfaceTxBytes(iface);
159     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
160 
161     ret = nativeClient_.GetIfaceRxPackets(iface);
162     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
163 }
164 
165 HWTEST_F(MockNetsysNativeClientTest, MockNetsysNativeClientBranchTest004, TestSize.Level1)
166 {
167     nativeClient_.Init();
168     nativeClient_.RegisterMockApi();
169 
170     std::string iface = "";
171     std::string filename = "";
172     int32_t ret = nativeClient_.GetIfaceTxPackets(iface);
173     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
174 
175     std::vector<std::string> ifList = {};
176     EXPECT_TRUE(nativeClient_.InterfaceGetList() != ifList);
177 
178     EXPECT_FALSE(nativeClient_.UidGetList() != ifList);
179 
180     ret = nativeClient_.AddRoute("", "", "", "");
181     EXPECT_NE(ret, NetManagerStandard::NETMANAGER_SUCCESS);
182 
183     int32_t netId = 0;
184     ret = nativeClient_.SetDefaultNetWork(netId);
185     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
186 
187     ret = nativeClient_.ClearDefaultNetWorkNetId();
188     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
189 
190     int32_t socketFd = 0;
191     ret = nativeClient_.BindSocket(socketFd, netId);
192     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
193 
194     uint16_t id = 0;
195     ret = nativeClient_.ShareDnsSet(id);
196     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
197 
198     NetsysNotifyCallback callback;
199     ret = nativeClient_.RegisterNetsysNotifyCallback(callback);
200     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
201 
202     ret = nativeClient_.BindNetworkServiceVpn(socketFd);
203     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
204 
205     struct ifreq ifRequest = {};
206     int32_t ifaceFd = 0;
207     ret = nativeClient_.EnableVirtualNetIfaceCard(socketFd, ifRequest, ifaceFd);
208     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
209 }
210 
211 HWTEST_F(MockNetsysNativeClientTest, MockNetsysNativeClientBranchTest005, TestSize.Level1)
212 {
213     nativeClient_.Init();
214     nativeClient_.RegisterMockApi();
215 
216     int32_t ifaceFd = 0;
217     std::string iface = "";
218     int32_t socketFd = 0;
219     struct ifreq ifRequest = {};
220     int32_t ret = nativeClient_.SetIpAddress(socketFd, "", ifaceFd, ifRequest);
221     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
222 
223     ret = nativeClient_.SetBlocking(ifaceFd, false);
224     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
225 
226     ret = nativeClient_.StartDhcpClient(iface, false);
227     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
228 
229     ret = nativeClient_.StopDhcpClient(iface, false);
230     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
231 
232     ret = nativeClient_.RegisterCallback(nullptr);
233     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
234 
235     ret = nativeClient_.StartDhcpService(iface, "");
236     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
237 
238     ret = nativeClient_.StopDhcpService(iface);
239     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
240 }
241 } // namespace NetManagerStandard
242 } // namespace OHOS
243