test4.c 48 KB

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