test15.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2022 IBM Corp, Ian Craggs
  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 - change will message test back to using proxy
  17. * Ian Craggs - MQTT 5.0 support
  18. *******************************************************************************/
  19. /**
  20. * @file
  21. * Tests for the MQTT C client
  22. */
  23. #include "MQTTClient.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. #define setenv(a, b, c) _putenv_s(a, b)
  34. #endif
  35. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  36. void usage(void)
  37. {
  38. printf("help!!\n");
  39. exit(EXIT_FAILURE);
  40. }
  41. struct Options
  42. {
  43. char* connection; /**< connection to system under test. */
  44. char** haconnections;
  45. char* proxy_connection;
  46. int hacount;
  47. int verbose;
  48. int test_no;
  49. int MQTTVersion;
  50. int iterations;
  51. } options =
  52. {
  53. "tcp://localhost:1883",
  54. NULL,
  55. "tcp://localhost:1884",
  56. 0,
  57. 0,
  58. 0,
  59. MQTTVERSION_5,
  60. 1,
  61. };
  62. void getopts(int argc, char** argv)
  63. {
  64. int count = 1;
  65. while (count < argc)
  66. {
  67. if (strcmp(argv[count], "--test_no") == 0)
  68. {
  69. if (++count < argc)
  70. options.test_no = atoi(argv[count]);
  71. else
  72. usage();
  73. }
  74. else if (strcmp(argv[count], "--connection") == 0)
  75. {
  76. if (++count < argc)
  77. {
  78. options.connection = argv[count];
  79. printf("\nSetting connection to %s\n", options.connection);
  80. }
  81. else
  82. usage();
  83. }
  84. else if (strcmp(argv[count], "--haconnections") == 0)
  85. {
  86. if (++count < argc)
  87. {
  88. char* tok = strtok(argv[count], " ");
  89. options.hacount = 0;
  90. options.haconnections = malloc(sizeof(char*) * 5);
  91. while (tok)
  92. {
  93. options.haconnections[options.hacount] = malloc(strlen(tok) + 1);
  94. strcpy(options.haconnections[options.hacount], tok);
  95. options.hacount++;
  96. tok = strtok(NULL, " ");
  97. }
  98. }
  99. else
  100. usage();
  101. }
  102. else if (strcmp(argv[count], "--proxy_connection") == 0)
  103. {
  104. if (++count < argc)
  105. options.proxy_connection = argv[count];
  106. else
  107. usage();
  108. }
  109. else if (strcmp(argv[count], "--MQTTversion") == 0)
  110. {
  111. if (++count < argc)
  112. {
  113. options.MQTTVersion = atoi(argv[count]);
  114. printf("setting MQTT version to %d\n", options.MQTTVersion);
  115. }
  116. else
  117. usage();
  118. }
  119. else if (strcmp(argv[count], "--iterations") == 0)
  120. {
  121. if (++count < argc)
  122. options.iterations = atoi(argv[count]);
  123. else
  124. usage();
  125. }
  126. else if (strcmp(argv[count], "--verbose") == 0)
  127. {
  128. options.verbose = 1;
  129. printf("\nSetting verbose on\n");
  130. }
  131. count++;
  132. }
  133. }
  134. #define LOGA_DEBUG 0
  135. #define LOGA_INFO 1
  136. #include <stdarg.h>
  137. #include <time.h>
  138. #include <sys/timeb.h>
  139. void MyLog(int LOGA_level, char* format, ...)
  140. {
  141. static char msg_buf[256];
  142. va_list args;
  143. #if defined(_WIN32) || defined(_WINDOWS)
  144. struct timeb ts;
  145. #else
  146. struct timeval ts;
  147. #endif
  148. struct tm timeinfo;
  149. if (LOGA_level == LOGA_DEBUG && options.verbose == 0)
  150. return;
  151. #if defined(_WIN32) || defined(_WINDOWS)
  152. ftime(&ts);
  153. localtime_s(&timeinfo, &ts.time);
  154. #else
  155. gettimeofday(&ts, NULL);
  156. localtime_r(&ts.tv_sec, &timeinfo);
  157. #endif
  158. strftime(msg_buf, 80, "%Y%m%d %H%M%S", &timeinfo);
  159. #if defined(_WIN32) || defined(_WINDOWS)
  160. sprintf(&msg_buf[strlen(msg_buf)], ".%.3hu ", ts.millitm);
  161. #else
  162. sprintf(&msg_buf[strlen(msg_buf)], ".%.3lu ", ts.tv_usec / 1000L);
  163. #endif
  164. va_start(args, format);
  165. vsnprintf(&msg_buf[strlen(msg_buf)], sizeof(msg_buf) - strlen(msg_buf), format, args);
  166. va_end(args);
  167. printf("%s\n", msg_buf);
  168. fflush(stdout);
  169. }
  170. #if defined(_WIN32) || defined(_WINDOWS)
  171. #define mqsleep(A) Sleep(1000*A)
  172. #define START_TIME_TYPE DWORD
  173. static DWORD start_time = 0;
  174. START_TIME_TYPE start_clock(void)
  175. {
  176. return GetTickCount();
  177. }
  178. #elif defined(AIX)
  179. #define mqsleep sleep
  180. #define START_TIME_TYPE struct timespec
  181. START_TIME_TYPE start_clock(void)
  182. {
  183. static struct timespec start;
  184. clock_gettime(CLOCK_REALTIME, &start);
  185. return start;
  186. }
  187. #else
  188. #define mqsleep sleep
  189. #define START_TIME_TYPE struct timeval
  190. /* TODO - unused - remove? static struct timeval start_time; */
  191. START_TIME_TYPE start_clock(void)
  192. {
  193. struct timeval start_time;
  194. gettimeofday(&start_time, NULL);
  195. return start_time;
  196. }
  197. #endif
  198. #if defined(_WIN32)
  199. long elapsed(START_TIME_TYPE start_time)
  200. {
  201. return GetTickCount() - start_time;
  202. }
  203. #elif defined(AIX)
  204. #define assert(a)
  205. long elapsed(struct timespec start)
  206. {
  207. struct timespec now, res;
  208. clock_gettime(CLOCK_REALTIME, &now);
  209. ntimersub(now, start, res);
  210. return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L;
  211. }
  212. #else
  213. long elapsed(START_TIME_TYPE start_time)
  214. {
  215. struct timeval now, res;
  216. gettimeofday(&now, NULL);
  217. timersub(&now, &start_time, &res);
  218. return (res.tv_sec)*1000 + (res.tv_usec)/1000;
  219. }
  220. #endif
  221. #define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d)
  222. #define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e)
  223. int tests = 0;
  224. int failures = 0;
  225. FILE* xml;
  226. START_TIME_TYPE global_start_time;
  227. char output[3000];
  228. char* cur_output = output;
  229. void write_test_result(void)
  230. {
  231. long duration = elapsed(global_start_time);
  232. fprintf(xml, " time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000);
  233. if (cur_output != output)
  234. {
  235. fprintf(xml, "%s", output);
  236. cur_output = output;
  237. }
  238. fprintf(xml, "</testcase>\n");
  239. }
  240. void myassert(char* filename, int lineno, char* description, int value, char* format, ...)
  241. {
  242. ++tests;
  243. if (!value)
  244. {
  245. va_list args;
  246. ++failures;
  247. MyLog(LOGA_INFO, "Assertion failed, file %s, line %d, description: %s\n", filename, lineno, description);
  248. va_start(args, format);
  249. vprintf(format, args);
  250. va_end(args);
  251. cur_output += sprintf(cur_output, "<failure type=\"%s\">file %s, line %d </failure>\n",
  252. description, filename, lineno);
  253. }
  254. else
  255. MyLog(LOGA_DEBUG, "Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description);
  256. }
  257. /*********************************************************************
  258. Test1: single-threaded client
  259. *********************************************************************/
  260. void test1_sendAndReceive(MQTTClient* c, int qos, char* test_topic)
  261. {
  262. MQTTClient_deliveryToken dt;
  263. MQTTClient_message pubmsg = MQTTClient_message_initializer;
  264. MQTTClient_message* m = NULL;
  265. char* topicName = NULL;
  266. int topicLen;
  267. int i = 0;
  268. int iterations = 50;
  269. int rc;
  270. MQTTResponse resp;
  271. MQTTProperty property;
  272. MyLog(LOGA_DEBUG, "%d messages at QoS %d", iterations, qos);
  273. pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  274. pubmsg.payloadlen = 11;
  275. pubmsg.qos = qos;
  276. pubmsg.retained = 0;
  277. property.identifier = MQTTPROPERTY_CODE_USER_PROPERTY;
  278. property.value.data.data = "test user property";
  279. property.value.data.len = (int)strlen(property.value.data.data);
  280. property.value.value.data = "test user property value";
  281. property.value.value.len = (int)strlen(property.value.value.data);
  282. MQTTProperties_add(&pubmsg.properties, &property);
  283. for (i = 0; i < iterations; ++i)
  284. {
  285. if (i % 10 == 0)
  286. resp = MQTTClient_publish5(c, test_topic, pubmsg.payloadlen, pubmsg.payload, pubmsg.qos, pubmsg.retained,
  287. &pubmsg.properties, &dt);
  288. else
  289. resp = MQTTClient_publishMessage5(c, test_topic, &pubmsg, &dt);
  290. assert("Good rc from publish", resp.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", resp.reasonCode);
  291. if (qos > 0)
  292. {
  293. rc = MQTTClient_waitForCompletion(c, dt, 5000L);
  294. assert("Good rc from waitforCompletion", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  295. }
  296. rc = MQTTClient_receive(c, &topicName, &topicLen, &m, 5000);
  297. assert("Good rc from receive", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  298. if (topicName)
  299. {
  300. MyLog(LOGA_DEBUG, "Message received on topic %s is %.*s", topicName, m->payloadlen, (char*)(m->payload));
  301. if (pubmsg.payloadlen != m->payloadlen ||
  302. memcmp(m->payload, pubmsg.payload, m->payloadlen) != 0)
  303. {
  304. failures++;
  305. MyLog(LOGA_INFO, "Error: wrong data - received lengths %d %d", pubmsg.payloadlen, m->payloadlen);
  306. break;
  307. }
  308. assert("Property count should be > 0", m->properties.count > 0, "property count was %d", m->properties.count);
  309. MQTTClient_free(topicName);
  310. MQTTClient_freeMessage(&m);
  311. }
  312. else
  313. MyLog(LOGA_INFO, "No message received within timeout period\n");
  314. }
  315. /* receive any outstanding messages */
  316. MQTTClient_receive(c, &topicName, &topicLen, &m, 2000);
  317. while (topicName)
  318. {
  319. MyLog(LOGA_INFO, "Message received on topic %s is %.*s.\n", topicName, m->payloadlen, (char*)(m->payload));
  320. MQTTClient_free(topicName);
  321. MQTTClient_freeMessage(&m);
  322. MQTTClient_receive(c, &topicName, &topicLen, &m, 2000);
  323. }
  324. MQTTProperties_free(&pubmsg.properties);
  325. }
  326. void logProperties(MQTTProperties *props)
  327. {
  328. int i = 0;
  329. for (i = 0; i < props->count; ++i)
  330. {
  331. int id = props->array[i].identifier;
  332. const char* name = MQTTPropertyName(id);
  333. char* intformat = "Property name %s value %d";
  334. switch (MQTTProperty_getType(id))
  335. {
  336. case MQTTPROPERTY_TYPE_BYTE:
  337. MyLog(LOGA_INFO, intformat, name, props->array[i].value.byte);
  338. break;
  339. case MQTTPROPERTY_TYPE_TWO_BYTE_INTEGER:
  340. MyLog(LOGA_INFO, intformat, name, props->array[i].value.integer2);
  341. break;
  342. case MQTTPROPERTY_TYPE_FOUR_BYTE_INTEGER:
  343. MyLog(LOGA_INFO, intformat, name, props->array[i].value.integer4);
  344. break;
  345. case MQTTPROPERTY_TYPE_VARIABLE_BYTE_INTEGER:
  346. MyLog(LOGA_INFO, intformat, name, props->array[i].value.integer4);
  347. break;
  348. case MQTTPROPERTY_TYPE_BINARY_DATA:
  349. case MQTTPROPERTY_TYPE_UTF_8_ENCODED_STRING:
  350. MyLog(LOGA_INFO, "Property name %s value %.*s", name,
  351. props->array[i].value.data.len, props->array[i].value.data.data);
  352. break;
  353. case MQTTPROPERTY_TYPE_UTF_8_STRING_PAIR:
  354. MyLog(LOGA_INFO, "Property name %s key %.*s value %.*s", name,
  355. props->array[i].value.data.len, props->array[i].value.data.data,
  356. props->array[i].value.value.len, props->array[i].value.value.data);
  357. break;
  358. }
  359. }
  360. }
  361. int test1(struct Options options)
  362. {
  363. int subsqos = 2;
  364. MQTTClient c;
  365. MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer5;
  366. MQTTClient_willOptions wopts = MQTTClient_willOptions_initializer;
  367. MQTTProperties props = MQTTProperties_initializer;
  368. MQTTProperties willProps = MQTTProperties_initializer;
  369. MQTTProperty property;
  370. MQTTSubscribe_options subopts = MQTTSubscribe_options_initializer;
  371. MQTTResponse response = MQTTResponse_initializer;
  372. int rc = 0;
  373. char* test_topic = "C client test1";
  374. MQTTClient_createOptions createOpts = MQTTClient_createOptions_initializer;
  375. fprintf(xml, "<testcase classname=\"test1\" name=\"single threaded client using receive\"");
  376. global_start_time = start_clock();
  377. failures = 0;
  378. MyLog(LOGA_INFO, "Starting test 1 - single threaded client using receive");
  379. createOpts.MQTTVersion = MQTTVERSION_5;
  380. rc = MQTTClient_createWithOptions(&c, options.connection, "single_threaded_test",
  381. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL, &createOpts);
  382. assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  383. if (rc != MQTTCLIENT_SUCCESS)
  384. {
  385. MQTTClient_destroy(&c);
  386. goto exit;
  387. }
  388. opts.keepAliveInterval = 20;
  389. opts.cleanstart = 1;
  390. opts.username = "testuser";
  391. opts.password = "testpassword";
  392. opts.MQTTVersion = options.MQTTVersion;
  393. printf("test MQTT version %d\n", options.MQTTVersion);
  394. if (options.haconnections != NULL)
  395. {
  396. opts.serverURIs = options.haconnections;
  397. opts.serverURIcount = options.hacount;
  398. }
  399. opts.will = &wopts;
  400. opts.will->message = "will message";
  401. opts.will->qos = 1;
  402. opts.will->retained = 0;
  403. opts.will->topicName = "will topic";
  404. opts.will = NULL;
  405. property.identifier = MQTTPROPERTY_CODE_SESSION_EXPIRY_INTERVAL;
  406. property.value.integer4 = 30;
  407. MQTTProperties_add(&props, &property);
  408. property.identifier = MQTTPROPERTY_CODE_USER_PROPERTY;
  409. property.value.data.data = "test user property";
  410. property.value.data.len = (int)strlen(property.value.data.data);
  411. property.value.value.data = "test user property value";
  412. property.value.value.len = (int)strlen(property.value.value.data);
  413. MQTTProperties_add(&props, &property);
  414. MyLog(LOGA_DEBUG, "Connecting");
  415. response = MQTTClient_connect5(c, &opts, &props, &willProps);
  416. assert("Good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
  417. MQTTProperties_free(&props);
  418. MQTTProperties_free(&willProps);
  419. if (response.reasonCode != MQTTCLIENT_SUCCESS)
  420. goto exit;
  421. if (response.properties)
  422. {
  423. logProperties(response.properties);
  424. MQTTResponse_free(response);
  425. }
  426. subopts.retainAsPublished = 1;
  427. property.identifier = MQTTPROPERTY_CODE_SUBSCRIPTION_IDENTIFIER;
  428. property.value.integer4 = 33;
  429. MQTTProperties_add(&props, &property);
  430. response = MQTTClient_subscribe5(c, test_topic, subsqos, &subopts, &props);
  431. assert("Good rc from subscribe", response.reasonCode == subsqos, "rc was %d", response.reasonCode);
  432. MQTTProperties_free(&props);
  433. if (response.properties)
  434. {
  435. logProperties(response.properties);
  436. MQTTProperties_free(response.properties);
  437. }
  438. test1_sendAndReceive(c, 0, test_topic);
  439. test1_sendAndReceive(c, 1, test_topic);
  440. test1_sendAndReceive(c, 2, test_topic);
  441. MyLog(LOGA_DEBUG, "Stopping\n");
  442. MQTTProperties_free(&props);
  443. property.identifier = MQTTPROPERTY_CODE_USER_PROPERTY;
  444. property.value.data.data = "User property name";
  445. property.value.data.len = (int)strlen(property.value.data.data);
  446. property.value.value.data = "User property value";
  447. property.value.value.len = (int)strlen(property.value.value.data);
  448. MQTTProperties_add(&props, &property);
  449. response = MQTTClient_unsubscribe5(c, test_topic, &props);
  450. assert("Unsubscribe successful", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
  451. MQTTResponse_free(response);
  452. MQTTProperties_free(&props);
  453. property.identifier = MQTTPROPERTY_CODE_SESSION_EXPIRY_INTERVAL;
  454. property.value.integer4 = 0;
  455. MQTTProperties_add(&props, &property);
  456. rc = MQTTClient_disconnect5(c, 0, MQTTREASONCODE_SUCCESS, &props);
  457. assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  458. /* Just to make sure we can connect again */
  459. response = MQTTClient_connect5(c, &opts, NULL, NULL);
  460. assert("Connect successful", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
  461. MQTTResponse_free(response);
  462. rc = MQTTClient_disconnect5(c, 0, MQTTREASONCODE_SUCCESS, &props);
  463. assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  464. MQTTProperties_free(&props);
  465. MQTTClient_destroy(&c);
  466. exit:
  467. MyLog(LOGA_INFO, "TEST1: test %s. %d tests run, %d failures.",
  468. (failures == 0) ? "passed" : "failed", tests, failures);
  469. write_test_result();
  470. return failures;
  471. }
  472. /*********************************************************************
  473. Test2: multi-threaded client using callbacks
  474. *********************************************************************/
  475. volatile int test2_arrivedcount = 0;
  476. int test2_deliveryCompleted = 0;
  477. MQTTClient_message test2_pubmsg = MQTTClient_message_initializer;
  478. void test2_deliveryComplete(void* context, MQTTClient_deliveryToken dt)
  479. {
  480. ++test2_deliveryCompleted;
  481. }
  482. int test2_messageArrived(void* context, char* topicName, int topicLen, MQTTClient_message* message)
  483. {
  484. ++test2_arrivedcount;
  485. MyLog(LOGA_DEBUG, "Callback: %d message received on topic %s is %.*s.",
  486. test2_arrivedcount, topicName, message->payloadlen, (char*)(message->payload));
  487. assert("Message structure version should be 1", message->struct_version == 1,
  488. "message->struct_version was %d", message->struct_version);
  489. if (message->struct_version == 1)
  490. {
  491. const int props_count = 0;
  492. assert("Properties count should be 0", message->properties.count == props_count,
  493. "Properties count was %d\n", message->properties.count);
  494. logProperties(&message->properties);
  495. }
  496. if (test2_pubmsg.payloadlen != message->payloadlen ||
  497. memcmp(message->payload, test2_pubmsg.payload, message->payloadlen) != 0)
  498. {
  499. failures++;
  500. MyLog(LOGA_INFO, "Error: wrong data received lengths %d %d\n", test2_pubmsg.payloadlen, message->payloadlen);
  501. }
  502. MQTTClient_free(topicName);
  503. MQTTClient_freeMessage(&message);
  504. return 1;
  505. }
  506. void test2_sendAndReceive(MQTTClient* c, int qos, char* test_topic)
  507. {
  508. MQTTClient_deliveryToken dt;
  509. int i = 0;
  510. int iterations = 50;
  511. MQTTResponse response = MQTTResponse_initializer;
  512. int wait_seconds = 0;
  513. test2_deliveryCompleted = 0;
  514. MyLog(LOGA_INFO, "%d messages at QoS %d", iterations, qos);
  515. test2_pubmsg.payload = "a much longer message that we can shorten to the extent that we need to";
  516. test2_pubmsg.payloadlen = 27;
  517. test2_pubmsg.qos = qos;
  518. test2_pubmsg.retained = 0;
  519. for (i = 1; i <= iterations; ++i)
  520. {
  521. if (i % 10 == 0)
  522. response = MQTTClient_publish5(c, test_topic, test2_pubmsg.payloadlen, test2_pubmsg.payload,
  523. test2_pubmsg.qos, test2_pubmsg.retained, NULL, NULL);
  524. else
  525. response = MQTTClient_publishMessage5(c, test_topic, &test2_pubmsg, &dt);
  526. assert("Good rc from publish", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
  527. #if defined(_WIN32)
  528. Sleep(100);
  529. #else
  530. usleep(100000L);
  531. #endif
  532. wait_seconds = 10;
  533. while ((test2_arrivedcount < i) && (wait_seconds-- > 0))
  534. {
  535. MyLog(LOGA_DEBUG, "Arrived %d count %d", test2_arrivedcount, i);
  536. #if defined(_WIN32)
  537. Sleep(1000);
  538. #else
  539. usleep(1000000L);
  540. #endif
  541. }
  542. assert("Message Arrived", wait_seconds > 0,
  543. "Time out waiting for message %d\n", i );
  544. }
  545. if (qos > 0)
  546. {
  547. /* MQ Telemetry can send a message to a subscriber before the server has
  548. completed the QoS 2 handshake with the publisher. For QoS 1 and 2,
  549. allow time for the final delivery complete callback before checking
  550. that all expected callbacks have been made */
  551. wait_seconds = 10;
  552. while ((test2_deliveryCompleted < iterations) && (wait_seconds-- > 0))
  553. {
  554. MyLog(LOGA_DEBUG, "Delivery Completed %d count %d", test2_deliveryCompleted, i);
  555. #if defined(_WIN32)
  556. Sleep(1000);
  557. #else
  558. usleep(1000000L);
  559. #endif
  560. }
  561. assert("All Deliveries Complete", wait_seconds > 0,
  562. "Number of deliveryCompleted callbacks was %d\n",
  563. test2_deliveryCompleted);
  564. }
  565. }
  566. int test2(struct Options options)
  567. {
  568. char* testname = "test2";
  569. int subsqos = 2;
  570. /* TODO - usused - remove ? MQTTClient_deliveryToken* dt = NULL; */
  571. MQTTClient c;
  572. MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer5;
  573. MQTTProperties props = MQTTProperties_initializer;
  574. MQTTProperties willProps = MQTTProperties_initializer;
  575. MQTTResponse response = MQTTResponse_initializer;
  576. MQTTSubscribe_options subopts = MQTTSubscribe_options_initializer;
  577. int rc = 0;
  578. char* test_topic = "C client test2";
  579. MQTTClient_createOptions createOpts = MQTTClient_createOptions_initializer;
  580. fprintf(xml, "<testcase classname=\"test1\" name=\"multi-threaded client using callbacks\"");
  581. MyLog(LOGA_INFO, "Starting test 2 - multi-threaded client using callbacks");
  582. global_start_time = start_clock();
  583. failures = 0;
  584. createOpts.MQTTVersion = MQTTVERSION_5;
  585. MQTTClient_createWithOptions(&c, options.connection, "multi_threaded_sample",
  586. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL, &createOpts);
  587. opts.keepAliveInterval = 20;
  588. opts.cleanstart = 1;
  589. opts.MQTTVersion = options.MQTTVersion;
  590. opts.username = "testuser";
  591. opts.binarypwd.data = "testpassword";
  592. opts.binarypwd.len = (int)strlen(opts.binarypwd.data);
  593. if (options.haconnections != NULL)
  594. {
  595. opts.serverURIs = options.haconnections;
  596. opts.serverURIcount = options.hacount;
  597. }
  598. rc = MQTTClient_setCallbacks(c, NULL, NULL, test2_messageArrived, test2_deliveryComplete);
  599. assert("Good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  600. MyLog(LOGA_DEBUG, "Connecting");
  601. response = MQTTClient_connect5(c, &opts, &props, &willProps);
  602. assert("Good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS,
  603. "rc was %d", response.reasonCode);
  604. MQTTResponse_free(response);
  605. if (rc != MQTTCLIENT_SUCCESS)
  606. goto exit;
  607. response = MQTTClient_subscribe5(c, test_topic, subsqos, &subopts, &props);
  608. assert("Good rc from subscribe", response.reasonCode == subsqos, "rc was %d", rc);
  609. test2_sendAndReceive(c, 0, test_topic);
  610. test2_sendAndReceive(c, 1, test_topic);
  611. test2_sendAndReceive(c, 2, test_topic);
  612. MyLog(LOGA_DEBUG, "Stopping");
  613. response = MQTTClient_unsubscribe5(c, test_topic, &props);
  614. assert("Unsubscribe successful", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  615. rc = MQTTClient_disconnect5(c, 0, MQTTREASONCODE_SUCCESS, &props);
  616. assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  617. MQTTClient_destroy(&c);
  618. exit:
  619. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  620. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  621. write_test_result();
  622. return failures;
  623. }
  624. /*********************************************************************
  625. Test 3: connack return codes
  626. for AMQTDD, needs an amqtdd.cfg of:
  627. allow_anonymous false
  628. password_file passwords
  629. and a passwords file of:
  630. Admin:Admin
  631. *********************************************************************/
  632. int test3(struct Options options)
  633. {
  634. char* testname = "test3";
  635. int rc;
  636. MQTTClient c;
  637. MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer5;
  638. MQTTClient_willOptions wopts = MQTTClient_willOptions_initializer;
  639. MQTTResponse response;
  640. MQTTClient_createOptions createOpts = MQTTClient_createOptions_initializer;
  641. fprintf(xml, "<testcase classname=\"test1\" name=\"connack return codes\"");
  642. global_start_time = start_clock();
  643. failures = 0;
  644. MyLog(LOGA_INFO, "Starting test 3 - connack return codes");
  645. createOpts.MQTTVersion = MQTTVERSION_5;
  646. #if 0
  647. /* clientid too long (RC = 2) */
  648. rc = MQTTClient_create(&c, options.connection, "client_ID_too_long_for_MQTT_protocol_version_3",
  649. MQTTCLIENT_PERSISTENCE_NONE, NULL);
  650. assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  651. rc = MQTTClient_connect(c, &opts);
  652. assert("identifier rejected", rc == 2, "rc was %d\n", rc);
  653. MQTTClient_destroy(&c);
  654. #endif
  655. /* broker unavailable (RC = 3) - TDD when allow_anonymous not set*/
  656. rc = MQTTClient_createWithOptions(&c, options.connection, "The C Client", MQTTCLIENT_PERSISTENCE_NONE,
  657. NULL, &createOpts);
  658. assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  659. #if 0
  660. rc = MQTTClient_connect(c, &opts);
  661. assert("broker unavailable", rc == 3, "rc was %d\n", rc);
  662. /* authentication failure (RC = 4) */
  663. opts.username = "Admin";
  664. opts.password = "fred";
  665. rc = MQTTClient_connect(c, &opts);
  666. assert("Bad user name or password", rc == 4, "rc was %d\n", rc);
  667. #endif
  668. /* authorization failure (RC = 5) */
  669. opts.username = "Admin";
  670. opts.password = "Admin";
  671. /*opts.will = &wopts; "Admin" not authorized to publish to Will topic by default
  672. opts.will->message = "will message";
  673. opts.will->qos = 1;
  674. opts.will->retained = 0;
  675. opts.will->topicName = "will topic";*/
  676. response = MQTTClient_connect5(c, &opts, NULL, NULL);
  677. //assert("Not authorized", rc == 5, "rc was %d\n", rc);
  678. MQTTResponse_free(response);
  679. #if 0
  680. /* successful connection (RC = 0) */
  681. opts.username = "Admin";
  682. opts.password = "Admin";
  683. opts.will = NULL;
  684. rc = MQTTClient_connect(c, &opts);
  685. assert("successful connection", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  686. MQTTClient_disconnect(c, 0);
  687. MQTTClient_destroy(&c);
  688. #endif
  689. /* TODO - unused - remove ? exit: */
  690. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  691. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  692. write_test_result();
  693. return failures;
  694. }
  695. /*********************************************************************
  696. Test 4: client persistence 1
  697. *********************************************************************/
  698. int test4_run(int qos, int start_mqtt_version, int restore_mqtt_version)
  699. {
  700. char* testname = "test 4";
  701. char* topic = "Persistence test 1";
  702. int subsqos = 2;
  703. MQTTClient c;
  704. MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer5;
  705. MQTTClient_message* m = NULL;
  706. char* topicName = NULL;
  707. int topicLen;
  708. MQTTClient_deliveryToken* tokens = NULL;
  709. int mytoken = -99;
  710. char buffer[100];
  711. int count = 3;
  712. MQTTProperty property;
  713. MQTTProperties props = MQTTProperties_initializer;
  714. MQTTProperties pub_props = MQTTProperties_initializer;
  715. MQTTResponse response = MQTTResponse_initializer;
  716. MQTTClient_createOptions createOpts = MQTTClient_createOptions_initializer;
  717. int i, rc;
  718. failures = 0;
  719. MyLog(LOGA_INFO, "Starting test 4 - persistence, qos %d, MQTT versions: %s then %s", qos,
  720. (start_mqtt_version == MQTTVERSION_5) ? "5" : "3.1.1",
  721. (restore_mqtt_version == MQTTVERSION_5) ? "5" : "3.1.1");
  722. createOpts.MQTTVersion = start_mqtt_version;
  723. rc = MQTTClient_createWithOptions(&c, options.connection, "xrctest15_test_4",
  724. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL, &createOpts);
  725. assert("Good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  726. /* we might get some tokens back because they may not be cleaned up until
  727. * we connect cleanstart
  728. */
  729. rc = MQTTClient_getPendingDeliveryTokens(c, &tokens);
  730. assert("getPendingDeliveryTokens rc == 0", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  731. if (tokens)
  732. MQTTClient_free(tokens);
  733. opts.keepAliveInterval = 20;
  734. opts.reliable = 0;
  735. opts.MQTTVersion = start_mqtt_version;
  736. if (options.haconnections != NULL)
  737. {
  738. opts.serverURIs = options.haconnections;
  739. opts.serverURIcount = options.hacount;
  740. }
  741. if (start_mqtt_version == MQTTVERSION_5)
  742. {
  743. MyLog(LOGA_DEBUG, "Cleanup by connecting clean start, add session expiry > 0\n");
  744. opts.cleanstart = 1;
  745. property.identifier = MQTTPROPERTY_CODE_SESSION_EXPIRY_INTERVAL;
  746. property.value.integer4 = 30; /* in seconds */
  747. MQTTProperties_add(&props, &property);
  748. response = MQTTClient_connect5(c, &opts, &props, NULL);
  749. assert("Good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS,
  750. "rc was %d", response.reasonCode);
  751. MQTTResponse_free(response);
  752. if (response.reasonCode != MQTTCLIENT_SUCCESS)
  753. return -1;
  754. }
  755. else
  756. {
  757. MyLog(LOGA_DEBUG, "Cleanup by connecting clean session, then reconnecting non-cleansession\n");
  758. opts.cleanstart = 0; /* only applies to MQTT V5 */
  759. opts.cleansession = 1;
  760. rc = MQTTClient_connect(c, &opts);
  761. assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  762. rc = MQTTClient_disconnect(c, 1000);
  763. assert("Good rc from disconnect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  764. opts.cleansession = 0;
  765. rc = MQTTClient_connect(c, &opts);
  766. assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  767. if (rc != MQTTCLIENT_SUCCESS)
  768. return -1;
  769. }
  770. /* subscribe so we can get messages back */
  771. if (start_mqtt_version == MQTTVERSION_5)
  772. {
  773. response = MQTTClient_subscribe5(c, topic, subsqos, NULL, NULL);
  774. assert("Good rc from subscribe", response.reasonCode == subsqos, "rc was %d", response.reasonCode);
  775. }
  776. else
  777. {
  778. rc = MQTTClient_subscribe(c, topic, subsqos);
  779. assert("Good rc from subscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  780. }
  781. /* send messages so that we can receive the same ones */
  782. if (start_mqtt_version == MQTTVERSION_5)
  783. {
  784. property.identifier = MQTTPROPERTY_CODE_USER_PROPERTY;
  785. property.value.data.data = "test user property";
  786. property.value.data.len = (int)strlen(property.value.data.data);
  787. property.value.value.data = "test user property value";
  788. property.value.value.len = (int)strlen(property.value.value.data);
  789. MQTTProperties_add(&pub_props, &property);
  790. }
  791. for (i = 0; i < count; ++i)
  792. {
  793. sprintf(buffer, "Message sequence no %d", i);
  794. if (start_mqtt_version == MQTTVERSION_5)
  795. {
  796. response = MQTTClient_publish5(c, topic, 10, buffer, qos, 0, &pub_props, NULL);
  797. assert("Good rc from publish", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
  798. }
  799. else
  800. {
  801. rc = MQTTClient_publish(c, topic, 10, buffer, qos, 0, NULL);
  802. assert("Good rc from publish", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  803. }
  804. }
  805. /* disconnect immediately without receiving the incoming messages */
  806. if (start_mqtt_version == MQTTVERSION_5)
  807. rc = MQTTClient_disconnect5(c, 0, MQTTREASONCODE_SUCCESS, NULL); /* now there should be "orphaned" publications */
  808. else
  809. rc = MQTTClient_disconnect(c, 0); /* now there should be "orphaned" publications */
  810. assert("Good rc from disconnect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  811. rc = MQTTClient_getPendingDeliveryTokens(c, &tokens);
  812. assert("getPendingDeliveryTokens rc == 0", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  813. assert("should get some tokens back", tokens != NULL, "tokens was %p", tokens);
  814. if (tokens)
  815. {
  816. int i = 0;
  817. while (tokens[i] != -1)
  818. MyLog(LOGA_DEBUG, "Pending delivery token %d", tokens[i++]);
  819. assert1("no of tokens should be count", i == count, "no of tokens %d count %d", i, count);
  820. mytoken = tokens[0];
  821. MQTTClient_free(tokens);
  822. }
  823. MQTTProperties_free(&props);
  824. MQTTProperties_free(&pub_props);
  825. MQTTClient_destroy(&c); /* force re-reading persistence on create */
  826. createOpts.MQTTVersion = restore_mqtt_version;
  827. rc = MQTTClient_createWithOptions(&c, options.connection, "xrctest15_test_4",
  828. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL, &createOpts);
  829. if (start_mqtt_version == MQTTVERSION_5 && restore_mqtt_version == MQTTVERSION_3_1_1)
  830. {
  831. assert("Persistence error from create", rc == MQTTCLIENT_PERSISTENCE_ERROR, "rc was %d", rc);
  832. goto exit;
  833. }
  834. else
  835. assert("Good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  836. rc = MQTTClient_getPendingDeliveryTokens(c, &tokens);
  837. assert("getPendingDeliveryTokens rc == 0", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  838. assert("should get some tokens back", tokens != NULL, "tokens was %p", tokens);
  839. if (tokens)
  840. {
  841. int i = 0;
  842. while (tokens[i] != -1)
  843. MyLog(LOGA_DEBUG, "Pending delivery token %d", tokens[i++]);
  844. MQTTClient_free(tokens);
  845. assert1("no of tokens should be count", i == count, "no of tokens %d count %d", i, count);
  846. }
  847. MyLog(LOGA_DEBUG, "Reconnecting");
  848. opts.cleanstart = 0;
  849. response = MQTTClient_connect5(c, &opts, NULL, NULL);
  850. assert("Good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
  851. MQTTResponse_free(response);
  852. if (response.reasonCode != MQTTCLIENT_SUCCESS)
  853. return -1;
  854. for (i = 0; i < count; ++i)
  855. {
  856. int dup = 0;
  857. do
  858. {
  859. dup = 0;
  860. MQTTClient_receive(c, &topicName, &topicLen, &m, 5000);
  861. if (m && m->dup)
  862. {
  863. assert("No duplicates should be received for qos 2", qos == 1, "qos is %d", qos);
  864. MyLog(LOGA_DEBUG, "Duplicate message id %d", m->msgid);
  865. assert("properties are received", m->properties.count > 0, "property count is %d",
  866. m->properties.count);
  867. logProperties(&m->properties);
  868. MQTTClient_freeMessage(&m);
  869. MQTTClient_free(topicName);
  870. dup = 1;
  871. }
  872. } while (dup == 1);
  873. assert("should get a message", m != NULL, "m was %p", m);
  874. if (m)
  875. {
  876. MyLog(LOGA_DEBUG, "Received message id %d", m->msgid);
  877. assert("topicName is correct", strcmp(topicName, topic) == 0, "topicName is %s", topicName);
  878. if (start_mqtt_version == MQTTVERSION_5)
  879. assert("properties are received", m->properties.count > 0, "property count is %d",
  880. m->properties.count);
  881. else
  882. assert("properties are not received", m->properties.count == 0, "property count is %d",
  883. m->properties.count);
  884. logProperties(&m->properties);
  885. MQTTClient_freeMessage(&m);
  886. MQTTClient_free(topicName);
  887. }
  888. }
  889. /* call yield a few times until unfinished protocol exchanges are finished */
  890. count = 0;
  891. do
  892. {
  893. MQTTClient_yield();
  894. rc = MQTTClient_getPendingDeliveryTokens(c, &tokens);
  895. assert("getPendingDeliveryTokens rc == 0", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  896. } while (tokens != NULL && ++count < 10);
  897. rc = MQTTClient_getPendingDeliveryTokens(c, &tokens);
  898. assert("getPendingDeliveryTokens rc == 0", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  899. assert("should get no tokens back", tokens == NULL, "tokens was %p", tokens);
  900. rc = MQTTClient_disconnect5(c, 0, MQTTREASONCODE_SUCCESS, NULL);
  901. MQTTClient_destroy(&c);
  902. exit:
  903. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  904. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  905. return failures;
  906. }
  907. int test4(struct Options options)
  908. {
  909. int rc = 0;
  910. fprintf(xml, "<testcase classname=\"test4\" name=\"persistence\"");
  911. global_start_time = start_clock();
  912. rc = test4_run(1, MQTTVERSION_5, MQTTVERSION_5) +
  913. test4_run(2, MQTTVERSION_5, MQTTVERSION_5) +
  914. test4_run(2, MQTTVERSION_3_1_1, MQTTVERSION_5) +
  915. test4_run(2, MQTTVERSION_5, MQTTVERSION_3_1_1);
  916. fprintf(xml, " time=\"%ld\" >\n", elapsed(global_start_time) / 1000);
  917. if (cur_output != output)
  918. {
  919. fprintf(xml, "%s", output);
  920. cur_output = output;
  921. }
  922. fprintf(xml, "</testcase>\n");
  923. return rc;
  924. }
  925. /*********************************************************************
  926. Test 5: disconnect with quiesce timeout should allow exchanges to complete
  927. *********************************************************************/
  928. int test5(struct Options options)
  929. {
  930. char* testname = "test 5";
  931. char* topic = "Persistence test 2";
  932. int subsqos = 2;
  933. MQTTClient c;
  934. MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer5;
  935. MQTTClient_deliveryToken* tokens = NULL;
  936. char buffer[100];
  937. int count = 5;
  938. MQTTProperty property;
  939. MQTTProperties props = MQTTProperties_initializer;
  940. MQTTResponse response = MQTTResponse_initializer;
  941. int i, rc;
  942. MQTTClient_createOptions createOpts = MQTTClient_createOptions_initializer;
  943. fprintf(xml, "<testcase classname=\"test1\" name=\"disconnect with quiesce timeout should allow exchanges to complete\"");
  944. global_start_time = start_clock();
  945. failures = 0;
  946. MyLog(LOGA_INFO, "Starting test 5 - disconnect with quiesce timeout should allow exchanges to complete");
  947. createOpts.MQTTVersion = MQTTVERSION_5;
  948. MQTTClient_createWithOptions(&c, options.connection, "xrctest15_test_5", MQTTCLIENT_PERSISTENCE_DEFAULT,
  949. NULL, &createOpts);
  950. opts.keepAliveInterval = 20;
  951. opts.cleanstart = 1;
  952. opts.reliable = 0;
  953. opts.MQTTVersion = options.MQTTVersion;
  954. if (options.haconnections != NULL)
  955. {
  956. opts.serverURIs = options.haconnections;
  957. opts.serverURIcount = options.hacount;
  958. }
  959. property.identifier = MQTTPROPERTY_CODE_SESSION_EXPIRY_INTERVAL;
  960. property.value.integer4 = 30;
  961. MQTTProperties_add(&props, &property);
  962. MyLog(LOGA_DEBUG, "Connecting");
  963. response = MQTTClient_connect5(c, &opts, &props, NULL);
  964. assert("Good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
  965. MQTTResponse_free(response);
  966. if (response.reasonCode != MQTTCLIENT_SUCCESS)
  967. {
  968. MQTTClient_destroy(&c);
  969. goto exit;
  970. }
  971. response = MQTTClient_subscribe5(c, topic, subsqos, NULL, NULL);
  972. assert("Good rc from subscribe", response.reasonCode == subsqos, "rc was %d", response.reasonCode);
  973. for (i = 0; i < count; ++i)
  974. {
  975. sprintf(buffer, "Message sequence no %d", i);
  976. response = MQTTClient_publish5(c, topic, 10, buffer, 1, 0, NULL, NULL);
  977. assert("Good rc from publish", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
  978. }
  979. MQTTClient_disconnect5(c, 1000, MQTTREASONCODE_SUCCESS, NULL); /* now there should be no "orphaned" publications */
  980. MyLog(LOGA_DEBUG, "Disconnected");
  981. rc = MQTTClient_getPendingDeliveryTokens(c, &tokens);
  982. assert("getPendingDeliveryTokens rc == 0", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  983. assert("should get no tokens back", tokens == NULL, "tokens was %p", tokens);
  984. MQTTProperties_free(&props);
  985. MQTTClient_destroy(&c);
  986. exit:
  987. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  988. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  989. write_test_result();
  990. return failures;
  991. }
  992. /*********************************************************************
  993. Test 6: connectionLost and will message
  994. *********************************************************************/
  995. MQTTClient test6_c1, test6_c2;
  996. volatile int test6_will_message_arrived = 0;
  997. volatile int test6_connection_lost_called = 0;
  998. void test6_connectionLost(void* context, char* cause)
  999. {
  1000. MQTTClient c = (MQTTClient)context;
  1001. MyLog(LOGA_INFO, "%s -> Callback: connection lost", (c == test6_c1) ? "Client-1" : "Client-2");
  1002. test6_connection_lost_called = 1;
  1003. }
  1004. void test6_deliveryComplete(void* context, MQTTClient_deliveryToken token)
  1005. {
  1006. MyLog(LOGA_DEBUG, "Client-2 -> Callback: publish complete for token %d", token);
  1007. }
  1008. char* test6_will_topic = "C Test 2: will topic";
  1009. char* test6_will_message = "will message from Client-1";
  1010. int test6_messageArrived(void* context, char* topicName, int topicLen, MQTTClient_message* m)
  1011. {
  1012. MQTTClient c = (MQTTClient)context;
  1013. MyLog(LOGA_INFO, "%s -> Callback: message received on topic '%s' is '%.*s'",
  1014. (c == test6_c1) ? "Client-1" : "Client-2", topicName, m->payloadlen, (char*)(m->payload));
  1015. if (c == test6_c2 && strcmp(topicName, test6_will_topic) == 0 && memcmp(m->payload, test6_will_message, m->payloadlen) == 0)
  1016. test6_will_message_arrived = 1;
  1017. MQTTClient_free(topicName);
  1018. MQTTClient_freeMessage(&m);
  1019. return 1;
  1020. }
  1021. int test6(struct Options options)
  1022. {
  1023. char* testname = "test6";
  1024. MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer5;
  1025. MQTTClient_willOptions wopts = MQTTClient_willOptions_initializer;
  1026. MQTTClient_connectOptions opts2 = MQTTClient_connectOptions_initializer5;
  1027. MQTTResponse response = MQTTResponse_initializer;
  1028. int rc, count;
  1029. char* mqttsas_topic = "MQTTSAS topic";
  1030. MQTTClient_createOptions createOpts = MQTTClient_createOptions_initializer;
  1031. failures = 0;
  1032. MyLog(LOGA_INFO, "Starting test 6 - connectionLost and will messages");
  1033. fprintf(xml, "<testcase classname=\"test1\" name=\"connectionLost and will messages\"");
  1034. global_start_time = start_clock();
  1035. opts.keepAliveInterval = 2;
  1036. opts.cleanstart = 1;
  1037. opts.MQTTVersion = options.MQTTVersion;
  1038. opts.will = &wopts;
  1039. opts.will->message = test6_will_message;
  1040. opts.will->qos = 1;
  1041. opts.will->retained = 0;
  1042. opts.will->topicName = test6_will_topic;
  1043. if (options.haconnections != NULL)
  1044. {
  1045. opts.serverURIs = options.haconnections;
  1046. opts.serverURIcount = options.hacount;
  1047. }
  1048. /* Client-1 with Will options */
  1049. createOpts.MQTTVersion = MQTTVERSION_5;
  1050. rc = MQTTClient_createWithOptions(&test6_c1, options.proxy_connection, "Client_1", MQTTCLIENT_PERSISTENCE_DEFAULT,
  1051. NULL, &createOpts);
  1052. assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  1053. if (rc != MQTTCLIENT_SUCCESS)
  1054. goto exit;
  1055. rc = MQTTClient_setCallbacks(test6_c1, (void*)test6_c1, test6_connectionLost, test6_messageArrived, test6_deliveryComplete);
  1056. assert("good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  1057. if (rc != MQTTCLIENT_SUCCESS)
  1058. goto exit;
  1059. /* Connect to the broker */
  1060. response = MQTTClient_connect5(test6_c1, &opts, NULL, NULL);
  1061. assert("good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  1062. MQTTResponse_free(response);
  1063. if (rc != MQTTCLIENT_SUCCESS)
  1064. goto exit;
  1065. /* Client - 2 (multi-threaded) */
  1066. rc = MQTTClient_createWithOptions(&test6_c2, options.connection, "Client_2", MQTTCLIENT_PERSISTENCE_DEFAULT,
  1067. NULL, &createOpts);
  1068. assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  1069. /* Set the callback functions for the client */
  1070. rc = MQTTClient_setCallbacks(test6_c2, (void*)test6_c2, test6_connectionLost, test6_messageArrived, test6_deliveryComplete);
  1071. assert("good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  1072. /* Connect to the broker */
  1073. opts2.keepAliveInterval = 20;
  1074. opts2.cleanstart = 1;
  1075. MyLog(LOGA_INFO, "Connecting Client_2 ...");
  1076. response = MQTTClient_connect5(test6_c2, &opts2, NULL, NULL);
  1077. MQTTResponse_free(response);
  1078. assert("Good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d\n", response.reasonCode);
  1079. response = MQTTClient_subscribe5(test6_c2, test6_will_topic, 2, NULL, NULL);
  1080. assert("Good rc from subscribe", response.reasonCode == MQTTREASONCODE_GRANTED_QOS_2, "rc was %d\n", response.reasonCode);
  1081. /* now send the command which will break the connection and cause the will message to be sent */
  1082. response = MQTTClient_publish5(test6_c1, mqttsas_topic, (int)strlen("TERMINATE"), "TERMINATE", 0, 0, NULL, NULL);
  1083. assert("Good rc from publish", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d\n", response.reasonCode);
  1084. MyLog(LOGA_INFO, "Waiting to receive the will message");
  1085. count = 0;
  1086. while (++count < 40)
  1087. {
  1088. #if defined(_WIN32)
  1089. Sleep(1000L);
  1090. #else
  1091. sleep(1);
  1092. #endif
  1093. if (test6_will_message_arrived == 1 && test6_connection_lost_called == 1)
  1094. break;
  1095. }
  1096. assert("will message arrived", test6_will_message_arrived == 1,
  1097. "will_message_arrived was %d\n", test6_will_message_arrived);
  1098. assert("connection lost called", test6_connection_lost_called == 1,
  1099. "connection_lost_called %d\n", test6_connection_lost_called);
  1100. response = MQTTClient_unsubscribe5(test6_c2, test6_will_topic, NULL);
  1101. assert("Good rc from unsubscribe", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
  1102. rc = MQTTClient_isConnected(test6_c2);
  1103. assert("Client-2 still connected", rc == 1, "isconnected is %d", rc);
  1104. rc = MQTTClient_isConnected(test6_c1);
  1105. assert("Client-1 not connected", rc == 0, "isconnected is %d", rc);
  1106. rc = MQTTClient_disconnect5(test6_c2, 100L, MQTTREASONCODE_SUCCESS, NULL);
  1107. assert("Good rc from disconnect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  1108. MQTTClient_destroy(&test6_c1);
  1109. MQTTClient_destroy(&test6_c2);
  1110. exit:
  1111. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.\n",
  1112. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  1113. write_test_result();
  1114. return failures;
  1115. }
  1116. int test6a(struct Options options)
  1117. {
  1118. char* testname = "test6a";
  1119. MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer5;
  1120. MQTTClient_willOptions wopts = MQTTClient_willOptions_initializer;
  1121. MQTTClient_connectOptions opts2 = MQTTClient_connectOptions_initializer5;
  1122. int rc, count;
  1123. MQTTResponse response = MQTTResponse_initializer;
  1124. char* mqttsas_topic = "MQTTSAS topic";
  1125. MQTTClient_createOptions createOpts = MQTTClient_createOptions_initializer;
  1126. failures = 0;
  1127. MyLog(LOGA_INFO, "Starting test 6 - connectionLost and binary will messages");
  1128. fprintf(xml, "<testcase classname=\"test1\" name=\"connectionLost and binary will messages\"");
  1129. global_start_time = start_clock();
  1130. opts.keepAliveInterval = 2;
  1131. opts.cleanstart = 1;
  1132. opts.MQTTVersion = options.MQTTVersion;
  1133. opts.will = &wopts;
  1134. opts.will->payload.data = test6_will_message;
  1135. opts.will->payload.len = (int)strlen(test6_will_message) + 1;
  1136. opts.will->qos = 1;
  1137. opts.will->retained = 0;
  1138. opts.will->topicName = test6_will_topic;
  1139. if (options.haconnections != NULL)
  1140. {
  1141. opts.serverURIs = options.haconnections;
  1142. opts.serverURIcount = options.hacount;
  1143. }
  1144. /* Client-1 with Will options */
  1145. createOpts.MQTTVersion = MQTTVERSION_5;
  1146. rc = MQTTClient_createWithOptions(&test6_c1, options.proxy_connection, "Client_1", MQTTCLIENT_PERSISTENCE_DEFAULT,
  1147. NULL, &createOpts);
  1148. assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  1149. if (rc != MQTTCLIENT_SUCCESS)
  1150. goto exit;
  1151. rc = MQTTClient_setCallbacks(test6_c1, (void*)test6_c1, test6_connectionLost, test6_messageArrived, test6_deliveryComplete);
  1152. assert("good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  1153. if (rc != MQTTCLIENT_SUCCESS)
  1154. goto exit;
  1155. /* Connect to the broker */
  1156. response = MQTTClient_connect5(test6_c1, &opts, NULL, NULL);
  1157. assert("good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS,
  1158. "rc was %d\n", response.reasonCode);
  1159. MQTTResponse_free(response);
  1160. if (response.reasonCode != MQTTCLIENT_SUCCESS)
  1161. goto exit;
  1162. /* Client - 2 (multi-threaded) */
  1163. rc = MQTTClient_createWithOptions(&test6_c2, options.connection, "Client_2", MQTTCLIENT_PERSISTENCE_DEFAULT,
  1164. NULL, &createOpts);
  1165. assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  1166. /* Set the callback functions for the client */
  1167. rc = MQTTClient_setCallbacks(test6_c2, (void*)test6_c2, test6_connectionLost, test6_messageArrived, test6_deliveryComplete);
  1168. assert("good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  1169. /* Connect to the broker */
  1170. opts2.keepAliveInterval = 20;
  1171. opts2.cleanstart = 1;
  1172. MyLog(LOGA_INFO, "Connecting Client_2 ...");
  1173. response = MQTTClient_connect5(test6_c2, &opts2, NULL, NULL);
  1174. MQTTResponse_free(response);
  1175. assert("Good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d\n", response.reasonCode);
  1176. response = MQTTClient_subscribe5(test6_c2, test6_will_topic, 2, NULL, NULL);
  1177. assert("Good rc from subscribe", response.reasonCode == MQTTREASONCODE_GRANTED_QOS_2, "rc was %d\n", response.reasonCode);
  1178. /* now send the command which will break the connection and cause the will message to be sent */
  1179. response = MQTTClient_publish5(test6_c1, mqttsas_topic, (int)strlen("TERMINATE"), "TERMINATE", 0, 0, NULL, NULL);
  1180. assert("Good rc from publish", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d\n", response.reasonCode);
  1181. MyLog(LOGA_INFO, "Waiting to receive the will message");
  1182. count = 0;
  1183. while (++count < 40)
  1184. {
  1185. #if defined(_WIN32)
  1186. Sleep(1000L);
  1187. #else
  1188. sleep(1);
  1189. #endif
  1190. if (test6_will_message_arrived == 1 && test6_connection_lost_called == 1)
  1191. break;
  1192. }
  1193. assert("will message arrived", test6_will_message_arrived == 1,
  1194. "will_message_arrived was %d\n", test6_will_message_arrived);
  1195. assert("connection lost called", test6_connection_lost_called == 1,
  1196. "connection_lost_called %d\n", test6_connection_lost_called);
  1197. response = MQTTClient_unsubscribe5(test6_c2, test6_will_topic, NULL);
  1198. assert("Good rc from unsubscribe", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
  1199. rc = MQTTClient_isConnected(test6_c2);
  1200. assert("Client-2 still connected", rc == 1, "isconnected is %d", rc);
  1201. rc = MQTTClient_isConnected(test6_c1);
  1202. assert("Client-1 not connected", rc == 0, "isconnected is %d", rc);
  1203. rc = MQTTClient_disconnect5(test6_c2, 100L, MQTTREASONCODE_SUCCESS, NULL);
  1204. assert("Good rc from disconnect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  1205. MQTTClient_destroy(&test6_c1);
  1206. MQTTClient_destroy(&test6_c2);
  1207. exit:
  1208. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.\n",
  1209. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  1210. write_test_result();
  1211. return failures;
  1212. }
  1213. int main(int argc, char** argv)
  1214. {
  1215. int rc = 0;
  1216. int (*tests[])() = {NULL, test1, test2, test3, test4, test5, test6, test6a};
  1217. int i;
  1218. xml = fopen("TEST-test1.xml", "w");
  1219. fprintf(xml, "<testsuite name=\"test1\" tests=\"%d\">\n", (int)(ARRAY_SIZE(tests) - 1));
  1220. setenv("MQTT_C_CLIENT_TRACE", "ON", 1);
  1221. setenv("MQTT_C_CLIENT_TRACE_LEVEL", "ERROR", 1);
  1222. getopts(argc, argv);
  1223. for (i = 0; i < options.iterations; ++i)
  1224. {
  1225. if (options.test_no == 0)
  1226. { /* run all the tests */
  1227. for (options.test_no = 1; options.test_no < ARRAY_SIZE(tests); ++options.test_no)
  1228. rc += tests[options.test_no](options); /* return number of failures. 0 = test succeeded */
  1229. }
  1230. else
  1231. rc = tests[options.test_no](options); /* run just the selected test */
  1232. }
  1233. if (rc == 0)
  1234. MyLog(LOGA_INFO, "verdict pass");
  1235. else
  1236. MyLog(LOGA_INFO, "verdict fail");
  1237. fprintf(xml, "</testsuite>\n");
  1238. fclose(xml);
  1239. return rc;
  1240. }