ec42fbaf22477a0e22cf6e0755b5812eb6b06d11
[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                 return ERROR_OPERATION_FAILED;
57         }
58
59         g_unix_fd_list_append(fd_list, iface_fd, &g_error);
60         if (g_error != NULL) {
61                 close(iface_fd);
62                 return ERROR_OPERATION_FAILED;
63         }
64
65         message = InvokeMethod_with_fd(fd_list,
66                 NETCONFIG_SERVICE,
67                 NETCONFIG_VPNSVC_PATH,
68                 NETCONFIG_VPNSVC_INTERFACE,
69                 VPN_INIT,
70                 g_variant_new("(su)", iface_name, iface_name_len),
71                 &error);
72
73         if (message == NULL) {
74                 GLOGD("Failed to invoke dbus method");
75                 close(iface_fd);
76                 return error;
77         }
78
79         g_variant_get(message, "(iis)", result, h_index, h_name);
80         g_variant_unref(message);
81         close(iface_fd);
82
83         return ERROR_NONE;
84 }
85
86 error_e Vpnsvc::vpn_deinit(const char *dev_name, int *result)
87 {
88         GVariant *message = NULL;
89         error_e error = ERROR_NONE;
90
91         message = InvokeMethod(NETCONFIG_SERVICE,
92                 NETCONFIG_VPNSVC_PATH,
93                 NETCONFIG_VPNSVC_INTERFACE,
94                 VPN_DEINIT,
95                 g_variant_new("(s)", dev_name),
96                 &error);
97
98         if (message == NULL) {
99                 GLOGD("Failed to invoke dbus method");
100                 return error;
101         }
102
103         g_variant_get(message, "(i)", result);
104         g_variant_unref(message);
105
106         return ERROR_NONE;
107 }
108
109 error_e Vpnsvc::vpn_protect(const char *dev_name, int *result)
110 {
111         GVariant *message = NULL;
112         error_e error = ERROR_NONE;
113         GError *g_error = NULL;
114         GUnixFDList *fd_list = NULL;
115         int iface_fd = 0;
116
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 ;
121         }
122
123         if ((iface_fd = open("/dev/net/tun", O_RDWR)) < 0) {
124                 GLOGD("tun device open fail\n");
125                 g_object_unref(fd_list);
126                 return ERROR_OPERATION_FAILED;
127         }
128
129         g_unix_fd_list_append(fd_list, iface_fd, &g_error);
130         if (g_error != NULL) {
131                 close(iface_fd);
132                 g_object_unref(fd_list);
133                 return ERROR_OPERATION_FAILED;
134         }
135
136         message = InvokeMethod_with_fd(fd_list,
137                 NETCONFIG_SERVICE,
138                 NETCONFIG_VPNSVC_PATH,
139                 NETCONFIG_VPNSVC_INTERFACE,
140                 VPN_PROTECT,
141                 g_variant_new("(s)", dev_name),
142                 &error);
143
144         if (message == NULL) {
145                 GLOGD("Failed to invoke dbus method");
146                 close(iface_fd);
147                 g_object_unref(fd_list);
148                 return error;
149         }
150
151         g_variant_get(message, "(i)", result);
152         g_variant_unref(message);
153         close(iface_fd);
154         g_object_unref(fd_list);
155
156         return ERROR_NONE;
157 }
158
159 error_e Vpnsvc::vpn_up(const char *iface_name, int *result)
160 {
161         GVariant *message = NULL;
162         error_e error = ERROR_NONE;
163
164         message = InvokeMethod(NETCONFIG_SERVICE,
165                 NETCONFIG_VPNSVC_PATH,
166                 NETCONFIG_VPNSVC_INTERFACE,
167                 VPN_UP,
168                 g_variant_new("(s)", iface_name),
169                 &error);
170
171         if (message == NULL) {
172                 GLOGD("Failed to invoke dbus method");
173                 return error;
174         }
175
176         g_variant_get(message, "(i)", result);
177         g_variant_unref(message);
178
179         return ERROR_NONE;
180 }
181
182 error_e Vpnsvc::vpn_down(const char *iface_name, int *result)
183 {
184         GVariant *message = NULL;
185         error_e error = ERROR_NONE;
186
187         message = InvokeMethod(NETCONFIG_SERVICE,
188                 NETCONFIG_VPNSVC_PATH,
189                 NETCONFIG_VPNSVC_INTERFACE,
190                 VPN_DOWN,
191                 g_variant_new("(s)", iface_name),
192                 &error);
193
194         if (message == NULL) {
195                 GLOGD("Failed to invoke dbus method");
196                 return error;
197         }
198
199         g_variant_get(message, "(i)", result);
200         g_variant_unref(message);
201
202         return ERROR_NONE;
203 }
204
205 error_e Vpnsvc::vpn_block_networks(GVariant *nets_vpn, int nr_nets_vpn,
206                 GVariant *nets_orig, int nr_nets_orig, int *result)
207 {
208         GVariant *message = NULL;
209         error_e error = ERROR_NONE;
210
211         message = InvokeMethod(NETCONFIG_SERVICE,
212                 NETCONFIG_VPNSVC_PATH,
213                 NETCONFIG_VPNSVC_INTERFACE,
214                 VPN_BLOCK_NETWORKS,
215                 g_variant_new("(vuvu)", nets_vpn, nr_nets_vpn,
216                 nets_orig, nr_nets_orig),
217                 &error);
218
219         if (message == NULL) {
220                 GLOGD("Failed to invoke dbus method");
221                 return error;
222         }
223
224         g_variant_get(message, "(i)", result);
225         g_variant_unref(message);
226
227         return ERROR_NONE;
228 }
229
230 error_e Vpnsvc::vpn_unblock_networks(int *result)
231 {
232         GVariant *message = NULL;
233         error_e error = ERROR_NONE;
234
235         message = InvokeMethod(NETCONFIG_SERVICE,
236                 NETCONFIG_VPNSVC_PATH,
237                 NETCONFIG_VPNSVC_INTERFACE,
238                 VPN_UNBLOCK_NETWORKS,
239                 NULL,
240                 &error);
241
242         if (message == NULL) {
243                 GLOGD("Failed to invoke dbus method");
244                 return error;
245         }
246
247         g_variant_get(message, "(i)", result);
248         g_variant_unref(message);
249
250         return ERROR_NONE;
251 }
252
253 error_e Vpnsvc::vpn_update_settings(int iface_index, const char *local_ip,
254                 const char *remote_ip, int mtu, int *result)
255 {
256         GVariant *message = NULL;
257         error_e error = ERROR_NONE;
258
259         message = InvokeMethod(NETCONFIG_SERVICE,
260                 NETCONFIG_VPNSVC_PATH,
261                 NETCONFIG_VPNSVC_INTERFACE,
262                 VPN_UPDATE_SETTINGS,
263                 g_variant_new("(issu)", iface_index, local_ip,
264                 remote_ip, mtu),
265                 &error);
266
267         if (message == NULL) {
268                 GLOGD("Failed to invoke dbus method");
269                 return error;
270         }
271
272         g_variant_get(message, "(i)", result);
273         g_variant_unref(message);
274
275         return ERROR_NONE;
276 }
277
278 error_e Vpnsvc::vpn_add_route(const char *iface_name, const char *route, int prefix,
279                 int *result)
280 {
281         GVariant *message = NULL;
282         error_e error = ERROR_NONE;
283
284         message = InvokeMethod(NETCONFIG_SERVICE,
285                 NETCONFIG_VPNSVC_PATH,
286                 NETCONFIG_VPNSVC_INTERFACE,
287                 VPN_ADD_ROUTE,
288                 g_variant_new("(ssi)", iface_name, route, prefix),
289                 &error);
290
291         if (message == NULL) {
292                 GLOGD("Failed to invoke dbus method");
293                 return error;
294         }
295
296         g_variant_get(message, "(i)", result);
297         g_variant_unref(message);
298
299         return ERROR_NONE;
300 }
301
302 error_e Vpnsvc::vpn_remove_route(const char *iface_name, const char *route, int prefix,
303                 int *result)
304 {
305         GVariant *message = NULL;
306         error_e error = ERROR_NONE;
307
308         message = InvokeMethod(NETCONFIG_SERVICE,
309                 NETCONFIG_VPNSVC_PATH,
310                 NETCONFIG_VPNSVC_INTERFACE,
311                 VPN_REMOVE_ROUTE,
312                 g_variant_new("(ssi)", iface_name, route, prefix),
313                 &error);
314
315         if (message == NULL) {
316                 GLOGD("Failed to invoke dbus method");
317                 return error;
318         }
319
320         g_variant_get(message, "(i)", result);
321         g_variant_unref(message);
322
323         return ERROR_NONE;
324 }
325
326 error_e Vpnsvc::vpn_add_dns_server(const char* iface_name, const char *dns_server,
327                 int *result)
328 {
329         GVariant *message = NULL;
330         error_e error = ERROR_NONE;
331
332         message = InvokeMethod(NETCONFIG_SERVICE,
333                 NETCONFIG_VPNSVC_PATH,
334                 NETCONFIG_VPNSVC_INTERFACE,
335                 VPN_ADD_DNS_SERVER,
336                 g_variant_new("(ss)", iface_name, dns_server),
337                 &error);
338
339         if (message == NULL) {
340                 GLOGD("Failed to invoke dbus method");
341                 return error;
342         }
343
344         g_variant_get(message, "(i)", result);
345         g_variant_unref(message);
346
347         return ERROR_NONE;
348 }
349
350 error_e Vpnsvc::vpn_remove_dns_server(const char* iface_name, const char *dns_server,
351                 int *result)
352 {
353         GVariant *message = NULL;
354         error_e error = ERROR_NONE;
355
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),
361                 &error);
362
363         if (message == NULL) {
364                 GLOGD("Failed to invoke dbus method");
365                 return error;
366         }
367
368         g_variant_get(message, "(i)", result);
369         g_variant_unref(message);
370
371         return ERROR_NONE;
372 }
373