Commit | Line | Data |
---|---|---|
93bed9fe | 1 | /* |
9d16b343 | 2 | * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
93bed9fe | 3 | * |
9d16b343 | 4 | * SPDX-License-Identifier: GPL-2.0-only |
93bed9fe | 5 | * |
93bed9fe JG |
6 | */ |
7 | ||
ce75e27a | 8 | #include <fcntl.h> |
93bed9fe | 9 | #include <stdbool.h> |
ce75e27a JG |
10 | #include <stdio.h> |
11 | #include <stdlib.h> | |
12 | #include <string.h> | |
93bed9fe | 13 | #include <sys/stat.h> |
ce75e27a JG |
14 | #include <sys/types.h> |
15 | #include <unistd.h> | |
93bed9fe | 16 | |
93bed9fe | 17 | #include <common/compat/directory-handle.h> |
edf4b93e | 18 | #include <common/compat/errno.h> |
ce75e27a JG |
19 | #include <common/error.h> |
20 | #include <tap/tap.h> | |
93bed9fe | 21 | |
ce75e27a | 22 | #define TEST_COUNT 9 |
93bed9fe JG |
23 | |
24 | /* For error.h */ | |
25 | int lttng_opt_quiet = 1; | |
26 | int lttng_opt_verbose = 3; | |
27 | int lttng_opt_mi; | |
28 | ||
93bed9fe JG |
29 | #define DIR_CREATION_MODE (S_IRWXU | S_IRWXG) |
30 | ||
ce75e27a JG |
31 | /* |
32 | * Returns the number of tests that ran (irrespective of the result) or a | |
33 | * negative value on error (will abort all tests). | |
34 | */ | |
35 | typedef int(test_func)(const char *test_base_path); | |
36 | ||
37 | static test_func test_rmdir_fail_non_empty; | |
38 | static test_func test_rmdir_skip_non_empty; | |
39 | ||
40 | static test_func *const test_funcs[] = { | |
41 | &test_rmdir_fail_non_empty, | |
42 | &test_rmdir_skip_non_empty, | |
43 | }; | |
44 | ||
45 | static bool dir_exists(const char *path) | |
93bed9fe JG |
46 | { |
47 | int ret; | |
48 | struct stat st; | |
49 | ||
50 | ret = stat(path, &st); | |
51 | return ret == 0 && S_ISDIR(st.st_mode); | |
52 | } | |
53 | ||
ce75e27a JG |
54 | /* |
55 | * Create a non-empty folder hierarchy from a directory handle: | |
56 | * | |
57 | * test_root_name | |
58 | * └── a | |
59 | * └── b | |
60 | * ├── c | |
61 | * │ └── d | |
62 | * └── e | |
63 | * ├── f | |
64 | * └── file1 | |
65 | */ | |
66 | static int create_non_empty_hierarchy_with_root( | |
67 | struct lttng_directory_handle *test_dir_handle, | |
68 | const char *test_root_name) | |
69 | { | |
70 | int ret; | |
71 | const int file_flags = O_WRONLY | O_CREAT | O_TRUNC; | |
72 | const mode_t file_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; | |
73 | char *branch_name = NULL; | |
74 | ||
75 | ret = asprintf(&branch_name, "%s/%s", test_root_name, "a/b/c/d"); | |
76 | if (ret < 0) { | |
77 | diag("Failed to format folder path"); | |
78 | goto end; | |
79 | } | |
80 | ret = lttng_directory_handle_create_subdirectory_recursive( | |
81 | test_dir_handle, | |
82 | branch_name, | |
83 | DIR_CREATION_MODE); | |
84 | if (ret) { | |
85 | diag("Failed to create test folder hierarchy %s", branch_name); | |
86 | goto end; | |
87 | } | |
88 | ||
89 | free(branch_name); | |
90 | ret = asprintf(&branch_name, "%s/%s", test_root_name, "a/b/e/f"); | |
91 | if (ret < 0) { | |
92 | diag("Failed to format folder path"); | |
93 | goto end; | |
94 | } | |
95 | ret = lttng_directory_handle_create_subdirectory_recursive( | |
96 | test_dir_handle, | |
97 | branch_name, | |
98 | DIR_CREATION_MODE); | |
99 | if (ret) { | |
100 | diag("Failed to create test folder hierarchy %s", branch_name); | |
101 | goto end; | |
102 | } | |
103 | ||
104 | free(branch_name); | |
105 | ret = asprintf(&branch_name, "%s/%s", test_root_name, "a/b/e/file1"); | |
106 | if (ret < 0) { | |
107 | diag("Failed to format file path"); | |
108 | goto end; | |
109 | } | |
110 | ret = lttng_directory_handle_open_file( | |
111 | test_dir_handle, branch_name, file_flags, file_mode); | |
112 | if (ret < 0) { | |
113 | diag("Failed to create file %s", branch_name); | |
114 | goto end; | |
115 | } | |
116 | ret = close(ret); | |
117 | if (ret) { | |
118 | PERROR("Failed to close fd to newly created file %s", | |
119 | branch_name); | |
120 | goto end; | |
121 | } | |
122 | end: | |
123 | free(branch_name); | |
124 | return ret; | |
125 | } | |
126 | ||
127 | /* Remove "file1" from the test folder hierarchy. */ | |
a815df68 | 128 | static |
ce75e27a JG |
129 | int remove_file_from_hierarchy(struct lttng_directory_handle *test_dir_handle, |
130 | const char *test_root_name) | |
93bed9fe JG |
131 | { |
132 | int ret; | |
ce75e27a JG |
133 | char *file_name = NULL; |
134 | ||
135 | ret = asprintf(&file_name, "%s/%s", test_root_name, "a/b/e/file1"); | |
136 | if (ret < 0) { | |
137 | diag("Failed to format file path"); | |
138 | goto end; | |
139 | } | |
140 | ||
141 | ret = lttng_directory_handle_unlink_file(test_dir_handle, | |
142 | file_name); | |
143 | if (ret) { | |
144 | PERROR("Failed to unlink file %s", file_name); | |
145 | goto end; | |
146 | } | |
147 | end: | |
148 | free(file_name); | |
149 | return ret; | |
150 | } | |
151 | ||
152 | static int test_rmdir_fail_non_empty(const char *test_dir) | |
153 | { | |
154 | int ret, tests_ran = 0; | |
cbf53d23 | 155 | struct lttng_directory_handle *test_dir_handle; |
93bed9fe | 156 | char *created_dir = NULL; |
ce75e27a JG |
157 | const char test_root_name[] = "fail_non_empty"; |
158 | char *test_dir_path = NULL; | |
93bed9fe | 159 | |
ce75e27a | 160 | diag("rmdir (fail if non-empty)"); |
93bed9fe | 161 | |
cbf53d23 JG |
162 | test_dir_handle = lttng_directory_handle_create(test_dir); |
163 | ok(test_dir_handle, "Initialized directory handle from the test directory"); | |
ce75e27a | 164 | tests_ran++; |
cbf53d23 JG |
165 | if (!test_dir_handle) { |
166 | ret = -1; | |
ce75e27a JG |
167 | goto end; |
168 | } | |
93bed9fe | 169 | |
cbf53d23 | 170 | ret = create_non_empty_hierarchy_with_root(test_dir_handle, test_root_name); |
ce75e27a JG |
171 | if (ret) { |
172 | diag("Failed to setup folder/file hierarchy to run test"); | |
173 | goto end; | |
174 | } | |
175 | ||
176 | ret = lttng_directory_handle_remove_subdirectory_recursive( | |
cbf53d23 | 177 | test_dir_handle, test_root_name, |
ce75e27a JG |
178 | LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG); |
179 | ok(ret == -1, "Error returned when attempting to recursively remove non-empty hierarchy with LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG"); | |
180 | tests_ran++; | |
181 | ||
cbf53d23 | 182 | ret = remove_file_from_hierarchy(test_dir_handle, test_root_name); |
ce75e27a JG |
183 | if (ret) { |
184 | diag("Failed to remove file from test folder hierarchy"); | |
185 | goto end; | |
186 | } | |
187 | ||
188 | ret = lttng_directory_handle_remove_subdirectory_recursive( | |
cbf53d23 | 189 | test_dir_handle, test_root_name, |
ce75e27a JG |
190 | LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG); |
191 | ok(ret == 0, "No error returned when recursively removing empty hierarchy with LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG"); | |
192 | tests_ran++; | |
193 | ||
194 | ret = asprintf(&test_dir_path, "%s/%s", test_dir, test_root_name); | |
195 | if (ret < 0) { | |
196 | diag("Failed to format test directory path"); | |
93bed9fe JG |
197 | goto end; |
198 | } | |
ce75e27a JG |
199 | ok(!dir_exists(test_dir_path) && errno == ENOENT, |
200 | "Folder hierarchy %s successfully removed", | |
201 | test_dir_path); | |
202 | tests_ran++; | |
203 | ret = 0; | |
204 | end: | |
cbf53d23 | 205 | lttng_directory_handle_put(test_dir_handle); |
ce75e27a JG |
206 | free(created_dir); |
207 | free(test_dir_path); | |
208 | return ret == 0 ? tests_ran : ret; | |
209 | } | |
210 | ||
211 | static int test_rmdir_skip_non_empty(const char *test_dir) | |
212 | { | |
213 | int ret, tests_ran = 0; | |
cbf53d23 | 214 | struct lttng_directory_handle *test_dir_handle; |
ce75e27a JG |
215 | char *created_dir = NULL; |
216 | const char test_root_name[] = "skip_non_empty"; | |
217 | char *test_dir_path = NULL; | |
218 | ||
219 | diag("rmdir (skip if non-empty)"); | |
93bed9fe | 220 | |
cbf53d23 JG |
221 | test_dir_handle = lttng_directory_handle_create(test_dir); |
222 | ok(test_dir_handle, "Initialized directory handle from the test directory"); | |
ce75e27a | 223 | tests_ran++; |
cbf53d23 JG |
224 | if (!test_dir_handle) { |
225 | ret = -1; | |
93bed9fe JG |
226 | goto end; |
227 | } | |
228 | ||
cbf53d23 | 229 | ret = create_non_empty_hierarchy_with_root(test_dir_handle, test_root_name); |
ce75e27a JG |
230 | if (ret) { |
231 | diag("Failed to setup folder/file hierarchy to run test"); | |
232 | goto end; | |
233 | } | |
234 | ||
235 | ret = lttng_directory_handle_remove_subdirectory_recursive( | |
cbf53d23 | 236 | test_dir_handle, test_root_name, |
ce75e27a JG |
237 | LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG); |
238 | ok(ret == 0, "No error returned when attempting to recursively remove non-empty hierarchy with LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG"); | |
239 | tests_ran++; | |
240 | ||
241 | ret = asprintf(&test_dir_path, "%s/%s", test_dir, test_root_name); | |
93bed9fe | 242 | if (ret < 0) { |
ce75e27a JG |
243 | diag("Failed to format test directory path"); |
244 | goto end; | |
245 | } | |
246 | ok(dir_exists(test_dir_path), "Test directory still exists after skip"); | |
247 | tests_ran++; | |
248 | ||
cbf53d23 | 249 | ret = remove_file_from_hierarchy(test_dir_handle, test_root_name); |
ce75e27a JG |
250 | if (ret) { |
251 | diag("Failed to remove file from test folder hierarchy"); | |
93bed9fe JG |
252 | goto end; |
253 | } | |
93bed9fe JG |
254 | |
255 | ret = lttng_directory_handle_remove_subdirectory_recursive( | |
cbf53d23 | 256 | test_dir_handle, test_root_name, |
ce75e27a JG |
257 | LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG); |
258 | ok(ret == 0, "No error returned when recursively removing empty hierarchy with LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG"); | |
259 | tests_ran++; | |
260 | ||
261 | ok(!dir_exists(test_dir_path) && errno == ENOENT, | |
262 | "Folder hierarchy %s successfully removed", | |
263 | test_dir_path); | |
264 | tests_ran++; | |
265 | ret = 0; | |
266 | end: | |
cbf53d23 | 267 | lttng_directory_handle_put(test_dir_handle); |
ce75e27a JG |
268 | free(created_dir); |
269 | free(test_dir_path); | |
270 | return ret == 0 ? tests_ran : ret; | |
271 | } | |
272 | ||
f46376a1 | 273 | int main(void) |
ce75e27a JG |
274 | { |
275 | int ret; | |
276 | char test_dir[] = "/tmp/lttng-XXXXXX"; | |
277 | int tests_left = TEST_COUNT; | |
278 | size_t func_idx; | |
279 | ||
280 | plan_tests(TEST_COUNT); | |
281 | ||
282 | diag("lttng_directory_handle tests"); | |
283 | ||
284 | if (!mkdtemp(test_dir)) { | |
285 | diag("Failed to generate temporary test directory"); | |
286 | goto end; | |
287 | } | |
288 | ||
289 | for (func_idx = 0; func_idx < sizeof(test_funcs) / sizeof(*test_funcs); | |
290 | func_idx++) { | |
291 | tests_left -= test_funcs[func_idx](test_dir); | |
292 | } | |
293 | if (tests_left) { | |
294 | diag("Skipping %d tests that could not be executed due to a prior error", | |
295 | tests_left); | |
296 | skip(tests_left, "test due to an error"); | |
297 | } | |
93bed9fe JG |
298 | end: |
299 | ret = rmdir(test_dir); | |
300 | if (ret) { | |
301 | diag("Failed to clean-up test directory: %s", strerror(errno)); | |
302 | } | |
93bed9fe JG |
303 | return exit_status(); |
304 | } |