| 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 |
#include "asterisk.h" |
| 26 |
#include <sys/stat.h> |
| 27 |
#include <stdio.h> |
| 28 |
|
| 29 |
#include "asterisk/file.h" |
| 30 |
#include "asterisk/paths.h" |
| 31 |
#include "asterisk/test.h" |
| 32 |
#include "asterisk/module.h" |
| 33 |
#include "asterisk/strings.h" |
| 34 |
#include "asterisk/vector.h" |
| 35 |
|
| 36 |
#define FOUND -7 |
| 37 |
|
| 38 |
AST_VECTOR(_filenames, struct ast_str *); |
| 39 |
|
| 40 |
static void rm_file(struct ast_str *filename) |
| 41 |
{ |
| 42 |
if (unlink(ast_str_buffer(filename))) { |
| 43 |
ast_log(LOG_ERROR, "Unable to remove file: %s\n", ast_str_buffer(filename)); |
| 44 |
} |
| 45 |
|
| 46 |
ast_free(filename); |
| 47 |
} |
| 48 |
|
| 49 |
static int test_files_destroy(struct ast_test *test, char *dir_name, |
| 50 |
struct _filenames *filenames) |
| 51 |
{ |
| 52 |
int res; |
| 53 |
|
| 54 |
if (filenames) { |
| 55 |
AST_VECTOR_CALLBACK_VOID(filenames, rm_file); |
| 56 |
AST_VECTOR_FREE(filenames); |
| 57 |
} |
| 58 |
|
| 59 |
if ((res = rmdir(dir_name)) < 0) { |
| 60 |
ast_test_status_update(test, "Failed to remove directory: %s\n", dir_name); |
| 61 |
} |
| 62 |
|
| 63 |
return res; |
| 64 |
} |
| 65 |
|
| 66 |
static int test_files_create(struct ast_test *test, char *dir_name, |
| 67 |
struct _filenames *filenames, int num) |
| 68 |
{ |
| 69 |
int i; |
| 70 |
|
| 71 |
if (!(mkdtemp(dir_name))) { |
| 72 |
ast_test_status_update(test, "Failed to create directory: %s\n", dir_name); |
| 73 |
return -1; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
AST_VECTOR_INIT(filenames, num); |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
for (i = 0; i < num; ++i) { |
| 83 |
int fd; |
| 84 |
struct ast_str *filename = ast_str_create(32); |
| 85 |
|
| 86 |
if (!filename) { |
| 87 |
break; |
| 88 |
} |
| 89 |
|
| 90 |
ast_str_set(&filename, 0, "%s/XXXXXX", dir_name); |
| 91 |
|
| 92 |
fd = mkstemp(ast_str_buffer(filename)); |
| 93 |
if (fd < 0) { |
| 94 |
ast_test_status_update(test, "Failed to create file: %s\n", |
| 95 |
ast_str_buffer(filename)); |
| 96 |
ast_free(filename); |
| 97 |
break; |
| 98 |
} |
| 99 |
close(fd); |
| 100 |
|
| 101 |
AST_VECTOR_APPEND(filenames, filename); |
| 102 |
} |
| 103 |
|
| 104 |
if (i != num) { |
| 105 |
test_files_destroy(test, dir_name, filenames); |
| 106 |
return -1; |
| 107 |
} |
| 108 |
|
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 112 |
static char *test_files_get_one(struct _filenames *filenames, int num) |
| 113 |
{ |
| 114 |
|
| 115 |
return strrchr(ast_str_buffer( |
| 116 |
AST_VECTOR_GET(filenames, ast_random() % (num - 1))), '/') + 1; |
| 117 |
} |
| 118 |
|
| 119 |
static int handle_find_file(const char *dir_name, const char *filename, void *obj) |
| 120 |
{ |
| 121 |
struct stat statbuf; |
| 122 |
char *full_path = ast_alloca(strlen(dir_name) + strlen(filename) + 2); |
| 123 |
|
| 124 |
sprintf(full_path, "%s/%s", dir_name, filename); |
| 125 |
|
| 126 |
errno = 0; |
| 127 |
if (stat(full_path, &statbuf)) { |
| 128 |
ast_log(LOG_ERROR, "Error reading path stats - %s: %s\n", |
| 129 |
full_path, strerror(errno)); |
| 130 |
return 0; |
| 131 |
} |
| 132 |
|
| 133 |
return strcmp(obj, filename) ? 0 : FOUND; |
| 134 |
} |
| 135 |
|
| 136 |
AST_TEST_DEFINE(read_dirs_test) |
| 137 |
{ |
| 138 |
char tmp_dir[] = "/tmp/tmpdir.XXXXXX"; |
| 139 |
struct ast_str *tmp_sub_dir; |
| 140 |
struct _filenames filenames; |
| 141 |
enum ast_test_result_state res; |
| 142 |
const int num_files = 10 + (ast_random() % 10); |
| 143 |
|
| 144 |
switch (cmd) { |
| 145 |
case TEST_INIT: |
| 146 |
info->name = "read_dir_test"; |
| 147 |
info->category = "/main/file/"; |
| 148 |
info->summary = "Read a directory's content"; |
| 149 |
info->description = "Iterate over directories looking for a file."; |
| 150 |
return AST_TEST_NOT_RUN; |
| 151 |
case TEST_EXECUTE: |
| 152 |
break; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
if (!(mkdtemp(tmp_dir))) { |
| 160 |
ast_test_status_update(test, "Failed to create directory: %s\n", tmp_dir); |
| 161 |
return AST_TEST_FAIL; |
| 162 |
} |
| 163 |
|
| 164 |
tmp_sub_dir = ast_str_alloca(32); |
| 165 |
ast_str_set(&tmp_sub_dir, 0, "%s/XXXXXX", tmp_dir); |
| 166 |
|
| 167 |
if (test_files_create(test, ast_str_buffer(tmp_sub_dir), &filenames, num_files)) { |
| 168 |
test_files_destroy(test, tmp_dir, NULL); |
| 169 |
return AST_TEST_FAIL; |
| 170 |
} |
| 171 |
|
| 172 |
res = ast_file_read_dirs(tmp_dir, handle_find_file, test_files_get_one( |
| 173 |
&filenames, num_files), 2) == FOUND ? AST_TEST_PASS : AST_TEST_FAIL; |
| 174 |
|
| 175 |
if (test_files_destroy(test, ast_str_buffer(tmp_sub_dir), &filenames) || |
| 176 |
test_files_destroy(test, tmp_dir, NULL)) { |
| 177 |
res = AST_TEST_FAIL; |
| 178 |
} |
| 179 |
|
| 180 |
return res; |
| 181 |
} |
| 182 |
|
| 183 |
static int unload_module(void) |
| 184 |
{ |
| 185 |
AST_TEST_UNREGISTER(read_dirs_test); |
| 186 |
return 0; |
| 187 |
} |
| 188 |
|
| 189 |
static int load_module(void) |
| 190 |
{ |
| 191 |
AST_TEST_REGISTER(read_dirs_test); |
| 192 |
return AST_MODULE_LOAD_SUCCESS; |
| 193 |
} |
| 194 |
|
| 195 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "File test module"); |