File: mqtt5_utils_tests.c

package info (click to toggle)
aws-crt-python 0.20.4%2Bdfsg-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 72,656 kB
  • sloc: ansic: 381,805; python: 23,008; makefile: 6,251; sh: 4,536; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (139 lines) | stat: -rw-r--r-- 7,742 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#include <aws/common/encoding.h>
#include <aws/mqtt/mqtt.h>
#include <aws/mqtt/private/v5/mqtt5_utils.h>

#include <aws/testing/aws_test_harness.h>

static int s_mqtt5_topic_skip_rules_prefix_fn(struct aws_allocator *allocator, void *ctx) {
    (void)ctx;
    (void)allocator;

    struct aws_byte_cursor skip_cursor;
    struct aws_byte_cursor expected_cursor;

    /* nothing should be skipped */
    skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(aws_byte_cursor_from_c_str("dont/skip/anything"));
    expected_cursor = aws_byte_cursor_from_c_str("dont/skip/anything");
    ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));

    skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(aws_byte_cursor_from_c_str(""));
    expected_cursor = aws_byte_cursor_from_c_str("");
    ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));

    skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(aws_byte_cursor_from_c_str("/"));
    expected_cursor = aws_byte_cursor_from_c_str("/");
    ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));

    skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(aws_byte_cursor_from_c_str("$aws"));
    expected_cursor = aws_byte_cursor_from_c_str("$aws");
    ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));

    skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(aws_byte_cursor_from_c_str("$aws/rules"));
    expected_cursor = aws_byte_cursor_from_c_str("$aws/rules");
    ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));

    skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(aws_byte_cursor_from_c_str("$aws/rules/"));
    expected_cursor = aws_byte_cursor_from_c_str("$aws/rules/");
    ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));

    skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(aws_byte_cursor_from_c_str("$aws/rules/rulename"));
    expected_cursor = aws_byte_cursor_from_c_str("$aws/rules/rulename");
    ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));

    skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(aws_byte_cursor_from_c_str("$share"));
    expected_cursor = aws_byte_cursor_from_c_str("$share");
    ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));

    skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(aws_byte_cursor_from_c_str("$share/"));
    expected_cursor = aws_byte_cursor_from_c_str("$share/");
    ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));

    skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(aws_byte_cursor_from_c_str("$share/share-name"));
    expected_cursor = aws_byte_cursor_from_c_str("$share/share-name");
    ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));

    /* prefix should be skipped */
    skip_cursor =
        aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(aws_byte_cursor_from_c_str("$aws/rules/rulename/"));
    expected_cursor = aws_byte_cursor_from_c_str("");
    ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));

    skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(
        aws_byte_cursor_from_c_str("$aws/rules/some-rule/segment1/segment2"));
    expected_cursor = aws_byte_cursor_from_c_str("segment1/segment2");
    ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));

    skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(
        aws_byte_cursor_from_c_str("$share/share-name/segment1/segment2"));
    expected_cursor = aws_byte_cursor_from_c_str("segment1/segment2");
    ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));

    skip_cursor = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(
        aws_byte_cursor_from_c_str("$share/share-name/$aws/rules/some-rule/segment1/segment2"));
    expected_cursor = aws_byte_cursor_from_c_str("segment1/segment2");
    ASSERT_TRUE(aws_byte_cursor_eq(&skip_cursor, &expected_cursor));

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(mqtt5_topic_skip_rules_prefix, s_mqtt5_topic_skip_rules_prefix_fn)

static int s_mqtt5_topic_get_segment_count_fn(struct aws_allocator *allocator, void *ctx) {
    (void)ctx;
    (void)allocator;

    ASSERT_INT_EQUALS(1, aws_mqtt5_topic_get_segment_count(aws_byte_cursor_from_c_str("")));
    ASSERT_INT_EQUALS(1, aws_mqtt5_topic_get_segment_count(aws_byte_cursor_from_c_str("hello")));
    ASSERT_INT_EQUALS(2, aws_mqtt5_topic_get_segment_count(aws_byte_cursor_from_c_str("hello/")));
    ASSERT_INT_EQUALS(2, aws_mqtt5_topic_get_segment_count(aws_byte_cursor_from_c_str("hello/world")));
    ASSERT_INT_EQUALS(3, aws_mqtt5_topic_get_segment_count(aws_byte_cursor_from_c_str("a/b/c")));
    ASSERT_INT_EQUALS(3, aws_mqtt5_topic_get_segment_count(aws_byte_cursor_from_c_str("//")));
    ASSERT_INT_EQUALS(4, aws_mqtt5_topic_get_segment_count(aws_byte_cursor_from_c_str("$SYS/bad/no/")));
    ASSERT_INT_EQUALS(1, aws_mqtt5_topic_get_segment_count(aws_byte_cursor_from_c_str("$aws")));
    ASSERT_INT_EQUALS(8, aws_mqtt5_topic_get_segment_count(aws_byte_cursor_from_c_str("//a//b/c//")));

    ASSERT_INT_EQUALS(
        2,
        aws_mqtt5_topic_get_segment_count(aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(
            aws_byte_cursor_from_c_str("$aws/rules/some-rule/segment1/segment2"))));
    ASSERT_INT_EQUALS(
        1,
        aws_mqtt5_topic_get_segment_count(aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(
            aws_byte_cursor_from_c_str("$aws/rules/some-rule/segment1"))));

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(mqtt5_topic_get_segment_count, s_mqtt5_topic_get_segment_count_fn)

static int s_mqtt5_shared_subscription_validation_fn(struct aws_allocator *allocator, void *ctx) {
    (void)ctx;
    (void)allocator;

    ASSERT_FALSE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("")));
    ASSERT_FALSE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("oof")));
    ASSERT_FALSE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$sha")));
    ASSERT_FALSE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share")));
    ASSERT_FALSE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share/")));
    ASSERT_FALSE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share//")));
    ASSERT_FALSE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share//test")));
    ASSERT_FALSE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share/m+/")));
    ASSERT_FALSE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share/m#/")));
    ASSERT_FALSE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share/m")));
    ASSERT_FALSE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share/m/")));

    ASSERT_TRUE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share/m/#")));
    ASSERT_TRUE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share/m/great")));
    ASSERT_TRUE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share/m/test/+")));
    ASSERT_TRUE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share/m/+/test")));
    ASSERT_TRUE(aws_mqtt_is_topic_filter_shared_subscription(aws_byte_cursor_from_c_str("$share/m/test/#")));

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(mqtt5_shared_subscription_validation, s_mqtt5_shared_subscription_validation_fn)