test4.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802
  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 - test8 - failure callbacks
  17. *******************************************************************************/
  18. /**
  19. * @file
  20. * Tests for the Paho Asynchronous MQTT C client
  21. */
  22. #include "MQTTAsync.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. #endif
  33. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  34. void usage(void)
  35. {
  36. printf("help!!\n");
  37. exit(EXIT_FAILURE);
  38. }
  39. struct Options
  40. {
  41. char* connection; /**< connection to system under test. */
  42. int verbose;
  43. int test_no;
  44. int size; /**< size of big message */
  45. int MQTTVersion;
  46. int iterations;
  47. } options =
  48. {
  49. "localhost:1883",
  50. 0,
  51. -1,
  52. 10000,
  53. MQTTVERSION_DEFAULT,
  54. 1,
  55. };
  56. void getopts(int argc, char** argv)
  57. {
  58. int count = 1;
  59. while (count < argc)
  60. {
  61. if (strcmp(argv[count], "--test_no") == 0)
  62. {
  63. if (++count < argc)
  64. options.test_no = atoi(argv[count]);
  65. else
  66. usage();
  67. }
  68. else if (strcmp(argv[count], "--size") == 0)
  69. {
  70. if (++count < argc)
  71. options.size = atoi(argv[count]);
  72. else
  73. usage();
  74. }
  75. else if (strcmp(argv[count], "--connection") == 0)
  76. {
  77. if (++count < argc)
  78. options.connection = argv[count];
  79. else
  80. usage();
  81. }
  82. else if (strcmp(argv[count], "--MQTTversion") == 0)
  83. {
  84. if (++count < argc)
  85. {
  86. options.MQTTVersion = atoi(argv[count]);
  87. printf("setting MQTT version to %d\n", options.MQTTVersion);
  88. }
  89. else
  90. usage();
  91. }
  92. else if (strcmp(argv[count], "--iterations") == 0)
  93. {
  94. if (++count < argc)
  95. options.iterations = atoi(argv[count]);
  96. else
  97. usage();
  98. }
  99. else if (strcmp(argv[count], "--verbose") == 0)
  100. options.verbose = 1;
  101. count++;
  102. }
  103. }
  104. #define LOGA_DEBUG 0
  105. #define LOGA_INFO 1
  106. #include <stdarg.h>
  107. #include <time.h>
  108. #include <sys/timeb.h>
  109. void MyLog(int LOGA_level, char* format, ...)
  110. {
  111. static char msg_buf[256];
  112. va_list args;
  113. #if defined(_WIN32) || defined(_WINDOWS)
  114. struct timeb ts;
  115. #else
  116. struct timeval ts;
  117. #endif
  118. struct tm timeinfo;
  119. if (LOGA_level == LOGA_DEBUG && options.verbose == 0)
  120. return;
  121. #if defined(_WIN32) || defined(_WINDOWS)
  122. ftime(&ts);
  123. localtime_s(&timeinfo, &ts.time);
  124. #else
  125. gettimeofday(&ts, NULL);
  126. localtime_r(&ts.tv_sec, &timeinfo);
  127. #endif
  128. strftime(msg_buf, 80, "%Y%m%d %H%M%S", &timeinfo);
  129. #if defined(_WIN32) || defined(_WINDOWS)
  130. sprintf(&msg_buf[strlen(msg_buf)], ".%.3hu ", ts.millitm);
  131. #else
  132. sprintf(&msg_buf[strlen(msg_buf)], ".%.3lu ", ts.tv_usec / 1000L);
  133. #endif
  134. va_start(args, format);
  135. vsnprintf(&msg_buf[strlen(msg_buf)], sizeof(msg_buf) - strlen(msg_buf), format, args);
  136. va_end(args);
  137. printf("%s\n", msg_buf);
  138. fflush(stdout);
  139. }
  140. #if defined(_WIN32) || defined(_WINDOWS)
  141. #define mqsleep(A) Sleep(1000*A)
  142. #define START_TIME_TYPE DWORD
  143. static DWORD start_time = 0;
  144. START_TIME_TYPE start_clock(void)
  145. {
  146. return GetTickCount();
  147. }
  148. #elif defined(AIX)
  149. #define mqsleep sleep
  150. #define START_TIME_TYPE struct timespec
  151. START_TIME_TYPE start_clock(void)
  152. {
  153. static struct timespec start;
  154. clock_gettime(CLOCK_REALTIME, &start);
  155. return start;
  156. }
  157. #else
  158. #define mqsleep sleep
  159. #define START_TIME_TYPE struct timeval
  160. /* TODO - unused - remove? static struct timeval start_time; */
  161. START_TIME_TYPE start_clock(void)
  162. {
  163. struct timeval start_time;
  164. gettimeofday(&start_time, NULL);
  165. return start_time;
  166. }
  167. #endif
  168. #if defined(_WIN32)
  169. long elapsed(START_TIME_TYPE start_time)
  170. {
  171. return GetTickCount() - start_time;
  172. }
  173. #elif defined(AIX)
  174. #define assert(a)
  175. long elapsed(struct timespec start)
  176. {
  177. struct timespec now, res;
  178. clock_gettime(CLOCK_REALTIME, &now);
  179. ntimersub(now, start, res);
  180. return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L;
  181. }
  182. #else
  183. long elapsed(START_TIME_TYPE start_time)
  184. {
  185. struct timeval now, res;
  186. gettimeofday(&now, NULL);
  187. timersub(&now, &start_time, &res);
  188. return (res.tv_sec)*1000 + (res.tv_usec)/1000;
  189. }
  190. #endif
  191. #define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d)
  192. #define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e)
  193. int tests = 0;
  194. int failures = 0;
  195. FILE* xml;
  196. START_TIME_TYPE global_start_time;
  197. char output[3000];
  198. char* cur_output = output;
  199. void write_test_result(void)
  200. {
  201. long duration = elapsed(global_start_time);
  202. fprintf(xml, " time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000);
  203. if (cur_output != output)
  204. {
  205. fprintf(xml, "%s", output);
  206. cur_output = output;
  207. }
  208. fprintf(xml, "</testcase>\n");
  209. }
  210. void myassert(char* filename, int lineno, char* description, int value, char* format, ...)
  211. {
  212. ++tests;
  213. if (!value)
  214. {
  215. va_list args;
  216. ++failures;
  217. printf("Assertion failed, file %s, line %d, description: %s\n", filename, lineno, description);
  218. va_start(args, format);
  219. vprintf(format, args);
  220. va_end(args);
  221. cur_output += sprintf(cur_output, "<failure type=\"%s\">file %s, line %d </failure>\n",
  222. description, filename, lineno);
  223. }
  224. else
  225. MyLog(LOGA_DEBUG, "Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description);
  226. }
  227. volatile int test_finished = 0;
  228. char* test_topic = "async test topic";
  229. void test1_onDisconnect(void* context, MQTTAsync_successData* response)
  230. {
  231. MQTTAsync c = (MQTTAsync)context;
  232. MyLog(LOGA_DEBUG, "In onDisconnect callback %p", c);
  233. test_finished = 1;
  234. }
  235. void test1_onUnsubscribe(void* context, MQTTAsync_successData* response)
  236. {
  237. MQTTAsync c = (MQTTAsync)context;
  238. MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
  239. int rc;
  240. MyLog(LOGA_DEBUG, "In onUnsubscribe onSuccess callback %p", c);
  241. opts.onSuccess = test1_onDisconnect;
  242. opts.context = c;
  243. opts.timeout = 1000;
  244. rc = MQTTAsync_disconnect(c, &opts);
  245. assert("Disconnect successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  246. }
  247. int test1_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message)
  248. {
  249. MQTTAsync c = (MQTTAsync)context;
  250. static int message_count = 0;
  251. int rc;
  252. MyLog(LOGA_DEBUG, "In messageArrived callback %p", c);
  253. if (++message_count == 1)
  254. {
  255. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  256. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  257. pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  258. pubmsg.payloadlen = 11;
  259. pubmsg.qos = 2;
  260. pubmsg.retained = 0;
  261. rc = MQTTAsync_sendMessage(c, test_topic, &pubmsg, &opts);
  262. assert("Good rc from send", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  263. }
  264. else
  265. {
  266. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  267. opts.onSuccess = test1_onUnsubscribe;
  268. opts.context = c;
  269. rc = MQTTAsync_unsubscribe(c, test_topic, &opts);
  270. assert("Unsubscribe successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  271. }
  272. MQTTAsync_freeMessage(&message);
  273. MQTTAsync_free(topicName);
  274. return 1;
  275. }
  276. void test1_onSubscribe(void* context, MQTTAsync_successData* response)
  277. {
  278. MQTTAsync c = (MQTTAsync)context;
  279. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  280. int rc;
  281. MyLog(LOGA_DEBUG, "In subscribe onSuccess callback %p granted qos %d", c, response->alt.qos);
  282. pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  283. pubmsg.payloadlen = 11;
  284. pubmsg.qos = 2;
  285. pubmsg.retained = 0;
  286. rc = MQTTAsync_send(c, test_topic, pubmsg.payloadlen, pubmsg.payload, pubmsg.qos, pubmsg.retained, NULL);
  287. assert("Good rc from send", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  288. }
  289. void test1_onConnect(void* context, MQTTAsync_successData* response)
  290. {
  291. MQTTAsync c = (MQTTAsync)context;
  292. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  293. int rc;
  294. MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
  295. opts.onSuccess = test1_onSubscribe;
  296. opts.context = c;
  297. rc = MQTTAsync_subscribe(c, test_topic, 2, &opts);
  298. assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  299. if (rc != MQTTASYNC_SUCCESS)
  300. test_finished = 1;
  301. }
  302. /*********************************************************************
  303. Test1: Basic connect, subscribe send and receive.
  304. *********************************************************************/
  305. int test1(struct Options options)
  306. {
  307. int subsqos = 2;
  308. MQTTAsync c;
  309. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  310. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  311. int rc = 0;
  312. char* test_topic = "C client test1";
  313. MyLog(LOGA_INFO, "Starting test 1 - asynchronous connect");
  314. fprintf(xml, "<testcase classname=\"test4\" name=\"asynchronous connect\"");
  315. global_start_time = start_clock();
  316. rc = MQTTAsync_create(&c, options.connection, "async_test",
  317. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  318. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  319. if (rc != MQTTASYNC_SUCCESS)
  320. {
  321. MQTTAsync_destroy(&c);
  322. goto exit;
  323. }
  324. rc = MQTTAsync_setCallbacks(c, c, NULL, test1_messageArrived, NULL);
  325. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  326. opts.keepAliveInterval = 20;
  327. opts.cleansession = 1;
  328. opts.username = "testuser";
  329. opts.password = "testpassword";
  330. opts.MQTTVersion = options.MQTTVersion;
  331. opts.will = &wopts;
  332. opts.will->message = "will message";
  333. opts.will->qos = 1;
  334. opts.will->retained = 0;
  335. opts.will->topicName = "will topic";
  336. opts.will = NULL;
  337. opts.onSuccess = test1_onConnect;
  338. opts.onFailure = NULL;
  339. opts.context = c;
  340. MyLog(LOGA_DEBUG, "Connecting");
  341. rc = MQTTAsync_connect(c, &opts);
  342. rc = 0;
  343. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  344. if (rc != MQTTASYNC_SUCCESS)
  345. goto exit;
  346. while (!test_finished)
  347. #if defined(_WIN32)
  348. Sleep(100);
  349. #else
  350. usleep(10000L);
  351. #endif
  352. MQTTAsync_destroy(&c);
  353. exit:
  354. MyLog(LOGA_INFO, "TEST1: test %s. %d tests run, %d failures.",
  355. (failures == 0) ? "passed" : "failed", tests, failures);
  356. write_test_result();
  357. return failures;
  358. }
  359. int test2_onFailure_called = 0;
  360. void test2_onFailure(void* context, MQTTAsync_failureData* response)
  361. {
  362. MyLog(LOGA_DEBUG, "In connect onFailure callback, context %p", context);
  363. test2_onFailure_called++;
  364. test_finished = 1;
  365. }
  366. void test2_onConnect(void* context, MQTTAsync_successData* response)
  367. {
  368. MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p\n", context);
  369. assert("Connect should not succeed", 0, "connect success callback was called", 0);
  370. test_finished = 1;
  371. }
  372. /*********************************************************************
  373. Test2: connect timeout
  374. *********************************************************************/
  375. int test2(struct Options options)
  376. {
  377. int subsqos = 2;
  378. MQTTAsync c;
  379. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  380. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  381. int rc = 0;
  382. char* test_topic = "C client test2";
  383. test_finished = 0;
  384. MyLog(LOGA_INFO, "Starting test 2 - connect timeout");
  385. fprintf(xml, "<testcase classname=\"test4\" name=\"connect timeout\"");
  386. global_start_time = start_clock();
  387. rc = MQTTAsync_create(&c, "tcp://9.20.96.160:66", "connect timeout",
  388. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  389. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  390. if (rc != MQTTASYNC_SUCCESS)
  391. {
  392. MQTTAsync_destroy(&c);
  393. goto exit;
  394. }
  395. rc = MQTTAsync_setCallbacks(c, c, NULL, test1_messageArrived, NULL);
  396. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  397. opts.connectTimeout = 5;
  398. opts.keepAliveInterval = 20;
  399. opts.cleansession = 1;
  400. opts.username = "testuser";
  401. opts.binarypwd.data = "testpassword";
  402. opts.binarypwd.len = (int)strlen(opts.binarypwd.data);
  403. opts.MQTTVersion = options.MQTTVersion;
  404. opts.will = &wopts;
  405. opts.will->message = "will message";
  406. opts.will->qos = 1;
  407. opts.will->retained = 0;
  408. opts.will->topicName = "will topic";
  409. opts.will = NULL;
  410. opts.onSuccess = test2_onConnect;
  411. opts.onFailure = test2_onFailure;
  412. opts.context = c;
  413. MyLog(LOGA_DEBUG, "Connecting");
  414. rc = MQTTAsync_connect(c, &opts);
  415. rc = 0;
  416. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  417. if (rc != MQTTASYNC_SUCCESS)
  418. goto exit;
  419. while (!test_finished)
  420. #if defined(_WIN32)
  421. Sleep(100);
  422. #else
  423. usleep(10000L);
  424. #endif
  425. MQTTAsync_destroy(&c);
  426. exit:
  427. assert("Connect onFailure should be called once", test2_onFailure_called == 1,
  428. "connect onFailure was called %d times", test2_onFailure_called);
  429. MyLog(LOGA_INFO, "TEST2: test %s. %d tests run, %d failures.",
  430. (failures == 0) ? "passed" : "failed", tests, failures);
  431. write_test_result();
  432. return failures;
  433. }
  434. typedef struct
  435. {
  436. MQTTAsync c;
  437. int index;
  438. char clientid[24];
  439. char test_topic[100];
  440. int message_count;
  441. } client_data;
  442. void test3_onDisconnect(void* context, MQTTAsync_successData* response)
  443. {
  444. client_data* cd = (client_data*)context;
  445. MyLog(LOGA_DEBUG, "In onDisconnect callback for client \"%s\"", cd->clientid);
  446. test_finished++;
  447. }
  448. void test3_onPublish(void* context, MQTTAsync_successData* response)
  449. {
  450. client_data* cd = (client_data*)context;
  451. MyLog(LOGA_DEBUG, "In QoS 0 onPublish callback for client \"%s\"", cd->clientid);
  452. }
  453. void test3_onUnsubscribe(void* context, MQTTAsync_successData* response)
  454. {
  455. client_data* cd = (client_data*)context;
  456. MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
  457. int rc;
  458. MyLog(LOGA_DEBUG, "In onUnsubscribe onSuccess callback \"%s\"", cd->clientid);
  459. opts.onSuccess = test3_onDisconnect;
  460. opts.context = cd;
  461. opts.timeout = 1000;
  462. rc = MQTTAsync_disconnect(cd->c, &opts);
  463. assert("Disconnect successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  464. }
  465. int test3_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message)
  466. {
  467. client_data* cd = (client_data*)context;
  468. int rc;
  469. MyLog(LOGA_DEBUG, "In messageArrived callback \"%s\" message count ", cd->clientid);
  470. if (++cd->message_count == 1)
  471. {
  472. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  473. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  474. pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  475. pubmsg.payloadlen = 25;
  476. pubmsg.qos = 1;
  477. pubmsg.retained = 0;
  478. rc = MQTTAsync_sendMessage(cd->c, cd->test_topic, &pubmsg, &opts);
  479. assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  480. }
  481. else if (cd->message_count == 2)
  482. {
  483. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  484. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  485. pubmsg.payload = "a QoS 0 message that we can shorten to the extent that we need to payload up to 11";
  486. pubmsg.payloadlen = 29;
  487. pubmsg.qos = 0;
  488. pubmsg.retained = 0;
  489. opts.context = cd;
  490. opts.onSuccess = test3_onPublish;
  491. rc = MQTTAsync_sendMessage(cd->c, cd->test_topic, &pubmsg, &opts);
  492. assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  493. }
  494. else
  495. {
  496. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  497. opts.onSuccess = test3_onUnsubscribe;
  498. opts.context = cd;
  499. rc = MQTTAsync_unsubscribe(cd->c, cd->test_topic, &opts);
  500. assert("Unsubscribe successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  501. }
  502. MQTTAsync_freeMessage(&message);
  503. MQTTAsync_free(topicName);
  504. return 1;
  505. }
  506. void test3_onSubscribe(void* context, MQTTAsync_successData* response)
  507. {
  508. client_data* cd = (client_data*)context;
  509. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  510. int rc;
  511. MyLog(LOGA_DEBUG, "In subscribe onSuccess callback \"%s\"", cd->clientid);
  512. pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  513. pubmsg.payloadlen = 11;
  514. pubmsg.qos = 2;
  515. pubmsg.retained = 0;
  516. rc = MQTTAsync_send(cd->c, cd->test_topic, pubmsg.payloadlen, pubmsg.payload, pubmsg.qos, pubmsg.retained, NULL);
  517. assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  518. }
  519. void test3_onConnect(void* context, MQTTAsync_successData* response)
  520. {
  521. client_data* cd = (client_data*)context;
  522. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  523. int rc;
  524. MyLog(LOGA_DEBUG, "In connect onSuccess callback, \"%s\"", cd->clientid);
  525. opts.onSuccess = test3_onSubscribe;
  526. opts.context = cd;
  527. rc = MQTTAsync_subscribe(cd->c, cd->test_topic, 2, &opts);
  528. assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  529. if (rc != MQTTASYNC_SUCCESS)
  530. test_finished++;
  531. }
  532. void test3_onFailure(void* context, MQTTAsync_failureData* response)
  533. {
  534. client_data* cd = (client_data*)context;
  535. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  536. assert("Should have connected", 0, "%s failed to connect\n", cd->clientid);
  537. MyLog(LOGA_DEBUG, "In connect onFailure callback, \"%s\" rc %d\n", cd->clientid, response ? response->code : -999);
  538. if (response && response->message)
  539. MyLog(LOGA_DEBUG, "In connect onFailure callback, \"%s\"\n", response->message);
  540. test_finished++;
  541. }
  542. /*********************************************************************
  543. Test3: More than one client object - simultaneous working.
  544. *********************************************************************/
  545. int test3(struct Options options)
  546. {
  547. #define num_clients 10
  548. int subsqos = 2;
  549. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  550. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  551. int rc = 0;
  552. int i;
  553. client_data clientdata[num_clients];
  554. test_finished = 0;
  555. MyLog(LOGA_INFO, "Starting test 3 - multiple connections");
  556. fprintf(xml, "<testcase classname=\"test4\" name=\"multiple connections\"");
  557. global_start_time = start_clock();
  558. for (i = 0; i < num_clients; ++i)
  559. {
  560. sprintf(clientdata[i].clientid, "async_test3_num_%d", i);
  561. sprintf(clientdata[i].test_topic, "async test3 topic num %d", i);
  562. clientdata[i].index = i;
  563. clientdata[i].message_count = 0;
  564. rc = MQTTAsync_create(&(clientdata[i].c), options.connection, clientdata[i].clientid,
  565. MQTTCLIENT_PERSISTENCE_NONE, NULL);
  566. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  567. rc = MQTTAsync_setCallbacks(clientdata[i].c, &clientdata[i], NULL, test3_messageArrived, NULL);
  568. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  569. opts.keepAliveInterval = 20;
  570. opts.cleansession = 1;
  571. opts.username = "testuser";
  572. opts.password = "testpassword";
  573. opts.MQTTVersion = options.MQTTVersion;
  574. opts.will = &wopts;
  575. opts.will->message = "will message";
  576. opts.will->qos = 1;
  577. opts.will->retained = 0;
  578. opts.will->topicName = "will topic";
  579. opts.onSuccess = test3_onConnect;
  580. opts.onFailure = test3_onFailure;
  581. opts.context = &clientdata[i];
  582. MyLog(LOGA_DEBUG, "Connecting");
  583. rc = MQTTAsync_connect(clientdata[i].c, &opts);
  584. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  585. }
  586. while (test_finished < num_clients)
  587. {
  588. MyLog(LOGA_DEBUG, "num_clients %d test_finished %d\n", num_clients, test_finished);
  589. #if defined(_WIN32)
  590. Sleep(100);
  591. #else
  592. usleep(10000L);
  593. #endif
  594. }
  595. MyLog(LOGA_DEBUG, "TEST3: destroying clients");
  596. for (i = 0; i < num_clients; ++i)
  597. MQTTAsync_destroy(&clientdata[i].c);
  598. //exit:
  599. MyLog(LOGA_INFO, "TEST3: test %s. %d tests run, %d failures.",
  600. (failures == 0) ? "passed" : "failed", tests, failures);
  601. write_test_result();
  602. return failures;
  603. }
  604. void* test4_payload = NULL;
  605. int test4_payloadlen = 0;
  606. void test4_onPublish(void* context, MQTTAsync_successData* response)
  607. {
  608. MQTTAsync c = (MQTTAsync)context;
  609. MyLog(LOGA_DEBUG, "In publish onSuccess callback, context %p", context);
  610. }
  611. int test4_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message)
  612. {
  613. MQTTAsync c = (MQTTAsync)context;
  614. static int message_count = 0;
  615. int rc, i;
  616. MyLog(LOGA_DEBUG, "In messageArrived callback %p", c);
  617. assert("Message size correct", message->payloadlen == test4_payloadlen,
  618. "message size was %d", message->payloadlen);
  619. for (i = 0; i < options.size; ++i)
  620. {
  621. if (((char*)test4_payload)[i] != ((char*)message->payload)[i])
  622. {
  623. assert("Message contents correct", ((char*)test4_payload)[i] != ((char*)message->payload)[i],
  624. "message content was %c", ((char*)message->payload)[i]);
  625. break;
  626. }
  627. }
  628. if (++message_count == 1)
  629. {
  630. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  631. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  632. pubmsg.payload = test4_payload;
  633. pubmsg.payloadlen = test4_payloadlen;
  634. pubmsg.qos = 1;
  635. pubmsg.retained = 0;
  636. opts.onSuccess = test4_onPublish;
  637. opts.context = c;
  638. rc = MQTTAsync_sendMessage(c, test_topic, &pubmsg, &opts);
  639. }
  640. else if (message_count == 2)
  641. {
  642. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  643. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  644. pubmsg.payload = test4_payload;
  645. pubmsg.payloadlen = test4_payloadlen;
  646. pubmsg.qos = 0;
  647. pubmsg.retained = 0;
  648. opts.onSuccess = test4_onPublish;
  649. opts.context = c;
  650. rc = MQTTAsync_sendMessage(c, test_topic, &pubmsg, &opts);
  651. }
  652. else
  653. {
  654. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  655. opts.onSuccess = test1_onUnsubscribe;
  656. opts.context = c;
  657. rc = MQTTAsync_unsubscribe(c, test_topic, &opts);
  658. assert("Unsubscribe successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  659. }
  660. MQTTAsync_freeMessage(&message);
  661. MQTTAsync_free(topicName);
  662. return 1;
  663. }
  664. void test4_onSubscribe(void* context, MQTTAsync_successData* response)
  665. {
  666. MQTTAsync c = (MQTTAsync)context;
  667. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  668. int rc, i;
  669. MyLog(LOGA_DEBUG, "In subscribe onSuccess callback %p", c);
  670. pubmsg.payload = test4_payload = malloc(options.size);
  671. pubmsg.payloadlen = test4_payloadlen = options.size;
  672. srand(33);
  673. for (i = 0; i < options.size; ++i)
  674. ((char*)pubmsg.payload)[i] = rand() % 256;
  675. pubmsg.qos = 2;
  676. pubmsg.retained = 0;
  677. rc = MQTTAsync_send(c, test_topic, pubmsg.payloadlen, pubmsg.payload, pubmsg.qos, pubmsg.retained, NULL);
  678. }
  679. void test4_onConnect(void* context, MQTTAsync_successData* response)
  680. {
  681. MQTTAsync c = (MQTTAsync)context;
  682. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  683. int rc;
  684. MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
  685. opts.onSuccess = test4_onSubscribe;
  686. opts.context = c;
  687. rc = MQTTAsync_subscribe(c, test_topic, 2, &opts);
  688. assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  689. if (rc != MQTTASYNC_SUCCESS)
  690. test_finished = 1;
  691. }
  692. /*********************************************************************
  693. Test4: Send and receive big messages
  694. *********************************************************************/
  695. int test4(struct Options options)
  696. {
  697. int subsqos = 2;
  698. MQTTAsync c;
  699. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  700. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  701. int rc = 0;
  702. char* test_topic = "C client test4";
  703. test_finished = failures = 0;
  704. MyLog(LOGA_INFO, "Starting test 4 - big messages");
  705. fprintf(xml, "<testcase classname=\"test4\" name=\"big messages\"");
  706. global_start_time = start_clock();
  707. rc = MQTTAsync_create(&c, options.connection, "async_test_4",
  708. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  709. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  710. if (rc != MQTTASYNC_SUCCESS)
  711. {
  712. MQTTAsync_destroy(&c);
  713. goto exit;
  714. }
  715. rc = MQTTAsync_setCallbacks(c, c, NULL, test4_messageArrived, NULL);
  716. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  717. opts.keepAliveInterval = 20;
  718. opts.cleansession = 1;
  719. opts.username = "testuser";
  720. opts.password = "testpassword";
  721. opts.MQTTVersion = options.MQTTVersion;
  722. opts.will = &wopts;
  723. opts.will->message = "will message";
  724. opts.will->qos = 1;
  725. opts.will->retained = 0;
  726. opts.will->topicName = "will topic";
  727. opts.will = NULL;
  728. opts.onSuccess = test4_onConnect;
  729. opts.onFailure = NULL;
  730. opts.context = c;
  731. MyLog(LOGA_DEBUG, "Connecting");
  732. rc = MQTTAsync_connect(c, &opts);
  733. rc = 0;
  734. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  735. if (rc != MQTTASYNC_SUCCESS)
  736. goto exit;
  737. while (!test_finished)
  738. #if defined(_WIN32)
  739. Sleep(100);
  740. #else
  741. usleep(1000L);
  742. #endif
  743. MQTTAsync_destroy(&c);
  744. exit:
  745. MyLog(LOGA_INFO, "TEST4: test %s. %d tests run, %d failures.",
  746. (failures == 0) ? "passed" : "failed", tests, failures);
  747. write_test_result();
  748. return failures;
  749. }
  750. void test5_onConnectFailure(void* context, MQTTAsync_failureData* response)
  751. {
  752. MQTTAsync c = (MQTTAsync)context;
  753. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  754. MyLog(LOGA_DEBUG, "In connect onFailure callback, context %p", context);
  755. MyLog(LOGA_INFO, "Connack rc is %d", response ? response->code : -999);
  756. test_finished = 1;
  757. }
  758. void test5_onConnect(void* context, MQTTAsync_successData* response)
  759. {
  760. MQTTAsync c = (MQTTAsync)context;
  761. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  762. MyLog(LOGA_DEBUG, "In connect onFailure callback, context %p", context);
  763. test_finished = 1;
  764. }
  765. /********************************************************************
  766. Test5: Connack return codes
  767. *********************************************************************/
  768. int test5(struct Options options)
  769. {
  770. int subsqos = 2;
  771. MQTTAsync c;
  772. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  773. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  774. int rc = 0;
  775. char* test_topic = "C client test1";
  776. test_finished = failures = 0;
  777. MyLog(LOGA_INFO, "Starting test 5 - connack return codes");
  778. fprintf(xml, "<testcase classname=\"test4\" name=\"connack return codes\"");
  779. global_start_time = start_clock();
  780. rc = MQTTAsync_create(&c, options.connection, "a clientid that is too long to be accepted",
  781. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  782. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  783. if (rc != MQTTASYNC_SUCCESS)
  784. {
  785. MQTTAsync_destroy(&c);
  786. goto exit;
  787. }
  788. rc = MQTTAsync_setCallbacks(c, c, NULL, test1_messageArrived, NULL);
  789. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  790. opts.onSuccess = test5_onConnect;
  791. opts.onFailure = test5_onConnectFailure;
  792. opts.context = c;
  793. MyLog(LOGA_DEBUG, "Connecting");
  794. rc = MQTTAsync_connect(c, &opts);
  795. rc = 0;
  796. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  797. if (rc != MQTTASYNC_SUCCESS)
  798. goto exit;
  799. while (!test_finished)
  800. #if defined(_WIN32)
  801. Sleep(100);
  802. #else
  803. usleep(10000L);
  804. #endif
  805. MQTTAsync_destroy(&c);
  806. exit:
  807. MyLog(LOGA_INFO, "TEST5: test %s. %d tests run, %d failures.",
  808. (failures == 0) ? "passed" : "failed", tests, failures);
  809. write_test_result();
  810. return failures;
  811. }
  812. typedef struct
  813. {
  814. MQTTAsync c;
  815. int should_fail;
  816. } test6_client_info;
  817. void test6_onConnectFailure(void* context, MQTTAsync_failureData* response)
  818. {
  819. test6_client_info cinfo = *(test6_client_info*)context;
  820. MyLog(LOGA_DEBUG, "In connect onFailure callback, context %p", context);
  821. if (response)
  822. MyLog(LOGA_INFO, "Connack rc is %d", response->code);
  823. assert("Should fail to connect", cinfo.should_fail, "should_fail was %d\n", cinfo.should_fail);
  824. test_finished = 1;
  825. }
  826. void test6_onConnect(void* context, MQTTAsync_successData* response)
  827. {
  828. test6_client_info cinfo = *(test6_client_info*)context;
  829. MyLog(LOGA_DEBUG, "In connect success callback, context %p", context);
  830. assert("Should connect correctly", !cinfo.should_fail, "should_fail was %d\n", cinfo.should_fail);
  831. test_finished = 1;
  832. }
  833. void test6_onDisconnect(void* context, MQTTAsync_successData* response)
  834. {
  835. test6_client_info cinfo = *(test6_client_info*)context;
  836. MyLog(LOGA_DEBUG, "In onDisconnect callback %p", cinfo.c);
  837. test_finished = 1;
  838. }
  839. /********************************************************************
  840. Test6: HA connections
  841. *********************************************************************/
  842. int test6(struct Options options)
  843. {
  844. int subsqos = 2;
  845. test6_client_info cinfo;
  846. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  847. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  848. MQTTAsync_disconnectOptions dopts = MQTTAsync_disconnectOptions_initializer;
  849. int rc = 0;
  850. char* test_topic = "C client test1";
  851. char* uris[2] = {options.connection, options.connection};
  852. failures = 0;
  853. MyLog(LOGA_INFO, "Starting test 6 - HA connections");
  854. fprintf(xml, "<testcase classname=\"test4\" name=\"HA connections\"");
  855. global_start_time = start_clock();
  856. test_finished = 0;
  857. cinfo.should_fail = 1; /* fail to connect */
  858. rc = MQTTAsync_create(&cinfo.c, "tcp://rubbish:1883", "async ha connection",
  859. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  860. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  861. if (rc != MQTTASYNC_SUCCESS)
  862. {
  863. MQTTAsync_destroy(&cinfo.c);
  864. goto exit;
  865. }
  866. rc = MQTTAsync_setCallbacks(cinfo.c, cinfo.c, NULL, test1_messageArrived, NULL);
  867. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  868. opts.onSuccess = test6_onConnect;
  869. opts.onFailure = test6_onConnectFailure;
  870. opts.context = &cinfo;
  871. opts.MQTTVersion = options.MQTTVersion;
  872. MyLog(LOGA_DEBUG, "Connecting");
  873. rc = MQTTAsync_connect(cinfo.c, &opts);
  874. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  875. if (rc != MQTTASYNC_SUCCESS)
  876. goto exit;
  877. while (!test_finished)
  878. #if defined(_WIN32)
  879. Sleep(100);
  880. #else
  881. usleep(10000L);
  882. #endif
  883. test_finished = 0;
  884. cinfo.should_fail = 0; /* should connect through the serverURIs in connect options*/
  885. opts.onSuccess = test6_onConnect;
  886. opts.onFailure = test6_onConnectFailure;
  887. opts.context = &cinfo;
  888. opts.serverURIs = uris;
  889. opts.serverURIcount = 2;
  890. MyLog(LOGA_DEBUG, "Connecting");
  891. rc = MQTTAsync_connect(cinfo.c, &opts);
  892. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  893. if (rc != MQTTASYNC_SUCCESS)
  894. goto exit;
  895. while (!test_finished)
  896. #if defined(_WIN32)
  897. Sleep(100);
  898. #else
  899. usleep(10000L);
  900. #endif
  901. test_finished = 0;
  902. dopts.timeout = 0;
  903. dopts.onSuccess = test6_onDisconnect;
  904. dopts.context = &cinfo;
  905. MQTTAsync_disconnect(cinfo.c, &dopts);
  906. while (!test_finished)
  907. #if defined(_WIN32)
  908. Sleep(100);
  909. #else
  910. usleep(10000L);
  911. #endif
  912. MQTTAsync_destroy(&cinfo.c);
  913. exit:
  914. MyLog(LOGA_INFO, "TEST6: test %s. %d tests run, %d failures.",
  915. (failures == 0) ? "passed" : "failed", tests, failures);
  916. write_test_result();
  917. return failures;
  918. }
  919. /********************************************************************
  920. Test7: Persistence
  921. *********************************************************************/
  922. char* test7_topic = "C client test7";
  923. int test7_messageCount = 0;
  924. void test7_onDisconnectFailure(void* context, MQTTAsync_failureData* response)
  925. {
  926. MQTTAsync c = (MQTTAsync)context;
  927. MyLog(LOGA_DEBUG, "In onDisconnect failure callback %p", c);
  928. assert("Successful disconnect", 0, "disconnect failed", 0);
  929. test_finished = 1;
  930. }
  931. void test7_onDisconnect(void* context, MQTTAsync_successData* response)
  932. {
  933. MQTTAsync c = (MQTTAsync)context;
  934. MyLog(LOGA_DEBUG, "In onDisconnect callback %p", c);
  935. test_finished = 1;
  936. }
  937. void test7_onUnsubscribe(void* context, MQTTAsync_successData* response)
  938. {
  939. MQTTAsync c = (MQTTAsync)context;
  940. MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
  941. int rc;
  942. MyLog(LOGA_DEBUG, "In onUnsubscribe onSuccess callback %p", c);
  943. opts.onSuccess = test7_onDisconnect;
  944. opts.context = c;
  945. opts.timeout = 1000;
  946. rc = MQTTAsync_disconnect(c, &opts);
  947. assert("Disconnect successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  948. }
  949. int test7_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message)
  950. {
  951. MQTTAsync c = (MQTTAsync)context;
  952. static int message_count = 0;
  953. MyLog(LOGA_DEBUG, "Test7: received message id %d", message->msgid);
  954. test7_messageCount++;
  955. MQTTAsync_freeMessage(&message);
  956. MQTTAsync_free(topicName);
  957. return 1;
  958. }
  959. static int test7_subscribed = 0;
  960. void test7_onSubscribe(void* context, MQTTAsync_successData* response)
  961. {
  962. MQTTAsync c = (MQTTAsync)context;
  963. MyLog(LOGA_DEBUG, "In subscribe onSuccess callback %p granted qos %d", c, response->alt.qos);
  964. test7_subscribed = 1;
  965. }
  966. void test7_onConnect(void* context, MQTTAsync_successData* response)
  967. {
  968. MQTTAsync c = (MQTTAsync)context;
  969. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  970. int rc;
  971. MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
  972. opts.onSuccess = test7_onSubscribe;
  973. opts.context = c;
  974. rc = MQTTAsync_subscribe(c, test7_topic, 2, &opts);
  975. assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  976. if (rc != MQTTASYNC_SUCCESS)
  977. test_finished = 1;
  978. }
  979. void test7_onConnectOnly(void* context, MQTTAsync_successData* response)
  980. {
  981. MQTTAsync c = (MQTTAsync)context;
  982. MQTTAsync_disconnectOptions dopts = MQTTAsync_disconnectOptions_initializer;
  983. int rc;
  984. MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
  985. dopts.context = context;
  986. dopts.timeout = 1000;
  987. dopts.onSuccess = test7_onDisconnect;
  988. rc = MQTTAsync_disconnect(c, &dopts);
  989. assert("Good rc from disconnect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  990. if (rc != MQTTASYNC_SUCCESS)
  991. test_finished = 1;
  992. }
  993. /*********************************************************************
  994. Test7: Pending tokens
  995. *********************************************************************/
  996. int test7(struct Options options)
  997. {
  998. int subsqos = 2;
  999. MQTTAsync c;
  1000. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  1001. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  1002. int rc = 0;
  1003. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  1004. MQTTAsync_responseOptions ropts = MQTTAsync_responseOptions_initializer;
  1005. MQTTAsync_disconnectOptions dopts = MQTTAsync_disconnectOptions_initializer;
  1006. MQTTAsync_token* tokens = NULL;
  1007. int msg_count = 6;
  1008. MyLog(LOGA_INFO, "Starting test 7 - pending tokens");
  1009. fprintf(xml, "<testcase classname=\"test4\" name=\"pending tokens\"");
  1010. global_start_time = start_clock();
  1011. test_finished = 0;
  1012. rc = MQTTAsync_create(&c, options.connection, "async_test7",
  1013. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  1014. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1015. if (rc != MQTTASYNC_SUCCESS)
  1016. {
  1017. MQTTAsync_destroy(&c);
  1018. goto exit;
  1019. }
  1020. rc = MQTTAsync_setCallbacks(c, c, NULL, test7_messageArrived, NULL);
  1021. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1022. opts.keepAliveInterval = 20;
  1023. opts.username = "testuser";
  1024. opts.password = "testpassword";
  1025. opts.MQTTVersion = options.MQTTVersion;
  1026. opts.will = &wopts;
  1027. opts.will->message = "will message";
  1028. opts.will->qos = 1;
  1029. opts.will->retained = 0;
  1030. opts.will->topicName = "will topic";
  1031. opts.will = NULL;
  1032. opts.onFailure = NULL;
  1033. opts.context = c;
  1034. opts.cleansession = 1;
  1035. opts.onSuccess = test7_onConnectOnly;
  1036. MyLog(LOGA_DEBUG, "Connecting to clean up");
  1037. rc = MQTTAsync_connect(c, &opts);
  1038. rc = 0;
  1039. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1040. if (rc != MQTTASYNC_SUCCESS)
  1041. goto exit;
  1042. while (!test_finished)
  1043. #if defined(_WIN32)
  1044. Sleep(100);
  1045. #else
  1046. usleep(10000L);
  1047. #endif
  1048. test_finished = 0;
  1049. MyLog(LOGA_DEBUG, "Connecting");
  1050. opts.cleansession = 0;
  1051. opts.onSuccess = test7_onConnect;
  1052. rc = MQTTAsync_connect(c, &opts);
  1053. rc = 0;
  1054. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1055. if (rc != MQTTASYNC_SUCCESS)
  1056. goto exit;
  1057. while (!test7_subscribed)
  1058. #if defined(_WIN32)
  1059. Sleep(100);
  1060. #else
  1061. usleep(10000L);
  1062. #endif
  1063. pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  1064. pubmsg.payloadlen = 11;
  1065. pubmsg.qos = 2;
  1066. pubmsg.retained = 0;
  1067. rc = MQTTAsync_send(c, test_topic, pubmsg.payloadlen, pubmsg.payload, pubmsg.qos, pubmsg.retained, &ropts);
  1068. MyLog(LOGA_DEBUG, "Token was %d", ropts.token);
  1069. rc = MQTTAsync_isComplete(c, ropts.token);
  1070. /*assert("0 rc from isComplete", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);*/
  1071. rc = MQTTAsync_waitForCompletion(c, ropts.token, 5000L);
  1072. assert("Good rc from waitForCompletion", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1073. rc = MQTTAsync_isComplete(c, ropts.token);
  1074. assert("1 rc from isComplete", rc == 1, "rc was %d", rc);
  1075. test7_messageCount = 0;
  1076. int i = 0;
  1077. pubmsg.qos = 2;
  1078. for (i = 0; i < msg_count; ++i)
  1079. {
  1080. pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  1081. pubmsg.payloadlen = 11;
  1082. //pubmsg.qos = (pubmsg.qos == 2) ? 1 : 2;
  1083. pubmsg.retained = 0;
  1084. rc = MQTTAsync_sendMessage(c, test_topic, &pubmsg, &ropts);
  1085. }
  1086. /* disconnect immediately without receiving the incoming messages */
  1087. dopts.timeout = 0;
  1088. dopts.onSuccess = test7_onDisconnect;
  1089. dopts.context = c;
  1090. MQTTAsync_disconnect(c, &dopts); /* now there should be "orphaned" publications */
  1091. while (!test_finished)
  1092. #if defined(_WIN32)
  1093. Sleep(100);
  1094. #else
  1095. usleep(10000L);
  1096. #endif
  1097. test_finished = 0;
  1098. rc = MQTTAsync_getPendingTokens(c, &tokens);
  1099. assert("getPendingTokens rc == 0", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1100. assert("should get some tokens back", tokens != NULL, "tokens was %p", tokens);
  1101. MQTTAsync_free(tokens);
  1102. MQTTAsync_destroy(&c); /* force re-reading persistence on create */
  1103. MQTTAsync_setTraceLevel(MQTTASYNC_TRACE_ERROR);
  1104. rc = MQTTAsync_create(&c, options.connection, "async_test7", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  1105. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1106. if (rc != MQTTASYNC_SUCCESS)
  1107. {
  1108. MQTTAsync_destroy(&c);
  1109. goto exit;
  1110. }
  1111. rc = MQTTAsync_getPendingTokens(c, &tokens);
  1112. assert("getPendingTokens rc == 0", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1113. assert("should get some tokens back", tokens != NULL, "tokens was %p", tokens);
  1114. if (tokens)
  1115. {
  1116. int i = 0;
  1117. while (tokens[i] != -1)
  1118. MyLog(LOGA_DEBUG, "Delivery token %d", tokens[i++]);
  1119. MQTTAsync_free(tokens);
  1120. //The following assertion should work, does with RSMB, but not Mosquitto
  1121. //assert1("no of tokens should be count", i == msg_count, "no of tokens %d count %d", i, msg_count);
  1122. }
  1123. rc = MQTTAsync_setCallbacks(c, c, NULL, test7_messageArrived, NULL);
  1124. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1125. MyLog(LOGA_DEBUG, "Reconnecting");
  1126. opts.context = c;
  1127. if (MQTTAsync_connect(c, &opts) != 0)
  1128. {
  1129. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1130. goto exit;
  1131. }
  1132. #if defined(_WIN32)
  1133. Sleep(5000);
  1134. #else
  1135. usleep(5000000L);
  1136. #endif
  1137. rc = MQTTAsync_getPendingTokens(c, &tokens);
  1138. assert("getPendingTokens rc == 0", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1139. /* assert("should get no tokens back", tokens == NULL, "tokens was %p", tokens);
  1140. assert1("no of messages should be count", test7_messageCount == msg_count, "no of tokens %d count %d",
  1141. test7_messageCount, msg_count);
  1142. assertions fail against Mosquitto - needs testing */
  1143. dopts.onFailure = test7_onDisconnectFailure;
  1144. dopts.onSuccess = test7_onDisconnect;
  1145. dopts.timeout = 1000;
  1146. MQTTAsync_disconnect(c, &dopts);
  1147. while (!test_finished)
  1148. #if defined(_WIN32)
  1149. Sleep(100);
  1150. #else
  1151. usleep(10000L);
  1152. #endif
  1153. MQTTAsync_destroy(&c);
  1154. exit:
  1155. MyLog(LOGA_INFO, "TEST7: test %s. %d tests run, %d failures.",
  1156. (failures == 0) ? "passed" : "failed", tests, failures);
  1157. write_test_result();
  1158. return failures;
  1159. }
  1160. /*********************************************************************
  1161. Test8: Incomplete commands and requests
  1162. *********************************************************************/
  1163. char* test8_topic = "C client test8";
  1164. int test8_messageCount = 0;
  1165. int test8_subscribed = 0;
  1166. int test8_publishFailures = 0;
  1167. void test8_onPublish(void* context, MQTTAsync_successData* response)
  1168. {
  1169. MQTTAsync c = (MQTTAsync)context;
  1170. MyLog(LOGA_DEBUG, "In publish onSuccess callback %p token %d", c, response->token);
  1171. }
  1172. void test8_onPublishFailure(void* context, MQTTAsync_failureData* response)
  1173. {
  1174. MQTTAsync c = (MQTTAsync)context;
  1175. MyLog(LOGA_DEBUG, "In onPublish failure callback %p", c);
  1176. assert("Response code should be interrupted", response->code == MQTTASYNC_OPERATION_INCOMPLETE,
  1177. "rc was %d", response->code);
  1178. test8_publishFailures++;
  1179. }
  1180. void test8_onDisconnectFailure(void* context, MQTTAsync_failureData* response)
  1181. {
  1182. MQTTAsync c = (MQTTAsync)context;
  1183. MyLog(LOGA_DEBUG, "In onDisconnect failure callback %p", c);
  1184. assert("Successful disconnect", 0, "disconnect failed", 0);
  1185. test_finished = 1;
  1186. }
  1187. void test8_onDisconnect(void* context, MQTTAsync_successData* response)
  1188. {
  1189. MQTTAsync c = (MQTTAsync)context;
  1190. MyLog(LOGA_DEBUG, "In onDisconnect callback %p", c);
  1191. test_finished = 1;
  1192. }
  1193. void test8_onSubscribe(void* context, MQTTAsync_successData* response)
  1194. {
  1195. MQTTAsync c = (MQTTAsync)context;
  1196. MyLog(LOGA_DEBUG, "In subscribe onSuccess callback %p granted qos %d", c, response->alt.qos);
  1197. test8_subscribed = 1;
  1198. }
  1199. void test8_onConnect(void* context, MQTTAsync_successData* response)
  1200. {
  1201. MQTTAsync c = (MQTTAsync)context;
  1202. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1203. int rc;
  1204. MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
  1205. opts.onSuccess = test8_onSubscribe;
  1206. opts.context = c;
  1207. rc = MQTTAsync_subscribe(c, test8_topic, 2, &opts);
  1208. assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1209. if (rc != MQTTASYNC_SUCCESS)
  1210. test_finished = 1;
  1211. }
  1212. int test8_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message)
  1213. {
  1214. MQTTAsync c = (MQTTAsync)context;
  1215. static int message_count = 0;
  1216. MyLog(LOGA_DEBUG, "Test8: received message id %d", message->msgid);
  1217. test8_messageCount++;
  1218. MQTTAsync_freeMessage(&message);
  1219. MQTTAsync_free(topicName);
  1220. return 1;
  1221. }
  1222. int test8(struct Options options)
  1223. {
  1224. int subsqos = 2;
  1225. MQTTAsync c;
  1226. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  1227. int rc = 0;
  1228. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  1229. MQTTAsync_responseOptions ropts = MQTTAsync_responseOptions_initializer;
  1230. MQTTAsync_disconnectOptions dopts = MQTTAsync_disconnectOptions_initializer;
  1231. MQTTAsync_token* tokens = NULL;
  1232. int msg_count = 6;
  1233. MyLog(LOGA_INFO, "Starting test 8 - incomplete commands");
  1234. fprintf(xml, "<testcase classname=\"test4\" name=\"incomplete commands\"");
  1235. global_start_time = start_clock();
  1236. test_finished = 0;
  1237. rc = MQTTAsync_create(&c, options.connection, "async_test8",
  1238. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  1239. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1240. if (rc != MQTTASYNC_SUCCESS)
  1241. {
  1242. MQTTAsync_destroy(&c);
  1243. goto exit;
  1244. }
  1245. rc = MQTTAsync_setCallbacks(c, c, NULL, test8_messageArrived, NULL);
  1246. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1247. opts.keepAliveInterval = 20;
  1248. opts.username = "testuser";
  1249. opts.password = "testpassword";
  1250. opts.MQTTVersion = options.MQTTVersion;
  1251. opts.onFailure = NULL;
  1252. opts.context = c;
  1253. MyLog(LOGA_DEBUG, "Connecting");
  1254. opts.cleansession = 1;
  1255. opts.onSuccess = test8_onConnect;
  1256. rc = MQTTAsync_connect(c, &opts);
  1257. rc = 0;
  1258. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1259. if (rc != MQTTASYNC_SUCCESS)
  1260. goto exit;
  1261. while (!test8_subscribed)
  1262. #if defined(_WIN32)
  1263. Sleep(100);
  1264. #else
  1265. usleep(10000L);
  1266. #endif
  1267. int i = 0;
  1268. pubmsg.qos = 2;
  1269. ropts.onSuccess = test8_onPublish;
  1270. ropts.onFailure = test8_onPublishFailure;
  1271. ropts.context = c;
  1272. for (i = 0; i < msg_count; ++i)
  1273. {
  1274. pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  1275. pubmsg.payloadlen = 11;
  1276. pubmsg.qos = (pubmsg.qos == 2) ? 1 : 2; /* alternate */
  1277. pubmsg.retained = 0;
  1278. rc = MQTTAsync_sendMessage(c, test8_topic, &pubmsg, &ropts);
  1279. assert("Good rc from sendMessage", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1280. }
  1281. /* disconnect immediately without completing the commands */
  1282. dopts.timeout = 0;
  1283. dopts.onSuccess = test8_onDisconnect;
  1284. dopts.context = c;
  1285. rc = MQTTAsync_disconnect(c, &dopts); /* now there should be incomplete commands */
  1286. assert("Good rc from disconnect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1287. while (!test_finished)
  1288. #if defined(_WIN32)
  1289. Sleep(100);
  1290. #else
  1291. usleep(10000L);
  1292. #endif
  1293. test_finished = 0;
  1294. rc = MQTTAsync_getPendingTokens(c, &tokens);
  1295. assert("getPendingTokens rc == 0", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1296. assert("should get no tokens back", tokens == NULL, "tokens was %p", tokens);
  1297. assert("test8_publishFailures > 0", test8_publishFailures > 0,
  1298. "test8_publishFailures = %d", test8_publishFailures);
  1299. /* Now elicit failure callbacks on destroy */
  1300. test8_subscribed = test8_publishFailures = 0;
  1301. MyLog(LOGA_DEBUG, "Connecting");
  1302. opts.cleansession = 0;
  1303. opts.onSuccess = test8_onConnect;
  1304. rc = MQTTAsync_connect(c, &opts);
  1305. rc = 0;
  1306. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1307. if (rc != MQTTASYNC_SUCCESS)
  1308. goto exit;
  1309. while (!test8_subscribed)
  1310. #if defined(_WIN32)
  1311. Sleep(100);
  1312. #else
  1313. usleep(10000L);
  1314. #endif
  1315. i = 0;
  1316. pubmsg.qos = 2;
  1317. ropts.onSuccess = test8_onPublish;
  1318. ropts.onFailure = test8_onPublishFailure;
  1319. ropts.context = c;
  1320. for (i = 0; i < msg_count; ++i)
  1321. {
  1322. pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  1323. pubmsg.payloadlen = 11;
  1324. pubmsg.qos = (pubmsg.qos == 2) ? 1 : 2; /* alternate */
  1325. pubmsg.retained = 0;
  1326. rc = MQTTAsync_sendMessage(c, test8_topic, &pubmsg, &ropts);
  1327. assert("Good rc from sendMessage", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1328. }
  1329. /* disconnect immediately without completing the commands */
  1330. dopts.timeout = 0;
  1331. dopts.onSuccess = test8_onDisconnect;
  1332. dopts.context = c;
  1333. rc = MQTTAsync_disconnect(c, &dopts); /* now there should be incomplete commands */
  1334. assert("Good rc from disconnect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1335. while (!test_finished)
  1336. #if defined(_WIN32)
  1337. Sleep(100);
  1338. #else
  1339. usleep(10000L);
  1340. #endif
  1341. test_finished = 0;
  1342. rc = MQTTAsync_getPendingTokens(c, &tokens);
  1343. assert("getPendingTokens rc == 0", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1344. assert("should get some tokens back", tokens != NULL, "tokens was %p", tokens);
  1345. MQTTAsync_free(tokens);
  1346. assert("test8_publishFailures == 0", test8_publishFailures == 0,
  1347. "test8_publishFailures = %d", test8_publishFailures);
  1348. MQTTAsync_destroy(&c);
  1349. assert("test8_publishFailures > 0", test8_publishFailures > 0,
  1350. "test8_publishFailures = %d", test8_publishFailures);
  1351. /* cleanup persistence of any left over message data*/
  1352. MQTTAsync_setTraceLevel(MQTTASYNC_TRACE_ERROR);
  1353. rc = MQTTAsync_create(&c, options.connection, "async_test8",
  1354. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  1355. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1356. if (rc != MQTTASYNC_SUCCESS)
  1357. {
  1358. MQTTAsync_destroy(&c);
  1359. goto exit;
  1360. }
  1361. test8_subscribed = 0;
  1362. opts.cleansession = 1;
  1363. opts.context = c;
  1364. rc = MQTTAsync_connect(c, &opts);
  1365. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1366. if (rc != MQTTASYNC_SUCCESS)
  1367. goto exit;
  1368. while (!test8_subscribed)
  1369. #if defined(_WIN32)
  1370. Sleep(100);
  1371. #else
  1372. usleep(10000L);
  1373. #endif
  1374. test_finished = 0;
  1375. dopts.context = c;
  1376. rc = MQTTAsync_disconnect(c, &dopts);
  1377. assert("Good rc from disconnect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1378. while (!test_finished)
  1379. #if defined(_WIN32)
  1380. Sleep(100);
  1381. #else
  1382. usleep(10000L);
  1383. #endif
  1384. MQTTAsync_destroy(&c);
  1385. exit:
  1386. MyLog(LOGA_INFO, "TEST8: test %s. %d tests run, %d failures.",
  1387. (failures == 0) ? "passed" : "failed", tests, failures);
  1388. write_test_result();
  1389. return failures;
  1390. }
  1391. void trace_callback(enum MQTTASYNC_TRACE_LEVELS level, char* message)
  1392. {
  1393. printf("Trace : %d, %s\n", level, message);
  1394. }
  1395. int main(int argc, char** argv)
  1396. {
  1397. int rc = 0;
  1398. int (*tests[])() = {NULL, test1, test2, test3, test4, test5, test6, test7, test8}; /* indexed starting from 1 */
  1399. MQTTAsync_nameValue* info;
  1400. int i;
  1401. xml = fopen("TEST-test4.xml", "w");
  1402. fprintf(xml, "<testsuite name=\"test4\" tests=\"%d\">\n", (int)(ARRAY_SIZE(tests)) - 1);
  1403. getopts(argc, argv);
  1404. MQTTAsync_setTraceCallback(trace_callback);
  1405. info = MQTTAsync_getVersionInfo();
  1406. while (info->name)
  1407. {
  1408. MyLog(LOGA_INFO, "%s: %s", info->name, info->value);
  1409. info++;
  1410. }
  1411. for (i = 0; i < options.iterations; ++i)
  1412. {
  1413. if (options.test_no == -1)
  1414. { /* run all the tests */
  1415. for (options.test_no = 1; options.test_no < ARRAY_SIZE(tests); ++options.test_no)
  1416. {
  1417. failures = 0;
  1418. MQTTAsync_setTraceLevel(MQTTASYNC_TRACE_ERROR);
  1419. rc += tests[options.test_no](options); /* return number of failures. 0 = test succeeded */
  1420. }
  1421. }
  1422. else
  1423. {
  1424. MQTTAsync_setTraceLevel(MQTTASYNC_TRACE_ERROR);
  1425. rc = tests[options.test_no](options); /* run just the selected test */
  1426. }
  1427. }
  1428. if (rc == 0)
  1429. MyLog(LOGA_INFO, "verdict pass");
  1430. else
  1431. MyLog(LOGA_INFO, "verdict fail");
  1432. fprintf(xml, "</testsuite>\n");
  1433. fclose(xml);
  1434. return rc;
  1435. }