| 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 |
#include "asterisk.h" |
| 36 |
|
| 37 |
#include <fcntl.h> |
| 38 |
|
| 39 |
#include "asterisk/module.h" |
| 40 |
#include "asterisk/http.h" |
| 41 |
#include "asterisk/bucket.h" |
| 42 |
#include "asterisk/test.h" |
| 43 |
|
| 44 |
#define CATEGORY "/res/http_media_cache/" |
| 45 |
|
| 46 |
#define TEST_URI "test_media_cache" |
| 47 |
|
| 48 |
struct test_options { |
| 49 |
int status_code; |
| 50 |
int send_file; |
| 51 |
struct { |
| 52 |
int s_maxage; |
| 53 |
int maxage; |
| 54 |
int no_cache; |
| 55 |
int must_revalidate; |
| 56 |
} cache_control; |
| 57 |
struct timeval expires; |
| 58 |
const char *status_text; |
| 59 |
const char *etag; |
| 60 |
const char *content_type; |
| 61 |
}; |
| 62 |
|
| 63 |
static struct test_options options; |
| 64 |
|
| 65 |
static char server_uri[512]; |
| 66 |
|
| 67 |
#define VALIDATE_EXPIRES(test, bucket_file, expected, delta) do { \ |
| 68 |
RAII_VAR(struct ast_bucket_metadata *, metadata, ast_bucket_file_metadata_get((bucket_file), "__actual_expires"), ao2_cleanup); \ |
| 69 |
int actual_expires; \ |
| 70 |
ast_test_validate(test, metadata != NULL); \ |
| 71 |
ast_test_validate(test, sscanf(metadata->value, "%d", &actual_expires) == 1); \ |
| 72 |
ast_test_status_update(test, "Checking %d >= %d and %d <= %d\n", \ |
| 73 |
(int) ((expected) + (delta)), actual_expires, \ |
| 74 |
(int) ((expected) - (delta)), actual_expires); \ |
| 75 |
ast_test_validate(test, (((expected) + (delta) >= actual_expires) && ((expected) - (delta) <= actual_expires))); \ |
| 76 |
} while (0) |
| 77 |
|
| 78 |
#define VALIDATE_STR_METADATA(test, bucket_file, key, expected) do { \ |
| 79 |
RAII_VAR(struct ast_bucket_metadata *, metadata, ast_bucket_file_metadata_get((bucket_file), (key)), ao2_cleanup); \ |
| 80 |
ast_test_validate(test, metadata != NULL); \ |
| 81 |
ast_test_validate(test, !strcmp(metadata->value, (expected))); \ |
| 82 |
} while (0) |
| 83 |
|
| 84 |
#define SET_OR_APPEND_CACHE_CONTROL(str) do { \ |
| 85 |
if (!ast_str_strlen((str))) { \ |
| 86 |
ast_str_set(&(str), 0, "%s", "cache-control: "); \ |
| 87 |
} else { \ |
| 88 |
ast_str_append(&(str), 0, "%s", ", "); \ |
| 89 |
} \ |
| 90 |
} while (0) |
| 91 |
|
| 92 |
static int http_callback(struct ast_tcptls_session_instance *ser, const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, struct ast_variable *get_params, struct ast_variable *headers) |
| 93 |
{ |
| 94 |
char file_name[64] = "/tmp/test-media-cache-XXXXXX"; |
| 95 |
struct ast_str *http_header = ast_str_create(128); |
| 96 |
struct ast_str *cache_control = ast_str_create(128); |
| 97 |
int fd = -1; |
| 98 |
int unmodified = 0; |
| 99 |
int send_file = options.send_file && method == AST_HTTP_GET; |
| 100 |
|
| 101 |
if (!http_header) { |
| 102 |
goto error; |
| 103 |
} |
| 104 |
|
| 105 |
if (send_file) { |
| 106 |
char buf[1024]; |
| 107 |
|
| 108 |
fd = mkstemp(file_name); |
| 109 |
if (fd == -1) { |
| 110 |
ast_log(LOG_ERROR, "Unable to open temp file for testing: %s (%d)", strerror(errno), errno); |
| 111 |
goto error; |
| 112 |
} |
| 113 |
|
| 114 |
memset(buf, 1, sizeof(buf)); |
| 115 |
if (write(fd, buf, sizeof(buf)) != sizeof(buf)) { |
| 116 |
ast_log(LOG_ERROR, "Failed to write expected number of bytes to pipe\n"); |
| 117 |
close(fd); |
| 118 |
goto error; |
| 119 |
} |
| 120 |
close(fd); |
| 121 |
|
| 122 |
fd = open(file_name, 0); |
| 123 |
if (fd == -1) { |
| 124 |
ast_log(LOG_ERROR, "Unable to open temp file for testing: %s (%d)", strerror(errno), errno); |
| 125 |
goto error; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
if (!ast_strlen_zero(options.content_type)) { |
| 130 |
ast_str_append(&http_header, 0, "Content-Type: %s\r\n", options.content_type); |
| 131 |
} |
| 132 |
|
| 133 |
if (options.cache_control.maxage) { |
| 134 |
SET_OR_APPEND_CACHE_CONTROL(cache_control); |
| 135 |
ast_str_append(&cache_control, 0, "max-age=%d", options.cache_control.maxage); |
| 136 |
} |
| 137 |
|
| 138 |
if (options.cache_control.s_maxage) { |
| 139 |
SET_OR_APPEND_CACHE_CONTROL(cache_control); |
| 140 |
ast_str_append(&cache_control, 0, "s-maxage=%d", options.cache_control.s_maxage); |
| 141 |
} |
| 142 |
|
| 143 |
if (options.cache_control.no_cache) { |
| 144 |
SET_OR_APPEND_CACHE_CONTROL(cache_control); |
| 145 |
ast_str_append(&cache_control, 0, "%s", "no-cache"); |
| 146 |
} |
| 147 |
|
| 148 |
if (options.cache_control.must_revalidate) { |
| 149 |
SET_OR_APPEND_CACHE_CONTROL(cache_control); |
| 150 |
ast_str_append(&cache_control, 0, "%s", "must-revalidate"); |
| 151 |
} |
| 152 |
|
| 153 |
if (ast_str_strlen(cache_control)) { |
| 154 |
ast_str_append(&http_header, 0, "%s\r\n", ast_str_buffer(cache_control)); |
| 155 |
} |
| 156 |
|
| 157 |
if (options.expires.tv_sec) { |
| 158 |
struct ast_tm now_time; |
| 159 |
char tmbuf[64]; |
| 160 |
|
| 161 |
ast_localtime(&options.expires, &now_time, NULL); |
| 162 |
ast_strftime(tmbuf, sizeof(tmbuf), "%a, %d %b %Y %T %z", &now_time); |
| 163 |
ast_str_append(&http_header, 0, "Expires: %s\r\n", tmbuf); |
| 164 |
} |
| 165 |
|
| 166 |
if (!ast_strlen_zero(options.etag)) { |
| 167 |
struct ast_variable *v; |
| 168 |
|
| 169 |
ast_str_append(&http_header, 0, "ETag: %s\r\n", options.etag); |
| 170 |
for (v = headers; v; v = v->next) { |
| 171 |
if (!strcasecmp(v->name, "If-None-Match") && !strcasecmp(v->value, options.etag)) { |
| 172 |
unmodified = 1; |
| 173 |
break; |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
if (!unmodified) { |
| 179 |
ast_http_send(ser, method, options.status_code, options.status_text, http_header, NULL, send_file ? fd : 0, 1); |
| 180 |
} else { |
| 181 |
ast_http_send(ser, method, 304, "Not Modified", http_header, NULL, 0, 1); |
| 182 |
} |
| 183 |
|
| 184 |
if (send_file) { |
| 185 |
close(fd); |
| 186 |
unlink(file_name); |
| 187 |
} |
| 188 |
|
| 189 |
ast_free(cache_control); |
| 190 |
|
| 191 |
return 0; |
| 192 |
|
| 193 |
error: |
| 194 |
ast_free(http_header); |
| 195 |
ast_free(cache_control); |
| 196 |
ast_http_request_close_on_completion(ser); |
| 197 |
ast_http_error(ser, 418, "I'm a Teapot", "Please don't ask me to brew coffee."); |
| 198 |
|
| 199 |
return 0; |
| 200 |
} |
| 201 |
|
| 202 |
static struct ast_http_uri test_uri = { |
| 203 |
.description = "HTTP Media Cache Test URI", |
| 204 |
.uri = TEST_URI, |
| 205 |
.callback = http_callback, |
| 206 |
.has_subtree = 1, |
| 207 |
.data = NULL, |
| 208 |
.key = __FILE__, |
| 209 |
}; |
| 210 |
|
| 211 |
static int pre_test_cb(struct ast_test_info *info, struct ast_test *test) |
| 212 |
{ |
| 213 |
memset(&options, 0, sizeof(options)); |
| 214 |
|
| 215 |
return 0; |
| 216 |
} |
| 217 |
|
| 218 |
static void bucket_file_cleanup(void *obj) |
| 219 |
{ |
| 220 |
struct ast_bucket_file *bucket_file = obj; |
| 221 |
|
| 222 |
if (bucket_file) { |
| 223 |
ast_bucket_file_delete(bucket_file); |
| 224 |
ao2_ref(bucket_file, -1); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
AST_TEST_DEFINE(retrieve_content_type) |
| 229 |
{ |
| 230 |
RAII_VAR(struct ast_bucket_file *, bucket_file, NULL, bucket_file_cleanup); |
| 231 |
char uri[1024]; |
| 232 |
|
| 233 |
switch (cmd) { |
| 234 |
case TEST_INIT: |
| 235 |
info->name = __func__; |
| 236 |
info->category = CATEGORY; |
| 237 |
info->summary = "Test retrieval of a resource with a Content-Type header"; |
| 238 |
info->description = |
| 239 |
"This test covers retrieval of a resource whose URL does not end with\n" |
| 240 |
"a parseable extension and whose response includes a Content-Type\n" |
| 241 |
"header that we recognize."; |
| 242 |
return AST_TEST_NOT_RUN; |
| 243 |
case TEST_EXECUTE: |
| 244 |
break; |
| 245 |
} |
| 246 |
|
| 247 |
options.send_file = 1; |
| 248 |
options.status_code = 200; |
| 249 |
options.status_text = "OK"; |
| 250 |
options.content_type = "audio/wav"; |
| 251 |
|
| 252 |
snprintf(uri, sizeof(uri), "%s/%s", server_uri, "foo.wav?account_id=1234"); |
| 253 |
|
| 254 |
bucket_file = ast_bucket_file_retrieve(uri); |
| 255 |
ast_test_validate(test, bucket_file != NULL); |
| 256 |
ast_test_validate(test, !strcmp(uri, ast_sorcery_object_get_id(bucket_file))); |
| 257 |
ast_test_validate(test, !ast_strlen_zero(bucket_file->path)); |
| 258 |
VALIDATE_STR_METADATA(test, bucket_file, "ext", ".wav"); |
| 259 |
|
| 260 |
return AST_TEST_PASS; |
| 261 |
} |
| 262 |
|
| 263 |
AST_TEST_DEFINE(retrieve_parsed_uri) |
| 264 |
{ |
| 265 |
RAII_VAR(struct ast_bucket_file *, bucket_file, NULL, bucket_file_cleanup); |
| 266 |
char uri[1024]; |
| 267 |
|
| 268 |
switch (cmd) { |
| 269 |
case TEST_INIT: |
| 270 |
info->name = __func__; |
| 271 |
info->category = CATEGORY; |
| 272 |
info->summary = "Test retrieval of a resource with a complex URI"; |
| 273 |
info->description = |
| 274 |
"This test covers retrieval of a resource whose URL does not end with\n" |
| 275 |
"a parseable extension, but the path portion of the URL does end with\n" |
| 276 |
"parseable extension."; |
| 277 |
return AST_TEST_NOT_RUN; |
| 278 |
case TEST_EXECUTE: |
| 279 |
break; |
| 280 |
} |
| 281 |
|
| 282 |
options.send_file = 1; |
| 283 |
options.status_code = 200; |
| 284 |
options.status_text = "OK"; |
| 285 |
|
| 286 |
snprintf(uri, sizeof(uri), "%s/%s", server_uri, "foo.wav?account_id=1234"); |
| 287 |
|
| 288 |
bucket_file = ast_bucket_file_retrieve(uri); |
| 289 |
ast_test_validate(test, bucket_file != NULL); |
| 290 |
ast_test_validate(test, !strcmp(uri, ast_sorcery_object_get_id(bucket_file))); |
| 291 |
ast_test_validate(test, !ast_strlen_zero(bucket_file->path)); |
| 292 |
VALIDATE_STR_METADATA(test, bucket_file, "ext", ".wav"); |
| 293 |
|
| 294 |
return AST_TEST_PASS; |
| 295 |
} |
| 296 |
|
| 297 |
AST_TEST_DEFINE(retrieve_cache_control_directives) |
| 298 |
{ |
| 299 |
RAII_VAR(struct ast_bucket_file *, bucket_file, NULL, bucket_file_cleanup); |
| 300 |
struct timeval now = ast_tvnow(); |
| 301 |
char uri[1024]; |
| 302 |
|
| 303 |
switch (cmd) { |
| 304 |
case TEST_INIT: |
| 305 |
info->name = __func__; |
| 306 |
info->category = CATEGORY; |
| 307 |
info->summary = "Test retrieval of a resource with Cache-Control directives that affect staleness"; |
| 308 |
info->description = |
| 309 |
"This test covers retrieval of a resource with the Cache-Control header,\n" |
| 310 |
"which specifies no-cache and/or must-revalidate."; |
| 311 |
return AST_TEST_NOT_RUN; |
| 312 |
case TEST_EXECUTE: |
| 313 |
break; |
| 314 |
} |
| 315 |
|
| 316 |
snprintf(uri, sizeof(uri), "%s/%s", server_uri, "foo.wav"); |
| 317 |
|
| 318 |
options.send_file = 1; |
| 319 |
options.status_code = 200; |
| 320 |
options.status_text = "OK"; |
| 321 |
|
| 322 |
ast_test_status_update(test, "Testing no-cache...\n"); |
| 323 |
options.cache_control.no_cache = 1; |
| 324 |
bucket_file = ast_bucket_file_retrieve(uri); |
| 325 |
ast_test_validate(test, bucket_file != NULL); |
| 326 |
ast_test_validate(test, ast_bucket_file_is_stale(bucket_file) == 1); |
| 327 |
bucket_file_cleanup(bucket_file); |
| 328 |
|
| 329 |
ast_test_status_update(test, "Testing no-cache with ETag...\n"); |
| 330 |
options.cache_control.no_cache = 1; |
| 331 |
options.etag = "123456789"; |
| 332 |
bucket_file = ast_bucket_file_retrieve(uri); |
| 333 |
ast_test_validate(test, bucket_file != NULL); |
| 334 |
ast_test_validate(test, ast_bucket_file_is_stale(bucket_file) == 0); |
| 335 |
bucket_file_cleanup(bucket_file); |
| 336 |
|
| 337 |
options.etag = NULL; |
| 338 |
|
| 339 |
ast_test_status_update(test, "Testing no-cache with max-age...\n"); |
| 340 |
options.cache_control.no_cache = 1; |
| 341 |
options.cache_control.maxage = 300; |
| 342 |
bucket_file = ast_bucket_file_retrieve(uri); |
| 343 |
ast_test_validate(test, bucket_file != NULL); |
| 344 |
VALIDATE_EXPIRES(test, bucket_file, now.tv_sec + 300, 3); |
| 345 |
ast_test_validate(test, ast_bucket_file_is_stale(bucket_file) == 1); |
| 346 |
bucket_file_cleanup(bucket_file); |
| 347 |
|
| 348 |
options.cache_control.maxage = 0; |
| 349 |
options.cache_control.no_cache = 0; |
| 350 |
|
| 351 |
ast_test_status_update(test, "Testing must-revalidate...\n"); |
| 352 |
options.cache_control.must_revalidate = 1; |
| 353 |
bucket_file = ast_bucket_file_retrieve(uri); |
| 354 |
ast_test_validate(test, bucket_file != NULL); |
| 355 |
ast_test_validate(test, ast_bucket_file_is_stale(bucket_file) == 1); |
| 356 |
bucket_file_cleanup(bucket_file); |
| 357 |
|
| 358 |
ast_test_status_update(test, "Testing must-revalidate with ETag...\n"); |
| 359 |
options.cache_control.must_revalidate = 1; |
| 360 |
options.etag = "123456789"; |
| 361 |
bucket_file = ast_bucket_file_retrieve(uri); |
| 362 |
ast_test_validate(test, bucket_file != NULL); |
| 363 |
ast_test_validate(test, ast_bucket_file_is_stale(bucket_file) == 0); |
| 364 |
bucket_file_cleanup(bucket_file); |
| 365 |
|
| 366 |
options.etag = NULL; |
| 367 |
|
| 368 |
ast_test_status_update(test, "Testing must-revalidate with max-age...\n"); |
| 369 |
options.cache_control.must_revalidate = 1; |
| 370 |
options.cache_control.maxage = 300; |
| 371 |
bucket_file = ast_bucket_file_retrieve(uri); |
| 372 |
ast_test_validate(test, bucket_file != NULL); |
| 373 |
VALIDATE_EXPIRES(test, bucket_file, now.tv_sec + 300, 3); |
| 374 |
ast_test_validate(test, ast_bucket_file_is_stale(bucket_file) == 1); |
| 375 |
|
| 376 |
return AST_TEST_PASS; |
| 377 |
} |
| 378 |
|
| 379 |
AST_TEST_DEFINE(retrieve_cache_control_age) |
| 380 |
{ |
| 381 |
RAII_VAR(struct ast_bucket_file *, bucket_file, NULL, bucket_file_cleanup); |
| 382 |
struct timeval now = ast_tvnow(); |
| 383 |
char uri[1024]; |
| 384 |
|
| 385 |
switch (cmd) { |
| 386 |
case TEST_INIT: |
| 387 |
info->name = __func__; |
| 388 |
info->category = CATEGORY; |
| 389 |
info->summary = "Test retrieval of a resource with age specifiers in Cache-Control"; |
| 390 |
info->description = |
| 391 |
"This test covers retrieval of a resource with the Cache-Control header,\n" |
| 392 |
"which specifies max-age and/or s-maxage. The test verifies proper precedence\n" |
| 393 |
"ordering of the header attributes, along with its relation if the Expires\n" |
| 394 |
"header is present."; |
| 395 |
return AST_TEST_NOT_RUN; |
| 396 |
case TEST_EXECUTE: |
| 397 |
break; |
| 398 |
} |
| 399 |
|
| 400 |
snprintf(uri, sizeof(uri), "%s/%s", server_uri, "foo.wav"); |
| 401 |
|
| 402 |
options.send_file = 1; |
| 403 |
options.status_code = 200; |
| 404 |
options.status_text = "OK"; |
| 405 |
|
| 406 |
ast_test_status_update(test, "Testing max-age...\n"); |
| 407 |
options.cache_control.maxage = 300; |
| 408 |
bucket_file = ast_bucket_file_retrieve(uri); |
| 409 |
ast_test_validate(test, bucket_file != NULL); |
| 410 |
VALIDATE_EXPIRES(test, bucket_file, now.tv_sec + 300, 3); |
| 411 |
ast_test_validate(test, ast_bucket_file_is_stale(bucket_file) == 0); |
| 412 |
bucket_file_cleanup(bucket_file); |
| 413 |
|
| 414 |
ast_test_status_update(test, "Testing s-maxage...\n"); |
| 415 |
now = ast_tvnow(); |
| 416 |
options.cache_control.maxage = 0; |
| 417 |
options.cache_control.s_maxage = 300; |
| 418 |
bucket_file = ast_bucket_file_retrieve(uri); |
| 419 |
ast_test_validate(test, bucket_file != NULL); |
| 420 |
VALIDATE_EXPIRES(test, bucket_file, now.tv_sec + 300, 3); |
| 421 |
ast_test_validate(test, ast_bucket_file_is_stale(bucket_file) == 0); |
| 422 |
bucket_file_cleanup(bucket_file); |
| 423 |
|
| 424 |
ast_test_status_update(test, "Testing max-age and s-maxage...\n"); |
| 425 |
now = ast_tvnow(); |
| 426 |
options.cache_control.maxage = 300; |
| 427 |
options.cache_control.s_maxage = 600; |
| 428 |
bucket_file = ast_bucket_file_retrieve(uri); |
| 429 |
ast_test_validate(test, bucket_file != NULL); |
| 430 |
VALIDATE_EXPIRES(test, bucket_file, now.tv_sec + 600, 3); |
| 431 |
ast_test_validate(test, ast_bucket_file_is_stale(bucket_file) == 0); |
| 432 |
bucket_file_cleanup(bucket_file); |
| 433 |
|
| 434 |
ast_test_status_update(test, "Testing max-age and Expires...\n"); |
| 435 |
now = ast_tvnow(); |
| 436 |
options.cache_control.maxage = 300; |
| 437 |
options.cache_control.s_maxage = 0; |
| 438 |
options.expires.tv_sec = now.tv_sec + 3000; |
| 439 |
bucket_file = ast_bucket_file_retrieve(uri); |
| 440 |
ast_test_validate(test, bucket_file != NULL); |
| 441 |
VALIDATE_EXPIRES(test, bucket_file, now.tv_sec + 300, 3); |
| 442 |
ast_test_validate(test, ast_bucket_file_is_stale(bucket_file) == 0); |
| 443 |
bucket_file_cleanup(bucket_file); |
| 444 |
|
| 445 |
ast_test_status_update(test, "Testing s-maxage and Expires...\n"); |
| 446 |
now = ast_tvnow(); |
| 447 |
options.cache_control.maxage = 0; |
| 448 |
options.cache_control.s_maxage = 300; |
| 449 |
options.expires.tv_sec = now.tv_sec + 3000; |
| 450 |
bucket_file = ast_bucket_file_retrieve(uri); |
| 451 |
ast_test_validate(test, bucket_file != NULL); |
| 452 |
VALIDATE_EXPIRES(test, bucket_file, now.tv_sec + 300, 3); |
| 453 |
ast_test_validate(test, ast_bucket_file_is_stale(bucket_file) == 0); |
| 454 |
bucket_file_cleanup(bucket_file); |
| 455 |
|
| 456 |
ast_test_status_update(test, "Testing s-maxage and Expires...\n"); |
| 457 |
now = ast_tvnow(); |
| 458 |
options.cache_control.maxage = 0; |
| 459 |
options.cache_control.s_maxage = 300; |
| 460 |
options.expires.tv_sec = now.tv_sec + 3000; |
| 461 |
bucket_file = ast_bucket_file_retrieve(uri); |
| 462 |
ast_test_validate(test, bucket_file != NULL); |
| 463 |
VALIDATE_EXPIRES(test, bucket_file, now.tv_sec + 300, 3); |
| 464 |
ast_test_validate(test, ast_bucket_file_is_stale(bucket_file) == 0); |
| 465 |
bucket_file_cleanup(bucket_file); |
| 466 |
|
| 467 |
ast_test_status_update(test, "Testing max-age, s-maxage, and Expires...\n"); |
| 468 |
now = ast_tvnow(); |
| 469 |
options.cache_control.maxage = 300; |
| 470 |
options.cache_control.s_maxage = 600; |
| 471 |
options.expires.tv_sec = now.tv_sec + 3000; |
| 472 |
bucket_file = ast_bucket_file_retrieve(uri); |
| 473 |
ast_test_validate(test, bucket_file != NULL); |
| 474 |
VALIDATE_EXPIRES(test, bucket_file, now.tv_sec + 600, 3); |
| 475 |
ast_test_validate(test, ast_bucket_file_is_stale(bucket_file) == 0); |
| 476 |
|
| 477 |
return AST_TEST_PASS; |
| 478 |
} |
| 479 |
|
| 480 |
AST_TEST_DEFINE(retrieve_etag_expired) |
| 481 |
{ |
| 482 |
RAII_VAR(struct ast_bucket_file *, bucket_file, NULL, bucket_file_cleanup); |
| 483 |
struct timeval now = ast_tvnow(); |
| 484 |
char uri[1024]; |
| 485 |
|
| 486 |
switch (cmd) { |
| 487 |
case TEST_INIT: |
| 488 |
info->name = __func__; |
| 489 |
info->category = CATEGORY; |
| 490 |
info->summary = "Test retrieval of an expired resource with an ETag"; |
| 491 |
info->description = |
| 492 |
"This test covers a staleness check of a resource with an ETag\n" |
| 493 |
"that has also expired. It guarantees that even if a resource\n" |
| 494 |
"is expired, we will still not consider it stale if the resource\n" |
| 495 |
"has not changed per the ETag value."; |
| 496 |
return AST_TEST_NOT_RUN; |
| 497 |
case TEST_EXECUTE: |
| 498 |
break; |
| 499 |
} |
| 500 |
|
| 501 |
options.send_file = 1; |
| 502 |
options.status_code = 200; |
| 503 |
options.status_text = "OK"; |
| 504 |
options.etag = "123456789"; |
| 505 |
options.expires.tv_sec = now.tv_sec - 1; |
| 506 |
|
| 507 |
snprintf(uri, sizeof(uri), "%s/%s", server_uri, "foo.wav"); |
| 508 |
|
| 509 |
bucket_file = ast_bucket_file_retrieve(uri); |
| 510 |
ast_test_validate(test, bucket_file != NULL); |
| 511 |
ast_test_validate(test, !strcmp(uri, ast_sorcery_object_get_id(bucket_file))); |
| 512 |
ast_test_validate(test, !ast_strlen_zero(bucket_file->path)); |
| 513 |
VALIDATE_STR_METADATA(test, bucket_file, "etag", options.etag); |
| 514 |
VALIDATE_EXPIRES(test, bucket_file, now.tv_sec - 1, 3); |
| 515 |
|
| 516 |
ast_test_validate(test, ast_bucket_file_is_stale(bucket_file) == 0); |
| 517 |
|
| 518 |
return AST_TEST_PASS; |
| 519 |
} |
| 520 |
|
| 521 |
AST_TEST_DEFINE(retrieve_expires) |
| 522 |
{ |
| 523 |
RAII_VAR(struct ast_bucket_file *, bucket_file, NULL, bucket_file_cleanup); |
| 524 |
struct timeval now = ast_tvnow(); |
| 525 |
char uri[1024]; |
| 526 |
|
| 527 |
switch (cmd) { |
| 528 |
case TEST_INIT: |
| 529 |
info->name = __func__; |
| 530 |
info->category = CATEGORY; |
| 531 |
info->summary = "Test retrieval with explicit expiration"; |
| 532 |
info->description = |
| 533 |
"This test covers retrieving a resource that has an Expires.\n" |
| 534 |
"After retrieval of the resource, staleness is checked. With\n" |
| 535 |
"a non-expired resource, we expect the resource to not be stale.\n" |
| 536 |
"When the expiration has occurred, we expect the staleness check\n" |
| 537 |
"to fail."; |
| 538 |
return AST_TEST_NOT_RUN; |
| 539 |
case TEST_EXECUTE: |
| 540 |
break; |
| 541 |
} |
| 542 |
|
| 543 |
options.send_file = 1; |
| 544 |
options.status_code = 200; |
| 545 |
options.status_text = "OK"; |
| 546 |
options.expires.tv_sec = now.tv_sec + 3000; |
| 547 |
|
| 548 |
snprintf(uri, sizeof(uri), "%s/%s", server_uri, "foo.wav"); |
| 549 |
|
| 550 |
bucket_file = ast_bucket_file_retrieve(uri); |
| 551 |
ast_test_validate(test, bucket_file != NULL); |
| 552 |
ast_test_validate(test, !strcmp(uri, ast_sorcery_object_get_id(bucket_file))); |
| 553 |
ast_test_validate(test, !ast_strlen_zero(bucket_file->path)); |
| 554 |
VALIDATE_EXPIRES(test, bucket_file, now.tv_sec + 3000, 3); |
| 555 |
|
| 556 |
ast_test_validate(test, ast_bucket_file_is_stale(bucket_file) == 0); |
| 557 |
|
| 558 |
|
| 559 |
bucket_file_cleanup(bucket_file); |
| 560 |
|
| 561 |
options.expires.tv_sec = now.tv_sec - 1; |
| 562 |
bucket_file = ast_bucket_file_retrieve(uri); |
| 563 |
ast_test_validate(test, bucket_file != NULL); |
| 564 |
VALIDATE_EXPIRES(test, bucket_file, now.tv_sec - 1, 3); |
| 565 |
|
| 566 |
ast_test_validate(test, ast_bucket_file_is_stale(bucket_file) == 1); |
| 567 |
|
| 568 |
return AST_TEST_PASS; |
| 569 |
} |
| 570 |
|
| 571 |
AST_TEST_DEFINE(retrieve_etag) |
| 572 |
{ |
| 573 |
RAII_VAR(struct ast_bucket_file *, bucket_file, NULL, bucket_file_cleanup); |
| 574 |
struct timeval now = ast_tvnow(); |
| 575 |
char uri[1024]; |
| 576 |
|
| 577 |
switch (cmd) { |
| 578 |
case TEST_INIT: |
| 579 |
info->name = __func__; |
| 580 |
info->category = CATEGORY; |
| 581 |
info->summary = "Test retrieval with an ETag"; |
| 582 |
info->description = |
| 583 |
"This test covers retrieving a resource that has an ETag.\n" |
| 584 |
"After retrieval of the resource, staleness is checked. With\n" |
| 585 |
"matching ETags, we expect the resource to not be stale. When\n" |
| 586 |
"the ETag does not match, we expect the resource to be stale."; |
| 587 |
return AST_TEST_NOT_RUN; |
| 588 |
case TEST_EXECUTE: |
| 589 |
break; |
| 590 |
} |
| 591 |
|
| 592 |
options.send_file = 1; |
| 593 |
options.status_code = 200; |
| 594 |
options.status_text = "OK"; |
| 595 |
options.etag = "123456789"; |
| 596 |
|
| 597 |
snprintf(uri, sizeof(uri), "%s/%s", server_uri, "foo.wav"); |
| 598 |
|
| 599 |
bucket_file = ast_bucket_file_retrieve(uri); |
| 600 |
ast_test_validate(test, bucket_file != NULL); |
| 601 |
ast_test_validate(test, !strcmp(uri, ast_sorcery_object_get_id(bucket_file))); |
| 602 |
ast_test_validate(test, !ast_strlen_zero(bucket_file->path)); |
| 603 |
VALIDATE_STR_METADATA(test, bucket_file, "etag", options.etag); |
| 604 |
VALIDATE_EXPIRES(test, bucket_file, now.tv_sec, 3); |
| 605 |
|
| 606 |
ast_test_validate(test, ast_bucket_file_is_stale(bucket_file) == 0); |
| 607 |
|
| 608 |
options.etag = "99999999"; |
| 609 |
ast_test_validate(test, ast_bucket_file_is_stale(bucket_file) == 1); |
| 610 |
|
| 611 |
return AST_TEST_PASS; |
| 612 |
} |
| 613 |
|
| 614 |
AST_TEST_DEFINE(retrieve_nominal) |
| 615 |
{ |
| 616 |
RAII_VAR(struct ast_bucket_file *, bucket_file, NULL, bucket_file_cleanup); |
| 617 |
struct timeval now = ast_tvnow(); |
| 618 |
char uri[1024]; |
| 619 |
|
| 620 |
switch (cmd) { |
| 621 |
case TEST_INIT: |
| 622 |
info->name = __func__; |
| 623 |
info->category = CATEGORY; |
| 624 |
info->summary = "Test nominal retrieval"; |
| 625 |
info->description = |
| 626 |
"Test nominal retrieval of a resource."; |
| 627 |
return AST_TEST_NOT_RUN; |
| 628 |
case TEST_EXECUTE: |
| 629 |
break; |
| 630 |
} |
| 631 |
|
| 632 |
options.send_file = 1; |
| 633 |
options.status_code = 200; |
| 634 |
options.status_text = "OK"; |
| 635 |
|
| 636 |
snprintf(uri, sizeof(uri), "%s/%s", server_uri, "foo.wav"); |
| 637 |
|
| 638 |
bucket_file = ast_bucket_file_retrieve(uri); |
| 639 |
ast_test_validate(test, bucket_file != NULL); |
| 640 |
ast_test_validate(test, !strcmp(uri, ast_sorcery_object_get_id(bucket_file))); |
| 641 |
ast_test_validate(test, !ast_strlen_zero(bucket_file->path)); |
| 642 |
VALIDATE_EXPIRES(test, bucket_file, now.tv_sec, 3); |
| 643 |
|
| 644 |
return AST_TEST_PASS; |
| 645 |
} |
| 646 |
|
| 647 |
AST_TEST_DEFINE(create_nominal) |
| 648 |
{ |
| 649 |
RAII_VAR(struct ast_bucket_file *, bucket_file, NULL, bucket_file_cleanup); |
| 650 |
struct timeval now = ast_tvnow(); |
| 651 |
char uri[1024]; |
| 652 |
|
| 653 |
switch (cmd) { |
| 654 |
case TEST_INIT: |
| 655 |
info->name = __func__; |
| 656 |
info->category = CATEGORY; |
| 657 |
info->summary = "Test nominal creation"; |
| 658 |
info->description = |
| 659 |
"Test nominal creation of a resource."; |
| 660 |
return AST_TEST_NOT_RUN; |
| 661 |
case TEST_EXECUTE: |
| 662 |
break; |
| 663 |
} |
| 664 |
|
| 665 |
options.send_file = 1; |
| 666 |
options.status_code = 200; |
| 667 |
options.status_text = "OK"; |
| 668 |
|
| 669 |
snprintf(uri, sizeof(uri), "%s/%s", server_uri, "foo.wav"); |
| 670 |
|
| 671 |
bucket_file = ast_bucket_file_alloc(uri); |
| 672 |
ast_test_validate(test, bucket_file != NULL); |
| 673 |
ast_test_validate(test, ast_bucket_file_temporary_create(bucket_file) == 0); |
| 674 |
ast_test_validate(test, ast_bucket_file_create(bucket_file) == 0); |
| 675 |
VALIDATE_EXPIRES(test, bucket_file, now.tv_sec, 3); |
| 676 |
|
| 677 |
return AST_TEST_PASS; |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
static int process_config(int reload) |
| 682 |
{ |
| 683 |
struct ast_config *config; |
| 684 |
struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 }; |
| 685 |
const char *bindaddr; |
| 686 |
const char *bindport; |
| 687 |
const char *prefix; |
| 688 |
const char *enabled; |
| 689 |
|
| 690 |
config = ast_config_load("http.conf", config_flags); |
| 691 |
if (!config || config == CONFIG_STATUS_FILEINVALID) { |
| 692 |
return -1; |
| 693 |
} else if (config == CONFIG_STATUS_FILEUNCHANGED) { |
| 694 |
return 0; |
| 695 |
} |
| 696 |
|
| 697 |
enabled = ast_config_option(config, "general", "enabled"); |
| 698 |
if (!enabled || ast_false(enabled)) { |
| 699 |
ast_config_destroy(config); |
| 700 |
return -1; |
| 701 |
} |
| 702 |
|
| 703 |
|
| 704 |
bindaddr = ast_config_option(config, "general", "bindaddr"); |
| 705 |
if (!bindaddr) { |
| 706 |
ast_config_destroy(config); |
| 707 |
return -1; |
| 708 |
} |
| 709 |
|
| 710 |
bindport = ast_config_option(config, "general", "bindport"); |
| 711 |
if (!bindport) { |
| 712 |
bindport = "8088"; |
| 713 |
} |
| 714 |
|
| 715 |
prefix = ast_config_option(config, "general", "prefix"); |
| 716 |
|
| 717 |
snprintf(server_uri, sizeof(server_uri), "http://%s:%s%s/%s", bindaddr, bindport, S_OR(prefix, ""), TEST_URI); |
| 718 |
|
| 719 |
ast_config_destroy(config); |
| 720 |
|
| 721 |
return 0; |
| 722 |
} |
| 723 |
|
| 724 |
static int reload_module(void) |
| 725 |
{ |
| 726 |
return process_config(1); |
| 727 |
} |
| 728 |
|
| 729 |
static int load_module(void) |
| 730 |
{ |
| 731 |
if (process_config(0)) { |
| 732 |
return AST_MODULE_LOAD_DECLINE; |
| 733 |
} |
| 734 |
|
| 735 |
if (ast_http_uri_link(&test_uri)) { |
| 736 |
return AST_MODULE_LOAD_DECLINE; |
| 737 |
} |
| 738 |
|
| 739 |
AST_TEST_REGISTER(create_nominal); |
| 740 |
|
| 741 |
AST_TEST_REGISTER(retrieve_nominal); |
| 742 |
AST_TEST_REGISTER(retrieve_etag); |
| 743 |
AST_TEST_REGISTER(retrieve_expires); |
| 744 |
AST_TEST_REGISTER(retrieve_etag_expired); |
| 745 |
AST_TEST_REGISTER(retrieve_cache_control_age); |
| 746 |
AST_TEST_REGISTER(retrieve_cache_control_directives); |
| 747 |
AST_TEST_REGISTER(retrieve_parsed_uri); |
| 748 |
AST_TEST_REGISTER(retrieve_content_type); |
| 749 |
|
| 750 |
ast_test_register_init(CATEGORY, pre_test_cb); |
| 751 |
|
| 752 |
return AST_MODULE_LOAD_SUCCESS; |
| 753 |
} |
| 754 |
|
| 755 |
static int unload_module(void) |
| 756 |
{ |
| 757 |
ast_http_uri_unlink(&test_uri); |
| 758 |
|
| 759 |
AST_TEST_UNREGISTER(create_nominal); |
| 760 |
|
| 761 |
AST_TEST_UNREGISTER(retrieve_nominal); |
| 762 |
AST_TEST_UNREGISTER(retrieve_etag); |
| 763 |
AST_TEST_UNREGISTER(retrieve_expires); |
| 764 |
AST_TEST_UNREGISTER(retrieve_etag_expired); |
| 765 |
AST_TEST_UNREGISTER(retrieve_cache_control_age); |
| 766 |
AST_TEST_UNREGISTER(retrieve_cache_control_directives); |
| 767 |
AST_TEST_UNREGISTER(retrieve_parsed_uri); |
| 768 |
AST_TEST_UNREGISTER(retrieve_content_type); |
| 769 |
|
| 770 |
return 0; |
| 771 |
} |
| 772 |
|
| 773 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "HTTP Media Cache Backend Tests", |
| 774 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 775 |
.load = load_module, |
| 776 |
.reload = reload_module, |
| 777 |
.unload = unload_module, |
| 778 |
.requires = "res_http_media_cache", |
| 779 |
); |