Debian Codegraph @

crt/aws-c-mqtt/tests/v3/connection_state_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/mqtt.h>
7
8 #include "mqtt311_testing_utils.h"
9 #include "mqtt_mock_server_handler.h"
10
11 #include <aws/common/clock.h>
12 #include <aws/mqtt/private/client_impl.h>
13 #include <aws/testing/aws_test_harness.h>
14
15 static struct mqtt_connection_state_test test_data = {0};
16
17 static int s_setup_mqtt_server_fn(struct aws_allocator *allocator, void *ctx) {
18 return aws_test311_setup_mqtt_server_fn(allocator, ctx);
19 }
20
21 static int s_clean_up_mqtt_server_fn(struct aws_allocator *allocator, int setup_result, void *ctx) {
22 return aws_test311_clean_up_mqtt_server_fn(allocator, setup_result, ctx);
23 }
24
25 /*
26 * Makes an Mqtt connect call, then a disconnect. Then verifies a CONNECT and DISCONNECT were sent.
27 */
28 static int s_test_mqtt_connect_disconnect_fn(struct aws_allocator *allocator, void *ctx) {
29 (void)allocator;
30 struct mqtt_connection_state_test *state_test_data = ctx;
31
32 struct aws_mqtt_connection_options connection_options = {
33 .user_data = state_test_data,
34 .clean_session = false,
35 .client_id = aws_byte_cursor_from_c_str("client1234"),
36 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
37 .socket_options = &state_test_data->socket_options,
38 .on_connection_complete = aws_test311_on_connection_complete_fn,
39 };
40
41 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
42 aws_test311_wait_for_connection_to_complete(state_test_data);
43 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
44 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
45 aws_test311_wait_for_disconnect_to_complete(state_test_data);
46
47 /* Decode all received packets by mock server */
48 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
49
50 ASSERT_UINT_EQUALS(2, mqtt_mock_server_decoded_packets_count(state_test_data->mock_server));
51 struct mqtt_decoded_packet *received_packet =
52 mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 0);
53 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
54 ASSERT_UINT_EQUALS(connection_options.clean_session, received_packet->clean_session);
55 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
56
57 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 1);
58 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_DISCONNECT, received_packet->type);
59
60 return AWS_OP_SUCCESS;
61 }
62
63 AWS_TEST_CASE_FIXTURE(
64 mqtt_connect_disconnect,
65 s_setup_mqtt_server_fn,
66 s_test_mqtt_connect_disconnect_fn,
67 s_clean_up_mqtt_server_fn,
68 &test_data)
69
70 /*
71 * Makes an Mqtt connect call, and set will and login information for the connection. Validate the those information are
72 * correctly included in the CONNECT package.
73 */
74 static int s_test_mqtt_connect_set_will_login_fn(struct aws_allocator *allocator, void *ctx) {
75 (void)allocator;
76 struct mqtt_connection_state_test *state_test_data = ctx;
77
78 struct aws_byte_cursor will_payload = aws_byte_cursor_from_c_str("this is a will.");
79 struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("test_topic");
80 struct aws_byte_cursor username = aws_byte_cursor_from_c_str("user name");
81 struct aws_byte_cursor password = aws_byte_cursor_from_c_str("password");
82 enum aws_mqtt_qos will_qos = AWS_MQTT_QOS_AT_LEAST_ONCE;
83
84 ASSERT_SUCCESS(aws_mqtt_client_connection_set_will(
85 state_test_data->mqtt_connection, &topic, will_qos, true /*retain*/, &will_payload));
86
87 ASSERT_SUCCESS(aws_mqtt_client_connection_set_login(state_test_data->mqtt_connection, &username, &password));
88
89 struct aws_mqtt_connection_options connection_options = {
90 .user_data = state_test_data,
91 .clean_session = false,
92 .client_id = aws_byte_cursor_from_c_str("client1234"),
93 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
94 .socket_options = &state_test_data->socket_options,
95 .on_connection_complete = aws_test311_on_connection_complete_fn,
96 };
97
98 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
99 aws_test311_wait_for_connection_to_complete(state_test_data);
100 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
101 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
102 aws_test311_wait_for_disconnect_to_complete(state_test_data);
103
104 /* Decode all received packets by mock server */
105 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
106
107 ASSERT_UINT_EQUALS(2, mqtt_mock_server_decoded_packets_count(state_test_data->mock_server));
108
109 /* CONNECT packet */
110 struct mqtt_decoded_packet *received_packet =
111 mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 0);
112 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
113 ASSERT_UINT_EQUALS(connection_options.clean_session, received_packet->clean_session);
114 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
115 /* validate the received will */
116 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->will_message, &will_payload));
117 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->will_topic, &topic));
118 ASSERT_UINT_EQUALS(will_qos, received_packet->will_qos);
119 ASSERT_TRUE(true == received_packet->will_retain);
120 /* validate the received login information */
121 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->username, &username));
122 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->password, &password));
123
124 /* DISCONNECT packet */
125 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 1);
126 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_DISCONNECT, received_packet->type);
127
128 /* Connect to the mock server again. If set will&loggin message is not called before the next connect, the
129 * will&loggin message will still be there and be sent to the server again */
130 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
131 aws_test311_wait_for_connection_to_complete(state_test_data);
132 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
133
134 /* The second CONNECT packet */
135 received_packet = mqtt_mock_server_get_latest_decoded_packet(state_test_data->mock_server);
136 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
137 ASSERT_UINT_EQUALS(connection_options.clean_session, received_packet->clean_session);
138 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
139 /* validate the received will */
140 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->will_message, &will_payload));
141 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->will_topic, &topic));
142 ASSERT_UINT_EQUALS(will_qos, received_packet->will_qos);
143 ASSERT_TRUE(true == received_packet->will_retain);
144 /* validate the received login information */
145 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->username, &username));
146 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->password, &password));
147
148 /* disconnect */
149 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
150 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
151 aws_test311_wait_for_disconnect_to_complete(state_test_data);
152
153 /* set new will & loggin message, before next connect, the next CONNECT packet will contain the new information */
154 struct aws_byte_cursor new_will_payload = aws_byte_cursor_from_c_str("this is a new will.");
155 struct aws_byte_cursor new_topic = aws_byte_cursor_from_c_str("test_topic_New");
156 struct aws_byte_cursor new_username = aws_byte_cursor_from_c_str("new user name");
157 struct aws_byte_cursor new_password = aws_byte_cursor_from_c_str("new password");
158 enum aws_mqtt_qos new_will_qos = AWS_MQTT_QOS_AT_MOST_ONCE;
159
160 ASSERT_SUCCESS(aws_mqtt_client_connection_set_will(
161 state_test_data->mqtt_connection, &new_topic, new_will_qos, true /*retain*/, &new_will_payload));
162
163 ASSERT_SUCCESS(
164 aws_mqtt_client_connection_set_login(state_test_data->mqtt_connection, &new_username, &new_password));
165
166 /* connect again */
167 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
168 aws_test311_wait_for_connection_to_complete(state_test_data);
169 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
170
171 /* The third CONNECT packet */
172 received_packet = mqtt_mock_server_get_latest_decoded_packet(state_test_data->mock_server);
173 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
174 ASSERT_UINT_EQUALS(connection_options.clean_session, received_packet->clean_session);
175 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
176 /* validate the received will */
177 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->will_message, &new_will_payload));
178 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->will_topic, &new_topic));
179 ASSERT_UINT_EQUALS(new_will_qos, received_packet->will_qos);
180 ASSERT_TRUE(true == received_packet->will_retain);
181 /* validate the received login information */
182 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->username, &new_username));
183 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->password, &new_password));
184
185 /* disconnect. FINISHED */
186 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
187 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
188 aws_test311_wait_for_disconnect_to_complete(state_test_data);
189
190 return AWS_OP_SUCCESS;
191 }
192
193 AWS_TEST_CASE_FIXTURE(
194 mqtt_connect_set_will_login,
195 s_setup_mqtt_server_fn,
196 s_test_mqtt_connect_set_will_login_fn,
197 s_clean_up_mqtt_server_fn,
198 &test_data)
199
200 #define MIN_RECONNECT_DELAY_SECONDS 5
201 #define MAX_RECONNECT_DELAY_SECONDS 120
202
203 /*
204 * Makes a CONNECT, then the server hangs up, tests that the client reconnects on its own, then sends a DISCONNECT.
205 * Also checks that the minimum reconnect time delay is honored.
206 */
207 static int s_test_mqtt_connection_interrupted_fn(struct aws_allocator *allocator, void *ctx) {
208 (void)allocator;
209 struct mqtt_connection_state_test *state_test_data = ctx;
210
211 struct aws_mqtt_connection_options connection_options = {
212 .user_data = state_test_data,
213 .clean_session = true,
214 .client_id = aws_byte_cursor_from_c_str("client1234"),
215 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
216 .socket_options = &state_test_data->socket_options,
217 .on_connection_complete = aws_test311_on_connection_complete_fn,
218 };
219
220 aws_mqtt_client_connection_set_reconnect_timeout(
221 state_test_data->mqtt_connection, MIN_RECONNECT_DELAY_SECONDS, MAX_RECONNECT_DELAY_SECONDS);
222
223 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
224 aws_test311_wait_for_connection_to_complete(state_test_data);
225
226 /* shut it down and make sure the client automatically reconnects.*/
227 uint64_t now = 0;
228 aws_high_res_clock_get_ticks(&now);
229 uint64_t start_shutdown = now;
230
231 aws_channel_shutdown(state_test_data->server_channel, AWS_OP_SUCCESS);
232 aws_test311_wait_for_reconnect_to_complete(state_test_data);
233
234 aws_high_res_clock_get_ticks(&now);
235 uint64_t reconnect_complete = now;
236
237 uint64_t elapsed_time = reconnect_complete - start_shutdown;
238 ASSERT_TRUE(
239 aws_timestamp_convert(elapsed_time, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, NULL) >=
240 MIN_RECONNECT_DELAY_SECONDS);
241
242 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
243 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
244 aws_test311_wait_for_disconnect_to_complete(state_test_data);
245
246 /* Decode all received packets by mock server */
247 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
248
249 ASSERT_UINT_EQUALS(3, mqtt_mock_server_decoded_packets_count(state_test_data->mock_server));
250 struct mqtt_decoded_packet *received_packet =
251 mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 0);
252 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
253 ASSERT_UINT_EQUALS(connection_options.clean_session, received_packet->clean_session);
254 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
255
256 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 1);
257 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
258 ASSERT_UINT_EQUALS(connection_options.clean_session, received_packet->clean_session);
259 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
260
261 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 2);
262 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_DISCONNECT, received_packet->type);
263
264 return AWS_OP_SUCCESS;
265 }
266
267 AWS_TEST_CASE_FIXTURE(
268 mqtt_connection_interrupted,
269 s_setup_mqtt_server_fn,
270 s_test_mqtt_connection_interrupted_fn,
271 s_clean_up_mqtt_server_fn,
272 &test_data)
273
274 /* Makes a CONNECT, with a 1 second keep alive ping interval, the mock is configured to not reply to the PING,
275 * this should cause a timeout, then the PING responses are turned back on, and the client should automatically
276 * reconnect. Then send a DISCONNECT. */
277 static int s_test_mqtt_connection_timeout_fn(struct aws_allocator *allocator, void *ctx) {
278 (void)allocator;
279 struct mqtt_connection_state_test *state_test_data = ctx;
280
281 struct aws_mqtt_connection_options connection_options = {
282 .user_data = state_test_data,
283 .clean_session = true,
284 .client_id = aws_byte_cursor_from_c_str("client1234"),
285 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
286 .socket_options = &state_test_data->socket_options,
287 .on_connection_complete = aws_test311_on_connection_complete_fn,
288 .keep_alive_time_secs = DEFAULT_TEST_KEEP_ALIVE_S,
289 .ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS,
290 };
291
292 mqtt_mock_server_set_max_ping_resp(state_test_data->mock_server, 0);
293 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
294 aws_test311_wait_for_connection_to_complete(state_test_data);
295
296 /* this should take about 1.1 seconds for the timeout and reconnect.*/
297 aws_test311_wait_for_reconnect_to_complete(state_test_data);
298
299 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
300 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
301 aws_test311_wait_for_disconnect_to_complete(state_test_data);
302
303 ASSERT_INT_EQUALS(AWS_ERROR_MQTT_TIMEOUT, state_test_data->interruption_error);
304
305 return AWS_OP_SUCCESS;
306 }
307
308 AWS_TEST_CASE_FIXTURE(
309 mqtt_connection_timeout,
310 s_setup_mqtt_server_fn,
311 s_test_mqtt_connection_timeout_fn,
312 s_clean_up_mqtt_server_fn,
313 &test_data)
314
315 /* Test set on_any_publish handler. User can set on_any_publish handler to be called whenever any publish packet is
316 * received */
317 static int s_test_mqtt_connection_any_publish_fn(struct aws_allocator *allocator, void *ctx) {
318 (void)allocator;
319 struct mqtt_connection_state_test *state_test_data = ctx;
320
321 struct aws_mqtt_connection_options connection_options = {
322 .user_data = state_test_data,
323 .clean_session = false,
324 .client_id = aws_byte_cursor_from_c_str("client1234"),
325 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
326 .socket_options = &state_test_data->socket_options,
327 .on_connection_complete = aws_test311_on_connection_complete_fn,
328 };
329
330 struct aws_byte_cursor topic_1 = aws_byte_cursor_from_c_str("/test/topic1");
331 struct aws_byte_cursor topic_2 = aws_byte_cursor_from_c_str("/test/topic2");
332
333 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
334 aws_test311_wait_for_connection_to_complete(state_test_data);
335
336 /* NOTE: mock server sends to client with no subscription at all, which should not happen in the real world! */
337 state_test_data->expected_any_publishes = 2;
338 struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
339 ASSERT_SUCCESS(mqtt_mock_server_send_publish(
340 state_test_data->mock_server,
341 &topic_1,
342 &payload_1,
343 false /*dup*/,
344 AWS_MQTT_QOS_AT_LEAST_ONCE,
345 false /*retain*/));
346 struct aws_byte_cursor payload_2 = aws_byte_cursor_from_c_str("Test Message 2");
347 ASSERT_SUCCESS(mqtt_mock_server_send_publish(
348 state_test_data->mock_server,
349 &topic_2,
350 &payload_2,
351 false /*dup*/,
352 AWS_MQTT_QOS_AT_LEAST_ONCE,
353 false /*retain*/));
354
355 aws_test311_wait_for_any_publish(state_test_data);
356 mqtt_mock_server_wait_for_pubacks(state_test_data->mock_server, 2);
357
358 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
359 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
360 aws_test311_wait_for_disconnect_to_complete(state_test_data);
361
362 /* Decode all received packets by mock server */
363 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
364
365 /* CONNECT two PUBACK DISCONNECT */
366 ASSERT_UINT_EQUALS(4, mqtt_mock_server_decoded_packets_count(state_test_data->mock_server));
367 struct mqtt_decoded_packet *received_packet =
368 mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 0);
369 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
370 ASSERT_UINT_EQUALS(connection_options.clean_session, received_packet->clean_session);
371 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
372
373 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 1);
374 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBACK, received_packet->type);
375
376 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 2);
377 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBACK, received_packet->type);
378
379 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 3);
380 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_DISCONNECT, received_packet->type);
381
382 /* Check the received publish packet from the client side */
383 ASSERT_UINT_EQUALS(2, aws_array_list_length(&state_test_data->any_published_messages));
384 struct received_publish_packet *publish_msg = NULL;
385 ASSERT_SUCCESS(aws_array_list_get_at_ptr(&state_test_data->any_published_messages, (void **)&publish_msg, 0));
386 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&topic_1, &publish_msg->topic));
387 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&payload_1, &publish_msg->payload));
388 ASSERT_SUCCESS(aws_array_list_get_at_ptr(&state_test_data->any_published_messages, (void **)&publish_msg, 1));
389 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&topic_2, &publish_msg->topic));
390 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&payload_2, &publish_msg->payload));
391
392 return AWS_OP_SUCCESS;
393 }
394
395 AWS_TEST_CASE_FIXTURE(
396 mqtt_connection_any_publish,
397 s_setup_mqtt_server_fn,
398 s_test_mqtt_connection_any_publish_fn,
399 s_clean_up_mqtt_server_fn,
400 &test_data)
401
402 /* Makes a CONNECT, channel is successfully setup, but the server never sends a connack, make sure we timeout. */
403 static int s_test_mqtt_connection_connack_timeout_fn(struct aws_allocator *allocator, void *ctx) {
404 (void)allocator;
405 struct mqtt_connection_state_test *state_test_data = ctx;
406
407 struct aws_mqtt_connection_options connection_options = {
408 .user_data = state_test_data,
409 .clean_session = true,
410 .client_id = aws_byte_cursor_from_c_str("client1234"),
411 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
412 .socket_options = &state_test_data->socket_options,
413 .on_connection_complete = aws_test311_on_connection_complete_fn,
414 .keep_alive_time_secs = DEFAULT_TEST_KEEP_ALIVE_S,
415 .ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS,
416 };
417
418 mqtt_mock_server_set_max_connack(state_test_data->mock_server, 0);
419 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
420 aws_test311_wait_for_connection_to_complete(state_test_data);
421
422 ASSERT_INT_EQUALS(AWS_ERROR_MQTT_TIMEOUT, state_test_data->error);
423
424 return AWS_OP_SUCCESS;
425 }
426
427 AWS_TEST_CASE_FIXTURE(
428 mqtt_connection_connack_timeout,
429 s_setup_mqtt_server_fn,
430 s_test_mqtt_connection_connack_timeout_fn,
431 s_clean_up_mqtt_server_fn,
432 &test_data)
433
434 /* Use the connack timeout to test the connection failure callback */
435 static int s_test_mqtt_connection_failure_callback_fn(struct aws_allocator *allocator, void *ctx) {
436 (void)allocator;
437 struct mqtt_connection_state_test *state_test_data = ctx;
438
439 struct aws_mqtt_connection_options connection_options = {
440 .user_data = state_test_data,
441 .clean_session = true,
442 .client_id = aws_byte_cursor_from_c_str("client1234"),
443 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
444 .socket_options = &state_test_data->socket_options,
445 .on_connection_complete = aws_test311_on_connection_complete_fn,
446 .keep_alive_time_secs = DEFAULT_TEST_KEEP_ALIVE_S,
447 .ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS,
448 };
449
450 mqtt_mock_server_set_max_connack(state_test_data->mock_server, 0);
451 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
452 aws_test311_wait_for_connection_to_fail(state_test_data);
453
454 ASSERT_INT_EQUALS(AWS_ERROR_MQTT_TIMEOUT, state_test_data->error);
455
456 return AWS_OP_SUCCESS;
457 }
458
459 AWS_TEST_CASE_FIXTURE(
460 mqtt_connection_failure_callback,
461 s_setup_mqtt_server_fn,
462 s_test_mqtt_connection_failure_callback_fn,
463 s_clean_up_mqtt_server_fn,
464 &test_data)
465
466 /* Quick test the connection succeed callback */
467 static int s_test_mqtt_connection_success_callback_fn(struct aws_allocator *allocator, void *ctx) {
468 (void)allocator;
469 struct mqtt_connection_state_test *state_test_data = ctx;
470
471 struct aws_mqtt_connection_options connection_options = {
472 .user_data = state_test_data,
473 .clean_session = false,
474 .client_id = aws_byte_cursor_from_c_str("client1234"),
475 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
476 .socket_options = &state_test_data->socket_options,
477 .on_connection_complete = aws_test311_on_connection_complete_fn,
478 };
479
480 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
481 aws_test311_wait_for_connection_to_succeed(state_test_data);
482
483 /* Decode all received packets by mock server */
484 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
485
486 ASSERT_UINT_EQUALS(1, mqtt_mock_server_decoded_packets_count(state_test_data->mock_server));
487 struct mqtt_decoded_packet *received_packet =
488 mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 0);
489 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
490 ASSERT_UINT_EQUALS(connection_options.clean_session, received_packet->clean_session);
491 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
492
493 // Disconnect and finish
494 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
495 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
496 aws_test311_wait_for_disconnect_to_complete(state_test_data);
497
498 return AWS_OP_SUCCESS;
499 }
500
501 AWS_TEST_CASE_FIXTURE(
502 mqtt_connection_success_callback,
503 s_setup_mqtt_server_fn,
504 s_test_mqtt_connection_success_callback_fn,
505 s_clean_up_mqtt_server_fn,
506 &test_data)
507
508 /* Subscribe to a topic prior to connection, make a CONNECT, have the server send PUBLISH messages,
509 * make sure they're received, then send a DISCONNECT. */
510 static int s_test_mqtt_subscribe_fn(struct aws_allocator *allocator, void *ctx) {
511 (void)allocator;
512 struct mqtt_connection_state_test *state_test_data = ctx;
513
514 struct aws_mqtt_connection_options connection_options = {
515 .user_data = state_test_data,
516 .clean_session = false,
517 .client_id = aws_byte_cursor_from_c_str("client1234"),
518 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
519 .socket_options = &state_test_data->socket_options,
520 .on_connection_complete = aws_test311_on_connection_complete_fn,
521 };
522
523 struct aws_byte_cursor sub_topic = aws_byte_cursor_from_c_str("/test/topic");
524
525 uint16_t packet_id = aws_mqtt_client_connection_subscribe(
526 state_test_data->mqtt_connection,
527 &sub_topic,
528 AWS_MQTT_QOS_AT_LEAST_ONCE,
529 aws_test311_on_publish_received,
530 state_test_data,
531 NULL,
532 aws_test311_on_suback,
533 state_test_data);
534 ASSERT_TRUE(packet_id > 0);
535
536 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
537 aws_test311_wait_for_connection_to_complete(state_test_data);
538
539 aws_test311_wait_for_subscribe_to_complete(state_test_data);
540
541 state_test_data->expected_publishes = 2;
542 state_test_data->expected_any_publishes = 2;
543 struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
544 ASSERT_SUCCESS(mqtt_mock_server_send_publish(
545 state_test_data->mock_server,
546 &sub_topic,
547 &payload_1,
548 false /*dup*/,
549 AWS_MQTT_QOS_AT_LEAST_ONCE,
550 true /*retain*/));
551 struct aws_byte_cursor payload_2 = aws_byte_cursor_from_c_str("Test Message 2");
552 ASSERT_SUCCESS(mqtt_mock_server_send_publish(
553 state_test_data->mock_server,
554 &sub_topic,
555 &payload_2,
556 true /*dup*/,
557 AWS_MQTT_QOS_AT_LEAST_ONCE,
558 false /*retain*/));
559
560 aws_test311_wait_for_publish(state_test_data);
561 aws_test311_wait_for_any_publish(state_test_data);
562 mqtt_mock_server_wait_for_pubacks(state_test_data->mock_server, 2);
563
564 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
565 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
566 aws_test311_wait_for_disconnect_to_complete(state_test_data);
567
568 /* Decode all received packets by mock server */
569 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
570
571 ASSERT_UINT_EQUALS(5, mqtt_mock_server_decoded_packets_count(state_test_data->mock_server));
572 struct mqtt_decoded_packet *received_packet =
573 mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 0);
574 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
575 ASSERT_UINT_EQUALS(connection_options.clean_session, received_packet->clean_session);
576 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
577
578 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 1);
579 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_SUBSCRIBE, received_packet->type);
580 ASSERT_UINT_EQUALS(1, aws_array_list_length(&received_packet->sub_topic_filters));
581 struct aws_mqtt_subscription val;
582 ASSERT_SUCCESS(aws_array_list_front(&received_packet->sub_topic_filters, &val));
583 ASSERT_TRUE(aws_byte_cursor_eq(&val.topic_filter, &sub_topic));
584 ASSERT_UINT_EQUALS(AWS_MQTT_QOS_AT_LEAST_ONCE, val.qos);
585 ASSERT_UINT_EQUALS(packet_id, received_packet->packet_identifier);
586
587 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 2);
588 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBACK, received_packet->type);
589
590 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 3);
591 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBACK, received_packet->type);
592
593 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 4);
594 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_DISCONNECT, received_packet->type);
595
596 /* Check PUBLISH packets received via subscription callback */
597 ASSERT_UINT_EQUALS(2, aws_array_list_length(&state_test_data->published_messages));
598
599 struct received_publish_packet *publish_msg = NULL;
600 ASSERT_SUCCESS(aws_array_list_get_at_ptr(&state_test_data->published_messages, (void **)&publish_msg, 0));
601 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&sub_topic, &publish_msg->topic));
602 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&payload_1, &publish_msg->payload));
603 ASSERT_FALSE(publish_msg->dup);
604 ASSERT_TRUE(publish_msg->retain);
605
606 ASSERT_SUCCESS(aws_array_list_get_at_ptr(&state_test_data->published_messages, (void **)&publish_msg, 1));
607 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&sub_topic, &publish_msg->topic));
608 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&payload_2, &publish_msg->payload));
609 ASSERT_TRUE(publish_msg->dup);
610 ASSERT_FALSE(publish_msg->retain);
611
612 /* Check PUBLISH packets received via on_any_publish callback */
613 ASSERT_UINT_EQUALS(2, aws_array_list_length(&state_test_data->any_published_messages));
614
615 ASSERT_SUCCESS(aws_array_list_get_at_ptr(&state_test_data->any_published_messages, (void **)&publish_msg, 0));
616 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&sub_topic, &publish_msg->topic));
617 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&payload_1, &publish_msg->payload));
618 ASSERT_FALSE(publish_msg->dup);
619 ASSERT_TRUE(publish_msg->retain);
620
621 ASSERT_SUCCESS(aws_array_list_get_at_ptr(&state_test_data->any_published_messages, (void **)&publish_msg, 1));
622 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&sub_topic, &publish_msg->topic));
623 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&payload_2, &publish_msg->payload));
624 ASSERT_TRUE(publish_msg->dup);
625 ASSERT_FALSE(publish_msg->retain);
626
627 return AWS_OP_SUCCESS;
628 }
629
630 AWS_TEST_CASE_FIXTURE(
631 mqtt_connect_subscribe,
632 s_setup_mqtt_server_fn,
633 s_test_mqtt_subscribe_fn,
634 s_clean_up_mqtt_server_fn,
635 &test_data)
636
637 static int s_test_mqtt_subscribe_incoming_dup_fn(struct aws_allocator *allocator, void *ctx) {
638 (void)allocator;
639 struct mqtt_connection_state_test *state_test_data = ctx;
640
641 struct aws_mqtt_connection_options connection_options = {
642 .user_data = state_test_data,
643 .clean_session = false,
644 .client_id = aws_byte_cursor_from_c_str("client1234"),
645 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
646 .socket_options = &state_test_data->socket_options,
647 .on_connection_complete = aws_test311_on_connection_complete_fn,
648 };
649
650 struct aws_byte_cursor subscribed_topic = aws_byte_cursor_from_c_str("/test/topic");
651 struct aws_byte_cursor any_topic = aws_byte_cursor_from_c_str("/a/b/c");
652
653 uint16_t packet_id = aws_mqtt_client_connection_subscribe(
654 state_test_data->mqtt_connection,
655 &subscribed_topic,
656 AWS_MQTT_QOS_AT_LEAST_ONCE,
657 aws_test311_on_publish_received,
658 state_test_data,
659 NULL,
660 aws_test311_on_suback,
661 state_test_data);
662 ASSERT_TRUE(packet_id > 0);
663
664 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
665 aws_test311_wait_for_connection_to_complete(state_test_data);
666 aws_test311_wait_for_subscribe_to_complete(state_test_data);
667
668 state_test_data->expected_publishes = 4;
669 state_test_data->expected_any_publishes = 8;
670
671 struct aws_byte_cursor subscribed_payload = aws_byte_cursor_from_c_str("Subscribed");
672 for (size_t i = 0; i < 4; ++i) {
673 ASSERT_SUCCESS(mqtt_mock_server_send_publish_by_id(
674 state_test_data->mock_server,
675 1111,
676 &subscribed_topic,
677 &subscribed_payload,
678 i > 0 /*dup*/,
679 AWS_MQTT_QOS_AT_LEAST_ONCE,
680 true /*retain*/));
681 }
682
683 struct aws_byte_cursor any_payload = aws_byte_cursor_from_c_str("Not subscribed. On-any only.");
684 for (size_t i = 0; i < 4; ++i) {
685 ASSERT_SUCCESS(mqtt_mock_server_send_publish_by_id(
686 state_test_data->mock_server,
687 1234,
688 &any_topic,
689 &any_payload,
690 i > 0 /*dup*/,
691 AWS_MQTT_QOS_AT_LEAST_ONCE,
692 false /*retain*/));
693 }
694
695 aws_test311_wait_for_publish(state_test_data);
696 aws_test311_wait_for_any_publish(state_test_data);
697 mqtt_mock_server_wait_for_pubacks(state_test_data->mock_server, 8);
698
699 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
700 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
701 aws_test311_wait_for_disconnect_to_complete(state_test_data);
702
703 /* Decode all received packets by mock server */
704 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
705
706 ASSERT_UINT_EQUALS(11, mqtt_mock_server_decoded_packets_count(state_test_data->mock_server));
707 struct mqtt_decoded_packet *received_packet =
708 mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 0);
709 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
710 ASSERT_UINT_EQUALS(connection_options.clean_session, received_packet->clean_session);
711 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
712
713 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 1);
714 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_SUBSCRIBE, received_packet->type);
715 ASSERT_UINT_EQUALS(1, aws_array_list_length(&received_packet->sub_topic_filters));
716 struct aws_mqtt_subscription val;
717 ASSERT_SUCCESS(aws_array_list_front(&received_packet->sub_topic_filters, &val));
718 ASSERT_TRUE(aws_byte_cursor_eq(&val.topic_filter, &subscribed_topic));
719 ASSERT_UINT_EQUALS(AWS_MQTT_QOS_AT_LEAST_ONCE, val.qos);
720 ASSERT_UINT_EQUALS(packet_id, received_packet->packet_identifier);
721
722 for (size_t i = 0; i < 8; ++i) {
723 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 2 + i);
724 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBACK, received_packet->type);
725 }
726
727 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 10);
728 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_DISCONNECT, received_packet->type);
729
730 /* Check PUBLISH packets received via subscription callback */
731 ASSERT_UINT_EQUALS(4, aws_array_list_length(&state_test_data->published_messages));
732
733 for (size_t i = 0; i < 4; ++i) {
734 struct received_publish_packet *publish_msg = NULL;
735 ASSERT_SUCCESS(aws_array_list_get_at_ptr(&state_test_data->published_messages, (void **)&publish_msg, i));
736 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&subscribed_topic, &publish_msg->topic));
737 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&subscribed_payload, &publish_msg->payload));
738 ASSERT_INT_EQUALS((i != 0) ? 1 : 0, publish_msg->dup ? 1 : 0);
739 ASSERT_TRUE(publish_msg->retain);
740 }
741
742 /* Check PUBLISH packets received via on_any_publish callback */
743 ASSERT_UINT_EQUALS(8, aws_array_list_length(&state_test_data->any_published_messages));
744
745 for (size_t i = 0; i < 4; ++i) {
746 struct received_publish_packet *publish_msg = NULL;
747 ASSERT_SUCCESS(aws_array_list_get_at_ptr(&state_test_data->any_published_messages, (void **)&publish_msg, i));
748 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&subscribed_topic, &publish_msg->topic));
749 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&subscribed_payload, &publish_msg->payload));
750 ASSERT_INT_EQUALS((i > 0) ? 1 : 0, publish_msg->dup ? 1 : 0);
751 ASSERT_TRUE(publish_msg->retain);
752 }
753
754 for (size_t i = 4; i < 8; ++i) {
755 struct received_publish_packet *publish_msg = NULL;
756 ASSERT_SUCCESS(aws_array_list_get_at_ptr(&state_test_data->any_published_messages, (void **)&publish_msg, i));
757 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&any_topic, &publish_msg->topic));
758 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&any_payload, &publish_msg->payload));
759 ASSERT_INT_EQUALS((i > 4) ? 1 : 0, publish_msg->dup ? 1 : 0);
760 ASSERT_FALSE(publish_msg->retain);
761 }
762
763 return AWS_OP_SUCCESS;
764 }
765
766 AWS_TEST_CASE_FIXTURE(
767 mqtt_connect_subscribe_incoming_dup,
768 s_setup_mqtt_server_fn,
769 s_test_mqtt_subscribe_incoming_dup_fn,
770 s_clean_up_mqtt_server_fn,
771 &test_data)
772
773 /* Subscribe to a topic and broker returns a SUBACK with failure return code, the subscribe should fail */
774 static int s_test_mqtt_connect_subscribe_fail_from_broker_fn(struct aws_allocator *allocator, void *ctx) {
775 (void)allocator;
776 struct mqtt_connection_state_test *state_test_data = ctx;
777
778 struct aws_mqtt_connection_options connection_options = {
779 .user_data = state_test_data,
780 .clean_session = false,
781 .client_id = aws_byte_cursor_from_c_str("client1234"),
782 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
783 .socket_options = &state_test_data->socket_options,
784 .on_connection_complete = aws_test311_on_connection_complete_fn,
785 };
786
787 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
788 aws_test311_wait_for_connection_to_complete(state_test_data);
789
790 /* Disable the auto ACK packets sent by the server, we will send failure SUBACK */
791 mqtt_mock_server_disable_auto_ack(state_test_data->mock_server);
792
793 struct aws_byte_cursor sub_topic = aws_byte_cursor_from_c_str("/test/topic");
794
795 uint16_t packet_id = aws_mqtt_client_connection_subscribe(
796 state_test_data->mqtt_connection,
797 &sub_topic,
798 AWS_MQTT_QOS_AT_LEAST_ONCE,
799 aws_test311_on_publish_received,
800 state_test_data,
801 NULL,
802 aws_test311_on_suback,
803 state_test_data);
804 ASSERT_TRUE(packet_id > 0);
805
806 ASSERT_SUCCESS(mqtt_mock_server_send_single_suback(state_test_data->mock_server, packet_id, AWS_MQTT_QOS_FAILURE));
807 aws_test311_wait_for_subscribe_to_complete(state_test_data);
808 /* Check the subscribe returned QoS is failure */
809 size_t length = aws_array_list_length(&state_test_data->qos_returned);
810 ASSERT_UINT_EQUALS(1, length);
811 uint8_t qos = 0;
812 ASSERT_SUCCESS(aws_array_list_get_at(&state_test_data->qos_returned, &qos, 0));
813 ASSERT_UINT_EQUALS(AWS_MQTT_QOS_FAILURE, qos);
814
815 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
816 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
817 aws_test311_wait_for_disconnect_to_complete(state_test_data);
818
819 return AWS_OP_SUCCESS;
820 }
821
822 AWS_TEST_CASE_FIXTURE(
823 mqtt_connect_subscribe_fail_from_broker,
824 s_setup_mqtt_server_fn,
825 s_test_mqtt_connect_subscribe_fail_from_broker_fn,
826 s_clean_up_mqtt_server_fn,
827 &test_data)
828
829 /* Subscribe to multiple topics prior to connection, make a CONNECT, have the server send PUBLISH messages,
830 * make sure they're received, then send a DISCONNECT. */
831 static int s_test_mqtt_subscribe_multi_fn(struct aws_allocator *allocator, void *ctx) {
832 (void)allocator;
833 struct mqtt_connection_state_test *state_test_data = ctx;
834
835 struct aws_mqtt_connection_options connection_options = {
836 .user_data = state_test_data,
837 .clean_session = false,
838 .client_id = aws_byte_cursor_from_c_str("client1234"),
839 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
840 .socket_options = &state_test_data->socket_options,
841 .on_connection_complete = aws_test311_on_connection_complete_fn,
842 };
843
844 struct aws_byte_cursor sub_topic_1 = aws_byte_cursor_from_c_str("/test/topic1");
845 struct aws_byte_cursor sub_topic_2 = aws_byte_cursor_from_c_str("/test/topic2");
846
847 struct aws_mqtt_topic_subscription sub1 = {
848 .topic = sub_topic_1,
849 .qos = AWS_MQTT_QOS_AT_LEAST_ONCE,
850 .on_publish = aws_test311_on_publish_received,
851 .on_cleanup = NULL,
852 .on_publish_ud = state_test_data,
853 };
854 struct aws_mqtt_topic_subscription sub2 = {
855 .topic = sub_topic_2,
856 .qos = AWS_MQTT_QOS_AT_LEAST_ONCE,
857 .on_publish = aws_test311_on_publish_received,
858 .on_cleanup = NULL,
859 .on_publish_ud = state_test_data,
860 };
861
862 struct aws_array_list topic_filters;
863 size_t list_len = 2;
864 AWS_VARIABLE_LENGTH_ARRAY(uint8_t, static_buf, list_len * sizeof(struct aws_mqtt_topic_subscription));
865 aws_array_list_init_static(&topic_filters, static_buf, list_len, sizeof(struct aws_mqtt_topic_subscription));
866
867 aws_array_list_push_back(&topic_filters, &sub1);
868 aws_array_list_push_back(&topic_filters, &sub2);
869
870 uint16_t packet_id = aws_mqtt_client_connection_subscribe_multiple(
871 state_test_data->mqtt_connection, &topic_filters, aws_test311_on_multi_suback, state_test_data);
872 ASSERT_TRUE(packet_id > 0);
873
874 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
875 aws_test311_wait_for_connection_to_complete(state_test_data);
876
877 aws_test311_wait_for_subscribe_to_complete(state_test_data);
878 /* Check the subscribe returned QoS is expected */
879 size_t length = aws_array_list_length(&state_test_data->qos_returned);
880 ASSERT_UINT_EQUALS(2, length);
881 uint8_t qos = 0;
882 ASSERT_SUCCESS(aws_array_list_get_at(&state_test_data->qos_returned, &qos, 0));
883 ASSERT_UINT_EQUALS(AWS_MQTT_QOS_EXACTLY_ONCE, qos);
884 ASSERT_SUCCESS(aws_array_list_get_at(&state_test_data->qos_returned, &qos, 1));
885 ASSERT_UINT_EQUALS(AWS_MQTT_QOS_EXACTLY_ONCE, qos);
886
887 state_test_data->expected_publishes = 2;
888 struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
889 ASSERT_SUCCESS(mqtt_mock_server_send_publish(
890 state_test_data->mock_server,
891 &sub_topic_1,
892 &payload_1,
893 false /*dup*/,
894 AWS_MQTT_QOS_AT_LEAST_ONCE,
895 false /*retain*/));
896 struct aws_byte_cursor payload_2 = aws_byte_cursor_from_c_str("Test Message 2");
897 ASSERT_SUCCESS(mqtt_mock_server_send_publish(
898 state_test_data->mock_server,
899 &sub_topic_2,
900 &payload_2,
901 false /*dup*/,
902 AWS_MQTT_QOS_AT_LEAST_ONCE,
903 false /*retain*/));
904 aws_test311_wait_for_publish(state_test_data);
905
906 /* Let's do another publish on a topic that is not subscribed by client.
907 * This can happen if the Server automatically assigned a subscription to the Client */
908 state_test_data->expected_any_publishes = 3;
909 struct aws_byte_cursor payload_3 = aws_byte_cursor_from_c_str("Test Message 3");
910 struct aws_byte_cursor topic_3 = aws_byte_cursor_from_c_str("/test/topic3");
911 ASSERT_SUCCESS(mqtt_mock_server_send_publish(
912 state_test_data->mock_server,
913 &topic_3,
914 &payload_3,
915 false /*dup*/,
916 AWS_MQTT_QOS_AT_LEAST_ONCE,
917 false /*retain*/));
918 aws_test311_wait_for_any_publish(state_test_data);
919
920 mqtt_mock_server_wait_for_pubacks(state_test_data->mock_server, 3);
921
922 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
923 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
924 aws_test311_wait_for_disconnect_to_complete(state_test_data);
925
926 /* Decode all received packets by mock server */
927 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
928
929 ASSERT_UINT_EQUALS(6, mqtt_mock_server_decoded_packets_count(state_test_data->mock_server));
930 struct mqtt_decoded_packet *received_packet =
931 mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 0);
932 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
933
934 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 1);
935 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_SUBSCRIBE, received_packet->type);
936 ASSERT_UINT_EQUALS(2, aws_array_list_length(&received_packet->sub_topic_filters));
937 struct aws_mqtt_subscription val;
938 ASSERT_SUCCESS(aws_array_list_front(&received_packet->sub_topic_filters, &val));
939 ASSERT_TRUE(aws_byte_cursor_eq(&val.topic_filter, &sub_topic_1));
940 ASSERT_UINT_EQUALS(AWS_MQTT_QOS_AT_LEAST_ONCE, val.qos);
941 ASSERT_SUCCESS(aws_array_list_back(&received_packet->sub_topic_filters, &val));
942 ASSERT_TRUE(aws_byte_cursor_eq(&val.topic_filter, &sub_topic_2));
943 ASSERT_UINT_EQUALS(AWS_MQTT_QOS_AT_LEAST_ONCE, val.qos);
944 ASSERT_UINT_EQUALS(packet_id, received_packet->packet_identifier);
945
946 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 2);
947 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBACK, received_packet->type);
948 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 3);
949 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBACK, received_packet->type);
950 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 4);
951 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBACK, received_packet->type);
952
953 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 5);
954 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_DISCONNECT, received_packet->type);
955
956 /* Only two packets should be recorded by the published_messages, but all the three packets will be recorded by
957 * any_published_messages */
958 ASSERT_UINT_EQUALS(2, aws_array_list_length(&state_test_data->published_messages));
959 ASSERT_UINT_EQUALS(3, aws_array_list_length(&state_test_data->any_published_messages));
960
961 struct received_publish_packet *publish_msg = NULL;
962 ASSERT_SUCCESS(aws_array_list_get_at_ptr(&state_test_data->published_messages, (void **)&publish_msg, 0));
963 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&sub_topic_1, &publish_msg->topic));
964 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&payload_1, &publish_msg->payload));
965 ASSERT_SUCCESS(aws_array_list_get_at_ptr(&state_test_data->published_messages, (void **)&publish_msg, 1));
966 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&sub_topic_2, &publish_msg->topic));
967 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&payload_2, &publish_msg->payload));
968
969 return AWS_OP_SUCCESS;
970 }
971
972 AWS_TEST_CASE_FIXTURE(
973 mqtt_connect_subscribe_multi,
974 s_setup_mqtt_server_fn,
975 s_test_mqtt_subscribe_multi_fn,
976 s_clean_up_mqtt_server_fn,
977 &test_data)
978
979 /* Subscribe to multiple topics prior to connection, make a CONNECT, have the server send PUBLISH messages, unsubscribe
980 * to a topic, have the server send PUBLISH messages again, make sure the unsubscribed topic callback will not be fired
981 */
982 static int s_test_mqtt_unsubscribe_fn(struct aws_allocator *allocator, void *ctx) {
983 (void)allocator;
984 struct mqtt_connection_state_test *state_test_data = ctx;
985
986 struct aws_mqtt_connection_options connection_options = {
987 .user_data = state_test_data,
988 .clean_session = false,
989 .client_id = aws_byte_cursor_from_c_str("client1234"),
990 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
991 .socket_options = &state_test_data->socket_options,
992 .on_connection_complete = aws_test311_on_connection_complete_fn,
993 };
994
995 struct aws_byte_cursor sub_topic_1 = aws_byte_cursor_from_c_str("/test/topic1");
996 struct aws_byte_cursor sub_topic_2 = aws_byte_cursor_from_c_str("/test/topic2");
997
998 struct aws_mqtt_topic_subscription sub1 = {
999 .topic = sub_topic_1,
1000 .qos = AWS_MQTT_QOS_AT_LEAST_ONCE,
1001 .on_publish = aws_test311_on_publish_received,
1002 .on_cleanup = NULL,
1003 .on_publish_ud = state_test_data,
1004 };
1005 struct aws_mqtt_topic_subscription sub2 = {
1006 .topic = sub_topic_2,
1007 .qos = AWS_MQTT_QOS_AT_LEAST_ONCE,
1008 .on_publish = aws_test311_on_publish_received,
1009 .on_cleanup = NULL,
1010 .on_publish_ud = state_test_data,
1011 };
1012
1013 struct aws_array_list topic_filters;
1014 size_t list_len = 2;
1015 AWS_VARIABLE_LENGTH_ARRAY(uint8_t, static_buf, list_len * sizeof(struct aws_mqtt_topic_subscription));
1016 aws_array_list_init_static(&topic_filters, static_buf, list_len, sizeof(struct aws_mqtt_topic_subscription));
1017
1018 aws_array_list_push_back(&topic_filters, &sub1);
1019 aws_array_list_push_back(&topic_filters, &sub2);
1020
1021 uint16_t sub_packet_id = aws_mqtt_client_connection_subscribe_multiple(
1022 state_test_data->mqtt_connection, &topic_filters, aws_test311_on_multi_suback, state_test_data);
1023 ASSERT_TRUE(sub_packet_id > 0);
1024
1025 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
1026 aws_test311_wait_for_connection_to_complete(state_test_data);
1027
1028 aws_test311_wait_for_subscribe_to_complete(state_test_data);
1029
1030 state_test_data->expected_any_publishes = 2;
1031 struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
1032 ASSERT_SUCCESS(mqtt_mock_server_send_publish(
1033 state_test_data->mock_server,
1034 &sub_topic_1,
1035 &payload_1,
1036 false /*dup*/,
1037 AWS_MQTT_QOS_AT_LEAST_ONCE,
1038 false /*retain*/));
1039 struct aws_byte_cursor payload_2 = aws_byte_cursor_from_c_str("Test Message 2");
1040 ASSERT_SUCCESS(mqtt_mock_server_send_publish(
1041 state_test_data->mock_server,
1042 &sub_topic_2,
1043 &payload_2,
1044 false /*dup*/,
1045 AWS_MQTT_QOS_AT_LEAST_ONCE,
1046 false /*retain*/));
1047 aws_test311_wait_for_any_publish(state_test_data);
1048 mqtt_mock_server_wait_for_pubacks(state_test_data->mock_server, 2);
1049
1050 aws_mutex_lock(&state_test_data->lock);
1051 state_test_data->expected_ops_completed = 1;
1052 aws_mutex_unlock(&state_test_data->lock);
1053 /* unsubscribe to the first topic */
1054 uint16_t unsub_packet_id = aws_mqtt_client_connection_unsubscribe(
1055 state_test_data->mqtt_connection, &sub_topic_1, aws_test311_on_op_complete, state_test_data);
1056 ASSERT_TRUE(unsub_packet_id > 0);
1057 /* Even when the UNSUBACK has not received, the client will not invoke the on_pub callback for that topic */
1058 ASSERT_SUCCESS(mqtt_mock_server_send_publish(
1059 state_test_data->mock_server,
1060 &sub_topic_1,
1061 &payload_1,
1062 false /*dup*/,
1063 AWS_MQTT_QOS_AT_LEAST_ONCE,
1064 false /*retain*/));
1065 ASSERT_SUCCESS(mqtt_mock_server_send_publish(
1066 state_test_data->mock_server,
1067 &sub_topic_2,
1068 &payload_2,
1069 false /*dup*/,
1070 AWS_MQTT_QOS_AT_LEAST_ONCE,
1071 false /*retain*/));
1072 state_test_data->expected_any_publishes = 2;
1073 aws_test311_wait_for_any_publish(state_test_data);
1074 mqtt_mock_server_wait_for_pubacks(state_test_data->mock_server, 2);
1075 aws_test311_wait_for_ops_completed(state_test_data);
1076
1077 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
1078 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
1079 aws_test311_wait_for_disconnect_to_complete(state_test_data);
1080
1081 /* Decode all received packets by mock server */
1082 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
1083
1084 struct mqtt_decoded_packet *received_packet =
1085 mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 0);
1086 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
1087
1088 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 1);
1089 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_SUBSCRIBE, received_packet->type);
1090 ASSERT_UINT_EQUALS(2, aws_array_list_length(&received_packet->sub_topic_filters));
1091 struct aws_mqtt_subscription val;
1092 ASSERT_SUCCESS(aws_array_list_front(&received_packet->sub_topic_filters, &val));
1093 ASSERT_TRUE(aws_byte_cursor_eq(&val.topic_filter, &sub_topic_1));
1094 ASSERT_UINT_EQUALS(AWS_MQTT_QOS_AT_LEAST_ONCE, val.qos);
1095 ASSERT_SUCCESS(aws_array_list_back(&received_packet->sub_topic_filters, &val));
1096 ASSERT_TRUE(aws_byte_cursor_eq(&val.topic_filter, &sub_topic_2));
1097 ASSERT_UINT_EQUALS(AWS_MQTT_QOS_AT_LEAST_ONCE, val.qos);
1098 ASSERT_UINT_EQUALS(sub_packet_id, received_packet->packet_identifier);
1099
1100 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 2);
1101 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBACK, received_packet->type);
1102 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 3);
1103 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBACK, received_packet->type);
1104
1105 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 4);
1106 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_UNSUBSCRIBE, received_packet->type);
1107 ASSERT_UINT_EQUALS(1, aws_array_list_length(&received_packet->unsub_topic_filters));
1108 struct aws_byte_cursor val_cur;
1109 ASSERT_SUCCESS(aws_array_list_front(&received_packet->unsub_topic_filters, &val_cur));
1110 ASSERT_TRUE(aws_byte_cursor_eq(&val_cur, &sub_topic_1));
1111 ASSERT_UINT_EQUALS(unsub_packet_id, received_packet->packet_identifier);
1112
1113 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 5);
1114 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBACK, received_packet->type);
1115 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 6);
1116 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBACK, received_packet->type);
1117
1118 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 7);
1119 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_DISCONNECT, received_packet->type);
1120
1121 /* Only three packets should be recorded by the published_messages, but all the four packets will be recorded by
1122 * any_published_messages */
1123 ASSERT_UINT_EQUALS(3, aws_array_list_length(&state_test_data->published_messages));
1124 ASSERT_UINT_EQUALS(4, aws_array_list_length(&state_test_data->any_published_messages));
1125
1126 struct received_publish_packet *publish_msg = NULL;
1127 ASSERT_SUCCESS(aws_array_list_get_at_ptr(&state_test_data->published_messages, (void **)&publish_msg, 0));
1128 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&sub_topic_1, &publish_msg->topic));
1129 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&payload_1, &publish_msg->payload));
1130 ASSERT_SUCCESS(aws_array_list_get_at_ptr(&state_test_data->published_messages, (void **)&publish_msg, 1));
1131 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&sub_topic_2, &publish_msg->topic));
1132 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&payload_2, &publish_msg->payload));
1133 ASSERT_SUCCESS(aws_array_list_get_at_ptr(&state_test_data->published_messages, (void **)&publish_msg, 2));
1134 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&sub_topic_2, &publish_msg->topic));
1135 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&payload_2, &publish_msg->payload));
1136
1137 return AWS_OP_SUCCESS;
1138 }
1139
1140 AWS_TEST_CASE_FIXTURE(
1141 mqtt_connect_unsubscribe,
1142 s_setup_mqtt_server_fn,
1143 s_test_mqtt_unsubscribe_fn,
1144 s_clean_up_mqtt_server_fn,
1145 &test_data)
1146
1147 /**
1148 * Subscribe to multiple topics prior to connection, make a CONNECT, have the server send PUBLISH messages, unsubscribe
1149 * to a topic, disconnect with the broker, make a connection with clean_session true, then call resubscribe, client will
1150 * successfully resubscribe to the old topics.
1151 */
1152 static int s_test_mqtt_resubscribe_fn(struct aws_allocator *allocator, void *ctx) {
1153 (void)allocator;
1154 struct mqtt_connection_state_test *state_test_data = ctx;
1155
1156 struct aws_mqtt_connection_options connection_options = {
1157 .user_data = state_test_data,
1158 .clean_session = false,
1159 .client_id = aws_byte_cursor_from_c_str("client1234"),
1160 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
1161 .socket_options = &state_test_data->socket_options,
1162 .on_connection_complete = aws_test311_on_connection_complete_fn,
1163 };
1164
1165 struct aws_byte_cursor sub_topic_1 = aws_byte_cursor_from_c_str("/test/topic1");
1166 struct aws_byte_cursor sub_topic_2 = aws_byte_cursor_from_c_str("/test/topic2");
1167 struct aws_byte_cursor sub_topic_3 = aws_byte_cursor_from_c_str("/test/topic3");
1168
1169 struct aws_mqtt_topic_subscription sub1 = {
1170 .topic = sub_topic_1,
1171 .qos = AWS_MQTT_QOS_AT_LEAST_ONCE,
1172 .on_publish = aws_test311_on_publish_received,
1173 .on_cleanup = NULL,
1174 .on_publish_ud = state_test_data,
1175 };
1176 struct aws_mqtt_topic_subscription sub2 = {
1177 .topic = sub_topic_2,
1178 .qos = AWS_MQTT_QOS_AT_LEAST_ONCE,
1179 .on_publish = aws_test311_on_publish_received,
1180 .on_cleanup = NULL,
1181 .on_publish_ud = state_test_data,
1182 };
1183 struct aws_mqtt_topic_subscription sub3 = {
1184 .topic = sub_topic_3,
1185 .qos = AWS_MQTT_QOS_AT_LEAST_ONCE,
1186 .on_publish = aws_test311_on_publish_received,
1187 .on_cleanup = NULL,
1188 .on_publish_ud = state_test_data,
1189 };
1190
1191 struct aws_array_list topic_filters;
1192 size_t list_len = 3;
1193 AWS_VARIABLE_LENGTH_ARRAY(uint8_t, static_buf, list_len * sizeof(struct aws_mqtt_topic_subscription));
1194 aws_array_list_init_static(&topic_filters, static_buf, list_len, sizeof(struct aws_mqtt_topic_subscription));
1195
1196 aws_array_list_push_back(&topic_filters, &sub1);
1197 aws_array_list_push_back(&topic_filters, &sub2);
1198 aws_array_list_push_back(&topic_filters, &sub3);
1199
1200 uint16_t sub_packet_id = aws_mqtt_client_connection_subscribe_multiple(
1201 state_test_data->mqtt_connection, &topic_filters, aws_test311_on_multi_suback, state_test_data);
1202 ASSERT_TRUE(sub_packet_id > 0);
1203
1204 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
1205 aws_test311_wait_for_connection_to_complete(state_test_data);
1206
1207 aws_test311_wait_for_subscribe_to_complete(state_test_data);
1208 aws_mutex_lock(&state_test_data->lock);
1209 state_test_data->expected_ops_completed = 1;
1210 aws_mutex_unlock(&state_test_data->lock);
1211 /* unsubscribe to the first topic */
1212 uint16_t unsub_packet_id = aws_mqtt_client_connection_unsubscribe(
1213 state_test_data->mqtt_connection, &sub_topic_1, aws_test311_on_op_complete, state_test_data);
1214 ASSERT_TRUE(unsub_packet_id > 0);
1215
1216 aws_test311_wait_for_ops_completed(state_test_data);
1217 /* client still subscribes to topic_2 & topic_3 */
1218
1219 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
1220 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
1221 aws_test311_wait_for_disconnect_to_complete(state_test_data);
1222
1223 /* reconnection to the same server */
1224 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
1225 aws_test311_wait_for_connection_to_complete(state_test_data);
1226
1227 /* Get all the packets out of the way */
1228 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
1229 size_t packets_count = mqtt_mock_server_decoded_packets_count(state_test_data->mock_server);
1230 struct mqtt_decoded_packet *t_received_packet =
1231 mqtt_mock_server_get_latest_decoded_packet(state_test_data->mock_server);
1232 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, t_received_packet->type);
1233
1234 /* resubscribes to topic_2 & topic_3 */
1235 uint16_t resub_packet_id = aws_mqtt_resubscribe_existing_topics(
1236 state_test_data->mqtt_connection, aws_test311_on_multi_suback, state_test_data);
1237 ASSERT_TRUE(resub_packet_id > 0);
1238 aws_test311_wait_for_subscribe_to_complete(state_test_data);
1239
1240 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
1241 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
1242 aws_test311_wait_for_disconnect_to_complete(state_test_data);
1243
1244 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
1245 struct mqtt_decoded_packet *received_packet =
1246 mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, packets_count);
1247 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_SUBSCRIBE, received_packet->type);
1248
1249 ASSERT_UINT_EQUALS(2, aws_array_list_length(&received_packet->sub_topic_filters));
1250 struct aws_mqtt_subscription val;
1251 ASSERT_SUCCESS(aws_array_list_front(&received_packet->sub_topic_filters, &val));
1252 ASSERT_TRUE(aws_byte_cursor_eq(&val.topic_filter, &sub_topic_3));
1253 ASSERT_UINT_EQUALS(AWS_MQTT_QOS_AT_LEAST_ONCE, val.qos);
1254 ASSERT_SUCCESS(aws_array_list_back(&received_packet->sub_topic_filters, &val));
1255 ASSERT_TRUE(aws_byte_cursor_eq(&val.topic_filter, &sub_topic_2));
1256 ASSERT_UINT_EQUALS(AWS_MQTT_QOS_AT_LEAST_ONCE, val.qos);
1257 ASSERT_UINT_EQUALS(resub_packet_id, received_packet->packet_identifier);
1258
1259 return AWS_OP_SUCCESS;
1260 }
1261
1262 AWS_TEST_CASE_FIXTURE(
1263 mqtt_connect_resubscribe,
1264 s_setup_mqtt_server_fn,
1265 s_test_mqtt_resubscribe_fn,
1266 s_clean_up_mqtt_server_fn,
1267 &test_data)
1268
1269 /* Make a CONNECT, PUBLISH to a topic, make sure server received, then send a DISCONNECT. */
1270 static int s_test_mqtt_publish_fn(struct aws_allocator *allocator, void *ctx) {
1271 (void)allocator;
1272 struct mqtt_connection_state_test *state_test_data = ctx;
1273
1274 struct aws_mqtt_connection_options connection_options = {
1275 .user_data = state_test_data,
1276 .clean_session = false,
1277 .client_id = aws_byte_cursor_from_c_str("client1234"),
1278 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
1279 .socket_options = &state_test_data->socket_options,
1280 .on_connection_complete = aws_test311_on_connection_complete_fn,
1281 };
1282
1283 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
1284 struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
1285 struct aws_byte_cursor payload_2 = aws_byte_cursor_from_c_str("Test Message 2");
1286
1287 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
1288 aws_test311_wait_for_connection_to_complete(state_test_data);
1289
1290 aws_mutex_lock(&state_test_data->lock);
1291 state_test_data->expected_ops_completed = 3;
1292 aws_mutex_unlock(&state_test_data->lock);
1293 uint16_t packet_id_1 = aws_mqtt_client_connection_publish(
1294 state_test_data->mqtt_connection,
1295 &pub_topic,
1296 AWS_MQTT_QOS_AT_LEAST_ONCE,
1297 false,
1298 &payload_1,
1299 aws_test311_on_op_complete,
1300 state_test_data);
1301 ASSERT_TRUE(packet_id_1 > 0);
1302 uint16_t packet_id_2 = aws_mqtt_client_connection_publish(
1303 state_test_data->mqtt_connection,
1304 &pub_topic,
1305 AWS_MQTT_QOS_AT_LEAST_ONCE,
1306 false,
1307 &payload_2,
1308 aws_test311_on_op_complete,
1309 state_test_data);
1310 ASSERT_TRUE(packet_id_2 > 0);
1311
1312 /* Null payload case */
1313 uint16_t packet_id_3 = aws_mqtt_client_connection_publish(
1314 state_test_data->mqtt_connection,
1315 &pub_topic,
1316 AWS_MQTT_QOS_AT_LEAST_ONCE,
1317 false,
1318 NULL,
1319 aws_test311_on_op_complete,
1320 state_test_data);
1321 ASSERT_TRUE(packet_id_3 > 0);
1322
1323 aws_test311_wait_for_ops_completed(state_test_data);
1324
1325 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
1326 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
1327 aws_test311_wait_for_disconnect_to_complete(state_test_data);
1328
1329 /* Decode all received packets by mock server */
1330 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
1331
1332 ASSERT_UINT_EQUALS(5, mqtt_mock_server_decoded_packets_count(state_test_data->mock_server));
1333 struct mqtt_decoded_packet *received_packet =
1334 mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 0);
1335 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
1336 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
1337
1338 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 1);
1339 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBLISH, received_packet->type);
1340 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->topic_name, &pub_topic));
1341 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->publish_payload, &payload_1));
1342
1343 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 2);
1344 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBLISH, received_packet->type);
1345 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->topic_name, &pub_topic));
1346 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->publish_payload, &payload_2));
1347
1348 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 3);
1349 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBLISH, received_packet->type);
1350 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->topic_name, &pub_topic));
1351 ASSERT_INT_EQUALS(0, received_packet->publish_payload.len);
1352
1353 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 4);
1354 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_DISCONNECT, received_packet->type);
1355
1356 return AWS_OP_SUCCESS;
1357 }
1358
1359 AWS_TEST_CASE_FIXTURE(
1360 mqtt_connect_publish,
1361 s_setup_mqtt_server_fn,
1362 s_test_mqtt_publish_fn,
1363 s_clean_up_mqtt_server_fn,
1364 &test_data)
1365
1366 /* Make a CONNECT, PUBLISH to a topic, free the payload before the publish completes to make sure it's safe */
1367 static int s_test_mqtt_publish_payload_fn(struct aws_allocator *allocator, void *ctx) {
1368 (void)allocator;
1369 struct mqtt_connection_state_test *state_test_data = ctx;
1370
1371 struct aws_mqtt_connection_options connection_options = {
1372 .user_data = state_test_data,
1373 .clean_session = false,
1374 .client_id = aws_byte_cursor_from_c_str("client1234"),
1375 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
1376 .socket_options = &state_test_data->socket_options,
1377 .on_connection_complete = aws_test311_on_connection_complete_fn,
1378 };
1379
1380 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
1381 struct aws_byte_buf buf_payload;
1382 struct aws_byte_cursor ori_payload = aws_byte_cursor_from_c_str("Test Message 1");
1383 ASSERT_SUCCESS(aws_byte_buf_init_copy_from_cursor(&buf_payload, allocator, ori_payload));
1384 struct aws_byte_cursor payload_curser = aws_byte_cursor_from_buf(&buf_payload);
1385
1386 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
1387 aws_test311_wait_for_connection_to_complete(state_test_data);
1388
1389 aws_mutex_lock(&state_test_data->lock);
1390 state_test_data->expected_ops_completed = 1;
1391 aws_mutex_unlock(&state_test_data->lock);
1392 uint16_t packet_id = aws_mqtt_client_connection_publish(
1393 state_test_data->mqtt_connection,
1394 &pub_topic,
1395 AWS_MQTT_QOS_AT_LEAST_ONCE,
1396 false,
1397 &payload_curser,
1398 aws_test311_on_op_complete,
1399 state_test_data);
1400 ASSERT_TRUE(packet_id > 0);
1401
1402 /* clean up the payload buf, as user don't need to manage the buf and keep it valid until publish completes */
1403 aws_byte_buf_clean_up(&buf_payload);
1404 aws_test311_wait_for_ops_completed(state_test_data);
1405
1406 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
1407 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
1408 aws_test311_wait_for_disconnect_to_complete(state_test_data);
1409
1410 /* Decode all received packets by mock server */
1411 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
1412
1413 ASSERT_UINT_EQUALS(3, mqtt_mock_server_decoded_packets_count(state_test_data->mock_server));
1414 struct mqtt_decoded_packet *received_packet =
1415 mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 0);
1416 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
1417 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
1418
1419 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 1);
1420 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBLISH, received_packet->type);
1421 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->topic_name, &pub_topic));
1422 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->publish_payload, &ori_payload));
1423
1424 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 2);
1425 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_DISCONNECT, received_packet->type);
1426
1427 return AWS_OP_SUCCESS;
1428 }
1429
1430 AWS_TEST_CASE_FIXTURE(
1431 mqtt_connect_publish_payload,
1432 s_setup_mqtt_server_fn,
1433 s_test_mqtt_publish_payload_fn,
1434 s_clean_up_mqtt_server_fn,
1435 &test_data)
1436
1437 /**
1438 * CONNECT, force the server to hang up after a successful connection and block all CONNACKS, send PUBLISH messages
1439 * let the server send CONNACKS, make sure when the client reconnects automatically, it sends the PUBLISH messages
1440 * that were sent during offline mode. Then send a DISCONNECT.
1441 */
1442 static int s_test_mqtt_connection_offline_publish_fn(struct aws_allocator *allocator, void *ctx) {
1443 (void)allocator;
1444 struct mqtt_connection_state_test *state_test_data = ctx;
1445
1446 struct aws_mqtt_connection_options connection_options = {
1447 .user_data = state_test_data,
1448 .clean_session = false,
1449 .client_id = aws_byte_cursor_from_c_str("client1234"),
1450 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
1451 .socket_options = &state_test_data->socket_options,
1452 .on_connection_complete = aws_test311_on_connection_complete_fn,
1453 .ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS,
1454 .keep_alive_time_secs = DEFAULT_TEST_KEEP_ALIVE_S,
1455 };
1456
1457 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
1458 aws_test311_wait_for_connection_to_complete(state_test_data);
1459
1460 mqtt_mock_server_set_max_connack(state_test_data->mock_server, 0);
1461
1462 /* shut it down and make sure the client automatically reconnects.*/
1463 aws_channel_shutdown(state_test_data->server_channel, AWS_OP_SUCCESS);
1464 aws_test311_wait_for_interrupt_to_complete(state_test_data);
1465
1466 state_test_data->server_disconnect_completed = false;
1467
1468 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
1469 struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
1470 struct aws_byte_cursor payload_2 = aws_byte_cursor_from_c_str("Test Message 2");
1471
1472 aws_mutex_lock(&state_test_data->lock);
1473 state_test_data->expected_ops_completed = 2;
1474 aws_mutex_unlock(&state_test_data->lock);
1475
1476 ASSERT_TRUE(
1477 aws_mqtt_client_connection_publish(
1478 state_test_data->mqtt_connection,
1479 &pub_topic,
1480 AWS_MQTT_QOS_AT_LEAST_ONCE,
1481 false,
1482 &payload_1,
1483 aws_test311_on_op_complete,
1484 state_test_data) > 0);
1485 ASSERT_TRUE(
1486 aws_mqtt_client_connection_publish(
1487 state_test_data->mqtt_connection,
1488 &pub_topic,
1489 AWS_MQTT_QOS_AT_LEAST_ONCE,
1490 false,
1491 &payload_2,
1492 aws_test311_on_op_complete,
1493 state_test_data) > 0);
1494
1495 aws_mutex_lock(&state_test_data->lock);
1496 ASSERT_FALSE(state_test_data->connection_resumed);
1497 aws_mutex_unlock(&state_test_data->lock);
1498 mqtt_mock_server_set_max_connack(state_test_data->mock_server, SIZE_MAX);
1499 aws_test311_wait_for_ops_completed(state_test_data);
1500 aws_mutex_lock(&state_test_data->lock);
1501 ASSERT_TRUE(state_test_data->connection_resumed);
1502 aws_mutex_unlock(&state_test_data->lock);
1503
1504 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
1505 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
1506 aws_test311_wait_for_disconnect_to_complete(state_test_data);
1507
1508 /* Decode all received packets by mock server */
1509 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
1510 size_t packets_count = mqtt_mock_server_decoded_packets_count(state_test_data->mock_server);
1511 ASSERT_TRUE(packets_count >= 5 && packets_count <= 6);
1512
1513 struct mqtt_decoded_packet *received_packet =
1514 mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 0);
1515 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
1516 ASSERT_UINT_EQUALS(connection_options.clean_session, received_packet->clean_session);
1517 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
1518
1519 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 1);
1520 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
1521 ASSERT_UINT_EQUALS(connection_options.clean_session, received_packet->clean_session);
1522 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
1523
1524 /* if message count is 6 there was an extra connect message due to the automatic reconnect behavior and timing. */
1525 size_t index = 2;
1526 if (packets_count == 6) {
1527 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, index++);
1528 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
1529 ASSERT_UINT_EQUALS(connection_options.clean_session, received_packet->clean_session);
1530 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
1531 }
1532
1533 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, index++);
1534 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBLISH, received_packet->type);
1535 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->topic_name, &pub_topic));
1536 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->publish_payload, &payload_1));
1537
1538 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, index++);
1539 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBLISH, received_packet->type);
1540 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->topic_name, &pub_topic));
1541 ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->publish_payload, &payload_2));
1542
1543 received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, index++);
1544 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_DISCONNECT, received_packet->type);
1545
1546 return AWS_OP_SUCCESS;
1547 }
1548
1549 AWS_TEST_CASE_FIXTURE(
1550 mqtt_connection_offline_publish,
1551 s_setup_mqtt_server_fn,
1552 s_test_mqtt_connection_offline_publish_fn,
1553 s_clean_up_mqtt_server_fn,
1554 &test_data)
1555
1556 /**
1557 * CONNECT, force the server to hang up after a successful connection and block all CONNACKS, DISCONNECT while client is
1558 * reconnecting. Resource and pending requests are cleaned up correctly
1559 */
1560 static int s_test_mqtt_connection_disconnect_while_reconnecting(struct aws_allocator *allocator, void *ctx) {
1561 (void)allocator;
1562 struct mqtt_connection_state_test *state_test_data = ctx;
1563
1564 struct aws_mqtt_connection_options connection_options = {
1565 .user_data = state_test_data,
1566 .clean_session = false,
1567 .client_id = aws_byte_cursor_from_c_str("client1234"),
1568 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
1569 .socket_options = &state_test_data->socket_options,
1570 .on_connection_complete = aws_test311_on_connection_complete_fn,
1571 .ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS,
1572 .keep_alive_time_secs = DEFAULT_TEST_KEEP_ALIVE_S,
1573 };
1574
1575 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
1576 aws_test311_wait_for_connection_to_complete(state_test_data);
1577
1578 mqtt_mock_server_set_max_connack(state_test_data->mock_server, 0);
1579
1580 /* shut it down and the client automatically reconnects.*/
1581 aws_channel_shutdown(state_test_data->server_channel, AWS_OP_SUCCESS);
1582 aws_test311_wait_for_interrupt_to_complete(state_test_data);
1583
1584 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
1585 struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
1586 struct aws_byte_cursor payload_2 = aws_byte_cursor_from_c_str("Test Message 2");
1587
1588 aws_mutex_lock(&state_test_data->lock);
1589 state_test_data->expected_ops_completed = 2;
1590 aws_mutex_unlock(&state_test_data->lock);
1591
1592 ASSERT_TRUE(
1593 aws_mqtt_client_connection_publish(
1594 state_test_data->mqtt_connection,
1595 &pub_topic,
1596 AWS_MQTT_QOS_AT_LEAST_ONCE,
1597 false,
1598 &payload_1,
1599 aws_test311_on_op_complete,
1600 state_test_data) > 0);
1601 ASSERT_TRUE(
1602 aws_mqtt_client_connection_publish(
1603 state_test_data->mqtt_connection,
1604 &pub_topic,
1605 AWS_MQTT_QOS_AT_LEAST_ONCE,
1606 false,
1607 &payload_2,
1608 aws_test311_on_op_complete,
1609 state_test_data) > 0);
1610
1611 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
1612 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
1613 aws_test311_wait_for_disconnect_to_complete(state_test_data);
1614 aws_mqtt_client_connection_release(state_test_data->mqtt_connection);
1615 state_test_data->mqtt_connection = NULL;
1616 aws_test311_wait_for_ops_completed(state_test_data);
1617
1618 return AWS_OP_SUCCESS;
1619 }
1620
1621 AWS_TEST_CASE_FIXTURE(
1622 mqtt_connection_disconnect_while_reconnecting,
1623 s_setup_mqtt_server_fn,
1624 s_test_mqtt_connection_disconnect_while_reconnecting,
1625 s_clean_up_mqtt_server_fn,
1626 &test_data)
1627
1628 /**
1629 * Regression test: Once upon a time there was a bug caused by race condition on the state of connection.
1630 * The scenario is the server/broker closes the connection, while the client is making requests.
1631 * The race condition between the eventloop thread closes the connection and the main thread makes request could cause a
1632 * bug.
1633 * Solution: put a lock for the state of connection, protect it from accessing by multiple threads at the same time.
1634 */
1635
1636 static int s_test_mqtt_connection_closes_while_making_requests_fn(struct aws_allocator *allocator, void *ctx) {
1637 (void)allocator;
1638 struct mqtt_connection_state_test *state_test_data = ctx;
1639
1640 struct aws_mqtt_connection_options connection_options = {
1641 .user_data = state_test_data,
1642 .clean_session = false,
1643 .client_id = aws_byte_cursor_from_c_str("client1234"),
1644 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
1645 .socket_options = &state_test_data->socket_options,
1646 .on_connection_complete = aws_test311_on_connection_complete_fn,
1647 .ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS,
1648 .keep_alive_time_secs = DEFAULT_TEST_KEEP_ALIVE_S,
1649 };
1650
1651 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
1652 struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
1653
1654 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
1655 aws_test311_wait_for_connection_to_complete(state_test_data);
1656
1657 aws_mutex_lock(&state_test_data->lock);
1658 state_test_data->expected_ops_completed = 1;
1659 aws_mutex_unlock(&state_test_data->lock);
1660
1661 /* shutdown the channel for some error */
1662 aws_channel_shutdown(state_test_data->server_channel, AWS_ERROR_INVALID_STATE);
1663
1664 /* While the shutdown is still in process, making a publish request */
1665 /* It may not 100% trigger the crash, the crash only happens when the s_mqtt_client_shutdown and mqtt_create_request
1666 * happen at the same time. Like the slot is removed by shutdown, and the create request still think the slot is
1667 * there and try to access it. Crash happens. It's not possible to trigger the crash 100% without changing the
1668 * implementation. */
1669 uint16_t packet_id_1 = aws_mqtt_client_connection_publish(
1670 state_test_data->mqtt_connection,
1671 &pub_topic,
1672 AWS_MQTT_QOS_AT_LEAST_ONCE,
1673 false,
1674 &payload_1,
1675 aws_test311_on_op_complete,
1676 state_test_data);
1677 ASSERT_TRUE(packet_id_1 > 0);
1678
1679 aws_test311_wait_for_reconnect_to_complete(state_test_data);
1680 aws_test311_wait_for_ops_completed(state_test_data);
1681
1682 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
1683 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
1684 aws_test311_wait_for_disconnect_to_complete(state_test_data);
1685
1686 return AWS_OP_SUCCESS;
1687 }
1688
1689 AWS_TEST_CASE_FIXTURE(
1690 mqtt_connection_closes_while_making_requests,
1691 s_setup_mqtt_server_fn,
1692 s_test_mqtt_connection_closes_while_making_requests_fn,
1693 s_clean_up_mqtt_server_fn,
1694 &test_data)
1695
1696 /* helper to make sure packets are received/resent in expected order and duplicat flag is appropriately set */
1697 static int s_check_resend_packets(
1698 struct aws_channel_handler *handler,
1699 size_t search_start_idx,
1700 bool duplicate_publish_expected,
1701 uint16_t *packet_ids,
1702 size_t packet_id_count) {
1703 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(handler));
1704
1705 if (packet_id_count == 0) {
1706 return AWS_OP_SUCCESS;
1707 }
1708
1709 size_t previous_index = 0;
1710 struct mqtt_decoded_packet *previous_packet =
1711 mqtt_mock_server_find_decoded_packet_by_id(handler, search_start_idx, packet_ids[0], &previous_index);
1712 if (previous_packet->type == AWS_MQTT_PACKET_PUBLISH) {
1713 ASSERT_INT_EQUALS(duplicate_publish_expected, previous_packet->duplicate);
1714 }
1715
1716 for (size_t i = 1; i < packet_id_count; ++i) {
1717 size_t current_index = 0;
1718 struct mqtt_decoded_packet *current_packet =
1719 mqtt_mock_server_find_decoded_packet_by_id(handler, search_start_idx, packet_ids[i], &current_index);
1720 if (current_packet->type == AWS_MQTT_PACKET_PUBLISH) {
1721 ASSERT_INT_EQUALS(duplicate_publish_expected, current_packet->duplicate);
1722 }
1723
1724 ASSERT_TRUE(current_index > previous_index);
1725 previous_packet = current_packet;
1726 previous_index = current_index;
1727 }
1728
1729 return AWS_OP_SUCCESS;
1730 }
1731
1732 /**
1733 * Test that when the response is not back from the server, and the connection lost, the sent packets will be retried
1734 * in the same order as they are sent before.
1735 */
1736 static int s_test_mqtt_connection_resend_packets_fn(struct aws_allocator *allocator, void *ctx) {
1737 (void)allocator;
1738 struct mqtt_connection_state_test *state_test_data = ctx;
1739
1740 struct aws_mqtt_connection_options connection_options = {
1741 .user_data = state_test_data,
1742 .clean_session = false,
1743 .client_id = aws_byte_cursor_from_c_str("client1234"),
1744 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
1745 .socket_options = &state_test_data->socket_options,
1746 .on_connection_complete = aws_test311_on_connection_complete_fn,
1747 .ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS,
1748 .keep_alive_time_secs = DEFAULT_TEST_KEEP_ALIVE_S,
1749 };
1750
1751 struct aws_byte_cursor sub_topic = aws_byte_cursor_from_c_str("/test/topic/sub/#");
1752 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
1753 struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
1754 struct aws_byte_cursor payload_2 = aws_byte_cursor_from_c_str("Test Message 2");
1755 struct aws_byte_cursor payload_3 = aws_byte_cursor_from_c_str("Test Message 3");
1756
1757 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
1758 aws_test311_wait_for_connection_to_complete(state_test_data);
1759
1760 /* Disable the auto ACK packets sent by the server, which blocks the requests to complete */
1761 mqtt_mock_server_disable_auto_ack(state_test_data->mock_server);
1762
1763 uint16_t packet_ids[5];
1764
1765 packet_ids[0] = aws_mqtt_client_connection_publish(
1766 state_test_data->mqtt_connection, &pub_topic, AWS_MQTT_QOS_AT_LEAST_ONCE, false, &payload_1, NULL, NULL);
1767 ASSERT_TRUE(packet_ids[0] > 0);
1768
1769 packet_ids[1] = aws_mqtt_client_connection_subscribe(
1770 state_test_data->mqtt_connection,
1771 &sub_topic,
1772 AWS_MQTT_QOS_AT_LEAST_ONCE,
1773 aws_test311_on_publish_received,
1774 state_test_data,
1775 NULL,
1776 aws_test311_on_suback,
1777 state_test_data);
1778 ASSERT_TRUE(packet_ids[1] > 0);
1779
1780 packet_ids[2] = aws_mqtt_client_connection_publish(
1781 state_test_data->mqtt_connection, &pub_topic, AWS_MQTT_QOS_AT_LEAST_ONCE, false, &payload_2, NULL, NULL);
1782 ASSERT_TRUE(packet_ids[2] > 0);
1783
1784 packet_ids[3] = aws_mqtt_client_connection_unsubscribe(state_test_data->mqtt_connection, &sub_topic, NULL, NULL);
1785 ASSERT_TRUE(packet_ids[3] > 0);
1786
1787 packet_ids[4] = aws_mqtt_client_connection_publish(
1788 state_test_data->mqtt_connection, &pub_topic, AWS_MQTT_QOS_AT_LEAST_ONCE, false, &payload_3, NULL, NULL);
1789 ASSERT_TRUE(packet_ids[4] > 0);
1790
1791 /* Wait for 1 sec. ensure all the publishes have been received by the server */
1792 aws_thread_current_sleep(ONE_SEC);
1793 ASSERT_SUCCESS(
1794 s_check_resend_packets(state_test_data->mock_server, 0, false, packet_ids, AWS_ARRAY_SIZE(packet_ids)));
1795
1796 size_t packet_count = mqtt_mock_server_decoded_packets_count(state_test_data->mock_server);
1797
1798 /* shutdown the channel for some error */
1799 aws_channel_shutdown(state_test_data->server_channel, AWS_ERROR_INVALID_STATE);
1800 aws_test311_wait_for_reconnect_to_complete(state_test_data);
1801 /* Wait again, and ensure the publishes have been resent */
1802 aws_thread_current_sleep(ONE_SEC);
1803 ASSERT_SUCCESS(s_check_resend_packets(
1804 state_test_data->mock_server, packet_count, true, packet_ids, AWS_ARRAY_SIZE(packet_ids)));
1805
1806 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
1807 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
1808 aws_test311_wait_for_disconnect_to_complete(state_test_data);
1809
1810 return AWS_OP_SUCCESS;
1811 }
1812
1813 AWS_TEST_CASE_FIXTURE(
1814 mqtt_connection_resend_packets,
1815 s_setup_mqtt_server_fn,
1816 s_test_mqtt_connection_resend_packets_fn,
1817 s_clean_up_mqtt_server_fn,
1818 &test_data)
1819
1820 /**
1821 * Test that connection lost before the publish QoS 0 ever sent, publish QoS 0 will not be retried.
1822 */
1823 static int s_test_mqtt_connection_not_retry_publish_QoS_0_fn(struct aws_allocator *allocator, void *ctx) {
1824 (void)allocator;
1825 struct mqtt_connection_state_test *state_test_data = ctx;
1826
1827 struct aws_mqtt_connection_options connection_options = {
1828 .user_data = state_test_data,
1829 .clean_session = true,
1830 .client_id = aws_byte_cursor_from_c_str("client1234"),
1831 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
1832 .socket_options = &state_test_data->socket_options,
1833 .on_connection_complete = aws_test311_on_connection_complete_fn,
1834 .ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS,
1835 .keep_alive_time_secs = 16960, /* basically stop automatically sending PINGREQ */
1836 };
1837
1838 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
1839 struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
1840
1841 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
1842 aws_test311_wait_for_connection_to_complete(state_test_data);
1843
1844 /* kill the connection */
1845 aws_channel_shutdown(state_test_data->server_channel, AWS_ERROR_INVALID_STATE);
1846 /* TODO: only one eventloop thread in the el group. Most likely the test will fail, the outgoing task will not be
1847 * cancelled because of connection lost */
1848 /* make a publish with QoS 0 immediate. */
1849 aws_mutex_lock(&state_test_data->lock);
1850 state_test_data->expected_ops_completed = 1;
1851 aws_mutex_unlock(&state_test_data->lock);
1852 uint16_t packet_id_1 = aws_mqtt_client_connection_publish(
1853 state_test_data->mqtt_connection,
1854 &pub_topic,
1855 AWS_MQTT_QOS_AT_MOST_ONCE,
1856 false,
1857 &payload_1,
1858 aws_test311_on_op_complete,
1859 state_test_data);
1860 ASSERT_TRUE(packet_id_1 > 0);
1861
1862 /* publish should complete after the shutdown */
1863 aws_test311_wait_for_ops_completed(state_test_data);
1864 /* wait for reconnect */
1865 aws_test311_wait_for_reconnect_to_complete(state_test_data);
1866
1867 /* Check all received packets, no publish packets ever received by the server. Because the connection lost before it
1868 * ever get sent. */
1869 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
1870 ASSERT_NULL(
1871 mqtt_mock_server_find_decoded_packet_by_type(state_test_data->mock_server, 0, AWS_MQTT_PACKET_PUBLISH, NULL));
1872 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
1873 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
1874 aws_test311_wait_for_disconnect_to_complete(state_test_data);
1875
1876 return AWS_OP_SUCCESS;
1877 }
1878
1879 AWS_TEST_CASE_FIXTURE(
1880 mqtt_connection_not_retry_publish_QoS_0,
1881 s_setup_mqtt_server_fn,
1882 s_test_mqtt_connection_not_retry_publish_QoS_0_fn,
1883 s_clean_up_mqtt_server_fn,
1884 &test_data)
1885
1886 /**
1887 * Test that the retry policy is consistent, which means either the request has been sent or not, when the connection
1888 * lost/resume, the request will be retried.
1889 */
1890 static int s_test_mqtt_connection_consistent_retry_policy_fn(struct aws_allocator *allocator, void *ctx) {
1891 (void)allocator;
1892 struct mqtt_connection_state_test *state_test_data = ctx;
1893
1894 struct aws_mqtt_connection_options connection_options = {
1895 .user_data = state_test_data,
1896 .clean_session = false,
1897 .client_id = aws_byte_cursor_from_c_str("client1234"),
1898 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
1899 .socket_options = &state_test_data->socket_options,
1900 .on_connection_complete = aws_test311_on_connection_complete_fn,
1901 .ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS,
1902 .protocol_operation_timeout_ms = 3000,
1903 .keep_alive_time_secs = 16960, /* basically stop automatically sending PINGREQ */
1904 };
1905
1906 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
1907 struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
1908 struct aws_byte_cursor sub_topic = aws_byte_cursor_from_c_str("/test/topic");
1909
1910 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
1911 aws_test311_wait_for_connection_to_complete(state_test_data);
1912 struct aws_channel_handler *handler = state_test_data->mock_server;
1913
1914 /* Disable the auto ACK packets sent by the server, which blocks the requests to complete */
1915 mqtt_mock_server_disable_auto_ack(handler);
1916
1917 /* kill the connection */
1918 aws_channel_shutdown(state_test_data->server_channel, AWS_ERROR_INVALID_STATE);
1919 /* There is a hidden race condition between channel shutdown from eventloop and publish/subscribe from main thread,
1920 * but either way, it should work as they are retried in the end. */
1921 /* make a publish with QoS 1 immediate. */
1922 uint16_t packet_id_1 = aws_mqtt_client_connection_publish(
1923 state_test_data->mqtt_connection,
1924 &pub_topic,
1925 AWS_MQTT_QOS_AT_LEAST_ONCE,
1926 false,
1927 &payload_1,
1928 aws_test311_on_op_complete,
1929 state_test_data);
1930 ASSERT_TRUE(packet_id_1 > 0);
1931 /* make another subscribe */
1932 uint16_t packet_id_2 = aws_mqtt_client_connection_subscribe(
1933 state_test_data->mqtt_connection,
1934 &sub_topic,
1935 AWS_MQTT_QOS_AT_LEAST_ONCE,
1936 NULL,
1937 NULL,
1938 NULL,
1939 aws_test311_on_suback,
1940 state_test_data);
1941 ASSERT_TRUE(packet_id_2 > 0);
1942
1943 /* wait for reconnect */
1944 aws_test311_wait_for_reconnect_to_complete(state_test_data);
1945 /* Wait for 1 sec. ensure all the requests have been received by the server */
1946 aws_thread_current_sleep(ONE_SEC);
1947
1948 /* Check all received packets, subscribe and publish has been received */
1949 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(handler));
1950 size_t packet_count = mqtt_mock_server_decoded_packets_count(handler);
1951 /* the latest packet should be subscribe */
1952 ASSERT_NOT_NULL(mqtt_mock_server_find_decoded_packet_by_type(handler, 0, AWS_MQTT_PACKET_SUBSCRIBE, NULL));
1953 ASSERT_NOT_NULL(mqtt_mock_server_find_decoded_packet_by_type(handler, 0, AWS_MQTT_PACKET_PUBLISH, NULL));
1954
1955 /* Re-enable the auto ack to finish the requests */
1956 mqtt_mock_server_enable_auto_ack(handler);
1957 /* Kill the connection again, the requests will be retried since the response has not been received yet. */
1958 aws_channel_shutdown(state_test_data->server_channel, AWS_ERROR_INVALID_STATE);
1959 aws_test311_wait_for_reconnect_to_complete(state_test_data);
1960 /* subscribe should be able to completed now */
1961 aws_test311_wait_for_subscribe_to_complete(state_test_data);
1962
1963 /* Check all received packets, subscribe and publish has been resent */
1964 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(handler));
1965 ASSERT_TRUE(mqtt_mock_server_decoded_packets_count(handler) > packet_count);
1966 /* the latest packet should be subscribe */
1967 ASSERT_NOT_NULL(
1968 mqtt_mock_server_find_decoded_packet_by_type(handler, packet_count, AWS_MQTT_PACKET_SUBSCRIBE, NULL));
1969 ASSERT_NOT_NULL(mqtt_mock_server_find_decoded_packet_by_type(handler, packet_count, AWS_MQTT_PACKET_PUBLISH, NULL));
1970
1971 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
1972 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
1973 aws_test311_wait_for_disconnect_to_complete(state_test_data);
1974
1975 return AWS_OP_SUCCESS;
1976 }
1977
1978 AWS_TEST_CASE_FIXTURE(
1979 mqtt_connection_consistent_retry_policy,
1980 s_setup_mqtt_server_fn,
1981 s_test_mqtt_connection_consistent_retry_policy_fn,
1982 s_clean_up_mqtt_server_fn,
1983 &test_data)
1984
1985 /**
1986 * Test that the request will not be retried even the response has not received for a while.
1987 */
1988 static int s_test_mqtt_connection_not_resend_packets_on_healthy_connection_fn(
1989 struct aws_allocator *allocator,
1990 void *ctx) {
1991 (void)allocator;
1992 struct mqtt_connection_state_test *state_test_data = ctx;
1993
1994 struct aws_mqtt_connection_options connection_options = {
1995 .user_data = state_test_data,
1996 .clean_session = true,
1997 .client_id = aws_byte_cursor_from_c_str("client1234"),
1998 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
1999 .socket_options = &state_test_data->socket_options,
2000 .on_connection_complete = aws_test311_on_connection_complete_fn,
2001 .ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS,
2002 .keep_alive_time_secs = DEFAULT_TEST_KEEP_ALIVE_S,
2003 };
2004
2005 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
2006 struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
2007 struct aws_byte_cursor sub_topic = aws_byte_cursor_from_c_str("/test/topic");
2008
2009 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
2010 aws_test311_wait_for_connection_to_complete(state_test_data);
2011 struct aws_channel_handler *handler = state_test_data->mock_server;
2012
2013 /* Disable the auto ACK packets sent by the server, which blocks the requests to complete */
2014 mqtt_mock_server_disable_auto_ack(handler);
2015
2016 /* make a publish with QoS 1 */
2017 aws_mutex_lock(&state_test_data->lock);
2018 state_test_data->expected_ops_completed = 1;
2019 aws_mutex_unlock(&state_test_data->lock);
2020 uint16_t packet_id_1 = aws_mqtt_client_connection_publish(
2021 state_test_data->mqtt_connection,
2022 &pub_topic,
2023 AWS_MQTT_QOS_AT_LEAST_ONCE,
2024 false,
2025 &payload_1,
2026 aws_test311_on_op_complete,
2027 state_test_data);
2028 ASSERT_TRUE(packet_id_1 > 0);
2029 /* make another subscribe */
2030 uint16_t packet_id_2 = aws_mqtt_client_connection_subscribe(
2031 state_test_data->mqtt_connection,
2032 &sub_topic,
2033 AWS_MQTT_QOS_AT_LEAST_ONCE,
2034 NULL,
2035 NULL,
2036 NULL,
2037 aws_test311_on_suback,
2038 state_test_data);
2039 ASSERT_TRUE(packet_id_2 > 0);
2040
2041 /* Wait for 3 sec. ensure no duplicate requests will be sent */
2042 aws_thread_current_sleep((uint64_t)ONE_SEC * 3);
2043 /* Check all received packets, only one publish and subscribe received */
2044 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
2045 size_t pre_index = SIZE_MAX;
2046 ASSERT_NOT_NULL(mqtt_mock_server_find_decoded_packet_by_type(handler, 0, AWS_MQTT_PACKET_PUBLISH, &pre_index));
2047 if (pre_index + 1 < mqtt_mock_server_decoded_packets_count(handler)) {
2048 /* If it's not the last packet, search again, and result should be NULL. */
2049 ASSERT_NULL(
2050 mqtt_mock_server_find_decoded_packet_by_type(handler, pre_index + 1, AWS_MQTT_PACKET_PUBLISH, NULL));
2051 }
2052 ASSERT_NOT_NULL(mqtt_mock_server_find_decoded_packet_by_type(handler, 0, AWS_MQTT_PACKET_SUBSCRIBE, &pre_index));
2053 if (pre_index + 1 < mqtt_mock_server_decoded_packets_count(handler)) {
2054 ASSERT_NULL(
2055 mqtt_mock_server_find_decoded_packet_by_type(handler, pre_index + 1, AWS_MQTT_PACKET_SUBSCRIBE, NULL));
2056 }
2057
2058 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
2059 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
2060 aws_test311_wait_for_disconnect_to_complete(state_test_data);
2061
2062 return AWS_OP_SUCCESS;
2063 }
2064
2065 AWS_TEST_CASE_FIXTURE(
2066 mqtt_connection_not_resend_packets_on_healthy_connection,
2067 s_setup_mqtt_server_fn,
2068 s_test_mqtt_connection_not_resend_packets_on_healthy_connection_fn,
2069 s_clean_up_mqtt_server_fn,
2070 &test_data)
2071
2072 /* Make requests during offline, and destory the connection before ever online, the resource should be cleaned up
2073 * properly */
2074 static int s_test_mqtt_connection_destory_pending_requests_fn(struct aws_allocator *allocator, void *ctx) {
2075 (void)allocator;
2076 struct mqtt_connection_state_test *state_test_data = ctx;
2077
2078 struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("/test/topic");
2079 struct aws_byte_cursor payload = aws_byte_cursor_from_c_str("Test Message 1");
2080
2081 ASSERT_TRUE(
2082 aws_mqtt_client_connection_publish(
2083 state_test_data->mqtt_connection,
2084 &topic,
2085 AWS_MQTT_QOS_AT_LEAST_ONCE,
2086 false,
2087 &payload,
2088 aws_test311_on_op_complete,
2089 state_test_data) > 0);
2090 ASSERT_TRUE(
2091 aws_mqtt_client_connection_subscribe(
2092 state_test_data->mqtt_connection,
2093 &topic,
2094 AWS_MQTT_QOS_AT_LEAST_ONCE,
2095 aws_test311_on_publish_received,
2096 state_test_data,
2097 NULL,
2098 aws_test311_on_suback,
2099 state_test_data) > 0);
2100
2101 return AWS_OP_SUCCESS;
2102 }
2103
2104 AWS_TEST_CASE_FIXTURE(
2105 mqtt_connection_destory_pending_requests,
2106 s_setup_mqtt_server_fn,
2107 s_test_mqtt_connection_destory_pending_requests_fn,
2108 s_clean_up_mqtt_server_fn,
2109 &test_data)
2110
2111 /* Make a clean session connection, all the request will not be retried when the connection lost */
2112 static int s_test_mqtt_clean_session_not_retry_fn(struct aws_allocator *allocator, void *ctx) {
2113 (void)allocator;
2114 struct mqtt_connection_state_test *state_test_data = ctx;
2115
2116 struct aws_mqtt_connection_options connection_options = {
2117 .user_data = state_test_data,
2118 .clean_session = true, /* make a clean_session connection */
2119 .client_id = aws_byte_cursor_from_c_str("client1234"),
2120 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
2121 .socket_options = &state_test_data->socket_options,
2122 .on_connection_complete = aws_test311_on_connection_complete_fn,
2123 .ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS,
2124 .keep_alive_time_secs = DEFAULT_TEST_KEEP_ALIVE_S,
2125 };
2126
2127 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
2128 aws_test311_wait_for_connection_to_complete(state_test_data);
2129 struct aws_channel_handler *handler = state_test_data->mock_server;
2130 mqtt_mock_server_disable_auto_ack(handler);
2131
2132 struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("/test/topic");
2133 struct aws_byte_cursor payload = aws_byte_cursor_from_c_str("Test Message 1");
2134
2135 ASSERT_TRUE(
2136 aws_mqtt_client_connection_publish(
2137 state_test_data->mqtt_connection,
2138 &topic,
2139 AWS_MQTT_QOS_AT_LEAST_ONCE,
2140 false,
2141 &payload,
2142 aws_test311_on_op_complete,
2143 state_test_data) > 0);
2144 ASSERT_TRUE(
2145 aws_mqtt_client_connection_subscribe(
2146 state_test_data->mqtt_connection,
2147 &topic,
2148 AWS_MQTT_QOS_AT_LEAST_ONCE,
2149 aws_test311_on_publish_received,
2150 state_test_data,
2151 NULL,
2152 aws_test311_on_suback,
2153 state_test_data) > 0);
2154 aws_mutex_lock(&state_test_data->lock);
2155 state_test_data->expected_ops_completed = 1;
2156 aws_mutex_unlock(&state_test_data->lock);
2157 /* Shutdown the connection */
2158 aws_channel_shutdown(state_test_data->server_channel, AWS_OP_SUCCESS);
2159 aws_test311_wait_for_interrupt_to_complete(state_test_data);
2160
2161 /* Once the connection lost, the requests will fail */
2162 ASSERT_UINT_EQUALS(state_test_data->op_complete_error, AWS_ERROR_MQTT_CANCELLED_FOR_CLEAN_SESSION);
2163 ASSERT_UINT_EQUALS(state_test_data->subscribe_complete_error, AWS_ERROR_MQTT_CANCELLED_FOR_CLEAN_SESSION);
2164
2165 /* Disconnect */
2166 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
2167 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
2168 aws_test311_wait_for_disconnect_to_complete(state_test_data);
2169 return AWS_OP_SUCCESS;
2170 }
2171
2172 AWS_TEST_CASE_FIXTURE(
2173 mqtt_clean_session_not_retry,
2174 s_setup_mqtt_server_fn,
2175 s_test_mqtt_clean_session_not_retry_fn,
2176 s_clean_up_mqtt_server_fn,
2177 &test_data)
2178
2179 /* Make a clean session connection, the previous session will be discard when a new connection with clean session true
2180 * is created */
2181 static int s_test_mqtt_clean_session_discard_previous_fn(struct aws_allocator *allocator, void *ctx) {
2182 (void)allocator;
2183 struct mqtt_connection_state_test *state_test_data = ctx;
2184
2185 struct aws_mqtt_connection_options connection_options = {
2186 .user_data = state_test_data,
2187 .clean_session = true, /* make a clean_session connection */
2188 .client_id = aws_byte_cursor_from_c_str("client1234"),
2189 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
2190 .socket_options = &state_test_data->socket_options,
2191 .on_connection_complete = aws_test311_on_connection_complete_fn,
2192 .ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS,
2193 .keep_alive_time_secs = 16960, /* basically stop automatically sending PINGREQ */
2194 };
2195
2196 struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("/test/topic");
2197 struct aws_byte_cursor payload = aws_byte_cursor_from_c_str("Test Message 1");
2198 aws_mutex_lock(&state_test_data->lock);
2199 state_test_data->expected_ops_completed = 1;
2200 aws_mutex_unlock(&state_test_data->lock);
2201 /* Requests made now will be considered as the previous session. */
2202 ASSERT_TRUE(
2203 aws_mqtt_client_connection_publish(
2204 state_test_data->mqtt_connection,
2205 &topic,
2206 AWS_MQTT_QOS_AT_LEAST_ONCE,
2207 false,
2208 &payload,
2209 aws_test311_on_op_complete,
2210 state_test_data) > 0);
2211 ASSERT_TRUE(
2212 aws_mqtt_client_connection_subscribe(
2213 state_test_data->mqtt_connection,
2214 &topic,
2215 AWS_MQTT_QOS_AT_LEAST_ONCE,
2216 aws_test311_on_publish_received,
2217 state_test_data,
2218 NULL,
2219 aws_test311_on_suback,
2220 state_test_data) > 0);
2221
2222 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
2223 aws_test311_wait_for_connection_to_complete(state_test_data);
2224 struct aws_channel_handler *handler = state_test_data->mock_server;
2225
2226 aws_test311_wait_for_ops_completed(state_test_data);
2227 aws_test311_wait_for_subscribe_to_complete(state_test_data);
2228 ASSERT_UINT_EQUALS(state_test_data->op_complete_error, AWS_ERROR_MQTT_CANCELLED_FOR_CLEAN_SESSION);
2229 ASSERT_UINT_EQUALS(state_test_data->subscribe_complete_error, AWS_ERROR_MQTT_CANCELLED_FOR_CLEAN_SESSION);
2230
2231 /* Check no request is received by the server */
2232 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(handler));
2233 ASSERT_NULL(mqtt_mock_server_find_decoded_packet_by_type(handler, 0, AWS_MQTT_PACKET_PUBLISH, NULL));
2234 ASSERT_NULL(mqtt_mock_server_find_decoded_packet_by_type(handler, 0, AWS_MQTT_PACKET_SUBSCRIBE, NULL));
2235
2236 /* Disconnect */
2237 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
2238 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
2239 aws_test311_wait_for_disconnect_to_complete(state_test_data);
2240 return AWS_OP_SUCCESS;
2241 }
2242
2243 AWS_TEST_CASE_FIXTURE(
2244 mqtt_clean_session_discard_previous,
2245 s_setup_mqtt_server_fn,
2246 s_test_mqtt_clean_session_discard_previous_fn,
2247 s_clean_up_mqtt_server_fn,
2248 &test_data)
2249
2250 /* Make a clean session connection, the requests after the connect function will be considered as the next session */
2251 static int s_test_mqtt_clean_session_keep_next_session_fn(struct aws_allocator *allocator, void *ctx) {
2252 (void)allocator;
2253 struct mqtt_connection_state_test *state_test_data = ctx;
2254
2255 struct aws_mqtt_connection_options connection_options = {
2256 .user_data = state_test_data,
2257 .clean_session = true, /* make a clean_session connection */
2258 .client_id = aws_byte_cursor_from_c_str("client1234"),
2259 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
2260 .socket_options = &state_test_data->socket_options,
2261 .on_connection_complete = aws_test311_on_connection_complete_fn,
2262 .ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS,
2263 .keep_alive_time_secs = 16960, /* basically stop automatically sending PINGREQ */
2264 };
2265
2266 struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("/test/topic");
2267 struct aws_byte_cursor payload = aws_byte_cursor_from_c_str("Test Message 1");
2268 aws_mutex_lock(&state_test_data->lock);
2269 state_test_data->expected_ops_completed = 1;
2270 aws_mutex_unlock(&state_test_data->lock);
2271
2272 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
2273 /* Requests made after the connect function will be considered as the next session, and will be sent eventually */
2274 ASSERT_TRUE(
2275 aws_mqtt_client_connection_publish(
2276 state_test_data->mqtt_connection,
2277 &topic,
2278 AWS_MQTT_QOS_AT_LEAST_ONCE,
2279 false,
2280 &payload,
2281 aws_test311_on_op_complete,
2282 state_test_data) > 0);
2283 ASSERT_TRUE(
2284 aws_mqtt_client_connection_subscribe(
2285 state_test_data->mqtt_connection,
2286 &topic,
2287 AWS_MQTT_QOS_AT_LEAST_ONCE,
2288 aws_test311_on_publish_received,
2289 state_test_data,
2290 NULL,
2291 aws_test311_on_suback,
2292 state_test_data) > 0);
2293 aws_test311_wait_for_connection_to_complete(state_test_data);
2294 struct aws_channel_handler *handler = state_test_data->mock_server;
2295
2296 aws_test311_wait_for_ops_completed(state_test_data);
2297 aws_test311_wait_for_subscribe_to_complete(state_test_data);
2298 ASSERT_UINT_EQUALS(state_test_data->op_complete_error, AWS_ERROR_SUCCESS);
2299 ASSERT_UINT_EQUALS(state_test_data->subscribe_complete_error, AWS_ERROR_SUCCESS);
2300
2301 /* Check no request is received by the server */
2302 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(handler));
2303 ASSERT_NOT_NULL(mqtt_mock_server_find_decoded_packet_by_type(handler, 0, AWS_MQTT_PACKET_PUBLISH, NULL));
2304 ASSERT_NOT_NULL(mqtt_mock_server_find_decoded_packet_by_type(handler, 0, AWS_MQTT_PACKET_SUBSCRIBE, NULL));
2305
2306 /* Disconnect */
2307 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
2308 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
2309 aws_test311_wait_for_disconnect_to_complete(state_test_data);
2310 return AWS_OP_SUCCESS;
2311 }
2312
2313 AWS_TEST_CASE_FIXTURE(
2314 mqtt_clean_session_keep_next_session,
2315 s_setup_mqtt_server_fn,
2316 s_test_mqtt_clean_session_keep_next_session_fn,
2317 s_clean_up_mqtt_server_fn,
2318 &test_data)
2319
2320 /**
2321 * Test that connection is healthy, user set the timeout for request, and timeout happens and the publish failed.
2322 */
2323 static int s_test_mqtt_connection_publish_QoS1_timeout_fn(struct aws_allocator *allocator, void *ctx) {
2324 (void)allocator;
2325 struct mqtt_connection_state_test *state_test_data = ctx;
2326
2327 struct aws_mqtt_connection_options connection_options = {
2328 .user_data = state_test_data,
2329 .clean_session = false,
2330 .client_id = aws_byte_cursor_from_c_str("client1234"),
2331 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
2332 .socket_options = &state_test_data->socket_options,
2333 .on_connection_complete = aws_test311_on_connection_complete_fn,
2334 .ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS,
2335 .protocol_operation_timeout_ms = 3000,
2336 .keep_alive_time_secs = 16960, /* basically stop automatically sending PINGREQ */
2337 };
2338
2339 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
2340 struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
2341
2342 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
2343 aws_test311_wait_for_connection_to_complete(state_test_data);
2344
2345 /* Disable the auto ACK packets sent by the server, which blocks the requests to complete */
2346 mqtt_mock_server_disable_auto_ack(state_test_data->mock_server);
2347
2348 /* make a publish with QoS 1 immediate. */
2349 aws_mutex_lock(&state_test_data->lock);
2350 state_test_data->expected_ops_completed = 1;
2351 aws_mutex_unlock(&state_test_data->lock);
2352 uint16_t packet_id_1 = aws_mqtt_client_connection_publish(
2353 state_test_data->mqtt_connection,
2354 &pub_topic,
2355 AWS_MQTT_QOS_AT_LEAST_ONCE,
2356 false,
2357 &payload_1,
2358 aws_test311_on_op_complete,
2359 state_test_data);
2360 ASSERT_TRUE(packet_id_1 > 0);
2361
2362 /* publish should complete after the shutdown */
2363 aws_test311_wait_for_ops_completed(state_test_data);
2364 /* Check the publish has been completed with timeout error */
2365 ASSERT_UINT_EQUALS(state_test_data->op_complete_error, AWS_ERROR_MQTT_TIMEOUT);
2366 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
2367 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
2368 aws_test311_wait_for_disconnect_to_complete(state_test_data);
2369
2370 return AWS_OP_SUCCESS;
2371 }
2372
2373 AWS_TEST_CASE_FIXTURE(
2374 mqtt_connection_publish_QoS1_timeout,
2375 s_setup_mqtt_server_fn,
2376 s_test_mqtt_connection_publish_QoS1_timeout_fn,
2377 s_clean_up_mqtt_server_fn,
2378 &test_data)
2379
2380 /**
2381 * Test that connection is healthy, user set the timeout for request, and timeout happens we still send ping reqs
2382 */
2383 static int s_test_mqtt_connection_publish_QoS1_timeout_with_ping_fn(struct aws_allocator *allocator, void *ctx) {
2384 (void)allocator;
2385 struct mqtt_connection_state_test *state_test_data = ctx;
2386
2387 struct aws_mqtt_connection_options connection_options = {
2388 .user_data = state_test_data,
2389 .clean_session = false,
2390 .client_id = aws_byte_cursor_from_c_str("client1234"),
2391 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
2392 .socket_options = &state_test_data->socket_options,
2393 .on_connection_complete = aws_test311_on_connection_complete_fn,
2394 .ping_timeout_ms = 99,
2395 .protocol_operation_timeout_ms = 20,
2396 .keep_alive_time_secs = 3, // connection->keep_alive_time_ns, pushoff the timestamp by that amount
2397 };
2398
2399 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
2400 struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
2401
2402 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
2403 aws_test311_wait_for_connection_to_complete(state_test_data);
2404
2405 /* Disable the auto ACK packets sent by the server, which blocks the requests to complete */
2406 mqtt_mock_server_disable_auto_ack(state_test_data->mock_server);
2407
2408 /* make a publish with QoS 1 immediate. */
2409 aws_mutex_lock(&state_test_data->lock);
2410 state_test_data->expected_ops_completed = 10;
2411 aws_mutex_unlock(&state_test_data->lock);
2412
2413 for (int i = 0; i < 10; i++) {
2414 uint16_t packet_id_1 = aws_mqtt_client_connection_publish(
2415 state_test_data->mqtt_connection,
2416 &pub_topic,
2417 AWS_MQTT_QOS_AT_LEAST_ONCE,
2418 false,
2419 &payload_1,
2420 aws_test311_on_op_complete,
2421 state_test_data);
2422 ASSERT_TRUE(packet_id_1 > 0);
2423
2424 // sleep 1 second
2425 aws_thread_current_sleep(1000000000);
2426 }
2427
2428 // Wait for 3 seconds
2429 aws_thread_current_sleep(3000000000);
2430
2431 // make sure we are still receiveing pings when the connection is down, in other words ping pushoff is not happening
2432 ASSERT_TRUE(mqtt_mock_server_get_ping_count(state_test_data->mock_server) > 1);
2433
2434 aws_channel_shutdown(state_test_data->server_channel, AWS_OP_SUCCESS);
2435
2436 /* publish should complete after the shutdown */
2437 aws_test311_wait_for_ops_completed(state_test_data);
2438
2439 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
2440 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
2441 aws_test311_wait_for_disconnect_to_complete(state_test_data);
2442
2443 return AWS_OP_SUCCESS;
2444 }
2445
2446 AWS_TEST_CASE_FIXTURE(
2447 mqtt_connection_publish_QoS1_timeout_with_ping,
2448 s_setup_mqtt_server_fn,
2449 s_test_mqtt_connection_publish_QoS1_timeout_with_ping_fn,
2450 s_clean_up_mqtt_server_fn,
2451 &test_data)
2452
2453 /**
2454 * Test that connection is healthy, user set the timeout for request, and timeout happens and the publish failed.
2455 */
2456 static int s_test_mqtt_connection_publish_QoS1_timeout_override_fn(struct aws_allocator *allocator, void *ctx) {
2457 (void)allocator;
2458 struct mqtt_connection_state_test *state_test_data = ctx;
2459
2460 struct aws_mqtt_connection_options connection_options = {
2461 .user_data = state_test_data,
2462 .clean_session = false,
2463 .client_id = aws_byte_cursor_from_c_str("client1234"),
2464 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
2465 .socket_options = &state_test_data->socket_options,
2466 .on_connection_complete = aws_test311_on_connection_complete_fn,
2467 .ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS,
2468 .keep_alive_time_secs = 16960, /* basically stop automatically sending PINGREQ */
2469 };
2470
2471 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
2472 struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
2473
2474 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
2475 aws_test311_wait_for_connection_to_complete(state_test_data);
2476
2477 /* Disable the auto ACK packets sent by the server, which blocks the requests to complete */
2478 mqtt_mock_server_disable_auto_ack(state_test_data->mock_server);
2479
2480 /* make a publish with QoS 1 immediate. */
2481 aws_mutex_lock(&state_test_data->lock);
2482 state_test_data->expected_ops_completed = 1;
2483 aws_mutex_unlock(&state_test_data->lock);
2484 uint16_t packet_id_1 = aws_mqtt_client_connection_311_publish(
2485 state_test_data->mqtt_connection->impl,
2486 &pub_topic,
2487 AWS_MQTT_QOS_AT_LEAST_ONCE,
2488 false,
2489 &payload_1,
2490 aws_test311_on_op_complete,
2491 state_test_data,
2492 aws_timestamp_convert(3, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL));
2493 ASSERT_TRUE(packet_id_1 > 0);
2494
2495 /* publish should complete after the shutdown */
2496 aws_test311_wait_for_ops_completed(state_test_data);
2497 /* Check the publish has been completed with timeout error */
2498 ASSERT_UINT_EQUALS(state_test_data->op_complete_error, AWS_ERROR_MQTT_TIMEOUT);
2499 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
2500 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
2501 aws_test311_wait_for_disconnect_to_complete(state_test_data);
2502
2503 return AWS_OP_SUCCESS;
2504 }
2505
2506 AWS_TEST_CASE_FIXTURE(
2507 mqtt_connection_publish_QoS1_timeout_override,
2508 s_setup_mqtt_server_fn,
2509 s_test_mqtt_connection_publish_QoS1_timeout_override_fn,
2510 s_clean_up_mqtt_server_fn,
2511 &test_data)
2512
2513 /**
2514 * Test that connection is healthy, user set the timeout for request, and timeout happens and the unsubscribe failed.
2515 */
2516 static int s_test_mqtt_connection_unsubscribe_timeout_fn(struct aws_allocator *allocator, void *ctx) {
2517 (void)allocator;
2518 struct mqtt_connection_state_test *state_test_data = ctx;
2519
2520 struct aws_mqtt_connection_options connection_options = {
2521 .user_data = state_test_data,
2522 .clean_session = false,
2523 .client_id = aws_byte_cursor_from_c_str("client1234"),
2524 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
2525 .socket_options = &state_test_data->socket_options,
2526 .on_connection_complete = aws_test311_on_connection_complete_fn,
2527 .ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS,
2528 .protocol_operation_timeout_ms = 3000,
2529 .keep_alive_time_secs = 16960, /* basically stop automatically sending PINGREQ */
2530 };
2531
2532 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
2533
2534 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
2535 aws_test311_wait_for_connection_to_complete(state_test_data);
2536
2537 /* Disable the auto ACK packets sent by the server, which blocks the requests to complete */
2538 mqtt_mock_server_disable_auto_ack(state_test_data->mock_server);
2539
2540 aws_mutex_lock(&state_test_data->lock);
2541 state_test_data->expected_ops_completed = 1;
2542 aws_mutex_unlock(&state_test_data->lock);
2543
2544 /* unsubscribe to the first topic */
2545 uint16_t unsub_packet_id = aws_mqtt_client_connection_unsubscribe(
2546 state_test_data->mqtt_connection, &pub_topic, aws_test311_on_op_complete, state_test_data);
2547 ASSERT_TRUE(unsub_packet_id > 0);
2548
2549 /* unsubscribe should complete after the timeout */
2550 aws_test311_wait_for_ops_completed(state_test_data);
2551 /* Check that the unsubscribe has been completed with a timeout error */
2552 ASSERT_UINT_EQUALS(state_test_data->op_complete_error, AWS_ERROR_MQTT_TIMEOUT);
2553 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
2554 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
2555 aws_test311_wait_for_disconnect_to_complete(state_test_data);
2556
2557 return AWS_OP_SUCCESS;
2558 }
2559
2560 AWS_TEST_CASE_FIXTURE(
2561 mqtt_connection_unsubscribe_timeout,
2562 s_setup_mqtt_server_fn,
2563 s_test_mqtt_connection_unsubscribe_timeout_fn,
2564 s_clean_up_mqtt_server_fn,
2565 &test_data)
2566
2567 /**
2568 * Test that connection is healthy, user set the timeout for request, and timeout happens and the unsubscribe failed.
2569 */
2570 static int s_test_mqtt_connection_unsubscribe_timeout_override_fn(struct aws_allocator *allocator, void *ctx) {
2571 (void)allocator;
2572 struct mqtt_connection_state_test *state_test_data = ctx;
2573
2574 struct aws_mqtt_connection_options connection_options = {
2575 .user_data = state_test_data,
2576 .clean_session = false,
2577 .client_id = aws_byte_cursor_from_c_str("client1234"),
2578 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
2579 .socket_options = &state_test_data->socket_options,
2580 .on_connection_complete = aws_test311_on_connection_complete_fn,
2581 .ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS,
2582 .keep_alive_time_secs = 16960, /* basically stop automatically sending PINGREQ */
2583 };
2584
2585 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
2586
2587 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
2588 aws_test311_wait_for_connection_to_complete(state_test_data);
2589
2590 /* Disable the auto ACK packets sent by the server, which blocks the requests to complete */
2591 mqtt_mock_server_disable_auto_ack(state_test_data->mock_server);
2592
2593 aws_mutex_lock(&state_test_data->lock);
2594 state_test_data->expected_ops_completed = 1;
2595 aws_mutex_unlock(&state_test_data->lock);
2596
2597 /* unsubscribe to the first topic */
2598 uint16_t unsub_packet_id = aws_mqtt_client_connection_311_unsubscribe(
2599 state_test_data->mqtt_connection->impl,
2600 &pub_topic,
2601 aws_test311_on_op_complete,
2602 state_test_data,
2603 aws_timestamp_convert(3, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL));
2604 ASSERT_TRUE(unsub_packet_id > 0);
2605
2606 /* unsubscribe should complete after the timeout */
2607 aws_test311_wait_for_ops_completed(state_test_data);
2608 /* Check that the unsubscribe has been completed with a timeout error */
2609 ASSERT_UINT_EQUALS(state_test_data->op_complete_error, AWS_ERROR_MQTT_TIMEOUT);
2610 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
2611 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
2612 aws_test311_wait_for_disconnect_to_complete(state_test_data);
2613
2614 return AWS_OP_SUCCESS;
2615 }
2616
2617 AWS_TEST_CASE_FIXTURE(
2618 mqtt_connection_unsubscribe_timeout_override,
2619 s_setup_mqtt_server_fn,
2620 s_test_mqtt_connection_unsubscribe_timeout_override_fn,
2621 s_clean_up_mqtt_server_fn,
2622 &test_data)
2623
2624 /**
2625 * Test that connection is healthy, user set the timeout for request, and timeout happens and the subscribe failed.
2626 */
2627 static int s_test_mqtt_connection_subscribe_single_timeout_fn(struct aws_allocator *allocator, void *ctx) {
2628 (void)allocator;
2629 struct mqtt_connection_state_test *state_test_data = ctx;
2630
2631 struct aws_mqtt_connection_options connection_options = {
2632 .user_data = state_test_data,
2633 .clean_session = false,
2634 .client_id = aws_byte_cursor_from_c_str("client1234"),
2635 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
2636 .socket_options = &state_test_data->socket_options,
2637 .on_connection_complete = aws_test311_on_connection_complete_fn,
2638 .ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS,
2639 .protocol_operation_timeout_ms = 3000,
2640 .keep_alive_time_secs = 16960, /* basically stop automatically sending PINGREQ */
2641 };
2642
2643 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
2644 aws_test311_wait_for_connection_to_complete(state_test_data);
2645
2646 /* Disable the auto ACK packets sent by the server, which blocks the requests to complete */
2647 mqtt_mock_server_disable_auto_ack(state_test_data->mock_server);
2648
2649 /* subscribe */
2650 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
2651 uint16_t sub_packet_id = aws_mqtt_client_connection_subscribe(
2652 state_test_data->mqtt_connection,
2653 &pub_topic,
2654 AWS_MQTT_QOS_AT_LEAST_ONCE,
2655 aws_test311_on_publish_received,
2656 state_test_data,
2657 NULL,
2658 aws_test311_on_suback,
2659 state_test_data);
2660 ASSERT_TRUE(sub_packet_id > 0);
2661
2662 /* subscribe should complete after the timeout */
2663 aws_test311_wait_for_subscribe_to_complete(state_test_data);
2664 /* Check that the subscribe has been completed with a timeout error */
2665 ASSERT_UINT_EQUALS(state_test_data->subscribe_complete_error, AWS_ERROR_MQTT_TIMEOUT);
2666
2667 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
2668 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
2669 aws_test311_wait_for_disconnect_to_complete(state_test_data);
2670
2671 return AWS_OP_SUCCESS;
2672 }
2673
2674 AWS_TEST_CASE_FIXTURE(
2675 mqtt_connection_subscribe_single_timeout,
2676 s_setup_mqtt_server_fn,
2677 s_test_mqtt_connection_subscribe_single_timeout_fn,
2678 s_clean_up_mqtt_server_fn,
2679 &test_data)
2680
2681 /**
2682 * Test that connection is healthy, user set the timeout for request, and timeout happens and the subscribe failed.
2683 */
2684 static int s_test_mqtt_connection_subscribe_single_timeout_override_fn(struct aws_allocator *allocator, void *ctx) {
2685 (void)allocator;
2686 struct mqtt_connection_state_test *state_test_data = ctx;
2687
2688 struct aws_mqtt_connection_options connection_options = {
2689 .user_data = state_test_data,
2690 .clean_session = false,
2691 .client_id = aws_byte_cursor_from_c_str("client1234"),
2692 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
2693 .socket_options = &state_test_data->socket_options,
2694 .on_connection_complete = aws_test311_on_connection_complete_fn,
2695 .ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS,
2696 .keep_alive_time_secs = 16960, /* basically stop automatically sending PINGREQ */
2697 };
2698
2699 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
2700 aws_test311_wait_for_connection_to_complete(state_test_data);
2701
2702 /* Disable the auto ACK packets sent by the server, which blocks the requests to complete */
2703 mqtt_mock_server_disable_auto_ack(state_test_data->mock_server);
2704
2705 /* subscribe */
2706 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
2707 uint16_t sub_packet_id = aws_mqtt_client_connection_311_subscribe(
2708 state_test_data->mqtt_connection->impl,
2709 &pub_topic,
2710 AWS_MQTT_QOS_AT_LEAST_ONCE,
2711 aws_test311_on_publish_received,
2712 state_test_data,
2713 NULL,
2714 aws_test311_on_suback,
2715 state_test_data,
2716 aws_timestamp_convert(3, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL));
2717 ASSERT_TRUE(sub_packet_id > 0);
2718
2719 /* subscribe should complete after the timeout */
2720 aws_test311_wait_for_subscribe_to_complete(state_test_data);
2721 /* Check that the subscribe has been completed with a timeout error */
2722 ASSERT_UINT_EQUALS(state_test_data->subscribe_complete_error, AWS_ERROR_MQTT_TIMEOUT);
2723
2724 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
2725 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
2726 aws_test311_wait_for_disconnect_to_complete(state_test_data);
2727
2728 return AWS_OP_SUCCESS;
2729 }
2730
2731 AWS_TEST_CASE_FIXTURE(
2732 mqtt_connection_subscribe_single_timeout_override,
2733 s_setup_mqtt_server_fn,
2734 s_test_mqtt_connection_subscribe_single_timeout_override_fn,
2735 s_clean_up_mqtt_server_fn,
2736 &test_data)
2737
2738 /**
2739 * Test that connection is healthy, user set the timeout for request, and timeout happens and the multi-subscribe
2740 * failed.
2741 */
2742 static int s_test_mqtt_connection_subscribe_multi_timeout_fn(struct aws_allocator *allocator, void *ctx) {
2743 (void)allocator;
2744 struct mqtt_connection_state_test *state_test_data = ctx;
2745
2746 struct aws_mqtt_connection_options connection_options = {
2747 .user_data = state_test_data,
2748 .clean_session = false,
2749 .client_id = aws_byte_cursor_from_c_str("client1234"),
2750 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
2751 .socket_options = &state_test_data->socket_options,
2752 .on_connection_complete = aws_test311_on_connection_complete_fn,
2753 .ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS,
2754 .protocol_operation_timeout_ms = 3000,
2755 .keep_alive_time_secs = 16960, /* basically stop automatically sending PINGREQ */
2756 };
2757
2758 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
2759 aws_test311_wait_for_connection_to_complete(state_test_data);
2760
2761 /* Disable the auto ACK packets sent by the server, which blocks the requests to complete */
2762 mqtt_mock_server_disable_auto_ack(state_test_data->mock_server);
2763
2764 struct aws_byte_cursor sub_topic_1 = aws_byte_cursor_from_c_str("/test/topic1");
2765 struct aws_byte_cursor sub_topic_2 = aws_byte_cursor_from_c_str("/test/topic2");
2766
2767 struct aws_mqtt_topic_subscription sub1 = {
2768 .topic = sub_topic_1,
2769 .qos = AWS_MQTT_QOS_AT_LEAST_ONCE,
2770 .on_publish = aws_test311_on_publish_received,
2771 .on_cleanup = NULL,
2772 .on_publish_ud = state_test_data,
2773 };
2774 struct aws_mqtt_topic_subscription sub2 = {
2775 .topic = sub_topic_2,
2776 .qos = AWS_MQTT_QOS_AT_LEAST_ONCE,
2777 .on_publish = aws_test311_on_publish_received,
2778 .on_cleanup = NULL,
2779 .on_publish_ud = state_test_data,
2780 };
2781
2782 struct aws_array_list topic_filters;
2783 size_t list_len = 2;
2784 AWS_VARIABLE_LENGTH_ARRAY(uint8_t, static_buf, list_len * sizeof(struct aws_mqtt_topic_subscription));
2785 aws_array_list_init_static(&topic_filters, static_buf, list_len, sizeof(struct aws_mqtt_topic_subscription));
2786
2787 aws_array_list_push_back(&topic_filters, &sub1);
2788 aws_array_list_push_back(&topic_filters, &sub2);
2789
2790 uint16_t sub_packet_id = aws_mqtt_client_connection_subscribe_multiple(
2791 state_test_data->mqtt_connection, &topic_filters, aws_test311_on_multi_suback, state_test_data);
2792
2793 ASSERT_TRUE(sub_packet_id > 0);
2794
2795 /* subscribe should complete after the timeout */
2796 aws_test311_wait_for_subscribe_to_complete(state_test_data);
2797 /* Check that the subscribe has been completed with a timeout error */
2798 ASSERT_UINT_EQUALS(state_test_data->subscribe_complete_error, AWS_ERROR_MQTT_TIMEOUT);
2799
2800 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
2801 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
2802 aws_test311_wait_for_disconnect_to_complete(state_test_data);
2803
2804 return AWS_OP_SUCCESS;
2805 }
2806
2807 AWS_TEST_CASE_FIXTURE(
2808 mqtt_connection_subscribe_multi_timeout,
2809 s_setup_mqtt_server_fn,
2810 s_test_mqtt_connection_subscribe_multi_timeout_fn,
2811 s_clean_up_mqtt_server_fn,
2812 &test_data)
2813
2814 /**
2815 * Test that connection is healthy, user set the timeout for request, and timeout happens and the subscribe failed.
2816 */
2817 static int s_test_mqtt_connection_resubscribe_timeout_fn(struct aws_allocator *allocator, void *ctx) {
2818 (void)allocator;
2819 struct mqtt_connection_state_test *state_test_data = ctx;
2820
2821 struct aws_mqtt_connection_options connection_options = {
2822 .user_data = state_test_data,
2823 .clean_session = false,
2824 .client_id = aws_byte_cursor_from_c_str("client1234"),
2825 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
2826 .socket_options = &state_test_data->socket_options,
2827 .on_connection_complete = aws_test311_on_connection_complete_fn,
2828 .ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS,
2829 .protocol_operation_timeout_ms = 3000,
2830 .keep_alive_time_secs = 16960, /* basically stop automatically sending PINGREQ */
2831 };
2832
2833 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
2834 aws_test311_wait_for_connection_to_complete(state_test_data);
2835
2836 /* subscribe */
2837 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
2838 uint16_t sub_packet_id = aws_mqtt_client_connection_subscribe(
2839 state_test_data->mqtt_connection,
2840 &pub_topic,
2841 AWS_MQTT_QOS_AT_LEAST_ONCE,
2842 aws_test311_on_publish_received,
2843 state_test_data,
2844 NULL,
2845 aws_test311_on_suback,
2846 state_test_data);
2847 ASSERT_TRUE(sub_packet_id > 0);
2848
2849 /* subscribe should complete after the timeout */
2850 aws_test311_wait_for_subscribe_to_complete(state_test_data);
2851 ASSERT_UINT_EQUALS(state_test_data->subscribe_complete_error, AWS_ERROR_SUCCESS);
2852
2853 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
2854 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
2855 aws_test311_wait_for_disconnect_to_complete(state_test_data);
2856
2857 /* reconnection to the same server */
2858 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
2859 aws_test311_wait_for_connection_to_complete(state_test_data);
2860
2861 /* Disable the auto ACK packets sent by the server, which blocks the requests to complete */
2862 mqtt_mock_server_disable_auto_ack(state_test_data->mock_server);
2863
2864 uint16_t resub_packet_id = aws_mqtt_resubscribe_existing_topics(
2865 state_test_data->mqtt_connection, aws_test311_on_multi_suback, state_test_data);
2866 ASSERT_TRUE(resub_packet_id > 0);
2867 aws_test311_wait_for_subscribe_to_complete(state_test_data);
2868
2869 /* Check that the resubscribe has been completed with a timeout error */
2870 ASSERT_UINT_EQUALS(state_test_data->subscribe_complete_error, AWS_ERROR_MQTT_TIMEOUT);
2871
2872 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
2873 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
2874 aws_test311_wait_for_disconnect_to_complete(state_test_data);
2875
2876 return AWS_OP_SUCCESS;
2877 }
2878
2879 AWS_TEST_CASE_FIXTURE(
2880 mqtt_connection_resubscribe_timeout,
2881 s_setup_mqtt_server_fn,
2882 s_test_mqtt_connection_resubscribe_timeout_fn,
2883 s_clean_up_mqtt_server_fn,
2884 &test_data)
2885
2886 /**
2887 * Test that connection is healthy, user set the timeout for request, and connection lost will reset timeout.
2888 */
2889 static int s_test_mqtt_connection_publish_QoS1_timeout_connection_lost_reset_time_fn(
2890 struct aws_allocator *allocator,
2891 void *ctx) {
2892 (void)allocator;
2893 struct mqtt_connection_state_test *state_test_data = ctx;
2894
2895 struct aws_mqtt_connection_options connection_options = {
2896 .user_data = state_test_data,
2897 .clean_session = false,
2898 .client_id = aws_byte_cursor_from_c_str("client1234"),
2899 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
2900 .socket_options = &state_test_data->socket_options,
2901 .on_connection_complete = aws_test311_on_connection_complete_fn,
2902 .ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS,
2903 .protocol_operation_timeout_ms = 3000,
2904 .keep_alive_time_secs = 16960, /* basically stop automatically sending PINGREQ */
2905 };
2906
2907 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
2908 struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
2909
2910 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
2911 aws_test311_wait_for_connection_to_complete(state_test_data);
2912
2913 /* Disable the auto ACK packets sent by the server, which blocks the requests to complete */
2914 mqtt_mock_server_disable_auto_ack(state_test_data->mock_server);
2915
2916 /* make a publish with QoS 1 immediate. */
2917 aws_mutex_lock(&state_test_data->lock);
2918 state_test_data->expected_ops_completed = 1;
2919 aws_mutex_unlock(&state_test_data->lock);
2920 uint16_t packet_id_1 = aws_mqtt_client_connection_publish(
2921 state_test_data->mqtt_connection,
2922 &pub_topic,
2923 AWS_MQTT_QOS_AT_LEAST_ONCE,
2924 false,
2925 &payload_1,
2926 aws_test311_on_op_complete,
2927 state_test_data);
2928 ASSERT_TRUE(packet_id_1 > 0);
2929
2930 /* sleep for 2 sec, close the connection */
2931 aws_thread_current_sleep((uint64_t)ONE_SEC * 2);
2932 /* Kill the connection, the requests will be retried and the timeout will be reset. */
2933 aws_channel_shutdown(state_test_data->server_channel, AWS_ERROR_INVALID_STATE);
2934 aws_test311_wait_for_reconnect_to_complete(state_test_data);
2935 /* sleep for 2 sec again, in total the response has not received for more than 4 sec, timeout should happen if the
2936 * lost of connection not reset the timeout */
2937 aws_thread_current_sleep((uint64_t)ONE_SEC * 2);
2938 /* send a puback */
2939 ASSERT_SUCCESS(mqtt_mock_server_send_puback(state_test_data->mock_server, packet_id_1));
2940
2941 /* publish should complete after the shutdown */
2942 aws_test311_wait_for_ops_completed(state_test_data);
2943 /* Check the publish has been completed successfully since the lost of the connection reset the timeout */
2944 ASSERT_UINT_EQUALS(state_test_data->op_complete_error, AWS_ERROR_SUCCESS);
2945 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
2946 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
2947 aws_test311_wait_for_disconnect_to_complete(state_test_data);
2948
2949 return AWS_OP_SUCCESS;
2950 }
2951
2952 AWS_TEST_CASE_FIXTURE(
2953 mqtt_connection_publish_QoS1_timeout_connection_lost_reset_time,
2954 s_setup_mqtt_server_fn,
2955 s_test_mqtt_connection_publish_QoS1_timeout_connection_lost_reset_time_fn,
2956 s_clean_up_mqtt_server_fn,
2957 &test_data)
2958
2959 /* Function called for testing the on_connection_closed callback */
2960 static void s_on_connection_closed_fn(
2961 struct aws_mqtt_client_connection *connection,
2962 struct on_connection_closed_data *data,
2963 void *userdata) {
2964 (void)connection;
2965 (void)data;
2966
2967 struct mqtt_connection_state_test *state_test_data = (struct mqtt_connection_state_test *)userdata;
2968
2969 aws_mutex_lock(&state_test_data->lock);
2970 state_test_data->connection_close_calls += 1;
2971 aws_mutex_unlock(&state_test_data->lock);
2972 }
2973
2974 /**
2975 * Test that the connection close callback is fired only once and when the connection was closed
2976 */
2977 static int s_test_mqtt_connection_close_callback_simple_fn(struct aws_allocator *allocator, void *ctx) {
2978 (void)allocator;
2979 struct mqtt_connection_state_test *state_test_data = ctx;
2980
2981 struct aws_mqtt_connection_options connection_options = {
2982 .user_data = state_test_data,
2983 .clean_session = false,
2984 .client_id = aws_byte_cursor_from_c_str("client1234"),
2985 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
2986 .socket_options = &state_test_data->socket_options,
2987 .on_connection_complete = aws_test311_on_connection_complete_fn,
2988 };
2989 aws_mqtt_client_connection_set_connection_closed_handler(
2990 state_test_data->mqtt_connection, s_on_connection_closed_fn, state_test_data);
2991
2992 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
2993 aws_test311_wait_for_connection_to_complete(state_test_data);
2994
2995 /* sleep for 2 sec, just to make sure the connection is stable */
2996 aws_thread_current_sleep((uint64_t)ONE_SEC * 2);
2997
2998 /* Disconnect */
2999 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
3000 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
3001 aws_test311_wait_for_disconnect_to_complete(state_test_data);
3002
3003 /* Make sure the callback was called and the value is what we expect */
3004 ASSERT_UINT_EQUALS(1, state_test_data->connection_close_calls);
3005
3006 return AWS_OP_SUCCESS;
3007 }
3008
3009 AWS_TEST_CASE_FIXTURE(
3010 mqtt_connection_close_callback_simple,
3011 s_setup_mqtt_server_fn,
3012 s_test_mqtt_connection_close_callback_simple_fn,
3013 s_clean_up_mqtt_server_fn,
3014 &test_data)
3015
3016 /**
3017 * Test that the connection close callback is NOT fired during an interrupt
3018 */
3019 static int s_test_mqtt_connection_close_callback_interrupted_fn(struct aws_allocator *allocator, void *ctx) {
3020 (void)allocator;
3021 struct mqtt_connection_state_test *state_test_data = ctx;
3022
3023 struct aws_mqtt_connection_options connection_options = {
3024 .user_data = state_test_data,
3025 .clean_session = false,
3026 .client_id = aws_byte_cursor_from_c_str("client1234"),
3027 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
3028 .socket_options = &state_test_data->socket_options,
3029 .on_connection_complete = aws_test311_on_connection_complete_fn,
3030 };
3031 aws_mqtt_client_connection_set_connection_closed_handler(
3032 state_test_data->mqtt_connection, s_on_connection_closed_fn, state_test_data);
3033
3034 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
3035 aws_test311_wait_for_connection_to_complete(state_test_data);
3036
3037 /* Kill the connection */
3038 aws_channel_shutdown(state_test_data->server_channel, AWS_ERROR_INVALID_STATE);
3039 aws_test311_wait_for_reconnect_to_complete(state_test_data);
3040
3041 /* sleep for 2 sec, just to make sure the connection is stable */
3042 aws_thread_current_sleep((uint64_t)ONE_SEC * 2);
3043
3044 /* Disconnect */
3045 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
3046 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
3047 aws_test311_wait_for_disconnect_to_complete(state_test_data);
3048
3049 /* Make sure the callback was called only ONCE and the value is what we expect */
3050 ASSERT_UINT_EQUALS(1, state_test_data->connection_close_calls);
3051
3052 return AWS_OP_SUCCESS;
3053 }
3054
3055 AWS_TEST_CASE_FIXTURE(
3056 mqtt_connection_close_callback_interrupted,
3057 s_setup_mqtt_server_fn,
3058 s_test_mqtt_connection_close_callback_interrupted_fn,
3059 s_clean_up_mqtt_server_fn,
3060 &test_data)
3061
3062 /**
3063 * Test that the connection close callback is called every time a disconnect happens, if it happens multiple times
3064 */
3065 static int s_test_mqtt_connection_close_callback_multi_fn(struct aws_allocator *allocator, void *ctx) {
3066 (void)allocator;
3067 struct mqtt_connection_state_test *state_test_data = ctx;
3068
3069 struct aws_mqtt_connection_options connection_options = {
3070 .user_data = state_test_data,
3071 .clean_session = false,
3072 .client_id = aws_byte_cursor_from_c_str("client1234"),
3073 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
3074 .socket_options = &state_test_data->socket_options,
3075 .on_connection_complete = aws_test311_on_connection_complete_fn,
3076 };
3077 aws_mqtt_client_connection_set_connection_closed_handler(
3078 state_test_data->mqtt_connection, s_on_connection_closed_fn, state_test_data);
3079
3080 int disconnect_amount = 10;
3081 for (int i = 0; i < disconnect_amount; i++) {
3082 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
3083 aws_test311_wait_for_connection_to_complete(state_test_data);
3084
3085 /* Disconnect */
3086 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
3087 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
3088 aws_test311_wait_for_disconnect_to_complete(state_test_data);
3089 }
3090
3091 /* Make sure the callback was called disconnect_amount times */
3092 ASSERT_UINT_EQUALS(disconnect_amount, state_test_data->connection_close_calls);
3093
3094 return AWS_OP_SUCCESS;
3095 }
3096
3097 AWS_TEST_CASE_FIXTURE(
3098 mqtt_connection_close_callback_multi,
3099 s_setup_mqtt_server_fn,
3100 s_test_mqtt_connection_close_callback_multi_fn,
3101 s_clean_up_mqtt_server_fn,
3102 &test_data)
3103
3104 static void s_test311_on_close_set_handler_fn(
3105 struct aws_mqtt_client_connection *connection,
3106 struct on_connection_closed_data *data,
3107 void *userdata) {
3108 (void)connection;
3109 (void)data;
3110 struct mqtt_connection_state_test *state_test_data = userdata;
3111 AWS_LOGF_DEBUG(TEST_LOG_SUBJECT, "close completed");
3112 aws_mutex_lock(&state_test_data->lock);
3113 state_test_data->connection_close_calls += 1;
3114 // set connection handler should failed in a close_set failure connect
3115 state_test_data->operation_set_result = aws_mqtt_client_connection_set_connection_closed_handler(
3116 connection, s_on_connection_closed_fn, state_test_data);
3117 aws_mutex_unlock(&state_test_data->lock);
3118
3119 aws_condition_variable_notify_one(&state_test_data->cvar);
3120 }
3121
3122 /**
3123 * Test that set close handler in on close callback should fail.
3124 */
3125 static int s_test_mqtt_connection_close_callback_set_failure_fn(struct aws_allocator *allocator, void *ctx) {
3126 (void)allocator;
3127 struct mqtt_connection_state_test *state_test_data = ctx;
3128
3129 struct aws_mqtt_connection_options connection_options = {
3130 .user_data = state_test_data,
3131 .clean_session = false,
3132 .client_id = aws_byte_cursor_from_c_str("client1234"),
3133 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
3134 .socket_options = &state_test_data->socket_options,
3135 .on_connection_complete = aws_test311_on_connection_complete_fn,
3136 };
3137 aws_mqtt_client_connection_set_connection_closed_handler(
3138 state_test_data->mqtt_connection, s_test311_on_close_set_handler_fn, state_test_data);
3139
3140 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
3141 aws_test311_wait_for_connection_to_complete(state_test_data);
3142
3143 /* sleep for 2 sec, just to make sure the connection is stable */
3144 aws_thread_current_sleep((uint64_t)ONE_SEC * 2);
3145
3146 /* reset the operation result */
3147 state_test_data->operation_set_result = AWS_OP_SUCCESS;
3148
3149 /* Disconnect */
3150 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
3151 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
3152 aws_test311_wait_for_disconnect_to_complete(state_test_data);
3153
3154 /* Make sure the callback was called and the value is what we expect */
3155 ASSERT_UINT_EQUALS(1, state_test_data->connection_close_calls);
3156 /* Set callbacks in callback function should fail */
3157 ASSERT_FAILS(state_test_data->operation_set_result);
3158
3159 return AWS_OP_SUCCESS;
3160 }
3161
3162 AWS_TEST_CASE_FIXTURE(
3163 mqtt_connection_close_callback_set_failure,
3164 s_setup_mqtt_server_fn,
3165 s_test_mqtt_connection_close_callback_set_failure_fn,
3166 s_clean_up_mqtt_server_fn,
3167 &test_data)
3168
3169 /**
3170 * Test that set close handler while connecting should fail
3171 */
3172 static int s_test_mqtt_connection_close_callback_set_during_connecting_fn(struct aws_allocator *allocator, void *ctx) {
3173 (void)allocator;
3174 struct mqtt_connection_state_test *state_test_data = ctx;
3175
3176 struct aws_mqtt_connection_options connection_options = {
3177 .user_data = state_test_data,
3178 .clean_session = false,
3179 .client_id = aws_byte_cursor_from_c_str("client1234"),
3180 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
3181 .socket_options = &state_test_data->socket_options,
3182 .on_connection_complete = aws_test311_on_connection_complete_fn,
3183 };
3184
3185 /* Disable server CONNACK packet, so the client would be kept in CONNECTING status. */
3186 mqtt_mock_server_set_max_connack(state_test_data->mock_server, 0);
3187
3188 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
3189
3190 /* set handler should fail while connecting */
3191 ASSERT_FAILS(aws_mqtt_client_connection_set_connection_closed_handler(
3192 state_test_data->mqtt_connection, s_test311_on_close_set_handler_fn, state_test_data));
3193
3194 aws_test311_wait_for_connection_to_fail(state_test_data);
3195
3196 return AWS_OP_SUCCESS;
3197 }
3198
3199 AWS_TEST_CASE_FIXTURE(
3200 mqtt_connection_close_callback_set_during_connecting,
3201 s_setup_mqtt_server_fn,
3202 s_test_mqtt_connection_close_callback_set_during_connecting_fn,
3203 s_clean_up_mqtt_server_fn,
3204 &test_data)
3205
3206 static int s_test_mqtt_connection_reconnection_backoff_stable(struct aws_allocator *allocator, void *ctx) {
3207
3208 (void)allocator;
3209 struct mqtt_connection_state_test *state_test_data = ctx;
3210
3211 struct aws_mqtt_connection_options connection_options = {
3212 .user_data = state_test_data,
3213 .clean_session = false,
3214
3215 .client_id = aws_byte_cursor_from_c_str("client1234"),
3216 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
3217 .socket_options = &state_test_data->socket_options,
3218 .on_connection_complete = aws_test311_on_connection_complete_fn,
3219 };
3220
3221 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
3222 aws_test311_wait_for_connection_to_complete(state_test_data);
3223
3224 uint64_t time_before = 0;
3225 uint64_t time_after = 0;
3226 for (int i = 0; i < 3; i++) {
3227 /* sleep for AWS_RESET_RECONNECT_BACKOFF_DELAY_SECONDS to make sure our connection is successful */
3228 aws_thread_current_sleep(
3229 (uint64_t)ONE_SEC * AWS_RESET_RECONNECT_BACKOFF_DELAY_SECONDS +
3230 RECONNECT_BACKOFF_DELAY_ERROR_MARGIN_NANO_SECONDS);
3231
3232 aws_high_res_clock_get_ticks(&time_before);
3233
3234 /* shut it down and make sure the client automatically reconnects.*/
3235 aws_channel_shutdown(state_test_data->server_channel, AWS_OP_SUCCESS);
3236 aws_test311_wait_for_reconnect_to_complete(state_test_data);
3237
3238 aws_high_res_clock_get_ticks(&time_after);
3239
3240 uint64_t reconnection_backoff_time = time_after - time_before;
3241 uint64_t remainder = 0;
3242 ASSERT_TRUE(
3243 aws_timestamp_convert(reconnection_backoff_time, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, &remainder) ==
3244 DEFAULT_MIN_RECONNECT_DELAY_SECONDS);
3245 ASSERT_TRUE(remainder <= RECONNECT_BACKOFF_DELAY_ERROR_MARGIN_NANO_SECONDS);
3246 }
3247
3248 /* Disconnect */
3249 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
3250 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
3251 aws_test311_wait_for_disconnect_to_complete(state_test_data);
3252
3253 return AWS_OP_SUCCESS;
3254 }
3255
3256 AWS_TEST_CASE_FIXTURE(
3257 mqtt_connection_reconnection_backoff_stable,
3258 s_setup_mqtt_server_fn,
3259 s_test_mqtt_connection_reconnection_backoff_stable,
3260 s_clean_up_mqtt_server_fn,
3261 &test_data)
3262
3263 static int s_test_mqtt_connection_reconnection_backoff_unstable(struct aws_allocator *allocator, void *ctx) {
3264
3265 (void)allocator;
3266 struct mqtt_connection_state_test *state_test_data = ctx;
3267
3268 struct aws_mqtt_connection_options connection_options = {
3269 .user_data = state_test_data,
3270 .clean_session = false,
3271
3272 .client_id = aws_byte_cursor_from_c_str("client1234"),
3273 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
3274 .socket_options = &state_test_data->socket_options,
3275 .on_connection_complete = aws_test311_on_connection_complete_fn,
3276 };
3277
3278 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
3279 aws_test311_wait_for_connection_to_complete(state_test_data);
3280
3281 uint64_t time_before = 0;
3282 uint64_t time_after = 0;
3283 uint64_t expected_reconnect_backoff = 1;
3284 for (int i = 0; i < 3; i++) {
3285
3286 aws_high_res_clock_get_ticks(&time_before);
3287
3288 /* shut it down and make sure the client automatically reconnects.*/
3289 aws_channel_shutdown(state_test_data->server_channel, AWS_OP_SUCCESS);
3290 aws_test311_wait_for_reconnect_to_complete(state_test_data);
3291
3292 aws_high_res_clock_get_ticks(&time_after);
3293
3294 uint64_t reconnection_backoff = time_after - time_before;
3295 uint64_t remainder = 0;
3296 ASSERT_TRUE(
3297 aws_timestamp_convert(reconnection_backoff, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, &remainder) ==
3298 expected_reconnect_backoff);
3299 ASSERT_TRUE(remainder <= RECONNECT_BACKOFF_DELAY_ERROR_MARGIN_NANO_SECONDS);
3300
3301 // Increase the exponential backoff
3302 expected_reconnect_backoff = aws_min_u64(expected_reconnect_backoff * 2, 10);
3303 }
3304
3305 /* Disconnect */
3306 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
3307 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
3308 aws_test311_wait_for_disconnect_to_complete(state_test_data);
3309
3310 return AWS_OP_SUCCESS;
3311 }
3312
3313 AWS_TEST_CASE_FIXTURE(
3314 mqtt_connection_reconnection_backoff_unstable,
3315 s_setup_mqtt_server_fn,
3316 s_test_mqtt_connection_reconnection_backoff_unstable,
3317 s_clean_up_mqtt_server_fn,
3318 &test_data)
3319
3320 static int s_test_mqtt_connection_reconnection_backoff_reset(struct aws_allocator *allocator, void *ctx) {
3321
3322 (void)allocator;
3323 struct mqtt_connection_state_test *state_test_data = ctx;
3324
3325 struct aws_mqtt_connection_options connection_options = {
3326 .user_data = state_test_data,
3327 .clean_session = false,
3328
3329 .client_id = aws_byte_cursor_from_c_str("client1234"),
3330 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
3331 .socket_options = &state_test_data->socket_options,
3332 .on_connection_complete = aws_test311_on_connection_complete_fn,
3333 };
3334
3335 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
3336 aws_test311_wait_for_connection_to_complete(state_test_data);
3337
3338 uint64_t time_before = 0;
3339 uint64_t time_after = 0;
3340 uint64_t expected_reconnect_backoff = 1;
3341 uint64_t reconnection_backoff = 0;
3342 for (int i = 0; i < 3; i++) {
3343
3344 aws_high_res_clock_get_ticks(&time_before);
3345
3346 /* shut it down and make sure the client automatically reconnects.*/
3347 aws_channel_shutdown(state_test_data->server_channel, AWS_OP_SUCCESS);
3348 aws_test311_wait_for_reconnect_to_complete(state_test_data);
3349
3350 aws_high_res_clock_get_ticks(&time_after);
3351 reconnection_backoff = time_after - time_before;
3352 ASSERT_TRUE(
3353 aws_timestamp_convert(reconnection_backoff, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, NULL) >=
3354 expected_reconnect_backoff);
3355
3356 expected_reconnect_backoff = aws_min_u64(expected_reconnect_backoff * 2, 10);
3357 }
3358
3359 /* sleep for AWS_RESET_RECONNECT_BACKOFF_DELAY_SECONDS to make sure our connection is successful */
3360 aws_thread_current_sleep(
3361 (uint64_t)ONE_SEC * AWS_RESET_RECONNECT_BACKOFF_DELAY_SECONDS +
3362 RECONNECT_BACKOFF_DELAY_ERROR_MARGIN_NANO_SECONDS);
3363
3364 aws_high_res_clock_get_ticks(&time_before);
3365
3366 /* shut it down and make sure the client automatically reconnects.*/
3367 aws_channel_shutdown(state_test_data->server_channel, AWS_OP_SUCCESS);
3368 aws_test311_wait_for_reconnect_to_complete(state_test_data);
3369
3370 aws_high_res_clock_get_ticks(&time_after);
3371 reconnection_backoff = time_after - time_before;
3372 uint64_t remainder = 0;
3373 ASSERT_TRUE(
3374 aws_timestamp_convert(reconnection_backoff, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, &remainder) ==
3375 DEFAULT_MIN_RECONNECT_DELAY_SECONDS);
3376 ASSERT_TRUE(remainder <= RECONNECT_BACKOFF_DELAY_ERROR_MARGIN_NANO_SECONDS);
3377
3378 /* Disconnect */
3379 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
3380 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
3381 aws_test311_wait_for_disconnect_to_complete(state_test_data);
3382
3383 return AWS_OP_SUCCESS;
3384 }
3385
3386 AWS_TEST_CASE_FIXTURE(
3387 mqtt_connection_reconnection_backoff_reset,
3388 s_setup_mqtt_server_fn,
3389 s_test_mqtt_connection_reconnection_backoff_reset,
3390 s_clean_up_mqtt_server_fn,
3391 &test_data)
3392
3393 static int s_test_mqtt_connection_reconnection_backoff_reset_after_disconnection(
3394 struct aws_allocator *allocator,
3395 void *ctx) {
3396
3397 (void)allocator;
3398 struct mqtt_connection_state_test *state_test_data = ctx;
3399
3400 struct aws_mqtt_connection_options connection_options = {
3401 .user_data = state_test_data,
3402 .clean_session = false,
3403
3404 .client_id = aws_byte_cursor_from_c_str("client1234"),
3405 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
3406 .socket_options = &state_test_data->socket_options,
3407 .on_connection_complete = aws_test311_on_connection_complete_fn,
3408 };
3409
3410 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
3411 aws_test311_wait_for_connection_to_complete(state_test_data);
3412
3413 uint64_t time_before = 0;
3414 uint64_t time_after = 0;
3415 uint64_t expected_reconnect_backoff = 1;
3416 uint64_t reconnection_backoff = 0;
3417 for (int i = 0; i < 3; i++) {
3418 aws_high_res_clock_get_ticks(&time_before);
3419
3420 /* shut it down and make sure the client automatically reconnects.*/
3421 aws_channel_shutdown(state_test_data->server_channel, AWS_OP_SUCCESS);
3422 aws_test311_wait_for_reconnect_to_complete(state_test_data);
3423
3424 aws_high_res_clock_get_ticks(&time_after);
3425 reconnection_backoff = time_after - time_before;
3426 ASSERT_TRUE(
3427 aws_timestamp_convert(reconnection_backoff, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, NULL) >=
3428 expected_reconnect_backoff);
3429
3430 expected_reconnect_backoff = aws_min_u64(expected_reconnect_backoff * 2, 10);
3431 }
3432 /* Disconnect */
3433 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
3434 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
3435 aws_test311_wait_for_disconnect_to_complete(state_test_data);
3436
3437 /* connect again */
3438 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
3439 aws_test311_wait_for_connection_to_complete(state_test_data);
3440
3441 aws_high_res_clock_get_ticks(&time_before);
3442
3443 aws_channel_shutdown(state_test_data->server_channel, AWS_OP_SUCCESS);
3444 aws_test311_wait_for_reconnect_to_complete(state_test_data);
3445
3446 aws_high_res_clock_get_ticks(&time_after);
3447 reconnection_backoff = time_after - time_before;
3448 uint64_t remainder = 0;
3449 ASSERT_TRUE(
3450 aws_timestamp_convert(reconnection_backoff, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, &remainder) ==
3451 DEFAULT_MIN_RECONNECT_DELAY_SECONDS);
3452 ASSERT_TRUE(remainder <= RECONNECT_BACKOFF_DELAY_ERROR_MARGIN_NANO_SECONDS);
3453
3454 /* Disconnect */
3455 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
3456 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
3457 aws_test311_wait_for_disconnect_to_complete(state_test_data);
3458
3459 return AWS_OP_SUCCESS;
3460 }
3461
3462 AWS_TEST_CASE_FIXTURE(
3463 mqtt_connection_reconnection_backoff_reset_after_disconnection,
3464 s_setup_mqtt_server_fn,
3465 s_test_mqtt_connection_reconnection_backoff_reset_after_disconnection,
3466 s_clean_up_mqtt_server_fn,
3467 &test_data)
3468
3469 /**
3470 * Makes a CONNECT, with 1 second keep alive ping interval, does nothing for roughly 4 seconds, ensures 4 pings are sent
3471 */
3472 static int s_test_mqtt_connection_ping_norm_fn(struct aws_allocator *allocator, void *ctx) {
3473 (void)allocator;
3474 struct mqtt_connection_state_test *state_test_data = ctx;
3475
3476 struct aws_mqtt_connection_options connection_options = {
3477 .user_data = state_test_data,
3478 .clean_session = true,
3479 .client_id = aws_byte_cursor_from_c_str("client1234"),
3480 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
3481 .socket_options = &state_test_data->socket_options,
3482 .on_connection_complete = aws_test311_on_connection_complete_fn,
3483 .keep_alive_time_secs = 1,
3484 .ping_timeout_ms = 100,
3485 };
3486
3487 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
3488
3489 /* Wait for 4.5 seconds (to account for slight drift/jitter) */
3490 aws_thread_current_sleep(4500000000);
3491
3492 /* Ensure the server got 4 PING packets */
3493 ASSERT_INT_EQUALS(4, mqtt_mock_server_get_ping_count(state_test_data->mock_server));
3494
3495 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
3496 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
3497 aws_test311_wait_for_disconnect_to_complete(state_test_data);
3498
3499 return AWS_OP_SUCCESS;
3500 }
3501
3502 AWS_TEST_CASE_FIXTURE(
3503 mqtt_connection_ping_norm,
3504 s_setup_mqtt_server_fn,
3505 s_test_mqtt_connection_ping_norm_fn,
3506 s_clean_up_mqtt_server_fn,
3507 &test_data)
3508
3509 /**
3510 * Makes a CONNECT, with 1 second keep alive ping interval. Publish QOS1 message for 4.5 seconds and then ensure NO
3511 * pings were sent. (The ping time will be push off on ack )
3512 */
3513 static int s_test_mqtt_connection_ping_no_fn(struct aws_allocator *allocator, void *ctx) {
3514 (void)allocator;
3515 struct mqtt_connection_state_test *state_test_data = ctx;
3516
3517 struct aws_mqtt_connection_options connection_options = {
3518 .user_data = state_test_data,
3519 .clean_session = true,
3520 .client_id = aws_byte_cursor_from_c_str("client1234"),
3521 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
3522 .socket_options = &state_test_data->socket_options,
3523 .on_connection_complete = aws_test311_on_connection_complete_fn,
3524 .keep_alive_time_secs = 1,
3525 .ping_timeout_ms = 100,
3526 };
3527
3528 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
3529 aws_test311_wait_for_connection_to_complete(state_test_data);
3530
3531 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
3532 struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
3533
3534 uint64_t begin_timestamp = 0;
3535 uint64_t elapsed_time = 0;
3536 uint64_t now = 0;
3537 aws_high_res_clock_get_ticks(&begin_timestamp);
3538 uint64_t test_duration = (uint64_t)4 * AWS_TIMESTAMP_NANOS;
3539
3540 // Make sure we publish for 4 seconds;
3541 while (elapsed_time < test_duration) {
3542 /* Publish qos1*/
3543 uint16_t packet_id = aws_mqtt_client_connection_publish(
3544 state_test_data->mqtt_connection,
3545 &pub_topic,
3546 AWS_MQTT_QOS_AT_LEAST_ONCE,
3547 false,
3548 &payload_1,
3549 aws_test311_on_op_complete,
3550 state_test_data);
3551 ASSERT_TRUE(packet_id > 0);
3552
3553 aws_thread_current_sleep(500000000); /* Sleep 0.5 seconds to avoid spamming*/
3554
3555 aws_high_res_clock_get_ticks(&now);
3556 elapsed_time = now - begin_timestamp;
3557 }
3558
3559 aws_thread_current_sleep(250000000); /* Sleep 0.25 seconds to consider jitter*/
3560
3561 /* Ensure the server got 0 PING packets */
3562 ASSERT_INT_EQUALS(0, mqtt_mock_server_get_ping_count(state_test_data->mock_server));
3563
3564 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
3565 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
3566 aws_test311_wait_for_disconnect_to_complete(state_test_data);
3567
3568 return AWS_OP_SUCCESS;
3569 }
3570
3571 AWS_TEST_CASE_FIXTURE(
3572 mqtt_connection_ping_no,
3573 s_setup_mqtt_server_fn,
3574 s_test_mqtt_connection_ping_no_fn,
3575 s_clean_up_mqtt_server_fn,
3576 &test_data)
3577
3578 /**
3579 * Makes a CONNECT, with 1 second keep alive ping interval, publish a qos0 messages for 4.5 seconds.
3580 * We should send a total of 4 pings
3581 */
3582 static int s_test_mqtt_connection_ping_noack_fn(struct aws_allocator *allocator, void *ctx) {
3583 (void)allocator;
3584 struct mqtt_connection_state_test *state_test_data = ctx;
3585
3586 struct aws_mqtt_connection_options connection_options = {
3587 .user_data = state_test_data,
3588 .clean_session = true,
3589 .client_id = aws_byte_cursor_from_c_str("client1234"),
3590 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
3591 .socket_options = &state_test_data->socket_options,
3592 .on_connection_complete = aws_test311_on_connection_complete_fn,
3593 .keep_alive_time_secs = 1,
3594 .ping_timeout_ms = 100,
3595 };
3596
3597 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
3598 aws_test311_wait_for_connection_to_complete(state_test_data);
3599
3600 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
3601 struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
3602
3603 uint64_t begin_timestamp = 0;
3604 uint64_t elapsed_time = 0;
3605 uint64_t now = 0;
3606 aws_high_res_clock_get_ticks(&begin_timestamp);
3607 uint64_t test_duration = (uint64_t)4 * AWS_TIMESTAMP_NANOS;
3608
3609 // Make sure we publish for 4 seconds;
3610 while (elapsed_time < test_duration) {
3611 /* Publish qos0*/
3612 uint16_t packet_id = aws_mqtt_client_connection_publish(
3613 state_test_data->mqtt_connection,
3614 &pub_topic,
3615 AWS_MQTT_QOS_AT_MOST_ONCE,
3616 false,
3617 &payload_1,
3618 aws_test311_on_op_complete,
3619 state_test_data);
3620 ASSERT_TRUE(packet_id > 0);
3621
3622 aws_thread_current_sleep(500000000); /* Sleep 0.5 seconds to avoid spamming*/
3623 aws_high_res_clock_get_ticks(&now);
3624 elapsed_time = now - begin_timestamp;
3625 }
3626
3627 aws_thread_current_sleep(250000000); /* Sleep 0.25 seconds to consider jitter*/
3628
3629 /* Ensure the server got 4 PING packets */
3630 ASSERT_INT_EQUALS(4, mqtt_mock_server_get_ping_count(state_test_data->mock_server));
3631
3632 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
3633 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
3634 aws_test311_wait_for_disconnect_to_complete(state_test_data);
3635
3636 return AWS_OP_SUCCESS;
3637 }
3638
3639 AWS_TEST_CASE_FIXTURE(
3640 mqtt_connection_ping_noack,
3641 s_setup_mqtt_server_fn,
3642 s_test_mqtt_connection_ping_noack_fn,
3643 s_clean_up_mqtt_server_fn,
3644 &test_data)
3645
3646 /**
3647 * Test to make sure the PING timing is correct if a publish/packet is sent near the end of the keep alive time.
3648 * Note: Because of socket write jitter and scheduling jitter, the times have a 0.25 (quarter of a second) delta range.
3649 *
3650 * To test this, this test has a keep alive at 4 seconds and makes a publish after 3 seconds. This resets the ping
3651 * task and will reschedule it for 4 seconds from the publish (the PING will be scheduled for 3 seconds after the 4
3652 * second task is invoked). This test then waits a second, makes sure a PING has NOT been sent (with no ping reschedule,
3653 * it would have) and then waits 3 seconds to ensure and checks that a PING has been sent. Finally, it waits 4 seconds
3654 * to ensure a second PING was sent at the correct time.
3655 */
3656 static int s_test_mqtt_connection_ping_basic_scenario_fn(struct aws_allocator *allocator, void *ctx) {
3657 (void)allocator;
3658 struct mqtt_connection_state_test *state_test_data = ctx;
3659
3660 struct aws_mqtt_connection_options connection_options = {
3661 .user_data = state_test_data,
3662 .clean_session = true,
3663 .client_id = aws_byte_cursor_from_c_str("client1234"),
3664 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
3665 .socket_options = &state_test_data->socket_options,
3666 .on_connection_complete = aws_test311_on_connection_complete_fn,
3667 .keep_alive_time_secs = 4,
3668 .ping_timeout_ms = 100,
3669 };
3670
3671 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
3672 aws_test311_wait_for_connection_to_complete(state_test_data);
3673 /* PING will be in 4 seconds */
3674
3675 aws_thread_current_sleep(3000000000); /* Wait 3 seconds */
3676
3677 aws_mutex_lock(&state_test_data->lock);
3678 state_test_data->expected_ops_completed = 1;
3679 aws_mutex_unlock(&state_test_data->lock);
3680
3681 /* Make a publish */
3682 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
3683 struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
3684 uint16_t packet_id_1 = aws_mqtt_client_connection_publish(
3685 state_test_data->mqtt_connection,
3686 &pub_topic,
3687 AWS_MQTT_QOS_AT_LEAST_ONCE,
3688 false,
3689 &payload_1,
3690 aws_test311_on_op_complete,
3691 state_test_data);
3692 ASSERT_TRUE(packet_id_1 > 0);
3693 aws_test311_wait_for_ops_completed(state_test_data);
3694 /* Publish packet written at 3 seconds */
3695
3696 aws_thread_current_sleep(1250000000); /* Wait 1.25 second (the extra 0.25 is to account for jitter) */
3697 /* PING task has executed and been rescheduled at 3 seconds (1 second passed) */
3698
3699 /* Ensure the server has gotten 0 PING packets so far */
3700 ASSERT_INT_EQUALS(0, mqtt_mock_server_get_ping_count(state_test_data->mock_server));
3701
3702 aws_thread_current_sleep(
3703 3000000000); /* Wait 3 seconds more (no jitter needed because we already added 0.25 in the prior sleep) */
3704 /* PING task (from publish) has been executed */
3705
3706 /* Ensure the server has gotten only 1 PING packet */
3707 ASSERT_INT_EQUALS(1, mqtt_mock_server_get_ping_count(state_test_data->mock_server));
3708
3709 aws_thread_current_sleep(4000000000); /* Wait 4 seconds (since we didn't publish or anything, it should go back to
3710 normal keep alive time) */
3711
3712 /* Ensure the server has gotten 2 PING packets */
3713 ASSERT_INT_EQUALS(2, mqtt_mock_server_get_ping_count(state_test_data->mock_server));
3714
3715 /* Disconnect and finish! */
3716 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
3717 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
3718 aws_test311_wait_for_disconnect_to_complete(state_test_data);
3719
3720 return AWS_OP_SUCCESS;
3721 }
3722
3723 AWS_TEST_CASE_FIXTURE(
3724 mqtt_connection_ping_basic_scenario,
3725 s_setup_mqtt_server_fn,
3726 s_test_mqtt_connection_ping_basic_scenario_fn,
3727 s_clean_up_mqtt_server_fn,
3728 &test_data)
3729
3730 /**
3731 * The test is the same as above (s_test_mqtt_connection_ping_basic_scenario_fn) but after the first publish, it waits
3732 * 1 second and makes another publish, before waiting 4 seconds from that point and ensures only a single PING was sent.
3733 */
3734 static int s_test_mqtt_connection_ping_double_scenario_fn(struct aws_allocator *allocator, void *ctx) {
3735 (void)allocator;
3736 struct mqtt_connection_state_test *state_test_data = ctx;
3737
3738 struct aws_mqtt_connection_options connection_options = {
3739 .user_data = state_test_data,
3740 .clean_session = true,
3741 .client_id = aws_byte_cursor_from_c_str("client1234"),
3742 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
3743 .socket_options = &state_test_data->socket_options,
3744 .on_connection_complete = aws_test311_on_connection_complete_fn,
3745 .keep_alive_time_secs = 4,
3746 .ping_timeout_ms = 100,
3747 };
3748
3749 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
3750 aws_test311_wait_for_connection_to_complete(state_test_data);
3751 /* PING will be in 4 seconds */
3752
3753 aws_thread_current_sleep(3000000000); /* Wait 3 seconds */
3754
3755 aws_mutex_lock(&state_test_data->lock);
3756 state_test_data->expected_ops_completed = 1;
3757 aws_mutex_unlock(&state_test_data->lock);
3758
3759 /* Make a publish */
3760 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
3761 struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
3762 uint16_t packet_id_1 = aws_mqtt_client_connection_publish(
3763 state_test_data->mqtt_connection,
3764 &pub_topic,
3765 AWS_MQTT_QOS_AT_LEAST_ONCE,
3766 false,
3767 &payload_1,
3768 aws_test311_on_op_complete,
3769 state_test_data);
3770 ASSERT_TRUE(packet_id_1 > 0);
3771 aws_test311_wait_for_ops_completed(state_test_data);
3772 /* Publish packet written at 3 seconds */
3773
3774 aws_thread_current_sleep(1250000000); /* Wait 1.25 second (the extra 0.25 is to account for jitter) */
3775 /* PING task has executed and been rescheduled at 3 seconds (1 second passed) */
3776
3777 /* Ensure the server has gotten 0 PING packets so far */
3778 ASSERT_INT_EQUALS(0, mqtt_mock_server_get_ping_count(state_test_data->mock_server));
3779
3780 aws_thread_current_sleep(750000000); /* wait 0.75 seconds */
3781
3782 aws_mutex_lock(&state_test_data->lock);
3783 state_test_data->expected_ops_completed = 2;
3784 aws_mutex_unlock(&state_test_data->lock);
3785
3786 /* Make as second publish */
3787 uint16_t packet_id_2 = aws_mqtt_client_connection_publish(
3788 state_test_data->mqtt_connection,
3789 &pub_topic,
3790 AWS_MQTT_QOS_AT_LEAST_ONCE,
3791 false,
3792 &payload_1,
3793 aws_test311_on_op_complete,
3794 state_test_data);
3795 ASSERT_TRUE(packet_id_2 > 0);
3796 aws_test311_wait_for_ops_completed(state_test_data);
3797 /* Publish packet written at 2 seconds (relative to PING that was scheduled above) */
3798
3799 aws_thread_current_sleep(4250000000); /* Wait 4.25 (the extra 0.25 is to account for jitter) seconds */
3800 /**
3801 * Note: The extra 2 seconds are to account for the time it takes to publish on the socket. Despite best efforts,
3802 * I cannot get it to trigger right away in the test suite...
3803 * If you read the logs though, the scheduled PINGs should be 4 seconds, 3 seconds, 2 seconds, 4 seconds
3804 */
3805
3806 /* Ensure the server has gotten only 1 PING packet */
3807 ASSERT_INT_EQUALS(1, mqtt_mock_server_get_ping_count(state_test_data->mock_server));
3808 /**
3809 * At this point a new PING task is scheduled for 4 seconds, but we do not care anymore for the
3810 * purposes of this test.
3811 */
3812
3813 /* Disconnect and finish! */
3814 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
3815 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
3816 aws_test311_wait_for_disconnect_to_complete(state_test_data);
3817
3818 return AWS_OP_SUCCESS;
3819 }
3820
3821 AWS_TEST_CASE_FIXTURE(
3822 mqtt_connection_ping_double_scenario,
3823 s_setup_mqtt_server_fn,
3824 s_test_mqtt_connection_ping_double_scenario_fn,
3825 s_clean_up_mqtt_server_fn,
3826 &test_data)
3827
3828 /**
3829 * Test that the connection termination callback is fired for the connection that was not actually connected ever.
3830 * \note Other tests use on_connection_termination callback as well, so one simple dedicated case is enough.
3831 */
3832 static int s_test_mqtt_connection_termination_callback_simple_fn(struct aws_allocator *allocator, void *ctx) {
3833 (void)allocator;
3834 struct mqtt_connection_state_test *state_test_data = ctx;
3835
3836 ASSERT_SUCCESS(aws_mqtt_client_connection_set_connection_termination_handler(
3837 state_test_data->mqtt_connection, aws_test311_on_connection_termination_fn, state_test_data));
3838
3839 return AWS_OP_SUCCESS;
3840 }
3841
3842 AWS_TEST_CASE_FIXTURE(
3843 mqtt_connection_termination_callback_simple,
3844 s_setup_mqtt_server_fn,
3845 s_test_mqtt_connection_termination_callback_simple_fn,
3846 s_clean_up_mqtt_server_fn,
3847 &test_data)
3848
3849 /*
3850 * Verifies that calling publish with a bad qos results in a validation failure
3851 */
3852 static int s_test_mqtt_validation_failure_publish_qos_fn(struct aws_allocator *allocator, void *ctx) {
3853 (void)allocator;
3854 struct mqtt_connection_state_test *state_test_data = ctx;
3855
3856 struct aws_mqtt_connection_options connection_options = {
3857 .user_data = state_test_data,
3858 .clean_session = false,
3859 .client_id = aws_byte_cursor_from_c_str("client1234"),
3860 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
3861 .socket_options = &state_test_data->socket_options,
3862 .on_connection_complete = aws_test311_on_connection_complete_fn,
3863 };
3864
3865 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
3866 aws_test311_wait_for_connection_to_complete(state_test_data);
3867
3868 struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("a/b");
3869 ASSERT_INT_EQUALS(
3870 0,
3871 aws_mqtt_client_connection_publish(
3872 state_test_data->mqtt_connection,
3873 &topic,
3874 (enum aws_mqtt_qos)3,
3875 true,
3876 NULL,
3877 aws_test311_on_op_complete,
3878 state_test_data));
3879 int error_code = aws_last_error();
3880 ASSERT_INT_EQUALS(AWS_ERROR_MQTT_INVALID_QOS, error_code);
3881
3882 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
3883 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
3884 aws_test311_wait_for_disconnect_to_complete(state_test_data);
3885
3886 return AWS_OP_SUCCESS;
3887 }
3888
3889 AWS_TEST_CASE_FIXTURE(
3890 mqtt_validation_failure_publish_qos,
3891 s_setup_mqtt_server_fn,
3892 s_test_mqtt_validation_failure_publish_qos_fn,
3893 s_clean_up_mqtt_server_fn,
3894 &test_data)
3895
3896 /*
3897 * Verifies that calling subscribe_multiple with no topics causes a validation failure
3898 */
3899 static int s_test_mqtt_validation_failure_subscribe_empty_fn(struct aws_allocator *allocator, void *ctx) {
3900 (void)allocator;
3901 struct mqtt_connection_state_test *state_test_data = ctx;
3902
3903 struct aws_mqtt_connection_options connection_options = {
3904 .user_data = state_test_data,
3905 .clean_session = false,
3906 .client_id = aws_byte_cursor_from_c_str("client1234"),
3907 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
3908 .socket_options = &state_test_data->socket_options,
3909 .on_connection_complete = aws_test311_on_connection_complete_fn,
3910 };
3911
3912 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
3913 aws_test311_wait_for_connection_to_complete(state_test_data);
3914
3915 struct aws_array_list topic_filters;
3916 size_t list_len = 2;
3917 AWS_VARIABLE_LENGTH_ARRAY(uint8_t, static_buf, list_len * sizeof(struct aws_mqtt_topic_subscription));
3918 aws_array_list_init_static(&topic_filters, static_buf, list_len, sizeof(struct aws_mqtt_topic_subscription));
3919
3920 ASSERT_INT_EQUALS(
3921 0,
3922 aws_mqtt_client_connection_subscribe_multiple(
3923 state_test_data->mqtt_connection, &topic_filters, aws_test311_on_multi_suback, state_test_data));
3924 int error_code = aws_last_error();
3925 ASSERT_INT_EQUALS(AWS_ERROR_INVALID_ARGUMENT, error_code);
3926
3927 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
3928 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
3929 aws_test311_wait_for_disconnect_to_complete(state_test_data);
3930
3931 return AWS_OP_SUCCESS;
3932 }
3933
3934 AWS_TEST_CASE_FIXTURE(
3935 mqtt_validation_failure_subscribe_empty,
3936 s_setup_mqtt_server_fn,
3937 s_test_mqtt_validation_failure_subscribe_empty_fn,
3938 s_clean_up_mqtt_server_fn,
3939 &test_data)
3940
3941 /*
3942 * Verifies that calling unsubscribe with a null topic causes a validation failure (not a crash)
3943 */
3944 static int s_test_mqtt_validation_failure_unsubscribe_null_fn(struct aws_allocator *allocator, void *ctx) {
3945 (void)allocator;
3946 struct mqtt_connection_state_test *state_test_data = ctx;
3947
3948 struct aws_mqtt_connection_options connection_options = {
3949 .user_data = state_test_data,
3950 .clean_session = false,
3951 .client_id = aws_byte_cursor_from_c_str("client1234"),
3952 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
3953 .socket_options = &state_test_data->socket_options,
3954 .on_connection_complete = aws_test311_on_connection_complete_fn,
3955 };
3956
3957 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
3958 aws_test311_wait_for_connection_to_complete(state_test_data);
3959
3960 ASSERT_INT_EQUALS(
3961 0,
3962 aws_mqtt_client_connection_unsubscribe(
3963 state_test_data->mqtt_connection, NULL, aws_test311_on_op_complete, state_test_data));
3964 int error_code = aws_last_error();
3965 ASSERT_INT_EQUALS(AWS_ERROR_MQTT_INVALID_TOPIC, error_code);
3966
3967 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
3968 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
3969 aws_test311_wait_for_disconnect_to_complete(state_test_data);
3970
3971 return AWS_OP_SUCCESS;
3972 }
3973
3974 AWS_TEST_CASE_FIXTURE(
3975 mqtt_validation_failure_unsubscribe_null,
3976 s_setup_mqtt_server_fn,
3977 s_test_mqtt_validation_failure_unsubscribe_null_fn,
3978 s_clean_up_mqtt_server_fn,
3979 &test_data)
3980
3981 static struct aws_byte_cursor s_bad_client_id_utf8 = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\x41\xED\xA0\x80\x41");
3982 static struct aws_byte_cursor s_bad_username_utf8 = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\x41\x00\x41");
3983 static struct aws_byte_cursor s_bad_will_topic_utf8 = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\x41\xED\xBF\xBF");
3984
3985 static int s_test_mqtt_validation_failure_connect_invalid_client_id_utf8_fn(
3986 struct aws_allocator *allocator,
3987 void *ctx) {
3988 (void)allocator;
3989 struct mqtt_connection_state_test *state_test_data = ctx;
3990
3991 struct aws_mqtt_connection_options connection_options = {
3992 .user_data = state_test_data,
3993 .clean_session = false,
3994 .client_id = s_bad_client_id_utf8,
3995 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
3996 .socket_options = &state_test_data->socket_options,
3997 .on_connection_complete = aws_test311_on_connection_complete_fn,
3998 };
3999
4000 ASSERT_FAILS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
4001 int error_code = aws_last_error();
4002 ASSERT_INT_EQUALS(AWS_ERROR_INVALID_ARGUMENT, error_code);
4003
4004 return AWS_OP_SUCCESS;
4005 }
4006
4007 AWS_TEST_CASE_FIXTURE(
4008 mqtt_validation_failure_connect_invalid_client_id_utf8,
4009 s_setup_mqtt_server_fn,
4010 s_test_mqtt_validation_failure_connect_invalid_client_id_utf8_fn,
4011 s_clean_up_mqtt_server_fn,
4012 &test_data)
4013
4014 static int s_test_mqtt_validation_failure_invalid_will_topic_utf8_fn(struct aws_allocator *allocator, void *ctx) {
4015 (void)allocator;
4016 struct mqtt_connection_state_test *state_test_data = ctx;
4017
4018 struct aws_byte_cursor will_topic_cursor = s_bad_will_topic_utf8;
4019 ASSERT_FAILS(aws_mqtt_client_connection_set_will(
4020 state_test_data->mqtt_connection, &will_topic_cursor, AWS_MQTT_QOS_AT_MOST_ONCE, false, &will_topic_cursor));
4021 int error_code = aws_last_error();
4022 ASSERT_INT_EQUALS(AWS_ERROR_MQTT_INVALID_TOPIC, error_code);
4023
4024 return AWS_OP_SUCCESS;
4025 }
4026
4027 AWS_TEST_CASE_FIXTURE(
4028 mqtt_validation_failure_invalid_will_topic_utf8,
4029 s_setup_mqtt_server_fn,
4030 s_test_mqtt_validation_failure_invalid_will_topic_utf8_fn,
4031 s_clean_up_mqtt_server_fn,
4032 &test_data)
4033
4034 static int s_mqtt_validation_failure_invalid_will_qos_fn(struct aws_allocator *allocator, void *ctx) {
4035 (void)allocator;
4036 struct mqtt_connection_state_test *state_test_data = ctx;
4037
4038 struct aws_byte_cursor will_topic_cursor = aws_byte_cursor_from_c_str("a/b");
4039
4040 ASSERT_FAILS(aws_mqtt_client_connection_set_will(
4041 state_test_data->mqtt_connection, &will_topic_cursor, 12, false, &will_topic_cursor));
4042 int error_code = aws_last_error();
4043 ASSERT_INT_EQUALS(AWS_ERROR_MQTT_INVALID_QOS, error_code);
4044
4045 return AWS_OP_SUCCESS;
4046 }
4047
4048 AWS_TEST_CASE_FIXTURE(
4049 mqtt_validation_failure_invalid_will_qos,
4050 s_setup_mqtt_server_fn,
4051 s_mqtt_validation_failure_invalid_will_qos_fn,
4052 s_clean_up_mqtt_server_fn,
4053 &test_data)
4054
4055 static int s_test_mqtt_validation_failure_invalid_username_utf8_fn(struct aws_allocator *allocator, void *ctx) {
4056 (void)allocator;
4057 struct mqtt_connection_state_test *state_test_data = ctx;
4058
4059 struct aws_byte_cursor login_cursor = s_bad_username_utf8;
4060 ASSERT_FAILS(aws_mqtt_client_connection_set_login(state_test_data->mqtt_connection, &login_cursor, NULL));
4061 int error_code = aws_last_error();
4062 ASSERT_INT_EQUALS(AWS_ERROR_INVALID_ARGUMENT, error_code);
4063
4064 return AWS_OP_SUCCESS;
4065 }
4066
4067 AWS_TEST_CASE_FIXTURE(
4068 mqtt_validation_failure_invalid_username_utf8,
4069 s_setup_mqtt_server_fn,
4070 s_test_mqtt_validation_failure_invalid_username_utf8_fn,
4071 s_clean_up_mqtt_server_fn,
4072 &test_data)
4073
4074 static void s_mqtt_client_test_websocket_failed_transform(
4075 struct aws_http_message *request,
4076 void *user_data,
4077 aws_mqtt_transform_websocket_handshake_complete_fn *complete_fn,
4078 void *complete_ctx) {
4079
4080 struct mqtt_connection_state_test *state_test_data = user_data;
4081 struct aws_mqtt_client_connection_311_impl *mqtt311_connection = state_test_data->mqtt_connection->impl;
4082 /* This test should also perform a regression check: websocket transform failed on a non-eventloop thread does not
4083 * cause crash. */
4084 AWS_FATAL_ASSERT(!aws_event_loop_thread_is_callers_thread(mqtt311_connection->loop));
4085
4086 (*complete_fn)(request, AWS_ERROR_INVALID_STATE, complete_ctx);
4087 }
4088
4089 /* Connection failure test where websocket MQTT channel establishment fails due to handshake transform failure */
4090 static int s_test_mqtt_websocket_failed_transform_fn(struct aws_allocator *allocator, void *ctx) {
4091 (void)allocator;
4092 struct mqtt_connection_state_test *state_test_data = ctx;
4093
4094 struct aws_mqtt_connection_options connection_options = {
4095 .socket_options = &state_test_data->socket_options,
4096 };
4097
4098 aws_mqtt_client_connection_use_websockets(
4099 state_test_data->mqtt_connection, s_mqtt_client_test_websocket_failed_transform, state_test_data, NULL, NULL);
4100
4101 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
4102 aws_test311_wait_for_connection_to_fail(state_test_data);
4103
4104 ASSERT_INT_EQUALS(AWS_ERROR_INVALID_STATE, state_test_data->error);
4105
4106 return AWS_OP_SUCCESS;
4107 }
4108
4109 AWS_TEST_CASE_FIXTURE(
4110 mqtt_websocket_failed_transform,
4111 s_setup_mqtt_server_fn,
4112 s_test_mqtt_websocket_failed_transform_fn,
4113 s_clean_up_mqtt_server_fn,
4114 &test_data)
4115
4116 /**
4117 * helper function to test client with different metrics by checking the received username field
4118 * @param allocator The allocator to use
4119 * @param metrics The metrics to set (can be NULL to disable metrics)
4120 * @param username The username to set (can be NULL to skip setting username)
4121 * @param ctx The test context
4122 */
4123 static int s_create_mqtt_connection_and_set_metrics(
4124 struct aws_allocator *allocator,
4125 struct aws_mqtt_iot_metrics *metrics,
4126 struct aws_byte_cursor *username,
4127 void *ctx) {
4128 struct mqtt_connection_state_test *state_test_data = ctx;
4129
4130 struct aws_mqtt_connection_options connection_options = {
4131 .user_data = state_test_data,
4132 .clean_session = false,
4133 .client_id = aws_byte_cursor_from_c_str("client1234"),
4134 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
4135 .socket_options = &state_test_data->socket_options,
4136 .on_connection_complete = aws_test311_on_connection_complete_fn,
4137 };
4138
4139 if (username != NULL) {
4140 ASSERT_SUCCESS(aws_mqtt_client_connection_set_login(state_test_data->mqtt_connection, username, NULL));
4141 }
4142
4143 ASSERT_SUCCESS(aws_mqtt_client_connection_set_metrics(state_test_data->mqtt_connection, metrics));
4144
4145 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
4146 aws_test311_wait_for_connection_to_complete(state_test_data);
4147
4148 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
4149 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
4150 aws_test311_wait_for_disconnect_to_complete(state_test_data);
4151
4152 /* Decode all received packets by mock server */
4153 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
4154
4155 ASSERT_UINT_EQUALS(2, mqtt_mock_server_decoded_packets_count(state_test_data->mock_server));
4156 struct mqtt_decoded_packet *received_packet =
4157 mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 0);
4158 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
4159
4160 struct aws_byte_buf expected_buf;
4161 AWS_ZERO_STRUCT(expected_buf);
4162 if (metrics) {
4163 aws_test_mqtt_build_expected_metrics(
4164 allocator,
4165 username,
4166 metrics->library_name,
4167 NULL,
4168 metrics->metadata_entries,
4169 metrics->metadata_count,
4170 &expected_buf);
4171 } else if (username != NULL) {
4172 aws_byte_buf_init_copy_from_cursor(&expected_buf, allocator, *username);
4173 }
4174
4175 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&received_packet->username, &expected_buf));
4176
4177 aws_byte_buf_clean_up(&expected_buf);
4178
4179 return AWS_OP_SUCCESS;
4180 }
4181
4182 /**
4183 * Test that aws_mqtt_client_connection_set_metrics works correctly with valid metrics
4184 */
4185 static int s_test_mqtt_connection_set_metrics_valid_fn(struct aws_allocator *allocator, void *ctx) {
4186 (void)allocator;
4187
4188 /* Create metadata entries */
4189 struct aws_mqtt_metadata_entry metadata_entries[] = {
4190 {
4191 .key = aws_byte_cursor_from_c_str("key1"),
4192 .value = aws_byte_cursor_from_c_str("value1"),
4193 },
4194 {
4195 .key = aws_byte_cursor_from_c_str("key2"),
4196 .value = aws_byte_cursor_from_c_str("value2"),
4197 },
4198 };
4199
4200 struct aws_mqtt_iot_metrics metrics = {
4201 .library_name = aws_byte_cursor_from_c_str("TestSDK/1.0"),
4202 .metadata_count = AWS_ARRAY_SIZE(metadata_entries),
4203 .metadata_entries = metadata_entries,
4204 };
4205
4206 struct aws_byte_cursor username = aws_byte_cursor_from_c_str("testuser");
4207 ASSERT_SUCCESS(s_create_mqtt_connection_and_set_metrics(allocator, &metrics, &username, ctx));
4208
4209 return AWS_OP_SUCCESS;
4210 }
4211
4212 AWS_TEST_CASE_FIXTURE(
4213 mqtt_connection_set_metrics_valid,
4214 s_setup_mqtt_server_fn,
4215 s_test_mqtt_connection_set_metrics_valid_fn,
4216 s_clean_up_mqtt_server_fn,
4217 &test_data)
4218
4219 /**
4220 * Test that aws_mqtt_client_connection_set_metrics works correctly with NULL metrics (disables metrics)
4221 */
4222 static int s_test_mqtt_connection_set_metrics_null_fn(struct aws_allocator *allocator, void *ctx) {
4223
4224 struct aws_byte_cursor username = aws_byte_cursor_from_c_str("testuser");
4225 ASSERT_SUCCESS(s_create_mqtt_connection_and_set_metrics(allocator, NULL, &username, ctx));
4226
4227 return AWS_OP_SUCCESS;
4228 }
4229
4230 AWS_TEST_CASE_FIXTURE(
4231 mqtt_connection_set_metrics_null,
4232 s_setup_mqtt_server_fn,
4233 s_test_mqtt_connection_set_metrics_null_fn,
4234 s_clean_up_mqtt_server_fn,
4235 &test_data)
4236
4237 static int s_test_mqtt_connection_set_metrics_with_null_username_fn(struct aws_allocator *allocator, void *ctx) {
4238
4239 /* Create metadata entries */
4240 struct aws_mqtt_metadata_entry metadata_entries[] = {
4241 {
4242 .key = aws_byte_cursor_from_c_str("key1"),
4243 .value = aws_byte_cursor_from_c_str("value1"),
4244 },
4245 {
4246 .key = aws_byte_cursor_from_c_str("key2"),
4247 .value = aws_byte_cursor_from_c_str("value2"),
4248 },
4249 };
4250
4251 struct aws_mqtt_iot_metrics metrics = {
4252 .library_name = aws_byte_cursor_from_c_str("TestSDK/1.0"),
4253 .metadata_count = AWS_ARRAY_SIZE(metadata_entries),
4254 .metadata_entries = metadata_entries,
4255 };
4256
4257 ASSERT_SUCCESS(s_create_mqtt_connection_and_set_metrics(allocator, &metrics, NULL, ctx));
4258
4259 return AWS_OP_SUCCESS;
4260 }
4261
4262 AWS_TEST_CASE_FIXTURE(
4263 mqtt_connection_set_metrics_with_null_username,
4264 s_setup_mqtt_server_fn,
4265 s_test_mqtt_connection_set_metrics_with_null_username_fn,
4266 s_clean_up_mqtt_server_fn,
4267 &test_data)
4268
4269 /**
4270 * Test that aws_mqtt_client_connection_set_metrics rejects invalid UTF-8 in library name
4271 */
4272 static int s_test_mqtt_connection_set_metrics_invalid_utf8_library_fn(struct aws_allocator *allocator, void *ctx) {
4273 (void)allocator;
4274 struct mqtt_connection_state_test *state_test_data = ctx;
4275
4276 /* Invalid UTF-8 sequence */
4277 struct aws_byte_cursor invalid_utf8_library = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("TestSDK\xFF\xFE");
4278
4279 struct aws_mqtt_iot_metrics metrics = {.library_name = invalid_utf8_library};
4280
4281 ASSERT_FAILS(aws_mqtt_client_connection_set_metrics(state_test_data->mqtt_connection, &metrics));
4282 ASSERT_INT_EQUALS(aws_last_error(), AWS_ERROR_INVALID_UTF8);
4283
4284 return AWS_OP_SUCCESS;
4285 }
4286
4287 AWS_TEST_CASE_FIXTURE(
4288 mqtt_connection_set_metrics_invalid_utf8_library,
4289 s_setup_mqtt_server_fn,
4290 s_test_mqtt_connection_set_metrics_invalid_utf8_library_fn,
4291 s_clean_up_mqtt_server_fn,
4292 &test_data)
4293
4294 /**
4295 * Test that metrics and username get updated properly on reconnect
4296 */
4297 static int s_test_mqtt_connection_set_metrics_modify_on_reconnect_fn(struct aws_allocator *allocator, void *ctx) {
4298 struct mqtt_connection_state_test *state_test_data = ctx;
4299
4300 struct aws_mqtt_connection_options connection_options = {
4301 .user_data = state_test_data,
4302 .clean_session = false,
4303 .client_id = aws_byte_cursor_from_c_str("client1234"),
4304 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
4305 .socket_options = &state_test_data->socket_options,
4306 .on_connection_complete = aws_test311_on_connection_complete_fn,
4307 };
4308
4309 aws_mqtt_client_connection_set_reconnect_timeout(
4310 state_test_data->mqtt_connection, MIN_RECONNECT_DELAY_SECONDS, MAX_RECONNECT_DELAY_SECONDS);
4311
4312 struct aws_mqtt_iot_metrics metrics1 = {
4313 .library_name = aws_byte_cursor_from_c_str("TestSDK/1.0"),
4314 };
4315 struct aws_mqtt_iot_metrics metrics2 = {
4316 .library_name = aws_byte_cursor_from_c_str("TestSDK/2.0"),
4317 };
4318
4319 struct aws_byte_cursor username1 = aws_byte_cursor_from_c_str("testuser1");
4320 struct aws_byte_cursor username2 = aws_byte_cursor_from_c_str("testuser2");
4321
4322 ASSERT_SUCCESS(aws_mqtt_client_connection_set_login(state_test_data->mqtt_connection, &username1, NULL));
4323
4324 ASSERT_SUCCESS(aws_mqtt_client_connection_set_metrics(state_test_data->mqtt_connection, &metrics1));
4325
4326 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
4327 aws_test311_wait_for_connection_to_complete(state_test_data);
4328
4329 /* Decode received packets by mock server */
4330 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
4331 struct mqtt_decoded_packet *received_packet1 =
4332 mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 0);
4333 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet1->type);
4334
4335 /* verify the username and metrics is setup properly */
4336 struct aws_byte_buf expected_buf;
4337 AWS_ZERO_STRUCT(expected_buf);
4338 aws_test_mqtt_build_expected_metrics(allocator, &username1, metrics1.library_name, NULL, NULL, 0, &expected_buf);
4339 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&received_packet1->username, &expected_buf));
4340 aws_byte_buf_clean_up(&expected_buf);
4341
4342 // set the second username and metrics
4343 ASSERT_SUCCESS(aws_mqtt_client_connection_set_login(state_test_data->mqtt_connection, &username2, NULL));
4344
4345 ASSERT_SUCCESS(aws_mqtt_client_connection_set_metrics(state_test_data->mqtt_connection, &metrics2));
4346
4347 /* shutdown the channel to reconnect */
4348 aws_channel_shutdown(state_test_data->server_channel, AWS_OP_SUCCESS);
4349 aws_test311_wait_for_reconnect_to_complete(state_test_data);
4350
4351 /* Decode second connect packets by mock server */
4352 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
4353 struct mqtt_decoded_packet *received_packet2 =
4354 mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 1);
4355 ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet2->type);
4356
4357 /* verify the username and metrics is setup properly */
4358 AWS_ZERO_STRUCT(expected_buf);
4359 aws_test_mqtt_build_expected_metrics(allocator, &username2, metrics2.library_name, NULL, NULL, 0, &expected_buf);
4360 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&received_packet2->username, &expected_buf));
4361 aws_byte_buf_clean_up(&expected_buf);
4362
4363 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
4364 state_test_data->mqtt_connection, aws_test311_on_disconnect_fn, state_test_data));
4365 aws_test311_wait_for_disconnect_to_complete(state_test_data);
4366
4367 return AWS_OP_SUCCESS;
4368 }
4369
4370 AWS_TEST_CASE_FIXTURE(
4371 mqtt_connection_set_metrics_modify_on_reconnect,
4372 s_setup_mqtt_server_fn,
4373 s_test_mqtt_connection_set_metrics_modify_on_reconnect_fn,
4374 s_clean_up_mqtt_server_fn,
4375 &test_data)