• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <glob.h>
18 
19 #include <dirent.h>
20 #include <sys/cdefs.h>
21 
22 #include <gtest/gtest.h>
23 
24 #include <string>
25 #include <vector>
26 
27 #include <android-base/file.h>
28 
29 #if defined(__BIONIC__)
30 #define ASSERT_MATCH_COUNT(n_,g_) ASSERT_EQ(n_, g_.gl_matchc)
31 #else
32 #define ASSERT_MATCH_COUNT(n_,g_)
33 #endif
34 
35 //
36 // Helper for use with GLOB_ALTDIRFUNC to iterate over the elements of `fake_dir`.
37 //
38 
39 #if !defined(ANDROID_HOST_MUSL)
40 static std::vector<std::string> fake_dir;
41 static size_t fake_dir_offset;
fake_closedir(void *)42 static void fake_closedir(void*) {
43 }
fake_readdir(void *)44 static dirent* fake_readdir(void*) {
45   static dirent d;
46   if (fake_dir_offset >= fake_dir.size()) return nullptr;
47   strcpy(d.d_name, fake_dir[fake_dir_offset++].c_str());
48   return &d;
49 }
fake_opendir(const char * path)50 static void* fake_opendir(const char* path) {
51   fake_dir_offset = 0;
52   if (strcmp(path, "/opendir-fail/") == 0) {
53     errno = EINVAL;
54     return nullptr;
55   }
56   return &fake_dir;
57 }
fake_lstat(const char *,struct stat *)58 static int fake_lstat(const char*, struct stat*) {
59   return 0;
60 }
fake_stat(const char *,struct stat *)61 static int fake_stat(const char*, struct stat*) {
62   return 0;
63 }
InstallFake(glob_t * g)64 static void InstallFake(glob_t* g) {
65   g->gl_closedir = fake_closedir;
66   g->gl_readdir = fake_readdir;
67   g->gl_opendir = fake_opendir;
68   g->gl_lstat = fake_lstat;
69   g->gl_stat = fake_stat;
70 }
71 #endif
72 
TEST(glob,glob_result_GLOB_NOMATCH)73 TEST(glob, glob_result_GLOB_NOMATCH) {
74   glob_t g = {};
75   ASSERT_EQ(GLOB_NOMATCH, glob("/will/match/nothing", 0, nullptr, &g));
76   ASSERT_EQ(0U, g.gl_pathc);
77   ASSERT_MATCH_COUNT(0U, g);
78 }
79 
TEST(glob,glob_GLOB_APPEND)80 TEST(glob, glob_GLOB_APPEND) {
81   glob_t g = {};
82   ASSERT_EQ(0, glob("/proc/version", 0, nullptr, &g));
83   ASSERT_EQ(1U, g.gl_pathc);
84   ASSERT_MATCH_COUNT(1U, g);
85   ASSERT_STREQ("/proc/version", g.gl_pathv[0]);
86   ASSERT_EQ(nullptr, g.gl_pathv[1]);
87   ASSERT_EQ(0, glob("/proc/version", GLOB_APPEND, nullptr, &g));
88   ASSERT_EQ(2U, g.gl_pathc);
89   ASSERT_MATCH_COUNT(1U, g);
90   ASSERT_STREQ("/proc/version", g.gl_pathv[0]);
91   ASSERT_STREQ("/proc/version", g.gl_pathv[1]);
92   ASSERT_EQ(nullptr, g.gl_pathv[2]);
93   globfree(&g);
94 }
95 
TEST(glob,glob_GLOB_DOOFFS)96 TEST(glob, glob_GLOB_DOOFFS) {
97   glob_t g = {};
98   g.gl_offs = 2;
99   ASSERT_EQ(0, glob("/proc/version", GLOB_DOOFFS, nullptr, &g));
100   ASSERT_EQ(1U, g.gl_pathc);
101   ASSERT_MATCH_COUNT(1U, g);
102   ASSERT_EQ(nullptr, g.gl_pathv[0]);
103   ASSERT_EQ(nullptr, g.gl_pathv[1]);
104   ASSERT_STREQ("/proc/version", g.gl_pathv[2]);
105   ASSERT_EQ(nullptr, g.gl_pathv[3]);
106   globfree(&g);
107 }
108 
109 #if !defined(ANDROID_HOST_MUSL)
110 static std::string g_failure_path;
111 static int g_failure_errno;
112 static int test_error_callback_result;
test_error_callback(const char * failure_path,int failure_errno)113 static int test_error_callback(const char* failure_path, int failure_errno) {
114   g_failure_path = failure_path;
115   g_failure_errno = failure_errno;
116   return test_error_callback_result;
117 }
118 #endif
119 
TEST(glob,glob_gl_errfunc)120 TEST(glob, glob_gl_errfunc) {
121 #if !defined(ANDROID_HOST_MUSL)
122   glob_t g = {};
123   InstallFake(&g);
124 
125   test_error_callback_result = 0;
126   g_failure_errno = 0;
127   ASSERT_EQ(GLOB_NOMATCH, glob("/opendir-fail/x*", GLOB_ALTDIRFUNC, test_error_callback, &g));
128   ASSERT_EQ("/opendir-fail/", g_failure_path);
129   ASSERT_EQ(EINVAL, g_failure_errno);
130 
131   test_error_callback_result = 1;
132   g_failure_errno = 0;
133   ASSERT_EQ(GLOB_ABORTED, glob("/opendir-fail/x*", GLOB_ALTDIRFUNC, test_error_callback, &g));
134   ASSERT_EQ("/opendir-fail/", g_failure_path);
135   ASSERT_EQ(EINVAL, g_failure_errno);
136 #else
137   GTEST_SKIP() << "musl doesn't support GLOB_ALTDIRFUNC";
138 #endif
139 }
140 
TEST(glob,glob_GLOB_ERR)141 TEST(glob, glob_GLOB_ERR) {
142 #if !defined(ANDROID_HOST_MUSL)
143   glob_t g = {};
144   InstallFake(&g);
145 
146   ASSERT_EQ(GLOB_NOMATCH, glob("/opendir-fail/x*", GLOB_ALTDIRFUNC, nullptr, &g));
147 
148   ASSERT_EQ(GLOB_ABORTED, glob("/opendir-fail/x*", GLOB_ALTDIRFUNC | GLOB_ERR, nullptr, &g));
149 #else
150   GTEST_SKIP() << "musl doesn't support GLOB_ALTDIRFUNC";
151 #endif
152 }
153 
TEST(glob,glob_GLOB_MARK)154 TEST(glob, glob_GLOB_MARK) {
155   TemporaryDir td;
156   // The pattern we're about to pass doesn't have a trailing '/'...
157   ASSERT_NE('/', std::string(td.path).back());
158 
159   glob_t g = {};
160   // Using GLOB_MARK gets you a trailing '/' on a directory...
161   ASSERT_EQ(0, glob(td.path, GLOB_MARK, nullptr, &g));
162   ASSERT_EQ(1U, g.gl_pathc);
163   ASSERT_MATCH_COUNT(1U, g);
164   ASSERT_EQ(std::string(td.path) + "/", g.gl_pathv[0]);
165   ASSERT_EQ(nullptr, g.gl_pathv[1]);
166 
167   TemporaryFile tf;
168   // But not on a file...
169   ASSERT_EQ(0, glob(tf.path, GLOB_MARK, nullptr, &g));
170   ASSERT_EQ(1U, g.gl_pathc);
171   ASSERT_MATCH_COUNT(1U, g);
172   ASSERT_STREQ(tf.path, g.gl_pathv[0]);
173   ASSERT_EQ(nullptr, g.gl_pathv[1]);
174 
175   globfree(&g);
176 }
177 
TEST(glob,glob_GLOB_NOCHECK)178 TEST(glob, glob_GLOB_NOCHECK) {
179   glob_t g = {};
180   ASSERT_EQ(0, glob("/will/match/nothing", GLOB_NOCHECK, nullptr, &g));
181   ASSERT_EQ(1U, g.gl_pathc);
182   ASSERT_MATCH_COUNT(0U, g);
183   ASSERT_STREQ("/will/match/nothing", g.gl_pathv[0]);
184   ASSERT_EQ(nullptr, g.gl_pathv[1]);
185   globfree(&g);
186 }
187 
TEST(glob,glob_GLOB_NOSORT)188 TEST(glob, glob_GLOB_NOSORT) {
189 #if !defined(ANDROID_HOST_MUSL)
190   fake_dir = { "c", "a", "d", "b" };
191 
192   glob_t g = {};
193   InstallFake(&g);
194 
195   ASSERT_EQ(0, glob("*", GLOB_ALTDIRFUNC, nullptr, &g));
196   ASSERT_EQ(4U, g.gl_pathc);
197   ASSERT_MATCH_COUNT(4U, g);
198   ASSERT_STREQ("a", g.gl_pathv[0]);
199   ASSERT_STREQ("b", g.gl_pathv[1]);
200   ASSERT_STREQ("c", g.gl_pathv[2]);
201   ASSERT_STREQ("d", g.gl_pathv[3]);
202   ASSERT_EQ(nullptr, g.gl_pathv[4]);
203 
204   ASSERT_EQ(0, glob("*", GLOB_ALTDIRFUNC | GLOB_NOSORT, nullptr, &g));
205   ASSERT_EQ(4U, g.gl_pathc);
206   ASSERT_MATCH_COUNT(4U, g);
207   ASSERT_STREQ("c", g.gl_pathv[0]);
208   ASSERT_STREQ("a", g.gl_pathv[1]);
209   ASSERT_STREQ("d", g.gl_pathv[2]);
210   ASSERT_STREQ("b", g.gl_pathv[3]);
211   ASSERT_EQ(nullptr, g.gl_pathv[4]);
212 #else
213   GTEST_SKIP() << "musl doesn't support GLOB_ALTDIRFUNC";
214 #endif
215 }
216 
TEST(glob,glob_GLOB_MAGCHAR)217 TEST(glob, glob_GLOB_MAGCHAR) {
218 #if !defined(ANDROID_HOST_MUSL)
219   glob_t g = {};
220   ASSERT_EQ(GLOB_NOMATCH, glob("/does-not-exist", 0, nullptr, &g));
221   ASSERT_TRUE((g.gl_flags & GLOB_MAGCHAR) == 0);
222   ASSERT_EQ(GLOB_NOMATCH, glob("/does-not-exist*", 0, nullptr, &g));
223   ASSERT_TRUE((g.gl_flags & GLOB_MAGCHAR) != 0);
224 
225   // We can lie, but glob(3) will turn that into truth...
226   ASSERT_EQ(GLOB_NOMATCH, glob("/does-not-exist", GLOB_MAGCHAR, nullptr, &g));
227   ASSERT_TRUE((g.gl_flags & GLOB_MAGCHAR) == 0);
228 #else
229   GTEST_SKIP() << "musl doesn't support GLOB_MAGCHAR";
230 #endif
231 }
232 
233 #if !defined(ANDROID_HOST_MUSL)
CheckGlob(const char * pattern,const std::vector<std::string> & expected_matches)234 static void CheckGlob(const char* pattern, const std::vector<std::string>& expected_matches) {
235   glob_t g = {};
236   InstallFake(&g);
237 
238   int expected_result = expected_matches.empty() ? GLOB_NOMATCH : 0;
239   ASSERT_EQ(expected_result, glob(pattern, GLOB_ALTDIRFUNC, nullptr, &g)) << pattern;
240   ASSERT_EQ(expected_matches.size(), g.gl_pathc);
241   ASSERT_MATCH_COUNT(expected_matches.size(), g);
242   for (size_t i = 0; i < expected_matches.size(); ++i) {
243     ASSERT_EQ(expected_matches[i], g.gl_pathv[i]);
244   }
245   if (!expected_matches.empty()) {
246     ASSERT_EQ(nullptr, g.gl_pathv[expected_matches.size()]);
247   }
248   globfree(&g);
249 }
250 #endif
251 
TEST(glob,glob_globbing)252 TEST(glob, glob_globbing) {
253 #if !defined(ANDROID_HOST_MUSL)
254   fake_dir = { "f1", "f2", "f30", "f40" };
255 
256   CheckGlob("f?", { "f1", "f2" });
257   CheckGlob("f??", { "f30", "f40" });
258   CheckGlob("f*", { "f1", "f2", "f30", "f40" });
259 #else
260   GTEST_SKIP() << "musl doesn't support GLOB_ALTDIRFUNC";
261 #endif
262 }
263 
TEST(glob,glob_globbing_rsc)264 TEST(glob, glob_globbing_rsc) {
265 #if !defined(ANDROID_HOST_MUSL)
266   // https://research.swtch.com/glob
267   fake_dir = { "axbxcxdxe" };
268   CheckGlob("a*b*c*d*e*", { "axbxcxdxe" });
269   fake_dir = { "axbxcxdxexxx" };
270   CheckGlob("a*b*c*d*e*", { "axbxcxdxexxx" });
271   fake_dir = { "abxbbxdbxebxczzx" };
272   CheckGlob("a*b?c*x", { "abxbbxdbxebxczzx" });
273   fake_dir = { "abxbbxdbxebxczzy" };
274   CheckGlob("a*b?c*x", {});
275 
276   fake_dir = { std::string(100, 'a') };
277   CheckGlob("a*a*a*a*b", {});
278 #else
279   GTEST_SKIP() << "musl doesn't support GLOB_ALTDIRFUNC";
280 #endif
281 }
282