From 6b80ed873362ec5c32f6468ad72b962642815b01 Mon Sep 17 00:00:00 2001 From: Ian Craggs Date: Mon, 14 Jul 2014 16:55:18 +0100 Subject: [PATCH] Subscribe tests --- src/MQTTSNSubscribe.h | 4 ++ src/MQTTSNSubscribeServer.c | 98 +++++++++++++++++++++++++++++++++++++ test/build_test | 2 +- test/test1.c | 73 ++++++++++++++------------- 4 files changed, 143 insertions(+), 34 deletions(-) create mode 100644 src/MQTTSNSubscribeServer.c diff --git a/src/MQTTSNSubscribe.h b/src/MQTTSNSubscribe.h index 3261ac1..c8b8a78 100644 --- a/src/MQTTSNSubscribe.h +++ b/src/MQTTSNSubscribe.h @@ -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); diff --git a/src/MQTTSNSubscribeServer.c b/src/MQTTSNSubscribeServer.c new file mode 100644 index 0000000..dd7d01d --- /dev/null +++ b/src/MQTTSNSubscribeServer.c @@ -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 + +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; +} + + diff --git a/test/build_test b/test/build_test index 2a963b8..2195230 100644 --- a/test/build_test +++ b/test/build_test @@ -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 diff --git a/test/test1.c b/test/test1.c index f72c9f1..e2adf5a 100644 --- a/test/test1.c +++ b/test/test1.c @@ -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, " 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, "\n", (int)(ARRAY_SIZE(tests) - 1));