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 <arpa/inet.h>
17 #include <cstdio>
18 #include <gtest/gtest.h>
19 #include <sys/socket.h>
20 #include <sys/types.h>
21
22 #define private public
23 #include "fwmark_client.h"
24 #include "fwmark_network.cpp"
25 #undef private
26 #include "net_manager_constants.h"
27 #include "netnative_log_wrapper.h"
28 #include "singleton.h"
29
30 namespace OHOS {
31 namespace NetsysNative {
32 using namespace testing::ext;
33 using namespace nmd;
34 namespace {
35 constexpr int32_t NETID_FIRST = 101;
36 constexpr int32_t NETID_SECOND = 102;
37 static constexpr const int32_t ERROR_CODE_SOCKETFD_INVALID = -1;
38 static constexpr const int32_t ERROR_CODE_CONNECT_FAILED = -2;
39 static constexpr const int32_t ERROR_CODE_SENDMSG_FAILED = -3;
40 static constexpr const int32_t ERROR_CODE_READ_FAILED = -4;
41 class ManagerNative : public std::enable_shared_from_this<ManagerNative> {
42 DECLARE_DELAYED_SINGLETON(ManagerNative);
43
44 public:
45 std::shared_ptr<FwmarkClient> GetFwmarkClient();
46
47 private:
48 std::shared_ptr<FwmarkClient> fwmarkClient_ = nullptr;
49 };
50
ManagerNative()51 ManagerNative::ManagerNative()
52 {
53 fwmarkClient_ = std::make_shared<FwmarkClient>();
54 }
55
GetFwmarkClient()56 std::shared_ptr<FwmarkClient> ManagerNative::GetFwmarkClient()
57 {
58 return fwmarkClient_;
59 }
60
~ManagerNative()61 ManagerNative::~ManagerNative() {}
62 } // namespace
63 class UnitTestFwmarkClient : public testing::Test {
64 public:
65 static void SetUpTestCase();
66 static void TearDownTestCase();
67 void SetUp();
68 void TearDown();
69 std::shared_ptr<FwmarkClient> fwmarkClient = DelayedSingleton<ManagerNative>::GetInstance()->GetFwmarkClient();
70 };
71
SetUpTestCase()72 void UnitTestFwmarkClient::SetUpTestCase() {}
73
TearDownTestCase()74 void UnitTestFwmarkClient::TearDownTestCase() {}
75
SetUp()76 void UnitTestFwmarkClient::SetUp() {}
77
TearDown()78 void UnitTestFwmarkClient::TearDown() {}
79
80 /**
81 * @tc.name: BindSocketTest001
82 * @tc.desc: Test FwmarkClient BindSocket.
83 * @tc.type: FUNC
84 */
85 HWTEST_F(UnitTestFwmarkClient, BindSocketTest001, TestSize.Level1)
86 {
87 int32_t udpSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
88 int32_t ret = fwmarkClient->BindSocket(udpSocket, NETID_FIRST);
89 NETNATIVE_LOGI("UnitTestFwmarkClient BindSocketTest001 ret=%{public}d", ret);
90 close(udpSocket);
91 udpSocket = -1;
92 EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
93 }
94
95 /**
96 * @tc.name: BindSocketTest002
97 * @tc.desc: Test FwmarkClient BindSocket.
98 * @tc.type: FUNC
99 */
100 HWTEST_F(UnitTestFwmarkClient, BindSocketTest002, TestSize.Level1)
101 {
102 int32_t tcpSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
103 int32_t ret = fwmarkClient->BindSocket(tcpSocket, NETID_SECOND);
104 NETNATIVE_LOGI("UnitTestFwmarkClient BindSocketTest002 ret=%{public}d", ret);
105 close(tcpSocket);
106 tcpSocket = -1;
107 EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
108 }
109
110 /**
111 * @tc.name: BindSocketTest003
112 * @tc.desc: Test FwmarkClient BindSocket.
113 * @tc.type: FUNC
114 */
115 HWTEST_F(UnitTestFwmarkClient, BindSocketTest003, TestSize.Level1)
116 {
117 int32_t tcpSocket = -1;
118 int32_t ret = fwmarkClient->BindSocket(tcpSocket, NETID_SECOND);
119 NETNATIVE_LOGI("UnitTestFwmarkClient BindSocketTest002 ret=%{public}d", ret);
120 close(tcpSocket);
121 tcpSocket = -1;
122 EXPECT_EQ(ret, -1);
123 }
124
125 /**
126 * @tc.name: HandleErrorTest
127 * @tc.desc: Test FwmarkClient BindSocket.
128 * @tc.type: FUNC
129 */
130 HWTEST_F(UnitTestFwmarkClient, HandleErrorTest, TestSize.Level1)
131 {
132 int32_t ret = -1;
133 int32_t sock = -1;
134 int32_t errorCode = ERROR_CODE_SOCKETFD_INVALID;
135 ret = fwmarkClient->HandleError(ret, errorCode, sock);
136 EXPECT_EQ(ret, -1);
137
138 errorCode = ERROR_CODE_CONNECT_FAILED;
139 ret = fwmarkClient->HandleError(ret, errorCode, sock);
140 EXPECT_EQ(ret, -1);
141
142 errorCode = ERROR_CODE_SENDMSG_FAILED;
143 ret = fwmarkClient->HandleError(ret, errorCode, sock);
144 EXPECT_EQ(ret, -1);
145
146 errorCode = ERROR_CODE_READ_FAILED;
147 ret = fwmarkClient->HandleError(ret, errorCode, sock);
148 EXPECT_EQ(ret, -1);
149
150 errorCode = 100;
151 ret = fwmarkClient->HandleError(ret, errorCode, sock);
152 EXPECT_EQ(ret, -1);
153 }
154
155 /**
156 * @tc.name: CloseSocketTest001
157 * @tc.desc: Test FwmarkNetwork CloseSocket.
158 * @tc.type: FUNC
159 */
160 HWTEST_F(UnitTestFwmarkClient, CloseSocketTest001, TestSize.Level1)
161 {
162 int32_t socket = 32;
163 int32_t ret = -1;
164 CloseSocket(nullptr, ret, NO_ERROR_CODE);
165 CloseSocket(&socket, ret, ERROR_CODE_RECVMSG_FAILED);
166 CloseSocket(&socket, ret, ERROR_CODE_SOCKETFD_INVALID);
167 CloseSocket(&socket, ret, ERROR_CODE_WRITE_FAILED);
168 CloseSocket(&socket, ret, ERROR_CODE_GETSOCKOPT_FAILED);
169 CloseSocket(&socket, ret, ERROR_CODE_SETSOCKOPT_FAILED);
170 CloseSocket(&socket, ret, ERROR_CODE_SETSOCKOPT_FAILED - 1);
171 EXPECT_EQ(socket, -1);
172 }
173
174 /**
175 * @tc.name: SetMarkTest001
176 * @tc.desc: Test FwmarkNetwork SetMark.
177 * @tc.type: FUNC
178 */
179 HWTEST_F(UnitTestFwmarkClient, SetMarkTest001, TestSize.Level1)
180 {
181 FwmarkCommand cmd;
182 auto ret = SetMark(nullptr, &cmd);
183 EXPECT_EQ(ret, -1);
184 }
185
186 /**
187 * @tc.name: SetMarkTest002
188 * @tc.desc: Test FwmarkNetwork SetMark.
189 * @tc.type: FUNC
190 */
191 HWTEST_F(UnitTestFwmarkClient, SetMarkTest002, TestSize.Level1)
192 {
193 int32_t socketFd = 0;
194 auto ret = SetMark(&socketFd, nullptr);
195 EXPECT_EQ(ret, -1);
196 }
197
198 /**
199 * @tc.name: SetMarkTest003
200 * @tc.desc: Test FwmarkNetwork SetMark.
201 * @tc.type: FUNC
202 */
203 HWTEST_F(UnitTestFwmarkClient, SetMarkTest003, TestSize.Level1)
204 {
205 int32_t socketFd = 1111;
206 FwmarkCommand cmd;
207 auto ret = SetMark(&socketFd, &cmd);
208 EXPECT_NE(ret, 0);
209 EXPECT_EQ(socketFd, -1);
210 }
211
212 /**
213 * @tc.name: SetMarkTest004
214 * @tc.desc: Test FwmarkNetwork SetMark.
215 * @tc.type: FUNC
216 */
217 HWTEST_F(UnitTestFwmarkClient, SetMarkTest004, TestSize.Level1)
218 {
219 FwmarkCommand cmd;
220 int32_t tcpSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
221 int32_t ret = fwmarkClient->BindSocket(tcpSocket, NETID_SECOND);
222 ASSERT_EQ(ret, 0);
223 cmd.cmdId = FwmarkCommand::SELECT_NETWORK;
224 cmd.netId = NETID_UNSET;
225 ret = SetMark(&tcpSocket, &cmd);
226 close(tcpSocket);
227 tcpSocket = -1;
228 EXPECT_EQ(ret, 0);
229 }
230
231 /**
232 * @tc.name: SetMarkTest005
233 * @tc.desc: Test FwmarkNetwork SetMark.
234 * @tc.type: FUNC
235 */
236 HWTEST_F(UnitTestFwmarkClient, SetMarkTest005, TestSize.Level1)
237 {
238 FwmarkCommand cmd;
239 int32_t tcpSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
240 int32_t ret = fwmarkClient->BindSocket(tcpSocket, NETID_SECOND);
241 ASSERT_EQ(ret, 0);
242 cmd.cmdId = FwmarkCommand::SELECT_NETWORK;
243 cmd.netId = 100;
244 ret = SetMark(&tcpSocket, &cmd);
245 close(tcpSocket);
246 tcpSocket = -1;
247 EXPECT_EQ(ret, 0);
248 }
249
250 /**
251 * @tc.name: SetMarkTest006
252 * @tc.desc: Test FwmarkNetwork SetMark.
253 * @tc.type: FUNC
254 */
255 HWTEST_F(UnitTestFwmarkClient, SetMarkTest006, TestSize.Level1)
256 {
257 FwmarkCommand cmd;
258 int32_t tcpSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
259 int32_t ret = fwmarkClient->BindSocket(tcpSocket, NETID_SECOND);
260 ASSERT_EQ(ret, 0);
261 cmd.cmdId = FwmarkCommand::PROTECT_FROM_VPN;
262 cmd.netId = 100;
263 ret = SetMark(&tcpSocket, &cmd);
264 close(tcpSocket);
265 tcpSocket = -1;
266 EXPECT_EQ(ret, 0);
267 }
268
269 /**
270 * @tc.name: SetMarkTest008
271 * @tc.desc: Test FwmarkNetwork ProtectFromVpn.
272 * @tc.type: FUNC
273 */
274 HWTEST_F(UnitTestFwmarkClient, SetMarkTest008, TestSize.Level1)
275 {
276 int32_t socketFd = 1111;
277 auto ret = fwmarkClient->ProtectFromVpn(socketFd);
278 EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_ERROR);
279 }
280
281 /**
282 * @tc.name: SetMarkTest009
283 * @tc.desc: Test FwmarkNetwork ProtectFromVpn.
284 * @tc.type: FUNC
285 */
286 HWTEST_F(UnitTestFwmarkClient, SetMarkTest009, TestSize.Level1)
287 {
288 int32_t socketFd = -1;
289 auto ret = fwmarkClient->ProtectFromVpn(socketFd);
290 EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_ERROR);
291 }
292
293 /**
294 * @tc.name: CloseSocketTest002
295 * @tc.desc: Test FwmarkNetwork CloseSocket.
296 * @tc.type: FUNC
297 */
298 HWTEST_F(UnitTestFwmarkClient, CloseSocketTest002, TestSize.Level1)
299 {
300 int32_t socket = 32;
301 int32_t ret = -1;
302 CloseSocket(&socket, ret, ERROR_CODE_SOCKETFD_INVALID);
303 EXPECT_EQ(socket, -1);
304 }
305
306 /**
307 * @tc.name: RunForClientFdTest001
308 * @tc.desc: Test FwmarkNetwork RunForClientFd.
309 * @tc.type: FUNC
310 */
311 HWTEST_F(UnitTestFwmarkClient, RunForClientFdTest001, TestSize.Level1)
312 {
313 int32_t socket = 32;
314 RunForClientFd(socket);
315 EXPECT_TRUE(socket == -1 || socket == 32);
316 }
317 } // namespace NetsysNative
318 } // namespace OHOS
319