From: Nishant Chaprana Date: Tue, 6 Jul 2021 10:20:31 +0000 (+0530) Subject: Add wireguard related changes and test cases X-Git-Tag: accepted/tizen/6.5/unified/20211028.101540^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F36%2F261336%2F3;p=platform%2Fcore%2Fapi%2Fvpn-setting.git Add wireguard related changes and test cases Change-Id: Ic67d1ab869a814a4778d608f1983e8f03b723387 Signed-off-by: Nishant Chaprana --- diff --git a/dvpnlib/src/dvpnlib-vpn-connnection.c b/dvpnlib/src/dvpnlib-vpn-connnection.c index 3473a89..436ed47 100755 --- a/dvpnlib/src/dvpnlib-vpn-connnection.c +++ b/dvpnlib/src/dvpnlib-vpn-connnection.c @@ -805,7 +805,7 @@ GList *vpn_get_connections(void) struct vpn_connection *vpn_get_connection( const char *name, const char *host, const char *domain) { - if (!name || !host || !domain) + if (!name || !host) return NULL; GList *iter; @@ -817,7 +817,7 @@ struct vpn_connection *vpn_get_connection( if (g_str_equal(connection->name, name) && g_str_equal(connection->host, host) && - g_str_equal(connection->domain, domain)) + (!domain || g_str_equal(connection->domain, domain))) return connection; } diff --git a/src/vpn-internal.c b/src/vpn-internal.c index dcfe898..2142917 100755 --- a/src/vpn-internal.c +++ b/src/vpn-internal.c @@ -360,7 +360,7 @@ int _vpn_get_vpn_handle(const char *name, const char *host, const char *domain, struct vpn_connection *connection = vpn_get_connection(name, host, domain); if (connection == NULL) { - VPN_LOG(VPN_ERROR, "name=%s host=%s domain=%s", name, host, domain); + VPN_LOG(VPN_ERROR, "name=%s host=%s domain=%s", name, host, domain ? domain : "NULL"); return VPN_ERROR_INVALID_PARAMETER; } diff --git a/src/vpn.c b/src/vpn.c index c3a7530..eb966a7 100755 --- a/src/vpn.c +++ b/src/vpn.c @@ -288,7 +288,8 @@ int vpn_get_vpn_handle(const char *name, const char *host, const char *domain, v return VPN_ERROR_INVALID_OPERATION; } - if (name == NULL || host == NULL || domain == NULL || handle == NULL) + // Note: Domain is optional parameter at connman + if (name == NULL || host == NULL || handle == NULL) return VPN_ERROR_INVALID_PARAMETER; rv = _vpn_get_vpn_handle(name, host, domain, handle); diff --git a/test/vpn_test.c b/test/vpn_test.c index 804d6f8..5e01d21 100755 --- a/test/vpn_test.c +++ b/test/vpn_test.c @@ -102,6 +102,11 @@ static const char *__test_convert_state_to_string(vpn_state_e state_type) return "UNKNOWN"; } +typedef struct { + char *key; + char *value; +} kv_s; + #if defined(IPSEC_TEST) typedef enum { IPSEC_HYBRID_RSA, @@ -131,12 +136,6 @@ static const char *__get_ipsec_name(ipsec_type_e type) } } -typedef struct { - char *key; - char *value; -} kv_s; - - typedef enum { IPSEC_VERSION, IPSEC_LEFT_ADDRS, @@ -208,6 +207,43 @@ kv_s ipsec_setting_kvs[] = { typedef void (*gen_ipsec_settings_f)(void); #endif +/** + * Note: Below template is to set Wireguard VPN Settings + * + * Type = wireguard + * Name = + * Host = + * WireGuard.Address = + * WireGuard.PrivateKey = + * WireGuard.PublicKey = + * WireGuard.AllowedIPs = + * WireGuard.EndpointPort = (Optional) + * WireGuard.ListenPort = (Optional) + * WireGuard.DNS = (Optional) + * WireGuard.PresharedKey = (Optional) + * WireGuard.PersistentKeepalive = (Optional) + */ + +typedef enum { + WIREGUARD_ADDRESS, + WIREGUARD_PRIVATE_KEY, + WIREGUARD_PUBLIC_KEY, + WIREGUARD_ALLOWED_IPS, + WIREGUARD_ENDPOINT_PORT, + WIREGUARD_PERSISTENT_KEEPALIVE, + WIREGUARD_KVS_MAX +} wireguard_kv_e; + +kv_s wg_setting_kvs[] = { + {"WireGuard.Address", NULL}, + {"WireGuard.PrivateKey", NULL}, + {"WireGuard.PublicKey", NULL}, + {"WireGuard.AllowedIPs", NULL}, + {"WireGuard.EndpointPort", "51820"}, + {"WireGuard.PersistentKeepalive", "10"}, + {NULL, NULL}, +}; + static void __test_created_callback(vpn_error_e result, void *user_data) { @@ -291,7 +327,7 @@ static void _test_get_vpn_handle(vpn_h *handle_ptr) printf(" Name[%p] - %s\n", iter->data, name); printf(" Type[%p] - %s\n", iter->data, type); printf(" Host[%p] - %s\n", iter->data, host); - printf(" Domain[%p] - %s\n", iter->data, domain); + printf(" Domain[%p] - %s\n", iter->data, domain ? domain : "NULL"); } printf("==================================\n"); @@ -374,37 +410,56 @@ int test_vpn_settings_init(void) return 1; } -int test_vpn_settings_add(void) +int test_vpn_settings_add(const char *type, const char *name, const char *host, const char *domain) { int rv = 0; char buf[MAX_USER_INPUT_LEN + 1]; - _test_get_user_input(&buf[0], "Type"); - rv = vpn_settings_set_type(&buf[0]); + if (type == NULL) { + _test_get_user_input(&buf[0], "Type"); + rv = vpn_settings_set_type(&buf[0]); + } else { + rv = vpn_settings_set_type(type); + } if (rv != VPN_ERROR_NONE) { printf("Fail to VPN Settings Type[%s]\n", __test_convert_error_to_string(rv)); return -1; } - _test_get_user_input(&buf[0], "Name"); - rv = vpn_settings_set_name(&buf[0]); + if (name == NULL) { + _test_get_user_input(&buf[0], "Name"); + rv = vpn_settings_set_name(&buf[0]); + } else { + rv = vpn_settings_set_name(name); + } if (rv != VPN_ERROR_NONE) { printf("Fail to VPN Settings Name[%s]\n", __test_convert_error_to_string(rv)); return -1; } - _test_get_user_input(&buf[0], "Host"); - rv = vpn_settings_set_host(&buf[0]); + if (host == NULL) { + _test_get_user_input(&buf[0], "Host"); + rv = vpn_settings_set_host(&buf[0]); + } else { + rv = vpn_settings_set_host(host); + } if (rv != VPN_ERROR_NONE) { printf("Fail to VPN Settings Host[%s]\n", __test_convert_error_to_string(rv)); return -1; } - _test_get_user_input(&buf[0], "Domain"); - rv = vpn_settings_set_domain(&buf[0]); + if (!g_strcmp0(type, "wireguard")) + return 1; + + if (domain == NULL) { + _test_get_user_input(&buf[0], "Domain"); + rv = vpn_settings_set_domain(&buf[0]); + } else { + rv = vpn_settings_set_domain(domain); + } if (rv != VPN_ERROR_NONE) { printf("Fail to VPN Settings Domain[%s]\n", __test_convert_error_to_string(rv)); @@ -453,6 +508,26 @@ int test_vpn_settings_set_specific(void) return 1; } +int _test_vpn_settings_set_specific(const char *key, const char *value) +{ + int rv = 0; + if (key == NULL || value == NULL) { + printf("Invalid parameters\n"); + return -1; + } + + rv = vpn_settings_set_specific(key, value); + if (rv != VPN_ERROR_NONE) { + printf("Fail to Set Specific VPN Settings %s[%s]\n", + key, __test_convert_error_to_string(rv)); + return -1; + } + + printf("Success in VPN Settings Add %s=%s\n", key, value); + + return 1; +} + int test_vpn_create(void) { int rv = 0; @@ -570,7 +645,6 @@ int test_vpn_disconnect(void) return 1; } -#if defined(IPSEC_TEST) static int __test_init() { int rv = 0; @@ -618,40 +692,7 @@ static int __test_deinit() return rv; } -static int __test_add(ipsec_type_e type) -{ - int rv = 0; - - rv = vpn_settings_set_type("ipsec"); - if (rv != VPN_ERROR_NONE) { - printf("Fail to VPN Settings Type[%s]\n", - __test_convert_error_to_string(rv)); - return -1; - } - - rv = vpn_settings_set_name(__get_ipsec_name(type)); - if (rv != VPN_ERROR_NONE) { - printf("Fail to VPN Settings Name[%s]\n", - __test_convert_error_to_string(rv)); - return -1; - } - - rv = vpn_settings_set_host("1.1.1.1"); - if (rv != VPN_ERROR_NONE) { - printf("Fail to VPN Settings Host[%s]\n", - __test_convert_error_to_string(rv)); - return -1; - } - - rv = vpn_settings_set_domain("default_domain@strongswan.org"); - if (rv != VPN_ERROR_NONE) { - printf("Fail to VPN Settings Domain[%s]\n", - __test_convert_error_to_string(rv)); - return -1; - } - return rv; -} - +#if defined(IPSEC_TEST) static void __init_ipsec_setting_kvs() { int i = 0; @@ -807,8 +848,9 @@ int test_create_ipsec(gpointer data, ipsec_type_e type) if (rv != VPN_ERROR_NONE) return -1; - rv = __test_add(type); - if (rv != VPN_ERROR_NONE) + rv = test_vpn_settings_add("ipsec", __get_ipsec_name(type), + "1.1.1.1", "default_domain@strongswan.org"); + if (rv < 0) return -1; gen_ipsec_settings[type](); @@ -841,6 +883,54 @@ int test_create_ipsec(gpointer data, ipsec_type_e type) } #endif +int test_create_wireguard(void) +{ + int rv = 0; + int i; + + printf("Creating Wireguard VPN Profile\n"); + + rv = test_vpn_settings_add("wireguard", "wireguard_test_client", + NULL, NULL); + if (rv < 0) { + printf("Fail to add wireguard vpn settings\n"); + return -1; + } + + for (i = 0; i < WIREGUARD_KVS_MAX; i++) { + char value[MAX_USER_INPUT_LEN + 1]; + if (wg_setting_kvs[i].value == NULL) + _test_get_user_input(&value[0], wg_setting_kvs[i].key); + else + g_strlcpy(value, wg_setting_kvs[i].value, sizeof(value)); + + rv = _test_vpn_settings_set_specific(wg_setting_kvs[i].key, value); + if (rv < 0) { + printf("Fail to set wireguard vpn settings"); + __test_deinit(); + return -1; + } + } + + rv = vpn_create(__test_created_callback, NULL); + if (rv != VPN_ERROR_NONE) { + printf("Fail to Create VPN Profile [%s]\n", + __test_convert_error_to_string(rv)); + __init_ipsec_setting_kvs(); + __test_deinit(); + return -1; + } + + printf("Success to Create Wireguard VPN profile\n"); + + return 1; +} + +int test_destroy_wireguard(void) +{ + return test_vpn_remove(); +} + int main(int argc, char **argv) { GMainLoop *mainloop; @@ -913,6 +1003,8 @@ gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data) #endif printf("g\t- VPN Set state callback - Set the VPN state callback\n"); printf("h\t- VPN Unset state callback - Unset the VPN state callback\n"); + printf("i\t- Create Wireguard VPN tunnel\n"); + printf("j\t- Destroy Wireguard VPN tunnel\n"); printf("0\t- Exit\n"); printf("ENTER - Show options menu.......\n"); @@ -935,7 +1027,7 @@ gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data) rv = test_vpn_settings_set_specific(); break; case '6': - rv = test_vpn_settings_add(); + rv = test_vpn_settings_add(NULL, NULL, NULL, NULL); break; case '7': rv = test_vpn_create(); @@ -972,6 +1064,12 @@ gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data) case 'h': rv = test_vpn_unset_state_callback(); break; + case 'i': + rv = test_create_wireguard(); + break; + case 'j': + rv = test_destroy_wireguard(); + break; default: break; }