]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/cpio/test/test_pathmatch.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[FreeBSD/FreeBSD.git] / usr.bin / cpio / test / test_pathmatch.c
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
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(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.
24  */
25 #include "test.h"
26 __FBSDID("$FreeBSD$");
27
28 #include "../pathmatch.h"
29
30 /*
31  * Verify that the pattern matcher implements the wildcard logic specified
32  * in SUSv2 for the cpio command.  This is essentially the
33  * shell glob syntax:
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
38  *
39  * The specification in SUSv2 is a bit incomplete, I assume the following:
40  *   Trailing '-' in [...] is not special.
41  */
42
43 DEFINE_TEST(test_pathmatch)
44 {
45         assertEqualInt(1, pathmatch("*","", 0));
46         assertEqualInt(1, pathmatch("*","a", 0));
47         assertEqualInt(1, pathmatch("*","abcd", 0));
48         /* SUSv2: * matches / */
49         assertEqualInt(1, pathmatch("*","abcd/efgh/ijkl", 0));
50         assertEqualInt(1, pathmatch("abcd*efgh/ijkl","abcd/efgh/ijkl", 0));
51         assertEqualInt(1, pathmatch("abcd***efgh/ijkl","abcd/efgh/ijkl", 0));
52         assertEqualInt(1, pathmatch("abcd***/efgh/ijkl","abcd/efgh/ijkl", 0));
53         assertEqualInt(0, pathmatch("?", "", 0));
54         assertEqualInt(0, pathmatch("?", "\0", 0));
55         assertEqualInt(1, pathmatch("?", "a", 0));
56         assertEqualInt(0, pathmatch("?", "ab", 0));
57         assertEqualInt(1, pathmatch("?", ".", 0));
58         assertEqualInt(1, pathmatch("?", "?", 0));
59         assertEqualInt(1, pathmatch("a", "a", 0));
60         assertEqualInt(0, pathmatch("a", "ab", 0));
61         assertEqualInt(0, pathmatch("a", "ab", 0));
62         assertEqualInt(1, pathmatch("a?c", "abc", 0));
63         /* SUSv2: ? matches / */
64         assertEqualInt(1, pathmatch("a?c", "a/c", 0));
65         assertEqualInt(1, pathmatch("a?*c*", "a/c", 0));
66         assertEqualInt(1, pathmatch("*a*", "a/c", 0));
67         assertEqualInt(1, pathmatch("*a*", "/a/c", 0));
68         assertEqualInt(1, pathmatch("*a*", "defaaaaaaa", 0));
69         assertEqualInt(0, pathmatch("a*", "defghi", 0));
70         assertEqualInt(0, pathmatch("*a*", "defghi", 0));
71         assertEqualInt(1, pathmatch("abc[def", "abc[def", 0));
72         assertEqualInt(0, pathmatch("abc[def]", "abc[def", 0));
73         assertEqualInt(0, pathmatch("abc[def", "abcd", 0));
74         assertEqualInt(1, pathmatch("abc[def]", "abcd", 0));
75         assertEqualInt(1, pathmatch("abc[def]", "abce", 0));
76         assertEqualInt(1, pathmatch("abc[def]", "abcf", 0));
77         assertEqualInt(0, pathmatch("abc[def]", "abcg", 0));
78         assertEqualInt(1, pathmatch("abc[d*f]", "abcd", 0));
79         assertEqualInt(1, pathmatch("abc[d*f]", "abc*", 0));
80         assertEqualInt(0, pathmatch("abc[d*f]", "abcdefghi", 0));
81         assertEqualInt(0, pathmatch("abc[d*", "abcdefghi", 0));
82         assertEqualInt(1, pathmatch("abc[d*", "abc[defghi", 0));
83         assertEqualInt(1, pathmatch("abc[d-f]", "abcd", 0));
84         assertEqualInt(1, pathmatch("abc[d-f]", "abce", 0));
85         assertEqualInt(1, pathmatch("abc[d-f]", "abcf", 0));
86         assertEqualInt(0, pathmatch("abc[d-f]", "abcg", 0));
87         assertEqualInt(1, pathmatch("abc[d-fh-k]", "abcd", 0));
88         assertEqualInt(1, pathmatch("abc[d-fh-k]", "abce", 0));
89         assertEqualInt(1, pathmatch("abc[d-fh-k]", "abcf", 0));
90         assertEqualInt(0, pathmatch("abc[d-fh-k]", "abcg", 0));
91         assertEqualInt(1, pathmatch("abc[d-fh-k]", "abch", 0));
92         assertEqualInt(1, pathmatch("abc[d-fh-k]", "abci", 0));
93         assertEqualInt(1, pathmatch("abc[d-fh-k]", "abcj", 0));
94         assertEqualInt(1, pathmatch("abc[d-fh-k]", "abck", 0));
95         assertEqualInt(0, pathmatch("abc[d-fh-k]", "abcl", 0));
96         assertEqualInt(0, pathmatch("abc[d-fh-k]", "abc-", 0));
97
98         /* I assume: Trailing '-' is non-special. */
99         assertEqualInt(0, pathmatch("abc[d-fh-]", "abcl", 0));
100         assertEqualInt(1, pathmatch("abc[d-fh-]", "abch", 0));
101         assertEqualInt(1, pathmatch("abc[d-fh-]", "abc-", 0));
102         assertEqualInt(1, pathmatch("abc[d-fh-]", "abc-", 0));
103
104         /* ']' can be backslash-quoted within a character class. */
105         assertEqualInt(1, pathmatch("abc[\\]]", "abc]", 0));
106         assertEqualInt(1, pathmatch("abc[\\]d]", "abc]", 0));
107         assertEqualInt(1, pathmatch("abc[\\]d]", "abcd", 0));
108         assertEqualInt(1, pathmatch("abc[d\\]]", "abc]", 0));
109         assertEqualInt(1, pathmatch("abc[d\\]]", "abcd", 0));
110         assertEqualInt(1, pathmatch("abc[d]e]", "abcde]", 0));
111         assertEqualInt(1, pathmatch("abc[d\\]e]", "abc]", 0));
112         assertEqualInt(0, pathmatch("abc[d\\]e]", "abcd]e", 0));
113         assertEqualInt(0, pathmatch("abc[d]e]", "abc]", 0));
114
115         /* backslash-quoted chars can appear as either end of a range. */
116         assertEqualInt(1, pathmatch("abc[\\d-f]gh", "abcegh", 0));
117         assertEqualInt(0, pathmatch("abc[\\d-f]gh", "abcggh", 0));
118         assertEqualInt(0, pathmatch("abc[\\d-f]gh", "abc\\gh", 0));
119         assertEqualInt(1, pathmatch("abc[d-\\f]gh", "abcegh", 0));
120         assertEqualInt(1, pathmatch("abc[\\d-\\f]gh", "abcegh", 0));
121         assertEqualInt(1, pathmatch("abc[\\d-\\f]gh", "abcegh", 0));
122         /* backslash-quoted '-' isn't special. */
123         assertEqualInt(0, pathmatch("abc[d\\-f]gh", "abcegh", 0));
124         assertEqualInt(1, pathmatch("abc[d\\-f]gh", "abc-gh", 0));
125
126         /* Leading '!' negates a character class. */
127         assertEqualInt(0, pathmatch("abc[!d]", "abcd", 0));
128         assertEqualInt(1, pathmatch("abc[!d]", "abce", 0));
129         assertEqualInt(1, pathmatch("abc[!d]", "abcc", 0));
130         assertEqualInt(0, pathmatch("abc[!d-z]", "abcq", 0));
131         assertEqualInt(1, pathmatch("abc[!d-gi-z]", "abch", 0));
132         assertEqualInt(1, pathmatch("abc[!fgijkl]", "abch", 0));
133         assertEqualInt(0, pathmatch("abc[!fghijkl]", "abch", 0));
134
135         /* Backslash quotes next character. */
136         assertEqualInt(0, pathmatch("abc\\[def]", "abc\\d", 0));
137         assertEqualInt(1, pathmatch("abc\\[def]", "abc[def]", 0));
138         assertEqualInt(0, pathmatch("abc\\\\[def]", "abc[def]", 0));
139         assertEqualInt(0, pathmatch("abc\\\\[def]", "abc\\[def]", 0));
140         assertEqualInt(1, pathmatch("abc\\\\[def]", "abc\\d", 0));
141
142         /*
143          * Because '.' and '/' have special meanings, we can
144          * identify many equivalent paths even if they're expressed
145          * differently.
146          */
147         assertEqualInt(1, pathmatch("./abc/./def/", "abc/def/", 0));
148         assertEqualInt(1, pathmatch("abc/def", "./././abc/./def", 0));
149         assertEqualInt(1, pathmatch("abc/def/././//", "./././abc/./def/", 0));
150         assertEqualInt(1, pathmatch(".////abc/.//def", "./././abc/./def", 0));
151         assertEqualInt(1, pathmatch("./abc?def/", "abc/def/", 0));
152         failure("\"?./\" is not the same as \"/./\"");
153         assertEqualInt(0, pathmatch("./abc?./def/", "abc/def/", 0));
154         failure("Trailing '/' should match no trailing '/'");
155         assertEqualInt(1, pathmatch("./abc/./def/", "abc/def", 0));
156         failure("Trailing '/./' is still the same directory.");
157         assertEqualInt(1, pathmatch("./abc/./def/./", "abc/def", 0));
158         failure("Trailing '/.' is still the same directory.");
159         assertEqualInt(1, pathmatch("./abc/./def/.", "abc/def", 0));
160         assertEqualInt(1, pathmatch("./abc/./def", "abc/def/", 0));
161         failure("Trailing '/./' is still the same directory.");
162         assertEqualInt(1, pathmatch("./abc/./def", "abc/def/./", 0));
163         failure("Trailing '/.' is still the same directory.");
164         assertEqualInt(1, pathmatch("./abc*/./def", "abc/def/.", 0));
165 }