Debian Codegraph @

crt/aws-c-mqtt/tests/v5/mqtt5_utils_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/common/encoding.h>
7 #include <aws/mqtt/mqtt.h>
8 #include <aws/mqtt/private/v5/mqtt5_utils.h>
9
10 #include <aws/testing/aws_test_harness.h>
11
12 static int s_mqtt5_topic_skip_rules_prefix_fn(struct aws_allocator *allocator, void *ctx) {
13 (void)ctx;
14 (void)allocator;
15
16 struct aws_byte_cursor skip_cursor;
17 struct aws_byte_cursor expected_cursor;
18
19 /* nothing should be skipped */
20 skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(aws_byte_cursor_from_c_str("dont/skip/anything"));
21 expected_cursor = aws_byte_cursor_from_c_str("dont/skip/anything");
22 ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));
23
24 skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(aws_byte_cursor_from_c_str(""));
25 expected_cursor = aws_byte_cursor_from_c_str("");
26 ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));
27
28 skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(aws_byte_cursor_from_c_str("/"));
29 expected_cursor = aws_byte_cursor_from_c_str("/");
30 ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));
31
32 skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(aws_byte_cursor_from_c_str("$aws"));
33 expected_cursor = aws_byte_cursor_from_c_str("$aws");
34 ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));
35
36 skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(aws_byte_cursor_from_c_str("$aws/rules"));
37 expected_cursor = aws_byte_cursor_from_c_str("$aws/rules");
38 ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));
39
40 skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(aws_byte_cursor_from_c_str("$aws/rules/"));
41 expected_cursor = aws_byte_cursor_from_c_str("$aws/rules/");
42 ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));
43
44 skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(aws_byte_cursor_from_c_str("$aws/rules/rulename"));
45 expected_cursor = aws_byte_cursor_from_c_str("$aws/rules/rulename");
46 ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));
47
48 skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(aws_byte_cursor_from_c_str("$share"));
49 expected_cursor = aws_byte_cursor_from_c_str("$share");
50 ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));
51
52 skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(aws_byte_cursor_from_c_str("$share/"));
53 expected_cursor = aws_byte_cursor_from_c_str("$share/");
54 ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));
55
56 skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(aws_byte_cursor_from_c_str("$share/share-name"));
57 expected_cursor = aws_byte_cursor_from_c_str("$share/share-name");
58 ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));
59
60 /* prefix should be skipped */
61 skip_cursor =
62 aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(aws_byte_cursor_from_c_str("$aws/rules/rulename/"));
63 expected_cursor = aws_byte_cursor_from_c_str("");
64 ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));
65
66 skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(
67 aws_byte_cursor_from_c_str("$aws/rules/some-rule/segment1/segment2"));
68 expected_cursor = aws_byte_cursor_from_c_str("segment1/segment2");
69 ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));
70
71 skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(
72 aws_byte_cursor_from_c_str("$share/share-name/segment1/segment2"));
73 expected_cursor = aws_byte_cursor_from_c_str("segment1/segment2");
74 ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));
75
76 skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(
77 aws_byte_cursor_from_c_str("$share/share-name/$aws/rules/some-rule/segment1/segment2"));
78 expected_cursor = aws_byte_cursor_from_c_str("segment1/segment2");
79 ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));
80
81 return AWS_OP_SUCCESS;
82 }
83
84 AWS_TEST_CASE(mqtt5_topic_skip_rules_prefix, s_mqtt5_topic_skip_rules_prefix_fn)
85
86 static int s_mqtt5_topic_get_segment_count_fn(struct aws_allocator *allocator, void *ctx) {
87 (void)ctx;
88 (void)allocator;
89
90 ASSERT_INT_EQUALS(1, aws_mqtt5_topic_get_segment_count(aws_byte_cursor_from_c_str("")));
91 ASSERT_INT_EQUALS(1, aws_mqtt5_topic_get_segment_count(aws_byte_cursor_from_c_str("hello")));
92 ASSERT_INT_EQUALS(2, aws_mqtt5_topic_get_segment_count(aws_byte_cursor_from_c_str("hello/")));
93 ASSERT_INT_EQUALS(2, aws_mqtt5_topic_get_segment_count(aws_byte_cursor_from_c_str("hello/world")));
94 ASSERT_INT_EQUALS(3, aws_mqtt5_topic_get_segment_count(aws_byte_cursor_from_c_str("a/b/c")));
95 ASSERT_INT_EQUALS(3, aws_mqtt5_topic_get_segment_count(aws_byte_cursor_from_c_str("//")));
96 ASSERT_INT_EQUALS(4, aws_mqtt5_topic_get_segment_count(aws_byte_cursor_from_c_str("$SYS/bad/no/")));
97 ASSERT_INT_EQUALS(1, aws_mqtt5_topic_get_segment_count(aws_byte_cursor_from_c_str("$aws")));
98 ASSERT_INT_EQUALS(8, aws_mqtt5_topic_get_segment_count(aws_byte_cursor_from_c_str("//a//b/c//")));
99
100 ASSERT_INT_EQUALS(
101 2,
102 aws_mqtt5_topic_get_segment_count(aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(
103 aws_byte_cursor_from_c_str("$aws/rules/some-rule/segment1/segment2"))));
104 ASSERT_INT_EQUALS(
105 1,
106 aws_mqtt5_topic_get_segment_count(aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(
107 aws_byte_cursor_from_c_str("$aws/rules/some-rule/segment1"))));
108
109 return AWS_OP_SUCCESS;
110 }
111
112 AWS_TEST_CASE(mqtt5_topic_get_segment_count, s_mqtt5_topic_get_segment_count_fn)
113
114 static int s_mqtt5_shared_subscription_validation_fn(struct aws_allocator *allocator, void *ctx) {
115 (void)ctx;
116 (void)allocator;
117
118 ASSERT_FALSE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("")));
119 ASSERT_FALSE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("oof")));
120 ASSERT_FALSE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$sha")));
121 ASSERT_FALSE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share")));
122 ASSERT_FALSE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share/")));
123 ASSERT_FALSE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share//")));
124 ASSERT_FALSE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share//test")));
125 ASSERT_FALSE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share/m+/")));
126 ASSERT_FALSE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share/m#/")));
127 ASSERT_FALSE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share/m")));
128 ASSERT_FALSE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share/m/")));
129
130 ASSERT_TRUE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share/m/#")));
131 ASSERT_TRUE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share/m/great")));
132 ASSERT_TRUE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share/m/test/+")));
133 ASSERT_TRUE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share/m/+/test")));
134 ASSERT_TRUE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share/m/test/#")));
135
136 return AWS_OP_SUCCESS;
137 }
138
139 AWS_TEST_CASE(mqtt5_shared_subscription_validation, s_mqtt5_shared_subscription_validation_fn)