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