blob: 8bf35bfad5e94099d29b095b49329e249ff335f6 [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>
20#include <gtest/gtest.h>
21
22#include <string>
23#include <vector>
24
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080025#include <android-base/file.h>
Elliott Hughesf1c568d2017-09-26 17:09:07 -070026
27#if defined(__BIONIC__)
28#define ASSERT_MATCH_COUNT(n_,g_) ASSERT_EQ(n_, g_.gl_matchc)
29#else
30#define ASSERT_MATCH_COUNT(n_,g_)
31#endif
32
33//
34// Helper for use with GLOB_ALTDIRFUNC to iterate over the elements of `fake_dir`.
35//
36
Colin Cross7da20342021-07-28 11:18:11 -070037#if !defined(MUSL)
Elliott Hughesf1c568d2017-09-26 17:09:07 -070038static std::vector<std::string> fake_dir;
39static size_t fake_dir_offset;
40static void fake_closedir(void*) {
41}
42static dirent* fake_readdir(void*) {
43 static dirent d;
44 if (fake_dir_offset >= fake_dir.size()) return nullptr;
45 strcpy(d.d_name, fake_dir[fake_dir_offset++].c_str());
46 return &d;
47}
48static void* fake_opendir(const char* path) {
49 fake_dir_offset = 0;
50 if (strcmp(path, "/opendir-fail/") == 0) {
51 errno = EINVAL;
52 return nullptr;
53 }
54 return &fake_dir;
55}
56static int fake_lstat(const char*, struct stat*) {
57 return 0;
58}
59static int fake_stat(const char*, struct stat*) {
60 return 0;
61}
62static void InstallFake(glob_t* g) {
63 g->gl_closedir = fake_closedir;
64 g->gl_readdir = fake_readdir;
65 g->gl_opendir = fake_opendir;
66 g->gl_lstat = fake_lstat;
67 g->gl_stat = fake_stat;
68}
Colin Cross7da20342021-07-28 11:18:11 -070069#endif
Elliott Hughesf1c568d2017-09-26 17:09:07 -070070
71TEST(glob, glob_result_GLOB_NOMATCH) {
72 glob_t g = {};
73 ASSERT_EQ(GLOB_NOMATCH, glob("/will/match/nothing", 0, nullptr, &g));
74 ASSERT_EQ(0U, g.gl_pathc);
75 ASSERT_MATCH_COUNT(0U, g);
76}
77
78TEST(glob, glob_GLOB_APPEND) {
79 glob_t g = {};
80 ASSERT_EQ(0, glob("/proc/version", 0, nullptr, &g));
81 ASSERT_EQ(1U, g.gl_pathc);
82 ASSERT_MATCH_COUNT(1U, g);
83 ASSERT_STREQ("/proc/version", g.gl_pathv[0]);
84 ASSERT_EQ(nullptr, g.gl_pathv[1]);
85 ASSERT_EQ(0, glob("/proc/version", GLOB_APPEND, nullptr, &g));
86 ASSERT_EQ(2U, g.gl_pathc);
87 ASSERT_MATCH_COUNT(1U, g);
88 ASSERT_STREQ("/proc/version", g.gl_pathv[0]);
89 ASSERT_STREQ("/proc/version", g.gl_pathv[1]);
90 ASSERT_EQ(nullptr, g.gl_pathv[2]);
91 globfree(&g);
92}
93
94TEST(glob, glob_GLOB_DOOFFS) {
95 glob_t g = {};
96 g.gl_offs = 2;
97 ASSERT_EQ(0, glob("/proc/version", GLOB_DOOFFS, nullptr, &g));
98 ASSERT_EQ(1U, g.gl_pathc);
99 ASSERT_MATCH_COUNT(1U, g);
100 ASSERT_EQ(nullptr, g.gl_pathv[0]);
101 ASSERT_EQ(nullptr, g.gl_pathv[1]);
102 ASSERT_STREQ("/proc/version", g.gl_pathv[2]);
103 ASSERT_EQ(nullptr, g.gl_pathv[3]);
104 globfree(&g);
105}
106
Colin Cross7da20342021-07-28 11:18:11 -0700107#if !defined(MUSL)
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700108static std::string g_failure_path;
109static int g_failure_errno;
110static int test_error_callback_result;
111static int test_error_callback(const char* failure_path, int failure_errno) {
112 g_failure_path = failure_path;
113 g_failure_errno = failure_errno;
114 return test_error_callback_result;
115}
Colin Cross7da20342021-07-28 11:18:11 -0700116#endif
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700117
118TEST(glob, glob_gl_errfunc) {
Colin Cross7da20342021-07-28 11:18:11 -0700119#if !defined(MUSL)
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700120 glob_t g = {};
121 InstallFake(&g);
122
123 test_error_callback_result = 0;
124 g_failure_errno = 0;
125 ASSERT_EQ(GLOB_NOMATCH, glob("/opendir-fail/x*", GLOB_ALTDIRFUNC, test_error_callback, &g));
126 ASSERT_EQ("/opendir-fail/", g_failure_path);
127 ASSERT_EQ(EINVAL, g_failure_errno);
128
129 test_error_callback_result = 1;
130 g_failure_errno = 0;
131 ASSERT_EQ(GLOB_ABORTED, glob("/opendir-fail/x*", GLOB_ALTDIRFUNC, test_error_callback, &g));
132 ASSERT_EQ("/opendir-fail/", g_failure_path);
133 ASSERT_EQ(EINVAL, g_failure_errno);
Colin Cross7da20342021-07-28 11:18:11 -0700134#else
135 GTEST_SKIP() << "musl doesn't support GLOB_ALTDIRFUNC";
136#endif
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700137}
138
139TEST(glob, glob_GLOB_ERR) {
Colin Cross7da20342021-07-28 11:18:11 -0700140#if !defined(MUSL)
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700141 glob_t g = {};
142 InstallFake(&g);
143
144 ASSERT_EQ(GLOB_NOMATCH, glob("/opendir-fail/x*", GLOB_ALTDIRFUNC, nullptr, &g));
145
146 ASSERT_EQ(GLOB_ABORTED, glob("/opendir-fail/x*", GLOB_ALTDIRFUNC | GLOB_ERR, nullptr, &g));
Colin Cross7da20342021-07-28 11:18:11 -0700147#else
148 GTEST_SKIP() << "musl doesn't support GLOB_ALTDIRFUNC";
149#endif
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700150}
151
152TEST(glob, glob_GLOB_MARK) {
153 TemporaryDir td;
154 // The pattern we're about to pass doesn't have a trailing '/'...
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800155 ASSERT_NE('/', std::string(td.path).back());
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700156
157 glob_t g = {};
158 // Using GLOB_MARK gets you a trailing '/' on a directory...
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800159 ASSERT_EQ(0, glob(td.path, GLOB_MARK, nullptr, &g));
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700160 ASSERT_EQ(1U, g.gl_pathc);
161 ASSERT_MATCH_COUNT(1U, g);
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800162 ASSERT_EQ(std::string(td.path) + "/", g.gl_pathv[0]);
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700163 ASSERT_EQ(nullptr, g.gl_pathv[1]);
164
165 TemporaryFile tf;
166 // But not on a file...
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800167 ASSERT_EQ(0, glob(tf.path, GLOB_MARK, nullptr, &g));
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700168 ASSERT_EQ(1U, g.gl_pathc);
169 ASSERT_MATCH_COUNT(1U, g);
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800170 ASSERT_STREQ(tf.path, g.gl_pathv[0]);
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700171 ASSERT_EQ(nullptr, g.gl_pathv[1]);
172
173 globfree(&g);
174}
175
176TEST(glob, glob_GLOB_NOCHECK) {
177 glob_t g = {};
178 ASSERT_EQ(0, glob("/will/match/nothing", GLOB_NOCHECK, nullptr, &g));
179 ASSERT_EQ(1U, g.gl_pathc);
180 ASSERT_MATCH_COUNT(0U, g);
181 ASSERT_STREQ("/will/match/nothing", g.gl_pathv[0]);
182 ASSERT_EQ(nullptr, g.gl_pathv[1]);
183 globfree(&g);
184}
185
186TEST(glob, glob_GLOB_NOSORT) {
Colin Cross7da20342021-07-28 11:18:11 -0700187#if !defined(MUSL)
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700188 fake_dir = { "c", "a", "d", "b" };
189
190 glob_t g = {};
191 InstallFake(&g);
192
193 ASSERT_EQ(0, glob("*", GLOB_ALTDIRFUNC, nullptr, &g));
194 ASSERT_EQ(4U, g.gl_pathc);
195 ASSERT_MATCH_COUNT(4U, g);
196 ASSERT_STREQ("a", g.gl_pathv[0]);
197 ASSERT_STREQ("b", g.gl_pathv[1]);
198 ASSERT_STREQ("c", g.gl_pathv[2]);
199 ASSERT_STREQ("d", g.gl_pathv[3]);
200 ASSERT_EQ(nullptr, g.gl_pathv[4]);
201
202 ASSERT_EQ(0, glob("*", GLOB_ALTDIRFUNC | GLOB_NOSORT, nullptr, &g));
203 ASSERT_EQ(4U, g.gl_pathc);
204 ASSERT_MATCH_COUNT(4U, g);
205 ASSERT_STREQ("c", g.gl_pathv[0]);
206 ASSERT_STREQ("a", g.gl_pathv[1]);
207 ASSERT_STREQ("d", g.gl_pathv[2]);
208 ASSERT_STREQ("b", g.gl_pathv[3]);
209 ASSERT_EQ(nullptr, g.gl_pathv[4]);
Colin Cross7da20342021-07-28 11:18:11 -0700210#else
211 GTEST_SKIP() << "musl doesn't support GLOB_ALTDIRFUNC";
212#endif
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700213}
214
215TEST(glob, glob_GLOB_MAGCHAR) {
Colin Cross7da20342021-07-28 11:18:11 -0700216#if !defined(MUSL)
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700217 glob_t g = {};
218 ASSERT_EQ(GLOB_NOMATCH, glob("/does-not-exist", 0, nullptr, &g));
219 ASSERT_TRUE((g.gl_flags & GLOB_MAGCHAR) == 0);
220 ASSERT_EQ(GLOB_NOMATCH, glob("/does-not-exist*", 0, nullptr, &g));
221 ASSERT_TRUE((g.gl_flags & GLOB_MAGCHAR) != 0);
222
223 // We can lie, but glob(3) will turn that into truth...
224 ASSERT_EQ(GLOB_NOMATCH, glob("/does-not-exist", GLOB_MAGCHAR, nullptr, &g));
225 ASSERT_TRUE((g.gl_flags & GLOB_MAGCHAR) == 0);
Colin Cross7da20342021-07-28 11:18:11 -0700226#else
227 GTEST_SKIP() << "musl doesn't support GLOB_MAGCHAR";
228#endif
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700229}
230
Colin Cross7da20342021-07-28 11:18:11 -0700231#if !defined(MUSL)
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700232static void CheckGlob(const char* pattern, const std::vector<std::string>& expected_matches) {
233 glob_t g = {};
234 InstallFake(&g);
235
236 int expected_result = expected_matches.empty() ? GLOB_NOMATCH : 0;
237 ASSERT_EQ(expected_result, glob(pattern, GLOB_ALTDIRFUNC, nullptr, &g)) << pattern;
238 ASSERT_EQ(expected_matches.size(), g.gl_pathc);
239 ASSERT_MATCH_COUNT(expected_matches.size(), g);
240 for (size_t i = 0; i < expected_matches.size(); ++i) {
241 ASSERT_EQ(expected_matches[i], g.gl_pathv[i]);
242 }
243 if (!expected_matches.empty()) {
244 ASSERT_EQ(nullptr, g.gl_pathv[expected_matches.size()]);
245 }
246 globfree(&g);
247}
Colin Cross7da20342021-07-28 11:18:11 -0700248#endif
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700249
250TEST(glob, glob_globbing) {
Colin Cross7da20342021-07-28 11:18:11 -0700251#if !defined(MUSL)
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700252 fake_dir = { "f1", "f2", "f30", "f40" };
253
254 CheckGlob("f?", { "f1", "f2" });
255 CheckGlob("f??", { "f30", "f40" });
256 CheckGlob("f*", { "f1", "f2", "f30", "f40" });
Colin Cross7da20342021-07-28 11:18:11 -0700257#else
258 GTEST_SKIP() << "musl doesn't support GLOB_ALTDIRFUNC";
259#endif
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700260}
261
262TEST(glob, glob_globbing_rsc) {
Colin Cross7da20342021-07-28 11:18:11 -0700263#if !defined(MUSL)
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700264 // https://research.swtch.com/glob
265 fake_dir = { "axbxcxdxe" };
266 CheckGlob("a*b*c*d*e*", { "axbxcxdxe" });
267 fake_dir = { "axbxcxdxexxx" };
268 CheckGlob("a*b*c*d*e*", { "axbxcxdxexxx" });
269 fake_dir = { "abxbbxdbxebxczzx" };
270 CheckGlob("a*b?c*x", { "abxbbxdbxebxczzx" });
271 fake_dir = { "abxbbxdbxebxczzy" };
272 CheckGlob("a*b?c*x", {});
273
274 fake_dir = { std::string(100, 'a') };
275 CheckGlob("a*a*a*a*b", {});
Colin Cross7da20342021-07-28 11:18:11 -0700276#else
277 GTEST_SKIP() << "musl doesn't support GLOB_ALTDIRFUNC";
278#endif
Elliott Hughesf1c568d2017-09-26 17:09:07 -0700279}