9ad6917189b4f2cb9d1710fd5f6a9c2b862e3bc6
[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                 return ERROR_OPERATION_FAILED;
130         }
131
132         g_unix_fd_list_append(fd_list, iface_fd, &g_error);
133         if (g_error != NULL) {
134                 close(iface_fd);
135                 return ERROR_OPERATION_FAILED;
136         }
137
138         message = InvokeMethod_with_fd(fd_list,
139                 NETCONFIG_SERVICE,
140                 NETCONFIG_VPNSVC_PATH,
141                 NETCONFIG_VPNSVC_INTERFACE,
142                 VPN_PROTECT,
143                 g_variant_new("(s)", dev_name),
144                 &error);
145
146         if (message == NULL) {
147                 GLOGD("Failed to invoke dbus method");
148                 close(iface_fd);
149                 return error;
150         }
151
152         g_variant_get(message, "(i)", result);
153         g_variant_unref(message);
154         close(iface_fd);
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