test1.c 36 KB

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