| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "mqtt5_testing_utils.h" |
| 7 |
|
| 8 |
#include <aws/common/clock.h> |
| 9 |
#include <aws/common/string.h> |
| 10 |
#include <aws/http/websocket.h> |
| 11 |
#include <aws/io/channel_bootstrap.h> |
| 12 |
#include <aws/mqtt/client.h> |
| 13 |
#include <aws/mqtt/mqtt.h> |
| 14 |
#include <aws/mqtt/private/v5/mqtt5_to_mqtt3_adapter_impl.h> |
| 15 |
#include <aws/mqtt/v5/mqtt5_client.h> |
| 16 |
|
| 17 |
#include <aws/testing/aws_test_harness.h> |
| 18 |
|
| 19 |
#include <inttypes.h> |
| 20 |
|
| 21 |
enum aws_mqtt3_lifecycle_event_type { |
| 22 |
AWS_MQTT3_LET_CONNECTION_COMPLETE, |
| 23 |
AWS_MQTT3_LET_INTERRUPTED, |
| 24 |
AWS_MQTT3_LET_RESUMED, |
| 25 |
AWS_MQTT3_LET_CLOSED, |
| 26 |
AWS_MQTT3_LET_DISCONNECTION_COMPLETE, |
| 27 |
AWS_MQTT3_LET_CONNECTION_SUCCESS, |
| 28 |
AWS_MQTT3_LET_CONNECTION_FAILURE, |
| 29 |
AWS_MQTT3_LET_TERMINATION, |
| 30 |
}; |
| 31 |
|
| 32 |
struct aws_mqtt3_lifecycle_event { |
| 33 |
enum aws_mqtt3_lifecycle_event_type type; |
| 34 |
|
| 35 |
uint64_t timestamp; |
| 36 |
int error_code; |
| 37 |
enum aws_mqtt_connect_return_code return_code; |
| 38 |
bool session_present; |
| 39 |
|
| 40 |
bool skip_error_code_equality; |
| 41 |
}; |
| 42 |
|
| 43 |
enum aws_mqtt3_operation_event_type { |
| 44 |
AWS_MQTT3_OET_PUBLISH_COMPLETE, |
| 45 |
AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, |
| 46 |
AWS_MQTT3_OET_UNSUBSCRIBE_COMPLETE, |
| 47 |
AWS_MQTT3_OET_PUBLISH_RECEIVED_SUBSCRIBED, |
| 48 |
AWS_MQTT3_OET_PUBLISH_RECEIVED_ANY, |
| 49 |
}; |
| 50 |
|
| 51 |
struct aws_mqtt3_operation_event { |
| 52 |
enum aws_mqtt3_operation_event_type type; |
| 53 |
|
| 54 |
uint64_t timestamp; |
| 55 |
int error_code; |
| 56 |
|
| 57 |
|
| 58 |
enum aws_mqtt_qos qos; |
| 59 |
struct aws_byte_buf topic; |
| 60 |
struct aws_byte_cursor topic_cursor; |
| 61 |
struct aws_byte_buf payload; |
| 62 |
struct aws_byte_cursor payload_cursor; |
| 63 |
|
| 64 |
|
| 65 |
struct aws_array_list granted_subscriptions; |
| 66 |
|
| 67 |
struct aws_byte_buf topic_storage; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
size_t expected_count; |
| 75 |
}; |
| 76 |
|
| 77 |
static void s_aws_mqtt3_operation_event_clean_up(struct aws_mqtt3_operation_event *event) { |
| 78 |
if (event == NULL) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
aws_byte_buf_clean_up(&event->topic); |
| 83 |
aws_byte_buf_clean_up(&event->payload); |
| 84 |
aws_byte_buf_clean_up(&event->topic_storage); |
| 85 |
|
| 86 |
aws_array_list_clean_up(&event->granted_subscriptions); |
| 87 |
} |
| 88 |
|
| 89 |
static bool s_aws_mqtt3_operation_event_equals( |
| 90 |
struct aws_mqtt3_operation_event *expected, |
| 91 |
struct aws_mqtt3_operation_event *actual) { |
| 92 |
if (expected->type != actual->type) { |
| 93 |
return false; |
| 94 |
} |
| 95 |
|
| 96 |
if (expected->error_code != actual->error_code) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
if (expected->qos != actual->qos) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
if (expected->topic_cursor.len != actual->topic_cursor.len) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
if (expected->topic_cursor.len > 0) { |
| 109 |
if (memcmp(expected->topic_cursor.ptr, actual->topic_cursor.ptr, expected->topic_cursor.len) != 0) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
if (expected->payload_cursor.len != actual->payload_cursor.len) { |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
if (expected->payload_cursor.len > 0) { |
| 119 |
if (memcmp(expected->payload_cursor.ptr, actual->payload_cursor.ptr, expected->payload_cursor.len) != 0) { |
| 120 |
return false; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
if (aws_array_list_length(&expected->granted_subscriptions) != |
| 125 |
aws_array_list_length(&actual->granted_subscriptions)) { |
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
for (size_t i = 0; i < aws_array_list_length(&expected->granted_subscriptions); ++i) { |
| 130 |
|
| 131 |
struct aws_mqtt_topic_subscription expected_sub; |
| 132 |
aws_array_list_get_at(&expected->granted_subscriptions, &expected_sub, i); |
| 133 |
|
| 134 |
bool found_match = false; |
| 135 |
for (size_t j = 0; j < aws_array_list_length(&actual->granted_subscriptions); ++j) { |
| 136 |
struct aws_mqtt_topic_subscription actual_sub; |
| 137 |
aws_array_list_get_at(&actual->granted_subscriptions, &actual_sub, j); |
| 138 |
|
| 139 |
if (expected_sub.qos != actual_sub.qos) { |
| 140 |
continue; |
| 141 |
} |
| 142 |
|
| 143 |
if (expected_sub.topic.len != actual_sub.topic.len) { |
| 144 |
continue; |
| 145 |
} |
| 146 |
|
| 147 |
if (expected_sub.topic.len > 0) { |
| 148 |
if (memcmp(expected_sub.topic.ptr, actual_sub.topic.ptr, expected_sub.topic.len) != 0) { |
| 149 |
continue; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
found_match = true; |
| 154 |
break; |
| 155 |
} |
| 156 |
|
| 157 |
if (!found_match) { |
| 158 |
return false; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
return true; |
| 163 |
} |
| 164 |
|
| 165 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture { |
| 166 |
struct aws_mqtt5_client_mock_test_fixture mqtt5_fixture; |
| 167 |
|
| 168 |
struct aws_mqtt_client_connection *connection; |
| 169 |
|
| 170 |
struct aws_array_list lifecycle_events; |
| 171 |
|
| 172 |
struct aws_array_list operation_events; |
| 173 |
|
| 174 |
struct aws_mutex lock; |
| 175 |
struct aws_condition_variable signal; |
| 176 |
}; |
| 177 |
|
| 178 |
static void s_init_adapter_connection_options_from_fixture( |
| 179 |
struct aws_mqtt_connection_options *connection_options, |
| 180 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture) { |
| 181 |
AWS_ZERO_STRUCT(*connection_options); |
| 182 |
|
| 183 |
connection_options->host_name = aws_byte_cursor_from_c_str(fixture->mqtt5_fixture.endpoint.address); |
| 184 |
connection_options->port = fixture->mqtt5_fixture.endpoint.port; |
| 185 |
connection_options->socket_options = &fixture->mqtt5_fixture.socket_options; |
| 186 |
connection_options->keep_alive_time_secs = 30; |
| 187 |
connection_options->ping_timeout_ms = 10000; |
| 188 |
connection_options->clean_session = true; |
| 189 |
} |
| 190 |
|
| 191 |
static int s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_operation_sequence( |
| 192 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture, |
| 193 |
size_t expected_event_count, |
| 194 |
struct aws_mqtt3_operation_event *expected_events, |
| 195 |
size_t maximum_event_count) { |
| 196 |
|
| 197 |
aws_mutex_lock(&fixture->lock); |
| 198 |
|
| 199 |
size_t actual_event_count = aws_array_list_length(&fixture->operation_events); |
| 200 |
ASSERT_TRUE(expected_event_count <= actual_event_count); |
| 201 |
ASSERT_TRUE(actual_event_count <= maximum_event_count); |
| 202 |
|
| 203 |
for (size_t i = 0; i < expected_event_count; ++i) { |
| 204 |
struct aws_mqtt3_operation_event *expected_event = expected_events + i; |
| 205 |
struct aws_mqtt3_operation_event *actual_event = NULL; |
| 206 |
aws_array_list_get_at_ptr(&fixture->operation_events, (void **)(&actual_event), i); |
| 207 |
|
| 208 |
ASSERT_TRUE(s_aws_mqtt3_operation_event_equals(expected_event, actual_event)); |
| 209 |
} |
| 210 |
|
| 211 |
aws_mutex_unlock(&fixture->lock); |
| 212 |
|
| 213 |
return AWS_OP_SUCCESS; |
| 214 |
} |
| 215 |
|
| 216 |
static int s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_operation_sequence_contains( |
| 217 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture, |
| 218 |
size_t expected_event_count, |
| 219 |
struct aws_mqtt3_operation_event *expected_events) { |
| 220 |
|
| 221 |
aws_mutex_lock(&fixture->lock); |
| 222 |
|
| 223 |
size_t actual_event_count = aws_array_list_length(&fixture->operation_events); |
| 224 |
|
| 225 |
for (size_t i = 0; i < expected_event_count; ++i) { |
| 226 |
struct aws_mqtt3_operation_event *expected_event = expected_events + i; |
| 227 |
size_t match_count = 0; |
| 228 |
|
| 229 |
for (size_t j = 0; j < actual_event_count; ++j) { |
| 230 |
struct aws_mqtt3_operation_event *actual_event = NULL; |
| 231 |
aws_array_list_get_at_ptr(&fixture->operation_events, (void **)(&actual_event), j); |
| 232 |
|
| 233 |
if (s_aws_mqtt3_operation_event_equals(expected_event, actual_event)) { |
| 234 |
++match_count; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
if (expected_event->expected_count == 0) { |
| 239 |
ASSERT_INT_EQUALS(1, match_count); |
| 240 |
} else { |
| 241 |
ASSERT_INT_EQUALS(expected_event->expected_count, match_count); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
aws_mutex_unlock(&fixture->lock); |
| 246 |
|
| 247 |
return AWS_OP_SUCCESS; |
| 248 |
} |
| 249 |
|
| 250 |
struct n_operation_event_wait_context { |
| 251 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture; |
| 252 |
enum aws_mqtt3_operation_event_type type; |
| 253 |
size_t count; |
| 254 |
}; |
| 255 |
|
| 256 |
static bool s_wait_for_n_adapter_operation_events_predicate(void *context) { |
| 257 |
struct n_operation_event_wait_context *wait_context = context; |
| 258 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture = wait_context->fixture; |
| 259 |
|
| 260 |
size_t actual_count = 0; |
| 261 |
size_t event_count = aws_array_list_length(&fixture->operation_events); |
| 262 |
for (size_t i = 0; i < event_count; ++i) { |
| 263 |
struct aws_mqtt3_operation_event *actual_event = NULL; |
| 264 |
aws_array_list_get_at_ptr(&fixture->operation_events, (void **)(&actual_event), i); |
| 265 |
if (actual_event->type == wait_context->type) { |
| 266 |
++actual_count; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
return actual_count >= wait_context->count; |
| 271 |
} |
| 272 |
|
| 273 |
static void s_wait_for_n_adapter_operation_events( |
| 274 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture, |
| 275 |
enum aws_mqtt3_operation_event_type type, |
| 276 |
size_t count) { |
| 277 |
struct n_operation_event_wait_context wait_context = { |
| 278 |
.fixture = fixture, |
| 279 |
.type = type, |
| 280 |
.count = count, |
| 281 |
}; |
| 282 |
|
| 283 |
aws_mutex_lock(&fixture->lock); |
| 284 |
aws_condition_variable_wait_pred( |
| 285 |
&fixture->signal, &fixture->lock, s_wait_for_n_adapter_operation_events_predicate, &wait_context); |
| 286 |
aws_mutex_unlock(&fixture->lock); |
| 287 |
} |
| 288 |
|
| 289 |
struct n_lifeycle_event_wait_context { |
| 290 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture; |
| 291 |
enum aws_mqtt3_lifecycle_event_type type; |
| 292 |
size_t count; |
| 293 |
}; |
| 294 |
|
| 295 |
static bool s_wait_for_n_adapter_lifecycle_events_predicate(void *context) { |
| 296 |
struct n_lifeycle_event_wait_context *wait_context = context; |
| 297 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture = wait_context->fixture; |
| 298 |
|
| 299 |
size_t actual_count = 0; |
| 300 |
size_t event_count = aws_array_list_length(&fixture->lifecycle_events); |
| 301 |
for (size_t i = 0; i < event_count; ++i) { |
| 302 |
struct aws_mqtt3_lifecycle_event *actual_event = NULL; |
| 303 |
aws_array_list_get_at_ptr(&fixture->lifecycle_events, (void **)(&actual_event), i); |
| 304 |
if (actual_event->type == wait_context->type) { |
| 305 |
++actual_count; |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
return actual_count >= wait_context->count; |
| 310 |
} |
| 311 |
|
| 312 |
static void s_wait_for_n_adapter_lifecycle_events( |
| 313 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture, |
| 314 |
enum aws_mqtt3_lifecycle_event_type type, |
| 315 |
size_t count) { |
| 316 |
struct n_lifeycle_event_wait_context wait_context = { |
| 317 |
.fixture = fixture, |
| 318 |
.type = type, |
| 319 |
.count = count, |
| 320 |
}; |
| 321 |
|
| 322 |
aws_mutex_lock(&fixture->lock); |
| 323 |
aws_condition_variable_wait_pred( |
| 324 |
&fixture->signal, &fixture->lock, s_wait_for_n_adapter_lifecycle_events_predicate, &wait_context); |
| 325 |
aws_mutex_unlock(&fixture->lock); |
| 326 |
} |
| 327 |
|
| 328 |
static int s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_lifecycle_event( |
| 329 |
struct aws_mqtt3_lifecycle_event *expected_event, |
| 330 |
struct aws_mqtt3_lifecycle_event *actual_event) { |
| 331 |
ASSERT_INT_EQUALS(actual_event->type, expected_event->type); |
| 332 |
if (expected_event->skip_error_code_equality) { |
| 333 |
|
| 334 |
ASSERT_TRUE((actual_event->error_code != 0) == (expected_event->error_code != 0)); |
| 335 |
} else { |
| 336 |
ASSERT_INT_EQUALS(actual_event->error_code, expected_event->error_code); |
| 337 |
} |
| 338 |
|
| 339 |
ASSERT_INT_EQUALS(actual_event->return_code, expected_event->return_code); |
| 340 |
ASSERT_TRUE(actual_event->session_present == expected_event->session_present); |
| 341 |
|
| 342 |
return AWS_OP_SUCCESS; |
| 343 |
} |
| 344 |
|
| 345 |
static int s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_lifecycle_sequence( |
| 346 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture, |
| 347 |
size_t expected_event_count, |
| 348 |
struct aws_mqtt3_lifecycle_event *expected_events, |
| 349 |
size_t maximum_event_count) { |
| 350 |
|
| 351 |
aws_mutex_lock(&fixture->lock); |
| 352 |
|
| 353 |
size_t actual_event_count = aws_array_list_length(&fixture->lifecycle_events); |
| 354 |
ASSERT_TRUE(expected_event_count <= actual_event_count); |
| 355 |
ASSERT_TRUE(actual_event_count <= maximum_event_count); |
| 356 |
|
| 357 |
for (size_t i = 0; i < expected_event_count; ++i) { |
| 358 |
struct aws_mqtt3_lifecycle_event *expected_event = expected_events + i; |
| 359 |
struct aws_mqtt3_lifecycle_event *actual_event = NULL; |
| 360 |
aws_array_list_get_at_ptr(&fixture->lifecycle_events, (void **)(&actual_event), i); |
| 361 |
|
| 362 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_lifecycle_event(expected_event, actual_event)); |
| 363 |
} |
| 364 |
|
| 365 |
aws_mutex_unlock(&fixture->lock); |
| 366 |
|
| 367 |
return AWS_OP_SUCCESS; |
| 368 |
} |
| 369 |
|
| 370 |
static int s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_lifecycle_sequence_starts_with( |
| 371 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture, |
| 372 |
size_t expected_event_count, |
| 373 |
struct aws_mqtt3_lifecycle_event *expected_events) { |
| 374 |
|
| 375 |
aws_mutex_lock(&fixture->lock); |
| 376 |
|
| 377 |
size_t actual_event_count = aws_array_list_length(&fixture->lifecycle_events); |
| 378 |
ASSERT_TRUE(expected_event_count <= actual_event_count); |
| 379 |
|
| 380 |
for (size_t i = 0; i < expected_event_count; ++i) { |
| 381 |
struct aws_mqtt3_lifecycle_event *expected_event = expected_events + i; |
| 382 |
struct aws_mqtt3_lifecycle_event *actual_event = NULL; |
| 383 |
aws_array_list_get_at_ptr(&fixture->lifecycle_events, (void **)(&actual_event), i); |
| 384 |
|
| 385 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_lifecycle_event(expected_event, actual_event)); |
| 386 |
} |
| 387 |
|
| 388 |
aws_mutex_unlock(&fixture->lock); |
| 389 |
|
| 390 |
return AWS_OP_SUCCESS; |
| 391 |
} |
| 392 |
|
| 393 |
static int s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_lifecycle_sequence_ends_with( |
| 394 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture, |
| 395 |
size_t expected_event_count, |
| 396 |
struct aws_mqtt3_lifecycle_event *expected_events) { |
| 397 |
|
| 398 |
aws_mutex_lock(&fixture->lock); |
| 399 |
|
| 400 |
size_t actual_event_count = aws_array_list_length(&fixture->lifecycle_events); |
| 401 |
ASSERT_TRUE(expected_event_count <= actual_event_count); |
| 402 |
|
| 403 |
for (size_t i = 0; i < expected_event_count; ++i) { |
| 404 |
struct aws_mqtt3_lifecycle_event *expected_event = expected_events + i; |
| 405 |
|
| 406 |
size_t actual_index = i + (actual_event_count - expected_event_count); |
| 407 |
struct aws_mqtt3_lifecycle_event *actual_event = NULL; |
| 408 |
aws_array_list_get_at_ptr(&fixture->lifecycle_events, (void **)(&actual_event), actual_index); |
| 409 |
|
| 410 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_lifecycle_event(expected_event, actual_event)); |
| 411 |
} |
| 412 |
|
| 413 |
aws_mutex_unlock(&fixture->lock); |
| 414 |
|
| 415 |
return AWS_OP_SUCCESS; |
| 416 |
} |
| 417 |
|
| 418 |
static void s_aws_mqtt5_to_mqtt3_adapter_test_fixture_closed_handler( |
| 419 |
struct aws_mqtt_client_connection *connection, |
| 420 |
struct on_connection_closed_data *data, |
| 421 |
void *userdata) { |
| 422 |
|
| 423 |
(void)connection; |
| 424 |
(void)data; |
| 425 |
|
| 426 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture = userdata; |
| 427 |
|
| 428 |
|
| 429 |
struct aws_mqtt3_lifecycle_event event; |
| 430 |
AWS_ZERO_STRUCT(event); |
| 431 |
|
| 432 |
event.type = AWS_MQTT3_LET_CLOSED; |
| 433 |
aws_high_res_clock_get_ticks(&event.timestamp); |
| 434 |
|
| 435 |
aws_mutex_lock(&fixture->lock); |
| 436 |
aws_array_list_push_back(&fixture->lifecycle_events, &event); |
| 437 |
aws_mutex_unlock(&fixture->lock); |
| 438 |
aws_condition_variable_notify_all(&fixture->signal); |
| 439 |
} |
| 440 |
|
| 441 |
static void s_aws_mqtt5_to_mqtt3_adapter_test_fixture_termination_handler(void *userdata) { |
| 442 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture = userdata; |
| 443 |
|
| 444 |
|
| 445 |
struct aws_mqtt3_lifecycle_event event; |
| 446 |
AWS_ZERO_STRUCT(event); |
| 447 |
|
| 448 |
event.type = AWS_MQTT3_LET_TERMINATION; |
| 449 |
aws_high_res_clock_get_ticks(&event.timestamp); |
| 450 |
|
| 451 |
aws_mutex_lock(&fixture->lock); |
| 452 |
aws_array_list_push_back(&fixture->lifecycle_events, &event); |
| 453 |
aws_mutex_unlock(&fixture->lock); |
| 454 |
aws_condition_variable_notify_all(&fixture->signal); |
| 455 |
} |
| 456 |
|
| 457 |
static void s_aws_mqtt5_to_mqtt3_adapter_test_fixture_interrupted_handler( |
| 458 |
struct aws_mqtt_client_connection *connection, |
| 459 |
int error_code, |
| 460 |
void *userdata) { |
| 461 |
|
| 462 |
(void)connection; |
| 463 |
|
| 464 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture = userdata; |
| 465 |
|
| 466 |
|
| 467 |
struct aws_mqtt3_lifecycle_event event; |
| 468 |
AWS_ZERO_STRUCT(event); |
| 469 |
|
| 470 |
event.type = AWS_MQTT3_LET_INTERRUPTED; |
| 471 |
aws_high_res_clock_get_ticks(&event.timestamp); |
| 472 |
event.error_code = error_code; |
| 473 |
|
| 474 |
aws_mutex_lock(&fixture->lock); |
| 475 |
aws_array_list_push_back(&fixture->lifecycle_events, &event); |
| 476 |
aws_mutex_unlock(&fixture->lock); |
| 477 |
aws_condition_variable_notify_all(&fixture->signal); |
| 478 |
} |
| 479 |
|
| 480 |
static void s_aws_mqtt5_to_mqtt3_adapter_test_fixture_resumed_handler( |
| 481 |
struct aws_mqtt_client_connection *connection, |
| 482 |
enum aws_mqtt_connect_return_code return_code, |
| 483 |
bool session_present, |
| 484 |
void *userdata) { |
| 485 |
|
| 486 |
(void)connection; |
| 487 |
|
| 488 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture = userdata; |
| 489 |
|
| 490 |
|
| 491 |
struct aws_mqtt3_lifecycle_event event; |
| 492 |
AWS_ZERO_STRUCT(event); |
| 493 |
|
| 494 |
event.type = AWS_MQTT3_LET_RESUMED; |
| 495 |
aws_high_res_clock_get_ticks(&event.timestamp); |
| 496 |
event.return_code = return_code; |
| 497 |
event.session_present = session_present; |
| 498 |
|
| 499 |
aws_mutex_lock(&fixture->lock); |
| 500 |
aws_array_list_push_back(&fixture->lifecycle_events, &event); |
| 501 |
aws_mutex_unlock(&fixture->lock); |
| 502 |
aws_condition_variable_notify_all(&fixture->signal); |
| 503 |
} |
| 504 |
|
| 505 |
static void s_aws_mqtt5_to_mqtt3_adapter_test_fixture_connection_failure_handler( |
| 506 |
struct aws_mqtt_client_connection *connection, |
| 507 |
int error_code, |
| 508 |
void *userdata) { |
| 509 |
|
| 510 |
(void)connection; |
| 511 |
|
| 512 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture = userdata; |
| 513 |
|
| 514 |
|
| 515 |
struct aws_mqtt3_lifecycle_event event; |
| 516 |
AWS_ZERO_STRUCT(event); |
| 517 |
|
| 518 |
event.type = AWS_MQTT3_LET_CONNECTION_FAILURE; |
| 519 |
aws_high_res_clock_get_ticks(&event.timestamp); |
| 520 |
event.error_code = error_code; |
| 521 |
|
| 522 |
aws_mutex_lock(&fixture->lock); |
| 523 |
aws_array_list_push_back(&fixture->lifecycle_events, &event); |
| 524 |
aws_mutex_unlock(&fixture->lock); |
| 525 |
aws_condition_variable_notify_all(&fixture->signal); |
| 526 |
} |
| 527 |
|
| 528 |
static void s_aws_mqtt5_to_mqtt3_adapter_test_fixture_connection_success_handler( |
| 529 |
struct aws_mqtt_client_connection *connection, |
| 530 |
enum aws_mqtt_connect_return_code return_code, |
| 531 |
bool session_present, |
| 532 |
void *userdata) { |
| 533 |
|
| 534 |
(void)connection; |
| 535 |
|
| 536 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture = userdata; |
| 537 |
|
| 538 |
|
| 539 |
struct aws_mqtt3_lifecycle_event event; |
| 540 |
AWS_ZERO_STRUCT(event); |
| 541 |
|
| 542 |
event.type = AWS_MQTT3_LET_CONNECTION_SUCCESS; |
| 543 |
aws_high_res_clock_get_ticks(&event.timestamp); |
| 544 |
event.return_code = return_code; |
| 545 |
event.session_present = session_present; |
| 546 |
|
| 547 |
aws_mutex_lock(&fixture->lock); |
| 548 |
aws_array_list_push_back(&fixture->lifecycle_events, &event); |
| 549 |
aws_mutex_unlock(&fixture->lock); |
| 550 |
aws_condition_variable_notify_all(&fixture->signal); |
| 551 |
} |
| 552 |
|
| 553 |
static void s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete( |
| 554 |
struct aws_mqtt_client_connection *connection, |
| 555 |
int error_code, |
| 556 |
enum aws_mqtt_connect_return_code return_code, |
| 557 |
bool session_present, |
| 558 |
void *user_data) { |
| 559 |
(void)connection; |
| 560 |
|
| 561 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture = user_data; |
| 562 |
|
| 563 |
struct aws_mqtt3_lifecycle_event event; |
| 564 |
AWS_ZERO_STRUCT(event); |
| 565 |
|
| 566 |
event.type = AWS_MQTT3_LET_CONNECTION_COMPLETE; |
| 567 |
aws_high_res_clock_get_ticks(&event.timestamp); |
| 568 |
event.error_code = error_code; |
| 569 |
event.return_code = return_code; |
| 570 |
event.session_present = session_present; |
| 571 |
|
| 572 |
aws_mutex_lock(&fixture->lock); |
| 573 |
aws_array_list_push_back(&fixture->lifecycle_events, &event); |
| 574 |
aws_mutex_unlock(&fixture->lock); |
| 575 |
aws_condition_variable_notify_all(&fixture->signal); |
| 576 |
} |
| 577 |
|
| 578 |
static void s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_disconnection_complete( |
| 579 |
struct aws_mqtt_client_connection *connection, |
| 580 |
void *user_data) { |
| 581 |
(void)connection; |
| 582 |
|
| 583 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture = user_data; |
| 584 |
|
| 585 |
struct aws_mqtt3_lifecycle_event event; |
| 586 |
AWS_ZERO_STRUCT(event); |
| 587 |
|
| 588 |
event.type = AWS_MQTT3_LET_DISCONNECTION_COMPLETE; |
| 589 |
aws_high_res_clock_get_ticks(&event.timestamp); |
| 590 |
|
| 591 |
aws_mutex_lock(&fixture->lock); |
| 592 |
aws_array_list_push_back(&fixture->lifecycle_events, &event); |
| 593 |
aws_mutex_unlock(&fixture->lock); |
| 594 |
aws_condition_variable_notify_all(&fixture->signal); |
| 595 |
} |
| 596 |
|
| 597 |
int aws_mqtt5_to_mqtt3_adapter_test_fixture_init( |
| 598 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture, |
| 599 |
struct aws_allocator *allocator, |
| 600 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options *mqtt5_fixture_config) { |
| 601 |
AWS_ZERO_STRUCT(*fixture); |
| 602 |
|
| 603 |
if (aws_mqtt5_client_mock_test_fixture_init(&fixture->mqtt5_fixture, allocator, mqtt5_fixture_config)) { |
| 604 |
return AWS_OP_ERR; |
| 605 |
} |
| 606 |
|
| 607 |
fixture->connection = aws_mqtt_client_connection_new_from_mqtt5_client(fixture->mqtt5_fixture.client); |
| 608 |
if (fixture->connection == NULL) { |
| 609 |
return AWS_OP_ERR; |
| 610 |
} |
| 611 |
|
| 612 |
aws_array_list_init_dynamic(&fixture->lifecycle_events, allocator, 10, sizeof(struct aws_mqtt3_lifecycle_event)); |
| 613 |
aws_array_list_init_dynamic(&fixture->operation_events, allocator, 10, sizeof(struct aws_mqtt3_operation_event)); |
| 614 |
|
| 615 |
aws_mutex_init(&fixture->lock); |
| 616 |
aws_condition_variable_init(&fixture->signal); |
| 617 |
|
| 618 |
aws_mqtt_client_connection_set_connection_termination_handler( |
| 619 |
fixture->connection, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_termination_handler, fixture); |
| 620 |
aws_mqtt_client_connection_set_connection_closed_handler( |
| 621 |
fixture->connection, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_closed_handler, fixture); |
| 622 |
aws_mqtt_client_connection_set_connection_interruption_handlers( |
| 623 |
fixture->connection, |
| 624 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_interrupted_handler, |
| 625 |
fixture, |
| 626 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_resumed_handler, |
| 627 |
fixture); |
| 628 |
aws_mqtt_client_connection_set_connection_result_handlers( |
| 629 |
fixture->connection, |
| 630 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_connection_success_handler, |
| 631 |
fixture, |
| 632 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_connection_failure_handler, |
| 633 |
fixture); |
| 634 |
|
| 635 |
return AWS_OP_SUCCESS; |
| 636 |
} |
| 637 |
|
| 638 |
void aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture) { |
| 639 |
aws_mqtt_client_connection_release(fixture->connection); |
| 640 |
|
| 641 |
s_wait_for_n_adapter_lifecycle_events(fixture, AWS_MQTT3_LET_TERMINATION, 1); |
| 642 |
|
| 643 |
aws_mqtt5_client_mock_test_fixture_clean_up(&fixture->mqtt5_fixture); |
| 644 |
|
| 645 |
aws_array_list_clean_up(&fixture->lifecycle_events); |
| 646 |
|
| 647 |
size_t operation_event_count = aws_array_list_length(&fixture->operation_events); |
| 648 |
for (size_t i = 0; i < operation_event_count; ++i) { |
| 649 |
struct aws_mqtt3_operation_event *event = NULL; |
| 650 |
aws_array_list_get_at_ptr(&fixture->operation_events, (void **)(&event), i); |
| 651 |
|
| 652 |
s_aws_mqtt3_operation_event_clean_up(event); |
| 653 |
} |
| 654 |
aws_array_list_clean_up(&fixture->operation_events); |
| 655 |
|
| 656 |
aws_mutex_clean_up(&fixture->lock); |
| 657 |
aws_condition_variable_clean_up(&fixture->signal); |
| 658 |
} |
| 659 |
|
| 660 |
void s_mqtt5to3_lifecycle_event_callback(const struct aws_mqtt5_client_lifecycle_event *event) { |
| 661 |
(void)event; |
| 662 |
} |
| 663 |
|
| 664 |
void s_mqtt5to3_publish_received_callback(const struct aws_mqtt5_packet_publish_view *publish, void *user_data) { |
| 665 |
(void)publish; |
| 666 |
(void)user_data; |
| 667 |
} |
| 668 |
|
| 669 |
static int s_do_mqtt5to3_adapter_create_destroy(struct aws_allocator *allocator, uint64_t sleep_nanos) { |
| 670 |
aws_mqtt_library_init(allocator); |
| 671 |
|
| 672 |
struct aws_mqtt5_packet_connect_view local_connect_options = { |
| 673 |
.keep_alive_interval_seconds = 30, |
| 674 |
.clean_start = true, |
| 675 |
}; |
| 676 |
|
| 677 |
struct aws_mqtt5_client_options client_options = { |
| 678 |
.connect_options = &local_connect_options, |
| 679 |
.lifecycle_event_handler = s_mqtt5to3_lifecycle_event_callback, |
| 680 |
.lifecycle_event_handler_user_data = NULL, |
| 681 |
.publish_received_handler = s_mqtt5to3_publish_received_callback, |
| 682 |
.publish_received_handler_user_data = NULL, |
| 683 |
.ping_timeout_ms = 10000, |
| 684 |
}; |
| 685 |
|
| 686 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_config = { |
| 687 |
.client_options = &client_options, |
| 688 |
}; |
| 689 |
|
| 690 |
struct aws_mqtt5_client_mock_test_fixture test_fixture; |
| 691 |
AWS_ZERO_STRUCT(test_fixture); |
| 692 |
|
| 693 |
ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_fixture, allocator, &test_fixture_config)); |
| 694 |
|
| 695 |
struct aws_mqtt_client_connection *connection = |
| 696 |
aws_mqtt_client_connection_new_from_mqtt5_client(test_fixture.client); |
| 697 |
|
| 698 |
if (sleep_nanos > 0) { |
| 699 |
|
| 700 |
aws_thread_current_sleep(aws_timestamp_convert(1, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL)); |
| 701 |
} |
| 702 |
|
| 703 |
aws_mqtt_client_connection_release(connection); |
| 704 |
|
| 705 |
if (sleep_nanos > 0) { |
| 706 |
|
| 707 |
aws_thread_current_sleep(aws_timestamp_convert(1, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL)); |
| 708 |
} |
| 709 |
|
| 710 |
aws_mqtt5_client_mock_test_fixture_clean_up(&test_fixture); |
| 711 |
|
| 712 |
aws_mqtt_library_clean_up(); |
| 713 |
|
| 714 |
return AWS_OP_SUCCESS; |
| 715 |
} |
| 716 |
|
| 717 |
static int s_mqtt5to3_adapter_create_destroy_fn(struct aws_allocator *allocator, void *ctx) { |
| 718 |
(void)ctx; |
| 719 |
|
| 720 |
ASSERT_SUCCESS(s_do_mqtt5to3_adapter_create_destroy(allocator, 0)); |
| 721 |
|
| 722 |
return AWS_OP_SUCCESS; |
| 723 |
} |
| 724 |
|
| 725 |
AWS_TEST_CASE(mqtt5to3_adapter_create_destroy, s_mqtt5to3_adapter_create_destroy_fn) |
| 726 |
|
| 727 |
static int s_mqtt5to3_adapter_create_destroy_delayed_fn(struct aws_allocator *allocator, void *ctx) { |
| 728 |
(void)ctx; |
| 729 |
|
| 730 |
ASSERT_SUCCESS(s_do_mqtt5to3_adapter_create_destroy( |
| 731 |
allocator, aws_timestamp_convert(1, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL))); |
| 732 |
|
| 733 |
return AWS_OP_SUCCESS; |
| 734 |
} |
| 735 |
|
| 736 |
AWS_TEST_CASE(mqtt5to3_adapter_create_destroy_delayed, s_mqtt5to3_adapter_create_destroy_delayed_fn) |
| 737 |
|
| 738 |
typedef int (*mqtt5to3_adapter_config_test_setup_fn)( |
| 739 |
struct aws_allocator *allocator, |
| 740 |
struct aws_mqtt_client_connection *adapter, |
| 741 |
struct aws_mqtt5_packet_connect_storage *expected_connect); |
| 742 |
|
| 743 |
static int s_do_mqtt5to3_adapter_config_test( |
| 744 |
struct aws_allocator *allocator, |
| 745 |
mqtt5to3_adapter_config_test_setup_fn setup_fn) { |
| 746 |
aws_mqtt_library_init(allocator); |
| 747 |
|
| 748 |
struct mqtt5_client_test_options test_options; |
| 749 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 750 |
|
| 751 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 752 |
.client_options = &test_options.client_options, |
| 753 |
.server_function_table = &test_options.server_function_table, |
| 754 |
}; |
| 755 |
|
| 756 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture test_fixture; |
| 757 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&test_fixture, allocator, &test_fixture_options)); |
| 758 |
|
| 759 |
struct aws_mqtt5_client *client = test_fixture.mqtt5_fixture.client; |
| 760 |
|
| 761 |
struct aws_mqtt_client_connection *adapter = test_fixture.connection; |
| 762 |
|
| 763 |
struct aws_mqtt5_packet_connect_storage expected_connect_storage; |
| 764 |
ASSERT_SUCCESS((*setup_fn)(allocator, adapter, &expected_connect_storage)); |
| 765 |
|
| 766 |
ASSERT_SUCCESS(aws_mqtt5_client_start(client)); |
| 767 |
|
| 768 |
aws_wait_for_connected_lifecycle_event(&test_fixture.mqtt5_fixture); |
| 769 |
|
| 770 |
ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL)); |
| 771 |
|
| 772 |
aws_wait_for_stopped_lifecycle_event(&test_fixture.mqtt5_fixture); |
| 773 |
|
| 774 |
struct aws_mqtt5_mock_server_packet_record expected_packets[] = { |
| 775 |
{ |
| 776 |
.packet_type = AWS_MQTT5_PT_CONNECT, |
| 777 |
.packet_storage = &expected_connect_storage, |
| 778 |
}, |
| 779 |
}; |
| 780 |
ASSERT_SUCCESS(aws_verify_received_packet_sequence( |
| 781 |
&test_fixture.mqtt5_fixture, expected_packets, AWS_ARRAY_SIZE(expected_packets))); |
| 782 |
|
| 783 |
aws_mqtt5_packet_connect_storage_clean_up(&expected_connect_storage); |
| 784 |
|
| 785 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&test_fixture); |
| 786 |
|
| 787 |
aws_mqtt_library_clean_up(); |
| 788 |
|
| 789 |
return AWS_OP_SUCCESS; |
| 790 |
} |
| 791 |
|
| 792 |
AWS_STATIC_STRING_FROM_LITERAL(s_simple_topic, "Hello/World"); |
| 793 |
AWS_STATIC_STRING_FROM_LITERAL(s_simple_payload, "A Payload"); |
| 794 |
|
| 795 |
static int s_mqtt5to3_adapter_set_will_setup( |
| 796 |
struct aws_allocator *allocator, |
| 797 |
struct aws_mqtt_client_connection *adapter, |
| 798 |
struct aws_mqtt5_packet_connect_storage *expected_connect) { |
| 799 |
|
| 800 |
struct aws_byte_cursor topic_cursor = aws_byte_cursor_from_string(s_simple_topic); |
| 801 |
struct aws_byte_cursor payload_cursor = aws_byte_cursor_from_string(s_simple_payload); |
| 802 |
|
| 803 |
ASSERT_SUCCESS( |
| 804 |
aws_mqtt_client_connection_set_will(adapter, &topic_cursor, AWS_MQTT_QOS_AT_LEAST_ONCE, true, &payload_cursor)); |
| 805 |
|
| 806 |
struct aws_mqtt5_packet_publish_view expected_will = { |
| 807 |
.payload = payload_cursor, |
| 808 |
.qos = AWS_MQTT5_QOS_AT_LEAST_ONCE, |
| 809 |
.retain = true, |
| 810 |
.topic = topic_cursor, |
| 811 |
}; |
| 812 |
|
| 813 |
struct aws_mqtt5_packet_connect_view expected_connect_view = { |
| 814 |
.client_id = aws_byte_cursor_from_string(g_default_client_id), |
| 815 |
.keep_alive_interval_seconds = 30, |
| 816 |
.clean_start = true, |
| 817 |
.will = &expected_will, |
| 818 |
}; |
| 819 |
|
| 820 |
ASSERT_SUCCESS(aws_mqtt5_packet_connect_storage_init(expected_connect, allocator, &expected_connect_view, NULL)); |
| 821 |
|
| 822 |
return AWS_OP_SUCCESS; |
| 823 |
} |
| 824 |
|
| 825 |
static int s_mqtt5to3_adapter_set_will_fn(struct aws_allocator *allocator, void *ctx) { |
| 826 |
(void)ctx; |
| 827 |
|
| 828 |
ASSERT_SUCCESS(s_do_mqtt5to3_adapter_config_test(allocator, s_mqtt5to3_adapter_set_will_setup)); |
| 829 |
|
| 830 |
return AWS_OP_SUCCESS; |
| 831 |
} |
| 832 |
|
| 833 |
AWS_TEST_CASE(mqtt5to3_adapter_set_will, s_mqtt5to3_adapter_set_will_fn) |
| 834 |
|
| 835 |
AWS_STATIC_STRING_FROM_LITERAL(s_username, "MyUsername"); |
| 836 |
AWS_STATIC_STRING_FROM_LITERAL(s_password, "TopTopSecret"); |
| 837 |
|
| 838 |
static int s_mqtt5to3_adapter_set_login_setup( |
| 839 |
struct aws_allocator *allocator, |
| 840 |
struct aws_mqtt_client_connection *adapter, |
| 841 |
struct aws_mqtt5_packet_connect_storage *expected_connect) { |
| 842 |
|
| 843 |
struct aws_byte_cursor username_cursor = aws_byte_cursor_from_string(s_username); |
| 844 |
struct aws_byte_cursor password_cursor = aws_byte_cursor_from_string(s_password); |
| 845 |
|
| 846 |
ASSERT_SUCCESS(aws_mqtt_client_connection_set_login(adapter, &username_cursor, &password_cursor)); |
| 847 |
|
| 848 |
struct aws_mqtt5_packet_connect_view expected_connect_view = { |
| 849 |
.client_id = aws_byte_cursor_from_string(g_default_client_id), |
| 850 |
.keep_alive_interval_seconds = 30, |
| 851 |
.clean_start = true, |
| 852 |
.username = &username_cursor, |
| 853 |
.password = &password_cursor, |
| 854 |
}; |
| 855 |
|
| 856 |
ASSERT_SUCCESS(aws_mqtt5_packet_connect_storage_init(expected_connect, allocator, &expected_connect_view, NULL)); |
| 857 |
|
| 858 |
return AWS_OP_SUCCESS; |
| 859 |
} |
| 860 |
|
| 861 |
static int s_mqtt5to3_adapter_set_login_fn(struct aws_allocator *allocator, void *ctx) { |
| 862 |
(void)ctx; |
| 863 |
|
| 864 |
ASSERT_SUCCESS(s_do_mqtt5to3_adapter_config_test(allocator, s_mqtt5to3_adapter_set_login_setup)); |
| 865 |
|
| 866 |
return AWS_OP_SUCCESS; |
| 867 |
} |
| 868 |
|
| 869 |
AWS_TEST_CASE(mqtt5to3_adapter_set_login, s_mqtt5to3_adapter_set_login_fn) |
| 870 |
|
| 871 |
static int s_mqtt5to3_adapter_set_reconnect_timeout_fn(struct aws_allocator *allocator, void *ctx) { |
| 872 |
(void)ctx; |
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
aws_mqtt_library_init(allocator); |
| 881 |
|
| 882 |
struct mqtt5_client_test_options test_options; |
| 883 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 884 |
|
| 885 |
|
| 886 |
test_options.client_options.retry_jitter_mode = AWS_EXPONENTIAL_BACKOFF_JITTER_NONE; |
| 887 |
test_options.client_options.min_reconnect_delay_ms = 10; |
| 888 |
test_options.client_options.max_reconnect_delay_ms = 50; |
| 889 |
test_options.client_options.min_connected_time_to_reset_reconnect_delay_ms = RECONNECT_TEST_BACKOFF_RESET_DELAY; |
| 890 |
|
| 891 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] = |
| 892 |
aws_mqtt5_mock_server_handle_connect_always_fail; |
| 893 |
|
| 894 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 895 |
.client_options = &test_options.client_options, |
| 896 |
.server_function_table = &test_options.server_function_table, |
| 897 |
}; |
| 898 |
|
| 899 |
struct aws_mqtt5_client_mock_test_fixture test_context; |
| 900 |
ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options)); |
| 901 |
|
| 902 |
struct aws_mqtt5_client *client = test_context.client; |
| 903 |
|
| 904 |
struct aws_mqtt_client_connection *adapter = aws_mqtt_client_connection_new_from_mqtt5_client(client); |
| 905 |
|
| 906 |
aws_mqtt_client_connection_set_reconnect_timeout(adapter, RECONNECT_TEST_MIN_BACKOFF, RECONNECT_TEST_MAX_BACKOFF); |
| 907 |
|
| 908 |
ASSERT_SUCCESS(aws_mqtt5_client_start(client)); |
| 909 |
|
| 910 |
aws_mqtt5_wait_for_n_lifecycle_events(&test_context, AWS_MQTT5_CLET_CONNECTION_FAILURE, 6); |
| 911 |
|
| 912 |
ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL)); |
| 913 |
|
| 914 |
aws_wait_for_stopped_lifecycle_event(&test_context); |
| 915 |
|
| 916 |
ASSERT_SUCCESS(aws_verify_reconnection_exponential_backoff_timestamps(&test_context)); |
| 917 |
|
| 918 |
|
| 919 |
enum aws_mqtt5_client_state expected_states[] = { |
| 920 |
AWS_MCS_CONNECTING, AWS_MCS_MQTT_CONNECT, AWS_MCS_CHANNEL_SHUTDOWN, AWS_MCS_PENDING_RECONNECT, |
| 921 |
AWS_MCS_CONNECTING, AWS_MCS_MQTT_CONNECT, AWS_MCS_CHANNEL_SHUTDOWN, AWS_MCS_PENDING_RECONNECT, |
| 922 |
AWS_MCS_CONNECTING, AWS_MCS_MQTT_CONNECT, AWS_MCS_CHANNEL_SHUTDOWN, AWS_MCS_PENDING_RECONNECT, |
| 923 |
AWS_MCS_CONNECTING, AWS_MCS_MQTT_CONNECT, AWS_MCS_CHANNEL_SHUTDOWN, AWS_MCS_PENDING_RECONNECT, |
| 924 |
AWS_MCS_CONNECTING, AWS_MCS_MQTT_CONNECT, AWS_MCS_CHANNEL_SHUTDOWN, AWS_MCS_PENDING_RECONNECT, |
| 925 |
AWS_MCS_CONNECTING, AWS_MCS_MQTT_CONNECT, AWS_MCS_CHANNEL_SHUTDOWN, |
| 926 |
}; |
| 927 |
ASSERT_SUCCESS(aws_verify_client_state_sequence(&test_context, expected_states, AWS_ARRAY_SIZE(expected_states))); |
| 928 |
|
| 929 |
aws_mqtt_client_connection_release(adapter); |
| 930 |
|
| 931 |
aws_mqtt5_client_mock_test_fixture_clean_up(&test_context); |
| 932 |
aws_mqtt_library_clean_up(); |
| 933 |
|
| 934 |
return AWS_OP_SUCCESS; |
| 935 |
} |
| 936 |
|
| 937 |
AWS_TEST_CASE(mqtt5to3_adapter_set_reconnect_timeout, s_mqtt5to3_adapter_set_reconnect_timeout_fn) |
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
static int s_mqtt5to3_adapter_connect_success_fn(struct aws_allocator *allocator, void *ctx) { |
| 943 |
(void)ctx; |
| 944 |
|
| 945 |
aws_mqtt_library_init(allocator); |
| 946 |
|
| 947 |
struct mqtt5_client_test_options test_options; |
| 948 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 949 |
|
| 950 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 951 |
.client_options = &test_options.client_options, |
| 952 |
.server_function_table = &test_options.server_function_table, |
| 953 |
}; |
| 954 |
|
| 955 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 956 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 957 |
|
| 958 |
struct aws_mqtt_client_connection *adapter = fixture.connection; |
| 959 |
|
| 960 |
struct aws_mqtt_connection_options connection_options; |
| 961 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 962 |
|
| 963 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 964 |
connection_options.user_data = &fixture; |
| 965 |
|
| 966 |
aws_mqtt_client_connection_connect(adapter, &connection_options); |
| 967 |
|
| 968 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 969 |
|
| 970 |
struct aws_mqtt3_lifecycle_event expected_events[] = { |
| 971 |
{ |
| 972 |
.type = AWS_MQTT3_LET_CONNECTION_SUCCESS, |
| 973 |
}, |
| 974 |
{ |
| 975 |
.type = AWS_MQTT3_LET_CONNECTION_COMPLETE, |
| 976 |
}, |
| 977 |
}; |
| 978 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_lifecycle_sequence( |
| 979 |
&fixture, AWS_ARRAY_SIZE(expected_events), expected_events, AWS_ARRAY_SIZE(expected_events))); |
| 980 |
|
| 981 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 982 |
aws_mqtt_library_clean_up(); |
| 983 |
|
| 984 |
return AWS_OP_SUCCESS; |
| 985 |
} |
| 986 |
|
| 987 |
AWS_TEST_CASE(mqtt5to3_adapter_connect_success, s_mqtt5to3_adapter_connect_success_fn) |
| 988 |
|
| 989 |
static int s_do_mqtt5to3_adapter_connect_success_disconnect_success_cycle( |
| 990 |
struct aws_allocator *allocator, |
| 991 |
size_t iterations) { |
| 992 |
aws_mqtt_library_init(allocator); |
| 993 |
|
| 994 |
struct mqtt5_client_test_options test_options; |
| 995 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 996 |
|
| 997 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 998 |
.client_options = &test_options.client_options, |
| 999 |
.server_function_table = &test_options.server_function_table, |
| 1000 |
}; |
| 1001 |
|
| 1002 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 1003 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 1004 |
|
| 1005 |
struct aws_mqtt_client_connection *adapter = fixture.connection; |
| 1006 |
|
| 1007 |
for (size_t i = 0; i < iterations; ++i) { |
| 1008 |
struct aws_mqtt_connection_options connection_options; |
| 1009 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 1010 |
|
| 1011 |
connection_options.on_connection_complete = |
| 1012 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 1013 |
connection_options.user_data = &fixture; |
| 1014 |
|
| 1015 |
aws_mqtt_client_connection_connect(adapter, &connection_options); |
| 1016 |
|
| 1017 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, i + 1); |
| 1018 |
|
| 1019 |
aws_mqtt_client_connection_disconnect( |
| 1020 |
adapter, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_disconnection_complete, &fixture); |
| 1021 |
|
| 1022 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_DISCONNECTION_COMPLETE, i + 1); |
| 1023 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CLOSED, i + 1); |
| 1024 |
|
| 1025 |
struct aws_mqtt3_lifecycle_event expected_event_sequence[] = { |
| 1026 |
{ |
| 1027 |
.type = AWS_MQTT3_LET_CONNECTION_SUCCESS, |
| 1028 |
}, |
| 1029 |
{ |
| 1030 |
.type = AWS_MQTT3_LET_CONNECTION_COMPLETE, |
| 1031 |
}, |
| 1032 |
{ |
| 1033 |
.type = AWS_MQTT3_LET_DISCONNECTION_COMPLETE, |
| 1034 |
}, |
| 1035 |
{ |
| 1036 |
.type = AWS_MQTT3_LET_CLOSED, |
| 1037 |
}, |
| 1038 |
}; |
| 1039 |
size_t sequence_size = AWS_ARRAY_SIZE(expected_event_sequence); |
| 1040 |
|
| 1041 |
size_t expected_event_count = (i + 1) * sequence_size; |
| 1042 |
struct aws_mqtt3_lifecycle_event *expected_events = |
| 1043 |
aws_mem_calloc(allocator, expected_event_count, sizeof(struct aws_mqtt3_lifecycle_event)); |
| 1044 |
for (size_t j = 0; j < i + 1; ++j) { |
| 1045 |
for (size_t k = 0; k < sequence_size; ++k) { |
| 1046 |
*(expected_events + j * sequence_size + k) = expected_event_sequence[k]; |
| 1047 |
} |
| 1048 |
} |
| 1049 |
|
| 1050 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_lifecycle_sequence( |
| 1051 |
&fixture, expected_event_count, expected_events, expected_event_count)); |
| 1052 |
|
| 1053 |
aws_mem_release(allocator, expected_events); |
| 1054 |
} |
| 1055 |
|
| 1056 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 1057 |
aws_mqtt_library_clean_up(); |
| 1058 |
|
| 1059 |
return AWS_OP_SUCCESS; |
| 1060 |
} |
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
static int s_mqtt5to3_adapter_connect_success_disconnect_success_fn(struct aws_allocator *allocator, void *ctx) { |
| 1067 |
(void)ctx; |
| 1068 |
|
| 1069 |
ASSERT_SUCCESS(s_do_mqtt5to3_adapter_connect_success_disconnect_success_cycle(allocator, 1)); |
| 1070 |
|
| 1071 |
return AWS_OP_SUCCESS; |
| 1072 |
} |
| 1073 |
|
| 1074 |
AWS_TEST_CASE( |
| 1075 |
mqtt5to3_adapter_connect_success_disconnect_success, |
| 1076 |
s_mqtt5to3_adapter_connect_success_disconnect_success_fn) |
| 1077 |
|
| 1078 |
static int s_mqtt5to3_adapter_connect_success_disconnect_success_thrice_fn(struct aws_allocator *allocator, void *ctx) { |
| 1079 |
(void)ctx; |
| 1080 |
|
| 1081 |
ASSERT_SUCCESS(s_do_mqtt5to3_adapter_connect_success_disconnect_success_cycle(allocator, 3)); |
| 1082 |
|
| 1083 |
return AWS_OP_SUCCESS; |
| 1084 |
} |
| 1085 |
|
| 1086 |
AWS_TEST_CASE( |
| 1087 |
mqtt5to3_adapter_connect_success_disconnect_success_thrice, |
| 1088 |
s_mqtt5to3_adapter_connect_success_disconnect_success_thrice_fn) |
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
static int s_mqtt5to3_adapter_connect_success_connect_failure_fn(struct aws_allocator *allocator, void *ctx) { |
| 1095 |
(void)ctx; |
| 1096 |
|
| 1097 |
aws_mqtt_library_init(allocator); |
| 1098 |
|
| 1099 |
struct mqtt5_client_test_options test_options; |
| 1100 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 1101 |
|
| 1102 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 1103 |
.client_options = &test_options.client_options, |
| 1104 |
.server_function_table = &test_options.server_function_table, |
| 1105 |
}; |
| 1106 |
|
| 1107 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 1108 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 1109 |
|
| 1110 |
struct aws_mqtt_client_connection *adapter = fixture.connection; |
| 1111 |
|
| 1112 |
struct aws_mqtt_connection_options connection_options; |
| 1113 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 1114 |
|
| 1115 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 1116 |
connection_options.user_data = &fixture; |
| 1117 |
|
| 1118 |
aws_mqtt_client_connection_connect(adapter, &connection_options); |
| 1119 |
|
| 1120 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 1121 |
|
| 1122 |
aws_mqtt_client_connection_connect(adapter, &connection_options); |
| 1123 |
|
| 1124 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 2); |
| 1125 |
|
| 1126 |
struct aws_mqtt3_lifecycle_event expected_events[] = { |
| 1127 |
{ |
| 1128 |
.type = AWS_MQTT3_LET_CONNECTION_SUCCESS, |
| 1129 |
}, |
| 1130 |
{ |
| 1131 |
.type = AWS_MQTT3_LET_CONNECTION_COMPLETE, |
| 1132 |
}, |
| 1133 |
{ |
| 1134 |
.type = AWS_MQTT3_LET_CONNECTION_COMPLETE, |
| 1135 |
.error_code = AWS_ERROR_MQTT_ALREADY_CONNECTED, |
| 1136 |
}, |
| 1137 |
}; |
| 1138 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_lifecycle_sequence( |
| 1139 |
&fixture, AWS_ARRAY_SIZE(expected_events), expected_events, AWS_ARRAY_SIZE(expected_events))); |
| 1140 |
|
| 1141 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 1142 |
aws_mqtt_library_clean_up(); |
| 1143 |
|
| 1144 |
return AWS_OP_SUCCESS; |
| 1145 |
} |
| 1146 |
|
| 1147 |
AWS_TEST_CASE(mqtt5to3_adapter_connect_success_connect_failure, s_mqtt5to3_adapter_connect_success_connect_failure_fn) |
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
static int s_mqtt5to3_adapter_connect_success_sloppy_shutdown_fn(struct aws_allocator *allocator, void *ctx) { |
| 1154 |
(void)ctx; |
| 1155 |
|
| 1156 |
aws_mqtt_library_init(allocator); |
| 1157 |
|
| 1158 |
struct mqtt5_client_test_options test_options; |
| 1159 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 1160 |
|
| 1161 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 1162 |
.client_options = &test_options.client_options, |
| 1163 |
.server_function_table = &test_options.server_function_table, |
| 1164 |
}; |
| 1165 |
|
| 1166 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 1167 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 1168 |
|
| 1169 |
struct aws_mqtt_client_connection *adapter = fixture.connection; |
| 1170 |
|
| 1171 |
struct aws_mqtt_connection_options connection_options; |
| 1172 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 1173 |
|
| 1174 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 1175 |
connection_options.user_data = &fixture; |
| 1176 |
|
| 1177 |
aws_mqtt_client_connection_connect(adapter, &connection_options); |
| 1178 |
|
| 1179 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 1180 |
aws_mqtt_library_clean_up(); |
| 1181 |
|
| 1182 |
return AWS_OP_SUCCESS; |
| 1183 |
} |
| 1184 |
|
| 1185 |
AWS_TEST_CASE(mqtt5to3_adapter_connect_success_sloppy_shutdown, s_mqtt5to3_adapter_connect_success_sloppy_shutdown_fn) |
| 1186 |
|
| 1187 |
static int s_aws_mqtt5_server_disconnect_after_connect( |
| 1188 |
void *packet, |
| 1189 |
struct aws_mqtt5_server_mock_connection_context *connection, |
| 1190 |
void *user_data) { |
| 1191 |
aws_mqtt5_mock_server_handle_connect_always_succeed(packet, connection, user_data); |
| 1192 |
|
| 1193 |
struct aws_mqtt5_packet_disconnect_view disconnect = { |
| 1194 |
.reason_code = AWS_MQTT5_DRC_SERVER_SHUTTING_DOWN, |
| 1195 |
}; |
| 1196 |
|
| 1197 |
int result = aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_DISCONNECT, &disconnect); |
| 1198 |
|
| 1199 |
return result; |
| 1200 |
} |
| 1201 |
|
| 1202 |
static int s_verify_bad_connectivity_callbacks(struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture) { |
| 1203 |
struct aws_mqtt3_lifecycle_event expected_events_start[] = { |
| 1204 |
{ |
| 1205 |
.type = AWS_MQTT3_LET_CONNECTION_SUCCESS, |
| 1206 |
}, |
| 1207 |
{ |
| 1208 |
.type = AWS_MQTT3_LET_CONNECTION_COMPLETE, |
| 1209 |
}, |
| 1210 |
{ |
| 1211 |
.type = AWS_MQTT3_LET_INTERRUPTED, |
| 1212 |
.error_code = AWS_ERROR_MQTT_UNEXPECTED_HANGUP, |
| 1213 |
}, |
| 1214 |
{ |
| 1215 |
.type = AWS_MQTT3_LET_CONNECTION_SUCCESS, |
| 1216 |
}, |
| 1217 |
{ |
| 1218 |
.type = AWS_MQTT3_LET_RESUMED, |
| 1219 |
}, |
| 1220 |
{ |
| 1221 |
.type = AWS_MQTT3_LET_INTERRUPTED, |
| 1222 |
.error_code = AWS_ERROR_MQTT_UNEXPECTED_HANGUP, |
| 1223 |
}, |
| 1224 |
{ |
| 1225 |
.type = AWS_MQTT3_LET_CONNECTION_SUCCESS, |
| 1226 |
}, |
| 1227 |
{ |
| 1228 |
.type = AWS_MQTT3_LET_RESUMED, |
| 1229 |
}, |
| 1230 |
{ |
| 1231 |
.type = AWS_MQTT3_LET_INTERRUPTED, |
| 1232 |
.error_code = AWS_ERROR_MQTT_UNEXPECTED_HANGUP, |
| 1233 |
}, |
| 1234 |
}; |
| 1235 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_lifecycle_sequence_starts_with( |
| 1236 |
fixture, AWS_ARRAY_SIZE(expected_events_start), expected_events_start)); |
| 1237 |
|
| 1238 |
struct aws_mqtt3_lifecycle_event expected_events_end[] = { |
| 1239 |
{ |
| 1240 |
.type = AWS_MQTT3_LET_DISCONNECTION_COMPLETE, |
| 1241 |
}, |
| 1242 |
{ |
| 1243 |
.type = AWS_MQTT3_LET_CLOSED, |
| 1244 |
}, |
| 1245 |
}; |
| 1246 |
|
| 1247 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_lifecycle_sequence_ends_with( |
| 1248 |
fixture, AWS_ARRAY_SIZE(expected_events_end), expected_events_end)); |
| 1249 |
|
| 1250 |
return AWS_OP_SUCCESS; |
| 1251 |
} |
| 1252 |
|
| 1253 |
static int s_do_bad_connectivity_basic_test(struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture) { |
| 1254 |
struct aws_mqtt_client_connection *adapter = fixture->connection; |
| 1255 |
|
| 1256 |
struct aws_mqtt_connection_options connection_options; |
| 1257 |
s_init_adapter_connection_options_from_fixture(&connection_options, fixture); |
| 1258 |
|
| 1259 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 1260 |
connection_options.user_data = fixture; |
| 1261 |
|
| 1262 |
aws_mqtt_client_connection_connect(adapter, &connection_options); |
| 1263 |
|
| 1264 |
s_wait_for_n_adapter_lifecycle_events(fixture, AWS_MQTT3_LET_INTERRUPTED, 3); |
| 1265 |
|
| 1266 |
aws_mqtt_client_connection_disconnect( |
| 1267 |
adapter, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_disconnection_complete, fixture); |
| 1268 |
|
| 1269 |
s_wait_for_n_adapter_lifecycle_events(fixture, AWS_MQTT3_LET_CLOSED, 1); |
| 1270 |
|
| 1271 |
ASSERT_SUCCESS(s_verify_bad_connectivity_callbacks(fixture)); |
| 1272 |
|
| 1273 |
return AWS_OP_SUCCESS; |
| 1274 |
} |
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
static int s_mqtt5to3_adapter_connect_bad_connectivity_fn(struct aws_allocator *allocator, void *ctx) { |
| 1281 |
(void)ctx; |
| 1282 |
|
| 1283 |
aws_mqtt_library_init(allocator); |
| 1284 |
|
| 1285 |
struct mqtt5_client_test_options test_options; |
| 1286 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 1287 |
|
| 1288 |
|
| 1289 |
test_options.client_options.min_reconnect_delay_ms = 500; |
| 1290 |
test_options.client_options.max_reconnect_delay_ms = 1000; |
| 1291 |
|
| 1292 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] = |
| 1293 |
s_aws_mqtt5_server_disconnect_after_connect; |
| 1294 |
|
| 1295 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 1296 |
.client_options = &test_options.client_options, |
| 1297 |
.server_function_table = &test_options.server_function_table, |
| 1298 |
}; |
| 1299 |
|
| 1300 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 1301 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 1302 |
|
| 1303 |
ASSERT_SUCCESS(s_do_bad_connectivity_basic_test(&fixture)); |
| 1304 |
|
| 1305 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 1306 |
aws_mqtt_library_clean_up(); |
| 1307 |
|
| 1308 |
return AWS_OP_SUCCESS; |
| 1309 |
} |
| 1310 |
|
| 1311 |
AWS_TEST_CASE(mqtt5to3_adapter_connect_bad_connectivity, s_mqtt5to3_adapter_connect_bad_connectivity_fn) |
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
static int s_mqtt5to3_adapter_connect_bad_connectivity_with_mqtt5_restart_fn( |
| 1318 |
struct aws_allocator *allocator, |
| 1319 |
void *ctx) { |
| 1320 |
(void)ctx; |
| 1321 |
|
| 1322 |
aws_mqtt_library_init(allocator); |
| 1323 |
|
| 1324 |
struct mqtt5_client_test_options test_options; |
| 1325 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 1326 |
|
| 1327 |
|
| 1328 |
test_options.client_options.min_reconnect_delay_ms = 500; |
| 1329 |
test_options.client_options.max_reconnect_delay_ms = 1000; |
| 1330 |
|
| 1331 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] = |
| 1332 |
s_aws_mqtt5_server_disconnect_after_connect; |
| 1333 |
|
| 1334 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 1335 |
.client_options = &test_options.client_options, |
| 1336 |
.server_function_table = &test_options.server_function_table, |
| 1337 |
}; |
| 1338 |
|
| 1339 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 1340 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 1341 |
|
| 1342 |
ASSERT_SUCCESS(s_do_bad_connectivity_basic_test(&fixture)); |
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
aws_mqtt5_client_start(fixture.mqtt5_fixture.client); |
| 1349 |
|
| 1350 |
aws_mqtt5_wait_for_n_lifecycle_events(&fixture.mqtt5_fixture, AWS_MQTT5_CLET_CONNECTION_SUCCESS, 6); |
| 1351 |
|
| 1352 |
aws_thread_current_sleep(aws_timestamp_convert(2, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL)); |
| 1353 |
|
| 1354 |
ASSERT_SUCCESS(s_verify_bad_connectivity_callbacks(&fixture)); |
| 1355 |
|
| 1356 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 1357 |
aws_mqtt_library_clean_up(); |
| 1358 |
|
| 1359 |
return AWS_OP_SUCCESS; |
| 1360 |
} |
| 1361 |
|
| 1362 |
AWS_TEST_CASE( |
| 1363 |
mqtt5to3_adapter_connect_bad_connectivity_with_mqtt5_restart, |
| 1364 |
s_mqtt5to3_adapter_connect_bad_connectivity_with_mqtt5_restart_fn) |
| 1365 |
|
| 1366 |
int aws_mqtt5_mock_server_handle_connect_succeed_on_or_after_nth( |
| 1367 |
void *packet, |
| 1368 |
struct aws_mqtt5_server_mock_connection_context *connection, |
| 1369 |
void *user_data) { |
| 1370 |
(void)packet; |
| 1371 |
|
| 1372 |
struct aws_mqtt5_mock_server_reconnect_state *context = user_data; |
| 1373 |
|
| 1374 |
struct aws_mqtt5_packet_connack_view connack_view; |
| 1375 |
AWS_ZERO_STRUCT(connack_view); |
| 1376 |
|
| 1377 |
if (context->connection_attempts >= context->required_connection_count_threshold) { |
| 1378 |
connack_view.reason_code = AWS_MQTT5_CRC_SUCCESS; |
| 1379 |
aws_high_res_clock_get_ticks(&context->connect_timestamp); |
| 1380 |
} else { |
| 1381 |
connack_view.reason_code = AWS_MQTT5_CRC_NOT_AUTHORIZED; |
| 1382 |
} |
| 1383 |
|
| 1384 |
++context->connection_attempts; |
| 1385 |
|
| 1386 |
return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_CONNACK, &connack_view); |
| 1387 |
} |
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
static int s_mqtt5to3_adapter_connect_failure_connect_success_via_mqtt5_fn(struct aws_allocator *allocator, void *ctx) { |
| 1394 |
(void)ctx; |
| 1395 |
|
| 1396 |
aws_mqtt_library_init(allocator); |
| 1397 |
|
| 1398 |
struct aws_mqtt5_mock_server_reconnect_state mock_server_state = { |
| 1399 |
.required_connection_count_threshold = 1, |
| 1400 |
}; |
| 1401 |
|
| 1402 |
struct mqtt5_client_test_options test_options; |
| 1403 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 1404 |
|
| 1405 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] = |
| 1406 |
aws_mqtt5_mock_server_handle_connect_succeed_on_or_after_nth; |
| 1407 |
|
| 1408 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 1409 |
.client_options = &test_options.client_options, |
| 1410 |
.server_function_table = &test_options.server_function_table, |
| 1411 |
.mock_server_user_data = &mock_server_state, |
| 1412 |
}; |
| 1413 |
|
| 1414 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 1415 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 1416 |
|
| 1417 |
struct aws_mqtt_client_connection *adapter = fixture.connection; |
| 1418 |
|
| 1419 |
struct aws_mqtt_connection_options connection_options; |
| 1420 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 1421 |
|
| 1422 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 1423 |
connection_options.user_data = &fixture; |
| 1424 |
|
| 1425 |
aws_mqtt_client_connection_connect(adapter, &connection_options); |
| 1426 |
|
| 1427 |
|
| 1428 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 1429 |
|
| 1430 |
struct aws_mqtt3_lifecycle_event expected_events[] = { |
| 1431 |
{ |
| 1432 |
.type = AWS_MQTT3_LET_CONNECTION_FAILURE, |
| 1433 |
.error_code = AWS_ERROR_MQTT_PROTOCOL_ERROR, |
| 1434 |
}, |
| 1435 |
{ |
| 1436 |
.type = AWS_MQTT3_LET_CONNECTION_COMPLETE, |
| 1437 |
.error_code = AWS_ERROR_MQTT_PROTOCOL_ERROR, |
| 1438 |
}, |
| 1439 |
}; |
| 1440 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_lifecycle_sequence( |
| 1441 |
&fixture, AWS_ARRAY_SIZE(expected_events), expected_events, AWS_ARRAY_SIZE(expected_events))); |
| 1442 |
|
| 1443 |
|
| 1444 |
aws_mqtt5_wait_for_n_lifecycle_events(&fixture.mqtt5_fixture, AWS_MQTT5_CLET_CONNECTION_SUCCESS, 1); |
| 1445 |
|
| 1446 |
|
| 1447 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_lifecycle_sequence( |
| 1448 |
&fixture, AWS_ARRAY_SIZE(expected_events), expected_events, AWS_ARRAY_SIZE(expected_events))); |
| 1449 |
|
| 1450 |
|
| 1451 |
aws_mqtt_client_connection_connect(adapter, &connection_options); |
| 1452 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 2); |
| 1453 |
|
| 1454 |
struct aws_mqtt3_lifecycle_event expected_reconnect_events[] = { |
| 1455 |
{ |
| 1456 |
.type = AWS_MQTT3_LET_CONNECTION_FAILURE, |
| 1457 |
.error_code = AWS_ERROR_MQTT_PROTOCOL_ERROR, |
| 1458 |
}, |
| 1459 |
{ |
| 1460 |
.type = AWS_MQTT3_LET_CONNECTION_COMPLETE, |
| 1461 |
.error_code = AWS_ERROR_MQTT_PROTOCOL_ERROR, |
| 1462 |
}, |
| 1463 |
{ |
| 1464 |
.type = AWS_MQTT3_LET_CONNECTION_SUCCESS, |
| 1465 |
}, |
| 1466 |
{ |
| 1467 |
.type = AWS_MQTT3_LET_CONNECTION_COMPLETE, |
| 1468 |
}, |
| 1469 |
}; |
| 1470 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_lifecycle_sequence( |
| 1471 |
&fixture, |
| 1472 |
AWS_ARRAY_SIZE(expected_reconnect_events), |
| 1473 |
expected_reconnect_events, |
| 1474 |
AWS_ARRAY_SIZE(expected_reconnect_events))); |
| 1475 |
|
| 1476 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 1477 |
aws_mqtt_library_clean_up(); |
| 1478 |
|
| 1479 |
return AWS_OP_SUCCESS; |
| 1480 |
} |
| 1481 |
|
| 1482 |
AWS_TEST_CASE( |
| 1483 |
mqtt5to3_adapter_connect_failure_connect_success_via_mqtt5, |
| 1484 |
s_mqtt5to3_adapter_connect_failure_connect_success_via_mqtt5_fn) |
| 1485 |
|
| 1486 |
AWS_STATIC_STRING_FROM_LITERAL(s_bad_host_name, "derpity_derp"); |
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
static int s_mqtt5to3_adapter_connect_failure_bad_config_success_good_config_fn( |
| 1493 |
struct aws_allocator *allocator, |
| 1494 |
void *ctx) { |
| 1495 |
(void)ctx; |
| 1496 |
|
| 1497 |
aws_mqtt_library_init(allocator); |
| 1498 |
|
| 1499 |
struct mqtt5_client_test_options test_options; |
| 1500 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 1501 |
|
| 1502 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 1503 |
.client_options = &test_options.client_options, |
| 1504 |
.server_function_table = &test_options.server_function_table, |
| 1505 |
}; |
| 1506 |
|
| 1507 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 1508 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 1509 |
|
| 1510 |
struct aws_mqtt_client_connection *adapter = fixture.connection; |
| 1511 |
|
| 1512 |
struct aws_mqtt_connection_options connection_options; |
| 1513 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 1514 |
struct aws_byte_cursor good_host_name = connection_options.host_name; |
| 1515 |
connection_options.host_name = aws_byte_cursor_from_string(s_bad_host_name); |
| 1516 |
|
| 1517 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 1518 |
connection_options.user_data = &fixture; |
| 1519 |
|
| 1520 |
aws_mqtt_client_connection_connect(adapter, &connection_options); |
| 1521 |
|
| 1522 |
|
| 1523 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 1524 |
|
| 1525 |
struct aws_mqtt3_lifecycle_event expected_events[] = { |
| 1526 |
{ |
| 1527 |
.type = AWS_MQTT3_LET_CONNECTION_FAILURE, |
| 1528 |
.error_code = AWS_ERROR_FILE_INVALID_PATH, |
| 1529 |
.skip_error_code_equality = true, |
| 1530 |
}, |
| 1531 |
{ |
| 1532 |
.type = AWS_MQTT3_LET_CONNECTION_COMPLETE, |
| 1533 |
.error_code = AWS_ERROR_FILE_INVALID_PATH, |
| 1534 |
.skip_error_code_equality = true, |
| 1535 |
}, |
| 1536 |
}; |
| 1537 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_lifecycle_sequence( |
| 1538 |
&fixture, AWS_ARRAY_SIZE(expected_events), expected_events, AWS_ARRAY_SIZE(expected_events))); |
| 1539 |
|
| 1540 |
|
| 1541 |
connection_options.host_name = good_host_name; |
| 1542 |
aws_mqtt_client_connection_connect(adapter, &connection_options); |
| 1543 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 2); |
| 1544 |
|
| 1545 |
struct aws_mqtt3_lifecycle_event expected_reconnect_events[] = { |
| 1546 |
{ |
| 1547 |
.type = AWS_MQTT3_LET_CONNECTION_FAILURE, |
| 1548 |
.error_code = AWS_ERROR_FILE_INVALID_PATH, |
| 1549 |
.skip_error_code_equality = true, |
| 1550 |
}, |
| 1551 |
{ |
| 1552 |
.type = AWS_MQTT3_LET_CONNECTION_COMPLETE, |
| 1553 |
.error_code = AWS_ERROR_FILE_INVALID_PATH, |
| 1554 |
.skip_error_code_equality = true, |
| 1555 |
}, |
| 1556 |
{ |
| 1557 |
.type = AWS_MQTT3_LET_CONNECTION_SUCCESS, |
| 1558 |
}, |
| 1559 |
{ |
| 1560 |
.type = AWS_MQTT3_LET_CONNECTION_COMPLETE, |
| 1561 |
}, |
| 1562 |
}; |
| 1563 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_lifecycle_sequence( |
| 1564 |
&fixture, |
| 1565 |
AWS_ARRAY_SIZE(expected_reconnect_events), |
| 1566 |
expected_reconnect_events, |
| 1567 |
AWS_ARRAY_SIZE(expected_reconnect_events))); |
| 1568 |
|
| 1569 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 1570 |
aws_mqtt_library_clean_up(); |
| 1571 |
|
| 1572 |
return AWS_OP_SUCCESS; |
| 1573 |
} |
| 1574 |
|
| 1575 |
AWS_TEST_CASE( |
| 1576 |
mqtt5to3_adapter_connect_failure_bad_config_success_good_config, |
| 1577 |
s_mqtt5to3_adapter_connect_failure_bad_config_success_good_config_fn) |
| 1578 |
|
| 1579 |
int aws_mqtt5_mock_server_handle_connect_fail_on_or_after_nth( |
| 1580 |
void *packet, |
| 1581 |
struct aws_mqtt5_server_mock_connection_context *connection, |
| 1582 |
void *user_data) { |
| 1583 |
(void)packet; |
| 1584 |
|
| 1585 |
struct aws_mqtt5_mock_server_reconnect_state *context = user_data; |
| 1586 |
|
| 1587 |
bool send_disconnect = false; |
| 1588 |
struct aws_mqtt5_packet_connack_view connack_view; |
| 1589 |
AWS_ZERO_STRUCT(connack_view); |
| 1590 |
|
| 1591 |
if (context->connection_attempts >= context->required_connection_count_threshold) { |
| 1592 |
connack_view.reason_code = AWS_MQTT5_CRC_NOT_AUTHORIZED; |
| 1593 |
} else { |
| 1594 |
connack_view.reason_code = AWS_MQTT5_CRC_SUCCESS; |
| 1595 |
aws_high_res_clock_get_ticks(&context->connect_timestamp); |
| 1596 |
send_disconnect = true; |
| 1597 |
} |
| 1598 |
|
| 1599 |
++context->connection_attempts; |
| 1600 |
|
| 1601 |
aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_CONNACK, &connack_view); |
| 1602 |
|
| 1603 |
if (send_disconnect) { |
| 1604 |
struct aws_mqtt5_packet_disconnect_view disconnect = { |
| 1605 |
.reason_code = AWS_MQTT5_DRC_SERVER_SHUTTING_DOWN, |
| 1606 |
}; |
| 1607 |
|
| 1608 |
aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_DISCONNECT, &disconnect); |
| 1609 |
} |
| 1610 |
|
| 1611 |
return AWS_OP_SUCCESS; |
| 1612 |
} |
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
static int s_mqtt5to3_adapter_connect_reconnect_failures_fn(struct aws_allocator *allocator, void *ctx) { |
| 1619 |
(void)ctx; |
| 1620 |
|
| 1621 |
aws_mqtt_library_init(allocator); |
| 1622 |
|
| 1623 |
struct aws_mqtt5_mock_server_reconnect_state mock_server_state = { |
| 1624 |
.required_connection_count_threshold = 1, |
| 1625 |
}; |
| 1626 |
|
| 1627 |
struct mqtt5_client_test_options test_options; |
| 1628 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 1629 |
|
| 1630 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] = |
| 1631 |
aws_mqtt5_mock_server_handle_connect_fail_on_or_after_nth; |
| 1632 |
|
| 1633 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 1634 |
.client_options = &test_options.client_options, |
| 1635 |
.server_function_table = &test_options.server_function_table, |
| 1636 |
.mock_server_user_data = &mock_server_state, |
| 1637 |
}; |
| 1638 |
|
| 1639 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 1640 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 1641 |
|
| 1642 |
struct aws_mqtt_client_connection *adapter = fixture.connection; |
| 1643 |
|
| 1644 |
struct aws_mqtt_connection_options connection_options; |
| 1645 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 1646 |
|
| 1647 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 1648 |
connection_options.user_data = &fixture; |
| 1649 |
|
| 1650 |
aws_mqtt_client_connection_connect(adapter, &connection_options); |
| 1651 |
|
| 1652 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 1653 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_FAILURE, 3); |
| 1654 |
|
| 1655 |
aws_mqtt_client_connection_disconnect(adapter, NULL, NULL); |
| 1656 |
|
| 1657 |
struct aws_mqtt3_lifecycle_event expected_events[] = { |
| 1658 |
{ |
| 1659 |
.type = AWS_MQTT3_LET_CONNECTION_SUCCESS, |
| 1660 |
}, |
| 1661 |
{ |
| 1662 |
.type = AWS_MQTT3_LET_CONNECTION_COMPLETE, |
| 1663 |
}, |
| 1664 |
{ |
| 1665 |
.type = AWS_MQTT3_LET_INTERRUPTED, |
| 1666 |
.error_code = AWS_ERROR_MQTT_UNEXPECTED_HANGUP, |
| 1667 |
}, |
| 1668 |
{ |
| 1669 |
.type = AWS_MQTT3_LET_CONNECTION_FAILURE, |
| 1670 |
.error_code = AWS_ERROR_MQTT_PROTOCOL_ERROR, |
| 1671 |
}, |
| 1672 |
{ |
| 1673 |
.type = AWS_MQTT3_LET_CONNECTION_FAILURE, |
| 1674 |
.error_code = AWS_ERROR_MQTT_PROTOCOL_ERROR, |
| 1675 |
}, |
| 1676 |
{ |
| 1677 |
.type = AWS_MQTT3_LET_CONNECTION_FAILURE, |
| 1678 |
.error_code = AWS_ERROR_MQTT_PROTOCOL_ERROR, |
| 1679 |
}, |
| 1680 |
}; |
| 1681 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_lifecycle_sequence_starts_with( |
| 1682 |
&fixture, AWS_ARRAY_SIZE(expected_events), expected_events)); |
| 1683 |
|
| 1684 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 1685 |
aws_mqtt_library_clean_up(); |
| 1686 |
|
| 1687 |
return AWS_OP_SUCCESS; |
| 1688 |
} |
| 1689 |
|
| 1690 |
AWS_TEST_CASE(mqtt5to3_adapter_connect_reconnect_failures, s_mqtt5to3_adapter_connect_reconnect_failures_fn) |
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
|
| 1695 |
|
| 1696 |
static int s_mqtt5to3_adapter_connect_success_disconnect_connect_fn(struct aws_allocator *allocator, void *ctx) { |
| 1697 |
(void)ctx; |
| 1698 |
|
| 1699 |
aws_mqtt_library_init(allocator); |
| 1700 |
|
| 1701 |
struct mqtt5_client_test_options test_options; |
| 1702 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 1703 |
|
| 1704 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 1705 |
.client_options = &test_options.client_options, |
| 1706 |
.server_function_table = &test_options.server_function_table, |
| 1707 |
}; |
| 1708 |
|
| 1709 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 1710 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 1711 |
|
| 1712 |
struct aws_mqtt_client_connection *adapter = fixture.connection; |
| 1713 |
|
| 1714 |
struct aws_mqtt_connection_options connection_options; |
| 1715 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 1716 |
|
| 1717 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 1718 |
connection_options.user_data = &fixture; |
| 1719 |
|
| 1720 |
aws_mqtt_client_connection_connect(adapter, &connection_options); |
| 1721 |
|
| 1722 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 1723 |
|
| 1724 |
aws_mqtt_client_connection_disconnect( |
| 1725 |
adapter, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_disconnection_complete, &fixture); |
| 1726 |
|
| 1727 |
aws_mqtt_client_connection_connect(adapter, &connection_options); |
| 1728 |
|
| 1729 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_DISCONNECTION_COMPLETE, 1); |
| 1730 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 2); |
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
|
| 1735 |
|
| 1736 |
|
| 1737 |
struct aws_mqtt3_lifecycle_event expected_sequence_beginning[] = { |
| 1738 |
{ |
| 1739 |
.type = AWS_MQTT3_LET_CONNECTION_SUCCESS, |
| 1740 |
}, |
| 1741 |
{ |
| 1742 |
.type = AWS_MQTT3_LET_CONNECTION_COMPLETE, |
| 1743 |
}, |
| 1744 |
{ |
| 1745 |
.type = AWS_MQTT3_LET_DISCONNECTION_COMPLETE, |
| 1746 |
}, |
| 1747 |
}; |
| 1748 |
|
| 1749 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_lifecycle_sequence_starts_with( |
| 1750 |
&fixture, AWS_ARRAY_SIZE(expected_sequence_beginning), expected_sequence_beginning)); |
| 1751 |
|
| 1752 |
struct aws_mqtt3_lifecycle_event expected_sequence_ending[] = { |
| 1753 |
{ |
| 1754 |
.type = AWS_MQTT3_LET_CONNECTION_SUCCESS, |
| 1755 |
}, |
| 1756 |
{ |
| 1757 |
.type = AWS_MQTT3_LET_CONNECTION_COMPLETE, |
| 1758 |
}, |
| 1759 |
}; |
| 1760 |
|
| 1761 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_lifecycle_sequence_ends_with( |
| 1762 |
&fixture, AWS_ARRAY_SIZE(expected_sequence_ending), expected_sequence_ending)); |
| 1763 |
|
| 1764 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 1765 |
aws_mqtt_library_clean_up(); |
| 1766 |
|
| 1767 |
return AWS_OP_SUCCESS; |
| 1768 |
} |
| 1769 |
|
| 1770 |
AWS_TEST_CASE( |
| 1771 |
mqtt5to3_adapter_connect_success_disconnect_connect, |
| 1772 |
s_mqtt5to3_adapter_connect_success_disconnect_connect_fn) |
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
|
| 1777 |
|
| 1778 |
static int s_mqtt5to3_adapter_connect_success_stop_mqtt5_disconnect_success_fn( |
| 1779 |
struct aws_allocator *allocator, |
| 1780 |
void *ctx) { |
| 1781 |
(void)ctx; |
| 1782 |
|
| 1783 |
aws_mqtt_library_init(allocator); |
| 1784 |
|
| 1785 |
struct mqtt5_client_test_options test_options; |
| 1786 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 1787 |
|
| 1788 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 1789 |
.client_options = &test_options.client_options, |
| 1790 |
.server_function_table = &test_options.server_function_table, |
| 1791 |
}; |
| 1792 |
|
| 1793 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 1794 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 1795 |
|
| 1796 |
struct aws_mqtt_client_connection *adapter = fixture.connection; |
| 1797 |
|
| 1798 |
struct aws_mqtt_connection_options connection_options; |
| 1799 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 1800 |
|
| 1801 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 1802 |
connection_options.user_data = &fixture; |
| 1803 |
|
| 1804 |
aws_mqtt_client_connection_connect(adapter, &connection_options); |
| 1805 |
|
| 1806 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 1807 |
|
| 1808 |
aws_mqtt5_client_stop(fixture.mqtt5_fixture.client, NULL, NULL); |
| 1809 |
|
| 1810 |
aws_wait_for_stopped_lifecycle_event(&fixture.mqtt5_fixture); |
| 1811 |
|
| 1812 |
aws_mqtt_client_connection_disconnect( |
| 1813 |
adapter, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_disconnection_complete, &fixture); |
| 1814 |
|
| 1815 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_DISCONNECTION_COMPLETE, 1); |
| 1816 |
|
| 1817 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 1818 |
aws_mqtt_library_clean_up(); |
| 1819 |
|
| 1820 |
return AWS_OP_SUCCESS; |
| 1821 |
} |
| 1822 |
|
| 1823 |
AWS_TEST_CASE( |
| 1824 |
mqtt5to3_adapter_connect_success_stop_mqtt5_disconnect_success, |
| 1825 |
s_mqtt5to3_adapter_connect_success_stop_mqtt5_disconnect_success_fn) |
| 1826 |
|
| 1827 |
|
| 1828 |
|
| 1829 |
|
| 1830 |
static int s_mqtt5to3_adapter_disconnect_success_fn(struct aws_allocator *allocator, void *ctx) { |
| 1831 |
(void)ctx; |
| 1832 |
|
| 1833 |
aws_mqtt_library_init(allocator); |
| 1834 |
|
| 1835 |
struct mqtt5_client_test_options test_options; |
| 1836 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 1837 |
|
| 1838 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 1839 |
.client_options = &test_options.client_options, |
| 1840 |
.server_function_table = &test_options.server_function_table, |
| 1841 |
}; |
| 1842 |
|
| 1843 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 1844 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 1845 |
|
| 1846 |
struct aws_mqtt_client_connection *adapter = fixture.connection; |
| 1847 |
|
| 1848 |
aws_mqtt_client_connection_disconnect( |
| 1849 |
adapter, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_disconnection_complete, &fixture); |
| 1850 |
|
| 1851 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_DISCONNECTION_COMPLETE, 1); |
| 1852 |
|
| 1853 |
struct aws_mqtt3_lifecycle_event expected_events[] = { |
| 1854 |
{ |
| 1855 |
.type = AWS_MQTT3_LET_DISCONNECTION_COMPLETE, |
| 1856 |
}, |
| 1857 |
}; |
| 1858 |
|
| 1859 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_lifecycle_sequence( |
| 1860 |
&fixture, AWS_ARRAY_SIZE(expected_events), expected_events, AWS_ARRAY_SIZE(expected_events))); |
| 1861 |
|
| 1862 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 1863 |
aws_mqtt_library_clean_up(); |
| 1864 |
|
| 1865 |
return AWS_OP_SUCCESS; |
| 1866 |
} |
| 1867 |
|
| 1868 |
AWS_TEST_CASE(mqtt5to3_adapter_disconnect_success, s_mqtt5to3_adapter_disconnect_success_fn) |
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
|
| 1873 |
|
| 1874 |
static int s_mqtt5to3_adapter_connect_success_disconnect_success_disconnect_success_fn( |
| 1875 |
struct aws_allocator *allocator, |
| 1876 |
void *ctx) { |
| 1877 |
(void)ctx; |
| 1878 |
|
| 1879 |
aws_mqtt_library_init(allocator); |
| 1880 |
|
| 1881 |
struct mqtt5_client_test_options test_options; |
| 1882 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 1883 |
|
| 1884 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 1885 |
.client_options = &test_options.client_options, |
| 1886 |
.server_function_table = &test_options.server_function_table, |
| 1887 |
}; |
| 1888 |
|
| 1889 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 1890 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 1891 |
|
| 1892 |
struct aws_mqtt_client_connection *adapter = fixture.connection; |
| 1893 |
|
| 1894 |
struct aws_mqtt_connection_options connection_options; |
| 1895 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 1896 |
|
| 1897 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 1898 |
connection_options.user_data = &fixture; |
| 1899 |
|
| 1900 |
aws_mqtt_client_connection_connect(adapter, &connection_options); |
| 1901 |
|
| 1902 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 1903 |
|
| 1904 |
aws_mqtt5_client_stop(fixture.mqtt5_fixture.client, NULL, NULL); |
| 1905 |
|
| 1906 |
aws_wait_for_stopped_lifecycle_event(&fixture.mqtt5_fixture); |
| 1907 |
|
| 1908 |
aws_mqtt_client_connection_disconnect( |
| 1909 |
adapter, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_disconnection_complete, &fixture); |
| 1910 |
|
| 1911 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_DISCONNECTION_COMPLETE, 1); |
| 1912 |
|
| 1913 |
aws_mqtt_client_connection_disconnect( |
| 1914 |
adapter, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_disconnection_complete, &fixture); |
| 1915 |
|
| 1916 |
aws_mqtt_client_connection_disconnect( |
| 1917 |
adapter, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_disconnection_complete, &fixture); |
| 1918 |
|
| 1919 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_DISCONNECTION_COMPLETE, 3); |
| 1920 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CLOSED, 1); |
| 1921 |
|
| 1922 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 1923 |
aws_mqtt_library_clean_up(); |
| 1924 |
|
| 1925 |
return AWS_OP_SUCCESS; |
| 1926 |
} |
| 1927 |
|
| 1928 |
AWS_TEST_CASE( |
| 1929 |
mqtt5to3_adapter_connect_success_disconnect_success_disconnect_success, |
| 1930 |
s_mqtt5to3_adapter_connect_success_disconnect_success_disconnect_success_fn) |
| 1931 |
|
| 1932 |
#define SIMPLE_ALLOCATION_COUNT 10 |
| 1933 |
|
| 1934 |
static int s_mqtt5to3_adapter_operation_allocation_simple_fn(struct aws_allocator *allocator, void *ctx) { |
| 1935 |
(void)ctx; |
| 1936 |
|
| 1937 |
aws_mqtt_library_init(allocator); |
| 1938 |
struct mqtt5_client_test_options test_options; |
| 1939 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 1940 |
|
| 1941 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 1942 |
.client_options = &test_options.client_options, |
| 1943 |
.server_function_table = &test_options.server_function_table, |
| 1944 |
}; |
| 1945 |
|
| 1946 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 1947 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 1948 |
|
| 1949 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 1950 |
struct aws_mqtt_client_connection_5_impl *adapter = connection->impl; |
| 1951 |
struct aws_mqtt5_to_mqtt3_adapter_operation_table *operational_state = &adapter->operational_state; |
| 1952 |
|
| 1953 |
for (size_t i = 0; i < SIMPLE_ALLOCATION_COUNT; ++i) { |
| 1954 |
struct aws_mqtt5_to_mqtt3_adapter_publish_options publish_options = { |
| 1955 |
.adapter = adapter, |
| 1956 |
.topic = aws_byte_cursor_from_c_str("derp"), |
| 1957 |
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 1958 |
}; |
| 1959 |
|
| 1960 |
struct aws_mqtt5_to_mqtt3_adapter_operation_publish *publish = |
| 1961 |
aws_mqtt5_to_mqtt3_adapter_operation_new_publish(allocator, &publish_options); |
| 1962 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_operation_table_add_operation(operational_state, &publish->base)); |
| 1963 |
|
| 1964 |
ASSERT_INT_EQUALS(i + 1, (size_t)(publish->base.id)); |
| 1965 |
} |
| 1966 |
|
| 1967 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 1968 |
|
| 1969 |
aws_mqtt_library_clean_up(); |
| 1970 |
|
| 1971 |
return AWS_OP_SUCCESS; |
| 1972 |
} |
| 1973 |
|
| 1974 |
AWS_TEST_CASE(mqtt5to3_adapter_operation_allocation_simple, s_mqtt5to3_adapter_operation_allocation_simple_fn) |
| 1975 |
|
| 1976 |
#define ALLOCATION_WRAP_AROUND_ID_START 100 |
| 1977 |
|
| 1978 |
static int s_mqtt5to3_adapter_operation_allocation_wraparound_fn(struct aws_allocator *allocator, void *ctx) { |
| 1979 |
(void)ctx; |
| 1980 |
|
| 1981 |
aws_mqtt_library_init(allocator); |
| 1982 |
struct mqtt5_client_test_options test_options; |
| 1983 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 1984 |
|
| 1985 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 1986 |
.client_options = &test_options.client_options, |
| 1987 |
.server_function_table = &test_options.server_function_table, |
| 1988 |
}; |
| 1989 |
|
| 1990 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 1991 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 1992 |
|
| 1993 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 1994 |
struct aws_mqtt_client_connection_5_impl *adapter = connection->impl; |
| 1995 |
struct aws_mqtt5_to_mqtt3_adapter_operation_table *operational_state = &adapter->operational_state; |
| 1996 |
|
| 1997 |
operational_state->next_id = ALLOCATION_WRAP_AROUND_ID_START; |
| 1998 |
|
| 1999 |
for (size_t i = 0; i < UINT16_MAX + 50; ++i) { |
| 2000 |
struct aws_mqtt5_to_mqtt3_adapter_publish_options publish_options = { |
| 2001 |
.adapter = adapter, |
| 2002 |
.topic = aws_byte_cursor_from_c_str("derp"), |
| 2003 |
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 2004 |
}; |
| 2005 |
|
| 2006 |
struct aws_mqtt5_to_mqtt3_adapter_operation_publish *publish = |
| 2007 |
aws_mqtt5_to_mqtt3_adapter_operation_new_publish(allocator, &publish_options); |
| 2008 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_operation_table_add_operation(operational_state, &publish->base)); |
| 2009 |
|
| 2010 |
size_t expected_id = (i + ALLOCATION_WRAP_AROUND_ID_START) % 65536; |
| 2011 |
if (i > UINT16_MAX - ALLOCATION_WRAP_AROUND_ID_START) { |
| 2012 |
++expected_id; |
| 2013 |
} |
| 2014 |
|
| 2015 |
ASSERT_INT_EQUALS(expected_id, (size_t)(publish->base.id)); |
| 2016 |
|
| 2017 |
aws_mqtt5_to_mqtt3_adapter_operation_table_remove_operation(operational_state, publish->base.id); |
| 2018 |
} |
| 2019 |
|
| 2020 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 2021 |
|
| 2022 |
aws_mqtt_library_clean_up(); |
| 2023 |
|
| 2024 |
return AWS_OP_SUCCESS; |
| 2025 |
} |
| 2026 |
|
| 2027 |
AWS_TEST_CASE(mqtt5to3_adapter_operation_allocation_wraparound, s_mqtt5to3_adapter_operation_allocation_wraparound_fn) |
| 2028 |
|
| 2029 |
static int s_mqtt5to3_adapter_operation_allocation_exhaustion_fn(struct aws_allocator *allocator, void *ctx) { |
| 2030 |
(void)ctx; |
| 2031 |
|
| 2032 |
aws_mqtt_library_init(allocator); |
| 2033 |
struct mqtt5_client_test_options test_options; |
| 2034 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 2035 |
|
| 2036 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 2037 |
.client_options = &test_options.client_options, |
| 2038 |
.server_function_table = &test_options.server_function_table, |
| 2039 |
}; |
| 2040 |
|
| 2041 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 2042 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 2043 |
|
| 2044 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 2045 |
struct aws_mqtt_client_connection_5_impl *adapter = connection->impl; |
| 2046 |
struct aws_mqtt5_to_mqtt3_adapter_operation_table *operational_state = &adapter->operational_state; |
| 2047 |
|
| 2048 |
operational_state->next_id = ALLOCATION_WRAP_AROUND_ID_START; |
| 2049 |
|
| 2050 |
for (size_t i = 0; i < UINT16_MAX + 50; ++i) { |
| 2051 |
struct aws_mqtt5_to_mqtt3_adapter_publish_options publish_options = { |
| 2052 |
.adapter = adapter, |
| 2053 |
.topic = aws_byte_cursor_from_c_str("derp"), |
| 2054 |
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 2055 |
}; |
| 2056 |
|
| 2057 |
struct aws_mqtt5_to_mqtt3_adapter_operation_publish *publish = |
| 2058 |
aws_mqtt5_to_mqtt3_adapter_operation_new_publish(allocator, &publish_options); |
| 2059 |
if (i >= UINT16_MAX) { |
| 2060 |
ASSERT_FAILS(aws_mqtt5_to_mqtt3_adapter_operation_table_add_operation(operational_state, &publish->base)); |
| 2061 |
aws_mqtt5_to_mqtt3_adapter_operation_release(&publish->base); |
| 2062 |
continue; |
| 2063 |
} |
| 2064 |
|
| 2065 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_operation_table_add_operation(operational_state, &publish->base)); |
| 2066 |
|
| 2067 |
size_t expected_id = (i + ALLOCATION_WRAP_AROUND_ID_START) % 65536; |
| 2068 |
if (i > UINT16_MAX - ALLOCATION_WRAP_AROUND_ID_START) { |
| 2069 |
++expected_id; |
| 2070 |
} |
| 2071 |
|
| 2072 |
ASSERT_INT_EQUALS(expected_id, (size_t)(publish->base.id)); |
| 2073 |
} |
| 2074 |
|
| 2075 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 2076 |
|
| 2077 |
aws_mqtt_library_clean_up(); |
| 2078 |
|
| 2079 |
return AWS_OP_SUCCESS; |
| 2080 |
} |
| 2081 |
|
| 2082 |
AWS_TEST_CASE(mqtt5to3_adapter_operation_allocation_exhaustion, s_mqtt5to3_adapter_operation_allocation_exhaustion_fn) |
| 2083 |
|
| 2084 |
static int s_aws_mqtt5_mock_server_handle_connect_succeed_with_small_payload( |
| 2085 |
void *packet, |
| 2086 |
struct aws_mqtt5_server_mock_connection_context *connection, |
| 2087 |
void *user_data) { |
| 2088 |
(void)packet; |
| 2089 |
(void)user_data; |
| 2090 |
|
| 2091 |
struct aws_mqtt5_packet_connack_view connack_view; |
| 2092 |
AWS_ZERO_STRUCT(connack_view); |
| 2093 |
|
| 2094 |
uint32_t maximum_packet_size = 1024; |
| 2095 |
|
| 2096 |
connack_view.reason_code = AWS_MQTT5_CRC_SUCCESS; |
| 2097 |
connack_view.maximum_packet_size = &maximum_packet_size; |
| 2098 |
|
| 2099 |
return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_CONNACK, &connack_view); |
| 2100 |
} |
| 2101 |
|
| 2102 |
static void s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_publish_complete( |
| 2103 |
struct aws_mqtt_client_connection *connection, |
| 2104 |
uint16_t packet_id, |
| 2105 |
int error_code, |
| 2106 |
void *userdata) { |
| 2107 |
|
| 2108 |
(void)connection; |
| 2109 |
(void)packet_id; |
| 2110 |
|
| 2111 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture = userdata; |
| 2112 |
|
| 2113 |
struct aws_mqtt3_operation_event operation_event = { |
| 2114 |
.type = AWS_MQTT3_OET_PUBLISH_COMPLETE, |
| 2115 |
.error_code = error_code, |
| 2116 |
}; |
| 2117 |
|
| 2118 |
aws_mutex_lock(&fixture->lock); |
| 2119 |
aws_array_list_push_back(&fixture->operation_events, &operation_event); |
| 2120 |
aws_mutex_unlock(&fixture->lock); |
| 2121 |
aws_condition_variable_notify_all(&fixture->signal); |
| 2122 |
} |
| 2123 |
|
| 2124 |
static int s_mqtt5to3_adapter_publish_failure_invalid_fn(struct aws_allocator *allocator, void *ctx) { |
| 2125 |
(void)ctx; |
| 2126 |
|
| 2127 |
aws_mqtt_library_init(allocator); |
| 2128 |
|
| 2129 |
struct mqtt5_client_test_options test_options; |
| 2130 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 2131 |
|
| 2132 |
|
| 2133 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] = |
| 2134 |
s_aws_mqtt5_mock_server_handle_connect_succeed_with_small_payload; |
| 2135 |
|
| 2136 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 2137 |
.client_options = &test_options.client_options, |
| 2138 |
.server_function_table = &test_options.server_function_table, |
| 2139 |
}; |
| 2140 |
|
| 2141 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 2142 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 2143 |
|
| 2144 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 2145 |
|
| 2146 |
struct aws_mqtt_connection_options connection_options; |
| 2147 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 2148 |
|
| 2149 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 2150 |
connection_options.user_data = &fixture; |
| 2151 |
|
| 2152 |
aws_mqtt_client_connection_connect(connection, &connection_options); |
| 2153 |
|
| 2154 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 2155 |
|
| 2156 |
struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("derp"); |
| 2157 |
|
| 2158 |
uint8_t payload_array[2 * 1024]; |
| 2159 |
struct aws_byte_cursor payload = aws_byte_cursor_from_array(payload_array, AWS_ARRAY_SIZE(payload_array)); |
| 2160 |
|
| 2161 |
aws_mqtt_client_connection_publish( |
| 2162 |
connection, |
| 2163 |
&topic, |
| 2164 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 2165 |
false, |
| 2166 |
&payload, |
| 2167 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_publish_complete, |
| 2168 |
&fixture); |
| 2169 |
|
| 2170 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_PUBLISH_COMPLETE, 1); |
| 2171 |
|
| 2172 |
struct aws_mqtt3_operation_event expected_events[] = {{ |
| 2173 |
.type = AWS_MQTT3_OET_PUBLISH_COMPLETE, |
| 2174 |
.error_code = AWS_ERROR_MQTT5_PACKET_VALIDATION, |
| 2175 |
}}; |
| 2176 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_operation_sequence( |
| 2177 |
&fixture, AWS_ARRAY_SIZE(expected_events), expected_events, AWS_ARRAY_SIZE(expected_events)); |
| 2178 |
|
| 2179 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 2180 |
aws_mqtt_library_clean_up(); |
| 2181 |
|
| 2182 |
return AWS_OP_SUCCESS; |
| 2183 |
} |
| 2184 |
|
| 2185 |
AWS_TEST_CASE(mqtt5to3_adapter_publish_failure_invalid, s_mqtt5to3_adapter_publish_failure_invalid_fn) |
| 2186 |
|
| 2187 |
static int s_mqtt5to3_adapter_publish_failure_offline_queue_policy_fn(struct aws_allocator *allocator, void *ctx) { |
| 2188 |
(void)ctx; |
| 2189 |
|
| 2190 |
aws_mqtt_library_init(allocator); |
| 2191 |
|
| 2192 |
struct mqtt5_client_test_options test_options; |
| 2193 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 2194 |
|
| 2195 |
test_options.client_options.offline_queue_behavior = AWS_MQTT5_COQBT_FAIL_QOS0_PUBLISH_ON_DISCONNECT; |
| 2196 |
|
| 2197 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 2198 |
.client_options = &test_options.client_options, |
| 2199 |
.server_function_table = &test_options.server_function_table, |
| 2200 |
}; |
| 2201 |
|
| 2202 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 2203 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 2204 |
|
| 2205 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 2206 |
|
| 2207 |
struct aws_mqtt_connection_options connection_options; |
| 2208 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 2209 |
|
| 2210 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 2211 |
connection_options.user_data = &fixture; |
| 2212 |
|
| 2213 |
aws_mqtt_client_connection_connect(connection, &connection_options); |
| 2214 |
|
| 2215 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 2216 |
|
| 2217 |
aws_mqtt_client_connection_disconnect( |
| 2218 |
connection, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_disconnection_complete, &fixture); |
| 2219 |
|
| 2220 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_DISCONNECTION_COMPLETE, 1); |
| 2221 |
|
| 2222 |
struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("derp"); |
| 2223 |
|
| 2224 |
aws_mqtt_client_connection_publish( |
| 2225 |
connection, |
| 2226 |
&topic, |
| 2227 |
AWS_MQTT_QOS_AT_MOST_ONCE, |
| 2228 |
false, |
| 2229 |
NULL, |
| 2230 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_publish_complete, |
| 2231 |
&fixture); |
| 2232 |
|
| 2233 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_PUBLISH_COMPLETE, 1); |
| 2234 |
|
| 2235 |
struct aws_mqtt3_operation_event expected_events[] = {{ |
| 2236 |
.type = AWS_MQTT3_OET_PUBLISH_COMPLETE, |
| 2237 |
.error_code = AWS_ERROR_MQTT5_OPERATION_FAILED_DUE_TO_OFFLINE_QUEUE_POLICY, |
| 2238 |
}}; |
| 2239 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_operation_sequence( |
| 2240 |
&fixture, AWS_ARRAY_SIZE(expected_events), expected_events, AWS_ARRAY_SIZE(expected_events)); |
| 2241 |
|
| 2242 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 2243 |
aws_mqtt_library_clean_up(); |
| 2244 |
|
| 2245 |
return AWS_OP_SUCCESS; |
| 2246 |
} |
| 2247 |
|
| 2248 |
AWS_TEST_CASE( |
| 2249 |
mqtt5to3_adapter_publish_failure_offline_queue_policy, |
| 2250 |
s_mqtt5to3_adapter_publish_failure_offline_queue_policy_fn) |
| 2251 |
|
| 2252 |
static int s_mqtt5to3_adapter_publish_success_qos0_fn(struct aws_allocator *allocator, void *ctx) { |
| 2253 |
(void)ctx; |
| 2254 |
|
| 2255 |
aws_mqtt_library_init(allocator); |
| 2256 |
|
| 2257 |
struct mqtt5_client_test_options test_options; |
| 2258 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 2259 |
|
| 2260 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 2261 |
.client_options = &test_options.client_options, |
| 2262 |
.server_function_table = &test_options.server_function_table, |
| 2263 |
}; |
| 2264 |
|
| 2265 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 2266 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 2267 |
|
| 2268 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 2269 |
|
| 2270 |
struct aws_mqtt_connection_options connection_options; |
| 2271 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 2272 |
|
| 2273 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 2274 |
connection_options.user_data = &fixture; |
| 2275 |
|
| 2276 |
aws_mqtt_client_connection_connect(connection, &connection_options); |
| 2277 |
|
| 2278 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 2279 |
|
| 2280 |
struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("derp"); |
| 2281 |
|
| 2282 |
aws_mqtt_client_connection_publish( |
| 2283 |
connection, |
| 2284 |
&topic, |
| 2285 |
AWS_MQTT_QOS_AT_MOST_ONCE, |
| 2286 |
false, |
| 2287 |
NULL, |
| 2288 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_publish_complete, |
| 2289 |
&fixture); |
| 2290 |
|
| 2291 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_PUBLISH_COMPLETE, 1); |
| 2292 |
|
| 2293 |
struct aws_mqtt3_operation_event expected_events[] = {{ |
| 2294 |
.type = AWS_MQTT3_OET_PUBLISH_COMPLETE, |
| 2295 |
.error_code = AWS_ERROR_SUCCESS, |
| 2296 |
}}; |
| 2297 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_operation_sequence( |
| 2298 |
&fixture, AWS_ARRAY_SIZE(expected_events), expected_events, AWS_ARRAY_SIZE(expected_events)); |
| 2299 |
|
| 2300 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 2301 |
aws_mqtt_library_clean_up(); |
| 2302 |
|
| 2303 |
return AWS_OP_SUCCESS; |
| 2304 |
} |
| 2305 |
|
| 2306 |
AWS_TEST_CASE(mqtt5to3_adapter_publish_success_qos0, s_mqtt5to3_adapter_publish_success_qos0_fn) |
| 2307 |
|
| 2308 |
static int s_mqtt5to3_adapter_publish_success_qos1_fn(struct aws_allocator *allocator, void *ctx) { |
| 2309 |
(void)ctx; |
| 2310 |
|
| 2311 |
aws_mqtt_library_init(allocator); |
| 2312 |
|
| 2313 |
struct mqtt5_client_test_options test_options; |
| 2314 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 2315 |
|
| 2316 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] = |
| 2317 |
aws_mqtt5_mock_server_handle_publish_puback; |
| 2318 |
|
| 2319 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 2320 |
.client_options = &test_options.client_options, |
| 2321 |
.server_function_table = &test_options.server_function_table, |
| 2322 |
}; |
| 2323 |
|
| 2324 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 2325 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 2326 |
|
| 2327 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 2328 |
|
| 2329 |
struct aws_mqtt_connection_options connection_options; |
| 2330 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 2331 |
|
| 2332 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 2333 |
connection_options.user_data = &fixture; |
| 2334 |
|
| 2335 |
aws_mqtt_client_connection_connect(connection, &connection_options); |
| 2336 |
|
| 2337 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 2338 |
|
| 2339 |
struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("derp"); |
| 2340 |
|
| 2341 |
aws_mqtt_client_connection_publish( |
| 2342 |
connection, |
| 2343 |
&topic, |
| 2344 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 2345 |
false, |
| 2346 |
NULL, |
| 2347 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_publish_complete, |
| 2348 |
&fixture); |
| 2349 |
|
| 2350 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_PUBLISH_COMPLETE, 1); |
| 2351 |
|
| 2352 |
struct aws_mqtt3_operation_event expected_events[] = {{ |
| 2353 |
.type = AWS_MQTT3_OET_PUBLISH_COMPLETE, |
| 2354 |
.error_code = AWS_ERROR_SUCCESS, |
| 2355 |
}}; |
| 2356 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_operation_sequence( |
| 2357 |
&fixture, AWS_ARRAY_SIZE(expected_events), expected_events, AWS_ARRAY_SIZE(expected_events)); |
| 2358 |
|
| 2359 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 2360 |
aws_mqtt_library_clean_up(); |
| 2361 |
|
| 2362 |
return AWS_OP_SUCCESS; |
| 2363 |
} |
| 2364 |
|
| 2365 |
AWS_TEST_CASE(mqtt5to3_adapter_publish_success_qos1, s_mqtt5to3_adapter_publish_success_qos1_fn) |
| 2366 |
|
| 2367 |
int aws_mqtt5_mock_server_handle_not_authorized_publish_puback( |
| 2368 |
void *packet, |
| 2369 |
struct aws_mqtt5_server_mock_connection_context *connection, |
| 2370 |
void *user_data) { |
| 2371 |
|
| 2372 |
(void)user_data; |
| 2373 |
|
| 2374 |
struct aws_mqtt5_packet_publish_view *publish_view = packet; |
| 2375 |
if (publish_view->qos != AWS_MQTT5_QOS_AT_LEAST_ONCE) { |
| 2376 |
return AWS_OP_SUCCESS; |
| 2377 |
} |
| 2378 |
|
| 2379 |
struct aws_mqtt5_packet_puback_view puback_view = { |
| 2380 |
.packet_id = publish_view->packet_id, |
| 2381 |
.reason_code = AWS_MQTT5_PARC_NOT_AUTHORIZED, |
| 2382 |
}; |
| 2383 |
|
| 2384 |
return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_PUBACK, &puback_view); |
| 2385 |
} |
| 2386 |
|
| 2387 |
static int s_mqtt5to3_adapter_publish_qos1_fail_ack_fn(struct aws_allocator *allocator, void *ctx) { |
| 2388 |
(void)ctx; |
| 2389 |
|
| 2390 |
aws_mqtt_library_init(allocator); |
| 2391 |
|
| 2392 |
struct mqtt5_client_test_options test_options; |
| 2393 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 2394 |
|
| 2395 |
|
| 2396 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] = |
| 2397 |
aws_mqtt5_mock_server_handle_not_authorized_publish_puback; |
| 2398 |
|
| 2399 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 2400 |
.client_options = &test_options.client_options, |
| 2401 |
.server_function_table = &test_options.server_function_table, |
| 2402 |
}; |
| 2403 |
|
| 2404 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 2405 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 2406 |
|
| 2407 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 2408 |
|
| 2409 |
struct aws_mqtt_connection_options connection_options; |
| 2410 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 2411 |
|
| 2412 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 2413 |
connection_options.user_data = &fixture; |
| 2414 |
|
| 2415 |
aws_mqtt_client_connection_connect(connection, &connection_options); |
| 2416 |
|
| 2417 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 2418 |
|
| 2419 |
struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("derp"); |
| 2420 |
|
| 2421 |
aws_mqtt_client_connection_publish( |
| 2422 |
connection, |
| 2423 |
&topic, |
| 2424 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 2425 |
false, |
| 2426 |
NULL, |
| 2427 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_publish_complete, |
| 2428 |
&fixture); |
| 2429 |
|
| 2430 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_PUBLISH_COMPLETE, 1); |
| 2431 |
|
| 2432 |
struct aws_mqtt3_operation_event expected_events[] = {{ |
| 2433 |
.type = AWS_MQTT3_OET_PUBLISH_COMPLETE, |
| 2434 |
.error_code = AWS_ERROR_MQTT_ACK_REASON_CODE_FAILURE, |
| 2435 |
}}; |
| 2436 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_operation_sequence( |
| 2437 |
&fixture, AWS_ARRAY_SIZE(expected_events), expected_events, AWS_ARRAY_SIZE(expected_events)); |
| 2438 |
|
| 2439 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 2440 |
aws_mqtt_library_clean_up(); |
| 2441 |
|
| 2442 |
return AWS_OP_SUCCESS; |
| 2443 |
} |
| 2444 |
|
| 2445 |
AWS_TEST_CASE(mqtt5to3_adapter_publish_qos1_fail_ack, s_mqtt5to3_adapter_publish_qos1_fail_ack_fn) |
| 2446 |
|
| 2447 |
static int s_mqtt5to3_adapter_publish_no_ack_fn(struct aws_allocator *allocator, void *ctx) { |
| 2448 |
(void)ctx; |
| 2449 |
|
| 2450 |
aws_mqtt_library_init(allocator); |
| 2451 |
|
| 2452 |
struct mqtt5_client_test_options test_options; |
| 2453 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 2454 |
|
| 2455 |
|
| 2456 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] = NULL; |
| 2457 |
|
| 2458 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 2459 |
.client_options = &test_options.client_options, |
| 2460 |
.server_function_table = &test_options.server_function_table, |
| 2461 |
}; |
| 2462 |
|
| 2463 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 2464 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 2465 |
|
| 2466 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 2467 |
|
| 2468 |
struct aws_mqtt_connection_options connection_options; |
| 2469 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 2470 |
|
| 2471 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 2472 |
connection_options.user_data = &fixture; |
| 2473 |
connection_options.protocol_operation_timeout_ms = 5000; |
| 2474 |
|
| 2475 |
aws_mqtt_client_connection_connect(connection, &connection_options); |
| 2476 |
|
| 2477 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 2478 |
|
| 2479 |
struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("derp"); |
| 2480 |
|
| 2481 |
aws_mqtt_client_connection_publish( |
| 2482 |
connection, |
| 2483 |
&topic, |
| 2484 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 2485 |
false, |
| 2486 |
NULL, |
| 2487 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_publish_complete, |
| 2488 |
&fixture); |
| 2489 |
|
| 2490 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_PUBLISH_COMPLETE, 1); |
| 2491 |
|
| 2492 |
struct aws_mqtt3_operation_event expected_events[] = {{ |
| 2493 |
.type = AWS_MQTT3_OET_PUBLISH_COMPLETE, |
| 2494 |
.error_code = AWS_ERROR_MQTT_TIMEOUT, |
| 2495 |
}}; |
| 2496 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_operation_sequence( |
| 2497 |
&fixture, AWS_ARRAY_SIZE(expected_events), expected_events, AWS_ARRAY_SIZE(expected_events)); |
| 2498 |
|
| 2499 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 2500 |
aws_mqtt_library_clean_up(); |
| 2501 |
|
| 2502 |
return AWS_OP_SUCCESS; |
| 2503 |
} |
| 2504 |
|
| 2505 |
AWS_TEST_CASE(mqtt5to3_adapter_publish_no_ack, s_mqtt5to3_adapter_publish_no_ack_fn) |
| 2506 |
|
| 2507 |
static int s_mqtt5to3_adapter_publish_interrupted_fn(struct aws_allocator *allocator, void *ctx) { |
| 2508 |
(void)ctx; |
| 2509 |
|
| 2510 |
aws_mqtt_library_init(allocator); |
| 2511 |
|
| 2512 |
struct mqtt5_client_test_options test_options; |
| 2513 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 2514 |
|
| 2515 |
|
| 2516 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] = NULL; |
| 2517 |
|
| 2518 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 2519 |
.client_options = &test_options.client_options, |
| 2520 |
.server_function_table = &test_options.server_function_table, |
| 2521 |
}; |
| 2522 |
|
| 2523 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 2524 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 2525 |
|
| 2526 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 2527 |
|
| 2528 |
struct aws_mqtt_connection_options connection_options; |
| 2529 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 2530 |
|
| 2531 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 2532 |
connection_options.user_data = &fixture; |
| 2533 |
|
| 2534 |
aws_mqtt_client_connection_connect(connection, &connection_options); |
| 2535 |
|
| 2536 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 2537 |
|
| 2538 |
struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("derp"); |
| 2539 |
|
| 2540 |
aws_mqtt_client_connection_publish( |
| 2541 |
connection, |
| 2542 |
&topic, |
| 2543 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 2544 |
false, |
| 2545 |
NULL, |
| 2546 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_publish_complete, |
| 2547 |
&fixture); |
| 2548 |
|
| 2549 |
|
| 2550 |
|
| 2551 |
|
| 2552 |
|
| 2553 |
aws_thread_current_sleep(aws_timestamp_convert(2, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_MILLIS, NULL)); |
| 2554 |
|
| 2555 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 2556 |
aws_mqtt_library_clean_up(); |
| 2557 |
|
| 2558 |
return AWS_OP_SUCCESS; |
| 2559 |
} |
| 2560 |
|
| 2561 |
AWS_TEST_CASE(mqtt5to3_adapter_publish_interrupted, s_mqtt5to3_adapter_publish_interrupted_fn) |
| 2562 |
|
| 2563 |
void s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_subscribe_complete( |
| 2564 |
struct aws_mqtt_client_connection *connection, |
| 2565 |
uint16_t packet_id, |
| 2566 |
const struct aws_byte_cursor *topic, |
| 2567 |
enum aws_mqtt_qos qos, |
| 2568 |
int error_code, |
| 2569 |
void *userdata) { |
| 2570 |
|
| 2571 |
(void)connection; |
| 2572 |
(void)packet_id; |
| 2573 |
(void)topic; |
| 2574 |
|
| 2575 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture = userdata; |
| 2576 |
|
| 2577 |
struct aws_mqtt3_operation_event operation_event = { |
| 2578 |
.type = AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, |
| 2579 |
.error_code = error_code, |
| 2580 |
}; |
| 2581 |
|
| 2582 |
aws_array_list_init_dynamic( |
| 2583 |
&operation_event.granted_subscriptions, |
| 2584 |
fixture->mqtt5_fixture.allocator, |
| 2585 |
1, |
| 2586 |
sizeof(struct aws_mqtt_topic_subscription)); |
| 2587 |
|
| 2588 |
aws_byte_buf_init_copy_from_cursor(&operation_event.topic_storage, fixture->mqtt5_fixture.allocator, *topic); |
| 2589 |
|
| 2590 |
|
| 2591 |
|
| 2592 |
|
| 2593 |
|
| 2594 |
struct aws_mqtt_topic_subscription sub = { |
| 2595 |
.topic = aws_byte_cursor_from_buf(&operation_event.topic_storage), |
| 2596 |
.qos = qos, |
| 2597 |
}; |
| 2598 |
aws_array_list_push_back(&operation_event.granted_subscriptions, (void *)&sub); |
| 2599 |
|
| 2600 |
aws_mutex_lock(&fixture->lock); |
| 2601 |
aws_array_list_push_back(&fixture->operation_events, &operation_event); |
| 2602 |
aws_mutex_unlock(&fixture->lock); |
| 2603 |
aws_condition_variable_notify_all(&fixture->signal); |
| 2604 |
} |
| 2605 |
|
| 2606 |
static void s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_publish_received( |
| 2607 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture, |
| 2608 |
enum aws_mqtt3_operation_event_type event_type, |
| 2609 |
struct aws_byte_cursor topic, |
| 2610 |
struct aws_byte_cursor payload, |
| 2611 |
enum aws_mqtt_qos qos) { |
| 2612 |
|
| 2613 |
struct aws_mqtt3_operation_event operation_event = { |
| 2614 |
.type = event_type, |
| 2615 |
.qos = qos, |
| 2616 |
}; |
| 2617 |
|
| 2618 |
aws_byte_buf_init_copy_from_cursor(&operation_event.topic, fixture->mqtt5_fixture.allocator, topic); |
| 2619 |
operation_event.topic_cursor = aws_byte_cursor_from_buf(&operation_event.topic); |
| 2620 |
aws_byte_buf_init_copy_from_cursor(&operation_event.payload, fixture->mqtt5_fixture.allocator, payload); |
| 2621 |
operation_event.payload_cursor = aws_byte_cursor_from_buf(&operation_event.payload); |
| 2622 |
|
| 2623 |
aws_mutex_lock(&fixture->lock); |
| 2624 |
aws_array_list_push_back(&fixture->operation_events, &operation_event); |
| 2625 |
aws_mutex_unlock(&fixture->lock); |
| 2626 |
aws_condition_variable_notify_all(&fixture->signal); |
| 2627 |
} |
| 2628 |
|
| 2629 |
static void s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_on_any_publish( |
| 2630 |
struct aws_mqtt_client_connection *connection, |
| 2631 |
const struct aws_byte_cursor *topic, |
| 2632 |
const struct aws_byte_cursor *payload, |
| 2633 |
bool dup, |
| 2634 |
enum aws_mqtt_qos qos, |
| 2635 |
bool retain, |
| 2636 |
void *userdata) { |
| 2637 |
|
| 2638 |
(void)connection; |
| 2639 |
(void)dup; |
| 2640 |
(void)retain; |
| 2641 |
|
| 2642 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture = userdata; |
| 2643 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_publish_received( |
| 2644 |
fixture, AWS_MQTT3_OET_PUBLISH_RECEIVED_ANY, *topic, *payload, qos); |
| 2645 |
} |
| 2646 |
|
| 2647 |
void s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_topic_specific_publish( |
| 2648 |
struct aws_mqtt_client_connection *connection, |
| 2649 |
const struct aws_byte_cursor *topic, |
| 2650 |
const struct aws_byte_cursor *payload, |
| 2651 |
bool dup, |
| 2652 |
enum aws_mqtt_qos qos, |
| 2653 |
bool retain, |
| 2654 |
void *userdata) { |
| 2655 |
|
| 2656 |
(void)connection; |
| 2657 |
(void)dup; |
| 2658 |
(void)retain; |
| 2659 |
|
| 2660 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture = userdata; |
| 2661 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_publish_received( |
| 2662 |
fixture, AWS_MQTT3_OET_PUBLISH_RECEIVED_SUBSCRIBED, *topic, *payload, qos); |
| 2663 |
} |
| 2664 |
|
| 2665 |
static int s_mqtt5_mock_server_handle_subscribe_suback_success( |
| 2666 |
void *packet, |
| 2667 |
struct aws_mqtt5_server_mock_connection_context *connection, |
| 2668 |
void *user_data) { |
| 2669 |
|
| 2670 |
(void)user_data; |
| 2671 |
|
| 2672 |
struct aws_mqtt5_packet_subscribe_view *subscribe_view = packet; |
| 2673 |
|
| 2674 |
AWS_VARIABLE_LENGTH_ARRAY( |
| 2675 |
enum aws_mqtt5_suback_reason_code, mqtt5_suback_codes, subscribe_view->subscription_count); |
| 2676 |
for (size_t i = 0; i < subscribe_view->subscription_count; ++i) { |
| 2677 |
enum aws_mqtt5_suback_reason_code *reason_code_ptr = &mqtt5_suback_codes[i]; |
| 2678 |
*reason_code_ptr = (enum aws_mqtt5_suback_reason_code)subscribe_view->subscriptions[i].qos; |
| 2679 |
} |
| 2680 |
|
| 2681 |
struct aws_mqtt5_packet_suback_view suback_view = { |
| 2682 |
.packet_id = subscribe_view->packet_id, |
| 2683 |
.reason_code_count = subscribe_view->subscription_count, |
| 2684 |
.reason_codes = mqtt5_suback_codes, |
| 2685 |
}; |
| 2686 |
|
| 2687 |
return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_SUBACK, &suback_view); |
| 2688 |
} |
| 2689 |
|
| 2690 |
static int s_mqtt5to3_adapter_subscribe_single_success_fn(struct aws_allocator *allocator, void *ctx) { |
| 2691 |
(void)ctx; |
| 2692 |
|
| 2693 |
aws_mqtt_library_init(allocator); |
| 2694 |
|
| 2695 |
struct mqtt5_client_test_options test_options; |
| 2696 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 2697 |
|
| 2698 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] = |
| 2699 |
s_mqtt5_mock_server_handle_subscribe_suback_success; |
| 2700 |
|
| 2701 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 2702 |
.client_options = &test_options.client_options, |
| 2703 |
.server_function_table = &test_options.server_function_table, |
| 2704 |
}; |
| 2705 |
|
| 2706 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 2707 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 2708 |
|
| 2709 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 2710 |
|
| 2711 |
struct aws_mqtt_connection_options connection_options; |
| 2712 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 2713 |
|
| 2714 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 2715 |
connection_options.user_data = &fixture; |
| 2716 |
|
| 2717 |
aws_mqtt_client_connection_connect(connection, &connection_options); |
| 2718 |
|
| 2719 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 2720 |
|
| 2721 |
struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("derp"); |
| 2722 |
|
| 2723 |
aws_mqtt_client_connection_subscribe( |
| 2724 |
connection, |
| 2725 |
&topic, |
| 2726 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 2727 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_topic_specific_publish, |
| 2728 |
&fixture, |
| 2729 |
NULL, |
| 2730 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_subscribe_complete, |
| 2731 |
&fixture); |
| 2732 |
|
| 2733 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, 1); |
| 2734 |
|
| 2735 |
struct aws_mqtt_topic_subscription expected_subs[1] = { |
| 2736 |
{ |
| 2737 |
.topic = topic, |
| 2738 |
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 2739 |
}, |
| 2740 |
}; |
| 2741 |
|
| 2742 |
struct aws_mqtt3_operation_event expected_events[] = { |
| 2743 |
{ |
| 2744 |
.type = AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, |
| 2745 |
.error_code = AWS_ERROR_SUCCESS, |
| 2746 |
}, |
| 2747 |
}; |
| 2748 |
aws_array_list_init_static_from_initialized( |
| 2749 |
&expected_events[0].granted_subscriptions, |
| 2750 |
(void *)expected_subs, |
| 2751 |
1, |
| 2752 |
sizeof(struct aws_mqtt_topic_subscription)); |
| 2753 |
|
| 2754 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_operation_sequence( |
| 2755 |
&fixture, AWS_ARRAY_SIZE(expected_events), expected_events, AWS_ARRAY_SIZE(expected_events))); |
| 2756 |
|
| 2757 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 2758 |
aws_mqtt_library_clean_up(); |
| 2759 |
|
| 2760 |
return AWS_OP_SUCCESS; |
| 2761 |
} |
| 2762 |
|
| 2763 |
AWS_TEST_CASE(mqtt5to3_adapter_subscribe_single_success, s_mqtt5to3_adapter_subscribe_single_success_fn) |
| 2764 |
|
| 2765 |
|
| 2766 |
|
| 2767 |
|
| 2768 |
|
| 2769 |
|
| 2770 |
static int s_mqtt5to3_adapter_subscribe_single_null_suback_fn(struct aws_allocator *allocator, void *ctx) { |
| 2771 |
(void)ctx; |
| 2772 |
|
| 2773 |
aws_mqtt_library_init(allocator); |
| 2774 |
|
| 2775 |
struct mqtt5_client_test_options test_options; |
| 2776 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 2777 |
|
| 2778 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 2779 |
.client_options = &test_options.client_options, |
| 2780 |
.server_function_table = &test_options.server_function_table, |
| 2781 |
}; |
| 2782 |
|
| 2783 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 2784 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 2785 |
|
| 2786 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 2787 |
|
| 2788 |
struct aws_mqtt_connection_options connection_options; |
| 2789 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 2790 |
|
| 2791 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 2792 |
connection_options.user_data = &fixture; |
| 2793 |
|
| 2794 |
aws_mqtt_client_connection_connect(connection, &connection_options); |
| 2795 |
|
| 2796 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 2797 |
|
| 2798 |
struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("derp"); |
| 2799 |
|
| 2800 |
aws_mqtt_client_connection_subscribe( |
| 2801 |
connection, |
| 2802 |
&topic, |
| 2803 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 2804 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_topic_specific_publish, |
| 2805 |
&fixture, |
| 2806 |
NULL, |
| 2807 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_subscribe_complete, |
| 2808 |
&fixture); |
| 2809 |
|
| 2810 |
struct aws_mqtt_topic_subscription expected_subs[1] = { |
| 2811 |
{ |
| 2812 |
.topic = topic, |
| 2813 |
.qos = AWS_MQTT_QOS_FAILURE, |
| 2814 |
}, |
| 2815 |
}; |
| 2816 |
|
| 2817 |
struct aws_mqtt3_operation_event expected_events[] = { |
| 2818 |
{ |
| 2819 |
.type = AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, |
| 2820 |
.error_code = AWS_ERROR_MQTT5_USER_REQUESTED_STOP, |
| 2821 |
}, |
| 2822 |
}; |
| 2823 |
aws_array_list_init_static_from_initialized( |
| 2824 |
&expected_events[0].granted_subscriptions, |
| 2825 |
(void *)expected_subs, |
| 2826 |
1, |
| 2827 |
sizeof(struct aws_mqtt_topic_subscription)); |
| 2828 |
|
| 2829 |
aws_mqtt_client_connection_disconnect( |
| 2830 |
connection, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_disconnection_complete, &fixture); |
| 2831 |
|
| 2832 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_DISCONNECTION_COMPLETE, 1); |
| 2833 |
|
| 2834 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, 1); |
| 2835 |
|
| 2836 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_operation_sequence( |
| 2837 |
&fixture, AWS_ARRAY_SIZE(expected_events), expected_events, AWS_ARRAY_SIZE(expected_events))); |
| 2838 |
|
| 2839 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 2840 |
aws_mqtt_library_clean_up(); |
| 2841 |
|
| 2842 |
return AWS_OP_SUCCESS; |
| 2843 |
} |
| 2844 |
|
| 2845 |
AWS_TEST_CASE(mqtt5to3_adapter_subscribe_single_null_suback, s_mqtt5to3_adapter_subscribe_single_null_suback_fn) |
| 2846 |
|
| 2847 |
static void s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_subscribe_multi_complete( |
| 2848 |
struct aws_mqtt_client_connection *connection, |
| 2849 |
uint16_t packet_id, |
| 2850 |
const struct aws_array_list *topic_subacks, |
| 2851 |
int error_code, |
| 2852 |
void *userdata) { |
| 2853 |
|
| 2854 |
(void)connection; |
| 2855 |
(void)packet_id; |
| 2856 |
|
| 2857 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture = userdata; |
| 2858 |
|
| 2859 |
struct aws_mqtt3_operation_event operation_event = { |
| 2860 |
.type = AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, |
| 2861 |
.error_code = error_code, |
| 2862 |
}; |
| 2863 |
|
| 2864 |
if (error_code == AWS_ERROR_SUCCESS) { |
| 2865 |
size_t granted_count = aws_array_list_length(topic_subacks); |
| 2866 |
|
| 2867 |
aws_array_list_init_dynamic( |
| 2868 |
&operation_event.granted_subscriptions, |
| 2869 |
fixture->mqtt5_fixture.allocator, |
| 2870 |
granted_count, |
| 2871 |
sizeof(struct aws_mqtt_topic_subscription)); |
| 2872 |
|
| 2873 |
size_t topic_length = 0; |
| 2874 |
for (size_t i = 0; i < granted_count; ++i) { |
| 2875 |
struct aws_mqtt_topic_subscription *granted_sub = NULL; |
| 2876 |
aws_array_list_get_at(topic_subacks, &granted_sub, i); |
| 2877 |
|
| 2878 |
aws_array_list_push_back(&operation_event.granted_subscriptions, (void *)granted_sub); |
| 2879 |
topic_length += granted_sub->topic.len; |
| 2880 |
} |
| 2881 |
|
| 2882 |
aws_byte_buf_init(&operation_event.topic_storage, fixture->mqtt5_fixture.allocator, topic_length); |
| 2883 |
|
| 2884 |
for (size_t i = 0; i < granted_count; ++i) { |
| 2885 |
struct aws_mqtt_topic_subscription *granted_sub = NULL; |
| 2886 |
aws_array_list_get_at_ptr(&operation_event.granted_subscriptions, (void **)&granted_sub, i); |
| 2887 |
|
| 2888 |
aws_byte_buf_append_and_update(&operation_event.topic_storage, &granted_sub->topic); |
| 2889 |
} |
| 2890 |
} |
| 2891 |
|
| 2892 |
aws_mutex_lock(&fixture->lock); |
| 2893 |
aws_array_list_push_back(&fixture->operation_events, &operation_event); |
| 2894 |
aws_mutex_unlock(&fixture->lock); |
| 2895 |
aws_condition_variable_notify_all(&fixture->signal); |
| 2896 |
} |
| 2897 |
|
| 2898 |
static int s_mqtt5to3_adapter_subscribe_multi_success_fn(struct aws_allocator *allocator, void *ctx) { |
| 2899 |
(void)ctx; |
| 2900 |
|
| 2901 |
aws_mqtt_library_init(allocator); |
| 2902 |
|
| 2903 |
struct mqtt5_client_test_options test_options; |
| 2904 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 2905 |
|
| 2906 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] = |
| 2907 |
s_mqtt5_mock_server_handle_subscribe_suback_success; |
| 2908 |
|
| 2909 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 2910 |
.client_options = &test_options.client_options, |
| 2911 |
.server_function_table = &test_options.server_function_table, |
| 2912 |
}; |
| 2913 |
|
| 2914 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 2915 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 2916 |
|
| 2917 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 2918 |
|
| 2919 |
struct aws_mqtt_connection_options connection_options; |
| 2920 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 2921 |
|
| 2922 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 2923 |
connection_options.user_data = &fixture; |
| 2924 |
|
| 2925 |
aws_mqtt_client_connection_connect(connection, &connection_options); |
| 2926 |
|
| 2927 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 2928 |
|
| 2929 |
struct aws_mqtt_topic_subscription subscriptions[] = { |
| 2930 |
{ |
| 2931 |
.topic = aws_byte_cursor_from_c_str("topic/1"), |
| 2932 |
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 2933 |
}, |
| 2934 |
{ |
| 2935 |
.topic = aws_byte_cursor_from_c_str("topic/2"), |
| 2936 |
.qos = AWS_MQTT_QOS_AT_MOST_ONCE, |
| 2937 |
}, |
| 2938 |
}; |
| 2939 |
|
| 2940 |
struct aws_array_list subscription_list; |
| 2941 |
aws_array_list_init_static_from_initialized( |
| 2942 |
&subscription_list, subscriptions, 2, sizeof(struct aws_mqtt_topic_subscription)); |
| 2943 |
|
| 2944 |
aws_mqtt_client_connection_subscribe_multiple( |
| 2945 |
connection, |
| 2946 |
&subscription_list, |
| 2947 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_subscribe_multi_complete, |
| 2948 |
&fixture); |
| 2949 |
|
| 2950 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, 1); |
| 2951 |
|
| 2952 |
struct aws_mqtt3_operation_event expected_events[] = { |
| 2953 |
{ |
| 2954 |
.type = AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, |
| 2955 |
.error_code = AWS_ERROR_SUCCESS, |
| 2956 |
}, |
| 2957 |
}; |
| 2958 |
aws_array_list_init_static_from_initialized( |
| 2959 |
&expected_events[0].granted_subscriptions, |
| 2960 |
(void *)subscriptions, |
| 2961 |
2, |
| 2962 |
sizeof(struct aws_mqtt_topic_subscription)); |
| 2963 |
|
| 2964 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_operation_sequence( |
| 2965 |
&fixture, AWS_ARRAY_SIZE(expected_events), expected_events, AWS_ARRAY_SIZE(expected_events))); |
| 2966 |
|
| 2967 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 2968 |
aws_mqtt_library_clean_up(); |
| 2969 |
|
| 2970 |
return AWS_OP_SUCCESS; |
| 2971 |
} |
| 2972 |
|
| 2973 |
AWS_TEST_CASE(mqtt5to3_adapter_subscribe_multi_success, s_mqtt5to3_adapter_subscribe_multi_success_fn) |
| 2974 |
|
| 2975 |
|
| 2976 |
|
| 2977 |
|
| 2978 |
|
| 2979 |
|
| 2980 |
static int s_mqtt5to3_adapter_subscribe_multi_null_suback_fn(struct aws_allocator *allocator, void *ctx) { |
| 2981 |
(void)ctx; |
| 2982 |
|
| 2983 |
aws_mqtt_library_init(allocator); |
| 2984 |
|
| 2985 |
struct mqtt5_client_test_options test_options; |
| 2986 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 2987 |
|
| 2988 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 2989 |
.client_options = &test_options.client_options, |
| 2990 |
.server_function_table = &test_options.server_function_table, |
| 2991 |
}; |
| 2992 |
|
| 2993 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 2994 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 2995 |
|
| 2996 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 2997 |
|
| 2998 |
struct aws_mqtt_connection_options connection_options; |
| 2999 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 3000 |
|
| 3001 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 3002 |
connection_options.user_data = &fixture; |
| 3003 |
|
| 3004 |
aws_mqtt_client_connection_connect(connection, &connection_options); |
| 3005 |
|
| 3006 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 3007 |
|
| 3008 |
struct aws_mqtt_topic_subscription subscriptions[] = { |
| 3009 |
{ |
| 3010 |
.topic = aws_byte_cursor_from_c_str("topic/1"), |
| 3011 |
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3012 |
}, |
| 3013 |
{ |
| 3014 |
.topic = aws_byte_cursor_from_c_str("topic/2"), |
| 3015 |
.qos = AWS_MQTT_QOS_AT_MOST_ONCE, |
| 3016 |
}, |
| 3017 |
}; |
| 3018 |
|
| 3019 |
struct aws_array_list subscription_list; |
| 3020 |
aws_array_list_init_static_from_initialized( |
| 3021 |
&subscription_list, subscriptions, 2, sizeof(struct aws_mqtt_topic_subscription)); |
| 3022 |
|
| 3023 |
aws_mqtt_client_connection_subscribe_multiple( |
| 3024 |
connection, |
| 3025 |
&subscription_list, |
| 3026 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_subscribe_multi_complete, |
| 3027 |
&fixture); |
| 3028 |
|
| 3029 |
struct aws_mqtt3_operation_event expected_events[] = { |
| 3030 |
{ |
| 3031 |
.type = AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, |
| 3032 |
.error_code = AWS_ERROR_MQTT5_USER_REQUESTED_STOP, |
| 3033 |
}, |
| 3034 |
}; |
| 3035 |
|
| 3036 |
aws_mqtt_client_connection_disconnect( |
| 3037 |
connection, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_disconnection_complete, &fixture); |
| 3038 |
|
| 3039 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_DISCONNECTION_COMPLETE, 1); |
| 3040 |
|
| 3041 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, 1); |
| 3042 |
|
| 3043 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_operation_sequence( |
| 3044 |
&fixture, AWS_ARRAY_SIZE(expected_events), expected_events, AWS_ARRAY_SIZE(expected_events))); |
| 3045 |
|
| 3046 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 3047 |
aws_mqtt_library_clean_up(); |
| 3048 |
|
| 3049 |
return AWS_OP_SUCCESS; |
| 3050 |
} |
| 3051 |
|
| 3052 |
AWS_TEST_CASE(mqtt5to3_adapter_subscribe_multi_null_suback, s_mqtt5to3_adapter_subscribe_multi_null_suback_fn) |
| 3053 |
|
| 3054 |
static int s_mqtt5_mock_server_handle_subscribe_suback_failure( |
| 3055 |
void *packet, |
| 3056 |
struct aws_mqtt5_server_mock_connection_context *connection, |
| 3057 |
void *user_data) { |
| 3058 |
|
| 3059 |
(void)user_data; |
| 3060 |
|
| 3061 |
struct aws_mqtt5_packet_subscribe_view *subscribe_view = packet; |
| 3062 |
|
| 3063 |
AWS_VARIABLE_LENGTH_ARRAY( |
| 3064 |
enum aws_mqtt5_suback_reason_code, mqtt5_suback_codes, subscribe_view->subscription_count); |
| 3065 |
for (size_t i = 0; i < subscribe_view->subscription_count; ++i) { |
| 3066 |
enum aws_mqtt5_suback_reason_code *reason_code_ptr = &mqtt5_suback_codes[i]; |
| 3067 |
*reason_code_ptr = (i % 2) ? (enum aws_mqtt5_suback_reason_code)subscribe_view->subscriptions[i].qos |
| 3068 |
: AWS_MQTT5_SARC_QUOTA_EXCEEDED; |
| 3069 |
} |
| 3070 |
|
| 3071 |
struct aws_mqtt5_packet_suback_view suback_view = { |
| 3072 |
.packet_id = subscribe_view->packet_id, |
| 3073 |
.reason_code_count = subscribe_view->subscription_count, |
| 3074 |
.reason_codes = mqtt5_suback_codes, |
| 3075 |
}; |
| 3076 |
|
| 3077 |
return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_SUBACK, &suback_view); |
| 3078 |
} |
| 3079 |
|
| 3080 |
static int s_mqtt5to3_adapter_subscribe_single_failure_fn(struct aws_allocator *allocator, void *ctx) { |
| 3081 |
(void)ctx; |
| 3082 |
|
| 3083 |
aws_mqtt_library_init(allocator); |
| 3084 |
|
| 3085 |
struct mqtt5_client_test_options test_options; |
| 3086 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 3087 |
|
| 3088 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] = |
| 3089 |
s_mqtt5_mock_server_handle_subscribe_suback_failure; |
| 3090 |
|
| 3091 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 3092 |
.client_options = &test_options.client_options, |
| 3093 |
.server_function_table = &test_options.server_function_table, |
| 3094 |
}; |
| 3095 |
|
| 3096 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 3097 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 3098 |
|
| 3099 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 3100 |
|
| 3101 |
struct aws_mqtt_connection_options connection_options; |
| 3102 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 3103 |
|
| 3104 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 3105 |
connection_options.user_data = &fixture; |
| 3106 |
|
| 3107 |
aws_mqtt_client_connection_connect(connection, &connection_options); |
| 3108 |
|
| 3109 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 3110 |
|
| 3111 |
struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("derp"); |
| 3112 |
|
| 3113 |
aws_mqtt_client_connection_subscribe( |
| 3114 |
connection, |
| 3115 |
&topic, |
| 3116 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3117 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_topic_specific_publish, |
| 3118 |
&fixture, |
| 3119 |
NULL, |
| 3120 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_subscribe_complete, |
| 3121 |
&fixture); |
| 3122 |
|
| 3123 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, 1); |
| 3124 |
|
| 3125 |
struct aws_mqtt_topic_subscription expected_subs[1] = { |
| 3126 |
{ |
| 3127 |
.topic = topic, |
| 3128 |
.qos = AWS_MQTT_QOS_FAILURE, |
| 3129 |
}, |
| 3130 |
}; |
| 3131 |
|
| 3132 |
struct aws_mqtt3_operation_event expected_events[] = { |
| 3133 |
{ |
| 3134 |
.type = AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, |
| 3135 |
.error_code = AWS_ERROR_SUCCESS, |
| 3136 |
}, |
| 3137 |
}; |
| 3138 |
|
| 3139 |
aws_array_list_init_static_from_initialized( |
| 3140 |
&expected_events[0].granted_subscriptions, |
| 3141 |
(void *)expected_subs, |
| 3142 |
1, |
| 3143 |
sizeof(struct aws_mqtt_topic_subscription)); |
| 3144 |
|
| 3145 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_operation_sequence( |
| 3146 |
&fixture, AWS_ARRAY_SIZE(expected_events), expected_events, AWS_ARRAY_SIZE(expected_events))); |
| 3147 |
|
| 3148 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 3149 |
aws_mqtt_library_clean_up(); |
| 3150 |
|
| 3151 |
return AWS_OP_SUCCESS; |
| 3152 |
} |
| 3153 |
|
| 3154 |
AWS_TEST_CASE(mqtt5to3_adapter_subscribe_single_failure, s_mqtt5to3_adapter_subscribe_single_failure_fn) |
| 3155 |
|
| 3156 |
static int s_mqtt5to3_adapter_subscribe_single_invalid_fn(struct aws_allocator *allocator, void *ctx) { |
| 3157 |
(void)ctx; |
| 3158 |
|
| 3159 |
aws_mqtt_library_init(allocator); |
| 3160 |
|
| 3161 |
struct mqtt5_client_test_options test_options; |
| 3162 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 3163 |
|
| 3164 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 3165 |
.client_options = &test_options.client_options, |
| 3166 |
.server_function_table = &test_options.server_function_table, |
| 3167 |
}; |
| 3168 |
|
| 3169 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 3170 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 3171 |
|
| 3172 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 3173 |
|
| 3174 |
struct aws_mqtt_connection_options connection_options; |
| 3175 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 3176 |
|
| 3177 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 3178 |
connection_options.user_data = &fixture; |
| 3179 |
|
| 3180 |
aws_mqtt_client_connection_connect(connection, &connection_options); |
| 3181 |
|
| 3182 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 3183 |
|
| 3184 |
struct aws_byte_cursor bad_topic = aws_byte_cursor_from_c_str("#/derp"); |
| 3185 |
|
| 3186 |
ASSERT_INT_EQUALS( |
| 3187 |
0, |
| 3188 |
aws_mqtt_client_connection_subscribe( |
| 3189 |
connection, |
| 3190 |
&bad_topic, |
| 3191 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3192 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_topic_specific_publish, |
| 3193 |
&fixture, |
| 3194 |
NULL, |
| 3195 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_subscribe_complete, |
| 3196 |
&fixture)); |
| 3197 |
|
| 3198 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 3199 |
aws_mqtt_library_clean_up(); |
| 3200 |
|
| 3201 |
return AWS_OP_SUCCESS; |
| 3202 |
} |
| 3203 |
|
| 3204 |
AWS_TEST_CASE(mqtt5to3_adapter_subscribe_single_invalid, s_mqtt5to3_adapter_subscribe_single_invalid_fn) |
| 3205 |
|
| 3206 |
static int s_mqtt5to3_adapter_subscribe_multi_failure_fn(struct aws_allocator *allocator, void *ctx) { |
| 3207 |
(void)ctx; |
| 3208 |
|
| 3209 |
aws_mqtt_library_init(allocator); |
| 3210 |
|
| 3211 |
struct mqtt5_client_test_options test_options; |
| 3212 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 3213 |
|
| 3214 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] = |
| 3215 |
s_mqtt5_mock_server_handle_subscribe_suback_failure; |
| 3216 |
|
| 3217 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 3218 |
.client_options = &test_options.client_options, |
| 3219 |
.server_function_table = &test_options.server_function_table, |
| 3220 |
}; |
| 3221 |
|
| 3222 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 3223 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 3224 |
|
| 3225 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 3226 |
|
| 3227 |
struct aws_mqtt_connection_options connection_options; |
| 3228 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 3229 |
|
| 3230 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 3231 |
connection_options.user_data = &fixture; |
| 3232 |
|
| 3233 |
aws_mqtt_client_connection_connect(connection, &connection_options); |
| 3234 |
|
| 3235 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 3236 |
|
| 3237 |
struct aws_mqtt_topic_subscription subscriptions[] = { |
| 3238 |
{ |
| 3239 |
.topic = aws_byte_cursor_from_c_str("topic/1"), |
| 3240 |
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3241 |
}, |
| 3242 |
{ |
| 3243 |
.topic = aws_byte_cursor_from_c_str("topic/2"), |
| 3244 |
.qos = AWS_MQTT_QOS_AT_MOST_ONCE, |
| 3245 |
}, |
| 3246 |
}; |
| 3247 |
|
| 3248 |
struct aws_array_list subscription_list; |
| 3249 |
aws_array_list_init_static_from_initialized( |
| 3250 |
&subscription_list, subscriptions, 2, sizeof(struct aws_mqtt_topic_subscription)); |
| 3251 |
|
| 3252 |
aws_mqtt_client_connection_subscribe_multiple( |
| 3253 |
connection, |
| 3254 |
&subscription_list, |
| 3255 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_subscribe_multi_complete, |
| 3256 |
&fixture); |
| 3257 |
|
| 3258 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, 1); |
| 3259 |
|
| 3260 |
|
| 3261 |
subscriptions[0].qos = AWS_MQTT_QOS_FAILURE; |
| 3262 |
|
| 3263 |
struct aws_mqtt3_operation_event expected_events[] = {{ |
| 3264 |
.type = AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, |
| 3265 |
.error_code = AWS_ERROR_SUCCESS, |
| 3266 |
}}; |
| 3267 |
aws_array_list_init_static_from_initialized( |
| 3268 |
&expected_events[0].granted_subscriptions, |
| 3269 |
(void *)subscriptions, |
| 3270 |
2, |
| 3271 |
sizeof(struct aws_mqtt_topic_subscription)); |
| 3272 |
|
| 3273 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_operation_sequence( |
| 3274 |
&fixture, AWS_ARRAY_SIZE(expected_events), expected_events, AWS_ARRAY_SIZE(expected_events))); |
| 3275 |
|
| 3276 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 3277 |
aws_mqtt_library_clean_up(); |
| 3278 |
|
| 3279 |
return AWS_OP_SUCCESS; |
| 3280 |
} |
| 3281 |
|
| 3282 |
AWS_TEST_CASE(mqtt5to3_adapter_subscribe_multi_failure, s_mqtt5to3_adapter_subscribe_multi_failure_fn) |
| 3283 |
|
| 3284 |
static int s_mqtt5to3_adapter_subscribe_multi_invalid_fn(struct aws_allocator *allocator, void *ctx) { |
| 3285 |
(void)ctx; |
| 3286 |
|
| 3287 |
aws_mqtt_library_init(allocator); |
| 3288 |
|
| 3289 |
struct mqtt5_client_test_options test_options; |
| 3290 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 3291 |
|
| 3292 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] = |
| 3293 |
s_mqtt5_mock_server_handle_subscribe_suback_failure; |
| 3294 |
|
| 3295 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 3296 |
.client_options = &test_options.client_options, |
| 3297 |
.server_function_table = &test_options.server_function_table, |
| 3298 |
}; |
| 3299 |
|
| 3300 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 3301 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 3302 |
|
| 3303 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 3304 |
|
| 3305 |
struct aws_mqtt_connection_options connection_options; |
| 3306 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 3307 |
|
| 3308 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 3309 |
connection_options.user_data = &fixture; |
| 3310 |
|
| 3311 |
aws_mqtt_client_connection_connect(connection, &connection_options); |
| 3312 |
|
| 3313 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 3314 |
|
| 3315 |
struct aws_mqtt_topic_subscription subscriptions[] = { |
| 3316 |
{ |
| 3317 |
.topic = aws_byte_cursor_from_c_str("topic/1"), |
| 3318 |
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3319 |
}, |
| 3320 |
{ |
| 3321 |
.topic = aws_byte_cursor_from_c_str("#/#"), |
| 3322 |
.qos = AWS_MQTT_QOS_AT_MOST_ONCE, |
| 3323 |
}, |
| 3324 |
}; |
| 3325 |
|
| 3326 |
struct aws_array_list subscription_list; |
| 3327 |
aws_array_list_init_static_from_initialized( |
| 3328 |
&subscription_list, subscriptions, 2, sizeof(struct aws_mqtt_topic_subscription)); |
| 3329 |
|
| 3330 |
ASSERT_INT_EQUALS( |
| 3331 |
0, |
| 3332 |
aws_mqtt_client_connection_subscribe_multiple( |
| 3333 |
connection, |
| 3334 |
&subscription_list, |
| 3335 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_subscribe_multi_complete, |
| 3336 |
&fixture)); |
| 3337 |
|
| 3338 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 3339 |
aws_mqtt_library_clean_up(); |
| 3340 |
|
| 3341 |
return AWS_OP_SUCCESS; |
| 3342 |
} |
| 3343 |
|
| 3344 |
AWS_TEST_CASE(mqtt5to3_adapter_subscribe_multi_invalid, s_mqtt5to3_adapter_subscribe_multi_invalid_fn) |
| 3345 |
|
| 3346 |
static int s_mqtt5to3_adapter_subscribe_single_publish_fn(struct aws_allocator *allocator, void *ctx) { |
| 3347 |
(void)ctx; |
| 3348 |
|
| 3349 |
aws_mqtt_library_init(allocator); |
| 3350 |
|
| 3351 |
struct mqtt5_client_test_options test_options; |
| 3352 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 3353 |
|
| 3354 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] = |
| 3355 |
s_mqtt5_mock_server_handle_subscribe_suback_success; |
| 3356 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] = |
| 3357 |
aws_mqtt5_mock_server_handle_publish_puback_and_forward; |
| 3358 |
|
| 3359 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 3360 |
.client_options = &test_options.client_options, |
| 3361 |
.server_function_table = &test_options.server_function_table, |
| 3362 |
}; |
| 3363 |
|
| 3364 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 3365 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 3366 |
|
| 3367 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 3368 |
|
| 3369 |
aws_mqtt_client_connection_set_on_any_publish_handler( |
| 3370 |
connection, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_on_any_publish, &fixture); |
| 3371 |
|
| 3372 |
struct aws_mqtt_connection_options connection_options; |
| 3373 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 3374 |
|
| 3375 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 3376 |
connection_options.user_data = &fixture; |
| 3377 |
|
| 3378 |
aws_mqtt_client_connection_connect(connection, &connection_options); |
| 3379 |
|
| 3380 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 3381 |
|
| 3382 |
struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("derp"); |
| 3383 |
|
| 3384 |
aws_mqtt_client_connection_subscribe( |
| 3385 |
connection, |
| 3386 |
&topic, |
| 3387 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3388 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_topic_specific_publish, |
| 3389 |
&fixture, |
| 3390 |
NULL, |
| 3391 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_subscribe_complete, |
| 3392 |
&fixture); |
| 3393 |
|
| 3394 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, 1); |
| 3395 |
|
| 3396 |
struct aws_byte_cursor payload = aws_byte_cursor_from_c_str("Payload!"); |
| 3397 |
|
| 3398 |
aws_mqtt_client_connection_publish( |
| 3399 |
connection, |
| 3400 |
&topic, |
| 3401 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3402 |
false, |
| 3403 |
&payload, |
| 3404 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_publish_complete, |
| 3405 |
&fixture); |
| 3406 |
|
| 3407 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_PUBLISH_COMPLETE, 1); |
| 3408 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_PUBLISH_RECEIVED_SUBSCRIBED, 1); |
| 3409 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_PUBLISH_RECEIVED_ANY, 1); |
| 3410 |
|
| 3411 |
struct aws_mqtt3_operation_event expected_events[] = { |
| 3412 |
{ |
| 3413 |
.type = AWS_MQTT3_OET_PUBLISH_COMPLETE, |
| 3414 |
.error_code = AWS_ERROR_SUCCESS, |
| 3415 |
}, |
| 3416 |
{ |
| 3417 |
.type = AWS_MQTT3_OET_PUBLISH_RECEIVED_SUBSCRIBED, |
| 3418 |
.error_code = AWS_ERROR_SUCCESS, |
| 3419 |
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3420 |
.topic_cursor = topic, |
| 3421 |
.payload_cursor = payload, |
| 3422 |
}, |
| 3423 |
{ |
| 3424 |
.type = AWS_MQTT3_OET_PUBLISH_RECEIVED_ANY, |
| 3425 |
.error_code = AWS_ERROR_SUCCESS, |
| 3426 |
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3427 |
.topic_cursor = topic, |
| 3428 |
.payload_cursor = payload, |
| 3429 |
}, |
| 3430 |
}; |
| 3431 |
|
| 3432 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_operation_sequence_contains( |
| 3433 |
&fixture, AWS_ARRAY_SIZE(expected_events), expected_events)); |
| 3434 |
|
| 3435 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 3436 |
aws_mqtt_library_clean_up(); |
| 3437 |
|
| 3438 |
return AWS_OP_SUCCESS; |
| 3439 |
} |
| 3440 |
|
| 3441 |
AWS_TEST_CASE(mqtt5to3_adapter_subscribe_single_publish, s_mqtt5to3_adapter_subscribe_single_publish_fn) |
| 3442 |
|
| 3443 |
static int s_mqtt5to3_adapter_subscribe_multi_overlapping_publish_fn(struct aws_allocator *allocator, void *ctx) { |
| 3444 |
(void)ctx; |
| 3445 |
|
| 3446 |
aws_mqtt_library_init(allocator); |
| 3447 |
|
| 3448 |
struct mqtt5_client_test_options test_options; |
| 3449 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 3450 |
|
| 3451 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] = |
| 3452 |
s_mqtt5_mock_server_handle_subscribe_suback_success; |
| 3453 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] = |
| 3454 |
aws_mqtt5_mock_server_handle_publish_puback_and_forward; |
| 3455 |
|
| 3456 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 3457 |
.client_options = &test_options.client_options, |
| 3458 |
.server_function_table = &test_options.server_function_table, |
| 3459 |
}; |
| 3460 |
|
| 3461 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 3462 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 3463 |
|
| 3464 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 3465 |
|
| 3466 |
aws_mqtt_client_connection_set_on_any_publish_handler( |
| 3467 |
connection, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_on_any_publish, &fixture); |
| 3468 |
|
| 3469 |
struct aws_mqtt_connection_options connection_options; |
| 3470 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 3471 |
|
| 3472 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 3473 |
connection_options.user_data = &fixture; |
| 3474 |
|
| 3475 |
aws_mqtt_client_connection_connect(connection, &connection_options); |
| 3476 |
|
| 3477 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 3478 |
|
| 3479 |
struct aws_byte_cursor topic1 = aws_byte_cursor_from_c_str("hello/world"); |
| 3480 |
struct aws_byte_cursor topic2 = aws_byte_cursor_from_c_str("hello/+"); |
| 3481 |
struct aws_byte_cursor topic3 = aws_byte_cursor_from_c_str("derp"); |
| 3482 |
|
| 3483 |
ASSERT_TRUE( |
| 3484 |
0 != aws_mqtt_client_connection_subscribe( |
| 3485 |
connection, |
| 3486 |
&topic1, |
| 3487 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3488 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_topic_specific_publish, |
| 3489 |
&fixture, |
| 3490 |
NULL, |
| 3491 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_subscribe_complete, |
| 3492 |
&fixture)); |
| 3493 |
|
| 3494 |
ASSERT_TRUE( |
| 3495 |
0 != aws_mqtt_client_connection_subscribe( |
| 3496 |
connection, |
| 3497 |
&topic2, |
| 3498 |
AWS_MQTT_QOS_AT_MOST_ONCE, |
| 3499 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_topic_specific_publish, |
| 3500 |
&fixture, |
| 3501 |
NULL, |
| 3502 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_subscribe_complete, |
| 3503 |
&fixture)); |
| 3504 |
|
| 3505 |
ASSERT_TRUE( |
| 3506 |
0 != aws_mqtt_client_connection_subscribe( |
| 3507 |
connection, |
| 3508 |
&topic3, |
| 3509 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3510 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_topic_specific_publish, |
| 3511 |
&fixture, |
| 3512 |
NULL, |
| 3513 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_subscribe_complete, |
| 3514 |
&fixture)); |
| 3515 |
|
| 3516 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, 3); |
| 3517 |
|
| 3518 |
struct aws_byte_cursor payload1 = aws_byte_cursor_from_c_str("Payload 1!"); |
| 3519 |
struct aws_byte_cursor payload2 = aws_byte_cursor_from_c_str("Payload 2!"); |
| 3520 |
|
| 3521 |
aws_mqtt_client_connection_publish( |
| 3522 |
connection, |
| 3523 |
&topic1, |
| 3524 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3525 |
false, |
| 3526 |
&payload1, |
| 3527 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_publish_complete, |
| 3528 |
&fixture); |
| 3529 |
aws_mqtt_client_connection_publish( |
| 3530 |
connection, |
| 3531 |
&topic3, |
| 3532 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3533 |
false, |
| 3534 |
&payload2, |
| 3535 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_publish_complete, |
| 3536 |
&fixture); |
| 3537 |
|
| 3538 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_PUBLISH_COMPLETE, 2); |
| 3539 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_PUBLISH_RECEIVED_ANY, 2); |
| 3540 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_PUBLISH_RECEIVED_SUBSCRIBED, 3); |
| 3541 |
|
| 3542 |
struct aws_mqtt3_operation_event expected_events[] = { |
| 3543 |
{ |
| 3544 |
.type = AWS_MQTT3_OET_PUBLISH_RECEIVED_ANY, |
| 3545 |
.error_code = AWS_ERROR_SUCCESS, |
| 3546 |
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3547 |
.topic_cursor = topic1, |
| 3548 |
.payload_cursor = payload1, |
| 3549 |
}, |
| 3550 |
{ |
| 3551 |
.type = AWS_MQTT3_OET_PUBLISH_RECEIVED_ANY, |
| 3552 |
.error_code = AWS_ERROR_SUCCESS, |
| 3553 |
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3554 |
.topic_cursor = topic3, |
| 3555 |
.payload_cursor = payload2, |
| 3556 |
}, |
| 3557 |
{ |
| 3558 |
.type = AWS_MQTT3_OET_PUBLISH_RECEIVED_SUBSCRIBED, |
| 3559 |
.error_code = AWS_ERROR_SUCCESS, |
| 3560 |
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3561 |
.topic_cursor = topic1, |
| 3562 |
.payload_cursor = payload1, |
| 3563 |
.expected_count = 2, |
| 3564 |
}, |
| 3565 |
{ |
| 3566 |
.type = AWS_MQTT3_OET_PUBLISH_RECEIVED_SUBSCRIBED, |
| 3567 |
.error_code = AWS_ERROR_SUCCESS, |
| 3568 |
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3569 |
.topic_cursor = topic3, |
| 3570 |
.payload_cursor = payload2, |
| 3571 |
}, |
| 3572 |
}; |
| 3573 |
|
| 3574 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_operation_sequence_contains( |
| 3575 |
&fixture, AWS_ARRAY_SIZE(expected_events), expected_events)); |
| 3576 |
|
| 3577 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 3578 |
aws_mqtt_library_clean_up(); |
| 3579 |
|
| 3580 |
return AWS_OP_SUCCESS; |
| 3581 |
} |
| 3582 |
|
| 3583 |
AWS_TEST_CASE( |
| 3584 |
mqtt5to3_adapter_subscribe_multi_overlapping_publish, |
| 3585 |
s_mqtt5to3_adapter_subscribe_multi_overlapping_publish_fn) |
| 3586 |
|
| 3587 |
static void s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_unsubscribe_complete( |
| 3588 |
struct aws_mqtt_client_connection *connection, |
| 3589 |
uint16_t packet_id, |
| 3590 |
int error_code, |
| 3591 |
void *userdata) { |
| 3592 |
|
| 3593 |
(void)connection; |
| 3594 |
(void)packet_id; |
| 3595 |
|
| 3596 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture *fixture = userdata; |
| 3597 |
|
| 3598 |
struct aws_mqtt3_operation_event operation_event = { |
| 3599 |
.type = AWS_MQTT3_OET_UNSUBSCRIBE_COMPLETE, |
| 3600 |
.error_code = error_code, |
| 3601 |
}; |
| 3602 |
|
| 3603 |
aws_mutex_lock(&fixture->lock); |
| 3604 |
aws_array_list_push_back(&fixture->operation_events, &operation_event); |
| 3605 |
aws_mutex_unlock(&fixture->lock); |
| 3606 |
aws_condition_variable_notify_all(&fixture->signal); |
| 3607 |
} |
| 3608 |
|
| 3609 |
static int s_mqtt5to3_adapter_unsubscribe_success_fn(struct aws_allocator *allocator, void *ctx) { |
| 3610 |
(void)ctx; |
| 3611 |
|
| 3612 |
aws_mqtt_library_init(allocator); |
| 3613 |
|
| 3614 |
struct mqtt5_client_test_options test_options; |
| 3615 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 3616 |
|
| 3617 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] = |
| 3618 |
s_mqtt5_mock_server_handle_subscribe_suback_success; |
| 3619 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] = |
| 3620 |
aws_mqtt5_mock_server_handle_publish_puback_and_forward; |
| 3621 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_UNSUBSCRIBE] = |
| 3622 |
aws_mqtt5_mock_server_handle_unsubscribe_unsuback_success; |
| 3623 |
|
| 3624 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 3625 |
.client_options = &test_options.client_options, |
| 3626 |
.server_function_table = &test_options.server_function_table, |
| 3627 |
}; |
| 3628 |
|
| 3629 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 3630 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 3631 |
|
| 3632 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 3633 |
|
| 3634 |
aws_mqtt_client_connection_set_on_any_publish_handler( |
| 3635 |
connection, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_on_any_publish, &fixture); |
| 3636 |
|
| 3637 |
struct aws_mqtt_connection_options connection_options; |
| 3638 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 3639 |
|
| 3640 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 3641 |
connection_options.user_data = &fixture; |
| 3642 |
|
| 3643 |
aws_mqtt_client_connection_connect(connection, &connection_options); |
| 3644 |
|
| 3645 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 3646 |
|
| 3647 |
struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("hello/world"); |
| 3648 |
|
| 3649 |
aws_mqtt_client_connection_subscribe( |
| 3650 |
connection, |
| 3651 |
&topic, |
| 3652 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3653 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_topic_specific_publish, |
| 3654 |
&fixture, |
| 3655 |
NULL, |
| 3656 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_subscribe_complete, |
| 3657 |
&fixture); |
| 3658 |
|
| 3659 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, 1); |
| 3660 |
|
| 3661 |
struct aws_byte_cursor payload = aws_byte_cursor_from_c_str("Payload 1!"); |
| 3662 |
|
| 3663 |
aws_mqtt_client_connection_publish( |
| 3664 |
connection, |
| 3665 |
&topic, |
| 3666 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3667 |
false, |
| 3668 |
&payload, |
| 3669 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_publish_complete, |
| 3670 |
&fixture); |
| 3671 |
|
| 3672 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_PUBLISH_COMPLETE, 1); |
| 3673 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_PUBLISH_RECEIVED_ANY, 1); |
| 3674 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_PUBLISH_RECEIVED_SUBSCRIBED, 1); |
| 3675 |
|
| 3676 |
struct aws_mqtt3_operation_event expected_events_before[] = { |
| 3677 |
{ |
| 3678 |
.type = AWS_MQTT3_OET_PUBLISH_RECEIVED_ANY, |
| 3679 |
.error_code = AWS_ERROR_SUCCESS, |
| 3680 |
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3681 |
.topic_cursor = topic, |
| 3682 |
.payload_cursor = payload, |
| 3683 |
}, |
| 3684 |
{ |
| 3685 |
.type = AWS_MQTT3_OET_PUBLISH_RECEIVED_SUBSCRIBED, |
| 3686 |
.error_code = AWS_ERROR_SUCCESS, |
| 3687 |
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3688 |
.topic_cursor = topic, |
| 3689 |
.payload_cursor = payload, |
| 3690 |
}, |
| 3691 |
}; |
| 3692 |
|
| 3693 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_operation_sequence_contains( |
| 3694 |
&fixture, AWS_ARRAY_SIZE(expected_events_before), expected_events_before)); |
| 3695 |
|
| 3696 |
aws_mqtt_client_connection_unsubscribe( |
| 3697 |
connection, &topic, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_unsubscribe_complete, &fixture); |
| 3698 |
|
| 3699 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_UNSUBSCRIBE_COMPLETE, 1); |
| 3700 |
|
| 3701 |
aws_mqtt_client_connection_publish( |
| 3702 |
connection, |
| 3703 |
&topic, |
| 3704 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3705 |
false, |
| 3706 |
&payload, |
| 3707 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_publish_complete, |
| 3708 |
&fixture); |
| 3709 |
|
| 3710 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_PUBLISH_RECEIVED_ANY, 2); |
| 3711 |
|
| 3712 |
struct aws_mqtt3_operation_event expected_events_after[] = { |
| 3713 |
{ |
| 3714 |
.type = AWS_MQTT3_OET_PUBLISH_RECEIVED_ANY, |
| 3715 |
.error_code = AWS_ERROR_SUCCESS, |
| 3716 |
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3717 |
.topic_cursor = topic, |
| 3718 |
.payload_cursor = payload, |
| 3719 |
.expected_count = 2, |
| 3720 |
}, |
| 3721 |
{ |
| 3722 |
.type = AWS_MQTT3_OET_PUBLISH_RECEIVED_SUBSCRIBED, |
| 3723 |
.error_code = AWS_ERROR_SUCCESS, |
| 3724 |
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3725 |
.topic_cursor = topic, |
| 3726 |
.payload_cursor = payload, |
| 3727 |
}, |
| 3728 |
{ |
| 3729 |
.type = AWS_MQTT3_OET_UNSUBSCRIBE_COMPLETE, |
| 3730 |
}, |
| 3731 |
}; |
| 3732 |
|
| 3733 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_operation_sequence_contains( |
| 3734 |
&fixture, AWS_ARRAY_SIZE(expected_events_after), expected_events_after)); |
| 3735 |
|
| 3736 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 3737 |
aws_mqtt_library_clean_up(); |
| 3738 |
|
| 3739 |
return AWS_OP_SUCCESS; |
| 3740 |
} |
| 3741 |
|
| 3742 |
AWS_TEST_CASE(mqtt5to3_adapter_unsubscribe_success, s_mqtt5to3_adapter_unsubscribe_success_fn) |
| 3743 |
|
| 3744 |
static int s_mqtt5_mock_server_handle_unsubscribe_unsuback_failure( |
| 3745 |
void *packet, |
| 3746 |
struct aws_mqtt5_server_mock_connection_context *connection, |
| 3747 |
void *user_data) { |
| 3748 |
(void)packet; |
| 3749 |
(void)user_data; |
| 3750 |
|
| 3751 |
struct aws_mqtt5_packet_unsubscribe_view *unsubscribe_view = packet; |
| 3752 |
|
| 3753 |
AWS_VARIABLE_LENGTH_ARRAY( |
| 3754 |
enum aws_mqtt5_unsuback_reason_code, mqtt5_unsuback_codes, unsubscribe_view->topic_filter_count); |
| 3755 |
for (size_t i = 0; i < unsubscribe_view->topic_filter_count; ++i) { |
| 3756 |
enum aws_mqtt5_unsuback_reason_code *reason_code_ptr = &mqtt5_unsuback_codes[i]; |
| 3757 |
*reason_code_ptr = AWS_MQTT5_UARC_IMPLEMENTATION_SPECIFIC_ERROR; |
| 3758 |
} |
| 3759 |
|
| 3760 |
struct aws_mqtt5_packet_unsuback_view unsuback_view = { |
| 3761 |
.packet_id = unsubscribe_view->packet_id, |
| 3762 |
.reason_code_count = AWS_ARRAY_SIZE(mqtt5_unsuback_codes), |
| 3763 |
.reason_codes = mqtt5_unsuback_codes, |
| 3764 |
}; |
| 3765 |
|
| 3766 |
return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_UNSUBACK, &unsuback_view); |
| 3767 |
} |
| 3768 |
|
| 3769 |
static int s_mqtt5to3_adapter_unsubscribe_failure_fn(struct aws_allocator *allocator, void *ctx) { |
| 3770 |
(void)ctx; |
| 3771 |
|
| 3772 |
aws_mqtt_library_init(allocator); |
| 3773 |
|
| 3774 |
struct mqtt5_client_test_options test_options; |
| 3775 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 3776 |
|
| 3777 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_UNSUBSCRIBE] = |
| 3778 |
s_mqtt5_mock_server_handle_unsubscribe_unsuback_failure; |
| 3779 |
|
| 3780 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 3781 |
.client_options = &test_options.client_options, |
| 3782 |
.server_function_table = &test_options.server_function_table, |
| 3783 |
}; |
| 3784 |
|
| 3785 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 3786 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 3787 |
|
| 3788 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 3789 |
|
| 3790 |
struct aws_mqtt_connection_options connection_options; |
| 3791 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 3792 |
|
| 3793 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 3794 |
connection_options.user_data = &fixture; |
| 3795 |
|
| 3796 |
aws_mqtt_client_connection_connect(connection, &connection_options); |
| 3797 |
|
| 3798 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 3799 |
|
| 3800 |
struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("hello/world"); |
| 3801 |
|
| 3802 |
aws_mqtt_client_connection_unsubscribe( |
| 3803 |
connection, &topic, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_unsubscribe_complete, &fixture); |
| 3804 |
|
| 3805 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_UNSUBSCRIBE_COMPLETE, 1); |
| 3806 |
|
| 3807 |
struct aws_mqtt3_operation_event expected_events[] = { |
| 3808 |
{ |
| 3809 |
.type = AWS_MQTT3_OET_UNSUBSCRIBE_COMPLETE, |
| 3810 |
}, |
| 3811 |
}; |
| 3812 |
|
| 3813 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_operation_sequence_contains( |
| 3814 |
&fixture, AWS_ARRAY_SIZE(expected_events), expected_events)); |
| 3815 |
|
| 3816 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 3817 |
aws_mqtt_library_clean_up(); |
| 3818 |
|
| 3819 |
return AWS_OP_SUCCESS; |
| 3820 |
} |
| 3821 |
|
| 3822 |
AWS_TEST_CASE(mqtt5to3_adapter_unsubscribe_failure, s_mqtt5to3_adapter_unsubscribe_failure_fn) |
| 3823 |
|
| 3824 |
static int s_mqtt5to3_adapter_unsubscribe_invalid_fn(struct aws_allocator *allocator, void *ctx) { |
| 3825 |
(void)ctx; |
| 3826 |
|
| 3827 |
aws_mqtt_library_init(allocator); |
| 3828 |
|
| 3829 |
struct mqtt5_client_test_options test_options; |
| 3830 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 3831 |
|
| 3832 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_UNSUBSCRIBE] = |
| 3833 |
s_mqtt5_mock_server_handle_unsubscribe_unsuback_failure; |
| 3834 |
|
| 3835 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 3836 |
.client_options = &test_options.client_options, |
| 3837 |
.server_function_table = &test_options.server_function_table, |
| 3838 |
}; |
| 3839 |
|
| 3840 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 3841 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 3842 |
|
| 3843 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 3844 |
|
| 3845 |
struct aws_mqtt_connection_options connection_options; |
| 3846 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 3847 |
|
| 3848 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 3849 |
connection_options.user_data = &fixture; |
| 3850 |
|
| 3851 |
aws_mqtt_client_connection_connect(connection, &connection_options); |
| 3852 |
|
| 3853 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 3854 |
|
| 3855 |
struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("#/bad"); |
| 3856 |
|
| 3857 |
ASSERT_INT_EQUALS( |
| 3858 |
0, |
| 3859 |
aws_mqtt_client_connection_unsubscribe( |
| 3860 |
connection, &topic, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_unsubscribe_complete, &fixture)); |
| 3861 |
|
| 3862 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 3863 |
aws_mqtt_library_clean_up(); |
| 3864 |
|
| 3865 |
return AWS_OP_SUCCESS; |
| 3866 |
} |
| 3867 |
|
| 3868 |
AWS_TEST_CASE(mqtt5to3_adapter_unsubscribe_invalid, s_mqtt5to3_adapter_unsubscribe_invalid_fn) |
| 3869 |
|
| 3870 |
static int s_mqtt5to3_adapter_unsubscribe_overlapped_fn(struct aws_allocator *allocator, void *ctx) { |
| 3871 |
(void)ctx; |
| 3872 |
|
| 3873 |
aws_mqtt_library_init(allocator); |
| 3874 |
|
| 3875 |
struct mqtt5_client_test_options test_options; |
| 3876 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 3877 |
|
| 3878 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] = |
| 3879 |
s_mqtt5_mock_server_handle_subscribe_suback_success; |
| 3880 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] = |
| 3881 |
aws_mqtt5_mock_server_handle_publish_puback_and_forward; |
| 3882 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_UNSUBSCRIBE] = |
| 3883 |
aws_mqtt5_mock_server_handle_unsubscribe_unsuback_success; |
| 3884 |
|
| 3885 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 3886 |
.client_options = &test_options.client_options, |
| 3887 |
.server_function_table = &test_options.server_function_table, |
| 3888 |
}; |
| 3889 |
|
| 3890 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 3891 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 3892 |
|
| 3893 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 3894 |
|
| 3895 |
aws_mqtt_client_connection_set_on_any_publish_handler( |
| 3896 |
connection, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_on_any_publish, &fixture); |
| 3897 |
|
| 3898 |
struct aws_mqtt_connection_options connection_options; |
| 3899 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 3900 |
|
| 3901 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 3902 |
connection_options.user_data = &fixture; |
| 3903 |
|
| 3904 |
aws_mqtt_client_connection_connect(connection, &connection_options); |
| 3905 |
|
| 3906 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 3907 |
|
| 3908 |
struct aws_byte_cursor topic1 = aws_byte_cursor_from_c_str("hello/world"); |
| 3909 |
struct aws_byte_cursor topic2 = aws_byte_cursor_from_c_str("hello/+"); |
| 3910 |
|
| 3911 |
aws_mqtt_client_connection_subscribe( |
| 3912 |
connection, |
| 3913 |
&topic1, |
| 3914 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3915 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_topic_specific_publish, |
| 3916 |
&fixture, |
| 3917 |
NULL, |
| 3918 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_subscribe_complete, |
| 3919 |
&fixture); |
| 3920 |
|
| 3921 |
aws_mqtt_client_connection_subscribe( |
| 3922 |
connection, |
| 3923 |
&topic2, |
| 3924 |
AWS_MQTT_QOS_AT_MOST_ONCE, |
| 3925 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_topic_specific_publish, |
| 3926 |
&fixture, |
| 3927 |
NULL, |
| 3928 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_subscribe_complete, |
| 3929 |
&fixture); |
| 3930 |
|
| 3931 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, 2); |
| 3932 |
|
| 3933 |
struct aws_byte_cursor payload1 = aws_byte_cursor_from_c_str("Payload 1!"); |
| 3934 |
|
| 3935 |
aws_mqtt_client_connection_publish( |
| 3936 |
connection, |
| 3937 |
&topic1, |
| 3938 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3939 |
false, |
| 3940 |
&payload1, |
| 3941 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_publish_complete, |
| 3942 |
&fixture); |
| 3943 |
|
| 3944 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_PUBLISH_COMPLETE, 1); |
| 3945 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_PUBLISH_RECEIVED_ANY, 1); |
| 3946 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_PUBLISH_RECEIVED_SUBSCRIBED, 2); |
| 3947 |
|
| 3948 |
struct aws_mqtt3_operation_event expected_events_before[] = { |
| 3949 |
{ |
| 3950 |
.type = AWS_MQTT3_OET_PUBLISH_RECEIVED_ANY, |
| 3951 |
.error_code = AWS_ERROR_SUCCESS, |
| 3952 |
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3953 |
.topic_cursor = topic1, |
| 3954 |
.payload_cursor = payload1, |
| 3955 |
}, |
| 3956 |
{ |
| 3957 |
.type = AWS_MQTT3_OET_PUBLISH_RECEIVED_SUBSCRIBED, |
| 3958 |
.error_code = AWS_ERROR_SUCCESS, |
| 3959 |
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3960 |
.topic_cursor = topic1, |
| 3961 |
.payload_cursor = payload1, |
| 3962 |
.expected_count = 2, |
| 3963 |
}, |
| 3964 |
}; |
| 3965 |
|
| 3966 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_operation_sequence_contains( |
| 3967 |
&fixture, AWS_ARRAY_SIZE(expected_events_before), expected_events_before)); |
| 3968 |
|
| 3969 |
|
| 3970 |
aws_mqtt_client_connection_unsubscribe( |
| 3971 |
connection, &topic2, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_unsubscribe_complete, &fixture); |
| 3972 |
|
| 3973 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_UNSUBSCRIBE_COMPLETE, 1); |
| 3974 |
|
| 3975 |
aws_mqtt_client_connection_publish( |
| 3976 |
connection, |
| 3977 |
&topic1, |
| 3978 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3979 |
false, |
| 3980 |
&payload1, |
| 3981 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_publish_complete, |
| 3982 |
&fixture); |
| 3983 |
|
| 3984 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_PUBLISH_COMPLETE, 2); |
| 3985 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_PUBLISH_RECEIVED_ANY, 2); |
| 3986 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_PUBLISH_RECEIVED_SUBSCRIBED, 3); |
| 3987 |
|
| 3988 |
struct aws_mqtt3_operation_event expected_events_after[] = { |
| 3989 |
{ |
| 3990 |
.type = AWS_MQTT3_OET_PUBLISH_RECEIVED_ANY, |
| 3991 |
.error_code = AWS_ERROR_SUCCESS, |
| 3992 |
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 3993 |
.topic_cursor = topic1, |
| 3994 |
.payload_cursor = payload1, |
| 3995 |
.expected_count = 2, |
| 3996 |
}, |
| 3997 |
{ |
| 3998 |
.type = AWS_MQTT3_OET_PUBLISH_RECEIVED_SUBSCRIBED, |
| 3999 |
.error_code = AWS_ERROR_SUCCESS, |
| 4000 |
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 4001 |
.topic_cursor = topic1, |
| 4002 |
.payload_cursor = payload1, |
| 4003 |
.expected_count = 3, |
| 4004 |
}, |
| 4005 |
{ |
| 4006 |
.type = AWS_MQTT3_OET_UNSUBSCRIBE_COMPLETE, |
| 4007 |
}, |
| 4008 |
}; |
| 4009 |
|
| 4010 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_operation_sequence_contains( |
| 4011 |
&fixture, AWS_ARRAY_SIZE(expected_events_after), expected_events_after)); |
| 4012 |
|
| 4013 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 4014 |
aws_mqtt_library_clean_up(); |
| 4015 |
|
| 4016 |
return AWS_OP_SUCCESS; |
| 4017 |
} |
| 4018 |
|
| 4019 |
AWS_TEST_CASE(mqtt5to3_adapter_unsubscribe_overlapped, s_mqtt5to3_adapter_unsubscribe_overlapped_fn) |
| 4020 |
|
| 4021 |
static int s_mqtt5_mock_server_handle_connect_publish_throttled( |
| 4022 |
void *packet, |
| 4023 |
struct aws_mqtt5_server_mock_connection_context *connection, |
| 4024 |
void *user_data) { |
| 4025 |
(void)packet; |
| 4026 |
(void)user_data; |
| 4027 |
|
| 4028 |
struct aws_mqtt5_packet_connack_view connack_view; |
| 4029 |
AWS_ZERO_STRUCT(connack_view); |
| 4030 |
|
| 4031 |
uint16_t receive_maximum = 1; |
| 4032 |
|
| 4033 |
connack_view.reason_code = AWS_MQTT5_CRC_SUCCESS; |
| 4034 |
connack_view.receive_maximum = &receive_maximum; |
| 4035 |
|
| 4036 |
return aws_mqtt5_mock_server_send_packet(connection, AWS_MQTT5_PT_CONNACK, &connack_view); |
| 4037 |
} |
| 4038 |
|
| 4039 |
|
| 4040 |
|
| 4041 |
|
| 4042 |
|
| 4043 |
|
| 4044 |
|
| 4045 |
static int s_mqtt5to3_adapter_get_stats_fn(struct aws_allocator *allocator, void *ctx) { |
| 4046 |
(void)ctx; |
| 4047 |
|
| 4048 |
aws_mqtt_library_init(allocator); |
| 4049 |
|
| 4050 |
struct mqtt5_client_test_options test_options; |
| 4051 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 4052 |
|
| 4053 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] = |
| 4054 |
s_mqtt5_mock_server_handle_connect_publish_throttled; |
| 4055 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] = NULL; |
| 4056 |
|
| 4057 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 4058 |
.client_options = &test_options.client_options, |
| 4059 |
.server_function_table = &test_options.server_function_table, |
| 4060 |
}; |
| 4061 |
|
| 4062 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 4063 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 4064 |
|
| 4065 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 4066 |
|
| 4067 |
aws_mqtt_client_connection_set_on_any_publish_handler( |
| 4068 |
connection, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_on_any_publish, &fixture); |
| 4069 |
|
| 4070 |
struct aws_mqtt_connection_options connection_options; |
| 4071 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 4072 |
|
| 4073 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 4074 |
connection_options.user_data = &fixture; |
| 4075 |
|
| 4076 |
aws_mqtt_client_connection_connect(connection, &connection_options); |
| 4077 |
|
| 4078 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 4079 |
|
| 4080 |
struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("hi/there"); |
| 4081 |
struct aws_byte_cursor payload = aws_byte_cursor_from_c_str("something"); |
| 4082 |
|
| 4083 |
aws_mqtt_client_connection_publish( |
| 4084 |
connection, |
| 4085 |
&topic, |
| 4086 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 4087 |
false, |
| 4088 |
&payload, |
| 4089 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_publish_complete, |
| 4090 |
&fixture); |
| 4091 |
aws_mqtt_client_connection_publish( |
| 4092 |
connection, |
| 4093 |
&topic, |
| 4094 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 4095 |
false, |
| 4096 |
&payload, |
| 4097 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_publish_complete, |
| 4098 |
&fixture); |
| 4099 |
aws_mqtt_client_connection_publish( |
| 4100 |
connection, |
| 4101 |
&topic, |
| 4102 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 4103 |
false, |
| 4104 |
&payload, |
| 4105 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_publish_complete, |
| 4106 |
&fixture); |
| 4107 |
|
| 4108 |
aws_thread_current_sleep(aws_timestamp_convert(1, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL)); |
| 4109 |
|
| 4110 |
struct aws_mqtt_connection_operation_statistics stats; |
| 4111 |
aws_mqtt_client_connection_get_stats(connection, &stats); |
| 4112 |
|
| 4113 |
ASSERT_INT_EQUALS(1, stats.unacked_operation_count); |
| 4114 |
ASSERT_INT_EQUALS(3, stats.incomplete_operation_count); |
| 4115 |
ASSERT_TRUE(stats.unacked_operation_size > 0); |
| 4116 |
ASSERT_TRUE(stats.incomplete_operation_size > 0); |
| 4117 |
ASSERT_INT_EQUALS(stats.unacked_operation_size * 3, stats.incomplete_operation_size); |
| 4118 |
|
| 4119 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 4120 |
aws_mqtt_library_clean_up(); |
| 4121 |
|
| 4122 |
return AWS_OP_SUCCESS; |
| 4123 |
} |
| 4124 |
|
| 4125 |
AWS_TEST_CASE(mqtt5to3_adapter_get_stats, s_mqtt5to3_adapter_get_stats_fn) |
| 4126 |
|
| 4127 |
|
| 4128 |
|
| 4129 |
|
| 4130 |
|
| 4131 |
static int s_mqtt5to3_adapter_resubscribe_nothing_fn(struct aws_allocator *allocator, void *ctx) { |
| 4132 |
(void)ctx; |
| 4133 |
|
| 4134 |
aws_mqtt_library_init(allocator); |
| 4135 |
|
| 4136 |
struct mqtt5_client_test_options test_options; |
| 4137 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 4138 |
|
| 4139 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] = |
| 4140 |
s_mqtt5_mock_server_handle_subscribe_suback_success; |
| 4141 |
|
| 4142 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 4143 |
.client_options = &test_options.client_options, |
| 4144 |
.server_function_table = &test_options.server_function_table, |
| 4145 |
}; |
| 4146 |
|
| 4147 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 4148 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 4149 |
|
| 4150 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 4151 |
|
| 4152 |
aws_mqtt_client_connection_set_on_any_publish_handler( |
| 4153 |
connection, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_on_any_publish, &fixture); |
| 4154 |
|
| 4155 |
struct aws_mqtt_connection_options connection_options; |
| 4156 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 4157 |
|
| 4158 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 4159 |
connection_options.user_data = &fixture; |
| 4160 |
|
| 4161 |
aws_mqtt_client_connection_connect(connection, &connection_options); |
| 4162 |
|
| 4163 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 4164 |
|
| 4165 |
aws_mqtt_resubscribe_existing_topics( |
| 4166 |
connection, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_subscribe_multi_complete, &fixture); |
| 4167 |
|
| 4168 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, 1); |
| 4169 |
|
| 4170 |
struct aws_mqtt3_operation_event resubscribe_ack[] = {{ |
| 4171 |
.type = AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, .error_code = AWS_ERROR_MQTT_CONNECTION_RESUBSCRIBE_NO_TOPICS, |
| 4172 |
|
| 4173 |
}}; |
| 4174 |
|
| 4175 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_operation_sequence_contains( |
| 4176 |
&fixture, AWS_ARRAY_SIZE(resubscribe_ack), resubscribe_ack)); |
| 4177 |
|
| 4178 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 4179 |
aws_mqtt_library_clean_up(); |
| 4180 |
|
| 4181 |
return AWS_OP_SUCCESS; |
| 4182 |
} |
| 4183 |
|
| 4184 |
AWS_TEST_CASE(mqtt5to3_adapter_resubscribe_nothing, s_mqtt5to3_adapter_resubscribe_nothing_fn) |
| 4185 |
|
| 4186 |
|
| 4187 |
|
| 4188 |
|
| 4189 |
|
| 4190 |
static int s_mqtt5to3_adapter_resubscribe_something_fn(struct aws_allocator *allocator, void *ctx) { |
| 4191 |
(void)ctx; |
| 4192 |
|
| 4193 |
aws_mqtt_library_init(allocator); |
| 4194 |
|
| 4195 |
struct mqtt5_client_test_options test_options; |
| 4196 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 4197 |
|
| 4198 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] = |
| 4199 |
s_mqtt5_mock_server_handle_subscribe_suback_success; |
| 4200 |
|
| 4201 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 4202 |
.client_options = &test_options.client_options, |
| 4203 |
.server_function_table = &test_options.server_function_table, |
| 4204 |
}; |
| 4205 |
|
| 4206 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 4207 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 4208 |
|
| 4209 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 4210 |
|
| 4211 |
aws_mqtt_client_connection_set_on_any_publish_handler( |
| 4212 |
connection, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_on_any_publish, &fixture); |
| 4213 |
|
| 4214 |
struct aws_mqtt_connection_options connection_options; |
| 4215 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 4216 |
|
| 4217 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 4218 |
connection_options.user_data = &fixture; |
| 4219 |
|
| 4220 |
aws_mqtt_client_connection_connect(connection, &connection_options); |
| 4221 |
|
| 4222 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 4223 |
|
| 4224 |
struct aws_byte_cursor topic1 = aws_byte_cursor_from_c_str("hello/world"); |
| 4225 |
struct aws_byte_cursor topic2 = aws_byte_cursor_from_c_str("foo/bar"); |
| 4226 |
struct aws_byte_cursor topic3 = aws_byte_cursor_from_c_str("a/b/c"); |
| 4227 |
|
| 4228 |
aws_mqtt_client_connection_subscribe( |
| 4229 |
connection, |
| 4230 |
&topic1, |
| 4231 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 4232 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_topic_specific_publish, |
| 4233 |
&fixture, |
| 4234 |
NULL, |
| 4235 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_subscribe_complete, |
| 4236 |
&fixture); |
| 4237 |
|
| 4238 |
aws_mqtt_client_connection_subscribe( |
| 4239 |
connection, |
| 4240 |
&topic2, |
| 4241 |
AWS_MQTT_QOS_AT_MOST_ONCE, |
| 4242 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_topic_specific_publish, |
| 4243 |
&fixture, |
| 4244 |
NULL, |
| 4245 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_subscribe_complete, |
| 4246 |
&fixture); |
| 4247 |
|
| 4248 |
aws_mqtt_client_connection_subscribe( |
| 4249 |
connection, |
| 4250 |
&topic3, |
| 4251 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 4252 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_topic_specific_publish, |
| 4253 |
&fixture, |
| 4254 |
NULL, |
| 4255 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_subscribe_complete, |
| 4256 |
&fixture); |
| 4257 |
|
| 4258 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, 3); |
| 4259 |
|
| 4260 |
aws_mqtt_resubscribe_existing_topics( |
| 4261 |
connection, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_subscribe_multi_complete, &fixture); |
| 4262 |
|
| 4263 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, 4); |
| 4264 |
|
| 4265 |
struct aws_mqtt3_operation_event resubscribe_ack[] = {{ |
| 4266 |
.type = AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, |
| 4267 |
}}; |
| 4268 |
|
| 4269 |
struct aws_mqtt_topic_subscription subscriptions[] = { |
| 4270 |
{ |
| 4271 |
.topic = topic1, |
| 4272 |
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 4273 |
}, |
| 4274 |
{ |
| 4275 |
.topic = topic2, |
| 4276 |
.qos = AWS_MQTT_QOS_AT_MOST_ONCE, |
| 4277 |
}, |
| 4278 |
{ |
| 4279 |
.topic = topic3, |
| 4280 |
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 4281 |
}, |
| 4282 |
}; |
| 4283 |
|
| 4284 |
aws_array_list_init_static_from_initialized( |
| 4285 |
&resubscribe_ack[0].granted_subscriptions, |
| 4286 |
(void *)subscriptions, |
| 4287 |
AWS_ARRAY_SIZE(subscriptions), |
| 4288 |
sizeof(struct aws_mqtt_topic_subscription)); |
| 4289 |
|
| 4290 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_operation_sequence_contains( |
| 4291 |
&fixture, AWS_ARRAY_SIZE(resubscribe_ack), resubscribe_ack)); |
| 4292 |
|
| 4293 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 4294 |
aws_mqtt_library_clean_up(); |
| 4295 |
|
| 4296 |
return AWS_OP_SUCCESS; |
| 4297 |
} |
| 4298 |
|
| 4299 |
AWS_TEST_CASE(mqtt5to3_adapter_resubscribe_something, s_mqtt5to3_adapter_resubscribe_something_fn) |
| 4300 |
|
| 4301 |
static int s_mqtt5to3_adapter_operation_callbacks_after_shutdown_fn(struct aws_allocator *allocator, void *ctx) { |
| 4302 |
(void)ctx; |
| 4303 |
|
| 4304 |
aws_mqtt_library_init(allocator); |
| 4305 |
|
| 4306 |
struct mqtt5_client_test_options test_options; |
| 4307 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 4308 |
|
| 4309 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_SUBSCRIBE] = NULL; |
| 4310 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_PUBLISH] = NULL; |
| 4311 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_UNSUBSCRIBE] = NULL; |
| 4312 |
|
| 4313 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 4314 |
.client_options = &test_options.client_options, |
| 4315 |
.server_function_table = &test_options.server_function_table, |
| 4316 |
}; |
| 4317 |
|
| 4318 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 4319 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 4320 |
|
| 4321 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 4322 |
|
| 4323 |
struct aws_mqtt_connection_options connection_options; |
| 4324 |
s_init_adapter_connection_options_from_fixture(&connection_options, &fixture); |
| 4325 |
|
| 4326 |
connection_options.on_connection_complete = s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_connection_complete; |
| 4327 |
connection_options.user_data = &fixture; |
| 4328 |
|
| 4329 |
aws_mqtt_client_connection_connect(connection, &connection_options); |
| 4330 |
|
| 4331 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_CONNECTION_COMPLETE, 1); |
| 4332 |
|
| 4333 |
struct aws_byte_cursor topic1 = aws_byte_cursor_from_c_str("hello/world"); |
| 4334 |
struct aws_byte_cursor topic2 = aws_byte_cursor_from_c_str("hello/+"); |
| 4335 |
|
| 4336 |
aws_mqtt_client_connection_subscribe( |
| 4337 |
connection, |
| 4338 |
&topic1, |
| 4339 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 4340 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_topic_specific_publish, |
| 4341 |
&fixture, |
| 4342 |
NULL, |
| 4343 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_subscribe_complete, |
| 4344 |
&fixture); |
| 4345 |
|
| 4346 |
struct aws_byte_cursor payload1 = aws_byte_cursor_from_c_str("Payload 1!"); |
| 4347 |
|
| 4348 |
aws_mqtt_client_connection_publish( |
| 4349 |
connection, |
| 4350 |
&topic1, |
| 4351 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 4352 |
false, |
| 4353 |
&payload1, |
| 4354 |
s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_publish_complete, |
| 4355 |
&fixture); |
| 4356 |
|
| 4357 |
aws_mqtt_client_connection_unsubscribe( |
| 4358 |
connection, &topic2, s_aws_mqtt5_to_mqtt3_adapter_test_fixture_record_unsubscribe_complete, &fixture); |
| 4359 |
|
| 4360 |
aws_mqtt_client_connection_release(connection); |
| 4361 |
|
| 4362 |
s_wait_for_n_adapter_lifecycle_events(&fixture, AWS_MQTT3_LET_TERMINATION, 1); |
| 4363 |
fixture.connection = NULL; |
| 4364 |
|
| 4365 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, 1); |
| 4366 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_PUBLISH_COMPLETE, 1); |
| 4367 |
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_UNSUBSCRIBE_COMPLETE, 1); |
| 4368 |
|
| 4369 |
struct aws_mqtt3_operation_event failed_ops[] = { |
| 4370 |
{ |
| 4371 |
.type = AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, |
| 4372 |
.error_code = AWS_ERROR_MQTT_CONNECTION_DESTROYED, |
| 4373 |
}, |
| 4374 |
{ |
| 4375 |
.type = AWS_MQTT3_OET_PUBLISH_COMPLETE, |
| 4376 |
.error_code = AWS_ERROR_MQTT_CONNECTION_DESTROYED, |
| 4377 |
}, |
| 4378 |
{ |
| 4379 |
.type = AWS_MQTT3_OET_UNSUBSCRIBE_COMPLETE, |
| 4380 |
.error_code = AWS_ERROR_MQTT_CONNECTION_DESTROYED, |
| 4381 |
}, |
| 4382 |
}; |
| 4383 |
|
| 4384 |
struct aws_mqtt_topic_subscription failed_subscriptions[] = { |
| 4385 |
{ |
| 4386 |
.topic = topic1, |
| 4387 |
.qos = AWS_MQTT_QOS_FAILURE, |
| 4388 |
}, |
| 4389 |
}; |
| 4390 |
|
| 4391 |
aws_array_list_init_static_from_initialized( |
| 4392 |
&failed_ops[0].granted_subscriptions, |
| 4393 |
(void *)failed_subscriptions, |
| 4394 |
AWS_ARRAY_SIZE(failed_subscriptions), |
| 4395 |
sizeof(struct aws_mqtt_topic_subscription)); |
| 4396 |
|
| 4397 |
ASSERT_SUCCESS(s_aws_mqtt5_to_mqtt3_adapter_test_fixture_verify_operation_sequence_contains( |
| 4398 |
&fixture, AWS_ARRAY_SIZE(failed_ops), failed_ops)); |
| 4399 |
|
| 4400 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 4401 |
aws_mqtt_library_clean_up(); |
| 4402 |
|
| 4403 |
return AWS_OP_SUCCESS; |
| 4404 |
} |
| 4405 |
|
| 4406 |
AWS_TEST_CASE( |
| 4407 |
mqtt5to3_adapter_operation_callbacks_after_shutdown, |
| 4408 |
s_mqtt5to3_adapter_operation_callbacks_after_shutdown_fn) |
| 4409 |
|
| 4410 |
|
| 4411 |
|
| 4412 |
|
| 4413 |
static int s_mqtt5to3_adapter_connection_set_metrics_valid_fn(struct aws_allocator *allocator, void *ctx) { |
| 4414 |
(void)ctx; |
| 4415 |
|
| 4416 |
aws_mqtt_library_init(allocator); |
| 4417 |
|
| 4418 |
struct mqtt5_client_test_options test_options; |
| 4419 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 4420 |
|
| 4421 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 4422 |
.client_options = &test_options.client_options, |
| 4423 |
.server_function_table = &test_options.server_function_table, |
| 4424 |
}; |
| 4425 |
|
| 4426 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 4427 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 4428 |
|
| 4429 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 4430 |
|
| 4431 |
struct aws_mqtt_metadata_entry metadata_entries[] = { |
| 4432 |
{ |
| 4433 |
.key = aws_byte_cursor_from_c_str("lang"), |
| 4434 |
.value = aws_byte_cursor_from_c_str("C"), |
| 4435 |
}, |
| 4436 |
{ |
| 4437 |
.key = aws_byte_cursor_from_c_str("version"), |
| 4438 |
.value = aws_byte_cursor_from_c_str("1.0.0"), |
| 4439 |
}, |
| 4440 |
}; |
| 4441 |
|
| 4442 |
struct aws_mqtt_iot_metrics metrics = { |
| 4443 |
.library_name = aws_byte_cursor_from_c_str("TestSDK/1.0"), |
| 4444 |
.metadata_count = AWS_ARRAY_SIZE(metadata_entries), |
| 4445 |
.metadata_entries = metadata_entries, |
| 4446 |
}; |
| 4447 |
|
| 4448 |
ASSERT_SUCCESS(aws_mqtt_client_connection_set_metrics(connection, &metrics)); |
| 4449 |
|
| 4450 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 4451 |
aws_mqtt_library_clean_up(); |
| 4452 |
|
| 4453 |
return AWS_OP_SUCCESS; |
| 4454 |
} |
| 4455 |
|
| 4456 |
AWS_TEST_CASE(mqtt5to3_adapter_set_metrics_valid, s_mqtt5to3_adapter_connection_set_metrics_valid_fn) |
| 4457 |
|
| 4458 |
|
| 4459 |
|
| 4460 |
|
| 4461 |
static int s_mqtt5to3_adapter_connection_set_metrics_null_fn(struct aws_allocator *allocator, void *ctx) { |
| 4462 |
(void)ctx; |
| 4463 |
|
| 4464 |
aws_mqtt_library_init(allocator); |
| 4465 |
|
| 4466 |
struct mqtt5_client_test_options test_options; |
| 4467 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 4468 |
|
| 4469 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 4470 |
.client_options = &test_options.client_options, |
| 4471 |
.server_function_table = &test_options.server_function_table, |
| 4472 |
}; |
| 4473 |
|
| 4474 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 4475 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 4476 |
|
| 4477 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 4478 |
|
| 4479 |
ASSERT_SUCCESS(aws_mqtt_client_connection_set_metrics(connection, NULL)); |
| 4480 |
|
| 4481 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 4482 |
aws_mqtt_library_clean_up(); |
| 4483 |
|
| 4484 |
return AWS_OP_SUCCESS; |
| 4485 |
} |
| 4486 |
|
| 4487 |
AWS_TEST_CASE(mqtt5to3_adapter_set_metrics_null, s_mqtt5to3_adapter_connection_set_metrics_null_fn) |
| 4488 |
|
| 4489 |
|
| 4490 |
|
| 4491 |
|
| 4492 |
static int s_mqtt5to3_adapter_connection_set_metrics_invalid_utf8_library_fn( |
| 4493 |
struct aws_allocator *allocator, |
| 4494 |
void *ctx) { |
| 4495 |
(void)ctx; |
| 4496 |
|
| 4497 |
aws_mqtt_library_init(allocator); |
| 4498 |
|
| 4499 |
struct mqtt5_client_test_options test_options; |
| 4500 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 4501 |
|
| 4502 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 4503 |
.client_options = &test_options.client_options, |
| 4504 |
.server_function_table = &test_options.server_function_table, |
| 4505 |
}; |
| 4506 |
|
| 4507 |
struct aws_mqtt5_to_mqtt3_adapter_test_fixture fixture; |
| 4508 |
ASSERT_SUCCESS(aws_mqtt5_to_mqtt3_adapter_test_fixture_init(&fixture, allocator, &test_fixture_options)); |
| 4509 |
|
| 4510 |
struct aws_mqtt_client_connection *connection = fixture.connection; |
| 4511 |
|
| 4512 |
|
| 4513 |
struct aws_byte_cursor invalid_utf8_library = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("TestSDK\xFF\xFE"); |
| 4514 |
|
| 4515 |
struct aws_mqtt_metadata_entry metadata_entries[] = {{ |
| 4516 |
.key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("key1\xFF\xFE"), |
| 4517 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("value1\xFF\xFE"), |
| 4518 |
}}; |
| 4519 |
|
| 4520 |
struct aws_mqtt_iot_metrics metrics = { |
| 4521 |
.library_name = invalid_utf8_library, |
| 4522 |
.metadata_count = AWS_ARRAY_SIZE(metadata_entries), |
| 4523 |
.metadata_entries = metadata_entries, |
| 4524 |
}; |
| 4525 |
|
| 4526 |
ASSERT_FAILS(aws_mqtt_client_connection_set_metrics(connection, &metrics)); |
| 4527 |
ASSERT_INT_EQUALS(aws_last_error(), AWS_ERROR_INVALID_UTF8); |
| 4528 |
|
| 4529 |
aws_mqtt5_to_mqtt3_adapter_test_fixture_clean_up(&fixture); |
| 4530 |
aws_mqtt_library_clean_up(); |
| 4531 |
|
| 4532 |
return AWS_OP_SUCCESS; |
| 4533 |
} |
| 4534 |
|
| 4535 |
AWS_TEST_CASE( |
| 4536 |
mqtt5to3_adapter_set_metrics_invalid_utf8_library, |
| 4537 |
s_mqtt5to3_adapter_connection_set_metrics_invalid_utf8_library_fn) |