2 * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <sys/socket.h>
27 #include <sys/ioctl.h>
31 #include <tizen_error.h>
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"
42 #define MAX_USER_INPUT_LEN 200
44 gboolean test_thread(GIOChannel *source,
45 GIOCondition condition,
48 static const char *__test_convert_error_to_string(vpn_error_e err_type)
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:
73 case VPN_ERROR_INVALID_KEY:
75 case VPN_ERROR_NO_REPLY:
77 case VPN_ERROR_SECURITY_RESTRICTED:
78 return "SECURITY_RESTRICTED";
85 static const char *__test_convert_state_to_string(vpn_state_e state_type)
92 case VPN_STATE_CONFIGURATION:
93 return "CONFIGURATION";
94 case VPN_STATE_DISCONNECT:
96 case VPN_STATE_FAILURE:
98 case VPN_STATE_UNKNOWN:
110 #if defined(IPSEC_TEST)
120 static const char *__get_ipsec_name(ipsec_type_e 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";
145 IPSEC_LOCAL_CERT_PASS,
147 IPSEC_LOCAL_XAUTH_ID,
148 IPSEC_LOCAL_XAUTH_AUTH,
149 IPSEC_LOCAL_XAUTH_XAUTH_ID,
152 IPSEC_REMOTE_CERT_PASS,
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,
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},
207 typedef void (*gen_ipsec_settings_f)(void);
211 * Note: Below template is to set Wireguard VPN Settings
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)
229 WIREGUARD_PRIVATE_KEY,
230 WIREGUARD_PUBLIC_KEY,
231 WIREGUARD_ALLOWED_IPS,
232 WIREGUARD_ENDPOINT_PORT,
233 WIREGUARD_PERSISTENT_KEEPALIVE,
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"},
247 static void __test_created_callback(vpn_error_e result,
250 if (result == VPN_ERROR_NONE)
251 printf("VPN Create Succeeded\n");
253 printf("VPN Create Failed! error : %s",
254 __test_convert_error_to_string(result));
257 static void __test_removed_callback(vpn_error_e result,
260 if (result == VPN_ERROR_NONE)
261 printf("VPN Remove Succeeded\n");
263 printf("VPN Remove Failed! error : %s",
264 __test_convert_error_to_string(result));
267 static void __test_connect_callback(vpn_error_e result,
270 if (result == VPN_ERROR_NONE)
271 printf("VPN Connect Succeeded\n");
273 printf("VPN Connect Failed! error : %s",
274 __test_convert_error_to_string(result));
277 static void __test_disconnect_callback(vpn_error_e result,
280 if (result == VPN_ERROR_NONE)
281 printf("VPN Disconnect Succeeded\n");
283 printf("VPN Disconnect Failed! error : %s",
284 __test_convert_error_to_string(result));
287 static void __test_state_callback(vpn_h handle_ptr, vpn_state_e state, void *user_data)
289 const char *name = NULL;
290 const char *type = NULL;
291 const char *host = NULL;
292 const char *domain = NULL;
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));
307 static void _test_get_vpn_handle(vpn_h *handle_ptr)
309 assert(handle_ptr != NULL);
311 const char *name = NULL;
312 const char *type = NULL;
313 const char *host = NULL;
314 const char *domain = NULL;
317 GList *handles = vpn_get_vpn_handle_list();
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");
333 printf("==================================\n");
334 printf(" Please ENTER Number: ");
335 if (scanf(" %d", &num) < 0)
336 printf("Error in Reading Host String\n");
340 for (iter = handles; iter != NULL; iter = iter->next) {
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);
356 static void _test_get_user_input(char *buf, char *what)
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");
366 int test_vpn_init(void)
368 int rv = vpn_initialize();
370 if (rv == VPN_ERROR_NONE) {
371 printf("Register Callbacks if Initialize is Successful\n");
373 printf("VPN init failed [%s]\n",
374 __test_convert_error_to_string(rv));
378 printf("VPN init succeeded\n");
382 int test_vpn_deinit(void)
384 int rv = vpn_deinitialize();
386 if (rv != VPN_ERROR_NONE) {
387 printf("VPN init failed [%s]\n",
388 __test_convert_error_to_string(rv));
392 printf("VPN deinit succeeded\n");
396 int test_vpn_settings_init(void)
400 rv = vpn_settings_init();
402 if (rv != VPN_ERROR_NONE) {
403 printf("Fail to Initialize Settings [%s]\n",
404 __test_convert_error_to_string(rv));
408 printf("Success Creating Settings API's\n");
413 int test_vpn_settings_add(const char *type, const char *name, const char *host, const char *domain)
416 char buf[MAX_USER_INPUT_LEN + 1];
419 _test_get_user_input(&buf[0], "Type");
420 rv = vpn_settings_set_type(&buf[0]);
422 rv = vpn_settings_set_type(type);
424 if (rv != VPN_ERROR_NONE) {
425 printf("Fail to VPN Settings Type[%s]\n",
426 __test_convert_error_to_string(rv));
431 _test_get_user_input(&buf[0], "Name");
432 rv = vpn_settings_set_name(&buf[0]);
434 rv = vpn_settings_set_name(name);
436 if (rv != VPN_ERROR_NONE) {
437 printf("Fail to VPN Settings Name[%s]\n",
438 __test_convert_error_to_string(rv));
443 _test_get_user_input(&buf[0], "Host");
444 rv = vpn_settings_set_host(&buf[0]);
446 rv = vpn_settings_set_host(host);
448 if (rv != VPN_ERROR_NONE) {
449 printf("Fail to VPN Settings Host[%s]\n",
450 __test_convert_error_to_string(rv));
454 if (!g_strcmp0(type, "wireguard"))
457 if (domain == NULL) {
458 _test_get_user_input(&buf[0], "Domain");
459 rv = vpn_settings_set_domain(&buf[0]);
461 rv = vpn_settings_set_domain(domain);
463 if (rv != VPN_ERROR_NONE) {
464 printf("Fail to VPN Settings Domain[%s]\n",
465 __test_convert_error_to_string(rv));
469 printf("Success Creating Settings API's\n");
474 int test_vpn_settings_deinit(void)
478 rv = vpn_settings_deinit();
480 if (rv != VPN_ERROR_NONE) {
481 printf("Fail to Deinitialize Settings [%s]\n",
482 __test_convert_error_to_string(rv));
486 printf("Success Deleting Settings API's\n");
491 int test_vpn_settings_set_specific(void)
494 char key[MAX_USER_INPUT_LEN + 1];
495 char value[MAX_USER_INPUT_LEN + 1];
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));
506 printf("Success in VPN Settings Add %s=%s\n", key, value);
511 int _test_vpn_settings_set_specific(const char *key, const char *value)
514 if (key == NULL || value == NULL) {
515 printf("Invalid parameters\n");
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));
526 printf("Success in VPN Settings Add %s=%s\n", key, value);
531 int test_vpn_create(void)
535 rv = vpn_create(__test_created_callback, NULL);
537 if (rv != VPN_ERROR_NONE) {
538 printf("Fail to Create VPN Profile [%s]\n",
539 __test_convert_error_to_string(rv));
543 printf("Success to Create VPN Profile\n");
548 int test_vpn_remove(void)
553 _test_get_vpn_handle(&handle);
555 rv = vpn_remove(handle, __test_removed_callback, NULL);
557 if (rv != VPN_ERROR_NONE) {
558 printf("Fail to Remove VPN Profile [%s]\n",
559 __test_convert_error_to_string(rv));
563 printf("Success to Remove VPN Profile\n");
568 int test_vpn_set_state_callback(void)
573 _test_get_vpn_handle(&handle);
575 rv = vpn_set_state_callback(handle, __test_state_callback, NULL);
577 if (rv != VPN_ERROR_NONE) {
578 printf("Fail to Set VPN state callback [%s]\n",
579 __test_convert_error_to_string(rv));
583 printf("Success to Set VPN state callback\n");
588 int test_vpn_unset_state_callback(void)
593 _test_get_vpn_handle(&handle);
595 rv = vpn_unset_state_callback(handle);
597 if (rv != VPN_ERROR_NONE) {
598 printf("Fail to Unset VPN state callback [%s]\n",
599 __test_convert_error_to_string(rv));
603 printf("Success to Unset VPN state callback\n");
608 int test_vpn_connect(void)
613 _test_get_vpn_handle(&handle);
615 rv = vpn_connect(handle, __test_connect_callback, NULL);
617 if (rv != VPN_ERROR_NONE) {
618 printf("Fail to Connect to VPN Profile [%s]\n",
619 __test_convert_error_to_string(rv));
623 printf("Success to Connect VPN Profile\n");
628 int test_vpn_disconnect(void)
633 _test_get_vpn_handle(&handle);
635 rv = vpn_disconnect(handle, __test_disconnect_callback, NULL);
637 if (rv != VPN_ERROR_NONE) {
638 printf("Fail to Disconnect from VPN Profile [%s]\n",
639 __test_convert_error_to_string(rv));
643 printf("Success to Disconnect VPN Profile\n");
648 static int __test_init()
652 rv = vpn_initialize();
653 if (rv == VPN_ERROR_NONE) {
654 printf("Register Callbacks if Initialize is Successful\n");
656 printf("VPN init failed [%s]\n",
657 __test_convert_error_to_string(rv));
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));
670 static int __test_deinit()
675 for (i = 0; i < IPSEC_KVS_MAX; i++)
676 g_free(ipsec_setting_kvs[i].value);
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));
685 rv = vpn_deinitialize();
686 if (rv != VPN_ERROR_NONE) {
687 printf("VPN init failed [%s]\n",
688 __test_convert_error_to_string(rv));
695 #if defined(IPSEC_TEST)
696 static void __init_ipsec_setting_kvs()
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;
707 static char* __get_user_input(char *what)
712 printf("Please ENTER %s:", what);
713 if (scanf(" %127[^\n]s", buf) < 0)
714 printf("Error in Reading the data to Buffer\n");
716 value = g_strdup(buf);
720 static void __gen_ipsec_hybrid_rsa_kvs(void)
722 __init_ipsec_setting_kvs();
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);
741 static void __gen_ipsec_xauth_psk_kvs(void)
743 __init_ipsec_setting_kvs();
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");
760 static void __gen_ipsec_xauth_rsa_kvs(void)
762 __init_ipsec_setting_kvs();
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);
785 static void __gen_ipsec_ikev2_psk_kvs(void)
787 __init_ipsec_setting_kvs();
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");
800 static void __gen_ipsec_ikev2_rsa_kvs()
802 __init_ipsec_setting_kvs();
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);
823 static int __set_vpn_settings_ipsec_kvs()
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);
832 rv = vpn_settings_set_specific(ipsec_setting_kvs[i].key, ipsec_setting_kvs[i].value);
834 if (rv != VPN_ERROR_NONE)
840 int test_create_ipsec(gpointer data, ipsec_type_e type)
842 gen_ipsec_settings_f *gen_ipsec_settings = (gen_ipsec_settings_f *)data;
845 printf("create IPsec type [%s]\n", __get_ipsec_name(type));
848 if (rv != VPN_ERROR_NONE)
851 rv = test_vpn_settings_add("ipsec", __get_ipsec_name(type),
852 "1.1.1.1", "default_domain@strongswan.org");
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();
866 rv = vpn_create(__test_created_callback, NULL);
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();
876 printf("Success to Create VPN Profile\n");
878 rv = __test_deinit();
879 if (rv != VPN_ERROR_NONE)
886 int test_create_wireguard(void)
891 printf("Creating Wireguard VPN Profile\n");
893 rv = test_vpn_settings_add("wireguard", "wireguard_test_client",
896 printf("Fail to add wireguard vpn settings\n");
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);
905 g_strlcpy(value, wg_setting_kvs[i].value, sizeof(value));
907 rv = _test_vpn_settings_set_specific(wg_setting_kvs[i].key, value);
909 printf("Fail to set wireguard vpn settings");
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();
924 printf("Success to Create Wireguard VPN profile\n");
929 int test_destroy_wireguard(void)
931 return test_vpn_remove();
934 int main(int argc, char **argv)
938 #if defined(IPSEC_TEST)
939 gen_ipsec_settings_f gen_ipsec_settings[IPSEC_MAX];
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;
948 mainloop = g_main_loop_new(NULL, FALSE);
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);
955 g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL),
959 printf("Test Thread created...\n");
961 g_main_loop_run(mainloop);
966 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
971 printf("Event received from stdin\n");
975 if (rv <= 0 || a[0] == '0') {
976 rv = vpn_deinitialize();
978 if (rv != VPN_ERROR_NONE)
979 printf("Fail to deinitialize.\n");
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");
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");
1010 printf("ENTER - Show options menu.......\n");
1015 rv = test_vpn_init();
1018 rv = test_vpn_deinit();
1021 rv = test_vpn_settings_init();
1024 rv = test_vpn_settings_deinit();
1027 rv = test_vpn_settings_set_specific();
1030 rv = test_vpn_settings_add(NULL, NULL, NULL, NULL);
1033 rv = test_vpn_create();
1036 rv = test_vpn_remove();
1039 rv = test_vpn_connect();
1042 rv = test_vpn_disconnect();
1044 #if defined(IPSEC_TEST)
1046 rv = test_create_ipsec(data, IPSEC_HYBRID_RSA);
1049 rv = test_create_ipsec(data, IPSEC_XAUTH_PSK);
1052 rv = test_create_ipsec(data, IPSEC_XAUTH_RSA);
1055 rv = test_create_ipsec(data, IPSEC_IKEV2_PSK);
1058 rv = test_create_ipsec(data, IPSEC_IKEV2_RSA);
1062 rv = test_vpn_set_state_callback();
1065 rv = test_vpn_unset_state_callback();
1068 rv = test_create_wireguard();
1071 rv = test_destroy_wireguard();
1078 printf("Operation succeeded!\n");
1080 printf("Operation failed!\n");