Debian Codegraph @

crt/aws-c-mqtt/tests/v5/mqtt5_encoding_tests.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 #include <aws/common/string.h>
8 #include <aws/io/stream.h>
9 #include <aws/mqtt/mqtt.h>
10 #include <aws/mqtt/private/v5/mqtt5_decoder.h>
11 #include <aws/mqtt/private/v5/mqtt5_encoder.h>
12 #include <aws/mqtt/private/v5/mqtt5_utils.h>
13 #include <aws/mqtt/v5/mqtt5_types.h>
14
15 #include <aws/testing/aws_test_harness.h>
16
17 static int s_mqtt5_vli_size_fn(struct aws_allocator *allocator, void *ctx) {
18 (void)ctx;
19 (void)allocator;
20
21 size_t encode_size = 0;
22
23 ASSERT_SUCCESS(aws_mqtt5_get_variable_length_encode_size(0, &encode_size));
24 ASSERT_INT_EQUALS(1, encode_size);
25
26 ASSERT_SUCCESS(aws_mqtt5_get_variable_length_encode_size(1, &encode_size));
27 ASSERT_INT_EQUALS(1, encode_size);
28
29 ASSERT_SUCCESS(aws_mqtt5_get_variable_length_encode_size(127, &encode_size));
30 ASSERT_INT_EQUALS(1, encode_size);
31
32 ASSERT_SUCCESS(aws_mqtt5_get_variable_length_encode_size(128, &encode_size));
33 ASSERT_INT_EQUALS(2, encode_size);
34
35 ASSERT_SUCCESS(aws_mqtt5_get_variable_length_encode_size(256, &encode_size));
36 ASSERT_INT_EQUALS(2, encode_size);
37
38 ASSERT_SUCCESS(aws_mqtt5_get_variable_length_encode_size(16383, &encode_size));
39 ASSERT_INT_EQUALS(2, encode_size);
40
41 ASSERT_SUCCESS(aws_mqtt5_get_variable_length_encode_size(16384, &encode_size));
42 ASSERT_INT_EQUALS(3, encode_size);
43
44 ASSERT_SUCCESS(aws_mqtt5_get_variable_length_encode_size(16385, &encode_size));
45 ASSERT_INT_EQUALS(3, encode_size);
46
47 ASSERT_SUCCESS(aws_mqtt5_get_variable_length_encode_size(2097151, &encode_size));
48 ASSERT_INT_EQUALS(3, encode_size);
49
50 ASSERT_SUCCESS(aws_mqtt5_get_variable_length_encode_size(2097152, &encode_size));
51 ASSERT_INT_EQUALS(4, encode_size);
52
53 ASSERT_SUCCESS(aws_mqtt5_get_variable_length_encode_size(AWS_MQTT5_MAXIMUM_VARIABLE_LENGTH_INTEGER, &encode_size));
54 ASSERT_INT_EQUALS(4, encode_size);
55
56 ASSERT_FAILS(
57 aws_mqtt5_get_variable_length_encode_size(AWS_MQTT5_MAXIMUM_VARIABLE_LENGTH_INTEGER + 1, &encode_size));
58 ASSERT_FAILS(aws_mqtt5_get_variable_length_encode_size(0xFFFFFFFF, &encode_size));
59
60 return AWS_OP_SUCCESS;
61 }
62
63 AWS_TEST_CASE(mqtt5_vli_size, s_mqtt5_vli_size_fn)
64
65 static int s_do_success_round_trip_vli_test(uint32_t value, struct aws_allocator *allocator) {
66 struct aws_byte_buf buffer;
67 aws_byte_buf_init(&buffer, allocator, 4);
68
69 ASSERT_SUCCESS(aws_mqtt5_encode_variable_length_integer(&buffer, value));
70
71 size_t encoded_size = 0;
72 ASSERT_SUCCESS(aws_mqtt5_get_variable_length_encode_size(value, &encoded_size));
73 ASSERT_INT_EQUALS(encoded_size, buffer.len);
74
75 uint32_t decoded_value = 0;
76 for (size_t i = 1; i < encoded_size; ++i) {
77 struct aws_byte_cursor partial_cursor = aws_byte_cursor_from_buf(&buffer);
78 partial_cursor.len = i;
79
80 enum aws_mqtt5_decode_result_type result = aws_mqtt5_decode_vli(&partial_cursor, &decoded_value);
81 ASSERT_INT_EQUALS(AWS_MQTT5_DRT_MORE_DATA, result);
82 }
83
84 struct aws_byte_cursor full_cursor = aws_byte_cursor_from_buf(&buffer);
85
86 enum aws_mqtt5_decode_result_type result = aws_mqtt5_decode_vli(&full_cursor, &decoded_value);
87 ASSERT_INT_EQUALS(AWS_MQTT5_DRT_SUCCESS, result);
88 ASSERT_INT_EQUALS(decoded_value, value);
89
90 aws_byte_buf_clean_up(&buffer);
91
92 return AWS_OP_SUCCESS;
93 }
94
95 static int s_mqtt5_vli_success_round_trip_fn(struct aws_allocator *allocator, void *ctx) {
96 (void)ctx;
97
98 ASSERT_SUCCESS(s_do_success_round_trip_vli_test(0, allocator));
99 ASSERT_SUCCESS(s_do_success_round_trip_vli_test(1, allocator));
100 ASSERT_SUCCESS(s_do_success_round_trip_vli_test(47, allocator));
101 ASSERT_SUCCESS(s_do_success_round_trip_vli_test(127, allocator));
102 ASSERT_SUCCESS(s_do_success_round_trip_vli_test(128, allocator));
103 ASSERT_SUCCESS(s_do_success_round_trip_vli_test(129, allocator));
104 ASSERT_SUCCESS(s_do_success_round_trip_vli_test(511, allocator));
105 ASSERT_SUCCESS(s_do_success_round_trip_vli_test(8000, allocator));
106 ASSERT_SUCCESS(s_do_success_round_trip_vli_test(16383, allocator));
107 ASSERT_SUCCESS(s_do_success_round_trip_vli_test(16384, allocator));
108 ASSERT_SUCCESS(s_do_success_round_trip_vli_test(16385, allocator));
109 ASSERT_SUCCESS(s_do_success_round_trip_vli_test(100000, allocator));
110 ASSERT_SUCCESS(s_do_success_round_trip_vli_test(4200000, allocator));
111 ASSERT_SUCCESS(s_do_success_round_trip_vli_test(34200000, allocator));
112 ASSERT_SUCCESS(s_do_success_round_trip_vli_test(AWS_MQTT5_MAXIMUM_VARIABLE_LENGTH_INTEGER, allocator));
113
114 return AWS_OP_SUCCESS;
115 }
116
117 AWS_TEST_CASE(mqtt5_vli_success_round_trip, s_mqtt5_vli_success_round_trip_fn)
118
119 static int s_mqtt5_vli_encode_failures_fn(struct aws_allocator *allocator, void *ctx) {
120 (void)ctx;
121
122 struct aws_byte_buf buffer;
123 aws_byte_buf_init(&buffer, allocator, 4);
124
125 ASSERT_FAILS(aws_mqtt5_encode_variable_length_integer(&buffer, AWS_MQTT5_MAXIMUM_VARIABLE_LENGTH_INTEGER + 1));
126 ASSERT_FAILS(aws_mqtt5_encode_variable_length_integer(&buffer, 0x80000000));
127 ASSERT_FAILS(aws_mqtt5_encode_variable_length_integer(&buffer, 0xFFFFFFFF));
128
129 aws_byte_buf_clean_up(&buffer);
130
131 return AWS_OP_SUCCESS;
132 }
133
134 AWS_TEST_CASE(mqtt5_vli_encode_failures, s_mqtt5_vli_encode_failures_fn)
135
136 static uint8_t bad_buffers0[] = {0x80, 0x80, 0x80, 0x80};
137 static uint8_t bad_buffers1[] = {0x81, 0x81, 0x81, 0xFF};
138
139 static int s_mqtt5_vli_decode_failures_fn(struct aws_allocator *allocator, void *ctx) {
140 (void)ctx;
141 (void)allocator;
142
143 uint32_t value = 0;
144
145 struct aws_byte_cursor cursor = aws_byte_cursor_from_array(&bad_buffers0[0], AWS_ARRAY_SIZE(bad_buffers0));
146 ASSERT_INT_EQUALS(AWS_MQTT5_DRT_ERROR, aws_mqtt5_decode_vli(&cursor, &value));
147
148 cursor = aws_byte_cursor_from_array(&bad_buffers1[0], AWS_ARRAY_SIZE(bad_buffers1));
149 ASSERT_INT_EQUALS(AWS_MQTT5_DRT_ERROR, aws_mqtt5_decode_vli(&cursor, &value));
150
151 return AWS_OP_SUCCESS;
152 }
153
154 AWS_TEST_CASE(mqtt5_vli_decode_failures, s_mqtt5_vli_decode_failures_fn)
155
156 static char s_user_prop1_name[] = "Property1";
157 static char s_user_prop1_value[] = "Value1";
158 static char s_user_prop2_name[] = "Property2";
159 static char s_user_prop2_value[] = "Value2";
160 static const struct aws_mqtt5_user_property s_user_properties[] = {
161 {
162 .name =
163 {
164 .ptr = (uint8_t *)s_user_prop1_name,
165 .len = AWS_ARRAY_SIZE(s_user_prop1_name) - 1,
166 },
167 .value =
168 {
169 .ptr = (uint8_t *)s_user_prop1_value,
170 .len = AWS_ARRAY_SIZE(s_user_prop1_value) - 1,
171 },
172 },
173 {
174 .name =
175 {
176 .ptr = (uint8_t *)s_user_prop2_name,
177 .len = AWS_ARRAY_SIZE(s_user_prop2_name) - 1,
178 },
179 .value =
180 {
181 .ptr = (uint8_t *)s_user_prop2_value,
182 .len = AWS_ARRAY_SIZE(s_user_prop2_value) - 1,
183 },
184 },
185 };
186
187 static const char *s_reason_string = "This is why I'm disconnecting";
188 static const char *s_server_reference = "connect-here-instead.com";
189 static const char *s_will_payload = "{\"content\":\"This is the payload of a will message\"}";
190 static const char *s_will_topic = "zomg/where-did/my/connection-go";
191 static const char *s_will_response_topic = "TheWillResponseTopic";
192 static const char *s_will_correlation_data = "ThisAndThat";
193 static const char *s_will_content_type = "Json";
194 static const char *s_client_id = "DeviceNumber47";
195 static const char *s_username = "MyUser";
196 static const char *s_password = "SuprSekritDontRead";
197
198 struct aws_mqtt5_encode_decode_tester {
199 struct aws_mqtt5_encoder_function_table encoder_function_table;
200 struct aws_mqtt5_decoder_function_table decoder_function_table;
201
202 size_t view_count;
203
204 size_t expected_view_count;
205 void *expected_views;
206 };
207
208 static void s_aws_mqtt5_encode_decoder_tester_init_single_view(
209 struct aws_mqtt5_encode_decode_tester *tester,
210 void *expected_view) {
211 tester->view_count = 0;
212 tester->expected_view_count = 1;
213 tester->expected_views = expected_view;
214
215 aws_mqtt5_encode_init_testing_function_table(&tester->encoder_function_table);
216 aws_mqtt5_decode_init_testing_function_table(&tester->decoder_function_table);
217 }
218
219 int s_check_packet_encoding_reserved_flags(struct aws_byte_buf *encoding, enum aws_mqtt5_packet_type packet_type) {
220 if (packet_type == AWS_MQTT5_PT_PUBLISH) {
221 return AWS_OP_SUCCESS;
222 }
223
224 uint8_t expected_reserved_flags_value = 0;
225 switch (packet_type) {
226 case AWS_MQTT5_PT_PUBREL:
227 case AWS_MQTT5_PT_SUBSCRIBE:
228 case AWS_MQTT5_PT_UNSUBSCRIBE:
229 expected_reserved_flags_value = 0x02;
230 break;
231
232 default:
233 break;
234 }
235
236 uint8_t first_byte = encoding->buffer[0];
237
238 uint8_t encoded_packet_type = (first_byte >> 4) & 0x0F;
239 ASSERT_INT_EQUALS(packet_type, encoded_packet_type);
240
241 uint8_t reserved_flags = first_byte & 0x0F;
242 ASSERT_INT_EQUALS(expected_reserved_flags_value, reserved_flags);
243
244 return AWS_OP_SUCCESS;
245 }
246
247 typedef int(aws_mqtt5_check_encoding_fn)(struct aws_byte_buf *encoding);
248
249 struct aws_mqtt5_packet_round_trip_test_context {
250 struct aws_allocator *allocator;
251 enum aws_mqtt5_packet_type packet_type;
252 void *packet_view;
253 aws_mqtt5_on_packet_received_fn *decoder_callback;
254 aws_mqtt5_check_encoding_fn *encoding_checker;
255 };
256
257 static int s_aws_mqtt5_encode_decode_round_trip_test(
258 struct aws_mqtt5_packet_round_trip_test_context *context,
259 size_t encode_fragment_size,
260 size_t decode_fragment_size) {
261 struct aws_byte_buf whole_dest;
262 aws_byte_buf_init(&whole_dest, context->allocator, 4096);
263
264 struct aws_mqtt5_encode_decode_tester tester;
265 s_aws_mqtt5_encode_decoder_tester_init_single_view(&tester, context->packet_view);
266
267 struct aws_mqtt5_encoder_options encoder_options = {
268 .encoders = &tester.encoder_function_table,
269 };
270
271 struct aws_mqtt5_encoder encoder;
272 ASSERT_SUCCESS(aws_mqtt5_encoder_init(&encoder, context->allocator, &encoder_options));
273 ASSERT_SUCCESS(aws_mqtt5_encoder_append_packet_encoding(&encoder, context->packet_type, context->packet_view));
274
275 enum aws_mqtt5_encoding_result result = AWS_MQTT5_ER_OUT_OF_ROOM;
276 while (result == AWS_MQTT5_ER_OUT_OF_ROOM) {
277 struct aws_byte_buf fragment_dest;
278 aws_byte_buf_init(&fragment_dest, context->allocator, encode_fragment_size);
279
280 result = aws_mqtt5_encoder_encode_to_buffer(&encoder, &fragment_dest);
281 ASSERT_TRUE(result != AWS_MQTT5_ER_ERROR);
282
283 struct aws_byte_cursor fragment_cursor = aws_byte_cursor_from_buf(&fragment_dest);
284 ASSERT_SUCCESS(aws_byte_buf_append_dynamic(&whole_dest, &fragment_cursor));
285
286 aws_byte_buf_clean_up(&fragment_dest);
287 }
288
289 /*
290 * For packet types that support encoded size calculations (outgoing operations), verify that the encoded size
291 * calculation matches the length we encoded
292 */
293 size_t expected_packet_size = 0;
294 if (!aws_mqtt5_packet_view_get_encoded_size(context->packet_type, context->packet_view, &expected_packet_size)) {
295 ASSERT_INT_EQUALS(whole_dest.len, expected_packet_size);
296 }
297
298 ASSERT_SUCCESS(s_check_packet_encoding_reserved_flags(&whole_dest, context->packet_type));
299
300 ASSERT_INT_EQUALS(AWS_MQTT5_ER_FINISHED, result);
301
302 struct aws_mqtt5_decoder_options decoder_options = {
303 .on_packet_received = context->decoder_callback,
304 .callback_user_data = &tester,
305 .decoder_table = &tester.decoder_function_table,
306 };
307
308 struct aws_mqtt5_decoder decoder;
309 ASSERT_SUCCESS(aws_mqtt5_decoder_init(&decoder, context->allocator, &decoder_options));
310
311 struct aws_mqtt5_inbound_topic_alias_resolver inbound_alias_resolver;
312 ASSERT_SUCCESS(aws_mqtt5_inbound_topic_alias_resolver_init(&inbound_alias_resolver, context->allocator));
313 ASSERT_SUCCESS(aws_mqtt5_inbound_topic_alias_resolver_reset(&inbound_alias_resolver, 65535));
314 aws_mqtt5_decoder_set_inbound_topic_alias_resolver(&decoder, &inbound_alias_resolver);
315
316 struct aws_byte_cursor whole_cursor = aws_byte_cursor_from_buf(&whole_dest);
317 while (whole_cursor.len > 0) {
318 size_t advance = aws_min_size(whole_cursor.len, decode_fragment_size);
319 struct aws_byte_cursor fragment_cursor = aws_byte_cursor_advance(&whole_cursor, advance);
320
321 ASSERT_SUCCESS(aws_mqtt5_decoder_on_data_received(&decoder, fragment_cursor));
322 }
323
324 ASSERT_INT_EQUALS(1, tester.view_count);
325
326 /* Now do a check where we partially decode the buffer, reset, and then fully decode. */
327 aws_mqtt5_decoder_reset(&decoder);
328 tester.view_count = 0;
329
330 whole_cursor = aws_byte_cursor_from_buf(&whole_dest);
331 if (decode_fragment_size >= whole_cursor.len) {
332 whole_cursor.len--;
333 } else {
334 whole_cursor.len -= decode_fragment_size;
335 }
336
337 while (whole_cursor.len > 0) {
338 size_t advance = aws_min_size(whole_cursor.len, decode_fragment_size);
339 struct aws_byte_cursor fragment_cursor = aws_byte_cursor_advance(&whole_cursor, advance);
340
341 ASSERT_SUCCESS(aws_mqtt5_decoder_on_data_received(&decoder, fragment_cursor));
342 }
343
344 /* Nothing should have been received */
345 ASSERT_INT_EQUALS(0, tester.view_count);
346
347 /* Reset and decode the whole packet, everything should be fine */
348 aws_mqtt5_decoder_reset(&decoder);
349 whole_cursor = aws_byte_cursor_from_buf(&whole_dest);
350 while (whole_cursor.len > 0) {
351 size_t advance = aws_min_size(whole_cursor.len, decode_fragment_size);
352 struct aws_byte_cursor fragment_cursor = aws_byte_cursor_advance(&whole_cursor, advance);
353
354 ASSERT_SUCCESS(aws_mqtt5_decoder_on_data_received(&decoder, fragment_cursor));
355 }
356
357 ASSERT_INT_EQUALS(1, tester.view_count);
358
359 aws_byte_buf_clean_up(&whole_dest);
360 aws_mqtt5_inbound_topic_alias_resolver_clean_up(&inbound_alias_resolver);
361 aws_mqtt5_encoder_clean_up(&encoder);
362 aws_mqtt5_decoder_clean_up(&decoder);
363
364 return AWS_OP_SUCCESS;
365 }
366
367 static size_t s_encode_fragment_sizes[] = {4, 5, 7, 65536};
368 static size_t s_decode_fragment_sizes[] = {1, 2, 3, 11, 65536};
369
370 static int s_aws_mqtt5_encode_decode_round_trip_matrix_test(struct aws_mqtt5_packet_round_trip_test_context *context) {
371 for (size_t i = 0; i < AWS_ARRAY_SIZE(s_encode_fragment_sizes); ++i) {
372 size_t encode_fragment_size = s_encode_fragment_sizes[i];
373 for (size_t j = 0; j < AWS_ARRAY_SIZE(s_decode_fragment_sizes); ++j) {
374 size_t decode_fragment_size = s_decode_fragment_sizes[j];
375
376 ASSERT_SUCCESS(
377 s_aws_mqtt5_encode_decode_round_trip_test(context, encode_fragment_size, decode_fragment_size));
378 }
379 }
380
381 return AWS_OP_SUCCESS;
382 }
383
384 static int s_aws_mqtt5_on_disconnect_received_fn(enum aws_mqtt5_packet_type type, void *packet_view, void *user_data) {
385 struct aws_mqtt5_encode_decode_tester *tester = user_data;
386
387 ++tester->view_count;
388
389 ASSERT_INT_EQUALS((uint32_t)AWS_MQTT5_PT_DISCONNECT, (uint32_t)type);
390
391 struct aws_mqtt5_packet_disconnect_view *disconnect_view = packet_view;
392 struct aws_mqtt5_packet_disconnect_view *expected_view = tester->expected_views;
393
394 ASSERT_INT_EQUALS((uint32_t)expected_view->reason_code, (uint32_t)disconnect_view->reason_code);
395 ASSERT_INT_EQUALS(
396 *expected_view->session_expiry_interval_seconds, *disconnect_view->session_expiry_interval_seconds);
397 ASSERT_BIN_ARRAYS_EQUALS(
398 expected_view->reason_string->ptr,
399 expected_view->reason_string->len,
400 disconnect_view->reason_string->ptr,
401 disconnect_view->reason_string->len);
402 ASSERT_BIN_ARRAYS_EQUALS(
403 expected_view->server_reference->ptr,
404 expected_view->server_reference->len,
405 disconnect_view->server_reference->ptr,
406 disconnect_view->server_reference->len);
407 ASSERT_SUCCESS(aws_mqtt5_test_verify_user_properties_raw(
408 disconnect_view->user_property_count,
409 disconnect_view->user_properties,
410 expected_view->user_property_count,
411 expected_view->user_properties));
412
413 return AWS_OP_SUCCESS;
414 }
415
416 static int s_mqtt5_packet_disconnect_round_trip_fn(struct aws_allocator *allocator, void *ctx) {
417 (void)ctx;
418
419 uint32_t session_expiry_interval_seconds = 333;
420 struct aws_byte_cursor reason_string_cursor = aws_byte_cursor_from_c_str(s_reason_string);
421 struct aws_byte_cursor server_reference_cursor = aws_byte_cursor_from_c_str(s_server_reference);
422
423 struct aws_mqtt5_packet_disconnect_view disconnect_view = {
424 .reason_code = AWS_MQTT5_DRC_DISCONNECT_WITH_WILL_MESSAGE,
425 .session_expiry_interval_seconds = &session_expiry_interval_seconds,
426 .reason_string = &reason_string_cursor,
427 .user_property_count = AWS_ARRAY_SIZE(s_user_properties),
428 .user_properties = &s_user_properties[0],
429 .server_reference = &server_reference_cursor,
430 };
431
432 struct aws_mqtt5_packet_round_trip_test_context context = {
433 .allocator = allocator,
434 .packet_type = AWS_MQTT5_PT_DISCONNECT,
435 .packet_view = &disconnect_view,
436 .decoder_callback = s_aws_mqtt5_on_disconnect_received_fn,
437 };
438 ASSERT_SUCCESS(s_aws_mqtt5_encode_decode_round_trip_matrix_test(&context));
439
440 return AWS_OP_SUCCESS;
441 }
442
443 AWS_TEST_CASE(mqtt5_packet_disconnect_round_trip, s_mqtt5_packet_disconnect_round_trip_fn)
444
445 static int s_aws_mqtt5_on_pingreq_received_fn(enum aws_mqtt5_packet_type type, void *packet_view, void *user_data) {
446 struct aws_mqtt5_encode_decode_tester *tester = user_data;
447
448 ++tester->view_count;
449
450 ASSERT_INT_EQUALS((uint32_t)AWS_MQTT5_PT_PINGREQ, (uint32_t)type);
451 ASSERT_NULL(packet_view);
452
453 return AWS_OP_SUCCESS;
454 }
455
456 static int s_mqtt5_packet_pingreq_round_trip_fn(struct aws_allocator *allocator, void *ctx) {
457 (void)ctx;
458
459 struct aws_mqtt5_packet_round_trip_test_context context = {
460 .allocator = allocator,
461 .packet_type = AWS_MQTT5_PT_PINGREQ,
462 .packet_view = NULL,
463 .decoder_callback = s_aws_mqtt5_on_pingreq_received_fn,
464 };
465 ASSERT_SUCCESS(s_aws_mqtt5_encode_decode_round_trip_matrix_test(&context));
466
467 return AWS_OP_SUCCESS;
468 }
469
470 AWS_TEST_CASE(mqtt5_packet_pingreq_round_trip, s_mqtt5_packet_pingreq_round_trip_fn)
471
472 static int s_aws_mqtt5_on_pingresp_received_fn(enum aws_mqtt5_packet_type type, void *packet_view, void *user_data) {
473 struct aws_mqtt5_encode_decode_tester *tester = user_data;
474
475 ++tester->view_count;
476
477 ASSERT_INT_EQUALS((uint32_t)AWS_MQTT5_PT_PINGRESP, (uint32_t)type);
478 ASSERT_NULL(packet_view);
479
480 return AWS_OP_SUCCESS;
481 }
482
483 static int s_mqtt5_packet_pingresp_round_trip_fn(struct aws_allocator *allocator, void *ctx) {
484 (void)ctx;
485
486 struct aws_mqtt5_packet_round_trip_test_context context = {
487 .allocator = allocator,
488 .packet_type = AWS_MQTT5_PT_PINGRESP,
489 .packet_view = NULL,
490 .decoder_callback = s_aws_mqtt5_on_pingresp_received_fn,
491 };
492
493 ASSERT_SUCCESS(s_aws_mqtt5_encode_decode_round_trip_matrix_test(&context));
494
495 return AWS_OP_SUCCESS;
496 }
497
498 AWS_TEST_CASE(mqtt5_packet_pingresp_round_trip, s_mqtt5_packet_pingresp_round_trip_fn)
499
500 static int s_aws_mqtt5_on_connect_received_fn(enum aws_mqtt5_packet_type type, void *packet_view, void *user_data) {
501 struct aws_mqtt5_encode_decode_tester *tester = user_data;
502
503 ++tester->view_count;
504
505 ASSERT_INT_EQUALS((uint32_t)AWS_MQTT5_PT_CONNECT, (uint32_t)type);
506
507 struct aws_mqtt5_packet_connect_view *connect_view = packet_view;
508 struct aws_mqtt5_packet_connect_view *expected_view = tester->expected_views;
509
510 ASSERT_INT_EQUALS(expected_view->keep_alive_interval_seconds, connect_view->keep_alive_interval_seconds);
511 ASSERT_BIN_ARRAYS_EQUALS(
512 expected_view->client_id.ptr,
513 expected_view->client_id.len,
514 connect_view->client_id.ptr,
515 connect_view->client_id.len);
516 ASSERT_BIN_ARRAYS_EQUALS(
517 expected_view->username->ptr,
518 expected_view->username->len,
519 connect_view->username->ptr,
520 connect_view->username->len);
521 ASSERT_BIN_ARRAYS_EQUALS(
522 expected_view->password->ptr,
523 expected_view->password->len,
524 connect_view->password->ptr,
525 connect_view->password->len);
526 ASSERT_TRUE(expected_view->clean_start == connect_view->clean_start);
527 ASSERT_INT_EQUALS(*expected_view->session_expiry_interval_seconds, *connect_view->session_expiry_interval_seconds);
528
529 ASSERT_INT_EQUALS(*expected_view->request_response_information, *connect_view->request_response_information);
530 ASSERT_INT_EQUALS(*expected_view->request_problem_information, *connect_view->request_problem_information);
531 ASSERT_INT_EQUALS(*expected_view->receive_maximum, *connect_view->receive_maximum);
532 ASSERT_INT_EQUALS(*expected_view->topic_alias_maximum, *connect_view->topic_alias_maximum);
533 ASSERT_INT_EQUALS(*expected_view->maximum_packet_size_bytes, *connect_view->maximum_packet_size_bytes);
534
535 ASSERT_INT_EQUALS(*expected_view->will_delay_interval_seconds, *connect_view->will_delay_interval_seconds);
536
537 const struct aws_mqtt5_packet_publish_view *expected_will_view = expected_view->will;
538 const struct aws_mqtt5_packet_publish_view *will_view = connect_view->will;
539
540 ASSERT_BIN_ARRAYS_EQUALS(
541 expected_will_view->payload.ptr,
542 expected_will_view->payload.len,
543 will_view->payload.ptr,
544 will_view->payload.len);
545 ASSERT_INT_EQUALS(expected_will_view->qos, will_view->qos);
546 ASSERT_INT_EQUALS(expected_will_view->retain, will_view->retain);
547 ASSERT_BIN_ARRAYS_EQUALS(
548 expected_will_view->topic.ptr, expected_will_view->topic.len, will_view->topic.ptr, will_view->topic.len);
549 ASSERT_INT_EQUALS(*expected_will_view->payload_format, *will_view->payload_format);
550 ASSERT_INT_EQUALS(
551 *expected_will_view->message_expiry_interval_seconds, *will_view->message_expiry_interval_seconds);
552 ASSERT_BIN_ARRAYS_EQUALS(
553 expected_will_view->response_topic->ptr,
554 expected_will_view->response_topic->len,
555 will_view->response_topic->ptr,
556 will_view->response_topic->len);
557 ASSERT_BIN_ARRAYS_EQUALS(
558 expected_will_view->correlation_data->ptr,
559 expected_will_view->correlation_data->len,
560 will_view->correlation_data->ptr,
561 will_view->correlation_data->len);
562 ASSERT_BIN_ARRAYS_EQUALS(
563 expected_will_view->content_type->ptr,
564 expected_will_view->content_type->len,
565 will_view->content_type->ptr,
566 will_view->content_type->len);
567
568 ASSERT_SUCCESS(aws_mqtt5_test_verify_user_properties_raw(
569 expected_will_view->user_property_count,
570 expected_will_view->user_properties,
571 will_view->user_property_count,
572 will_view->user_properties));
573
574 ASSERT_SUCCESS(aws_mqtt5_test_verify_user_properties_raw(
575 expected_view->user_property_count,
576 expected_view->user_properties,
577 connect_view->user_property_count,
578 connect_view->user_properties));
579
580 ASSERT_BIN_ARRAYS_EQUALS(
581 expected_view->authentication_method->ptr,
582 expected_view->authentication_method->len,
583 connect_view->authentication_method->ptr,
584 connect_view->authentication_method->len);
585 ASSERT_BIN_ARRAYS_EQUALS(
586 expected_view->authentication_data->ptr,
587 expected_view->authentication_data->len,
588 connect_view->authentication_data->ptr,
589 connect_view->authentication_data->len);
590
591 return AWS_OP_SUCCESS;
592 }
593
594 static int s_mqtt5_packet_encode_connect_to_buffer(
595 struct aws_allocator *allocator,
596 struct aws_mqtt5_packet_connect_view *connect_view,
597 struct aws_byte_buf *buffer) {
598 AWS_ZERO_STRUCT(*buffer);
599 aws_byte_buf_init(buffer, allocator, 4096);
600
601 struct aws_mqtt5_encoder_options encoder_options;
602 AWS_ZERO_STRUCT(encoder_options);
603
604 struct aws_mqtt5_encoder encoder;
605 ASSERT_SUCCESS(aws_mqtt5_encoder_init(&encoder, allocator, &encoder_options));
606 ASSERT_SUCCESS(aws_mqtt5_encoder_append_packet_encoding(&encoder, AWS_MQTT5_PT_CONNECT, connect_view));
607
608 enum aws_mqtt5_encoding_result result = aws_mqtt5_encoder_encode_to_buffer(&encoder, buffer);
609 ASSERT_INT_EQUALS(AWS_MQTT5_ER_FINISHED, result);
610
611 aws_mqtt5_encoder_clean_up(&encoder);
612
613 return AWS_OP_SUCCESS;
614 }
615
616 static int s_mqtt5_packet_connect_round_trip_fn(struct aws_allocator *allocator, void *ctx) {
617 (void)ctx;
618
619 struct aws_byte_cursor will_payload_cursor = aws_byte_cursor_from_c_str(s_will_payload);
620 enum aws_mqtt5_payload_format_indicator payload_format = AWS_MQTT5_PFI_UTF8;
621 uint32_t message_expiry_interval_seconds = 65537;
622 struct aws_byte_cursor will_response_topic = aws_byte_cursor_from_c_str(s_will_response_topic);
623 struct aws_byte_cursor will_correlation_data = aws_byte_cursor_from_c_str(s_will_correlation_data);
624 struct aws_byte_cursor will_content_type = aws_byte_cursor_from_c_str(s_will_content_type);
625 struct aws_byte_cursor username = aws_byte_cursor_from_c_str(s_username);
626 struct aws_byte_cursor password = aws_byte_cursor_from_c_str(s_password);
627 uint32_t session_expiry_interval_seconds = 3600;
628 uint8_t request_response_information = 1;
629 uint8_t request_problem_information = 1;
630 uint16_t receive_maximum = 50;
631 uint16_t topic_alias_maximum = 16;
632 uint32_t maximum_packet_size_bytes = 1ULL << 24;
633 uint32_t will_delay_interval_seconds = 30;
634 struct aws_byte_cursor authentication_method = aws_byte_cursor_from_c_str("AuthMethod");
635 struct aws_byte_cursor authentication_data = aws_byte_cursor_from_c_str("SuperSecret");
636
637 struct aws_mqtt5_packet_publish_view will_view = {
638 .payload = will_payload_cursor,
639 .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE,
640 .retain = true,
641 .topic = aws_byte_cursor_from_c_str(s_will_topic),
642 .payload_format = &payload_format,
643 .message_expiry_interval_seconds = &message_expiry_interval_seconds,
644 .response_topic = &will_response_topic,
645 .correlation_data = &will_correlation_data,
646 .content_type = &will_content_type,
647 .user_property_count = AWS_ARRAY_SIZE(s_user_properties),
648 .user_properties = &s_user_properties[0],
649 };
650
651 struct aws_mqtt5_packet_connect_view connect_view = {
652 .keep_alive_interval_seconds = 1200,
653 .client_id = aws_byte_cursor_from_c_str(s_client_id),
654 .username = &username,
655 .password = &password,
656 .clean_start = true,
657 .session_expiry_interval_seconds = &session_expiry_interval_seconds,
658 .request_response_information = &request_response_information,
659 .request_problem_information = &request_problem_information,
660 .receive_maximum = &receive_maximum,
661 .topic_alias_maximum = &topic_alias_maximum,
662 .maximum_packet_size_bytes = &maximum_packet_size_bytes,
663 .will_delay_interval_seconds = &will_delay_interval_seconds,
664 .will = &will_view,
665 .user_property_count = AWS_ARRAY_SIZE(s_user_properties),
666 .user_properties = &s_user_properties[0],
667 .authentication_method = &authentication_method,
668 .authentication_data = &authentication_data,
669 };
670
671 struct aws_mqtt5_packet_round_trip_test_context context = {
672 .allocator = allocator,
673 .packet_type = AWS_MQTT5_PT_CONNECT,
674 .packet_view = &connect_view,
675 .decoder_callback = s_aws_mqtt5_on_connect_received_fn,
676 };
677
678 ASSERT_SUCCESS(s_aws_mqtt5_encode_decode_round_trip_matrix_test(&context));
679
680 struct aws_byte_buf encoding_buffer;
681 ASSERT_SUCCESS(s_mqtt5_packet_encode_connect_to_buffer(allocator, &connect_view, &encoding_buffer));
682
683 /*
684 * For this packet: 1 byte packet type + flags, 2 bytes vli remaining length + 7 bytes protocol prefix
685 * (0x00, 0x04, "MQTT", 0x05), then we're at the CONNECT flags which we want to check
686 */
687 size_t connect_flags_byte_index = 10;
688 uint8_t connect_flags = encoding_buffer.buffer[connect_flags_byte_index];
689
690 /*
691 * Verify Will flag (0x04), Will QoS (0x08), Will Retain (0x20), Username (0x80),
692 * clean start (0x02), password (0x40) flags are set
693 */
694 ASSERT_INT_EQUALS(connect_flags, 0xEE);
695
696 aws_byte_buf_clean_up(&encoding_buffer);
697
698 return AWS_OP_SUCCESS;
699 }
700
701 AWS_TEST_CASE(mqtt5_packet_connect_round_trip, s_mqtt5_packet_connect_round_trip_fn)
702
703 static int s_aws_mqtt5_on_connack_received_fn(enum aws_mqtt5_packet_type type, void *packet_view, void *user_data) {
704 struct aws_mqtt5_encode_decode_tester *tester = user_data;
705
706 ++tester->view_count;
707
708 ASSERT_INT_EQUALS((uint32_t)AWS_MQTT5_PT_CONNACK, (uint32_t)type);
709
710 struct aws_mqtt5_packet_connack_view *connack_view = packet_view;
711 struct aws_mqtt5_packet_connack_view *expected_view = tester->expected_views;
712
713 ASSERT_INT_EQUALS(expected_view->session_present, connack_view->session_present);
714 ASSERT_INT_EQUALS(expected_view->reason_code, connack_view->reason_code);
715 ASSERT_INT_EQUALS(*expected_view->session_expiry_interval, *connack_view->session_expiry_interval);
716 ASSERT_INT_EQUALS(*expected_view->receive_maximum, *connack_view->receive_maximum);
717 ASSERT_INT_EQUALS(*expected_view->maximum_qos, *connack_view->maximum_qos);
718 ASSERT_INT_EQUALS(*expected_view->retain_available, *connack_view->retain_available);
719 ASSERT_INT_EQUALS(*expected_view->maximum_packet_size, *connack_view->maximum_packet_size);
720 ASSERT_BIN_ARRAYS_EQUALS(
721 expected_view->assigned_client_identifier->ptr,
722 expected_view->assigned_client_identifier->len,
723 connack_view->assigned_client_identifier->ptr,
724 connack_view->assigned_client_identifier->len);
725 ASSERT_INT_EQUALS(*expected_view->topic_alias_maximum, *connack_view->topic_alias_maximum);
726 ASSERT_BIN_ARRAYS_EQUALS(
727 expected_view->reason_string->ptr,
728 expected_view->reason_string->len,
729 connack_view->reason_string->ptr,
730 connack_view->reason_string->len);
731
732 ASSERT_SUCCESS(aws_mqtt5_test_verify_user_properties_raw(
733 expected_view->user_property_count,
734 expected_view->user_properties,
735 connack_view->user_property_count,
736 connack_view->user_properties));
737
738 ASSERT_INT_EQUALS(
739 *expected_view->wildcard_subscriptions_available, *connack_view->wildcard_subscriptions_available);
740 ASSERT_INT_EQUALS(
741 *expected_view->subscription_identifiers_available, *connack_view->subscription_identifiers_available);
742 ASSERT_INT_EQUALS(*expected_view->shared_subscriptions_available, *connack_view->shared_subscriptions_available);
743 ASSERT_INT_EQUALS(*expected_view->server_keep_alive, *connack_view->server_keep_alive);
744
745 ASSERT_BIN_ARRAYS_EQUALS(
746 expected_view->response_information->ptr,
747 expected_view->response_information->len,
748 connack_view->response_information->ptr,
749 connack_view->response_information->len);
750 ASSERT_BIN_ARRAYS_EQUALS(
751 expected_view->server_reference->ptr,
752 expected_view->server_reference->len,
753 connack_view->server_reference->ptr,
754 connack_view->server_reference->len);
755 ASSERT_BIN_ARRAYS_EQUALS(
756 expected_view->authentication_method->ptr,
757 expected_view->authentication_method->len,
758 connack_view->authentication_method->ptr,
759 connack_view->authentication_method->len);
760 ASSERT_BIN_ARRAYS_EQUALS(
761 expected_view->authentication_data->ptr,
762 expected_view->authentication_data->len,
763 connack_view->authentication_data->ptr,
764 connack_view->authentication_data->len);
765
766 return AWS_OP_SUCCESS;
767 }
768
769 static int s_mqtt5_packet_connack_round_trip_fn(struct aws_allocator *allocator, void *ctx) {
770 (void)ctx;
771
772 uint32_t session_expiry_interval = 3600;
773 uint16_t receive_maximum = 20;
774 enum aws_mqtt5_qos maximum_qos = AWS_MQTT5_QOS_AT_LEAST_ONCE;
775 bool retain_available = true;
776 uint32_t maximum_packet_size = 1U << 18;
777 struct aws_byte_cursor assigned_client_identifier = aws_byte_cursor_from_c_str("server-assigned-client-id-01");
778 uint16_t topic_alias_maximum = 10;
779 struct aws_byte_cursor reason_string = aws_byte_cursor_from_c_str("You've been banned from this server");
780 bool wildcard_subscriptions_available = true;
781 bool subscription_identifiers_available = true;
782 bool shared_subscriptions_available = true;
783 uint16_t server_keep_alive = 1200;
784 struct aws_byte_cursor response_information = aws_byte_cursor_from_c_str("some/topic-prefix");
785 struct aws_byte_cursor server_reference = aws_byte_cursor_from_c_str("www.somewhere-else.com:1883");
786 struct aws_byte_cursor authentication_method = aws_byte_cursor_from_c_str("GSSAPI");
787 struct aws_byte_cursor authentication_data = aws_byte_cursor_from_c_str("TOP-SECRET");
788
789 struct aws_mqtt5_packet_connack_view connack_view = {
790 .session_present = true,
791 .reason_code = AWS_MQTT5_CRC_BANNED,
792 .session_expiry_interval = &session_expiry_interval,
793 .receive_maximum = &receive_maximum,
794 .maximum_qos = &maximum_qos,
795 .retain_available = &retain_available,
796 .maximum_packet_size = &maximum_packet_size,
797 .assigned_client_identifier = &assigned_client_identifier,
798 .topic_alias_maximum = &topic_alias_maximum,
799 .reason_string = &reason_string,
800 .user_property_count = AWS_ARRAY_SIZE(s_user_properties),
801 .user_properties = &s_user_properties[0],
802 .wildcard_subscriptions_available = &wildcard_subscriptions_available,
803 .subscription_identifiers_available = &subscription_identifiers_available,
804 .shared_subscriptions_available = &shared_subscriptions_available,
805 .server_keep_alive = &server_keep_alive,
806 .response_information = &response_information,
807 .server_reference = &server_reference,
808 .authentication_method = &authentication_method,
809 .authentication_data = &authentication_data,
810 };
811
812 struct aws_mqtt5_packet_round_trip_test_context context = {
813 .allocator = allocator,
814 .packet_type = AWS_MQTT5_PT_CONNACK,
815 .packet_view = &connack_view,
816 .decoder_callback = s_aws_mqtt5_on_connack_received_fn,
817 };
818
819 ASSERT_SUCCESS(s_aws_mqtt5_encode_decode_round_trip_matrix_test(&context));
820
821 return AWS_OP_SUCCESS;
822 }
823
824 AWS_TEST_CASE(mqtt5_packet_connack_round_trip, s_mqtt5_packet_connack_round_trip_fn)
825
826 static char s_subscription_topic_1[] = "test_topic_1";
827 static char s_subscription_topic_2[] = "test_topic_2";
828 static char s_subscription_topic_3[] = "test_topic_3";
829
830 static const struct aws_mqtt5_subscription_view s_subscriptions[] = {
831 {.topic_filter =
832 {
833 .ptr = (uint8_t *)s_subscription_topic_1,
834 .len = AWS_ARRAY_SIZE(s_subscription_topic_1) - 1,
835 },
836 .qos = AWS_MQTT5_QOS_AT_MOST_ONCE,
837 .no_local = true,
838 .retain_as_published = true,
839 .retain_handling_type = AWS_MQTT5_RHT_DONT_SEND},
840 {.topic_filter =
841 {
842 .ptr = (uint8_t *)s_subscription_topic_2,
843 .len = AWS_ARRAY_SIZE(s_subscription_topic_2) - 1,
844 },
845 .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE,
846 .no_local = false,
847 .retain_as_published = true,
848 .retain_handling_type = AWS_MQTT5_RHT_SEND_ON_SUBSCRIBE},
849 {.topic_filter =
850 {
851 .ptr = (uint8_t *)s_subscription_topic_3,
852 .len = AWS_ARRAY_SIZE(s_subscription_topic_3) - 1,
853 },
854 .qos = AWS_MQTT5_QOS_AT_MOST_ONCE,
855 .no_local = false,
856 .retain_as_published = true,
857 .retain_handling_type = AWS_MQTT5_RHT_SEND_ON_SUBSCRIBE_IF_NEW},
858 };
859
860 int aws_mqtt5_test_verify_subscriptions_raw(
861 size_t subscription_count,
862 const struct aws_mqtt5_subscription_view *subscriptions,
863 size_t expected_count,
864 const struct aws_mqtt5_subscription_view *expected_subscriptions) {
865
866 ASSERT_UINT_EQUALS(expected_count, subscription_count);
867
868 for (size_t i = 0; i < expected_count; ++i) {
869 const struct aws_mqtt5_subscription_view *expected_subscription = &expected_subscriptions[i];
870 ASSERT_BIN_ARRAYS_EQUALS(
871 expected_subscription->topic_filter.ptr,
872 expected_subscription->topic_filter.len,
873 subscriptions[i].topic_filter.ptr,
874 subscriptions[i].topic_filter.len);
875 ASSERT_INT_EQUALS(expected_subscription->qos, subscriptions[i].qos);
876 ASSERT_INT_EQUALS(expected_subscription->no_local, subscriptions[i].no_local);
877 ASSERT_INT_EQUALS(expected_subscription->retain_as_published, subscriptions[i].retain_as_published);
878 ASSERT_INT_EQUALS(expected_subscription->retain_handling_type, subscriptions[i].retain_handling_type);
879 }
880
881 return AWS_OP_SUCCESS;
882 }
883
884 static int s_aws_mqtt5_on_subscribe_received_fn(enum aws_mqtt5_packet_type type, void *packet_view, void *user_data) {
885 (void)type;
886
887 struct aws_mqtt5_encode_decode_tester *tester = user_data;
888
889 ++tester->view_count;
890
891 struct aws_mqtt5_packet_subscribe_view *subscribe_view = packet_view;
892 struct aws_mqtt5_packet_subscribe_view *expected_view = tester->expected_views;
893
894 ASSERT_INT_EQUALS(expected_view->packet_id, subscribe_view->packet_id);
895 ASSERT_SUCCESS(aws_mqtt5_test_verify_subscriptions_raw(
896 expected_view->subscription_count,
897 expected_view->subscriptions,
898 subscribe_view->subscription_count,
899 subscribe_view->subscriptions));
900
901 ASSERT_INT_EQUALS(*expected_view->subscription_identifier, *subscribe_view->subscription_identifier);
902
903 ASSERT_SUCCESS(aws_mqtt5_test_verify_user_properties_raw(
904 expected_view->user_property_count,
905 expected_view->user_properties,
906 subscribe_view->user_property_count,
907 subscribe_view->user_properties));
908
909 return AWS_OP_SUCCESS;
910 }
911
912 static int s_mqtt5_packet_subscribe_round_trip_fn(struct aws_allocator *allocator, void *ctx) {
913 (void)ctx;
914
915 aws_mqtt5_packet_id_t packet_id = 47;
916 uint32_t subscription_identifier = 1;
917
918 struct aws_mqtt5_packet_subscribe_view subscribe_view = {
919 .packet_id = packet_id,
920 .subscription_count = AWS_ARRAY_SIZE(s_subscriptions),
921 .subscriptions = &s_subscriptions[0],
922 .subscription_identifier = &subscription_identifier,
923 .user_property_count = AWS_ARRAY_SIZE(s_user_properties),
924 .user_properties = &s_user_properties[0],
925 };
926
927 struct aws_mqtt5_packet_round_trip_test_context context = {
928 .allocator = allocator,
929 .packet_type = AWS_MQTT5_PT_SUBSCRIBE,
930 .packet_view = &subscribe_view,
931 .decoder_callback = s_aws_mqtt5_on_subscribe_received_fn,
932 };
933
934 ASSERT_SUCCESS(s_aws_mqtt5_encode_decode_round_trip_matrix_test(&context));
935
936 return AWS_OP_SUCCESS;
937 }
938
939 AWS_TEST_CASE(mqtt5_packet_subscribe_round_trip, s_mqtt5_packet_subscribe_round_trip_fn)
940
941 static int s_aws_mqtt5_on_suback_received_fn(enum aws_mqtt5_packet_type type, void *packet_view, void *user_data) {
942 struct aws_mqtt5_encode_decode_tester *tester = user_data;
943
944 ++tester->view_count;
945
946 ASSERT_INT_EQUALS((uint32_t)AWS_MQTT5_PT_SUBACK, (uint32_t)type);
947
948 struct aws_mqtt5_packet_suback_view *suback_view = packet_view;
949 struct aws_mqtt5_packet_suback_view *expected_view = tester->expected_views;
950
951 ASSERT_INT_EQUALS(expected_view->packet_id, suback_view->packet_id);
952
953 ASSERT_BIN_ARRAYS_EQUALS(
954 expected_view->reason_string->ptr,
955 expected_view->reason_string->len,
956 suback_view->reason_string->ptr,
957 suback_view->reason_string->len);
958
959 ASSERT_SUCCESS(aws_mqtt5_test_verify_user_properties_raw(
960 expected_view->user_property_count,
961 expected_view->user_properties,
962 suback_view->user_property_count,
963 suback_view->user_properties));
964
965 ASSERT_INT_EQUALS(expected_view->reason_code_count, suback_view->reason_code_count);
966 for (size_t i = 0; i < suback_view->reason_code_count; ++i) {
967 ASSERT_INT_EQUALS(expected_view->reason_codes[i], suback_view->reason_codes[i]);
968 }
969 return AWS_OP_SUCCESS;
970 }
971
972 static int s_mqtt5_packet_suback_round_trip_fn(struct aws_allocator *allocator, void *ctx) {
973 (void)ctx;
974
975 aws_mqtt5_packet_id_t packet_id = 47;
976 struct aws_byte_cursor reason_string = aws_byte_cursor_from_c_str("Some random reason string");
977 enum aws_mqtt5_suback_reason_code reason_code_1 = AWS_MQTT5_SARC_GRANTED_QOS_0;
978 enum aws_mqtt5_suback_reason_code reason_code_2 = AWS_MQTT5_SARC_GRANTED_QOS_1;
979 enum aws_mqtt5_suback_reason_code reason_code_3 = AWS_MQTT5_SARC_GRANTED_QOS_2;
980 const enum aws_mqtt5_suback_reason_code reason_codes[3] = {
981 reason_code_1,
982 reason_code_2,
983 reason_code_3,
984 };
985
986 struct aws_mqtt5_packet_suback_view suback_view = {
987 .packet_id = packet_id,
988 .reason_string = &reason_string,
989 .user_property_count = AWS_ARRAY_SIZE(s_user_properties),
990 .user_properties = &s_user_properties[0],
991 .reason_code_count = 3,
992 .reason_codes = &reason_codes[0],
993 };
994
995 struct aws_mqtt5_packet_round_trip_test_context context = {
996 .allocator = allocator,
997 .packet_type = AWS_MQTT5_PT_SUBACK,
998 .packet_view = &suback_view,
999 .decoder_callback = s_aws_mqtt5_on_suback_received_fn,
1000 };
1001
1002 ASSERT_SUCCESS(s_aws_mqtt5_encode_decode_round_trip_matrix_test(&context));
1003
1004 return AWS_OP_SUCCESS;
1005 }
1006
1007 AWS_TEST_CASE(mqtt5_packet_suback_round_trip, s_mqtt5_packet_suback_round_trip_fn)
1008
1009 static char s_unsubscribe_topic_1[] = "test_topic_1";
1010 static char s_unsubscribe_topic_2[] = "test_topic_2";
1011 static char s_unsubscribe_topic_3[] = "test_topic_3";
1012
1013 static const struct aws_byte_cursor s_unsubscribe_topics[] = {
1014 {
1015 .ptr = (uint8_t *)s_unsubscribe_topic_1,
1016 .len = AWS_ARRAY_SIZE(s_unsubscribe_topic_1) - 1,
1017 },
1018 {
1019 .ptr = (uint8_t *)s_unsubscribe_topic_2,
1020 .len = AWS_ARRAY_SIZE(s_unsubscribe_topic_2) - 1,
1021 },
1022 {
1023 .ptr = (uint8_t *)s_unsubscribe_topic_3,
1024 .len = AWS_ARRAY_SIZE(s_unsubscribe_topic_3) - 1,
1025 },
1026 };
1027
1028 static int s_aws_mqtt5_on_unsubscribe_received_fn(enum aws_mqtt5_packet_type type, void *packet_view, void *user_data) {
1029 (void)type;
1030
1031 struct aws_mqtt5_encode_decode_tester *tester = user_data;
1032
1033 ++tester->view_count;
1034
1035 struct aws_mqtt5_packet_unsubscribe_view *unsubscribe_view = packet_view;
1036 struct aws_mqtt5_packet_unsubscribe_view *expected_view = tester->expected_views;
1037
1038 ASSERT_INT_EQUALS(expected_view->packet_id, unsubscribe_view->packet_id);
1039
1040 ASSERT_INT_EQUALS(expected_view->topic_filter_count, unsubscribe_view->topic_filter_count);
1041
1042 for (size_t i = 0; i < unsubscribe_view->topic_filter_count; ++i) {
1043 const struct aws_byte_cursor unsubscribe_topic = unsubscribe_view->topic_filters[i];
1044 const struct aws_byte_cursor expected_topic = expected_view->topic_filters[i];
1045 ASSERT_BIN_ARRAYS_EQUALS(expected_topic.ptr, expected_topic.len, unsubscribe_topic.ptr, unsubscribe_topic.len);
1046 }
1047
1048 ASSERT_SUCCESS(aws_mqtt5_test_verify_user_properties_raw(
1049 expected_view->user_property_count,
1050 expected_view->user_properties,
1051 unsubscribe_view->user_property_count,
1052 unsubscribe_view->user_properties));
1053
1054 return AWS_OP_SUCCESS;
1055 }
1056
1057 static int s_mqtt5_packet_unsubscribe_round_trip_fn(struct aws_allocator *allocator, void *ctx) {
1058 (void)ctx;
1059
1060 aws_mqtt5_packet_id_t packet_id = 47;
1061
1062 struct aws_mqtt5_packet_unsubscribe_view unsubscribe_view = {
1063 .packet_id = packet_id,
1064 .topic_filter_count = AWS_ARRAY_SIZE(s_unsubscribe_topics),
1065 .topic_filters = &s_unsubscribe_topics[0],
1066 .user_property_count = AWS_ARRAY_SIZE(s_user_properties),
1067 .user_properties = &s_user_properties[0],
1068 };
1069
1070 struct aws_mqtt5_packet_round_trip_test_context context = {
1071 .allocator = allocator,
1072 .packet_type = AWS_MQTT5_PT_UNSUBSCRIBE,
1073 .packet_view = &unsubscribe_view,
1074 .decoder_callback = s_aws_mqtt5_on_unsubscribe_received_fn,
1075 };
1076
1077 ASSERT_SUCCESS(s_aws_mqtt5_encode_decode_round_trip_matrix_test(&context));
1078
1079 return AWS_OP_SUCCESS;
1080 }
1081
1082 AWS_TEST_CASE(mqtt5_packet_unsubscribe_round_trip, s_mqtt5_packet_unsubscribe_round_trip_fn)
1083
1084 static int s_aws_mqtt5_on_unsuback_received_fn(enum aws_mqtt5_packet_type type, void *packet_view, void *user_data) {
1085 struct aws_mqtt5_encode_decode_tester *tester = user_data;
1086
1087 ++tester->view_count;
1088
1089 ASSERT_INT_EQUALS((uint32_t)AWS_MQTT5_PT_UNSUBACK, (uint32_t)type);
1090
1091 struct aws_mqtt5_packet_unsuback_view *unsuback_view = packet_view;
1092 struct aws_mqtt5_packet_unsuback_view *expected_view = tester->expected_views;
1093
1094 ASSERT_INT_EQUALS(expected_view->packet_id, unsuback_view->packet_id);
1095
1096 ASSERT_BIN_ARRAYS_EQUALS(
1097 expected_view->reason_string->ptr,
1098 expected_view->reason_string->len,
1099 unsuback_view->reason_string->ptr,
1100 unsuback_view->reason_string->len);
1101
1102 ASSERT_SUCCESS(aws_mqtt5_test_verify_user_properties_raw(
1103 expected_view->user_property_count,
1104 expected_view->user_properties,
1105 unsuback_view->user_property_count,
1106 unsuback_view->user_properties));
1107
1108 ASSERT_INT_EQUALS(expected_view->reason_code_count, unsuback_view->reason_code_count);
1109 for (size_t i = 0; i < unsuback_view->reason_code_count; ++i) {
1110 ASSERT_INT_EQUALS(expected_view->reason_codes[i], unsuback_view->reason_codes[i]);
1111 }
1112 return AWS_OP_SUCCESS;
1113 }
1114
1115 static int s_mqtt5_packet_unsuback_round_trip_fn(struct aws_allocator *allocator, void *ctx) {
1116 (void)ctx;
1117
1118 aws_mqtt5_packet_id_t packet_id = 47;
1119 struct aws_byte_cursor reason_string = aws_byte_cursor_from_c_str("Some random reason string");
1120 enum aws_mqtt5_unsuback_reason_code reason_code_1 = AWS_MQTT5_UARC_SUCCESS;
1121 enum aws_mqtt5_unsuback_reason_code reason_code_2 = AWS_MQTT5_UARC_NO_SUBSCRIPTION_EXISTED;
1122 enum aws_mqtt5_unsuback_reason_code reason_code_3 = AWS_MQTT5_UARC_UNSPECIFIED_ERROR;
1123 const enum aws_mqtt5_unsuback_reason_code reason_codes[3] = {
1124 reason_code_1,
1125 reason_code_2,
1126 reason_code_3,
1127 };
1128
1129 struct aws_mqtt5_packet_unsuback_view unsuback_view = {
1130 .packet_id = packet_id,
1131 .reason_string = &reason_string,
1132 .user_property_count = AWS_ARRAY_SIZE(s_user_properties),
1133 .user_properties = &s_user_properties[0],
1134 .reason_code_count = AWS_ARRAY_SIZE(reason_codes),
1135 .reason_codes = &reason_codes[0],
1136 };
1137
1138 struct aws_mqtt5_packet_round_trip_test_context context = {
1139 .allocator = allocator,
1140 .packet_type = AWS_MQTT5_PT_UNSUBACK,
1141 .packet_view = &unsuback_view,
1142 .decoder_callback = s_aws_mqtt5_on_unsuback_received_fn,
1143 };
1144
1145 ASSERT_SUCCESS(s_aws_mqtt5_encode_decode_round_trip_matrix_test(&context));
1146
1147 return AWS_OP_SUCCESS;
1148 }
1149
1150 AWS_TEST_CASE(mqtt5_packet_unsuback_round_trip, s_mqtt5_packet_unsuback_round_trip_fn)
1151
1152 static char s_publish_topic[] = "test_topic";
1153 static char s_publish_payload[] = "test payload contents";
1154 static char s_publish_response_topic[] = "test response topic";
1155 static char s_publish_correlation_data[] = "test correlation data";
1156 static char s_publish_content_type[] = "test content type";
1157
1158 static int s_aws_mqtt5_on_publish_received_fn(enum aws_mqtt5_packet_type type, void *packet_view, void *user_data) {
1159 (void)type;
1160
1161 struct aws_mqtt5_encode_decode_tester *tester = user_data;
1162
1163 ++tester->view_count;
1164
1165 struct aws_mqtt5_packet_publish_view *publish_view = packet_view;
1166 struct aws_mqtt5_packet_publish_view *expected_view = tester->expected_views;
1167
1168 ASSERT_BIN_ARRAYS_EQUALS(
1169 expected_view->payload.ptr, expected_view->payload.len, publish_view->payload.ptr, publish_view->payload.len);
1170 ASSERT_INT_EQUALS(expected_view->packet_id, publish_view->packet_id);
1171 ASSERT_INT_EQUALS(expected_view->qos, publish_view->qos);
1172 ASSERT_INT_EQUALS(expected_view->duplicate, publish_view->duplicate);
1173 ASSERT_INT_EQUALS(expected_view->retain, publish_view->retain);
1174 ASSERT_BIN_ARRAYS_EQUALS(
1175 expected_view->topic.ptr, expected_view->topic.len, publish_view->topic.ptr, publish_view->topic.len);
1176 ASSERT_INT_EQUALS(*expected_view->payload_format, *publish_view->payload_format);
1177 ASSERT_INT_EQUALS(*expected_view->message_expiry_interval_seconds, *publish_view->message_expiry_interval_seconds);
1178 ASSERT_INT_EQUALS(*expected_view->topic_alias, *publish_view->topic_alias);
1179 ASSERT_BIN_ARRAYS_EQUALS(
1180 expected_view->response_topic->ptr,
1181 expected_view->response_topic->len,
1182 publish_view->response_topic->ptr,
1183 publish_view->response_topic->len);
1184 ASSERT_BIN_ARRAYS_EQUALS(
1185 expected_view->correlation_data->ptr,
1186 expected_view->correlation_data->len,
1187 publish_view->correlation_data->ptr,
1188 publish_view->correlation_data->len);
1189 ASSERT_BIN_ARRAYS_EQUALS(
1190 expected_view->content_type->ptr,
1191 expected_view->content_type->len,
1192 publish_view->content_type->ptr,
1193 publish_view->content_type->len);
1194 ASSERT_SUCCESS(aws_mqtt5_test_verify_user_properties_raw(
1195 expected_view->user_property_count,
1196 expected_view->user_properties,
1197 publish_view->user_property_count,
1198 publish_view->user_properties));
1199
1200 return AWS_OP_SUCCESS;
1201 }
1202
1203 static int s_mqtt5_packet_publish_round_trip_fn(struct aws_allocator *allocator, void *ctx) {
1204 (void)ctx;
1205
1206 aws_mqtt5_packet_id_t packet_id = 47;
1207 enum aws_mqtt5_payload_format_indicator payload_format = AWS_MQTT5_PFI_UTF8;
1208 uint32_t message_expiry_interval_seconds = 100;
1209 uint16_t topic_alias = 1;
1210 struct aws_byte_cursor response_topic = {
1211 .ptr = (uint8_t *)s_publish_response_topic,
1212 .len = AWS_ARRAY_SIZE(s_publish_response_topic) - 1,
1213 };
1214 struct aws_byte_cursor correlation_data = {
1215 .ptr = (uint8_t *)s_publish_correlation_data,
1216 .len = AWS_ARRAY_SIZE(s_publish_correlation_data) - 1,
1217 };
1218 struct aws_byte_cursor content_type = {
1219 .ptr = (uint8_t *)s_publish_content_type,
1220 .len = AWS_ARRAY_SIZE(s_publish_content_type) - 1,
1221 };
1222
1223 uint32_t subscription_identifiers[2] = {2, 3};
1224
1225 struct aws_mqtt5_packet_publish_view publish_view = {
1226 .payload =
1227 {
1228 .ptr = (uint8_t *)s_publish_payload,
1229 .len = AWS_ARRAY_SIZE(s_publish_payload) - 1,
1230 },
1231 .packet_id = packet_id,
1232 .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE,
1233 .duplicate = false,
1234 .retain = false,
1235 .topic =
1236 {
1237 .ptr = (uint8_t *)s_publish_topic,
1238 .len = AWS_ARRAY_SIZE(s_publish_topic) - 1,
1239 },
1240 .payload_format = &payload_format,
1241 .message_expiry_interval_seconds = &message_expiry_interval_seconds,
1242 .topic_alias = &topic_alias,
1243 .response_topic = &response_topic,
1244 .correlation_data = &correlation_data,
1245 .subscription_identifier_count = AWS_ARRAY_SIZE(subscription_identifiers),
1246 .subscription_identifiers = subscription_identifiers,
1247 .content_type = &content_type,
1248 .user_property_count = AWS_ARRAY_SIZE(s_user_properties),
1249 .user_properties = &s_user_properties[0],
1250 };
1251
1252 struct aws_mqtt5_packet_round_trip_test_context context = {
1253 .allocator = allocator,
1254 .packet_type = AWS_MQTT5_PT_PUBLISH,
1255 .packet_view = &publish_view,
1256 .decoder_callback = s_aws_mqtt5_on_publish_received_fn,
1257 };
1258
1259 ASSERT_SUCCESS(s_aws_mqtt5_encode_decode_round_trip_matrix_test(&context));
1260
1261 return AWS_OP_SUCCESS;
1262 }
1263
1264 AWS_TEST_CASE(mqtt5_packet_publish_round_trip, s_mqtt5_packet_publish_round_trip_fn)
1265
1266 static char s_puback_reason_string[] = "test reason string";
1267
1268 static int s_aws_mqtt5_on_puback_received_fn(enum aws_mqtt5_packet_type type, void *packet_view, void *user_data) {
1269 (void)type;
1270
1271 struct aws_mqtt5_encode_decode_tester *tester = user_data;
1272
1273 ++tester->view_count;
1274
1275 struct aws_mqtt5_packet_puback_view *puback_view = packet_view;
1276 struct aws_mqtt5_packet_puback_view *expected_view = tester->expected_views;
1277 ASSERT_INT_EQUALS(expected_view->packet_id, puback_view->packet_id);
1278 ASSERT_INT_EQUALS(expected_view->reason_code, puback_view->reason_code);
1279 ASSERT_BIN_ARRAYS_EQUALS(
1280 expected_view->reason_string->ptr,
1281 expected_view->reason_string->len,
1282 puback_view->reason_string->ptr,
1283 puback_view->reason_string->len);
1284 ASSERT_SUCCESS(aws_mqtt5_test_verify_user_properties_raw(
1285 expected_view->user_property_count,
1286 expected_view->user_properties,
1287 puback_view->user_property_count,
1288 puback_view->user_properties));
1289
1290 return AWS_OP_SUCCESS;
1291 }
1292
1293 static int s_mqtt5_packet_puback_round_trip_fn(struct aws_allocator *allocator, void *ctx) {
1294 (void)ctx;
1295
1296 aws_mqtt5_packet_id_t packet_id = 47;
1297 enum aws_mqtt5_puback_reason_code reason_code = AWS_MQTT5_PARC_NO_MATCHING_SUBSCRIBERS;
1298 struct aws_byte_cursor reason_string = {
1299 .ptr = (uint8_t *)s_puback_reason_string,
1300 .len = AWS_ARRAY_SIZE(s_puback_reason_string) - 1,
1301 };
1302
1303 struct aws_mqtt5_packet_puback_view puback_view = {
1304 .packet_id = packet_id,
1305 .reason_code = reason_code,
1306 .reason_string = &reason_string,
1307 .user_property_count = AWS_ARRAY_SIZE(s_user_properties),
1308 .user_properties = &s_user_properties[0],
1309 };
1310
1311 struct aws_mqtt5_packet_round_trip_test_context context = {
1312 .allocator = allocator,
1313 .packet_type = AWS_MQTT5_PT_PUBACK,
1314 .packet_view = &puback_view,
1315 .decoder_callback = s_aws_mqtt5_on_puback_received_fn,
1316 };
1317
1318 ASSERT_SUCCESS(s_aws_mqtt5_encode_decode_round_trip_matrix_test(&context));
1319
1320 return AWS_OP_SUCCESS;
1321 }
1322
1323 AWS_TEST_CASE(mqtt5_packet_puback_round_trip, s_mqtt5_packet_puback_round_trip_fn)
1324
1325 static int s_aws_mqtt5_on_no_will_connect_received_fn(
1326 enum aws_mqtt5_packet_type type,
1327 void *packet_view,
1328 void *user_data) {
1329 struct aws_mqtt5_encode_decode_tester *tester = user_data;
1330
1331 ++tester->view_count;
1332
1333 ASSERT_INT_EQUALS((uint32_t)AWS_MQTT5_PT_CONNECT, (uint32_t)type);
1334
1335 struct aws_mqtt5_packet_connect_view *connect_view = packet_view;
1336 struct aws_mqtt5_packet_connect_view *expected_view = tester->expected_views;
1337
1338 ASSERT_INT_EQUALS(expected_view->keep_alive_interval_seconds, connect_view->keep_alive_interval_seconds);
1339 ASSERT_BIN_ARRAYS_EQUALS(
1340 expected_view->client_id.ptr,
1341 expected_view->client_id.len,
1342 connect_view->client_id.ptr,
1343 connect_view->client_id.len);
1344
1345 if (expected_view->username != NULL) {
1346 ASSERT_BIN_ARRAYS_EQUALS(
1347 expected_view->username->ptr,
1348 expected_view->username->len,
1349 connect_view->username->ptr,
1350 connect_view->username->len);
1351 } else {
1352 ASSERT_NULL(connect_view->username);
1353 }
1354
1355 if (expected_view->password != NULL) {
1356 ASSERT_BIN_ARRAYS_EQUALS(
1357 expected_view->password->ptr,
1358 expected_view->password->len,
1359 connect_view->password->ptr,
1360 connect_view->password->len);
1361 } else {
1362 ASSERT_NULL(connect_view->password);
1363 }
1364
1365 ASSERT_TRUE(expected_view->clean_start == connect_view->clean_start);
1366 ASSERT_INT_EQUALS(*expected_view->session_expiry_interval_seconds, *connect_view->session_expiry_interval_seconds);
1367
1368 ASSERT_INT_EQUALS(*expected_view->request_response_information, *connect_view->request_response_information);
1369 ASSERT_INT_EQUALS(*expected_view->request_problem_information, *connect_view->request_problem_information);
1370 ASSERT_INT_EQUALS(*expected_view->receive_maximum, *connect_view->receive_maximum);
1371 ASSERT_INT_EQUALS(*expected_view->topic_alias_maximum, *connect_view->topic_alias_maximum);
1372 ASSERT_INT_EQUALS(*expected_view->maximum_packet_size_bytes, *connect_view->maximum_packet_size_bytes);
1373
1374 ASSERT_NULL(connect_view->will_delay_interval_seconds);
1375
1376 ASSERT_NULL(connect_view->will);
1377
1378 ASSERT_SUCCESS(aws_mqtt5_test_verify_user_properties_raw(
1379 expected_view->user_property_count,
1380 expected_view->user_properties,
1381 connect_view->user_property_count,
1382 connect_view->user_properties));
1383
1384 ASSERT_BIN_ARRAYS_EQUALS(
1385 expected_view->authentication_method->ptr,
1386 expected_view->authentication_method->len,
1387 connect_view->authentication_method->ptr,
1388 connect_view->authentication_method->len);
1389 ASSERT_BIN_ARRAYS_EQUALS(
1390 expected_view->authentication_data->ptr,
1391 expected_view->authentication_data->len,
1392 connect_view->authentication_data->ptr,
1393 connect_view->authentication_data->len);
1394
1395 return AWS_OP_SUCCESS;
1396 }
1397
1398 static int s_mqtt5_packet_encode_connect_no_will_fn(struct aws_allocator *allocator, void *ctx) {
1399 (void)ctx;
1400
1401 struct aws_byte_cursor username = aws_byte_cursor_from_c_str(s_username);
1402 struct aws_byte_cursor password = aws_byte_cursor_from_c_str(s_password);
1403 uint32_t session_expiry_interval_seconds = 3600;
1404 uint8_t request_response_information = 1;
1405 uint8_t request_problem_information = 1;
1406 uint16_t receive_maximum = 50;
1407 uint16_t topic_alias_maximum = 16;
1408 uint32_t maximum_packet_size_bytes = 1ULL << 24;
1409 uint32_t will_delay_interval_seconds = 30;
1410 struct aws_byte_cursor authentication_method = aws_byte_cursor_from_c_str("AuthMethod");
1411 struct aws_byte_cursor authentication_data = aws_byte_cursor_from_c_str("SuperSecret");
1412
1413 struct aws_mqtt5_packet_connect_view connect_view = {
1414 .keep_alive_interval_seconds = 1200,
1415 .client_id = aws_byte_cursor_from_c_str(s_client_id),
1416 .username = &username,
1417 .password = &password,
1418 .clean_start = true,
1419 .session_expiry_interval_seconds = &session_expiry_interval_seconds,
1420 .request_response_information = &request_response_information,
1421 .request_problem_information = &request_problem_information,
1422 .receive_maximum = &receive_maximum,
1423 .topic_alias_maximum = &topic_alias_maximum,
1424 .maximum_packet_size_bytes = &maximum_packet_size_bytes,
1425 .will_delay_interval_seconds = &will_delay_interval_seconds,
1426 .will = NULL,
1427 .user_property_count = AWS_ARRAY_SIZE(s_user_properties),
1428 .user_properties = &s_user_properties[0],
1429 .authentication_method = &authentication_method,
1430 .authentication_data = &authentication_data,
1431 };
1432
1433 struct aws_mqtt5_packet_round_trip_test_context context = {
1434 .allocator = allocator,
1435 .packet_type = AWS_MQTT5_PT_CONNECT,
1436 .packet_view = &connect_view,
1437 .decoder_callback = s_aws_mqtt5_on_no_will_connect_received_fn,
1438 };
1439
1440 ASSERT_SUCCESS(s_aws_mqtt5_encode_decode_round_trip_matrix_test(&context));
1441
1442 struct aws_byte_buf encoding_buffer;
1443 ASSERT_SUCCESS(s_mqtt5_packet_encode_connect_to_buffer(allocator, &connect_view, &encoding_buffer));
1444
1445 /*
1446 * For this packet: 1 byte packet type + flags, 2 bytes vli remaining length + 7 bytes protocol prefix
1447 * (0x00, 0x04, "MQTT", 0x05), then we're at the CONNECT flags which we want to check
1448 */
1449 size_t connect_flags_byte_index = 10;
1450 uint8_t connect_flags = encoding_buffer.buffer[connect_flags_byte_index];
1451
1452 /*
1453 * Verify Will flag, Will QoS, Will Retain are all zero,
1454 * while clean start (0x02), password (0x40) and username (0x80) flags are set
1455 */
1456 ASSERT_INT_EQUALS(connect_flags, 0xC2);
1457
1458 aws_byte_buf_clean_up(&encoding_buffer);
1459
1460 return AWS_OP_SUCCESS;
1461 }
1462
1463 AWS_TEST_CASE(mqtt5_packet_encode_connect_no_will, s_mqtt5_packet_encode_connect_no_will_fn)
1464
1465 static int s_mqtt5_packet_encode_connect_no_username_fn(struct aws_allocator *allocator, void *ctx) {
1466 (void)ctx;
1467
1468 struct aws_byte_cursor password = aws_byte_cursor_from_c_str(s_password);
1469 uint32_t session_expiry_interval_seconds = 3600;
1470 uint8_t request_response_information = 1;
1471 uint8_t request_problem_information = 1;
1472 uint16_t receive_maximum = 50;
1473 uint16_t topic_alias_maximum = 16;
1474 uint32_t maximum_packet_size_bytes = 1ULL << 24;
1475 uint32_t will_delay_interval_seconds = 30;
1476 struct aws_byte_cursor authentication_method = aws_byte_cursor_from_c_str("AuthMethod");
1477 struct aws_byte_cursor authentication_data = aws_byte_cursor_from_c_str("SuperSecret");
1478
1479 struct aws_mqtt5_packet_connect_view connect_view = {
1480 .keep_alive_interval_seconds = 1200,
1481 .client_id = aws_byte_cursor_from_c_str(s_client_id),
1482 .username = NULL,
1483 .password = &password,
1484 .clean_start = true,
1485 .session_expiry_interval_seconds = &session_expiry_interval_seconds,
1486 .request_response_information = &request_response_information,
1487 .request_problem_information = &request_problem_information,
1488 .receive_maximum = &receive_maximum,
1489 .topic_alias_maximum = &topic_alias_maximum,
1490 .maximum_packet_size_bytes = &maximum_packet_size_bytes,
1491 .will_delay_interval_seconds = &will_delay_interval_seconds,
1492 .will = NULL,
1493 .user_property_count = AWS_ARRAY_SIZE(s_user_properties),
1494 .user_properties = &s_user_properties[0],
1495 .authentication_method = &authentication_method,
1496 .authentication_data = &authentication_data,
1497 };
1498
1499 struct aws_mqtt5_packet_round_trip_test_context context = {
1500 .allocator = allocator,
1501 .packet_type = AWS_MQTT5_PT_CONNECT,
1502 .packet_view = &connect_view,
1503 .decoder_callback = s_aws_mqtt5_on_no_will_connect_received_fn,
1504 };
1505
1506 ASSERT_SUCCESS(s_aws_mqtt5_encode_decode_round_trip_matrix_test(&context));
1507
1508 struct aws_byte_buf encoding_buffer;
1509 ASSERT_SUCCESS(s_mqtt5_packet_encode_connect_to_buffer(allocator, &connect_view, &encoding_buffer));
1510
1511 /*
1512 * For this packet: 1 byte packet type + flags, 2 bytes vli remaining length + 7 bytes protocol prefix
1513 * (0x00, 0x04, "MQTT", 0x05), then we're at the CONNECT flags which we want to check
1514 */
1515 size_t connect_flags_byte_index = 10;
1516 uint8_t connect_flags = encoding_buffer.buffer[connect_flags_byte_index];
1517
1518 /*
1519 * Verify Will flag, Will QoS, Will Retain, Username are all zero,
1520 * while clean start (0x02), password (0x40) flags are set
1521 */
1522 ASSERT_INT_EQUALS(connect_flags, 0x42);
1523
1524 aws_byte_buf_clean_up(&encoding_buffer);
1525
1526 return AWS_OP_SUCCESS;
1527 }
1528
1529 AWS_TEST_CASE(mqtt5_packet_encode_connect_no_username, s_mqtt5_packet_encode_connect_no_username_fn)
1530
1531 static int s_mqtt5_packet_encode_connect_no_password_fn(struct aws_allocator *allocator, void *ctx) {
1532 (void)ctx;
1533
1534 struct aws_byte_cursor username = aws_byte_cursor_from_c_str(s_username);
1535 uint32_t session_expiry_interval_seconds = 3600;
1536 uint8_t request_response_information = 1;
1537 uint8_t request_problem_information = 1;
1538 uint16_t receive_maximum = 50;
1539 uint16_t topic_alias_maximum = 16;
1540 uint32_t maximum_packet_size_bytes = 1ULL << 24;
1541 uint32_t will_delay_interval_seconds = 30;
1542 struct aws_byte_cursor authentication_method = aws_byte_cursor_from_c_str("AuthMethod");
1543 struct aws_byte_cursor authentication_data = aws_byte_cursor_from_c_str("SuperSecret");
1544
1545 struct aws_mqtt5_packet_connect_view connect_view = {
1546 .keep_alive_interval_seconds = 1200,
1547 .client_id = aws_byte_cursor_from_c_str(s_client_id),
1548 .username = &username,
1549 .password = NULL,
1550 .clean_start = true,
1551 .session_expiry_interval_seconds = &session_expiry_interval_seconds,
1552 .request_response_information = &request_response_information,
1553 .request_problem_information = &request_problem_information,
1554 .receive_maximum = &receive_maximum,
1555 .topic_alias_maximum = &topic_alias_maximum,
1556 .maximum_packet_size_bytes = &maximum_packet_size_bytes,
1557 .will_delay_interval_seconds = &will_delay_interval_seconds,
1558 .will = NULL,
1559 .user_property_count = AWS_ARRAY_SIZE(s_user_properties),
1560 .user_properties = &s_user_properties[0],
1561 .authentication_method = &authentication_method,
1562 .authentication_data = &authentication_data,
1563 };
1564
1565 struct aws_mqtt5_packet_round_trip_test_context context = {
1566 .allocator = allocator,
1567 .packet_type = AWS_MQTT5_PT_CONNECT,
1568 .packet_view = &connect_view,
1569 .decoder_callback = s_aws_mqtt5_on_no_will_connect_received_fn,
1570 };
1571
1572 ASSERT_SUCCESS(s_aws_mqtt5_encode_decode_round_trip_matrix_test(&context));
1573
1574 struct aws_byte_buf encoding_buffer;
1575 ASSERT_SUCCESS(s_mqtt5_packet_encode_connect_to_buffer(allocator, &connect_view, &encoding_buffer));
1576
1577 /*
1578 * For this packet: 1 byte packet type + flags, 1 byte vli remaining length + 7 bytes protocol prefix
1579 * (0x00, 0x04, "MQTT", 0x05), then we're at the CONNECT flags which we want to check
1580 */
1581 size_t connect_flags_byte_index = 9;
1582 uint8_t connect_flags = encoding_buffer.buffer[connect_flags_byte_index];
1583
1584 /*
1585 * Verify Will flag, Will QoS, Will Retain, Password are all zero,
1586 * while clean start (0x02), username (0x80) flags are set
1587 */
1588 ASSERT_INT_EQUALS(connect_flags, 0x82);
1589
1590 aws_byte_buf_clean_up(&encoding_buffer);
1591
1592 return AWS_OP_SUCCESS;
1593 }
1594
1595 AWS_TEST_CASE(mqtt5_packet_encode_connect_no_password, s_mqtt5_packet_encode_connect_no_password_fn)
1596
1597 static int s_mqtt5_packet_encode_connect_will_property_order_fn(struct aws_allocator *allocator, void *ctx) {
1598 (void)ctx;
1599
1600 struct aws_byte_cursor will_payload_cursor = aws_byte_cursor_from_c_str(s_will_payload);
1601 enum aws_mqtt5_payload_format_indicator payload_format = AWS_MQTT5_PFI_UTF8;
1602 uint32_t message_expiry_interval_seconds = 65537;
1603 struct aws_byte_cursor will_response_topic = aws_byte_cursor_from_c_str(s_will_response_topic);
1604 struct aws_byte_cursor will_correlation_data = aws_byte_cursor_from_c_str(s_will_correlation_data);
1605 struct aws_byte_cursor will_content_type = aws_byte_cursor_from_c_str(s_will_content_type);
1606 struct aws_byte_cursor username = aws_byte_cursor_from_c_str(s_username);
1607 struct aws_byte_cursor password = aws_byte_cursor_from_c_str(s_password);
1608 uint32_t session_expiry_interval_seconds = 3600;
1609 uint8_t request_response_information = 1;
1610 uint8_t request_problem_information = 1;
1611 uint16_t receive_maximum = 50;
1612 uint16_t topic_alias_maximum = 16;
1613 uint32_t maximum_packet_size_bytes = 1ULL << 24;
1614 uint32_t will_delay_interval_seconds = 30;
1615 struct aws_byte_cursor authentication_method = aws_byte_cursor_from_c_str("AuthMethod");
1616 struct aws_byte_cursor authentication_data = aws_byte_cursor_from_c_str("SuperSecret");
1617
1618 struct aws_mqtt5_packet_publish_view will_view = {
1619 .payload = will_payload_cursor,
1620 .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE,
1621 .retain = true,
1622 .topic = aws_byte_cursor_from_c_str(s_will_topic),
1623 .payload_format = &payload_format,
1624 .message_expiry_interval_seconds = &message_expiry_interval_seconds,
1625 .response_topic = &will_response_topic,
1626 .correlation_data = &will_correlation_data,
1627 .content_type = &will_content_type,
1628 .user_property_count = AWS_ARRAY_SIZE(s_user_properties),
1629 .user_properties = &s_user_properties[0],
1630 };
1631
1632 struct aws_mqtt5_packet_connect_view connect_view = {
1633 .keep_alive_interval_seconds = 1200,
1634 .client_id = aws_byte_cursor_from_c_str(s_client_id),
1635 .username = &username,
1636 .password = &password,
1637 .clean_start = true,
1638 .session_expiry_interval_seconds = &session_expiry_interval_seconds,
1639 .request_response_information = &request_response_information,
1640 .request_problem_information = &request_problem_information,
1641 .receive_maximum = &receive_maximum,
1642 .topic_alias_maximum = &topic_alias_maximum,
1643 .maximum_packet_size_bytes = &maximum_packet_size_bytes,
1644 .will_delay_interval_seconds = &will_delay_interval_seconds,
1645 .will = &will_view,
1646 .user_property_count = AWS_ARRAY_SIZE(s_user_properties),
1647 .user_properties = &s_user_properties[0],
1648 .authentication_method = &authentication_method,
1649 .authentication_data = &authentication_data,
1650 };
1651
1652 struct aws_byte_buf encoding_buffer;
1653 ASSERT_SUCCESS(s_mqtt5_packet_encode_connect_to_buffer(allocator, &connect_view, &encoding_buffer));
1654
1655 struct aws_byte_cursor encoding_cursor = aws_byte_cursor_from_buf(&encoding_buffer);
1656
1657 struct aws_byte_cursor client_id_cursor;
1658 AWS_ZERO_STRUCT(client_id_cursor);
1659 ASSERT_SUCCESS(aws_byte_cursor_find_exact(&encoding_cursor, &connect_view.client_id, &client_id_cursor));
1660
1661 struct aws_byte_cursor will_topic_cursor;
1662 AWS_ZERO_STRUCT(will_topic_cursor);
1663 ASSERT_SUCCESS(aws_byte_cursor_find_exact(&encoding_cursor, &will_view.topic, &will_topic_cursor));
1664
1665 struct aws_byte_cursor will_message_payload_cursor;
1666 AWS_ZERO_STRUCT(will_message_payload_cursor);
1667 ASSERT_SUCCESS(aws_byte_cursor_find_exact(&encoding_cursor, &will_view.payload, &will_message_payload_cursor));
1668
1669 struct aws_byte_cursor username_cursor;
1670 AWS_ZERO_STRUCT(username_cursor);
1671 ASSERT_SUCCESS(aws_byte_cursor_find_exact(&encoding_cursor, connect_view.username, &username_cursor));
1672
1673 struct aws_byte_cursor password_cursor;
1674 AWS_ZERO_STRUCT(password_cursor);
1675 ASSERT_SUCCESS(aws_byte_cursor_find_exact(&encoding_cursor, connect_view.password, &password_cursor));
1676
1677 ASSERT_NOT_NULL(client_id_cursor.ptr);
1678 ASSERT_NOT_NULL(will_topic_cursor.ptr);
1679 ASSERT_NOT_NULL(will_message_payload_cursor.ptr);
1680 ASSERT_NOT_NULL(username_cursor.ptr);
1681 ASSERT_NOT_NULL(password_cursor.ptr);
1682
1683 ASSERT_TRUE(client_id_cursor.ptr < will_topic_cursor.ptr);
1684 ASSERT_TRUE(will_topic_cursor.ptr < will_message_payload_cursor.ptr);
1685 ASSERT_TRUE(will_message_payload_cursor.ptr < username_cursor.ptr);
1686 ASSERT_TRUE(username_cursor.ptr < password_cursor.ptr);
1687
1688 aws_byte_buf_clean_up(&encoding_buffer);
1689
1690 return AWS_OP_SUCCESS;
1691 }
1692
1693 AWS_TEST_CASE(mqtt5_packet_encode_connect_will_property_order, s_mqtt5_packet_encode_connect_will_property_order_fn)
1694
1695 static int s_aws_mqtt5_decoder_decode_subscribe_first_byte_check(struct aws_mqtt5_decoder *decoder) {
1696 uint8_t first_byte = decoder->packet_first_byte;
1697 if ((first_byte & 0x0F) != 2) {
1698 return AWS_OP_ERR;
1699 }
1700
1701 return AWS_OP_SUCCESS;
1702 }
1703 static int s_aws_mqtt5_decoder_decode_unsubscribe_first_byte_check(struct aws_mqtt5_decoder *decoder) {
1704 uint8_t first_byte = decoder->packet_first_byte;
1705 if ((first_byte & 0x0F) != 2) {
1706 return AWS_OP_ERR;
1707 }
1708
1709 return AWS_OP_SUCCESS;
1710 }
1711 static int s_aws_mqtt5_decoder_decode_disconnect_first_byte_check(struct aws_mqtt5_decoder *decoder) {
1712 uint8_t first_byte = decoder->packet_first_byte;
1713 if ((first_byte & 0x0F) != 0) {
1714 return AWS_OP_ERR;
1715 }
1716
1717 return AWS_OP_SUCCESS;
1718 }
1719
1720 static int s_aws_mqtt5_first_byte_check(
1721 struct aws_allocator *allocator,
1722 enum aws_mqtt5_packet_type packet_type,
1723 void *packet_view) {
1724 struct aws_byte_buf whole_dest;
1725 aws_byte_buf_init(&whole_dest, allocator, 4096);
1726
1727 struct aws_mqtt5_encode_decode_tester tester;
1728 aws_mqtt5_encode_init_testing_function_table(&tester.encoder_function_table);
1729 aws_mqtt5_decode_init_testing_function_table(&tester.decoder_function_table);
1730
1731 tester.decoder_function_table.decoders_by_packet_type[AWS_MQTT5_PT_SUBSCRIBE] =
1732 &s_aws_mqtt5_decoder_decode_subscribe_first_byte_check;
1733 tester.decoder_function_table.decoders_by_packet_type[AWS_MQTT5_PT_UNSUBSCRIBE] =
1734 &s_aws_mqtt5_decoder_decode_unsubscribe_first_byte_check;
1735 tester.decoder_function_table.decoders_by_packet_type[AWS_MQTT5_PT_DISCONNECT] =
1736 &s_aws_mqtt5_decoder_decode_disconnect_first_byte_check;
1737
1738 struct aws_mqtt5_encoder_options encoder_options = {
1739 .encoders = &tester.encoder_function_table,
1740 };
1741
1742 struct aws_mqtt5_encoder encoder;
1743
1744 ASSERT_SUCCESS(aws_mqtt5_encoder_init(&encoder, allocator, &encoder_options));
1745 ASSERT_SUCCESS(aws_mqtt5_encoder_append_packet_encoding(&encoder, packet_type, packet_view));
1746
1747 enum aws_mqtt5_encoding_result result = AWS_MQTT5_ER_ERROR;
1748 result = aws_mqtt5_encoder_encode_to_buffer(&encoder, &whole_dest);
1749 ASSERT_INT_EQUALS(AWS_MQTT5_ER_FINISHED, result);
1750
1751 struct aws_mqtt5_decoder_options decoder_options = {
1752 .callback_user_data = &tester,
1753 .decoder_table = &tester.decoder_function_table,
1754 };
1755
1756 struct aws_mqtt5_decoder decoder;
1757 ASSERT_SUCCESS(aws_mqtt5_decoder_init(&decoder, allocator, &decoder_options));
1758
1759 struct aws_byte_cursor whole_cursor = aws_byte_cursor_from_buf(&whole_dest);
1760 ASSERT_SUCCESS(aws_mqtt5_decoder_on_data_received(&decoder, whole_cursor));
1761
1762 aws_byte_buf_clean_up(&whole_dest);
1763 aws_mqtt5_encoder_clean_up(&encoder);
1764 aws_mqtt5_decoder_clean_up(&decoder);
1765
1766 return AWS_OP_SUCCESS;
1767 }
1768
1769 static int mqtt5_first_byte_reserved_header_check_subscribe_fn(struct aws_allocator *allocator, void *ctx) {
1770 (void)ctx;
1771
1772 aws_mqtt5_packet_id_t packet_id = 47;
1773 uint32_t subscription_identifier = 1;
1774
1775 struct aws_mqtt5_packet_subscribe_view subscribe_view = {
1776 .packet_id = packet_id,
1777 .subscription_count = AWS_ARRAY_SIZE(s_subscriptions),
1778 .subscriptions = &s_subscriptions[0],
1779 .subscription_identifier = &subscription_identifier,
1780 .user_property_count = AWS_ARRAY_SIZE(s_user_properties),
1781 .user_properties = &s_user_properties[0],
1782 };
1783
1784 ASSERT_SUCCESS(s_aws_mqtt5_first_byte_check(allocator, AWS_MQTT5_PT_SUBSCRIBE, &subscribe_view));
1785
1786 return AWS_OP_SUCCESS;
1787 }
1788
1789 AWS_TEST_CASE(mqtt5_first_byte_reserved_header_check_subscribe, mqtt5_first_byte_reserved_header_check_subscribe_fn)
1790
1791 static int mqtt5_first_byte_reserved_header_check_unsubscribe_fn(struct aws_allocator *allocator, void *ctx) {
1792 (void)ctx;
1793
1794 aws_mqtt5_packet_id_t packet_id = 47;
1795
1796 struct aws_mqtt5_packet_unsubscribe_view unsubscribe_view = {
1797 .packet_id = packet_id,
1798 .topic_filter_count = AWS_ARRAY_SIZE(s_unsubscribe_topics),
1799 .topic_filters = &s_unsubscribe_topics[0],
1800 .user_property_count = AWS_ARRAY_SIZE(s_user_properties),
1801 .user_properties = &s_user_properties[0],
1802 };
1803
1804 ASSERT_SUCCESS(s_aws_mqtt5_first_byte_check(allocator, AWS_MQTT5_PT_UNSUBSCRIBE, &unsubscribe_view));
1805
1806 return AWS_OP_SUCCESS;
1807 }
1808
1809 AWS_TEST_CASE(mqtt5_first_byte_reserved_header_check_unsubscribe, mqtt5_first_byte_reserved_header_check_unsubscribe_fn)
1810
1811 static int mqtt5_first_byte_reserved_header_check_disconnect_fn(struct aws_allocator *allocator, void *ctx) {
1812 (void)ctx;
1813
1814 uint32_t session_expiry_interval_seconds = 333;
1815 struct aws_byte_cursor reason_string_cursor = aws_byte_cursor_from_c_str(s_reason_string);
1816 struct aws_byte_cursor server_reference_cursor = aws_byte_cursor_from_c_str(s_server_reference);
1817
1818 struct aws_mqtt5_packet_disconnect_view disconnect_view = {
1819 .reason_code = AWS_MQTT5_DRC_DISCONNECT_WITH_WILL_MESSAGE,
1820 .session_expiry_interval_seconds = &session_expiry_interval_seconds,
1821 .reason_string = &reason_string_cursor,
1822 .user_property_count = AWS_ARRAY_SIZE(s_user_properties),
1823 .user_properties = &s_user_properties[0],
1824 .server_reference = &server_reference_cursor,
1825 };
1826
1827 ASSERT_SUCCESS(s_aws_mqtt5_first_byte_check(allocator, AWS_MQTT5_PT_DISCONNECT, &disconnect_view));
1828
1829 return AWS_OP_SUCCESS;
1830 }
1831
1832 AWS_TEST_CASE(mqtt5_first_byte_reserved_header_check_disconnect, mqtt5_first_byte_reserved_header_check_disconnect_fn)