xref: /freebsd/lib/libc/tests/gen/fnmatch_test.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /*-
2  * Copyright (c) 2010 Jilles Tjoelker
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <errno.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 
37 #include <atf-c.h>
38 
39 #include "fnmatch_testcases.h"
40 
41 static const char *
42 flags_to_string(int flags)
43 {
44 	static const int flagvalues[] = { FNM_NOESCAPE, FNM_PATHNAME,
45 		FNM_PERIOD, FNM_LEADING_DIR, FNM_CASEFOLD, 0 };
46 	static const char flagnames[] = "FNM_NOESCAPE\0FNM_PATHNAME\0FNM_PERIOD\0FNM_LEADING_DIR\0FNM_CASEFOLD\0";
47 	static char result[sizeof(flagnames) + 3 * sizeof(int) + 2];
48 	char *p;
49 	size_t i, len;
50 	const char *fp;
51 
52 	p = result;
53 	fp = flagnames;
54 	for (i = 0; flagvalues[i] != 0; i++) {
55 		len = strlen(fp);
56 		if (flags & flagvalues[i]) {
57 			if (p != result)
58 				*p++ = '|';
59 			memcpy(p, fp, len);
60 			p += len;
61 			flags &= ~flagvalues[i];
62 		}
63 		fp += len + 1;
64 	}
65 	if (p == result)
66 		memcpy(p, "0", 2);
67 	else if (flags != 0)
68 		sprintf(p, "%d", flags);
69 	else
70 		*p = '\0';
71 	return result;
72 }
73 
74 ATF_TC_WITHOUT_HEAD(fnmatch_test);
75 ATF_TC_BODY(fnmatch_test, tc)
76 {
77 	size_t i;
78 	int flags, result;
79 	struct testcase *t;
80 
81 	for (i = 0; i < nitems(testcases); i++) {
82 		t = &testcases[i];
83 		flags = t->flags;
84 		do {
85 			result = fnmatch(t->pattern, t->string, flags);
86 			if (result != t->result)
87 				break;
88 			if (strchr(t->pattern, '\\') == NULL &&
89 			    !(flags & FNM_NOESCAPE)) {
90 				flags |= FNM_NOESCAPE;
91 				result = fnmatch(t->pattern, t->string, flags);
92 				if (result != t->result)
93 					break;
94 				flags = t->flags;
95 			}
96 			if (strchr(t->pattern, '\\') != NULL &&
97 			    strchr(t->string, '\\') == NULL &&
98 			    t->result == FNM_NOMATCH &&
99 			    !(flags & (FNM_NOESCAPE | FNM_LEADING_DIR))) {
100 				flags |= FNM_NOESCAPE;
101 				result = fnmatch(t->pattern, t->string, flags);
102 				if (result != t->result)
103 					break;
104 				flags = t->flags;
105 			}
106 			if ((t->string[0] != '.' || t->pattern[0] == '.' ||
107 			    t->result == FNM_NOMATCH) &&
108 			    !(flags & (FNM_PATHNAME | FNM_PERIOD))) {
109 				flags |= FNM_PERIOD;
110 				result = fnmatch(t->pattern, t->string, flags);
111 				if (result != t->result)
112 					break;
113 				flags = t->flags;
114 			}
115 			if ((strchr(t->string, '/') == NULL ||
116 			    t->result == FNM_NOMATCH) &&
117 			    !(flags & FNM_PATHNAME)) {
118 				flags |= FNM_PATHNAME;
119 				result = fnmatch(t->pattern, t->string, flags);
120 				if (result != t->result)
121 					break;
122 				flags = t->flags;
123 			}
124 			if ((((t->string[0] != '.' || t->pattern[0] == '.') &&
125 			    strstr(t->string, "/.") == NULL) ||
126 			    t->result == FNM_NOMATCH) &&
127 			    flags & FNM_PATHNAME && !(flags & FNM_PERIOD)) {
128 				flags |= FNM_PERIOD;
129 				result = fnmatch(t->pattern, t->string, flags);
130 				if (result != t->result)
131 					break;
132 				flags = t->flags;
133 			}
134 			if ((((t->string[0] != '.' || t->pattern[0] == '.') &&
135 			    strchr(t->string, '/') == NULL) ||
136 			    t->result == FNM_NOMATCH) &&
137 			    !(flags & (FNM_PATHNAME | FNM_PERIOD))) {
138 				flags |= FNM_PATHNAME | FNM_PERIOD;
139 				result = fnmatch(t->pattern, t->string, flags);
140 				if (result != t->result)
141 					break;
142 				flags = t->flags;
143 			}
144 			if ((strchr(t->string, '/') == NULL || t->result == 0)
145 			    && !(flags & FNM_LEADING_DIR)) {
146 				flags |= FNM_LEADING_DIR;
147 				result = fnmatch(t->pattern, t->string, flags);
148 				if (result != t->result)
149 					break;
150 				flags = t->flags;
151 			}
152 			if (t->result == 0 && !(flags & FNM_CASEFOLD)) {
153 				flags |= FNM_CASEFOLD;
154 				result = fnmatch(t->pattern, t->string, flags);
155 				if (result != t->result)
156 					break;
157 				flags = t->flags;
158 			}
159 			if (strchr(t->pattern, '\\') == NULL &&
160 			    t->result == 0 &&
161 			    !(flags & (FNM_NOESCAPE | FNM_CASEFOLD))) {
162 				flags |= FNM_NOESCAPE | FNM_CASEFOLD;
163 				result = fnmatch(t->pattern, t->string, flags);
164 				if (result != t->result)
165 					break;
166 				flags = t->flags;
167 			}
168 		} while (0);
169 
170 		ATF_CHECK(result == t->result);
171 		if (result == t->result)
172 			printf("fnmatch(\"%s\", \"%s\", %s) == %d\n",
173 			    t->pattern, t->string, flags_to_string(flags), result);
174 		else
175 			printf("fnmatch(\"%s\", \"%s\", %s) != %d (was %d)\n",
176 			    t->pattern, t->string, flags_to_string(flags),
177 			    t->result, result);
178 	}
179 
180 }
181 
182 ATF_TP_ADD_TCS(tp)
183 {
184 
185 	ATF_TP_ADD_TC(tp, fnmatch_test);
186 
187 	return (atf_no_error());
188 }
189