| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/mqtt/private/client_impl.h> |
| 7 |
|
| 8 |
#include <aws/mqtt/mqtt.h> |
| 9 |
#include <aws/mqtt/private/packets.h> |
| 10 |
|
| 11 |
#include <aws/io/channel.h> |
| 12 |
#include <aws/io/channel_bootstrap.h> |
| 13 |
#include <aws/io/event_loop.h> |
| 14 |
#include <aws/io/socket.h> |
| 15 |
#include <aws/io/socket_channel_handler.h> |
| 16 |
|
| 17 |
#include <aws/common/condition_variable.h> |
| 18 |
#include <aws/common/device_random.h> |
| 19 |
#include <aws/common/mutex.h> |
| 20 |
#include <aws/common/string.h> |
| 21 |
#include <aws/common/thread.h> |
| 22 |
|
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#ifdef WIN32 |
| 26 |
# include <windows.h> |
| 27 |
# define sleep Sleep |
| 28 |
#else |
| 29 |
# include <unistd.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
static struct aws_byte_cursor s_client_id = { |
| 35 |
.ptr = (uint8_t *)"MyClientId1", |
| 36 |
.len = 11, |
| 37 |
}; |
| 38 |
AWS_STATIC_STRING_FROM_LITERAL(s_subscribe_topic, "a/b"); |
| 39 |
AWS_STATIC_STRING_FROM_LITERAL(s_hostname, "localhost"); |
| 40 |
|
| 41 |
enum { PAYLOAD_LEN = 20000 }; |
| 42 |
static uint8_t s_payload[PAYLOAD_LEN]; |
| 43 |
|
| 44 |
struct test_context { |
| 45 |
struct aws_allocator *allocator; |
| 46 |
|
| 47 |
struct aws_mutex lock; |
| 48 |
struct aws_condition_variable signal; |
| 49 |
struct aws_event_loop_group *el_group; |
| 50 |
struct aws_host_resolver *resolver; |
| 51 |
struct aws_client_bootstrap *bootstrap; |
| 52 |
struct aws_mqtt_client *client; |
| 53 |
struct aws_mqtt_client_connection *connection; |
| 54 |
bool retained_packet_received; |
| 55 |
bool on_any_publish_fired; |
| 56 |
bool connection_complete; |
| 57 |
bool received_pub_ack; |
| 58 |
bool received_unsub_ack; |
| 59 |
bool on_disconnect_received; |
| 60 |
}; |
| 61 |
|
| 62 |
static void s_on_packet_received( |
| 63 |
struct aws_mqtt_client_connection *connection, |
| 64 |
const struct aws_byte_cursor *topic, |
| 65 |
const struct aws_byte_cursor *payload, |
| 66 |
bool dup, |
| 67 |
enum aws_mqtt_qos qos, |
| 68 |
bool retain, |
| 69 |
void *user_data) { |
| 70 |
|
| 71 |
(void)connection; |
| 72 |
(void)topic; |
| 73 |
(void)dup; |
| 74 |
(void)qos; |
| 75 |
(void)retain; |
| 76 |
|
| 77 |
struct aws_byte_cursor expected_payload = { |
| 78 |
.ptr = s_payload, |
| 79 |
.len = PAYLOAD_LEN, |
| 80 |
}; |
| 81 |
(void)expected_payload; |
| 82 |
AWS_FATAL_ASSERT(aws_byte_cursor_eq(payload, &expected_payload)); |
| 83 |
|
| 84 |
printf("2 started\n"); |
| 85 |
|
| 86 |
struct test_context *tester = user_data; |
| 87 |
|
| 88 |
aws_mutex_lock(&tester->lock); |
| 89 |
tester->retained_packet_received = true; |
| 90 |
aws_mutex_unlock(&tester->lock); |
| 91 |
aws_condition_variable_notify_one(&tester->signal); |
| 92 |
} |
| 93 |
|
| 94 |
static void s_on_any_packet_received( |
| 95 |
struct aws_mqtt_client_connection *connection, |
| 96 |
const struct aws_byte_cursor *topic, |
| 97 |
const struct aws_byte_cursor *payload, |
| 98 |
bool dup, |
| 99 |
enum aws_mqtt_qos qos, |
| 100 |
bool retain, |
| 101 |
void *user_data) { |
| 102 |
|
| 103 |
(void)connection; |
| 104 |
(void)topic; |
| 105 |
(void)dup; |
| 106 |
(void)qos; |
| 107 |
(void)retain; |
| 108 |
|
| 109 |
struct aws_byte_cursor expected_payload = { |
| 110 |
.ptr = s_payload, |
| 111 |
.len = PAYLOAD_LEN, |
| 112 |
}; |
| 113 |
(void)expected_payload; |
| 114 |
AWS_FATAL_ASSERT(aws_byte_cursor_eq(payload, &expected_payload)); |
| 115 |
|
| 116 |
struct test_context *tester = user_data; |
| 117 |
|
| 118 |
aws_mutex_lock(&tester->lock); |
| 119 |
tester->on_any_publish_fired = true; |
| 120 |
aws_mutex_unlock(&tester->lock); |
| 121 |
aws_condition_variable_notify_one(&tester->signal); |
| 122 |
} |
| 123 |
|
| 124 |
static void s_mqtt_on_puback( |
| 125 |
struct aws_mqtt_client_connection *connection, |
| 126 |
uint16_t packet_id, |
| 127 |
int error_code, |
| 128 |
void *userdata) { |
| 129 |
|
| 130 |
(void)connection; |
| 131 |
(void)packet_id; |
| 132 |
(void)error_code; |
| 133 |
|
| 134 |
AWS_FATAL_ASSERT(error_code == AWS_OP_SUCCESS); |
| 135 |
|
| 136 |
struct test_context *tester = userdata; |
| 137 |
|
| 138 |
printf("2 started\n"); |
| 139 |
|
| 140 |
aws_mutex_lock(&tester->lock); |
| 141 |
tester->received_pub_ack = true; |
| 142 |
aws_mutex_unlock(&tester->lock); |
| 143 |
aws_condition_variable_notify_one(&tester->signal); |
| 144 |
} |
| 145 |
|
| 146 |
static void s_mqtt_on_connection_complete( |
| 147 |
struct aws_mqtt_client_connection *connection, |
| 148 |
int error_code, |
| 149 |
enum aws_mqtt_connect_return_code return_code, |
| 150 |
bool session_present, |
| 151 |
void *user_data) { |
| 152 |
|
| 153 |
(void)connection; |
| 154 |
(void)error_code; |
| 155 |
(void)return_code; |
| 156 |
(void)session_present; |
| 157 |
|
| 158 |
AWS_FATAL_ASSERT(error_code == AWS_OP_SUCCESS); |
| 159 |
AWS_FATAL_ASSERT(return_code == AWS_MQTT_CONNECT_ACCEPTED); |
| 160 |
AWS_FATAL_ASSERT(session_present == false); |
| 161 |
|
| 162 |
struct test_context *tester = user_data; |
| 163 |
|
| 164 |
printf("1 started\n"); |
| 165 |
|
| 166 |
aws_mutex_lock(&tester->lock); |
| 167 |
tester->connection_complete = true; |
| 168 |
aws_mutex_unlock(&tester->lock); |
| 169 |
aws_condition_variable_notify_one(&tester->signal); |
| 170 |
} |
| 171 |
|
| 172 |
static void s_mqtt_on_interrupted(struct aws_mqtt_client_connection *connection, int error_code, void *userdata) { |
| 173 |
|
| 174 |
(void)connection; |
| 175 |
(void)error_code; |
| 176 |
(void)userdata; |
| 177 |
|
| 178 |
printf("Connection offline\n"); |
| 179 |
} |
| 180 |
|
| 181 |
static void s_mqtt_on_resumed( |
| 182 |
struct aws_mqtt_client_connection *connection, |
| 183 |
enum aws_mqtt_connect_return_code return_code, |
| 184 |
bool session_present, |
| 185 |
void *userdata) { |
| 186 |
|
| 187 |
(void)connection; |
| 188 |
(void)return_code; |
| 189 |
(void)session_present; |
| 190 |
(void)userdata; |
| 191 |
|
| 192 |
printf("Connection resumed\n"); |
| 193 |
} |
| 194 |
|
| 195 |
static void s_mqtt_on_unsuback( |
| 196 |
struct aws_mqtt_client_connection *connection, |
| 197 |
uint16_t packet_id, |
| 198 |
int error_code, |
| 199 |
void *userdata) { |
| 200 |
|
| 201 |
(void)connection; |
| 202 |
(void)packet_id; |
| 203 |
(void)error_code; |
| 204 |
|
| 205 |
AWS_FATAL_ASSERT(error_code == AWS_OP_SUCCESS); |
| 206 |
|
| 207 |
struct test_context *tester = userdata; |
| 208 |
|
| 209 |
printf("2 started\n"); |
| 210 |
|
| 211 |
aws_mutex_lock(&tester->lock); |
| 212 |
tester->received_unsub_ack = true; |
| 213 |
aws_mutex_unlock(&tester->lock); |
| 214 |
aws_condition_variable_notify_one(&tester->signal); |
| 215 |
} |
| 216 |
|
| 217 |
static void s_mqtt_on_disconnect(struct aws_mqtt_client_connection *connection, void *user_data) { |
| 218 |
|
| 219 |
(void)connection; |
| 220 |
|
| 221 |
struct test_context *tester = user_data; |
| 222 |
|
| 223 |
printf("3 started\n"); |
| 224 |
|
| 225 |
aws_mutex_lock(&tester->lock); |
| 226 |
tester->on_disconnect_received = true; |
| 227 |
aws_mutex_unlock(&tester->lock); |
| 228 |
aws_condition_variable_notify_one(&tester->signal); |
| 229 |
} |
| 230 |
|
| 231 |
static bool s_is_connection_complete_pred(void *user_data) { |
| 232 |
struct test_context *tester = user_data; |
| 233 |
return tester->connection_complete; |
| 234 |
} |
| 235 |
|
| 236 |
static bool s_received_pub_ack_pred(void *user_data) { |
| 237 |
struct test_context *tester = user_data; |
| 238 |
return tester->received_pub_ack; |
| 239 |
} |
| 240 |
|
| 241 |
static bool s_on_disconnect_received(void *user_data) { |
| 242 |
struct test_context *tester = user_data; |
| 243 |
return tester->on_disconnect_received; |
| 244 |
} |
| 245 |
|
| 246 |
static bool s_retained_and_any_publish_pred(void *user_data) { |
| 247 |
struct test_context *tester = user_data; |
| 248 |
return tester->retained_packet_received && tester->on_any_publish_fired; |
| 249 |
} |
| 250 |
static bool s_received_unsub_ack_pred(void *user_data) { |
| 251 |
struct test_context *tester = user_data; |
| 252 |
return tester->received_unsub_ack; |
| 253 |
} |
| 254 |
|
| 255 |
static void s_wait_on_tester_predicate(struct test_context *tester, bool (*predicate)(void *)) { |
| 256 |
aws_mutex_lock(&tester->lock); |
| 257 |
aws_condition_variable_wait_pred(&tester->signal, &tester->lock, predicate, tester); |
| 258 |
aws_mutex_unlock(&tester->lock); |
| 259 |
} |
| 260 |
|
| 261 |
static int s_initialize_test( |
| 262 |
struct test_context *tester, |
| 263 |
struct aws_allocator *allocator, |
| 264 |
struct aws_mqtt_connection_options *conn_options) { |
| 265 |
aws_mqtt_library_init(allocator); |
| 266 |
|
| 267 |
aws_mutex_init(&tester->lock); |
| 268 |
aws_condition_variable_init(&tester->signal); |
| 269 |
|
| 270 |
tester->el_group = aws_event_loop_group_new_default(allocator, 1, NULL); |
| 271 |
|
| 272 |
struct aws_host_resolver_default_options resolver_options = { |
| 273 |
.el_group = tester->el_group, |
| 274 |
.max_entries = 8, |
| 275 |
}; |
| 276 |
tester->resolver = aws_host_resolver_new_default(allocator, &resolver_options); |
| 277 |
|
| 278 |
struct aws_client_bootstrap_options bootstrap_options = { |
| 279 |
.event_loop_group = tester->el_group, |
| 280 |
.host_resolver = tester->resolver, |
| 281 |
}; |
| 282 |
tester->bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options); |
| 283 |
|
| 284 |
tester->client = aws_mqtt_client_new(allocator, tester->bootstrap); |
| 285 |
tester->connection = aws_mqtt_client_connection_new(tester->client); |
| 286 |
|
| 287 |
aws_mqtt_client_connection_set_connection_interruption_handlers( |
| 288 |
tester->connection, s_mqtt_on_interrupted, NULL, s_mqtt_on_resumed, NULL); |
| 289 |
|
| 290 |
aws_mqtt_client_connection_set_on_any_publish_handler(tester->connection, s_on_any_packet_received, tester); |
| 291 |
|
| 292 |
AWS_FATAL_ASSERT(aws_mqtt_client_connection_connect(tester->connection, conn_options) == AWS_OP_SUCCESS); |
| 293 |
|
| 294 |
return AWS_OP_SUCCESS; |
| 295 |
} |
| 296 |
|
| 297 |
static void s_cleanup_test(struct test_context *tester) { |
| 298 |
|
| 299 |
aws_mqtt_client_connection_release(tester->connection); |
| 300 |
aws_mqtt_client_release(tester->client); |
| 301 |
|
| 302 |
aws_client_bootstrap_release(tester->bootstrap); |
| 303 |
aws_host_resolver_release(tester->resolver); |
| 304 |
aws_event_loop_group_release(tester->el_group); |
| 305 |
|
| 306 |
aws_thread_join_all_managed(); |
| 307 |
|
| 308 |
aws_mutex_clean_up(&tester->lock); |
| 309 |
aws_condition_variable_clean_up(&tester->signal); |
| 310 |
|
| 311 |
aws_mqtt_library_clean_up(); |
| 312 |
} |
| 313 |
|
| 314 |
int main(int argc, char **argv) { |
| 315 |
(void)argc; |
| 316 |
(void)argv; |
| 317 |
|
| 318 |
struct aws_allocator *allocator = aws_mem_tracer_new(aws_default_allocator(), NULL, AWS_MEMTRACE_BYTES, 0); |
| 319 |
|
| 320 |
struct test_context tester; |
| 321 |
AWS_ZERO_STRUCT(tester); |
| 322 |
|
| 323 |
struct aws_byte_cursor host_name_cur = aws_byte_cursor_from_string(s_hostname); |
| 324 |
|
| 325 |
struct aws_socket_options options; |
| 326 |
AWS_ZERO_STRUCT(options); |
| 327 |
options.connect_timeout_ms = 3000; |
| 328 |
options.type = AWS_SOCKET_STREAM; |
| 329 |
options.domain = AWS_SOCKET_IPV4; |
| 330 |
|
| 331 |
struct aws_mqtt_connection_options conn_options = { |
| 332 |
.host_name = host_name_cur, |
| 333 |
.port = 1883, |
| 334 |
.socket_options = &options, |
| 335 |
.tls_options = NULL, |
| 336 |
.client_id = s_client_id, |
| 337 |
.keep_alive_time_secs = 0, |
| 338 |
.ping_timeout_ms = 0, |
| 339 |
.on_connection_complete = s_mqtt_on_connection_complete, |
| 340 |
.user_data = &tester, |
| 341 |
.clean_session = true, |
| 342 |
}; |
| 343 |
|
| 344 |
AWS_FATAL_ASSERT(AWS_OP_SUCCESS == s_initialize_test(&tester, allocator, &conn_options)); |
| 345 |
|
| 346 |
|
| 347 |
s_wait_on_tester_predicate(&tester, s_is_connection_complete_pred); |
| 348 |
|
| 349 |
printf("1 done\n"); |
| 350 |
|
| 351 |
struct aws_byte_cursor subscribe_topic_cur = aws_byte_cursor_from_string(s_subscribe_topic); |
| 352 |
|
| 353 |
|
| 354 |
struct aws_byte_cursor payload_cur = aws_byte_cursor_from_array(s_payload, PAYLOAD_LEN); |
| 355 |
|
| 356 |
aws_mqtt_client_connection_publish( |
| 357 |
tester.connection, |
| 358 |
&subscribe_topic_cur, |
| 359 |
AWS_MQTT_QOS_EXACTLY_ONCE, |
| 360 |
true, |
| 361 |
&payload_cur, |
| 362 |
&s_mqtt_on_puback, |
| 363 |
&tester); |
| 364 |
|
| 365 |
|
| 366 |
s_wait_on_tester_predicate(&tester, s_received_pub_ack_pred); |
| 367 |
|
| 368 |
printf("2 done\n"); |
| 369 |
|
| 370 |
aws_mqtt_client_connection_disconnect(tester.connection, s_mqtt_on_disconnect, &tester); |
| 371 |
|
| 372 |
|
| 373 |
s_wait_on_tester_predicate(&tester, s_on_disconnect_received); |
| 374 |
|
| 375 |
printf("3 done\n"); |
| 376 |
|
| 377 |
aws_mutex_lock(&tester.lock); |
| 378 |
tester.connection_complete = false; |
| 379 |
tester.on_disconnect_received = false; |
| 380 |
aws_mutex_unlock(&tester.lock); |
| 381 |
|
| 382 |
AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_mqtt_client_connection_connect(tester.connection, &conn_options)); |
| 383 |
|
| 384 |
|
| 385 |
s_wait_on_tester_predicate(&tester, s_is_connection_complete_pred); |
| 386 |
|
| 387 |
printf("1 done\n"); |
| 388 |
|
| 389 |
|
| 390 |
aws_mqtt_client_connection_subscribe( |
| 391 |
tester.connection, |
| 392 |
&subscribe_topic_cur, |
| 393 |
AWS_MQTT_QOS_EXACTLY_ONCE, |
| 394 |
&s_on_packet_received, |
| 395 |
&tester, |
| 396 |
NULL, |
| 397 |
NULL, |
| 398 |
NULL); |
| 399 |
|
| 400 |
|
| 401 |
s_wait_on_tester_predicate(&tester, s_retained_and_any_publish_pred); |
| 402 |
|
| 403 |
printf("2 done\n"); |
| 404 |
|
| 405 |
struct aws_byte_cursor topic_filter = |
| 406 |
aws_byte_cursor_from_array(aws_string_bytes(s_subscribe_topic), s_subscribe_topic->len); |
| 407 |
aws_mqtt_client_connection_unsubscribe(tester.connection, &topic_filter, &s_mqtt_on_unsuback, &tester); |
| 408 |
|
| 409 |
|
| 410 |
s_wait_on_tester_predicate(&tester, s_received_unsub_ack_pred); |
| 411 |
|
| 412 |
printf("3 done\n"); |
| 413 |
|
| 414 |
sleep(4); |
| 415 |
|
| 416 |
aws_mqtt_client_connection_disconnect(tester.connection, s_mqtt_on_disconnect, &tester); |
| 417 |
|
| 418 |
s_wait_on_tester_predicate(&tester, s_on_disconnect_received); |
| 419 |
|
| 420 |
s_cleanup_test(&tester); |
| 421 |
|
| 422 |
AWS_FATAL_ASSERT(0 == aws_mem_tracer_count(allocator)); |
| 423 |
allocator = aws_mem_tracer_destroy(allocator); |
| 424 |
|
| 425 |
return AWS_OP_SUCCESS; |
| 426 |
} |