test5.c 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740
  1. /*******************************************************************************
  2. * Copyright (c) 2012, 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. * Allan Stockdill-Mander - initial API and implementation and/or initial documentation
  15. * Ian Craggs - fix Windows includes
  16. *******************************************************************************/
  17. /**
  18. * @file
  19. * SSL tests for the Eclipse Paho Asynchronous MQTT C client
  20. */
  21. #include "MQTTAsync.h"
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include "Thread.h"
  25. #if defined(_WINDOWS)
  26. #include <windows.h>
  27. #include <openssl/applink.c>
  28. #define MAXHOSTNAMELEN 256
  29. #define snprintf _snprintf
  30. #else
  31. #include <sys/time.h>
  32. #include <sys/socket.h>
  33. #include <unistd.h>
  34. #include <errno.h>
  35. #endif
  36. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  37. void usage(void)
  38. {
  39. printf("Options:\n");
  40. printf("\t--test_no <test_no> - Run test number <test_no>\n");
  41. printf("\t--hostname <hostname> - Connect to <hostname> for tests\n");
  42. printf("\t--client_key <key_file> - Use <key_file> as the client certificate for SSL authentication\n");
  43. printf("\t--client_key_pass <password> - Use <password> to access the private key in the client certificate\n");
  44. printf("\t--server_key <key_file> - Use <key_file> as the trusted certificate for server\n");
  45. printf("\t--verbose - Enable verbose output \n");
  46. printf("\t--help - This help output\n");
  47. exit(EXIT_FAILURE);
  48. }
  49. struct Options
  50. {
  51. char connection[100]; /**< connection to system under test. */
  52. char mutual_auth_connection[100]; /**< connection to system under test. */
  53. char nocert_mutual_auth_connection[100];
  54. char server_auth_connection[100];
  55. char anon_connection[100];
  56. char psk_connection[100];
  57. char* client_key_file;
  58. char* client_key_pass;
  59. char* server_key_file;
  60. char* client_private_key_file;
  61. char* capath;
  62. int verbose;
  63. int test_no;
  64. int size;
  65. int websockets;
  66. int message_count;
  67. int start_port;
  68. } options =
  69. {
  70. "ssl://localhost:18883",
  71. "mqtts://localhost:18884",
  72. "ssl://localhost:18887",
  73. "mqtts://localhost:18885",
  74. "ssl://localhost:18886",
  75. "mqtts://localhost:18888",
  76. NULL, // "../../../test/ssl/client.pem",
  77. NULL,
  78. NULL, // "../../../test/ssl/test-root-ca.crt",
  79. NULL, // "../../../test/ssl/capath",
  80. NULL,
  81. 0,
  82. 0,
  83. 5000000,
  84. 0,
  85. 3,
  86. 18883,
  87. };
  88. typedef struct
  89. {
  90. MQTTAsync client;
  91. char clientid[24];
  92. char topic[100];
  93. int maxmsgs;
  94. int rcvdmsgs[3];
  95. int sentmsgs[3];
  96. int testFinished;
  97. int subscribed;
  98. } AsyncTestClient;
  99. #define AsyncTestClient_initializer {NULL, "\0", "\0", 0, {0, 0, 0}, {0, 0, 0}, 0, 0}
  100. void getopts(int argc, char** argv)
  101. {
  102. int count = 1;
  103. while (count < argc)
  104. {
  105. if (strcmp(argv[count], "--help") == 0)
  106. {
  107. usage();
  108. }
  109. else if (strcmp(argv[count], "--test_no") == 0)
  110. {
  111. if (++count < argc)
  112. options.test_no = atoi(argv[count]);
  113. else
  114. usage();
  115. }
  116. else if (strcmp(argv[count], "--client_key") == 0)
  117. {
  118. if (++count < argc)
  119. options.client_key_file = argv[count];
  120. else
  121. usage();
  122. }
  123. else if (strcmp(argv[count], "--client_key_pass") == 0)
  124. {
  125. if (++count < argc)
  126. options.client_key_pass = argv[count];
  127. else
  128. usage();
  129. }
  130. else if (strcmp(argv[count], "--server_key") == 0)
  131. {
  132. if (++count < argc)
  133. options.server_key_file = argv[count];
  134. else
  135. usage();
  136. }
  137. else if (strcmp(argv[count], "--capath") == 0)
  138. {
  139. if (++count < argc)
  140. options.capath = argv[count];
  141. else
  142. usage();
  143. }
  144. else if (strcmp(argv[count], "--verbose") == 0)
  145. {
  146. options.verbose = 1;
  147. printf("\nSetting verbose on\n");
  148. }
  149. else if (strcmp(argv[count], "--hostname") == 0)
  150. {
  151. if (++count < argc)
  152. {
  153. char* prefix = (options.websockets) ? "wss" : "ssl";
  154. sprintf(options.connection, "%s://%s:%d", prefix, argv[count],
  155. options.start_port);
  156. printf("Setting connection to %s\n", options.connection);
  157. sprintf(options.mutual_auth_connection, "%s://%s:%d", prefix, argv[count],
  158. options.start_port+1);
  159. printf("Setting mutual_auth_connection to %s\n", options.mutual_auth_connection);
  160. sprintf(options.nocert_mutual_auth_connection, "%s://%s:%d", prefix,
  161. argv[count], options.start_port+4);
  162. printf("Setting nocert_mutual_auth_connection to %s\n",
  163. options.nocert_mutual_auth_connection);
  164. sprintf(options.server_auth_connection, "%s://%s:%d", prefix, argv[count],
  165. options.start_port+2);
  166. printf("Setting server_auth_connection to %s\n", options.server_auth_connection);
  167. sprintf(options.anon_connection, "%s://%s:%d", prefix, argv[count],
  168. options.start_port+3);
  169. printf("Setting anon_connection to %s\n", options.anon_connection);
  170. sprintf(options.psk_connection, "%s://%s:%d", prefix, argv[count],
  171. options.start_port+5);
  172. printf("Setting psk_connection to %s\n", options.psk_connection);
  173. }
  174. else
  175. usage();
  176. }
  177. else if (strcmp(argv[count], "--ws") == 0)
  178. {
  179. options.websockets = 1;
  180. printf("\nSetting websockets on\n");
  181. }
  182. else if (strcmp(argv[count], "--size") == 0)
  183. {
  184. if (++count < argc)
  185. {
  186. options.size = atoi(argv[count]);
  187. printf("\nSetting size to %d\n", options.size);
  188. }else
  189. usage();
  190. }
  191. else if (strcmp(argv[count], "--port") == 0)
  192. {
  193. if (++count < argc)
  194. {
  195. options.start_port = atoi(argv[count]);
  196. printf("\nSetting start_port to %d\n", options.start_port);
  197. }else
  198. usage();
  199. }
  200. else if (strcmp(argv[count], "--count") == 0)
  201. {
  202. if (++count < argc)
  203. {
  204. options.message_count = atoi(argv[count]);
  205. printf("\nSetting message count to %d\n", options.message_count);
  206. }else
  207. usage();
  208. }
  209. else
  210. printf("Unrecognized option %s\n", argv[count]);
  211. count++;
  212. }
  213. }
  214. #define LOGA_DEBUG 0
  215. #define LOGA_INFO 1
  216. #include <stdarg.h>
  217. #include <time.h>
  218. #include <sys/timeb.h>
  219. void MyLog(int LOGA_level, char* format, ...)
  220. {
  221. static char msg_buf[256];
  222. va_list args;
  223. #if defined(_WIN32) || defined(_WINDOWS)
  224. struct timeb ts;
  225. #else
  226. struct timeval ts;
  227. #endif
  228. struct tm timeinfo;
  229. if (LOGA_level == LOGA_DEBUG && options.verbose == 0)
  230. return;
  231. #if defined(_WIN32) || defined(_WINDOWS)
  232. ftime(&ts);
  233. localtime_s(&timeinfo, &ts.time);
  234. #else
  235. gettimeofday(&ts, NULL);
  236. localtime_r(&ts.tv_sec, &timeinfo);
  237. #endif
  238. strftime(msg_buf, 80, "%Y%m%d %H%M%S", &timeinfo);
  239. #if defined(_WIN32) || defined(_WINDOWS)
  240. sprintf(&msg_buf[strlen(msg_buf)], ".%.3hu ", ts.millitm);
  241. #else
  242. sprintf(&msg_buf[strlen(msg_buf)], ".%.3lu ", ts.tv_usec / 1000L);
  243. #endif
  244. va_start(args, format);
  245. vsnprintf(&msg_buf[strlen(msg_buf)], sizeof(msg_buf) - strlen(msg_buf), format, args);
  246. va_end(args);
  247. printf("%s\n", msg_buf);
  248. fflush(stdout);
  249. }
  250. #if defined(_WIN32) || defined(_WINDOWS)
  251. #define mqsleep(A) Sleep(1000*A)
  252. #define START_TIME_TYPE DWORD
  253. static DWORD start_time = 0;
  254. START_TIME_TYPE start_clock(void)
  255. {
  256. return GetTickCount();
  257. }
  258. #elif defined(AIX)
  259. #define mqsleep sleep
  260. #define START_TIME_TYPE struct timespec
  261. START_TIME_TYPE start_clock(void)
  262. {
  263. static struct timespec start;
  264. clock_gettime(CLOCK_REALTIME, &start);
  265. return start;
  266. }
  267. #else
  268. #define mqsleep sleep
  269. #define START_TIME_TYPE struct timeval
  270. /* TODO - unused - remove? static struct timeval start_time; */
  271. START_TIME_TYPE start_clock(void)
  272. {
  273. struct timeval start_time;
  274. gettimeofday(&start_time, NULL);
  275. return start_time;
  276. }
  277. #endif
  278. #if defined(_WIN32)
  279. long elapsed(START_TIME_TYPE start_time)
  280. {
  281. return GetTickCount() - start_time;
  282. }
  283. #elif defined(AIX)
  284. #define assert(a)
  285. long elapsed(struct timespec start)
  286. {
  287. struct timespec now, res;
  288. clock_gettime(CLOCK_REALTIME, &now);
  289. ntimersub(now, start, res);
  290. return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L;
  291. }
  292. #else
  293. long elapsed(START_TIME_TYPE start_time)
  294. {
  295. struct timeval now, res;
  296. gettimeofday(&now, NULL);
  297. timersub(&now, &start_time, &res);
  298. return (res.tv_sec) * 1000 + (res.tv_usec) / 1000;
  299. }
  300. #endif
  301. #define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d)
  302. #define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e)
  303. #define MAXMSGS 30;
  304. int tests = 0;
  305. int failures = 0;
  306. FILE* xml;
  307. START_TIME_TYPE global_start_time;
  308. char output[3000];
  309. char* cur_output = output;
  310. void write_test_result(void)
  311. {
  312. long duration = elapsed(global_start_time);
  313. fprintf(xml, " time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000);
  314. if (cur_output != output)
  315. {
  316. fprintf(xml, "%s", output);
  317. cur_output = output;
  318. }
  319. fprintf(xml, "</testcase>\n");
  320. }
  321. void myassert(char* filename, int lineno, char* description, int value,
  322. char* format, ...)
  323. {
  324. ++tests;
  325. if (!value)
  326. {
  327. va_list args;
  328. ++failures;
  329. printf("Assertion failed, file %s, line %d, description: %s", filename,
  330. lineno, description);
  331. va_start(args, format);
  332. vprintf(format, args);
  333. va_end(args);
  334. cur_output += sprintf(cur_output, "<failure type=\"%s\">file %s, line %d </failure>\n",
  335. description, filename, lineno);
  336. }
  337. else
  338. MyLog(LOGA_DEBUG,
  339. "Assertion succeeded, file %s, line %d, description: %s",
  340. filename, lineno, description);
  341. }
  342. /*********************************************************************
  343. Test: multi-threaded client using callbacks
  344. *********************************************************************/
  345. volatile int multiThread_arrivedcount = 0;
  346. int multiThread_deliveryCompleted = 0;
  347. MQTTAsync_message multiThread_pubmsg = MQTTAsync_message_initializer;
  348. void multiThread_deliveryComplete(void* context, MQTTAsync_token dt)
  349. {
  350. ++multiThread_deliveryCompleted;
  351. }
  352. int multiThread_messageArrived(void* context, char* topicName, int topicLen,
  353. MQTTAsync_message* m)
  354. {
  355. ++multiThread_arrivedcount;
  356. MyLog(LOGA_DEBUG, "Callback: %d message received on topic %s is %.*s.",
  357. multiThread_arrivedcount, topicName, m->payloadlen,
  358. (char*) (m->payload));
  359. if (multiThread_pubmsg.payloadlen != m->payloadlen || memcmp(m->payload,
  360. multiThread_pubmsg.payload, m->payloadlen) != 0)
  361. {
  362. failures++;
  363. MyLog(LOGA_INFO, "Error: wrong data received lengths %d %d\n",
  364. multiThread_pubmsg.payloadlen, m->payloadlen);
  365. }
  366. MQTTAsync_free(topicName);
  367. MQTTAsync_freeMessage(&m);
  368. return 1;
  369. }
  370. void sendAndReceive(MQTTAsync* c, int qos, char* test_topic)
  371. {
  372. MQTTAsync_responseOptions ropts;
  373. int i = 0;
  374. int iterations = 50;
  375. int rc = 0;
  376. int wait_seconds = 0;
  377. multiThread_deliveryCompleted = 0;
  378. multiThread_arrivedcount = 0;
  379. MyLog(LOGA_DEBUG, "%d messages at QoS %d", iterations, qos);
  380. multiThread_pubmsg.payload
  381. = "a much longer message that we can shorten to the extent that we need to";
  382. multiThread_pubmsg.payloadlen = 27;
  383. multiThread_pubmsg.qos = qos;
  384. multiThread_pubmsg.retained = 0;
  385. for (i = 1; i <= iterations; ++i)
  386. {
  387. if (i % 10 == 0)
  388. rc = MQTTAsync_send(c, test_topic, multiThread_pubmsg.payloadlen,
  389. multiThread_pubmsg.payload, multiThread_pubmsg.qos,
  390. multiThread_pubmsg.retained, NULL);
  391. else
  392. rc = MQTTAsync_sendMessage(c, test_topic, &multiThread_pubmsg,
  393. &ropts);
  394. assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  395. #if defined(_WIN32)
  396. Sleep(100);
  397. #else
  398. usleep(100000L);
  399. #endif
  400. wait_seconds = 10;
  401. while ((multiThread_arrivedcount < i) && (wait_seconds-- > 0))
  402. {
  403. MyLog(LOGA_DEBUG, "Arrived %d count %d", multiThread_arrivedcount,
  404. i);
  405. #if defined(_WIN32)
  406. Sleep(1000);
  407. #else
  408. usleep(1000000L);
  409. #endif
  410. }
  411. assert("Message Arrived", wait_seconds > 0,
  412. "Time out waiting for message %d\n", i );
  413. }
  414. if (qos > 0)
  415. {
  416. /* MQ Telemetry can send a message to a subscriber before the server has
  417. completed the QoS 2 handshake with the publisher. For QoS 1 and 2,
  418. allow time for the final delivery complete callback before checking
  419. that all expected callbacks have been made */
  420. wait_seconds = 10;
  421. while ((multiThread_deliveryCompleted < iterations) && (wait_seconds--
  422. > 0))
  423. {
  424. MyLog(LOGA_DEBUG, "Delivery Completed %d count %d",
  425. multiThread_deliveryCompleted, i);
  426. #if defined(_WIN32)
  427. Sleep(1000);
  428. #else
  429. usleep(1000000L);
  430. #endif
  431. }
  432. assert("All Deliveries Complete", wait_seconds > 0,
  433. "Number of deliveryCompleted callbacks was %d\n",
  434. multiThread_deliveryCompleted);
  435. }
  436. }
  437. /*********************************************************************
  438. Async Callbacks - generic callbacks for send/receive tests
  439. *********************************************************************/
  440. /*static mutex_type client_mutex = NULL;
  441. static pthread_mutex_t client_mutex_store = PTHREAD_MUTEX_INITIALIZER;
  442. static mutex_type client_mutex = &client_mutex_store;*/
  443. void asyncTestOnDisconnect(void* context, MQTTAsync_successData* response)
  444. {
  445. //int rc;
  446. AsyncTestClient* tc = (AsyncTestClient*) context;
  447. MyLog(LOGA_DEBUG, "In asyncTestOnDisconnect callback, %s", tc->clientid);
  448. //rc = Thread_lock_mutex(client_mutex);
  449. tc->testFinished = 1;
  450. //rc = Thread_unlock_mutex(client_mutex);
  451. }
  452. void asyncTestOnSend(void* context, MQTTAsync_successData* response)
  453. {
  454. AsyncTestClient* tc = (AsyncTestClient*) context;
  455. //int rc;
  456. int qos = response->alt.pub.message.qos;
  457. MyLog(LOGA_DEBUG, "In asyncTestOnSend callback, %s", tc->clientid);
  458. //rc = Thread_lock_mutex(client_mutex);
  459. tc->sentmsgs[qos]++;
  460. //rc = Thread_unlock_mutex(client_mutex);
  461. }
  462. void asyncTestOnSubscribeFailure(void* context, MQTTAsync_failureData* response)
  463. {
  464. AsyncTestClient* tc = (AsyncTestClient*) context;
  465. MyLog(LOGA_DEBUG, "In asyncTestOnSubscribeFailure callback, %s",
  466. tc->clientid);
  467. assert("There should be no failures in this test. ", 0, "asyncTestOnSubscribeFailure callback was called\n", 0);
  468. }
  469. void asyncTestOnUnsubscribe(void* context, MQTTAsync_successData* response)
  470. {
  471. AsyncTestClient* tc = (AsyncTestClient*) context;
  472. MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
  473. int rc;
  474. MyLog(LOGA_DEBUG, "In asyncTestOnUnsubscribe callback, %s", tc->clientid);
  475. opts.onSuccess = asyncTestOnDisconnect;
  476. opts.context = tc;
  477. opts.timeout = 1000;
  478. rc = MQTTAsync_disconnect(tc->client, &opts);
  479. }
  480. void asyncTestOnSubscribe(void* context, MQTTAsync_successData* response)
  481. {
  482. AsyncTestClient* tc = (AsyncTestClient*) context;
  483. int rc, i;
  484. MyLog(LOGA_DEBUG, "In asyncTestOnSubscribe callback, %s", tc->clientid);
  485. //rc = Thread_lock_mutex(client_mutex);
  486. tc->subscribed = 1;
  487. //rc = Thread_unlock_mutex(client_mutex);
  488. for (i = 0; i < 3; i++)
  489. {
  490. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  491. pubmsg.payload
  492. = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  493. pubmsg.payloadlen = 11;
  494. pubmsg.qos = i;
  495. pubmsg.retained = 0;
  496. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  497. //opts.onSuccess = asyncTestOnSend;
  498. opts.context = &tc;
  499. rc = MQTTAsync_send(tc->client, tc->topic, pubmsg.payloadlen,
  500. pubmsg.payload, pubmsg.qos, pubmsg.retained, &opts);
  501. assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  502. tc->sentmsgs[i]++;
  503. MyLog(LOGA_DEBUG, "Maxmsgs %d", tc->maxmsgs);
  504. }
  505. }
  506. int asyncTestMessageArrived(void* context, char* topicName, int topicLen,
  507. MQTTAsync_message* m)
  508. {
  509. AsyncTestClient* tc = (AsyncTestClient*) context;
  510. int rc;
  511. //rc = Thread_lock_mutex(client_mutex);
  512. tc->rcvdmsgs[m->qos]++;
  513. //printf("Received messages: %d\n", tc->rcvdmsgs[m->qos]);
  514. MyLog(LOGA_DEBUG,
  515. "In asyncTestMessageArrived callback, %s total to exit %d, total received %d,%d,%d",
  516. tc->clientid, (tc->maxmsgs * 3), tc->rcvdmsgs[0], tc->rcvdmsgs[1],
  517. tc->rcvdmsgs[2]);
  518. if (tc->sentmsgs[m->qos] < tc->maxmsgs)
  519. {
  520. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  521. //opts.onSuccess = asyncTestOnSend;
  522. opts.context = tc;
  523. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  524. pubmsg.payload
  525. = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  526. pubmsg.payloadlen = 11;
  527. pubmsg.qos = m->qos;
  528. pubmsg.retained = 0;
  529. rc = MQTTAsync_send(tc->client, tc->topic, pubmsg.payloadlen,
  530. pubmsg.payload, pubmsg.qos, pubmsg.retained, &opts);
  531. assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d messages sent %d,%d,%d", rc);
  532. MyLog(LOGA_DEBUG, "Messages sent %d,%d,%d", tc->sentmsgs[0],
  533. tc->sentmsgs[1], tc->sentmsgs[2]);
  534. tc->sentmsgs[m->qos]++;
  535. }
  536. if ((tc->rcvdmsgs[0] + tc->rcvdmsgs[1] + tc->rcvdmsgs[2]) == (tc->maxmsgs* 3))
  537. {
  538. MyLog(LOGA_DEBUG, "Ready to unsubscribe");
  539. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  540. opts.onSuccess = asyncTestOnUnsubscribe;
  541. opts.context = tc;
  542. rc = MQTTAsync_unsubscribe(tc->client, tc->topic, &opts);
  543. assert("Unsubscribe successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  544. }
  545. //rc = Thread_unlock_mutex(client_mutex);
  546. MyLog(LOGA_DEBUG, "Leaving asyncTestMessageArrived callback");
  547. MQTTAsync_freeMessage(&m);
  548. MQTTAsync_free(topicName);
  549. return 1;
  550. }
  551. void asyncTestOnDeliveryComplete(void* context, MQTTAsync_token token)
  552. {
  553. }
  554. void asyncTestOnConnect(void* context, MQTTAsync_successData* response)
  555. {
  556. AsyncTestClient* tc = (AsyncTestClient*) context;
  557. int subsqos = 2;
  558. int rc;
  559. MyLog(LOGA_DEBUG, "In asyncTestOnConnect callback, %s", tc->clientid);
  560. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  561. opts.onSuccess = asyncTestOnSubscribe;
  562. opts.onFailure = asyncTestOnSubscribeFailure;
  563. opts.context = tc;
  564. rc = MQTTAsync_subscribe(tc->client, tc->topic, subsqos, &opts);
  565. assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  566. }
  567. /*********************************************************************
  568. Test1: SSL connection to non SSL MQTT server
  569. *********************************************************************/
  570. int test1Finished = 0;
  571. int test1OnFailureCalled = 0;
  572. void test1OnFailure(void* context, MQTTAsync_failureData* response)
  573. {
  574. MyLog(LOGA_DEBUG, "In connect onFailure callback, context %p", context);
  575. test1OnFailureCalled++;
  576. test1Finished = 1;
  577. }
  578. void test1OnConnect(void* context, MQTTAsync_successData* response)
  579. {
  580. MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p\n", context);
  581. assert("Connect should not succeed", 0, "connect success callback was called", 0);
  582. test1Finished = 1;
  583. }
  584. int test1(struct Options options)
  585. {
  586. char* testname = "test1";
  587. int subsqos = 2;
  588. MQTTAsync c;
  589. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  590. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  591. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  592. int rc = 0;
  593. char* test_topic = "C client SSL test1";
  594. int count = 0;
  595. test1Finished = 0;
  596. failures = 0;
  597. MyLog(LOGA_INFO, "Starting SSL test 1 - connection to nonSSL MQTT server");
  598. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  599. global_start_time = start_clock();
  600. rc = MQTTAsync_create(&c, "rubbish://wrong", "test1", MQTTCLIENT_PERSISTENCE_DEFAULT,
  601. NULL);
  602. assert("bad rc from create", rc == MQTTASYNC_BAD_PROTOCOL, "rc was %d \n", rc);
  603. rc = MQTTAsync_create(&c, options.connection, "test1", MQTTCLIENT_PERSISTENCE_DEFAULT,
  604. NULL);
  605. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d \n", rc);
  606. if (rc != MQTTASYNC_SUCCESS)
  607. {
  608. MQTTAsync_destroy(&c);
  609. goto exit;
  610. }
  611. opts.keepAliveInterval = 20;
  612. opts.cleansession = 1;
  613. opts.username = "testuser";
  614. opts.password = "testpassword";
  615. opts.will = &wopts;
  616. opts.will->message = "will message";
  617. opts.will->qos = 1;
  618. opts.will->retained = 0;
  619. opts.will->topicName = "will topic";
  620. opts.will = NULL;
  621. opts.onSuccess = test1OnConnect;
  622. opts.onFailure = test1OnFailure;
  623. opts.context = c;
  624. rc = MQTTAsync_connect(c, &opts);
  625. assert("Bad rc from connect", rc == MQTTASYNC_NULL_PARAMETER, "rc was %d ", rc);
  626. opts.ssl = &sslopts;
  627. opts.ssl->enableServerCertAuth = 0;
  628. MyLog(LOGA_DEBUG, "Connecting");
  629. rc = MQTTAsync_connect(c, &opts);
  630. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d ", rc);
  631. if (rc != MQTTASYNC_SUCCESS)
  632. {
  633. failures++;
  634. goto exit;
  635. }
  636. /* wait for success or failure callback */
  637. while (!test1Finished && ++count < 10000)
  638. #if defined(_WIN32)
  639. Sleep(100);
  640. #else
  641. usleep(10000L);
  642. #endif
  643. exit: MQTTAsync_destroy(&c);
  644. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  645. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  646. write_test_result();
  647. return failures;
  648. }
  649. /*********************************************************************
  650. Test2a: Mutual SSL Authentication - Certificates in place on client and server
  651. *********************************************************************/
  652. void test2aOnConnectFailure(void* context, MQTTAsync_failureData* response)
  653. {
  654. AsyncTestClient* client = (AsyncTestClient*) context;
  655. MyLog(LOGA_DEBUG, "In test2aOnConnectFailure callback, %s",
  656. client->clientid);
  657. assert("There should be no failures in this test. ", 0, "test2aOnConnectFailure callback was called\n", 0);
  658. client->testFinished = 1;
  659. }
  660. void test2aOnPublishFailure(void* context, MQTTAsync_failureData* response)
  661. {
  662. AsyncTestClient* client = (AsyncTestClient*) context;
  663. MyLog(LOGA_DEBUG, "In test2aOnPublishFailure callback, %s",
  664. client->clientid);
  665. assert("There should be no failures in this test. ", 0, "test2aOnPublishFailure callback was called\n", 0);
  666. }
  667. int test2a(struct Options options)
  668. {
  669. char* testname = "test2a";
  670. AsyncTestClient tc =
  671. AsyncTestClient_initializer;
  672. MQTTAsync c;
  673. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  674. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  675. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  676. int rc = 0;
  677. failures = 0;
  678. MyLog(LOGA_INFO, "Starting test 2a - Mutual SSL authentication");
  679. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  680. global_start_time = start_clock();
  681. rc = MQTTAsync_create(&c, options.mutual_auth_connection, "test2a", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  682. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  683. if (rc != MQTTASYNC_SUCCESS)
  684. goto exit;
  685. tc.client = c;
  686. sprintf(tc.clientid, "%s", testname);
  687. sprintf(tc.topic, "C client SSL test2a");
  688. tc.maxmsgs = MAXMSGS;
  689. //tc.rcvdmsgs = 0;
  690. tc.subscribed = 0;
  691. tc.testFinished = 0;
  692. opts.keepAliveInterval = 20;
  693. opts.cleansession = 1;
  694. opts.username = "testuser";
  695. opts.password = "testpassword";
  696. opts.will = &wopts;
  697. opts.will->message = "will message";
  698. opts.will->qos = 1;
  699. opts.will->retained = 0;
  700. opts.will->topicName = "will topic";
  701. opts.will = NULL;
  702. opts.onSuccess = asyncTestOnConnect;
  703. opts.onFailure = test2aOnConnectFailure;
  704. opts.context = &tc;
  705. opts.ssl = &sslopts;
  706. if (options.server_key_file != NULL)
  707. opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
  708. opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  709. if (options.client_key_pass != NULL)
  710. opts.ssl->privateKeyPassword = options.client_key_pass;
  711. //opts.ssl->enabledCipherSuites = "DEFAULT";
  712. //opts.ssl->enabledServerCertAuth = 1;
  713. opts.ssl->verify = 1;
  714. MyLog(LOGA_DEBUG, "enableServerCertAuth %d\n", opts.ssl->enableServerCertAuth);
  715. MyLog(LOGA_DEBUG, "verify %d\n", opts.ssl->verify);
  716. rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
  717. asyncTestOnDeliveryComplete);
  718. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  719. MyLog(LOGA_DEBUG, "Connecting");
  720. rc = MQTTAsync_connect(c, &opts);
  721. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  722. if (rc != MQTTASYNC_SUCCESS)
  723. goto exit;
  724. while (!tc.subscribed && !tc.testFinished)
  725. #if defined(_WIN32)
  726. Sleep(100);
  727. #else
  728. usleep(10000L);
  729. #endif
  730. if (tc.testFinished)
  731. goto exit;
  732. while (!tc.testFinished)
  733. #if defined(_WIN32)
  734. Sleep(100);
  735. #else
  736. usleep(10000L);
  737. #endif
  738. MyLog(LOGA_DEBUG, "Stopping");
  739. exit: MQTTAsync_destroy(&c);
  740. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  741. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  742. write_test_result();
  743. return failures;
  744. }
  745. /*********************************************************************
  746. Test2b: Mutual SSL Authentication - Server does not have Client cert
  747. *********************************************************************/
  748. int test2bFinished;
  749. void test2bOnConnectFailure(void* context, MQTTAsync_failureData* response)
  750. {
  751. MyLog(LOGA_DEBUG, "In test2bOnConnectFailure callback, context %p", context);
  752. assert("This test should call test2bOnConnectFailure. ", 1, "test2bOnConnectFailure callback was called\n", 1);
  753. test2bFinished = 1;
  754. }
  755. void test2bOnConnect(void* context, MQTTAsync_successData* response)
  756. {
  757. MyLog(LOGA_DEBUG, "In test2bOnConnectFailure callback, context %p", context);
  758. assert("This connect should not succeed. ", 0, "test2bOnConnect callback was called\n", 0);
  759. test2bFinished = 1;
  760. }
  761. int test2b(struct Options options)
  762. {
  763. char* testname = "test2b";
  764. int subsqos = 2;
  765. MQTTAsync c;
  766. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  767. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  768. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  769. int rc = 0;
  770. int count = 0;
  771. test2bFinished = 0;
  772. failures = 0;
  773. MyLog(LOGA_INFO,
  774. "Starting test 2b - connection to SSL MQTT server with clientauth=req but server does not have client cert");
  775. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  776. global_start_time = start_clock();
  777. rc = MQTTAsync_create(&c, options.nocert_mutual_auth_connection,
  778. "test2b", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  779. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  780. if (rc != MQTTASYNC_SUCCESS)
  781. {
  782. MQTTAsync_destroy(&c);
  783. goto exit;
  784. }
  785. opts.keepAliveInterval = 20;
  786. opts.cleansession = 1;
  787. opts.username = "testuser";
  788. opts.password = "testpassword";
  789. opts.will = &wopts;
  790. opts.will->message = "will message";
  791. opts.will->qos = 1;
  792. opts.will->retained = 0;
  793. opts.will->topicName = "will topic";
  794. opts.will = NULL;
  795. opts.onSuccess = test2bOnConnect;
  796. opts.onFailure = test2bOnConnectFailure;
  797. opts.context = c;
  798. opts.ssl = &sslopts;
  799. if (options.server_key_file != NULL)
  800. opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
  801. opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  802. if (options.client_key_pass != NULL)
  803. opts.ssl->privateKeyPassword = options.client_key_pass;
  804. //opts.ssl->enabledCipherSuites = "DEFAULT";
  805. //opts.ssl->enabledServerCertAuth = 0;
  806. MyLog(LOGA_DEBUG, "Connecting");
  807. rc = MQTTAsync_connect(c, &opts);
  808. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  809. if (rc != MQTTASYNC_SUCCESS)
  810. goto exit;
  811. while (!test2bFinished && ++count < 10000)
  812. #if defined(_WIN32)
  813. Sleep(100);
  814. #else
  815. usleep(10000L);
  816. #endif
  817. exit: MQTTAsync_destroy(&c);
  818. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  819. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  820. write_test_result();
  821. return failures;
  822. }
  823. /*********************************************************************
  824. Test2c: Mutual SSL Authentication - Client does not have Server cert
  825. *********************************************************************/
  826. int test2cFinished;
  827. void test2cOnConnectFailure(void* context, MQTTAsync_failureData* response)
  828. {
  829. MyLog(LOGA_DEBUG, "In test2cOnConnectFailure callback, context %p", context);
  830. assert("This test should call test2cOnConnectFailure. ", 1, "test2cOnConnectFailure callback was called\n", 0);
  831. test2cFinished = 1;
  832. }
  833. void test2cOnConnect(void* context, MQTTAsync_successData* response)
  834. {
  835. MyLog(LOGA_DEBUG, "In test2cOnConnect callback, context %p", context);
  836. assert("This connect should not succeed. ", 0, "test2cOnConnect callback was called\n", 0);
  837. test2cFinished = 1;
  838. }
  839. int test2c(struct Options options)
  840. {
  841. char* testname = "test2c";
  842. int subsqos = 2;
  843. MQTTAsync c;
  844. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  845. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  846. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  847. int rc = 0;
  848. char* test_topic = "C client test2c";
  849. int count = 0;
  850. failures = 0;
  851. MyLog(
  852. LOGA_INFO,
  853. "Starting test 2c - connection to SSL MQTT server, server auth enabled but unknown cert");
  854. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  855. global_start_time = start_clock();
  856. rc = MQTTAsync_create(&c, options.nocert_mutual_auth_connection,
  857. "test2c", 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(&c);
  862. goto exit;
  863. }
  864. opts.keepAliveInterval = 20;
  865. opts.cleansession = 1;
  866. opts.username = "testuser";
  867. opts.password = "testpassword";
  868. opts.will = &wopts;
  869. opts.will->message = "will message";
  870. opts.will->qos = 1;
  871. opts.will->retained = 0;
  872. opts.will->topicName = "will topic";
  873. opts.will = NULL;
  874. opts.onSuccess = test2cOnConnect;
  875. opts.onFailure = test2cOnConnectFailure;
  876. opts.context = c;
  877. opts.ssl = &sslopts;
  878. //if (options.server_key_file != NULL) opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
  879. opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  880. if (options.client_key_pass != NULL)
  881. opts.ssl->privateKeyPassword = options.client_key_pass;
  882. //opts.ssl->enabledCipherSuites = "DEFAULT";
  883. //opts.ssl->enabledServerCertAuth = 0;
  884. MyLog(LOGA_DEBUG, "Connecting");
  885. rc = MQTTAsync_connect(c, &opts);
  886. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  887. if (rc != MQTTASYNC_SUCCESS)
  888. {
  889. failures++;
  890. goto exit;
  891. }
  892. while (!test2cFinished && ++count < 10000)
  893. #if defined(_WIN32)
  894. Sleep(100);
  895. #else
  896. usleep(10000L);
  897. #endif
  898. exit: MQTTAsync_destroy(&c);
  899. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  900. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  901. write_test_result();
  902. return failures;
  903. }
  904. /*********************************************************************
  905. Test2d: Mutual SSL Authentication - client has no certs
  906. *********************************************************************/
  907. int test2dFinished;
  908. void test2dOnConnectFailure(void* context, MQTTAsync_failureData* response)
  909. {
  910. MyLog(LOGA_DEBUG, "In test2dOnConnectFailure callback, context %p", context);
  911. assert("This test should call test2dOnConnectFailure. ", 1, "test2dOnConnectFailure callback was called\n", 0);
  912. test2dFinished = 1;
  913. }
  914. void test2dOnConnect(void* context, MQTTAsync_successData* response)
  915. {
  916. MyLog(LOGA_DEBUG, "In test2dOnConnect callback, context %p", context);
  917. assert("This connect should not succeed. ", 0, "test2dOnConnect callback was called\n", 0);
  918. test2dFinished = 1;
  919. }
  920. int test2d(struct Options options)
  921. {
  922. char* testname = "test2d";
  923. int subsqos = 2;
  924. MQTTAsync c;
  925. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  926. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  927. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  928. int rc = 0;
  929. char* test_topic = "C client test2d";
  930. int count = 0;
  931. unsigned int iteration = 0;
  932. failures = 0;
  933. MyLog(
  934. LOGA_INFO,
  935. "Starting test 2d - connection to SSL MQTT server, server auth enabled but unknown cert");
  936. fprintf(xml, "<testcase classname=\"test2d\" name=\"%s\"", testname);
  937. global_start_time = start_clock();
  938. // As reported in https://github.com/eclipse/paho.mqtt.c/issues/190
  939. // there is/was some race condition, which caused _sometimes_ that the library failed to detect,
  940. // that the connect attempt has already failed.
  941. // Therefore we need to test this several times!
  942. for (iteration = 0; !failures && (iteration < 20) ; iteration++)
  943. {
  944. count = 0;
  945. MQTTAsync_setTraceLevel(MQTTASYNC_TRACE_ERROR);
  946. rc = MQTTAsync_create(&c, options.mutual_auth_connection,
  947. "test2d", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  948. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  949. if (rc != MQTTASYNC_SUCCESS)
  950. {
  951. MQTTAsync_destroy(&c);
  952. failures++;
  953. break;
  954. }
  955. opts.keepAliveInterval = 60;
  956. opts.cleansession = 1;
  957. opts.will = &wopts;
  958. opts.will->message = "will message";
  959. opts.will->qos = 1;
  960. opts.will->retained = 0;
  961. opts.will->topicName = "will topic";
  962. opts.will = NULL;
  963. opts.onSuccess = test2dOnConnect;
  964. opts.onFailure = test2dOnConnectFailure;
  965. opts.context = c;
  966. opts.ssl = &sslopts;
  967. if (options.server_key_file != NULL) opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
  968. opts.ssl->keyStore = NULL; /*file of certificate for client to present to server - In this test the client has no certificate! */
  969. test2dFinished = 0;
  970. MyLog(LOGA_DEBUG, "Connecting");
  971. rc = MQTTAsync_connect(c, &opts);
  972. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  973. if (rc != MQTTASYNC_SUCCESS)
  974. {
  975. failures++;
  976. MyLog(LOGA_INFO, "Failed in iteration %d\n",iteration);
  977. MQTTAsync_destroy(&c);
  978. break;
  979. }
  980. #define TEST2D_COUNT 1000
  981. while (!test2dFinished && ++count < TEST2D_COUNT)
  982. {
  983. #if defined(_WIN32)
  984. Sleep(100);
  985. #else
  986. usleep(10000L);
  987. #endif
  988. }
  989. if (!test2dFinished && count >= TEST2D_COUNT)
  990. {
  991. MyLog(LOGA_INFO, "Failed in iteration %d\n",iteration);
  992. failures++;
  993. }
  994. MQTTAsync_destroy(&c);
  995. }
  996. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  997. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  998. write_test_result();
  999. return failures;
  1000. }
  1001. /*********************************************************************
  1002. Test2e: Mutual SSL Authentication using serverURIs
  1003. *********************************************************************/
  1004. void test2eOnConnectFailure(void* context, MQTTAsync_failureData* response)
  1005. {
  1006. AsyncTestClient* client = (AsyncTestClient*) context;
  1007. MyLog(LOGA_DEBUG, "In test2eOnConnectFailure callback, %s",
  1008. client->clientid);
  1009. assert("There should be no failures in this test. ", 0, "test2eOnConnectFailure callback was called\n", 0);
  1010. client->testFinished = 1;
  1011. }
  1012. void test2eOnPublishFailure(void* context, MQTTAsync_failureData* response)
  1013. {
  1014. AsyncTestClient* client = (AsyncTestClient*) context;
  1015. MyLog(LOGA_DEBUG, "In test2eOnPublishFailure callback, %s",
  1016. client->clientid);
  1017. assert("There should be no failures in this test. ", 0, "test2eOnPublishFailure callback was called\n", 0);
  1018. }
  1019. int test2e(struct Options options)
  1020. {
  1021. char* testname = "test2e";
  1022. AsyncTestClient tc =
  1023. AsyncTestClient_initializer;
  1024. MQTTAsync c;
  1025. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  1026. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  1027. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  1028. char* uris[2] = {"rubbish", options.mutual_auth_connection};
  1029. int rc = 0;
  1030. failures = 0;
  1031. MyLog(LOGA_INFO, "Starting test 2e - Mutual SSL authentication with serverURIs");
  1032. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  1033. global_start_time = start_clock();
  1034. rc = MQTTAsync_create(&c, "none", "test2e", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  1035. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1036. if (rc != MQTTASYNC_SUCCESS)
  1037. goto exit;
  1038. tc.client = c;
  1039. sprintf(tc.clientid, "%s", testname);
  1040. sprintf(tc.topic, "C client SSL test2e");
  1041. tc.maxmsgs = MAXMSGS;
  1042. //tc.rcvdmsgs = 0;
  1043. tc.subscribed = 0;
  1044. tc.testFinished = 0;
  1045. opts.keepAliveInterval = 20;
  1046. opts.cleansession = 1;
  1047. opts.username = "testuser";
  1048. opts.password = "testpassword";
  1049. opts.will = &wopts;
  1050. opts.will->message = "will message";
  1051. opts.will->qos = 1;
  1052. opts.will->retained = 0;
  1053. opts.will->topicName = "will topic";
  1054. opts.will = NULL;
  1055. opts.onSuccess = asyncTestOnConnect;
  1056. opts.onFailure = test2eOnConnectFailure;
  1057. opts.context = &tc;
  1058. opts.serverURIs = uris;
  1059. opts.serverURIcount = 2;
  1060. opts.ssl = &sslopts;
  1061. if (options.server_key_file != NULL)
  1062. opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
  1063. opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  1064. if (options.client_key_pass != NULL)
  1065. opts.ssl->privateKeyPassword = options.client_key_pass;
  1066. //opts.ssl->enabledCipherSuites = "DEFAULT";
  1067. //opts.ssl->enabledServerCertAuth = 1;
  1068. opts.ssl->verify = 1;
  1069. MyLog(LOGA_DEBUG, "enableServerCertAuth %d\n", opts.ssl->enableServerCertAuth);
  1070. MyLog(LOGA_DEBUG, "verify %d\n", opts.ssl->verify);
  1071. rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
  1072. asyncTestOnDeliveryComplete);
  1073. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1074. MyLog(LOGA_DEBUG, "Connecting");
  1075. rc = MQTTAsync_connect(c, &opts);
  1076. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1077. if (rc != MQTTASYNC_SUCCESS)
  1078. goto exit;
  1079. while (!tc.subscribed && !tc.testFinished)
  1080. #if defined(_WIN32)
  1081. Sleep(100);
  1082. #else
  1083. usleep(10000L);
  1084. #endif
  1085. if (tc.testFinished)
  1086. goto exit;
  1087. while (!tc.testFinished)
  1088. #if defined(_WIN32)
  1089. Sleep(100);
  1090. #else
  1091. usleep(10000L);
  1092. #endif
  1093. MyLog(LOGA_DEBUG, "Stopping");
  1094. exit: MQTTAsync_destroy(&c);
  1095. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  1096. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  1097. write_test_result();
  1098. return failures;
  1099. }
  1100. /*********************************************************************
  1101. Test3a: Server Authentication - server certificate in client trust store
  1102. *********************************************************************/
  1103. void test3aOnConnectFailure(void* context, MQTTAsync_failureData* response)
  1104. {
  1105. AsyncTestClient* client = (AsyncTestClient*) context;
  1106. MyLog(LOGA_DEBUG, "In test3aOnConnectFailure callback, context %p", context);
  1107. assert("There should be no failures in this test. ", 0, "test3aOnConnectFailure callback was called\n", 0);
  1108. client->testFinished = 1;
  1109. }
  1110. int test3a(struct Options options)
  1111. {
  1112. char* testname = "test3a";
  1113. int subsqos = 2;
  1114. /* TODO - usused - remove ? MQTTAsync_deliveryToken* dt = NULL; */
  1115. AsyncTestClient tc =
  1116. AsyncTestClient_initializer;
  1117. MQTTAsync c;
  1118. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  1119. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  1120. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  1121. int rc = 0;
  1122. int i;
  1123. failures = 0;
  1124. MyLog(LOGA_INFO, "Starting test 3a - Server authentication");
  1125. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  1126. global_start_time = start_clock();
  1127. MQTTAsync_create(&c, options.server_auth_connection, "test3a", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  1128. tc.client = c;
  1129. sprintf(tc.clientid, "%s", testname);
  1130. sprintf(tc.topic, "C client SSL test3a");
  1131. tc.maxmsgs = MAXMSGS;
  1132. //tc.rcvdmsgs = 0;
  1133. tc.subscribed = 0;
  1134. tc.testFinished = 0;
  1135. opts.keepAliveInterval = 20;
  1136. opts.cleansession = 1;
  1137. opts.username = "testuser";
  1138. opts.password = "testpassword";
  1139. opts.will = &wopts;
  1140. opts.will->message = "will message";
  1141. opts.will->qos = 1;
  1142. opts.will->retained = 0;
  1143. opts.will->topicName = "will topic";
  1144. opts.will = NULL;
  1145. opts.onSuccess = asyncTestOnConnect;
  1146. opts.onFailure = test3aOnConnectFailure;
  1147. opts.context = &tc;
  1148. opts.ssl = &sslopts;
  1149. if (options.server_key_file != NULL)
  1150. opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
  1151. //opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  1152. //if (options.client_key_pass != NULL) opts.ssl->privateKeyPassword = options.client_key_pass;
  1153. //opts.ssl->enabledCipherSuites = "DEFAULT";
  1154. //opts.ssl->enabledServerCertAuth = 1;
  1155. rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
  1156. asyncTestOnDeliveryComplete);
  1157. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1158. MyLog(LOGA_DEBUG, "Connecting");
  1159. rc = MQTTAsync_connect(c, &opts);
  1160. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1161. if (rc != MQTTASYNC_SUCCESS)
  1162. goto exit;
  1163. while (!tc.subscribed && !tc.testFinished)
  1164. #if defined(_WIN32)
  1165. Sleep(100);
  1166. #else
  1167. usleep(10000L);
  1168. #endif
  1169. if (tc.testFinished)
  1170. goto exit;
  1171. for (i = 0; i < 3; i++)
  1172. {
  1173. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  1174. pubmsg.payload
  1175. = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  1176. pubmsg.payloadlen = 11;
  1177. pubmsg.qos = i;
  1178. pubmsg.retained = 0;
  1179. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1180. opts.onSuccess = asyncTestOnSend;
  1181. opts.context = &tc;
  1182. rc = MQTTAsync_send(c, tc.topic, pubmsg.payloadlen, pubmsg.payload,
  1183. pubmsg.qos, pubmsg.retained, &opts);
  1184. assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1185. }
  1186. while (!tc.testFinished)
  1187. #if defined(_WIN32)
  1188. Sleep(100);
  1189. #else
  1190. usleep(10000L);
  1191. #endif
  1192. MyLog(LOGA_DEBUG, "Stopping");
  1193. MQTTAsync_destroy(&c);
  1194. exit: MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.", (failures
  1195. == 0) ? "passed" : "failed", testname, tests, failures);
  1196. write_test_result();
  1197. return failures;
  1198. }
  1199. /*********************************************************************
  1200. Test3b: Server Authentication - Client does not have server cert
  1201. *********************************************************************/
  1202. int test3bFinished;
  1203. void test3bOnConnectFailure(void* context, MQTTAsync_failureData* response)
  1204. {
  1205. MyLog(LOGA_DEBUG, "In test3bOnConnectFailure callback, context %p", context);
  1206. assert("This test should call test3bOnConnectFailure. ", 1, "test3bOnConnectFailure callback was called\n", 1);
  1207. test3bFinished = 1;
  1208. }
  1209. void test3bOnConnect(void* context, MQTTAsync_successData* response)
  1210. {
  1211. MyLog(LOGA_DEBUG, "In test3bOnConnectFailure callback, context %p", context);
  1212. assert("This connect should not succeed. ", 0, "test3bOnConnect callback was called\n", 0);
  1213. test3bFinished = 1;
  1214. }
  1215. int test3b(struct Options options)
  1216. {
  1217. char* testname = "test3b";
  1218. int subsqos = 2;
  1219. MQTTAsync c;
  1220. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  1221. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  1222. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  1223. int rc = 0;
  1224. int count = 0;
  1225. test3bFinished = 0;
  1226. failures = 0;
  1227. MyLog(
  1228. LOGA_INFO,
  1229. "Starting test 3b - connection to SSL MQTT server with clientauth=opt but client does not have server cert");
  1230. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  1231. global_start_time = start_clock();
  1232. rc = MQTTAsync_create(&c, options.server_auth_connection, "test3b", MQTTCLIENT_PERSISTENCE_DEFAULT,
  1233. NULL);
  1234. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1235. if (rc != MQTTASYNC_SUCCESS)
  1236. goto exit;
  1237. opts.keepAliveInterval = 20;
  1238. opts.cleansession = 1;
  1239. opts.username = "testuser";
  1240. opts.password = "testpassword";
  1241. opts.will = &wopts;
  1242. opts.will->message = "will message";
  1243. opts.will->qos = 1;
  1244. opts.will->retained = 0;
  1245. opts.will->topicName = "will topic";
  1246. opts.will = NULL;
  1247. opts.onSuccess = test3bOnConnect;
  1248. opts.onFailure = test3bOnConnectFailure;
  1249. opts.context = c;
  1250. opts.ssl = &sslopts;
  1251. //if (options.server_key_file != NULL) opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
  1252. //opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  1253. //if (options.client_key_pass != NULL) opts.ssl->privateKeyPassword = options.client_key_pass;
  1254. //opts.ssl->enabledCipherSuites = "DEFAULT";
  1255. //opts.ssl->enabledServerCertAuth = 0;
  1256. MyLog(LOGA_DEBUG, "Connecting");
  1257. rc = MQTTAsync_connect(c, &opts);
  1258. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1259. if (rc != MQTTASYNC_SUCCESS)
  1260. goto exit;
  1261. while (!test3bFinished && ++count < 10000)
  1262. #if defined(_WIN32)
  1263. Sleep(100);
  1264. #else
  1265. usleep(10000L);
  1266. #endif
  1267. exit: MQTTAsync_destroy(&c);
  1268. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  1269. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  1270. write_test_result();
  1271. return failures;
  1272. }
  1273. /*********************************************************************
  1274. Test4: Accept invalid server certificates
  1275. *********************************************************************/
  1276. void test4OnConnectFailure(void* context, MQTTAsync_failureData* response)
  1277. {
  1278. AsyncTestClient* client = (AsyncTestClient*) context;
  1279. MyLog(LOGA_DEBUG, "In test4OnConnectFailure callback, context %p", context);
  1280. assert("There should be no failures in this test. ", 0, "test4OnConnectFailure callback was called\n", 0);
  1281. client->testFinished = 1;
  1282. }
  1283. void test4OnPublishFailure(void* context, MQTTAsync_failureData* response)
  1284. {
  1285. MyLog(LOGA_DEBUG, "In test4OnPublishFailure callback, context %p", context);
  1286. assert("There should be no failures in this test. ", 0, "test4OnPublishFailure callback was called\n", 0);
  1287. }
  1288. int test4(struct Options options)
  1289. {
  1290. char* testname = "test4";
  1291. int subsqos = 2;
  1292. /* TODO - usused - remove ? MQTTAsync_deliveryToken* dt = NULL; */
  1293. AsyncTestClient tc =
  1294. AsyncTestClient_initializer;
  1295. MQTTAsync c;
  1296. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  1297. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  1298. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  1299. int rc = 0;
  1300. int i;
  1301. failures = 0;
  1302. MyLog(LOGA_INFO, "Starting test 4 - accept invalid server certificates");
  1303. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  1304. global_start_time = start_clock();
  1305. MQTTAsync_create(&c, options.server_auth_connection, "test4", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  1306. tc.client = c;
  1307. sprintf(tc.clientid, "%s", testname);
  1308. sprintf(tc.topic, "C client SSL test4");
  1309. tc.maxmsgs = MAXMSGS;
  1310. //tc.rcvdmsgs = 0;
  1311. tc.subscribed = 0;
  1312. tc.testFinished = 0;
  1313. opts.keepAliveInterval = 20;
  1314. opts.cleansession = 1;
  1315. opts.username = "testuser";
  1316. opts.password = "testpassword";
  1317. opts.will = &wopts;
  1318. opts.will->message = "will message";
  1319. opts.will->qos = 1;
  1320. opts.will->retained = 0;
  1321. opts.will->topicName = "will topic";
  1322. opts.will = NULL;
  1323. opts.onSuccess = asyncTestOnConnect;
  1324. opts.onFailure = test4OnConnectFailure;
  1325. opts.context = &tc;
  1326. opts.ssl = &sslopts;
  1327. //if (options.server_key_file != NULL) opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
  1328. //opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  1329. //if (options.client_key_pass != NULL) opts.ssl->privateKeyPassword = options.client_key_pass;
  1330. //opts.ssl->enabledCipherSuites = "DEFAULT";
  1331. opts.ssl->enableServerCertAuth = 0;
  1332. rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
  1333. asyncTestOnDeliveryComplete);
  1334. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1335. MyLog(LOGA_DEBUG, "Connecting");
  1336. rc = MQTTAsync_connect(c, &opts);
  1337. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1338. if (rc != MQTTASYNC_SUCCESS)
  1339. goto exit;
  1340. while (!tc.subscribed && !tc.testFinished)
  1341. #if defined(_WIN32)
  1342. Sleep(100);
  1343. #else
  1344. usleep(10000L);
  1345. #endif
  1346. if (tc.testFinished)
  1347. goto exit;
  1348. while (!tc.testFinished)
  1349. #if defined(_WIN32)
  1350. Sleep(100);
  1351. #else
  1352. usleep(10000L);
  1353. #endif
  1354. MyLog(LOGA_DEBUG, "Stopping");
  1355. exit: MQTTAsync_destroy(&c);
  1356. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  1357. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  1358. write_test_result();
  1359. return failures;
  1360. }
  1361. /*********************************************************************
  1362. Test5a: Anonymous ciphers - server auth disabled
  1363. *********************************************************************/
  1364. void test5aOnConnectFailure(void* context, MQTTAsync_failureData* response)
  1365. {
  1366. AsyncTestClient* client = (AsyncTestClient*) context;
  1367. MyLog(LOGA_DEBUG, "In test5aOnConnectFailure callback, context %p", context);
  1368. assert("There should be no failures in this test. ", 0, "test5aOnConnectFailure callback was called\n", 0);
  1369. client->testFinished = 1;
  1370. }
  1371. void test5aOnPublishFailure(void* context, MQTTAsync_failureData* response)
  1372. {
  1373. MyLog(LOGA_DEBUG, "In test5aOnPublishFailure callback, context %p", context);
  1374. assert("There should be no failures in this test. ", 0, "test5aOnPublishFailure callback was called\n", 0);
  1375. }
  1376. int test5a(struct Options options)
  1377. {
  1378. char* testname = "test5a";
  1379. AsyncTestClient tc =
  1380. AsyncTestClient_initializer;
  1381. MQTTAsync c;
  1382. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  1383. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  1384. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  1385. int rc = 0;
  1386. int i;
  1387. failures = 0;
  1388. MyLog(LOGA_INFO,
  1389. "Starting SSL test 5a - Anonymous ciphers - server authentication disabled");
  1390. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  1391. global_start_time = start_clock();
  1392. rc = MQTTAsync_create(&c, options.anon_connection, "test5a", MQTTCLIENT_PERSISTENCE_DEFAULT,
  1393. NULL);
  1394. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1395. if (rc != MQTTASYNC_SUCCESS)
  1396. goto exit;
  1397. tc.client = c;
  1398. sprintf(tc.clientid, "%s", testname);
  1399. sprintf(tc.topic, "C client SSL test5a");
  1400. tc.maxmsgs = MAXMSGS;
  1401. //tc.rcvdmsgs = 0;
  1402. tc.subscribed = 0;
  1403. tc.testFinished = 0;
  1404. opts.keepAliveInterval = 20;
  1405. opts.cleansession = 1;
  1406. opts.username = "testuser";
  1407. opts.password = "testpassword";
  1408. opts.will = &wopts;
  1409. opts.will->message = "will message";
  1410. opts.will->qos = 1;
  1411. opts.will->retained = 0;
  1412. opts.will->topicName = "will topic";
  1413. opts.will = NULL;
  1414. opts.onSuccess = asyncTestOnConnect;
  1415. opts.onFailure = test5aOnConnectFailure;
  1416. opts.context = &tc;
  1417. opts.ssl = &sslopts;
  1418. //opts.ssl->trustStore = /*file of certificates trusted by client*/
  1419. //opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  1420. //if (options.client_key_pass != NULL) opts.ssl->privateKeyPassword = options.client_key_pass;
  1421. opts.ssl->enabledCipherSuites = "aNULL";
  1422. opts.ssl->enableServerCertAuth = 0;
  1423. rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
  1424. asyncTestOnDeliveryComplete);
  1425. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1426. MyLog(LOGA_DEBUG, "Connecting");
  1427. rc = MQTTAsync_connect(c, &opts);
  1428. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1429. if (rc != MQTTASYNC_SUCCESS)
  1430. goto exit;
  1431. while (!tc.subscribed && !tc.testFinished)
  1432. #if defined(_WIN32)
  1433. Sleep(100);
  1434. #else
  1435. usleep(10000L);
  1436. #endif
  1437. if (tc.testFinished)
  1438. goto exit;
  1439. for (i = 0; i < 3; i++)
  1440. {
  1441. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  1442. pubmsg.payload
  1443. = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  1444. pubmsg.payloadlen = 11;
  1445. pubmsg.qos = i;
  1446. pubmsg.retained = 0;
  1447. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1448. opts.onSuccess = asyncTestOnSend;
  1449. opts.context = &tc;
  1450. rc = MQTTAsync_send(c, tc.topic, pubmsg.payloadlen, pubmsg.payload,
  1451. pubmsg.qos, pubmsg.retained, &opts);
  1452. assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1453. }
  1454. while (!tc.testFinished)
  1455. #if defined(_WIN32)
  1456. Sleep(100);
  1457. #else
  1458. usleep(10000L);
  1459. #endif
  1460. MyLog(LOGA_DEBUG, "Stopping");
  1461. exit: MQTTAsync_destroy(&c);
  1462. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  1463. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  1464. write_test_result();
  1465. return failures;
  1466. }
  1467. /*********************************************************************
  1468. Test5b: Anonymous ciphers - server auth enabled
  1469. ********************************************************************/
  1470. void test5bOnConnectFailure(void* context, MQTTAsync_failureData* response)
  1471. {
  1472. AsyncTestClient* client = (AsyncTestClient*) context;
  1473. MyLog(LOGA_DEBUG, "In test5bOnConnectFailure callback, context %p", context);
  1474. assert("There should be no failures in this test. ", 0, "test5bOnConnectFailure callback was called\n", 0);
  1475. client->testFinished = 1;
  1476. }
  1477. void test5bOnPublishFailure(void* context, MQTTAsync_failureData* response)
  1478. {
  1479. MyLog(LOGA_DEBUG, "In test5bOnPublishFailure callback, context %p", context);
  1480. assert("There should be no failures in this test. ", 0, "test5bOnPublishFailure callback was called\n", 0);
  1481. }
  1482. int test5b(struct Options options)
  1483. {
  1484. char* testname = "test5b";
  1485. AsyncTestClient tc =
  1486. AsyncTestClient_initializer;
  1487. MQTTAsync c;
  1488. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  1489. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  1490. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  1491. int rc = 0;
  1492. int i;
  1493. failures = 0;
  1494. MyLog(LOGA_INFO,
  1495. "Starting SSL test 5b - Anonymous ciphers - server authentication enabled");
  1496. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  1497. global_start_time = start_clock();
  1498. rc = MQTTAsync_create(&c, options.anon_connection, "test5b", MQTTCLIENT_PERSISTENCE_DEFAULT,
  1499. NULL);
  1500. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1501. if (rc != MQTTASYNC_SUCCESS)
  1502. goto exit;
  1503. tc.client = c;
  1504. sprintf(tc.clientid, "%s", testname);
  1505. sprintf(tc.topic, "C client SSL test5b");
  1506. tc.maxmsgs = MAXMSGS;
  1507. //tc.rcvdmsgs = 0;
  1508. tc.subscribed = 0;
  1509. tc.testFinished = 0;
  1510. opts.keepAliveInterval = 20;
  1511. opts.cleansession = 1;
  1512. opts.username = "testuser";
  1513. opts.password = "testpassword";
  1514. opts.will = &wopts;
  1515. opts.will->message = "will message";
  1516. opts.will->qos = 1;
  1517. opts.will->retained = 0;
  1518. opts.will->topicName = "will topic";
  1519. opts.will = NULL;
  1520. opts.onSuccess = asyncTestOnConnect;
  1521. opts.onFailure = test5bOnConnectFailure;
  1522. opts.context = &tc;
  1523. opts.ssl = &sslopts;
  1524. //opts.ssl->trustStore = /*file of certificates trusted by client*/
  1525. //opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  1526. //if (options.client_key_pass != NULL) opts.ssl->privateKeyPassword = options.client_key_pass;
  1527. opts.ssl->enabledCipherSuites = "aNULL";
  1528. opts.ssl->enableServerCertAuth = 1;
  1529. rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
  1530. asyncTestOnDeliveryComplete);
  1531. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1532. MyLog(LOGA_DEBUG, "Connecting");
  1533. rc = MQTTAsync_connect(c, &opts);
  1534. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1535. if (rc != MQTTASYNC_SUCCESS)
  1536. goto exit;
  1537. while (!tc.subscribed && !tc.testFinished)
  1538. #if defined(_WIN32)
  1539. Sleep(100);
  1540. #else
  1541. usleep(10000L);
  1542. #endif
  1543. if (tc.testFinished)
  1544. goto exit;
  1545. for (i = 0; i < 3; i++)
  1546. {
  1547. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  1548. pubmsg.payload
  1549. = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  1550. pubmsg.payloadlen = 11;
  1551. pubmsg.qos = i;
  1552. pubmsg.retained = 0;
  1553. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1554. opts.onSuccess = asyncTestOnSend;
  1555. opts.context = &tc;
  1556. rc = MQTTAsync_send(c, tc.topic, pubmsg.payloadlen, pubmsg.payload,
  1557. pubmsg.qos, pubmsg.retained, &opts);
  1558. assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1559. }
  1560. while (!tc.testFinished)
  1561. #if defined(_WIN32)
  1562. Sleep(100);
  1563. #else
  1564. usleep(10000L);
  1565. #endif
  1566. MyLog(LOGA_DEBUG, "Stopping");
  1567. exit: MQTTAsync_destroy(&c);
  1568. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  1569. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  1570. write_test_result();
  1571. return failures;
  1572. }
  1573. /*********************************************************************
  1574. Test5c: Anonymous ciphers - client not using anonymous ciphers
  1575. *********************************************************************/
  1576. int test5cFinished;
  1577. void test5cOnConnectFailure(void* context, MQTTAsync_failureData* response)
  1578. {
  1579. MyLog(LOGA_DEBUG, "In test5cOnConnectFailure callback, context %p", context);
  1580. assert("This test should call test5cOnConnectFailure. ", 1, "test5cOnConnectFailure callback was called\n", 1);
  1581. test5cFinished = 1;
  1582. }
  1583. void test5cOnConnect(void* context, MQTTAsync_successData* response)
  1584. {
  1585. MyLog(LOGA_DEBUG, "In test5cOnConnectFailure callback, context %p", context);
  1586. assert("This connect should not succeed. ", 0, "test5cOnConnect callback was called\n", 0);
  1587. test5cFinished = 1;
  1588. }
  1589. int test5c(struct Options options)
  1590. {
  1591. char* testname = "test5c";
  1592. int subsqos = 2;
  1593. MQTTAsync c;
  1594. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  1595. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  1596. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  1597. int rc = 0;
  1598. int count = 0;
  1599. test5cFinished = 0;
  1600. failures = 0;
  1601. MyLog(LOGA_INFO,
  1602. "Starting SSL test 5c - Anonymous ciphers - client not using anonymous cipher");
  1603. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  1604. global_start_time = start_clock();
  1605. rc = MQTTAsync_create(&c, options.anon_connection, "test5c", MQTTCLIENT_PERSISTENCE_DEFAULT,
  1606. NULL);
  1607. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1608. if (rc != MQTTASYNC_SUCCESS)
  1609. goto exit;
  1610. opts.keepAliveInterval = 20;
  1611. opts.cleansession = 1;
  1612. opts.username = "testuser";
  1613. opts.password = "testpassword";
  1614. opts.will = &wopts;
  1615. opts.will->message = "will message";
  1616. opts.will->qos = 1;
  1617. opts.will->retained = 0;
  1618. opts.will->topicName = "will topic";
  1619. opts.will = NULL;
  1620. opts.onSuccess = test5cOnConnect;
  1621. opts.onFailure = test5cOnConnectFailure;
  1622. opts.context = c;
  1623. opts.ssl = &sslopts;
  1624. //opts.ssl->trustStore = /*file of certificates trusted by client*/
  1625. //opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  1626. //if (options.client_key_pass != NULL) opts.ssl->privateKeyPassword = options.client_key_pass;
  1627. //opts.ssl->enabledCipherSuites = "DEFAULT";
  1628. opts.ssl->enableServerCertAuth = 0;
  1629. MyLog(LOGA_DEBUG, "Connecting");
  1630. rc = MQTTAsync_connect(c, &opts);
  1631. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1632. if (rc != MQTTASYNC_SUCCESS)
  1633. goto exit;
  1634. while (!test5cFinished && ++count < 10000)
  1635. #if defined(_WIN32)
  1636. Sleep(100);
  1637. #else
  1638. usleep(10000L);
  1639. #endif
  1640. exit:
  1641. MQTTAsync_destroy(&c);
  1642. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  1643. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  1644. write_test_result();
  1645. return failures;
  1646. }
  1647. /*********************************************************************
  1648. Test6: More than one client object - simultaneous working.
  1649. *********************************************************************/
  1650. void test6OnConnectFailure(void* context, MQTTAsync_failureData* response)
  1651. {
  1652. AsyncTestClient* client = (AsyncTestClient*) context;
  1653. MyLog(LOGA_DEBUG, "In test6OnConnectFailure callback, context %p", context);
  1654. assert("There should be no failures in this test. ", 0, "test6OnConnectFailure callback was called\n", 0);
  1655. client->testFinished = 1;
  1656. }
  1657. void test6OnPublishFailure(void* context, MQTTAsync_failureData* response)
  1658. {
  1659. MyLog(LOGA_DEBUG, "In test6OnPublishFailure callback, context %p", context);
  1660. assert("There should be no failures in this test. ", 0, "test6OnPublishFailure callback was called\n", 0);
  1661. }
  1662. int test6(struct Options options)
  1663. {
  1664. char* testname = "test6";
  1665. #define num_clients 10
  1666. int subsqos = 2;
  1667. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  1668. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  1669. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  1670. int rc = 0;
  1671. int i;
  1672. AsyncTestClient tc[num_clients];
  1673. int test6finished = 0;
  1674. MyLog(LOGA_INFO, "Starting test 6 - multiple connections");
  1675. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  1676. global_start_time = start_clock();
  1677. for (i = 0; i < num_clients; ++i)
  1678. {
  1679. tc[i].maxmsgs = MAXMSGS;
  1680. tc[i].rcvdmsgs[0] = 0;
  1681. tc[i].rcvdmsgs[1] = 0;
  1682. tc[i].rcvdmsgs[2] = 0;
  1683. tc[i].sentmsgs[0] = 0;
  1684. tc[i].sentmsgs[1] = 0;
  1685. tc[i].sentmsgs[2] = 0;
  1686. tc[i].testFinished = 0;
  1687. sprintf(tc[i].clientid, "sslasync_test6_num_%d", i);
  1688. sprintf(tc[i].topic, "sslasync test6 topic num %d", i);
  1689. rc = MQTTAsync_create(&(tc[i].client), options.server_auth_connection, tc[i].clientid,
  1690. MQTTCLIENT_PERSISTENCE_NONE, NULL);
  1691. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1692. rc = MQTTAsync_setCallbacks(tc[i].client, &tc[i], NULL,
  1693. asyncTestMessageArrived, NULL);
  1694. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1695. opts.keepAliveInterval = 20;
  1696. opts.cleansession = 1;
  1697. opts.username = "testuser";
  1698. opts.password = "testpassword";
  1699. opts.will = &wopts;
  1700. opts.will->message = "will message";
  1701. opts.will->qos = 1;
  1702. opts.will->retained = 0;
  1703. opts.will->topicName = "will topic";
  1704. opts.onSuccess = asyncTestOnConnect;
  1705. opts.onFailure = test6OnConnectFailure;
  1706. opts.context = &tc[i];
  1707. opts.ssl = &sslopts;
  1708. if (options.server_key_file != NULL)
  1709. opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
  1710. opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  1711. if (options.client_key_pass != NULL)
  1712. opts.ssl->privateKeyPassword = options.client_key_pass;
  1713. //opts.ssl->enabledCipherSuites = "DEFAULT";
  1714. //opts.ssl->enabledServerCertAuth = 1;
  1715. MyLog(LOGA_DEBUG, "Connecting");
  1716. rc = MQTTAsync_connect(tc[i].client, &opts);
  1717. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1718. }
  1719. while (test6finished < num_clients)
  1720. {
  1721. MyLog(LOGA_DEBUG, "num_clients %d test_finished %d\n", num_clients,
  1722. test6finished);
  1723. #if defined(_WIN32)
  1724. Sleep(100);
  1725. #else
  1726. usleep(10000L);
  1727. #endif
  1728. for (i = 0; i < num_clients; ++i)
  1729. {
  1730. if (tc[i].testFinished)
  1731. {
  1732. test6finished++;
  1733. tc[i].testFinished = 0;
  1734. }
  1735. }
  1736. }
  1737. MyLog(LOGA_DEBUG, "test6: destroying clients");
  1738. for (i = 0; i < num_clients; ++i)
  1739. MQTTAsync_destroy(&tc[i].client);
  1740. //exit:
  1741. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  1742. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  1743. write_test_result();
  1744. return failures;
  1745. }
  1746. /*********************************************************************
  1747. Test7: Send and receive big messages
  1748. *********************************************************************/
  1749. void* test7_payload = NULL;
  1750. int test7_payloadlen = 0;
  1751. void test7OnConnectFailure(void* context, MQTTAsync_failureData* response)
  1752. {
  1753. AsyncTestClient* client = (AsyncTestClient*) context;
  1754. MyLog(LOGA_DEBUG, "In test7OnConnectFailure callback, %s", client->clientid);
  1755. assert("There should be no failures in this test. ", 0, "test7OnConnectFailure callback was called\n", 0);
  1756. client->testFinished = 1;
  1757. }
  1758. int test7OnPublishSuccessCount = 0;
  1759. int test7OnUnsubscribed = 0;
  1760. void test7OnPublishSuccess(void* context, MQTTAsync_successData* response)
  1761. {
  1762. AsyncTestClient* tc = (AsyncTestClient*) context;
  1763. MyLog(LOGA_DEBUG, "In test7OnPublishSuccess callback, %s, qos %d", tc->clientid,
  1764. response->alt.pub.message.qos);
  1765. test7OnPublishSuccessCount++;
  1766. }
  1767. void test7OnPublishFailure(void* context, MQTTAsync_failureData* response)
  1768. {
  1769. AsyncTestClient* client = (AsyncTestClient*) context;
  1770. MyLog(LOGA_DEBUG, "In test7OnPublishFailure callback, %s %d", client->clientid);
  1771. assert("There should be no failures in this test. ", 0, "test7OnPublishFailure callback was called\n", 0);
  1772. client->testFinished = 1;
  1773. }
  1774. void test7OnUnsubscribe(void* context, MQTTAsync_successData* response)
  1775. {
  1776. AsyncTestClient* tc = (AsyncTestClient*) context;
  1777. int rc;
  1778. MyLog(LOGA_DEBUG, "In test7OnUnsubscribe callback, %s %d %d", tc->clientid,
  1779. test7OnUnsubscribed, test7OnPublishSuccessCount);
  1780. test7OnUnsubscribed++;
  1781. }
  1782. int test7MessageArrived(void* context, char* topicName, int topicLen,
  1783. MQTTAsync_message* message)
  1784. {
  1785. AsyncTestClient* tc = (AsyncTestClient*) context;
  1786. static int message_count = 0;
  1787. int rc, i;
  1788. MyLog(LOGA_DEBUG, "In messageArrived callback %p", tc);
  1789. assert("Message size correct", message->payloadlen == test7_payloadlen,
  1790. "message size was %d", message->payloadlen);
  1791. for (i = 0; i < options.size; ++i)
  1792. {
  1793. if (((char*) test7_payload)[i] != ((char*) message->payload)[i])
  1794. {
  1795. assert("Message contents correct", ((char*)test7_payload)[i] != ((char*)message->payload)[i],
  1796. "message content was %c", ((char*)message->payload)[i]);
  1797. break;
  1798. }
  1799. }
  1800. if (++message_count == 1)
  1801. {
  1802. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  1803. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1804. pubmsg.payload = test7_payload;
  1805. pubmsg.payloadlen = test7_payloadlen;
  1806. pubmsg.qos = 1;
  1807. pubmsg.retained = 0;
  1808. opts.onSuccess = test7OnPublishSuccess;
  1809. opts.onFailure = test7OnPublishFailure;
  1810. opts.context = tc;
  1811. rc = MQTTAsync_sendMessage(tc->client, tc->topic, &pubmsg, &opts);
  1812. assert("Publish successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1813. }
  1814. else if (message_count < options.message_count)
  1815. {
  1816. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  1817. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1818. pubmsg.payload = test7_payload;
  1819. pubmsg.payloadlen = test7_payloadlen;
  1820. pubmsg.qos = 0;
  1821. pubmsg.retained = 0;
  1822. opts.onSuccess = test7OnPublishSuccess;
  1823. opts.onFailure = test7OnPublishFailure;
  1824. opts.context = tc;
  1825. rc = MQTTAsync_sendMessage(tc->client, tc->topic, &pubmsg, &opts);
  1826. assert("Publish successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1827. }
  1828. else
  1829. {
  1830. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1831. opts.onSuccess = test7OnUnsubscribe;
  1832. opts.context = tc;
  1833. rc = MQTTAsync_unsubscribe(tc->client, tc->topic, &opts);
  1834. assert("Unsubscribe successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1835. }
  1836. MQTTAsync_freeMessage(&message);
  1837. MQTTAsync_free(topicName);
  1838. return 1;
  1839. }
  1840. void test7OnSubscribe(void* context, MQTTAsync_successData* response)
  1841. {
  1842. AsyncTestClient* tc = (AsyncTestClient*) context;
  1843. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  1844. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1845. int rc, i;
  1846. MyLog(LOGA_DEBUG, "In subscribe onSuccess callback %p", tc);
  1847. pubmsg.payload = test7_payload = malloc(options.size);
  1848. pubmsg.payloadlen = test7_payloadlen = options.size;
  1849. srand(33);
  1850. for (i = 0; i < options.size; ++i)
  1851. ((char*) pubmsg.payload)[i] = rand() % 256;
  1852. pubmsg.qos = 2;
  1853. pubmsg.retained = 0;
  1854. pubmsg.payload = test7_payload;
  1855. pubmsg.payloadlen = test7_payloadlen;
  1856. opts.onSuccess = test7OnPublishSuccess;
  1857. opts.onFailure = test7OnPublishFailure;
  1858. opts.context = tc;
  1859. rc = MQTTAsync_send(tc->client, tc->topic, pubmsg.payloadlen, pubmsg.payload,
  1860. pubmsg.qos, pubmsg.retained, &opts);
  1861. assert("Publish successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1862. }
  1863. void test7OnConnect(void* context, MQTTAsync_successData* response)
  1864. {
  1865. AsyncTestClient* tc = (AsyncTestClient*) context;
  1866. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1867. int rc;
  1868. MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
  1869. opts.onSuccess = test7OnSubscribe;
  1870. opts.context = tc;
  1871. rc = MQTTAsync_subscribe(tc->client, tc->topic, 2, &opts);
  1872. assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1873. if (rc != MQTTASYNC_SUCCESS)
  1874. tc->testFinished = 1;
  1875. }
  1876. int test7(struct Options options)
  1877. {
  1878. char* testname = "test7";
  1879. int subsqos = 2;
  1880. AsyncTestClient tc = AsyncTestClient_initializer;
  1881. MQTTAsync c;
  1882. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  1883. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  1884. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  1885. MQTTAsync_disconnectOptions dopts = MQTTAsync_disconnectOptions_initializer;
  1886. int rc = 0;
  1887. char* test_topic = "C client test7";
  1888. int test_finished;
  1889. test_finished = failures = 0;
  1890. MyLog(LOGA_INFO, "Starting test 7 - big messages");
  1891. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  1892. global_start_time = start_clock();
  1893. rc = MQTTAsync_create(&c, options.server_auth_connection, "async_test_7", MQTTCLIENT_PERSISTENCE_NONE,
  1894. NULL);
  1895. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1896. if (rc != MQTTASYNC_SUCCESS)
  1897. {
  1898. MQTTAsync_destroy(&c);
  1899. goto exit;
  1900. }
  1901. rc = MQTTAsync_setCallbacks(c, &tc, NULL, test7MessageArrived, NULL);
  1902. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1903. tc.client = c;
  1904. sprintf(tc.clientid, "%s", testname);
  1905. sprintf(tc.topic, "C client SSL test7");
  1906. tc.maxmsgs = MAXMSGS;
  1907. //tc.rcvdmsgs = 0;
  1908. tc.subscribed = 0;
  1909. tc.testFinished = 0;
  1910. opts.keepAliveInterval = 60;
  1911. opts.cleansession = 1;
  1912. //opts.username = "testuser";
  1913. //opts.password = "testpassword";
  1914. opts.will = &wopts;
  1915. opts.will->message = "will message";
  1916. opts.will->qos = 1;
  1917. opts.will->retained = 0;
  1918. opts.will->topicName = "will topic";
  1919. opts.will = NULL;
  1920. opts.onSuccess = test7OnConnect;
  1921. opts.onFailure = test7OnConnectFailure;
  1922. opts.context = &tc;
  1923. opts.ssl = &sslopts;
  1924. if (options.server_key_file != NULL)
  1925. opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
  1926. if (options.client_key_file != NULL)
  1927. opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  1928. if (options.client_key_pass != NULL)
  1929. opts.ssl->privateKeyPassword = options.client_key_pass;
  1930. //opts.ssl->enabledCipherSuites = "DEFAULT";
  1931. //opts.ssl->enabledServerCertAuth = 1;
  1932. MyLog(LOGA_DEBUG, "Connecting");
  1933. rc = MQTTAsync_connect(c, &opts);
  1934. rc = 0;
  1935. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1936. if (rc != MQTTASYNC_SUCCESS)
  1937. goto exit;
  1938. while (tc.testFinished == 0 && test7OnUnsubscribed == 0 && test7OnPublishSuccessCount < options.message_count)
  1939. #if defined(_WIN32)
  1940. Sleep(100);
  1941. #else
  1942. usleep(1000L);
  1943. #endif
  1944. dopts.onSuccess = asyncTestOnDisconnect;
  1945. dopts.context = &tc;
  1946. rc = MQTTAsync_disconnect(c, &dopts);
  1947. while (!tc.testFinished)
  1948. #if defined(_WIN32)
  1949. Sleep(100);
  1950. #else
  1951. usleep(1000L);
  1952. #endif
  1953. MQTTAsync_destroy(&c);
  1954. if (test7_payload)
  1955. {
  1956. free(test7_payload);
  1957. test7_payload = NULL;
  1958. }
  1959. exit: MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  1960. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  1961. write_test_result();
  1962. return failures;
  1963. }
  1964. /*********************************************************************
  1965. Test8: TLS-PSK - client and server has a common pre-shared key
  1966. *********************************************************************/
  1967. static unsigned int onPSKAuth(const char* hint,
  1968. char* identity,
  1969. unsigned int max_identity_len,
  1970. unsigned char* psk,
  1971. unsigned int max_psk_len,
  1972. void* context)
  1973. {
  1974. unsigned char test_psk[] = {0x50, 0x53, 0x4B, 0x00}; /* {'P', 'S', 'K', '\0' } */
  1975. MyLog(LOGA_DEBUG, "PSK auth callback");
  1976. assert("Good application context in onPSKAuth", context == (void *) 42, "context was %d\n", context);
  1977. strncpy(identity, "id", max_identity_len);
  1978. memcpy(psk, test_psk, sizeof(test_psk));
  1979. return sizeof(test_psk);
  1980. }
  1981. int test8(struct Options options)
  1982. {
  1983. char* testname = "test8";
  1984. AsyncTestClient tc =
  1985. AsyncTestClient_initializer;
  1986. MQTTAsync c;
  1987. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  1988. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  1989. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  1990. int rc = 0;
  1991. failures = 0;
  1992. MyLog(LOGA_INFO, "Starting test 8 - TLS-PSK - client and server has a common pre-shared key");
  1993. fprintf(xml, "<testcase classname=\"test8\" name=\"%s\"", testname);
  1994. global_start_time = start_clock();
  1995. MQTTAsync_create(&c, options.psk_connection, "test8", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  1996. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1997. if (rc != MQTTASYNC_SUCCESS)
  1998. goto exit;
  1999. tc.client = c;
  2000. sprintf(tc.clientid, "%s", testname);
  2001. sprintf(tc.topic, "C client SSL test8");
  2002. tc.maxmsgs = MAXMSGS;
  2003. tc.subscribed = 0;
  2004. tc.testFinished = 0;
  2005. opts.keepAliveInterval = 20;
  2006. opts.cleansession = 1;
  2007. opts.username = "testuser";
  2008. opts.password = "testpassword";
  2009. opts.onSuccess = asyncTestOnConnect;
  2010. opts.onFailure = asyncTestOnSubscribeFailure;
  2011. opts.context = &tc;
  2012. opts.ssl = &sslopts;
  2013. opts.ssl->ssl_psk_cb = onPSKAuth;
  2014. opts.ssl->ssl_psk_context = (void *) 42;
  2015. opts.ssl->enabledCipherSuites = "PSK-AES128-CBC-SHA";
  2016. rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
  2017. asyncTestOnDeliveryComplete);
  2018. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  2019. MyLog(LOGA_DEBUG, "Connecting");
  2020. rc = MQTTAsync_connect(c, &opts);
  2021. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  2022. if (rc != MQTTASYNC_SUCCESS)
  2023. goto exit;
  2024. while (!tc.subscribed && !tc.testFinished)
  2025. #if defined(_WIN32)
  2026. Sleep(100);
  2027. #else
  2028. usleep(10000L);
  2029. #endif
  2030. if (tc.testFinished)
  2031. goto exit;
  2032. while (!tc.testFinished)
  2033. #if defined(_WIN32)
  2034. Sleep(100);
  2035. #else
  2036. usleep(10000L);
  2037. #endif
  2038. MyLog(LOGA_DEBUG, "Stopping");
  2039. exit:
  2040. MQTTAsync_destroy(&c);
  2041. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  2042. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  2043. write_test_result();
  2044. return failures;
  2045. }
  2046. /*********************************************************************
  2047. Test9: Mutual SSL Authentication - Testing CApath
  2048. *********************************************************************/
  2049. void test9OnConnectFailure(void* context, MQTTAsync_failureData* response)
  2050. {
  2051. AsyncTestClient* client = (AsyncTestClient*) context;
  2052. MyLog(LOGA_DEBUG, "In test9OnConnectFailure callback, %s",
  2053. client->clientid);
  2054. assert("There should be no failures in this test. ", 0, "test9OnConnectFailure callback was called\n", 0);
  2055. client->testFinished = 1;
  2056. }
  2057. int test9(struct Options options)
  2058. {
  2059. char* testname = "test9";
  2060. AsyncTestClient tc =
  2061. AsyncTestClient_initializer;
  2062. MQTTAsync c;
  2063. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  2064. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  2065. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  2066. int rc = 0;
  2067. failures = 0;
  2068. MyLog(LOGA_INFO, "Starting test 9 - Mutual SSL authentication with CApath");
  2069. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  2070. global_start_time = start_clock();
  2071. MQTTAsync_create(&c, options.mutual_auth_connection, "test9", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  2072. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  2073. if (rc != MQTTASYNC_SUCCESS)
  2074. goto exit;
  2075. tc.client = c;
  2076. sprintf(tc.clientid, "%s", testname);
  2077. sprintf(tc.topic, "C client SSL test9");
  2078. tc.maxmsgs = MAXMSGS;
  2079. //tc.rcvdmsgs = 0;
  2080. tc.subscribed = 0;
  2081. tc.testFinished = 0;
  2082. opts.keepAliveInterval = 20;
  2083. opts.cleansession = 1;
  2084. opts.username = "testuser";
  2085. opts.password = "testpassword";
  2086. opts.will = &wopts;
  2087. opts.will->message = "will message";
  2088. opts.will->qos = 1;
  2089. opts.will->retained = 0;
  2090. opts.will->topicName = "will topic";
  2091. opts.will = NULL;
  2092. opts.onSuccess = asyncTestOnConnect;
  2093. opts.onFailure = test9OnConnectFailure;
  2094. opts.context = &tc;
  2095. opts.ssl = &sslopts;
  2096. if (options.server_key_file != NULL)
  2097. opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
  2098. opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  2099. if (options.client_key_pass != NULL)
  2100. opts.ssl->privateKeyPassword = options.client_key_pass;
  2101. opts.ssl->CApath = options.capath;
  2102. opts.ssl->enableServerCertAuth = 1;
  2103. opts.ssl->verify = 1;
  2104. MyLog(LOGA_DEBUG, "enableServerCertAuth %d\n", opts.ssl->enableServerCertAuth);
  2105. MyLog(LOGA_DEBUG, "verify %d\n", opts.ssl->verify);
  2106. rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
  2107. asyncTestOnDeliveryComplete);
  2108. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  2109. MyLog(LOGA_DEBUG, "Connecting");
  2110. rc = MQTTAsync_connect(c, &opts);
  2111. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  2112. if (rc != MQTTASYNC_SUCCESS)
  2113. goto exit;
  2114. while (!tc.subscribed && !tc.testFinished)
  2115. #if defined(_WIN32)
  2116. Sleep(100);
  2117. #else
  2118. usleep(10000L);
  2119. #endif
  2120. if (tc.testFinished)
  2121. goto exit;
  2122. while (!tc.testFinished)
  2123. #if defined(_WIN32)
  2124. Sleep(100);
  2125. #else
  2126. usleep(10000L);
  2127. #endif
  2128. MyLog(LOGA_DEBUG, "Stopping");
  2129. exit: MQTTAsync_destroy(&c);
  2130. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  2131. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  2132. write_test_result();
  2133. return failures;
  2134. }
  2135. /*********************************************************************
  2136. Test10: Mutual SSL Authentication - Testing CApath
  2137. *********************************************************************/
  2138. int test10Finished;
  2139. void test10OnConnectFailure(void* context, MQTTAsync_failureData* response)
  2140. {
  2141. AsyncTestClient* client = (AsyncTestClient*) context;
  2142. MyLog(LOGA_DEBUG, "In test10OnConnectFailure callback, %s",
  2143. client->clientid);
  2144. assert("This test should call test10OnConnectFailure. ", 1, "test10OnConnectFailure callback was called\n", 1);
  2145. test10Finished = 1;
  2146. }
  2147. void test10OnConnect(void* context, MQTTAsync_successData* response)
  2148. {
  2149. MyLog(LOGA_DEBUG, "In test10OnConnect callback, context %p", context);
  2150. assert("This connect should not succeed. ", 0, "test10OnConnect callback was called\n", 0);
  2151. test10Finished = 1;
  2152. }
  2153. int test10(struct Options options)
  2154. {
  2155. char* testname = "test10";
  2156. AsyncTestClient tc =
  2157. AsyncTestClient_initializer;
  2158. MQTTAsync c;
  2159. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  2160. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  2161. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  2162. int rc = 0;
  2163. failures = 0;
  2164. test10Finished = 0;
  2165. MyLog(LOGA_INFO, "Starting test 10 - dummy CApath");
  2166. fprintf(xml, "<testcase classname=\"test10\" name=\"%s\"", testname);
  2167. global_start_time = start_clock();
  2168. MQTTAsync_create(&c, options.mutual_auth_connection, "test10", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  2169. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  2170. if (rc != MQTTASYNC_SUCCESS)
  2171. goto exit;
  2172. tc.client = c;
  2173. sprintf(tc.clientid, "%s", testname);
  2174. sprintf(tc.topic, "C client SSL test10");
  2175. tc.maxmsgs = MAXMSGS;
  2176. //tc.rcvdmsgs = 0;
  2177. tc.subscribed = 0;
  2178. tc.testFinished = 0;
  2179. opts.keepAliveInterval = 20;
  2180. opts.cleansession = 1;
  2181. opts.username = "testuser";
  2182. opts.password = "testpassword";
  2183. opts.will = &wopts;
  2184. opts.will->message = "will message";
  2185. opts.will->qos = 1;
  2186. opts.will->retained = 0;
  2187. opts.will->topicName = "will topic";
  2188. opts.will = NULL;
  2189. opts.onSuccess = test10OnConnect;
  2190. opts.onFailure = test10OnConnectFailure;
  2191. opts.context = &tc;
  2192. opts.ssl = &sslopts;
  2193. if (options.server_key_file != NULL)
  2194. opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
  2195. opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  2196. if (options.client_key_pass != NULL)
  2197. opts.ssl->privateKeyPassword = options.client_key_pass;
  2198. opts.ssl->CApath = "DUMMY";
  2199. opts.ssl->enableServerCertAuth = 1;
  2200. opts.ssl->verify = 1;
  2201. MyLog(LOGA_DEBUG, "enableServerCertAuth %d\n", opts.ssl->enableServerCertAuth);
  2202. MyLog(LOGA_DEBUG, "verify %d\n", opts.ssl->verify);
  2203. rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
  2204. asyncTestOnDeliveryComplete);
  2205. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  2206. MyLog(LOGA_DEBUG, "Connecting");
  2207. rc = MQTTAsync_connect(c, &opts);
  2208. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  2209. if (rc != MQTTASYNC_SUCCESS)
  2210. goto exit;
  2211. while (!test10Finished)
  2212. #if defined(_WIN32)
  2213. Sleep(100);
  2214. #else
  2215. usleep(10000L);
  2216. #endif
  2217. MyLog(LOGA_DEBUG, "Stopping");
  2218. exit: MQTTAsync_destroy(&c);
  2219. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  2220. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  2221. write_test_result();
  2222. return failures;
  2223. }
  2224. void handleTrace(enum MQTTASYNC_TRACE_LEVELS level, char* message)
  2225. {
  2226. printf("%s\n", message);
  2227. }
  2228. int main(int argc, char** argv)
  2229. {
  2230. int* numtests = &tests;
  2231. int rc = 0;
  2232. int (*tests[])() =
  2233. { NULL, test1, test2a, test2b, test2c, test2d, test3a, test3b, test4, /* test5a,
  2234. test5b, test5c, */ test6, test7, test8, test9, test10, test2e };
  2235. xml = fopen("TEST-test5.xml", "w");
  2236. fprintf(xml, "<testsuite name=\"test5\" tests=\"%d\">\n", (int)ARRAY_SIZE(tests) - 1);
  2237. MQTTAsync_setTraceCallback(handleTrace);
  2238. getopts(argc, argv);
  2239. if (options.test_no == 0)
  2240. { /* run all the tests */
  2241. for (options.test_no = 1; options.test_no < ARRAY_SIZE(tests); ++options.test_no)
  2242. {
  2243. failures = 0;
  2244. MQTTAsync_setTraceLevel(MQTTASYNC_TRACE_ERROR);
  2245. rc += tests[options.test_no](options); /* return number of failures. 0 = test succeeded */
  2246. }
  2247. }
  2248. else
  2249. {
  2250. MQTTAsync_setTraceLevel(MQTTASYNC_TRACE_ERROR);
  2251. rc = tests[options.test_no](options); /* run just the selected test */
  2252. }
  2253. MyLog(LOGA_INFO, "Total tests run: %d", *numtests);
  2254. if (rc == 0)
  2255. MyLog(LOGA_INFO, "verdict pass");
  2256. else
  2257. MyLog(LOGA_INFO, "verdict fail");
  2258. fprintf(xml, "</testsuite>\n");
  2259. fclose(xml);
  2260. return rc;
  2261. }
  2262. /* Local Variables: */
  2263. /* indent-tabs-mode: t */
  2264. /* c-basic-offset: 8 */
  2265. /* End: */