Debian Codegraph @

crt/aws-c-mqtt/tests/v5/mqtt5_testing_utils.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include "mqtt5_testing_utils.h"
7
8 #include <aws/common/clock.h>
9 #include <aws/io/channel_bootstrap.h>
10 #include <aws/io/event_loop.h>
11 #include <aws/io/host_resolver.h>
12 #include <aws/mqtt/private/v5/mqtt5_decoder.h>
13 #include <aws/mqtt/private/v5/mqtt5_encoder.h>
14 #include <aws/mqtt/private/v5/mqtt5_utils.h>
15 #include <aws/mqtt/v5/mqtt5_client.h>
16
17 #include <aws/testing/aws_test_harness.h>
18
19 #include <inttypes.h>
20
21 int aws_mqtt5_test_verify_user_properties_raw(
22 size_t property_count,
23 const struct aws_mqtt5_user_property *properties,
24 size_t expected_count,
25 const struct aws_mqtt5_user_property *expected_properties) {
26
27 ASSERT_UINT_EQUALS(expected_count, property_count);
28
29 for (size_t i = 0; i < expected_count; ++i) {
30 const struct aws_mqtt5_user_property *expected_property = &expected_properties[i];
31 struct aws_byte_cursor expected_name = expected_property->name;
32 struct aws_byte_cursor expected_value = expected_property->value;
33
34 bool found = false;
35 for (size_t j = 0; j < property_count; ++j) {
36 const struct aws_mqtt5_user_property *nv_pair = &properties[j];
37
38 if (aws_byte_cursor_compare_lexical(&expected_name, &nv_pair->name) == 0 &&
39 aws_byte_cursor_compare_lexical(&expected_value, &nv_pair->value) == 0) {
40 found = true;
41 break;
42 }
43 }
44
45 if (!found) {
46 return AWS_OP_ERR;
47 }
48 }
49
50 return AWS_OP_SUCCESS;
51 }
52
53 static int s_compute_connack_variable_length_fields(
54 const struct aws_mqtt5_packet_connack_view *connack_view,
55 uint32_t *total_remaining_length,
56 uint32_t *property_length) {
57
58 size_t local_property_length =
59 aws_mqtt5_compute_user_property_encode_length(connack_view->user_properties, connack_view->user_property_count);
60
61 ADD_OPTIONAL_U32_PROPERTY_LENGTH(connack_view->session_expiry_interval, local_property_length);
62 ADD_OPTIONAL_U16_PROPERTY_LENGTH(connack_view->receive_maximum, local_property_length);
63 ADD_OPTIONAL_U8_PROPERTY_LENGTH(connack_view->maximum_qos, local_property_length);
64 ADD_OPTIONAL_U8_PROPERTY_LENGTH(connack_view->retain_available, local_property_length);
65 ADD_OPTIONAL_U32_PROPERTY_LENGTH(connack_view->maximum_packet_size, local_property_length);
66 ADD_OPTIONAL_CURSOR_PROPERTY_LENGTH(connack_view->assigned_client_identifier, local_property_length);
67 ADD_OPTIONAL_U16_PROPERTY_LENGTH(connack_view->topic_alias_maximum, local_property_length);
68 ADD_OPTIONAL_CURSOR_PROPERTY_LENGTH(connack_view->reason_string, local_property_length);
69 ADD_OPTIONAL_U8_PROPERTY_LENGTH(connack_view->wildcard_subscriptions_available, local_property_length);
70 ADD_OPTIONAL_U8_PROPERTY_LENGTH(connack_view->subscription_identifiers_available, local_property_length);
71 ADD_OPTIONAL_U8_PROPERTY_LENGTH(connack_view->shared_subscriptions_available, local_property_length);
72 ADD_OPTIONAL_U16_PROPERTY_LENGTH(connack_view->server_keep_alive, local_property_length);
73 ADD_OPTIONAL_CURSOR_PROPERTY_LENGTH(connack_view->response_information, local_property_length);
74 ADD_OPTIONAL_CURSOR_PROPERTY_LENGTH(connack_view->server_reference, local_property_length);
75 ADD_OPTIONAL_CURSOR_PROPERTY_LENGTH(connack_view->authentication_method, local_property_length);
76 ADD_OPTIONAL_CURSOR_PROPERTY_LENGTH(connack_view->authentication_data, local_property_length);
77
78 *property_length = (uint32_t)local_property_length;
79
80 size_t property_length_encoding_length = 0;
81 if (aws_mqtt5_get_variable_length_encode_size(local_property_length, &property_length_encoding_length)) {
82 return AWS_OP_ERR;
83 }
84
85 /* reason code (1 byte) + flags (1 byte) */
86 *total_remaining_length = *property_length + (uint32_t)property_length_encoding_length + 2;
87
88 return AWS_OP_SUCCESS;
89 }
90
91 int aws_mqtt5_encoder_begin_connack(struct aws_mqtt5_encoder *encoder, const void *packet_view) {
92
93 const struct aws_mqtt5_packet_connack_view *connack_view = packet_view;
94
95 uint32_t total_remaining_length = 0;
96 uint32_t property_length = 0;
97 if (s_compute_connack_variable_length_fields(connack_view, &total_remaining_length, &property_length)) {
98 int error_code = aws_last_error();
99 AWS_LOGF_ERROR(
100 AWS_LS_MQTT5_GENERAL,
101 "(%p) mqtt5 client encoder - failed to compute variable length values for CONNACK packet with error "
102 "%d(%s)",
103 (void *)encoder->config.client,
104 error_code,
105 aws_error_debug_str(error_code));
106 return AWS_OP_ERR;
107 }
108
109 AWS_LOGF_DEBUG(
110 AWS_LS_MQTT5_GENERAL,
111 "(%p) mqtt5 client encoder - setting up encode for a CONNACK packet with remaining length %" PRIu32,
112 (void *)encoder->config.client,
113 total_remaining_length);
114
115 uint8_t flags = connack_view->session_present ? 1 : 0;
116
117 ADD_ENCODE_STEP_U8(encoder, aws_mqtt5_compute_fixed_header_byte1(AWS_MQTT5_PT_CONNACK, 0));
118 ADD_ENCODE_STEP_VLI(encoder, total_remaining_length);
119 ADD_ENCODE_STEP_U8(encoder, flags);
120 ADD_ENCODE_STEP_U8(encoder, (uint8_t)connack_view->reason_code);
121 ADD_ENCODE_STEP_VLI(encoder, property_length);
122
123 if (property_length > 0) {
124 ADD_ENCODE_STEP_OPTIONAL_U32_PROPERTY(
125 encoder, AWS_MQTT5_PROPERTY_TYPE_SESSION_EXPIRY_INTERVAL, connack_view->session_expiry_interval);
126 ADD_ENCODE_STEP_OPTIONAL_U16_PROPERTY(
127 encoder, AWS_MQTT5_PROPERTY_TYPE_RECEIVE_MAXIMUM, connack_view->receive_maximum);
128 ADD_ENCODE_STEP_OPTIONAL_U8_PROPERTY(encoder, AWS_MQTT5_PROPERTY_TYPE_MAXIMUM_QOS, connack_view->maximum_qos);
129 ADD_ENCODE_STEP_OPTIONAL_U8_PROPERTY(
130 encoder, AWS_MQTT5_PROPERTY_TYPE_RETAIN_AVAILABLE, connack_view->retain_available);
131 ADD_ENCODE_STEP_OPTIONAL_U32_PROPERTY(
132 encoder, AWS_MQTT5_PROPERTY_TYPE_MAXIMUM_PACKET_SIZE, connack_view->maximum_packet_size);
133 ADD_ENCODE_STEP_OPTIONAL_CURSOR_PROPERTY(
134 encoder, AWS_MQTT5_PROPERTY_TYPE_ASSIGNED_CLIENT_IDENTIFIER, connack_view->assigned_client_identifier);
135 ADD_ENCODE_STEP_OPTIONAL_U16_PROPERTY(
136 encoder, AWS_MQTT5_PROPERTY_TYPE_TOPIC_ALIAS_MAXIMUM, connack_view->topic_alias_maximum);
137 ADD_ENCODE_STEP_OPTIONAL_CURSOR_PROPERTY(
138 encoder, AWS_MQTT5_PROPERTY_TYPE_REASON_STRING, connack_view->reason_string);
139 ADD_ENCODE_STEP_OPTIONAL_U8_PROPERTY(
140 encoder,
141 AWS_MQTT5_PROPERTY_TYPE_WILDCARD_SUBSCRIPTIONS_AVAILABLE,
142 connack_view->wildcard_subscriptions_available);
143 ADD_ENCODE_STEP_OPTIONAL_U8_PROPERTY(
144 encoder,
145 AWS_MQTT5_PROPERTY_TYPE_SUBSCRIPTION_IDENTIFIERS_AVAILABLE,
146 connack_view->subscription_identifiers_available);
147 ADD_ENCODE_STEP_OPTIONAL_U8_PROPERTY(
148 encoder,
149 AWS_MQTT5_PROPERTY_TYPE_SHARED_SUBSCRIPTIONS_AVAILABLE,
150 connack_view->shared_subscriptions_available);
151 ADD_ENCODE_STEP_OPTIONAL_U16_PROPERTY(
152 encoder, AWS_MQTT5_PROPERTY_TYPE_SERVER_KEEP_ALIVE, connack_view->server_keep_alive);
153 ADD_ENCODE_STEP_OPTIONAL_CURSOR_PROPERTY(
154 encoder, AWS_MQTT5_PROPERTY_TYPE_RESPONSE_INFORMATION, connack_view->response_information);
155 ADD_ENCODE_STEP_OPTIONAL_CURSOR_PROPERTY(
156 encoder, AWS_MQTT5_PROPERTY_TYPE_SERVER_REFERENCE, connack_view->server_reference);
157 ADD_ENCODE_STEP_OPTIONAL_CURSOR_PROPERTY(
158 encoder, AWS_MQTT5_PROPERTY_TYPE_AUTHENTICATION_METHOD, connack_view->authentication_method);
159 ADD_ENCODE_STEP_OPTIONAL_CURSOR_PROPERTY(
160 encoder, AWS_MQTT5_PROPERTY_TYPE_AUTHENTICATION_DATA, connack_view->authentication_data);
161
162 aws_mqtt5_add_user_property_encoding_steps(
163 encoder, connack_view->user_properties, connack_view->user_property_count);
164 }
165
166 return AWS_OP_SUCCESS;
167 }
168
169 int aws_mqtt5_encoder_begin_pingresp(struct aws_mqtt5_encoder *encoder, const void *packet_view) {
170 (void)packet_view;
171
172 AWS_LOGF_DEBUG(
173 AWS_LS_MQTT5_GENERAL,
174 "(%p) mqtt5 client encoder - setting up encode for a PINGRESP packet",
175 (void *)encoder->config.client);
176
177 /* A ping response is just a fixed header with a 0-valued remaining length which we encode as a 0 u8 */
178 ADD_ENCODE_STEP_U8(encoder, aws_mqtt5_compute_fixed_header_byte1(AWS_MQTT5_PT_PINGRESP, 0));
179 ADD_ENCODE_STEP_U8(encoder, 0);
180
181 return AWS_OP_SUCCESS;
182 }
183
184 static int s_compute_suback_variable_length_fields(
185 const struct aws_mqtt5_packet_suback_view *suback_view,
186 uint32_t *total_remaining_length,
187 uint32_t *property_length) {
188 /* User Properties length */
189 size_t local_property_length =
190 aws_mqtt5_compute_user_property_encode_length(suback_view->user_properties, suback_view->user_property_count);
191 /* Optional Reason String */
192 ADD_OPTIONAL_CURSOR_PROPERTY_LENGTH(suback_view->reason_string, local_property_length);
193
194 *property_length = (uint32_t)local_property_length;
195
196 size_t local_total_remaining_length = 0;
197 if (aws_mqtt5_get_variable_length_encode_size(local_property_length, &local_total_remaining_length)) {
198 return AWS_OP_ERR;
199 }
200
201 /* Packet Identifier (2 bytes) */
202 local_total_remaining_length += 2;
203
204 /* Reason Codes (1 byte each) */
205 local_total_remaining_length += suback_view->reason_code_count;
206
207 /* Add property length */
208 *total_remaining_length = *property_length + (uint32_t)local_total_remaining_length;
209
210 return AWS_OP_SUCCESS;
211 }
212
213 int aws_mqtt5_encoder_begin_suback(struct aws_mqtt5_encoder *encoder, const void *packet_view) {
214
215 const struct aws_mqtt5_packet_suback_view *suback_view = packet_view;
216
217 uint32_t total_remaining_length = 0;
218 uint32_t property_length = 0;
219 if (s_compute_suback_variable_length_fields(suback_view, &total_remaining_length, &property_length)) {
220 int error_code = aws_last_error();
221 AWS_LOGF_ERROR(
222 AWS_LS_MQTT5_GENERAL,
223 "(%p) mqtt5 client encoder - failed to compute variable length values for SUBACK packet with error "
224 "%d(%s)",
225 (void *)encoder->config.client,
226 error_code,
227 aws_error_debug_str(error_code));
228 return AWS_OP_ERR;
229 }
230
231 AWS_LOGF_DEBUG(
232 AWS_LS_MQTT5_GENERAL,
233 "(%p) mqtt5 client encoder - setting up encode for a SUBACK packet with remaining length %" PRIu32,
234 (void *)encoder->config.client,
235 total_remaining_length);
236
237 ADD_ENCODE_STEP_U8(encoder, aws_mqtt5_compute_fixed_header_byte1(AWS_MQTT5_PT_SUBACK, 0));
238 ADD_ENCODE_STEP_VLI(encoder, total_remaining_length);
239 ADD_ENCODE_STEP_U16(encoder, suback_view->packet_id);
240 ADD_ENCODE_STEP_VLI(encoder, property_length);
241
242 ADD_ENCODE_STEP_OPTIONAL_CURSOR_PROPERTY(
243 encoder, AWS_MQTT5_PROPERTY_TYPE_REASON_STRING, suback_view->reason_string);
244
245 aws_mqtt5_add_user_property_encoding_steps(encoder, suback_view->user_properties, suback_view->user_property_count);
246
247 for (size_t i = 0; i < suback_view->reason_code_count; ++i) {
248 ADD_ENCODE_STEP_U8(encoder, suback_view->reason_codes[i]);
249 }
250
251 return AWS_OP_SUCCESS;
252 }
253
254 static int s_compute_unsuback_variable_length_fields(
255 const struct aws_mqtt5_packet_unsuback_view *unsuback_view,
256 uint32_t *total_remaining_length,
257 uint32_t *property_length) {
258 /* User Properties length */
259 size_t local_property_length = aws_mqtt5_compute_user_property_encode_length(
260 unsuback_view->user_properties, unsuback_view->user_property_count);
261 /* Optional Reason String */
262 ADD_OPTIONAL_CURSOR_PROPERTY_LENGTH(unsuback_view->reason_string, local_property_length);
263
264 *property_length = (uint32_t)local_property_length;
265
266 size_t local_total_remaining_length = 0;
267 if (aws_mqtt5_get_variable_length_encode_size(local_property_length, &local_total_remaining_length)) {
268 return AWS_OP_ERR;
269 }
270
271 /* Packet Identifier (2 bytes) */
272 local_total_remaining_length += 2;
273
274 /* Reason Codes (1 byte each) */
275 local_total_remaining_length += unsuback_view->reason_code_count;
276
277 /* Add property length */
278 *total_remaining_length = *property_length + (uint32_t)local_total_remaining_length;
279
280 return AWS_OP_SUCCESS;
281 }
282
283 int aws_mqtt5_encoder_begin_unsuback(struct aws_mqtt5_encoder *encoder, const void *packet_view) {
284
285 const struct aws_mqtt5_packet_unsuback_view *unsuback_view = packet_view;
286
287 uint32_t total_remaining_length = 0;
288 uint32_t property_length = 0;
289 if (s_compute_unsuback_variable_length_fields(unsuback_view, &total_remaining_length, &property_length)) {
290 int error_code = aws_last_error();
291 AWS_LOGF_ERROR(
292 AWS_LS_MQTT5_GENERAL,
293 "(%p) mqtt5 client encoder - failed to compute variable length values for UNSUBACK packet with error "
294 "%d(%s)",
295 (void *)encoder->config.client,
296 error_code,
297 aws_error_debug_str(error_code));
298 return AWS_OP_ERR;
299 }
300
301 AWS_LOGF_DEBUG(
302 AWS_LS_MQTT5_GENERAL,
303 "(%p) mqtt5 client encoder - setting up encode for an UNSUBACK packet with remaining length %" PRIu32,
304 (void *)encoder->config.client,
305 total_remaining_length);
306
307 ADD_ENCODE_STEP_U8(encoder, aws_mqtt5_compute_fixed_header_byte1(AWS_MQTT5_PT_UNSUBACK, 0));
308 ADD_ENCODE_STEP_VLI(encoder, total_remaining_length);
309 ADD_ENCODE_STEP_U16(encoder, unsuback_view->packet_id);
310 ADD_ENCODE_STEP_VLI(encoder, property_length);
311
312 ADD_ENCODE_STEP_OPTIONAL_CURSOR_PROPERTY(
313 encoder, AWS_MQTT5_PROPERTY_TYPE_REASON_STRING, unsuback_view->reason_string);
314
315 aws_mqtt5_add_user_property_encoding_steps(
316 encoder, unsuback_view->user_properties, unsuback_view->user_property_count);
317
318 for (size_t i = 0; i < unsuback_view->reason_code_count; ++i) {
319 ADD_ENCODE_STEP_U8(encoder, unsuback_view->reason_codes[i]);
320 }
321
322 return AWS_OP_SUCCESS;
323 }
324
325 static int s_compute_puback_variable_length_fields(
326 const struct aws_mqtt5_packet_puback_view *puback_view,
327 uint32_t *total_remaining_length,
328 uint32_t *property_length) {
329 /* User Properties length */
330 size_t local_property_length =
331 aws_mqtt5_compute_user_property_encode_length(puback_view->user_properties, puback_view->user_property_count);
332 /* Optional Reason String */
333 ADD_OPTIONAL_CURSOR_PROPERTY_LENGTH(puback_view->reason_string, local_property_length);
334
335 *property_length = (uint32_t)local_property_length;
336
337 size_t local_total_remaining_length = 0;
338 if (aws_mqtt5_get_variable_length_encode_size(local_property_length, &local_total_remaining_length)) {
339 return AWS_OP_ERR;
340 }
341
342 /* Packet Identifier (2 bytes) */
343 local_total_remaining_length += 2;
344
345 /* Reason Code */
346 local_total_remaining_length += 1;
347
348 /* Add property length */
349 *total_remaining_length = *property_length + (uint32_t)local_total_remaining_length;
350
351 return AWS_OP_SUCCESS;
352 }
353
354 int aws_mqtt5_encoder_begin_puback(struct aws_mqtt5_encoder *encoder, const void *packet_view) {
355
356 const struct aws_mqtt5_packet_puback_view *puback_view = packet_view;
357
358 uint32_t total_remaining_length = 0;
359 uint32_t property_length = 0;
360 if (s_compute_puback_variable_length_fields(puback_view, &total_remaining_length, &property_length)) {
361 int error_code = aws_last_error();
362 AWS_LOGF_ERROR(
363 AWS_LS_MQTT5_GENERAL,
364 "(%p) mqtt5 client encoder - failed to compute variable length values for PUBACK packet with error "
365 "%d(%s)",
366 (void *)encoder->config.client,
367 error_code,
368 aws_error_debug_str(error_code));
369 return AWS_OP_ERR;
370 }
371
372 AWS_LOGF_DEBUG(
373 AWS_LS_MQTT5_GENERAL,
374 "(%p) mqtt5 client encoder - setting up encode for an PUBACK packet with remaining length %" PRIu32,
375 (void *)encoder->config.client,
376 total_remaining_length);
377
378 ADD_ENCODE_STEP_U8(encoder, aws_mqtt5_compute_fixed_header_byte1(AWS_MQTT5_PT_PUBACK, 0));
379 ADD_ENCODE_STEP_VLI(encoder, total_remaining_length);
380 ADD_ENCODE_STEP_U16(encoder, puback_view->packet_id);
381 ADD_ENCODE_STEP_U8(encoder, puback_view->reason_code);
382 ADD_ENCODE_STEP_VLI(encoder, property_length);
383 ADD_ENCODE_STEP_OPTIONAL_CURSOR_PROPERTY(
384 encoder, AWS_MQTT5_PROPERTY_TYPE_REASON_STRING, puback_view->reason_string);
385 aws_mqtt5_add_user_property_encoding_steps(encoder, puback_view->user_properties, puback_view->user_property_count);
386
387 return AWS_OP_SUCCESS;
388 }
389
390 void aws_mqtt5_encode_init_testing_function_table(struct aws_mqtt5_encoder_function_table *function_table) {
391 *function_table = *g_aws_mqtt5_encoder_default_function_table;
392 function_table->encoders_by_packet_type[AWS_MQTT5_PT_PINGRESP] = &aws_mqtt5_encoder_begin_pingresp;
393 function_table->encoders_by_packet_type[AWS_MQTT5_PT_CONNACK] = &aws_mqtt5_encoder_begin_connack;
394 function_table->encoders_by_packet_type[AWS_MQTT5_PT_SUBACK] = &aws_mqtt5_encoder_begin_suback;
395 function_table->encoders_by_packet_type[AWS_MQTT5_PT_UNSUBACK] = &aws_mqtt5_encoder_begin_unsuback;
396 function_table->encoders_by_packet_type[AWS_MQTT5_PT_PUBACK] = &aws_mqtt5_encoder_begin_puback;
397 }
398
399 static int s_aws_mqtt5_decoder_decode_pingreq(struct aws_mqtt5_decoder *decoder) {
400 if (decoder->packet_cursor.len != 0) {
401 goto error;
402 }
403
404 uint8_t expected_first_byte = aws_mqtt5_compute_fixed_header_byte1(AWS_MQTT5_PT_PINGREQ, 0);
405 if (decoder->packet_first_byte != expected_first_byte || decoder->remaining_length != 0) {
406 goto error;
407 }
408
409 if (decoder->options.on_packet_received != NULL) {
410 (*decoder->options.on_packet_received)(AWS_MQTT5_PT_PINGREQ, NULL, decoder->options.callback_user_data);
411 }
412
413 return AWS_OP_SUCCESS;
414
415 error:
416
417 AWS_LOGF_ERROR(
418 AWS_LS_MQTT5_GENERAL, "(%p) aws_mqtt5_decoder - PINGREQ decode failure", decoder->options.callback_user_data);
419 return aws_raise_error(AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR);
420 }
421
422 /* decode function for all CONNECT properties. Movable to test-only code if we switched to a decoding function table */
423 static int s_read_connect_property(
424 struct aws_mqtt5_packet_connect_storage *storage,
425 struct aws_byte_cursor *packet_cursor) {
426 int result = AWS_OP_ERR;
427
428 uint8_t property_type = 0;
429 AWS_MQTT5_DECODE_U8(packet_cursor, &property_type, done);
430
431 struct aws_mqtt5_packet_connect_view *storage_view = &storage->storage_view;
432
433 switch (property_type) {
434 case AWS_MQTT5_PROPERTY_TYPE_SESSION_EXPIRY_INTERVAL:
435 AWS_MQTT5_DECODE_U32_OPTIONAL(
436 packet_cursor,
437 &storage->session_expiry_interval_seconds,
438 &storage_view->session_expiry_interval_seconds,
439 done);
440 break;
441
442 case AWS_MQTT5_PROPERTY_TYPE_RECEIVE_MAXIMUM:
443 AWS_MQTT5_DECODE_U16_OPTIONAL(
444 packet_cursor, &storage->receive_maximum, &storage_view->receive_maximum, done);
445 break;
446
447 case AWS_MQTT5_PROPERTY_TYPE_MAXIMUM_PACKET_SIZE:
448 AWS_MQTT5_DECODE_U32_OPTIONAL(
449 packet_cursor, &storage->maximum_packet_size_bytes, &storage_view->maximum_packet_size_bytes, done);
450 break;
451
452 case AWS_MQTT5_PROPERTY_TYPE_TOPIC_ALIAS_MAXIMUM:
453 AWS_MQTT5_DECODE_U16_OPTIONAL(
454 packet_cursor, &storage->topic_alias_maximum, &storage_view->topic_alias_maximum, done);
455 break;
456
457 case AWS_MQTT5_PROPERTY_TYPE_REQUEST_RESPONSE_INFORMATION:
458 AWS_MQTT5_DECODE_U8_OPTIONAL(
459 packet_cursor,
460 &storage->request_response_information,
461 &storage_view->request_response_information,
462 done);
463 break;
464
465 case AWS_MQTT5_PROPERTY_TYPE_REQUEST_PROBLEM_INFORMATION:
466 AWS_MQTT5_DECODE_U8_OPTIONAL(
467 packet_cursor, &storage->request_problem_information, &storage_view->request_problem_information, done);
468 break;
469
470 case AWS_MQTT5_PROPERTY_TYPE_AUTHENTICATION_METHOD:
471 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR_OPTIONAL(
472 packet_cursor, &storage->authentication_method, &storage_view->authentication_method, done);
473 break;
474
475 case AWS_MQTT5_PROPERTY_TYPE_AUTHENTICATION_DATA:
476 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR_OPTIONAL(
477 packet_cursor, &storage->authentication_data, &storage_view->authentication_data, done);
478 break;
479
480 case AWS_MQTT5_PROPERTY_TYPE_USER_PROPERTY:
481 if (aws_mqtt5_decode_user_property(packet_cursor, &storage->user_properties)) {
482 goto done;
483 }
484 break;
485
486 default:
487 goto done;
488 }
489
490 result = AWS_OP_SUCCESS;
491
492 done:
493
494 if (result != AWS_OP_SUCCESS) {
495 aws_raise_error(AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR);
496 }
497
498 return result;
499 }
500
501 /* decode function for all will properties. Movable to test-only code if we switched to a decoding function table */
502 static int s_read_will_property(
503 struct aws_mqtt5_packet_connect_storage *connect_storage,
504 struct aws_mqtt5_packet_publish_storage *will_storage,
505 struct aws_byte_cursor *packet_cursor) {
506 int result = AWS_OP_ERR;
507
508 uint8_t property_type = 0;
509 AWS_MQTT5_DECODE_U8(packet_cursor, &property_type, done);
510
511 struct aws_mqtt5_packet_connect_view *connect_storage_view = &connect_storage->storage_view;
512 struct aws_mqtt5_packet_publish_view *will_storage_view = &will_storage->storage_view;
513
514 switch (property_type) {
515 case AWS_MQTT5_PROPERTY_TYPE_WILL_DELAY_INTERVAL:
516 AWS_MQTT5_DECODE_U32_OPTIONAL(
517 packet_cursor,
518 &connect_storage->will_delay_interval_seconds,
519 &connect_storage_view->will_delay_interval_seconds,
520 done);
521 break;
522
523 case AWS_MQTT5_PROPERTY_TYPE_PAYLOAD_FORMAT_INDICATOR:
524 AWS_MQTT5_DECODE_U8_OPTIONAL(
525 packet_cursor, &will_storage->payload_format, &will_storage_view->payload_format, done);
526 break;
527
528 case AWS_MQTT5_PROPERTY_TYPE_MESSAGE_EXPIRY_INTERVAL:
529 AWS_MQTT5_DECODE_U32_OPTIONAL(
530 packet_cursor,
531 &will_storage->message_expiry_interval_seconds,
532 &will_storage_view->message_expiry_interval_seconds,
533 done);
534 break;
535
536 case AWS_MQTT5_PROPERTY_TYPE_CONTENT_TYPE:
537 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR_OPTIONAL(
538 packet_cursor, &will_storage->content_type, &will_storage_view->content_type, done);
539 break;
540
541 case AWS_MQTT5_PROPERTY_TYPE_RESPONSE_TOPIC:
542 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR_OPTIONAL(
543 packet_cursor, &will_storage->response_topic, &will_storage_view->response_topic, done);
544 break;
545
546 case AWS_MQTT5_PROPERTY_TYPE_CORRELATION_DATA:
547 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR_OPTIONAL(
548 packet_cursor, &will_storage->correlation_data, &will_storage_view->correlation_data, done);
549 break;
550
551 case AWS_MQTT5_PROPERTY_TYPE_USER_PROPERTY:
552 if (aws_mqtt5_decode_user_property(packet_cursor, &will_storage->user_properties)) {
553 goto done;
554 }
555 break;
556
557 default:
558 goto done;
559 }
560
561 result = AWS_OP_SUCCESS;
562
563 done:
564
565 if (result != AWS_OP_SUCCESS) {
566 aws_raise_error(AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR);
567 }
568
569 return result;
570 }
571
572 /*
573 * Decodes a CONNECT packet whose data must be in the scratch buffer.
574 * Could be moved to test-only if we used a function table for per-packet-type decoding.
575 */
576 static int s_aws_mqtt5_decoder_decode_connect(struct aws_mqtt5_decoder *decoder) {
577 struct aws_mqtt5_packet_connect_storage connect_storage;
578 struct aws_mqtt5_packet_publish_storage publish_storage;
579 int result = AWS_OP_ERR;
580 bool has_will = false;
581
582 if (aws_mqtt5_packet_connect_storage_init_from_external_storage(&connect_storage, decoder->allocator)) {
583 return AWS_OP_ERR;
584 }
585
586 if (aws_mqtt5_packet_publish_storage_init_from_external_storage(&publish_storage, decoder->allocator)) {
587 goto done;
588 }
589
590 uint8_t first_byte = decoder->packet_first_byte;
591 /* CONNECT flags must be zero by protocol */
592 if ((first_byte & 0x0F) != 0) {
593 goto done;
594 }
595
596 struct aws_byte_cursor packet_cursor = decoder->packet_cursor;
597 if (decoder->remaining_length != (uint32_t)packet_cursor.len) {
598 goto done;
599 }
600
601 struct aws_byte_cursor protocol_cursor = aws_byte_cursor_advance(&packet_cursor, 7);
602 if (!aws_byte_cursor_eq(&protocol_cursor, &g_aws_mqtt5_connect_protocol_cursor)) {
603 goto done;
604 }
605
606 uint8_t connect_flags = 0;
607 AWS_MQTT5_DECODE_U8(&packet_cursor, &connect_flags, done);
608
609 struct aws_mqtt5_packet_connect_view *connect_storage_view = &connect_storage.storage_view;
610 struct aws_mqtt5_packet_publish_view *will_storage_view = &publish_storage.storage_view;
611
612 connect_storage_view->clean_start = (connect_flags & AWS_MQTT5_CONNECT_FLAGS_CLEAN_START_BIT) != 0;
613
614 AWS_MQTT5_DECODE_U16(&packet_cursor, &connect_storage_view->keep_alive_interval_seconds, done);
615
616 uint32_t connect_property_length = 0;
617 AWS_MQTT5_DECODE_VLI(&packet_cursor, &connect_property_length, done);
618 if (connect_property_length > packet_cursor.len) {
619 goto done;
620 }
621
622 struct aws_byte_cursor connect_property_cursor = aws_byte_cursor_advance(&packet_cursor, connect_property_length);
623 while (connect_property_cursor.len > 0) {
624 if (s_read_connect_property(&connect_storage, &connect_property_cursor)) {
625 goto done;
626 }
627 }
628
629 connect_storage_view->user_property_count = aws_mqtt5_user_property_set_size(&connect_storage.user_properties);
630 connect_storage_view->user_properties = connect_storage.user_properties.properties.data;
631
632 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR(&packet_cursor, &connect_storage_view->client_id, done);
633
634 has_will = (connect_flags & AWS_MQTT5_CONNECT_FLAGS_WILL_BIT) != 0;
635 if (has_will) {
636 uint32_t will_property_length = 0;
637 AWS_MQTT5_DECODE_VLI(&packet_cursor, &will_property_length, done);
638 if (will_property_length > packet_cursor.len) {
639 goto done;
640 }
641
642 struct aws_byte_cursor will_property_cursor = aws_byte_cursor_advance(&packet_cursor, will_property_length);
643 while (will_property_cursor.len > 0) {
644 if (s_read_will_property(&connect_storage, &publish_storage, &will_property_cursor)) {
645 goto done;
646 }
647 }
648
649 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR(&packet_cursor, &will_storage_view->topic, done);
650 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR(&packet_cursor, &will_storage_view->payload, done);
651
652 /* apply will flags from the connect flags to the will's storage */
653 will_storage_view->qos = (enum aws_mqtt5_qos)(
654 (connect_flags >> AWS_MQTT5_CONNECT_FLAGS_WILL_QOS_BIT_POSITION) &
655 AWS_MQTT5_CONNECT_FLAGS_WILL_QOS_BIT_MASK);
656 will_storage_view->retain = (connect_flags & AWS_MQTT5_CONNECT_FLAGS_WILL_RETAIN_BIT) != 0;
657
658 will_storage_view->user_property_count = aws_mqtt5_user_property_set_size(&publish_storage.user_properties);
659 will_storage_view->user_properties = publish_storage.user_properties.properties.data;
660 }
661
662 if ((connect_flags & AWS_MQTT5_CONNECT_FLAGS_USER_NAME_BIT) != 0) {
663 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR_OPTIONAL(
664 &packet_cursor, &connect_storage.username, &connect_storage_view->username, done);
665 }
666
667 if ((connect_flags & AWS_MQTT5_CONNECT_FLAGS_PASSWORD_BIT) != 0) {
668 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR_OPTIONAL(
669 &packet_cursor, &connect_storage.password, &connect_storage_view->password, done);
670 }
671
672 if (packet_cursor.len == 0) {
673 result = AWS_OP_SUCCESS;
674 }
675
676 done:
677
678 if (result == AWS_OP_SUCCESS) {
679 if (decoder->options.on_packet_received != NULL) {
680 if (has_will) {
681 connect_storage.storage_view.will = &publish_storage.storage_view;
682 }
683
684 result = (*decoder->options.on_packet_received)(
685 AWS_MQTT5_PT_CONNECT, &connect_storage.storage_view, decoder->options.callback_user_data);
686 }
687 } else {
688 AWS_LOGF_ERROR(
689 AWS_LS_MQTT5_GENERAL,
690 "(%p) aws_mqtt5_decoder - CONNECT decode failure",
691 decoder->options.callback_user_data);
692 aws_raise_error(AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR);
693 }
694
695 aws_mqtt5_packet_publish_storage_clean_up(&publish_storage);
696 aws_mqtt5_packet_connect_storage_clean_up(&connect_storage);
697
698 return result;
699 }
700
701 /* decode function for subscribe properties. Movable to test-only code if we switched to a decoding function table */
702 static int s_read_subscribe_property(
703 struct aws_mqtt5_packet_subscribe_storage *subscribe_storage,
704 struct aws_byte_cursor *packet_cursor) {
705 int result = AWS_OP_ERR;
706
707 uint8_t property_type = 0;
708 AWS_MQTT5_DECODE_U8(packet_cursor, &property_type, done);
709
710 struct aws_mqtt5_packet_subscribe_view *storage_view = &subscribe_storage->storage_view;
711
712 switch (property_type) {
713 case AWS_MQTT5_PROPERTY_TYPE_SUBSCRIPTION_IDENTIFIER:
714 AWS_MQTT5_DECODE_VLI(packet_cursor, &subscribe_storage->subscription_identifier, done);
715 storage_view->subscription_identifier = &subscribe_storage->subscription_identifier;
716
717 break;
718 case AWS_MQTT5_PROPERTY_TYPE_USER_PROPERTY:
719 if (aws_mqtt5_decode_user_property(packet_cursor, &subscribe_storage->user_properties)) {
720 goto done;
721 }
722 break;
723
724 default:
725 goto done;
726 }
727
728 result = AWS_OP_SUCCESS;
729
730 done:
731
732 if (result != AWS_OP_SUCCESS) {
733 aws_raise_error(AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR);
734 }
735
736 return result;
737 }
738
739 /*
740 * Decodes a SUBSCRIBE packet whose data must be in the scratch buffer.
741 */
742 static int s_aws_mqtt5_decoder_decode_subscribe(struct aws_mqtt5_decoder *decoder) {
743 struct aws_mqtt5_packet_subscribe_storage subscribe_storage;
744 int result = AWS_OP_ERR;
745
746 if (aws_mqtt5_packet_subscribe_storage_init_from_external_storage(&subscribe_storage, decoder->allocator)) {
747 goto done;
748 }
749
750 struct aws_mqtt5_packet_subscribe_view *storage_view = &subscribe_storage.storage_view;
751
752 /* SUBSCRIBE flags must be 2 by protocol*/
753 uint8_t first_byte = decoder->packet_first_byte;
754 if ((first_byte & 0x0F) != 2) {
755 goto done;
756 }
757
758 struct aws_byte_cursor packet_cursor = decoder->packet_cursor;
759 if (decoder->remaining_length != (uint32_t)packet_cursor.len) {
760 goto done;
761 }
762
763 uint16_t packet_id = 0;
764 AWS_MQTT5_DECODE_U16(&packet_cursor, &packet_id, done);
765 subscribe_storage.storage_view.packet_id = packet_id;
766
767 uint32_t property_length = 0;
768 AWS_MQTT5_DECODE_VLI(&packet_cursor, &property_length, done);
769 if (property_length > packet_cursor.len) {
770 goto done;
771 }
772
773 struct aws_byte_cursor property_cursor = aws_byte_cursor_advance(&packet_cursor, property_length);
774 while (property_cursor.len > 0) {
775 if (s_read_subscribe_property(&subscribe_storage, &property_cursor)) {
776 goto done;
777 }
778 }
779
780 while (packet_cursor.len > 0) {
781 struct aws_mqtt5_subscription_view subscription_view;
782 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR(&packet_cursor, &subscription_view.topic_filter, done);
783
784 uint8_t subscription_options = 0;
785 AWS_MQTT5_DECODE_U8(&packet_cursor, &subscription_options, done);
786
787 subscription_view.no_local = (subscription_options & AWS_MQTT5_SUBSCRIBE_FLAGS_NO_LOCAL) != 0;
788 subscription_view.retain_as_published =
789 (subscription_options & AWS_MQTT5_SUBSCRIBE_FLAGS_RETAIN_AS_PUBLISHED) != 0;
790 subscription_view.qos = (enum aws_mqtt5_qos)(
791 (subscription_options >> AWS_MQTT5_SUBSCRIBE_FLAGS_QOS_BIT_POSITION) &
792 AWS_MQTT5_SUBSCRIBE_FLAGS_QOS_BIT_MASK);
793 subscription_view.retain_handling_type = (enum aws_mqtt5_retain_handling_type)(
794 (subscription_options >> AWS_MQTT5_SUBSCRIBE_FLAGS_RETAIN_HANDLING_TYPE_BIT_POSITION) &
795 AWS_MQTT5_SUBSCRIBE_FLAGS_RETAIN_HANDLING_TYPE_BIT_MASK);
796
797 if (aws_array_list_push_back(&subscribe_storage.subscriptions, &subscription_view)) {
798 goto done;
799 }
800 }
801
802 storage_view->subscription_count = aws_array_list_length(&subscribe_storage.subscriptions);
803 storage_view->subscriptions = subscribe_storage.subscriptions.data;
804 storage_view->user_property_count = aws_mqtt5_user_property_set_size(&subscribe_storage.user_properties);
805 storage_view->user_properties = subscribe_storage.user_properties.properties.data;
806
807 if (packet_cursor.len == 0) {
808 result = AWS_OP_SUCCESS;
809 }
810
811 done:
812
813 if (result == AWS_OP_SUCCESS) {
814 if (decoder->options.on_packet_received != NULL) {
815 result = (*decoder->options.on_packet_received)(
816 AWS_MQTT5_PT_SUBSCRIBE, &subscribe_storage.storage_view, decoder->options.callback_user_data);
817 }
818 } else {
819 AWS_LOGF_ERROR(
820 AWS_LS_MQTT5_GENERAL,
821 "(%p) aws_mqtt5_decoder - SUBSCRIBE decode failure",
822 decoder->options.callback_user_data);
823 aws_raise_error(AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR);
824 }
825
826 aws_mqtt5_packet_subscribe_storage_clean_up(&subscribe_storage);
827
828 return result;
829 }
830
831 /* decode function for unsubscribe properties. Movable to test-only code if we switched to a decoding function table */
832 static int s_read_unsubscribe_property(
833 struct aws_mqtt5_packet_unsubscribe_storage *unsubscribe_storage,
834 struct aws_byte_cursor *packet_cursor) {
835 int result = AWS_OP_ERR;
836
837 uint8_t property_type = 0;
838 AWS_MQTT5_DECODE_U8(packet_cursor, &property_type, done);
839
840 switch (property_type) {
841 case AWS_MQTT5_PROPERTY_TYPE_USER_PROPERTY:
842 if (aws_mqtt5_decode_user_property(packet_cursor, &unsubscribe_storage->user_properties)) {
843 goto done;
844 }
845 break;
846
847 default:
848 goto done;
849 }
850
851 result = AWS_OP_SUCCESS;
852
853 done:
854
855 if (result != AWS_OP_SUCCESS) {
856 aws_raise_error(AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR);
857 }
858
859 return result;
860 }
861
862 /*
863 * Decodes an UNSUBSCRIBE packet whose data must be in the scratch buffer.
864 */
865 static int s_aws_mqtt5_decoder_decode_unsubscribe(struct aws_mqtt5_decoder *decoder) {
866 struct aws_mqtt5_packet_unsubscribe_storage unsubscribe_storage;
867 int result = AWS_OP_ERR;
868
869 if (aws_mqtt5_packet_unsubscribe_storage_init_from_external_storage(&unsubscribe_storage, decoder->allocator)) {
870 goto done;
871 }
872
873 struct aws_mqtt5_packet_unsubscribe_view *storage_view = &unsubscribe_storage.storage_view;
874
875 /* UNSUBSCRIBE flags must be 2 by protocol*/
876 uint8_t first_byte = decoder->packet_first_byte;
877 if ((first_byte & 0x0F) != 2) {
878 goto done;
879 }
880
881 struct aws_byte_cursor packet_cursor = decoder->packet_cursor;
882 if (decoder->remaining_length != (uint32_t)packet_cursor.len) {
883 goto done;
884 }
885
886 uint16_t packet_id = 0;
887 AWS_MQTT5_DECODE_U16(&packet_cursor, &packet_id, done);
888 unsubscribe_storage.storage_view.packet_id = packet_id;
889
890 uint32_t property_length = 0;
891 AWS_MQTT5_DECODE_VLI(&packet_cursor, &property_length, done);
892 if (property_length > packet_cursor.len) {
893 goto done;
894 }
895
896 struct aws_byte_cursor property_cursor = aws_byte_cursor_advance(&packet_cursor, property_length);
897 while (property_cursor.len > 0) {
898 if (s_read_unsubscribe_property(&unsubscribe_storage, &property_cursor)) {
899 goto done;
900 }
901 }
902
903 while (packet_cursor.len > 0) {
904 struct aws_byte_cursor topic;
905 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR(&packet_cursor, &topic, done);
906 if (aws_array_list_push_back(&unsubscribe_storage.topic_filters, &topic)) {
907 goto done;
908 }
909 }
910
911 storage_view->topic_filter_count = aws_array_list_length(&unsubscribe_storage.topic_filters);
912 storage_view->topic_filters = unsubscribe_storage.topic_filters.data;
913 storage_view->user_property_count = aws_mqtt5_user_property_set_size(&unsubscribe_storage.user_properties);
914 storage_view->user_properties = unsubscribe_storage.user_properties.properties.data;
915
916 result = AWS_OP_SUCCESS;
917
918 done:
919
920 if (result == AWS_OP_SUCCESS) {
921 if (decoder->options.on_packet_received != NULL) {
922 result = (*decoder->options.on_packet_received)(
923 AWS_MQTT5_PT_UNSUBSCRIBE, &unsubscribe_storage.storage_view, decoder->options.callback_user_data);
924 }
925 } else {
926 AWS_LOGF_ERROR(
927 AWS_LS_MQTT5_GENERAL,
928 "(%p) aws_mqtt5_decoder - UNSUBSCRIBE decode failure",
929 decoder->options.callback_user_data);
930 aws_raise_error(AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR);
931 }
932
933 aws_mqtt5_packet_unsubscribe_storage_clean_up(&unsubscribe_storage);
934
935 return result;
936 }
937
938 void aws_mqtt5_decode_init_testing_function_table(struct aws_mqtt5_decoder_function_table *function_table) {
939 *function_table = *g_aws_mqtt5_default_decoder_table;
940 function_table->decoders_by_packet_type[AWS_MQTT5_PT_PINGREQ] = &s_aws_mqtt5_decoder_decode_pingreq;
941 function_table->decoders_by_packet_type[AWS_MQTT5_PT_CONNECT] = &s_aws_mqtt5_decoder_decode_connect;
942 function_table->decoders_by_packet_type[AWS_MQTT5_PT_SUBSCRIBE] = &s_aws_mqtt5_decoder_decode_subscribe;
943 function_table->decoders_by_packet_type[AWS_MQTT5_PT_UNSUBSCRIBE] = &s_aws_mqtt5_decoder_decode_unsubscribe;
944 }
945
946 static int s_aws_mqtt5_mock_test_fixture_on_packet_received_fn(
947 enum aws_mqtt5_packet_type type,
948 void *packet_view,
949 void *decoder_callback_user_data) {
950
951 struct aws_mqtt5_mock_server_packet_record packet_record;
952 AWS_ZERO_STRUCT(packet_record);
953
954 struct aws_mqtt5_server_mock_connection_context *server_connection = decoder_callback_user_data;
955 struct aws_mqtt5_client_mock_test_fixture *test_fixture = server_connection->test_fixture;
956
957 switch (type) {
958 case AWS_MQTT5_PT_CONNECT:
959 packet_record.packet_storage =
960 aws_mem_calloc(test_fixture->allocator, 1, sizeof(struct aws_mqtt5_packet_connect_storage));
961 aws_mqtt5_packet_connect_storage_init(
962 packet_record.packet_storage, test_fixture->allocator, packet_view, NULL);
963 break;
964
965 case AWS_MQTT5_PT_DISCONNECT:
966 packet_record.packet_storage =
967 aws_mem_calloc(test_fixture->allocator, 1, sizeof(struct aws_mqtt5_packet_disconnect_storage));
968 aws_mqtt5_packet_disconnect_storage_init(
969 packet_record.packet_storage, test_fixture->allocator, packet_view);
970 break;
971
972 case AWS_MQTT5_PT_SUBSCRIBE:
973 packet_record.packet_storage =
974 aws_mem_calloc(test_fixture->allocator, 1, sizeof(struct aws_mqtt5_packet_subscribe_storage));
975 aws_mqtt5_packet_subscribe_storage_init(packet_record.packet_storage, test_fixture->allocator, packet_view);
976 break;
977
978 case AWS_MQTT5_PT_UNSUBSCRIBE:
979 packet_record.packet_storage =
980 aws_mem_calloc(test_fixture->allocator, 1, sizeof(struct aws_mqtt5_packet_unsubscribe_storage));
981 aws_mqtt5_packet_unsubscribe_storage_init(
982 packet_record.packet_storage, test_fixture->allocator, packet_view);
983 break;
984
985 case AWS_MQTT5_PT_PUBLISH:
986 packet_record.packet_storage =
987 aws_mem_calloc(test_fixture->allocator, 1, sizeof(struct aws_mqtt5_packet_publish_storage));
988 aws_mqtt5_packet_publish_storage_init(packet_record.packet_storage, test_fixture->allocator, packet_view);
989 break;
990
991 case AWS_MQTT5_PT_PUBACK:
992 packet_record.packet_storage =
993 aws_mem_calloc(test_fixture->allocator, 1, sizeof(struct aws_mqtt5_packet_puback_storage));
994 aws_mqtt5_packet_puback_storage_init(packet_record.packet_storage, test_fixture->allocator, packet_view);
995 break;
996
997 default:
998 break;
999 }
1000
1001 packet_record.storage_allocator = test_fixture->allocator;
1002 packet_record.timestamp = (*test_fixture->client_vtable.get_current_time_fn)();
1003 packet_record.packet_type = type;
1004
1005 aws_mutex_lock(&test_fixture->lock);
1006 AWS_LOGF_DEBUG(
1007 AWS_LS_MQTT5_GENERAL, "mqtt5 mock server received packet of type %s", aws_mqtt5_packet_type_to_c_string(type));
1008 aws_array_list_push_back(&test_fixture->server_received_packets, &packet_record);
1009 aws_mutex_unlock(&test_fixture->lock);
1010 aws_condition_variable_notify_all(&test_fixture->signal);
1011
1012 int result = AWS_OP_SUCCESS;
1013 aws_mqtt5_on_mock_server_packet_received_fn *packet_handler =
1014 test_fixture->server_function_table->packet_handlers[type];
1015 if (packet_handler != NULL) {
1016 result =
1017 (*packet_handler)(packet_view, server_connection, server_connection->test_fixture->mock_server_user_data);
1018 }
1019
1020 return result;
1021 }
1022
1023 static int s_process_read_message(
1024 struct aws_channel_handler *handler,
1025 struct aws_channel_slot *slot,
1026 struct aws_io_message *message) {
1027
1028 struct aws_mqtt5_server_mock_connection_context *server_connection = handler->impl;
1029
1030 if (message->message_type != AWS_IO_MESSAGE_APPLICATION_DATA) {
1031 return AWS_OP_ERR;
1032 }
1033
1034 struct aws_byte_cursor message_cursor = aws_byte_cursor_from_buf(&message->message_data);
1035
1036 int result = aws_mqtt5_decoder_on_data_received(&server_connection->decoder, message_cursor);
1037 if (result != AWS_OP_SUCCESS) {
1038 aws_channel_shutdown(server_connection->channel, aws_last_error());
1039 goto done;
1040 }
1041
1042 aws_channel_slot_increment_read_window(slot, message->message_data.len);
1043
1044 done:
1045
1046 aws_mem_release(message->allocator, message);
1047
1048 return AWS_OP_SUCCESS;
1049 }
1050
1051 static int s_shutdown(
1052 struct aws_channel_handler *handler,
1053 struct aws_channel_slot *slot,
1054 enum aws_channel_direction dir,
1055 int error_code,
1056 bool free_scarce_resources_immediately) {
1057
1058 (void)handler;
1059
1060 return aws_channel_slot_on_handler_shutdown_complete(slot, dir, error_code, free_scarce_resources_immediately);
1061 }
1062
1063 static size_t s_initial_window_size(struct aws_channel_handler *handler) {
1064 (void)handler;
1065
1066 return SIZE_MAX;
1067 }
1068
1069 static void s_destroy(struct aws_channel_handler *handler) {
1070 struct aws_mqtt5_server_mock_connection_context *server_connection = handler->impl;
1071
1072 aws_event_loop_cancel_task(
1073 aws_channel_get_event_loop(server_connection->channel), &server_connection->service_task);
1074
1075 aws_mqtt5_decoder_clean_up(&server_connection->decoder);
1076 aws_mqtt5_encoder_clean_up(&server_connection->encoder);
1077 aws_mqtt5_inbound_topic_alias_resolver_clean_up(&server_connection->inbound_alias_resolver);
1078
1079 aws_mem_release(server_connection->allocator, server_connection);
1080 }
1081
1082 static size_t s_message_overhead(struct aws_channel_handler *handler) {
1083 (void)handler;
1084
1085 return 0;
1086 }
1087
1088 static struct aws_channel_handler_vtable s_mqtt5_mock_server_channel_handler_vtable = {
1089 .process_read_message = &s_process_read_message,
1090 .process_write_message = NULL,
1091 .increment_read_window = NULL,
1092 .shutdown = &s_shutdown,
1093 .initial_window_size = &s_initial_window_size,
1094 .message_overhead = &s_message_overhead,
1095 .destroy = &s_destroy,
1096 };
1097
1098 static void s_mock_server_service_task_fn(struct aws_task *task, void *arg, enum aws_task_status status) {
1099 if (status != AWS_TASK_STATUS_RUN_READY) {
1100 return;
1101 }
1102
1103 struct aws_mqtt5_server_mock_connection_context *server_connection = arg;
1104
1105 aws_mqtt5_mock_server_service_fn *service_fn =
1106 server_connection->test_fixture->server_function_table->service_task_fn;
1107 if (service_fn != NULL) {
1108 (*service_fn)(server_connection, server_connection->test_fixture->mock_server_user_data);
1109 }
1110
1111 uint64_t now = 0;
1112 aws_high_res_clock_get_ticks(&now);
1113 uint64_t next_service_time = now + aws_timestamp_convert(1, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL);
1114
1115 aws_event_loop_schedule_task_future(
1116 aws_channel_get_event_loop(server_connection->channel), task, next_service_time);
1117 }
1118
1119 static void s_on_incoming_channel_setup_fn(
1120 struct aws_server_bootstrap *bootstrap,
1121 int error_code,
1122 struct aws_channel *channel,
1123 void *user_data) {
1124 (void)bootstrap;
1125 struct aws_mqtt5_client_mock_test_fixture *test_fixture = user_data;
1126
1127 if (!error_code) {
1128 struct aws_channel_slot *test_handler_slot = aws_channel_slot_new(channel);
1129 aws_channel_slot_insert_end(channel, test_handler_slot);
1130
1131 struct aws_mqtt5_server_mock_connection_context *server_connection =
1132 aws_mem_calloc(test_fixture->allocator, 1, sizeof(struct aws_mqtt5_server_mock_connection_context));
1133 server_connection->allocator = test_fixture->allocator;
1134 server_connection->channel = channel;
1135 server_connection->test_fixture = test_fixture;
1136 server_connection->slot = test_handler_slot;
1137 server_connection->handler.alloc = server_connection->allocator;
1138 server_connection->handler.vtable = &s_mqtt5_mock_server_channel_handler_vtable;
1139 server_connection->handler.impl = server_connection;
1140
1141 aws_task_init(
1142 &server_connection->service_task,
1143 s_mock_server_service_task_fn,
1144 server_connection,
1145 "mock_server_service_task_fn");
1146 aws_event_loop_schedule_task_now(aws_channel_get_event_loop(channel), &server_connection->service_task);
1147
1148 aws_channel_slot_set_handler(server_connection->slot, &server_connection->handler);
1149
1150 aws_mqtt5_encode_init_testing_function_table(&server_connection->encoding_table);
1151
1152 struct aws_mqtt5_encoder_options encoder_options = {
1153 .client = NULL,
1154 .encoders = &server_connection->encoding_table,
1155 };
1156
1157 aws_mqtt5_encoder_init(&server_connection->encoder, server_connection->allocator, &encoder_options);
1158
1159 aws_mqtt5_decode_init_testing_function_table(&server_connection->decoding_table);
1160
1161 struct aws_mqtt5_decoder_options decoder_options = {
1162 .callback_user_data = server_connection,
1163 .on_packet_received = s_aws_mqtt5_mock_test_fixture_on_packet_received_fn,
1164 .decoder_table = &server_connection->decoding_table,
1165 };
1166
1167 aws_mqtt5_decoder_init(&server_connection->decoder, server_connection->allocator, &decoder_options);
1168 aws_mqtt5_inbound_topic_alias_resolver_init(
1169 &server_connection->inbound_alias_resolver, server_connection->allocator);
1170 aws_mqtt5_inbound_topic_alias_resolver_reset(
1171 &server_connection->inbound_alias_resolver, test_fixture->maximum_inbound_topic_aliases);
1172 aws_mqtt5_decoder_set_inbound_topic_alias_resolver(
1173 &server_connection->decoder, &server_connection->inbound_alias_resolver);
1174
1175 aws_mutex_lock(&test_fixture->lock);
1176 test_fixture->server_channel = channel;
1177 aws_mutex_unlock(&test_fixture->lock);
1178
1179 /*
1180 * Just like the tls tests in aws-c-io, it's possible for the server channel setup to execute after the client
1181 * channel setup has already posted data to the socket. In this case, the read notification gets lost because
1182 * the server hasn't subscribed to it yet and then we hang and time out. So do the same thing we do for
1183 * tls server channel setup and force a read of the socket after we're fully initialized.
1184 */
1185 aws_channel_trigger_read(channel);
1186 }
1187 }
1188
1189 static void s_on_incoming_channel_shutdown_fn(
1190 struct aws_server_bootstrap *bootstrap,
1191 int error_code,
1192 struct aws_channel *channel,
1193 void *user_data) {
1194 (void)bootstrap;
1195 (void)error_code;
1196 (void)channel;
1197 (void)user_data;
1198 }
1199
1200 static void s_on_listener_destroy(struct aws_server_bootstrap *bootstrap, void *user_data) {
1201 (void)bootstrap;
1202 struct aws_mqtt5_client_mock_test_fixture *test_fixture = user_data;
1203 aws_mutex_lock(&test_fixture->lock);
1204 test_fixture->listener_destroyed = true;
1205 aws_mutex_unlock(&test_fixture->lock);
1206 aws_condition_variable_notify_one(&test_fixture->signal);
1207 }
1208
1209 static bool s_is_listener_destroyed(void *arg) {
1210 struct aws_mqtt5_client_mock_test_fixture *test_fixture = arg;
1211 return test_fixture->listener_destroyed;
1212 }
1213
1214 static void s_wait_on_listener_cleanup(struct aws_mqtt5_client_mock_test_fixture *test_fixture) {
1215 aws_mutex_lock(&test_fixture->lock);
1216 aws_condition_variable_wait_pred(&test_fixture->signal, &test_fixture->lock, s_is_listener_destroyed, test_fixture);
1217 aws_mutex_unlock(&test_fixture->lock);
1218 }
1219
1220 static bool s_has_client_terminated(void *arg) {
1221 struct aws_mqtt5_client_mock_test_fixture *test_fixture = arg;
1222 return test_fixture->client_terminated;
1223 }
1224
1225 static void s_wait_for_client_terminated(struct aws_mqtt5_client_mock_test_fixture *test_context) {
1226 aws_mutex_lock(&test_context->lock);
1227 aws_condition_variable_wait_pred(&test_context->signal, &test_context->lock, s_has_client_terminated, test_context);
1228 aws_mutex_unlock(&test_context->lock);
1229 }
1230
1231 void s_aws_mqtt5_test_fixture_lifecycle_event_handler(const struct aws_mqtt5_client_lifecycle_event *event) {
1232 struct aws_mqtt5_client_mock_test_fixture *test_fixture = event->user_data;
1233
1234 struct aws_mqtt5_lifecycle_event_record *record =
1235 aws_mem_calloc(test_fixture->allocator, 1, sizeof(struct aws_mqtt5_lifecycle_event_record));
1236 record->allocator = test_fixture->allocator;
1237 aws_high_res_clock_get_ticks(&record->timestamp);
1238 record->event = *event;
1239
1240 if (event->settings != NULL) {
1241 record->settings_storage = *event->settings;
1242 record->event.settings = &record->settings_storage;
1243 }
1244
1245 if (event->disconnect_data != NULL) {
1246 aws_mqtt5_packet_disconnect_storage_init(
1247 &record->disconnect_storage, record->allocator, event->disconnect_data);
1248 record->event.disconnect_data = &record->disconnect_storage.storage_view;
1249 }
1250
1251 if (event->connack_data != NULL) {
1252 aws_mqtt5_packet_connack_storage_init(&record->connack_storage, record->allocator, event->connack_data);
1253 record->event.connack_data = &record->connack_storage.storage_view;
1254 }
1255
1256 aws_mutex_lock(&test_fixture->lock);
1257 AWS_LOGF_DEBUG(
1258 AWS_LS_MQTT5_GENERAL,
1259 "mqtt5 mock server received lifecycle event of type %s",
1260 aws_mqtt5_client_lifecycle_event_type_to_c_string(event->event_type));
1261 aws_array_list_push_back(&test_fixture->lifecycle_events, &record);
1262 aws_mutex_unlock(&test_fixture->lock);
1263 aws_condition_variable_notify_all(&test_fixture->signal);
1264
1265 aws_mqtt5_client_connection_event_callback_fn *event_callback = test_fixture->original_lifecycle_event_handler;
1266 if (event_callback != NULL) {
1267 struct aws_mqtt5_client_lifecycle_event event_copy = *event;
1268 event_copy.user_data = test_fixture->original_lifecycle_event_handler_user_data;
1269
1270 (*event_callback)(&event_copy);
1271 }
1272 }
1273
1274 void s_aws_mqtt5_test_fixture_state_changed_callback(
1275 struct aws_mqtt5_client *client,
1276 enum aws_mqtt5_client_state old_state,
1277 enum aws_mqtt5_client_state new_state,
1278 void *vtable_user_data) {
1279 (void)old_state;
1280 (void)client;
1281
1282 struct aws_mqtt5_client_mock_test_fixture *test_fixture = vtable_user_data;
1283
1284 aws_mutex_lock(&test_fixture->lock);
1285 AWS_LOGF_DEBUG(
1286 AWS_LS_MQTT5_GENERAL,
1287 "mqtt5 mock server received client state change to %s",
1288 aws_mqtt5_client_state_to_c_string(new_state));
1289 aws_array_list_push_back(&test_fixture->client_states, &new_state);
1290 aws_mutex_unlock(&test_fixture->lock);
1291 }
1292
1293 static void s_aws_mqtt5_test_fixture_statistics_changed_callback(
1294 struct aws_mqtt5_client *client,
1295 struct aws_mqtt5_operation *operation,
1296 void *vtable_user_data) {
1297 (void)operation;
1298
1299 struct aws_mqtt5_client_mock_test_fixture *test_fixture = vtable_user_data;
1300
1301 struct aws_mqtt5_client_operation_statistics stats;
1302 AWS_ZERO_STRUCT(stats);
1303
1304 aws_mutex_lock(&test_fixture->lock);
1305 aws_mqtt5_client_get_stats(client, &stats);
1306 aws_array_list_push_back(&test_fixture->client_statistics, &stats);
1307 aws_mutex_unlock(&test_fixture->lock);
1308 }
1309
1310 static void s_on_test_client_termination(void *user_data) {
1311 struct aws_mqtt5_client_mock_test_fixture *test_fixture = user_data;
1312
1313 aws_mutex_lock(&test_fixture->lock);
1314 test_fixture->client_terminated = true;
1315 aws_mutex_unlock(&test_fixture->lock);
1316 aws_condition_variable_notify_all(&test_fixture->signal);
1317 }
1318
1319 int aws_mqtt5_client_mock_test_fixture_init(
1320 struct aws_mqtt5_client_mock_test_fixture *test_fixture,
1321 struct aws_allocator *allocator,
1322 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options *options) {
1323
1324 AWS_ZERO_STRUCT(*test_fixture);
1325
1326 test_fixture->allocator = allocator;
1327
1328 aws_mutex_init(&test_fixture->lock);
1329 aws_condition_variable_init(&test_fixture->signal);
1330
1331 test_fixture->server_function_table = options->server_function_table;
1332 test_fixture->mock_server_user_data = options->mock_server_user_data;
1333
1334 struct aws_socket_options socket_options = {
1335 .connect_timeout_ms = 1000,
1336 .domain = AWS_SOCKET_LOCAL,
1337 };
1338
1339 test_fixture->socket_options = socket_options;
1340 test_fixture->server_elg = aws_event_loop_group_new_default(allocator, 1, NULL);
1341 test_fixture->server_bootstrap = aws_server_bootstrap_new(allocator, test_fixture->server_elg);
1342
1343 test_fixture->client_elg = aws_event_loop_group_new_default(allocator, 4, NULL);
1344 struct aws_host_resolver_default_options resolver_options = {
1345 .el_group = test_fixture->client_elg,
1346 .max_entries = 1,
1347 };
1348 test_fixture->host_resolver = aws_host_resolver_new_default(allocator, &resolver_options);
1349
1350 struct aws_client_bootstrap_options bootstrap_options = {
1351 .event_loop_group = test_fixture->client_elg,
1352 .user_data = test_fixture,
1353 .host_resolver = test_fixture->host_resolver,
1354 };
1355
1356 test_fixture->client_bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
1357
1358 aws_socket_endpoint_init_local_address_for_test(&test_fixture->endpoint);
1359
1360 struct aws_server_socket_channel_bootstrap_options server_bootstrap_options = {
1361 .bootstrap = test_fixture->server_bootstrap,
1362 .host_name = test_fixture->endpoint.address,
1363 .port = test_fixture->endpoint.port,
1364 .socket_options = &test_fixture->socket_options,
1365 .incoming_callback = s_on_incoming_channel_setup_fn,
1366 .shutdown_callback = s_on_incoming_channel_shutdown_fn,
1367 .destroy_callback = s_on_listener_destroy,
1368 .user_data = test_fixture,
1369 };
1370 test_fixture->listener = aws_server_bootstrap_new_socket_listener(&server_bootstrap_options);
1371
1372 test_fixture->original_lifecycle_event_handler = options->client_options->lifecycle_event_handler;
1373 test_fixture->original_lifecycle_event_handler_user_data =
1374 options->client_options->lifecycle_event_handler_user_data;
1375 options->client_options->lifecycle_event_handler = &s_aws_mqtt5_test_fixture_lifecycle_event_handler;
1376 options->client_options->lifecycle_event_handler_user_data = test_fixture;
1377
1378 options->client_options->host_name = aws_byte_cursor_from_c_str(test_fixture->endpoint.address);
1379 options->client_options->port = test_fixture->endpoint.port;
1380 options->client_options->socket_options = &test_fixture->socket_options;
1381 options->client_options->bootstrap = test_fixture->client_bootstrap;
1382
1383 options->client_options->client_termination_handler = s_on_test_client_termination;
1384 options->client_options->client_termination_handler_user_data = test_fixture;
1385
1386 test_fixture->client = aws_mqtt5_client_new(allocator, options->client_options);
1387
1388 test_fixture->client_vtable = *aws_mqtt5_client_get_default_vtable();
1389 test_fixture->client_vtable.on_client_state_change_callback_fn = s_aws_mqtt5_test_fixture_state_changed_callback;
1390 test_fixture->client_vtable.on_client_statistics_changed_callback_fn =
1391 s_aws_mqtt5_test_fixture_statistics_changed_callback;
1392 test_fixture->client_vtable.vtable_user_data = test_fixture;
1393
1394 aws_mqtt5_client_set_vtable(test_fixture->client, &test_fixture->client_vtable);
1395
1396 aws_array_list_init_dynamic(
1397 &test_fixture->server_received_packets, allocator, 10, sizeof(struct aws_mqtt5_mock_server_packet_record));
1398
1399 aws_array_list_init_dynamic(
1400 &test_fixture->lifecycle_events, allocator, 10, sizeof(struct aws_mqtt5_lifecycle_event_record *));
1401
1402 aws_array_list_init_dynamic(&test_fixture->client_states, allocator, 10, sizeof(enum aws_mqtt5_client_state));
1403 aws_array_list_init_dynamic(
1404 &test_fixture->client_statistics, allocator, 10, sizeof(struct aws_mqtt5_client_operation_statistics));
1405
1406 return AWS_OP_SUCCESS;
1407 }
1408
1409 static void s_destroy_packet_storage(
1410 void *packet_storage,
1411 struct aws_allocator *allocator,
1412 enum aws_mqtt5_packet_type packet_type) {
1413 switch (packet_type) {
1414 case AWS_MQTT5_PT_CONNECT:
1415 aws_mqtt5_packet_connect_storage_clean_up(packet_storage);
1416 break;
1417
1418 case AWS_MQTT5_PT_DISCONNECT:
1419 aws_mqtt5_packet_disconnect_storage_clean_up(packet_storage);
1420 break;
1421
1422 case AWS_MQTT5_PT_SUBSCRIBE:
1423 aws_mqtt5_packet_subscribe_storage_clean_up(packet_storage);
1424 break;
1425
1426 case AWS_MQTT5_PT_SUBACK:
1427 aws_mqtt5_packet_suback_storage_clean_up(packet_storage);
1428 break;
1429
1430 case AWS_MQTT5_PT_UNSUBSCRIBE:
1431 aws_mqtt5_packet_unsubscribe_storage_clean_up(packet_storage);
1432 break;
1433
1434 case AWS_MQTT5_PT_UNSUBACK:
1435 aws_mqtt5_packet_unsuback_storage_clean_up(packet_storage);
1436 break;
1437
1438 case AWS_MQTT5_PT_PUBLISH:
1439 aws_mqtt5_packet_publish_storage_clean_up(packet_storage);
1440 break;
1441
1442 case AWS_MQTT5_PT_PUBACK:
1443 /* TODO */
1444 break;
1445
1446 case AWS_MQTT5_PT_PINGREQ:
1447 AWS_FATAL_ASSERT(packet_storage == NULL);
1448 break;
1449
1450 default:
1451 AWS_FATAL_ASSERT(false);
1452 }
1453
1454 if (packet_storage != NULL) {
1455 aws_mem_release(allocator, packet_storage);
1456 }
1457 }
1458
1459 static void s_destroy_lifecycle_event_storage(struct aws_mqtt5_lifecycle_event_record *event) {
1460 aws_mqtt5_packet_connack_storage_clean_up(&event->connack_storage);
1461 aws_mqtt5_packet_disconnect_storage_clean_up(&event->disconnect_storage);
1462
1463 aws_mem_release(event->allocator, event);
1464 }
1465
1466 void aws_mqtt5_client_mock_test_fixture_clean_up(struct aws_mqtt5_client_mock_test_fixture *test_fixture) {
1467 aws_mqtt5_client_release(test_fixture->client);
1468 s_wait_for_client_terminated(test_fixture);
1469 aws_client_bootstrap_release(test_fixture->client_bootstrap);
1470 aws_host_resolver_release(test_fixture->host_resolver);
1471 aws_server_bootstrap_destroy_socket_listener(test_fixture->server_bootstrap, test_fixture->listener);
1472
1473 s_wait_on_listener_cleanup(test_fixture);
1474
1475 aws_server_bootstrap_release(test_fixture->server_bootstrap);
1476 aws_event_loop_group_release(test_fixture->server_elg);
1477 aws_event_loop_group_release(test_fixture->client_elg);
1478
1479 for (size_t i = 0; i < aws_array_list_length(&test_fixture->server_received_packets); ++i) {
1480 struct aws_mqtt5_mock_server_packet_record *packet = NULL;
1481 aws_array_list_get_at_ptr(&test_fixture->server_received_packets, (void **)&packet, i);
1482
1483 s_destroy_packet_storage(packet->packet_storage, packet->storage_allocator, packet->packet_type);
1484 }
1485
1486 aws_array_list_clean_up(&test_fixture->server_received_packets);
1487
1488 for (size_t i = 0; i < aws_array_list_length(&test_fixture->lifecycle_events); ++i) {
1489 struct aws_mqtt5_lifecycle_event_record *event = NULL;
1490 aws_array_list_get_at(&test_fixture->lifecycle_events, &event, i);
1491
1492 s_destroy_lifecycle_event_storage(event);
1493 }
1494
1495 aws_array_list_clean_up(&test_fixture->lifecycle_events);
1496 aws_array_list_clean_up(&test_fixture->client_states);
1497 aws_array_list_clean_up(&test_fixture->client_statistics);
1498
1499 aws_mutex_clean_up(&test_fixture->lock);
1500 aws_condition_variable_clean_up(&test_fixture->signal);
1501 }
1502
1503 #define AWS_MQTT5_CLIENT_TEST_CHECK_INT_EQUALS(lhs, rhs) \
1504 if ((lhs) != (rhs)) { \
1505 return false; \
1506 }
1507
1508 #define AWS_MQTT5_CLIENT_TEST_CHECK_OPTIONAL_INT_EQUALS(lhs, rhs) \
1509 if (((lhs) != NULL) != ((rhs) != NULL)) { \
1510 return false; \
1511 } \
1512 if (((lhs) != NULL) && ((rhs) != NULL) && (*(lhs) != *(rhs))) { \
1513 return false; \
1514 }
1515
1516 #define AWS_MQTT5_CLIENT_TEST_CHECK_CURSOR_EQUALS(lhs, rhs) \
1517 if (!aws_byte_cursor_eq(&(lhs), &(rhs))) { \
1518 return false; \
1519 }
1520
1521 #define AWS_MQTT5_CLIENT_TEST_CHECK_OPTIONAL_CURSOR_EQUALS(lhs, rhs) \
1522 if (((lhs) != NULL) != ((rhs) != NULL)) { \
1523 return false; \
1524 } \
1525 if (((lhs) != NULL) && ((rhs) != NULL) && (!aws_byte_cursor_eq(lhs, rhs))) { \
1526 return false; \
1527 }
1528
1529 #define AWS_MQTT5_CLIENT_TEST_CHECK_USER_PROPERTIES(lhs_props, lhs_prop_count, rhs_props, rhs_prop_count) \
1530 if (aws_mqtt5_test_verify_user_properties_raw((lhs_prop_count), (lhs_props), (rhs_prop_count), (rhs_props)) != \
1531 AWS_OP_SUCCESS) { \
1532 return false; \
1533 }
1534
1535 static bool s_aws_publish_packets_equal(
1536 const struct aws_mqtt5_packet_publish_view *lhs,
1537 const struct aws_mqtt5_packet_publish_view *rhs) {
1538
1539 // Don't check packet id intentionally
1540
1541 AWS_MQTT5_CLIENT_TEST_CHECK_CURSOR_EQUALS(lhs->payload, rhs->payload);
1542 AWS_MQTT5_CLIENT_TEST_CHECK_INT_EQUALS(lhs->qos, rhs->qos);
1543 AWS_MQTT5_CLIENT_TEST_CHECK_INT_EQUALS(lhs->duplicate, rhs->duplicate);
1544 AWS_MQTT5_CLIENT_TEST_CHECK_INT_EQUALS(lhs->retain, rhs->retain);
1545 AWS_MQTT5_CLIENT_TEST_CHECK_CURSOR_EQUALS(lhs->topic, rhs->topic);
1546 AWS_MQTT5_CLIENT_TEST_CHECK_OPTIONAL_INT_EQUALS(lhs->payload_format, rhs->payload_format);
1547 AWS_MQTT5_CLIENT_TEST_CHECK_OPTIONAL_INT_EQUALS(
1548 lhs->message_expiry_interval_seconds, rhs->message_expiry_interval_seconds);
1549 AWS_MQTT5_CLIENT_TEST_CHECK_OPTIONAL_INT_EQUALS(lhs->topic_alias, rhs->topic_alias);
1550 AWS_MQTT5_CLIENT_TEST_CHECK_OPTIONAL_CURSOR_EQUALS(lhs->response_topic, rhs->response_topic);
1551 AWS_MQTT5_CLIENT_TEST_CHECK_OPTIONAL_CURSOR_EQUALS(lhs->correlation_data, rhs->correlation_data);
1552
1553 AWS_MQTT5_CLIENT_TEST_CHECK_INT_EQUALS(lhs->subscription_identifier_count, rhs->subscription_identifier_count);
1554 for (size_t i = 0; i < lhs->subscription_identifier_count; ++i) {
1555 AWS_MQTT5_CLIENT_TEST_CHECK_INT_EQUALS(lhs->subscription_identifiers[i], rhs->subscription_identifiers[i]);
1556 }
1557
1558 AWS_MQTT5_CLIENT_TEST_CHECK_USER_PROPERTIES(
1559 lhs->user_properties, lhs->user_property_count, rhs->user_properties, rhs->user_property_count);
1560
1561 return true;
1562 }
1563
1564 static bool s_aws_connect_packets_equal(
1565 const struct aws_mqtt5_packet_connect_view *lhs,
1566 const struct aws_mqtt5_packet_connect_view *rhs) {
1567 AWS_MQTT5_CLIENT_TEST_CHECK_INT_EQUALS(lhs->keep_alive_interval_seconds, rhs->keep_alive_interval_seconds);
1568 AWS_MQTT5_CLIENT_TEST_CHECK_CURSOR_EQUALS(lhs->client_id, rhs->client_id);
1569 AWS_MQTT5_CLIENT_TEST_CHECK_OPTIONAL_CURSOR_EQUALS(lhs->username, rhs->username);
1570 AWS_MQTT5_CLIENT_TEST_CHECK_OPTIONAL_CURSOR_EQUALS(lhs->password, rhs->password);
1571 AWS_MQTT5_CLIENT_TEST_CHECK_INT_EQUALS(lhs->clean_start, rhs->clean_start);
1572 AWS_MQTT5_CLIENT_TEST_CHECK_OPTIONAL_INT_EQUALS(
1573 lhs->session_expiry_interval_seconds, rhs->session_expiry_interval_seconds);
1574 AWS_MQTT5_CLIENT_TEST_CHECK_OPTIONAL_INT_EQUALS(
1575 lhs->request_response_information, rhs->request_response_information);
1576 AWS_MQTT5_CLIENT_TEST_CHECK_OPTIONAL_INT_EQUALS(lhs->request_problem_information, rhs->request_problem_information);
1577 AWS_MQTT5_CLIENT_TEST_CHECK_OPTIONAL_INT_EQUALS(lhs->receive_maximum, rhs->receive_maximum);
1578 AWS_MQTT5_CLIENT_TEST_CHECK_OPTIONAL_INT_EQUALS(lhs->topic_alias_maximum, rhs->topic_alias_maximum);
1579 AWS_MQTT5_CLIENT_TEST_CHECK_OPTIONAL_INT_EQUALS(lhs->maximum_packet_size_bytes, rhs->maximum_packet_size_bytes);
1580 AWS_MQTT5_CLIENT_TEST_CHECK_OPTIONAL_INT_EQUALS(lhs->will_delay_interval_seconds, rhs->will_delay_interval_seconds);
1581
1582 if ((lhs->will != NULL) != (rhs->will != NULL)) {
1583 return false;
1584 }
1585
1586 if (lhs->will) {
1587 if (!s_aws_publish_packets_equal(lhs->will, rhs->will)) {
1588 return false;
1589 }
1590 }
1591
1592 AWS_MQTT5_CLIENT_TEST_CHECK_USER_PROPERTIES(
1593 lhs->user_properties, lhs->user_property_count, rhs->user_properties, rhs->user_property_count);
1594
1595 AWS_MQTT5_CLIENT_TEST_CHECK_OPTIONAL_CURSOR_EQUALS(lhs->authentication_method, rhs->authentication_method);
1596 AWS_MQTT5_CLIENT_TEST_CHECK_OPTIONAL_CURSOR_EQUALS(lhs->authentication_data, rhs->authentication_data);
1597
1598 return true;
1599 }
1600
1601 static bool s_aws_disconnect_packets_equal(
1602 const struct aws_mqtt5_packet_disconnect_view *lhs,
1603 const struct aws_mqtt5_packet_disconnect_view *rhs) {
1604
1605 AWS_MQTT5_CLIENT_TEST_CHECK_INT_EQUALS(lhs->reason_code, rhs->reason_code);
1606 AWS_MQTT5_CLIENT_TEST_CHECK_OPTIONAL_CURSOR_EQUALS(lhs->reason_string, rhs->reason_string);
1607 AWS_MQTT5_CLIENT_TEST_CHECK_OPTIONAL_INT_EQUALS(
1608 lhs->session_expiry_interval_seconds, rhs->session_expiry_interval_seconds);
1609 AWS_MQTT5_CLIENT_TEST_CHECK_OPTIONAL_CURSOR_EQUALS(lhs->server_reference, rhs->server_reference);
1610
1611 AWS_MQTT5_CLIENT_TEST_CHECK_USER_PROPERTIES(
1612 lhs->user_properties, lhs->user_property_count, rhs->user_properties, rhs->user_property_count);
1613
1614 return true;
1615 }
1616
1617 static bool s_are_subscription_views_equal(
1618 const struct aws_mqtt5_subscription_view *lhs,
1619 const struct aws_mqtt5_subscription_view *rhs) {
1620 AWS_MQTT5_CLIENT_TEST_CHECK_CURSOR_EQUALS(lhs->topic_filter, rhs->topic_filter);
1621 AWS_MQTT5_CLIENT_TEST_CHECK_INT_EQUALS(lhs->qos, rhs->qos);
1622 AWS_MQTT5_CLIENT_TEST_CHECK_INT_EQUALS(lhs->no_local, rhs->no_local);
1623 AWS_MQTT5_CLIENT_TEST_CHECK_INT_EQUALS(lhs->retain_as_published, rhs->retain_as_published);
1624 AWS_MQTT5_CLIENT_TEST_CHECK_INT_EQUALS(lhs->retain_handling_type, rhs->retain_handling_type);
1625
1626 return true;
1627 }
1628
1629 static bool s_aws_subscribe_packets_equal(
1630 const struct aws_mqtt5_packet_subscribe_view *lhs,
1631 const struct aws_mqtt5_packet_subscribe_view *rhs) {
1632
1633 // Don't check packet id intentionally
1634
1635 AWS_MQTT5_CLIENT_TEST_CHECK_INT_EQUALS(lhs->subscription_count, rhs->subscription_count);
1636 for (size_t i = 0; i < lhs->subscription_count; ++i) {
1637 const struct aws_mqtt5_subscription_view *lhs_sub_view = &lhs->subscriptions[i];
1638 const struct aws_mqtt5_subscription_view *rhs_sub_view = &rhs->subscriptions[i];
1639
1640 if (!s_are_subscription_views_equal(lhs_sub_view, rhs_sub_view)) {
1641 return false;
1642 }
1643 }
1644
1645 AWS_MQTT5_CLIENT_TEST_CHECK_OPTIONAL_INT_EQUALS(lhs->subscription_identifier, rhs->subscription_identifier);
1646
1647 AWS_MQTT5_CLIENT_TEST_CHECK_USER_PROPERTIES(
1648 lhs->user_properties, lhs->user_property_count, rhs->user_properties, rhs->user_property_count);
1649
1650 return true;
1651 }
1652
1653 static bool s_aws_unsubscribe_packets_equal(
1654 const struct aws_mqtt5_packet_unsubscribe_view *lhs,
1655 const struct aws_mqtt5_packet_unsubscribe_view *rhs) {
1656
1657 // Don't check packet id intentionally
1658
1659 AWS_MQTT5_CLIENT_TEST_CHECK_INT_EQUALS(lhs->topic_filter_count, rhs->topic_filter_count);
1660 for (size_t i = 0; i < lhs->topic_filter_count; ++i) {
1661 struct aws_byte_cursor lhs_topic_filter = lhs->topic_filters[i];
1662 struct aws_byte_cursor rhs_topic_filter = rhs->topic_filters[i];
1663
1664 AWS_MQTT5_CLIENT_TEST_CHECK_CURSOR_EQUALS(lhs_topic_filter, rhs_topic_filter);
1665 }
1666
1667 AWS_MQTT5_CLIENT_TEST_CHECK_USER_PROPERTIES(
1668 lhs->user_properties, lhs->user_property_count, rhs->user_properties, rhs->user_property_count);
1669
1670 return true;
1671 }
1672
1673 static bool s_aws_puback_packets_equal(
1674 const struct aws_mqtt5_packet_puback_view *lhs,
1675 const struct aws_mqtt5_packet_puback_view *rhs) {
1676
1677 // Don't check packet id intentionally
1678
1679 AWS_MQTT5_CLIENT_TEST_CHECK_INT_EQUALS(lhs->reason_code, rhs->reason_code);
1680 AWS_MQTT5_CLIENT_TEST_CHECK_OPTIONAL_CURSOR_EQUALS(lhs->reason_string, rhs->reason_string);
1681
1682 AWS_MQTT5_CLIENT_TEST_CHECK_USER_PROPERTIES(
1683 lhs->user_properties, lhs->user_property_count, rhs->user_properties, rhs->user_property_count);
1684
1685 return true;
1686 }
1687
1688 bool aws_mqtt5_client_test_are_packets_equal(
1689 enum aws_mqtt5_packet_type packet_type,
1690 void *lhs_packet_storage,
1691 void *rhs_packet_storage) {
1692 switch (packet_type) {
1693 case AWS_MQTT5_PT_CONNECT:
1694 return s_aws_connect_packets_equal(
1695 &((struct aws_mqtt5_packet_connect_storage *)lhs_packet_storage)->storage_view,
1696 &((struct aws_mqtt5_packet_connect_storage *)rhs_packet_storage)->storage_view);
1697
1698 case AWS_MQTT5_PT_DISCONNECT:
1699 return s_aws_disconnect_packets_equal(
1700 &((struct aws_mqtt5_packet_disconnect_storage *)lhs_packet_storage)->storage_view,
1701 &((struct aws_mqtt5_packet_disconnect_storage *)rhs_packet_storage)->storage_view);
1702
1703 case AWS_MQTT5_PT_SUBSCRIBE:
1704 return s_aws_subscribe_packets_equal(
1705 &((struct aws_mqtt5_packet_subscribe_storage *)lhs_packet_storage)->storage_view,
1706 &((struct aws_mqtt5_packet_subscribe_storage *)rhs_packet_storage)->storage_view);
1707
1708 case AWS_MQTT5_PT_UNSUBSCRIBE:
1709 return s_aws_unsubscribe_packets_equal(
1710 &((struct aws_mqtt5_packet_unsubscribe_storage *)lhs_packet_storage)->storage_view,
1711 &((struct aws_mqtt5_packet_unsubscribe_storage *)rhs_packet_storage)->storage_view);
1712
1713 case AWS_MQTT5_PT_PUBLISH:
1714 return s_aws_publish_packets_equal(
1715 &((struct aws_mqtt5_packet_publish_storage *)lhs_packet_storage)->storage_view,
1716 &((struct aws_mqtt5_packet_publish_storage *)rhs_packet_storage)->storage_view);
1717
1718 case AWS_MQTT5_PT_PUBACK:
1719 return s_aws_puback_packets_equal(
1720 &((struct aws_mqtt5_packet_puback_storage *)lhs_packet_storage)->storage_view,
1721 &((struct aws_mqtt5_packet_puback_storage *)rhs_packet_storage)->storage_view);
1722
1723 case AWS_MQTT5_PT_PINGREQ:
1724 case AWS_MQTT5_PT_PINGRESP:
1725 return true;
1726
1727 default:
1728 return false;
1729 }
1730 }
1731
1732 size_t aws_mqtt5_linked_list_length(struct aws_linked_list *list) {
1733 size_t length = 0;
1734 struct aws_linked_list_node *node = aws_linked_list_begin(list);
1735 while (node != aws_linked_list_end(list)) {
1736 ++length;
1737 node = aws_linked_list_next(node);
1738 }
1739
1740 return length;
1741 }
1742
1743 #define TEST_IO_MESSAGE_LENGTH 4096
1744
1745 int aws_mqtt5_mock_server_send_packet(
1746 struct aws_mqtt5_server_mock_connection_context *connection,
1747 enum aws_mqtt5_packet_type packet_type,
1748 void *packet) {
1749 aws_mqtt5_encoder_append_packet_encoding(&connection->encoder, packet_type, packet);
1750
1751 struct aws_io_message *message = aws_channel_acquire_message_from_pool(
1752 connection->slot->channel, AWS_IO_MESSAGE_APPLICATION_DATA, TEST_IO_MESSAGE_LENGTH);
1753 if (message == NULL) {
1754 return AWS_OP_ERR;
1755 }
1756
1757 enum aws_mqtt5_encoding_result result =
1758 aws_mqtt5_encoder_encode_to_buffer(&connection->encoder, &message->message_data);
1759 AWS_FATAL_ASSERT(result == AWS_MQTT5_ER_FINISHED);
1760
1761 if (aws_channel_slot_send_message(connection->slot, message, AWS_CHANNEL_DIR_WRITE)) {
1762 aws_mem_release(message->allocator, message);
1763 return AWS_OP_ERR;
1764 }
1765
1766 return AWS_OP_SUCCESS;
1767 }