Merge "Fix for CID:860243" into tizen
[platform/core/connectivity/net-config.git] / gtest / vpn.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <iostream>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include <stdint.h>
23 #include <fcntl.h>
24 #include <gio/gio.h>
25 #include <gio/gunixfdlist.h>
26
27 #include "vpn.h"
28
29 Vpnsvc::Vpnsvc()
30 {
31         Create();
32 }
33
34 Vpnsvc::~Vpnsvc()
35 {
36         Destroy();
37 }
38
39 error_e Vpnsvc::vpn_init(const char *iface_name, int iface_name_len, int *result,
40                         int *h_index, char *h_name)
41 {
42         GVariant *message = NULL;
43         error_e error = ERROR_NONE;
44         GError *g_error = NULL;
45         GUnixFDList *fd_list = NULL;
46         int iface_fd = 0;
47
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 ;
52         }
53
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;
58         }
59
60         g_unix_fd_list_append(fd_list, iface_fd, &g_error);
61         if (g_error != NULL) {
62                 close(iface_fd);
63                 g_object_unref(fd_list);
64                 return ERROR_OPERATION_FAILED;
65         }
66
67         message = InvokeMethod_with_fd(fd_list,
68                 NETCONFIG_SERVICE,
69                 NETCONFIG_VPNSVC_PATH,
70                 NETCONFIG_VPNSVC_INTERFACE,
71                 VPN_INIT,
72                 g_variant_new("(su)", iface_name, iface_name_len),
73                 &error);
74
75         if (message == NULL) {
76                 GLOGD("Failed to invoke dbus method");
77                 close(iface_fd);
78                 g_object_unref(fd_list);
79                 return error;
80         }
81
82         g_variant_get(message, "(iis)", result, h_index, h_name);
83         g_variant_unref(message);
84         close(iface_fd);
85         g_object_unref(fd_list);
86
87         return ERROR_NONE;
88 }
89
90 error_e Vpnsvc::vpn_deinit(const char *dev_name, int *result)
91 {
92         GVariant *message = NULL;
93         error_e error = ERROR_NONE;
94
95         message = InvokeMethod(NETCONFIG_SERVICE,
96                 NETCONFIG_VPNSVC_PATH,
97                 NETCONFIG_VPNSVC_INTERFACE,
98                 VPN_DEINIT,
99                 g_variant_new("(s)", dev_name),
100                 &error);
101
102         if (message == NULL) {
103                 GLOGD("Failed to invoke dbus method");
104                 return error;
105         }
106
107         g_variant_get(message, "(i)", result);
108         g_variant_unref(message);
109
110         return ERROR_NONE;
111 }
112
113 error_e Vpnsvc::vpn_protect(const char *dev_name, int *result)
114 {
115         GVariant *message = NULL;
116         error_e error = ERROR_NONE;
117         GError *g_error = NULL;
118         GUnixFDList *fd_list = NULL;
119         int iface_fd = 0;
120
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 ;
125         }
126
127         if ((iface_fd = open("/dev/net/tun", O_RDWR)) < 0) {
128                 GLOGD("tun device open fail\n");
129                 g_object_unref(fd_list);
130                 return ERROR_OPERATION_FAILED;
131         }
132
133         g_unix_fd_list_append(fd_list, iface_fd, &g_error);
134         if (g_error != NULL) {
135                 close(iface_fd);
136                 g_object_unref(fd_list);
137                 return ERROR_OPERATION_FAILED;
138         }
139
140         message = InvokeMethod_with_fd(fd_list,
141                 NETCONFIG_SERVICE,
142                 NETCONFIG_VPNSVC_PATH,
143                 NETCONFIG_VPNSVC_INTERFACE,
144                 VPN_PROTECT,
145                 g_variant_new("(s)", dev_name),
146                 &error);
147
148         if (message == NULL) {
149                 GLOGD("Failed to invoke dbus method");
150                 close(iface_fd);
151                 g_object_unref(fd_list);
152                 return error;
153         }
154
155         g_variant_get(message, "(i)", result);
156         g_variant_unref(message);
157         close(iface_fd);
158         g_object_unref(fd_list);
159
160         return ERROR_NONE;
161 }
162
163 error_e Vpnsvc::vpn_up(const char *iface_name, int *result)
164 {
165         GVariant *message = NULL;
166         error_e error = ERROR_NONE;
167
168         message = InvokeMethod(NETCONFIG_SERVICE,
169                 NETCONFIG_VPNSVC_PATH,
170                 NETCONFIG_VPNSVC_INTERFACE,
171                 VPN_UP,
172                 g_variant_new("(s)", iface_name),
173                 &error);
174
175         if (message == NULL) {
176                 GLOGD("Failed to invoke dbus method");
177                 return error;
178         }
179
180         g_variant_get(message, "(i)", result);
181         g_variant_unref(message);
182
183         return ERROR_NONE;
184 }
185
186 error_e Vpnsvc::vpn_down(const char *iface_name, int *result)
187 {
188         GVariant *message = NULL;
189         error_e error = ERROR_NONE;
190
191         message = InvokeMethod(NETCONFIG_SERVICE,
192                 NETCONFIG_VPNSVC_PATH,
193                 NETCONFIG_VPNSVC_INTERFACE,
194                 VPN_DOWN,
195                 g_variant_new("(s)", iface_name),
196                 &error);
197
198         if (message == NULL) {
199                 GLOGD("Failed to invoke dbus method");
200                 return error;
201         }
202
203         g_variant_get(message, "(i)", result);
204         g_variant_unref(message);
205
206         return ERROR_NONE;
207 }
208
209 error_e Vpnsvc::vpn_block_networks(GVariant *nets_vpn, int nr_nets_vpn,
210                 GVariant *nets_orig, int nr_nets_orig, int *result)
211 {
212         GVariant *message = NULL;
213         error_e error = ERROR_NONE;
214
215         message = InvokeMethod(NETCONFIG_SERVICE,
216                 NETCONFIG_VPNSVC_PATH,
217                 NETCONFIG_VPNSVC_INTERFACE,
218                 VPN_BLOCK_NETWORKS,
219                 g_variant_new("(vuvu)", nets_vpn, nr_nets_vpn,
220                 nets_orig, nr_nets_orig),
221                 &error);
222
223         if (message == NULL) {
224                 GLOGD("Failed to invoke dbus method");
225                 return error;
226         }
227
228         g_variant_get(message, "(i)", result);
229         g_variant_unref(message);
230
231         return ERROR_NONE;
232 }
233
234 error_e Vpnsvc::vpn_unblock_networks(int *result)
235 {
236         GVariant *message = NULL;
237         error_e error = ERROR_NONE;
238
239         message = InvokeMethod(NETCONFIG_SERVICE,
240                 NETCONFIG_VPNSVC_PATH,
241                 NETCONFIG_VPNSVC_INTERFACE,
242                 VPN_UNBLOCK_NETWORKS,
243                 NULL,
244                 &error);
245
246         if (message == NULL) {
247                 GLOGD("Failed to invoke dbus method");
248                 return error;
249         }
250
251         g_variant_get(message, "(i)", result);
252         g_variant_unref(message);
253
254         return ERROR_NONE;
255 }
256
257 error_e Vpnsvc::vpn_update_settings(int iface_index, const char *local_ip,
258                 const char *remote_ip, int mtu, int *result)
259 {
260         GVariant *message = NULL;
261         error_e error = ERROR_NONE;
262
263         message = InvokeMethod(NETCONFIG_SERVICE,
264                 NETCONFIG_VPNSVC_PATH,
265                 NETCONFIG_VPNSVC_INTERFACE,
266                 VPN_UPDATE_SETTINGS,
267                 g_variant_new("(issu)", iface_index, local_ip,
268                 remote_ip, mtu),
269                 &error);
270
271         if (message == NULL) {
272                 GLOGD("Failed to invoke dbus method");
273                 return error;
274         }
275
276         g_variant_get(message, "(i)", result);
277         g_variant_unref(message);
278
279         return ERROR_NONE;
280 }
281
282 error_e Vpnsvc::vpn_add_route(const char *iface_name, const char *route, int prefix,
283                 int *result)
284 {
285         GVariant *message = NULL;
286         error_e error = ERROR_NONE;
287
288         message = InvokeMethod(NETCONFIG_SERVICE,
289                 NETCONFIG_VPNSVC_PATH,
290                 NETCONFIG_VPNSVC_INTERFACE,
291                 VPN_ADD_ROUTE,
292                 g_variant_new("(ssi)", iface_name, route, prefix),
293                 &error);
294
295         if (message == NULL) {
296                 GLOGD("Failed to invoke dbus method");
297                 return error;
298         }
299
300         g_variant_get(message, "(i)", result);
301         g_variant_unref(message);
302
303         return ERROR_NONE;
304 }
305
306 error_e Vpnsvc::vpn_remove_route(const char *iface_name, const char *route, int prefix,
307                 int *result)
308 {
309         GVariant *message = NULL;
310         error_e error = ERROR_NONE;
311
312         message = InvokeMethod(NETCONFIG_SERVICE,
313                 NETCONFIG_VPNSVC_PATH,
314                 NETCONFIG_VPNSVC_INTERFACE,
315                 VPN_REMOVE_ROUTE,
316                 g_variant_new("(ssi)", iface_name, route, prefix),
317                 &error);
318
319         if (message == NULL) {
320                 GLOGD("Failed to invoke dbus method");
321                 return error;
322         }
323
324         g_variant_get(message, "(i)", result);
325         g_variant_unref(message);
326
327         return ERROR_NONE;
328 }
329
330 error_e Vpnsvc::vpn_add_dns_server(const char* iface_name, const char *dns_server,
331                 int *result)
332 {
333         GVariant *message = NULL;
334         error_e error = ERROR_NONE;
335
336         message = InvokeMethod(NETCONFIG_SERVICE,
337                 NETCONFIG_VPNSVC_PATH,
338                 NETCONFIG_VPNSVC_INTERFACE,
339                 VPN_ADD_DNS_SERVER,
340                 g_variant_new("(ss)", iface_name, dns_server),
341                 &error);
342
343         if (message == NULL) {
344                 GLOGD("Failed to invoke dbus method");
345                 return error;
346         }
347
348         g_variant_get(message, "(i)", result);
349         g_variant_unref(message);
350
351         return ERROR_NONE;
352 }
353
354 error_e Vpnsvc::vpn_remove_dns_server(const char* iface_name, const char *dns_server,
355                 int *result)
356 {
357         GVariant *message = NULL;
358         error_e error = ERROR_NONE;
359
360         message = InvokeMethod(NETCONFIG_SERVICE,
361                 NETCONFIG_VPNSVC_PATH,
362                 NETCONFIG_VPNSVC_INTERFACE,
363                 VPN_REMOVE_DNS_SERVER,
364                 g_variant_new("(ss)", iface_name, dns_server),
365                 &error);
366
367         if (message == NULL) {
368                 GLOGD("Failed to invoke dbus method");
369                 return error;
370         }
371
372         g_variant_get(message, "(i)", result);
373         g_variant_unref(message);
374
375         return ERROR_NONE;
376 }
377