Add full test1 tests for V5

This commit is contained in:
Ian Craggs 2018-05-31 17:10:34 +01:00
parent 1b61a3bf1f
commit a892672057
10 changed files with 1751 additions and 180 deletions

View File

@ -801,7 +801,7 @@ static void MQTTClient_closeSession(Clients* client, enum MQTTReasonCodes reason
client->connected = 0;
client->connect_state = 0;
if (client->cleansession)
if (client->MQTTVersion < MQTTVERSION_5 && client->cleansession)
MQTTClient_cleanSession(client);
FUNC_EXIT;
}
@ -1551,7 +1551,13 @@ exit:
int MQTTClient_subscribeMany(MQTTClient handle, int count, char* const* topic, int* qos)
{
MQTTResponse response = MQTTClient_subscribeMany5(handle, count, topic, qos, NULL, NULL);
MQTTClients* m = handle;
MQTTResponse response = {MQTTCLIENT_SUCCESS, NULL};
if (m->c->MQTTVersion >= MQTTVERSION_5)
response.reasonCode = MQTTCLIENT_BAD_MQTT_VERSION;
else
response = MQTTClient_subscribeMany5(handle, count, topic, qos, NULL, NULL);
return response.reasonCode;
}
@ -1575,7 +1581,13 @@ MQTTResponse MQTTClient_subscribe5(MQTTClient handle, const char* topic, int qos
int MQTTClient_subscribe(MQTTClient handle, const char* topic, int qos)
{
MQTTResponse response = MQTTClient_subscribe5(handle, topic, qos, NULL, NULL);
MQTTClients* m = handle;
MQTTResponse response = {MQTTCLIENT_SUCCESS, NULL};
if (m->c->MQTTVersion >= MQTTVERSION_5)
response.reasonCode = MQTTCLIENT_BAD_MQTT_VERSION;
else
response = MQTTClient_subscribe5(handle, topic, qos, NULL, NULL);
return response.reasonCode;
}
@ -1698,8 +1710,6 @@ MQTTResponse MQTTClient_publish5(MQTTClient handle, const char* topicName, int p
rc = MQTTCLIENT_DISCONNECTED;
else if (!UTF8_validateString(topicName))
rc = MQTTCLIENT_BAD_UTF8_STRING;
else if (m->c->MQTTVersion >= MQTTVERSION_5 && properties == NULL)
rc = MQTTCLIENT_NULL_PARAMETER;
if (rc != MQTTCLIENT_SUCCESS)
goto exit;
@ -1738,7 +1748,15 @@ MQTTResponse MQTTClient_publish5(MQTTClient handle, const char* topicName, int p
p->msgId = msgid;
p->MQTTVersion = m->c->MQTTVersion;
if (m->c->MQTTVersion >= MQTTVERSION_5)
p->properties = *properties;
{
if (properties)
p->properties = *properties;
else
{
MQTTProperties props = MQTTProperties_initializer;
p->properties = props;
}
}
rc = MQTTProtocol_startPublish(m->c, p, qos, retained, &msg);
@ -1781,7 +1799,13 @@ exit:
int MQTTClient_publish(MQTTClient handle, const char* topicName, int payloadlen, void* payload,
int qos, int retained, MQTTClient_deliveryToken* deliveryToken)
{
MQTTResponse rc = MQTTClient_publish5(handle, topicName, payloadlen, payload, qos, retained, NULL, deliveryToken);
MQTTClients* m = handle;
MQTTResponse rc = {MQTTCLIENT_SUCCESS, NULL};
if (m->c->MQTTVersion >= MQTTVERSION_5)
rc.reasonCode = MQTTCLIENT_BAD_MQTT_VERSION;
else
rc = MQTTClient_publish5(handle, topicName, payloadlen, payload, qos, retained, NULL, deliveryToken);
return rc.reasonCode;
}
@ -1820,13 +1844,16 @@ exit:
int MQTTClient_publishMessage(MQTTClient handle, const char* topicName, MQTTClient_message* message,
MQTTClient_deliveryToken* deliveryToken)
{
MQTTClients* m = handle;
MQTTResponse rc = {MQTTCLIENT_SUCCESS, NULL};
if (strncmp(message->struct_id, "MQTM", 4) != 0 ||
(message->struct_version != 0 && message->struct_version != 1))
return MQTTCLIENT_BAD_STRUCTURE;
rc = MQTTClient_publishMessage5(handle, topicName, message, deliveryToken);
rc.reasonCode = MQTTCLIENT_BAD_STRUCTURE;
else if (m->c->MQTTVersion >= MQTTVERSION_5)
rc.reasonCode = MQTTCLIENT_BAD_MQTT_VERSION;
else
rc = MQTTClient_publishMessage5(handle, topicName, message, deliveryToken);
return rc.reasonCode;
}

View File

@ -712,6 +712,7 @@ typedef struct
* MQTTVERSION_DEFAULT (0) = default: start with 3.1.1, and if that fails, fall back to 3.1
* MQTTVERSION_3_1 (3) = only try version 3.1
* MQTTVERSION_3_1_1 (4) = only try version 3.1.1
* MQTTVERSION_5 (5) = only try version 5.0
*/
int MQTTVersion;
/**
@ -726,8 +727,9 @@ typedef struct
/**
* Optional binary password. Only checked and used if the password option is NULL
*/
struct {
int len; /**< binary password length */
struct
{
int len; /**< binary password length */
const void* data; /**< binary password data */
} binarypwd;
} MQTTClient_connectOptions;

View File

@ -110,8 +110,44 @@ ADD_TEST(
COMMAND "test15" "--test_no" "1" "--connection" ${MQTT_TEST_BROKER} "--proxy_connection" ${MQTT_TEST_PROXY}
)
ADD_TEST(
NAME test15-2-multithread-callbacks
COMMAND "test15" "--test_no" "2" "--connection" ${MQTT_TEST_BROKER} "--proxy_connection" ${MQTT_TEST_PROXY}
)
ADD_TEST(
NAME test15-3-connack-return-codes
COMMAND "test15" "--test_no" "3" "--connection" ${MQTT_TEST_BROKER} "--proxy_connection" ${MQTT_TEST_PROXY}
)
ADD_TEST(
NAME test15-4-client-persistence
COMMAND "test15" "--test_no" "4" "--connection" ${MQTT_TEST_BROKER} "--proxy_connection" ${MQTT_TEST_PROXY}
)
ADD_TEST(
NAME test15-5-disconnect-with-quiesce
COMMAND "test15" "--test_no" "5" "--connection" ${MQTT_TEST_BROKER} "--proxy_connection" ${MQTT_TEST_PROXY}
)
ADD_TEST(
NAME test15-6-connlost-will-message
COMMAND "test15" "--test_no" "6" "--connection" ${MQTT_TEST_BROKER} "--proxy_connection" ${MQTT_TEST_PROXY}
)
ADD_TEST(
NAME test15-7-connlost-binary-will-message
COMMAND "test15" "--test_no" "7" "--connection" ${MQTT_TEST_BROKER} "--proxy_connection" ${MQTT_TEST_PROXY}
)
SET_TESTS_PROPERTIES(
test15-1-single-thread-client
test15-2-multithread-callbacks
test15-3-connack-return-codes
test15-4-client-persistence
test15-5-disconnect-with-quiesce
test15-6-connlost-will-message
test15-7-connlost-binary-will-message
PROPERTIES TIMEOUT 540
)

View File

@ -1,16 +1,16 @@
"""
*******************************************************************
Copyright (c) 2013, 2014 IBM Corp.
Copyright (c) 2013, 2018 IBM Corp.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
http://www.eclipse.org/legal/epl-v10.html
and the Eclipse Distribution License is available at
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
Contributors:
Ian Craggs - initial implementation and/or documentation
*******************************************************************
@ -31,7 +31,7 @@ logger = logging.getLogger("mqttsas")
class MQTTException(Exception):
pass
# Message types
CONNECT, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL, \
@ -106,10 +106,10 @@ class FixedHeaders:
self.RETAIN == fh.RETAIN # and \
# self.remainingLength == fh.remainingLength
def __repr__(self):
"return printable representation of our data"
return classNames[self.MessageType]+'(DUP='+repr(self.DUP)+ \
", QoS="+repr(self.QoS)+", Retain="+repr(self.RETAIN)
def __str__(self):
"return printable stresentation of our data"
return classNames[self.MessageType]+'(DUP='+str(self.DUP)+ \
", QoS="+str(self.QoS)+", Retain="+str(self.RETAIN)
def pack(self, length):
"pack data into string buffer ready for transmission down socket"
@ -185,7 +185,7 @@ def readUTF(buffer, maxlen):
if zz != -1:
raise MQTTException("[MQTT-1.5.3-1] D800-DFFF found in UTF data "+buf)
if buf.find("\uFEFF") != -1:
logger.info("[MQTT-1.5.3-3] U+FEFF in UTF string")
logger.info("[MQTT-1.5.3-3] U+FEFF in UTF string")
return buf
def writeBytes(buffer):
@ -202,8 +202,8 @@ class Packets:
buffer = self.fh.pack(0)
return buffer
def __repr__(self):
return repr(self.fh)
def __str__(self):
return str(self.fh)
def __eq__(self, packet):
return self.fh == packet.fh if packet else False
@ -235,20 +235,20 @@ class Connects(Packets):
if buffer != None:
self.unpack(buffer)
def pack(self):
def pack(self):
connectFlags = bytes([(self.CleanSession << 1) | (self.WillFlag << 2) | \
(self.WillQoS << 3) | (self.WillRETAIN << 5) | \
(self.usernameFlag << 6) | (self.passwordFlag << 7)])
buffer = writeUTF(self.ProtocolName) + bytes([self.ProtocolVersion]) + \
connectFlags + writeInt16(self.KeepAliveTimer)
buffer += writeUTF(self.ClientIdentifier)
buffer += writeUTF(self.ClientIdentifier)
if self.WillFlag:
buffer += writeUTF(self.WillTopic)
buffer += writeBytes(self.WillMessage)
buffer += writeUTF(self.WillTopic)
buffer += writeBytes(self.WillMessage)
if self.usernameFlag:
buffer += writeUTF(self.username)
buffer += writeUTF(self.username)
if self.passwordFlag:
buffer += writeBytes(self.password)
buffer += writeBytes(self.password)
buffer = self.fh.pack(len(buffer)) + buffer
return buffer
@ -331,15 +331,15 @@ class Connects(Packets):
def __repr__(self):
buf = repr(self.fh)+", ProtocolName="+str(self.ProtocolName)+", ProtocolVersion=" +\
repr(self.ProtocolVersion)+", CleanSession="+repr(self.CleanSession) +\
", WillFlag="+repr(self.WillFlag)+", KeepAliveTimer=" +\
repr(self.KeepAliveTimer)+", ClientId="+str(self.ClientIdentifier) +\
", usernameFlag="+repr(self.usernameFlag)+", passwordFlag="+repr(self.passwordFlag)
def __str__(self):
buf = str(self.fh)+", ProtocolName="+str(self.ProtocolName)+", ProtocolVersion=" +\
str(self.ProtocolVersion)+", CleanSession="+str(self.CleanSession) +\
", WillFlag="+str(self.WillFlag)+", KeepAliveTimer=" +\
str(self.KeepAliveTimer)+", ClientId="+str(self.ClientIdentifier) +\
", usernameFlag="+str(self.usernameFlag)+", passwordFlag="+str(self.passwordFlag)
if self.WillFlag:
buf += ", WillQoS=" + repr(self.WillQoS) +\
", WillRETAIN=" + repr(self.WillRETAIN) +\
buf += ", WillQoS=" + str(self.WillQoS) +\
", WillRETAIN=" + str(self.WillRETAIN) +\
", WillTopic='"+ self.WillTopic +\
"', WillMessage='"+str(self.WillMessage)+"'"
if self.username:
@ -393,8 +393,8 @@ class Connacks(Packets):
assert self.fh.QoS == 0, "[MQTT-2.1.2-1]"
assert self.fh.RETAIN == False, "[MQTT-2.1.2-1]"
def __repr__(self):
return repr(self.fh)+", Session present="+str((self.flags & 0x01) == 1)+", ReturnCode="+repr(self.returnCode)+")"
def __str__(self):
return str(self.fh)+", Session present="+str((self.flags & 0x01) == 1)+", ReturnCode="+str(self.returnCode)+")"
def __eq__(self, packet):
return Packets.__eq__(self, packet) and \
@ -421,8 +421,8 @@ class Disconnects(Packets):
assert self.fh.QoS == 0, "[MQTT-2.1.2-1]"
assert self.fh.RETAIN == False, "[MQTT-2.1.2-1]"
def __repr__(self):
return repr(self.fh)+")"
def __str__(self):
return str(self.fh)+")"
class Publishes(Packets):
@ -475,11 +475,11 @@ class Publishes(Packets):
assert self.fh.DUP == False, "[MQTT-2.1.2-4]"
return fhlen + self.fh.remainingLength
def __repr__(self):
rc = repr(self.fh)
def __str__(self):
rc = str(self.fh)
if self.fh.QoS != 0:
rc += ", MsgId="+repr(self.messageIdentifier)
rc += ", TopicName="+repr(self.topicName)+", Payload="+repr(self.data)+")"
rc += ", MsgId="+str(self.messageIdentifier)
rc += ", TopicName="+str(self.topicName)+", Payload="+str(self.data)+")"
return rc
def __eq__(self, packet):
@ -520,8 +520,8 @@ class Pubacks(Packets):
assert self.fh.RETAIN == False, "[MQTT-2.1.2-1] Puback reserved bits must be 0"
return fhlen + 2
def __repr__(self):
return repr(self.fh)+", MsgId "+repr(self.messageIdentifier)
def __str__(self):
return str(self.fh)+", MsgId "+str(self.messageIdentifier)
def __eq__(self, packet):
return Packets.__eq__(self, packet) and \
@ -557,8 +557,8 @@ class Pubrecs(Packets):
assert self.fh.RETAIN == False, "[MQTT-2.1.2-1] Pubrec reserved bits must be 0"
return fhlen + 2
def __repr__(self):
return repr(self.fh)+", MsgId="+repr(self.messageIdentifier)+")"
def __str__(self):
return str(self.fh)+", MsgId="+str(self.messageIdentifier)+")"
def __eq__(self, packet):
return Packets.__eq__(self, packet) and \
@ -595,8 +595,8 @@ class Pubrels(Packets):
logger.info("[MQTT-3.6.1-1] bits in fixed header for pubrel are ok")
return fhlen + 2
def __repr__(self):
return repr(self.fh)+", MsgId="+repr(self.messageIdentifier)+")"
def __str__(self):
return str(self.fh)+", MsgId="+str(self.messageIdentifier)+")"
def __eq__(self, packet):
return Packets.__eq__(self, packet) and \
@ -632,8 +632,8 @@ class Pubcomps(Packets):
assert self.fh.RETAIN == False, "[MQTT-2.1.2-1] Retain should be false in Pubcomp"
return fhlen + 2
def __repr__(self):
return repr(self.fh)+", MsgId="+repr(self.messageIdentifier)+")"
def __str__(self):
return str(self.fh)+", MsgId="+str(self.messageIdentifier)+")"
def __eq__(self, packet):
return Packets.__eq__(self, packet) and \
@ -685,9 +685,9 @@ class Subscribes(Packets):
assert self.fh.RETAIN == False, "[MQTT-2.1.2-1] RETAIN must be false in subscribe"
return fhlen + self.fh.remainingLength
def __repr__(self):
return repr(self.fh)+", MsgId="+repr(self.messageIdentifier)+\
", Data="+repr(self.data)+")"
def __str__(self):
return str(self.fh)+", MsgId="+str(self.messageIdentifier)+\
", Data="+str(self.data)+")"
def __eq__(self, packet):
return Packets.__eq__(self, packet) and \
@ -735,9 +735,9 @@ class Subacks(Packets):
assert self.fh.RETAIN == False, "[MQTT-2.1.2-1] Retain should be false in suback"
return fhlen + self.fh.remainingLength
def __repr__(self):
return repr(self.fh)+", MsgId="+repr(self.messageIdentifier)+\
", Data="+repr(self.data)+")"
def __str__(self):
return str(self.fh)+", MsgId="+str(self.messageIdentifier)+\
", Data="+str(self.data)+")"
def __eq__(self, packet):
return Packets.__eq__(self, packet) and \
@ -787,9 +787,9 @@ class Unsubscribes(Packets):
logger.info("[MQTT-3-10.1-1] fixed header bits are 0,0,1,0")
return fhlen + self.fh.remainingLength
def __repr__(self):
return repr(self.fh)+", MsgId="+repr(self.messageIdentifier)+\
", Data="+repr(self.data)+")"
def __str__(self):
return str(self.fh)+", MsgId="+str(self.messageIdentifier)+\
", Data="+str(self.data)+")"
def __eq__(self, packet):
return Packets.__eq__(self, packet) and \
@ -827,8 +827,8 @@ class Unsubacks(Packets):
assert self.fh.RETAIN == False, "[MQTT-2.1.2-1]"
return fhlen + self.fh.remainingLength
def __repr__(self):
return repr(self.fh)+", MsgId="+repr(self.messageIdentifier)+")"
def __str__(self):
return str(self.fh)+", MsgId="+str(self.messageIdentifier)+")"
def __eq__(self, packet):
return Packets.__eq__(self, packet) and \
@ -855,8 +855,8 @@ class Pingreqs(Packets):
assert self.fh.RETAIN == False, "[MQTT-2.1.2-1]"
return fhlen
def __repr__(self):
return repr(self.fh)+")"
def __str__(self):
return str(self.fh)+")"
class Pingresps(Packets):
@ -879,8 +879,8 @@ class Pingresps(Packets):
assert self.fh.RETAIN == False, "[MQTT-2.1.2-1]"
return fhlen
def __repr__(self):
return repr(self.fh)+")"
def __str__(self):
return str(self.fh)+")"
classes = [None, Connects, Connacks, Publishes, Pubacks, Pubrecs,
Pubrels, Pubcomps, Subscribes, Subacks, Unsubscribes,
@ -910,11 +910,10 @@ if __name__ == "__main__":
pass
for packet in classes[1:]:
before = str(packet())
before = str(packet())
after = str(unpackPacket(packet().pack()))
try:
assert before == after
except:
print("before:", before, "\nafter:", after)
print("End")

1464
test/MQTTV5.py Normal file

File diff suppressed because it is too large Load Diff

View File

@ -13,6 +13,7 @@
Contributors:
Ian Craggs - initial implementation and/or documentation
Ian Craggs - add MQTTV5 support
*******************************************************************
"""
from __future__ import print_function
@ -20,11 +21,15 @@ from __future__ import print_function
import socket, sys, select, traceback, datetime, os
try:
import socketserver
import MQTTV311 as MQTTV3 # Trace MQTT traffic - Python 3 version
import MQTTV311 # Trace MQTT traffic - Python 3 version
import MQTTV5
except:
traceback.print_exc()
import SocketServer as socketserver
import MQTTV3112 as MQTTV3 # Trace MQTT traffic - Python 2 version
import MQTTV3112 as MQTTV311 # Trace MQTT traffic - Python 2 version
import MQTTV5
MQTT = MQTTV311
logging = True
myWindow = None
@ -38,6 +43,7 @@ suspended = []
class MyHandler(socketserver.StreamRequestHandler):
def handle(self):
global MQTT
if not hasattr(self, "ids"):
self.ids = {}
if not hasattr(self, "versions"):
@ -55,12 +61,30 @@ class MyHandler(socketserver.StreamRequestHandler):
if s in suspended:
print("suspended")
if s == clients and s not in suspended:
inbuf = MQTTV3.getPacket(clients) # get one packet
inbuf = MQTT.getPacket(clients) # get one packet
if inbuf == None:
break
try:
packet = MQTTV3.unpackPacket(inbuf)
if packet.fh.MessageType == MQTTV3.PUBLISH and \
# if connect, this could be MQTTV3 or MQTTV5
if inbuf[0] >> 4 == 1: # connect packet
protocol_string = b'MQTT'
pos = inbuf.find(protocol_string)
if pos != -1:
version = inbuf[pos + len(protocol_string)]
if version == 5:
MQTT = MQTTV5
else:
MQTT = MQTTV311
packet = MQTT.unpackPacket(inbuf)
if hasattr(packet.fh, "MessageType"):
packet_type = packet.fh.MessageType
publish_type = MQTT.PUBLISH
connect_type = MQTT.CONNECT
else:
packet_type = packet.fh.PacketType
publish_type = MQTT.PacketTypes.PUBLISH
connect_type = MQTT.PacketTypes.CONNECT
if packet_type == publish_type and \
packet.topicName == "MQTTSAS topic" and \
packet.data == b"TERMINATE":
print("Terminating client", self.ids[id(clients)])
@ -68,26 +92,26 @@ class MyHandler(socketserver.StreamRequestHandler):
clients.close()
terminated = True
break
elif packet.fh.MessageType == MQTTV3.PUBLISH and \
elif packet_type == publish_type and \
packet.topicName == "MQTTSAS topic" and \
packet.data == b"TERMINATE_SERVER":
print("Suspending client ", self.ids[id(clients)])
suspended.append(clients)
elif packet.fh.MessageType == MQTTV3.CONNECT:
elif packet_type == connect_type:
self.ids[id(clients)] = packet.ClientIdentifier
self.versions[id(clients)] = 3
print(timestamp() , "C to S", self.ids[id(clients)], repr(packet))
print(timestamp() , "C to S", self.ids[id(clients)], str(packet))
#print([hex(b) for b in inbuf])
#print(inbuf)
except:
traceback.print_exc()
brokers.send(inbuf) # pass it on
elif s == brokers:
inbuf = MQTTV3.getPacket(brokers) # get one packet
inbuf = MQTT.getPacket(brokers) # get one packet
if inbuf == None:
break
try:
print(timestamp(), "S to C", self.ids[id(clients)], repr(MQTTV3.unpackPacket(inbuf)))
print(timestamp(), "S to C", self.ids[id(clients)], str(MQTT.unpackPacket(inbuf)))
except:
traceback.print_exc()
clients.send(inbuf)

View File

@ -1166,7 +1166,7 @@ int main(int argc, char** argv)
fprintf(xml, "<testsuite name=\"test1\" tests=\"%d\">\n", (int)(ARRAY_SIZE(tests) - 1));
setenv("MQTT_C_CLIENT_TRACE", "ON", 1);
setenv("MQTT_C_CLIENT_TRACE_LEVEL", "ERROR", 0);
setenv("MQTT_C_CLIENT_TRACE_LEVEL", "ERROR", 1);
getopts(argc, argv);

View File

@ -20,16 +20,10 @@
/**
* @file
* Tests for the MQ Telemetry MQTT C client
* Tests for the MQTT C client
*/
/*
#if !defined(_RTSHEADER)
#include <rts.h>
#endif
*/
#include "MQTTClient.h"
#include <string.h>
#include <stdlib.h>
@ -542,19 +536,31 @@ void test2_deliveryComplete(void* context, MQTTClient_deliveryToken dt)
++test2_deliveryCompleted;
}
int test2_messageArrived(void* context, char* topicName, int topicLen, MQTTClient_message* m)
int test2_messageArrived(void* context, char* topicName, int topicLen, MQTTClient_message* message)
{
++test2_arrivedcount;
MyLog(LOGA_DEBUG, "Callback: %d message received on topic %s is %.*s.",
test2_arrivedcount, topicName, m->payloadlen, (char*)(m->payload));
if (test2_pubmsg.payloadlen != m->payloadlen ||
memcmp(m->payload, test2_pubmsg.payload, m->payloadlen) != 0)
test2_arrivedcount, topicName, message->payloadlen, (char*)(message->payload));
assert("Message structure version should be 1", message->struct_version == 1,
"message->struct_version was %d", message->struct_version);
if (message->struct_version == 1)
{
const int props_count = 0;
assert("Properties count should be 0", message->properties.count == props_count,
"Properties count was %d\n", message->properties.count);
logProperties(&message->properties);
}
if (test2_pubmsg.payloadlen != message->payloadlen ||
memcmp(message->payload, test2_pubmsg.payload, message->payloadlen) != 0)
{
failures++;
MyLog(LOGA_INFO, "Error: wrong data received lengths %d %d\n", test2_pubmsg.payloadlen, m->payloadlen);
MyLog(LOGA_INFO, "Error: wrong data received lengths %d %d\n", test2_pubmsg.payloadlen, message->payloadlen);
}
MQTTClient_free(topicName);
MQTTClient_freeMessage(&m);
MQTTClient_freeMessage(&message);
return 1;
}
@ -564,7 +570,7 @@ void test2_sendAndReceive(MQTTClient* c, int qos, char* test_topic)
MQTTClient_deliveryToken dt;
int i = 0;
int iterations = 50;
int rc = 0;
MQTTResponse response = {SUCCESS, NULL};
int wait_seconds = 0;
test2_deliveryCompleted = 0;
@ -578,11 +584,11 @@ void test2_sendAndReceive(MQTTClient* c, int qos, char* test_topic)
for (i = 1; i <= iterations; ++i)
{
if (i % 10 == 0)
rc = MQTTClient_publish(c, test_topic, test2_pubmsg.payloadlen, test2_pubmsg.payload,
test2_pubmsg.qos, test2_pubmsg.retained, NULL);
response = MQTTClient_publish5(c, test_topic, test2_pubmsg.payloadlen, test2_pubmsg.payload,
test2_pubmsg.qos, test2_pubmsg.retained, NULL, NULL);
else
rc = MQTTClient_publishMessage(c, test_topic, &test2_pubmsg, &dt);
assert("Good rc from publish", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
response = MQTTClient_publishMessage5(c, test_topic, &test2_pubmsg, &dt);
assert("Good rc from publish", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
#if defined(WIN32)
Sleep(100);
@ -633,6 +639,10 @@ int test2(struct Options options)
/* TODO - usused - remove ? MQTTClient_deliveryToken* dt = NULL; */
MQTTClient c;
MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer;
MQTTProperties props = MQTTProperties_initializer;
MQTTProperties willProps = MQTTProperties_initializer;
MQTTResponse response = {SUCCESS, NULL};
MQTTSubscribe_options subopts = MQTTSubscribe_options_initializer;
int rc = 0;
char* test_topic = "C client test2";
@ -659,12 +669,12 @@ int test2(struct Options options)
assert("Good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
MyLog(LOGA_DEBUG, "Connecting");
rc = MQTTClient_connect(c, &opts);
response = MQTTClient_connect5(c, &opts, &props, &willProps);
assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
if (rc != MQTTCLIENT_SUCCESS)
goto exit;
rc = MQTTClient_subscribe(c, test_topic, subsqos);
response = MQTTClient_subscribe5(c, test_topic, subsqos, &subopts, &props);
assert("Good rc from subscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
test2_sendAndReceive(c, 0, test_topic);
@ -673,9 +683,9 @@ int test2(struct Options options)
MyLog(LOGA_DEBUG, "Stopping");
rc = MQTTClient_unsubscribe(c, test_topic);
assert("Unsubscribe successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
rc = MQTTClient_disconnect(c, 0);
response = MQTTClient_unsubscribe5(c, test_topic, &props);
assert("Unsubscribe successful", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", rc);
rc = MQTTClient_disconnect5(c, 0, SUCCESS, &props);
assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
MQTTClient_destroy(&c);
@ -788,12 +798,15 @@ int test4_run(int qos)
int mytoken = -99;
char buffer[100];
int count = 3;
MQTTProperty property;
MQTTProperties props = MQTTProperties_initializer;
MQTTResponse response = {SUCCESS, NULL};
int i, rc;
failures = 0;
MyLog(LOGA_INFO, "Starting test 4 - persistence, qos %d", qos);
MQTTClient_create(&c, options.connection, "xrctest1_test_4", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
MQTTClient_create(&c, options.connection, "xrctest15_test_4", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
opts.keepAliveInterval = 20;
opts.reliable = 0;
@ -804,37 +817,31 @@ int test4_run(int qos)
opts.serverURIcount = options.hacount;
}
MyLog(LOGA_DEBUG, "Cleanup by connecting clean session\n");
MyLog(LOGA_DEBUG, "Cleanup by connecting clean start, add session expiry > 0\n");
opts.cleansession = 1;
if ((rc = MQTTClient_connect(c, &opts)) != 0)
{
assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
property.identifier = SESSION_EXPIRY_INTERVAL;
property.value.integer4 = 30; /* in seconds */
MQTTProperties_add(&props, &property);
response = MQTTClient_connect5(c, &opts, &props, NULL);
assert("Good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS,
"rc was %d", response.reasonCode);
if (response.reasonCode != MQTTCLIENT_SUCCESS)
return -1;
}
opts.cleansession = 0;
MQTTClient_disconnect(c, 0);
MyLog(LOGA_DEBUG, "Connecting\n");
if ((rc = MQTTClient_connect(c, &opts)) != 0)
{
assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
return -1;
}
/* subscribe so we can get messages back */
rc = MQTTClient_subscribe(c, topic, subsqos);
assert("Good rc from subscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
response = MQTTClient_subscribe5(c, topic, subsqos, NULL, NULL);
assert("Good rc from subscribe", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
/* send messages so that we can receive the same ones */
for (i = 0; i < count; ++i)
{
sprintf(buffer, "Message sequence no %d", i);
rc = MQTTClient_publish(c, topic, 10, buffer, qos, 0, NULL);
assert("Good rc from publish", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
response = MQTTClient_publish5(c, topic, 10, buffer, qos, 0, NULL, NULL);
assert("Good rc from publish", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
}
/* disconnect immediately without receiving the incoming messages */
MQTTClient_disconnect(c, 0); /* now there should be "orphaned" publications */
MQTTClient_disconnect5(c, 0, SUCCESS, NULL); /* now there should be "orphaned" publications */
rc = MQTTClient_getPendingDeliveryTokens(c, &tokens);
assert("getPendingDeliveryTokens rc == 0", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
@ -850,10 +857,10 @@ int test4_run(int qos)
assert1("no of tokens should be count", i == count, "no of tokens %d count %d", i, count);
mytoken = tokens[0];
}
MQTTProperties_free(&props);
MQTTClient_destroy(&c); /* force re-reading persistence on create */
MQTTClient_create(&c, options.connection, "xrctest1_test_4", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
MQTTClient_create(&c, options.connection, "xrctest15_test_4", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
rc = MQTTClient_getPendingDeliveryTokens(c, &tokens);
assert("getPendingDeliveryTokens rc == 0", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
@ -869,18 +876,18 @@ int test4_run(int qos)
}
MyLog(LOGA_DEBUG, "Reconnecting");
if (MQTTClient_connect(c, &opts) != 0)
{
assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
opts.cleansession = 0;
response = MQTTClient_connect5(c, &opts, NULL, NULL);
assert("Good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
if (response.reasonCode != MQTTCLIENT_SUCCESS)
return -1;
}
for (i = 0; i < count; ++i)
{
int dup = 0;
do
{
dup = 0;
dup = 0;
MQTTClient_receive(c, &topicName, &topicLen, &m, 5000);
if (m && m->dup)
{
@ -888,7 +895,7 @@ int test4_run(int qos)
MyLog(LOGA_DEBUG, "Duplicate message id %d", m->msgid);
MQTTClient_freeMessage(&m);
MQTTClient_free(topicName);
dup = 1;
dup = 1;
}
} while (dup == 1);
assert("should get a message", m != NULL, "m was %p", m);
@ -907,7 +914,7 @@ int test4_run(int qos)
assert("getPendingDeliveryTokens rc == 0", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
assert("should get no tokens back", tokens == NULL, "tokens was %p", tokens);
MQTTClient_disconnect(c, 0);
rc = MQTTClient_disconnect5(c, 0, SUCCESS, NULL);
MQTTClient_destroy(&c);
@ -951,6 +958,9 @@ int test5(struct Options options)
MQTTClient_deliveryToken* tokens = NULL;
char buffer[100];
int count = 5;
MQTTProperty property;
MQTTProperties props = MQTTProperties_initializer;
MQTTResponse response = {SUCCESS, NULL};
int i, rc;
fprintf(xml, "<testcase classname=\"test1\" name=\"disconnect with quiesce timeout should allow exchanges to complete\"");
@ -958,10 +968,10 @@ int test5(struct Options options)
failures = 0;
MyLog(LOGA_INFO, "Starting test 5 - disconnect with quiesce timeout should allow exchanges to complete");
MQTTClient_create(&c, options.connection, "xrctest1_test_5", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
MQTTClient_create(&c, options.connection, "xrctest15_test_5", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
opts.keepAliveInterval = 20;
opts.cleansession = 0;
opts.cleansession = 1;
opts.reliable = 0;
opts.MQTTVersion = options.MQTTVersion;
if (options.haconnections != NULL)
@ -969,26 +979,30 @@ int test5(struct Options options)
opts.serverURIs = options.haconnections;
opts.serverURIcount = options.hacount;
}
property.identifier = SESSION_EXPIRY_INTERVAL;
property.value.integer4 = 30;
MQTTProperties_add(&props, &property);
MyLog(LOGA_DEBUG, "Connecting");
if ((rc = MQTTClient_connect(c, &opts)) != 0)
response = MQTTClient_connect5(c, &opts, &props, NULL);
assert("Good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
if (response.reasonCode != MQTTCLIENT_SUCCESS)
{
assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
MQTTClient_destroy(&c);
goto exit;
}
rc = MQTTClient_subscribe(c, topic, subsqos);
assert("Good rc from subscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
response = MQTTClient_subscribe5(c, topic, subsqos, NULL, NULL);
assert("Good rc from subscribe", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
for (i = 0; i < count; ++i)
{
sprintf(buffer, "Message sequence no %d", i);
rc = MQTTClient_publish(c, topic, 10, buffer, 1, 0, NULL);
assert("Good rc from publish", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
response = MQTTClient_publish5(c, topic, 10, buffer, 1, 0, NULL, NULL);
assert("Good rc from publish", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
}
MQTTClient_disconnect(c, 1000); /* now there should be no "orphaned" publications */
MQTTClient_disconnect5(c, 1000, SUCCESS, NULL); /* now there should be no "orphaned" publications */
MyLog(LOGA_DEBUG, "Disconnected");
rc = MQTTClient_getPendingDeliveryTokens(c, &tokens);
@ -996,6 +1010,7 @@ int test5(struct Options options)
assert("should get no tokens back", tokens == NULL, "tokens was %p", tokens);
MQTTProperties_free(&props);
MQTTClient_destroy(&c);
exit:
@ -1049,6 +1064,7 @@ int test6(struct Options options)
MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer;
MQTTClient_willOptions wopts = MQTTClient_willOptions_initializer;
MQTTClient_connectOptions opts2 = MQTTClient_connectOptions_initializer;
MQTTResponse response = {SUCCESS, NULL};
int rc, count;
char* mqttsas_topic = "MQTTSAS topic";
@ -1059,7 +1075,7 @@ int test6(struct Options options)
opts.keepAliveInterval = 2;
opts.cleansession = 1;
opts.MQTTVersion = MQTTVERSION_3_1_1;
opts.MQTTVersion = options.MQTTVersion;
opts.will = &wopts;
opts.will->message = test6_will_message;
opts.will->qos = 1;
@ -1083,7 +1099,7 @@ int test6(struct Options options)
goto exit;
/* Connect to the broker */
rc = MQTTClient_connect(test6_c1, &opts);
response = MQTTClient_connect5(test6_c1, &opts, NULL, NULL);
assert("good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
if (rc != MQTTCLIENT_SUCCESS)
goto exit;
@ -1100,15 +1116,15 @@ int test6(struct Options options)
opts2.keepAliveInterval = 20;
opts2.cleansession = 1;
MyLog(LOGA_INFO, "Connecting Client_2 ...");
rc = MQTTClient_connect(test6_c2, &opts2);
assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
response = MQTTClient_connect5(test6_c2, &opts2, NULL, NULL);
assert("Good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d\n", response.reasonCode);
rc = MQTTClient_subscribe(test6_c2, test6_will_topic, 2);
assert("Good rc from subscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
response = MQTTClient_subscribe5(test6_c2, test6_will_topic, 2, NULL, NULL);
assert("Good rc from subscribe", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d\n", response.reasonCode);
/* now send the command which will break the connection and cause the will message to be sent */
rc = MQTTClient_publish(test6_c1, mqttsas_topic, (int)strlen("TERMINATE"), "TERMINATE", 0, 0, NULL);
assert("Good rc from publish", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
response = MQTTClient_publish5(test6_c1, mqttsas_topic, (int)strlen("TERMINATE"), "TERMINATE", 0, 0, NULL, NULL);
assert("Good rc from publish", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d\n", response.reasonCode);
MyLog(LOGA_INFO, "Waiting to receive the will message");
count = 0;
@ -1127,8 +1143,8 @@ int test6(struct Options options)
assert("connection lost called", test6_connection_lost_called == 1,
"connection_lost_called %d\n", test6_connection_lost_called);
rc = MQTTClient_unsubscribe(test6_c2, test6_will_topic);
assert("Good rc from unsubscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
response = MQTTClient_unsubscribe5(test6_c2, test6_will_topic, NULL);
assert("Good rc from unsubscribe", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
rc = MQTTClient_isConnected(test6_c2);
assert("Client-2 still connected", rc == 1, "isconnected is %d", rc);
@ -1136,7 +1152,7 @@ int test6(struct Options options)
rc = MQTTClient_isConnected(test6_c1);
assert("Client-1 not connected", rc == 0, "isconnected is %d", rc);
rc = MQTTClient_disconnect(test6_c2, 100L);
rc = MQTTClient_disconnect5(test6_c2, 100L, SUCCESS, NULL);
assert("Good rc from disconnect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
MQTTClient_destroy(&test6_c1);
@ -1157,6 +1173,7 @@ int test6a(struct Options options)
MQTTClient_willOptions wopts = MQTTClient_willOptions_initializer;
MQTTClient_connectOptions opts2 = MQTTClient_connectOptions_initializer;
int rc, count;
MQTTResponse response = {SUCCESS, NULL};
char* mqttsas_topic = "MQTTSAS topic";
failures = 0;
@ -1166,7 +1183,7 @@ int test6a(struct Options options)
opts.keepAliveInterval = 2;
opts.cleansession = 1;
opts.MQTTVersion = MQTTVERSION_3_1_1;
opts.MQTTVersion = options.MQTTVersion;
opts.will = &wopts;
opts.will->payload.data = test6_will_message;
opts.will->payload.len = strlen(test6_will_message) + 1;
@ -1191,9 +1208,10 @@ int test6a(struct Options options)
goto exit;
/* Connect to the broker */
rc = MQTTClient_connect(test6_c1, &opts);
assert("good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
if (rc != MQTTCLIENT_SUCCESS)
response = MQTTClient_connect5(test6_c1, &opts, NULL, NULL);
assert("good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS,
"rc was %d\n", response.reasonCode);
if (response.reasonCode != MQTTCLIENT_SUCCESS)
goto exit;
/* Client - 2 (multi-threaded) */
@ -1208,15 +1226,15 @@ int test6a(struct Options options)
opts2.keepAliveInterval = 20;
opts2.cleansession = 1;
MyLog(LOGA_INFO, "Connecting Client_2 ...");
rc = MQTTClient_connect(test6_c2, &opts2);
assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
response = MQTTClient_connect5(test6_c2, &opts2, NULL, NULL);
assert("Good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d\n", response.reasonCode);
rc = MQTTClient_subscribe(test6_c2, test6_will_topic, 2);
assert("Good rc from subscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
response = MQTTClient_subscribe5(test6_c2, test6_will_topic, 2, NULL, NULL);
assert("Good rc from subscribe", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d\n", response.reasonCode);
/* now send the command which will break the connection and cause the will message to be sent */
rc = MQTTClient_publish(test6_c1, mqttsas_topic, (int)strlen("TERMINATE"), "TERMINATE", 0, 0, NULL);
assert("Good rc from publish", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
response = MQTTClient_publish5(test6_c1, mqttsas_topic, (int)strlen("TERMINATE"), "TERMINATE", 0, 0, NULL, NULL);
assert("Good rc from publish", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d\n", response.reasonCode);
MyLog(LOGA_INFO, "Waiting to receive the will message");
count = 0;
@ -1235,8 +1253,8 @@ int test6a(struct Options options)
assert("connection lost called", test6_connection_lost_called == 1,
"connection_lost_called %d\n", test6_connection_lost_called);
rc = MQTTClient_unsubscribe(test6_c2, test6_will_topic);
assert("Good rc from unsubscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
response = MQTTClient_unsubscribe5(test6_c2, test6_will_topic, NULL);
assert("Good rc from unsubscribe", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
rc = MQTTClient_isConnected(test6_c2);
assert("Client-2 still connected", rc == 1, "isconnected is %d", rc);
@ -1244,7 +1262,7 @@ int test6a(struct Options options)
rc = MQTTClient_isConnected(test6_c1);
assert("Client-1 not connected", rc == 0, "isconnected is %d", rc);
rc = MQTTClient_disconnect(test6_c2, 100L);
rc = MQTTClient_disconnect5(test6_c2, 100L, SUCCESS, NULL);
assert("Good rc from disconnect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
MQTTClient_destroy(&test6_c1);
@ -1267,8 +1285,7 @@ int main(int argc, char** argv)
fprintf(xml, "<testsuite name=\"test1\" tests=\"%d\">\n", (int)(ARRAY_SIZE(tests) - 1));
setenv("MQTT_C_CLIENT_TRACE", "ON", 1);
setenv("MQTT_C_CLIENT_TRACE_LEVEL", "ERROR", 0);
//setenv("MQTT_C_CLIENT_TRACE_LEVEL", "PROTOCOL", 0);
setenv("MQTT_C_CLIENT_TRACE_LEVEL", "ERROR", 1);
getopts(argc, argv);

View File

@ -2056,8 +2056,7 @@ int test7(struct Options options)
{
char* testname = "test7";
int subsqos = 2;
AsyncTestClient tc =
AsyncTestClient_initializer;
AsyncTestClient tc = AsyncTestClient_initializer;
MQTTAsync c;
MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;

View File

@ -61,7 +61,7 @@ struct
int persistence;
} opts =
{
"tcp://localhost:1885",
"tcp://localhost:1884",
NULL,
0,
"tcp://localhost:7777",
@ -297,22 +297,24 @@ void control_connectionLost(void* context, char* cause)
*/
int control_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* m)
{
MyLog(LOGA_ALWAYS, "Control message arrived: %.*s %s",
MyLog(LOGA_ALWAYS, "Control message arrived: %.*s wait message: %s",
m->payloadlen, m->payload, (wait_message == NULL) ? "None" : wait_message);
if (strncmp(m->payload, "stop", 4) == 0)
{
MyLog(LOGA_ALWAYS, "Stop message arrived, stopping...");
stopping = 1;
stopping = 1;
}
else if (wait_message != NULL && strncmp(wait_message, m->payload,
strlen(wait_message)) == 0)
{
MyLog(LOGA_ALWAYS, "Wait message %s found", wait_message);
control_found = 1;
wait_message = NULL;
}
else if (wait_message2 != NULL && strncmp(wait_message2, m->payload,
strlen(wait_message2)) == 0)
{
MyLog(LOGA_ALWAYS, "Wait message2 %s found", wait_message);
control_found = 2;
wait_message2 = NULL;
}
@ -351,7 +353,7 @@ int control_wait(char* message)
sprintf(buf, "waiting for: %s", message);
control_send(buf);
MyLog(LOGA_ALWAYS, "waiting for: %s", message);
MyLog(LOGA_ALWAYS, "Waiting for: %s", message);
while (control_found == 0 && stopping == 0)
{
if (++count == 300)
@ -362,6 +364,7 @@ int control_wait(char* message)
}
MySleep(1000);
}
MyLog(LOGA_ALWAYS, "Control message found: %s, control_found %d", message, control_found);
return control_found;
}
@ -377,7 +380,7 @@ int control_which(char* message1, char* message2)
while (control_found == 0)
{
if (++count == 300)
return 0; /* time out and tell the caller the message was not found */
break; /* time out and tell the caller the message was not found */
MySleep(1000);
}
return control_found;
@ -750,7 +753,7 @@ void client_onSubscribe(void* context, MQTTAsync_successData* response)
void client_onFailure(void* context, MQTTAsync_failureData* response)
{
MQTTAsync c = (MQTTAsync)context;
MyLog(LOGA_DEBUG, "In failure callback");
MyLog(LOGA_INFO, "In failure callback");
client_subscribed = -1;
}