test_connect_destroy.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2020 IBM Corp. and others
  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. * Michal Kasperek - initial API and implementation and/or initial documentation
  15. *******************************************************************************/
  16. /**
  17. * @file
  18. * Test for issue 675: crash when invoking MQTTClient_destroy during MQTTClient_connect
  19. */
  20. #include "MQTTClient.h"
  21. #include "Thread.h"
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #if !defined(_WINDOWS)
  25. #include <sys/time.h>
  26. #include <sys/socket.h>
  27. #include <unistd.h>
  28. #include <errno.h>
  29. #define WINAPI
  30. #else
  31. #include <windows.h>
  32. #define setenv(a, b, c) _putenv_s(a, b)
  33. #endif
  34. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  35. void usage(void)
  36. {
  37. printf("help!!\n");
  38. exit(EXIT_FAILURE);
  39. }
  40. struct Options
  41. {
  42. char* connection; /**< connection to system under test. */
  43. char** haconnections;
  44. int hacount;
  45. int verbose;
  46. int test_no;
  47. int MQTTVersion;
  48. int iterations;
  49. } options =
  50. {
  51. "localhost:1883",
  52. NULL,
  53. 0,
  54. 0,
  55. 0,
  56. MQTTVERSION_DEFAULT,
  57. 100,
  58. };
  59. void getopts(int argc, char** argv)
  60. {
  61. int count = 1;
  62. while (count < argc)
  63. {
  64. if (strcmp(argv[count], "--test_no") == 0)
  65. {
  66. if (++count < argc)
  67. options.test_no = atoi(argv[count]);
  68. else
  69. usage();
  70. }
  71. else if (strcmp(argv[count], "--connection") == 0)
  72. {
  73. if (++count < argc)
  74. {
  75. options.connection = argv[count];
  76. printf("\nSetting connection to %s\n", options.connection);
  77. }
  78. else
  79. usage();
  80. }
  81. else if (strcmp(argv[count], "--haconnections") == 0)
  82. {
  83. if (++count < argc)
  84. {
  85. char* tok = strtok(argv[count], " ");
  86. options.hacount = 0;
  87. options.haconnections = malloc(sizeof(char*) * 5);
  88. while (tok)
  89. {
  90. options.haconnections[options.hacount] = malloc(strlen(tok) + 1);
  91. strcpy(options.haconnections[options.hacount], tok);
  92. options.hacount++;
  93. tok = strtok(NULL, " ");
  94. }
  95. }
  96. else
  97. usage();
  98. }
  99. else if (strcmp(argv[count], "--MQTTversion") == 0)
  100. {
  101. if (++count < argc)
  102. {
  103. options.MQTTVersion = atoi(argv[count]);
  104. printf("setting MQTT version to %d\n", options.MQTTVersion);
  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. void MySleep(long milliseconds)
  161. {
  162. #if defined(WIN32) || defined(WIN64)
  163. Sleep(milliseconds);
  164. #else
  165. usleep(milliseconds*1000);
  166. #endif
  167. }
  168. #if defined(WIN32) || defined(_WINDOWS)
  169. #define START_TIME_TYPE DWORD
  170. static DWORD start_time = 0;
  171. START_TIME_TYPE start_clock(void)
  172. {
  173. return GetTickCount();
  174. }
  175. #elif defined(AIX)
  176. #define START_TIME_TYPE struct timespec
  177. START_TIME_TYPE start_clock(void)
  178. {
  179. static struct timespec start;
  180. clock_gettime(CLOCK_REALTIME, &start);
  181. return start;
  182. }
  183. #else
  184. #define START_TIME_TYPE struct timeval
  185. /* TODO - unused - remove? static struct timeval start_time; */
  186. START_TIME_TYPE start_clock(void)
  187. {
  188. struct timeval start_time;
  189. gettimeofday(&start_time, NULL);
  190. return start_time;
  191. }
  192. #endif
  193. #if defined(WIN32)
  194. long elapsed(START_TIME_TYPE start_time)
  195. {
  196. return GetTickCount() - start_time;
  197. }
  198. #elif defined(AIX)
  199. #define assert(a)
  200. long elapsed(struct timespec start)
  201. {
  202. struct timespec now, res;
  203. clock_gettime(CLOCK_REALTIME, &now);
  204. ntimersub(now, start, res);
  205. return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L;
  206. }
  207. #else
  208. long elapsed(START_TIME_TYPE start_time)
  209. {
  210. struct timeval now, res;
  211. gettimeofday(&now, NULL);
  212. timersub(&now, &start_time, &res);
  213. return (res.tv_sec)*1000 + (res.tv_usec)/1000;
  214. }
  215. #endif
  216. #define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d)
  217. #define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e)
  218. int tests = 0;
  219. int failures = 0;
  220. FILE* xml;
  221. START_TIME_TYPE global_start_time;
  222. char output[3000];
  223. char* cur_output = output;
  224. void write_test_result(void)
  225. {
  226. long duration = elapsed(global_start_time);
  227. fprintf(xml, " time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000);
  228. if (cur_output != output)
  229. {
  230. fprintf(xml, "%s", output);
  231. cur_output = output;
  232. }
  233. fprintf(xml, "</testcase>\n");
  234. }
  235. void myassert(char* filename, int lineno, char* description, int value, char* format, ...)
  236. {
  237. ++tests;
  238. if (!value)
  239. {
  240. va_list args;
  241. ++failures;
  242. MyLog(LOGA_INFO, "Assertion failed, file %s, line %d, description: %s\n", filename, lineno, description);
  243. va_start(args, format);
  244. vprintf(format, args);
  245. va_end(args);
  246. cur_output += sprintf(cur_output, "<failure type=\"%s\">file %s, line %d </failure>\n",
  247. description, filename, lineno);
  248. }
  249. else
  250. MyLog(LOGA_DEBUG, "Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description);
  251. }
  252. void lock_mutex(mutex_type amutex)
  253. {
  254. int rc = Thread_lock_mutex(amutex);
  255. if (rc != 0)
  256. MyLog(LOGA_INFO, "Error %s locking mutex", strerror(rc));
  257. }
  258. void unlock_mutex(mutex_type amutex)
  259. {
  260. int rc = Thread_unlock_mutex(amutex);
  261. if (rc != 0)
  262. MyLog(LOGA_INFO, "Error %s unlocking mutex", strerror(rc));
  263. }
  264. /*********************************************************************
  265. Test1: destroy MQTT client while connectig
  266. *********************************************************************/
  267. struct thread_parms
  268. {
  269. MQTTClient* c;
  270. };
  271. thread_return_type WINAPI test1_destroy(void* n)
  272. {
  273. MySleep(rand() % 500);
  274. struct thread_parms *parms = n;
  275. MQTTClient* c = parms->c;
  276. MQTTClient_destroy(c);
  277. return 0;
  278. }
  279. int test1(struct Options options)
  280. {
  281. srand((unsigned int)time(0));
  282. MQTTClient c;
  283. MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer;
  284. MQTTClient_willOptions wopts = MQTTClient_willOptions_initializer;
  285. int rc = 0;
  286. char* test_topic = "C client test1";
  287. fprintf(xml, "<testcase classname=\"test1\" name=\"destroy mqtt while connecting\"");
  288. global_start_time = start_clock();
  289. failures = 0;
  290. MyLog(LOGA_INFO, "Starting test 1 - execute destroy while connecting");
  291. rc = MQTTClient_create(&c, options.connection, "connect crash test",
  292. MQTTCLIENT_PERSISTENCE_NONE, NULL);
  293. assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  294. if (rc != MQTTCLIENT_SUCCESS)
  295. {
  296. MQTTClient_destroy(&c);
  297. goto exit;
  298. }
  299. opts.connectTimeout = 30;
  300. opts.keepAliveInterval = 20;
  301. opts.cleansession = 1;
  302. opts.username = "testuser";
  303. opts.password = "testpassword";
  304. opts.MQTTVersion = options.MQTTVersion;
  305. if (options.haconnections != NULL)
  306. {
  307. opts.serverURIs = options.haconnections;
  308. opts.serverURIcount = options.hacount;
  309. }
  310. opts.will = &wopts;
  311. opts.will->message = "will message";
  312. opts.will->qos = 1;
  313. opts.will->retained = 0;
  314. opts.will->topicName = "will topic";
  315. opts.will = NULL;
  316. struct thread_parms parms = {&c};
  317. Thread_start(test1_destroy, (void*)&parms);
  318. MQTTClient_connect(c, &opts);
  319. MySleep(1000);
  320. exit:
  321. MyLog(LOGA_INFO, "TEST1: test %s. %d tests run, %d failures.",
  322. (failures == 0) ? "passed" : "failed", tests, failures);
  323. write_test_result();
  324. return failures;
  325. }
  326. int main(int argc, char** argv)
  327. {
  328. int rc = 0;
  329. int (*tests[])() = {NULL, test1};
  330. int i;
  331. xml = fopen("TEST-test2.xml", "w");
  332. fprintf(xml, "<testsuite name=\"test1\" tests=\"%d\">\n", (int)(ARRAY_SIZE(tests) - 1));
  333. setenv("MQTT_C_CLIENT_TRACE", "ON", 1);
  334. setenv("MQTT_C_CLIENT_TRACE_LEVEL", "ERROR", 0);
  335. getopts(argc, argv);
  336. for (i = 0; i < options.iterations; ++i)
  337. {
  338. if (options.test_no == 0)
  339. { /* run all the tests */
  340. for (options.test_no = 1; options.test_no < ARRAY_SIZE(tests); ++options.test_no)
  341. rc += tests[options.test_no](options); /* return number of failures. 0 = test succeeded */
  342. options.test_no = 0;
  343. }
  344. else
  345. rc = tests[options.test_no](options); /* run just the selected test */
  346. }
  347. if (rc == 0)
  348. MyLog(LOGA_INFO, "verdict pass");
  349. else
  350. MyLog(LOGA_INFO, "verdict fail");
  351. fprintf(xml, "</testsuite>\n");
  352. fclose(xml);
  353. return rc;
  354. }