1 /*
2 * Copyright (C) 2020 The Android Open Source Project
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 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <gtest/gtest.h>
30
31 #include <fnmatch.h>
32
TEST(fnmatch,basic)33 TEST(fnmatch, basic) {
34 EXPECT_EQ(0, fnmatch("abc", "abc", 0));
35 EXPECT_EQ(FNM_NOMATCH, fnmatch("abc", "abd", 0));
36 }
37
TEST(fnmatch,casefold)38 TEST(fnmatch, casefold) {
39 EXPECT_EQ(FNM_NOMATCH, fnmatch("abc", "aBc", 0));
40 EXPECT_EQ(0, fnmatch("abc", "aBc", FNM_CASEFOLD));
41 }
42
TEST(fnmatch,character_class)43 TEST(fnmatch, character_class) {
44 // Literal.
45 EXPECT_EQ(0, fnmatch("ab[cd]", "abc", 0));
46 EXPECT_EQ(0, fnmatch("ab[cd]", "abd", 0));
47 EXPECT_EQ(FNM_NOMATCH, fnmatch("ab[cd]", "abe", 0));
48
49 // Inverted literal.
50 EXPECT_EQ(FNM_NOMATCH, fnmatch("ab[^cd]", "abc", 0));
51 EXPECT_EQ(FNM_NOMATCH, fnmatch("ab[^cd]", "abd", 0));
52 EXPECT_EQ(0, fnmatch("ab[^cd]", "abe", 0));
53
54 // Range.
55 EXPECT_EQ(0, fnmatch("a[0-9]b", "a0b", 0));
56 EXPECT_EQ(FNM_NOMATCH, fnmatch("a[0-9]b", "aOb", 0));
57
58 // Inverted range.
59 EXPECT_EQ(0, fnmatch("a[^0-9]b", "aOb", 0));
60 EXPECT_EQ(FNM_NOMATCH, fnmatch("a[^0-9]b", "a0b", 0));
61
62 // Named.
63 EXPECT_EQ(0, fnmatch("a[[:digit:]]b", "a0b", 0));
64 EXPECT_EQ(FNM_NOMATCH, fnmatch("a[[:digit:]]b", "aOb", 0));
65
66 // Inverted named.
67 EXPECT_EQ(FNM_NOMATCH, fnmatch("a[^[:digit:]]b", "a0b", 0));
68 EXPECT_EQ(0, fnmatch("a[^[:digit:]]b", "aOb", 0));
69 }
70
TEST(fnmatch,wild_any)71 TEST(fnmatch, wild_any) {
72 EXPECT_EQ(0, fnmatch("ab*", "ab", 0));
73 EXPECT_EQ(0, fnmatch("ab*", "abc", 0));
74 EXPECT_EQ(0, fnmatch("ab*", "abcd", 0));
75 EXPECT_EQ(0, fnmatch("ab*", "ab/cd", 0));
76 EXPECT_EQ(FNM_NOMATCH, fnmatch("ab*", "ab/cd", FNM_PATHNAME));
77 }
78
TEST(fnmatch,wild_one)79 TEST(fnmatch, wild_one) {
80 EXPECT_EQ(FNM_NOMATCH, fnmatch("ab?", "ab", 0));
81 EXPECT_EQ(0, fnmatch("ab?", "abc", 0));
82 EXPECT_EQ(FNM_NOMATCH, fnmatch("ab?", "abcd", 0));
83 EXPECT_EQ(0, fnmatch("ab?d", "abcd", 0));
84 EXPECT_EQ(0, fnmatch("ab?cd", "ab/cd", 0));
85 EXPECT_EQ(FNM_NOMATCH, fnmatch("ab?cd", "ab/cd", FNM_PATHNAME));
86 }
87
TEST(fnmatch,leading_dir)88 TEST(fnmatch, leading_dir) {
89 EXPECT_EQ(FNM_NOMATCH, fnmatch("ab", "abcd", FNM_LEADING_DIR));
90 EXPECT_EQ(0, fnmatch("ab*", "abcd", FNM_LEADING_DIR));
91 EXPECT_EQ(0, fnmatch("*ab*", "1/2/3/4/abcd", FNM_LEADING_DIR));
92 EXPECT_EQ(FNM_NOMATCH, fnmatch("*ab*", "1/2/3/4/abcd", FNM_PATHNAME | FNM_LEADING_DIR));
93 EXPECT_EQ(FNM_NOMATCH, fnmatch("ab?", "abcd", FNM_LEADING_DIR));
94
95 EXPECT_EQ(0, fnmatch("ab", "ab/cd", FNM_LEADING_DIR));
96 EXPECT_EQ(0, fnmatch("ab", "ab/cd", FNM_PATHNAME | FNM_LEADING_DIR));
97 // TODO(b/175302045) fix this case and enable this test.
98 // EXPECT_EQ(0, fnmatch("*ab", "1/2/3/4/ab/cd", FNM_LEADING_DIR));
99 EXPECT_EQ(FNM_NOMATCH, fnmatch("*ab", "1/2/3/4/ab/cd", FNM_PATHNAME | FNM_LEADING_DIR));
100 EXPECT_EQ(0, fnmatch("ab*", "ab/cd/ef", FNM_LEADING_DIR));
101 EXPECT_EQ(0, fnmatch("ab*", "ab/cd/ef", FNM_PATHNAME | FNM_LEADING_DIR));
102 EXPECT_EQ(0, fnmatch("*ab*", "1/2/3/4/ab/cd/ef", FNM_LEADING_DIR));
103 EXPECT_EQ(FNM_NOMATCH, fnmatch("*ab*", "1/2/3/4/ab/cd/ef", FNM_PATHNAME | FNM_LEADING_DIR));
104 EXPECT_EQ(FNM_NOMATCH, fnmatch("ab?", "ab/cd/ef", FNM_LEADING_DIR));
105 EXPECT_EQ(0, fnmatch("ab?", "abx/cd/ef", FNM_LEADING_DIR));
106
107 EXPECT_EQ(FNM_NOMATCH, fnmatch("ab/", "ab/cd/ef", FNM_LEADING_DIR));
108 EXPECT_EQ(0, fnmatch("ab/*", "ab/cd/ef", FNM_LEADING_DIR));
109 EXPECT_EQ(FNM_NOMATCH, fnmatch("ab/?", "ab/cd/ef", FNM_LEADING_DIR));
110
111 // TODO(b/175302045) fix this case and enable this test.
112 // EXPECT_EQ(0, fnmatch("ab*c", "ab/1/2/3/c/d/e", FNM_LEADING_DIR));
113 EXPECT_EQ(0, fnmatch("ab?c", "ab/c/ef", FNM_LEADING_DIR));
114
115 EXPECT_EQ(0, fnmatch("ab*c*", "ab/1/2/3/c/d/e", FNM_LEADING_DIR));
116 EXPECT_EQ(0, fnmatch("ab?c*", "ab/c/ef", FNM_LEADING_DIR));
117
118 EXPECT_EQ(FNM_NOMATCH, fnmatch("ab*c/", "ab/1/2/3/c/d/e", FNM_LEADING_DIR));
119 EXPECT_EQ(FNM_NOMATCH, fnmatch("ab?c/", "ab/c/ef", FNM_LEADING_DIR));
120
121 EXPECT_EQ(0, fnmatch("ab*c/*", "ab/1/2/3/c/d/e", FNM_LEADING_DIR));
122 EXPECT_EQ(0, fnmatch("ab?c/*", "ab/c/ef", FNM_LEADING_DIR));
123 }
124