connect_spec.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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_connect_fails_no_network() {
  11. IT("fails to connect if underlying client doesn't connect");
  12. ShimClient shimClient;
  13. shimClient.setAllowConnect(false);
  14. PubSubClient client(server, 1883, callback, shimClient);
  15. int rc = client.connect((char*)"client_test1");
  16. IS_FALSE(rc);
  17. int state = client.state();
  18. IS_TRUE(state == MQTT_CONNECT_FAILED);
  19. END_IT
  20. }
  21. int test_connect_fails_on_no_response() {
  22. IT("fails to connect if no response received after 15 seconds");
  23. ShimClient shimClient;
  24. shimClient.setAllowConnect(true);
  25. PubSubClient client(server, 1883, callback, shimClient);
  26. int rc = client.connect((char*)"client_test1");
  27. IS_FALSE(rc);
  28. int state = client.state();
  29. IS_TRUE(state == MQTT_CONNECTION_TIMEOUT);
  30. END_IT
  31. }
  32. int test_connect_properly_formatted() {
  33. IT("sends a properly formatted connect packet and succeeds");
  34. ShimClient shimClient;
  35. shimClient.setAllowConnect(true);
  36. byte expectServer[] = { 172, 16, 0, 2 };
  37. shimClient.expectConnect(expectServer,1883);
  38. byte connect[] = {0x10,0x18,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0x2,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31};
  39. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  40. shimClient.expect(connect,26);
  41. shimClient.respond(connack,4);
  42. PubSubClient client(server, 1883, callback, shimClient);
  43. int state = client.state();
  44. IS_TRUE(state == MQTT_DISCONNECTED);
  45. int rc = client.connect((char*)"client_test1");
  46. IS_TRUE(rc);
  47. IS_FALSE(shimClient.error());
  48. state = client.state();
  49. IS_TRUE(state == MQTT_CONNECTED);
  50. END_IT
  51. }
  52. int test_connect_properly_formatted_hostname() {
  53. IT("accepts a hostname");
  54. ShimClient shimClient;
  55. shimClient.setAllowConnect(true);
  56. shimClient.expectConnect((char* const)"localhost",1883);
  57. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  58. shimClient.respond(connack,4);
  59. PubSubClient client((char* const)"localhost", 1883, callback, shimClient);
  60. int rc = client.connect((char*)"client_test1");
  61. IS_TRUE(rc);
  62. IS_FALSE(shimClient.error());
  63. END_IT
  64. }
  65. int test_connect_fails_on_bad_rc() {
  66. IT("fails to connect if a bad return code is received");
  67. ShimClient shimClient;
  68. shimClient.setAllowConnect(true);
  69. byte connack[] = { 0x20, 0x02, 0x00, 0x01 };
  70. shimClient.respond(connack,4);
  71. PubSubClient client(server, 1883, callback, shimClient);
  72. int rc = client.connect((char*)"client_test1");
  73. IS_FALSE(rc);
  74. int state = client.state();
  75. IS_TRUE(state == 0x01);
  76. END_IT
  77. }
  78. int test_connect_accepts_username_password() {
  79. IT("accepts a username and password");
  80. ShimClient shimClient;
  81. shimClient.setAllowConnect(true);
  82. byte connect[] = { 0x10,0x24,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0xc2,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31,0x0,0x4,0x75,0x73,0x65,0x72,0x0,0x4,0x70,0x61,0x73,0x73};
  83. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  84. shimClient.expect(connect,0x26);
  85. shimClient.respond(connack,4);
  86. PubSubClient client(server, 1883, callback, shimClient);
  87. int rc = client.connect((char*)"client_test1",(char*)"user",(char*)"pass");
  88. IS_TRUE(rc);
  89. IS_FALSE(shimClient.error());
  90. END_IT
  91. }
  92. int test_connect_accepts_username_no_password() {
  93. IT("accepts a username but no password");
  94. ShimClient shimClient;
  95. shimClient.setAllowConnect(true);
  96. byte connect[] = { 0x10,0x1e,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0x82,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31,0x0,0x4,0x75,0x73,0x65,0x72};
  97. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  98. shimClient.expect(connect,0x20);
  99. shimClient.respond(connack,4);
  100. PubSubClient client(server, 1883, callback, shimClient);
  101. int rc = client.connect((char*)"client_test1",(char*)"user",0);
  102. IS_TRUE(rc);
  103. IS_FALSE(shimClient.error());
  104. END_IT
  105. }
  106. int test_connect_ignores_password_no_username() {
  107. IT("ignores a password but no username");
  108. ShimClient shimClient;
  109. shimClient.setAllowConnect(true);
  110. byte connect[] = {0x10,0x18,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0x2,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31};
  111. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  112. shimClient.expect(connect,26);
  113. shimClient.respond(connack,4);
  114. PubSubClient client(server, 1883, callback, shimClient);
  115. int rc = client.connect((char*)"client_test1",0,(char*)"pass");
  116. IS_TRUE(rc);
  117. IS_FALSE(shimClient.error());
  118. END_IT
  119. }
  120. int test_connect_with_will() {
  121. IT("accepts a will");
  122. ShimClient shimClient;
  123. shimClient.setAllowConnect(true);
  124. byte connect[] = {0x10,0x30,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0xe,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31,0x0,0x9,0x77,0x69,0x6c,0x6c,0x54,0x6f,0x70,0x69,0x63,0x0,0xb,0x77,0x69,0x6c,0x6c,0x4d,0x65,0x73,0x73,0x61,0x67,0x65};
  125. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  126. shimClient.expect(connect,0x32);
  127. shimClient.respond(connack,4);
  128. PubSubClient client(server, 1883, callback, shimClient);
  129. int rc = client.connect((char*)"client_test1",(char*)"willTopic",1,0,(char*)"willMessage");
  130. IS_TRUE(rc);
  131. IS_FALSE(shimClient.error());
  132. END_IT
  133. }
  134. int test_connect_with_will_username_password() {
  135. IT("accepts a will, username and password");
  136. ShimClient shimClient;
  137. shimClient.setAllowConnect(true);
  138. byte connect[] = {0x10,0x40,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0xce,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31,0x0,0x9,0x77,0x69,0x6c,0x6c,0x54,0x6f,0x70,0x69,0x63,0x0,0xb,0x77,0x69,0x6c,0x6c,0x4d,0x65,0x73,0x73,0x61,0x67,0x65,0x0,0x4,0x75,0x73,0x65,0x72,0x0,0x8,0x70,0x61,0x73,0x73,0x77,0x6f,0x72,0x64};
  139. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  140. shimClient.expect(connect,0x42);
  141. shimClient.respond(connack,4);
  142. PubSubClient client(server, 1883, callback, shimClient);
  143. int rc = client.connect((char*)"client_test1",(char*)"user",(char*)"password",(char*)"willTopic",1,0,(char*)"willMessage");
  144. IS_TRUE(rc);
  145. IS_FALSE(shimClient.error());
  146. END_IT
  147. }
  148. int test_connect_disconnect_connect() {
  149. IT("connects, disconnects and connects again");
  150. ShimClient shimClient;
  151. shimClient.setAllowConnect(true);
  152. byte expectServer[] = { 172, 16, 0, 2 };
  153. shimClient.expectConnect(expectServer,1883);
  154. byte connect[] = {0x10,0x18,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0x2,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31};
  155. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  156. shimClient.expect(connect,26);
  157. shimClient.respond(connack,4);
  158. PubSubClient client(server, 1883, callback, shimClient);
  159. int state = client.state();
  160. IS_TRUE(state == MQTT_DISCONNECTED);
  161. int rc = client.connect((char*)"client_test1");
  162. IS_TRUE(rc);
  163. IS_FALSE(shimClient.error());
  164. state = client.state();
  165. IS_TRUE(state == MQTT_CONNECTED);
  166. byte disconnect[] = {0xE0,0x00};
  167. shimClient.expect(disconnect,2);
  168. client.disconnect();
  169. IS_FALSE(client.connected());
  170. IS_FALSE(shimClient.connected());
  171. IS_FALSE(shimClient.error());
  172. state = client.state();
  173. IS_TRUE(state == MQTT_DISCONNECTED);
  174. shimClient.expect(connect,28);
  175. shimClient.respond(connack,4);
  176. rc = client.connect((char*)"client_test1");
  177. IS_TRUE(rc);
  178. IS_FALSE(shimClient.error());
  179. state = client.state();
  180. IS_TRUE(state == MQTT_CONNECTED);
  181. END_IT
  182. }
  183. int main()
  184. {
  185. SUITE("Connect");
  186. test_connect_fails_no_network();
  187. test_connect_fails_on_no_response();
  188. test_connect_properly_formatted();
  189. test_connect_accepts_username_password();
  190. test_connect_fails_on_bad_rc();
  191. test_connect_properly_formatted_hostname();
  192. test_connect_accepts_username_no_password();
  193. test_connect_ignores_password_no_username();
  194. test_connect_with_will();
  195. test_connect_with_will_username_password();
  196. test_connect_disconnect_connect();
  197. FINISH
  198. }