From 3a18e05558aeac8a63c3da5c700dcb971cb078a1 Mon Sep 17 00:00:00 2001 From: CIPop Date: Wed, 16 Aug 2023 23:17:07 +0000 Subject: [PATCH] Extracting the test framework in a separate include file. --- MQTTPacket/test/build_test | 8 -- MQTTPacket/test/test1.c | 231 +----------------------------- MQTTPacket/test/test2.c | 228 +----------------------------- MQTTPacket/test/test3.c | 219 +---------------------------- MQTTPacket/test/test35.c | 223 +---------------------------- MQTTPacket/test/test_framework.h | 232 +++++++++++++++++++++++++++++++ 6 files changed, 245 insertions(+), 896 deletions(-) delete mode 100644 MQTTPacket/test/build_test create mode 100644 MQTTPacket/test/test_framework.h diff --git a/MQTTPacket/test/build_test b/MQTTPacket/test/build_test deleted file mode 100644 index f35ac8d6..00000000 --- a/MQTTPacket/test/build_test +++ /dev/null @@ -1,8 +0,0 @@ -gcc -Wall test1.c -o test1 -I../src ../src/MQTTConnectClient.c ../src/MQTTConnectServer.c ../src/MQTTPacket.c ../src/MQTTSerializePublish.c ../src/MQTTDeserializePublish.c ../src/MQTTSubscribeServer.c ../src/MQTTSubscribeClient.c ../src/MQTTUnsubscribeServer.c ../src/MQTTUnsubscribeClient.c - -gcc -Wall test1.c -o test15 -I../src ../src/MQTTConnectClient.c ../src/MQTTConnectServer.c ../src/MQTTPacket.c ../src/MQTTSerializePublish.c ../src/MQTTDeserializePublish.c ../src/MQTTSubscribeServer.c ../src/MQTTSubscribeClient.c ../src/MQTTUnsubscribeServer.c ../src/MQTTUnsubscribeClient.c -DMQTTV5 ../src/V5/MQTTProperties.c ../src/V5/MQTTV5Packet.c - -gcc -Wall test2.c -o test2 -I../src ../src/MQTTConnectClient.c ../src/MQTTConnectServer.c ../src/MQTTPacket.c ../src/MQTTSerializePublish.c ../src/MQTTDeserializePublish.c ../src/MQTTSubscribeServer.c ../src/MQTTSubscribeClient.c ../src/MQTTUnsubscribeServer.c ../src/MQTTUnsubscribeClient.c -DMQTTV5 ../src/V5/MQTTProperties.c ../src/V5/MQTTV5Packet.c - -gcc -Wall -c ../samples/transport.c -Os -s -I../samples -gcc test3.c transport.o -I ../src ../src/MQTTConnectClient.c ../src/MQTTSerializePublish.c ../src/MQTTPacket.c -o test3 -Os -s -I../samples -DMQTTV5 ../src/V5/MQTTProperties.c ../src/V5/MQTTV5Packet.c -I../src/V5 ../src/MQTTSubscribeClient.c ../src/MQTTSubscribeServer.c ../src/MQTTUnsubscribeClient.c ../src/MQTTUnsubscribeServer.c ../src/MQTTDeserializePublish.c diff --git a/MQTTPacket/test/test1.c b/MQTTPacket/test/test1.c index 13099620..f7689e2b 100644 --- a/MQTTPacket/test/test1.c +++ b/MQTTPacket/test/test1.c @@ -14,235 +14,12 @@ * Ian Craggs - initial API and implementation and/or initial documentation *******************************************************************************/ +/*** + Tests for MQTTV3 serialization and deserialization +***/ +#include "test_framework.h" #include "MQTTPacket.h" -#include -#include -#include - -#if !defined(_WINDOWS) - #include - #include - #include - #include -#else -#include -#include -#define MAXHOSTNAMELEN 256 -#define EAGAIN WSAEWOULDBLOCK -#define EINTR WSAEINTR -#define EINPROGRESS WSAEINPROGRESS -#define EWOULDBLOCK WSAEWOULDBLOCK -#define ENOTCONN WSAENOTCONN -#define ECONNRESET WSAECONNRESET -#endif - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) - -struct Options -{ - char* connection; /**< connection to system under test. */ - char** haconnections; - int hacount; - int verbose; - int test_no; -} options = -{ - "tcp://localhost:1883", - NULL, - 0, - 0, - 0, -}; - -void usage() -{ - -} - -void getopts(int argc, char** argv) -{ - int count = 1; - - while (count < argc) - { - if (strcmp(argv[count], "--test_no") == 0) - { - if (++count < argc) - options.test_no = atoi(argv[count]); - else - usage(); - } - else if (strcmp(argv[count], "--connection") == 0) - { - if (++count < argc) - { - options.connection = argv[count]; - printf("\nSetting connection to %s\n", options.connection); - } - else - usage(); - } - else if (strcmp(argv[count], "--haconnections") == 0) - { - if (++count < argc) - { - char* tok = strtok(argv[count], " "); - options.hacount = 0; - options.haconnections = malloc(sizeof(char*) * 5); - while (tok) - { - options.haconnections[options.hacount] = malloc(strlen(tok) + 1); - strcpy(options.haconnections[options.hacount], tok); - options.hacount++; - tok = strtok(NULL, " "); - } - } - else - usage(); - } - else if (strcmp(argv[count], "--verbose") == 0) - { - options.verbose = 1; - printf("\nSetting verbose on\n"); - } - count++; - } -} - - -#define LOGA_DEBUG 0 -#define LOGA_INFO 1 -#include -#include -#include -void MyLog(int LOGA_level, char* format, ...) -{ - static char msg_buf[256]; - va_list args; - struct timeb ts; - - struct tm *timeinfo; - - if (LOGA_level == LOGA_DEBUG && options.verbose == 0) - return; - - ftime(&ts); - timeinfo = localtime(&ts.time); - strftime(msg_buf, 80, "%Y%m%d %H%M%S", timeinfo); - - sprintf(&msg_buf[strlen(msg_buf)], ".%.3hu ", ts.millitm); - - va_start(args, format); - vsnprintf(&msg_buf[strlen(msg_buf)], sizeof(msg_buf) - strlen(msg_buf), format, args); - va_end(args); - - printf("%s\n", msg_buf); - fflush(stdout); -} - - -#if defined(WIN32) || defined(_WINDOWS) -#define mqsleep(A) Sleep(1000*A) -#define START_TIME_TYPE DWORD -static DWORD start_time = 0; -START_TIME_TYPE start_clock(void) -{ - return GetTickCount(); -} -#elif defined(AIX) -#define mqsleep sleep -#define START_TIME_TYPE struct timespec -START_TIME_TYPE start_clock(void) -{ - static struct timespec start; - clock_gettime(CLOCK_REALTIME, &start); - return start; -} -#else -#define mqsleep sleep -#define START_TIME_TYPE struct timeval -START_TIME_TYPE start_clock(void) -{ - struct timeval start_time; - gettimeofday(&start_time, NULL); - return start_time; -} -#endif - - -#if defined(WIN32) -long elapsed(START_TIME_TYPE start_time) -{ - return GetTickCount() - start_time; -} -#elif defined(AIX) -#define assert(a) -long elapsed(struct timespec start) -{ - struct timespec now, res; - - clock_gettime(CLOCK_REALTIME, &now); - ntimersub(now, start, res); - return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L; -} -#else -long elapsed(START_TIME_TYPE start_time) -{ - struct timeval now, res; - - gettimeofday(&now, NULL); - timersub(&now, &start_time, &res); - return (res.tv_sec)*1000 + (res.tv_usec)/1000; -} -#endif - - -#define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d) -#define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e) - -int tests = 0; -int failures = 0; -FILE* xml; -START_TIME_TYPE global_start_time; -char output[3000]; -char* cur_output = output; - - -void write_test_result() -{ - long duration = elapsed(global_start_time); - - fprintf(xml, " time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000); - if (cur_output != output) - { - fprintf(xml, "%s", output); - cur_output = output; - } - fprintf(xml, "\n"); -} - - -void myassert(char* filename, int lineno, char* description, int value, char* format, ...) -{ - ++tests; - if (!value) - { - va_list args; - - ++failures; - printf("Assertion failed, file %s, line %d, description: %s\n", filename, lineno, description); - - va_start(args, format); - vprintf(format, args); - va_end(args); - - cur_output += sprintf(cur_output, "file %s, line %d \n", - description, filename, lineno); - } - else - MyLog(LOGA_DEBUG, "Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description); -} #define min(a, b) ((a < b) ? a : b) diff --git a/MQTTPacket/test/test2.c b/MQTTPacket/test/test2.c index 77a95895..3a399e93 100644 --- a/MQTTPacket/test/test2.c +++ b/MQTTPacket/test/test2.c @@ -18,234 +18,8 @@ Tests for MQTTV5 serialization and deserialization ***/ +#include "test_framework.h" #include "MQTTV5Packet.h" -#include -#include -#include - -#if !defined(_WINDOWS) - #include - #include - #include - #include -#else -#include -#include -#define MAXHOSTNAMELEN 256 -#define EAGAIN WSAEWOULDBLOCK -#define EINTR WSAEINTR -#define EINPROGRESS WSAEINPROGRESS -#define EWOULDBLOCK WSAEWOULDBLOCK -#define ENOTCONN WSAENOTCONN -#define ECONNRESET WSAECONNRESET -#endif - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) - -struct Options -{ - char* connection; /**< connection to system under test. */ - char** haconnections; - int hacount; - int verbose; - int test_no; -} options = -{ - "tcp://localhost:1883", - NULL, - 0, - 0, - 0, -}; - -void usage() -{ - -} - -void getopts(int argc, char** argv) -{ - int count = 1; - - while (count < argc) - { - if (strcmp(argv[count], "--test_no") == 0) - { - if (++count < argc) - options.test_no = atoi(argv[count]); - else - usage(); - } - else if (strcmp(argv[count], "--connection") == 0) - { - if (++count < argc) - { - options.connection = argv[count]; - printf("\nSetting connection to %s\n", options.connection); - } - else - usage(); - } - else if (strcmp(argv[count], "--haconnections") == 0) - { - if (++count < argc) - { - char* tok = strtok(argv[count], " "); - options.hacount = 0; - options.haconnections = malloc(sizeof(char*) * 5); - while (tok) - { - options.haconnections[options.hacount] = malloc(strlen(tok) + 1); - strcpy(options.haconnections[options.hacount], tok); - options.hacount++; - tok = strtok(NULL, " "); - } - } - else - usage(); - } - else if (strcmp(argv[count], "--verbose") == 0) - { - options.verbose = 1; - printf("\nSetting verbose on\n"); - } - count++; - } -} - - -#define LOGA_DEBUG 0 -#define LOGA_INFO 1 -#include -#include -#include -void MyLog(int LOGA_level, char* format, ...) -{ - static char msg_buf[256]; - va_list args; - struct timeb ts; - - struct tm *timeinfo; - - if (LOGA_level == LOGA_DEBUG && options.verbose == 0) - return; - - ftime(&ts); - timeinfo = localtime(&ts.time); - strftime(msg_buf, 80, "%Y%m%d %H%M%S", timeinfo); - - sprintf(&msg_buf[strlen(msg_buf)], ".%.3hu ", ts.millitm); - - va_start(args, format); - vsnprintf(&msg_buf[strlen(msg_buf)], sizeof(msg_buf) - strlen(msg_buf), format, args); - va_end(args); - - printf("%s\n", msg_buf); - fflush(stdout); -} - - -#if defined(WIN32) || defined(_WINDOWS) -#define mqsleep(A) Sleep(1000*A) -#define START_TIME_TYPE DWORD -static DWORD start_time = 0; -START_TIME_TYPE start_clock(void) -{ - return GetTickCount(); -} -#elif defined(AIX) -#define mqsleep sleep -#define START_TIME_TYPE struct timespec -START_TIME_TYPE start_clock(void) -{ - static struct timespec start; - clock_gettime(CLOCK_REALTIME, &start); - return start; -} -#else -#define mqsleep sleep -#define START_TIME_TYPE struct timeval -START_TIME_TYPE start_clock(void) -{ - struct timeval start_time; - gettimeofday(&start_time, NULL); - return start_time; -} -#endif - - -#if defined(WIN32) -long elapsed(START_TIME_TYPE start_time) -{ - return GetTickCount() - start_time; -} -#elif defined(AIX) -#define assert(a) -long elapsed(struct timespec start) -{ - struct timespec now, res; - - clock_gettime(CLOCK_REALTIME, &now); - ntimersub(now, start, res); - return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L; -} -#else -long elapsed(START_TIME_TYPE start_time) -{ - struct timeval now, res; - - gettimeofday(&now, NULL); - timersub(&now, &start_time, &res); - return (res.tv_sec)*1000 + (res.tv_usec)/1000; -} -#endif - - -#define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d) -#define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e) - -int tests = 0; -int failures = 0; -FILE* xml; -START_TIME_TYPE global_start_time; -char output[3000]; -char* cur_output = output; - - -void write_test_result() -{ - long duration = elapsed(global_start_time); - - fprintf(xml, " time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000); - if (cur_output != output) - { - fprintf(xml, "%s", output); - cur_output = output; - } - fprintf(xml, "\n"); -} - - -void myassert(char* filename, int lineno, char* description, int value, char* format, ...) -{ - ++tests; - if (!value) - { - va_list args; - - ++failures; - printf("Assertion failed, file %s, line %d, description: %s\n", filename, lineno, description); - - va_start(args, format); - vprintf(format, args); - va_end(args); - - cur_output += sprintf(cur_output, "file %s, line %d \n", - description, filename, lineno); - } - else - MyLog(LOGA_DEBUG, "Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description); -} #define min(a, b) ((a < b) ? a : b) diff --git a/MQTTPacket/test/test3.c b/MQTTPacket/test/test3.c index 44afc36c..bf1d9ffa 100644 --- a/MQTTPacket/test/test3.c +++ b/MQTTPacket/test/test3.c @@ -15,225 +15,14 @@ * Sergio R. Caprile - clarifications and/or documentation extension *******************************************************************************/ -#include -#include -#include +/*** + MQTTV5 Packet tests using a broker +***/ +#include "test_framework.h" #include "MQTTV5Packet.h" #include "transport.h" -#if !defined(_WINDOWS) - #include - #include - #include - #include -#else -#include -#include -#define MAXHOSTNAMELEN 256 -#define EAGAIN WSAEWOULDBLOCK -#define EINTR WSAEINTR -#define EINPROGRESS WSAEINPROGRESS -#define EWOULDBLOCK WSAEWOULDBLOCK -#define ENOTCONN WSAENOTCONN -#define ECONNRESET WSAECONNRESET -#endif - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) - -struct Options -{ - char* host; /**< connection to system under test. */ - int port; - int verbose; - int test_no; -} options = -{ - "localhost", - 1883, - 0, - 0, -}; - -void usage() -{ - -} - -void getopts(int argc, char** argv) -{ - int count = 1; - - while (count < argc) - { - if (strcmp(argv[count], "--test_no") == 0) - { - if (++count < argc) - options.test_no = atoi(argv[count]); - else - usage(); - } - else if (strcmp(argv[count], "--host") == 0) - { - if (++count < argc) - { - options.host = argv[count]; - printf("\nSetting host to %s\n", options.host); - } - else - usage(); - } - else if (strcmp(argv[count], "--port") == 0) - { - if (++count < argc) - options.port = atoi(argv[count]); - else - usage(); - } - else if (strcmp(argv[count], "--verbose") == 0) - { - options.verbose = 1; - printf("\nSetting verbose on\n"); - } - count++; - } -} - -#define LOGA_DEBUG 0 -#define LOGA_INFO 1 -#include -#include -#include -void MyLog(int LOGA_level, char* format, ...) -{ - static char msg_buf[256]; - va_list args; - struct timeb ts; - - struct tm *timeinfo; - - if (LOGA_level == LOGA_DEBUG && options.verbose == 0) - return; - - ftime(&ts); - timeinfo = localtime(&ts.time); - strftime(msg_buf, 80, "%Y%m%d %H%M%S", timeinfo); - - sprintf(&msg_buf[strlen(msg_buf)], ".%.3hu ", ts.millitm); - - va_start(args, format); - vsnprintf(&msg_buf[strlen(msg_buf)], sizeof(msg_buf) - strlen(msg_buf), format, args); - va_end(args); - - printf("%s\n", msg_buf); - fflush(stdout); -} - - -#if defined(WIN32) || defined(_WINDOWS) -#define mqsleep(A) Sleep(1000*A) -#define START_TIME_TYPE DWORD -static DWORD start_time = 0; -START_TIME_TYPE start_clock(void) -{ - return GetTickCount(); -} -#elif defined(AIX) -#define mqsleep sleep -#define START_TIME_TYPE struct timespec -START_TIME_TYPE start_clock(void) -{ - static struct timespec start; - clock_gettime(CLOCK_REALTIME, &start); - return start; -} -#else -#define mqsleep sleep -#define START_TIME_TYPE struct timeval -START_TIME_TYPE start_clock(void) -{ - struct timeval start_time; - gettimeofday(&start_time, NULL); - return start_time; -} -#endif - - -#if defined(WIN32) -long elapsed(START_TIME_TYPE start_time) -{ - return GetTickCount() - start_time; -} -#elif defined(AIX) -#define assert(a) -long elapsed(struct timespec start) -{ - struct timespec now, res; - - clock_gettime(CLOCK_REALTIME, &now); - ntimersub(now, start, res); - return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L; -} -#else -long elapsed(START_TIME_TYPE start_time) -{ - struct timeval now, res; - - gettimeofday(&now, NULL); - timersub(&now, &start_time, &res); - return (res.tv_sec)*1000 + (res.tv_usec)/1000; -} -#endif - - -#define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d) -#define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e) - -int tests = 0; -int failures = 0; -FILE* xml; -START_TIME_TYPE global_start_time; -char output[3000]; -char* cur_output = output; - - -void write_test_result() -{ - long duration = elapsed(global_start_time); - - fprintf(xml, " time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000); - if (cur_output != output) - { - fprintf(xml, "%s", output); - cur_output = output; - } - fprintf(xml, "\n"); -} - - -void myassert(char* filename, int lineno, char* description, int value, char* format, ...) -{ - ++tests; - if (!value) - { - va_list args; - - ++failures; - printf("Assertion failed, file %s, line %d, description: %s\n", filename, lineno, description); - - va_start(args, format); - vprintf(format, args); - va_end(args); - - cur_output += sprintf(cur_output, "file %s, line %d \n", - description, filename, lineno); - } - else - MyLog(LOGA_DEBUG, "Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description); -} - - - int test1(struct Options options) { MQTTV5Packet_connectData data = MQTTV5Packet_connectData_initializer; diff --git a/MQTTPacket/test/test35.c b/MQTTPacket/test/test35.c index 5ba04ccf..19bdf21d 100644 --- a/MQTTPacket/test/test35.c +++ b/MQTTPacket/test/test35.c @@ -12,226 +12,14 @@ * *******************************************************************************/ -#include -#include -#include - +/*** + Side-by-side MQTTv3 and MQTTv5 Tests. +***/ +#include "test_framework.h" #include "MQTTPacket.h" #include "MQTTV5Packet.h" #include "transport.h" -#if !defined(_WINDOWS) - #include - #include - #include - #include -#else -#include -#include -#define MAXHOSTNAMELEN 256 -#define EAGAIN WSAEWOULDBLOCK -#define EINTR WSAEINTR -#define EINPROGRESS WSAEINPROGRESS -#define EWOULDBLOCK WSAEWOULDBLOCK -#define ENOTCONN WSAENOTCONN -#define ECONNRESET WSAECONNRESET -#endif - - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) - -struct Options -{ - char* host; /**< connection to system under test. */ - int port; - int verbose; - int test_no; -} options = -{ - "localhost", - 1883, - 0, - 0, -}; - -void usage() -{ - -} - -void getopts(int argc, char** argv) -{ - int count = 1; - - while (count < argc) - { - if (strcmp(argv[count], "--test_no") == 0) - { - if (++count < argc) - options.test_no = atoi(argv[count]); - else - usage(); - } - else if (strcmp(argv[count], "--host") == 0) - { - if (++count < argc) - { - options.host = argv[count]; - printf("\nSetting host to %s\n", options.host); - } - else - usage(); - } - else if (strcmp(argv[count], "--port") == 0) - { - if (++count < argc) - options.port = atoi(argv[count]); - else - usage(); - } - else if (strcmp(argv[count], "--verbose") == 0) - { - options.verbose = 1; - printf("\nSetting verbose on\n"); - } - count++; - } -} - -#define LOGA_DEBUG 0 -#define LOGA_INFO 1 -#include -#include -#include -void MyLog(int LOGA_level, char* format, ...) -{ - static char msg_buf[256]; - va_list args; - struct timeb ts; - - struct tm *timeinfo; - - if (LOGA_level == LOGA_DEBUG && options.verbose == 0) - return; - - ftime(&ts); - timeinfo = localtime(&ts.time); - strftime(msg_buf, 80, "%Y%m%d %H%M%S", timeinfo); - - sprintf(&msg_buf[strlen(msg_buf)], ".%.3hu ", ts.millitm); - - va_start(args, format); - vsnprintf(&msg_buf[strlen(msg_buf)], sizeof(msg_buf) - strlen(msg_buf), format, args); - va_end(args); - - printf("%s\n", msg_buf); - fflush(stdout); -} - - -#if defined(WIN32) || defined(_WINDOWS) -#define mqsleep(A) Sleep(1000*A) -#define START_TIME_TYPE DWORD -static DWORD start_time = 0; -START_TIME_TYPE start_clock(void) -{ - return GetTickCount(); -} -#elif defined(AIX) -#define mqsleep sleep -#define START_TIME_TYPE struct timespec -START_TIME_TYPE start_clock(void) -{ - static struct timespec start; - clock_gettime(CLOCK_REALTIME, &start); - return start; -} -#else -#define mqsleep sleep -#define START_TIME_TYPE struct timeval -START_TIME_TYPE start_clock(void) -{ - struct timeval start_time; - gettimeofday(&start_time, NULL); - return start_time; -} -#endif - - -#if defined(WIN32) -long elapsed(START_TIME_TYPE start_time) -{ - return GetTickCount() - start_time; -} -#elif defined(AIX) -#define assert(a) -long elapsed(struct timespec start) -{ - struct timespec now, res; - - clock_gettime(CLOCK_REALTIME, &now); - ntimersub(now, start, res); - return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L; -} -#else -long elapsed(START_TIME_TYPE start_time) -{ - struct timeval now, res; - - gettimeofday(&now, NULL); - timersub(&now, &start_time, &res); - return (res.tv_sec)*1000 + (res.tv_usec)/1000; -} -#endif - - -#define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d) -#define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e) - -int tests = 0; -int failures = 0; -FILE* xml; -START_TIME_TYPE global_start_time; -char output[3000]; -char* cur_output = output; - - -void write_test_result() -{ - long duration = elapsed(global_start_time); - - fprintf(xml, " time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000); - if (cur_output != output) - { - fprintf(xml, "%s", output); - cur_output = output; - } - fprintf(xml, "\n"); -} - - -void myassert(char* filename, int lineno, char* description, int value, char* format, ...) -{ - ++tests; - if (!value) - { - va_list args; - - ++failures; - printf("Assertion failed, file %s, line %d, description: %s\n", filename, lineno, description); - - va_start(args, format); - vprintf(format, args); - va_end(args); - - cur_output += sprintf(cur_output, "file %s, line %d \n", - description, filename, lineno); - } - else - MyLog(LOGA_DEBUG, "Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description); -} - -#if 1 int test_v3(struct Options options) { MQTTPacket_connectData data = MQTTPacket_connectData_initializer; @@ -498,9 +286,7 @@ int test_v3(struct Options options) return 0; } -#endif -#if 1 int test_v5(struct Options options) { MQTTV5Packet_connectData data = MQTTV5Packet_connectData_initializer; @@ -820,7 +606,6 @@ int test_v5(struct Options options) write_test_result(); return failures; } -#endif int main(int argc, char** argv) { diff --git a/MQTTPacket/test/test_framework.h b/MQTTPacket/test/test_framework.h new file mode 100644 index 00000000..68f92642 --- /dev/null +++ b/MQTTPacket/test/test_framework.h @@ -0,0 +1,232 @@ +/******************************************************************************* + * Copyright (c) 2023 Microsoft Corporation. All rights reserved. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + *******************************************************************************/ + +#ifndef TEST_FRAMEWORK_H_ +#define TEST_FRAMEWORK_H_ + +#include +#include +#include + +#if !defined(_WINDOWS) + #include + #include + #include + #include +#else +#include +#include +#define MAXHOSTNAMELEN 256 +#define EAGAIN WSAEWOULDBLOCK +#define EINTR WSAEINTR +#define EINPROGRESS WSAEINPROGRESS +#define EWOULDBLOCK WSAEWOULDBLOCK +#define ENOTCONN WSAENOTCONN +#define ECONNRESET WSAECONNRESET +#endif + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) + +struct Options +{ + char* host; /**< connection to system under test. */ + int port; + int verbose; + int test_no; +} options = +{ + "localhost", + 1883, + 0, + 0, +}; + +void usage() +{ + +} + +void getopts(int argc, char** argv) +{ + int count = 1; + + while (count < argc) + { + if (strcmp(argv[count], "--test_no") == 0) + { + if (++count < argc) + options.test_no = atoi(argv[count]); + else + usage(); + } + else if (strcmp(argv[count], "--host") == 0) + { + if (++count < argc) + { + options.host = argv[count]; + printf("\nSetting host to %s\n", options.host); + } + else + usage(); + } + else if (strcmp(argv[count], "--port") == 0) + { + if (++count < argc) + options.port = atoi(argv[count]); + else + usage(); + } + else if (strcmp(argv[count], "--verbose") == 0) + { + options.verbose = 1; + printf("\nSetting verbose on\n"); + } + count++; + } +} + +#define LOGA_DEBUG 0 +#define LOGA_INFO 1 +#include +#include +#include +void MyLog(int LOGA_level, char* format, ...) +{ + static char msg_buf[256]; + va_list args; + struct timeb ts; + + struct tm *timeinfo; + + if (LOGA_level == LOGA_DEBUG && options.verbose == 0) + return; + + ftime(&ts); + timeinfo = localtime(&ts.time); + strftime(msg_buf, 80, "%Y%m%d %H%M%S", timeinfo); + + sprintf(&msg_buf[strlen(msg_buf)], ".%.3hu ", ts.millitm); + + va_start(args, format); + vsnprintf(&msg_buf[strlen(msg_buf)], sizeof(msg_buf) - strlen(msg_buf), format, args); + va_end(args); + + printf("%s\n", msg_buf); + fflush(stdout); +} + + +#if defined(WIN32) || defined(_WINDOWS) +#define mqsleep(A) Sleep(1000*A) +#define START_TIME_TYPE DWORD +static DWORD start_time = 0; +START_TIME_TYPE start_clock(void) +{ + return GetTickCount(); +} +#elif defined(AIX) +#define mqsleep sleep +#define START_TIME_TYPE struct timespec +START_TIME_TYPE start_clock(void) +{ + static struct timespec start; + clock_gettime(CLOCK_REALTIME, &start); + return start; +} +#else +#define mqsleep sleep +#define START_TIME_TYPE struct timeval +START_TIME_TYPE start_clock(void) +{ + struct timeval start_time; + gettimeofday(&start_time, NULL); + return start_time; +} +#endif + + +#if defined(WIN32) +long elapsed(START_TIME_TYPE start_time) +{ + return GetTickCount() - start_time; +} +#elif defined(AIX) +#define assert(a) +long elapsed(struct timespec start) +{ + struct timespec now, res; + + clock_gettime(CLOCK_REALTIME, &now); + ntimersub(now, start, res); + return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L; +} +#else +long elapsed(START_TIME_TYPE start_time) +{ + struct timeval now, res; + + gettimeofday(&now, NULL); + timersub(&now, &start_time, &res); + return (res.tv_sec)*1000 + (res.tv_usec)/1000; +} +#endif + + +#define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d) +#define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e) + +int tests = 0; +int failures = 0; +FILE* xml; +START_TIME_TYPE global_start_time; +char output[3000]; +char* cur_output = output; + + +void write_test_result() +{ + long duration = elapsed(global_start_time); + + fprintf(xml, " time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000); + if (cur_output != output) + { + fprintf(xml, "%s", output); + cur_output = output; + } + fprintf(xml, "\n"); +} + + +void myassert(char* filename, int lineno, char* description, int value, char* format, ...) +{ + ++tests; + if (!value) + { + va_list args; + + ++failures; + printf("Assertion failed, file %s, line %d, description: %s\n", filename, lineno, description); + + va_start(args, format); + vprintf(format, args); + va_end(args); + + cur_output += sprintf(cur_output, "file %s, line %d \n", + description, filename, lineno); + } + else + MyLog(LOGA_DEBUG, "Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description); +} + +#endif // TEST_FRAMEWORK_H_