Tests: remove leftover debug printing in test_add_trigger_cli
[lttng-tools.git] / tests / unit / test_directory_handle.cpp
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
28ab034a
JG
8#include <common/compat/directory-handle.hpp>
9#include <common/compat/errno.hpp>
10#include <common/error.hpp>
11
ce75e27a 12#include <fcntl.h>
93bed9fe 13#include <stdbool.h>
ce75e27a
JG
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
93bed9fe 17#include <sys/stat.h>
ce75e27a 18#include <sys/types.h>
ce75e27a 19#include <tap/tap.h>
28ab034a 20#include <unistd.h>
93bed9fe 21
ce75e27a 22#define TEST_COUNT 9
93bed9fe
JG
23
24/* For error.h */
25int lttng_opt_quiet = 1;
26int lttng_opt_verbose = 3;
27int 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 */
e665dfbc 35using test_func = int(const char *);
ce75e27a
JG
36
37static test_func test_rmdir_fail_non_empty;
38static test_func test_rmdir_skip_non_empty;
39
40static test_func *const test_funcs[] = {
41 &test_rmdir_fail_non_empty,
42 &test_rmdir_skip_non_empty,
43};
44
45static 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 */
28ab034a
JG
66static int create_non_empty_hierarchy_with_root(struct lttng_directory_handle *test_dir_handle,
67 const char *test_root_name)
ce75e27a
JG
68{
69 int ret;
70 const int file_flags = O_WRONLY | O_CREAT | O_TRUNC;
71 const mode_t file_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
cd9adb8b 72 char *branch_name = nullptr;
ce75e27a
JG
73
74 ret = asprintf(&branch_name, "%s/%s", test_root_name, "a/b/c/d");
75 if (ret < 0) {
76 diag("Failed to format folder path");
77 goto end;
78 }
79 ret = lttng_directory_handle_create_subdirectory_recursive(
28ab034a 80 test_dir_handle, branch_name, DIR_CREATION_MODE);
ce75e27a
JG
81 if (ret) {
82 diag("Failed to create test folder hierarchy %s", branch_name);
83 goto end;
84 }
85
86 free(branch_name);
87 ret = asprintf(&branch_name, "%s/%s", test_root_name, "a/b/e/f");
88 if (ret < 0) {
89 diag("Failed to format folder path");
90 goto end;
91 }
92 ret = lttng_directory_handle_create_subdirectory_recursive(
28ab034a 93 test_dir_handle, branch_name, DIR_CREATION_MODE);
ce75e27a
JG
94 if (ret) {
95 diag("Failed to create test folder hierarchy %s", branch_name);
96 goto end;
97 }
98
99 free(branch_name);
100 ret = asprintf(&branch_name, "%s/%s", test_root_name, "a/b/e/file1");
101 if (ret < 0) {
102 diag("Failed to format file path");
103 goto end;
104 }
28ab034a 105 ret = lttng_directory_handle_open_file(test_dir_handle, branch_name, file_flags, file_mode);
ce75e27a
JG
106 if (ret < 0) {
107 diag("Failed to create file %s", branch_name);
108 goto end;
109 }
110 ret = close(ret);
111 if (ret) {
28ab034a 112 PERROR("Failed to close fd to newly created file %s", branch_name);
ce75e27a
JG
113 goto end;
114 }
115end:
116 free(branch_name);
117 return ret;
118}
119
120/* Remove "file1" from the test folder hierarchy. */
28ab034a
JG
121static int remove_file_from_hierarchy(struct lttng_directory_handle *test_dir_handle,
122 const char *test_root_name)
93bed9fe
JG
123{
124 int ret;
cd9adb8b 125 char *file_name = nullptr;
ce75e27a
JG
126
127 ret = asprintf(&file_name, "%s/%s", test_root_name, "a/b/e/file1");
128 if (ret < 0) {
129 diag("Failed to format file path");
130 goto end;
131 }
132
28ab034a 133 ret = lttng_directory_handle_unlink_file(test_dir_handle, file_name);
ce75e27a
JG
134 if (ret) {
135 PERROR("Failed to unlink file %s", file_name);
136 goto end;
137 }
138end:
139 free(file_name);
140 return ret;
141}
142
143static int test_rmdir_fail_non_empty(const char *test_dir)
144{
145 int ret, tests_ran = 0;
cbf53d23 146 struct lttng_directory_handle *test_dir_handle;
cd9adb8b 147 char *created_dir = nullptr;
ce75e27a 148 const char test_root_name[] = "fail_non_empty";
cd9adb8b 149 char *test_dir_path = nullptr;
93bed9fe 150
ce75e27a 151 diag("rmdir (fail if non-empty)");
93bed9fe 152
cbf53d23
JG
153 test_dir_handle = lttng_directory_handle_create(test_dir);
154 ok(test_dir_handle, "Initialized directory handle from the test directory");
ce75e27a 155 tests_ran++;
cbf53d23
JG
156 if (!test_dir_handle) {
157 ret = -1;
ce75e27a
JG
158 goto end;
159 }
93bed9fe 160
cbf53d23 161 ret = create_non_empty_hierarchy_with_root(test_dir_handle, test_root_name);
ce75e27a
JG
162 if (ret) {
163 diag("Failed to setup folder/file hierarchy to run test");
164 goto end;
165 }
166
167 ret = lttng_directory_handle_remove_subdirectory_recursive(
28ab034a
JG
168 test_dir_handle, test_root_name, LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG);
169 ok(ret == -1,
170 "Error returned when attempting to recursively remove non-empty hierarchy with LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG");
ce75e27a
JG
171 tests_ran++;
172
cbf53d23 173 ret = remove_file_from_hierarchy(test_dir_handle, test_root_name);
ce75e27a
JG
174 if (ret) {
175 diag("Failed to remove file from test folder hierarchy");
176 goto end;
177 }
178
179 ret = lttng_directory_handle_remove_subdirectory_recursive(
28ab034a
JG
180 test_dir_handle, test_root_name, LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG);
181 ok(ret == 0,
182 "No error returned when recursively removing empty hierarchy with LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG");
ce75e27a
JG
183 tests_ran++;
184
185 ret = asprintf(&test_dir_path, "%s/%s", test_dir, test_root_name);
186 if (ret < 0) {
187 diag("Failed to format test directory path");
93bed9fe
JG
188 goto end;
189 }
ce75e27a 190 ok(!dir_exists(test_dir_path) && errno == ENOENT,
28ab034a
JG
191 "Folder hierarchy %s successfully removed",
192 test_dir_path);
ce75e27a
JG
193 tests_ran++;
194 ret = 0;
195end:
cbf53d23 196 lttng_directory_handle_put(test_dir_handle);
ce75e27a
JG
197 free(created_dir);
198 free(test_dir_path);
199 return ret == 0 ? tests_ran : ret;
200}
201
202static int test_rmdir_skip_non_empty(const char *test_dir)
203{
204 int ret, tests_ran = 0;
cbf53d23 205 struct lttng_directory_handle *test_dir_handle;
cd9adb8b 206 char *created_dir = nullptr;
ce75e27a 207 const char test_root_name[] = "skip_non_empty";
cd9adb8b 208 char *test_dir_path = nullptr;
ce75e27a
JG
209
210 diag("rmdir (skip if non-empty)");
93bed9fe 211
cbf53d23
JG
212 test_dir_handle = lttng_directory_handle_create(test_dir);
213 ok(test_dir_handle, "Initialized directory handle from the test directory");
ce75e27a 214 tests_ran++;
cbf53d23
JG
215 if (!test_dir_handle) {
216 ret = -1;
93bed9fe
JG
217 goto end;
218 }
219
cbf53d23 220 ret = create_non_empty_hierarchy_with_root(test_dir_handle, test_root_name);
ce75e27a
JG
221 if (ret) {
222 diag("Failed to setup folder/file hierarchy to run test");
223 goto end;
224 }
225
226 ret = lttng_directory_handle_remove_subdirectory_recursive(
28ab034a
JG
227 test_dir_handle, test_root_name, LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG);
228 ok(ret == 0,
229 "No error returned when attempting to recursively remove non-empty hierarchy with LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG");
ce75e27a
JG
230 tests_ran++;
231
232 ret = asprintf(&test_dir_path, "%s/%s", test_dir, test_root_name);
93bed9fe 233 if (ret < 0) {
ce75e27a
JG
234 diag("Failed to format test directory path");
235 goto end;
236 }
237 ok(dir_exists(test_dir_path), "Test directory still exists after skip");
238 tests_ran++;
239
cbf53d23 240 ret = remove_file_from_hierarchy(test_dir_handle, test_root_name);
ce75e27a
JG
241 if (ret) {
242 diag("Failed to remove file from test folder hierarchy");
93bed9fe
JG
243 goto end;
244 }
93bed9fe
JG
245
246 ret = lttng_directory_handle_remove_subdirectory_recursive(
28ab034a
JG
247 test_dir_handle, test_root_name, LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG);
248 ok(ret == 0,
249 "No error returned when recursively removing empty hierarchy with LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG");
ce75e27a
JG
250 tests_ran++;
251
252 ok(!dir_exists(test_dir_path) && errno == ENOENT,
28ab034a
JG
253 "Folder hierarchy %s successfully removed",
254 test_dir_path);
ce75e27a
JG
255 tests_ran++;
256 ret = 0;
257end:
cbf53d23 258 lttng_directory_handle_put(test_dir_handle);
ce75e27a
JG
259 free(created_dir);
260 free(test_dir_path);
261 return ret == 0 ? tests_ran : ret;
262}
263
cd9adb8b 264int main()
ce75e27a
JG
265{
266 int ret;
267 char test_dir[] = "/tmp/lttng-XXXXXX";
268 int tests_left = TEST_COUNT;
269 size_t func_idx;
270
271 plan_tests(TEST_COUNT);
272
273 diag("lttng_directory_handle tests");
274
275 if (!mkdtemp(test_dir)) {
276 diag("Failed to generate temporary test directory");
277 goto end;
278 }
279
28ab034a 280 for (func_idx = 0; func_idx < sizeof(test_funcs) / sizeof(*test_funcs); func_idx++) {
ce75e27a
JG
281 tests_left -= test_funcs[func_idx](test_dir);
282 }
283 if (tests_left) {
284 diag("Skipping %d tests that could not be executed due to a prior error",
28ab034a 285 tests_left);
ce75e27a
JG
286 skip(tests_left, "test due to an error");
287 }
93bed9fe
JG
288end:
289 ret = rmdir(test_dir);
290 if (ret) {
291 diag("Failed to clean-up test directory: %s", strerror(errno));
292 }
93bed9fe
JG
293 return exit_status();
294}
This page took 0.063814 seconds and 4 git commands to generate.