test11.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2018 IBM Corp.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v2.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * https://www.eclipse.org/legal/epl-2.0/
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial API and implementation and/or initial documentation
  15. * Ian Craggs - MQTT 3.1.1 support
  16. * Ian Craggs - test8 - failure callbacks
  17. * Ian Craggs - MQTT V5
  18. *******************************************************************************/
  19. /**
  20. * @file
  21. * Tests for the Paho Asynchronous MQTT C client
  22. */
  23. #include "MQTTAsync.h"
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #if !defined(_WINDOWS)
  27. #include <sys/time.h>
  28. #include <sys/socket.h>
  29. #include <unistd.h>
  30. #include <errno.h>
  31. #else
  32. #include <windows.h>
  33. #endif
  34. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  35. void usage(void)
  36. {
  37. printf("help!!\n");
  38. exit(EXIT_FAILURE);
  39. }
  40. struct Options
  41. {
  42. char* connection; /**< connection to system under test. */
  43. int verbose;
  44. int test_no;
  45. int size; /**< size of big message */
  46. int MQTTVersion;
  47. int iterations;
  48. } options =
  49. {
  50. "localhost:1883",
  51. 0,
  52. -1,
  53. 10000,
  54. MQTTVERSION_5,
  55. 1,
  56. };
  57. void getopts(int argc, char** argv)
  58. {
  59. int count = 1;
  60. while (count < argc)
  61. {
  62. if (strcmp(argv[count], "--test_no") == 0)
  63. {
  64. if (++count < argc)
  65. options.test_no = atoi(argv[count]);
  66. else
  67. usage();
  68. }
  69. else if (strcmp(argv[count], "--size") == 0)
  70. {
  71. if (++count < argc)
  72. options.size = atoi(argv[count]);
  73. else
  74. usage();
  75. }
  76. else if (strcmp(argv[count], "--connection") == 0)
  77. {
  78. if (++count < argc)
  79. options.connection = argv[count];
  80. else
  81. usage();
  82. }
  83. else if (strcmp(argv[count], "--MQTTversion") == 0)
  84. {
  85. if (++count < argc)
  86. {
  87. options.MQTTVersion = atoi(argv[count]);
  88. printf("setting MQTT version to %d\n", options.MQTTVersion);
  89. }
  90. else
  91. usage();
  92. }
  93. else if (strcmp(argv[count], "--iterations") == 0)
  94. {
  95. if (++count < argc)
  96. options.iterations = atoi(argv[count]);
  97. else
  98. usage();
  99. }
  100. else if (strcmp(argv[count], "--verbose") == 0)
  101. options.verbose = 1;
  102. count++;
  103. }
  104. }
  105. #if 0
  106. #include <logaX.h> /* For general log messages */
  107. #define MyLog logaLine
  108. #else
  109. #define LOGA_DEBUG 0
  110. #define LOGA_INFO 1
  111. #include <stdarg.h>
  112. #include <time.h>
  113. #include <sys/timeb.h>
  114. void MyLog(int LOGA_level, char* format, ...)
  115. {
  116. static char msg_buf[256];
  117. va_list args;
  118. #if defined(_WIN32) || defined(_WINDOWS)
  119. struct timeb ts;
  120. #else
  121. struct timeval ts;
  122. #endif
  123. struct tm *timeinfo;
  124. if (LOGA_level == LOGA_DEBUG && options.verbose == 0)
  125. return;
  126. #if defined(_WIN32) || defined(_WINDOWS)
  127. ftime(&ts);
  128. timeinfo = localtime(&ts.time);
  129. #else
  130. gettimeofday(&ts, NULL);
  131. timeinfo = localtime(&ts.tv_sec);
  132. #endif
  133. strftime(msg_buf, 80, "%Y%m%d %H%M%S", timeinfo);
  134. #if defined(_WIN32) || defined(_WINDOWS)
  135. sprintf(&msg_buf[strlen(msg_buf)], ".%.3hu ", ts.millitm);
  136. #else
  137. sprintf(&msg_buf[strlen(msg_buf)], ".%.3lu ", ts.tv_usec / 1000L);
  138. #endif
  139. va_start(args, format);
  140. vsnprintf(&msg_buf[strlen(msg_buf)], sizeof(msg_buf) - strlen(msg_buf), format, args);
  141. va_end(args);
  142. printf("%s\n", msg_buf);
  143. fflush(stdout);
  144. }
  145. #endif
  146. #if defined(_WIN32) || defined(_WINDOWS)
  147. #define mqsleep(A) Sleep(1000*A)
  148. #define START_TIME_TYPE DWORD
  149. static DWORD start_time = 0;
  150. START_TIME_TYPE start_clock(void)
  151. {
  152. return GetTickCount();
  153. }
  154. #elif defined(AIX)
  155. #define mqsleep sleep
  156. #define START_TIME_TYPE struct timespec
  157. START_TIME_TYPE start_clock(void)
  158. {
  159. static struct timespec start;
  160. clock_gettime(CLOCK_REALTIME, &start);
  161. return start;
  162. }
  163. #else
  164. #define mqsleep sleep
  165. #define START_TIME_TYPE struct timeval
  166. /* TODO - unused - remove? static struct timeval start_time; */
  167. START_TIME_TYPE start_clock(void)
  168. {
  169. struct timeval start_time;
  170. gettimeofday(&start_time, NULL);
  171. return start_time;
  172. }
  173. #endif
  174. #if defined(_WIN32)
  175. long elapsed(START_TIME_TYPE start_time)
  176. {
  177. return GetTickCount() - start_time;
  178. }
  179. #elif defined(AIX)
  180. #define assert(a)
  181. long elapsed(struct timespec start)
  182. {
  183. struct timespec now, res;
  184. clock_gettime(CLOCK_REALTIME, &now);
  185. ntimersub(now, start, res);
  186. return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L;
  187. }
  188. #else
  189. long elapsed(START_TIME_TYPE start_time)
  190. {
  191. struct timeval now, res;
  192. gettimeofday(&now, NULL);
  193. timersub(&now, &start_time, &res);
  194. return (res.tv_sec)*1000 + (res.tv_usec)/1000;
  195. }
  196. #endif
  197. #define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d)
  198. #define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e)
  199. int tests = 0;
  200. int failures = 0;
  201. FILE* xml;
  202. START_TIME_TYPE global_start_time;
  203. char output[3000];
  204. char* cur_output = output;
  205. void write_test_result(void)
  206. {
  207. long duration = elapsed(global_start_time);
  208. fprintf(xml, " time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000);
  209. if (cur_output != output)
  210. {
  211. fprintf(xml, "%s", output);
  212. cur_output = output;
  213. }
  214. fprintf(xml, "</testcase>\n");
  215. }
  216. void myassert(char* filename, int lineno, char* description, int value, char* format, ...)
  217. {
  218. ++tests;
  219. if (!value)
  220. {
  221. va_list args;
  222. ++failures;
  223. printf("Assertion failed, file %s, line %d, description: %s\n", filename, lineno, description);
  224. va_start(args, format);
  225. vprintf(format, args);
  226. va_end(args);
  227. cur_output += sprintf(cur_output, "<failure type=\"%s\">file %s, line %d </failure>\n",
  228. description, filename, lineno);
  229. }
  230. else
  231. MyLog(LOGA_DEBUG, "Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description);
  232. }
  233. void logProperties(MQTTProperties *props)
  234. {
  235. int i = 0;
  236. for (i = 0; i < props->count; ++i)
  237. {
  238. int id = props->array[i].identifier;
  239. const char* name = MQTTPropertyName(id);
  240. char* intformat = "Property name %s value %d";
  241. switch (MQTTProperty_getType(id))
  242. {
  243. case MQTTPROPERTY_TYPE_BYTE:
  244. MyLog(LOGA_DEBUG, intformat, name, props->array[i].value.byte);
  245. break;
  246. case MQTTPROPERTY_TYPE_TWO_BYTE_INTEGER:
  247. MyLog(LOGA_DEBUG, intformat, name, props->array[i].value.integer2);
  248. break;
  249. case MQTTPROPERTY_TYPE_FOUR_BYTE_INTEGER:
  250. MyLog(LOGA_DEBUG, intformat, name, props->array[i].value.integer4);
  251. break;
  252. case MQTTPROPERTY_TYPE_VARIABLE_BYTE_INTEGER:
  253. MyLog(LOGA_DEBUG, intformat, name, props->array[i].value.integer4);
  254. break;
  255. case MQTTPROPERTY_TYPE_BINARY_DATA:
  256. case MQTTPROPERTY_TYPE_UTF_8_ENCODED_STRING:
  257. MyLog(LOGA_DEBUG, "Property name %s value len %.*s", name,
  258. props->array[i].value.data.len, props->array[i].value.data.data);
  259. break;
  260. case MQTTPROPERTY_TYPE_UTF_8_STRING_PAIR:
  261. MyLog(LOGA_DEBUG, "Property name %s key %.*s value %.*s", name,
  262. props->array[i].value.data.len, props->array[i].value.data.data,
  263. props->array[i].value.value.len, props->array[i].value.value.data);
  264. break;
  265. }
  266. }
  267. }
  268. struct
  269. {
  270. int disconnected;
  271. char* test_topic;
  272. int test_finished;
  273. } test_client_topic_aliases_globals =
  274. {
  275. 0, "client topic aliases topic", 0
  276. };
  277. void test1_onDisconnect(void* context, MQTTAsync_successData* response)
  278. {
  279. MQTTAsync c = (MQTTAsync)context;
  280. MyLog(LOGA_DEBUG, "In onDisconnect callback %p", c);
  281. test_client_topic_aliases_globals.test_finished = 1;
  282. }
  283. int test_client_topic_aliases_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message)
  284. {
  285. MQTTAsync c = (MQTTAsync)context;
  286. static int received = 0;
  287. received++;
  288. assert("Message structure version should be 1", message->struct_version == 1,
  289. "message->struct_version was %d", message->struct_version);
  290. if (message->struct_version == 1)
  291. {
  292. const int props_count = 0;
  293. assert("No topic alias", MQTTProperties_hasProperty(&message->properties, MQTTPROPERTY_CODE_TOPIC_ALIAS) == 0,
  294. "topic alias is %d\n", MQTTProperties_hasProperty(&message->properties, MQTTPROPERTY_CODE_TOPIC_ALIAS));
  295. assert("Topic should be name", strcmp(topicName, test_client_topic_aliases_globals.test_topic) == 0,
  296. "Topic name was %s\n", topicName);
  297. logProperties(&message->properties);
  298. }
  299. if (received == 2)
  300. test_client_topic_aliases_globals.test_finished = 1;
  301. MQTTAsync_freeMessage(&message);
  302. MQTTAsync_free(topicName);
  303. return 1;
  304. }
  305. void test_client_topic_aliases_onSubscribe(void* context, MQTTAsync_successData5* response)
  306. {
  307. MQTTAsync c = (MQTTAsync)context;
  308. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  309. MQTTProperty property;
  310. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  311. int rc;
  312. MyLog(LOGA_DEBUG, "Suback properties:");
  313. logProperties(&response->properties);
  314. pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  315. pubmsg.payloadlen = 11;
  316. pubmsg.retained = 0;
  317. pubmsg.qos = 1;
  318. /* first set the topic alias */
  319. property.identifier = MQTTPROPERTY_CODE_TOPIC_ALIAS;
  320. property.value.integer2 = 1;
  321. MQTTProperties_add(&pubmsg.properties, &property);
  322. rc = MQTTAsync_sendMessage(c, test_client_topic_aliases_globals.test_topic, &pubmsg, &opts);
  323. assert("Good rc from send", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  324. if (rc != MQTTASYNC_SUCCESS)
  325. test_client_topic_aliases_globals.test_finished = 1;
  326. else
  327. {
  328. /* now send the publish with topic alias only */
  329. rc = MQTTAsync_sendMessage(c, "", &pubmsg, &opts);
  330. assert("Good rc from send", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  331. if (rc != MQTTASYNC_SUCCESS)
  332. test_client_topic_aliases_globals.test_finished = 1;
  333. }
  334. MQTTProperties_free(&pubmsg.properties);
  335. }
  336. void test_client_topic_aliases_disconnected(void* context, MQTTProperties* props, enum MQTTReasonCodes rc)
  337. {
  338. MQTTAsync c = (MQTTAsync)context;
  339. MyLog(LOGA_DEBUG, "Callback: disconnected, reason code \"%s\"", MQTTReasonCode_toString(rc));
  340. logProperties(props);
  341. test_client_topic_aliases_globals.disconnected = 1;
  342. }
  343. void test_client_topic_aliases_onConnect(void* context, MQTTAsync_successData5* response)
  344. {
  345. MQTTAsync c = (MQTTAsync)context;
  346. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  347. MQTTProperty property;
  348. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  349. int rc;
  350. static int first = 1;
  351. MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
  352. assert("Reason code should be 0", response->reasonCode == MQTTREASONCODE_SUCCESS,
  353. "Reason code was %d\n", response->reasonCode);
  354. MyLog(LOGA_DEBUG, "Connack properties:");
  355. logProperties(&response->properties);
  356. if (first)
  357. {
  358. first = 0;
  359. pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  360. pubmsg.payloadlen = 11;
  361. pubmsg.qos = 1;
  362. pubmsg.retained = 0;
  363. /* a Topic Alias of 0 is not allowed, so we should be disconnected */
  364. property.identifier = MQTTPROPERTY_CODE_TOPIC_ALIAS;
  365. property.value.integer2 = 0;
  366. MQTTProperties_add(&pubmsg.properties, &property);
  367. rc = MQTTAsync_sendMessage(c, test_client_topic_aliases_globals.test_topic, &pubmsg, &opts);
  368. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  369. if (rc != MQTTASYNC_SUCCESS)
  370. test_client_topic_aliases_globals.test_finished = 1;
  371. MQTTProperties_free(&pubmsg.properties);
  372. }
  373. else
  374. {
  375. opts.onSuccess5 = test_client_topic_aliases_onSubscribe;
  376. opts.context = c;
  377. rc = MQTTAsync_subscribe(c, test_client_topic_aliases_globals.test_topic, 2, &opts);
  378. assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  379. if (rc != MQTTASYNC_SUCCESS)
  380. test_client_topic_aliases_globals.test_finished = 1;
  381. }
  382. }
  383. /*********************************************************************
  384. Test1: client topic aliases
  385. *********************************************************************/
  386. int test_client_topic_aliases(struct Options options)
  387. {
  388. int subsqos = 2;
  389. MQTTAsync c;
  390. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer5;
  391. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  392. MQTTProperties props = MQTTProperties_initializer;
  393. MQTTProperties willProps = MQTTProperties_initializer;
  394. MQTTProperty property;
  395. int rc = 0;
  396. char* test_topic = "V5 C client test client topic aliases";
  397. MQTTAsync_createOptions createOpts = MQTTAsync_createOptions_initializer;
  398. MyLog(LOGA_INFO, "Starting V5 test 1 - client topic aliases");
  399. fprintf(xml, "<testcase classname=\"test11\" name=\"client topic aliases\"");
  400. global_start_time = start_clock();
  401. createOpts.MQTTVersion = MQTTVERSION_5;
  402. rc = MQTTAsync_createWithOptions(&c, options.connection, "async_test_aliases",
  403. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL, &createOpts);
  404. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  405. if (rc != MQTTASYNC_SUCCESS)
  406. {
  407. MQTTAsync_destroy(&c);
  408. goto exit;
  409. }
  410. rc = MQTTAsync_setCallbacks(c, c, NULL, test_client_topic_aliases_messageArrived, NULL);
  411. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  412. rc = MQTTAsync_setDisconnected(c, c, test_client_topic_aliases_disconnected);
  413. opts.keepAliveInterval = 20;
  414. opts.username = "testuser";
  415. opts.password = "testpassword";
  416. opts.MQTTVersion = options.MQTTVersion;
  417. opts.cleanstart = 1;
  418. opts.will = &wopts;
  419. opts.will->message = "will message";
  420. opts.will->qos = 1;
  421. opts.will->retained = 0;
  422. opts.will->topicName = "will topic";
  423. opts.will = NULL;
  424. opts.onSuccess5 = test_client_topic_aliases_onConnect;
  425. opts.onFailure5 = NULL;
  426. opts.context = c;
  427. property.identifier = MQTTPROPERTY_CODE_SESSION_EXPIRY_INTERVAL;
  428. property.value.integer4 = 30;
  429. MQTTProperties_add(&props, &property);
  430. property.identifier = MQTTPROPERTY_CODE_USER_PROPERTY;
  431. property.value.data.data = "test user property";
  432. property.value.data.len = (int)strlen(property.value.data.data);
  433. property.value.value.data = "test user property value";
  434. property.value.value.len = (int)strlen(property.value.value.data);
  435. MQTTProperties_add(&props, &property);
  436. opts.connectProperties = &props;
  437. opts.willProperties = &willProps;
  438. MyLog(LOGA_DEBUG, "Connecting");
  439. rc = MQTTAsync_connect(c, &opts);
  440. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  441. if (rc != MQTTASYNC_SUCCESS)
  442. goto exit;
  443. while (test_client_topic_aliases_globals.disconnected == 0)
  444. #if defined(_WIN32)
  445. Sleep(100);
  446. #else
  447. usleep(10000L);
  448. #endif
  449. /* Now try a valid topic alias */
  450. rc = MQTTAsync_connect(c, &opts);
  451. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  452. if (rc != MQTTASYNC_SUCCESS)
  453. goto exit;
  454. while (test_client_topic_aliases_globals.test_finished == 0)
  455. #if defined(_WIN32)
  456. Sleep(100);
  457. #else
  458. usleep(10000L);
  459. #endif
  460. MQTTProperties_free(&props);
  461. MQTTProperties_free(&willProps);
  462. MQTTAsync_destroy(&c);
  463. exit:
  464. MyLog(LOGA_INFO, "TEST1: test %s. %d tests run, %d failures.",
  465. (failures == 0) ? "passed" : "failed", tests, failures);
  466. write_test_result();
  467. return failures;
  468. }
  469. struct
  470. {
  471. char* test_topic;
  472. int test_finished;
  473. int messages_arrived;
  474. int msg_count;
  475. } test_server_topic_aliases_globals =
  476. {
  477. "server topic aliases topic", 0, 0, 3
  478. };
  479. int test_server_topic_aliases_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message)
  480. {
  481. MQTTAsync c = (MQTTAsync)context;
  482. static int received = 0;
  483. static int first_topic_alias = 0;
  484. int topicAlias = 0;
  485. received++;
  486. assert("Message structure version should be 1", message->struct_version == 1,
  487. "message->struct_version was %d", message->struct_version);
  488. if (message->struct_version == 1)
  489. {
  490. const int props_count = 0;
  491. if (MQTTProperties_hasProperty(&message->properties, MQTTPROPERTY_CODE_TOPIC_ALIAS))
  492. topicAlias = MQTTProperties_getNumericValue(&message->properties, MQTTPROPERTY_CODE_TOPIC_ALIAS);
  493. if (received == 1)
  494. {
  495. first_topic_alias = topicAlias;
  496. assert("Topic should be name", strcmp(topicName, test_server_topic_aliases_globals.test_topic) == 0,
  497. "Topic name was %s\n", topicName);
  498. }
  499. else
  500. {
  501. assert("All topic aliases should be the same", topicAlias == first_topic_alias,
  502. "Topic alias was %d\n", topicAlias);
  503. assert("Topic should be 0 length", strcmp(topicName, "") == 0,
  504. "Topic name was %s\n", topicName);
  505. }
  506. assert("topicAlias should not be 0", topicAlias > 0, "Topic alias was %d\n", topicAlias);
  507. logProperties(&message->properties);
  508. }
  509. test_server_topic_aliases_globals.messages_arrived++;
  510. if (test_server_topic_aliases_globals.messages_arrived == test_server_topic_aliases_globals.msg_count)
  511. test_server_topic_aliases_globals.test_finished = 1;
  512. MQTTAsync_freeMessage(&message);
  513. MQTTAsync_free(topicName);
  514. return 1;
  515. }
  516. void test_server_topic_aliases_onSubscribe(void* context, MQTTAsync_successData5* response)
  517. {
  518. MQTTAsync c = (MQTTAsync)context;
  519. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  520. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  521. int qos = 0, rc;
  522. MyLog(LOGA_DEBUG, "Suback properties:");
  523. logProperties(&response->properties);
  524. test_server_topic_aliases_globals.messages_arrived = 0;
  525. pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  526. pubmsg.payloadlen = 11;
  527. pubmsg.retained = 0;
  528. for (qos = 0; qos < test_server_topic_aliases_globals.msg_count; ++qos)
  529. {
  530. pubmsg.qos = qos;
  531. rc = MQTTAsync_sendMessage(c, test_server_topic_aliases_globals.test_topic, &pubmsg, &opts);
  532. assert("Good rc from send", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  533. if (rc != MQTTASYNC_SUCCESS)
  534. {
  535. test_server_topic_aliases_globals.test_finished = 1;
  536. break;
  537. }
  538. }
  539. }
  540. void test_server_topic_aliases_onConnect(void* context, MQTTAsync_successData5* response)
  541. {
  542. MQTTAsync c = (MQTTAsync)context;
  543. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  544. int rc;
  545. MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
  546. assert("Reason code should be 0", response->reasonCode == MQTTREASONCODE_SUCCESS,
  547. "Reason code was %d\n", response->reasonCode);
  548. MyLog(LOGA_DEBUG, "Connack properties:");
  549. logProperties(&response->properties);
  550. opts.onSuccess5 = test_server_topic_aliases_onSubscribe;
  551. opts.context = c;
  552. rc = MQTTAsync_subscribe(c, test_server_topic_aliases_globals.test_topic, 2, &opts);
  553. assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  554. if (rc != MQTTASYNC_SUCCESS)
  555. test_server_topic_aliases_globals.test_finished = 1;
  556. }
  557. /*********************************************************************
  558. Test2: server topic aliases
  559. *********************************************************************/
  560. int test_server_topic_aliases(struct Options options)
  561. {
  562. int subsqos = 2;
  563. MQTTAsync c;
  564. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer5;
  565. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  566. MQTTProperties connect_props = MQTTProperties_initializer;
  567. MQTTProperty property;
  568. int rc = 0;
  569. char* test_topic = "V5 C client test server topic aliases";
  570. MQTTAsync_createOptions createOpts = MQTTAsync_createOptions_initializer;
  571. MyLog(LOGA_INFO, "Starting V5 test 2 - server topic aliases");
  572. fprintf(xml, "<testcase classname=\"test11\" name=\"server topic aliases\"");
  573. global_start_time = start_clock();
  574. createOpts.MQTTVersion = MQTTVERSION_5;
  575. rc = MQTTAsync_createWithOptions(&c, options.connection, "server_topic_aliases",
  576. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL, &createOpts);
  577. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  578. if (rc != MQTTASYNC_SUCCESS)
  579. {
  580. MQTTAsync_destroy(&c);
  581. goto exit;
  582. }
  583. rc = MQTTAsync_setCallbacks(c, c, NULL, test_server_topic_aliases_messageArrived, NULL);
  584. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  585. /* Allow at least one server topic alias */
  586. property.identifier = MQTTPROPERTY_CODE_TOPIC_ALIAS_MAXIMUM;
  587. property.value.integer2 = 1;
  588. MQTTProperties_add(&connect_props, &property);
  589. opts.MQTTVersion = options.MQTTVersion;
  590. opts.onSuccess5 = test_server_topic_aliases_onConnect;
  591. opts.context = c;
  592. opts.connectProperties = &connect_props;
  593. MyLog(LOGA_DEBUG, "Connecting");
  594. rc = MQTTAsync_connect(c, &opts);
  595. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  596. if (rc != MQTTASYNC_SUCCESS)
  597. goto exit;
  598. while (test_server_topic_aliases_globals.test_finished == 0)
  599. #if defined(_WIN32)
  600. Sleep(100);
  601. #else
  602. usleep(10000L);
  603. #endif
  604. MQTTProperties_free(&connect_props);
  605. MQTTAsync_destroy(&c);
  606. exit:
  607. MyLog(LOGA_INFO, "TEST2: test %s. %d tests run, %d failures.",
  608. (failures == 0) ? "passed" : "failed", tests, failures);
  609. write_test_result();
  610. return failures;
  611. }
  612. /*********************************************************************
  613. Test3: subscription ids
  614. *********************************************************************/
  615. struct
  616. {
  617. char* test_topic;
  618. int test_finished;
  619. int messages_arrived;
  620. } test_subscription_ids_globals =
  621. {
  622. "server subscription ids topic", 0, 0
  623. };
  624. int test_subscription_ids_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message)
  625. {
  626. MQTTAsync c = (MQTTAsync)context;
  627. test_subscription_ids_globals.messages_arrived++;
  628. assert("Message structure version should be 1", message->struct_version == 1,
  629. "message->struct_version was %d", message->struct_version);
  630. MyLog(LOGA_DEBUG, "Callback: message received on topic %s is %.*s.",
  631. topicName, message->payloadlen, (char*)(message->payload));
  632. if (message->struct_version == 1)
  633. {
  634. int subsidcount = 0, i = 0;
  635. subsidcount = MQTTProperties_propertyCount(&message->properties, MQTTPROPERTY_CODE_SUBSCRIPTION_IDENTIFIER);
  636. for (i = 0; i < subsidcount; ++i)
  637. {
  638. int subsid = MQTTProperties_getNumericValueAt(&message->properties, MQTTPROPERTY_CODE_SUBSCRIPTION_IDENTIFIER, i);
  639. assert("Subsid is i+1", subsid == i+1, "subsid is not correct %d\n", subsid);
  640. }
  641. logProperties(&message->properties);
  642. }
  643. test_subscription_ids_globals.test_finished = 1;
  644. MQTTAsync_freeMessage(&message);
  645. MQTTAsync_free(topicName);
  646. return 1;
  647. }
  648. void test_subscription_ids_onSubscribe(void* context, MQTTAsync_successData5* response)
  649. {
  650. MQTTAsync c = (MQTTAsync)context;
  651. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  652. MQTTProperty property;
  653. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  654. int rc;
  655. static int subs_count = 0;
  656. MyLog(LOGA_DEBUG, "Suback properties:");
  657. logProperties(&response->properties);
  658. if (++subs_count == 1)
  659. {
  660. /* subscribe to a wildcard */
  661. property.identifier = MQTTPROPERTY_CODE_SUBSCRIPTION_IDENTIFIER;
  662. property.value.integer4 = 2;
  663. MQTTProperties_add(&opts.properties, &property);
  664. opts.onSuccess5 = test_subscription_ids_onSubscribe;
  665. opts.context = c;
  666. rc = MQTTAsync_subscribe(c, "+", 2, &opts);
  667. assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  668. if (rc != MQTTASYNC_SUCCESS)
  669. test_subscription_ids_globals.test_finished = 1;
  670. MQTTProperties_free(&opts.properties);
  671. }
  672. else
  673. {
  674. test_subscription_ids_globals.messages_arrived = 0;
  675. pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  676. pubmsg.payloadlen = 11;
  677. pubmsg.retained = 0;
  678. pubmsg.qos = 1;
  679. rc = MQTTAsync_sendMessage(c, test_subscription_ids_globals.test_topic, &pubmsg, &opts);
  680. assert("Good rc from send", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  681. if (rc != MQTTASYNC_SUCCESS)
  682. test_subscription_ids_globals.test_finished = 1;
  683. }
  684. }
  685. void test_subscription_ids_onConnect(void* context, MQTTAsync_successData5* response)
  686. {
  687. MQTTAsync c = (MQTTAsync)context;
  688. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  689. MQTTProperty property;
  690. int rc;
  691. MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
  692. assert("Reason code should be 0", response->reasonCode == MQTTREASONCODE_SUCCESS,
  693. "Reason code was %d\n", response->reasonCode);
  694. MyLog(LOGA_DEBUG, "Connack properties:");
  695. logProperties(&response->properties);
  696. opts.onSuccess5 = test_subscription_ids_onSubscribe;
  697. opts.context = c;
  698. /* subscribe to the test topic */
  699. property.identifier = MQTTPROPERTY_CODE_SUBSCRIPTION_IDENTIFIER;
  700. property.value.integer4 = 1;
  701. MQTTProperties_add(&opts.properties, &property);
  702. rc = MQTTAsync_subscribe(c, test_subscription_ids_globals.test_topic, 2, &opts);
  703. assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  704. if (rc != MQTTASYNC_SUCCESS)
  705. test_subscription_ids_globals.test_finished = 1;
  706. MQTTProperties_free(&opts.properties);
  707. }
  708. int test_subscription_ids(struct Options options)
  709. {
  710. MQTTAsync c;
  711. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer5;
  712. MQTTAsync_createOptions createOpts = MQTTAsync_createOptions_initializer;
  713. int rc = 0;
  714. MyLog(LOGA_INFO, "Starting V5 test 3 - subscription ids");
  715. fprintf(xml, "<testcase classname=\"test11\" name=\"subscription ids\"");
  716. global_start_time = start_clock();
  717. createOpts.MQTTVersion = MQTTVERSION_5;
  718. rc = MQTTAsync_createWithOptions(&c, options.connection, "subscription_ids",
  719. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL, &createOpts);
  720. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  721. if (rc != MQTTASYNC_SUCCESS)
  722. {
  723. MQTTAsync_destroy(&c);
  724. goto exit;
  725. }
  726. rc = MQTTAsync_setCallbacks(c, c, NULL, test_subscription_ids_messageArrived, NULL);
  727. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  728. opts.MQTTVersion = options.MQTTVersion;
  729. opts.onSuccess5 = test_subscription_ids_onConnect;
  730. opts.context = c;
  731. opts.cleanstart = 1;
  732. MyLog(LOGA_DEBUG, "Connecting");
  733. rc = MQTTAsync_connect(c, &opts);
  734. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  735. if (rc != MQTTASYNC_SUCCESS)
  736. goto exit;
  737. while (test_subscription_ids_globals.test_finished == 0)
  738. #if defined(_WIN32)
  739. Sleep(100);
  740. #else
  741. usleep(10000L);
  742. #endif
  743. MQTTAsync_destroy(&c);
  744. exit:
  745. MyLog(LOGA_INFO, "TEST3: test %s. %d tests run, %d failures.",
  746. (failures == 0) ? "passed" : "failed", tests, failures);
  747. write_test_result();
  748. return failures;
  749. }
  750. /*********************************************************************
  751. Test: flow control
  752. *********************************************************************/
  753. struct
  754. {
  755. char * test_topic;
  756. int test_finished;
  757. int messages_arrived;
  758. int receive_maximum;
  759. int blocking_found;
  760. } test_flow_control_globals =
  761. {
  762. "flow control topic", 0, 0, 65535, 0
  763. };
  764. int test_flow_control_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message)
  765. {
  766. MQTTAsync c = (MQTTAsync)context;
  767. test_flow_control_globals.messages_arrived++;
  768. assert("Message structure version should be 1", message->struct_version == 1,
  769. "message->struct_version was %d", message->struct_version);
  770. MyLog(LOGA_DEBUG, "Callback: message received on topic %s is %.*s.",
  771. topicName, message->payloadlen, (char*)(message->payload));
  772. if (message->struct_version == 1)
  773. logProperties(&message->properties);
  774. if (test_flow_control_globals.messages_arrived == test_flow_control_globals.receive_maximum + 2)
  775. test_flow_control_globals.test_finished = 1;
  776. MQTTAsync_freeMessage(&message);
  777. MQTTAsync_free(topicName);
  778. return 1;
  779. }
  780. void test_flow_control_onSubscribe(void* context, MQTTAsync_successData5* response)
  781. {
  782. MQTTAsync c = (MQTTAsync)context;
  783. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  784. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  785. int rc;
  786. int i = 0;
  787. MyLog(LOGA_DEBUG, "Suback properties:");
  788. logProperties(&response->properties);
  789. test_flow_control_globals.messages_arrived = 0;
  790. pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  791. pubmsg.payloadlen = 11;
  792. pubmsg.retained = 0;
  793. pubmsg.qos = 2;
  794. for (i = 0; i < test_flow_control_globals.receive_maximum + 2; ++i)
  795. {
  796. rc = MQTTAsync_sendMessage(c, test_flow_control_globals.test_topic, &pubmsg, &opts);
  797. assert("Good rc from send", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  798. if (rc != MQTTASYNC_SUCCESS)
  799. test_flow_control_globals.test_finished = 1;
  800. }
  801. }
  802. void test_flow_control_onConnect(void* context, MQTTAsync_successData5* response)
  803. {
  804. MQTTAsync c = (MQTTAsync)context;
  805. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  806. int rc;
  807. MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
  808. assert("Reason code should be 0", response->reasonCode == MQTTREASONCODE_SUCCESS,
  809. "Reason code was %d\n", response->reasonCode);
  810. MyLog(LOGA_DEBUG, "Connack properties:");
  811. logProperties(&response->properties);
  812. if (MQTTProperties_hasProperty(&response->properties, MQTTPROPERTY_CODE_RECEIVE_MAXIMUM))
  813. test_flow_control_globals.receive_maximum = MQTTProperties_getNumericValue(&response->properties, MQTTPROPERTY_CODE_RECEIVE_MAXIMUM);
  814. opts.onSuccess5 = test_flow_control_onSubscribe;
  815. opts.context = c;
  816. rc = MQTTAsync_subscribe(c, test_flow_control_globals.test_topic, 2, &opts);
  817. assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  818. if (rc != MQTTASYNC_SUCCESS)
  819. test_flow_control_globals.test_finished = 1;
  820. MQTTProperties_free(&opts.properties);
  821. }
  822. void flow_control_trace_callback(enum MQTTASYNC_TRACE_LEVELS level, char* message)
  823. {
  824. static char* msg = "Blocking on server receive maximum";
  825. if (strstr(message, msg) != NULL)
  826. test_flow_control_globals.blocking_found = 1;
  827. }
  828. int test_flow_control(struct Options options)
  829. {
  830. MQTTAsync c;
  831. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer5;
  832. MQTTAsync_createOptions createOpts = MQTTAsync_createOptions_initializer;
  833. int rc = 0;
  834. MyLog(LOGA_INFO, "Starting V5 test - flow control");
  835. fprintf(xml, "<testcase classname=\"test11\" name=\"flow control\"");
  836. global_start_time = start_clock();
  837. createOpts.MQTTVersion = MQTTVERSION_5;
  838. rc = MQTTAsync_createWithOptions(&c, options.connection, "flow_control",
  839. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL, &createOpts);
  840. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  841. if (rc != MQTTASYNC_SUCCESS)
  842. {
  843. MQTTAsync_destroy(&c);
  844. goto exit;
  845. }
  846. MQTTAsync_setTraceCallback(flow_control_trace_callback);
  847. MQTTAsync_setTraceLevel(MQTTASYNC_TRACE_MINIMUM);
  848. rc = MQTTAsync_setCallbacks(c, c, NULL, test_flow_control_messageArrived, NULL);
  849. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  850. opts.MQTTVersion = options.MQTTVersion;
  851. opts.onSuccess5 = test_flow_control_onConnect;
  852. opts.context = c;
  853. opts.cleanstart = 1;
  854. MyLog(LOGA_DEBUG, "Connecting");
  855. rc = MQTTAsync_connect(c, &opts);
  856. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  857. if (rc != MQTTASYNC_SUCCESS)
  858. goto exit;
  859. while (test_flow_control_globals.test_finished == 0)
  860. #if defined(_WIN32)
  861. Sleep(100);
  862. #else
  863. usleep(10000L);
  864. #endif
  865. assert("should have blocked", test_flow_control_globals.blocking_found == 1, "was %d\n",
  866. test_flow_control_globals.blocking_found);
  867. MQTTAsync_destroy(&c);
  868. exit:
  869. MyLog(LOGA_INFO, "TEST4: test %s. %d tests run, %d failures.",
  870. (failures == 0) ? "passed" : "failed", tests, failures);
  871. write_test_result();
  872. return failures;
  873. }
  874. struct
  875. {
  876. char* test_topic;
  877. int test_finished;
  878. int messages_arrived;
  879. } test_error_reporting_globals =
  880. {
  881. "error reporting topic", 0, 0
  882. };
  883. void test_error_reporting_onUnsubscribe(void* context, MQTTAsync_successData5* response)
  884. {
  885. MQTTAsync c = (MQTTAsync)context;
  886. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  887. int i = 0;
  888. MyLog(LOGA_DEBUG, "Unsuback properties:");
  889. logProperties(&response->properties);
  890. assert("Reason code count should be 2", response->alt.unsub.reasonCodeCount == 2,
  891. "Reason code count was %d\n", response->alt.unsub.reasonCodeCount);
  892. if (response->alt.unsub.reasonCodeCount == 1)
  893. MyLog(LOGA_DEBUG, "reason code %d", response->reasonCode);
  894. else if (response->alt.unsub.reasonCodeCount > 1)
  895. {
  896. for (i = 0; i < response->alt.unsub.reasonCodeCount; ++i)
  897. {
  898. MyLog(LOGA_DEBUG, "Unsubscribe reason code %d", response->alt.unsub.reasonCodes[i]);
  899. }
  900. }
  901. test_error_reporting_globals.test_finished = 1;
  902. }
  903. void test_error_reporting_onSubscribe(void* context, MQTTAsync_successData5* response)
  904. {
  905. MQTTAsync c = (MQTTAsync)context;
  906. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  907. MQTTProperty property;
  908. char* topics[2] = {test_error_reporting_globals.test_topic, "+"};
  909. int rc;
  910. int i = 0;
  911. MyLog(LOGA_DEBUG, "Suback properties:");
  912. logProperties(&response->properties);
  913. assert("Reason code count should be 2", response->alt.sub.reasonCodeCount == 2,
  914. "Reason code count was %d\n", response->alt.sub.reasonCodeCount);
  915. if (response->alt.sub.reasonCodeCount == 1)
  916. MyLog(LOGA_DEBUG, "reason code %d", response->reasonCode);
  917. else if (response->alt.sub.reasonCodeCount > 1)
  918. {
  919. for (i = 0; i < response->alt.sub.reasonCodeCount; ++i)
  920. {
  921. MyLog(LOGA_DEBUG, "Subscribe reason code %d", response->alt.sub.reasonCodes[i]);
  922. }
  923. }
  924. opts.onSuccess5 = test_error_reporting_onUnsubscribe;
  925. opts.context = c;
  926. property.identifier = MQTTPROPERTY_CODE_USER_PROPERTY;
  927. property.value.data.data = "test user property";
  928. property.value.data.len = (int)strlen(property.value.data.data);
  929. property.value.value.data = "test user property value";
  930. property.value.value.len = (int)strlen(property.value.value.data);
  931. MQTTProperties_add(&opts.properties, &property);
  932. rc = MQTTAsync_unsubscribeMany(c, 2, topics, &opts);
  933. assert("Good rc from unsubscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  934. if (rc != MQTTASYNC_SUCCESS)
  935. test_flow_control_globals.test_finished = 1;
  936. MQTTProperties_free(&opts.properties);
  937. }
  938. void test_error_reporting_onConnect(void* context, MQTTAsync_successData5* response)
  939. {
  940. MQTTAsync c = (MQTTAsync)context;
  941. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  942. int rc;
  943. char* topics[2] = {test_error_reporting_globals.test_topic, "+"};
  944. int qoss[2] = {2, 2};
  945. MQTTSubscribe_options subopts[2] = {MQTTSubscribe_options_initializer, MQTTSubscribe_options_initializer};
  946. MQTTProperty property;
  947. MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
  948. assert("Reason code should be 0", response->reasonCode == MQTTREASONCODE_SUCCESS,
  949. "Reason code was %d\n", response->reasonCode);
  950. MyLog(LOGA_DEBUG, "Connack properties:");
  951. logProperties(&response->properties);
  952. opts.onSuccess5 = test_error_reporting_onSubscribe;
  953. opts.context = c;
  954. property.identifier = MQTTPROPERTY_CODE_USER_PROPERTY;
  955. property.value.data.data = "test user property";
  956. property.value.data.len = (int)strlen(property.value.data.data);
  957. property.value.value.data = "test user property value";
  958. property.value.value.len = (int)strlen(property.value.value.data);
  959. MQTTProperties_add(&opts.properties, &property);
  960. opts.subscribeOptionsCount = 2;
  961. opts.subscribeOptionsList = subopts;
  962. rc = MQTTAsync_subscribeMany(c, 2, topics, qoss, &opts);
  963. assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  964. if (rc != MQTTASYNC_SUCCESS)
  965. test_flow_control_globals.test_finished = 1;
  966. MQTTProperties_free(&opts.properties);
  967. }
  968. int test_error_reporting(struct Options options)
  969. {
  970. MQTTAsync c;
  971. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer5;
  972. MQTTAsync_createOptions createOpts = MQTTAsync_createOptions_initializer;
  973. int rc = 0;
  974. MyLog(LOGA_INFO, "Starting V5 test - error reporting");
  975. fprintf(xml, "<testcase classname=\"test11\" name=\"error reporting\"");
  976. global_start_time = start_clock();
  977. createOpts.MQTTVersion = MQTTVERSION_5;
  978. rc = MQTTAsync_createWithOptions(&c, options.connection, "error reporting",
  979. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL, &createOpts);
  980. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  981. if (rc != MQTTASYNC_SUCCESS)
  982. {
  983. MQTTAsync_destroy(&c);
  984. goto exit;
  985. }
  986. rc = MQTTAsync_setCallbacks(c, c, NULL, test_flow_control_messageArrived, NULL);
  987. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  988. opts.MQTTVersion = options.MQTTVersion;
  989. opts.onSuccess5 = test_error_reporting_onConnect;
  990. opts.context = c;
  991. opts.cleanstart = 1;
  992. MyLog(LOGA_DEBUG, "Connecting");
  993. rc = MQTTAsync_connect(c, &opts);
  994. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  995. if (rc != MQTTASYNC_SUCCESS)
  996. goto exit;
  997. while (test_error_reporting_globals.test_finished == 0)
  998. #if defined(_WIN32)
  999. Sleep(100);
  1000. #else
  1001. usleep(10000L);
  1002. #endif
  1003. MQTTAsync_destroy(&c);
  1004. exit:
  1005. MyLog(LOGA_INFO, "TEST5: test %s. %d tests run, %d failures.",
  1006. (failures == 0) ? "passed" : "failed", tests, failures);
  1007. write_test_result();
  1008. return failures;
  1009. }
  1010. struct
  1011. {
  1012. int test_finished;
  1013. char* test_topic;
  1014. } test_qos_1_2_errors_globals =
  1015. {
  1016. 0, "test_qos_1_2_errors"
  1017. };
  1018. void test_qos_1_2_errors_onPublishSuccess(void* context, MQTTAsync_successData5* response)
  1019. {
  1020. MQTTAsync c = (MQTTAsync)context;
  1021. MyLog(LOGA_DEBUG, "Callback: publish success, reason code \"%s\" msgid: %d",
  1022. MQTTReasonCode_toString(response->reasonCode), response->token);
  1023. logProperties(&response->properties);
  1024. test_qos_1_2_errors_globals.test_finished = 1;
  1025. }
  1026. void test_qos_1_2_errors_onPublishFailure3(void* context, MQTTAsync_failureData5* response)
  1027. {
  1028. MQTTAsync c = (MQTTAsync)context;
  1029. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1030. MyLog(LOGA_DEBUG, "Callback: publish failure, reason code \"%s\" msgid: %d packet type: %d",
  1031. MQTTReasonCode_toString(response->reasonCode), response->token, response->packet_type);
  1032. logProperties(&response->properties);
  1033. test_qos_1_2_errors_globals.test_finished = 1;
  1034. }
  1035. void test_qos_1_2_errors_onPublishFailure2(void* context, MQTTAsync_failureData5* response)
  1036. {
  1037. MQTTAsync c = (MQTTAsync)context;
  1038. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1039. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  1040. MQTTProperty property;
  1041. int rc;
  1042. MyLog(LOGA_DEBUG, "Callback: publish failure, reason code \"%s\" msgid: %d packet type: %d",
  1043. MQTTReasonCode_toString(response->reasonCode), response->token, response->packet_type);
  1044. logProperties(&response->properties);
  1045. opts.onSuccess5 = test_qos_1_2_errors_onPublishSuccess;
  1046. opts.onFailure5 = test_qos_1_2_errors_onPublishFailure3;
  1047. opts.context = c;
  1048. pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  1049. pubmsg.payloadlen = 11;
  1050. pubmsg.qos = 2;
  1051. pubmsg.retained = 0;
  1052. property.identifier = MQTTPROPERTY_CODE_USER_PROPERTY;
  1053. property.value.data.data = "pub user property";
  1054. property.value.data.len = (int)strlen(property.value.data.data);
  1055. property.value.value.data = "pub user property value";
  1056. property.value.value.len = (int)strlen(property.value.value.data);
  1057. MQTTProperties_add(&pubmsg.properties, &property);
  1058. rc = MQTTAsync_sendMessage(c, "test_qos_1_2_errors_pubcomp", &pubmsg, &opts);
  1059. assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1060. if (rc != MQTTREASONCODE_SUCCESS)
  1061. test_qos_1_2_errors_globals.test_finished = 1;
  1062. MQTTProperties_free(&pubmsg.properties);
  1063. }
  1064. void test_qos_1_2_errors_onPublishFailure(void* context, MQTTAsync_failureData5* response)
  1065. {
  1066. MQTTAsync c = (MQTTAsync)context;
  1067. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1068. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  1069. MQTTProperty property;
  1070. int rc;
  1071. MyLog(LOGA_DEBUG, "Callback: publish failure, reason code \"%s\" msgid: %d packet type: %d",
  1072. MQTTReasonCode_toString(response->reasonCode), response->token, response->packet_type);
  1073. logProperties(&response->properties);
  1074. opts.onSuccess5 = test_qos_1_2_errors_onPublishSuccess;
  1075. opts.onFailure5 = test_qos_1_2_errors_onPublishFailure2;
  1076. opts.context = c;
  1077. pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  1078. pubmsg.payloadlen = 11;
  1079. pubmsg.qos = 2;
  1080. pubmsg.retained = 0;
  1081. property.identifier = MQTTPROPERTY_CODE_USER_PROPERTY;
  1082. property.value.data.data = "pub user property";
  1083. property.value.data.len = (int)strlen(property.value.data.data);
  1084. property.value.value.data = "pub user property value";
  1085. property.value.value.len = (int)strlen(property.value.value.data);
  1086. MQTTProperties_add(&pubmsg.properties, &property);
  1087. rc = MQTTAsync_sendMessage(c, test_qos_1_2_errors_globals.test_topic, &pubmsg, &opts);
  1088. assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1089. if (rc != MQTTREASONCODE_SUCCESS)
  1090. test_qos_1_2_errors_globals.test_finished = 1;
  1091. MQTTProperties_free(&pubmsg.properties);
  1092. }
  1093. void test_qos_1_2_errors_onConnect(void* context, MQTTAsync_successData5* response)
  1094. {
  1095. MQTTAsync c = (MQTTAsync)context;
  1096. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1097. MQTTProperty property;
  1098. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  1099. int rc;
  1100. MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
  1101. assert("Reason code should be 0", response->reasonCode == MQTTREASONCODE_SUCCESS,
  1102. "Reason code was %d\n", response->reasonCode);
  1103. MyLog(LOGA_DEBUG, "Connack properties:");
  1104. logProperties(&response->properties);
  1105. opts.onSuccess5 = test_qos_1_2_errors_onPublishSuccess;
  1106. opts.onFailure5 = test_qos_1_2_errors_onPublishFailure;
  1107. opts.context = c;
  1108. pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  1109. pubmsg.payloadlen = 11;
  1110. pubmsg.qos = 1;
  1111. pubmsg.retained = 0;
  1112. property.identifier = MQTTPROPERTY_CODE_USER_PROPERTY;
  1113. property.value.data.data = "pub user property";
  1114. property.value.data.len = (int)strlen(property.value.data.data);
  1115. property.value.value.data = "pub user property value";
  1116. property.value.value.len = (int)strlen(property.value.value.data);
  1117. MQTTProperties_add(&pubmsg.properties, &property);
  1118. rc = MQTTAsync_sendMessage(c, test_qos_1_2_errors_globals.test_topic, &pubmsg, &opts);
  1119. assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1120. if (rc != MQTTREASONCODE_SUCCESS)
  1121. test_qos_1_2_errors_globals.test_finished = 1;
  1122. MQTTProperties_free(&pubmsg.properties);
  1123. }
  1124. int test_qos_1_2_errors(struct Options options)
  1125. {
  1126. MQTTAsync c;
  1127. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer5;
  1128. MQTTAsync_createOptions createOpts = MQTTAsync_createOptions_initializer;
  1129. int rc = 0;
  1130. MyLog(LOGA_INFO, "Starting V5 test - qos 1 and 2 errors");
  1131. fprintf(xml, "<testcase classname=\"test11\" name=\"qos 1 and 2 errors\"");
  1132. global_start_time = start_clock();
  1133. createOpts.MQTTVersion = MQTTVERSION_5;
  1134. rc = MQTTAsync_createWithOptions(&c, options.connection, "qos 1 and 2 errors",
  1135. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL, &createOpts);
  1136. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1137. if (rc != MQTTASYNC_SUCCESS)
  1138. {
  1139. MQTTAsync_destroy(&c);
  1140. goto exit;
  1141. }
  1142. rc = MQTTAsync_setCallbacks(c, c, NULL, test_flow_control_messageArrived, NULL);
  1143. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1144. opts.MQTTVersion = options.MQTTVersion;
  1145. opts.onSuccess5 = test_qos_1_2_errors_onConnect;
  1146. opts.context = c;
  1147. opts.cleanstart = 1;
  1148. MyLog(LOGA_DEBUG, "Connecting");
  1149. rc = MQTTAsync_connect(c, &opts);
  1150. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1151. if (rc != MQTTASYNC_SUCCESS)
  1152. goto exit;
  1153. while (test_qos_1_2_errors_globals.test_finished == 0)
  1154. #if defined(_WIN32)
  1155. Sleep(100);
  1156. #else
  1157. usleep(10000L);
  1158. #endif
  1159. MQTTAsync_destroy(&c);
  1160. exit:
  1161. MyLog(LOGA_INFO, "TEST6: test %s. %d tests run, %d failures.",
  1162. (failures == 0) ? "passed" : "failed", tests, failures);
  1163. write_test_result();
  1164. return failures;
  1165. }
  1166. struct
  1167. {
  1168. int test_finished;
  1169. char* response_topic;
  1170. char* request_topic;
  1171. char* correlation_id;
  1172. int messages_arrived;
  1173. } test_request_response_globals =
  1174. {
  1175. 0,
  1176. "test_7_request",
  1177. "test_7_response",
  1178. "test_7_correlation_id",
  1179. 0
  1180. };
  1181. int test_request_response_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message)
  1182. {
  1183. MQTTAsync c = (MQTTAsync)context;
  1184. int rc;
  1185. test_request_response_globals.messages_arrived++;
  1186. assert("Message structure version should be 1", message->struct_version == 1,
  1187. "message->struct_version was %d", message->struct_version);
  1188. MyLog(LOGA_DEBUG, "Callback: message received on topic %s is %.*s.",
  1189. topicName, message->payloadlen, (char*)(message->payload));
  1190. if (message->struct_version == 1)
  1191. logProperties(&message->properties);
  1192. if (test_request_response_globals.messages_arrived == 1)
  1193. {
  1194. /* this is the request */
  1195. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1196. MQTTProperty *corr_prop = NULL, *response_topic_prop = NULL;
  1197. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  1198. MQTTProperty property;
  1199. char myTopicName[100];
  1200. assert("Topic should be request",
  1201. strcmp(test_request_response_globals.request_topic, topicName) == 0,
  1202. "topic was %s\n", topicName);
  1203. if (MQTTProperties_hasProperty(&message->properties, MQTTPROPERTY_CODE_RESPONSE_TOPIC))
  1204. response_topic_prop = MQTTProperties_getProperty(&message->properties, MQTTPROPERTY_CODE_RESPONSE_TOPIC);
  1205. assert("Topic should be response",
  1206. strncmp(test_request_response_globals.response_topic, response_topic_prop->value.data.data,
  1207. response_topic_prop->value.data.len) == 0,
  1208. "topic was %.4s\n", response_topic_prop->value.data.data);
  1209. if (MQTTProperties_hasProperty(&message->properties, MQTTPROPERTY_CODE_CORRELATION_DATA))
  1210. corr_prop = MQTTProperties_getProperty(&message->properties, MQTTPROPERTY_CODE_CORRELATION_DATA);
  1211. assert("Correlation data should be",
  1212. strncmp(test_request_response_globals.correlation_id, corr_prop->value.data.data,
  1213. corr_prop->value.data.len) == 0,
  1214. "Correlation data was %.4s\n", corr_prop->value.data.data);
  1215. /* send response */
  1216. pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  1217. pubmsg.payloadlen = 11;
  1218. pubmsg.retained = 0;
  1219. pubmsg.qos = 1;
  1220. property.identifier = MQTTPROPERTY_CODE_CORRELATION_DATA;
  1221. property.value.data.data = test_request_response_globals.correlation_id;
  1222. property.value.data.len = (int)strlen(property.value.data.data);
  1223. MQTTProperties_add(&pubmsg.properties, &property);
  1224. memcpy(myTopicName, response_topic_prop->value.data.data, response_topic_prop->value.data.len);
  1225. myTopicName[response_topic_prop->value.data.len] = '\0';
  1226. rc = MQTTAsync_sendMessage(c, myTopicName, &pubmsg, &opts);
  1227. if (rc != MQTTREASONCODE_SUCCESS)
  1228. test_request_response_globals.test_finished = 1;
  1229. assert("Good rc from sendMessage", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1230. MQTTProperties_free(&pubmsg.properties);
  1231. }
  1232. else if (test_request_response_globals.messages_arrived == 2)
  1233. {
  1234. MQTTProperty *corr_prop = NULL;
  1235. /* this should be response */
  1236. assert("Topic should be response",
  1237. strcmp(test_request_response_globals.response_topic, topicName) == 0,
  1238. "topic was %s\n", topicName);
  1239. if (MQTTProperties_hasProperty(&message->properties, MQTTPROPERTY_CODE_CORRELATION_DATA))
  1240. corr_prop = MQTTProperties_getProperty(&message->properties, MQTTPROPERTY_CODE_CORRELATION_DATA);
  1241. assert("Correlation data should be",
  1242. strncmp(test_request_response_globals.correlation_id, corr_prop->value.data.data,
  1243. corr_prop->value.data.len) == 0,
  1244. "Correlation data was %.4s\n", corr_prop->value.data.data);
  1245. test_request_response_globals.test_finished = 1;
  1246. }
  1247. MQTTAsync_freeMessage(&message);
  1248. MQTTAsync_free(topicName);
  1249. return 1;
  1250. }
  1251. void test_request_response_onSubscribe(void* context, MQTTAsync_successData5* response)
  1252. {
  1253. MQTTAsync c = (MQTTAsync)context;
  1254. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1255. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  1256. MQTTProperty property;
  1257. char* topics[2] = {test_error_reporting_globals.test_topic, "+"};
  1258. int rc;
  1259. int i = 0;
  1260. MyLog(LOGA_DEBUG, "Suback properties:");
  1261. logProperties(&response->properties);
  1262. assert("Reason code count should be 2", response->alt.sub.reasonCodeCount == 2,
  1263. "Reason code count was %d\n", response->alt.sub.reasonCodeCount);
  1264. if (response->alt.sub.reasonCodeCount == 1)
  1265. MyLog(LOGA_DEBUG, "reason code %d", response->reasonCode);
  1266. else if (response->alt.sub.reasonCodeCount > 1)
  1267. {
  1268. for (i = 0; i < response->alt.sub.reasonCodeCount; ++i)
  1269. {
  1270. MyLog(LOGA_DEBUG, "Subscribe reason code %d", response->alt.sub.reasonCodes[i]);
  1271. assert("Reason code should be 2", response->alt.sub.reasonCodes[i] == MQTTREASONCODE_GRANTED_QOS_2,
  1272. "Reason code was %d\n", response->alt.sub.reasonCodes[i]);
  1273. }
  1274. }
  1275. pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  1276. pubmsg.payloadlen = 11;
  1277. pubmsg.retained = 0;
  1278. pubmsg.qos = 1;
  1279. property.identifier = MQTTPROPERTY_CODE_RESPONSE_TOPIC;
  1280. property.value.data.data = test_request_response_globals.response_topic;
  1281. property.value.data.len = (int)strlen(property.value.data.data);
  1282. MQTTProperties_add(&pubmsg.properties, &property);
  1283. property.identifier = MQTTPROPERTY_CODE_CORRELATION_DATA;
  1284. property.value.data.data = test_request_response_globals.correlation_id;
  1285. property.value.data.len = (int)strlen(property.value.data.data);
  1286. MQTTProperties_add(&pubmsg.properties, &property);
  1287. rc = MQTTAsync_sendMessage(c, test_request_response_globals.request_topic, &pubmsg, &opts);
  1288. if (rc != MQTTREASONCODE_SUCCESS)
  1289. test_request_response_globals.test_finished = 1;
  1290. assert("Good rc from sendMessage", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1291. MQTTProperties_free(&pubmsg.properties);
  1292. }
  1293. void test_request_response_onConnect(void* context, MQTTAsync_successData5* response)
  1294. {
  1295. MQTTAsync c = (MQTTAsync)context;
  1296. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1297. int rc;
  1298. char* topics[2] = {test_request_response_globals.request_topic,
  1299. test_request_response_globals.response_topic};
  1300. int qos[2] = {2, 2};
  1301. MyLog(LOGA_DEBUG, "In request response connect onSuccess callback, context %p", context);
  1302. assert("Reason code should be 0", response->reasonCode == MQTTREASONCODE_SUCCESS,
  1303. "Reason code was %d\n", response->reasonCode);
  1304. MyLog(LOGA_DEBUG, "Connack properties:");
  1305. logProperties(&response->properties);
  1306. opts.onSuccess5 = test_request_response_onSubscribe;
  1307. opts.context = c;
  1308. rc = MQTTAsync_subscribeMany(c, 2, topics, qos, &opts);
  1309. if (rc != MQTTREASONCODE_SUCCESS)
  1310. test_request_response_globals.test_finished = 1;
  1311. assert("Good rc from subscribeMany", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1312. }
  1313. int test_request_response(struct Options options)
  1314. {
  1315. MQTTAsync c;
  1316. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer5;
  1317. MQTTAsync_createOptions createOpts = MQTTAsync_createOptions_initializer;
  1318. int rc = 0;
  1319. MyLog(LOGA_INFO, "Starting V5 test - request response");
  1320. fprintf(xml, "<testcase classname=\"test11\" name=\"request response\"");
  1321. global_start_time = start_clock();
  1322. createOpts.MQTTVersion = MQTTVERSION_5;
  1323. rc = MQTTAsync_createWithOptions(&c, options.connection, "request response",
  1324. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL, &createOpts);
  1325. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1326. if (rc != MQTTASYNC_SUCCESS)
  1327. {
  1328. MQTTAsync_destroy(&c);
  1329. goto exit;
  1330. }
  1331. rc = MQTTAsync_setCallbacks(c, c, NULL, test_request_response_messageArrived, NULL);
  1332. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1333. opts.MQTTVersion = options.MQTTVersion;
  1334. opts.onSuccess5 = test_request_response_onConnect;
  1335. opts.context = c;
  1336. opts.cleanstart = 1;
  1337. MyLog(LOGA_DEBUG, "Connecting");
  1338. rc = MQTTAsync_connect(c, &opts);
  1339. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1340. if (rc != MQTTASYNC_SUCCESS)
  1341. goto exit;
  1342. while (test_request_response_globals.test_finished == 0)
  1343. #if defined(_WIN32)
  1344. Sleep(100);
  1345. #else
  1346. usleep(10000L);
  1347. #endif
  1348. MQTTAsync_destroy(&c);
  1349. exit:
  1350. MyLog(LOGA_INFO, "TEST7: test %s. %d tests run, %d failures.",
  1351. (failures == 0) ? "passed" : "failed", tests, failures);
  1352. write_test_result();
  1353. return failures;
  1354. }
  1355. struct
  1356. {
  1357. int test_finished;
  1358. char* topic1;
  1359. char* topic2;
  1360. int messages_arrived;
  1361. } test_subscribeOptions_globals =
  1362. {
  1363. 0,
  1364. "subscribe options topic",
  1365. "+",
  1366. 0
  1367. };
  1368. int test_subscribeOptions_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message)
  1369. {
  1370. MQTTAsync c = (MQTTAsync)context;
  1371. test_subscribeOptions_globals.messages_arrived++;
  1372. assert("Message structure version should be 1", message->struct_version == 1,
  1373. "message->struct_version was %d", message->struct_version);
  1374. MyLog(LOGA_DEBUG, "Callback: message received on topic %s is %.*s.",
  1375. topicName, message->payloadlen, (char*)(message->payload));
  1376. if (message->struct_version == 1)
  1377. logProperties(&message->properties);
  1378. if (test_subscribeOptions_globals.messages_arrived == 1)
  1379. {
  1380. int subsidcount, subsid;
  1381. subsidcount = MQTTProperties_propertyCount(&message->properties, MQTTPROPERTY_CODE_SUBSCRIPTION_IDENTIFIER);
  1382. assert("Subsidcount is i", subsidcount == 1, "subsidcount is not correct %d\n", subsidcount);
  1383. subsid = MQTTProperties_getNumericValueAt(&message->properties, MQTTPROPERTY_CODE_SUBSCRIPTION_IDENTIFIER, 0);
  1384. assert("Subsid is 2", subsid == 2, "subsid is not correct %d\n", subsid);
  1385. test_subscribeOptions_globals.test_finished = 1;
  1386. }
  1387. MQTTAsync_freeMessage(&message);
  1388. MQTTAsync_free(topicName);
  1389. return 1;
  1390. }
  1391. void test_subscribeOptions_onSubscribe(void* context, MQTTAsync_successData5* response)
  1392. {
  1393. MQTTAsync c = (MQTTAsync)context;
  1394. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1395. int rc;
  1396. static int called = 0;
  1397. MyLog(LOGA_DEBUG, "In subscribe options connect onSuccess callback, context %p", context);
  1398. called++;
  1399. assert("Reason code should be 0", response->reasonCode == MQTTREASONCODE_GRANTED_QOS_2,
  1400. "Reason code was %d\n", response->reasonCode);
  1401. if (response->properties.count > 0)
  1402. {
  1403. MyLog(LOGA_DEBUG, "Suback properties:");
  1404. logProperties(&response->properties);
  1405. }
  1406. if (called == 1)
  1407. {
  1408. MQTTProperty property;
  1409. property.identifier = MQTTPROPERTY_CODE_SUBSCRIPTION_IDENTIFIER;
  1410. property.value.integer4 = 2;
  1411. MQTTProperties_add(&opts.properties, &property);
  1412. opts.onSuccess5 = test_subscribeOptions_onSubscribe;
  1413. opts.context = c;
  1414. opts.subscribeOptions.retainHandling = 2;
  1415. opts.subscribeOptions.retainAsPublished = 1;
  1416. rc = MQTTAsync_subscribe(c, test_subscribeOptions_globals.topic2, 2, &opts);
  1417. if (rc != MQTTREASONCODE_SUCCESS)
  1418. test_subscribeOptions_globals.test_finished = 1;
  1419. assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1420. MQTTProperties_free(&opts.properties);
  1421. }
  1422. else if (called == 2)
  1423. {
  1424. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  1425. pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  1426. pubmsg.payloadlen = 11;
  1427. pubmsg.retained = 0;
  1428. pubmsg.qos = 2;
  1429. rc = MQTTAsync_sendMessage(c, test_subscribeOptions_globals.topic1, &pubmsg, &opts);
  1430. assert("Good rc from send", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1431. if (rc != MQTTASYNC_SUCCESS)
  1432. test_subscribeOptions_globals.test_finished = 1;
  1433. }
  1434. }
  1435. void test_subscribeOptions_onConnect(void* context, MQTTAsync_successData5* response)
  1436. {
  1437. MQTTAsync c = (MQTTAsync)context;
  1438. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1439. MQTTProperty property;
  1440. int rc;
  1441. MyLog(LOGA_DEBUG, "In subscribe options connect onSuccess callback, context %p", context);
  1442. assert("Reason code should be 0", response->reasonCode == MQTTREASONCODE_SUCCESS,
  1443. "Reason code was %d\n", response->reasonCode);
  1444. MyLog(LOGA_DEBUG, "Connack properties:");
  1445. logProperties(&response->properties);
  1446. property.identifier = MQTTPROPERTY_CODE_SUBSCRIPTION_IDENTIFIER;
  1447. property.value.integer4 = 1;
  1448. MQTTProperties_add(&opts.properties, &property);
  1449. opts.subscribeOptions.noLocal = 1;
  1450. opts.onSuccess5 = test_subscribeOptions_onSubscribe;
  1451. opts.context = c;
  1452. rc = MQTTAsync_subscribe(c, test_subscribeOptions_globals.topic1, 2, &opts);
  1453. if (rc != MQTTREASONCODE_SUCCESS)
  1454. test_request_response_globals.test_finished = 1;
  1455. assert("Good rc from subscribeMany", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1456. MQTTProperties_free(&opts.properties);
  1457. }
  1458. int test_subscribeOptions(struct Options options)
  1459. {
  1460. MQTTAsync c;
  1461. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer5;
  1462. MQTTAsync_createOptions createOpts = MQTTAsync_createOptions_initializer;
  1463. int rc = 0;
  1464. MyLog(LOGA_INFO, "Starting V5 test - subscribe options");
  1465. fprintf(xml, "<testcase classname=\"test11\" name=\"subscribe options\"");
  1466. global_start_time = start_clock();
  1467. createOpts.MQTTVersion = MQTTVERSION_5;
  1468. rc = MQTTAsync_createWithOptions(&c, options.connection, "subscribe options",
  1469. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL, &createOpts);
  1470. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1471. if (rc != MQTTASYNC_SUCCESS)
  1472. {
  1473. MQTTAsync_destroy(&c);
  1474. goto exit;
  1475. }
  1476. rc = MQTTAsync_setCallbacks(c, c, NULL, test_subscribeOptions_messageArrived, NULL);
  1477. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1478. opts.MQTTVersion = options.MQTTVersion;
  1479. opts.onSuccess5 = test_subscribeOptions_onConnect;
  1480. opts.context = c;
  1481. opts.cleanstart = 1;
  1482. MyLog(LOGA_DEBUG, "Connecting");
  1483. rc = MQTTAsync_connect(c, &opts);
  1484. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1485. if (rc != MQTTASYNC_SUCCESS)
  1486. goto exit;
  1487. while (test_subscribeOptions_globals.test_finished == 0)
  1488. #if defined(_WIN32)
  1489. Sleep(100);
  1490. #else
  1491. usleep(10000L);
  1492. #endif
  1493. MQTTAsync_destroy(&c);
  1494. exit:
  1495. MyLog(LOGA_INFO, "TEST8: test %s. %d tests run, %d failures.",
  1496. (failures == 0) ? "passed" : "failed", tests, failures);
  1497. write_test_result();
  1498. return failures;
  1499. }
  1500. struct
  1501. {
  1502. char* shared_topic;
  1503. char* topic;
  1504. int messages_arrived;
  1505. int test_finished;
  1506. int message_count;
  1507. } test_shared_subscriptions_globals =
  1508. {
  1509. "$share/share_test/any",
  1510. "any",
  1511. 0,
  1512. 0,
  1513. 10,
  1514. };
  1515. int test_shared_subscriptions_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message)
  1516. {
  1517. MQTTAsync c = (MQTTAsync)context;
  1518. test_shared_subscriptions_globals.messages_arrived++;
  1519. assert("Message structure version should be 1", message->struct_version == 1,
  1520. "message->struct_version was %d", message->struct_version);
  1521. MyLog(LOGA_DEBUG, "Callback: message received on topic %s is %.*s.",
  1522. topicName, message->payloadlen, (char*)(message->payload));
  1523. if (message->struct_version == 1)
  1524. logProperties(&message->properties);
  1525. if (test_shared_subscriptions_globals.message_count == test_shared_subscriptions_globals.messages_arrived)
  1526. test_shared_subscriptions_globals.test_finished = 1;
  1527. MQTTAsync_freeMessage(&message);
  1528. MQTTAsync_free(topicName);
  1529. return 1;
  1530. }
  1531. void test_shared_subscriptions_onSubscribe(void* context, MQTTAsync_successData5* response)
  1532. {
  1533. MQTTAsync c = (MQTTAsync)context;
  1534. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1535. int rc;
  1536. static int called = 0;
  1537. called++;
  1538. MyLog(LOGA_DEBUG, "In subscribe options connect onSuccess callback, context %p, called %d", context, called);
  1539. assert("Reason code should be 0", response->reasonCode == MQTTREASONCODE_GRANTED_QOS_2,
  1540. "Reason code was %d\n", response->reasonCode);
  1541. if (response->properties.count > 0)
  1542. {
  1543. MyLog(LOGA_DEBUG, "Suback properties:");
  1544. logProperties(&response->properties);
  1545. }
  1546. if (called == 2) /* both clients now connected and subscribed */
  1547. {
  1548. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  1549. int i = 0;
  1550. char buf[100];
  1551. /* for each message we publish, only one of the subscribers should get it, not both */
  1552. sprintf(buf, "shared subscriptions sequence number %d", i);
  1553. pubmsg.payload = buf;
  1554. pubmsg.payloadlen = (int)strlen(buf);
  1555. pubmsg.retained = 0;
  1556. pubmsg.qos = 2;
  1557. for (i = 0; i < test_shared_subscriptions_globals.message_count; ++i)
  1558. {
  1559. rc = MQTTAsync_sendMessage(c, test_shared_subscriptions_globals.topic, &pubmsg, &opts);
  1560. assert("Good rc from send", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1561. if (rc != MQTTASYNC_SUCCESS)
  1562. {
  1563. test_subscribeOptions_globals.test_finished = 1;
  1564. break;
  1565. }
  1566. }
  1567. }
  1568. }
  1569. void test_shared_subscriptions_onConnectd(void* context, MQTTAsync_successData5* response)
  1570. {
  1571. MQTTAsync d = (MQTTAsync)context;
  1572. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1573. int rc;
  1574. MyLog(LOGA_DEBUG, "In shared subscriptions connect d onSuccess callback, context %p", context);
  1575. assert("Reason code should be 0", response->reasonCode == MQTTREASONCODE_SUCCESS,
  1576. "Reason code was %d\n", response->reasonCode);
  1577. opts.onSuccess5 = test_shared_subscriptions_onSubscribe;
  1578. opts.context = d;
  1579. rc = MQTTAsync_subscribe(d, test_shared_subscriptions_globals.shared_topic, 2, &opts);
  1580. assert("Good rc from subscribe", rc == MQTTREASONCODE_SUCCESS, "rc was %d", rc);
  1581. if (rc != MQTTREASONCODE_SUCCESS)
  1582. test_shared_subscriptions_globals.test_finished = 1;
  1583. }
  1584. void test_shared_subscriptions_onConnectc(void* context, MQTTAsync_successData5* response)
  1585. {
  1586. MQTTAsync c = (MQTTAsync)context;
  1587. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1588. int rc;
  1589. MyLog(LOGA_DEBUG, "In shared subscriptions connect c onSuccess callback, context %p", context);
  1590. assert("Reason code should be 0", response->reasonCode == MQTTREASONCODE_SUCCESS,
  1591. "Reason code was %d\n", response->reasonCode);
  1592. opts.onSuccess5 = test_shared_subscriptions_onSubscribe;
  1593. opts.context = c;
  1594. rc = MQTTAsync_subscribe(c, test_shared_subscriptions_globals.shared_topic, 2, &opts);
  1595. assert("Good rc from subscribe", rc == MQTTREASONCODE_SUCCESS, "rc was %d", rc);
  1596. if (rc != MQTTREASONCODE_SUCCESS)
  1597. test_shared_subscriptions_globals.test_finished = 1;
  1598. }
  1599. int test_shared_subscriptions(struct Options options)
  1600. {
  1601. MQTTAsync c, d;
  1602. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer5;
  1603. MQTTAsync_createOptions createOpts = MQTTAsync_createOptions_initializer;
  1604. int rc = 0, count = 0;
  1605. MyLog(LOGA_INFO, "Starting V5 test - shared subscriptions");
  1606. fprintf(xml, "<testcase classname=\"test11\" name=\"shared subscriptions\"");
  1607. global_start_time = start_clock();
  1608. createOpts.MQTTVersion = MQTTVERSION_5;
  1609. rc = MQTTAsync_createWithOptions(&c, options.connection, "shared subscriptions c",
  1610. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL, &createOpts);
  1611. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1612. if (rc != MQTTASYNC_SUCCESS)
  1613. {
  1614. MQTTAsync_destroy(&c);
  1615. goto exit;
  1616. }
  1617. rc = MQTTAsync_createWithOptions(&d, options.connection, "shared subscriptions d",
  1618. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL, &createOpts);
  1619. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1620. if (rc != MQTTASYNC_SUCCESS)
  1621. {
  1622. MQTTAsync_destroy(&c);
  1623. goto exit;
  1624. }
  1625. rc = MQTTAsync_setCallbacks(c, c, NULL, test_shared_subscriptions_messageArrived, NULL);
  1626. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1627. rc = MQTTAsync_setCallbacks(d, d, NULL, test_shared_subscriptions_messageArrived, NULL);
  1628. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1629. opts.MQTTVersion = options.MQTTVersion;
  1630. opts.onSuccess5 = test_shared_subscriptions_onConnectc;
  1631. opts.context = c;
  1632. opts.cleanstart = 1;
  1633. MyLog(LOGA_DEBUG, "Connecting");
  1634. rc = MQTTAsync_connect(c, &opts);
  1635. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1636. if (rc != MQTTASYNC_SUCCESS)
  1637. goto exit;
  1638. opts.onSuccess5 = test_shared_subscriptions_onConnectd;
  1639. opts.context = d;
  1640. MyLog(LOGA_DEBUG, "Connecting");
  1641. rc = MQTTAsync_connect(d, &opts);
  1642. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1643. if (rc != MQTTASYNC_SUCCESS)
  1644. goto exit;
  1645. while (test_shared_subscriptions_globals.test_finished == 0 && ++count < 1000)
  1646. #if defined(_WIN32)
  1647. Sleep(100);
  1648. #else
  1649. usleep(10000L);
  1650. #endif
  1651. /* sleep a bit more to see if any other messages arrive */
  1652. #if defined(_WIN32)
  1653. Sleep(100);
  1654. #else
  1655. usleep(10000L);
  1656. #endif
  1657. assert("Correct number of messages arrived",
  1658. test_shared_subscriptions_globals.message_count == test_shared_subscriptions_globals.messages_arrived,
  1659. "Actual number of messages received %d\n", test_shared_subscriptions_globals.messages_arrived);
  1660. MQTTAsync_destroy(&c);
  1661. MQTTAsync_destroy(&d);
  1662. exit:
  1663. MyLog(LOGA_INFO, "TEST9: test %s. %d tests run, %d failures.",
  1664. (failures == 0) ? "passed" : "failed", tests, failures);
  1665. write_test_result();
  1666. return failures;
  1667. }
  1668. void trace_callback(enum MQTTASYNC_TRACE_LEVELS level, char* message)
  1669. {
  1670. printf("Trace : %d, %s\n", level, message);
  1671. }
  1672. int main(int argc, char** argv)
  1673. {
  1674. int rc = -1;
  1675. int (*tests[])() = {NULL,
  1676. test_client_topic_aliases,
  1677. test_server_topic_aliases,
  1678. test_subscription_ids,
  1679. test_flow_control,
  1680. test_error_reporting,
  1681. test_qos_1_2_errors,
  1682. test_request_response,
  1683. test_subscribeOptions,
  1684. test_shared_subscriptions
  1685. }; /* indexed starting from 1 */
  1686. MQTTAsync_nameValue* info;
  1687. int i;
  1688. xml = fopen("TEST-test4.xml", "w");
  1689. fprintf(xml, "<testsuite name=\"test4\" tests=\"%d\">\n", (int)(ARRAY_SIZE(tests)) - 1);
  1690. getopts(argc, argv);
  1691. MQTTAsync_setTraceCallback(trace_callback);
  1692. info = MQTTAsync_getVersionInfo();
  1693. while (info->name)
  1694. {
  1695. MyLog(LOGA_INFO, "%s: %s", info->name, info->value);
  1696. info++;
  1697. }
  1698. for (i = 0; i < options.iterations; ++i)
  1699. {
  1700. if (options.test_no == -1)
  1701. { /* run all the tests */
  1702. for (options.test_no = 1; options.test_no < ARRAY_SIZE(tests); ++options.test_no)
  1703. {
  1704. failures = rc = 0;
  1705. MQTTAsync_setTraceLevel(MQTTASYNC_TRACE_ERROR);
  1706. rc += tests[options.test_no](options); /* return number of failures. 0 = test succeeded */
  1707. }
  1708. }
  1709. else
  1710. {
  1711. if (options.test_no >= ARRAY_SIZE(tests))
  1712. MyLog(LOGA_INFO, "No test number %d", options.test_no);
  1713. else
  1714. {
  1715. MQTTAsync_setTraceLevel(MQTTASYNC_TRACE_ERROR);
  1716. rc = tests[options.test_no](options); /* run just the selected test */
  1717. }
  1718. }
  1719. }
  1720. if (rc == 0)
  1721. MyLog(LOGA_INFO, "verdict pass");
  1722. else
  1723. MyLog(LOGA_INFO, "verdict fail");
  1724. fprintf(xml, "</testsuite>\n");
  1725. fclose(xml);
  1726. return rc;
  1727. }