| 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/mqtt/mqtt.h> |
| 11 |
#include <aws/mqtt/private/v5/mqtt5_utils.h> |
| 12 |
#include <aws/mqtt/v5/mqtt5_client.h> |
| 13 |
#include <aws/mqtt/v5/mqtt5_listener.h> |
| 14 |
|
| 15 |
#include <aws/testing/aws_test_harness.h> |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
struct aws_mqtt5_manual_pub_ack_test_context { |
| 21 |
struct aws_mqtt5_client_mock_test_fixture *test_fixture; |
| 22 |
|
| 23 |
|
| 24 |
uint64_t pub_ack_control_ids[10]; |
| 25 |
size_t puback_control_count; |
| 26 |
|
| 27 |
|
| 28 |
enum aws_mqtt5_manual_publish_acknowledgement_result puback_results[10]; |
| 29 |
size_t puback_result_count; |
| 30 |
|
| 31 |
|
| 32 |
bool publish_received; |
| 33 |
size_t publishes_received_count; |
| 34 |
bool puback_callback_invoked; |
| 35 |
size_t puback_callbacks_invoked_count; |
| 36 |
|
| 37 |
|
| 38 |
uint16_t received_packet_ids[10]; |
| 39 |
size_t received_packet_id_count; |
| 40 |
}; |
| 41 |
|
| 42 |
static void s_manual_pub_ack_test_context_init(struct aws_mqtt5_manual_pub_ack_test_context *context) { |
| 43 |
AWS_ZERO_STRUCT(*context); |
| 44 |
} |
| 45 |
|
| 46 |
static void s_manual_pub_ack_completion_fn( |
| 47 |
enum aws_mqtt5_manual_publish_acknowledgement_result puback_result, |
| 48 |
void *complete_ctx) { |
| 49 |
|
| 50 |
struct aws_mqtt5_manual_pub_ack_test_context *context = complete_ctx; |
| 51 |
struct aws_mqtt5_client_mock_test_fixture *test_fixture = context->test_fixture; |
| 52 |
|
| 53 |
aws_mutex_lock(&test_fixture->lock); |
| 54 |
|
| 55 |
if (context->puback_result_count < AWS_ARRAY_SIZE(context->puback_results)) { |
| 56 |
context->puback_results[context->puback_result_count++] = puback_result; |
| 57 |
} |
| 58 |
|
| 59 |
context->puback_callback_invoked = true; |
| 60 |
context->puback_callbacks_invoked_count++; |
| 61 |
|
| 62 |
aws_mutex_unlock(&test_fixture->lock); |
| 63 |
aws_condition_variable_notify_all(&test_fixture->signal); |
| 64 |
} |
| 65 |
|
| 66 |
static bool s_manual_pub_ack_publish_received_handler( |
| 67 |
const struct aws_mqtt5_packet_publish_view *publish, |
| 68 |
void *user_data) { |
| 69 |
|
| 70 |
struct aws_mqtt5_manual_pub_ack_test_context *context = user_data; |
| 71 |
struct aws_mqtt5_client_mock_test_fixture *test_fixture = context->test_fixture; |
| 72 |
|
| 73 |
if (publish->qos == AWS_MQTT5_QOS_AT_LEAST_ONCE) { |
| 74 |
aws_mutex_lock(&test_fixture->lock); |
| 75 |
|
| 76 |
|
| 77 |
uint64_t control_id = aws_mqtt5_client_acquire_publish_acknowledgement(test_fixture->client, publish); |
| 78 |
|
| 79 |
if (context->puback_control_count < AWS_ARRAY_SIZE(context->pub_ack_control_ids)) { |
| 80 |
context->pub_ack_control_ids[context->puback_control_count++] = control_id; |
| 81 |
} |
| 82 |
|
| 83 |
if (context->received_packet_id_count < AWS_ARRAY_SIZE(context->received_packet_ids)) { |
| 84 |
context->received_packet_ids[context->received_packet_id_count++] = publish->packet_id; |
| 85 |
} |
| 86 |
|
| 87 |
context->publish_received = true; |
| 88 |
context->publishes_received_count++; |
| 89 |
|
| 90 |
aws_mutex_unlock(&test_fixture->lock); |
| 91 |
aws_condition_variable_notify_all(&test_fixture->signal); |
| 92 |
|
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
static bool s_manual_pub_ack_callback_invoked(void *arg) { |
| 100 |
struct aws_mqtt5_manual_pub_ack_test_context *context = arg; |
| 101 |
return context->puback_callback_invoked; |
| 102 |
} |
| 103 |
|
| 104 |
static void s_wait_for_manual_pub_ack_callback(struct aws_mqtt5_manual_pub_ack_test_context *context) { |
| 105 |
struct aws_mqtt5_client_mock_test_fixture *test_fixture = context->test_fixture; |
| 106 |
|
| 107 |
aws_mutex_lock(&test_fixture->lock); |
| 108 |
aws_condition_variable_wait_pred( |
| 109 |
&test_fixture->signal, &test_fixture->lock, s_manual_pub_ack_callback_invoked, context); |
| 110 |
aws_mutex_unlock(&test_fixture->lock); |
| 111 |
} |
| 112 |
|
| 113 |
static bool s_manual_pub_ack_publish_received(void *arg) { |
| 114 |
struct aws_mqtt5_manual_pub_ack_test_context *context = arg; |
| 115 |
return context->publish_received; |
| 116 |
} |
| 117 |
|
| 118 |
static void s_wait_for_manual_pub_ack_publish(struct aws_mqtt5_manual_pub_ack_test_context *context) { |
| 119 |
struct aws_mqtt5_client_mock_test_fixture *test_fixture = context->test_fixture; |
| 120 |
|
| 121 |
aws_mutex_lock(&test_fixture->lock); |
| 122 |
aws_condition_variable_wait_pred( |
| 123 |
&test_fixture->signal, &test_fixture->lock, s_manual_pub_ack_publish_received, context); |
| 124 |
aws_mutex_unlock(&test_fixture->lock); |
| 125 |
} |
| 126 |
|
| 127 |
static void s_wait_for_n_manual_pub_ack_publishes(struct aws_mqtt5_manual_pub_ack_test_context *context, size_t count) { |
| 128 |
struct aws_mqtt5_client_mock_test_fixture *test_fixture = context->test_fixture; |
| 129 |
|
| 130 |
aws_mutex_lock(&test_fixture->lock); |
| 131 |
while (context->publishes_received_count < count) { |
| 132 |
aws_condition_variable_wait(&test_fixture->signal, &test_fixture->lock); |
| 133 |
} |
| 134 |
aws_mutex_unlock(&test_fixture->lock); |
| 135 |
} |
| 136 |
|
| 137 |
static void s_wait_for_n_manual_pub_ack_callbacks(struct aws_mqtt5_manual_pub_ack_test_context *context, size_t count) { |
| 138 |
struct aws_mqtt5_client_mock_test_fixture *test_fixture = context->test_fixture; |
| 139 |
|
| 140 |
aws_mutex_lock(&test_fixture->lock); |
| 141 |
while (context->puback_callbacks_invoked_count < count) { |
| 142 |
aws_condition_variable_wait(&test_fixture->signal, &test_fixture->lock); |
| 143 |
} |
| 144 |
aws_mutex_unlock(&test_fixture->lock); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
static uint8_t s_test_topic[] = "test/manual/puback"; |
| 149 |
static uint8_t s_test_payload[] = "test_payload"; |
| 150 |
|
| 151 |
struct aws_mqtt5_server_manual_pub_ack_context { |
| 152 |
struct aws_mqtt5_client_mock_test_fixture *test_fixture; |
| 153 |
bool publish_sent; |
| 154 |
bool connack_sent; |
| 155 |
size_t publishes_to_send; |
| 156 |
}; |
| 157 |
|
| 158 |
static void s_aws_mqtt5_mock_server_send_qos1_publish( |
| 159 |
struct aws_mqtt5_server_mock_connection_context *mock_server, |
| 160 |
void *user_data) { |
| 161 |
|
| 162 |
struct aws_mqtt5_server_manual_pub_ack_context *context = user_data; |
| 163 |
|
| 164 |
if (context->publish_sent || !context->connack_sent) { |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
context->publish_sent = true; |
| 169 |
|
| 170 |
size_t count = context->publishes_to_send > 0 ? context->publishes_to_send : 1; |
| 171 |
|
| 172 |
for (size_t i = 0; i < count; i++) { |
| 173 |
struct aws_mqtt5_packet_publish_view publish_view = { |
| 174 |
.packet_id = (uint16_t)(i + 1), |
| 175 |
.qos = AWS_MQTT5_QOS_AT_LEAST_ONCE, |
| 176 |
.topic = |
| 177 |
{ |
| 178 |
.ptr = s_test_topic, |
| 179 |
.len = AWS_ARRAY_SIZE(s_test_topic) - 1, |
| 180 |
}, |
| 181 |
.payload = |
| 182 |
{ |
| 183 |
.ptr = s_test_payload, |
| 184 |
.len = AWS_ARRAY_SIZE(s_test_payload) - 1, |
| 185 |
}, |
| 186 |
}; |
| 187 |
|
| 188 |
aws_mqtt5_mock_server_send_packet(mock_server, AWS_MQTT5_PT_PUBLISH, &publish_view); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
static int s_aws_mqtt5_server_send_qos1_publish_on_connect( |
| 193 |
void *packet, |
| 194 |
struct aws_mqtt5_server_mock_connection_context *connection, |
| 195 |
void *user_data) { |
| 196 |
|
| 197 |
int result = aws_mqtt5_mock_server_handle_connect_always_succeed(packet, connection, user_data); |
| 198 |
|
| 199 |
struct aws_mqtt5_server_manual_pub_ack_context *context = user_data; |
| 200 |
context->connack_sent = true; |
| 201 |
|
| 202 |
return result; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
static int s_mqtt5_client_manual_pub_ack_basic_success_fn(struct aws_allocator *allocator, void *ctx) { |
| 213 |
(void)ctx; |
| 214 |
|
| 215 |
aws_mqtt_library_init(allocator); |
| 216 |
|
| 217 |
struct mqtt5_client_test_options test_options; |
| 218 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 219 |
|
| 220 |
struct aws_mqtt5_manual_pub_ack_test_context puback_context; |
| 221 |
s_manual_pub_ack_test_context_init(&puback_context); |
| 222 |
|
| 223 |
struct aws_mqtt5_server_manual_pub_ack_context server_context = { |
| 224 |
.publish_sent = false, |
| 225 |
.connack_sent = false, |
| 226 |
.publishes_to_send = 1, |
| 227 |
}; |
| 228 |
|
| 229 |
test_options.server_function_table.service_task_fn = s_aws_mqtt5_mock_server_send_qos1_publish; |
| 230 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] = |
| 231 |
s_aws_mqtt5_server_send_qos1_publish_on_connect; |
| 232 |
|
| 233 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 234 |
.client_options = &test_options.client_options, |
| 235 |
.server_function_table = &test_options.server_function_table, |
| 236 |
.mock_server_user_data = &server_context, |
| 237 |
}; |
| 238 |
|
| 239 |
struct aws_mqtt5_client_mock_test_fixture test_context; |
| 240 |
ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options)); |
| 241 |
|
| 242 |
puback_context.test_fixture = &test_context; |
| 243 |
server_context.test_fixture = &test_context; |
| 244 |
|
| 245 |
|
| 246 |
struct aws_mqtt5_listener_config listener_config = { |
| 247 |
.client = test_context.client, |
| 248 |
.listener_callbacks = { |
| 249 |
.listener_publish_received_handler = s_manual_pub_ack_publish_received_handler, |
| 250 |
.listener_publish_received_handler_user_data = &puback_context, |
| 251 |
}}; |
| 252 |
|
| 253 |
struct aws_mqtt5_listener *listener = aws_mqtt5_listener_new(allocator, &listener_config); |
| 254 |
ASSERT_NOT_NULL(listener); |
| 255 |
|
| 256 |
|
| 257 |
ASSERT_SUCCESS(aws_mqtt5_client_start(test_context.client)); |
| 258 |
aws_wait_for_connected_lifecycle_event(&test_context); |
| 259 |
|
| 260 |
|
| 261 |
s_wait_for_manual_pub_ack_publish(&puback_context); |
| 262 |
|
| 263 |
|
| 264 |
aws_mutex_lock(&test_context.lock); |
| 265 |
ASSERT_TRUE(puback_context.puback_control_count == 1); |
| 266 |
ASSERT_TRUE(puback_context.pub_ack_control_ids[0] != 0); |
| 267 |
uint64_t control_id = puback_context.pub_ack_control_ids[0]; |
| 268 |
aws_mutex_unlock(&test_context.lock); |
| 269 |
|
| 270 |
|
| 271 |
struct aws_mqtt5_manual_publish_acknowledgement_completion_options completion_options = { |
| 272 |
.completion_callback = s_manual_pub_ack_completion_fn, |
| 273 |
.completion_user_data = &puback_context, |
| 274 |
}; |
| 275 |
|
| 276 |
ASSERT_SUCCESS( |
| 277 |
aws_mqtt5_client_invoke_publish_acknowledgement(test_context.client, control_id, &completion_options)); |
| 278 |
|
| 279 |
|
| 280 |
s_wait_for_manual_pub_ack_callback(&puback_context); |
| 281 |
|
| 282 |
|
| 283 |
aws_mutex_lock(&test_context.lock); |
| 284 |
ASSERT_INT_EQUALS(1, puback_context.puback_result_count); |
| 285 |
ASSERT_INT_EQUALS(AWS_MQTT5_MPAR_SUCCESS, puback_context.puback_results[0]); |
| 286 |
aws_mutex_unlock(&test_context.lock); |
| 287 |
|
| 288 |
|
| 289 |
aws_mqtt5_listener_release(listener); |
| 290 |
ASSERT_SUCCESS(aws_mqtt5_client_stop(test_context.client, NULL, NULL)); |
| 291 |
aws_wait_for_stopped_lifecycle_event(&test_context); |
| 292 |
|
| 293 |
aws_mqtt5_client_mock_test_fixture_clean_up(&test_context); |
| 294 |
aws_mqtt_library_clean_up(); |
| 295 |
|
| 296 |
return AWS_OP_SUCCESS; |
| 297 |
} |
| 298 |
|
| 299 |
AWS_TEST_CASE(mqtt5_client_manual_pub_ack_basic_success, s_mqtt5_client_manual_pub_ack_basic_success_fn) |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
static int s_mqtt5_client_manual_pub_ack_no_auto_puback_fn(struct aws_allocator *allocator, void *ctx) { |
| 310 |
(void)ctx; |
| 311 |
|
| 312 |
aws_mqtt_library_init(allocator); |
| 313 |
|
| 314 |
struct mqtt5_client_test_options test_options; |
| 315 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 316 |
|
| 317 |
struct aws_mqtt5_manual_pub_ack_test_context puback_context; |
| 318 |
s_manual_pub_ack_test_context_init(&puback_context); |
| 319 |
|
| 320 |
struct aws_mqtt5_server_manual_pub_ack_context server_context = { |
| 321 |
.publish_sent = false, |
| 322 |
.connack_sent = false, |
| 323 |
.publishes_to_send = 1, |
| 324 |
}; |
| 325 |
|
| 326 |
test_options.server_function_table.service_task_fn = s_aws_mqtt5_mock_server_send_qos1_publish; |
| 327 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] = |
| 328 |
s_aws_mqtt5_server_send_qos1_publish_on_connect; |
| 329 |
|
| 330 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 331 |
.client_options = &test_options.client_options, |
| 332 |
.server_function_table = &test_options.server_function_table, |
| 333 |
.mock_server_user_data = &server_context, |
| 334 |
}; |
| 335 |
|
| 336 |
struct aws_mqtt5_client_mock_test_fixture test_context; |
| 337 |
ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options)); |
| 338 |
|
| 339 |
puback_context.test_fixture = &test_context; |
| 340 |
server_context.test_fixture = &test_context; |
| 341 |
|
| 342 |
|
| 343 |
struct aws_mqtt5_listener_config listener_config = { |
| 344 |
.client = test_context.client, |
| 345 |
.listener_callbacks = { |
| 346 |
.listener_publish_received_handler = s_manual_pub_ack_publish_received_handler, |
| 347 |
.listener_publish_received_handler_user_data = &puback_context, |
| 348 |
}}; |
| 349 |
|
| 350 |
struct aws_mqtt5_listener *listener = aws_mqtt5_listener_new(allocator, &listener_config); |
| 351 |
|
| 352 |
ASSERT_SUCCESS(aws_mqtt5_client_start(test_context.client)); |
| 353 |
aws_wait_for_connected_lifecycle_event(&test_context); |
| 354 |
|
| 355 |
s_wait_for_manual_pub_ack_publish(&puback_context); |
| 356 |
|
| 357 |
aws_mutex_lock(&test_context.lock); |
| 358 |
uint64_t control_id = puback_context.pub_ack_control_ids[0]; |
| 359 |
aws_mutex_unlock(&test_context.lock); |
| 360 |
|
| 361 |
|
| 362 |
aws_thread_current_sleep(aws_timestamp_convert(1, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL)); |
| 363 |
|
| 364 |
|
| 365 |
aws_mutex_lock(&test_context.lock); |
| 366 |
size_t packet_count = aws_array_list_length(&test_context.server_received_packets); |
| 367 |
bool found_puback = false; |
| 368 |
for (size_t i = 0; i < packet_count; i++) { |
| 369 |
struct aws_mqtt5_mock_server_packet_record *record = NULL; |
| 370 |
aws_array_list_get_at_ptr(&test_context.server_received_packets, (void **)&record, i); |
| 371 |
if (record->packet_type == AWS_MQTT5_PT_PUBACK) { |
| 372 |
found_puback = true; |
| 373 |
break; |
| 374 |
} |
| 375 |
} |
| 376 |
ASSERT_FALSE(found_puback); |
| 377 |
aws_mutex_unlock(&test_context.lock); |
| 378 |
|
| 379 |
|
| 380 |
struct aws_mqtt5_manual_publish_acknowledgement_completion_options completion_options = { |
| 381 |
.completion_callback = s_manual_pub_ack_completion_fn, |
| 382 |
.completion_user_data = &puback_context, |
| 383 |
}; |
| 384 |
|
| 385 |
ASSERT_SUCCESS( |
| 386 |
aws_mqtt5_client_invoke_publish_acknowledgement(test_context.client, control_id, &completion_options)); |
| 387 |
s_wait_for_manual_pub_ack_callback(&puback_context); |
| 388 |
|
| 389 |
|
| 390 |
aws_thread_current_sleep(aws_timestamp_convert(100, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL)); |
| 391 |
|
| 392 |
aws_mutex_lock(&test_context.lock); |
| 393 |
packet_count = aws_array_list_length(&test_context.server_received_packets); |
| 394 |
found_puback = false; |
| 395 |
for (size_t i = 0; i < packet_count; i++) { |
| 396 |
struct aws_mqtt5_mock_server_packet_record *record = NULL; |
| 397 |
aws_array_list_get_at_ptr(&test_context.server_received_packets, (void **)&record, i); |
| 398 |
if (record->packet_type == AWS_MQTT5_PT_PUBACK) { |
| 399 |
found_puback = true; |
| 400 |
break; |
| 401 |
} |
| 402 |
} |
| 403 |
ASSERT_TRUE(found_puback); |
| 404 |
aws_mutex_unlock(&test_context.lock); |
| 405 |
|
| 406 |
aws_mqtt5_listener_release(listener); |
| 407 |
ASSERT_SUCCESS(aws_mqtt5_client_stop(test_context.client, NULL, NULL)); |
| 408 |
aws_wait_for_stopped_lifecycle_event(&test_context); |
| 409 |
|
| 410 |
aws_mqtt5_client_mock_test_fixture_clean_up(&test_context); |
| 411 |
aws_mqtt_library_clean_up(); |
| 412 |
|
| 413 |
return AWS_OP_SUCCESS; |
| 414 |
} |
| 415 |
|
| 416 |
AWS_TEST_CASE(mqtt5_client_manual_pub_ack_no_auto_puback, s_mqtt5_client_manual_pub_ack_no_auto_puback_fn) |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
static int s_mqtt5_client_manual_pub_ack_invalid_control_id_fn(struct aws_allocator *allocator, void *ctx) { |
| 424 |
(void)ctx; |
| 425 |
|
| 426 |
aws_mqtt_library_init(allocator); |
| 427 |
|
| 428 |
struct mqtt5_client_test_options test_options; |
| 429 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 430 |
|
| 431 |
struct aws_mqtt5_manual_pub_ack_test_context puback_context; |
| 432 |
s_manual_pub_ack_test_context_init(&puback_context); |
| 433 |
|
| 434 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 435 |
.client_options = &test_options.client_options, |
| 436 |
.server_function_table = &test_options.server_function_table, |
| 437 |
}; |
| 438 |
|
| 439 |
struct aws_mqtt5_client_mock_test_fixture test_context; |
| 440 |
ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options)); |
| 441 |
|
| 442 |
puback_context.test_fixture = &test_context; |
| 443 |
|
| 444 |
ASSERT_SUCCESS(aws_mqtt5_client_start(test_context.client)); |
| 445 |
aws_wait_for_connected_lifecycle_event(&test_context); |
| 446 |
|
| 447 |
|
| 448 |
struct aws_mqtt5_manual_publish_acknowledgement_completion_options completion_options = { |
| 449 |
.completion_callback = s_manual_pub_ack_completion_fn, |
| 450 |
.completion_user_data = &puback_context, |
| 451 |
}; |
| 452 |
|
| 453 |
uint64_t invalid_control_id = 999999; |
| 454 |
ASSERT_SUCCESS( |
| 455 |
aws_mqtt5_client_invoke_publish_acknowledgement(test_context.client, invalid_control_id, &completion_options)); |
| 456 |
|
| 457 |
s_wait_for_manual_pub_ack_callback(&puback_context); |
| 458 |
|
| 459 |
|
| 460 |
aws_mutex_lock(&test_context.lock); |
| 461 |
ASSERT_INT_EQUALS(1, puback_context.puback_result_count); |
| 462 |
ASSERT_INT_EQUALS(AWS_MQTT5_MPAR_PUBACK_INVALID, puback_context.puback_results[0]); |
| 463 |
aws_mutex_unlock(&test_context.lock); |
| 464 |
|
| 465 |
ASSERT_SUCCESS(aws_mqtt5_client_stop(test_context.client, NULL, NULL)); |
| 466 |
aws_wait_for_stopped_lifecycle_event(&test_context); |
| 467 |
|
| 468 |
aws_mqtt5_client_mock_test_fixture_clean_up(&test_context); |
| 469 |
aws_mqtt_library_clean_up(); |
| 470 |
|
| 471 |
return AWS_OP_SUCCESS; |
| 472 |
} |
| 473 |
|
| 474 |
AWS_TEST_CASE(mqtt5_client_manual_pub_ack_invalid_control_id, s_mqtt5_client_manual_pub_ack_invalid_control_id_fn) |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
static int s_mqtt5_client_manual_pub_ack_multiple_publishes_fn(struct aws_allocator *allocator, void *ctx) { |
| 484 |
(void)ctx; |
| 485 |
|
| 486 |
aws_mqtt_library_init(allocator); |
| 487 |
|
| 488 |
struct mqtt5_client_test_options test_options; |
| 489 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 490 |
|
| 491 |
struct aws_mqtt5_manual_pub_ack_test_context puback_context; |
| 492 |
s_manual_pub_ack_test_context_init(&puback_context); |
| 493 |
|
| 494 |
struct aws_mqtt5_server_manual_pub_ack_context server_context = { |
| 495 |
.publish_sent = false, |
| 496 |
.connack_sent = false, |
| 497 |
.publishes_to_send = 3, |
| 498 |
}; |
| 499 |
|
| 500 |
test_options.server_function_table.service_task_fn = s_aws_mqtt5_mock_server_send_qos1_publish; |
| 501 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] = |
| 502 |
s_aws_mqtt5_server_send_qos1_publish_on_connect; |
| 503 |
|
| 504 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 505 |
.client_options = &test_options.client_options, |
| 506 |
.server_function_table = &test_options.server_function_table, |
| 507 |
.mock_server_user_data = &server_context, |
| 508 |
}; |
| 509 |
|
| 510 |
struct aws_mqtt5_client_mock_test_fixture test_context; |
| 511 |
ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options)); |
| 512 |
|
| 513 |
puback_context.test_fixture = &test_context; |
| 514 |
server_context.test_fixture = &test_context; |
| 515 |
|
| 516 |
struct aws_mqtt5_listener_config listener_config = { |
| 517 |
.client = test_context.client, |
| 518 |
.listener_callbacks = { |
| 519 |
.listener_publish_received_handler = s_manual_pub_ack_publish_received_handler, |
| 520 |
.listener_publish_received_handler_user_data = &puback_context, |
| 521 |
}}; |
| 522 |
|
| 523 |
struct aws_mqtt5_listener *listener = aws_mqtt5_listener_new(allocator, &listener_config); |
| 524 |
|
| 525 |
ASSERT_SUCCESS(aws_mqtt5_client_start(test_context.client)); |
| 526 |
aws_wait_for_connected_lifecycle_event(&test_context); |
| 527 |
|
| 528 |
|
| 529 |
s_wait_for_n_manual_pub_ack_publishes(&puback_context, 3); |
| 530 |
|
| 531 |
|
| 532 |
aws_mutex_lock(&test_context.lock); |
| 533 |
ASSERT_INT_EQUALS(3, puback_context.puback_control_count); |
| 534 |
aws_mutex_unlock(&test_context.lock); |
| 535 |
|
| 536 |
|
| 537 |
struct aws_mqtt5_manual_publish_acknowledgement_completion_options completion_options = { |
| 538 |
.completion_callback = s_manual_pub_ack_completion_fn, |
| 539 |
.completion_user_data = &puback_context, |
| 540 |
}; |
| 541 |
|
| 542 |
for (int i = 2; i >= 0; i--) { |
| 543 |
ASSERT_SUCCESS(aws_mqtt5_client_invoke_publish_acknowledgement( |
| 544 |
test_context.client, puback_context.pub_ack_control_ids[i], &completion_options)); |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
s_wait_for_n_manual_pub_ack_callbacks(&puback_context, 3); |
| 549 |
|
| 550 |
|
| 551 |
aws_mutex_lock(&test_context.lock); |
| 552 |
ASSERT_INT_EQUALS(3, puback_context.puback_result_count); |
| 553 |
for (size_t i = 0; i < 3; i++) { |
| 554 |
ASSERT_INT_EQUALS(AWS_MQTT5_MPAR_SUCCESS, puback_context.puback_results[i]); |
| 555 |
} |
| 556 |
aws_mutex_unlock(&test_context.lock); |
| 557 |
|
| 558 |
aws_mqtt5_listener_release(listener); |
| 559 |
ASSERT_SUCCESS(aws_mqtt5_client_stop(test_context.client, NULL, NULL)); |
| 560 |
aws_wait_for_stopped_lifecycle_event(&test_context); |
| 561 |
|
| 562 |
aws_mqtt5_client_mock_test_fixture_clean_up(&test_context); |
| 563 |
aws_mqtt_library_clean_up(); |
| 564 |
|
| 565 |
return AWS_OP_SUCCESS; |
| 566 |
} |
| 567 |
|
| 568 |
AWS_TEST_CASE(mqtt5_client_manual_pub_ack_multiple_publishes, s_mqtt5_client_manual_pub_ack_multiple_publishes_fn) |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
static int s_mqtt5_client_manual_pub_ack_disconnect_cancellation_fn(struct aws_allocator *allocator, void *ctx) { |
| 579 |
(void)ctx; |
| 580 |
|
| 581 |
aws_mqtt_library_init(allocator); |
| 582 |
|
| 583 |
struct mqtt5_client_test_options test_options; |
| 584 |
aws_mqtt5_client_test_init_default_options(&test_options); |
| 585 |
|
| 586 |
struct aws_mqtt5_manual_pub_ack_test_context puback_context; |
| 587 |
s_manual_pub_ack_test_context_init(&puback_context); |
| 588 |
|
| 589 |
struct aws_mqtt5_server_manual_pub_ack_context server_context = { |
| 590 |
.publish_sent = false, |
| 591 |
.connack_sent = false, |
| 592 |
.publishes_to_send = 1, |
| 593 |
}; |
| 594 |
|
| 595 |
test_options.server_function_table.service_task_fn = s_aws_mqtt5_mock_server_send_qos1_publish; |
| 596 |
test_options.server_function_table.packet_handlers[AWS_MQTT5_PT_CONNECT] = |
| 597 |
s_aws_mqtt5_server_send_qos1_publish_on_connect; |
| 598 |
|
| 599 |
struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 600 |
.client_options = &test_options.client_options, |
| 601 |
.server_function_table = &test_options.server_function_table, |
| 602 |
.mock_server_user_data = &server_context, |
| 603 |
}; |
| 604 |
|
| 605 |
struct aws_mqtt5_client_mock_test_fixture test_context; |
| 606 |
ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options)); |
| 607 |
|
| 608 |
puback_context.test_fixture = &test_context; |
| 609 |
server_context.test_fixture = &test_context; |
| 610 |
|
| 611 |
struct aws_mqtt5_listener_config listener_config = { |
| 612 |
.client = test_context.client, |
| 613 |
.listener_callbacks = { |
| 614 |
.listener_publish_received_handler = s_manual_pub_ack_publish_received_handler, |
| 615 |
.listener_publish_received_handler_user_data = &puback_context, |
| 616 |
}}; |
| 617 |
|
| 618 |
struct aws_mqtt5_listener *listener = aws_mqtt5_listener_new(allocator, &listener_config); |
| 619 |
|
| 620 |
ASSERT_SUCCESS(aws_mqtt5_client_start(test_context.client)); |
| 621 |
aws_wait_for_connected_lifecycle_event(&test_context); |
| 622 |
|
| 623 |
s_wait_for_manual_pub_ack_publish(&puback_context); |
| 624 |
|
| 625 |
aws_mutex_lock(&test_context.lock); |
| 626 |
uint64_t control_id = puback_context.pub_ack_control_ids[0]; |
| 627 |
aws_mutex_unlock(&test_context.lock); |
| 628 |
|
| 629 |
|
| 630 |
ASSERT_SUCCESS(aws_mqtt5_client_stop(test_context.client, NULL, NULL)); |
| 631 |
aws_wait_for_stopped_lifecycle_event(&test_context); |
| 632 |
|
| 633 |
|
| 634 |
struct aws_mqtt5_manual_publish_acknowledgement_completion_options completion_options = { |
| 635 |
.completion_callback = s_manual_pub_ack_completion_fn, |
| 636 |
.completion_user_data = &puback_context, |
| 637 |
}; |
| 638 |
|
| 639 |
ASSERT_SUCCESS( |
| 640 |
aws_mqtt5_client_invoke_publish_acknowledgement(test_context.client, control_id, &completion_options)); |
| 641 |
s_wait_for_manual_pub_ack_callback(&puback_context); |
| 642 |
|
| 643 |
|
| 644 |
aws_mutex_lock(&test_context.lock); |
| 645 |
ASSERT_INT_EQUALS(1, puback_context.puback_result_count); |
| 646 |
ASSERT_INT_EQUALS(AWS_MQTT5_MPAR_PUBACK_CANCELLED, puback_context.puback_results[0]); |
| 647 |
aws_mutex_unlock(&test_context.lock); |
| 648 |
|
| 649 |
aws_mqtt5_listener_release(listener); |
| 650 |
aws_mqtt5_client_mock_test_fixture_clean_up(&test_context); |
| 651 |
aws_mqtt_library_clean_up(); |
| 652 |
|
| 653 |
return AWS_OP_SUCCESS; |
| 654 |
} |
| 655 |
|
| 656 |
AWS_TEST_CASE( |
| 657 |
mqtt5_client_manual_pub_ack_disconnect_cancellation, |
| 658 |
s_mqtt5_client_manual_pub_ack_disconnect_cancellation_fn) |