2 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
25 #include <gio/gunixfdlist.h>
39 error_e Vpnsvc::vpn_init(const char *iface_name, int iface_name_len, int *result,
40 int *h_index, char *h_name)
42 GVariant *message = NULL;
43 error_e error = ERROR_NONE;
44 GError *g_error = NULL;
45 GUnixFDList *fd_list = NULL;
48 fd_list = g_unix_fd_list_new();
49 if (fd_list == NULL) {
50 GLOGD("g_unix_fd_list_new() failed!");
51 return ERROR_OPERATION_FAILED ;
54 if ((iface_fd = open("/dev/net/tun", O_RDWR)) < 0) {
55 GLOGD("tun device open fail\n");
56 return ERROR_OPERATION_FAILED;
59 g_unix_fd_list_append(fd_list, iface_fd, &g_error);
60 if (g_error != NULL) {
62 return ERROR_OPERATION_FAILED;
65 message = InvokeMethod_with_fd(fd_list,
67 NETCONFIG_VPNSVC_PATH,
68 NETCONFIG_VPNSVC_INTERFACE,
70 g_variant_new("(su)", iface_name, iface_name_len),
73 if (message == NULL) {
74 GLOGD("Failed to invoke dbus method");
79 g_variant_get(message, "(iis)", result, h_index, h_name);
80 g_variant_unref(message);
86 error_e Vpnsvc::vpn_deinit(const char *dev_name, int *result)
88 GVariant *message = NULL;
89 error_e error = ERROR_NONE;
91 message = InvokeMethod(NETCONFIG_SERVICE,
92 NETCONFIG_VPNSVC_PATH,
93 NETCONFIG_VPNSVC_INTERFACE,
95 g_variant_new("(s)", dev_name),
98 if (message == NULL) {
99 GLOGD("Failed to invoke dbus method");
103 g_variant_get(message, "(i)", result);
104 g_variant_unref(message);
109 error_e Vpnsvc::vpn_protect(const char *dev_name, int *result)
111 GVariant *message = NULL;
112 error_e error = ERROR_NONE;
113 GError *g_error = NULL;
114 GUnixFDList *fd_list = NULL;
117 fd_list = g_unix_fd_list_new();
118 if (fd_list == NULL) {
119 GLOGD("g_unix_fd_list_new() failed!");
120 return ERROR_OPERATION_FAILED ;
123 if ((iface_fd = open("/dev/net/tun", O_RDWR)) < 0) {
124 GLOGD("tun device open fail\n");
125 return ERROR_OPERATION_FAILED;
128 g_unix_fd_list_append(fd_list, iface_fd, &g_error);
129 if (g_error != NULL) {
131 return ERROR_OPERATION_FAILED;
134 message = InvokeMethod_with_fd(fd_list,
136 NETCONFIG_VPNSVC_PATH,
137 NETCONFIG_VPNSVC_INTERFACE,
139 g_variant_new("(s)", dev_name),
142 if (message == NULL) {
143 GLOGD("Failed to invoke dbus method");
148 g_variant_get(message, "(i)", result);
149 g_variant_unref(message);
155 error_e Vpnsvc::vpn_up(const char *iface_name, int *result)
157 GVariant *message = NULL;
158 error_e error = ERROR_NONE;
160 message = InvokeMethod(NETCONFIG_SERVICE,
161 NETCONFIG_VPNSVC_PATH,
162 NETCONFIG_VPNSVC_INTERFACE,
164 g_variant_new("(s)", iface_name),
167 if (message == NULL) {
168 GLOGD("Failed to invoke dbus method");
172 g_variant_get(message, "(i)", result);
173 g_variant_unref(message);
178 error_e Vpnsvc::vpn_down(const char *iface_name, int *result)
180 GVariant *message = NULL;
181 error_e error = ERROR_NONE;
183 message = InvokeMethod(NETCONFIG_SERVICE,
184 NETCONFIG_VPNSVC_PATH,
185 NETCONFIG_VPNSVC_INTERFACE,
187 g_variant_new("(s)", iface_name),
190 if (message == NULL) {
191 GLOGD("Failed to invoke dbus method");
195 g_variant_get(message, "(i)", result);
196 g_variant_unref(message);
201 error_e Vpnsvc::vpn_block_networks(GVariant *nets_vpn, int nr_nets_vpn,
202 GVariant *nets_orig, int nr_nets_orig, int *result)
204 GVariant *message = NULL;
205 error_e error = ERROR_NONE;
207 message = InvokeMethod(NETCONFIG_SERVICE,
208 NETCONFIG_VPNSVC_PATH,
209 NETCONFIG_VPNSVC_INTERFACE,
211 g_variant_new("(vuvu)", nets_vpn, nr_nets_vpn,
212 nets_orig, nr_nets_orig),
215 if (message == NULL) {
216 GLOGD("Failed to invoke dbus method");
220 g_variant_get(message, "(i)", result);
221 g_variant_unref(message);
226 error_e Vpnsvc::vpn_unblock_networks(int *result)
228 GVariant *message = NULL;
229 error_e error = ERROR_NONE;
231 message = InvokeMethod(NETCONFIG_SERVICE,
232 NETCONFIG_VPNSVC_PATH,
233 NETCONFIG_VPNSVC_INTERFACE,
234 VPN_UNBLOCK_NETWORKS,
238 if (message == NULL) {
239 GLOGD("Failed to invoke dbus method");
243 g_variant_get(message, "(i)", result);
244 g_variant_unref(message);
249 error_e Vpnsvc::vpn_update_settings(int iface_index, const char *local_ip,
250 const char *remote_ip, int mtu, int *result)
252 GVariant *message = NULL;
253 error_e error = ERROR_NONE;
255 message = InvokeMethod(NETCONFIG_SERVICE,
256 NETCONFIG_VPNSVC_PATH,
257 NETCONFIG_VPNSVC_INTERFACE,
259 g_variant_new("(issu)", iface_index, local_ip,
263 if (message == NULL) {
264 GLOGD("Failed to invoke dbus method");
268 g_variant_get(message, "(i)", result);
269 g_variant_unref(message);
274 error_e Vpnsvc::vpn_add_route(const char *iface_name, const char *route, int prefix,
277 GVariant *message = NULL;
278 error_e error = ERROR_NONE;
280 message = InvokeMethod(NETCONFIG_SERVICE,
281 NETCONFIG_VPNSVC_PATH,
282 NETCONFIG_VPNSVC_INTERFACE,
284 g_variant_new("(ssi)", iface_name, route, prefix),
287 if (message == NULL) {
288 GLOGD("Failed to invoke dbus method");
292 g_variant_get(message, "(i)", result);
293 g_variant_unref(message);
298 error_e Vpnsvc::vpn_remove_route(const char *iface_name, const char *route, int prefix,
301 GVariant *message = NULL;
302 error_e error = ERROR_NONE;
304 message = InvokeMethod(NETCONFIG_SERVICE,
305 NETCONFIG_VPNSVC_PATH,
306 NETCONFIG_VPNSVC_INTERFACE,
308 g_variant_new("(ssi)", iface_name, route, prefix),
311 if (message == NULL) {
312 GLOGD("Failed to invoke dbus method");
316 g_variant_get(message, "(i)", result);
317 g_variant_unref(message);
322 error_e Vpnsvc::vpn_add_dns_server(const char* iface_name, const char *dns_server,
325 GVariant *message = NULL;
326 error_e error = ERROR_NONE;
328 message = InvokeMethod(NETCONFIG_SERVICE,
329 NETCONFIG_VPNSVC_PATH,
330 NETCONFIG_VPNSVC_INTERFACE,
332 g_variant_new("(ss)", iface_name, dns_server),
335 if (message == NULL) {
336 GLOGD("Failed to invoke dbus method");
340 g_variant_get(message, "(i)", result);
341 g_variant_unref(message);
346 error_e Vpnsvc::vpn_remove_dns_server(const char* iface_name, const char *dns_server,
349 GVariant *message = NULL;
350 error_e error = ERROR_NONE;
352 message = InvokeMethod(NETCONFIG_SERVICE,
353 NETCONFIG_VPNSVC_PATH,
354 NETCONFIG_VPNSVC_INTERFACE,
355 VPN_REMOVE_DNS_SERVER,
356 g_variant_new("(ss)", iface_name, dns_server),
359 if (message == NULL) {
360 GLOGD("Failed to invoke dbus method");
364 g_variant_get(message, "(i)", result);
365 g_variant_unref(message);