]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-glob-util.c
libudev: hide definition of struct udev_list from other libudev components
[thirdparty/systemd.git] / src / test / test-glob-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
6a4f4a0f
RC
2
3#include <fcntl.h>
48d7c648
ZJS
4#include <glob.h>
5#include <sys/stat.h>
6a4f4a0f
RC
6#include <unistd.h>
7
8#include "alloc-util.h"
48d7c648 9#include "dirent-util.h"
48d7c648 10#include "fs-util.h"
6a4f4a0f
RC
11#include "glob-util.h"
12#include "macro.h"
48d7c648 13#include "rm-rf.h"
e4de7287 14#include "tmpfile-util.h"
6a4f4a0f
RC
15
16static void test_glob_exists(void) {
17 char name[] = "/tmp/test-glob_exists.XXXXXX";
18 int fd = -1;
19 int r;
20
646853bd 21 fd = mkostemp_safe(name);
6a4f4a0f
RC
22 assert_se(fd >= 0);
23 close(fd);
24
25 r = glob_exists("/tmp/test-glob_exists*");
26 assert_se(r == 1);
27
28 r = unlink(name);
29 assert_se(r == 0);
30 r = glob_exists("/tmp/test-glob_exists*");
31 assert_se(r == 0);
32}
33
46dcfbbd 34static void closedir_wrapper(void* v) {
1fe10174
ZJS
35 (void) closedir(v);
36}
37
48d7c648
ZJS
38static void test_glob_no_dot(void) {
39 char template[] = "/tmp/test-glob-util.XXXXXXX";
40 const char *fn;
41
42 _cleanup_globfree_ glob_t g = {
46dcfbbd 43 .gl_closedir = closedir_wrapper,
48d7c648
ZJS
44 .gl_readdir = (struct dirent *(*)(void *)) readdir_no_dot,
45 .gl_opendir = (void *(*)(const char *)) opendir,
46 .gl_lstat = lstat,
47 .gl_stat = stat,
48 };
49
50 int r;
51
52 assert_se(mkdtemp(template));
53
54 fn = strjoina(template, "/*");
55 r = glob(fn, GLOB_NOSORT|GLOB_BRACE|GLOB_ALTDIRFUNC, NULL, &g);
56 assert_se(r == GLOB_NOMATCH);
57
58 fn = strjoina(template, "/.*");
59 r = glob(fn, GLOB_NOSORT|GLOB_BRACE|GLOB_ALTDIRFUNC, NULL, &g);
60 assert_se(r == GLOB_NOMATCH);
61
62 (void) rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL);
63}
64
65static void test_safe_glob(void) {
66 char template[] = "/tmp/test-glob-util.XXXXXXX";
67 const char *fn, *fn2, *fname;
68
69 _cleanup_globfree_ glob_t g = {};
70 int r;
71
72 assert_se(mkdtemp(template));
73
74 fn = strjoina(template, "/*");
75 r = safe_glob(fn, 0, &g);
76 assert_se(r == -ENOENT);
77
78 fn2 = strjoina(template, "/.*");
79 r = safe_glob(fn2, GLOB_NOSORT|GLOB_BRACE, &g);
80 assert_se(r == -ENOENT);
81
82 fname = strjoina(template, "/.foobar");
83 assert_se(touch(fname) == 0);
84
85 r = safe_glob(fn, 0, &g);
86 assert_se(r == -ENOENT);
87
88 r = safe_glob(fn2, GLOB_NOSORT|GLOB_BRACE, &g);
89 assert_se(r == 0);
90 assert_se(g.gl_pathc == 1);
91 assert_se(streq(g.gl_pathv[0], fname));
92 assert_se(g.gl_pathv[1] == NULL);
93
94 (void) rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL);
95}
96
6a4f4a0f
RC
97int main(void) {
98 test_glob_exists();
48d7c648
ZJS
99 test_glob_no_dot();
100 test_safe_glob();
6a4f4a0f
RC
101
102 return 0;
103}