Asterisk - The Open Source Telephony Project  18.5.0
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 <[email protected]>
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 <[email protected]> \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"
39 #include "asterisk/media_cache.h"
40 
41 /*! The unit test category */
42 #define CATEGORY "/main/media_cache/"
43 
44 /*! A 'valid' resource for the test bucket behind the media cache facade */
45 #define VALID_RESOURCE "httptest://localhost:8088/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  */
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  * \internal
72  * \brief Update callback for the httptest bucket backend
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  * \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  */
109 static int bucket_http_test_wizard_delete(const struct ast_sorcery *sorcery, void *data,
110  void *object)
111 {
112  if (!strcmp(ast_sorcery_object_get_id(object), VALID_RESOURCE)) {
113  return 0;
114  }
115 
116  return -1;
117 }
118 
120  .name = "httptest",
124 };
125 
127  .name = "httptest",
132 };
133 
134 AST_TEST_DEFINE(exists_nominal)
135 {
136  int res;
137 
138  switch (cmd) {
139  case TEST_INIT:
140  info->name = __func__;
141  info->category = CATEGORY;
142  info->summary = "Test nominal existance of resources in the cache";
143  info->description =
144  "This test verifies that if a known resource is in the cache, "
145  "calling ast_media_cache_exists will return logical True. If "
146  "a resource does not exist, the same function call will return "
147  "logical False.";
148  return AST_TEST_NOT_RUN;
149  case TEST_EXECUTE:
150  break;
151  }
152 
154  ast_test_validate(test, res == 0);
155 
157  ast_test_validate(test, res == 1);
158 
159  return AST_TEST_PASS;
160 }
161 
162 AST_TEST_DEFINE(exists_off_nominal)
163 {
164  int res;
165 
166  switch (cmd) {
167  case TEST_INIT:
168  info->name = __func__;
169  info->category = CATEGORY;
170  info->summary = "Test off nominal existance of resources in the cache";
171  info->description =
172  "This test verifies that checking for bad resources (NULL, bad "
173  "scheme, etc.) does not result in false positivies.";
174  return AST_TEST_NOT_RUN;
175  case TEST_EXECUTE:
176  break;
177  }
178 
179  res = ast_media_cache_exists("");
180  ast_test_validate(test, res != 1);
181 
183  ast_test_validate(test, res != 1);
184 
186  ast_test_validate(test, res != 1);
187 
189  ast_test_validate(test, res != 1);
190 
191  return AST_TEST_PASS;
192 }
193 
194 AST_TEST_DEFINE(create_update_nominal)
195 {
196  int res;
197  char file_path[PATH_MAX];
198  char tmp_path_one[PATH_MAX] = "/tmp/test-media-cache-XXXXXX";
199  char tmp_path_two[PATH_MAX] = "/tmp/test-media-cache-XXXXXX";
200  int fd;
201 
202  switch (cmd) {
203  case TEST_INIT:
204  info->name = __func__;
205  info->category = CATEGORY;
206  info->summary = "Test nominal creation/updating of a resource";
207  info->description =
208  "This test creates a resource and associates it with a file. "
209  "It then updates the resource with a new file. In both cases, "
210  "the test verifies that the resource is associated with the "
211  "file.";
212  return AST_TEST_NOT_RUN;
213  case TEST_EXECUTE:
214  break;
215  }
216 
217  /* Create two local files to associate with a resource */
218  fd = mkstemp(tmp_path_one);
219  if (fd < 0) {
220  ast_test_status_update(test, "Failed to create first tmp file: %s\n",
221  tmp_path_one);
222  return AST_TEST_FAIL;
223  }
224  /* We don't need anything in the file */
225  close(fd);
226 
227  fd = mkstemp(tmp_path_two);
228  if (fd < 0) {
229  ast_test_status_update(test, "Failed to create second tmp file: %s\n",
230  tmp_path_two);
231  return AST_TEST_FAIL;
232  }
233  close(fd);
234 
235  ast_test_status_update(test, "Creating resource with %s\n", tmp_path_one);
237  ast_test_validate(test, res == 0);
238 
240  ast_test_status_update(test, "Got %s for first file path\n", file_path);
241  ast_test_validate(test, res == 0);
242  ast_test_validate(test, strcmp(file_path, tmp_path_one) == 0);
243 
244  ast_test_status_update(test, "Creating resource with %s\n", tmp_path_two);
246  ast_test_validate(test, res == 0);
247 
249  ast_test_status_update(test, "Got %s for second file path\n", file_path);
250  ast_test_validate(test, res == 0);
251  ast_test_validate(test, strcmp(file_path, tmp_path_two) == 0);
252 
253  unlink(tmp_path_one);
254  unlink(tmp_path_two);
255 
256  return AST_TEST_PASS;
257 }
258 
259 AST_TEST_DEFINE(create_update_off_nominal)
260 {
261  int res;
262  char tmp_path[PATH_MAX] = "/tmp/test-media-cache-XXXXXX";
263  int fd;
264 
265  switch (cmd) {
266  case TEST_INIT:
267  info->name = __func__;
268  info->category = CATEGORY;
269  info->summary = "Test off nominal creation/updating of a resource";
270  info->description =
271  "Test creation/updating of a resource with a variety of invalid\n"
272  "inputs.";
273  return AST_TEST_NOT_RUN;
274  case TEST_EXECUTE:
275  break;
276  }
277 
278  /* Create two local files to associate with a resource */
279  fd = mkstemp(tmp_path);
280  if (fd < 0) {
281  ast_test_status_update(test, "Failed to create first tmp file: %s\n",
282  tmp_path);
283  return AST_TEST_FAIL;
284  }
285  /* We don't need anything in the file */
286  close(fd);
287 
289  ast_test_validate(test, res != 0);
290 
292  ast_test_validate(test, res != 0);
293 
294  res = ast_media_cache_create_or_update(VALID_RESOURCE, "I don't exist", NULL);
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 
306  unlink(tmp_path);
307 
308  return AST_TEST_PASS;
309 }
310 
311 AST_TEST_DEFINE(create_update_metadata)
312 {
313  int res;
314  char tmp_path[PATH_MAX] = "/tmp/test-media-cache-XXXXXX";
315  char file_path[PATH_MAX];
316  char actual_metadata[32];
317  struct ast_variable *meta_list = NULL;
318  struct ast_variable *tmp;
319  int fd;
320 
321  switch (cmd) {
322  case TEST_INIT:
323  info->name = __func__;
324  info->category = CATEGORY;
325  info->summary = "Test nominal creation/updating of a resource";
326  info->description =
327  "This test creates a resource and associates it with a file. "
328  "It then updates the resource with a new file. In both cases, "
329  "the test verifies that the resource is associated with the "
330  "file.";
331  return AST_TEST_NOT_RUN;
332  case TEST_EXECUTE:
333  break;
334  }
335 
336  /* Create two local files to associate with a resource */
337  fd = mkstemp(tmp_path);
338  if (fd < 0) {
339  ast_test_status_update(test, "Failed to create first tmp file: %s\n",
340  tmp_path);
341  return AST_TEST_FAIL;
342  }
343  /* We don't need anything in the file */
344  close(fd);
345 
346  tmp = ast_variable_new("meta1", "value1", __FILE__);
347  if (!tmp) {
348  ast_test_status_update(test, "Failed to create metadata 1 for test\n");
349  return AST_TEST_FAIL;
350  }
351  ast_variable_list_append(&meta_list, tmp);
352 
353  tmp = ast_variable_new("meta2", "value2", __FILE__);
354  if (!tmp) {
355  ast_test_status_update(test, "Failed to create metadata 2 for test\n");
356  return AST_TEST_FAIL;
357  }
358  ast_variable_list_append(&meta_list, tmp);
359 
360  res = ast_media_cache_create_or_update(VALID_RESOURCE, tmp_path, meta_list);
361  ast_test_validate(test, res == 0);
362 
364  ast_test_status_update(test, "Got %s for second file path\n", file_path);
365  ast_test_validate(test, res == 0);
366  ast_test_validate(test, strcmp(file_path, tmp_path) == 0);
367 
369  actual_metadata, sizeof(actual_metadata));
370  ast_test_validate(test, res == 0);
371  ast_test_validate(test, strcmp(actual_metadata, "value1") == 0);
372 
374  actual_metadata, sizeof(actual_metadata));
375  ast_test_validate(test, res == 0);
376  ast_test_validate(test, strcmp(actual_metadata, "value2") == 0);
377 
378  unlink(tmp_path);
379 
380  return AST_TEST_PASS;
381 }
382 
383 static int unload_module(void)
384 {
385  AST_TEST_UNREGISTER(exists_nominal);
386  AST_TEST_UNREGISTER(exists_off_nominal);
387 
388  AST_TEST_UNREGISTER(create_update_nominal);
389  AST_TEST_UNREGISTER(create_update_metadata);
390  AST_TEST_UNREGISTER(create_update_off_nominal);
391 
392  return 0;
393 }
394 
395 static int load_module(void)
396 {
397  if (ast_bucket_scheme_register("httptest", &bucket_test_wizard,
398  &bucket_file_test_wizard, NULL, NULL)) {
399  ast_log(LOG_ERROR, "Failed to register Bucket HTTP test wizard scheme implementation\n");
401  }
402 
403  AST_TEST_REGISTER(exists_nominal);
404  AST_TEST_REGISTER(exists_off_nominal);
405 
406  AST_TEST_REGISTER(create_update_nominal);
407  AST_TEST_REGISTER(create_update_metadata);
408  AST_TEST_REGISTER(create_update_off_nominal);
409 
411 }
412 
static const char type[]
Definition: chan_ooh323.c:109
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:567
Asterisk main include file. File version handling, generic pbx functions.
#define VALID_RESOURCE
#define NO_SCHEME
static int tmp()
Definition: bt_open.c:389
Structure for variables, used for configurations and for channel variables.
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:258
Test Framework API.
#define AST_TEST_REGISTER(cb)
Definition: test.h:127
Full structure for sorcery.
Definition: sorcery.c:230
static struct ast_sorcery_wizard bucket_file_test_wizard
static int unload_module(void)
#define NULL
Definition: resample.c:96
struct ast_bucket_file * ast_bucket_file_alloc(const char *uri)
Allocate a new bucket file.
Definition: bucket.c:663
const char * name
Name of the wizard.
Definition: sorcery.h:278
Bucket File API.
AST_TEST_DEFINE(exists_nominal)
Utility functions.
void(* close)(void *data)
Callback for closing a wizard.
Definition: sorcery.h:322
#define ast_log
Definition: astobj2.c:42
#define CATEGORY
static int load_module(void)
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
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:285
static int bucket_http_test_wizard_delete(const struct ast_sorcery *sorcery, void *data, void *object)
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2312
Bucket file structure, contains reference to file and information about it.
Definition: bucket.h:78
#define ast_variable_new(name, value, filename)
#define INVALID_RESOURCE
#define LOG_ERROR
Definition: logger.h:285
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128
def info(msg)
An in-memory media cache.
static struct ast_sorcery_wizard bucket_test_wizard
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
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 int bucket_http_test_wizard_create(const struct ast_sorcery *sorcery, void *data, void *object)
#define INVALID_SCHEME
Interface for a sorcery wizard.
Definition: sorcery.h:276
static struct ast_sorcery * sorcery
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:197
#define ast_bucket_scheme_register(name, bucket, file, create_cb, destroy_cb)
Register support for a specific scheme.
Definition: bucket.h:137
#define PATH_MAX
Definition: asterisk.h:40
static void * bucket_http_test_wizard_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
#define ast_variable_list_append(head, new_var)
static int bucket_http_test_wizard_update(const struct ast_sorcery *sorcery, void *data, void *object)
int ast_media_cache_exists(const char *uri)
Check if an item exists in the cache.
Definition: media_cache.c:53