Make lttng_directory_handle reference countable
[lttng-tools.git] / tests / unit / test_directory_handle.c
CommitLineData
93bed9fe
JG
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>
93bed9fe 19#include <errno.h>
ce75e27a 20#include <fcntl.h>
93bed9fe 21#include <stdbool.h>
ce75e27a
JG
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
93bed9fe 25#include <sys/stat.h>
ce75e27a
JG
26#include <sys/types.h>
27#include <unistd.h>
93bed9fe 28
93bed9fe 29#include <common/compat/directory-handle.h>
ce75e27a
JG
30#include <common/error.h>
31#include <tap/tap.h>
93bed9fe 32
ce75e27a 33#define TEST_COUNT 9
93bed9fe
JG
34
35/* For error.h */
36int lttng_opt_quiet = 1;
37int lttng_opt_verbose = 3;
38int lttng_opt_mi;
39
93bed9fe
JG
40#define DIR_CREATION_MODE (S_IRWXU | S_IRWXG)
41
ce75e27a
JG
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 */
46typedef int(test_func)(const char *test_base_path);
47
48static test_func test_rmdir_fail_non_empty;
49static test_func test_rmdir_skip_non_empty;
50
51static test_func *const test_funcs[] = {
52 &test_rmdir_fail_non_empty,
53 &test_rmdir_skip_non_empty,
54};
55
56static bool dir_exists(const char *path)
93bed9fe
JG
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
ce75e27a
JG
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 */
77static 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 }
133end:
134 free(branch_name);
135 return ret;
136}
137
138/* Remove "file1" from the test folder hierarchy. */
139int remove_file_from_hierarchy(struct lttng_directory_handle *test_dir_handle,
140 const char *test_root_name)
93bed9fe
JG
141{
142 int ret;
ce75e27a
JG
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 }
157end:
158 free(file_name);
159 return ret;
160}
161
162static int test_rmdir_fail_non_empty(const char *test_dir)
163{
164 int ret, tests_ran = 0;
cbf53d23 165 struct lttng_directory_handle *test_dir_handle;
93bed9fe 166 char *created_dir = NULL;
ce75e27a
JG
167 const char test_root_name[] = "fail_non_empty";
168 char *test_dir_path = NULL;
93bed9fe 169
ce75e27a 170 diag("rmdir (fail if non-empty)");
93bed9fe 171
cbf53d23
JG
172 test_dir_handle = lttng_directory_handle_create(test_dir);
173 ok(test_dir_handle, "Initialized directory handle from the test directory");
ce75e27a 174 tests_ran++;
cbf53d23
JG
175 if (!test_dir_handle) {
176 ret = -1;
ce75e27a
JG
177 goto end;
178 }
93bed9fe 179
cbf53d23 180 ret = create_non_empty_hierarchy_with_root(test_dir_handle, test_root_name);
ce75e27a
JG
181 if (ret) {
182 diag("Failed to setup folder/file hierarchy to run test");
183 goto end;
184 }
185
186 ret = lttng_directory_handle_remove_subdirectory_recursive(
cbf53d23 187 test_dir_handle, test_root_name,
ce75e27a
JG
188 LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG);
189 ok(ret == -1, "Error returned when attempting to recursively remove non-empty hierarchy with LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG");
190 tests_ran++;
191
cbf53d23 192 ret = remove_file_from_hierarchy(test_dir_handle, test_root_name);
ce75e27a
JG
193 if (ret) {
194 diag("Failed to remove file from test folder hierarchy");
195 goto end;
196 }
197
198 ret = lttng_directory_handle_remove_subdirectory_recursive(
cbf53d23 199 test_dir_handle, test_root_name,
ce75e27a
JG
200 LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG);
201 ok(ret == 0, "No error returned when recursively removing empty hierarchy with LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG");
202 tests_ran++;
203
204 ret = asprintf(&test_dir_path, "%s/%s", test_dir, test_root_name);
205 if (ret < 0) {
206 diag("Failed to format test directory path");
93bed9fe
JG
207 goto end;
208 }
ce75e27a
JG
209 ok(!dir_exists(test_dir_path) && errno == ENOENT,
210 "Folder hierarchy %s successfully removed",
211 test_dir_path);
212 tests_ran++;
213 ret = 0;
214end:
cbf53d23 215 lttng_directory_handle_put(test_dir_handle);
ce75e27a
JG
216 free(created_dir);
217 free(test_dir_path);
218 return ret == 0 ? tests_ran : ret;
219}
220
221static int test_rmdir_skip_non_empty(const char *test_dir)
222{
223 int ret, tests_ran = 0;
cbf53d23 224 struct lttng_directory_handle *test_dir_handle;
ce75e27a
JG
225 char *created_dir = NULL;
226 const char test_root_name[] = "skip_non_empty";
227 char *test_dir_path = NULL;
228
229 diag("rmdir (skip if non-empty)");
93bed9fe 230
cbf53d23
JG
231 test_dir_handle = lttng_directory_handle_create(test_dir);
232 ok(test_dir_handle, "Initialized directory handle from the test directory");
ce75e27a 233 tests_ran++;
cbf53d23
JG
234 if (!test_dir_handle) {
235 ret = -1;
93bed9fe
JG
236 goto end;
237 }
238
cbf53d23 239 ret = create_non_empty_hierarchy_with_root(test_dir_handle, test_root_name);
ce75e27a
JG
240 if (ret) {
241 diag("Failed to setup folder/file hierarchy to run test");
242 goto end;
243 }
244
245 ret = lttng_directory_handle_remove_subdirectory_recursive(
cbf53d23 246 test_dir_handle, test_root_name,
ce75e27a
JG
247 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG);
248 ok(ret == 0, "No error returned when attempting to recursively remove non-empty hierarchy with LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG");
249 tests_ran++;
250
251 ret = asprintf(&test_dir_path, "%s/%s", test_dir, test_root_name);
93bed9fe 252 if (ret < 0) {
ce75e27a
JG
253 diag("Failed to format test directory path");
254 goto end;
255 }
256 ok(dir_exists(test_dir_path), "Test directory still exists after skip");
257 tests_ran++;
258
cbf53d23 259 ret = remove_file_from_hierarchy(test_dir_handle, test_root_name);
ce75e27a
JG
260 if (ret) {
261 diag("Failed to remove file from test folder hierarchy");
93bed9fe
JG
262 goto end;
263 }
93bed9fe
JG
264
265 ret = lttng_directory_handle_remove_subdirectory_recursive(
cbf53d23 266 test_dir_handle, test_root_name,
ce75e27a
JG
267 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG);
268 ok(ret == 0, "No error returned when recursively removing empty hierarchy with LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG");
269 tests_ran++;
270
271 ok(!dir_exists(test_dir_path) && errno == ENOENT,
272 "Folder hierarchy %s successfully removed",
273 test_dir_path);
274 tests_ran++;
275 ret = 0;
276end:
cbf53d23 277 lttng_directory_handle_put(test_dir_handle);
ce75e27a
JG
278 free(created_dir);
279 free(test_dir_path);
280 return ret == 0 ? tests_ran : ret;
281}
282
283int main(int argc, char **argv)
284{
285 int ret;
286 char test_dir[] = "/tmp/lttng-XXXXXX";
287 int tests_left = TEST_COUNT;
288 size_t func_idx;
289
290 plan_tests(TEST_COUNT);
291
292 diag("lttng_directory_handle tests");
293
294 if (!mkdtemp(test_dir)) {
295 diag("Failed to generate temporary test directory");
296 goto end;
297 }
298
299 for (func_idx = 0; func_idx < sizeof(test_funcs) / sizeof(*test_funcs);
300 func_idx++) {
301 tests_left -= test_funcs[func_idx](test_dir);
302 }
303 if (tests_left) {
304 diag("Skipping %d tests that could not be executed due to a prior error",
305 tests_left);
306 skip(tests_left, "test due to an error");
307 }
93bed9fe
JG
308end:
309 ret = rmdir(test_dir);
310 if (ret) {
311 diag("Failed to clean-up test directory: %s", strerror(errno));
312 }
93bed9fe
JG
313 return exit_status();
314}
This page took 0.036076 seconds and 4 git commands to generate.