test1.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2018 IBM Corp.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v2.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * https://www.eclipse.org/legal/epl-2.0/
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial API and implementation and/or initial documentation
  15. * Ian Craggs - MQTT 3.1.1 support
  16. * Ian Craggs - 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://mqtt.eclipse.org:1883",
  53. NULL,
  54. "tcp://localhost:1883",
  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 / 1000);
  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. MQTTClient_yield(); /* allow any unfinished protocol exchanges to finish */
  690. rc = MQTTClient_getPendingDeliveryTokens(c, &tokens);
  691. assert("getPendingDeliveryTokens rc == 0", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  692. assert("should get no tokens back", tokens == NULL, "tokens was %p", tokens);
  693. MQTTClient_disconnect(c, 0);
  694. MQTTClient_destroy(&c);
  695. /* TODO - unused -remove? exit: */
  696. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  697. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  698. return failures;
  699. }
  700. int test4(struct Options options)
  701. {
  702. int rc = 0;
  703. fprintf(xml, "<testcase classname=\"test1\" name=\"persistence\"");
  704. global_start_time = start_clock();
  705. rc = test4_run(1) + test4_run(2);
  706. fprintf(xml, " time=\"%ld\" >\n", elapsed(global_start_time) / 1000);
  707. if (cur_output != output)
  708. {
  709. fprintf(xml, "%s", output);
  710. cur_output = output;
  711. }
  712. fprintf(xml, "</testcase>\n");
  713. return rc;
  714. }
  715. /*********************************************************************
  716. Test 5: disconnect with quiesce timeout should allow exchanges to complete
  717. *********************************************************************/
  718. int test5(struct Options options)
  719. {
  720. char* testname = "test 5";
  721. char* topic = "Persistence test 2";
  722. int subsqos = 2;
  723. MQTTClient c;
  724. MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer;
  725. MQTTClient_deliveryToken* tokens = NULL;
  726. char buffer[100];
  727. int count = 5;
  728. int i, rc;
  729. fprintf(xml, "<testcase classname=\"test1\" name=\"disconnect with quiesce timeout should allow exchanges to complete\"");
  730. global_start_time = start_clock();
  731. failures = 0;
  732. MyLog(LOGA_INFO, "Starting test 5 - disconnect with quiesce timeout should allow exchanges to complete");
  733. MQTTClient_create(&c, options.connection, "xrctest1_test_5", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  734. opts.keepAliveInterval = 20;
  735. opts.cleansession = 0;
  736. opts.reliable = 0;
  737. opts.MQTTVersion = options.MQTTVersion;
  738. if (options.haconnections != NULL)
  739. {
  740. opts.serverURIs = options.haconnections;
  741. opts.serverURIcount = options.hacount;
  742. }
  743. MyLog(LOGA_DEBUG, "Connecting");
  744. if ((rc = MQTTClient_connect(c, &opts)) != 0)
  745. {
  746. assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  747. MQTTClient_destroy(&c);
  748. goto exit;
  749. }
  750. rc = MQTTClient_subscribe(c, topic, subsqos);
  751. assert("Good rc from subscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  752. for (i = 0; i < count; ++i)
  753. {
  754. sprintf(buffer, "Message sequence no %d", i);
  755. rc = MQTTClient_publish(c, topic, 10, buffer, 1, 0, NULL);
  756. assert("Good rc from publish", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  757. }
  758. MQTTClient_disconnect(c, 1000); /* now there should be no "orphaned" publications */
  759. MyLog(LOGA_DEBUG, "Disconnected");
  760. rc = MQTTClient_getPendingDeliveryTokens(c, &tokens);
  761. assert("getPendingDeliveryTokens rc == 0", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  762. assert("should get no tokens back", tokens == NULL, "tokens was %p", tokens);
  763. MQTTClient_destroy(&c);
  764. exit:
  765. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  766. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  767. write_test_result();
  768. return failures;
  769. }
  770. /*********************************************************************
  771. Test 6: connectionLost and will message
  772. *********************************************************************/
  773. MQTTClient test6_c1, test6_c2;
  774. volatile int test6_will_message_arrived = 0;
  775. volatile int test6_connection_lost_called = 0;
  776. void test6_connectionLost(void* context, char* cause)
  777. {
  778. MQTTClient c = (MQTTClient)context;
  779. printf("%s -> Callback: connection lost\n", (c == test6_c1) ? "Client-1" : "Client-2");
  780. test6_connection_lost_called = 1;
  781. }
  782. void test6_deliveryComplete(void* context, MQTTClient_deliveryToken token)
  783. {
  784. printf("Client-2 -> Callback: publish complete for token %d\n", token);
  785. }
  786. char* test6_will_topic = "C Test 2: will topic";
  787. char* test6_will_message = "will message from Client-1";
  788. int test6_messageArrived(void* context, char* topicName, int topicLen, MQTTClient_message* m)
  789. {
  790. MQTTClient c = (MQTTClient)context;
  791. printf("%s -> Callback: message received on topic '%s' is '%.*s'.\n",
  792. (c == test6_c1) ? "Client-1" : "Client-2", topicName, m->payloadlen, (char*)(m->payload));
  793. if (c == test6_c2 && strcmp(topicName, test6_will_topic) == 0 && memcmp(m->payload, test6_will_message, m->payloadlen) == 0)
  794. test6_will_message_arrived = 1;
  795. MQTTClient_free(topicName);
  796. MQTTClient_freeMessage(&m);
  797. return 1;
  798. }
  799. int test6(struct Options options)
  800. {
  801. char* testname = "test6";
  802. MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer;
  803. MQTTClient_willOptions wopts = MQTTClient_willOptions_initializer;
  804. MQTTClient_connectOptions opts2 = MQTTClient_connectOptions_initializer;
  805. int rc, count;
  806. char* mqttsas_topic = "MQTTSAS topic";
  807. failures = 0;
  808. MyLog(LOGA_INFO, "Starting test 6 - connectionLost and will messages");
  809. fprintf(xml, "<testcase classname=\"test1\" name=\"connectionLost and will messages\"");
  810. global_start_time = start_clock();
  811. opts.keepAliveInterval = 2;
  812. opts.cleansession = 1;
  813. opts.MQTTVersion = MQTTVERSION_3_1_1;
  814. opts.will = &wopts;
  815. opts.will->message = test6_will_message;
  816. opts.will->qos = 1;
  817. opts.will->retained = 0;
  818. opts.will->topicName = test6_will_topic;
  819. if (options.haconnections != NULL)
  820. {
  821. opts.serverURIs = options.haconnections;
  822. opts.serverURIcount = options.hacount;
  823. }
  824. /* Client-1 with Will options */
  825. rc = MQTTClient_create(&test6_c1, options.proxy_connection, "Client_1", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  826. assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  827. if (rc != MQTTCLIENT_SUCCESS)
  828. goto exit;
  829. rc = MQTTClient_setCallbacks(test6_c1, (void*)test6_c1, test6_connectionLost, test6_messageArrived, test6_deliveryComplete);
  830. assert("good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  831. if (rc != MQTTCLIENT_SUCCESS)
  832. goto exit;
  833. /* Connect to the broker */
  834. rc = MQTTClient_connect(test6_c1, &opts);
  835. assert("good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  836. if (rc != MQTTCLIENT_SUCCESS)
  837. goto exit;
  838. /* Client - 2 (multi-threaded) */
  839. rc = MQTTClient_create(&test6_c2, options.connection, "Client_2", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  840. assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  841. /* Set the callback functions for the client */
  842. rc = MQTTClient_setCallbacks(test6_c2, (void*)test6_c2, test6_connectionLost, test6_messageArrived, test6_deliveryComplete);
  843. assert("good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  844. /* Connect to the broker */
  845. opts2.keepAliveInterval = 20;
  846. opts2.cleansession = 1;
  847. MyLog(LOGA_INFO, "Connecting Client_2 ...");
  848. rc = MQTTClient_connect(test6_c2, &opts2);
  849. assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  850. rc = MQTTClient_subscribe(test6_c2, test6_will_topic, 2);
  851. assert("Good rc from subscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  852. /* now send the command which will break the connection and cause the will message to be sent */
  853. rc = MQTTClient_publish(test6_c1, mqttsas_topic, (int)strlen("TERMINATE"), "TERMINATE", 0, 0, NULL);
  854. assert("Good rc from publish", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  855. MyLog(LOGA_INFO, "Waiting to receive the will message");
  856. count = 0;
  857. while (++count < 40)
  858. {
  859. #if defined(_WIN32)
  860. Sleep(1000L);
  861. #else
  862. sleep(1);
  863. #endif
  864. if (test6_will_message_arrived == 1 && test6_connection_lost_called == 1)
  865. break;
  866. }
  867. assert("will message arrived", test6_will_message_arrived == 1,
  868. "will_message_arrived was %d\n", test6_will_message_arrived);
  869. assert("connection lost called", test6_connection_lost_called == 1,
  870. "connection_lost_called %d\n", test6_connection_lost_called);
  871. rc = MQTTClient_unsubscribe(test6_c2, test6_will_topic);
  872. assert("Good rc from unsubscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  873. rc = MQTTClient_isConnected(test6_c2);
  874. assert("Client-2 still connected", rc == 1, "isconnected is %d", rc);
  875. rc = MQTTClient_isConnected(test6_c1);
  876. assert("Client-1 not connected", rc == 0, "isconnected is %d", rc);
  877. rc = MQTTClient_disconnect(test6_c2, 100L);
  878. assert("Good rc from disconnect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  879. MQTTClient_destroy(&test6_c1);
  880. MQTTClient_destroy(&test6_c2);
  881. exit:
  882. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.\n",
  883. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  884. write_test_result();
  885. return failures;
  886. }
  887. int test6a(struct Options options)
  888. {
  889. char* testname = "test6a";
  890. MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer;
  891. MQTTClient_willOptions wopts = MQTTClient_willOptions_initializer;
  892. MQTTClient_connectOptions opts2 = MQTTClient_connectOptions_initializer;
  893. int rc, count;
  894. char* mqttsas_topic = "MQTTSAS topic";
  895. failures = 0;
  896. MyLog(LOGA_INFO, "Starting test 6 - connectionLost and binary will messages");
  897. fprintf(xml, "<testcase classname=\"test1\" name=\"connectionLost and binary will messages\"");
  898. global_start_time = start_clock();
  899. opts.keepAliveInterval = 2;
  900. opts.cleansession = 1;
  901. opts.MQTTVersion = MQTTVERSION_3_1_1;
  902. opts.will = &wopts;
  903. opts.will->payload.data = test6_will_message;
  904. opts.will->payload.len = (int)strlen(test6_will_message) + 1;
  905. opts.will->qos = 1;
  906. opts.will->retained = 0;
  907. opts.will->topicName = test6_will_topic;
  908. if (options.haconnections != NULL)
  909. {
  910. opts.serverURIs = options.haconnections;
  911. opts.serverURIcount = options.hacount;
  912. }
  913. /* Client-1 with Will options */
  914. rc = MQTTClient_create(&test6_c1, options.proxy_connection, "Client_1", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  915. assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  916. if (rc != MQTTCLIENT_SUCCESS)
  917. goto exit;
  918. rc = MQTTClient_setCallbacks(test6_c1, (void*)test6_c1, test6_connectionLost, test6_messageArrived, test6_deliveryComplete);
  919. assert("good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  920. if (rc != MQTTCLIENT_SUCCESS)
  921. goto exit;
  922. /* Connect to the broker */
  923. rc = MQTTClient_connect(test6_c1, &opts);
  924. assert("good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  925. if (rc != MQTTCLIENT_SUCCESS)
  926. goto exit;
  927. /* Client - 2 (multi-threaded) */
  928. rc = MQTTClient_create(&test6_c2, options.connection, "Client_2", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  929. assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  930. /* Set the callback functions for the client */
  931. rc = MQTTClient_setCallbacks(test6_c2, (void*)test6_c2, test6_connectionLost, test6_messageArrived, test6_deliveryComplete);
  932. assert("good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  933. /* Connect to the broker */
  934. opts2.keepAliveInterval = 20;
  935. opts2.cleansession = 1;
  936. MyLog(LOGA_INFO, "Connecting Client_2 ...");
  937. rc = MQTTClient_connect(test6_c2, &opts2);
  938. assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  939. rc = MQTTClient_subscribe(test6_c2, test6_will_topic, 2);
  940. assert("Good rc from subscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  941. /* now send the command which will break the connection and cause the will message to be sent */
  942. rc = MQTTClient_publish(test6_c1, mqttsas_topic, (int)strlen("TERMINATE"), "TERMINATE", 0, 0, NULL);
  943. assert("Good rc from publish", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  944. MyLog(LOGA_INFO, "Waiting to receive the will message");
  945. count = 0;
  946. while (++count < 40)
  947. {
  948. #if defined(_WIN32)
  949. Sleep(1000L);
  950. #else
  951. sleep(1);
  952. #endif
  953. if (test6_will_message_arrived == 1 && test6_connection_lost_called == 1)
  954. break;
  955. }
  956. assert("will message arrived", test6_will_message_arrived == 1,
  957. "will_message_arrived was %d\n", test6_will_message_arrived);
  958. assert("connection lost called", test6_connection_lost_called == 1,
  959. "connection_lost_called %d\n", test6_connection_lost_called);
  960. rc = MQTTClient_unsubscribe(test6_c2, test6_will_topic);
  961. assert("Good rc from unsubscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  962. rc = MQTTClient_isConnected(test6_c2);
  963. assert("Client-2 still connected", rc == 1, "isconnected is %d", rc);
  964. rc = MQTTClient_isConnected(test6_c1);
  965. assert("Client-1 not connected", rc == 0, "isconnected is %d", rc);
  966. rc = MQTTClient_disconnect(test6_c2, 100L);
  967. assert("Good rc from disconnect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  968. MQTTClient_destroy(&test6_c1);
  969. MQTTClient_destroy(&test6_c2);
  970. exit:
  971. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.\n",
  972. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  973. write_test_result();
  974. return failures;
  975. }
  976. int main(int argc, char** argv)
  977. {
  978. int rc = 0;
  979. int (*tests[])() = {NULL, test1, test2, test3, test4, test5, test6, test6a};
  980. int i;
  981. xml = fopen("TEST-test1.xml", "w");
  982. fprintf(xml, "<testsuite name=\"test1\" tests=\"%d\">\n", (int)(ARRAY_SIZE(tests) - 1));
  983. setenv("MQTT_C_CLIENT_TRACE", "ON", 1);
  984. setenv("MQTT_C_CLIENT_TRACE_LEVEL", "ERROR", 1);
  985. getopts(argc, argv);
  986. for (i = 0; i < options.iterations; ++i)
  987. {
  988. if (options.test_no == 0)
  989. { /* run all the tests */
  990. for (options.test_no = 1; options.test_no < ARRAY_SIZE(tests); ++options.test_no)
  991. rc += tests[options.test_no](options); /* return number of failures. 0 = test succeeded */
  992. }
  993. else
  994. rc = tests[options.test_no](options); /* run just the selected test */
  995. }
  996. if (rc == 0)
  997. MyLog(LOGA_INFO, "verdict pass");
  998. else
  999. MyLog(LOGA_INFO, "verdict fail");
  1000. fprintf(xml, "</testsuite>\n");
  1001. fclose(xml);
  1002. return rc;
  1003. }