Debian Codegraph @

crt/aws-c-mqtt/tests/v5/mqtt5_client_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
8 #include <aws/common/clock.h>
9 #include <aws/common/string.h>
10 #include <aws/http/websocket.h>
11 #include <aws/io/channel_bootstrap.h>
12 #include <aws/io/event_loop.h>
13 #include <aws/mqtt/mqtt.h>
14 #include <aws/mqtt/private/v5/mqtt5_utils.h>
15 #include <aws/mqtt/v5/mqtt5_client.h>
16 #include <aws/mqtt/v5/mqtt5_listener.h>
17
18 #include <aws/testing/aws_test_harness.h>
19
20 #include <math.h>
21
22 /* The delay margin accounts for system-level latencies, particularly when scheduling tasks on dispatch queues. In
23 * typical test environments, we observe delays of around 150–170ms before the task begins execution. To ensure
24 * stability, we define a margin of 200ms (AWS_MQTT5_TESTING_DELAY_NS). If related tests fail, it may indicate this
25 * margin is insufficient—consider increasing the value. */
26 #define AWS_MQTT5_TESTING_DELAY_NS 200 * 1000 * 1000 /*200ms*/
27
28 static bool s_is_within_percentage_of(uint64_t expected_time, uint64_t actual_time, double percentage) {
29 double actual_percent = 1.0 - (double)actual_time / (double)expected_time;
30 return fabs(actual_percent) <= percentage;
31 }
32
33 /* The function is used to validate the time interval for the expected and actual time. We used a user determined
34 * percentage for margin and a minimal of AWS_MQTT5_TESTING_DELAY_NS considering the system delay. */
35 static bool s_validate_time_interval(uint64_t expected_time, uint64_t actual_time, double percentage) {
36 uint64_t delta = actual_time > expected_time ? actual_time - expected_time : expected_time - actual_time;
37 return delta < AWS_MQTT5_TESTING_DELAY_NS || s_is_within_percentage_of(expected_time, actual_time, percentage);
38 }
39
40 static bool s_check_time_lower_bound(uint64_t expected_time, uint64_t actual_time) {
41 return actual_time >= expected_time;
42 }
43
44 int aws_mqtt5_mock_server_handle_connect_always_succeed(
45 void *packet,
46 struct aws_mqtt5_server_mock_connection_context *connection,
47 void *user_data) {
48 (void)packet;
49 (void)user_data;
50
51 struct aws_mqtt5_packet_connack_view connack_view;
52 AWS_ZERO_STRUCT(connack_view);
53
54 connack_view.reason_code = AWS_MQTT5_CRC_SUCCESS;
55
56 return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_CONNACK, &connack_view);
57 }
58
59 static int s_aws_mqtt5_mock_server_handle_pingreq_always_respond(
60 void *packet,
61 struct aws_mqtt5_server_mock_connection_context *connection,
62 void *user_data) {
63 (void)packet;
64 (void)user_data;
65
66 return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_PINGRESP, NULL);
67 }
68
69 static int s_aws_mqtt5_mock_server_handle_disconnect(
70 void *packet,
71 struct aws_mqtt5_server_mock_connection_context *connection,
72 void *user_data) {
73 (void)packet;
74 (void)connection;
75 (void)user_data;
76
77 return AWS_OP_SUCCESS;
78 }
79
80 void s_lifecycle_event_callback(const struct aws_mqtt5_client_lifecycle_event *event) {
81 (void)event;
82 }
83
84 void s_publish_received_callback(const struct aws_mqtt5_packet_publish_view *publish, void *user_data) {
85 (void)publish;
86 (void)user_data;
87 }
88
89 AWS_STRING_FROM_LITERAL(g_default_client_id, "HelloWorld");
90
91 void aws_mqtt5_client_test_init_default_options(struct mqtt5_client_test_options *test_options) {
92
93 struct aws_mqtt5_client_topic_alias_options local_topic_aliasing_options = {
94 .outbound_topic_alias_behavior = AWS_MQTT5_COTABT_DISABLED,
95 };
96
97 test_options->topic_aliasing_options = local_topic_aliasing_options;
98
99 struct aws_mqtt5_packet_connect_view local_connect_options = {
100 .keep_alive_interval_seconds = 30,
101 .client_id = aws_byte_cursor_from_string(g_default_client_id),
102 .clean_start = true,
103 };
104
105 test_options->connect_options = local_connect_options;
106
107 struct aws_mqtt5_client_options local_client_options = {
108 .connect_options = &test_options->connect_options,
109 .session_behavior = AWS_MQTT5_CSBT_CLEAN,
110 .lifecycle_event_handler = s_lifecycle_event_callback,
111 .lifecycle_event_handler_user_data = NULL,
112 .max_reconnect_delay_ms = 120000,
113 .min_connected_time_to_reset_reconnect_delay_ms = 30000,
114 .min_reconnect_delay_ms = 1000,
115 .ping_timeout_ms = 10000,
116 .publish_received_handler = s_publish_received_callback,
117 .ack_timeout_seconds = 0,
118 .topic_aliasing_options = &test_options->topic_aliasing_options,
119 };
120
121 test_options->client_options = local_client_options;
122
123 struct aws_mqtt5_mock_server_vtable local_server_function_table = {
124 .packet_handlers = {
125 NULL, /* RESERVED = 0 */
126 &aws_mqtt5_mock_server_handle_connect_always_succeed, /* CONNECT */
127 NULL, /* CONNACK */
128 NULL, /* PUBLISH */
129 NULL, /* PUBACK */
130 NULL, /* PUBREC */
131 NULL, /* PUBREL */
132 NULL, /* PUBCOMP */
133 NULL, /* SUBSCRIBE */
134 NULL, /* SUBACK */
135 NULL, /* UNSUBSCRIBE */
136 NULL, /* UNSUBACK */
137 &s_aws_mqtt5_mock_server_handle_pingreq_always_respond, /* PINGREQ */
138 NULL, /* PINGRESP */
139 &s_aws_mqtt5_mock_server_handle_disconnect, /* DISCONNECT */
140 NULL /* AUTH */
141 }};
142
143 test_options->server_function_table = local_server_function_table;
144 }
145
146 static int s_aws_mqtt5_client_test_init_default_connect_storage(
147 struct aws_mqtt5_packet_connect_storage *storage,
148 struct aws_allocator *allocator) {
149
150 struct aws_mqtt5_packet_connect_view connect_view = {
151 .keep_alive_interval_seconds = 30,
152 .client_id = aws_byte_cursor_from_string(g_default_client_id),
153 .clean_start = true,
154 };
155
156 return aws_mqtt5_packet_connect_storage_init(storage, allocator, &connect_view, NULL);
157 }
158
159 static int s_aws_mqtt5_client_test_init_default_disconnect_storage(
160 struct aws_mqtt5_packet_disconnect_storage *storage,
161 struct aws_allocator *allocator) {
162 struct aws_mqtt5_packet_disconnect_view disconnect_view = {
163 .reason_code = AWS_MQTT5_DRC_NORMAL_DISCONNECTION,
164 };
165
166 return aws_mqtt5_packet_disconnect_storage_init(storage, allocator, &disconnect_view);
167 }
168
169 static bool s_last_life_cycle_event_is(
170 struct aws_mqtt5_client_mock_test_fixture *test_fixture,
171 enum aws_mqtt5_client_lifecycle_event_type event_type) {
172 size_t event_count = aws_array_list_length(&test_fixture->lifecycle_events);
173 if (event_count == 0) {
174 return false;
175 }
176
177 struct aws_mqtt5_lifecycle_event_record *record = NULL;
178 aws_array_list_get_at(&test_fixture->lifecycle_events, &record, event_count - 1);
179
180 return record->event.event_type == event_type;
181 }
182
183 static bool s_last_mock_server_packet_received_is(
184 struct aws_mqtt5_client_mock_test_fixture *test_fixture,
185 enum aws_mqtt5_packet_type packet_type) {
186 size_t packet_count = aws_array_list_length(&test_fixture->server_received_packets);
187 if (packet_count == 0) {
188 return false;
189 }
190
191 struct aws_mqtt5_mock_server_packet_record *packet = NULL;
192 aws_array_list_get_at_ptr(&test_fixture->server_received_packets, (void **)&packet, packet_count - 1);
193
194 return packet_type == packet->packet_type;
195 }
196
197 static bool s_last_mock_server_packet_received_is_disconnect(void *arg) {
198 struct aws_mqtt5_client_mock_test_fixture *test_fixture = arg;
199
200 return s_last_mock_server_packet_received_is(test_fixture, AWS_MQTT5_PT_DISCONNECT);
201 }
202
203 static void s_wait_for_mock_server_to_receive_disconnect_packet(
204 struct aws_mqtt5_client_mock_test_fixture *test_context) {
205 aws_mutex_lock(&test_context->lock);
206 aws_condition_variable_wait_pred(
207 &test_context->signal, &test_context->lock, s_last_mock_server_packet_received_is_disconnect, test_context);
208 aws_mutex_unlock(&test_context->lock);
209 }
210
211 static bool s_last_lifecycle_event_is_connected(void *arg) {
212 struct aws_mqtt5_client_mock_test_fixture *test_fixture = arg;
213
214 return s_last_life_cycle_event_is(test_fixture, AWS_MQTT5_CLET_CONNECTION_SUCCESS);
215 }
216
217 void aws_wait_for_connected_lifecycle_event(struct aws_mqtt5_client_mock_test_fixture *test_context) {
218 aws_mutex_lock(&test_context->lock);
219 aws_condition_variable_wait_pred(
220 &test_context->signal, &test_context->lock, s_last_lifecycle_event_is_connected, test_context);
221 aws_mutex_unlock(&test_context->lock);
222 }
223
224 static bool s_last_lifecycle_event_is_stopped(void *arg) {
225 struct aws_mqtt5_client_mock_test_fixture *test_fixture = arg;
226
227 return s_last_life_cycle_event_is(test_fixture, AWS_MQTT5_CLET_STOPPED);
228 }
229
230 void aws_wait_for_stopped_lifecycle_event(struct aws_mqtt5_client_mock_test_fixture *test_context) {
231 aws_mutex_lock(&test_context->lock);
232 aws_condition_variable_wait_pred(
233 &test_context->signal, &test_context->lock, s_last_lifecycle_event_is_stopped, test_context);
234 aws_mutex_unlock(&test_context->lock);
235 }
236
237 static bool s_has_lifecycle_event(
238 struct aws_mqtt5_client_mock_test_fixture *test_fixture,
239 enum aws_mqtt5_client_lifecycle_event_type event_type) {
240
241 size_t record_count = aws_array_list_length(&test_fixture->lifecycle_events);
242 for (size_t i = 0; i < record_count; ++i) {
243 struct aws_mqtt5_lifecycle_event_record *record = NULL;
244 aws_array_list_get_at(&test_fixture->lifecycle_events, &record, i);
245 if (record->event.event_type == event_type) {
246 return true;
247 }
248 }
249
250 return false;
251 }
252
253 static bool s_has_connection_failure_event(void *arg) {
254 struct aws_mqtt5_client_mock_test_fixture *test_fixture = arg;
255
256 return s_has_lifecycle_event(test_fixture, AWS_MQTT5_CLET_CONNECTION_FAILURE);
257 }
258
259 static void s_wait_for_connection_failure_lifecycle_event(struct aws_mqtt5_client_mock_test_fixture *test_context) {
260 aws_mutex_lock(&test_context->lock);
261 aws_condition_variable_wait_pred(
262 &test_context->signal, &test_context->lock, s_has_connection_failure_event, test_context);
263 aws_mutex_unlock(&test_context->lock);
264 }
265
266 static bool s_has_disconnect_event(void *arg) {
267 struct aws_mqtt5_client_mock_test_fixture *test_fixture = arg;
268
269 return s_has_lifecycle_event(test_fixture, AWS_MQTT5_CLET_DISCONNECTION);
270 }
271
272 static void s_wait_for_disconnection_lifecycle_event(struct aws_mqtt5_client_mock_test_fixture *test_context) {
273 aws_mutex_lock(&test_context->lock);
274 aws_condition_variable_wait_pred(&test_context->signal, &test_context->lock, s_has_disconnect_event, test_context);
275 aws_mutex_unlock(&test_context->lock);
276 }
277
278 static bool s_disconnect_completion_invoked(void *arg) {
279 struct aws_mqtt5_client_mock_test_fixture *test_fixture = arg;
280
281 return test_fixture->disconnect_completion_callback_invoked;
282 }
283
284 static void s_on_disconnect_completion(int error_code, void *user_data) {
285 (void)error_code;
286
287 struct aws_mqtt5_client_mock_test_fixture *test_fixture = user_data;
288
289 aws_mutex_lock(&test_fixture->lock);
290 test_fixture->disconnect_completion_callback_invoked = true;
291 aws_mutex_unlock(&test_fixture->lock);
292 aws_condition_variable_notify_all(&test_fixture->signal);
293 }
294
295 static void s_wait_for_disconnect_completion(struct aws_mqtt5_client_mock_test_fixture *test_context) {
296 aws_mutex_lock(&test_context->lock);
297 aws_condition_variable_wait_pred(
298 &test_context->signal, &test_context->lock, s_disconnect_completion_invoked, test_context);
299 aws_mutex_unlock(&test_context->lock);
300 }
301
302 int aws_verify_client_state_sequence(
303 struct aws_mqtt5_client_mock_test_fixture *test_context,
304 enum aws_mqtt5_client_state *expected_states,
305 size_t expected_states_count) {
306 aws_mutex_lock(&test_context->lock);
307
308 size_t actual_states_count = aws_array_list_length(&test_context->client_states);
309 ASSERT_TRUE(actual_states_count >= expected_states_count);
310
311 for (size_t i = 0; i < expected_states_count; ++i) {
312 enum aws_mqtt5_client_state state = AWS_MCS_STOPPED;
313 aws_array_list_get_at(&test_context->client_states, &state, i);
314
315 ASSERT_INT_EQUALS(expected_states[i], state);
316 }
317
318 aws_mutex_unlock(&test_context->lock);
319
320 return AWS_OP_SUCCESS;
321 }
322
323 static int s_verify_simple_lifecycle_event_sequence(
324 struct aws_mqtt5_client_mock_test_fixture *test_context,
325 struct aws_mqtt5_client_lifecycle_event *expected_events,
326 size_t expected_events_count) {
327 aws_mutex_lock(&test_context->lock);
328
329 size_t actual_events_count = aws_array_list_length(&test_context->lifecycle_events);
330 ASSERT_TRUE(actual_events_count >= expected_events_count);
331
332 for (size_t i = 0; i < expected_events_count; ++i) {
333 struct aws_mqtt5_lifecycle_event_record *lifecycle_event = NULL;
334 aws_array_list_get_at(&test_context->lifecycle_events, &lifecycle_event, i);
335
336 struct aws_mqtt5_client_lifecycle_event *expected_event = &expected_events[i];
337 ASSERT_INT_EQUALS(expected_event->event_type, lifecycle_event->event.event_type);
338 ASSERT_INT_EQUALS(expected_event->error_code, lifecycle_event->event.error_code);
339 }
340
341 aws_mutex_unlock(&test_context->lock);
342
343 return AWS_OP_SUCCESS;
344 }
345
346 int aws_verify_received_packet_sequence(
347 struct aws_mqtt5_client_mock_test_fixture *test_context,
348 struct aws_mqtt5_mock_server_packet_record *expected_packets,
349 size_t expected_packets_count) {
350 aws_mutex_lock(&test_context->lock);
351
352 size_t actual_packets_count = aws_array_list_length(&test_context->server_received_packets);
353 ASSERT_TRUE(actual_packets_count >= expected_packets_count);
354
355 for (size_t i = 0; i < expected_packets_count; ++i) {
356 struct aws_mqtt5_mock_server_packet_record *actual_packet = NULL;
357 aws_array_list_get_at_ptr(&test_context->server_received_packets, (void **)&actual_packet, i);
358
359 struct aws_mqtt5_mock_server_packet_record *expected_packet = &expected_packets[i];
360
361 ASSERT_INT_EQUALS(expected_packet->packet_type, actual_packet->packet_type);
362
363 /* a NULL storage means we don't care about verifying it on a field-by-field basis */
364 if (expected_packet->packet_storage != NULL) {
365 ASSERT_TRUE(aws_mqtt5_client_test_are_packets_equal(
366 expected_packet->packet_type, expected_packet->packet_storage, actual_packet->packet_storage));
367 }
368 }
369
370 aws_mutex_unlock(&test_context->lock);
371
372 return AWS_OP_SUCCESS;
373 }
374
375 /*
376 * Basic successful connect/disconnect test. We check expected lifecycle events, internal client state changes,
377 * and server received packets.
378 */
379 static int s_mqtt5_client_direct_connect_success_fn(struct aws_allocator *allocator, void *ctx) {
380 (void)ctx;
381
382 aws_mqtt_library_init(allocator);
383
384 struct mqtt5_client_test_options test_options;
385 aws_mqtt5_client_test_init_default_options(&test_options);
386
387 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
388 .client_options = &test_options.client_options,
389 .server_function_table = &test_options.server_function_table,
390 };
391
392 struct aws_mqtt5_client_mock_test_fixture test_context;
393 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
394
395 struct aws_mqtt5_client *client = test_context.client;
396 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
397
398 aws_wait_for_connected_lifecycle_event(&test_context);
399
400 struct aws_mqtt5_packet_disconnect_view disconnect_options = {
401 .reason_code = AWS_MQTT5_DRC_DISCONNECT_WITH_WILL_MESSAGE,
402 };
403
404 struct aws_mqtt5_disconnect_completion_options completion_options = {
405 .completion_callback = s_on_disconnect_completion,
406 .completion_user_data = &test_context,
407 };
408
409 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, &disconnect_options, &completion_options));
410
411 aws_wait_for_stopped_lifecycle_event(&test_context);
412 s_wait_for_disconnect_completion(&test_context);
413 s_wait_for_mock_server_to_receive_disconnect_packet(&test_context);
414
415 struct aws_mqtt5_client_lifecycle_event expected_events[] = {
416 {
417 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
418 },
419 {
420 .event_type = AWS_MQTT5_CLET_CONNECTION_SUCCESS,
421 },
422 {
423 .event_type = AWS_MQTT5_CLET_DISCONNECTION,
424 .error_code = AWS_ERROR_MQTT5_USER_REQUESTED_STOP,
425 },
426 {
427 .event_type = AWS_MQTT5_CLET_STOPPED,
428 },
429 };
430 ASSERT_SUCCESS(
431 s_verify_simple_lifecycle_event_sequence(&test_context, expected_events, AWS_ARRAY_SIZE(expected_events)));
432
433 enum aws_mqtt5_client_state expected_states[] = {
434 AWS_MCS_CONNECTING,
435 AWS_MCS_MQTT_CONNECT,
436 AWS_MCS_CONNECTED,
437 AWS_MCS_CLEAN_DISCONNECT,
438 AWS_MCS_CHANNEL_SHUTDOWN,
439 AWS_MCS_STOPPED,
440 };
441
442 ASSERT_SUCCESS(aws_verify_client_state_sequence(&test_context, expected_states, AWS_ARRAY_SIZE(expected_states)));
443
444 struct aws_mqtt5_packet_connect_storage expected_connect_storage;
445 ASSERT_SUCCESS(s_aws_mqtt5_client_test_init_default_connect_storage(&expected_connect_storage, allocator));
446
447 struct aws_mqtt5_packet_disconnect_storage expected_disconnect_storage;
448 ASSERT_SUCCESS(s_aws_mqtt5_client_test_init_default_disconnect_storage(&expected_disconnect_storage, allocator));
449 expected_disconnect_storage.storage_view.reason_code = AWS_MQTT5_DRC_DISCONNECT_WITH_WILL_MESSAGE;
450
451 struct aws_mqtt5_mock_server_packet_record expected_packets[] = {
452 {
453 .packet_type = AWS_MQTT5_PT_CONNECT,
454 .packet_storage = &expected_connect_storage,
455 },
456 {
457 .packet_type = AWS_MQTT5_PT_DISCONNECT,
458 .packet_storage = &expected_disconnect_storage,
459 },
460 };
461 ASSERT_SUCCESS(
462 aws_verify_received_packet_sequence(&test_context, expected_packets, AWS_ARRAY_SIZE(expected_packets)));
463
464 aws_mqtt5_packet_connect_storage_clean_up(&expected_connect_storage);
465
466 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
467 aws_mqtt_library_clean_up();
468
469 return AWS_OP_SUCCESS;
470 }
471
472 AWS_TEST_CASE(mqtt5_client_direct_connect_success, s_mqtt5_client_direct_connect_success_fn)
473
474 /*
475 * Connection failure test infrastructure. Supplied callbacks are used to modify the way in which the connection
476 * establishment fails.
477 */
478 static int s_mqtt5_client_simple_failure_test_fn(
479 struct aws_allocator *allocator,
480 void (*change_client_test_config_fn)(struct aws_mqtt5_client_mqtt5_mock_test_fixture_options *config),
481 void (*change_client_vtable_fn)(struct aws_mqtt5_client_vtable *)) {
482
483 aws_mqtt_library_init(allocator);
484
485 struct mqtt5_client_test_options test_options;
486 aws_mqtt5_client_test_init_default_options(&test_options);
487
488 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
489 .client_options = &test_options.client_options,
490 .server_function_table = &test_options.server_function_table,
491 };
492
493 if (change_client_test_config_fn != NULL) {
494 (*change_client_test_config_fn)(&test_fixture_options);
495 }
496
497 struct aws_mqtt5_client_mock_test_fixture test_context;
498 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
499
500 struct aws_mqtt5_client *client = test_context.client;
501 struct aws_mqtt5_client_vtable vtable = *client->vtable;
502 if (change_client_vtable_fn != NULL) {
503 (*change_client_vtable_fn)(&vtable);
504 aws_mqtt5_client_set_vtable(client, &vtable);
505 }
506
507 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
508
509 s_wait_for_connection_failure_lifecycle_event(&test_context);
510
511 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
512
513 aws_wait_for_stopped_lifecycle_event(&test_context);
514
515 struct aws_mqtt5_client_lifecycle_event expected_events[] = {
516 {
517 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
518 },
519 {
520 .event_type = AWS_MQTT5_CLET_CONNECTION_FAILURE,
521 .error_code = AWS_ERROR_INVALID_STATE,
522 },
523 };
524 ASSERT_SUCCESS(
525 s_verify_simple_lifecycle_event_sequence(&test_context, expected_events, AWS_ARRAY_SIZE(expected_events)));
526
527 enum aws_mqtt5_client_state expected_states[] = {
528 AWS_MCS_CONNECTING,
529 AWS_MCS_PENDING_RECONNECT,
530 };
531 ASSERT_SUCCESS(aws_verify_client_state_sequence(&test_context, expected_states, AWS_ARRAY_SIZE(expected_states)));
532
533 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
534 aws_mqtt_library_clean_up();
535
536 return AWS_OP_SUCCESS;
537 }
538
539 static int s_mqtt5_client_test_synchronous_socket_channel_failure_fn(
540 struct aws_socket_channel_bootstrap_options *options) {
541 (void)options;
542
543 return aws_raise_error(AWS_ERROR_INVALID_STATE);
544 }
545
546 static void s_change_client_vtable_synchronous_direct_failure(struct aws_mqtt5_client_vtable *vtable) {
547 vtable->client_bootstrap_new_socket_channel_fn = s_mqtt5_client_test_synchronous_socket_channel_failure_fn;
548 }
549
550 /* Connection failure test where direct MQTT channel establishment fails synchronously */
551 static int s_mqtt5_client_direct_connect_sync_channel_failure_fn(struct aws_allocator *allocator, void *ctx) {
552 (void)ctx;
553
554 ASSERT_SUCCESS(
555 s_mqtt5_client_simple_failure_test_fn(allocator, NULL, s_change_client_vtable_synchronous_direct_failure));
556
557 return AWS_OP_SUCCESS;
558 }
559
560 AWS_TEST_CASE(mqtt5_client_direct_connect_sync_channel_failure, s_mqtt5_client_direct_connect_sync_channel_failure_fn)
561
562 struct socket_channel_failure_wrapper {
563 struct aws_socket_channel_bootstrap_options bootstrap_options;
564 struct aws_task task;
565 };
566
567 static struct socket_channel_failure_wrapper s_socket_channel_failure_wrapper;
568
569 void s_socket_channel_async_failure_task_fn(struct aws_task *task, void *arg, enum aws_task_status status) {
570 (void)task;
571 if (status != AWS_TASK_STATUS_RUN_READY) {
572 return;
573 }
574
575 struct socket_channel_failure_wrapper *wrapper = arg;
576 struct aws_socket_channel_bootstrap_options *options = &wrapper->bootstrap_options;
577
578 (*wrapper->bootstrap_options.setup_callback)(options->bootstrap, AWS_ERROR_INVALID_STATE, NULL, options->user_data);
579 }
580
581 static int s_mqtt5_client_test_asynchronous_socket_channel_failure_fn(
582 struct aws_socket_channel_bootstrap_options *options) {
583 aws_task_init(
584 &s_socket_channel_failure_wrapper.task,
585 s_socket_channel_async_failure_task_fn,
586 &s_socket_channel_failure_wrapper,
587 "asynchronous_socket_channel_failure");
588 s_socket_channel_failure_wrapper.bootstrap_options = *options;
589
590 struct aws_mqtt5_client *client = options->user_data;
591 aws_event_loop_schedule_task_now(client->loop, &s_socket_channel_failure_wrapper.task);
592
593 return AWS_OP_SUCCESS;
594 }
595
596 static void s_change_client_vtable_asynchronous_direct_failure(struct aws_mqtt5_client_vtable *vtable) {
597 vtable->client_bootstrap_new_socket_channel_fn = s_mqtt5_client_test_asynchronous_socket_channel_failure_fn;
598 }
599
600 /* Connection failure test where direct MQTT channel establishment fails asynchronously */
601 static int s_mqtt5_client_direct_connect_async_channel_failure_fn(struct aws_allocator *allocator, void *ctx) {
602 (void)ctx;
603
604 ASSERT_SUCCESS(
605 s_mqtt5_client_simple_failure_test_fn(allocator, NULL, s_change_client_vtable_asynchronous_direct_failure));
606
607 return AWS_OP_SUCCESS;
608 }
609
610 AWS_TEST_CASE(mqtt5_client_direct_connect_async_channel_failure, s_mqtt5_client_direct_connect_async_channel_failure_fn)
611
612 static int s_mqtt5_client_test_synchronous_websocket_failure_fn(
613 const struct aws_websocket_client_connection_options *options) {
614 (void)options;
615
616 return aws_raise_error(AWS_ERROR_INVALID_STATE);
617 }
618
619 static void s_change_client_vtable_synchronous_websocket_failure(struct aws_mqtt5_client_vtable *vtable) {
620 vtable->websocket_connect_fn = s_mqtt5_client_test_synchronous_websocket_failure_fn;
621 }
622
623 static void s_mqtt5_client_test_websocket_successful_transform(
624 struct aws_http_message *request,
625 void *user_data,
626 aws_mqtt5_transform_websocket_handshake_complete_fn *complete_fn,
627 void *complete_ctx) {
628 (void)user_data;
629
630 (*complete_fn)(request, AWS_ERROR_SUCCESS, complete_ctx);
631 }
632
633 static void s_change_client_options_to_websockets(struct aws_mqtt5_client_mqtt5_mock_test_fixture_options *config) {
634 config->client_options->websocket_handshake_transform = s_mqtt5_client_test_websocket_successful_transform;
635 }
636
637 /* Connection failure test where websocket MQTT channel establishment fails synchronously */
638 static int s_mqtt5_client_websocket_connect_sync_channel_failure_fn(struct aws_allocator *allocator, void *ctx) {
639 (void)ctx;
640
641 ASSERT_SUCCESS(s_mqtt5_client_simple_failure_test_fn(
642 allocator, s_change_client_options_to_websockets, s_change_client_vtable_synchronous_websocket_failure));
643
644 return AWS_OP_SUCCESS;
645 }
646
647 AWS_TEST_CASE(
648 mqtt5_client_websocket_connect_sync_channel_failure,
649 s_mqtt5_client_websocket_connect_sync_channel_failure_fn)
650
651 struct websocket_channel_failure_wrapper {
652 struct aws_websocket_client_connection_options websocket_options;
653 struct aws_task task;
654 };
655
656 static struct websocket_channel_failure_wrapper s_websocket_channel_failure_wrapper;
657
658 void s_websocket_channel_async_failure_task_fn(struct aws_task *task, void *arg, enum aws_task_status status) {
659 (void)task;
660 if (status != AWS_TASK_STATUS_RUN_READY) {
661 return;
662 }
663
664 struct websocket_channel_failure_wrapper *wrapper = arg;
665 struct aws_websocket_client_connection_options *options = &wrapper->websocket_options;
666
667 struct aws_websocket_on_connection_setup_data websocket_setup = {.error_code = AWS_ERROR_INVALID_STATE};
668 (*wrapper->websocket_options.on_connection_setup)(&websocket_setup, options->user_data);
669 }
670
671 static int s_mqtt5_client_test_asynchronous_websocket_failure_fn(
672 const struct aws_websocket_client_connection_options *options) {
673 aws_task_init(
674 &s_websocket_channel_failure_wrapper.task,
675 s_websocket_channel_async_failure_task_fn,
676 &s_websocket_channel_failure_wrapper,
677 "asynchronous_websocket_channel_failure");
678 s_websocket_channel_failure_wrapper.websocket_options = *options;
679
680 struct aws_mqtt5_client *client = options->user_data;
681 aws_event_loop_schedule_task_now(client->loop, &s_websocket_channel_failure_wrapper.task);
682
683 return AWS_OP_SUCCESS;
684 }
685
686 static void s_change_client_vtable_asynchronous_websocket_failure(struct aws_mqtt5_client_vtable *vtable) {
687 vtable->websocket_connect_fn = s_mqtt5_client_test_asynchronous_websocket_failure_fn;
688 }
689
690 /* Connection failure test where websocket MQTT channel establishment fails asynchronously */
691 static int s_mqtt5_client_websocket_connect_async_channel_failure_fn(struct aws_allocator *allocator, void *ctx) {
692 (void)ctx;
693
694 ASSERT_SUCCESS(s_mqtt5_client_simple_failure_test_fn(
695 allocator, s_change_client_options_to_websockets, s_change_client_vtable_asynchronous_websocket_failure));
696
697 return AWS_OP_SUCCESS;
698 }
699
700 AWS_TEST_CASE(
701 mqtt5_client_websocket_connect_async_channel_failure,
702 s_mqtt5_client_websocket_connect_async_channel_failure_fn)
703
704 static void s_mqtt5_client_test_websocket_failed_transform(
705 struct aws_http_message *request,
706 void *user_data,
707 aws_mqtt5_transform_websocket_handshake_complete_fn *complete_fn,
708 void *complete_ctx) {
709
710 (void)user_data;
711
712 (*complete_fn)(request, AWS_ERROR_INVALID_STATE, complete_ctx);
713 }
714
715 static void s_change_client_options_to_failed_websocket_transform(
716 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options *config) {
717 config->client_options->websocket_handshake_transform = s_mqtt5_client_test_websocket_failed_transform;
718 }
719
720 /* Connection failure test where websocket MQTT channel establishment fails due to handshake transform failure */
721 static int s_mqtt5_client_websocket_connect_handshake_failure_fn(struct aws_allocator *allocator, void *ctx) {
722 (void)ctx;
723
724 ASSERT_SUCCESS(
725 s_mqtt5_client_simple_failure_test_fn(allocator, s_change_client_options_to_failed_websocket_transform, NULL));
726
727 return AWS_OP_SUCCESS;
728 }
729
730 AWS_TEST_CASE(mqtt5_client_websocket_connect_handshake_failure, s_mqtt5_client_websocket_connect_handshake_failure_fn)
731
732 int aws_mqtt5_mock_server_handle_connect_always_fail(
733 void *packet,
734 struct aws_mqtt5_server_mock_connection_context *connection,
735 void *user_data) {
736 (void)packet;
737 (void)user_data;
738
739 struct aws_mqtt5_packet_connack_view connack_view;
740 AWS_ZERO_STRUCT(connack_view);
741
742 connack_view.reason_code = AWS_MQTT5_CRC_BANNED;
743
744 return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_CONNACK, &connack_view);
745 }
746
747 /* Connection failure test where overall connection fails due to a CONNACK error code */
748 static int s_mqtt5_client_direct_connect_connack_refusal_fn(struct aws_allocator *allocator, void *ctx) {
749 (void)ctx;
750
751 aws_mqtt_library_init(allocator);
752
753 struct mqtt5_client_test_options test_options;
754 aws_mqtt5_client_test_init_default_options(&test_options);
755
756 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] =
757 aws_mqtt5_mock_server_handle_connect_always_fail;
758
759 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
760 .client_options = &test_options.client_options,
761 .server_function_table = &test_options.server_function_table,
762 };
763
764 struct aws_mqtt5_client_mock_test_fixture test_context;
765 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
766
767 struct aws_mqtt5_client *client = test_context.client;
768
769 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
770
771 s_wait_for_connection_failure_lifecycle_event(&test_context);
772
773 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
774
775 aws_wait_for_stopped_lifecycle_event(&test_context);
776
777 struct aws_mqtt5_client_lifecycle_event expected_events[] = {
778 {
779 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
780 },
781 {
782 .event_type = AWS_MQTT5_CLET_CONNECTION_FAILURE,
783 .error_code = AWS_ERROR_MQTT5_CONNACK_CONNECTION_REFUSED,
784 },
785 };
786 ASSERT_SUCCESS(
787 s_verify_simple_lifecycle_event_sequence(&test_context, expected_events, AWS_ARRAY_SIZE(expected_events)));
788
789 enum aws_mqtt5_client_state expected_states[] = {
790 AWS_MCS_CONNECTING,
791 AWS_MCS_MQTT_CONNECT,
792 AWS_MCS_CHANNEL_SHUTDOWN,
793 };
794 ASSERT_SUCCESS(aws_verify_client_state_sequence(&test_context, expected_states, AWS_ARRAY_SIZE(expected_states)));
795
796 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
797 aws_mqtt_library_clean_up();
798
799 return AWS_OP_SUCCESS;
800 }
801
802 AWS_TEST_CASE(mqtt5_client_direct_connect_connack_refusal, s_mqtt5_client_direct_connect_connack_refusal_fn)
803
804 /* Connection failure test where overall connection fails because there's no response to the CONNECT packet */
805 static int s_mqtt5_client_direct_connect_connack_timeout_fn(struct aws_allocator *allocator, void *ctx) {
806 (void)ctx;
807
808 aws_mqtt_library_init(allocator);
809
810 struct mqtt5_client_test_options test_options;
811 aws_mqtt5_client_test_init_default_options(&test_options);
812
813 /* fast CONNACK timeout and don't response to the CONNECT packet */
814 test_options.client_options.connack_timeout_ms = 2000;
815 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] = NULL;
816
817 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
818 .client_options = &test_options.client_options,
819 .server_function_table = &test_options.server_function_table,
820 };
821
822 struct aws_mqtt5_client_mock_test_fixture test_context;
823 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
824
825 struct aws_mqtt5_client *client = test_context.client;
826
827 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
828
829 s_wait_for_connection_failure_lifecycle_event(&test_context);
830
831 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
832
833 aws_wait_for_stopped_lifecycle_event(&test_context);
834
835 struct aws_mqtt5_client_lifecycle_event expected_events[] = {
836 {
837 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
838 },
839 {
840 .event_type = AWS_MQTT5_CLET_CONNECTION_FAILURE,
841 .error_code = AWS_ERROR_MQTT5_CONNACK_TIMEOUT,
842 },
843 };
844 ASSERT_SUCCESS(
845 s_verify_simple_lifecycle_event_sequence(&test_context, expected_events, AWS_ARRAY_SIZE(expected_events)));
846
847 enum aws_mqtt5_client_state expected_states[] = {
848 AWS_MCS_CONNECTING,
849 AWS_MCS_MQTT_CONNECT,
850 AWS_MCS_CHANNEL_SHUTDOWN,
851 };
852 ASSERT_SUCCESS(aws_verify_client_state_sequence(&test_context, expected_states, AWS_ARRAY_SIZE(expected_states)));
853
854 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
855 aws_mqtt_library_clean_up();
856
857 return AWS_OP_SUCCESS;
858 }
859
860 AWS_TEST_CASE(mqtt5_client_direct_connect_connack_timeout, s_mqtt5_client_direct_connect_connack_timeout_fn)
861
862 struct aws_mqtt5_server_disconnect_test_context {
863 struct aws_mqtt5_client_mock_test_fixture *test_fixture;
864 bool disconnect_sent;
865 bool connack_sent;
866 };
867
868 static void s_server_disconnect_service_fn(
869 struct aws_mqtt5_server_mock_connection_context *mock_server,
870 void *user_data) {
871
872 struct aws_mqtt5_server_disconnect_test_context *test_context = user_data;
873 if (test_context->disconnect_sent || !test_context->connack_sent) {
874 return;
875 }
876
877 test_context->disconnect_sent = true;
878
879 struct aws_mqtt5_packet_disconnect_view disconnect = {
880 .reason_code = AWS_MQTT5_DRC_PACKET_TOO_LARGE,
881 };
882
883 aws_mqtt5_mock_server_send_packet(mock_server, AWS_MQTT5_PT_DISCONNECT, &disconnect);
884 }
885
886 static int s_aws_mqtt5_server_disconnect_on_connect(
887 void *packet,
888 struct aws_mqtt5_server_mock_connection_context *connection,
889 void *user_data) {
890
891 /*
892 * We intercept the CONNECT in order to correctly set the connack_sent test state. Otherwise we risk sometimes
893 * sending the DISCONNECT before the CONNACK
894 */
895 int result = aws_mqtt5_mock_server_handle_connect_always_succeed(packet, connection, user_data);
896
897 struct aws_mqtt5_server_disconnect_test_context *test_context = user_data;
898 test_context->connack_sent = true;
899
900 return result;
901 }
902
903 /* Connection test where we succeed and then the server sends a DISCONNECT */
904 static int s_mqtt5_client_direct_connect_from_server_disconnect_fn(struct aws_allocator *allocator, void *ctx) {
905 (void)ctx;
906
907 aws_mqtt_library_init(allocator);
908
909 struct mqtt5_client_test_options test_options;
910 aws_mqtt5_client_test_init_default_options(&test_options);
911
912 /* mock server sends a DISCONNECT packet back to the client after a successful CONNECTION establishment */
913 test_options.server_function_table.service_task_fn = s_server_disconnect_service_fn;
914 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] = s_aws_mqtt5_server_disconnect_on_connect;
915
916 struct aws_mqtt5_client_mock_test_fixture test_context;
917
918 struct aws_mqtt5_server_disconnect_test_context disconnect_context = {
919 .test_fixture = &test_context,
920 .disconnect_sent = false,
921 };
922
923 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
924 .client_options = &test_options.client_options,
925 .server_function_table = &test_options.server_function_table,
926 .mock_server_user_data = &disconnect_context,
927 };
928
929 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
930
931 struct aws_mqtt5_client *client = test_context.client;
932 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
933
934 s_wait_for_disconnection_lifecycle_event(&test_context);
935
936 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
937
938 aws_wait_for_stopped_lifecycle_event(&test_context);
939
940 struct aws_mqtt5_client_lifecycle_event expected_events[] = {
941 {
942 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
943 },
944 {
945 .event_type = AWS_MQTT5_CLET_CONNECTION_SUCCESS,
946 },
947 {
948 .event_type = AWS_MQTT5_CLET_DISCONNECTION,
949 .error_code = AWS_ERROR_MQTT5_DISCONNECT_RECEIVED,
950 },
951 };
952 ASSERT_SUCCESS(
953 s_verify_simple_lifecycle_event_sequence(&test_context, expected_events, AWS_ARRAY_SIZE(expected_events)));
954
955 enum aws_mqtt5_client_state expected_states[] = {
956 AWS_MCS_CONNECTING,
957 AWS_MCS_MQTT_CONNECT,
958 AWS_MCS_CONNECTED,
959 AWS_MCS_CHANNEL_SHUTDOWN,
960 };
961 ASSERT_SUCCESS(aws_verify_client_state_sequence(&test_context, expected_states, AWS_ARRAY_SIZE(expected_states)));
962
963 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
964 aws_mqtt_library_clean_up();
965
966 return AWS_OP_SUCCESS;
967 }
968
969 AWS_TEST_CASE(
970 mqtt5_client_direct_connect_from_server_disconnect,
971 s_mqtt5_client_direct_connect_from_server_disconnect_fn)
972
973 struct aws_mqtt5_client_test_wait_for_n_context {
974 size_t required_event_count;
975 struct aws_mqtt5_client_mock_test_fixture *test_fixture;
976 };
977
978 static size_t s_count_pingreqs(struct aws_mqtt5_client_mock_test_fixture *test_fixture) {
979 size_t ping_count = 0;
980 size_t packet_count = aws_array_list_length(&test_fixture->server_received_packets);
981 for (size_t i = 0; i < packet_count; ++i) {
982 struct aws_mqtt5_mock_server_packet_record *record = NULL;
983 aws_array_list_get_at_ptr(&test_fixture->server_received_packets, (void **)&record, i);
984
985 if (record->packet_type == AWS_MQTT5_PT_PINGREQ) {
986 ping_count++;
987 }
988 }
989
990 return ping_count;
991 }
992
993 static bool s_received_at_least_n_pingreqs(void *arg) {
994 struct aws_mqtt5_client_test_wait_for_n_context *ping_context = arg;
995 struct aws_mqtt5_client_mock_test_fixture *test_fixture = ping_context->test_fixture;
996
997 size_t ping_count = s_count_pingreqs(test_fixture);
998
999 return ping_count >= ping_context->required_event_count;
1000 }
1001
1002 static void s_wait_for_n_pingreqs(struct aws_mqtt5_client_test_wait_for_n_context *ping_context) {
1003
1004 struct aws_mqtt5_client_mock_test_fixture *test_context = ping_context->test_fixture;
1005 aws_mutex_lock(&test_context->lock);
1006 aws_condition_variable_wait_pred(
1007 &test_context->signal, &test_context->lock, s_received_at_least_n_pingreqs, ping_context);
1008 aws_mutex_unlock(&test_context->lock);
1009 }
1010
1011 #define TEST_PING_INTERVAL_MS 2000
1012
1013 static int s_verify_ping_sequence_timing(struct aws_mqtt5_client_mock_test_fixture *test_context) {
1014 aws_mutex_lock(&test_context->lock);
1015
1016 uint64_t last_packet_time = 0;
1017
1018 size_t packet_count = aws_array_list_length(&test_context->server_received_packets);
1019 for (size_t i = 0; i < packet_count; ++i) {
1020 struct aws_mqtt5_mock_server_packet_record *record = NULL;
1021 aws_array_list_get_at_ptr(&test_context->server_received_packets, (void **)&record, i);
1022
1023 if (i == 0) {
1024 ASSERT_INT_EQUALS(record->packet_type, AWS_MQTT5_PT_CONNECT);
1025 last_packet_time = record->timestamp;
1026 } else {
1027 if (record->packet_type == AWS_MQTT5_PT_PINGREQ) {
1028 uint64_t time_delta_ns = record->timestamp - last_packet_time;
1029 uint64_t time_delta_millis =
1030 aws_timestamp_convert(time_delta_ns, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_MILLIS, NULL);
1031
1032 ASSERT_TRUE(s_is_within_percentage_of(TEST_PING_INTERVAL_MS, time_delta_millis, .3));
1033
1034 last_packet_time = record->timestamp;
1035 }
1036 }
1037 }
1038 aws_mutex_unlock(&test_context->lock);
1039
1040 return AWS_OP_SUCCESS;
1041 }
1042
1043 static int s_aws_mqtt5_client_test_init_ping_test_connect_storage(
1044 struct aws_mqtt5_packet_connect_storage *storage,
1045 struct aws_allocator *allocator) {
1046
1047 struct aws_mqtt5_packet_connect_view connect_view = {
1048 .keep_alive_interval_seconds =
1049 (uint16_t)aws_timestamp_convert(TEST_PING_INTERVAL_MS, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_SECS, NULL),
1050 .client_id = aws_byte_cursor_from_string(g_default_client_id),
1051 .clean_start = true,
1052 };
1053
1054 return aws_mqtt5_packet_connect_storage_init(storage, allocator, &connect_view, NULL);
1055 }
1056
1057 /*
1058 * Test that the client sends pings at regular intervals to the server
1059 *
1060 * This is a low-keep-alive variant of the basic success test that waits for N pingreqs to be received by the server
1061 * and validates the approximate time intervals between them.
1062 */
1063 static int s_mqtt5_client_ping_sequence_fn(struct aws_allocator *allocator, void *ctx) {
1064 (void)ctx;
1065
1066 aws_mqtt_library_init(allocator);
1067
1068 struct mqtt5_client_test_options test_options;
1069 aws_mqtt5_client_test_init_default_options(&test_options);
1070
1071 /* fast keep alive in order keep tests reasonably short */
1072 uint16_t keep_alive_seconds =
1073 (uint16_t)aws_timestamp_convert(TEST_PING_INTERVAL_MS, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_SECS, NULL);
1074 test_options.connect_options.keep_alive_interval_seconds = keep_alive_seconds;
1075
1076 /* faster ping timeout */
1077 test_options.client_options.ping_timeout_ms = 750;
1078
1079 struct aws_mqtt5_client_mock_test_fixture test_context;
1080 struct aws_mqtt5_client_test_wait_for_n_context ping_context = {
1081 .required_event_count = 5,
1082 .test_fixture = &test_context,
1083 };
1084
1085 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
1086 .client_options = &test_options.client_options,
1087 .server_function_table = &test_options.server_function_table,
1088 .mock_server_user_data = &ping_context,
1089 };
1090
1091 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
1092
1093 struct aws_mqtt5_client *client = test_context.client;
1094 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
1095
1096 aws_wait_for_connected_lifecycle_event(&test_context);
1097 s_wait_for_n_pingreqs(&ping_context);
1098
1099 /*
1100 * There's a really unpleasant race condition where we can stop the client so fast (based on the mock
1101 * server receiving PINGREQs that the mock server's socket gets closed underneath it as it is trying to
1102 * write the PINGRESP back to the client, which in turn triggers channel shutdown where no further data
1103 * is read from the socket, so we never see the DISCONNECT that the client actually sent.
1104 *
1105 * We're not able to wait on the PINGRESP because we have no insight into when it's received. So for now,
1106 * we'll insert an artificial sleep before stopping the client. We should try and come up with a more
1107 * elegant solution.
1108 */
1109
1110 aws_thread_current_sleep(aws_timestamp_convert(1, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL));
1111
1112 struct aws_mqtt5_packet_disconnect_view disconnect_view = {
1113 .reason_code = AWS_MQTT5_DRC_NORMAL_DISCONNECTION,
1114 };
1115
1116 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, &disconnect_view, NULL));
1117
1118 aws_wait_for_stopped_lifecycle_event(&test_context);
1119 s_wait_for_mock_server_to_receive_disconnect_packet(&test_context);
1120
1121 struct aws_mqtt5_client_lifecycle_event expected_events[] = {
1122 {
1123 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
1124 },
1125 {
1126 .event_type = AWS_MQTT5_CLET_CONNECTION_SUCCESS,
1127 },
1128 {
1129 .event_type = AWS_MQTT5_CLET_DISCONNECTION,
1130 .error_code = AWS_ERROR_MQTT5_USER_REQUESTED_STOP,
1131 },
1132 {
1133 .event_type = AWS_MQTT5_CLET_STOPPED,
1134 },
1135 };
1136 ASSERT_SUCCESS(
1137 s_verify_simple_lifecycle_event_sequence(&test_context, expected_events, AWS_ARRAY_SIZE(expected_events)));
1138
1139 enum aws_mqtt5_client_state expected_states[] = {
1140 AWS_MCS_CONNECTING,
1141 AWS_MCS_MQTT_CONNECT,
1142 AWS_MCS_CONNECTED,
1143 AWS_MCS_CLEAN_DISCONNECT,
1144 AWS_MCS_CHANNEL_SHUTDOWN,
1145 AWS_MCS_STOPPED,
1146 };
1147 ASSERT_SUCCESS(aws_verify_client_state_sequence(&test_context, expected_states, AWS_ARRAY_SIZE(expected_states)));
1148
1149 struct aws_mqtt5_packet_connect_storage expected_connect_storage;
1150 ASSERT_SUCCESS(s_aws_mqtt5_client_test_init_ping_test_connect_storage(&expected_connect_storage, allocator));
1151
1152 struct aws_mqtt5_packet_disconnect_storage expected_disconnect_storage;
1153 ASSERT_SUCCESS(s_aws_mqtt5_client_test_init_default_disconnect_storage(&expected_disconnect_storage, allocator));
1154
1155 struct aws_mqtt5_mock_server_packet_record expected_packets[] = {
1156 {
1157 .packet_type = AWS_MQTT5_PT_CONNECT,
1158 .packet_storage = &expected_connect_storage,
1159 },
1160 {
1161 .packet_type = AWS_MQTT5_PT_PINGREQ,
1162 },
1163 {
1164 .packet_type = AWS_MQTT5_PT_PINGREQ,
1165 },
1166 {
1167 .packet_type = AWS_MQTT5_PT_PINGREQ,
1168 },
1169 {
1170 .packet_type = AWS_MQTT5_PT_PINGREQ,
1171 },
1172 {
1173 .packet_type = AWS_MQTT5_PT_PINGREQ,
1174 },
1175 {
1176 .packet_type = AWS_MQTT5_PT_DISCONNECT,
1177 .packet_storage = &expected_disconnect_storage,
1178 },
1179 };
1180 ASSERT_SUCCESS(
1181 aws_verify_received_packet_sequence(&test_context, expected_packets, AWS_ARRAY_SIZE(expected_packets)));
1182
1183 ASSERT_SUCCESS(s_verify_ping_sequence_timing(&test_context));
1184
1185 aws_mqtt5_packet_connect_storage_clean_up(&expected_connect_storage);
1186
1187 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
1188 aws_mqtt_library_clean_up();
1189
1190 return AWS_OP_SUCCESS;
1191 }
1192
1193 AWS_TEST_CASE(mqtt5_client_ping_sequence, s_mqtt5_client_ping_sequence_fn)
1194
1195 /*
1196 * Test that sending other data to the server pushes out the client ping timer
1197 *
1198 * This is a low-keep-alive variant of the basic success test that writes UNSUBSCRIBEs to the server at fast intervals.
1199 * Verify the server doesn't receive any PINGREQs until the right amount of time after we stop sending the CONNECTs to
1200 * it.
1201 *
1202 * TODO: we can't write this test until we have proper operation handling during CONNECTED state
1203 */
1204 static int s_mqtt5_client_ping_write_pushout_fn(struct aws_allocator *allocator, void *ctx) {
1205 (void)allocator;
1206 (void)ctx;
1207
1208 return AWS_OP_SUCCESS;
1209 }
1210
1211 AWS_TEST_CASE(mqtt5_client_ping_write_pushout, s_mqtt5_client_ping_write_pushout_fn)
1212
1213 #define TIMEOUT_TEST_PING_INTERVAL_MS ((uint64_t)10000)
1214
1215 static int s_verify_ping_timeout_interval(
1216 struct aws_mqtt5_client_mock_test_fixture *test_context,
1217 uint64_t expected_connected_time_ms) {
1218 aws_mutex_lock(&test_context->lock);
1219
1220 uint64_t connected_time = 0;
1221 uint64_t disconnected_time = 0;
1222
1223 size_t event_count = aws_array_list_length(&test_context->lifecycle_events);
1224 for (size_t i = 0; i < event_count; ++i) {
1225 struct aws_mqtt5_lifecycle_event_record *record = NULL;
1226 aws_array_list_get_at(&test_context->lifecycle_events, &record, i);
1227 if (connected_time == 0 && record->event.event_type == AWS_MQTT5_CLET_CONNECTION_SUCCESS) {
1228 connected_time = record->timestamp;
1229 }
1230
1231 if (disconnected_time == 0 && record->event.event_type == AWS_MQTT5_CLET_DISCONNECTION) {
1232 disconnected_time = record->timestamp;
1233 }
1234 }
1235
1236 aws_mutex_unlock(&test_context->lock);
1237
1238 ASSERT_TRUE(connected_time > 0 && disconnected_time > 0 && disconnected_time > connected_time);
1239
1240 uint64_t connected_interval_ms =
1241 aws_timestamp_convert(disconnected_time - connected_time, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_MILLIS, NULL);
1242
1243 ASSERT_TRUE(s_is_within_percentage_of(expected_connected_time_ms, connected_interval_ms, .3));
1244
1245 return AWS_OP_SUCCESS;
1246 }
1247
1248 /*
1249 * Test that not receiving a PINGRESP causes a disconnection
1250 *
1251 * This is a low-keep-alive variant of the basic success test where the mock server does not respond to a PINGREQ.
1252 */
1253 static int s_mqtt5_client_ping_timeout_fn(struct aws_allocator *allocator, void *ctx) {
1254 (void)ctx;
1255
1256 aws_mqtt_library_init(allocator);
1257
1258 struct mqtt5_client_test_options test_options;
1259 aws_mqtt5_client_test_init_default_options(&test_options);
1260
1261 /* fast keep alive in order keep tests reasonably short */
1262 uint16_t keep_alive_seconds =
1263 (uint16_t)aws_timestamp_convert(TIMEOUT_TEST_PING_INTERVAL_MS, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_SECS, NULL);
1264 test_options.connect_options.keep_alive_interval_seconds = keep_alive_seconds;
1265
1266 /* don't respond to PINGREQs */
1267 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PINGREQ] = NULL;
1268
1269 /* faster ping timeout */
1270 test_options.client_options.ping_timeout_ms = 5000;
1271
1272 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
1273 .client_options = &test_options.client_options,
1274 .server_function_table = &test_options.server_function_table,
1275 };
1276
1277 struct aws_mqtt5_client_mock_test_fixture test_context;
1278 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
1279
1280 struct aws_mqtt5_client *client = test_context.client;
1281 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
1282
1283 aws_wait_for_connected_lifecycle_event(&test_context);
1284 s_wait_for_disconnection_lifecycle_event(&test_context);
1285
1286 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
1287
1288 aws_wait_for_stopped_lifecycle_event(&test_context);
1289
1290 struct aws_mqtt5_client_lifecycle_event expected_events[] = {
1291 {
1292 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
1293 },
1294 {
1295 .event_type = AWS_MQTT5_CLET_CONNECTION_SUCCESS,
1296 },
1297 {
1298 .event_type = AWS_MQTT5_CLET_DISCONNECTION,
1299 .error_code = AWS_ERROR_MQTT5_PING_RESPONSE_TIMEOUT,
1300 },
1301 {
1302 .event_type = AWS_MQTT5_CLET_STOPPED,
1303 },
1304 };
1305 ASSERT_SUCCESS(
1306 s_verify_simple_lifecycle_event_sequence(&test_context, expected_events, AWS_ARRAY_SIZE(expected_events)));
1307
1308 uint64_t expected_connected_time_ms =
1309 TIMEOUT_TEST_PING_INTERVAL_MS + (uint64_t)test_context.client->config->ping_timeout_ms;
1310 ASSERT_SUCCESS(s_verify_ping_timeout_interval(&test_context, expected_connected_time_ms));
1311
1312 enum aws_mqtt5_client_state expected_states[] = {
1313 AWS_MCS_CONNECTING,
1314 AWS_MCS_MQTT_CONNECT,
1315 AWS_MCS_CONNECTED,
1316 AWS_MCS_CLEAN_DISCONNECT,
1317 AWS_MCS_CHANNEL_SHUTDOWN,
1318 };
1319 ASSERT_SUCCESS(aws_verify_client_state_sequence(&test_context, expected_states, AWS_ARRAY_SIZE(expected_states)));
1320
1321 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
1322 aws_mqtt_library_clean_up();
1323
1324 return AWS_OP_SUCCESS;
1325 }
1326
1327 AWS_TEST_CASE(mqtt5_client_ping_timeout, s_mqtt5_client_ping_timeout_fn)
1328
1329 /*
1330 * A variant of the basic ping timeout test that uses a timeout that is larger than the keep alive. Previously,
1331 * we forbid this because taken literally, it leads to broken behavior. We now clamp the ping timeout dynamically
1332 * based on the connection's established keep alive.
1333 */
1334 static int s_mqtt5_client_ping_timeout_with_keep_alive_conflict_fn(struct aws_allocator *allocator, void *ctx) {
1335 (void)ctx;
1336
1337 aws_mqtt_library_init(allocator);
1338
1339 struct mqtt5_client_test_options test_options;
1340 aws_mqtt5_client_test_init_default_options(&test_options);
1341
1342 /* fast keep alive in order keep tests reasonably short */
1343 uint16_t keep_alive_seconds =
1344 (uint16_t)aws_timestamp_convert(TIMEOUT_TEST_PING_INTERVAL_MS, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_SECS, NULL);
1345 test_options.connect_options.keep_alive_interval_seconds = keep_alive_seconds;
1346
1347 /* don't respond to PINGREQs */
1348 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PINGREQ] = NULL;
1349
1350 /* ping timeout slower than keep alive */
1351 test_options.client_options.ping_timeout_ms = 2 * TIMEOUT_TEST_PING_INTERVAL_MS;
1352
1353 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
1354 .client_options = &test_options.client_options,
1355 .server_function_table = &test_options.server_function_table,
1356 };
1357
1358 struct aws_mqtt5_client_mock_test_fixture test_context;
1359 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
1360
1361 struct aws_mqtt5_client *client = test_context.client;
1362 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
1363
1364 aws_wait_for_connected_lifecycle_event(&test_context);
1365 s_wait_for_disconnection_lifecycle_event(&test_context);
1366
1367 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
1368
1369 aws_wait_for_stopped_lifecycle_event(&test_context);
1370
1371 struct aws_mqtt5_client_lifecycle_event expected_events[] = {
1372 {
1373 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
1374 },
1375 {
1376 .event_type = AWS_MQTT5_CLET_CONNECTION_SUCCESS,
1377 },
1378 {
1379 .event_type = AWS_MQTT5_CLET_DISCONNECTION,
1380 .error_code = AWS_ERROR_MQTT5_PING_RESPONSE_TIMEOUT,
1381 },
1382 {
1383 .event_type = AWS_MQTT5_CLET_STOPPED,
1384 },
1385 };
1386 ASSERT_SUCCESS(
1387 s_verify_simple_lifecycle_event_sequence(&test_context, expected_events, AWS_ARRAY_SIZE(expected_events)));
1388
1389 uint64_t expected_connected_time_ms = 3 * TIMEOUT_TEST_PING_INTERVAL_MS / 2;
1390 ASSERT_SUCCESS(s_verify_ping_timeout_interval(&test_context, expected_connected_time_ms));
1391
1392 enum aws_mqtt5_client_state expected_states[] = {
1393 AWS_MCS_CONNECTING,
1394 AWS_MCS_MQTT_CONNECT,
1395 AWS_MCS_CONNECTED,
1396 AWS_MCS_CLEAN_DISCONNECT,
1397 AWS_MCS_CHANNEL_SHUTDOWN,
1398 };
1399 ASSERT_SUCCESS(aws_verify_client_state_sequence(&test_context, expected_states, AWS_ARRAY_SIZE(expected_states)));
1400
1401 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
1402 aws_mqtt_library_clean_up();
1403
1404 return AWS_OP_SUCCESS;
1405 }
1406
1407 AWS_TEST_CASE(
1408 mqtt5_client_ping_timeout_with_keep_alive_conflict,
1409 s_mqtt5_client_ping_timeout_with_keep_alive_conflict_fn)
1410
1411 /*
1412 * Set up a zero keep alive and verify no pings get sent over an interval of time.
1413 */
1414 static int s_mqtt5_client_disabled_keep_alive_fn(struct aws_allocator *allocator, void *ctx) {
1415 (void)ctx;
1416
1417 aws_mqtt_library_init(allocator);
1418
1419 struct mqtt5_client_test_options test_options;
1420 aws_mqtt5_client_test_init_default_options(&test_options);
1421
1422 /* no keep alive at all */
1423 test_options.connect_options.keep_alive_interval_seconds = 0;
1424
1425 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
1426 .client_options = &test_options.client_options,
1427 .server_function_table = &test_options.server_function_table,
1428 };
1429
1430 struct aws_mqtt5_client_mock_test_fixture test_context;
1431 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
1432
1433 struct aws_mqtt5_client *client = test_context.client;
1434 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
1435
1436 aws_wait_for_connected_lifecycle_event(&test_context);
1437
1438 uint16_t negotiated_keep_alive = 65535;
1439 aws_mutex_lock(&test_context.lock);
1440 size_t event_count = aws_array_list_length(&test_context.lifecycle_events);
1441 struct aws_mqtt5_lifecycle_event_record *record = NULL;
1442 aws_array_list_get_at(&test_context.lifecycle_events, &record, event_count - 1);
1443 ASSERT_TRUE(AWS_MQTT5_CLET_CONNECTION_SUCCESS == record->event.event_type);
1444 negotiated_keep_alive = record->settings_storage.server_keep_alive;
1445 aws_mutex_unlock(&test_context.lock);
1446
1447 ASSERT_INT_EQUALS(0, negotiated_keep_alive);
1448
1449 // zzz
1450 aws_thread_current_sleep(aws_timestamp_convert(5, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL));
1451
1452 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
1453
1454 aws_wait_for_stopped_lifecycle_event(&test_context);
1455
1456 // verify the mock server did not get any PINGREQs
1457 aws_mutex_lock(&test_context.lock);
1458 size_t pingreq_count = s_count_pingreqs(&test_context);
1459 aws_mutex_unlock(&test_context.lock);
1460 ASSERT_INT_EQUALS(0, pingreq_count);
1461
1462 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
1463 aws_mqtt_library_clean_up();
1464
1465 return AWS_OP_SUCCESS;
1466 }
1467
1468 AWS_TEST_CASE(mqtt5_client_disabled_keep_alive, s_mqtt5_client_disabled_keep_alive_fn)
1469
1470 struct aws_lifecycle_event_wait_context {
1471 enum aws_mqtt5_client_lifecycle_event_type type;
1472 size_t count;
1473 struct aws_mqtt5_client_mock_test_fixture *test_fixture;
1474 };
1475
1476 static bool s_received_at_least_n_events(void *arg) {
1477 struct aws_lifecycle_event_wait_context *context = arg;
1478 struct aws_mqtt5_client_mock_test_fixture *test_fixture = context->test_fixture;
1479
1480 size_t actual_count = 0;
1481 size_t event_count = aws_array_list_length(&test_fixture->lifecycle_events);
1482 for (size_t i = 0; i < event_count; ++i) {
1483 struct aws_mqtt5_lifecycle_event_record *record = NULL;
1484 aws_array_list_get_at(&test_fixture->lifecycle_events, &record, i);
1485
1486 if (record->event.event_type == context->type) {
1487 actual_count++;
1488 }
1489 }
1490
1491 return actual_count >= context->count;
1492 }
1493
1494 void aws_mqtt5_wait_for_n_lifecycle_events(
1495 struct aws_mqtt5_client_mock_test_fixture *test_context,
1496 enum aws_mqtt5_client_lifecycle_event_type type,
1497 size_t count) {
1498 struct aws_lifecycle_event_wait_context context = {
1499 .type = type,
1500 .count = count,
1501 .test_fixture = test_context,
1502 };
1503
1504 aws_mutex_lock(&test_context->lock);
1505 aws_condition_variable_wait_pred(
1506 &test_context->signal, &test_context->lock, s_received_at_least_n_events, &context);
1507 aws_mutex_unlock(&test_context->lock);
1508 }
1509
1510 int aws_verify_reconnection_exponential_backoff_timestamps(struct aws_mqtt5_client_mock_test_fixture *test_fixture) {
1511 aws_mutex_lock(&test_fixture->lock);
1512
1513 size_t event_count = aws_array_list_length(&test_fixture->lifecycle_events);
1514 uint64_t last_timestamp = 0;
1515 uint64_t expected_backoff = RECONNECT_TEST_MIN_BACKOFF;
1516
1517 for (size_t i = 0; i < event_count; ++i) {
1518 struct aws_mqtt5_lifecycle_event_record *record = NULL;
1519 aws_array_list_get_at(&test_fixture->lifecycle_events, &record, i);
1520
1521 if (record->event.event_type == AWS_MQTT5_CLET_CONNECTION_FAILURE) {
1522 if (last_timestamp == 0) {
1523 last_timestamp = record->timestamp;
1524 } else {
1525 uint64_t time_diff = aws_timestamp_convert(
1526 record->timestamp - last_timestamp, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_MILLIS, NULL);
1527
1528 if (!s_check_time_lower_bound(expected_backoff, time_diff)) {
1529 return AWS_OP_ERR;
1530 }
1531
1532 expected_backoff = aws_min_u64(expected_backoff * 2, RECONNECT_TEST_MAX_BACKOFF);
1533 last_timestamp = record->timestamp;
1534 }
1535 }
1536 }
1537
1538 aws_mutex_unlock(&test_fixture->lock);
1539
1540 return AWS_OP_SUCCESS;
1541 }
1542
1543 /*
1544 * Always-fail variant that waits for 6 connection failures and then checks the timestamps between them against
1545 * what we'd expect with exponential backoff and no jitter
1546 */
1547 static int s_mqtt5_client_reconnect_failure_backoff_fn(struct aws_allocator *allocator, void *ctx) {
1548 (void)ctx;
1549
1550 aws_mqtt_library_init(allocator);
1551
1552 struct mqtt5_client_test_options test_options;
1553 aws_mqtt5_client_test_init_default_options(&test_options);
1554
1555 /* backoff delay sequence: 500, 1000, 2000, 4000, 5000, ... */
1556 test_options.client_options.retry_jitter_mode = AWS_EXPONENTIAL_BACKOFF_JITTER_NONE;
1557 test_options.client_options.min_reconnect_delay_ms = RECONNECT_TEST_MIN_BACKOFF;
1558 test_options.client_options.max_reconnect_delay_ms = RECONNECT_TEST_MAX_BACKOFF;
1559 test_options.client_options.min_connected_time_to_reset_reconnect_delay_ms = RECONNECT_TEST_BACKOFF_RESET_DELAY;
1560
1561 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] =
1562 aws_mqtt5_mock_server_handle_connect_always_fail;
1563
1564 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
1565 .client_options = &test_options.client_options,
1566 .server_function_table = &test_options.server_function_table,
1567 };
1568
1569 struct aws_mqtt5_client_mock_test_fixture test_context;
1570 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
1571
1572 struct aws_mqtt5_client *client = test_context.client;
1573
1574 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
1575
1576 aws_mqtt5_wait_for_n_lifecycle_events(&test_context, AWS_MQTT5_CLET_CONNECTION_FAILURE, 6);
1577
1578 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
1579
1580 aws_wait_for_stopped_lifecycle_event(&test_context);
1581
1582 /* 6 (connecting, connection failure) pairs */
1583 struct aws_mqtt5_client_lifecycle_event expected_events[] = {
1584 {
1585 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
1586 },
1587 {
1588 .event_type = AWS_MQTT5_CLET_CONNECTION_FAILURE,
1589 .error_code = AWS_ERROR_MQTT5_CONNACK_CONNECTION_REFUSED,
1590 },
1591 {
1592 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
1593 },
1594 {
1595 .event_type = AWS_MQTT5_CLET_CONNECTION_FAILURE,
1596 .error_code = AWS_ERROR_MQTT5_CONNACK_CONNECTION_REFUSED,
1597 },
1598 {
1599 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
1600 },
1601 {
1602 .event_type = AWS_MQTT5_CLET_CONNECTION_FAILURE,
1603 .error_code = AWS_ERROR_MQTT5_CONNACK_CONNECTION_REFUSED,
1604 },
1605 {
1606 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
1607 },
1608 {
1609 .event_type = AWS_MQTT5_CLET_CONNECTION_FAILURE,
1610 .error_code = AWS_ERROR_MQTT5_CONNACK_CONNECTION_REFUSED,
1611 },
1612 {
1613 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
1614 },
1615 {
1616 .event_type = AWS_MQTT5_CLET_CONNECTION_FAILURE,
1617 .error_code = AWS_ERROR_MQTT5_CONNACK_CONNECTION_REFUSED,
1618 },
1619 {
1620 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
1621 },
1622 {
1623 .event_type = AWS_MQTT5_CLET_CONNECTION_FAILURE,
1624 .error_code = AWS_ERROR_MQTT5_CONNACK_CONNECTION_REFUSED,
1625 },
1626 };
1627 ASSERT_SUCCESS(
1628 s_verify_simple_lifecycle_event_sequence(&test_context, expected_events, AWS_ARRAY_SIZE(expected_events)));
1629
1630 ASSERT_SUCCESS(aws_verify_reconnection_exponential_backoff_timestamps(&test_context));
1631
1632 /* 6 (connecting, mqtt_connect, channel_shutdown, pending_reconnect) tuples (minus the final pending_reconnect) */
1633 enum aws_mqtt5_client_state expected_states[] = {
1634 AWS_MCS_CONNECTING, AWS_MCS_MQTT_CONNECT, AWS_MCS_CHANNEL_SHUTDOWN, AWS_MCS_PENDING_RECONNECT,
1635 AWS_MCS_CONNECTING, AWS_MCS_MQTT_CONNECT, AWS_MCS_CHANNEL_SHUTDOWN, AWS_MCS_PENDING_RECONNECT,
1636 AWS_MCS_CONNECTING, AWS_MCS_MQTT_CONNECT, AWS_MCS_CHANNEL_SHUTDOWN, AWS_MCS_PENDING_RECONNECT,
1637 AWS_MCS_CONNECTING, AWS_MCS_MQTT_CONNECT, AWS_MCS_CHANNEL_SHUTDOWN, AWS_MCS_PENDING_RECONNECT,
1638 AWS_MCS_CONNECTING, AWS_MCS_MQTT_CONNECT, AWS_MCS_CHANNEL_SHUTDOWN, AWS_MCS_PENDING_RECONNECT,
1639 AWS_MCS_CONNECTING, AWS_MCS_MQTT_CONNECT, AWS_MCS_CHANNEL_SHUTDOWN,
1640 };
1641 ASSERT_SUCCESS(aws_verify_client_state_sequence(&test_context, expected_states, AWS_ARRAY_SIZE(expected_states)));
1642 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
1643 aws_mqtt_library_clean_up();
1644
1645 return AWS_OP_SUCCESS;
1646 }
1647
1648 AWS_TEST_CASE(mqtt5_client_reconnect_failure_backoff, s_mqtt5_client_reconnect_failure_backoff_fn)
1649
1650 int aws_mqtt5_mock_server_handle_connect_succeed_on_nth(
1651 void *packet,
1652 struct aws_mqtt5_server_mock_connection_context *connection,
1653 void *user_data) {
1654 (void)packet;
1655
1656 struct aws_mqtt5_mock_server_reconnect_state *context = user_data;
1657
1658 struct aws_mqtt5_packet_connack_view connack_view;
1659 AWS_ZERO_STRUCT(connack_view);
1660
1661 if (context->connection_attempts == context->required_connection_count_threshold) {
1662 connack_view.reason_code = AWS_MQTT5_CRC_SUCCESS;
1663 aws_high_res_clock_get_ticks(&context->connect_timestamp);
1664 } else {
1665 connack_view.reason_code = AWS_MQTT5_CRC_NOT_AUTHORIZED;
1666 }
1667
1668 ++context->connection_attempts;
1669
1670 return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_CONNACK, &connack_view);
1671 }
1672
1673 static void s_aws_mqtt5_mock_server_disconnect_after_n_ms(
1674 struct aws_mqtt5_server_mock_connection_context *mock_server,
1675 void *user_data) {
1676
1677 struct aws_mqtt5_mock_server_reconnect_state *context = user_data;
1678 if (context->connect_timestamp == 0) {
1679 return;
1680 }
1681
1682 uint64_t now = 0;
1683 aws_high_res_clock_get_ticks(&now);
1684
1685 if (now < context->connect_timestamp) {
1686 return;
1687 }
1688
1689 uint64_t elapsed_ms =
1690 aws_timestamp_convert(now - context->connect_timestamp, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_MILLIS, NULL);
1691 if (elapsed_ms > context->successful_connection_disconnect_delay_ms) {
1692
1693 struct aws_mqtt5_packet_disconnect_view disconnect = {
1694 .reason_code = AWS_MQTT5_DRC_PACKET_TOO_LARGE,
1695 };
1696
1697 aws_mqtt5_mock_server_send_packet(mock_server, AWS_MQTT5_PT_DISCONNECT, &disconnect);
1698 context->connect_timestamp = 0;
1699 }
1700 }
1701
1702 static int s_verify_reconnection_after_success_used_backoff(
1703 struct aws_mqtt5_client_mock_test_fixture *test_fixture,
1704 uint64_t expected_reconnect_delay_ms) {
1705
1706 aws_mutex_lock(&test_fixture->lock);
1707
1708 size_t event_count = aws_array_list_length(&test_fixture->lifecycle_events);
1709
1710 uint64_t disconnect_after_success_timestamp = 0;
1711 uint64_t reconnect_failure_after_disconnect_timestamp = 0;
1712
1713 for (size_t i = 0; i < event_count; ++i) {
1714 struct aws_mqtt5_lifecycle_event_record *record = NULL;
1715 aws_array_list_get_at(&test_fixture->lifecycle_events, &record, i);
1716
1717 if (record->event.event_type == AWS_MQTT5_CLET_DISCONNECTION) {
1718 ASSERT_INT_EQUALS(0, disconnect_after_success_timestamp);
1719 disconnect_after_success_timestamp = record->timestamp;
1720 } else if (record->event.event_type == AWS_MQTT5_CLET_CONNECTION_FAILURE) {
1721 if (reconnect_failure_after_disconnect_timestamp == 0 && disconnect_after_success_timestamp > 0) {
1722 reconnect_failure_after_disconnect_timestamp = record->timestamp;
1723 }
1724 }
1725 }
1726
1727 aws_mutex_unlock(&test_fixture->lock);
1728
1729 ASSERT_TRUE(disconnect_after_success_timestamp > 0 && reconnect_failure_after_disconnect_timestamp > 0);
1730
1731 uint64_t post_success_reconnect_time_ms = aws_timestamp_convert(
1732 reconnect_failure_after_disconnect_timestamp - disconnect_after_success_timestamp,
1733 AWS_TIMESTAMP_NANOS,
1734 AWS_TIMESTAMP_MILLIS,
1735 NULL);
1736
1737 /* The reconnect delay should be at least the expected backoff time.
1738 * Meanwhile, to make sure the backoff time is reset to minimal properly, we also would like to validate the upper
1739 * bound of the reconnect time. With function `s_validate_time_interval()`, we used a 30% margin and a minimal of
1740 * AWS_MQTT5_TESTING_DELAY_NS for system delay to validate the upper bound.
1741 */
1742 if (!s_check_time_lower_bound(expected_reconnect_delay_ms, post_success_reconnect_time_ms) &&
1743 !s_validate_time_interval(expected_reconnect_delay_ms, post_success_reconnect_time_ms, .3)) {
1744 return AWS_OP_ERR;
1745 }
1746
1747 return AWS_OP_SUCCESS;
1748 }
1749
1750 /*
1751 * Fail-until-max-backoff variant, followed by a success that then quickly disconnects. Verify the next reconnect
1752 * attempt still uses the maximum backoff because we weren't connected long enough to reset it.
1753 */
1754 static int s_mqtt5_client_reconnect_backoff_insufficient_reset_fn(struct aws_allocator *allocator, void *ctx) {
1755 (void)ctx;
1756
1757 aws_mqtt_library_init(allocator);
1758
1759 struct mqtt5_client_test_options test_options;
1760 aws_mqtt5_client_test_init_default_options(&test_options);
1761
1762 struct aws_mqtt5_mock_server_reconnect_state mock_server_state = {
1763 .required_connection_count_threshold = 6,
1764 /* quick disconnect should not reset reconnect delay */
1765 .successful_connection_disconnect_delay_ms = RECONNECT_TEST_BACKOFF_RESET_DELAY / 5,
1766 };
1767
1768 /* backoff delay sequence: 500, 1000, 2000, 4000, 5000, ... */
1769 test_options.client_options.retry_jitter_mode = AWS_EXPONENTIAL_BACKOFF_JITTER_NONE;
1770 test_options.client_options.min_reconnect_delay_ms = RECONNECT_TEST_MIN_BACKOFF;
1771 test_options.client_options.max_reconnect_delay_ms = RECONNECT_TEST_MAX_BACKOFF;
1772 test_options.client_options.min_connected_time_to_reset_reconnect_delay_ms = RECONNECT_TEST_BACKOFF_RESET_DELAY;
1773
1774 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] =
1775 aws_mqtt5_mock_server_handle_connect_succeed_on_nth;
1776 test_options.server_function_table.service_task_fn = s_aws_mqtt5_mock_server_disconnect_after_n_ms;
1777
1778 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
1779 .client_options = &test_options.client_options,
1780 .server_function_table = &test_options.server_function_table,
1781 .mock_server_user_data = &mock_server_state,
1782 };
1783
1784 struct aws_mqtt5_client_mock_test_fixture test_context;
1785 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
1786
1787 struct aws_mqtt5_client *client = test_context.client;
1788
1789 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
1790
1791 aws_mqtt5_wait_for_n_lifecycle_events(&test_context, AWS_MQTT5_CLET_CONNECTION_FAILURE, 7);
1792
1793 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
1794
1795 aws_wait_for_stopped_lifecycle_event(&test_context);
1796
1797 /* 6 (connecting, connection failure) pairs, followed by a successful connection, then a disconnect and reconnect */
1798 struct aws_mqtt5_client_lifecycle_event expected_events[] = {
1799 {
1800 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
1801 },
1802 {
1803 .event_type = AWS_MQTT5_CLET_CONNECTION_FAILURE,
1804 .error_code = AWS_ERROR_MQTT5_CONNACK_CONNECTION_REFUSED,
1805 },
1806 {
1807 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
1808 },
1809 {
1810 .event_type = AWS_MQTT5_CLET_CONNECTION_FAILURE,
1811 .error_code = AWS_ERROR_MQTT5_CONNACK_CONNECTION_REFUSED,
1812 },
1813 {
1814 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
1815 },
1816 {
1817 .event_type = AWS_MQTT5_CLET_CONNECTION_FAILURE,
1818 .error_code = AWS_ERROR_MQTT5_CONNACK_CONNECTION_REFUSED,
1819 },
1820 {
1821 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
1822 },
1823 {
1824 .event_type = AWS_MQTT5_CLET_CONNECTION_FAILURE,
1825 .error_code = AWS_ERROR_MQTT5_CONNACK_CONNECTION_REFUSED,
1826 },
1827 {
1828 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
1829 },
1830 {
1831 .event_type = AWS_MQTT5_CLET_CONNECTION_FAILURE,
1832 .error_code = AWS_ERROR_MQTT5_CONNACK_CONNECTION_REFUSED,
1833 },
1834 {
1835 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
1836 },
1837 {
1838 .event_type = AWS_MQTT5_CLET_CONNECTION_FAILURE,
1839 .error_code = AWS_ERROR_MQTT5_CONNACK_CONNECTION_REFUSED,
1840 },
1841 {
1842 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
1843 },
1844 {
1845 .event_type = AWS_MQTT5_CLET_CONNECTION_SUCCESS,
1846 },
1847 {
1848 .event_type = AWS_MQTT5_CLET_DISCONNECTION,
1849 .error_code = AWS_ERROR_MQTT5_DISCONNECT_RECEIVED,
1850 },
1851 {
1852 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
1853 },
1854 {
1855 .event_type = AWS_MQTT5_CLET_CONNECTION_FAILURE,
1856 .error_code = AWS_ERROR_MQTT5_CONNACK_CONNECTION_REFUSED,
1857 },
1858 };
1859 ASSERT_SUCCESS(
1860 s_verify_simple_lifecycle_event_sequence(&test_context, expected_events, AWS_ARRAY_SIZE(expected_events)));
1861
1862 ASSERT_SUCCESS(s_verify_reconnection_after_success_used_backoff(&test_context, RECONNECT_TEST_MAX_BACKOFF));
1863 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
1864 aws_mqtt_library_clean_up();
1865
1866 return AWS_OP_SUCCESS;
1867 }
1868
1869 AWS_TEST_CASE(mqtt5_client_reconnect_backoff_insufficient_reset, s_mqtt5_client_reconnect_backoff_insufficient_reset_fn)
1870
1871 /*
1872 * Fail-until-max-backoff variant, followed by a success that disconnects after enough time has passed that the backoff
1873 * should be reset. Verify that the next reconnect is back to using the minimum backoff value.
1874 */
1875 static int s_mqtt5_client_reconnect_backoff_sufficient_reset_fn(struct aws_allocator *allocator, void *ctx) {
1876 (void)ctx;
1877 aws_mqtt_library_init(allocator);
1878
1879 struct mqtt5_client_test_options test_options;
1880 aws_mqtt5_client_test_init_default_options(&test_options);
1881
1882 struct aws_mqtt5_mock_server_reconnect_state mock_server_state = {
1883 .required_connection_count_threshold = 6,
1884 /* slow disconnect should reset reconnect delay */
1885 .successful_connection_disconnect_delay_ms = RECONNECT_TEST_BACKOFF_RESET_DELAY * 2,
1886 };
1887
1888 /* backoff delay sequence: 500, 1000, 2000, 4000, 5000, ... */
1889 test_options.client_options.retry_jitter_mode = AWS_EXPONENTIAL_BACKOFF_JITTER_NONE;
1890 test_options.client_options.min_reconnect_delay_ms = RECONNECT_TEST_MIN_BACKOFF;
1891 test_options.client_options.max_reconnect_delay_ms = RECONNECT_TEST_MAX_BACKOFF;
1892 test_options.client_options.min_connected_time_to_reset_reconnect_delay_ms = RECONNECT_TEST_BACKOFF_RESET_DELAY;
1893
1894 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] =
1895 aws_mqtt5_mock_server_handle_connect_succeed_on_nth;
1896 test_options.server_function_table.service_task_fn = s_aws_mqtt5_mock_server_disconnect_after_n_ms;
1897
1898 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
1899 .client_options = &test_options.client_options,
1900 .server_function_table = &test_options.server_function_table,
1901 .mock_server_user_data = &mock_server_state,
1902 };
1903
1904 struct aws_mqtt5_client_mock_test_fixture test_context;
1905 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
1906
1907 struct aws_mqtt5_client *client = test_context.client;
1908
1909 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
1910
1911 aws_mqtt5_wait_for_n_lifecycle_events(&test_context, AWS_MQTT5_CLET_CONNECTION_FAILURE, 7);
1912
1913 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
1914
1915 aws_wait_for_stopped_lifecycle_event(&test_context);
1916
1917 /* 6 (connecting, connection failure) pairs, followed by a successful connection, then a disconnect and reconnect */
1918 struct aws_mqtt5_client_lifecycle_event expected_events[] = {
1919 {
1920 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
1921 },
1922 {
1923 .event_type = AWS_MQTT5_CLET_CONNECTION_FAILURE,
1924 .error_code = AWS_ERROR_MQTT5_CONNACK_CONNECTION_REFUSED,
1925 },
1926 {
1927 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
1928 },
1929 {
1930 .event_type = AWS_MQTT5_CLET_CONNECTION_FAILURE,
1931 .error_code = AWS_ERROR_MQTT5_CONNACK_CONNECTION_REFUSED,
1932 },
1933 {
1934 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
1935 },
1936 {
1937 .event_type = AWS_MQTT5_CLET_CONNECTION_FAILURE,
1938 .error_code = AWS_ERROR_MQTT5_CONNACK_CONNECTION_REFUSED,
1939 },
1940 {
1941 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
1942 },
1943 {
1944 .event_type = AWS_MQTT5_CLET_CONNECTION_FAILURE,
1945 .error_code = AWS_ERROR_MQTT5_CONNACK_CONNECTION_REFUSED,
1946 },
1947 {
1948 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
1949 },
1950 {
1951 .event_type = AWS_MQTT5_CLET_CONNECTION_FAILURE,
1952 .error_code = AWS_ERROR_MQTT5_CONNACK_CONNECTION_REFUSED,
1953 },
1954 {
1955 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
1956 },
1957 {
1958 .event_type = AWS_MQTT5_CLET_CONNECTION_FAILURE,
1959 .error_code = AWS_ERROR_MQTT5_CONNACK_CONNECTION_REFUSED,
1960 },
1961 {
1962 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
1963 },
1964 {
1965 .event_type = AWS_MQTT5_CLET_CONNECTION_SUCCESS,
1966 },
1967 {
1968 .event_type = AWS_MQTT5_CLET_DISCONNECTION,
1969 .error_code = AWS_ERROR_MQTT5_DISCONNECT_RECEIVED,
1970 },
1971 {
1972 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
1973 },
1974 {
1975 .event_type = AWS_MQTT5_CLET_CONNECTION_FAILURE,
1976 .error_code = AWS_ERROR_MQTT5_CONNACK_CONNECTION_REFUSED,
1977 },
1978 };
1979 ASSERT_SUCCESS(
1980 s_verify_simple_lifecycle_event_sequence(&test_context, expected_events, AWS_ARRAY_SIZE(expected_events)));
1981
1982 ASSERT_SUCCESS(s_verify_reconnection_after_success_used_backoff(&test_context, RECONNECT_TEST_MIN_BACKOFF));
1983 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
1984 aws_mqtt_library_clean_up();
1985
1986 return AWS_OP_SUCCESS;
1987 }
1988
1989 AWS_TEST_CASE(mqtt5_client_reconnect_backoff_sufficient_reset, s_mqtt5_client_reconnect_backoff_sufficient_reset_fn)
1990
1991 static const char s_topic_filter1[] = "some/topic/but/letsmakeit/longer/soIcanfailpacketsizetests/+";
1992
1993 static struct aws_mqtt5_subscription_view s_subscriptions[] = {
1994 {
1995 .topic_filter =
1996 {
1997 .ptr = (uint8_t *)s_topic_filter1,
1998 .len = AWS_ARRAY_SIZE(s_topic_filter1) - 1,
1999 },
2000 .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE,
2001 .no_local = false,
2002 .retain_as_published = false,
2003 .retain_handling_type = AWS_MQTT5_RHT_SEND_ON_SUBSCRIBE,
2004 },
2005 };
2006
2007 static enum aws_mqtt5_suback_reason_code s_suback_reason_codes[] = {
2008 AWS_MQTT5_SARC_GRANTED_QOS_1,
2009 };
2010
2011 void s_aws_mqtt5_subscribe_complete_fn(
2012 const struct aws_mqtt5_packet_suback_view *suback,
2013 int error_code,
2014 void *complete_ctx) {
2015
2016 (void)suback;
2017 (void)error_code;
2018
2019 struct aws_mqtt5_client_mock_test_fixture *test_context = complete_ctx;
2020
2021 aws_mutex_lock(&test_context->lock);
2022 test_context->subscribe_complete = true;
2023 aws_mutex_unlock(&test_context->lock);
2024 aws_condition_variable_notify_all(&test_context->signal);
2025 }
2026
2027 static bool s_received_suback(void *arg) {
2028 struct aws_mqtt5_client_mock_test_fixture *test_context = arg;
2029
2030 return test_context->subscribe_complete;
2031 }
2032
2033 static void s_wait_for_suback_received(struct aws_mqtt5_client_mock_test_fixture *test_context) {
2034 aws_mutex_lock(&test_context->lock);
2035 aws_condition_variable_wait_pred(&test_context->signal, &test_context->lock, s_received_suback, test_context);
2036 aws_mutex_unlock(&test_context->lock);
2037 }
2038
2039 int aws_mqtt5_server_send_suback_on_subscribe(
2040 void *packet,
2041 struct aws_mqtt5_server_mock_connection_context *connection,
2042 void *user_data) {
2043 (void)packet;
2044 (void)user_data;
2045
2046 struct aws_mqtt5_packet_subscribe_view *subscribe_view = packet;
2047
2048 struct aws_mqtt5_packet_suback_view suback_view = {
2049 .packet_id = subscribe_view->packet_id,
2050 .reason_code_count = AWS_ARRAY_SIZE(s_suback_reason_codes),
2051 .reason_codes = s_suback_reason_codes,
2052 };
2053
2054 return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_SUBACK, &suback_view);
2055 }
2056
2057 /* Connection test where we succeed, send a SUBSCRIBE, and wait for a SUBACK */
2058 static int s_mqtt5_client_subscribe_success_fn(struct aws_allocator *allocator, void *ctx) {
2059 (void)ctx;
2060
2061 aws_mqtt_library_init(allocator);
2062
2063 struct mqtt5_client_test_options test_options;
2064 aws_mqtt5_client_test_init_default_options(&test_options);
2065
2066 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] =
2067 aws_mqtt5_server_send_suback_on_subscribe;
2068
2069 struct aws_mqtt5_client_mock_test_fixture test_context;
2070
2071 struct aws_mqtt5_server_disconnect_test_context disconnect_context = {
2072 .test_fixture = &test_context,
2073 .disconnect_sent = false,
2074 };
2075
2076 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
2077 .client_options = &test_options.client_options,
2078 .server_function_table = &test_options.server_function_table,
2079 .mock_server_user_data = &disconnect_context,
2080 };
2081
2082 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
2083
2084 struct aws_mqtt5_client *client = test_context.client;
2085 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
2086
2087 aws_wait_for_connected_lifecycle_event(&test_context);
2088
2089 struct aws_mqtt5_packet_subscribe_view subscribe_view = {
2090 .subscriptions = s_subscriptions,
2091 .subscription_count = AWS_ARRAY_SIZE(s_subscriptions),
2092 };
2093
2094 struct aws_mqtt5_subscribe_completion_options completion_options = {
2095 .completion_callback = s_aws_mqtt5_subscribe_complete_fn,
2096 .completion_user_data = &test_context,
2097 };
2098 aws_mqtt5_client_subscribe(client, &subscribe_view, &completion_options);
2099
2100 s_wait_for_suback_received(&test_context);
2101
2102 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
2103
2104 aws_wait_for_stopped_lifecycle_event(&test_context);
2105
2106 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
2107 aws_mqtt_library_clean_up();
2108
2109 return AWS_OP_SUCCESS;
2110 }
2111
2112 AWS_TEST_CASE(mqtt5_client_subscribe_success, s_mqtt5_client_subscribe_success_fn)
2113
2114 static int s_aws_mqtt5_mock_server_handle_connect_succeed_maximum_packet_size(
2115 void *packet,
2116 struct aws_mqtt5_server_mock_connection_context *connection,
2117 void *user_data) {
2118 (void)packet;
2119 (void)user_data;
2120
2121 struct aws_mqtt5_packet_connack_view connack_view;
2122 AWS_ZERO_STRUCT(connack_view);
2123
2124 uint32_t maximum_packet_size = 50;
2125
2126 connack_view.reason_code = AWS_MQTT5_CRC_SUCCESS;
2127 connack_view.maximum_packet_size = &maximum_packet_size;
2128
2129 return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_CONNACK, &connack_view);
2130 }
2131
2132 void s_aws_mqtt5_subscribe_complete_packet_size_too_small_fn(
2133 const struct aws_mqtt5_packet_suback_view *suback,
2134 int error_code,
2135 void *complete_ctx) {
2136
2137 AWS_FATAL_ASSERT(suback == NULL);
2138 AWS_FATAL_ASSERT(AWS_ERROR_MQTT5_PACKET_VALIDATION == error_code);
2139
2140 struct aws_mqtt5_client_mock_test_fixture *test_context = complete_ctx;
2141
2142 aws_mutex_lock(&test_context->lock);
2143 test_context->subscribe_complete = true;
2144 aws_mutex_unlock(&test_context->lock);
2145 aws_condition_variable_notify_all(&test_context->signal);
2146 }
2147
2148 static int s_mqtt5_client_subscribe_fail_packet_too_big_fn(struct aws_allocator *allocator, void *ctx) {
2149 (void)ctx;
2150
2151 aws_mqtt_library_init(allocator);
2152
2153 struct mqtt5_client_test_options test_options;
2154 aws_mqtt5_client_test_init_default_options(&test_options);
2155
2156 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] =
2157 s_aws_mqtt5_mock_server_handle_connect_succeed_maximum_packet_size;
2158
2159 struct aws_mqtt5_client_mock_test_fixture test_context;
2160
2161 struct aws_mqtt5_server_disconnect_test_context disconnect_context = {
2162 .test_fixture = &test_context,
2163 .disconnect_sent = false,
2164 };
2165
2166 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
2167 .client_options = &test_options.client_options,
2168 .server_function_table = &test_options.server_function_table,
2169 .mock_server_user_data = &disconnect_context,
2170 };
2171
2172 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
2173
2174 struct aws_mqtt5_client *client = test_context.client;
2175 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
2176
2177 aws_wait_for_connected_lifecycle_event(&test_context);
2178
2179 struct aws_mqtt5_packet_subscribe_view subscribe_view = {
2180 .subscriptions = s_subscriptions,
2181 .subscription_count = AWS_ARRAY_SIZE(s_subscriptions),
2182 };
2183
2184 struct aws_mqtt5_subscribe_completion_options completion_options = {
2185 .completion_callback = s_aws_mqtt5_subscribe_complete_packet_size_too_small_fn,
2186 .completion_user_data = &test_context,
2187 };
2188 aws_mqtt5_client_subscribe(client, &subscribe_view, &completion_options);
2189
2190 s_wait_for_suback_received(&test_context);
2191
2192 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
2193
2194 aws_wait_for_stopped_lifecycle_event(&test_context);
2195
2196 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
2197 aws_mqtt_library_clean_up();
2198
2199 return AWS_OP_SUCCESS;
2200 }
2201
2202 AWS_TEST_CASE(mqtt5_client_subscribe_fail_packet_too_big, s_mqtt5_client_subscribe_fail_packet_too_big_fn)
2203
2204 static void s_aws_mqtt5_disconnect_failure_completion_fn(int error_code, void *complete_ctx) {
2205 AWS_FATAL_ASSERT(error_code == AWS_ERROR_MQTT5_PACKET_VALIDATION);
2206
2207 s_on_disconnect_completion(error_code, complete_ctx);
2208 }
2209
2210 static int s_mqtt5_client_disconnect_fail_packet_too_big_fn(struct aws_allocator *allocator, void *ctx) {
2211 (void)ctx;
2212
2213 aws_mqtt_library_init(allocator);
2214
2215 struct mqtt5_client_test_options test_options;
2216 aws_mqtt5_client_test_init_default_options(&test_options);
2217
2218 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] =
2219 s_aws_mqtt5_mock_server_handle_connect_succeed_maximum_packet_size;
2220
2221 struct aws_mqtt5_client_mock_test_fixture test_context;
2222
2223 struct aws_mqtt5_server_disconnect_test_context disconnect_context = {
2224 .test_fixture = &test_context,
2225 .disconnect_sent = false,
2226 };
2227
2228 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
2229 .client_options = &test_options.client_options,
2230 .server_function_table = &test_options.server_function_table,
2231 .mock_server_user_data = &disconnect_context,
2232 };
2233
2234 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
2235
2236 struct aws_mqtt5_client *client = test_context.client;
2237 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
2238
2239 aws_wait_for_connected_lifecycle_event(&test_context);
2240
2241 struct aws_byte_cursor long_reason_string_cursor = aws_byte_cursor_from_c_str(
2242 "Not valid because it includes the 0-terminator but we don't check utf-8 so who cares");
2243
2244 struct aws_mqtt5_packet_disconnect_view disconnect_view = {
2245 .reason_string = &long_reason_string_cursor,
2246 };
2247
2248 struct aws_mqtt5_disconnect_completion_options completion_options = {
2249 .completion_callback = s_aws_mqtt5_disconnect_failure_completion_fn,
2250 .completion_user_data = &test_context,
2251 };
2252
2253 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, &disconnect_view, &completion_options));
2254
2255 s_wait_for_disconnect_completion(&test_context);
2256 aws_wait_for_stopped_lifecycle_event(&test_context);
2257
2258 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
2259 aws_mqtt_library_clean_up();
2260
2261 return AWS_OP_SUCCESS;
2262 }
2263
2264 AWS_TEST_CASE(mqtt5_client_disconnect_fail_packet_too_big, s_mqtt5_client_disconnect_fail_packet_too_big_fn)
2265
2266 static uint8_t s_topic[] = "Hello/world";
2267
2268 #define RECEIVE_MAXIMUM_PUBLISH_COUNT 30
2269 #define TEST_RECEIVE_MAXIMUM 3
2270
2271 static int s_aws_mqtt5_mock_server_handle_connect_succeed_receive_maximum(
2272 void *packet,
2273 struct aws_mqtt5_server_mock_connection_context *connection,
2274 void *user_data) {
2275 (void)packet;
2276 (void)user_data;
2277
2278 struct aws_mqtt5_packet_connack_view connack_view;
2279 AWS_ZERO_STRUCT(connack_view);
2280
2281 uint16_t receive_maximum = TEST_RECEIVE_MAXIMUM;
2282
2283 connack_view.reason_code = AWS_MQTT5_CRC_SUCCESS;
2284 connack_view.receive_maximum = &receive_maximum;
2285
2286 return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_CONNACK, &connack_view);
2287 }
2288
2289 struct send_puback_task {
2290 struct aws_allocator *allocator;
2291 struct aws_task task;
2292 struct aws_mqtt5_server_mock_connection_context *connection;
2293 uint16_t packet_id;
2294 };
2295
2296 void send_puback_fn(struct aws_task *task, void *arg, enum aws_task_status status) {
2297 (void)task;
2298
2299 struct send_puback_task *puback_response_task = arg;
2300 if (status == AWS_TASK_STATUS_CANCELED) {
2301 goto done;
2302 }
2303
2304 struct aws_mqtt5_client_mock_test_fixture *test_fixture = puback_response_task->connection->test_fixture;
2305
2306 aws_mutex_lock(&test_fixture->lock);
2307 --test_fixture->server_current_inflight_publishes;
2308 aws_mutex_unlock(&test_fixture->lock);
2309
2310 struct aws_mqtt5_packet_puback_view puback_view = {
2311 .packet_id = puback_response_task->packet_id,
2312 };
2313
2314 aws_mqtt5_mock_server_send_packet(puback_response_task->connection, AWS_MQTT5_PT_PUBACK, &puback_view);
2315
2316 done:
2317
2318 aws_mem_release(puback_response_task->allocator, puback_response_task);
2319 }
2320
2321 static int s_aws_mqtt5_mock_server_handle_publish_delayed_puback(
2322 void *packet,
2323 struct aws_mqtt5_server_mock_connection_context *connection,
2324 void *user_data) {
2325
2326 (void)user_data;
2327
2328 struct aws_mqtt5_client_mock_test_fixture *test_fixture = connection->test_fixture;
2329
2330 aws_mutex_lock(&test_fixture->lock);
2331 ++test_fixture->server_current_inflight_publishes;
2332 test_fixture->server_maximum_inflight_publishes =
2333 aws_max_u32(test_fixture->server_current_inflight_publishes, test_fixture->server_maximum_inflight_publishes);
2334 aws_mutex_unlock(&test_fixture->lock);
2335
2336 struct aws_mqtt5_packet_publish_view *publish_view = packet;
2337
2338 struct send_puback_task *puback_response_task =
2339 aws_mem_calloc(connection->allocator, 1, sizeof(struct send_puback_task));
2340 puback_response_task->allocator = connection->allocator;
2341 puback_response_task->connection = connection;
2342 puback_response_task->packet_id = publish_view->packet_id;
2343
2344 aws_task_init(&puback_response_task->task, send_puback_fn, puback_response_task, "delayed_puback_response");
2345
2346 struct aws_event_loop *event_loop = aws_channel_get_event_loop(connection->slot->channel);
2347
2348 uint64_t now = 0;
2349 aws_event_loop_current_clock_time(event_loop, &now);
2350
2351 uint64_t min_delay = aws_timestamp_convert(250, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL);
2352 uint64_t max_delay = aws_timestamp_convert(500, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL);
2353 uint64_t delay_nanos = aws_mqtt5_client_random_in_range(min_delay, max_delay);
2354
2355 uint64_t puback_time = aws_add_u64_saturating(now, delay_nanos);
2356 aws_event_loop_schedule_task_future(event_loop, &puback_response_task->task, puback_time);
2357
2358 return AWS_OP_SUCCESS;
2359 }
2360
2361 static void s_receive_maximum_publish_completion_fn(
2362 enum aws_mqtt5_packet_type packet_type,
2363 const void *packet,
2364 int error_code,
2365 void *complete_ctx) {
2366
2367 if (packet_type != AWS_MQTT5_PT_PUBACK) {
2368 return;
2369 }
2370
2371 const struct aws_mqtt5_packet_puback_view *puback = packet;
2372 struct aws_mqtt5_client_mock_test_fixture *test_context = complete_ctx;
2373
2374 uint64_t now = 0;
2375 aws_high_res_clock_get_ticks(&now);
2376
2377 aws_mutex_lock(&test_context->lock);
2378
2379 ++test_context->total_pubacks_received;
2380 if (error_code == AWS_ERROR_SUCCESS && puback->reason_code < 128) {
2381 ++test_context->successful_pubacks_received;
2382 }
2383
2384 aws_mutex_unlock(&test_context->lock);
2385 aws_condition_variable_notify_all(&test_context->signal);
2386 }
2387
2388 static bool s_received_n_successful_publishes(void *arg) {
2389 struct aws_mqtt5_client_test_wait_for_n_context *context = arg;
2390 struct aws_mqtt5_client_mock_test_fixture *test_fixture = context->test_fixture;
2391
2392 return test_fixture->successful_pubacks_received >= context->required_event_count;
2393 }
2394
2395 static void s_wait_for_n_successful_publishes(struct aws_mqtt5_client_test_wait_for_n_context *context) {
2396
2397 struct aws_mqtt5_client_mock_test_fixture *test_context = context->test_fixture;
2398 aws_mutex_lock(&test_context->lock);
2399 aws_condition_variable_wait_pred(
2400 &test_context->signal, &test_context->lock, s_received_n_successful_publishes, context);
2401 aws_mutex_unlock(&test_context->lock);
2402 }
2403
2404 static int s_mqtt5_client_flow_control_receive_maximum_fn(struct aws_allocator *allocator, void *ctx) {
2405 (void)ctx;
2406
2407 aws_mqtt_library_init(allocator);
2408
2409 struct mqtt5_client_test_options test_options;
2410 aws_mqtt5_client_test_init_default_options(&test_options);
2411
2412 /* send delayed pubacks */
2413 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] =
2414 s_aws_mqtt5_mock_server_handle_publish_delayed_puback;
2415
2416 /* establish a low receive maximum */
2417 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] =
2418 s_aws_mqtt5_mock_server_handle_connect_succeed_receive_maximum;
2419
2420 struct aws_mqtt5_client_mock_test_fixture test_context;
2421
2422 struct aws_mqtt5_server_disconnect_test_context disconnect_context = {
2423 .test_fixture = &test_context,
2424 .disconnect_sent = false,
2425 };
2426
2427 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
2428 .client_options = &test_options.client_options,
2429 .server_function_table = &test_options.server_function_table,
2430 .mock_server_user_data = &disconnect_context,
2431 };
2432
2433 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
2434
2435 struct aws_mqtt5_client *client = test_context.client;
2436 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
2437
2438 aws_wait_for_connected_lifecycle_event(&test_context);
2439
2440 /* send a bunch of publishes */
2441 for (size_t i = 0; i < RECEIVE_MAXIMUM_PUBLISH_COUNT; ++i) {
2442 struct aws_mqtt5_packet_publish_view qos1_publish_view = {
2443 .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE,
2444 .topic =
2445 {
2446 .ptr = s_topic,
2447 .len = AWS_ARRAY_SIZE(s_topic) - 1,
2448 },
2449 };
2450
2451 struct aws_mqtt5_publish_completion_options completion_options = {
2452 .completion_callback = s_receive_maximum_publish_completion_fn,
2453 .completion_user_data = &test_context,
2454 };
2455
2456 ASSERT_SUCCESS(aws_mqtt5_client_publish(client, &qos1_publish_view, &completion_options));
2457 }
2458
2459 /* wait for all publishes to succeed */
2460 struct aws_mqtt5_client_test_wait_for_n_context wait_context = {
2461 .test_fixture = &test_context,
2462 .required_event_count = RECEIVE_MAXIMUM_PUBLISH_COUNT,
2463 };
2464 s_wait_for_n_successful_publishes(&wait_context);
2465
2466 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
2467
2468 aws_wait_for_stopped_lifecycle_event(&test_context);
2469
2470 /*
2471 * verify that the maximum number of in-progress qos1 publishes on the server was never more than what the
2472 * server said its maximum was
2473 */
2474 aws_mutex_lock(&test_context.lock);
2475 uint32_t max_inflight_publishes = test_context.server_maximum_inflight_publishes;
2476 aws_mutex_unlock(&test_context.lock);
2477
2478 ASSERT_TRUE(max_inflight_publishes <= TEST_RECEIVE_MAXIMUM);
2479
2480 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
2481 aws_mqtt_library_clean_up();
2482
2483 return AWS_OP_SUCCESS;
2484 }
2485
2486 AWS_TEST_CASE(mqtt5_client_flow_control_receive_maximum, s_mqtt5_client_flow_control_receive_maximum_fn)
2487
2488 static void s_publish_timeout_publish_completion_fn(
2489 enum aws_mqtt5_packet_type packet_type,
2490 const void *packet,
2491 int error_code,
2492 void *complete_ctx) {
2493 (void)packet;
2494 (void)packet_type;
2495
2496 struct aws_mqtt5_client_mock_test_fixture *test_context = complete_ctx;
2497
2498 aws_mutex_lock(&test_context->lock);
2499
2500 if (error_code == AWS_ERROR_MQTT_TIMEOUT) {
2501 ++test_context->timeouts_received;
2502 }
2503
2504 aws_mutex_unlock(&test_context->lock);
2505 aws_condition_variable_notify_all(&test_context->signal);
2506 }
2507
2508 static bool s_received_n_publish_timeouts(void *arg) {
2509 struct aws_mqtt5_client_test_wait_for_n_context *context = arg;
2510 struct aws_mqtt5_client_mock_test_fixture *test_fixture = context->test_fixture;
2511
2512 return test_fixture->timeouts_received >= context->required_event_count;
2513 }
2514
2515 static void s_wait_for_n_publish_timeouts(struct aws_mqtt5_client_test_wait_for_n_context *context) {
2516 struct aws_mqtt5_client_mock_test_fixture *test_context = context->test_fixture;
2517 aws_mutex_lock(&test_context->lock);
2518 aws_condition_variable_wait_pred(
2519 &test_context->signal, &test_context->lock, s_received_n_publish_timeouts, context);
2520 aws_mutex_unlock(&test_context->lock);
2521 }
2522
2523 static bool s_sent_n_timeout_publish_packets(void *arg) {
2524 struct aws_mqtt5_client_test_wait_for_n_context *context = arg;
2525 struct aws_mqtt5_client_mock_test_fixture *test_fixture = context->test_fixture;
2526
2527 return test_fixture->publishes_received >= context->required_event_count;
2528 }
2529
2530 static int s_aws_mqtt5_mock_server_handle_timeout_publish(
2531 void *packet,
2532 struct aws_mqtt5_server_mock_connection_context *connection,
2533 void *user_data) {
2534
2535 (void)packet;
2536 (void)user_data;
2537 struct aws_mqtt5_client_mock_test_fixture *test_fixture = connection->test_fixture;
2538
2539 aws_mutex_lock(&test_fixture->lock);
2540 ++connection->test_fixture->publishes_received;
2541 aws_mutex_unlock(&test_fixture->lock);
2542
2543 return AWS_OP_SUCCESS;
2544 }
2545
2546 static void s_wait_for_n_successful_server_timeout_publishes(struct aws_mqtt5_client_test_wait_for_n_context *context) {
2547 struct aws_mqtt5_client_mock_test_fixture *test_context = context->test_fixture;
2548 aws_mutex_lock(&test_context->lock);
2549 aws_condition_variable_wait_pred(
2550 &test_context->signal, &test_context->lock, s_sent_n_timeout_publish_packets, context);
2551 aws_mutex_unlock(&test_context->lock);
2552 }
2553
2554 /*
2555 * Test that not receiving a PUBACK causes the PUBLISH waiting for the PUBACK to timeout
2556 */
2557 static int s_mqtt5_client_publish_timeout_fn(struct aws_allocator *allocator, void *ctx) {
2558 (void)ctx;
2559
2560 aws_mqtt_library_init(allocator);
2561
2562 struct mqtt5_client_test_options test_options;
2563 aws_mqtt5_client_test_init_default_options(&test_options);
2564
2565 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] =
2566 s_aws_mqtt5_mock_server_handle_timeout_publish;
2567
2568 /* fast publish timeout */
2569 test_options.client_options.ack_timeout_seconds = 5;
2570
2571 struct aws_mqtt5_client_mock_test_fixture test_context;
2572
2573 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
2574 .client_options = &test_options.client_options,
2575 .server_function_table = &test_options.server_function_table,
2576 };
2577
2578 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
2579
2580 struct aws_mqtt5_client *client = test_context.client;
2581 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
2582
2583 aws_wait_for_connected_lifecycle_event(&test_context);
2584
2585 struct aws_mqtt5_publish_completion_options completion_options = {
2586 .completion_callback = &s_publish_timeout_publish_completion_fn,
2587 .completion_user_data = &test_context,
2588 };
2589
2590 struct aws_mqtt5_packet_publish_view packet_publish_view = {
2591 .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE,
2592 .topic =
2593 {
2594 .ptr = s_topic,
2595 .len = AWS_ARRAY_SIZE(s_topic) - 1,
2596 },
2597 };
2598
2599 struct aws_mqtt5_client_test_wait_for_n_context wait_context = {
2600 .test_fixture = &test_context,
2601 .required_event_count = (size_t)aws_mqtt5_client_random_in_range(3, 20),
2602 };
2603
2604 /* Send semi-random number of publishes that will not be acked */
2605 for (size_t publish_count = 0; publish_count < wait_context.required_event_count; ++publish_count) {
2606 ASSERT_SUCCESS(aws_mqtt5_client_publish(client, &packet_publish_view, &completion_options));
2607 }
2608
2609 s_wait_for_n_successful_server_timeout_publishes(&wait_context);
2610
2611 s_wait_for_n_publish_timeouts(&wait_context);
2612
2613 ASSERT_INT_EQUALS(wait_context.required_event_count, test_context.timeouts_received);
2614
2615 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
2616
2617 aws_wait_for_stopped_lifecycle_event(&test_context);
2618
2619 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
2620 aws_mqtt_library_clean_up();
2621
2622 return AWS_OP_SUCCESS;
2623 }
2624
2625 AWS_TEST_CASE(mqtt5_client_publish_timeout, s_mqtt5_client_publish_timeout_fn)
2626
2627 int aws_mqtt5_mock_server_handle_publish_puback(
2628 void *packet,
2629 struct aws_mqtt5_server_mock_connection_context *connection,
2630 void *user_data) {
2631
2632 (void)user_data;
2633
2634 struct aws_mqtt5_packet_publish_view *publish_view = packet;
2635 if (publish_view->qos != AWS_MQTT5_QOS_AT_LEAST_ONCE) {
2636 return AWS_OP_SUCCESS;
2637 }
2638
2639 struct aws_mqtt5_packet_puback_view puback_view = {
2640 .packet_id = publish_view->packet_id,
2641 };
2642
2643 return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_PUBACK, &puback_view);
2644 }
2645
2646 #define IOT_CORE_THROUGHPUT_PACKETS 21
2647
2648 static uint8_t s_large_packet_payload[127 * 1024];
2649
2650 static int s_do_iot_core_throughput_test(struct aws_allocator *allocator, bool use_iot_core_limits) {
2651
2652 struct mqtt5_client_test_options test_options;
2653 aws_mqtt5_client_test_init_default_options(&test_options);
2654
2655 /* send pubacks */
2656 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] =
2657 aws_mqtt5_mock_server_handle_publish_puback;
2658
2659 if (use_iot_core_limits) {
2660 test_options.client_options.extended_validation_and_flow_control_options =
2661 AWS_MQTT5_EVAFCO_AWS_IOT_CORE_DEFAULTS;
2662 }
2663
2664 struct aws_mqtt5_client_mock_test_fixture test_context;
2665
2666 struct aws_mqtt5_server_disconnect_test_context disconnect_context = {
2667 .test_fixture = &test_context,
2668 .disconnect_sent = false,
2669 };
2670
2671 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
2672 .client_options = &test_options.client_options,
2673 .server_function_table = &test_options.server_function_table,
2674 .mock_server_user_data = &disconnect_context,
2675 };
2676
2677 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
2678
2679 struct aws_mqtt5_client *client = test_context.client;
2680 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
2681
2682 aws_wait_for_connected_lifecycle_event(&test_context);
2683
2684 /* send a bunch of large publishes */
2685 aws_secure_zero(s_large_packet_payload, AWS_ARRAY_SIZE(s_large_packet_payload));
2686
2687 for (size_t i = 0; i < IOT_CORE_THROUGHPUT_PACKETS; ++i) {
2688 struct aws_mqtt5_packet_publish_view qos1_publish_view = {
2689 .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE,
2690 .topic =
2691 {
2692 .ptr = s_topic,
2693 .len = AWS_ARRAY_SIZE(s_topic) - 1,
2694 },
2695 .payload = aws_byte_cursor_from_array(s_large_packet_payload, AWS_ARRAY_SIZE(s_large_packet_payload)),
2696 };
2697
2698 struct aws_mqtt5_publish_completion_options completion_options = {
2699 .completion_callback = s_receive_maximum_publish_completion_fn, /* can reuse receive_maximum callback */
2700 .completion_user_data = &test_context,
2701 };
2702
2703 ASSERT_SUCCESS(aws_mqtt5_client_publish(client, &qos1_publish_view, &completion_options));
2704 }
2705
2706 /* wait for all publishes to succeed */
2707 struct aws_mqtt5_client_test_wait_for_n_context wait_context = {
2708 .test_fixture = &test_context,
2709 .required_event_count = IOT_CORE_THROUGHPUT_PACKETS,
2710 };
2711 s_wait_for_n_successful_publishes(&wait_context);
2712
2713 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
2714
2715 aws_wait_for_stopped_lifecycle_event(&test_context);
2716
2717 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
2718 aws_thread_join_all_managed();
2719
2720 return AWS_OP_SUCCESS;
2721 }
2722
2723 static int s_mqtt5_client_flow_control_iot_core_throughput_fn(struct aws_allocator *allocator, void *ctx) {
2724 (void)ctx;
2725
2726 aws_mqtt_library_init(allocator);
2727
2728 uint64_t start_time = 0;
2729 aws_high_res_clock_get_ticks(&start_time);
2730
2731 ASSERT_SUCCESS(s_do_iot_core_throughput_test(allocator, true));
2732
2733 uint64_t end_time = 0;
2734 aws_high_res_clock_get_ticks(&end_time);
2735
2736 uint64_t test_time = end_time - start_time;
2737
2738 /*
2739 * We expect the throttled version to take around 5 seconds, since we're sending 21 almost-max size (127k) packets
2740 * against a limit of 512KB/s. Since the packets are submitted immediately on CONNACK, the rate limiter
2741 * token bucket is starting at zero and so will give us immediate throttling.
2742 */
2743 ASSERT_TRUE(test_time > 5 * (uint64_t)AWS_TIMESTAMP_NANOS);
2744
2745 aws_mqtt_library_clean_up();
2746
2747 return AWS_OP_SUCCESS;
2748 }
2749
2750 AWS_TEST_CASE(mqtt5_client_flow_control_iot_core_throughput, s_mqtt5_client_flow_control_iot_core_throughput_fn)
2751
2752 #define IOT_CORE_PUBLISH_TPS_PACKETS 650
2753
2754 static int s_do_iot_core_publish_tps_test(
2755 struct aws_allocator *allocator,
2756 bool use_iot_core_limits,
2757 uint64_t *test_time) {
2758
2759 struct mqtt5_client_test_options test_options;
2760 aws_mqtt5_client_test_init_default_options(&test_options);
2761
2762 /* send pubacks */
2763 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] =
2764 aws_mqtt5_mock_server_handle_publish_puback;
2765
2766 if (use_iot_core_limits) {
2767 test_options.client_options.extended_validation_and_flow_control_options =
2768 AWS_MQTT5_EVAFCO_AWS_IOT_CORE_DEFAULTS;
2769 }
2770
2771 struct aws_mqtt5_client_mock_test_fixture test_context;
2772
2773 struct aws_mqtt5_server_disconnect_test_context disconnect_context = {
2774 .test_fixture = &test_context,
2775 .disconnect_sent = false,
2776 };
2777
2778 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
2779 .client_options = &test_options.client_options,
2780 .server_function_table = &test_options.server_function_table,
2781 .mock_server_user_data = &disconnect_context,
2782 };
2783
2784 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
2785
2786 struct aws_mqtt5_client *client = test_context.client;
2787 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
2788
2789 aws_wait_for_connected_lifecycle_event(&test_context);
2790
2791 /* send a bunch of tiny publishes */
2792 for (size_t i = 0; i < IOT_CORE_PUBLISH_TPS_PACKETS; ++i) {
2793 struct aws_mqtt5_packet_publish_view qos1_publish_view = {
2794 .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE,
2795 .topic =
2796 {
2797 .ptr = s_topic,
2798 .len = AWS_ARRAY_SIZE(s_topic) - 1,
2799 },
2800 };
2801
2802 struct aws_mqtt5_publish_completion_options completion_options = {
2803 .completion_callback = s_receive_maximum_publish_completion_fn, /* can reuse receive_maximum callback */
2804 .completion_user_data = &test_context,
2805 };
2806
2807 ASSERT_SUCCESS(aws_mqtt5_client_publish(client, &qos1_publish_view, &completion_options));
2808 }
2809
2810 /* wait for all publishes to succeed */
2811 struct aws_mqtt5_client_test_wait_for_n_context wait_context = {
2812 .test_fixture = &test_context,
2813 .required_event_count = IOT_CORE_PUBLISH_TPS_PACKETS,
2814 };
2815 s_wait_for_n_successful_publishes(&wait_context);
2816
2817 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
2818
2819 aws_wait_for_stopped_lifecycle_event(&test_context);
2820
2821 aws_high_res_clock_get_ticks(test_time);
2822
2823 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
2824 aws_thread_join_all_managed();
2825
2826 return AWS_OP_SUCCESS;
2827 }
2828
2829 static int s_mqtt5_client_flow_control_iot_core_publish_tps_fn(struct aws_allocator *allocator, void *ctx) {
2830 (void)ctx;
2831
2832 aws_mqtt_library_init(allocator);
2833
2834 uint64_t start_time1 = 0;
2835 aws_high_res_clock_get_ticks(&start_time1);
2836 uint64_t end_time1 = 0;
2837
2838 ASSERT_SUCCESS(s_do_iot_core_publish_tps_test(allocator, false, &end_time1));
2839
2840 uint64_t test_time1 = end_time1 - start_time1;
2841
2842 uint64_t start_time2 = 0;
2843 aws_high_res_clock_get_ticks(&start_time2);
2844
2845 uint64_t end_time2 = 0;
2846 ASSERT_SUCCESS(s_do_iot_core_publish_tps_test(allocator, true, &end_time2));
2847
2848 uint64_t test_time2 = end_time2 - start_time2;
2849
2850 /* We expect the unthrottled test to complete quickly */
2851 ASSERT_TRUE(test_time1 < AWS_TIMESTAMP_NANOS);
2852
2853 /*
2854 * We expect the throttled version to take over 6 seconds, since we're sending over 650 tiny publish packets
2855 * against a limit of 100TPS. Since the packets are submitted immediately on CONNACK, the rate limiter
2856 * token bucket is starting at zero and so will give us immediate throttling.
2857 */
2858 ASSERT_TRUE(test_time2 > 6 * (uint64_t)AWS_TIMESTAMP_NANOS);
2859
2860 aws_mqtt_library_clean_up();
2861
2862 return AWS_OP_SUCCESS;
2863 }
2864
2865 AWS_TEST_CASE(mqtt5_client_flow_control_iot_core_publish_tps, s_mqtt5_client_flow_control_iot_core_publish_tps_fn)
2866
2867 static int s_aws_mqtt5_mock_server_handle_connect_honor_session_after_success(
2868 void *packet,
2869 struct aws_mqtt5_server_mock_connection_context *connection,
2870 void *user_data) {
2871 (void)packet;
2872 (void)user_data;
2873
2874 struct aws_mqtt5_packet_connect_view *connect_packet = packet;
2875
2876 struct aws_mqtt5_packet_connack_view connack_view;
2877 AWS_ZERO_STRUCT(connack_view);
2878
2879 connack_view.reason_code = AWS_MQTT5_CRC_SUCCESS;
2880 /* Only resume a connection if the client has already connected to the server before */
2881 if (connection->test_fixture->client->has_connected_successfully) {
2882 connack_view.session_present = !connect_packet->clean_start;
2883 }
2884
2885 return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_CONNACK, &connack_view);
2886 }
2887
2888 int aws_mqtt5_mock_server_handle_connect_honor_session_unconditional(
2889 void *packet,
2890 struct aws_mqtt5_server_mock_connection_context *connection,
2891 void *user_data) {
2892 (void)packet;
2893 (void)user_data;
2894
2895 struct aws_mqtt5_packet_connect_view *connect_packet = packet;
2896
2897 struct aws_mqtt5_packet_connack_view connack_view;
2898 AWS_ZERO_STRUCT(connack_view);
2899
2900 connack_view.reason_code = AWS_MQTT5_CRC_SUCCESS;
2901 connack_view.session_present = !connect_packet->clean_start;
2902
2903 return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_CONNACK, &connack_view);
2904 }
2905
2906 struct aws_mqtt5_wait_for_n_lifecycle_events_context {
2907 struct aws_mqtt5_client_mock_test_fixture *test_fixture;
2908 enum aws_mqtt5_client_lifecycle_event_type event_type;
2909 size_t expected_event_count;
2910 };
2911
2912 static bool s_received_n_lifecycle_events(void *arg) {
2913 struct aws_mqtt5_wait_for_n_lifecycle_events_context *context = arg;
2914 struct aws_mqtt5_client_mock_test_fixture *test_fixture = context->test_fixture;
2915
2916 size_t matching_events = 0;
2917 size_t event_count = aws_array_list_length(&test_fixture->lifecycle_events);
2918 for (size_t i = 0; i < event_count; ++i) {
2919 struct aws_mqtt5_lifecycle_event_record *record = NULL;
2920 aws_array_list_get_at(&test_fixture->lifecycle_events, &record, i);
2921
2922 if (record->event.event_type == context->event_type) {
2923 ++matching_events;
2924 }
2925 }
2926
2927 return matching_events >= context->expected_event_count;
2928 }
2929
2930 void aws_wait_for_n_lifecycle_events(
2931 struct aws_mqtt5_client_mock_test_fixture *test_fixture,
2932 enum aws_mqtt5_client_lifecycle_event_type event_type,
2933 size_t expected_event_count) {
2934 struct aws_mqtt5_wait_for_n_lifecycle_events_context wait_context = {
2935 .test_fixture = test_fixture,
2936 .event_type = event_type,
2937 .expected_event_count = expected_event_count,
2938 };
2939
2940 aws_mutex_lock(&test_fixture->lock);
2941 aws_condition_variable_wait_pred(
2942 &test_fixture->signal, &test_fixture->lock, s_received_n_lifecycle_events, &wait_context);
2943 aws_mutex_unlock(&test_fixture->lock);
2944 }
2945
2946 static bool s_compute_expected_rejoined_session(
2947 enum aws_mqtt5_client_session_behavior_type session_behavior,
2948 size_t connect_index) {
2949 switch (session_behavior) {
2950 case AWS_MQTT5_CSBT_CLEAN:
2951 return false;
2952
2953 case AWS_MQTT5_CSBT_REJOIN_POST_SUCCESS:
2954 return connect_index > 0;
2955
2956 case AWS_MQTT5_CSBT_REJOIN_ALWAYS:
2957 default:
2958 return true;
2959 }
2960 }
2961
2962 static int s_aws_mqtt5_client_test_init_resume_session_connect_storage(
2963 struct aws_mqtt5_packet_connect_storage *storage,
2964 struct aws_allocator *allocator) {
2965
2966 struct aws_mqtt5_packet_connect_view connect_view = {
2967 .keep_alive_interval_seconds = 30,
2968 .client_id = aws_byte_cursor_from_string(g_default_client_id),
2969 .clean_start = false,
2970 };
2971
2972 return aws_mqtt5_packet_connect_storage_init(storage, allocator, &connect_view, NULL);
2973 }
2974
2975 #define SESSION_RESUMPTION_CONNECT_COUNT 5
2976
2977 static int s_do_mqtt5_client_session_resumption_test(
2978 struct aws_allocator *allocator,
2979 enum aws_mqtt5_client_session_behavior_type session_behavior) {
2980 aws_mqtt_library_init(allocator);
2981
2982 struct mqtt5_client_test_options test_options;
2983 aws_mqtt5_client_test_init_default_options(&test_options);
2984
2985 test_options.client_options.session_behavior = session_behavior;
2986 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] =
2987 aws_mqtt5_mock_server_handle_connect_honor_session_unconditional;
2988
2989 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
2990 .client_options = &test_options.client_options,
2991 .server_function_table = &test_options.server_function_table,
2992 };
2993
2994 struct aws_mqtt5_client_mock_test_fixture test_context;
2995 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
2996
2997 struct aws_mqtt5_client *client = test_context.client;
2998
2999 for (size_t i = 0; i < SESSION_RESUMPTION_CONNECT_COUNT; ++i) {
3000 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
3001 aws_wait_for_n_lifecycle_events(&test_context, AWS_MQTT5_CLET_CONNECTION_SUCCESS, i + 1);
3002
3003 /* not technically truly safe to query depending on memory model. Remove if it becomes a problem. */
3004 bool expected_rejoined_session = s_compute_expected_rejoined_session(session_behavior, i);
3005 ASSERT_INT_EQUALS(expected_rejoined_session, client->negotiated_settings.rejoined_session);
3006
3007 /* can't use stop as that wipes session state */
3008 aws_channel_shutdown(test_context.server_channel, AWS_ERROR_UNKNOWN);
3009
3010 aws_wait_for_n_lifecycle_events(&test_context, AWS_MQTT5_CLET_DISCONNECTION, i + 1);
3011 }
3012
3013 struct aws_mqtt5_packet_connect_storage clean_start_connect_storage;
3014 ASSERT_SUCCESS(s_aws_mqtt5_client_test_init_default_connect_storage(&clean_start_connect_storage, allocator));
3015
3016 struct aws_mqtt5_packet_connect_storage resume_session_connect_storage;
3017 ASSERT_SUCCESS(
3018 s_aws_mqtt5_client_test_init_resume_session_connect_storage(&resume_session_connect_storage, allocator));
3019
3020 struct aws_mqtt5_mock_server_packet_record expected_packets[SESSION_RESUMPTION_CONNECT_COUNT];
3021 for (size_t i = 0; i < SESSION_RESUMPTION_CONNECT_COUNT; ++i) {
3022 expected_packets[i].packet_type = AWS_MQTT5_PT_CONNECT;
3023 if (s_compute_expected_rejoined_session(session_behavior, i)) {
3024 expected_packets[i].packet_storage = &resume_session_connect_storage;
3025 } else {
3026 expected_packets[i].packet_storage = &clean_start_connect_storage;
3027 }
3028 }
3029
3030 ASSERT_SUCCESS(
3031 aws_verify_received_packet_sequence(&test_context, expected_packets, AWS_ARRAY_SIZE(expected_packets)));
3032
3033 aws_mqtt5_packet_connect_storage_clean_up(&clean_start_connect_storage);
3034 aws_mqtt5_packet_connect_storage_clean_up(&resume_session_connect_storage);
3035 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
3036
3037 aws_mqtt_library_clean_up();
3038
3039 return AWS_OP_SUCCESS;
3040 }
3041
3042 static int s_mqtt5_client_session_resumption_clean_start_fn(struct aws_allocator *allocator, void *ctx) {
3043 (void)ctx;
3044
3045 ASSERT_SUCCESS(s_do_mqtt5_client_session_resumption_test(allocator, AWS_MQTT5_CSBT_CLEAN));
3046
3047 return AWS_OP_SUCCESS;
3048 }
3049
3050 AWS_TEST_CASE(mqtt5_client_session_resumption_clean_start, s_mqtt5_client_session_resumption_clean_start_fn)
3051
3052 static int s_mqtt5_client_session_resumption_post_success_fn(struct aws_allocator *allocator, void *ctx) {
3053 (void)ctx;
3054
3055 ASSERT_SUCCESS(s_do_mqtt5_client_session_resumption_test(allocator, AWS_MQTT5_CSBT_REJOIN_POST_SUCCESS));
3056
3057 return AWS_OP_SUCCESS;
3058 }
3059
3060 AWS_TEST_CASE(mqtt5_client_session_resumption_post_success, s_mqtt5_client_session_resumption_post_success_fn)
3061
3062 static int s_mqtt5_client_session_resumption_always_fn(struct aws_allocator *allocator, void *ctx) {
3063 (void)ctx;
3064
3065 ASSERT_SUCCESS(s_do_mqtt5_client_session_resumption_test(allocator, AWS_MQTT5_CSBT_REJOIN_ALWAYS));
3066
3067 return AWS_OP_SUCCESS;
3068 }
3069
3070 AWS_TEST_CASE(mqtt5_client_session_resumption_always, s_mqtt5_client_session_resumption_always_fn)
3071
3072 static uint8_t s_sub_pub_unsub_topic_filter[] = "hello/+";
3073
3074 static struct aws_mqtt5_subscription_view s_sub_pub_unsub_subscriptions[] = {
3075 {
3076 .topic_filter =
3077 {
3078 .ptr = (uint8_t *)s_sub_pub_unsub_topic_filter,
3079 .len = AWS_ARRAY_SIZE(s_sub_pub_unsub_topic_filter) - 1,
3080 },
3081 .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE,
3082 .no_local = false,
3083 .retain_as_published = false,
3084 .retain_handling_type = AWS_MQTT5_RHT_SEND_ON_SUBSCRIBE,
3085 },
3086 };
3087
3088 static struct aws_byte_cursor s_sub_pub_unsub_topic_filters[] = {
3089 {
3090 .ptr = s_sub_pub_unsub_topic_filter,
3091 .len = AWS_ARRAY_SIZE(s_sub_pub_unsub_topic_filter) - 1,
3092 },
3093 };
3094
3095 struct aws_mqtt5_sub_pub_unsub_context {
3096 struct aws_mqtt5_client_mock_test_fixture *test_fixture;
3097
3098 bool subscribe_complete;
3099 bool publish_complete;
3100 bool publish_received;
3101 bool unsubscribe_complete;
3102 size_t publishes_received;
3103
3104 size_t subscribe_failures;
3105 size_t publish_failures;
3106 size_t unsubscribe_failures;
3107
3108 struct aws_mqtt5_packet_publish_storage publish_storage;
3109 };
3110
3111 static void s_sub_pub_unsub_context_clean_up(struct aws_mqtt5_sub_pub_unsub_context *context) {
3112 aws_mqtt5_packet_publish_storage_clean_up(&context->publish_storage);
3113 }
3114
3115 void s_sub_pub_unsub_subscribe_complete_fn(
3116 const struct aws_mqtt5_packet_suback_view *suback,
3117 int error_code,
3118 void *complete_ctx) {
3119
3120 AWS_FATAL_ASSERT(suback != NULL);
3121 AWS_FATAL_ASSERT(error_code == AWS_ERROR_SUCCESS);
3122
3123 struct aws_mqtt5_sub_pub_unsub_context *test_context = complete_ctx;
3124 struct aws_mqtt5_client_mock_test_fixture *test_fixture = test_context->test_fixture;
3125
3126 aws_mutex_lock(&test_fixture->lock);
3127 test_context->subscribe_complete = true;
3128 if (error_code != AWS_ERROR_SUCCESS) {
3129 ++test_context->subscribe_failures;
3130 }
3131 aws_mutex_unlock(&test_fixture->lock);
3132 aws_condition_variable_notify_all(&test_fixture->signal);
3133 }
3134
3135 static bool s_sub_pub_unsub_received_suback(void *arg) {
3136 struct aws_mqtt5_sub_pub_unsub_context *test_context = arg;
3137
3138 return test_context->subscribe_complete && test_context->subscribe_failures == 0;
3139 }
3140
3141 static void s_sub_pub_unsub_wait_for_suback_received(struct aws_mqtt5_sub_pub_unsub_context *test_context) {
3142 struct aws_mqtt5_client_mock_test_fixture *test_fixture = test_context->test_fixture;
3143
3144 aws_mutex_lock(&test_fixture->lock);
3145 aws_condition_variable_wait_pred(
3146 &test_fixture->signal, &test_fixture->lock, s_sub_pub_unsub_received_suback, test_context);
3147 aws_mutex_unlock(&test_fixture->lock);
3148 }
3149
3150 static int s_mqtt5_client_sub_pub_unsub_subscribe(
3151 struct aws_mqtt5_sub_pub_unsub_context *full_test_context,
3152 struct aws_mqtt5_packet_subscribe_storage *expected_subscribe_storage) {
3153 struct aws_mqtt5_packet_subscribe_view subscribe_view = {
3154 .subscriptions = s_sub_pub_unsub_subscriptions,
3155 .subscription_count = AWS_ARRAY_SIZE(s_sub_pub_unsub_subscriptions),
3156 };
3157
3158 struct aws_mqtt5_subscribe_completion_options completion_options = {
3159 .completion_callback = s_sub_pub_unsub_subscribe_complete_fn,
3160 .completion_user_data = full_test_context,
3161 };
3162
3163 struct aws_mqtt5_client *client = full_test_context->test_fixture->client;
3164 ASSERT_SUCCESS(aws_mqtt5_client_subscribe(client, &subscribe_view, &completion_options));
3165
3166 s_sub_pub_unsub_wait_for_suback_received(full_test_context);
3167
3168 ASSERT_SUCCESS(aws_mqtt5_packet_subscribe_storage_init(
3169 expected_subscribe_storage, full_test_context->test_fixture->allocator, &subscribe_view));
3170
3171 return AWS_OP_SUCCESS;
3172 }
3173
3174 void s_sub_pub_unsub_unsubscribe_complete_fn(
3175 const struct aws_mqtt5_packet_unsuback_view *unsuback,
3176 int error_code,
3177 void *complete_ctx) {
3178
3179 AWS_FATAL_ASSERT(unsuback != NULL);
3180 AWS_FATAL_ASSERT(error_code == AWS_ERROR_SUCCESS);
3181
3182 struct aws_mqtt5_sub_pub_unsub_context *test_context = complete_ctx;
3183 struct aws_mqtt5_client_mock_test_fixture *test_fixture = test_context->test_fixture;
3184
3185 aws_mutex_lock(&test_fixture->lock);
3186 test_context->unsubscribe_complete = true;
3187 if (error_code != AWS_ERROR_SUCCESS) {
3188 ++test_context->unsubscribe_failures;
3189 }
3190 aws_mutex_unlock(&test_fixture->lock);
3191 aws_condition_variable_notify_all(&test_fixture->signal);
3192 }
3193
3194 static bool s_sub_pub_unsub_received_unsuback(void *arg) {
3195 struct aws_mqtt5_sub_pub_unsub_context *test_context = arg;
3196
3197 return test_context->unsubscribe_complete && test_context->unsubscribe_failures == 0;
3198 }
3199
3200 static void s_sub_pub_unsub_wait_for_unsuback_received(struct aws_mqtt5_sub_pub_unsub_context *test_context) {
3201 struct aws_mqtt5_client_mock_test_fixture *test_fixture = test_context->test_fixture;
3202
3203 aws_mutex_lock(&test_fixture->lock);
3204 aws_condition_variable_wait_pred(
3205 &test_fixture->signal, &test_fixture->lock, s_sub_pub_unsub_received_unsuback, test_context);
3206 aws_mutex_unlock(&test_fixture->lock);
3207 }
3208
3209 static int s_mqtt5_client_sub_pub_unsub_unsubscribe(
3210 struct aws_mqtt5_sub_pub_unsub_context *full_test_context,
3211 struct aws_mqtt5_packet_unsubscribe_storage *expected_unsubscribe_storage) {
3212 struct aws_mqtt5_packet_unsubscribe_view unsubscribe_view = {
3213 .topic_filters = s_sub_pub_unsub_topic_filters,
3214 .topic_filter_count = AWS_ARRAY_SIZE(s_sub_pub_unsub_topic_filters),
3215 };
3216
3217 struct aws_mqtt5_unsubscribe_completion_options completion_options = {
3218 .completion_callback = s_sub_pub_unsub_unsubscribe_complete_fn,
3219 .completion_user_data = full_test_context,
3220 };
3221
3222 struct aws_mqtt5_client *client = full_test_context->test_fixture->client;
3223 ASSERT_SUCCESS(aws_mqtt5_client_unsubscribe(client, &unsubscribe_view, &completion_options));
3224
3225 s_sub_pub_unsub_wait_for_unsuback_received(full_test_context);
3226
3227 ASSERT_SUCCESS(aws_mqtt5_packet_unsubscribe_storage_init(
3228 expected_unsubscribe_storage, full_test_context->test_fixture->allocator, &unsubscribe_view));
3229
3230 return AWS_OP_SUCCESS;
3231 }
3232
3233 void s_sub_pub_unsub_publish_received_fn(const struct aws_mqtt5_packet_publish_view *publish, void *complete_ctx) {
3234
3235 AWS_FATAL_ASSERT(publish != NULL);
3236
3237 struct aws_mqtt5_sub_pub_unsub_context *test_context = complete_ctx;
3238 struct aws_mqtt5_client_mock_test_fixture *test_fixture = test_context->test_fixture;
3239
3240 aws_mutex_lock(&test_fixture->lock);
3241 test_context->publish_received = true;
3242 aws_mqtt5_packet_publish_storage_init(&test_context->publish_storage, test_fixture->allocator, publish);
3243
3244 aws_mutex_unlock(&test_fixture->lock);
3245 aws_condition_variable_notify_all(&test_fixture->signal);
3246 }
3247
3248 static bool s_sub_pub_unsub_received_publish(void *arg) {
3249 struct aws_mqtt5_sub_pub_unsub_context *test_context = arg;
3250
3251 return test_context->publish_received && test_context->publish_failures == 0;
3252 }
3253
3254 static void s_sub_pub_unsub_wait_for_publish_received(struct aws_mqtt5_sub_pub_unsub_context *test_context) {
3255 struct aws_mqtt5_client_mock_test_fixture *test_fixture = test_context->test_fixture;
3256
3257 aws_mutex_lock(&test_fixture->lock);
3258 aws_condition_variable_wait_pred(
3259 &test_fixture->signal, &test_fixture->lock, s_sub_pub_unsub_received_publish, test_context);
3260 aws_mutex_unlock(&test_fixture->lock);
3261 }
3262
3263 int aws_mqtt5_mock_server_handle_unsubscribe_unsuback_success(
3264 void *packet,
3265 struct aws_mqtt5_server_mock_connection_context *connection,
3266 void *user_data) {
3267 (void)packet;
3268 (void)user_data;
3269
3270 struct aws_mqtt5_packet_unsubscribe_view *unsubscribe_view = packet;
3271
3272 AWS_VARIABLE_LENGTH_ARRAY(
3273 enum aws_mqtt5_unsuback_reason_code, mqtt5_unsuback_codes, unsubscribe_view->topic_filter_count);
3274 for (size_t i = 0; i < unsubscribe_view->topic_filter_count; ++i) {
3275 enum aws_mqtt5_unsuback_reason_code *reason_code_ptr = &mqtt5_unsuback_codes[i];
3276 *reason_code_ptr = AWS_MQTT5_UARC_SUCCESS;
3277 }
3278
3279 struct aws_mqtt5_packet_unsuback_view unsuback_view = {
3280 .packet_id = unsubscribe_view->packet_id,
3281 .reason_code_count = unsubscribe_view->topic_filter_count,
3282 .reason_codes = mqtt5_unsuback_codes,
3283 };
3284
3285 return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_UNSUBACK, &unsuback_view);
3286 }
3287
3288 #define FORWARDED_PUBLISH_PACKET_ID 32768
3289
3290 int aws_mqtt5_mock_server_handle_publish_puback_and_forward(
3291 void *packet,
3292 struct aws_mqtt5_server_mock_connection_context *connection,
3293 void *user_data) {
3294 (void)packet;
3295 (void)user_data;
3296
3297 struct aws_mqtt5_packet_publish_view *publish_view = packet;
3298
3299 /* send a PUBACK? */
3300 if (publish_view->qos == AWS_MQTT5_QOS_AT_LEAST_ONCE) {
3301 struct aws_mqtt5_packet_puback_view puback_view = {
3302 .packet_id = publish_view->packet_id,
3303 .reason_code = AWS_MQTT5_PARC_SUCCESS,
3304 };
3305
3306 if (aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_PUBACK, &puback_view)) {
3307 return AWS_OP_ERR;
3308 }
3309 }
3310
3311 /* assume we're subscribed, reflect the publish back to the test client */
3312 struct aws_mqtt5_packet_publish_view reflect_publish_view = *publish_view;
3313 if (publish_view->qos == AWS_MQTT5_QOS_AT_LEAST_ONCE) {
3314 reflect_publish_view.packet_id = FORWARDED_PUBLISH_PACKET_ID;
3315 }
3316
3317 return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_PUBLISH, &reflect_publish_view);
3318 }
3319
3320 void s_sub_pub_unsub_publish_complete_fn(
3321 enum aws_mqtt5_packet_type packet_type,
3322 const void *packet,
3323 int error_code,
3324 void *complete_ctx) {
3325
3326 (void)packet;
3327 (void)packet_type;
3328
3329 AWS_FATAL_ASSERT(error_code == AWS_ERROR_SUCCESS);
3330
3331 struct aws_mqtt5_sub_pub_unsub_context *test_context = complete_ctx;
3332 struct aws_mqtt5_client_mock_test_fixture *test_fixture = test_context->test_fixture;
3333
3334 aws_mutex_lock(&test_fixture->lock);
3335 test_context->publish_complete = true;
3336 if (error_code != AWS_ERROR_SUCCESS) {
3337 ++test_context->publish_failures;
3338 }
3339 aws_mutex_unlock(&test_fixture->lock);
3340 aws_condition_variable_notify_all(&test_fixture->signal);
3341 }
3342
3343 static bool s_sub_pub_unsub_publish_complete(void *arg) {
3344 struct aws_mqtt5_sub_pub_unsub_context *test_context = arg;
3345
3346 return test_context->publish_complete;
3347 }
3348
3349 static void s_sub_pub_unsub_wait_for_publish_complete(struct aws_mqtt5_sub_pub_unsub_context *test_context) {
3350 struct aws_mqtt5_client_mock_test_fixture *test_fixture = test_context->test_fixture;
3351
3352 aws_mutex_lock(&test_fixture->lock);
3353 aws_condition_variable_wait_pred(
3354 &test_fixture->signal, &test_fixture->lock, s_sub_pub_unsub_publish_complete, test_context);
3355 aws_mutex_unlock(&test_fixture->lock);
3356 }
3357
3358 static uint8_t s_sub_pub_unsub_publish_topic[] = "hello/world";
3359 static uint8_t s_sub_pub_unsub_publish_payload[] = "PublishPayload";
3360
3361 static int s_mqtt5_client_sub_pub_unsub_publish(
3362 struct aws_mqtt5_sub_pub_unsub_context *full_test_context,
3363 enum aws_mqtt5_qos qos,
3364 struct aws_mqtt5_packet_publish_storage *expected_publish_storage,
3365 struct aws_mqtt5_packet_puback_storage *expected_puback_storage) {
3366 struct aws_mqtt5_packet_publish_view publish_view = {
3367 .qos = qos,
3368 .topic =
3369 {
3370 .ptr = s_sub_pub_unsub_publish_topic,
3371 .len = AWS_ARRAY_SIZE(s_sub_pub_unsub_publish_topic) - 1,
3372 },
3373 .payload =
3374 {
3375 .ptr = s_sub_pub_unsub_publish_payload,
3376 .len = AWS_ARRAY_SIZE(s_sub_pub_unsub_publish_payload) - 1,
3377 },
3378 };
3379
3380 struct aws_mqtt5_publish_completion_options completion_options = {
3381 .completion_callback = s_sub_pub_unsub_publish_complete_fn,
3382 .completion_user_data = full_test_context,
3383 };
3384
3385 struct aws_mqtt5_client *client = full_test_context->test_fixture->client;
3386 ASSERT_SUCCESS(aws_mqtt5_client_publish(client, &publish_view, &completion_options));
3387
3388 if (qos != AWS_MQTT5_QOS_AT_MOST_ONCE) {
3389 s_sub_pub_unsub_wait_for_publish_complete(full_test_context);
3390 }
3391
3392 struct aws_allocator *allocator = full_test_context->test_fixture->allocator;
3393 ASSERT_SUCCESS(aws_mqtt5_packet_publish_storage_init(expected_publish_storage, allocator, &publish_view));
3394
3395 struct aws_mqtt5_packet_puback_view puback_view = {
3396 .packet_id = FORWARDED_PUBLISH_PACKET_ID,
3397 .reason_code = AWS_MQTT5_PARC_SUCCESS,
3398 };
3399 ASSERT_SUCCESS(aws_mqtt5_packet_puback_storage_init(expected_puback_storage, allocator, &puback_view));
3400
3401 return AWS_OP_SUCCESS;
3402 }
3403
3404 static int s_do_sub_pub_unsub_test(struct aws_allocator *allocator, enum aws_mqtt5_qos qos) {
3405 aws_mqtt_library_init(allocator);
3406
3407 struct mqtt5_client_test_options test_options;
3408 aws_mqtt5_client_test_init_default_options(&test_options);
3409
3410 struct aws_mqtt5_client_mock_test_fixture test_context;
3411 struct aws_mqtt5_sub_pub_unsub_context full_test_context = {
3412 .test_fixture = &test_context,
3413 };
3414
3415 test_options.client_options.publish_received_handler = s_sub_pub_unsub_publish_received_fn;
3416 test_options.client_options.publish_received_handler_user_data = &full_test_context;
3417
3418 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] =
3419 aws_mqtt5_server_send_suback_on_subscribe;
3420 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] =
3421 aws_mqtt5_mock_server_handle_publish_puback_and_forward;
3422 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_UNSUBSCRIBE] =
3423 aws_mqtt5_mock_server_handle_unsubscribe_unsuback_success;
3424
3425 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
3426 .client_options = &test_options.client_options,
3427 .server_function_table = &test_options.server_function_table,
3428 .mock_server_user_data = &full_test_context,
3429 };
3430
3431 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
3432
3433 struct aws_mqtt5_client *client = test_context.client;
3434 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
3435
3436 aws_wait_for_connected_lifecycle_event(&test_context);
3437
3438 struct aws_mqtt5_packet_subscribe_storage expected_subscribe_storage;
3439 AWS_ZERO_STRUCT(expected_subscribe_storage);
3440 struct aws_mqtt5_packet_publish_storage expected_publish_storage;
3441 AWS_ZERO_STRUCT(expected_publish_storage);
3442 struct aws_mqtt5_packet_puback_storage expected_puback_storage;
3443 AWS_ZERO_STRUCT(expected_puback_storage);
3444 struct aws_mqtt5_packet_unsubscribe_storage expected_unsubscribe_storage;
3445 AWS_ZERO_STRUCT(expected_unsubscribe_storage);
3446
3447 ASSERT_SUCCESS(s_mqtt5_client_sub_pub_unsub_subscribe(&full_test_context, &expected_subscribe_storage));
3448 ASSERT_SUCCESS(s_mqtt5_client_sub_pub_unsub_publish(
3449 &full_test_context, qos, &expected_publish_storage, &expected_puback_storage));
3450 s_sub_pub_unsub_wait_for_publish_received(&full_test_context);
3451 ASSERT_SUCCESS(s_mqtt5_client_sub_pub_unsub_unsubscribe(&full_test_context, &expected_unsubscribe_storage));
3452
3453 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
3454
3455 aws_wait_for_stopped_lifecycle_event(&test_context);
3456
3457 /* verify packets that server received: connect,subscribe, publish, puback(if qos1), unsubscribe */
3458 struct aws_array_list expected_packets;
3459 aws_array_list_init_dynamic(&expected_packets, allocator, 5, sizeof(struct aws_mqtt5_mock_server_packet_record));
3460
3461 struct aws_mqtt5_mock_server_packet_record connect_record = {
3462 .packet_type = AWS_MQTT5_PT_CONNECT,
3463 .packet_storage = NULL,
3464 };
3465 aws_array_list_push_back(&expected_packets, &connect_record);
3466
3467 struct aws_mqtt5_mock_server_packet_record subscribe_record = {
3468 .packet_type = AWS_MQTT5_PT_SUBSCRIBE,
3469 .packet_storage = &expected_subscribe_storage,
3470 };
3471 aws_array_list_push_back(&expected_packets, &subscribe_record);
3472
3473 struct aws_mqtt5_mock_server_packet_record publish_record = {
3474 .packet_type = AWS_MQTT5_PT_PUBLISH,
3475 .packet_storage = &expected_publish_storage,
3476 };
3477 aws_array_list_push_back(&expected_packets, &publish_record);
3478
3479 if (qos == AWS_MQTT5_QOS_AT_LEAST_ONCE) {
3480 struct aws_mqtt5_mock_server_packet_record puback_record = {
3481 .packet_type = AWS_MQTT5_PT_PUBACK,
3482 .packet_storage = &expected_puback_storage,
3483 };
3484 aws_array_list_push_back(&expected_packets, &puback_record);
3485 }
3486
3487 struct aws_mqtt5_mock_server_packet_record unsubscribe_record = {
3488 .packet_type = AWS_MQTT5_PT_UNSUBSCRIBE,
3489 .packet_storage = &expected_unsubscribe_storage,
3490 };
3491 aws_array_list_push_back(&expected_packets, &unsubscribe_record);
3492
3493 ASSERT_SUCCESS(aws_verify_received_packet_sequence(
3494 &test_context, expected_packets.data, aws_array_list_length(&expected_packets)));
3495
3496 /* verify client received the publish that we sent */
3497 const struct aws_mqtt5_packet_publish_view *received_publish = &full_test_context.publish_storage.storage_view;
3498 ASSERT_TRUE((received_publish->packet_id != 0) == (qos == AWS_MQTT5_QOS_AT_LEAST_ONCE));
3499 ASSERT_INT_EQUALS((uint32_t)qos, (uint32_t)received_publish->qos);
3500
3501 ASSERT_BIN_ARRAYS_EQUALS(
3502 s_sub_pub_unsub_publish_topic,
3503 AWS_ARRAY_SIZE(s_sub_pub_unsub_publish_topic) - 1,
3504 received_publish->topic.ptr,
3505 received_publish->topic.len);
3506
3507 ASSERT_BIN_ARRAYS_EQUALS(
3508 s_sub_pub_unsub_publish_payload,
3509 AWS_ARRAY_SIZE(s_sub_pub_unsub_publish_payload) - 1,
3510 received_publish->payload.ptr,
3511 received_publish->payload.len);
3512
3513 aws_mqtt5_packet_subscribe_storage_clean_up(&expected_subscribe_storage);
3514 aws_mqtt5_packet_publish_storage_clean_up(&expected_publish_storage);
3515 aws_mqtt5_packet_puback_storage_clean_up(&expected_puback_storage);
3516 aws_mqtt5_packet_unsubscribe_storage_clean_up(&expected_unsubscribe_storage);
3517 aws_array_list_clean_up(&expected_packets);
3518
3519 s_sub_pub_unsub_context_clean_up(&full_test_context);
3520 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
3521 aws_mqtt_library_clean_up();
3522
3523 return AWS_OP_SUCCESS;
3524 }
3525
3526 static int s_mqtt5_client_sub_pub_unsub_qos0_fn(struct aws_allocator *allocator, void *ctx) {
3527 (void)ctx;
3528
3529 ASSERT_SUCCESS(s_do_sub_pub_unsub_test(allocator, AWS_MQTT5_QOS_AT_MOST_ONCE));
3530
3531 return AWS_OP_SUCCESS;
3532 }
3533
3534 AWS_TEST_CASE(mqtt5_client_sub_pub_unsub_qos0, s_mqtt5_client_sub_pub_unsub_qos0_fn)
3535
3536 static int s_mqtt5_client_sub_pub_unsub_qos1_fn(struct aws_allocator *allocator, void *ctx) {
3537 (void)ctx;
3538
3539 ASSERT_SUCCESS(s_do_sub_pub_unsub_test(allocator, AWS_MQTT5_QOS_AT_LEAST_ONCE));
3540
3541 return AWS_OP_SUCCESS;
3542 }
3543
3544 AWS_TEST_CASE(mqtt5_client_sub_pub_unsub_qos1, s_mqtt5_client_sub_pub_unsub_qos1_fn)
3545
3546 static enum aws_mqtt5_unsuback_reason_code s_unsubscribe_success_reason_codes[] = {
3547 AWS_MQTT5_UARC_NO_SUBSCRIPTION_EXISTED,
3548 };
3549
3550 static int s_aws_mqtt5_server_send_not_subscribe_unsuback_on_unsubscribe(
3551 void *packet,
3552 struct aws_mqtt5_server_mock_connection_context *connection,
3553 void *user_data) {
3554 (void)user_data;
3555
3556 struct aws_mqtt5_packet_unsubscribe_view *unsubscribe_view = packet;
3557
3558 struct aws_mqtt5_packet_unsuback_view unsuback_view = {
3559 .packet_id = unsubscribe_view->packet_id,
3560 .reason_code_count = AWS_ARRAY_SIZE(s_unsubscribe_success_reason_codes),
3561 .reason_codes = s_unsubscribe_success_reason_codes,
3562 };
3563
3564 return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_UNSUBACK, &unsuback_view);
3565 }
3566
3567 static int s_mqtt5_client_unsubscribe_success_fn(struct aws_allocator *allocator, void *ctx) {
3568 (void)ctx;
3569
3570 aws_mqtt_library_init(allocator);
3571
3572 struct mqtt5_client_test_options test_options;
3573 aws_mqtt5_client_test_init_default_options(&test_options);
3574
3575 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_UNSUBSCRIBE] =
3576 s_aws_mqtt5_server_send_not_subscribe_unsuback_on_unsubscribe;
3577
3578 struct aws_mqtt5_client_mock_test_fixture test_context;
3579 struct aws_mqtt5_sub_pub_unsub_context full_test_context = {
3580 .test_fixture = &test_context,
3581 };
3582
3583 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
3584 .client_options = &test_options.client_options,
3585 .server_function_table = &test_options.server_function_table,
3586 .mock_server_user_data = &full_test_context,
3587 };
3588
3589 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
3590
3591 struct aws_mqtt5_client *client = test_context.client;
3592 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
3593
3594 aws_wait_for_connected_lifecycle_event(&test_context);
3595
3596 struct aws_mqtt5_packet_unsubscribe_view unsubscribe_view = {
3597 .topic_filters = s_sub_pub_unsub_topic_filters,
3598 .topic_filter_count = AWS_ARRAY_SIZE(s_sub_pub_unsub_topic_filters),
3599 };
3600
3601 struct aws_mqtt5_unsubscribe_completion_options completion_options = {
3602 .completion_callback = s_sub_pub_unsub_unsubscribe_complete_fn,
3603 .completion_user_data = &full_test_context,
3604 };
3605 aws_mqtt5_client_unsubscribe(client, &unsubscribe_view, &completion_options);
3606
3607 s_sub_pub_unsub_wait_for_unsuback_received(&full_test_context);
3608
3609 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
3610
3611 aws_wait_for_stopped_lifecycle_event(&test_context);
3612
3613 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
3614 aws_mqtt_library_clean_up();
3615
3616 return AWS_OP_SUCCESS;
3617 }
3618
3619 AWS_TEST_CASE(mqtt5_client_unsubscribe_success, s_mqtt5_client_unsubscribe_success_fn)
3620
3621 static aws_mqtt5_packet_id_t s_puback_packet_id = 183;
3622
3623 struct aws_mqtt5_server_send_qos1_publish_context {
3624 struct aws_mqtt5_client_mock_test_fixture *test_fixture;
3625 bool publish_sent;
3626 bool connack_sent;
3627 bool connack_checked;
3628 };
3629
3630 static int s_aws_mqtt5_mock_server_handle_puback(
3631 void *packet,
3632 struct aws_mqtt5_server_mock_connection_context *connection,
3633 void *user_data) {
3634 (void)user_data;
3635
3636 struct aws_mqtt5_packet_puback_view *puback_view = packet;
3637
3638 ASSERT_INT_EQUALS(puback_view->packet_id, s_puback_packet_id);
3639 ASSERT_TRUE(puback_view->reason_code == AWS_MQTT5_PARC_SUCCESS);
3640
3641 struct aws_mqtt5_client_mock_test_fixture *test_fixture = connection->test_fixture;
3642 struct aws_mqtt5_server_send_qos1_publish_context *publish_context =
3643 connection->test_fixture->mock_server_user_data;
3644 aws_mutex_lock(&test_fixture->lock);
3645 publish_context->connack_checked = true;
3646 aws_mutex_unlock(&test_fixture->lock);
3647
3648 return AWS_OP_SUCCESS;
3649 }
3650
3651 static void s_aws_mqtt5_mock_server_send_qos1_publish(
3652 struct aws_mqtt5_server_mock_connection_context *mock_server,
3653 void *user_data) {
3654
3655 struct aws_mqtt5_server_send_qos1_publish_context *test_context = user_data;
3656 if (test_context->publish_sent || !test_context->connack_sent) {
3657 return;
3658 }
3659
3660 test_context->publish_sent = true;
3661
3662 struct aws_mqtt5_packet_publish_view qos1_publish_view = {
3663 .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE,
3664 .topic =
3665 {
3666 .ptr = s_topic,
3667 .len = AWS_ARRAY_SIZE(s_topic) - 1,
3668 },
3669 .packet_id = s_puback_packet_id,
3670 };
3671
3672 aws_mqtt5_mock_server_send_packet(mock_server, AWS_MQTT5_PT_PUBLISH, &qos1_publish_view);
3673 }
3674
3675 static int s_aws_mqtt5_server_send_qos1_publish_on_connect(
3676 void *packet,
3677 struct aws_mqtt5_server_mock_connection_context *connection,
3678 void *user_data) {
3679 int result = aws_mqtt5_mock_server_handle_connect_always_succeed(packet, connection, user_data);
3680
3681 struct aws_mqtt5_server_send_qos1_publish_context *test_context = user_data;
3682 test_context->connack_sent = true;
3683
3684 return result;
3685 }
3686
3687 static bool s_publish_qos1_puback(void *arg) {
3688 struct aws_mqtt5_server_send_qos1_publish_context *test_context = arg;
3689 return test_context->connack_checked;
3690 }
3691
3692 static void s_publish_qos1_wait_for_puback(struct aws_mqtt5_server_send_qos1_publish_context *test_context) {
3693 struct aws_mqtt5_client_mock_test_fixture *test_fixture = test_context->test_fixture;
3694 aws_mutex_lock(&test_fixture->lock);
3695 aws_condition_variable_wait_pred(&test_fixture->signal, &test_fixture->lock, s_publish_qos1_puback, test_context);
3696 aws_mutex_unlock(&test_fixture->lock);
3697 }
3698
3699 /* When client receives a QoS1 PUBLISH it must send a valid PUBACK with packet id */
3700 static int mqtt5_client_receive_qos1_return_puback_test_fn(struct aws_allocator *allocator, void *ctx) {
3701 (void)ctx;
3702
3703 aws_mqtt_library_init(allocator);
3704
3705 struct mqtt5_client_test_options test_options;
3706 aws_mqtt5_client_test_init_default_options(&test_options);
3707
3708 /* mock server sends a PUBLISH packet to the client */
3709 test_options.server_function_table.service_task_fn = s_aws_mqtt5_mock_server_send_qos1_publish;
3710 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBACK] = s_aws_mqtt5_mock_server_handle_puback;
3711 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] =
3712 s_aws_mqtt5_server_send_qos1_publish_on_connect;
3713
3714 struct aws_mqtt5_client_mock_test_fixture test_context;
3715
3716 struct aws_mqtt5_server_send_qos1_publish_context publish_context = {
3717 .test_fixture = &test_context,
3718 .publish_sent = false,
3719 .connack_sent = false,
3720 .connack_checked = false,
3721 };
3722
3723 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
3724 .client_options = &test_options.client_options,
3725 .server_function_table = &test_options.server_function_table,
3726 .mock_server_user_data = &publish_context,
3727 };
3728
3729 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
3730
3731 struct aws_mqtt5_client *client = test_context.client;
3732 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
3733
3734 aws_wait_for_connected_lifecycle_event(&test_context);
3735
3736 s_publish_qos1_wait_for_puback(&publish_context);
3737
3738 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
3739
3740 aws_wait_for_stopped_lifecycle_event(&test_context);
3741
3742 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
3743 aws_mqtt_library_clean_up();
3744
3745 return AWS_OP_SUCCESS;
3746 }
3747
3748 AWS_TEST_CASE(mqtt5_client_receive_qos1_return_puback_test, mqtt5_client_receive_qos1_return_puback_test_fn)
3749
3750 static int s_aws_mqtt5_mock_server_handle_connect_session_present(
3751 void *packet,
3752 struct aws_mqtt5_server_mock_connection_context *connection,
3753 void *user_data) {
3754 (void)packet;
3755 (void)user_data;
3756
3757 struct aws_mqtt5_packet_connack_view connack_view;
3758 AWS_ZERO_STRUCT(connack_view);
3759
3760 connack_view.reason_code = AWS_MQTT5_CRC_SUCCESS;
3761 connack_view.session_present = true;
3762
3763 return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_CONNACK, &connack_view);
3764 }
3765
3766 /* When client receives a CONNACK with existing session state when one isn't present it should disconnect */
3767 static int mqtt5_client_receive_nonexisting_session_state_fn(struct aws_allocator *allocator, void *ctx) {
3768 (void)ctx;
3769
3770 aws_mqtt_library_init(allocator);
3771
3772 struct mqtt5_client_test_options test_options;
3773 aws_mqtt5_client_test_init_default_options(&test_options);
3774
3775 /* mock server returns a CONNACK indicating a session is being resumed */
3776 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] =
3777 s_aws_mqtt5_mock_server_handle_connect_session_present;
3778
3779 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
3780 .client_options = &test_options.client_options,
3781 .server_function_table = &test_options.server_function_table,
3782 };
3783
3784 struct aws_mqtt5_client_mock_test_fixture test_context;
3785 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
3786
3787 struct aws_mqtt5_client *client = test_context.client;
3788 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
3789
3790 s_wait_for_connection_failure_lifecycle_event(&test_context);
3791
3792 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
3793
3794 aws_wait_for_stopped_lifecycle_event(&test_context);
3795
3796 enum aws_mqtt5_client_state expected_states[] = {
3797 AWS_MCS_CONNECTING,
3798 AWS_MCS_MQTT_CONNECT,
3799 AWS_MCS_CHANNEL_SHUTDOWN,
3800 };
3801 ASSERT_SUCCESS(aws_verify_client_state_sequence(&test_context, expected_states, AWS_ARRAY_SIZE(expected_states)));
3802
3803 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
3804 aws_mqtt_library_clean_up();
3805
3806 return AWS_OP_SUCCESS;
3807 }
3808
3809 AWS_TEST_CASE(mqtt5_client_receive_nonexisting_session_state, mqtt5_client_receive_nonexisting_session_state_fn)
3810
3811 static const char *s_receive_assigned_client_id_client_id = "Assigned_Client_ID";
3812
3813 struct aws_mqtt5_server_receive_assigned_client_id_context {
3814 struct aws_mqtt5_client_mock_test_fixture *test_fixture;
3815 bool assigned_client_id_checked;
3816 };
3817
3818 static int s_aws_mqtt5_mock_server_handle_connect_assigned_client_id(
3819 void *packet,
3820 struct aws_mqtt5_server_mock_connection_context *connection,
3821 void *user_data) {
3822 (void)user_data;
3823
3824 struct aws_mqtt5_packet_connect_view *connect_view = packet;
3825
3826 struct aws_mqtt5_packet_connack_view connack_view;
3827 AWS_ZERO_STRUCT(connack_view);
3828
3829 connack_view.reason_code = AWS_MQTT5_CRC_SUCCESS;
3830 struct aws_byte_cursor assigned_client_id = aws_byte_cursor_from_c_str(s_receive_assigned_client_id_client_id);
3831
3832 /* Server behavior sets the Assigned Client Identifier on a CONNECT packet with an empty Client ID */
3833 if (connect_view->client_id.len == 0) {
3834 connack_view.assigned_client_identifier = &assigned_client_id;
3835 } else {
3836 ASSERT_BIN_ARRAYS_EQUALS(
3837 assigned_client_id.ptr, assigned_client_id.len, connect_view->client_id.ptr, connect_view->client_id.len);
3838 struct aws_mqtt5_server_receive_assigned_client_id_context *test_context = user_data;
3839 test_context->assigned_client_id_checked = true;
3840 }
3841
3842 return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_CONNACK, &connack_view);
3843 }
3844
3845 /*
3846 * When client connects with a zero length Client ID, server provides one.
3847 * The client should then use the assigned Client ID on reconnection attempts.
3848 */
3849 static int mqtt5_client_receive_assigned_client_id_fn(struct aws_allocator *allocator, void *ctx) {
3850 (void)ctx;
3851
3852 aws_mqtt_library_init(allocator);
3853
3854 struct mqtt5_client_test_options test_options;
3855 aws_mqtt5_client_test_init_default_options(&test_options);
3856
3857 /* Empty the Client ID for connect */
3858 test_options.connect_options.client_id.len = 0;
3859
3860 /* mock server checks for a client ID and if it's missing sends an assigned one */
3861 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] =
3862 s_aws_mqtt5_mock_server_handle_connect_assigned_client_id;
3863
3864 struct aws_mqtt5_client_mock_test_fixture test_context;
3865 struct aws_mqtt5_server_receive_assigned_client_id_context assinged_id_context = {
3866 .test_fixture = &test_context,
3867 .assigned_client_id_checked = false,
3868 };
3869
3870 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
3871 .client_options = &test_options.client_options,
3872 .server_function_table = &test_options.server_function_table,
3873 .mock_server_user_data = &assinged_id_context,
3874 };
3875
3876 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
3877
3878 struct aws_mqtt5_client *client = test_context.client;
3879
3880 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
3881
3882 aws_wait_for_connected_lifecycle_event(&test_context);
3883
3884 struct aws_byte_cursor assigned_client_id = aws_byte_cursor_from_c_str(s_receive_assigned_client_id_client_id);
3885 struct aws_byte_cursor negotiated_settings_client_id =
3886 aws_byte_cursor_from_buf(&client->negotiated_settings.client_id_storage);
3887 /* Test that Assigned Client ID is stored */
3888 ASSERT_BIN_ARRAYS_EQUALS(
3889 assigned_client_id.ptr,
3890 assigned_client_id.len,
3891 negotiated_settings_client_id.ptr,
3892 negotiated_settings_client_id.len);
3893
3894 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
3895
3896 aws_wait_for_stopped_lifecycle_event(&test_context);
3897
3898 /* Check for Assigned Client ID on reconnect */
3899 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
3900
3901 aws_wait_for_connected_lifecycle_event(&test_context);
3902
3903 ASSERT_TRUE(assinged_id_context.assigned_client_id_checked);
3904
3905 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
3906
3907 aws_wait_for_stopped_lifecycle_event(&test_context);
3908
3909 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
3910 aws_mqtt_library_clean_up();
3911
3912 return AWS_OP_SUCCESS;
3913 }
3914
3915 AWS_TEST_CASE(mqtt5_client_receive_assigned_client_id, mqtt5_client_receive_assigned_client_id_fn);
3916
3917 #define TEST_PUBLISH_COUNT 10
3918
3919 static int s_aws_mqtt5_mock_server_handle_publish_no_puback_on_first_connect(
3920 void *packet,
3921 struct aws_mqtt5_server_mock_connection_context *connection,
3922 void *user_data) {
3923 (void)user_data;
3924
3925 struct aws_mqtt5_client_mock_test_fixture *test_fixture = connection->test_fixture;
3926 struct aws_mqtt5_packet_publish_view *publish_view = packet;
3927
3928 aws_mutex_lock(&test_fixture->lock);
3929 ++connection->test_fixture->publishes_received;
3930 aws_mutex_unlock(&test_fixture->lock);
3931
3932 /* Only send the PUBACK on the second attempt after a reconnect and restored session */
3933 if (publish_view->duplicate) {
3934 struct aws_mqtt5_packet_puback_view puback_view = {
3935 .packet_id = publish_view->packet_id,
3936 };
3937 return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_PUBACK, &puback_view);
3938 }
3939
3940 return AWS_OP_SUCCESS;
3941 }
3942
3943 static void s_receive_stored_session_publish_completion_fn(
3944 enum aws_mqtt5_packet_type packet_type,
3945 const void *packet,
3946 int error_code,
3947 void *complete_ctx) {
3948
3949 if (packet_type != AWS_MQTT5_PT_PUBACK) {
3950 return;
3951 }
3952
3953 const struct aws_mqtt5_packet_puback_view *puback = packet;
3954
3955 struct aws_mqtt5_client_mock_test_fixture *test_context = complete_ctx;
3956
3957 aws_mutex_lock(&test_context->lock);
3958
3959 if (error_code == AWS_ERROR_SUCCESS && puback->reason_code < 128) {
3960 ++test_context->successful_pubacks_received;
3961 }
3962
3963 aws_mutex_unlock(&test_context->lock);
3964 aws_condition_variable_notify_all(&test_context->signal);
3965 }
3966
3967 static bool s_received_n_unacked_publishes(void *arg) {
3968 struct aws_mqtt5_client_test_wait_for_n_context *context = arg;
3969 struct aws_mqtt5_client_mock_test_fixture *test_fixture = context->test_fixture;
3970
3971 return test_fixture->publishes_received >= context->required_event_count;
3972 }
3973
3974 static void s_wait_for_n_unacked_publishes(struct aws_mqtt5_client_test_wait_for_n_context *context) {
3975
3976 struct aws_mqtt5_client_mock_test_fixture *test_context = context->test_fixture;
3977 aws_mutex_lock(&test_context->lock);
3978 aws_condition_variable_wait_pred(
3979 &test_context->signal, &test_context->lock, s_received_n_unacked_publishes, context);
3980 aws_mutex_unlock(&test_context->lock);
3981 }
3982
3983 static int mqtt5_client_no_session_after_client_stop_fn(struct aws_allocator *allocator, void *ctx) {
3984 (void)ctx;
3985
3986 aws_mqtt_library_init(allocator);
3987
3988 struct mqtt5_client_test_options test_options;
3989 aws_mqtt5_client_test_init_default_options(&test_options);
3990
3991 /* Set to rejoin */
3992 test_options.client_options.session_behavior = AWS_MQTT5_CSBT_REJOIN_POST_SUCCESS;
3993
3994 /* mock server will not send PUBACKS on initial connect */
3995 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] =
3996 s_aws_mqtt5_mock_server_handle_publish_no_puback_on_first_connect;
3997 /* Simulate reconnecting to an existing connection */
3998 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] =
3999 s_aws_mqtt5_mock_server_handle_connect_honor_session_after_success;
4000
4001 struct aws_mqtt5_client_mock_test_fixture test_context;
4002
4003 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
4004 .client_options = &test_options.client_options,
4005 .server_function_table = &test_options.server_function_table,
4006 };
4007
4008 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
4009
4010 struct aws_mqtt5_client *client = test_context.client;
4011
4012 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
4013
4014 aws_wait_for_connected_lifecycle_event(&test_context);
4015
4016 for (size_t i = 0; i < TEST_PUBLISH_COUNT; ++i) {
4017 struct aws_mqtt5_packet_publish_view qos1_publish_view = {
4018 .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE,
4019 .topic =
4020 {
4021 .ptr = s_topic,
4022 .len = AWS_ARRAY_SIZE(s_topic) - 1,
4023 },
4024 };
4025
4026 struct aws_mqtt5_publish_completion_options completion_options = {
4027 .completion_callback = s_receive_stored_session_publish_completion_fn,
4028 .completion_user_data = &test_context,
4029 };
4030
4031 ASSERT_SUCCESS(aws_mqtt5_client_publish(client, &qos1_publish_view, &completion_options));
4032 }
4033
4034 /* Wait for publishes to have gone out from client */
4035 struct aws_mqtt5_client_test_wait_for_n_context wait_context = {
4036 .test_fixture = &test_context,
4037 .required_event_count = TEST_PUBLISH_COUNT,
4038 };
4039 s_wait_for_n_unacked_publishes(&wait_context);
4040
4041 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
4042
4043 aws_wait_for_stopped_lifecycle_event(&test_context);
4044
4045 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
4046
4047 aws_wait_for_connected_lifecycle_event(&test_context);
4048
4049 aws_mutex_lock(&test_context.lock);
4050 size_t event_count = aws_array_list_length(&test_context.lifecycle_events);
4051 struct aws_mqtt5_lifecycle_event_record *record = NULL;
4052 aws_array_list_get_at(&test_context.lifecycle_events, &record, event_count - 1);
4053 aws_mutex_unlock(&test_context.lock);
4054
4055 ASSERT_FALSE(record->connack_storage.storage_view.session_present);
4056
4057 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
4058 aws_wait_for_stopped_lifecycle_event(&test_context);
4059
4060 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
4061 aws_mqtt_library_clean_up();
4062
4063 return AWS_OP_SUCCESS;
4064 }
4065
4066 AWS_TEST_CASE(mqtt5_client_no_session_after_client_stop, mqtt5_client_no_session_after_client_stop_fn);
4067
4068 static int mqtt5_client_restore_session_on_ping_timeout_reconnect_fn(struct aws_allocator *allocator, void *ctx) {
4069 (void)ctx;
4070
4071 aws_mqtt_library_init(allocator);
4072
4073 struct mqtt5_client_test_options test_options;
4074 aws_mqtt5_client_test_init_default_options(&test_options);
4075
4076 /* Set to rejoin */
4077 test_options.client_options.session_behavior = AWS_MQTT5_CSBT_REJOIN_POST_SUCCESS;
4078 /* faster ping timeout */
4079 test_options.client_options.ping_timeout_ms = 3000;
4080 test_options.connect_options.keep_alive_interval_seconds = 5;
4081
4082 /* don't respond to PINGREQs */
4083 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PINGREQ] = NULL;
4084 /* mock server will not send PUBACKS on initial connect */
4085 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] =
4086 s_aws_mqtt5_mock_server_handle_publish_no_puback_on_first_connect;
4087 /* Simulate reconnecting to an existing connection */
4088 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] =
4089 s_aws_mqtt5_mock_server_handle_connect_honor_session_after_success;
4090
4091 struct aws_mqtt5_client_mock_test_fixture test_context;
4092
4093 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
4094 .client_options = &test_options.client_options,
4095 .server_function_table = &test_options.server_function_table,
4096 };
4097
4098 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
4099
4100 struct aws_mqtt5_client *client = test_context.client;
4101
4102 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
4103
4104 aws_wait_for_connected_lifecycle_event(&test_context);
4105
4106 for (size_t i = 0; i < TEST_PUBLISH_COUNT; ++i) {
4107 struct aws_mqtt5_packet_publish_view qos1_publish_view = {
4108 .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE,
4109 .topic =
4110 {
4111 .ptr = s_topic,
4112 .len = AWS_ARRAY_SIZE(s_topic) - 1,
4113 },
4114 };
4115
4116 struct aws_mqtt5_publish_completion_options completion_options = {
4117 .completion_callback = s_receive_stored_session_publish_completion_fn,
4118 .completion_user_data = &test_context,
4119 };
4120
4121 ASSERT_SUCCESS(aws_mqtt5_client_publish(client, &qos1_publish_view, &completion_options));
4122 }
4123
4124 /* Wait for publishes to have gone out from client */
4125 struct aws_mqtt5_client_test_wait_for_n_context wait_context = {
4126 .test_fixture = &test_context,
4127 .required_event_count = TEST_PUBLISH_COUNT,
4128 };
4129 s_wait_for_n_unacked_publishes(&wait_context);
4130
4131 /* disconnect due to failed ping */
4132 s_wait_for_disconnection_lifecycle_event(&test_context);
4133
4134 /* Reconnect from a disconnect automatically */
4135 aws_wait_for_connected_lifecycle_event(&test_context);
4136
4137 s_wait_for_n_successful_publishes(&wait_context);
4138
4139 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
4140 aws_wait_for_stopped_lifecycle_event(&test_context);
4141
4142 enum aws_mqtt5_client_state expected_states[] = {
4143 AWS_MCS_CONNECTING,
4144 AWS_MCS_MQTT_CONNECT,
4145 AWS_MCS_CONNECTED,
4146 AWS_MCS_CLEAN_DISCONNECT,
4147 AWS_MCS_CHANNEL_SHUTDOWN,
4148 AWS_MCS_PENDING_RECONNECT,
4149 AWS_MCS_CONNECTING,
4150 AWS_MCS_MQTT_CONNECT,
4151 AWS_MCS_CONNECTED,
4152 AWS_MCS_CHANNEL_SHUTDOWN,
4153 AWS_MCS_STOPPED,
4154 };
4155
4156 ASSERT_SUCCESS(aws_verify_client_state_sequence(&test_context, expected_states, AWS_ARRAY_SIZE(expected_states)));
4157
4158 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
4159 aws_mqtt_library_clean_up();
4160
4161 return AWS_OP_SUCCESS;
4162 }
4163
4164 AWS_TEST_CASE(
4165 mqtt5_client_restore_session_on_ping_timeout_reconnect,
4166 mqtt5_client_restore_session_on_ping_timeout_reconnect_fn);
4167
4168 /* If the server returns a Clean Session, client must discard any existing Session and start a new Session */
4169 static int mqtt5_client_discard_session_on_server_clean_start_fn(struct aws_allocator *allocator, void *ctx) {
4170 (void)ctx;
4171
4172 aws_mqtt_library_init(allocator);
4173
4174 struct mqtt5_client_test_options test_options;
4175 aws_mqtt5_client_test_init_default_options(&test_options);
4176
4177 /* Set to rejoin */
4178 test_options.client_options.session_behavior = AWS_MQTT5_CSBT_REJOIN_POST_SUCCESS;
4179
4180 /* mock server will not send PUBACKS on initial connect */
4181 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] =
4182 s_aws_mqtt5_mock_server_handle_publish_no_puback_on_first_connect;
4183
4184 struct aws_mqtt5_client_mock_test_fixture test_context;
4185
4186 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
4187 .client_options = &test_options.client_options,
4188 .server_function_table = &test_options.server_function_table,
4189 };
4190
4191 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
4192
4193 struct aws_mqtt5_client *client = test_context.client;
4194
4195 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
4196
4197 aws_wait_for_connected_lifecycle_event(&test_context);
4198
4199 for (size_t i = 0; i < TEST_PUBLISH_COUNT; ++i) {
4200 struct aws_mqtt5_packet_publish_view qos1_publish_view = {
4201 .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE,
4202 .topic =
4203 {
4204 .ptr = s_topic,
4205 .len = AWS_ARRAY_SIZE(s_topic) - 1,
4206 },
4207 };
4208
4209 struct aws_mqtt5_publish_completion_options completion_options = {
4210 .completion_callback = s_receive_stored_session_publish_completion_fn,
4211 .completion_user_data = &test_context,
4212 };
4213
4214 ASSERT_SUCCESS(aws_mqtt5_client_publish(client, &qos1_publish_view, &completion_options));
4215 }
4216
4217 /* Wait for QoS1 publishes to have gone out from client */
4218 struct aws_mqtt5_client_test_wait_for_n_context wait_context = {
4219 .test_fixture = &test_context,
4220 .required_event_count = TEST_PUBLISH_COUNT,
4221 };
4222 s_wait_for_n_unacked_publishes(&wait_context);
4223
4224 /* Disconnect with unacked publishes */
4225 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
4226 aws_wait_for_stopped_lifecycle_event(&test_context);
4227
4228 /* Reconnect with a Client Stored Session */
4229 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
4230 aws_wait_for_connected_lifecycle_event(&test_context);
4231
4232 /* Provide time for Client to process any queued operations */
4233 aws_thread_current_sleep(1000000000);
4234
4235 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
4236 aws_wait_for_stopped_lifecycle_event(&test_context);
4237
4238 /* Check that no publishes were resent after the initial batch on first connect */
4239 ASSERT_INT_EQUALS(test_context.publishes_received, TEST_PUBLISH_COUNT);
4240
4241 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
4242 aws_mqtt_library_clean_up();
4243
4244 return AWS_OP_SUCCESS;
4245 }
4246
4247 AWS_TEST_CASE(
4248 mqtt5_client_discard_session_on_server_clean_start,
4249 mqtt5_client_discard_session_on_server_clean_start_fn);
4250
4251 static int s_verify_zero_statistics(struct aws_mqtt5_client_operation_statistics *stats) {
4252 ASSERT_INT_EQUALS(stats->incomplete_operation_size, 0);
4253 ASSERT_INT_EQUALS(stats->incomplete_operation_count, 0);
4254 ASSERT_INT_EQUALS(stats->unacked_operation_size, 0);
4255 ASSERT_INT_EQUALS(stats->unacked_operation_count, 0);
4256
4257 return AWS_OP_SUCCESS;
4258 }
4259
4260 static int s_verify_statistics_equal(
4261 struct aws_mqtt5_client_operation_statistics *expected_stats,
4262 struct aws_mqtt5_client_operation_statistics *actual_stats) {
4263 ASSERT_INT_EQUALS(expected_stats->incomplete_operation_size, actual_stats->incomplete_operation_size);
4264 ASSERT_INT_EQUALS(expected_stats->incomplete_operation_count, actual_stats->incomplete_operation_count);
4265 ASSERT_INT_EQUALS(expected_stats->unacked_operation_size, actual_stats->unacked_operation_size);
4266 ASSERT_INT_EQUALS(expected_stats->unacked_operation_size, actual_stats->unacked_operation_size);
4267
4268 return AWS_OP_SUCCESS;
4269 }
4270
4271 static int s_verify_client_statistics(
4272 struct aws_mqtt5_client_mock_test_fixture *test_context,
4273 struct aws_mqtt5_client_operation_statistics *expected_stats,
4274 size_t expected_stats_count) {
4275 struct aws_array_list *actual_stats = &test_context->client_statistics;
4276 size_t actual_stats_count = aws_array_list_length(actual_stats);
4277
4278 /* we expect the last stats to be zero, the expected stats represent the stats before that */
4279 ASSERT_INT_EQUALS(actual_stats_count, expected_stats_count + 1);
4280
4281 struct aws_mqtt5_client_operation_statistics *current_stats = NULL;
4282 aws_array_list_get_at_ptr(actual_stats, (void **)&current_stats, actual_stats_count - 1);
4283 ASSERT_SUCCESS(s_verify_zero_statistics(current_stats));
4284
4285 for (size_t i = 0; i < expected_stats_count; ++i) {
4286 aws_array_list_get_at_ptr(actual_stats, (void **)&current_stats, i);
4287 ASSERT_SUCCESS(s_verify_statistics_equal(&expected_stats[i], current_stats));
4288 }
4289
4290 return AWS_OP_SUCCESS;
4291 }
4292
4293 static struct aws_mqtt5_client_operation_statistics s_subscribe_test_statistics[] = {
4294 {
4295 .incomplete_operation_size = 68,
4296 .incomplete_operation_count = 1,
4297 .unacked_operation_size = 0,
4298 .unacked_operation_count = 0,
4299 },
4300 {
4301 .incomplete_operation_size = 68,
4302 .incomplete_operation_count = 1,
4303 .unacked_operation_size = 68,
4304 .unacked_operation_count = 1,
4305 },
4306 };
4307
4308 static int s_mqtt5_client_statistics_subscribe_fn(struct aws_allocator *allocator, void *ctx) {
4309 (void)ctx;
4310
4311 aws_mqtt_library_init(allocator);
4312
4313 struct mqtt5_client_test_options test_options;
4314 aws_mqtt5_client_test_init_default_options(&test_options);
4315
4316 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] =
4317 aws_mqtt5_server_send_suback_on_subscribe;
4318
4319 struct aws_mqtt5_client_mock_test_fixture test_context;
4320
4321 struct aws_mqtt5_server_disconnect_test_context disconnect_context = {
4322 .test_fixture = &test_context,
4323 .disconnect_sent = false,
4324 };
4325
4326 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
4327 .client_options = &test_options.client_options,
4328 .server_function_table = &test_options.server_function_table,
4329 .mock_server_user_data = &disconnect_context,
4330 };
4331
4332 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
4333
4334 struct aws_mqtt5_client *client = test_context.client;
4335 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
4336
4337 aws_wait_for_connected_lifecycle_event(&test_context);
4338
4339 struct aws_mqtt5_packet_subscribe_view subscribe_view = {
4340 .subscriptions = s_subscriptions,
4341 .subscription_count = AWS_ARRAY_SIZE(s_subscriptions),
4342 };
4343
4344 struct aws_mqtt5_subscribe_completion_options completion_options = {
4345 .completion_callback = s_aws_mqtt5_subscribe_complete_fn,
4346 .completion_user_data = &test_context,
4347 };
4348 aws_mqtt5_client_subscribe(client, &subscribe_view, &completion_options);
4349
4350 s_wait_for_suback_received(&test_context);
4351
4352 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
4353
4354 aws_wait_for_stopped_lifecycle_event(&test_context);
4355
4356 ASSERT_SUCCESS(s_verify_client_statistics(
4357 &test_context, s_subscribe_test_statistics, AWS_ARRAY_SIZE(s_subscribe_test_statistics)));
4358
4359 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
4360 aws_mqtt_library_clean_up();
4361
4362 return AWS_OP_SUCCESS;
4363 }
4364
4365 AWS_TEST_CASE(mqtt5_client_statistics_subscribe, s_mqtt5_client_statistics_subscribe_fn)
4366
4367 static struct aws_mqtt5_client_operation_statistics s_unsubscribe_test_statistics[] = {
4368 {
4369 .incomplete_operation_size = 14,
4370 .incomplete_operation_count = 1,
4371 .unacked_operation_size = 0,
4372 .unacked_operation_count = 0,
4373 },
4374 {
4375 .incomplete_operation_size = 14,
4376 .incomplete_operation_count = 1,
4377 .unacked_operation_size = 14,
4378 .unacked_operation_count = 1,
4379 },
4380 };
4381
4382 static int s_mqtt5_client_statistics_unsubscribe_fn(struct aws_allocator *allocator, void *ctx) {
4383 (void)ctx;
4384
4385 aws_mqtt_library_init(allocator);
4386
4387 struct mqtt5_client_test_options test_options;
4388 aws_mqtt5_client_test_init_default_options(&test_options);
4389
4390 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_UNSUBSCRIBE] =
4391 s_aws_mqtt5_server_send_not_subscribe_unsuback_on_unsubscribe;
4392
4393 struct aws_mqtt5_client_mock_test_fixture test_context;
4394 struct aws_mqtt5_sub_pub_unsub_context full_test_context = {
4395 .test_fixture = &test_context,
4396 };
4397
4398 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
4399 .client_options = &test_options.client_options,
4400 .server_function_table = &test_options.server_function_table,
4401 .mock_server_user_data = &full_test_context,
4402 };
4403
4404 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
4405
4406 struct aws_mqtt5_client *client = test_context.client;
4407 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
4408
4409 aws_wait_for_connected_lifecycle_event(&test_context);
4410
4411 struct aws_mqtt5_packet_unsubscribe_view unsubscribe_view = {
4412 .topic_filters = s_sub_pub_unsub_topic_filters,
4413 .topic_filter_count = AWS_ARRAY_SIZE(s_sub_pub_unsub_topic_filters),
4414 };
4415
4416 struct aws_mqtt5_unsubscribe_completion_options completion_options = {
4417 .completion_callback = s_sub_pub_unsub_unsubscribe_complete_fn,
4418 .completion_user_data = &full_test_context,
4419 };
4420 aws_mqtt5_client_unsubscribe(client, &unsubscribe_view, &completion_options);
4421
4422 s_sub_pub_unsub_wait_for_unsuback_received(&full_test_context);
4423
4424 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
4425
4426 aws_wait_for_stopped_lifecycle_event(&test_context);
4427
4428 ASSERT_SUCCESS(s_verify_client_statistics(
4429 &test_context, s_unsubscribe_test_statistics, AWS_ARRAY_SIZE(s_unsubscribe_test_statistics)));
4430
4431 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
4432 aws_mqtt_library_clean_up();
4433
4434 return AWS_OP_SUCCESS;
4435 }
4436
4437 AWS_TEST_CASE(mqtt5_client_statistics_unsubscribe, s_mqtt5_client_statistics_unsubscribe_fn)
4438
4439 static struct aws_mqtt5_client_operation_statistics s_publish_qos1_test_statistics[] = {
4440 {
4441 .incomplete_operation_size = 30,
4442 .incomplete_operation_count = 1,
4443 .unacked_operation_size = 0,
4444 .unacked_operation_count = 0,
4445 },
4446 {
4447 .incomplete_operation_size = 30,
4448 .incomplete_operation_count = 1,
4449 .unacked_operation_size = 30,
4450 .unacked_operation_count = 1,
4451 },
4452 };
4453
4454 static int s_do_mqtt5_client_statistics_publish_test(
4455 struct aws_allocator *allocator,
4456 enum aws_mqtt5_qos qos,
4457 struct aws_mqtt5_client_operation_statistics *expected_stats,
4458 size_t expected_stats_count) {
4459 aws_mqtt_library_init(allocator);
4460
4461 struct mqtt5_client_test_options test_options;
4462 aws_mqtt5_client_test_init_default_options(&test_options);
4463
4464 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] =
4465 aws_mqtt5_mock_server_handle_publish_puback_and_forward;
4466
4467 struct aws_mqtt5_client_mock_test_fixture test_context;
4468 struct aws_mqtt5_sub_pub_unsub_context full_test_context = {
4469 .test_fixture = &test_context,
4470 };
4471
4472 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
4473 .client_options = &test_options.client_options,
4474 .server_function_table = &test_options.server_function_table,
4475 .mock_server_user_data = &full_test_context,
4476 };
4477
4478 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
4479
4480 struct aws_mqtt5_client *client = test_context.client;
4481 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
4482
4483 aws_wait_for_connected_lifecycle_event(&test_context);
4484
4485 struct aws_mqtt5_packet_publish_view publish_view = {
4486 .qos = qos,
4487 .topic =
4488 {
4489 .ptr = s_sub_pub_unsub_publish_topic,
4490 .len = AWS_ARRAY_SIZE(s_sub_pub_unsub_publish_topic) - 1,
4491 },
4492 .payload =
4493 {
4494 .ptr = s_sub_pub_unsub_publish_payload,
4495 .len = AWS_ARRAY_SIZE(s_sub_pub_unsub_publish_payload) - 1,
4496 },
4497 };
4498
4499 struct aws_mqtt5_publish_completion_options completion_options = {
4500 .completion_callback = s_sub_pub_unsub_publish_complete_fn,
4501 .completion_user_data = &full_test_context,
4502 };
4503 aws_mqtt5_client_publish(client, &publish_view, &completion_options);
4504
4505 s_sub_pub_unsub_wait_for_publish_complete(&full_test_context);
4506
4507 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
4508
4509 aws_wait_for_stopped_lifecycle_event(&test_context);
4510
4511 ASSERT_SUCCESS(s_verify_client_statistics(&test_context, expected_stats, expected_stats_count));
4512
4513 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
4514 aws_mqtt_library_clean_up();
4515
4516 return AWS_OP_SUCCESS;
4517 }
4518
4519 static int s_mqtt5_client_statistics_publish_qos1_fn(struct aws_allocator *allocator, void *ctx) {
4520 (void)ctx;
4521
4522 ASSERT_SUCCESS(s_do_mqtt5_client_statistics_publish_test(
4523 allocator,
4524 AWS_MQTT5_QOS_AT_LEAST_ONCE,
4525 s_publish_qos1_test_statistics,
4526 AWS_ARRAY_SIZE(s_publish_qos1_test_statistics)));
4527
4528 return AWS_OP_SUCCESS;
4529 }
4530
4531 AWS_TEST_CASE(mqtt5_client_statistics_publish_qos1, s_mqtt5_client_statistics_publish_qos1_fn)
4532
4533 static struct aws_mqtt5_client_operation_statistics s_publish_qos0_test_statistics[] = {
4534 {
4535 .incomplete_operation_size = 30,
4536 .incomplete_operation_count = 1,
4537 .unacked_operation_size = 0,
4538 .unacked_operation_count = 0,
4539 },
4540 };
4541
4542 static int s_mqtt5_client_statistics_publish_qos0_fn(struct aws_allocator *allocator, void *ctx) {
4543 (void)ctx;
4544
4545 ASSERT_SUCCESS(s_do_mqtt5_client_statistics_publish_test(
4546 allocator,
4547 AWS_MQTT5_QOS_AT_MOST_ONCE,
4548 s_publish_qos0_test_statistics,
4549 AWS_ARRAY_SIZE(s_publish_qos0_test_statistics)));
4550
4551 return AWS_OP_SUCCESS;
4552 }
4553
4554 AWS_TEST_CASE(mqtt5_client_statistics_publish_qos0, s_mqtt5_client_statistics_publish_qos0_fn)
4555
4556 static struct aws_mqtt5_client_operation_statistics s_publish_qos1_requeue_test_statistics[] = {
4557 {
4558 .incomplete_operation_size = 30,
4559 .incomplete_operation_count = 1,
4560 .unacked_operation_size = 0,
4561 .unacked_operation_count = 0,
4562 },
4563 {
4564 .incomplete_operation_size = 30,
4565 .incomplete_operation_count = 1,
4566 .unacked_operation_size = 30,
4567 .unacked_operation_count = 1,
4568 },
4569 {
4570 .incomplete_operation_size = 30,
4571 .incomplete_operation_count = 1,
4572 .unacked_operation_size = 0,
4573 .unacked_operation_count = 0,
4574 },
4575 {
4576 .incomplete_operation_size = 30,
4577 .incomplete_operation_count = 1,
4578 .unacked_operation_size = 30,
4579 .unacked_operation_count = 1,
4580 },
4581 };
4582
4583 static int s_aws_mqtt5_server_disconnect_on_first_publish_puback_after(
4584 void *packet,
4585 struct aws_mqtt5_server_mock_connection_context *connection,
4586 void *user_data) {
4587 (void)packet;
4588
4589 struct aws_mqtt5_sub_pub_unsub_context *test_context = user_data;
4590 ++test_context->publishes_received;
4591 if (test_context->publishes_received == 1) {
4592 return AWS_OP_ERR;
4593 }
4594
4595 struct aws_mqtt5_packet_publish_view *publish_view = packet;
4596
4597 /* send a PUBACK? */
4598 if (publish_view->qos == AWS_MQTT5_QOS_AT_LEAST_ONCE) {
4599 struct aws_mqtt5_packet_puback_view puback_view = {
4600 .packet_id = publish_view->packet_id,
4601 .reason_code = AWS_MQTT5_PARC_SUCCESS,
4602 };
4603
4604 if (aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_PUBACK, &puback_view)) {
4605 return AWS_OP_ERR;
4606 }
4607 }
4608
4609 return AWS_OP_SUCCESS;
4610 }
4611
4612 static int s_mqtt5_client_statistics_publish_qos1_requeue_fn(struct aws_allocator *allocator, void *ctx) {
4613 (void)ctx;
4614
4615 aws_mqtt_library_init(allocator);
4616
4617 struct mqtt5_client_test_options test_options;
4618 aws_mqtt5_client_test_init_default_options(&test_options);
4619
4620 test_options.client_options.session_behavior = AWS_MQTT5_CSBT_REJOIN_POST_SUCCESS;
4621
4622 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] =
4623 s_aws_mqtt5_server_disconnect_on_first_publish_puback_after;
4624 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] =
4625 s_aws_mqtt5_mock_server_handle_connect_honor_session_after_success;
4626
4627 struct aws_mqtt5_client_mock_test_fixture test_context;
4628 struct aws_mqtt5_sub_pub_unsub_context full_test_context = {
4629 .test_fixture = &test_context,
4630 };
4631
4632 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
4633 .client_options = &test_options.client_options,
4634 .server_function_table = &test_options.server_function_table,
4635 .mock_server_user_data = &full_test_context,
4636 };
4637
4638 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
4639
4640 struct aws_mqtt5_client *client = test_context.client;
4641 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
4642
4643 aws_wait_for_connected_lifecycle_event(&test_context);
4644
4645 struct aws_mqtt5_packet_publish_view publish_view = {
4646 .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE,
4647 .topic =
4648 {
4649 .ptr = s_sub_pub_unsub_publish_topic,
4650 .len = AWS_ARRAY_SIZE(s_sub_pub_unsub_publish_topic) - 1,
4651 },
4652 .payload =
4653 {
4654 .ptr = s_sub_pub_unsub_publish_payload,
4655 .len = AWS_ARRAY_SIZE(s_sub_pub_unsub_publish_payload) - 1,
4656 },
4657 };
4658
4659 struct aws_mqtt5_publish_completion_options completion_options = {
4660 .completion_callback = s_sub_pub_unsub_publish_complete_fn,
4661 .completion_user_data = &full_test_context,
4662 };
4663 aws_mqtt5_client_publish(client, &publish_view, &completion_options);
4664
4665 s_sub_pub_unsub_wait_for_publish_complete(&full_test_context);
4666
4667 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
4668
4669 aws_wait_for_stopped_lifecycle_event(&test_context);
4670
4671 ASSERT_SUCCESS(s_verify_client_statistics(
4672 &test_context, s_publish_qos1_requeue_test_statistics, AWS_ARRAY_SIZE(s_publish_qos1_requeue_test_statistics)));
4673
4674 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
4675 aws_mqtt_library_clean_up();
4676
4677 return AWS_OP_SUCCESS;
4678 }
4679
4680 AWS_TEST_CASE(mqtt5_client_statistics_publish_qos1_requeue, s_mqtt5_client_statistics_publish_qos1_requeue_fn)
4681
4682 #define PUBACK_ORDERING_PUBLISH_COUNT 5
4683
4684 static int s_aws_mqtt5_server_send_multiple_publishes(
4685 void *packet,
4686 struct aws_mqtt5_server_mock_connection_context *connection,
4687 void *user_data) {
4688 (void)packet;
4689 (void)user_data;
4690
4691 /* in order: send PUBLISH packet ids 1, 2, 3, 4, 5 */
4692 for (size_t i = 0; i < PUBACK_ORDERING_PUBLISH_COUNT; ++i) {
4693 struct aws_mqtt5_packet_publish_view publish_view = {
4694 .packet_id = (uint16_t)i + 1,
4695 .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE,
4696 .topic =
4697 {
4698 .ptr = s_sub_pub_unsub_publish_topic,
4699 .len = AWS_ARRAY_SIZE(s_sub_pub_unsub_publish_topic) - 1,
4700 },
4701 .payload =
4702 {
4703 .ptr = s_sub_pub_unsub_publish_payload,
4704 .len = AWS_ARRAY_SIZE(s_sub_pub_unsub_publish_payload) - 1,
4705 },
4706 };
4707
4708 if (aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_PUBLISH, &publish_view)) {
4709 return AWS_OP_ERR;
4710 }
4711 }
4712
4713 return AWS_OP_SUCCESS;
4714 }
4715
4716 static bool s_server_received_all_pubacks(void *arg) {
4717 struct aws_mqtt5_client_mock_test_fixture *test_fixture = arg;
4718
4719 size_t pubacks_received = 0;
4720 size_t packet_count = aws_array_list_length(&test_fixture->server_received_packets);
4721 for (size_t i = 0; i < packet_count; ++i) {
4722 struct aws_mqtt5_mock_server_packet_record *packet = NULL;
4723 aws_array_list_get_at_ptr(&test_fixture->server_received_packets, (void **)&packet, i);
4724 if (packet->packet_type == AWS_MQTT5_PT_PUBACK) {
4725 ++pubacks_received;
4726 }
4727 }
4728
4729 return pubacks_received == PUBACK_ORDERING_PUBLISH_COUNT;
4730 }
4731
4732 static void s_wait_for_mock_server_pubacks(struct aws_mqtt5_client_mock_test_fixture *test_context) {
4733 aws_mutex_lock(&test_context->lock);
4734 aws_condition_variable_wait_pred(
4735 &test_context->signal, &test_context->lock, s_server_received_all_pubacks, test_context);
4736 aws_mutex_unlock(&test_context->lock);
4737 }
4738
4739 static int s_verify_mock_puback_order(struct aws_mqtt5_client_mock_test_fixture *test_context) {
4740
4741 aws_mutex_lock(&test_context->lock);
4742
4743 uint16_t expected_packet_id = 1;
4744 size_t packet_count = aws_array_list_length(&test_context->server_received_packets);
4745
4746 /* in order: received PUBACK packet ids 1, 2, 3, 4, 5 */
4747 for (size_t i = 0; i < packet_count; ++i) {
4748 struct aws_mqtt5_mock_server_packet_record *packet = NULL;
4749 aws_array_list_get_at_ptr(&test_context->server_received_packets, (void **)&packet, i);
4750 if (packet->packet_type == AWS_MQTT5_PT_PUBACK) {
4751 struct aws_mqtt5_packet_puback_view *puback_view =
4752 &((struct aws_mqtt5_packet_puback_storage *)(packet->packet_storage))->storage_view;
4753 ASSERT_INT_EQUALS(expected_packet_id, puback_view->packet_id);
4754 ++expected_packet_id;
4755 }
4756 }
4757
4758 ASSERT_INT_EQUALS(PUBACK_ORDERING_PUBLISH_COUNT + 1, expected_packet_id);
4759
4760 aws_mutex_unlock(&test_context->lock);
4761
4762 return AWS_OP_SUCCESS;
4763 }
4764
4765 static int s_mqtt5_client_puback_ordering_fn(struct aws_allocator *allocator, void *ctx) {
4766 (void)ctx;
4767
4768 aws_mqtt_library_init(allocator);
4769
4770 struct mqtt5_client_test_options test_options;
4771 aws_mqtt5_client_test_init_default_options(&test_options);
4772
4773 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] =
4774 s_aws_mqtt5_server_send_multiple_publishes;
4775
4776 struct aws_mqtt5_client_mock_test_fixture test_context;
4777 struct aws_mqtt5_sub_pub_unsub_context full_test_context = {
4778 .test_fixture = &test_context,
4779 };
4780
4781 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
4782 .client_options = &test_options.client_options,
4783 .server_function_table = &test_options.server_function_table,
4784 .mock_server_user_data = &full_test_context,
4785 };
4786
4787 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
4788
4789 struct aws_mqtt5_client *client = test_context.client;
4790 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
4791
4792 aws_wait_for_connected_lifecycle_event(&test_context);
4793
4794 struct aws_mqtt5_packet_publish_view publish_view = {
4795 .qos = AWS_MQTT5_QOS_AT_MOST_ONCE,
4796 .topic =
4797 {
4798 .ptr = s_sub_pub_unsub_publish_topic,
4799 .len = AWS_ARRAY_SIZE(s_sub_pub_unsub_publish_topic) - 1,
4800 },
4801 .payload =
4802 {
4803 .ptr = s_sub_pub_unsub_publish_payload,
4804 .len = AWS_ARRAY_SIZE(s_sub_pub_unsub_publish_payload) - 1,
4805 },
4806 };
4807
4808 aws_mqtt5_client_publish(client, &publish_view, NULL);
4809
4810 s_wait_for_mock_server_pubacks(&test_context);
4811
4812 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
4813
4814 aws_wait_for_stopped_lifecycle_event(&test_context);
4815
4816 ASSERT_SUCCESS(s_verify_mock_puback_order(&test_context));
4817
4818 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
4819 aws_mqtt_library_clean_up();
4820
4821 return AWS_OP_SUCCESS;
4822 }
4823
4824 AWS_TEST_CASE(mqtt5_client_puback_ordering, s_mqtt5_client_puback_ordering_fn)
4825
4826 enum aws_mqtt5_listener_test_publish_received_callback_type {
4827 AWS_MQTT5_LTPRCT_DEFAULT,
4828 AWS_MQTT5_LTPRCT_LISTENER,
4829 };
4830
4831 struct aws_mqtt5_listeners_test_context {
4832 struct aws_mqtt5_client_mock_test_fixture *test_fixture;
4833 struct aws_array_list publish_received_callback_types;
4834 struct aws_mutex lock;
4835 struct aws_condition_variable signal;
4836 };
4837
4838 static void s_aws_mqtt5_listeners_test_context_init(
4839 struct aws_mqtt5_listeners_test_context *listener_test_context,
4840 struct aws_allocator *allocator) {
4841 AWS_ZERO_STRUCT(*listener_test_context);
4842
4843 aws_array_list_init_dynamic(
4844 &listener_test_context->publish_received_callback_types,
4845 allocator,
4846 0,
4847 sizeof(enum aws_mqtt5_listener_test_publish_received_callback_type));
4848 aws_mutex_init(&listener_test_context->lock);
4849 aws_condition_variable_init(&listener_test_context->signal);
4850 }
4851
4852 static void s_aws_mqtt5_listeners_test_context_clean_up(
4853 struct aws_mqtt5_listeners_test_context *listener_test_context) {
4854 aws_condition_variable_clean_up(&listener_test_context->signal);
4855 aws_mutex_clean_up(&listener_test_context->lock);
4856 aws_array_list_clean_up(&listener_test_context->publish_received_callback_types);
4857 }
4858
4859 static int s_aws_mqtt5_mock_server_reflect_publish(
4860 void *packet,
4861 struct aws_mqtt5_server_mock_connection_context *connection,
4862 void *user_data) {
4863 (void)user_data;
4864
4865 struct aws_mqtt5_packet_publish_view *publish_view = packet;
4866 struct aws_mqtt5_packet_publish_view reflected_view = *publish_view;
4867
4868 if (reflected_view.qos != AWS_MQTT5_QOS_AT_MOST_ONCE) {
4869 reflected_view.packet_id = 1;
4870 }
4871
4872 if (aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_PUBLISH, &reflected_view)) {
4873 return AWS_OP_ERR;
4874 }
4875
4876 return AWS_OP_SUCCESS;
4877 }
4878
4879 static void s_aws_mqtt5_listeners_test_publish_received_default_handler(
4880 const struct aws_mqtt5_packet_publish_view *publish_view,
4881 void *user_data) {
4882 (void)publish_view;
4883
4884 struct aws_mqtt5_listeners_test_context *context = user_data;
4885
4886 aws_mutex_lock(&context->lock);
4887 enum aws_mqtt5_listener_test_publish_received_callback_type callback_type = AWS_MQTT5_LTPRCT_DEFAULT;
4888 aws_array_list_push_back(&context->publish_received_callback_types, &callback_type);
4889 aws_mutex_unlock(&context->lock);
4890 aws_condition_variable_notify_all(&context->signal);
4891 }
4892
4893 static bool s_aws_mqtt5_listeners_test_publish_received_listener_handler(
4894 const struct aws_mqtt5_packet_publish_view *publish_view,
4895 void *user_data) {
4896 struct aws_mqtt5_listeners_test_context *context = user_data;
4897
4898 aws_mutex_lock(&context->lock);
4899 enum aws_mqtt5_listener_test_publish_received_callback_type callback_type = AWS_MQTT5_LTPRCT_LISTENER;
4900 aws_array_list_push_back(&context->publish_received_callback_types, &callback_type);
4901 aws_mutex_unlock(&context->lock);
4902 aws_condition_variable_notify_all(&context->signal);
4903
4904 return publish_view->qos == AWS_MQTT5_QOS_AT_LEAST_ONCE;
4905 }
4906
4907 struct aws_mqtt5_listeners_test_wait_context {
4908 size_t callback_count;
4909 struct aws_mqtt5_listeners_test_context *test_fixture;
4910 };
4911
4912 static bool s_aws_mqtt5_listeners_test_wait_on_callback_count(void *context) {
4913 struct aws_mqtt5_listeners_test_wait_context *wait_context = context;
4914 return wait_context->callback_count ==
4915 aws_array_list_length(&wait_context->test_fixture->publish_received_callback_types);
4916 }
4917
4918 static int s_aws_mqtt5_listeners_test_wait_on_and_verify_callbacks(
4919 struct aws_mqtt5_listeners_test_context *context,
4920 size_t callback_count,
4921 enum aws_mqtt5_listener_test_publish_received_callback_type *callback_types) {
4922 struct aws_mqtt5_listeners_test_wait_context wait_context = {
4923 .callback_count = callback_count,
4924 .test_fixture = context,
4925 };
4926
4927 aws_mutex_lock(&context->lock);
4928 aws_condition_variable_wait_pred(
4929 &context->signal, &context->lock, s_aws_mqtt5_listeners_test_wait_on_callback_count, &wait_context);
4930 for (size_t i = 0; i < callback_count; ++i) {
4931 enum aws_mqtt5_listener_test_publish_received_callback_type callback_type;
4932 aws_array_list_get_at(&context->publish_received_callback_types, &callback_type, i);
4933
4934 ASSERT_INT_EQUALS(callback_types[i], callback_type);
4935 }
4936 aws_mutex_unlock(&context->lock);
4937
4938 return AWS_OP_SUCCESS;
4939 }
4940
4941 static int s_mqtt5_client_listeners_fn(struct aws_allocator *allocator, void *ctx) {
4942 (void)ctx;
4943
4944 aws_mqtt_library_init(allocator);
4945
4946 struct mqtt5_client_test_options test_options;
4947 aws_mqtt5_client_test_init_default_options(&test_options);
4948
4949 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] = s_aws_mqtt5_mock_server_reflect_publish;
4950
4951 struct aws_mqtt5_client_mock_test_fixture test_context;
4952 struct aws_mqtt5_listeners_test_context full_test_context = {
4953 .test_fixture = &test_context,
4954 };
4955 s_aws_mqtt5_listeners_test_context_init(&full_test_context, allocator);
4956
4957 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
4958 .client_options = &test_options.client_options,
4959 .server_function_table = &test_options.server_function_table,
4960 .mock_server_user_data = &full_test_context,
4961 };
4962
4963 test_fixture_options.client_options->publish_received_handler =
4964 s_aws_mqtt5_listeners_test_publish_received_default_handler;
4965 test_fixture_options.client_options->publish_received_handler_user_data = &full_test_context;
4966
4967 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
4968
4969 struct aws_mqtt5_client *client = test_context.client;
4970 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
4971
4972 aws_wait_for_connected_lifecycle_event(&test_context);
4973
4974 struct aws_mqtt5_packet_publish_view qos0_publish_view = {
4975 .qos = AWS_MQTT5_QOS_AT_MOST_ONCE,
4976 .topic =
4977 {
4978 .ptr = s_sub_pub_unsub_publish_topic,
4979 .len = AWS_ARRAY_SIZE(s_sub_pub_unsub_publish_topic) - 1,
4980 },
4981 };
4982
4983 struct aws_mqtt5_packet_publish_view qos1_publish_view = {
4984 .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE,
4985 .topic =
4986 {
4987 .ptr = s_sub_pub_unsub_publish_topic,
4988 .len = AWS_ARRAY_SIZE(s_sub_pub_unsub_publish_topic) - 1,
4989 },
4990 };
4991
4992 // send a qos 0 publish, wait for it to reflect, verify it's the default handler
4993 aws_mqtt5_client_publish(client, &qos0_publish_view, NULL);
4994
4995 enum aws_mqtt5_listener_test_publish_received_callback_type first_callback_type_array[] = {
4996 AWS_MQTT5_LTPRCT_DEFAULT,
4997 };
4998 ASSERT_SUCCESS(s_aws_mqtt5_listeners_test_wait_on_and_verify_callbacks(
4999 &full_test_context, AWS_ARRAY_SIZE(first_callback_type_array), first_callback_type_array));
5000
5001 // attach a listener at the beginning of the handler chain
5002 struct aws_mqtt5_listener_config listener_config = {
5003 .client = client,
5004 .listener_callbacks = {
5005 .listener_publish_received_handler_user_data = &full_test_context,
5006 .listener_publish_received_handler = s_aws_mqtt5_listeners_test_publish_received_listener_handler,
5007 }};
5008 struct aws_mqtt5_listener *listener = aws_mqtt5_listener_new(allocator, &listener_config);
5009
5010 // send a qos 0 publish, wait for it to reflect, verify both handlers were invoked in the proper order
5011 aws_mqtt5_client_publish(client, &qos0_publish_view, NULL);
5012
5013 enum aws_mqtt5_listener_test_publish_received_callback_type second_callback_type_array[] = {
5014 AWS_MQTT5_LTPRCT_DEFAULT,
5015 AWS_MQTT5_LTPRCT_LISTENER,
5016 AWS_MQTT5_LTPRCT_DEFAULT,
5017 };
5018 ASSERT_SUCCESS(s_aws_mqtt5_listeners_test_wait_on_and_verify_callbacks(
5019 &full_test_context, AWS_ARRAY_SIZE(second_callback_type_array), second_callback_type_array));
5020
5021 // send a qos1 publish (which is short-circuited by the listener), verify just the listener was notified
5022 aws_mqtt5_client_publish(client, &qos1_publish_view, NULL);
5023
5024 enum aws_mqtt5_listener_test_publish_received_callback_type third_callback_type_array[] = {
5025 AWS_MQTT5_LTPRCT_DEFAULT,
5026 AWS_MQTT5_LTPRCT_LISTENER,
5027 AWS_MQTT5_LTPRCT_DEFAULT,
5028 AWS_MQTT5_LTPRCT_LISTENER,
5029 };
5030 ASSERT_SUCCESS(s_aws_mqtt5_listeners_test_wait_on_and_verify_callbacks(
5031 &full_test_context, AWS_ARRAY_SIZE(third_callback_type_array), third_callback_type_array));
5032
5033 // remove the listener
5034 aws_mqtt5_listener_release(listener);
5035
5036 // send a qos1 publish, wait for it to reflect, verify it's the default handler
5037 aws_mqtt5_client_publish(client, &qos1_publish_view, NULL);
5038
5039 enum aws_mqtt5_listener_test_publish_received_callback_type fourth_callback_type_array[] = {
5040 AWS_MQTT5_LTPRCT_DEFAULT,
5041 AWS_MQTT5_LTPRCT_LISTENER,
5042 AWS_MQTT5_LTPRCT_DEFAULT,
5043 AWS_MQTT5_LTPRCT_LISTENER,
5044 AWS_MQTT5_LTPRCT_DEFAULT,
5045 };
5046 ASSERT_SUCCESS(s_aws_mqtt5_listeners_test_wait_on_and_verify_callbacks(
5047 &full_test_context, AWS_ARRAY_SIZE(fourth_callback_type_array), fourth_callback_type_array));
5048
5049 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
5050
5051 aws_wait_for_stopped_lifecycle_event(&test_context);
5052
5053 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
5054 s_aws_mqtt5_listeners_test_context_clean_up(&full_test_context);
5055 aws_mqtt_library_clean_up();
5056
5057 return AWS_OP_SUCCESS;
5058 }
5059
5060 AWS_TEST_CASE(mqtt5_client_listeners, s_mqtt5_client_listeners_fn)
5061
5062 static void s_on_offline_publish_completion(
5063 enum aws_mqtt5_packet_type packet_type,
5064 const void *packet,
5065 int error_code,
5066 void *user_data) {
5067 (void)packet_type;
5068 (void)packet;
5069
5070 struct aws_mqtt5_sub_pub_unsub_context *full_test_context = user_data;
5071
5072 aws_mutex_lock(&full_test_context->test_fixture->lock);
5073
5074 if (error_code == AWS_ERROR_MQTT5_OPERATION_FAILED_DUE_TO_OFFLINE_QUEUE_POLICY) {
5075 ++full_test_context->publish_failures;
5076 } else if (error_code == 0) {
5077 full_test_context->publish_complete = true;
5078 }
5079
5080 aws_mutex_unlock(&full_test_context->test_fixture->lock);
5081 aws_condition_variable_notify_all(&full_test_context->test_fixture->signal);
5082 }
5083
5084 static bool s_has_failed_publishes(void *user_data) {
5085 struct aws_mqtt5_sub_pub_unsub_context *full_test_context = user_data;
5086
5087 return full_test_context->publish_failures > 0;
5088 }
5089
5090 static void s_aws_mqtt5_wait_for_publish_failure(struct aws_mqtt5_sub_pub_unsub_context *full_test_context) {
5091 aws_mutex_lock(&full_test_context->test_fixture->lock);
5092 aws_condition_variable_wait_pred(
5093 &full_test_context->test_fixture->signal,
5094 &full_test_context->test_fixture->lock,
5095 s_has_failed_publishes,
5096 full_test_context);
5097 aws_mutex_unlock(&full_test_context->test_fixture->lock);
5098 }
5099
5100 static int s_offline_publish(
5101 struct aws_mqtt5_client *client,
5102 struct aws_mqtt5_sub_pub_unsub_context *full_test_context,
5103 enum aws_mqtt5_qos qos) {
5104 struct aws_mqtt5_packet_publish_view publish_view = {
5105 .qos = qos,
5106 .topic =
5107 {
5108 .ptr = s_sub_pub_unsub_publish_topic,
5109 .len = AWS_ARRAY_SIZE(s_sub_pub_unsub_publish_topic) - 1,
5110 },
5111 .payload =
5112 {
5113 .ptr = s_sub_pub_unsub_publish_payload,
5114 .len = AWS_ARRAY_SIZE(s_sub_pub_unsub_publish_payload) - 1,
5115 },
5116 };
5117
5118 struct aws_mqtt5_publish_completion_options completion_options = {
5119 .completion_callback = s_on_offline_publish_completion,
5120 .completion_user_data = full_test_context,
5121 };
5122
5123 return aws_mqtt5_client_publish(client, &publish_view, &completion_options);
5124 }
5125
5126 static int s_verify_offline_publish_failure(
5127 struct aws_mqtt5_client *client,
5128 struct aws_mqtt5_sub_pub_unsub_context *full_test_context,
5129 enum aws_mqtt5_qos qos) {
5130
5131 aws_mutex_lock(&full_test_context->test_fixture->lock);
5132 full_test_context->publish_failures = 0;
5133 aws_mutex_unlock(&full_test_context->test_fixture->lock);
5134
5135 ASSERT_SUCCESS(s_offline_publish(client, full_test_context, qos));
5136
5137 s_aws_mqtt5_wait_for_publish_failure(full_test_context);
5138
5139 return AWS_OP_SUCCESS;
5140 }
5141
5142 /* There's no signalable event for internal queue changes so we have to spin-poll this in a dumb manner */
5143 static void s_aws_mqtt5_wait_for_offline_queue_size(
5144 struct aws_mqtt5_sub_pub_unsub_context *full_test_context,
5145 size_t expected_queue_size) {
5146 bool done = false;
5147 struct aws_mqtt5_client *client = full_test_context->test_fixture->client;
5148 while (!done) {
5149 aws_thread_current_sleep(aws_timestamp_convert(100, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL));
5150
5151 struct aws_mqtt5_client_operation_statistics stats;
5152 AWS_ZERO_STRUCT(stats);
5153
5154 aws_mqtt5_client_get_stats(client, &stats);
5155
5156 done = stats.incomplete_operation_count == expected_queue_size;
5157 }
5158 }
5159
5160 static void s_on_offline_subscribe_completion(
5161 const struct aws_mqtt5_packet_suback_view *suback_view,
5162 int error_code,
5163 void *user_data) {
5164 (void)suback_view;
5165
5166 struct aws_mqtt5_sub_pub_unsub_context *full_test_context = user_data;
5167
5168 aws_mutex_lock(&full_test_context->test_fixture->lock);
5169 if (error_code == AWS_ERROR_MQTT5_OPERATION_FAILED_DUE_TO_OFFLINE_QUEUE_POLICY) {
5170 ++full_test_context->subscribe_failures;
5171 }
5172 aws_mutex_unlock(&full_test_context->test_fixture->lock);
5173 aws_condition_variable_notify_all(&full_test_context->test_fixture->signal);
5174 }
5175
5176 static bool s_has_failed_subscribes(void *user_data) {
5177 struct aws_mqtt5_sub_pub_unsub_context *full_test_context = user_data;
5178
5179 return full_test_context->subscribe_failures > 0;
5180 }
5181
5182 static void s_aws_mqtt5_wait_for_subscribe_failure(struct aws_mqtt5_sub_pub_unsub_context *full_test_context) {
5183 aws_mutex_lock(&full_test_context->test_fixture->lock);
5184 aws_condition_variable_wait_pred(
5185 &full_test_context->test_fixture->signal,
5186 &full_test_context->test_fixture->lock,
5187 s_has_failed_subscribes,
5188 full_test_context);
5189 aws_mutex_unlock(&full_test_context->test_fixture->lock);
5190 }
5191
5192 static int s_offline_subscribe(
5193 struct aws_mqtt5_client *client,
5194 struct aws_mqtt5_sub_pub_unsub_context *full_test_context) {
5195 struct aws_mqtt5_packet_subscribe_view subscribe_view = {
5196 .subscriptions = s_subscriptions,
5197 .subscription_count = AWS_ARRAY_SIZE(s_subscriptions),
5198 };
5199
5200 struct aws_mqtt5_subscribe_completion_options completion_options = {
5201 .completion_callback = s_on_offline_subscribe_completion,
5202 .completion_user_data = full_test_context,
5203 };
5204
5205 return aws_mqtt5_client_subscribe(client, &subscribe_view, &completion_options);
5206 }
5207
5208 static int s_verify_offline_subscribe_failure(
5209 struct aws_mqtt5_client *client,
5210 struct aws_mqtt5_sub_pub_unsub_context *full_test_context) {
5211 aws_mutex_lock(&full_test_context->test_fixture->lock);
5212 full_test_context->subscribe_failures = 0;
5213 aws_mutex_unlock(&full_test_context->test_fixture->lock);
5214
5215 ASSERT_SUCCESS(s_offline_subscribe(client, full_test_context));
5216
5217 s_aws_mqtt5_wait_for_subscribe_failure(full_test_context);
5218
5219 return AWS_OP_SUCCESS;
5220 }
5221
5222 static void s_on_offline_unsubscribe_completion(
5223 const struct aws_mqtt5_packet_unsuback_view *unsuback_view,
5224 int error_code,
5225 void *user_data) {
5226 (void)unsuback_view;
5227
5228 struct aws_mqtt5_sub_pub_unsub_context *full_test_context = user_data;
5229
5230 aws_mutex_lock(&full_test_context->test_fixture->lock);
5231 if (error_code == AWS_ERROR_MQTT5_OPERATION_FAILED_DUE_TO_OFFLINE_QUEUE_POLICY) {
5232 ++full_test_context->unsubscribe_failures;
5233 }
5234 aws_mutex_unlock(&full_test_context->test_fixture->lock);
5235 aws_condition_variable_notify_all(&full_test_context->test_fixture->signal);
5236 }
5237
5238 static bool s_has_failed_unsubscribes(void *user_data) {
5239 struct aws_mqtt5_sub_pub_unsub_context *full_test_context = user_data;
5240
5241 return full_test_context->unsubscribe_failures > 0;
5242 }
5243
5244 static void s_aws_mqtt5_wait_for_unsubscribe_failure(struct aws_mqtt5_sub_pub_unsub_context *full_test_context) {
5245 aws_mutex_lock(&full_test_context->test_fixture->lock);
5246 aws_condition_variable_wait_pred(
5247 &full_test_context->test_fixture->signal,
5248 &full_test_context->test_fixture->lock,
5249 s_has_failed_unsubscribes,
5250 full_test_context);
5251 aws_mutex_unlock(&full_test_context->test_fixture->lock);
5252 }
5253
5254 static int s_offline_unsubscribe(
5255 struct aws_mqtt5_client *client,
5256 struct aws_mqtt5_sub_pub_unsub_context *full_test_context) {
5257 struct aws_mqtt5_packet_unsubscribe_view unsubscribe_view = {
5258 .topic_filters = s_sub_pub_unsub_topic_filters,
5259 .topic_filter_count = AWS_ARRAY_SIZE(s_sub_pub_unsub_topic_filters),
5260 };
5261
5262 struct aws_mqtt5_unsubscribe_completion_options completion_options = {
5263 .completion_callback = s_on_offline_unsubscribe_completion,
5264 .completion_user_data = full_test_context,
5265 };
5266
5267 return aws_mqtt5_client_unsubscribe(client, &unsubscribe_view, &completion_options);
5268 }
5269
5270 static int s_verify_offline_unsubscribe_failure(
5271 struct aws_mqtt5_client *client,
5272 struct aws_mqtt5_sub_pub_unsub_context *full_test_context) {
5273 aws_mutex_lock(&full_test_context->test_fixture->lock);
5274 full_test_context->unsubscribe_failures = 0;
5275 aws_mutex_unlock(&full_test_context->test_fixture->lock);
5276
5277 ASSERT_SUCCESS(s_offline_unsubscribe(client, full_test_context));
5278
5279 s_aws_mqtt5_wait_for_unsubscribe_failure(full_test_context);
5280
5281 return AWS_OP_SUCCESS;
5282 }
5283
5284 static int s_mqtt5_client_offline_operation_submission_fail_all_fn(struct aws_allocator *allocator, void *ctx) {
5285 (void)ctx;
5286
5287 aws_mqtt_library_init(allocator);
5288
5289 struct mqtt5_client_test_options test_options;
5290 aws_mqtt5_client_test_init_default_options(&test_options);
5291
5292 test_options.client_options.offline_queue_behavior = AWS_MQTT5_COQBT_FAIL_ALL_ON_DISCONNECT;
5293
5294 struct aws_mqtt5_client_mock_test_fixture test_context;
5295 struct aws_mqtt5_sub_pub_unsub_context full_test_context = {
5296 .test_fixture = &test_context,
5297 };
5298
5299 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
5300 .client_options = &test_options.client_options,
5301 .server_function_table = &test_options.server_function_table,
5302 .mock_server_user_data = &full_test_context,
5303 };
5304
5305 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
5306
5307 struct aws_mqtt5_client *client = test_context.client;
5308
5309 /* everything should fail on submission */
5310 ASSERT_SUCCESS(s_verify_offline_publish_failure(client, &full_test_context, AWS_MQTT5_QOS_AT_MOST_ONCE));
5311 ASSERT_SUCCESS(s_verify_offline_publish_failure(client, &full_test_context, AWS_MQTT5_QOS_AT_LEAST_ONCE));
5312 ASSERT_SUCCESS(s_verify_offline_subscribe_failure(client, &full_test_context));
5313 ASSERT_SUCCESS(s_verify_offline_unsubscribe_failure(client, &full_test_context));
5314
5315 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
5316 aws_mqtt_library_clean_up();
5317
5318 return AWS_OP_SUCCESS;
5319 }
5320
5321 AWS_TEST_CASE(
5322 mqtt5_client_offline_operation_submission_fail_all,
5323 s_mqtt5_client_offline_operation_submission_fail_all_fn)
5324
5325 static int s_mqtt5_client_offline_operation_submission_fail_qos0_fn(struct aws_allocator *allocator, void *ctx) {
5326 (void)ctx;
5327
5328 aws_mqtt_library_init(allocator);
5329
5330 struct mqtt5_client_test_options test_options;
5331 aws_mqtt5_client_test_init_default_options(&test_options);
5332
5333 test_options.client_options.offline_queue_behavior = AWS_MQTT5_COQBT_FAIL_QOS0_PUBLISH_ON_DISCONNECT;
5334
5335 struct aws_mqtt5_client_mock_test_fixture test_context;
5336 struct aws_mqtt5_sub_pub_unsub_context full_test_context = {
5337 .test_fixture = &test_context,
5338 };
5339
5340 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
5341 .client_options = &test_options.client_options,
5342 .server_function_table = &test_options.server_function_table,
5343 .mock_server_user_data = &full_test_context,
5344 };
5345
5346 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
5347
5348 struct aws_mqtt5_client *client = test_context.client;
5349
5350 /* qos 0 publish should fail on submission */
5351 ASSERT_SUCCESS(s_verify_offline_publish_failure(client, &full_test_context, AWS_MQTT5_QOS_AT_MOST_ONCE));
5352
5353 /* qos 1 publish, subscribe, and unsubscribe should queue on submission */
5354 ASSERT_SUCCESS(s_offline_publish(client, &full_test_context, AWS_MQTT5_QOS_AT_LEAST_ONCE));
5355 s_aws_mqtt5_wait_for_offline_queue_size(&full_test_context, 1);
5356 ASSERT_SUCCESS(s_offline_subscribe(client, &full_test_context));
5357 s_aws_mqtt5_wait_for_offline_queue_size(&full_test_context, 2);
5358 ASSERT_SUCCESS(s_offline_unsubscribe(client, &full_test_context));
5359 s_aws_mqtt5_wait_for_offline_queue_size(&full_test_context, 3);
5360
5361 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
5362 aws_mqtt_library_clean_up();
5363
5364 return AWS_OP_SUCCESS;
5365 }
5366
5367 AWS_TEST_CASE(
5368 mqtt5_client_offline_operation_submission_fail_qos0,
5369 s_mqtt5_client_offline_operation_submission_fail_qos0_fn)
5370
5371 static int s_mqtt5_client_offline_operation_submission_fail_non_qos1_fn(struct aws_allocator *allocator, void *ctx) {
5372 (void)ctx;
5373
5374 aws_mqtt_library_init(allocator);
5375
5376 struct mqtt5_client_test_options test_options;
5377 aws_mqtt5_client_test_init_default_options(&test_options);
5378
5379 test_options.client_options.offline_queue_behavior = AWS_MQTT5_COQBT_FAIL_NON_QOS1_PUBLISH_ON_DISCONNECT;
5380
5381 struct aws_mqtt5_client_mock_test_fixture test_context;
5382 struct aws_mqtt5_sub_pub_unsub_context full_test_context = {
5383 .test_fixture = &test_context,
5384 };
5385
5386 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
5387 .client_options = &test_options.client_options,
5388 .server_function_table = &test_options.server_function_table,
5389 .mock_server_user_data = &full_test_context,
5390 };
5391
5392 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
5393
5394 struct aws_mqtt5_client *client = test_context.client;
5395
5396 /* qos0 publish, subscribe, and unsubscribe should fail on submission */
5397 ASSERT_SUCCESS(s_verify_offline_publish_failure(client, &full_test_context, AWS_MQTT5_QOS_AT_MOST_ONCE));
5398 ASSERT_SUCCESS(s_verify_offline_subscribe_failure(client, &full_test_context));
5399 ASSERT_SUCCESS(s_verify_offline_unsubscribe_failure(client, &full_test_context));
5400
5401 /* qos1 publish should queue on submission */
5402 ASSERT_SUCCESS(s_offline_publish(client, &full_test_context, AWS_MQTT5_QOS_AT_LEAST_ONCE));
5403 s_aws_mqtt5_wait_for_offline_queue_size(&full_test_context, 1);
5404
5405 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
5406 aws_mqtt_library_clean_up();
5407
5408 return AWS_OP_SUCCESS;
5409 }
5410
5411 AWS_TEST_CASE(
5412 mqtt5_client_offline_operation_submission_fail_non_qos1,
5413 s_mqtt5_client_offline_operation_submission_fail_non_qos1_fn)
5414
5415 static int s_mqtt5_client_offline_operation_submission_then_connect_fn(struct aws_allocator *allocator, void *ctx) {
5416 (void)ctx;
5417
5418 aws_mqtt_library_init(allocator);
5419
5420 struct mqtt5_client_test_options test_options;
5421 aws_mqtt5_client_test_init_default_options(&test_options);
5422
5423 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] =
5424 aws_mqtt5_mock_server_handle_publish_puback_and_forward;
5425
5426 test_options.client_options.offline_queue_behavior = AWS_MQTT5_COQBT_FAIL_NON_QOS1_PUBLISH_ON_DISCONNECT;
5427
5428 struct aws_mqtt5_client_mock_test_fixture test_context;
5429 struct aws_mqtt5_sub_pub_unsub_context full_test_context = {
5430 .test_fixture = &test_context,
5431 };
5432
5433 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
5434 .client_options = &test_options.client_options,
5435 .server_function_table = &test_options.server_function_table,
5436 .mock_server_user_data = &full_test_context,
5437 };
5438
5439 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
5440
5441 struct aws_mqtt5_client *client = test_context.client;
5442
5443 /* qos1 publish should queue on submission */
5444 ASSERT_SUCCESS(s_offline_publish(client, &full_test_context, AWS_MQTT5_QOS_AT_LEAST_ONCE));
5445 s_aws_mqtt5_wait_for_offline_queue_size(&full_test_context, 1);
5446
5447 /* start the client, it should connect and immediately send the publish */
5448 aws_mqtt5_client_start(client);
5449
5450 s_sub_pub_unsub_wait_for_publish_complete(&full_test_context);
5451
5452 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
5453
5454 aws_wait_for_stopped_lifecycle_event(&test_context);
5455 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
5456 aws_mqtt_library_clean_up();
5457
5458 return AWS_OP_SUCCESS;
5459 }
5460
5461 AWS_TEST_CASE(
5462 mqtt5_client_offline_operation_submission_then_connect,
5463 s_mqtt5_client_offline_operation_submission_then_connect_fn)
5464
5465 #define ALIASED_PUBLISH_SEQUENCE_COUNT 4
5466
5467 struct aws_mqtt5_aliased_publish_sequence_context {
5468 struct aws_mqtt5_client_mock_test_fixture *test_fixture;
5469 struct aws_allocator *allocator;
5470
5471 struct aws_array_list publishes_received;
5472 };
5473
5474 static int s_aws_mqtt5_aliased_publish_sequence_context_init(
5475 struct aws_mqtt5_aliased_publish_sequence_context *context,
5476 struct aws_allocator *allocator) {
5477 AWS_ZERO_STRUCT(*context);
5478
5479 context->allocator = allocator;
5480 return aws_array_list_init_dynamic(
5481 &context->publishes_received, allocator, 10, sizeof(struct aws_mqtt5_packet_publish_storage *));
5482 }
5483
5484 static void s_aws_mqtt5_aliased_publish_sequence_context_clean_up(
5485 struct aws_mqtt5_aliased_publish_sequence_context *context) {
5486 for (size_t i = 0; i < aws_array_list_length(&context->publishes_received); ++i) {
5487 struct aws_mqtt5_packet_publish_storage *storage = NULL;
5488 aws_array_list_get_at(&context->publishes_received, &storage, i);
5489
5490 aws_mqtt5_packet_publish_storage_clean_up(storage);
5491 aws_mem_release(context->allocator, storage);
5492 }
5493 aws_array_list_clean_up(&context->publishes_received);
5494 }
5495
5496 void s_aliased_publish_received_fn(const struct aws_mqtt5_packet_publish_view *publish, void *complete_ctx) {
5497
5498 AWS_FATAL_ASSERT(publish != NULL);
5499
5500 struct aws_mqtt5_aliased_publish_sequence_context *full_test_context = complete_ctx;
5501 struct aws_mqtt5_client_mock_test_fixture *test_fixture = full_test_context->test_fixture;
5502
5503 aws_mutex_lock(&test_fixture->lock);
5504
5505 struct aws_mqtt5_packet_publish_storage *storage =
5506 aws_mem_calloc(full_test_context->allocator, 1, sizeof(struct aws_mqtt5_packet_publish_storage));
5507 aws_mqtt5_packet_publish_storage_init(storage, full_test_context->allocator, publish);
5508
5509 aws_array_list_push_back(&full_test_context->publishes_received, &storage);
5510
5511 aws_mutex_unlock(&test_fixture->lock);
5512 aws_condition_variable_notify_all(&test_fixture->signal);
5513 }
5514
5515 static enum aws_mqtt5_suback_reason_code s_alias_reason_codes[] = {
5516 AWS_MQTT5_SARC_GRANTED_QOS_1,
5517 AWS_MQTT5_SARC_GRANTED_QOS_1,
5518 };
5519 static uint8_t s_alias_topic1[] = "alias/first/topic";
5520 static uint8_t s_alias_topic2[] = "alias/second/topic";
5521
5522 static int s_aws_mqtt5_server_send_aliased_publish_sequence(
5523 void *packet,
5524 struct aws_mqtt5_server_mock_connection_context *connection,
5525 void *user_data) {
5526 (void)user_data;
5527
5528 struct aws_mqtt5_packet_subscribe_view *subscribe_view = packet;
5529
5530 struct aws_mqtt5_packet_suback_view suback_view = {
5531 .packet_id = subscribe_view->packet_id,
5532 .reason_code_count = 1,
5533 .reason_codes = s_alias_reason_codes,
5534 };
5535
5536 // just to be thorough, send a suback
5537 if (aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_SUBACK, &suback_view)) {
5538 return AWS_OP_ERR;
5539 }
5540
5541 uint16_t alias_id = 1;
5542 struct aws_mqtt5_packet_publish_view publish_view = {
5543 .packet_id = 1,
5544 .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE,
5545 .topic =
5546 {
5547 .ptr = s_alias_topic1,
5548 .len = AWS_ARRAY_SIZE(s_alias_topic1) - 1,
5549 },
5550 .topic_alias = &alias_id,
5551 };
5552
5553 // establish an alias with id 1
5554 if (aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_PUBLISH, &publish_view)) {
5555 return AWS_OP_ERR;
5556 }
5557
5558 // alias alone
5559 AWS_ZERO_STRUCT(publish_view.topic);
5560 if (aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_PUBLISH, &publish_view)) {
5561 return AWS_OP_ERR;
5562 }
5563
5564 // establish a new alias with id 1
5565 publish_view.topic.ptr = s_alias_topic2;
5566 publish_view.topic.len = AWS_ARRAY_SIZE(s_alias_topic2) - 1;
5567
5568 if (aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_PUBLISH, &publish_view)) {
5569 return AWS_OP_ERR;
5570 }
5571
5572 // alias alone
5573 AWS_ZERO_STRUCT(publish_view.topic);
5574 if (aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_PUBLISH, &publish_view)) {
5575 return AWS_OP_ERR;
5576 }
5577
5578 return AWS_OP_SUCCESS;
5579 }
5580
5581 static bool s_client_received_aliased_publish_sequence(void *arg) {
5582 struct aws_mqtt5_aliased_publish_sequence_context *full_test_context = arg;
5583
5584 return aws_array_list_length(&full_test_context->publishes_received) == ALIASED_PUBLISH_SEQUENCE_COUNT;
5585 }
5586
5587 static void s_wait_for_aliased_publish_sequence(struct aws_mqtt5_aliased_publish_sequence_context *full_test_context) {
5588 struct aws_mqtt5_client_mock_test_fixture *test_fixture = full_test_context->test_fixture;
5589
5590 aws_mutex_lock(&test_fixture->lock);
5591 aws_condition_variable_wait_pred(
5592 &test_fixture->signal, &test_fixture->lock, s_client_received_aliased_publish_sequence, full_test_context);
5593 aws_mutex_unlock(&test_fixture->lock);
5594 }
5595
5596 static int s_verify_aliased_publish_sequence(struct aws_mqtt5_aliased_publish_sequence_context *full_test_context) {
5597
5598 struct aws_mqtt5_client_mock_test_fixture *test_fixture = full_test_context->test_fixture;
5599 aws_mutex_lock(&test_fixture->lock);
5600
5601 for (size_t i = 0; i < aws_array_list_length(&full_test_context->publishes_received); ++i) {
5602 struct aws_mqtt5_packet_publish_storage *publish_storage = NULL;
5603 aws_array_list_get_at(&full_test_context->publishes_received, &publish_storage, i);
5604
5605 struct aws_byte_cursor topic_cursor = publish_storage->storage_view.topic;
5606
5607 if (i < 2) {
5608 // the first two publishes should be the first topic
5609 ASSERT_BIN_ARRAYS_EQUALS(
5610 s_alias_topic1, AWS_ARRAY_SIZE(s_alias_topic1) - 1, topic_cursor.ptr, topic_cursor.len);
5611 } else {
5612 // the last two publishes should be the second topic
5613 ASSERT_BIN_ARRAYS_EQUALS(
5614 s_alias_topic2, AWS_ARRAY_SIZE(s_alias_topic2) - 1, topic_cursor.ptr, topic_cursor.len);
5615 }
5616 }
5617
5618 aws_mutex_unlock(&test_fixture->lock);
5619
5620 return AWS_OP_SUCCESS;
5621 }
5622
5623 static struct aws_mqtt5_subscription_view s_alias_subscriptions[] = {
5624 {
5625 .topic_filter =
5626 {
5627 .ptr = (uint8_t *)s_alias_topic1,
5628 .len = AWS_ARRAY_SIZE(s_alias_topic1) - 1,
5629 },
5630 .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE,
5631 },
5632 {
5633 .topic_filter =
5634 {
5635 .ptr = (uint8_t *)s_alias_topic2,
5636 .len = AWS_ARRAY_SIZE(s_alias_topic2) - 1,
5637 },
5638 .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE,
5639 },
5640 };
5641
5642 static int s_mqtt5_client_inbound_alias_success_fn(struct aws_allocator *allocator, void *ctx) {
5643 (void)ctx;
5644
5645 aws_mqtt_library_init(allocator);
5646
5647 struct mqtt5_client_test_options test_options;
5648 aws_mqtt5_client_test_init_default_options(&test_options);
5649
5650 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] =
5651 s_aws_mqtt5_server_send_aliased_publish_sequence;
5652
5653 struct aws_mqtt5_client_mock_test_fixture test_context;
5654
5655 struct aws_mqtt5_aliased_publish_sequence_context full_test_context;
5656 ASSERT_SUCCESS(s_aws_mqtt5_aliased_publish_sequence_context_init(&full_test_context, allocator));
5657 full_test_context.test_fixture = &test_context;
5658
5659 struct aws_mqtt5_client_topic_alias_options aliasing_config = {
5660 .inbound_alias_cache_size = 10,
5661 .inbound_topic_alias_behavior = AWS_MQTT5_CITABT_ENABLED,
5662 };
5663
5664 test_options.client_options.topic_aliasing_options = &aliasing_config;
5665 test_options.client_options.publish_received_handler = s_aliased_publish_received_fn;
5666 test_options.client_options.publish_received_handler_user_data = &full_test_context;
5667
5668 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
5669 .client_options = &test_options.client_options,
5670 .server_function_table = &test_options.server_function_table,
5671 .mock_server_user_data = &full_test_context,
5672 };
5673
5674 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
5675
5676 struct aws_mqtt5_client *client = test_context.client;
5677 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
5678
5679 aws_wait_for_connected_lifecycle_event(&test_context);
5680
5681 struct aws_mqtt5_packet_subscribe_view subscribe = {
5682 .subscriptions = s_alias_subscriptions,
5683 .subscription_count = AWS_ARRAY_SIZE(s_alias_subscriptions),
5684 };
5685
5686 ASSERT_SUCCESS(aws_mqtt5_client_subscribe(client, &subscribe, NULL));
5687
5688 s_wait_for_aliased_publish_sequence(&full_test_context);
5689
5690 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
5691
5692 aws_wait_for_stopped_lifecycle_event(&test_context);
5693
5694 ASSERT_SUCCESS(s_verify_aliased_publish_sequence(&full_test_context));
5695
5696 s_aws_mqtt5_aliased_publish_sequence_context_clean_up(&full_test_context);
5697
5698 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
5699 aws_mqtt_library_clean_up();
5700
5701 return AWS_OP_SUCCESS;
5702 }
5703
5704 AWS_TEST_CASE(mqtt5_client_inbound_alias_success, s_mqtt5_client_inbound_alias_success_fn)
5705
5706 enum aws_mqtt5_test_inbound_alias_failure_type {
5707 AWS_MTIAFT_DISABLED,
5708 AWS_MTIAFT_ZERO_ID,
5709 AWS_MTIAFT_TOO_LARGE_ID,
5710 AWS_MTIAFT_UNBOUND_ID
5711 };
5712
5713 struct aws_mqtt5_test_inbound_alias_failure_context {
5714 struct aws_mqtt5_client_mock_test_fixture *test_fixture;
5715
5716 enum aws_mqtt5_test_inbound_alias_failure_type failure_type;
5717 };
5718
5719 static int s_aws_mqtt5_server_send_aliased_publish_failure(
5720 void *packet,
5721 struct aws_mqtt5_server_mock_connection_context *connection,
5722 void *user_data) {
5723
5724 struct aws_mqtt5_test_inbound_alias_failure_context *test_context = user_data;
5725
5726 struct aws_mqtt5_packet_subscribe_view *subscribe_view = packet;
5727
5728 struct aws_mqtt5_packet_suback_view suback_view = {
5729 .packet_id = subscribe_view->packet_id,
5730 .reason_code_count = 1,
5731 .reason_codes = s_alias_reason_codes,
5732 };
5733
5734 // just to be thorough, send a suback
5735 if (aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_SUBACK, &suback_view)) {
5736 return AWS_OP_ERR;
5737 }
5738
5739 uint16_t alias_id = 0;
5740 struct aws_byte_cursor topic_cursor = {
5741 .ptr = s_alias_topic1,
5742 .len = AWS_ARRAY_SIZE(s_alias_topic1) - 1,
5743 };
5744
5745 switch (test_context->failure_type) {
5746 case AWS_MTIAFT_TOO_LARGE_ID:
5747 alias_id = 100;
5748 break;
5749
5750 case AWS_MTIAFT_UNBOUND_ID:
5751 AWS_ZERO_STRUCT(topic_cursor);
5752 alias_id = 1;
5753 break;
5754
5755 default:
5756 break;
5757 }
5758
5759 struct aws_mqtt5_packet_publish_view publish_view = {
5760 .packet_id = 1, .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE, .topic = topic_cursor, .topic_alias = &alias_id};
5761
5762 // establish an alias with id 1
5763 if (aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_PUBLISH, &publish_view)) {
5764 return AWS_OP_ERR;
5765 }
5766
5767 return AWS_OP_SUCCESS;
5768 }
5769
5770 static bool s_has_decoding_error_disconnect_event(void *arg) {
5771 struct aws_mqtt5_test_inbound_alias_failure_context *test_context = arg;
5772 struct aws_mqtt5_client_mock_test_fixture *test_fixture = test_context->test_fixture;
5773
5774 size_t record_count = aws_array_list_length(&test_fixture->lifecycle_events);
5775 for (size_t i = 0; i < record_count; ++i) {
5776 struct aws_mqtt5_lifecycle_event_record *record = NULL;
5777 aws_array_list_get_at(&test_fixture->lifecycle_events, &record, i);
5778 if (record->event.event_type == AWS_MQTT5_CLET_DISCONNECTION) {
5779 if (record->event.error_code == AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR) {
5780 return true;
5781 }
5782 }
5783 }
5784
5785 return false;
5786 }
5787
5788 static void s_wait_for_decoding_error_disconnect(struct aws_mqtt5_test_inbound_alias_failure_context *test_context) {
5789 struct aws_mqtt5_client_mock_test_fixture *test_fixture = test_context->test_fixture;
5790
5791 aws_mutex_lock(&test_fixture->lock);
5792 aws_condition_variable_wait_pred(
5793 &test_fixture->signal, &test_fixture->lock, s_has_decoding_error_disconnect_event, test_context);
5794 aws_mutex_unlock(&test_fixture->lock);
5795 }
5796
5797 static int s_do_inbound_alias_failure_test(
5798 struct aws_allocator *allocator,
5799 enum aws_mqtt5_test_inbound_alias_failure_type test_failure_type) {
5800 aws_mqtt_library_init(allocator);
5801
5802 struct mqtt5_client_test_options test_options;
5803 aws_mqtt5_client_test_init_default_options(&test_options);
5804
5805 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] =
5806 s_aws_mqtt5_server_send_aliased_publish_failure;
5807
5808 struct aws_mqtt5_client_mock_test_fixture test_context;
5809
5810 struct aws_mqtt5_test_inbound_alias_failure_context full_test_context = {
5811 .test_fixture = &test_context,
5812 .failure_type = test_failure_type,
5813 };
5814
5815 struct aws_mqtt5_client_topic_alias_options aliasing_config = {
5816 .inbound_alias_cache_size = 10,
5817 .inbound_topic_alias_behavior =
5818 (test_failure_type == AWS_MTIAFT_DISABLED) ? AWS_MQTT5_CITABT_DISABLED : AWS_MQTT5_CITABT_ENABLED,
5819 };
5820
5821 test_options.client_options.topic_aliasing_options = &aliasing_config;
5822
5823 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
5824 .client_options = &test_options.client_options,
5825 .server_function_table = &test_options.server_function_table,
5826 .mock_server_user_data = &full_test_context,
5827 };
5828
5829 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
5830
5831 struct aws_mqtt5_client *client = test_context.client;
5832 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
5833
5834 aws_wait_for_connected_lifecycle_event(&test_context);
5835
5836 struct aws_mqtt5_packet_subscribe_view subscribe = {
5837 .subscriptions = s_alias_subscriptions,
5838 .subscription_count = AWS_ARRAY_SIZE(s_alias_subscriptions),
5839 };
5840
5841 ASSERT_SUCCESS(aws_mqtt5_client_subscribe(client, &subscribe, NULL));
5842
5843 s_wait_for_decoding_error_disconnect(&full_test_context);
5844
5845 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
5846
5847 aws_wait_for_stopped_lifecycle_event(&test_context);
5848
5849 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
5850 aws_mqtt_library_clean_up();
5851
5852 return AWS_OP_SUCCESS;
5853 }
5854
5855 static int s_mqtt5_client_inbound_alias_failure_disabled_fn(struct aws_allocator *allocator, void *ctx) {
5856 (void)ctx;
5857
5858 ASSERT_SUCCESS(s_do_inbound_alias_failure_test(allocator, AWS_MTIAFT_DISABLED));
5859
5860 return AWS_OP_SUCCESS;
5861 }
5862
5863 AWS_TEST_CASE(mqtt5_client_inbound_alias_failure_disabled, s_mqtt5_client_inbound_alias_failure_disabled_fn)
5864
5865 static int s_mqtt5_client_inbound_alias_failure_zero_id_fn(struct aws_allocator *allocator, void *ctx) {
5866 (void)ctx;
5867
5868 ASSERT_SUCCESS(s_do_inbound_alias_failure_test(allocator, AWS_MTIAFT_ZERO_ID));
5869
5870 return AWS_OP_SUCCESS;
5871 }
5872
5873 AWS_TEST_CASE(mqtt5_client_inbound_alias_failure_zero_id, s_mqtt5_client_inbound_alias_failure_zero_id_fn)
5874
5875 static int s_mqtt5_client_inbound_alias_failure_too_large_id_fn(struct aws_allocator *allocator, void *ctx) {
5876 (void)ctx;
5877
5878 ASSERT_SUCCESS(s_do_inbound_alias_failure_test(allocator, AWS_MTIAFT_TOO_LARGE_ID));
5879
5880 return AWS_OP_SUCCESS;
5881 }
5882
5883 AWS_TEST_CASE(mqtt5_client_inbound_alias_failure_too_large_id, s_mqtt5_client_inbound_alias_failure_too_large_id_fn)
5884
5885 static int s_mqtt5_client_inbound_alias_failure_unbound_id_fn(struct aws_allocator *allocator, void *ctx) {
5886 (void)ctx;
5887
5888 ASSERT_SUCCESS(s_do_inbound_alias_failure_test(allocator, AWS_MTIAFT_UNBOUND_ID));
5889
5890 return AWS_OP_SUCCESS;
5891 }
5892
5893 AWS_TEST_CASE(mqtt5_client_inbound_alias_failure_unbound_id, s_mqtt5_client_inbound_alias_failure_unbound_id_fn)
5894
5895 void s_outbound_alias_failure_publish_complete_fn(
5896 enum aws_mqtt5_packet_type packet_type,
5897 const void *packet,
5898 int error_code,
5899 void *complete_ctx) {
5900
5901 (void)packet_type;
5902 (void)packet;
5903 AWS_FATAL_ASSERT(error_code != AWS_ERROR_SUCCESS);
5904
5905 struct aws_mqtt5_sub_pub_unsub_context *test_context = complete_ctx;
5906 struct aws_mqtt5_client_mock_test_fixture *test_fixture = test_context->test_fixture;
5907
5908 aws_mutex_lock(&test_fixture->lock);
5909 test_context->publish_failures++;
5910 aws_mutex_unlock(&test_fixture->lock);
5911 aws_condition_variable_notify_all(&test_fixture->signal);
5912 }
5913
5914 #define SEQUENCE_TEST_CACHE_SIZE 2
5915
5916 static int s_aws_mqtt5_mock_server_handle_connect_allow_aliasing(
5917 void *packet,
5918 struct aws_mqtt5_server_mock_connection_context *connection,
5919 void *user_data) {
5920 (void)packet;
5921 (void)user_data;
5922
5923 struct aws_mqtt5_packet_connack_view connack_view;
5924 AWS_ZERO_STRUCT(connack_view);
5925
5926 uint16_t topic_alias_maximum = SEQUENCE_TEST_CACHE_SIZE;
5927 connack_view.topic_alias_maximum = &topic_alias_maximum;
5928
5929 return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_CONNACK, &connack_view);
5930 }
5931
5932 static int s_do_mqtt5_client_outbound_alias_failure_test(
5933 struct aws_allocator *allocator,
5934 enum aws_mqtt5_client_outbound_topic_alias_behavior_type behavior_type) {
5935
5936 aws_mqtt_library_init(allocator);
5937
5938 struct mqtt5_client_test_options test_options;
5939 aws_mqtt5_client_test_init_default_options(&test_options);
5940
5941 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] =
5942 s_aws_mqtt5_mock_server_handle_connect_allow_aliasing;
5943
5944 test_options.topic_aliasing_options.outbound_topic_alias_behavior = behavior_type;
5945
5946 struct aws_mqtt5_client_mock_test_fixture test_context;
5947
5948 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
5949 .client_options = &test_options.client_options,
5950 .server_function_table = &test_options.server_function_table,
5951 };
5952
5953 struct aws_mqtt5_sub_pub_unsub_context full_test_context = {
5954 .test_fixture = &test_context,
5955 };
5956 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
5957
5958 struct aws_mqtt5_client *client = test_context.client;
5959 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
5960
5961 aws_wait_for_connected_lifecycle_event(&test_context);
5962
5963 uint16_t topic_alias = 1;
5964 struct aws_mqtt5_packet_publish_view packet_publish_view = {
5965 .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE,
5966 .topic =
5967 {
5968 .ptr = s_topic,
5969 .len = AWS_ARRAY_SIZE(s_topic) - 1,
5970 },
5971 .topic_alias = &topic_alias,
5972 };
5973
5974 if (behavior_type == AWS_MQTT5_COTABT_MANUAL) {
5975 AWS_ZERO_STRUCT(packet_publish_view.topic);
5976 }
5977
5978 struct aws_mqtt5_publish_completion_options completion_options = {
5979 .completion_callback = s_outbound_alias_failure_publish_complete_fn,
5980 .completion_user_data = &full_test_context,
5981 };
5982
5983 /* should result in an immediate validation failure or a subsequent dynamic validation failure */
5984 if (aws_mqtt5_client_publish(client, &packet_publish_view, &completion_options) == AWS_OP_SUCCESS) {
5985 s_aws_mqtt5_wait_for_publish_failure(&full_test_context);
5986 }
5987
5988 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
5989
5990 aws_wait_for_stopped_lifecycle_event(&test_context);
5991
5992 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
5993 aws_mqtt_library_clean_up();
5994
5995 return AWS_OP_SUCCESS;
5996 }
5997
5998 static int s_mqtt5_client_outbound_alias_manual_failure_empty_topic_fn(struct aws_allocator *allocator, void *ctx) {
5999 (void)ctx;
6000
6001 ASSERT_SUCCESS(s_do_mqtt5_client_outbound_alias_failure_test(allocator, AWS_MQTT5_COTABT_MANUAL));
6002
6003 return AWS_OP_SUCCESS;
6004 }
6005
6006 AWS_TEST_CASE(
6007 mqtt5_client_outbound_alias_manual_failure_empty_topic,
6008 s_mqtt5_client_outbound_alias_manual_failure_empty_topic_fn)
6009
6010 struct outbound_alias_publish {
6011 struct aws_byte_cursor topic;
6012 uint16_t topic_alias;
6013
6014 size_t expected_alias_id;
6015 bool expected_reuse;
6016 };
6017
6018 #define DEFINE_OUTBOUND_ALIAS_PUBLISH(topic_suffix, desired_alias, expected_alias_index, reused) \
6019 { \
6020 .topic = aws_byte_cursor_from_string(s_topic_##topic_suffix), \
6021 .topic_alias = desired_alias, \
6022 .expected_alias_id = expected_alias_index, \
6023 .expected_reuse = reused, \
6024 }
6025
6026 static void s_outbound_alias_publish_completion_fn(
6027 enum aws_mqtt5_packet_type packet_type,
6028 const void *packet,
6029 int error_code,
6030 void *complete_ctx) {
6031
6032 AWS_FATAL_ASSERT(packet_type == AWS_MQTT5_PT_PUBACK);
6033 AWS_FATAL_ASSERT(error_code == AWS_ERROR_SUCCESS);
6034
6035 const struct aws_mqtt5_packet_puback_view *puback = packet;
6036 struct aws_mqtt5_client_mock_test_fixture *test_context = complete_ctx;
6037
6038 aws_mutex_lock(&test_context->lock);
6039
6040 ++test_context->total_pubacks_received;
6041 if (error_code == AWS_ERROR_SUCCESS && puback->reason_code < 128) {
6042 ++test_context->successful_pubacks_received;
6043 }
6044
6045 aws_mutex_unlock(&test_context->lock);
6046 aws_condition_variable_notify_all(&test_context->signal);
6047 }
6048
6049 static int s_perform_outbound_alias_publish(
6050 struct aws_mqtt5_client_mock_test_fixture *test_fixture,
6051 struct outbound_alias_publish *publish) {
6052
6053 struct aws_mqtt5_client *client = test_fixture->client;
6054
6055 uint16_t alias_id = publish->topic_alias;
6056 struct aws_mqtt5_packet_publish_view publish_view = {
6057 .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE,
6058 .topic = publish->topic,
6059 };
6060
6061 if (alias_id != 0) {
6062 publish_view.topic_alias = &alias_id;
6063 }
6064
6065 struct aws_mqtt5_publish_completion_options completion_options = {
6066 .completion_callback = s_outbound_alias_publish_completion_fn,
6067 .completion_user_data = test_fixture,
6068 };
6069
6070 ASSERT_SUCCESS(aws_mqtt5_client_publish(client, &publish_view, &completion_options));
6071
6072 return AWS_OP_SUCCESS;
6073 }
6074
6075 static int s_perform_outbound_alias_sequence(
6076 struct aws_mqtt5_client_mock_test_fixture *test_fixture,
6077 struct outbound_alias_publish *publishes,
6078 size_t publish_count) {
6079
6080 for (size_t i = 0; i < publish_count; ++i) {
6081 struct outbound_alias_publish *publish = &publishes[i];
6082 ASSERT_SUCCESS(s_perform_outbound_alias_publish(test_fixture, publish));
6083 }
6084
6085 return AWS_OP_SUCCESS;
6086 }
6087
6088 static int s_perform_outbound_alias_sequence_test(
6089 struct aws_allocator *allocator,
6090 enum aws_mqtt5_client_outbound_topic_alias_behavior_type behavior_type,
6091 struct outbound_alias_publish *publishes,
6092 size_t publish_count) {
6093
6094 aws_mqtt_library_init(allocator);
6095
6096 struct mqtt5_client_test_options test_options;
6097 aws_mqtt5_client_test_init_default_options(&test_options);
6098
6099 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] =
6100 s_aws_mqtt5_mock_server_handle_connect_allow_aliasing;
6101 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] =
6102 aws_mqtt5_mock_server_handle_publish_puback;
6103
6104 test_options.topic_aliasing_options.outbound_topic_alias_behavior = behavior_type;
6105
6106 struct aws_mqtt5_client_mock_test_fixture test_context;
6107
6108 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
6109 .client_options = &test_options.client_options,
6110 .server_function_table = &test_options.server_function_table,
6111 };
6112
6113 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
6114 test_context.maximum_inbound_topic_aliases = SEQUENCE_TEST_CACHE_SIZE;
6115
6116 struct aws_mqtt5_client *client = test_context.client;
6117 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
6118
6119 aws_wait_for_connected_lifecycle_event(&test_context);
6120
6121 ASSERT_SUCCESS(s_perform_outbound_alias_sequence(&test_context, publishes, publish_count));
6122
6123 struct aws_mqtt5_client_test_wait_for_n_context wait_context = {
6124 .test_fixture = &test_context,
6125 .required_event_count = publish_count,
6126 };
6127 s_wait_for_n_successful_publishes(&wait_context);
6128
6129 aws_mutex_lock(&test_context.lock);
6130 size_t packet_count = aws_array_list_length(&test_context.server_received_packets);
6131 ASSERT_INT_EQUALS(1 + publish_count, packet_count); // N publishes, 1 connect
6132
6133 /* start at 1 and skip the connect */
6134 for (size_t i = 1; i < packet_count; ++i) {
6135 struct aws_mqtt5_mock_server_packet_record *packet = NULL;
6136 aws_array_list_get_at_ptr(&test_context.server_received_packets, (void **)&packet, i);
6137
6138 ASSERT_INT_EQUALS(AWS_MQTT5_PT_PUBLISH, packet->packet_type);
6139 struct aws_mqtt5_packet_publish_storage *publish_storage = packet->packet_storage;
6140 struct aws_mqtt5_packet_publish_view *publish_view = &publish_storage->storage_view;
6141
6142 struct outbound_alias_publish *publish = &publishes[i - 1];
6143 ASSERT_NOT_NULL(publish_view->topic_alias);
6144 ASSERT_INT_EQUALS(publish->expected_alias_id, *publish_view->topic_alias);
6145
6146 /*
6147 * Unfortunately, the decoder fails unless it has an inbound resolver and the inbound resolver will always
6148 * resolve the topics first. So we can't actually check that an empty topic was sent. It would be nice to
6149 * harden this up in the future.
6150 */
6151 ASSERT_BIN_ARRAYS_EQUALS(
6152 publish->topic.ptr, publish->topic.len, publish_view->topic.ptr, publish_view->topic.len);
6153 }
6154
6155 aws_mutex_unlock(&test_context.lock);
6156
6157 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
6158
6159 aws_wait_for_stopped_lifecycle_event(&test_context);
6160
6161 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
6162 aws_mqtt_library_clean_up();
6163
6164 return AWS_OP_SUCCESS;
6165 }
6166
6167 AWS_STATIC_STRING_FROM_LITERAL(s_topic_a, "topic/a");
6168 AWS_STATIC_STRING_FROM_LITERAL(s_topic_b, "b/topic");
6169 AWS_STATIC_STRING_FROM_LITERAL(s_topic_c, "topic/c");
6170
6171 static int s_mqtt5_client_outbound_alias_manual_success_a_b_ar_br_fn(struct aws_allocator *allocator, void *ctx) {
6172 (void)ctx;
6173
6174 struct outbound_alias_publish test_publishes[] = {
6175 DEFINE_OUTBOUND_ALIAS_PUBLISH(a, 1, 1, false),
6176 DEFINE_OUTBOUND_ALIAS_PUBLISH(b, 2, 2, false),
6177 DEFINE_OUTBOUND_ALIAS_PUBLISH(a, 1, 1, true),
6178 DEFINE_OUTBOUND_ALIAS_PUBLISH(b, 2, 2, true),
6179 };
6180
6181 ASSERT_SUCCESS(s_perform_outbound_alias_sequence_test(
6182 allocator, AWS_MQTT5_COTABT_MANUAL, test_publishes, AWS_ARRAY_SIZE(test_publishes)));
6183
6184 return AWS_OP_SUCCESS;
6185 }
6186
6187 AWS_TEST_CASE(
6188 mqtt5_client_outbound_alias_manual_success_a_b_ar_br,
6189 s_mqtt5_client_outbound_alias_manual_success_a_b_ar_br_fn)
6190
6191 static int s_mqtt5_client_outbound_alias_lru_success_a_b_c_br_cr_a_fn(struct aws_allocator *allocator, void *ctx) {
6192 (void)ctx;
6193
6194 struct outbound_alias_publish test_publishes[] = {
6195 DEFINE_OUTBOUND_ALIAS_PUBLISH(a, 0, 1, false),
6196 DEFINE_OUTBOUND_ALIAS_PUBLISH(b, 0, 2, false),
6197 DEFINE_OUTBOUND_ALIAS_PUBLISH(c, 0, 1, false),
6198 DEFINE_OUTBOUND_ALIAS_PUBLISH(b, 0, 2, true),
6199 DEFINE_OUTBOUND_ALIAS_PUBLISH(c, 0, 1, true),
6200 DEFINE_OUTBOUND_ALIAS_PUBLISH(a, 0, 2, false),
6201 };
6202
6203 ASSERT_SUCCESS(s_perform_outbound_alias_sequence_test(
6204 allocator, AWS_MQTT5_COTABT_LRU, test_publishes, AWS_ARRAY_SIZE(test_publishes)));
6205
6206 return AWS_OP_SUCCESS;
6207 }
6208
6209 AWS_TEST_CASE(
6210 mqtt5_client_outbound_alias_lru_success_a_b_c_br_cr_a,
6211 s_mqtt5_client_outbound_alias_lru_success_a_b_c_br_cr_a_fn)
6212
6213 struct mqtt5_operation_timeout_completion_callback {
6214 enum aws_mqtt5_packet_type type;
6215 uint64_t timepoint_ns;
6216 int error_code;
6217 };
6218
6219 struct mqtt5_dynamic_operation_timeout_test_context {
6220 struct aws_allocator *allocator;
6221 struct aws_mqtt5_client_mock_test_fixture *fixture;
6222 struct aws_array_list completion_callbacks;
6223
6224 const struct mqtt5_operation_timeout_completion_callback *expected_callbacks;
6225 size_t expected_callback_count;
6226 };
6227
6228 static void s_mqtt5_dynamic_operation_timeout_test_context_init(
6229 struct mqtt5_dynamic_operation_timeout_test_context *context,
6230 struct aws_allocator *allocator,
6231 struct aws_mqtt5_client_mock_test_fixture *fixture) {
6232 AWS_ZERO_STRUCT(*context);
6233
6234 context->allocator = allocator;
6235 context->fixture = fixture;
6236 aws_array_list_init_dynamic(
6237 &context->completion_callbacks, allocator, 5, sizeof(struct mqtt5_operation_timeout_completion_callback));
6238 }
6239
6240 static void s_mqtt5_dynamic_operation_timeout_test_context_cleanup(
6241 struct mqtt5_dynamic_operation_timeout_test_context *context) {
6242 aws_array_list_clean_up(&context->completion_callbacks);
6243 }
6244
6245 static bool s_mqtt5_dynamic_operation_timeout_test_context_callback_sequence_equals_expected(
6246 struct mqtt5_dynamic_operation_timeout_test_context *context) {
6247
6248 if (context->expected_callback_count != aws_array_list_length(&context->completion_callbacks)) {
6249 return false;
6250 }
6251
6252 for (size_t i = 0; i < context->expected_callback_count; ++i) {
6253 const struct mqtt5_operation_timeout_completion_callback *expected_callback = &context->expected_callbacks[i];
6254 struct mqtt5_operation_timeout_completion_callback *callback = NULL;
6255 aws_array_list_get_at_ptr(&context->completion_callbacks, (void **)&callback, i);
6256
6257 if (callback->type != expected_callback->type) {
6258 return false;
6259 }
6260
6261 if (callback->error_code != expected_callback->error_code) {
6262 return false;
6263 }
6264 }
6265
6266 return true;
6267 }
6268
6269 static void s_add_completion_callback(
6270 struct mqtt5_dynamic_operation_timeout_test_context *context,
6271 enum aws_mqtt5_packet_type type,
6272 int error_code) {
6273 aws_mutex_lock(&context->fixture->lock);
6274
6275 struct mqtt5_operation_timeout_completion_callback callback_entry = {
6276 .type = type, .error_code = error_code, .timepoint_ns = 0};
6277
6278 aws_high_res_clock_get_ticks(&callback_entry.timepoint_ns);
6279
6280 aws_array_list_push_back(&context->completion_callbacks, &callback_entry);
6281
6282 aws_mutex_unlock(&context->fixture->lock);
6283
6284 aws_condition_variable_notify_all(&context->fixture->signal);
6285 }
6286
6287 static void s_timeout_test_publish_completion_fn(
6288 enum aws_mqtt5_packet_type packet_type,
6289 const void *packet,
6290 int error_code,
6291 void *complete_ctx) {
6292 (void)packet_type;
6293 (void)packet;
6294
6295 s_add_completion_callback(complete_ctx, AWS_MQTT5_PT_PUBLISH, error_code);
6296 }
6297
6298 static void s_timeout_test_subscribe_completion_fn(
6299 const struct aws_mqtt5_packet_suback_view *suback,
6300 int error_code,
6301 void *complete_ctx) {
6302 (void)suback;
6303
6304 s_add_completion_callback(complete_ctx, AWS_MQTT5_PT_SUBSCRIBE, error_code);
6305 }
6306
6307 static void s_timeout_test_unsubscribe_completion_fn(
6308 const struct aws_mqtt5_packet_unsuback_view *unsuback,
6309 int error_code,
6310 void *complete_ctx) {
6311 (void)unsuback;
6312
6313 s_add_completion_callback(complete_ctx, AWS_MQTT5_PT_UNSUBSCRIBE, error_code);
6314 }
6315
6316 static bool s_all_timeout_operations_complete(void *arg) {
6317 struct mqtt5_dynamic_operation_timeout_test_context *context = arg;
6318
6319 return aws_array_list_length(&context->completion_callbacks) == context->expected_callback_count;
6320 }
6321
6322 static void s_wait_for_all_operation_timeouts(struct mqtt5_dynamic_operation_timeout_test_context *context) {
6323 aws_mutex_lock(&context->fixture->lock);
6324 aws_condition_variable_wait_pred(
6325 &context->fixture->signal, &context->fixture->lock, s_all_timeout_operations_complete, context);
6326 aws_mutex_unlock(&context->fixture->lock);
6327 }
6328
6329 /*
6330 * Tests a mixture of qos 0 publish, qos 1 publish, subscribe, and unsubscribe with override ack timeouts.
6331 *
6332 * qos 1 publish with 3 second timeout
6333 * subscribe with 2 second timeout
6334 * qos 0 publish
6335 * unsubscribe with 4 second timeout
6336 * qos 1 publish with 1 second timeout
6337 *
6338 * We expect to see callbacks in sequence:
6339 *
6340 * qos 0 publish success
6341 * qos 1 publish failure by timeout after 1 second
6342 * subscribe failure by timeout after 2 seconds
6343 * qos 1 publish failure by timeout after 3 seconds
6344 * unsubscribe failure by timeout after 4 seconds
6345 */
6346 static int s_mqtt5_client_dynamic_operation_timeout_fn(struct aws_allocator *allocator, void *ctx) {
6347 (void)ctx;
6348
6349 aws_mqtt_library_init(allocator);
6350
6351 struct mqtt5_client_test_options test_options;
6352 aws_mqtt5_client_test_init_default_options(&test_options);
6353
6354 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] =
6355 s_aws_mqtt5_mock_server_handle_timeout_publish;
6356
6357 struct aws_mqtt5_client_mock_test_fixture test_context;
6358
6359 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
6360 .client_options = &test_options.client_options,
6361 .server_function_table = &test_options.server_function_table,
6362 };
6363
6364 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
6365
6366 struct mqtt5_dynamic_operation_timeout_test_context context;
6367 s_mqtt5_dynamic_operation_timeout_test_context_init(&context, allocator, &test_context);
6368
6369 struct mqtt5_operation_timeout_completion_callback expected_callbacks[] = {
6370 {
6371 .type = AWS_MQTT5_PT_PUBLISH,
6372 .error_code = AWS_ERROR_SUCCESS,
6373 },
6374 {
6375 .type = AWS_MQTT5_PT_PUBLISH,
6376 .error_code = AWS_ERROR_MQTT_TIMEOUT,
6377 },
6378 {
6379 .type = AWS_MQTT5_PT_SUBSCRIBE,
6380 .error_code = AWS_ERROR_MQTT_TIMEOUT,
6381 },
6382 {
6383 .type = AWS_MQTT5_PT_PUBLISH,
6384 .error_code = AWS_ERROR_MQTT_TIMEOUT,
6385 },
6386 {
6387 .type = AWS_MQTT5_PT_UNSUBSCRIBE,
6388 .error_code = AWS_ERROR_MQTT_TIMEOUT,
6389 },
6390 };
6391
6392 context.expected_callbacks = expected_callbacks;
6393 context.expected_callback_count = AWS_ARRAY_SIZE(expected_callbacks);
6394
6395 struct aws_mqtt5_client *client = test_context.client;
6396 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
6397
6398 aws_wait_for_connected_lifecycle_event(&test_context);
6399
6400 uint64_t operation_start = 0;
6401 aws_high_res_clock_get_ticks(&operation_start);
6402
6403 struct aws_byte_cursor topic = {
6404 .ptr = s_topic,
6405 .len = AWS_ARRAY_SIZE(s_topic) - 1,
6406 };
6407
6408 // qos 1 publish - 3 second timeout
6409 struct aws_mqtt5_packet_publish_view qos1_publish = {
6410 .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE,
6411 .topic = topic,
6412 };
6413
6414 struct aws_mqtt5_publish_completion_options qos1_publish_options = {
6415 .completion_callback = s_timeout_test_publish_completion_fn,
6416 .completion_user_data = &context,
6417 .ack_timeout_seconds_override = 3,
6418 };
6419
6420 ASSERT_SUCCESS(aws_mqtt5_client_publish(client, &qos1_publish, &qos1_publish_options));
6421
6422 // subscribe - 2 seconds timeout
6423 struct aws_mqtt5_subscription_view subscriptions[] = {{
6424 .topic_filter = topic,
6425 .qos = AWS_MQTT5_QOS_AT_MOST_ONCE,
6426 }};
6427
6428 struct aws_mqtt5_packet_subscribe_view subscribe_view = {
6429 .subscriptions = subscriptions,
6430 .subscription_count = AWS_ARRAY_SIZE(subscriptions),
6431 };
6432
6433 struct aws_mqtt5_subscribe_completion_options subscribe_options = {
6434 .completion_callback = s_timeout_test_subscribe_completion_fn,
6435 .completion_user_data = &context,
6436 .ack_timeout_seconds_override = 2,
6437 };
6438
6439 ASSERT_SUCCESS(aws_mqtt5_client_subscribe(client, &subscribe_view, &subscribe_options));
6440
6441 // qos 0 publish
6442 struct aws_mqtt5_packet_publish_view qos0_publish = {
6443 .qos = AWS_MQTT5_QOS_AT_MOST_ONCE,
6444 .topic = topic,
6445 };
6446
6447 struct aws_mqtt5_publish_completion_options qos0_publish_options = {
6448 .completion_callback = s_timeout_test_publish_completion_fn,
6449 .completion_user_data = &context,
6450 };
6451
6452 ASSERT_SUCCESS(aws_mqtt5_client_publish(client, &qos0_publish, &qos0_publish_options));
6453
6454 // unsubscribe - 4 second timeout
6455 struct aws_byte_cursor topic_filters[] = {
6456 topic,
6457 };
6458
6459 struct aws_mqtt5_packet_unsubscribe_view unsubscribe = {
6460 .topic_filters = topic_filters,
6461 .topic_filter_count = AWS_ARRAY_SIZE(topic_filters),
6462 };
6463
6464 struct aws_mqtt5_unsubscribe_completion_options unsubscribe_options = {
6465 .completion_callback = s_timeout_test_unsubscribe_completion_fn,
6466 .completion_user_data = &context,
6467 .ack_timeout_seconds_override = 4,
6468 };
6469
6470 ASSERT_SUCCESS(aws_mqtt5_client_unsubscribe(client, &unsubscribe, &unsubscribe_options));
6471
6472 // qos 1 publish - 1 second timeout
6473 qos1_publish_options.ack_timeout_seconds_override = 1;
6474
6475 ASSERT_SUCCESS(aws_mqtt5_client_publish(client, &qos1_publish, &qos1_publish_options));
6476
6477 s_wait_for_all_operation_timeouts(&context);
6478
6479 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
6480
6481 aws_wait_for_stopped_lifecycle_event(&test_context);
6482
6483 aws_mutex_lock(&test_context.lock);
6484 s_mqtt5_dynamic_operation_timeout_test_context_callback_sequence_equals_expected(&context);
6485 aws_mutex_unlock(&test_context.lock);
6486
6487 /*
6488 * Finally, do a minimum time elapsed check:
6489 * each operation after the first (the qos 0 publish which did not time out) should have a completion
6490 * timepoint N seconds or later after the start of the test (where N is the operation's index in the sequence)
6491 */
6492 for (size_t i = 1; i < context.expected_callback_count; ++i) {
6493 struct mqtt5_operation_timeout_completion_callback *callback = NULL;
6494 aws_array_list_get_at_ptr(&context.completion_callbacks, (void **)&callback, i);
6495
6496 uint64_t delta_ns = callback->timepoint_ns - operation_start;
6497 ASSERT_TRUE(delta_ns >= aws_timestamp_convert(i, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL));
6498 }
6499
6500 s_mqtt5_dynamic_operation_timeout_test_context_cleanup(&context);
6501 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
6502 aws_mqtt_library_clean_up();
6503
6504 return AWS_OP_SUCCESS;
6505 }
6506
6507 AWS_TEST_CASE(mqtt5_client_dynamic_operation_timeout, s_mqtt5_client_dynamic_operation_timeout_fn)
6508
6509 #define DYNAMIC_TIMEOUT_DEFAULT_SECONDS 2
6510
6511 /*
6512 * Checks that using a override operation timeout of zero results in using the client's default timeout
6513 */
6514 static int s_mqtt5_client_dynamic_operation_timeout_default_fn(struct aws_allocator *allocator, void *ctx) {
6515 (void)ctx;
6516
6517 aws_mqtt_library_init(allocator);
6518
6519 struct mqtt5_client_test_options test_options;
6520 aws_mqtt5_client_test_init_default_options(&test_options);
6521 test_options.client_options.ack_timeout_seconds = DYNAMIC_TIMEOUT_DEFAULT_SECONDS;
6522
6523 test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] =
6524 s_aws_mqtt5_mock_server_handle_timeout_publish;
6525
6526 struct aws_mqtt5_client_mock_test_fixture test_context;
6527
6528 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
6529 .client_options = &test_options.client_options,
6530 .server_function_table = &test_options.server_function_table,
6531 };
6532
6533 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
6534
6535 struct mqtt5_dynamic_operation_timeout_test_context context;
6536 s_mqtt5_dynamic_operation_timeout_test_context_init(&context, allocator, &test_context);
6537
6538 struct mqtt5_operation_timeout_completion_callback expected_callbacks[] = {
6539 {
6540 .type = AWS_MQTT5_PT_PUBLISH,
6541 .error_code = AWS_ERROR_MQTT_TIMEOUT,
6542 },
6543 };
6544
6545 context.expected_callbacks = expected_callbacks;
6546 context.expected_callback_count = AWS_ARRAY_SIZE(expected_callbacks);
6547
6548 struct aws_mqtt5_client *client = test_context.client;
6549 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
6550
6551 aws_wait_for_connected_lifecycle_event(&test_context);
6552
6553 uint64_t operation_start = 0;
6554 aws_high_res_clock_get_ticks(&operation_start);
6555
6556 struct aws_byte_cursor topic = {
6557 .ptr = s_topic,
6558 .len = AWS_ARRAY_SIZE(s_topic) - 1,
6559 };
6560
6561 struct aws_mqtt5_packet_publish_view qos1_publish = {
6562 .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE,
6563 .topic = topic,
6564 };
6565
6566 struct aws_mqtt5_publish_completion_options qos1_publish_options = {
6567 .completion_callback = s_timeout_test_publish_completion_fn,
6568 .completion_user_data = &context,
6569 .ack_timeout_seconds_override = 0,
6570 };
6571
6572 ASSERT_SUCCESS(aws_mqtt5_client_publish(client, &qos1_publish, &qos1_publish_options));
6573
6574 s_wait_for_all_operation_timeouts(&context);
6575
6576 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));
6577
6578 aws_wait_for_stopped_lifecycle_event(&test_context);
6579
6580 aws_mutex_lock(&test_context.lock);
6581 s_mqtt5_dynamic_operation_timeout_test_context_callback_sequence_equals_expected(&context);
6582 aws_mutex_unlock(&test_context.lock);
6583
6584 /*
6585 * Finally, do a minimum time elapsed check:
6586 */
6587 for (size_t i = 0; i < context.expected_callback_count; ++i) {
6588 struct mqtt5_operation_timeout_completion_callback *callback = NULL;
6589 aws_array_list_get_at_ptr(&context.completion_callbacks, (void **)&callback, i);
6590
6591 uint64_t delta_ns = callback->timepoint_ns - operation_start;
6592 ASSERT_TRUE(
6593 delta_ns >=
6594 aws_timestamp_convert(DYNAMIC_TIMEOUT_DEFAULT_SECONDS, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL));
6595 }
6596
6597 s_mqtt5_dynamic_operation_timeout_test_context_cleanup(&context);
6598 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
6599 aws_mqtt_library_clean_up();
6600
6601 return AWS_OP_SUCCESS;
6602 }
6603
6604 AWS_TEST_CASE(mqtt5_client_dynamic_operation_timeout_default, s_mqtt5_client_dynamic_operation_timeout_default_fn)
6605
6606 static int s_mqtt5_do_auto_assigned_client_id_test(struct aws_allocator *allocator, bool use_iot_core_validation) {
6607 aws_mqtt_library_init(allocator);
6608
6609 struct mqtt5_client_test_options test_options;
6610 aws_mqtt5_client_test_init_default_options(&test_options);
6611 AWS_ZERO_STRUCT(((struct aws_mqtt5_packet_connect_view *)test_options.client_options.connect_options)->client_id);
6612 if (use_iot_core_validation) {
6613 test_options.client_options.extended_validation_and_flow_control_options =
6614 AWS_MQTT5_EVAFCO_AWS_IOT_CORE_DEFAULTS;
6615 }
6616
6617 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
6618 .client_options = &test_options.client_options,
6619 .server_function_table = &test_options.server_function_table,
6620 };
6621
6622 struct aws_mqtt5_client_mock_test_fixture test_context;
6623 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
6624
6625 struct aws_mqtt5_client *client = test_context.client;
6626
6627 const struct aws_byte_cursor auto_assigned_client_id = client->config->connect->storage_view.client_id;
6628 if (use_iot_core_validation) {
6629 ASSERT_INT_EQUALS(35, auto_assigned_client_id.len); /* "gen" + uuid as string no dashes length */
6630 } else {
6631 ASSERT_INT_EQUALS(0, auto_assigned_client_id.len);
6632 }
6633
6634 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
6635 aws_mqtt_library_clean_up();
6636
6637 return AWS_OP_SUCCESS;
6638 }
6639
6640 static int s_mqtt5_client_auto_assigned_client_id_fn(struct aws_allocator *allocator, void *ctx) {
6641 (void)ctx;
6642
6643 return s_mqtt5_do_auto_assigned_client_id_test(allocator, false);
6644 }
6645
6646 AWS_TEST_CASE(mqtt5_client_auto_assigned_client_id, s_mqtt5_client_auto_assigned_client_id_fn)
6647
6648 static int s_mqtt5_client_auto_assigned_client_id_iot_core_fn(struct aws_allocator *allocator, void *ctx) {
6649 (void)ctx;
6650
6651 return s_mqtt5_do_auto_assigned_client_id_test(allocator, true);
6652 }
6653
6654 AWS_TEST_CASE(mqtt5_client_auto_assigned_client_id_iot_core, s_mqtt5_client_auto_assigned_client_id_iot_core_fn)
6655 /*
6656 * Test that verifies metrics are properly appended to username in CONNECT packet
6657 * Based on s_mqtt5_client_direct_connect_success_fn
6658 */
6659 static int s_mqtt5_client_metrics_in_username_fn(
6660 struct aws_allocator *allocator,
6661 struct aws_mqtt_iot_metrics *metrics,
6662 struct aws_byte_cursor *original_username,
6663 void *ctx) {
6664 (void)ctx;
6665
6666 aws_mqtt_library_init(allocator);
6667
6668 struct mqtt5_client_test_options test_options;
6669 aws_mqtt5_client_test_init_default_options(&test_options);
6670 test_options.client_options.metrics = metrics;
6671
6672 struct aws_mqtt5_packet_connect_view connect_view = {
6673 .keep_alive_interval_seconds = 30,
6674 .client_id = aws_byte_cursor_from_string(g_default_client_id),
6675 .clean_start = true,
6676 .username = original_username};
6677
6678 test_options.connect_options = connect_view;
6679
6680 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
6681 .client_options = &test_options.client_options,
6682 .server_function_table = &test_options.server_function_table,
6683 };
6684
6685 struct aws_mqtt5_client_mock_test_fixture test_context;
6686 ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));
6687
6688 struct aws_mqtt5_client *client = test_context.client;
6689 ASSERT_SUCCESS(aws_mqtt5_client_start(client));
6690
6691 aws_wait_for_connected_lifecycle_event(&test_context);
6692
6693 struct aws_mqtt5_packet_disconnect_view disconnect_options = {
6694 .reason_code = AWS_MQTT5_DRC_NORMAL_DISCONNECTION,
6695 };
6696
6697 struct aws_mqtt5_disconnect_completion_options completion_options = {
6698 .completion_callback = s_on_disconnect_completion,
6699 .completion_user_data = &test_context,
6700 };
6701
6702 ASSERT_SUCCESS(aws_mqtt5_client_stop(client, &disconnect_options, &completion_options));
6703
6704 aws_wait_for_stopped_lifecycle_event(&test_context);
6705 s_wait_for_disconnect_completion(&test_context);
6706 s_wait_for_mock_server_to_receive_disconnect_packet(&test_context);
6707
6708 struct aws_mqtt5_client_lifecycle_event expected_events[] = {
6709 {
6710 .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT,
6711 },
6712 {
6713 .event_type = AWS_MQTT5_CLET_CONNECTION_SUCCESS,
6714 },
6715 {
6716 .event_type = AWS_MQTT5_CLET_DISCONNECTION,
6717 .error_code = AWS_ERROR_MQTT5_USER_REQUESTED_STOP,
6718 },
6719 {
6720 .event_type = AWS_MQTT5_CLET_STOPPED,
6721 },
6722 };
6723 ASSERT_SUCCESS(
6724 s_verify_simple_lifecycle_event_sequence(&test_context, expected_events, AWS_ARRAY_SIZE(expected_events)));
6725
6726 enum aws_mqtt5_client_state expected_states[] = {
6727 AWS_MCS_CONNECTING,
6728 AWS_MCS_MQTT_CONNECT,
6729 AWS_MCS_CONNECTED,
6730 AWS_MCS_CLEAN_DISCONNECT,
6731 AWS_MCS_CHANNEL_SHUTDOWN,
6732 AWS_MCS_STOPPED,
6733 };
6734
6735 ASSERT_SUCCESS(aws_verify_client_state_sequence(&test_context, expected_states, AWS_ARRAY_SIZE(expected_states)));
6736
6737 struct aws_mqtt5_packet_connect_storage expected_connect_storage;
6738
6739 struct aws_mqtt5_client_options_storage *options_storage =
6740 aws_mqtt5_client_options_storage_new(allocator, &test_options.client_options);
6741 aws_mqtt5_packet_connect_storage_init(&expected_connect_storage, allocator, &connect_view, options_storage);
6742 aws_mqtt5_client_options_storage_destroy(options_storage);
6743
6744 struct aws_mqtt5_packet_disconnect_storage expected_disconnect_storage;
6745 ASSERT_SUCCESS(s_aws_mqtt5_client_test_init_default_disconnect_storage(&expected_disconnect_storage, allocator));
6746 expected_disconnect_storage.storage_view.reason_code = AWS_MQTT5_DRC_NORMAL_DISCONNECTION;
6747
6748 struct aws_mqtt5_mock_server_packet_record expected_packets[] = {
6749 {
6750 .packet_type = AWS_MQTT5_PT_CONNECT,
6751 .packet_storage = &expected_connect_storage,
6752 },
6753 {
6754 .packet_type = AWS_MQTT5_PT_DISCONNECT,
6755 .packet_storage = &expected_disconnect_storage,
6756 },
6757 };
6758 ASSERT_SUCCESS(
6759 aws_verify_received_packet_sequence(&test_context, expected_packets, AWS_ARRAY_SIZE(expected_packets)));
6760
6761 aws_mqtt5_packet_connect_storage_clean_up(&expected_connect_storage);
6762
6763 aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
6764 aws_mqtt_library_clean_up();
6765
6766 return AWS_OP_SUCCESS;
6767 }
6768
6769 static int s_test_mqtt5_client_set_metrics_valid(struct aws_allocator *allocator, void *ctx) {
6770
6771 struct aws_mqtt_metadata_entry metadata_entries[] = {
6772 {
6773 .key = aws_byte_cursor_from_c_str("lang"),
6774 .value = aws_byte_cursor_from_c_str("C"),
6775 },
6776 {
6777 .key = aws_byte_cursor_from_c_str("version"),
6778 .value = aws_byte_cursor_from_c_str("1.0.0"),
6779 },
6780 };
6781
6782 struct aws_mqtt_iot_metrics metrics = {
6783 .library_name = aws_byte_cursor_from_c_str("TestSDK/1.0"),
6784 .metadata_count = AWS_ARRAY_SIZE(metadata_entries),
6785 .metadata_entries = metadata_entries,
6786 };
6787
6788 struct aws_byte_cursor username = aws_byte_cursor_from_c_str("test_user");
6789
6790 return s_mqtt5_client_metrics_in_username_fn(allocator, &metrics, &username, ctx);
6791 }
6792
6793 AWS_TEST_CASE(mqtt5_client_set_metrics_valid, s_test_mqtt5_client_set_metrics_valid)
6794
6795 static int s_test_mqtt5_client_set_metrics_with_null_username(struct aws_allocator *allocator, void *ctx) {
6796
6797 struct aws_mqtt_metadata_entry metadata_entries[] = {
6798 {
6799 .key = aws_byte_cursor_from_c_str("lang"),
6800 .value = aws_byte_cursor_from_c_str("C"),
6801 },
6802 {
6803 .key = aws_byte_cursor_from_c_str("version"),
6804 .value = aws_byte_cursor_from_c_str("1.0.0"),
6805 },
6806 };
6807
6808 struct aws_mqtt_iot_metrics metrics = {
6809 .library_name = aws_byte_cursor_from_c_str("TestSDK/1.0"),
6810 .metadata_count = AWS_ARRAY_SIZE(metadata_entries),
6811 .metadata_entries = metadata_entries,
6812 };
6813
6814 return s_mqtt5_client_metrics_in_username_fn(allocator, &metrics, NULL, ctx);
6815 }
6816
6817 AWS_TEST_CASE(mqtt5_client_set_metrics_with_null_username, s_test_mqtt5_client_set_metrics_with_null_username)
6818
6819 static int s_test_mqtt5_client_set_metrics_null(struct aws_allocator *allocator, void *ctx) {
6820
6821 struct aws_byte_cursor username = aws_byte_cursor_from_c_str("test_user");
6822 return s_mqtt5_client_metrics_in_username_fn(allocator, NULL, &username, ctx);
6823 }
6824
6825 AWS_TEST_CASE(mqtt5_client_set_metrics_null, s_test_mqtt5_client_set_metrics_null)