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 g_object_unref(fd_list);
57 return ERROR_OPERATION_FAILED;
60 g_unix_fd_list_append(fd_list, iface_fd, &g_error);
61 if (g_error != NULL) {
63 g_object_unref(fd_list);
64 return ERROR_OPERATION_FAILED;
67 message = InvokeMethod_with_fd(fd_list,
69 NETCONFIG_VPNSVC_PATH,
70 NETCONFIG_VPNSVC_INTERFACE,
72 g_variant_new("(su)", iface_name, iface_name_len),
75 if (message == NULL) {
76 GLOGD("Failed to invoke dbus method");
78 g_object_unref(fd_list);
82 g_variant_get(message, "(iis)", result, h_index, h_name);
83 g_variant_unref(message);
85 g_object_unref(fd_list);
90 error_e Vpnsvc::vpn_deinit(const char *dev_name, int *result)
92 GVariant *message = NULL;
93 error_e error = ERROR_NONE;
95 message = InvokeMethod(NETCONFIG_SERVICE,
96 NETCONFIG_VPNSVC_PATH,
97 NETCONFIG_VPNSVC_INTERFACE,
99 g_variant_new("(s)", dev_name),
102 if (message == NULL) {
103 GLOGD("Failed to invoke dbus method");
107 g_variant_get(message, "(i)", result);
108 g_variant_unref(message);
113 error_e Vpnsvc::vpn_protect(const char *dev_name, int *result)
115 GVariant *message = NULL;
116 error_e error = ERROR_NONE;
117 GError *g_error = NULL;
118 GUnixFDList *fd_list = NULL;
121 fd_list = g_unix_fd_list_new();
122 if (fd_list == NULL) {
123 GLOGD("g_unix_fd_list_new() failed!");
124 return ERROR_OPERATION_FAILED ;
127 if ((iface_fd = open("/dev/net/tun", O_RDWR)) < 0) {
128 GLOGD("tun device open fail\n");
129 return ERROR_OPERATION_FAILED;
132 g_unix_fd_list_append(fd_list, iface_fd, &g_error);
133 if (g_error != NULL) {
135 return ERROR_OPERATION_FAILED;
138 message = InvokeMethod_with_fd(fd_list,
140 NETCONFIG_VPNSVC_PATH,
141 NETCONFIG_VPNSVC_INTERFACE,
143 g_variant_new("(s)", dev_name),
146 if (message == NULL) {
147 GLOGD("Failed to invoke dbus method");
152 g_variant_get(message, "(i)", result);
153 g_variant_unref(message);
159 error_e Vpnsvc::vpn_up(const char *iface_name, int *result)
161 GVariant *message = NULL;
162 error_e error = ERROR_NONE;
164 message = InvokeMethod(NETCONFIG_SERVICE,
165 NETCONFIG_VPNSVC_PATH,
166 NETCONFIG_VPNSVC_INTERFACE,
168 g_variant_new("(s)", iface_name),
171 if (message == NULL) {
172 GLOGD("Failed to invoke dbus method");
176 g_variant_get(message, "(i)", result);
177 g_variant_unref(message);
182 error_e Vpnsvc::vpn_down(const char *iface_name, int *result)
184 GVariant *message = NULL;
185 error_e error = ERROR_NONE;
187 message = InvokeMethod(NETCONFIG_SERVICE,
188 NETCONFIG_VPNSVC_PATH,
189 NETCONFIG_VPNSVC_INTERFACE,
191 g_variant_new("(s)", iface_name),
194 if (message == NULL) {
195 GLOGD("Failed to invoke dbus method");
199 g_variant_get(message, "(i)", result);
200 g_variant_unref(message);
205 error_e Vpnsvc::vpn_block_networks(GVariant *nets_vpn, int nr_nets_vpn,
206 GVariant *nets_orig, int nr_nets_orig, int *result)
208 GVariant *message = NULL;
209 error_e error = ERROR_NONE;
211 message = InvokeMethod(NETCONFIG_SERVICE,
212 NETCONFIG_VPNSVC_PATH,
213 NETCONFIG_VPNSVC_INTERFACE,
215 g_variant_new("(vuvu)", nets_vpn, nr_nets_vpn,
216 nets_orig, nr_nets_orig),
219 if (message == NULL) {
220 GLOGD("Failed to invoke dbus method");
224 g_variant_get(message, "(i)", result);
225 g_variant_unref(message);
230 error_e Vpnsvc::vpn_unblock_networks(int *result)
232 GVariant *message = NULL;
233 error_e error = ERROR_NONE;
235 message = InvokeMethod(NETCONFIG_SERVICE,
236 NETCONFIG_VPNSVC_PATH,
237 NETCONFIG_VPNSVC_INTERFACE,
238 VPN_UNBLOCK_NETWORKS,
242 if (message == NULL) {
243 GLOGD("Failed to invoke dbus method");
247 g_variant_get(message, "(i)", result);
248 g_variant_unref(message);
253 error_e Vpnsvc::vpn_update_settings(int iface_index, const char *local_ip,
254 const char *remote_ip, int mtu, int *result)
256 GVariant *message = NULL;
257 error_e error = ERROR_NONE;
259 message = InvokeMethod(NETCONFIG_SERVICE,
260 NETCONFIG_VPNSVC_PATH,
261 NETCONFIG_VPNSVC_INTERFACE,
263 g_variant_new("(issu)", iface_index, local_ip,
267 if (message == NULL) {
268 GLOGD("Failed to invoke dbus method");
272 g_variant_get(message, "(i)", result);
273 g_variant_unref(message);
278 error_e Vpnsvc::vpn_add_route(const char *iface_name, const char *route, int prefix,
281 GVariant *message = NULL;
282 error_e error = ERROR_NONE;
284 message = InvokeMethod(NETCONFIG_SERVICE,
285 NETCONFIG_VPNSVC_PATH,
286 NETCONFIG_VPNSVC_INTERFACE,
288 g_variant_new("(ssi)", iface_name, route, prefix),
291 if (message == NULL) {
292 GLOGD("Failed to invoke dbus method");
296 g_variant_get(message, "(i)", result);
297 g_variant_unref(message);
302 error_e Vpnsvc::vpn_remove_route(const char *iface_name, const char *route, int prefix,
305 GVariant *message = NULL;
306 error_e error = ERROR_NONE;
308 message = InvokeMethod(NETCONFIG_SERVICE,
309 NETCONFIG_VPNSVC_PATH,
310 NETCONFIG_VPNSVC_INTERFACE,
312 g_variant_new("(ssi)", iface_name, route, prefix),
315 if (message == NULL) {
316 GLOGD("Failed to invoke dbus method");
320 g_variant_get(message, "(i)", result);
321 g_variant_unref(message);
326 error_e Vpnsvc::vpn_add_dns_server(const char* iface_name, const char *dns_server,
329 GVariant *message = NULL;
330 error_e error = ERROR_NONE;
332 message = InvokeMethod(NETCONFIG_SERVICE,
333 NETCONFIG_VPNSVC_PATH,
334 NETCONFIG_VPNSVC_INTERFACE,
336 g_variant_new("(ss)", iface_name, dns_server),
339 if (message == NULL) {
340 GLOGD("Failed to invoke dbus method");
344 g_variant_get(message, "(i)", result);
345 g_variant_unref(message);
350 error_e Vpnsvc::vpn_remove_dns_server(const char* iface_name, const char *dns_server,
353 GVariant *message = NULL;
354 error_e error = ERROR_NONE;
356 message = InvokeMethod(NETCONFIG_SERVICE,
357 NETCONFIG_VPNSVC_PATH,
358 NETCONFIG_VPNSVC_INTERFACE,
359 VPN_REMOVE_DNS_SERVER,
360 g_variant_new("(ss)", iface_name, dns_server),
363 if (message == NULL) {
364 GLOGD("Failed to invoke dbus method");
368 g_variant_get(message, "(i)", result);
369 g_variant_unref(message);