xref: /freebsd/contrib/netbsd-tests/lib/libc/gen/t_fnmatch.c (revision 57718be8fa0bd5edc11ab9a72e68cc71982939a6)
1*57718be8SEnji Cooper /* $NetBSD: t_fnmatch.c,v 1.3 2012/04/08 09:58:59 jruoho Exp $ */
2*57718be8SEnji Cooper 
3*57718be8SEnji Cooper /*-
4*57718be8SEnji Cooper  * Copyright (c) 2012 The NetBSD Foundation, Inc.
5*57718be8SEnji Cooper  * All rights reserved.
6*57718be8SEnji Cooper  *
7*57718be8SEnji Cooper  * This code is derived from software contributed to The NetBSD Foundation
8*57718be8SEnji Cooper  * by Jukka Ruohonen.
9*57718be8SEnji Cooper  *
10*57718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
11*57718be8SEnji Cooper  * modification, are permitted provided that the following conditions
12*57718be8SEnji Cooper  * are met:
13*57718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
14*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
15*57718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
16*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
17*57718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
18*57718be8SEnji Cooper  *
19*57718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*57718be8SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*57718be8SEnji Cooper  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*57718be8SEnji Cooper  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*57718be8SEnji Cooper  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*57718be8SEnji Cooper  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*57718be8SEnji Cooper  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*57718be8SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*57718be8SEnji Cooper  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*57718be8SEnji Cooper  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*57718be8SEnji Cooper  * POSSIBILITY OF SUCH DAMAGE.
30*57718be8SEnji Cooper  */
31*57718be8SEnji Cooper #include <sys/cdefs.h>
32*57718be8SEnji Cooper __RCSID("$NetBSD: t_fnmatch.c,v 1.3 2012/04/08 09:58:59 jruoho Exp $");
33*57718be8SEnji Cooper 
34*57718be8SEnji Cooper #include <atf-c.h>
35*57718be8SEnji Cooper #include <fnmatch.h>
36*57718be8SEnji Cooper #include <stdio.h>
37*57718be8SEnji Cooper 
38*57718be8SEnji Cooper ATF_TC(fnmatch_backslashes);
39*57718be8SEnji Cooper ATF_TC_HEAD(fnmatch_backslashes, tc)
40*57718be8SEnji Cooper {
41*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
42*57718be8SEnji Cooper 	    "Test translation of '\\' with fnmatch(3) (PR lib/41558)");
43*57718be8SEnji Cooper }
44*57718be8SEnji Cooper 
45*57718be8SEnji Cooper ATF_TC_BODY(fnmatch_backslashes, tc)
46*57718be8SEnji Cooper {
47*57718be8SEnji Cooper 	const int rv = fnmatch(/* pattern */ "\\", "\\", 0);
48*57718be8SEnji Cooper 
49*57718be8SEnji Cooper 	if (rv != FNM_NOMATCH)
50*57718be8SEnji Cooper 		atf_tc_fail("fnmatch(3) did not translate '\\'");
51*57718be8SEnji Cooper }
52*57718be8SEnji Cooper 
53*57718be8SEnji Cooper ATF_TC(fnmatch_casefold);
54*57718be8SEnji Cooper ATF_TC_HEAD(fnmatch_casefold, tc)
55*57718be8SEnji Cooper {
56*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test FNM_CASEFOLD");
57*57718be8SEnji Cooper }
58*57718be8SEnji Cooper 
59*57718be8SEnji Cooper ATF_TC_BODY(fnmatch_casefold, tc)
60*57718be8SEnji Cooper {
61*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("xxx", "XXX", 0) != 0);
62*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("XXX", "xxx", 0) != 0);
63*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("xxx", "XxX", 0) != 0);
64*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("XxX", "xxx", 0) != 0);
65*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("x*x", "XXX", 0) != 0);
66*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("**x", "XXX", 0) != 0);
67*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("*?x", "XXX", 0) != 0);
68*57718be8SEnji Cooper 
69*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("xxx", "XXX", FNM_CASEFOLD) == 0);
70*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("XXX", "xxx", FNM_CASEFOLD) == 0);
71*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("xxx", "XxX", FNM_CASEFOLD) == 0);
72*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("XxX", "xxx", FNM_CASEFOLD) == 0);
73*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("x*x", "XXX", FNM_CASEFOLD) == 0);
74*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("**x", "XXX", FNM_CASEFOLD) == 0);
75*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("*?x", "XXX", FNM_CASEFOLD) == 0);
76*57718be8SEnji Cooper }
77*57718be8SEnji Cooper 
78*57718be8SEnji Cooper ATF_TC(fnmatch_leadingdir);
79*57718be8SEnji Cooper ATF_TC_HEAD(fnmatch_leadingdir, tc)
80*57718be8SEnji Cooper {
81*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test FNM_LEADING_DIR");
82*57718be8SEnji Cooper }
83*57718be8SEnji Cooper 
84*57718be8SEnji Cooper ATF_TC_BODY(fnmatch_leadingdir, tc)
85*57718be8SEnji Cooper {
86*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("", "/*", 0) != 0);
87*57718be8SEnji Cooper 	ATF_CHECK(fnmatch(" ", " /*", 0) != 0);
88*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("x", "x/*", 0) != 0);
89*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("///", "////*", 0) != 0);
90*57718be8SEnji Cooper 
91*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("", "/*", FNM_LEADING_DIR) == 0);
92*57718be8SEnji Cooper 	ATF_CHECK(fnmatch(" ", " /*", FNM_LEADING_DIR) == 0);
93*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("x", "x/*", FNM_LEADING_DIR) == 0);
94*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("///", "////*", FNM_LEADING_DIR) == 0);
95*57718be8SEnji Cooper }
96*57718be8SEnji Cooper 
97*57718be8SEnji Cooper ATF_TC(fnmatch_noescape);
98*57718be8SEnji Cooper ATF_TC_HEAD(fnmatch_noescape, tc)
99*57718be8SEnji Cooper {
100*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test FNM_NOESCAPE");
101*57718be8SEnji Cooper }
102*57718be8SEnji Cooper 
103*57718be8SEnji Cooper ATF_TC_BODY(fnmatch_noescape, tc)
104*57718be8SEnji Cooper {
105*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("  \\x", "  \\x", 0) != 0);
106*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("xx\\x", "xx\\x", 0) != 0);
107*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("\\xxx", "\\xxx", 0) != 0);
108*57718be8SEnji Cooper 
109*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("  \\x", "  \\x", FNM_NOESCAPE) == 0);
110*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("xx\\x", "xx\\x", FNM_NOESCAPE) == 0);
111*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("\\xxx", "\\xxx", FNM_NOESCAPE) == 0);
112*57718be8SEnji Cooper }
113*57718be8SEnji Cooper 
114*57718be8SEnji Cooper ATF_TC(fnmatch_pathname);
115*57718be8SEnji Cooper ATF_TC_HEAD(fnmatch_pathname, tc)
116*57718be8SEnji Cooper {
117*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test FNM_PATHNAME");
118*57718be8SEnji Cooper }
119*57718be8SEnji Cooper 
120*57718be8SEnji Cooper ATF_TC_BODY(fnmatch_pathname, tc)
121*57718be8SEnji Cooper {
122*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("???x", "xxx/x", FNM_PATHNAME) != 0);
123*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("***x", "xxx/x", FNM_PATHNAME) != 0);
124*57718be8SEnji Cooper 
125*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("???x", "xxxx", FNM_PATHNAME) == 0);
126*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("*/xxx", "/xxx", FNM_PATHNAME) == 0);
127*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("x/*.y", "x/z.y", FNM_PATHNAME) == 0);
128*57718be8SEnji Cooper }
129*57718be8SEnji Cooper 
130*57718be8SEnji Cooper ATF_TC(fnmatch_period);
131*57718be8SEnji Cooper ATF_TC_HEAD(fnmatch_period, tc)
132*57718be8SEnji Cooper {
133*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test FNM_PERIOD");
134*57718be8SEnji Cooper }
135*57718be8SEnji Cooper 
136*57718be8SEnji Cooper ATF_TC_BODY(fnmatch_period, tc)
137*57718be8SEnji Cooper {
138*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("*x*", "X", FNM_PERIOD) != 0);
139*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("*x*", "X", FNM_PERIOD | FNM_CASEFOLD) == 0);
140*57718be8SEnji Cooper 
141*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("x?y", "x.y", FNM_PATHNAME | FNM_PERIOD) == 0);
142*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("x*y", "x.y", FNM_PATHNAME | FNM_PERIOD) == 0);
143*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("*.c", "x.c", FNM_PATHNAME | FNM_PERIOD) == 0);
144*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("*/?", "x/y", FNM_PATHNAME | FNM_PERIOD) == 0);
145*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("*/*", "x/y", FNM_PATHNAME | FNM_PERIOD) == 0);
146*57718be8SEnji Cooper 	ATF_CHECK(fnmatch(".*/?", ".x/y", FNM_PATHNAME | FNM_PERIOD) == 0);
147*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("*/.?", "x/.y", FNM_PATHNAME | FNM_PERIOD) == 0);
148*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("x[.]y", "x.y", FNM_PATHNAME | FNM_PERIOD) == 0);
149*57718be8SEnji Cooper 
150*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("?x/y", ".x/y", FNM_PATHNAME | FNM_PERIOD) != 0);
151*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("*x/y", ".x/y", FNM_PATHNAME | FNM_PERIOD) != 0);
152*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("x/?y", "x/.y", FNM_PATHNAME | FNM_PERIOD) != 0);
153*57718be8SEnji Cooper 	ATF_CHECK(fnmatch("x/*y", "x/.y", FNM_PATHNAME | FNM_PERIOD) != 0);
154*57718be8SEnji Cooper }
155*57718be8SEnji Cooper 
156*57718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
157*57718be8SEnji Cooper {
158*57718be8SEnji Cooper 
159*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, fnmatch_backslashes);
160*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, fnmatch_casefold);
161*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, fnmatch_leadingdir);
162*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, fnmatch_noescape);
163*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, fnmatch_pathname);
164*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, fnmatch_period);
165*57718be8SEnji Cooper 
166*57718be8SEnji Cooper 	return atf_no_error();
167*57718be8SEnji Cooper }
168