Add wireguard related changes and test cases
[platform/core/api/vpn-setting.git] / test / vpn_test.c
1 /*
2  * Copyright (c) 2014-2015 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 <glib.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <netdb.h>
21 #include <sys/socket.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/un.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <sys/ioctl.h>
28 #include <signal.h>
29 #include <assert.h>
30 #include <vpn.h>
31 #include <tizen_error.h>
32
33 #if defined(IPSEC_TEST)
34 #define CA_CERT "root-ca-cert.PEM"
35 #define CLIENT_CERT_DER "client-cert.crt"
36 #define CLIENT_CERT_PEM "client-cert.PEM"
37 #define CLIENT_CERT_PKCS12 "client-cert.p12"
38 #define HOST_CERT "host-cert.PEM"
39 #define CLIENT_PRIVATE_KEY "client-private.PEM"
40 #endif
41
42 #define MAX_USER_INPUT_LEN 200
43
44 gboolean test_thread(GIOChannel *source,
45                         GIOCondition condition,
46                         gpointer data);
47
48 static const char *__test_convert_error_to_string(vpn_error_e err_type)
49 {
50         switch (err_type) {
51         case VPN_ERROR_NONE:
52                 return "NONE";
53         case VPN_ERROR_INVALID_PARAMETER:
54                 return "INVALID_PARAMETER";
55         case VPN_ERROR_OUT_OF_MEMORY:
56                 return "OUT_OF_MEMORY";
57         case VPN_ERROR_INVALID_OPERATION:
58                 return "INVALID_OPERATION";
59         case VPN_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
60                 return "ADDRESS_FAMILY_NOT_SUPPORTED";
61         case VPN_ERROR_OPERATION_FAILED:
62                 return "OPERATION_FAILED";
63         case VPN_ERROR_NO_CONNECTION:
64                 return "NO_CONNECTION";
65         case VPN_ERROR_NOW_IN_PROGRESS:
66                 return "NOW_IN_PROGRESS";
67         case VPN_ERROR_ALREADY_EXISTS:
68                 return "ALREADY_EXISTS";
69         case VPN_ERROR_OPERATION_ABORTED:
70                 return "OPERATION_ABORTED";
71         case VPN_ERROR_DHCP_FAILED:
72                 return "DHCP_FAILED";
73         case VPN_ERROR_INVALID_KEY:
74                 return "INVALID_KEY";
75         case VPN_ERROR_NO_REPLY:
76                 return "NO_REPLY";
77         case VPN_ERROR_SECURITY_RESTRICTED:
78                 return "SECURITY_RESTRICTED";
79         }
80
81         return "UNKNOWN";
82 }
83
84
85 static const char *__test_convert_state_to_string(vpn_state_e state_type)
86 {
87         switch (state_type) {
88         case VPN_STATE_IDLE:
89                 return "IDLE";
90         case VPN_STATE_READY:
91                 return "READY";
92         case VPN_STATE_CONFIGURATION:
93                 return "CONFIGURATION";
94         case VPN_STATE_DISCONNECT:
95                 return "DISCONNECT";
96         case VPN_STATE_FAILURE:
97                 return "FAILURE";
98         case VPN_STATE_UNKNOWN:
99                 return "UNKNOWN";
100         }
101
102         return "UNKNOWN";
103 }
104
105 typedef struct {
106         char *key;
107         char *value;
108 } kv_s;
109
110 #if defined(IPSEC_TEST)
111 typedef enum {
112         IPSEC_HYBRID_RSA,
113         IPSEC_XAUTH_PSK,
114         IPSEC_XAUTH_RSA,
115         IPSEC_IKEV2_PSK,
116         IPSEC_IKEV2_RSA,
117         IPSEC_MAX,
118 } ipsec_type_e;
119
120 static const char *__get_ipsec_name(ipsec_type_e type)
121 {
122         switch (type) {
123         case IPSEC_HYBRID_RSA:
124                 return "IPSEC_HYBRID_RSA";
125         case IPSEC_XAUTH_PSK:
126                 return "IPSEC_XAUTH_PSK";
127         case IPSEC_XAUTH_RSA:
128                 return "IPSEC_XAUTH_RSA";
129         case IPSEC_IKEV2_PSK:
130                 return "IPSEC_IKEV2_PSK";
131         case IPSEC_IKEV2_RSA:
132                 return "IPSEC_IKEV2_RSA";
133         case IPSEC_MAX:
134         default:
135                 return "UNKNOWN";
136         }
137 }
138
139 typedef enum {
140         IPSEC_VERSION,
141         IPSEC_LEFT_ADDRS,
142         IPSEC_RIGHT_ADDRS,
143         IPSEC_LOCAL_AUTH,
144         IPSEC_LOCAL_CERTS,
145         IPSEC_LOCAL_CERT_PASS,
146         IPSEC_LOCAL_ID,
147         IPSEC_LOCAL_XAUTH_ID,
148         IPSEC_LOCAL_XAUTH_AUTH,
149         IPSEC_LOCAL_XAUTH_XAUTH_ID,
150         IPSEC_REMOTE_AUTH,
151         IPSEC_REMOTE_CERTS,
152         IPSEC_REMOTE_CERT_PASS,
153         IPSEC_REMOTE_ID,
154         IPSEC_REMOTE_XAUTH_ID,
155         IPSEC_REMOTE_XAUTH_AUTH,
156         IPSEC_REMOTE_XAUTH_XAUTH_ID,
157         IPSEC_CHILDREN_LOCAL_TS,
158         IPSEC_CHILDREN_REMOTE_TS,
159         IPSEC_IKE_DATA,
160         IPSEC_IKE_OWNERS,
161         IPSEC_XAUTH_DATA,
162         IPSEC_XAUTH_OWNERS,
163         IPSEC_CERT_TYPE,
164         IPSEC_CERT_FLAG,
165         IPSEC_CERT_DATA,
166         IPSEC_CERT_PASS,
167         IPSEC_PKEY_TYPE,
168         IPSEC_PKEY_DATA,
169         IPSEC_CA_CERTS_DIR,
170         IPSEC_KVS_MAX,
171 } ipsec_kv_e;
172
173 kv_s ipsec_setting_kvs[] = {
174                 {"IPsec.Version", NULL},
175                 {"IPsec.LeftAddrs", NULL},
176                 {"IPsec.RightAddrs", NULL},
177                 {"IPsec.LocalAuth", NULL},
178                 {"IPsec.LocalCerts", NULL},
179                 {"IPsec.LocalCertPass", NULL},
180                 {"IPsec.LocalID", NULL},
181                 {"IPsec.LocalXauthID", NULL},
182                 {"IPsec.LocalXauthAuth", NULL},
183                 {"IPsec.LocalXauthXauthID", NULL},
184                 {"IPsec.RemoteAuth", NULL},
185                 {"IPsec.RemoteCerts", NULL},
186                 {"IPsec.RemoteCertPass", NULL},
187                 {"IPsec.RemoteID", NULL},
188                 {"IPsec.RemoteXauthID", NULL},
189                 {"IPsec.RemoteXauthAuth", NULL},
190                 {"IPsec.RemoteXauthXauthID", NULL},
191                 {"IPsec.ChildrenLocalTS", NULL},
192                 {"IPsec.ChildrenRemoteTS", NULL},
193                 {"IPsec.IKEData", NULL},
194                 {"IPsec.IKEOwners", NULL},
195                 {"IPsec.XauthData", NULL},
196                 {"IPsec.XauthOwners", NULL},
197                 {"IPsec.CertType", NULL},
198                 {"IPsec.CertFlag", NULL},
199                 {"IPsec.CertData", NULL},
200                 {"IPsec.CertPass", NULL},
201                 {"IPsec.PKeyType", NULL},
202                 {"IPsec.PKeyData", NULL},
203                 {"IPsec.CACertsDir", NULL},
204                 {NULL, NULL},
205 };
206
207 typedef void (*gen_ipsec_settings_f)(void);
208 #endif
209
210 /**
211  * Note: Below template is to set Wireguard VPN Settings
212  *
213  * Type = wireguard
214  * Name = <Anything you like>
215  * Host = <Public IP of the WireGuard server>
216  * WireGuard.Address = <The internal IP of the client node, e.g. a /24 address>
217  * WireGuard.PrivateKey = <The client private key>
218  * WireGuard.PublicKey = <The server public key>
219  * WireGuard.AllowedIPs = <Subnets accessed via the tunnel, 0.0.0.0/0 is "route all traffic">
220  * WireGuard.EndpointPort = <The server listen port, default: 51820> (Optional)
221  * WireGuard.ListenPort = <The client/own device listen port> (Optional)
222  * WireGuard.DNS = <comma separated DNS> (Optional)
223  * WireGuard.PresharedKey = <The preshared key> (Optional)
224  * WireGuard.PersistentKeepalive = <The time in seconds to emit periodic keep alive message> (Optional)
225  */
226
227 typedef enum {
228         WIREGUARD_ADDRESS,
229         WIREGUARD_PRIVATE_KEY,
230         WIREGUARD_PUBLIC_KEY,
231         WIREGUARD_ALLOWED_IPS,
232         WIREGUARD_ENDPOINT_PORT,
233         WIREGUARD_PERSISTENT_KEEPALIVE,
234         WIREGUARD_KVS_MAX
235 } wireguard_kv_e;
236
237 kv_s wg_setting_kvs[] = {
238         {"WireGuard.Address", NULL},
239         {"WireGuard.PrivateKey", NULL},
240         {"WireGuard.PublicKey", NULL},
241         {"WireGuard.AllowedIPs", NULL},
242         {"WireGuard.EndpointPort", "51820"},
243         {"WireGuard.PersistentKeepalive", "10"},
244         {NULL, NULL},
245 };
246
247 static void __test_created_callback(vpn_error_e result,
248                                 void *user_data)
249 {
250         if (result == VPN_ERROR_NONE)
251                 printf("VPN Create Succeeded\n");
252         else
253                 printf("VPN Create Failed! error : %s",
254                                 __test_convert_error_to_string(result));
255 }
256
257 static void __test_removed_callback(vpn_error_e result,
258                                 void *user_data)
259 {
260         if (result == VPN_ERROR_NONE)
261                 printf("VPN Remove Succeeded\n");
262         else
263                 printf("VPN Remove Failed! error : %s",
264                                 __test_convert_error_to_string(result));
265 }
266
267 static void __test_connect_callback(vpn_error_e result,
268                                 void *user_data)
269 {
270         if (result == VPN_ERROR_NONE)
271                 printf("VPN Connect Succeeded\n");
272         else
273                 printf("VPN Connect Failed! error : %s",
274                                 __test_convert_error_to_string(result));
275 }
276
277 static void __test_disconnect_callback(vpn_error_e result,
278                                 void *user_data)
279 {
280         if (result == VPN_ERROR_NONE)
281                 printf("VPN Disconnect Succeeded\n");
282         else
283                 printf("VPN Disconnect Failed! error : %s",
284                                 __test_convert_error_to_string(result));
285 }
286
287 static void __test_state_callback(vpn_h handle_ptr, vpn_state_e state, void *user_data)
288 {
289         const char *name = NULL;
290         const char *type = NULL;
291         const char *host = NULL;
292         const char *domain = NULL;
293
294         printf("\n Handle = %p\n", handle_ptr);
295         vpn_get_vpn_info_name(handle_ptr, &name);
296         vpn_get_vpn_info_type(handle_ptr, &type);
297         vpn_get_vpn_info_host(handle_ptr, &host);
298         vpn_get_vpn_info_domain(handle_ptr, &domain);
299         printf(" Name[%p] - %s\n", handle_ptr, name);
300         printf(" Type[%p] - %s\n", handle_ptr, type);
301         printf(" Host[%p] - %s\n", handle_ptr, host);
302         printf(" Domain[%p] - %s\n", handle_ptr, domain);
303         printf("VPN state changed! : %s",
304                                 __test_convert_state_to_string(state));
305 }
306
307 static void _test_get_vpn_handle(vpn_h *handle_ptr)
308 {
309         assert(handle_ptr != NULL);
310
311         const char *name = NULL;
312         const char *type = NULL;
313         const char *host = NULL;
314         const char *domain = NULL;
315
316         GList *iter;
317         GList *handles = vpn_get_vpn_handle_list();
318         int index = 1;
319         int num;
320         for (iter = handles; iter != NULL; iter = iter->next) {
321                 printf(" \n%d)\n", index++);
322                 printf(" Handle = %p\n", iter->data);
323                 vpn_get_vpn_info_name(iter->data, &name);
324                 vpn_get_vpn_info_type(iter->data, &type);
325                 vpn_get_vpn_info_host(iter->data, &host);
326                 vpn_get_vpn_info_domain(iter->data, &domain);
327                 printf(" Name[%p] - %s\n", iter->data, name);
328                 printf(" Type[%p] - %s\n", iter->data, type);
329                 printf(" Host[%p] - %s\n", iter->data, host);
330                 printf(" Domain[%p] - %s\n", iter->data, domain ? domain : "NULL");
331         }
332
333         printf("==================================\n");
334         printf(" Please ENTER Number: ");
335         if (scanf(" %d", &num) < 0)
336                 printf("Error in Reading Host String\n");
337
338         if (num < index) {
339                 int i = 1;
340                 for (iter = handles; iter != NULL; iter = iter->next) {
341                         if (i == num) {
342                                 vpn_get_vpn_info_name(iter->data, &name);
343                                 vpn_get_vpn_info_type(iter->data, &type);
344                                 vpn_get_vpn_info_host(iter->data, &host);
345                                 vpn_get_vpn_info_domain(iter->data, &domain);
346                                 vpn_get_vpn_handle(name, host, domain, handle_ptr);
347                                 break;
348                         }
349                         i++;
350                 }
351         } else {
352                 printf("Error\n");
353         }
354 }
355
356 static void _test_get_user_input(char *buf, char *what)
357 {
358         char format[12];
359
360         snprintf(format, 12, " %%%d[^\n]s", MAX_USER_INPUT_LEN);
361         printf("Please ENTER %s:", what);
362         if (scanf(format, buf) < 0)
363                 printf("Error in Reading the data to Buffer\n");
364 }
365
366 int test_vpn_init(void)
367 {
368         int rv = vpn_initialize();
369
370         if (rv == VPN_ERROR_NONE) {
371                 printf("Register Callbacks if Initialize is Successful\n");
372         } else {
373                 printf("VPN init failed [%s]\n",
374                         __test_convert_error_to_string(rv));
375                 return -1;
376         }
377
378         printf("VPN init succeeded\n");
379         return 1;
380 }
381
382 int test_vpn_deinit(void)
383 {
384         int rv = vpn_deinitialize();
385
386         if (rv != VPN_ERROR_NONE) {
387                 printf("VPN init failed [%s]\n",
388                         __test_convert_error_to_string(rv));
389                 return -1;
390         }
391
392         printf("VPN deinit succeeded\n");
393         return 1;
394 }
395
396 int test_vpn_settings_init(void)
397 {
398         int rv = 0;
399
400         rv = vpn_settings_init();
401
402         if (rv != VPN_ERROR_NONE) {
403                 printf("Fail to Initialize Settings [%s]\n",
404                                 __test_convert_error_to_string(rv));
405                 return -1;
406         }
407
408         printf("Success Creating Settings API's\n");
409
410         return 1;
411 }
412
413 int test_vpn_settings_add(const char *type, const char *name, const char *host, const char *domain)
414 {
415         int rv = 0;
416         char buf[MAX_USER_INPUT_LEN + 1];
417
418         if (type == NULL) {
419                 _test_get_user_input(&buf[0], "Type");
420                 rv = vpn_settings_set_type(&buf[0]);
421         } else {
422                 rv = vpn_settings_set_type(type);
423         }
424         if (rv != VPN_ERROR_NONE) {
425                 printf("Fail to VPN Settings Type[%s]\n",
426                                 __test_convert_error_to_string(rv));
427                 return -1;
428         }
429
430         if (name == NULL) {
431                 _test_get_user_input(&buf[0], "Name");
432                 rv = vpn_settings_set_name(&buf[0]);
433         } else {
434                 rv = vpn_settings_set_name(name);
435         }
436         if (rv != VPN_ERROR_NONE) {
437                 printf("Fail to VPN Settings Name[%s]\n",
438                                 __test_convert_error_to_string(rv));
439                 return -1;
440         }
441
442         if (host == NULL) {
443                 _test_get_user_input(&buf[0], "Host");
444                 rv = vpn_settings_set_host(&buf[0]);
445         } else {
446                 rv = vpn_settings_set_host(host);
447         }
448         if (rv != VPN_ERROR_NONE) {
449                 printf("Fail to VPN Settings Host[%s]\n",
450                                 __test_convert_error_to_string(rv));
451                 return -1;
452         }
453
454         if (!g_strcmp0(type, "wireguard"))
455                 return 1;
456
457         if (domain == NULL) {
458                 _test_get_user_input(&buf[0], "Domain");
459                 rv = vpn_settings_set_domain(&buf[0]);
460         } else {
461                 rv = vpn_settings_set_domain(domain);
462         }
463         if (rv != VPN_ERROR_NONE) {
464                 printf("Fail to VPN Settings Domain[%s]\n",
465                                 __test_convert_error_to_string(rv));
466                 return -1;
467         }
468
469         printf("Success Creating Settings API's\n");
470
471         return 1;
472 }
473
474 int test_vpn_settings_deinit(void)
475 {
476         int rv = 0;
477
478         rv = vpn_settings_deinit();
479
480         if (rv != VPN_ERROR_NONE) {
481                 printf("Fail to Deinitialize Settings [%s]\n",
482                                 __test_convert_error_to_string(rv));
483                 return -1;
484         }
485
486         printf("Success Deleting Settings API's\n");
487
488         return 1;
489 }
490
491 int test_vpn_settings_set_specific(void)
492 {
493         int rv = 0;
494         char key[MAX_USER_INPUT_LEN + 1];
495         char value[MAX_USER_INPUT_LEN + 1];
496
497         _test_get_user_input(&key[0], "Key");
498         _test_get_user_input(&value[0], "Value");
499         rv = vpn_settings_set_specific(&key[0], &value[0]);
500         if (rv != VPN_ERROR_NONE) {
501                 printf("Fail to Set Specific VPN Settings %s[%s]\n",
502                                 key, __test_convert_error_to_string(rv));
503                 return -1;
504         }
505
506         printf("Success in VPN Settings Add %s=%s\n", key, value);
507
508         return 1;
509 }
510
511 int _test_vpn_settings_set_specific(const char *key, const char *value)
512 {
513         int rv = 0;
514         if (key == NULL || value == NULL) {
515                 printf("Invalid parameters\n");
516                 return -1;
517         }
518
519         rv = vpn_settings_set_specific(key, value);
520         if (rv != VPN_ERROR_NONE) {
521                 printf("Fail to Set Specific VPN Settings %s[%s]\n",
522                                 key, __test_convert_error_to_string(rv));
523                 return -1;
524         }
525
526         printf("Success in VPN Settings Add %s=%s\n", key, value);
527
528         return 1;
529 }
530
531 int test_vpn_create(void)
532 {
533         int rv = 0;
534
535         rv = vpn_create(__test_created_callback, NULL);
536
537         if (rv != VPN_ERROR_NONE) {
538                 printf("Fail to Create VPN Profile [%s]\n",
539                                 __test_convert_error_to_string(rv));
540                 return -1;
541         }
542
543         printf("Success to Create VPN Profile\n");
544
545         return 1;
546 }
547
548 int test_vpn_remove(void)
549 {
550         int rv = 0;
551         vpn_h handle = NULL;
552
553         _test_get_vpn_handle(&handle);
554
555         rv = vpn_remove(handle, __test_removed_callback, NULL);
556
557         if (rv != VPN_ERROR_NONE) {
558                 printf("Fail to Remove VPN Profile [%s]\n",
559                                 __test_convert_error_to_string(rv));
560                 return -1;
561         }
562
563         printf("Success to Remove VPN Profile\n");
564
565         return 1;
566 }
567
568 int test_vpn_set_state_callback(void)
569 {
570         int rv = 0;
571         vpn_h handle = NULL;
572
573         _test_get_vpn_handle(&handle);
574
575         rv = vpn_set_state_callback(handle, __test_state_callback, NULL);
576
577         if (rv != VPN_ERROR_NONE) {
578                 printf("Fail to Set VPN state callback [%s]\n",
579                                 __test_convert_error_to_string(rv));
580                 return -1;
581         }
582
583         printf("Success to Set VPN state callback\n");
584
585         return 1;
586 }
587
588 int test_vpn_unset_state_callback(void)
589 {
590         int rv = 0;
591         vpn_h handle = NULL;
592
593         _test_get_vpn_handle(&handle);
594
595         rv = vpn_unset_state_callback(handle);
596
597         if (rv != VPN_ERROR_NONE) {
598                 printf("Fail to Unset VPN state callback [%s]\n",
599                                 __test_convert_error_to_string(rv));
600                 return -1;
601         }
602
603         printf("Success to Unset VPN state callback\n");
604
605         return 1;
606 }
607
608 int test_vpn_connect(void)
609 {
610         int rv = 0;
611         vpn_h handle = NULL;
612
613         _test_get_vpn_handle(&handle);
614
615         rv = vpn_connect(handle, __test_connect_callback, NULL);
616
617         if (rv != VPN_ERROR_NONE) {
618                 printf("Fail to Connect to VPN Profile [%s]\n",
619                                 __test_convert_error_to_string(rv));
620                 return -1;
621         }
622
623         printf("Success to Connect VPN Profile\n");
624
625         return 1;
626 }
627
628 int test_vpn_disconnect(void)
629 {
630         int rv = 0;
631         vpn_h handle = NULL;
632
633         _test_get_vpn_handle(&handle);
634
635         rv = vpn_disconnect(handle, __test_disconnect_callback, NULL);
636
637         if (rv != VPN_ERROR_NONE) {
638                 printf("Fail to Disconnect from VPN Profile [%s]\n",
639                                 __test_convert_error_to_string(rv));
640                 return -1;
641         }
642
643         printf("Success to Disconnect VPN Profile\n");
644
645         return 1;
646 }
647
648 static int __test_init()
649 {
650         int rv = 0;
651
652         rv = vpn_initialize();
653         if (rv == VPN_ERROR_NONE) {
654                 printf("Register Callbacks if Initialize is Successful\n");
655         } else {
656                 printf("VPN init failed [%s]\n",
657                         __test_convert_error_to_string(rv));
658                 return -1;
659         }
660
661         rv = vpn_settings_init();
662         if (rv != VPN_ERROR_NONE) {
663                 printf("Fail to Initialize Settings [%s]\n",
664                                 __test_convert_error_to_string(rv));
665                 return -1;
666         }
667         return rv;
668 }
669
670 static int __test_deinit()
671 {
672         int rv = 0;
673         int i = 0;
674
675         for (i = 0; i < IPSEC_KVS_MAX; i++)
676                 g_free(ipsec_setting_kvs[i].value);
677
678         rv = vpn_settings_deinit();
679         if (rv != VPN_ERROR_NONE) {
680                 printf("Fail to Deinitialize Settings [%s]\n",
681                                 __test_convert_error_to_string(rv));
682                 return -1;
683         }
684
685         rv = vpn_deinitialize();
686         if (rv != VPN_ERROR_NONE) {
687                 printf("VPN init failed [%s]\n",
688                         __test_convert_error_to_string(rv));
689                 return -1;
690         }
691
692         return rv;
693 }
694
695 #if defined(IPSEC_TEST)
696 static void __init_ipsec_setting_kvs()
697 {
698         int i = 0;
699         for (i = 0; i < IPSEC_KVS_MAX; i++) {
700                 if (ipsec_setting_kvs[i].value) {
701                         g_free(ipsec_setting_kvs[i].value);
702                         ipsec_setting_kvs[i].value = NULL;
703                 }
704         }
705 }
706
707 static char* __get_user_input(char *what)
708 {
709         char buf[128];
710         char *value = NULL;
711
712         printf("Please ENTER %s:", what);
713         if (scanf(" %127[^\n]s", buf) < 0)
714                 printf("Error in Reading the data to Buffer\n");
715         else
716                 value = g_strdup(buf);
717         return value;
718 }
719
720 static void __gen_ipsec_hybrid_rsa_kvs(void)
721 {
722         __init_ipsec_setting_kvs();
723
724         ipsec_setting_kvs[IPSEC_VERSION].value = g_strdup("1");
725         ipsec_setting_kvs[IPSEC_LEFT_ADDRS].value = __get_user_input("Local Address for IPsec");
726         ipsec_setting_kvs[IPSEC_RIGHT_ADDRS].value = __get_user_input("Remote Address for IPsec");
727         ipsec_setting_kvs[IPSEC_LOCAL_AUTH].value = g_strdup("xauth");
728         ipsec_setting_kvs[IPSEC_LOCAL_XAUTH_ID].value = g_strdup("dave");
729         ipsec_setting_kvs[IPSEC_REMOTE_AUTH].value = g_strdup("pubkey");
730         //ipsec_setting_kvs[IPSEC_REMOTE_CERTS].value = __get_user_input("File path for remote cert");
731         ipsec_setting_kvs[IPSEC_CHILDREN_REMOTE_TS].value = g_strdup("0.0.0.0/0");
732         ipsec_setting_kvs[IPSEC_CHILDREN_LOCAL_TS].value = g_strdup("0.0.0.0/0");
733         ipsec_setting_kvs[IPSEC_XAUTH_DATA].value = g_strdup("12345678");
734         ipsec_setting_kvs[IPSEC_XAUTH_OWNERS].value = g_strdup("dave");
735         ipsec_setting_kvs[IPSEC_CERT_TYPE].value = g_strdup("X509");
736         ipsec_setting_kvs[IPSEC_CERT_FLAG].value = g_strdup("CA");
737         ipsec_setting_kvs[IPSEC_CERT_DATA].value = g_strdup_printf("%s%s", CERT_EXAMPLES_DIR, CA_CERT);
738         return;
739 }
740
741 static void __gen_ipsec_xauth_psk_kvs(void)
742 {
743         __init_ipsec_setting_kvs();
744
745         ipsec_setting_kvs[IPSEC_VERSION].value = g_strdup("1");
746         ipsec_setting_kvs[IPSEC_LEFT_ADDRS].value = __get_user_input("Local Address for IPsec");
747         ipsec_setting_kvs[IPSEC_RIGHT_ADDRS].value = __get_user_input("Remote Address for IPsec");
748         ipsec_setting_kvs[IPSEC_LOCAL_AUTH].value = g_strdup("psk");
749         ipsec_setting_kvs[IPSEC_LOCAL_XAUTH_AUTH].value = g_strdup("xauth");
750         ipsec_setting_kvs[IPSEC_LOCAL_XAUTH_XAUTH_ID].value = g_strdup("dave");
751         ipsec_setting_kvs[IPSEC_REMOTE_AUTH].value = g_strdup("psk");
752         ipsec_setting_kvs[IPSEC_CHILDREN_REMOTE_TS].value = g_strdup("0.0.0.0/0");
753         ipsec_setting_kvs[IPSEC_CHILDREN_LOCAL_TS].value = g_strdup("0.0.0.0/0");
754         ipsec_setting_kvs[IPSEC_IKE_DATA].value = g_strdup("ABCDEFGH");
755         ipsec_setting_kvs[IPSEC_IKE_OWNERS].value = g_strdup("0.0.0.0/0");
756         ipsec_setting_kvs[IPSEC_XAUTH_DATA].value = g_strdup("12345678");
757         ipsec_setting_kvs[IPSEC_XAUTH_OWNERS].value = g_strdup("dave");
758         return;
759 }
760 static void __gen_ipsec_xauth_rsa_kvs(void)
761 {
762         __init_ipsec_setting_kvs();
763
764         ipsec_setting_kvs[IPSEC_VERSION].value = g_strdup("1");
765         ipsec_setting_kvs[IPSEC_LEFT_ADDRS].value = __get_user_input("Local Address for IPsec");
766         ipsec_setting_kvs[IPSEC_RIGHT_ADDRS].value = __get_user_input("Remote Address for IPsec");
767         ipsec_setting_kvs[IPSEC_LOCAL_AUTH].value = g_strdup("pubkey");
768         ipsec_setting_kvs[IPSEC_LOCAL_CERTS].value = g_strdup_printf("%s%s", CERT_EXAMPLES_DIR, CLIENT_CERT_PEM);
769         //ipsec_setting_kvs[IPSEC_LOCAL_CERT_PASS].value = __get_user_input("Local Certificate passwd");
770         ipsec_setting_kvs[IPSEC_LOCAL_XAUTH_AUTH].value = g_strdup("xauth");
771         ipsec_setting_kvs[IPSEC_LOCAL_XAUTH_XAUTH_ID].value = g_strdup("dave");
772         ipsec_setting_kvs[IPSEC_REMOTE_AUTH].value = g_strdup("pubkey");
773         //ipsec_setting_kvs[IPSEC_REMOTE_CERTS].value = __get_user_input("File path for remote cert");
774         ipsec_setting_kvs[IPSEC_CHILDREN_REMOTE_TS].value = g_strdup("0.0.0.0/0");
775         ipsec_setting_kvs[IPSEC_CHILDREN_LOCAL_TS].value = g_strdup("0.0.0.0/0");
776         ipsec_setting_kvs[IPSEC_XAUTH_DATA].value = g_strdup("12345678");
777         ipsec_setting_kvs[IPSEC_XAUTH_OWNERS].value = g_strdup("dave");
778         ipsec_setting_kvs[IPSEC_CERT_TYPE].value = g_strdup("X509");
779         ipsec_setting_kvs[IPSEC_CERT_FLAG].value = g_strdup("CA");
780         ipsec_setting_kvs[IPSEC_CERT_DATA].value = g_strdup_printf("%s%s", CERT_EXAMPLES_DIR, CA_CERT);
781         ipsec_setting_kvs[IPSEC_PKEY_TYPE].value = g_strdup("RSA");
782         ipsec_setting_kvs[IPSEC_PKEY_DATA].value = g_strdup_printf("%s%s", CERT_EXAMPLES_DIR, CLIENT_PRIVATE_KEY);
783         return;
784 }
785 static void __gen_ipsec_ikev2_psk_kvs(void)
786 {
787         __init_ipsec_setting_kvs();
788
789         ipsec_setting_kvs[IPSEC_VERSION].value = g_strdup("2");
790         ipsec_setting_kvs[IPSEC_LEFT_ADDRS].value = __get_user_input("Local Address for IPsec");
791         ipsec_setting_kvs[IPSEC_RIGHT_ADDRS].value = __get_user_input("Remote Address for IPsec");
792         ipsec_setting_kvs[IPSEC_LOCAL_AUTH].value = g_strdup("psk");
793         ipsec_setting_kvs[IPSEC_REMOTE_AUTH].value = g_strdup("psk");
794         ipsec_setting_kvs[IPSEC_CHILDREN_REMOTE_TS].value = g_strdup("0.0.0.0/0");
795         ipsec_setting_kvs[IPSEC_CHILDREN_LOCAL_TS].value = g_strdup("0.0.0.0/0");
796         ipsec_setting_kvs[IPSEC_IKE_DATA].value = g_strdup("ABCDEFGH");
797         ipsec_setting_kvs[IPSEC_IKE_OWNERS].value = g_strdup("0.0.0.0/0");
798         return;
799 }
800 static void __gen_ipsec_ikev2_rsa_kvs()
801 {
802         __init_ipsec_setting_kvs();
803
804         ipsec_setting_kvs[IPSEC_VERSION].value = g_strdup("2");
805         ipsec_setting_kvs[IPSEC_LEFT_ADDRS].value = __get_user_input("Local Address for IPsec");
806         ipsec_setting_kvs[IPSEC_RIGHT_ADDRS].value = __get_user_input("Remote Address for IPsec");
807         ipsec_setting_kvs[IPSEC_LOCAL_AUTH].value = g_strdup("pubkey");
808         ipsec_setting_kvs[IPSEC_LOCAL_CERTS].value = g_strdup_printf("%s%s", CERT_EXAMPLES_DIR, CLIENT_CERT_PEM);
809         //ipsec_setting_kvs[IPSEC_LOCAL_CERT_PASS].value = __get_user_input("Local Certificate passwd");
810         ipsec_setting_kvs[IPSEC_REMOTE_AUTH].value = g_strdup("pubkey");
811         //ipsec_setting_kvs[IPSEC_REMOTE_CERTS].value = __get_user_input("File path for remote cert");
812         ipsec_setting_kvs[IPSEC_CHILDREN_REMOTE_TS].value = g_strdup("0.0.0.0/0");
813         ipsec_setting_kvs[IPSEC_CHILDREN_LOCAL_TS].value = g_strdup("0.0.0.0/0");
814         ipsec_setting_kvs[IPSEC_CERT_TYPE].value = g_strdup("X509");
815         ipsec_setting_kvs[IPSEC_CERT_FLAG].value = g_strdup("CA");
816         ipsec_setting_kvs[IPSEC_CERT_DATA].value = g_strdup_printf("%s%s", CERT_EXAMPLES_DIR, CA_CERT);
817         //ipsec_setting_kvs[IPSEC_CERT_PASS].value = __get_user_input("Certificate passwd");
818         ipsec_setting_kvs[IPSEC_PKEY_TYPE].value = g_strdup("RSA");
819         ipsec_setting_kvs[IPSEC_PKEY_DATA].value = g_strdup_printf("%s%s", CERT_EXAMPLES_DIR, CLIENT_PRIVATE_KEY);
820         return;
821 }
822
823 static int __set_vpn_settings_ipsec_kvs()
824 {
825         int rv = 0;
826         int i = 0;
827         for (i = 0; i < IPSEC_KVS_MAX; i++) {
828                 if (ipsec_setting_kvs[i].value) {
829                         if (i == IPSEC_RIGHT_ADDRS) 
830                                 rv = vpn_settings_set_host(ipsec_setting_kvs[i].value);
831                         else
832                                 rv = vpn_settings_set_specific(ipsec_setting_kvs[i].key, ipsec_setting_kvs[i].value);
833                 }
834                 if (rv != VPN_ERROR_NONE)
835                         break;
836         }
837         return rv;
838 }
839
840 int test_create_ipsec(gpointer data, ipsec_type_e type)
841 {
842         gen_ipsec_settings_f *gen_ipsec_settings = (gen_ipsec_settings_f *)data;
843         int rv = 0;
844
845         printf("create IPsec type [%s]\n", __get_ipsec_name(type));
846
847         rv = __test_init();
848         if (rv != VPN_ERROR_NONE)
849                 return -1;
850
851         rv = test_vpn_settings_add("ipsec", __get_ipsec_name(type),
852                         "1.1.1.1", "default_domain@strongswan.org");
853         if (rv < 0)
854                 return -1;
855
856         gen_ipsec_settings[type]();
857         rv = __set_vpn_settings_ipsec_kvs();
858         if (rv != VPN_ERROR_NONE) {
859                 printf("Fail to __set_vpn_settings_ipsec_kvs [%s]\n",
860                                 __test_convert_error_to_string(rv));
861                 __init_ipsec_setting_kvs();
862                 __test_deinit();
863                 return -1;
864         }
865
866         rv = vpn_create(__test_created_callback, NULL);
867
868         if (rv != VPN_ERROR_NONE) {
869                 printf("Fail to Create VPN Profile [%s]\n",
870                                 __test_convert_error_to_string(rv));
871                 __init_ipsec_setting_kvs();
872                 __test_deinit();
873                 return -1;
874         }
875
876         printf("Success to Create VPN Profile\n");
877
878         rv = __test_deinit();
879         if (rv != VPN_ERROR_NONE)
880                 return -1;
881
882         return 1;
883 }
884 #endif
885
886 int test_create_wireguard(void)
887 {
888         int rv = 0;
889         int i;
890
891         printf("Creating Wireguard VPN Profile\n");
892
893         rv = test_vpn_settings_add("wireguard", "wireguard_test_client",
894                         NULL, NULL);
895         if (rv < 0) {
896                 printf("Fail to add wireguard vpn settings\n");
897                 return -1;
898         }
899
900         for (i = 0; i < WIREGUARD_KVS_MAX; i++) {
901                 char value[MAX_USER_INPUT_LEN + 1];
902                 if (wg_setting_kvs[i].value == NULL)
903                         _test_get_user_input(&value[0], wg_setting_kvs[i].key);
904                 else
905                         g_strlcpy(value, wg_setting_kvs[i].value, sizeof(value));
906
907                 rv = _test_vpn_settings_set_specific(wg_setting_kvs[i].key, value);
908                 if (rv < 0) {
909                         printf("Fail to set wireguard vpn settings");
910                         __test_deinit();
911                         return -1;
912                 }
913         }
914
915         rv = vpn_create(__test_created_callback, NULL);
916         if (rv != VPN_ERROR_NONE) {
917                 printf("Fail to Create VPN Profile [%s]\n",
918                                 __test_convert_error_to_string(rv));
919                 __init_ipsec_setting_kvs();
920                 __test_deinit();
921                 return -1;
922         }
923
924         printf("Success to Create Wireguard VPN profile\n");
925
926         return 1;
927 }
928
929 int test_destroy_wireguard(void)
930 {
931         return test_vpn_remove();
932 }
933
934 int main(int argc, char **argv)
935 {
936         GMainLoop *mainloop;
937
938 #if defined(IPSEC_TEST)
939         gen_ipsec_settings_f gen_ipsec_settings[IPSEC_MAX];
940
941         gen_ipsec_settings[IPSEC_HYBRID_RSA] = __gen_ipsec_hybrid_rsa_kvs;
942         gen_ipsec_settings[IPSEC_XAUTH_PSK] = __gen_ipsec_xauth_psk_kvs;
943         gen_ipsec_settings[IPSEC_XAUTH_RSA] = __gen_ipsec_xauth_rsa_kvs;
944         gen_ipsec_settings[IPSEC_IKEV2_PSK] = __gen_ipsec_ikev2_psk_kvs;
945         gen_ipsec_settings[IPSEC_IKEV2_RSA] = __gen_ipsec_ikev2_rsa_kvs;
946 #endif
947
948         mainloop = g_main_loop_new(NULL, FALSE);
949
950         GIOChannel *channel = g_io_channel_unix_new(0);
951 #if defined(IPSEC_TEST)
952         g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL),
953                         test_thread, gen_ipsec_settings);
954 #else
955         g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL),
956                         test_thread, NULL);
957 #endif
958
959         printf("Test Thread created...\n");
960
961         g_main_loop_run(mainloop);
962
963         return 0;
964 }
965
966 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
967 {
968         int rv;
969         char a[10];
970
971         printf("Event received from stdin\n");
972
973         rv = read(0, a, 10);
974
975         if (rv <= 0 || a[0] == '0') {
976                 rv = vpn_deinitialize();
977
978                 if (rv != VPN_ERROR_NONE)
979                         printf("Fail to deinitialize.\n");
980
981                 exit(1);
982         }
983
984         if (a[0] == '\n' || a[0] == '\r') {
985                 printf("\n\n Network Connection API Test App\n\n");
986                 printf("Options..\n");
987                 printf("1\t- VPN init and set callbacks\n");
988                 printf("2\t- VPN deinit(unset callbacks automatically)\n");
989                 printf("3\t- VPN Settings Initialize - Initialize Settings for Creating a VPN profile\n");
990                 printf("4\t- VPN Settings Delete - Delete Settings VPN profile\n");
991                 printf("5\t- VPN Settings Set Specific - Allows to add a specific setting\n");
992                 printf("6\t- VPN Settings Add - Add Type,Host,Name,Domain settings\n");
993                 printf("7\t- VPN Create - Creates the VPN profile\n");
994                 printf("8\t- VPN Remove - Removes the VPN profile\n");
995                 printf("9\t- VPN Connect - Connect the VPN profile\n");
996                 printf("a\t- VPN Disconnect - Disconnect the VPN profile\n");
997 #if defined(IPSEC_TEST)
998                 printf("b\t- VPN Test Create IPSec Hybrid RSA - Create IPSec Hybrid RSA\n");
999                 printf("c\t- VPN Test Create IPSec Xauth PSK - Create IPSec Xauth PSK\n");
1000                 printf("d\t- VPN Test Create IPSec Xauth RSA - Create IPSec Xauth RSA\n");
1001                 printf("e\t- VPN Test Create IPSec IKEv2 PSK - Create IPSec IKEv2 PSK\n");
1002                 printf("f\t- VPN Test Create IPSec IKEv2 RSA - Create IPSec IKEv2 RSA\n");
1003 #endif
1004                 printf("g\t- VPN Set state callback - Set the VPN state callback\n");
1005                 printf("h\t- VPN Unset state callback - Unset the VPN state callback\n");
1006                 printf("i\t- Create Wireguard VPN tunnel\n");
1007                 printf("j\t- Destroy Wireguard VPN tunnel\n");
1008                 printf("0\t- Exit\n");
1009
1010                 printf("ENTER  - Show options menu.......\n");
1011         }
1012
1013         switch (a[0]) {
1014         case '1':
1015                 rv = test_vpn_init();
1016                 break;
1017         case '2':
1018                 rv = test_vpn_deinit();
1019                 break;
1020         case '3':
1021                 rv = test_vpn_settings_init();
1022                 break;
1023         case '4':
1024                 rv = test_vpn_settings_deinit();
1025                 break;
1026         case '5':
1027                 rv = test_vpn_settings_set_specific();
1028                 break;
1029         case '6':
1030                 rv = test_vpn_settings_add(NULL, NULL, NULL, NULL);
1031                 break;
1032         case '7':
1033                 rv = test_vpn_create();
1034                 break;
1035         case '8':
1036                 rv = test_vpn_remove();
1037                 break;
1038         case '9':
1039                 rv = test_vpn_connect();
1040                 break;
1041         case 'a':
1042                 rv = test_vpn_disconnect();
1043                 break;
1044 #if defined(IPSEC_TEST)
1045         case 'b':
1046                 rv = test_create_ipsec(data, IPSEC_HYBRID_RSA);
1047                 break;
1048         case 'c':
1049                 rv = test_create_ipsec(data, IPSEC_XAUTH_PSK);
1050                 break;
1051         case 'd':
1052                 rv = test_create_ipsec(data, IPSEC_XAUTH_RSA);
1053                 break;
1054         case 'e':
1055                 rv = test_create_ipsec(data, IPSEC_IKEV2_PSK);
1056                 break;
1057         case 'f':
1058                 rv = test_create_ipsec(data, IPSEC_IKEV2_RSA);
1059                 break;
1060 #endif
1061         case 'g':
1062                 rv = test_vpn_set_state_callback();
1063                 break;
1064         case 'h':
1065                 rv = test_vpn_unset_state_callback();
1066                 break;
1067         case 'i':
1068                 rv = test_create_wireguard();
1069                 break;
1070         case 'j':
1071                 rv = test_destroy_wireguard();
1072                 break;
1073         default:
1074                 break;
1075         }
1076
1077         if (rv == 1)
1078                 printf("Operation succeeded!\n");
1079         else
1080                 printf("Operation failed!\n");
1081
1082         return TRUE;
1083 }
1084
1085