File: test_directory_handle.c

package info (click to toggle)
ltt-control 2.13.9-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 17,668 kB
  • sloc: ansic: 166,727; sh: 26,657; makefile: 2,804; python: 1,374; yacc: 692; lex: 129; java: 109; perl: 99; cpp: 27; xml: 23
file content (305 lines) | stat: -rw-r--r-- 7,868 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
 * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
 *
 * SPDX-License-Identifier: GPL-2.0-only
 *
 */

#include <assert.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <common/compat/directory-handle.h>
#include <common/compat/errno.h>
#include <common/error.h>
#include <tap/tap.h>

#define TEST_COUNT 9

/* For error.h */
int lttng_opt_quiet = 1;
int lttng_opt_verbose = 3;
int lttng_opt_mi;

#define DIR_CREATION_MODE (S_IRWXU | S_IRWXG)

/*
 * Returns the number of tests that ran (irrespective of the result) or a
 * negative value on error (will abort all tests).
 */
typedef int(test_func)(const char *test_base_path);

static test_func test_rmdir_fail_non_empty;
static test_func test_rmdir_skip_non_empty;

static test_func *const test_funcs[] = {
	&test_rmdir_fail_non_empty,
	&test_rmdir_skip_non_empty,
};

static bool dir_exists(const char *path)
{
	int ret;
	struct stat st;

	ret = stat(path, &st);
	return ret == 0 && S_ISDIR(st.st_mode);
}

/*
 * Create a non-empty folder hierarchy from a directory handle:
 *
 * test_root_name
 * └── a
 *     └── b
 *         ├── c
 *         │   └── d
 *         └── e
 *             ├── f
 *             └── file1
 */
static int create_non_empty_hierarchy_with_root(
		struct lttng_directory_handle *test_dir_handle,
		const char *test_root_name)
{
	int ret;
	const int file_flags = O_WRONLY | O_CREAT | O_TRUNC;
	const mode_t file_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
	char *branch_name = NULL;

	ret = asprintf(&branch_name, "%s/%s", test_root_name, "a/b/c/d");
	if (ret < 0) {
		diag("Failed to format folder path");
		goto end;
	}
	ret = lttng_directory_handle_create_subdirectory_recursive(
			test_dir_handle,
			branch_name,
			DIR_CREATION_MODE);
	if (ret) {
		diag("Failed to create test folder hierarchy %s", branch_name);
		goto end;
	}

	free(branch_name);
	ret = asprintf(&branch_name, "%s/%s", test_root_name, "a/b/e/f");
	if (ret < 0) {
		diag("Failed to format folder path");
		goto end;
	}
	ret = lttng_directory_handle_create_subdirectory_recursive(
			test_dir_handle,
			branch_name,
			DIR_CREATION_MODE);
	if (ret) {
		diag("Failed to create test folder hierarchy %s", branch_name);
		goto end;
	}

	free(branch_name);
	ret = asprintf(&branch_name, "%s/%s", test_root_name, "a/b/e/file1");
	if (ret < 0) {
		diag("Failed to format file path");
		goto end;
	}
	ret = lttng_directory_handle_open_file(
			test_dir_handle, branch_name, file_flags, file_mode);
	if (ret < 0) {
		diag("Failed to create file %s", branch_name);
		goto end;
	}
	ret = close(ret);
	if (ret) {
		PERROR("Failed to close fd to newly created file %s",
				branch_name);
		goto end;
	}
end:
	free(branch_name);
	return ret;
}

/* Remove "file1" from the test folder hierarchy. */
static
int remove_file_from_hierarchy(struct lttng_directory_handle *test_dir_handle,
		const char *test_root_name)
{
	int ret;
	char *file_name = NULL;

	ret = asprintf(&file_name, "%s/%s", test_root_name, "a/b/e/file1");
	if (ret < 0) {
		diag("Failed to format file path");
		goto end;
	}

	ret = lttng_directory_handle_unlink_file(test_dir_handle,
			file_name);
	if (ret) {
		PERROR("Failed to unlink file %s", file_name);
		goto end;
	}
end:
	free(file_name);
	return ret;
}

static int test_rmdir_fail_non_empty(const char *test_dir)
{
	int ret, tests_ran = 0;
	struct lttng_directory_handle *test_dir_handle;
	char *created_dir = NULL;
	const char test_root_name[] = "fail_non_empty";
	char *test_dir_path = NULL;

	diag("rmdir (fail if non-empty)");

	test_dir_handle = lttng_directory_handle_create(test_dir);
	ok(test_dir_handle, "Initialized directory handle from the test directory");
	tests_ran++;
	if (!test_dir_handle) {
		ret = -1;
		goto end;
	}

	ret = create_non_empty_hierarchy_with_root(test_dir_handle, test_root_name);
	if (ret) {
		diag("Failed to setup folder/file hierarchy to run test");
		goto end;
	}

	ret = lttng_directory_handle_remove_subdirectory_recursive(
			test_dir_handle, test_root_name,
			LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG);
	ok(ret == -1, "Error returned when attempting to recursively remove non-empty hierarchy with LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG");
	tests_ran++;

	ret = remove_file_from_hierarchy(test_dir_handle, test_root_name);
	if (ret) {
		diag("Failed to remove file from test folder hierarchy");
		goto end;
	}

	ret = lttng_directory_handle_remove_subdirectory_recursive(
			test_dir_handle, test_root_name,
			LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG);
	ok(ret == 0, "No error returned when recursively removing empty hierarchy with LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG");
	tests_ran++;

	ret = asprintf(&test_dir_path, "%s/%s", test_dir, test_root_name);
	if (ret < 0) {
		diag("Failed to format test directory path");
		goto end;
	}
	ok(!dir_exists(test_dir_path) && errno == ENOENT,
			"Folder hierarchy %s successfully removed",
			test_dir_path);
	tests_ran++;
	ret = 0;
end:
	lttng_directory_handle_put(test_dir_handle);
	free(created_dir);
	free(test_dir_path);
	return ret == 0 ? tests_ran : ret;
}

static int test_rmdir_skip_non_empty(const char *test_dir)
{
	int ret, tests_ran = 0;
	struct lttng_directory_handle *test_dir_handle;
	char *created_dir = NULL;
	const char test_root_name[] = "skip_non_empty";
	char *test_dir_path = NULL;

	diag("rmdir (skip if non-empty)");

	test_dir_handle = lttng_directory_handle_create(test_dir);
	ok(test_dir_handle, "Initialized directory handle from the test directory");
	tests_ran++;
	if (!test_dir_handle) {
		ret = -1;
		goto end;
	}

	ret = create_non_empty_hierarchy_with_root(test_dir_handle, test_root_name);
	if (ret) {
		diag("Failed to setup folder/file hierarchy to run test");
		goto end;
	}

	ret = lttng_directory_handle_remove_subdirectory_recursive(
			test_dir_handle, test_root_name,
			LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG);
	ok(ret == 0, "No error returned when attempting to recursively remove non-empty hierarchy with LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG");
	tests_ran++;

	ret = asprintf(&test_dir_path, "%s/%s", test_dir, test_root_name);
	if (ret < 0) {
		diag("Failed to format test directory path");
		goto end;
	}
	ok(dir_exists(test_dir_path), "Test directory still exists after skip");
	tests_ran++;

	ret = remove_file_from_hierarchy(test_dir_handle, test_root_name);
	if (ret) {
		diag("Failed to remove file from test folder hierarchy");
		goto end;
	}

	ret = lttng_directory_handle_remove_subdirectory_recursive(
			test_dir_handle, test_root_name,
			LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG);
	ok(ret == 0, "No error returned when recursively removing empty hierarchy with LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG");
	tests_ran++;

	ok(!dir_exists(test_dir_path) && errno == ENOENT,
			"Folder hierarchy %s successfully removed",
			test_dir_path);
	tests_ran++;
	ret = 0;
end:
	lttng_directory_handle_put(test_dir_handle);
	free(created_dir);
	free(test_dir_path);
	return ret == 0 ? tests_ran : ret;
}

int main(int argc, char **argv)
{
	int ret;
	char test_dir[] = "/tmp/lttng-XXXXXX";
	int tests_left = TEST_COUNT;
	size_t func_idx;

	plan_tests(TEST_COUNT);

	diag("lttng_directory_handle tests");

	if (!mkdtemp(test_dir)) {
		diag("Failed to generate temporary test directory");
		goto end;
	}

	for (func_idx = 0; func_idx < sizeof(test_funcs) / sizeof(*test_funcs);
			func_idx++) {
		tests_left -= test_funcs[func_idx](test_dir);
	}
	if (tests_left) {
		diag("Skipping %d tests that could not be executed due to a prior error",
				tests_left);
		skip(tests_left, "test due to an error");
	}
end:
	ret = rmdir(test_dir);
	if (ret) {
		diag("Failed to clean-up test directory: %s", strerror(errno));
	}
	return exit_status();
}