Added dump log for connect fail #2
[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                 return ERROR_OPERATION_FAILED;
126         }
127
128         g_unix_fd_list_append(fd_list, iface_fd, &g_error);
129         if (g_error != NULL) {
130                 close(iface_fd);
131                 return ERROR_OPERATION_FAILED;
132         }
133
134         message = InvokeMethod_with_fd(fd_list,
135                 NETCONFIG_SERVICE,
136                 NETCONFIG_VPNSVC_PATH,
137                 NETCONFIG_VPNSVC_INTERFACE,
138                 VPN_PROTECT,
139                 g_variant_new("(s)", dev_name),
140                 &error);
141
142         if (message == NULL) {
143                 GLOGD("Failed to invoke dbus method");
144                 close(iface_fd);
145                 return error;
146         }
147
148         g_variant_get(message, "(i)", result);
149         g_variant_unref(message);
150         close(iface_fd);
151
152         return ERROR_NONE;
153 }
154
155 error_e Vpnsvc::vpn_up(const char *iface_name, int *result)
156 {
157         GVariant *message = NULL;
158         error_e error = ERROR_NONE;
159
160         message = InvokeMethod(NETCONFIG_SERVICE,
161                 NETCONFIG_VPNSVC_PATH,
162                 NETCONFIG_VPNSVC_INTERFACE,
163                 VPN_UP,
164                 g_variant_new("(s)", iface_name),
165                 &error);
166
167         if (message == NULL) {
168                 GLOGD("Failed to invoke dbus method");
169                 return error;
170         }
171
172         g_variant_get(message, "(i)", result);
173         g_variant_unref(message);
174
175         return ERROR_NONE;
176 }
177
178 error_e Vpnsvc::vpn_down(const char *iface_name, int *result)
179 {
180         GVariant *message = NULL;
181         error_e error = ERROR_NONE;
182
183         message = InvokeMethod(NETCONFIG_SERVICE,
184                 NETCONFIG_VPNSVC_PATH,
185                 NETCONFIG_VPNSVC_INTERFACE,
186                 VPN_DOWN,
187                 g_variant_new("(s)", iface_name),
188                 &error);
189
190         if (message == NULL) {
191                 GLOGD("Failed to invoke dbus method");
192                 return error;
193         }
194
195         g_variant_get(message, "(i)", result);
196         g_variant_unref(message);
197
198         return ERROR_NONE;
199 }
200
201 error_e Vpnsvc::vpn_block_networks(GVariant *nets_vpn, int nr_nets_vpn,
202                 GVariant *nets_orig, int nr_nets_orig, int *result)
203 {
204         GVariant *message = NULL;
205         error_e error = ERROR_NONE;
206
207         message = InvokeMethod(NETCONFIG_SERVICE,
208                 NETCONFIG_VPNSVC_PATH,
209                 NETCONFIG_VPNSVC_INTERFACE,
210                 VPN_BLOCK_NETWORKS,
211                 g_variant_new("(vuvu)", nets_vpn, nr_nets_vpn,
212                 nets_orig, nr_nets_orig),
213                 &error);
214
215         if (message == NULL) {
216                 GLOGD("Failed to invoke dbus method");
217                 return error;
218         }
219
220         g_variant_get(message, "(i)", result);
221         g_variant_unref(message);
222
223         return ERROR_NONE;
224 }
225
226 error_e Vpnsvc::vpn_unblock_networks(int *result)
227 {
228         GVariant *message = NULL;
229         error_e error = ERROR_NONE;
230
231         message = InvokeMethod(NETCONFIG_SERVICE,
232                 NETCONFIG_VPNSVC_PATH,
233                 NETCONFIG_VPNSVC_INTERFACE,
234                 VPN_UNBLOCK_NETWORKS,
235                 NULL,
236                 &error);
237
238         if (message == NULL) {
239                 GLOGD("Failed to invoke dbus method");
240                 return error;
241         }
242
243         g_variant_get(message, "(i)", result);
244         g_variant_unref(message);
245
246         return ERROR_NONE;
247 }
248
249 error_e Vpnsvc::vpn_update_settings(int iface_index, const char *local_ip,
250                 const char *remote_ip, int mtu, int *result)
251 {
252         GVariant *message = NULL;
253         error_e error = ERROR_NONE;
254
255         message = InvokeMethod(NETCONFIG_SERVICE,
256                 NETCONFIG_VPNSVC_PATH,
257                 NETCONFIG_VPNSVC_INTERFACE,
258                 VPN_UPDATE_SETTINGS,
259                 g_variant_new("(issu)", iface_index, local_ip,
260                 remote_ip, mtu),
261                 &error);
262
263         if (message == NULL) {
264                 GLOGD("Failed to invoke dbus method");
265                 return error;
266         }
267
268         g_variant_get(message, "(i)", result);
269         g_variant_unref(message);
270
271         return ERROR_NONE;
272 }
273
274 error_e Vpnsvc::vpn_add_route(const char *iface_name, const char *route, int prefix,
275                 int *result)
276 {
277         GVariant *message = NULL;
278         error_e error = ERROR_NONE;
279
280         message = InvokeMethod(NETCONFIG_SERVICE,
281                 NETCONFIG_VPNSVC_PATH,
282                 NETCONFIG_VPNSVC_INTERFACE,
283                 VPN_ADD_ROUTE,
284                 g_variant_new("(ssi)", iface_name, route, prefix),
285                 &error);
286
287         if (message == NULL) {
288                 GLOGD("Failed to invoke dbus method");
289                 return error;
290         }
291
292         g_variant_get(message, "(i)", result);
293         g_variant_unref(message);
294
295         return ERROR_NONE;
296 }
297
298 error_e Vpnsvc::vpn_remove_route(const char *iface_name, const char *route, int prefix,
299                 int *result)
300 {
301         GVariant *message = NULL;
302         error_e error = ERROR_NONE;
303
304         message = InvokeMethod(NETCONFIG_SERVICE,
305                 NETCONFIG_VPNSVC_PATH,
306                 NETCONFIG_VPNSVC_INTERFACE,
307                 VPN_REMOVE_ROUTE,
308                 g_variant_new("(ssi)", iface_name, route, prefix),
309                 &error);
310
311         if (message == NULL) {
312                 GLOGD("Failed to invoke dbus method");
313                 return error;
314         }
315
316         g_variant_get(message, "(i)", result);
317         g_variant_unref(message);
318
319         return ERROR_NONE;
320 }
321
322 error_e Vpnsvc::vpn_add_dns_server(const char* iface_name, const char *dns_server,
323                 int *result)
324 {
325         GVariant *message = NULL;
326         error_e error = ERROR_NONE;
327
328         message = InvokeMethod(NETCONFIG_SERVICE,
329                 NETCONFIG_VPNSVC_PATH,
330                 NETCONFIG_VPNSVC_INTERFACE,
331                 VPN_ADD_DNS_SERVER,
332                 g_variant_new("(ss)", iface_name, dns_server),
333                 &error);
334
335         if (message == NULL) {
336                 GLOGD("Failed to invoke dbus method");
337                 return error;
338         }
339
340         g_variant_get(message, "(i)", result);
341         g_variant_unref(message);
342
343         return ERROR_NONE;
344 }
345
346 error_e Vpnsvc::vpn_remove_dns_server(const char* iface_name, const char *dns_server,
347                 int *result)
348 {
349         GVariant *message = NULL;
350         error_e error = ERROR_NONE;
351
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),
357                 &error);
358
359         if (message == NULL) {
360                 GLOGD("Failed to invoke dbus method");
361                 return error;
362         }
363
364         g_variant_get(message, "(i)", result);
365         g_variant_unref(message);
366
367         return ERROR_NONE;
368 }
369