tests: Expose print functions 05/258605/3
authorSeonah Moon <seonah1.moon@samsung.com>
Fri, 21 May 2021 05:02:40 +0000 (14:02 +0900)
committerSeonah Moon <seonah1.moon@samsung.com>
Fri, 21 May 2021 05:10:49 +0000 (14:10 +0900)
Change-Id: Ibbe00dd27d6b60f3010818f5356c8f3922b8134d

tests/vine-test/CMakeLists.txt
tests/vine-test/vine-multi-thread-test.cpp
tests/vine-test/vine-test-utils.cpp [new file with mode: 0644]
tests/vine-test/vine-test-utils.h [new file with mode: 0644]
tests/vine-test/vine-test.cpp

index 691d9ee..b8f0953 100755 (executable)
@@ -26,9 +26,12 @@ INCLUDE_DIRECTORIES(
 )
 
 FILE(GLOB VINE_TEST_SRCS *.cpp)
+FILE(GLOB VINE_TEST_UTILS_SRC *utils*.cpp)
+LIST(REMOVE_ITEM VINE_TEST_SRCS ${VINE_TEST_UTILS_SRC})
+
 foreach(file ${VINE_TEST_SRCS})
        GET_FILENAME_COMPONENT(name ${file} NAME_WE)
-       ADD_EXECUTABLE(${name} ${file})
+       ADD_EXECUTABLE(${name} ${file} ${VINE_TEST_UTILS_SRC})
        TARGET_LINK_LIBRARIES(${name}
                ${TARGET_VINE}
                ${VINE_LOGGER}
index 86de01a..7030b76 100644 (file)
@@ -16,7 +16,6 @@
 
 #include <sys/epoll.h>
 
-#include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdbool.h>
 #include <vine-log.h>
 #include <pthread.h>
 
-#define KGRN "\033[0;32;32m"
-//#define KCYN "\033[0;36m"
-#define KRED "\033[0;32;31m"
-#define KYEL "\033[1;33m"
-//#define KMAG "\033[0;35m"
-//#define KBLU "\033[0;32;34m"
-#define KCYN_L "\033[1;36m"
-#define RESET "\033[0m"
-
-
-#define PRINT_IF_ERROR(ret, func) __print_result(ret, func, false)
-#define PRINT_RESULT(ret, func) __print_result(ret, func, true)
-#define PRINT_LOG(format, args...) __print_log("[TID:%u] " format, (unsigned int)syscall(__NR_gettid), ##args)
+#include "vine-test-utils.h"
 
 #define MAX_EVENTS 10
 
 #define MAX_SERVICE_NAME_LEN 63
 #define MAX_IP_LEN 39 // for IPv6
 
-static const char *__error_to_str(vine_error_e error)
-{
-       switch (error) {
-       case VINE_ERROR_NONE:
-               return "NO ERROR";
-       case VINE_ERROR_NOT_PERMITTED:
-               return "NOT PERMITTED";
-       case VINE_ERROR_OUT_OF_MEMORY:
-               return "OUT OF MEMORY";
-       case VINE_ERROR_PERMISSION_DENIED:
-               return "PERMISSION DENIED";
-       case VINE_ERROR_INVALID_PARAMETER:
-               return "INVALID PARAMETER";
-       case VINE_ERROR_INVALID_OPERATION:
-               return "INVALID OPERATION";
-       case VINE_ERROR_CONNECTION_TIME_OUT:
-               return "CONNECTION TIME OUT";
-       case VINE_ERROR_NOW_IN_PROGRESS:
-               return "NOW IN PROGRESS";
-       case VINE_ERROR_NOT_SUPPORTED:
-               return "NOT SUPPORTED";
-       case VINE_ERROR_NOT_INITIALIZED:
-               return "NOT INITIALIZED";
-       case VINE_ERROR_ALREADY_INITIALIZED:
-               return "ALREADY INITIALIZED";
-       case VINE_ERROR_ALREADY_ENABLED:
-               return "ALREADY ENABLED";
-       case VINE_ERROR_OPERATION_FAILED:
-               return "OPERATION FAILED";
-       default:
-               return "UNKNOWN";
-       }
-}
-
-static void __print_error(const char *format, ...)
-{
-       va_list args;
-       va_start(args, format);
-
-       printf(KRED);
-       vprintf(format, args);
-       printf(RESET"\n");
-
-       va_end(args);
-}
-
-static void __print_log(const char *format, ...)
-{
-       va_list args;
-       va_start(args, format);
-
-       printf(KGRN);
-       vprintf(format, args);
-       printf(RESET"\n");
-
-       va_end(args);
-}
-
-static void __print_result(int ret, const char *function_name, bool print_if_no_error)
-{
-       unsigned int tid = (unsigned int)syscall(__NR_gettid);
-       if (ret == VINE_ERROR_NONE) {
-               if (print_if_no_error)
-                       printf(KGRN "[TID:%u] %s" RESET "\n", tid, function_name);
-       } else {
-               __print_error("[TID:%u] %s: %s(%d) ", tid, function_name,
-                       __error_to_str((vine_error_e)ret), ret);
-       }
-}
-
 static bool __print_attr(const char *key, const char *value, void *user_data)
 {
        PRINT_LOG("\t   > Attributes: %s=%s\n", key, value);
@@ -163,7 +80,7 @@ static void __get_current_time(char *buf)
 
        time(&now);
        if (localtime_r(&now, &local) == NULL) {
-               __print_error("localtime_r() fails");
+               print_error("localtime_r() fails");
                return;
        }
 
@@ -186,11 +103,11 @@ void __logger(int log_level, const char *log)
        unsigned int ctid = (unsigned int)syscall(__NR_gettid);
 
        if (ctid == g_main_tid) {
-               printf("%s" KGRN "[T%5u]", timestamp, g_main_tid);
+               printf("%s" COLOR_GRN "[T%5u]", timestamp, g_main_tid);
        } else if (ctid == g_main_tid + 1) {
-               printf("%s" KCYN_L "[T%5u]", timestamp, ctid);
+               printf("%s" COLOR_CYN "[T%5u]", timestamp, ctid);
        } else if (ctid == g_main_tid + 2) {
-               printf("%s" KYEL "[T%5u]", timestamp, ctid);
+               printf("%s" COLOR_YEL "[T%5u]", timestamp, ctid);
        } else {
                printf("%s[T%5u] ", timestamp, ctid);
        }
@@ -203,10 +120,10 @@ void __logger(int log_level, const char *log)
                printf("I/");
                break;
        case VINE_LOG_ERROR:
-               printf(KRED "E/");
+               printf(COLOR_RED "E/");
                break;
        }
-       printf("%s" RESET "\n", log);
+       printf("%s" COLOR_RESET "\n", log);
 }
 
 static void __event_handler(vine_session_h session)
@@ -226,7 +143,7 @@ static void __start_event_loop(vine_session_h session)
 
        int epollfd = epoll_create1(0);
        if (epollfd == -1) {
-               __print_error("Fail to create epoll fd %d", errno);
+               print_error("Fail to create epoll fd %d", errno);
                exit(1);
        }
 
@@ -234,14 +151,14 @@ static void __start_event_loop(vine_session_h session)
        ev.events = EPOLLIN;
        ev.data.fd = fd;
        if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
-               __print_error("Fail to add an epoll event for fd %d: %d", fd, errno);
+               print_error("Fail to add an epoll event for fd %d: %d", fd, errno);
                exit(1);
        }
 
        while (true) {
                int n = epoll_wait(epollfd, events, MAX_EVENTS, 0);
                if (n == -1) {
-                       __print_error("epoll_wait %d", errno);
+                       print_error("epoll_wait %d", errno);
                        exit(1);
                }
 
@@ -264,7 +181,7 @@ static void start_vine()
                        "vine_session_set_discovered_cb");
        PRINT_LOG("Session Created");
 
-       PRINT_IF_ERROR(vine_session_start_discovery(session, "", NULL),
+       PRINT_IF_ERROR(vine_session_start_discovery(session, "vine", NULL),
                "vine_session_start_discovery");
        PRINT_LOG("Subscribed");
 
diff --git a/tests/vine-test/vine-test-utils.cpp b/tests/vine-test/vine-test-utils.cpp
new file mode 100644 (file)
index 0000000..5260b6f
--- /dev/null
@@ -0,0 +1,78 @@
+#include "vine-test-utils.h"
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+//#include <vine.h>
+
+const char *convert_error_to_str(vine_error_e error)
+{
+    switch (error) {
+    case VINE_ERROR_NONE:
+        return "NO ERROR";
+    case VINE_ERROR_NOT_PERMITTED:
+        return "NOT PERMITTED";
+    case VINE_ERROR_OUT_OF_MEMORY:
+        return "OUT OF MEMORY";
+    case VINE_ERROR_PERMISSION_DENIED:
+        return "PERMISSION DENIED";
+    case VINE_ERROR_INVALID_PARAMETER:
+        return "INVALID PARAMETER";
+    case VINE_ERROR_INVALID_OPERATION:
+        return "INVALID OPERATION";
+    case VINE_ERROR_CONNECTION_TIME_OUT:
+        return "CONNECTION TIME OUT";
+    case VINE_ERROR_NOW_IN_PROGRESS:
+        return "NOW IN PROGRESS";
+    case VINE_ERROR_NOT_SUPPORTED:
+        return "NOT SUPPORTED";
+    case VINE_ERROR_NOT_INITIALIZED:
+        return "NOT INITIALIZED";
+    case VINE_ERROR_ALREADY_INITIALIZED:
+        return "ALREADY INITIALIZED";
+    case VINE_ERROR_ALREADY_ENABLED:
+        return "ALREADY ENABLED";
+    case VINE_ERROR_OPERATION_FAILED:
+        return "OPERATION FAILED";
+    default:
+        return "UNKNOWN";
+    }
+}
+
+void print_log(const char *format, ...)
+{
+    va_list args;
+
+    printf(COLOR_GRN "[TID: %u] ", (unsigned int)syscall(__NR_gettid));
+
+    va_start(args, format);
+    vprintf(format, args);
+    printf(COLOR_RESET"\n");
+
+    va_end(args);
+}
+
+void print_error(const char *format, ...)
+{
+    va_list args;
+    va_start(args, format);
+
+    printf(COLOR_RED);
+    vprintf(format, args);
+    printf(COLOR_RESET"\n");
+
+    va_end(args);
+}
+
+void print_result(int ret, const char *function_name, bool print_if_no_error)
+{
+       unsigned int tid = (unsigned int)syscall(__NR_gettid);
+       if (ret == VINE_ERROR_NONE) {
+               if (print_if_no_error)
+                       printf(COLOR_GRN "[TID:%u] %s" COLOR_RESET "\n", tid, function_name);
+       } else {
+               print_error("[TID:%u] %s: %s(%d) ", tid, function_name,
+                               convert_error_to_str((vine_error_e)ret), ret);
+       }
+}
diff --git a/tests/vine-test/vine-test-utils.h b/tests/vine-test/vine-test-utils.h
new file mode 100644 (file)
index 0000000..60255e9
--- /dev/null
@@ -0,0 +1,18 @@
+#include <vine.h>
+
+#define COLOR_RESET "\e[m"
+#define COLOR_RED "\e[31m"
+#define COLOR_GRN "\e[32m"
+#define COLOR_YEL "\e[33m"
+#define COLOR_BLU "\e[34m"
+#define COLOR_CYN "\e[36m"
+
+#define PRINT_IF_ERROR(ret, func) print_result(ret, func, false)
+#define PRINT_RESULT(ret, func) print_result(ret, func, true)
+#define PRINT_LOG(format, args...) print_log(format, ##args)
+
+const char *convert_error_to_str(vine_error_e error);
+
+void print_log(const char *format, ...);
+void print_error(const char *format, ...);
+void print_result(int ret, const char *function_name, bool print_if_no_error);
index fde6a78..4a692c5 100644 (file)
@@ -17,7 +17,6 @@
 #include <list>
 #include <iterator>
 
-#include <stdarg.h>
 #include <stdio.h>
 #include <stdbool.h>
 #include <sys/epoll.h>
 #include <vine.h>
 #include <vine-log.h>
 
-#define MAX_EVENTS 16
+#include "vine-test-utils.h"
 
+#define MAX_EVENTS 16
 #define TEST_DEBUG
 
-#define RESET_COLOR "\e[m"
-#define MAKE_RED "\e[31m"
-#define MAKE_GREEN "\e[32m"
-
-#define PRINT_IF_ERROR(ret, func) __print_result(ret, func, false)
-#define PRINT_RESULT(ret, func) __print_result(ret, func, true)
 #define CHECK_SESSION \
        do { \
                if (!g_session) { \
-                       __print_error("No Session"); \
+                       print_error("No Session"); \
                        return; \
                } \
        } while (0)
@@ -62,62 +56,6 @@ static vine_dp_h g_pubsub_dp = NULL;
 static std::list<vine_dp_h> g_datapath_list;
 static std::list<vine_service_h> g_service_list;
 
-static const char *__error_to_str(vine_error_e error)
-{
-       switch (error) {
-       case VINE_ERROR_NONE:
-               return "NO ERROR";
-       case VINE_ERROR_NOT_PERMITTED:
-               return "NOT PERMITTED";
-       case VINE_ERROR_OUT_OF_MEMORY:
-               return "OUT OF MEMORY";
-       case VINE_ERROR_PERMISSION_DENIED:
-               return "PERMISSION DENIED";
-       case VINE_ERROR_INVALID_PARAMETER:
-               return "INVALID PARAMETER";
-       case VINE_ERROR_INVALID_OPERATION:
-               return "INVALID OPERATION";
-       case VINE_ERROR_CONNECTION_TIME_OUT:
-               return "CONNECTION TIME OUT";
-       case VINE_ERROR_NOW_IN_PROGRESS:
-               return "NOW IN PROGRESS";
-       case VINE_ERROR_NOT_SUPPORTED:
-               return "NOT SUPPORTED";
-       case VINE_ERROR_NOT_INITIALIZED:
-               return "NOT INITIALIZED";
-       case VINE_ERROR_ALREADY_INITIALIZED:
-               return "ALREADY INITIALIZED";
-       case VINE_ERROR_ALREADY_ENABLED:
-               return "ALREADY ENABLED";
-       case VINE_ERROR_OPERATION_FAILED:
-               return "OPERATION FAILED";
-       default:
-               return "UNKNOWN";
-       }
-}
-
-static void __print_error(const char *format, ...)
-{
-       va_list args;
-       va_start(args, format);
-
-       printf(MAKE_RED);
-       vprintf(format, args);
-       printf(RESET_COLOR"\n");
-
-       va_end(args);
-}
-
-static void __print_result(int ret, const char *function_name, bool print_if_no_error)
-{
-       if (ret == VINE_ERROR_NONE) {
-               if (print_if_no_error)
-                       printf(MAKE_GREEN "%s" RESET_COLOR "\n", function_name);
-       } else {
-               __print_error("%s: %s(%d) ", function_name, __error_to_str((vine_error_e)ret), ret);
-       }
-}
-
 static bool test_get_user_string(const char *msg, char *buf, int buf_size)
 {
        if (msg == NULL || buf == NULL || buf_size < 2)
@@ -164,7 +102,7 @@ static void __start_event_loop()
        ev.events = EPOLLIN;
        ev.data.ptr = g_session;
        if (epoll_ctl(g_epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
-               __print_error("Fail to add an epoll event for fd %d: %d", fd, errno);
+               print_error("Fail to add an epoll event for fd %d: %d", fd, errno);
                exit(1);
        }
 }
@@ -270,7 +208,7 @@ static void __received_cb(vine_dp_h dp, size_t received_len, void *user_data)
        unsigned char buf[20] = {0, };
        size_t bytes = 0;
 
-       printf(MAKE_GREEN "[RECV_CB] %p received %zd bytes." RESET_COLOR "\n", dp, received_len);
+       printf(COLOR_GRN "[RECV_CB] %p received %zd bytes." COLOR_RESET "\n", dp, received_len);
        do {
                vine_dp_recv(dp, buf, 20, &bytes);
                __print_received_data(buf, bytes);
@@ -280,7 +218,7 @@ static void __received_cb(vine_dp_h dp, size_t received_len, void *user_data)
 
 static void __terminated_cb(vine_dp_h dp, void *user_data)
 {
-       printf(MAKE_GREEN "[TERMINATED_CB] %p is terminated." RESET_COLOR "\n", dp);
+       printf(COLOR_GRN "[TERMINATED_CB] %p is terminated." COLOR_RESET "\n", dp);
        g_datapath_list.remove(dp);
        vine_dp_destroy(dp);
 }
@@ -297,7 +235,7 @@ static void __opened_cb(vine_dp_h dp, vine_error_e result, void *user_data)
        int port;
 
        vine_dp_get_port(dp, &port);
-       printf(MAKE_GREEN "[OPENED_CB] %p is %s. port[%d]" RESET_COLOR "\n",
+       printf(COLOR_GRN "[OPENED_CB] %p is %s. port[%d]" COLOR_RESET "\n",
                        dp, (result == VINE_ERROR_NONE) ? "opened" : "not opened", port);
        if (dp == g_client_dp || dp == g_pubsub_dp)
                __add_new_dp(dp);
@@ -339,7 +277,7 @@ static void __set_service_type()
        char type[64];
        printf(" >> Service Type: ");
        if (scanf(" %63s", type) < 1) {
-               __print_error("Scan failed");
+               print_error("Scan failed");
                return;
        }
        PRINT_RESULT(vine_service_set_type(g_service, type),
@@ -352,7 +290,7 @@ static void __set_service_name()
        char name[64];
        printf(" >> Service Name (Max length 64): ");
        if (scanf(" %63s", name) < 1) {
-               __print_error("Scan failed");
+               print_error("Scan failed");
                return;
        }
        PRINT_RESULT(vine_service_set_name(g_service, name),
@@ -365,7 +303,7 @@ static void __set_port()
        int port;
        printf(" >> Port Number (0 ~ 65535): ");
        if (scanf(" %d", &port) < 1) {
-               __print_error("Scan failed");
+               print_error("Scan failed");
                return;
        }
        PRINT_RESULT(vine_service_set_port(g_service, port), "vine_session_set_port");
@@ -429,7 +367,7 @@ static void __start_discovery()
        char type[20];
        printf(" >> Service Type: ");
        if (scanf(" %19s", type) < 1) {
-               __print_error("Scan failed");
+               print_error("Scan failed");
                return;
        }
 
@@ -530,10 +468,10 @@ void __logger(int log_level, const char *log)
                printf("I/");
                break;
        case VINE_LOG_ERROR:
-               printf(MAKE_RED "E/");
+               printf(COLOR_RED "E/");
                break;
        }
-       printf("%s" RESET_COLOR "\n", log);
+       printf("%s" COLOR_RESET "\n", log);
 }
 #endif
 
@@ -587,12 +525,12 @@ static vine_security_h __create_security_handle(int type, bool is_server)
 
 static void __accepted_cb(vine_dp_h dp, vine_dp_h accepted_dp, void *user_data)
 {
-       printf(MAKE_GREEN "[ACCEPTED_CB] %p is accepted." RESET_COLOR "\n", accepted_dp);
+       printf(COLOR_GRN "[ACCEPTED_CB] %p is accepted." COLOR_RESET "\n", accepted_dp);
        vine_address_family_e addr_family;
        char *ip;
        int ret = vine_dp_get_remote_ip(accepted_dp, &addr_family, &ip);
        PRINT_IF_ERROR(ret, "vine_dp_get_remote_ip");
-       printf(MAKE_GREEN "  Client IP[%s] Family[%d]" RESET_COLOR "\n", ip,  (int)addr_family);
+       printf(COLOR_GRN "  Client IP[%s] Family[%d]" COLOR_RESET "\n", ip,  (int)addr_family);
        free(ip);
        __add_new_dp(accepted_dp);
 }
@@ -606,19 +544,19 @@ static void __open_server()
 
        printf(" >> Address type(0-default, 1-IPv4, 2-IPv6): ");
        if (scanf(" %d", &addr_family) < 1) {
-               __print_error("Scan failed");
+               print_error("Scan failed");
                return;
        }
 
        printf(" >> listen port: ");
        if (scanf(" %d", &port) < 1) {
-               __print_error("Scan failed");
+               print_error("Scan failed");
                return;
        }
 
        printf("Set security type(0.NONE, 1.TLS, 2.PSK): ");
        if (scanf("%d", &security_type) < 0) {
-               __print_error("Scan failed");
+               print_error("Scan failed");
                return;
        }
 
@@ -644,24 +582,24 @@ static void __connect_server()
 
        printf(" >> Address Family(0-IPv4, 1-IPv6): ");
        if (scanf(" %d", &addr_type) < 1) {
-               __print_error("Scan failed");
+               print_error("Scan failed");
                return;
        }
        printf(" >> Peer IP: ");
        if (scanf(" %39s", ip) < 1) {
-               __print_error("Scan failed");
+               print_error("Scan failed");
                return;
        }
 
        printf(" >> Peer port: ");
        if (scanf(" %d", &port) < 1) {
-               __print_error("Scan failed");
+               print_error("Scan failed");
                return;
        }
 
        printf("Set security type(0.NONE, 1.TLS, 2.PSK): ");
        if (scanf("%d", &security_type) < 0) {
-               __print_error("Scan failed");
+               print_error("Scan failed");
                return;
        }
 
@@ -684,19 +622,19 @@ static void __join_service()
 
        printf(" >> Address type(0-default, 1-IPv4, 2-IPv6): ");
        if (scanf(" %d", &addr_family) < 1) {
-               __print_error("Scan failed");
+               print_error("Scan failed");
                return;
        }
 
        printf(" >> Listen port: ");
        if (scanf(" %d", &port) < 1) {
-               __print_error("Scan failed");
+               print_error("Scan failed");
                return;
        }
 
        printf(" >> Set topic: ");
        if (scanf(" %63s", topic) < 1) {
-               __print_error("Scan failed");
+               print_error("Scan failed");
                return;
        }
 
@@ -890,7 +828,7 @@ int main()
 
        g_epollfd = epoll_create1(0);
        if (g_epollfd == -1) {
-               __print_error("Fail to create epoll fd %d", errno);
+               print_error("Fail to create epoll fd %d", errno);
                return -1;
        }
 
@@ -898,14 +836,14 @@ int main()
        ev.events = EPOLLIN;
        ev.data.fd = STDIN_FILENO;
        if (epoll_ctl(g_epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1) {
-               __print_error("Fail to add an epoll event for stdin: %d", errno);
+               print_error("Fail to add an epoll event for stdin: %d", errno);
                return -1;
        }
 
        while (true) {
                int n = epoll_wait(g_epollfd, events, MAX_EVENTS, 0);
                if (n == -1) {
-                       __print_error("epoll_wait %d", errno);
+                       print_error("epoll_wait %d", errno);
                        return -1;
                }