Debian Codegraph @

crt/aws-c-mqtt/tests/request-response/request_response_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 <aws/common/clock.h>
7 #include <aws/common/json.h>
8 #include <aws/common/uuid.h>
9 #include <aws/mqtt/private/client_impl_shared.h>
10 #include <aws/mqtt/private/request-response/protocol_adapter.h>
11 #include <aws/mqtt/private/request-response/request_response_subscription_set.h>
12 #include <aws/mqtt/request-response/request_response_client.h>
13
14 #include <aws/testing/aws_test_harness.h>
15
16 #include "../v3/mqtt311_testing_utils.h"
17 #include "../v3/mqtt_mock_server_handler.h"
18 #include "../v5/mqtt5_testing_utils.h"
19
20 enum rr_test_client_protocol {
21 RRCP_MQTT311,
22 RRCP_MQTT5,
23 };
24
25 struct aws_rr_client_test_fixture {
26 struct aws_allocator *allocator;
27
28 struct aws_mqtt_request_response_client *rr_client;
29
30 enum rr_test_client_protocol test_protocol;
31 union {
32 struct aws_mqtt5_client_mock_test_fixture mqtt5_test_fixture;
33 struct mqtt_connection_state_test mqtt311_test_fixture;
34 } client_test_fixture;
35
36 void *test_context;
37
38 struct aws_mutex lock;
39 struct aws_condition_variable signal;
40
41 bool client_initialized;
42 bool client_destroyed;
43
44 struct aws_hash_table request_response_records;
45 struct aws_hash_table streaming_records;
46 };
47
48 struct aws_rr_client_fixture_request_response_record {
49 struct aws_allocator *allocator;
50
51 struct aws_rr_client_test_fixture *fixture;
52
53 struct aws_byte_cursor record_key_cursor;
54 struct aws_byte_buf record_key;
55
56 bool completed;
57 int error_code;
58 struct aws_byte_buf response;
59 struct aws_byte_buf response_topic;
60 };
61
62 struct aws_rr_client_fixture_request_response_record *s_aws_rr_client_fixture_request_response_record_new(
63 struct aws_allocator *allocator,
64 struct aws_rr_client_test_fixture *fixture,
65 struct aws_byte_cursor request_payload) {
66 struct aws_rr_client_fixture_request_response_record *record =
67 aws_mem_calloc(allocator, 1, sizeof(struct aws_rr_client_fixture_request_response_record));
68
69 record->allocator = allocator;
70 record->fixture = fixture;
71
72 aws_byte_buf_init_copy_from_cursor(&record->record_key, allocator, request_payload);
73 record->record_key_cursor = aws_byte_cursor_from_buf(&record->record_key);
74
75 return record;
76 }
77
78 void s_aws_rr_client_fixture_request_response_record_delete(
79 struct aws_rr_client_fixture_request_response_record *record) {
80 aws_byte_buf_clean_up(&record->record_key);
81 aws_byte_buf_clean_up(&record->response);
82 aws_byte_buf_clean_up(&record->response_topic);
83
84 aws_mem_release(record->allocator, record);
85 }
86
87 static void s_aws_rr_client_fixture_request_response_record_hash_destroy(void *element) {
88 struct aws_rr_client_fixture_request_response_record *record = element;
89
90 s_aws_rr_client_fixture_request_response_record_delete(record);
91 }
92
93 static void s_rrc_fixture_request_completion_callback(
94 const struct aws_mqtt_rr_incoming_publish_event *publish_event,
95 int error_code,
96 void *user_data) {
97 struct aws_rr_client_fixture_request_response_record *record = user_data;
98 struct aws_rr_client_test_fixture *fixture = record->fixture;
99
100 aws_mutex_lock(&fixture->lock);
101
102 if (error_code == AWS_ERROR_SUCCESS) {
103 AWS_FATAL_ASSERT(publish_event != NULL);
104
105 aws_byte_buf_init_copy_from_cursor(&record->response, fixture->allocator, publish_event->payload);
106 aws_byte_buf_init_copy_from_cursor(&record->response_topic, fixture->allocator, publish_event->topic);
107 } else {
108 AWS_FATAL_ASSERT(publish_event == NULL);
109 record->error_code = error_code;
110 }
111
112 record->completed = true;
113
114 aws_mutex_unlock(&fixture->lock);
115 aws_condition_variable_notify_all(&fixture->signal);
116 }
117
118 static struct aws_rr_client_fixture_request_response_record *s_rrc_fixture_add_request_record(
119 struct aws_rr_client_test_fixture *fixture,
120 struct aws_byte_cursor record_key) {
121 struct aws_rr_client_fixture_request_response_record *record =
122 s_aws_rr_client_fixture_request_response_record_new(fixture->allocator, fixture, record_key);
123
124 aws_hash_table_put(&fixture->request_response_records, &record->record_key_cursor, record, NULL);
125
126 return record;
127 }
128
129 struct rrc_operation_completion_context {
130 struct aws_byte_cursor key;
131 struct aws_rr_client_test_fixture *fixture;
132 };
133
134 static bool s_is_request_complete(void *context) {
135 struct rrc_operation_completion_context *completion_context = context;
136
137 struct aws_hash_element *element = NULL;
138 aws_hash_table_find(&completion_context->fixture->request_response_records, &completion_context->key, &element);
139
140 AWS_FATAL_ASSERT(element != NULL && element->value != NULL);
141
142 struct aws_rr_client_fixture_request_response_record *record = element->value;
143
144 return record->completed;
145 }
146
147 static void s_rrc_wait_on_request_completion(
148 struct aws_rr_client_test_fixture *fixture,
149 struct aws_byte_cursor record_key) {
150 struct rrc_operation_completion_context context = {
151 .key = record_key,
152 .fixture = fixture,
153 };
154
155 aws_mutex_lock(&fixture->lock);
156 aws_condition_variable_wait_pred(&fixture->signal, &fixture->lock, s_is_request_complete, &context);
157 aws_mutex_unlock(&fixture->lock);
158 }
159
160 static int s_rrc_verify_request_completion(
161 struct aws_rr_client_test_fixture *fixture,
162 struct aws_byte_cursor record_key,
163 int expected_error_code,
164 struct aws_byte_cursor *expected_response_topic,
165 struct aws_byte_cursor *expected_response) {
166 aws_mutex_lock(&fixture->lock);
167
168 struct aws_hash_element *element = NULL;
169 aws_hash_table_find(&fixture->request_response_records, &record_key, &element);
170
171 AWS_FATAL_ASSERT(element != NULL && element->value != NULL);
172
173 struct aws_rr_client_fixture_request_response_record *record = element->value;
174
175 ASSERT_INT_EQUALS(expected_error_code, record->error_code);
176
177 if (expected_response != NULL) {
178 struct aws_byte_cursor actual_payload = aws_byte_cursor_from_buf(&record->response);
179 ASSERT_TRUE(aws_byte_cursor_eq(expected_response, &actual_payload));
180
181 struct aws_byte_cursor actual_response_topic = aws_byte_cursor_from_buf(&record->response_topic);
182 ASSERT_TRUE(aws_byte_cursor_eq(expected_response_topic, &actual_response_topic));
183 } else {
184 ASSERT_INT_EQUALS(0, record->response.len);
185 ASSERT_INT_EQUALS(0, record->response_topic.len);
186 }
187
188 aws_mutex_unlock(&fixture->lock);
189
190 return AWS_OP_SUCCESS;
191 }
192
193 struct aws_rr_client_fixture_user_properties {
194 struct aws_byte_buf name;
195 struct aws_byte_buf value;
196 };
197
198 struct aws_rr_client_fixture_publish_message {
199 struct aws_byte_buf payload;
200 struct aws_byte_buf topic;
201 struct aws_byte_buf content_type;
202 size_t user_property_count;
203 struct aws_rr_client_fixture_user_properties *user_properties;
204 /* A view for user_properties field, for convenient comparison with expected results. */
205 struct aws_mqtt5_user_property *user_properties_view;
206 uint32_t message_expiry_interval_seconds;
207 };
208
209 struct aws_rr_client_fixture_publish_message_view {
210 struct aws_byte_cursor payload;
211 struct aws_byte_cursor topic;
212 struct aws_byte_cursor content_type;
213 size_t user_property_count;
214 const struct aws_mqtt5_user_property *user_properties;
215 uint32_t message_expiry_interval_seconds;
216 };
217
218 struct aws_rr_client_fixture_streaming_record {
219 struct aws_allocator *allocator;
220
221 struct aws_rr_client_test_fixture *fixture;
222
223 struct aws_byte_cursor record_key_cursor;
224 struct aws_byte_buf record_key;
225
226 struct aws_array_list publishes;
227 struct aws_array_list subscription_events;
228
229 bool terminated;
230 };
231
232 struct aws_rr_client_fixture_streaming_record_subscription_event {
233 enum aws_rr_streaming_subscription_event_type status;
234 int error_code;
235 };
236
237 struct aws_rr_client_fixture_streaming_record *s_aws_rr_client_fixture_streaming_record_new(
238 struct aws_allocator *allocator,
239 struct aws_rr_client_test_fixture *fixture,
240 struct aws_byte_cursor record_key) {
241 struct aws_rr_client_fixture_streaming_record *record =
242 aws_mem_calloc(allocator, 1, sizeof(struct aws_rr_client_fixture_streaming_record));
243
244 record->allocator = allocator;
245 record->fixture = fixture;
246
247 aws_byte_buf_init_copy_from_cursor(&record->record_key, allocator, record_key);
248 record->record_key_cursor = aws_byte_cursor_from_buf(&record->record_key);
249
250 aws_array_list_init_dynamic(
251 &record->publishes, allocator, 10, sizeof(struct aws_rr_client_fixture_publish_message));
252 aws_array_list_init_dynamic(
253 &record->subscription_events,
254 allocator,
255 10,
256 sizeof(struct aws_rr_client_fixture_streaming_record_subscription_event));
257
258 return record;
259 }
260
261 void s_aws_rr_client_fixture_streaming_record_delete(struct aws_rr_client_fixture_streaming_record *record) {
262 aws_byte_buf_clean_up(&record->record_key);
263
264 size_t publish_count = aws_array_list_length(&record->publishes);
265 for (size_t i = 0; i < publish_count; ++i) {
266 struct aws_rr_client_fixture_publish_message publish_message;
267 aws_array_list_get_at(&record->publishes, &publish_message, i);
268
269 aws_byte_buf_clean_up(&publish_message.payload);
270 aws_byte_buf_clean_up(&publish_message.topic);
271 aws_byte_buf_clean_up(&publish_message.content_type);
272 aws_mem_release(record->allocator, publish_message.user_properties_view);
273 for (size_t j = 0; j < publish_message.user_property_count; ++j) {
274 aws_byte_buf_clean_up(&publish_message.user_properties[j].name);
275 aws_byte_buf_clean_up(&publish_message.user_properties[j].value);
276 }
277 aws_mem_release(record->allocator, publish_message.user_properties);
278 }
279
280 aws_array_list_clean_up(&record->publishes);
281 aws_array_list_clean_up(&record->subscription_events);
282
283 aws_mem_release(record->allocator, record);
284 }
285
286 static void s_aws_rr_client_fixture_streaming_record_hash_destroy(void *element) {
287 struct aws_rr_client_fixture_streaming_record *record = element;
288
289 s_aws_rr_client_fixture_streaming_record_delete(record);
290 }
291
292 static void s_rrc_fixture_streaming_operation_subscription_status_callback(
293 enum aws_rr_streaming_subscription_event_type status,
294 int error_code,
295 void *user_data) {
296
297 struct aws_rr_client_fixture_streaming_record *record = user_data;
298 struct aws_rr_client_test_fixture *fixture = record->fixture;
299
300 aws_mutex_lock(&fixture->lock);
301
302 struct aws_rr_client_fixture_streaming_record_subscription_event event = {
303 .status = status,
304 .error_code = error_code,
305 };
306 aws_array_list_push_back(&record->subscription_events, &event);
307
308 aws_mutex_unlock(&fixture->lock);
309 aws_condition_variable_notify_all(&fixture->signal);
310 }
311
312 static void s_rrc_fixture_streaming_operation_incoming_publish_callback(
313 const struct aws_mqtt_rr_incoming_publish_event *publish_event,
314 void *user_data) {
315 struct aws_rr_client_fixture_streaming_record *record = user_data;
316 struct aws_rr_client_test_fixture *fixture = record->fixture;
317
318 aws_mutex_lock(&fixture->lock);
319
320 struct aws_rr_client_fixture_publish_message publish_message;
321 AWS_ZERO_STRUCT(publish_message);
322
323 aws_byte_buf_init_copy_from_cursor(&publish_message.payload, fixture->allocator, publish_event->payload);
324 aws_byte_buf_init_copy_from_cursor(&publish_message.topic, fixture->allocator, publish_event->topic);
325 if (publish_event->content_type) {
326 aws_byte_buf_init_copy_from_cursor(
327 &publish_message.content_type, fixture->allocator, *publish_event->content_type);
328 }
329
330 publish_message.user_property_count = publish_event->user_property_count;
331 if (publish_event->user_property_count > 0) {
332 publish_message.user_properties = aws_mem_calloc(
333 fixture->allocator,
334 publish_event->user_property_count,
335 sizeof(struct aws_rr_client_fixture_user_properties));
336 publish_message.user_properties_view = aws_mem_calloc(
337 fixture->allocator, publish_event->user_property_count, sizeof(struct aws_mqtt5_user_property));
338 for (size_t i = 0; i < publish_event->user_property_count; ++i) {
339 aws_byte_buf_init_copy_from_cursor(
340 &publish_message.user_properties[i].name, fixture->allocator, publish_event->user_properties[i].name);
341 aws_byte_buf_init_copy_from_cursor(
342 &publish_message.user_properties[i].value, fixture->allocator, publish_event->user_properties[i].value);
343 publish_message.user_properties_view[i].name =
344 aws_byte_cursor_from_buf(&publish_message.user_properties[i].name);
345 publish_message.user_properties_view[i].value =
346 aws_byte_cursor_from_buf(&publish_message.user_properties[i].value);
347 }
348 }
349
350 if (publish_event->message_expiry_interval_seconds) {
351 publish_message.message_expiry_interval_seconds = *publish_event->message_expiry_interval_seconds;
352 }
353
354 aws_array_list_push_back(&record->publishes, &publish_message);
355
356 aws_mutex_unlock(&fixture->lock);
357 aws_condition_variable_notify_all(&fixture->signal);
358 }
359
360 static void s_rrc_fixture_streaming_operation_terminated_callback(void *user_data) {
361 struct aws_rr_client_fixture_streaming_record *record = user_data;
362 struct aws_rr_client_test_fixture *fixture = record->fixture;
363
364 aws_mutex_lock(&fixture->lock);
365
366 record->terminated = true;
367
368 aws_mutex_unlock(&fixture->lock);
369 aws_condition_variable_notify_all(&fixture->signal);
370 }
371
372 static struct aws_rr_client_fixture_streaming_record *s_rrc_fixture_add_streaming_record(
373 struct aws_rr_client_test_fixture *fixture,
374 struct aws_byte_cursor key) {
375 struct aws_rr_client_fixture_streaming_record *record =
376 s_aws_rr_client_fixture_streaming_record_new(fixture->allocator, fixture, key);
377
378 aws_hash_table_put(&fixture->streaming_records, &record->record_key_cursor, record, NULL);
379
380 return record;
381 }
382
383 static bool s_is_stream_terminated(void *context) {
384 struct rrc_operation_completion_context *completion_context = context;
385
386 struct aws_hash_element *element = NULL;
387 aws_hash_table_find(&completion_context->fixture->streaming_records, &completion_context->key, &element);
388
389 AWS_FATAL_ASSERT(element != NULL && element->value != NULL);
390
391 struct aws_rr_client_fixture_streaming_record *record = element->value;
392
393 return record->terminated;
394 }
395
396 static void s_rrc_wait_on_streaming_termination(
397 struct aws_rr_client_test_fixture *fixture,
398 struct aws_byte_cursor key) {
399 struct rrc_operation_completion_context context = {
400 .key = key,
401 .fixture = fixture,
402 };
403
404 aws_mutex_lock(&fixture->lock);
405 aws_condition_variable_wait_pred(&fixture->signal, &fixture->lock, s_is_stream_terminated, &context);
406 aws_mutex_unlock(&fixture->lock);
407 }
408
409 struct rrc_streaming_event_wait_context {
410 struct aws_byte_cursor operation_key;
411 struct aws_rr_client_test_fixture *fixture;
412 size_t event_count;
413 };
414
415 static bool s_streaming_operation_has_n_publishes(void *context) {
416 struct rrc_streaming_event_wait_context *streaming_publish_context = context;
417
418 struct aws_hash_element *element = NULL;
419 aws_hash_table_find(
420 &streaming_publish_context->fixture->streaming_records, &streaming_publish_context->operation_key, &element);
421
422 AWS_FATAL_ASSERT(element != NULL && element->value != NULL);
423
424 struct aws_rr_client_fixture_streaming_record *record = element->value;
425
426 return aws_array_list_length(&record->publishes) >= streaming_publish_context->event_count;
427 }
428
429 static void s_rrc_wait_for_n_streaming_publishes(
430 struct aws_rr_client_test_fixture *fixture,
431 struct aws_byte_cursor key,
432 size_t count) {
433 struct rrc_streaming_event_wait_context context = {
434 .operation_key = key,
435 .fixture = fixture,
436 .event_count = count,
437 };
438
439 aws_mutex_lock(&fixture->lock);
440 aws_condition_variable_wait_pred(&fixture->signal, &fixture->lock, s_streaming_operation_has_n_publishes, &context);
441 aws_mutex_unlock(&fixture->lock);
442 }
443
444 static int s_rrc_verify_streaming_publishes(
445 struct aws_rr_client_test_fixture *fixture,
446 struct aws_byte_cursor key,
447 size_t expected_publish_count,
448 struct aws_rr_client_fixture_publish_message_view *expected_publishes) {
449
450 aws_mutex_lock(&fixture->lock);
451
452 struct aws_hash_element *element = NULL;
453 aws_hash_table_find(&fixture->streaming_records, &key, &element);
454
455 AWS_FATAL_ASSERT(element != NULL && element->value != NULL);
456
457 struct aws_rr_client_fixture_streaming_record *record = element->value;
458
459 size_t actual_publish_count = aws_array_list_length(&record->publishes);
460 ASSERT_INT_EQUALS(expected_publish_count, actual_publish_count);
461
462 for (size_t i = 0; i < actual_publish_count; ++i) {
463 struct aws_rr_client_fixture_publish_message actual_publish_message;
464 aws_array_list_get_at(&record->publishes, &actual_publish_message, i);
465
466 struct aws_rr_client_fixture_publish_message_view *expected_publish_message = &expected_publishes[i];
467
468 ASSERT_BIN_ARRAYS_EQUALS(
469 expected_publish_message->payload.ptr,
470 expected_publish_message->payload.len,
471 actual_publish_message.payload.buffer,
472 actual_publish_message.payload.len);
473 ASSERT_BIN_ARRAYS_EQUALS(
474 expected_publish_message->topic.ptr,
475 expected_publish_message->topic.len,
476 actual_publish_message.topic.buffer,
477 actual_publish_message.topic.len);
478 if (fixture->test_protocol == RRCP_MQTT5) {
479 ASSERT_BIN_ARRAYS_EQUALS(
480 expected_publish_message->content_type.ptr,
481 expected_publish_message->content_type.len,
482 actual_publish_message.content_type.buffer,
483 actual_publish_message.content_type.len);
484 aws_mqtt5_test_verify_user_properties_raw(
485 actual_publish_message.user_property_count,
486 actual_publish_message.user_properties_view,
487 expected_publish_message->user_property_count,
488 expected_publish_message->user_properties);
489 ASSERT_INT_EQUALS(
490 expected_publish_message->message_expiry_interval_seconds,
491 actual_publish_message.message_expiry_interval_seconds);
492 } else {
493 ASSERT_INT_EQUALS(0, actual_publish_message.content_type.len);
494 ASSERT_PTR_EQUALS(NULL, actual_publish_message.content_type.buffer);
495 ASSERT_INT_EQUALS(0, actual_publish_message.user_property_count);
496 ASSERT_PTR_EQUALS(NULL, actual_publish_message.user_properties);
497 ASSERT_INT_EQUALS(0, actual_publish_message.message_expiry_interval_seconds);
498 }
499 }
500
501 aws_mutex_unlock(&fixture->lock);
502
503 return AWS_OP_SUCCESS;
504 }
505
506 static bool s_streaming_operation_has_n_subscription_events(void *context) {
507 struct rrc_streaming_event_wait_context *streaming_publish_context = context;
508
509 struct aws_hash_element *element = NULL;
510 aws_hash_table_find(
511 &streaming_publish_context->fixture->streaming_records, &streaming_publish_context->operation_key, &element);
512
513 AWS_FATAL_ASSERT(element != NULL && element->value != NULL);
514
515 struct aws_rr_client_fixture_streaming_record *record = element->value;
516
517 return aws_array_list_length(&record->subscription_events) >= streaming_publish_context->event_count;
518 }
519
520 static void s_rrc_wait_for_n_streaming_subscription_events(
521 struct aws_rr_client_test_fixture *fixture,
522 struct aws_byte_cursor key,
523 size_t count) {
524 struct rrc_streaming_event_wait_context context = {
525 .operation_key = key,
526 .fixture = fixture,
527 .event_count = count,
528 };
529
530 aws_mutex_lock(&fixture->lock);
531 aws_condition_variable_wait_pred(
532 &fixture->signal, &fixture->lock, s_streaming_operation_has_n_subscription_events, &context);
533 aws_mutex_unlock(&fixture->lock);
534 }
535
536 static int s_rrc_verify_streaming_record_subscription_events(
537 struct aws_rr_client_test_fixture *fixture,
538 struct aws_byte_cursor key,
539 size_t expected_subscription_event_count,
540 struct aws_rr_client_fixture_streaming_record_subscription_event *expected_subscription_events) {
541 aws_mutex_lock(&fixture->lock);
542
543 struct aws_hash_element *element = NULL;
544 aws_hash_table_find(&fixture->streaming_records, &key, &element);
545
546 AWS_FATAL_ASSERT(element != NULL && element->value != NULL);
547
548 struct aws_rr_client_fixture_streaming_record *record = element->value;
549
550 size_t actual_subscription_event_count = aws_array_list_length(&record->subscription_events);
551 ASSERT_INT_EQUALS(expected_subscription_event_count, actual_subscription_event_count);
552
553 for (size_t i = 0; i < actual_subscription_event_count; ++i) {
554 struct aws_rr_client_fixture_streaming_record_subscription_event actual_event;
555 aws_array_list_get_at(&record->subscription_events, &actual_event, i);
556
557 struct aws_rr_client_fixture_streaming_record_subscription_event *expected_event =
558 &expected_subscription_events[i];
559
560 ASSERT_INT_EQUALS(expected_event->status, actual_event.status);
561 ASSERT_INT_EQUALS(expected_event->error_code, actual_event.error_code);
562 }
563
564 aws_mutex_unlock(&fixture->lock);
565
566 return AWS_OP_SUCCESS;
567 }
568
569 static void s_aws_rr_client_test_fixture_on_initialized(void *user_data) {
570 struct aws_rr_client_test_fixture *fixture = user_data;
571
572 aws_mutex_lock(&fixture->lock);
573 fixture->client_initialized = true;
574 aws_mutex_unlock(&fixture->lock);
575 aws_condition_variable_notify_all(&fixture->signal);
576 }
577
578 static bool s_rr_client_test_fixture_initialized(void *context) {
579 struct aws_rr_client_test_fixture *fixture = context;
580
581 return fixture->client_initialized;
582 }
583
584 static void s_aws_rr_client_test_fixture_wait_for_initialized(struct aws_rr_client_test_fixture *fixture) {
585 aws_mutex_lock(&fixture->lock);
586 aws_condition_variable_wait_pred(&fixture->signal, &fixture->lock, s_rr_client_test_fixture_initialized, fixture);
587 aws_mutex_unlock(&fixture->lock);
588 }
589
590 static void s_aws_rr_client_test_fixture_on_terminated(void *user_data) {
591 struct aws_rr_client_test_fixture *fixture = user_data;
592
593 aws_mutex_lock(&fixture->lock);
594 fixture->client_destroyed = true;
595 aws_mutex_unlock(&fixture->lock);
596 aws_condition_variable_notify_all(&fixture->signal);
597 }
598
599 static int s_aws_rr_client_test_fixture_init_from_mqtt5(
600 struct aws_rr_client_test_fixture *fixture,
601 struct aws_allocator *allocator,
602 struct aws_mqtt_request_response_client_options *rr_client_options,
603 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options *client_test_fixture_options,
604 void *test_context) {
605 AWS_ZERO_STRUCT(*fixture);
606 fixture->allocator = allocator;
607 fixture->test_protocol = RRCP_MQTT5;
608
609 aws_mutex_init(&fixture->lock);
610 aws_condition_variable_init(&fixture->signal);
611 fixture->test_context = test_context;
612
613 aws_hash_table_init(
614 &fixture->request_response_records,
615 allocator,
616 10,
617 aws_hash_byte_cursor_ptr,
618 aws_mqtt_byte_cursor_hash_equality,
619 NULL,
620 s_aws_rr_client_fixture_request_response_record_hash_destroy);
621
622 aws_hash_table_init(
623 &fixture->streaming_records,
624 allocator,
625 10,
626 aws_hash_byte_cursor_ptr,
627 aws_mqtt_byte_cursor_hash_equality,
628 NULL,
629 s_aws_rr_client_fixture_streaming_record_hash_destroy);
630
631 if (aws_mqtt5_client_mock_test_fixture_init(
632 &fixture->client_test_fixture.mqtt5_test_fixture, allocator, client_test_fixture_options)) {
633 return AWS_OP_ERR;
634 }
635
636 struct aws_mqtt_request_response_client_options client_options = {
637 .max_request_response_subscriptions = 2,
638 .max_streaming_subscriptions = 2,
639 .operation_timeout_seconds = 5,
640 };
641
642 if (rr_client_options != NULL) {
643 client_options = *rr_client_options;
644 }
645
646 client_options.initialized_callback = s_aws_rr_client_test_fixture_on_initialized;
647 client_options.terminated_callback = s_aws_rr_client_test_fixture_on_terminated;
648 client_options.user_data = fixture;
649
650 fixture->rr_client = aws_mqtt_request_response_client_new_from_mqtt5_client(
651 allocator, fixture->client_test_fixture.mqtt5_test_fixture.client, &client_options);
652 AWS_FATAL_ASSERT(fixture->rr_client != NULL);
653
654 aws_mqtt5_client_start(fixture->client_test_fixture.mqtt5_test_fixture.client);
655
656 aws_wait_for_connected_lifecycle_event(&fixture->client_test_fixture.mqtt5_test_fixture);
657 s_aws_rr_client_test_fixture_wait_for_initialized(fixture);
658
659 return AWS_OP_SUCCESS;
660 }
661
662 static int s_aws_rr_client_test_fixture_init_from_mqtt311(
663 struct aws_rr_client_test_fixture *fixture,
664 struct aws_allocator *allocator,
665 struct aws_mqtt_request_response_client_options *rr_client_options,
666 void *test_context) {
667 AWS_ZERO_STRUCT(*fixture);
668 fixture->allocator = allocator;
669 fixture->test_protocol = RRCP_MQTT311;
670
671 aws_mutex_init(&fixture->lock);
672 aws_condition_variable_init(&fixture->signal);
673 fixture->test_context = test_context;
674
675 aws_hash_table_init(
676 &fixture->request_response_records,
677 allocator,
678 10,
679 aws_hash_byte_cursor_ptr,
680 aws_mqtt_byte_cursor_hash_equality,
681 NULL,
682 s_aws_rr_client_fixture_request_response_record_hash_destroy);
683
684 aws_hash_table_init(
685 &fixture->streaming_records,
686 allocator,
687 10,
688 aws_hash_byte_cursor_ptr,
689 aws_mqtt_byte_cursor_hash_equality,
690 NULL,
691 s_aws_rr_client_fixture_streaming_record_hash_destroy);
692
693 aws_test311_setup_mqtt_server_fn(allocator, &fixture->client_test_fixture.mqtt311_test_fixture);
694
695 struct aws_mqtt_request_response_client_options client_options = {
696 .max_request_response_subscriptions = 2,
697 .max_streaming_subscriptions = 2,
698 .operation_timeout_seconds = 5,
699 };
700
701 if (rr_client_options != NULL) {
702 client_options = *rr_client_options;
703 }
704
705 client_options.initialized_callback = s_aws_rr_client_test_fixture_on_initialized;
706 client_options.terminated_callback = s_aws_rr_client_test_fixture_on_terminated;
707 client_options.user_data = fixture;
708
709 struct aws_mqtt_client_connection *mqtt_client = fixture->client_test_fixture.mqtt311_test_fixture.mqtt_connection;
710
711 /* Server should reflect PUBLISH messages. */
712 mqtt_mock_server_set_publish_reflection(fixture->client_test_fixture.mqtt311_test_fixture.mock_server, true);
713
714 fixture->rr_client =
715 aws_mqtt_request_response_client_new_from_mqtt311_client(allocator, mqtt_client, &client_options);
716 AWS_FATAL_ASSERT(fixture->rr_client != NULL);
717
718 struct aws_mqtt_connection_options connection_options = {
719 .user_data = &fixture->client_test_fixture.mqtt311_test_fixture,
720 .clean_session = false,
721 .client_id = aws_byte_cursor_from_c_str("client1234"),
722 .host_name = aws_byte_cursor_from_c_str(fixture->client_test_fixture.mqtt311_test_fixture.endpoint.address),
723 .socket_options = &fixture->client_test_fixture.mqtt311_test_fixture.socket_options,
724 .on_connection_complete = aws_test311_on_connection_complete_fn,
725 .ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS,
726 .keep_alive_time_secs = 16960,
727 };
728
729 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(mqtt_client, &connection_options));
730 aws_test311_wait_for_connection_to_complete(&fixture->client_test_fixture.mqtt311_test_fixture);
731
732 s_aws_rr_client_test_fixture_wait_for_initialized(fixture);
733
734 return AWS_OP_SUCCESS;
735 }
736
737 static bool s_rr_client_test_fixture_terminated(void *context) {
738 struct aws_rr_client_test_fixture *fixture = context;
739
740 return fixture->client_destroyed;
741 }
742
743 static void s_aws_rr_client_test_fixture_clean_up(
744 struct aws_rr_client_test_fixture *fixture,
745 bool protocol_has_terminated) {
746 aws_mqtt_request_response_client_release(fixture->rr_client);
747
748 aws_mutex_lock(&fixture->lock);
749 aws_condition_variable_wait_pred(&fixture->signal, &fixture->lock, s_rr_client_test_fixture_terminated, fixture);
750 aws_mutex_unlock(&fixture->lock);
751
752 if (!protocol_has_terminated) {
753 if (fixture->test_protocol == RRCP_MQTT5) {
754 aws_mqtt5_client_mock_test_fixture_clean_up(&fixture->client_test_fixture.mqtt5_test_fixture);
755 } else {
756 struct mqtt_connection_state_test *mqtt311_test_fixture =
757 &fixture->client_test_fixture.mqtt311_test_fixture;
758 aws_mqtt_client_connection_disconnect(
759 mqtt311_test_fixture->mqtt_connection, aws_test311_on_disconnect_fn, mqtt311_test_fixture);
760 aws_test311_clean_up_mqtt_server_fn(
761 fixture->allocator, AWS_OP_SUCCESS, &fixture->client_test_fixture.mqtt311_test_fixture);
762 }
763 }
764
765 aws_thread_join_all_managed();
766 aws_mutex_clean_up(&fixture->lock);
767 aws_condition_variable_clean_up(&fixture->signal);
768
769 aws_hash_table_clean_up(&fixture->request_response_records);
770 aws_hash_table_clean_up(&fixture->streaming_records);
771 }
772
773 static int s_rrc_mqtt5_create_destroy_fn(struct aws_allocator *allocator, void *ctx) {
774 (void)ctx;
775
776 aws_mqtt_library_init(allocator);
777
778 struct mqtt5_client_test_options client_test_options;
779 aws_mqtt5_client_test_init_default_options(&client_test_options);
780
781 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options client_test_fixture_options = {
782 .client_options = &client_test_options.client_options,
783 .server_function_table = &client_test_options.server_function_table,
784 };
785
786 struct aws_rr_client_test_fixture fixture;
787 ASSERT_SUCCESS(
788 s_aws_rr_client_test_fixture_init_from_mqtt5(&fixture, allocator, NULL, &client_test_fixture_options, NULL));
789
790 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
791
792 aws_mqtt_library_clean_up();
793
794 return AWS_OP_SUCCESS;
795 }
796
797 AWS_TEST_CASE(rrc_mqtt5_create_destroy, s_rrc_mqtt5_create_destroy_fn)
798
799 static int s_rrc_mqtt311_create_destroy_fn(struct aws_allocator *allocator, void *ctx) {
800 (void)ctx;
801
802 aws_mqtt_library_init(allocator);
803
804 struct aws_rr_client_test_fixture fixture;
805 ASSERT_SUCCESS(s_aws_rr_client_test_fixture_init_from_mqtt311(&fixture, allocator, NULL, NULL));
806
807 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
808
809 aws_mqtt_library_clean_up();
810
811 return AWS_OP_SUCCESS;
812 }
813
814 AWS_TEST_CASE(rrc_mqtt311_create_destroy, s_rrc_mqtt311_create_destroy_fn)
815
816 static char s_response_filter_wildcard[] = "response/filter/+";
817 static struct aws_byte_cursor s_response_filter_wildcard_cursor = {
818 .ptr = (uint8_t *)s_response_filter_wildcard,
819 .len = AWS_ARRAY_SIZE(s_response_filter_wildcard) - 1,
820 };
821
822 static int s_rrc_do_submit_request_operation_failure_test(
823 struct aws_allocator *allocator,
824 void (*request_mutator_fn)(struct aws_mqtt_request_operation_options *)) {
825 aws_mqtt_library_init(allocator);
826
827 struct mqtt5_client_test_options client_test_options;
828 aws_mqtt5_client_test_init_default_options(&client_test_options);
829
830 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options client_test_fixture_options = {
831 .client_options = &client_test_options.client_options,
832 .server_function_table = &client_test_options.server_function_table,
833 };
834
835 struct aws_rr_client_test_fixture fixture;
836 ASSERT_SUCCESS(
837 s_aws_rr_client_test_fixture_init_from_mqtt5(&fixture, allocator, NULL, &client_test_fixture_options, NULL));
838
839 struct aws_mqtt_request_operation_response_path response_paths[] = {
840 {
841 .topic = aws_byte_cursor_from_c_str("response/filter/accepted"),
842 .correlation_token_json_path = aws_byte_cursor_from_c_str("client_token"),
843 },
844 {
845 .topic = aws_byte_cursor_from_c_str("response/filter/rejected"),
846 .correlation_token_json_path = aws_byte_cursor_from_c_str("client_token"),
847 },
848 };
849 struct aws_mqtt_request_operation_options good_request = {
850 .subscription_topic_filters = &s_response_filter_wildcard_cursor,
851 .subscription_topic_filter_count = 1,
852 .response_paths = response_paths,
853 .response_path_count = AWS_ARRAY_SIZE(response_paths),
854 .publish_topic = aws_byte_cursor_from_c_str("get/shadow"),
855 .serialized_request = aws_byte_cursor_from_c_str("{}"),
856 .correlation_token = aws_byte_cursor_from_c_str("MyRequest#1"),
857 };
858 ASSERT_SUCCESS(aws_mqtt_request_response_client_submit_request(fixture.rr_client, &good_request));
859
860 struct aws_mqtt_request_operation_options bad_request = good_request;
861 (*request_mutator_fn)(&bad_request);
862
863 ASSERT_FAILS(aws_mqtt_request_response_client_submit_request(fixture.rr_client, &bad_request));
864
865 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
866
867 aws_mqtt_library_clean_up();
868
869 return AWS_OP_SUCCESS;
870 }
871
872 static void s_no_response_paths_mutator(struct aws_mqtt_request_operation_options *request_options) {
873 request_options->response_path_count = 0;
874 request_options->response_paths = NULL;
875 }
876
877 static int s_rrc_submit_request_operation_failure_no_response_paths_fn(struct aws_allocator *allocator, void *ctx) {
878 (void)ctx;
879
880 return s_rrc_do_submit_request_operation_failure_test(allocator, s_no_response_paths_mutator);
881 }
882
883 AWS_TEST_CASE(
884 rrc_submit_request_operation_failure_no_response_paths,
885 s_rrc_submit_request_operation_failure_no_response_paths_fn)
886
887 static void s_invalid_response_topic_mutator(struct aws_mqtt_request_operation_options *request_options) {
888 request_options->response_paths[0].topic = aws_byte_cursor_from_c_str("a/b/#");
889 }
890
891 static int s_rrc_submit_request_operation_failure_invalid_response_topic_fn(
892 struct aws_allocator *allocator,
893 void *ctx) {
894 (void)ctx;
895
896 return s_rrc_do_submit_request_operation_failure_test(allocator, s_invalid_response_topic_mutator);
897 }
898
899 AWS_TEST_CASE(
900 rrc_submit_request_operation_failure_invalid_response_topic,
901 s_rrc_submit_request_operation_failure_invalid_response_topic_fn)
902
903 static void s_invalid_publish_topic_mutator(struct aws_mqtt_request_operation_options *request_options) {
904 request_options->publish_topic = aws_byte_cursor_from_c_str("a/b/#");
905 }
906
907 static int s_rrc_submit_request_operation_failure_invalid_publish_topic_fn(struct aws_allocator *allocator, void *ctx) {
908 (void)ctx;
909
910 return s_rrc_do_submit_request_operation_failure_test(allocator, s_invalid_publish_topic_mutator);
911 }
912
913 AWS_TEST_CASE(
914 rrc_submit_request_operation_failure_invalid_publish_topic,
915 s_rrc_submit_request_operation_failure_invalid_publish_topic_fn)
916
917 static char s_bad_filter[] = "a/#/c";
918 static struct aws_byte_cursor s_bad_filter_cursor = {
919 .ptr = (uint8_t *)s_bad_filter,
920 .len = AWS_ARRAY_SIZE(s_bad_filter) - 1,
921 };
922
923 static void s_invalid_subscription_topic_filter_mutator(struct aws_mqtt_request_operation_options *request_options) {
924 request_options->subscription_topic_filters = &s_bad_filter_cursor;
925 }
926
927 static int s_rrc_submit_request_operation_failure_invalid_subscription_topic_filter_fn(
928 struct aws_allocator *allocator,
929 void *ctx) {
930 (void)ctx;
931
932 return s_rrc_do_submit_request_operation_failure_test(allocator, s_invalid_subscription_topic_filter_mutator);
933 }
934
935 AWS_TEST_CASE(
936 rrc_submit_request_operation_failure_invalid_subscription_topic_filter,
937 s_rrc_submit_request_operation_failure_invalid_subscription_topic_filter_fn)
938
939 static void s_no_subscription_topic_filter_mutator(struct aws_mqtt_request_operation_options *request_options) {
940 request_options->subscription_topic_filter_count = 0;
941 }
942
943 static int s_rrc_submit_request_operation_failure_no_subscription_topic_filters_fn(
944 struct aws_allocator *allocator,
945 void *ctx) {
946 (void)ctx;
947
948 return s_rrc_do_submit_request_operation_failure_test(allocator, s_no_subscription_topic_filter_mutator);
949 }
950
951 AWS_TEST_CASE(
952 rrc_submit_request_operation_failure_no_subscription_topic_filters,
953 s_rrc_submit_request_operation_failure_no_subscription_topic_filters_fn)
954
955 static void s_empty_request_mutator(struct aws_mqtt_request_operation_options *request_options) {
956 request_options->serialized_request = aws_byte_cursor_from_c_str("");
957 }
958
959 static int s_rrc_submit_request_operation_failure_empty_request_fn(struct aws_allocator *allocator, void *ctx) {
960 (void)ctx;
961
962 return s_rrc_do_submit_request_operation_failure_test(allocator, s_empty_request_mutator);
963 }
964
965 AWS_TEST_CASE(
966 rrc_submit_request_operation_failure_empty_request,
967 s_rrc_submit_request_operation_failure_empty_request_fn)
968
969 static int s_rrc_submit_streaming_operation_failure_invalid_subscription_topic_filter_fn(
970 struct aws_allocator *allocator,
971 void *ctx) {
972 (void)ctx;
973
974 aws_mqtt_library_init(allocator);
975
976 struct mqtt5_client_test_options client_test_options;
977 aws_mqtt5_client_test_init_default_options(&client_test_options);
978
979 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options client_test_fixture_options = {
980 .client_options = &client_test_options.client_options,
981 .server_function_table = &client_test_options.server_function_table,
982 };
983
984 struct aws_rr_client_test_fixture fixture;
985 ASSERT_SUCCESS(
986 s_aws_rr_client_test_fixture_init_from_mqtt5(&fixture, allocator, NULL, &client_test_fixture_options, NULL));
987
988 struct aws_mqtt_streaming_operation_options good_options = {
989 .topic_filter = aws_byte_cursor_from_c_str("a/b"),
990 };
991
992 struct aws_mqtt_rr_client_operation *good_operation =
993 aws_mqtt_request_response_client_create_streaming_operation(fixture.rr_client, &good_options);
994 ASSERT_NOT_NULL(good_operation);
995 ASSERT_SUCCESS(aws_mqtt_rr_client_operation_activate(good_operation));
996
997 aws_mqtt_rr_client_operation_release(good_operation);
998
999 struct aws_mqtt_streaming_operation_options bad_options = good_options;
1000 bad_options.topic_filter = aws_byte_cursor_from_c_str("");
1001
1002 struct aws_mqtt_rr_client_operation *bad_operation =
1003 aws_mqtt_request_response_client_create_streaming_operation(fixture.rr_client, &bad_options);
1004 ASSERT_NULL(bad_operation);
1005
1006 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
1007
1008 aws_mqtt_library_clean_up();
1009
1010 return AWS_OP_SUCCESS;
1011 }
1012
1013 AWS_TEST_CASE(
1014 rrc_submit_streaming_operation_failure_invalid_subscription_topic_filter,
1015 s_rrc_submit_streaming_operation_failure_invalid_subscription_topic_filter_fn)
1016
1017 static int s_do_rrc_single_request_operation_test_fn(
1018 struct aws_allocator *allocator,
1019 struct aws_mqtt_request_response_client_options *rr_client_options,
1020 struct aws_mqtt_request_operation_options *request_options,
1021 int expected_error_code,
1022 struct aws_byte_cursor *expected_response_topic,
1023 struct aws_byte_cursor *expected_payload,
1024 bool shutdown_after_submit) {
1025 aws_mqtt_library_init(allocator);
1026
1027 struct mqtt5_client_test_options client_test_options;
1028 aws_mqtt5_client_test_init_default_options(&client_test_options);
1029
1030 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options client_test_fixture_options = {
1031 .client_options = &client_test_options.client_options,
1032 .server_function_table = &client_test_options.server_function_table,
1033 };
1034
1035 struct aws_rr_client_test_fixture fixture;
1036 ASSERT_SUCCESS(s_aws_rr_client_test_fixture_init_from_mqtt5(
1037 &fixture, allocator, rr_client_options, &client_test_fixture_options, NULL));
1038
1039 struct aws_rr_client_fixture_request_response_record *record =
1040 s_rrc_fixture_add_request_record(&fixture, request_options->serialized_request);
1041
1042 request_options->completion_callback = s_rrc_fixture_request_completion_callback;
1043 request_options->user_data = record;
1044
1045 ASSERT_SUCCESS(aws_mqtt_request_response_client_submit_request(fixture.rr_client, request_options));
1046
1047 if (shutdown_after_submit) {
1048 aws_mqtt_request_response_client_release(fixture.rr_client);
1049 fixture.rr_client = NULL;
1050 }
1051
1052 s_rrc_wait_on_request_completion(&fixture, request_options->serialized_request);
1053
1054 ASSERT_SUCCESS(s_rrc_verify_request_completion(
1055 &fixture, request_options->serialized_request, expected_error_code, expected_response_topic, expected_payload));
1056
1057 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
1058
1059 aws_mqtt_library_clean_up();
1060
1061 return AWS_OP_SUCCESS;
1062 }
1063
1064 static int s_rrc_submit_request_operation_failure_by_shutdown_fn(struct aws_allocator *allocator, void *ctx) {
1065 (void)ctx;
1066
1067 struct aws_mqtt_request_operation_response_path response_paths[] = {
1068 {
1069 .topic = aws_byte_cursor_from_c_str("response/filter/accepted"),
1070 .correlation_token_json_path = aws_byte_cursor_from_c_str("client_token"),
1071 },
1072 };
1073
1074 struct aws_mqtt_request_operation_options request = {
1075 .subscription_topic_filters = &s_response_filter_wildcard_cursor,
1076 .subscription_topic_filter_count = 1,
1077 .response_paths = response_paths,
1078 .response_path_count = AWS_ARRAY_SIZE(response_paths),
1079 .publish_topic = aws_byte_cursor_from_c_str("get/shadow"),
1080 .serialized_request = aws_byte_cursor_from_c_str("request1"),
1081 .correlation_token = aws_byte_cursor_from_c_str("MyRequest#1"),
1082 };
1083
1084 return s_do_rrc_single_request_operation_test_fn(
1085 allocator, NULL, &request, AWS_ERROR_MQTT_REQUEST_RESPONSE_CLIENT_SHUT_DOWN, NULL, NULL, true);
1086 }
1087
1088 AWS_TEST_CASE(rrc_submit_request_operation_failure_by_shutdown, s_rrc_submit_request_operation_failure_by_shutdown_fn)
1089
1090 static int s_do_rrc_single_streaming_operation_test_fn(
1091 struct aws_allocator *allocator,
1092 struct aws_mqtt_request_response_client_options *rr_client_options,
1093 struct aws_mqtt_streaming_operation_options *streaming_options,
1094 size_t expected_subscription_event_count,
1095 struct aws_rr_client_fixture_streaming_record_subscription_event *expected_subscription_events,
1096 bool should_activate,
1097 bool should_activate_after_shutdown,
1098 bool release_after_client_shutdown) {
1099 aws_mqtt_library_init(allocator);
1100
1101 struct mqtt5_client_test_options client_test_options;
1102 aws_mqtt5_client_test_init_default_options(&client_test_options);
1103
1104 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options client_test_fixture_options = {
1105 .client_options = &client_test_options.client_options,
1106 .server_function_table = &client_test_options.server_function_table,
1107 };
1108
1109 struct aws_rr_client_test_fixture fixture;
1110 ASSERT_SUCCESS(s_aws_rr_client_test_fixture_init_from_mqtt5(
1111 &fixture, allocator, rr_client_options, &client_test_fixture_options, NULL));
1112
1113 struct aws_byte_cursor streaming_id = aws_byte_cursor_from_c_str("streaming1");
1114 struct aws_rr_client_fixture_streaming_record *record = s_rrc_fixture_add_streaming_record(&fixture, streaming_id);
1115
1116 streaming_options->incoming_publish_callback = s_rrc_fixture_streaming_operation_incoming_publish_callback;
1117 streaming_options->subscription_status_callback = s_rrc_fixture_streaming_operation_subscription_status_callback;
1118 streaming_options->terminated_callback = s_rrc_fixture_streaming_operation_terminated_callback;
1119 streaming_options->user_data = record;
1120
1121 struct aws_mqtt_rr_client_operation *streaming_operation =
1122 aws_mqtt_request_response_client_create_streaming_operation(fixture.rr_client, streaming_options);
1123 ASSERT_NOT_NULL(streaming_operation);
1124
1125 if (should_activate && !should_activate_after_shutdown) {
1126 ASSERT_SUCCESS(aws_mqtt_rr_client_operation_activate(streaming_operation));
1127 }
1128
1129 aws_mqtt_request_response_client_release(fixture.rr_client);
1130 fixture.rr_client = NULL;
1131
1132 /*
1133 * Extremely awkward sleep:
1134 *
1135 * We've submitted the operation and we've decref'd the client to zero. When the operation submit task
1136 * is processed, if the release in the succeeding line has happened-before the client external destroy task
1137 * has run, then the operation's destroy will be scheduled in-thread and run ahead of the client external
1138 * destroy. This doesn't break correctness, but it does prevent the client from emitting a HALTED event
1139 * on the subscription because the subscription/operation will be gone before the client external destroy
1140 * task runs.
1141 *
1142 * So we add a nice, fat sleep to guarantee that the client external destroy task runs before the operation
1143 * destroy task.
1144 */
1145 aws_thread_current_sleep(aws_timestamp_convert(1, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL));
1146
1147 if (should_activate && should_activate_after_shutdown) {
1148 ASSERT_SUCCESS(aws_mqtt_rr_client_operation_activate(streaming_operation));
1149 }
1150
1151 /* Wait for the client finish termination.
1152 * This option is used for a regression test. In the previous implementation, releasing of the protocol client would
1153 * potentially release the event loop group if no other component acquire the event loop group. This would cause a
1154 * race condition where the rr_client or streaming operation is trying to use the event loop to process its tasks
1155 * while the event loop has already be released. We fixed it by acquire the event loop group for rr client. */
1156 if (release_after_client_shutdown) {
1157 aws_mqtt5_client_mock_test_fixture_clean_up(&fixture.client_test_fixture.mqtt5_test_fixture);
1158 // We sleep here to wait for event loop to be released by the client.
1159 aws_thread_current_sleep(aws_timestamp_convert(1, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL));
1160 }
1161
1162 aws_mqtt_rr_client_operation_release(streaming_operation);
1163
1164 s_rrc_wait_on_streaming_termination(&fixture, streaming_id);
1165
1166 ASSERT_SUCCESS(s_rrc_verify_streaming_record_subscription_events(
1167 &fixture, streaming_id, expected_subscription_event_count, expected_subscription_events));
1168
1169 s_aws_rr_client_test_fixture_clean_up(&fixture, release_after_client_shutdown);
1170
1171 aws_mqtt_library_clean_up();
1172
1173 return AWS_OP_SUCCESS;
1174 }
1175
1176 static int s_rrc_activate_streaming_operation_and_shutdown_fn(struct aws_allocator *allocator, void *ctx) {
1177 (void)ctx;
1178
1179 struct aws_rr_client_fixture_streaming_record_subscription_event expected_events[] = {
1180 {
1181 .status = ARRSSET_SUBSCRIPTION_HALTED,
1182 .error_code = AWS_ERROR_MQTT_REQUEST_RESPONSE_CLIENT_SHUT_DOWN,
1183 },
1184 };
1185
1186 struct aws_mqtt_streaming_operation_options streaming_options = {
1187 .topic_filter = aws_byte_cursor_from_c_str("derp/filter"),
1188 };
1189
1190 return s_do_rrc_single_streaming_operation_test_fn(
1191 allocator,
1192 NULL,
1193 &streaming_options,
1194 AWS_ARRAY_SIZE(expected_events),
1195 expected_events,
1196 /*should_activate=*/true,
1197 /*should_activate_after_shutdown=*/false,
1198 /*release_after_client_shutdown=*/false);
1199 }
1200
1201 AWS_TEST_CASE(rrc_activate_streaming_operation_and_shutdown, s_rrc_activate_streaming_operation_and_shutdown_fn)
1202
1203 static int s_rrc_shutdown_and_activate_streaming_operation_fn(struct aws_allocator *allocator, void *ctx) {
1204 (void)ctx;
1205
1206 struct aws_rr_client_fixture_streaming_record_subscription_event expected_events[] = {
1207 {
1208 .status = ARRSSET_SUBSCRIPTION_HALTED,
1209 .error_code = AWS_ERROR_MQTT_REQUEST_RESPONSE_CLIENT_SHUT_DOWN,
1210 },
1211 };
1212
1213 struct aws_mqtt_streaming_operation_options streaming_options = {
1214 .topic_filter = aws_byte_cursor_from_c_str("derp/filter"),
1215 };
1216
1217 return s_do_rrc_single_streaming_operation_test_fn(
1218 allocator,
1219 NULL,
1220 &streaming_options,
1221 AWS_ARRAY_SIZE(expected_events),
1222 expected_events,
1223 /*should_activate=*/true,
1224 /*should_activate_after_shutdown=*/true,
1225 /*release_after_client_shutdown=*/false);
1226 }
1227
1228 AWS_TEST_CASE(rrc_shutdown_and_activate_streaming_operation, s_rrc_shutdown_and_activate_streaming_operation_fn)
1229
1230 static int s_rrc_create_streaming_operation_and_shutdown_fn(struct aws_allocator *allocator, void *ctx) {
1231 (void)ctx;
1232
1233 struct aws_mqtt_streaming_operation_options streaming_options = {
1234 .topic_filter = aws_byte_cursor_from_c_str("derp/filter"),
1235 };
1236
1237 return s_do_rrc_single_streaming_operation_test_fn(
1238 allocator,
1239 NULL,
1240 &streaming_options,
1241 0,
1242 NULL,
1243 /*should_activate=*/false,
1244 /*should_activate_after_shutdown=*/false,
1245 /*release_after_client_shutdown=*/false);
1246 }
1247
1248 AWS_TEST_CASE(rrc_create_streaming_operation_and_shutdown, s_rrc_create_streaming_operation_and_shutdown_fn)
1249
1250 static int s_rrc_release_streaming_operation_after_client_shutdown_fn(struct aws_allocator *allocator, void *ctx) {
1251 (void)ctx;
1252
1253 struct aws_mqtt_streaming_operation_options streaming_options = {
1254 .topic_filter = aws_byte_cursor_from_c_str("derp/filter")};
1255
1256 return s_do_rrc_single_streaming_operation_test_fn(
1257 allocator,
1258 NULL,
1259 &streaming_options,
1260 0,
1261 NULL,
1262 /*should_activate=*/false,
1263 /*should_activate_after_shutdown=*/false,
1264 /*release_after_client_shutdown=*/true);
1265 }
1266
1267 AWS_TEST_CASE(
1268 rrc_release_streaming_operation_after_client_shutdown,
1269 s_rrc_release_streaming_operation_after_client_shutdown_fn)
1270
1271 static int s_rrc_submit_request_operation_failure_by_timeout_fn(struct aws_allocator *allocator, void *ctx) {
1272 (void)ctx;
1273
1274 struct aws_mqtt_request_operation_response_path response_paths[] = {
1275 {
1276 .topic = aws_byte_cursor_from_c_str("response/filter/accepted"),
1277 .correlation_token_json_path = aws_byte_cursor_from_c_str("client_token"),
1278 },
1279 };
1280
1281 struct aws_mqtt_request_operation_options request = {
1282 .subscription_topic_filters = &s_response_filter_wildcard_cursor,
1283 .subscription_topic_filter_count = 1,
1284 .response_paths = response_paths,
1285 .response_path_count = AWS_ARRAY_SIZE(response_paths),
1286 .publish_topic = aws_byte_cursor_from_c_str("get/shadow"),
1287 .serialized_request = aws_byte_cursor_from_c_str("request1"),
1288 .correlation_token = aws_byte_cursor_from_c_str("MyRequest#1"),
1289 };
1290
1291 struct aws_mqtt_request_response_client_options rr_client_options = {
1292 .max_request_response_subscriptions = 2,
1293 .max_streaming_subscriptions = 1,
1294 .operation_timeout_seconds = 2,
1295 };
1296
1297 return s_do_rrc_single_request_operation_test_fn(
1298 allocator, &rr_client_options, &request, AWS_ERROR_MQTT_REQUEST_RESPONSE_TIMEOUT, NULL, NULL, false);
1299 }
1300
1301 AWS_TEST_CASE(rrc_submit_request_operation_failure_by_timeout, s_rrc_submit_request_operation_failure_by_timeout_fn)
1302
1303 static struct aws_mqtt_rr_client_operation *s_create_streaming_operation(
1304 struct aws_rr_client_test_fixture *fixture,
1305 struct aws_byte_cursor record_key,
1306 struct aws_byte_cursor topic_filter) {
1307 struct aws_rr_client_fixture_streaming_record *record = s_rrc_fixture_add_streaming_record(fixture, record_key);
1308
1309 struct aws_mqtt_streaming_operation_options streaming_options = {
1310 .topic_filter = topic_filter,
1311 };
1312 streaming_options.incoming_publish_callback = s_rrc_fixture_streaming_operation_incoming_publish_callback;
1313 streaming_options.subscription_status_callback = s_rrc_fixture_streaming_operation_subscription_status_callback;
1314 streaming_options.terminated_callback = s_rrc_fixture_streaming_operation_terminated_callback;
1315 streaming_options.user_data = record;
1316
1317 struct aws_mqtt_rr_client_operation *streaming_operation =
1318 aws_mqtt_request_response_client_create_streaming_operation(fixture->rr_client, &streaming_options);
1319 aws_mqtt_rr_client_operation_activate(streaming_operation);
1320
1321 return streaming_operation;
1322 }
1323
1324 static int s_rrc_publish_5(
1325 struct aws_mqtt5_client *client,
1326 struct aws_byte_cursor topic,
1327 struct aws_byte_cursor payload,
1328 const struct aws_byte_cursor *content_type,
1329 size_t user_property_count,
1330 const struct aws_mqtt5_user_property *user_properties,
1331 const uint32_t *message_expiry_interval_seconds) {
1332 struct aws_mqtt5_packet_publish_view publish_options = {
1333 .topic = topic,
1334 .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE,
1335 .payload = payload,
1336 .content_type = content_type,
1337 .user_property_count = user_property_count,
1338 .user_properties = user_properties,
1339 .message_expiry_interval_seconds = message_expiry_interval_seconds};
1340
1341 struct aws_mqtt5_publish_completion_options completion_options;
1342 AWS_ZERO_STRUCT(completion_options);
1343
1344 return aws_mqtt5_client_publish(client, &publish_options, &completion_options);
1345 }
1346
1347 static int s_rrc_publish_311(
1348 struct aws_mqtt_client_connection *connection,
1349 struct aws_byte_cursor topic,
1350 struct aws_byte_cursor payload) {
1351 uint16_t packet_id =
1352 aws_mqtt_client_connection_publish(connection, &topic, AWS_MQTT_QOS_AT_LEAST_ONCE, false, &payload, NULL, NULL);
1353 return packet_id > 0 ? AWS_OP_SUCCESS : AWS_OP_ERR;
1354 }
1355
1356 static int s_rrc_protocol_client_publish(
1357 struct aws_rr_client_test_fixture *fixture,
1358 struct aws_byte_cursor topic,
1359 struct aws_byte_cursor payload) {
1360
1361 if (fixture->test_protocol == RRCP_MQTT311) {
1362 return s_rrc_publish_311(fixture->client_test_fixture.mqtt311_test_fixture.mqtt_connection, topic, payload);
1363 } else {
1364 const struct aws_byte_cursor *content_type = NULL;
1365 size_t user_property_count = 0;
1366 const struct aws_mqtt5_user_property *user_properties = NULL;
1367 const uint32_t *message_expiry_interval_seconds = NULL;
1368 return s_rrc_publish_5(
1369 fixture->client_test_fixture.mqtt5_test_fixture.client,
1370 topic,
1371 payload,
1372 content_type,
1373 user_property_count,
1374 user_properties,
1375 message_expiry_interval_seconds);
1376 }
1377 }
1378
1379 static int s_rrc_protocol_client_publish_with_extra_fields(
1380 struct aws_rr_client_test_fixture *fixture,
1381 struct aws_byte_cursor topic,
1382 struct aws_byte_cursor payload,
1383 const struct aws_byte_cursor *content_type,
1384 size_t user_property_count,
1385 const struct aws_mqtt5_user_property *user_properties,
1386 const uint32_t *message_expiry_interval_seconds) {
1387
1388 if (fixture->test_protocol == RRCP_MQTT311) {
1389 return s_rrc_publish_311(fixture->client_test_fixture.mqtt311_test_fixture.mqtt_connection, topic, payload);
1390 } else {
1391 return s_rrc_publish_5(
1392 fixture->client_test_fixture.mqtt5_test_fixture.client,
1393 topic,
1394 payload,
1395 content_type,
1396 user_property_count,
1397 user_properties,
1398 message_expiry_interval_seconds);
1399 }
1400 }
1401
1402 typedef void(modify_fixture_options_fn)(
1403 struct aws_mqtt_request_response_client_options *fixture_options,
1404 struct mqtt5_client_test_options *client_test_options);
1405
1406 static int s_init_fixture_streaming_operation_success(
1407 struct aws_rr_client_test_fixture *fixture,
1408 struct mqtt5_client_test_options *client_test_options,
1409 struct aws_allocator *allocator,
1410 modify_fixture_options_fn *config_modifier,
1411 void *user_data,
1412 enum rr_test_client_protocol protocol) {
1413
1414 if (protocol == RRCP_MQTT5) {
1415 aws_mqtt5_client_test_init_default_options(client_test_options);
1416
1417 client_test_options->server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] =
1418 aws_mqtt5_server_send_suback_on_subscribe;
1419 client_test_options->server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] =
1420 aws_mqtt5_mock_server_handle_publish_puback_and_forward;
1421
1422 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options client_test_fixture_options = {
1423 .client_options = &client_test_options->client_options,
1424 .server_function_table = &client_test_options->server_function_table,
1425 .mock_server_user_data = user_data,
1426 };
1427
1428 struct aws_mqtt_request_response_client_options rr_client_options = {
1429 .max_request_response_subscriptions = 2,
1430 .max_streaming_subscriptions = 1,
1431 .operation_timeout_seconds = 2,
1432 };
1433
1434 if (config_modifier != NULL) {
1435 (*config_modifier)(&rr_client_options, client_test_options);
1436 }
1437
1438 ASSERT_SUCCESS(s_aws_rr_client_test_fixture_init_from_mqtt5(
1439 fixture, allocator, &rr_client_options, &client_test_fixture_options, NULL));
1440 } else if (protocol == RRCP_MQTT311) {
1441 struct aws_mqtt_request_response_client_options rr_client_options = {
1442 .max_request_response_subscriptions = 2,
1443 .max_streaming_subscriptions = 1,
1444 .operation_timeout_seconds = 2,
1445 };
1446 ASSERT_SUCCESS(s_aws_rr_client_test_fixture_init_from_mqtt311(fixture, allocator, &rr_client_options, NULL));
1447 } else {
1448 return AWS_OP_ERR;
1449 }
1450
1451 return AWS_OP_SUCCESS;
1452 }
1453
1454 /*
1455 * Minimal success test:
1456 *
1457 * Create a streaming operation, verify subscription established, inject several publishes to the operation's topic,
1458 * verify publishes received and routed through callbacks
1459 */
1460 static int s_rrc_streaming_operation_success_single_fn(struct aws_allocator *allocator, void *ctx) {
1461 (void)ctx;
1462
1463 aws_mqtt_library_init(allocator);
1464
1465 struct mqtt5_client_test_options client_test_options;
1466 struct aws_rr_client_test_fixture fixture;
1467 ASSERT_SUCCESS(
1468 s_init_fixture_streaming_operation_success(&fixture, &client_test_options, allocator, NULL, NULL, RRCP_MQTT5));
1469
1470 struct aws_byte_cursor record_key1 = aws_byte_cursor_from_c_str("key1");
1471 struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("topic/+");
1472 struct aws_byte_cursor topic_filter1 = aws_byte_cursor_from_c_str("topic/1");
1473 struct aws_mqtt_rr_client_operation *operation = s_create_streaming_operation(&fixture, record_key1, topic);
1474
1475 s_rrc_wait_for_n_streaming_subscription_events(&fixture, record_key1, 1);
1476
1477 struct aws_rr_client_fixture_streaming_record_subscription_event expected_events[] = {
1478 {
1479 .status = ARRSSET_SUBSCRIPTION_ESTABLISHED,
1480 .error_code = AWS_ERROR_SUCCESS,
1481 },
1482 };
1483 ASSERT_SUCCESS(s_rrc_verify_streaming_record_subscription_events(
1484 &fixture, record_key1, AWS_ARRAY_SIZE(expected_events), expected_events));
1485
1486 // two publishes on the mqtt client that get reflected into our subscription topic
1487 struct aws_byte_cursor payload1 = aws_byte_cursor_from_c_str("Payload1");
1488 struct aws_byte_cursor payload2 = aws_byte_cursor_from_c_str("Payload2");
1489 ASSERT_SUCCESS(s_rrc_protocol_client_publish(&fixture, topic_filter1, payload1));
1490 ASSERT_SUCCESS(s_rrc_protocol_client_publish(&fixture, topic_filter1, payload2));
1491
1492 s_rrc_wait_for_n_streaming_publishes(&fixture, record_key1, 2);
1493
1494 struct aws_rr_client_fixture_publish_message_view expected_publishes[] = {
1495 {
1496 payload1,
1497 topic_filter1,
1498 },
1499 {
1500 payload2,
1501 topic_filter1,
1502 },
1503 };
1504 ASSERT_SUCCESS(s_rrc_verify_streaming_publishes(
1505 &fixture, record_key1, AWS_ARRAY_SIZE(expected_publishes), expected_publishes));
1506
1507 aws_mqtt_rr_client_operation_release(operation);
1508
1509 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
1510
1511 aws_mqtt_library_clean_up();
1512
1513 return AWS_OP_SUCCESS;
1514 }
1515
1516 AWS_TEST_CASE(rrc_streaming_operation_success_single, s_rrc_streaming_operation_success_single_fn)
1517
1518 /*
1519 * Test that all required PUBLISH packet fields are passed to stream operation if they're present in the packet.
1520 */
1521 static int s_rrc_streaming_operation_success_capture_publish_packet_fn(
1522 struct aws_allocator *allocator,
1523 enum rr_test_client_protocol protocol) {
1524
1525 aws_mqtt_library_init(allocator);
1526
1527 struct mqtt5_client_test_options client_test_options;
1528 struct aws_rr_client_test_fixture fixture;
1529 ASSERT_SUCCESS(
1530 s_init_fixture_streaming_operation_success(&fixture, &client_test_options, allocator, NULL, NULL, protocol));
1531
1532 struct aws_byte_cursor record_key1 = aws_byte_cursor_from_c_str("key1");
1533 struct aws_byte_cursor topic_filter1 = aws_byte_cursor_from_c_str("topic/1");
1534 struct aws_mqtt_rr_client_operation *operation = s_create_streaming_operation(&fixture, record_key1, topic_filter1);
1535
1536 s_rrc_wait_for_n_streaming_subscription_events(&fixture, record_key1, 1);
1537
1538 struct aws_rr_client_fixture_streaming_record_subscription_event expected_events[] = {
1539 {
1540 .status = ARRSSET_SUBSCRIPTION_ESTABLISHED,
1541 .error_code = AWS_ERROR_SUCCESS,
1542 },
1543 };
1544 ASSERT_SUCCESS(s_rrc_verify_streaming_record_subscription_events(
1545 &fixture, record_key1, AWS_ARRAY_SIZE(expected_events), expected_events));
1546
1547 /* two publishes on the mqtt client that get reflected into our subscription topic */
1548 struct aws_byte_cursor payload1 = aws_byte_cursor_from_c_str("Payload1");
1549 struct aws_byte_cursor content_type1 = aws_byte_cursor_from_c_str("application/json");
1550 uint32_t message_expiry_interval_seconds1 = 10;
1551
1552 char user_prop1_name[] = "Property1";
1553 char user_prop1_value[] = "Value1";
1554 char user_prop2_name[] = "Property2";
1555 char user_prop2_value[] = "Value2";
1556 const struct aws_mqtt5_user_property user_properties1[] = {
1557 {
1558 .name =
1559 {
1560 .ptr = (uint8_t *)user_prop1_name,
1561 .len = AWS_ARRAY_SIZE(user_prop1_name) - 1,
1562 },
1563 .value =
1564 {
1565 .ptr = (uint8_t *)user_prop1_value,
1566 .len = AWS_ARRAY_SIZE(user_prop1_value) - 1,
1567 },
1568 },
1569 {
1570 .name =
1571 {
1572 .ptr = (uint8_t *)user_prop2_name,
1573 .len = AWS_ARRAY_SIZE(user_prop2_name) - 1,
1574 },
1575 .value =
1576 {
1577 .ptr = (uint8_t *)user_prop2_value,
1578 .len = AWS_ARRAY_SIZE(user_prop2_value) - 1,
1579 },
1580 },
1581 };
1582
1583 struct aws_byte_cursor payload2 = aws_byte_cursor_from_c_str("Payload2");
1584 struct aws_byte_cursor content_type2 = aws_byte_cursor_from_c_str("");
1585 uint32_t message_expiry_interval_seconds2 = 0;
1586 const struct aws_mqtt5_user_property user_properties2[] = {{
1587 .name =
1588 {
1589 .ptr = (uint8_t *)user_prop1_name,
1590 .len = AWS_ARRAY_SIZE(user_prop1_name) - 1,
1591 },
1592 .value =
1593 {
1594 .ptr = (uint8_t *)user_prop1_value,
1595 .len = AWS_ARRAY_SIZE(user_prop1_value) - 1,
1596 },
1597 }};
1598
1599 ASSERT_SUCCESS(s_rrc_protocol_client_publish_with_extra_fields(
1600 &fixture,
1601 topic_filter1,
1602 payload1,
1603 &content_type1,
1604 AWS_ARRAY_SIZE(user_properties1),
1605 user_properties1,
1606 &message_expiry_interval_seconds1));
1607 ASSERT_SUCCESS(s_rrc_protocol_client_publish_with_extra_fields(
1608 &fixture,
1609 topic_filter1,
1610 payload2,
1611 &content_type2,
1612 AWS_ARRAY_SIZE(user_properties2),
1613 user_properties2,
1614 &message_expiry_interval_seconds2));
1615
1616 s_rrc_wait_for_n_streaming_publishes(&fixture, record_key1, 2);
1617
1618 struct aws_rr_client_fixture_publish_message_view expected_publishes[] = {
1619 {
1620 payload1,
1621 topic_filter1,
1622 content_type1,
1623 AWS_ARRAY_SIZE(user_properties1),
1624 user_properties1,
1625 message_expiry_interval_seconds1,
1626 },
1627 {
1628 payload2,
1629 topic_filter1,
1630 content_type2,
1631 AWS_ARRAY_SIZE(user_properties2),
1632 user_properties2,
1633 message_expiry_interval_seconds2,
1634 },
1635 };
1636 ASSERT_SUCCESS(s_rrc_verify_streaming_publishes(
1637 &fixture, record_key1, AWS_ARRAY_SIZE(expected_publishes), expected_publishes));
1638
1639 aws_mqtt_rr_client_operation_release(operation);
1640
1641 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
1642
1643 aws_mqtt_library_clean_up();
1644
1645 return AWS_OP_SUCCESS;
1646 }
1647
1648 static int s_rrc_streaming_operation_success_capture_mqtt5_publish_packet_fn(
1649 struct aws_allocator *allocator,
1650 void *ctx) {
1651 (void)ctx;
1652 return s_rrc_streaming_operation_success_capture_publish_packet_fn(allocator, RRCP_MQTT5);
1653 }
1654
1655 AWS_TEST_CASE(
1656 rrc_streaming_operation_success_capture_mqtt5_publish_packet,
1657 s_rrc_streaming_operation_success_capture_mqtt5_publish_packet_fn)
1658
1659 static int s_rrc_streaming_operation_success_capture_mqtt311_publish_packet_fn(
1660 struct aws_allocator *allocator,
1661 void *ctx) {
1662 (void)ctx;
1663 return s_rrc_streaming_operation_success_capture_publish_packet_fn(allocator, RRCP_MQTT311);
1664 }
1665
1666 AWS_TEST_CASE(
1667 rrc_streaming_operation_success_capture_mqtt311_publish_packet,
1668 s_rrc_streaming_operation_success_capture_mqtt311_publish_packet_fn)
1669
1670 /*
1671 * Variant of the minimal success test where we create two operations on the same topic filter, verify they both
1672 * get subscriptions established and publishes, then close one, send another publish and verify only the still-open
1673 * operation received it.
1674 */
1675 static int s_rrc_streaming_operation_success_overlapping_fn(struct aws_allocator *allocator, void *ctx) {
1676 (void)ctx;
1677
1678 aws_mqtt_library_init(allocator);
1679
1680 struct mqtt5_client_test_options client_test_options;
1681 struct aws_rr_client_test_fixture fixture;
1682 ASSERT_SUCCESS(
1683 s_init_fixture_streaming_operation_success(&fixture, &client_test_options, allocator, NULL, NULL, RRCP_MQTT5));
1684
1685 struct aws_byte_cursor record_key1 = aws_byte_cursor_from_c_str("key1");
1686 struct aws_byte_cursor topic_filter1 = aws_byte_cursor_from_c_str("topic/1");
1687 struct aws_mqtt_rr_client_operation *operation1 =
1688 s_create_streaming_operation(&fixture, record_key1, topic_filter1);
1689
1690 struct aws_byte_cursor record_key2 = aws_byte_cursor_from_c_str("key2");
1691 struct aws_mqtt_rr_client_operation *operation2 =
1692 s_create_streaming_operation(&fixture, record_key2, topic_filter1);
1693
1694 s_rrc_wait_for_n_streaming_subscription_events(&fixture, record_key1, 1);
1695 s_rrc_wait_for_n_streaming_subscription_events(&fixture, record_key2, 1);
1696
1697 struct aws_rr_client_fixture_streaming_record_subscription_event expected_events[] = {
1698 {
1699 .status = ARRSSET_SUBSCRIPTION_ESTABLISHED,
1700 .error_code = AWS_ERROR_SUCCESS,
1701 },
1702 };
1703 ASSERT_SUCCESS(s_rrc_verify_streaming_record_subscription_events(
1704 &fixture, record_key1, AWS_ARRAY_SIZE(expected_events), expected_events));
1705 ASSERT_SUCCESS(s_rrc_verify_streaming_record_subscription_events(
1706 &fixture, record_key2, AWS_ARRAY_SIZE(expected_events), expected_events));
1707
1708 // two publishes on the mqtt client that get reflected into our subscription topic
1709 struct aws_byte_cursor payload1 = aws_byte_cursor_from_c_str("Payload1");
1710 struct aws_byte_cursor payload2 = aws_byte_cursor_from_c_str("Payload2");
1711 struct aws_byte_cursor payload3 = aws_byte_cursor_from_c_str("Payload3");
1712 ASSERT_SUCCESS(s_rrc_protocol_client_publish(&fixture, topic_filter1, payload1));
1713 ASSERT_SUCCESS(s_rrc_protocol_client_publish(&fixture, topic_filter1, payload2));
1714
1715 s_rrc_wait_for_n_streaming_publishes(&fixture, record_key1, 2);
1716 s_rrc_wait_for_n_streaming_publishes(&fixture, record_key2, 2);
1717
1718 struct aws_rr_client_fixture_publish_message_view expected_publishes[] = {
1719 {
1720 payload1,
1721 topic_filter1,
1722 },
1723 {
1724 payload2,
1725 topic_filter1,
1726 },
1727 {
1728 payload3,
1729 topic_filter1,
1730 },
1731 };
1732 ASSERT_SUCCESS(s_rrc_verify_streaming_publishes(&fixture, record_key1, 2, expected_publishes));
1733 ASSERT_SUCCESS(s_rrc_verify_streaming_publishes(&fixture, record_key2, 2, expected_publishes));
1734
1735 // close the first, wait for terminate
1736 aws_mqtt_rr_client_operation_release(operation1);
1737 s_rrc_wait_on_streaming_termination(&fixture, record_key1);
1738
1739 // publish again
1740 ASSERT_SUCCESS(s_rrc_protocol_client_publish(&fixture, topic_filter1, payload3));
1741
1742 // verify second operation got the new publish
1743 s_rrc_wait_for_n_streaming_publishes(&fixture, record_key2, 3);
1744 ASSERT_SUCCESS(s_rrc_verify_streaming_publishes(
1745 &fixture, record_key2, AWS_ARRAY_SIZE(expected_publishes), expected_publishes));
1746
1747 // verify first operation did not
1748 ASSERT_SUCCESS(s_rrc_verify_streaming_publishes(&fixture, record_key1, 2, expected_publishes));
1749
1750 aws_mqtt_rr_client_operation_release(operation2);
1751
1752 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
1753
1754 aws_mqtt_library_clean_up();
1755
1756 return AWS_OP_SUCCESS;
1757 }
1758
1759 AWS_TEST_CASE(rrc_streaming_operation_success_overlapping, s_rrc_streaming_operation_success_overlapping_fn)
1760
1761 /*
1762 * Variant of the simple test where we start the protocol client offline. In addition to the normal verifies, we also
1763 * verify nothing happens event wise until we start the client.
1764 */
1765 static int s_rrc_streaming_operation_success_starting_offline_fn(struct aws_allocator *allocator, void *ctx) {
1766 (void)ctx;
1767
1768 aws_mqtt_library_init(allocator);
1769
1770 struct mqtt5_client_test_options client_test_options;
1771 struct aws_rr_client_test_fixture fixture;
1772 ASSERT_SUCCESS(
1773 s_init_fixture_streaming_operation_success(&fixture, &client_test_options, allocator, NULL, NULL, RRCP_MQTT5));
1774
1775 /* stop and start the underlying client */
1776 aws_mqtt5_client_stop(fixture.client_test_fixture.mqtt5_test_fixture.client, NULL, NULL);
1777 aws_wait_for_stopped_lifecycle_event(&fixture.client_test_fixture.mqtt5_test_fixture);
1778
1779 struct aws_byte_cursor record_key1 = aws_byte_cursor_from_c_str("key1");
1780 struct aws_byte_cursor topic_filter1 = aws_byte_cursor_from_c_str("topic/1");
1781 struct aws_mqtt_rr_client_operation *operation = s_create_streaming_operation(&fixture, record_key1, topic_filter1);
1782
1783 /* wait a while (longer than request timeout) to see if anything happens */
1784 aws_thread_current_sleep(aws_timestamp_convert(3, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL));
1785
1786 /* fails the test if any subscription events have been emitted */
1787 ASSERT_SUCCESS(s_rrc_verify_streaming_record_subscription_events(&fixture, record_key1, 0, NULL));
1788
1789 /* start the protocol client */
1790 aws_mqtt5_client_start(fixture.client_test_fixture.mqtt5_test_fixture.client);
1791
1792 s_rrc_wait_for_n_streaming_subscription_events(&fixture, record_key1, 1);
1793
1794 struct aws_rr_client_fixture_streaming_record_subscription_event expected_events[] = {
1795 {
1796 .status = ARRSSET_SUBSCRIPTION_ESTABLISHED,
1797 .error_code = AWS_ERROR_SUCCESS,
1798 },
1799 };
1800 ASSERT_SUCCESS(s_rrc_verify_streaming_record_subscription_events(
1801 &fixture, record_key1, AWS_ARRAY_SIZE(expected_events), expected_events));
1802
1803 // two publishes on the mqtt client that get reflected into our subscription topic
1804 struct aws_byte_cursor payload1 = aws_byte_cursor_from_c_str("Payload1");
1805 struct aws_byte_cursor payload2 = aws_byte_cursor_from_c_str("Payload2");
1806 ASSERT_SUCCESS(s_rrc_protocol_client_publish(&fixture, topic_filter1, payload1));
1807 ASSERT_SUCCESS(s_rrc_protocol_client_publish(&fixture, topic_filter1, payload2));
1808
1809 s_rrc_wait_for_n_streaming_publishes(&fixture, record_key1, 2);
1810
1811 struct aws_rr_client_fixture_publish_message_view expected_publishes[] = {
1812 {
1813 payload1,
1814 topic_filter1,
1815 },
1816 {
1817 payload2,
1818 topic_filter1,
1819 },
1820 };
1821 ASSERT_SUCCESS(s_rrc_verify_streaming_publishes(
1822 &fixture, record_key1, AWS_ARRAY_SIZE(expected_publishes), expected_publishes));
1823
1824 aws_mqtt_rr_client_operation_release(operation);
1825
1826 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
1827
1828 aws_mqtt_library_clean_up();
1829
1830 return AWS_OP_SUCCESS;
1831 }
1832
1833 AWS_TEST_CASE(rrc_streaming_operation_success_starting_offline, s_rrc_streaming_operation_success_starting_offline_fn)
1834
1835 static void s_rrc_force_clean_session_config(
1836 struct aws_mqtt_request_response_client_options *fixture_options,
1837 struct mqtt5_client_test_options *client_test_options) {
1838 (void)fixture_options;
1839
1840 client_test_options->client_options.session_behavior = AWS_MQTT5_CSBT_CLEAN;
1841 }
1842
1843 /*
1844 * Verifies that a streaming operation recovers properly from a clean session resumption (by resubscribing), emitting
1845 * the proper subscription events, and receiving expected publishes once the subscription is re-established.
1846 */
1847 static int s_rrc_streaming_operation_clean_session_reestablish_subscription_fn(
1848 struct aws_allocator *allocator,
1849 void *ctx) {
1850 (void)ctx;
1851
1852 aws_mqtt_library_init(allocator);
1853
1854 struct mqtt5_client_test_options client_test_options;
1855 struct aws_rr_client_test_fixture fixture;
1856 ASSERT_SUCCESS(s_init_fixture_streaming_operation_success(
1857 &fixture, &client_test_options, allocator, s_rrc_force_clean_session_config, NULL, RRCP_MQTT5));
1858
1859 struct aws_byte_cursor record_key1 = aws_byte_cursor_from_c_str("key1");
1860 struct aws_byte_cursor topic_filter1 = aws_byte_cursor_from_c_str("topic/1");
1861 struct aws_mqtt_rr_client_operation *operation = s_create_streaming_operation(&fixture, record_key1, topic_filter1);
1862
1863 s_rrc_wait_for_n_streaming_subscription_events(&fixture, record_key1, 1);
1864
1865 struct aws_rr_client_fixture_streaming_record_subscription_event expected_events[] = {
1866 {
1867 .status = ARRSSET_SUBSCRIPTION_ESTABLISHED,
1868 .error_code = AWS_ERROR_SUCCESS,
1869 },
1870 {
1871 .status = ARRSSET_SUBSCRIPTION_LOST,
1872 .error_code = AWS_ERROR_SUCCESS,
1873 },
1874 {
1875 .status = ARRSSET_SUBSCRIPTION_ESTABLISHED,
1876 .error_code = AWS_ERROR_SUCCESS,
1877 },
1878 };
1879 ASSERT_SUCCESS(s_rrc_verify_streaming_record_subscription_events(&fixture, record_key1, 1, expected_events));
1880
1881 // two publishes on the mqtt client that get reflected into our subscription topic
1882 struct aws_byte_cursor payload1 = aws_byte_cursor_from_c_str("Payload1");
1883 struct aws_byte_cursor payload2 = aws_byte_cursor_from_c_str("Payload2");
1884 ASSERT_SUCCESS(s_rrc_protocol_client_publish(&fixture, topic_filter1, payload1));
1885
1886 s_rrc_wait_for_n_streaming_publishes(&fixture, record_key1, 1);
1887
1888 struct aws_rr_client_fixture_publish_message_view expected_publishes[] = {
1889 {
1890 payload1,
1891 topic_filter1,
1892 },
1893 {
1894 payload2,
1895 topic_filter1,
1896 },
1897 };
1898 ASSERT_SUCCESS(s_rrc_verify_streaming_publishes(&fixture, record_key1, 1, expected_publishes));
1899
1900 /* stop and start the underlying client, this will force a resubscribe since it's a clean session */
1901 aws_mqtt5_client_stop(fixture.client_test_fixture.mqtt5_test_fixture.client, NULL, NULL);
1902 aws_wait_for_stopped_lifecycle_event(&fixture.client_test_fixture.mqtt5_test_fixture);
1903
1904 aws_mqtt5_client_start(fixture.client_test_fixture.mqtt5_test_fixture.client);
1905
1906 s_rrc_wait_for_n_streaming_subscription_events(&fixture, record_key1, 3);
1907 ASSERT_SUCCESS(s_rrc_verify_streaming_record_subscription_events(&fixture, record_key1, 3, expected_events));
1908
1909 ASSERT_SUCCESS(s_rrc_protocol_client_publish(&fixture, topic_filter1, payload2));
1910
1911 s_rrc_wait_for_n_streaming_publishes(&fixture, record_key1, 2);
1912 ASSERT_SUCCESS(s_rrc_verify_streaming_publishes(&fixture, record_key1, 2, expected_publishes));
1913
1914 aws_mqtt_rr_client_operation_release(operation);
1915
1916 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
1917
1918 aws_mqtt_library_clean_up();
1919
1920 return AWS_OP_SUCCESS;
1921 }
1922
1923 AWS_TEST_CASE(
1924 rrc_streaming_operation_clean_session_reestablish_subscription,
1925 s_rrc_streaming_operation_clean_session_reestablish_subscription_fn)
1926
1927 static void s_rrc_force_resume_session_config(
1928 struct aws_mqtt_request_response_client_options *fixture_options,
1929 struct mqtt5_client_test_options *client_test_options) {
1930 (void)fixture_options;
1931
1932 client_test_options->server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] =
1933 aws_mqtt5_mock_server_handle_connect_honor_session_unconditional;
1934 client_test_options->client_options.session_behavior = AWS_MQTT5_CSBT_REJOIN_ALWAYS;
1935 }
1936
1937 /*
1938 * Variant of the clean session test where instead we always resume a session. Verify we don't get subscription
1939 * lost/established events afterwards and can still receive messages.
1940 */
1941 static int s_rrc_streaming_operation_resume_session_fn(struct aws_allocator *allocator, void *ctx) {
1942 (void)ctx;
1943
1944 aws_mqtt_library_init(allocator);
1945
1946 struct mqtt5_client_test_options client_test_options;
1947 struct aws_rr_client_test_fixture fixture;
1948 ASSERT_SUCCESS(s_init_fixture_streaming_operation_success(
1949 &fixture, &client_test_options, allocator, s_rrc_force_resume_session_config, NULL, RRCP_MQTT5));
1950
1951 struct aws_byte_cursor record_key1 = aws_byte_cursor_from_c_str("key1");
1952 struct aws_byte_cursor topic_filter1 = aws_byte_cursor_from_c_str("topic/1");
1953 struct aws_mqtt_rr_client_operation *operation = s_create_streaming_operation(&fixture, record_key1, topic_filter1);
1954
1955 s_rrc_wait_for_n_streaming_subscription_events(&fixture, record_key1, 1);
1956
1957 struct aws_rr_client_fixture_streaming_record_subscription_event expected_events[] = {
1958 {
1959 .status = ARRSSET_SUBSCRIPTION_ESTABLISHED,
1960 .error_code = AWS_ERROR_SUCCESS,
1961 },
1962 };
1963 ASSERT_SUCCESS(s_rrc_verify_streaming_record_subscription_events(&fixture, record_key1, 1, expected_events));
1964
1965 // two publishes on the mqtt client that get reflected into our subscription topic
1966 struct aws_byte_cursor payload1 = aws_byte_cursor_from_c_str("Payload1");
1967 struct aws_byte_cursor payload2 = aws_byte_cursor_from_c_str("Payload2");
1968 ASSERT_SUCCESS(s_rrc_protocol_client_publish(&fixture, topic_filter1, payload1));
1969
1970 s_rrc_wait_for_n_streaming_publishes(&fixture, record_key1, 1);
1971
1972 struct aws_rr_client_fixture_publish_message_view expected_publishes[] = {
1973 {
1974 payload1,
1975 topic_filter1,
1976 },
1977 {
1978 payload2,
1979 topic_filter1,
1980 },
1981 };
1982 ASSERT_SUCCESS(s_rrc_verify_streaming_publishes(&fixture, record_key1, 1, expected_publishes));
1983
1984 /* stop and start the underlying client */
1985 aws_mqtt5_client_stop(fixture.client_test_fixture.mqtt5_test_fixture.client, NULL, NULL);
1986 aws_wait_for_stopped_lifecycle_event(&fixture.client_test_fixture.mqtt5_test_fixture);
1987
1988 aws_mqtt5_client_start(fixture.client_test_fixture.mqtt5_test_fixture.client);
1989
1990 ASSERT_SUCCESS(s_rrc_protocol_client_publish(&fixture, topic_filter1, payload2));
1991
1992 s_rrc_wait_for_n_streaming_publishes(&fixture, record_key1, 2);
1993 ASSERT_SUCCESS(s_rrc_verify_streaming_publishes(&fixture, record_key1, 2, expected_publishes));
1994
1995 /* check events after the publish has completed, that shows nothing happened subscription-wise */
1996 s_rrc_wait_for_n_streaming_subscription_events(&fixture, record_key1, 1);
1997 ASSERT_SUCCESS(s_rrc_verify_streaming_record_subscription_events(&fixture, record_key1, 1, expected_events));
1998
1999 aws_mqtt_rr_client_operation_release(operation);
2000
2001 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
2002
2003 aws_mqtt_library_clean_up();
2004
2005 return AWS_OP_SUCCESS;
2006 }
2007
2008 AWS_TEST_CASE(rrc_streaming_operation_resume_session, s_rrc_streaming_operation_resume_session_fn)
2009
2010 struct rrc_subscribe_handler_context {
2011 struct aws_rr_client_test_fixture *fixture;
2012
2013 size_t subscribes_received;
2014 };
2015
2016 static enum aws_mqtt5_suback_reason_code s_rrc_success_suback_rcs[] = {
2017 AWS_MQTT5_SARC_GRANTED_QOS_1,
2018 };
2019
2020 int s_handle_subscribe_with_initial_timeout(
2021 void *packet,
2022 struct aws_mqtt5_server_mock_connection_context *connection,
2023 void *user_data) {
2024 (void)packet;
2025
2026 struct rrc_subscribe_handler_context *context = user_data;
2027
2028 bool should_respond = false;
2029 aws_mutex_lock(&context->fixture->lock);
2030 should_respond = context->subscribes_received > 0;
2031 ++context->subscribes_received;
2032 aws_mutex_unlock(&context->fixture->lock);
2033
2034 if (!should_respond) {
2035 return AWS_OP_SUCCESS;
2036 }
2037
2038 struct aws_mqtt5_packet_subscribe_view *subscribe_packet = packet;
2039
2040 struct aws_mqtt5_packet_suback_view suback_view = {
2041 .packet_id = subscribe_packet->packet_id,
2042 .reason_code_count = 1,
2043 .reason_codes = s_rrc_success_suback_rcs,
2044 };
2045
2046 return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_SUBACK, &suback_view);
2047 }
2048
2049 static void s_rrc_initial_subscribe_timeout_config(
2050 struct aws_mqtt_request_response_client_options *fixture_options,
2051 struct mqtt5_client_test_options *client_test_options) {
2052 (void)fixture_options;
2053
2054 client_test_options->server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] =
2055 s_handle_subscribe_with_initial_timeout;
2056 }
2057
2058 /*
2059 * Variant of the basic success test where the first subscribe is ignored, causing it to timeout. Verify the
2060 * client sends a second subscribe (which succeeds) after which everything is fine.
2061 */
2062 static int s_rrc_streaming_operation_first_subscribe_times_out_resub_succeeds_fn(
2063 struct aws_allocator *allocator,
2064 void *ctx) {
2065 (void)ctx;
2066
2067 aws_mqtt_library_init(allocator);
2068
2069 struct mqtt5_client_test_options client_test_options;
2070 struct aws_rr_client_test_fixture fixture;
2071
2072 struct rrc_subscribe_handler_context subscribe_context = {
2073 .fixture = &fixture,
2074 .subscribes_received = 0,
2075 };
2076 ASSERT_SUCCESS(s_init_fixture_streaming_operation_success(
2077 &fixture,
2078 &client_test_options,
2079 allocator,
2080 s_rrc_initial_subscribe_timeout_config,
2081 &subscribe_context,
2082 RRCP_MQTT5));
2083
2084 struct aws_byte_cursor record_key1 = aws_byte_cursor_from_c_str("key1");
2085 struct aws_byte_cursor topic_filter1 = aws_byte_cursor_from_c_str("topic/1");
2086 struct aws_mqtt_rr_client_operation *operation = s_create_streaming_operation(&fixture, record_key1, topic_filter1);
2087
2088 s_rrc_wait_for_n_streaming_subscription_events(&fixture, record_key1, 1);
2089
2090 struct aws_rr_client_fixture_streaming_record_subscription_event expected_events[] = {
2091 {
2092 .status = ARRSSET_SUBSCRIPTION_ESTABLISHED,
2093 .error_code = AWS_ERROR_SUCCESS,
2094 },
2095 };
2096 ASSERT_SUCCESS(s_rrc_verify_streaming_record_subscription_events(
2097 &fixture, record_key1, AWS_ARRAY_SIZE(expected_events), expected_events));
2098
2099 // verify we ignored the first subscribe, triggering a second
2100 aws_mutex_lock(&fixture.lock);
2101 ASSERT_INT_EQUALS(2, subscribe_context.subscribes_received);
2102 aws_mutex_unlock(&fixture.lock);
2103
2104 // two publishes on the mqtt client that get reflected into our subscription topic
2105 struct aws_byte_cursor payload1 = aws_byte_cursor_from_c_str("Payload1");
2106 struct aws_byte_cursor payload2 = aws_byte_cursor_from_c_str("Payload2");
2107 ASSERT_SUCCESS(s_rrc_protocol_client_publish(&fixture, topic_filter1, payload1));
2108 ASSERT_SUCCESS(s_rrc_protocol_client_publish(&fixture, topic_filter1, payload2));
2109
2110 s_rrc_wait_for_n_streaming_publishes(&fixture, record_key1, 2);
2111
2112 struct aws_rr_client_fixture_publish_message_view expected_publishes[] = {
2113 {
2114 payload1,
2115 topic_filter1,
2116 },
2117 {
2118 payload2,
2119 topic_filter1,
2120 },
2121 };
2122 ASSERT_SUCCESS(s_rrc_verify_streaming_publishes(
2123 &fixture, record_key1, AWS_ARRAY_SIZE(expected_publishes), expected_publishes));
2124
2125 aws_mqtt_rr_client_operation_release(operation);
2126
2127 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
2128
2129 aws_mqtt_library_clean_up();
2130
2131 return AWS_OP_SUCCESS;
2132 }
2133
2134 AWS_TEST_CASE(
2135 rrc_streaming_operation_first_subscribe_times_out_resub_succeeds,
2136 s_rrc_streaming_operation_first_subscribe_times_out_resub_succeeds_fn)
2137
2138 static enum aws_mqtt5_suback_reason_code s_rrc_retryable_suback_rcs[] = {
2139 AWS_MQTT5_SARC_UNSPECIFIED_ERROR,
2140 };
2141
2142 int s_handle_subscribe_with_initial_retryable_failure(
2143 void *packet,
2144 struct aws_mqtt5_server_mock_connection_context *connection,
2145 void *user_data) {
2146 (void)packet;
2147
2148 struct rrc_subscribe_handler_context *context = user_data;
2149
2150 bool should_succeed = false;
2151 aws_mutex_lock(&context->fixture->lock);
2152 should_succeed = context->subscribes_received > 0;
2153 ++context->subscribes_received;
2154 aws_mutex_unlock(&context->fixture->lock);
2155
2156 struct aws_mqtt5_packet_subscribe_view *subscribe_packet = packet;
2157
2158 struct aws_mqtt5_packet_suback_view suback_view = {
2159 .packet_id = subscribe_packet->packet_id,
2160 .reason_code_count = 1,
2161 };
2162
2163 if (should_succeed) {
2164 suback_view.reason_codes = s_rrc_success_suback_rcs;
2165 } else {
2166 suback_view.reason_codes = s_rrc_retryable_suback_rcs;
2167 }
2168
2169 return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_SUBACK, &suback_view);
2170 }
2171
2172 static void s_rrc_initial_subscribe_retryable_failure_config(
2173 struct aws_mqtt_request_response_client_options *fixture_options,
2174 struct mqtt5_client_test_options *client_test_options) {
2175 (void)fixture_options;
2176
2177 client_test_options->server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] =
2178 s_handle_subscribe_with_initial_retryable_failure;
2179 }
2180
2181 /*
2182 * Variant of the basic success test where the first subscribe triggers a retryable suback failure. Verify the
2183 * client sends a second subscribe (which succeeds) after which everything is fine.
2184 */
2185 static int s_rrc_streaming_operation_first_subscribe_retryable_failure_resub_succeeds_fn(
2186 struct aws_allocator *allocator,
2187 void *ctx) {
2188 (void)ctx;
2189
2190 aws_mqtt_library_init(allocator);
2191
2192 struct mqtt5_client_test_options client_test_options;
2193 struct aws_rr_client_test_fixture fixture;
2194
2195 struct rrc_subscribe_handler_context subscribe_context = {
2196 .fixture = &fixture,
2197 .subscribes_received = 0,
2198 };
2199 ASSERT_SUCCESS(s_init_fixture_streaming_operation_success(
2200 &fixture,
2201 &client_test_options,
2202 allocator,
2203 s_rrc_initial_subscribe_retryable_failure_config,
2204 &subscribe_context,
2205 RRCP_MQTT5));
2206
2207 struct aws_byte_cursor record_key1 = aws_byte_cursor_from_c_str("key1");
2208 struct aws_byte_cursor topic_filter1 = aws_byte_cursor_from_c_str("topic/1");
2209 struct aws_mqtt_rr_client_operation *operation = s_create_streaming_operation(&fixture, record_key1, topic_filter1);
2210
2211 s_rrc_wait_for_n_streaming_subscription_events(&fixture, record_key1, 1);
2212
2213 struct aws_rr_client_fixture_streaming_record_subscription_event expected_events[] = {
2214 {
2215 .status = ARRSSET_SUBSCRIPTION_ESTABLISHED,
2216 .error_code = AWS_ERROR_SUCCESS,
2217 },
2218 };
2219 ASSERT_SUCCESS(s_rrc_verify_streaming_record_subscription_events(
2220 &fixture, record_key1, AWS_ARRAY_SIZE(expected_events), expected_events));
2221
2222 // verify we ignored the first subscribe, triggering a second
2223 aws_mutex_lock(&fixture.lock);
2224 ASSERT_INT_EQUALS(2, subscribe_context.subscribes_received);
2225 aws_mutex_unlock(&fixture.lock);
2226
2227 // two publishes on the mqtt client that get reflected into our subscription topic
2228 struct aws_byte_cursor payload1 = aws_byte_cursor_from_c_str("Payload1");
2229 struct aws_byte_cursor payload2 = aws_byte_cursor_from_c_str("Payload2");
2230 ASSERT_SUCCESS(s_rrc_protocol_client_publish(&fixture, topic_filter1, payload1));
2231 ASSERT_SUCCESS(s_rrc_protocol_client_publish(&fixture, topic_filter1, payload2));
2232
2233 s_rrc_wait_for_n_streaming_publishes(&fixture, record_key1, 2);
2234
2235 struct aws_rr_client_fixture_publish_message_view expected_publishes[] = {
2236 {
2237 payload1,
2238 topic_filter1,
2239 },
2240 {
2241 payload2,
2242 topic_filter1,
2243 },
2244 };
2245 ASSERT_SUCCESS(s_rrc_verify_streaming_publishes(
2246 &fixture, record_key1, AWS_ARRAY_SIZE(expected_publishes), expected_publishes));
2247
2248 aws_mqtt_rr_client_operation_release(operation);
2249
2250 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
2251
2252 aws_mqtt_library_clean_up();
2253
2254 return AWS_OP_SUCCESS;
2255 }
2256
2257 AWS_TEST_CASE(
2258 rrc_streaming_operation_first_subscribe_retryable_failure_resub_succeeds,
2259 s_rrc_streaming_operation_first_subscribe_retryable_failure_resub_succeeds_fn)
2260
2261 static enum aws_mqtt5_suback_reason_code s_rrc_unretryable_suback_rcs[] = {
2262 AWS_MQTT5_SARC_NOT_AUTHORIZED,
2263 };
2264
2265 int s_handle_subscribe_with_terminal_failure(
2266 void *packet,
2267 struct aws_mqtt5_server_mock_connection_context *connection,
2268 void *user_data) {
2269 (void)packet;
2270
2271 struct rrc_subscribe_handler_context *context = user_data;
2272
2273 aws_mutex_lock(&context->fixture->lock);
2274 ++context->subscribes_received;
2275 aws_mutex_unlock(&context->fixture->lock);
2276
2277 struct aws_mqtt5_packet_subscribe_view *subscribe_packet = packet;
2278
2279 struct aws_mqtt5_packet_suback_view suback_view = {
2280 .packet_id = subscribe_packet->packet_id,
2281 .reason_code_count = 1,
2282 .reason_codes = s_rrc_unretryable_suback_rcs,
2283 };
2284
2285 return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_SUBACK, &suback_view);
2286 }
2287
2288 static void s_rrc_subscribe_terminal_failure_config(
2289 struct aws_mqtt_request_response_client_options *fixture_options,
2290 struct mqtt5_client_test_options *client_test_options) {
2291 (void)fixture_options;
2292
2293 client_test_options->server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] =
2294 s_handle_subscribe_with_terminal_failure;
2295 }
2296
2297 /*
2298 * Failure variant where the subscribe triggers a non-retryable suback failure. Verify the
2299 * operation gets halted.
2300 */
2301 static int s_rrc_streaming_operation_subscribe_unretryable_failure_fn(struct aws_allocator *allocator, void *ctx) {
2302 (void)ctx;
2303
2304 aws_mqtt_library_init(allocator);
2305
2306 struct mqtt5_client_test_options client_test_options;
2307 struct aws_rr_client_test_fixture fixture;
2308
2309 struct rrc_subscribe_handler_context subscribe_context = {
2310 .fixture = &fixture,
2311 .subscribes_received = 0,
2312 };
2313 ASSERT_SUCCESS(s_init_fixture_streaming_operation_success(
2314 &fixture,
2315 &client_test_options,
2316 allocator,
2317 s_rrc_subscribe_terminal_failure_config,
2318 &subscribe_context,
2319 RRCP_MQTT5));
2320
2321 struct aws_byte_cursor record_key1 = aws_byte_cursor_from_c_str("key1");
2322 struct aws_byte_cursor topic_filter1 = aws_byte_cursor_from_c_str("topic/1");
2323 struct aws_mqtt_rr_client_operation *operation = s_create_streaming_operation(&fixture, record_key1, topic_filter1);
2324
2325 // wait an extra amount just for fun
2326 aws_thread_current_sleep(aws_timestamp_convert(2, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL));
2327
2328 s_rrc_wait_for_n_streaming_subscription_events(&fixture, record_key1, 1);
2329
2330 struct aws_rr_client_fixture_streaming_record_subscription_event expected_events[] = {
2331 {
2332 .status = ARRSSET_SUBSCRIPTION_HALTED,
2333 .error_code = AWS_ERROR_MQTT_REQUEST_RESPONSE_SUBSCRIBE_FAILURE,
2334 },
2335 };
2336 ASSERT_SUCCESS(s_rrc_verify_streaming_record_subscription_events(
2337 &fixture, record_key1, AWS_ARRAY_SIZE(expected_events), expected_events));
2338
2339 aws_mutex_lock(&fixture.lock);
2340 ASSERT_INT_EQUALS(1, subscribe_context.subscribes_received);
2341 aws_mutex_unlock(&fixture.lock);
2342
2343 aws_mqtt_rr_client_operation_release(operation);
2344
2345 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
2346
2347 aws_mqtt_library_clean_up();
2348
2349 return AWS_OP_SUCCESS;
2350 }
2351
2352 AWS_TEST_CASE(
2353 rrc_streaming_operation_subscribe_unretryable_failure,
2354 s_rrc_streaming_operation_subscribe_unretryable_failure_fn)
2355
2356 static void s_rrc_unsubscribe_success_config(
2357 struct aws_mqtt_request_response_client_options *fixture_options,
2358 struct mqtt5_client_test_options *client_test_options) {
2359 (void)fixture_options;
2360
2361 client_test_options->server_function_table.packet_handlers[AWS_MQTT5_PT_UNSUBSCRIBE] =
2362 aws_mqtt5_mock_server_handle_unsubscribe_unsuback_success;
2363 }
2364
2365 /*
2366 * Multi-operation variant where we exceed the streaming subscription budget, release everything and then verify
2367 * we can successfully establish a new streaming operation after everything cleans up.
2368 */
2369 static int s_rrc_streaming_operation_failure_exceeds_subscription_budget_fn(
2370 struct aws_allocator *allocator,
2371 void *ctx) {
2372 (void)ctx;
2373
2374 aws_mqtt_library_init(allocator);
2375
2376 struct mqtt5_client_test_options client_test_options;
2377 struct aws_rr_client_test_fixture fixture;
2378 ASSERT_SUCCESS(s_init_fixture_streaming_operation_success(
2379 &fixture, &client_test_options, allocator, s_rrc_unsubscribe_success_config, NULL, RRCP_MQTT5));
2380
2381 struct aws_byte_cursor record_key1 = aws_byte_cursor_from_c_str("key1");
2382 struct aws_byte_cursor topic_filter1 = aws_byte_cursor_from_c_str("topic/1/+");
2383 struct aws_byte_cursor topic1 = aws_byte_cursor_from_c_str("topic/1/abc");
2384 struct aws_mqtt_rr_client_operation *operation1 =
2385 s_create_streaming_operation(&fixture, record_key1, topic_filter1);
2386
2387 struct aws_byte_cursor record_key2 = aws_byte_cursor_from_c_str("key2");
2388 struct aws_byte_cursor topic_filter2 = aws_byte_cursor_from_c_str("/topic/2/+");
2389 struct aws_byte_cursor topic2 = aws_byte_cursor_from_c_str("/topic/2/def");
2390 struct aws_mqtt_rr_client_operation *operation2 =
2391 s_create_streaming_operation(&fixture, record_key2, topic_filter2);
2392
2393 s_rrc_wait_for_n_streaming_subscription_events(&fixture, record_key1, 1);
2394 s_rrc_wait_for_n_streaming_subscription_events(&fixture, record_key2, 1);
2395
2396 struct aws_rr_client_fixture_streaming_record_subscription_event expected_success_events[] = {
2397 {
2398 .status = ARRSSET_SUBSCRIPTION_ESTABLISHED,
2399 .error_code = AWS_ERROR_SUCCESS,
2400 },
2401 };
2402 ASSERT_SUCCESS(s_rrc_verify_streaming_record_subscription_events(
2403 &fixture, record_key1, AWS_ARRAY_SIZE(expected_success_events), expected_success_events));
2404
2405 struct aws_rr_client_fixture_streaming_record_subscription_event expected_failure_events[] = {
2406 {
2407 .status = ARRSSET_SUBSCRIPTION_HALTED,
2408 .error_code = AWS_ERROR_MQTT_REQUEST_RESPONSE_NO_SUBSCRIPTION_CAPACITY,
2409 },
2410 };
2411 ASSERT_SUCCESS(s_rrc_verify_streaming_record_subscription_events(
2412 &fixture, record_key2, AWS_ARRAY_SIZE(expected_success_events), expected_failure_events));
2413
2414 // two publishes on the mqtt client that get reflected into our subscription topic1
2415 struct aws_byte_cursor payload1 = aws_byte_cursor_from_c_str("Payload1");
2416 struct aws_byte_cursor payload2 = aws_byte_cursor_from_c_str("Payload2");
2417 ASSERT_SUCCESS(s_rrc_protocol_client_publish(&fixture, topic1, payload1));
2418 ASSERT_SUCCESS(s_rrc_protocol_client_publish(&fixture, topic1, payload2));
2419
2420 s_rrc_wait_for_n_streaming_publishes(&fixture, record_key1, 2);
2421
2422 struct aws_rr_client_fixture_publish_message_view expected_publishes[] = {
2423 {
2424 payload1,
2425 topic1,
2426 },
2427 {
2428 payload2,
2429 topic1,
2430 },
2431 };
2432 ASSERT_SUCCESS(s_rrc_verify_streaming_publishes(&fixture, record_key1, 2, expected_publishes));
2433
2434 // close the first, wait for terminate
2435 aws_mqtt_rr_client_operation_release(operation1);
2436 s_rrc_wait_on_streaming_termination(&fixture, record_key1);
2437
2438 // close the second, wait for terminate
2439 aws_mqtt_rr_client_operation_release(operation2);
2440 s_rrc_wait_on_streaming_termination(&fixture, record_key2);
2441
2442 // let the unsubscribe resolve
2443 aws_thread_current_sleep(aws_timestamp_convert(1, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL));
2444
2445 // make a third using topic filter 2
2446 struct aws_byte_cursor record_key3 = aws_byte_cursor_from_c_str("key3");
2447 struct aws_mqtt_rr_client_operation *operation3 = s_create_streaming_operation(&fixture, record_key3, topic2);
2448
2449 s_rrc_wait_for_n_streaming_subscription_events(&fixture, record_key3, 1);
2450 ASSERT_SUCCESS(s_rrc_verify_streaming_record_subscription_events(
2451 &fixture, record_key3, AWS_ARRAY_SIZE(expected_success_events), expected_success_events));
2452
2453 // publish again
2454 struct aws_byte_cursor payload3 = aws_byte_cursor_from_c_str("payload3");
2455 ASSERT_SUCCESS(s_rrc_protocol_client_publish(&fixture, topic2, payload3));
2456
2457 // verify third operation got the new publish
2458 s_rrc_wait_for_n_streaming_publishes(&fixture, record_key3, 1);
2459
2460 struct aws_rr_client_fixture_publish_message_view third_expected_publishes[] = {
2461 {
2462 payload3,
2463 topic2,
2464 },
2465 };
2466 ASSERT_SUCCESS(s_rrc_verify_streaming_publishes(
2467 &fixture, record_key3, AWS_ARRAY_SIZE(third_expected_publishes), third_expected_publishes));
2468
2469 aws_mqtt_rr_client_operation_release(operation3);
2470
2471 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
2472
2473 aws_mqtt_library_clean_up();
2474
2475 return AWS_OP_SUCCESS;
2476 }
2477
2478 AWS_TEST_CASE(
2479 rrc_streaming_operation_failure_exceeds_subscription_budget,
2480 s_rrc_streaming_operation_failure_exceeds_subscription_budget_fn)
2481
2482 static int s_submit_request_operation_from_prefix(
2483 struct aws_rr_client_test_fixture *fixture,
2484 struct aws_byte_cursor record_key,
2485 struct aws_byte_cursor prefix) {
2486 char accepted_path[128];
2487 char rejected_path[128];
2488 char subscription_topic_filter[128];
2489 char publish_topic[128];
2490
2491 snprintf(accepted_path, AWS_ARRAY_SIZE(accepted_path), PRInSTR "/accepted", AWS_BYTE_CURSOR_PRI(prefix));
2492 snprintf(rejected_path, AWS_ARRAY_SIZE(rejected_path), PRInSTR "/rejected", AWS_BYTE_CURSOR_PRI(prefix));
2493 snprintf(
2494 subscription_topic_filter,
2495 AWS_ARRAY_SIZE(subscription_topic_filter),
2496 PRInSTR "/+",
2497 AWS_BYTE_CURSOR_PRI(prefix));
2498 snprintf(publish_topic, AWS_ARRAY_SIZE(publish_topic), PRInSTR "/get", AWS_BYTE_CURSOR_PRI(prefix));
2499
2500 struct aws_byte_cursor subscription_topic_filter_cursor = aws_byte_cursor_from_c_str(subscription_topic_filter);
2501
2502 char correlation_token[128];
2503 struct aws_byte_buf correlation_token_buf =
2504 aws_byte_buf_from_empty_array(correlation_token, AWS_ARRAY_SIZE(correlation_token));
2505
2506 struct aws_uuid uuid;
2507 aws_uuid_init(&uuid);
2508 aws_uuid_to_str(&uuid, &correlation_token_buf);
2509
2510 struct aws_mqtt_request_operation_response_path response_paths[] = {
2511 {
2512 .topic = aws_byte_cursor_from_c_str(accepted_path),
2513 .correlation_token_json_path = aws_byte_cursor_from_c_str("client_token"),
2514 },
2515 {
2516 .topic = aws_byte_cursor_from_c_str(rejected_path),
2517 .correlation_token_json_path = aws_byte_cursor_from_c_str("client_token"),
2518 },
2519 };
2520
2521 struct aws_rr_client_fixture_request_response_record *record =
2522 s_rrc_fixture_add_request_record(fixture, record_key);
2523
2524 struct aws_mqtt_request_operation_options request = {
2525 .subscription_topic_filters = &subscription_topic_filter_cursor,
2526 .subscription_topic_filter_count = 1,
2527 .response_paths = response_paths,
2528 .response_path_count = AWS_ARRAY_SIZE(response_paths),
2529 .publish_topic = aws_byte_cursor_from_c_str(publish_topic),
2530 .serialized_request = aws_byte_cursor_from_c_str("{}"),
2531 .correlation_token = aws_byte_cursor_from_buf(&correlation_token_buf),
2532 .completion_callback = s_rrc_fixture_request_completion_callback,
2533 .user_data = record,
2534 };
2535
2536 return aws_mqtt_request_response_client_submit_request(fixture->rr_client, &request);
2537 }
2538
2539 /*
2540 * Configure server to only respond to subscribes that match a streaming filter. Submit a couple of
2541 * request-response operations ahead of a streaming operation. Verify they both time out and that the streaming
2542 * operation successfully subscribes and receives publishes.
2543 */
2544 static int s_rrc_streaming_operation_success_delayed_by_request_operations_fn(
2545 struct aws_allocator *allocator,
2546 void *ctx) {
2547 (void)ctx;
2548
2549 aws_mqtt_library_init(allocator);
2550
2551 struct mqtt5_client_test_options client_test_options;
2552 struct aws_rr_client_test_fixture fixture;
2553 ASSERT_SUCCESS(s_init_fixture_streaming_operation_success(
2554 &fixture, &client_test_options, allocator, s_rrc_unsubscribe_success_config, NULL, RRCP_MQTT5));
2555
2556 struct aws_byte_cursor request_key1 = aws_byte_cursor_from_c_str("requestkey1");
2557 struct aws_byte_cursor request_key2 = aws_byte_cursor_from_c_str("requestkey2");
2558
2559 ASSERT_SUCCESS(s_submit_request_operation_from_prefix(&fixture, request_key1, request_key1));
2560 ASSERT_SUCCESS(s_submit_request_operation_from_prefix(&fixture, request_key2, request_key2));
2561
2562 struct aws_byte_cursor record_key1 = aws_byte_cursor_from_c_str("key1");
2563 struct aws_byte_cursor topic_filter1 = aws_byte_cursor_from_c_str("topic/1");
2564 struct aws_mqtt_rr_client_operation *operation = s_create_streaming_operation(&fixture, record_key1, topic_filter1);
2565
2566 s_rrc_wait_on_request_completion(&fixture, request_key1);
2567 ASSERT_SUCCESS(
2568 s_rrc_verify_request_completion(&fixture, request_key1, AWS_ERROR_MQTT_REQUEST_RESPONSE_TIMEOUT, NULL, NULL));
2569 s_rrc_wait_on_request_completion(&fixture, request_key2);
2570 ASSERT_SUCCESS(
2571 s_rrc_verify_request_completion(&fixture, request_key2, AWS_ERROR_MQTT_REQUEST_RESPONSE_TIMEOUT, NULL, NULL));
2572
2573 s_rrc_wait_for_n_streaming_subscription_events(&fixture, record_key1, 1);
2574
2575 struct aws_rr_client_fixture_streaming_record_subscription_event expected_events[] = {
2576 {
2577 .status = ARRSSET_SUBSCRIPTION_ESTABLISHED,
2578 .error_code = AWS_ERROR_SUCCESS,
2579 },
2580 };
2581 ASSERT_SUCCESS(s_rrc_verify_streaming_record_subscription_events(
2582 &fixture, record_key1, AWS_ARRAY_SIZE(expected_events), expected_events));
2583
2584 // two publishes on the mqtt client that get reflected into our subscription topic
2585 struct aws_byte_cursor payload1 = aws_byte_cursor_from_c_str("Payload1");
2586 struct aws_byte_cursor payload2 = aws_byte_cursor_from_c_str("Payload2");
2587 ASSERT_SUCCESS(s_rrc_protocol_client_publish(&fixture, topic_filter1, payload1));
2588 ASSERT_SUCCESS(s_rrc_protocol_client_publish(&fixture, topic_filter1, payload2));
2589
2590 s_rrc_wait_for_n_streaming_publishes(&fixture, record_key1, 2);
2591
2592 struct aws_rr_client_fixture_publish_message_view expected_publishes[] = {
2593 {
2594 payload1,
2595 topic_filter1,
2596 },
2597 {
2598 payload2,
2599 topic_filter1,
2600 },
2601 };
2602 ASSERT_SUCCESS(s_rrc_verify_streaming_publishes(
2603 &fixture, record_key1, AWS_ARRAY_SIZE(expected_publishes), expected_publishes));
2604
2605 aws_mqtt_rr_client_operation_release(operation);
2606
2607 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
2608
2609 aws_mqtt_library_clean_up();
2610
2611 return AWS_OP_SUCCESS;
2612 }
2613
2614 AWS_TEST_CASE(
2615 rrc_streaming_operation_success_delayed_by_request_operations,
2616 s_rrc_streaming_operation_success_delayed_by_request_operations_fn)
2617
2618 /*
2619 * Variant of previous test where we sandwich the streaming operation by multiple request response operations and
2620 * verify all request-response operations fail with a timeout.
2621 */
2622 static int s_rrc_streaming_operation_success_sandwiched_by_request_operations_fn(
2623 struct aws_allocator *allocator,
2624 void *ctx) {
2625 (void)ctx;
2626
2627 aws_mqtt_library_init(allocator);
2628
2629 struct mqtt5_client_test_options client_test_options;
2630 struct aws_rr_client_test_fixture fixture;
2631 ASSERT_SUCCESS(s_init_fixture_streaming_operation_success(
2632 &fixture, &client_test_options, allocator, s_rrc_unsubscribe_success_config, NULL, RRCP_MQTT5));
2633
2634 struct aws_byte_cursor request_key1 = aws_byte_cursor_from_c_str("requestkey1");
2635 struct aws_byte_cursor request_key2 = aws_byte_cursor_from_c_str("requestkey2");
2636 struct aws_byte_cursor request_key3 = aws_byte_cursor_from_c_str("requestkey3");
2637 struct aws_byte_cursor request_key4 = aws_byte_cursor_from_c_str("requestkey4");
2638
2639 ASSERT_SUCCESS(s_submit_request_operation_from_prefix(&fixture, request_key1, request_key1));
2640 ASSERT_SUCCESS(s_submit_request_operation_from_prefix(&fixture, request_key2, request_key2));
2641
2642 struct aws_byte_cursor record_key1 = aws_byte_cursor_from_c_str("key1");
2643 struct aws_byte_cursor topic_filter1 = aws_byte_cursor_from_c_str("topic/1");
2644 struct aws_mqtt_rr_client_operation *operation = s_create_streaming_operation(&fixture, record_key1, topic_filter1);
2645
2646 ASSERT_SUCCESS(s_submit_request_operation_from_prefix(&fixture, request_key3, request_key3));
2647 ASSERT_SUCCESS(s_submit_request_operation_from_prefix(&fixture, request_key4, request_key4));
2648
2649 s_rrc_wait_on_request_completion(&fixture, request_key1);
2650 ASSERT_SUCCESS(
2651 s_rrc_verify_request_completion(&fixture, request_key1, AWS_ERROR_MQTT_REQUEST_RESPONSE_TIMEOUT, NULL, NULL));
2652 s_rrc_wait_on_request_completion(&fixture, request_key2);
2653 ASSERT_SUCCESS(
2654 s_rrc_verify_request_completion(&fixture, request_key2, AWS_ERROR_MQTT_REQUEST_RESPONSE_TIMEOUT, NULL, NULL));
2655
2656 s_rrc_wait_for_n_streaming_subscription_events(&fixture, record_key1, 1);
2657
2658 struct aws_rr_client_fixture_streaming_record_subscription_event expected_events[] = {
2659 {
2660 .status = ARRSSET_SUBSCRIPTION_ESTABLISHED,
2661 .error_code = AWS_ERROR_SUCCESS,
2662 },
2663 };
2664 ASSERT_SUCCESS(s_rrc_verify_streaming_record_subscription_events(
2665 &fixture, record_key1, AWS_ARRAY_SIZE(expected_events), expected_events));
2666
2667 // two publishes on the mqtt client that get reflected into our subscription topic
2668 struct aws_byte_cursor payload1 = aws_byte_cursor_from_c_str("Payload1");
2669 struct aws_byte_cursor payload2 = aws_byte_cursor_from_c_str("Payload2");
2670 ASSERT_SUCCESS(s_rrc_protocol_client_publish(&fixture, topic_filter1, payload1));
2671 ASSERT_SUCCESS(s_rrc_protocol_client_publish(&fixture, topic_filter1, payload2));
2672
2673 s_rrc_wait_for_n_streaming_publishes(&fixture, record_key1, 2);
2674
2675 struct aws_rr_client_fixture_publish_message_view expected_publishes[] = {
2676 {
2677 payload1,
2678 topic_filter1,
2679 },
2680 {
2681 payload2,
2682 topic_filter1,
2683 },
2684 };
2685 ASSERT_SUCCESS(s_rrc_verify_streaming_publishes(
2686 &fixture, record_key1, AWS_ARRAY_SIZE(expected_publishes), expected_publishes));
2687
2688 s_rrc_wait_on_request_completion(&fixture, request_key3);
2689 ASSERT_SUCCESS(
2690 s_rrc_verify_request_completion(&fixture, request_key3, AWS_ERROR_MQTT_REQUEST_RESPONSE_TIMEOUT, NULL, NULL));
2691 s_rrc_wait_on_request_completion(&fixture, request_key4);
2692 ASSERT_SUCCESS(
2693 s_rrc_verify_request_completion(&fixture, request_key4, AWS_ERROR_MQTT_REQUEST_RESPONSE_TIMEOUT, NULL, NULL));
2694
2695 aws_mqtt_rr_client_operation_release(operation);
2696
2697 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
2698
2699 aws_mqtt_library_clean_up();
2700
2701 return AWS_OP_SUCCESS;
2702 }
2703
2704 AWS_TEST_CASE(
2705 rrc_streaming_operation_success_sandwiched_by_request_operations,
2706 s_rrc_streaming_operation_success_sandwiched_by_request_operations_fn)
2707
2708 enum rrc_publish_handler_directive_type {
2709 RRC_PHDT_SUCCESS,
2710 RRC_PHDT_FAILURE_PUBACK_REASON_CODE,
2711 RRC_PHDT_FAILURE_BAD_PAYLOAD_FORMAT,
2712 RRC_PHDT_FAILURE_MISSING_CORRELATION_TOKEN,
2713 RRC_PHDT_FAILURE_BAD_CORRELATION_TOKEN_TYPE,
2714 RRC_PHDT_FAILURE_MISMATCHED_CORRELATION_TOKEN,
2715 };
2716
2717 int aws_mqtt5_mock_server_handle_publish_json_request(
2718 void *packet,
2719 struct aws_mqtt5_server_mock_connection_context *connection,
2720 void *user_data) {
2721 (void)user_data;
2722
2723 struct aws_json_value *payload_json = NULL;
2724 struct aws_json_value *response_json = NULL;
2725 struct aws_allocator *allocator = connection->allocator;
2726
2727 struct aws_mqtt5_packet_publish_view *publish_view = packet;
2728
2729 /* unmarshal the payload as json */
2730 payload_json = aws_json_value_new_from_string(allocator, publish_view->payload);
2731 AWS_FATAL_ASSERT(payload_json != NULL);
2732
2733 /* 'topic' field is where we should publish to */
2734 struct aws_json_value *topic_value =
2735 aws_json_value_get_from_object(payload_json, aws_byte_cursor_from_c_str("topic"));
2736 AWS_FATAL_ASSERT(topic_value != NULL && aws_json_value_is_string(topic_value));
2737
2738 struct aws_byte_cursor topic;
2739 AWS_ZERO_STRUCT(topic);
2740 aws_json_value_get_string(topic_value, &topic);
2741
2742 /* 'token' field is the correlation token we should reflect */
2743 struct aws_byte_cursor token;
2744 AWS_ZERO_STRUCT(token);
2745
2746 struct aws_json_value *token_value =
2747 aws_json_value_get_from_object(payload_json, aws_byte_cursor_from_c_str("token"));
2748 if (token_value != NULL) {
2749 AWS_FATAL_ASSERT(aws_json_value_is_string(token_value));
2750 aws_json_value_get_string(token_value, &token);
2751 }
2752
2753 /* 'reflection' field is an optional field we should reflect. Used to ensure proper correlation on requests that
2754 * don't use correlation tokens */
2755 struct aws_byte_cursor reflection;
2756 AWS_ZERO_STRUCT(reflection);
2757
2758 struct aws_json_value *reflection_value =
2759 aws_json_value_get_from_object(payload_json, aws_byte_cursor_from_c_str("reflection"));
2760 if (reflection_value != NULL) {
2761 AWS_FATAL_ASSERT(aws_json_value_is_string(reflection_value));
2762 aws_json_value_get_string(reflection_value, &reflection);
2763 }
2764
2765 /* 'directive' field indicates how the response handler should behave */
2766 struct aws_json_value *directive_value =
2767 aws_json_value_get_from_object(payload_json, aws_byte_cursor_from_c_str("directive"));
2768 AWS_FATAL_ASSERT(directive_value != NULL && aws_json_value_is_number(directive_value));
2769
2770 double raw_directive_value = 0;
2771 aws_json_value_get_number(directive_value, &raw_directive_value);
2772 enum rrc_publish_handler_directive_type directive = (int)raw_directive_value;
2773
2774 /* send a PUBACK? */
2775 if (publish_view->qos == AWS_MQTT5_QOS_AT_LEAST_ONCE) {
2776 struct aws_mqtt5_packet_puback_view puback_view = {
2777 .packet_id = publish_view->packet_id,
2778 .reason_code = (directive == RRC_PHDT_FAILURE_PUBACK_REASON_CODE) ? AWS_MQTT5_PARC_NOT_AUTHORIZED
2779 : AWS_MQTT5_PARC_SUCCESS,
2780 };
2781
2782 if (aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_PUBACK, &puback_view)) {
2783 return AWS_OP_ERR;
2784 }
2785 }
2786
2787 if (directive == RRC_PHDT_FAILURE_PUBACK_REASON_CODE) {
2788 goto done;
2789 }
2790
2791 /* build the json response blob */
2792 char response_buffer[512];
2793 switch (directive) {
2794 case RRC_PHDT_FAILURE_BAD_PAYLOAD_FORMAT:
2795 snprintf(
2796 response_buffer,
2797 AWS_ARRAY_SIZE(response_buffer),
2798 "<token>" PRInSTR "</token>",
2799 AWS_BYTE_CURSOR_PRI(token));
2800 break;
2801 case RRC_PHDT_FAILURE_MISSING_CORRELATION_TOKEN:
2802 snprintf(
2803 response_buffer,
2804 AWS_ARRAY_SIZE(response_buffer),
2805 "{\"wrongfield\":\"" PRInSTR "\"}",
2806 AWS_BYTE_CURSOR_PRI(token));
2807 break;
2808 case RRC_PHDT_FAILURE_BAD_CORRELATION_TOKEN_TYPE:
2809 snprintf(response_buffer, AWS_ARRAY_SIZE(response_buffer), "{\"token\":5}");
2810 break;
2811 case RRC_PHDT_FAILURE_MISMATCHED_CORRELATION_TOKEN:
2812 snprintf(response_buffer, AWS_ARRAY_SIZE(response_buffer), "{\"token\":\"NotTheToken\"}");
2813 break;
2814 default: {
2815 int bytes_used = snprintf(response_buffer, AWS_ARRAY_SIZE(response_buffer), "{");
2816 if (token.len > 0) {
2817 bytes_used += snprintf(
2818 response_buffer + bytes_used,
2819 AWS_ARRAY_SIZE(response_buffer) - bytes_used,
2820 "\"token\":\"" PRInSTR "\"",
2821 AWS_BYTE_CURSOR_PRI(token));
2822 }
2823 if (reflection.len > 0) {
2824 if (token.len > 0) {
2825 bytes_used +=
2826 snprintf(response_buffer + bytes_used, AWS_ARRAY_SIZE(response_buffer) - bytes_used, ",");
2827 }
2828 bytes_used += snprintf(
2829 response_buffer + bytes_used,
2830 AWS_ARRAY_SIZE(response_buffer) - bytes_used,
2831 "\"reflection\":\"" PRInSTR "\"",
2832 AWS_BYTE_CURSOR_PRI(reflection));
2833 }
2834 snprintf(response_buffer + bytes_used, AWS_ARRAY_SIZE(response_buffer) - bytes_used, "}");
2835 break;
2836 }
2837 }
2838
2839 /* build the response publish packet */
2840 struct aws_mqtt5_packet_publish_view response_publish_view = {
2841 .qos = AWS_MQTT5_QOS_AT_MOST_ONCE,
2842 .topic = topic,
2843 .payload = aws_byte_cursor_from_c_str(response_buffer),
2844 };
2845
2846 aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_PUBLISH, &response_publish_view);
2847
2848 done:
2849
2850 aws_json_value_destroy(payload_json);
2851 aws_json_value_destroy(response_json);
2852
2853 return AWS_OP_SUCCESS;
2854 }
2855
2856 static int s_init_fixture_request_operation_success(
2857 struct aws_rr_client_test_fixture *fixture,
2858 struct mqtt5_client_test_options *client_test_options,
2859 struct aws_allocator *allocator,
2860 modify_fixture_options_fn *config_modifier,
2861 void *user_data) {
2862
2863 aws_mqtt5_client_test_init_default_options(client_test_options);
2864
2865 client_test_options->server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] =
2866 aws_mqtt5_server_send_suback_on_subscribe;
2867 client_test_options->server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] =
2868 aws_mqtt5_mock_server_handle_publish_json_request;
2869 client_test_options->server_function_table.packet_handlers[AWS_MQTT5_PT_UNSUBSCRIBE] =
2870 aws_mqtt5_mock_server_handle_unsubscribe_unsuback_success;
2871
2872 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options client_test_fixture_options = {
2873 .client_options = &client_test_options->client_options,
2874 .server_function_table = &client_test_options->server_function_table,
2875 .mock_server_user_data = user_data,
2876 };
2877
2878 struct aws_mqtt_request_response_client_options rr_client_options = {
2879 .max_request_response_subscriptions = 2,
2880 .max_streaming_subscriptions = 2,
2881 .operation_timeout_seconds = 2,
2882 };
2883
2884 if (config_modifier != NULL) {
2885 (*config_modifier)(&rr_client_options, client_test_options);
2886 }
2887
2888 ASSERT_SUCCESS(s_aws_rr_client_test_fixture_init_from_mqtt5(
2889 fixture, allocator, &rr_client_options, &client_test_fixture_options, NULL));
2890
2891 return AWS_OP_SUCCESS;
2892 }
2893
2894 static int s_rrc_test_submit_test_request(
2895 struct aws_rr_client_test_fixture *fixture,
2896 enum rrc_publish_handler_directive_type test_directive,
2897 const char *topic_prefix,
2898 struct aws_byte_cursor record_key,
2899 const char *response_topic,
2900 const char *token,
2901 const char *reflection,
2902 bool is_multi_subscribe) {
2903
2904 char path1_buffer[128];
2905 snprintf(path1_buffer, AWS_ARRAY_SIZE(path1_buffer), "%s/accepted", topic_prefix);
2906 char path2_buffer[128];
2907 snprintf(path2_buffer, AWS_ARRAY_SIZE(path2_buffer), "%s/rejected", topic_prefix);
2908
2909 struct aws_byte_cursor token_path = aws_byte_cursor_from_c_str("token");
2910 if (token == NULL) {
2911 AWS_ZERO_STRUCT(token_path);
2912 }
2913
2914 struct aws_mqtt_request_operation_response_path response_paths[] = {
2915 {
2916 .topic = aws_byte_cursor_from_c_str(path1_buffer),
2917 .correlation_token_json_path = token_path,
2918 },
2919 {
2920 .topic = aws_byte_cursor_from_c_str(path2_buffer),
2921 .correlation_token_json_path = token_path,
2922 },
2923 };
2924
2925 char subscription_buffer[128];
2926 snprintf(subscription_buffer, AWS_ARRAY_SIZE(subscription_buffer), "%s/+", topic_prefix);
2927 struct aws_byte_cursor subscription_buffer_cursor = aws_byte_cursor_from_c_str(subscription_buffer);
2928
2929 struct aws_byte_cursor *subscriptions = &subscription_buffer_cursor;
2930 size_t subscription_count = 1;
2931
2932 struct aws_byte_cursor multi_subs[] = {
2933 aws_byte_cursor_from_c_str(path1_buffer),
2934 aws_byte_cursor_from_c_str(path2_buffer),
2935 };
2936 if (is_multi_subscribe) {
2937 subscriptions = multi_subs;
2938 subscription_count = 2;
2939 }
2940
2941 char publish_topic_buffer[128];
2942 snprintf(publish_topic_buffer, AWS_ARRAY_SIZE(publish_topic_buffer), "%s/publish", topic_prefix);
2943
2944 char request_buffer[512];
2945 int used_bytes = snprintf(
2946 request_buffer,
2947 AWS_ARRAY_SIZE(request_buffer),
2948 "{\"topic\":\"%s\",\"directive\":%d",
2949 response_topic,
2950 (int)test_directive);
2951
2952 if (token != NULL) {
2953 used_bytes += snprintf(
2954 request_buffer + used_bytes, AWS_ARRAY_SIZE(request_buffer) - used_bytes, ",\"token\":\"%s\"", token);
2955 }
2956
2957 if (reflection != NULL) {
2958 used_bytes += snprintf(
2959 request_buffer + used_bytes,
2960 AWS_ARRAY_SIZE(request_buffer) - used_bytes,
2961 ",\"reflection\":\"%s\"",
2962 reflection);
2963 }
2964
2965 snprintf(request_buffer + used_bytes, AWS_ARRAY_SIZE(request_buffer) - used_bytes, "}");
2966
2967 struct aws_mqtt_request_operation_options request = {
2968 .subscription_topic_filters = subscriptions,
2969 .subscription_topic_filter_count = subscription_count,
2970 .response_paths = response_paths,
2971 .response_path_count = AWS_ARRAY_SIZE(response_paths),
2972 .publish_topic = aws_byte_cursor_from_c_str(publish_topic_buffer),
2973 .serialized_request = aws_byte_cursor_from_c_str(request_buffer),
2974 .correlation_token = aws_byte_cursor_from_c_str(token),
2975 };
2976
2977 struct aws_rr_client_fixture_request_response_record *record =
2978 s_rrc_fixture_add_request_record(fixture, record_key);
2979
2980 request.completion_callback = s_rrc_fixture_request_completion_callback;
2981 request.user_data = record;
2982
2983 return aws_mqtt_request_response_client_submit_request(fixture->rr_client, &request);
2984 }
2985
2986 static int s_rrc_request_response_success_response_path_accepted_fn(struct aws_allocator *allocator, void *ctx) {
2987 (void)ctx;
2988
2989 aws_mqtt_library_init(allocator);
2990
2991 struct mqtt5_client_test_options client_test_options;
2992 struct aws_rr_client_test_fixture fixture;
2993 ASSERT_SUCCESS(s_init_fixture_request_operation_success(&fixture, &client_test_options, allocator, NULL, NULL));
2994
2995 struct aws_byte_cursor record_key = aws_byte_cursor_from_c_str("testkey");
2996 ASSERT_SUCCESS(s_rrc_test_submit_test_request(
2997 &fixture, RRC_PHDT_SUCCESS, "test", record_key, "test/accepted", "token1", NULL, false));
2998
2999 s_rrc_wait_on_request_completion(&fixture, record_key);
3000
3001 struct aws_byte_cursor expected_response_topic = aws_byte_cursor_from_c_str("test/accepted");
3002 struct aws_byte_cursor expected_payload = aws_byte_cursor_from_c_str("{\"token\":\"token1\"}");
3003 ASSERT_SUCCESS(s_rrc_verify_request_completion(
3004 &fixture, record_key, AWS_ERROR_SUCCESS, &expected_response_topic, &expected_payload));
3005
3006 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
3007
3008 aws_mqtt_library_clean_up();
3009
3010 return AWS_OP_SUCCESS;
3011 }
3012
3013 AWS_TEST_CASE(
3014 rrc_request_response_success_response_path_accepted,
3015 s_rrc_request_response_success_response_path_accepted_fn)
3016
3017 static int s_rrc_request_response_multi_sub_success_response_path_accepted_fn(
3018 struct aws_allocator *allocator,
3019 void *ctx) {
3020 (void)ctx;
3021
3022 aws_mqtt_library_init(allocator);
3023
3024 struct mqtt5_client_test_options client_test_options;
3025 struct aws_rr_client_test_fixture fixture;
3026 ASSERT_SUCCESS(s_init_fixture_request_operation_success(&fixture, &client_test_options, allocator, NULL, NULL));
3027
3028 struct aws_byte_cursor record_key = aws_byte_cursor_from_c_str("testkey");
3029 ASSERT_SUCCESS(s_rrc_test_submit_test_request(
3030 &fixture, RRC_PHDT_SUCCESS, "test", record_key, "test/accepted", "token1", NULL, true));
3031
3032 s_rrc_wait_on_request_completion(&fixture, record_key);
3033
3034 struct aws_byte_cursor expected_response_topic = aws_byte_cursor_from_c_str("test/accepted");
3035 struct aws_byte_cursor expected_payload = aws_byte_cursor_from_c_str("{\"token\":\"token1\"}");
3036 ASSERT_SUCCESS(s_rrc_verify_request_completion(
3037 &fixture, record_key, AWS_ERROR_SUCCESS, &expected_response_topic, &expected_payload));
3038
3039 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
3040
3041 aws_mqtt_library_clean_up();
3042
3043 return AWS_OP_SUCCESS;
3044 }
3045
3046 AWS_TEST_CASE(
3047 rrc_request_response_multi_sub_success_response_path_accepted,
3048 s_rrc_request_response_multi_sub_success_response_path_accepted_fn)
3049
3050 static int s_rrc_request_response_success_response_path_rejected_fn(struct aws_allocator *allocator, void *ctx) {
3051 (void)ctx;
3052
3053 aws_mqtt_library_init(allocator);
3054
3055 struct mqtt5_client_test_options client_test_options;
3056 struct aws_rr_client_test_fixture fixture;
3057 ASSERT_SUCCESS(s_init_fixture_request_operation_success(&fixture, &client_test_options, allocator, NULL, NULL));
3058
3059 struct aws_byte_cursor record_key = aws_byte_cursor_from_c_str("testkey");
3060 ASSERT_SUCCESS(s_rrc_test_submit_test_request(
3061 &fixture, RRC_PHDT_SUCCESS, "test", record_key, "test/rejected", "token5", NULL, false));
3062
3063 s_rrc_wait_on_request_completion(&fixture, record_key);
3064
3065 struct aws_byte_cursor expected_response_topic = aws_byte_cursor_from_c_str("test/rejected");
3066 struct aws_byte_cursor expected_payload = aws_byte_cursor_from_c_str("{\"token\":\"token5\"}");
3067 ASSERT_SUCCESS(s_rrc_verify_request_completion(
3068 &fixture, record_key, AWS_ERROR_SUCCESS, &expected_response_topic, &expected_payload));
3069
3070 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
3071
3072 aws_mqtt_library_clean_up();
3073
3074 return AWS_OP_SUCCESS;
3075 }
3076
3077 AWS_TEST_CASE(
3078 rrc_request_response_success_response_path_rejected,
3079 s_rrc_request_response_success_response_path_rejected_fn)
3080
3081 static int s_rrc_request_response_multi_sub_success_response_path_rejected_fn(
3082 struct aws_allocator *allocator,
3083 void *ctx) {
3084 (void)ctx;
3085
3086 aws_mqtt_library_init(allocator);
3087
3088 struct mqtt5_client_test_options client_test_options;
3089 struct aws_rr_client_test_fixture fixture;
3090 ASSERT_SUCCESS(s_init_fixture_request_operation_success(&fixture, &client_test_options, allocator, NULL, NULL));
3091
3092 struct aws_byte_cursor record_key = aws_byte_cursor_from_c_str("testkey");
3093 ASSERT_SUCCESS(s_rrc_test_submit_test_request(
3094 &fixture, RRC_PHDT_SUCCESS, "test", record_key, "test/rejected", "token5", NULL, true));
3095
3096 s_rrc_wait_on_request_completion(&fixture, record_key);
3097
3098 struct aws_byte_cursor expected_response_topic = aws_byte_cursor_from_c_str("test/rejected");
3099 struct aws_byte_cursor expected_payload = aws_byte_cursor_from_c_str("{\"token\":\"token5\"}");
3100 ASSERT_SUCCESS(s_rrc_verify_request_completion(
3101 &fixture, record_key, AWS_ERROR_SUCCESS, &expected_response_topic, &expected_payload));
3102
3103 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
3104
3105 aws_mqtt_library_clean_up();
3106
3107 return AWS_OP_SUCCESS;
3108 }
3109
3110 AWS_TEST_CASE(
3111 rrc_request_response_multi_sub_success_response_path_rejected,
3112 s_rrc_request_response_multi_sub_success_response_path_rejected_fn)
3113
3114 static void s_fail_all_subscribes_config_modifier_fn(
3115 struct aws_mqtt_request_response_client_options *fixture_options,
3116 struct mqtt5_client_test_options *client_test_options) {
3117 (void)fixture_options;
3118
3119 client_test_options->server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] =
3120 s_handle_subscribe_with_terminal_failure;
3121 }
3122
3123 static int s_rrc_request_response_subscribe_failure_fn(struct aws_allocator *allocator, void *ctx) {
3124 (void)ctx;
3125
3126 aws_mqtt_library_init(allocator);
3127
3128 struct mqtt5_client_test_options client_test_options;
3129 struct aws_rr_client_test_fixture fixture;
3130
3131 struct rrc_subscribe_handler_context subscribe_context = {
3132 .fixture = &fixture,
3133 .subscribes_received = 0,
3134 };
3135 ASSERT_SUCCESS(s_init_fixture_request_operation_success(
3136 &fixture, &client_test_options, allocator, s_fail_all_subscribes_config_modifier_fn, &subscribe_context));
3137
3138 struct aws_byte_cursor record_key = aws_byte_cursor_from_c_str("testkey");
3139 ASSERT_SUCCESS(s_rrc_test_submit_test_request(
3140 &fixture, RRC_PHDT_SUCCESS, "test", record_key, "test/accepted", "token1", NULL, false));
3141
3142 s_rrc_wait_on_request_completion(&fixture, record_key);
3143
3144 ASSERT_SUCCESS(s_rrc_verify_request_completion(
3145 &fixture, record_key, AWS_ERROR_MQTT_REQUEST_RESPONSE_SUBSCRIBE_FAILURE, NULL, NULL));
3146
3147 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
3148
3149 aws_mqtt_library_clean_up();
3150
3151 return AWS_OP_SUCCESS;
3152 }
3153
3154 AWS_TEST_CASE(rrc_request_response_subscribe_failure, s_rrc_request_response_subscribe_failure_fn)
3155
3156 static enum aws_mqtt5_suback_reason_code s_rrc_usuback_success_rcs[] = {
3157 AWS_MQTT5_SARC_GRANTED_QOS_1,
3158 };
3159
3160 int s_handle_second_subscribe_with_failure(
3161 void *packet,
3162 struct aws_mqtt5_server_mock_connection_context *connection,
3163 void *user_data) {
3164 (void)packet;
3165
3166 struct rrc_subscribe_handler_context *context = user_data;
3167
3168 size_t subscribes_received = 0;
3169
3170 aws_mutex_lock(&context->fixture->lock);
3171 ++context->subscribes_received;
3172 subscribes_received = context->subscribes_received;
3173 aws_mutex_unlock(&context->fixture->lock);
3174
3175 struct aws_mqtt5_packet_subscribe_view *subscribe_packet = packet;
3176
3177 struct aws_mqtt5_packet_suback_view suback_view = {
3178 .packet_id = subscribe_packet->packet_id,
3179 .reason_code_count = 1,
3180 .reason_codes = (subscribes_received == 1) ? s_rrc_usuback_success_rcs : s_rrc_unretryable_suback_rcs,
3181 };
3182
3183 return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_SUBACK, &suback_view);
3184 }
3185
3186 static void s_fail_second_subscribe_config_modifier_fn(
3187 struct aws_mqtt_request_response_client_options *fixture_options,
3188 struct mqtt5_client_test_options *client_test_options) {
3189 (void)fixture_options;
3190
3191 client_test_options->server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] =
3192 s_handle_second_subscribe_with_failure;
3193 }
3194
3195 static int s_rrc_request_response_multi_subscribe_failure_fn(struct aws_allocator *allocator, void *ctx) {
3196 (void)ctx;
3197
3198 aws_mqtt_library_init(allocator);
3199
3200 struct mqtt5_client_test_options client_test_options;
3201 struct aws_rr_client_test_fixture fixture;
3202
3203 struct rrc_subscribe_handler_context subscribe_context = {
3204 .fixture = &fixture,
3205 .subscribes_received = 0,
3206 };
3207 ASSERT_SUCCESS(s_init_fixture_request_operation_success(
3208 &fixture, &client_test_options, allocator, s_fail_second_subscribe_config_modifier_fn, &subscribe_context));
3209
3210 struct aws_byte_cursor record_key = aws_byte_cursor_from_c_str("testkey");
3211 ASSERT_SUCCESS(s_rrc_test_submit_test_request(
3212 &fixture, RRC_PHDT_SUCCESS, "test", record_key, "test/accepted", "token1", NULL, true));
3213
3214 s_rrc_wait_on_request_completion(&fixture, record_key);
3215
3216 ASSERT_SUCCESS(s_rrc_verify_request_completion(
3217 &fixture, record_key, AWS_ERROR_MQTT_REQUEST_RESPONSE_SUBSCRIBE_FAILURE, NULL, NULL));
3218
3219 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
3220
3221 aws_mqtt_library_clean_up();
3222
3223 return AWS_OP_SUCCESS;
3224 }
3225
3226 AWS_TEST_CASE(rrc_request_response_multi_subscribe_failure, s_rrc_request_response_multi_subscribe_failure_fn)
3227
3228 static int s_rrc_request_response_failure_puback_reason_code_fn(struct aws_allocator *allocator, void *ctx) {
3229 (void)ctx;
3230
3231 aws_mqtt_library_init(allocator);
3232
3233 struct mqtt5_client_test_options client_test_options;
3234 struct aws_rr_client_test_fixture fixture;
3235 ASSERT_SUCCESS(s_init_fixture_request_operation_success(&fixture, &client_test_options, allocator, NULL, NULL));
3236
3237 struct aws_byte_cursor record_key = aws_byte_cursor_from_c_str("testkey");
3238 ASSERT_SUCCESS(s_rrc_test_submit_test_request(
3239 &fixture, RRC_PHDT_FAILURE_PUBACK_REASON_CODE, "test", record_key, "test/accepted", "token1", NULL, false));
3240
3241 s_rrc_wait_on_request_completion(&fixture, record_key);
3242
3243 ASSERT_SUCCESS(s_rrc_verify_request_completion(
3244 &fixture, record_key, AWS_ERROR_MQTT_REQUEST_RESPONSE_PUBLISH_FAILURE, NULL, NULL));
3245
3246 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
3247
3248 aws_mqtt_library_clean_up();
3249
3250 return AWS_OP_SUCCESS;
3251 }
3252
3253 AWS_TEST_CASE(rrc_request_response_failure_puback_reason_code, s_rrc_request_response_failure_puback_reason_code_fn)
3254
3255 static int s_rrc_request_response_failure_invalid_payload_fn(struct aws_allocator *allocator, void *ctx) {
3256 (void)ctx;
3257
3258 aws_mqtt_library_init(allocator);
3259
3260 struct mqtt5_client_test_options client_test_options;
3261 struct aws_rr_client_test_fixture fixture;
3262 ASSERT_SUCCESS(s_init_fixture_request_operation_success(&fixture, &client_test_options, allocator, NULL, NULL));
3263
3264 struct aws_byte_cursor record_key = aws_byte_cursor_from_c_str("testkey");
3265 ASSERT_SUCCESS(s_rrc_test_submit_test_request(
3266 &fixture, RRC_PHDT_FAILURE_BAD_PAYLOAD_FORMAT, "test", record_key, "test/accepted", "token1", NULL, false));
3267
3268 s_rrc_wait_on_request_completion(&fixture, record_key);
3269
3270 ASSERT_SUCCESS(
3271 s_rrc_verify_request_completion(&fixture, record_key, AWS_ERROR_MQTT_REQUEST_RESPONSE_TIMEOUT, NULL, NULL));
3272
3273 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
3274
3275 aws_mqtt_library_clean_up();
3276
3277 return AWS_OP_SUCCESS;
3278 }
3279
3280 AWS_TEST_CASE(rrc_request_response_failure_invalid_payload, s_rrc_request_response_failure_invalid_payload_fn)
3281
3282 static int s_rrc_request_response_failure_missing_correlation_token_fn(struct aws_allocator *allocator, void *ctx) {
3283 (void)ctx;
3284
3285 aws_mqtt_library_init(allocator);
3286
3287 struct mqtt5_client_test_options client_test_options;
3288 struct aws_rr_client_test_fixture fixture;
3289 ASSERT_SUCCESS(s_init_fixture_request_operation_success(&fixture, &client_test_options, allocator, NULL, NULL));
3290
3291 struct aws_byte_cursor record_key = aws_byte_cursor_from_c_str("testkey");
3292 ASSERT_SUCCESS(s_rrc_test_submit_test_request(
3293 &fixture,
3294 RRC_PHDT_FAILURE_MISSING_CORRELATION_TOKEN,
3295 "test",
3296 record_key,
3297 "test/accepted",
3298 "token1",
3299 NULL,
3300 false));
3301
3302 s_rrc_wait_on_request_completion(&fixture, record_key);
3303
3304 ASSERT_SUCCESS(
3305 s_rrc_verify_request_completion(&fixture, record_key, AWS_ERROR_MQTT_REQUEST_RESPONSE_TIMEOUT, NULL, NULL));
3306
3307 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
3308
3309 aws_mqtt_library_clean_up();
3310
3311 return AWS_OP_SUCCESS;
3312 }
3313
3314 AWS_TEST_CASE(
3315 rrc_request_response_failure_missing_correlation_token,
3316 s_rrc_request_response_failure_missing_correlation_token_fn)
3317
3318 static int s_rrc_request_response_failure_invalid_correlation_token_type_fn(
3319 struct aws_allocator *allocator,
3320 void *ctx) {
3321 (void)ctx;
3322
3323 aws_mqtt_library_init(allocator);
3324
3325 struct mqtt5_client_test_options client_test_options;
3326 struct aws_rr_client_test_fixture fixture;
3327 ASSERT_SUCCESS(s_init_fixture_request_operation_success(&fixture, &client_test_options, allocator, NULL, NULL));
3328
3329 struct aws_byte_cursor record_key = aws_byte_cursor_from_c_str("testkey");
3330 ASSERT_SUCCESS(s_rrc_test_submit_test_request(
3331 &fixture,
3332 RRC_PHDT_FAILURE_BAD_CORRELATION_TOKEN_TYPE,
3333 "test",
3334 record_key,
3335 "test/accepted",
3336 "token1",
3337 NULL,
3338 false));
3339
3340 s_rrc_wait_on_request_completion(&fixture, record_key);
3341
3342 ASSERT_SUCCESS(
3343 s_rrc_verify_request_completion(&fixture, record_key, AWS_ERROR_MQTT_REQUEST_RESPONSE_TIMEOUT, NULL, NULL));
3344
3345 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
3346
3347 aws_mqtt_library_clean_up();
3348
3349 return AWS_OP_SUCCESS;
3350 }
3351
3352 AWS_TEST_CASE(
3353 rrc_request_response_failure_invalid_correlation_token_type,
3354 s_rrc_request_response_failure_invalid_correlation_token_type_fn)
3355
3356 static int s_rrc_request_response_failure_non_matching_correlation_token_fn(
3357 struct aws_allocator *allocator,
3358 void *ctx) {
3359 (void)ctx;
3360
3361 aws_mqtt_library_init(allocator);
3362
3363 struct mqtt5_client_test_options client_test_options;
3364 struct aws_rr_client_test_fixture fixture;
3365 ASSERT_SUCCESS(s_init_fixture_request_operation_success(&fixture, &client_test_options, allocator, NULL, NULL));
3366
3367 struct aws_byte_cursor record_key = aws_byte_cursor_from_c_str("testkey");
3368 ASSERT_SUCCESS(s_rrc_test_submit_test_request(
3369 &fixture,
3370 RRC_PHDT_FAILURE_MISMATCHED_CORRELATION_TOKEN,
3371 "test",
3372 record_key,
3373 "test/accepted",
3374 "token1",
3375 NULL,
3376 false));
3377
3378 s_rrc_wait_on_request_completion(&fixture, record_key);
3379
3380 ASSERT_SUCCESS(
3381 s_rrc_verify_request_completion(&fixture, record_key, AWS_ERROR_MQTT_REQUEST_RESPONSE_TIMEOUT, NULL, NULL));
3382
3383 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
3384
3385 aws_mqtt_library_clean_up();
3386
3387 return AWS_OP_SUCCESS;
3388 }
3389
3390 AWS_TEST_CASE(
3391 rrc_request_response_failure_non_matching_correlation_token,
3392 s_rrc_request_response_failure_non_matching_correlation_token_fn)
3393
3394 static int s_rrc_request_response_success_empty_correlation_token_fn(struct aws_allocator *allocator, void *ctx) {
3395 (void)ctx;
3396
3397 aws_mqtt_library_init(allocator);
3398
3399 struct mqtt5_client_test_options client_test_options;
3400 struct aws_rr_client_test_fixture fixture;
3401 ASSERT_SUCCESS(s_init_fixture_request_operation_success(&fixture, &client_test_options, allocator, NULL, NULL));
3402
3403 struct aws_byte_cursor record_key = aws_byte_cursor_from_c_str("testkey");
3404 ASSERT_SUCCESS(s_rrc_test_submit_test_request(
3405 &fixture, RRC_PHDT_SUCCESS, "test", record_key, "test/accepted", NULL, NULL, false));
3406
3407 s_rrc_wait_on_request_completion(&fixture, record_key);
3408
3409 struct aws_byte_cursor expected_response_topic = aws_byte_cursor_from_c_str("test/accepted");
3410 struct aws_byte_cursor expected_payload = aws_byte_cursor_from_c_str("{}");
3411 ASSERT_SUCCESS(s_rrc_verify_request_completion(
3412 &fixture, record_key, AWS_ERROR_SUCCESS, &expected_response_topic, &expected_payload));
3413
3414 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
3415
3416 aws_mqtt_library_clean_up();
3417
3418 return AWS_OP_SUCCESS;
3419 }
3420
3421 AWS_TEST_CASE(
3422 rrc_request_response_success_empty_correlation_token,
3423 s_rrc_request_response_success_empty_correlation_token_fn)
3424
3425 static int s_rrc_request_response_success_empty_correlation_token_sequence_fn(
3426 struct aws_allocator *allocator,
3427 void *ctx) {
3428 (void)ctx;
3429
3430 aws_mqtt_library_init(allocator);
3431
3432 struct mqtt5_client_test_options client_test_options;
3433 struct aws_rr_client_test_fixture fixture;
3434 ASSERT_SUCCESS(s_init_fixture_request_operation_success(&fixture, &client_test_options, allocator, NULL, NULL));
3435
3436 for (size_t i = 0; i < 20; ++i) {
3437 char key_buffer[128];
3438 snprintf(key_buffer, AWS_ARRAY_SIZE(key_buffer), "testkey%zu", i);
3439 struct aws_byte_cursor record_key = aws_byte_cursor_from_c_str(key_buffer);
3440
3441 char prefix_buffer[128];
3442 snprintf(prefix_buffer, AWS_ARRAY_SIZE(prefix_buffer), "test%zu", i);
3443
3444 char response_topic_buffer[128];
3445 snprintf(response_topic_buffer, AWS_ARRAY_SIZE(response_topic_buffer), "test%zu/accepted", i);
3446
3447 ASSERT_SUCCESS(s_rrc_test_submit_test_request(
3448 &fixture, RRC_PHDT_SUCCESS, prefix_buffer, record_key, response_topic_buffer, NULL, NULL, false));
3449 }
3450
3451 for (size_t i = 0; i < 20; ++i) {
3452 char key_buffer[128];
3453 snprintf(key_buffer, AWS_ARRAY_SIZE(key_buffer), "testkey%zu", i);
3454 struct aws_byte_cursor record_key = aws_byte_cursor_from_c_str(key_buffer);
3455
3456 char response_topic_buffer[128];
3457 snprintf(response_topic_buffer, AWS_ARRAY_SIZE(response_topic_buffer), "test%zu/accepted", i);
3458
3459 s_rrc_wait_on_request_completion(&fixture, record_key);
3460
3461 struct aws_byte_cursor expected_response_topic = aws_byte_cursor_from_c_str(response_topic_buffer);
3462 struct aws_byte_cursor expected_payload = aws_byte_cursor_from_c_str("{}");
3463 ASSERT_SUCCESS(s_rrc_verify_request_completion(
3464 &fixture, record_key, AWS_ERROR_SUCCESS, &expected_response_topic, &expected_payload));
3465 }
3466
3467 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
3468
3469 aws_mqtt_library_clean_up();
3470
3471 return AWS_OP_SUCCESS;
3472 }
3473
3474 AWS_TEST_CASE(
3475 rrc_request_response_success_empty_correlation_token_sequence,
3476 s_rrc_request_response_success_empty_correlation_token_sequence_fn)
3477
3478 /*
3479 * Test that multiple rr operations with identical correlation tokens all get executed
3480 */
3481 static int s_rrc_request_response_success_duplicate_correlation_token_fn(struct aws_allocator *allocator, void *ctx) {
3482 (void)ctx;
3483
3484 aws_mqtt_library_init(allocator);
3485
3486 struct mqtt5_client_test_options client_test_options;
3487 struct aws_rr_client_test_fixture fixture;
3488 ASSERT_SUCCESS(s_init_fixture_request_operation_success(&fixture, &client_test_options, allocator, NULL, NULL));
3489
3490 struct aws_byte_cursor expected_response_topic = aws_byte_cursor_from_c_str("test/accepted");
3491 struct aws_byte_cursor expected_payload = aws_byte_cursor_from_c_str("{\"token\":\"token1\"}");
3492 struct aws_byte_cursor record_key = aws_byte_cursor_from_c_str("testkey");
3493 struct aws_byte_cursor record_key_2 = aws_byte_cursor_from_c_str("testkey2");
3494 struct aws_byte_cursor record_key_3 = aws_byte_cursor_from_c_str("testkey3");
3495
3496 ASSERT_SUCCESS(s_rrc_test_submit_test_request(
3497 &fixture, RRC_PHDT_SUCCESS, "test", record_key, "test/accepted", "token1", NULL, false));
3498 ASSERT_SUCCESS(s_rrc_test_submit_test_request(
3499 &fixture, RRC_PHDT_SUCCESS, "test", record_key_2, "test/accepted", "token1", NULL, false));
3500 ASSERT_SUCCESS(s_rrc_test_submit_test_request(
3501 &fixture, RRC_PHDT_SUCCESS, "test", record_key_3, "test/accepted", "token1", NULL, false));
3502
3503 // Requests using a duplicate correlation token should all complete. They will be executed in-order and
3504 // the queue blocked until a previous operation using the duplicate correlation token completes its task.
3505 s_rrc_wait_on_request_completion(&fixture, record_key_3);
3506 ASSERT_SUCCESS(s_rrc_verify_request_completion(
3507 &fixture, record_key, AWS_ERROR_SUCCESS, &expected_response_topic, &expected_payload));
3508 ASSERT_SUCCESS(s_rrc_verify_request_completion(
3509 &fixture, record_key_2, AWS_ERROR_SUCCESS, &expected_response_topic, &expected_payload));
3510 ASSERT_SUCCESS(s_rrc_verify_request_completion(
3511 &fixture, record_key_3, AWS_ERROR_SUCCESS, &expected_response_topic, &expected_payload));
3512
3513 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
3514
3515 aws_mqtt_library_clean_up();
3516
3517 return AWS_OP_SUCCESS;
3518 }
3519
3520 AWS_TEST_CASE(
3521 rrc_request_response_success_duplicate_correlation_token,
3522 s_rrc_request_response_success_duplicate_correlation_token_fn)
3523
3524 struct rrc_multi_test_operation {
3525 const char *prefix;
3526 const char *token;
3527 const char *reflection;
3528 };
3529
3530 static int s_do_rrc_operation_sequence_test(
3531 struct aws_rr_client_test_fixture *fixture,
3532 size_t operation_count,
3533 struct rrc_multi_test_operation *operations) {
3534 for (size_t i = 0; i < operation_count; ++i) {
3535
3536 struct rrc_multi_test_operation *operation = &operations[i];
3537
3538 char key_buffer[128];
3539 snprintf(key_buffer, AWS_ARRAY_SIZE(key_buffer), "testkey%zu", i);
3540 struct aws_byte_cursor record_key = aws_byte_cursor_from_c_str(key_buffer);
3541
3542 char response_topic_buffer[128];
3543 snprintf(response_topic_buffer, AWS_ARRAY_SIZE(response_topic_buffer), "%s/accepted", operation->prefix);
3544
3545 ASSERT_SUCCESS(s_rrc_test_submit_test_request(
3546 fixture,
3547 RRC_PHDT_SUCCESS,
3548 operation->prefix,
3549 record_key,
3550 response_topic_buffer,
3551 operation->token,
3552 operation->reflection,
3553 false));
3554 }
3555
3556 for (size_t i = 0; i < operation_count; ++i) {
3557
3558 struct rrc_multi_test_operation *operation = &operations[i];
3559
3560 char key_buffer[128];
3561 snprintf(key_buffer, AWS_ARRAY_SIZE(key_buffer), "testkey%zu", i);
3562 struct aws_byte_cursor record_key = aws_byte_cursor_from_c_str(key_buffer);
3563
3564 char response_topic_buffer[128];
3565 snprintf(response_topic_buffer, AWS_ARRAY_SIZE(response_topic_buffer), "%s/accepted", operation->prefix);
3566
3567 s_rrc_wait_on_request_completion(fixture, record_key);
3568
3569 struct aws_byte_cursor expected_response_topic = aws_byte_cursor_from_c_str(response_topic_buffer);
3570 struct aws_byte_cursor expected_payload;
3571 AWS_ZERO_STRUCT(expected_payload);
3572 char payload_buffer[256];
3573 int bytes_used = snprintf(payload_buffer, AWS_ARRAY_SIZE(payload_buffer), "{");
3574 if (operation->token != NULL) {
3575 bytes_used += snprintf(
3576 payload_buffer + bytes_used,
3577 AWS_ARRAY_SIZE(payload_buffer) - bytes_used,
3578 "\"token\":\"%s\"",
3579 operation->token);
3580 if (operation->reflection != NULL) {
3581 bytes_used += snprintf(payload_buffer + bytes_used, AWS_ARRAY_SIZE(payload_buffer) - bytes_used, ",");
3582 }
3583 }
3584 if (operation->reflection != NULL) {
3585 bytes_used += snprintf(
3586 payload_buffer + bytes_used,
3587 AWS_ARRAY_SIZE(payload_buffer) - bytes_used,
3588 "\"reflection\":\"%s\"",
3589 operation->reflection);
3590 }
3591 snprintf(payload_buffer + bytes_used, AWS_ARRAY_SIZE(payload_buffer) - bytes_used, "}");
3592
3593 expected_payload = aws_byte_cursor_from_c_str(payload_buffer);
3594 ASSERT_SUCCESS(s_rrc_verify_request_completion(
3595 fixture, record_key, AWS_ERROR_SUCCESS, &expected_response_topic, &expected_payload));
3596 }
3597
3598 return AWS_OP_SUCCESS;
3599 }
3600
3601 static int s_rrc_request_response_multi_operation_sequence_fn(struct aws_allocator *allocator, void *ctx) {
3602 (void)ctx;
3603
3604 aws_mqtt_library_init(allocator);
3605
3606 struct mqtt5_client_test_options client_test_options;
3607 struct aws_rr_client_test_fixture fixture;
3608 ASSERT_SUCCESS(s_init_fixture_request_operation_success(&fixture, &client_test_options, allocator, NULL, NULL));
3609
3610 struct rrc_multi_test_operation request_sequence[] = {
3611 {
3612 .prefix = "test",
3613 .token = "token1",
3614 },
3615 {
3616 .prefix = "test",
3617 .token = "token2",
3618 },
3619 {
3620 .prefix = "test2",
3621 .token = "token3",
3622 },
3623 {
3624 .prefix = "whatthe",
3625 .token = "hey",
3626 .reflection = "something",
3627 },
3628 {
3629 .prefix = "test",
3630 .token = "token4",
3631 },
3632 {
3633 .prefix = "test2",
3634 .token = "token5",
3635 },
3636 {
3637 .prefix = "provision",
3638 .reflection = "provision1",
3639 },
3640 {
3641 .prefix = "provision",
3642 .reflection = "provision2",
3643 },
3644 {
3645 .prefix = "create-keys-and-cert",
3646 .reflection = "create-keys1",
3647 },
3648 {
3649 .prefix = "test",
3650 .token = "token6",
3651 },
3652 {
3653 .prefix = "test2",
3654 .token = "token7",
3655 },
3656 {
3657 .prefix = "provision",
3658 .reflection = "provision3",
3659 },
3660 };
3661
3662 ASSERT_SUCCESS(s_do_rrc_operation_sequence_test(&fixture, AWS_ARRAY_SIZE(request_sequence), request_sequence));
3663
3664 s_aws_rr_client_test_fixture_clean_up(&fixture, false);
3665
3666 aws_mqtt_library_clean_up();
3667
3668 return AWS_OP_SUCCESS;
3669 }
3670
3671 AWS_TEST_CASE(rrc_request_response_multi_operation_sequence, s_rrc_request_response_multi_operation_sequence_fn)
3672
3673 struct aws_rr_client_fixture_matched_stream_subscription {
3674 struct aws_allocator *allocator;
3675 struct aws_byte_buf payload;
3676 struct aws_byte_buf topic;
3677 struct aws_byte_buf topic_filter;
3678 };
3679
3680 struct aws_rr_client_fixture_matched_stream_subscription_view {
3681 struct aws_byte_cursor payload;
3682 struct aws_byte_cursor topic;
3683 struct aws_byte_cursor topic_filter;
3684 };
3685
3686 struct aws_rr_client_fixture_matched_request_subscription {
3687 struct aws_allocator *allocator;
3688 struct aws_byte_buf payload;
3689 struct aws_byte_buf topic;
3690 struct aws_byte_buf topic_filter;
3691 struct aws_byte_buf token_path;
3692 };
3693
3694 struct aws_rr_client_fixture_matched_request_subscription_view {
3695 struct aws_byte_cursor payload;
3696 struct aws_byte_cursor topic;
3697 struct aws_byte_cursor topic_filter;
3698 struct aws_byte_cursor token_path;
3699 };
3700
3701 struct aws_rr_client_fixture_subscriptions_matches_record {
3702 struct aws_allocator *allocator;
3703 // table: topic_filter -> aws_rr_client_fixture_matched_stream_subscription
3704 struct aws_hash_table stream_matches;
3705 // table: topic_filter -> aws_rr_client_fixture_matched_request_subscription
3706 struct aws_hash_table request_matches;
3707 size_t stream_matches_count;
3708 size_t request_matches_count;
3709 };
3710
3711 static void s_aws_rr_client_fixture_stream_subscription_destroy(void *value) {
3712 struct aws_rr_client_fixture_matched_stream_subscription *matched_subscription = value;
3713 aws_byte_buf_clean_up(&matched_subscription->payload);
3714 aws_byte_buf_clean_up(&matched_subscription->topic);
3715 aws_byte_buf_clean_up(&matched_subscription->topic_filter);
3716 aws_mem_release(matched_subscription->allocator, matched_subscription);
3717 }
3718
3719 static void s_aws_rr_client_fixture_request_subscription_destroy(void *value) {
3720 struct aws_rr_client_fixture_matched_request_subscription *matched_subscription = value;
3721 aws_byte_buf_clean_up(&matched_subscription->payload);
3722 aws_byte_buf_clean_up(&matched_subscription->topic);
3723 aws_byte_buf_clean_up(&matched_subscription->topic_filter);
3724 aws_byte_buf_clean_up(&matched_subscription->token_path);
3725 aws_mem_release(matched_subscription->allocator, matched_subscription);
3726 }
3727
3728 struct aws_rr_client_fixture_subscriptions_matches_record *s_aws_rr_client_fixture_subscriptions_matches_record_new(
3729 struct aws_allocator *allocator) {
3730 struct aws_rr_client_fixture_subscriptions_matches_record *record =
3731 aws_mem_calloc(allocator, 1, sizeof(struct aws_rr_client_fixture_subscriptions_matches_record));
3732
3733 record->allocator = allocator;
3734 aws_hash_table_init(
3735 &record->stream_matches,
3736 allocator,
3737 10,
3738 aws_hash_byte_cursor_ptr,
3739 aws_mqtt_byte_cursor_hash_equality,
3740 NULL,
3741 s_aws_rr_client_fixture_stream_subscription_destroy);
3742 record->stream_matches_count = 0;
3743
3744 aws_hash_table_init(
3745 &record->request_matches,
3746 allocator,
3747 10,
3748 aws_hash_byte_cursor_ptr,
3749 aws_mqtt_byte_cursor_hash_equality,
3750 NULL,
3751 s_aws_rr_client_fixture_request_subscription_destroy);
3752 record->request_matches_count = 0;
3753
3754 return record;
3755 }
3756
3757 void s_aws_rr_client_fixture_subscriptions_macthes_record_delete(
3758 struct aws_rr_client_fixture_subscriptions_matches_record *record) {
3759 aws_hash_table_clean_up(&record->stream_matches);
3760 aws_hash_table_clean_up(&record->request_matches);
3761 aws_mem_release(record->allocator, record);
3762 }
3763
3764 static int s_rrc_verify_subscriptions_publishes(
3765 struct aws_rr_client_fixture_subscriptions_matches_record *record,
3766 size_t expected_stream_matches_count,
3767 struct aws_rr_client_fixture_matched_stream_subscription_view *expected_stream_matched_subscriptions,
3768 size_t expected_request_matches_count,
3769 struct aws_rr_client_fixture_matched_request_subscription_view *expected_request_matched_subscriptions) {
3770
3771 ASSERT_INT_EQUALS(expected_stream_matches_count, record->stream_matches_count);
3772
3773 for (size_t i = 0; i < expected_stream_matches_count; ++i) {
3774 struct aws_rr_client_fixture_matched_stream_subscription_view *expected_matched_subscription =
3775 &expected_stream_matched_subscriptions[i];
3776
3777 struct aws_hash_element *element = NULL;
3778 ASSERT_SUCCESS(
3779 aws_hash_table_find(&record->stream_matches, &expected_matched_subscription->topic_filter, &element));
3780
3781 struct aws_rr_client_fixture_matched_stream_subscription *actual_matched_subscription = element->value;
3782
3783 ASSERT_BIN_ARRAYS_EQUALS(
3784 expected_matched_subscription->payload.ptr,
3785 expected_matched_subscription->payload.len,
3786 actual_matched_subscription->payload.buffer,
3787 actual_matched_subscription->payload.len);
3788 ASSERT_BIN_ARRAYS_EQUALS(
3789 expected_matched_subscription->topic.ptr,
3790 expected_matched_subscription->topic.len,
3791 actual_matched_subscription->topic.buffer,
3792 actual_matched_subscription->topic.len);
3793 ASSERT_BIN_ARRAYS_EQUALS(
3794 expected_matched_subscription->topic_filter.ptr,
3795 expected_matched_subscription->topic_filter.len,
3796 actual_matched_subscription->topic_filter.buffer,
3797 actual_matched_subscription->topic_filter.len);
3798 }
3799
3800 ASSERT_INT_EQUALS(expected_request_matches_count, record->request_matches_count);
3801
3802 for (size_t i = 0; i < expected_request_matches_count; ++i) {
3803 struct aws_rr_client_fixture_matched_request_subscription_view *expected_matched_subscription =
3804 &expected_request_matched_subscriptions[i];
3805
3806 struct aws_hash_element *element = NULL;
3807 ASSERT_SUCCESS(
3808 aws_hash_table_find(&record->request_matches, &expected_matched_subscription->topic_filter, &element));
3809
3810 struct aws_rr_client_fixture_matched_request_subscription *actual_matched_subscription = element->value;
3811
3812 ASSERT_BIN_ARRAYS_EQUALS(
3813 expected_matched_subscription->payload.ptr,
3814 expected_matched_subscription->payload.len,
3815 actual_matched_subscription->payload.buffer,
3816 actual_matched_subscription->payload.len);
3817 ASSERT_BIN_ARRAYS_EQUALS(
3818 expected_matched_subscription->topic.ptr,
3819 expected_matched_subscription->topic.len,
3820 actual_matched_subscription->topic.buffer,
3821 actual_matched_subscription->topic.len);
3822 ASSERT_BIN_ARRAYS_EQUALS(
3823 expected_matched_subscription->topic_filter.ptr,
3824 expected_matched_subscription->topic_filter.len,
3825 actual_matched_subscription->topic_filter.buffer,
3826 actual_matched_subscription->topic_filter.len);
3827 ASSERT_BIN_ARRAYS_EQUALS(
3828 expected_matched_subscription->token_path.ptr,
3829 expected_matched_subscription->token_path.len,
3830 actual_matched_subscription->token_path.buffer,
3831 actual_matched_subscription->token_path.len);
3832 }
3833
3834 return AWS_OP_SUCCESS;
3835 }
3836
3837 static void s_rrs_fixture_on_stream_operation_subscription_match(
3838 const struct aws_linked_list *operations,
3839 const struct aws_byte_cursor *topic_filter,
3840 const struct aws_mqtt_rr_incoming_publish_event *publish_event,
3841 void *user_data) {
3842
3843 (void)operations;
3844
3845 struct aws_rr_client_fixture_subscriptions_matches_record *record = user_data;
3846
3847 struct aws_rr_client_fixture_matched_stream_subscription *matched_subscription =
3848 aws_mem_calloc(record->allocator, 1, sizeof(struct aws_rr_client_fixture_matched_stream_subscription));
3849 matched_subscription->allocator = record->allocator;
3850 aws_byte_buf_init_copy_from_cursor(&matched_subscription->payload, record->allocator, publish_event->payload);
3851 aws_byte_buf_init_copy_from_cursor(&matched_subscription->topic, record->allocator, publish_event->topic);
3852 aws_byte_buf_init_copy_from_cursor(&matched_subscription->topic_filter, record->allocator, *topic_filter);
3853
3854 aws_hash_table_put(&record->stream_matches, topic_filter, matched_subscription, NULL);
3855 ++record->stream_matches_count;
3856 }
3857
3858 static void s_rrs_fixture_on_request_operation_subscription_match(
3859 struct aws_rr_response_path_entry *entry,
3860 const struct aws_mqtt_rr_incoming_publish_event *publish_event,
3861 void *user_data) {
3862 (void)entry;
3863 (void)publish_event;
3864
3865 struct aws_rr_client_fixture_subscriptions_matches_record *record = user_data;
3866
3867 struct aws_rr_client_fixture_matched_request_subscription *matched_subscription =
3868 aws_mem_calloc(record->allocator, 1, sizeof(struct aws_rr_client_fixture_matched_request_subscription));
3869 matched_subscription->allocator = record->allocator;
3870 aws_byte_buf_init_copy_from_cursor(&matched_subscription->payload, record->allocator, publish_event->payload);
3871 aws_byte_buf_init_copy_from_cursor(&matched_subscription->topic, record->allocator, publish_event->topic);
3872 aws_byte_buf_init_copy_from_cursor(&matched_subscription->topic_filter, record->allocator, publish_event->topic);
3873 aws_byte_buf_init_copy(&matched_subscription->token_path, record->allocator, &entry->correlation_token_json_path);
3874
3875 aws_hash_table_put(&record->request_matches, &publish_event->topic, matched_subscription, NULL);
3876 ++record->request_matches_count;
3877 }
3878
3879 static int s_rrs_init_cleanup_fn(struct aws_allocator *allocator, void *ctx) {
3880 (void)ctx;
3881
3882 struct aws_request_response_subscriptions subscriptions;
3883 ASSERT_SUCCESS(aws_mqtt_request_response_client_subscriptions_init(&subscriptions, allocator));
3884 aws_mqtt_request_response_client_subscriptions_clean_up(&subscriptions);
3885 return AWS_OP_SUCCESS;
3886 }
3887
3888 AWS_TEST_CASE(rrs_init_cleanup, s_rrs_init_cleanup_fn)
3889
3890 static int s_rrs_stream_subscriptions_match_single_level_wildcards_fn(struct aws_allocator *allocator, void *ctx) {
3891 (void)ctx;
3892
3893 struct aws_request_response_subscriptions subscriptions;
3894
3895 aws_mqtt_request_response_client_subscriptions_init(&subscriptions, allocator);
3896
3897 struct aws_byte_cursor topic_filter1 = aws_byte_cursor_from_c_str("topic/123/abc");
3898 struct aws_byte_cursor topic_filter2 = aws_byte_cursor_from_c_str("topic/123/+");
3899 struct aws_byte_cursor topic_filter3 = aws_byte_cursor_from_c_str("topic/+/abc");
3900
3901 struct aws_byte_cursor topic1 = aws_byte_cursor_from_c_str("topic/123/abc");
3902 struct aws_byte_cursor payload1 = aws_byte_cursor_from_c_str("Payload1");
3903
3904 aws_mqtt_request_response_client_subscriptions_add_stream_subscription(&subscriptions, &topic_filter1);
3905 aws_mqtt_request_response_client_subscriptions_add_stream_subscription(&subscriptions, &topic_filter2);
3906 aws_mqtt_request_response_client_subscriptions_add_stream_subscription(&subscriptions, &topic_filter3);
3907
3908 struct aws_mqtt_rr_incoming_publish_event publish_event = {
3909 .topic = topic1,
3910 .payload = payload1,
3911 };
3912
3913 struct aws_rr_client_fixture_subscriptions_matches_record *record =
3914 s_aws_rr_client_fixture_subscriptions_matches_record_new(allocator);
3915
3916 aws_mqtt_request_response_client_subscriptions_handle_incoming_publish(
3917 &subscriptions,
3918 &publish_event,
3919 s_rrs_fixture_on_stream_operation_subscription_match,
3920 s_rrs_fixture_on_request_operation_subscription_match,
3921 record);
3922
3923 struct aws_rr_client_fixture_matched_stream_subscription_view matched_subscriptions[] = {
3924 {payload1, topic1, topic_filter1},
3925 {payload1, topic1, topic_filter2},
3926 {payload1, topic1, topic_filter3},
3927 };
3928
3929 ASSERT_SUCCESS(s_rrc_verify_subscriptions_publishes(
3930 record,
3931 AWS_ARRAY_SIZE(matched_subscriptions),
3932 matched_subscriptions,
3933 0, /* expected_request_matches_count */
3934 NULL));
3935
3936 s_aws_rr_client_fixture_subscriptions_macthes_record_delete(record);
3937
3938 aws_mqtt_request_response_client_subscriptions_clean_up(&subscriptions);
3939
3940 return AWS_OP_SUCCESS;
3941 }
3942
3943 AWS_TEST_CASE(
3944 rrs_stream_subscriptions_match_single_level_wildcards,
3945 s_rrs_stream_subscriptions_match_single_level_wildcards_fn)
3946
3947 static int s_rrs_stream_subscriptions_match_multi_level_wildcards_fn(struct aws_allocator *allocator, void *ctx) {
3948 (void)ctx;
3949
3950 struct aws_request_response_subscriptions subscriptions;
3951
3952 aws_mqtt_request_response_client_subscriptions_init(&subscriptions, allocator);
3953
3954 struct aws_byte_cursor topic_filter1 = aws_byte_cursor_from_c_str("topic/123/abc");
3955 struct aws_byte_cursor topic_filter2 = aws_byte_cursor_from_c_str("topic/123/#");
3956 struct aws_byte_cursor topic_filter3 = aws_byte_cursor_from_c_str("topic/#");
3957
3958 struct aws_byte_cursor topic1 = aws_byte_cursor_from_c_str("topic/123/abc");
3959 struct aws_byte_cursor payload1 = aws_byte_cursor_from_c_str("Payload1");
3960
3961 aws_mqtt_request_response_client_subscriptions_add_stream_subscription(&subscriptions, &topic_filter1);
3962 aws_mqtt_request_response_client_subscriptions_add_stream_subscription(&subscriptions, &topic_filter2);
3963 aws_mqtt_request_response_client_subscriptions_add_stream_subscription(&subscriptions, &topic_filter3);
3964
3965 struct aws_mqtt_rr_incoming_publish_event publish_event = {
3966 .topic = topic1,
3967 .payload = payload1,
3968 };
3969
3970 struct aws_rr_client_fixture_subscriptions_matches_record *record =
3971 s_aws_rr_client_fixture_subscriptions_matches_record_new(allocator);
3972
3973 aws_mqtt_request_response_client_subscriptions_handle_incoming_publish(
3974 &subscriptions,
3975 &publish_event,
3976 s_rrs_fixture_on_stream_operation_subscription_match,
3977 s_rrs_fixture_on_request_operation_subscription_match,
3978 record);
3979
3980 struct aws_rr_client_fixture_matched_stream_subscription_view matched_subscriptions[] = {
3981 {payload1, topic1, topic_filter1},
3982 {payload1, topic1, topic_filter2},
3983 {payload1, topic1, topic_filter3},
3984 };
3985
3986 ASSERT_SUCCESS(s_rrc_verify_subscriptions_publishes(
3987 record,
3988 AWS_ARRAY_SIZE(matched_subscriptions),
3989 matched_subscriptions,
3990 0, /* expected_request_matches_count */
3991 NULL));
3992
3993 s_aws_rr_client_fixture_subscriptions_macthes_record_delete(record);
3994
3995 aws_mqtt_request_response_client_subscriptions_clean_up(&subscriptions);
3996
3997 return AWS_OP_SUCCESS;
3998 }
3999
4000 AWS_TEST_CASE(
4001 rrs_stream_subscriptions_match_multi_level_wildcards,
4002 s_rrs_stream_subscriptions_match_multi_level_wildcards_fn)
4003
4004 /* Adding multiple identical stream subscriptions should add only one record. */
4005 static int s_rrs_stream_subscriptions_add_duplicate_fn(struct aws_allocator *allocator, void *ctx) {
4006 (void)ctx;
4007
4008 struct aws_request_response_subscriptions subscriptions;
4009
4010 aws_mqtt_request_response_client_subscriptions_init(&subscriptions, allocator);
4011
4012 struct aws_byte_cursor topic_filter1 = aws_byte_cursor_from_c_str("topic/123/+");
4013
4014 struct aws_byte_cursor topic1 = aws_byte_cursor_from_c_str("topic/123/abc");
4015 struct aws_byte_cursor payload1 = aws_byte_cursor_from_c_str("Payload1");
4016
4017 aws_mqtt_request_response_client_subscriptions_add_stream_subscription(&subscriptions, &topic_filter1);
4018 aws_mqtt_request_response_client_subscriptions_add_stream_subscription(&subscriptions, &topic_filter1);
4019
4020 struct aws_mqtt_rr_incoming_publish_event publish_event = {
4021 .topic = topic1,
4022 .payload = payload1,
4023 };
4024
4025 struct aws_rr_client_fixture_subscriptions_matches_record *record =
4026 s_aws_rr_client_fixture_subscriptions_matches_record_new(allocator);
4027
4028 aws_mqtt_request_response_client_subscriptions_handle_incoming_publish(
4029 &subscriptions,
4030 &publish_event,
4031 s_rrs_fixture_on_stream_operation_subscription_match,
4032 s_rrs_fixture_on_request_operation_subscription_match,
4033 record);
4034
4035 struct aws_rr_client_fixture_matched_stream_subscription_view matched_subscriptions[] = {
4036 {payload1, topic1, topic_filter1},
4037 };
4038
4039 ASSERT_SUCCESS(s_rrc_verify_subscriptions_publishes(
4040 record,
4041 AWS_ARRAY_SIZE(matched_subscriptions),
4042 matched_subscriptions,
4043 0, /* expected_request_matches_count */
4044 NULL));
4045
4046 s_aws_rr_client_fixture_subscriptions_macthes_record_delete(record);
4047
4048 aws_mqtt_request_response_client_subscriptions_clean_up(&subscriptions);
4049
4050 return AWS_OP_SUCCESS;
4051 }
4052
4053 AWS_TEST_CASE(rrs_stream_subscriptions_add_duplicate, s_rrs_stream_subscriptions_add_duplicate_fn)
4054
4055 static int s_rrs_stream_subscriptions_too_long_publish_topic_fn(struct aws_allocator *allocator, void *ctx) {
4056 (void)ctx;
4057
4058 struct aws_request_response_subscriptions subscriptions;
4059
4060 aws_mqtt_request_response_client_subscriptions_init(&subscriptions, allocator);
4061
4062 struct aws_byte_cursor topic_filter1 = aws_byte_cursor_from_c_str("topic/123/+");
4063
4064 struct aws_byte_cursor topic1 = aws_byte_cursor_from_c_str("topic/123/abc/def");
4065 struct aws_byte_cursor payload1 = aws_byte_cursor_from_c_str("Payload1");
4066
4067 aws_mqtt_request_response_client_subscriptions_add_stream_subscription(&subscriptions, &topic_filter1);
4068 aws_mqtt_request_response_client_subscriptions_add_stream_subscription(&subscriptions, &topic_filter1);
4069
4070 struct aws_mqtt_rr_incoming_publish_event publish_event = {
4071 .topic = topic1,
4072 .payload = payload1,
4073 };
4074
4075 struct aws_rr_client_fixture_subscriptions_matches_record *record =
4076 s_aws_rr_client_fixture_subscriptions_matches_record_new(allocator);
4077
4078 aws_mqtt_request_response_client_subscriptions_handle_incoming_publish(
4079 &subscriptions,
4080 &publish_event,
4081 s_rrs_fixture_on_stream_operation_subscription_match,
4082 s_rrs_fixture_on_request_operation_subscription_match,
4083 record);
4084
4085 ASSERT_SUCCESS(s_rrc_verify_subscriptions_publishes(
4086 record,
4087 0, /* expected_stream_matches_count */
4088 NULL,
4089 0, /* expected_request_matches_count */
4090 NULL));
4091
4092 s_aws_rr_client_fixture_subscriptions_macthes_record_delete(record);
4093
4094 aws_mqtt_request_response_client_subscriptions_clean_up(&subscriptions);
4095
4096 return AWS_OP_SUCCESS;
4097 }
4098
4099 AWS_TEST_CASE(rrs_stream_subscriptions_too_long_publish_topic, s_rrs_stream_subscriptions_too_long_publish_topic_fn)
4100
4101 static int s_rrs_stream_subscriptions_too_short_publish_topic_fn(struct aws_allocator *allocator, void *ctx) {
4102 (void)ctx;
4103
4104 struct aws_request_response_subscriptions subscriptions;
4105
4106 aws_mqtt_request_response_client_subscriptions_init(&subscriptions, allocator);
4107
4108 struct aws_byte_cursor topic_filter1 = aws_byte_cursor_from_c_str("topic/123/+");
4109
4110 struct aws_byte_cursor topic1 = aws_byte_cursor_from_c_str("topic/123");
4111 struct aws_byte_cursor payload1 = aws_byte_cursor_from_c_str("Payload1");
4112
4113 aws_mqtt_request_response_client_subscriptions_add_stream_subscription(&subscriptions, &topic_filter1);
4114 aws_mqtt_request_response_client_subscriptions_add_stream_subscription(&subscriptions, &topic_filter1);
4115
4116 struct aws_mqtt_rr_incoming_publish_event publish_event = {
4117 .topic = topic1,
4118 .payload = payload1,
4119 };
4120
4121 struct aws_rr_client_fixture_subscriptions_matches_record *record =
4122 s_aws_rr_client_fixture_subscriptions_matches_record_new(allocator);
4123
4124 aws_mqtt_request_response_client_subscriptions_handle_incoming_publish(
4125 &subscriptions,
4126 &publish_event,
4127 s_rrs_fixture_on_stream_operation_subscription_match,
4128 s_rrs_fixture_on_request_operation_subscription_match,
4129 record);
4130
4131 ASSERT_SUCCESS(s_rrc_verify_subscriptions_publishes(
4132 record,
4133 0, /* expected_stream_matches_count */
4134 NULL,
4135 0, /* expected_request_matches_count */
4136 NULL));
4137
4138 s_aws_rr_client_fixture_subscriptions_macthes_record_delete(record);
4139
4140 aws_mqtt_request_response_client_subscriptions_clean_up(&subscriptions);
4141
4142 return AWS_OP_SUCCESS;
4143 }
4144
4145 AWS_TEST_CASE(rrs_stream_subscriptions_too_short_publish_topic, s_rrs_stream_subscriptions_too_short_publish_topic_fn)
4146
4147 static int s_rrs_request_subscriptions_add_single_subscription_fn(struct aws_allocator *allocator, void *ctx) {
4148 (void)ctx;
4149
4150 struct aws_request_response_subscriptions subscriptions;
4151
4152 aws_mqtt_request_response_client_subscriptions_init(&subscriptions, allocator);
4153
4154 struct aws_byte_cursor topic1 = aws_byte_cursor_from_c_str("topic/123/abc");
4155 struct aws_byte_cursor token_path1 = aws_byte_cursor_from_c_str("token1");
4156 struct aws_byte_cursor payload1 = aws_byte_cursor_from_c_str("Payload1");
4157
4158 aws_mqtt_request_response_client_subscriptions_add_request_subscription(&subscriptions, &topic1, &token_path1);
4159
4160 struct aws_mqtt_rr_incoming_publish_event publish_event = {
4161 .topic = topic1,
4162 .payload = payload1,
4163 };
4164
4165 struct aws_rr_client_fixture_subscriptions_matches_record *record =
4166 s_aws_rr_client_fixture_subscriptions_matches_record_new(allocator);
4167
4168 aws_mqtt_request_response_client_subscriptions_handle_incoming_publish(
4169 &subscriptions,
4170 &publish_event,
4171 s_rrs_fixture_on_stream_operation_subscription_match,
4172 s_rrs_fixture_on_request_operation_subscription_match,
4173 record);
4174
4175 struct aws_rr_client_fixture_matched_request_subscription_view matched_subscriptions[] = {
4176 {payload1, topic1, topic1, token_path1},
4177 };
4178
4179 ASSERT_SUCCESS(s_rrc_verify_subscriptions_publishes(
4180 record,
4181 0 /* expected_stream_matches_count */,
4182 NULL,
4183 AWS_ARRAY_SIZE(matched_subscriptions),
4184 matched_subscriptions));
4185
4186 s_aws_rr_client_fixture_subscriptions_macthes_record_delete(record);
4187
4188 aws_mqtt_request_response_client_subscriptions_clean_up(&subscriptions);
4189
4190 return AWS_OP_SUCCESS;
4191 }
4192
4193 AWS_TEST_CASE(rrs_request_subscriptions_add_single_subscription, s_rrs_request_subscriptions_add_single_subscription_fn)
4194
4195 static int s_rrs_request_subscriptions_remove_subscription_fn(struct aws_allocator *allocator, void *ctx) {
4196 (void)ctx;
4197
4198 struct aws_request_response_subscriptions subscriptions;
4199
4200 aws_mqtt_request_response_client_subscriptions_init(&subscriptions, allocator);
4201
4202 struct aws_byte_cursor topic1 = aws_byte_cursor_from_c_str("topic/123/abc");
4203 struct aws_byte_cursor token_path1 = aws_byte_cursor_from_c_str("token1");
4204 struct aws_byte_cursor payload1 = aws_byte_cursor_from_c_str("Payload1");
4205
4206 aws_mqtt_request_response_client_subscriptions_add_request_subscription(&subscriptions, &topic1, &token_path1);
4207
4208 struct aws_mqtt_rr_incoming_publish_event publish_event = {
4209 .topic = topic1,
4210 .payload = payload1,
4211 };
4212
4213 struct aws_rr_client_fixture_subscriptions_matches_record *record =
4214 s_aws_rr_client_fixture_subscriptions_matches_record_new(allocator);
4215
4216 aws_mqtt_request_response_client_subscriptions_handle_incoming_publish(
4217 &subscriptions,
4218 &publish_event,
4219 s_rrs_fixture_on_stream_operation_subscription_match,
4220 s_rrs_fixture_on_request_operation_subscription_match,
4221 record);
4222
4223 struct aws_rr_client_fixture_matched_request_subscription_view matched_subscriptions[] = {
4224 {payload1, topic1, topic1, token_path1},
4225 };
4226
4227 ASSERT_SUCCESS(s_rrc_verify_subscriptions_publishes(
4228 record,
4229 0 /* expected_stream_matches_count */,
4230 NULL,
4231 AWS_ARRAY_SIZE(matched_subscriptions),
4232 matched_subscriptions));
4233
4234 s_aws_rr_client_fixture_subscriptions_macthes_record_delete(record);
4235
4236 aws_mqtt_request_response_client_subscriptions_remove_request_subscription(&subscriptions, &topic1);
4237
4238 struct aws_rr_client_fixture_subscriptions_matches_record *record2 =
4239 s_aws_rr_client_fixture_subscriptions_matches_record_new(allocator);
4240
4241 aws_mqtt_request_response_client_subscriptions_handle_incoming_publish(
4242 &subscriptions,
4243 &publish_event,
4244 s_rrs_fixture_on_stream_operation_subscription_match,
4245 s_rrs_fixture_on_request_operation_subscription_match,
4246 record2);
4247
4248 ASSERT_SUCCESS(s_rrc_verify_subscriptions_publishes(
4249 record2, 0 /* expected_stream_matches_count */, NULL, 0 /* expected_request_matches_count */, NULL));
4250
4251 s_aws_rr_client_fixture_subscriptions_macthes_record_delete(record2);
4252
4253 aws_mqtt_request_response_client_subscriptions_clean_up(&subscriptions);
4254
4255 return AWS_OP_SUCCESS;
4256 }
4257
4258 AWS_TEST_CASE(rrs_request_subscriptions_remove_subscription, s_rrs_request_subscriptions_remove_subscription_fn)
4259
4260 /* After adding multiple identical request subscriptions, the same number of removals is required to actually remove
4261 * the subscription. */
4262 static int s_rrs_request_subscriptions_add_duplicate_then_remove_fn(struct aws_allocator *allocator, void *ctx) {
4263 (void)ctx;
4264
4265 struct aws_request_response_subscriptions subscriptions;
4266
4267 aws_mqtt_request_response_client_subscriptions_init(&subscriptions, allocator);
4268
4269 struct aws_byte_cursor topic1 = aws_byte_cursor_from_c_str("topic/123/abc");
4270 struct aws_byte_cursor token_path1 = aws_byte_cursor_from_c_str("token1");
4271 struct aws_byte_cursor payload1 = aws_byte_cursor_from_c_str("Payload1");
4272
4273 aws_mqtt_request_response_client_subscriptions_add_request_subscription(&subscriptions, &topic1, &token_path1);
4274 aws_mqtt_request_response_client_subscriptions_add_request_subscription(&subscriptions, &topic1, &token_path1);
4275
4276 struct aws_mqtt_rr_incoming_publish_event publish_event = {
4277 .topic = topic1,
4278 .payload = payload1,
4279 };
4280
4281 struct aws_rr_client_fixture_subscriptions_matches_record *record =
4282 s_aws_rr_client_fixture_subscriptions_matches_record_new(allocator);
4283
4284 aws_mqtt_request_response_client_subscriptions_handle_incoming_publish(
4285 &subscriptions,
4286 &publish_event,
4287 s_rrs_fixture_on_stream_operation_subscription_match,
4288 s_rrs_fixture_on_request_operation_subscription_match,
4289 record);
4290
4291 struct aws_rr_client_fixture_matched_request_subscription_view matched_subscriptions[] = {
4292 {payload1, topic1, topic1, token_path1},
4293 };
4294
4295 ASSERT_SUCCESS(s_rrc_verify_subscriptions_publishes(
4296 record,
4297 0 /* expected_stream_matches_count */,
4298 NULL,
4299 AWS_ARRAY_SIZE(matched_subscriptions),
4300 matched_subscriptions));
4301
4302 s_aws_rr_client_fixture_subscriptions_macthes_record_delete(record);
4303
4304 // First remove, decrement subscription's internal counter to 1.
4305 aws_mqtt_request_response_client_subscriptions_remove_request_subscription(&subscriptions, &topic1);
4306
4307 struct aws_rr_client_fixture_subscriptions_matches_record *record2 =
4308 s_aws_rr_client_fixture_subscriptions_matches_record_new(allocator);
4309
4310 aws_mqtt_request_response_client_subscriptions_handle_incoming_publish(
4311 &subscriptions,
4312 &publish_event,
4313 s_rrs_fixture_on_stream_operation_subscription_match,
4314 s_rrs_fixture_on_request_operation_subscription_match,
4315 record2);
4316
4317 ASSERT_SUCCESS(s_rrc_verify_subscriptions_publishes(
4318 record2,
4319 0 /* expected_stream_matches_count */,
4320 NULL,
4321 AWS_ARRAY_SIZE(matched_subscriptions),
4322 matched_subscriptions));
4323
4324 s_aws_rr_client_fixture_subscriptions_macthes_record_delete(record2);
4325
4326 // Second remove, decrement subscription's internal counter to 0 and remove it.
4327 aws_mqtt_request_response_client_subscriptions_remove_request_subscription(&subscriptions, &topic1);
4328
4329 struct aws_rr_client_fixture_subscriptions_matches_record *record3 =
4330 s_aws_rr_client_fixture_subscriptions_matches_record_new(allocator);
4331
4332 aws_mqtt_request_response_client_subscriptions_handle_incoming_publish(
4333 &subscriptions,
4334 &publish_event,
4335 s_rrs_fixture_on_stream_operation_subscription_match,
4336 s_rrs_fixture_on_request_operation_subscription_match,
4337 record3);
4338
4339 ASSERT_SUCCESS(s_rrc_verify_subscriptions_publishes(
4340 record3, 0 /* expected_stream_matches_count */, NULL, 0 /* expected_request_matches_count */, NULL));
4341
4342 s_aws_rr_client_fixture_subscriptions_macthes_record_delete(record3);
4343
4344 aws_mqtt_request_response_client_subscriptions_clean_up(&subscriptions);
4345
4346 return AWS_OP_SUCCESS;
4347 }
4348
4349 AWS_TEST_CASE(
4350 rrs_request_subscriptions_add_duplicate_then_remove,
4351 s_rrs_request_subscriptions_add_duplicate_then_remove_fn)
4352
4353 static int s_rrs_request_subscriptions_remove_nonexistent_subscription_fn(struct aws_allocator *allocator, void *ctx) {
4354 (void)ctx;
4355
4356 struct aws_request_response_subscriptions subscriptions;
4357
4358 aws_mqtt_request_response_client_subscriptions_init(&subscriptions, allocator);
4359
4360 struct aws_byte_cursor topic1 = aws_byte_cursor_from_c_str("topic/123/abc");
4361 aws_mqtt_request_response_client_subscriptions_remove_request_subscription(&subscriptions, &topic1);
4362
4363 aws_mqtt_request_response_client_subscriptions_clean_up(&subscriptions);
4364
4365 return AWS_OP_SUCCESS;
4366 }
4367
4368 AWS_TEST_CASE(
4369 rrs_request_subscriptions_remove_nonexistent_subscription,
4370 s_rrs_request_subscriptions_remove_nonexistent_subscription_fn)
4371
4372 static int s_rrs_stream_and_request_subscriptions_add_same_subscription_fn(struct aws_allocator *allocator, void *ctx) {
4373 (void)ctx;
4374
4375 struct aws_request_response_subscriptions subscriptions;
4376
4377 aws_mqtt_request_response_client_subscriptions_init(&subscriptions, allocator);
4378
4379 struct aws_byte_cursor topic1 = aws_byte_cursor_from_c_str("topic/123/abc");
4380 struct aws_byte_cursor topic2 = aws_byte_cursor_from_c_str("topic/123/+");
4381 struct aws_byte_cursor token_path1 = aws_byte_cursor_from_c_str("token1");
4382 struct aws_byte_cursor payload1 = aws_byte_cursor_from_c_str("Payload1");
4383
4384 aws_mqtt_request_response_client_subscriptions_add_stream_subscription(&subscriptions, &topic1);
4385 aws_mqtt_request_response_client_subscriptions_add_stream_subscription(&subscriptions, &topic2);
4386 aws_mqtt_request_response_client_subscriptions_add_request_subscription(&subscriptions, &topic1, &token_path1);
4387
4388 struct aws_mqtt_rr_incoming_publish_event publish_event = {
4389 .topic = topic1,
4390 .payload = payload1,
4391 };
4392
4393 struct aws_rr_client_fixture_subscriptions_matches_record *record =
4394 s_aws_rr_client_fixture_subscriptions_matches_record_new(allocator);
4395
4396 aws_mqtt_request_response_client_subscriptions_handle_incoming_publish(
4397 &subscriptions,
4398 &publish_event,
4399 s_rrs_fixture_on_stream_operation_subscription_match,
4400 s_rrs_fixture_on_request_operation_subscription_match,
4401 record);
4402
4403 struct aws_rr_client_fixture_matched_stream_subscription_view matched_stream_subscriptions[] = {
4404 {payload1, topic1, topic1},
4405 {payload1, topic1, topic2},
4406 };
4407
4408 struct aws_rr_client_fixture_matched_request_subscription_view matched_request_subscriptions[] = {
4409 {payload1, topic1, topic1, token_path1},
4410 };
4411
4412 ASSERT_SUCCESS(s_rrc_verify_subscriptions_publishes(
4413 record,
4414 AWS_ARRAY_SIZE(matched_stream_subscriptions),
4415 matched_stream_subscriptions,
4416 AWS_ARRAY_SIZE(matched_request_subscriptions),
4417 matched_request_subscriptions));
4418
4419 s_aws_rr_client_fixture_subscriptions_macthes_record_delete(record);
4420
4421 aws_mqtt_request_response_client_subscriptions_clean_up(&subscriptions);
4422
4423 return AWS_OP_SUCCESS;
4424 }
4425
4426 AWS_TEST_CASE(
4427 rrs_stream_and_request_subscriptions_add_same_subscription,
4428 s_rrs_stream_and_request_subscriptions_add_same_subscription_fn)