Debian Codegraph @

crt/aws-c-mqtt/tests/v3-client/aws_iot_client_test.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
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/logging.h>
15 #include <aws/io/socket.h>
16 #include <aws/io/socket_channel_handler.h>
17 #include <aws/io/tls_channel_handler.h>
18
19 #include <aws/common/condition_variable.h>
20 #include <aws/common/device_random.h>
21 #include <aws/common/mutex.h>
22 #include <aws/common/string.h>
23 #include <aws/common/thread.h>
24 #include <aws/common/uuid.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #ifdef WIN32
29 # include <windows.h>
30 # define sleep Sleep
31 #else
32 # include <unistd.h>
33 #endif
34
35 const char s_client_id_prefix[] = "sdk-c-v2-";
36 AWS_STATIC_STRING_FROM_LITERAL(s_subscribe_topic, "sdk/test/c");
37
38 enum { PUBLISHES = 20 };
39
40 enum { PAYLOAD_LEN = 200 };
41 static uint8_t s_payload[PAYLOAD_LEN];
42
43 static uint8_t s_will_payload[] = "The client has gone offline!";
44 enum { WILL_PAYLOAD_LEN = sizeof(s_will_payload) };
45
46 struct test_context {
47 struct aws_allocator *allocator;
48
49 struct aws_mutex lock;
50 struct aws_condition_variable signal;
51
52 struct aws_logger logger;
53 struct aws_tls_ctx *tls_ctx;
54 struct aws_event_loop_group *el_group;
55 struct aws_host_resolver *resolver;
56 struct aws_client_bootstrap *bootstrap;
57 struct aws_mqtt_client *client;
58 struct aws_mqtt_client_connection *connection;
59 struct aws_tls_connection_options tls_connection_options;
60
61 size_t pubacks_gotten;
62 size_t packets_gotten;
63
64 bool received_on_connection_complete;
65 bool received_on_suback;
66 bool received_on_disconnect;
67 };
68
69 static void s_on_puback(
70 struct aws_mqtt_client_connection *connection,
71 uint16_t packet_id,
72 int error_code,
73 void *userdata) {
74
75 (void)connection;
76 (void)packet_id;
77 (void)error_code;
78
79 AWS_FATAL_ASSERT(error_code == AWS_ERROR_SUCCESS);
80
81 struct test_context *tester = userdata;
82
83 aws_mutex_lock(&tester->lock);
84 ++tester->pubacks_gotten;
85 aws_mutex_unlock(&tester->lock);
86 }
87
88 static void s_on_packet_received(
89 struct aws_mqtt_client_connection *connection,
90 const struct aws_byte_cursor *topic,
91 const struct aws_byte_cursor *payload,
92 bool dup,
93 enum aws_mqtt_qos qos,
94 bool retain,
95 void *userdata) {
96
97 (void)connection;
98 (void)topic;
99 (void)dup;
100 (void)qos;
101 (void)retain;
102
103 AWS_FATAL_ASSERT(payload->len == PAYLOAD_LEN);
104 AWS_FATAL_ASSERT(0 == memcmp(payload->ptr, s_payload, PAYLOAD_LEN));
105
106 bool notify = false;
107 struct test_context *tester = userdata;
108
109 aws_mutex_lock(&tester->lock);
110 ++tester->packets_gotten;
111 if (tester->packets_gotten == PUBLISHES) {
112 notify = true;
113 }
114 aws_mutex_unlock(&tester->lock);
115
116 if (notify) {
117 aws_condition_variable_notify_one(&tester->signal);
118 }
119 }
120
121 static bool s_all_packets_received_cond(void *userdata) {
122 struct test_context *tester = userdata;
123 return tester->packets_gotten == PUBLISHES;
124 }
125
126 static void s_mqtt_on_connection_complete(
127 struct aws_mqtt_client_connection *connection,
128 int error_code,
129 enum aws_mqtt_connect_return_code return_code,
130 bool session_present,
131 void *userdata) {
132
133 (void)connection;
134 (void)error_code;
135 (void)return_code;
136 (void)session_present;
137
138 AWS_FATAL_ASSERT(error_code == AWS_ERROR_SUCCESS);
139 AWS_FATAL_ASSERT(return_code == AWS_MQTT_CONNECT_ACCEPTED);
140 AWS_FATAL_ASSERT(session_present == false);
141
142 struct test_context *tester = userdata;
143
144 aws_mutex_lock(&tester->lock);
145 tester->received_on_connection_complete = true;
146 aws_mutex_unlock(&tester->lock);
147 aws_condition_variable_notify_one(&tester->signal);
148 }
149
150 static void s_mqtt_on_suback(
151 struct aws_mqtt_client_connection *connection,
152 uint16_t packet_id,
153 const struct aws_byte_cursor *topic,
154 enum aws_mqtt_qos qos,
155 int error_code,
156 void *userdata) {
157
158 (void)connection;
159 (void)packet_id;
160 (void)topic;
161 (void)qos;
162 (void)error_code;
163
164 AWS_FATAL_ASSERT(error_code == AWS_ERROR_SUCCESS);
165 AWS_FATAL_ASSERT(qos != AWS_MQTT_QOS_FAILURE);
166
167 struct test_context *tester = userdata;
168
169 aws_mutex_lock(&tester->lock);
170 tester->received_on_suback = true;
171 aws_mutex_unlock(&tester->lock);
172 aws_condition_variable_notify_one(&tester->signal);
173 }
174
175 static void s_on_connection_interrupted(struct aws_mqtt_client_connection *connection, int error_code, void *userdata) {
176
177 (void)connection;
178 (void)userdata;
179 printf("CONNECTION INTERRUPTED error_code=%d\n", error_code);
180 }
181
182 static void s_on_resubscribed(
183 struct aws_mqtt_client_connection *connection,
184 uint16_t packet_id,
185 const struct aws_array_list *topic_subacks, /* contains aws_mqtt_topic_subscription pointers */
186 int error_code,
187 void *userdata) {
188
189 (void)connection;
190 (void)packet_id;
191 (void)userdata;
192
193 AWS_FATAL_ASSERT(error_code == AWS_ERROR_SUCCESS);
194
195 size_t num_topics = aws_array_list_length(topic_subacks);
196 printf("RESUBSCRIBE_COMPLETE. error_code=%d num_topics=%zu\n", error_code, num_topics);
197 for (size_t i = 0; i < num_topics; ++i) {
198 struct aws_mqtt_topic_subscription sub_i;
199 aws_array_list_get_at(topic_subacks, &sub_i, i);
200 printf(" topic=" PRInSTR " qos=%d\n", AWS_BYTE_CURSOR_PRI(sub_i.topic), sub_i.qos);
201 AWS_FATAL_ASSERT(sub_i.qos != AWS_MQTT_QOS_FAILURE);
202 }
203 }
204
205 static void s_on_connection_resumed(
206 struct aws_mqtt_client_connection *connection,
207 enum aws_mqtt_connect_return_code return_code,
208 bool session_present,
209 void *userdata) {
210
211 (void)connection;
212 (void)userdata;
213
214 printf("CONNECTION RESUMED return_code=%d session_present=%d\n", return_code, session_present);
215 if (!session_present) {
216 printf("RESUBSCRIBING...");
217 uint16_t packet_id = aws_mqtt_resubscribe_existing_topics(connection, s_on_resubscribed, NULL);
218 AWS_FATAL_ASSERT(packet_id);
219 }
220 }
221
222 static void s_mqtt_on_disconnect(struct aws_mqtt_client_connection *connection, void *userdata) {
223 (void)connection;
224
225 struct test_context *tester = userdata;
226
227 aws_mutex_lock(&tester->lock);
228 tester->received_on_disconnect = true;
229 aws_mutex_unlock(&tester->lock);
230 aws_condition_variable_notify_one(&tester->signal);
231 }
232
233 static void s_wait_on_tester_predicate(struct test_context *tester, bool (*predicate)(void *)) {
234 aws_mutex_lock(&tester->lock);
235 aws_condition_variable_wait_pred(&tester->signal, &tester->lock, predicate, tester);
236 aws_mutex_unlock(&tester->lock);
237 }
238
239 static bool s_received_on_disconnect_pred(void *user_data) {
240 struct test_context *tester = user_data;
241 return tester->received_on_disconnect;
242 }
243
244 static bool s_received_on_suback_pred(void *user_data) {
245 struct test_context *tester = user_data;
246 return tester->received_on_suback;
247 }
248
249 static bool s_connection_complete_pred(void *user_data) {
250 struct test_context *tester = user_data;
251 return tester->received_on_connection_complete;
252 }
253
254 int s_initialize_test(
255 struct test_context *tester,
256 struct aws_allocator *allocator,
257 const char *cert,
258 const char *private_key,
259 const char *root_ca,
260 const char *endpoint) {
261 aws_mqtt_library_init(allocator);
262
263 struct aws_logger_standard_options logger_options = {
264 .level = AWS_LL_TRACE,
265 .file = stdout,
266 };
267
268 aws_logger_init_standard(&tester->logger, allocator, &logger_options);
269 aws_logger_set(&tester->logger);
270
271 tester->allocator = allocator;
272 aws_mutex_init(&tester->lock);
273 aws_condition_variable_init(&tester->signal);
274
275 tester->el_group = aws_event_loop_group_new_default(allocator, 1, NULL);
276
277 struct aws_host_resolver_default_options resolver_options = {
278 .el_group = tester->el_group,
279 .max_entries = 8,
280 };
281 tester->resolver = aws_host_resolver_new_default(allocator, &resolver_options);
282
283 struct aws_client_bootstrap_options bootstrap_options = {
284 .event_loop_group = tester->el_group,
285 .host_resolver = tester->resolver,
286 };
287 tester->bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
288
289 struct aws_tls_ctx_options tls_ctx_opt;
290 AWS_FATAL_ASSERT(
291 AWS_OP_SUCCESS == aws_tls_ctx_options_init_client_mtls_from_path(&tls_ctx_opt, allocator, cert, private_key));
292 AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_tls_ctx_options_set_alpn_list(&tls_ctx_opt, "x-amzn-mqtt-ca"));
293 AWS_FATAL_ASSERT(
294 AWS_OP_SUCCESS == aws_tls_ctx_options_override_default_trust_store_from_path(&tls_ctx_opt, NULL, root_ca));
295
296 tester->tls_ctx = aws_tls_client_ctx_new(allocator, &tls_ctx_opt);
297 AWS_FATAL_ASSERT(tester->tls_ctx != NULL);
298
299 aws_tls_ctx_options_clean_up(&tls_ctx_opt);
300
301 aws_tls_connection_options_init_from_ctx(&tester->tls_connection_options, tester->tls_ctx);
302
303 tester->client = aws_mqtt_client_new(allocator, tester->bootstrap);
304 tester->connection = aws_mqtt_client_connection_new(tester->client);
305
306 struct aws_socket_options socket_options;
307 AWS_ZERO_STRUCT(socket_options);
308 socket_options.connect_timeout_ms = 3000;
309 socket_options.type = AWS_SOCKET_STREAM;
310 socket_options.domain = AWS_SOCKET_IPV6;
311
312 struct aws_byte_cursor host_name_cur = aws_byte_cursor_from_c_str(endpoint);
313
314 /* Generate a random clientid */
315 char client_id[128];
316 struct aws_byte_buf client_id_buf = aws_byte_buf_from_empty_array(client_id, AWS_ARRAY_SIZE(client_id));
317
318 aws_byte_buf_write(&client_id_buf, (const uint8_t *)s_client_id_prefix, AWS_ARRAY_SIZE(s_client_id_prefix));
319
320 struct aws_uuid uuid;
321 aws_uuid_init(&uuid);
322 aws_uuid_to_str(&uuid, &client_id_buf);
323
324 struct aws_byte_cursor client_id_cur = aws_byte_cursor_from_buf(&client_id_buf);
325
326 struct aws_mqtt_connection_options conn_options = {
327 .host_name = host_name_cur,
328 .port = 8883,
329 .socket_options = &socket_options,
330 .tls_options = &tester->tls_connection_options,
331 .client_id = client_id_cur,
332 .keep_alive_time_secs = 0,
333 .ping_timeout_ms = 0,
334 .on_connection_complete = s_mqtt_on_connection_complete,
335 .user_data = tester,
336 .clean_session = true,
337 };
338
339 AWS_FATAL_ASSERT(
340 AWS_OP_SUCCESS == aws_mqtt_client_connection_set_connection_interruption_handlers(
341 tester->connection, s_on_connection_interrupted, NULL, s_on_connection_resumed, NULL));
342
343 struct aws_byte_cursor subscribe_topic_cur = aws_byte_cursor_from_string(s_subscribe_topic);
344 struct aws_byte_cursor will_cur = aws_byte_cursor_from_array(s_will_payload, WILL_PAYLOAD_LEN);
345 aws_mqtt_client_connection_set_will(tester->connection, &subscribe_topic_cur, 1, false, &will_cur);
346
347 aws_mqtt_client_connection_connect(tester->connection, &conn_options);
348
349 return AWS_OP_SUCCESS;
350 }
351
352 static void s_cleanup_test(struct test_context *tester) {
353 aws_tls_connection_options_clean_up(&tester->tls_connection_options);
354
355 aws_mqtt_client_connection_release(tester->connection);
356 aws_mqtt_client_release(tester->client);
357
358 aws_tls_ctx_release(tester->tls_ctx);
359
360 aws_client_bootstrap_release(tester->bootstrap);
361
362 aws_host_resolver_release(tester->resolver);
363 aws_event_loop_group_release(tester->el_group);
364
365 aws_thread_join_all_managed();
366
367 aws_logger_clean_up(&tester->logger);
368
369 aws_mutex_clean_up(&tester->lock);
370 aws_condition_variable_clean_up(&tester->signal);
371
372 aws_mqtt_library_clean_up();
373 }
374
375 int main(int argc, char **argv) {
376
377 if (argc < 5) {
378 printf(
379 "4 args required, only %d passed. Usage:\n"
380 "aws-c-mqtt-iot-client [endpoint] [certificate] [private_key] [root_ca]\n",
381 argc - 1);
382 return 1;
383 }
384
385 const char *endpoint = argv[1];
386 const char *cert = argv[2];
387 const char *private_key = argv[3];
388 const char *root_ca = argv[4];
389
390 struct aws_allocator *allocator = aws_mem_tracer_new(aws_default_allocator(), NULL, AWS_MEMTRACE_BYTES, 0);
391
392 struct test_context tester;
393 AWS_ZERO_STRUCT(tester);
394
395 AWS_FATAL_ASSERT(s_initialize_test(&tester, allocator, cert, private_key, root_ca, endpoint) == AWS_OP_SUCCESS);
396
397 struct aws_byte_cursor subscribe_topic_cur = aws_byte_cursor_from_string(s_subscribe_topic);
398
399 s_wait_on_tester_predicate(&tester, s_connection_complete_pred);
400
401 aws_mqtt_client_connection_subscribe(
402 tester.connection,
403 &subscribe_topic_cur,
404 AWS_MQTT_QOS_AT_LEAST_ONCE,
405 &s_on_packet_received,
406 &tester,
407 NULL,
408 s_mqtt_on_suback,
409 &tester);
410
411 s_wait_on_tester_predicate(&tester, s_received_on_suback_pred);
412
413 /* Populate the payload */
414 struct aws_byte_buf payload_buf = aws_byte_buf_from_empty_array(s_payload, PAYLOAD_LEN);
415 aws_device_random_buffer(&payload_buf);
416 struct aws_byte_cursor payload_cur = aws_byte_cursor_from_buf(&payload_buf);
417
418 for (int i = 0; i < PUBLISHES; ++i) {
419 aws_mqtt_client_connection_publish(
420 tester.connection,
421 &subscribe_topic_cur,
422 AWS_MQTT_QOS_AT_LEAST_ONCE,
423 false,
424 &payload_cur,
425 &s_on_puback,
426 &tester);
427
428 /* Keep the service endpoint from throttling the connection */
429 if (i != 0 && i % 100 == 0) {
430 sleep(1);
431 }
432 }
433
434 s_wait_on_tester_predicate(&tester, s_all_packets_received_cond);
435
436 AWS_FATAL_ASSERT(PUBLISHES == tester.packets_gotten);
437
438 aws_mqtt_client_connection_disconnect(tester.connection, s_mqtt_on_disconnect, &tester);
439
440 s_wait_on_tester_predicate(&tester, s_received_on_disconnect_pred);
441
442 s_cleanup_test(&tester);
443
444 AWS_FATAL_ASSERT(0 == aws_mem_tracer_count(allocator));
445 allocator = aws_mem_tracer_destroy(allocator);
446
447 return AWS_OP_SUCCESS;
448 }