2
* Copyright (c) 2003-2007 Tim Kientzle
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
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.
14
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
__FBSDID("$FreeBSD$");
28
#include "pathmatch.h"
31
* Verify that the pattern matcher implements the wildcard logic specified
32
* in SUSv2 for the cpio command. This is essentially the
34
* * - matches any sequence of chars, including '/'
35
* ? - matches any single char, including '/'
36
* [...] - matches any of a set of chars, '-' specifies a range,
37
* initial '!' is undefined
39
* The specification in SUSv2 is a bit incomplete, I assume the following:
40
* Trailing '-' in [...] is not special.
42
* TODO: Figure out if there's a good way to extend this to handle
43
* Windows paths that use '\' as a path separator. <sigh>
46
DEFINE_TEST(test_pathmatch)
48
assertEqualInt(1, lafe_pathmatch("a/b/c", "a/b/c", 0));
49
assertEqualInt(0, lafe_pathmatch("a/b/", "a/b/c", 0));
50
assertEqualInt(0, lafe_pathmatch("a/b", "a/b/c", 0));
51
assertEqualInt(0, lafe_pathmatch("a/b/c", "a/b/", 0));
52
assertEqualInt(0, lafe_pathmatch("a/b/c", "a/b", 0));
54
/* Empty pattern only matches empty string. */
55
assertEqualInt(1, lafe_pathmatch("","", 0));
56
assertEqualInt(0, lafe_pathmatch("","a", 0));
57
assertEqualInt(1, lafe_pathmatch("*","", 0));
58
assertEqualInt(1, lafe_pathmatch("*","a", 0));
59
assertEqualInt(1, lafe_pathmatch("*","abcd", 0));
60
/* SUSv2: * matches / */
61
assertEqualInt(1, lafe_pathmatch("*","abcd/efgh/ijkl", 0));
62
assertEqualInt(1, lafe_pathmatch("abcd*efgh/ijkl","abcd/efgh/ijkl", 0));
63
assertEqualInt(1, lafe_pathmatch("abcd***efgh/ijkl","abcd/efgh/ijkl", 0));
64
assertEqualInt(1, lafe_pathmatch("abcd***/efgh/ijkl","abcd/efgh/ijkl", 0));
65
assertEqualInt(0, lafe_pathmatch("?", "", 0));
66
assertEqualInt(0, lafe_pathmatch("?", "\0", 0));
67
assertEqualInt(1, lafe_pathmatch("?", "a", 0));
68
assertEqualInt(0, lafe_pathmatch("?", "ab", 0));
69
assertEqualInt(1, lafe_pathmatch("?", ".", 0));
70
assertEqualInt(1, lafe_pathmatch("?", "?", 0));
71
assertEqualInt(1, lafe_pathmatch("a", "a", 0));
72
assertEqualInt(0, lafe_pathmatch("a", "ab", 0));
73
assertEqualInt(0, lafe_pathmatch("a", "ab", 0));
74
assertEqualInt(1, lafe_pathmatch("a?c", "abc", 0));
75
/* SUSv2: ? matches / */
76
assertEqualInt(1, lafe_pathmatch("a?c", "a/c", 0));
77
assertEqualInt(1, lafe_pathmatch("a?*c*", "a/c", 0));
78
assertEqualInt(1, lafe_pathmatch("*a*", "a/c", 0));
79
assertEqualInt(1, lafe_pathmatch("*a*", "/a/c", 0));
80
assertEqualInt(1, lafe_pathmatch("*a*", "defaaaaaaa", 0));
81
assertEqualInt(0, lafe_pathmatch("a*", "defghi", 0));
82
assertEqualInt(0, lafe_pathmatch("*a*", "defghi", 0));
84
/* Character classes */
85
assertEqualInt(1, lafe_pathmatch("abc[def", "abc[def", 0));
86
assertEqualInt(0, lafe_pathmatch("abc[def]", "abc[def", 0));
87
assertEqualInt(0, lafe_pathmatch("abc[def", "abcd", 0));
88
assertEqualInt(1, lafe_pathmatch("abc[def]", "abcd", 0));
89
assertEqualInt(1, lafe_pathmatch("abc[def]", "abce", 0));
90
assertEqualInt(1, lafe_pathmatch("abc[def]", "abcf", 0));
91
assertEqualInt(0, lafe_pathmatch("abc[def]", "abcg", 0));
92
assertEqualInt(1, lafe_pathmatch("abc[d*f]", "abcd", 0));
93
assertEqualInt(1, lafe_pathmatch("abc[d*f]", "abc*", 0));
94
assertEqualInt(0, lafe_pathmatch("abc[d*f]", "abcdefghi", 0));
95
assertEqualInt(0, lafe_pathmatch("abc[d*", "abcdefghi", 0));
96
assertEqualInt(1, lafe_pathmatch("abc[d*", "abc[defghi", 0));
97
assertEqualInt(1, lafe_pathmatch("abc[d-f]", "abcd", 0));
98
assertEqualInt(1, lafe_pathmatch("abc[d-f]", "abce", 0));
99
assertEqualInt(1, lafe_pathmatch("abc[d-f]", "abcf", 0));
100
assertEqualInt(0, lafe_pathmatch("abc[d-f]", "abcg", 0));
101
assertEqualInt(0, lafe_pathmatch("abc[d-fh-k]", "abca", 0));
102
assertEqualInt(1, lafe_pathmatch("abc[d-fh-k]", "abcd", 0));
103
assertEqualInt(1, lafe_pathmatch("abc[d-fh-k]", "abce", 0));
104
assertEqualInt(1, lafe_pathmatch("abc[d-fh-k]", "abcf", 0));
105
assertEqualInt(0, lafe_pathmatch("abc[d-fh-k]", "abcg", 0));
106
assertEqualInt(1, lafe_pathmatch("abc[d-fh-k]", "abch", 0));
107
assertEqualInt(1, lafe_pathmatch("abc[d-fh-k]", "abci", 0));
108
assertEqualInt(1, lafe_pathmatch("abc[d-fh-k]", "abcj", 0));
109
assertEqualInt(1, lafe_pathmatch("abc[d-fh-k]", "abck", 0));
110
assertEqualInt(0, lafe_pathmatch("abc[d-fh-k]", "abcl", 0));
111
assertEqualInt(0, lafe_pathmatch("abc[d-fh-k]", "abc-", 0));
113
/* [] matches nothing, [!] is the same as ? */
114
assertEqualInt(0, lafe_pathmatch("abc[]efg", "abcdefg", 0));
115
assertEqualInt(0, lafe_pathmatch("abc[]efg", "abcqefg", 0));
116
assertEqualInt(0, lafe_pathmatch("abc[]efg", "abcefg", 0));
117
assertEqualInt(1, lafe_pathmatch("abc[!]efg", "abcdefg", 0));
118
assertEqualInt(1, lafe_pathmatch("abc[!]efg", "abcqefg", 0));
119
assertEqualInt(0, lafe_pathmatch("abc[!]efg", "abcefg", 0));
121
/* I assume: Trailing '-' is non-special. */
122
assertEqualInt(0, lafe_pathmatch("abc[d-fh-]", "abcl", 0));
123
assertEqualInt(1, lafe_pathmatch("abc[d-fh-]", "abch", 0));
124
assertEqualInt(1, lafe_pathmatch("abc[d-fh-]", "abc-", 0));
125
assertEqualInt(1, lafe_pathmatch("abc[d-fh-]", "abc-", 0));
127
/* ']' can be backslash-quoted within a character class. */
128
assertEqualInt(1, lafe_pathmatch("abc[\\]]", "abc]", 0));
129
assertEqualInt(1, lafe_pathmatch("abc[\\]d]", "abc]", 0));
130
assertEqualInt(1, lafe_pathmatch("abc[\\]d]", "abcd", 0));
131
assertEqualInt(1, lafe_pathmatch("abc[d\\]]", "abc]", 0));
132
assertEqualInt(1, lafe_pathmatch("abc[d\\]]", "abcd", 0));
133
assertEqualInt(1, lafe_pathmatch("abc[d]e]", "abcde]", 0));
134
assertEqualInt(1, lafe_pathmatch("abc[d\\]e]", "abc]", 0));
135
assertEqualInt(0, lafe_pathmatch("abc[d\\]e]", "abcd]e", 0));
136
assertEqualInt(0, lafe_pathmatch("abc[d]e]", "abc]", 0));
138
/* backslash-quoted chars can appear as either end of a range. */
139
assertEqualInt(1, lafe_pathmatch("abc[\\d-f]gh", "abcegh", 0));
140
assertEqualInt(0, lafe_pathmatch("abc[\\d-f]gh", "abcggh", 0));
141
assertEqualInt(0, lafe_pathmatch("abc[\\d-f]gh", "abc\\gh", 0));
142
assertEqualInt(1, lafe_pathmatch("abc[d-\\f]gh", "abcegh", 0));
143
assertEqualInt(1, lafe_pathmatch("abc[\\d-\\f]gh", "abcegh", 0));
144
assertEqualInt(1, lafe_pathmatch("abc[\\d-\\f]gh", "abcegh", 0));
145
/* backslash-quoted '-' isn't special. */
146
assertEqualInt(0, lafe_pathmatch("abc[d\\-f]gh", "abcegh", 0));
147
assertEqualInt(1, lafe_pathmatch("abc[d\\-f]gh", "abc-gh", 0));
149
/* Leading '!' negates a character class. */
150
assertEqualInt(0, lafe_pathmatch("abc[!d]", "abcd", 0));
151
assertEqualInt(1, lafe_pathmatch("abc[!d]", "abce", 0));
152
assertEqualInt(1, lafe_pathmatch("abc[!d]", "abcc", 0));
153
assertEqualInt(0, lafe_pathmatch("abc[!d-z]", "abcq", 0));
154
assertEqualInt(1, lafe_pathmatch("abc[!d-gi-z]", "abch", 0));
155
assertEqualInt(1, lafe_pathmatch("abc[!fgijkl]", "abch", 0));
156
assertEqualInt(0, lafe_pathmatch("abc[!fghijkl]", "abch", 0));
158
/* Backslash quotes next character. */
159
assertEqualInt(0, lafe_pathmatch("abc\\[def]", "abc\\d", 0));
160
assertEqualInt(1, lafe_pathmatch("abc\\[def]", "abc[def]", 0));
161
assertEqualInt(0, lafe_pathmatch("abc\\\\[def]", "abc[def]", 0));
162
assertEqualInt(0, lafe_pathmatch("abc\\\\[def]", "abc\\[def]", 0));
163
assertEqualInt(1, lafe_pathmatch("abc\\\\[def]", "abc\\d", 0));
164
assertEqualInt(1, lafe_pathmatch("abcd\\", "abcd\\", 0));
165
assertEqualInt(0, lafe_pathmatch("abcd\\", "abcd\\[", 0));
166
assertEqualInt(0, lafe_pathmatch("abcd\\", "abcde", 0));
167
assertEqualInt(0, lafe_pathmatch("abcd\\[", "abcd\\", 0));
170
* Because '.' and '/' have special meanings, we can
171
* identify many equivalent paths even if they're expressed
172
* differently. (But quoting a character with '\\' suppresses
175
assertEqualInt(0, lafe_pathmatch("a/b/", "a/bc", 0));
176
assertEqualInt(1, lafe_pathmatch("a/./b", "a/b", 0));
177
assertEqualInt(0, lafe_pathmatch("a\\/./b", "a/b", 0));
178
assertEqualInt(0, lafe_pathmatch("a/\\./b", "a/b", 0));
179
assertEqualInt(0, lafe_pathmatch("a/.\\/b", "a/b", 0));
180
assertEqualInt(0, lafe_pathmatch("a\\/\\.\\/b", "a/b", 0));
181
assertEqualInt(1, lafe_pathmatch("./abc/./def/", "abc/def/", 0));
182
assertEqualInt(1, lafe_pathmatch("abc/def", "./././abc/./def", 0));
183
assertEqualInt(1, lafe_pathmatch("abc/def/././//", "./././abc/./def/", 0));
184
assertEqualInt(1, lafe_pathmatch(".////abc/.//def", "./././abc/./def", 0));
185
assertEqualInt(1, lafe_pathmatch("./abc?def/", "abc/def/", 0));
186
failure("\"?./\" is not the same as \"/./\"");
187
assertEqualInt(0, lafe_pathmatch("./abc?./def/", "abc/def/", 0));
188
failure("Trailing '/' should match no trailing '/'");
189
assertEqualInt(1, lafe_pathmatch("./abc/./def/", "abc/def", 0));
190
failure("Trailing '/./' is still the same directory.");
191
assertEqualInt(1, lafe_pathmatch("./abc/./def/./", "abc/def", 0));
192
failure("Trailing '/.' is still the same directory.");
193
assertEqualInt(1, lafe_pathmatch("./abc/./def/.", "abc/def", 0));
194
assertEqualInt(1, lafe_pathmatch("./abc/./def", "abc/def/", 0));
195
failure("Trailing '/./' is still the same directory.");
196
assertEqualInt(1, lafe_pathmatch("./abc/./def", "abc/def/./", 0));
197
failure("Trailing '/.' is still the same directory.");
198
assertEqualInt(1, lafe_pathmatch("./abc*/./def", "abc/def/.", 0));
200
/* Matches not anchored at beginning. */
202
lafe_pathmatch("bcd", "abcd", PATHMATCH_NO_ANCHOR_START));
204
lafe_pathmatch("abcd", "abcd", PATHMATCH_NO_ANCHOR_START));
206
lafe_pathmatch("^bcd", "abcd", PATHMATCH_NO_ANCHOR_START));
208
lafe_pathmatch("b/c/d", "a/b/c/d", PATHMATCH_NO_ANCHOR_START));
210
lafe_pathmatch("b/c", "a/b/c/d", PATHMATCH_NO_ANCHOR_START));
212
lafe_pathmatch("^b/c", "a/b/c/d", PATHMATCH_NO_ANCHOR_START));
214
/* Matches not anchored at end. */
216
lafe_pathmatch("bcd", "abcd", PATHMATCH_NO_ANCHOR_END));
218
lafe_pathmatch("abcd", "abcd", PATHMATCH_NO_ANCHOR_END));
220
lafe_pathmatch("abcd", "abcd/", PATHMATCH_NO_ANCHOR_END));
222
lafe_pathmatch("abcd", "abcd/.", PATHMATCH_NO_ANCHOR_END));
224
lafe_pathmatch("abc", "abcd", PATHMATCH_NO_ANCHOR_END));
226
lafe_pathmatch("a/b/c", "a/b/c/d", PATHMATCH_NO_ANCHOR_END));
228
lafe_pathmatch("a/b/c$", "a/b/c/d", PATHMATCH_NO_ANCHOR_END));
230
lafe_pathmatch("a/b/c$", "a/b/c", PATHMATCH_NO_ANCHOR_END));
232
lafe_pathmatch("a/b/c$", "a/b/c/", PATHMATCH_NO_ANCHOR_END));
234
lafe_pathmatch("a/b/c/", "a/b/c/d", PATHMATCH_NO_ANCHOR_END));
236
lafe_pathmatch("a/b/c/$", "a/b/c/d", PATHMATCH_NO_ANCHOR_END));
238
lafe_pathmatch("a/b/c/$", "a/b/c/", PATHMATCH_NO_ANCHOR_END));
240
lafe_pathmatch("a/b/c/$", "a/b/c", PATHMATCH_NO_ANCHOR_END));
242
lafe_pathmatch("b/c", "a/b/c/d", PATHMATCH_NO_ANCHOR_END));