xref: /freebsd/contrib/netbsd-tests/lib/libc/gen/t_fnmatch.c (revision d370fd1cd8fc69e87dc63f4f4a82e5a8b4956c93)
1*cdebaff8SEnji Cooper /* $NetBSD: t_fnmatch.c,v 1.7 2016/10/31 05:08:53 dholland Exp $ */
257718be8SEnji Cooper 
357718be8SEnji Cooper /*-
457718be8SEnji Cooper  * Copyright (c) 2012 The NetBSD Foundation, Inc.
557718be8SEnji Cooper  * All rights reserved.
657718be8SEnji Cooper  *
757718be8SEnji Cooper  * This code is derived from software contributed to The NetBSD Foundation
857718be8SEnji Cooper  * by Jukka Ruohonen.
957718be8SEnji Cooper  *
1057718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
1157718be8SEnji Cooper  * modification, are permitted provided that the following conditions
1257718be8SEnji Cooper  * are met:
1357718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
1457718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
1557718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
1657718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
1757718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
1857718be8SEnji Cooper  *
1957718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2057718be8SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2157718be8SEnji Cooper  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2257718be8SEnji Cooper  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2357718be8SEnji Cooper  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2457718be8SEnji Cooper  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2557718be8SEnji Cooper  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2657718be8SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2757718be8SEnji Cooper  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2857718be8SEnji Cooper  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2957718be8SEnji Cooper  * POSSIBILITY OF SUCH DAMAGE.
3057718be8SEnji Cooper  */
3157718be8SEnji Cooper #include <sys/cdefs.h>
32*cdebaff8SEnji Cooper __RCSID("$NetBSD: t_fnmatch.c,v 1.7 2016/10/31 05:08:53 dholland Exp $");
3357718be8SEnji Cooper 
3457718be8SEnji Cooper #include <atf-c.h>
3557718be8SEnji Cooper #include <fnmatch.h>
3657718be8SEnji Cooper #include <stdio.h>
3757718be8SEnji Cooper 
3857718be8SEnji Cooper ATF_TC(fnmatch_backslashes);
ATF_TC_HEAD(fnmatch_backslashes,tc)3957718be8SEnji Cooper ATF_TC_HEAD(fnmatch_backslashes, tc)
4057718be8SEnji Cooper {
4157718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
4257718be8SEnji Cooper 	    "Test translation of '\\' with fnmatch(3) (PR lib/41558)");
4357718be8SEnji Cooper }
4457718be8SEnji Cooper 
ATF_TC_BODY(fnmatch_backslashes,tc)4557718be8SEnji Cooper ATF_TC_BODY(fnmatch_backslashes, tc)
4657718be8SEnji Cooper {
4757718be8SEnji Cooper 	const int rv = fnmatch(/* pattern */ "\\", "\\", 0);
4857718be8SEnji Cooper 
4957718be8SEnji Cooper 	if (rv != FNM_NOMATCH)
5057718be8SEnji Cooper 		atf_tc_fail("fnmatch(3) did not translate '\\'");
5157718be8SEnji Cooper }
5257718be8SEnji Cooper 
5357718be8SEnji Cooper ATF_TC(fnmatch_casefold);
ATF_TC_HEAD(fnmatch_casefold,tc)5457718be8SEnji Cooper ATF_TC_HEAD(fnmatch_casefold, tc)
5557718be8SEnji Cooper {
5657718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test FNM_CASEFOLD");
5757718be8SEnji Cooper }
5857718be8SEnji Cooper 
ATF_TC_BODY(fnmatch_casefold,tc)5957718be8SEnji Cooper ATF_TC_BODY(fnmatch_casefold, tc)
6057718be8SEnji Cooper {
6157718be8SEnji Cooper 	ATF_CHECK(fnmatch("xxx", "XXX", 0) != 0);
6257718be8SEnji Cooper 	ATF_CHECK(fnmatch("XXX", "xxx", 0) != 0);
6357718be8SEnji Cooper 	ATF_CHECK(fnmatch("xxx", "XxX", 0) != 0);
6457718be8SEnji Cooper 	ATF_CHECK(fnmatch("XxX", "xxx", 0) != 0);
6557718be8SEnji Cooper 	ATF_CHECK(fnmatch("x*x", "XXX", 0) != 0);
6657718be8SEnji Cooper 	ATF_CHECK(fnmatch("**x", "XXX", 0) != 0);
6757718be8SEnji Cooper 	ATF_CHECK(fnmatch("*?x", "XXX", 0) != 0);
6857718be8SEnji Cooper 
6957718be8SEnji Cooper 	ATF_CHECK(fnmatch("xxx", "XXX", FNM_CASEFOLD) == 0);
7057718be8SEnji Cooper 	ATF_CHECK(fnmatch("XXX", "xxx", FNM_CASEFOLD) == 0);
7157718be8SEnji Cooper 	ATF_CHECK(fnmatch("xxx", "XxX", FNM_CASEFOLD) == 0);
7257718be8SEnji Cooper 	ATF_CHECK(fnmatch("XxX", "xxx", FNM_CASEFOLD) == 0);
7357718be8SEnji Cooper 	ATF_CHECK(fnmatch("x*x", "XXX", FNM_CASEFOLD) == 0);
7457718be8SEnji Cooper 	ATF_CHECK(fnmatch("**x", "XXX", FNM_CASEFOLD) == 0);
7557718be8SEnji Cooper 	ATF_CHECK(fnmatch("*?x", "XXX", FNM_CASEFOLD) == 0);
7657718be8SEnji Cooper }
7757718be8SEnji Cooper 
7857718be8SEnji Cooper ATF_TC(fnmatch_leadingdir);
ATF_TC_HEAD(fnmatch_leadingdir,tc)7957718be8SEnji Cooper ATF_TC_HEAD(fnmatch_leadingdir, tc)
8057718be8SEnji Cooper {
8157718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test FNM_LEADING_DIR");
8257718be8SEnji Cooper }
8357718be8SEnji Cooper 
ATF_TC_BODY(fnmatch_leadingdir,tc)8457718be8SEnji Cooper ATF_TC_BODY(fnmatch_leadingdir, tc)
8557718be8SEnji Cooper {
8657718be8SEnji Cooper 	ATF_CHECK(fnmatch("", "/*", 0) != 0);
8757718be8SEnji Cooper 	ATF_CHECK(fnmatch(" ", " /*", 0) != 0);
8857718be8SEnji Cooper 	ATF_CHECK(fnmatch("x", "x/*", 0) != 0);
8957718be8SEnji Cooper 	ATF_CHECK(fnmatch("///", "////*", 0) != 0);
9057718be8SEnji Cooper 
9157718be8SEnji Cooper 	ATF_CHECK(fnmatch("", "/*", FNM_LEADING_DIR) == 0);
9257718be8SEnji Cooper 	ATF_CHECK(fnmatch(" ", " /*", FNM_LEADING_DIR) == 0);
9357718be8SEnji Cooper 	ATF_CHECK(fnmatch("x", "x/*", FNM_LEADING_DIR) == 0);
9457718be8SEnji Cooper 	ATF_CHECK(fnmatch("///", "////*", FNM_LEADING_DIR) == 0);
9557718be8SEnji Cooper }
9657718be8SEnji Cooper 
9757718be8SEnji Cooper ATF_TC(fnmatch_noescape);
ATF_TC_HEAD(fnmatch_noescape,tc)9857718be8SEnji Cooper ATF_TC_HEAD(fnmatch_noescape, tc)
9957718be8SEnji Cooper {
10057718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test FNM_NOESCAPE");
10157718be8SEnji Cooper }
10257718be8SEnji Cooper 
ATF_TC_BODY(fnmatch_noescape,tc)10357718be8SEnji Cooper ATF_TC_BODY(fnmatch_noescape, tc)
10457718be8SEnji Cooper {
10557718be8SEnji Cooper 	ATF_CHECK(fnmatch("  \\x", "  \\x", 0) != 0);
10657718be8SEnji Cooper 	ATF_CHECK(fnmatch("xx\\x", "xx\\x", 0) != 0);
10757718be8SEnji Cooper 	ATF_CHECK(fnmatch("\\xxx", "\\xxx", 0) != 0);
10857718be8SEnji Cooper 
10957718be8SEnji Cooper 	ATF_CHECK(fnmatch("  \\x", "  \\x", FNM_NOESCAPE) == 0);
11057718be8SEnji Cooper 	ATF_CHECK(fnmatch("xx\\x", "xx\\x", FNM_NOESCAPE) == 0);
11157718be8SEnji Cooper 	ATF_CHECK(fnmatch("\\xxx", "\\xxx", FNM_NOESCAPE) == 0);
11257718be8SEnji Cooper }
11357718be8SEnji Cooper 
11457718be8SEnji Cooper ATF_TC(fnmatch_pathname);
ATF_TC_HEAD(fnmatch_pathname,tc)11557718be8SEnji Cooper ATF_TC_HEAD(fnmatch_pathname, tc)
11657718be8SEnji Cooper {
11757718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test FNM_PATHNAME");
11857718be8SEnji Cooper }
11957718be8SEnji Cooper 
ATF_TC_BODY(fnmatch_pathname,tc)12057718be8SEnji Cooper ATF_TC_BODY(fnmatch_pathname, tc)
12157718be8SEnji Cooper {
12257718be8SEnji Cooper 	ATF_CHECK(fnmatch("???x", "xxx/x", FNM_PATHNAME) != 0);
12357718be8SEnji Cooper 	ATF_CHECK(fnmatch("***x", "xxx/x", FNM_PATHNAME) != 0);
12457718be8SEnji Cooper 
12557718be8SEnji Cooper 	ATF_CHECK(fnmatch("???x", "xxxx", FNM_PATHNAME) == 0);
12657718be8SEnji Cooper 	ATF_CHECK(fnmatch("*/xxx", "/xxx", FNM_PATHNAME) == 0);
12757718be8SEnji Cooper 	ATF_CHECK(fnmatch("x/*.y", "x/z.y", FNM_PATHNAME) == 0);
12857718be8SEnji Cooper }
12957718be8SEnji Cooper 
13057718be8SEnji Cooper ATF_TC(fnmatch_period);
ATF_TC_HEAD(fnmatch_period,tc)13157718be8SEnji Cooper ATF_TC_HEAD(fnmatch_period, tc)
13257718be8SEnji Cooper {
13357718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test FNM_PERIOD");
13457718be8SEnji Cooper }
13557718be8SEnji Cooper 
ATF_TC_BODY(fnmatch_period,tc)13657718be8SEnji Cooper ATF_TC_BODY(fnmatch_period, tc)
13757718be8SEnji Cooper {
13857718be8SEnji Cooper 	ATF_CHECK(fnmatch("*x*", "X", FNM_PERIOD) != 0);
13957718be8SEnji Cooper 	ATF_CHECK(fnmatch("*x*", "X", FNM_PERIOD | FNM_CASEFOLD) == 0);
14057718be8SEnji Cooper 
14157718be8SEnji Cooper 	ATF_CHECK(fnmatch("x?y", "x.y", FNM_PATHNAME | FNM_PERIOD) == 0);
14257718be8SEnji Cooper 	ATF_CHECK(fnmatch("x*y", "x.y", FNM_PATHNAME | FNM_PERIOD) == 0);
14357718be8SEnji Cooper 	ATF_CHECK(fnmatch("*.c", "x.c", FNM_PATHNAME | FNM_PERIOD) == 0);
14457718be8SEnji Cooper 	ATF_CHECK(fnmatch("*/?", "x/y", FNM_PATHNAME | FNM_PERIOD) == 0);
14557718be8SEnji Cooper 	ATF_CHECK(fnmatch("*/*", "x/y", FNM_PATHNAME | FNM_PERIOD) == 0);
14657718be8SEnji Cooper 	ATF_CHECK(fnmatch(".*/?", ".x/y", FNM_PATHNAME | FNM_PERIOD) == 0);
14757718be8SEnji Cooper 	ATF_CHECK(fnmatch("*/.?", "x/.y", FNM_PATHNAME | FNM_PERIOD) == 0);
14857718be8SEnji Cooper 	ATF_CHECK(fnmatch("x[.]y", "x.y", FNM_PATHNAME | FNM_PERIOD) == 0);
14957718be8SEnji Cooper 
15057718be8SEnji Cooper 	ATF_CHECK(fnmatch("?x/y", ".x/y", FNM_PATHNAME | FNM_PERIOD) != 0);
15157718be8SEnji Cooper 	ATF_CHECK(fnmatch("*x/y", ".x/y", FNM_PATHNAME | FNM_PERIOD) != 0);
15257718be8SEnji Cooper 	ATF_CHECK(fnmatch("x/?y", "x/.y", FNM_PATHNAME | FNM_PERIOD) != 0);
15357718be8SEnji Cooper 	ATF_CHECK(fnmatch("x/*y", "x/.y", FNM_PATHNAME | FNM_PERIOD) != 0);
15457718be8SEnji Cooper }
15557718be8SEnji Cooper 
156640235e2SEnji Cooper ATF_TC(fnmatch_initialbracket);
ATF_TC_HEAD(fnmatch_initialbracket,tc)157640235e2SEnji Cooper ATF_TC_HEAD(fnmatch_initialbracket, tc)
158640235e2SEnji Cooper {
159640235e2SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test fnmatch with initial [");
160640235e2SEnji Cooper }
161640235e2SEnji Cooper 
ATF_TC_BODY(fnmatch_initialbracket,tc)162640235e2SEnji Cooper ATF_TC_BODY(fnmatch_initialbracket, tc)
163640235e2SEnji Cooper {
164640235e2SEnji Cooper 	ATF_CHECK(fnmatch("[[?*\\\\]", "\\", 0) == 0);
165640235e2SEnji Cooper 	ATF_CHECK(fnmatch("[]?*\\\\]", "]", 0) == 0);
166640235e2SEnji Cooper 	ATF_CHECK(fnmatch("[!]a-]", "b", 0) == 0);
167640235e2SEnji Cooper 	ATF_CHECK(fnmatch("[]-_]", "^", 0) == 0); /* range: ']', '^', '_' */
168640235e2SEnji Cooper 	ATF_CHECK(fnmatch("[!]-_]", "X", 0) == 0);
169*cdebaff8SEnji Cooper 	ATF_CHECK(fnmatch("[A-\\\\]", "[", 0) == 0);
170640235e2SEnji Cooper 	ATF_CHECK(fnmatch("[a-z]/[a-z]", "a/b", 0) == 0);
171640235e2SEnji Cooper 	ATF_CHECK(fnmatch("[*]/b", "*/b", 0) == 0);
172640235e2SEnji Cooper 	ATF_CHECK(fnmatch("[?]/b", "?/b", 0) == 0);
173640235e2SEnji Cooper 	ATF_CHECK(fnmatch("[[a]/b", "a/b", 0) == 0);
174640235e2SEnji Cooper 	ATF_CHECK(fnmatch("[[a]/b", "[/b", 0) == 0);
175640235e2SEnji Cooper 	ATF_CHECK(fnmatch("[/b", "[/b", 0) == 0);
176640235e2SEnji Cooper 
177640235e2SEnji Cooper 	ATF_CHECK(fnmatch("[*]/b", "a/b", 0) != 0);
178640235e2SEnji Cooper 	ATF_CHECK(fnmatch("[?]/b", "a/b", 0) != 0);
179640235e2SEnji Cooper }
180640235e2SEnji Cooper 
ATF_TP_ADD_TCS(tp)18157718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
18257718be8SEnji Cooper {
18357718be8SEnji Cooper 
18457718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, fnmatch_backslashes);
18557718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, fnmatch_casefold);
18657718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, fnmatch_leadingdir);
18757718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, fnmatch_noescape);
18857718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, fnmatch_pathname);
18957718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, fnmatch_period);
190640235e2SEnji Cooper 	ATF_TP_ADD_TC(tp, fnmatch_initialbracket);
19157718be8SEnji Cooper 
19257718be8SEnji Cooper 	return atf_no_error();
19357718be8SEnji Cooper }
194