| 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 |
#include "asterisk.h" |
| 34 |
|
| 35 |
#include "asterisk/utils.h" |
| 36 |
#include "asterisk/module.h" |
| 37 |
#include "asterisk/test.h" |
| 38 |
#include "asterisk/bucket.h" |
| 39 |
#include "asterisk/media_cache.h" |
| 40 |
|
| 41 |
|
| 42 |
#define CATEGORY "/main/media_cache/" |
| 43 |
|
| 44 |
|
| 45 |
#define VALID_RESOURCE "httptest://localhost:8088/test_media_cache/test_media_cache/test_media_cache/test_media_cache/test_media_cache/test_media_cache/test_media_cache/test_media_cache/test_media_cache/test_media_cache/test_media_cache/test_media_cache/test_media_cache/monkeys.wav" |
| 46 |
|
| 47 |
|
| 48 |
#define INVALID_RESOURCE "httptest://localhost:8088/test_media_cache/bad.wav" |
| 49 |
|
| 50 |
|
| 51 |
#define INVALID_SCHEME "foo://localhost:8088/test_media_cache/monkeys.wav" |
| 52 |
|
| 53 |
|
| 54 |
#define NO_SCHEME "localhost:8088/test_media_cache/monkeys.wav" |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
static int bucket_http_test_wizard_create(const struct ast_sorcery *sorcery, void *data, |
| 61 |
void *object) |
| 62 |
{ |
| 63 |
if (!strcmp(ast_sorcery_object_get_id(object), VALID_RESOURCE)) { |
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
return -1; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
static int bucket_http_test_wizard_update(const struct ast_sorcery *sorcery, void *data, |
| 75 |
void *object) |
| 76 |
{ |
| 77 |
if (!strcmp(ast_sorcery_object_get_id(object), VALID_RESOURCE)) { |
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
return -1; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
static void *bucket_http_test_wizard_retrieve_id(const struct ast_sorcery *sorcery, |
| 89 |
void *data, const char *type, const char *id) |
| 90 |
{ |
| 91 |
struct ast_bucket_file *bucket_file; |
| 92 |
|
| 93 |
if (!strcmp(type, "file") && !strcmp(id, VALID_RESOURCE)) { |
| 94 |
bucket_file = ast_bucket_file_alloc(id); |
| 95 |
if (!bucket_file) { |
| 96 |
return NULL; |
| 97 |
} |
| 98 |
|
| 99 |
ast_bucket_file_temporary_create(bucket_file); |
| 100 |
return bucket_file; |
| 101 |
} |
| 102 |
return NULL; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
static int bucket_http_test_wizard_delete(const struct ast_sorcery *sorcery, void *data, |
| 110 |
void *object) |
| 111 |
{ |
| 112 |
ast_media_cache_delete(VALID_RESOURCE); |
| 113 |
if (!strcmp(ast_sorcery_object_get_id(object), VALID_RESOURCE)) { |
| 114 |
return 0; |
| 115 |
} |
| 116 |
|
| 117 |
return -1; |
| 118 |
} |
| 119 |
|
| 120 |
static struct ast_sorcery_wizard bucket_test_wizard = { |
| 121 |
.name = "httptest", |
| 122 |
.create = bucket_http_test_wizard_create, |
| 123 |
.retrieve_id = bucket_http_test_wizard_retrieve_id, |
| 124 |
.delete = bucket_http_test_wizard_delete, |
| 125 |
}; |
| 126 |
|
| 127 |
static struct ast_sorcery_wizard bucket_file_test_wizard = { |
| 128 |
.name = "httptest", |
| 129 |
.create = bucket_http_test_wizard_create, |
| 130 |
.update = bucket_http_test_wizard_update, |
| 131 |
.retrieve_id = bucket_http_test_wizard_retrieve_id, |
| 132 |
.delete = bucket_http_test_wizard_delete, |
| 133 |
}; |
| 134 |
|
| 135 |
AST_TEST_DEFINE(exists_nominal) |
| 136 |
{ |
| 137 |
int res; |
| 138 |
|
| 139 |
switch (cmd) { |
| 140 |
case TEST_INIT: |
| 141 |
info->name = __func__; |
| 142 |
info->category = CATEGORY; |
| 143 |
info->summary = "Test nominal existance of resources in the cache"; |
| 144 |
info->description = |
| 145 |
"This test verifies that if a known resource is in the cache, " |
| 146 |
"calling ast_media_cache_exists will return logical True. If " |
| 147 |
"a resource does not exist, the same function call will return " |
| 148 |
"logical False."; |
| 149 |
return AST_TEST_NOT_RUN; |
| 150 |
case TEST_EXECUTE: |
| 151 |
break; |
| 152 |
} |
| 153 |
|
| 154 |
res = ast_media_cache_exists(INVALID_RESOURCE); |
| 155 |
ast_test_validate(test, res == 0); |
| 156 |
|
| 157 |
res = ast_media_cache_exists(VALID_RESOURCE); |
| 158 |
ast_test_validate(test, res == 1); |
| 159 |
|
| 160 |
return AST_TEST_PASS; |
| 161 |
} |
| 162 |
|
| 163 |
AST_TEST_DEFINE(exists_off_nominal) |
| 164 |
{ |
| 165 |
int res; |
| 166 |
|
| 167 |
switch (cmd) { |
| 168 |
case TEST_INIT: |
| 169 |
info->name = __func__; |
| 170 |
info->category = CATEGORY; |
| 171 |
info->summary = "Test off nominal existance of resources in the cache"; |
| 172 |
info->description = |
| 173 |
"This test verifies that checking for bad resources (NULL, bad " |
| 174 |
"scheme, etc.) does not result in false positives."; |
| 175 |
return AST_TEST_NOT_RUN; |
| 176 |
case TEST_EXECUTE: |
| 177 |
break; |
| 178 |
} |
| 179 |
|
| 180 |
res = ast_media_cache_exists(""); |
| 181 |
ast_test_validate(test, res != 1); |
| 182 |
|
| 183 |
res = ast_media_cache_exists(NULL); |
| 184 |
ast_test_validate(test, res != 1); |
| 185 |
|
| 186 |
res = ast_media_cache_exists(NO_SCHEME); |
| 187 |
ast_test_validate(test, res != 1); |
| 188 |
|
| 189 |
res = ast_media_cache_exists(INVALID_SCHEME); |
| 190 |
ast_test_validate(test, res != 1); |
| 191 |
|
| 192 |
return AST_TEST_PASS; |
| 193 |
} |
| 194 |
|
| 195 |
AST_TEST_DEFINE(create_update_nominal) |
| 196 |
{ |
| 197 |
int res; |
| 198 |
char file_path[PATH_MAX]; |
| 199 |
char tmp_path_one[PATH_MAX] = "/tmp/test-media-cache-XXXXXX"; |
| 200 |
char tmp_path_two[PATH_MAX] = "/tmp/test-media-cache-XXXXXX"; |
| 201 |
int fd; |
| 202 |
|
| 203 |
switch (cmd) { |
| 204 |
case TEST_INIT: |
| 205 |
info->name = __func__; |
| 206 |
info->category = CATEGORY; |
| 207 |
info->summary = "Test nominal creation/updating of a resource"; |
| 208 |
info->description = |
| 209 |
"This test creates a resource and associates it with a file. " |
| 210 |
"It then updates the resource with a new file. In both cases, " |
| 211 |
"the test verifies that the resource is associated with the " |
| 212 |
"file."; |
| 213 |
return AST_TEST_NOT_RUN; |
| 214 |
case TEST_EXECUTE: |
| 215 |
break; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
fd = mkstemp(tmp_path_one); |
| 220 |
if (fd < 0) { |
| 221 |
ast_test_status_update(test, "Failed to create first tmp file: %s\n", |
| 222 |
tmp_path_one); |
| 223 |
return AST_TEST_FAIL; |
| 224 |
} |
| 225 |
|
| 226 |
close(fd); |
| 227 |
|
| 228 |
fd = mkstemp(tmp_path_two); |
| 229 |
if (fd < 0) { |
| 230 |
ast_test_status_update(test, "Failed to create second tmp file: %s\n", |
| 231 |
tmp_path_two); |
| 232 |
return AST_TEST_FAIL; |
| 233 |
} |
| 234 |
close(fd); |
| 235 |
|
| 236 |
ast_test_status_update(test, "Creating resource with %s\n", tmp_path_one); |
| 237 |
res = ast_media_cache_create_or_update(VALID_RESOURCE, tmp_path_one, NULL); |
| 238 |
ast_test_validate(test, res == 0); |
| 239 |
|
| 240 |
res = ast_media_cache_retrieve(VALID_RESOURCE, NULL, file_path, PATH_MAX); |
| 241 |
ast_test_status_update(test, "Got %s for first file path\n", file_path); |
| 242 |
ast_test_validate(test, res == 0); |
| 243 |
ast_test_validate(test, strcmp(file_path, tmp_path_one) == 0); |
| 244 |
|
| 245 |
ast_test_status_update(test, "Creating resource with %s\n", tmp_path_two); |
| 246 |
res = ast_media_cache_create_or_update(VALID_RESOURCE, tmp_path_two, NULL); |
| 247 |
ast_test_validate(test, res == 0); |
| 248 |
|
| 249 |
res = ast_media_cache_retrieve(VALID_RESOURCE, NULL, file_path, PATH_MAX); |
| 250 |
ast_test_status_update(test, "Got %s for second file path\n", file_path); |
| 251 |
ast_test_validate(test, res == 0); |
| 252 |
ast_test_validate(test, strcmp(file_path, tmp_path_two) == 0); |
| 253 |
|
| 254 |
ast_media_cache_delete(VALID_RESOURCE); |
| 255 |
|
| 256 |
unlink(tmp_path_one); |
| 257 |
unlink(tmp_path_two); |
| 258 |
|
| 259 |
return AST_TEST_PASS; |
| 260 |
} |
| 261 |
|
| 262 |
AST_TEST_DEFINE(create_update_off_nominal) |
| 263 |
{ |
| 264 |
int res; |
| 265 |
char tmp_path[PATH_MAX] = "/tmp/test-media-cache-XXXXXX"; |
| 266 |
int fd; |
| 267 |
|
| 268 |
switch (cmd) { |
| 269 |
case TEST_INIT: |
| 270 |
info->name = __func__; |
| 271 |
info->category = CATEGORY; |
| 272 |
info->summary = "Test off nominal creation/updating of a resource"; |
| 273 |
info->description = |
| 274 |
"Test creation/updating of a resource with a variety of invalid\n" |
| 275 |
"inputs."; |
| 276 |
return AST_TEST_NOT_RUN; |
| 277 |
case TEST_EXECUTE: |
| 278 |
break; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
fd = mkstemp(tmp_path); |
| 283 |
if (fd < 0) { |
| 284 |
ast_test_status_update(test, "Failed to create first tmp file: %s\n", |
| 285 |
tmp_path); |
| 286 |
return AST_TEST_FAIL; |
| 287 |
} |
| 288 |
|
| 289 |
close(fd); |
| 290 |
|
| 291 |
res = ast_media_cache_create_or_update(VALID_RESOURCE, NULL, NULL); |
| 292 |
ast_test_validate(test, res != 0); |
| 293 |
|
| 294 |
res = ast_media_cache_create_or_update(VALID_RESOURCE, "", NULL); |
| 295 |
ast_test_validate(test, res != 0); |
| 296 |
|
| 297 |
res = ast_media_cache_create_or_update(VALID_RESOURCE, "I don't exist", NULL); |
| 298 |
ast_test_validate(test, res != 0); |
| 299 |
|
| 300 |
res = ast_media_cache_create_or_update(INVALID_RESOURCE, tmp_path, NULL); |
| 301 |
ast_test_validate(test, res != 0); |
| 302 |
|
| 303 |
res = ast_media_cache_create_or_update(INVALID_SCHEME, tmp_path, NULL); |
| 304 |
ast_test_validate(test, res != 0); |
| 305 |
|
| 306 |
res = ast_media_cache_create_or_update(NO_SCHEME, tmp_path, NULL); |
| 307 |
ast_test_validate(test, res != 0); |
| 308 |
|
| 309 |
unlink(tmp_path); |
| 310 |
|
| 311 |
return AST_TEST_PASS; |
| 312 |
} |
| 313 |
|
| 314 |
AST_TEST_DEFINE(create_update_metadata) |
| 315 |
{ |
| 316 |
int res; |
| 317 |
char tmp_path[PATH_MAX] = "/tmp/test-media-cache-XXXXXX"; |
| 318 |
char file_path[PATH_MAX]; |
| 319 |
char actual_metadata[32]; |
| 320 |
struct ast_variable *meta_list = NULL; |
| 321 |
struct ast_variable *tmp; |
| 322 |
int fd; |
| 323 |
|
| 324 |
switch (cmd) { |
| 325 |
case TEST_INIT: |
| 326 |
info->name = __func__; |
| 327 |
info->category = CATEGORY; |
| 328 |
info->summary = "Test nominal creation/updating of a resource"; |
| 329 |
info->description = |
| 330 |
"This test creates a resource and associates it with a file. " |
| 331 |
"It then updates the resource with a new file. In both cases, " |
| 332 |
"the test verifies that the resource is associated with the " |
| 333 |
"file."; |
| 334 |
return AST_TEST_NOT_RUN; |
| 335 |
case TEST_EXECUTE: |
| 336 |
break; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
fd = mkstemp(tmp_path); |
| 341 |
if (fd < 0) { |
| 342 |
ast_test_status_update(test, "Failed to create first tmp file: %s\n", |
| 343 |
tmp_path); |
| 344 |
return AST_TEST_FAIL; |
| 345 |
} |
| 346 |
|
| 347 |
close(fd); |
| 348 |
|
| 349 |
tmp = ast_variable_new("meta1", "value1", __FILE__); |
| 350 |
if (!tmp) { |
| 351 |
ast_test_status_update(test, "Failed to create metadata 1 for test\n"); |
| 352 |
return AST_TEST_FAIL; |
| 353 |
} |
| 354 |
ast_variable_list_append(&meta_list, tmp); |
| 355 |
|
| 356 |
tmp = ast_variable_new("meta2", "value2", __FILE__); |
| 357 |
if (!tmp) { |
| 358 |
ast_test_status_update(test, "Failed to create metadata 2 for test\n"); |
| 359 |
return AST_TEST_FAIL; |
| 360 |
} |
| 361 |
ast_variable_list_append(&meta_list, tmp); |
| 362 |
|
| 363 |
res = ast_media_cache_create_or_update(VALID_RESOURCE, tmp_path, meta_list); |
| 364 |
ast_test_validate(test, res == 0); |
| 365 |
|
| 366 |
res = ast_media_cache_retrieve(VALID_RESOURCE, NULL, file_path, PATH_MAX); |
| 367 |
ast_test_status_update(test, "Got %s for second file path\n", file_path); |
| 368 |
ast_test_validate(test, res == 0); |
| 369 |
ast_test_validate(test, strcmp(file_path, tmp_path) == 0); |
| 370 |
|
| 371 |
res = ast_media_cache_retrieve_metadata(VALID_RESOURCE, "meta1", |
| 372 |
actual_metadata, sizeof(actual_metadata)); |
| 373 |
ast_test_validate(test, res == 0); |
| 374 |
ast_test_validate(test, strcmp(actual_metadata, "value1") == 0); |
| 375 |
|
| 376 |
res = ast_media_cache_retrieve_metadata(VALID_RESOURCE, "meta2", |
| 377 |
actual_metadata, sizeof(actual_metadata)); |
| 378 |
ast_test_validate(test, res == 0); |
| 379 |
ast_test_validate(test, strcmp(actual_metadata, "value2") == 0); |
| 380 |
|
| 381 |
unlink(tmp_path); |
| 382 |
|
| 383 |
return AST_TEST_PASS; |
| 384 |
} |
| 385 |
|
| 386 |
static int unload_module(void) |
| 387 |
{ |
| 388 |
AST_TEST_UNREGISTER(exists_nominal); |
| 389 |
AST_TEST_UNREGISTER(exists_off_nominal); |
| 390 |
|
| 391 |
AST_TEST_UNREGISTER(create_update_nominal); |
| 392 |
AST_TEST_UNREGISTER(create_update_metadata); |
| 393 |
AST_TEST_UNREGISTER(create_update_off_nominal); |
| 394 |
|
| 395 |
return 0; |
| 396 |
} |
| 397 |
|
| 398 |
static int load_module(void) |
| 399 |
{ |
| 400 |
if (ast_bucket_scheme_register("httptest", &bucket_test_wizard, |
| 401 |
&bucket_file_test_wizard, NULL, NULL)) { |
| 402 |
ast_log(LOG_ERROR, "Failed to register Bucket HTTP test wizard scheme implementation\n"); |
| 403 |
return AST_MODULE_LOAD_DECLINE; |
| 404 |
} |
| 405 |
|
| 406 |
AST_TEST_REGISTER(exists_nominal); |
| 407 |
AST_TEST_REGISTER(exists_off_nominal); |
| 408 |
|
| 409 |
AST_TEST_REGISTER(create_update_nominal); |
| 410 |
AST_TEST_REGISTER(create_update_metadata); |
| 411 |
AST_TEST_REGISTER(create_update_off_nominal); |
| 412 |
|
| 413 |
return AST_MODULE_LOAD_SUCCESS; |
| 414 |
} |
| 415 |
|
| 416 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Media Cache Tests"); |