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 #include <sys/socket.h>
18
19 #include "accesstoken_kit.h"
20 #include "extended_vpn_ctl.h"
21 #include "nativetoken_kit.h"
22 #include "token_setproc.h"
23 #include "vpn_config.h"
24
25 #ifdef GTEST_API_
26 #define private public
27 #define protected public
28 #endif
29
30 #include "net_manager_constants.h"
31 #include "networkvpn_service.h"
32 #include "vpn_event_callback_stub.h"
33 #include "system_ability_definition.h"
34 #include "inet_addr.h"
35
36 namespace OHOS {
37 namespace NetManagerStandard {
38 namespace {
39 using namespace testing::ext;
40
41 } // namespace
42
43 class NetworkVpnServiceExtTest : public testing::Test {
44 public:
45 static void SetUpTestCase();
46 static void TearDownTestCase();
47 void SetUp();
48 void TearDown();
49 static inline auto instance_ = DelayedSingleton<NetworkVpnService>::GetInstance();
50 };
51
SetUpTestCase()52 void NetworkVpnServiceExtTest::SetUpTestCase()
53 {
54 instance_->OnStart();
55 }
56
TearDownTestCase()57 void NetworkVpnServiceExtTest::TearDownTestCase()
58 {
59 instance_->OnStop();
60 }
61
SetUp()62 void NetworkVpnServiceExtTest::SetUp() {}
63
TearDown()64 void NetworkVpnServiceExtTest::TearDown() {}
65
66
GetRouteInfo()67 Route GetRouteInfo()
68 {
69 std::string iface("eth0");
70 Route route;
71 route.iface_ = iface;
72 route.rtnType_ = RTN_UNICAST;
73 route.hasGateway_ = true;
74 route.isDefaultRoute_ = false;
75 route.destination_.type_ = INetAddr::IPV4;
76 route.destination_.family_ = AF_INET;
77 route.destination_.prefixlen_ = 0x18;
78 route.destination_.address_ = "192.168.2.10";
79 route.destination_.netMask_ = "255.255.255.0";
80 route.destination_.hostName_ = "netAddr";
81 route.gateway_.type_ = INetAddr::IPV4;
82 route.gateway_.family_ = AF_INET;
83 route.gateway_.prefixlen_ = 0x18;
84 route.gateway_.address_ = "192.168.2.1";
85 route.gateway_.netMask_ = "255.255.255.0";
86 route.gateway_.hostName_ = "netAddr";
87 return route;
88 }
89
90
91 HWTEST_F(NetworkVpnServiceExtTest, ConvertNetAddrToConfigTest001, TestSize.Level1)
92 {
93 INetAddr sourAddr;
94 INetAddr destAddr;
95 sourAddr.type_ = INetAddr::IPV4;
96 sourAddr.family_ = AF_INET;
97 sourAddr.prefixlen_ = 0x18;
98 sourAddr.address_ = "192.168.2.10";
99 sourAddr.netMask_ = "255.255.255.0";
100 sourAddr.hostName_ = "netAddr";
101 sourAddr.port_ = 80;
102 cJSON *json = cJSON_CreateObject();
103 if (json != nullptr) {
104 instance_->ConvertNetAddrToJson(sourAddr, json);
105 instance_->ConvertNetAddrToConfig(destAddr, json);
106 cJSON_Delete(json);
107 json = nullptr;
108 EXPECT_EQ(destAddr.family_, AF_INET);
109 }
110 }
111
112 HWTEST_F(NetworkVpnServiceExtTest, ConvertNetAddrToConfigTest002, TestSize.Level1)
113 {
114 INetAddr destAddr;
115 cJSON *json = cJSON_CreateObject();
116 if (json != nullptr) {
117 instance_->ConvertNetAddrToConfig(destAddr, json);
118 cJSON_Delete(json);
119 json = nullptr;
120 }
121
122 EXPECT_EQ(destAddr.port_, 0);
123 }
124
125 HWTEST_F(NetworkVpnServiceExtTest, ConvertNetAddrToConfigTest003, TestSize.Level1)
126 {
127 INetAddr destAddr;
128 cJSON *json = cJSON_CreateObject();
129 if (json != nullptr) {
130 cJSON_AddItemToObject(json, "type", cJSON_CreateString("ss"));
131 cJSON_AddItemToObject(json, "family", cJSON_CreateString("ss"));
132 cJSON_AddItemToObject(json, "prefixlen", cJSON_CreateString("ss"));
133 cJSON_AddItemToObject(json, "port", cJSON_CreateString("ss"));
134 cJSON_AddItemToObject(json, "address", cJSON_CreateNumber(1));
135 cJSON_AddItemToObject(json, "netMask", cJSON_CreateNumber(1));
136 cJSON_AddItemToObject(json, "hostName", cJSON_CreateNumber(1));
137 instance_->ConvertNetAddrToConfig(destAddr, json);
138 cJSON_Delete(json);
139 json = nullptr;
140 }
141
142 EXPECT_EQ(destAddr.port_, 0);
143 }
144
145 HWTEST_F(NetworkVpnServiceExtTest, ConvertRouteToConfig001, TestSize.Level1)
146 {
147 Route destRoute;
148 Route mem = GetRouteInfo();
149 cJSON *json = cJSON_CreateObject();
150 if (json != nullptr) {
151 cJSON_AddItemToObject(json, "iface", cJSON_CreateString(mem.iface_.c_str()));
152 cJSON *jDestination = cJSON_CreateObject();
153 instance_->ConvertNetAddrToJson(mem.destination_, jDestination);
154 cJSON_AddItemToObject(json, "destination", jDestination);
155 cJSON *jGateway = cJSON_CreateObject();
156 instance_->ConvertNetAddrToJson(mem.gateway_, jGateway);
157 cJSON_AddItemToObject(json, "gateway", jGateway);
158 cJSON_AddItemToObject(json, "rtnType", cJSON_CreateNumber(mem.rtnType_));
159 cJSON_AddItemToObject(json, "mtu", cJSON_CreateNumber(mem.mtu_));
160 cJSON_AddItemToObject(json, "isHost", cJSON_CreateBool(mem.isHost_));
161 cJSON_AddItemToObject(json, "hasGateway", cJSON_CreateBool(mem.hasGateway_));
162 cJSON_AddItemToObject(json, "isDefaultRoute", cJSON_CreateBool(mem.isDefaultRoute_));
163 instance_->ConvertRouteToConfig(destRoute, json);
164 cJSON_Delete(json);
165 json = nullptr;
166 EXPECT_STREQ(destRoute.iface_.c_str(), "eth0");
167 }
168 }
169 HWTEST_F(NetworkVpnServiceExtTest, ConvertRouteToConfig002, TestSize.Level1)
170 {
171 Route destRoute;
172 cJSON *json = cJSON_CreateObject();
173 if (json != nullptr) {
174 instance_->ConvertRouteToConfig(destRoute, json);
175 cJSON_Delete(json);
176 json = nullptr;
177 }
178 EXPECT_EQ(destRoute.rtnType_, RTN_UNICAST);
179 }
180
181 HWTEST_F(NetworkVpnServiceExtTest, ConvertRouteToConfig003, TestSize.Level1)
182 {
183 Route destRoute;
184 cJSON *json = cJSON_CreateObject();
185 if (json != nullptr) {
186 cJSON_AddItemToObject(json, "iface", cJSON_CreateNumber(1));
187 cJSON_AddItemToObject(json, "destination", cJSON_CreateString("s"));
188 cJSON_AddItemToObject(json, "gateway", cJSON_CreateString("s"));
189 cJSON_AddItemToObject(json, "rtnType", cJSON_CreateString("s"));
190 cJSON_AddItemToObject(json, "mtu", cJSON_CreateString("s"));
191 cJSON_AddItemToObject(json, "isHost", cJSON_CreateString("s"));
192 cJSON_AddItemToObject(json, "hasGateway", cJSON_CreateString("s"));
193 cJSON_AddItemToObject(json, "isDefaultRoute", cJSON_CreateString("s"));
194 instance_->ConvertRouteToConfig(destRoute, json);
195 cJSON_Delete(json);
196 json = nullptr;
197 }
198 EXPECT_EQ(destRoute.rtnType_, RTN_UNICAST);
199 }
200
201 HWTEST_F(NetworkVpnServiceExtTest, ParseJsonToConfig001, TestSize.Level1)
202 {
203 std::string josnStr;
204 sptr<VpnConfig> vpnCfg = new VpnConfig();
205 cJSON *json = cJSON_CreateObject();
206 if (json != nullptr) {
207 cJSON_AddItemToObject(json, "test", cJSON_CreateNumber(1));
208 char *str = cJSON_Print(json);
209 if (str != nullptr) {
210 josnStr = str;
211 free(str);
212 str = nullptr;
213 instance_->ParseJsonToConfig(vpnCfg, josnStr);
214 }
215 cJSON_Delete(json);
216 json = nullptr;
217 }
218 EXPECT_EQ(vpnCfg->mtu_, 0);
219 }
220
221 HWTEST_F(NetworkVpnServiceExtTest, ParseJsonToConfig002, TestSize.Level1)
222 {
223 std::string josnStr;
224 sptr<VpnConfig> vpnCfg = new VpnConfig();
225 cJSON *json = cJSON_CreateObject();
226 if (json != nullptr) {
227 cJSON_AddItemToObject(json, "mtu", cJSON_CreateString("s"));
228 cJSON_AddItemToObject(json, "isAcceptIPv4", cJSON_CreateString("s"));
229 cJSON_AddItemToObject(json, "isAcceptIPv6", cJSON_CreateString("s"));
230 cJSON_AddItemToObject(json, "isLegacy", cJSON_CreateString("s"));
231 cJSON_AddItemToObject(json, "isMetered", cJSON_CreateString("s"));
232 cJSON_AddItemToObject(json, "isBlocking", cJSON_CreateString("s"));
233 char *str = cJSON_Print(json);
234 if (str != nullptr) {
235 josnStr = str;
236 free(str);
237 str = nullptr;
238 instance_->ParseJsonToConfig(vpnCfg, josnStr);
239 }
240 cJSON_Delete(json);
241 json = nullptr;
242 }
243 EXPECT_EQ(vpnCfg->mtu_, 0);
244 }
245
246 HWTEST_F(NetworkVpnServiceExtTest, ParseJsonToConfig003, TestSize.Level1)
247 {
248 std::string josnStr = "";
249 sptr<VpnConfig> vpnCfg = new VpnConfig();
250 instance_->ParseJsonToConfig(vpnCfg, josnStr);
251 EXPECT_EQ(vpnCfg->mtu_, 0);
252 }
253 } // namespace NetManagerStandard
254 } // namespace OHOS
255