receive_spec.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. TRACE("Callback received topic=[" << topic << "] length=" << length << "\n")
  19. callback_called = true;
  20. strcpy(lastTopic,topic);
  21. memcpy(lastPayload,payload,length);
  22. lastLength = length;
  23. }
  24. int test_receive_callback() {
  25. IT("receives a callback message");
  26. reset_callback();
  27. ShimClient shimClient;
  28. shimClient.setAllowConnect(true);
  29. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  30. shimClient.respond(connack,4);
  31. PubSubClient client(server, 1883, callback, shimClient);
  32. int rc = client.connect((char*)"client_test1");
  33. IS_TRUE(rc);
  34. byte publish[] = {0x30,0xe,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64};
  35. shimClient.respond(publish,16);
  36. rc = client.loop();
  37. IS_TRUE(rc);
  38. IS_TRUE(callback_called);
  39. IS_TRUE(strcmp(lastTopic,"topic")==0);
  40. IS_TRUE(memcmp(lastPayload,"payload",7)==0);
  41. IS_TRUE(lastLength == 7);
  42. IS_FALSE(shimClient.error());
  43. END_IT
  44. }
  45. int test_receive_stream() {
  46. IT("receives a streamed callback message");
  47. reset_callback();
  48. Stream stream;
  49. stream.expect((uint8_t*)"payload",7);
  50. ShimClient shimClient;
  51. shimClient.setAllowConnect(true);
  52. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  53. shimClient.respond(connack,4);
  54. PubSubClient client(server, 1883, callback, shimClient, stream);
  55. int rc = client.connect((char*)"client_test1");
  56. IS_TRUE(rc);
  57. byte publish[] = {0x30,0xe,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64};
  58. shimClient.respond(publish,16);
  59. rc = client.loop();
  60. IS_TRUE(rc);
  61. IS_TRUE(callback_called);
  62. IS_TRUE(strcmp(lastTopic,"topic")==0);
  63. IS_TRUE(lastLength == 7);
  64. IS_FALSE(stream.error());
  65. IS_FALSE(shimClient.error());
  66. END_IT
  67. }
  68. int test_receive_max_sized_message() {
  69. IT("receives an max-sized message");
  70. reset_callback();
  71. ShimClient shimClient;
  72. shimClient.setAllowConnect(true);
  73. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  74. shimClient.respond(connack,4);
  75. PubSubClient client(server, 1883, callback, shimClient);
  76. int length = 80; // If this is changed to > 128 then the publish packet below
  77. // is no longer valid as it assumes the remaining length
  78. // is a single-byte. Don't make that mistake like I just
  79. // did and lose a whole evening tracking down the issue.
  80. client.setBufferSize(length);
  81. int rc = client.connect((char*)"client_test1");
  82. IS_TRUE(rc);
  83. byte publish[] = {0x30,length-2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64};
  84. byte bigPublish[length];
  85. memset(bigPublish,'A',length);
  86. bigPublish[length] = 'B';
  87. memcpy(bigPublish,publish,16);
  88. shimClient.respond(bigPublish,length);
  89. rc = client.loop();
  90. IS_TRUE(rc);
  91. IS_TRUE(callback_called);
  92. IS_TRUE(strcmp(lastTopic,"topic")==0);
  93. IS_TRUE(lastLength == length-9);
  94. IS_TRUE(memcmp(lastPayload,bigPublish+9,lastLength)==0);
  95. IS_FALSE(shimClient.error());
  96. END_IT
  97. }
  98. int test_receive_oversized_message() {
  99. IT("drops an oversized message");
  100. reset_callback();
  101. ShimClient shimClient;
  102. shimClient.setAllowConnect(true);
  103. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  104. shimClient.respond(connack,4);
  105. int length = 80; // See comment in test_receive_max_sized_message before changing this value
  106. PubSubClient client(server, 1883, callback, shimClient);
  107. client.setBufferSize(length-1);
  108. int rc = client.connect((char*)"client_test1");
  109. IS_TRUE(rc);
  110. byte publish[] = {0x30,length-2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64};
  111. byte bigPublish[length];
  112. memset(bigPublish,'A',length);
  113. bigPublish[length] = 'B';
  114. memcpy(bigPublish,publish,16);
  115. shimClient.respond(bigPublish,length);
  116. rc = client.loop();
  117. IS_TRUE(rc);
  118. IS_FALSE(callback_called);
  119. IS_FALSE(shimClient.error());
  120. END_IT
  121. }
  122. int test_drop_invalid_remaining_length_message() {
  123. IT("drops invalid remaining length message");
  124. reset_callback();
  125. ShimClient shimClient;
  126. shimClient.setAllowConnect(true);
  127. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  128. shimClient.respond(connack,4);
  129. PubSubClient client(server, 1883, callback, shimClient);
  130. int rc = client.connect((char*)"client_test1");
  131. IS_TRUE(rc);
  132. byte publish[] = {0x30,0x92,0x92,0x92,0x92,0x01,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64};
  133. shimClient.respond(publish,20);
  134. rc = client.loop();
  135. IS_FALSE(rc);
  136. IS_FALSE(callback_called);
  137. IS_FALSE(shimClient.error());
  138. END_IT
  139. }
  140. int test_resize_buffer() {
  141. IT("receives a message larger than the default maximum");
  142. reset_callback();
  143. ShimClient shimClient;
  144. shimClient.setAllowConnect(true);
  145. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  146. shimClient.respond(connack,4);
  147. int length = 80; // See comment in test_receive_max_sized_message before changing this value
  148. PubSubClient client(server, 1883, callback, shimClient);
  149. client.setBufferSize(length-1);
  150. int rc = client.connect((char*)"client_test1");
  151. IS_TRUE(rc);
  152. byte publish[] = {0x30,length-2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64};
  153. byte bigPublish[length];
  154. memset(bigPublish,'A',length);
  155. bigPublish[length] = 'B';
  156. memcpy(bigPublish,publish,16);
  157. // Send it twice
  158. shimClient.respond(bigPublish,length);
  159. shimClient.respond(bigPublish,length);
  160. rc = client.loop();
  161. IS_TRUE(rc);
  162. // First message fails as it is too big
  163. IS_FALSE(callback_called);
  164. // Resize the buffer
  165. client.setBufferSize(length);
  166. rc = client.loop();
  167. IS_TRUE(rc);
  168. IS_TRUE(callback_called);
  169. IS_TRUE(strcmp(lastTopic,"topic")==0);
  170. IS_TRUE(lastLength == length-9);
  171. IS_TRUE(memcmp(lastPayload,bigPublish+9,lastLength)==0);
  172. IS_FALSE(shimClient.error());
  173. END_IT
  174. }
  175. int test_receive_oversized_stream_message() {
  176. IT("receive an oversized streamed message");
  177. reset_callback();
  178. Stream stream;
  179. ShimClient shimClient;
  180. shimClient.setAllowConnect(true);
  181. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  182. shimClient.respond(connack,4);
  183. int length = 80; // See comment in test_receive_max_sized_message before changing this value
  184. PubSubClient client(server, 1883, callback, shimClient, stream);
  185. client.setBufferSize(length-1);
  186. int rc = client.connect((char*)"client_test1");
  187. IS_TRUE(rc);
  188. byte publish[] = {0x30,length-2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64};
  189. byte bigPublish[length];
  190. memset(bigPublish,'A',length);
  191. bigPublish[length] = 'B';
  192. memcpy(bigPublish,publish,16);
  193. shimClient.respond(bigPublish,length);
  194. stream.expect(bigPublish+9,length-9);
  195. rc = client.loop();
  196. IS_TRUE(rc);
  197. IS_TRUE(callback_called);
  198. IS_TRUE(strcmp(lastTopic,"topic")==0);
  199. IS_TRUE(lastLength == length-10);
  200. IS_FALSE(stream.error());
  201. IS_FALSE(shimClient.error());
  202. END_IT
  203. }
  204. int test_receive_qos1() {
  205. IT("receives a qos1 message");
  206. reset_callback();
  207. ShimClient shimClient;
  208. shimClient.setAllowConnect(true);
  209. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  210. shimClient.respond(connack,4);
  211. PubSubClient client(server, 1883, callback, shimClient);
  212. int rc = client.connect((char*)"client_test1");
  213. IS_TRUE(rc);
  214. byte publish[] = {0x32,0x10,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x12,0x34,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64};
  215. shimClient.respond(publish,18);
  216. byte puback[] = {0x40,0x2,0x12,0x34};
  217. shimClient.expect(puback,4);
  218. rc = client.loop();
  219. IS_TRUE(rc);
  220. IS_TRUE(callback_called);
  221. IS_TRUE(strcmp(lastTopic,"topic")==0);
  222. IS_TRUE(memcmp(lastPayload,"payload",7)==0);
  223. IS_TRUE(lastLength == 7);
  224. IS_FALSE(shimClient.error());
  225. END_IT
  226. }
  227. int main()
  228. {
  229. SUITE("Receive");
  230. test_receive_callback();
  231. test_receive_stream();
  232. test_receive_max_sized_message();
  233. test_drop_invalid_remaining_length_message();
  234. test_receive_oversized_message();
  235. test_resize_buffer();
  236. test_receive_oversized_stream_message();
  237. test_receive_qos1();
  238. FINISH
  239. }