aboutsummaryrefslogtreecommitdiff
path: root/tests/glob_test.cpp
blob: 6ce5f2f647d69e642fc2502ffffed3852e9a9c32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <glob.h>

#include <dirent.h>
#include <sys/cdefs.h>

#include <gtest/gtest.h>

#include <string>
#include <vector>

#include <android-base/file.h>

#if defined(__BIONIC__)
#define ASSERT_MATCH_COUNT(n_,g_) ASSERT_EQ(n_, g_.gl_matchc)
#else
#define ASSERT_MATCH_COUNT(n_,g_)
#endif

//
// Helper for use with GLOB_ALTDIRFUNC to iterate over the elements of `fake_dir`.
//

#if !defined(ANDROID_HOST_MUSL)
static std::vector<std::string> fake_dir;
static size_t fake_dir_offset;
static void fake_closedir(void*) {
}
static dirent* fake_readdir(void*) {
  static dirent d;
  if (fake_dir_offset >= fake_dir.size()) return nullptr;
  strcpy(d.d_name, fake_dir[fake_dir_offset++].c_str());
  return &d;
}
static void* fake_opendir(const char* path) {
  fake_dir_offset = 0;
  if (strcmp(path, "/opendir-fail/") == 0) {
    errno = EINVAL;
    return nullptr;
  }
  return &fake_dir;
}
static int fake_lstat(const char*, struct stat*) {
  return 0;
}
static int fake_stat(const char*, struct stat*) {
  return 0;
}
static void InstallFake(glob_t* g) {
  g->gl_closedir = fake_closedir;
  g->gl_readdir = fake_readdir;
  g->gl_opendir = fake_opendir;
  g->gl_lstat = fake_lstat;
  g->gl_stat = fake_stat;
}
#endif

TEST(glob, glob_result_GLOB_NOMATCH) {
  glob_t g = {};
  ASSERT_EQ(GLOB_NOMATCH, glob("/will/match/nothing", 0, nullptr, &g));
  ASSERT_EQ(0U, g.gl_pathc);
  ASSERT_MATCH_COUNT(0U, g);
}

TEST(glob, glob_GLOB_APPEND) {
  glob_t g = {};
  ASSERT_EQ(0, glob("/proc/version", 0, nullptr, &g));
  ASSERT_EQ(1U, g.gl_pathc);
  ASSERT_MATCH_COUNT(1U, g);
  ASSERT_STREQ("/proc/version", g.gl_pathv[0]);
  ASSERT_EQ(nullptr, g.gl_pathv[1]);
  ASSERT_EQ(0, glob("/proc/version", GLOB_APPEND, nullptr, &g));
  ASSERT_EQ(2U, g.gl_pathc);
  ASSERT_MATCH_COUNT(1U, g);
  ASSERT_STREQ("/proc/version", g.gl_pathv[0]);
  ASSERT_STREQ("/proc/version", g.gl_pathv[1]);
  ASSERT_EQ(nullptr, g.gl_pathv[2]);
  globfree(&g);
}

TEST(glob, glob_GLOB_DOOFFS) {
  glob_t g = {};
  g.gl_offs = 2;
  ASSERT_EQ(0, glob("/proc/version", GLOB_DOOFFS, nullptr, &g));
  ASSERT_EQ(1U, g.gl_pathc);
  ASSERT_MATCH_COUNT(1U, g);
  ASSERT_EQ(nullptr, g.gl_pathv[0]);
  ASSERT_EQ(nullptr, g.gl_pathv[1]);
  ASSERT_STREQ("/proc/version", g.gl_pathv[2]);
  ASSERT_EQ(nullptr, g.gl_pathv[3]);
  globfree(&g);
}

#if !defined(ANDROID_HOST_MUSL)
static std::string g_failure_path;
static int g_failure_errno;
static int test_error_callback_result;
static int test_error_callback(const char* failure_path, int failure_errno) {
  g_failure_path = failure_path;
  g_failure_errno = failure_errno;
  return test_error_callback_result;
}
#endif

TEST(glob, glob_gl_errfunc) {
#if !defined(ANDROID_HOST_MUSL)
  glob_t g = {};
  InstallFake(&g);

  test_error_callback_result = 0;
  g_failure_errno = 0;
  ASSERT_EQ(GLOB_NOMATCH, glob("/opendir-fail/x*", GLOB_ALTDIRFUNC, test_error_callback, &g));
  ASSERT_EQ("/opendir-fail/", g_failure_path);
  ASSERT_EQ(EINVAL, g_failure_errno);

  test_error_callback_result = 1;
  g_failure_errno = 0;
  ASSERT_EQ(GLOB_ABORTED, glob("/opendir-fail/x*", GLOB_ALTDIRFUNC, test_error_callback, &g));
  ASSERT_EQ("/opendir-fail/", g_failure_path);
  ASSERT_EQ(EINVAL, g_failure_errno);
#else
  GTEST_SKIP() << "musl doesn't support GLOB_ALTDIRFUNC";
#endif
}

TEST(glob, glob_GLOB_ERR) {
#if !defined(ANDROID_HOST_MUSL)
  glob_t g = {};
  InstallFake(&g);

  ASSERT_EQ(GLOB_NOMATCH, glob("/opendir-fail/x*", GLOB_ALTDIRFUNC, nullptr, &g));

  ASSERT_EQ(GLOB_ABORTED, glob("/opendir-fail/x*", GLOB_ALTDIRFUNC | GLOB_ERR, nullptr, &g));
#else
  GTEST_SKIP() << "musl doesn't support GLOB_ALTDIRFUNC";
#endif
}

TEST(glob, glob_GLOB_MARK) {
  TemporaryDir td;
  // The pattern we're about to pass doesn't have a trailing '/'...
  ASSERT_NE('/', std::string(td.path).back());

  glob_t g = {};
  // Using GLOB_MARK gets you a trailing '/' on a directory...
  ASSERT_EQ(0, glob(td.path, GLOB_MARK, nullptr, &g));
  ASSERT_EQ(1U, g.gl_pathc);
  ASSERT_MATCH_COUNT(1U, g);
  ASSERT_EQ(std::string(td.path) + "/", g.gl_pathv[0]);
  ASSERT_EQ(nullptr, g.gl_pathv[1]);

  TemporaryFile tf;
  // But not on a file...
  ASSERT_EQ(0, glob(tf.path, GLOB_MARK, nullptr, &g));
  ASSERT_EQ(1U, g.gl_pathc);
  ASSERT_MATCH_COUNT(1U, g);
  ASSERT_STREQ(tf.path, g.gl_pathv[0]);
  ASSERT_EQ(nullptr, g.gl_pathv[1]);

  globfree(&g);
}

TEST(glob, glob_GLOB_NOCHECK) {
  glob_t g = {};
  ASSERT_EQ(0, glob("/will/match/nothing", GLOB_NOCHECK, nullptr, &g));
  ASSERT_EQ(1U, g.gl_pathc);
  ASSERT_MATCH_COUNT(0U, g);
  ASSERT_STREQ("/will/match/nothing", g.gl_pathv[0]);
  ASSERT_EQ(nullptr, g.gl_pathv[1]);
  globfree(&g);
}

TEST(glob, glob_GLOB_NOSORT) {
#if !defined(ANDROID_HOST_MUSL)
  fake_dir = { "c", "a", "d", "b" };

  glob_t g = {};
  InstallFake(&g);

  ASSERT_EQ(0, glob("*", GLOB_ALTDIRFUNC, nullptr, &g));
  ASSERT_EQ(4U, g.gl_pathc);
  ASSERT_MATCH_COUNT(4U, g);
  ASSERT_STREQ("a", g.gl_pathv[0]);
  ASSERT_STREQ("b", g.gl_pathv[1]);
  ASSERT_STREQ("c", g.gl_pathv[2]);
  ASSERT_STREQ("d", g.gl_pathv[3]);
  ASSERT_EQ(nullptr, g.gl_pathv[4]);

  ASSERT_EQ(0, glob("*", GLOB_ALTDIRFUNC | GLOB_NOSORT, nullptr, &g));
  ASSERT_EQ(4U, g.gl_pathc);
  ASSERT_MATCH_COUNT(4U, g);
  ASSERT_STREQ("c", g.gl_pathv[0]);
  ASSERT_STREQ("a", g.gl_pathv[1]);
  ASSERT_STREQ("d", g.gl_pathv[2]);
  ASSERT_STREQ("b", g.gl_pathv[3]);
  ASSERT_EQ(nullptr, g.gl_pathv[4]);
#else
  GTEST_SKIP() << "musl doesn't support GLOB_ALTDIRFUNC";
#endif
}

TEST(glob, glob_GLOB_MAGCHAR) {
#if !defined(ANDROID_HOST_MUSL)
  glob_t g = {};
  ASSERT_EQ(GLOB_NOMATCH, glob("/does-not-exist", 0, nullptr, &g));
  ASSERT_TRUE((g.gl_flags & GLOB_MAGCHAR) == 0);
  ASSERT_EQ(GLOB_NOMATCH, glob("/does-not-exist*", 0, nullptr, &g));
  ASSERT_TRUE((g.gl_flags & GLOB_MAGCHAR) != 0);

  // We can lie, but glob(3) will turn that into truth...
  ASSERT_EQ(GLOB_NOMATCH, glob("/does-not-exist", GLOB_MAGCHAR, nullptr, &g));
  ASSERT_TRUE((g.gl_flags & GLOB_MAGCHAR) == 0);
#else
  GTEST_SKIP() << "musl doesn't support GLOB_MAGCHAR";
#endif
}

#if !defined(ANDROID_HOST_MUSL)
static void CheckGlob(const char* pattern, const std::vector<std::string>& expected_matches) {
  glob_t g = {};
  InstallFake(&g);

  int expected_result = expected_matches.empty() ? GLOB_NOMATCH : 0;
  ASSERT_EQ(expected_result, glob(pattern, GLOB_ALTDIRFUNC, nullptr, &g)) << pattern;
  ASSERT_EQ(expected_matches.size(), g.gl_pathc);
  ASSERT_MATCH_COUNT(expected_matches.size(), g);
  for (size_t i = 0; i < expected_matches.size(); ++i) {
    ASSERT_EQ(expected_matches[i], g.gl_pathv[i]);
  }
  if (!expected_matches.empty()) {
    ASSERT_EQ(nullptr, g.gl_pathv[expected_matches.size()]);
  }
  globfree(&g);
}
#endif

TEST(glob, glob_globbing) {
#if !defined(ANDROID_HOST_MUSL)
  fake_dir = { "f1", "f2", "f30", "f40" };

  CheckGlob("f?", { "f1", "f2" });
  CheckGlob("f??", { "f30", "f40" });
  CheckGlob("f*", { "f1", "f2", "f30", "f40" });
#else
  GTEST_SKIP() << "musl doesn't support GLOB_ALTDIRFUNC";
#endif
}

TEST(glob, glob_globbing_rsc) {
#if !defined(ANDROID_HOST_MUSL)
  // https://research.swtch.com/glob
  fake_dir = { "axbxcxdxe" };
  CheckGlob("a*b*c*d*e*", { "axbxcxdxe" });
  fake_dir = { "axbxcxdxexxx" };
  CheckGlob("a*b*c*d*e*", { "axbxcxdxexxx" });
  fake_dir = { "abxbbxdbxebxczzx" };
  CheckGlob("a*b?c*x", { "abxbbxdbxebxczzx" });
  fake_dir = { "abxbbxdbxebxczzy" };
  CheckGlob("a*b?c*x", {});

  fake_dir = { std::string(100, 'a') };
  CheckGlob("a*a*a*a*b", {});
#else
  GTEST_SKIP() << "musl doesn't support GLOB_ALTDIRFUNC";
#endif
}