12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802 |
- /*******************************************************************************
- * Copyright (c) 2009, 2022 IBM Corp., Ian Craggs
- *
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v2.0
- * and Eclipse Distribution License v1.0 which accompany this distribution.
- *
- * The Eclipse Public License is available at
- * https://www.eclipse.org/legal/epl-2.0/
- * 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
- * Ian Craggs - MQTT 3.1.1 support
- * Ian Craggs - test8 - failure callbacks
- *******************************************************************************/
- /**
- * @file
- * Tests for the Paho Asynchronous MQTT C client
- */
- #include "MQTTAsync.h"
- #include <string.h>
- #include <stdlib.h>
- #if !defined(_WINDOWS)
- #include <sys/time.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <errno.h>
- #else
- #include <windows.h>
- #endif
- #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
- void usage(void)
- {
- printf("help!!\n");
- exit(EXIT_FAILURE);
- }
- struct Options
- {
- char* connection; /**< connection to system under test. */
- int verbose;
- int test_no;
- int size; /**< size of big message */
- int MQTTVersion;
- int iterations;
- } options =
- {
- "localhost:1883",
- 0,
- -1,
- 10000,
- MQTTVERSION_DEFAULT,
- 1,
- };
- 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], "--size") == 0)
- {
- if (++count < argc)
- options.size = atoi(argv[count]);
- else
- usage();
- }
- else if (strcmp(argv[count], "--connection") == 0)
- {
- if (++count < argc)
- options.connection = argv[count];
- else
- usage();
- }
- else if (strcmp(argv[count], "--MQTTversion") == 0)
- {
- if (++count < argc)
- {
- options.MQTTVersion = atoi(argv[count]);
- printf("setting MQTT version to %d\n", options.MQTTVersion);
- }
- else
- usage();
- }
- else if (strcmp(argv[count], "--iterations") == 0)
- {
- if (++count < argc)
- options.iterations = atoi(argv[count]);
- else
- usage();
- }
- else if (strcmp(argv[count], "--verbose") == 0)
- options.verbose = 1;
- count++;
- }
- }
- #define LOGA_DEBUG 0
- #define LOGA_INFO 1
- #include <stdarg.h>
- #include <time.h>
- #include <sys/timeb.h>
- void MyLog(int LOGA_level, char* format, ...)
- {
- static char msg_buf[256];
- va_list args;
- #if defined(_WIN32) || defined(_WINDOWS)
- struct timeb ts;
- #else
- struct timeval ts;
- #endif
- struct tm timeinfo;
- if (LOGA_level == LOGA_DEBUG && options.verbose == 0)
- return;
- #if defined(_WIN32) || defined(_WINDOWS)
- ftime(&ts);
- localtime_s(&timeinfo, &ts.time);
- #else
- gettimeofday(&ts, NULL);
- localtime_r(&ts.tv_sec, &timeinfo);
- #endif
- strftime(msg_buf, 80, "%Y%m%d %H%M%S", &timeinfo);
- #if defined(_WIN32) || defined(_WINDOWS)
- sprintf(&msg_buf[strlen(msg_buf)], ".%.3hu ", ts.millitm);
- #else
- sprintf(&msg_buf[strlen(msg_buf)], ".%.3lu ", ts.tv_usec / 1000L);
- #endif
- 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(void)
- {
- 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, "</testcase>\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, "<failure type=\"%s\">file %s, line %d </failure>\n",
- description, filename, lineno);
- }
- else
- MyLog(LOGA_DEBUG, "Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description);
- }
- volatile int test_finished = 0;
- char* test_topic = "async test topic";
- void test1_onDisconnect(void* context, MQTTAsync_successData* response)
- {
- MQTTAsync c = (MQTTAsync)context;
- MyLog(LOGA_DEBUG, "In onDisconnect callback %p", c);
- test_finished = 1;
- }
- void test1_onUnsubscribe(void* context, MQTTAsync_successData* response)
- {
- MQTTAsync c = (MQTTAsync)context;
- MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
- int rc;
- MyLog(LOGA_DEBUG, "In onUnsubscribe onSuccess callback %p", c);
- opts.onSuccess = test1_onDisconnect;
- opts.context = c;
- opts.timeout = 1000;
- rc = MQTTAsync_disconnect(c, &opts);
- assert("Disconnect successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- }
- int test1_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message)
- {
- MQTTAsync c = (MQTTAsync)context;
- static int message_count = 0;
- int rc;
- MyLog(LOGA_DEBUG, "In messageArrived callback %p", c);
- if (++message_count == 1)
- {
- MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
- pubmsg.payloadlen = 11;
- pubmsg.qos = 2;
- pubmsg.retained = 0;
- rc = MQTTAsync_sendMessage(c, test_topic, &pubmsg, &opts);
- assert("Good rc from send", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- }
- else
- {
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- opts.onSuccess = test1_onUnsubscribe;
- opts.context = c;
- rc = MQTTAsync_unsubscribe(c, test_topic, &opts);
- assert("Unsubscribe successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- }
- MQTTAsync_freeMessage(&message);
- MQTTAsync_free(topicName);
- return 1;
- }
- void test1_onSubscribe(void* context, MQTTAsync_successData* response)
- {
- MQTTAsync c = (MQTTAsync)context;
- MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
- int rc;
- MyLog(LOGA_DEBUG, "In subscribe onSuccess callback %p granted qos %d", c, response->alt.qos);
- pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
- pubmsg.payloadlen = 11;
- pubmsg.qos = 2;
- pubmsg.retained = 0;
- rc = MQTTAsync_send(c, test_topic, pubmsg.payloadlen, pubmsg.payload, pubmsg.qos, pubmsg.retained, NULL);
- assert("Good rc from send", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- }
- void test1_onConnect(void* context, MQTTAsync_successData* response)
- {
- MQTTAsync c = (MQTTAsync)context;
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- int rc;
- MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
- opts.onSuccess = test1_onSubscribe;
- opts.context = c;
- rc = MQTTAsync_subscribe(c, test_topic, 2, &opts);
- assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- test_finished = 1;
- }
- /*********************************************************************
- Test1: Basic connect, subscribe send and receive.
- *********************************************************************/
- int test1(struct Options options)
- {
- int subsqos = 2;
- MQTTAsync c;
- MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
- MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
- int rc = 0;
- char* test_topic = "C client test1";
- MyLog(LOGA_INFO, "Starting test 1 - asynchronous connect");
- fprintf(xml, "<testcase classname=\"test4\" name=\"asynchronous connect\"");
- global_start_time = start_clock();
- rc = MQTTAsync_create(&c, options.connection, "async_test",
- MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
- assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- if (rc != MQTTASYNC_SUCCESS)
- {
- MQTTAsync_destroy(&c);
- goto exit;
- }
- rc = MQTTAsync_setCallbacks(c, c, NULL, test1_messageArrived, NULL);
- assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- opts.keepAliveInterval = 20;
- opts.cleansession = 1;
- opts.username = "testuser";
- opts.password = "testpassword";
- opts.MQTTVersion = options.MQTTVersion;
- opts.will = &wopts;
- opts.will->message = "will message";
- opts.will->qos = 1;
- opts.will->retained = 0;
- opts.will->topicName = "will topic";
- opts.will = NULL;
- opts.onSuccess = test1_onConnect;
- opts.onFailure = NULL;
- opts.context = c;
- MyLog(LOGA_DEBUG, "Connecting");
- rc = MQTTAsync_connect(c, &opts);
- rc = 0;
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- while (!test_finished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- MQTTAsync_destroy(&c);
- exit:
- MyLog(LOGA_INFO, "TEST1: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", tests, failures);
- write_test_result();
- return failures;
- }
- int test2_onFailure_called = 0;
- void test2_onFailure(void* context, MQTTAsync_failureData* response)
- {
- MyLog(LOGA_DEBUG, "In connect onFailure callback, context %p", context);
- test2_onFailure_called++;
- test_finished = 1;
- }
- void test2_onConnect(void* context, MQTTAsync_successData* response)
- {
- MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p\n", context);
- assert("Connect should not succeed", 0, "connect success callback was called", 0);
- test_finished = 1;
- }
- /*********************************************************************
- Test2: connect timeout
- *********************************************************************/
- int test2(struct Options options)
- {
- int subsqos = 2;
- MQTTAsync c;
- MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
- MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
- int rc = 0;
- char* test_topic = "C client test2";
- test_finished = 0;
- MyLog(LOGA_INFO, "Starting test 2 - connect timeout");
- fprintf(xml, "<testcase classname=\"test4\" name=\"connect timeout\"");
- global_start_time = start_clock();
- rc = MQTTAsync_create(&c, "tcp://9.20.96.160:66", "connect timeout",
- MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
- assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- if (rc != MQTTASYNC_SUCCESS)
- {
- MQTTAsync_destroy(&c);
- goto exit;
- }
- rc = MQTTAsync_setCallbacks(c, c, NULL, test1_messageArrived, NULL);
- assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- opts.connectTimeout = 5;
- opts.keepAliveInterval = 20;
- opts.cleansession = 1;
- opts.username = "testuser";
- opts.binarypwd.data = "testpassword";
- opts.binarypwd.len = (int)strlen(opts.binarypwd.data);
- opts.MQTTVersion = options.MQTTVersion;
- opts.will = &wopts;
- opts.will->message = "will message";
- opts.will->qos = 1;
- opts.will->retained = 0;
- opts.will->topicName = "will topic";
- opts.will = NULL;
- opts.onSuccess = test2_onConnect;
- opts.onFailure = test2_onFailure;
- opts.context = c;
- MyLog(LOGA_DEBUG, "Connecting");
- rc = MQTTAsync_connect(c, &opts);
- rc = 0;
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- while (!test_finished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- MQTTAsync_destroy(&c);
- exit:
- assert("Connect onFailure should be called once", test2_onFailure_called == 1,
- "connect onFailure was called %d times", test2_onFailure_called);
- MyLog(LOGA_INFO, "TEST2: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", tests, failures);
- write_test_result();
- return failures;
- }
- typedef struct
- {
- MQTTAsync c;
- int index;
- char clientid[24];
- char test_topic[100];
- int message_count;
- } client_data;
- void test3_onDisconnect(void* context, MQTTAsync_successData* response)
- {
- client_data* cd = (client_data*)context;
- MyLog(LOGA_DEBUG, "In onDisconnect callback for client \"%s\"", cd->clientid);
- test_finished++;
- }
- void test3_onPublish(void* context, MQTTAsync_successData* response)
- {
- client_data* cd = (client_data*)context;
- MyLog(LOGA_DEBUG, "In QoS 0 onPublish callback for client \"%s\"", cd->clientid);
- }
- void test3_onUnsubscribe(void* context, MQTTAsync_successData* response)
- {
- client_data* cd = (client_data*)context;
- MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
- int rc;
- MyLog(LOGA_DEBUG, "In onUnsubscribe onSuccess callback \"%s\"", cd->clientid);
- opts.onSuccess = test3_onDisconnect;
- opts.context = cd;
- opts.timeout = 1000;
- rc = MQTTAsync_disconnect(cd->c, &opts);
- assert("Disconnect successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- }
- int test3_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message)
- {
- client_data* cd = (client_data*)context;
- int rc;
- MyLog(LOGA_DEBUG, "In messageArrived callback \"%s\" message count ", cd->clientid);
- if (++cd->message_count == 1)
- {
- MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
- pubmsg.payloadlen = 25;
- pubmsg.qos = 1;
- pubmsg.retained = 0;
- rc = MQTTAsync_sendMessage(cd->c, cd->test_topic, &pubmsg, &opts);
- assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- }
- else if (cd->message_count == 2)
- {
- MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- pubmsg.payload = "a QoS 0 message that we can shorten to the extent that we need to payload up to 11";
- pubmsg.payloadlen = 29;
- pubmsg.qos = 0;
- pubmsg.retained = 0;
- opts.context = cd;
- opts.onSuccess = test3_onPublish;
- rc = MQTTAsync_sendMessage(cd->c, cd->test_topic, &pubmsg, &opts);
- assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- }
- else
- {
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- opts.onSuccess = test3_onUnsubscribe;
- opts.context = cd;
- rc = MQTTAsync_unsubscribe(cd->c, cd->test_topic, &opts);
- assert("Unsubscribe successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- }
- MQTTAsync_freeMessage(&message);
- MQTTAsync_free(topicName);
- return 1;
- }
- void test3_onSubscribe(void* context, MQTTAsync_successData* response)
- {
- client_data* cd = (client_data*)context;
- MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
- int rc;
- MyLog(LOGA_DEBUG, "In subscribe onSuccess callback \"%s\"", cd->clientid);
- pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
- pubmsg.payloadlen = 11;
- pubmsg.qos = 2;
- pubmsg.retained = 0;
- rc = MQTTAsync_send(cd->c, cd->test_topic, pubmsg.payloadlen, pubmsg.payload, pubmsg.qos, pubmsg.retained, NULL);
- assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- }
- void test3_onConnect(void* context, MQTTAsync_successData* response)
- {
- client_data* cd = (client_data*)context;
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- int rc;
- MyLog(LOGA_DEBUG, "In connect onSuccess callback, \"%s\"", cd->clientid);
- opts.onSuccess = test3_onSubscribe;
- opts.context = cd;
- rc = MQTTAsync_subscribe(cd->c, cd->test_topic, 2, &opts);
- assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- test_finished++;
- }
- void test3_onFailure(void* context, MQTTAsync_failureData* response)
- {
- client_data* cd = (client_data*)context;
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- assert("Should have connected", 0, "%s failed to connect\n", cd->clientid);
- MyLog(LOGA_DEBUG, "In connect onFailure callback, \"%s\" rc %d\n", cd->clientid, response ? response->code : -999);
- if (response && response->message)
- MyLog(LOGA_DEBUG, "In connect onFailure callback, \"%s\"\n", response->message);
- test_finished++;
- }
- /*********************************************************************
- Test3: More than one client object - simultaneous working.
- *********************************************************************/
- int test3(struct Options options)
- {
- #define num_clients 10
- int subsqos = 2;
- MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
- MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
- int rc = 0;
- int i;
- client_data clientdata[num_clients];
- test_finished = 0;
- MyLog(LOGA_INFO, "Starting test 3 - multiple connections");
- fprintf(xml, "<testcase classname=\"test4\" name=\"multiple connections\"");
- global_start_time = start_clock();
- for (i = 0; i < num_clients; ++i)
- {
- sprintf(clientdata[i].clientid, "async_test3_num_%d", i);
- sprintf(clientdata[i].test_topic, "async test3 topic num %d", i);
- clientdata[i].index = i;
- clientdata[i].message_count = 0;
- rc = MQTTAsync_create(&(clientdata[i].c), options.connection, clientdata[i].clientid,
- MQTTCLIENT_PERSISTENCE_NONE, NULL);
- assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- rc = MQTTAsync_setCallbacks(clientdata[i].c, &clientdata[i], NULL, test3_messageArrived, NULL);
- assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- opts.keepAliveInterval = 20;
- opts.cleansession = 1;
- opts.username = "testuser";
- opts.password = "testpassword";
- opts.MQTTVersion = options.MQTTVersion;
- opts.will = &wopts;
- opts.will->message = "will message";
- opts.will->qos = 1;
- opts.will->retained = 0;
- opts.will->topicName = "will topic";
- opts.onSuccess = test3_onConnect;
- opts.onFailure = test3_onFailure;
- opts.context = &clientdata[i];
- MyLog(LOGA_DEBUG, "Connecting");
- rc = MQTTAsync_connect(clientdata[i].c, &opts);
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- }
- while (test_finished < num_clients)
- {
- MyLog(LOGA_DEBUG, "num_clients %d test_finished %d\n", num_clients, test_finished);
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- }
- MyLog(LOGA_DEBUG, "TEST3: destroying clients");
- for (i = 0; i < num_clients; ++i)
- MQTTAsync_destroy(&clientdata[i].c);
- //exit:
- MyLog(LOGA_INFO, "TEST3: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", tests, failures);
- write_test_result();
- return failures;
- }
- void* test4_payload = NULL;
- int test4_payloadlen = 0;
- void test4_onPublish(void* context, MQTTAsync_successData* response)
- {
- MQTTAsync c = (MQTTAsync)context;
- MyLog(LOGA_DEBUG, "In publish onSuccess callback, context %p", context);
- }
- int test4_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message)
- {
- MQTTAsync c = (MQTTAsync)context;
- static int message_count = 0;
- int rc, i;
- MyLog(LOGA_DEBUG, "In messageArrived callback %p", c);
- assert("Message size correct", message->payloadlen == test4_payloadlen,
- "message size was %d", message->payloadlen);
- for (i = 0; i < options.size; ++i)
- {
- if (((char*)test4_payload)[i] != ((char*)message->payload)[i])
- {
- assert("Message contents correct", ((char*)test4_payload)[i] != ((char*)message->payload)[i],
- "message content was %c", ((char*)message->payload)[i]);
- break;
- }
- }
- if (++message_count == 1)
- {
- MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- pubmsg.payload = test4_payload;
- pubmsg.payloadlen = test4_payloadlen;
- pubmsg.qos = 1;
- pubmsg.retained = 0;
- opts.onSuccess = test4_onPublish;
- opts.context = c;
- rc = MQTTAsync_sendMessage(c, test_topic, &pubmsg, &opts);
- }
- else if (message_count == 2)
- {
- MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- pubmsg.payload = test4_payload;
- pubmsg.payloadlen = test4_payloadlen;
- pubmsg.qos = 0;
- pubmsg.retained = 0;
- opts.onSuccess = test4_onPublish;
- opts.context = c;
- rc = MQTTAsync_sendMessage(c, test_topic, &pubmsg, &opts);
- }
- else
- {
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- opts.onSuccess = test1_onUnsubscribe;
- opts.context = c;
- rc = MQTTAsync_unsubscribe(c, test_topic, &opts);
- assert("Unsubscribe successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- }
- MQTTAsync_freeMessage(&message);
- MQTTAsync_free(topicName);
- return 1;
- }
- void test4_onSubscribe(void* context, MQTTAsync_successData* response)
- {
- MQTTAsync c = (MQTTAsync)context;
- MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
- int rc, i;
- MyLog(LOGA_DEBUG, "In subscribe onSuccess callback %p", c);
- pubmsg.payload = test4_payload = malloc(options.size);
- pubmsg.payloadlen = test4_payloadlen = options.size;
- srand(33);
- for (i = 0; i < options.size; ++i)
- ((char*)pubmsg.payload)[i] = rand() % 256;
- pubmsg.qos = 2;
- pubmsg.retained = 0;
- rc = MQTTAsync_send(c, test_topic, pubmsg.payloadlen, pubmsg.payload, pubmsg.qos, pubmsg.retained, NULL);
- }
- void test4_onConnect(void* context, MQTTAsync_successData* response)
- {
- MQTTAsync c = (MQTTAsync)context;
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- int rc;
- MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
- opts.onSuccess = test4_onSubscribe;
- opts.context = c;
- rc = MQTTAsync_subscribe(c, test_topic, 2, &opts);
- assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- test_finished = 1;
- }
- /*********************************************************************
- Test4: Send and receive big messages
- *********************************************************************/
- int test4(struct Options options)
- {
- int subsqos = 2;
- MQTTAsync c;
- MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
- MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
- int rc = 0;
- char* test_topic = "C client test4";
- test_finished = failures = 0;
- MyLog(LOGA_INFO, "Starting test 4 - big messages");
- fprintf(xml, "<testcase classname=\"test4\" name=\"big messages\"");
- global_start_time = start_clock();
- rc = MQTTAsync_create(&c, options.connection, "async_test_4",
- MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
- assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- if (rc != MQTTASYNC_SUCCESS)
- {
- MQTTAsync_destroy(&c);
- goto exit;
- }
- rc = MQTTAsync_setCallbacks(c, c, NULL, test4_messageArrived, NULL);
- assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- opts.keepAliveInterval = 20;
- opts.cleansession = 1;
- opts.username = "testuser";
- opts.password = "testpassword";
- opts.MQTTVersion = options.MQTTVersion;
- opts.will = &wopts;
- opts.will->message = "will message";
- opts.will->qos = 1;
- opts.will->retained = 0;
- opts.will->topicName = "will topic";
- opts.will = NULL;
- opts.onSuccess = test4_onConnect;
- opts.onFailure = NULL;
- opts.context = c;
- MyLog(LOGA_DEBUG, "Connecting");
- rc = MQTTAsync_connect(c, &opts);
- rc = 0;
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- while (!test_finished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(1000L);
- #endif
- MQTTAsync_destroy(&c);
- exit:
- MyLog(LOGA_INFO, "TEST4: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", tests, failures);
- write_test_result();
- return failures;
- }
- void test5_onConnectFailure(void* context, MQTTAsync_failureData* response)
- {
- MQTTAsync c = (MQTTAsync)context;
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- MyLog(LOGA_DEBUG, "In connect onFailure callback, context %p", context);
- MyLog(LOGA_INFO, "Connack rc is %d", response ? response->code : -999);
- test_finished = 1;
- }
- void test5_onConnect(void* context, MQTTAsync_successData* response)
- {
- MQTTAsync c = (MQTTAsync)context;
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- MyLog(LOGA_DEBUG, "In connect onFailure callback, context %p", context);
- test_finished = 1;
- }
- /********************************************************************
- Test5: Connack return codes
- *********************************************************************/
- int test5(struct Options options)
- {
- int subsqos = 2;
- MQTTAsync c;
- MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
- MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
- int rc = 0;
- char* test_topic = "C client test1";
- test_finished = failures = 0;
- MyLog(LOGA_INFO, "Starting test 5 - connack return codes");
- fprintf(xml, "<testcase classname=\"test4\" name=\"connack return codes\"");
- global_start_time = start_clock();
- rc = MQTTAsync_create(&c, options.connection, "a clientid that is too long to be accepted",
- MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
- assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- if (rc != MQTTASYNC_SUCCESS)
- {
- MQTTAsync_destroy(&c);
- goto exit;
- }
- rc = MQTTAsync_setCallbacks(c, c, NULL, test1_messageArrived, NULL);
- assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- opts.onSuccess = test5_onConnect;
- opts.onFailure = test5_onConnectFailure;
- opts.context = c;
- MyLog(LOGA_DEBUG, "Connecting");
- rc = MQTTAsync_connect(c, &opts);
- rc = 0;
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- while (!test_finished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- MQTTAsync_destroy(&c);
- exit:
- MyLog(LOGA_INFO, "TEST5: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", tests, failures);
- write_test_result();
- return failures;
- }
- typedef struct
- {
- MQTTAsync c;
- int should_fail;
- } test6_client_info;
- void test6_onConnectFailure(void* context, MQTTAsync_failureData* response)
- {
- test6_client_info cinfo = *(test6_client_info*)context;
- MyLog(LOGA_DEBUG, "In connect onFailure callback, context %p", context);
- if (response)
- MyLog(LOGA_INFO, "Connack rc is %d", response->code);
- assert("Should fail to connect", cinfo.should_fail, "should_fail was %d\n", cinfo.should_fail);
- test_finished = 1;
- }
- void test6_onConnect(void* context, MQTTAsync_successData* response)
- {
- test6_client_info cinfo = *(test6_client_info*)context;
- MyLog(LOGA_DEBUG, "In connect success callback, context %p", context);
- assert("Should connect correctly", !cinfo.should_fail, "should_fail was %d\n", cinfo.should_fail);
- test_finished = 1;
- }
- void test6_onDisconnect(void* context, MQTTAsync_successData* response)
- {
- test6_client_info cinfo = *(test6_client_info*)context;
- MyLog(LOGA_DEBUG, "In onDisconnect callback %p", cinfo.c);
- test_finished = 1;
- }
- /********************************************************************
- Test6: HA connections
- *********************************************************************/
- int test6(struct Options options)
- {
- int subsqos = 2;
- test6_client_info cinfo;
- MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
- MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
- MQTTAsync_disconnectOptions dopts = MQTTAsync_disconnectOptions_initializer;
- int rc = 0;
- char* test_topic = "C client test1";
- char* uris[2] = {options.connection, options.connection};
- failures = 0;
- MyLog(LOGA_INFO, "Starting test 6 - HA connections");
- fprintf(xml, "<testcase classname=\"test4\" name=\"HA connections\"");
- global_start_time = start_clock();
- test_finished = 0;
- cinfo.should_fail = 1; /* fail to connect */
- rc = MQTTAsync_create(&cinfo.c, "tcp://rubbish:1883", "async ha connection",
- MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
- assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- if (rc != MQTTASYNC_SUCCESS)
- {
- MQTTAsync_destroy(&cinfo.c);
- goto exit;
- }
- rc = MQTTAsync_setCallbacks(cinfo.c, cinfo.c, NULL, test1_messageArrived, NULL);
- assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- opts.onSuccess = test6_onConnect;
- opts.onFailure = test6_onConnectFailure;
- opts.context = &cinfo;
- opts.MQTTVersion = options.MQTTVersion;
- MyLog(LOGA_DEBUG, "Connecting");
- rc = MQTTAsync_connect(cinfo.c, &opts);
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- while (!test_finished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- test_finished = 0;
- cinfo.should_fail = 0; /* should connect through the serverURIs in connect options*/
- opts.onSuccess = test6_onConnect;
- opts.onFailure = test6_onConnectFailure;
- opts.context = &cinfo;
- opts.serverURIs = uris;
- opts.serverURIcount = 2;
- MyLog(LOGA_DEBUG, "Connecting");
- rc = MQTTAsync_connect(cinfo.c, &opts);
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- while (!test_finished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- test_finished = 0;
- dopts.timeout = 0;
- dopts.onSuccess = test6_onDisconnect;
- dopts.context = &cinfo;
- MQTTAsync_disconnect(cinfo.c, &dopts);
- while (!test_finished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- MQTTAsync_destroy(&cinfo.c);
- exit:
- MyLog(LOGA_INFO, "TEST6: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", tests, failures);
- write_test_result();
- return failures;
- }
- /********************************************************************
- Test7: Persistence
- *********************************************************************/
- char* test7_topic = "C client test7";
- int test7_messageCount = 0;
- void test7_onDisconnectFailure(void* context, MQTTAsync_failureData* response)
- {
- MQTTAsync c = (MQTTAsync)context;
- MyLog(LOGA_DEBUG, "In onDisconnect failure callback %p", c);
- assert("Successful disconnect", 0, "disconnect failed", 0);
- test_finished = 1;
- }
- void test7_onDisconnect(void* context, MQTTAsync_successData* response)
- {
- MQTTAsync c = (MQTTAsync)context;
- MyLog(LOGA_DEBUG, "In onDisconnect callback %p", c);
- test_finished = 1;
- }
- void test7_onUnsubscribe(void* context, MQTTAsync_successData* response)
- {
- MQTTAsync c = (MQTTAsync)context;
- MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
- int rc;
- MyLog(LOGA_DEBUG, "In onUnsubscribe onSuccess callback %p", c);
- opts.onSuccess = test7_onDisconnect;
- opts.context = c;
- opts.timeout = 1000;
- rc = MQTTAsync_disconnect(c, &opts);
- assert("Disconnect successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- }
- int test7_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message)
- {
- MQTTAsync c = (MQTTAsync)context;
- static int message_count = 0;
- MyLog(LOGA_DEBUG, "Test7: received message id %d", message->msgid);
- test7_messageCount++;
- MQTTAsync_freeMessage(&message);
- MQTTAsync_free(topicName);
- return 1;
- }
- static int test7_subscribed = 0;
- void test7_onSubscribe(void* context, MQTTAsync_successData* response)
- {
- MQTTAsync c = (MQTTAsync)context;
- MyLog(LOGA_DEBUG, "In subscribe onSuccess callback %p granted qos %d", c, response->alt.qos);
- test7_subscribed = 1;
- }
- void test7_onConnect(void* context, MQTTAsync_successData* response)
- {
- MQTTAsync c = (MQTTAsync)context;
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- int rc;
- MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
- opts.onSuccess = test7_onSubscribe;
- opts.context = c;
- rc = MQTTAsync_subscribe(c, test7_topic, 2, &opts);
- assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- test_finished = 1;
- }
- void test7_onConnectOnly(void* context, MQTTAsync_successData* response)
- {
- MQTTAsync c = (MQTTAsync)context;
- MQTTAsync_disconnectOptions dopts = MQTTAsync_disconnectOptions_initializer;
- int rc;
- MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
- dopts.context = context;
- dopts.timeout = 1000;
- dopts.onSuccess = test7_onDisconnect;
- rc = MQTTAsync_disconnect(c, &dopts);
- assert("Good rc from disconnect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- test_finished = 1;
- }
- /*********************************************************************
- Test7: Pending tokens
- *********************************************************************/
- int test7(struct Options options)
- {
- int subsqos = 2;
- MQTTAsync c;
- MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
- MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
- int rc = 0;
- MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
- MQTTAsync_responseOptions ropts = MQTTAsync_responseOptions_initializer;
- MQTTAsync_disconnectOptions dopts = MQTTAsync_disconnectOptions_initializer;
- MQTTAsync_token* tokens = NULL;
- int msg_count = 6;
- MyLog(LOGA_INFO, "Starting test 7 - pending tokens");
- fprintf(xml, "<testcase classname=\"test4\" name=\"pending tokens\"");
- global_start_time = start_clock();
- test_finished = 0;
- rc = MQTTAsync_create(&c, options.connection, "async_test7",
- MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
- assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- if (rc != MQTTASYNC_SUCCESS)
- {
- MQTTAsync_destroy(&c);
- goto exit;
- }
- rc = MQTTAsync_setCallbacks(c, c, NULL, test7_messageArrived, NULL);
- assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- opts.keepAliveInterval = 20;
- opts.username = "testuser";
- opts.password = "testpassword";
- opts.MQTTVersion = options.MQTTVersion;
- opts.will = &wopts;
- opts.will->message = "will message";
- opts.will->qos = 1;
- opts.will->retained = 0;
- opts.will->topicName = "will topic";
- opts.will = NULL;
- opts.onFailure = NULL;
- opts.context = c;
- opts.cleansession = 1;
- opts.onSuccess = test7_onConnectOnly;
- MyLog(LOGA_DEBUG, "Connecting to clean up");
- rc = MQTTAsync_connect(c, &opts);
- rc = 0;
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- while (!test_finished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- test_finished = 0;
- MyLog(LOGA_DEBUG, "Connecting");
- opts.cleansession = 0;
- opts.onSuccess = test7_onConnect;
- rc = MQTTAsync_connect(c, &opts);
- rc = 0;
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- while (!test7_subscribed)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
- pubmsg.payloadlen = 11;
- pubmsg.qos = 2;
- pubmsg.retained = 0;
- rc = MQTTAsync_send(c, test_topic, pubmsg.payloadlen, pubmsg.payload, pubmsg.qos, pubmsg.retained, &ropts);
- MyLog(LOGA_DEBUG, "Token was %d", ropts.token);
- rc = MQTTAsync_isComplete(c, ropts.token);
- /*assert("0 rc from isComplete", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);*/
- rc = MQTTAsync_waitForCompletion(c, ropts.token, 5000L);
- assert("Good rc from waitForCompletion", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- rc = MQTTAsync_isComplete(c, ropts.token);
- assert("1 rc from isComplete", rc == 1, "rc was %d", rc);
- test7_messageCount = 0;
- int i = 0;
- pubmsg.qos = 2;
- for (i = 0; i < msg_count; ++i)
- {
- pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
- pubmsg.payloadlen = 11;
- //pubmsg.qos = (pubmsg.qos == 2) ? 1 : 2;
- pubmsg.retained = 0;
- rc = MQTTAsync_sendMessage(c, test_topic, &pubmsg, &ropts);
- }
- /* disconnect immediately without receiving the incoming messages */
- dopts.timeout = 0;
- dopts.onSuccess = test7_onDisconnect;
- dopts.context = c;
- MQTTAsync_disconnect(c, &dopts); /* now there should be "orphaned" publications */
- while (!test_finished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- test_finished = 0;
- rc = MQTTAsync_getPendingTokens(c, &tokens);
- assert("getPendingTokens rc == 0", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- assert("should get some tokens back", tokens != NULL, "tokens was %p", tokens);
- MQTTAsync_free(tokens);
- MQTTAsync_destroy(&c); /* force re-reading persistence on create */
- MQTTAsync_setTraceLevel(MQTTASYNC_TRACE_ERROR);
- rc = MQTTAsync_create(&c, options.connection, "async_test7", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
- assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- if (rc != MQTTASYNC_SUCCESS)
- {
- MQTTAsync_destroy(&c);
- goto exit;
- }
- rc = MQTTAsync_getPendingTokens(c, &tokens);
- assert("getPendingTokens rc == 0", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- assert("should get some tokens back", tokens != NULL, "tokens was %p", tokens);
- if (tokens)
- {
- int i = 0;
- while (tokens[i] != -1)
- MyLog(LOGA_DEBUG, "Delivery token %d", tokens[i++]);
- MQTTAsync_free(tokens);
- //The following assertion should work, does with RSMB, but not Mosquitto
- //assert1("no of tokens should be count", i == msg_count, "no of tokens %d count %d", i, msg_count);
- }
- rc = MQTTAsync_setCallbacks(c, c, NULL, test7_messageArrived, NULL);
- assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- MyLog(LOGA_DEBUG, "Reconnecting");
- opts.context = c;
- if (MQTTAsync_connect(c, &opts) != 0)
- {
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- goto exit;
- }
- #if defined(_WIN32)
- Sleep(5000);
- #else
- usleep(5000000L);
- #endif
- rc = MQTTAsync_getPendingTokens(c, &tokens);
- assert("getPendingTokens rc == 0", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- /* assert("should get no tokens back", tokens == NULL, "tokens was %p", tokens);
- assert1("no of messages should be count", test7_messageCount == msg_count, "no of tokens %d count %d",
- test7_messageCount, msg_count);
- assertions fail against Mosquitto - needs testing */
- dopts.onFailure = test7_onDisconnectFailure;
- dopts.onSuccess = test7_onDisconnect;
- dopts.timeout = 1000;
- MQTTAsync_disconnect(c, &dopts);
- while (!test_finished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- MQTTAsync_destroy(&c);
- exit:
- MyLog(LOGA_INFO, "TEST7: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", tests, failures);
- write_test_result();
- return failures;
- }
- /*********************************************************************
- Test8: Incomplete commands and requests
- *********************************************************************/
- char* test8_topic = "C client test8";
- int test8_messageCount = 0;
- int test8_subscribed = 0;
- int test8_publishFailures = 0;
- void test8_onPublish(void* context, MQTTAsync_successData* response)
- {
- MQTTAsync c = (MQTTAsync)context;
- MyLog(LOGA_DEBUG, "In publish onSuccess callback %p token %d", c, response->token);
- }
- void test8_onPublishFailure(void* context, MQTTAsync_failureData* response)
- {
- MQTTAsync c = (MQTTAsync)context;
- MyLog(LOGA_DEBUG, "In onPublish failure callback %p", c);
- assert("Response code should be interrupted", response->code == MQTTASYNC_OPERATION_INCOMPLETE,
- "rc was %d", response->code);
- test8_publishFailures++;
- }
- void test8_onDisconnectFailure(void* context, MQTTAsync_failureData* response)
- {
- MQTTAsync c = (MQTTAsync)context;
- MyLog(LOGA_DEBUG, "In onDisconnect failure callback %p", c);
- assert("Successful disconnect", 0, "disconnect failed", 0);
- test_finished = 1;
- }
- void test8_onDisconnect(void* context, MQTTAsync_successData* response)
- {
- MQTTAsync c = (MQTTAsync)context;
- MyLog(LOGA_DEBUG, "In onDisconnect callback %p", c);
- test_finished = 1;
- }
- void test8_onSubscribe(void* context, MQTTAsync_successData* response)
- {
- MQTTAsync c = (MQTTAsync)context;
- MyLog(LOGA_DEBUG, "In subscribe onSuccess callback %p granted qos %d", c, response->alt.qos);
- test8_subscribed = 1;
- }
- void test8_onConnect(void* context, MQTTAsync_successData* response)
- {
- MQTTAsync c = (MQTTAsync)context;
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- int rc;
- MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
- opts.onSuccess = test8_onSubscribe;
- opts.context = c;
- rc = MQTTAsync_subscribe(c, test8_topic, 2, &opts);
- assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- if (rc != MQTTASYNC_SUCCESS)
- test_finished = 1;
- }
- int test8_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message)
- {
- MQTTAsync c = (MQTTAsync)context;
- static int message_count = 0;
- MyLog(LOGA_DEBUG, "Test8: received message id %d", message->msgid);
- test8_messageCount++;
- MQTTAsync_freeMessage(&message);
- MQTTAsync_free(topicName);
- return 1;
- }
- int test8(struct Options options)
- {
- int subsqos = 2;
- MQTTAsync c;
- MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
- int rc = 0;
- MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
- MQTTAsync_responseOptions ropts = MQTTAsync_responseOptions_initializer;
- MQTTAsync_disconnectOptions dopts = MQTTAsync_disconnectOptions_initializer;
- MQTTAsync_token* tokens = NULL;
- int msg_count = 6;
- MyLog(LOGA_INFO, "Starting test 8 - incomplete commands");
- fprintf(xml, "<testcase classname=\"test4\" name=\"incomplete commands\"");
- global_start_time = start_clock();
- test_finished = 0;
- rc = MQTTAsync_create(&c, options.connection, "async_test8",
- MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
- assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- if (rc != MQTTASYNC_SUCCESS)
- {
- MQTTAsync_destroy(&c);
- goto exit;
- }
- rc = MQTTAsync_setCallbacks(c, c, NULL, test8_messageArrived, NULL);
- assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- opts.keepAliveInterval = 20;
- opts.username = "testuser";
- opts.password = "testpassword";
- opts.MQTTVersion = options.MQTTVersion;
- opts.onFailure = NULL;
- opts.context = c;
- MyLog(LOGA_DEBUG, "Connecting");
- opts.cleansession = 1;
- opts.onSuccess = test8_onConnect;
- rc = MQTTAsync_connect(c, &opts);
- rc = 0;
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- while (!test8_subscribed)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- int i = 0;
- pubmsg.qos = 2;
- ropts.onSuccess = test8_onPublish;
- ropts.onFailure = test8_onPublishFailure;
- ropts.context = c;
- for (i = 0; i < msg_count; ++i)
- {
- pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
- pubmsg.payloadlen = 11;
- pubmsg.qos = (pubmsg.qos == 2) ? 1 : 2; /* alternate */
- pubmsg.retained = 0;
- rc = MQTTAsync_sendMessage(c, test8_topic, &pubmsg, &ropts);
- assert("Good rc from sendMessage", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- }
- /* disconnect immediately without completing the commands */
- dopts.timeout = 0;
- dopts.onSuccess = test8_onDisconnect;
- dopts.context = c;
- rc = MQTTAsync_disconnect(c, &dopts); /* now there should be incomplete commands */
- assert("Good rc from disconnect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- while (!test_finished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- test_finished = 0;
- rc = MQTTAsync_getPendingTokens(c, &tokens);
- assert("getPendingTokens rc == 0", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- assert("should get no tokens back", tokens == NULL, "tokens was %p", tokens);
- assert("test8_publishFailures > 0", test8_publishFailures > 0,
- "test8_publishFailures = %d", test8_publishFailures);
- /* Now elicit failure callbacks on destroy */
- test8_subscribed = test8_publishFailures = 0;
- MyLog(LOGA_DEBUG, "Connecting");
- opts.cleansession = 0;
- opts.onSuccess = test8_onConnect;
- rc = MQTTAsync_connect(c, &opts);
- rc = 0;
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- while (!test8_subscribed)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- i = 0;
- pubmsg.qos = 2;
- ropts.onSuccess = test8_onPublish;
- ropts.onFailure = test8_onPublishFailure;
- ropts.context = c;
- for (i = 0; i < msg_count; ++i)
- {
- pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
- pubmsg.payloadlen = 11;
- pubmsg.qos = (pubmsg.qos == 2) ? 1 : 2; /* alternate */
- pubmsg.retained = 0;
- rc = MQTTAsync_sendMessage(c, test8_topic, &pubmsg, &ropts);
- assert("Good rc from sendMessage", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- }
- /* disconnect immediately without completing the commands */
- dopts.timeout = 0;
- dopts.onSuccess = test8_onDisconnect;
- dopts.context = c;
- rc = MQTTAsync_disconnect(c, &dopts); /* now there should be incomplete commands */
- assert("Good rc from disconnect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- while (!test_finished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- test_finished = 0;
- rc = MQTTAsync_getPendingTokens(c, &tokens);
- assert("getPendingTokens rc == 0", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- assert("should get some tokens back", tokens != NULL, "tokens was %p", tokens);
- MQTTAsync_free(tokens);
- assert("test8_publishFailures == 0", test8_publishFailures == 0,
- "test8_publishFailures = %d", test8_publishFailures);
- MQTTAsync_destroy(&c);
- assert("test8_publishFailures > 0", test8_publishFailures > 0,
- "test8_publishFailures = %d", test8_publishFailures);
- /* cleanup persistence of any left over message data*/
- MQTTAsync_setTraceLevel(MQTTASYNC_TRACE_ERROR);
- rc = MQTTAsync_create(&c, options.connection, "async_test8",
- MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
- assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- if (rc != MQTTASYNC_SUCCESS)
- {
- MQTTAsync_destroy(&c);
- goto exit;
- }
- test8_subscribed = 0;
- opts.cleansession = 1;
- opts.context = c;
- rc = MQTTAsync_connect(c, &opts);
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- while (!test8_subscribed)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- test_finished = 0;
- dopts.context = c;
- rc = MQTTAsync_disconnect(c, &dopts);
- assert("Good rc from disconnect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- while (!test_finished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- MQTTAsync_destroy(&c);
- exit:
- MyLog(LOGA_INFO, "TEST8: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", tests, failures);
- write_test_result();
- return failures;
- }
- void trace_callback(enum MQTTASYNC_TRACE_LEVELS level, char* message)
- {
- printf("Trace : %d, %s\n", level, message);
- }
- int main(int argc, char** argv)
- {
- int rc = 0;
- int (*tests[])() = {NULL, test1, test2, test3, test4, test5, test6, test7, test8}; /* indexed starting from 1 */
- MQTTAsync_nameValue* info;
- int i;
- xml = fopen("TEST-test4.xml", "w");
- fprintf(xml, "<testsuite name=\"test4\" tests=\"%d\">\n", (int)(ARRAY_SIZE(tests)) - 1);
- getopts(argc, argv);
- MQTTAsync_setTraceCallback(trace_callback);
- info = MQTTAsync_getVersionInfo();
- while (info->name)
- {
- MyLog(LOGA_INFO, "%s: %s", info->name, info->value);
- info++;
- }
- for (i = 0; i < options.iterations; ++i)
- {
- if (options.test_no == -1)
- { /* run all the tests */
- for (options.test_no = 1; options.test_no < ARRAY_SIZE(tests); ++options.test_no)
- {
- failures = 0;
- MQTTAsync_setTraceLevel(MQTTASYNC_TRACE_ERROR);
- rc += tests[options.test_no](options); /* return number of failures. 0 = test succeeded */
- }
- }
- else
- {
- MQTTAsync_setTraceLevel(MQTTASYNC_TRACE_ERROR);
- 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, "</testsuite>\n");
- fclose(xml);
- return rc;
- }
|