Basic C library for MQTT packet serialization and deserialization

Dependents:   MQTT MQTT MQTT MQTT ... more

Fork of MQTTPacket by MQTT

This library is part of the EclipseTM Paho project; specifically the embedded client.

A basic MQTT library in C for packet serialization and deserialization

Committer:
icraggs
Date:
Thu Apr 10 15:11:16 2014 +0000
Revision:
2:bc3bc0e3b764
Child:
12:cd99ac9cb25a
Add samples and test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icraggs 2:bc3bc0e3b764 1 /*******************************************************************************
icraggs 2:bc3bc0e3b764 2 * Copyright (c) 2014 IBM Corp.
icraggs 2:bc3bc0e3b764 3 *
icraggs 2:bc3bc0e3b764 4 * All rights reserved. This program and the accompanying materials
icraggs 2:bc3bc0e3b764 5 * are made available under the terms of the Eclipse Public License v1.0
icraggs 2:bc3bc0e3b764 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
icraggs 2:bc3bc0e3b764 7 *
icraggs 2:bc3bc0e3b764 8 * The Eclipse Public License is available at
icraggs 2:bc3bc0e3b764 9 * http://www.eclipse.org/legal/epl-v10.html
icraggs 2:bc3bc0e3b764 10 * and the Eclipse Distribution License is available at
icraggs 2:bc3bc0e3b764 11 * http://www.eclipse.org/org/documents/edl-v10.php.
icraggs 2:bc3bc0e3b764 12 *
icraggs 2:bc3bc0e3b764 13 * Contributors:
icraggs 2:bc3bc0e3b764 14 * Ian Craggs - initial API and implementation and/or initial documentation
icraggs 2:bc3bc0e3b764 15 *******************************************************************************/
icraggs 2:bc3bc0e3b764 16
icraggs 2:bc3bc0e3b764 17
icraggs 2:bc3bc0e3b764 18 #include "MQTTPacket.h"
icraggs 2:bc3bc0e3b764 19 #include <string.h>
icraggs 2:bc3bc0e3b764 20 #include <stdlib.h>
icraggs 2:bc3bc0e3b764 21 #include <stdio.h>
icraggs 2:bc3bc0e3b764 22 #include "Timer.h"
icraggs 2:bc3bc0e3b764 23
icraggs 2:bc3bc0e3b764 24
icraggs 2:bc3bc0e3b764 25 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
icraggs 2:bc3bc0e3b764 26
icraggs 2:bc3bc0e3b764 27 struct Options
icraggs 2:bc3bc0e3b764 28 {
icraggs 2:bc3bc0e3b764 29 char* connection; /**< connection to system under test. */
icraggs 2:bc3bc0e3b764 30 char** haconnections;
icraggs 2:bc3bc0e3b764 31 int hacount;
icraggs 2:bc3bc0e3b764 32 int verbose;
icraggs 2:bc3bc0e3b764 33 int test_no;
icraggs 2:bc3bc0e3b764 34 } options =
icraggs 2:bc3bc0e3b764 35 {
icraggs 2:bc3bc0e3b764 36 "tcp://m2m.eclipse.org:1883",
icraggs 2:bc3bc0e3b764 37 NULL,
icraggs 2:bc3bc0e3b764 38 0,
icraggs 2:bc3bc0e3b764 39 0,
icraggs 2:bc3bc0e3b764 40 0,
icraggs 2:bc3bc0e3b764 41 };
icraggs 2:bc3bc0e3b764 42
icraggs 2:bc3bc0e3b764 43 void usage()
icraggs 2:bc3bc0e3b764 44 {
icraggs 2:bc3bc0e3b764 45
icraggs 2:bc3bc0e3b764 46 }
icraggs 2:bc3bc0e3b764 47
icraggs 2:bc3bc0e3b764 48 void getopts(int argc, char** argv)
icraggs 2:bc3bc0e3b764 49 {
icraggs 2:bc3bc0e3b764 50 int count = 1;
icraggs 2:bc3bc0e3b764 51
icraggs 2:bc3bc0e3b764 52 while (count < argc)
icraggs 2:bc3bc0e3b764 53 {
icraggs 2:bc3bc0e3b764 54 if (strcmp(argv[count], "--test_no") == 0)
icraggs 2:bc3bc0e3b764 55 {
icraggs 2:bc3bc0e3b764 56 if (++count < argc)
icraggs 2:bc3bc0e3b764 57 options.test_no = atoi(argv[count]);
icraggs 2:bc3bc0e3b764 58 else
icraggs 2:bc3bc0e3b764 59 usage();
icraggs 2:bc3bc0e3b764 60 }
icraggs 2:bc3bc0e3b764 61 else if (strcmp(argv[count], "--connection") == 0)
icraggs 2:bc3bc0e3b764 62 {
icraggs 2:bc3bc0e3b764 63 if (++count < argc)
icraggs 2:bc3bc0e3b764 64 {
icraggs 2:bc3bc0e3b764 65 options.connection = argv[count];
icraggs 2:bc3bc0e3b764 66 printf("\nSetting connection to %s\n", options.connection);
icraggs 2:bc3bc0e3b764 67 }
icraggs 2:bc3bc0e3b764 68 else
icraggs 2:bc3bc0e3b764 69 usage();
icraggs 2:bc3bc0e3b764 70 }
icraggs 2:bc3bc0e3b764 71 else if (strcmp(argv[count], "--haconnections") == 0)
icraggs 2:bc3bc0e3b764 72 {
icraggs 2:bc3bc0e3b764 73 if (++count < argc)
icraggs 2:bc3bc0e3b764 74 {
icraggs 2:bc3bc0e3b764 75 char* tok = strtok(argv[count], " ");
icraggs 2:bc3bc0e3b764 76 options.hacount = 0;
icraggs 2:bc3bc0e3b764 77 options.haconnections = (char**)malloc(sizeof(char*) * 5);
icraggs 2:bc3bc0e3b764 78 while (tok)
icraggs 2:bc3bc0e3b764 79 {
icraggs 2:bc3bc0e3b764 80 options.haconnections[options.hacount] = (char*)malloc(strlen(tok) + 1);
icraggs 2:bc3bc0e3b764 81 strcpy(options.haconnections[options.hacount], tok);
icraggs 2:bc3bc0e3b764 82 options.hacount++;
icraggs 2:bc3bc0e3b764 83 tok = strtok(NULL, " ");
icraggs 2:bc3bc0e3b764 84 }
icraggs 2:bc3bc0e3b764 85 }
icraggs 2:bc3bc0e3b764 86 else
icraggs 2:bc3bc0e3b764 87 usage();
icraggs 2:bc3bc0e3b764 88 }
icraggs 2:bc3bc0e3b764 89 else if (strcmp(argv[count], "--verbose") == 0)
icraggs 2:bc3bc0e3b764 90 {
icraggs 2:bc3bc0e3b764 91 options.verbose = 1;
icraggs 2:bc3bc0e3b764 92 printf("\nSetting verbose on\n");
icraggs 2:bc3bc0e3b764 93 }
icraggs 2:bc3bc0e3b764 94 count++;
icraggs 2:bc3bc0e3b764 95 }
icraggs 2:bc3bc0e3b764 96 }
icraggs 2:bc3bc0e3b764 97
icraggs 2:bc3bc0e3b764 98
icraggs 2:bc3bc0e3b764 99 #define LOGA_DEBUG 0
icraggs 2:bc3bc0e3b764 100 #define LOGA_INFO 1
icraggs 2:bc3bc0e3b764 101 #include <stdarg.h>
icraggs 2:bc3bc0e3b764 102 #include <time.h>
icraggs 2:bc3bc0e3b764 103 //#include <sys/timeb.h>
icraggs 2:bc3bc0e3b764 104 void MyLog(int LOGA_level, char* format, ...)
icraggs 2:bc3bc0e3b764 105 {
icraggs 2:bc3bc0e3b764 106 static char msg_buf[256];
icraggs 2:bc3bc0e3b764 107 va_list args;
icraggs 2:bc3bc0e3b764 108 //struct timeb ts;
icraggs 2:bc3bc0e3b764 109
icraggs 2:bc3bc0e3b764 110 struct tm *timeinfo;
icraggs 2:bc3bc0e3b764 111 time_t rawtime;
icraggs 2:bc3bc0e3b764 112
icraggs 2:bc3bc0e3b764 113 if (LOGA_level == LOGA_DEBUG && options.verbose == 0)
icraggs 2:bc3bc0e3b764 114 return;
icraggs 2:bc3bc0e3b764 115
icraggs 2:bc3bc0e3b764 116 time(&rawtime);
icraggs 2:bc3bc0e3b764 117 timeinfo = localtime(&rawtime);
icraggs 2:bc3bc0e3b764 118 strftime(msg_buf, 80, "%Y%m%d %H%M%S ", timeinfo);
icraggs 2:bc3bc0e3b764 119
icraggs 2:bc3bc0e3b764 120 //sprintf(&msg_buf[strlen(msg_buf)], ".%.3hu ", ts.millitm);
icraggs 2:bc3bc0e3b764 121
icraggs 2:bc3bc0e3b764 122 va_start(args, format);
icraggs 2:bc3bc0e3b764 123 vsnprintf(&msg_buf[strlen(msg_buf)], sizeof(msg_buf) - strlen(msg_buf), format, args);
icraggs 2:bc3bc0e3b764 124 va_end(args);
icraggs 2:bc3bc0e3b764 125
icraggs 2:bc3bc0e3b764 126 printf("%s\n", msg_buf);
icraggs 2:bc3bc0e3b764 127 fflush(stdout);
icraggs 2:bc3bc0e3b764 128 }
icraggs 2:bc3bc0e3b764 129
icraggs 2:bc3bc0e3b764 130
icraggs 2:bc3bc0e3b764 131 #define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d)
icraggs 2:bc3bc0e3b764 132 #define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e)
icraggs 2:bc3bc0e3b764 133
icraggs 2:bc3bc0e3b764 134 int tests = 0;
icraggs 2:bc3bc0e3b764 135 int failures = 0;
icraggs 2:bc3bc0e3b764 136 FILE* xml;
icraggs 2:bc3bc0e3b764 137 mbed::Timer global_time;
icraggs 2:bc3bc0e3b764 138 char output[3000];
icraggs 2:bc3bc0e3b764 139 char* cur_output = output;
icraggs 2:bc3bc0e3b764 140
icraggs 2:bc3bc0e3b764 141
icraggs 2:bc3bc0e3b764 142 void write_test_result()
icraggs 2:bc3bc0e3b764 143 {
icraggs 2:bc3bc0e3b764 144 long duration = global_time.read_ms();
icraggs 2:bc3bc0e3b764 145
icraggs 2:bc3bc0e3b764 146 fprintf(xml, " time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000);
icraggs 2:bc3bc0e3b764 147 if (cur_output != output)
icraggs 2:bc3bc0e3b764 148 {
icraggs 2:bc3bc0e3b764 149 fprintf(xml, "%s", output);
icraggs 2:bc3bc0e3b764 150 cur_output = output;
icraggs 2:bc3bc0e3b764 151 }
icraggs 2:bc3bc0e3b764 152 fprintf(xml, "</testcase>\n");
icraggs 2:bc3bc0e3b764 153 }
icraggs 2:bc3bc0e3b764 154
icraggs 2:bc3bc0e3b764 155
icraggs 2:bc3bc0e3b764 156 void myassert(char* filename, int lineno, char* description, int value, char* format, ...)
icraggs 2:bc3bc0e3b764 157 {
icraggs 2:bc3bc0e3b764 158 ++tests;
icraggs 2:bc3bc0e3b764 159 if (!value)
icraggs 2:bc3bc0e3b764 160 {
icraggs 2:bc3bc0e3b764 161 va_list args;
icraggs 2:bc3bc0e3b764 162
icraggs 2:bc3bc0e3b764 163 ++failures;
icraggs 2:bc3bc0e3b764 164 printf("Assertion failed, file %s, line %d, description: %s\n", filename, lineno, description);
icraggs 2:bc3bc0e3b764 165
icraggs 2:bc3bc0e3b764 166 va_start(args, format);
icraggs 2:bc3bc0e3b764 167 vprintf(format, args);
icraggs 2:bc3bc0e3b764 168 va_end(args);
icraggs 2:bc3bc0e3b764 169
icraggs 2:bc3bc0e3b764 170 cur_output += sprintf(cur_output, "<failure type=\"%s\">file %s, line %d </failure>\n",
icraggs 2:bc3bc0e3b764 171 description, filename, lineno);
icraggs 2:bc3bc0e3b764 172 }
icraggs 2:bc3bc0e3b764 173 else
icraggs 2:bc3bc0e3b764 174 MyLog(LOGA_DEBUG, "Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description);
icraggs 2:bc3bc0e3b764 175 }
icraggs 2:bc3bc0e3b764 176
icraggs 2:bc3bc0e3b764 177 #define min(a, b) ((a < b) ? a : b)
icraggs 2:bc3bc0e3b764 178
icraggs 2:bc3bc0e3b764 179 int checkMQTTStrings(MQTTString a, MQTTString b)
icraggs 2:bc3bc0e3b764 180 {
icraggs 2:bc3bc0e3b764 181 if (!a.lenstring.data)
icraggs 2:bc3bc0e3b764 182 {
icraggs 2:bc3bc0e3b764 183 a.lenstring.data = a.cstring;
icraggs 2:bc3bc0e3b764 184 if (a.cstring)
icraggs 2:bc3bc0e3b764 185 a.lenstring.len = strlen(a.cstring);
icraggs 2:bc3bc0e3b764 186 }
icraggs 2:bc3bc0e3b764 187 if (!b.lenstring.data)
icraggs 2:bc3bc0e3b764 188 {
icraggs 2:bc3bc0e3b764 189 b.lenstring.data = b.cstring;
icraggs 2:bc3bc0e3b764 190 if (b.cstring)
icraggs 2:bc3bc0e3b764 191 b.lenstring.len = strlen(b.cstring);
icraggs 2:bc3bc0e3b764 192 }
icraggs 2:bc3bc0e3b764 193 return memcmp(a.lenstring.data, b.lenstring.data, min(a.lenstring.len, b.lenstring.len)) == 0;
icraggs 2:bc3bc0e3b764 194 }
icraggs 2:bc3bc0e3b764 195
icraggs 2:bc3bc0e3b764 196
icraggs 2:bc3bc0e3b764 197 int checkConnectPackets(MQTTPacket_connectData* before, MQTTPacket_connectData* after)
icraggs 2:bc3bc0e3b764 198 {
icraggs 2:bc3bc0e3b764 199 int rc = 0;
icraggs 2:bc3bc0e3b764 200 int start_failures = failures;
icraggs 2:bc3bc0e3b764 201
icraggs 2:bc3bc0e3b764 202 assert("struct_ids should be the same",
icraggs 2:bc3bc0e3b764 203 memcmp(before->struct_id, after->struct_id, 4) == 0, "struct_ids were different %.4s\n", after->struct_id);
icraggs 2:bc3bc0e3b764 204
icraggs 2:bc3bc0e3b764 205 assert("struct_versions should be the same",
icraggs 2:bc3bc0e3b764 206 before->struct_version == after->struct_version, "struct_versions were different\n", rc);
icraggs 2:bc3bc0e3b764 207
icraggs 2:bc3bc0e3b764 208 assert("MQTT versions should be the same",
icraggs 2:bc3bc0e3b764 209 before->MQTTVersion == after->MQTTVersion, "MQTT versions were different\n", rc);
icraggs 2:bc3bc0e3b764 210
icraggs 2:bc3bc0e3b764 211 assert("ClientIDs should be the same",
icraggs 2:bc3bc0e3b764 212 checkMQTTStrings(before->clientID, after->clientID), "ClientIDs were different\n", rc);
icraggs 2:bc3bc0e3b764 213
icraggs 2:bc3bc0e3b764 214 assert("keepAliveIntervals should be the same",
icraggs 2:bc3bc0e3b764 215 before->keepAliveInterval == after->keepAliveInterval, "keepAliveIntervals were different\n", rc);
icraggs 2:bc3bc0e3b764 216
icraggs 2:bc3bc0e3b764 217 assert("cleansessions should be the same",
icraggs 2:bc3bc0e3b764 218 before->cleansession == after->cleansession, "cleansessions were different\n", rc);
icraggs 2:bc3bc0e3b764 219
icraggs 2:bc3bc0e3b764 220 assert("willFlags should be the same",
icraggs 2:bc3bc0e3b764 221 before->willFlag == after->willFlag, "willFlags were different\n", rc);
icraggs 2:bc3bc0e3b764 222
icraggs 2:bc3bc0e3b764 223 if (before->willFlag)
icraggs 2:bc3bc0e3b764 224 {
icraggs 2:bc3bc0e3b764 225 assert("will struct_ids should be the same",
icraggs 2:bc3bc0e3b764 226 memcmp(before->will.struct_id, after->will.struct_id, 4) == 0, "will struct_ids were different %.4s\n", after->struct_id);
icraggs 2:bc3bc0e3b764 227
icraggs 2:bc3bc0e3b764 228 assert("will struct_versions should be the same",
icraggs 2:bc3bc0e3b764 229 before->will.struct_version == after->will.struct_version, "will struct_versions were different\n", rc);
icraggs 2:bc3bc0e3b764 230
icraggs 2:bc3bc0e3b764 231 assert("topic names should be the same",
icraggs 2:bc3bc0e3b764 232 checkMQTTStrings(before->will.topicName, after->will.topicName), "topic names were different\n", rc);
icraggs 2:bc3bc0e3b764 233
icraggs 2:bc3bc0e3b764 234 assert("messages should be the same",
icraggs 2:bc3bc0e3b764 235 checkMQTTStrings(before->will.message, after->will.message), "messages were different\n", rc);
icraggs 2:bc3bc0e3b764 236
icraggs 2:bc3bc0e3b764 237 assert("retained flags should be the same",
icraggs 2:bc3bc0e3b764 238 before->will.retained == after->will.retained, "retained flags were different\n", rc);
icraggs 2:bc3bc0e3b764 239
icraggs 2:bc3bc0e3b764 240 assert("will qos should be the same",
icraggs 2:bc3bc0e3b764 241 before->will.qos == after->will.qos, "will qos were different\n", rc);
icraggs 2:bc3bc0e3b764 242 }
icraggs 2:bc3bc0e3b764 243
icraggs 2:bc3bc0e3b764 244 assert("usernames should be the same",
icraggs 2:bc3bc0e3b764 245 checkMQTTStrings(before->clientID, after->clientID), "usernames were different\n", rc);
icraggs 2:bc3bc0e3b764 246 assert("passwords should be the same",
icraggs 2:bc3bc0e3b764 247 checkMQTTStrings(before->password, after->password), "passwords were different\n", rc);
icraggs 2:bc3bc0e3b764 248 return failures == start_failures;
icraggs 2:bc3bc0e3b764 249 }
icraggs 2:bc3bc0e3b764 250
icraggs 2:bc3bc0e3b764 251 int test1(struct Options options)
icraggs 2:bc3bc0e3b764 252 {
icraggs 2:bc3bc0e3b764 253 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
icraggs 2:bc3bc0e3b764 254 MQTTPacket_connectData data_after = MQTTPacket_connectData_initializer;
icraggs 2:bc3bc0e3b764 255 int rc = 0;
icraggs 2:bc3bc0e3b764 256 char buf[100];
icraggs 2:bc3bc0e3b764 257 int buflen = sizeof(buf);
icraggs 2:bc3bc0e3b764 258
icraggs 2:bc3bc0e3b764 259 fprintf(xml, "<testcase classname=\"test1\" name=\"de/serialization\"");
icraggs 2:bc3bc0e3b764 260 global_time.reset();
icraggs 2:bc3bc0e3b764 261 failures = 0;
icraggs 2:bc3bc0e3b764 262 MyLog(LOGA_INFO, "Starting test 1 - serialization of connect and back");
icraggs 2:bc3bc0e3b764 263
icraggs 2:bc3bc0e3b764 264 data.clientID.cstring = "me";
icraggs 2:bc3bc0e3b764 265
icraggs 2:bc3bc0e3b764 266 data.keepAliveInterval = 20;
icraggs 2:bc3bc0e3b764 267 data.cleansession = 1;
icraggs 2:bc3bc0e3b764 268 data.username.cstring = "testuser";
icraggs 2:bc3bc0e3b764 269 data.password.cstring = "testpassword";
icraggs 2:bc3bc0e3b764 270
icraggs 2:bc3bc0e3b764 271 data.willFlag = 1;
icraggs 2:bc3bc0e3b764 272 data.will.message.cstring = "will message";
icraggs 2:bc3bc0e3b764 273 data.will.qos = 1;
icraggs 2:bc3bc0e3b764 274 data.will.retained = 0;
icraggs 2:bc3bc0e3b764 275 data.will.topicName.cstring = "will topic";
icraggs 2:bc3bc0e3b764 276
icraggs 2:bc3bc0e3b764 277 rc = MQTTSerialize_connect(buf, buflen, &data);
icraggs 2:bc3bc0e3b764 278 assert("good rc from serialize connect", rc > 0, "rc was %d\n", rc);
icraggs 2:bc3bc0e3b764 279
icraggs 2:bc3bc0e3b764 280 rc = MQTTDeserialize_connect(&data_after, buf, buflen);
icraggs 2:bc3bc0e3b764 281 assert("good rc from deserialize connect", rc == 1, "rc was %d\n", rc);
icraggs 2:bc3bc0e3b764 282
icraggs 2:bc3bc0e3b764 283 /* data after should be the same as data before */
icraggs 2:bc3bc0e3b764 284 rc = checkConnectPackets(&data, &data_after);
icraggs 2:bc3bc0e3b764 285 assert("packets should be the same", rc == 1, "packets were different\n", rc);
icraggs 2:bc3bc0e3b764 286
icraggs 2:bc3bc0e3b764 287 /* exit: */
icraggs 2:bc3bc0e3b764 288 MyLog(LOGA_INFO, "TEST1: test %s. %d tests run, %d failures.",
icraggs 2:bc3bc0e3b764 289 (failures == 0) ? "passed" : "failed", tests, failures);
icraggs 2:bc3bc0e3b764 290 write_test_result();
icraggs 2:bc3bc0e3b764 291 return failures;
icraggs 2:bc3bc0e3b764 292 }
icraggs 2:bc3bc0e3b764 293
icraggs 2:bc3bc0e3b764 294
icraggs 2:bc3bc0e3b764 295 int test2(struct Options options)
icraggs 2:bc3bc0e3b764 296 {
icraggs 2:bc3bc0e3b764 297 int rc = 0;
icraggs 2:bc3bc0e3b764 298 char buf[100];
icraggs 2:bc3bc0e3b764 299 int buflen = sizeof(buf);
icraggs 2:bc3bc0e3b764 300
icraggs 2:bc3bc0e3b764 301 int dup = 0;
icraggs 2:bc3bc0e3b764 302 int qos = 2;
icraggs 2:bc3bc0e3b764 303 int retained = 0;
icraggs 2:bc3bc0e3b764 304 int msgid = 23;
icraggs 2:bc3bc0e3b764 305 MQTTString topicString = MQTTString_initializer;
icraggs 2:bc3bc0e3b764 306 char *payload = "kkhkhkjkj jkjjk jk jk ";
icraggs 2:bc3bc0e3b764 307 int payloadlen = strlen(payload);
icraggs 2:bc3bc0e3b764 308
icraggs 2:bc3bc0e3b764 309 int dup2 = 1;
icraggs 2:bc3bc0e3b764 310 int qos2 = 1;
icraggs 2:bc3bc0e3b764 311 int retained2 = 1;
icraggs 2:bc3bc0e3b764 312 int msgid2 = 3243;
icraggs 2:bc3bc0e3b764 313 MQTTString topicString2 = MQTTString_initializer;
icraggs 2:bc3bc0e3b764 314 char *payload2 = NULL;
icraggs 2:bc3bc0e3b764 315 int payloadlen2 = 0;
icraggs 2:bc3bc0e3b764 316
icraggs 2:bc3bc0e3b764 317 fprintf(xml, "<testcase classname=\"test1\" name=\"de/serialization\"");
icraggs 2:bc3bc0e3b764 318 global_time.reset();
icraggs 2:bc3bc0e3b764 319 failures = 0;
icraggs 2:bc3bc0e3b764 320 MyLog(LOGA_INFO, "Starting test 2 - serialization of publish and back");
icraggs 2:bc3bc0e3b764 321
icraggs 2:bc3bc0e3b764 322 topicString.cstring = "mytopic";
icraggs 2:bc3bc0e3b764 323 rc = MQTTSerialize_publish(buf, buflen, dup, qos, retained, msgid, topicString,
icraggs 2:bc3bc0e3b764 324 payload, payloadlen);
icraggs 2:bc3bc0e3b764 325 assert("good rc from serialize publish", rc > 0, "rc was %d\n", rc);
icraggs 2:bc3bc0e3b764 326
icraggs 2:bc3bc0e3b764 327 rc = MQTTDeserialize_publish(&dup2, &qos2, &retained2, &msgid2, &topicString2,
icraggs 2:bc3bc0e3b764 328 &payload2, &payloadlen2, buf, buflen);
icraggs 2:bc3bc0e3b764 329 assert("good rc from deserialize publish", rc == 1, "rc was %d\n", rc);
icraggs 2:bc3bc0e3b764 330
icraggs 2:bc3bc0e3b764 331 /* data after should be the same as data before */
icraggs 2:bc3bc0e3b764 332 assert("dups should be the same", dup == dup2, "dups were different %d\n", dup2);
icraggs 2:bc3bc0e3b764 333 assert("qoss should be the same", qos == qos2, "qoss were different %d\n", qos2);
icraggs 2:bc3bc0e3b764 334 assert("retaineds should be the same", retained == retained2, "retaineds were different %d\n", retained2);
icraggs 2:bc3bc0e3b764 335 assert("msgids should be the same", msgid == msgid2, "msgids were different %d\n", msgid2);
icraggs 2:bc3bc0e3b764 336
icraggs 2:bc3bc0e3b764 337 assert("topics should be the same",
icraggs 2:bc3bc0e3b764 338 checkMQTTStrings(topicString, topicString2), "topics were different %s\n", ""); //topicString2);
icraggs 2:bc3bc0e3b764 339
icraggs 2:bc3bc0e3b764 340 assert("payload lengths should be the same",
icraggs 2:bc3bc0e3b764 341 payloadlen == payloadlen2, "payload lengths were different %d\n", payloadlen2);
icraggs 2:bc3bc0e3b764 342
icraggs 2:bc3bc0e3b764 343 assert("payloads should be the same",
icraggs 2:bc3bc0e3b764 344 memcmp(payload, payload2, payloadlen) == 0, "payloads were different %s\n", "");
icraggs 2:bc3bc0e3b764 345
icraggs 2:bc3bc0e3b764 346 /*exit:*/
icraggs 2:bc3bc0e3b764 347 MyLog(LOGA_INFO, "TEST2: test %s. %d tests run, %d failures.",
icraggs 2:bc3bc0e3b764 348 (failures == 0) ? "passed" : "failed", tests, failures);
icraggs 2:bc3bc0e3b764 349 write_test_result();
icraggs 2:bc3bc0e3b764 350 return failures;
icraggs 2:bc3bc0e3b764 351 }
icraggs 2:bc3bc0e3b764 352
icraggs 2:bc3bc0e3b764 353
icraggs 2:bc3bc0e3b764 354
icraggs 2:bc3bc0e3b764 355 int test3(struct Options options)
icraggs 2:bc3bc0e3b764 356 {
icraggs 2:bc3bc0e3b764 357 int i = 0;
icraggs 2:bc3bc0e3b764 358 int rc = 0;
icraggs 2:bc3bc0e3b764 359 char buf[100];
icraggs 2:bc3bc0e3b764 360 int buflen = sizeof(buf);
icraggs 2:bc3bc0e3b764 361 #define TOPIC_COUNT 2
icraggs 2:bc3bc0e3b764 362
icraggs 2:bc3bc0e3b764 363 int dup = 0;
icraggs 2:bc3bc0e3b764 364 int msgid = 23;
icraggs 2:bc3bc0e3b764 365 int count = TOPIC_COUNT;
icraggs 2:bc3bc0e3b764 366 MQTTString topicStrings[TOPIC_COUNT] = { MQTTString_initializer, MQTTString_initializer };
icraggs 2:bc3bc0e3b764 367 int req_qoss[TOPIC_COUNT] = {2, 1};
icraggs 2:bc3bc0e3b764 368
icraggs 2:bc3bc0e3b764 369 int dup2 = 1;
icraggs 2:bc3bc0e3b764 370 int msgid2 = 2223;
icraggs 2:bc3bc0e3b764 371 int count2 = 0;
icraggs 2:bc3bc0e3b764 372 MQTTString topicStrings2[TOPIC_COUNT] = { MQTTString_initializer, MQTTString_initializer };
icraggs 2:bc3bc0e3b764 373 int req_qoss2[TOPIC_COUNT] = {0, 0};
icraggs 2:bc3bc0e3b764 374
icraggs 2:bc3bc0e3b764 375 fprintf(xml, "<testcase classname=\"test1\" name=\"de/serialization\"");
icraggs 2:bc3bc0e3b764 376 global_time.reset();
icraggs 2:bc3bc0e3b764 377 failures = 0;
icraggs 2:bc3bc0e3b764 378 MyLog(LOGA_INFO, "Starting test 2 - serialization of subscribe and back");
icraggs 2:bc3bc0e3b764 379
icraggs 2:bc3bc0e3b764 380 topicStrings[0].cstring = "mytopic";
icraggs 2:bc3bc0e3b764 381 topicStrings[1].cstring = "mytopic2";
icraggs 2:bc3bc0e3b764 382 rc = MQTTSerialize_subscribe(buf, buflen, dup, msgid, count, topicStrings, req_qoss);
icraggs 2:bc3bc0e3b764 383 assert("good rc from serialize subscribe", rc > 0, "rc was %d\n", rc);
icraggs 2:bc3bc0e3b764 384
icraggs 2:bc3bc0e3b764 385 rc = MQTTDeserialize_subscribe(&dup2, &msgid2, 2, &count2, topicStrings2, req_qoss2, buf, buflen);
icraggs 2:bc3bc0e3b764 386 assert("good rc from deserialize subscribe", rc == 1, "rc was %d\n", rc);
icraggs 2:bc3bc0e3b764 387
icraggs 2:bc3bc0e3b764 388 /* data after should be the same as data before */
icraggs 2:bc3bc0e3b764 389 assert("dups should be the same", dup == dup2, "dups were different %d\n", dup2);
icraggs 2:bc3bc0e3b764 390 assert("msgids should be the same", msgid == msgid2, "msgids were different %d\n", msgid2);
icraggs 2:bc3bc0e3b764 391
icraggs 2:bc3bc0e3b764 392 assert("count should be the same", count == count2, "counts were different %d\n", count2);
icraggs 2:bc3bc0e3b764 393
icraggs 2:bc3bc0e3b764 394 for (i = 0; i < count2; ++i)
icraggs 2:bc3bc0e3b764 395 {
icraggs 2:bc3bc0e3b764 396 assert("topics should be the same",
icraggs 2:bc3bc0e3b764 397 checkMQTTStrings(topicStrings[i], topicStrings2[i]), "topics were different %s\n", "");
icraggs 2:bc3bc0e3b764 398
icraggs 2:bc3bc0e3b764 399 assert("qoss should be the same", req_qoss[i] == req_qoss2[i], "qoss were different %d\n", req_qoss2[i]);
icraggs 2:bc3bc0e3b764 400 }
icraggs 2:bc3bc0e3b764 401
icraggs 2:bc3bc0e3b764 402 /*exit:*/
icraggs 2:bc3bc0e3b764 403 MyLog(LOGA_INFO, "TEST3: test %s. %d tests run, %d failures.",
icraggs 2:bc3bc0e3b764 404 (failures == 0) ? "passed" : "failed", tests, failures);
icraggs 2:bc3bc0e3b764 405 write_test_result();
icraggs 2:bc3bc0e3b764 406 return failures;
icraggs 2:bc3bc0e3b764 407 }
icraggs 2:bc3bc0e3b764 408
icraggs 2:bc3bc0e3b764 409
icraggs 2:bc3bc0e3b764 410 int test4(struct Options options)
icraggs 2:bc3bc0e3b764 411 {
icraggs 2:bc3bc0e3b764 412 int i = 0;
icraggs 2:bc3bc0e3b764 413 int rc = 0;
icraggs 2:bc3bc0e3b764 414 char buf[100];
icraggs 2:bc3bc0e3b764 415 int buflen = sizeof(buf);
icraggs 2:bc3bc0e3b764 416 #define TOPIC_COUNT 2
icraggs 2:bc3bc0e3b764 417
icraggs 2:bc3bc0e3b764 418 int msgid = 23;
icraggs 2:bc3bc0e3b764 419 int count = TOPIC_COUNT;
icraggs 2:bc3bc0e3b764 420 int granted_qoss[TOPIC_COUNT] = {2, 1};
icraggs 2:bc3bc0e3b764 421 ;
icraggs 2:bc3bc0e3b764 422 int msgid2 = 2223;
icraggs 2:bc3bc0e3b764 423 int count2 = 0;
icraggs 2:bc3bc0e3b764 424 int granted_qoss2[TOPIC_COUNT] = {0, 0};
icraggs 2:bc3bc0e3b764 425
icraggs 2:bc3bc0e3b764 426 fprintf(xml, "<testcase classname=\"test1\" name=\"de/serialization\"");
icraggs 2:bc3bc0e3b764 427 global_time.reset();
icraggs 2:bc3bc0e3b764 428 failures = 0;
icraggs 2:bc3bc0e3b764 429 MyLog(LOGA_INFO, "Starting test 4 - serialization of suback and back");
icraggs 2:bc3bc0e3b764 430
icraggs 2:bc3bc0e3b764 431 rc = MQTTSerialize_suback(buf, buflen, msgid, count, granted_qoss);
icraggs 2:bc3bc0e3b764 432 assert("good rc from serialize suback", rc > 0, "rc was %d\n", rc);
icraggs 2:bc3bc0e3b764 433
icraggs 2:bc3bc0e3b764 434 rc = MQTTDeserialize_suback(&msgid2, 2, &count2, granted_qoss2, buf, buflen);
icraggs 2:bc3bc0e3b764 435 assert("good rc from deserialize suback", rc == 1, "rc was %d\n", rc);
icraggs 2:bc3bc0e3b764 436
icraggs 2:bc3bc0e3b764 437 /* data after should be the same as data before */
icraggs 2:bc3bc0e3b764 438 assert("msgids should be the same", msgid == msgid2, "msgids were different %d\n", msgid2);
icraggs 2:bc3bc0e3b764 439
icraggs 2:bc3bc0e3b764 440 assert("count should be the same", count == count2, "counts were different %d\n", count2);
icraggs 2:bc3bc0e3b764 441
icraggs 2:bc3bc0e3b764 442 for (i = 0; i < count2; ++i)
icraggs 2:bc3bc0e3b764 443 assert("qoss should be the same", granted_qoss[i] == granted_qoss2[i], "qoss were different %d\n", granted_qoss2[i]);
icraggs 2:bc3bc0e3b764 444
icraggs 2:bc3bc0e3b764 445 /* exit: */
icraggs 2:bc3bc0e3b764 446 MyLog(LOGA_INFO, "TEST4: test %s. %d tests run, %d failures.",
icraggs 2:bc3bc0e3b764 447 (failures == 0) ? "passed" : "failed", tests, failures);
icraggs 2:bc3bc0e3b764 448 write_test_result();
icraggs 2:bc3bc0e3b764 449 return failures;
icraggs 2:bc3bc0e3b764 450 }
icraggs 2:bc3bc0e3b764 451
icraggs 2:bc3bc0e3b764 452
icraggs 2:bc3bc0e3b764 453 int test5(struct Options options)
icraggs 2:bc3bc0e3b764 454 {
icraggs 2:bc3bc0e3b764 455 int i = 0;
icraggs 2:bc3bc0e3b764 456 int rc = 0;
icraggs 2:bc3bc0e3b764 457 char buf[100];
icraggs 2:bc3bc0e3b764 458 int buflen = sizeof(buf);
icraggs 2:bc3bc0e3b764 459 #define TOPIC_COUNT 2
icraggs 2:bc3bc0e3b764 460
icraggs 2:bc3bc0e3b764 461 int dup = 0;
icraggs 2:bc3bc0e3b764 462 int msgid = 23;
icraggs 2:bc3bc0e3b764 463 int count = TOPIC_COUNT;
icraggs 2:bc3bc0e3b764 464 MQTTString topicStrings[TOPIC_COUNT] = { MQTTString_initializer, MQTTString_initializer };
icraggs 2:bc3bc0e3b764 465
icraggs 2:bc3bc0e3b764 466 int dup2 = 1;
icraggs 2:bc3bc0e3b764 467 int msgid2 = 2223;
icraggs 2:bc3bc0e3b764 468 int count2 = 0;
icraggs 2:bc3bc0e3b764 469 MQTTString topicStrings2[TOPIC_COUNT] = { MQTTString_initializer, MQTTString_initializer };
icraggs 2:bc3bc0e3b764 470
icraggs 2:bc3bc0e3b764 471 fprintf(xml, "<testcase classname=\"test1\" name=\"de/serialization\"");
icraggs 2:bc3bc0e3b764 472 global_time.reset();
icraggs 2:bc3bc0e3b764 473 failures = 0;
icraggs 2:bc3bc0e3b764 474 MyLog(LOGA_INFO, "Starting test 2 - serialization of unsubscribe and back");
icraggs 2:bc3bc0e3b764 475
icraggs 2:bc3bc0e3b764 476 topicStrings[0].cstring = "mytopic";
icraggs 2:bc3bc0e3b764 477 topicStrings[1].cstring = "mytopic2";
icraggs 2:bc3bc0e3b764 478 rc = MQTTSerialize_unsubscribe(buf, buflen, dup, msgid, count, topicStrings);
icraggs 2:bc3bc0e3b764 479 assert("good rc from serialize unsubscribe", rc > 0, "rc was %d\n", rc);
icraggs 2:bc3bc0e3b764 480
icraggs 2:bc3bc0e3b764 481 rc = MQTTDeserialize_unsubscribe(&dup2, &msgid2, 2, &count2, topicStrings2, buf, buflen);
icraggs 2:bc3bc0e3b764 482 assert("good rc from deserialize unsubscribe", rc == 1, "rc was %d\n", rc);
icraggs 2:bc3bc0e3b764 483
icraggs 2:bc3bc0e3b764 484 /* data after should be the same as data before */
icraggs 2:bc3bc0e3b764 485 assert("dups should be the same", dup == dup2, "dups were different %d\n", dup2);
icraggs 2:bc3bc0e3b764 486 assert("msgids should be the same", msgid == msgid2, "msgids were different %d\n", msgid2);
icraggs 2:bc3bc0e3b764 487
icraggs 2:bc3bc0e3b764 488 assert("count should be the same", count == count2, "counts were different %d\n", count2);
icraggs 2:bc3bc0e3b764 489
icraggs 2:bc3bc0e3b764 490 for (i = 0; i < count2; ++i)
icraggs 2:bc3bc0e3b764 491 assert("topics should be the same",
icraggs 2:bc3bc0e3b764 492 checkMQTTStrings(topicStrings[i], topicStrings2[i]), "topics were different %s\n", "");
icraggs 2:bc3bc0e3b764 493
icraggs 2:bc3bc0e3b764 494 /* exit: */
icraggs 2:bc3bc0e3b764 495 MyLog(LOGA_INFO, "TEST5: test %s. %d tests run, %d failures.",
icraggs 2:bc3bc0e3b764 496 (failures == 0) ? "passed" : "failed", tests, failures);
icraggs 2:bc3bc0e3b764 497 write_test_result();
icraggs 2:bc3bc0e3b764 498 return failures;
icraggs 2:bc3bc0e3b764 499 }
icraggs 2:bc3bc0e3b764 500
icraggs 2:bc3bc0e3b764 501
icraggs 2:bc3bc0e3b764 502 int test6(struct Options options)
icraggs 2:bc3bc0e3b764 503 {
icraggs 2:bc3bc0e3b764 504 int rc = 0;
icraggs 2:bc3bc0e3b764 505 char buf[100];
icraggs 2:bc3bc0e3b764 506 int buflen = sizeof(buf);
icraggs 2:bc3bc0e3b764 507
icraggs 2:bc3bc0e3b764 508 int connack_rc = 77;
icraggs 2:bc3bc0e3b764 509
icraggs 2:bc3bc0e3b764 510 int connack_rc2 = 0;
icraggs 2:bc3bc0e3b764 511
icraggs 2:bc3bc0e3b764 512 fprintf(xml, "<testcase classname=\"test1\" name=\"de/serialization\"");
icraggs 2:bc3bc0e3b764 513 global_time.reset();
icraggs 2:bc3bc0e3b764 514 failures = 0;
icraggs 2:bc3bc0e3b764 515 MyLog(LOGA_INFO, "Starting test 2 - serialization of connack and back");
icraggs 2:bc3bc0e3b764 516
icraggs 2:bc3bc0e3b764 517 rc = MQTTSerialize_connack(buf, buflen, connack_rc);
icraggs 2:bc3bc0e3b764 518 assert("good rc from serialize connack", rc > 0, "rc was %d\n", rc);
icraggs 2:bc3bc0e3b764 519
icraggs 2:bc3bc0e3b764 520 rc = MQTTDeserialize_connack(&connack_rc2, buf, buflen);
icraggs 2:bc3bc0e3b764 521 assert("good rc from deserialize connack", rc == 1, "rc was %d\n", rc);
icraggs 2:bc3bc0e3b764 522
icraggs 2:bc3bc0e3b764 523 /* data after should be the same as data before */
icraggs 2:bc3bc0e3b764 524 assert("dups should be the same", connack_rc == connack_rc2, "dups were different %d\n", connack_rc2);
icraggs 2:bc3bc0e3b764 525
icraggs 2:bc3bc0e3b764 526 /* exit: */
icraggs 2:bc3bc0e3b764 527 MyLog(LOGA_INFO, "TEST6: test %s. %d tests run, %d failures.",
icraggs 2:bc3bc0e3b764 528 (failures == 0) ? "passed" : "failed", tests, failures);
icraggs 2:bc3bc0e3b764 529 write_test_result();
icraggs 2:bc3bc0e3b764 530 return failures;
icraggs 2:bc3bc0e3b764 531 }
icraggs 2:bc3bc0e3b764 532
icraggs 2:bc3bc0e3b764 533
icraggs 2:bc3bc0e3b764 534 int main(int argc, char** argv)
icraggs 2:bc3bc0e3b764 535 {
icraggs 2:bc3bc0e3b764 536 int rc = 0;
icraggs 2:bc3bc0e3b764 537 int (*tests[])(Options) = {NULL, test1, test2, test3, test4, test5, test6};
icraggs 2:bc3bc0e3b764 538
icraggs 2:bc3bc0e3b764 539 xml = fopen("TEST-test1.xml", "w");
icraggs 2:bc3bc0e3b764 540 fprintf(xml, "<testsuite name=\"test1\" tests=\"%d\">\n", (int)(ARRAY_SIZE(tests) - 1));
icraggs 2:bc3bc0e3b764 541
icraggs 2:bc3bc0e3b764 542 getopts(argc, argv);
icraggs 2:bc3bc0e3b764 543
icraggs 2:bc3bc0e3b764 544 if (options.test_no == 0)
icraggs 2:bc3bc0e3b764 545 { /* run all the tests */
icraggs 2:bc3bc0e3b764 546 for (options.test_no = 1; options.test_no < ARRAY_SIZE(tests); ++options.test_no)
icraggs 2:bc3bc0e3b764 547 rc += tests[options.test_no](options); /* return number of failures. 0 = test succeeded */
icraggs 2:bc3bc0e3b764 548 }
icraggs 2:bc3bc0e3b764 549 else
icraggs 2:bc3bc0e3b764 550 rc = tests[options.test_no](options); /* run just the selected test */
icraggs 2:bc3bc0e3b764 551
icraggs 2:bc3bc0e3b764 552 if (rc == 0)
icraggs 2:bc3bc0e3b764 553 MyLog(LOGA_INFO, "verdict pass");
icraggs 2:bc3bc0e3b764 554 else
icraggs 2:bc3bc0e3b764 555 MyLog(LOGA_INFO, "verdict fail");
icraggs 2:bc3bc0e3b764 556
icraggs 2:bc3bc0e3b764 557 fprintf(xml, "</testsuite>\n");
icraggs 2:bc3bc0e3b764 558 fclose(xml);
icraggs 2:bc3bc0e3b764 559 return rc;
icraggs 2:bc3bc0e3b764 560 }