| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "../v3/mqtt311_testing_utils.h" |
| 7 |
#include "../v3/mqtt_mock_server_handler.h" |
| 8 |
#include "../v5/mqtt5_testing_utils.h" |
| 9 |
#include <aws/common/common.h> |
| 10 |
#include <aws/mqtt/request-response/request_response_client.h> |
| 11 |
|
| 12 |
#include "aws/mqtt/private/request-response/protocol_adapter.h" |
| 13 |
|
| 14 |
#include <aws/testing/aws_test_harness.h> |
| 15 |
|
| 16 |
struct request_response_protocol_adapter_incoming_publish_event_record { |
| 17 |
struct aws_byte_buf topic; |
| 18 |
struct aws_byte_buf payload; |
| 19 |
}; |
| 20 |
|
| 21 |
static void s_request_response_protocol_adapter_incoming_publish_event_record_init( |
| 22 |
struct request_response_protocol_adapter_incoming_publish_event_record *record, |
| 23 |
struct aws_allocator *allocator, |
| 24 |
struct aws_byte_cursor topic, |
| 25 |
struct aws_byte_cursor payload) { |
| 26 |
|
| 27 |
aws_byte_buf_init_copy_from_cursor(&record->topic, allocator, topic); |
| 28 |
aws_byte_buf_init_copy_from_cursor(&record->payload, allocator, payload); |
| 29 |
} |
| 30 |
|
| 31 |
static void s_request_response_protocol_adapter_incoming_publish_event_record_clean_up( |
| 32 |
struct request_response_protocol_adapter_incoming_publish_event_record *record) { |
| 33 |
aws_byte_buf_clean_up(&record->topic); |
| 34 |
aws_byte_buf_clean_up(&record->payload); |
| 35 |
} |
| 36 |
|
| 37 |
struct request_response_protocol_adapter_subscription_event_record { |
| 38 |
enum aws_protocol_adapter_subscription_event_type event_type; |
| 39 |
struct aws_byte_buf topic_filter; |
| 40 |
int error_code; |
| 41 |
bool retryable; |
| 42 |
}; |
| 43 |
|
| 44 |
static void s_request_response_protocol_adapter_subscription_event_record_init( |
| 45 |
struct request_response_protocol_adapter_subscription_event_record *record, |
| 46 |
struct aws_allocator *allocator, |
| 47 |
enum aws_protocol_adapter_subscription_event_type event_type, |
| 48 |
struct aws_byte_cursor topic_filter, |
| 49 |
int error_code, |
| 50 |
bool retryable) { |
| 51 |
|
| 52 |
AWS_ZERO_STRUCT(*record); |
| 53 |
|
| 54 |
record->event_type = event_type; |
| 55 |
record->error_code = error_code; |
| 56 |
record->retryable = retryable; |
| 57 |
aws_byte_buf_init_copy_from_cursor(&record->topic_filter, allocator, topic_filter); |
| 58 |
} |
| 59 |
|
| 60 |
static void s_request_response_protocol_adapter_subscription_event_record_cleanup( |
| 61 |
struct request_response_protocol_adapter_subscription_event_record *record) { |
| 62 |
aws_byte_buf_clean_up(&record->topic_filter); |
| 63 |
} |
| 64 |
|
| 65 |
enum aws_protocol_adapter_protocol_type { |
| 66 |
AWS_PAPT_MQTT5, |
| 67 |
AWS_PAPT_MQTT311, |
| 68 |
}; |
| 69 |
|
| 70 |
struct mqtt311_protocol_testing_context { |
| 71 |
struct mqtt_connection_state_test mqtt311_test_context; |
| 72 |
int mqtt311_test_context_setup_result; |
| 73 |
}; |
| 74 |
|
| 75 |
struct aws_request_response_protocol_adapter_test_fixture { |
| 76 |
struct aws_allocator *allocator; |
| 77 |
|
| 78 |
enum aws_protocol_adapter_protocol_type protocol_type; |
| 79 |
|
| 80 |
union { |
| 81 |
struct aws_mqtt5_client_mock_test_fixture mqtt5_fixture; |
| 82 |
struct mqtt311_protocol_testing_context mqtt311_fixture; |
| 83 |
} protocol_context; |
| 84 |
|
| 85 |
struct aws_mqtt_protocol_adapter *protocol_adapter; |
| 86 |
|
| 87 |
struct aws_array_list incoming_publish_events; |
| 88 |
struct aws_array_list connection_events; |
| 89 |
struct aws_array_list subscription_events; |
| 90 |
struct aws_array_list publish_results; |
| 91 |
|
| 92 |
bool adapter_terminated; |
| 93 |
|
| 94 |
struct aws_mutex lock; |
| 95 |
struct aws_condition_variable signal; |
| 96 |
}; |
| 97 |
|
| 98 |
static void s_rr_mqtt_protocol_adapter_test_on_subscription_event( |
| 99 |
const struct aws_protocol_adapter_subscription_event *event, |
| 100 |
void *user_data) { |
| 101 |
struct aws_request_response_protocol_adapter_test_fixture *fixture = user_data; |
| 102 |
|
| 103 |
struct request_response_protocol_adapter_subscription_event_record record; |
| 104 |
s_request_response_protocol_adapter_subscription_event_record_init( |
| 105 |
&record, fixture->allocator, event->event_type, event->topic_filter, event->error_code, event->retryable); |
| 106 |
|
| 107 |
aws_mutex_lock(&fixture->lock); |
| 108 |
aws_array_list_push_back(&fixture->subscription_events, &record); |
| 109 |
aws_mutex_unlock(&fixture->lock); |
| 110 |
aws_condition_variable_notify_all(&fixture->signal); |
| 111 |
} |
| 112 |
|
| 113 |
static void s_rr_mqtt_protocol_adapter_test_on_incoming_publish( |
| 114 |
const struct aws_mqtt_rr_incoming_publish_event *publish, |
| 115 |
void *user_data) { |
| 116 |
struct aws_request_response_protocol_adapter_test_fixture *fixture = user_data; |
| 117 |
|
| 118 |
struct request_response_protocol_adapter_incoming_publish_event_record record; |
| 119 |
AWS_ZERO_STRUCT(record); |
| 120 |
s_request_response_protocol_adapter_incoming_publish_event_record_init( |
| 121 |
&record, fixture->allocator, publish->topic, publish->payload); |
| 122 |
|
| 123 |
aws_mutex_lock(&fixture->lock); |
| 124 |
aws_array_list_push_back(&fixture->incoming_publish_events, &record); |
| 125 |
aws_mutex_unlock(&fixture->lock); |
| 126 |
aws_condition_variable_notify_all(&fixture->signal); |
| 127 |
} |
| 128 |
|
| 129 |
static void s_rr_mqtt_protocol_adapter_test_on_terminate_callback(void *user_data) { |
| 130 |
struct aws_request_response_protocol_adapter_test_fixture *fixture = user_data; |
| 131 |
|
| 132 |
aws_mutex_lock(&fixture->lock); |
| 133 |
fixture->adapter_terminated = true; |
| 134 |
aws_mutex_unlock(&fixture->lock); |
| 135 |
aws_condition_variable_notify_all(&fixture->signal); |
| 136 |
} |
| 137 |
|
| 138 |
static void s_rr_mqtt_protocol_adapter_test_on_connection_event( |
| 139 |
const struct aws_protocol_adapter_connection_event *event, |
| 140 |
void *user_data) { |
| 141 |
struct aws_request_response_protocol_adapter_test_fixture *fixture = user_data; |
| 142 |
|
| 143 |
aws_mutex_lock(&fixture->lock); |
| 144 |
aws_array_list_push_back(&fixture->connection_events, event); |
| 145 |
aws_mutex_unlock(&fixture->lock); |
| 146 |
aws_condition_variable_notify_all(&fixture->signal); |
| 147 |
} |
| 148 |
|
| 149 |
static void s_rr_mqtt_protocol_adapter_test_on_publish_result(int error_code, void *user_data) { |
| 150 |
struct aws_request_response_protocol_adapter_test_fixture *fixture = user_data; |
| 151 |
|
| 152 |
aws_mutex_lock(&fixture->lock); |
| 153 |
aws_array_list_push_back(&fixture->publish_results, &error_code); |
| 154 |
aws_mutex_unlock(&fixture->lock); |
| 155 |
aws_condition_variable_notify_all(&fixture->signal); |
| 156 |
} |
| 157 |
|
| 158 |
static int s_aws_request_response_protocol_adapter_test_fixture_init_shared( |
| 159 |
struct aws_request_response_protocol_adapter_test_fixture *fixture, |
| 160 |
enum aws_protocol_adapter_protocol_type protocol_type) { |
| 161 |
|
| 162 |
struct aws_allocator *allocator = fixture->allocator; |
| 163 |
fixture->protocol_type = protocol_type; |
| 164 |
|
| 165 |
struct aws_mqtt_protocol_adapter_options protocol_adapter_options = { |
| 166 |
.subscription_event_callback = s_rr_mqtt_protocol_adapter_test_on_subscription_event, |
| 167 |
.incoming_publish_callback = s_rr_mqtt_protocol_adapter_test_on_incoming_publish, |
| 168 |
.terminate_callback = s_rr_mqtt_protocol_adapter_test_on_terminate_callback, |
| 169 |
.connection_event_callback = s_rr_mqtt_protocol_adapter_test_on_connection_event, |
| 170 |
.user_data = fixture}; |
| 171 |
|
| 172 |
if (protocol_type == AWS_PAPT_MQTT5) { |
| 173 |
fixture->protocol_adapter = aws_mqtt_protocol_adapter_new_from_5( |
| 174 |
allocator, &protocol_adapter_options, fixture->protocol_context.mqtt5_fixture.client); |
| 175 |
} else { |
| 176 |
fixture->protocol_adapter = aws_mqtt_protocol_adapter_new_from_311( |
| 177 |
allocator, |
| 178 |
&protocol_adapter_options, |
| 179 |
fixture->protocol_context.mqtt311_fixture.mqtt311_test_context.mqtt_connection); |
| 180 |
} |
| 181 |
AWS_FATAL_ASSERT(fixture->protocol_adapter != NULL); |
| 182 |
|
| 183 |
aws_array_list_init_dynamic( |
| 184 |
&fixture->incoming_publish_events, |
| 185 |
allocator, |
| 186 |
10, |
| 187 |
sizeof(struct request_response_protocol_adapter_incoming_publish_event_record)); |
| 188 |
aws_array_list_init_dynamic( |
| 189 |
&fixture->connection_events, allocator, 10, sizeof(struct aws_protocol_adapter_connection_event)); |
| 190 |
aws_array_list_init_dynamic( |
| 191 |
&fixture->subscription_events, |
| 192 |
allocator, |
| 193 |
10, |
| 194 |
sizeof(struct request_response_protocol_adapter_subscription_event_record)); |
| 195 |
aws_array_list_init_dynamic(&fixture->publish_results, allocator, 10, sizeof(int)); |
| 196 |
|
| 197 |
aws_mutex_init(&fixture->lock); |
| 198 |
aws_condition_variable_init(&fixture->signal); |
| 199 |
|
| 200 |
return AWS_OP_SUCCESS; |
| 201 |
} |
| 202 |
|
| 203 |
static int s_aws_request_response_protocol_adapter_test_fixture_init_mqtt5( |
| 204 |
struct aws_request_response_protocol_adapter_test_fixture *fixture, |
| 205 |
struct aws_allocator *allocator, |
| 206 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options *mqtt5_fixture_config) { |
| 207 |
|
| 208 |
AWS_ZERO_STRUCT(*fixture); |
| 209 |
|
| 210 |
fixture->allocator = allocator; |
| 211 |
|
| 212 |
if (aws_mqtt5_client_mock_test_fixture_init( |
| 213 |
&fixture->protocol_context.mqtt5_fixture, allocator, mqtt5_fixture_config)) { |
| 214 |
return AWS_OP_ERR; |
| 215 |
} |
| 216 |
|
| 217 |
return s_aws_request_response_protocol_adapter_test_fixture_init_shared(fixture, AWS_PAPT_MQTT5); |
| 218 |
} |
| 219 |
|
| 220 |
static int s_aws_request_response_protocol_adapter_test_fixture_init_mqtt311( |
| 221 |
struct aws_request_response_protocol_adapter_test_fixture *fixture, |
| 222 |
struct aws_allocator *allocator) { |
| 223 |
|
| 224 |
AWS_ZERO_STRUCT(*fixture); |
| 225 |
|
| 226 |
fixture->allocator = allocator; |
| 227 |
|
| 228 |
int result = |
| 229 |
aws_test311_setup_mqtt_server_fn(allocator, &fixture->protocol_context.mqtt311_fixture.mqtt311_test_context); |
| 230 |
ASSERT_SUCCESS(result); |
| 231 |
fixture->protocol_context.mqtt311_fixture.mqtt311_test_context_setup_result = result; |
| 232 |
|
| 233 |
return s_aws_request_response_protocol_adapter_test_fixture_init_shared(fixture, AWS_PAPT_MQTT311); |
| 234 |
} |
| 235 |
|
| 236 |
static bool s_is_adapter_terminated(void *context) { |
| 237 |
struct aws_request_response_protocol_adapter_test_fixture *fixture = context; |
| 238 |
|
| 239 |
return fixture->adapter_terminated; |
| 240 |
} |
| 241 |
|
| 242 |
static void s_aws_request_response_protocol_adapter_test_fixture_destroy_adapters( |
| 243 |
struct aws_request_response_protocol_adapter_test_fixture *fixture) { |
| 244 |
if (fixture->protocol_adapter != NULL) { |
| 245 |
aws_mqtt_protocol_adapter_destroy(fixture->protocol_adapter); |
| 246 |
|
| 247 |
aws_mutex_lock(&fixture->lock); |
| 248 |
aws_condition_variable_wait_pred(&fixture->signal, &fixture->lock, s_is_adapter_terminated, fixture); |
| 249 |
aws_mutex_unlock(&fixture->lock); |
| 250 |
fixture->protocol_adapter = NULL; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
static void s_aws_request_response_protocol_adapter_test_fixture_clean_up( |
| 255 |
struct aws_request_response_protocol_adapter_test_fixture *fixture) { |
| 256 |
|
| 257 |
s_aws_request_response_protocol_adapter_test_fixture_destroy_adapters(fixture); |
| 258 |
|
| 259 |
if (fixture->protocol_type == AWS_PAPT_MQTT5) { |
| 260 |
aws_mqtt5_client_mock_test_fixture_clean_up(&fixture->protocol_context.mqtt5_fixture); |
| 261 |
} else { |
| 262 |
aws_test311_clean_up_mqtt_server_fn( |
| 263 |
fixture->allocator, |
| 264 |
fixture->protocol_context.mqtt311_fixture.mqtt311_test_context_setup_result, |
| 265 |
&fixture->protocol_context.mqtt311_fixture.mqtt311_test_context); |
| 266 |
} |
| 267 |
|
| 268 |
for (size_t i = 0; i < aws_array_list_length(&fixture->subscription_events); ++i) { |
| 269 |
struct request_response_protocol_adapter_subscription_event_record record; |
| 270 |
aws_array_list_get_at(&fixture->subscription_events, &record, i); |
| 271 |
s_request_response_protocol_adapter_subscription_event_record_cleanup(&record); |
| 272 |
} |
| 273 |
aws_array_list_clean_up(&fixture->subscription_events); |
| 274 |
|
| 275 |
for (size_t i = 0; i < aws_array_list_length(&fixture->incoming_publish_events); ++i) { |
| 276 |
struct request_response_protocol_adapter_incoming_publish_event_record record; |
| 277 |
aws_array_list_get_at(&fixture->incoming_publish_events, &record, i); |
| 278 |
s_request_response_protocol_adapter_incoming_publish_event_record_clean_up(&record); |
| 279 |
} |
| 280 |
aws_array_list_clean_up(&fixture->incoming_publish_events); |
| 281 |
|
| 282 |
aws_array_list_clean_up(&fixture->connection_events); |
| 283 |
aws_array_list_clean_up(&fixture->publish_results); |
| 284 |
|
| 285 |
aws_mutex_clean_up(&fixture->lock); |
| 286 |
aws_condition_variable_clean_up(&fixture->signal); |
| 287 |
} |
| 288 |
|
| 289 |
struct test_subscription_event_wait_context { |
| 290 |
struct request_response_protocol_adapter_subscription_event_record *expected_event; |
| 291 |
size_t expected_count; |
| 292 |
struct aws_request_response_protocol_adapter_test_fixture *fixture; |
| 293 |
}; |
| 294 |
|
| 295 |
static bool s_do_subscription_events_contain(void *context) { |
| 296 |
struct test_subscription_event_wait_context *wait_context = context; |
| 297 |
|
| 298 |
size_t found = 0; |
| 299 |
|
| 300 |
size_t num_events = aws_array_list_length(&wait_context->fixture->subscription_events); |
| 301 |
for (size_t i = 0; i < num_events; ++i) { |
| 302 |
struct request_response_protocol_adapter_subscription_event_record record; |
| 303 |
aws_array_list_get_at(&wait_context->fixture->subscription_events, &record, i); |
| 304 |
|
| 305 |
if (record.event_type != wait_context->expected_event->event_type) { |
| 306 |
continue; |
| 307 |
} |
| 308 |
|
| 309 |
if (record.error_code != wait_context->expected_event->error_code) { |
| 310 |
continue; |
| 311 |
} |
| 312 |
|
| 313 |
struct aws_byte_cursor record_topic_filter = aws_byte_cursor_from_buf(&record.topic_filter); |
| 314 |
struct aws_byte_cursor expected_topic_filter = |
| 315 |
aws_byte_cursor_from_buf(&wait_context->expected_event->topic_filter); |
| 316 |
if (!aws_byte_cursor_eq(&record_topic_filter, &expected_topic_filter)) { |
| 317 |
continue; |
| 318 |
} |
| 319 |
|
| 320 |
if (record.retryable != wait_context->expected_event->retryable) { |
| 321 |
continue; |
| 322 |
} |
| 323 |
|
| 324 |
++found; |
| 325 |
} |
| 326 |
|
| 327 |
return found >= wait_context->expected_count; |
| 328 |
} |
| 329 |
|
| 330 |
static void s_wait_for_subscription_events_contains( |
| 331 |
struct aws_request_response_protocol_adapter_test_fixture *fixture, |
| 332 |
struct request_response_protocol_adapter_subscription_event_record *expected_event, |
| 333 |
size_t expected_count) { |
| 334 |
|
| 335 |
struct test_subscription_event_wait_context context = { |
| 336 |
.expected_event = expected_event, |
| 337 |
.expected_count = expected_count, |
| 338 |
.fixture = fixture, |
| 339 |
}; |
| 340 |
|
| 341 |
aws_mutex_lock(&fixture->lock); |
| 342 |
aws_condition_variable_wait_pred(&fixture->signal, &fixture->lock, s_do_subscription_events_contain, &context); |
| 343 |
aws_mutex_unlock(&fixture->lock); |
| 344 |
} |
| 345 |
|
| 346 |
struct test_connection_event_wait_context { |
| 347 |
struct aws_protocol_adapter_connection_event *expected_event; |
| 348 |
size_t expected_count; |
| 349 |
struct aws_request_response_protocol_adapter_test_fixture *fixture; |
| 350 |
}; |
| 351 |
|
| 352 |
static bool s_do_connection_events_contain(void *context) { |
| 353 |
struct test_connection_event_wait_context *wait_context = context; |
| 354 |
|
| 355 |
size_t found = 0; |
| 356 |
|
| 357 |
size_t num_events = aws_array_list_length(&wait_context->fixture->connection_events); |
| 358 |
for (size_t i = 0; i < num_events; ++i) { |
| 359 |
struct aws_protocol_adapter_connection_event record; |
| 360 |
aws_array_list_get_at(&wait_context->fixture->connection_events, &record, i); |
| 361 |
|
| 362 |
if (record.event_type != wait_context->expected_event->event_type) { |
| 363 |
continue; |
| 364 |
} |
| 365 |
|
| 366 |
if (record.joined_session != wait_context->expected_event->joined_session) { |
| 367 |
continue; |
| 368 |
} |
| 369 |
|
| 370 |
++found; |
| 371 |
} |
| 372 |
|
| 373 |
return found >= wait_context->expected_count; |
| 374 |
} |
| 375 |
|
| 376 |
static void s_wait_for_connection_events_contains( |
| 377 |
struct aws_request_response_protocol_adapter_test_fixture *fixture, |
| 378 |
struct aws_protocol_adapter_connection_event *expected_event, |
| 379 |
size_t expected_count) { |
| 380 |
|
| 381 |
struct test_connection_event_wait_context context = { |
| 382 |
.expected_event = expected_event, |
| 383 |
.expected_count = expected_count, |
| 384 |
.fixture = fixture, |
| 385 |
}; |
| 386 |
|
| 387 |
aws_mutex_lock(&fixture->lock); |
| 388 |
aws_condition_variable_wait_pred(&fixture->signal, &fixture->lock, s_do_connection_events_contain, &context); |
| 389 |
aws_mutex_unlock(&fixture->lock); |
| 390 |
} |
| 391 |
|
| 392 |
struct test_incoming_publish_event_wait_context { |
| 393 |
struct request_response_protocol_adapter_incoming_publish_event_record *expected_event; |
| 394 |
size_t expected_count; |
| 395 |
struct aws_request_response_protocol_adapter_test_fixture *fixture; |
| 396 |
}; |
| 397 |
|
| 398 |
static bool s_do_incoming_publish_events_contain(void *context) { |
| 399 |
struct test_incoming_publish_event_wait_context *wait_context = context; |
| 400 |
|
| 401 |
size_t found = 0; |
| 402 |
|
| 403 |
size_t num_events = aws_array_list_length(&wait_context->fixture->incoming_publish_events); |
| 404 |
for (size_t i = 0; i < num_events; ++i) { |
| 405 |
struct request_response_protocol_adapter_incoming_publish_event_record record; |
| 406 |
aws_array_list_get_at(&wait_context->fixture->incoming_publish_events, &record, i); |
| 407 |
|
| 408 |
struct aws_byte_cursor record_topic = aws_byte_cursor_from_buf(&record.topic); |
| 409 |
struct aws_byte_cursor expected_topic = aws_byte_cursor_from_buf(&wait_context->expected_event->topic); |
| 410 |
if (!aws_byte_cursor_eq(&record_topic, &expected_topic)) { |
| 411 |
continue; |
| 412 |
} |
| 413 |
|
| 414 |
struct aws_byte_cursor record_payload = aws_byte_cursor_from_buf(&record.payload); |
| 415 |
struct aws_byte_cursor expected_payload = aws_byte_cursor_from_buf(&wait_context->expected_event->payload); |
| 416 |
if (!aws_byte_cursor_eq(&record_payload, &expected_payload)) { |
| 417 |
continue; |
| 418 |
} |
| 419 |
|
| 420 |
++found; |
| 421 |
} |
| 422 |
|
| 423 |
return found >= wait_context->expected_count; |
| 424 |
} |
| 425 |
|
| 426 |
static void s_wait_for_incoming_publish_events_contains( |
| 427 |
struct aws_request_response_protocol_adapter_test_fixture *fixture, |
| 428 |
struct request_response_protocol_adapter_incoming_publish_event_record *expected_event, |
| 429 |
size_t expected_count) { |
| 430 |
|
| 431 |
struct test_incoming_publish_event_wait_context context = { |
| 432 |
.expected_event = expected_event, |
| 433 |
.expected_count = expected_count, |
| 434 |
.fixture = fixture, |
| 435 |
}; |
| 436 |
|
| 437 |
aws_mutex_lock(&fixture->lock); |
| 438 |
aws_condition_variable_wait_pred(&fixture->signal, &fixture->lock, s_do_incoming_publish_events_contain, &context); |
| 439 |
aws_mutex_unlock(&fixture->lock); |
| 440 |
} |
| 441 |
|
| 442 |
struct test_publish_result_wait_context { |
| 443 |
int expected_error_code; |
| 444 |
size_t expected_count; |
| 445 |
struct aws_request_response_protocol_adapter_test_fixture *fixture; |
| 446 |
}; |
| 447 |
|
| 448 |
static bool s_do_publish_results_contain(void *context) { |
| 449 |
struct test_publish_result_wait_context *wait_context = context; |
| 450 |
|
| 451 |
size_t found = 0; |
| 452 |
|
| 453 |
size_t num_events = aws_array_list_length(&wait_context->fixture->publish_results); |
| 454 |
for (size_t i = 0; i < num_events; ++i) { |
| 455 |
int error_code = AWS_ERROR_SUCCESS; |
| 456 |
aws_array_list_get_at(&wait_context->fixture->publish_results, &error_code, i); |
| 457 |
|
| 458 |
if (error_code == wait_context->expected_error_code) { |
| 459 |
++found; |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
return found >= wait_context->expected_count; |
| 464 |
} |
| 465 |
|
| 466 |
static void s_wait_for_publish_results_contains( |
| 467 |
struct aws_request_response_protocol_adapter_test_fixture *fixture, |
| 468 |
int expected_error_code, |
| 469 |
size_t expected_count) { |
| 470 |
|
| 471 |
struct test_publish_result_wait_context context = { |
| 472 |
.expected_error_code = expected_error_code, |
| 473 |
.expected_count = expected_count, |
| 474 |
.fixture = fixture, |
| 475 |
}; |
| 476 |
|
| 477 |
aws_mutex_lock(&fixture->lock); |
| 478 |
aws_condition_variable_wait_pred(&fixture->signal, &fixture->lock, s_do_publish_results_contain, &context); |
| 479 |
aws_mutex_unlock(&fixture->lock); |
| 480 |
} |
| 481 |
|
| 482 |
enum protocol_adapter_operation_test_type { |
| 483 |
PAOTT_SUCCESS, |
| 484 |
PAOTT_FAILURE_TIMEOUT, |
| 485 |
PAOTT_FAILURE_REASON_CODE_RETRYABLE, |
| 486 |
PAOTT_FAILURE_REASON_CODE_NOT_RETRYABLE, |
| 487 |
PAOTT_FAILURE_ERROR_CODE, |
| 488 |
}; |
| 489 |
|
| 490 |
static enum aws_mqtt5_suback_reason_code s_failed_suback_reason_codes[] = { |
| 491 |
AWS_MQTT5_SARC_NOT_AUTHORIZED, |
| 492 |
}; |
| 493 |
|
| 494 |
static int s_aws_mqtt5_server_send_failed_suback_on_subscribe( |
| 495 |
void *packet, |
| 496 |
struct aws_mqtt5_server_mock_connection_context *connection, |
| 497 |
void *user_data) { |
| 498 |
(void)packet; |
| 499 |
(void)user_data; |
| 500 |
|
| 501 |
struct aws_mqtt5_packet_subscribe_view *subscribe_view = packet; |
| 502 |
|
| 503 |
struct aws_mqtt5_packet_suback_view suback_view = { |
| 504 |
.packet_id = subscribe_view->packet_id, |
| 505 |
.reason_code_count = AWS_ARRAY_SIZE(s_failed_suback_reason_codes), |
| 506 |
.reason_codes = s_failed_suback_reason_codes, |
| 507 |
}; |
| 508 |
|
| 509 |
return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_SUBACK, &suback_view); |
| 510 |
} |
| 511 |
|
| 512 |
static int s_test_type_to_expected_error_code( |
| 513 |
enum protocol_adapter_operation_test_type test_type, |
| 514 |
enum aws_protocol_adapter_protocol_type protocol_type) { |
| 515 |
if (protocol_type == AWS_PAPT_MQTT5) { |
| 516 |
switch (test_type) { |
| 517 |
case PAOTT_FAILURE_TIMEOUT: |
| 518 |
return AWS_ERROR_MQTT_TIMEOUT; |
| 519 |
case PAOTT_FAILURE_REASON_CODE_RETRYABLE: |
| 520 |
case PAOTT_FAILURE_REASON_CODE_NOT_RETRYABLE: |
| 521 |
return AWS_ERROR_MQTT_PROTOCOL_ADAPTER_FAILING_REASON_CODE; |
| 522 |
case PAOTT_FAILURE_ERROR_CODE: |
| 523 |
return AWS_ERROR_MQTT5_OPERATION_FAILED_DUE_TO_OFFLINE_QUEUE_POLICY; |
| 524 |
default: |
| 525 |
return AWS_ERROR_SUCCESS; |
| 526 |
} |
| 527 |
} else { |
| 528 |
switch (test_type) { |
| 529 |
case PAOTT_FAILURE_TIMEOUT: |
| 530 |
return AWS_ERROR_MQTT_TIMEOUT; |
| 531 |
case PAOTT_FAILURE_REASON_CODE_RETRYABLE: |
| 532 |
case PAOTT_FAILURE_REASON_CODE_NOT_RETRYABLE: |
| 533 |
return AWS_ERROR_MQTT_PROTOCOL_ADAPTER_FAILING_REASON_CODE; |
| 534 |
case PAOTT_FAILURE_ERROR_CODE: |
| 535 |
return AWS_ERROR_MQTT5_OPERATION_FAILED_DUE_TO_OFFLINE_QUEUE_POLICY; |
| 536 |
default: |
| 537 |
return AWS_ERROR_SUCCESS; |
| 538 |
} |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
static int s_do_request_response_mqtt5_protocol_adapter_subscribe_test( |
| 543 |
struct aws_allocator *allocator, |
| 544 |
enum protocol_adapter_operation_test_type test_type) { |
| 545 |
aws_mqtt_library_init(allocator); |
| 546 |
|
| 547 |
struct mqtt5_client_test_options test_options; |
| 548 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 549 |
|
| 550 |
if (test_type == PAOTT_SUCCESS) { |
| 551 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] = |
| 552 |
aws_mqtt5_server_send_suback_on_subscribe; |
| 553 |
} else if ( |
| 554 |
test_type == PAOTT_FAILURE_REASON_CODE_RETRYABLE || test_type == PAOTT_FAILURE_REASON_CODE_NOT_RETRYABLE) { |
| 555 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] = |
| 556 |
s_aws_mqtt5_server_send_failed_suback_on_subscribe; |
| 557 |
} |
| 558 |
|
| 559 |
if (test_type == PAOTT_FAILURE_ERROR_CODE) { |
| 560 |
test_options.client_options.offline_queue_behavior = AWS_MQTT5_COQBT_FAIL_ALL_ON_DISCONNECT; |
| 561 |
} |
| 562 |
|
| 563 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options mqtt5_test_fixture_options = { |
| 564 |
.client_options = &test_options.client_options, |
| 565 |
.server_function_table = &test_options.server_function_table, |
| 566 |
}; |
| 567 |
|
| 568 |
struct aws_request_response_protocol_adapter_test_fixture fixture; |
| 569 |
ASSERT_SUCCESS(s_aws_request_response_protocol_adapter_test_fixture_init_mqtt5( |
| 570 |
&fixture, allocator, &mqtt5_test_fixture_options)); |
| 571 |
|
| 572 |
struct aws_mqtt5_client *client = fixture.protocol_context.mqtt5_fixture.client; |
| 573 |
|
| 574 |
if (test_type != PAOTT_FAILURE_ERROR_CODE) { |
| 575 |
ASSERT_SUCCESS(aws_mqtt5_client_start(client)); |
| 576 |
|
| 577 |
aws_wait_for_connected_lifecycle_event(&fixture.protocol_context.mqtt5_fixture); |
| 578 |
} |
| 579 |
|
| 580 |
int expected_error_code = s_test_type_to_expected_error_code(test_type, AWS_PAPT_MQTT5); |
| 581 |
|
| 582 |
struct request_response_protocol_adapter_subscription_event_record expected_outcome; |
| 583 |
s_request_response_protocol_adapter_subscription_event_record_init( |
| 584 |
&expected_outcome, |
| 585 |
allocator, |
| 586 |
AWS_PASET_SUBSCRIBE, |
| 587 |
aws_byte_cursor_from_c_str("hello/world"), |
| 588 |
expected_error_code, |
| 589 |
test_type != PAOTT_FAILURE_REASON_CODE_NOT_RETRYABLE); |
| 590 |
|
| 591 |
struct aws_protocol_adapter_subscribe_options subscribe_options = { |
| 592 |
.topic_filter = aws_byte_cursor_from_buf(&expected_outcome.topic_filter), |
| 593 |
.ack_timeout_seconds = 2, |
| 594 |
}; |
| 595 |
|
| 596 |
aws_mqtt_protocol_adapter_subscribe(fixture.protocol_adapter, &subscribe_options); |
| 597 |
|
| 598 |
s_wait_for_subscription_events_contains(&fixture, &expected_outcome, 1); |
| 599 |
|
| 600 |
s_request_response_protocol_adapter_subscription_event_record_cleanup(&expected_outcome); |
| 601 |
|
| 602 |
s_aws_request_response_protocol_adapter_test_fixture_clean_up(&fixture); |
| 603 |
|
| 604 |
aws_mqtt_library_clean_up(); |
| 605 |
|
| 606 |
return AWS_OP_SUCCESS; |
| 607 |
} |
| 608 |
|
| 609 |
static int s_request_response_mqtt5_protocol_adapter_subscribe_success_fn(struct aws_allocator *allocator, void *ctx) { |
| 610 |
(void)ctx; |
| 611 |
|
| 612 |
ASSERT_SUCCESS(s_do_request_response_mqtt5_protocol_adapter_subscribe_test(allocator, PAOTT_SUCCESS)); |
| 613 |
|
| 614 |
return AWS_OP_SUCCESS; |
| 615 |
} |
| 616 |
|
| 617 |
AWS_TEST_CASE( |
| 618 |
request_response_mqtt5_protocol_adapter_subscribe_success, |
| 619 |
s_request_response_mqtt5_protocol_adapter_subscribe_success_fn) |
| 620 |
|
| 621 |
static int s_request_response_mqtt5_protocol_adapter_subscribe_failure_timeout_fn( |
| 622 |
struct aws_allocator *allocator, |
| 623 |
void *ctx) { |
| 624 |
(void)ctx; |
| 625 |
|
| 626 |
ASSERT_SUCCESS(s_do_request_response_mqtt5_protocol_adapter_subscribe_test(allocator, PAOTT_FAILURE_TIMEOUT)); |
| 627 |
|
| 628 |
return AWS_OP_SUCCESS; |
| 629 |
} |
| 630 |
|
| 631 |
AWS_TEST_CASE( |
| 632 |
request_response_mqtt5_protocol_adapter_subscribe_failure_timeout, |
| 633 |
s_request_response_mqtt5_protocol_adapter_subscribe_failure_timeout_fn) |
| 634 |
|
| 635 |
static int s_request_response_mqtt5_protocol_adapter_subscribe_failure_reason_code_fn( |
| 636 |
struct aws_allocator *allocator, |
| 637 |
void *ctx) { |
| 638 |
(void)ctx; |
| 639 |
|
| 640 |
ASSERT_SUCCESS(s_do_request_response_mqtt5_protocol_adapter_subscribe_test( |
| 641 |
allocator, PAOTT_FAILURE_REASON_CODE_NOT_RETRYABLE)); |
| 642 |
|
| 643 |
return AWS_OP_SUCCESS; |
| 644 |
} |
| 645 |
|
| 646 |
AWS_TEST_CASE( |
| 647 |
request_response_mqtt5_protocol_adapter_subscribe_failure_reason_code, |
| 648 |
s_request_response_mqtt5_protocol_adapter_subscribe_failure_reason_code_fn) |
| 649 |
|
| 650 |
static int s_request_response_mqtt5_protocol_adapter_subscribe_failure_error_code_fn( |
| 651 |
struct aws_allocator *allocator, |
| 652 |
void *ctx) { |
| 653 |
(void)ctx; |
| 654 |
|
| 655 |
ASSERT_SUCCESS(s_do_request_response_mqtt5_protocol_adapter_subscribe_test(allocator, PAOTT_FAILURE_ERROR_CODE)); |
| 656 |
|
| 657 |
return AWS_OP_SUCCESS; |
| 658 |
} |
| 659 |
|
| 660 |
AWS_TEST_CASE( |
| 661 |
request_response_mqtt5_protocol_adapter_subscribe_failure_error_code, |
| 662 |
s_request_response_mqtt5_protocol_adapter_subscribe_failure_error_code_fn) |
| 663 |
|
| 664 |
static enum aws_mqtt5_unsuback_reason_code s_retryable_failed_unsuback_reason_codes[] = { |
| 665 |
AWS_MQTT5_UARC_IMPLEMENTATION_SPECIFIC_ERROR, |
| 666 |
}; |
| 667 |
|
| 668 |
static int s_aws_mqtt5_server_send_retryable_failed_unsuback_on_unsubscribe( |
| 669 |
void *packet, |
| 670 |
struct aws_mqtt5_server_mock_connection_context *connection, |
| 671 |
void *user_data) { |
| 672 |
(void)packet; |
| 673 |
(void)user_data; |
| 674 |
|
| 675 |
struct aws_mqtt5_packet_subscribe_view *unsubscribe_view = packet; |
| 676 |
|
| 677 |
struct aws_mqtt5_packet_unsuback_view unsuback_view = { |
| 678 |
.packet_id = unsubscribe_view->packet_id, |
| 679 |
.reason_code_count = AWS_ARRAY_SIZE(s_retryable_failed_unsuback_reason_codes), |
| 680 |
.reason_codes = s_retryable_failed_unsuback_reason_codes, |
| 681 |
}; |
| 682 |
|
| 683 |
return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_UNSUBACK, &unsuback_view); |
| 684 |
} |
| 685 |
|
| 686 |
static enum aws_mqtt5_unsuback_reason_code s_not_retryable_failed_unsuback_reason_codes[] = { |
| 687 |
AWS_MQTT5_UARC_NOT_AUTHORIZED, |
| 688 |
}; |
| 689 |
|
| 690 |
static int s_aws_mqtt5_server_send_not_retryable_failed_unsuback_on_unsubscribe( |
| 691 |
void *packet, |
| 692 |
struct aws_mqtt5_server_mock_connection_context *connection, |
| 693 |
void *user_data) { |
| 694 |
(void)packet; |
| 695 |
(void)user_data; |
| 696 |
|
| 697 |
struct aws_mqtt5_packet_subscribe_view *unsubscribe_view = packet; |
| 698 |
|
| 699 |
struct aws_mqtt5_packet_unsuback_view unsuback_view = { |
| 700 |
.packet_id = unsubscribe_view->packet_id, |
| 701 |
.reason_code_count = AWS_ARRAY_SIZE(s_not_retryable_failed_unsuback_reason_codes), |
| 702 |
.reason_codes = s_not_retryable_failed_unsuback_reason_codes, |
| 703 |
}; |
| 704 |
|
| 705 |
return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_UNSUBACK, &unsuback_view); |
| 706 |
} |
| 707 |
|
| 708 |
static int s_do_request_response_mqtt5_protocol_adapter_unsubscribe_test( |
| 709 |
struct aws_allocator *allocator, |
| 710 |
enum protocol_adapter_operation_test_type test_type) { |
| 711 |
aws_mqtt_library_init(allocator); |
| 712 |
|
| 713 |
struct mqtt5_client_test_options test_options; |
| 714 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 715 |
|
| 716 |
if (test_type == PAOTT_SUCCESS) { |
| 717 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_UNSUBSCRIBE] = |
| 718 |
aws_mqtt5_mock_server_handle_unsubscribe_unsuback_success; |
| 719 |
} else if (test_type == PAOTT_FAILURE_REASON_CODE_RETRYABLE) { |
| 720 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_UNSUBSCRIBE] = |
| 721 |
s_aws_mqtt5_server_send_retryable_failed_unsuback_on_unsubscribe; |
| 722 |
} else if (test_type == PAOTT_FAILURE_REASON_CODE_NOT_RETRYABLE) { |
| 723 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_UNSUBSCRIBE] = |
| 724 |
s_aws_mqtt5_server_send_not_retryable_failed_unsuback_on_unsubscribe; |
| 725 |
} |
| 726 |
|
| 727 |
if (test_type == PAOTT_FAILURE_ERROR_CODE) { |
| 728 |
test_options.client_options.offline_queue_behavior = AWS_MQTT5_COQBT_FAIL_ALL_ON_DISCONNECT; |
| 729 |
} |
| 730 |
|
| 731 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options mqtt5_test_fixture_options = { |
| 732 |
.client_options = &test_options.client_options, |
| 733 |
.server_function_table = &test_options.server_function_table, |
| 734 |
}; |
| 735 |
|
| 736 |
struct aws_request_response_protocol_adapter_test_fixture fixture; |
| 737 |
ASSERT_SUCCESS(s_aws_request_response_protocol_adapter_test_fixture_init_mqtt5( |
| 738 |
&fixture, allocator, &mqtt5_test_fixture_options)); |
| 739 |
|
| 740 |
struct aws_mqtt5_client *client = fixture.protocol_context.mqtt5_fixture.client; |
| 741 |
|
| 742 |
if (test_type != PAOTT_FAILURE_ERROR_CODE) { |
| 743 |
ASSERT_SUCCESS(aws_mqtt5_client_start(client)); |
| 744 |
|
| 745 |
aws_wait_for_connected_lifecycle_event(&fixture.protocol_context.mqtt5_fixture); |
| 746 |
} |
| 747 |
|
| 748 |
int expected_error_code = s_test_type_to_expected_error_code(test_type, AWS_PAPT_MQTT5); |
| 749 |
|
| 750 |
struct request_response_protocol_adapter_subscription_event_record expected_outcome; |
| 751 |
s_request_response_protocol_adapter_subscription_event_record_init( |
| 752 |
&expected_outcome, |
| 753 |
allocator, |
| 754 |
AWS_PASET_UNSUBSCRIBE, |
| 755 |
aws_byte_cursor_from_c_str("hello/world"), |
| 756 |
expected_error_code, |
| 757 |
test_type == PAOTT_FAILURE_REASON_CODE_RETRYABLE || test_type == PAOTT_FAILURE_TIMEOUT); |
| 758 |
|
| 759 |
struct aws_protocol_adapter_unsubscribe_options unsubscribe_options = { |
| 760 |
.topic_filter = aws_byte_cursor_from_buf(&expected_outcome.topic_filter), |
| 761 |
.ack_timeout_seconds = 2, |
| 762 |
}; |
| 763 |
|
| 764 |
aws_mqtt_protocol_adapter_unsubscribe(fixture.protocol_adapter, &unsubscribe_options); |
| 765 |
|
| 766 |
s_wait_for_subscription_events_contains(&fixture, &expected_outcome, 1); |
| 767 |
|
| 768 |
s_request_response_protocol_adapter_subscription_event_record_cleanup(&expected_outcome); |
| 769 |
|
| 770 |
s_aws_request_response_protocol_adapter_test_fixture_clean_up(&fixture); |
| 771 |
|
| 772 |
aws_mqtt_library_clean_up(); |
| 773 |
|
| 774 |
return AWS_OP_SUCCESS; |
| 775 |
} |
| 776 |
|
| 777 |
static int s_request_response_mqtt5_protocol_adapter_unsubscribe_success_fn( |
| 778 |
struct aws_allocator *allocator, |
| 779 |
void *ctx) { |
| 780 |
(void)ctx; |
| 781 |
|
| 782 |
ASSERT_SUCCESS(s_do_request_response_mqtt5_protocol_adapter_unsubscribe_test(allocator, PAOTT_SUCCESS)); |
| 783 |
|
| 784 |
return AWS_OP_SUCCESS; |
| 785 |
} |
| 786 |
|
| 787 |
AWS_TEST_CASE( |
| 788 |
request_response_mqtt5_protocol_adapter_unsubscribe_success, |
| 789 |
s_request_response_mqtt5_protocol_adapter_unsubscribe_success_fn) |
| 790 |
|
| 791 |
static int s_request_response_mqtt5_protocol_adapter_unsubscribe_failure_timeout_fn( |
| 792 |
struct aws_allocator *allocator, |
| 793 |
void *ctx) { |
| 794 |
(void)ctx; |
| 795 |
|
| 796 |
ASSERT_SUCCESS(s_do_request_response_mqtt5_protocol_adapter_unsubscribe_test(allocator, PAOTT_FAILURE_TIMEOUT)); |
| 797 |
|
| 798 |
return AWS_OP_SUCCESS; |
| 799 |
} |
| 800 |
|
| 801 |
AWS_TEST_CASE( |
| 802 |
request_response_mqtt5_protocol_adapter_unsubscribe_failure_timeout, |
| 803 |
s_request_response_mqtt5_protocol_adapter_unsubscribe_failure_timeout_fn) |
| 804 |
|
| 805 |
static int s_request_response_mqtt5_protocol_adapter_unsubscribe_failure_reason_code_retryable_fn( |
| 806 |
struct aws_allocator *allocator, |
| 807 |
void *ctx) { |
| 808 |
(void)ctx; |
| 809 |
|
| 810 |
ASSERT_SUCCESS( |
| 811 |
s_do_request_response_mqtt5_protocol_adapter_unsubscribe_test(allocator, PAOTT_FAILURE_REASON_CODE_RETRYABLE)); |
| 812 |
|
| 813 |
return AWS_OP_SUCCESS; |
| 814 |
} |
| 815 |
|
| 816 |
AWS_TEST_CASE( |
| 817 |
request_response_mqtt5_protocol_adapter_unsubscribe_failure_reason_code_retryable, |
| 818 |
s_request_response_mqtt5_protocol_adapter_unsubscribe_failure_reason_code_retryable_fn) |
| 819 |
|
| 820 |
static int s_request_response_mqtt5_protocol_adapter_unsubscribe_failure_reason_code_not_retryable_fn( |
| 821 |
struct aws_allocator *allocator, |
| 822 |
void *ctx) { |
| 823 |
(void)ctx; |
| 824 |
|
| 825 |
ASSERT_SUCCESS(s_do_request_response_mqtt5_protocol_adapter_unsubscribe_test( |
| 826 |
allocator, PAOTT_FAILURE_REASON_CODE_NOT_RETRYABLE)); |
| 827 |
|
| 828 |
return AWS_OP_SUCCESS; |
| 829 |
} |
| 830 |
|
| 831 |
AWS_TEST_CASE( |
| 832 |
request_response_mqtt5_protocol_adapter_unsubscribe_failure_reason_code_not_retryable, |
| 833 |
s_request_response_mqtt5_protocol_adapter_unsubscribe_failure_reason_code_not_retryable_fn) |
| 834 |
|
| 835 |
static int s_request_response_mqtt5_protocol_adapter_unsubscribe_failure_error_code_fn( |
| 836 |
struct aws_allocator *allocator, |
| 837 |
void *ctx) { |
| 838 |
(void)ctx; |
| 839 |
|
| 840 |
ASSERT_SUCCESS(s_do_request_response_mqtt5_protocol_adapter_unsubscribe_test(allocator, PAOTT_FAILURE_ERROR_CODE)); |
| 841 |
|
| 842 |
return AWS_OP_SUCCESS; |
| 843 |
} |
| 844 |
|
| 845 |
AWS_TEST_CASE( |
| 846 |
request_response_mqtt5_protocol_adapter_unsubscribe_failure_error_code, |
| 847 |
s_request_response_mqtt5_protocol_adapter_unsubscribe_failure_error_code_fn) |
| 848 |
|
| 849 |
static int s_aws_mqtt5_server_send_failed_puback_on_publish( |
| 850 |
void *packet, |
| 851 |
struct aws_mqtt5_server_mock_connection_context *connection, |
| 852 |
void *user_data) { |
| 853 |
(void)packet; |
| 854 |
(void)user_data; |
| 855 |
|
| 856 |
struct aws_mqtt5_packet_publish_view *publish_view = packet; |
| 857 |
|
| 858 |
if (publish_view->qos == AWS_MQTT5_QOS_AT_LEAST_ONCE) { |
| 859 |
struct aws_mqtt5_packet_puback_view puback_view = { |
| 860 |
.packet_id = publish_view->packet_id, |
| 861 |
.reason_code = AWS_MQTT5_PARC_NOT_AUTHORIZED, |
| 862 |
}; |
| 863 |
|
| 864 |
return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_PUBACK, &puback_view); |
| 865 |
} |
| 866 |
|
| 867 |
return AWS_OP_SUCCESS; |
| 868 |
} |
| 869 |
|
| 870 |
static int s_do_request_response_mqtt5_protocol_adapter_publish_test( |
| 871 |
struct aws_allocator *allocator, |
| 872 |
enum protocol_adapter_operation_test_type test_type) { |
| 873 |
aws_mqtt_library_init(allocator); |
| 874 |
|
| 875 |
struct mqtt5_client_test_options test_options; |
| 876 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 877 |
|
| 878 |
if (test_type == PAOTT_SUCCESS) { |
| 879 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] = |
| 880 |
aws_mqtt5_mock_server_handle_publish_puback; |
| 881 |
} else if ( |
| 882 |
test_type == PAOTT_FAILURE_REASON_CODE_RETRYABLE || test_type == PAOTT_FAILURE_REASON_CODE_NOT_RETRYABLE) { |
| 883 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] = |
| 884 |
s_aws_mqtt5_server_send_failed_puback_on_publish; |
| 885 |
} |
| 886 |
|
| 887 |
if (test_type == PAOTT_FAILURE_ERROR_CODE) { |
| 888 |
test_options.client_options.offline_queue_behavior = AWS_MQTT5_COQBT_FAIL_ALL_ON_DISCONNECT; |
| 889 |
} |
| 890 |
|
| 891 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options mqtt5_test_fixture_options = { |
| 892 |
.client_options = &test_options.client_options, |
| 893 |
.server_function_table = &test_options.server_function_table, |
| 894 |
}; |
| 895 |
|
| 896 |
struct aws_request_response_protocol_adapter_test_fixture fixture; |
| 897 |
ASSERT_SUCCESS(s_aws_request_response_protocol_adapter_test_fixture_init_mqtt5( |
| 898 |
&fixture, allocator, &mqtt5_test_fixture_options)); |
| 899 |
|
| 900 |
struct aws_mqtt5_client *client = fixture.protocol_context.mqtt5_fixture.client; |
| 901 |
|
| 902 |
if (test_type != PAOTT_FAILURE_ERROR_CODE) { |
| 903 |
ASSERT_SUCCESS(aws_mqtt5_client_start(client)); |
| 904 |
|
| 905 |
aws_wait_for_connected_lifecycle_event(&fixture.protocol_context.mqtt5_fixture); |
| 906 |
} |
| 907 |
|
| 908 |
struct aws_protocol_adapter_publish_options publish_options = { |
| 909 |
.topic = aws_byte_cursor_from_c_str("hello/world"), |
| 910 |
.payload = aws_byte_cursor_from_c_str("SomePayload"), |
| 911 |
.ack_timeout_seconds = 2, |
| 912 |
.completion_callback_fn = s_rr_mqtt_protocol_adapter_test_on_publish_result, |
| 913 |
.user_data = &fixture, |
| 914 |
}; |
| 915 |
|
| 916 |
aws_mqtt_protocol_adapter_publish(fixture.protocol_adapter, &publish_options); |
| 917 |
|
| 918 |
int expected_error_code = s_test_type_to_expected_error_code(test_type, AWS_PAPT_MQTT5); |
| 919 |
s_wait_for_publish_results_contains(&fixture, expected_error_code, 1); |
| 920 |
|
| 921 |
s_aws_request_response_protocol_adapter_test_fixture_clean_up(&fixture); |
| 922 |
|
| 923 |
aws_mqtt_library_clean_up(); |
| 924 |
|
| 925 |
return AWS_OP_SUCCESS; |
| 926 |
} |
| 927 |
|
| 928 |
static int s_request_response_mqtt5_protocol_adapter_publish_success_fn(struct aws_allocator *allocator, void *ctx) { |
| 929 |
(void)ctx; |
| 930 |
|
| 931 |
ASSERT_SUCCESS(s_do_request_response_mqtt5_protocol_adapter_publish_test(allocator, PAOTT_SUCCESS)); |
| 932 |
|
| 933 |
return AWS_OP_SUCCESS; |
| 934 |
} |
| 935 |
|
| 936 |
AWS_TEST_CASE( |
| 937 |
request_response_mqtt5_protocol_adapter_publish_success, |
| 938 |
s_request_response_mqtt5_protocol_adapter_publish_success_fn) |
| 939 |
|
| 940 |
static int s_request_response_mqtt5_protocol_adapter_publish_failure_timeout_fn( |
| 941 |
struct aws_allocator *allocator, |
| 942 |
void *ctx) { |
| 943 |
(void)ctx; |
| 944 |
|
| 945 |
ASSERT_SUCCESS(s_do_request_response_mqtt5_protocol_adapter_publish_test(allocator, PAOTT_FAILURE_TIMEOUT)); |
| 946 |
|
| 947 |
return AWS_OP_SUCCESS; |
| 948 |
} |
| 949 |
|
| 950 |
AWS_TEST_CASE( |
| 951 |
request_response_mqtt5_protocol_adapter_publish_failure_timeout, |
| 952 |
s_request_response_mqtt5_protocol_adapter_publish_failure_timeout_fn) |
| 953 |
|
| 954 |
static int s_request_response_mqtt5_protocol_adapter_publish_failure_reason_code_fn( |
| 955 |
struct aws_allocator *allocator, |
| 956 |
void *ctx) { |
| 957 |
(void)ctx; |
| 958 |
|
| 959 |
ASSERT_SUCCESS( |
| 960 |
s_do_request_response_mqtt5_protocol_adapter_publish_test(allocator, PAOTT_FAILURE_REASON_CODE_NOT_RETRYABLE)); |
| 961 |
|
| 962 |
return AWS_OP_SUCCESS; |
| 963 |
} |
| 964 |
|
| 965 |
AWS_TEST_CASE( |
| 966 |
request_response_mqtt5_protocol_adapter_publish_failure_reason_code, |
| 967 |
s_request_response_mqtt5_protocol_adapter_publish_failure_reason_code_fn) |
| 968 |
|
| 969 |
static int s_request_response_mqtt5_protocol_adapter_publish_failure_error_code_fn( |
| 970 |
struct aws_allocator *allocator, |
| 971 |
void *ctx) { |
| 972 |
(void)ctx; |
| 973 |
|
| 974 |
ASSERT_SUCCESS(s_do_request_response_mqtt5_protocol_adapter_publish_test(allocator, PAOTT_FAILURE_ERROR_CODE)); |
| 975 |
|
| 976 |
return AWS_OP_SUCCESS; |
| 977 |
} |
| 978 |
|
| 979 |
AWS_TEST_CASE( |
| 980 |
request_response_mqtt5_protocol_adapter_publish_failure_error_code, |
| 981 |
s_request_response_mqtt5_protocol_adapter_publish_failure_error_code_fn) |
| 982 |
|
| 983 |
static int s_do_request_response_mqtt5_protocol_adapter_connection_event_connect_test( |
| 984 |
struct aws_allocator *allocator, |
| 985 |
bool rejoin_session) { |
| 986 |
aws_mqtt_library_init(allocator); |
| 987 |
|
| 988 |
struct mqtt5_client_test_options test_options; |
| 989 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 990 |
|
| 991 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] = |
| 992 |
aws_mqtt5_mock_server_handle_connect_honor_session_unconditional; |
| 993 |
test_options.client_options.session_behavior = rejoin_session ? AWS_MQTT5_CSBT_REJOIN_ALWAYS : AWS_MQTT5_CSBT_CLEAN; |
| 994 |
|
| 995 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options mqtt5_test_fixture_options = { |
| 996 |
.client_options = &test_options.client_options, |
| 997 |
.server_function_table = &test_options.server_function_table, |
| 998 |
}; |
| 999 |
|
| 1000 |
struct aws_request_response_protocol_adapter_test_fixture fixture; |
| 1001 |
ASSERT_SUCCESS(s_aws_request_response_protocol_adapter_test_fixture_init_mqtt5( |
| 1002 |
&fixture, allocator, &mqtt5_test_fixture_options)); |
| 1003 |
|
| 1004 |
struct aws_mqtt5_client *client = fixture.protocol_context.mqtt5_fixture.client; |
| 1005 |
|
| 1006 |
struct aws_protocol_adapter_connection_event expected_connect_record = { |
| 1007 |
.event_type = AWS_PACET_CONNECTED, |
| 1008 |
.joined_session = rejoin_session, |
| 1009 |
}; |
| 1010 |
|
| 1011 |
ASSERT_SUCCESS(aws_mqtt5_client_start(client)); |
| 1012 |
s_wait_for_connection_events_contains(&fixture, &expected_connect_record, 1); |
| 1013 |
|
| 1014 |
ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL)); |
| 1015 |
aws_wait_for_stopped_lifecycle_event(&fixture.protocol_context.mqtt5_fixture); |
| 1016 |
|
| 1017 |
struct aws_protocol_adapter_connection_event expected_disconnect_record = { |
| 1018 |
.event_type = AWS_PACET_DISCONNECTED, |
| 1019 |
}; |
| 1020 |
|
| 1021 |
s_wait_for_connection_events_contains(&fixture, &expected_disconnect_record, 1); |
| 1022 |
|
| 1023 |
s_aws_request_response_protocol_adapter_test_fixture_clean_up(&fixture); |
| 1024 |
|
| 1025 |
aws_mqtt_library_clean_up(); |
| 1026 |
|
| 1027 |
return AWS_OP_SUCCESS; |
| 1028 |
} |
| 1029 |
|
| 1030 |
static int s_request_response_mqtt5_protocol_adapter_connection_event_connect_no_session_fn( |
| 1031 |
struct aws_allocator *allocator, |
| 1032 |
void *ctx) { |
| 1033 |
(void)ctx; |
| 1034 |
|
| 1035 |
return s_do_request_response_mqtt5_protocol_adapter_connection_event_connect_test(allocator, false); |
| 1036 |
} |
| 1037 |
|
| 1038 |
AWS_TEST_CASE( |
| 1039 |
request_response_mqtt5_protocol_adapter_connection_event_connect_no_session, |
| 1040 |
s_request_response_mqtt5_protocol_adapter_connection_event_connect_no_session_fn) |
| 1041 |
|
| 1042 |
static int s_request_response_mqtt5_protocol_adapter_connection_event_connect_session_fn( |
| 1043 |
struct aws_allocator *allocator, |
| 1044 |
void *ctx) { |
| 1045 |
(void)ctx; |
| 1046 |
|
| 1047 |
return s_do_request_response_mqtt5_protocol_adapter_connection_event_connect_test(allocator, true); |
| 1048 |
} |
| 1049 |
|
| 1050 |
AWS_TEST_CASE( |
| 1051 |
request_response_mqtt5_protocol_adapter_connection_event_connect_session, |
| 1052 |
s_request_response_mqtt5_protocol_adapter_connection_event_connect_session_fn) |
| 1053 |
|
| 1054 |
static int s_request_response_mqtt5_protocol_adapter_incoming_publish_fn(struct aws_allocator *allocator, void *ctx) { |
| 1055 |
(void)ctx; |
| 1056 |
|
| 1057 |
aws_mqtt_library_init(allocator); |
| 1058 |
|
| 1059 |
struct mqtt5_client_test_options test_options; |
| 1060 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 1061 |
|
| 1062 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] = |
| 1063 |
aws_mqtt5_mock_server_handle_publish_puback_and_forward; |
| 1064 |
|
| 1065 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options mqtt5_test_fixture_options = { |
| 1066 |
.client_options = &test_options.client_options, |
| 1067 |
.server_function_table = &test_options.server_function_table, |
| 1068 |
}; |
| 1069 |
|
| 1070 |
struct aws_request_response_protocol_adapter_test_fixture fixture; |
| 1071 |
ASSERT_SUCCESS(s_aws_request_response_protocol_adapter_test_fixture_init_mqtt5( |
| 1072 |
&fixture, allocator, &mqtt5_test_fixture_options)); |
| 1073 |
|
| 1074 |
struct aws_mqtt5_client *client = fixture.protocol_context.mqtt5_fixture.client; |
| 1075 |
|
| 1076 |
ASSERT_SUCCESS(aws_mqtt5_client_start(client)); |
| 1077 |
|
| 1078 |
aws_wait_for_connected_lifecycle_event(&fixture.protocol_context.mqtt5_fixture); |
| 1079 |
|
| 1080 |
struct request_response_protocol_adapter_incoming_publish_event_record expected_publish; |
| 1081 |
AWS_ZERO_STRUCT(expected_publish); |
| 1082 |
|
| 1083 |
s_request_response_protocol_adapter_incoming_publish_event_record_init( |
| 1084 |
&expected_publish, |
| 1085 |
allocator, |
| 1086 |
aws_byte_cursor_from_c_str("hello/world"), |
| 1087 |
aws_byte_cursor_from_c_str("SomePayload")); |
| 1088 |
|
| 1089 |
struct aws_protocol_adapter_publish_options publish_options = { |
| 1090 |
.topic = aws_byte_cursor_from_buf(&expected_publish.topic), |
| 1091 |
.payload = aws_byte_cursor_from_buf(&expected_publish.payload), |
| 1092 |
.ack_timeout_seconds = 2, |
| 1093 |
.completion_callback_fn = s_rr_mqtt_protocol_adapter_test_on_publish_result, |
| 1094 |
.user_data = &fixture, |
| 1095 |
}; |
| 1096 |
|
| 1097 |
aws_mqtt_protocol_adapter_publish(fixture.protocol_adapter, &publish_options); |
| 1098 |
|
| 1099 |
s_wait_for_incoming_publish_events_contains(&fixture, &expected_publish, 1); |
| 1100 |
|
| 1101 |
s_request_response_protocol_adapter_incoming_publish_event_record_clean_up(&expected_publish); |
| 1102 |
|
| 1103 |
s_aws_request_response_protocol_adapter_test_fixture_clean_up(&fixture); |
| 1104 |
|
| 1105 |
aws_mqtt_library_clean_up(); |
| 1106 |
|
| 1107 |
return AWS_OP_SUCCESS; |
| 1108 |
} |
| 1109 |
|
| 1110 |
AWS_TEST_CASE( |
| 1111 |
request_response_mqtt5_protocol_adapter_incoming_publish, |
| 1112 |
s_request_response_mqtt5_protocol_adapter_incoming_publish_fn) |
| 1113 |
|
| 1114 |
static int s_request_response_mqtt5_protocol_adapter_shutdown_while_pending_fn( |
| 1115 |
struct aws_allocator *allocator, |
| 1116 |
void *ctx) { |
| 1117 |
(void)ctx; |
| 1118 |
|
| 1119 |
aws_mqtt_library_init(allocator); |
| 1120 |
|
| 1121 |
struct mqtt5_client_test_options test_options; |
| 1122 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 1123 |
|
| 1124 |
|
| 1125 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] = NULL; |
| 1126 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] = NULL; |
| 1127 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_UNSUBSCRIBE] = NULL; |
| 1128 |
|
| 1129 |
test_options.client_options.offline_queue_behavior = AWS_MQTT5_COQBT_FAIL_ALL_ON_DISCONNECT; |
| 1130 |
|
| 1131 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options mqtt5_test_fixture_options = { |
| 1132 |
.client_options = &test_options.client_options, |
| 1133 |
.server_function_table = &test_options.server_function_table, |
| 1134 |
}; |
| 1135 |
|
| 1136 |
struct aws_request_response_protocol_adapter_test_fixture fixture; |
| 1137 |
ASSERT_SUCCESS(s_aws_request_response_protocol_adapter_test_fixture_init_mqtt5( |
| 1138 |
&fixture, allocator, &mqtt5_test_fixture_options)); |
| 1139 |
|
| 1140 |
struct aws_mqtt5_client *client = fixture.protocol_context.mqtt5_fixture.client; |
| 1141 |
|
| 1142 |
ASSERT_SUCCESS(aws_mqtt5_client_start(client)); |
| 1143 |
|
| 1144 |
aws_wait_for_connected_lifecycle_event(&fixture.protocol_context.mqtt5_fixture); |
| 1145 |
|
| 1146 |
|
| 1147 |
struct aws_protocol_adapter_publish_options publish_options = { |
| 1148 |
.topic = aws_byte_cursor_from_c_str("hello/world"), |
| 1149 |
.payload = aws_byte_cursor_from_c_str("SomePayload"), |
| 1150 |
.ack_timeout_seconds = 5, |
| 1151 |
.completion_callback_fn = s_rr_mqtt_protocol_adapter_test_on_publish_result, |
| 1152 |
.user_data = &fixture, |
| 1153 |
}; |
| 1154 |
|
| 1155 |
aws_mqtt_protocol_adapter_publish(fixture.protocol_adapter, &publish_options); |
| 1156 |
|
| 1157 |
|
| 1158 |
struct aws_protocol_adapter_subscribe_options subscribe_options = { |
| 1159 |
.topic_filter = aws_byte_cursor_from_c_str("hello/world"), |
| 1160 |
.ack_timeout_seconds = 5, |
| 1161 |
}; |
| 1162 |
|
| 1163 |
aws_mqtt_protocol_adapter_subscribe(fixture.protocol_adapter, &subscribe_options); |
| 1164 |
|
| 1165 |
|
| 1166 |
struct aws_protocol_adapter_unsubscribe_options unsubscribe_options = { |
| 1167 |
.topic_filter = aws_byte_cursor_from_c_str("hello/world"), |
| 1168 |
.ack_timeout_seconds = 5, |
| 1169 |
}; |
| 1170 |
|
| 1171 |
aws_mqtt_protocol_adapter_unsubscribe(fixture.protocol_adapter, &unsubscribe_options); |
| 1172 |
|
| 1173 |
|
| 1174 |
s_aws_request_response_protocol_adapter_test_fixture_destroy_adapters(&fixture); |
| 1175 |
|
| 1176 |
|
| 1177 |
ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL)); |
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
aws_wait_for_n_lifecycle_events(&fixture.protocol_context.mqtt5_fixture, AWS_MQTT5_CLET_STOPPED, 1); |
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
s_aws_request_response_protocol_adapter_test_fixture_clean_up(&fixture); |
| 1186 |
|
| 1187 |
aws_mqtt_library_clean_up(); |
| 1188 |
|
| 1189 |
return AWS_OP_SUCCESS; |
| 1190 |
} |
| 1191 |
|
| 1192 |
AWS_TEST_CASE( |
| 1193 |
request_response_mqtt5_protocol_adapter_shutdown_while_pending, |
| 1194 |
s_request_response_mqtt5_protocol_adapter_shutdown_while_pending_fn) |
| 1195 |
|
| 1196 |
static int s_do_request_response_mqtt311_protocol_adapter_subscribe_test( |
| 1197 |
struct aws_allocator *allocator, |
| 1198 |
enum protocol_adapter_operation_test_type test_type) { |
| 1199 |
aws_mqtt_library_init(allocator); |
| 1200 |
|
| 1201 |
struct aws_request_response_protocol_adapter_test_fixture fixture; |
| 1202 |
ASSERT_SUCCESS(s_aws_request_response_protocol_adapter_test_fixture_init_mqtt311(&fixture, allocator)); |
| 1203 |
|
| 1204 |
struct aws_mqtt_client_connection *connection = |
| 1205 |
fixture.protocol_context.mqtt311_fixture.mqtt311_test_context.mqtt_connection; |
| 1206 |
|
| 1207 |
struct mqtt_connection_state_test *test_context_311 = |
| 1208 |
&fixture.protocol_context.mqtt311_fixture.mqtt311_test_context; |
| 1209 |
|
| 1210 |
switch (test_type) { |
| 1211 |
case PAOTT_SUCCESS: |
| 1212 |
mqtt_mock_server_enable_auto_ack(test_context_311->mock_server); |
| 1213 |
break; |
| 1214 |
case PAOTT_FAILURE_REASON_CODE_RETRYABLE: |
| 1215 |
case PAOTT_FAILURE_REASON_CODE_NOT_RETRYABLE: |
| 1216 |
mqtt_mock_server_enable_auto_ack(test_context_311->mock_server); |
| 1217 |
mqtt_mock_server_suback_reason_code(test_context_311->mock_server, 128); |
| 1218 |
break; |
| 1219 |
case PAOTT_FAILURE_ERROR_CODE: |
| 1220 |
|
| 1221 |
AWS_FATAL_ASSERT(false); |
| 1222 |
break; |
| 1223 |
case PAOTT_FAILURE_TIMEOUT: |
| 1224 |
mqtt_mock_server_disable_auto_ack(test_context_311->mock_server); |
| 1225 |
default: |
| 1226 |
break; |
| 1227 |
} |
| 1228 |
|
| 1229 |
struct aws_mqtt_connection_options connection_options = { |
| 1230 |
.user_data = test_context_311, |
| 1231 |
.clean_session = false, |
| 1232 |
.client_id = aws_byte_cursor_from_c_str("client1234"), |
| 1233 |
.host_name = aws_byte_cursor_from_c_str(test_context_311->endpoint.address), |
| 1234 |
.socket_options = &test_context_311->socket_options, |
| 1235 |
.on_connection_complete = aws_test311_on_connection_complete_fn, |
| 1236 |
.ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS, |
| 1237 |
.protocol_operation_timeout_ms = 3000, |
| 1238 |
.keep_alive_time_secs = 16960, |
| 1239 |
}; |
| 1240 |
|
| 1241 |
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(connection, &connection_options)); |
| 1242 |
|
| 1243 |
struct aws_protocol_adapter_connection_event connection_success_event = { |
| 1244 |
.event_type = AWS_PACET_CONNECTED, |
| 1245 |
.joined_session = false, |
| 1246 |
}; |
| 1247 |
|
| 1248 |
s_wait_for_connection_events_contains(&fixture, &connection_success_event, 1); |
| 1249 |
|
| 1250 |
int expected_error_code = s_test_type_to_expected_error_code(test_type, AWS_PAPT_MQTT311); |
| 1251 |
|
| 1252 |
struct request_response_protocol_adapter_subscription_event_record expected_outcome; |
| 1253 |
s_request_response_protocol_adapter_subscription_event_record_init( |
| 1254 |
&expected_outcome, |
| 1255 |
allocator, |
| 1256 |
AWS_PASET_SUBSCRIBE, |
| 1257 |
aws_byte_cursor_from_c_str("hello/world"), |
| 1258 |
expected_error_code, |
| 1259 |
true); |
| 1260 |
|
| 1261 |
struct aws_protocol_adapter_subscribe_options subscribe_options = { |
| 1262 |
.topic_filter = aws_byte_cursor_from_buf(&expected_outcome.topic_filter), |
| 1263 |
.ack_timeout_seconds = 2, |
| 1264 |
}; |
| 1265 |
|
| 1266 |
aws_mqtt_protocol_adapter_subscribe(fixture.protocol_adapter, &subscribe_options); |
| 1267 |
|
| 1268 |
s_wait_for_subscription_events_contains(&fixture, &expected_outcome, 1); |
| 1269 |
|
| 1270 |
s_request_response_protocol_adapter_subscription_event_record_cleanup(&expected_outcome); |
| 1271 |
|
| 1272 |
ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(connection, aws_test311_on_disconnect_fn, test_context_311)); |
| 1273 |
|
| 1274 |
struct aws_protocol_adapter_connection_event interruption_event = { |
| 1275 |
.event_type = AWS_PACET_DISCONNECTED, |
| 1276 |
}; |
| 1277 |
|
| 1278 |
s_wait_for_connection_events_contains(&fixture, &interruption_event, 1); |
| 1279 |
|
| 1280 |
s_aws_request_response_protocol_adapter_test_fixture_clean_up(&fixture); |
| 1281 |
|
| 1282 |
aws_mqtt_library_clean_up(); |
| 1283 |
|
| 1284 |
return AWS_OP_SUCCESS; |
| 1285 |
} |
| 1286 |
|
| 1287 |
static int s_request_response_mqtt311_protocol_adapter_subscribe_success_fn( |
| 1288 |
struct aws_allocator *allocator, |
| 1289 |
void *ctx) { |
| 1290 |
(void)ctx; |
| 1291 |
|
| 1292 |
ASSERT_SUCCESS(s_do_request_response_mqtt311_protocol_adapter_subscribe_test(allocator, PAOTT_SUCCESS)); |
| 1293 |
|
| 1294 |
return AWS_OP_SUCCESS; |
| 1295 |
} |
| 1296 |
|
| 1297 |
AWS_TEST_CASE( |
| 1298 |
request_response_mqtt311_protocol_adapter_subscribe_success, |
| 1299 |
s_request_response_mqtt311_protocol_adapter_subscribe_success_fn) |
| 1300 |
|
| 1301 |
static int s_request_response_mqtt311_protocol_adapter_subscribe_failure_timeout_fn( |
| 1302 |
struct aws_allocator *allocator, |
| 1303 |
void *ctx) { |
| 1304 |
(void)ctx; |
| 1305 |
|
| 1306 |
ASSERT_SUCCESS(s_do_request_response_mqtt311_protocol_adapter_subscribe_test(allocator, PAOTT_FAILURE_TIMEOUT)); |
| 1307 |
|
| 1308 |
return AWS_OP_SUCCESS; |
| 1309 |
} |
| 1310 |
|
| 1311 |
AWS_TEST_CASE( |
| 1312 |
request_response_mqtt311_protocol_adapter_subscribe_failure_timeout, |
| 1313 |
s_request_response_mqtt311_protocol_adapter_subscribe_failure_timeout_fn) |
| 1314 |
|
| 1315 |
static int s_request_response_mqtt311_protocol_adapter_subscribe_failure_reason_code_fn( |
| 1316 |
struct aws_allocator *allocator, |
| 1317 |
void *ctx) { |
| 1318 |
(void)ctx; |
| 1319 |
|
| 1320 |
ASSERT_SUCCESS(s_do_request_response_mqtt311_protocol_adapter_subscribe_test( |
| 1321 |
allocator, PAOTT_FAILURE_REASON_CODE_NOT_RETRYABLE)); |
| 1322 |
|
| 1323 |
return AWS_OP_SUCCESS; |
| 1324 |
} |
| 1325 |
|
| 1326 |
AWS_TEST_CASE( |
| 1327 |
request_response_mqtt311_protocol_adapter_subscribe_failure_reason_code, |
| 1328 |
s_request_response_mqtt311_protocol_adapter_subscribe_failure_reason_code_fn) |
| 1329 |
|
| 1330 |
static int s_do_request_response_mqtt311_protocol_adapter_unsubscribe_test( |
| 1331 |
struct aws_allocator *allocator, |
| 1332 |
enum protocol_adapter_operation_test_type test_type) { |
| 1333 |
aws_mqtt_library_init(allocator); |
| 1334 |
|
| 1335 |
struct aws_request_response_protocol_adapter_test_fixture fixture; |
| 1336 |
ASSERT_SUCCESS(s_aws_request_response_protocol_adapter_test_fixture_init_mqtt311(&fixture, allocator)); |
| 1337 |
|
| 1338 |
struct aws_mqtt_client_connection *connection = |
| 1339 |
fixture.protocol_context.mqtt311_fixture.mqtt311_test_context.mqtt_connection; |
| 1340 |
|
| 1341 |
struct mqtt_connection_state_test *test_context_311 = |
| 1342 |
&fixture.protocol_context.mqtt311_fixture.mqtt311_test_context; |
| 1343 |
|
| 1344 |
switch (test_type) { |
| 1345 |
case PAOTT_SUCCESS: |
| 1346 |
mqtt_mock_server_enable_auto_ack(test_context_311->mock_server); |
| 1347 |
break; |
| 1348 |
case PAOTT_FAILURE_REASON_CODE_RETRYABLE: |
| 1349 |
case PAOTT_FAILURE_REASON_CODE_NOT_RETRYABLE: |
| 1350 |
|
| 1351 |
AWS_FATAL_ASSERT(false); |
| 1352 |
break; |
| 1353 |
case PAOTT_FAILURE_ERROR_CODE: |
| 1354 |
|
| 1355 |
AWS_FATAL_ASSERT(false); |
| 1356 |
break; |
| 1357 |
case PAOTT_FAILURE_TIMEOUT: |
| 1358 |
mqtt_mock_server_disable_auto_ack(test_context_311->mock_server); |
| 1359 |
default: |
| 1360 |
break; |
| 1361 |
} |
| 1362 |
|
| 1363 |
struct aws_mqtt_connection_options connection_options = { |
| 1364 |
.user_data = test_context_311, |
| 1365 |
.clean_session = false, |
| 1366 |
.client_id = aws_byte_cursor_from_c_str("client1234"), |
| 1367 |
.host_name = aws_byte_cursor_from_c_str(test_context_311->endpoint.address), |
| 1368 |
.socket_options = &test_context_311->socket_options, |
| 1369 |
.on_connection_complete = aws_test311_on_connection_complete_fn, |
| 1370 |
.ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS, |
| 1371 |
.protocol_operation_timeout_ms = 3000, |
| 1372 |
.keep_alive_time_secs = 16960, |
| 1373 |
}; |
| 1374 |
|
| 1375 |
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(connection, &connection_options)); |
| 1376 |
|
| 1377 |
struct aws_protocol_adapter_connection_event connection_success_event = { |
| 1378 |
.event_type = AWS_PACET_CONNECTED, |
| 1379 |
.joined_session = false, |
| 1380 |
}; |
| 1381 |
|
| 1382 |
s_wait_for_connection_events_contains(&fixture, &connection_success_event, 1); |
| 1383 |
|
| 1384 |
int expected_error_code = s_test_type_to_expected_error_code(test_type, AWS_PAPT_MQTT311); |
| 1385 |
|
| 1386 |
struct request_response_protocol_adapter_subscription_event_record expected_outcome; |
| 1387 |
s_request_response_protocol_adapter_subscription_event_record_init( |
| 1388 |
&expected_outcome, |
| 1389 |
allocator, |
| 1390 |
AWS_PASET_UNSUBSCRIBE, |
| 1391 |
aws_byte_cursor_from_c_str("hello/world"), |
| 1392 |
expected_error_code, |
| 1393 |
expected_error_code == AWS_ERROR_MQTT_TIMEOUT); |
| 1394 |
|
| 1395 |
struct aws_protocol_adapter_unsubscribe_options unsubscribe_options = { |
| 1396 |
.topic_filter = aws_byte_cursor_from_buf(&expected_outcome.topic_filter), |
| 1397 |
.ack_timeout_seconds = 2, |
| 1398 |
}; |
| 1399 |
|
| 1400 |
aws_mqtt_protocol_adapter_unsubscribe(fixture.protocol_adapter, &unsubscribe_options); |
| 1401 |
|
| 1402 |
s_wait_for_subscription_events_contains(&fixture, &expected_outcome, 1); |
| 1403 |
|
| 1404 |
s_request_response_protocol_adapter_subscription_event_record_cleanup(&expected_outcome); |
| 1405 |
|
| 1406 |
ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(connection, aws_test311_on_disconnect_fn, test_context_311)); |
| 1407 |
|
| 1408 |
struct aws_protocol_adapter_connection_event interruption_event = { |
| 1409 |
.event_type = AWS_PACET_DISCONNECTED, |
| 1410 |
}; |
| 1411 |
|
| 1412 |
s_wait_for_connection_events_contains(&fixture, &interruption_event, 1); |
| 1413 |
|
| 1414 |
s_aws_request_response_protocol_adapter_test_fixture_clean_up(&fixture); |
| 1415 |
|
| 1416 |
aws_mqtt_library_clean_up(); |
| 1417 |
|
| 1418 |
return AWS_OP_SUCCESS; |
| 1419 |
} |
| 1420 |
|
| 1421 |
static int s_request_response_mqtt311_protocol_adapter_unsubscribe_success_fn( |
| 1422 |
struct aws_allocator *allocator, |
| 1423 |
void *ctx) { |
| 1424 |
(void)ctx; |
| 1425 |
|
| 1426 |
ASSERT_SUCCESS(s_do_request_response_mqtt311_protocol_adapter_unsubscribe_test(allocator, PAOTT_SUCCESS)); |
| 1427 |
|
| 1428 |
return AWS_OP_SUCCESS; |
| 1429 |
} |
| 1430 |
|
| 1431 |
AWS_TEST_CASE( |
| 1432 |
request_response_mqtt311_protocol_adapter_unsubscribe_success, |
| 1433 |
s_request_response_mqtt311_protocol_adapter_unsubscribe_success_fn) |
| 1434 |
|
| 1435 |
static int s_request_response_mqtt311_protocol_adapter_unsubscribe_failure_timeout_fn( |
| 1436 |
struct aws_allocator *allocator, |
| 1437 |
void *ctx) { |
| 1438 |
(void)ctx; |
| 1439 |
|
| 1440 |
ASSERT_SUCCESS(s_do_request_response_mqtt311_protocol_adapter_unsubscribe_test(allocator, PAOTT_FAILURE_TIMEOUT)); |
| 1441 |
|
| 1442 |
return AWS_OP_SUCCESS; |
| 1443 |
} |
| 1444 |
|
| 1445 |
AWS_TEST_CASE( |
| 1446 |
request_response_mqtt311_protocol_adapter_unsubscribe_failure_timeout, |
| 1447 |
s_request_response_mqtt311_protocol_adapter_unsubscribe_failure_timeout_fn) |
| 1448 |
|
| 1449 |
static int s_do_request_response_mqtt311_protocol_adapter_publish_test( |
| 1450 |
struct aws_allocator *allocator, |
| 1451 |
enum protocol_adapter_operation_test_type test_type) { |
| 1452 |
aws_mqtt_library_init(allocator); |
| 1453 |
|
| 1454 |
struct aws_request_response_protocol_adapter_test_fixture fixture; |
| 1455 |
ASSERT_SUCCESS(s_aws_request_response_protocol_adapter_test_fixture_init_mqtt311(&fixture, allocator)); |
| 1456 |
|
| 1457 |
struct aws_mqtt_client_connection *connection = |
| 1458 |
fixture.protocol_context.mqtt311_fixture.mqtt311_test_context.mqtt_connection; |
| 1459 |
|
| 1460 |
struct mqtt_connection_state_test *test_context_311 = |
| 1461 |
&fixture.protocol_context.mqtt311_fixture.mqtt311_test_context; |
| 1462 |
|
| 1463 |
switch (test_type) { |
| 1464 |
case PAOTT_SUCCESS: |
| 1465 |
mqtt_mock_server_enable_auto_ack(test_context_311->mock_server); |
| 1466 |
break; |
| 1467 |
case PAOTT_FAILURE_REASON_CODE_RETRYABLE: |
| 1468 |
case PAOTT_FAILURE_REASON_CODE_NOT_RETRYABLE: |
| 1469 |
|
| 1470 |
AWS_FATAL_ASSERT(false); |
| 1471 |
break; |
| 1472 |
case PAOTT_FAILURE_ERROR_CODE: |
| 1473 |
|
| 1474 |
AWS_FATAL_ASSERT(false); |
| 1475 |
break; |
| 1476 |
case PAOTT_FAILURE_TIMEOUT: |
| 1477 |
mqtt_mock_server_disable_auto_ack(test_context_311->mock_server); |
| 1478 |
default: |
| 1479 |
break; |
| 1480 |
} |
| 1481 |
|
| 1482 |
struct aws_mqtt_connection_options connection_options = { |
| 1483 |
.user_data = test_context_311, |
| 1484 |
.clean_session = false, |
| 1485 |
.client_id = aws_byte_cursor_from_c_str("client1234"), |
| 1486 |
.host_name = aws_byte_cursor_from_c_str(test_context_311->endpoint.address), |
| 1487 |
.socket_options = &test_context_311->socket_options, |
| 1488 |
.on_connection_complete = aws_test311_on_connection_complete_fn, |
| 1489 |
.ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS, |
| 1490 |
.protocol_operation_timeout_ms = 3000, |
| 1491 |
.keep_alive_time_secs = 16960, |
| 1492 |
}; |
| 1493 |
|
| 1494 |
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(connection, &connection_options)); |
| 1495 |
|
| 1496 |
struct aws_protocol_adapter_connection_event connection_success_event = { |
| 1497 |
.event_type = AWS_PACET_CONNECTED, |
| 1498 |
.joined_session = false, |
| 1499 |
}; |
| 1500 |
|
| 1501 |
s_wait_for_connection_events_contains(&fixture, &connection_success_event, 1); |
| 1502 |
|
| 1503 |
struct aws_protocol_adapter_publish_options publish_options = { |
| 1504 |
.topic = aws_byte_cursor_from_c_str("hello/world"), |
| 1505 |
.payload = aws_byte_cursor_from_c_str("SomePayload"), |
| 1506 |
.ack_timeout_seconds = 2, |
| 1507 |
.completion_callback_fn = s_rr_mqtt_protocol_adapter_test_on_publish_result, |
| 1508 |
.user_data = &fixture, |
| 1509 |
}; |
| 1510 |
|
| 1511 |
aws_mqtt_protocol_adapter_publish(fixture.protocol_adapter, &publish_options); |
| 1512 |
|
| 1513 |
int expected_error_code = s_test_type_to_expected_error_code(test_type, AWS_PAPT_MQTT311); |
| 1514 |
s_wait_for_publish_results_contains(&fixture, expected_error_code, 1); |
| 1515 |
|
| 1516 |
ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(connection, aws_test311_on_disconnect_fn, test_context_311)); |
| 1517 |
|
| 1518 |
struct aws_protocol_adapter_connection_event interruption_event = { |
| 1519 |
.event_type = AWS_PACET_DISCONNECTED, |
| 1520 |
}; |
| 1521 |
|
| 1522 |
s_wait_for_connection_events_contains(&fixture, &interruption_event, 1); |
| 1523 |
|
| 1524 |
s_aws_request_response_protocol_adapter_test_fixture_clean_up(&fixture); |
| 1525 |
|
| 1526 |
aws_mqtt_library_clean_up(); |
| 1527 |
|
| 1528 |
return AWS_OP_SUCCESS; |
| 1529 |
} |
| 1530 |
|
| 1531 |
static int s_request_response_mqtt311_protocol_adapter_publish_success_fn(struct aws_allocator *allocator, void *ctx) { |
| 1532 |
(void)ctx; |
| 1533 |
|
| 1534 |
ASSERT_SUCCESS(s_do_request_response_mqtt311_protocol_adapter_publish_test(allocator, PAOTT_SUCCESS)); |
| 1535 |
|
| 1536 |
return AWS_OP_SUCCESS; |
| 1537 |
} |
| 1538 |
|
| 1539 |
AWS_TEST_CASE( |
| 1540 |
request_response_mqtt311_protocol_adapter_publish_success, |
| 1541 |
s_request_response_mqtt311_protocol_adapter_publish_success_fn) |
| 1542 |
|
| 1543 |
static int s_request_response_mqtt311_protocol_adapter_publish_failure_timeout_fn( |
| 1544 |
struct aws_allocator *allocator, |
| 1545 |
void *ctx) { |
| 1546 |
(void)ctx; |
| 1547 |
|
| 1548 |
ASSERT_SUCCESS(s_do_request_response_mqtt311_protocol_adapter_publish_test(allocator, PAOTT_FAILURE_TIMEOUT)); |
| 1549 |
|
| 1550 |
return AWS_OP_SUCCESS; |
| 1551 |
} |
| 1552 |
|
| 1553 |
AWS_TEST_CASE( |
| 1554 |
request_response_mqtt311_protocol_adapter_publish_failure_timeout, |
| 1555 |
s_request_response_mqtt311_protocol_adapter_publish_failure_timeout_fn) |
| 1556 |
|
| 1557 |
static int s_request_response_mqtt311_protocol_adapter_connection_events_fn( |
| 1558 |
struct aws_allocator *allocator, |
| 1559 |
void *ctx) { |
| 1560 |
(void)ctx; |
| 1561 |
|
| 1562 |
aws_mqtt_library_init(allocator); |
| 1563 |
|
| 1564 |
struct aws_request_response_protocol_adapter_test_fixture fixture; |
| 1565 |
ASSERT_SUCCESS(s_aws_request_response_protocol_adapter_test_fixture_init_mqtt311(&fixture, allocator)); |
| 1566 |
|
| 1567 |
struct aws_mqtt_client_connection *connection = |
| 1568 |
fixture.protocol_context.mqtt311_fixture.mqtt311_test_context.mqtt_connection; |
| 1569 |
|
| 1570 |
struct mqtt_connection_state_test *test_context_311 = |
| 1571 |
&fixture.protocol_context.mqtt311_fixture.mqtt311_test_context; |
| 1572 |
|
| 1573 |
|
| 1574 |
mqtt_mock_server_set_session_present(test_context_311->mock_server, true); |
| 1575 |
mqtt_mock_server_set_max_ping_resp(test_context_311->mock_server, 0); |
| 1576 |
|
| 1577 |
struct aws_mqtt_connection_options connection_options = { |
| 1578 |
.user_data = test_context_311, |
| 1579 |
.clean_session = false, |
| 1580 |
.client_id = aws_byte_cursor_from_c_str("client1234"), |
| 1581 |
.host_name = aws_byte_cursor_from_c_str(test_context_311->endpoint.address), |
| 1582 |
.socket_options = &test_context_311->socket_options, |
| 1583 |
.on_connection_complete = aws_test311_on_connection_complete_fn, |
| 1584 |
.ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS, |
| 1585 |
.protocol_operation_timeout_ms = 3000, |
| 1586 |
.keep_alive_time_secs = 2, |
| 1587 |
}; |
| 1588 |
|
| 1589 |
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(connection, &connection_options)); |
| 1590 |
|
| 1591 |
struct aws_protocol_adapter_connection_event connection_success_event = { |
| 1592 |
.event_type = AWS_PACET_CONNECTED, |
| 1593 |
.joined_session = true, |
| 1594 |
}; |
| 1595 |
|
| 1596 |
s_wait_for_connection_events_contains(&fixture, &connection_success_event, 1); |
| 1597 |
|
| 1598 |
struct aws_protocol_adapter_connection_event interruption_event = { |
| 1599 |
.event_type = AWS_PACET_DISCONNECTED, |
| 1600 |
}; |
| 1601 |
|
| 1602 |
|
| 1603 |
s_wait_for_connection_events_contains(&fixture, &interruption_event, 1); |
| 1604 |
|
| 1605 |
|
| 1606 |
s_wait_for_connection_events_contains(&fixture, &connection_success_event, 2); |
| 1607 |
|
| 1608 |
ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(connection, aws_test311_on_disconnect_fn, test_context_311)); |
| 1609 |
|
| 1610 |
s_wait_for_connection_events_contains(&fixture, &interruption_event, 2); |
| 1611 |
|
| 1612 |
s_aws_request_response_protocol_adapter_test_fixture_clean_up(&fixture); |
| 1613 |
|
| 1614 |
aws_mqtt_library_clean_up(); |
| 1615 |
|
| 1616 |
return AWS_OP_SUCCESS; |
| 1617 |
} |
| 1618 |
|
| 1619 |
AWS_TEST_CASE( |
| 1620 |
request_response_mqtt311_protocol_adapter_connection_events, |
| 1621 |
s_request_response_mqtt311_protocol_adapter_connection_events_fn) |
| 1622 |
|
| 1623 |
static int s_request_response_mqtt311_protocol_adapter_incoming_publish_fn(struct aws_allocator *allocator, void *ctx) { |
| 1624 |
(void)ctx; |
| 1625 |
|
| 1626 |
aws_mqtt_library_init(allocator); |
| 1627 |
|
| 1628 |
struct aws_request_response_protocol_adapter_test_fixture fixture; |
| 1629 |
ASSERT_SUCCESS(s_aws_request_response_protocol_adapter_test_fixture_init_mqtt311(&fixture, allocator)); |
| 1630 |
|
| 1631 |
struct aws_mqtt_client_connection *connection = |
| 1632 |
fixture.protocol_context.mqtt311_fixture.mqtt311_test_context.mqtt_connection; |
| 1633 |
|
| 1634 |
struct mqtt_connection_state_test *test_context_311 = |
| 1635 |
&fixture.protocol_context.mqtt311_fixture.mqtt311_test_context; |
| 1636 |
|
| 1637 |
|
| 1638 |
mqtt_mock_server_set_publish_reflection(test_context_311->mock_server, true); |
| 1639 |
|
| 1640 |
struct aws_mqtt_connection_options connection_options = { |
| 1641 |
.user_data = test_context_311, |
| 1642 |
.clean_session = false, |
| 1643 |
.client_id = aws_byte_cursor_from_c_str("client1234"), |
| 1644 |
.host_name = aws_byte_cursor_from_c_str(test_context_311->endpoint.address), |
| 1645 |
.socket_options = &test_context_311->socket_options, |
| 1646 |
.on_connection_complete = aws_test311_on_connection_complete_fn, |
| 1647 |
.ping_timeout_ms = 30 * 1000, |
| 1648 |
.protocol_operation_timeout_ms = 3000, |
| 1649 |
.keep_alive_time_secs = 20000, |
| 1650 |
}; |
| 1651 |
|
| 1652 |
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(connection, &connection_options)); |
| 1653 |
|
| 1654 |
struct aws_protocol_adapter_connection_event connection_success_event = { |
| 1655 |
.event_type = AWS_PACET_CONNECTED, |
| 1656 |
}; |
| 1657 |
|
| 1658 |
s_wait_for_connection_events_contains(&fixture, &connection_success_event, 1); |
| 1659 |
|
| 1660 |
struct request_response_protocol_adapter_incoming_publish_event_record expected_publish; |
| 1661 |
AWS_ZERO_STRUCT(expected_publish); |
| 1662 |
|
| 1663 |
s_request_response_protocol_adapter_incoming_publish_event_record_init( |
| 1664 |
&expected_publish, |
| 1665 |
allocator, |
| 1666 |
aws_byte_cursor_from_c_str("hello/world"), |
| 1667 |
aws_byte_cursor_from_c_str("SomePayload")); |
| 1668 |
|
| 1669 |
struct aws_protocol_adapter_publish_options publish_options = { |
| 1670 |
.topic = aws_byte_cursor_from_buf(&expected_publish.topic), |
| 1671 |
.payload = aws_byte_cursor_from_buf(&expected_publish.payload), |
| 1672 |
.ack_timeout_seconds = 2, |
| 1673 |
.completion_callback_fn = s_rr_mqtt_protocol_adapter_test_on_publish_result, |
| 1674 |
.user_data = &fixture, |
| 1675 |
}; |
| 1676 |
|
| 1677 |
aws_mqtt_protocol_adapter_publish(fixture.protocol_adapter, &publish_options); |
| 1678 |
|
| 1679 |
s_wait_for_incoming_publish_events_contains(&fixture, &expected_publish, 1); |
| 1680 |
|
| 1681 |
s_request_response_protocol_adapter_incoming_publish_event_record_clean_up(&expected_publish); |
| 1682 |
|
| 1683 |
struct aws_protocol_adapter_connection_event interruption_event = { |
| 1684 |
.event_type = AWS_PACET_DISCONNECTED, |
| 1685 |
}; |
| 1686 |
|
| 1687 |
ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(connection, aws_test311_on_disconnect_fn, test_context_311)); |
| 1688 |
|
| 1689 |
s_wait_for_connection_events_contains(&fixture, &interruption_event, 1); |
| 1690 |
|
| 1691 |
s_aws_request_response_protocol_adapter_test_fixture_clean_up(&fixture); |
| 1692 |
|
| 1693 |
aws_mqtt_library_clean_up(); |
| 1694 |
|
| 1695 |
return AWS_OP_SUCCESS; |
| 1696 |
} |
| 1697 |
|
| 1698 |
AWS_TEST_CASE( |
| 1699 |
request_response_mqtt311_protocol_adapter_incoming_publish, |
| 1700 |
s_request_response_mqtt311_protocol_adapter_incoming_publish_fn) |
| 1701 |
|
| 1702 |
static int s_request_response_mqtt311_protocol_adapter_shutdown_while_pending_fn( |
| 1703 |
struct aws_allocator *allocator, |
| 1704 |
void *ctx) { |
| 1705 |
(void)ctx; |
| 1706 |
|
| 1707 |
aws_mqtt_library_init(allocator); |
| 1708 |
|
| 1709 |
struct aws_request_response_protocol_adapter_test_fixture fixture; |
| 1710 |
ASSERT_SUCCESS(s_aws_request_response_protocol_adapter_test_fixture_init_mqtt311(&fixture, allocator)); |
| 1711 |
|
| 1712 |
struct aws_mqtt_client_connection *connection = |
| 1713 |
fixture.protocol_context.mqtt311_fixture.mqtt311_test_context.mqtt_connection; |
| 1714 |
|
| 1715 |
struct mqtt_connection_state_test *test_context_311 = |
| 1716 |
&fixture.protocol_context.mqtt311_fixture.mqtt311_test_context; |
| 1717 |
|
| 1718 |
|
| 1719 |
mqtt_mock_server_set_publish_reflection(test_context_311->mock_server, true); |
| 1720 |
|
| 1721 |
struct aws_mqtt_connection_options connection_options = { |
| 1722 |
.user_data = test_context_311, |
| 1723 |
.clean_session = false, |
| 1724 |
.client_id = aws_byte_cursor_from_c_str("client1234"), |
| 1725 |
.host_name = aws_byte_cursor_from_c_str(test_context_311->endpoint.address), |
| 1726 |
.socket_options = &test_context_311->socket_options, |
| 1727 |
.on_connection_complete = aws_test311_on_connection_complete_fn, |
| 1728 |
.ping_timeout_ms = 30 * 1000, |
| 1729 |
.protocol_operation_timeout_ms = 3000, |
| 1730 |
.keep_alive_time_secs = 20000, |
| 1731 |
}; |
| 1732 |
|
| 1733 |
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(connection, &connection_options)); |
| 1734 |
|
| 1735 |
struct aws_protocol_adapter_connection_event connection_success_event = { |
| 1736 |
.event_type = AWS_PACET_CONNECTED, |
| 1737 |
}; |
| 1738 |
|
| 1739 |
s_wait_for_connection_events_contains(&fixture, &connection_success_event, 1); |
| 1740 |
|
| 1741 |
|
| 1742 |
struct aws_protocol_adapter_publish_options publish_options = { |
| 1743 |
.topic = aws_byte_cursor_from_c_str("hello/world"), |
| 1744 |
.payload = aws_byte_cursor_from_c_str("SomePayload"), |
| 1745 |
.ack_timeout_seconds = 5, |
| 1746 |
.completion_callback_fn = s_rr_mqtt_protocol_adapter_test_on_publish_result, |
| 1747 |
.user_data = &fixture, |
| 1748 |
}; |
| 1749 |
|
| 1750 |
aws_mqtt_protocol_adapter_publish(fixture.protocol_adapter, &publish_options); |
| 1751 |
|
| 1752 |
|
| 1753 |
struct aws_protocol_adapter_subscribe_options subscribe_options = { |
| 1754 |
.topic_filter = aws_byte_cursor_from_c_str("hello/world"), |
| 1755 |
.ack_timeout_seconds = 5, |
| 1756 |
}; |
| 1757 |
|
| 1758 |
aws_mqtt_protocol_adapter_subscribe(fixture.protocol_adapter, &subscribe_options); |
| 1759 |
|
| 1760 |
|
| 1761 |
struct aws_protocol_adapter_unsubscribe_options unsubscribe_options = { |
| 1762 |
.topic_filter = aws_byte_cursor_from_c_str("hello/world"), |
| 1763 |
.ack_timeout_seconds = 5, |
| 1764 |
}; |
| 1765 |
|
| 1766 |
aws_mqtt_protocol_adapter_unsubscribe(fixture.protocol_adapter, &unsubscribe_options); |
| 1767 |
|
| 1768 |
|
| 1769 |
s_aws_request_response_protocol_adapter_test_fixture_destroy_adapters(&fixture); |
| 1770 |
|
| 1771 |
ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(connection, aws_test311_on_disconnect_fn, test_context_311)); |
| 1772 |
|
| 1773 |
|
| 1774 |
aws_test311_wait_for_disconnect_to_complete(test_context_311); |
| 1775 |
|
| 1776 |
s_aws_request_response_protocol_adapter_test_fixture_clean_up(&fixture); |
| 1777 |
|
| 1778 |
aws_mqtt_library_clean_up(); |
| 1779 |
|
| 1780 |
return AWS_OP_SUCCESS; |
| 1781 |
} |
| 1782 |
|
| 1783 |
AWS_TEST_CASE( |
| 1784 |
request_response_mqtt311_protocol_adapter_shutdown_while_pending, |
| 1785 |
s_request_response_mqtt311_protocol_adapter_shutdown_while_pending_fn) |