test15.c
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright (c) 2009, 2020 IBM Corp.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v2.0
6  * and Eclipse Distribution License v1.0 which accompany this distribution.
7  *
8  * The Eclipse Public License is available at
9  * https://www.eclipse.org/legal/epl-2.0/
10  * and the Eclipse Distribution License is available at
11  * http://www.eclipse.org/org/documents/edl-v10.php.
12  *
13  * Contributors:
14  * Ian Craggs - initial API and implementation and/or initial documentation
15  * Ian Craggs - MQTT 3.1.1 support
16  * Ian Craggs - change will message test back to using proxy
17  * Ian Craggs - MQTT 5.0 support
18  *******************************************************************************/
19 
20 
27 #include "MQTTClient.h"
28 #include <string.h>
29 #include <stdlib.h>
30 
31 #if !defined(_WINDOWS)
32  #include <sys/time.h>
33  #include <sys/socket.h>
34  #include <unistd.h>
35  #include <errno.h>
36 #else
37  #include <windows.h>
38  #define setenv(a, b, c) _putenv_s(a, b)
39 #endif
40 
41 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
42 
43 void usage(void)
44 {
45  printf("help!!\n");
46  exit(EXIT_FAILURE);
47 }
48 
49 struct Options
50 {
51  char* connection;
52  char** haconnections;
53  char* proxy_connection;
54  int hacount;
55  int verbose;
56  int test_no;
57  int MQTTVersion;
58  int iterations;
59 } options =
60 {
61  "tcp://localhost:1883",
62  NULL,
63  "tcp://localhost:1884",
64  0,
65  0,
66  0,
68  1,
69 };
70 
71 void getopts(int argc, char** argv)
72 {
73  int count = 1;
74 
75  while (count < argc)
76  {
77  if (strcmp(argv[count], "--test_no") == 0)
78  {
79  if (++count < argc)
80  options.test_no = atoi(argv[count]);
81  else
82  usage();
83  }
84  else if (strcmp(argv[count], "--connection") == 0)
85  {
86  if (++count < argc)
87  {
88  options.connection = argv[count];
89  printf("\nSetting connection to %s\n", options.connection);
90  }
91  else
92  usage();
93  }
94  else if (strcmp(argv[count], "--haconnections") == 0)
95  {
96  if (++count < argc)
97  {
98  char* tok = strtok(argv[count], " ");
99  options.hacount = 0;
100  options.haconnections = malloc(sizeof(char*) * 5);
101  while (tok)
102  {
103  options.haconnections[options.hacount] = malloc(strlen(tok) + 1);
104  strcpy(options.haconnections[options.hacount], tok);
105  options.hacount++;
106  tok = strtok(NULL, " ");
107  }
108  }
109  else
110  usage();
111  }
112  else if (strcmp(argv[count], "--proxy_connection") == 0)
113  {
114  if (++count < argc)
116  else
117  usage();
118  }
119  else if (strcmp(argv[count], "--MQTTversion") == 0)
120  {
121  if (++count < argc)
122  {
123  options.MQTTVersion = atoi(argv[count]);
124  printf("setting MQTT version to %d\n", options.MQTTVersion);
125  }
126  else
127  usage();
128  }
129  else if (strcmp(argv[count], "--iterations") == 0)
130  {
131  if (++count < argc)
132  options.iterations = atoi(argv[count]);
133  else
134  usage();
135  }
136  else if (strcmp(argv[count], "--verbose") == 0)
137  {
138  options.verbose = 1;
139  printf("\nSetting verbose on\n");
140  }
141  count++;
142  }
143 }
144 
145 
146 #define LOGA_DEBUG 0
147 #define LOGA_INFO 1
148 #include <stdarg.h>
149 #include <time.h>
150 #include <sys/timeb.h>
151 void MyLog(int LOGA_level, char* format, ...)
152 {
153  static char msg_buf[256];
154  va_list args;
155 #if defined(_WIN32) || defined(_WINDOWS)
156  struct timeb ts;
157 #else
158  struct timeval ts;
159 #endif
160  struct tm timeinfo;
161 
162  if (LOGA_level == LOGA_DEBUG && options.verbose == 0)
163  return;
164 
165 #if defined(_WIN32) || defined(_WINDOWS)
166  ftime(&ts);
167  localtime_s(&timeinfo, &ts.time);
168 #else
169  gettimeofday(&ts, NULL);
170  localtime_r(&ts.tv_sec, &timeinfo);
171 #endif
172  strftime(msg_buf, 80, "%Y%m%d %H%M%S", &timeinfo);
173 
174 #if defined(_WIN32) || defined(_WINDOWS)
175  sprintf(&msg_buf[strlen(msg_buf)], ".%.3hu ", ts.millitm);
176 #else
177  sprintf(&msg_buf[strlen(msg_buf)], ".%.3lu ", ts.tv_usec / 1000);
178 #endif
179 
180  va_start(args, format);
181  vsnprintf(&msg_buf[strlen(msg_buf)], sizeof(msg_buf) - strlen(msg_buf), format, args);
182  va_end(args);
183 
184  printf("%s\n", msg_buf);
185  fflush(stdout);
186 }
187 
188 
189 #if defined(_WIN32) || defined(_WINDOWS)
190 #define mqsleep(A) Sleep(1000*A)
191 #define START_TIME_TYPE DWORD
192 static DWORD start_time = 0;
194 {
195  return GetTickCount();
196 }
197 #elif defined(AIX)
198 #define mqsleep sleep
199 #define START_TIME_TYPE struct timespec
201 {
202  static struct timespec start;
203  clock_gettime(CLOCK_REALTIME, &start);
204  return start;
205 }
206 #else
207 #define mqsleep sleep
208 #define START_TIME_TYPE struct timeval
209 /* TODO - unused - remove? static struct timeval start_time; */
211 {
212  struct timeval start_time;
213  gettimeofday(&start_time, NULL);
214  return start_time;
215 }
216 #endif
217 
218 
219 #if defined(_WIN32)
220 long elapsed(START_TIME_TYPE start_time)
221 {
222  return GetTickCount() - start_time;
223 }
224 #elif defined(AIX)
225 #define assert(a)
226 long elapsed(struct timespec start)
227 {
228  struct timespec now, res;
229 
230  clock_gettime(CLOCK_REALTIME, &now);
231  ntimersub(now, start, res);
232  return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L;
233 }
234 #else
235 long elapsed(START_TIME_TYPE start_time)
236 {
237  struct timeval now, res;
238 
239  gettimeofday(&now, NULL);
240  timersub(&now, &start_time, &res);
241  return (res.tv_sec)*1000 + (res.tv_usec)/1000;
242 }
243 #endif
244 
245 
246 #define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d)
247 #define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e)
248 
249 int tests = 0;
250 int failures = 0;
251 FILE* xml;
253 char output[3000];
255 
256 
258 {
259  long duration = elapsed(global_start_time);
260 
261  fprintf(xml, " time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000);
262  if (cur_output != output)
263  {
264  fprintf(xml, "%s", output);
265  cur_output = output;
266  }
267  fprintf(xml, "</testcase>\n");
268 }
269 
270 
271 void myassert(char* filename, int lineno, char* description, int value, char* format, ...)
272 {
273  ++tests;
274  if (!value)
275  {
276  va_list args;
277 
278  ++failures;
279  MyLog(LOGA_INFO, "Assertion failed, file %s, line %d, description: %s\n", filename, lineno, description);
280 
281  va_start(args, format);
282  vprintf(format, args);
283  va_end(args);
284 
285  cur_output += sprintf(cur_output, "<failure type=\"%s\">file %s, line %d </failure>\n",
286  description, filename, lineno);
287  }
288  else
289  MyLog(LOGA_DEBUG, "Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description);
290 }
291 
292 
293 /*********************************************************************
294 
295 Test1: single-threaded client
296 
297 *********************************************************************/
299 {
302  MQTTClient_message* m = NULL;
303  char* topicName = NULL;
304  int topicLen;
305  int i = 0;
306  int iterations = 50;
307  int rc;
308  MQTTResponse resp;
310 
311  MyLog(LOGA_DEBUG, "%d messages at QoS %d", iterations, qos);
312  pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
313  pubmsg.payloadlen = 11;
314  pubmsg.qos = qos;
315  pubmsg.retained = 0;
316 
317  property.identifier = MQTTPROPERTY_CODE_USER_PROPERTY;
318  property.value.data.data = "test user property";
319  property.value.data.len = (int)strlen(property.value.data.data);
320  property.value.value.data = "test user property value";
321  property.value.value.len = (int)strlen(property.value.value.data);
322  MQTTProperties_add(&pubmsg.properties, &property);
323 
324  for (i = 0; i < iterations; ++i)
325  {
326  if (i % 10 == 0)
327  resp = MQTTClient_publish5(c, test_topic, pubmsg.payloadlen, pubmsg.payload, pubmsg.qos, pubmsg.retained,
328  &pubmsg.properties, &dt);
329  else
330  resp = MQTTClient_publishMessage5(c, test_topic, &pubmsg, &dt);
331  assert("Good rc from publish", resp.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", resp.reasonCode);
332 
333  if (qos > 0)
334  {
335  rc = MQTTClient_waitForCompletion(c, dt, 5000L);
336  assert("Good rc from waitforCompletion", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
337  }
338  rc = MQTTClient_receive(c, &topicName, &topicLen, &m, 5000);
339  assert("Good rc from receive", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
340  if (topicName)
341  {
342  MyLog(LOGA_DEBUG, "Message received on topic %s is %.*s", topicName, m->payloadlen, (char*)(m->payload));
343  if (pubmsg.payloadlen != m->payloadlen ||
344  memcmp(m->payload, pubmsg.payload, m->payloadlen) != 0)
345  {
346  failures++;
347  MyLog(LOGA_INFO, "Error: wrong data - received lengths %d %d", pubmsg.payloadlen, m->payloadlen);
348  break;
349  }
350  assert("Property count should be > 0", m->properties.count > 0, "property count was %d", m->properties.count);
351  MQTTClient_free(topicName);
353  }
354  else
355  MyLog(LOGA_INFO, "No message received within timeout period\n");
356  }
357 
358  /* receive any outstanding messages */
359  MQTTClient_receive(c, &topicName, &topicLen, &m, 2000);
360  while (topicName)
361  {
362  MyLog(LOGA_INFO, "Message received on topic %s is %.*s.\n", topicName, m->payloadlen, (char*)(m->payload));
363  MQTTClient_free(topicName);
365  MQTTClient_receive(c, &topicName, &topicLen, &m, 2000);
366  }
367 
369 }
370 
372 {
373  int i = 0;
374 
375  for (i = 0; i < props->count; ++i)
376  {
377  int id = props->array[i].identifier;
378  const char* name = MQTTPropertyName(id);
379  char* intformat = "Property name %s value %d";
380 
381  switch (MQTTProperty_getType(id))
382  {
384  MyLog(LOGA_INFO, intformat, name, props->array[i].value.byte);
385  break;
387  MyLog(LOGA_INFO, intformat, name, props->array[i].value.integer2);
388  break;
390  MyLog(LOGA_INFO, intformat, name, props->array[i].value.integer4);
391  break;
393  MyLog(LOGA_INFO, intformat, name, props->array[i].value.integer4);
394  break;
397  MyLog(LOGA_INFO, "Property name %s value %.*s", name,
398  props->array[i].value.data.len, props->array[i].value.data.data);
399  break;
401  MyLog(LOGA_INFO, "Property name %s key %.*s value %.*s", name,
402  props->array[i].value.data.len, props->array[i].value.data.data,
403  props->array[i].value.value.len, props->array[i].value.value.data);
404  break;
405  }
406  }
407 }
408 
409 int test1(struct Options options)
410 {
411  int subsqos = 2;
412  MQTTClient c;
420  int rc = 0;
421  char* test_topic = "C client test1";
423 
424  fprintf(xml, "<testcase classname=\"test1\" name=\"single threaded client using receive\"");
426  failures = 0;
427  MyLog(LOGA_INFO, "Starting test 1 - single threaded client using receive");
428 
429  createOpts.MQTTVersion = MQTTVERSION_5;
430  rc = MQTTClient_createWithOptions(&c, options.connection, "single_threaded_test",
431  MQTTCLIENT_PERSISTENCE_DEFAULT, NULL, &createOpts);
432  assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
433  if (rc != MQTTCLIENT_SUCCESS)
434  {
435  MQTTClient_destroy(&c);
436  goto exit;
437  }
438 
439  opts.keepAliveInterval = 20;
440  opts.cleanstart = 1;
441  opts.username = "testuser";
442  opts.password = "testpassword";
443  opts.MQTTVersion = options.MQTTVersion;
444  printf("test MQTT version %d\n", options.MQTTVersion);
445  if (options.haconnections != NULL)
446  {
447  opts.serverURIs = options.haconnections;
448  opts.serverURIcount = options.hacount;
449  }
450 
451  opts.will = &wopts;
452  opts.will->message = "will message";
453  opts.will->qos = 1;
454  opts.will->retained = 0;
455  opts.will->topicName = "will topic";
456  opts.will = NULL;
457 
458  property.identifier = MQTTPROPERTY_CODE_SESSION_EXPIRY_INTERVAL;
459  property.value.integer4 = 30;
460  MQTTProperties_add(&props, &property);
461 
462  property.identifier = MQTTPROPERTY_CODE_USER_PROPERTY;
463  property.value.data.data = "test user property";
464  property.value.data.len = (int)strlen(property.value.data.data);
465  property.value.value.data = "test user property value";
466  property.value.value.len = (int)strlen(property.value.value.data);
467  MQTTProperties_add(&props, &property);
468 
469  MyLog(LOGA_DEBUG, "Connecting");
470  response = MQTTClient_connect5(c, &opts, &props, &willProps);
471  assert("Good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
472  MQTTProperties_free(&props);
473  MQTTProperties_free(&willProps);
474  if (response.reasonCode != MQTTCLIENT_SUCCESS)
475  goto exit;
476 
477  if (response.properties)
478  {
479  logProperties(response.properties);
480  MQTTResponse_free(response);
481  }
482 
483  subopts.retainAsPublished = 1;
484  property.identifier = MQTTPROPERTY_CODE_SUBSCRIPTION_IDENTIFIER;
485  property.value.integer4 = 33;
486  MQTTProperties_add(&props, &property);
487  response = MQTTClient_subscribe5(c, test_topic, subsqos, &subopts, &props);
488  assert("Good rc from subscribe", response.reasonCode == subsqos, "rc was %d", response.reasonCode);
489  MQTTProperties_free(&props);
490 
491  if (response.properties)
492  {
493  logProperties(response.properties);
495  }
496 
497  test1_sendAndReceive(c, 0, test_topic);
498  test1_sendAndReceive(c, 1, test_topic);
499  test1_sendAndReceive(c, 2, test_topic);
500 
501  MyLog(LOGA_DEBUG, "Stopping\n");
502 
503  MQTTProperties_free(&props);
504  property.identifier = MQTTPROPERTY_CODE_USER_PROPERTY;
505  property.value.data.data = "User property name";
506  property.value.data.len = (int)strlen(property.value.data.data);
507  property.value.value.data = "User property value";
508  property.value.value.len = (int)strlen(property.value.value.data);
509  MQTTProperties_add(&props, &property);
510 
511  response = MQTTClient_unsubscribe5(c, test_topic, &props);
512  assert("Unsubscribe successful", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
513  MQTTResponse_free(response);
514 
515  MQTTProperties_free(&props);
516  property.identifier = MQTTPROPERTY_CODE_SESSION_EXPIRY_INTERVAL;
517  property.value.integer4 = 0;
518  MQTTProperties_add(&props, &property);
519 
521  assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
522 
523  /* Just to make sure we can connect again */
524  response = MQTTClient_connect5(c, &opts, NULL, NULL);
525  assert("Connect successful", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
526  MQTTResponse_free(response);
528  assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
529 
530  MQTTProperties_free(&props);
531  MQTTClient_destroy(&c);
532 
533 exit:
534  MyLog(LOGA_INFO, "TEST1: test %s. %d tests run, %d failures.",
535  (failures == 0) ? "passed" : "failed", tests, failures);
537  return failures;
538 }
539 
540 
541 /*********************************************************************
542 
543 Test2: multi-threaded client using callbacks
544 
545 *********************************************************************/
546 volatile int test2_arrivedcount = 0;
549 
551 {
553 }
554 
555 int test2_messageArrived(void* context, char* topicName, int topicLen, MQTTClient_message* message)
556 {
558  MyLog(LOGA_DEBUG, "Callback: %d message received on topic %s is %.*s.",
559  test2_arrivedcount, topicName, message->payloadlen, (char*)(message->payload));
560 
561  assert("Message structure version should be 1", message->struct_version == 1,
562  "message->struct_version was %d", message->struct_version);
563  if (message->struct_version == 1)
564  {
565  const int props_count = 0;
566 
567  assert("Properties count should be 0", message->properties.count == props_count,
568  "Properties count was %d\n", message->properties.count);
569  logProperties(&message->properties);
570  }
571 
572  if (test2_pubmsg.payloadlen != message->payloadlen ||
573  memcmp(message->payload, test2_pubmsg.payload, message->payloadlen) != 0)
574  {
575  failures++;
576  MyLog(LOGA_INFO, "Error: wrong data received lengths %d %d\n", test2_pubmsg.payloadlen, message->payloadlen);
577  }
578  MQTTClient_free(topicName);
579  MQTTClient_freeMessage(&message);
580  return 1;
581 }
582 
583 
585 {
587  int i = 0;
588  int iterations = 50;
590  int wait_seconds = 0;
591 
593 
594  MyLog(LOGA_INFO, "%d messages at QoS %d", iterations, qos);
595  test2_pubmsg.payload = "a much longer message that we can shorten to the extent that we need to";
596  test2_pubmsg.payloadlen = 27;
597  test2_pubmsg.qos = qos;
598  test2_pubmsg.retained = 0;
599 
600  for (i = 1; i <= iterations; ++i)
601  {
602  if (i % 10 == 0)
603  response = MQTTClient_publish5(c, test_topic, test2_pubmsg.payloadlen, test2_pubmsg.payload,
604  test2_pubmsg.qos, test2_pubmsg.retained, NULL, NULL);
605  else
606  response = MQTTClient_publishMessage5(c, test_topic, &test2_pubmsg, &dt);
607  assert("Good rc from publish", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
608 
609  #if defined(_WIN32)
610  Sleep(100);
611  #else
612  usleep(100000L);
613  #endif
614 
615  wait_seconds = 10;
616  while ((test2_arrivedcount < i) && (wait_seconds-- > 0))
617  {
618  MyLog(LOGA_DEBUG, "Arrived %d count %d", test2_arrivedcount, i);
619  #if defined(_WIN32)
620  Sleep(1000);
621  #else
622  usleep(1000000L);
623  #endif
624  }
625  assert("Message Arrived", wait_seconds > 0,
626  "Time out waiting for message %d\n", i );
627  }
628  if (qos > 0)
629  {
630  /* MQ Telemetry can send a message to a subscriber before the server has
631  completed the QoS 2 handshake with the publisher. For QoS 1 and 2,
632  allow time for the final delivery complete callback before checking
633  that all expected callbacks have been made */
634  wait_seconds = 10;
635  while ((test2_deliveryCompleted < iterations) && (wait_seconds-- > 0))
636  {
637  MyLog(LOGA_DEBUG, "Delivery Completed %d count %d", test2_deliveryCompleted, i);
638  #if defined(_WIN32)
639  Sleep(1000);
640  #else
641  usleep(1000000L);
642  #endif
643  }
644  assert("All Deliveries Complete", wait_seconds > 0,
645  "Number of deliveryCompleted callbacks was %d\n",
647  }
648 }
649 
650 
651 int test2(struct Options options)
652 {
653  char* testname = "test2";
654  int subsqos = 2;
655  /* TODO - usused - remove ? MQTTClient_deliveryToken* dt = NULL; */
656  MQTTClient c;
662  int rc = 0;
663  char* test_topic = "C client test2";
665 
666  fprintf(xml, "<testcase classname=\"test1\" name=\"multi-threaded client using callbacks\"");
667  MyLog(LOGA_INFO, "Starting test 2 - multi-threaded client using callbacks");
669  failures = 0;
670 
671  createOpts.MQTTVersion = MQTTVERSION_5;
672  MQTTClient_createWithOptions(&c, options.connection, "multi_threaded_sample",
673  MQTTCLIENT_PERSISTENCE_DEFAULT, NULL, &createOpts);
674 
675  opts.keepAliveInterval = 20;
676  opts.cleanstart = 1;
677  opts.MQTTVersion = options.MQTTVersion;
678  opts.username = "testuser";
679  opts.binarypwd.data = "testpassword";
680  opts.binarypwd.len = (int)strlen(opts.binarypwd.data);
681  if (options.haconnections != NULL)
682  {
683  opts.serverURIs = options.haconnections;
684  opts.serverURIcount = options.hacount;
685  }
686 
688  assert("Good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
689 
690  MyLog(LOGA_DEBUG, "Connecting");
691  response = MQTTClient_connect5(c, &opts, &props, &willProps);
692  assert("Good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS,
693  "rc was %d", response.reasonCode);
694  MQTTResponse_free(response);
695  if (rc != MQTTCLIENT_SUCCESS)
696  goto exit;
697 
698  response = MQTTClient_subscribe5(c, test_topic, subsqos, &subopts, &props);
699  assert("Good rc from subscribe", response.reasonCode == subsqos, "rc was %d", rc);
700 
701  test2_sendAndReceive(c, 0, test_topic);
702  test2_sendAndReceive(c, 1, test_topic);
703  test2_sendAndReceive(c, 2, test_topic);
704 
705  MyLog(LOGA_DEBUG, "Stopping");
706 
707  response = MQTTClient_unsubscribe5(c, test_topic, &props);
708  assert("Unsubscribe successful", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", rc);
710  assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
711 
712  MQTTClient_destroy(&c);
713 
714 exit:
715  MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
716  (failures == 0) ? "passed" : "failed", testname, tests, failures);
718  return failures;
719 }
720 
721 
722 /*********************************************************************
723 
724 Test 3: connack return codes
725 
726 for AMQTDD, needs an amqtdd.cfg of:
727 
728  allow_anonymous false
729  password_file passwords
730 
731 and a passwords file of:
732 
733  Admin:Admin
734 
735 *********************************************************************/
736 int test3(struct Options options)
737 {
738  char* testname = "test3";
739  int rc;
740  MQTTClient c;
743  MQTTResponse response;
745 
746  fprintf(xml, "<testcase classname=\"test1\" name=\"connack return codes\"");
748  failures = 0;
749  MyLog(LOGA_INFO, "Starting test 3 - connack return codes");
750 
751  createOpts.MQTTVersion = MQTTVERSION_5;
752 
753 #if 0
754  /* clientid too long (RC = 2) */
755  rc = MQTTClient_create(&c, options.connection, "client_ID_too_long_for_MQTT_protocol_version_3",
757  assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
758  rc = MQTTClient_connect(c, &opts);
759  assert("identifier rejected", rc == 2, "rc was %d\n", rc);
760  MQTTClient_destroy(&c);
761 #endif
762  /* broker unavailable (RC = 3) - TDD when allow_anonymous not set*/
763  rc = MQTTClient_createWithOptions(&c, options.connection, "The C Client", MQTTCLIENT_PERSISTENCE_NONE,
764  NULL, &createOpts);
765  assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
766 #if 0
767  rc = MQTTClient_connect(c, &opts);
768  assert("broker unavailable", rc == 3, "rc was %d\n", rc);
769 
770  /* authentication failure (RC = 4) */
771  opts.username = "Admin";
772  opts.password = "fred";
773  rc = MQTTClient_connect(c, &opts);
774  assert("Bad user name or password", rc == 4, "rc was %d\n", rc);
775 #endif
776 
777  /* authorization failure (RC = 5) */
778  opts.username = "Admin";
779  opts.password = "Admin";
780  /*opts.will = &wopts; "Admin" not authorized to publish to Will topic by default
781  opts.will->message = "will message";
782  opts.will->qos = 1;
783  opts.will->retained = 0;
784  opts.will->topicName = "will topic";*/
785  response = MQTTClient_connect5(c, &opts, NULL, NULL);
786  //assert("Not authorized", rc == 5, "rc was %d\n", rc);
787  MQTTResponse_free(response);
788 
789 #if 0
790  /* successful connection (RC = 0) */
791  opts.username = "Admin";
792  opts.password = "Admin";
793  opts.will = NULL;
794  rc = MQTTClient_connect(c, &opts);
795  assert("successful connection", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
796  MQTTClient_disconnect(c, 0);
797  MQTTClient_destroy(&c);
798 #endif
799 
800 /* TODO - unused - remove ? exit: */
801  MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
802  (failures == 0) ? "passed" : "failed", testname, tests, failures);
804  return failures;
805 }
806 
807 
808 /*********************************************************************
809 
810 Test 4: client persistence 1
811 
812 
813 *********************************************************************/
814 int test4_run(int qos, int start_mqtt_version, int restore_mqtt_version)
815 {
816  char* testname = "test 4";
817  char* topic = "Persistence test 1";
818  int subsqos = 2;
819  MQTTClient c;
821  MQTTClient_message* m = NULL;
822  char* topicName = NULL;
823  int topicLen;
824  MQTTClient_deliveryToken* tokens = NULL;
825  int mytoken = -99;
826  char buffer[100];
827  int count = 3;
833  int i, rc;
834 
835  failures = 0;
836  MyLog(LOGA_INFO, "Starting test 4 - persistence, qos %d, MQTT versions: %s then %s", qos,
837  (start_mqtt_version == MQTTVERSION_5) ? "5" : "3.1.1",
838  (restore_mqtt_version == MQTTVERSION_5) ? "5" : "3.1.1");
839 
840  createOpts.MQTTVersion = start_mqtt_version;
841  rc = MQTTClient_createWithOptions(&c, options.connection, "xrctest15_test_4",
842  MQTTCLIENT_PERSISTENCE_DEFAULT, NULL, &createOpts);
843  assert("Good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
844 
845  /* we might get some tokens back because they may not be cleaned up until
846  * we connect cleanstart
847  */
848  rc = MQTTClient_getPendingDeliveryTokens(c, &tokens);
849  assert("getPendingDeliveryTokens rc == 0", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
850  if (tokens)
851  MQTTClient_free(tokens);
852 
853  opts.keepAliveInterval = 20;
854  opts.reliable = 0;
855  opts.MQTTVersion = start_mqtt_version;
856  if (options.haconnections != NULL)
857  {
860  }
861 
862  if (start_mqtt_version == MQTTVERSION_5)
863  {
864  MyLog(LOGA_DEBUG, "Cleanup by connecting clean start, add session expiry > 0\n");
865  opts.cleanstart = 1;
866  property.identifier = MQTTPROPERTY_CODE_SESSION_EXPIRY_INTERVAL;
867  property.value.integer4 = 30; /* in seconds */
868  MQTTProperties_add(&props, &property);
869  response = MQTTClient_connect5(c, &opts, &props, NULL);
870  assert("Good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS,
871  "rc was %d", response.reasonCode);
872  MQTTResponse_free(response);
873  if (response.reasonCode != MQTTCLIENT_SUCCESS)
874  return -1;
875  }
876  else
877  {
878  MyLog(LOGA_DEBUG, "Cleanup by connecting clean session, then reconnecting non-cleansession\n");
879  opts.cleanstart = 0; /* only applies to MQTT V5 */
880  opts.cleansession = 1;
881  rc = MQTTClient_connect(c, &opts);
882  assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
883  rc = MQTTClient_disconnect(c, 1000);
884  assert("Good rc from disconnect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
885  opts.cleansession = 0;
886  rc = MQTTClient_connect(c, &opts);
887  assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
888  if (rc != MQTTCLIENT_SUCCESS)
889  return -1;
890  }
891 
892  /* subscribe so we can get messages back */
893  if (start_mqtt_version == MQTTVERSION_5)
894  {
895  response = MQTTClient_subscribe5(c, topic, subsqos, NULL, NULL);
896  assert("Good rc from subscribe", response.reasonCode == subsqos, "rc was %d", response.reasonCode);
897  }
898  else
899  {
900  rc = MQTTClient_subscribe(c, topic, subsqos);
901  assert("Good rc from subscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
902  }
903 
904  /* send messages so that we can receive the same ones */
905  if (start_mqtt_version == MQTTVERSION_5)
906  {
907  property.identifier = MQTTPROPERTY_CODE_USER_PROPERTY;
908  property.value.data.data = "test user property";
909  property.value.data.len = (int)strlen(property.value.data.data);
910  property.value.value.data = "test user property value";
911  property.value.value.len = (int)strlen(property.value.value.data);
912  MQTTProperties_add(&pub_props, &property);
913  }
914 
915  for (i = 0; i < count; ++i)
916  {
917  sprintf(buffer, "Message sequence no %d", i);
918  if (start_mqtt_version == MQTTVERSION_5)
919  {
920  response = MQTTClient_publish5(c, topic, 10, buffer, qos, 0, &pub_props, NULL);
921  assert("Good rc from publish", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
922  }
923  else
924  {
925  rc = MQTTClient_publish(c, topic, 10, buffer, qos, 0, NULL);
926  assert("Good rc from publish", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
927  }
928  }
929 
930  /* disconnect immediately without receiving the incoming messages */
931  if (start_mqtt_version == MQTTVERSION_5)
932  rc = MQTTClient_disconnect5(c, 0, MQTTREASONCODE_SUCCESS, NULL); /* now there should be "orphaned" publications */
933  else
934  rc = MQTTClient_disconnect(c, 0); /* now there should be "orphaned" publications */
935  assert("Good rc from disconnect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
936 
937  rc = MQTTClient_getPendingDeliveryTokens(c, &tokens);
938  assert("getPendingDeliveryTokens rc == 0", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
939 
940  assert("should get some tokens back", tokens != NULL, "tokens was %p", tokens);
941  if (tokens)
942  {
943  int i = 0;
944 
945  while (tokens[i] != -1)
946  MyLog(LOGA_DEBUG, "Pending delivery token %d", tokens[i++]);
947  assert1("no of tokens should be count", i == count, "no of tokens %d count %d", i, count);
948  mytoken = tokens[0];
949  MQTTClient_free(tokens);
950  }
951  MQTTProperties_free(&props);
952  MQTTProperties_free(&pub_props);
953  MQTTClient_destroy(&c); /* force re-reading persistence on create */
954 
955  createOpts.MQTTVersion = restore_mqtt_version;
956  rc = MQTTClient_createWithOptions(&c, options.connection, "xrctest15_test_4",
957  MQTTCLIENT_PERSISTENCE_DEFAULT, NULL, &createOpts);
958  if (start_mqtt_version == MQTTVERSION_5 && restore_mqtt_version == MQTTVERSION_3_1_1)
959  {
960  assert("Persistence error from create", rc == MQTTCLIENT_PERSISTENCE_ERROR, "rc was %d", rc);
961  goto exit;
962  }
963  else
964  assert("Good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
965 
966  rc = MQTTClient_getPendingDeliveryTokens(c, &tokens);
967  assert("getPendingDeliveryTokens rc == 0", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
968 
969  assert("should get some tokens back", tokens != NULL, "tokens was %p", tokens);
970  if (tokens)
971  {
972  int i = 0;
973  while (tokens[i] != -1)
974  MyLog(LOGA_DEBUG, "Pending delivery token %d", tokens[i++]);
975  MQTTClient_free(tokens);
976  assert1("no of tokens should be count", i == count, "no of tokens %d count %d", i, count);
977  }
978 
979  MyLog(LOGA_DEBUG, "Reconnecting");
980  opts.cleanstart = 0;
981  response = MQTTClient_connect5(c, &opts, NULL, NULL);
982  assert("Good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
983  MQTTResponse_free(response);
984  if (response.reasonCode != MQTTCLIENT_SUCCESS)
985  return -1;
986 
987  for (i = 0; i < count; ++i)
988  {
989  int dup = 0;
990  do
991  {
992  dup = 0;
993  MQTTClient_receive(c, &topicName, &topicLen, &m, 5000);
994  if (m && m->dup)
995  {
996  assert("No duplicates should be received for qos 2", qos == 1, "qos is %d", qos);
997  MyLog(LOGA_DEBUG, "Duplicate message id %d", m->msgid);
998  assert("properties are received", m->properties.count > 0, "property count is %d",
999  m->properties.count);
1002  MQTTClient_free(topicName);
1003  dup = 1;
1004  }
1005  } while (dup == 1);
1006  assert("should get a message", m != NULL, "m was %p", m);
1007  if (m)
1008  {
1009  MyLog(LOGA_DEBUG, "Received message id %d", m->msgid);
1010  assert("topicName is correct", strcmp(topicName, topic) == 0, "topicName is %s", topicName);
1011  if (start_mqtt_version == MQTTVERSION_5)
1012  assert("properties are received", m->properties.count > 0, "property count is %d",
1013  m->properties.count);
1014  else
1015  assert("properties are not received", m->properties.count == 0, "property count is %d",
1016  m->properties.count);
1019  MQTTClient_free(topicName);
1020  }
1021  }
1022 
1023  MQTTClient_yield(); /* allow any unfinished protocol exchanges to finish */
1024 
1025  rc = MQTTClient_getPendingDeliveryTokens(c, &tokens);
1026  assert("getPendingDeliveryTokens rc == 0", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
1027  assert("should get no tokens back", tokens == NULL, "tokens was %p", tokens);
1028 
1030 
1031  MQTTClient_destroy(&c);
1032 
1033 exit:
1034  MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
1035  (failures == 0) ? "passed" : "failed", testname, tests, failures);
1036 
1037  return failures;
1038 }
1039 
1040 
1041 int test4(struct Options options)
1042 {
1043  int rc = 0;
1044  fprintf(xml, "<testcase classname=\"test4\" name=\"persistence\"");
1050  fprintf(xml, " time=\"%ld\" >\n", elapsed(global_start_time) / 1000);
1051  if (cur_output != output)
1052  {
1053  fprintf(xml, "%s", output);
1054  cur_output = output;
1055  }
1056  fprintf(xml, "</testcase>\n");
1057  return rc;
1058 }
1059 
1060 
1061 /*********************************************************************
1062 
1063 Test 5: disconnect with quiesce timeout should allow exchanges to complete
1064 
1065 *********************************************************************/
1066 int test5(struct Options options)
1067 {
1068  char* testname = "test 5";
1069  char* topic = "Persistence test 2";
1070  int subsqos = 2;
1071  MQTTClient c;
1073  MQTTClient_deliveryToken* tokens = NULL;
1074  char buffer[100];
1075  int count = 5;
1079  int i, rc;
1081 
1082  fprintf(xml, "<testcase classname=\"test1\" name=\"disconnect with quiesce timeout should allow exchanges to complete\"");
1084  failures = 0;
1085  MyLog(LOGA_INFO, "Starting test 5 - disconnect with quiesce timeout should allow exchanges to complete");
1086 
1087  createOpts.MQTTVersion = MQTTVERSION_5;
1089  NULL, &createOpts);
1090 
1091  opts.keepAliveInterval = 20;
1092  opts.cleanstart = 1;
1093  opts.reliable = 0;
1094  opts.MQTTVersion = options.MQTTVersion;
1095  if (options.haconnections != NULL)
1096  {
1097  opts.serverURIs = options.haconnections;
1098  opts.serverURIcount = options.hacount;
1099  }
1100  property.identifier = MQTTPROPERTY_CODE_SESSION_EXPIRY_INTERVAL;
1101  property.value.integer4 = 30;
1102  MQTTProperties_add(&props, &property);
1103 
1104  MyLog(LOGA_DEBUG, "Connecting");
1105  response = MQTTClient_connect5(c, &opts, &props, NULL);
1106  assert("Good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
1107  MQTTResponse_free(response);
1108  if (response.reasonCode != MQTTCLIENT_SUCCESS)
1109  {
1110  MQTTClient_destroy(&c);
1111  goto exit;
1112  }
1113 
1114  response = MQTTClient_subscribe5(c, topic, subsqos, NULL, NULL);
1115  assert("Good rc from subscribe", response.reasonCode == subsqos, "rc was %d", response.reasonCode);
1116 
1117  for (i = 0; i < count; ++i)
1118  {
1119  sprintf(buffer, "Message sequence no %d", i);
1120  response = MQTTClient_publish5(c, topic, 10, buffer, 1, 0, NULL, NULL);
1121  assert("Good rc from publish", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
1122  }
1123 
1124  MQTTClient_disconnect5(c, 1000, MQTTREASONCODE_SUCCESS, NULL); /* now there should be no "orphaned" publications */
1125  MyLog(LOGA_DEBUG, "Disconnected");
1126 
1127  rc = MQTTClient_getPendingDeliveryTokens(c, &tokens);
1128  assert("getPendingDeliveryTokens rc == 0", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
1129 
1130  assert("should get no tokens back", tokens == NULL, "tokens was %p", tokens);
1131 
1132  MQTTProperties_free(&props);
1133  MQTTClient_destroy(&c);
1134 
1135 exit:
1136  MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
1137  (failures == 0) ? "passed" : "failed", testname, tests, failures);
1139  return failures;
1140 }
1141 
1142 
1143 /*********************************************************************
1144 
1145 Test 6: connectionLost and will message
1146 
1147 *********************************************************************/
1151 
1152 void test6_connectionLost(void* context, char* cause)
1153 {
1154  MQTTClient c = (MQTTClient)context;
1155  MyLog(LOGA_INFO, "%s -> Callback: connection lost", (c == test6_c1) ? "Client-1" : "Client-2");
1157 }
1158 
1160 {
1161  MyLog(LOGA_DEBUG, "Client-2 -> Callback: publish complete for token %d", token);
1162 }
1163 
1164 char* test6_will_topic = "C Test 2: will topic";
1165 char* test6_will_message = "will message from Client-1";
1166 
1167 int test6_messageArrived(void* context, char* topicName, int topicLen, MQTTClient_message* m)
1168 {
1169  MQTTClient c = (MQTTClient)context;
1170  MyLog(LOGA_INFO, "%s -> Callback: message received on topic '%s' is '%.*s'",
1171  (c == test6_c1) ? "Client-1" : "Client-2", topicName, m->payloadlen, (char*)(m->payload));
1172  if (c == test6_c2 && strcmp(topicName, test6_will_topic) == 0 && memcmp(m->payload, test6_will_message, m->payloadlen) == 0)
1174  MQTTClient_free(topicName);
1176  return 1;
1177 }
1178 
1179 
1180 int test6(struct Options options)
1181 {
1182  char* testname = "test6";
1187  int rc, count;
1188  char* mqttsas_topic = "MQTTSAS topic";
1190 
1191  failures = 0;
1192  MyLog(LOGA_INFO, "Starting test 6 - connectionLost and will messages");
1193  fprintf(xml, "<testcase classname=\"test1\" name=\"connectionLost and will messages\"");
1195 
1196  opts.keepAliveInterval = 2;
1197  opts.cleanstart = 1;
1198  opts.MQTTVersion = options.MQTTVersion;
1199  opts.will = &wopts;
1201  opts.will->qos = 1;
1202  opts.will->retained = 0;
1204  if (options.haconnections != NULL)
1205  {
1206  opts.serverURIs = options.haconnections;
1207  opts.serverURIcount = options.hacount;
1208  }
1209 
1210  /* Client-1 with Will options */
1211  createOpts.MQTTVersion = MQTTVERSION_5;
1213  NULL, &createOpts);
1214  assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
1215  if (rc != MQTTCLIENT_SUCCESS)
1216  goto exit;
1217 
1219  assert("good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
1220  if (rc != MQTTCLIENT_SUCCESS)
1221  goto exit;
1222 
1223  /* Connect to the broker */
1224  response = MQTTClient_connect5(test6_c1, &opts, NULL, NULL);
1225  assert("good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
1226  MQTTResponse_free(response);
1227  if (rc != MQTTCLIENT_SUCCESS)
1228  goto exit;
1229 
1230  /* Client - 2 (multi-threaded) */
1232  NULL, &createOpts);
1233  assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
1234 
1235  /* Set the callback functions for the client */
1237  assert("good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
1238 
1239  /* Connect to the broker */
1240  opts2.keepAliveInterval = 20;
1241  opts2.cleanstart = 1;
1242  MyLog(LOGA_INFO, "Connecting Client_2 ...");
1243  response = MQTTClient_connect5(test6_c2, &opts2, NULL, NULL);
1244  MQTTResponse_free(response);
1245  assert("Good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d\n", response.reasonCode);
1246 
1247  response = MQTTClient_subscribe5(test6_c2, test6_will_topic, 2, NULL, NULL);
1248  assert("Good rc from subscribe", response.reasonCode == MQTTREASONCODE_GRANTED_QOS_2, "rc was %d\n", response.reasonCode);
1249 
1250  /* now send the command which will break the connection and cause the will message to be sent */
1251  response = MQTTClient_publish5(test6_c1, mqttsas_topic, (int)strlen("TERMINATE"), "TERMINATE", 0, 0, NULL, NULL);
1252  assert("Good rc from publish", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d\n", response.reasonCode);
1253 
1254  MyLog(LOGA_INFO, "Waiting to receive the will message");
1255  count = 0;
1256  while (++count < 40)
1257  {
1258  #if defined(_WIN32)
1259  Sleep(1000L);
1260  #else
1261  sleep(1);
1262  #endif
1264  break;
1265  }
1266  assert("will message arrived", test6_will_message_arrived == 1,
1267  "will_message_arrived was %d\n", test6_will_message_arrived);
1268  assert("connection lost called", test6_connection_lost_called == 1,
1269  "connection_lost_called %d\n", test6_connection_lost_called);
1270 
1272  assert("Good rc from unsubscribe", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
1273 
1275  assert("Client-2 still connected", rc == 1, "isconnected is %d", rc);
1276 
1278  assert("Client-1 not connected", rc == 0, "isconnected is %d", rc);
1279 
1281  assert("Good rc from disconnect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
1282 
1285 
1286 exit:
1287  MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.\n",
1288  (failures == 0) ? "passed" : "failed", testname, tests, failures);
1290  return failures;
1291 }
1292 
1293 
1295 {
1296  char* testname = "test6a";
1300  int rc, count;
1302  char* mqttsas_topic = "MQTTSAS topic";
1304 
1305  failures = 0;
1306  MyLog(LOGA_INFO, "Starting test 6 - connectionLost and binary will messages");
1307  fprintf(xml, "<testcase classname=\"test1\" name=\"connectionLost and binary will messages\"");
1309 
1310  opts.keepAliveInterval = 2;
1311  opts.cleanstart = 1;
1312  opts.MQTTVersion = options.MQTTVersion;
1313  opts.will = &wopts;
1315  opts.will->payload.len = (int)strlen(test6_will_message) + 1;
1316  opts.will->qos = 1;
1317  opts.will->retained = 0;
1319  if (options.haconnections != NULL)
1320  {
1321  opts.serverURIs = options.haconnections;
1322  opts.serverURIcount = options.hacount;
1323  }
1324 
1325  /* Client-1 with Will options */
1326  createOpts.MQTTVersion = MQTTVERSION_5;
1328  NULL, &createOpts);
1329  assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
1330  if (rc != MQTTCLIENT_SUCCESS)
1331  goto exit;
1332 
1334  assert("good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
1335  if (rc != MQTTCLIENT_SUCCESS)
1336  goto exit;
1337 
1338  /* Connect to the broker */
1339  response = MQTTClient_connect5(test6_c1, &opts, NULL, NULL);
1340  assert("good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS,
1341  "rc was %d\n", response.reasonCode);
1342  MQTTResponse_free(response);
1343  if (response.reasonCode != MQTTCLIENT_SUCCESS)
1344  goto exit;
1345 
1346  /* Client - 2 (multi-threaded) */
1348  NULL, &createOpts);
1349  assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
1350 
1351  /* Set the callback functions for the client */
1353  assert("good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
1354 
1355  /* Connect to the broker */
1356  opts2.keepAliveInterval = 20;
1357  opts2.cleanstart = 1;
1358  MyLog(LOGA_INFO, "Connecting Client_2 ...");
1359  response = MQTTClient_connect5(test6_c2, &opts2, NULL, NULL);
1360  MQTTResponse_free(response);
1361  assert("Good rc from connect", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d\n", response.reasonCode);
1362 
1363  response = MQTTClient_subscribe5(test6_c2, test6_will_topic, 2, NULL, NULL);
1364  assert("Good rc from subscribe", response.reasonCode == MQTTREASONCODE_GRANTED_QOS_2, "rc was %d\n", response.reasonCode);
1365 
1366  /* now send the command which will break the connection and cause the will message to be sent */
1367  response = MQTTClient_publish5(test6_c1, mqttsas_topic, (int)strlen("TERMINATE"), "TERMINATE", 0, 0, NULL, NULL);
1368  assert("Good rc from publish", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d\n", response.reasonCode);
1369 
1370  MyLog(LOGA_INFO, "Waiting to receive the will message");
1371  count = 0;
1372  while (++count < 40)
1373  {
1374  #if defined(_WIN32)
1375  Sleep(1000L);
1376  #else
1377  sleep(1);
1378  #endif
1380  break;
1381  }
1382  assert("will message arrived", test6_will_message_arrived == 1,
1383  "will_message_arrived was %d\n", test6_will_message_arrived);
1384  assert("connection lost called", test6_connection_lost_called == 1,
1385  "connection_lost_called %d\n", test6_connection_lost_called);
1386 
1388  assert("Good rc from unsubscribe", response.reasonCode == MQTTCLIENT_SUCCESS, "rc was %d", response.reasonCode);
1389 
1391  assert("Client-2 still connected", rc == 1, "isconnected is %d", rc);
1392 
1394  assert("Client-1 not connected", rc == 0, "isconnected is %d", rc);
1395 
1397  assert("Good rc from disconnect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
1398 
1401 
1402 exit:
1403  MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.\n",
1404  (failures == 0) ? "passed" : "failed", testname, tests, failures);
1406  return failures;
1407 }
1408 
1409 int main(int argc, char** argv)
1410 {
1411  int rc = 0;
1412  int (*tests[])() = {NULL, test1, test2, test3, test4, test5, test6, test6a};
1413  int i;
1414 
1415  xml = fopen("TEST-test1.xml", "w");
1416  fprintf(xml, "<testsuite name=\"test1\" tests=\"%d\">\n", (int)(ARRAY_SIZE(tests) - 1));
1417 
1418  setenv("MQTT_C_CLIENT_TRACE", "ON", 1);
1419  setenv("MQTT_C_CLIENT_TRACE_LEVEL", "ERROR", 1);
1420 
1421  getopts(argc, argv);
1422 
1423  for (i = 0; i < options.iterations; ++i)
1424  {
1425  if (options.test_no == 0)
1426  { /* run all the tests */
1428  rc += tests[options.test_no](options); /* return number of failures. 0 = test succeeded */
1429  }
1430  else
1431  rc = tests[options.test_no](options); /* run just the selected test */
1432  }
1433 
1434  if (rc == 0)
1435  MyLog(LOGA_INFO, "verdict pass");
1436  else
1437  MyLog(LOGA_INFO, "verdict fail");
1438 
1439  fprintf(xml, "</testsuite>\n");
1440  fclose(xml);
1441  return rc;
1442 }
int MQTTClient_disconnect5(MQTTClient handle, int timeout, enum MQTTReasonCodes reason, MQTTProperties *props)
Definition: MQTTClient.c:1919
#define assert(a, b, c, d)
Definition: test15.c:246
int test6_messageArrived(void *context, char *topicName, int topicLen, MQTTClient_message *m)
Definition: test15.c:1167
int MQTTClient_waitForCompletion(MQTTClient handle, MQTTClient_deliveryToken mdt, unsigned long timeout)
Definition: MQTTClient.c:2772
int test6(struct Options options)
Definition: test15.c:1180
enum MQTTPropertyCodes value
char ** haconnections
Definition: test1.c:50
int MQTTClient_receive(MQTTClient handle, char **topicName, int *topicLen, MQTTClient_message **message, unsigned long timeout)
Definition: MQTTClient.c:2674
void logProperties(MQTTProperties *props)
Definition: test15.c:371
MQTTResponse MQTTClient_publish5(MQTTClient handle, const char *topicName, int payloadlen, const void *payload, int qos, int retained, MQTTProperties *properties, MQTTClient_deliveryToken *deliveryToken)
Definition: MQTTClient.c:2247
FMT_INLINE std::basic_string< Char > format(const S &format_str, Args &&...args)
Definition: core.h:2081
#define MQTTCLIENT_SUCCESS
Definition: MQTTClient.h:131
int failures
Definition: test15.c:250
string topic
Definition: test2.py:8
char * cur_output
Definition: test15.c:254
int MQTTClient_isConnected(MQTTClient handle)
Definition: MQTTClient.c:1930
MQTTProperties props
Definition: paho_c_pub.c:54
int MQTTClient_connect(MQTTClient handle, MQTTClient_connectOptions *options)
Definition: MQTTClient.c:1644
void test6_connectionLost(void *context, char *cause)
Definition: test15.c:1152
char * proxy_connection
Definition: test1.c:51
int MQTTClient_publish(MQTTClient handle, const char *topicName, int payloadlen, const void *payload, int qos, int retained, MQTTClient_deliveryToken *deliveryToken)
Definition: MQTTClient.c:2387
MQTTLenString value
char * connection
#define MQTTCLIENT_PERSISTENCE_ERROR
int MQTTClient_disconnect(MQTTClient handle, int timeout)
Definition: MQTTClient.c:1908
void test2_sendAndReceive(MQTTClient *c, int qos, char *test_topic)
Definition: test15.c:584
void MQTTProperties_free(MQTTProperties *props)
int main(int argc, char **argv)
Definition: test15.c:1409
struct pubsub_opts opts
Definition: paho_c_pub.c:42
int MQTTProperties_add(MQTTProperties *props, const MQTTProperty *prop)
char output[3000]
Definition: test15.c:253
size_t strftime(char *str, size_t count, const char *format, const std::tm *time)
Definition: chrono.h:375
int MQTTProperty_getType(enum MQTTPropertyCodes value)
#define malloc(x)
Definition: Heap.h:41
#define MQTTClient_connectOptions_initializer5
Definition: MQTTClient.h:956
char * test6_will_message
Definition: test15.c:1165
int MQTTClient_createWithOptions(MQTTClient *handle, const char *serverURI, const char *clientId, int persistence_type, void *persistence_context, MQTTClient_createOptions *options)
Definition: MQTTClient.c:364
unsigned char retainAsPublished
MQTTResponse MQTTClient_unsubscribe5(MQTTClient handle, const char *topic, MQTTProperties *props)
Definition: MQTTClient.c:2230
void myassert(char *filename, int lineno, char *description, int value, char *format,...)
Definition: test15.c:271
int test4(struct Options options)
Definition: test15.c:1041
#define MQTTClient_message_initializer
Definition: MQTTClient.h:327
enum MQTTReasonCodes reasonCode
Definition: MQTTClient.h:991
static char msg_buf[512]
Definition: Log.c:122
void MQTTResponse_free(MQTTResponse response)
Definition: MQTTClient.c:620
MQTTClient_message test2_pubmsg
Definition: test15.c:548
int test4_run(int qos, int start_mqtt_version, int restore_mqtt_version)
Definition: test15.c:814
#define MQTTClient_willOptions_initializer
Definition: MQTTClient.h:639
struct Options options
int test2_messageArrived(void *context, char *topicName, int topicLen, MQTTClient_message *message)
Definition: test15.c:555
int MQTTClient_setCallbacks(MQTTClient handle, void *context, MQTTClient_connectionLost *cl, MQTTClient_messageArrived *ma, MQTTClient_deliveryComplete *dc)
Definition: MQTTClient.c:1032
#define LOGA_INFO
Definition: test15.c:147
constexpr size_t count()
Definition: core.h:960
int hacount
Definition: test1.c:52
#define assert1(a, b, c, d, e)
Definition: test15.c:247
const char * MQTTPropertyName(enum MQTTPropertyCodes value)
MQTTResponse MQTTClient_publishMessage5(MQTTClient handle, const char *topicName, MQTTClient_message *message, MQTTClient_deliveryToken *deliveryToken)
Definition: MQTTClient.c:2401
description
Definition: setup.py:19
struct MQTTClient_willOptions::@56 payload
#define ARRAY_SIZE(a)
Definition: test15.c:41
#define MQTTVERSION_3_1_1
Definition: MQTTAsync.h:203
enum MQTTPropertyCodes identifier
int test3(struct Options options)
Definition: test15.c:736
#define START_TIME_TYPE
Definition: test15.c:208
int qos
Definition: test6.c:56
void usage(void)
Definition: test15.c:43
const char * topicName
Definition: MQTTClient.h:619
long elapsed(START_TIME_TYPE start_time)
Definition: test15.c:235
void MQTTClient_freeMessage(MQTTClient_message **message)
Definition: MQTTClient.c:601
int test6a(struct Options options)
Definition: test15.c:1294
void * MQTTClient
Definition: MQTTClient.h:246
void MQTTClient_yield(void)
Definition: MQTTClient.c:2730
void MQTTClient_destroy(MQTTClient *handle)
Definition: MQTTClient.c:556
MQTTResponse MQTTClient_connect5(MQTTClient handle, MQTTClient_connectOptions *options, MQTTProperties *connectProperties, MQTTProperties *willProperties)
Definition: MQTTClient.c:1658
#define MQTTCLIENT_PERSISTENCE_DEFAULT
MQTTProperties properties
Definition: MQTTClient.h:324
const char * name
MQTTProperty * array
int test5(struct Options options)
Definition: test15.c:1066
int tests
Definition: test15.c:249
volatile int test2_arrivedcount
Definition: test15.c:546
START_TIME_TYPE global_start_time
Definition: test15.c:252
MQTTClient test6_c2
Definition: test15.c:1148
#define LOGA_DEBUG
Definition: test15.c:146
int MQTTClient_subscribe(MQTTClient handle, const char *topic, int qos)
Definition: MQTTClient.c:2104
#define MQTTVERSION_5
Definition: MQTTAsync.h:207
volatile int test6_connection_lost_called
Definition: test15.c:1150
char * test_topic
Definition: test11.c:307
MQTTResponse MQTTClient_subscribe5(MQTTClient handle, const char *topic, int qos, MQTTSubscribe_options *opts, MQTTProperties *props)
Definition: MQTTClient.c:2090
MQTTClient c
Definition: test10.c:1656
char * test6_will_topic
Definition: test15.c:1164
START_TIME_TYPE start_clock(void)
Definition: test15.c:210
dictionary context
Definition: test2.py:57
void MQTTClient_free(void *memory)
Definition: MQTTClient.c:612
null localtime_s(...)
Definition: chrono.h:286
void MyLog(int LOGA_level, char *format,...)
Definition: test15.c:151
#define MQTTCLIENT_PERSISTENCE_NONE
#define MQTTResponse_initializer
Definition: MQTTClient.h:997
MQTTClient test6_c1
Definition: test15.c:1148
int test2_deliveryCompleted
Definition: test15.c:547
int MQTTClient_deliveryToken
Definition: MQTTClient.h:257
#define MQTTClient_createOptions_initializer
Definition: MQTTClient.h:549
int MQTTClient_getPendingDeliveryTokens(MQTTClient handle, MQTTClient_deliveryToken **tokens)
Definition: MQTTClient.c:2814
void test6_deliveryComplete(void *context, MQTTClient_deliveryToken token)
Definition: test15.c:1159
volatile int test6_will_message_arrived
Definition: test15.c:1149
void test1_sendAndReceive(MQTTClient *c, int qos, char *test_topic)
Definition: test15.c:298
void write_test_result(void)
Definition: test15.c:257
#define MQTTSubscribe_options_initializer
int test1(struct Options options)
Definition: test15.c:409
char *const * serverURIs
Definition: MQTTClient.h:913
void getopts(int argc, char **argv)
Definition: test15.c:71
const char * message
Definition: MQTTClient.h:621
MQTTProperty property
Definition: paho_c_pub.c:53
enum MQTTReasonCodes rc
Definition: test10.c:1112
void test2_deliveryComplete(void *context, MQTTClient_deliveryToken dt)
Definition: test15.c:550
int MQTTClient_create(MQTTClient *handle, const char *serverURI, const char *clientId, int persistence_type, void *persistence_context)
Definition: MQTTClient.c:507
MQTTClient_willOptions * will
Definition: MQTTClient.h:866
MQTTProperties * properties
Definition: MQTTClient.h:994
int test_no
Definition: test1.c:54
int test2(struct Options options)
Definition: test15.c:651
FILE * xml
Definition: test15.c:251
struct MQTTClient_connectOptions::@58 binarypwd
#define MQTTProperties_initializer


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 04:02:48