test15.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2020 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 - 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 / 1000);
  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. MQTTClient_yield(); /* allow any unfinished protocol exchanges to finish */
  890. rc = MQTTClient_getPendingDeliveryTokens(c, &tokens);
  891. assert("getPendingDeliveryTokens rc == 0", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  892. assert("should get no tokens back", tokens == NULL, "tokens was %p", tokens);
  893. rc = MQTTClient_disconnect5(c, 0, MQTTREASONCODE_SUCCESS, NULL);
  894. MQTTClient_destroy(&c);
  895. exit:
  896. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  897. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  898. return failures;
  899. }
  900. int test4(struct Options options)
  901. {
  902. int rc = 0;
  903. fprintf(xml, "<testcase classname=\"test4\" name=\"persistence\"");
  904. global_start_time = start_clock();
  905. rc = test4_run(1, MQTTVERSION_5, MQTTVERSION_5) +
  906. test4_run(2, MQTTVERSION_5, MQTTVERSION_5) +
  907. test4_run(2, MQTTVERSION_3_1_1, MQTTVERSION_5) +
  908. test4_run(2, MQTTVERSION_5, MQTTVERSION_3_1_1);
  909. fprintf(xml, " time=\"%ld\" >\n", elapsed(global_start_time) / 1000);
  910. if (cur_output != output)
  911. {
  912. fprintf(xml, "%s", output);
  913. cur_output = output;
  914. }
  915. fprintf(xml, "</testcase>\n");
  916. return rc;
  917. }
  918. /*********************************************************************
  919. Test 5: disconnect with quiesce timeout should allow exchanges to complete
  920. *********************************************************************/
  921. int test5(struct Options options)
  922. {
  923. char* testname = "test 5";
  924. char* topic = "Persistence test 2";
  925. int subsqos = 2;
  926. MQTTClient c;
  927. MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer5;
  928. MQTTClient_deliveryToken* tokens = NULL;
  929. char buffer[100];
  930. int count = 5;
  931. MQTTProperty property;
  932. MQTTProperties props = MQTTProperties_initializer;
  933. MQTTResponse response = MQTTResponse_initializer;
  934. int i, rc;
  935. MQTTClient_createOptions createOpts = MQTTClient_createOptions_initializer;
  936. fprintf(xml, "<testcase classname=\"test1\" name=\"disconnect with quiesce timeout should allow exchanges to complete\"");
  937. global_start_time = start_clock();
  938. failures = 0;
  939. MyLog(LOGA_INFO, "Starting test 5 - disconnect with quiesce timeout should allow exchanges to complete");
  940. createOpts.MQTTVersion = MQTTVERSION_5;
  941. MQTTClient_createWithOptions(&c, options.connection, "xrctest15_test_5", MQTTCLIENT_PERSISTENCE_DEFAULT,
  942. NULL, &createOpts);
  943. opts.keepAliveInterval = 20;
  944. opts.cleanstart = 1;
  945. opts.reliable = 0;
  946. opts.MQTTVersion = options.MQTTVersion;
  947. if (options.haconnections != NULL)
  948. {
  949. opts.serverURIs = options.haconnections;
  950. opts.serverURIcount = options.hacount;
  951. }
  952. property.identifier = MQTTPROPERTY_CODE_SESSION_EXPIRY_INTERVAL;
  953. property.value.integer4 = 30;
  954. MQTTProperties_add(&props, &property);
  955. MyLog(LOGA_DEBUG, "Connecting");
  956. response = MQTTClient_connect5(c, &opts, &props, NULL);
  957. assert("Good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
  958. MQTTResponse_free(response);
  959. if (response.reasonCode != MQTTCLIENT_SUCCESS)
  960. {
  961. MQTTClient_destroy(&c);
  962. goto exit;
  963. }
  964. response = MQTTClient_subscribe5(c, topic, subsqos, NULL, NULL);
  965. assert("Good rc from subscribe", response.reasonCode == subsqos, "rc was %d", response.reasonCode);
  966. for (i = 0; i < count; ++i)
  967. {
  968. sprintf(buffer, "Message sequence no %d", i);
  969. response = MQTTClient_publish5(c, topic, 10, buffer, 1, 0, NULL, NULL);
  970. assert("Good rc from publish", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
  971. }
  972. MQTTClient_disconnect5(c, 1000, MQTTREASONCODE_SUCCESS, NULL); /* now there should be no "orphaned" publications */
  973. MyLog(LOGA_DEBUG, "Disconnected");
  974. rc = MQTTClient_getPendingDeliveryTokens(c, &tokens);
  975. assert("getPendingDeliveryTokens rc == 0", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  976. assert("should get no tokens back", tokens == NULL, "tokens was %p", tokens);
  977. MQTTProperties_free(&props);
  978. MQTTClient_destroy(&c);
  979. exit:
  980. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  981. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  982. write_test_result();
  983. return failures;
  984. }
  985. /*********************************************************************
  986. Test 6: connectionLost and will message
  987. *********************************************************************/
  988. MQTTClient test6_c1, test6_c2;
  989. volatile int test6_will_message_arrived = 0;
  990. volatile int test6_connection_lost_called = 0;
  991. void test6_connectionLost(void* context, char* cause)
  992. {
  993. MQTTClient c = (MQTTClient)context;
  994. MyLog(LOGA_INFO, "%s -> Callback: connection lost", (c == test6_c1) ? "Client-1" : "Client-2");
  995. test6_connection_lost_called = 1;
  996. }
  997. void test6_deliveryComplete(void* context, MQTTClient_deliveryToken token)
  998. {
  999. MyLog(LOGA_DEBUG, "Client-2 -> Callback: publish complete for token %d", token);
  1000. }
  1001. char* test6_will_topic = "C Test 2: will topic";
  1002. char* test6_will_message = "will message from Client-1";
  1003. int test6_messageArrived(void* context, char* topicName, int topicLen, MQTTClient_message* m)
  1004. {
  1005. MQTTClient c = (MQTTClient)context;
  1006. MyLog(LOGA_INFO, "%s -> Callback: message received on topic '%s' is '%.*s'",
  1007. (c == test6_c1) ? "Client-1" : "Client-2", topicName, m->payloadlen, (char*)(m->payload));
  1008. if (c == test6_c2 && strcmp(topicName, test6_will_topic) == 0 && memcmp(m->payload, test6_will_message, m->payloadlen) == 0)
  1009. test6_will_message_arrived = 1;
  1010. MQTTClient_free(topicName);
  1011. MQTTClient_freeMessage(&m);
  1012. return 1;
  1013. }
  1014. int test6(struct Options options)
  1015. {
  1016. char* testname = "test6";
  1017. MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer5;
  1018. MQTTClient_willOptions wopts = MQTTClient_willOptions_initializer;
  1019. MQTTClient_connectOptions opts2 = MQTTClient_connectOptions_initializer5;
  1020. MQTTResponse response = MQTTResponse_initializer;
  1021. int rc, count;
  1022. char* mqttsas_topic = "MQTTSAS topic";
  1023. MQTTClient_createOptions createOpts = MQTTClient_createOptions_initializer;
  1024. failures = 0;
  1025. MyLog(LOGA_INFO, "Starting test 6 - connectionLost and will messages");
  1026. fprintf(xml, "<testcase classname=\"test1\" name=\"connectionLost and will messages\"");
  1027. global_start_time = start_clock();
  1028. opts.keepAliveInterval = 2;
  1029. opts.cleanstart = 1;
  1030. opts.MQTTVersion = options.MQTTVersion;
  1031. opts.will = &wopts;
  1032. opts.will->message = test6_will_message;
  1033. opts.will->qos = 1;
  1034. opts.will->retained = 0;
  1035. opts.will->topicName = test6_will_topic;
  1036. if (options.haconnections != NULL)
  1037. {
  1038. opts.serverURIs = options.haconnections;
  1039. opts.serverURIcount = options.hacount;
  1040. }
  1041. /* Client-1 with Will options */
  1042. createOpts.MQTTVersion = MQTTVERSION_5;
  1043. rc = MQTTClient_createWithOptions(&test6_c1, options.proxy_connection, "Client_1", MQTTCLIENT_PERSISTENCE_DEFAULT,
  1044. NULL, &createOpts);
  1045. assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  1046. if (rc != MQTTCLIENT_SUCCESS)
  1047. goto exit;
  1048. rc = MQTTClient_setCallbacks(test6_c1, (void*)test6_c1, test6_connectionLost, test6_messageArrived, test6_deliveryComplete);
  1049. assert("good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  1050. if (rc != MQTTCLIENT_SUCCESS)
  1051. goto exit;
  1052. /* Connect to the broker */
  1053. response = MQTTClient_connect5(test6_c1, &opts, NULL, NULL);
  1054. assert("good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  1055. MQTTResponse_free(response);
  1056. if (rc != MQTTCLIENT_SUCCESS)
  1057. goto exit;
  1058. /* Client - 2 (multi-threaded) */
  1059. rc = MQTTClient_createWithOptions(&test6_c2, options.connection, "Client_2", MQTTCLIENT_PERSISTENCE_DEFAULT,
  1060. NULL, &createOpts);
  1061. assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  1062. /* Set the callback functions for the client */
  1063. rc = MQTTClient_setCallbacks(test6_c2, (void*)test6_c2, test6_connectionLost, test6_messageArrived, test6_deliveryComplete);
  1064. assert("good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  1065. /* Connect to the broker */
  1066. opts2.keepAliveInterval = 20;
  1067. opts2.cleanstart = 1;
  1068. MyLog(LOGA_INFO, "Connecting Client_2 ...");
  1069. response = MQTTClient_connect5(test6_c2, &opts2, NULL, NULL);
  1070. MQTTResponse_free(response);
  1071. assert("Good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d\n", response.reasonCode);
  1072. response = MQTTClient_subscribe5(test6_c2, test6_will_topic, 2, NULL, NULL);
  1073. assert("Good rc from subscribe", response.reasonCode == MQTTREASONCODE_GRANTED_QOS_2, "rc was %d\n", response.reasonCode);
  1074. /* now send the command which will break the connection and cause the will message to be sent */
  1075. response = MQTTClient_publish5(test6_c1, mqttsas_topic, (int)strlen("TERMINATE"), "TERMINATE", 0, 0, NULL, NULL);
  1076. assert("Good rc from publish", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d\n", response.reasonCode);
  1077. MyLog(LOGA_INFO, "Waiting to receive the will message");
  1078. count = 0;
  1079. while (++count < 40)
  1080. {
  1081. #if defined(_WIN32)
  1082. Sleep(1000L);
  1083. #else
  1084. sleep(1);
  1085. #endif
  1086. if (test6_will_message_arrived == 1 && test6_connection_lost_called == 1)
  1087. break;
  1088. }
  1089. assert("will message arrived", test6_will_message_arrived == 1,
  1090. "will_message_arrived was %d\n", test6_will_message_arrived);
  1091. assert("connection lost called", test6_connection_lost_called == 1,
  1092. "connection_lost_called %d\n", test6_connection_lost_called);
  1093. response = MQTTClient_unsubscribe5(test6_c2, test6_will_topic, NULL);
  1094. assert("Good rc from unsubscribe", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
  1095. rc = MQTTClient_isConnected(test6_c2);
  1096. assert("Client-2 still connected", rc == 1, "isconnected is %d", rc);
  1097. rc = MQTTClient_isConnected(test6_c1);
  1098. assert("Client-1 not connected", rc == 0, "isconnected is %d", rc);
  1099. rc = MQTTClient_disconnect5(test6_c2, 100L, MQTTREASONCODE_SUCCESS, NULL);
  1100. assert("Good rc from disconnect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  1101. MQTTClient_destroy(&test6_c1);
  1102. MQTTClient_destroy(&test6_c2);
  1103. exit:
  1104. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.\n",
  1105. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  1106. write_test_result();
  1107. return failures;
  1108. }
  1109. int test6a(struct Options options)
  1110. {
  1111. char* testname = "test6a";
  1112. MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer5;
  1113. MQTTClient_willOptions wopts = MQTTClient_willOptions_initializer;
  1114. MQTTClient_connectOptions opts2 = MQTTClient_connectOptions_initializer5;
  1115. int rc, count;
  1116. MQTTResponse response = MQTTResponse_initializer;
  1117. char* mqttsas_topic = "MQTTSAS topic";
  1118. MQTTClient_createOptions createOpts = MQTTClient_createOptions_initializer;
  1119. failures = 0;
  1120. MyLog(LOGA_INFO, "Starting test 6 - connectionLost and binary will messages");
  1121. fprintf(xml, "<testcase classname=\"test1\" name=\"connectionLost and binary will messages\"");
  1122. global_start_time = start_clock();
  1123. opts.keepAliveInterval = 2;
  1124. opts.cleanstart = 1;
  1125. opts.MQTTVersion = options.MQTTVersion;
  1126. opts.will = &wopts;
  1127. opts.will->payload.data = test6_will_message;
  1128. opts.will->payload.len = (int)strlen(test6_will_message) + 1;
  1129. opts.will->qos = 1;
  1130. opts.will->retained = 0;
  1131. opts.will->topicName = test6_will_topic;
  1132. if (options.haconnections != NULL)
  1133. {
  1134. opts.serverURIs = options.haconnections;
  1135. opts.serverURIcount = options.hacount;
  1136. }
  1137. /* Client-1 with Will options */
  1138. createOpts.MQTTVersion = MQTTVERSION_5;
  1139. rc = MQTTClient_createWithOptions(&test6_c1, options.proxy_connection, "Client_1", MQTTCLIENT_PERSISTENCE_DEFAULT,
  1140. NULL, &createOpts);
  1141. assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  1142. if (rc != MQTTCLIENT_SUCCESS)
  1143. goto exit;
  1144. rc = MQTTClient_setCallbacks(test6_c1, (void*)test6_c1, test6_connectionLost, test6_messageArrived, test6_deliveryComplete);
  1145. assert("good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  1146. if (rc != MQTTCLIENT_SUCCESS)
  1147. goto exit;
  1148. /* Connect to the broker */
  1149. response = MQTTClient_connect5(test6_c1, &opts, NULL, NULL);
  1150. assert("good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS,
  1151. "rc was %d\n", response.reasonCode);
  1152. MQTTResponse_free(response);
  1153. if (response.reasonCode != MQTTCLIENT_SUCCESS)
  1154. goto exit;
  1155. /* Client - 2 (multi-threaded) */
  1156. rc = MQTTClient_createWithOptions(&test6_c2, options.connection, "Client_2", MQTTCLIENT_PERSISTENCE_DEFAULT,
  1157. NULL, &createOpts);
  1158. assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  1159. /* Set the callback functions for the client */
  1160. rc = MQTTClient_setCallbacks(test6_c2, (void*)test6_c2, test6_connectionLost, test6_messageArrived, test6_deliveryComplete);
  1161. assert("good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  1162. /* Connect to the broker */
  1163. opts2.keepAliveInterval = 20;
  1164. opts2.cleanstart = 1;
  1165. MyLog(LOGA_INFO, "Connecting Client_2 ...");
  1166. response = MQTTClient_connect5(test6_c2, &opts2, NULL, NULL);
  1167. MQTTResponse_free(response);
  1168. assert("Good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d\n", response.reasonCode);
  1169. response = MQTTClient_subscribe5(test6_c2, test6_will_topic, 2, NULL, NULL);
  1170. assert("Good rc from subscribe", response.reasonCode == MQTTREASONCODE_GRANTED_QOS_2, "rc was %d\n", response.reasonCode);
  1171. /* now send the command which will break the connection and cause the will message to be sent */
  1172. response = MQTTClient_publish5(test6_c1, mqttsas_topic, (int)strlen("TERMINATE"), "TERMINATE", 0, 0, NULL, NULL);
  1173. assert("Good rc from publish", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d\n", response.reasonCode);
  1174. MyLog(LOGA_INFO, "Waiting to receive the will message");
  1175. count = 0;
  1176. while (++count < 40)
  1177. {
  1178. #if defined(_WIN32)
  1179. Sleep(1000L);
  1180. #else
  1181. sleep(1);
  1182. #endif
  1183. if (test6_will_message_arrived == 1 && test6_connection_lost_called == 1)
  1184. break;
  1185. }
  1186. assert("will message arrived", test6_will_message_arrived == 1,
  1187. "will_message_arrived was %d\n", test6_will_message_arrived);
  1188. assert("connection lost called", test6_connection_lost_called == 1,
  1189. "connection_lost_called %d\n", test6_connection_lost_called);
  1190. response = MQTTClient_unsubscribe5(test6_c2, test6_will_topic, NULL);
  1191. assert("Good rc from unsubscribe", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
  1192. rc = MQTTClient_isConnected(test6_c2);
  1193. assert("Client-2 still connected", rc == 1, "isconnected is %d", rc);
  1194. rc = MQTTClient_isConnected(test6_c1);
  1195. assert("Client-1 not connected", rc == 0, "isconnected is %d", rc);
  1196. rc = MQTTClient_disconnect5(test6_c2, 100L, MQTTREASONCODE_SUCCESS, NULL);
  1197. assert("Good rc from disconnect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  1198. MQTTClient_destroy(&test6_c1);
  1199. MQTTClient_destroy(&test6_c2);
  1200. exit:
  1201. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.\n",
  1202. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  1203. write_test_result();
  1204. return failures;
  1205. }
  1206. int main(int argc, char** argv)
  1207. {
  1208. int rc = 0;
  1209. int (*tests[])() = {NULL, test1, test2, test3, test4, test5, test6, test6a};
  1210. int i;
  1211. xml = fopen("TEST-test1.xml", "w");
  1212. fprintf(xml, "<testsuite name=\"test1\" tests=\"%d\">\n", (int)(ARRAY_SIZE(tests) - 1));
  1213. setenv("MQTT_C_CLIENT_TRACE", "ON", 1);
  1214. setenv("MQTT_C_CLIENT_TRACE_LEVEL", "ERROR", 1);
  1215. getopts(argc, argv);
  1216. for (i = 0; i < options.iterations; ++i)
  1217. {
  1218. if (options.test_no == 0)
  1219. { /* run all the tests */
  1220. for (options.test_no = 1; options.test_no < ARRAY_SIZE(tests); ++options.test_no)
  1221. rc += tests[options.test_no](options); /* return number of failures. 0 = test succeeded */
  1222. }
  1223. else
  1224. rc = tests[options.test_no](options); /* run just the selected test */
  1225. }
  1226. if (rc == 0)
  1227. MyLog(LOGA_INFO, "verdict pass");
  1228. else
  1229. MyLog(LOGA_INFO, "verdict fail");
  1230. fprintf(xml, "</testsuite>\n");
  1231. fclose(xml);
  1232. return rc;
  1233. }