source: rtos_arduino/trunk/arduino_lib/libraries/pubsubclient-2.6/tests/src/receive_spec.cpp@ 209

Last change on this file since 209 was 209, checked in by ertl-honda, 8 years ago

BlueMix用のフィアルを追加

File size: 5.9 KB
Line 
1#include "PubSubClient.h"
2#include "ShimClient.h"
3#include "Buffer.h"
4#include "BDDTest.h"
5#include "trace.h"
6
7
8byte server[] = { 172, 16, 0, 2 };
9
10bool callback_called = false;
11char lastTopic[1024];
12char lastPayload[1024];
13unsigned int lastLength;
14
15void reset_callback() {
16 callback_called = false;
17 lastTopic[0] = '\0';
18 lastPayload[0] = '\0';
19 lastLength = 0;
20}
21
22void callback(char* topic, byte* payload, unsigned int length) {
23 callback_called = true;
24 strcpy(lastTopic,topic);
25 memcpy(lastPayload,payload,length);
26 lastLength = length;
27}
28
29int test_receive_callback() {
30 IT("receives a callback message");
31 reset_callback();
32
33 ShimClient shimClient;
34 shimClient.setAllowConnect(true);
35
36 byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
37 shimClient.respond(connack,4);
38
39 PubSubClient client(server, 1883, callback, shimClient);
40 int rc = client.connect((char*)"client_test1");
41 IS_TRUE(rc);
42
43 byte publish[] = {0x30,0xe,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64};
44 shimClient.respond(publish,16);
45
46 rc = client.loop();
47
48 IS_TRUE(rc);
49
50 IS_TRUE(callback_called);
51 IS_TRUE(strcmp(lastTopic,"topic")==0);
52 IS_TRUE(memcmp(lastPayload,"payload",7)==0);
53 IS_TRUE(lastLength == 7);
54
55 IS_FALSE(shimClient.error());
56
57 END_IT
58}
59
60int test_receive_stream() {
61 IT("receives a streamed callback message");
62 reset_callback();
63
64 Stream stream;
65 stream.expect((uint8_t*)"payload",7);
66
67 ShimClient shimClient;
68 shimClient.setAllowConnect(true);
69
70 byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
71 shimClient.respond(connack,4);
72
73 PubSubClient client(server, 1883, callback, shimClient, stream);
74 int rc = client.connect((char*)"client_test1");
75 IS_TRUE(rc);
76
77 byte publish[] = {0x30,0xe,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64};
78 shimClient.respond(publish,16);
79
80 rc = client.loop();
81
82 IS_TRUE(rc);
83
84 IS_TRUE(callback_called);
85 IS_TRUE(strcmp(lastTopic,"topic")==0);
86 IS_TRUE(lastLength == 7);
87
88 IS_FALSE(stream.error());
89 IS_FALSE(shimClient.error());
90
91 END_IT
92}
93
94int test_receive_max_sized_message() {
95 IT("receives an max-sized message");
96 reset_callback();
97
98 ShimClient shimClient;
99 shimClient.setAllowConnect(true);
100
101 byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
102 shimClient.respond(connack,4);
103
104 PubSubClient client(server, 1883, callback, shimClient);
105 int rc = client.connect((char*)"client_test1");
106 IS_TRUE(rc);
107
108 int length = MQTT_MAX_PACKET_SIZE;
109 byte publish[] = {0x30,length-2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64};
110 byte bigPublish[length];
111 memset(bigPublish,'A',length);
112 bigPublish[length] = 'B';
113 memcpy(bigPublish,publish,16);
114 shimClient.respond(bigPublish,length);
115
116 rc = client.loop();
117
118 IS_TRUE(rc);
119
120 IS_TRUE(callback_called);
121 IS_TRUE(strcmp(lastTopic,"topic")==0);
122 IS_TRUE(lastLength == length-9);
123 IS_TRUE(memcmp(lastPayload,bigPublish+9,lastLength)==0);
124
125 IS_FALSE(shimClient.error());
126
127 END_IT
128}
129
130int test_receive_oversized_message() {
131 IT("drops an oversized message");
132 reset_callback();
133
134 ShimClient shimClient;
135 shimClient.setAllowConnect(true);
136
137 byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
138 shimClient.respond(connack,4);
139
140 PubSubClient client(server, 1883, callback, shimClient);
141 int rc = client.connect((char*)"client_test1");
142 IS_TRUE(rc);
143
144 int length = MQTT_MAX_PACKET_SIZE+1;
145 byte publish[] = {0x30,length-2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64};
146 byte bigPublish[length];
147 memset(bigPublish,'A',length);
148 bigPublish[length] = 'B';
149 memcpy(bigPublish,publish,16);
150 shimClient.respond(bigPublish,length);
151
152 rc = client.loop();
153
154 IS_TRUE(rc);
155
156 IS_FALSE(callback_called);
157
158 IS_FALSE(shimClient.error());
159
160 END_IT
161}
162
163int test_receive_oversized_stream_message() {
164 IT("drops an oversized message");
165 reset_callback();
166
167 Stream stream;
168
169 ShimClient shimClient;
170 shimClient.setAllowConnect(true);
171
172 byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
173 shimClient.respond(connack,4);
174
175 PubSubClient client(server, 1883, callback, shimClient, stream);
176 int rc = client.connect((char*)"client_test1");
177 IS_TRUE(rc);
178
179 int length = MQTT_MAX_PACKET_SIZE+1;
180 byte publish[] = {0x30,length-2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64};
181
182 byte bigPublish[length];
183 memset(bigPublish,'A',length);
184 bigPublish[length] = 'B';
185 memcpy(bigPublish,publish,16);
186
187 shimClient.respond(bigPublish,length);
188 stream.expect(bigPublish+9,length-9);
189
190 rc = client.loop();
191
192 IS_TRUE(rc);
193
194 IS_TRUE(callback_called);
195 IS_TRUE(strcmp(lastTopic,"topic")==0);
196 IS_TRUE(lastLength == length-9);
197
198 IS_FALSE(stream.error());
199 IS_FALSE(shimClient.error());
200
201 END_IT
202}
203
204int test_receive_qos1() {
205 IT("receives a qos1 message");
206 reset_callback();
207
208 ShimClient shimClient;
209 shimClient.setAllowConnect(true);
210
211 byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
212 shimClient.respond(connack,4);
213
214 PubSubClient client(server, 1883, callback, shimClient);
215 int rc = client.connect((char*)"client_test1");
216 IS_TRUE(rc);
217
218 byte publish[] = {0x32,0x10,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x12,0x34,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64};
219 shimClient.respond(publish,18);
220
221 byte puback[] = {0x40,0x2,0x12,0x34};
222 shimClient.expect(puback,4);
223
224 rc = client.loop();
225
226 IS_TRUE(rc);
227
228 IS_TRUE(callback_called);
229 IS_TRUE(strcmp(lastTopic,"topic")==0);
230 IS_TRUE(memcmp(lastPayload,"payload",7)==0);
231 IS_TRUE(lastLength == 7);
232
233 IS_FALSE(shimClient.error());
234
235 END_IT
236}
237
238int main()
239{
240 SUITE("Receive");
241 test_receive_callback();
242 test_receive_stream();
243 test_receive_max_sized_message();
244 test_receive_oversized_message();
245 test_receive_oversized_stream_message();
246 test_receive_qos1();
247
248 FINISH
249}
Note: See TracBrowser for help on using the repository browser.