summaryrefslogtreecommitdiffstats
path: root/src/test/test-dirent-util.c
blob: f9816973bfce632f566f1d02cdbea768a3d08357 (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
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>

#include "alloc-util.h"
#include "dirent-util.h"
#include "fs-util.h"
#include "mkdir.h"
#include "path-util.h"
#include "rm-rf.h"
#include "stat-util.h"
#include "string-util.h"
#include "tmpfile-util.h"
#include "tests.h"

TEST (test_dirent_ensure_type) {
        int r, dir_fd;
        static struct dirent de = {
                .d_type = DT_UNKNOWN,
                .d_name = "test",
        };

        assert_se(de.d_type == DT_UNKNOWN);

        dir_fd = 0;
        dirent_ensure_type(dir_fd, &de);

        /* Test when d_name is "." or ".." */
        strcpy(de.d_name, ".");
        r = dirent_ensure_type(dir_fd, &de);
        assert_se(r == 0);
        assert_se(de.d_type == DT_DIR);

        strcpy(de.d_name, "..");
        r = dirent_ensure_type(dir_fd, &de);
        assert_se(r == 0);
        assert_se(de.d_type == DT_DIR);
}

TEST (test_dirent_is_file) {
        _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
        const char *name, *dotfile, *name_alias, *bakfile, *tilda;
        const struct dirent *de_reg, *de_lnk, *de_dot, *de_bak, *de_tilda;
        DIR *dir;

        static const struct dirent de_unknown = {
                .d_type = DT_UNKNOWN,
                .d_name = "test_unknown",
        };

        assert_se(mkdtemp_malloc(NULL, &t) >= 0);

        name = strjoina(t, "/test.txt");
        dotfile = strjoina(t, "/.hidden_file");
        bakfile = strjoina(t, "/test.bak");
        tilda = strjoina(t, "/test~");
        name_alias = strjoina(t, "/test_link");

        assert_se(touch(name) >= 0);
        assert_se(touch(dotfile) >= 0);
        assert_se(touch(bakfile) >= 0);
        assert_se(touch(tilda) >= 0);

        if (symlink(name, name_alias) < 0) {
                assert_se(IN_SET(errno, EINVAL, ENOSYS, ENOTTY, EPERM));
                log_tests_skipped_errno(errno, "symlink() not possible");
        }

        dir = opendir(t);
        if (dir == NULL) {
                log_error_errno(errno, "Failed to open directory '%s': %m", t);
                exit(EXIT_FAILURE);
        }

        rewinddir(dir);
        while ((de_reg = readdir_ensure_type(dir)) != NULL)
                if (strcmp(de_reg->d_name, "test.txt") == 0)
                        break;

        rewinddir(dir);
        while ((de_lnk = readdir_ensure_type(dir)) != NULL)
                if (strcmp(de_lnk->d_name, "test_link") == 0)
                        break;

        rewinddir(dir);
        while ((de_dot = readdir_ensure_type(dir)) != NULL)
                if (strcmp(de_dot->d_name, ".hidden_file") == 0)
                        break;

        rewinddir(dir);
        while ((de_bak = readdir(dir)) != NULL)
                if (strcmp(de_bak->d_name, "test.bak") == 0)
                        break;

        rewinddir(dir);
        while ((de_tilda = readdir(dir)) != NULL)
                if (strcmp(de_tilda->d_name, "test~") == 0)
                        break;

        /* Test when d_type is DT_REG, DT_LNK, or DT_UNKNOWN */
        assert_se(dirent_is_file(de_reg) == true);
        if (de_lnk)
                assert_se(dirent_is_file(de_lnk) == true);
        else
                log_tests_skipped("de_lnk is NULL, skipping test");
        assert_se(dirent_is_file(&de_unknown) == true);

        /* Test for hidden files */
        assert_se(dirent_is_file(de_dot) == false);
        assert_se(dirent_is_file(de_bak) == false);
        assert_se(dirent_is_file(de_tilda) == false);

        closedir(dir);
}

TEST (test_dirent_is_file_with_suffix) {
        _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
        const char *name, *dotfile, *name_alias, *dotdot, *chr;
        const struct dirent *de_reg, *de_lnk, *de_dot, *de_dotdot, *de_chr;
        DIR *dir;

        static const struct dirent de_unknown = {
                .d_type = DT_UNKNOWN,
                .d_name = "test_unknown",
        };

        assert_se(mkdtemp_malloc(NULL, &t) >= 0);

        name = strjoina(t, "/test.txt");
        dotfile = strjoina(t, "/.hidden_file");
        dotdot = strjoina(t, "/..dotdot");
        chr = strjoina(t, "/test_chr");
        name_alias = strjoina(t, "/test_link");

        assert_se(touch(name) >= 0);
        assert_se(touch(dotfile) >= 0);
        assert_se(touch(dotdot) >= 0);
        assert_se(mknod(chr, 0775 | S_IFCHR, makedev(0, 0)) >= 0);

        if (symlink(name, name_alias) < 0) {
                assert_se(IN_SET(errno, EINVAL, ENOSYS, ENOTTY, EPERM));
                log_tests_skipped_errno(errno, "symlink() not possible");
        }

        dir = opendir(t);
        if (dir == NULL) {
                log_error_errno(errno, "Failed to open directory '%s': %m", t);
                exit(EXIT_FAILURE);
        }

        rewinddir(dir);
        while ((de_reg = readdir_ensure_type(dir)) != NULL)
                if (strcmp(de_reg->d_name, "test.txt") == 0)
                        break;

        rewinddir(dir);
        while ((de_lnk = readdir_ensure_type(dir)) != NULL)
                if (strcmp(de_lnk->d_name, "test_link") == 0)
                        break;

        rewinddir(dir);
        while ((de_dot = readdir_ensure_type(dir)) != NULL)
                if (strcmp(de_dot->d_name, ".hidden_file") == 0)
                        break;

        rewinddir(dir);
        while ((de_dotdot = readdir(dir)) != NULL)
                if (strcmp(de_dotdot->d_name, "..dotdot") == 0)
                        break;

        rewinddir(dir);
        while ((de_chr = readdir(dir)) != NULL)
                if (strcmp(de_chr->d_name, "test_chr") == 0)
                        break;

        /* Test when d_type is not DT_REG, DT_LNK, or DT_UNKNOWN */
        assert_se(!dirent_is_file_with_suffix(de_chr, NULL));

        /* Test when suffix is NULL */
        assert_se(dirent_is_file_with_suffix(de_reg, NULL) == true);
        if (de_lnk)
                assert_se(dirent_is_file_with_suffix(de_lnk, NULL) == true);
        else
                log_tests_skipped("de_lnk is NULL, skipping test");
        assert_se(dirent_is_file_with_suffix(&de_unknown, NULL) == true);

        /* Test for present suffix */
        assert_se(dirent_is_file_with_suffix(de_reg, "txt") == true);
        if (de_lnk)
                assert_se(dirent_is_file_with_suffix(de_lnk, "link") == true);
        else
                log_tests_skipped("de_lnk is NULL, skipping test");
        assert_se(dirent_is_file_with_suffix(&de_unknown, "unknown") == true);

        /* Test for absent suffix */
        assert_se(dirent_is_file_with_suffix(de_reg, "svg") == false);
        if (de_lnk)
                assert_se(dirent_is_file_with_suffix(de_lnk, "pdf") == false);
        else
                log_tests_skipped("de_lnk is NULL, skipping test");
        assert_se(dirent_is_file_with_suffix(&de_unknown, "yes") == false);

        /* Test for dot and dot-dot */
        assert_se(dirent_is_file_with_suffix(de_dot, NULL) == false);
        assert_se(dirent_is_file_with_suffix(de_dotdot, NULL) == false);

        closedir(dir);
}

DEFINE_TEST_MAIN(LOG_INFO);