Add IPsec specific test code 78/116378/1
authorYu Jiung <jiung.yu@samsung.com>
Fri, 24 Feb 2017 05:54:04 +0000 (14:54 +0900)
committerYu Jiung <jiung.yu@samsung.com>
Fri, 24 Feb 2017 05:54:10 +0000 (14:54 +0900)
Change-Id: I89f4989fc00e2732951397d96ea9b6e54cd77d97
Signed-off-by: Yu jiung <jiung.yu@samsung.com>
test/vpn_test.c

index a2641319ed5e3862ac13ed7aa07fbc8b24497ba6..0cf47b09f755faeb7dd8f35b87f9f988a55a1faa 100755 (executable)
@@ -70,6 +70,74 @@ static const char *__test_convert_error_to_string(vpn_error_e err_type)
        return "UNKNOWN";
 }
 
+typedef enum {
+       IPSEC_HYBRID_RSA,
+       IPSEC_XAUTH_PSK,
+       IPSEC_XAUTH_RSA,
+       IPSEC_IKEV2_PSK,
+       IPSEC_IKEV2_RSA,
+       IPSEC_MAX,
+} ipsec_type_e;
+
+static const char *__get_ipsec_name(ipsec_type_e type)
+{
+       switch (type) {
+       case IPSEC_HYBRID_RSA:
+               return "IPSEC_HYBRID_RSA";
+       case IPSEC_XAUTH_PSK:
+               return "IPSEC_XAUTH_PSK";
+       case IPSEC_XAUTH_RSA:
+               return "IPSEC_XAUTH_RSA";
+       case IPSEC_IKEV2_PSK:
+               return "IPSEC_IKEV2_PSK";
+       case IPSEC_IKEV2_RSA:
+               return "IPSEC_IKEV2_RSA";
+       case IPSEC_MAX:
+       default:
+               return "UNKNOWN";
+       }
+}
+
+typedef struct {
+       char *key;
+       char *value;
+} kv_s;
+
+
+typedef enum {
+       IPSEC_VERSION,
+       IPSEC_LOCAL_ADDRS,
+       IPSEC_REMOTE_ADDRS,
+       IPSEC_LOCAL_AUTH,
+       IPSEC_LOCAL_CERTS,
+       IPSEC_LOCAL_ID,
+       IPSEC_LOCAL_XAUTH_ID,
+       IPSEC_REMOTE_AUTH,
+       IPSEC_REMOTE_CERTS,
+       IPSEC_REMOTE_TS,
+       IPSEC_XAUTH_SECRET,
+       IPSEC_IKE_SECRET,
+       IPSEC_KVS_MAX,
+} ipsec_kv_e;
+
+kv_s ipsec_setting_kvs[] = {
+               {"IPsec.Version", NULL},
+               {"IPsec.LocalAddrs", NULL},
+               {"IPsec.RemoteAddrs", NULL},
+               {"IPsec.LocalAuth", NULL},
+               {"IPsec.LocalCerts", NULL},
+               {"IPsec.LocalID", NULL},
+               {"IPsec.LocalXauthID", NULL},
+               {"IPsec.RemoteAuth", NULL},
+               {"IPsec.RemoteCerts", NULL},
+               {"IPsec.RemoteTS", NULL},
+               {"IPsec.XauthSecret", NULL},
+               {"IPsec.IKESecret", NULL},
+               {NULL, NULL},
+};
+
+typedef void (*gen_ipsec_settings_f)(void);
+
 static void __test_created_callback(vpn_error_e result,
                                void *user_data)
 {
@@ -357,14 +425,251 @@ int test_vpn_disconnect(void)
        return 1;
 }
 
+static int __test_init()
+{
+       int rv = 0;
+
+       rv = vpn_initialize();
+       if (rv == VPN_ERROR_NONE) {
+               printf("Register Callbacks if Initialize is Successful\n");
+       } else {
+               printf("VPN init failed [%s]\n",
+                       __test_convert_error_to_string(rv));
+               return -1;
+       }
+
+       rv = vpn_settings_init();
+       if (rv != VPN_ERROR_NONE) {
+               printf("Fail to Initialize Settings [%s]\n",
+                               __test_convert_error_to_string(rv));
+               return -1;
+       }
+       return rv;
+}
+
+static int __test_deinit()
+{
+       int rv = 0;
+
+       rv = vpn_settings_deinit();
+       if (rv != VPN_ERROR_NONE) {
+               printf("Fail to Deinitialize Settings [%s]\n",
+                               __test_convert_error_to_string(rv));
+               return -1;
+       }
+
+       rv = vpn_deinitialize();
+       if (rv != VPN_ERROR_NONE) {
+               printf("VPN init failed [%s]\n",
+                       __test_convert_error_to_string(rv));
+               return -1;
+       }
+
+       return rv;
+}
+
+static int __test_add()
+{
+       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("TEST_IPSEC");
+       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;
+}
+
+static void __init_ipsec_setting_kvs()
+{
+       int i = 0;
+       for(i = 0; i < IPSEC_KVS_MAX; i++){
+               if(ipsec_setting_kvs[i].value) {
+                       g_free(ipsec_setting_kvs[i].value);
+                       ipsec_setting_kvs[i].value = NULL;
+               }
+       }
+}
+
+static char* __get_user_input(char *what)
+{
+       char buf[128];
+       char *value = NULL;
+
+       printf("Please ENTER %s:", what);
+       if (scanf(" %[^\n]s", buf) < 0)
+               printf("Error in Reading the data to Buffer\n");
+       else
+               value = g_strdup(buf);
+       return value;
+}
+
+static void __gen_ipsec_hybrid_rsa_kvs(void)
+{
+       __init_ipsec_setting_kvs();
+
+       ipsec_setting_kvs[IPSEC_VERSION].value = g_strdup("1");
+       ipsec_setting_kvs[IPSEC_LOCAL_ADDRS].value = __get_user_input("Local Address for IPsec");
+       ipsec_setting_kvs[IPSEC_REMOTE_ADDRS].value = __get_user_input("Remote Address for IPsec");
+       ipsec_setting_kvs[IPSEC_LOCAL_AUTH].value = g_strdup("xauth");
+       ipsec_setting_kvs[IPSEC_LOCAL_CERTS].value = __get_user_input("File path for local cert");
+       ipsec_setting_kvs[IPSEC_LOCAL_XAUTH_ID].value = g_strdup("dave");
+       ipsec_setting_kvs[IPSEC_REMOTE_AUTH].value = g_strdup("pubkey");
+       ipsec_setting_kvs[IPSEC_REMOTE_CERTS].value = __get_user_input("File path for remote cert");
+       ipsec_setting_kvs[IPSEC_XAUTH_SECRET].value = g_strdup("ABCDEFGH");
+       return;
+}
+
+static void __gen_ipsec_xauth_psk_kvs(void)
+{
+       __init_ipsec_setting_kvs();
+
+       ipsec_setting_kvs[IPSEC_VERSION].value = g_strdup("1");
+       ipsec_setting_kvs[IPSEC_LOCAL_ADDRS].value = __get_user_input("Local Address for IPsec");
+       ipsec_setting_kvs[IPSEC_REMOTE_ADDRS].value = __get_user_input("Remote Address for IPsec");
+       ipsec_setting_kvs[IPSEC_LOCAL_AUTH].value = g_strdup("xauth");
+       ipsec_setting_kvs[IPSEC_LOCAL_XAUTH_ID].value = g_strdup("dave");
+       ipsec_setting_kvs[IPSEC_REMOTE_AUTH].value = g_strdup("psk");
+       ipsec_setting_kvs[IPSEC_XAUTH_SECRET].value = g_strdup("ABCDEFGH");
+       ipsec_setting_kvs[IPSEC_IKE_SECRET].value = g_strdup("ABCDEFGH");
+       return;
+}
+static void __gen_ipsec_xauth_rsa_kvs(void)
+{
+       __init_ipsec_setting_kvs();
+
+       ipsec_setting_kvs[IPSEC_VERSION].value = g_strdup("1");
+       ipsec_setting_kvs[IPSEC_LOCAL_ADDRS].value = __get_user_input("Local Address for IPsec");
+       ipsec_setting_kvs[IPSEC_REMOTE_ADDRS].value = __get_user_input("Remote Address for IPsec");
+       ipsec_setting_kvs[IPSEC_LOCAL_AUTH].value = g_strdup("xauth");
+       ipsec_setting_kvs[IPSEC_LOCAL_CERTS].value = __get_user_input("File path for local cert");
+       ipsec_setting_kvs[IPSEC_LOCAL_XAUTH_ID].value = g_strdup("dave");
+       ipsec_setting_kvs[IPSEC_REMOTE_AUTH].value = g_strdup("pubkey");
+       ipsec_setting_kvs[IPSEC_REMOTE_CERTS].value = __get_user_input("File path for remote cert");
+       ipsec_setting_kvs[IPSEC_XAUTH_SECRET].value = g_strdup("ABCDEFGH");
+       return;
+}
+static void __gen_ipsec_ikev2_psk_kvs(void)
+{
+       __init_ipsec_setting_kvs();
+
+       ipsec_setting_kvs[IPSEC_VERSION].value = g_strdup("2");
+       ipsec_setting_kvs[IPSEC_LOCAL_ADDRS].value = __get_user_input("Local Address for IPsec");
+       ipsec_setting_kvs[IPSEC_REMOTE_ADDRS].value = __get_user_input("Remote Address for IPsec");
+       ipsec_setting_kvs[IPSEC_LOCAL_AUTH].value = g_strdup("psk");
+       ipsec_setting_kvs[IPSEC_REMOTE_AUTH].value = g_strdup("psk");
+       ipsec_setting_kvs[IPSEC_IKE_SECRET].value = g_strdup("ABCDEFGH");
+       return;
+}
+static void __gen_ipsec_ikev2_rsa_kvs()
+{
+       __init_ipsec_setting_kvs();
+
+       ipsec_setting_kvs[IPSEC_VERSION].value = g_strdup("2");
+       ipsec_setting_kvs[IPSEC_LOCAL_ADDRS].value = __get_user_input("Local Address for IPsec");
+       ipsec_setting_kvs[IPSEC_REMOTE_ADDRS].value = __get_user_input("Remote Address for IPsec");
+       ipsec_setting_kvs[IPSEC_LOCAL_AUTH].value = g_strdup("pubkey");
+       ipsec_setting_kvs[IPSEC_LOCAL_CERTS].value = __get_user_input("File path for local cert");
+       ipsec_setting_kvs[IPSEC_REMOTE_AUTH].value = g_strdup("pubkey");
+       ipsec_setting_kvs[IPSEC_REMOTE_CERTS].value = __get_user_input("File path for remote cert");
+       return;
+}
+
+static int __set_vpn_settings_ipsec_kvs()
+{
+       int rv = 0;
+       int i = 0;
+       for (i = 0; i < IPSEC_KVS_MAX; i++) {
+               if(ipsec_setting_kvs[i].value)
+                       rv = vpn_settings_set_specific(ipsec_setting_kvs[i].key, ipsec_setting_kvs[i].value);
+               if(rv != VPN_ERROR_NONE)
+                       break;
+       }
+       return rv;
+}
+
+int test_create_ipsec(gpointer data, ipsec_type_e type)
+{
+       gen_ipsec_settings_f *gen_ipsec_settings = (gen_ipsec_settings_f *)data;
+       int rv = 0;
+
+       printf("create IPsec type [%s]\n", __get_ipsec_name(type));
+
+       rv = __test_init();
+       if (rv != VPN_ERROR_NONE)
+               return -1;
+
+       rv = __test_add();
+       if (rv != VPN_ERROR_NONE)
+               return -1;
+
+       gen_ipsec_settings[type]();
+       rv = __set_vpn_settings_ipsec_kvs();
+       if (rv != VPN_ERROR_NONE) {
+               printf("Fail to __set_vpn_settings_ipsec_kvs [%s]\n",
+                               __test_convert_error_to_string(rv));
+               __init_ipsec_setting_kvs();
+               __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 VPN Profile\n");
+
+       rv = __test_deinit();
+       if (rv != VPN_ERROR_NONE)
+               return -1;
+
+       return 1;
+}
+
 int main(int argc, char **argv)
 {
+       gen_ipsec_settings_f gen_ipsec_settings[IPSEC_MAX];
        GMainLoop *mainloop;
+
+       gen_ipsec_settings[IPSEC_HYBRID_RSA] = __gen_ipsec_hybrid_rsa_kvs;
+       gen_ipsec_settings[IPSEC_XAUTH_PSK] = __gen_ipsec_xauth_psk_kvs;
+       gen_ipsec_settings[IPSEC_XAUTH_RSA] = __gen_ipsec_xauth_rsa_kvs;
+       gen_ipsec_settings[IPSEC_IKEV2_PSK] = __gen_ipsec_ikev2_psk_kvs;
+       gen_ipsec_settings[IPSEC_IKEV2_RSA] = __gen_ipsec_ikev2_rsa_kvs;
+
        mainloop = g_main_loop_new(NULL, FALSE);
 
        GIOChannel *channel = g_io_channel_unix_new(0);
        g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL),
-                       test_thread, NULL);
+                       test_thread, gen_ipsec_settings);
 
        printf("Test Thread created...\n");
 
@@ -404,6 +709,11 @@ gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
                printf("8\t- VPN Remove - Removes the VPN profile\n");
                printf("9\t- VPN Connect - Connect the VPN profile\n");
                printf("a\t- VPN Disconnect - Disconnect the VPN profile\n");
+               printf("b\t- VPN Test Create IPSec Hybrid RSA - Create IPSec Hybrid RSA\n");
+               printf("c\t- VPN Test Create IPSec Xauth PSK - Create IPSec Xauth PSK\n");
+               printf("d\t- VPN Test Create IPSec Xauth RSA - Create IPSec Xauth RSA\n");
+               printf("e\t- VPN Test Create IPSec IKEv2 PSK - Create IPSec IKEv2 PSK\n");
+               printf("f\t- VPN Test Create IPSec IKEv2 RSA - Create IPSec IKEv2 RSA\n");
                printf("0\t- Exit\n");
 
                printf("ENTER  - Show options menu.......\n");
@@ -440,6 +750,21 @@ gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
        case 'a':
                rv = test_vpn_disconnect();
                break;
+       case 'b':
+               rv = test_create_ipsec(data, IPSEC_HYBRID_RSA);
+               break;
+       case 'c':
+               rv = test_create_ipsec(data, IPSEC_XAUTH_PSK);
+               break;
+       case 'd':
+               rv = test_create_ipsec(data, IPSEC_XAUTH_RSA);
+               break;
+       case 'e':
+               rv = test_create_ipsec(data, IPSEC_IKEV2_PSK);
+               break;
+       case 'f':
+               rv = test_create_ipsec(data, IPSEC_IKEV2_RSA);
+               break;
        default:
                break;
        }