FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pubsubclient/tests/src/receive_spec.cpp at master · ArduinoLibrariesForks/pubsubclient · GitHub
ArduinoLibrariesForks
pubsubclient
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pubsubclient
/
tests
/
src
/
receive_spec.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
279 lines (196 loc) · 6.62 KB
Breadcrumbs
pubsubclient
/
tests
/
src
/
receive_spec.cpp
Copy path
File metadata and controls
279 lines (196 loc) · 6.62 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#
include
"
PubSubClient.h
"
#
include
"
ShimClient.h
"
#
include
"
Buffer.h
"
#
include
"
BDDTest.h
"
#
include
"
trace.h
"
byte server[] = {
172
,
16
,
0
,
2
};
bool
callback_called =
false
;
char
lastTopic[
1024
];
char
lastPayload[
1024
];
unsigned
int
lastLength;
void
reset_callback
() {
callback_called =
false
;
lastTopic[
0
] =
'
\0
'
;
lastPayload[
0
] =
'
\0
'
;
lastLength =
0
;
}
void
callback
(
char
* topic, byte* payload,
unsigned
int
length) {
callback_called =
true
;
strcpy
(lastTopic,topic);
memcpy
(lastPayload,payload,length);
lastLength = length;
}
int
test_receive_callback
() {
IT
(
"
receives a callback message
"
);
reset_callback
();
ShimClient shimClient;
shimClient.
setAllowConnect
(
true
);
byte connack[] = {
0x20
,
0x02
,
0x00
,
0x00
};
shimClient.
respond
(connack,
4
);
PubSubClient
client
(server,
1883
, callback, shimClient);
int
rc = client.
connect
((
char
*)
"
client_test1
"
);
IS_TRUE
(rc);
byte publish[] = {
0x30
,
0xe
,
0x0
,
0x5
,
0x74
,
0x6f
,
0x70
,
0x69
,
0x63
,
0x70
,
0x61
,
0x79
,
0x6c
,
0x6f
,
0x61
,
0x64
};
shimClient.
respond
(publish,
16
);
rc = client.
loop
();
IS_TRUE
(rc);
IS_TRUE
(callback_called);
IS_TRUE
(
strcmp
(lastTopic,
"
topic
"
)==
0
);
IS_TRUE
(
memcmp
(lastPayload,
"
payload
"
,
7
)==
0
);
IS_TRUE
(lastLength ==
7
);
IS_FALSE
(shimClient.
error
());
END_IT
}
int
test_receive_stream
() {
IT
(
"
receives a streamed callback message
"
);
reset_callback
();
Stream stream;
stream.
expect
((
uint8_t
*)
"
payload
"
,
7
);
ShimClient shimClient;
shimClient.
setAllowConnect
(
true
);
byte connack[] = {
0x20
,
0x02
,
0x00
,
0x00
};
shimClient.
respond
(connack,
4
);
PubSubClient
client
(server,
1883
, callback, shimClient, stream);
int
rc = client.
connect
((
char
*)
"
client_test1
"
);
IS_TRUE
(rc);
byte publish[] = {
0x30
,
0xe
,
0x0
,
0x5
,
0x74
,
0x6f
,
0x70
,
0x69
,
0x63
,
0x70
,
0x61
,
0x79
,
0x6c
,
0x6f
,
0x61
,
0x64
};
shimClient.
respond
(publish,
16
);
rc = client.
loop
();
IS_TRUE
(rc);
IS_TRUE
(callback_called);
IS_TRUE
(
strcmp
(lastTopic,
"
topic
"
)==
0
);
IS_TRUE
(lastLength ==
7
);
IS_FALSE
(stream.
error
());
IS_FALSE
(shimClient.
error
());
END_IT
}
int
test_receive_max_sized_message
() {
IT
(
"
receives an max-sized message
"
);
reset_callback
();
ShimClient shimClient;
shimClient.
setAllowConnect
(
true
);
byte connack[] = {
0x20
,
0x02
,
0x00
,
0x00
};
shimClient.
respond
(connack,
4
);
PubSubClient
client
(server,
1883
, callback, shimClient);
int
rc = client.
connect
((
char
*)
"
client_test1
"
);
IS_TRUE
(rc);
int
length =
MQTT_MAX_PACKET_SIZE
;
byte publish[] = {
0x30
,length-
2
,
0x0
,
0x5
,
0x74
,
0x6f
,
0x70
,
0x69
,
0x63
,
0x70
,
0x61
,
0x79
,
0x6c
,
0x6f
,
0x61
,
0x64
};
byte bigPublish[length];
memset
(bigPublish,
'
A
'
,length);
bigPublish[length] =
'
B
'
;
memcpy
(bigPublish,publish,
16
);
shimClient.
respond
(bigPublish,length);
rc = client.
loop
();
IS_TRUE
(rc);
IS_TRUE
(callback_called);
IS_TRUE
(
strcmp
(lastTopic,
"
topic
"
)==
0
);
IS_TRUE
(lastLength == length-
9
);
IS_TRUE
(
memcmp
(lastPayload,bigPublish+
9
,lastLength)==
0
);
IS_FALSE
(shimClient.
error
());
END_IT
}
int
test_receive_oversized_message
() {
IT
(
"
drops an oversized message
"
);
reset_callback
();
ShimClient shimClient;
shimClient.
setAllowConnect
(
true
);
byte connack[] = {
0x20
,
0x02
,
0x00
,
0x00
};
shimClient.
respond
(connack,
4
);
PubSubClient
client
(server,
1883
, callback, shimClient);
int
rc = client.
connect
((
char
*)
"
client_test1
"
);
IS_TRUE
(rc);
int
length =
MQTT_MAX_PACKET_SIZE
+
1
;
byte publish[] = {
0x30
,length-
2
,
0x0
,
0x5
,
0x74
,
0x6f
,
0x70
,
0x69
,
0x63
,
0x70
,
0x61
,
0x79
,
0x6c
,
0x6f
,
0x61
,
0x64
};
byte bigPublish[length];
memset
(bigPublish,
'
A
'
,length);
bigPublish[length] =
'
B
'
;
memcpy
(bigPublish,publish,
16
);
shimClient.
respond
(bigPublish,length);
rc = client.
loop
();
IS_TRUE
(rc);
IS_FALSE
(callback_called);
IS_FALSE
(shimClient.
error
());
END_IT
}
int
test_drop_invalid_remaining_length_message
() {
IT
(
"
drops invalid remaining length message
"
);
reset_callback
();
ShimClient shimClient;
shimClient.
setAllowConnect
(
true
);
byte connack[] = {
0x20
,
0x02
,
0x00
,
0x00
};
shimClient.
respond
(connack,
4
);
PubSubClient
client
(server,
1883
, callback, shimClient);
int
rc = client.
connect
((
char
*)
"
client_test1
"
);
IS_TRUE
(rc);
byte publish[] = {
0x30
,
0x92
,
0x92
,
0x92
,
0x92
,
0x01
,
0x0
,
0x5
,
0x74
,
0x6f
,
0x70
,
0x69
,
0x63
,
0x70
,
0x61
,
0x79
,
0x6c
,
0x6f
,
0x61
,
0x64
};
shimClient.
respond
(publish,
20
);
rc = client.
loop
();
IS_FALSE
(rc);
IS_FALSE
(callback_called);
IS_FALSE
(shimClient.
error
());
END_IT
}
int
test_receive_oversized_stream_message
() {
IT
(
"
drops an oversized message
"
);
reset_callback
();
Stream stream;
ShimClient shimClient;
shimClient.
setAllowConnect
(
true
);
byte connack[] = {
0x20
,
0x02
,
0x00
,
0x00
};
shimClient.
respond
(connack,
4
);
PubSubClient
client
(server,
1883
, callback, shimClient, stream);
int
rc = client.
connect
((
char
*)
"
client_test1
"
);
IS_TRUE
(rc);
int
length =
MQTT_MAX_PACKET_SIZE
+
1
;
byte publish[] = {
0x30
,length-
2
,
0x0
,
0x5
,
0x74
,
0x6f
,
0x70
,
0x69
,
0x63
,
0x70
,
0x61
,
0x79
,
0x6c
,
0x6f
,
0x61
,
0x64
};
byte bigPublish[length];
memset
(bigPublish,
'
A
'
,length);
bigPublish[length] =
'
B
'
;
memcpy
(bigPublish,publish,
16
);
shimClient.
respond
(bigPublish,length);
stream.
expect
(bigPublish+
9
,length-
9
);
rc = client.
loop
();
IS_TRUE
(rc);
IS_TRUE
(callback_called);
IS_TRUE
(
strcmp
(lastTopic,
"
topic
"
)==
0
);
IS_TRUE
(lastLength == length-
9
);
IS_FALSE
(stream.
error
());
IS_FALSE
(shimClient.
error
());
END_IT
}
int
test_receive_qos1
() {
IT
(
"
receives a qos1 message
"
);
reset_callback
();
ShimClient shimClient;
shimClient.
setAllowConnect
(
true
);
byte connack[] = {
0x20
,
0x02
,
0x00
,
0x00
};
shimClient.
respond
(connack,
4
);
PubSubClient
client
(server,
1883
, callback, shimClient);
int
rc = client.
connect
((
char
*)
"
client_test1
"
);
IS_TRUE
(rc);
byte publish[] = {
0x32
,
0x10
,
0x0
,
0x5
,
0x74
,
0x6f
,
0x70
,
0x69
,
0x63
,
0x12
,
0x34
,
0x70
,
0x61
,
0x79
,
0x6c
,
0x6f
,
0x61
,
0x64
};
shimClient.
respond
(publish,
18
);
byte puback[] = {
0x40
,
0x2
,
0x12
,
0x34
};
shimClient.
expect
(puback,
4
);
rc = client.
loop
();
IS_TRUE
(rc);
IS_TRUE
(callback_called);
IS_TRUE
(
strcmp
(lastTopic,
"
topic
"
)==
0
);
IS_TRUE
(
memcmp
(lastPayload,
"
payload
"
,
7
)==
0
);
IS_TRUE
(lastLength ==
7
);
IS_FALSE
(shimClient.
error
());
END_IT
}
int
main
()
{
SUITE
(
"
Receive
"
);
test_receive_callback
();
test_receive_stream
();
test_receive_max_sized_message
();
test_drop_invalid_remaining_length_message
();
test_receive_oversized_message
();
test_receive_oversized_stream_message
();
test_receive_qos1
();
FINISH
}
Back
|
FazBrowse Home
|
New Git URL