29 #if !defined(_WINDOWS) 31 #include <sys/socket.h> 36 #define setenv(a, b, c) _putenv_s(a, b) 39 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) 59 "tcp://mqtt.eclipse.org:1883",
61 "tcp://localhost:1883",
75 if (strcmp(argv[count],
"--test_no") == 0)
82 else if (strcmp(argv[count],
"--connection") == 0)
92 else if (strcmp(argv[count],
"--haconnections") == 0)
96 char* tok = strtok(argv[count],
" ");
104 tok = strtok(NULL,
" ");
110 else if (strcmp(argv[count],
"--proxy_connection") == 0)
117 else if (strcmp(argv[count],
"--MQTTversion") == 0)
127 else if (strcmp(argv[count],
"--iterations") == 0)
134 else if (strcmp(argv[count],
"--verbose") == 0)
137 printf(
"\nSetting verbose on\n");
148 #include <sys/timeb.h> 153 #if defined(_WIN32) || defined(_WINDOWS) 163 #if defined(_WIN32) || defined(_WINDOWS) 167 gettimeofday(&ts, NULL);
168 localtime_r(&ts.tv_sec, &timeinfo);
170 strftime(msg_buf, 80,
"%Y%m%d %H%M%S", &timeinfo);
172 #if defined(_WIN32) || defined(_WINDOWS) 173 sprintf(&msg_buf[strlen(msg_buf)],
".%.3hu ", ts.millitm);
175 sprintf(&msg_buf[strlen(msg_buf)],
".%.3lu ", ts.tv_usec / 1000);
178 va_start(args, format);
179 vsnprintf(&msg_buf[strlen(msg_buf)],
sizeof(msg_buf) - strlen(msg_buf), format, args);
182 printf(
"%s\n", msg_buf);
187 #if defined(_WIN32) || defined(_WINDOWS) 188 #define mqsleep(A) Sleep(1000*A) 189 #define START_TIME_TYPE DWORD 190 static DWORD start_time = 0;
193 return GetTickCount();
196 #define mqsleep sleep 197 #define START_TIME_TYPE struct timespec 200 static struct timespec start;
201 clock_gettime(CLOCK_REALTIME, &start);
205 #define mqsleep sleep 206 #define START_TIME_TYPE struct timeval 210 struct timeval start_time;
211 gettimeofday(&start_time, NULL);
220 return GetTickCount() - start_time;
224 long elapsed(
struct timespec start)
226 struct timespec now, res;
228 clock_gettime(CLOCK_REALTIME, &now);
229 ntimersub(now, start, res);
230 return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L;
235 struct timeval now, res;
237 gettimeofday(&now, NULL);
238 timersub(&now, &start_time, &res);
239 return (res.tv_sec)*1000 + (res.tv_usec)/1000;
244 #define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d) 245 #define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e) 259 fprintf(
xml,
" time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000);
265 fprintf(
xml,
"</testcase>\n");
277 MyLog(
LOGA_INFO,
"Assertion failed, file %s, line %d, description: %s\n", filename, lineno, description);
279 va_start(args, format);
280 vprintf(format, args);
284 description, filename, lineno);
287 MyLog(
LOGA_DEBUG,
"Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description);
301 char* topicName = NULL;
308 pubmsg.
payload =
"a much longer message that we can shorten to the extent that we need to payload up to 11";
343 printf(
"No message received within timeout period\n");
350 printf(
"Message received on topic %s is %.*s.\n", topicName, m->
payloadlen, (
char*)(m->
payload));
367 fprintf(
xml,
"<testcase classname=\"test1\" name=\"single threaded client using receive\"");
370 MyLog(
LOGA_INFO,
"Starting test 1 - single threaded client using receive");
452 MyLog(
LOGA_DEBUG,
"Callback: %d message received on topic %s is %.*s.",
472 int wait_seconds = 0;
477 test2_pubmsg.
payload =
"a much longer message that we can shorten to the extent that we need to";
507 assert(
"Message Arrived", wait_seconds > 0,
508 "Time out waiting for message %d\n", i );
526 assert(
"All Deliveries Complete", wait_seconds > 0,
527 "Number of deliveryCompleted callbacks was %d\n",
535 char* testname =
"test2";
543 fprintf(
xml,
"<testcase classname=\"test1\" name=\"multi-threaded client using callbacks\"");
544 MyLog(
LOGA_INFO,
"Starting test 2 - multi-threaded client using callbacks");
611 char* testname =
"test3";
617 fprintf(
xml,
"<testcase classname=\"test1\" name=\"connack return codes\"");
628 assert(
"identifier rejected", rc == 2,
"rc was %d\n", rc);
636 assert(
"broker unavailable", rc == 3,
"rc was %d\n", rc);
642 assert(
"Bad user name or password", rc == 4,
"rc was %d\n", rc);
683 char* testname =
"test 4";
684 char*
topic =
"Persistence test 1";
689 char* topicName = NULL;
733 for (i = 0; i <
count; ++i)
735 sprintf(buffer,
"Message sequence no %d", i);
746 assert(
"should get some tokens back", tokens != NULL,
"tokens was %p", tokens);
751 while (tokens[i] != -1)
754 assert1(
"no of tokens should be count", i == count,
"no of tokens %d count %d", i, count);
765 assert(
"should get some tokens back", tokens != NULL,
"tokens was %p", tokens);
769 while (tokens[i] != -1)
772 assert1(
"no of tokens should be count", i == count,
"no of tokens %d count %d", i, count);
782 for (i = 0; i <
count; ++i)
791 assert(
"No duplicates should be received for qos 2", qos == 1,
"qos is %d", qos);
798 assert(
"should get a message", m != NULL,
"m was %p", m);
802 assert(
"topicName is correct", strcmp(topicName, topic) == 0,
"topicName is %s", topicName);
812 assert(
"should get no tokens back", tokens == NULL,
"tokens was %p", tokens);
829 fprintf(
xml,
"<testcase classname=\"test1\" name=\"persistence\"");
838 fprintf(
xml,
"</testcase>\n");
850 char* testname =
"test 5";
851 char*
topic =
"Persistence test 2";
860 fprintf(
xml,
"<testcase classname=\"test1\" name=\"disconnect with quiesce timeout should allow exchanges to complete\"");
863 MyLog(
LOGA_INFO,
"Starting test 5 - disconnect with quiesce timeout should allow exchanges to complete");
888 for (i = 0; i <
count; ++i)
890 sprintf(buffer,
"Message sequence no %d", i);
901 assert(
"should get no tokens back", tokens == NULL,
"tokens was %p", tokens);
925 printf(
"%s -> Callback: connection lost\n", (c ==
test6_c1) ?
"Client-1" :
"Client-2");
931 printf(
"Client-2 -> Callback: publish complete for token %d\n", token);
940 printf(
"%s -> Callback: message received on topic '%s' is '%.*s'.\n",
952 char* testname =
"test6";
957 char* mqttsas_topic =
"MQTTSAS topic";
960 MyLog(
LOGA_INFO,
"Starting test 6 - connectionLost and will messages");
961 fprintf(
xml,
"<testcase classname=\"test1\" name=\"connectionLost and will messages\"");
1019 while (++count < 40)
1038 assert(
"Client-2 still connected", rc == 1,
"isconnected is %d", rc);
1041 assert(
"Client-1 not connected", rc == 0,
"isconnected is %d", rc);
1059 char* testname =
"test6a";
1064 char* mqttsas_topic =
"MQTTSAS topic";
1067 MyLog(
LOGA_INFO,
"Starting test 6 - connectionLost and binary will messages");
1068 fprintf(
xml,
"<testcase classname=\"test1\" name=\"connectionLost and binary will messages\"");
1127 while (++count < 40)
1146 assert(
"Client-2 still connected", rc == 1,
"isconnected is %d", rc);
1149 assert(
"Client-1 not connected", rc == 0,
"isconnected is %d", rc);
1170 xml = fopen(
"TEST-test1.xml",
"w");
1171 fprintf(
xml,
"<testsuite name=\"test1\" tests=\"%d\">\n", (
int)(
ARRAY_SIZE(
tests) - 1));
1173 setenv(
"MQTT_C_CLIENT_TRACE",
"ON", 1);
1174 setenv(
"MQTT_C_CLIENT_TRACE_LEVEL",
"ERROR", 1);
1194 fprintf(
xml,
"</testsuite>\n");
#define assert1(a, b, c, d, e)
int MQTTClient_waitForCompletion(MQTTClient handle, MQTTClient_deliveryToken mdt, unsigned long timeout)
void test2_sendAndReceive(MQTTClient *c, int qos, char *test_topic)
enum MQTTPropertyCodes value
int MQTTClient_receive(MQTTClient handle, char **topicName, int *topicLen, MQTTClient_message **message, unsigned long timeout)
void test6_connectionLost(void *context, char *cause)
FMT_INLINE std::basic_string< Char > format(const S &format_str, Args &&...args)
#define MQTTCLIENT_SUCCESS
int test6(struct Options options)
int MQTTClient_isConnected(MQTTClient handle)
int MQTTClient_connect(MQTTClient handle, MQTTClient_connectOptions *options)
int MQTTClient_publish(MQTTClient handle, const char *topicName, int payloadlen, const void *payload, int qos, int retained, MQTTClient_deliveryToken *deliveryToken)
volatile int test6_will_message_arrived
int MQTTClient_disconnect(MQTTClient handle, int timeout)
int test4(struct Options options)
START_TIME_TYPE start_clock(void)
int test6_messageArrived(void *context, char *topicName, int topicLen, MQTTClient_message *m)
int test6a(struct Options options)
size_t strftime(char *str, size_t count, const char *format, const std::tm *time)
long elapsed(START_TIME_TYPE start_time)
int test2(struct Options options)
#define MQTTClient_message_initializer
#define MQTTClient_willOptions_initializer
MQTTClient_message test2_pubmsg
int main(int argc, char **argv)
int MQTTClient_setCallbacks(MQTTClient handle, void *context, MQTTClient_connectionLost *cl, MQTTClient_messageArrived *ma, MQTTClient_deliveryComplete *dc)
int MQTTClient_publishMessage(MQTTClient handle, const char *topicName, MQTTClient_message *message, MQTTClient_deliveryToken *deliveryToken)
void getopts(int argc, char **argv)
volatile int test6_connection_lost_called
void test6_deliveryComplete(void *context, MQTTClient_deliveryToken token)
int test2_messageArrived(void *context, char *topicName, int topicLen, MQTTClient_message *m)
int MQTTClient_unsubscribe(MQTTClient handle, const char *topic)
struct MQTTClient_willOptions::@56 payload
void myassert(char *filename, int lineno, char *description, int value, char *format,...)
#define MQTTVERSION_3_1_1
int test2_deliveryCompleted
char * test6_will_message
void MQTTClient_freeMessage(MQTTClient_message **message)
void test1_sendAndReceive(MQTTClient *c, int qos, char *test_topic)
void MQTTClient_yield(void)
void test2_deliveryComplete(void *context, MQTTClient_deliveryToken dt)
void MQTTClient_destroy(MQTTClient *handle)
int test3(struct Options options)
volatile int test2_arrivedcount
#define MQTTCLIENT_PERSISTENCE_DEFAULT
void MyLog(int LOGA_level, char *format,...)
int MQTTClient_subscribe(MQTTClient handle, const char *topic, int qos)
void MQTTClient_free(void *memory)
#define MQTTCLIENT_PERSISTENCE_NONE
#define MQTTClient_connectOptions_initializer
int test1(struct Options options)
int test5(struct Options options)
int MQTTClient_deliveryToken
int MQTTClient_getPendingDeliveryTokens(MQTTClient handle, MQTTClient_deliveryToken **tokens)
int MQTTClient_create(MQTTClient *handle, const char *serverURI, const char *clientId, int persistence_type, void *persistence_context)
#define MQTTVERSION_DEFAULT
void write_test_result(void)
MQTTClient_willOptions * will
#define assert(a, b, c, d)
START_TIME_TYPE global_start_time
struct MQTTClient_connectOptions::@58 binarypwd