connect_spec.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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_non_clean_session() {
  79. IT("sends a properly formatted non-clean session connect packet and succeeds");
  80. ShimClient shimClient;
  81. shimClient.setAllowConnect(true);
  82. byte expectServer[] = { 172, 16, 0, 2 };
  83. shimClient.expectConnect(expectServer,1883);
  84. byte connect[] = {0x10,0x18,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0x0,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31};
  85. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  86. shimClient.expect(connect,26);
  87. shimClient.respond(connack,4);
  88. PubSubClient client(server, 1883, callback, shimClient);
  89. int state = client.state();
  90. IS_TRUE(state == MQTT_DISCONNECTED);
  91. int rc = client.connect((char*)"client_test1",0,0,0,0,0,0,0);
  92. IS_TRUE(rc);
  93. IS_FALSE(shimClient.error());
  94. state = client.state();
  95. IS_TRUE(state == MQTT_CONNECTED);
  96. END_IT
  97. }
  98. int test_connect_accepts_username_password() {
  99. IT("accepts a username and password");
  100. ShimClient shimClient;
  101. shimClient.setAllowConnect(true);
  102. 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};
  103. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  104. shimClient.expect(connect,0x26);
  105. shimClient.respond(connack,4);
  106. PubSubClient client(server, 1883, callback, shimClient);
  107. int rc = client.connect((char*)"client_test1",(char*)"user",(char*)"pass");
  108. IS_TRUE(rc);
  109. IS_FALSE(shimClient.error());
  110. END_IT
  111. }
  112. int test_connect_accepts_username_no_password() {
  113. IT("accepts a username but no password");
  114. ShimClient shimClient;
  115. shimClient.setAllowConnect(true);
  116. 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};
  117. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  118. shimClient.expect(connect,0x20);
  119. shimClient.respond(connack,4);
  120. PubSubClient client(server, 1883, callback, shimClient);
  121. int rc = client.connect((char*)"client_test1",(char*)"user",0);
  122. IS_TRUE(rc);
  123. IS_FALSE(shimClient.error());
  124. END_IT
  125. }
  126. int test_connect_accepts_username_blank_password() {
  127. IT("accepts a username and blank password");
  128. ShimClient shimClient;
  129. shimClient.setAllowConnect(true);
  130. byte connect[] = { 0x10,0x20,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,0x0};
  131. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  132. shimClient.expect(connect,0x26);
  133. shimClient.respond(connack,4);
  134. PubSubClient client(server, 1883, callback, shimClient);
  135. int rc = client.connect((char*)"client_test1",(char*)"user",(char*)"pass");
  136. IS_TRUE(rc);
  137. IS_FALSE(shimClient.error());
  138. END_IT
  139. }
  140. int test_connect_ignores_password_no_username() {
  141. IT("ignores a password but no username");
  142. ShimClient shimClient;
  143. shimClient.setAllowConnect(true);
  144. 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};
  145. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  146. shimClient.expect(connect,26);
  147. shimClient.respond(connack,4);
  148. PubSubClient client(server, 1883, callback, shimClient);
  149. int rc = client.connect((char*)"client_test1",0,(char*)"pass");
  150. IS_TRUE(rc);
  151. IS_FALSE(shimClient.error());
  152. END_IT
  153. }
  154. int test_connect_with_will() {
  155. IT("accepts a will");
  156. ShimClient shimClient;
  157. shimClient.setAllowConnect(true);
  158. 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};
  159. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  160. shimClient.expect(connect,0x32);
  161. shimClient.respond(connack,4);
  162. PubSubClient client(server, 1883, callback, shimClient);
  163. int rc = client.connect((char*)"client_test1",(char*)"willTopic",1,0,(char*)"willMessage");
  164. IS_TRUE(rc);
  165. IS_FALSE(shimClient.error());
  166. END_IT
  167. }
  168. int test_connect_with_will_username_password() {
  169. IT("accepts a will, username and password");
  170. ShimClient shimClient;
  171. shimClient.setAllowConnect(true);
  172. 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};
  173. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  174. shimClient.expect(connect,0x42);
  175. shimClient.respond(connack,4);
  176. PubSubClient client(server, 1883, callback, shimClient);
  177. int rc = client.connect((char*)"client_test1",(char*)"user",(char*)"password",(char*)"willTopic",1,0,(char*)"willMessage");
  178. IS_TRUE(rc);
  179. IS_FALSE(shimClient.error());
  180. END_IT
  181. }
  182. int test_connect_disconnect_connect() {
  183. IT("connects, disconnects and connects again");
  184. ShimClient shimClient;
  185. shimClient.setAllowConnect(true);
  186. byte expectServer[] = { 172, 16, 0, 2 };
  187. shimClient.expectConnect(expectServer,1883);
  188. 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};
  189. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  190. shimClient.expect(connect,26);
  191. shimClient.respond(connack,4);
  192. PubSubClient client(server, 1883, callback, shimClient);
  193. int state = client.state();
  194. IS_TRUE(state == MQTT_DISCONNECTED);
  195. int rc = client.connect((char*)"client_test1");
  196. IS_TRUE(rc);
  197. IS_FALSE(shimClient.error());
  198. state = client.state();
  199. IS_TRUE(state == MQTT_CONNECTED);
  200. byte disconnect[] = {0xE0,0x00};
  201. shimClient.expect(disconnect,2);
  202. client.disconnect();
  203. IS_FALSE(client.connected());
  204. IS_FALSE(shimClient.connected());
  205. IS_FALSE(shimClient.error());
  206. state = client.state();
  207. IS_TRUE(state == MQTT_DISCONNECTED);
  208. shimClient.expect(connect,28);
  209. shimClient.respond(connack,4);
  210. rc = client.connect((char*)"client_test1");
  211. IS_TRUE(rc);
  212. IS_FALSE(shimClient.error());
  213. state = client.state();
  214. IS_TRUE(state == MQTT_CONNECTED);
  215. END_IT
  216. }
  217. int main()
  218. {
  219. SUITE("Connect");
  220. test_connect_fails_no_network();
  221. test_connect_fails_on_no_response();
  222. test_connect_properly_formatted();
  223. test_connect_non_clean_session();
  224. test_connect_accepts_username_password();
  225. test_connect_fails_on_bad_rc();
  226. test_connect_properly_formatted_hostname();
  227. test_connect_accepts_username_no_password();
  228. test_connect_ignores_password_no_username();
  229. test_connect_with_will();
  230. test_connect_with_will_username_password();
  231. test_connect_disconnect_connect();
  232. FINISH
  233. }