Debian Codegraph @

crt/aws-c-mqtt/tests/v3/topic_tree_test.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/mqtt/private/topic_tree.h>
7
8 #include <aws/testing/aws_test_harness.h>
9
10 #include <aws/common/string.h>
11
12 static struct aws_byte_cursor s_empty_cursor = {
13 .ptr = NULL,
14 .len = 0,
15 };
16
17 static int times_called = 0;
18 static void on_publish(
19 const struct aws_byte_cursor *topic,
20 const struct aws_byte_cursor *payload,
21 bool dup,
22 enum aws_mqtt_qos qos,
23 bool retain,
24 void *user_data) {
25
26 (void)topic;
27 (void)payload;
28 (void)dup;
29 (void)qos;
30 (void)retain;
31 (void)user_data;
32
33 times_called++;
34 }
35
36 static void s_string_clean_up(void *userdata) {
37 struct aws_string *string = userdata;
38 aws_string_destroy(string);
39 }
40
41 /* Subscribes to multiple topics and returns the number that matched with the pub_topic */
42 static int s_check_multi_topic_match(
43 struct aws_allocator *allocator,
44 const char **sub_filters,
45 size_t sub_filters_len,
46 const char *pub_topic) {
47
48 times_called = 0;
49
50 struct aws_mqtt_topic_tree tree;
51 aws_mqtt_topic_tree_init(&tree, allocator);
52
53 for (size_t i = 0; i < sub_filters_len; ++i) {
54 struct aws_string *topic_filter = aws_string_new_from_c_str(allocator, sub_filters[i]);
55 aws_mqtt_topic_tree_insert(
56 &tree, topic_filter, AWS_MQTT_QOS_AT_MOST_ONCE, &on_publish, s_string_clean_up, topic_filter);
57 }
58
59 struct aws_byte_cursor filter_cursor = aws_byte_cursor_from_array(pub_topic, strlen(pub_topic));
60 struct aws_mqtt_packet_publish publish;
61 aws_mqtt_packet_publish_init(&publish, false, AWS_MQTT_QOS_AT_MOST_ONCE, false, filter_cursor, 1, s_empty_cursor);
62
63 aws_mqtt_topic_tree_publish(&tree, &publish);
64
65 aws_mqtt_topic_tree_clean_up(&tree);
66
67 return times_called;
68 }
69 static bool s_check_topic_match(struct aws_allocator *allocator, const char *sub_filter, const char *pub_topic) {
70 int matches = s_check_multi_topic_match(allocator, &sub_filter, 1, pub_topic);
71 return matches == 1;
72 }
73
74 AWS_TEST_CASE(mqtt_topic_tree_match, s_mqtt_topic_tree_match_fn)
75 static int s_mqtt_topic_tree_match_fn(struct aws_allocator *allocator, void *ctx) {
76 (void)ctx;
77
78 /* Check single-level filters */
79 ASSERT_TRUE(s_check_topic_match(allocator, "a", "a"));
80 ASSERT_FALSE(s_check_topic_match(allocator, "b", "B"));
81
82 /* Check multi-level filters */
83 ASSERT_TRUE(s_check_topic_match(allocator, "a/b", "a/b"));
84 ASSERT_FALSE(s_check_topic_match(allocator, "a/b", "a/B"));
85 ASSERT_FALSE(s_check_topic_match(allocator, "a/b", "a/b/c"));
86 ASSERT_FALSE(s_check_topic_match(allocator, "a/b/c", "a/b"));
87 ASSERT_TRUE(s_check_topic_match(allocator, "a/b/c", "a/b/c"));
88 ASSERT_FALSE(s_check_topic_match(allocator, "a/b/c", "a/B/c"));
89
90 /* Check single-level wildcard filters */
91 ASSERT_TRUE(s_check_topic_match(allocator, "sport/tennis/+", "sport/tennis/player1"));
92 ASSERT_TRUE(s_check_topic_match(allocator, "sport/tennis/+", "sport/tennis/player2"));
93 ASSERT_FALSE(s_check_topic_match(allocator, "sport/tennis/+", "sport/tennis/player1/ranking"));
94
95 ASSERT_TRUE(s_check_topic_match(allocator, "sport/+", "sport/"));
96 ASSERT_FALSE(s_check_topic_match(allocator, "sport/+", "sport"));
97
98 ASSERT_TRUE(s_check_topic_match(allocator, "+/+", "/finance"));
99 ASSERT_TRUE(s_check_topic_match(allocator, "/+", "/finance"));
100 ASSERT_FALSE(s_check_topic_match(allocator, "+", "/finance"));
101
102 ASSERT_TRUE(s_check_topic_match(allocator, "///", "///"));
103 ASSERT_FALSE(s_check_topic_match(allocator, "///", "//"));
104
105 const char *sub_topics[] = {"a/b/c", "a/+/c", "a/#"};
106 ASSERT_INT_EQUALS(s_check_multi_topic_match(allocator, sub_topics, AWS_ARRAY_SIZE(sub_topics), "a/b/c"), 3);
107 ASSERT_INT_EQUALS(s_check_multi_topic_match(allocator, sub_topics, AWS_ARRAY_SIZE(sub_topics), "a/Z/c"), 2);
108 ASSERT_INT_EQUALS(s_check_multi_topic_match(allocator, sub_topics, AWS_ARRAY_SIZE(sub_topics), "a/b/Z"), 1);
109 ASSERT_INT_EQUALS(s_check_multi_topic_match(allocator, sub_topics, AWS_ARRAY_SIZE(sub_topics), "Z/b/c"), 0);
110
111 return AWS_OP_SUCCESS;
112 }
113
114 static struct aws_byte_cursor s_topic_a_a = {
115 .ptr = (uint8_t *)"a/a",
116 .len = 3,
117 };
118 static struct aws_byte_cursor s_topic_a_a_a = {
119 .ptr = (uint8_t *)"a/a/a",
120 .len = 5,
121 };
122 static struct aws_byte_cursor s_topic_a_a_b = {
123 .ptr = (uint8_t *)"a/a/b",
124 .len = 5,
125 };
126
127 AWS_TEST_CASE(mqtt_topic_tree_unsubscribe, s_mqtt_topic_tree_unsubscribe_fn)
128 static int s_mqtt_topic_tree_unsubscribe_fn(struct aws_allocator *allocator, void *ctx) {
129 (void)ctx;
130
131 struct aws_mqtt_topic_tree tree;
132 ASSERT_SUCCESS(aws_mqtt_topic_tree_init(&tree, allocator));
133
134 struct aws_string *topic_a_a = aws_string_new_from_array(allocator, s_topic_a_a.ptr, s_topic_a_a.len);
135 struct aws_string *topic_a_a_a = aws_string_new_from_array(allocator, s_topic_a_a_a.ptr, s_topic_a_a_a.len);
136 struct aws_string *topic_a_a_b = aws_string_new_from_array(allocator, s_topic_a_a_b.ptr, s_topic_a_a_b.len);
137
138 AWS_VARIABLE_LENGTH_ARRAY(uint8_t, transaction_buf, aws_mqtt_topic_tree_action_size * 3);
139 struct aws_array_list transaction;
140 aws_array_list_init_static(&transaction, transaction_buf, 3, aws_mqtt_topic_tree_action_size);
141
142 ASSERT_SUCCESS(aws_mqtt_topic_tree_insert(
143 &tree, topic_a_a_a, AWS_MQTT_QOS_AT_MOST_ONCE, &on_publish, s_string_clean_up, topic_a_a_a));
144 /* At this moment, the topic_a_a was not inserted. Though the remove returns a success, the topic_a_a_a should still
145 * remained in the tree.
146 * The test is inspired by the ticket: https://github.com/awslabs/aws-crt-nodejs/issues/405. There was a crash when
147 * we unsubscribe from an unsubscribed parent topic.
148 * We fixed the issue in https://github.com/awslabs/aws-c-mqtt/pull/297 */
149 ASSERT_SUCCESS(aws_mqtt_topic_tree_remove(&tree, &s_topic_a_a));
150 ASSERT_SUCCESS(aws_mqtt_topic_tree_remove(&tree, &s_topic_a_a_a));
151 /* Re-create, it was nuked by remove. */
152 topic_a_a_a = aws_string_new_from_array(allocator, s_topic_a_a_a.ptr, s_topic_a_a_a.len);
153 /* Ensure that the intermediate 'a' node was removed as well. */
154 ASSERT_UINT_EQUALS(0, aws_hash_table_get_entry_count(&tree.root->subtopics));
155
156 /* Put it back so we can test removal of a partial tree. */
157 /* Bonus points: test transactions here */
158 ASSERT_SUCCESS(aws_mqtt_topic_tree_transaction_insert(
159 &tree, &transaction, topic_a_a_a, AWS_MQTT_QOS_AT_MOST_ONCE, &on_publish, s_string_clean_up, topic_a_a_a));
160 ASSERT_SUCCESS(aws_mqtt_topic_tree_transaction_insert(
161 &tree, &transaction, topic_a_a, AWS_MQTT_QOS_AT_MOST_ONCE, &on_publish, s_string_clean_up, topic_a_a));
162 ASSERT_SUCCESS(aws_mqtt_topic_tree_transaction_insert(
163 &tree, &transaction, topic_a_a_b, AWS_MQTT_QOS_AT_MOST_ONCE, &on_publish, s_string_clean_up, topic_a_a_b));
164 aws_mqtt_topic_tree_transaction_commit(&tree, &transaction);
165
166 /* Should remove the last /a, but not the first 2. */
167 void *userdata = (void *)0xBADCAFE;
168 ASSERT_SUCCESS(aws_mqtt_topic_tree_transaction_remove(&tree, &transaction, &s_topic_a_a_a, &userdata));
169 /* Ensure userdata was set back to the right user_data correctly. */
170 ASSERT_PTR_EQUALS(topic_a_a_a, userdata);
171 ASSERT_SUCCESS(aws_mqtt_topic_tree_transaction_remove(&tree, &transaction, &s_topic_a_a, NULL));
172 aws_mqtt_topic_tree_transaction_commit(&tree, &transaction);
173
174 struct aws_mqtt_packet_publish publish;
175 aws_mqtt_packet_publish_init(&publish, false, AWS_MQTT_QOS_AT_MOST_ONCE, false, s_topic_a_a_a, 1, s_topic_a_a_a);
176
177 times_called = 0;
178 aws_mqtt_topic_tree_publish(&tree, &publish);
179 ASSERT_INT_EQUALS(times_called, 0);
180
181 publish.topic_name = s_topic_a_a_b;
182
183 times_called = 0;
184 aws_mqtt_topic_tree_publish(&tree, &publish);
185 ASSERT_INT_EQUALS(times_called, 1);
186
187 const struct aws_byte_cursor not_in_tree = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("not/in/tree");
188 aws_mqtt_topic_tree_remove(&tree, &not_in_tree);
189
190 aws_mqtt_topic_tree_clean_up(&tree);
191 return AWS_OP_SUCCESS;
192 }
193
194 struct s_duplicate_test_ud {
195 struct aws_string *string;
196 bool cleaned;
197 };
198
199 static void s_userdata_cleanup(void *userdata) {
200 struct s_duplicate_test_ud *ud = userdata;
201 aws_string_destroy(ud->string);
202 ud->cleaned = true;
203 }
204
205 AWS_TEST_CASE(mqtt_topic_tree_duplicate_transactions, s_mqtt_topic_tree_duplicate_transactions_fn)
206 static int s_mqtt_topic_tree_duplicate_transactions_fn(struct aws_allocator *allocator, void *ctx) {
207 (void)ctx;
208
209 struct aws_mqtt_topic_tree tree;
210 ASSERT_SUCCESS(aws_mqtt_topic_tree_init(&tree, allocator));
211
212 struct aws_string *topic_a_a = aws_string_new_from_array(allocator, s_topic_a_a.ptr, s_topic_a_a.len);
213 struct aws_string *topic_a_a_a = aws_string_new_from_array(allocator, s_topic_a_a_a.ptr, s_topic_a_a_a.len);
214 struct aws_string *topic_a_a_a_copy = aws_string_new_from_array(allocator, s_topic_a_a_a.ptr, s_topic_a_a_a.len);
215 struct aws_string *topic_a_a_b = aws_string_new_from_array(allocator, s_topic_a_a_b.ptr, s_topic_a_a_b.len);
216
217 size_t number_topics = 4;
218
219 AWS_VARIABLE_LENGTH_ARRAY(uint8_t, transaction_buf, aws_mqtt_topic_tree_action_size * number_topics);
220 struct aws_array_list transaction;
221 aws_array_list_init_static(&transaction, transaction_buf, number_topics, aws_mqtt_topic_tree_action_size);
222
223 /* Ensure that the intermediate 'a' node was removed as well. */
224 ASSERT_UINT_EQUALS(0, aws_hash_table_get_entry_count(&tree.root->subtopics));
225
226 struct s_duplicate_test_ud ud_a_a = {.string = topic_a_a, .cleaned = false};
227 struct s_duplicate_test_ud ud_a_a_a = {.string = topic_a_a_a, .cleaned = false};
228 struct s_duplicate_test_ud ud_a_a_a_copy = {.string = topic_a_a_a_copy, .cleaned = false};
229 struct s_duplicate_test_ud ud_a_a_b = {.string = topic_a_a_b, .cleaned = false};
230
231 /* insert duplicate strings, and the old userdata will be cleaned up */
232 ASSERT_SUCCESS(aws_mqtt_topic_tree_transaction_insert(
233 &tree, &transaction, topic_a_a_a, AWS_MQTT_QOS_AT_MOST_ONCE, &on_publish, s_userdata_cleanup, &ud_a_a_a));
234 ASSERT_SUCCESS(aws_mqtt_topic_tree_transaction_insert(
235 &tree,
236 &transaction,
237 topic_a_a_a_copy,
238 AWS_MQTT_QOS_AT_MOST_ONCE,
239 &on_publish,
240 s_userdata_cleanup,
241 &ud_a_a_a_copy));
242 ASSERT_SUCCESS(aws_mqtt_topic_tree_transaction_insert(
243 &tree, &transaction, topic_a_a, AWS_MQTT_QOS_AT_MOST_ONCE, &on_publish, s_userdata_cleanup, &ud_a_a));
244 ASSERT_SUCCESS(aws_mqtt_topic_tree_transaction_insert(
245 &tree, &transaction, topic_a_a_b, AWS_MQTT_QOS_AT_MOST_ONCE, &on_publish, s_userdata_cleanup, &ud_a_a_b));
246 aws_mqtt_topic_tree_transaction_commit(&tree, &transaction);
247
248 /* The copy replaced the original node, and the old string has been cleaned up, but the new string will live with
249 * the topic tree. */
250 ASSERT_TRUE(ud_a_a_a.cleaned);
251 ASSERT_FALSE(ud_a_a_a_copy.cleaned);
252 ASSERT_FALSE(ud_a_a.cleaned);
253 ASSERT_FALSE(ud_a_a_b.cleaned);
254
255 /* the result will be the same as we just intert three nodes */
256 ASSERT_UINT_EQUALS(1, aws_hash_table_get_entry_count(&tree.root->subtopics));
257
258 struct aws_mqtt_packet_publish publish;
259 aws_mqtt_packet_publish_init(&publish, false, AWS_MQTT_QOS_AT_MOST_ONCE, false, s_topic_a_a, 1, s_topic_a_a);
260
261 times_called = 0;
262 aws_mqtt_topic_tree_publish(&tree, &publish);
263 ASSERT_INT_EQUALS(times_called, 1);
264
265 aws_mqtt_topic_tree_clean_up(&tree);
266 ASSERT_TRUE(ud_a_a_a_copy.cleaned);
267 ASSERT_TRUE(ud_a_a.cleaned);
268 ASSERT_TRUE(ud_a_a_b.cleaned);
269 return AWS_OP_SUCCESS;
270 }
271
272 AWS_TEST_CASE(mqtt_topic_tree_transactions, s_mqtt_topic_tree_transactions_fn)
273 static int s_mqtt_topic_tree_transactions_fn(struct aws_allocator *allocator, void *ctx) {
274 (void)ctx;
275
276 struct aws_mqtt_topic_tree tree;
277 ASSERT_SUCCESS(aws_mqtt_topic_tree_init(&tree, allocator));
278
279 struct aws_string *topic_a_a = aws_string_new_from_array(allocator, s_topic_a_a.ptr, s_topic_a_a.len);
280
281 AWS_VARIABLE_LENGTH_ARRAY(uint8_t, transaction_buf, aws_mqtt_topic_tree_action_size * 3);
282 struct aws_array_list transaction;
283 aws_array_list_init_static(&transaction, transaction_buf, 3, aws_mqtt_topic_tree_action_size);
284
285 ASSERT_SUCCESS(aws_mqtt_topic_tree_transaction_insert(
286 &tree, &transaction, topic_a_a, AWS_MQTT_QOS_AT_MOST_ONCE, &on_publish, s_string_clean_up, topic_a_a));
287 /* The userdata will not be cleaned up by roll back, since the transaction has not been commit yet. */
288 aws_mqtt_topic_tree_transaction_roll_back(&tree, &transaction);
289
290 /* Ensure that the intermediate 'a' node was removed as well. */
291 ASSERT_UINT_EQUALS(0, aws_hash_table_get_entry_count(&tree.root->subtopics));
292 /* Insert(commit), remove, roll back the removal */
293 ASSERT_SUCCESS(aws_mqtt_topic_tree_insert(
294 &tree, topic_a_a, AWS_MQTT_QOS_AT_MOST_ONCE, &on_publish, s_string_clean_up, topic_a_a));
295 ASSERT_SUCCESS(aws_mqtt_topic_tree_transaction_remove(&tree, &transaction, &s_topic_a_a, NULL));
296 aws_mqtt_topic_tree_transaction_roll_back(&tree, &transaction);
297
298 struct aws_mqtt_packet_publish publish;
299 aws_mqtt_packet_publish_init(&publish, false, AWS_MQTT_QOS_AT_MOST_ONCE, false, s_topic_a_a, 1, s_topic_a_a);
300
301 times_called = 0;
302 aws_mqtt_topic_tree_publish(&tree, &publish);
303 ASSERT_INT_EQUALS(times_called, 1);
304
305 aws_mqtt_topic_tree_clean_up(&tree);
306 return AWS_OP_SUCCESS;
307 }
308
309 AWS_TEST_CASE(mqtt_topic_validation, s_mqtt_topic_validation_fn)
310 static int s_mqtt_topic_validation_fn(struct aws_allocator *allocator, void *ctx) {
311 (void)allocator;
312 (void)ctx;
313
314 #define ASSERT_TOPIC_VALIDITY(expected, topic) \
315 do { \
316 struct aws_byte_cursor topic_cursor; \
317 topic_cursor.ptr = (uint8_t *)(topic); \
318 topic_cursor.len = strlen(topic); \
319 ASSERT_##expected(aws_mqtt_is_valid_topic(&topic_cursor)); \
320 } while (false)
321
322 ASSERT_TOPIC_VALIDITY(TRUE, "/");
323 ASSERT_TOPIC_VALIDITY(TRUE, "a/");
324 ASSERT_TOPIC_VALIDITY(TRUE, "/b");
325 ASSERT_TOPIC_VALIDITY(TRUE, "a/b/c");
326
327 ASSERT_TOPIC_VALIDITY(FALSE, "#");
328 ASSERT_TOPIC_VALIDITY(FALSE, "sport/tennis/#");
329 ASSERT_TOPIC_VALIDITY(FALSE, "sport/tennis#");
330 ASSERT_TOPIC_VALIDITY(FALSE, "sport/tennis/#/ranking");
331 ASSERT_TOPIC_VALIDITY(FALSE, "");
332 ASSERT_TOPIC_VALIDITY(FALSE, "+");
333 ASSERT_TOPIC_VALIDITY(FALSE, "+/tennis/#");
334 ASSERT_TOPIC_VALIDITY(FALSE, "sport/+/player1");
335 ASSERT_TOPIC_VALIDITY(FALSE, "sport+");
336
337 ASSERT_TOPIC_VALIDITY(FALSE, "\x41/\xED\xBF\xBF/\x41");
338
339 return AWS_OP_SUCCESS;
340 }
341
342 AWS_TEST_CASE(mqtt_topic_filter_validation, s_mqtt_topic_filter_validation_fn)
343 static int s_mqtt_topic_filter_validation_fn(struct aws_allocator *allocator, void *ctx) {
344 (void)allocator;
345 (void)ctx;
346
347 #define ASSERT_TOPIC_FILTER_VALIDITY(expected, topic_filter) \
348 do { \
349 struct aws_byte_cursor topic_filter_cursor; \
350 topic_filter_cursor.ptr = (uint8_t *)(topic_filter); \
351 topic_filter_cursor.len = strlen(topic_filter); \
352 ASSERT_##expected(aws_mqtt_is_valid_topic_filter(&topic_filter_cursor)); \
353 } while (false)
354
355 ASSERT_TOPIC_FILTER_VALIDITY(TRUE, "#");
356 ASSERT_TOPIC_FILTER_VALIDITY(TRUE, "sport/tennis/#");
357 ASSERT_TOPIC_FILTER_VALIDITY(FALSE, "sport/tennis#");
358 ASSERT_TOPIC_FILTER_VALIDITY(FALSE, "sport/tennis/#/ranking");
359 ASSERT_TOPIC_FILTER_VALIDITY(FALSE, "");
360
361 ASSERT_TOPIC_FILTER_VALIDITY(TRUE, "+/");
362 ASSERT_TOPIC_FILTER_VALIDITY(TRUE, "+");
363 ASSERT_TOPIC_FILTER_VALIDITY(TRUE, "+/tennis/#");
364 ASSERT_TOPIC_FILTER_VALIDITY(TRUE, "sport/+/player1");
365 ASSERT_TOPIC_FILTER_VALIDITY(FALSE, "sport+");
366
367 ASSERT_TOPIC_FILTER_VALIDITY(FALSE, "\x41/\xED\xA0\x80/\x41");
368
369 return AWS_OP_SUCCESS;
370 }