#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);
time(&now);
if (localtime_r(&now, &local) == NULL) {
- __print_error("localtime_r() fails");
+ print_error("localtime_r() fails");
return;
}
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);
}
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)
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);
}
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);
}
"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");
--- /dev/null
+#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);
+ }
+}
#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)
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)
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);
}
}
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);
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);
}
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);
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),
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),
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");
char type[20];
printf(" >> Service Type: ");
if (scanf(" %19s", type) < 1) {
- __print_error("Scan failed");
+ print_error("Scan failed");
return;
}
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
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);
}
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;
}
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;
}
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;
}
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;
}
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;
}