Debian Codegraph @

crt/aws-c-mqtt/tests/v5/mqtt5_operation_validation_failure_tests.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 <aws/io/channel_bootstrap.h>
9 #include <aws/io/event_loop.h>
10 #include <aws/io/host_resolver.h>
11 #include <aws/mqtt/private/v5/mqtt5_client_impl.h>
12 #include <aws/mqtt/private/v5/mqtt5_options_storage.h>
13 #include <aws/mqtt/private/v5/mqtt5_utils.h>
14 #include <aws/mqtt/v5/mqtt5_client.h>
15 #include <aws/mqtt/v5/mqtt5_types.h>
16
17 #include <aws/testing/aws_test_harness.h>
18
19 static uint8_t s_server_reference[] = "derp.com";
20 static struct aws_byte_cursor s_server_reference_cursor = {
21 .ptr = s_server_reference,
22 .len = AWS_ARRAY_SIZE(s_server_reference) - 1,
23 };
24
25 static uint8_t s_binary_data[] = "binary data";
26 static struct aws_byte_cursor s_binary_data_cursor = {
27 .ptr = s_binary_data,
28 .len = AWS_ARRAY_SIZE(s_binary_data) - 1,
29 };
30
31 static uint8_t s_too_long_for_uint16[UINT16_MAX + 1];
32 static struct aws_byte_cursor s_too_long_for_uint16_cursor = {
33 .ptr = s_too_long_for_uint16,
34 .len = AWS_ARRAY_SIZE(s_too_long_for_uint16),
35 };
36
37 // Mqtt5 Specific invalid codepoint in prohibited range U+007F - U+009F (value = U+008F)",
38 static uint8_t s_invalid_utf8[] = "\xC2\x8F";
39 static struct aws_byte_cursor s_invalid_utf8_string = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(s_invalid_utf8);
40 static uint8_t s_user_prop_name[] = "name";
41 static uint8_t s_user_prop_value[] = "value";
42
43 static const struct aws_mqtt5_user_property s_bad_user_properties_name[] = {
44 {
45 .name =
46 {
47 .ptr = s_too_long_for_uint16,
48 .len = AWS_ARRAY_SIZE(s_too_long_for_uint16),
49 },
50 .value =
51 {
52 .ptr = (uint8_t *)s_user_prop_value,
53 .len = AWS_ARRAY_SIZE(s_user_prop_value) - 1,
54 },
55 },
56 };
57
58 static const struct aws_mqtt5_user_property s_bad_user_properties_value[] = {
59 {
60 .name =
61 {
62 .ptr = s_user_prop_name,
63 .len = AWS_ARRAY_SIZE(s_user_prop_name) - 1,
64 },
65 .value =
66 {
67 .ptr = s_too_long_for_uint16,
68 .len = AWS_ARRAY_SIZE(s_too_long_for_uint16),
69 },
70 },
71 };
72
73 static const struct aws_mqtt5_user_property s_user_properties_with_invalid_name[] = {
74 {.name =
75 {
76 .ptr = (uint8_t *)s_invalid_utf8,
77 .len = AWS_ARRAY_SIZE(s_invalid_utf8) - 1,
78 },
79 .value = {
80 .ptr = (uint8_t *)s_user_prop_value,
81 .len = AWS_ARRAY_SIZE(s_user_prop_value) - 1,
82 }}};
83
84 static const struct aws_mqtt5_user_property s_user_properties_with_invalid_value[] = {
85 {.name =
86 {
87 .ptr = s_user_prop_name,
88 .len = AWS_ARRAY_SIZE(s_user_prop_name) - 1,
89 },
90 .value = {
91 .ptr = (uint8_t *)s_invalid_utf8,
92 .len = AWS_ARRAY_SIZE(s_invalid_utf8) - 1,
93 }}};
94
95 static struct aws_mqtt5_user_property s_bad_user_properties_too_many[AWS_MQTT5_CLIENT_MAXIMUM_USER_PROPERTIES + 1];
96
97 /*
98 * rather than just checking the bad view, we do a side-by-side before-after view validation as well to give more
99 * confidence that the failure is coming from what we're actually trying to check
100 */
101 #define AWS_VALIDATION_FAILURE_PREFIX(packet_type, failure_reason, view_name, mutate_function) \
102 static int s_mqtt5_operation_##packet_type##_validation_failure_##failure_reason##_fn( \
103 struct aws_allocator *allocator, void *ctx) { \
104 (void)ctx; \
105 \
106 struct aws_mqtt5_packet_##packet_type##_view good_view = view_name; \
107 struct aws_mqtt5_packet_##packet_type##_view bad_view = view_name; \
108 (*mutate_function)(&bad_view); \
109 ASSERT_SUCCESS(aws_mqtt5_packet_##packet_type##_view_validate(&good_view)); \
110 ASSERT_FAILS(aws_mqtt5_packet_##packet_type##_view_validate(&bad_view));
111
112 #define AWS_VALIDATION_FAILURE_SUFFIX(packet_type, failure_reason) \
113 return AWS_OP_SUCCESS; \
114 } \
115 \
116 AWS_TEST_CASE( \
117 mqtt5_operation_##packet_type##_validation_failure_##failure_reason, \
118 s_mqtt5_operation_##packet_type##_validation_failure_##failure_reason##_fn)
119
120 #define AWS_VALIDATION_FAILURE_TEST4(packet_type, failure_reason, view_name, mutate_function) \
121 AWS_VALIDATION_FAILURE_PREFIX(packet_type, failure_reason, view_name, mutate_function) \
122 struct aws_mqtt5_operation_##packet_type *operation = \
123 aws_mqtt5_operation_##packet_type##_new(allocator, &good_view, NULL, NULL); \
124 ASSERT_NOT_NULL(operation); \
125 aws_mqtt5_operation_release(&operation->base); \
126 ASSERT_NULL(aws_mqtt5_operation_##packet_type##_new(allocator, &bad_view, NULL, NULL)); \
127 AWS_VALIDATION_FAILURE_SUFFIX(packet_type, failure_reason)
128
129 #define AWS_VALIDATION_FAILURE_TEST3(packet_type, failure_reason, view_name, mutate_function) \
130 AWS_VALIDATION_FAILURE_PREFIX(packet_type, failure_reason, view_name, mutate_function) \
131 ASSERT_NULL(aws_mqtt5_operation_##packet_type##_new(allocator, NULL, &bad_view, NULL)); \
132 AWS_VALIDATION_FAILURE_SUFFIX(packet_type, failure_reason)
133
134 #define AWS_VALIDATION_FAILURE_TEST_CONNECT(packet_type, failure_reason, view_name, mutate_function) \
135 AWS_VALIDATION_FAILURE_PREFIX(packet_type, failure_reason, view_name, mutate_function) \
136 ASSERT_NULL(aws_mqtt5_operation_##packet_type##_new(allocator, &bad_view, NULL)); \
137 AWS_VALIDATION_FAILURE_SUFFIX(packet_type, failure_reason)
138
139 #define AWS_IOT_CORE_VALIDATION_FAILURE(packet_type, failure_reason, view_name, mutate_function) \
140 static int s_mqtt5_operation_##packet_type##_validation_failure_##failure_reason##_fn( \
141 struct aws_allocator *allocator, void *ctx) { \
142 (void)ctx; \
143 \
144 struct aws_mqtt5_packet_##packet_type##_view good_view = view_name; \
145 struct aws_mqtt5_packet_##packet_type##_view bad_view = view_name; \
146 (*mutate_function)(&bad_view); \
147 ASSERT_SUCCESS(aws_mqtt5_packet_##packet_type##_view_validate(&good_view)); \
148 ASSERT_SUCCESS(aws_mqtt5_packet_##packet_type##_view_validate(&bad_view)); \
149 \
150 struct aws_mqtt5_client dummy_client; \
151 AWS_ZERO_STRUCT(dummy_client); \
152 struct aws_mqtt5_client_options_storage client_options_storage; \
153 AWS_ZERO_STRUCT(client_options_storage); \
154 client_options_storage.extended_validation_and_flow_control_options = AWS_MQTT5_EVAFCO_AWS_IOT_CORE_DEFAULTS; \
155 dummy_client.config = &client_options_storage; \
156 \
157 ASSERT_NULL(aws_mqtt5_operation_##packet_type##_new(allocator, &dummy_client, &bad_view, NULL)); \
158 return AWS_OP_SUCCESS; \
159 } \
160 \
161 AWS_TEST_CASE( \
162 mqtt5_operation_##packet_type##_validation_failure_##failure_reason, \
163 s_mqtt5_operation_##packet_type##_validation_failure_##failure_reason##_fn)
164
165 static struct aws_mqtt5_packet_disconnect_view s_good_disconnect_view;
166
167 static void s_make_server_reference_disconnect_view(struct aws_mqtt5_packet_disconnect_view *view) {
168 view->server_reference = &s_server_reference_cursor;
169 }
170
171 AWS_VALIDATION_FAILURE_TEST4(
172 disconnect,
173 server_reference,
174 s_good_disconnect_view,
175 s_make_server_reference_disconnect_view)
176
177 static void s_make_bad_reason_code_disconnect_view(struct aws_mqtt5_packet_disconnect_view *view) {
178 view->reason_code = -1;
179 }
180
181 AWS_VALIDATION_FAILURE_TEST4(
182 disconnect,
183 bad_reason_code,
184 s_good_disconnect_view,
185 s_make_bad_reason_code_disconnect_view)
186
187 static void s_make_reason_string_too_long_disconnect_view(struct aws_mqtt5_packet_disconnect_view *view) {
188 view->reason_string = &s_too_long_for_uint16_cursor;
189 }
190
191 AWS_VALIDATION_FAILURE_TEST4(
192 disconnect,
193 reason_string_too_long,
194 s_good_disconnect_view,
195 s_make_reason_string_too_long_disconnect_view)
196
197 static void s_make_reason_string_invalid_utf8_disconnect_view(struct aws_mqtt5_packet_disconnect_view *view) {
198 view->reason_string = &s_invalid_utf8_string;
199 }
200
201 AWS_VALIDATION_FAILURE_TEST4(
202 disconnect,
203 reason_string_invalid_utf8,
204 s_good_disconnect_view,
205 s_make_reason_string_invalid_utf8_disconnect_view)
206
207 static void s_make_user_properties_name_too_long_disconnect_view(struct aws_mqtt5_packet_disconnect_view *view) {
208 view->user_property_count = AWS_ARRAY_SIZE(s_bad_user_properties_name);
209 view->user_properties = s_bad_user_properties_name;
210 }
211
212 AWS_VALIDATION_FAILURE_TEST4(
213 disconnect,
214 user_properties_name_too_long,
215 s_good_disconnect_view,
216 s_make_user_properties_name_too_long_disconnect_view)
217
218 static void s_make_user_properties_name_invalid_utf8_disconnect_view(struct aws_mqtt5_packet_disconnect_view *view) {
219 view->user_property_count = AWS_ARRAY_SIZE(s_user_properties_with_invalid_name);
220 view->user_properties = s_user_properties_with_invalid_name;
221 }
222
223 AWS_VALIDATION_FAILURE_TEST4(
224 disconnect,
225 user_properties_name_invalid_utf8,
226 s_good_disconnect_view,
227 s_make_user_properties_name_invalid_utf8_disconnect_view)
228
229 static void s_make_user_properties_value_too_long_disconnect_view(struct aws_mqtt5_packet_disconnect_view *view) {
230 view->user_property_count = AWS_ARRAY_SIZE(s_bad_user_properties_value);
231 view->user_properties = s_bad_user_properties_value;
232 }
233
234 AWS_VALIDATION_FAILURE_TEST4(
235 disconnect,
236 user_properties_value_too_long,
237 s_good_disconnect_view,
238 s_make_user_properties_value_too_long_disconnect_view)
239
240 static void s_make_user_properties_value_invalid_utf8_disconnect_view(struct aws_mqtt5_packet_disconnect_view *view) {
241 view->user_property_count = AWS_ARRAY_SIZE(s_user_properties_with_invalid_value);
242 view->user_properties = s_user_properties_with_invalid_value;
243 }
244
245 AWS_VALIDATION_FAILURE_TEST4(
246 disconnect,
247 user_properties_value_invalid_utf8,
248 s_good_disconnect_view,
249 s_make_user_properties_value_invalid_utf8_disconnect_view)
250
251 static void s_make_user_properties_too_many_disconnect_view(struct aws_mqtt5_packet_disconnect_view *view) {
252 view->user_property_count = AWS_ARRAY_SIZE(s_bad_user_properties_too_many);
253 view->user_properties = s_bad_user_properties_too_many;
254 }
255
256 AWS_VALIDATION_FAILURE_TEST4(
257 disconnect,
258 user_properties_too_many,
259 s_good_disconnect_view,
260 s_make_user_properties_too_many_disconnect_view)
261
262 static struct aws_mqtt5_packet_connect_view s_good_connect_view;
263
264 static void s_make_client_id_too_long_connect_view(struct aws_mqtt5_packet_connect_view *view) {
265 view->client_id.ptr = s_too_long_for_uint16;
266 view->client_id.len = AWS_ARRAY_SIZE(s_too_long_for_uint16);
267 }
268
269 AWS_VALIDATION_FAILURE_TEST_CONNECT(
270 connect,
271 client_id_too_long,
272 s_good_connect_view,
273 s_make_client_id_too_long_connect_view)
274
275 static void s_make_client_id_invalid_utf8_connect_view(struct aws_mqtt5_packet_connect_view *view) {
276 view->client_id = s_invalid_utf8_string;
277 }
278
279 AWS_VALIDATION_FAILURE_TEST_CONNECT(
280 connect,
281 client_id_invalid_utf8,
282 s_good_connect_view,
283 s_make_client_id_invalid_utf8_connect_view)
284
285 static void s_make_username_too_long_connect_view(struct aws_mqtt5_packet_connect_view *view) {
286 view->username = &s_too_long_for_uint16_cursor;
287 }
288
289 AWS_VALIDATION_FAILURE_TEST_CONNECT(
290 connect,
291 username_too_long,
292 s_good_connect_view,
293 s_make_username_too_long_connect_view)
294
295 static void s_make_username_invalid_utf8_connect_view(struct aws_mqtt5_packet_connect_view *view) {
296 view->username = &s_invalid_utf8_string;
297 }
298
299 AWS_VALIDATION_FAILURE_TEST_CONNECT(
300 connect,
301 username_invalid_utf8,
302 s_good_connect_view,
303 s_make_username_invalid_utf8_connect_view)
304
305 static void s_make_password_too_long_connect_view(struct aws_mqtt5_packet_connect_view *view) {
306 view->password = &s_too_long_for_uint16_cursor;
307 }
308
309 AWS_VALIDATION_FAILURE_TEST_CONNECT(
310 connect,
311 password_too_long,
312 s_good_connect_view,
313 s_make_password_too_long_connect_view)
314
315 static const uint16_t s_zero_receive_maximum = 0;
316 static void s_make_receive_maximum_zero_connect_view(struct aws_mqtt5_packet_connect_view *view) {
317 view->receive_maximum = &s_zero_receive_maximum;
318 }
319
320 AWS_VALIDATION_FAILURE_TEST_CONNECT(
321 connect,
322 receive_maximum_zero,
323 s_good_connect_view,
324 s_make_receive_maximum_zero_connect_view)
325
326 static const uint32_t s_maximum_packet_size_zero = 0;
327 static void s_make_maximum_packet_size_zero_connect_view(struct aws_mqtt5_packet_connect_view *view) {
328 view->maximum_packet_size_bytes = &s_maximum_packet_size_zero;
329 }
330
331 AWS_VALIDATION_FAILURE_TEST_CONNECT(
332 connect,
333 maximum_packet_size_zero,
334 s_good_connect_view,
335 s_make_maximum_packet_size_zero_connect_view)
336
337 static void s_make_auth_method_unsupported_connect_view(struct aws_mqtt5_packet_connect_view *view) {
338 view->authentication_method = &s_binary_data_cursor;
339 }
340
341 AWS_VALIDATION_FAILURE_TEST_CONNECT(
342 connect,
343 auth_method_unsupported,
344 s_good_connect_view,
345 s_make_auth_method_unsupported_connect_view)
346
347 static void s_make_auth_data_unsupported_connect_view(struct aws_mqtt5_packet_connect_view *view) {
348 view->authentication_data = &s_binary_data_cursor;
349 }
350
351 AWS_VALIDATION_FAILURE_TEST_CONNECT(
352 connect,
353 auth_data_unsupported,
354 s_good_connect_view,
355 s_make_auth_data_unsupported_connect_view)
356
357 static uint8_t s_bad_boolean = 2;
358 static void s_make_request_problem_information_invalid_connect_view(struct aws_mqtt5_packet_connect_view *view) {
359 view->request_problem_information = &s_bad_boolean;
360 }
361
362 AWS_VALIDATION_FAILURE_TEST_CONNECT(
363 connect,
364 request_problem_information_invalid,
365 s_good_connect_view,
366 s_make_request_problem_information_invalid_connect_view)
367
368 static void s_make_request_response_information_invalid_connect_view(struct aws_mqtt5_packet_connect_view *view) {
369 view->request_response_information = &s_bad_boolean;
370 };
371
372 AWS_VALIDATION_FAILURE_TEST_CONNECT(
373 connect,
374 request_response_information_invalid,
375 s_good_connect_view,
376 s_make_request_response_information_invalid_connect_view)
377
378 static void s_make_user_properties_name_too_long_connect_view(struct aws_mqtt5_packet_connect_view *view) {
379 view->user_property_count = AWS_ARRAY_SIZE(s_bad_user_properties_name);
380 view->user_properties = s_bad_user_properties_name;
381 }
382
383 AWS_VALIDATION_FAILURE_TEST_CONNECT(
384 connect,
385 user_properties_name_too_long,
386 s_good_connect_view,
387 s_make_user_properties_name_too_long_connect_view)
388
389 static void s_make_user_properties_name_invalid_utf8_connect_view(struct aws_mqtt5_packet_connect_view *view) {
390 view->user_property_count = AWS_ARRAY_SIZE(s_user_properties_with_invalid_name);
391 view->user_properties = s_user_properties_with_invalid_name;
392 }
393
394 AWS_VALIDATION_FAILURE_TEST_CONNECT(
395 connect,
396 user_properties_name_invalid_utf8,
397 s_good_connect_view,
398 s_make_user_properties_name_invalid_utf8_connect_view)
399
400 static void s_make_user_properties_value_too_long_connect_view(struct aws_mqtt5_packet_connect_view *view) {
401 view->user_property_count = AWS_ARRAY_SIZE(s_bad_user_properties_value);
402 view->user_properties = s_bad_user_properties_value;
403 }
404
405 AWS_VALIDATION_FAILURE_TEST_CONNECT(
406 connect,
407 user_properties_value_too_long,
408 s_good_connect_view,
409 s_make_user_properties_value_too_long_connect_view)
410
411 static void s_make_user_properties_value_invalid_utf8_connect_view(struct aws_mqtt5_packet_connect_view *view) {
412 view->user_property_count = AWS_ARRAY_SIZE(s_user_properties_with_invalid_value);
413 view->user_properties = s_user_properties_with_invalid_value;
414 }
415
416 AWS_VALIDATION_FAILURE_TEST_CONNECT(
417 connect,
418 user_properties_value_invalid_utf8,
419 s_good_connect_view,
420 s_make_user_properties_value_invalid_utf8_connect_view)
421
422 static void s_make_user_properties_too_many_connect_view(struct aws_mqtt5_packet_connect_view *view) {
423 view->user_property_count = AWS_ARRAY_SIZE(s_bad_user_properties_too_many);
424 view->user_properties = s_bad_user_properties_too_many;
425 }
426
427 AWS_VALIDATION_FAILURE_TEST_CONNECT(
428 connect,
429 user_properties_too_many,
430 s_good_connect_view,
431 s_make_user_properties_too_many_connect_view)
432
433 static uint8_t s_good_topic[] = "hello/world";
434
435 /* no-topic and no-topic-alias is invalid */
436 static struct aws_mqtt5_packet_publish_view s_good_will_publish_view = {
437 .topic =
438 {
439 .ptr = s_good_topic,
440 .len = AWS_ARRAY_SIZE(s_good_topic) - 1,
441 },
442 };
443
444 static struct aws_mqtt5_packet_connect_view s_good_will_connect_view = {
445 .will = &s_good_will_publish_view,
446 };
447
448 static struct aws_mqtt5_packet_publish_view s_will_invalid_publish_view;
449
450 static void s_make_will_invalid_connect_view(struct aws_mqtt5_packet_connect_view *view) {
451 view->will = &s_will_invalid_publish_view;
452 }
453
454 AWS_VALIDATION_FAILURE_TEST_CONNECT(connect, will_invalid, s_good_will_connect_view, s_make_will_invalid_connect_view)
455
456 static struct aws_mqtt5_packet_publish_view s_will_payload_too_long_publish_view = {
457 .topic =
458 {
459 .ptr = s_user_prop_name,
460 .len = AWS_ARRAY_SIZE(s_user_prop_name) - 1,
461 },
462 .payload = {
463 .ptr = s_too_long_for_uint16,
464 .len = AWS_ARRAY_SIZE(s_too_long_for_uint16),
465 }};
466
467 static void s_make_will_payload_too_long_connect_view(struct aws_mqtt5_packet_connect_view *view) {
468 view->will = &s_will_payload_too_long_publish_view;
469 }
470
471 AWS_VALIDATION_FAILURE_TEST_CONNECT(
472 connect,
473 will_payload_too_long,
474 s_good_will_connect_view,
475 s_make_will_payload_too_long_connect_view)
476
477 static struct aws_mqtt5_subscription_view s_good_subscription[] = {
478 {
479 .topic_filter =
480 {
481 .ptr = s_good_topic,
482 .len = AWS_ARRAY_SIZE(s_good_topic) - 1,
483 },
484 .qos = AWS_MQTT5_QOS_AT_MOST_ONCE,
485 .no_local = false,
486 .retain_handling_type = AWS_MQTT5_RHT_SEND_ON_SUBSCRIBE,
487 .retain_as_published = false,
488 },
489 };
490
491 static struct aws_mqtt5_packet_subscribe_view s_good_subscribe_view = {
492 .subscriptions = s_good_subscription,
493 .subscription_count = AWS_ARRAY_SIZE(s_good_subscription),
494 };
495
496 static void s_make_no_subscriptions_subscribe_view(struct aws_mqtt5_packet_subscribe_view *view) {
497 view->subscriptions = NULL;
498 view->subscription_count = 0;
499 }
500
501 AWS_VALIDATION_FAILURE_TEST3(subscribe, no_subscriptions, s_good_subscribe_view, s_make_no_subscriptions_subscribe_view)
502
503 static struct aws_mqtt5_subscription_view
504 s_too_many_subscriptions[AWS_MQTT5_CLIENT_MAXIMUM_SUBSCRIPTIONS_PER_SUBSCRIBE + 1];
505
506 static void s_make_too_many_subscriptions_subscribe_view(struct aws_mqtt5_packet_subscribe_view *view) {
507 for (size_t i = 0; i < AWS_ARRAY_SIZE(s_too_many_subscriptions); ++i) {
508 struct aws_mqtt5_subscription_view *subscription_view = &s_too_many_subscriptions[i];
509
510 subscription_view->topic_filter = aws_byte_cursor_from_array(s_good_topic, AWS_ARRAY_SIZE(s_good_topic) - 1);
511 subscription_view->qos = AWS_MQTT5_QOS_AT_MOST_ONCE;
512 subscription_view->no_local = false;
513 subscription_view->retain_handling_type = AWS_MQTT5_RHT_SEND_ON_SUBSCRIBE;
514 subscription_view->retain_as_published = false;
515 }
516
517 view->subscriptions = s_too_many_subscriptions;
518 view->subscription_count = AWS_ARRAY_SIZE(s_too_many_subscriptions);
519 }
520
521 AWS_VALIDATION_FAILURE_TEST3(
522 subscribe,
523 too_many_subscriptions,
524 s_good_subscribe_view,
525 s_make_too_many_subscriptions_subscribe_view)
526
527 static const uint32_t s_invalid_subscription_identifier = AWS_MQTT5_MAXIMUM_VARIABLE_LENGTH_INTEGER + 1;
528
529 static void s_make_invalid_subscription_identifier_subscribe_view(struct aws_mqtt5_packet_subscribe_view *view) {
530 view->subscription_identifier = &s_invalid_subscription_identifier;
531 }
532
533 AWS_VALIDATION_FAILURE_TEST3(
534 subscribe,
535 invalid_subscription_identifier,
536 s_good_subscribe_view,
537 s_make_invalid_subscription_identifier_subscribe_view)
538
539 static uint8_t s_bad_topic_filter[] = "hello/#/world";
540
541 static struct aws_mqtt5_subscription_view s_invalid_topic_filter_subscription[] = {
542 {
543 .topic_filter =
544 {
545 .ptr = s_bad_topic_filter,
546 .len = AWS_ARRAY_SIZE(s_bad_topic_filter) - 1,
547 },
548 .qos = AWS_MQTT5_QOS_AT_MOST_ONCE,
549 .no_local = false,
550 .retain_handling_type = AWS_MQTT5_RHT_SEND_ON_SUBSCRIBE,
551 .retain_as_published = false,
552 },
553 };
554
555 static struct aws_mqtt5_subscription_view s_invalid_utf8_topic_filter_subscription[] = {
556 {
557 .topic_filter =
558 {
559 .ptr = (uint8_t *)s_invalid_utf8,
560 .len = AWS_ARRAY_SIZE(s_invalid_utf8) - 1,
561 },
562 .qos = AWS_MQTT5_QOS_AT_MOST_ONCE,
563 .no_local = false,
564 .retain_handling_type = AWS_MQTT5_RHT_SEND_ON_SUBSCRIBE,
565 .retain_as_published = false,
566 },
567 };
568
569 static void s_make_invalid_topic_filter_subscribe_view(struct aws_mqtt5_packet_subscribe_view *view) {
570 view->subscriptions = s_invalid_topic_filter_subscription;
571 view->subscription_count = AWS_ARRAY_SIZE(s_invalid_topic_filter_subscription);
572 };
573
574 AWS_VALIDATION_FAILURE_TEST3(
575 subscribe,
576 invalid_topic_filter,
577 s_good_subscribe_view,
578 s_make_invalid_topic_filter_subscribe_view)
579
580 static void s_make_invalid_utf8_topic_filter_subscribe_view(struct aws_mqtt5_packet_subscribe_view *view) {
581 view->subscriptions = s_invalid_utf8_topic_filter_subscription;
582 view->subscription_count = AWS_ARRAY_SIZE(s_invalid_utf8_topic_filter_subscription);
583 };
584
585 AWS_VALIDATION_FAILURE_TEST3(
586 subscribe,
587 invalid_utf8_topic_filter,
588 s_good_subscribe_view,
589 s_make_invalid_utf8_topic_filter_subscribe_view)
590
591 static struct aws_mqtt5_subscription_view s_invalid_qos_subscription[] = {
592 {
593 .topic_filter =
594 {
595 .ptr = s_good_topic,
596 .len = AWS_ARRAY_SIZE(s_good_topic) - 1,
597 },
598 .qos = 3,
599 .no_local = false,
600 .retain_handling_type = AWS_MQTT5_RHT_SEND_ON_SUBSCRIBE,
601 .retain_as_published = false,
602 },
603 };
604
605 static void s_make_invalid_qos_subscribe_view(struct aws_mqtt5_packet_subscribe_view *view) {
606 view->subscriptions = s_invalid_qos_subscription;
607 view->subscription_count = AWS_ARRAY_SIZE(s_invalid_qos_subscription);
608 }
609
610 AWS_VALIDATION_FAILURE_TEST3(subscribe, invalid_qos, s_good_subscribe_view, s_make_invalid_qos_subscribe_view)
611
612 static struct aws_mqtt5_subscription_view s_invalid_retain_type_subscription[] = {
613 {
614 .topic_filter =
615 {
616 .ptr = s_good_topic,
617 .len = AWS_ARRAY_SIZE(s_good_topic) - 1,
618 },
619 .qos = AWS_MQTT5_QOS_AT_MOST_ONCE,
620 .no_local = false,
621 .retain_handling_type = 5,
622 .retain_as_published = false,
623 },
624 };
625
626 static void s_make_invalid_retain_type_subscribe_view(struct aws_mqtt5_packet_subscribe_view *view) {
627 view->subscriptions = s_invalid_retain_type_subscription;
628 view->subscription_count = AWS_ARRAY_SIZE(s_invalid_retain_type_subscription);
629 }
630
631 AWS_VALIDATION_FAILURE_TEST3(
632 subscribe,
633 invalid_retain_type,
634 s_good_subscribe_view,
635 s_make_invalid_retain_type_subscribe_view)
636
637 static uint8_t s_shared_topic[] = "$share/sharename/topic/filter";
638
639 static struct aws_mqtt5_subscription_view s_invalid_no_local_subscription[] = {
640 {
641 .topic_filter =
642 {
643 .ptr = s_shared_topic,
644 .len = AWS_ARRAY_SIZE(s_shared_topic) - 1,
645 },
646 .qos = AWS_MQTT5_QOS_AT_MOST_ONCE,
647 .no_local = true,
648 .retain_handling_type = AWS_MQTT5_RHT_SEND_ON_SUBSCRIBE,
649 .retain_as_published = false,
650 },
651 };
652
653 static void s_make_invalid_no_local_subscribe_view(struct aws_mqtt5_packet_subscribe_view *view) {
654 view->subscriptions = s_invalid_no_local_subscription;
655 view->subscription_count = AWS_ARRAY_SIZE(s_invalid_no_local_subscription);
656 }
657
658 AWS_VALIDATION_FAILURE_TEST3(subscribe, invalid_no_local, s_good_subscribe_view, s_make_invalid_no_local_subscribe_view)
659
660 static void s_make_user_properties_name_too_long_subscribe_view(struct aws_mqtt5_packet_subscribe_view *view) {
661 view->user_property_count = AWS_ARRAY_SIZE(s_bad_user_properties_name);
662 view->user_properties = s_bad_user_properties_name;
663 }
664
665 AWS_VALIDATION_FAILURE_TEST3(
666 subscribe,
667 user_properties_name_too_long,
668 s_good_subscribe_view,
669 s_make_user_properties_name_too_long_subscribe_view)
670
671 static void s_make_user_properties_name_invalid_utf8_subscribe_view(struct aws_mqtt5_packet_subscribe_view *view) {
672 view->user_property_count = AWS_ARRAY_SIZE(s_user_properties_with_invalid_name);
673 view->user_properties = s_user_properties_with_invalid_name;
674 }
675
676 AWS_VALIDATION_FAILURE_TEST3(
677 subscribe,
678 user_properties_name_invalid_utf8,
679 s_good_subscribe_view,
680 s_make_user_properties_name_invalid_utf8_subscribe_view)
681
682 static void s_make_user_properties_value_too_long_subscribe_view(struct aws_mqtt5_packet_subscribe_view *view) {
683 view->user_property_count = AWS_ARRAY_SIZE(s_bad_user_properties_value);
684 view->user_properties = s_bad_user_properties_value;
685 }
686
687 AWS_VALIDATION_FAILURE_TEST3(
688 subscribe,
689 user_properties_value_too_long,
690 s_good_subscribe_view,
691 s_make_user_properties_value_too_long_subscribe_view)
692
693 static void s_make_user_properties_value_invalid_utf8_subscribe_view(struct aws_mqtt5_packet_subscribe_view *view) {
694 view->user_property_count = AWS_ARRAY_SIZE(s_user_properties_with_invalid_value);
695 view->user_properties = s_user_properties_with_invalid_value;
696 }
697
698 AWS_VALIDATION_FAILURE_TEST3(
699 subscribe,
700 user_properties_value_invalid_utf8,
701 s_good_subscribe_view,
702 s_make_user_properties_value_invalid_utf8_subscribe_view)
703
704 static void s_make_user_properties_too_many_subscribe_view(struct aws_mqtt5_packet_subscribe_view *view) {
705 view->user_property_count = AWS_ARRAY_SIZE(s_bad_user_properties_too_many);
706 view->user_properties = s_bad_user_properties_too_many;
707 }
708
709 AWS_VALIDATION_FAILURE_TEST3(
710 subscribe,
711 user_properties_too_many,
712 s_good_subscribe_view,
713 s_make_user_properties_too_many_subscribe_view)
714
715 static struct aws_byte_cursor s_good_topic_filter[] = {
716 {
717 .ptr = s_good_topic,
718 .len = AWS_ARRAY_SIZE(s_good_topic) - 1,
719 },
720 };
721
722 static struct aws_mqtt5_packet_unsubscribe_view s_good_unsubscribe_view = {
723 .topic_filters = s_good_topic_filter,
724 .topic_filter_count = AWS_ARRAY_SIZE(s_good_topic_filter),
725 };
726
727 static void s_make_no_topic_filters_unsubscribe_view(struct aws_mqtt5_packet_unsubscribe_view *view) {
728 view->topic_filters = NULL;
729 view->topic_filter_count = 0;
730 }
731
732 AWS_VALIDATION_FAILURE_TEST3(
733 unsubscribe,
734 no_topic_filters,
735 s_good_unsubscribe_view,
736 s_make_no_topic_filters_unsubscribe_view)
737
738 static struct aws_byte_cursor s_too_many_topic_filters[AWS_MQTT5_CLIENT_MAXIMUM_TOPIC_FILTERS_PER_UNSUBSCRIBE + 1];
739
740 static void s_make_too_many_topic_filters_unsubscribe_view(struct aws_mqtt5_packet_unsubscribe_view *view) {
741 for (size_t i = 0; i < AWS_ARRAY_SIZE(s_too_many_topic_filters); ++i) {
742 struct aws_byte_cursor *cursor = &s_too_many_topic_filters[i];
743
744 cursor->ptr = s_good_topic;
745 cursor->len = AWS_ARRAY_SIZE(s_good_topic) - 1;
746 }
747
748 view->topic_filters = s_too_many_topic_filters;
749 view->topic_filter_count = AWS_ARRAY_SIZE(s_too_many_topic_filters);
750 }
751
752 AWS_VALIDATION_FAILURE_TEST3(
753 unsubscribe,
754 too_many_topic_filters,
755 s_good_unsubscribe_view,
756 s_make_too_many_topic_filters_unsubscribe_view)
757
758 static struct aws_byte_cursor s_invalid_topic_filter[] = {
759 {
760 .ptr = s_bad_topic_filter,
761 .len = AWS_ARRAY_SIZE(s_bad_topic_filter) - 1,
762 },
763 };
764
765 static struct aws_byte_cursor s_invalid_utf8_topic_filter[] = {{
766 .ptr = (uint8_t *)s_invalid_utf8,
767 .len = AWS_ARRAY_SIZE(s_invalid_utf8) - 1,
768 }};
769
770 static void s_make_invalid_topic_filter_unsubscribe_view(struct aws_mqtt5_packet_unsubscribe_view *view) {
771 view->topic_filters = s_invalid_topic_filter;
772 view->topic_filter_count = AWS_ARRAY_SIZE(s_invalid_topic_filter);
773 }
774
775 AWS_VALIDATION_FAILURE_TEST3(
776 unsubscribe,
777 invalid_topic_filter,
778 s_good_unsubscribe_view,
779 s_make_invalid_topic_filter_unsubscribe_view)
780
781 static void s_make_invalid_utf8_topic_filter_unsubscribe_view(struct aws_mqtt5_packet_unsubscribe_view *view) {
782 view->topic_filters = s_invalid_utf8_topic_filter;
783 view->topic_filter_count = AWS_ARRAY_SIZE(s_invalid_utf8_topic_filter);
784 }
785
786 AWS_VALIDATION_FAILURE_TEST3(
787 unsubscribe,
788 invalid_utf8_topic_filter,
789 s_good_unsubscribe_view,
790 s_make_invalid_utf8_topic_filter_unsubscribe_view)
791
792 static void s_make_user_properties_name_too_long_unsubscribe_view(struct aws_mqtt5_packet_unsubscribe_view *view) {
793 view->user_property_count = AWS_ARRAY_SIZE(s_bad_user_properties_name);
794 view->user_properties = s_bad_user_properties_name;
795 }
796
797 AWS_VALIDATION_FAILURE_TEST3(
798 unsubscribe,
799 user_properties_name_too_long,
800 s_good_unsubscribe_view,
801 s_make_user_properties_name_too_long_unsubscribe_view)
802
803 static void s_make_user_properties_name_invalid_utf8_unsubscribe_view(struct aws_mqtt5_packet_unsubscribe_view *view) {
804 view->user_property_count = AWS_ARRAY_SIZE(s_user_properties_with_invalid_name);
805 view->user_properties = s_user_properties_with_invalid_name;
806 }
807
808 AWS_VALIDATION_FAILURE_TEST3(
809 unsubscribe,
810 user_properties_name_invalid_utf8,
811 s_good_unsubscribe_view,
812 s_make_user_properties_name_invalid_utf8_unsubscribe_view)
813
814 static void s_make_user_properties_value_too_long_unsubscribe_view(struct aws_mqtt5_packet_unsubscribe_view *view) {
815 view->user_property_count = AWS_ARRAY_SIZE(s_bad_user_properties_value);
816 view->user_properties = s_bad_user_properties_value;
817 }
818
819 AWS_VALIDATION_FAILURE_TEST3(
820 unsubscribe,
821 user_properties_value_too_long,
822 s_good_unsubscribe_view,
823 s_make_user_properties_value_too_long_unsubscribe_view)
824
825 static void s_make_user_properties_value_invalid_utf8_unsubscribe_view(struct aws_mqtt5_packet_unsubscribe_view *view) {
826 view->user_property_count = AWS_ARRAY_SIZE(s_user_properties_with_invalid_value);
827 view->user_properties = s_user_properties_with_invalid_value;
828 }
829
830 AWS_VALIDATION_FAILURE_TEST3(
831 unsubscribe,
832 user_properties_value_invalid_utf8,
833 s_good_unsubscribe_view,
834 s_make_user_properties_value_invalid_utf8_unsubscribe_view)
835
836 static void s_make_user_properties_too_many_unsubscribe_view(struct aws_mqtt5_packet_unsubscribe_view *view) {
837 view->user_property_count = AWS_ARRAY_SIZE(s_bad_user_properties_too_many);
838 view->user_properties = s_bad_user_properties_too_many;
839 }
840
841 AWS_VALIDATION_FAILURE_TEST3(
842 unsubscribe,
843 user_properties_too_many,
844 s_good_unsubscribe_view,
845 s_make_user_properties_too_many_unsubscribe_view)
846
847 static struct aws_mqtt5_packet_publish_view s_good_publish_view = {
848 .topic =
849 {
850 .ptr = s_good_topic,
851 .len = AWS_ARRAY_SIZE(s_good_topic) - 1,
852 },
853 };
854
855 static uint8_t s_invalid_topic[] = "hello/#";
856 static void s_make_invalid_topic_publish_view(struct aws_mqtt5_packet_publish_view *view) {
857 view->topic.ptr = s_invalid_topic;
858 view->topic.len = AWS_ARRAY_SIZE(s_invalid_topic) - 1;
859 }
860
861 AWS_VALIDATION_FAILURE_TEST3(publish, invalid_topic, s_good_publish_view, s_make_invalid_topic_publish_view)
862
863 static void s_make_invalid_utf8_topic_publish_view(struct aws_mqtt5_packet_publish_view *view) {
864 view->topic = s_invalid_utf8_string;
865 }
866
867 AWS_VALIDATION_FAILURE_TEST3(publish, invalid_utf8_topic, s_good_publish_view, s_make_invalid_utf8_topic_publish_view)
868
869 static void s_make_no_topic_publish_view(struct aws_mqtt5_packet_publish_view *view) {
870 view->topic.ptr = NULL;
871 view->topic.len = 0;
872 }
873
874 AWS_VALIDATION_FAILURE_TEST3(publish, no_topic, s_good_publish_view, s_make_no_topic_publish_view)
875
876 static enum aws_mqtt5_payload_format_indicator s_invalid_payload_format = 3;
877 static enum aws_mqtt5_payload_format_indicator s_valid_payload_format = AWS_MQTT5_PFI_UTF8;
878
879 static void s_make_invalid_payload_format_publish_view(struct aws_mqtt5_packet_publish_view *view) {
880 view->payload_format = &s_invalid_payload_format;
881 }
882
883 AWS_VALIDATION_FAILURE_TEST3(
884 publish,
885 invalid_payload_format,
886 s_good_publish_view,
887 s_make_invalid_payload_format_publish_view)
888
889 static void s_make_invalid_utf8_payload_publish_view(struct aws_mqtt5_packet_publish_view *view) {
890 view->payload_format = &s_valid_payload_format;
891 view->payload = s_invalid_utf8_string;
892 }
893
894 AWS_VALIDATION_FAILURE_TEST3(
895 publish,
896 invalid_utf8_payload,
897 s_good_publish_view,
898 s_make_invalid_utf8_payload_publish_view)
899
900 static void s_make_response_topic_too_long_publish_view(struct aws_mqtt5_packet_publish_view *view) {
901 view->response_topic = &s_too_long_for_uint16_cursor;
902 }
903
904 AWS_VALIDATION_FAILURE_TEST3(
905 publish,
906 response_topic_too_long,
907 s_good_publish_view,
908 s_make_response_topic_too_long_publish_view)
909
910 static struct aws_byte_cursor s_invalid_topic_cursor = {
911 .ptr = s_invalid_topic,
912 .len = AWS_ARRAY_SIZE(s_invalid_topic) - 1,
913 };
914
915 static void s_make_response_topic_invalid_publish_view(struct aws_mqtt5_packet_publish_view *view) {
916 view->response_topic = &s_invalid_topic_cursor;
917 }
918
919 AWS_VALIDATION_FAILURE_TEST3(
920 publish,
921 invalid_response_topic,
922 s_good_publish_view,
923 s_make_response_topic_invalid_publish_view)
924
925 static void s_make_response_topic_invalid_utf8_publish_view(struct aws_mqtt5_packet_publish_view *view) {
926 view->response_topic = &s_invalid_utf8_string;
927 }
928
929 AWS_VALIDATION_FAILURE_TEST3(
930 publish,
931 invalid_utf8_response_topic,
932 s_good_publish_view,
933 s_make_response_topic_invalid_utf8_publish_view)
934
935 static void s_make_correlation_data_too_long_publish_view(struct aws_mqtt5_packet_publish_view *view) {
936 view->correlation_data = &s_too_long_for_uint16_cursor;
937 }
938
939 AWS_VALIDATION_FAILURE_TEST3(
940 publish,
941 correlation_data_too_long,
942 s_good_publish_view,
943 s_make_correlation_data_too_long_publish_view)
944
945 static void s_make_content_type_invalid_utf8_publish_view(struct aws_mqtt5_packet_publish_view *view) {
946 view->content_type = &s_invalid_utf8_string;
947 }
948
949 AWS_VALIDATION_FAILURE_TEST3(
950 publish,
951 invalid_utf8_content_type,
952 s_good_publish_view,
953 s_make_content_type_invalid_utf8_publish_view)
954
955 static const uint32_t s_subscription_identifiers[] = {1, 2};
956
957 static void s_make_subscription_identifier_exists_publish_view(struct aws_mqtt5_packet_publish_view *view) {
958 view->subscription_identifiers = s_subscription_identifiers;
959 view->subscription_identifier_count = AWS_ARRAY_SIZE(s_subscription_identifiers);
960 }
961
962 AWS_VALIDATION_FAILURE_TEST3(
963 publish,
964 subscription_identifier_exists,
965 s_good_publish_view,
966 s_make_subscription_identifier_exists_publish_view)
967
968 static void s_make_content_type_too_long_publish_view(struct aws_mqtt5_packet_publish_view *view) {
969 view->content_type = &s_too_long_for_uint16_cursor;
970 }
971
972 AWS_VALIDATION_FAILURE_TEST3(
973 publish,
974 content_type_too_long,
975 s_good_publish_view,
976 s_make_content_type_too_long_publish_view)
977
978 static const uint16_t s_topic_alias_zero = 0;
979 static void s_make_topic_alias_zero_publish_view(struct aws_mqtt5_packet_publish_view *view) {
980 view->topic_alias = &s_topic_alias_zero;
981 view->topic.ptr = NULL;
982 view->topic.len = 0;
983 }
984
985 AWS_VALIDATION_FAILURE_TEST3(publish, topic_alias_zero, s_good_publish_view, s_make_topic_alias_zero_publish_view)
986
987 static void s_make_user_properties_name_too_long_publish_view(struct aws_mqtt5_packet_publish_view *view) {
988 view->user_property_count = AWS_ARRAY_SIZE(s_bad_user_properties_name);
989 view->user_properties = s_bad_user_properties_name;
990 }
991
992 AWS_VALIDATION_FAILURE_TEST3(
993 publish,
994 user_properties_name_too_long,
995 s_good_publish_view,
996 s_make_user_properties_name_too_long_publish_view)
997
998 static void s_make_user_properties_name_invalid_utf8_publish_view(struct aws_mqtt5_packet_publish_view *view) {
999 view->user_property_count = AWS_ARRAY_SIZE(s_user_properties_with_invalid_name);
1000 view->user_properties = s_user_properties_with_invalid_name;
1001 }
1002
1003 AWS_VALIDATION_FAILURE_TEST3(
1004 publish,
1005 user_properties_name_invalid_utf8,
1006 s_good_publish_view,
1007 s_make_user_properties_name_invalid_utf8_publish_view)
1008
1009 static void s_make_user_properties_value_too_long_publish_view(struct aws_mqtt5_packet_publish_view *view) {
1010 view->user_property_count = AWS_ARRAY_SIZE(s_bad_user_properties_value);
1011 view->user_properties = s_bad_user_properties_value;
1012 }
1013
1014 AWS_VALIDATION_FAILURE_TEST3(
1015 publish,
1016 user_properties_value_too_long,
1017 s_good_publish_view,
1018 s_make_user_properties_value_too_long_publish_view)
1019
1020 static void s_make_user_properties_value_invalid_utf8_publish_view(struct aws_mqtt5_packet_publish_view *view) {
1021 view->user_property_count = AWS_ARRAY_SIZE(s_user_properties_with_invalid_value);
1022 view->user_properties = s_user_properties_with_invalid_value;
1023 }
1024
1025 AWS_VALIDATION_FAILURE_TEST3(
1026 publish,
1027 user_properties_value_invalid_utf8,
1028 s_good_publish_view,
1029 s_make_user_properties_value_invalid_utf8_publish_view)
1030
1031 static void s_make_user_properties_too_many_publish_view(struct aws_mqtt5_packet_publish_view *view) {
1032 view->user_property_count = AWS_ARRAY_SIZE(s_bad_user_properties_too_many);
1033 view->user_properties = s_bad_user_properties_too_many;
1034 }
1035
1036 AWS_VALIDATION_FAILURE_TEST3(
1037 publish,
1038 user_properties_too_many,
1039 s_good_publish_view,
1040 s_make_user_properties_too_many_publish_view)
1041
1042 static void s_make_qos0_duplicate_true_publish_view(struct aws_mqtt5_packet_publish_view *view) {
1043 view->qos = AWS_MQTT5_QOS_AT_MOST_ONCE;
1044 view->duplicate = true;
1045 }
1046
1047 AWS_VALIDATION_FAILURE_TEST3(publish, qos0_duplicate_true, s_good_publish_view, s_make_qos0_duplicate_true_publish_view)
1048
1049 static void s_make_qos0_with_packet_id_publish_view(struct aws_mqtt5_packet_publish_view *view) {
1050 view->qos = AWS_MQTT5_QOS_AT_MOST_ONCE;
1051 view->packet_id = 1;
1052 }
1053
1054 AWS_VALIDATION_FAILURE_TEST3(publish, qos0_with_packet_id, s_good_publish_view, s_make_qos0_with_packet_id_publish_view)
1055
1056 #define AWS_CLIENT_CREATION_VALIDATION_FAILURE(failure_reason, base_options, mutate_function) \
1057 static int s_mqtt5_client_options_validation_failure_##failure_reason##_fn( \
1058 struct aws_allocator *allocator, void *ctx) { \
1059 (void)ctx; \
1060 aws_mqtt_library_init(allocator); \
1061 struct aws_event_loop_group *elg = NULL; \
1062 struct aws_host_resolver *hr = NULL; \
1063 struct aws_client_bootstrap *bootstrap = NULL; \
1064 elg = aws_event_loop_group_new_default(allocator, 1, NULL); \
1065 \
1066 struct aws_host_resolver_default_options hr_options = { \
1067 .el_group = elg, \
1068 .max_entries = 1, \
1069 }; \
1070 hr = aws_host_resolver_new_default(allocator, &hr_options); \
1071 \
1072 struct aws_client_bootstrap_options bootstrap_options = { \
1073 .event_loop_group = elg, \
1074 .host_resolver = hr, \
1075 }; \
1076 bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options); \
1077 \
1078 struct aws_mqtt5_client_options good_options = base_options; \
1079 good_options.bootstrap = bootstrap; \
1080 \
1081 ASSERT_SUCCESS(aws_mqtt5_client_options_validate(&good_options)); \
1082 struct aws_mqtt5_client *client = aws_mqtt5_client_new(allocator, &good_options); \
1083 ASSERT_NOT_NULL(client); \
1084 aws_mqtt5_client_release(client); \
1085 \
1086 struct aws_mqtt5_client_options bad_options = good_options; \
1087 (*mutate_function)(&bad_options); \
1088 ASSERT_FAILS(aws_mqtt5_client_options_validate(&bad_options)); \
1089 ASSERT_NULL(aws_mqtt5_client_new(allocator, &bad_options)); \
1090 \
1091 aws_client_bootstrap_release(bootstrap); \
1092 aws_host_resolver_release(hr); \
1093 aws_event_loop_group_release(elg); \
1094 \
1095 aws_mqtt_library_clean_up(); \
1096 return AWS_OP_SUCCESS; \
1097 } \
1098 \
1099 AWS_TEST_CASE( \
1100 mqtt5_client_options_validation_failure_##failure_reason, \
1101 s_mqtt5_client_options_validation_failure_##failure_reason##_fn)
1102
1103 static struct aws_socket_options s_good_socket_options = {
1104 .type = AWS_SOCKET_STREAM,
1105 .domain = AWS_SOCKET_IPV4,
1106 .connect_timeout_ms = 10000,
1107 };
1108
1109 static struct aws_mqtt5_packet_connect_view s_good_connect = {
1110 .keep_alive_interval_seconds = 30,
1111 };
1112
1113 void s_lifecycle_event_handler(const struct aws_mqtt5_client_lifecycle_event *event) {
1114 (void)event;
1115 }
1116
1117 void s_publish_received(const struct aws_mqtt5_packet_publish_view *publish, void *user_data) {
1118 (void)publish;
1119 (void)user_data;
1120 }
1121
1122 static struct aws_mqtt5_client_options s_good_client_options = {
1123 .host_name =
1124 {
1125 .ptr = s_server_reference,
1126 .len = AWS_ARRAY_SIZE(s_server_reference) - 1,
1127 },
1128 .port = 1883,
1129 .socket_options = &s_good_socket_options,
1130 .connect_options = &s_good_connect,
1131 .ping_timeout_ms = 5000,
1132 .lifecycle_event_handler = &s_lifecycle_event_handler,
1133 .publish_received_handler = &s_publish_received,
1134 };
1135
1136 static void s_make_no_host_client_options(struct aws_mqtt5_client_options *options) {
1137 options->host_name.ptr = NULL;
1138 options->host_name.len = 0;
1139 }
1140
1141 AWS_CLIENT_CREATION_VALIDATION_FAILURE(no_host, s_good_client_options, s_make_no_host_client_options)
1142
1143 static void s_make_no_bootstrap_client_options(struct aws_mqtt5_client_options *options) {
1144 options->bootstrap = NULL;
1145 }
1146
1147 AWS_CLIENT_CREATION_VALIDATION_FAILURE(no_bootstrap, s_good_client_options, s_make_no_bootstrap_client_options)
1148
1149 static void s_make_no_publish_received_client_options(struct aws_mqtt5_client_options *options) {
1150 options->publish_received_handler = NULL;
1151 }
1152
1153 AWS_CLIENT_CREATION_VALIDATION_FAILURE(
1154 no_publish_received,
1155 s_good_client_options,
1156 s_make_no_publish_received_client_options)
1157
1158 static struct aws_socket_options s_bad_socket_options = {
1159 .type = AWS_SOCKET_DGRAM,
1160 };
1161
1162 static void s_make_invalid_socket_options_client_options(struct aws_mqtt5_client_options *options) {
1163 options->socket_options = &s_bad_socket_options;
1164 };
1165
1166 AWS_CLIENT_CREATION_VALIDATION_FAILURE(
1167 invalid_socket_options,
1168 s_good_client_options,
1169 s_make_invalid_socket_options_client_options)
1170
1171 static struct aws_mqtt5_packet_connect_view s_client_id_too_long_connect_view = {
1172 .client_id =
1173 {
1174 .ptr = s_too_long_for_uint16,
1175 .len = AWS_ARRAY_SIZE(s_too_long_for_uint16),
1176 },
1177 };
1178
1179 static void s_make_invalid_connect_client_options(struct aws_mqtt5_client_options *options) {
1180 options->connect_options = &s_client_id_too_long_connect_view;
1181 }
1182
1183 AWS_CLIENT_CREATION_VALIDATION_FAILURE(invalid_connect, s_good_client_options, s_make_invalid_connect_client_options)
1184
1185 static struct aws_mqtt_iot_metrics invalid_utf8_metrics = {
1186 .library_name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("TestSDK\xFF\xFE")};
1187
1188 static void s_make_invalid_utf8_metrics(struct aws_mqtt5_client_options *options) {
1189 options->metrics = &invalid_utf8_metrics;
1190 }
1191
1192 AWS_CLIENT_CREATION_VALIDATION_FAILURE(invalid_metrics_string, s_good_client_options, s_make_invalid_utf8_metrics)
1193
1194 static void s_make_invalid_port_client_options(struct aws_mqtt5_client_options *options) {
1195 options->port = 0xFFFFFFFF;
1196 }
1197
1198 AWS_CLIENT_CREATION_VALIDATION_FAILURE(invalid_port, s_good_client_options, s_make_invalid_port_client_options)
1199
1200 #define AWS_CONNECTION_SETTINGS_VALIDATION_FAILURE_TEST_PREFIX(packet_type, failure_reason, init_success_settings_fn) \
1201 static int s_mqtt5_operation_##packet_type##_connection_settings_validation_failure_##failure_reason##_fn( \
1202 struct aws_allocator *allocator, void *ctx) { \
1203 (void)ctx; \
1204 \
1205 struct aws_mqtt5_client dummy_client; \
1206 AWS_ZERO_STRUCT(dummy_client); \
1207 \
1208 dummy_client.current_state = AWS_MCS_CONNECTED; \
1209 (*init_success_settings_fn)(&dummy_client);
1210
1211 #define AWS_CONNECTION_SETTINGS_VALIDATION_FAILURE_TEST_SUFFIX(packet_type, failure_reason, init_failure_settings_fn) \
1212 ASSERT_NOT_NULL(operation); \
1213 \
1214 ASSERT_SUCCESS(aws_mqtt5_operation_validate_vs_connection_settings(&operation->base, &dummy_client)); \
1215 \
1216 (*init_failure_settings_fn)(&dummy_client); \
1217 \
1218 ASSERT_FAILS(aws_mqtt5_operation_validate_vs_connection_settings(&operation->base, &dummy_client)); \
1219 \
1220 aws_mqtt5_operation_release(&operation->base); \
1221 \
1222 return AWS_OP_SUCCESS; \
1223 } \
1224 \
1225 AWS_TEST_CASE( \
1226 mqtt5_operation_##packet_type##_connection_settings_validation_failure_##failure_reason, \
1227 s_mqtt5_operation_##packet_type##_connection_settings_validation_failure_##failure_reason##_fn)
1228
1229 #define AWS_CONNECTION_SETTINGS_VALIDATION_FAILURE_TEST3( \
1230 packet_type, failure_reason, view_name, init_success_settings_fn, init_failure_settings_fn) \
1231 AWS_CONNECTION_SETTINGS_VALIDATION_FAILURE_TEST_PREFIX(packet_type, failure_reason, init_success_settings_fn) \
1232 struct aws_mqtt5_operation_##packet_type *operation = \
1233 aws_mqtt5_operation_##packet_type##_new(allocator, NULL, &view_name, NULL); \
1234 AWS_CONNECTION_SETTINGS_VALIDATION_FAILURE_TEST_SUFFIX(packet_type, failure_reason, init_failure_settings_fn)
1235
1236 static struct aws_mqtt5_packet_subscribe_view s_exceeds_maximum_packet_size_subscribe_view = {
1237 .subscriptions = s_good_subscription,
1238 .subscription_count = AWS_ARRAY_SIZE(s_good_subscription),
1239 };
1240
1241 static void s_packet_size_init_settings_success_fn(struct aws_mqtt5_client *dummy_client) {
1242 dummy_client->negotiated_settings.maximum_packet_size_to_server = 100;
1243 }
1244
1245 static void s_packet_size_init_settings_failure_fn(struct aws_mqtt5_client *dummy_client) {
1246 dummy_client->negotiated_settings.maximum_packet_size_to_server = 10;
1247 }
1248
1249 AWS_CONNECTION_SETTINGS_VALIDATION_FAILURE_TEST3(
1250 subscribe,
1251 exceeds_maximum_packet_size,
1252 s_exceeds_maximum_packet_size_subscribe_view,
1253 s_packet_size_init_settings_success_fn,
1254 s_packet_size_init_settings_failure_fn)
1255
1256 static struct aws_mqtt5_packet_unsubscribe_view s_exceeds_maximum_packet_size_unsubscribe_view = {
1257 .topic_filters = s_good_topic_filter,
1258 .topic_filter_count = AWS_ARRAY_SIZE(s_good_topic_filter),
1259 };
1260
1261 AWS_CONNECTION_SETTINGS_VALIDATION_FAILURE_TEST3(
1262 unsubscribe,
1263 exceeds_maximum_packet_size,
1264 s_exceeds_maximum_packet_size_unsubscribe_view,
1265 s_packet_size_init_settings_success_fn,
1266 s_packet_size_init_settings_failure_fn)
1267
1268 static struct aws_mqtt5_packet_publish_view s_exceeds_maximum_packet_size_publish_view = {
1269 .topic =
1270 {
1271 .ptr = s_good_topic,
1272 .len = AWS_ARRAY_SIZE(s_good_topic) - 1,
1273 },
1274 .payload =
1275 {
1276 .ptr = s_binary_data,
1277 .len = AWS_ARRAY_SIZE(s_binary_data),
1278 },
1279 };
1280
1281 AWS_CONNECTION_SETTINGS_VALIDATION_FAILURE_TEST3(
1282 publish,
1283 exceeds_maximum_packet_size,
1284 s_exceeds_maximum_packet_size_publish_view,
1285 s_packet_size_init_settings_success_fn,
1286 s_packet_size_init_settings_failure_fn)
1287
1288 static struct aws_mqtt5_packet_publish_view s_exceeds_maximum_qos_publish_view = {
1289 .topic =
1290 {
1291 .ptr = s_good_topic,
1292 .len = AWS_ARRAY_SIZE(s_good_topic) - 1,
1293 },
1294 .qos = AWS_MQTT5_QOS_EXACTLY_ONCE,
1295 };
1296
1297 static void s_maximum_qos_init_settings_success_fn(struct aws_mqtt5_client *dummy_client) {
1298 dummy_client->negotiated_settings.maximum_packet_size_to_server = 100;
1299 dummy_client->negotiated_settings.maximum_qos = AWS_MQTT5_QOS_EXACTLY_ONCE;
1300 }
1301
1302 static void s_maximum_qos_init_settings_failure_fn(struct aws_mqtt5_client *dummy_client) {
1303 dummy_client->negotiated_settings.maximum_qos = AWS_MQTT5_QOS_AT_LEAST_ONCE;
1304 }
1305
1306 AWS_CONNECTION_SETTINGS_VALIDATION_FAILURE_TEST3(
1307 publish,
1308 exceeds_maximum_qos,
1309 s_exceeds_maximum_qos_publish_view,
1310 s_maximum_qos_init_settings_success_fn,
1311 s_maximum_qos_init_settings_failure_fn)
1312
1313 static struct aws_mqtt5_packet_publish_view s_invalid_retain_publish_view = {
1314 .topic =
1315 {
1316 .ptr = s_good_topic,
1317 .len = AWS_ARRAY_SIZE(s_good_topic) - 1,
1318 },
1319 .qos = AWS_MQTT5_QOS_AT_MOST_ONCE,
1320 .retain = true,
1321 };
1322
1323 static void s_invalid_retain_init_settings_success_fn(struct aws_mqtt5_client *dummy_client) {
1324 dummy_client->negotiated_settings.maximum_packet_size_to_server = 100;
1325 dummy_client->negotiated_settings.retain_available = true;
1326 }
1327
1328 static void s_invalid_retain_init_settings_failure_fn(struct aws_mqtt5_client *dummy_client) {
1329 dummy_client->negotiated_settings.retain_available = false;
1330 }
1331
1332 AWS_CONNECTION_SETTINGS_VALIDATION_FAILURE_TEST3(
1333 publish,
1334 invalid_retain,
1335 s_invalid_retain_publish_view,
1336 s_invalid_retain_init_settings_success_fn,
1337 s_invalid_retain_init_settings_failure_fn)
1338
1339 #define AWS_CONNECTION_SETTINGS_VALIDATION_FAILURE_TEST4( \
1340 packet_type, failure_reason, view_name, init_success_settings_fn, init_failure_settings_fn) \
1341 AWS_CONNECTION_SETTINGS_VALIDATION_FAILURE_TEST_PREFIX(packet_type, failure_reason, init_success_settings_fn) \
1342 struct aws_mqtt5_operation_##packet_type *operation = \
1343 aws_mqtt5_operation_##packet_type##_new(allocator, &view_name, NULL, NULL); \
1344 AWS_CONNECTION_SETTINGS_VALIDATION_FAILURE_TEST_SUFFIX(packet_type, failure_reason, init_failure_settings_fn)
1345
1346 static uint8_t s_disconnect_reason_string[] = "We're leaving this planet for somewhere else";
1347 static struct aws_byte_cursor s_disconnect_reason_string_cursor = {
1348 .ptr = s_disconnect_reason_string,
1349 .len = AWS_ARRAY_SIZE(s_disconnect_reason_string) - 1,
1350 };
1351
1352 static struct aws_mqtt5_packet_disconnect_view s_exceeds_maximum_packet_size_disconnect_view = {
1353 .reason_string = &s_disconnect_reason_string_cursor,
1354 };
1355
1356 AWS_CONNECTION_SETTINGS_VALIDATION_FAILURE_TEST4(
1357 disconnect,
1358 exceeds_maximum_packet_size,
1359 s_exceeds_maximum_packet_size_disconnect_view,
1360 s_packet_size_init_settings_success_fn,
1361 s_packet_size_init_settings_failure_fn)
1362
1363 static const uint32_t s_positive_session_expiry = 1;
1364 static const uint32_t s_session_expiry = 5;
1365
1366 static struct aws_mqtt5_packet_disconnect_view s_promote_zero_session_expiry_disconnect_view = {
1367 .session_expiry_interval_seconds = &s_session_expiry,
1368 };
1369
1370 static int mqtt5_operation_disconnect_connection_settings_validation_failure_promote_zero_session_expiry_fn(
1371 struct aws_allocator *allocator,
1372 void *ctx) {
1373 (void)ctx;
1374
1375 aws_mqtt_library_init(allocator);
1376
1377 struct aws_event_loop_group *elg = aws_event_loop_group_new_default(allocator, 1, NULL);
1378
1379 struct aws_host_resolver_default_options hr_options = {
1380 .el_group = elg,
1381 .max_entries = 1,
1382 };
1383 struct aws_host_resolver *hr = aws_host_resolver_new_default(allocator, &hr_options);
1384
1385 struct aws_client_bootstrap_options bootstrap_options = {
1386 .event_loop_group = elg,
1387 .host_resolver = hr,
1388 };
1389 struct aws_client_bootstrap *bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
1390
1391 struct aws_mqtt5_client_options client_options = s_good_client_options;
1392 client_options.host_name = s_server_reference_cursor;
1393 client_options.bootstrap = bootstrap;
1394
1395 struct aws_mqtt5_packet_connect_view connect_options = s_good_connect;
1396 connect_options.session_expiry_interval_seconds = &s_positive_session_expiry;
1397 client_options.connect_options = &connect_options;
1398
1399 struct aws_mqtt5_client_options_storage *client_options_storage =
1400 aws_mqtt5_client_options_storage_new(allocator, &client_options);
1401 ASSERT_NOT_NULL(client_options_storage);
1402
1403 struct aws_mqtt5_client dummy_client;
1404 AWS_ZERO_STRUCT(dummy_client);
1405
1406 dummy_client.current_state = AWS_MCS_CONNECTED;
1407 dummy_client.negotiated_settings.maximum_packet_size_to_server = 100;
1408 dummy_client.config = client_options_storage;
1409
1410 struct aws_mqtt5_operation_disconnect *operation =
1411 aws_mqtt5_operation_disconnect_new(allocator, &s_promote_zero_session_expiry_disconnect_view, NULL, NULL);
1412
1413 ASSERT_SUCCESS(aws_mqtt5_operation_validate_vs_connection_settings(&operation->base, &dummy_client));
1414
1415 ((struct aws_mqtt5_client_options_storage *)dummy_client.config)
1416 ->connect->storage_view.session_expiry_interval_seconds = NULL;
1417
1418 ASSERT_FAILS(aws_mqtt5_operation_validate_vs_connection_settings(&operation->base, &dummy_client));
1419
1420 aws_mqtt5_operation_release(&operation->base);
1421 aws_mqtt5_client_options_storage_destroy(client_options_storage);
1422
1423 aws_client_bootstrap_release(bootstrap);
1424 aws_host_resolver_release(hr);
1425 aws_event_loop_group_release(elg);
1426
1427 aws_mqtt_library_clean_up();
1428
1429 return AWS_OP_SUCCESS;
1430 }
1431
1432 AWS_TEST_CASE(
1433 mqtt5_operation_disconnect_connection_settings_validation_failure_promote_zero_session_expiry,
1434 mqtt5_operation_disconnect_connection_settings_validation_failure_promote_zero_session_expiry_fn)