• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #undef private
25 #include "net_manager_constants.h"
26 #include "netnative_log_wrapper.h"
27 #include "singleton.h"
28 
29 namespace OHOS {
30 namespace NetsysNative {
31 using namespace testing::ext;
32 using namespace nmd;
33 namespace {
34 constexpr int32_t NETID_FIRST = 101;
35 constexpr int32_t NETID_SECOND = 102;
36 static constexpr const int32_t ERROR_CODE_SOCKETFD_INVALID = -1;
37 static constexpr const int32_t ERROR_CODE_CONNECT_FAILED = -2;
38 static constexpr const int32_t ERROR_CODE_SENDMSG_FAILED = -3;
39 static constexpr const int32_t ERROR_CODE_READ_FAILED = -4;
40 class ManagerNative : public std::enable_shared_from_this<ManagerNative> {
41     DECLARE_DELAYED_SINGLETON(ManagerNative);
42 
43 public:
44     std::shared_ptr<FwmarkClient> GetFwmarkClient();
45 
46 private:
47     std::shared_ptr<FwmarkClient> fwmarkClient_ = nullptr;
48 };
49 
ManagerNative()50 ManagerNative::ManagerNative()
51 {
52     fwmarkClient_ = std::make_shared<FwmarkClient>();
53 }
54 
GetFwmarkClient()55 std::shared_ptr<FwmarkClient> ManagerNative::GetFwmarkClient()
56 {
57     return fwmarkClient_;
58 }
59 
~ManagerNative()60 ManagerNative::~ManagerNative() {}
61 } // namespace
62 class UnitTestFwmarkClient : public testing::Test {
63 public:
64     std::shared_ptr<FwmarkClient> fwmarkClient = DelayedSingleton<ManagerNative>::GetInstance()->GetFwmarkClient();
65 };
66 
67 /**
68  * @tc.name: BindSocketTest001
69  * @tc.desc: Test FwmarkClient BindSocket.
70  * @tc.type: FUNC
71  */
72 HWTEST_F(UnitTestFwmarkClient, BindSocketTest001, TestSize.Level1)
73 {
74     int32_t udpSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
75     int32_t ret = fwmarkClient->BindSocket(udpSocket, NETID_FIRST);
76     NETNATIVE_LOGI("UnitTestFwmarkClient BindSocketTest001 ret=%{public}d", ret);
77     close(udpSocket);
78     udpSocket = -1;
79     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
80 }
81 
82 /**
83  * @tc.name: BindSocketTest002
84  * @tc.desc: Test FwmarkClient BindSocket.
85  * @tc.type: FUNC
86  */
87 HWTEST_F(UnitTestFwmarkClient, BindSocketTest002, TestSize.Level1)
88 {
89     int32_t tcpSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
90     int32_t ret = fwmarkClient->BindSocket(tcpSocket, NETID_SECOND);
91     NETNATIVE_LOGI("UnitTestFwmarkClient BindSocketTest002 ret=%{public}d", ret);
92     close(tcpSocket);
93     tcpSocket = -1;
94     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
95 }
96 
97 /**
98  * @tc.name: BindSocketTest003
99  * @tc.desc: Test FwmarkClient BindSocket.
100  * @tc.type: FUNC
101  */
102 HWTEST_F(UnitTestFwmarkClient, BindSocketTest003, TestSize.Level1)
103 {
104     int32_t tcpSocket = -1;
105     int32_t ret = fwmarkClient->BindSocket(tcpSocket, NETID_SECOND);
106     NETNATIVE_LOGI("UnitTestFwmarkClient BindSocketTest002 ret=%{public}d", ret);
107     close(tcpSocket);
108     tcpSocket = -1;
109     EXPECT_EQ(ret, -1);
110 }
111 
112 /**
113  * @tc.name: HandleErrorTest
114  * @tc.desc: Test FwmarkClient BindSocket.
115  * @tc.type: FUNC
116  */
117 HWTEST_F(UnitTestFwmarkClient, HandleErrorTest, TestSize.Level1)
118 {
119     int32_t ret = -1;
120     int32_t errorCode = ERROR_CODE_SOCKETFD_INVALID;
121     ret = fwmarkClient->HandleError(ret, errorCode);
122     EXPECT_EQ(ret, -1);
123 
124     errorCode = ERROR_CODE_CONNECT_FAILED;
125     ret = fwmarkClient->HandleError(ret, errorCode);
126     EXPECT_EQ(ret, -1);
127 
128     errorCode = ERROR_CODE_SENDMSG_FAILED;
129     ret = fwmarkClient->HandleError(ret, errorCode);
130     EXPECT_EQ(ret, -1);
131 
132     errorCode = ERROR_CODE_READ_FAILED;
133     ret = fwmarkClient->HandleError(ret, errorCode);
134     EXPECT_EQ(ret, -1);
135 
136     errorCode = 100;
137     ret = fwmarkClient->HandleError(ret, errorCode);
138     EXPECT_EQ(ret, -1);
139 }
140 } // namespace NetsysNative
141 } // namespace OHOS
142