Debian Codegraph @

tests/test_media_cache.c

raw symbols SCIP
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' 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 */
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 */
88 static void *bucket_http_test_wizard_retrieve_id(const struct ast_sorcery *sorcery,
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
99 ast_bucket_file_temporary_create(bucket_file);
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 ast_media_cache_delete(VALID_RESOURCE);
113 if (!strcmp(ast_sorcery_object_get_id(object), VALID_RESOURCE)) {
114 return 0;
115 }
116
117 return -1;
118 }
119
120 static struct ast_sorcery_wizard bucket_test_wizard = {
121 .name = "httptest",
122 .create = bucket_http_test_wizard_create,
123 .retrieve_id = bucket_http_test_wizard_retrieve_id,
124 .delete = bucket_http_test_wizard_delete,
125 };
126
127 static struct ast_sorcery_wizard bucket_file_test_wizard = {
128 .name = "httptest",
129 .create = bucket_http_test_wizard_create,
130 .update = bucket_http_test_wizard_update,
131 .retrieve_id = bucket_http_test_wizard_retrieve_id,
132 .delete = bucket_http_test_wizard_delete,
133 };
134
135 AST_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
154 res = ast_media_cache_exists(INVALID_RESOURCE);
155 ast_test_validate(test, res == 0);
156
157 res = ast_media_cache_exists(VALID_RESOURCE);
158 ast_test_validate(test, res == 1);
159
160 return AST_TEST_PASS;
161 }
162
163 AST_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
183 res = ast_media_cache_exists(NULL);
184 ast_test_validate(test, res != 1);
185
186 res = ast_media_cache_exists(NO_SCHEME);
187 ast_test_validate(test, res != 1);
188
189 res = ast_media_cache_exists(INVALID_SCHEME);
190 ast_test_validate(test, res != 1);
191
192 return AST_TEST_PASS;
193 }
194
195 AST_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);
237 res = ast_media_cache_create_or_update(VALID_RESOURCE, tmp_path_one, NULL);
238 ast_test_validate(test, res == 0);
239
240 res = ast_media_cache_retrieve(VALID_RESOURCE, NULL, file_path, PATH_MAX);
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);
246 res = ast_media_cache_create_or_update(VALID_RESOURCE, tmp_path_two, NULL);
247 ast_test_validate(test, res == 0);
248
249 res = ast_media_cache_retrieve(VALID_RESOURCE, NULL, file_path, PATH_MAX);
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
254 ast_media_cache_delete(VALID_RESOURCE);
255
256 unlink(tmp_path_one);
257 unlink(tmp_path_two);
258
259 return AST_TEST_PASS;
260 }
261
262 AST_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
291 res = ast_media_cache_create_or_update(VALID_RESOURCE, NULL, NULL);
292 ast_test_validate(test, res != 0);
293
294 res = ast_media_cache_create_or_update(VALID_RESOURCE, "", NULL);
295 ast_test_validate(test, res != 0);
296
297 res = ast_media_cache_create_or_update(VALID_RESOURCE, "I don't exist", NULL);
298 ast_test_validate(test, res != 0);
299
300 res = ast_media_cache_create_or_update(INVALID_RESOURCE, tmp_path, NULL);
301 ast_test_validate(test, res != 0);
302
303 res = ast_media_cache_create_or_update(INVALID_SCHEME, tmp_path, NULL);
304 ast_test_validate(test, res != 0);
305
306 res = ast_media_cache_create_or_update(NO_SCHEME, tmp_path, NULL);
307 ast_test_validate(test, res != 0);
308
309 unlink(tmp_path);
310
311 return AST_TEST_PASS;
312 }
313
314 AST_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
366 res = ast_media_cache_retrieve(VALID_RESOURCE, NULL, file_path, PATH_MAX);
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
371 res = ast_media_cache_retrieve_metadata(VALID_RESOURCE, "meta1",
372 actual_metadata, sizeof(actual_metadata));
373 ast_test_validate(test, res == 0);
374 ast_test_validate(test, strcmp(actual_metadata, "value1") == 0);
375
376 res = ast_media_cache_retrieve_metadata(VALID_RESOURCE, "meta2",
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
386 static 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
398 static int load_module(void)
399 {
400 if (ast_bucket_scheme_register("httptest", &bucket_test_wizard,
401 &bucket_file_test_wizard, NULL, NULL)) {
402 ast_log(LOG_ERROR, "Failed to register Bucket HTTP test wizard scheme implementation\n");
403 return AST_MODULE_LOAD_DECLINE;
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
413 return AST_MODULE_LOAD_SUCCESS;
414 }
415
416 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Media Cache Tests");