Debian Codegraph @

crt/aws-c-mqtt/tests/v5/mqtt5_testing_utils.h

raw symbols SCIP
1 #ifndef MQTT_MQTT5_TESTING_UTILS_H
2 #define MQTT_MQTT5_TESTING_UTILS_H
3 /**
4 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: Apache-2.0.
6 */
7
8 #include <aws/mqtt/mqtt.h>
9
10 #include <aws/common/array_list.h>
11 #include <aws/common/condition_variable.h>
12 #include <aws/common/mutex.h>
13 #include <aws/io/channel.h>
14 #include <aws/mqtt/private/v5/mqtt5_client_impl.h>
15 #include <aws/mqtt/private/v5/mqtt5_decoder.h>
16 #include <aws/mqtt/private/v5/mqtt5_encoder.h>
17 #include <aws/mqtt/v5/mqtt5_types.h>
18
19 struct aws_event_loop_group;
20
21 struct aws_mqtt5_mock_server_packet_record {
22 struct aws_allocator *storage_allocator;
23
24 uint64_t timestamp;
25
26 void *packet_storage;
27 enum aws_mqtt5_packet_type packet_type;
28 };
29
30 struct aws_mqtt5_lifecycle_event_record {
31 struct aws_allocator *allocator;
32
33 uint64_t timestamp;
34
35 struct aws_mqtt5_client_lifecycle_event event;
36
37 struct aws_mqtt5_negotiated_settings settings_storage;
38 struct aws_mqtt5_packet_disconnect_storage disconnect_storage;
39 struct aws_mqtt5_packet_connack_storage connack_storage;
40 };
41
42 struct aws_mqtt5_server_mock_connection_context {
43 struct aws_allocator *allocator;
44
45 struct aws_channel *channel;
46 struct aws_channel_handler handler;
47 struct aws_channel_slot *slot;
48
49 struct aws_mqtt5_encoder_function_table encoding_table;
50 struct aws_mqtt5_encoder encoder;
51
52 struct aws_mqtt5_decoder_function_table decoding_table;
53 struct aws_mqtt5_decoder decoder;
54 struct aws_mqtt5_inbound_topic_alias_resolver inbound_alias_resolver;
55
56 struct aws_mqtt5_client_mock_test_fixture *test_fixture;
57
58 struct aws_task service_task;
59 };
60
61 /**
62 * Callback invoked when the mock server receives a packet from the client.
63 *
64 * @param packet_view The decoded packet view (type-specific, e.g., aws_mqtt5_packet_connect_view)
65 * @param connection The mock server connection context
66 * @param packet_received_user_data User data from the vtable (mock_server_user_data)
67 * @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
68 */
69 typedef int(aws_mqtt5_on_mock_server_packet_received_fn)(
70 void *packet_view,
71 struct aws_mqtt5_server_mock_connection_context *connection,
72 void *packet_received_user_data);
73
74 /**
75 * Periodic service callback for the mock MQTT5 server.
76 *
77 * This function is called on a periodic timer (every 1 second) and allows the mock server
78 * to perform proactive operations such as:
79 * - Sending server-initiated packets (PUBLISH, DISCONNECT, etc.) to the client
80 * - Simulating network delays or timeouts
81 * - Checking elapsed time and triggering time-based behaviors
82 * - Managing connection state transitions
83 *
84 * The callback runs on the server's event loop and is automatically scheduled:
85 * 1. Initially when the connection is established (runs immediately)
86 * 2. After each execution (reschedules itself to run 1 second later)
87 *
88 * The task is automatically cancelled when the connection is destroyed.
89 *
90 * @param mock_server The mock server connection context (provides channel, encoder, etc.)
91 * @param user_data User data from the vtable (mock_server_user_data from test fixture)
92 *
93 * @note This callback is optional. Set to NULL if periodic server behavior is not needed.
94 * @note Use aws_mqtt5_mock_server_send_packet() to send packets to the client.
95 */
96 typedef void(
97 aws_mqtt5_mock_server_service_fn)(struct aws_mqtt5_server_mock_connection_context *mock_server, void *user_data);
98
99 /**
100 * Virtual function table for the mock MQTT5 server.
101 *
102 * Defines callbacks for handling client packets and performing periodic server operations.
103 */
104 struct aws_mqtt5_mock_server_vtable {
105 /** Handlers for packets received from the client (indexed by aws_mqtt5_packet_type) */
106 aws_mqtt5_on_mock_server_packet_received_fn *packet_handlers[16];
107
108 /** Optional periodic service callback (runs every 1 second). Can be NULL. */
109 aws_mqtt5_mock_server_service_fn *service_task_fn;
110 };
111
112 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options {
113 struct aws_mqtt5_client_options *client_options;
114 const struct aws_mqtt5_mock_server_vtable *server_function_table;
115
116 void *mock_server_user_data;
117 };
118
119 struct aws_mqtt5_client_mock_test_fixture {
120 struct aws_allocator *allocator;
121
122 struct aws_event_loop_group *client_elg;
123 struct aws_event_loop_group *server_elg;
124 struct aws_host_resolver *host_resolver;
125 struct aws_client_bootstrap *client_bootstrap;
126 struct aws_server_bootstrap *server_bootstrap;
127 struct aws_socket_endpoint endpoint;
128 struct aws_socket_options socket_options;
129 struct aws_socket *listener;
130 struct aws_channel *server_channel;
131
132 const struct aws_mqtt5_mock_server_vtable *server_function_table;
133 void *mock_server_user_data;
134
135 struct aws_mqtt5_client_vtable client_vtable;
136 struct aws_mqtt5_client *client;
137
138 aws_mqtt5_client_connection_event_callback_fn *original_lifecycle_event_handler;
139 void *original_lifecycle_event_handler_user_data;
140
141 uint16_t maximum_inbound_topic_aliases;
142
143 struct aws_mutex lock;
144 struct aws_condition_variable signal;
145 struct aws_array_list server_received_packets;
146 struct aws_array_list lifecycle_events;
147 struct aws_array_list client_states;
148 struct aws_array_list client_statistics;
149 bool listener_destroyed;
150 bool subscribe_complete;
151 bool disconnect_completion_callback_invoked;
152 bool client_terminated;
153 uint32_t total_pubacks_received;
154 uint32_t publishes_received;
155 uint32_t successful_pubacks_received;
156 uint32_t timeouts_received;
157
158 uint32_t server_maximum_inflight_publishes;
159 uint32_t server_current_inflight_publishes;
160 };
161
162 struct mqtt5_client_test_options {
163 struct aws_mqtt5_client_topic_alias_options topic_aliasing_options;
164 struct aws_mqtt5_packet_connect_view connect_options;
165 struct aws_mqtt5_client_options client_options;
166 struct aws_mqtt5_mock_server_vtable server_function_table;
167 };
168
169 struct aws_mqtt5_mock_server_reconnect_state {
170 size_t required_connection_count_threshold;
171
172 size_t connection_attempts;
173 uint64_t connect_timestamp;
174
175 uint64_t successful_connection_disconnect_delay_ms;
176 };
177
178 int aws_mqtt5_test_verify_user_properties_raw(
179 size_t property_count,
180 const struct aws_mqtt5_user_property *properties,
181 size_t expected_count,
182 const struct aws_mqtt5_user_property *expected_properties);
183
184 void aws_mqtt5_encode_init_testing_function_table(struct aws_mqtt5_encoder_function_table *function_table);
185
186 void aws_mqtt5_decode_init_testing_function_table(struct aws_mqtt5_decoder_function_table *function_table);
187
188 int aws_mqtt5_client_mock_test_fixture_init(
189 struct aws_mqtt5_client_mock_test_fixture *test_fixture,
190 struct aws_allocator *allocator,
191 struct aws_mqtt5_client_mqtt5_mock_test_fixture_options *options);
192
193 void aws_mqtt5_client_mock_test_fixture_clean_up(struct aws_mqtt5_client_mock_test_fixture *test_fixture);
194
195 bool aws_mqtt5_client_test_are_packets_equal(
196 enum aws_mqtt5_packet_type packet_type,
197 void *lhs_packet_storage,
198 void *rhs_packet_storage);
199
200 size_t aws_mqtt5_linked_list_length(struct aws_linked_list *list);
201
202 void aws_mqtt5_client_test_init_default_options(struct mqtt5_client_test_options *test_options);
203
204 void aws_wait_for_connected_lifecycle_event(struct aws_mqtt5_client_mock_test_fixture *test_context);
205 void aws_wait_for_stopped_lifecycle_event(struct aws_mqtt5_client_mock_test_fixture *test_context);
206
207 int aws_verify_received_packet_sequence(
208 struct aws_mqtt5_client_mock_test_fixture *test_context,
209 struct aws_mqtt5_mock_server_packet_record *expected_packets,
210 size_t expected_packets_count);
211
212 int aws_mqtt5_mock_server_handle_connect_always_fail(
213 void *packet,
214 struct aws_mqtt5_server_mock_connection_context *connection,
215 void *user_data);
216
217 void aws_mqtt5_wait_for_n_lifecycle_events(
218 struct aws_mqtt5_client_mock_test_fixture *test_context,
219 enum aws_mqtt5_client_lifecycle_event_type type,
220 size_t count);
221
222 int aws_verify_reconnection_exponential_backoff_timestamps(struct aws_mqtt5_client_mock_test_fixture *test_fixture);
223
224 int aws_verify_client_state_sequence(
225 struct aws_mqtt5_client_mock_test_fixture *test_context,
226 enum aws_mqtt5_client_state *expected_states,
227 size_t expected_states_count);
228
229 int aws_mqtt5_mock_server_handle_connect_always_succeed(
230 void *packet,
231 struct aws_mqtt5_server_mock_connection_context *connection,
232 void *user_data);
233
234 int aws_mqtt5_mock_server_send_packet(
235 struct aws_mqtt5_server_mock_connection_context *connection,
236 enum aws_mqtt5_packet_type packet_type,
237 void *packet);
238
239 int aws_mqtt5_mock_server_handle_connect_succeed_on_nth(
240 void *packet,
241 struct aws_mqtt5_server_mock_connection_context *connection,
242 void *user_data);
243
244 int aws_mqtt5_mock_server_handle_publish_puback(
245 void *packet,
246 struct aws_mqtt5_server_mock_connection_context *connection,
247 void *user_data);
248
249 int aws_mqtt5_mock_server_handle_publish_puback_and_forward(
250 void *packet,
251 struct aws_mqtt5_server_mock_connection_context *connection,
252 void *user_data);
253
254 int aws_mqtt5_mock_server_handle_unsubscribe_unsuback_success(
255 void *packet,
256 struct aws_mqtt5_server_mock_connection_context *connection,
257 void *user_data);
258
259 int aws_mqtt5_mock_server_send_packet(
260 struct aws_mqtt5_server_mock_connection_context *connection,
261 enum aws_mqtt5_packet_type packet_type,
262 void *packet);
263
264 int aws_mqtt5_server_send_suback_on_subscribe(
265 void *packet,
266 struct aws_mqtt5_server_mock_connection_context *connection,
267 void *user_data);
268
269 int aws_mqtt5_mock_server_handle_connect_honor_session_unconditional(
270 void *packet,
271 struct aws_mqtt5_server_mock_connection_context *connection,
272 void *user_data);
273
274 void aws_wait_for_n_lifecycle_events(
275 struct aws_mqtt5_client_mock_test_fixture *test_fixture,
276 enum aws_mqtt5_client_lifecycle_event_type event_type,
277 size_t expected_event_count);
278
279 extern const struct aws_string *g_default_client_id;
280
281 #define RECONNECT_TEST_MIN_BACKOFF 500
282 #define RECONNECT_TEST_MAX_BACKOFF 5000
283 #define RECONNECT_TEST_BACKOFF_RESET_DELAY 5000
284
285 #endif /* MQTT_MQTT5_TESTING_UTILS_H */