21 #if !defined(_WINDOWS) 23 #include <sys/socket.h> 29 #define MAXHOSTNAMELEN 256 30 #define EAGAIN WSAEWOULDBLOCK 31 #define EINTR WSAEINTR 32 #define EINPROGRESS WSAEINPROGRESS 33 #define EWOULDBLOCK WSAEWOULDBLOCK 34 #define ENOTCONN WSAENOTCONN 35 #define ECONNRESET WSAECONNRESET 38 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) 41 char*
topics[] = {
"TopicA",
"TopicA/B",
"Topic/C",
"TopicA/C",
"/TopicA"};
42 char*
wildtopics[] = {
"TopicA/+",
"+/C",
"#",
"/#",
"/+",
"+/+",
"TopicA/#"};
59 "tcp://localhost:1883",
74 printf(
"options:\n connection, clientid1, clientid2, username, password, MQTTversion, iterations, verbose\n");
84 if (strcmp(argv[count],
"--dollar_topics_test") == 0 || strcmp(argv[count],
"--$") == 0)
87 printf(
"Running $ topics test\n");
89 else if (strcmp(argv[count],
"--subscribe_failure_test") == 0 || strcmp(argv[count],
"-s") == 0)
92 printf(
"Running subscribe failure test\n");
94 else if (strcmp(argv[count],
"--connection") == 0)
104 else if (strcmp(argv[count],
"--clientid1") == 0)
114 else if (strcmp(argv[count],
"--clientid2") == 0)
124 else if (strcmp(argv[count],
"--username") == 0)
134 else if (strcmp(argv[count],
"--password") == 0)
144 else if (strcmp(argv[count],
"--MQTTversion") == 0)
154 else if (strcmp(argv[count],
"--iterations") == 0)
164 else if (strcmp(argv[count],
"--verbose") == 0)
167 printf(
"\nSetting verbose on\n");
174 #if defined(_WIN32) || defined(_WINDOWS) 176 #define START_TIME_TYPE DWORD 177 static DWORD start_time = 0;
180 return GetTickCount();
183 #define mqsleep sleep 184 #define START_TIME_TYPE struct timespec 187 static struct timespec start;
188 clock_gettime(CLOCK_REALTIME, &start);
192 #define msleep(A) usleep(A*1000) 193 #define START_TIME_TYPE struct timeval 197 struct timeval start_time;
198 gettimeofday(&start_time, NULL);
207 #include <sys/timeb.h> 221 strftime(msg_buf, 80,
"%Y%m%d %H%M%S", timeinfo);
223 sprintf(&msg_buf[strlen(msg_buf)],
".%.3hu ", ts.millitm);
225 va_start(args, format);
226 vsnprintf(&msg_buf[strlen(msg_buf)],
sizeof(msg_buf) - strlen(msg_buf), format, args);
229 printf(
"%s\n", msg_buf);
247 printf(
"Assertion failed, file %s, line %d, description: %s\n", filename, lineno, description);
249 va_start(args, format);
250 count = vprintf(format, args);
259 MyLog(
LOGA_DEBUG,
"Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description);
263 #define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d) 264 #define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e) 281 MyLog(
LOGA_DEBUG,
"Callback: %d message received on topic %s is %.*s.",
315 for (i = 0; i < 2; ++i)
349 if (messagesArrived[i].m->retained)
351 MyLog(
LOGA_INFO,
"Deleting retained message for topic %s", (
char*)messagesArrived[i].topicName);
559 assert(
"messages should be retained", messagesArrived[i].m->retained,
"retained was %d",
588 assert(
"0 messages received", messageCount == 0,
"messageCount was %d", messageCount);
596 #define SOCKET_ERROR -1 605 errno = WSAGetLastError();
607 if (errno != EINTR && errno != EAGAIN && errno != EINPROGRESS && errno != EWOULDBLOCK)
609 if (strcmp(aString,
"shutdown") != 0 || (errno != ENOTCONN && errno != ECONNRESET))
610 printf(
"Socket error %d in %s for socket %d", errno, aString, sock);
649 unsigned int cleansession : 1;
651 unsigned int good : 1;
652 unsigned int ping_outstanding : 1;
653 int connect_state : 4;
753 int qosList[] = {2, 1};
779 "overlapping topic filters", 2, 0, NULL);
788 MyLog(
LOGA_INFO,
"This server is publishing one message for all matching overlapping subscriptions, not one for each.");
789 assert(
"QoS should be 2", messagesArrived[0].m->qos == 2,
"QoS was %d", messagesArrived[0].
m->
qos);
793 MyLog(
LOGA_INFO,
"This server is publishing one message per each matching overlapping subscription.");
794 assert1(
"QoSs should be 1 and 2",
795 (messagesArrived[0].m->qos == 2 && messagesArrived[1].
m->
qos == 1) ||
796 (messagesArrived[0].
m->
qos == 1 && messagesArrived[1].
m->
qos == 2),
797 "QoSs were %d %d", messagesArrived[0].
m->
qos, messagesArrived[1].
m->
qos);
956 assert(
"rc 2 from connect", rc == 2,
"rc was %d", rc);
962 MyLog(
LOGA_INFO,
"This server %s support zero length clientids", (rc == 2) ?
"does not" :
"does");
983 char dollartopic[20];
987 sprintf(dollartopic,
"$%s",
topics[1]);
1015 rc =
MQTTClient_publish(aclient, dollartopic, 20,
"sent to dollar topic", 1, 0, NULL);
1062 assert(
"0x80 rc from subscribe", subqos == 0x80,
"subqos was %d", subqos);
1077 int all_failures = 0;
1100 MyLog(
LOGA_INFO,
"Test suite %s", (all_failures == 0) ?
"succeeded" :
"failed");
enum MQTTPropertyCodes value
int overlapping_subscriptions_test(void)
FMT_INLINE std::basic_string< Char > format(const S &format_str, Args &&...args)
#define MQTTCLIENT_SUCCESS
void getopts(int argc, char **argv)
int MQTTClient_connect(MQTTClient handle, MQTTClient_connectOptions *options)
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)
int messageArrived(void *context, char *topicName, int topicLen, MQTTClient_message *m)
int retained_message_test(void)
int offline_message_queueing_test(void)
int test6_socket_error(char *aString, int sock)
size_t strftime(char *str, size_t count, const char *format, const std::tm *time)
std::tm localtime(std::time_t time)
void MyLog(int LOGA_level, char *format,...)
#define MQTTClient_willOptions_initializer
int MQTTClient_setCallbacks(MQTTClient handle, void *context, MQTTClient_connectionLost *cl, MQTTClient_messageArrived *ma, MQTTClient_deliveryComplete *dc)
char * nosubscribe_topics[]
int MQTTClient_unsubscribe(MQTTClient handle, const char *topic)
#define MQTTVERSION_3_1_1
void MQTTClient_freeMessage(MQTTClient_message **message)
int main(int argc, char **argv)
int subscribe_failure_test(void)
void MQTTClient_yield(void)
int zero_length_clientid_test(void)
void MQTTClient_destroy(MQTTClient *handle)
int dollar_topics_test(void)
#define MQTTCLIENT_PERSISTENCE_DEFAULT
void MQTTClient_deliveryComplete(void *context, MQTTClient_deliveryToken dt)
void myassert(char *filename, int lineno, char *description, int value, char *format,...)
messageStruct messagesArrived[1000]
#define assert(a, b, c, d)
int MQTTClient_subscribe(MQTTClient handle, const char *topic, int qos)
int test6_socket_close(int socket)
void MQTTClient_free(void *memory)
#define MQTTClient_connectOptions_initializer
int MQTTClient_subscribeMany(MQTTClient handle, int count, char *const *topic, int *qos)
int run_subscribe_failure_test
int will_message_test(void)
#define assert1(a, b, c, d, e)
int run_dollar_topics_test
int redelivery_on_reconnect_test(void)
int MQTTClient_create(MQTTClient *handle, const char *serverURI, const char *clientId, int persistence_type, void *persistence_context)
MQTTClient_willOptions * will
START_TIME_TYPE start_clock(void)