| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "mqtt311_testing_utils.h" |
| 7 |
|
| 8 |
#include <aws/common/math.h> |
| 9 |
#include <aws/io/channel_bootstrap.h> |
| 10 |
#include <aws/io/event_loop.h> |
| 11 |
#include <aws/io/host_resolver.h> |
| 12 |
#include <aws/mqtt/private/client_impl.h> |
| 13 |
#include <aws/testing/aws_test_harness.h> |
| 14 |
|
| 15 |
#include "mqtt_mock_server_handler.h" |
| 16 |
|
| 17 |
static void s_on_incoming_channel_setup_fn( |
| 18 |
struct aws_server_bootstrap *bootstrap, |
| 19 |
int error_code, |
| 20 |
struct aws_channel *channel, |
| 21 |
void *user_data) { |
| 22 |
(void)bootstrap; |
| 23 |
struct mqtt_connection_state_test *state_test_data = user_data; |
| 24 |
|
| 25 |
state_test_data->error = error_code; |
| 26 |
|
| 27 |
if (!error_code) { |
| 28 |
aws_mutex_lock(&state_test_data->lock); |
| 29 |
state_test_data->server_disconnect_completed = false; |
| 30 |
aws_mutex_unlock(&state_test_data->lock); |
| 31 |
AWS_LOGF_DEBUG(TEST_LOG_SUBJECT, "server channel setup completed"); |
| 32 |
|
| 33 |
state_test_data->server_channel = channel; |
| 34 |
struct aws_channel_slot *test_handler_slot = aws_channel_slot_new(channel); |
| 35 |
aws_channel_slot_insert_end(channel, test_handler_slot); |
| 36 |
mqtt_mock_server_handler_update_slot(state_test_data->mock_server, test_handler_slot); |
| 37 |
aws_channel_slot_set_handler(test_handler_slot, state_test_data->mock_server); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
static void s_on_incoming_channel_shutdown_fn( |
| 42 |
struct aws_server_bootstrap *bootstrap, |
| 43 |
int error_code, |
| 44 |
struct aws_channel *channel, |
| 45 |
void *user_data) { |
| 46 |
(void)bootstrap; |
| 47 |
(void)error_code; |
| 48 |
(void)channel; |
| 49 |
struct mqtt_connection_state_test *state_test_data = user_data; |
| 50 |
aws_mutex_lock(&state_test_data->lock); |
| 51 |
state_test_data->server_disconnect_completed = true; |
| 52 |
AWS_LOGF_DEBUG(TEST_LOG_SUBJECT, "server channel shutdown completed"); |
| 53 |
aws_mutex_unlock(&state_test_data->lock); |
| 54 |
aws_condition_variable_notify_one(&state_test_data->cvar); |
| 55 |
} |
| 56 |
|
| 57 |
static void s_on_listener_destroy(struct aws_server_bootstrap *bootstrap, void *user_data) { |
| 58 |
(void)bootstrap; |
| 59 |
struct mqtt_connection_state_test *state_test_data = user_data; |
| 60 |
aws_mutex_lock(&state_test_data->lock); |
| 61 |
state_test_data->listener_destroyed = true; |
| 62 |
aws_mutex_unlock(&state_test_data->lock); |
| 63 |
aws_condition_variable_notify_one(&state_test_data->cvar); |
| 64 |
} |
| 65 |
|
| 66 |
static bool s_is_listener_destroyed(void *arg) { |
| 67 |
struct mqtt_connection_state_test *state_test_data = arg; |
| 68 |
return state_test_data->listener_destroyed; |
| 69 |
} |
| 70 |
|
| 71 |
static void s_wait_on_listener_cleanup(struct mqtt_connection_state_test *state_test_data) { |
| 72 |
aws_mutex_lock(&state_test_data->lock); |
| 73 |
aws_condition_variable_wait_pred( |
| 74 |
&state_test_data->cvar, &state_test_data->lock, s_is_listener_destroyed, state_test_data); |
| 75 |
aws_mutex_unlock(&state_test_data->lock); |
| 76 |
} |
| 77 |
|
| 78 |
static void s_on_connection_interrupted(struct aws_mqtt_client_connection *connection, int error_code, void *userdata) { |
| 79 |
(void)connection; |
| 80 |
(void)error_code; |
| 81 |
struct mqtt_connection_state_test *state_test_data = userdata; |
| 82 |
|
| 83 |
aws_mutex_lock(&state_test_data->lock); |
| 84 |
state_test_data->connection_interrupted = true; |
| 85 |
state_test_data->interruption_error = error_code; |
| 86 |
aws_mutex_unlock(&state_test_data->lock); |
| 87 |
AWS_LOGF_DEBUG(TEST_LOG_SUBJECT, "connection interrupted"); |
| 88 |
aws_condition_variable_notify_one(&state_test_data->cvar); |
| 89 |
} |
| 90 |
|
| 91 |
static bool s_is_connection_interrupted(void *arg) { |
| 92 |
struct mqtt_connection_state_test *state_test_data = arg; |
| 93 |
return state_test_data->connection_interrupted; |
| 94 |
} |
| 95 |
|
| 96 |
void aws_test311_wait_for_interrupt_to_complete(struct mqtt_connection_state_test *state_test_data) { |
| 97 |
aws_mutex_lock(&state_test_data->lock); |
| 98 |
aws_condition_variable_wait_pred( |
| 99 |
&state_test_data->cvar, &state_test_data->lock, s_is_connection_interrupted, state_test_data); |
| 100 |
state_test_data->connection_interrupted = false; |
| 101 |
aws_mutex_unlock(&state_test_data->lock); |
| 102 |
} |
| 103 |
|
| 104 |
static void s_on_connection_resumed( |
| 105 |
struct aws_mqtt_client_connection *connection, |
| 106 |
enum aws_mqtt_connect_return_code return_code, |
| 107 |
bool session_present, |
| 108 |
void *userdata) { |
| 109 |
(void)connection; |
| 110 |
(void)return_code; |
| 111 |
(void)session_present; |
| 112 |
AWS_LOGF_DEBUG(TEST_LOG_SUBJECT, "reconnect completed"); |
| 113 |
|
| 114 |
struct mqtt_connection_state_test *state_test_data = userdata; |
| 115 |
|
| 116 |
aws_mutex_lock(&state_test_data->lock); |
| 117 |
state_test_data->connection_resumed = true; |
| 118 |
aws_mutex_unlock(&state_test_data->lock); |
| 119 |
aws_condition_variable_notify_one(&state_test_data->cvar); |
| 120 |
} |
| 121 |
|
| 122 |
static bool s_is_connection_resumed(void *arg) { |
| 123 |
struct mqtt_connection_state_test *state_test_data = arg; |
| 124 |
return state_test_data->connection_resumed; |
| 125 |
} |
| 126 |
|
| 127 |
void aws_test311_wait_for_reconnect_to_complete(struct mqtt_connection_state_test *state_test_data) { |
| 128 |
aws_mutex_lock(&state_test_data->lock); |
| 129 |
aws_condition_variable_wait_pred( |
| 130 |
&state_test_data->cvar, &state_test_data->lock, s_is_connection_resumed, state_test_data); |
| 131 |
state_test_data->connection_resumed = false; |
| 132 |
aws_mutex_unlock(&state_test_data->lock); |
| 133 |
} |
| 134 |
|
| 135 |
static void s_on_connection_success( |
| 136 |
struct aws_mqtt_client_connection *connection, |
| 137 |
enum aws_mqtt_connect_return_code return_code, |
| 138 |
bool session_present, |
| 139 |
void *userdata) { |
| 140 |
(void)connection; |
| 141 |
struct mqtt_connection_state_test *state_test_data = userdata; |
| 142 |
aws_mutex_lock(&state_test_data->lock); |
| 143 |
|
| 144 |
state_test_data->session_present = session_present; |
| 145 |
state_test_data->mqtt_return_code = return_code; |
| 146 |
state_test_data->connection_success = true; |
| 147 |
aws_mutex_unlock(&state_test_data->lock); |
| 148 |
|
| 149 |
aws_condition_variable_notify_one(&state_test_data->cvar); |
| 150 |
} |
| 151 |
|
| 152 |
static void s_on_connection_failure(struct aws_mqtt_client_connection *connection, int error_code, void *userdata) { |
| 153 |
(void)connection; |
| 154 |
struct mqtt_connection_state_test *state_test_data = userdata; |
| 155 |
aws_mutex_lock(&state_test_data->lock); |
| 156 |
|
| 157 |
state_test_data->error = error_code; |
| 158 |
state_test_data->connection_failure = true; |
| 159 |
aws_mutex_unlock(&state_test_data->lock); |
| 160 |
|
| 161 |
aws_condition_variable_notify_one(&state_test_data->cvar); |
| 162 |
} |
| 163 |
|
| 164 |
static bool s_is_connection_succeed(void *arg) { |
| 165 |
struct mqtt_connection_state_test *state_test_data = arg; |
| 166 |
return state_test_data->connection_success; |
| 167 |
} |
| 168 |
|
| 169 |
static bool s_is_connection_failed(void *arg) { |
| 170 |
struct mqtt_connection_state_test *state_test_data = arg; |
| 171 |
return state_test_data->connection_failure; |
| 172 |
} |
| 173 |
|
| 174 |
void aws_test311_wait_for_connection_to_succeed(struct mqtt_connection_state_test *state_test_data) { |
| 175 |
aws_mutex_lock(&state_test_data->lock); |
| 176 |
aws_condition_variable_wait_pred( |
| 177 |
&state_test_data->cvar, &state_test_data->lock, s_is_connection_succeed, state_test_data); |
| 178 |
state_test_data->connection_success = false; |
| 179 |
aws_mutex_unlock(&state_test_data->lock); |
| 180 |
} |
| 181 |
|
| 182 |
void aws_test311_wait_for_connection_to_fail(struct mqtt_connection_state_test *state_test_data) { |
| 183 |
aws_mutex_lock(&state_test_data->lock); |
| 184 |
aws_condition_variable_wait_pred( |
| 185 |
&state_test_data->cvar, &state_test_data->lock, s_is_connection_failed, state_test_data); |
| 186 |
state_test_data->connection_failure = false; |
| 187 |
aws_mutex_unlock(&state_test_data->lock); |
| 188 |
} |
| 189 |
|
| 190 |
static bool s_is_termination_completed(void *arg) { |
| 191 |
struct mqtt_connection_state_test *state_test_data = arg; |
| 192 |
return state_test_data->connection_terminated; |
| 193 |
} |
| 194 |
|
| 195 |
static void s_wait_for_termination_to_complete(struct mqtt_connection_state_test *state_test_data) { |
| 196 |
aws_mutex_lock(&state_test_data->lock); |
| 197 |
aws_condition_variable_wait_pred( |
| 198 |
&state_test_data->cvar, &state_test_data->lock, s_is_termination_completed, state_test_data); |
| 199 |
state_test_data->connection_terminated = false; |
| 200 |
aws_mutex_unlock(&state_test_data->lock); |
| 201 |
} |
| 202 |
|
| 203 |
void aws_test311_on_connection_termination_fn(void *userdata) { |
| 204 |
struct mqtt_connection_state_test *state_test_data = (struct mqtt_connection_state_test *)userdata; |
| 205 |
|
| 206 |
aws_mutex_lock(&state_test_data->lock); |
| 207 |
state_test_data->connection_termination_calls += 1; |
| 208 |
state_test_data->connection_terminated = true; |
| 209 |
aws_mutex_unlock(&state_test_data->lock); |
| 210 |
aws_condition_variable_notify_one(&state_test_data->cvar); |
| 211 |
} |
| 212 |
|
| 213 |
static void s_on_any_publish_received( |
| 214 |
struct aws_mqtt_client_connection *connection, |
| 215 |
const struct aws_byte_cursor *topic, |
| 216 |
const struct aws_byte_cursor *payload, |
| 217 |
bool dup, |
| 218 |
enum aws_mqtt_qos qos, |
| 219 |
bool retain, |
| 220 |
void *userdata) { |
| 221 |
(void)connection; |
| 222 |
struct mqtt_connection_state_test *state_test_data = userdata; |
| 223 |
|
| 224 |
struct aws_byte_buf payload_cp; |
| 225 |
aws_byte_buf_init_copy_from_cursor(&payload_cp, state_test_data->allocator, *payload); |
| 226 |
struct aws_byte_buf topic_cp; |
| 227 |
aws_byte_buf_init_copy_from_cursor(&topic_cp, state_test_data->allocator, *topic); |
| 228 |
struct received_publish_packet received_packet = { |
| 229 |
.payload = payload_cp, |
| 230 |
.topic = topic_cp, |
| 231 |
.dup = dup, |
| 232 |
.qos = qos, |
| 233 |
.retain = retain, |
| 234 |
}; |
| 235 |
|
| 236 |
aws_mutex_lock(&state_test_data->lock); |
| 237 |
aws_array_list_push_back(&state_test_data->any_published_messages, &received_packet); |
| 238 |
state_test_data->any_publishes_received++; |
| 239 |
aws_mutex_unlock(&state_test_data->lock); |
| 240 |
aws_condition_variable_notify_one(&state_test_data->cvar); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
int aws_test311_setup_mqtt_server_fn(struct aws_allocator *allocator, void *ctx) { |
| 248 |
aws_mqtt_library_init(allocator); |
| 249 |
|
| 250 |
struct mqtt_connection_state_test *state_test_data = ctx; |
| 251 |
|
| 252 |
AWS_ZERO_STRUCT(*state_test_data); |
| 253 |
|
| 254 |
state_test_data->allocator = allocator; |
| 255 |
state_test_data->el_group = aws_event_loop_group_new_default(allocator, 1, NULL); |
| 256 |
|
| 257 |
state_test_data->mock_server = new_mqtt_mock_server(allocator); |
| 258 |
ASSERT_NOT_NULL(state_test_data->mock_server); |
| 259 |
|
| 260 |
state_test_data->server_bootstrap = aws_server_bootstrap_new(allocator, state_test_data->el_group); |
| 261 |
ASSERT_NOT_NULL(state_test_data->server_bootstrap); |
| 262 |
|
| 263 |
struct aws_socket_options socket_options = { |
| 264 |
.connect_timeout_ms = 100, |
| 265 |
.domain = AWS_SOCKET_LOCAL, |
| 266 |
}; |
| 267 |
|
| 268 |
state_test_data->socket_options = socket_options; |
| 269 |
ASSERT_SUCCESS(aws_condition_variable_init(&state_test_data->cvar)); |
| 270 |
ASSERT_SUCCESS(aws_mutex_init(&state_test_data->lock)); |
| 271 |
|
| 272 |
aws_socket_endpoint_init_local_address_for_test(&state_test_data->endpoint); |
| 273 |
|
| 274 |
struct aws_server_socket_channel_bootstrap_options server_bootstrap_options = { |
| 275 |
.bootstrap = state_test_data->server_bootstrap, |
| 276 |
.host_name = state_test_data->endpoint.address, |
| 277 |
.port = state_test_data->endpoint.port, |
| 278 |
.socket_options = &state_test_data->socket_options, |
| 279 |
.incoming_callback = s_on_incoming_channel_setup_fn, |
| 280 |
.shutdown_callback = s_on_incoming_channel_shutdown_fn, |
| 281 |
.destroy_callback = s_on_listener_destroy, |
| 282 |
.user_data = state_test_data, |
| 283 |
}; |
| 284 |
state_test_data->listener = aws_server_bootstrap_new_socket_listener(&server_bootstrap_options); |
| 285 |
|
| 286 |
ASSERT_NOT_NULL(state_test_data->listener); |
| 287 |
|
| 288 |
struct aws_host_resolver_default_options resolver_options = { |
| 289 |
.el_group = state_test_data->el_group, |
| 290 |
.max_entries = 1, |
| 291 |
}; |
| 292 |
state_test_data->host_resolver = aws_host_resolver_new_default(allocator, &resolver_options); |
| 293 |
|
| 294 |
struct aws_client_bootstrap_options bootstrap_options = { |
| 295 |
.event_loop_group = state_test_data->el_group, |
| 296 |
.user_data = state_test_data, |
| 297 |
.host_resolver = state_test_data->host_resolver, |
| 298 |
}; |
| 299 |
|
| 300 |
state_test_data->client_bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options); |
| 301 |
|
| 302 |
state_test_data->mqtt_client = aws_mqtt_client_new(allocator, state_test_data->client_bootstrap); |
| 303 |
state_test_data->mqtt_connection = aws_mqtt_client_connection_new(state_test_data->mqtt_client); |
| 304 |
ASSERT_NOT_NULL(state_test_data->mqtt_connection); |
| 305 |
|
| 306 |
ASSERT_SUCCESS(aws_mqtt_client_connection_set_connection_interruption_handlers( |
| 307 |
state_test_data->mqtt_connection, |
| 308 |
s_on_connection_interrupted, |
| 309 |
state_test_data, |
| 310 |
s_on_connection_resumed, |
| 311 |
state_test_data)); |
| 312 |
|
| 313 |
ASSERT_SUCCESS(aws_mqtt_client_connection_set_connection_result_handlers( |
| 314 |
state_test_data->mqtt_connection, |
| 315 |
s_on_connection_success, |
| 316 |
state_test_data, |
| 317 |
s_on_connection_failure, |
| 318 |
state_test_data)); |
| 319 |
|
| 320 |
ASSERT_SUCCESS(aws_mqtt_client_connection_set_on_any_publish_handler( |
| 321 |
state_test_data->mqtt_connection, s_on_any_publish_received, state_test_data)); |
| 322 |
|
| 323 |
ASSERT_SUCCESS(aws_array_list_init_dynamic( |
| 324 |
&state_test_data->published_messages, allocator, 4, sizeof(struct received_publish_packet))); |
| 325 |
ASSERT_SUCCESS(aws_array_list_init_dynamic( |
| 326 |
&state_test_data->any_published_messages, allocator, 4, sizeof(struct received_publish_packet))); |
| 327 |
ASSERT_SUCCESS(aws_array_list_init_dynamic(&state_test_data->qos_returned, allocator, 2, sizeof(uint8_t))); |
| 328 |
|
| 329 |
ASSERT_SUCCESS(aws_mqtt_client_connection_set_connection_termination_handler( |
| 330 |
state_test_data->mqtt_connection, aws_test311_on_connection_termination_fn, state_test_data)); |
| 331 |
|
| 332 |
return AWS_OP_SUCCESS; |
| 333 |
} |
| 334 |
|
| 335 |
static void s_received_publish_packet_list_clean_up(struct aws_array_list *list) { |
| 336 |
for (size_t i = 0; i < aws_array_list_length(list); ++i) { |
| 337 |
struct received_publish_packet *val_ptr = NULL; |
| 338 |
aws_array_list_get_at_ptr(list, (void **)&val_ptr, i); |
| 339 |
aws_byte_buf_clean_up(&val_ptr->payload); |
| 340 |
aws_byte_buf_clean_up(&val_ptr->topic); |
| 341 |
} |
| 342 |
aws_array_list_clean_up(list); |
| 343 |
} |
| 344 |
|
| 345 |
int aws_test311_clean_up_mqtt_server_fn(struct aws_allocator *allocator, int setup_result, void *ctx) { |
| 346 |
(void)allocator; |
| 347 |
|
| 348 |
if (!setup_result) { |
| 349 |
struct mqtt_connection_state_test *state_test_data = ctx; |
| 350 |
|
| 351 |
s_received_publish_packet_list_clean_up(&state_test_data->published_messages); |
| 352 |
s_received_publish_packet_list_clean_up(&state_test_data->any_published_messages); |
| 353 |
aws_array_list_clean_up(&state_test_data->qos_returned); |
| 354 |
aws_mqtt_client_connection_release(state_test_data->mqtt_connection); |
| 355 |
|
| 356 |
s_wait_for_termination_to_complete(state_test_data); |
| 357 |
ASSERT_UINT_EQUALS(1, state_test_data->connection_termination_calls); |
| 358 |
|
| 359 |
aws_mqtt_client_release(state_test_data->mqtt_client); |
| 360 |
aws_client_bootstrap_release(state_test_data->client_bootstrap); |
| 361 |
aws_host_resolver_release(state_test_data->host_resolver); |
| 362 |
aws_server_bootstrap_destroy_socket_listener(state_test_data->server_bootstrap, state_test_data->listener); |
| 363 |
s_wait_on_listener_cleanup(state_test_data); |
| 364 |
aws_server_bootstrap_release(state_test_data->server_bootstrap); |
| 365 |
aws_event_loop_group_release(state_test_data->el_group); |
| 366 |
destroy_mqtt_mock_server(state_test_data->mock_server); |
| 367 |
} |
| 368 |
|
| 369 |
aws_mqtt_library_clean_up(); |
| 370 |
return AWS_OP_SUCCESS; |
| 371 |
} |
| 372 |
|
| 373 |
void aws_test311_on_connection_complete_fn( |
| 374 |
struct aws_mqtt_client_connection *connection, |
| 375 |
int error_code, |
| 376 |
enum aws_mqtt_connect_return_code return_code, |
| 377 |
bool session_present, |
| 378 |
void *userdata) { |
| 379 |
(void)connection; |
| 380 |
struct mqtt_connection_state_test *state_test_data = userdata; |
| 381 |
aws_mutex_lock(&state_test_data->lock); |
| 382 |
|
| 383 |
state_test_data->session_present = session_present; |
| 384 |
state_test_data->mqtt_return_code = return_code; |
| 385 |
state_test_data->error = error_code; |
| 386 |
state_test_data->connection_completed = true; |
| 387 |
aws_mutex_unlock(&state_test_data->lock); |
| 388 |
|
| 389 |
aws_condition_variable_notify_one(&state_test_data->cvar); |
| 390 |
} |
| 391 |
|
| 392 |
static bool s_is_connection_completed(void *arg) { |
| 393 |
struct mqtt_connection_state_test *state_test_data = arg; |
| 394 |
return state_test_data->connection_completed; |
| 395 |
} |
| 396 |
|
| 397 |
void aws_test311_wait_for_connection_to_complete(struct mqtt_connection_state_test *state_test_data) { |
| 398 |
aws_mutex_lock(&state_test_data->lock); |
| 399 |
aws_condition_variable_wait_pred( |
| 400 |
&state_test_data->cvar, &state_test_data->lock, s_is_connection_completed, state_test_data); |
| 401 |
state_test_data->connection_completed = false; |
| 402 |
aws_mutex_unlock(&state_test_data->lock); |
| 403 |
} |
| 404 |
|
| 405 |
void aws_test311_on_disconnect_fn(struct aws_mqtt_client_connection *connection, void *userdata) { |
| 406 |
(void)connection; |
| 407 |
struct mqtt_connection_state_test *state_test_data = userdata; |
| 408 |
AWS_LOGF_DEBUG(TEST_LOG_SUBJECT, "disconnect completed"); |
| 409 |
aws_mutex_lock(&state_test_data->lock); |
| 410 |
state_test_data->client_disconnect_completed = true; |
| 411 |
aws_mutex_unlock(&state_test_data->lock); |
| 412 |
|
| 413 |
aws_condition_variable_notify_one(&state_test_data->cvar); |
| 414 |
} |
| 415 |
|
| 416 |
static bool s_is_disconnect_completed(void *arg) { |
| 417 |
struct mqtt_connection_state_test *state_test_data = arg; |
| 418 |
return state_test_data->client_disconnect_completed && state_test_data->server_disconnect_completed; |
| 419 |
} |
| 420 |
|
| 421 |
void aws_test311_wait_for_disconnect_to_complete(struct mqtt_connection_state_test *state_test_data) { |
| 422 |
aws_mutex_lock(&state_test_data->lock); |
| 423 |
aws_condition_variable_wait_pred( |
| 424 |
&state_test_data->cvar, &state_test_data->lock, s_is_disconnect_completed, state_test_data); |
| 425 |
state_test_data->client_disconnect_completed = false; |
| 426 |
state_test_data->server_disconnect_completed = false; |
| 427 |
aws_mutex_unlock(&state_test_data->lock); |
| 428 |
} |
| 429 |
|
| 430 |
static bool s_is_any_publish_received(void *arg) { |
| 431 |
struct mqtt_connection_state_test *state_test_data = arg; |
| 432 |
return state_test_data->any_publishes_received == state_test_data->expected_any_publishes; |
| 433 |
} |
| 434 |
|
| 435 |
void aws_test311_wait_for_any_publish(struct mqtt_connection_state_test *state_test_data) { |
| 436 |
aws_mutex_lock(&state_test_data->lock); |
| 437 |
aws_condition_variable_wait_pred( |
| 438 |
&state_test_data->cvar, &state_test_data->lock, s_is_any_publish_received, state_test_data); |
| 439 |
state_test_data->any_publishes_received = 0; |
| 440 |
state_test_data->expected_any_publishes = 0; |
| 441 |
aws_mutex_unlock(&state_test_data->lock); |
| 442 |
} |
| 443 |
|
| 444 |
void aws_test311_on_publish_received( |
| 445 |
struct aws_mqtt_client_connection *connection, |
| 446 |
const struct aws_byte_cursor *topic, |
| 447 |
const struct aws_byte_cursor *payload, |
| 448 |
bool dup, |
| 449 |
enum aws_mqtt_qos qos, |
| 450 |
bool retain, |
| 451 |
void *userdata) { |
| 452 |
|
| 453 |
(void)connection; |
| 454 |
(void)topic; |
| 455 |
struct mqtt_connection_state_test *state_test_data = userdata; |
| 456 |
|
| 457 |
struct aws_byte_buf payload_cp; |
| 458 |
aws_byte_buf_init_copy_from_cursor(&payload_cp, state_test_data->allocator, *payload); |
| 459 |
struct aws_byte_buf topic_cp; |
| 460 |
aws_byte_buf_init_copy_from_cursor(&topic_cp, state_test_data->allocator, *topic); |
| 461 |
struct received_publish_packet received_packet = { |
| 462 |
.payload = payload_cp, |
| 463 |
.topic = topic_cp, |
| 464 |
.dup = dup, |
| 465 |
.qos = qos, |
| 466 |
.retain = retain, |
| 467 |
}; |
| 468 |
|
| 469 |
aws_mutex_lock(&state_test_data->lock); |
| 470 |
aws_array_list_push_back(&state_test_data->published_messages, &received_packet); |
| 471 |
state_test_data->publishes_received++; |
| 472 |
aws_mutex_unlock(&state_test_data->lock); |
| 473 |
aws_condition_variable_notify_one(&state_test_data->cvar); |
| 474 |
} |
| 475 |
|
| 476 |
static bool s_is_publish_received(void *arg) { |
| 477 |
struct mqtt_connection_state_test *state_test_data = arg; |
| 478 |
return state_test_data->publishes_received == state_test_data->expected_publishes; |
| 479 |
} |
| 480 |
|
| 481 |
void aws_test311_wait_for_publish(struct mqtt_connection_state_test *state_test_data) { |
| 482 |
aws_mutex_lock(&state_test_data->lock); |
| 483 |
aws_condition_variable_wait_pred( |
| 484 |
&state_test_data->cvar, &state_test_data->lock, s_is_publish_received, state_test_data); |
| 485 |
state_test_data->publishes_received = 0; |
| 486 |
state_test_data->expected_publishes = 0; |
| 487 |
aws_mutex_unlock(&state_test_data->lock); |
| 488 |
} |
| 489 |
|
| 490 |
void aws_test311_on_suback( |
| 491 |
struct aws_mqtt_client_connection *connection, |
| 492 |
uint16_t packet_id, |
| 493 |
const struct aws_byte_cursor *topic, |
| 494 |
enum aws_mqtt_qos qos, |
| 495 |
int error_code, |
| 496 |
void *userdata) { |
| 497 |
(void)connection; |
| 498 |
(void)packet_id; |
| 499 |
(void)topic; |
| 500 |
|
| 501 |
struct mqtt_connection_state_test *state_test_data = userdata; |
| 502 |
|
| 503 |
aws_mutex_lock(&state_test_data->lock); |
| 504 |
if (!error_code) { |
| 505 |
aws_array_list_push_back(&state_test_data->qos_returned, &qos); |
| 506 |
} |
| 507 |
state_test_data->subscribe_completed = true; |
| 508 |
state_test_data->subscribe_complete_error = error_code; |
| 509 |
aws_mutex_unlock(&state_test_data->lock); |
| 510 |
aws_condition_variable_notify_one(&state_test_data->cvar); |
| 511 |
} |
| 512 |
|
| 513 |
static bool s_is_subscribe_completed(void *arg) { |
| 514 |
struct mqtt_connection_state_test *state_test_data = arg; |
| 515 |
return state_test_data->subscribe_completed; |
| 516 |
} |
| 517 |
|
| 518 |
void aws_test311_wait_for_subscribe_to_complete(struct mqtt_connection_state_test *state_test_data) { |
| 519 |
aws_mutex_lock(&state_test_data->lock); |
| 520 |
aws_condition_variable_wait_pred( |
| 521 |
&state_test_data->cvar, &state_test_data->lock, s_is_subscribe_completed, state_test_data); |
| 522 |
state_test_data->subscribe_completed = false; |
| 523 |
aws_mutex_unlock(&state_test_data->lock); |
| 524 |
} |
| 525 |
|
| 526 |
void aws_test311_on_multi_suback( |
| 527 |
struct aws_mqtt_client_connection *connection, |
| 528 |
uint16_t packet_id, |
| 529 |
const struct aws_array_list *topic_subacks, |
| 530 |
int error_code, |
| 531 |
void *userdata) { |
| 532 |
(void)connection; |
| 533 |
(void)packet_id; |
| 534 |
(void)topic_subacks; |
| 535 |
(void)error_code; |
| 536 |
|
| 537 |
struct mqtt_connection_state_test *state_test_data = userdata; |
| 538 |
|
| 539 |
aws_mutex_lock(&state_test_data->lock); |
| 540 |
state_test_data->subscribe_completed = true; |
| 541 |
state_test_data->subscribe_complete_error = error_code; |
| 542 |
|
| 543 |
if (!error_code) { |
| 544 |
size_t length = aws_array_list_length(topic_subacks); |
| 545 |
for (size_t i = 0; i < length; ++i) { |
| 546 |
struct aws_mqtt_topic_subscription *subscription = NULL; |
| 547 |
aws_array_list_get_at(topic_subacks, &subscription, i); |
| 548 |
aws_array_list_push_back(&state_test_data->qos_returned, &subscription->qos); |
| 549 |
} |
| 550 |
} |
| 551 |
aws_mutex_unlock(&state_test_data->lock); |
| 552 |
aws_condition_variable_notify_one(&state_test_data->cvar); |
| 553 |
} |
| 554 |
|
| 555 |
void aws_test311_on_op_complete( |
| 556 |
struct aws_mqtt_client_connection *connection, |
| 557 |
uint16_t packet_id, |
| 558 |
int error_code, |
| 559 |
void *userdata) { |
| 560 |
(void)connection; |
| 561 |
(void)packet_id; |
| 562 |
|
| 563 |
struct mqtt_connection_state_test *state_test_data = userdata; |
| 564 |
AWS_LOGF_DEBUG(TEST_LOG_SUBJECT, "pub op completed"); |
| 565 |
aws_mutex_lock(&state_test_data->lock); |
| 566 |
state_test_data->ops_completed++; |
| 567 |
state_test_data->op_complete_error = error_code; |
| 568 |
aws_mutex_unlock(&state_test_data->lock); |
| 569 |
aws_condition_variable_notify_one(&state_test_data->cvar); |
| 570 |
} |
| 571 |
|
| 572 |
static bool s_is_ops_completed(void *arg) { |
| 573 |
struct mqtt_connection_state_test *state_test_data = arg; |
| 574 |
return state_test_data->ops_completed == state_test_data->expected_ops_completed; |
| 575 |
} |
| 576 |
|
| 577 |
void aws_test311_wait_for_ops_completed(struct mqtt_connection_state_test *state_test_data) { |
| 578 |
aws_mutex_lock(&state_test_data->lock); |
| 579 |
aws_condition_variable_wait_for_pred( |
| 580 |
&state_test_data->cvar, &state_test_data->lock, 10000000000, s_is_ops_completed, state_test_data); |
| 581 |
aws_mutex_unlock(&state_test_data->lock); |
| 582 |
} |
| 583 |
|
| 584 |
static const struct aws_byte_cursor SDK_ATT_STR = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("?SDK="); |
| 585 |
static const struct aws_byte_cursor PLATFORM_ATT_STR = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("&Platform="); |
| 586 |
static const struct aws_byte_cursor METADATA_ATT_STR = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("&Metadata=("); |
| 587 |
static const struct aws_byte_cursor METADATA_CLOSE_PAREN = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(")"); |
| 588 |
static const struct aws_byte_cursor METADATA_KEY_VALUE_DELIM = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("="); |
| 589 |
static const struct aws_byte_cursor METADATA_ENTRY_DELIM = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(";"); |
| 590 |
|
| 591 |
void aws_test_mqtt_build_expected_metrics( |
| 592 |
struct aws_allocator *allocator, |
| 593 |
const struct aws_byte_cursor *original_username, |
| 594 |
const struct aws_byte_cursor sdk, |
| 595 |
const struct aws_byte_cursor *platform, |
| 596 |
const struct aws_mqtt_metadata_entry *metadata_entries, |
| 597 |
size_t metadata_count, |
| 598 |
struct aws_byte_buf *expected_buf) { |
| 599 |
|
| 600 |
struct aws_byte_cursor platform_to_use = platform ? *platform : aws_get_platform_build_os_string(); |
| 601 |
if (original_username) { |
| 602 |
aws_byte_buf_init_copy_from_cursor(expected_buf, allocator, *original_username); |
| 603 |
} else { |
| 604 |
aws_byte_buf_init(expected_buf, allocator, 0); |
| 605 |
} |
| 606 |
aws_byte_buf_append_dynamic(expected_buf, &SDK_ATT_STR); |
| 607 |
aws_byte_buf_append_dynamic(expected_buf, &sdk); |
| 608 |
aws_byte_buf_append_dynamic(expected_buf, &PLATFORM_ATT_STR); |
| 609 |
aws_byte_buf_append_dynamic(expected_buf, &platform_to_use); |
| 610 |
|
| 611 |
|
| 612 |
if (metadata_entries != NULL && metadata_count > 0) { |
| 613 |
aws_byte_buf_append_dynamic(expected_buf, &METADATA_ATT_STR); |
| 614 |
|
| 615 |
for (size_t i = 0; i < metadata_count; ++i) { |
| 616 |
const struct aws_mqtt_metadata_entry *entry = &metadata_entries[i]; |
| 617 |
aws_byte_buf_append_dynamic(expected_buf, &entry->key); |
| 618 |
aws_byte_buf_append_dynamic(expected_buf, &METADATA_KEY_VALUE_DELIM); |
| 619 |
aws_byte_buf_append_dynamic(expected_buf, &entry->value); |
| 620 |
|
| 621 |
|
| 622 |
if (i < metadata_count - 1) { |
| 623 |
aws_byte_buf_append_dynamic(expected_buf, &METADATA_ENTRY_DELIM); |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
aws_byte_buf_append_dynamic(expected_buf, &METADATA_CLOSE_PAREN); |
| 628 |
} |
| 629 |
} |