Tests: Add add-trigger CLI tests
[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>
ce75e27a 9#include <fcntl.h>
93bed9fe 10#include <stdbool.h>
ce75e27a
JG
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
93bed9fe 14#include <sys/stat.h>
ce75e27a
JG
15#include <sys/types.h>
16#include <unistd.h>
93bed9fe 17
93bed9fe 18#include <common/compat/directory-handle.h>
edf4b93e 19#include <common/compat/errno.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. */
a815df68 129static
ce75e27a
JG
130int remove_file_from_hierarchy(struct lttng_directory_handle *test_dir_handle,
131 const char *test_root_name)
93bed9fe
JG
132{
133 int ret;
ce75e27a
JG
134 char *file_name = NULL;
135
136 ret = asprintf(&file_name, "%s/%s", test_root_name, "a/b/e/file1");
137 if (ret < 0) {
138 diag("Failed to format file path");
139 goto end;
140 }
141
142 ret = lttng_directory_handle_unlink_file(test_dir_handle,
143 file_name);
144 if (ret) {
145 PERROR("Failed to unlink file %s", file_name);
146 goto end;
147 }
148end:
149 free(file_name);
150 return ret;
151}
152
153static int test_rmdir_fail_non_empty(const char *test_dir)
154{
155 int ret, tests_ran = 0;
cbf53d23 156 struct lttng_directory_handle *test_dir_handle;
93bed9fe 157 char *created_dir = NULL;
ce75e27a
JG
158 const char test_root_name[] = "fail_non_empty";
159 char *test_dir_path = NULL;
93bed9fe 160
ce75e27a 161 diag("rmdir (fail if non-empty)");
93bed9fe 162
cbf53d23
JG
163 test_dir_handle = lttng_directory_handle_create(test_dir);
164 ok(test_dir_handle, "Initialized directory handle from the test directory");
ce75e27a 165 tests_ran++;
cbf53d23
JG
166 if (!test_dir_handle) {
167 ret = -1;
ce75e27a
JG
168 goto end;
169 }
93bed9fe 170
cbf53d23 171 ret = create_non_empty_hierarchy_with_root(test_dir_handle, test_root_name);
ce75e27a
JG
172 if (ret) {
173 diag("Failed to setup folder/file hierarchy to run test");
174 goto end;
175 }
176
177 ret = lttng_directory_handle_remove_subdirectory_recursive(
cbf53d23 178 test_dir_handle, test_root_name,
ce75e27a
JG
179 LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG);
180 ok(ret == -1, "Error returned when attempting to recursively remove non-empty hierarchy with LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG");
181 tests_ran++;
182
cbf53d23 183 ret = remove_file_from_hierarchy(test_dir_handle, test_root_name);
ce75e27a
JG
184 if (ret) {
185 diag("Failed to remove file from test folder hierarchy");
186 goto end;
187 }
188
189 ret = lttng_directory_handle_remove_subdirectory_recursive(
cbf53d23 190 test_dir_handle, test_root_name,
ce75e27a
JG
191 LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG);
192 ok(ret == 0, "No error returned when recursively removing empty hierarchy with LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG");
193 tests_ran++;
194
195 ret = asprintf(&test_dir_path, "%s/%s", test_dir, test_root_name);
196 if (ret < 0) {
197 diag("Failed to format test directory path");
93bed9fe
JG
198 goto end;
199 }
ce75e27a
JG
200 ok(!dir_exists(test_dir_path) && errno == ENOENT,
201 "Folder hierarchy %s successfully removed",
202 test_dir_path);
203 tests_ran++;
204 ret = 0;
205end:
cbf53d23 206 lttng_directory_handle_put(test_dir_handle);
ce75e27a
JG
207 free(created_dir);
208 free(test_dir_path);
209 return ret == 0 ? tests_ran : ret;
210}
211
212static int test_rmdir_skip_non_empty(const char *test_dir)
213{
214 int ret, tests_ran = 0;
cbf53d23 215 struct lttng_directory_handle *test_dir_handle;
ce75e27a
JG
216 char *created_dir = NULL;
217 const char test_root_name[] = "skip_non_empty";
218 char *test_dir_path = NULL;
219
220 diag("rmdir (skip if non-empty)");
93bed9fe 221
cbf53d23
JG
222 test_dir_handle = lttng_directory_handle_create(test_dir);
223 ok(test_dir_handle, "Initialized directory handle from the test directory");
ce75e27a 224 tests_ran++;
cbf53d23
JG
225 if (!test_dir_handle) {
226 ret = -1;
93bed9fe
JG
227 goto end;
228 }
229
cbf53d23 230 ret = create_non_empty_hierarchy_with_root(test_dir_handle, test_root_name);
ce75e27a
JG
231 if (ret) {
232 diag("Failed to setup folder/file hierarchy to run test");
233 goto end;
234 }
235
236 ret = lttng_directory_handle_remove_subdirectory_recursive(
cbf53d23 237 test_dir_handle, test_root_name,
ce75e27a
JG
238 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG);
239 ok(ret == 0, "No error returned when attempting to recursively remove non-empty hierarchy with LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG");
240 tests_ran++;
241
242 ret = asprintf(&test_dir_path, "%s/%s", test_dir, test_root_name);
93bed9fe 243 if (ret < 0) {
ce75e27a
JG
244 diag("Failed to format test directory path");
245 goto end;
246 }
247 ok(dir_exists(test_dir_path), "Test directory still exists after skip");
248 tests_ran++;
249
cbf53d23 250 ret = remove_file_from_hierarchy(test_dir_handle, test_root_name);
ce75e27a
JG
251 if (ret) {
252 diag("Failed to remove file from test folder hierarchy");
93bed9fe
JG
253 goto end;
254 }
93bed9fe
JG
255
256 ret = lttng_directory_handle_remove_subdirectory_recursive(
cbf53d23 257 test_dir_handle, test_root_name,
ce75e27a
JG
258 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG);
259 ok(ret == 0, "No error returned when recursively removing empty hierarchy with LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG");
260 tests_ran++;
261
262 ok(!dir_exists(test_dir_path) && errno == ENOENT,
263 "Folder hierarchy %s successfully removed",
264 test_dir_path);
265 tests_ran++;
266 ret = 0;
267end:
cbf53d23 268 lttng_directory_handle_put(test_dir_handle);
ce75e27a
JG
269 free(created_dir);
270 free(test_dir_path);
271 return ret == 0 ? tests_ran : ret;
272}
273
274int main(int argc, char **argv)
275{
276 int ret;
277 char test_dir[] = "/tmp/lttng-XXXXXX";
278 int tests_left = TEST_COUNT;
279 size_t func_idx;
280
281 plan_tests(TEST_COUNT);
282
283 diag("lttng_directory_handle tests");
284
285 if (!mkdtemp(test_dir)) {
286 diag("Failed to generate temporary test directory");
287 goto end;
288 }
289
290 for (func_idx = 0; func_idx < sizeof(test_funcs) / sizeof(*test_funcs);
291 func_idx++) {
292 tests_left -= test_funcs[func_idx](test_dir);
293 }
294 if (tests_left) {
295 diag("Skipping %d tests that could not be executed due to a prior error",
296 tests_left);
297 skip(tests_left, "test due to an error");
298 }
93bed9fe
JG
299end:
300 ret = rmdir(test_dir);
301 if (ret) {
302 diag("Failed to clean-up test directory: %s", strerror(errno));
303 }
93bed9fe
JG
304 return exit_status();
305}
This page took 0.038104 seconds and 4 git commands to generate.