1 /*
2  * Copyright (c) 2023 Meta
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/posix/fnmatch.h>
8 #include <zephyr/ztest.h>
9 
10 /*
11  * Adapted from
12  * https://git.musl-libc.org/cgit/libc-testsuite/tree/fnmatch.c
13  */
ZTEST(posix_c_lib_ext,test_fnmatch)14 ZTEST(posix_c_lib_ext, test_fnmatch)
15 {
16 	/* Note: commented out lines indicate known problems to be addressed in #55186 */
17 
18 	zassert_ok(fnmatch("*.c", "foo.c", 0));
19 	zassert_ok(fnmatch("*.c", ".c", 0));
20 	zassert_equal(fnmatch("*.a", "foo.c", 0), FNM_NOMATCH);
21 	zassert_ok(fnmatch("*.c", ".foo.c", 0));
22 	zassert_equal(fnmatch("*.c", ".foo.c", FNM_PERIOD), FNM_NOMATCH);
23 	zassert_ok(fnmatch("*.c", "foo.c", FNM_PERIOD));
24 	zassert_equal(fnmatch("a\\*.c", "a*.c", FNM_NOESCAPE), FNM_NOMATCH);
25 	zassert_equal(fnmatch("a\\*.c", "ax.c", 0), FNM_NOMATCH);
26 	zassert_ok(fnmatch("a[xy].c", "ax.c", 0));
27 	zassert_ok(fnmatch("a[!y].c", "ax.c", 0));
28 	zassert_equal(fnmatch("a[a/z]*.c", "a/x.c", FNM_PATHNAME), FNM_NOMATCH);
29 	zassert_ok(fnmatch("a/*.c", "a/x.c", FNM_PATHNAME));
30 	zassert_equal(fnmatch("a*.c", "a/x.c", FNM_PATHNAME), FNM_NOMATCH);
31 	zassert_ok(fnmatch("*/foo", "/foo", FNM_PATHNAME));
32 	zassert_ok(fnmatch("-O[01]", "-O1", 0));
33 	/* zassert_ok(fnmatch("[[?*\\]", "\\", 0)); */
34 	/* zassert_ok(fnmatch("[]?*\\]", "]", 0)); */
35 	zassert_ok(fnmatch("[!]a-]", "b", 0));
36 	zassert_ok(fnmatch("[]-_]", "^", 0));
37 	zassert_ok(fnmatch("[!]-_]", "X", 0));
38 	zassert_equal(fnmatch("??", "-", 0), FNM_NOMATCH);
39 	zassert_equal(fnmatch("*LIB*", "lib", FNM_PERIOD), FNM_NOMATCH);
40 	/* zassert_ok(fnmatch("a[/]b", "a/b", 0)); */
41 	zassert_equal(fnmatch("a[/]b", "a/b", FNM_PATHNAME), FNM_NOMATCH);
42 	zassert_ok(fnmatch("[a-z]/[a-z]", "a/b", 0));
43 	zassert_equal(fnmatch("*", "a/b", FNM_PATHNAME), FNM_NOMATCH);
44 	zassert_equal(fnmatch("*[/]b", "a/b", FNM_PATHNAME), FNM_NOMATCH);
45 	zassert_equal(fnmatch("*[b]", "a/b", FNM_PATHNAME), FNM_NOMATCH);
46 	zassert_equal(fnmatch("[*]/b", "a/b", 0), FNM_NOMATCH);
47 	zassert_ok(fnmatch("[*]/b", "*/b", 0));
48 	zassert_equal(fnmatch("[?]/b", "a/b", 0), FNM_NOMATCH);
49 	zassert_ok(fnmatch("[?]/b", "?/b", 0));
50 	zassert_ok(fnmatch("[[a]/b", "a/b", 0));
51 	zassert_ok(fnmatch("[[a]/b", "[/b", 0));
52 	zassert_equal(fnmatch("\\*/b", "a/b", 0), FNM_NOMATCH);
53 	zassert_ok(fnmatch("\\*/b", "*/b", 0));
54 	zassert_equal(fnmatch("\\?/b", "a/b", 0), FNM_NOMATCH);
55 	zassert_ok(fnmatch("\\?/b", "?/b", 0));
56 	zassert_ok(fnmatch("[/b", "[/b", 0));
57 	zassert_ok(fnmatch("\\[/b", "[/b", 0));
58 	zassert_ok(fnmatch("??"
59 			   "/b",
60 			   "aa/b", 0));
61 	zassert_ok(fnmatch("???b", "aa/b", 0));
62 	zassert_equal(fnmatch("???b", "aa/b", FNM_PATHNAME), FNM_NOMATCH);
63 	zassert_equal(fnmatch("?a/b", ".a/b", FNM_PATHNAME | FNM_PERIOD), FNM_NOMATCH);
64 	zassert_equal(fnmatch("a/?b", "a/.b", FNM_PATHNAME | FNM_PERIOD), FNM_NOMATCH);
65 	zassert_equal(fnmatch("*a/b", ".a/b", FNM_PATHNAME | FNM_PERIOD), FNM_NOMATCH);
66 	zassert_equal(fnmatch("a/*b", "a/.b", FNM_PATHNAME | FNM_PERIOD), FNM_NOMATCH);
67 	/* zassert_equal(fnmatch("[.]a/b", ".a/b", FNM_PATHNAME | FNM_PERIOD), FNM_NOMATCH); */
68 	/* zassert_equal(fnmatch("a/[.]b", "a/.b", FNM_PATHNAME | FNM_PERIOD), FNM_NOMATCH); */
69 	zassert_ok(fnmatch("*/?", "a/b", FNM_PATHNAME | FNM_PERIOD));
70 	zassert_ok(fnmatch("?/*", "a/b", FNM_PATHNAME | FNM_PERIOD));
71 	zassert_ok(fnmatch(".*/?", ".a/b", FNM_PATHNAME | FNM_PERIOD));
72 	zassert_ok(fnmatch("*/.?", "a/.b", FNM_PATHNAME | FNM_PERIOD));
73 	zassert_equal(fnmatch("*/*", "a/.b", FNM_PATHNAME | FNM_PERIOD), FNM_NOMATCH);
74 	zassert_ok(fnmatch("*?*/*", "a/.b", FNM_PERIOD));
75 	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);
79 	zassert_not_equal(fnmatch("*[![:digit:]]*/[![:d-d]", "a/[", FNM_PATHNAME), 0);
80 	zassert_ok(fnmatch("a?b", "a.b", FNM_PATHNAME | FNM_PERIOD));
81 	zassert_ok(fnmatch("a*b", "a.b", FNM_PATHNAME | FNM_PERIOD));
82 	zassert_ok(fnmatch("a[.]b", "a.b", FNM_PATHNAME | FNM_PERIOD));
83 }
84