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