|
| 1 | +/** |
| 2 | + * @file test_path_utils.c |
| 3 | + * @brief Layer 2 unit tests — stream name validation and sanitization |
| 4 | + * |
| 5 | + * Covers is_valid_stream_name() (added for #396) and sanitize_stream_name(). |
| 6 | + * |
| 7 | + * Stream names flow through filesystem paths, URLs, and the go2rtc stream |
| 8 | + * table, so they must survive URL-encoding round-trips unchanged. Names |
| 9 | + * containing spaces or other special characters produce "mse: stream not |
| 10 | + * found" errors because the client sends the URL-encoded form while |
| 11 | + * go2rtc registered the decoded form. |
| 12 | + */ |
| 13 | + |
| 14 | +#define _POSIX_C_SOURCE 200809L |
| 15 | +#define _GNU_SOURCE |
| 16 | + |
| 17 | +#include <string.h> |
| 18 | + |
| 19 | +#include "unity.h" |
| 20 | +#include "core/path_utils.h" |
| 21 | + |
| 22 | +void setUp(void) {} |
| 23 | +void tearDown(void) {} |
| 24 | + |
| 25 | +/* ================================================================ |
| 26 | + * is_valid_stream_name — accepts |
| 27 | + * ================================================================ */ |
| 28 | + |
| 29 | +void test_valid_simple_lowercase(void) { |
| 30 | + TEST_ASSERT_TRUE(is_valid_stream_name("front_door")); |
| 31 | +} |
| 32 | + |
| 33 | +void test_valid_mixed_case(void) { |
| 34 | + TEST_ASSERT_TRUE(is_valid_stream_name("FrontDoor")); |
| 35 | +} |
| 36 | + |
| 37 | +void test_valid_digits(void) { |
| 38 | + TEST_ASSERT_TRUE(is_valid_stream_name("cam01")); |
| 39 | +} |
| 40 | + |
| 41 | +void test_valid_hyphen(void) { |
| 42 | + TEST_ASSERT_TRUE(is_valid_stream_name("front-door")); |
| 43 | +} |
| 44 | + |
| 45 | +void test_valid_underscore(void) { |
| 46 | + TEST_ASSERT_TRUE(is_valid_stream_name("front_door_camera")); |
| 47 | +} |
| 48 | + |
| 49 | +void test_valid_dot_in_middle(void) { |
| 50 | + TEST_ASSERT_TRUE(is_valid_stream_name("cam.1")); |
| 51 | +} |
| 52 | + |
| 53 | +void test_valid_single_char(void) { |
| 54 | + TEST_ASSERT_TRUE(is_valid_stream_name("a")); |
| 55 | +} |
| 56 | + |
| 57 | +/* ================================================================ |
| 58 | + * is_valid_stream_name — rejects (the #396 cases) |
| 59 | + * ================================================================ */ |
| 60 | + |
| 61 | +void test_reject_null(void) { |
| 62 | + TEST_ASSERT_FALSE(is_valid_stream_name(NULL)); |
| 63 | +} |
| 64 | + |
| 65 | +void test_reject_empty(void) { |
| 66 | + TEST_ASSERT_FALSE(is_valid_stream_name("")); |
| 67 | +} |
| 68 | + |
| 69 | +void test_reject_space_in_middle(void) { |
| 70 | + /* The primary #396 case: "Front Door Camera" breaks MSE/HLS. */ |
| 71 | + TEST_ASSERT_FALSE(is_valid_stream_name("Front Door Camera")); |
| 72 | +} |
| 73 | + |
| 74 | +void test_reject_leading_space(void) { |
| 75 | + TEST_ASSERT_FALSE(is_valid_stream_name(" front_door")); |
| 76 | +} |
| 77 | + |
| 78 | +void test_reject_trailing_space(void) { |
| 79 | + TEST_ASSERT_FALSE(is_valid_stream_name("front_door ")); |
| 80 | +} |
| 81 | + |
| 82 | +void test_reject_tab(void) { |
| 83 | + TEST_ASSERT_FALSE(is_valid_stream_name("front\tdoor")); |
| 84 | +} |
| 85 | + |
| 86 | +void test_reject_slash(void) { |
| 87 | + /* Would break path routing (/api/streams/{name}/...). */ |
| 88 | + TEST_ASSERT_FALSE(is_valid_stream_name("front/door")); |
| 89 | +} |
| 90 | + |
| 91 | +void test_reject_backslash(void) { |
| 92 | + TEST_ASSERT_FALSE(is_valid_stream_name("front\\door")); |
| 93 | +} |
| 94 | + |
| 95 | +void test_reject_question_mark(void) { |
| 96 | + /* Would become a query-string boundary. */ |
| 97 | + TEST_ASSERT_FALSE(is_valid_stream_name("front?door")); |
| 98 | +} |
| 99 | + |
| 100 | +void test_reject_ampersand(void) { |
| 101 | + TEST_ASSERT_FALSE(is_valid_stream_name("front&door")); |
| 102 | +} |
| 103 | + |
| 104 | +void test_reject_hash(void) { |
| 105 | + /* URL fragment boundary. */ |
| 106 | + TEST_ASSERT_FALSE(is_valid_stream_name("front#door")); |
| 107 | +} |
| 108 | + |
| 109 | +void test_reject_percent(void) { |
| 110 | + /* Looks like URL-encoding. */ |
| 111 | + TEST_ASSERT_FALSE(is_valid_stream_name("front%20door")); |
| 112 | +} |
| 113 | + |
| 114 | +void test_reject_leading_dot(void) { |
| 115 | + /* Would produce a hidden-file path. */ |
| 116 | + TEST_ASSERT_FALSE(is_valid_stream_name(".hidden")); |
| 117 | +} |
| 118 | + |
| 119 | +void test_reject_dot_dot(void) { |
| 120 | + /* Path traversal. */ |
| 121 | + TEST_ASSERT_FALSE(is_valid_stream_name("..")); |
| 122 | +} |
| 123 | + |
| 124 | +void test_reject_non_ascii(void) { |
| 125 | + /* Non-ASCII round-trips poorly through URL encoding. */ |
| 126 | + TEST_ASSERT_FALSE(is_valid_stream_name("caméra")); |
| 127 | +} |
| 128 | + |
| 129 | +void test_reject_null_byte_is_not_applicable(void) { |
| 130 | + /* C strings terminate at NUL; nothing to test beyond empty case. */ |
| 131 | + TEST_PASS(); |
| 132 | +} |
| 133 | + |
| 134 | +/* ================================================================ |
| 135 | + * sanitize_stream_name regression coverage |
| 136 | + * |
| 137 | + * The validator and sanitizer must agree on the "safe" character set: |
| 138 | + * sanitize_stream_name() maps spaces to underscores, which means |
| 139 | + * "Front Door" and "Front_Door" both collapse to "Front_Door" on disk. |
| 140 | + * That collision is exactly why is_valid_stream_name() rejects spaces. |
| 141 | + * ================================================================ */ |
| 142 | + |
| 143 | +void test_sanitize_maps_space_to_underscore(void) { |
| 144 | + char out[64]; |
| 145 | + sanitize_stream_name("Front Door", out, sizeof(out)); |
| 146 | + TEST_ASSERT_EQUAL_STRING("Front_Door", out); |
| 147 | +} |
| 148 | + |
| 149 | +void test_sanitize_preserves_valid_names(void) { |
| 150 | + /* A name that passes is_valid_stream_name must be unchanged by sanitize. */ |
| 151 | + char out[64]; |
| 152 | + sanitize_stream_name("front_door-1", out, sizeof(out)); |
| 153 | + TEST_ASSERT_EQUAL_STRING("front_door-1", out); |
| 154 | +} |
| 155 | + |
| 156 | +void test_sanitize_two_names_that_would_collide(void) { |
| 157 | + /* Demonstrates the collision that motivates rejecting spaces at creation. */ |
| 158 | + char out_a[64], out_b[64]; |
| 159 | + sanitize_stream_name("Front Door", out_a, sizeof(out_a)); |
| 160 | + sanitize_stream_name("Front_Door", out_b, sizeof(out_b)); |
| 161 | + TEST_ASSERT_EQUAL_STRING(out_a, out_b); |
| 162 | +} |
| 163 | + |
| 164 | +/* ================================================================ |
| 165 | + * main |
| 166 | + * ================================================================ */ |
| 167 | + |
| 168 | +int main(void) { |
| 169 | + UNITY_BEGIN(); |
| 170 | + |
| 171 | + RUN_TEST(test_valid_simple_lowercase); |
| 172 | + RUN_TEST(test_valid_mixed_case); |
| 173 | + RUN_TEST(test_valid_digits); |
| 174 | + RUN_TEST(test_valid_hyphen); |
| 175 | + RUN_TEST(test_valid_underscore); |
| 176 | + RUN_TEST(test_valid_dot_in_middle); |
| 177 | + RUN_TEST(test_valid_single_char); |
| 178 | + |
| 179 | + RUN_TEST(test_reject_null); |
| 180 | + RUN_TEST(test_reject_empty); |
| 181 | + RUN_TEST(test_reject_space_in_middle); |
| 182 | + RUN_TEST(test_reject_leading_space); |
| 183 | + RUN_TEST(test_reject_trailing_space); |
| 184 | + RUN_TEST(test_reject_tab); |
| 185 | + RUN_TEST(test_reject_slash); |
| 186 | + RUN_TEST(test_reject_backslash); |
| 187 | + RUN_TEST(test_reject_question_mark); |
| 188 | + RUN_TEST(test_reject_ampersand); |
| 189 | + RUN_TEST(test_reject_hash); |
| 190 | + RUN_TEST(test_reject_percent); |
| 191 | + RUN_TEST(test_reject_leading_dot); |
| 192 | + RUN_TEST(test_reject_dot_dot); |
| 193 | + RUN_TEST(test_reject_non_ascii); |
| 194 | + RUN_TEST(test_reject_null_byte_is_not_applicable); |
| 195 | + |
| 196 | + RUN_TEST(test_sanitize_maps_space_to_underscore); |
| 197 | + RUN_TEST(test_sanitize_preserves_valid_names); |
| 198 | + RUN_TEST(test_sanitize_two_names_that_would_collide); |
| 199 | + |
| 200 | + return UNITY_END(); |
| 201 | +} |
0 commit comments