aboutsummaryrefslogtreecommitdiff
path: root/config.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2026-07-26 14:10:06 +0000
committerDaniel Eklöf <daniel@ekloef.se>2026-07-26 17:38:05 +0000
commit2055f06842fcd64fd3049cad7865cc321fca68f5 (patch)
treea57c2687779b07d6f8371a03d5f12352e605e2c8 /config.c
parentconfig: update default URL regex (diff)
config: add unit test for default URL regex
Diffstat (limited to 'config.c')
-rw-r--r--config.c137
1 files changed, 137 insertions, 0 deletions
diff --git a/config.c b/config.c
index 618ee116..69968c4a 100644
--- a/config.c
+++ b/config.c
@@ -4239,3 +4239,140 @@ conf_modifiers_to_mask(const struct seat *seat,
return mods;
}
#endif
+
+
+static void UNUSED
+unittest_check_url_match(const char *url, const char *expected_match,
+ regmatch_t match)
+{
+ const size_t mlen = match.rm_eo - match.rm_so;
+ const size_t start = &url[match.rm_so] - url;
+ //const size_t end = start + mlen;
+
+ if (mlen != strlen(expected_match) ||
+ memcmp(&url[start], expected_match, mlen) != 0)
+ {
+ printf("url: %s\n"
+ " expected match: %s\n"
+ " match: %.*s\n",
+ url, expected_match, (int)mlen, &url[start]);
+ BUG("URL regex failure");
+ }
+}
+
+UNITTEST
+{
+ regex_t re;
+ int r = regcomp(&re, default_url_regex_string, REG_EXTENDED);
+ xassert(r == 0);
+ xassert(re.re_nsub >= 1);
+
+ const struct {
+ const char *url;
+ const char *expected_match;
+ } test_cases[] = {
+ {"https://www.foobar.com/[a](b)\"c\"'d'efg.html?foo=bar&bar=foo",
+ "https://www.foobar.com/[a](b)\"c\"'d'efg.html?foo=bar&bar=foo"},
+
+ /* Trailing quotes are not matched */
+ {"https://quote-at-the-end'",
+ "https://quote-at-the-end"},
+
+ {"https://quote-at-the-end\"",
+ "https://quote-at-the-end"},
+
+ /* That includes quoted URLs too */
+ {"'https://quote-at-the-end''",
+ "https://quote-at-the-end"},
+
+ {"\"https://quote-at-the-end\"\"",
+ "https://quote-at-the-end"},
+
+ /* Allow '-quotes in the middle */
+ {"https://unbalanced'not-at-the-end",
+ "https://unbalanced'not-at-the-end"},
+
+ /* But not "-quotes */
+ {"https://unbalanced\"not-at-the-end",
+ "https://unbalanced"},
+
+ /* "Balanced" quotes are allowed, at the end */
+ {"https://balanced-'quote'",
+ "https://balanced-'quote'"},
+
+ /* ... and in the middle */
+ {"https://balanced-'quote'-in-the-middle",
+ "https://balanced-'quote'-in-the-middle"},
+
+ /* Balanced "-quotes too */
+ {"https://balanced-\"quote\"",
+ "https://balanced-\"quote\""},
+
+ {"https://balanced-\"quote\"-in-the-middle",
+ "https://balanced-\"quote\"-in-the-middle"},
+
+ /* Quoted URLs should match the URL part, not the quotes */
+ {"'https://quoted-url'",
+ "https://quoted-url"},
+
+ /* Same with "-quotes */
+ {"\"https://quoted-url\"",
+ "https://quoted-url"},
+
+ {"\"https://quoted-url-with-'-inside\"",
+ "https://quoted-url-with-'-inside"},
+
+ {"'https://abc' foobar",
+ "https://abc"},
+
+ {"https://foo[bar",
+ "https://foo"},
+
+ {"https://foo[]bar",
+ "https://foo"},
+
+ {"https://foo[abc]",
+ "https://foo[abc]"},
+
+ {"https://foo[abc]bar",
+ "https://foo[abc]bar"},
+
+ {"https://foo(bar",
+ "https://foo"},
+
+ {"https://foo()bar",
+ "https://foo"},
+
+ {"https://foo(abc)",
+ "https://foo(abc)"},
+
+ {"https://foo(abc)bar",
+ "https://foo(abc)bar"},
+
+ {"https://foo{bar",
+ "https://foo"},
+
+ {"https://foo{}bar",
+ "https://foo"},
+
+ {"https://foo{abc}bar",
+ "https://foo"},
+ };
+
+ for (size_t i = 0; i < ALEN(test_cases); i++) {
+ const char *const url = test_cases[i].url;
+ const char *const expected_match = test_cases[i].expected_match;
+
+#if 0
+ printf("url: %s\n", url);
+ printf("expecede: %s\n", expected_match);
+#endif
+
+ regmatch_t matches[re.re_nsub + 1];
+ int r UNUSED = regexec(&re, url, re.re_nsub + 1, matches, 0);
+ assert(r == 0);
+ unittest_check_url_match(url, expected_match, matches[1]);
+ }
+
+ regfree(&re);
+}