test_media_cache.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. * \file
  20. * \brief Tests for the media cache API
  21. *
  22. * \author \verbatim Matt Jordan <mjordan@digium.com> \endverbatim
  23. *
  24. * \ingroup tests
  25. */
  26. /*** MODULEINFO
  27. <depend>TEST_FRAMEWORK</depend>
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. #include "asterisk/utils.h"
  32. #include "asterisk/module.h"
  33. #include "asterisk/test.h"
  34. #include "asterisk/bucket.h"
  35. #include "asterisk/media_cache.h"
  36. /*! The unit test category */
  37. #define CATEGORY "/main/media_cache/"
  38. /*! A 'valid' long resource for the test bucket behind the media cache facade */
  39. #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"
  40. /*! An 'invalid' resource for the test bucket behind the media cache facade */
  41. #define INVALID_RESOURCE "httptest://localhost:8088/test_media_cache/bad.wav"
  42. /*! An 'invalid' scheme, not mapping to a valid bucket backend */
  43. #define INVALID_SCHEME "foo://localhost:8088/test_media_cache/monkeys.wav"
  44. /*! A URI with no scheme */
  45. #define NO_SCHEME "localhost:8088/test_media_cache/monkeys.wav"
  46. /*!
  47. * \internal
  48. * \brief Create callback for the httptest bucket backend
  49. */
  50. static int bucket_http_test_wizard_create(const struct ast_sorcery *sorcery, void *data,
  51. void *object)
  52. {
  53. if (!strcmp(ast_sorcery_object_get_id(object), VALID_RESOURCE)) {
  54. return 0;
  55. }
  56. return -1;
  57. }
  58. /*!
  59. * \internal
  60. * \brief Update callback for the httptest bucket backend
  61. */
  62. static int bucket_http_test_wizard_update(const struct ast_sorcery *sorcery, void *data,
  63. void *object)
  64. {
  65. if (!strcmp(ast_sorcery_object_get_id(object), VALID_RESOURCE)) {
  66. return 0;
  67. }
  68. return -1;
  69. }
  70. /*!
  71. * \internal
  72. * \brief Retrieve callback for the httptest bucket backend
  73. */
  74. static void *bucket_http_test_wizard_retrieve_id(const struct ast_sorcery *sorcery,
  75. void *data, const char *type, const char *id)
  76. {
  77. struct ast_bucket_file *bucket_file;
  78. if (!strcmp(type, "file") && !strcmp(id, VALID_RESOURCE)) {
  79. bucket_file = ast_bucket_file_alloc(id);
  80. if (!bucket_file) {
  81. return NULL;
  82. }
  83. ast_bucket_file_temporary_create(bucket_file);
  84. return bucket_file;
  85. }
  86. return NULL;
  87. }
  88. /*!
  89. * \internal
  90. * \brief Delete callback for the httptest bucket backend
  91. */
  92. static int bucket_http_test_wizard_delete(const struct ast_sorcery *sorcery, void *data,
  93. void *object)
  94. {
  95. ast_media_cache_delete(VALID_RESOURCE);
  96. if (!strcmp(ast_sorcery_object_get_id(object), VALID_RESOURCE)) {
  97. return 0;
  98. }
  99. return -1;
  100. }
  101. static struct ast_sorcery_wizard bucket_test_wizard = {
  102. .name = "httptest",
  103. .create = bucket_http_test_wizard_create,
  104. .retrieve_id = bucket_http_test_wizard_retrieve_id,
  105. .delete = bucket_http_test_wizard_delete,
  106. };
  107. static struct ast_sorcery_wizard bucket_file_test_wizard = {
  108. .name = "httptest",
  109. .create = bucket_http_test_wizard_create,
  110. .update = bucket_http_test_wizard_update,
  111. .retrieve_id = bucket_http_test_wizard_retrieve_id,
  112. .delete = bucket_http_test_wizard_delete,
  113. };
  114. AST_TEST_DEFINE(exists_nominal)
  115. {
  116. int res;
  117. switch (cmd) {
  118. case TEST_INIT:
  119. info->name = __func__;
  120. info->category = CATEGORY;
  121. info->summary = "Test nominal existance of resources in the cache";
  122. info->description =
  123. "This test verifies that if a known resource is in the cache, "
  124. "calling ast_media_cache_exists will return logical True. If "
  125. "a resource does not exist, the same function call will return "
  126. "logical False.";
  127. return AST_TEST_NOT_RUN;
  128. case TEST_EXECUTE:
  129. break;
  130. }
  131. res = ast_media_cache_exists(INVALID_RESOURCE);
  132. ast_test_validate(test, res == 0);
  133. res = ast_media_cache_exists(VALID_RESOURCE);
  134. ast_test_validate(test, res == 1);
  135. return AST_TEST_PASS;
  136. }
  137. AST_TEST_DEFINE(exists_off_nominal)
  138. {
  139. int res;
  140. switch (cmd) {
  141. case TEST_INIT:
  142. info->name = __func__;
  143. info->category = CATEGORY;
  144. info->summary = "Test off nominal existance of resources in the cache";
  145. info->description =
  146. "This test verifies that checking for bad resources (NULL, bad "
  147. "scheme, etc.) does not result in false positives.";
  148. return AST_TEST_NOT_RUN;
  149. case TEST_EXECUTE:
  150. break;
  151. }
  152. res = ast_media_cache_exists("");
  153. ast_test_validate(test, res != 1);
  154. res = ast_media_cache_exists(NULL);
  155. ast_test_validate(test, res != 1);
  156. res = ast_media_cache_exists(NO_SCHEME);
  157. ast_test_validate(test, res != 1);
  158. res = ast_media_cache_exists(INVALID_SCHEME);
  159. ast_test_validate(test, res != 1);
  160. return AST_TEST_PASS;
  161. }
  162. AST_TEST_DEFINE(create_update_nominal)
  163. {
  164. int res;
  165. char file_path[PATH_MAX];
  166. char tmp_path_one[PATH_MAX] = "/tmp/test-media-cache-XXXXXX";
  167. char tmp_path_two[PATH_MAX] = "/tmp/test-media-cache-XXXXXX";
  168. int fd;
  169. switch (cmd) {
  170. case TEST_INIT:
  171. info->name = __func__;
  172. info->category = CATEGORY;
  173. info->summary = "Test nominal creation/updating of a resource";
  174. info->description =
  175. "This test creates a resource and associates it with a file. "
  176. "It then updates the resource with a new file. In both cases, "
  177. "the test verifies that the resource is associated with the "
  178. "file.";
  179. return AST_TEST_NOT_RUN;
  180. case TEST_EXECUTE:
  181. break;
  182. }
  183. /* Create two local files to associate with a resource */
  184. fd = mkstemp(tmp_path_one);
  185. if (fd < 0) {
  186. ast_test_status_update(test, "Failed to create first tmp file: %s\n",
  187. tmp_path_one);
  188. return AST_TEST_FAIL;
  189. }
  190. /* We don't need anything in the file */
  191. close(fd);
  192. fd = mkstemp(tmp_path_two);
  193. if (fd < 0) {
  194. ast_test_status_update(test, "Failed to create second tmp file: %s\n",
  195. tmp_path_two);
  196. return AST_TEST_FAIL;
  197. }
  198. close(fd);
  199. ast_test_status_update(test, "Creating resource with %s\n", tmp_path_one);
  200. res = ast_media_cache_create_or_update(VALID_RESOURCE, tmp_path_one, NULL);
  201. ast_test_validate(test, res == 0);
  202. res = ast_media_cache_retrieve(VALID_RESOURCE, NULL, file_path, PATH_MAX);
  203. ast_test_status_update(test, "Got %s for first file path\n", file_path);
  204. ast_test_validate(test, res == 0);
  205. ast_test_validate(test, strcmp(file_path, tmp_path_one) == 0);
  206. ast_test_status_update(test, "Creating resource with %s\n", tmp_path_two);
  207. res = ast_media_cache_create_or_update(VALID_RESOURCE, tmp_path_two, NULL);
  208. ast_test_validate(test, res == 0);
  209. res = ast_media_cache_retrieve(VALID_RESOURCE, NULL, file_path, PATH_MAX);
  210. ast_test_status_update(test, "Got %s for second file path\n", file_path);
  211. ast_test_validate(test, res == 0);
  212. ast_test_validate(test, strcmp(file_path, tmp_path_two) == 0);
  213. ast_media_cache_delete(VALID_RESOURCE);
  214. unlink(tmp_path_one);
  215. unlink(tmp_path_two);
  216. return AST_TEST_PASS;
  217. }
  218. AST_TEST_DEFINE(create_update_off_nominal)
  219. {
  220. int res;
  221. char tmp_path[PATH_MAX] = "/tmp/test-media-cache-XXXXXX";
  222. int fd;
  223. switch (cmd) {
  224. case TEST_INIT:
  225. info->name = __func__;
  226. info->category = CATEGORY;
  227. info->summary = "Test off nominal creation/updating of a resource";
  228. info->description =
  229. "Test creation/updating of a resource with a variety of invalid\n"
  230. "inputs.";
  231. return AST_TEST_NOT_RUN;
  232. case TEST_EXECUTE:
  233. break;
  234. }
  235. /* Create two local files to associate with a resource */
  236. fd = mkstemp(tmp_path);
  237. if (fd < 0) {
  238. ast_test_status_update(test, "Failed to create first tmp file: %s\n",
  239. tmp_path);
  240. return AST_TEST_FAIL;
  241. }
  242. /* We don't need anything in the file */
  243. close(fd);
  244. res = ast_media_cache_create_or_update(VALID_RESOURCE, NULL, NULL);
  245. ast_test_validate(test, res != 0);
  246. res = ast_media_cache_create_or_update(VALID_RESOURCE, "", NULL);
  247. ast_test_validate(test, res != 0);
  248. res = ast_media_cache_create_or_update(VALID_RESOURCE, "I don't exist", NULL);
  249. ast_test_validate(test, res != 0);
  250. res = ast_media_cache_create_or_update(INVALID_RESOURCE, tmp_path, NULL);
  251. ast_test_validate(test, res != 0);
  252. res = ast_media_cache_create_or_update(INVALID_SCHEME, tmp_path, NULL);
  253. ast_test_validate(test, res != 0);
  254. res = ast_media_cache_create_or_update(NO_SCHEME, tmp_path, NULL);
  255. ast_test_validate(test, res != 0);
  256. unlink(tmp_path);
  257. return AST_TEST_PASS;
  258. }
  259. AST_TEST_DEFINE(create_update_metadata)
  260. {
  261. int res;
  262. char tmp_path[PATH_MAX] = "/tmp/test-media-cache-XXXXXX";
  263. char file_path[PATH_MAX];
  264. char actual_metadata[32];
  265. struct ast_variable *meta_list = NULL;
  266. struct ast_variable *tmp;
  267. int fd;
  268. switch (cmd) {
  269. case TEST_INIT:
  270. info->name = __func__;
  271. info->category = CATEGORY;
  272. info->summary = "Test nominal creation/updating of a resource";
  273. info->description =
  274. "This test creates a resource and associates it with a file. "
  275. "It then updates the resource with a new file. In both cases, "
  276. "the test verifies that the resource is associated with the "
  277. "file.";
  278. return AST_TEST_NOT_RUN;
  279. case TEST_EXECUTE:
  280. break;
  281. }
  282. /* Create two local files to associate with a resource */
  283. fd = mkstemp(tmp_path);
  284. if (fd < 0) {
  285. ast_test_status_update(test, "Failed to create first tmp file: %s\n",
  286. tmp_path);
  287. return AST_TEST_FAIL;
  288. }
  289. /* We don't need anything in the file */
  290. close(fd);
  291. tmp = ast_variable_new("meta1", "value1", __FILE__);
  292. if (!tmp) {
  293. ast_test_status_update(test, "Failed to create metadata 1 for test\n");
  294. return AST_TEST_FAIL;
  295. }
  296. ast_variable_list_append(&meta_list, tmp);
  297. tmp = ast_variable_new("meta2", "value2", __FILE__);
  298. if (!tmp) {
  299. ast_test_status_update(test, "Failed to create metadata 2 for test\n");
  300. return AST_TEST_FAIL;
  301. }
  302. ast_variable_list_append(&meta_list, tmp);
  303. res = ast_media_cache_create_or_update(VALID_RESOURCE, tmp_path, meta_list);
  304. ast_test_validate(test, res == 0);
  305. res = ast_media_cache_retrieve(VALID_RESOURCE, NULL, file_path, PATH_MAX);
  306. ast_test_status_update(test, "Got %s for second file path\n", file_path);
  307. ast_test_validate(test, res == 0);
  308. ast_test_validate(test, strcmp(file_path, tmp_path) == 0);
  309. res = ast_media_cache_retrieve_metadata(VALID_RESOURCE, "meta1",
  310. actual_metadata, sizeof(actual_metadata));
  311. ast_test_validate(test, res == 0);
  312. ast_test_validate(test, strcmp(actual_metadata, "value1") == 0);
  313. res = ast_media_cache_retrieve_metadata(VALID_RESOURCE, "meta2",
  314. actual_metadata, sizeof(actual_metadata));
  315. ast_test_validate(test, res == 0);
  316. ast_test_validate(test, strcmp(actual_metadata, "value2") == 0);
  317. unlink(tmp_path);
  318. return AST_TEST_PASS;
  319. }
  320. static int unload_module(void)
  321. {
  322. AST_TEST_UNREGISTER(exists_nominal);
  323. AST_TEST_UNREGISTER(exists_off_nominal);
  324. AST_TEST_UNREGISTER(create_update_nominal);
  325. AST_TEST_UNREGISTER(create_update_metadata);
  326. AST_TEST_UNREGISTER(create_update_off_nominal);
  327. return 0;
  328. }
  329. static int load_module(void)
  330. {
  331. if (ast_bucket_scheme_register("httptest", &bucket_test_wizard,
  332. &bucket_file_test_wizard, NULL, NULL)) {
  333. ast_log(LOG_ERROR, "Failed to register Bucket HTTP test wizard scheme implementation\n");
  334. return AST_MODULE_LOAD_DECLINE;
  335. }
  336. AST_TEST_REGISTER(exists_nominal);
  337. AST_TEST_REGISTER(exists_off_nominal);
  338. AST_TEST_REGISTER(create_update_nominal);
  339. AST_TEST_REGISTER(create_update_metadata);
  340. AST_TEST_REGISTER(create_update_off_nominal);
  341. return AST_MODULE_LOAD_SUCCESS;
  342. }
  343. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Media Cache Tests");