summaryrefslogtreecommitdiff
path: root/tests/test_file.c
blob: bb8a99584d13139c71d8165ebcab4fe83761bdbe (plain)
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
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2016, Digium, Inc.
 *
 * Kevin Harwell <kharwell@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*** MODULEINFO
	<depend>TEST_FRAMEWORK</depend>
	<support_level>core</support_level>
 ***/


#include "asterisk.h"
#include <sys/stat.h>
#include <stdio.h>

#include "asterisk/file.h"
#include "asterisk/paths.h"
#include "asterisk/test.h"
#include "asterisk/module.h"
#include "asterisk/strings.h"
#include "asterisk/vector.h"

#define FOUND -7

AST_VECTOR(_filenames, struct ast_str *);

static void rm_file(struct ast_str *filename)
{
	if (unlink(ast_str_buffer(filename))) {
		ast_log(LOG_ERROR, "Unable to remove file: %s\n", ast_str_buffer(filename));
	}

	ast_free(filename);
}

static int test_files_destroy(struct ast_test *test, char *dir_name,
			      struct _filenames *filenames)
{
	int res;

	if (filenames) {
		AST_VECTOR_CALLBACK_VOID(filenames, rm_file);
		AST_VECTOR_FREE(filenames);
	}

	if ((res = rmdir(dir_name)) < 0) {
		ast_test_status_update(test, "Failed to remove directory: %s\n", dir_name);
	}

	return res;
}

static int test_files_create(struct ast_test *test, char *dir_name,
			     struct _filenames *filenames, int num)
{
	int i;

	if (!(mkdtemp(dir_name))) {
		ast_test_status_update(test, "Failed to create directory: %s\n", dir_name);
		return -1;
	}


	AST_VECTOR_INIT(filenames, num);

	/*
	 * Create "num" files under the specified directory
	 */
	for (i = 0; i < num; ++i) {
		int fd;
		struct ast_str *filename = ast_str_create(32);

		if (!filename) {
			break;
		}

		ast_str_set(&filename, 0, "%s/XXXXXX", dir_name);

		fd = mkstemp(ast_str_buffer(filename));
		if (fd < 0) {
			ast_test_status_update(test, "Failed to create file: %s\n",
					       ast_str_buffer(filename));
			ast_free(filename);
			break;
		}
		close(fd);

		AST_VECTOR_APPEND(filenames, filename);
	}

	if (i != num) {
		test_files_destroy(test, dir_name, filenames);
		return -1;
	}

	return 0;
}

static char *test_files_get_one(struct _filenames *filenames, int num)
{
	/* Every file is in a directory and contains a '/' so okay to do this */
	return strrchr(ast_str_buffer(
		       AST_VECTOR_GET(filenames, ast_random() % (num - 1))), '/') + 1;
}

static int handle_find_file(const char *dir_name, const char *filename, void *obj)
{
	struct stat statbuf;
	char *full_path = ast_alloca(strlen(dir_name) + strlen(filename) + 2);

	sprintf(full_path, "%s/%s", dir_name, filename);

	errno = 0;
	if (stat(full_path, &statbuf)) {
		ast_log(LOG_ERROR, "Error reading path stats - %s: %s\n",
			full_path, strerror(errno));
		return 0;
	}
	/* obj contains the name of the file we are looking for */
	return strcmp(obj, filename) ? 0 : FOUND;
}

AST_TEST_DEFINE(read_dirs_test)
{
	char tmp_dir[] = "/tmp/tmpdir.XXXXXX";
	struct ast_str *tmp_sub_dir;
	struct _filenames filenames;
	enum ast_test_result_state res;
	const int num_files = 10 + (ast_random() % 10); /* 10-19 random files */

	switch (cmd) {
	case TEST_INIT:
		info->name = "read_dir_test";
		info->category = "/main/file/";
		info->summary = "Read a directory's content";
		info->description = "Iterate over directories looking for a file.";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	/*
	 * We want to test recursively searching into a subdirectory, so
	 * create a top level tmp directory where we will start the search.
	 */
	if (!(mkdtemp(tmp_dir))) {
		ast_test_status_update(test, "Failed to create directory: %s\n", tmp_dir);
		return AST_TEST_FAIL;
	}

	tmp_sub_dir = ast_str_alloca(32);
	ast_str_set(&tmp_sub_dir, 0, "%s/XXXXXX", tmp_dir);

	if (test_files_create(test, ast_str_buffer(tmp_sub_dir), &filenames, num_files)) {
		test_files_destroy(test, tmp_dir, NULL);
		return AST_TEST_FAIL;
	}

	res = ast_file_read_dirs(tmp_dir, handle_find_file, test_files_get_one(
		 &filenames, num_files), 2) == FOUND ? AST_TEST_PASS : AST_TEST_FAIL;

	if (test_files_destroy(test, ast_str_buffer(tmp_sub_dir), &filenames) ||
	    test_files_destroy(test, tmp_dir, NULL)) {
		res = AST_TEST_FAIL;
	}

	return res;
}

static int unload_module(void)
{
	AST_TEST_UNREGISTER(read_dirs_test);
	return 0;
}

static int load_module(void)
{
	AST_TEST_REGISTER(read_dirs_test);
	return AST_MODULE_LOAD_SUCCESS;
}

AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "File test module");