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