| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/common/clock.h> |
| 7 |
#include <aws/common/command_line_parser.h> |
| 8 |
#include <aws/common/condition_variable.h> |
| 9 |
#include <aws/common/hash_table.h> |
| 10 |
#include <aws/common/log_channel.h> |
| 11 |
#include <aws/common/log_formatter.h> |
| 12 |
#include <aws/common/log_writer.h> |
| 13 |
#include <aws/common/mutex.h> |
| 14 |
#include <aws/common/string.h> |
| 15 |
|
| 16 |
#include <aws/io/channel_bootstrap.h> |
| 17 |
#include <aws/io/event_loop.h> |
| 18 |
#include <aws/io/logging.h> |
| 19 |
#include <aws/io/socket.h> |
| 20 |
#include <aws/io/stream.h> |
| 21 |
#include <aws/io/tls_channel_handler.h> |
| 22 |
#include <aws/io/uri.h> |
| 23 |
|
| 24 |
#include <aws/mqtt/private/v5/mqtt5_client_impl.h> |
| 25 |
#include <aws/mqtt/private/v5/mqtt5_utils.h> |
| 26 |
#include <aws/mqtt/v5/mqtt5_client.h> |
| 27 |
#include <aws/mqtt/v5/mqtt5_types.h> |
| 28 |
|
| 29 |
#include <inttypes.h> |
| 30 |
|
| 31 |
#ifdef _MSC_VER |
| 32 |
# pragma warning(disable : 4996) |
| 33 |
# pragma warning(disable : 4204) |
| 34 |
# pragma warning(disable : 4221) |
| 35 |
#endif |
| 36 |
|
| 37 |
#define AWS_MQTT5_CANARY_CLIENT_CREATION_SLEEP_TIME 10000000 |
| 38 |
#define AWS_MQTT5_CANARY_OPERATION_ARRAY_SIZE 10000 |
| 39 |
#define AWS_MQTT5_CANARY_TOPIC_ARRAY_SIZE 256 |
| 40 |
#define AWS_MQTT5_CANARY_CLIENT_MAX 50 |
| 41 |
#define AWS_MQTT5_CANARY_PAYLOAD_SIZE_MAX UINT16_MAX |
| 42 |
|
| 43 |
struct app_ctx { |
| 44 |
struct aws_allocator *allocator; |
| 45 |
struct aws_mutex lock; |
| 46 |
struct aws_condition_variable signal; |
| 47 |
struct aws_uri uri; |
| 48 |
uint32_t port; |
| 49 |
const char *cacert; |
| 50 |
const char *cert; |
| 51 |
const char *key; |
| 52 |
int connect_timeout; |
| 53 |
bool use_websockets; |
| 54 |
|
| 55 |
struct aws_tls_connection_options tls_connection_options; |
| 56 |
|
| 57 |
const char *log_filename; |
| 58 |
enum aws_log_level log_level; |
| 59 |
}; |
| 60 |
|
| 61 |
enum aws_mqtt5_canary_operations { |
| 62 |
AWS_MQTT5_CANARY_OPERATION_NULL = 0, |
| 63 |
AWS_MQTT5_CANARY_OPERATION_START = 1, |
| 64 |
AWS_MQTT5_CANARY_OPERATION_STOP = 2, |
| 65 |
AWS_MQTT5_CANARY_OPERATION_DESTROY = 3, |
| 66 |
AWS_MQTT5_CANARY_OPERATION_SUBSCRIBE = 4, |
| 67 |
AWS_MQTT5_CANARY_OPERATION_UNSUBSCRIBE = 5, |
| 68 |
AWS_MQTT5_CANARY_OPERATION_UNSUBSCRIBE_BAD = 6, |
| 69 |
AWS_MQTT5_CANARY_OPERATION_PUBLISH_QOS0 = 7, |
| 70 |
AWS_MQTT5_CANARY_OPERATION_PUBLISH_QOS1 = 8, |
| 71 |
AWS_MQTT5_CANARY_OPERATION_PUBLISH_TO_SUBSCRIBED_TOPIC_QOS0 = 9, |
| 72 |
AWS_MQTT5_CANARY_OPERATION_PUBLISH_TO_SUBSCRIBED_TOPIC_QOS1 = 10, |
| 73 |
AWS_MQTT5_CANARY_OPERATION_PUBLISH_TO_SHARED_TOPIC_QOS0 = 11, |
| 74 |
AWS_MQTT5_CANARY_OPERATION_PUBLISH_TO_SHARED_TOPIC_QOS1 = 12, |
| 75 |
AWS_MQTT5_CANARY_OPERATION_COUNT = 13, |
| 76 |
}; |
| 77 |
|
| 78 |
struct aws_mqtt5_canary_tester_options { |
| 79 |
uint16_t elg_max_threads; |
| 80 |
uint16_t client_count; |
| 81 |
size_t tps; |
| 82 |
uint64_t tps_sleep_time; |
| 83 |
size_t distributions_total; |
| 84 |
enum aws_mqtt5_canary_operations *operations; |
| 85 |
size_t test_run_seconds; |
| 86 |
}; |
| 87 |
|
| 88 |
static void s_usage(int exit_code) { |
| 89 |
|
| 90 |
fprintf(stderr, "usage: elastipubsub5 [options] endpoint\n"); |
| 91 |
fprintf(stderr, " endpoint: url to connect to\n"); |
| 92 |
fprintf(stderr, "\n Options:\n\n"); |
| 93 |
fprintf(stderr, " --cacert FILE: path to a CA certficate file.\n"); |
| 94 |
fprintf(stderr, " --cert FILE: path to a PEM encoded certificate to use with mTLS\n"); |
| 95 |
fprintf(stderr, " --key FILE: Path to a PEM encoded private key that matches cert.\n"); |
| 96 |
fprintf(stderr, " --connect-timeout INT: time in milliseconds to wait for a connection.\n"); |
| 97 |
fprintf(stderr, " -l, --log FILE: dumps logs to FILE instead of stderr.\n"); |
| 98 |
fprintf(stderr, " -v, --verbose: ERROR|INFO|DEBUG|TRACE: log level to configure. Default is none.\n"); |
| 99 |
fprintf(stderr, " -w, --websockets: use mqtt-over-websockets rather than direct mqtt\n"); |
| 100 |
fprintf(stderr, " -p, --port: Port to use when making MQTT connections\n"); |
| 101 |
|
| 102 |
fprintf(stderr, " -t, --threads: number of eventloop group threads to use\n"); |
| 103 |
fprintf(stderr, " -C, --clients: number of mqtt5 clients to use\n"); |
| 104 |
fprintf(stderr, " -T, --tps: operations to run per second\n"); |
| 105 |
fprintf(stderr, " -s, --seconds: seconds to run canary test (set as 0 to run forever)\n"); |
| 106 |
fprintf(stderr, " -h, --help\n"); |
| 107 |
fprintf(stderr, " Display this message and quit.\n"); |
| 108 |
exit(exit_code); |
| 109 |
} |
| 110 |
|
| 111 |
static struct aws_cli_option s_long_options[] = { |
| 112 |
{"cacert", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'a'}, |
| 113 |
{"cert", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'c'}, |
| 114 |
{"key", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'e'}, |
| 115 |
{"connect-timeout", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'f'}, |
| 116 |
{"log", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'l'}, |
| 117 |
{"verbose", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'v'}, |
| 118 |
{"websockets", AWS_CLI_OPTIONS_NO_ARGUMENT, NULL, 'w'}, |
| 119 |
{"port", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'p'}, |
| 120 |
{"help", AWS_CLI_OPTIONS_NO_ARGUMENT, NULL, 'h'}, |
| 121 |
|
| 122 |
{"threads", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 't'}, |
| 123 |
{"clients", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'C'}, |
| 124 |
{"tps", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'T'}, |
| 125 |
{"seconds", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 's'}, |
| 126 |
|
| 127 |
{NULL, AWS_CLI_OPTIONS_NO_ARGUMENT, NULL, 0}, |
| 128 |
}; |
| 129 |
|
| 130 |
static void s_parse_options( |
| 131 |
int argc, |
| 132 |
char **argv, |
| 133 |
struct app_ctx *ctx, |
| 134 |
struct aws_mqtt5_canary_tester_options *tester_options) { |
| 135 |
bool uri_found = false; |
| 136 |
|
| 137 |
while (true) { |
| 138 |
int option_index = 0; |
| 139 |
int c = aws_cli_getopt_long(argc, argv, "a:c:e:f:l:v:wht:p:C:T:s:", s_long_options, &option_index); |
| 140 |
if (c == -1) { |
| 141 |
break; |
| 142 |
} |
| 143 |
|
| 144 |
switch (c) { |
| 145 |
case 0: |
| 146 |
|
| 147 |
break; |
| 148 |
case 'a': |
| 149 |
ctx->cacert = aws_cli_optarg; |
| 150 |
break; |
| 151 |
case 'c': |
| 152 |
ctx->cert = aws_cli_optarg; |
| 153 |
break; |
| 154 |
case 'e': |
| 155 |
ctx->key = aws_cli_optarg; |
| 156 |
break; |
| 157 |
case 'f': |
| 158 |
ctx->connect_timeout = atoi(aws_cli_optarg); |
| 159 |
break; |
| 160 |
case 'l': |
| 161 |
ctx->log_filename = aws_cli_optarg; |
| 162 |
break; |
| 163 |
case 'v': |
| 164 |
if (!strcmp(aws_cli_optarg, "TRACE")) { |
| 165 |
ctx->log_level = AWS_LL_TRACE; |
| 166 |
} else if (!strcmp(aws_cli_optarg, "INFO")) { |
| 167 |
ctx->log_level = AWS_LL_INFO; |
| 168 |
} else if (!strcmp(aws_cli_optarg, "DEBUG")) { |
| 169 |
ctx->log_level = AWS_LL_DEBUG; |
| 170 |
} else if (!strcmp(aws_cli_optarg, "ERROR")) { |
| 171 |
ctx->log_level = AWS_LL_ERROR; |
| 172 |
} else { |
| 173 |
fprintf(stderr, "unsupported log level %s.\n", aws_cli_optarg); |
| 174 |
s_usage(1); |
| 175 |
} |
| 176 |
break; |
| 177 |
case 'h': |
| 178 |
s_usage(0); |
| 179 |
break; |
| 180 |
case 'w': |
| 181 |
ctx->use_websockets = true; |
| 182 |
break; |
| 183 |
case 'p': |
| 184 |
ctx->port = (uint32_t)atoi(aws_cli_optarg); |
| 185 |
break; |
| 186 |
case 't': |
| 187 |
tester_options->elg_max_threads = (uint16_t)atoi(aws_cli_optarg); |
| 188 |
break; |
| 189 |
case 'C': |
| 190 |
tester_options->client_count = (uint16_t)atoi(aws_cli_optarg); |
| 191 |
if (tester_options->client_count > AWS_MQTT5_CANARY_CLIENT_MAX) { |
| 192 |
tester_options->client_count = AWS_MQTT5_CANARY_CLIENT_MAX; |
| 193 |
} |
| 194 |
break; |
| 195 |
case 'T': |
| 196 |
tester_options->tps = atoi(aws_cli_optarg); |
| 197 |
break; |
| 198 |
case 's': |
| 199 |
tester_options->test_run_seconds = atoi(aws_cli_optarg); |
| 200 |
break; |
| 201 |
case 0x02: { |
| 202 |
struct aws_byte_cursor uri_cursor = aws_byte_cursor_from_c_str(aws_cli_positional_arg); |
| 203 |
if (aws_uri_init_parse(&ctx->uri, ctx->allocator, &uri_cursor)) { |
| 204 |
fprintf( |
| 205 |
stderr, |
| 206 |
"Failed to parse uri %s with error %s\n", |
| 207 |
(char *)uri_cursor.ptr, |
| 208 |
aws_error_debug_str(aws_last_error())); |
| 209 |
s_usage(1); |
| 210 |
} |
| 211 |
uri_found = true; |
| 212 |
break; |
| 213 |
} |
| 214 |
|
| 215 |
default: |
| 216 |
fprintf(stderr, "Unknown option\n"); |
| 217 |
s_usage(1); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
if (!uri_found) { |
| 222 |
fprintf(stderr, "A URI for the request must be supplied.\n"); |
| 223 |
s_usage(1); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
static void s_aws_mqtt5_canary_update_tps_sleep_time(struct aws_mqtt5_canary_tester_options *tester_options) { |
| 232 |
tester_options->tps_sleep_time = |
| 233 |
(aws_timestamp_convert(1, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL) / tester_options->tps); |
| 234 |
} |
| 235 |
|
| 236 |
static void s_aws_mqtt5_canary_init_tester_options(struct aws_mqtt5_canary_tester_options *tester_options) { |
| 237 |
|
| 238 |
tester_options->elg_max_threads = 3; |
| 239 |
|
| 240 |
tester_options->client_count = 10; |
| 241 |
|
| 242 |
tester_options->tps = 50; |
| 243 |
|
| 244 |
tester_options->test_run_seconds = 25200; |
| 245 |
} |
| 246 |
|
| 247 |
struct aws_mqtt5_canary_test_client { |
| 248 |
struct aws_mqtt5_client *client; |
| 249 |
const struct aws_mqtt5_negotiated_settings *settings; |
| 250 |
struct aws_byte_cursor shared_topic; |
| 251 |
struct aws_byte_cursor client_id; |
| 252 |
size_t subscription_count; |
| 253 |
bool is_connected; |
| 254 |
}; |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
typedef int(aws_mqtt5_canary_operation_fn)(struct aws_mqtt5_canary_test_client *test_client); |
| 261 |
|
| 262 |
struct aws_mqtt5_canary_operations_function_table { |
| 263 |
aws_mqtt5_canary_operation_fn *operation_by_operation_type[AWS_MQTT5_CANARY_OPERATION_COUNT]; |
| 264 |
}; |
| 265 |
|
| 266 |
static void s_aws_mqtt5_canary_add_operation_to_array( |
| 267 |
struct aws_mqtt5_canary_tester_options *tester_options, |
| 268 |
enum aws_mqtt5_canary_operations operation_type, |
| 269 |
size_t probability) { |
| 270 |
for (size_t i = 0; i < probability; ++i) { |
| 271 |
|
| 272 |
tester_options->operations[tester_options->distributions_total] = operation_type; |
| 273 |
tester_options->distributions_total += 1; |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
static void s_aws_mqtt5_canary_init_weighted_operations(struct aws_mqtt5_canary_tester_options *tester_options) { |
| 279 |
|
| 280 |
s_aws_mqtt5_canary_add_operation_to_array(tester_options, AWS_MQTT5_CANARY_OPERATION_STOP, 1); |
| 281 |
s_aws_mqtt5_canary_add_operation_to_array(tester_options, AWS_MQTT5_CANARY_OPERATION_SUBSCRIBE, 200); |
| 282 |
s_aws_mqtt5_canary_add_operation_to_array(tester_options, AWS_MQTT5_CANARY_OPERATION_UNSUBSCRIBE, 200); |
| 283 |
s_aws_mqtt5_canary_add_operation_to_array(tester_options, AWS_MQTT5_CANARY_OPERATION_UNSUBSCRIBE_BAD, 100); |
| 284 |
s_aws_mqtt5_canary_add_operation_to_array(tester_options, AWS_MQTT5_CANARY_OPERATION_PUBLISH_QOS0, 300); |
| 285 |
s_aws_mqtt5_canary_add_operation_to_array(tester_options, AWS_MQTT5_CANARY_OPERATION_PUBLISH_QOS1, 150); |
| 286 |
s_aws_mqtt5_canary_add_operation_to_array( |
| 287 |
tester_options, AWS_MQTT5_CANARY_OPERATION_PUBLISH_TO_SUBSCRIBED_TOPIC_QOS0, 100); |
| 288 |
s_aws_mqtt5_canary_add_operation_to_array( |
| 289 |
tester_options, AWS_MQTT5_CANARY_OPERATION_PUBLISH_TO_SUBSCRIBED_TOPIC_QOS1, 50); |
| 290 |
s_aws_mqtt5_canary_add_operation_to_array( |
| 291 |
tester_options, AWS_MQTT5_CANARY_OPERATION_PUBLISH_TO_SHARED_TOPIC_QOS0, 50); |
| 292 |
s_aws_mqtt5_canary_add_operation_to_array( |
| 293 |
tester_options, AWS_MQTT5_CANARY_OPERATION_PUBLISH_TO_SHARED_TOPIC_QOS1, 50); |
| 294 |
} |
| 295 |
|
| 296 |
static enum aws_mqtt5_canary_operations s_aws_mqtt5_canary_get_random_operation( |
| 297 |
struct aws_mqtt5_canary_tester_options *tester_options) { |
| 298 |
size_t random_index = rand() % tester_options->distributions_total; |
| 299 |
|
| 300 |
return tester_options->operations[random_index]; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
static void s_on_publish_received(const struct aws_mqtt5_packet_publish_view *publish, void *user_data) { |
| 308 |
(void)publish; |
| 309 |
|
| 310 |
struct aws_mqtt5_canary_test_client *test_client = user_data; |
| 311 |
|
| 312 |
AWS_LOGF_INFO( |
| 313 |
AWS_LS_MQTT5_CANARY, |
| 314 |
"ID:" PRInSTR " Publish Received on topic " PRInSTR, |
| 315 |
AWS_BYTE_CURSOR_PRI(test_client->client_id), |
| 316 |
AWS_BYTE_CURSOR_PRI(publish->topic)); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
static void s_handle_lifecycle_event_connection_success( |
| 324 |
struct aws_mqtt5_canary_test_client *test_client, |
| 325 |
const struct aws_mqtt5_negotiated_settings *settings) { |
| 326 |
AWS_ASSERT(test_client != NULL); |
| 327 |
test_client->is_connected = true; |
| 328 |
test_client->settings = settings; |
| 329 |
test_client->client_id = aws_byte_cursor_from_buf(&settings->client_id_storage); |
| 330 |
|
| 331 |
AWS_LOGF_INFO( |
| 332 |
AWS_LS_MQTT5_CANARY, |
| 333 |
"ID:" PRInSTR " Lifecycle Event: Connection Success", |
| 334 |
AWS_BYTE_CURSOR_PRI(test_client->client_id)); |
| 335 |
} |
| 336 |
|
| 337 |
static void s_handle_lifecycle_event_disconnection(struct aws_mqtt5_canary_test_client *test_client) { |
| 338 |
AWS_ASSERT(test_client != NULL); |
| 339 |
test_client->is_connected = false; |
| 340 |
AWS_LOGF_INFO( |
| 341 |
AWS_LS_MQTT5_CANARY, "ID:" PRInSTR " Lifecycle Event: Disconnect", AWS_BYTE_CURSOR_PRI(test_client->client_id)); |
| 342 |
} |
| 343 |
|
| 344 |
static void s_handle_lifecycle_event_stopped(struct aws_mqtt5_canary_test_client *test_client) { |
| 345 |
AWS_ASSERT(test_client != NULL); |
| 346 |
AWS_LOGF_INFO( |
| 347 |
AWS_LS_MQTT5_CANARY, "ID:" PRInSTR " Lifecycle Event: Stopped", AWS_BYTE_CURSOR_PRI(test_client->client_id)); |
| 348 |
} |
| 349 |
|
| 350 |
static void s_lifecycle_event_callback(const struct aws_mqtt5_client_lifecycle_event *event) { |
| 351 |
switch (event->event_type) { |
| 352 |
case AWS_MQTT5_CLET_STOPPED: |
| 353 |
s_handle_lifecycle_event_stopped(event->user_data); |
| 354 |
break; |
| 355 |
|
| 356 |
case AWS_MQTT5_CLET_ATTEMPTING_CONNECT: |
| 357 |
AWS_LOGF_INFO(AWS_LS_MQTT5_CANARY, "Lifecycle event: Attempting Connect!"); |
| 358 |
break; |
| 359 |
|
| 360 |
case AWS_MQTT5_CLET_CONNECTION_FAILURE: |
| 361 |
AWS_LOGF_INFO(AWS_LS_MQTT5_CANARY, "Lifecycle event: Connection Failure!"); |
| 362 |
AWS_LOGF_INFO( |
| 363 |
AWS_LS_MQTT5_CANARY, " Error Code: %d(%s)", event->error_code, aws_error_debug_str(event->error_code)); |
| 364 |
break; |
| 365 |
|
| 366 |
case AWS_MQTT5_CLET_CONNECTION_SUCCESS: |
| 367 |
s_handle_lifecycle_event_connection_success(event->user_data, event->settings); |
| 368 |
break; |
| 369 |
|
| 370 |
case AWS_MQTT5_CLET_DISCONNECTION: |
| 371 |
s_handle_lifecycle_event_disconnection(event->user_data); |
| 372 |
AWS_LOGF_INFO( |
| 373 |
AWS_LS_MQTT5_CANARY, " Error Code: %d(%s)", event->error_code, aws_error_debug_str(event->error_code)); |
| 374 |
break; |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
static void s_aws_mqtt5_transform_websocket_handshake_fn( |
| 379 |
struct aws_http_message *request, |
| 380 |
void *user_data, |
| 381 |
aws_mqtt5_transform_websocket_handshake_complete_fn *complete_fn, |
| 382 |
void *complete_ctx) { |
| 383 |
|
| 384 |
(void)user_data; |
| 385 |
|
| 386 |
(*complete_fn)(request, AWS_ERROR_SUCCESS, complete_ctx); |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
static void s_aws_mqtt5_canary_operation_subscribe_completion( |
| 394 |
const struct aws_mqtt5_packet_suback_view *suback, |
| 395 |
int error_code, |
| 396 |
void *complete_ctx) { |
| 397 |
(void)suback; |
| 398 |
|
| 399 |
if (error_code != AWS_MQTT5_UARC_SUCCESS) { |
| 400 |
struct aws_mqtt5_canary_test_client *test_client = (struct aws_mqtt5_canary_test_client *)(complete_ctx); |
| 401 |
if (test_client != NULL) { |
| 402 |
AWS_LOGF_ERROR( |
| 403 |
AWS_LS_MQTT5_CANARY, |
| 404 |
"ID:" PRInSTR " Subscribe completed with error code: %i", |
| 405 |
AWS_BYTE_CURSOR_PRI(test_client->client_id), |
| 406 |
error_code); |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
static void s_aws_mqtt5_canary_operation_unsubscribe_completion( |
| 412 |
const struct aws_mqtt5_packet_unsuback_view *unsuback, |
| 413 |
int error_code, |
| 414 |
void *complete_ctx) { |
| 415 |
(void)unsuback; |
| 416 |
|
| 417 |
if (error_code != AWS_MQTT5_UARC_SUCCESS) { |
| 418 |
struct aws_mqtt5_canary_test_client *test_client = (struct aws_mqtt5_canary_test_client *)(complete_ctx); |
| 419 |
if (test_client != NULL) { |
| 420 |
AWS_LOGF_ERROR( |
| 421 |
AWS_LS_MQTT5_CANARY, |
| 422 |
"ID:" PRInSTR " Unsubscribe completed with error code: %i", |
| 423 |
AWS_BYTE_CURSOR_PRI(test_client->client_id), |
| 424 |
error_code); |
| 425 |
} |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
static void s_aws_mqtt5_canary_operation_publish_completion( |
| 430 |
enum aws_mqtt5_packet_type packet_type, |
| 431 |
const void *packet, |
| 432 |
int error_code, |
| 433 |
void *complete_ctx) { |
| 434 |
(void)packet; |
| 435 |
(void)packet_type; |
| 436 |
|
| 437 |
if (error_code != AWS_MQTT5_PARC_SUCCESS) { |
| 438 |
struct aws_mqtt5_canary_test_client *test_client = (struct aws_mqtt5_canary_test_client *)(complete_ctx); |
| 439 |
if (test_client != NULL) { |
| 440 |
AWS_LOGF_ERROR( |
| 441 |
AWS_LS_MQTT5_CANARY, |
| 442 |
"ID:" PRInSTR " Publish completed with error code: %i", |
| 443 |
AWS_BYTE_CURSOR_PRI(test_client->client_id), |
| 444 |
error_code); |
| 445 |
} |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
static int s_aws_mqtt5_canary_operation_start(struct aws_mqtt5_canary_test_client *test_client) { |
| 454 |
if (test_client->is_connected) { |
| 455 |
return AWS_OP_SUCCESS; |
| 456 |
} |
| 457 |
aws_mqtt5_client_start(test_client->client); |
| 458 |
struct aws_byte_cursor client_id; |
| 459 |
if (test_client->client_id.len) { |
| 460 |
client_id.ptr = test_client->client_id.ptr; |
| 461 |
client_id.len = test_client->client_id.len; |
| 462 |
} else { |
| 463 |
client_id = aws_byte_cursor_from_c_str("Client ID not set"); |
| 464 |
} |
| 465 |
AWS_LOGF_INFO(AWS_LS_MQTT5_CANARY, "ID:" PRInSTR " Start", AWS_BYTE_CURSOR_PRI(client_id)); |
| 466 |
return AWS_OP_SUCCESS; |
| 467 |
} |
| 468 |
|
| 469 |
static int s_aws_mqtt5_canary_operation_stop(struct aws_mqtt5_canary_test_client *test_client) { |
| 470 |
if (!test_client->is_connected) { |
| 471 |
return AWS_OP_SUCCESS; |
| 472 |
} |
| 473 |
aws_mqtt5_client_stop(test_client->client, NULL, NULL); |
| 474 |
test_client->subscription_count = 0; |
| 475 |
AWS_LOGF_INFO(AWS_LS_MQTT5_CANARY, "ID:" PRInSTR " Stop", AWS_BYTE_CURSOR_PRI(test_client->client_id)); |
| 476 |
return AWS_OP_SUCCESS; |
| 477 |
} |
| 478 |
|
| 479 |
static int s_aws_mqtt5_canary_operation_subscribe(struct aws_mqtt5_canary_test_client *test_client) { |
| 480 |
if (!test_client->is_connected) { |
| 481 |
return s_aws_mqtt5_canary_operation_start(test_client); |
| 482 |
} |
| 483 |
char topic_array[AWS_MQTT5_CANARY_TOPIC_ARRAY_SIZE] = ""; |
| 484 |
snprintf( |
| 485 |
topic_array, |
| 486 |
sizeof topic_array, |
| 487 |
PRInSTR "_%zu", |
| 488 |
AWS_BYTE_CURSOR_PRI(test_client->client_id), |
| 489 |
test_client->subscription_count); |
| 490 |
|
| 491 |
struct aws_mqtt5_subscription_view subscriptions[] = { |
| 492 |
{ |
| 493 |
.topic_filter = aws_byte_cursor_from_c_str(topic_array), |
| 494 |
.qos = AWS_MQTT5_QOS_AT_LEAST_ONCE, |
| 495 |
.no_local = false, |
| 496 |
.retain_as_published = false, |
| 497 |
.retain_handling_type = AWS_MQTT5_RHT_SEND_ON_SUBSCRIBE, |
| 498 |
}, |
| 499 |
{ |
| 500 |
.topic_filter = |
| 501 |
{ |
| 502 |
.ptr = test_client->shared_topic.ptr, |
| 503 |
.len = test_client->shared_topic.len, |
| 504 |
}, |
| 505 |
.qos = AWS_MQTT5_QOS_AT_LEAST_ONCE, |
| 506 |
.no_local = false, |
| 507 |
.retain_as_published = false, |
| 508 |
.retain_handling_type = AWS_MQTT5_RHT_SEND_ON_SUBSCRIBE, |
| 509 |
}, |
| 510 |
}; |
| 511 |
|
| 512 |
struct aws_mqtt5_packet_subscribe_view subscribe_view = { |
| 513 |
.subscriptions = subscriptions, |
| 514 |
.subscription_count = AWS_ARRAY_SIZE(subscriptions), |
| 515 |
}; |
| 516 |
|
| 517 |
struct aws_mqtt5_subscribe_completion_options subscribe_view_options = { |
| 518 |
.completion_callback = &s_aws_mqtt5_canary_operation_subscribe_completion, |
| 519 |
.completion_user_data = test_client, |
| 520 |
}; |
| 521 |
|
| 522 |
test_client->subscription_count++; |
| 523 |
|
| 524 |
AWS_LOGF_INFO( |
| 525 |
AWS_LS_MQTT5_CANARY, |
| 526 |
"ID:" PRInSTR " Subscribe to topic: " PRInSTR, |
| 527 |
AWS_BYTE_CURSOR_PRI(test_client->client_id), |
| 528 |
AWS_BYTE_CURSOR_PRI(subscriptions->topic_filter)); |
| 529 |
return aws_mqtt5_client_subscribe(test_client->client, &subscribe_view, &subscribe_view_options); |
| 530 |
} |
| 531 |
|
| 532 |
static int s_aws_mqtt5_canary_operation_unsubscribe_bad(struct aws_mqtt5_canary_test_client *test_client) { |
| 533 |
if (!test_client->is_connected) { |
| 534 |
return s_aws_mqtt5_canary_operation_start(test_client); |
| 535 |
} |
| 536 |
char topic_array[AWS_MQTT5_CANARY_TOPIC_ARRAY_SIZE] = ""; |
| 537 |
snprintf( |
| 538 |
topic_array, sizeof topic_array, PRInSTR "_non_existing_topic", AWS_BYTE_CURSOR_PRI(test_client->client_id)); |
| 539 |
struct aws_byte_cursor topic = aws_byte_cursor_from_c_str(topic_array); |
| 540 |
struct aws_byte_cursor unsubscribes[] = { |
| 541 |
{ |
| 542 |
.ptr = topic.ptr, |
| 543 |
.len = topic.len, |
| 544 |
}, |
| 545 |
}; |
| 546 |
|
| 547 |
struct aws_mqtt5_packet_unsubscribe_view unsubscribe_view = { |
| 548 |
.topic_filters = unsubscribes, |
| 549 |
.topic_filter_count = AWS_ARRAY_SIZE(unsubscribes), |
| 550 |
}; |
| 551 |
|
| 552 |
AWS_LOGF_INFO(AWS_LS_MQTT5_CANARY, "ID:" PRInSTR " Unsubscribe Bad", AWS_BYTE_CURSOR_PRI(test_client->client_id)); |
| 553 |
return aws_mqtt5_client_unsubscribe(test_client->client, &unsubscribe_view, NULL); |
| 554 |
} |
| 555 |
|
| 556 |
static int s_aws_mqtt5_canary_operation_unsubscribe(struct aws_mqtt5_canary_test_client *test_client) { |
| 557 |
if (!test_client->is_connected) { |
| 558 |
return s_aws_mqtt5_canary_operation_start(test_client); |
| 559 |
} |
| 560 |
|
| 561 |
if (test_client->subscription_count <= 0) { |
| 562 |
return s_aws_mqtt5_canary_operation_unsubscribe_bad(test_client); |
| 563 |
} |
| 564 |
|
| 565 |
test_client->subscription_count--; |
| 566 |
char topic_array[AWS_MQTT5_CANARY_TOPIC_ARRAY_SIZE] = ""; |
| 567 |
snprintf( |
| 568 |
topic_array, |
| 569 |
sizeof topic_array, |
| 570 |
PRInSTR "_%zu", |
| 571 |
AWS_BYTE_CURSOR_PRI(test_client->client_id), |
| 572 |
test_client->subscription_count); |
| 573 |
struct aws_byte_cursor topic = aws_byte_cursor_from_c_str(topic_array); |
| 574 |
struct aws_byte_cursor unsubscribes[] = { |
| 575 |
{ |
| 576 |
.ptr = topic.ptr, |
| 577 |
.len = topic.len, |
| 578 |
}, |
| 579 |
}; |
| 580 |
|
| 581 |
struct aws_mqtt5_packet_unsubscribe_view unsubscribe_view = { |
| 582 |
.topic_filters = unsubscribes, |
| 583 |
.topic_filter_count = AWS_ARRAY_SIZE(unsubscribes), |
| 584 |
}; |
| 585 |
|
| 586 |
struct aws_mqtt5_unsubscribe_completion_options unsubscribe_view_options = { |
| 587 |
.completion_callback = &s_aws_mqtt5_canary_operation_unsubscribe_completion, |
| 588 |
.completion_user_data = test_client}; |
| 589 |
|
| 590 |
AWS_LOGF_INFO( |
| 591 |
AWS_LS_MQTT5_CANARY, |
| 592 |
"ID:" PRInSTR " Unsubscribe from topic: " PRInSTR, |
| 593 |
AWS_BYTE_CURSOR_PRI(test_client->client_id), |
| 594 |
AWS_BYTE_CURSOR_PRI(topic)); |
| 595 |
return aws_mqtt5_client_unsubscribe(test_client->client, &unsubscribe_view, &unsubscribe_view_options); |
| 596 |
} |
| 597 |
|
| 598 |
static int s_aws_mqtt5_canary_operation_publish( |
| 599 |
struct aws_mqtt5_canary_test_client *test_client, |
| 600 |
struct aws_byte_cursor topic_filter, |
| 601 |
enum aws_mqtt5_qos qos) { |
| 602 |
|
| 603 |
struct aws_byte_cursor property_cursor = aws_byte_cursor_from_c_str("property"); |
| 604 |
struct aws_mqtt5_user_property user_properties[] = { |
| 605 |
{ |
| 606 |
.name = |
| 607 |
{ |
| 608 |
.ptr = property_cursor.ptr, |
| 609 |
.len = property_cursor.len, |
| 610 |
}, |
| 611 |
.value = |
| 612 |
{ |
| 613 |
.ptr = property_cursor.ptr, |
| 614 |
.len = property_cursor.len, |
| 615 |
}, |
| 616 |
}, |
| 617 |
{ |
| 618 |
.name = |
| 619 |
{ |
| 620 |
.ptr = property_cursor.ptr, |
| 621 |
.len = property_cursor.len, |
| 622 |
}, |
| 623 |
.value = |
| 624 |
{ |
| 625 |
.ptr = property_cursor.ptr, |
| 626 |
.len = property_cursor.len, |
| 627 |
}, |
| 628 |
}, |
| 629 |
}; |
| 630 |
|
| 631 |
uint16_t payload_size = (rand() % UINT16_MAX) + 1; |
| 632 |
uint8_t payload_data[AWS_MQTT5_CANARY_PAYLOAD_SIZE_MAX]; |
| 633 |
|
| 634 |
struct aws_mqtt5_packet_publish_view packet_publish_view = { |
| 635 |
.qos = qos, |
| 636 |
.topic = topic_filter, |
| 637 |
.retain = false, |
| 638 |
.duplicate = false, |
| 639 |
.payload = |
| 640 |
{ |
| 641 |
.ptr = payload_data, |
| 642 |
.len = payload_size, |
| 643 |
}, |
| 644 |
.user_properties = user_properties, |
| 645 |
.user_property_count = AWS_ARRAY_SIZE(user_properties), |
| 646 |
}; |
| 647 |
|
| 648 |
struct aws_mqtt5_publish_completion_options packet_publish_view_options = { |
| 649 |
.completion_callback = &s_aws_mqtt5_canary_operation_publish_completion, .completion_user_data = test_client}; |
| 650 |
|
| 651 |
return aws_mqtt5_client_publish(test_client->client, &packet_publish_view, &packet_publish_view_options); |
| 652 |
} |
| 653 |
|
| 654 |
static int s_aws_mqtt5_canary_operation_publish_qos0(struct aws_mqtt5_canary_test_client *test_client) { |
| 655 |
if (!test_client->is_connected) { |
| 656 |
return s_aws_mqtt5_canary_operation_start(test_client); |
| 657 |
} |
| 658 |
|
| 659 |
struct aws_byte_cursor topic_cursor; |
| 660 |
AWS_ZERO_STRUCT(topic_cursor); |
| 661 |
topic_cursor = aws_byte_cursor_from_c_str("topic1"); |
| 662 |
AWS_LOGF_INFO(AWS_LS_MQTT5_CANARY, "ID:" PRInSTR " Publish qos0", AWS_BYTE_CURSOR_PRI(test_client->client_id)); |
| 663 |
return s_aws_mqtt5_canary_operation_publish(test_client, topic_cursor, AWS_MQTT5_QOS_AT_MOST_ONCE); |
| 664 |
} |
| 665 |
|
| 666 |
static int s_aws_mqtt5_canary_operation_publish_qos1(struct aws_mqtt5_canary_test_client *test_client) { |
| 667 |
if (!test_client->is_connected) { |
| 668 |
return s_aws_mqtt5_canary_operation_start(test_client); |
| 669 |
} |
| 670 |
struct aws_byte_cursor topic_cursor; |
| 671 |
AWS_ZERO_STRUCT(topic_cursor); |
| 672 |
topic_cursor = aws_byte_cursor_from_c_str("topic1"); |
| 673 |
AWS_LOGF_INFO(AWS_LS_MQTT5_CANARY, "ID:" PRInSTR " Publish qos1", AWS_BYTE_CURSOR_PRI(test_client->client_id)); |
| 674 |
return s_aws_mqtt5_canary_operation_publish(test_client, topic_cursor, AWS_MQTT5_QOS_AT_LEAST_ONCE); |
| 675 |
} |
| 676 |
|
| 677 |
static int s_aws_mqtt5_canary_operation_publish_to_subscribed_topic_qos0( |
| 678 |
struct aws_mqtt5_canary_test_client *test_client) { |
| 679 |
if (!test_client->is_connected) { |
| 680 |
return s_aws_mqtt5_canary_operation_start(test_client); |
| 681 |
} |
| 682 |
|
| 683 |
if (test_client->subscription_count < 1) { |
| 684 |
return s_aws_mqtt5_canary_operation_publish_qos0(test_client); |
| 685 |
} |
| 686 |
char topic_array[AWS_MQTT5_CANARY_TOPIC_ARRAY_SIZE] = ""; |
| 687 |
snprintf( |
| 688 |
topic_array, |
| 689 |
sizeof topic_array, |
| 690 |
PRInSTR "_%zu", |
| 691 |
AWS_BYTE_CURSOR_PRI(test_client->client_id), |
| 692 |
test_client->subscription_count - 1); |
| 693 |
struct aws_byte_cursor topic_cursor = aws_byte_cursor_from_c_str(topic_array); |
| 694 |
AWS_LOGF_INFO( |
| 695 |
AWS_LS_MQTT5_CANARY, |
| 696 |
"ID:" PRInSTR " Publish qos 0 to subscribed topic: " PRInSTR, |
| 697 |
AWS_BYTE_CURSOR_PRI(test_client->client_id), |
| 698 |
AWS_BYTE_CURSOR_PRI(topic_cursor)); |
| 699 |
return s_aws_mqtt5_canary_operation_publish(test_client, topic_cursor, AWS_MQTT5_QOS_AT_MOST_ONCE); |
| 700 |
} |
| 701 |
|
| 702 |
static int s_aws_mqtt5_canary_operation_publish_to_subscribed_topic_qos1( |
| 703 |
struct aws_mqtt5_canary_test_client *test_client) { |
| 704 |
if (!test_client->is_connected) { |
| 705 |
return s_aws_mqtt5_canary_operation_start(test_client); |
| 706 |
} |
| 707 |
|
| 708 |
if (test_client->subscription_count < 1) { |
| 709 |
return s_aws_mqtt5_canary_operation_publish_qos1(test_client); |
| 710 |
} |
| 711 |
|
| 712 |
char topic_array[AWS_MQTT5_CANARY_TOPIC_ARRAY_SIZE] = ""; |
| 713 |
snprintf( |
| 714 |
topic_array, |
| 715 |
sizeof topic_array, |
| 716 |
PRInSTR "_%zu", |
| 717 |
AWS_BYTE_CURSOR_PRI(test_client->client_id), |
| 718 |
test_client->subscription_count - 1); |
| 719 |
struct aws_byte_cursor topic_cursor = aws_byte_cursor_from_c_str(topic_array); |
| 720 |
AWS_LOGF_INFO( |
| 721 |
AWS_LS_MQTT5_CANARY, |
| 722 |
"ID:" PRInSTR " Publish qos 1 to subscribed topic: " PRInSTR, |
| 723 |
AWS_BYTE_CURSOR_PRI(test_client->client_id), |
| 724 |
AWS_BYTE_CURSOR_PRI(topic_cursor)); |
| 725 |
return s_aws_mqtt5_canary_operation_publish(test_client, topic_cursor, AWS_MQTT5_QOS_AT_LEAST_ONCE); |
| 726 |
} |
| 727 |
|
| 728 |
static int s_aws_mqtt5_canary_operation_publish_to_shared_topic_qos0(struct aws_mqtt5_canary_test_client *test_client) { |
| 729 |
if (!test_client->is_connected) { |
| 730 |
return s_aws_mqtt5_canary_operation_start(test_client); |
| 731 |
} |
| 732 |
AWS_LOGF_INFO( |
| 733 |
AWS_LS_MQTT5_CANARY, |
| 734 |
"ID:" PRInSTR " Publish qos 0 to shared topic: " PRInSTR, |
| 735 |
AWS_BYTE_CURSOR_PRI(test_client->client_id), |
| 736 |
AWS_BYTE_CURSOR_PRI(test_client->shared_topic)); |
| 737 |
return s_aws_mqtt5_canary_operation_publish(test_client, test_client->shared_topic, AWS_MQTT5_QOS_AT_MOST_ONCE); |
| 738 |
} |
| 739 |
|
| 740 |
static int s_aws_mqtt5_canary_operation_publish_to_shared_topic_qos1(struct aws_mqtt5_canary_test_client *test_client) { |
| 741 |
if (!test_client->is_connected) { |
| 742 |
return s_aws_mqtt5_canary_operation_start(test_client); |
| 743 |
} |
| 744 |
AWS_LOGF_INFO( |
| 745 |
AWS_LS_MQTT5_CANARY, |
| 746 |
"ID:" PRInSTR " Publish qos 1 to shared topic: " PRInSTR, |
| 747 |
AWS_BYTE_CURSOR_PRI(test_client->client_id), |
| 748 |
AWS_BYTE_CURSOR_PRI(test_client->shared_topic)); |
| 749 |
return s_aws_mqtt5_canary_operation_publish(test_client, test_client->shared_topic, AWS_MQTT5_QOS_AT_LEAST_ONCE); |
| 750 |
} |
| 751 |
|
| 752 |
static struct aws_mqtt5_canary_operations_function_table s_aws_mqtt5_canary_operation_table = { |
| 753 |
.operation_by_operation_type = |
| 754 |
{ |
| 755 |
NULL, |
| 756 |
&s_aws_mqtt5_canary_operation_start, |
| 757 |
&s_aws_mqtt5_canary_operation_stop, |
| 758 |
NULL, |
| 759 |
&s_aws_mqtt5_canary_operation_subscribe, |
| 760 |
&s_aws_mqtt5_canary_operation_unsubscribe, |
| 761 |
&s_aws_mqtt5_canary_operation_unsubscribe_bad, |
| 762 |
&s_aws_mqtt5_canary_operation_publish_qos0, |
| 763 |
&s_aws_mqtt5_canary_operation_publish_qos1, |
| 764 |
&s_aws_mqtt5_canary_operation_publish_to_subscribed_topic_qos0, |
| 765 |
&s_aws_mqtt5_canary_operation_publish_to_subscribed_topic_qos1, |
| 766 |
&s_aws_mqtt5_canary_operation_publish_to_shared_topic_qos0, |
| 767 |
&s_aws_mqtt5_canary_operation_publish_to_shared_topic_qos1, |
| 768 |
}, |
| 769 |
}; |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
int main(int argc, char **argv) { |
| 776 |
|
| 777 |
struct aws_allocator *allocator = aws_mem_tracer_new(aws_default_allocator(), NULL, AWS_MEMTRACE_STACKS, 15); |
| 778 |
aws_mqtt_library_init(allocator); |
| 779 |
|
| 780 |
struct app_ctx app_ctx; |
| 781 |
AWS_ZERO_STRUCT(app_ctx); |
| 782 |
app_ctx.allocator = allocator; |
| 783 |
app_ctx.signal = (struct aws_condition_variable)AWS_CONDITION_VARIABLE_INIT; |
| 784 |
app_ctx.connect_timeout = 3000; |
| 785 |
aws_mutex_init(&app_ctx.lock); |
| 786 |
if (app_ctx.port == 0) { |
| 787 |
app_ctx.port = 1883; |
| 788 |
} |
| 789 |
|
| 790 |
struct aws_mqtt5_canary_tester_options tester_options; |
| 791 |
AWS_ZERO_STRUCT(tester_options); |
| 792 |
s_aws_mqtt5_canary_init_tester_options(&tester_options); |
| 793 |
enum aws_mqtt5_canary_operations operations[AWS_MQTT5_CANARY_OPERATION_ARRAY_SIZE]; |
| 794 |
AWS_ZERO_STRUCT(operations); |
| 795 |
tester_options.operations = operations; |
| 796 |
|
| 797 |
s_parse_options(argc, argv, &app_ctx, &tester_options); |
| 798 |
if (app_ctx.uri.port) { |
| 799 |
app_ctx.port = app_ctx.uri.port; |
| 800 |
} |
| 801 |
|
| 802 |
s_aws_mqtt5_canary_update_tps_sleep_time(&tester_options); |
| 803 |
s_aws_mqtt5_canary_init_weighted_operations(&tester_options); |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
struct aws_logger logger; |
| 809 |
AWS_ZERO_STRUCT(logger); |
| 810 |
|
| 811 |
struct aws_logger_standard_options options = { |
| 812 |
.level = app_ctx.log_level, |
| 813 |
}; |
| 814 |
|
| 815 |
if (app_ctx.log_level) { |
| 816 |
if (app_ctx.log_filename) { |
| 817 |
options.filename = app_ctx.log_filename; |
| 818 |
} else { |
| 819 |
options.file = stderr; |
| 820 |
} |
| 821 |
if (aws_logger_init_standard(&logger, allocator, &options)) { |
| 822 |
fprintf(stderr, "Faled to initialize logger with error %s", aws_error_debug_str(aws_last_error())); |
| 823 |
exit(1); |
| 824 |
} |
| 825 |
aws_logger_set(&logger); |
| 826 |
} else { |
| 827 |
options.file = stderr; |
| 828 |
} |
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
bool use_tls = false; |
| 834 |
struct aws_tls_ctx *tls_ctx = NULL; |
| 835 |
struct aws_tls_ctx_options tls_ctx_options; |
| 836 |
AWS_ZERO_STRUCT(tls_ctx_options); |
| 837 |
struct aws_tls_connection_options tls_connection_options; |
| 838 |
AWS_ZERO_STRUCT(tls_connection_options); |
| 839 |
|
| 840 |
if (app_ctx.cert && app_ctx.key) { |
| 841 |
if (aws_tls_ctx_options_init_client_mtls_from_path(&tls_ctx_options, allocator, app_ctx.cert, app_ctx.key)) { |
| 842 |
fprintf( |
| 843 |
stderr, |
| 844 |
"Failed to load %s and %s with error %s.", |
| 845 |
app_ctx.cert, |
| 846 |
app_ctx.key, |
| 847 |
aws_error_debug_str(aws_last_error())); |
| 848 |
exit(1); |
| 849 |
} |
| 850 |
|
| 851 |
if (app_ctx.cacert) { |
| 852 |
if (aws_tls_ctx_options_override_default_trust_store_from_path(&tls_ctx_options, NULL, app_ctx.cacert)) { |
| 853 |
fprintf( |
| 854 |
stderr, "Failed to load %s with error %s", app_ctx.cacert, aws_error_debug_str(aws_last_error())); |
| 855 |
exit(1); |
| 856 |
} |
| 857 |
} |
| 858 |
|
| 859 |
if (aws_tls_ctx_options_set_alpn_list(&tls_ctx_options, "x-amzn-mqtt-ca")) { |
| 860 |
fprintf(stderr, "Failed to set alpn list with error %s.", aws_error_debug_str(aws_last_error())); |
| 861 |
exit(1); |
| 862 |
} |
| 863 |
|
| 864 |
tls_ctx = aws_tls_client_ctx_new(allocator, &tls_ctx_options); |
| 865 |
|
| 866 |
if (!tls_ctx) { |
| 867 |
fprintf(stderr, "Failed to initialize TLS context with error %s.", aws_error_debug_str(aws_last_error())); |
| 868 |
exit(1); |
| 869 |
} |
| 870 |
|
| 871 |
aws_tls_connection_options_init_from_ctx(&tls_connection_options, tls_ctx); |
| 872 |
if (aws_tls_connection_options_set_server_name(&tls_connection_options, allocator, &app_ctx.uri.host_name)) { |
| 873 |
fprintf(stderr, "Failed to set servername with error %s.", aws_error_debug_str(aws_last_error())); |
| 874 |
exit(1); |
| 875 |
} |
| 876 |
|
| 877 |
use_tls = true; |
| 878 |
} |
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
struct aws_event_loop_group *el_group = |
| 884 |
aws_event_loop_group_new_default(allocator, tester_options.elg_max_threads, NULL); |
| 885 |
|
| 886 |
struct aws_host_resolver_default_options resolver_options = { |
| 887 |
.el_group = el_group, |
| 888 |
.max_entries = 8, |
| 889 |
}; |
| 890 |
|
| 891 |
struct aws_host_resolver *resolver = aws_host_resolver_new_default(allocator, &resolver_options); |
| 892 |
|
| 893 |
struct aws_client_bootstrap_options bootstrap_options = { |
| 894 |
.event_loop_group = el_group, |
| 895 |
.host_resolver = resolver, |
| 896 |
}; |
| 897 |
|
| 898 |
struct aws_client_bootstrap *bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options); |
| 899 |
|
| 900 |
struct aws_socket_options socket_options = { |
| 901 |
.type = AWS_SOCKET_STREAM, |
| 902 |
.connect_timeout_ms = (uint32_t)app_ctx.connect_timeout, |
| 903 |
.keep_alive_timeout_sec = 0, |
| 904 |
.keepalive = false, |
| 905 |
.keep_alive_interval_sec = 0, |
| 906 |
}; |
| 907 |
|
| 908 |
uint16_t receive_maximum = 9; |
| 909 |
uint32_t maximum_packet_size = 128 * 1024; |
| 910 |
|
| 911 |
aws_mqtt5_transform_websocket_handshake_fn *websocket_handshake_transform = NULL; |
| 912 |
void *websocket_handshake_transform_user_data = NULL; |
| 913 |
if (app_ctx.use_websockets) { |
| 914 |
websocket_handshake_transform = &s_aws_mqtt5_transform_websocket_handshake_fn; |
| 915 |
} |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
struct aws_mqtt5_packet_connect_view connect_options = { |
| 922 |
.keep_alive_interval_seconds = 30, |
| 923 |
.clean_start = true, |
| 924 |
.maximum_packet_size_bytes = &maximum_packet_size, |
| 925 |
.receive_maximum = &receive_maximum, |
| 926 |
}; |
| 927 |
|
| 928 |
struct aws_mqtt5_client_options client_options = { |
| 929 |
.host_name = app_ctx.uri.host_name, |
| 930 |
.port = app_ctx.port, |
| 931 |
.bootstrap = bootstrap, |
| 932 |
.socket_options = &socket_options, |
| 933 |
.tls_options = (use_tls) ? &tls_connection_options : NULL, |
| 934 |
.connect_options = &connect_options, |
| 935 |
.session_behavior = AWS_MQTT5_CSBT_CLEAN, |
| 936 |
.lifecycle_event_handler = s_lifecycle_event_callback, |
| 937 |
.retry_jitter_mode = AWS_EXPONENTIAL_BACKOFF_JITTER_NONE, |
| 938 |
.min_reconnect_delay_ms = 1000, |
| 939 |
.max_reconnect_delay_ms = 120000, |
| 940 |
.min_connected_time_to_reset_reconnect_delay_ms = 30000, |
| 941 |
.ping_timeout_ms = 10000, |
| 942 |
.websocket_handshake_transform = websocket_handshake_transform, |
| 943 |
.websocket_handshake_transform_user_data = websocket_handshake_transform_user_data, |
| 944 |
.publish_received_handler = s_on_publish_received, |
| 945 |
.ack_timeout_seconds = 300, |
| 946 |
}; |
| 947 |
|
| 948 |
struct aws_mqtt5_canary_test_client clients[AWS_MQTT5_CANARY_CLIENT_MAX]; |
| 949 |
AWS_ZERO_STRUCT(clients); |
| 950 |
|
| 951 |
uint64_t start_time = 0; |
| 952 |
aws_high_res_clock_get_ticks(&start_time); |
| 953 |
char shared_topic_array[AWS_MQTT5_CANARY_TOPIC_ARRAY_SIZE] = ""; |
| 954 |
snprintf(shared_topic_array, sizeof shared_topic_array, "%" PRIu64 "_shared_topic", start_time); |
| 955 |
struct aws_byte_cursor shared_topic = aws_byte_cursor_from_c_str(shared_topic_array); |
| 956 |
|
| 957 |
for (size_t i = 0; i < tester_options.client_count; ++i) { |
| 958 |
|
| 959 |
client_options.lifecycle_event_handler_user_data = &clients[i]; |
| 960 |
client_options.publish_received_handler_user_data = &clients[i]; |
| 961 |
|
| 962 |
clients[i].shared_topic = shared_topic; |
| 963 |
clients[i].client = aws_mqtt5_client_new(allocator, &client_options); |
| 964 |
|
| 965 |
aws_mqtt5_canary_operation_fn *operation_fn = |
| 966 |
s_aws_mqtt5_canary_operation_table.operation_by_operation_type[AWS_MQTT5_CANARY_OPERATION_START]; |
| 967 |
(*operation_fn)(&clients[i]); |
| 968 |
|
| 969 |
aws_thread_current_sleep(AWS_MQTT5_CANARY_CLIENT_CREATION_SLEEP_TIME); |
| 970 |
} |
| 971 |
|
| 972 |
fprintf(stderr, "Clients created\n"); |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
bool done = false; |
| 978 |
size_t operations_executed = 0; |
| 979 |
uint64_t time_test_finish = 0; |
| 980 |
aws_high_res_clock_get_ticks(&time_test_finish); |
| 981 |
time_test_finish += |
| 982 |
aws_timestamp_convert(tester_options.test_run_seconds, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL); |
| 983 |
|
| 984 |
if (tester_options.test_run_seconds > 0) { |
| 985 |
printf("Running test for %zu seconds\n", tester_options.test_run_seconds); |
| 986 |
} else { |
| 987 |
printf("Running test forever\n"); |
| 988 |
} |
| 989 |
|
| 990 |
while (!done) { |
| 991 |
uint64_t now = 0; |
| 992 |
aws_high_res_clock_get_ticks(&now); |
| 993 |
operations_executed++; |
| 994 |
|
| 995 |
enum aws_mqtt5_canary_operations next_operation = s_aws_mqtt5_canary_get_random_operation(&tester_options); |
| 996 |
aws_mqtt5_canary_operation_fn *operation_fn = |
| 997 |
s_aws_mqtt5_canary_operation_table.operation_by_operation_type[next_operation]; |
| 998 |
|
| 999 |
(*operation_fn)(&clients[rand() % tester_options.client_count]); |
| 1000 |
|
| 1001 |
if (now > time_test_finish && tester_options.test_run_seconds > 0) { |
| 1002 |
done = true; |
| 1003 |
} |
| 1004 |
|
| 1005 |
aws_thread_current_sleep(tester_options.tps_sleep_time); |
| 1006 |
} |
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
for (size_t i = 0; i < tester_options.client_count; ++i) { |
| 1012 |
struct aws_mqtt5_client *client = clients[i].client; |
| 1013 |
aws_mqtt5_client_release(client); |
| 1014 |
} |
| 1015 |
|
| 1016 |
aws_client_bootstrap_release(bootstrap); |
| 1017 |
aws_host_resolver_release(resolver); |
| 1018 |
aws_event_loop_group_release(el_group); |
| 1019 |
|
| 1020 |
if (tls_ctx) { |
| 1021 |
aws_tls_connection_options_clean_up(&tls_connection_options); |
| 1022 |
aws_tls_ctx_release(tls_ctx); |
| 1023 |
aws_tls_ctx_options_clean_up(&tls_ctx_options); |
| 1024 |
} |
| 1025 |
|
| 1026 |
aws_thread_join_all_managed(); |
| 1027 |
|
| 1028 |
const size_t outstanding_bytes = aws_mem_tracer_bytes(allocator); |
| 1029 |
printf("Summary:\n"); |
| 1030 |
printf(" Outstanding bytes: %zu\n", outstanding_bytes); |
| 1031 |
|
| 1032 |
if (app_ctx.log_level) { |
| 1033 |
aws_logger_set(NULL); |
| 1034 |
aws_logger_clean_up(&logger); |
| 1035 |
} |
| 1036 |
|
| 1037 |
aws_uri_clean_up(&app_ctx.uri); |
| 1038 |
|
| 1039 |
aws_mqtt_library_clean_up(); |
| 1040 |
|
| 1041 |
printf(" Operations executed: %zu\n", operations_executed); |
| 1042 |
printf(" Operating TPS average over test: %zu\n\n", operations_executed / tester_options.test_run_seconds); |
| 1043 |
|
| 1044 |
const size_t leaked_bytes = aws_mem_tracer_bytes(allocator); |
| 1045 |
if (leaked_bytes) { |
| 1046 |
struct aws_logger memory_logger; |
| 1047 |
AWS_ZERO_STRUCT(memory_logger); |
| 1048 |
|
| 1049 |
aws_logger_init_noalloc(&memory_logger, aws_default_allocator(), &options); |
| 1050 |
aws_logger_set(&memory_logger); |
| 1051 |
|
| 1052 |
aws_mqtt_library_init(aws_default_allocator()); |
| 1053 |
|
| 1054 |
printf("Writing memory leaks to log.\n"); |
| 1055 |
aws_mem_tracer_dump(allocator); |
| 1056 |
|
| 1057 |
aws_logger_set(NULL); |
| 1058 |
aws_logger_clean_up(&memory_logger); |
| 1059 |
|
| 1060 |
aws_mqtt_library_clean_up(); |
| 1061 |
} else { |
| 1062 |
printf("Finished, with no memory leaks\n"); |
| 1063 |
} |
| 1064 |
|
| 1065 |
aws_mem_tracer_destroy(allocator); |
| 1066 |
|
| 1067 |
return 0; |
| 1068 |
} |