receive_spec.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. bool callback_called = false;
  8. char lastTopic[1024];
  9. char lastPayload[1024];
  10. unsigned int lastLength;
  11. void reset_callback() {
  12. callback_called = false;
  13. lastTopic[0] = '\0';
  14. lastPayload[0] = '\0';
  15. lastLength = 0;
  16. }
  17. void callback(char* topic, byte* payload, unsigned int length) {
  18. callback_called = true;
  19. strcpy(lastTopic,topic);
  20. memcpy(lastPayload,payload,length);
  21. lastLength = length;
  22. }
  23. int test_receive_callback() {
  24. IT("receives a callback message");
  25. reset_callback();
  26. ShimClient shimClient;
  27. shimClient.setAllowConnect(true);
  28. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  29. shimClient.respond(connack,4);
  30. PubSubClient client(server, 1883, callback, shimClient);
  31. int rc = client.connect((char*)"client_test1");
  32. IS_TRUE(rc);
  33. byte publish[] = {0x30,0xe,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64};
  34. shimClient.respond(publish,16);
  35. rc = client.loop();
  36. IS_TRUE(rc);
  37. IS_TRUE(callback_called);
  38. IS_TRUE(strcmp(lastTopic,"topic")==0);
  39. IS_TRUE(memcmp(lastPayload,"payload",7)==0);
  40. IS_TRUE(lastLength == 7);
  41. IS_FALSE(shimClient.error());
  42. END_IT
  43. }
  44. int test_receive_stream() {
  45. IT("receives a streamed callback message");
  46. reset_callback();
  47. Stream stream;
  48. stream.expect((uint8_t*)"payload",7);
  49. ShimClient shimClient;
  50. shimClient.setAllowConnect(true);
  51. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  52. shimClient.respond(connack,4);
  53. PubSubClient client(server, 1883, callback, shimClient, stream);
  54. int rc = client.connect((char*)"client_test1");
  55. IS_TRUE(rc);
  56. byte publish[] = {0x30,0xe,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64};
  57. shimClient.respond(publish,16);
  58. rc = client.loop();
  59. IS_TRUE(rc);
  60. IS_TRUE(callback_called);
  61. IS_TRUE(strcmp(lastTopic,"topic")==0);
  62. IS_TRUE(lastLength == 7);
  63. IS_FALSE(stream.error());
  64. IS_FALSE(shimClient.error());
  65. END_IT
  66. }
  67. int test_receive_max_sized_message() {
  68. IT("receives an max-sized message");
  69. reset_callback();
  70. ShimClient shimClient;
  71. shimClient.setAllowConnect(true);
  72. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  73. shimClient.respond(connack,4);
  74. PubSubClient client(server, 1883, callback, shimClient);
  75. int rc = client.connect((char*)"client_test1");
  76. IS_TRUE(rc);
  77. int length = MQTT_MAX_PACKET_SIZE;
  78. byte publish[] = {0x30,length-2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64};
  79. byte bigPublish[length];
  80. memset(bigPublish,'A',length);
  81. bigPublish[length] = 'B';
  82. memcpy(bigPublish,publish,16);
  83. shimClient.respond(bigPublish,length);
  84. rc = client.loop();
  85. IS_TRUE(rc);
  86. IS_TRUE(callback_called);
  87. IS_TRUE(strcmp(lastTopic,"topic")==0);
  88. IS_TRUE(lastLength == length-9);
  89. IS_TRUE(memcmp(lastPayload,bigPublish+9,lastLength)==0);
  90. IS_FALSE(shimClient.error());
  91. END_IT
  92. }
  93. int test_receive_oversized_message() {
  94. IT("drops an oversized message");
  95. reset_callback();
  96. ShimClient shimClient;
  97. shimClient.setAllowConnect(true);
  98. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  99. shimClient.respond(connack,4);
  100. PubSubClient client(server, 1883, callback, shimClient);
  101. int rc = client.connect((char*)"client_test1");
  102. IS_TRUE(rc);
  103. int length = MQTT_MAX_PACKET_SIZE+1;
  104. byte publish[] = {0x30,length-2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64};
  105. byte bigPublish[length];
  106. memset(bigPublish,'A',length);
  107. bigPublish[length] = 'B';
  108. memcpy(bigPublish,publish,16);
  109. shimClient.respond(bigPublish,length);
  110. rc = client.loop();
  111. IS_TRUE(rc);
  112. IS_FALSE(callback_called);
  113. IS_FALSE(shimClient.error());
  114. END_IT
  115. }
  116. int test_receive_oversized_stream_message() {
  117. IT("drops an oversized message");
  118. reset_callback();
  119. Stream stream;
  120. ShimClient shimClient;
  121. shimClient.setAllowConnect(true);
  122. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  123. shimClient.respond(connack,4);
  124. PubSubClient client(server, 1883, callback, shimClient, stream);
  125. int rc = client.connect((char*)"client_test1");
  126. IS_TRUE(rc);
  127. int length = MQTT_MAX_PACKET_SIZE+1;
  128. byte publish[] = {0x30,length-2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64};
  129. byte bigPublish[length];
  130. memset(bigPublish,'A',length);
  131. bigPublish[length] = 'B';
  132. memcpy(bigPublish,publish,16);
  133. shimClient.respond(bigPublish,length);
  134. stream.expect(bigPublish+9,length-9);
  135. rc = client.loop();
  136. IS_TRUE(rc);
  137. IS_TRUE(callback_called);
  138. IS_TRUE(strcmp(lastTopic,"topic")==0);
  139. IS_TRUE(lastLength == length-9);
  140. IS_FALSE(stream.error());
  141. IS_FALSE(shimClient.error());
  142. END_IT
  143. }
  144. int test_receive_qos1() {
  145. IT("receives a qos1 message");
  146. reset_callback();
  147. ShimClient shimClient;
  148. shimClient.setAllowConnect(true);
  149. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  150. shimClient.respond(connack,4);
  151. PubSubClient client(server, 1883, callback, shimClient);
  152. int rc = client.connect((char*)"client_test1");
  153. IS_TRUE(rc);
  154. byte publish[] = {0x32,0x10,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x12,0x34,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64};
  155. shimClient.respond(publish,18);
  156. byte puback[] = {0x40,0x2,0x12,0x34};
  157. shimClient.expect(puback,4);
  158. rc = client.loop();
  159. IS_TRUE(rc);
  160. IS_TRUE(callback_called);
  161. IS_TRUE(strcmp(lastTopic,"topic")==0);
  162. IS_TRUE(memcmp(lastPayload,"payload",7)==0);
  163. IS_TRUE(lastLength == 7);
  164. IS_FALSE(shimClient.error());
  165. END_IT
  166. }
  167. int main()
  168. {
  169. SUITE("Receive");
  170. test_receive_callback();
  171. test_receive_stream();
  172. test_receive_max_sized_message();
  173. test_receive_oversized_message();
  174. test_receive_oversized_stream_message();
  175. test_receive_qos1();
  176. FINISH
  177. }