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