test_mqtt4sync.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. *******************************************************************************/
  17. /**
  18. * @file
  19. * MQTT 3.1.1 Tests for the synchronous Paho MQTT C client
  20. */
  21. /*
  22. #if !defined(_RTSHEADER)
  23. #include <rts.h>
  24. #endif
  25. */
  26. #include "MQTTClient.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. "tcp://localhost: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. #if defined(_WIN32) || defined(_WINDOWS)
  134. struct timeb ts;
  135. #else
  136. struct timeval ts;
  137. #endif
  138. struct tm timeinfo;
  139. if (LOGA_level == LOGA_DEBUG && options.verbose == 0)
  140. return;
  141. #if defined(_WIN32) || defined(_WINDOWS)
  142. ftime(&ts);
  143. localtime_s(&timeinfo, &ts.time);
  144. #else
  145. gettimeofday(&ts, NULL);
  146. localtime_r(&ts.tv_sec, &timeinfo);
  147. #endif
  148. strftime(msg_buf, 80, "%Y%m%d %H%M%S", &timeinfo);
  149. #if defined(_WIN32) || defined(_WINDOWS)
  150. sprintf(&msg_buf[strlen(msg_buf)], ".%.3hu ", ts.millitm);
  151. #else
  152. sprintf(&msg_buf[strlen(msg_buf)], ".%.3lu ", ts.tv_usec / 1000L);
  153. #endif
  154. va_start(args, format);
  155. vsnprintf(&msg_buf[strlen(msg_buf)], sizeof(msg_buf) - strlen(msg_buf), format, args);
  156. va_end(args);
  157. printf("%s\n", msg_buf);
  158. fflush(stdout);
  159. }
  160. #if defined(_WIN32) || defined(_WINDOWS)
  161. #define mqsleep(A) Sleep(1000*A)
  162. #define START_TIME_TYPE DWORD
  163. static DWORD start_time = 0;
  164. START_TIME_TYPE start_clock(void)
  165. {
  166. return GetTickCount();
  167. }
  168. #elif defined(AIX)
  169. #define mqsleep sleep
  170. #define START_TIME_TYPE struct timespec
  171. START_TIME_TYPE start_clock(void)
  172. {
  173. static struct timespec start;
  174. clock_gettime(CLOCK_REALTIME, &start);
  175. return start;
  176. }
  177. #else
  178. #define mqsleep sleep
  179. #define START_TIME_TYPE struct timeval
  180. /* TODO - unused - remove? static struct timeval start_time; */
  181. START_TIME_TYPE start_clock(void)
  182. {
  183. struct timeval start_time;
  184. gettimeofday(&start_time, NULL);
  185. return start_time;
  186. }
  187. #endif
  188. #if defined(_WIN32)
  189. long elapsed(START_TIME_TYPE start_time)
  190. {
  191. return GetTickCount() - start_time;
  192. }
  193. #elif defined(AIX)
  194. #define assert(a)
  195. long elapsed(struct timespec start)
  196. {
  197. struct timespec now, res;
  198. clock_gettime(CLOCK_REALTIME, &now);
  199. ntimersub(now, start, res);
  200. return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L;
  201. }
  202. #else
  203. long elapsed(START_TIME_TYPE start_time)
  204. {
  205. struct timeval now, res;
  206. gettimeofday(&now, NULL);
  207. timersub(&now, &start_time, &res);
  208. return (res.tv_sec)*1000 + (res.tv_usec)/1000;
  209. }
  210. #endif
  211. #define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d)
  212. #define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e)
  213. int tests = 0;
  214. int failures = 0;
  215. FILE* xml;
  216. START_TIME_TYPE global_start_time;
  217. char output[3000];
  218. char* cur_output = output;
  219. void write_test_result(void)
  220. {
  221. long duration = elapsed(global_start_time);
  222. fprintf(xml, " time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000);
  223. if (cur_output != output)
  224. {
  225. fprintf(xml, "%s", output);
  226. cur_output = output;
  227. }
  228. fprintf(xml, "</testcase>\n");
  229. }
  230. void myassert(char* filename, int lineno, char* description, int value, char* format, ...)
  231. {
  232. ++tests;
  233. if (!value)
  234. {
  235. va_list args;
  236. ++failures;
  237. MyLog(LOGA_INFO, "Assertion failed, file %s, line %d, description: %s\n", filename, lineno, description);
  238. va_start(args, format);
  239. vprintf(format, args);
  240. va_end(args);
  241. cur_output += sprintf(cur_output, "<failure type=\"%s\">file %s, line %d </failure>\n",
  242. description, filename, lineno);
  243. }
  244. else
  245. MyLog(LOGA_DEBUG, "Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description);
  246. }
  247. /*********************************************************************
  248. Test1: sessionPresent
  249. *********************************************************************/
  250. int test1(struct Options options)
  251. {
  252. MQTTClient c;
  253. MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer;
  254. MQTTClient_willOptions wopts = MQTTClient_willOptions_initializer;
  255. int rc = 0;
  256. char* test_topic = "C client test1";
  257. fprintf(xml, "<testcase classname=\"test1\" name=\"sessionPresent\"");
  258. global_start_time = start_clock();
  259. failures = 0;
  260. MyLog(LOGA_INFO, "Starting test 1 - sessionPresent");
  261. rc = MQTTClient_create(&c, options.connection, "sesssionPresent",
  262. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  263. assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  264. if (rc != MQTTCLIENT_SUCCESS)
  265. {
  266. MQTTClient_destroy(&c);
  267. goto exit;
  268. }
  269. opts.keepAliveInterval = 20;
  270. opts.username = "testuser";
  271. opts.password = "testpassword";
  272. opts.MQTTVersion = 4;
  273. if (options.haconnections != NULL)
  274. {
  275. opts.serverURIs = options.haconnections;
  276. opts.serverURIcount = options.hacount;
  277. }
  278. opts.will = &wopts;
  279. opts.will->message = "will message";
  280. opts.will->qos = 1;
  281. opts.will->retained = 0;
  282. opts.will->topicName = "will topic";
  283. opts.will = NULL;
  284. /* Connect cleansession */
  285. opts.cleansession = 1;
  286. MyLog(LOGA_DEBUG, "Connecting");
  287. rc = MQTTClient_connect(c, &opts);
  288. assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  289. if (rc != MQTTCLIENT_SUCCESS)
  290. goto exit;
  291. assert("Correct serverURI returned", strcmp(opts.returned.serverURI, options.connection) == 0, "serverURI was %s",
  292. opts.returned.serverURI);
  293. assert("Correct MQTTVersion returned", opts.returned.MQTTVersion == 4, "MQTTVersion was %d",
  294. opts.returned.MQTTVersion);
  295. assert("Correct sessionPresent returned", opts.returned.sessionPresent == 0, "sessionPresent was %d",
  296. opts.returned.sessionPresent);
  297. rc = MQTTClient_disconnect(c, 0);
  298. assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  299. /* Connect again, non-cleansession */
  300. opts.cleansession = 0;
  301. rc = MQTTClient_connect(c, &opts);
  302. assert("Connect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  303. assert("Correct serverURI returned", strcmp(opts.returned.serverURI, options.connection) == 0, "serverURI was %s",
  304. opts.returned.serverURI);
  305. assert("Correct MQTTVersion returned", opts.returned.MQTTVersion == 4, "MQTTVersion was %d",
  306. opts.returned.MQTTVersion);
  307. assert("Correct sessionPresent returned", opts.returned.sessionPresent == 0, "sessionPresent was %d",
  308. opts.returned.sessionPresent);
  309. rc = MQTTClient_disconnect(c, 0);
  310. assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  311. /* Connect again, non-cleansession */
  312. opts.cleansession = 0;
  313. rc = MQTTClient_connect(c, &opts);
  314. assert("Connect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  315. assert("Correct serverURI returned", strcmp(opts.returned.serverURI, options.connection) == 0, "serverURI was %s",
  316. opts.returned.serverURI);
  317. assert("Correct MQTTVersion returned", opts.returned.MQTTVersion == 4, "MQTTVersion was %d",
  318. opts.returned.MQTTVersion);
  319. assert("Correct sessionPresent returned", opts.returned.sessionPresent == 1, "sessionPresent was %d",
  320. opts.returned.sessionPresent);
  321. rc = MQTTClient_disconnect(c, 0);
  322. assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  323. MQTTClient_destroy(&c);
  324. exit:
  325. MyLog(LOGA_INFO, "TEST1: test %s. %d tests run, %d failures.",
  326. (failures == 0) ? "passed" : "failed", tests, failures);
  327. write_test_result();
  328. return failures;
  329. }
  330. /*********************************************************************
  331. Test2: 0x80 return code from subscribe
  332. *********************************************************************/
  333. volatile int test2_arrivedcount = 0;
  334. int test2_deliveryCompleted = 0;
  335. MQTTClient_message test2_pubmsg = MQTTClient_message_initializer;
  336. void test2_deliveryComplete(void* context, MQTTClient_deliveryToken dt)
  337. {
  338. ++test2_deliveryCompleted;
  339. }
  340. int test2_messageArrived(void* context, char* topicName, int topicLen, MQTTClient_message* m)
  341. {
  342. ++test2_arrivedcount;
  343. MyLog(LOGA_DEBUG, "Callback: %d message received on topic %s is %.*s.",
  344. test2_arrivedcount, topicName, m->payloadlen, (char*)(m->payload));
  345. MQTTClient_free(topicName);
  346. MQTTClient_freeMessage(&m);
  347. return 1;
  348. }
  349. int test2(struct Options options)
  350. {
  351. char* testname = "test2";
  352. int subsqos = 2;
  353. MQTTClient c;
  354. MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer;
  355. int rc = 0;
  356. char* test_topic = "C client test2";
  357. char* topics[2] = {"test_topic", "nosubscribe"};
  358. int qoss[2] = {2, 2};
  359. fprintf(xml, "<testcase classname=\"test1\" name=\"bad return code from subscribe\"");
  360. MyLog(LOGA_INFO, "Starting test 2 - bad return code from subscribe");
  361. global_start_time = start_clock();
  362. failures = 0;
  363. MQTTClient_create(&c, options.connection, "multi_threaded_sample", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  364. opts.keepAliveInterval = 20;
  365. opts.cleansession = 1;
  366. opts.MQTTVersion = 4;
  367. if (options.haconnections != NULL)
  368. {
  369. opts.serverURIs = options.haconnections;
  370. opts.serverURIcount = options.hacount;
  371. }
  372. rc = MQTTClient_setCallbacks(c, NULL, NULL, test2_messageArrived, test2_deliveryComplete);
  373. assert("Good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  374. MyLog(LOGA_DEBUG, "Connecting");
  375. rc = MQTTClient_connect(c, &opts);
  376. assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  377. if (rc != MQTTCLIENT_SUCCESS)
  378. goto exit;
  379. assert("Correct serverURI returned", strcmp(opts.returned.serverURI, options.connection) == 0, "serverURI was %s",
  380. opts.returned.serverURI);
  381. assert("Correct MQTTVersion returned", opts.returned.MQTTVersion == 4, "MQTTVersion was %d",
  382. opts.returned.MQTTVersion);
  383. assert("Correct sessionPresent returned", opts.returned.sessionPresent == 0, "sessionPresent was %d",
  384. opts.returned.sessionPresent);
  385. rc = MQTTClient_subscribe(c, test_topic, subsqos);
  386. assert("Good rc from subscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  387. rc = MQTTClient_subscribe(c, "nosubscribe", 2);
  388. assert("0x80 from subscribe", rc == 0x80, "rc was %d", rc);
  389. rc = MQTTClient_subscribeMany(c, 2, topics, qoss);
  390. assert("Good rc from subscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  391. assert("Correct returned qos from subscribe", qoss[0] == 2, "qos 0 was %d", qoss[0]);
  392. assert("Correct returned qos from subscribe", qoss[1] == 0x80, "qos 0 was %d", qoss[0]);
  393. rc = MQTTClient_unsubscribe(c, test_topic);
  394. assert("Unsubscribe successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  395. rc = MQTTClient_disconnect(c, 0);
  396. assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  397. MQTTClient_destroy(&c);
  398. exit:
  399. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  400. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  401. write_test_result();
  402. return failures;
  403. }
  404. int main(int argc, char** argv)
  405. {
  406. int rc = 0;
  407. int (*tests[])() = {NULL, test1, test2};
  408. int i;
  409. xml = fopen("TEST-MQTT4sync.xml", "w");
  410. fprintf(xml, "<testsuite name=\"test-mqtt4sync\" tests=\"%d\">\n", (int)(ARRAY_SIZE(tests) - 1));
  411. setenv("MQTT_C_CLIENT_TRACE", "ON", 1);
  412. setenv("MQTT_C_CLIENT_TRACE_LEVEL", "ERROR", 1);
  413. getopts(argc, argv);
  414. for (i = 0; i < options.iterations; ++i)
  415. {
  416. if (options.test_no == 0)
  417. { /* run all the tests */
  418. for (options.test_no = 1; options.test_no < ARRAY_SIZE(tests); ++options.test_no)
  419. rc += tests[options.test_no](options); /* return number of failures. 0 = test succeeded */
  420. }
  421. else
  422. rc = tests[options.test_no](options); /* run just the selected test */
  423. }
  424. if (rc == 0)
  425. MyLog(LOGA_INFO, "verdict pass");
  426. else
  427. MyLog(LOGA_INFO, "verdict fail");
  428. fprintf(xml, "</testsuite>\n");
  429. fclose(xml);
  430. return rc;
  431. }