test_mqtt4async.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2014 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. *******************************************************************************/
  17. /**
  18. * @file
  19. * MQTT 3.1.1 Tests for the asynchronous Paho MQTT C client
  20. */
  21. /*
  22. #if !defined(_RTSHEADER)
  23. #include <rts.h>
  24. #endif
  25. */
  26. #include "MQTTAsync.h"
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #if !defined(_WINDOWS)
  30. #include <sys/time.h>
  31. #include <sys/socket.h>
  32. #include <unistd.h>
  33. #include <errno.h>
  34. #else
  35. #include <winsock2.h>
  36. #include <ws2tcpip.h>
  37. #define MAXHOSTNAMELEN 256
  38. #define EAGAIN WSAEWOULDBLOCK
  39. #define EINTR WSAEINTR
  40. #define EINPROGRESS WSAEINPROGRESS
  41. #define EWOULDBLOCK WSAEWOULDBLOCK
  42. #define ENOTCONN WSAENOTCONN
  43. #define ECONNRESET WSAECONNRESET
  44. #define setenv(a, b, c) _putenv_s(a, b)
  45. #endif
  46. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  47. void usage(void)
  48. {
  49. printf("help!!\n");
  50. exit(EXIT_FAILURE);
  51. }
  52. struct Options
  53. {
  54. char* connection; /**< connection to system under test. */
  55. char** haconnections;
  56. int hacount;
  57. int verbose;
  58. int test_no;
  59. int iterations;
  60. } options =
  61. {
  62. "m2m.eclipse.org:1883",
  63. NULL,
  64. 0,
  65. 0,
  66. 0,
  67. 1,
  68. };
  69. void getopts(int argc, char** argv)
  70. {
  71. int count = 1;
  72. while (count < argc)
  73. {
  74. if (strcmp(argv[count], "--test_no") == 0)
  75. {
  76. if (++count < argc)
  77. options.test_no = atoi(argv[count]);
  78. else
  79. usage();
  80. }
  81. else if (strcmp(argv[count], "--connection") == 0)
  82. {
  83. if (++count < argc)
  84. {
  85. options.connection = argv[count];
  86. printf("\nSetting connection to %s\n", options.connection);
  87. }
  88. else
  89. usage();
  90. }
  91. else if (strcmp(argv[count], "--haconnections") == 0)
  92. {
  93. if (++count < argc)
  94. {
  95. char* tok = strtok(argv[count], " ");
  96. options.hacount = 0;
  97. options.haconnections = malloc(sizeof(char*) * 5);
  98. while (tok)
  99. {
  100. options.haconnections[options.hacount] = malloc(strlen(tok) + 1);
  101. strcpy(options.haconnections[options.hacount], tok);
  102. options.hacount++;
  103. tok = strtok(NULL, " ");
  104. }
  105. }
  106. else
  107. usage();
  108. }
  109. else if (strcmp(argv[count], "--iterations") == 0)
  110. {
  111. if (++count < argc)
  112. options.iterations = atoi(argv[count]);
  113. else
  114. usage();
  115. }
  116. else if (strcmp(argv[count], "--verbose") == 0)
  117. {
  118. options.verbose = 1;
  119. printf("\nSetting verbose on\n");
  120. }
  121. count++;
  122. }
  123. }
  124. #define LOGA_DEBUG 0
  125. #define LOGA_INFO 1
  126. #include <stdarg.h>
  127. #include <time.h>
  128. #include <sys/timeb.h>
  129. void MyLog(int LOGA_level, char* format, ...)
  130. {
  131. static char msg_buf[256];
  132. va_list args;
  133. struct timeb ts;
  134. struct tm *timeinfo;
  135. if (LOGA_level == LOGA_DEBUG && options.verbose == 0)
  136. return;
  137. ftime(&ts);
  138. timeinfo = localtime(&ts.time);
  139. strftime(msg_buf, 80, "%Y%m%d %H%M%S", timeinfo);
  140. sprintf(&msg_buf[strlen(msg_buf)], ".%.3hu ", ts.millitm);
  141. va_start(args, format);
  142. vsnprintf(&msg_buf[strlen(msg_buf)], sizeof(msg_buf) - strlen(msg_buf), format, args);
  143. va_end(args);
  144. printf("%s\n", msg_buf);
  145. fflush(stdout);
  146. }
  147. #if defined(_WIN32) || defined(_WINDOWS)
  148. #define mqsleep(A) Sleep(1000*A)
  149. #define START_TIME_TYPE DWORD
  150. static DWORD start_time = 0;
  151. START_TIME_TYPE start_clock(void)
  152. {
  153. return GetTickCount();
  154. }
  155. #elif defined(AIX)
  156. #define mqsleep sleep
  157. #define START_TIME_TYPE struct timespec
  158. START_TIME_TYPE start_clock(void)
  159. {
  160. static struct timespec start;
  161. clock_gettime(CLOCK_REALTIME, &start);
  162. return start;
  163. }
  164. #else
  165. #define mqsleep sleep
  166. #define START_TIME_TYPE struct timeval
  167. /* TODO - unused - remove? static struct timeval start_time; */
  168. START_TIME_TYPE start_clock(void)
  169. {
  170. struct timeval start_time;
  171. gettimeofday(&start_time, NULL);
  172. return start_time;
  173. }
  174. #endif
  175. #if defined(_WIN32)
  176. long elapsed(START_TIME_TYPE start_time)
  177. {
  178. return GetTickCount() - start_time;
  179. }
  180. #elif defined(AIX)
  181. #define assert(a)
  182. long elapsed(struct timespec start)
  183. {
  184. struct timespec now, res;
  185. clock_gettime(CLOCK_REALTIME, &now);
  186. ntimersub(now, start, res);
  187. return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L;
  188. }
  189. #else
  190. long elapsed(START_TIME_TYPE start_time)
  191. {
  192. struct timeval now, res;
  193. gettimeofday(&now, NULL);
  194. timersub(&now, &start_time, &res);
  195. return (res.tv_sec)*1000 + (res.tv_usec)/1000;
  196. }
  197. #endif
  198. #define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d)
  199. #define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e)
  200. int tests = 0;
  201. int failures = 0;
  202. FILE* xml;
  203. START_TIME_TYPE global_start_time;
  204. char output[3000];
  205. char* cur_output = output;
  206. int test_finished = 0;
  207. void write_test_result(void)
  208. {
  209. long duration = elapsed(global_start_time);
  210. fprintf(xml, " time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000);
  211. if (cur_output != output)
  212. {
  213. fprintf(xml, "%s", output);
  214. cur_output = output;
  215. }
  216. fprintf(xml, "</testcase>\n");
  217. }
  218. void myassert(char* filename, int lineno, char* description, int value, char* format, ...)
  219. {
  220. ++tests;
  221. if (!value)
  222. {
  223. va_list args;
  224. ++failures;
  225. MyLog(LOGA_INFO, "Assertion failed, file %s, line %d, description: %s\n", filename, lineno, description);
  226. va_start(args, format);
  227. vprintf(format, args);
  228. va_end(args);
  229. cur_output += sprintf(cur_output, "<failure type=\"%s\">file %s, line %d </failure>\n",
  230. description, filename, lineno);
  231. }
  232. else
  233. MyLog(LOGA_DEBUG, "Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description);
  234. }
  235. void test1_onDisconnect3(void* context, MQTTAsync_successData* response)
  236. {
  237. MQTTAsync c = (MQTTAsync)context;
  238. MyLog(LOGA_DEBUG, "In onDisconnect callback %p", c);
  239. test_finished = 1;
  240. }
  241. void test1_onConnect3(void* context, MQTTAsync_successData* response)
  242. {
  243. MQTTAsync c = (MQTTAsync)context;
  244. MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
  245. int rc;
  246. MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
  247. opts.onSuccess = test1_onDisconnect3;
  248. opts.context = c;
  249. assert("Correct serverURI returned", strstr(response->alt.connect.serverURI, options.connection) != NULL,
  250. "serverURI was %s", response->alt.connect.serverURI);
  251. assert("Correct MQTTVersion returned", response->alt.connect.MQTTVersion == 4,
  252. "MQTTVersion was %d", response->alt.connect.MQTTVersion);
  253. assert("Correct sessionPresent returned", response->alt.connect.sessionPresent == 1,
  254. "sessionPresent was %d", response->alt.connect.sessionPresent);
  255. rc = MQTTAsync_disconnect(c, &opts);
  256. assert("Disconnect successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  257. }
  258. void test1_onDisconnect2(void* context, MQTTAsync_successData* response)
  259. {
  260. MQTTAsync c = (MQTTAsync)context;
  261. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  262. int rc;
  263. MyLog(LOGA_DEBUG, "In onDisconnect callback %p", c);
  264. opts.MQTTVersion = 4;
  265. opts.cleansession = 0;
  266. if (options.haconnections != NULL)
  267. {
  268. opts.serverURIs = options.haconnections;
  269. opts.serverURIcount = options.hacount;
  270. }
  271. opts.onSuccess = test1_onConnect3;
  272. opts.onFailure = NULL;
  273. opts.context = c;
  274. opts.cleansession = 0;
  275. rc = MQTTAsync_connect(c, &opts);
  276. assert("Connect successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  277. }
  278. void test1_onConnect2(void* context, MQTTAsync_successData* response)
  279. {
  280. MQTTAsync c = (MQTTAsync)context;
  281. MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
  282. int rc;
  283. MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
  284. opts.onSuccess = test1_onDisconnect2;
  285. opts.context = c;
  286. assert("Correct serverURI returned", strcmp(response->alt.connect.serverURI, options.connection) == 0,
  287. "serverURI was %s", response->alt.connect.serverURI);
  288. assert("Correct MQTTVersion returned", response->alt.connect.MQTTVersion == 4,
  289. "MQTTVersion was %d", response->alt.connect.MQTTVersion);
  290. assert("Correct sessionPresent returned", response->alt.connect.sessionPresent == 0,
  291. "sessionPresent was %d", response->alt.connect.sessionPresent);
  292. rc = MQTTAsync_disconnect(c, &opts);
  293. assert("Disconnect successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  294. }
  295. void test1_onDisconnect1(void* context, MQTTAsync_successData* response)
  296. {
  297. MQTTAsync c = (MQTTAsync)context;
  298. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  299. int rc;
  300. MyLog(LOGA_DEBUG, "In onDisconnect callback %p", c);
  301. opts.MQTTVersion = 4;
  302. opts.cleansession = 0;
  303. if (options.haconnections != NULL)
  304. {
  305. opts.serverURIs = options.haconnections;
  306. opts.serverURIcount = options.hacount;
  307. }
  308. opts.onSuccess = test1_onConnect2;
  309. opts.onFailure = NULL;
  310. opts.context = c;
  311. opts.cleansession = 0;
  312. rc = MQTTAsync_connect(c, &opts);
  313. assert("Connect successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  314. }
  315. void test1_onConnect1(void* context, MQTTAsync_successData* response)
  316. {
  317. MQTTAsync c = (MQTTAsync)context;
  318. MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
  319. int rc;
  320. MyLog(LOGA_DEBUG, "In connect onSuccess callback 1, context %p", context);
  321. opts.onSuccess = test1_onDisconnect1;
  322. opts.context = c;
  323. assert("Correct serverURI returned", strcmp(response->alt.connect.serverURI, options.connection) == 0,
  324. "serverURI was %s", response->alt.connect.serverURI);
  325. assert("Correct MQTTVersion returned", response->alt.connect.MQTTVersion == 4,
  326. "MQTTVersion was %d", response->alt.connect.MQTTVersion);
  327. assert("Correct sessionPresent returned", response->alt.connect.sessionPresent == 0,
  328. "sessionPresent was %d", response->alt.connect.sessionPresent);
  329. rc = MQTTAsync_disconnect(c, &opts);
  330. assert("Disconnect successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  331. }
  332. /*********************************************************************
  333. Test1: sessionPresent
  334. *********************************************************************/
  335. int test1(struct Options options)
  336. {
  337. MQTTAsync c;
  338. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  339. int rc = 0;
  340. char* test_topic = "C client test1";
  341. fprintf(xml, "<testcase classname=\"test1\" name=\"sessionPresent\"");
  342. global_start_time = start_clock();
  343. test_finished = failures = 0;
  344. MyLog(LOGA_INFO, "Starting test 1 - sessionPresent");
  345. rc = MQTTAsync_create(&c, options.connection, "sesssionPresent",
  346. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  347. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  348. if (rc != MQTTASYNC_SUCCESS)
  349. {
  350. MQTTAsync_destroy(&c);
  351. goto exit;
  352. }
  353. opts.MQTTVersion = 4;
  354. if (options.haconnections != NULL)
  355. {
  356. opts.serverURIs = options.haconnections;
  357. opts.serverURIcount = options.hacount;
  358. }
  359. opts.onSuccess = test1_onConnect1;
  360. opts.onFailure = NULL;
  361. opts.context = c;
  362. /* Connect cleansession */
  363. opts.cleansession = 1;
  364. MyLog(LOGA_DEBUG, "Connecting");
  365. rc = MQTTAsync_connect(c, &opts);
  366. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  367. if (rc != MQTTASYNC_SUCCESS)
  368. goto exit;
  369. while (!test_finished)
  370. #if defined(_WIN32)
  371. Sleep(100);
  372. #else
  373. usleep(10000L);
  374. #endif
  375. MQTTAsync_destroy(&c);
  376. exit:
  377. MyLog(LOGA_INFO, "TEST1: test %s. %d tests run, %d failures.",
  378. (failures == 0) ? "passed" : "failed", tests, failures);
  379. write_test_result();
  380. return failures;
  381. }
  382. void test2_onDisconnect(void* context, MQTTAsync_successData* response)
  383. {
  384. MQTTAsync c = (MQTTAsync)context;
  385. MyLog(LOGA_DEBUG, "In onDisconnect callback %p", c);
  386. test_finished = 1;
  387. }
  388. void test2_onSubscribe2(void* context, MQTTAsync_failureData* response)
  389. {
  390. MQTTAsync c = (MQTTAsync)context;
  391. MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
  392. int rc;
  393. MyLog(LOGA_DEBUG, "In subscribe onFailure callback, context %p", context);
  394. assert("Correct subscribe return code", response->code == MQTT_BAD_SUBSCRIBE,
  395. "qos was %d", response->code);
  396. opts.onSuccess = test2_onDisconnect;
  397. rc = MQTTAsync_disconnect(c, &opts);
  398. assert("Disconnect successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  399. }
  400. void test2_onSubscribe1(void* context, MQTTAsync_successData* response)
  401. {
  402. MQTTAsync c = (MQTTAsync)context;
  403. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  404. int rc;
  405. MyLog(LOGA_DEBUG, "In subscribe onSuccess callback, context %p", context);
  406. assert("Correct subscribe return code", response->alt.qos == 2,
  407. "qos was %d", response->alt.qos);
  408. }
  409. void test2_onConnect1(void* context, MQTTAsync_successData* response)
  410. {
  411. MQTTAsync c = (MQTTAsync)context;
  412. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  413. int rc;
  414. MyLog(LOGA_DEBUG, "In connect onSuccess callback 1, context %p", context);
  415. assert("Correct serverURI returned", strcmp(response->alt.connect.serverURI, options.connection) == 0,
  416. "serverURI was %s", response->alt.connect.serverURI);
  417. assert("Correct MQTTVersion returned", response->alt.connect.MQTTVersion == 4,
  418. "MQTTVersion was %d", response->alt.connect.MQTTVersion);
  419. assert("Correct sessionPresent returned", response->alt.connect.sessionPresent == 0,
  420. "sessionPresent was %d", response->alt.connect.sessionPresent);
  421. MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
  422. opts.context = c;
  423. opts.onSuccess = test2_onSubscribe1;
  424. rc = MQTTAsync_subscribe(c, "a topic I can subscribe to", 2, &opts);
  425. assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  426. if (rc != MQTTASYNC_SUCCESS)
  427. test_finished = 1;
  428. opts.onSuccess = NULL;
  429. opts.onFailure = test2_onSubscribe2;
  430. rc = MQTTAsync_subscribe(c, "nosubscribe", 2, &opts);
  431. assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  432. if (rc != MQTTASYNC_SUCCESS)
  433. test_finished = 1;
  434. }
  435. /*********************************************************************
  436. Test1: 0x80 from subscribe
  437. *********************************************************************/
  438. int test2(struct Options options)
  439. {
  440. MQTTAsync c;
  441. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  442. int rc = 0;
  443. char* test_topic = "C client test1";
  444. fprintf(xml, "<testcase classname=\"test2\" name=\"bad subscribe\"");
  445. global_start_time = start_clock();
  446. test_finished = failures = 0;
  447. MyLog(LOGA_INFO, "Starting test 2 - bad subscribe");
  448. rc = MQTTAsync_create(&c, options.connection, "badSubscribe test",
  449. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  450. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  451. if (rc != MQTTASYNC_SUCCESS)
  452. {
  453. MQTTAsync_destroy(&c);
  454. goto exit;
  455. }
  456. opts.MQTTVersion = 4;
  457. if (options.haconnections != NULL)
  458. {
  459. opts.serverURIs = options.haconnections;
  460. opts.serverURIcount = options.hacount;
  461. }
  462. opts.onSuccess = test2_onConnect1;
  463. opts.onFailure = NULL;
  464. opts.context = c;
  465. /* Connect cleansession */
  466. opts.cleansession = 1;
  467. MyLog(LOGA_DEBUG, "Connecting");
  468. rc = MQTTAsync_connect(c, &opts);
  469. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  470. if (rc != MQTTASYNC_SUCCESS)
  471. goto exit;
  472. while (!test_finished)
  473. #if defined(_WIN32)
  474. Sleep(100);
  475. #else
  476. usleep(10000L);
  477. #endif
  478. MQTTAsync_destroy(&c);
  479. exit:
  480. MyLog(LOGA_INFO, "TEST2: test %s. %d tests run, %d failures.",
  481. (failures == 0) ? "passed" : "failed", tests, failures);
  482. write_test_result();
  483. return failures;
  484. }
  485. int main(int argc, char** argv)
  486. {
  487. int rc = 0;
  488. int (*tests[])() = {NULL, test1, test2};
  489. int i;
  490. xml = fopen("TEST-MQTT4sync.xml", "w");
  491. fprintf(xml, "<testsuite name=\"test-mqtt4sync\" tests=\"%d\">\n", (int)(ARRAY_SIZE(tests) - 1));
  492. setenv("MQTT_C_CLIENT_TRACE", "ON", 1);
  493. setenv("MQTT_C_CLIENT_TRACE_LEVEL", "ERROR", 1);
  494. getopts(argc, argv);
  495. for (i = 0; i < options.iterations; ++i)
  496. {
  497. if (options.test_no == 0)
  498. { /* run all the tests */
  499. for (options.test_no = 1; options.test_no < ARRAY_SIZE(tests); ++options.test_no)
  500. rc += tests[options.test_no](options); /* return number of failures. 0 = test succeeded */
  501. }
  502. else
  503. rc = tests[options.test_no](options); /* run just the selected test */
  504. }
  505. if (rc == 0)
  506. MyLog(LOGA_INFO, "verdict pass");
  507. else
  508. MyLog(LOGA_INFO, "verdict fail");
  509. fprintf(xml, "</testsuite>\n");
  510. fclose(xml);
  511. return rc;
  512. }