Asterisk - The Open Source Telephony Project GIT-master-f36a736
Macros | Functions | Variables
test_media_cache.c File Reference

Tests for the media cache API. More...

#include "asterisk.h"
#include "asterisk/utils.h"
#include "asterisk/module.h"
#include "asterisk/test.h"
#include "asterisk/bucket.h"
#include "asterisk/media_cache.h"
Include dependency graph for test_media_cache.c:

Go to the source code of this file.

Macros

#define CATEGORY   "/main/media_cache/"
 
#define INVALID_RESOURCE   "httptest://localhost:8088/test_media_cache/bad.wav"
 
#define INVALID_SCHEME   "foo://localhost:8088/test_media_cache/monkeys.wav"
 
#define NO_SCHEME   "localhost:8088/test_media_cache/monkeys.wav"
 
#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"
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 AST_TEST_DEFINE (create_update_metadata)
 
 AST_TEST_DEFINE (create_update_nominal)
 
 AST_TEST_DEFINE (create_update_off_nominal)
 
 AST_TEST_DEFINE (exists_nominal)
 
 AST_TEST_DEFINE (exists_off_nominal)
 
static int bucket_http_test_wizard_create (const struct ast_sorcery *sorcery, void *data, void *object)
 
static int bucket_http_test_wizard_delete (const struct ast_sorcery *sorcery, void *data, void *object)
 
static void * bucket_http_test_wizard_retrieve_id (const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
 
static int bucket_http_test_wizard_update (const struct ast_sorcery *sorcery, void *data, void *object)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Media Cache Tests" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_sorcery_wizard bucket_file_test_wizard
 
static struct ast_sorcery_wizard bucket_test_wizard
 

Detailed Description

Tests for the media cache API.

Author
Matt Jordan <mjordan@digium.com> 

Definition in file test_media_cache.c.

Macro Definition Documentation

◆ CATEGORY

#define CATEGORY   "/main/media_cache/"

The unit test category

Definition at line 42 of file test_media_cache.c.

◆ INVALID_RESOURCE

#define INVALID_RESOURCE   "httptest://localhost:8088/test_media_cache/bad.wav"

An 'invalid' resource for the test bucket behind the media cache facade

Definition at line 48 of file test_media_cache.c.

◆ INVALID_SCHEME

#define INVALID_SCHEME   "foo://localhost:8088/test_media_cache/monkeys.wav"

An 'invalid' scheme, not mapping to a valid bucket backend

Definition at line 51 of file test_media_cache.c.

◆ NO_SCHEME

#define NO_SCHEME   "localhost:8088/test_media_cache/monkeys.wav"

A URI with no scheme

Definition at line 54 of file test_media_cache.c.

◆ VALID_RESOURCE

#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"

A 'valid' long resource for the test bucket behind the media cache facade

Definition at line 45 of file test_media_cache.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 416 of file test_media_cache.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 416 of file test_media_cache.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 416 of file test_media_cache.c.

◆ AST_TEST_DEFINE() [1/5]

AST_TEST_DEFINE ( create_update_metadata  )

Definition at line 314 of file test_media_cache.c.

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 /* Create two local files to associate with a resource */
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 /* We don't need anything in the file */
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
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
372 actual_metadata, sizeof(actual_metadata));
373 ast_test_validate(test, res == 0);
374 ast_test_validate(test, strcmp(actual_metadata, "value1") == 0);
375
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}
#define PATH_MAX
Definition: asterisk.h:40
static int tmp()
Definition: bt_open.c:389
#define ast_variable_new(name, value, filename)
#define ast_variable_list_append(head, new_var)
int ast_media_cache_retrieve(const char *uri, const char *preferred_file_name, char *file_path, size_t len)
Retrieve an item from the cache.
Definition: media_cache.c:157
int ast_media_cache_create_or_update(const char *uri, const char *file_path, struct ast_variable *metadata)
Create/update a cached media item.
Definition: media_cache.c:268
int ast_media_cache_retrieve_metadata(const char *uri, const char *key, char *value, size_t len)
Retrieve metadata from an item in the cache.
Definition: media_cache.c:241
def info(msg)
#define NULL
Definition: resample.c:96
Structure for variables, used for configurations and for channel variables.
@ TEST_INIT
Definition: test.h:200
@ TEST_EXECUTE
Definition: test.h:201
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
@ AST_TEST_PASS
Definition: test.h:195
@ AST_TEST_FAIL
Definition: test.h:196
@ AST_TEST_NOT_RUN
Definition: test.h:194
#define VALID_RESOURCE
#define CATEGORY

References ast_media_cache_create_or_update(), ast_media_cache_retrieve(), ast_media_cache_retrieve_metadata(), AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, ast_variable_list_append, ast_variable_new, CATEGORY, sip_to_pjsip::info(), NULL, PATH_MAX, TEST_EXECUTE, TEST_INIT, tmp(), and VALID_RESOURCE.

◆ AST_TEST_DEFINE() [2/5]

AST_TEST_DEFINE ( create_update_nominal  )

Definition at line 195 of file test_media_cache.c.

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 /* Create two local files to associate with a resource */
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 /* We don't need anything in the file */
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);
238 ast_test_validate(test, res == 0);
239
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);
247 ast_test_validate(test, res == 0);
248
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
255
256 unlink(tmp_path_one);
257 unlink(tmp_path_two);
258
259 return AST_TEST_PASS;
260}
int ast_media_cache_delete(const char *uri)
Remove an item from the media cache.
Definition: media_cache.c:347

References ast_media_cache_create_or_update(), ast_media_cache_delete(), ast_media_cache_retrieve(), AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, CATEGORY, ast_sorcery_wizard::close, sip_to_pjsip::info(), NULL, PATH_MAX, TEST_EXECUTE, TEST_INIT, and VALID_RESOURCE.

◆ AST_TEST_DEFINE() [3/5]

AST_TEST_DEFINE ( create_update_off_nominal  )

Definition at line 262 of file test_media_cache.c.

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 /* Create two local files to associate with a resource */
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 /* We don't need anything in the file */
289 close(fd);
290
292 ast_test_validate(test, res != 0);
293
295 ast_test_validate(test, res != 0);
296
298 ast_test_validate(test, res != 0);
299
301 ast_test_validate(test, res != 0);
302
304 ast_test_validate(test, res != 0);
305
307 ast_test_validate(test, res != 0);
308
309 unlink(tmp_path);
310
311 return AST_TEST_PASS;
312}
#define INVALID_SCHEME
#define NO_SCHEME
#define INVALID_RESOURCE

References ast_media_cache_create_or_update(), AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, CATEGORY, ast_sorcery_wizard::close, sip_to_pjsip::info(), INVALID_RESOURCE, INVALID_SCHEME, NO_SCHEME, NULL, PATH_MAX, TEST_EXECUTE, TEST_INIT, and VALID_RESOURCE.

◆ AST_TEST_DEFINE() [4/5]

AST_TEST_DEFINE ( exists_nominal  )

Definition at line 135 of file test_media_cache.c.

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
155 ast_test_validate(test, res == 0);
156
158 ast_test_validate(test, res == 1);
159
160 return AST_TEST_PASS;
161}
int ast_media_cache_exists(const char *uri)
Check if an item exists in the cache.
Definition: media_cache.c:53

References ast_media_cache_exists(), AST_TEST_NOT_RUN, AST_TEST_PASS, CATEGORY, sip_to_pjsip::info(), INVALID_RESOURCE, TEST_EXECUTE, TEST_INIT, and VALID_RESOURCE.

◆ AST_TEST_DEFINE() [5/5]

AST_TEST_DEFINE ( exists_off_nominal  )

Definition at line 163 of file test_media_cache.c.

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
184 ast_test_validate(test, res != 1);
185
187 ast_test_validate(test, res != 1);
188
190 ast_test_validate(test, res != 1);
191
192 return AST_TEST_PASS;
193}

References ast_media_cache_exists(), AST_TEST_NOT_RUN, AST_TEST_PASS, CATEGORY, sip_to_pjsip::info(), INVALID_SCHEME, NO_SCHEME, NULL, TEST_EXECUTE, and TEST_INIT.

◆ bucket_http_test_wizard_create()

static int bucket_http_test_wizard_create ( const struct ast_sorcery sorcery,
void *  data,
void *  object 
)
static

Definition at line 60 of file test_media_cache.c.

62{
63 if (!strcmp(ast_sorcery_object_get_id(object), VALID_RESOURCE)) {
64 return 0;
65 }
66
67 return -1;
68}
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2317

References ast_sorcery_object_get_id(), and VALID_RESOURCE.

◆ bucket_http_test_wizard_delete()

static int bucket_http_test_wizard_delete ( const struct ast_sorcery sorcery,
void *  data,
void *  object 
)
static

Definition at line 109 of file test_media_cache.c.

111{
113 if (!strcmp(ast_sorcery_object_get_id(object), VALID_RESOURCE)) {
114 return 0;
115 }
116
117 return -1;
118}

References ast_media_cache_delete(), ast_sorcery_object_get_id(), and VALID_RESOURCE.

◆ bucket_http_test_wizard_retrieve_id()

static void * bucket_http_test_wizard_retrieve_id ( const struct ast_sorcery sorcery,
void *  data,
const char *  type,
const char *  id 
)
static

Definition at line 88 of file test_media_cache.c.

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
100 return bucket_file;
101 }
102 return NULL;
103}
struct ast_bucket_file * ast_bucket_file_alloc(const char *uri)
Allocate a new bucket file.
Definition: bucket.c:663
int ast_bucket_file_temporary_create(struct ast_bucket_file *file)
Common file snapshot creation callback for creating a temporary file.
Definition: bucket.c:899
static const char type[]
Definition: chan_ooh323.c:109
Bucket file structure, contains reference to file and information about it.
Definition: bucket.h:78

References ast_bucket_file_alloc(), ast_bucket_file_temporary_create(), NULL, type, and VALID_RESOURCE.

◆ bucket_http_test_wizard_update()

static int bucket_http_test_wizard_update ( const struct ast_sorcery sorcery,
void *  data,
void *  object 
)
static

Definition at line 74 of file test_media_cache.c.

76{
77 if (!strcmp(ast_sorcery_object_get_id(object), VALID_RESOURCE)) {
78 return 0;
79 }
80
81 return -1;
82}

References ast_sorcery_object_get_id(), and VALID_RESOURCE.

◆ load_module()

static int load_module ( void  )
static

Definition at line 398 of file test_media_cache.c.

399{
402 ast_log(LOG_ERROR, "Failed to register Bucket HTTP test wizard scheme implementation\n");
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
414}
#define ast_log
Definition: astobj2.c:42
#define ast_bucket_scheme_register(name, bucket, file, create_cb, destroy_cb)
Register support for a specific scheme.
Definition: bucket.h:137
#define LOG_ERROR
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
#define AST_TEST_REGISTER(cb)
Definition: test.h:127
static struct ast_sorcery_wizard bucket_file_test_wizard
static struct ast_sorcery_wizard bucket_test_wizard

References ast_bucket_scheme_register, ast_log, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, AST_TEST_REGISTER, bucket_file_test_wizard, bucket_test_wizard, LOG_ERROR, and NULL.

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 386 of file test_media_cache.c.

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}
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128

References AST_TEST_UNREGISTER.

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Media Cache Tests" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 416 of file test_media_cache.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 416 of file test_media_cache.c.

◆ bucket_file_test_wizard

struct ast_sorcery_wizard bucket_file_test_wizard
static

Definition at line 127 of file test_media_cache.c.

Referenced by load_module().

◆ bucket_test_wizard

struct ast_sorcery_wizard bucket_test_wizard
static

Definition at line 120 of file test_media_cache.c.

Referenced by load_module().