From 2e590a956476c1e04e486e0fb1a7f3982aa684c8 Mon Sep 17 00:00:00 2001 From: CIPop Date: Mon, 14 Aug 2023 23:22:19 +0000 Subject: [PATCH] Add side-by-side v3 and v5 test. --- MQTTPacket/src/MQTTPacketCommon.h | 7 +- MQTTPacket/test/CMakeLists.txt | 28 +- MQTTPacket/test/test35.c | 904 ++++++++++++++++++++++++++++++ 3 files changed, 933 insertions(+), 6 deletions(-) create mode 100644 MQTTPacket/test/test35.c diff --git a/MQTTPacket/src/MQTTPacketCommon.h b/MQTTPacket/src/MQTTPacketCommon.h index 1eb7ee8..3b4f4bf 100644 --- a/MQTTPacket/src/MQTTPacketCommon.h +++ b/MQTTPacket/src/MQTTPacketCommon.h @@ -43,10 +43,7 @@ enum msgTypes { CONNECT = 1, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL, PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK, - PINGREQ, PINGRESP, DISCONNECT -#if defined(MQTTV5) - , AUTH -#endif + PINGREQ, PINGRESP, DISCONNECT, AUTH }; /** @@ -118,7 +115,7 @@ typedef struct { int rem_len; int32_t len; char state; -}MQTTTransport; +} MQTTTransport; int MQTTPacket_readnb(unsigned char* buf, int32_t buflen, MQTTTransport *trp); diff --git a/MQTTPacket/test/CMakeLists.txt b/MQTTPacket/test/CMakeLists.txt index a18ecc7..a607d97 100644 --- a/MQTTPacket/test/CMakeLists.txt +++ b/MQTTPacket/test/CMakeLists.txt @@ -87,4 +87,30 @@ ADD_TEST( SET_TESTS_PROPERTIES( test3 PROPERTIES TIMEOUT 540 -) \ No newline at end of file +) + +ADD_EXECUTABLE( + test35 + test35.c + ../samples/transport.c +) + +# Disabling deprecation warnings: +# warning: ‘ftime’ is deprecated [-Wdeprecated-declarations] +target_compile_options(test35 PRIVATE -Wno-deprecated-declarations) + +TARGET_LINK_LIBRARIES( + test35 + paho-embed-mqtt3c + paho-embed-mqtt5c +) + +ADD_TEST( + NAME test35 + COMMAND "test35" "--connection" ${MQTT_TEST_BROKER} +) + +SET_TESTS_PROPERTIES( + test35 + PROPERTIES TIMEOUT 540 +) diff --git a/MQTTPacket/test/test35.c b/MQTTPacket/test/test35.c new file mode 100644 index 0000000..77ce036 --- /dev/null +++ b/MQTTPacket/test/test35.c @@ -0,0 +1,904 @@ +/******************************************************************************* + * 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. + * + *******************************************************************************/ + +#include +#include +#include + +//#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 +/* TODO - unused - remove? static struct timeval start_time; */ +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 test_v3(struct Options options) +{ + MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + int rc = 0; + unsigned char buf[200]; + int buflen = sizeof(buf); + int mysock = 0; + MQTTString topicString = MQTTString_initializer; + char* payload = "mypayload"; + int payloadlen = strlen(payload); + int len = 0; + MQTTProperties properties = MQTTProperties_initializer; + MQTTProperty props[10]; + MQTTProperty one; + int msgid = 0; + char* test_topic = "MQTTV5/test/test3_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; +}