1 | #include "PubSubClient.h"
|
---|
2 | #include "ShimClient.h"
|
---|
3 | #include "Buffer.h"
|
---|
4 | #include "BDDTest.h"
|
---|
5 | #include "trace.h"
|
---|
6 |
|
---|
7 |
|
---|
8 | byte server[] = { 172, 16, 0, 2 };
|
---|
9 |
|
---|
10 | void callback(char* topic, byte* payload, unsigned int length) {
|
---|
11 | // handle message arrived
|
---|
12 | }
|
---|
13 |
|
---|
14 | int test_subscribe_no_qos() {
|
---|
15 | IT("subscribe without qos defaults to 0");
|
---|
16 | ShimClient shimClient;
|
---|
17 | shimClient.setAllowConnect(true);
|
---|
18 |
|
---|
19 | byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
|
---|
20 | shimClient.respond(connack,4);
|
---|
21 |
|
---|
22 | PubSubClient client(server, 1883, callback, shimClient);
|
---|
23 | int rc = client.connect((char*)"client_test1");
|
---|
24 | IS_TRUE(rc);
|
---|
25 |
|
---|
26 | byte subscribe[] = { 0x82,0xa,0x0,0x2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x0 };
|
---|
27 | shimClient.expect(subscribe,12);
|
---|
28 | byte suback[] = { 0x90,0x3,0x0,0x2,0x0 };
|
---|
29 | shimClient.respond(suback,5);
|
---|
30 |
|
---|
31 | rc = client.subscribe((char*)"topic");
|
---|
32 | IS_TRUE(rc);
|
---|
33 |
|
---|
34 | IS_FALSE(shimClient.error());
|
---|
35 |
|
---|
36 | END_IT
|
---|
37 | }
|
---|
38 |
|
---|
39 | int test_subscribe_qos_1() {
|
---|
40 | IT("subscribes qos 1");
|
---|
41 | ShimClient shimClient;
|
---|
42 | shimClient.setAllowConnect(true);
|
---|
43 |
|
---|
44 | byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
|
---|
45 | shimClient.respond(connack,4);
|
---|
46 |
|
---|
47 | PubSubClient client(server, 1883, callback, shimClient);
|
---|
48 | int rc = client.connect((char*)"client_test1");
|
---|
49 | IS_TRUE(rc);
|
---|
50 |
|
---|
51 | byte subscribe[] = { 0x82,0xa,0x0,0x2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x1 };
|
---|
52 | shimClient.expect(subscribe,12);
|
---|
53 | byte suback[] = { 0x90,0x3,0x0,0x2,0x1 };
|
---|
54 | shimClient.respond(suback,5);
|
---|
55 |
|
---|
56 | rc = client.subscribe((char*)"topic",1);
|
---|
57 | IS_TRUE(rc);
|
---|
58 |
|
---|
59 | IS_FALSE(shimClient.error());
|
---|
60 |
|
---|
61 | END_IT
|
---|
62 | }
|
---|
63 |
|
---|
64 | int test_subscribe_not_connected() {
|
---|
65 | IT("subscribe fails when not connected");
|
---|
66 | ShimClient shimClient;
|
---|
67 |
|
---|
68 | PubSubClient client(server, 1883, callback, shimClient);
|
---|
69 |
|
---|
70 | int rc = client.subscribe((char*)"topic");
|
---|
71 | IS_FALSE(rc);
|
---|
72 |
|
---|
73 | IS_FALSE(shimClient.error());
|
---|
74 |
|
---|
75 | END_IT
|
---|
76 | }
|
---|
77 |
|
---|
78 | int test_subscribe_invalid_qos() {
|
---|
79 | IT("subscribe fails with invalid qos values");
|
---|
80 | ShimClient shimClient;
|
---|
81 | shimClient.setAllowConnect(true);
|
---|
82 |
|
---|
83 | byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
|
---|
84 | shimClient.respond(connack,4);
|
---|
85 |
|
---|
86 | PubSubClient client(server, 1883, callback, shimClient);
|
---|
87 | int rc = client.connect((char*)"client_test1");
|
---|
88 | IS_TRUE(rc);
|
---|
89 |
|
---|
90 | rc = client.subscribe((char*)"topic",2);
|
---|
91 | IS_FALSE(rc);
|
---|
92 | rc = client.subscribe((char*)"topic",254);
|
---|
93 | IS_FALSE(rc);
|
---|
94 |
|
---|
95 | IS_FALSE(shimClient.error());
|
---|
96 |
|
---|
97 | END_IT
|
---|
98 | }
|
---|
99 |
|
---|
100 | int test_subscribe_too_long() {
|
---|
101 | IT("subscribe fails with too long topic");
|
---|
102 | ShimClient shimClient;
|
---|
103 | shimClient.setAllowConnect(true);
|
---|
104 |
|
---|
105 | byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
|
---|
106 | shimClient.respond(connack,4);
|
---|
107 |
|
---|
108 | PubSubClient client(server, 1883, callback, shimClient);
|
---|
109 | int rc = client.connect((char*)"client_test1");
|
---|
110 | IS_TRUE(rc);
|
---|
111 |
|
---|
112 | // max length should be allowed
|
---|
113 | // 0 1 2 3 4 5 6 7 8 9 0 1 2
|
---|
114 | rc = client.subscribe((char*)"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789");
|
---|
115 | IS_TRUE(rc);
|
---|
116 |
|
---|
117 | // 0 1 2 3 4 5 6 7 8 9 0 1 2
|
---|
118 | rc = client.subscribe((char*)"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
|
---|
119 | IS_FALSE(rc);
|
---|
120 |
|
---|
121 | IS_FALSE(shimClient.error());
|
---|
122 |
|
---|
123 | END_IT
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | int test_unsubscribe() {
|
---|
128 | IT("unsubscribes");
|
---|
129 | ShimClient shimClient;
|
---|
130 | shimClient.setAllowConnect(true);
|
---|
131 |
|
---|
132 | byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
|
---|
133 | shimClient.respond(connack,4);
|
---|
134 |
|
---|
135 | PubSubClient client(server, 1883, callback, shimClient);
|
---|
136 | int rc = client.connect((char*)"client_test1");
|
---|
137 | IS_TRUE(rc);
|
---|
138 |
|
---|
139 | byte unsubscribe[] = { 0xA2,0x9,0x0,0x2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63 };
|
---|
140 | shimClient.expect(unsubscribe,12);
|
---|
141 | byte unsuback[] = { 0xB0,0x2,0x0,0x2 };
|
---|
142 | shimClient.respond(unsuback,4);
|
---|
143 |
|
---|
144 | rc = client.unsubscribe((char*)"topic");
|
---|
145 | IS_TRUE(rc);
|
---|
146 |
|
---|
147 | IS_FALSE(shimClient.error());
|
---|
148 |
|
---|
149 | END_IT
|
---|
150 | }
|
---|
151 |
|
---|
152 | int test_unsubscribe_not_connected() {
|
---|
153 | IT("unsubscribe fails when not connected");
|
---|
154 | ShimClient shimClient;
|
---|
155 |
|
---|
156 | PubSubClient client(server, 1883, callback, shimClient);
|
---|
157 |
|
---|
158 | int rc = client.unsubscribe((char*)"topic");
|
---|
159 | IS_FALSE(rc);
|
---|
160 |
|
---|
161 | IS_FALSE(shimClient.error());
|
---|
162 |
|
---|
163 | END_IT
|
---|
164 | }
|
---|
165 |
|
---|
166 | int main()
|
---|
167 | {
|
---|
168 | SUITE("Subscribe");
|
---|
169 | test_subscribe_no_qos();
|
---|
170 | test_subscribe_qos_1();
|
---|
171 | test_subscribe_not_connected();
|
---|
172 | test_subscribe_invalid_qos();
|
---|
173 | test_subscribe_too_long();
|
---|
174 | test_unsubscribe();
|
---|
175 | test_unsubscribe_not_connected();
|
---|
176 | FINISH
|
---|
177 | }
|
---|