From 93e67fb73ff7e06c97589c45072be3011f3ecfca Mon Sep 17 00:00:00 2001 From: Chris Friedt Date: Sat, 25 Feb 2023 08:15:08 -0500 Subject: [PATCH] tests: posix: add coverage for fnmatch Previously, the `fnmatch()` function was available in `lib/util` but it did not have any test coverage. After moving it to `lib/posix`, add test coverage to the POSIX testsuite. Signed-off-by: Chris Friedt --- tests/posix/common/src/fnmatch.c | 83 ++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 tests/posix/common/src/fnmatch.c diff --git a/tests/posix/common/src/fnmatch.c b/tests/posix/common/src/fnmatch.c new file mode 100644 index 0000000000..29c6587108 --- /dev/null +++ b/tests/posix/common/src/fnmatch.c @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2023 Meta + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +/* + * Adapted from + * https://git.musl-libc.org/cgit/libc-testsuite/tree/fnmatch.c + */ +ZTEST(posix_apis, fnmatch) +{ + /* Note: commented out lines indicate known problems to be addressed in #55186 */ + + zassert_ok(fnmatch("*.c", "foo.c", 0)); + zassert_ok(fnmatch("*.c", ".c", 0)); + zassert_equal(fnmatch("*.a", "foo.c", 0), FNM_NOMATCH); + zassert_ok(fnmatch("*.c", ".foo.c", 0)); + zassert_equal(fnmatch("*.c", ".foo.c", FNM_PERIOD), FNM_NOMATCH); + zassert_ok(fnmatch("*.c", "foo.c", FNM_PERIOD)); + zassert_equal(fnmatch("a\\*.c", "a*.c", FNM_NOESCAPE), FNM_NOMATCH); + zassert_equal(fnmatch("a\\*.c", "ax.c", 0), FNM_NOMATCH); + zassert_ok(fnmatch("a[xy].c", "ax.c", 0)); + zassert_ok(fnmatch("a[!y].c", "ax.c", 0)); + zassert_equal(fnmatch("a[a/z]*.c", "a/x.c", FNM_PATHNAME), FNM_NOMATCH); + zassert_ok(fnmatch("a/*.c", "a/x.c", FNM_PATHNAME)); + zassert_equal(fnmatch("a*.c", "a/x.c", FNM_PATHNAME), FNM_NOMATCH); + zassert_ok(fnmatch("*/foo", "/foo", FNM_PATHNAME)); + zassert_ok(fnmatch("-O[01]", "-O1", 0)); + /* zassert_ok(fnmatch("[[?*\\]", "\\", 0)); */ + /* zassert_ok(fnmatch("[]?*\\]", "]", 0)); */ + zassert_ok(fnmatch("[!]a-]", "b", 0)); + zassert_ok(fnmatch("[]-_]", "^", 0)); + zassert_ok(fnmatch("[!]-_]", "X", 0)); + zassert_equal(fnmatch("??", "-", 0), FNM_NOMATCH); + zassert_equal(fnmatch("*LIB*", "lib", FNM_PERIOD), FNM_NOMATCH); + /* zassert_ok(fnmatch("a[/]b", "a/b", 0)); */ + zassert_equal(fnmatch("a[/]b", "a/b", FNM_PATHNAME), FNM_NOMATCH); + zassert_ok(fnmatch("[a-z]/[a-z]", "a/b", 0)); + zassert_equal(fnmatch("*", "a/b", FNM_PATHNAME), FNM_NOMATCH); + zassert_equal(fnmatch("*[/]b", "a/b", FNM_PATHNAME), FNM_NOMATCH); + zassert_equal(fnmatch("*[b]", "a/b", FNM_PATHNAME), FNM_NOMATCH); + zassert_equal(fnmatch("[*]/b", "a/b", 0), FNM_NOMATCH); + zassert_ok(fnmatch("[*]/b", "*/b", 0)); + zassert_equal(fnmatch("[?]/b", "a/b", 0), FNM_NOMATCH); + zassert_ok(fnmatch("[?]/b", "?/b", 0)); + zassert_ok(fnmatch("[[a]/b", "a/b", 0)); + zassert_ok(fnmatch("[[a]/b", "[/b", 0)); + zassert_equal(fnmatch("\\*/b", "a/b", 0), FNM_NOMATCH); + zassert_ok(fnmatch("\\*/b", "*/b", 0)); + zassert_equal(fnmatch("\\?/b", "a/b", 0), FNM_NOMATCH); + zassert_ok(fnmatch("\\?/b", "?/b", 0)); + zassert_ok(fnmatch("[/b", "[/b", 0)); + zassert_ok(fnmatch("\\[/b", "[/b", 0)); + zassert_ok(fnmatch("??" + "/b", + "aa/b", 0)); + zassert_ok(fnmatch("???b", "aa/b", 0)); + zassert_equal(fnmatch("???b", "aa/b", FNM_PATHNAME), FNM_NOMATCH); + zassert_equal(fnmatch("?a/b", ".a/b", FNM_PATHNAME | FNM_PERIOD), FNM_NOMATCH); + zassert_equal(fnmatch("a/?b", "a/.b", FNM_PATHNAME | FNM_PERIOD), FNM_NOMATCH); + zassert_equal(fnmatch("*a/b", ".a/b", FNM_PATHNAME | FNM_PERIOD), FNM_NOMATCH); + zassert_equal(fnmatch("a/*b", "a/.b", FNM_PATHNAME | FNM_PERIOD), FNM_NOMATCH); + /* zassert_equal(fnmatch("[.]a/b", ".a/b", FNM_PATHNAME | FNM_PERIOD), FNM_NOMATCH); */ + /* zassert_equal(fnmatch("a/[.]b", "a/.b", FNM_PATHNAME | FNM_PERIOD), FNM_NOMATCH); */ + zassert_ok(fnmatch("*/?", "a/b", FNM_PATHNAME | FNM_PERIOD)); + zassert_ok(fnmatch("?/*", "a/b", FNM_PATHNAME | FNM_PERIOD)); + zassert_ok(fnmatch(".*/?", ".a/b", FNM_PATHNAME | FNM_PERIOD)); + zassert_ok(fnmatch("*/.?", "a/.b", FNM_PATHNAME | FNM_PERIOD)); + zassert_equal(fnmatch("*/*", "a/.b", FNM_PATHNAME | FNM_PERIOD), FNM_NOMATCH); + zassert_ok(fnmatch("*?*/*", "a/.b", FNM_PERIOD)); + zassert_ok(fnmatch("*[.]/b", "a./b", FNM_PATHNAME | FNM_PERIOD)); + /* zassert_ok(fnmatch("*[[:alpha:]]/""*[[:alnum:]]", "a/b", FNM_PATHNAME)); */ + zassert_not_equal(fnmatch("*[![:digit:]]*/[![:d-d]", "a/b", FNM_PATHNAME), 0); + zassert_not_equal(fnmatch("*[![:digit:]]*/[[:d-d]", "a/[", FNM_PATHNAME), 0); + zassert_not_equal(fnmatch("*[![:digit:]]*/[![:d-d]", "a/[", FNM_PATHNAME), 0); + zassert_ok(fnmatch("a?b", "a.b", FNM_PATHNAME | FNM_PERIOD)); + zassert_ok(fnmatch("a*b", "a.b", FNM_PATHNAME | FNM_PERIOD)); + zassert_ok(fnmatch("a[.]b", "a.b", FNM_PATHNAME | FNM_PERIOD)); +}