11-prop-oversize-packet.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <mosquitto.h>
  6. static int run = -1;
  7. static int sent_mid = -1;
  8. void on_connect(struct mosquitto *mosq, void *obj, int rc)
  9. {
  10. if(rc){
  11. exit(1);
  12. }else{
  13. rc = mosquitto_subscribe(mosq, NULL, "0123456789012345678901234567890", 0);
  14. if(rc != MOSQ_ERR_OVERSIZE_PACKET){
  15. printf("Fail on subscribe\n");
  16. exit(1);
  17. }
  18. rc = mosquitto_unsubscribe(mosq, NULL, "0123456789012345678901234567890");
  19. if(rc != MOSQ_ERR_OVERSIZE_PACKET){
  20. printf("Fail on unsubscribe\n");
  21. exit(1);
  22. }
  23. rc = mosquitto_publish(mosq, &sent_mid, "pub/test", strlen("0123456789012345678"), "0123456789012345678", 0, false);
  24. if(rc != MOSQ_ERR_OVERSIZE_PACKET){
  25. printf("Fail on publish 1\n");
  26. exit(1);
  27. }
  28. rc = mosquitto_publish(mosq, &sent_mid, "pub/test", strlen("012345678901234567"), "012345678901234567", 0, false);
  29. if(rc != MOSQ_ERR_SUCCESS){
  30. printf("Fail on publish 2\n");
  31. exit(1);
  32. }
  33. }
  34. }
  35. void on_publish(struct mosquitto *mosq, void *obj, int mid)
  36. {
  37. if(mid == sent_mid){
  38. mosquitto_disconnect(mosq);
  39. run = 0;
  40. }else{
  41. exit(1);
  42. }
  43. }
  44. int main(int argc, char *argv[])
  45. {
  46. int rc;
  47. struct mosquitto *mosq;
  48. int port = atoi(argv[1]);
  49. mosquitto_lib_init();
  50. mosq = mosquitto_new("publish-qos0-test", true, NULL);
  51. if(mosq == NULL){
  52. return 1;
  53. }
  54. mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5);
  55. mosquitto_connect_callback_set(mosq, on_connect);
  56. mosquitto_publish_callback_set(mosq, on_publish);
  57. rc = mosquitto_connect(mosq, "localhost", port, 60);
  58. while(run == -1){
  59. rc = mosquitto_loop(mosq, -1, 1);
  60. }
  61. mosquitto_destroy(mosq);
  62. mosquitto_lib_cleanup();
  63. return run;
  64. }