Subscribe tests

This commit is contained in:
Ian Craggs
2014-07-14 16:55:18 +01:00
parent 1aadfb34a6
commit 6b80ed8733
4 changed files with 143 additions and 34 deletions

View File

@@ -19,7 +19,11 @@
int MQTTSNSerialize_subscribe(unsigned char* buf, int buflen, int dup, int qos, unsigned short packetid,
MQTTSN_topicid* topicFilter);
int MQTTSNDeserialize_subscribe(int* dup, int* qos, unsigned short* packetid,
MQTTSN_topicid* topicFilter, unsigned char* buf, int buflen);
int MQTTSNSerialize_suback(unsigned char* buf, int buflen, int qos, unsigned short topicid, unsigned short packetid,
unsigned char returncode);
int MQTTSNDeserialize_suback(int* qos, unsigned short* topicid, unsigned short* packetid,
unsigned char* returncode, unsigned char* buf, int buflen);

View File

@@ -0,0 +1,98 @@
/*******************************************************************************
* Copyright (c) 2014 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
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Ian Craggs - initial API and implementation and/or initial documentation
*******************************************************************************/
#include "StackTrace.h"
#include "MQTTSNPacket.h"
#include <string.h>
int MQTTSNDeserialize_subscribe(int* dup, int* qos, unsigned short* packetid,
MQTTSN_topicid* topicFilter, unsigned char* buf, int buflen)
{
MQTTSNFlags flags;
unsigned char* curdata = buf;
unsigned char* enddata = NULL;
int rc = 0;
int mylen = 0;
FUNC_ENTRY;
curdata += (rc = MQTTSNPacket_decode(curdata, buflen, &mylen)); /* read length */
enddata = buf + mylen;
if (enddata - curdata > buflen)
goto exit;
if (readChar(&curdata) != MQTTSN_SUBSCRIBE)
goto exit;
flags.all = readChar(&curdata);
*dup = flags.bits.dup;
*qos = flags.bits.QoS;
*packetid = readInt(&curdata);
topicFilter->type = flags.bits.topicIdType;
if (topicFilter->type == MQTTSN_TOPIC_TYPE_NORMAL)
{
topicFilter->data.long_.len = enddata - curdata;
topicFilter->data.long_.name = (char*)curdata;
}
else if (topicFilter->type == MQTTSN_TOPIC_TYPE_PREDEFINED)
topicFilter->data.id = readInt(&curdata);
else if (topicFilter->type == MQTTSN_TOPIC_TYPE_SHORT)
{
topicFilter->data.short_name[0] = readChar(&curdata);
topicFilter->data.short_name[1] = readChar(&curdata);
}
rc = 1;
exit:
FUNC_EXIT_RC(rc);
return rc;
}
int MQTTSNSerialize_suback(unsigned char* buf, int buflen, int qos, unsigned short topicid, unsigned short packetid,
unsigned char returncode)
{
MQTTSNFlags flags;
unsigned char *ptr = buf;
int len = 0;
int rc = 0;
FUNC_ENTRY;
if ((len = MQTTSNPacket_len(7)) > buflen)
{
rc = MQTTSNPACKET_BUFFER_TOO_SHORT;
goto exit;
}
ptr += MQTTSNPacket_encode(ptr, len); /* write length */
writeChar(&ptr, MQTTSN_SUBACK); /* write message type */
flags.all = 0;
flags.bits.QoS = qos;
writeChar(&ptr, flags.all);
writeInt(&ptr, topicid);
writeInt(&ptr, packetid);
writeChar(&ptr, returncode);
rc = ptr - buf;
exit:
FUNC_EXIT_RC(rc);
return rc;
}

View File

@@ -1,3 +1,3 @@
gcc -Wall test1.c -o test1 -I../src ../src/MQTTSNConnectClient.c ../src/MQTTSNConnectServer.c ../src/MQTTSNPacket.c ../src/MQTTSNSerializePublish.c ../src/MQTTSNDeserializePublish.c
gcc -Wall test1.c -o test1 -I../src ../src/MQTTSNConnectClient.c ../src/MQTTSNConnectServer.c ../src/MQTTSNPacket.c ../src/MQTTSNSerializePublish.c ../src/MQTTSNDeserializePublish.c ../src/MQTTSNSubscribeClient.c ../src/MQTTSNSubscribeServer.c
gcc -Wall test2.c -o test2 -I../src ../src/MQTTSNConnectClient.c ../src/MQTTSNConnectServer.c ../src/MQTTSNPacket.c ../src/MQTTSNSerializePublish.c ../src/MQTTSNDeserializePublish.c ../src/MQTTSNSubscribeClient.c

View File

@@ -266,7 +266,20 @@ int checkMQTTStrings(MQTTString a, MQTTString b)
int checkMQTTSNTopics(MQTTSN_topicid a, MQTTSN_topicid b)
{
return a.type == b.type && a.data.id == b.data.id;
int rc = 0;
if (a.type != b.type)
goto exit;
if (a.data.long_.name)
rc = memcmp(a.data.long_.name, b.data.long_.name, a.data.long_.len) == 0;
else if (a.type == MQTTSN_TOPIC_TYPE_SHORT)
rc = (memcpy(a.data.short_name, b.data.short_name, 2) == 0);
else
rc = (a.data.id == b.data.id);
exit:
return rc;
}
@@ -570,62 +583,56 @@ int test3(struct Options options)
}
#if 0
int test3(struct Options options)
int test4(struct Options options)
{
int i = 0;
int rc = 0;
char buf[100];
int buflen = sizeof(buf);
#define TOPIC_COUNT 2
unsigned char buf[100];
size_t buflen = sizeof(buf);
int dup = 0;
int msgid = 23;
int count = TOPIC_COUNT;
MQTTString topicStrings[TOPIC_COUNT] = { MQTTString_initializer, MQTTString_initializer };
int req_qoss[TOPIC_COUNT] = {2, 1};
unsigned short packetid = 23;
MQTTSN_topicid topicFilter;
int req_qos = 2;
int dup2 = 1;
int msgid2 = 2223;
int count2 = 0;
MQTTString topicStrings2[TOPIC_COUNT] = { MQTTString_initializer, MQTTString_initializer };
int req_qoss2[TOPIC_COUNT] = {0, 0};
unsigned short packetid2 = 2223;
MQTTSN_topicid topicFilter2;
int req_qos2 = 0;
fprintf(xml, "<testcase classname=\"test1\" name=\"de/serialization\"");
fprintf(xml, "<testcase classname=\"test4\" name=\"de/serialization\"");
global_start_time = start_clock();
failures = 0;
MyLog(LOGA_INFO, "Starting test 2 - serialization of subscribe and back");
MyLog(LOGA_INFO, "Starting test 4 - serialization of subscribe and back");
topicStrings[0].cstring = "mytopic";
topicStrings[1].cstring = "mytopic2";
rc = MQTTSerialize_subscribe(buf, buflen, dup, msgid, count, topicStrings, req_qoss);
memset(&topicFilter, '\0', sizeof(topicFilter));
memset(&topicFilter2, '\0', sizeof(topicFilter2));
topicFilter.type = MQTTSN_TOPIC_TYPE_NORMAL;
topicFilter.data.long_.name = "mytopic";
topicFilter.data.long_.len = strlen(topicFilter.data.long_.name);
rc = MQTTSNSerialize_subscribe(buf, buflen, dup, req_qos, packetid, &topicFilter);
assert("good rc from serialize subscribe", rc > 0, "rc was %d\n", rc);
rc = MQTTDeserialize_subscribe(&dup2, &msgid2, 2, &count2, topicStrings2, req_qoss2, buf, buflen);
rc = MQTTSNDeserialize_subscribe(&dup2, &req_qos2, &packetid2, &topicFilter2, buf, buflen);
assert("good rc from deserialize subscribe", rc == 1, "rc was %d\n", rc);
/* data after should be the same as data before */
assert("dups should be the same", dup == dup2, "dups were different %d\n", dup2);
assert("msgids should be the same", msgid == msgid2, "msgids were different %d\n", msgid2);
assert("msgids should be the same", packetid == packetid2, "packetids were different %d\n", packetid2);
assert("count should be the same", count == count2, "counts were different %d\n", count2);
for (i = 0; i < count2; ++i)
{
assert("topics should be the same",
checkMQTTStrings(topicStrings[i], topicStrings2[i]), "topics were different %s\n", "");
assert("qoss should be the same", req_qoss[i] == req_qoss2[i], "qoss were different %d\n", req_qoss2[i]);
}
assert("topics should be the same",
checkMQTTSNTopics(topicFilter, topicFilter2), "topics were different %s\n", "");
assert("qoss should be the same", req_qos == req_qos2, "qoss were different %d\n", req_qos2);
/*exit:*/
MyLog(LOGA_INFO, "TEST3: test %s. %d tests run, %d failures.",
MyLog(LOGA_INFO, "TEST4: test %s. %d tests run, %d failures.",
(failures == 0) ? "passed" : "failed", tests, failures);
write_test_result();
return failures;
}
#if 0
int test4(struct Options options)
{
int i = 0;
@@ -754,7 +761,7 @@ int test6(struct Options options)
int main(int argc, char** argv)
{
int rc = 0;
int (*tests[])() = {NULL, test1, test2, test3, /*test4, test5,*/ test6};
int (*tests[])() = {NULL, test1, test2, test3, test4, test6};
xml = fopen("TEST-test1.xml", "w");
fprintf(xml, "<testsuite name=\"test1\" tests=\"%d\">\n", (int)(ARRAY_SIZE(tests) - 1));