Asterisk - The Open Source Telephony Project  18.5.0
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/monkeys.wav"
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 AST_TEST_DEFINE (exists_nominal)
 
 AST_TEST_DEFINE (exists_off_nominal)
 
 AST_TEST_DEFINE (create_update_nominal)
 
 AST_TEST_DEFINE (create_update_off_nominal)
 
 AST_TEST_DEFINE (create_update_metadata)
 
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 <[email protected]> 

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.

Referenced by AST_TEST_DEFINE().

◆ 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.

Referenced by AST_TEST_DEFINE().

◆ 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.

Referenced by AST_TEST_DEFINE().

◆ 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.

Referenced by AST_TEST_DEFINE().

◆ VALID_RESOURCE

#define VALID_RESOURCE   "httptest://localhost:8088/test_media_cache/monkeys.wav"

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

Definition at line 45 of file test_media_cache.c.

Referenced by AST_TEST_DEFINE(), bucket_http_test_wizard_create(), bucket_http_test_wizard_delete(), bucket_http_test_wizard_retrieve_id(), and bucket_http_test_wizard_update().

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 413 of file test_media_cache.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 413 of file test_media_cache.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 413 of file test_media_cache.c.

◆ AST_TEST_DEFINE() [1/5]

AST_TEST_DEFINE ( exists_nominal  )

Definition at line 134 of file test_media_cache.c.

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.

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 }
#define VALID_RESOURCE
#define CATEGORY
#define INVALID_RESOURCE
def info(msg)
int ast_media_cache_exists(const char *uri)
Check if an item exists in the cache.
Definition: media_cache.c:53

◆ AST_TEST_DEFINE() [2/5]

AST_TEST_DEFINE ( exists_off_nominal  )

Definition at line 162 of file test_media_cache.c.

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.

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 }
#define NO_SCHEME
#define NULL
Definition: resample.c:96
#define CATEGORY
def info(msg)
#define INVALID_SCHEME
int ast_media_cache_exists(const char *uri)
Check if an item exists in the cache.
Definition: media_cache.c:53

◆ AST_TEST_DEFINE() [3/5]

AST_TEST_DEFINE ( create_update_nominal  )

Definition at line 194 of file test_media_cache.c.

References ast_media_cache_create_or_update(), 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.

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 }
#define VALID_RESOURCE
#define NULL
Definition: resample.c:96
#define CATEGORY
#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
def info(msg)
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 PATH_MAX
Definition: asterisk.h:40

◆ AST_TEST_DEFINE() [4/5]

AST_TEST_DEFINE ( create_update_off_nominal  )

Definition at line 259 of file test_media_cache.c.

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.

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 }
#define VALID_RESOURCE
#define NO_SCHEME
#define NULL
Definition: resample.c:96
#define CATEGORY
#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
#define INVALID_RESOURCE
def info(msg)
#define INVALID_SCHEME
#define PATH_MAX
Definition: asterisk.h:40

◆ AST_TEST_DEFINE() [5/5]

AST_TEST_DEFINE ( create_update_metadata  )

Definition at line 311 of file test_media_cache.c.

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.

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 }
#define VALID_RESOURCE
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
#define NULL
Definition: resample.c:96
#define CATEGORY
#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
#define ast_variable_new(name, value, filename)
def info(msg)
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 PATH_MAX
Definition: asterisk.h:40
#define ast_variable_list_append(head, new_var)

◆ 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.

References ast_sorcery_object_get_id(), and VALID_RESOURCE.

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

◆ 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.

References ast_sorcery_object_get_id(), and VALID_RESOURCE.

111 {
112  if (!strcmp(ast_sorcery_object_get_id(object), VALID_RESOURCE)) {
113  return 0;
114  }
115 
116  return -1;
117 }
#define VALID_RESOURCE
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2312

◆ 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.

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

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 }
static const char type[]
Definition: chan_ooh323.c:109
#define VALID_RESOURCE
#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
Bucket file structure, contains reference to file and information about it.
Definition: bucket.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

◆ 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.

References ast_sorcery_object_get_id(), and VALID_RESOURCE.

76 {
77  if (!strcmp(ast_sorcery_object_get_id(object), VALID_RESOURCE)) {
78  return 0;
79  }
80 
81  return -1;
82 }
#define VALID_RESOURCE
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2312

◆ load_module()

static int load_module ( void  )
static

Definition at line 395 of file test_media_cache.c.

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

396 {
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 }
#define AST_TEST_REGISTER(cb)
Definition: test.h:127
static struct ast_sorcery_wizard bucket_file_test_wizard
#define NULL
Definition: resample.c:96
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285
static struct ast_sorcery_wizard bucket_test_wizard
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
#define ast_bucket_scheme_register(name, bucket, file, create_cb, destroy_cb)
Register support for a specific scheme.
Definition: bucket.h:137

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 383 of file test_media_cache.c.

References AST_TEST_UNREGISTER.

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

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 413 of file test_media_cache.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 413 of file test_media_cache.c.

◆ bucket_file_test_wizard

struct ast_sorcery_wizard bucket_file_test_wizard
static

Definition at line 126 of file test_media_cache.c.

◆ bucket_test_wizard

struct ast_sorcery_wizard bucket_test_wizard
static

Definition at line 119 of file test_media_cache.c.