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#define __LIBARCHIVE_TEST 29#include "archive_pathmatch.h" 30 31/* 32 * Verify that the pattern matcher implements the wildcard logic specified 33 * in SUSv2 for the cpio command. This is essentially the 34 * shell glob syntax: 35 * * - matches any sequence of chars, including '/' 36 * ? - matches any single char, including '/' 37 * [...] - matches any of a set of chars, '-' specifies a range, 38 * initial '!' is undefined 39 * 40 * The specification in SUSv2 is a bit incomplete, I assume the following: 41 * Trailing '-' in [...] is not special. 42 * 43 * TODO: Figure out if there's a good way to extend this to handle 44 * Windows paths that use '\' as a path separator. <sigh> 45 */ 46 47DEFINE_TEST(test_archive_pathmatch) 48{ 49 assertEqualInt(1, archive_pathmatch("a/b/c", "a/b/c", 0)); 50 assertEqualInt(0, archive_pathmatch("a/b/", "a/b/c", 0)); 51 assertEqualInt(0, archive_pathmatch("a/b", "a/b/c", 0)); 52 assertEqualInt(0, archive_pathmatch("a/b/c", "a/b/", 0)); 53 assertEqualInt(0, archive_pathmatch("a/b/c", "a/b", 0)); 54 55 /* Empty pattern only matches empty string. */ 56 assertEqualInt(1, archive_pathmatch("","", 0)); 57 assertEqualInt(0, archive_pathmatch("","a", 0)); 58 assertEqualInt(1, archive_pathmatch("*","", 0)); 59 assertEqualInt(1, archive_pathmatch("*","a", 0)); 60 assertEqualInt(1, archive_pathmatch("*","abcd", 0)); 61 /* SUSv2: * matches / */ 62 assertEqualInt(1, archive_pathmatch("*","abcd/efgh/ijkl", 0)); 63 assertEqualInt(1, archive_pathmatch("abcd*efgh/ijkl","abcd/efgh/ijkl", 0)); 64 assertEqualInt(1, archive_pathmatch("abcd***efgh/ijkl","abcd/efgh/ijkl", 0)); 65 assertEqualInt(1, archive_pathmatch("abcd***/efgh/ijkl","abcd/efgh/ijkl", 0)); 66 assertEqualInt(0, archive_pathmatch("?", "", 0)); 67 assertEqualInt(0, archive_pathmatch("?", "\0", 0)); 68 assertEqualInt(1, archive_pathmatch("?", "a", 0)); 69 assertEqualInt(0, archive_pathmatch("?", "ab", 0)); 70 assertEqualInt(1, archive_pathmatch("?", ".", 0)); 71 assertEqualInt(1, archive_pathmatch("?", "?", 0)); 72 assertEqualInt(1, archive_pathmatch("a", "a", 0)); 73 assertEqualInt(0, archive_pathmatch("a", "ab", 0)); 74 assertEqualInt(0, archive_pathmatch("a", "ab", 0)); 75 assertEqualInt(1, archive_pathmatch("a?c", "abc", 0)); 76 /* SUSv2: ? matches / */ 77 assertEqualInt(1, archive_pathmatch("a?c", "a/c", 0)); 78 assertEqualInt(1, archive_pathmatch("a?*c*", "a/c", 0)); 79 assertEqualInt(1, archive_pathmatch("*a*", "a/c", 0)); 80 assertEqualInt(1, archive_pathmatch("*a*", "/a/c", 0)); 81 assertEqualInt(1, archive_pathmatch("*a*", "defaaaaaaa", 0)); 82 assertEqualInt(0, archive_pathmatch("a*", "defghi", 0)); 83 assertEqualInt(0, archive_pathmatch("*a*", "defghi", 0)); 84 85 /* Character classes */ 86 assertEqualInt(1, archive_pathmatch("abc[def", "abc[def", 0)); 87 assertEqualInt(0, archive_pathmatch("abc[def]", "abc[def", 0)); 88 assertEqualInt(0, archive_pathmatch("abc[def", "abcd", 0)); 89 assertEqualInt(1, archive_pathmatch("abc[def]", "abcd", 0)); 90 assertEqualInt(1, archive_pathmatch("abc[def]", "abce", 0)); 91 assertEqualInt(1, archive_pathmatch("abc[def]", "abcf", 0)); 92 assertEqualInt(0, archive_pathmatch("abc[def]", "abcg", 0)); 93 assertEqualInt(1, archive_pathmatch("abc[d*f]", "abcd", 0)); 94 assertEqualInt(1, archive_pathmatch("abc[d*f]", "abc*", 0)); 95 assertEqualInt(0, archive_pathmatch("abc[d*f]", "abcdefghi", 0)); 96 assertEqualInt(0, archive_pathmatch("abc[d*", "abcdefghi", 0)); 97 assertEqualInt(1, archive_pathmatch("abc[d*", "abc[defghi", 0)); 98 assertEqualInt(1, archive_pathmatch("abc[d-f]", "abcd", 0)); 99 assertEqualInt(1, archive_pathmatch("abc[d-f]", "abce", 0)); 100 assertEqualInt(1, archive_pathmatch("abc[d-f]", "abcf", 0)); 101 assertEqualInt(0, archive_pathmatch("abc[d-f]", "abcg", 0)); 102 assertEqualInt(0, archive_pathmatch("abc[d-fh-k]", "abca", 0)); 103 assertEqualInt(1, archive_pathmatch("abc[d-fh-k]", "abcd", 0)); 104 assertEqualInt(1, archive_pathmatch("abc[d-fh-k]", "abce", 0)); 105 assertEqualInt(1, archive_pathmatch("abc[d-fh-k]", "abcf", 0)); 106 assertEqualInt(0, archive_pathmatch("abc[d-fh-k]", "abcg", 0)); 107 assertEqualInt(1, archive_pathmatch("abc[d-fh-k]", "abch", 0)); 108 assertEqualInt(1, archive_pathmatch("abc[d-fh-k]", "abci", 0)); 109 assertEqualInt(1, archive_pathmatch("abc[d-fh-k]", "abcj", 0)); 110 assertEqualInt(1, archive_pathmatch("abc[d-fh-k]", "abck", 0)); 111 assertEqualInt(0, archive_pathmatch("abc[d-fh-k]", "abcl", 0)); 112 assertEqualInt(0, archive_pathmatch("abc[d-fh-k]", "abc-", 0)); 113 114 /* [] matches nothing, [!] is the same as ? */ 115 assertEqualInt(0, archive_pathmatch("abc[]efg", "abcdefg", 0)); 116 assertEqualInt(0, archive_pathmatch("abc[]efg", "abcqefg", 0)); 117 assertEqualInt(0, archive_pathmatch("abc[]efg", "abcefg", 0)); 118 assertEqualInt(1, archive_pathmatch("abc[!]efg", "abcdefg", 0)); 119 assertEqualInt(1, archive_pathmatch("abc[!]efg", "abcqefg", 0)); 120 assertEqualInt(0, archive_pathmatch("abc[!]efg", "abcefg", 0)); 121 122 /* I assume: Trailing '-' is non-special. */ 123 assertEqualInt(0, archive_pathmatch("abc[d-fh-]", "abcl", 0)); 124 assertEqualInt(1, archive_pathmatch("abc[d-fh-]", "abch", 0)); 125 assertEqualInt(1, archive_pathmatch("abc[d-fh-]", "abc-", 0)); 126 assertEqualInt(1, archive_pathmatch("abc[d-fh-]", "abc-", 0)); 127 128 /* ']' can be backslash-quoted within a character class. */ 129 assertEqualInt(1, archive_pathmatch("abc[\\]]", "abc]", 0)); 130 assertEqualInt(1, archive_pathmatch("abc[\\]d]", "abc]", 0)); 131 assertEqualInt(1, archive_pathmatch("abc[\\]d]", "abcd", 0)); 132 assertEqualInt(1, archive_pathmatch("abc[d\\]]", "abc]", 0)); 133 assertEqualInt(1, archive_pathmatch("abc[d\\]]", "abcd", 0)); 134 assertEqualInt(1, archive_pathmatch("abc[d]e]", "abcde]", 0)); 135 assertEqualInt(1, archive_pathmatch("abc[d\\]e]", "abc]", 0)); 136 assertEqualInt(0, archive_pathmatch("abc[d\\]e]", "abcd]e", 0)); 137 assertEqualInt(0, archive_pathmatch("abc[d]e]", "abc]", 0)); 138 139 /* backslash-quoted chars can appear as either end of a range. */ 140 assertEqualInt(1, archive_pathmatch("abc[\\d-f]gh", "abcegh", 0)); 141 assertEqualInt(0, archive_pathmatch("abc[\\d-f]gh", "abcggh", 0)); 142 assertEqualInt(0, archive_pathmatch("abc[\\d-f]gh", "abc\\gh", 0)); 143 assertEqualInt(1, archive_pathmatch("abc[d-\\f]gh", "abcegh", 0)); 144 assertEqualInt(1, archive_pathmatch("abc[\\d-\\f]gh", "abcegh", 0)); 145 assertEqualInt(1, archive_pathmatch("abc[\\d-\\f]gh", "abcegh", 0)); 146 /* backslash-quoted '-' isn't special. */ 147 assertEqualInt(0, archive_pathmatch("abc[d\\-f]gh", "abcegh", 0)); 148 assertEqualInt(1, archive_pathmatch("abc[d\\-f]gh", "abc-gh", 0)); 149 150 /* Leading '!' negates a character class. */ 151 assertEqualInt(0, archive_pathmatch("abc[!d]", "abcd", 0)); 152 assertEqualInt(1, archive_pathmatch("abc[!d]", "abce", 0)); 153 assertEqualInt(1, archive_pathmatch("abc[!d]", "abcc", 0)); 154 assertEqualInt(0, archive_pathmatch("abc[!d-z]", "abcq", 0)); 155 assertEqualInt(1, archive_pathmatch("abc[!d-gi-z]", "abch", 0)); 156 assertEqualInt(1, archive_pathmatch("abc[!fgijkl]", "abch", 0)); 157 assertEqualInt(0, archive_pathmatch("abc[!fghijkl]", "abch", 0)); 158 159 /* Backslash quotes next character. */ 160 assertEqualInt(0, archive_pathmatch("abc\\[def]", "abc\\d", 0)); 161 assertEqualInt(1, archive_pathmatch("abc\\[def]", "abc[def]", 0)); 162 assertEqualInt(0, archive_pathmatch("abc\\\\[def]", "abc[def]", 0)); 163 assertEqualInt(0, archive_pathmatch("abc\\\\[def]", "abc\\[def]", 0)); 164 assertEqualInt(1, archive_pathmatch("abc\\\\[def]", "abc\\d", 0)); 165 assertEqualInt(1, archive_pathmatch("abcd\\", "abcd\\", 0)); 166 assertEqualInt(0, archive_pathmatch("abcd\\", "abcd\\[", 0)); 167 assertEqualInt(0, archive_pathmatch("abcd\\", "abcde", 0)); 168 assertEqualInt(0, archive_pathmatch("abcd\\[", "abcd\\", 0)); 169 170 /* 171 * Because '.' and '/' have special meanings, we can 172 * identify many equivalent paths even if they're expressed 173 * differently. (But quoting a character with '\\' suppresses 174 * special meanings!) 175 */ 176 assertEqualInt(0, archive_pathmatch("a/b/", "a/bc", 0)); 177 assertEqualInt(1, archive_pathmatch("a/./b", "a/b", 0)); 178 assertEqualInt(0, archive_pathmatch("a\\/./b", "a/b", 0)); 179 assertEqualInt(0, archive_pathmatch("a/\\./b", "a/b", 0)); 180 assertEqualInt(0, archive_pathmatch("a/.\\/b", "a/b", 0)); 181 assertEqualInt(0, archive_pathmatch("a\\/\\.\\/b", "a/b", 0)); 182 assertEqualInt(1, archive_pathmatch("./abc/./def/", "abc/def/", 0)); 183 assertEqualInt(1, archive_pathmatch("abc/def", "./././abc/./def", 0)); 184 assertEqualInt(1, archive_pathmatch("abc/def/././//", "./././abc/./def/", 0)); 185 assertEqualInt(1, archive_pathmatch(".////abc/.//def", "./././abc/./def", 0)); 186 assertEqualInt(1, archive_pathmatch("./abc?def/", "abc/def/", 0)); 187 failure("\"?./\" is not the same as \"/./\""); 188 assertEqualInt(0, archive_pathmatch("./abc?./def/", "abc/def/", 0)); 189 failure("Trailing '/' should match no trailing '/'"); 190 assertEqualInt(1, archive_pathmatch("./abc/./def/", "abc/def", 0)); 191 failure("Trailing '/./' is still the same directory."); 192 assertEqualInt(1, archive_pathmatch("./abc/./def/./", "abc/def", 0)); 193 failure("Trailing '/.' is still the same directory."); 194 assertEqualInt(1, archive_pathmatch("./abc/./def/.", "abc/def", 0)); 195 assertEqualInt(1, archive_pathmatch("./abc/./def", "abc/def/", 0)); 196 failure("Trailing '/./' is still the same directory."); 197 assertEqualInt(1, archive_pathmatch("./abc/./def", "abc/def/./", 0)); 198 failure("Trailing '/.' is still the same directory."); 199 assertEqualInt(1, archive_pathmatch("./abc*/./def", "abc/def/.", 0)); 200 201 /* Matches not anchored at beginning. */ 202 assertEqualInt(0, 203 archive_pathmatch("bcd", "abcd", PATHMATCH_NO_ANCHOR_START)); 204 assertEqualInt(1, 205 archive_pathmatch("abcd", "abcd", PATHMATCH_NO_ANCHOR_START)); 206 assertEqualInt(0, 207 archive_pathmatch("^bcd", "abcd", PATHMATCH_NO_ANCHOR_START)); 208 assertEqualInt(1, 209 archive_pathmatch("b/c/d", "a/b/c/d", PATHMATCH_NO_ANCHOR_START)); 210 assertEqualInt(0, 211 archive_pathmatch("b/c", "a/b/c/d", PATHMATCH_NO_ANCHOR_START)); 212 assertEqualInt(0, 213 archive_pathmatch("^b/c", "a/b/c/d", PATHMATCH_NO_ANCHOR_START)); 214 215 /* Matches not anchored at end. */ 216 assertEqualInt(0, 217 archive_pathmatch("bcd", "abcd", PATHMATCH_NO_ANCHOR_END)); 218 assertEqualInt(1, 219 archive_pathmatch("abcd", "abcd", PATHMATCH_NO_ANCHOR_END)); 220 assertEqualInt(1, 221 archive_pathmatch("abcd", "abcd/", PATHMATCH_NO_ANCHOR_END)); 222 assertEqualInt(1, 223 archive_pathmatch("abcd", "abcd/.", PATHMATCH_NO_ANCHOR_END)); 224 assertEqualInt(0, 225 archive_pathmatch("abc", "abcd", PATHMATCH_NO_ANCHOR_END)); 226 assertEqualInt(1, 227 archive_pathmatch("a/b/c", "a/b/c/d", PATHMATCH_NO_ANCHOR_END)); 228 assertEqualInt(0, 229 archive_pathmatch("a/b/c$", "a/b/c/d", PATHMATCH_NO_ANCHOR_END)); 230 assertEqualInt(1, 231 archive_pathmatch("a/b/c$", "a/b/c", PATHMATCH_NO_ANCHOR_END)); 232 assertEqualInt(1, 233 archive_pathmatch("a/b/c$", "a/b/c/", PATHMATCH_NO_ANCHOR_END)); 234 assertEqualInt(1, 235 archive_pathmatch("a/b/c/", "a/b/c/d", PATHMATCH_NO_ANCHOR_END)); 236 assertEqualInt(0, 237 archive_pathmatch("a/b/c/$", "a/b/c/d", PATHMATCH_NO_ANCHOR_END)); 238 assertEqualInt(1, 239 archive_pathmatch("a/b/c/$", "a/b/c/", PATHMATCH_NO_ANCHOR_END)); 240 assertEqualInt(1, 241 archive_pathmatch("a/b/c/$", "a/b/c", PATHMATCH_NO_ANCHOR_END)); 242 assertEqualInt(0, 243 archive_pathmatch("b/c", "a/b/c/d", PATHMATCH_NO_ANCHOR_END)); 244} 245