Skip to content

Commit 4e1e77c

Browse files
keith-packardjukkar
authored andcommitted
test/posix: Adjust fnmatch tests to conform with POSIX specs
Some of the fnmatch tests didn't match POSIX. > - zassert_ok(fnmatch("[[?*\\]", "\\", 0)); This pattern includes a back-slash, and because FNM_NOESCAPE was not specified, it will escape the ], so there isn't actually a range expression here, nor is there any literal backslash in the pattern. So, this does not match backslash. Replace this with three tests to check each of these issues: > + /* \\ escapes ], no bracket expr, no match */ > + zassert_equal(fnmatch("[[?*\\]", "\\", 0), FNM_NOMATCH); > + /* \\ unescaped, match */ > + zassert_ok(fnmatch("[[?*\\]", "\\", FNM_NOESCAPE)); > + /* \\ escapes \\, match */ > + zassert_ok(fnmatch("[[?*\\\\]", "\\", 0)); > - zassert_ok(fnmatch("[]?*\\]", "]", 0)); This one has the same issue, except it tries to match ] instead. Again, because the \ will end up escaping the final ], this is not a range expression, and so it doesn't match ]. Replace this with three tests to check each case separately: > + /* \\ escapes ], no bracket expr, no match */ > + zassert_equal(fnmatch("[]?*\\]", "]", 0), FNM_NOMATCH); > + /* \\ unescaped, match */ > + zassert_ok(fnmatch("[]?*\\]", "]", FNM_NOESCAPE)); > + /* \\ escapes \\, match */ > + zassert_ok(fnmatch("[]?*\\\\]", "]", 0)); > - /* zassert_ok(fnmatch("*[[:alpha:]]/""*[[:alnum:]]", "a/b", > - FNM_PATHNAME)); */ > + zassert_ok(fnmatch("*[[:alpha:]]/""*[[:alnum:]]", "a/b", > + FNM_PATHNAME)); Re-enable this test; it looks good. The "" will be elided by the compiler, so I'm not sure why it's here. > - zassert_not_equal(fnmatch("*[![:digit:]]*/[![:d-d]", "a/b", > - FNM_PATHNAME), 0); > + zassert_ok(fnmatch("*[![:digit:]]*/[![:d-d]", "a/b", FNM_PATHNAME)); This pattern matches the string: * - matches zero characters [![:digit:]] - matches 'a' (it's not a digit) * - matches zero characters / - matches slash (FNM_PATHNAME specified) [![:d-d] - matches 'b' (not in the set "[:d-d") > - zassert_not_equal(fnmatch("*[![:digit:]]*/[[:d-d]", "a/[", > - FNM_PATHNAME), 0); > + zassert_ok(fnmatch("*[![:digit:]]*/[[:d-d]", "a/[", FNM_PATHNAME)); This pattern matches the string: * - matches zero characters [![:digit:]] - matches 'a' * - matches zero characters / - matches / (FNM_PATHNAME is specified) [[:d-d] - matches [ (in the set "[:d-d") I've run all of these tests against glibc which agrees with this version, and also picolibc, which also works with a pending PR that adds support for character classes, equivalence classes and collating sequences. Signed-off-by: Keith Packard <keithp@keithp.com>
1 parent e49042c commit 4e1e77c

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

tests/posix/c_lib_ext/src/fnmatch.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,18 @@ ZTEST(posix_c_lib_ext, test_fnmatch)
3030
zassert_equal(fnmatch("a*.c", "a/x.c", FNM_PATHNAME), FNM_NOMATCH);
3131
zassert_ok(fnmatch("*/foo", "/foo", FNM_PATHNAME));
3232
zassert_ok(fnmatch("-O[01]", "-O1", 0));
33-
zassert_ok(fnmatch("[[?*\\]", "\\", 0));
34-
zassert_ok(fnmatch("[]?*\\]", "]", 0));
33+
/* \\ escapes ], no bracket expr, no match */
34+
zassert_equal(fnmatch("[[?*\\]", "\\", 0), FNM_NOMATCH);
35+
/* \\ unescaped, match */
36+
zassert_ok(fnmatch("[[?*\\]", "\\", FNM_NOESCAPE));
37+
/* \\ escapes \\, match */
38+
zassert_ok(fnmatch("[[?*\\\\]", "\\", 0));
39+
/* \\ escapes ], no bracket expr, no match */
40+
zassert_equal(fnmatch("[]?*\\]", "]", 0), FNM_NOMATCH);
41+
/* \\ unescaped, match */
42+
zassert_ok(fnmatch("[]?*\\]", "]", FNM_NOESCAPE));
43+
/* \\ escapes \\, match */
44+
zassert_ok(fnmatch("[]?*\\\\]", "]", 0));
3545
zassert_ok(fnmatch("[!]a-]", "b", 0));
3646
zassert_ok(fnmatch("[]-_]", "^", 0));
3747
zassert_ok(fnmatch("[!]-_]", "X", 0));
@@ -73,9 +83,9 @@ ZTEST(posix_c_lib_ext, test_fnmatch)
7383
zassert_equal(fnmatch("*/*", "a/.b", FNM_PATHNAME | FNM_PERIOD), FNM_NOMATCH);
7484
zassert_ok(fnmatch("*?*/*", "a/.b", FNM_PERIOD));
7585
zassert_ok(fnmatch("*[.]/b", "a./b", FNM_PATHNAME | FNM_PERIOD));
76-
/* zassert_ok(fnmatch("*[[:alpha:]]/""*[[:alnum:]]", "a/b", FNM_PATHNAME)); */
77-
zassert_not_equal(fnmatch("*[![:digit:]]*/[![:d-d]", "a/b", FNM_PATHNAME), 0);
78-
zassert_not_equal(fnmatch("*[![:digit:]]*/[[:d-d]", "a/[", FNM_PATHNAME), 0);
86+
zassert_ok(fnmatch("*[[:alpha:]]/""*[[:alnum:]]", "a/b", FNM_PATHNAME));
87+
zassert_ok(fnmatch("*[![:digit:]]*/[![:d-d]", "a/b", FNM_PATHNAME));
88+
zassert_ok(fnmatch("*[![:digit:]]*/[[:d-d]", "a/[", FNM_PATHNAME));
7989
zassert_not_equal(fnmatch("*[![:digit:]]*/[![:d-d]", "a/[", FNM_PATHNAME), 0);
8090
zassert_ok(fnmatch("a?b", "a.b", FNM_PATHNAME | FNM_PERIOD));
8191
zassert_ok(fnmatch("a*b", "a.b", FNM_PATHNAME | FNM_PERIOD));

0 commit comments

Comments
 (0)