Port to C027 (using AppShield and Ethernet)

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Committer:
samdanbury
Date:
Wed Aug 20 12:45:14 2014 +0000
Revision:
6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application

Who changed what in which revision?

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