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