Jack Hansdampf / mbed-mqtt-GSOE1

Dependents:   ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers test2.c Source File

test2.c

00001 /*******************************************************************************
00002  * Copyright (c) 2014 IBM Corp.
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    Ian Craggs - initial API and implementation and/or initial documentation
00015  *******************************************************************************/
00016 
00017 
00018 #include "MQTTSNPacket.h"
00019 
00020 #include <sys/types.h>
00021 
00022 #if !defined(SOCKET_ERROR)
00023     /** error in socket operation */
00024     #define SOCKET_ERROR -1
00025 #endif
00026 
00027 #include <string.h>
00028 #include <stdlib.h>
00029 #include <stdio.h>
00030 
00031 #if !defined(_WINDOWS)
00032 #define INVALID_SOCKET SOCKET_ERROR
00033 #include <sys/time.h>
00034 #include <sys/socket.h>
00035 #include <unistd.h>
00036 #include <errno.h>
00037 #include <netinet/in.h>
00038 #include <netinet/tcp.h>
00039 #include <arpa/inet.h>
00040 #else
00041 #include <winsock2.h>
00042 #include <ws2tcpip.h>
00043 #define MAXHOSTNAMELEN 256
00044 #define EAGAIN WSAEWOULDBLOCK
00045 #define EINTR WSAEINTR
00046 #define EINPROGRESS WSAEINPROGRESS
00047 #define EWOULDBLOCK WSAEWOULDBLOCK
00048 #define ENOTCONN WSAENOTCONN
00049 #define ECONNRESET WSAECONNRESET
00050 #endif
00051 
00052 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
00053 
00054 struct Options
00055 {
00056     char* host;  
00057     int port;
00058     int verbose;
00059     int test_no;
00060 } options =
00061 {
00062     "127.0.0.1",
00063     1884,
00064     0,
00065     0,
00066 };
00067 
00068 void usage()
00069 {
00070 
00071 }
00072 
00073 void getopts(int argc, char** argv)
00074 {
00075     int count = 1;
00076 
00077     while (count < argc)
00078     {
00079         if (strcmp(argv[count], "--test_no") == 0)
00080         {
00081             if (++count < argc)
00082                 options.test_no = atoi(argv[count]);
00083             else
00084                 usage();
00085         }
00086         else if (strcmp(argv[count], "--host") == 0)
00087         {
00088             if (++count < argc)
00089             {
00090                 options.host = argv[count];
00091                 printf("\nSetting host to %s\n", options.host);
00092             }
00093             else
00094                 usage();
00095         }
00096         else if (strcmp(argv[count], "--port") == 0)
00097         {
00098             if (++count < argc)
00099                 options.port = atoi(argv[count]);
00100             else
00101                 usage();
00102         }
00103         else if (strcmp(argv[count], "--verbose") == 0)
00104         {
00105             options.verbose = 1;
00106             printf("\nSetting verbose on\n");
00107         }
00108         count++;
00109     }
00110 }
00111 
00112 
00113 #define LOGA_DEBUG 0
00114 #define LOGA_INFO 1
00115 #include <stdarg.h>
00116 #include <time.h>
00117 #include <sys/timeb.h>
00118 void MyLog(int LOGA_level, char* format, ...)
00119 {
00120     static char msg_buf[256];
00121     va_list args;
00122     struct timeb ts;
00123 
00124     struct tm *timeinfo;
00125 
00126     if (LOGA_level == LOGA_DEBUG && options.verbose == 0)
00127       return;
00128 
00129     ftime(&ts);
00130     timeinfo = localtime(&ts.time);
00131     strftime(msg_buf, 80, "%Y%m%d %H%M%S", timeinfo);
00132 
00133     sprintf(&msg_buf[strlen(msg_buf)], ".%.3hu ", ts.millitm);
00134 
00135     va_start(args, format);
00136     vsnprintf(&msg_buf[strlen(msg_buf)], sizeof(msg_buf) - strlen(msg_buf), format, args);
00137     va_end(args);
00138 
00139     printf("%s\n", msg_buf);
00140     fflush(stdout);
00141 }
00142 
00143 
00144 #if defined(WIN32) || defined(_WINDOWS)
00145 #define mqsleep(A) Sleep(1000*A)
00146 #define START_TIME_TYPE DWORD
00147 static DWORD start_time = 0;
00148 START_TIME_TYPE start_clock(void)
00149 {
00150     return GetTickCount();
00151 }
00152 #elif defined(AIX)
00153 #define mqsleep sleep
00154 #define START_TIME_TYPE struct timespec
00155 START_TIME_TYPE start_clock(void)
00156 {
00157     static struct timespec start;
00158     clock_gettime(CLOCK_REALTIME, &start);
00159     return start;
00160 }
00161 #else
00162 #define mqsleep sleep
00163 #define START_TIME_TYPE struct timeval
00164 /* TODO - unused - remove? static struct timeval start_time; */
00165 START_TIME_TYPE start_clock(void)
00166 {
00167     struct timeval start_time;
00168     gettimeofday(&start_time, NULL);
00169     return start_time;
00170 }
00171 #endif
00172 
00173 
00174 #if defined(WIN32)
00175 long elapsed(START_TIME_TYPE start_time)
00176 {
00177     return GetTickCount() - start_time;
00178 }
00179 #elif defined(AIX)
00180 #define assert(a)
00181 long elapsed(struct timespec start)
00182 {
00183     struct timespec now, res;
00184 
00185     clock_gettime(CLOCK_REALTIME, &now);
00186     ntimersub(now, start, res);
00187     return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L;
00188 }
00189 #else
00190 long elapsed(START_TIME_TYPE start_time)
00191 {
00192     struct timeval now, res;
00193 
00194     gettimeofday(&now, NULL);
00195     timersub(&now, &start_time, &res);
00196     return (res.tv_sec)*1000 + (res.tv_usec)/1000;
00197 }
00198 #endif
00199 
00200 
00201 #define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d)
00202 #define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e)
00203 
00204 int tests = 0;
00205 int failures = 0;
00206 FILE* xml;
00207 START_TIME_TYPE global_start_time;
00208 char output[3000];
00209 char* cur_output = output;
00210 
00211 
00212 void write_test_result()
00213 {
00214     long duration = elapsed(global_start_time);
00215 
00216     fprintf(xml, " time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000);
00217     if (cur_output != output)
00218     {
00219         fprintf(xml, "%s", output);
00220         cur_output = output;
00221     }
00222     fprintf(xml, "</testcase>\n");
00223 }
00224 
00225 
00226 void myassert(char* filename, int lineno, char* description, int value, char* format, ...)
00227 {
00228     ++tests;
00229     if (!value)
00230     {
00231         va_list args;
00232 
00233         ++failures;
00234         printf("Assertion failed, file %s, line %d, description: %s\n", filename, lineno, description);
00235 
00236         va_start(args, format);
00237         vprintf(format, args);
00238         va_end(args);
00239 
00240         cur_output += sprintf(cur_output, "<failure type=\"%s\">file %s, line %d </failure>\n",
00241                         description, filename, lineno);
00242     }
00243     else
00244         MyLog(LOGA_DEBUG, "Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description);
00245 }
00246 
00247 #define min(a, b) ((a < b) ? a : b)
00248 
00249 int Socket_error(char* aString, int sock)
00250 {
00251 #if defined(WIN32)
00252     int errno;
00253 #endif
00254 
00255 #if defined(WIN32)
00256     errno = WSAGetLastError();
00257 #endif
00258     if (errno != EINTR && errno != EAGAIN && errno != EINPROGRESS && errno != EWOULDBLOCK)
00259     {
00260         if (strcmp(aString, "shutdown") != 0 || (errno != ENOTCONN && errno != ECONNRESET))
00261         {
00262             int orig_errno = errno;
00263             char* errmsg = strerror(errno);
00264 
00265             printf("Socket error %d (%s) in %s for socket %d\n", orig_errno, errmsg, aString, sock);
00266         }
00267     }
00268     return errno;
00269 }
00270 
00271 
00272 int sendPacketBuffer(int asocket, char* host, int port, unsigned char* buf, int buflen)
00273 {
00274     struct sockaddr_in cliaddr;
00275     int rc = 0;
00276 
00277     memset(&cliaddr, 0, sizeof(cliaddr));
00278     cliaddr.sin_family = AF_INET;
00279     cliaddr.sin_addr.s_addr = inet_addr(host);
00280     cliaddr.sin_port = htons(port);
00281 
00282     if ((rc = sendto(asocket, buf, buflen, 0, (const struct sockaddr*)&cliaddr, sizeof(cliaddr))) == SOCKET_ERROR)
00283         Socket_error("sendto", asocket);
00284     else
00285         rc = 0;
00286     return rc;
00287 }
00288 
00289 
00290 int mysock = 0;
00291 
00292 int getdata(unsigned char* buf, int count)
00293 {
00294     int rc = recvfrom(mysock, buf, count, 0, NULL, NULL);
00295     //printf("received %d bytes count %d\n", rc, (int)count);
00296     return rc;
00297 }
00298 
00299 
00300 int connectDisconnect(struct Options options)
00301 {
00302     MQTTSNPacket_connectData data = MQTTSNPacket_connectData_initializer;
00303     int rc = 0;
00304     unsigned char buf[100];
00305     int buflen = sizeof(buf);
00306     int len = 0;
00307 
00308     mysock = socket(AF_INET, SOCK_DGRAM, 0);
00309     if (mysock == INVALID_SOCKET)
00310         rc = Socket_error("socket", mysock);
00311 
00312     data.clientID.cstring = "test2/test1";
00313 
00314     data.cleansession = 0;
00315 
00316     rc = MQTTSNSerialize_connect(buf, buflen, &data);
00317     assert("good rc from serialize connect", rc > 0, "rc was %d\n", rc);
00318 
00319     rc = sendPacketBuffer(mysock, options.host, options.port, buf, rc);
00320     assert("good rc from sendPacketBuffer", rc == 0, "rc was %d\n", rc);
00321 
00322     /* wait for connack */
00323     if (MQTTSNPacket_read(buf, buflen, getdata) == MQTTSN_CONNACK)
00324     {
00325         int connack_rc = -1;
00326 
00327         if (MQTTSNDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0)
00328         {
00329             printf("Unable to connect, return code %d\n", connack_rc);
00330             goto exit;
00331         }
00332         else 
00333             printf("connected rc %d\n", connack_rc);
00334     }
00335     else
00336         goto exit;
00337 
00338     len = MQTTSNSerialize_disconnect(buf, buflen, 0);
00339     rc = sendPacketBuffer(mysock, options.host, options.port, buf, len);
00340 
00341     rc = shutdown(mysock, SHUT_WR);
00342     rc = close(mysock);
00343 
00344 exit:
00345     return rc;
00346 }
00347 
00348 int test1(struct Options options)
00349 {
00350 
00351     fprintf(xml, "<testcase classname=\"test1\" name=\"reconnect\"");
00352     global_start_time = start_clock();
00353     failures = 0;
00354     MyLog(LOGA_INFO, "Starting test 1 - reconnection");
00355 
00356     connectDisconnect(options);
00357     connectDisconnect(options);
00358 
00359     MyLog(LOGA_INFO, "TEST1: test %s. %d tests run, %d failures.",
00360             (failures == 0) ? "passed" : "failed", tests, failures);
00361     write_test_result();
00362     return failures;
00363 }
00364 
00365 
00366 int connectSubscribeDisconnect(struct Options options)
00367 {
00368     MQTTSNPacket_connectData data = MQTTSNPacket_connectData_initializer;
00369     int rc = 0;
00370     unsigned char buf[100];
00371     int buflen = sizeof(buf);
00372     int len = 0;
00373     MQTTSN_topicid topic;
00374 
00375     mysock = socket(AF_INET, SOCK_DGRAM, 0);
00376     if (mysock == INVALID_SOCKET)
00377         rc = Socket_error("socket", mysock);
00378 
00379     data.clientID.cstring = "test2/test1";
00380 
00381     data.cleansession = 0;
00382 
00383     rc = MQTTSNSerialize_connect(buf, buflen, &data);
00384     assert("good rc from serialize connect", rc > 0, "rc was %d\n", rc);
00385 
00386     rc = sendPacketBuffer(mysock, options.host, options.port, buf, rc);
00387     assert("good rc from sendPacketBuffer", rc == 0, "rc was %d\n", rc);
00388 
00389     /* wait for connack */
00390     if (MQTTSNPacket_read(buf, buflen, getdata) == MQTTSN_CONNACK)
00391     {
00392         int connack_rc = -1;
00393 
00394         rc = MQTTSNDeserialize_connack(&connack_rc, buf, buflen);
00395         assert("Good rc from deserialize connack", rc == 1, "rc was %d\n", rc);
00396         assert("Good rc from connect", connack_rc == 0, "connack_rc was %d\n", rc);
00397         if (connack_rc != 0)
00398             goto exit;
00399     }
00400     else
00401         goto exit;
00402 
00403     /* subscribe */
00404     printf("Subscribing\n");
00405     topic.type = MQTTSN_TOPIC_TYPE_NORMAL;
00406     topic.data.long_.name = "substopic";
00407     topic.data.long_.len = strlen(topic.data.long_.name);
00408     len = MQTTSNSerialize_subscribe(buf, buflen, 0, 2, /*msgid*/ 1, &topic);
00409     rc = sendPacketBuffer(mysock, options.host, options.port, buf, len);
00410 
00411     if (MQTTSNPacket_read(buf, buflen, getdata) == MQTTSN_SUBACK)   /* wait for suback */
00412     {
00413         unsigned short submsgid;
00414         int granted_qos;
00415         unsigned char returncode;
00416         unsigned short topicid;
00417 
00418         rc = MQTTSNDeserialize_suback(&granted_qos, &topicid, &submsgid, &returncode, buf, buflen);
00419         if (granted_qos != 2 || returncode != 0)
00420         {
00421             printf("granted qos != 2, %d return code %d\n", granted_qos, returncode);
00422             goto exit;
00423         }
00424         else
00425             printf("suback topic id %d\n", topicid);
00426     }
00427     else
00428         goto exit;
00429 
00430     len = MQTTSNSerialize_disconnect(buf, buflen, 0);
00431     rc = sendPacketBuffer(mysock, options.host, options.port, buf, len);
00432 
00433     rc = shutdown(mysock, SHUT_WR);
00434     rc = close(mysock);
00435 
00436 exit:
00437     return rc;
00438 }
00439 
00440 
00441 /*
00442  * Connect non-cleansession, subscribe, disconnect.
00443  * Then reconnect non-cleansession.
00444  */
00445 int test2(struct Options options)
00446 {
00447 
00448     fprintf(xml, "<testcase classname=\"test2\" name=\"clientid free\"");
00449     global_start_time = start_clock();
00450     failures = 0;
00451     MyLog(LOGA_INFO, "Starting test 2 - clientid free");
00452 
00453     connectSubscribeDisconnect(options);
00454     connectDisconnect(options);
00455 
00456     MyLog(LOGA_INFO, "TEST2: test %s. %d tests run, %d failures.",
00457             (failures == 0) ? "passed" : "failed", tests, failures);
00458     write_test_result();
00459     return failures;
00460 }
00461 
00462 
00463 #if 0
00464 int test3(struct Options options)
00465 {
00466     int rc = 0;
00467     unsigned char buf[100];
00468     int buflen = sizeof(buf);
00469 
00470     fprintf(xml, "<testcase classname=\"test3\" name=\"de/serialization\"");
00471     global_start_time = start_clock();
00472     failures = 0;
00473     MyLog(LOGA_INFO, "Starting test 3 - will messages");
00474 
00475     rc = MQTTSNSerialize_willtopicreq(buf, buflen);
00476     assert("good rc from serialize willtopicreq", rc > 0, "rc was %d\n", rc);
00477 
00478     rc = MQTTSNDeserialize_willtopicreq(buf, buflen);
00479     assert("good rc from deserialize willtopicreq", rc == 1, "rc was %d\n", rc);
00480 
00481     rc = MQTTSNSerialize_willmsgreq(buf, buflen);
00482     assert("good rc from serialize willmsgreq", rc > 0, "rc was %d\n", rc);
00483 
00484     rc = MQTTSNDeserialize_willmsgreq(buf, rc);
00485     assert("good rc from deserialize willmsgreq", rc == 1, "rc was %d\n", rc);
00486 
00487 /* exit: */
00488     MyLog(LOGA_INFO, "TEST1: test %s. %d tests run, %d failures.",
00489             (failures == 0) ? "passed" : "failed", tests, failures);
00490     write_test_result();
00491     return failures;
00492 }
00493 
00494 
00495 #if 0
00496 int test3(struct Options options)
00497 {
00498     int i = 0;
00499     int rc = 0;
00500     char buf[100];
00501     int buflen = sizeof(buf);
00502 #define TOPIC_COUNT 2
00503 
00504     int dup = 0;
00505     int msgid = 23;
00506     int count = TOPIC_COUNT;
00507     MQTTString topicStrings[TOPIC_COUNT] = { MQTTString_initializer, MQTTString_initializer };
00508     int req_qoss[TOPIC_COUNT] = {2, 1};
00509 
00510     int dup2 = 1;
00511     int msgid2 = 2223;
00512     int count2 = 0;
00513     MQTTString topicStrings2[TOPIC_COUNT] = { MQTTString_initializer, MQTTString_initializer };
00514     int req_qoss2[TOPIC_COUNT] = {0, 0};
00515 
00516     fprintf(xml, "<testcase classname=\"test1\" name=\"de/serialization\"");
00517     global_start_time = start_clock();
00518     failures = 0;
00519     MyLog(LOGA_INFO, "Starting test 2 - serialization of subscribe and back");
00520 
00521     topicStrings[0].cstring = "mytopic";
00522     topicStrings[1].cstring = "mytopic2";
00523     rc = MQTTSerialize_subscribe(buf, buflen, dup, msgid, count, topicStrings, req_qoss);
00524     assert("good rc from serialize subscribe", rc > 0, "rc was %d\n", rc);
00525 
00526     rc = MQTTDeserialize_subscribe(&dup2, &msgid2, 2, &count2, topicStrings2, req_qoss2, buf, buflen);
00527     assert("good rc from deserialize subscribe", rc == 1, "rc was %d\n", rc);
00528 
00529     /* data after should be the same as data before */
00530     assert("dups should be the same", dup == dup2, "dups were different %d\n", dup2);
00531     assert("msgids should be the same", msgid == msgid2, "msgids were different %d\n", msgid2);
00532 
00533     assert("count should be the same", count == count2, "counts were different %d\n", count2);
00534 
00535     for (i = 0; i < count2; ++i)
00536     {
00537         assert("topics should be the same",
00538                     checkMQTTStrings(topicStrings[i], topicStrings2[i]), "topics were different %s\n", "");
00539 
00540         assert("qoss should be the same", req_qoss[i] == req_qoss2[i], "qoss were different %d\n", req_qoss2[i]);
00541     }
00542 
00543 /*exit:*/
00544     MyLog(LOGA_INFO, "TEST3: test %s. %d tests run, %d failures.",
00545             (failures == 0) ? "passed" : "failed", tests, failures);
00546     write_test_result();
00547     return failures;
00548 }
00549 
00550 
00551 int test4(struct Options options)
00552 {
00553     int i = 0;
00554     int rc = 0;
00555     char buf[100];
00556     int buflen = sizeof(buf);
00557 #define TOPIC_COUNT 2
00558 
00559     int msgid = 23;
00560     int count = TOPIC_COUNT;
00561     int granted_qoss[TOPIC_COUNT] = {2, 1};
00562 ;
00563     int msgid2 = 2223;
00564     int count2 = 0;
00565     int granted_qoss2[TOPIC_COUNT] = {0, 0};
00566 
00567     fprintf(xml, "<testcase classname=\"test1\" name=\"de/serialization\"");
00568     global_start_time = start_clock();
00569     failures = 0;
00570     MyLog(LOGA_INFO, "Starting test 4 - serialization of suback and back");
00571 
00572     rc = MQTTSerialize_suback(buf, buflen, msgid, count, granted_qoss);
00573     assert("good rc from serialize suback", rc > 0, "rc was %d\n", rc);
00574 
00575     rc = MQTTDeserialize_suback(&msgid2, 2, &count2, granted_qoss2, buf, buflen);
00576     assert("good rc from deserialize suback", rc == 1, "rc was %d\n", rc);
00577 
00578     /* data after should be the same as data before */
00579     assert("msgids should be the same", msgid == msgid2, "msgids were different %d\n", msgid2);
00580 
00581     assert("count should be the same", count == count2, "counts were different %d\n", count2);
00582 
00583     for (i = 0; i < count2; ++i)
00584         assert("qoss should be the same", granted_qoss[i] == granted_qoss2[i], "qoss were different %d\n", granted_qoss2[i]);
00585 
00586 /* exit: */
00587     MyLog(LOGA_INFO, "TEST4: test %s. %d tests run, %d failures.",
00588             (failures == 0) ? "passed" : "failed", tests, failures);
00589     write_test_result();
00590     return failures;
00591 }
00592 
00593 
00594 int test5(struct Options options)
00595 {
00596     int i = 0;
00597     int rc = 0;
00598     char buf[100];
00599     int buflen = sizeof(buf);
00600 #define TOPIC_COUNT 2
00601 
00602     int dup = 0;
00603     int msgid = 23;
00604     int count = TOPIC_COUNT;
00605     MQTTString topicStrings[TOPIC_COUNT] = { MQTTString_initializer, MQTTString_initializer };
00606 
00607     int dup2 = 1;
00608     int msgid2 = 2223;
00609     int count2 = 0;
00610     MQTTString topicStrings2[TOPIC_COUNT] = { MQTTString_initializer, MQTTString_initializer };
00611 
00612     fprintf(xml, "<testcase classname=\"test1\" name=\"de/serialization\"");
00613     global_start_time = start_clock();
00614     failures = 0;
00615     MyLog(LOGA_INFO, "Starting test 2 - serialization of unsubscribe and back");
00616 
00617     topicStrings[0].cstring = "mytopic";
00618     topicStrings[1].cstring = "mytopic2";
00619     rc = MQTTSerialize_unsubscribe(buf, buflen, dup, msgid, count, topicStrings);
00620     assert("good rc from serialize unsubscribe", rc > 0, "rc was %d\n", rc);
00621 
00622     rc = MQTTDeserialize_unsubscribe(&dup2, &msgid2, 2, &count2, topicStrings2, buf, buflen);
00623     assert("good rc from deserialize unsubscribe", rc == 1, "rc was %d\n", rc);
00624 
00625     /* data after should be the same as data before */
00626     assert("dups should be the same", dup == dup2, "dups were different %d\n", dup2);
00627     assert("msgids should be the same", msgid == msgid2, "msgids were different %d\n", msgid2);
00628 
00629     assert("count should be the same", count == count2, "counts were different %d\n", count2);
00630 
00631     for (i = 0; i < count2; ++i)
00632         assert("topics should be the same",
00633                     checkMQTTStrings(topicStrings[i], topicStrings2[i]), "topics were different %s\n", "");
00634 
00635 /* exit: */
00636     MyLog(LOGA_INFO, "TEST5: test %s. %d tests run, %d failures.",
00637             (failures == 0) ? "passed" : "failed", tests, failures);
00638     write_test_result();
00639     return failures;
00640 }
00641 #endif
00642 
00643 
00644 int test6(struct Options options)
00645 {
00646     int rc = 0;
00647     unsigned char buf[100];
00648     int buflen = sizeof(buf);
00649 
00650     int connack_rc = 77;
00651 
00652     int connack_rc2 = 0;
00653 
00654     fprintf(xml, "<testcase classname=\"test1\" name=\"de/serialization\"");
00655     global_start_time = start_clock();
00656     failures = 0;
00657     MyLog(LOGA_INFO, "Starting test 2 - serialization of connack and back");
00658 
00659     rc = MQTTSNSerialize_connack(buf, buflen, connack_rc);
00660     assert("good rc from serialize connack", rc > 0, "rc was %d\n", rc);
00661 
00662     rc = MQTTSNDeserialize_connack(&connack_rc2, buf, buflen);
00663     assert("good rc from deserialize connack", rc == 1, "rc was %d\n", rc);
00664 
00665     /* data after should be the same as data before */
00666     assert("dups should be the same", connack_rc == connack_rc2, "dups were different %d\n", connack_rc2);
00667 
00668 /* exit: */
00669     MyLog(LOGA_INFO, "TEST6: test %s. %d tests run, %d failures.",
00670             (failures == 0) ? "passed" : "failed", tests, failures);
00671     write_test_result();
00672     return failures;
00673 }
00674 
00675 #endif
00676 
00677 int main(int argc, char** argv)
00678 {
00679     int rc = 0;
00680     int (*tests[])() = {NULL, test1, test2};
00681 
00682     xml = fopen("TEST-test1.xml", "w");
00683     fprintf(xml, "<testsuite name=\"test2\" tests=\"%d\">\n", (int)(ARRAY_SIZE(tests) - 1));
00684 
00685     getopts(argc, argv);
00686 
00687     if (options.test_no == 0)
00688     { /* run all the tests */
00689         for (options.test_no = 1; options.test_no < ARRAY_SIZE(tests); ++options.test_no)
00690             rc += tests[options.test_no](options); /* return number of failures.  0 = test succeeded */
00691     }
00692     else
00693         rc = tests[options.test_no](options); /* run just the selected test */
00694 
00695     if (rc == 0)
00696         MyLog(LOGA_INFO, "verdict pass");
00697     else
00698         MyLog(LOGA_INFO, "verdict fail");
00699 
00700     fprintf(xml, "</testsuite>\n");
00701     fclose(xml);
00702     return rc;
00703 }