Introduce libcommon-lgpl for liblttng-ctl
[lttng-tools.git] / tests / unit / test_directory_handle.cpp
1 /*
2 * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include <fcntl.h>
9 #include <stdbool.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/stat.h>
14 #include <sys/types.h>
15 #include <unistd.h>
16
17 #include <common/compat/directory-handle.h>
18 #include <common/compat/errno.h>
19 #include <common/error.h>
20 #include <tap/tap.h>
21
22 #define TEST_COUNT 9
23
24 /* For error.h */
25 int lttng_opt_quiet = 1;
26 int lttng_opt_verbose = 3;
27 int lttng_opt_mi;
28
29 #define DIR_CREATION_MODE (S_IRWXU | S_IRWXG)
30
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)
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
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. */
128 static
129 int remove_file_from_hierarchy(struct lttng_directory_handle *test_dir_handle,
130 const char *test_root_name)
131 {
132 int ret;
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;
155 struct lttng_directory_handle *test_dir_handle;
156 char *created_dir = NULL;
157 const char test_root_name[] = "fail_non_empty";
158 char *test_dir_path = NULL;
159
160 diag("rmdir (fail if non-empty)");
161
162 test_dir_handle = lttng_directory_handle_create(test_dir);
163 ok(test_dir_handle, "Initialized directory handle from the test directory");
164 tests_ran++;
165 if (!test_dir_handle) {
166 ret = -1;
167 goto end;
168 }
169
170 ret = create_non_empty_hierarchy_with_root(test_dir_handle, test_root_name);
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(
177 test_dir_handle, test_root_name,
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
182 ret = remove_file_from_hierarchy(test_dir_handle, test_root_name);
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(
189 test_dir_handle, test_root_name,
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");
197 goto end;
198 }
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:
205 lttng_directory_handle_put(test_dir_handle);
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;
214 struct lttng_directory_handle *test_dir_handle;
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)");
220
221 test_dir_handle = lttng_directory_handle_create(test_dir);
222 ok(test_dir_handle, "Initialized directory handle from the test directory");
223 tests_ran++;
224 if (!test_dir_handle) {
225 ret = -1;
226 goto end;
227 }
228
229 ret = create_non_empty_hierarchy_with_root(test_dir_handle, test_root_name);
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(
236 test_dir_handle, test_root_name,
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);
242 if (ret < 0) {
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
249 ret = remove_file_from_hierarchy(test_dir_handle, test_root_name);
250 if (ret) {
251 diag("Failed to remove file from test folder hierarchy");
252 goto end;
253 }
254
255 ret = lttng_directory_handle_remove_subdirectory_recursive(
256 test_dir_handle, test_root_name,
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:
267 lttng_directory_handle_put(test_dir_handle);
268 free(created_dir);
269 free(test_dir_path);
270 return ret == 0 ? tests_ran : ret;
271 }
272
273 int main(int argc, char **argv)
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 }
298 end:
299 ret = rmdir(test_dir);
300 if (ret) {
301 diag("Failed to clean-up test directory: %s", strerror(errno));
302 }
303 return exit_status();
304 }
This page took 0.042483 seconds and 4 git commands to generate.