xref: /minix3/tests/lib/libc/gen/t_fnmatch.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_fnmatch.c,v 1.6 2014/10/12 22:33:41 christos Exp $ */
211be35a1SLionel Sambuc 
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc  * Copyright (c) 2012 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc  * All rights reserved.
611be35a1SLionel Sambuc  *
711be35a1SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
811be35a1SLionel Sambuc  * by Jukka Ruohonen.
911be35a1SLionel Sambuc  *
1011be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
1111be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
1211be35a1SLionel Sambuc  * are met:
1311be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1411be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1511be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1611be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1711be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1811be35a1SLionel Sambuc  *
1911be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2011be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2111be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2211be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2311be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2411be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2511be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2611be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2711be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2811be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2911be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
3011be35a1SLionel Sambuc  */
3111be35a1SLionel Sambuc #include <sys/cdefs.h>
32*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_fnmatch.c,v 1.6 2014/10/12 22:33:41 christos Exp $");
3311be35a1SLionel Sambuc 
3411be35a1SLionel Sambuc #include <atf-c.h>
3511be35a1SLionel Sambuc #include <fnmatch.h>
3611be35a1SLionel Sambuc #include <stdio.h>
3711be35a1SLionel Sambuc 
3811be35a1SLionel Sambuc ATF_TC(fnmatch_backslashes);
ATF_TC_HEAD(fnmatch_backslashes,tc)3911be35a1SLionel Sambuc ATF_TC_HEAD(fnmatch_backslashes, tc)
4011be35a1SLionel Sambuc {
4111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
4211be35a1SLionel Sambuc 	    "Test translation of '\\' with fnmatch(3) (PR lib/41558)");
4311be35a1SLionel Sambuc }
4411be35a1SLionel Sambuc 
ATF_TC_BODY(fnmatch_backslashes,tc)4511be35a1SLionel Sambuc ATF_TC_BODY(fnmatch_backslashes, tc)
4611be35a1SLionel Sambuc {
4711be35a1SLionel Sambuc 	const int rv = fnmatch(/* pattern */ "\\", "\\", 0);
4811be35a1SLionel Sambuc 
4911be35a1SLionel Sambuc 	if (rv != FNM_NOMATCH)
5011be35a1SLionel Sambuc 		atf_tc_fail("fnmatch(3) did not translate '\\'");
5111be35a1SLionel Sambuc }
5211be35a1SLionel Sambuc 
5311be35a1SLionel Sambuc ATF_TC(fnmatch_casefold);
ATF_TC_HEAD(fnmatch_casefold,tc)5411be35a1SLionel Sambuc ATF_TC_HEAD(fnmatch_casefold, tc)
5511be35a1SLionel Sambuc {
5611be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test FNM_CASEFOLD");
5711be35a1SLionel Sambuc }
5811be35a1SLionel Sambuc 
ATF_TC_BODY(fnmatch_casefold,tc)5911be35a1SLionel Sambuc ATF_TC_BODY(fnmatch_casefold, tc)
6011be35a1SLionel Sambuc {
6111be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("xxx", "XXX", 0) != 0);
6211be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("XXX", "xxx", 0) != 0);
6311be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("xxx", "XxX", 0) != 0);
6411be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("XxX", "xxx", 0) != 0);
6511be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("x*x", "XXX", 0) != 0);
6611be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("**x", "XXX", 0) != 0);
6711be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("*?x", "XXX", 0) != 0);
6811be35a1SLionel Sambuc 
6911be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("xxx", "XXX", FNM_CASEFOLD) == 0);
7011be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("XXX", "xxx", FNM_CASEFOLD) == 0);
7111be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("xxx", "XxX", FNM_CASEFOLD) == 0);
7211be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("XxX", "xxx", FNM_CASEFOLD) == 0);
7311be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("x*x", "XXX", FNM_CASEFOLD) == 0);
7411be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("**x", "XXX", FNM_CASEFOLD) == 0);
7511be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("*?x", "XXX", FNM_CASEFOLD) == 0);
7611be35a1SLionel Sambuc }
7711be35a1SLionel Sambuc 
7811be35a1SLionel Sambuc ATF_TC(fnmatch_leadingdir);
ATF_TC_HEAD(fnmatch_leadingdir,tc)7911be35a1SLionel Sambuc ATF_TC_HEAD(fnmatch_leadingdir, tc)
8011be35a1SLionel Sambuc {
8111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test FNM_LEADING_DIR");
8211be35a1SLionel Sambuc }
8311be35a1SLionel Sambuc 
ATF_TC_BODY(fnmatch_leadingdir,tc)8411be35a1SLionel Sambuc ATF_TC_BODY(fnmatch_leadingdir, tc)
8511be35a1SLionel Sambuc {
8611be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("", "/*", 0) != 0);
8711be35a1SLionel Sambuc 	ATF_CHECK(fnmatch(" ", " /*", 0) != 0);
8811be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("x", "x/*", 0) != 0);
8911be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("///", "////*", 0) != 0);
9011be35a1SLionel Sambuc 
9111be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("", "/*", FNM_LEADING_DIR) == 0);
9211be35a1SLionel Sambuc 	ATF_CHECK(fnmatch(" ", " /*", FNM_LEADING_DIR) == 0);
9311be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("x", "x/*", FNM_LEADING_DIR) == 0);
9411be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("///", "////*", FNM_LEADING_DIR) == 0);
9511be35a1SLionel Sambuc }
9611be35a1SLionel Sambuc 
9711be35a1SLionel Sambuc ATF_TC(fnmatch_noescape);
ATF_TC_HEAD(fnmatch_noescape,tc)9811be35a1SLionel Sambuc ATF_TC_HEAD(fnmatch_noescape, tc)
9911be35a1SLionel Sambuc {
10011be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test FNM_NOESCAPE");
10111be35a1SLionel Sambuc }
10211be35a1SLionel Sambuc 
ATF_TC_BODY(fnmatch_noescape,tc)10311be35a1SLionel Sambuc ATF_TC_BODY(fnmatch_noescape, tc)
10411be35a1SLionel Sambuc {
10511be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("  \\x", "  \\x", 0) != 0);
10611be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("xx\\x", "xx\\x", 0) != 0);
10711be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("\\xxx", "\\xxx", 0) != 0);
10811be35a1SLionel Sambuc 
10911be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("  \\x", "  \\x", FNM_NOESCAPE) == 0);
11011be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("xx\\x", "xx\\x", FNM_NOESCAPE) == 0);
11111be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("\\xxx", "\\xxx", FNM_NOESCAPE) == 0);
11211be35a1SLionel Sambuc }
11311be35a1SLionel Sambuc 
11411be35a1SLionel Sambuc ATF_TC(fnmatch_pathname);
ATF_TC_HEAD(fnmatch_pathname,tc)11511be35a1SLionel Sambuc ATF_TC_HEAD(fnmatch_pathname, tc)
11611be35a1SLionel Sambuc {
11711be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test FNM_PATHNAME");
11811be35a1SLionel Sambuc }
11911be35a1SLionel Sambuc 
ATF_TC_BODY(fnmatch_pathname,tc)12011be35a1SLionel Sambuc ATF_TC_BODY(fnmatch_pathname, tc)
12111be35a1SLionel Sambuc {
12211be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("???x", "xxx/x", FNM_PATHNAME) != 0);
12311be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("***x", "xxx/x", FNM_PATHNAME) != 0);
12411be35a1SLionel Sambuc 
12511be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("???x", "xxxx", FNM_PATHNAME) == 0);
12611be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("*/xxx", "/xxx", FNM_PATHNAME) == 0);
12711be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("x/*.y", "x/z.y", FNM_PATHNAME) == 0);
12811be35a1SLionel Sambuc }
12911be35a1SLionel Sambuc 
13011be35a1SLionel Sambuc ATF_TC(fnmatch_period);
ATF_TC_HEAD(fnmatch_period,tc)13111be35a1SLionel Sambuc ATF_TC_HEAD(fnmatch_period, tc)
13211be35a1SLionel Sambuc {
13311be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test FNM_PERIOD");
13411be35a1SLionel Sambuc }
13511be35a1SLionel Sambuc 
ATF_TC_BODY(fnmatch_period,tc)13611be35a1SLionel Sambuc ATF_TC_BODY(fnmatch_period, tc)
13711be35a1SLionel Sambuc {
13811be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("*x*", "X", FNM_PERIOD) != 0);
13911be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("*x*", "X", FNM_PERIOD | FNM_CASEFOLD) == 0);
14011be35a1SLionel Sambuc 
14111be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("x?y", "x.y", FNM_PATHNAME | FNM_PERIOD) == 0);
14211be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("x*y", "x.y", FNM_PATHNAME | FNM_PERIOD) == 0);
14311be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("*.c", "x.c", FNM_PATHNAME | FNM_PERIOD) == 0);
14411be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("*/?", "x/y", FNM_PATHNAME | FNM_PERIOD) == 0);
14511be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("*/*", "x/y", FNM_PATHNAME | FNM_PERIOD) == 0);
14611be35a1SLionel Sambuc 	ATF_CHECK(fnmatch(".*/?", ".x/y", FNM_PATHNAME | FNM_PERIOD) == 0);
14711be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("*/.?", "x/.y", FNM_PATHNAME | FNM_PERIOD) == 0);
14811be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("x[.]y", "x.y", FNM_PATHNAME | FNM_PERIOD) == 0);
14911be35a1SLionel Sambuc 
15011be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("?x/y", ".x/y", FNM_PATHNAME | FNM_PERIOD) != 0);
15111be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("*x/y", ".x/y", FNM_PATHNAME | FNM_PERIOD) != 0);
15211be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("x/?y", "x/.y", FNM_PATHNAME | FNM_PERIOD) != 0);
15311be35a1SLionel Sambuc 	ATF_CHECK(fnmatch("x/*y", "x/.y", FNM_PATHNAME | FNM_PERIOD) != 0);
15411be35a1SLionel Sambuc }
15511be35a1SLionel Sambuc 
156*0a6a1f1dSLionel Sambuc ATF_TC(fnmatch_initialbracket);
ATF_TC_HEAD(fnmatch_initialbracket,tc)157*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(fnmatch_initialbracket, tc)
158*0a6a1f1dSLionel Sambuc {
159*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test fnmatch with initial [");
160*0a6a1f1dSLionel Sambuc }
161*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(fnmatch_initialbracket,tc)162*0a6a1f1dSLionel Sambuc ATF_TC_BODY(fnmatch_initialbracket, tc)
163*0a6a1f1dSLionel Sambuc {
164*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fnmatch("[[?*\\\\]", "\\", 0) == 0);
165*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fnmatch("[]?*\\\\]", "]", 0) == 0);
166*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fnmatch("[!]a-]", "b", 0) == 0);
167*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fnmatch("[]-_]", "^", 0) == 0); /* range: ']', '^', '_' */
168*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fnmatch("[!]-_]", "X", 0) == 0);
169*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fnmatch("[a-z]/[a-z]", "a/b", 0) == 0);
170*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fnmatch("[*]/b", "*/b", 0) == 0);
171*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fnmatch("[?]/b", "?/b", 0) == 0);
172*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fnmatch("[[a]/b", "a/b", 0) == 0);
173*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fnmatch("[[a]/b", "[/b", 0) == 0);
174*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fnmatch("[/b", "[/b", 0) == 0);
175*0a6a1f1dSLionel Sambuc 
176*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fnmatch("[*]/b", "a/b", 0) != 0);
177*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fnmatch("[?]/b", "a/b", 0) != 0);
178*0a6a1f1dSLionel Sambuc }
179*0a6a1f1dSLionel Sambuc 
ATF_TP_ADD_TCS(tp)18011be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
18111be35a1SLionel Sambuc {
18211be35a1SLionel Sambuc 
18311be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, fnmatch_backslashes);
18411be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, fnmatch_casefold);
18511be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, fnmatch_leadingdir);
18611be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, fnmatch_noescape);
18711be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, fnmatch_pathname);
18811be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, fnmatch_period);
189*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, fnmatch_initialbracket);
19011be35a1SLionel Sambuc 
19111be35a1SLionel Sambuc 	return atf_no_error();
19211be35a1SLionel Sambuc }
193