blob: 6ce5f2f647d69e642fc2502ffffed3852e9a9c32 [file] [log] [blame]
Elliott Hughesf1c568d2017-09-26 17:09:07 -07001/*
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>
Colin Cross4c5595c2021-08-16 15:51:59 -070020#include <sys/cdefs.h>
21
Elliott Hughesf1c568d2017-09-26 17:09:07 -070022#include <gtest/gtest.h>
23
24#include <string>
25#include <vector>
26
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080027#include <android-base/file.h>
Elliott Hughesf1c568d2017-09-26 17:09:07 -070028
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
Colin Cross4c5595c2021-08-16 15:51:59 -070039#if !defined(ANDROID_HOST_MUSL)
Elliott Hughesf1c568d2017-09-26 17:09:07 -070040static std::vector<std::string> fake_dir;
41static size_t fake_dir_offset;
42static void fake_closedir(void*) {
43}
44static 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}
50static 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}
58static int fake_lstat(const char*, struct stat*) {
59 return 0;
60}
61static int fake_stat(const char*, struct stat*) {
62 return 0;
63}
64static 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}
Colin Cross7da20342021-07-28 11:18:11 -070071#endif
Elliott Hughesf1c568d2017-09-26 17:09:07 -070072
73TEST(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
80TEST(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
96TEST(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
Colin Cross4c5595c2021-08-16 15:51:59 -0700109#if !defined(ANDROID_HOST_MUSL)
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700110static std::string g_failure_path;
111static int g_failure_errno;
112static int test_error_callback_result;
113static 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}
Colin Cross7da20342021-07-28 11:18:11 -0700118#endif
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700119
120TEST(glob, glob_gl_errfunc) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700121#if !defined(ANDROID_HOST_MUSL)
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700122 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);
Colin Cross7da20342021-07-28 11:18:11 -0700136#else
137 GTEST_SKIP() << "musl doesn't support GLOB_ALTDIRFUNC";
138#endif
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700139}
140
141TEST(glob, glob_GLOB_ERR) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700142#if !defined(ANDROID_HOST_MUSL)
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700143 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));
Colin Cross7da20342021-07-28 11:18:11 -0700149#else
150 GTEST_SKIP() << "musl doesn't support GLOB_ALTDIRFUNC";
151#endif
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700152}
153
154TEST(glob, glob_GLOB_MARK) {
155 TemporaryDir td;
156 // The pattern we're about to pass doesn't have a trailing '/'...
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800157 ASSERT_NE('/', std::string(td.path).back());
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700158
159 glob_t g = {};
160 // Using GLOB_MARK gets you a trailing '/' on a directory...
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800161 ASSERT_EQ(0, glob(td.path, GLOB_MARK, nullptr, &g));
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700162 ASSERT_EQ(1U, g.gl_pathc);
163 ASSERT_MATCH_COUNT(1U, g);
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800164 ASSERT_EQ(std::string(td.path) + "/", g.gl_pathv[0]);
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700165 ASSERT_EQ(nullptr, g.gl_pathv[1]);
166
167 TemporaryFile tf;
168 // But not on a file...
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800169 ASSERT_EQ(0, glob(tf.path, GLOB_MARK, nullptr, &g));
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700170 ASSERT_EQ(1U, g.gl_pathc);
171 ASSERT_MATCH_COUNT(1U, g);
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800172 ASSERT_STREQ(tf.path, g.gl_pathv[0]);
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700173 ASSERT_EQ(nullptr, g.gl_pathv[1]);
174
175 globfree(&g);
176}
177
178TEST(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
188TEST(glob, glob_GLOB_NOSORT) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700189#if !defined(ANDROID_HOST_MUSL)
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700190 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]);
Colin Cross7da20342021-07-28 11:18:11 -0700212#else
213 GTEST_SKIP() << "musl doesn't support GLOB_ALTDIRFUNC";
214#endif
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700215}
216
217TEST(glob, glob_GLOB_MAGCHAR) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700218#if !defined(ANDROID_HOST_MUSL)
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700219 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);
Colin Cross7da20342021-07-28 11:18:11 -0700228#else
229 GTEST_SKIP() << "musl doesn't support GLOB_MAGCHAR";
230#endif
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700231}
232
Colin Cross4c5595c2021-08-16 15:51:59 -0700233#if !defined(ANDROID_HOST_MUSL)
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700234static 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}
Colin Cross7da20342021-07-28 11:18:11 -0700250#endif
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700251
252TEST(glob, glob_globbing) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700253#if !defined(ANDROID_HOST_MUSL)
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700254 fake_dir = { "f1", "f2", "f30", "f40" };
255
256 CheckGlob("f?", { "f1", "f2" });
257 CheckGlob("f??", { "f30", "f40" });
258 CheckGlob("f*", { "f1", "f2", "f30", "f40" });
Colin Cross7da20342021-07-28 11:18:11 -0700259#else
260 GTEST_SKIP() << "musl doesn't support GLOB_ALTDIRFUNC";
261#endif
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700262}
263
264TEST(glob, glob_globbing_rsc) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700265#if !defined(ANDROID_HOST_MUSL)
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700266 // 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", {});
Colin Cross7da20342021-07-28 11:18:11 -0700278#else
279 GTEST_SKIP() << "musl doesn't support GLOB_ALTDIRFUNC";
280#endif
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700281}