1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740 |
- /*******************************************************************************
- * Copyright (c) 2012, 2022 IBM Corp., Ian Craggs
- *
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v2.0
- * and Eclipse Distribution License v1.0 which accompany this distribution.
- *
- * The Eclipse Public License is available at
- * https://www.eclipse.org/legal/epl-2.0/
- * and the Eclipse Distribution License is available at
- * http://www.eclipse.org/org/documents/edl-v10.php.
- *
- * Contributors:
- * Allan Stockdill-Mander - initial API and implementation and/or initial documentation
- * Ian Craggs - fix Windows includes
- *******************************************************************************/
- /**
- * @file
- * SSL tests for the Eclipse Paho Asynchronous MQTT C client
- */
- #include "MQTTAsync.h"
- #include <string.h>
- #include <stdlib.h>
- #include "Thread.h"
- #if defined(_WINDOWS)
- #include <windows.h>
- #include <openssl/applink.c>
- #define MAXHOSTNAMELEN 256
- #define snprintf _snprintf
- #else
- #include <sys/time.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <errno.h>
- #endif
- #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
- void usage(void)
- {
- printf("Options:\n");
- printf("\t--test_no <test_no> - Run test number <test_no>\n");
- printf("\t--hostname <hostname> - Connect to <hostname> for tests\n");
- printf("\t--client_key <key_file> - Use <key_file> as the client certificate for SSL authentication\n");
- printf("\t--client_key_pass <password> - Use <password> to access the private key in the client certificate\n");
- printf("\t--server_key <key_file> - Use <key_file> as the trusted certificate for server\n");
- printf("\t--verbose - Enable verbose output \n");
- printf("\t--help - This help output\n");
- exit(EXIT_FAILURE);
- }
- struct Options
- {
- char connection[100]; /**< connection to system under test. */
- char mutual_auth_connection[100]; /**< connection to system under test. */
- char nocert_mutual_auth_connection[100];
- char server_auth_connection[100];
- char anon_connection[100];
- char psk_connection[100];
- char* client_key_file;
- char* client_key_pass;
- char* server_key_file;
- char* client_private_key_file;
- char* capath;
- int verbose;
- int test_no;
- int size;
- int websockets;
- int message_count;
- int start_port;
- } options =
- {
- "ssl://localhost:18883",
- "mqtts://localhost:18884",
- "ssl://localhost:18887",
- "mqtts://localhost:18885",
- "ssl://localhost:18886",
- "mqtts://localhost:18888",
- NULL, // "../../../test/ssl/client.pem",
- NULL,
- NULL, // "../../../test/ssl/test-root-ca.crt",
- NULL, // "../../../test/ssl/capath",
- NULL,
- 0,
- 0,
- 5000000,
- 0,
- 3,
- 18883,
- };
- typedef struct
- {
- MQTTAsync client;
- char clientid[24];
- char topic[100];
- int maxmsgs;
- int rcvdmsgs[3];
- int sentmsgs[3];
- int testFinished;
- int subscribed;
- } AsyncTestClient;
- #define AsyncTestClient_initializer {NULL, "\0", "\0", 0, {0, 0, 0}, {0, 0, 0}, 0, 0}
- void getopts(int argc, char** argv)
- {
- int count = 1;
- while (count < argc)
- {
- if (strcmp(argv[count], "--help") == 0)
- {
- usage();
- }
- else if (strcmp(argv[count], "--test_no") == 0)
- {
- if (++count < argc)
- options.test_no = atoi(argv[count]);
- else
- usage();
- }
- else if (strcmp(argv[count], "--client_key") == 0)
- {
- if (++count < argc)
- options.client_key_file = argv[count];
- else
- usage();
- }
- else if (strcmp(argv[count], "--client_key_pass") == 0)
- {
- if (++count < argc)
- options.client_key_pass = argv[count];
- else
- usage();
- }
- else if (strcmp(argv[count], "--server_key") == 0)
- {
- if (++count < argc)
- options.server_key_file = argv[count];
- else
- usage();
- }
- else if (strcmp(argv[count], "--capath") == 0)
- {
- if (++count < argc)
- options.capath = argv[count];
- else
- usage();
- }
- else if (strcmp(argv[count], "--verbose") == 0)
- {
- options.verbose = 1;
- printf("\nSetting verbose on\n");
- }
- else if (strcmp(argv[count], "--hostname") == 0)
- {
- if (++count < argc)
- {
- char* prefix = (options.websockets) ? "wss" : "ssl";
- sprintf(options.connection, "%s://%s:%d", prefix, argv[count],
- options.start_port);
- printf("Setting connection to %s\n", options.connection);
- sprintf(options.mutual_auth_connection, "%s://%s:%d", prefix, argv[count],
- options.start_port+1);
- printf("Setting mutual_auth_connection to %s\n", options.mutual_auth_connection);
- sprintf(options.nocert_mutual_auth_connection, "%s://%s:%d", prefix,
- argv[count], options.start_port+4);
- printf("Setting nocert_mutual_auth_connection to %s\n",
- options.nocert_mutual_auth_connection);
- sprintf(options.server_auth_connection, "%s://%s:%d", prefix, argv[count],
- options.start_port+2);
- printf("Setting server_auth_connection to %s\n", options.server_auth_connection);
- sprintf(options.anon_connection, "%s://%s:%d", prefix, argv[count],
- options.start_port+3);
- printf("Setting anon_connection to %s\n", options.anon_connection);
- sprintf(options.psk_connection, "%s://%s:%d", prefix, argv[count],
- options.start_port+5);
- printf("Setting psk_connection to %s\n", options.psk_connection);
- }
- else
- usage();
- }
- else if (strcmp(argv[count], "--ws") == 0)
- {
- options.websockets = 1;
- printf("\nSetting websockets on\n");
- }
- else if (strcmp(argv[count], "--size") == 0)
- {
- if (++count < argc)
- {
- options.size = atoi(argv[count]);
- printf("\nSetting size to %d\n", options.size);
- }else
- usage();
- }
- else if (strcmp(argv[count], "--port") == 0)
- {
- if (++count < argc)
- {
- options.start_port = atoi(argv[count]);
- printf("\nSetting start_port to %d\n", options.start_port);
- }else
- usage();
- }
- else if (strcmp(argv[count], "--count") == 0)
- {
- if (++count < argc)
- {
- options.message_count = atoi(argv[count]);
- printf("\nSetting message count to %d\n", options.message_count);
- }else
- usage();
- }
- else
- printf("Unrecognized option %s\n", argv[count]);
- count++;
- }
- }
- #define LOGA_DEBUG 0
- #define LOGA_INFO 1
- #include <stdarg.h>
- #include <time.h>
- #include <sys/timeb.h>
- void MyLog(int LOGA_level, char* format, ...)
- {
- static char msg_buf[256];
- va_list args;
- #if defined(_WIN32) || defined(_WINDOWS)
- struct timeb ts;
- #else
- struct timeval ts;
- #endif
- struct tm timeinfo;
- if (LOGA_level == LOGA_DEBUG && options.verbose == 0)
- return;
- #if defined(_WIN32) || defined(_WINDOWS)
- ftime(&ts);
- localtime_s(&timeinfo, &ts.time);
- #else
- gettimeofday(&ts, NULL);
- localtime_r(&ts.tv_sec, &timeinfo);
- #endif
- strftime(msg_buf, 80, "%Y%m%d %H%M%S", &timeinfo);
- #if defined(_WIN32) || defined(_WINDOWS)
- sprintf(&msg_buf[strlen(msg_buf)], ".%.3hu ", ts.millitm);
- #else
- sprintf(&msg_buf[strlen(msg_buf)], ".%.3lu ", ts.tv_usec / 1000L);
- #endif
- va_start(args, format);
- vsnprintf(&msg_buf[strlen(msg_buf)], sizeof(msg_buf) - strlen(msg_buf), format, args);
- va_end(args);
- printf("%s\n", msg_buf);
- fflush(stdout);
- }
- #if defined(_WIN32) || defined(_WINDOWS)
- #define mqsleep(A) Sleep(1000*A)
- #define START_TIME_TYPE DWORD
- static DWORD start_time = 0;
- START_TIME_TYPE start_clock(void)
- {
- return GetTickCount();
- }
- #elif defined(AIX)
- #define mqsleep sleep
- #define START_TIME_TYPE struct timespec
- START_TIME_TYPE start_clock(void)
- {
- static struct timespec start;
- clock_gettime(CLOCK_REALTIME, &start);
- return start;
- }
- #else
- #define mqsleep sleep
- #define START_TIME_TYPE struct timeval
- /* TODO - unused - remove? static struct timeval start_time; */
- START_TIME_TYPE start_clock(void)
- {
- struct timeval start_time;
- gettimeofday(&start_time, NULL);
- return start_time;
- }
- #endif
- #if defined(_WIN32)
- long elapsed(START_TIME_TYPE start_time)
- {
- return GetTickCount() - start_time;
- }
- #elif defined(AIX)
- #define assert(a)
- long elapsed(struct timespec start)
- {
- struct timespec now, res;
- clock_gettime(CLOCK_REALTIME, &now);
- ntimersub(now, start, res);
- return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L;
- }
- #else
- long elapsed(START_TIME_TYPE start_time)
- {
- struct timeval now, res;
- gettimeofday(&now, NULL);
- timersub(&now, &start_time, &res);
- return (res.tv_sec) * 1000 + (res.tv_usec) / 1000;
- }
- #endif
- #define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d)
- #define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e)
- #define MAXMSGS 30;
- int tests = 0;
- int failures = 0;
- FILE* xml;
- START_TIME_TYPE global_start_time;
- char output[3000];
- char* cur_output = output;
- void write_test_result(void)
- {
- long duration = elapsed(global_start_time);
- fprintf(xml, " time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000);
- if (cur_output != output)
- {
- fprintf(xml, "%s", output);
- cur_output = output;
- }
- fprintf(xml, "</testcase>\n");
- }
- void myassert(char* filename, int lineno, char* description, int value,
- char* format, ...)
- {
- ++tests;
- if (!value)
- {
- va_list args;
- ++failures;
- printf("Assertion failed, file %s, line %d, description: %s", filename,
- lineno, description);
- va_start(args, format);
- vprintf(format, args);
- va_end(args);
- cur_output += sprintf(cur_output, "<failure type=\"%s\">file %s, line %d </failure>\n",
- description, filename, lineno);
- }
- else
- MyLog(LOGA_DEBUG,
- "Assertion succeeded, file %s, line %d, description: %s",
- filename, lineno, description);
- }
- /*********************************************************************
- Test: multi-threaded client using callbacks
- *********************************************************************/
- volatile int multiThread_arrivedcount = 0;
- int multiThread_deliveryCompleted = 0;
- MQTTAsync_message multiThread_pubmsg = MQTTAsync_message_initializer;
- void multiThread_deliveryComplete(void* context, MQTTAsync_token dt)
- {
- ++multiThread_deliveryCompleted;
- }
- int multiThread_messageArrived(void* context, char* topicName, int topicLen,
- MQTTAsync_message* m)
- {
- ++multiThread_arrivedcount;
- MyLog(LOGA_DEBUG, "Callback: %d message received on topic %s is %.*s.",
- multiThread_arrivedcount, topicName, m->payloadlen,
- (char*) (m->payload));
- if (multiThread_pubmsg.payloadlen != m->payloadlen || memcmp(m->payload,
- multiThread_pubmsg.payload, m->payloadlen) != 0)
- {
- failures++;
- MyLog(LOGA_INFO, "Error: wrong data received lengths %d %d\n",
- multiThread_pubmsg.payloadlen, m->payloadlen);
- }
- MQTTAsync_free(topicName);
- MQTTAsync_freeMessage(&m);
- return 1;
- }
- void sendAndReceive(MQTTAsync* c, int qos, char* test_topic)
- {
- MQTTAsync_responseOptions ropts;
- int i = 0;
- int iterations = 50;
- int rc = 0;
- int wait_seconds = 0;
- multiThread_deliveryCompleted = 0;
- multiThread_arrivedcount = 0;
- MyLog(LOGA_DEBUG, "%d messages at QoS %d", iterations, qos);
- multiThread_pubmsg.payload
- = "a much longer message that we can shorten to the extent that we need to";
- multiThread_pubmsg.payloadlen = 27;
- multiThread_pubmsg.qos = qos;
- multiThread_pubmsg.retained = 0;
- for (i = 1; i <= iterations; ++i)
- {
- if (i % 10 == 0)
- rc = MQTTAsync_send(c, test_topic, multiThread_pubmsg.payloadlen,
- multiThread_pubmsg.payload, multiThread_pubmsg.qos,
- multiThread_pubmsg.retained, NULL);
- else
- rc = MQTTAsync_sendMessage(c, test_topic, &multiThread_pubmsg,
- &ropts);
- assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(100000L);
- #endif
- wait_seconds = 10;
- while ((multiThread_arrivedcount < i) && (wait_seconds-- > 0))
- {
- MyLog(LOGA_DEBUG, "Arrived %d count %d", multiThread_arrivedcount,
- i);
- #if defined(_WIN32)
- Sleep(1000);
- #else
- usleep(1000000L);
- #endif
- }
- assert("Message Arrived", wait_seconds > 0,
- "Time out waiting for message %d\n", i );
- }
- if (qos > 0)
- {
- /* MQ Telemetry can send a message to a subscriber before the server has
- completed the QoS 2 handshake with the publisher. For QoS 1 and 2,
- allow time for the final delivery complete callback before checking
- that all expected callbacks have been made */
- wait_seconds = 10;
- while ((multiThread_deliveryCompleted < iterations) && (wait_seconds--
- > 0))
- {
- MyLog(LOGA_DEBUG, "Delivery Completed %d count %d",
- multiThread_deliveryCompleted, i);
- #if defined(_WIN32)
- Sleep(1000);
- #else
- usleep(1000000L);
- #endif
- }
- assert("All Deliveries Complete", wait_seconds > 0,
- "Number of deliveryCompleted callbacks was %d\n",
- multiThread_deliveryCompleted);
- }
- }
- /*********************************************************************
- Async Callbacks - generic callbacks for send/receive tests
- *********************************************************************/
- /*static mutex_type client_mutex = NULL;
- static pthread_mutex_t client_mutex_store = PTHREAD_MUTEX_INITIALIZER;
- static mutex_type client_mutex = &client_mutex_store;*/
- void asyncTestOnDisconnect(void* context, MQTTAsync_successData* response)
- {
- //int rc;
- AsyncTestClient* tc = (AsyncTestClient*) context;
- MyLog(LOGA_DEBUG, "In asyncTestOnDisconnect callback, %s", tc->clientid);
- //rc = Thread_lock_mutex(client_mutex);
- tc->testFinished = 1;
- //rc = Thread_unlock_mutex(client_mutex);
- }
- void asyncTestOnSend(void* context, MQTTAsync_successData* response)
- {
- AsyncTestClient* tc = (AsyncTestClient*) context;
- //int rc;
- int qos = response->alt.pub.message.qos;
- MyLog(LOGA_DEBUG, "In asyncTestOnSend callback, %s", tc->clientid);
- //rc = Thread_lock_mutex(client_mutex);
- tc->sentmsgs[qos]++;
- //rc = Thread_unlock_mutex(client_mutex);
- }
- void asyncTestOnSubscribeFailure(void* context, MQTTAsync_failureData* response)
- {
- AsyncTestClient* tc = (AsyncTestClient*) context;
- MyLog(LOGA_DEBUG, "In asyncTestOnSubscribeFailure callback, %s",
- tc->clientid);
- assert("There should be no failures in this test. ", 0, "asyncTestOnSubscribeFailure callback was called\n", 0);
- }
- void asyncTestOnUnsubscribe(void* context, MQTTAsync_successData* response)
- {
- AsyncTestClient* tc = (AsyncTestClient*) context;
- MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
- int rc;
- MyLog(LOGA_DEBUG, "In asyncTestOnUnsubscribe callback, %s", tc->clientid);
- opts.onSuccess = asyncTestOnDisconnect;
- opts.context = tc;
- opts.timeout = 1000;
- rc = MQTTAsync_disconnect(tc->client, &opts);
- }
- void asyncTestOnSubscribe(void* context, MQTTAsync_successData* response)
- {
- AsyncTestClient* tc = (AsyncTestClient*) context;
- int rc, i;
- MyLog(LOGA_DEBUG, "In asyncTestOnSubscribe callback, %s", tc->clientid);
- //rc = Thread_lock_mutex(client_mutex);
- tc->subscribed = 1;
- //rc = Thread_unlock_mutex(client_mutex);
- for (i = 0; i < 3; i++)
- {
- MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
- pubmsg.payload
- = "a much longer message that we can shorten to the extent that we need to payload up to 11";
- pubmsg.payloadlen = 11;
- pubmsg.qos = i;
- pubmsg.retained = 0;
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- //opts.onSuccess = asyncTestOnSend;
- opts.context = &tc;
- rc = MQTTAsync_send(tc->client, tc->topic, pubmsg.payloadlen,
- pubmsg.payload, pubmsg.qos, pubmsg.retained, &opts);
- assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- tc->sentmsgs[i]++;
- MyLog(LOGA_DEBUG, "Maxmsgs %d", tc->maxmsgs);
- }
- }
- int asyncTestMessageArrived(void* context, char* topicName, int topicLen,
- MQTTAsync_message* m)
- {
- AsyncTestClient* tc = (AsyncTestClient*) context;
- int rc;
- //rc = Thread_lock_mutex(client_mutex);
- tc->rcvdmsgs[m->qos]++;
- //printf("Received messages: %d\n", tc->rcvdmsgs[m->qos]);
- MyLog(LOGA_DEBUG,
- "In asyncTestMessageArrived callback, %s total to exit %d, total received %d,%d,%d",
- tc->clientid, (tc->maxmsgs * 3), tc->rcvdmsgs[0], tc->rcvdmsgs[1],
- tc->rcvdmsgs[2]);
- if (tc->sentmsgs[m->qos] < tc->maxmsgs)
- {
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- //opts.onSuccess = asyncTestOnSend;
- opts.context = tc;
- MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
- pubmsg.payload
- = "a much longer message that we can shorten to the extent that we need to payload up to 11";
- pubmsg.payloadlen = 11;
- pubmsg.qos = m->qos;
- pubmsg.retained = 0;
- rc = MQTTAsync_send(tc->client, tc->topic, pubmsg.payloadlen,
- pubmsg.payload, pubmsg.qos, pubmsg.retained, &opts);
- assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d messages sent %d,%d,%d", rc);
- MyLog(LOGA_DEBUG, "Messages sent %d,%d,%d", tc->sentmsgs[0],
- tc->sentmsgs[1], tc->sentmsgs[2]);
- tc->sentmsgs[m->qos]++;
- }
- if ((tc->rcvdmsgs[0] + tc->rcvdmsgs[1] + tc->rcvdmsgs[2]) == (tc->maxmsgs* 3))
- {
- MyLog(LOGA_DEBUG, "Ready to unsubscribe");
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- opts.onSuccess = asyncTestOnUnsubscribe;
- opts.context = tc;
- rc = MQTTAsync_unsubscribe(tc->client, tc->topic, &opts);
- assert("Unsubscribe successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- }
- //rc = Thread_unlock_mutex(client_mutex);
- MyLog(LOGA_DEBUG, "Leaving asyncTestMessageArrived callback");
- MQTTAsync_freeMessage(&m);
- MQTTAsync_free(topicName);
- return 1;
- }
- void asyncTestOnDeliveryComplete(void* context, MQTTAsync_token token)
- {
- }
- void asyncTestOnConnect(void* context, MQTTAsync_successData* response)
- {
- AsyncTestClient* tc = (AsyncTestClient*) context;
- int subsqos = 2;
- int rc;
- MyLog(LOGA_DEBUG, "In asyncTestOnConnect callback, %s", tc->clientid);
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- opts.onSuccess = asyncTestOnSubscribe;
- opts.onFailure = asyncTestOnSubscribeFailure;
- opts.context = tc;
- rc = MQTTAsync_subscribe(tc->client, tc->topic, subsqos, &opts);
- assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- }
- /*********************************************************************
- Test1: SSL connection to non SSL MQTT server
- *********************************************************************/
- int test1Finished = 0;
- int test1OnFailureCalled = 0;
- void test1OnFailure(void* context, MQTTAsync_failureData* response)
- {
- MyLog(LOGA_DEBUG, "In connect onFailure callback, context %p", context);
- test1OnFailureCalled++;
- test1Finished = 1;
- }
- void test1OnConnect(void* context, MQTTAsync_successData* response)
- {
- MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p\n", context);
- assert("Connect should not succeed", 0, "connect success callback was called", 0);
- test1Finished = 1;
- }
- int test1(struct Options options)
- {
- char* testname = "test1";
- int subsqos = 2;
- MQTTAsync c;
- MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
- MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
- MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
- int rc = 0;
- char* test_topic = "C client SSL test1";
- int count = 0;
- test1Finished = 0;
- failures = 0;
- MyLog(LOGA_INFO, "Starting SSL test 1 - connection to nonSSL MQTT server");
- fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
- global_start_time = start_clock();
- rc = MQTTAsync_create(&c, "rubbish://wrong", "test1", MQTTCLIENT_PERSISTENCE_DEFAULT,
- NULL);
- assert("bad rc from create", rc == MQTTASYNC_BAD_PROTOCOL, "rc was %d \n", rc);
- rc = MQTTAsync_create(&c, options.connection, "test1", MQTTCLIENT_PERSISTENCE_DEFAULT,
- NULL);
- assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d \n", rc);
- if (rc != MQTTASYNC_SUCCESS)
- {
- MQTTAsync_destroy(&c);
- goto exit;
- }
- opts.keepAliveInterval = 20;
- opts.cleansession = 1;
- opts.username = "testuser";
- opts.password = "testpassword";
- opts.will = &wopts;
- opts.will->message = "will message";
- opts.will->qos = 1;
- opts.will->retained = 0;
- opts.will->topicName = "will topic";
- opts.will = NULL;
- opts.onSuccess = test1OnConnect;
- opts.onFailure = test1OnFailure;
- opts.context = c;
- rc = MQTTAsync_connect(c, &opts);
- assert("Bad rc from connect", rc == MQTTASYNC_NULL_PARAMETER, "rc was %d ", rc);
- opts.ssl = &sslopts;
- opts.ssl->enableServerCertAuth = 0;
- MyLog(LOGA_DEBUG, "Connecting");
- rc = MQTTAsync_connect(c, &opts);
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d ", rc);
- if (rc != MQTTASYNC_SUCCESS)
- {
- failures++;
- goto exit;
- }
- /* wait for success or failure callback */
- while (!test1Finished && ++count < 10000)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- exit: MQTTAsync_destroy(&c);
- MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", testname, tests, failures);
- write_test_result();
- return failures;
- }
- /*********************************************************************
- Test2a: Mutual SSL Authentication - Certificates in place on client and server
- *********************************************************************/
- void test2aOnConnectFailure(void* context, MQTTAsync_failureData* response)
- {
- AsyncTestClient* client = (AsyncTestClient*) context;
- MyLog(LOGA_DEBUG, "In test2aOnConnectFailure callback, %s",
- client->clientid);
- assert("There should be no failures in this test. ", 0, "test2aOnConnectFailure callback was called\n", 0);
- client->testFinished = 1;
- }
- void test2aOnPublishFailure(void* context, MQTTAsync_failureData* response)
- {
- AsyncTestClient* client = (AsyncTestClient*) context;
- MyLog(LOGA_DEBUG, "In test2aOnPublishFailure callback, %s",
- client->clientid);
- assert("There should be no failures in this test. ", 0, "test2aOnPublishFailure callback was called\n", 0);
- }
- int test2a(struct Options options)
- {
- char* testname = "test2a";
- AsyncTestClient tc =
- AsyncTestClient_initializer;
- MQTTAsync c;
- MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
- MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
- MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
- int rc = 0;
- failures = 0;
- MyLog(LOGA_INFO, "Starting test 2a - Mutual SSL authentication");
- fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
- global_start_time = start_clock();
- rc = MQTTAsync_create(&c, options.mutual_auth_connection, "test2a", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
- assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- tc.client = c;
- sprintf(tc.clientid, "%s", testname);
- sprintf(tc.topic, "C client SSL test2a");
- tc.maxmsgs = MAXMSGS;
- //tc.rcvdmsgs = 0;
- tc.subscribed = 0;
- tc.testFinished = 0;
- opts.keepAliveInterval = 20;
- opts.cleansession = 1;
- opts.username = "testuser";
- opts.password = "testpassword";
- opts.will = &wopts;
- opts.will->message = "will message";
- opts.will->qos = 1;
- opts.will->retained = 0;
- opts.will->topicName = "will topic";
- opts.will = NULL;
- opts.onSuccess = asyncTestOnConnect;
- opts.onFailure = test2aOnConnectFailure;
- opts.context = &tc;
- opts.ssl = &sslopts;
- if (options.server_key_file != NULL)
- opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
- opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
- if (options.client_key_pass != NULL)
- opts.ssl->privateKeyPassword = options.client_key_pass;
- //opts.ssl->enabledCipherSuites = "DEFAULT";
- //opts.ssl->enabledServerCertAuth = 1;
- opts.ssl->verify = 1;
- MyLog(LOGA_DEBUG, "enableServerCertAuth %d\n", opts.ssl->enableServerCertAuth);
- MyLog(LOGA_DEBUG, "verify %d\n", opts.ssl->verify);
- rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
- asyncTestOnDeliveryComplete);
- assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- MyLog(LOGA_DEBUG, "Connecting");
- rc = MQTTAsync_connect(c, &opts);
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- while (!tc.subscribed && !tc.testFinished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- if (tc.testFinished)
- goto exit;
- while (!tc.testFinished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- MyLog(LOGA_DEBUG, "Stopping");
- exit: MQTTAsync_destroy(&c);
- MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", testname, tests, failures);
- write_test_result();
- return failures;
- }
- /*********************************************************************
- Test2b: Mutual SSL Authentication - Server does not have Client cert
- *********************************************************************/
- int test2bFinished;
- void test2bOnConnectFailure(void* context, MQTTAsync_failureData* response)
- {
- MyLog(LOGA_DEBUG, "In test2bOnConnectFailure callback, context %p", context);
- assert("This test should call test2bOnConnectFailure. ", 1, "test2bOnConnectFailure callback was called\n", 1);
- test2bFinished = 1;
- }
- void test2bOnConnect(void* context, MQTTAsync_successData* response)
- {
- MyLog(LOGA_DEBUG, "In test2bOnConnectFailure callback, context %p", context);
- assert("This connect should not succeed. ", 0, "test2bOnConnect callback was called\n", 0);
- test2bFinished = 1;
- }
- int test2b(struct Options options)
- {
- char* testname = "test2b";
- int subsqos = 2;
- MQTTAsync c;
- MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
- MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
- MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
- int rc = 0;
- int count = 0;
- test2bFinished = 0;
- failures = 0;
- MyLog(LOGA_INFO,
- "Starting test 2b - connection to SSL MQTT server with clientauth=req but server does not have client cert");
- fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
- global_start_time = start_clock();
- rc = MQTTAsync_create(&c, options.nocert_mutual_auth_connection,
- "test2b", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
- assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- if (rc != MQTTASYNC_SUCCESS)
- {
- MQTTAsync_destroy(&c);
- goto exit;
- }
- opts.keepAliveInterval = 20;
- opts.cleansession = 1;
- opts.username = "testuser";
- opts.password = "testpassword";
- opts.will = &wopts;
- opts.will->message = "will message";
- opts.will->qos = 1;
- opts.will->retained = 0;
- opts.will->topicName = "will topic";
- opts.will = NULL;
- opts.onSuccess = test2bOnConnect;
- opts.onFailure = test2bOnConnectFailure;
- opts.context = c;
- opts.ssl = &sslopts;
- if (options.server_key_file != NULL)
- opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
- opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
- if (options.client_key_pass != NULL)
- opts.ssl->privateKeyPassword = options.client_key_pass;
- //opts.ssl->enabledCipherSuites = "DEFAULT";
- //opts.ssl->enabledServerCertAuth = 0;
- MyLog(LOGA_DEBUG, "Connecting");
- rc = MQTTAsync_connect(c, &opts);
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- while (!test2bFinished && ++count < 10000)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- exit: MQTTAsync_destroy(&c);
- MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", testname, tests, failures);
- write_test_result();
- return failures;
- }
- /*********************************************************************
- Test2c: Mutual SSL Authentication - Client does not have Server cert
- *********************************************************************/
- int test2cFinished;
- void test2cOnConnectFailure(void* context, MQTTAsync_failureData* response)
- {
- MyLog(LOGA_DEBUG, "In test2cOnConnectFailure callback, context %p", context);
- assert("This test should call test2cOnConnectFailure. ", 1, "test2cOnConnectFailure callback was called\n", 0);
- test2cFinished = 1;
- }
- void test2cOnConnect(void* context, MQTTAsync_successData* response)
- {
- MyLog(LOGA_DEBUG, "In test2cOnConnect callback, context %p", context);
- assert("This connect should not succeed. ", 0, "test2cOnConnect callback was called\n", 0);
- test2cFinished = 1;
- }
- int test2c(struct Options options)
- {
- char* testname = "test2c";
- int subsqos = 2;
- MQTTAsync c;
- MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
- MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
- MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
- int rc = 0;
- char* test_topic = "C client test2c";
- int count = 0;
- failures = 0;
- MyLog(
- LOGA_INFO,
- "Starting test 2c - connection to SSL MQTT server, server auth enabled but unknown cert");
- fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
- global_start_time = start_clock();
- rc = MQTTAsync_create(&c, options.nocert_mutual_auth_connection,
- "test2c", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
- assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- if (rc != MQTTASYNC_SUCCESS)
- {
- MQTTAsync_destroy(&c);
- goto exit;
- }
- opts.keepAliveInterval = 20;
- opts.cleansession = 1;
- opts.username = "testuser";
- opts.password = "testpassword";
- opts.will = &wopts;
- opts.will->message = "will message";
- opts.will->qos = 1;
- opts.will->retained = 0;
- opts.will->topicName = "will topic";
- opts.will = NULL;
- opts.onSuccess = test2cOnConnect;
- opts.onFailure = test2cOnConnectFailure;
- opts.context = c;
- opts.ssl = &sslopts;
- //if (options.server_key_file != NULL) opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
- opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
- if (options.client_key_pass != NULL)
- opts.ssl->privateKeyPassword = options.client_key_pass;
- //opts.ssl->enabledCipherSuites = "DEFAULT";
- //opts.ssl->enabledServerCertAuth = 0;
- MyLog(LOGA_DEBUG, "Connecting");
- rc = MQTTAsync_connect(c, &opts);
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- {
- failures++;
- goto exit;
- }
- while (!test2cFinished && ++count < 10000)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- exit: MQTTAsync_destroy(&c);
- MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", testname, tests, failures);
- write_test_result();
- return failures;
- }
- /*********************************************************************
- Test2d: Mutual SSL Authentication - client has no certs
- *********************************************************************/
- int test2dFinished;
- void test2dOnConnectFailure(void* context, MQTTAsync_failureData* response)
- {
- MyLog(LOGA_DEBUG, "In test2dOnConnectFailure callback, context %p", context);
- assert("This test should call test2dOnConnectFailure. ", 1, "test2dOnConnectFailure callback was called\n", 0);
- test2dFinished = 1;
- }
- void test2dOnConnect(void* context, MQTTAsync_successData* response)
- {
- MyLog(LOGA_DEBUG, "In test2dOnConnect callback, context %p", context);
- assert("This connect should not succeed. ", 0, "test2dOnConnect callback was called\n", 0);
- test2dFinished = 1;
- }
- int test2d(struct Options options)
- {
- char* testname = "test2d";
- int subsqos = 2;
- MQTTAsync c;
- MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
- MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
- MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
- int rc = 0;
- char* test_topic = "C client test2d";
- int count = 0;
- unsigned int iteration = 0;
- failures = 0;
- MyLog(
- LOGA_INFO,
- "Starting test 2d - connection to SSL MQTT server, server auth enabled but unknown cert");
- fprintf(xml, "<testcase classname=\"test2d\" name=\"%s\"", testname);
- global_start_time = start_clock();
- // As reported in https://github.com/eclipse/paho.mqtt.c/issues/190
- // there is/was some race condition, which caused _sometimes_ that the library failed to detect,
- // that the connect attempt has already failed.
- // Therefore we need to test this several times!
- for (iteration = 0; !failures && (iteration < 20) ; iteration++)
- {
- count = 0;
- MQTTAsync_setTraceLevel(MQTTASYNC_TRACE_ERROR);
- rc = MQTTAsync_create(&c, options.mutual_auth_connection,
- "test2d", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
- assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- if (rc != MQTTASYNC_SUCCESS)
- {
- MQTTAsync_destroy(&c);
- failures++;
- break;
- }
- opts.keepAliveInterval = 60;
- opts.cleansession = 1;
- opts.will = &wopts;
- opts.will->message = "will message";
- opts.will->qos = 1;
- opts.will->retained = 0;
- opts.will->topicName = "will topic";
- opts.will = NULL;
- opts.onSuccess = test2dOnConnect;
- opts.onFailure = test2dOnConnectFailure;
- opts.context = c;
- opts.ssl = &sslopts;
- if (options.server_key_file != NULL) opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
- opts.ssl->keyStore = NULL; /*file of certificate for client to present to server - In this test the client has no certificate! */
- test2dFinished = 0;
- MyLog(LOGA_DEBUG, "Connecting");
- rc = MQTTAsync_connect(c, &opts);
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- {
- failures++;
- MyLog(LOGA_INFO, "Failed in iteration %d\n",iteration);
- MQTTAsync_destroy(&c);
- break;
- }
- #define TEST2D_COUNT 1000
- while (!test2dFinished && ++count < TEST2D_COUNT)
- {
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- }
- if (!test2dFinished && count >= TEST2D_COUNT)
- {
- MyLog(LOGA_INFO, "Failed in iteration %d\n",iteration);
- failures++;
- }
- MQTTAsync_destroy(&c);
- }
- MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", testname, tests, failures);
- write_test_result();
- return failures;
- }
- /*********************************************************************
- Test2e: Mutual SSL Authentication using serverURIs
- *********************************************************************/
- void test2eOnConnectFailure(void* context, MQTTAsync_failureData* response)
- {
- AsyncTestClient* client = (AsyncTestClient*) context;
- MyLog(LOGA_DEBUG, "In test2eOnConnectFailure callback, %s",
- client->clientid);
- assert("There should be no failures in this test. ", 0, "test2eOnConnectFailure callback was called\n", 0);
- client->testFinished = 1;
- }
- void test2eOnPublishFailure(void* context, MQTTAsync_failureData* response)
- {
- AsyncTestClient* client = (AsyncTestClient*) context;
- MyLog(LOGA_DEBUG, "In test2eOnPublishFailure callback, %s",
- client->clientid);
- assert("There should be no failures in this test. ", 0, "test2eOnPublishFailure callback was called\n", 0);
- }
- int test2e(struct Options options)
- {
- char* testname = "test2e";
- AsyncTestClient tc =
- AsyncTestClient_initializer;
- MQTTAsync c;
- MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
- MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
- MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
- char* uris[2] = {"rubbish", options.mutual_auth_connection};
- int rc = 0;
- failures = 0;
- MyLog(LOGA_INFO, "Starting test 2e - Mutual SSL authentication with serverURIs");
- fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
- global_start_time = start_clock();
- rc = MQTTAsync_create(&c, "none", "test2e", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
- assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- tc.client = c;
- sprintf(tc.clientid, "%s", testname);
- sprintf(tc.topic, "C client SSL test2e");
- tc.maxmsgs = MAXMSGS;
- //tc.rcvdmsgs = 0;
- tc.subscribed = 0;
- tc.testFinished = 0;
- opts.keepAliveInterval = 20;
- opts.cleansession = 1;
- opts.username = "testuser";
- opts.password = "testpassword";
- opts.will = &wopts;
- opts.will->message = "will message";
- opts.will->qos = 1;
- opts.will->retained = 0;
- opts.will->topicName = "will topic";
- opts.will = NULL;
- opts.onSuccess = asyncTestOnConnect;
- opts.onFailure = test2eOnConnectFailure;
- opts.context = &tc;
- opts.serverURIs = uris;
- opts.serverURIcount = 2;
- opts.ssl = &sslopts;
- if (options.server_key_file != NULL)
- opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
- opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
- if (options.client_key_pass != NULL)
- opts.ssl->privateKeyPassword = options.client_key_pass;
- //opts.ssl->enabledCipherSuites = "DEFAULT";
- //opts.ssl->enabledServerCertAuth = 1;
- opts.ssl->verify = 1;
- MyLog(LOGA_DEBUG, "enableServerCertAuth %d\n", opts.ssl->enableServerCertAuth);
- MyLog(LOGA_DEBUG, "verify %d\n", opts.ssl->verify);
- rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
- asyncTestOnDeliveryComplete);
- assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- MyLog(LOGA_DEBUG, "Connecting");
- rc = MQTTAsync_connect(c, &opts);
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- while (!tc.subscribed && !tc.testFinished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- if (tc.testFinished)
- goto exit;
- while (!tc.testFinished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- MyLog(LOGA_DEBUG, "Stopping");
- exit: MQTTAsync_destroy(&c);
- MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", testname, tests, failures);
- write_test_result();
- return failures;
- }
- /*********************************************************************
- Test3a: Server Authentication - server certificate in client trust store
- *********************************************************************/
- void test3aOnConnectFailure(void* context, MQTTAsync_failureData* response)
- {
- AsyncTestClient* client = (AsyncTestClient*) context;
- MyLog(LOGA_DEBUG, "In test3aOnConnectFailure callback, context %p", context);
- assert("There should be no failures in this test. ", 0, "test3aOnConnectFailure callback was called\n", 0);
- client->testFinished = 1;
- }
- int test3a(struct Options options)
- {
- char* testname = "test3a";
- int subsqos = 2;
- /* TODO - usused - remove ? MQTTAsync_deliveryToken* dt = NULL; */
- AsyncTestClient tc =
- AsyncTestClient_initializer;
- MQTTAsync c;
- MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
- MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
- MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
- int rc = 0;
- int i;
- failures = 0;
- MyLog(LOGA_INFO, "Starting test 3a - Server authentication");
- fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
- global_start_time = start_clock();
- MQTTAsync_create(&c, options.server_auth_connection, "test3a", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
- tc.client = c;
- sprintf(tc.clientid, "%s", testname);
- sprintf(tc.topic, "C client SSL test3a");
- tc.maxmsgs = MAXMSGS;
- //tc.rcvdmsgs = 0;
- tc.subscribed = 0;
- tc.testFinished = 0;
- opts.keepAliveInterval = 20;
- opts.cleansession = 1;
- opts.username = "testuser";
- opts.password = "testpassword";
- opts.will = &wopts;
- opts.will->message = "will message";
- opts.will->qos = 1;
- opts.will->retained = 0;
- opts.will->topicName = "will topic";
- opts.will = NULL;
- opts.onSuccess = asyncTestOnConnect;
- opts.onFailure = test3aOnConnectFailure;
- opts.context = &tc;
- opts.ssl = &sslopts;
- if (options.server_key_file != NULL)
- opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
- //opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
- //if (options.client_key_pass != NULL) opts.ssl->privateKeyPassword = options.client_key_pass;
- //opts.ssl->enabledCipherSuites = "DEFAULT";
- //opts.ssl->enabledServerCertAuth = 1;
- rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
- asyncTestOnDeliveryComplete);
- assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- MyLog(LOGA_DEBUG, "Connecting");
- rc = MQTTAsync_connect(c, &opts);
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- while (!tc.subscribed && !tc.testFinished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- if (tc.testFinished)
- goto exit;
- for (i = 0; i < 3; i++)
- {
- MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
- pubmsg.payload
- = "a much longer message that we can shorten to the extent that we need to payload up to 11";
- pubmsg.payloadlen = 11;
- pubmsg.qos = i;
- pubmsg.retained = 0;
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- opts.onSuccess = asyncTestOnSend;
- opts.context = &tc;
- rc = MQTTAsync_send(c, tc.topic, pubmsg.payloadlen, pubmsg.payload,
- pubmsg.qos, pubmsg.retained, &opts);
- assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- }
- while (!tc.testFinished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- MyLog(LOGA_DEBUG, "Stopping");
- MQTTAsync_destroy(&c);
- exit: MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.", (failures
- == 0) ? "passed" : "failed", testname, tests, failures);
- write_test_result();
- return failures;
- }
- /*********************************************************************
- Test3b: Server Authentication - Client does not have server cert
- *********************************************************************/
- int test3bFinished;
- void test3bOnConnectFailure(void* context, MQTTAsync_failureData* response)
- {
- MyLog(LOGA_DEBUG, "In test3bOnConnectFailure callback, context %p", context);
- assert("This test should call test3bOnConnectFailure. ", 1, "test3bOnConnectFailure callback was called\n", 1);
- test3bFinished = 1;
- }
- void test3bOnConnect(void* context, MQTTAsync_successData* response)
- {
- MyLog(LOGA_DEBUG, "In test3bOnConnectFailure callback, context %p", context);
- assert("This connect should not succeed. ", 0, "test3bOnConnect callback was called\n", 0);
- test3bFinished = 1;
- }
- int test3b(struct Options options)
- {
- char* testname = "test3b";
- int subsqos = 2;
- MQTTAsync c;
- MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
- MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
- MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
- int rc = 0;
- int count = 0;
- test3bFinished = 0;
- failures = 0;
- MyLog(
- LOGA_INFO,
- "Starting test 3b - connection to SSL MQTT server with clientauth=opt but client does not have server cert");
- fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
- global_start_time = start_clock();
- rc = MQTTAsync_create(&c, options.server_auth_connection, "test3b", MQTTCLIENT_PERSISTENCE_DEFAULT,
- NULL);
- assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- opts.keepAliveInterval = 20;
- opts.cleansession = 1;
- opts.username = "testuser";
- opts.password = "testpassword";
- opts.will = &wopts;
- opts.will->message = "will message";
- opts.will->qos = 1;
- opts.will->retained = 0;
- opts.will->topicName = "will topic";
- opts.will = NULL;
- opts.onSuccess = test3bOnConnect;
- opts.onFailure = test3bOnConnectFailure;
- opts.context = c;
- opts.ssl = &sslopts;
- //if (options.server_key_file != NULL) opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
- //opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
- //if (options.client_key_pass != NULL) opts.ssl->privateKeyPassword = options.client_key_pass;
- //opts.ssl->enabledCipherSuites = "DEFAULT";
- //opts.ssl->enabledServerCertAuth = 0;
- MyLog(LOGA_DEBUG, "Connecting");
- rc = MQTTAsync_connect(c, &opts);
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- while (!test3bFinished && ++count < 10000)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- exit: MQTTAsync_destroy(&c);
- MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", testname, tests, failures);
- write_test_result();
- return failures;
- }
- /*********************************************************************
- Test4: Accept invalid server certificates
- *********************************************************************/
- void test4OnConnectFailure(void* context, MQTTAsync_failureData* response)
- {
- AsyncTestClient* client = (AsyncTestClient*) context;
- MyLog(LOGA_DEBUG, "In test4OnConnectFailure callback, context %p", context);
- assert("There should be no failures in this test. ", 0, "test4OnConnectFailure callback was called\n", 0);
- client->testFinished = 1;
- }
- void test4OnPublishFailure(void* context, MQTTAsync_failureData* response)
- {
- MyLog(LOGA_DEBUG, "In test4OnPublishFailure callback, context %p", context);
- assert("There should be no failures in this test. ", 0, "test4OnPublishFailure callback was called\n", 0);
- }
- int test4(struct Options options)
- {
- char* testname = "test4";
- int subsqos = 2;
- /* TODO - usused - remove ? MQTTAsync_deliveryToken* dt = NULL; */
- AsyncTestClient tc =
- AsyncTestClient_initializer;
- MQTTAsync c;
- MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
- MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
- MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
- int rc = 0;
- int i;
- failures = 0;
- MyLog(LOGA_INFO, "Starting test 4 - accept invalid server certificates");
- fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
- global_start_time = start_clock();
- MQTTAsync_create(&c, options.server_auth_connection, "test4", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
- tc.client = c;
- sprintf(tc.clientid, "%s", testname);
- sprintf(tc.topic, "C client SSL test4");
- tc.maxmsgs = MAXMSGS;
- //tc.rcvdmsgs = 0;
- tc.subscribed = 0;
- tc.testFinished = 0;
- opts.keepAliveInterval = 20;
- opts.cleansession = 1;
- opts.username = "testuser";
- opts.password = "testpassword";
- opts.will = &wopts;
- opts.will->message = "will message";
- opts.will->qos = 1;
- opts.will->retained = 0;
- opts.will->topicName = "will topic";
- opts.will = NULL;
- opts.onSuccess = asyncTestOnConnect;
- opts.onFailure = test4OnConnectFailure;
- opts.context = &tc;
- opts.ssl = &sslopts;
- //if (options.server_key_file != NULL) opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
- //opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
- //if (options.client_key_pass != NULL) opts.ssl->privateKeyPassword = options.client_key_pass;
- //opts.ssl->enabledCipherSuites = "DEFAULT";
- opts.ssl->enableServerCertAuth = 0;
- rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
- asyncTestOnDeliveryComplete);
- assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- MyLog(LOGA_DEBUG, "Connecting");
- rc = MQTTAsync_connect(c, &opts);
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- while (!tc.subscribed && !tc.testFinished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- if (tc.testFinished)
- goto exit;
- while (!tc.testFinished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- MyLog(LOGA_DEBUG, "Stopping");
- exit: MQTTAsync_destroy(&c);
- MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", testname, tests, failures);
- write_test_result();
- return failures;
- }
- /*********************************************************************
- Test5a: Anonymous ciphers - server auth disabled
- *********************************************************************/
- void test5aOnConnectFailure(void* context, MQTTAsync_failureData* response)
- {
- AsyncTestClient* client = (AsyncTestClient*) context;
- MyLog(LOGA_DEBUG, "In test5aOnConnectFailure callback, context %p", context);
- assert("There should be no failures in this test. ", 0, "test5aOnConnectFailure callback was called\n", 0);
- client->testFinished = 1;
- }
- void test5aOnPublishFailure(void* context, MQTTAsync_failureData* response)
- {
- MyLog(LOGA_DEBUG, "In test5aOnPublishFailure callback, context %p", context);
- assert("There should be no failures in this test. ", 0, "test5aOnPublishFailure callback was called\n", 0);
- }
- int test5a(struct Options options)
- {
- char* testname = "test5a";
- AsyncTestClient tc =
- AsyncTestClient_initializer;
- MQTTAsync c;
- MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
- MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
- MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
- int rc = 0;
- int i;
- failures = 0;
- MyLog(LOGA_INFO,
- "Starting SSL test 5a - Anonymous ciphers - server authentication disabled");
- fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
- global_start_time = start_clock();
- rc = MQTTAsync_create(&c, options.anon_connection, "test5a", MQTTCLIENT_PERSISTENCE_DEFAULT,
- NULL);
- assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- tc.client = c;
- sprintf(tc.clientid, "%s", testname);
- sprintf(tc.topic, "C client SSL test5a");
- tc.maxmsgs = MAXMSGS;
- //tc.rcvdmsgs = 0;
- tc.subscribed = 0;
- tc.testFinished = 0;
- opts.keepAliveInterval = 20;
- opts.cleansession = 1;
- opts.username = "testuser";
- opts.password = "testpassword";
- opts.will = &wopts;
- opts.will->message = "will message";
- opts.will->qos = 1;
- opts.will->retained = 0;
- opts.will->topicName = "will topic";
- opts.will = NULL;
- opts.onSuccess = asyncTestOnConnect;
- opts.onFailure = test5aOnConnectFailure;
- opts.context = &tc;
- opts.ssl = &sslopts;
- //opts.ssl->trustStore = /*file of certificates trusted by client*/
- //opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
- //if (options.client_key_pass != NULL) opts.ssl->privateKeyPassword = options.client_key_pass;
- opts.ssl->enabledCipherSuites = "aNULL";
- opts.ssl->enableServerCertAuth = 0;
- rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
- asyncTestOnDeliveryComplete);
- assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- MyLog(LOGA_DEBUG, "Connecting");
- rc = MQTTAsync_connect(c, &opts);
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- while (!tc.subscribed && !tc.testFinished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- if (tc.testFinished)
- goto exit;
- for (i = 0; i < 3; i++)
- {
- MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
- pubmsg.payload
- = "a much longer message that we can shorten to the extent that we need to payload up to 11";
- pubmsg.payloadlen = 11;
- pubmsg.qos = i;
- pubmsg.retained = 0;
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- opts.onSuccess = asyncTestOnSend;
- opts.context = &tc;
- rc = MQTTAsync_send(c, tc.topic, pubmsg.payloadlen, pubmsg.payload,
- pubmsg.qos, pubmsg.retained, &opts);
- assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- }
- while (!tc.testFinished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- MyLog(LOGA_DEBUG, "Stopping");
- exit: MQTTAsync_destroy(&c);
- MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", testname, tests, failures);
- write_test_result();
- return failures;
- }
- /*********************************************************************
- Test5b: Anonymous ciphers - server auth enabled
- ********************************************************************/
- void test5bOnConnectFailure(void* context, MQTTAsync_failureData* response)
- {
- AsyncTestClient* client = (AsyncTestClient*) context;
- MyLog(LOGA_DEBUG, "In test5bOnConnectFailure callback, context %p", context);
- assert("There should be no failures in this test. ", 0, "test5bOnConnectFailure callback was called\n", 0);
- client->testFinished = 1;
- }
- void test5bOnPublishFailure(void* context, MQTTAsync_failureData* response)
- {
- MyLog(LOGA_DEBUG, "In test5bOnPublishFailure callback, context %p", context);
- assert("There should be no failures in this test. ", 0, "test5bOnPublishFailure callback was called\n", 0);
- }
- int test5b(struct Options options)
- {
- char* testname = "test5b";
- AsyncTestClient tc =
- AsyncTestClient_initializer;
- MQTTAsync c;
- MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
- MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
- MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
- int rc = 0;
- int i;
- failures = 0;
- MyLog(LOGA_INFO,
- "Starting SSL test 5b - Anonymous ciphers - server authentication enabled");
- fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
- global_start_time = start_clock();
- rc = MQTTAsync_create(&c, options.anon_connection, "test5b", MQTTCLIENT_PERSISTENCE_DEFAULT,
- NULL);
- assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- tc.client = c;
- sprintf(tc.clientid, "%s", testname);
- sprintf(tc.topic, "C client SSL test5b");
- tc.maxmsgs = MAXMSGS;
- //tc.rcvdmsgs = 0;
- tc.subscribed = 0;
- tc.testFinished = 0;
- opts.keepAliveInterval = 20;
- opts.cleansession = 1;
- opts.username = "testuser";
- opts.password = "testpassword";
- opts.will = &wopts;
- opts.will->message = "will message";
- opts.will->qos = 1;
- opts.will->retained = 0;
- opts.will->topicName = "will topic";
- opts.will = NULL;
- opts.onSuccess = asyncTestOnConnect;
- opts.onFailure = test5bOnConnectFailure;
- opts.context = &tc;
- opts.ssl = &sslopts;
- //opts.ssl->trustStore = /*file of certificates trusted by client*/
- //opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
- //if (options.client_key_pass != NULL) opts.ssl->privateKeyPassword = options.client_key_pass;
- opts.ssl->enabledCipherSuites = "aNULL";
- opts.ssl->enableServerCertAuth = 1;
- rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
- asyncTestOnDeliveryComplete);
- assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- MyLog(LOGA_DEBUG, "Connecting");
- rc = MQTTAsync_connect(c, &opts);
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- while (!tc.subscribed && !tc.testFinished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- if (tc.testFinished)
- goto exit;
- for (i = 0; i < 3; i++)
- {
- MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
- pubmsg.payload
- = "a much longer message that we can shorten to the extent that we need to payload up to 11";
- pubmsg.payloadlen = 11;
- pubmsg.qos = i;
- pubmsg.retained = 0;
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- opts.onSuccess = asyncTestOnSend;
- opts.context = &tc;
- rc = MQTTAsync_send(c, tc.topic, pubmsg.payloadlen, pubmsg.payload,
- pubmsg.qos, pubmsg.retained, &opts);
- assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- }
- while (!tc.testFinished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- MyLog(LOGA_DEBUG, "Stopping");
- exit: MQTTAsync_destroy(&c);
- MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", testname, tests, failures);
- write_test_result();
- return failures;
- }
- /*********************************************************************
- Test5c: Anonymous ciphers - client not using anonymous ciphers
- *********************************************************************/
- int test5cFinished;
- void test5cOnConnectFailure(void* context, MQTTAsync_failureData* response)
- {
- MyLog(LOGA_DEBUG, "In test5cOnConnectFailure callback, context %p", context);
- assert("This test should call test5cOnConnectFailure. ", 1, "test5cOnConnectFailure callback was called\n", 1);
- test5cFinished = 1;
- }
- void test5cOnConnect(void* context, MQTTAsync_successData* response)
- {
- MyLog(LOGA_DEBUG, "In test5cOnConnectFailure callback, context %p", context);
- assert("This connect should not succeed. ", 0, "test5cOnConnect callback was called\n", 0);
- test5cFinished = 1;
- }
- int test5c(struct Options options)
- {
- char* testname = "test5c";
- int subsqos = 2;
- MQTTAsync c;
- MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
- MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
- MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
- int rc = 0;
- int count = 0;
- test5cFinished = 0;
- failures = 0;
- MyLog(LOGA_INFO,
- "Starting SSL test 5c - Anonymous ciphers - client not using anonymous cipher");
- fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
- global_start_time = start_clock();
- rc = MQTTAsync_create(&c, options.anon_connection, "test5c", MQTTCLIENT_PERSISTENCE_DEFAULT,
- NULL);
- assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- opts.keepAliveInterval = 20;
- opts.cleansession = 1;
- opts.username = "testuser";
- opts.password = "testpassword";
- opts.will = &wopts;
- opts.will->message = "will message";
- opts.will->qos = 1;
- opts.will->retained = 0;
- opts.will->topicName = "will topic";
- opts.will = NULL;
- opts.onSuccess = test5cOnConnect;
- opts.onFailure = test5cOnConnectFailure;
- opts.context = c;
- opts.ssl = &sslopts;
- //opts.ssl->trustStore = /*file of certificates trusted by client*/
- //opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
- //if (options.client_key_pass != NULL) opts.ssl->privateKeyPassword = options.client_key_pass;
- //opts.ssl->enabledCipherSuites = "DEFAULT";
- opts.ssl->enableServerCertAuth = 0;
- MyLog(LOGA_DEBUG, "Connecting");
- rc = MQTTAsync_connect(c, &opts);
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- while (!test5cFinished && ++count < 10000)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- exit:
- MQTTAsync_destroy(&c);
- MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", testname, tests, failures);
- write_test_result();
- return failures;
- }
- /*********************************************************************
- Test6: More than one client object - simultaneous working.
- *********************************************************************/
- void test6OnConnectFailure(void* context, MQTTAsync_failureData* response)
- {
- AsyncTestClient* client = (AsyncTestClient*) context;
- MyLog(LOGA_DEBUG, "In test6OnConnectFailure callback, context %p", context);
- assert("There should be no failures in this test. ", 0, "test6OnConnectFailure callback was called\n", 0);
- client->testFinished = 1;
- }
- void test6OnPublishFailure(void* context, MQTTAsync_failureData* response)
- {
- MyLog(LOGA_DEBUG, "In test6OnPublishFailure callback, context %p", context);
- assert("There should be no failures in this test. ", 0, "test6OnPublishFailure callback was called\n", 0);
- }
- int test6(struct Options options)
- {
- char* testname = "test6";
- #define num_clients 10
- int subsqos = 2;
- MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
- MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
- MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
- int rc = 0;
- int i;
- AsyncTestClient tc[num_clients];
- int test6finished = 0;
- MyLog(LOGA_INFO, "Starting test 6 - multiple connections");
- fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
- global_start_time = start_clock();
- for (i = 0; i < num_clients; ++i)
- {
- tc[i].maxmsgs = MAXMSGS;
- tc[i].rcvdmsgs[0] = 0;
- tc[i].rcvdmsgs[1] = 0;
- tc[i].rcvdmsgs[2] = 0;
- tc[i].sentmsgs[0] = 0;
- tc[i].sentmsgs[1] = 0;
- tc[i].sentmsgs[2] = 0;
- tc[i].testFinished = 0;
- sprintf(tc[i].clientid, "sslasync_test6_num_%d", i);
- sprintf(tc[i].topic, "sslasync test6 topic num %d", i);
- rc = MQTTAsync_create(&(tc[i].client), options.server_auth_connection, tc[i].clientid,
- MQTTCLIENT_PERSISTENCE_NONE, NULL);
- assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- rc = MQTTAsync_setCallbacks(tc[i].client, &tc[i], NULL,
- asyncTestMessageArrived, NULL);
- assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- opts.keepAliveInterval = 20;
- opts.cleansession = 1;
- opts.username = "testuser";
- opts.password = "testpassword";
- opts.will = &wopts;
- opts.will->message = "will message";
- opts.will->qos = 1;
- opts.will->retained = 0;
- opts.will->topicName = "will topic";
- opts.onSuccess = asyncTestOnConnect;
- opts.onFailure = test6OnConnectFailure;
- opts.context = &tc[i];
- opts.ssl = &sslopts;
- if (options.server_key_file != NULL)
- opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
- opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
- if (options.client_key_pass != NULL)
- opts.ssl->privateKeyPassword = options.client_key_pass;
- //opts.ssl->enabledCipherSuites = "DEFAULT";
- //opts.ssl->enabledServerCertAuth = 1;
- MyLog(LOGA_DEBUG, "Connecting");
- rc = MQTTAsync_connect(tc[i].client, &opts);
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- }
- while (test6finished < num_clients)
- {
- MyLog(LOGA_DEBUG, "num_clients %d test_finished %d\n", num_clients,
- test6finished);
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- for (i = 0; i < num_clients; ++i)
- {
- if (tc[i].testFinished)
- {
- test6finished++;
- tc[i].testFinished = 0;
- }
- }
- }
- MyLog(LOGA_DEBUG, "test6: destroying clients");
- for (i = 0; i < num_clients; ++i)
- MQTTAsync_destroy(&tc[i].client);
- //exit:
- MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", testname, tests, failures);
- write_test_result();
- return failures;
- }
- /*********************************************************************
- Test7: Send and receive big messages
- *********************************************************************/
- void* test7_payload = NULL;
- int test7_payloadlen = 0;
- void test7OnConnectFailure(void* context, MQTTAsync_failureData* response)
- {
- AsyncTestClient* client = (AsyncTestClient*) context;
- MyLog(LOGA_DEBUG, "In test7OnConnectFailure callback, %s", client->clientid);
- assert("There should be no failures in this test. ", 0, "test7OnConnectFailure callback was called\n", 0);
- client->testFinished = 1;
- }
- int test7OnPublishSuccessCount = 0;
- int test7OnUnsubscribed = 0;
- void test7OnPublishSuccess(void* context, MQTTAsync_successData* response)
- {
- AsyncTestClient* tc = (AsyncTestClient*) context;
- MyLog(LOGA_DEBUG, "In test7OnPublishSuccess callback, %s, qos %d", tc->clientid,
- response->alt.pub.message.qos);
- test7OnPublishSuccessCount++;
- }
- void test7OnPublishFailure(void* context, MQTTAsync_failureData* response)
- {
- AsyncTestClient* client = (AsyncTestClient*) context;
- MyLog(LOGA_DEBUG, "In test7OnPublishFailure callback, %s %d", client->clientid);
- assert("There should be no failures in this test. ", 0, "test7OnPublishFailure callback was called\n", 0);
- client->testFinished = 1;
- }
- void test7OnUnsubscribe(void* context, MQTTAsync_successData* response)
- {
- AsyncTestClient* tc = (AsyncTestClient*) context;
- int rc;
- MyLog(LOGA_DEBUG, "In test7OnUnsubscribe callback, %s %d %d", tc->clientid,
- test7OnUnsubscribed, test7OnPublishSuccessCount);
- test7OnUnsubscribed++;
- }
- int test7MessageArrived(void* context, char* topicName, int topicLen,
- MQTTAsync_message* message)
- {
- AsyncTestClient* tc = (AsyncTestClient*) context;
- static int message_count = 0;
- int rc, i;
- MyLog(LOGA_DEBUG, "In messageArrived callback %p", tc);
- assert("Message size correct", message->payloadlen == test7_payloadlen,
- "message size was %d", message->payloadlen);
- for (i = 0; i < options.size; ++i)
- {
- if (((char*) test7_payload)[i] != ((char*) message->payload)[i])
- {
- assert("Message contents correct", ((char*)test7_payload)[i] != ((char*)message->payload)[i],
- "message content was %c", ((char*)message->payload)[i]);
- break;
- }
- }
- if (++message_count == 1)
- {
- MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- pubmsg.payload = test7_payload;
- pubmsg.payloadlen = test7_payloadlen;
- pubmsg.qos = 1;
- pubmsg.retained = 0;
- opts.onSuccess = test7OnPublishSuccess;
- opts.onFailure = test7OnPublishFailure;
- opts.context = tc;
- rc = MQTTAsync_sendMessage(tc->client, tc->topic, &pubmsg, &opts);
- assert("Publish successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- }
- else if (message_count < options.message_count)
- {
- MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- pubmsg.payload = test7_payload;
- pubmsg.payloadlen = test7_payloadlen;
- pubmsg.qos = 0;
- pubmsg.retained = 0;
- opts.onSuccess = test7OnPublishSuccess;
- opts.onFailure = test7OnPublishFailure;
- opts.context = tc;
- rc = MQTTAsync_sendMessage(tc->client, tc->topic, &pubmsg, &opts);
- assert("Publish successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- }
- else
- {
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- opts.onSuccess = test7OnUnsubscribe;
- opts.context = tc;
- rc = MQTTAsync_unsubscribe(tc->client, tc->topic, &opts);
- assert("Unsubscribe successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- }
- MQTTAsync_freeMessage(&message);
- MQTTAsync_free(topicName);
- return 1;
- }
- void test7OnSubscribe(void* context, MQTTAsync_successData* response)
- {
- AsyncTestClient* tc = (AsyncTestClient*) context;
- MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- int rc, i;
- MyLog(LOGA_DEBUG, "In subscribe onSuccess callback %p", tc);
- pubmsg.payload = test7_payload = malloc(options.size);
- pubmsg.payloadlen = test7_payloadlen = options.size;
- srand(33);
- for (i = 0; i < options.size; ++i)
- ((char*) pubmsg.payload)[i] = rand() % 256;
- pubmsg.qos = 2;
- pubmsg.retained = 0;
- pubmsg.payload = test7_payload;
- pubmsg.payloadlen = test7_payloadlen;
- opts.onSuccess = test7OnPublishSuccess;
- opts.onFailure = test7OnPublishFailure;
- opts.context = tc;
- rc = MQTTAsync_send(tc->client, tc->topic, pubmsg.payloadlen, pubmsg.payload,
- pubmsg.qos, pubmsg.retained, &opts);
- assert("Publish successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- }
- void test7OnConnect(void* context, MQTTAsync_successData* response)
- {
- AsyncTestClient* tc = (AsyncTestClient*) context;
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- int rc;
- MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
- opts.onSuccess = test7OnSubscribe;
- opts.context = tc;
- rc = MQTTAsync_subscribe(tc->client, tc->topic, 2, &opts);
- assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- tc->testFinished = 1;
- }
- int test7(struct Options options)
- {
- char* testname = "test7";
- int subsqos = 2;
- AsyncTestClient tc = AsyncTestClient_initializer;
- MQTTAsync c;
- MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
- MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
- MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
- MQTTAsync_disconnectOptions dopts = MQTTAsync_disconnectOptions_initializer;
- int rc = 0;
- char* test_topic = "C client test7";
- int test_finished;
- test_finished = failures = 0;
- MyLog(LOGA_INFO, "Starting test 7 - big messages");
- fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
- global_start_time = start_clock();
- rc = MQTTAsync_create(&c, options.server_auth_connection, "async_test_7", MQTTCLIENT_PERSISTENCE_NONE,
- NULL);
- assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- if (rc != MQTTASYNC_SUCCESS)
- {
- MQTTAsync_destroy(&c);
- goto exit;
- }
- rc = MQTTAsync_setCallbacks(c, &tc, NULL, test7MessageArrived, NULL);
- assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- tc.client = c;
- sprintf(tc.clientid, "%s", testname);
- sprintf(tc.topic, "C client SSL test7");
- tc.maxmsgs = MAXMSGS;
- //tc.rcvdmsgs = 0;
- tc.subscribed = 0;
- tc.testFinished = 0;
- opts.keepAliveInterval = 60;
- opts.cleansession = 1;
- //opts.username = "testuser";
- //opts.password = "testpassword";
- opts.will = &wopts;
- opts.will->message = "will message";
- opts.will->qos = 1;
- opts.will->retained = 0;
- opts.will->topicName = "will topic";
- opts.will = NULL;
- opts.onSuccess = test7OnConnect;
- opts.onFailure = test7OnConnectFailure;
- opts.context = &tc;
- opts.ssl = &sslopts;
- if (options.server_key_file != NULL)
- opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
- if (options.client_key_file != NULL)
- opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
- if (options.client_key_pass != NULL)
- opts.ssl->privateKeyPassword = options.client_key_pass;
- //opts.ssl->enabledCipherSuites = "DEFAULT";
- //opts.ssl->enabledServerCertAuth = 1;
- MyLog(LOGA_DEBUG, "Connecting");
- rc = MQTTAsync_connect(c, &opts);
- rc = 0;
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- while (tc.testFinished == 0 && test7OnUnsubscribed == 0 && test7OnPublishSuccessCount < options.message_count)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(1000L);
- #endif
- dopts.onSuccess = asyncTestOnDisconnect;
- dopts.context = &tc;
- rc = MQTTAsync_disconnect(c, &dopts);
- while (!tc.testFinished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(1000L);
- #endif
- MQTTAsync_destroy(&c);
- if (test7_payload)
- {
- free(test7_payload);
- test7_payload = NULL;
- }
- exit: MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", testname, tests, failures);
- write_test_result();
- return failures;
- }
- /*********************************************************************
- Test8: TLS-PSK - client and server has a common pre-shared key
- *********************************************************************/
- static unsigned int onPSKAuth(const char* hint,
- char* identity,
- unsigned int max_identity_len,
- unsigned char* psk,
- unsigned int max_psk_len,
- void* context)
- {
- unsigned char test_psk[] = {0x50, 0x53, 0x4B, 0x00}; /* {'P', 'S', 'K', '\0' } */
- MyLog(LOGA_DEBUG, "PSK auth callback");
- assert("Good application context in onPSKAuth", context == (void *) 42, "context was %d\n", context);
- strncpy(identity, "id", max_identity_len);
- memcpy(psk, test_psk, sizeof(test_psk));
- return sizeof(test_psk);
- }
- int test8(struct Options options)
- {
- char* testname = "test8";
- AsyncTestClient tc =
- AsyncTestClient_initializer;
- MQTTAsync c;
- MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
- MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
- MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
- int rc = 0;
- failures = 0;
- MyLog(LOGA_INFO, "Starting test 8 - TLS-PSK - client and server has a common pre-shared key");
- fprintf(xml, "<testcase classname=\"test8\" name=\"%s\"", testname);
- global_start_time = start_clock();
- MQTTAsync_create(&c, options.psk_connection, "test8", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
- assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- tc.client = c;
- sprintf(tc.clientid, "%s", testname);
- sprintf(tc.topic, "C client SSL test8");
- tc.maxmsgs = MAXMSGS;
- tc.subscribed = 0;
- tc.testFinished = 0;
- opts.keepAliveInterval = 20;
- opts.cleansession = 1;
- opts.username = "testuser";
- opts.password = "testpassword";
- opts.onSuccess = asyncTestOnConnect;
- opts.onFailure = asyncTestOnSubscribeFailure;
- opts.context = &tc;
- opts.ssl = &sslopts;
- opts.ssl->ssl_psk_cb = onPSKAuth;
- opts.ssl->ssl_psk_context = (void *) 42;
- opts.ssl->enabledCipherSuites = "PSK-AES128-CBC-SHA";
- rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
- asyncTestOnDeliveryComplete);
- assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- MyLog(LOGA_DEBUG, "Connecting");
- rc = MQTTAsync_connect(c, &opts);
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- while (!tc.subscribed && !tc.testFinished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- if (tc.testFinished)
- goto exit;
- while (!tc.testFinished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- MyLog(LOGA_DEBUG, "Stopping");
- exit:
- MQTTAsync_destroy(&c);
- MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", testname, tests, failures);
- write_test_result();
- return failures;
- }
- /*********************************************************************
- Test9: Mutual SSL Authentication - Testing CApath
- *********************************************************************/
- void test9OnConnectFailure(void* context, MQTTAsync_failureData* response)
- {
- AsyncTestClient* client = (AsyncTestClient*) context;
- MyLog(LOGA_DEBUG, "In test9OnConnectFailure callback, %s",
- client->clientid);
- assert("There should be no failures in this test. ", 0, "test9OnConnectFailure callback was called\n", 0);
- client->testFinished = 1;
- }
- int test9(struct Options options)
- {
- char* testname = "test9";
- AsyncTestClient tc =
- AsyncTestClient_initializer;
- MQTTAsync c;
- MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
- MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
- MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
- int rc = 0;
- failures = 0;
- MyLog(LOGA_INFO, "Starting test 9 - Mutual SSL authentication with CApath");
- fprintf(xml, "<testcase classname=\"test5\" name=\"%s\"", testname);
- global_start_time = start_clock();
- MQTTAsync_create(&c, options.mutual_auth_connection, "test9", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
- assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- tc.client = c;
- sprintf(tc.clientid, "%s", testname);
- sprintf(tc.topic, "C client SSL test9");
- tc.maxmsgs = MAXMSGS;
- //tc.rcvdmsgs = 0;
- tc.subscribed = 0;
- tc.testFinished = 0;
- opts.keepAliveInterval = 20;
- opts.cleansession = 1;
- opts.username = "testuser";
- opts.password = "testpassword";
- opts.will = &wopts;
- opts.will->message = "will message";
- opts.will->qos = 1;
- opts.will->retained = 0;
- opts.will->topicName = "will topic";
- opts.will = NULL;
- opts.onSuccess = asyncTestOnConnect;
- opts.onFailure = test9OnConnectFailure;
- opts.context = &tc;
- opts.ssl = &sslopts;
- if (options.server_key_file != NULL)
- opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
- opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
- if (options.client_key_pass != NULL)
- opts.ssl->privateKeyPassword = options.client_key_pass;
- opts.ssl->CApath = options.capath;
- opts.ssl->enableServerCertAuth = 1;
- opts.ssl->verify = 1;
- MyLog(LOGA_DEBUG, "enableServerCertAuth %d\n", opts.ssl->enableServerCertAuth);
- MyLog(LOGA_DEBUG, "verify %d\n", opts.ssl->verify);
- rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
- asyncTestOnDeliveryComplete);
- assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- MyLog(LOGA_DEBUG, "Connecting");
- rc = MQTTAsync_connect(c, &opts);
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- while (!tc.subscribed && !tc.testFinished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- if (tc.testFinished)
- goto exit;
- while (!tc.testFinished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- MyLog(LOGA_DEBUG, "Stopping");
- exit: MQTTAsync_destroy(&c);
- MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", testname, tests, failures);
- write_test_result();
- return failures;
- }
- /*********************************************************************
- Test10: Mutual SSL Authentication - Testing CApath
- *********************************************************************/
- int test10Finished;
- void test10OnConnectFailure(void* context, MQTTAsync_failureData* response)
- {
- AsyncTestClient* client = (AsyncTestClient*) context;
- MyLog(LOGA_DEBUG, "In test10OnConnectFailure callback, %s",
- client->clientid);
- assert("This test should call test10OnConnectFailure. ", 1, "test10OnConnectFailure callback was called\n", 1);
- test10Finished = 1;
- }
- void test10OnConnect(void* context, MQTTAsync_successData* response)
- {
- MyLog(LOGA_DEBUG, "In test10OnConnect callback, context %p", context);
- assert("This connect should not succeed. ", 0, "test10OnConnect callback was called\n", 0);
- test10Finished = 1;
- }
- int test10(struct Options options)
- {
- char* testname = "test10";
- AsyncTestClient tc =
- AsyncTestClient_initializer;
- MQTTAsync c;
- MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
- MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
- MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
- int rc = 0;
- failures = 0;
- test10Finished = 0;
- MyLog(LOGA_INFO, "Starting test 10 - dummy CApath");
- fprintf(xml, "<testcase classname=\"test10\" name=\"%s\"", testname);
- global_start_time = start_clock();
- MQTTAsync_create(&c, options.mutual_auth_connection, "test10", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
- assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- tc.client = c;
- sprintf(tc.clientid, "%s", testname);
- sprintf(tc.topic, "C client SSL test10");
- tc.maxmsgs = MAXMSGS;
- //tc.rcvdmsgs = 0;
- tc.subscribed = 0;
- tc.testFinished = 0;
- opts.keepAliveInterval = 20;
- opts.cleansession = 1;
- opts.username = "testuser";
- opts.password = "testpassword";
- opts.will = &wopts;
- opts.will->message = "will message";
- opts.will->qos = 1;
- opts.will->retained = 0;
- opts.will->topicName = "will topic";
- opts.will = NULL;
- opts.onSuccess = test10OnConnect;
- opts.onFailure = test10OnConnectFailure;
- opts.context = &tc;
- opts.ssl = &sslopts;
- if (options.server_key_file != NULL)
- opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/
- opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
- if (options.client_key_pass != NULL)
- opts.ssl->privateKeyPassword = options.client_key_pass;
- opts.ssl->CApath = "DUMMY";
- opts.ssl->enableServerCertAuth = 1;
- opts.ssl->verify = 1;
- MyLog(LOGA_DEBUG, "enableServerCertAuth %d\n", opts.ssl->enableServerCertAuth);
- MyLog(LOGA_DEBUG, "verify %d\n", opts.ssl->verify);
- rc = MQTTAsync_setCallbacks(c, &tc, NULL, asyncTestMessageArrived,
- asyncTestOnDeliveryComplete);
- assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- MyLog(LOGA_DEBUG, "Connecting");
- rc = MQTTAsync_connect(c, &opts);
- assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
- if (rc != MQTTASYNC_SUCCESS)
- goto exit;
- while (!test10Finished)
- #if defined(_WIN32)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- MyLog(LOGA_DEBUG, "Stopping");
- exit: MQTTAsync_destroy(&c);
- MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", testname, tests, failures);
- write_test_result();
- return failures;
- }
- void handleTrace(enum MQTTASYNC_TRACE_LEVELS level, char* message)
- {
- printf("%s\n", message);
- }
- int main(int argc, char** argv)
- {
- int* numtests = &tests;
- int rc = 0;
- int (*tests[])() =
- { NULL, test1, test2a, test2b, test2c, test2d, test3a, test3b, test4, /* test5a,
- test5b, test5c, */ test6, test7, test8, test9, test10, test2e };
- xml = fopen("TEST-test5.xml", "w");
- fprintf(xml, "<testsuite name=\"test5\" tests=\"%d\">\n", (int)ARRAY_SIZE(tests) - 1);
- MQTTAsync_setTraceCallback(handleTrace);
- getopts(argc, argv);
- if (options.test_no == 0)
- { /* run all the tests */
- for (options.test_no = 1; options.test_no < ARRAY_SIZE(tests); ++options.test_no)
- {
- failures = 0;
- MQTTAsync_setTraceLevel(MQTTASYNC_TRACE_ERROR);
- rc += tests[options.test_no](options); /* return number of failures. 0 = test succeeded */
- }
- }
- else
- {
- MQTTAsync_setTraceLevel(MQTTASYNC_TRACE_ERROR);
- rc = tests[options.test_no](options); /* run just the selected test */
- }
- MyLog(LOGA_INFO, "Total tests run: %d", *numtests);
- if (rc == 0)
- MyLog(LOGA_INFO, "verdict pass");
- else
- MyLog(LOGA_INFO, "verdict fail");
- fprintf(xml, "</testsuite>\n");
- fclose(xml);
- return rc;
- }
- /* Local Variables: */
- /* indent-tabs-mode: t */
- /* c-basic-offset: 8 */
- /* End: */
|