44#define CATEGORY "/res/http_media_cache/"
46#define TEST_URI "test_media_cache"
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); \
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))); \
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))); \
84#define SET_OR_APPEND_CACHE_CONTROL(str) do { \
85 if (!ast_str_strlen((str))) { \
86 ast_str_set(&(str), 0, "%s", "cache-control: "); \
88 ast_str_append(&(str), 0, "%s", ", "); \
94 char file_name[64] =
"/tmp/test-media-cache-XXXXXX";
108 fd = mkstemp(file_name);
114 memset(
buf, 1,
sizeof(
buf));
115 if (write(fd,
buf,
sizeof(
buf)) !=
sizeof(
buf)) {
122 fd = open(file_name, 0);
162 ast_strftime(tmbuf,
sizeof(tmbuf),
"%a, %d %b %Y %T %z", &now_time);
170 for (v = headers; v; v = v->
next) {
197 ast_http_error(ser, 418,
"I'm a Teapot",
"Please don't ask me to brew coffee.");
235 info->name = __func__;
237 info->summary =
"Test retrieval of a resource with a Content-Type header";
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.";
252 snprintf(uri,
sizeof(uri),
"%s/%s",
server_uri,
"foo.wav?account_id=1234");
255 ast_test_validate(
test, bucket_file !=
NULL);
270 info->name = __func__;
272 info->summary =
"Test retrieval of a resource with a complex URI";
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.";
286 snprintf(uri,
sizeof(uri),
"%s/%s",
server_uri,
"foo.wav?account_id=1234");
289 ast_test_validate(
test, bucket_file !=
NULL);
305 info->name = __func__;
307 info->summary =
"Test retrieval of a resource with Cache-Control directives that affect staleness";
309 "This test covers retrieval of a resource with the Cache-Control header,\n"
310 "which specifies no-cache and/or must-revalidate.";
316 snprintf(uri,
sizeof(uri),
"%s/%s",
server_uri,
"foo.wav");
325 ast_test_validate(
test, bucket_file !=
NULL);
333 ast_test_validate(
test, bucket_file !=
NULL);
343 ast_test_validate(
test, bucket_file !=
NULL);
354 ast_test_validate(
test, bucket_file !=
NULL);
362 ast_test_validate(
test, bucket_file !=
NULL);
372 ast_test_validate(
test, bucket_file !=
NULL);
387 info->name = __func__;
389 info->summary =
"Test retrieval of a resource with age specifiers in Cache-Control";
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.";
400 snprintf(uri,
sizeof(uri),
"%s/%s",
server_uri,
"foo.wav");
409 ast_test_validate(
test, bucket_file !=
NULL);
419 ast_test_validate(
test, bucket_file !=
NULL);
429 ast_test_validate(
test, bucket_file !=
NULL);
440 ast_test_validate(
test, bucket_file !=
NULL);
451 ast_test_validate(
test, bucket_file !=
NULL);
462 ast_test_validate(
test, bucket_file !=
NULL);
473 ast_test_validate(
test, bucket_file !=
NULL);
488 info->name = __func__;
490 info->summary =
"Test retrieval of an expired resource with an ETag";
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.";
507 snprintf(uri,
sizeof(uri),
"%s/%s",
server_uri,
"foo.wav");
510 ast_test_validate(
test, bucket_file !=
NULL);
529 info->name = __func__;
531 info->summary =
"Test retrieval with explicit expiration";
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"
548 snprintf(uri,
sizeof(uri),
"%s/%s",
server_uri,
"foo.wav");
551 ast_test_validate(
test, bucket_file !=
NULL);
563 ast_test_validate(
test, bucket_file !=
NULL);
579 info->name = __func__;
581 info->summary =
"Test retrieval with an ETag";
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.";
597 snprintf(uri,
sizeof(uri),
"%s/%s",
server_uri,
"foo.wav");
600 ast_test_validate(
test, bucket_file !=
NULL);
622 info->name = __func__;
624 info->summary =
"Test nominal retrieval";
626 "Test nominal retrieval of a resource.";
636 snprintf(uri,
sizeof(uri),
"%s/%s",
server_uri,
"foo.wav");
639 ast_test_validate(
test, bucket_file !=
NULL);
655 info->name = __func__;
657 info->summary =
"Test nominal creation";
659 "Test nominal creation of a resource.";
669 snprintf(uri,
sizeof(uri),
"%s/%s",
server_uri,
"foo.wav");
672 ast_test_validate(
test, bucket_file !=
NULL);
686 const char *bindport;
778 .
requires =
"res_http_media_cache",
Asterisk main include file. File version handling, generic pbx functions.
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
struct ast_bucket_file * ast_bucket_file_alloc(const char *uri)
Allocate a new bucket file.
int ast_bucket_file_temporary_create(struct ast_bucket_file *file)
Common file snapshot creation callback for creating a temporary file.
int ast_bucket_file_create(struct ast_bucket_file *file)
Create a new bucket file in backend storage.
struct ast_bucket_file * ast_bucket_file_retrieve(const char *uri)
Retrieve a bucket file.
int ast_bucket_file_delete(struct ast_bucket_file *file)
Delete a bucket file from backend storage.
int ast_bucket_file_is_stale(struct ast_bucket_file *file)
Retrieve whether or not the backing datastore views the bucket file as stale.
struct ast_sockaddr bindaddr
static const char config[]
static char prefix[MAX_PREFIX]
Support for Private Asterisk HTTP Servers.
void ast_http_send(struct ast_tcptls_session_instance *ser, enum ast_http_method method, int status_code, const char *status_title, struct ast_str *http_header, struct ast_str *out, int fd, unsigned int static_content)
Generic function for sending HTTP/1.1 response.
ast_http_method
HTTP Request methods known by Asterisk.
void ast_http_uri_unlink(struct ast_http_uri *urihandler)
Unregister a URI handler.
void ast_http_request_close_on_completion(struct ast_tcptls_session_instance *ser)
Request the HTTP connection be closed after this HTTP request.
void ast_http_error(struct ast_tcptls_session_instance *ser, int status, const char *title, const char *text)
Send HTTP error message and close socket.
int ast_http_uri_link(struct ast_http_uri *urihandler)
Register a URI handler.
#define ast_config_load(filename, flags)
Load a config file.
@ CONFIG_FLAG_FILEUNCHANGED
#define CONFIG_STATUS_FILEUNCHANGED
#define CONFIG_STATUS_FILEINVALID
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
const char * ast_config_option(struct ast_config *cfg, const char *cat, const char *var)
Retrieve a configuration variable within the configuration set.
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
Asterisk module definitions.
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
@ AST_MODULE_SUPPORT_CORE
#define ASTERISK_GPL_KEY
The text the key() function should return.
@ AST_MODULE_LOAD_SUCCESS
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
int attribute_pure ast_false(const char *val)
Make sure something is false. Determine if a string containing a boolean value is "false"....
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Bucket file structure, contains reference to file and information about it.
Structure used to handle boolean flags.
Definition of a URI handler.
Support for dynamic strings.
describes a server instance
Contains all the initialization information required to store a new test definition.
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
struct test_options::@503 cache_control
const char * content_type
#define AST_TEST_REGISTER(cb)
#define ast_test_status_update(a, b, c...)
#define AST_TEST_UNREGISTER(cb)
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
int error(const char *format,...)
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.