diff --git a/MQTTPacket/test/CMakeLists.txt b/MQTTPacket/test/CMakeLists.txt index 9113d962..bbf056c4 100644 --- a/MQTTPacket/test/CMakeLists.txt +++ b/MQTTPacket/test/CMakeLists.txt @@ -15,6 +15,7 @@ ENDIF () # transport.h/c are reused from ../samples: include_directories(../src ../src/V5 ../samples) +# test1 - MQTTv3 serializer test ADD_EXECUTABLE( test1 test1.c @@ -39,6 +40,7 @@ SET_TESTS_PROPERTIES( PROPERTIES TIMEOUT 540 ) +# test2 - MQTTv5 serializer test ADD_EXECUTABLE( test2 test2.c @@ -63,28 +65,28 @@ SET_TESTS_PROPERTIES( PROPERTIES TIMEOUT 540 ) - +# test4 - MQTTv5 broker test ADD_EXECUTABLE( - test3 - test3.c + test4 + test4.c ../samples/transport.c ) # Disabling deprecation warnings: # warning: ‘ftime’ is deprecated [-Wdeprecated-declarations] -target_compile_options(test3 PRIVATE -Wno-deprecated-declarations) +target_compile_options(test4 PRIVATE -Wno-deprecated-declarations) TARGET_LINK_LIBRARIES( - test3 + test4 paho-embed-mqtt5c ) ADD_TEST( - NAME test3 - COMMAND "test3" "--connection" ${MQTT_TEST_BROKER} + NAME test4 + COMMAND "test4" "--connection" ${MQTT_TEST_BROKER} ) SET_TESTS_PROPERTIES( - test3 + test4 PROPERTIES TIMEOUT 540 ) 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/test4.c b/MQTTPacket/test/test4.c new file mode 100644 index 00000000..d3f71e4b --- /dev/null +++ b/MQTTPacket/test/test4.c @@ -0,0 +1,51 @@ +/******************************************************************************* + * 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. + * + *******************************************************************************/ + +/*** + Test MQTTV5Packet against a broker. +***/ + +#include "test_framework.h" +#include "MQTTV5Packet.h" +#include "transport.h" + +#include "test_mqtt5.h" + +int main(int argc, char** argv) +{ + int rc = 0; + int (*tests[])(struct Options) = {NULL, test_v5}; + + xml = fopen("TEST-test4.xml", "w"); + fprintf(xml, "\n", (int)(ARRAY_SIZE(tests) - 1)); + + getopts(argc, argv); + + if (options.test_no == 0) + { /* run all the tests */ + for (options.test_no = 1; options.test_no < ARRAY_SIZE(tests); ++options.test_no) + rc += tests[options.test_no](options); /* return number of failures. 0 = test succeeded */ + } + else + rc = tests[options.test_no](options); /* run just the selected test */ + + if (rc == 0) + MyLog(LOGA_INFO, "verdict pass"); + else + MyLog(LOGA_INFO, "verdict fail"); + + fprintf(xml, "\n"); + fclose(xml); + return rc; +} 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_ diff --git a/MQTTPacket/test/test3.c b/MQTTPacket/test/test_mqtt5.h similarity index 71% rename from MQTTPacket/test/test3.c rename to MQTTPacket/test/test_mqtt5.h index 44afc36c..f1314c37 100644 --- a/MQTTPacket/test/test3.c +++ b/MQTTPacket/test/test_mqtt5.h @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2014, 2023 IBM Corp., Ian Craggs and others + * 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 @@ -10,231 +10,12 @@ * and the Eclipse Distribution License is available at * http://www.eclipse.org/org/documents/edl-v10.php. * - * Contributors: - * Ian Craggs - initial API and implementation and/or initial documentation - * Sergio R. Caprile - clarifications and/or documentation extension *******************************************************************************/ -#include -#include -#include - -#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) +/*** + Test MQTTV5Packet against a broker. +***/ +int test_v5(struct Options options) { MQTTV5Packet_connectData data = MQTTV5Packet_connectData_initializer; int rc = 0; @@ -249,21 +30,21 @@ int test1(struct Options options) MQTTProperty props[10]; MQTTProperty one; int msgid = 0; - char* test_topic = "MQTTV5/test/test3_topic"; + char* test_topic = "MQTTV5/test/test35_topic"; int i = 0; mysock = transport_open(options.host, options.port); if(mysock < 0) return mysock; - fprintf(xml, "\n", (int)(ARRAY_SIZE(tests) - 1)); - - getopts(argc, argv); - - if (options.test_no == 0) - { /* run all the tests */ - for (options.test_no = 1; options.test_no < ARRAY_SIZE(tests); ++options.test_no) - rc += tests[options.test_no](options); /* return number of failures. 0 = test succeeded */ - } - else - rc = tests[options.test_no](options); /* run just the selected test */ - - if (rc == 0) - MyLog(LOGA_INFO, "verdict pass"); - else - MyLog(LOGA_INFO, "verdict fail"); - - fprintf(xml, "\n"); - fclose(xml); - return rc; + return failures; }