Asterisk - The Open Source Telephony Project GIT-master-f36a736
test_media_cache.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2015, Matt Jordan
5 *
6 * Matt Jordan <mjordan@digium.com>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19/*!
20 * \file
21 * \brief Tests for the media cache API
22 *
23 * \author \verbatim Matt Jordan <mjordan@digium.com> \endverbatim
24 *
25 * \ingroup tests
26 */
27
28/*** MODULEINFO
29 <depend>TEST_FRAMEWORK</depend>
30 <support_level>core</support_level>
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"
40
41/*! The unit test category */
42#define CATEGORY "/main/media_cache/"
43
44/*! A 'valid' long resource for the test bucket behind the media cache facade */
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/*! An 'invalid' resource for the test bucket behind the media cache facade */
48#define INVALID_RESOURCE "httptest://localhost:8088/test_media_cache/bad.wav"
49
50/*! An 'invalid' scheme, not mapping to a valid bucket backend */
51#define INVALID_SCHEME "foo://localhost:8088/test_media_cache/monkeys.wav"
52
53/*! A URI with no scheme */
54#define NO_SCHEME "localhost:8088/test_media_cache/monkeys.wav"
55
56/*!
57 * \internal
58 * \brief Create callback for the httptest bucket backend
59 */
60static 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 * \internal
72 * \brief Update callback for the httptest bucket backend
73 */
74static 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 * \internal
86 * \brief Retrieve callback for the httptest bucket backend
87 */
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
100 return bucket_file;
101 }
102 return NULL;
103}
104
105/*!
106 * \internal
107 * \brief Delete callback for the httptest bucket backend
108 */
109static int bucket_http_test_wizard_delete(const struct ast_sorcery *sorcery, void *data,
110 void *object)
111{
113 if (!strcmp(ast_sorcery_object_get_id(object), VALID_RESOURCE)) {
114 return 0;
115 }
116
117 return -1;
118}
119
121 .name = "httptest",
125};
126
128 .name = "httptest",
133};
134
135AST_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
155 ast_test_validate(test, res == 0);
156
158 ast_test_validate(test, res == 1);
159
160 return AST_TEST_PASS;
161}
162
163AST_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
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}
194
195AST_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 /* 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}
261
262AST_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 /* 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}
313
314AST_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 /* 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}
385
386static 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
398static int load_module(void)
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}
415
Asterisk main include file. File version handling, generic pbx functions.
#define PATH_MAX
Definition: asterisk.h:40
#define ast_log
Definition: astobj2.c:42
static int tmp()
Definition: bt_open.c:389
Bucket File API.
struct ast_bucket_file * ast_bucket_file_alloc(const char *uri)
Allocate a new bucket file.
Definition: bucket.c:663
#define ast_bucket_scheme_register(name, bucket, file, create_cb, destroy_cb)
Register support for a specific scheme.
Definition: bucket.h:137
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
#define ast_variable_new(name, value, filename)
#define ast_variable_list_append(head, new_var)
#define LOG_ERROR
An in-memory media cache.
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_delete(const char *uri)
Remove an item from the media cache.
Definition: media_cache.c:347
int ast_media_cache_exists(const char *uri)
Check if an item exists in the cache.
Definition: media_cache.c:53
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
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ 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
def info(msg)
static struct ast_sorcery * sorcery
#define NULL
Definition: resample.c:96
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2317
Bucket file structure, contains reference to file and information about it.
Definition: bucket.h:78
Interface for a sorcery wizard.
Definition: sorcery.h:276
void(* close)(void *data)
Callback for closing a wizard.
Definition: sorcery.h:322
const char * name
Name of the wizard.
Definition: sorcery.h:278
Full structure for sorcery.
Definition: sorcery.c:230
Structure for variables, used for configurations and for channel variables.
Test Framework API.
@ TEST_INIT
Definition: test.h:200
@ TEST_EXECUTE
Definition: test.h:201
#define AST_TEST_REGISTER(cb)
Definition: test.h:127
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128
@ AST_TEST_PASS
Definition: test.h:195
@ AST_TEST_FAIL
Definition: test.h:196
@ AST_TEST_NOT_RUN
Definition: test.h:194
static struct ast_sorcery_wizard bucket_file_test_wizard
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_create(const struct ast_sorcery *sorcery, void *data, void *object)
#define INVALID_SCHEME
AST_TEST_DEFINE(exists_nominal)
#define NO_SCHEME
#define VALID_RESOURCE
#define CATEGORY
#define INVALID_RESOURCE
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)
static int bucket_http_test_wizard_delete(const struct ast_sorcery *sorcery, void *data, void *object)
static struct ast_sorcery_wizard bucket_test_wizard
Utility functions.