Tests: fd-tracker: fix: leak of test paths
[lttng-tools.git] / tests / unit / test_directory_handle.c
CommitLineData
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
8#include <assert.h>
93bed9fe 9#include <errno.h>
ce75e27a 10#include <fcntl.h>
93bed9fe 11#include <stdbool.h>
ce75e27a
JG
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
93bed9fe 15#include <sys/stat.h>
ce75e27a
JG
16#include <sys/types.h>
17#include <unistd.h>
93bed9fe 18
93bed9fe 19#include <common/compat/directory-handle.h>
ce75e27a
JG
20#include <common/error.h>
21#include <tap/tap.h>
93bed9fe 22
ce75e27a 23#define TEST_COUNT 9
93bed9fe
JG
24
25/* For error.h */
26int lttng_opt_quiet = 1;
27int lttng_opt_verbose = 3;
28int lttng_opt_mi;
29
93bed9fe
JG
30#define DIR_CREATION_MODE (S_IRWXU | S_IRWXG)
31
ce75e27a
JG
32/*
33 * Returns the number of tests that ran (irrespective of the result) or a
34 * negative value on error (will abort all tests).
35 */
36typedef int(test_func)(const char *test_base_path);
37
38static test_func test_rmdir_fail_non_empty;
39static test_func test_rmdir_skip_non_empty;
40
41static test_func *const test_funcs[] = {
42 &test_rmdir_fail_non_empty,
43 &test_rmdir_skip_non_empty,
44};
45
46static bool dir_exists(const char *path)
93bed9fe
JG
47{
48 int ret;
49 struct stat st;
50
51 ret = stat(path, &st);
52 return ret == 0 && S_ISDIR(st.st_mode);
53}
54
ce75e27a
JG
55/*
56 * Create a non-empty folder hierarchy from a directory handle:
57 *
58 * test_root_name
59 * └── a
60 * └── b
61 * ├── c
62 * │   └── d
63 * └── e
64 * ├── f
65 * └── file1
66 */
67static int create_non_empty_hierarchy_with_root(
68 struct lttng_directory_handle *test_dir_handle,
69 const char *test_root_name)
70{
71 int ret;
72 const int file_flags = O_WRONLY | O_CREAT | O_TRUNC;
73 const mode_t file_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
74 char *branch_name = NULL;
75
76 ret = asprintf(&branch_name, "%s/%s", test_root_name, "a/b/c/d");
77 if (ret < 0) {
78 diag("Failed to format folder path");
79 goto end;
80 }
81 ret = lttng_directory_handle_create_subdirectory_recursive(
82 test_dir_handle,
83 branch_name,
84 DIR_CREATION_MODE);
85 if (ret) {
86 diag("Failed to create test folder hierarchy %s", branch_name);
87 goto end;
88 }
89
90 free(branch_name);
91 ret = asprintf(&branch_name, "%s/%s", test_root_name, "a/b/e/f");
92 if (ret < 0) {
93 diag("Failed to format folder path");
94 goto end;
95 }
96 ret = lttng_directory_handle_create_subdirectory_recursive(
97 test_dir_handle,
98 branch_name,
99 DIR_CREATION_MODE);
100 if (ret) {
101 diag("Failed to create test folder hierarchy %s", branch_name);
102 goto end;
103 }
104
105 free(branch_name);
106 ret = asprintf(&branch_name, "%s/%s", test_root_name, "a/b/e/file1");
107 if (ret < 0) {
108 diag("Failed to format file path");
109 goto end;
110 }
111 ret = lttng_directory_handle_open_file(
112 test_dir_handle, branch_name, file_flags, file_mode);
113 if (ret < 0) {
114 diag("Failed to create file %s", branch_name);
115 goto end;
116 }
117 ret = close(ret);
118 if (ret) {
119 PERROR("Failed to close fd to newly created file %s",
120 branch_name);
121 goto end;
122 }
123end:
124 free(branch_name);
125 return ret;
126}
127
128/* Remove "file1" from the test folder hierarchy. */
129int 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 }
147end:
148 free(file_name);
149 return ret;
150}
151
152static 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;
204end:
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
211static 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;
266end:
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
273int 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 }
93bed9fe
JG
298end:
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}
This page took 0.043151 seconds and 5 git commands to generate.