30 #define MAXHOSTNAMELEN 256 31 #define snprintf _snprintf 32 #define setenv(a, b, c) _putenv_s(a, b) 35 #include <sys/socket.h> 52 #include <sys/timeb.h> 54 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) 59 printf(
"\t--test_no <test_no> - Run test number <test_no>\n");
60 printf(
"\t--connection <mqtt URL> - Connect to <mqtt URL> for tests\n");
61 printf(
"\t--haconnections \"<mqtt URLs>\" - Use \"<mqtt URLs>\" as the list of servers for HA tests (space separated)\n");
62 printf(
"\t--client_key <key_file> - Use <key_file> as the client certificate for SSL authentication\n");
63 printf(
"\t--client_key_pass <password> - Use <password> to access the private key in the client certificate\n");
64 printf(
"\t--client_privatekey <file> - Client private key file if not in certificate file\n");
65 printf(
"\t--server_key <key_file> - Use <key_file> as the trusted certificate for server\n");
66 printf(
"\t--verbose - Enable verbose output \n");
67 printf(
"\tserver connection URLs should be in the form; (tcp|ssl)://hostname:port\n");
68 printf(
"\t--help - This help output\n");
91 "ssl://m2m.eclipse.org:18883",
92 "ssl://m2m.eclipse.org:18884",
93 "ssl://m2m.eclipse.org:18887",
94 "ssl://m2m.eclipse.org:18885",
95 "ssl://m2m.eclipse.org:18886",
96 "ssl://m2m.eclipse.org:18888",
99 "../../../test/ssl/client.pem",
101 "../../../test/ssl/test-root-ca.crt",
134 if (strcmp(argv[count],
"--help") == 0)
136 else if (strcmp(argv[count],
"--test_no") == 0)
144 if (strcmp(argv[count],
test_map[i]) == 0)
157 else if (strcmp(argv[count],
"--hostname") == 0)
168 printf(
"Setting nocert_mutual_auth_connection to %s\n",
180 else if (strcmp(argv[count],
"--haconnections") == 0)
184 char* tok = strtok(argv[count],
" ");
192 tok = strtok(NULL,
" ");
198 else if (strcmp(argv[count],
"--client_key") == 0)
203 strcat(ckeytmp, getenv(
"HOME"));
204 strcat(ckeytmp, argv[count]);
213 else if (strcmp(argv[count],
"--client_privatekey") == 0)
218 strcat(ckeytmp, getenv(
"HOME"));
219 strcat(ckeytmp, argv[count]);
228 else if (strcmp(argv[count],
"--client_key_pass") == 0)
235 else if (strcmp(argv[count],
"--server_key") == 0)
240 strcat(skeytmp, getenv(
"HOME"));
241 strcat(skeytmp, argv[count]);
250 else if (strcmp(argv[count],
"--verbose") == 0)
253 printf(
"\nSetting verbose on\n");
255 else if (strcmp(argv[count],
"--ws") == 0)
258 printf(
"\nSetting websockets on\n");
272 #if defined(_WIN32) || defined(_WINDOWS) 282 #if defined(_WIN32) || defined(_WINDOWS) 286 gettimeofday(&ts, NULL);
287 localtime_r(&ts.tv_sec, &timeinfo);
289 strftime(msg_buf, 80,
"%Y%m%d %H%M%S", &timeinfo);
291 #if defined(_WIN32) || defined(_WINDOWS) 292 sprintf(&msg_buf[strlen(msg_buf)],
".%.3hu ", ts.millitm);
294 sprintf(&msg_buf[strlen(msg_buf)],
".%.3lu ", ts.tv_usec / 1000);
297 va_start(args, format);
298 vsnprintf(&msg_buf[strlen(msg_buf)],
sizeof(msg_buf) - strlen(msg_buf), format, args);
301 printf(
"%s\n", msg_buf);
306 #if defined(_WIN32) || defined(_WINDOWS) 307 #define mqsleep(A) Sleep(1000*A) 308 #define START_TIME_TYPE DWORD 309 static DWORD start_time = 0;
312 return GetTickCount();
315 #define mqsleep sleep 316 #define START_TIME_TYPE struct timespec 319 static struct timespec start;
320 clock_gettime(CLOCK_REALTIME, &start);
324 #define mqsleep sleep 325 #define START_TIME_TYPE struct timeval 329 struct timeval start_time;
330 gettimeofday(&start_time, NULL);
339 return GetTickCount() - start_time;
343 long elapsed(
struct timespec start)
345 struct timespec now, res;
347 clock_gettime(CLOCK_REALTIME, &now);
348 ntimersub(now, start, res);
349 return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L;
354 struct timeval now, res;
356 gettimeofday(&now, NULL);
357 timersub(&now, &start_time, &res);
358 return (res.tv_sec)*1000 + (res.tv_usec)/1000;
363 #define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d) 364 #define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e) 379 fprintf(
xml,
" time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000);
385 fprintf(
xml,
"</testcase>\n");
397 printf(
"Assertion failed, file %s, line %d, description: %s\n", filename, lineno, description);
399 va_start(args, format);
400 vprintf(format, args);
404 description, filename, lineno);
407 MyLog(
LOGA_DEBUG,
"Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description);
422 char* topicName = NULL;
429 pubmsg.
payload =
"a much longer message that we can shorten to the extent that we need to payload up to 11";
464 printf(
"No message received within timeout period\n");
471 printf(
"Message received on topic %s is %.*s.\n", topicName, m->
payloadlen, (
char*)(m->
payload));
495 MyLog(
LOGA_DEBUG,
"Callback: %d message received on topic %s is %.*s.",
515 int wait_seconds = 0;
521 multiThread_pubmsg.
payload =
"a much longer message that we can shorten to the extent that we need to";
523 multiThread_pubmsg.
qos =
qos;
530 multiThread_pubmsg.
qos, multiThread_pubmsg.
retained, NULL);
551 assert(
"Message Arrived", wait_seconds > 0,
552 "Time out waiting for message %d\n", i );
570 assert(
"All Deliveries Complete", wait_seconds > 0,
571 "Number of deliveryCompleted callbacks was %d\n",
584 char* testname =
"test1";
594 MyLog(
LOGA_INFO,
"Starting SSL test 1 - connection to nonSSL MQTT server");
595 fprintf(
xml,
"<testcase classname=\"test3\" name=\"SSL connect fail to nonSSL MQTT server\"");
646 char* testname =
"test2a_s";
656 MyLog(
LOGA_INFO,
"Starting test 2a_s - Mutual SSL authentication - single threaded client using receive");
657 fprintf(
xml,
"<testcase classname=\"test3\" name=\"test 2a_s\"");
730 char* testname =
"test2a_m";
741 MyLog(
LOGA_INFO,
"Starting test 2a_m - Mutual SSL authentication - multi-threaded client using callbacks");
742 fprintf(
xml,
"<testcase classname=\"test3\" name=\"test 2a_m\"");
812 char* testname =
"test2b";
822 MyLog(
LOGA_INFO,
"Starting test 2b - connection to SSL MQTT server with clientauth=req but server does not have client cert");
823 fprintf(
xml,
"<testcase classname=\"test3\" name=\"test 2b\"");
872 char* testname =
"test2c";
882 MyLog(
LOGA_INFO,
"Starting test 2c - connection to SSL MQTT server, server auth enabled but unknown cert");
883 fprintf(
xml,
"<testcase classname=\"test3\" name=\"test 2c\"");
933 char* testname =
"test2e_s";
944 MyLog(
LOGA_INFO,
"Starting test 2e_s - Mutual SSL authentication - HA");
945 fprintf(
xml,
"<testcase classname=\"test3\" name=\"test 2e_s\"");
1016 char* testname =
"test3a_s";
1026 MyLog(
LOGA_INFO,
"Starting test 3a_s - Server authentication - single threaded client using receive");
1027 fprintf(
xml,
"<testcase classname=\"test3\" name=\"test 3a_s\"");
1044 opts.
ssl = &sslopts;
1095 char* testname =
"test3a_m";
1105 MyLog(
LOGA_INFO,
"Starting test 3a_m - Server authentication - multi-threaded client using callbacks");
1106 fprintf(
xml,
"<testcase classname=\"test3\" name=\"test 3a_m\"");
1123 opts.
ssl = &sslopts;
1169 char* testname =
"test3b";
1179 MyLog(
LOGA_INFO,
"Starting test 3b - connection to SSL MQTT server with clientauth=opt but client does not have server cert");
1180 fprintf(
xml,
"<testcase classname=\"test3\" name=\"test 3b\"");
1197 opts.
ssl = &sslopts;
1221 char* testname =
"test4_s";
1231 MyLog(
LOGA_INFO,
"Starting test 4_s - accept invalid server certificates - single threaded");
1232 fprintf(
xml,
"<testcase classname=\"test3\" name=\"test 4_s\"");
1249 opts.
ssl = &sslopts;
1298 char* testname =
"test4_m";
1308 MyLog(
LOGA_INFO,
"Starting test 4_m - accept invalid server certificates - multi-threaded");
1309 fprintf(
xml,
"<testcase classname=\"test3\" name=\"test 4_m\"");
1326 opts.
ssl = &sslopts;
1372 char* testname =
"test5a";
1382 MyLog(
LOGA_INFO,
"Starting SSL test 5a - Anonymous ciphers - server authentication disabled");
1383 fprintf(
xml,
"<testcase classname=\"test3\" name=\"test 5a\"");
1400 opts.
ssl = &sslopts;
1451 char* testname =
"test5b";
1461 MyLog(
LOGA_INFO,
"Starting SSL test 5b - Anonymous ciphers - server authentication enabled");
1462 fprintf(
xml,
"<testcase classname=\"test3\" name=\"test 5b\"");
1479 opts.
ssl = &sslopts;
1533 char* testname =
"test5c";
1543 MyLog(
LOGA_INFO,
"Starting SSL test 5c - Anonymous ciphers - client not using anonymous cipher");
1544 fprintf(
xml,
"<testcase classname=\"test3\" name=\"test 5c\"");
1561 opts.
ssl = &sslopts;
1590 unsigned int max_identity_len,
1592 unsigned int max_psk_len,
1595 unsigned char test_psk[] = {0x50, 0x53, 0x4B, 0x00};
1598 if (!(
assert(
"Good application context in onPSKAuth", context == (
void *) 42,
"context was %d\n", context)))
1601 strncpy(identity,
"id", max_identity_len);
1602 memcpy(psk, test_psk,
sizeof(test_psk));
1603 return sizeof(test_psk);
1609 char* testname =
"test6";
1617 MyLog(
LOGA_INFO,
"Starting test 6 - TLS-PSK - client and server has a common pre-shared key");
1618 fprintf(
xml,
"<testcase classname=\"test6\" name=\"test 6\"");
1635 opts.
ssl = &sslopts;
1665 unsigned int cleansession : 1;
1667 unsigned int good : 1;
1668 unsigned int ping_outstanding : 1;
1669 unsigned int connect_state : 2;
1672 int keepAliveInterval;
1674 int maxInflightMessages;
1705 int* numtests = &
tests;
1707 int (*
tests[])() = {NULL,
test1,
test2a_s,
test2a_m,
test2b,
test2c,
test3a_s,
test3a_m,
test3b,
test4_s,
test4_m,
test6,
1711 xml = fopen(
"TEST-test3.xml",
"w");
1712 fprintf(
xml,
"<testsuite name=\"test3\" tests=\"%d\">\n", (
int)(
ARRAY_SIZE(
tests) - 1));
1714 setenv(
"MQTT_C_CLIENT_TRACE",
"ON", 1);
1715 setenv(
"MQTT_C_CLIENT_TRACE_LEVEL",
"ERROR", 1);
1731 fprintf(
xml,
"</testsuite>\n");
char nocert_mutual_auth_connection[100]
int MQTTClient_waitForCompletion(MQTTClient handle, MQTTClient_deliveryToken mdt, unsigned long timeout)
enum MQTTPropertyCodes value
#define MQTTCLIENT_FAILURE
int MQTTClient_receive(MQTTClient handle, char **topicName, int *topicLen, MQTTClient_message **message, unsigned long timeout)
FMT_INLINE std::basic_string< Char > format(const S &format_str, Args &&...args)
#define MQTTCLIENT_SUCCESS
char * client_private_key_file
int MQTTClient_connect(MQTTClient handle, MQTTClient_connectOptions *options)
void MyLog(int LOGA_level, char *format,...)
int MQTTClient_publish(MQTTClient handle, const char *topicName, int payloadlen, const void *payload, int qos, int retained, MQTTClient_deliveryToken *deliveryToken)
int MQTTClient_messageArrived(void *context, char *topicName, int topicLen, MQTTClient_message *message)
void MQTTClient_connectionLost(void *context, char *cause)
int MQTTClient_disconnect(MQTTClient handle, int timeout)
const char * enabledCipherSuites
int multiThread_messageArrived(void *context, char *topicName, int topicLen, MQTTClient_message *m)
size_t strftime(char *str, size_t count, const char *format, const std::tm *time)
int myassert(char *filename, int lineno, char *description, int value, char *format,...)
int test3b(struct Options options)
#define MQTTClient_message_initializer
#define MQTTClient_willOptions_initializer
int MQTTClient_setCallbacks(MQTTClient handle, void *context, MQTTClient_connectionLost *cl, MQTTClient_messageArrived *ma, MQTTClient_deliveryComplete *dc)
int test6(struct Options options)
MQTTClient_SSLOptions * ssl
int MQTTClient_publishMessage(MQTTClient handle, const char *topicName, MQTTClient_message *message, MQTTClient_deliveryToken *deliveryToken)
void write_test_result(void)
#define assert(a, b, c, d)
START_TIME_TYPE global_start_time
unsigned int(* ssl_psk_cb)(const char *hint, char *identity, unsigned int max_identity_len, unsigned char *psk, unsigned int max_psk_len, void *u)
#define MQTTCLIENT_BAD_PROTOCOL
int MQTTClient_unsubscribe(MQTTClient handle, const char *topic)
char mutual_auth_connection[100]
int test4_s(struct Options options)
int test1(struct Options options)
#define MQTTCLIENT_NULL_PARAMETER
volatile int multiThread_arrivedcount
int test2e_s(struct Options options)
int connectOptionsVersion
void MQTTClient_freeMessage(MQTTClient_message **message)
int test3a_m(struct Options options)
void MQTTClient_destroy(MQTTClient *handle)
#define MQTTCLIENT_PERSISTENCE_DEFAULT
void MQTTClient_deliveryComplete(void *context, MQTTClient_deliveryToken dt)
int test2b(struct Options options)
START_TIME_TYPE start_clock(void)
int main(int argc, char **argv)
char server_auth_connection[100]
int test5a(struct Options options)
int MQTTClient_subscribe(MQTTClient handle, const char *topic, int qos)
int test2c(struct Options options)
void MQTTClient_free(void *memory)
#define MQTTClient_connectOptions_initializer
void multiThread_sendAndReceive(MQTTClient *c, int qos, char *test_topic)
int MQTTClient_deliveryToken
int test5b(struct Options options)
long elapsed(START_TIME_TYPE start_time)
void multiThread_deliveryComplete(void *context, MQTTClient_deliveryToken dt)
A structure containing the function pointers to a persistence implementation and the context or state...
int test2a_s(struct Options options)
#define MQTTClient_SSLOptions_initializer
int multiThread_deliveryCompleted
static unsigned int onPSKAuth(const char *hint, char *identity, unsigned int max_identity_len, unsigned char *psk, unsigned int max_psk_len, void *context)
const char * privateKeyPassword
int test3a_s(struct Options options)
MQTTClient_message multiThread_pubmsg
int test5c(struct Options options)
void getopts(int argc, char **argv)
int MQTTClient_create(MQTTClient *handle, const char *serverURI, const char *clientId, int persistence_type, void *persistence_context)
void singleThread_sendAndReceive(MQTTClient *c, int qos, char *test_topic)
int test2a_m(struct Options options)
char anon_connection[100]
int test4_m(struct Options options)