test_mqtt4sync.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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 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://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. void write_test_result(void)
  207. {
  208. long duration = elapsed(global_start_time);
  209. fprintf(xml, " time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000);
  210. if (cur_output != output)
  211. {
  212. fprintf(xml, "%s", output);
  213. cur_output = output;
  214. }
  215. fprintf(xml, "</testcase>\n");
  216. }
  217. void myassert(char* filename, int lineno, char* description, int value, char* format, ...)
  218. {
  219. ++tests;
  220. if (!value)
  221. {
  222. va_list args;
  223. ++failures;
  224. MyLog(LOGA_INFO, "Assertion failed, file %s, line %d, description: %s\n", filename, lineno, description);
  225. va_start(args, format);
  226. vprintf(format, args);
  227. va_end(args);
  228. cur_output += sprintf(cur_output, "<failure type=\"%s\">file %s, line %d </failure>\n",
  229. description, filename, lineno);
  230. }
  231. else
  232. MyLog(LOGA_DEBUG, "Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description);
  233. }
  234. /*********************************************************************
  235. Test1: sessionPresent
  236. *********************************************************************/
  237. int test1(struct Options options)
  238. {
  239. MQTTClient c;
  240. MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer;
  241. MQTTClient_willOptions wopts = MQTTClient_willOptions_initializer;
  242. int rc = 0;
  243. char* test_topic = "C client test1";
  244. fprintf(xml, "<testcase classname=\"test1\" name=\"sessionPresent\"");
  245. global_start_time = start_clock();
  246. failures = 0;
  247. MyLog(LOGA_INFO, "Starting test 1 - sessionPresent");
  248. rc = MQTTClient_create(&c, options.connection, "sesssionPresent",
  249. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  250. assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  251. if (rc != MQTTCLIENT_SUCCESS)
  252. {
  253. MQTTClient_destroy(&c);
  254. goto exit;
  255. }
  256. opts.keepAliveInterval = 20;
  257. opts.username = "testuser";
  258. opts.password = "testpassword";
  259. opts.MQTTVersion = 4;
  260. if (options.haconnections != NULL)
  261. {
  262. opts.serverURIs = options.haconnections;
  263. opts.serverURIcount = options.hacount;
  264. }
  265. opts.will = &wopts;
  266. opts.will->message = "will message";
  267. opts.will->qos = 1;
  268. opts.will->retained = 0;
  269. opts.will->topicName = "will topic";
  270. opts.will = NULL;
  271. /* Connect cleansession */
  272. opts.cleansession = 1;
  273. MyLog(LOGA_DEBUG, "Connecting");
  274. rc = MQTTClient_connect(c, &opts);
  275. assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  276. if (rc != MQTTCLIENT_SUCCESS)
  277. goto exit;
  278. assert("Correct serverURI returned", strcmp(opts.returned.serverURI, options.connection) == 0, "serverURI was %s",
  279. opts.returned.serverURI);
  280. assert("Correct MQTTVersion returned", opts.returned.MQTTVersion == 4, "MQTTVersion was %d",
  281. opts.returned.MQTTVersion);
  282. assert("Correct sessionPresent returned", opts.returned.sessionPresent == 0, "sessionPresent was %d",
  283. opts.returned.sessionPresent);
  284. rc = MQTTClient_disconnect(c, 0);
  285. assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  286. /* Connect again, non-cleansession */
  287. opts.cleansession = 0;
  288. rc = MQTTClient_connect(c, &opts);
  289. assert("Connect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  290. assert("Correct serverURI returned", strcmp(opts.returned.serverURI, options.connection) == 0, "serverURI was %s",
  291. opts.returned.serverURI);
  292. assert("Correct MQTTVersion returned", opts.returned.MQTTVersion == 4, "MQTTVersion was %d",
  293. opts.returned.MQTTVersion);
  294. assert("Correct sessionPresent returned", opts.returned.sessionPresent == 0, "sessionPresent was %d",
  295. opts.returned.sessionPresent);
  296. rc = MQTTClient_disconnect(c, 0);
  297. assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  298. /* Connect again, non-cleansession */
  299. opts.cleansession = 0;
  300. rc = MQTTClient_connect(c, &opts);
  301. assert("Connect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  302. assert("Correct serverURI returned", strcmp(opts.returned.serverURI, options.connection) == 0, "serverURI was %s",
  303. opts.returned.serverURI);
  304. assert("Correct MQTTVersion returned", opts.returned.MQTTVersion == 4, "MQTTVersion was %d",
  305. opts.returned.MQTTVersion);
  306. assert("Correct sessionPresent returned", opts.returned.sessionPresent == 1, "sessionPresent was %d",
  307. opts.returned.sessionPresent);
  308. rc = MQTTClient_disconnect(c, 0);
  309. assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  310. MQTTClient_destroy(&c);
  311. exit:
  312. MyLog(LOGA_INFO, "TEST1: test %s. %d tests run, %d failures.",
  313. (failures == 0) ? "passed" : "failed", tests, failures);
  314. write_test_result();
  315. return failures;
  316. }
  317. /*********************************************************************
  318. Test2: 0x80 return code from subscribe
  319. *********************************************************************/
  320. volatile int test2_arrivedcount = 0;
  321. int test2_deliveryCompleted = 0;
  322. MQTTClient_message test2_pubmsg = MQTTClient_message_initializer;
  323. void test2_deliveryComplete(void* context, MQTTClient_deliveryToken dt)
  324. {
  325. ++test2_deliveryCompleted;
  326. }
  327. int test2_messageArrived(void* context, char* topicName, int topicLen, MQTTClient_message* m)
  328. {
  329. ++test2_arrivedcount;
  330. MyLog(LOGA_DEBUG, "Callback: %d message received on topic %s is %.*s.",
  331. test2_arrivedcount, topicName, m->payloadlen, (char*)(m->payload));
  332. MQTTClient_free(topicName);
  333. MQTTClient_freeMessage(&m);
  334. return 1;
  335. }
  336. int test2(struct Options options)
  337. {
  338. char* testname = "test2";
  339. int subsqos = 2;
  340. MQTTClient c;
  341. MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer;
  342. int rc = 0;
  343. char* test_topic = "C client test2";
  344. char* topics[2] = {"test_topic", "nosubscribe"};
  345. int qoss[2] = {2, 2};
  346. fprintf(xml, "<testcase classname=\"test1\" name=\"bad return code from subscribe\"");
  347. MyLog(LOGA_INFO, "Starting test 2 - bad return code from subscribe");
  348. global_start_time = start_clock();
  349. failures = 0;
  350. MQTTClient_create(&c, options.connection, "multi_threaded_sample", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  351. opts.keepAliveInterval = 20;
  352. opts.cleansession = 1;
  353. opts.MQTTVersion = 4;
  354. if (options.haconnections != NULL)
  355. {
  356. opts.serverURIs = options.haconnections;
  357. opts.serverURIcount = options.hacount;
  358. }
  359. rc = MQTTClient_setCallbacks(c, NULL, NULL, test2_messageArrived, test2_deliveryComplete);
  360. assert("Good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  361. MyLog(LOGA_DEBUG, "Connecting");
  362. rc = MQTTClient_connect(c, &opts);
  363. assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  364. if (rc != MQTTCLIENT_SUCCESS)
  365. goto exit;
  366. assert("Correct serverURI returned", strcmp(opts.returned.serverURI, options.connection) == 0, "serverURI was %s",
  367. opts.returned.serverURI);
  368. assert("Correct MQTTVersion returned", opts.returned.MQTTVersion == 4, "MQTTVersion was %d",
  369. opts.returned.MQTTVersion);
  370. assert("Correct sessionPresent returned", opts.returned.sessionPresent == 0, "sessionPresent was %d",
  371. opts.returned.sessionPresent);
  372. rc = MQTTClient_subscribe(c, test_topic, subsqos);
  373. assert("Good rc from subscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  374. rc = MQTTClient_subscribe(c, "nosubscribe", 2);
  375. assert("0x80 from subscribe", rc == 0x80, "rc was %d", rc);
  376. rc = MQTTClient_subscribeMany(c, 2, topics, qoss);
  377. assert("Good rc from subscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  378. assert("Correct returned qos from subscribe", qoss[0] == 2, "qos 0 was %d", qoss[0]);
  379. assert("Correct returned qos from subscribe", qoss[1] == 0x80, "qos 0 was %d", qoss[0]);
  380. rc = MQTTClient_unsubscribe(c, test_topic);
  381. assert("Unsubscribe successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  382. rc = MQTTClient_disconnect(c, 0);
  383. assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  384. MQTTClient_destroy(&c);
  385. exit:
  386. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  387. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  388. write_test_result();
  389. return failures;
  390. }
  391. int main(int argc, char** argv)
  392. {
  393. int rc = 0;
  394. int (*tests[])() = {NULL, test1, test2};
  395. int i;
  396. xml = fopen("TEST-MQTT4sync.xml", "w");
  397. fprintf(xml, "<testsuite name=\"test-mqtt4sync\" tests=\"%d\">\n", (int)(ARRAY_SIZE(tests) - 1));
  398. setenv("MQTT_C_CLIENT_TRACE", "ON", 1);
  399. setenv("MQTT_C_CLIENT_TRACE_LEVEL", "ERROR", 1);
  400. getopts(argc, argv);
  401. for (i = 0; i < options.iterations; ++i)
  402. {
  403. if (options.test_no == 0)
  404. { /* run all the tests */
  405. for (options.test_no = 1; options.test_no < ARRAY_SIZE(tests); ++options.test_no)
  406. rc += tests[options.test_no](options); /* return number of failures. 0 = test succeeded */
  407. }
  408. else
  409. rc = tests[options.test_no](options); /* run just the selected test */
  410. }
  411. if (rc == 0)
  412. MyLog(LOGA_INFO, "verdict pass");
  413. else
  414. MyLog(LOGA_INFO, "verdict fail");
  415. fprintf(xml, "</testsuite>\n");
  416. fclose(xml);
  417. return rc;
  418. }