subscribe_spec.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_subscribe_no_qos() {
  11. IT("subscribe without qos defaults to 0");
  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 subscribe[] = { 0x82,0xa,0x0,0x2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x0 };
  20. shimClient.expect(subscribe,12);
  21. byte suback[] = { 0x90,0x3,0x0,0x2,0x0 };
  22. shimClient.respond(suback,5);
  23. rc = client.subscribe((char*)"topic");
  24. IS_TRUE(rc);
  25. IS_FALSE(shimClient.error());
  26. END_IT
  27. }
  28. int test_subscribe_qos_1() {
  29. IT("subscribes qos 1");
  30. ShimClient shimClient;
  31. shimClient.setAllowConnect(true);
  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 subscribe[] = { 0x82,0xa,0x0,0x2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x1 };
  38. shimClient.expect(subscribe,12);
  39. byte suback[] = { 0x90,0x3,0x0,0x2,0x1 };
  40. shimClient.respond(suback,5);
  41. rc = client.subscribe((char*)"topic",1);
  42. IS_TRUE(rc);
  43. IS_FALSE(shimClient.error());
  44. END_IT
  45. }
  46. int test_subscribe_not_connected() {
  47. IT("subscribe fails when not connected");
  48. ShimClient shimClient;
  49. PubSubClient client(server, 1883, callback, shimClient);
  50. int rc = client.subscribe((char*)"topic");
  51. IS_FALSE(rc);
  52. IS_FALSE(shimClient.error());
  53. END_IT
  54. }
  55. int test_subscribe_invalid_qos() {
  56. IT("subscribe fails with invalid qos values");
  57. ShimClient shimClient;
  58. shimClient.setAllowConnect(true);
  59. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  60. shimClient.respond(connack,4);
  61. PubSubClient client(server, 1883, callback, shimClient);
  62. int rc = client.connect((char*)"client_test1");
  63. IS_TRUE(rc);
  64. rc = client.subscribe((char*)"topic",2);
  65. IS_FALSE(rc);
  66. rc = client.subscribe((char*)"topic",254);
  67. IS_FALSE(rc);
  68. IS_FALSE(shimClient.error());
  69. END_IT
  70. }
  71. int test_subscribe_too_long() {
  72. IT("subscribe fails with too long topic");
  73. ShimClient shimClient;
  74. shimClient.setAllowConnect(true);
  75. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  76. shimClient.respond(connack,4);
  77. PubSubClient client(server, 1883, callback, shimClient);
  78. int rc = client.connect((char*)"client_test1");
  79. IS_TRUE(rc);
  80. // max length should be allowed
  81. // 0 1 2 3 4 5 6 7 8 9 0 1 2
  82. rc = client.subscribe((char*)"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789");
  83. IS_TRUE(rc);
  84. // 0 1 2 3 4 5 6 7 8 9 0 1 2
  85. rc = client.subscribe((char*)"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
  86. IS_FALSE(rc);
  87. IS_FALSE(shimClient.error());
  88. END_IT
  89. }
  90. int test_unsubscribe() {
  91. IT("unsubscribes");
  92. ShimClient shimClient;
  93. shimClient.setAllowConnect(true);
  94. byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
  95. shimClient.respond(connack,4);
  96. PubSubClient client(server, 1883, callback, shimClient);
  97. int rc = client.connect((char*)"client_test1");
  98. IS_TRUE(rc);
  99. byte unsubscribe[] = { 0xA2,0x9,0x0,0x2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63 };
  100. shimClient.expect(unsubscribe,12);
  101. byte unsuback[] = { 0xB0,0x2,0x0,0x2 };
  102. shimClient.respond(unsuback,4);
  103. rc = client.unsubscribe((char*)"topic");
  104. IS_TRUE(rc);
  105. IS_FALSE(shimClient.error());
  106. END_IT
  107. }
  108. int test_unsubscribe_not_connected() {
  109. IT("unsubscribe fails when not connected");
  110. ShimClient shimClient;
  111. PubSubClient client(server, 1883, callback, shimClient);
  112. int rc = client.unsubscribe((char*)"topic");
  113. IS_FALSE(rc);
  114. IS_FALSE(shimClient.error());
  115. END_IT
  116. }
  117. int main()
  118. {
  119. SUITE("Subscribe");
  120. test_subscribe_no_qos();
  121. test_subscribe_qos_1();
  122. test_subscribe_not_connected();
  123. test_subscribe_invalid_qos();
  124. test_subscribe_too_long();
  125. test_unsubscribe();
  126. test_unsubscribe_not_connected();
  127. FINISH
  128. }