publish_spec.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include "PubSubClient.h"
  2. #include "ShimClient.h"
  3. #include "Buffer.h"
  4. #include "BDDTest.h"
  5. #include "trace.h"
  6. byte server[] = { 172, 16, 0, 2 };
  7. void callback(char* topic, byte* payload, unsigned int length) {
  8. // handle message arrived
  9. }
  10. int test_publish() {
  11. IT("publishes a null-terminated string");
  12. ShimClient shimClient;
  13. shimClient.setAllowConnect(true);
  14. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  15. shimClient.respond(connack,4);
  16. PubSubClient client(server, 1883, callback, shimClient);
  17. int rc = client.connect((char*)"client_test1");
  18. IS_TRUE(rc);
  19. byte publish[] = {0x30,0xe,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64};
  20. shimClient.expect(publish,16);
  21. rc = client.publish((char*)"topic",(char*)"payload");
  22. IS_TRUE(rc);
  23. IS_FALSE(shimClient.error());
  24. END_IT
  25. }
  26. int test_publish_bytes() {
  27. IT("publishes a byte array");
  28. ShimClient shimClient;
  29. shimClient.setAllowConnect(true);
  30. byte payload[] = { 0x01,0x02,0x03,0x0,0x05 };
  31. int length = 5;
  32. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  33. shimClient.respond(connack,4);
  34. PubSubClient client(server, 1883, callback, shimClient);
  35. int rc = client.connect((char*)"client_test1");
  36. IS_TRUE(rc);
  37. byte publish[] = {0x30,0xc,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x1,0x2,0x3,0x0,0x5};
  38. shimClient.expect(publish,14);
  39. rc = client.publish((char*)"topic",payload,length);
  40. IS_TRUE(rc);
  41. IS_FALSE(shimClient.error());
  42. END_IT
  43. }
  44. int test_publish_retained() {
  45. IT("publishes retained - 1");
  46. ShimClient shimClient;
  47. shimClient.setAllowConnect(true);
  48. byte payload[] = { 0x01,0x02,0x03,0x0,0x05 };
  49. int length = 5;
  50. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  51. shimClient.respond(connack,4);
  52. PubSubClient client(server, 1883, callback, shimClient);
  53. int rc = client.connect((char*)"client_test1");
  54. IS_TRUE(rc);
  55. byte publish[] = {0x31,0xc,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x1,0x2,0x3,0x0,0x5};
  56. shimClient.expect(publish,14);
  57. rc = client.publish((char*)"topic",payload,length,true);
  58. IS_TRUE(rc);
  59. IS_FALSE(shimClient.error());
  60. END_IT
  61. }
  62. int test_publish_retained_2() {
  63. IT("publishes retained - 2");
  64. ShimClient shimClient;
  65. shimClient.setAllowConnect(true);
  66. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  67. shimClient.respond(connack,4);
  68. PubSubClient client(server, 1883, callback, shimClient);
  69. int rc = client.connect((char*)"client_test1");
  70. IS_TRUE(rc);
  71. byte publish[] = {0x31,0xc,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,'A','B','C','D','E'};
  72. shimClient.expect(publish,14);
  73. rc = client.publish((char*)"topic",(char*)"ABCDE",true);
  74. IS_TRUE(rc);
  75. IS_FALSE(shimClient.error());
  76. END_IT
  77. }
  78. int test_publish_not_connected() {
  79. IT("publish fails when not connected");
  80. ShimClient shimClient;
  81. PubSubClient client(server, 1883, callback, shimClient);
  82. int rc = client.publish((char*)"topic",(char*)"payload");
  83. IS_FALSE(rc);
  84. IS_FALSE(shimClient.error());
  85. END_IT
  86. }
  87. int test_publish_too_long() {
  88. IT("publish fails when topic/payload are too long");
  89. ShimClient shimClient;
  90. shimClient.setAllowConnect(true);
  91. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  92. shimClient.respond(connack,4);
  93. PubSubClient client(server, 1883, callback, shimClient);
  94. int rc = client.connect((char*)"client_test1");
  95. IS_TRUE(rc);
  96. // 0 1 2 3 4 5 6 7 8 9 0 1 2
  97. rc = client.publish((char*)"topic",(char*)"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
  98. IS_FALSE(rc);
  99. IS_FALSE(shimClient.error());
  100. END_IT
  101. }
  102. int test_publish_P() {
  103. IT("publishes using PROGMEM");
  104. ShimClient shimClient;
  105. shimClient.setAllowConnect(true);
  106. byte payload[] = { 0x01,0x02,0x03,0x0,0x05 };
  107. int length = 5;
  108. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  109. shimClient.respond(connack,4);
  110. PubSubClient client(server, 1883, callback, shimClient);
  111. int rc = client.connect((char*)"client_test1");
  112. IS_TRUE(rc);
  113. byte publish[] = {0x31,0xc,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x1,0x2,0x3,0x0,0x5};
  114. shimClient.expect(publish,14);
  115. rc = client.publish_P((char*)"topic",payload,length,true);
  116. IS_TRUE(rc);
  117. IS_FALSE(shimClient.error());
  118. END_IT
  119. }
  120. int main()
  121. {
  122. SUITE("Publish");
  123. test_publish();
  124. test_publish_bytes();
  125. test_publish_retained();
  126. test_publish_retained_2();
  127. test_publish_not_connected();
  128. test_publish_too_long();
  129. test_publish_P();
  130. FINISH
  131. }