test5.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736
  1. /*******************************************************************************
  2. * Copyright (c) 2012, 2020 IBM Corp.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v2.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * https://www.eclipse.org/legal/epl-2.0/
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * 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://m2m.eclipse.org:18883",
  71. "ssl://m2m.eclipse.org:18884",
  72. "ssl://m2m.eclipse.org:18887",
  73. "ssl://m2m.eclipse.org:18885",
  74. "ssl://m2m.eclipse.org:18886",
  75. "ssl://m2m.eclipse.org: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 / 1000);
  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. rc = MQTTAsync_disconnect(tc->client, &opts);
  478. }
  479. void asyncTestOnSubscribe(void* context, MQTTAsync_successData* response)
  480. {
  481. AsyncTestClient* tc = (AsyncTestClient*) context;
  482. int rc, i;
  483. MyLog(LOGA_DEBUG, "In asyncTestOnSubscribe callback, %s", tc->clientid);
  484. //rc = Thread_lock_mutex(client_mutex);
  485. tc->subscribed = 1;
  486. //rc = Thread_unlock_mutex(client_mutex);
  487. for (i = 0; i < 3; i++)
  488. {
  489. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  490. pubmsg.payload
  491. = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  492. pubmsg.payloadlen = 11;
  493. pubmsg.qos = i;
  494. pubmsg.retained = 0;
  495. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  496. //opts.onSuccess = asyncTestOnSend;
  497. opts.context = &tc;
  498. rc = MQTTAsync_send(tc->client, tc->topic, pubmsg.payloadlen,
  499. pubmsg.payload, pubmsg.qos, pubmsg.retained, &opts);
  500. assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  501. tc->sentmsgs[i]++;
  502. MyLog(LOGA_DEBUG, "Maxmsgs %d", tc->maxmsgs);
  503. }
  504. }
  505. int asyncTestMessageArrived(void* context, char* topicName, int topicLen,
  506. MQTTAsync_message* m)
  507. {
  508. AsyncTestClient* tc = (AsyncTestClient*) context;
  509. int rc;
  510. //rc = Thread_lock_mutex(client_mutex);
  511. tc->rcvdmsgs[m->qos]++;
  512. //printf("Received messages: %d\n", tc->rcvdmsgs[m->qos]);
  513. MyLog(LOGA_DEBUG,
  514. "In asyncTestMessageArrived callback, %s total to exit %d, total received %d,%d,%d",
  515. tc->clientid, (tc->maxmsgs * 3), tc->rcvdmsgs[0], tc->rcvdmsgs[1],
  516. tc->rcvdmsgs[2]);
  517. if (tc->sentmsgs[m->qos] < tc->maxmsgs)
  518. {
  519. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  520. //opts.onSuccess = asyncTestOnSend;
  521. opts.context = tc;
  522. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  523. pubmsg.payload
  524. = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  525. pubmsg.payloadlen = 11;
  526. pubmsg.qos = m->qos;
  527. pubmsg.retained = 0;
  528. rc = MQTTAsync_send(tc->client, tc->topic, pubmsg.payloadlen,
  529. pubmsg.payload, pubmsg.qos, pubmsg.retained, &opts);
  530. assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d messages sent %d,%d,%d", rc);
  531. MyLog(LOGA_DEBUG, "Messages sent %d,%d,%d", tc->sentmsgs[0],
  532. tc->sentmsgs[1], tc->sentmsgs[2]);
  533. tc->sentmsgs[m->qos]++;
  534. }
  535. if ((tc->rcvdmsgs[0] + tc->rcvdmsgs[1] + tc->rcvdmsgs[2]) == (tc->maxmsgs* 3))
  536. {
  537. MyLog(LOGA_DEBUG, "Ready to unsubscribe");
  538. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  539. opts.onSuccess = asyncTestOnUnsubscribe;
  540. opts.context = tc;
  541. rc = MQTTAsync_unsubscribe(tc->client, tc->topic, &opts);
  542. assert("Unsubscribe successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  543. }
  544. //rc = Thread_unlock_mutex(client_mutex);
  545. MyLog(LOGA_DEBUG, "Leaving asyncTestMessageArrived callback");
  546. MQTTAsync_freeMessage(&m);
  547. MQTTAsync_free(topicName);
  548. return 1;
  549. }
  550. void asyncTestOnDeliveryComplete(void* context, MQTTAsync_token token)
  551. {
  552. }
  553. void asyncTestOnConnect(void* context, MQTTAsync_successData* response)
  554. {
  555. AsyncTestClient* tc = (AsyncTestClient*) context;
  556. int subsqos = 2;
  557. int rc;
  558. MyLog(LOGA_DEBUG, "In asyncTestOnConnect callback, %s", tc->clientid);
  559. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  560. opts.onSuccess = asyncTestOnSubscribe;
  561. opts.onFailure = asyncTestOnSubscribeFailure;
  562. opts.context = tc;
  563. rc = MQTTAsync_subscribe(tc->client, tc->topic, subsqos, &opts);
  564. assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  565. }
  566. /*********************************************************************
  567. Test1: SSL connection to non SSL MQTT server
  568. *********************************************************************/
  569. int test1Finished = 0;
  570. int test1OnFailureCalled = 0;
  571. void test1OnFailure(void* context, MQTTAsync_failureData* response)
  572. {
  573. MyLog(LOGA_DEBUG, "In connect onFailure callback, context %p", context);
  574. test1OnFailureCalled++;
  575. test1Finished = 1;
  576. }
  577. void test1OnConnect(void* context, MQTTAsync_successData* response)
  578. {
  579. MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p\n", context);
  580. assert("Connect should not succeed", 0, "connect success callback was called", 0);
  581. test1Finished = 1;
  582. }
  583. int test1(struct Options options)
  584. {
  585. char* testname = "test1";
  586. int subsqos = 2;
  587. MQTTAsync c;
  588. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  589. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  590. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  591. int rc = 0;
  592. char* test_topic = "C client SSL test1";
  593. int count = 0;
  594. test1Finished = 0;
  595. failures = 0;
  596. MyLog(LOGA_INFO, "Starting SSL test 1 - connection to nonSSL MQTT server");
  597. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  598. global_start_time = start_clock();
  599. rc = MQTTAsync_create(&c, "rubbish://wrong", "test1", MQTTCLIENT_PERSISTENCE_DEFAULT,
  600. NULL);
  601. assert("bad rc from create", rc == MQTTASYNC_BAD_PROTOCOL, "rc was %d \n", rc);
  602. rc = MQTTAsync_create(&c, options.connection, "test1", MQTTCLIENT_PERSISTENCE_DEFAULT,
  603. NULL);
  604. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d \n", rc);
  605. if (rc != MQTTASYNC_SUCCESS)
  606. {
  607. MQTTAsync_destroy(&c);
  608. goto exit;
  609. }
  610. opts.keepAliveInterval = 20;
  611. opts.cleansession = 1;
  612. opts.username = "testuser";
  613. opts.password = "testpassword";
  614. opts.will = &wopts;
  615. opts.will->message = "will message";
  616. opts.will->qos = 1;
  617. opts.will->retained = 0;
  618. opts.will->topicName = "will topic";
  619. opts.will = NULL;
  620. opts.onSuccess = test1OnConnect;
  621. opts.onFailure = test1OnFailure;
  622. opts.context = c;
  623. rc = MQTTAsync_connect(c, &opts);
  624. assert("Bad rc from connect", rc == MQTTASYNC_NULL_PARAMETER, "rc was %d ", rc);
  625. opts.ssl = &sslopts;
  626. opts.ssl->enableServerCertAuth = 0;
  627. MyLog(LOGA_DEBUG, "Connecting");
  628. rc = MQTTAsync_connect(c, &opts);
  629. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d ", rc);
  630. if (rc != MQTTASYNC_SUCCESS)
  631. {
  632. failures++;
  633. goto exit;
  634. }
  635. /* wait for success or failure callback */
  636. while (!test1Finished && ++count < 10000)
  637. #if defined(_WIN32)
  638. Sleep(100);
  639. #else
  640. usleep(10000L);
  641. #endif
  642. exit: MQTTAsync_destroy(&c);
  643. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  644. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  645. write_test_result();
  646. return failures;
  647. }
  648. /*********************************************************************
  649. Test2a: Mutual SSL Authentication - Certificates in place on client and server
  650. *********************************************************************/
  651. void test2aOnConnectFailure(void* context, MQTTAsync_failureData* response)
  652. {
  653. AsyncTestClient* client = (AsyncTestClient*) context;
  654. MyLog(LOGA_DEBUG, "In test2aOnConnectFailure callback, %s",
  655. client->clientid);
  656. assert("There should be no failures in this test. ", 0, "test2aOnConnectFailure callback was called\n", 0);
  657. client->testFinished = 1;
  658. }
  659. void test2aOnPublishFailure(void* context, MQTTAsync_failureData* response)
  660. {
  661. AsyncTestClient* client = (AsyncTestClient*) context;
  662. MyLog(LOGA_DEBUG, "In test2aOnPublishFailure callback, %s",
  663. client->clientid);
  664. assert("There should be no failures in this test. ", 0, "test2aOnPublishFailure callback was called\n", 0);
  665. }
  666. int test2a(struct Options options)
  667. {
  668. char* testname = "test2a";
  669. AsyncTestClient tc =
  670. AsyncTestClient_initializer;
  671. MQTTAsync c;
  672. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  673. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  674. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  675. int rc = 0;
  676. failures = 0;
  677. MyLog(LOGA_INFO, "Starting test 2a - Mutual SSL authentication");
  678. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  679. global_start_time = start_clock();
  680. rc = MQTTAsync_create(&c, options.mutual_auth_connection, "test2a", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  681. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  682. if (rc != MQTTASYNC_SUCCESS)
  683. goto exit;
  684. tc.client = c;
  685. sprintf(tc.clientid, "%s", testname);
  686. sprintf(tc.topic, "C client SSL test2a");
  687. tc.maxmsgs = MAXMSGS;
  688. //tc.rcvdmsgs = 0;
  689. tc.subscribed = 0;
  690. tc.testFinished = 0;
  691. opts.keepAliveInterval = 20;
  692. opts.cleansession = 1;
  693. opts.username = "testuser";
  694. opts.password = "testpassword";
  695. opts.will = &wopts;
  696. opts.will->message = "will message";
  697. opts.will->qos = 1;
  698. opts.will->retained = 0;
  699. opts.will->topicName = "will topic";
  700. opts.will = NULL;
  701. opts.onSuccess = asyncTestOnConnect;
  702. opts.onFailure = test2aOnConnectFailure;
  703. opts.context = &tc;
  704. opts.ssl = &sslopts;
  705. if (options.server_key_file != NULL)
  706. opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
  707. opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  708. if (options.client_key_pass != NULL)
  709. opts.ssl->privateKeyPassword = options.client_key_pass;
  710. //opts.ssl->enabledCipherSuites = "DEFAULT";
  711. //opts.ssl->enabledServerCertAuth = 1;
  712. opts.ssl->verify = 1;
  713. MyLog(LOGA_DEBUG, "enableServerCertAuth %d\n", opts.ssl->enableServerCertAuth);
  714. MyLog(LOGA_DEBUG, "verify %d\n", opts.ssl->verify);
  715. rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
  716. asyncTestOnDeliveryComplete);
  717. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  718. MyLog(LOGA_DEBUG, "Connecting");
  719. rc = MQTTAsync_connect(c, &opts);
  720. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  721. if (rc != MQTTASYNC_SUCCESS)
  722. goto exit;
  723. while (!tc.subscribed && !tc.testFinished)
  724. #if defined(_WIN32)
  725. Sleep(100);
  726. #else
  727. usleep(10000L);
  728. #endif
  729. if (tc.testFinished)
  730. goto exit;
  731. while (!tc.testFinished)
  732. #if defined(_WIN32)
  733. Sleep(100);
  734. #else
  735. usleep(10000L);
  736. #endif
  737. MyLog(LOGA_DEBUG, "Stopping");
  738. exit: MQTTAsync_destroy(&c);
  739. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  740. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  741. write_test_result();
  742. return failures;
  743. }
  744. /*********************************************************************
  745. Test2b: Mutual SSL Authentication - Server does not have Client cert
  746. *********************************************************************/
  747. int test2bFinished;
  748. void test2bOnConnectFailure(void* context, MQTTAsync_failureData* response)
  749. {
  750. MyLog(LOGA_DEBUG, "In test2bOnConnectFailure callback, context %p", context);
  751. assert("This test should call test2bOnConnectFailure. ", 1, "test2bOnConnectFailure callback was called\n", 1);
  752. test2bFinished = 1;
  753. }
  754. void test2bOnConnect(void* context, MQTTAsync_successData* response)
  755. {
  756. MyLog(LOGA_DEBUG, "In test2bOnConnectFailure callback, context %p", context);
  757. assert("This connect should not succeed. ", 0, "test2bOnConnect callback was called\n", 0);
  758. test2bFinished = 1;
  759. }
  760. int test2b(struct Options options)
  761. {
  762. char* testname = "test2b";
  763. int subsqos = 2;
  764. MQTTAsync c;
  765. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  766. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  767. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  768. int rc = 0;
  769. int count = 0;
  770. test2bFinished = 0;
  771. failures = 0;
  772. MyLog(LOGA_INFO,
  773. "Starting test 2b - connection to SSL MQTT server with clientauth=req but server does not have client cert");
  774. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  775. global_start_time = start_clock();
  776. rc = MQTTAsync_create(&c, options.nocert_mutual_auth_connection,
  777. "test2b", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  778. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  779. if (rc != MQTTASYNC_SUCCESS)
  780. {
  781. MQTTAsync_destroy(&c);
  782. goto exit;
  783. }
  784. opts.keepAliveInterval = 20;
  785. opts.cleansession = 1;
  786. opts.username = "testuser";
  787. opts.password = "testpassword";
  788. opts.will = &wopts;
  789. opts.will->message = "will message";
  790. opts.will->qos = 1;
  791. opts.will->retained = 0;
  792. opts.will->topicName = "will topic";
  793. opts.will = NULL;
  794. opts.onSuccess = test2bOnConnect;
  795. opts.onFailure = test2bOnConnectFailure;
  796. opts.context = c;
  797. opts.ssl = &sslopts;
  798. if (options.server_key_file != NULL)
  799. opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
  800. opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  801. if (options.client_key_pass != NULL)
  802. opts.ssl->privateKeyPassword = options.client_key_pass;
  803. //opts.ssl->enabledCipherSuites = "DEFAULT";
  804. //opts.ssl->enabledServerCertAuth = 0;
  805. MyLog(LOGA_DEBUG, "Connecting");
  806. rc = MQTTAsync_connect(c, &opts);
  807. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  808. if (rc != MQTTASYNC_SUCCESS)
  809. goto exit;
  810. while (!test2bFinished && ++count < 10000)
  811. #if defined(_WIN32)
  812. Sleep(100);
  813. #else
  814. usleep(10000L);
  815. #endif
  816. exit: MQTTAsync_destroy(&c);
  817. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  818. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  819. write_test_result();
  820. return failures;
  821. }
  822. /*********************************************************************
  823. Test2c: Mutual SSL Authentication - Client does not have Server cert
  824. *********************************************************************/
  825. int test2cFinished;
  826. void test2cOnConnectFailure(void* context, MQTTAsync_failureData* response)
  827. {
  828. MyLog(LOGA_DEBUG, "In test2cOnConnectFailure callback, context %p", context);
  829. assert("This test should call test2cOnConnectFailure. ", 1, "test2cOnConnectFailure callback was called\n", 0);
  830. test2cFinished = 1;
  831. }
  832. void test2cOnConnect(void* context, MQTTAsync_successData* response)
  833. {
  834. MyLog(LOGA_DEBUG, "In test2cOnConnect callback, context %p", context);
  835. assert("This connect should not succeed. ", 0, "test2cOnConnect callback was called\n", 0);
  836. test2cFinished = 1;
  837. }
  838. int test2c(struct Options options)
  839. {
  840. char* testname = "test2c";
  841. int subsqos = 2;
  842. MQTTAsync c;
  843. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  844. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  845. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  846. int rc = 0;
  847. char* test_topic = "C client test2c";
  848. int count = 0;
  849. failures = 0;
  850. MyLog(
  851. LOGA_INFO,
  852. "Starting test 2c - connection to SSL MQTT server, server auth enabled but unknown cert");
  853. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  854. global_start_time = start_clock();
  855. rc = MQTTAsync_create(&c, options.nocert_mutual_auth_connection,
  856. "test2c", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  857. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  858. if (rc != MQTTASYNC_SUCCESS)
  859. {
  860. MQTTAsync_destroy(&c);
  861. goto exit;
  862. }
  863. opts.keepAliveInterval = 20;
  864. opts.cleansession = 1;
  865. opts.username = "testuser";
  866. opts.password = "testpassword";
  867. opts.will = &wopts;
  868. opts.will->message = "will message";
  869. opts.will->qos = 1;
  870. opts.will->retained = 0;
  871. opts.will->topicName = "will topic";
  872. opts.will = NULL;
  873. opts.onSuccess = test2cOnConnect;
  874. opts.onFailure = test2cOnConnectFailure;
  875. opts.context = c;
  876. opts.ssl = &sslopts;
  877. //if (options.server_key_file != NULL) opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
  878. opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  879. if (options.client_key_pass != NULL)
  880. opts.ssl->privateKeyPassword = options.client_key_pass;
  881. //opts.ssl->enabledCipherSuites = "DEFAULT";
  882. //opts.ssl->enabledServerCertAuth = 0;
  883. MyLog(LOGA_DEBUG, "Connecting");
  884. rc = MQTTAsync_connect(c, &opts);
  885. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  886. if (rc != MQTTASYNC_SUCCESS)
  887. {
  888. failures++;
  889. goto exit;
  890. }
  891. while (!test2cFinished && ++count < 10000)
  892. #if defined(_WIN32)
  893. Sleep(100);
  894. #else
  895. usleep(10000L);
  896. #endif
  897. exit: MQTTAsync_destroy(&c);
  898. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  899. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  900. write_test_result();
  901. return failures;
  902. }
  903. /*********************************************************************
  904. Test2d: Mutual SSL Authentication - client has no certs
  905. *********************************************************************/
  906. int test2dFinished;
  907. void test2dOnConnectFailure(void* context, MQTTAsync_failureData* response)
  908. {
  909. MyLog(LOGA_DEBUG, "In test2dOnConnectFailure callback, context %p", context);
  910. assert("This test should call test2dOnConnectFailure. ", 1, "test2dOnConnectFailure callback was called\n", 0);
  911. test2dFinished = 1;
  912. }
  913. void test2dOnConnect(void* context, MQTTAsync_successData* response)
  914. {
  915. MyLog(LOGA_DEBUG, "In test2dOnConnect callback, context %p", context);
  916. assert("This connect should not succeed. ", 0, "test2dOnConnect callback was called\n", 0);
  917. test2dFinished = 1;
  918. }
  919. int test2d(struct Options options)
  920. {
  921. char* testname = "test2d";
  922. int subsqos = 2;
  923. MQTTAsync c;
  924. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  925. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  926. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  927. int rc = 0;
  928. char* test_topic = "C client test2d";
  929. int count = 0;
  930. unsigned int iteration = 0;
  931. failures = 0;
  932. MyLog(
  933. LOGA_INFO,
  934. "Starting test 2d - connection to SSL MQTT server, server auth enabled but unknown cert");
  935. fprintf(xml, "<testcase classname=\"test2d\" name=\"%s\"", testname);
  936. global_start_time = start_clock();
  937. // As reported in https://github.com/eclipse/paho.mqtt.c/issues/190
  938. // there is/was some race condition, which caused _sometimes_ that the library failed to detect,
  939. // that the connect attempt has already failed.
  940. // Therefore we need to test this several times!
  941. for (iteration = 0; !failures && (iteration < 20) ; iteration++)
  942. {
  943. count = 0;
  944. MQTTAsync_setTraceLevel(MQTTASYNC_TRACE_ERROR);
  945. rc = MQTTAsync_create(&c, options.mutual_auth_connection,
  946. "test2d", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  947. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  948. if (rc != MQTTASYNC_SUCCESS)
  949. {
  950. MQTTAsync_destroy(&c);
  951. failures++;
  952. break;
  953. }
  954. opts.keepAliveInterval = 60;
  955. opts.cleansession = 1;
  956. opts.will = &wopts;
  957. opts.will->message = "will message";
  958. opts.will->qos = 1;
  959. opts.will->retained = 0;
  960. opts.will->topicName = "will topic";
  961. opts.will = NULL;
  962. opts.onSuccess = test2dOnConnect;
  963. opts.onFailure = test2dOnConnectFailure;
  964. opts.context = c;
  965. opts.ssl = &sslopts;
  966. if (options.server_key_file != NULL) opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
  967. opts.ssl->keyStore = NULL; /*file of certificate for client to present to server - In this test the client has no certificate! */
  968. test2dFinished = 0;
  969. MyLog(LOGA_DEBUG, "Connecting");
  970. rc = MQTTAsync_connect(c, &opts);
  971. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  972. if (rc != MQTTASYNC_SUCCESS)
  973. {
  974. failures++;
  975. MyLog(LOGA_INFO, "Failed in iteration %d\n",iteration);
  976. MQTTAsync_destroy(&c);
  977. break;
  978. }
  979. #define TEST2D_COUNT 1000
  980. while (!test2dFinished && ++count < TEST2D_COUNT)
  981. {
  982. #if defined(_WIN32)
  983. Sleep(100);
  984. #else
  985. usleep(10000L);
  986. #endif
  987. }
  988. if (!test2dFinished && count >= TEST2D_COUNT)
  989. {
  990. MyLog(LOGA_INFO, "Failed in iteration %d\n",iteration);
  991. failures++;
  992. }
  993. MQTTAsync_destroy(&c);
  994. }
  995. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  996. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  997. write_test_result();
  998. return failures;
  999. }
  1000. /*********************************************************************
  1001. Test2e: Mutual SSL Authentication using serverURIs
  1002. *********************************************************************/
  1003. void test2eOnConnectFailure(void* context, MQTTAsync_failureData* response)
  1004. {
  1005. AsyncTestClient* client = (AsyncTestClient*) context;
  1006. MyLog(LOGA_DEBUG, "In test2eOnConnectFailure callback, %s",
  1007. client->clientid);
  1008. assert("There should be no failures in this test. ", 0, "test2eOnConnectFailure callback was called\n", 0);
  1009. client->testFinished = 1;
  1010. }
  1011. void test2eOnPublishFailure(void* context, MQTTAsync_failureData* response)
  1012. {
  1013. AsyncTestClient* client = (AsyncTestClient*) context;
  1014. MyLog(LOGA_DEBUG, "In test2eOnPublishFailure callback, %s",
  1015. client->clientid);
  1016. assert("There should be no failures in this test. ", 0, "test2eOnPublishFailure callback was called\n", 0);
  1017. }
  1018. int test2e(struct Options options)
  1019. {
  1020. char* testname = "test2e";
  1021. AsyncTestClient tc =
  1022. AsyncTestClient_initializer;
  1023. MQTTAsync c;
  1024. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  1025. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  1026. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  1027. char* uris[2] = {"rubbish", options.mutual_auth_connection};
  1028. int rc = 0;
  1029. failures = 0;
  1030. MyLog(LOGA_INFO, "Starting test 2e - Mutual SSL authentication with serverURIs");
  1031. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  1032. global_start_time = start_clock();
  1033. rc = MQTTAsync_create(&c, "none", "test2e", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  1034. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1035. if (rc != MQTTASYNC_SUCCESS)
  1036. goto exit;
  1037. tc.client = c;
  1038. sprintf(tc.clientid, "%s", testname);
  1039. sprintf(tc.topic, "C client SSL test2e");
  1040. tc.maxmsgs = MAXMSGS;
  1041. //tc.rcvdmsgs = 0;
  1042. tc.subscribed = 0;
  1043. tc.testFinished = 0;
  1044. opts.keepAliveInterval = 20;
  1045. opts.cleansession = 1;
  1046. opts.username = "testuser";
  1047. opts.password = "testpassword";
  1048. opts.will = &wopts;
  1049. opts.will->message = "will message";
  1050. opts.will->qos = 1;
  1051. opts.will->retained = 0;
  1052. opts.will->topicName = "will topic";
  1053. opts.will = NULL;
  1054. opts.onSuccess = asyncTestOnConnect;
  1055. opts.onFailure = test2eOnConnectFailure;
  1056. opts.context = &tc;
  1057. opts.serverURIs = uris;
  1058. opts.serverURIcount = 2;
  1059. opts.ssl = &sslopts;
  1060. if (options.server_key_file != NULL)
  1061. opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
  1062. opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  1063. if (options.client_key_pass != NULL)
  1064. opts.ssl->privateKeyPassword = options.client_key_pass;
  1065. //opts.ssl->enabledCipherSuites = "DEFAULT";
  1066. //opts.ssl->enabledServerCertAuth = 1;
  1067. opts.ssl->verify = 1;
  1068. MyLog(LOGA_DEBUG, "enableServerCertAuth %d\n", opts.ssl->enableServerCertAuth);
  1069. MyLog(LOGA_DEBUG, "verify %d\n", opts.ssl->verify);
  1070. rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
  1071. asyncTestOnDeliveryComplete);
  1072. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1073. MyLog(LOGA_DEBUG, "Connecting");
  1074. rc = MQTTAsync_connect(c, &opts);
  1075. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1076. if (rc != MQTTASYNC_SUCCESS)
  1077. goto exit;
  1078. while (!tc.subscribed && !tc.testFinished)
  1079. #if defined(_WIN32)
  1080. Sleep(100);
  1081. #else
  1082. usleep(10000L);
  1083. #endif
  1084. if (tc.testFinished)
  1085. goto exit;
  1086. while (!tc.testFinished)
  1087. #if defined(_WIN32)
  1088. Sleep(100);
  1089. #else
  1090. usleep(10000L);
  1091. #endif
  1092. MyLog(LOGA_DEBUG, "Stopping");
  1093. exit: MQTTAsync_destroy(&c);
  1094. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  1095. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  1096. write_test_result();
  1097. return failures;
  1098. }
  1099. /*********************************************************************
  1100. Test3a: Server Authentication - server certificate in client trust store
  1101. *********************************************************************/
  1102. void test3aOnConnectFailure(void* context, MQTTAsync_failureData* response)
  1103. {
  1104. AsyncTestClient* client = (AsyncTestClient*) context;
  1105. MyLog(LOGA_DEBUG, "In test3aOnConnectFailure callback, context %p", context);
  1106. assert("There should be no failures in this test. ", 0, "test3aOnConnectFailure callback was called\n", 0);
  1107. client->testFinished = 1;
  1108. }
  1109. int test3a(struct Options options)
  1110. {
  1111. char* testname = "test3a";
  1112. int subsqos = 2;
  1113. /* TODO - usused - remove ? MQTTAsync_deliveryToken* dt = NULL; */
  1114. AsyncTestClient tc =
  1115. AsyncTestClient_initializer;
  1116. MQTTAsync c;
  1117. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  1118. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  1119. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  1120. int rc = 0;
  1121. int i;
  1122. failures = 0;
  1123. MyLog(LOGA_INFO, "Starting test 3a - Server authentication");
  1124. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  1125. global_start_time = start_clock();
  1126. MQTTAsync_create(&c, options.server_auth_connection, "test3a", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  1127. tc.client = c;
  1128. sprintf(tc.clientid, "%s", testname);
  1129. sprintf(tc.topic, "C client SSL test3a");
  1130. tc.maxmsgs = MAXMSGS;
  1131. //tc.rcvdmsgs = 0;
  1132. tc.subscribed = 0;
  1133. tc.testFinished = 0;
  1134. opts.keepAliveInterval = 20;
  1135. opts.cleansession = 1;
  1136. opts.username = "testuser";
  1137. opts.password = "testpassword";
  1138. opts.will = &wopts;
  1139. opts.will->message = "will message";
  1140. opts.will->qos = 1;
  1141. opts.will->retained = 0;
  1142. opts.will->topicName = "will topic";
  1143. opts.will = NULL;
  1144. opts.onSuccess = asyncTestOnConnect;
  1145. opts.onFailure = test3aOnConnectFailure;
  1146. opts.context = &tc;
  1147. opts.ssl = &sslopts;
  1148. if (options.server_key_file != NULL)
  1149. opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
  1150. //opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  1151. //if (options.client_key_pass != NULL) opts.ssl->privateKeyPassword = options.client_key_pass;
  1152. //opts.ssl->enabledCipherSuites = "DEFAULT";
  1153. //opts.ssl->enabledServerCertAuth = 1;
  1154. rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
  1155. asyncTestOnDeliveryComplete);
  1156. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1157. MyLog(LOGA_DEBUG, "Connecting");
  1158. rc = MQTTAsync_connect(c, &opts);
  1159. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1160. if (rc != MQTTASYNC_SUCCESS)
  1161. goto exit;
  1162. while (!tc.subscribed && !tc.testFinished)
  1163. #if defined(_WIN32)
  1164. Sleep(100);
  1165. #else
  1166. usleep(10000L);
  1167. #endif
  1168. if (tc.testFinished)
  1169. goto exit;
  1170. for (i = 0; i < 3; i++)
  1171. {
  1172. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  1173. pubmsg.payload
  1174. = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  1175. pubmsg.payloadlen = 11;
  1176. pubmsg.qos = i;
  1177. pubmsg.retained = 0;
  1178. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1179. opts.onSuccess = asyncTestOnSend;
  1180. opts.context = &tc;
  1181. rc = MQTTAsync_send(c, tc.topic, pubmsg.payloadlen, pubmsg.payload,
  1182. pubmsg.qos, pubmsg.retained, &opts);
  1183. assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1184. }
  1185. while (!tc.testFinished)
  1186. #if defined(_WIN32)
  1187. Sleep(100);
  1188. #else
  1189. usleep(10000L);
  1190. #endif
  1191. MyLog(LOGA_DEBUG, "Stopping");
  1192. MQTTAsync_destroy(&c);
  1193. exit: MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.", (failures
  1194. == 0) ? "passed" : "failed", testname, tests, failures);
  1195. write_test_result();
  1196. return failures;
  1197. }
  1198. /*********************************************************************
  1199. Test3b: Server Authentication - Client does not have server cert
  1200. *********************************************************************/
  1201. int test3bFinished;
  1202. void test3bOnConnectFailure(void* context, MQTTAsync_failureData* response)
  1203. {
  1204. MyLog(LOGA_DEBUG, "In test3bOnConnectFailure callback, context %p", context);
  1205. assert("This test should call test3bOnConnectFailure. ", 1, "test3bOnConnectFailure callback was called\n", 1);
  1206. test3bFinished = 1;
  1207. }
  1208. void test3bOnConnect(void* context, MQTTAsync_successData* response)
  1209. {
  1210. MyLog(LOGA_DEBUG, "In test3bOnConnectFailure callback, context %p", context);
  1211. assert("This connect should not succeed. ", 0, "test3bOnConnect callback was called\n", 0);
  1212. test3bFinished = 1;
  1213. }
  1214. int test3b(struct Options options)
  1215. {
  1216. char* testname = "test3b";
  1217. int subsqos = 2;
  1218. MQTTAsync c;
  1219. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  1220. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  1221. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  1222. int rc = 0;
  1223. int count = 0;
  1224. test3bFinished = 0;
  1225. failures = 0;
  1226. MyLog(
  1227. LOGA_INFO,
  1228. "Starting test 3b - connection to SSL MQTT server with clientauth=opt but client does not have server cert");
  1229. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  1230. global_start_time = start_clock();
  1231. rc = MQTTAsync_create(&c, options.server_auth_connection, "test3b", MQTTCLIENT_PERSISTENCE_DEFAULT,
  1232. NULL);
  1233. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1234. if (rc != MQTTASYNC_SUCCESS)
  1235. goto exit;
  1236. opts.keepAliveInterval = 20;
  1237. opts.cleansession = 1;
  1238. opts.username = "testuser";
  1239. opts.password = "testpassword";
  1240. opts.will = &wopts;
  1241. opts.will->message = "will message";
  1242. opts.will->qos = 1;
  1243. opts.will->retained = 0;
  1244. opts.will->topicName = "will topic";
  1245. opts.will = NULL;
  1246. opts.onSuccess = test3bOnConnect;
  1247. opts.onFailure = test3bOnConnectFailure;
  1248. opts.context = c;
  1249. opts.ssl = &sslopts;
  1250. //if (options.server_key_file != NULL) opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
  1251. //opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  1252. //if (options.client_key_pass != NULL) opts.ssl->privateKeyPassword = options.client_key_pass;
  1253. //opts.ssl->enabledCipherSuites = "DEFAULT";
  1254. //opts.ssl->enabledServerCertAuth = 0;
  1255. MyLog(LOGA_DEBUG, "Connecting");
  1256. rc = MQTTAsync_connect(c, &opts);
  1257. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1258. if (rc != MQTTASYNC_SUCCESS)
  1259. goto exit;
  1260. while (!test3bFinished && ++count < 10000)
  1261. #if defined(_WIN32)
  1262. Sleep(100);
  1263. #else
  1264. usleep(10000L);
  1265. #endif
  1266. exit: MQTTAsync_destroy(&c);
  1267. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  1268. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  1269. write_test_result();
  1270. return failures;
  1271. }
  1272. /*********************************************************************
  1273. Test4: Accept invalid server certificates
  1274. *********************************************************************/
  1275. void test4OnConnectFailure(void* context, MQTTAsync_failureData* response)
  1276. {
  1277. AsyncTestClient* client = (AsyncTestClient*) context;
  1278. MyLog(LOGA_DEBUG, "In test4OnConnectFailure callback, context %p", context);
  1279. assert("There should be no failures in this test. ", 0, "test4OnConnectFailure callback was called\n", 0);
  1280. client->testFinished = 1;
  1281. }
  1282. void test4OnPublishFailure(void* context, MQTTAsync_failureData* response)
  1283. {
  1284. MyLog(LOGA_DEBUG, "In test4OnPublishFailure callback, context %p", context);
  1285. assert("There should be no failures in this test. ", 0, "test4OnPublishFailure callback was called\n", 0);
  1286. }
  1287. int test4(struct Options options)
  1288. {
  1289. char* testname = "test4";
  1290. int subsqos = 2;
  1291. /* TODO - usused - remove ? MQTTAsync_deliveryToken* dt = NULL; */
  1292. AsyncTestClient tc =
  1293. AsyncTestClient_initializer;
  1294. MQTTAsync c;
  1295. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  1296. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  1297. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  1298. int rc = 0;
  1299. int i;
  1300. failures = 0;
  1301. MyLog(LOGA_INFO, "Starting test 4 - accept invalid server certificates");
  1302. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  1303. global_start_time = start_clock();
  1304. MQTTAsync_create(&c, options.server_auth_connection, "test4", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  1305. tc.client = c;
  1306. sprintf(tc.clientid, "%s", testname);
  1307. sprintf(tc.topic, "C client SSL test4");
  1308. tc.maxmsgs = MAXMSGS;
  1309. //tc.rcvdmsgs = 0;
  1310. tc.subscribed = 0;
  1311. tc.testFinished = 0;
  1312. opts.keepAliveInterval = 20;
  1313. opts.cleansession = 1;
  1314. opts.username = "testuser";
  1315. opts.password = "testpassword";
  1316. opts.will = &wopts;
  1317. opts.will->message = "will message";
  1318. opts.will->qos = 1;
  1319. opts.will->retained = 0;
  1320. opts.will->topicName = "will topic";
  1321. opts.will = NULL;
  1322. opts.onSuccess = asyncTestOnConnect;
  1323. opts.onFailure = test4OnConnectFailure;
  1324. opts.context = &tc;
  1325. opts.ssl = &sslopts;
  1326. //if (options.server_key_file != NULL) opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
  1327. //opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  1328. //if (options.client_key_pass != NULL) opts.ssl->privateKeyPassword = options.client_key_pass;
  1329. //opts.ssl->enabledCipherSuites = "DEFAULT";
  1330. opts.ssl->enableServerCertAuth = 0;
  1331. rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
  1332. asyncTestOnDeliveryComplete);
  1333. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1334. MyLog(LOGA_DEBUG, "Connecting");
  1335. rc = MQTTAsync_connect(c, &opts);
  1336. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1337. if (rc != MQTTASYNC_SUCCESS)
  1338. goto exit;
  1339. while (!tc.subscribed && !tc.testFinished)
  1340. #if defined(_WIN32)
  1341. Sleep(100);
  1342. #else
  1343. usleep(10000L);
  1344. #endif
  1345. if (tc.testFinished)
  1346. goto exit;
  1347. while (!tc.testFinished)
  1348. #if defined(_WIN32)
  1349. Sleep(100);
  1350. #else
  1351. usleep(10000L);
  1352. #endif
  1353. MyLog(LOGA_DEBUG, "Stopping");
  1354. exit: MQTTAsync_destroy(&c);
  1355. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  1356. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  1357. write_test_result();
  1358. return failures;
  1359. }
  1360. /*********************************************************************
  1361. Test5a: Anonymous ciphers - server auth disabled
  1362. *********************************************************************/
  1363. void test5aOnConnectFailure(void* context, MQTTAsync_failureData* response)
  1364. {
  1365. AsyncTestClient* client = (AsyncTestClient*) context;
  1366. MyLog(LOGA_DEBUG, "In test5aOnConnectFailure callback, context %p", context);
  1367. assert("There should be no failures in this test. ", 0, "test5aOnConnectFailure callback was called\n", 0);
  1368. client->testFinished = 1;
  1369. }
  1370. void test5aOnPublishFailure(void* context, MQTTAsync_failureData* response)
  1371. {
  1372. MyLog(LOGA_DEBUG, "In test5aOnPublishFailure callback, context %p", context);
  1373. assert("There should be no failures in this test. ", 0, "test5aOnPublishFailure callback was called\n", 0);
  1374. }
  1375. int test5a(struct Options options)
  1376. {
  1377. char* testname = "test5a";
  1378. AsyncTestClient tc =
  1379. AsyncTestClient_initializer;
  1380. MQTTAsync c;
  1381. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  1382. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  1383. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  1384. int rc = 0;
  1385. int i;
  1386. failures = 0;
  1387. MyLog(LOGA_INFO,
  1388. "Starting SSL test 5a - Anonymous ciphers - server authentication disabled");
  1389. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  1390. global_start_time = start_clock();
  1391. rc = MQTTAsync_create(&c, options.anon_connection, "test5a", MQTTCLIENT_PERSISTENCE_DEFAULT,
  1392. NULL);
  1393. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1394. if (rc != MQTTASYNC_SUCCESS)
  1395. goto exit;
  1396. tc.client = c;
  1397. sprintf(tc.clientid, "%s", testname);
  1398. sprintf(tc.topic, "C client SSL test5a");
  1399. tc.maxmsgs = MAXMSGS;
  1400. //tc.rcvdmsgs = 0;
  1401. tc.subscribed = 0;
  1402. tc.testFinished = 0;
  1403. opts.keepAliveInterval = 20;
  1404. opts.cleansession = 1;
  1405. opts.username = "testuser";
  1406. opts.password = "testpassword";
  1407. opts.will = &wopts;
  1408. opts.will->message = "will message";
  1409. opts.will->qos = 1;
  1410. opts.will->retained = 0;
  1411. opts.will->topicName = "will topic";
  1412. opts.will = NULL;
  1413. opts.onSuccess = asyncTestOnConnect;
  1414. opts.onFailure = test5aOnConnectFailure;
  1415. opts.context = &tc;
  1416. opts.ssl = &sslopts;
  1417. //opts.ssl->trustStore = /*file of certificates trusted by client*/
  1418. //opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  1419. //if (options.client_key_pass != NULL) opts.ssl->privateKeyPassword = options.client_key_pass;
  1420. opts.ssl->enabledCipherSuites = "aNULL";
  1421. opts.ssl->enableServerCertAuth = 0;
  1422. rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
  1423. asyncTestOnDeliveryComplete);
  1424. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1425. MyLog(LOGA_DEBUG, "Connecting");
  1426. rc = MQTTAsync_connect(c, &opts);
  1427. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1428. if (rc != MQTTASYNC_SUCCESS)
  1429. goto exit;
  1430. while (!tc.subscribed && !tc.testFinished)
  1431. #if defined(_WIN32)
  1432. Sleep(100);
  1433. #else
  1434. usleep(10000L);
  1435. #endif
  1436. if (tc.testFinished)
  1437. goto exit;
  1438. for (i = 0; i < 3; i++)
  1439. {
  1440. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  1441. pubmsg.payload
  1442. = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  1443. pubmsg.payloadlen = 11;
  1444. pubmsg.qos = i;
  1445. pubmsg.retained = 0;
  1446. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1447. opts.onSuccess = asyncTestOnSend;
  1448. opts.context = &tc;
  1449. rc = MQTTAsync_send(c, tc.topic, pubmsg.payloadlen, pubmsg.payload,
  1450. pubmsg.qos, pubmsg.retained, &opts);
  1451. assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1452. }
  1453. while (!tc.testFinished)
  1454. #if defined(_WIN32)
  1455. Sleep(100);
  1456. #else
  1457. usleep(10000L);
  1458. #endif
  1459. MyLog(LOGA_DEBUG, "Stopping");
  1460. exit: MQTTAsync_destroy(&c);
  1461. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  1462. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  1463. write_test_result();
  1464. return failures;
  1465. }
  1466. /*********************************************************************
  1467. Test5b: Anonymous ciphers - server auth enabled
  1468. ********************************************************************/
  1469. void test5bOnConnectFailure(void* context, MQTTAsync_failureData* response)
  1470. {
  1471. AsyncTestClient* client = (AsyncTestClient*) context;
  1472. MyLog(LOGA_DEBUG, "In test5bOnConnectFailure callback, context %p", context);
  1473. assert("There should be no failures in this test. ", 0, "test5bOnConnectFailure callback was called\n", 0);
  1474. client->testFinished = 1;
  1475. }
  1476. void test5bOnPublishFailure(void* context, MQTTAsync_failureData* response)
  1477. {
  1478. MyLog(LOGA_DEBUG, "In test5bOnPublishFailure callback, context %p", context);
  1479. assert("There should be no failures in this test. ", 0, "test5bOnPublishFailure callback was called\n", 0);
  1480. }
  1481. int test5b(struct Options options)
  1482. {
  1483. char* testname = "test5b";
  1484. AsyncTestClient tc =
  1485. AsyncTestClient_initializer;
  1486. MQTTAsync c;
  1487. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  1488. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  1489. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  1490. int rc = 0;
  1491. int i;
  1492. failures = 0;
  1493. MyLog(LOGA_INFO,
  1494. "Starting SSL test 5b - Anonymous ciphers - server authentication enabled");
  1495. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  1496. global_start_time = start_clock();
  1497. rc = MQTTAsync_create(&c, options.anon_connection, "test5b", MQTTCLIENT_PERSISTENCE_DEFAULT,
  1498. NULL);
  1499. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1500. if (rc != MQTTASYNC_SUCCESS)
  1501. goto exit;
  1502. tc.client = c;
  1503. sprintf(tc.clientid, "%s", testname);
  1504. sprintf(tc.topic, "C client SSL test5b");
  1505. tc.maxmsgs = MAXMSGS;
  1506. //tc.rcvdmsgs = 0;
  1507. tc.subscribed = 0;
  1508. tc.testFinished = 0;
  1509. opts.keepAliveInterval = 20;
  1510. opts.cleansession = 1;
  1511. opts.username = "testuser";
  1512. opts.password = "testpassword";
  1513. opts.will = &wopts;
  1514. opts.will->message = "will message";
  1515. opts.will->qos = 1;
  1516. opts.will->retained = 0;
  1517. opts.will->topicName = "will topic";
  1518. opts.will = NULL;
  1519. opts.onSuccess = asyncTestOnConnect;
  1520. opts.onFailure = test5bOnConnectFailure;
  1521. opts.context = &tc;
  1522. opts.ssl = &sslopts;
  1523. //opts.ssl->trustStore = /*file of certificates trusted by client*/
  1524. //opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  1525. //if (options.client_key_pass != NULL) opts.ssl->privateKeyPassword = options.client_key_pass;
  1526. opts.ssl->enabledCipherSuites = "aNULL";
  1527. opts.ssl->enableServerCertAuth = 1;
  1528. rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
  1529. asyncTestOnDeliveryComplete);
  1530. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1531. MyLog(LOGA_DEBUG, "Connecting");
  1532. rc = MQTTAsync_connect(c, &opts);
  1533. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1534. if (rc != MQTTASYNC_SUCCESS)
  1535. goto exit;
  1536. while (!tc.subscribed && !tc.testFinished)
  1537. #if defined(_WIN32)
  1538. Sleep(100);
  1539. #else
  1540. usleep(10000L);
  1541. #endif
  1542. if (tc.testFinished)
  1543. goto exit;
  1544. for (i = 0; i < 3; i++)
  1545. {
  1546. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  1547. pubmsg.payload
  1548. = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  1549. pubmsg.payloadlen = 11;
  1550. pubmsg.qos = i;
  1551. pubmsg.retained = 0;
  1552. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1553. opts.onSuccess = asyncTestOnSend;
  1554. opts.context = &tc;
  1555. rc = MQTTAsync_send(c, tc.topic, pubmsg.payloadlen, pubmsg.payload,
  1556. pubmsg.qos, pubmsg.retained, &opts);
  1557. assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1558. }
  1559. while (!tc.testFinished)
  1560. #if defined(_WIN32)
  1561. Sleep(100);
  1562. #else
  1563. usleep(10000L);
  1564. #endif
  1565. MyLog(LOGA_DEBUG, "Stopping");
  1566. exit: MQTTAsync_destroy(&c);
  1567. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  1568. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  1569. write_test_result();
  1570. return failures;
  1571. }
  1572. /*********************************************************************
  1573. Test5c: Anonymous ciphers - client not using anonymous ciphers
  1574. *********************************************************************/
  1575. int test5cFinished;
  1576. void test5cOnConnectFailure(void* context, MQTTAsync_failureData* response)
  1577. {
  1578. MyLog(LOGA_DEBUG, "In test5cOnConnectFailure callback, context %p", context);
  1579. assert("This test should call test5cOnConnectFailure. ", 1, "test5cOnConnectFailure callback was called\n", 1);
  1580. test5cFinished = 1;
  1581. }
  1582. void test5cOnConnect(void* context, MQTTAsync_successData* response)
  1583. {
  1584. MyLog(LOGA_DEBUG, "In test5cOnConnectFailure callback, context %p", context);
  1585. assert("This connect should not succeed. ", 0, "test5cOnConnect callback was called\n", 0);
  1586. test5cFinished = 1;
  1587. }
  1588. int test5c(struct Options options)
  1589. {
  1590. char* testname = "test5c";
  1591. int subsqos = 2;
  1592. MQTTAsync c;
  1593. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  1594. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  1595. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  1596. int rc = 0;
  1597. int count = 0;
  1598. test5cFinished = 0;
  1599. failures = 0;
  1600. MyLog(LOGA_INFO,
  1601. "Starting SSL test 5c - Anonymous ciphers - client not using anonymous cipher");
  1602. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  1603. global_start_time = start_clock();
  1604. rc = MQTTAsync_create(&c, options.anon_connection, "test5c", MQTTCLIENT_PERSISTENCE_DEFAULT,
  1605. NULL);
  1606. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1607. if (rc != MQTTASYNC_SUCCESS)
  1608. goto exit;
  1609. opts.keepAliveInterval = 20;
  1610. opts.cleansession = 1;
  1611. opts.username = "testuser";
  1612. opts.password = "testpassword";
  1613. opts.will = &wopts;
  1614. opts.will->message = "will message";
  1615. opts.will->qos = 1;
  1616. opts.will->retained = 0;
  1617. opts.will->topicName = "will topic";
  1618. opts.will = NULL;
  1619. opts.onSuccess = test5cOnConnect;
  1620. opts.onFailure = test5cOnConnectFailure;
  1621. opts.context = c;
  1622. opts.ssl = &sslopts;
  1623. //opts.ssl->trustStore = /*file of certificates trusted by client*/
  1624. //opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  1625. //if (options.client_key_pass != NULL) opts.ssl->privateKeyPassword = options.client_key_pass;
  1626. //opts.ssl->enabledCipherSuites = "DEFAULT";
  1627. opts.ssl->enableServerCertAuth = 0;
  1628. MyLog(LOGA_DEBUG, "Connecting");
  1629. rc = MQTTAsync_connect(c, &opts);
  1630. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1631. if (rc != MQTTASYNC_SUCCESS)
  1632. goto exit;
  1633. while (!test5cFinished && ++count < 10000)
  1634. #if defined(_WIN32)
  1635. Sleep(100);
  1636. #else
  1637. usleep(10000L);
  1638. #endif
  1639. exit:
  1640. MQTTAsync_destroy(&c);
  1641. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  1642. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  1643. write_test_result();
  1644. return failures;
  1645. }
  1646. /*********************************************************************
  1647. Test6: More than one client object - simultaneous working.
  1648. *********************************************************************/
  1649. void test6OnConnectFailure(void* context, MQTTAsync_failureData* response)
  1650. {
  1651. AsyncTestClient* client = (AsyncTestClient*) context;
  1652. MyLog(LOGA_DEBUG, "In test6OnConnectFailure callback, context %p", context);
  1653. assert("There should be no failures in this test. ", 0, "test6OnConnectFailure callback was called\n", 0);
  1654. client->testFinished = 1;
  1655. }
  1656. void test6OnPublishFailure(void* context, MQTTAsync_failureData* response)
  1657. {
  1658. MyLog(LOGA_DEBUG, "In test6OnPublishFailure callback, context %p", context);
  1659. assert("There should be no failures in this test. ", 0, "test6OnPublishFailure callback was called\n", 0);
  1660. }
  1661. int test6(struct Options options)
  1662. {
  1663. char* testname = "test6";
  1664. #define num_clients 10
  1665. int subsqos = 2;
  1666. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  1667. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  1668. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  1669. int rc = 0;
  1670. int i;
  1671. AsyncTestClient tc[num_clients];
  1672. int test6finished = 0;
  1673. MyLog(LOGA_INFO, "Starting test 6 - multiple connections");
  1674. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  1675. global_start_time = start_clock();
  1676. for (i = 0; i < num_clients; ++i)
  1677. {
  1678. tc[i].maxmsgs = MAXMSGS;
  1679. tc[i].rcvdmsgs[0] = 0;
  1680. tc[i].rcvdmsgs[1] = 0;
  1681. tc[i].rcvdmsgs[2] = 0;
  1682. tc[i].sentmsgs[0] = 0;
  1683. tc[i].sentmsgs[1] = 0;
  1684. tc[i].sentmsgs[2] = 0;
  1685. tc[i].testFinished = 0;
  1686. sprintf(tc[i].clientid, "sslasync_test6_num_%d", i);
  1687. sprintf(tc[i].topic, "sslasync test6 topic num %d", i);
  1688. rc = MQTTAsync_create(&(tc[i].client), options.server_auth_connection, tc[i].clientid,
  1689. MQTTCLIENT_PERSISTENCE_NONE, NULL);
  1690. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1691. rc = MQTTAsync_setCallbacks(tc[i].client, &tc[i], NULL,
  1692. asyncTestMessageArrived, NULL);
  1693. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1694. opts.keepAliveInterval = 20;
  1695. opts.cleansession = 1;
  1696. opts.username = "testuser";
  1697. opts.password = "testpassword";
  1698. opts.will = &wopts;
  1699. opts.will->message = "will message";
  1700. opts.will->qos = 1;
  1701. opts.will->retained = 0;
  1702. opts.will->topicName = "will topic";
  1703. opts.onSuccess = asyncTestOnConnect;
  1704. opts.onFailure = test6OnConnectFailure;
  1705. opts.context = &tc[i];
  1706. opts.ssl = &sslopts;
  1707. if (options.server_key_file != NULL)
  1708. opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
  1709. opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  1710. if (options.client_key_pass != NULL)
  1711. opts.ssl->privateKeyPassword = options.client_key_pass;
  1712. //opts.ssl->enabledCipherSuites = "DEFAULT";
  1713. //opts.ssl->enabledServerCertAuth = 1;
  1714. MyLog(LOGA_DEBUG, "Connecting");
  1715. rc = MQTTAsync_connect(tc[i].client, &opts);
  1716. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1717. }
  1718. while (test6finished < num_clients)
  1719. {
  1720. MyLog(LOGA_DEBUG, "num_clients %d test_finished %d\n", num_clients,
  1721. test6finished);
  1722. #if defined(_WIN32)
  1723. Sleep(100);
  1724. #else
  1725. usleep(10000L);
  1726. #endif
  1727. for (i = 0; i < num_clients; ++i)
  1728. {
  1729. if (tc[i].testFinished)
  1730. {
  1731. test6finished++;
  1732. tc[i].testFinished = 0;
  1733. }
  1734. }
  1735. }
  1736. MyLog(LOGA_DEBUG, "test6: destroying clients");
  1737. for (i = 0; i < num_clients; ++i)
  1738. MQTTAsync_destroy(&tc[i].client);
  1739. //exit:
  1740. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  1741. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  1742. write_test_result();
  1743. return failures;
  1744. }
  1745. /*********************************************************************
  1746. Test7: Send and receive big messages
  1747. *********************************************************************/
  1748. void* test7_payload = NULL;
  1749. int test7_payloadlen = 0;
  1750. void test7OnConnectFailure(void* context, MQTTAsync_failureData* response)
  1751. {
  1752. AsyncTestClient* client = (AsyncTestClient*) context;
  1753. MyLog(LOGA_DEBUG, "In test7OnConnectFailure callback, %s", client->clientid);
  1754. assert("There should be no failures in this test. ", 0, "test7OnConnectFailure callback was called\n", 0);
  1755. client->testFinished = 1;
  1756. }
  1757. int test7OnPublishSuccessCount = 0;
  1758. int test7OnUnsubscribed = 0;
  1759. void test7OnPublishSuccess(void* context, MQTTAsync_successData* response)
  1760. {
  1761. AsyncTestClient* tc = (AsyncTestClient*) context;
  1762. MyLog(LOGA_DEBUG, "In test7OnPublishSuccess callback, %s, qos %d", tc->clientid,
  1763. response->alt.pub.message.qos);
  1764. test7OnPublishSuccessCount++;
  1765. }
  1766. void test7OnPublishFailure(void* context, MQTTAsync_failureData* response)
  1767. {
  1768. AsyncTestClient* client = (AsyncTestClient*) context;
  1769. MyLog(LOGA_DEBUG, "In test7OnPublishFailure callback, %s %d", client->clientid);
  1770. assert("There should be no failures in this test. ", 0, "test7OnPublishFailure callback was called\n", 0);
  1771. client->testFinished = 1;
  1772. }
  1773. void test7OnUnsubscribe(void* context, MQTTAsync_successData* response)
  1774. {
  1775. AsyncTestClient* tc = (AsyncTestClient*) context;
  1776. int rc;
  1777. MyLog(LOGA_DEBUG, "In test7OnUnsubscribe callback, %s %d %d", tc->clientid,
  1778. test7OnUnsubscribed, test7OnPublishSuccessCount);
  1779. test7OnUnsubscribed++;
  1780. }
  1781. int test7MessageArrived(void* context, char* topicName, int topicLen,
  1782. MQTTAsync_message* message)
  1783. {
  1784. AsyncTestClient* tc = (AsyncTestClient*) context;
  1785. static int message_count = 0;
  1786. int rc, i;
  1787. MyLog(LOGA_DEBUG, "In messageArrived callback %p", tc);
  1788. assert("Message size correct", message->payloadlen == test7_payloadlen,
  1789. "message size was %d", message->payloadlen);
  1790. for (i = 0; i < options.size; ++i)
  1791. {
  1792. if (((char*) test7_payload)[i] != ((char*) message->payload)[i])
  1793. {
  1794. assert("Message contents correct", ((char*)test7_payload)[i] != ((char*)message->payload)[i],
  1795. "message content was %c", ((char*)message->payload)[i]);
  1796. break;
  1797. }
  1798. }
  1799. if (++message_count == 1)
  1800. {
  1801. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  1802. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1803. pubmsg.payload = test7_payload;
  1804. pubmsg.payloadlen = test7_payloadlen;
  1805. pubmsg.qos = 1;
  1806. pubmsg.retained = 0;
  1807. opts.onSuccess = test7OnPublishSuccess;
  1808. opts.onFailure = test7OnPublishFailure;
  1809. opts.context = tc;
  1810. rc = MQTTAsync_sendMessage(tc->client, tc->topic, &pubmsg, &opts);
  1811. }
  1812. else if (message_count < options.message_count)
  1813. {
  1814. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  1815. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1816. pubmsg.payload = test7_payload;
  1817. pubmsg.payloadlen = test7_payloadlen;
  1818. pubmsg.qos = 0;
  1819. pubmsg.retained = 0;
  1820. opts.onSuccess = test7OnPublishSuccess;
  1821. opts.onFailure = test7OnPublishFailure;
  1822. opts.context = tc;
  1823. rc = MQTTAsync_sendMessage(tc->client, tc->topic, &pubmsg, &opts);
  1824. }
  1825. else
  1826. {
  1827. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1828. opts.onSuccess = test7OnUnsubscribe;
  1829. opts.context = tc;
  1830. rc = MQTTAsync_unsubscribe(tc->client, tc->topic, &opts);
  1831. assert("Unsubscribe successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1832. }
  1833. MQTTAsync_freeMessage(&message);
  1834. MQTTAsync_free(topicName);
  1835. return 1;
  1836. }
  1837. void test7OnSubscribe(void* context, MQTTAsync_successData* response)
  1838. {
  1839. AsyncTestClient* tc = (AsyncTestClient*) context;
  1840. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  1841. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1842. int rc, i;
  1843. MyLog(LOGA_DEBUG, "In subscribe onSuccess callback %p", tc);
  1844. pubmsg.payload = test7_payload = malloc(options.size);
  1845. pubmsg.payloadlen = test7_payloadlen = options.size;
  1846. srand(33);
  1847. for (i = 0; i < options.size; ++i)
  1848. ((char*) pubmsg.payload)[i] = rand() % 256;
  1849. pubmsg.qos = 2;
  1850. pubmsg.retained = 0;
  1851. pubmsg.payload = test7_payload;
  1852. pubmsg.payloadlen = test7_payloadlen;
  1853. opts.onSuccess = test7OnPublishSuccess;
  1854. opts.onFailure = test7OnPublishFailure;
  1855. opts.context = tc;
  1856. rc = MQTTAsync_send(tc->client, tc->topic, pubmsg.payloadlen, pubmsg.payload,
  1857. pubmsg.qos, pubmsg.retained, &opts);
  1858. }
  1859. void test7OnConnect(void* context, MQTTAsync_successData* response)
  1860. {
  1861. AsyncTestClient* tc = (AsyncTestClient*) context;
  1862. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  1863. int rc;
  1864. MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
  1865. opts.onSuccess = test7OnSubscribe;
  1866. opts.context = tc;
  1867. rc = MQTTAsync_subscribe(tc->client, tc->topic, 2, &opts);
  1868. assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1869. if (rc != MQTTASYNC_SUCCESS)
  1870. tc->testFinished = 1;
  1871. }
  1872. int test7(struct Options options)
  1873. {
  1874. char* testname = "test7";
  1875. int subsqos = 2;
  1876. AsyncTestClient tc = AsyncTestClient_initializer;
  1877. MQTTAsync c;
  1878. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  1879. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  1880. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  1881. MQTTAsync_disconnectOptions dopts = MQTTAsync_disconnectOptions_initializer;
  1882. int rc = 0;
  1883. char* test_topic = "C client test7";
  1884. int test_finished;
  1885. test_finished = failures = 0;
  1886. MyLog(LOGA_INFO, "Starting test 7 - big messages");
  1887. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  1888. global_start_time = start_clock();
  1889. rc = MQTTAsync_create(&c, options.server_auth_connection, "async_test_7", MQTTCLIENT_PERSISTENCE_NONE,
  1890. NULL);
  1891. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1892. if (rc != MQTTASYNC_SUCCESS)
  1893. {
  1894. MQTTAsync_destroy(&c);
  1895. goto exit;
  1896. }
  1897. rc = MQTTAsync_setCallbacks(c, &tc, NULL, test7MessageArrived, NULL);
  1898. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1899. tc.client = c;
  1900. sprintf(tc.clientid, "%s", testname);
  1901. sprintf(tc.topic, "C client SSL test7");
  1902. tc.maxmsgs = MAXMSGS;
  1903. //tc.rcvdmsgs = 0;
  1904. tc.subscribed = 0;
  1905. tc.testFinished = 0;
  1906. opts.keepAliveInterval = 20;
  1907. opts.cleansession = 1;
  1908. //opts.username = "testuser";
  1909. //opts.password = "testpassword";
  1910. opts.will = &wopts;
  1911. opts.will->message = "will message";
  1912. opts.will->qos = 1;
  1913. opts.will->retained = 0;
  1914. opts.will->topicName = "will topic";
  1915. opts.will = NULL;
  1916. opts.onSuccess = test7OnConnect;
  1917. opts.onFailure = test7OnConnectFailure;
  1918. opts.context = &tc;
  1919. opts.ssl = &sslopts;
  1920. if (options.server_key_file != NULL)
  1921. opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
  1922. if (options.client_key_file != NULL)
  1923. opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  1924. if (options.client_key_pass != NULL)
  1925. opts.ssl->privateKeyPassword = options.client_key_pass;
  1926. //opts.ssl->enabledCipherSuites = "DEFAULT";
  1927. //opts.ssl->enabledServerCertAuth = 1;
  1928. MyLog(LOGA_DEBUG, "Connecting");
  1929. rc = MQTTAsync_connect(c, &opts);
  1930. rc = 0;
  1931. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1932. if (rc != MQTTASYNC_SUCCESS)
  1933. goto exit;
  1934. while (test7OnUnsubscribed == 0 && test7OnPublishSuccessCount < options.message_count)
  1935. #if defined(_WIN32)
  1936. Sleep(100);
  1937. #else
  1938. usleep(1000L);
  1939. #endif
  1940. dopts.onSuccess = asyncTestOnDisconnect;
  1941. dopts.context = &tc;
  1942. rc = MQTTAsync_disconnect(c, &dopts);
  1943. while (!tc.testFinished)
  1944. #if defined(_WIN32)
  1945. Sleep(100);
  1946. #else
  1947. usleep(1000L);
  1948. #endif
  1949. MQTTAsync_destroy(&c);
  1950. if (test7_payload)
  1951. {
  1952. free(test7_payload);
  1953. test7_payload = NULL;
  1954. }
  1955. exit: MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  1956. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  1957. write_test_result();
  1958. return failures;
  1959. }
  1960. /*********************************************************************
  1961. Test8: TLS-PSK - client and server has a common pre-shared key
  1962. *********************************************************************/
  1963. static unsigned int onPSKAuth(const char* hint,
  1964. char* identity,
  1965. unsigned int max_identity_len,
  1966. unsigned char* psk,
  1967. unsigned int max_psk_len,
  1968. void* context)
  1969. {
  1970. unsigned char test_psk[] = {0x50, 0x53, 0x4B, 0x00}; /* {'P', 'S', 'K', '\0' } */
  1971. MyLog(LOGA_DEBUG, "PSK auth callback");
  1972. assert("Good application context in onPSKAuth", context == (void *) 42, "context was %d\n", context);
  1973. strncpy(identity, "id", max_identity_len);
  1974. memcpy(psk, test_psk, sizeof(test_psk));
  1975. return sizeof(test_psk);
  1976. }
  1977. int test8(struct Options options)
  1978. {
  1979. char* testname = "test8";
  1980. AsyncTestClient tc =
  1981. AsyncTestClient_initializer;
  1982. MQTTAsync c;
  1983. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  1984. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  1985. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  1986. int rc = 0;
  1987. failures = 0;
  1988. MyLog(LOGA_INFO, "Starting test 8 - TLS-PSK - client and server has a common pre-shared key");
  1989. fprintf(xml, "<testcase classname=\"test8\" name=\"%s\"", testname);
  1990. global_start_time = start_clock();
  1991. MQTTAsync_create(&c, options.psk_connection, "test8", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  1992. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1993. if (rc != MQTTASYNC_SUCCESS)
  1994. goto exit;
  1995. tc.client = c;
  1996. sprintf(tc.clientid, "%s", testname);
  1997. sprintf(tc.topic, "C client SSL test8");
  1998. tc.maxmsgs = MAXMSGS;
  1999. tc.subscribed = 0;
  2000. tc.testFinished = 0;
  2001. opts.keepAliveInterval = 20;
  2002. opts.cleansession = 1;
  2003. opts.username = "testuser";
  2004. opts.password = "testpassword";
  2005. opts.onSuccess = asyncTestOnConnect;
  2006. opts.onFailure = asyncTestOnSubscribeFailure;
  2007. opts.context = &tc;
  2008. opts.ssl = &sslopts;
  2009. opts.ssl->ssl_psk_cb = onPSKAuth;
  2010. opts.ssl->ssl_psk_context = (void *) 42;
  2011. opts.ssl->enabledCipherSuites = "PSK-AES128-CBC-SHA";
  2012. rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
  2013. asyncTestOnDeliveryComplete);
  2014. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  2015. MyLog(LOGA_DEBUG, "Connecting");
  2016. rc = MQTTAsync_connect(c, &opts);
  2017. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  2018. if (rc != MQTTASYNC_SUCCESS)
  2019. goto exit;
  2020. while (!tc.subscribed && !tc.testFinished)
  2021. #if defined(_WIN32)
  2022. Sleep(100);
  2023. #else
  2024. usleep(10000L);
  2025. #endif
  2026. if (tc.testFinished)
  2027. goto exit;
  2028. while (!tc.testFinished)
  2029. #if defined(_WIN32)
  2030. Sleep(100);
  2031. #else
  2032. usleep(10000L);
  2033. #endif
  2034. MyLog(LOGA_DEBUG, "Stopping");
  2035. exit:
  2036. MQTTAsync_destroy(&c);
  2037. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  2038. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  2039. write_test_result();
  2040. return failures;
  2041. }
  2042. /*********************************************************************
  2043. Test9: Mutual SSL Authentication - Testing CApath
  2044. *********************************************************************/
  2045. void test9OnConnectFailure(void* context, MQTTAsync_failureData* response)
  2046. {
  2047. AsyncTestClient* client = (AsyncTestClient*) context;
  2048. MyLog(LOGA_DEBUG, "In test9OnConnectFailure callback, %s",
  2049. client->clientid);
  2050. assert("There should be no failures in this test. ", 0, "test9OnConnectFailure callback was called\n", 0);
  2051. client->testFinished = 1;
  2052. }
  2053. int test9(struct Options options)
  2054. {
  2055. char* testname = "test9";
  2056. AsyncTestClient tc =
  2057. AsyncTestClient_initializer;
  2058. MQTTAsync c;
  2059. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  2060. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  2061. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  2062. int rc = 0;
  2063. failures = 0;
  2064. MyLog(LOGA_INFO, "Starting test 9 - Mutual SSL authentication with CApath");
  2065. fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
  2066. global_start_time = start_clock();
  2067. MQTTAsync_create(&c, options.mutual_auth_connection, "test9", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  2068. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  2069. if (rc != MQTTASYNC_SUCCESS)
  2070. goto exit;
  2071. tc.client = c;
  2072. sprintf(tc.clientid, "%s", testname);
  2073. sprintf(tc.topic, "C client SSL test9");
  2074. tc.maxmsgs = MAXMSGS;
  2075. //tc.rcvdmsgs = 0;
  2076. tc.subscribed = 0;
  2077. tc.testFinished = 0;
  2078. opts.keepAliveInterval = 20;
  2079. opts.cleansession = 1;
  2080. opts.username = "testuser";
  2081. opts.password = "testpassword";
  2082. opts.will = &wopts;
  2083. opts.will->message = "will message";
  2084. opts.will->qos = 1;
  2085. opts.will->retained = 0;
  2086. opts.will->topicName = "will topic";
  2087. opts.will = NULL;
  2088. opts.onSuccess = asyncTestOnConnect;
  2089. opts.onFailure = test9OnConnectFailure;
  2090. opts.context = &tc;
  2091. opts.ssl = &sslopts;
  2092. if (options.server_key_file != NULL)
  2093. opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
  2094. opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  2095. if (options.client_key_pass != NULL)
  2096. opts.ssl->privateKeyPassword = options.client_key_pass;
  2097. opts.ssl->CApath = options.capath;
  2098. opts.ssl->enableServerCertAuth = 1;
  2099. opts.ssl->verify = 1;
  2100. MyLog(LOGA_DEBUG, "enableServerCertAuth %d\n", opts.ssl->enableServerCertAuth);
  2101. MyLog(LOGA_DEBUG, "verify %d\n", opts.ssl->verify);
  2102. rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
  2103. asyncTestOnDeliveryComplete);
  2104. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  2105. MyLog(LOGA_DEBUG, "Connecting");
  2106. rc = MQTTAsync_connect(c, &opts);
  2107. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  2108. if (rc != MQTTASYNC_SUCCESS)
  2109. goto exit;
  2110. while (!tc.subscribed && !tc.testFinished)
  2111. #if defined(_WIN32)
  2112. Sleep(100);
  2113. #else
  2114. usleep(10000L);
  2115. #endif
  2116. if (tc.testFinished)
  2117. goto exit;
  2118. while (!tc.testFinished)
  2119. #if defined(_WIN32)
  2120. Sleep(100);
  2121. #else
  2122. usleep(10000L);
  2123. #endif
  2124. MyLog(LOGA_DEBUG, "Stopping");
  2125. exit: MQTTAsync_destroy(&c);
  2126. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  2127. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  2128. write_test_result();
  2129. return failures;
  2130. }
  2131. /*********************************************************************
  2132. Test10: Mutual SSL Authentication - Testing CApath
  2133. *********************************************************************/
  2134. int test10Finished;
  2135. void test10OnConnectFailure(void* context, MQTTAsync_failureData* response)
  2136. {
  2137. AsyncTestClient* client = (AsyncTestClient*) context;
  2138. MyLog(LOGA_DEBUG, "In test10OnConnectFailure callback, %s",
  2139. client->clientid);
  2140. assert("This test should call test10OnConnectFailure. ", 1, "test10OnConnectFailure callback was called\n", 1);
  2141. test10Finished = 1;
  2142. }
  2143. void test10OnConnect(void* context, MQTTAsync_successData* response)
  2144. {
  2145. MyLog(LOGA_DEBUG, "In test10OnConnect callback, context %p", context);
  2146. assert("This connect should not succeed. ", 0, "test10OnConnect callback was called\n", 0);
  2147. test10Finished = 1;
  2148. }
  2149. int test10(struct Options options)
  2150. {
  2151. char* testname = "test10";
  2152. AsyncTestClient tc =
  2153. AsyncTestClient_initializer;
  2154. MQTTAsync c;
  2155. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  2156. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  2157. MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
  2158. int rc = 0;
  2159. failures = 0;
  2160. test10Finished = 0;
  2161. MyLog(LOGA_INFO, "Starting test 10 - dummy CApath");
  2162. fprintf(xml, "<testcase classname=\"test10\" name=\"%s\"", testname);
  2163. global_start_time = start_clock();
  2164. MQTTAsync_create(&c, options.mutual_auth_connection, "test10", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  2165. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  2166. if (rc != MQTTASYNC_SUCCESS)
  2167. goto exit;
  2168. tc.client = c;
  2169. sprintf(tc.clientid, "%s", testname);
  2170. sprintf(tc.topic, "C client SSL test10");
  2171. tc.maxmsgs = MAXMSGS;
  2172. //tc.rcvdmsgs = 0;
  2173. tc.subscribed = 0;
  2174. tc.testFinished = 0;
  2175. opts.keepAliveInterval = 20;
  2176. opts.cleansession = 1;
  2177. opts.username = "testuser";
  2178. opts.password = "testpassword";
  2179. opts.will = &wopts;
  2180. opts.will->message = "will message";
  2181. opts.will->qos = 1;
  2182. opts.will->retained = 0;
  2183. opts.will->topicName = "will topic";
  2184. opts.will = NULL;
  2185. opts.onSuccess = test10OnConnect;
  2186. opts.onFailure = test10OnConnectFailure;
  2187. opts.context = &tc;
  2188. opts.ssl = &sslopts;
  2189. if (options.server_key_file != NULL)
  2190. opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
  2191. opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
  2192. if (options.client_key_pass != NULL)
  2193. opts.ssl->privateKeyPassword = options.client_key_pass;
  2194. opts.ssl->CApath = "DUMMY";
  2195. opts.ssl->enableServerCertAuth = 1;
  2196. opts.ssl->verify = 1;
  2197. MyLog(LOGA_DEBUG, "enableServerCertAuth %d\n", opts.ssl->enableServerCertAuth);
  2198. MyLog(LOGA_DEBUG, "verify %d\n", opts.ssl->verify);
  2199. rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
  2200. asyncTestOnDeliveryComplete);
  2201. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  2202. MyLog(LOGA_DEBUG, "Connecting");
  2203. rc = MQTTAsync_connect(c, &opts);
  2204. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  2205. if (rc != MQTTASYNC_SUCCESS)
  2206. goto exit;
  2207. while (!test10Finished)
  2208. #if defined(_WIN32)
  2209. Sleep(100);
  2210. #else
  2211. usleep(10000L);
  2212. #endif
  2213. MyLog(LOGA_DEBUG, "Stopping");
  2214. exit: MQTTAsync_destroy(&c);
  2215. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  2216. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  2217. write_test_result();
  2218. return failures;
  2219. }
  2220. void handleTrace(enum MQTTASYNC_TRACE_LEVELS level, char* message)
  2221. {
  2222. printf("%s\n", message);
  2223. }
  2224. int main(int argc, char** argv)
  2225. {
  2226. int* numtests = &tests;
  2227. int rc = 0;
  2228. int (*tests[])() =
  2229. { NULL, test1, test2a, test2b, test2c, test2d, test3a, test3b, test4, /* test5a,
  2230. test5b, test5c, */ test6, test7, test8, test9, test10, test2e };
  2231. xml = fopen("TEST-test5.xml", "w");
  2232. fprintf(xml, "<testsuite name=\"test5\" tests=\"%d\">\n", (int)ARRAY_SIZE(tests) - 1);
  2233. MQTTAsync_setTraceCallback(handleTrace);
  2234. getopts(argc, argv);
  2235. if (options.test_no == 0)
  2236. { /* run all the tests */
  2237. for (options.test_no = 1; options.test_no < ARRAY_SIZE(tests); ++options.test_no)
  2238. {
  2239. failures = 0;
  2240. MQTTAsync_setTraceLevel(MQTTASYNC_TRACE_ERROR);
  2241. rc += tests[options.test_no](options); /* return number of failures. 0 = test succeeded */
  2242. }
  2243. }
  2244. else
  2245. {
  2246. MQTTAsync_setTraceLevel(MQTTASYNC_TRACE_ERROR);
  2247. rc = tests[options.test_no](options); /* run just the selected test */
  2248. }
  2249. MyLog(LOGA_INFO, "Total tests run: %d", *numtests);
  2250. if (rc == 0)
  2251. MyLog(LOGA_INFO, "verdict pass");
  2252. else
  2253. MyLog(LOGA_INFO, "verdict fail");
  2254. fprintf(xml, "</testsuite>\n");
  2255. fclose(xml);
  2256. return rc;
  2257. }
  2258. /* Local Variables: */
  2259. /* indent-tabs-mode: t */
  2260. /* c-basic-offset: 8 */
  2261. /* End: */