test_format_api.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2010, Digium, Inc.
  5. *
  6. * David Vossel <dvossel@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 ast_event API
  21. *
  22. * \author David Vossel <dvossel@digium.com>
  23. *
  24. * \ingroup tests
  25. *
  26. */
  27. /*** MODULEINFO
  28. <depend>TEST_FRAMEWORK</depend>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include "asterisk/module.h"
  33. #include "asterisk/test.h"
  34. #include "asterisk/format.h"
  35. #include "asterisk/format_cap.h"
  36. #include "asterisk/strings.h"
  37. /*! These are the keys for accessing attributes */
  38. enum test_attr_keys {
  39. TEST_ATTR_KEY_SAMP_RATE,
  40. TEST_ATTR_KEY_STRING,
  41. };
  42. /*! These are the values for the TEST_ATTR_KEY_SAMP_RATE key */
  43. enum test_attr_vals_samp {
  44. TEST_ATTR_VAL_SAMP_8KHZ = (1 << 0),
  45. TEST_ATTR_VAL_SAMP_12KHZ = (1 << 1),
  46. TEST_ATTR_VAL_SAMP_16KHZ = (1 << 2),
  47. TEST_ATTR_VAL_SAMP_32KHZ = (1 << 3),
  48. TEST_ATTR_VAL_SAMP_48KHZ = (1 << 4),
  49. };
  50. /*! This is the attribute structure used for our test interface. */
  51. struct test_attr {
  52. enum test_attr_vals_samp samp_flags;
  53. char string[32];
  54. };
  55. static enum ast_format_cmp_res test_cmp(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2)
  56. {
  57. struct test_attr *attr1 = (struct test_attr *) fattr1;
  58. struct test_attr *attr2 = (struct test_attr *) fattr2;
  59. if ((attr1->samp_flags == attr2->samp_flags) &&
  60. !(strcmp(attr1->string, attr2->string))) {
  61. return AST_FORMAT_CMP_EQUAL;
  62. }
  63. if ((attr1->samp_flags != (attr1->samp_flags & attr2->samp_flags)) ||
  64. (!ast_strlen_zero(attr1->string) && strcmp(attr1->string, attr2->string))) {
  65. return AST_FORMAT_CMP_NOT_EQUAL;
  66. }
  67. return AST_FORMAT_CMP_SUBSET;
  68. }
  69. static int test_getjoint(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2, struct ast_format_attr *result)
  70. {
  71. struct test_attr *attr1 = (struct test_attr *) fattr1;
  72. struct test_attr *attr2 = (struct test_attr *) fattr2;
  73. struct test_attr *attr_res = (struct test_attr *) result;
  74. int joint = -1;
  75. attr_res->samp_flags = (attr1->samp_flags & attr2->samp_flags);
  76. if (attr_res->samp_flags) {
  77. joint = 0;
  78. }
  79. if (!strcmp(attr1->string, attr2->string)) {
  80. ast_copy_string(attr_res->string, attr1->string, sizeof(attr_res->string));
  81. joint = 0;
  82. }
  83. return joint;
  84. }
  85. static void test_set(struct ast_format_attr *fattr, va_list ap)
  86. {
  87. enum test_attr_keys key;
  88. struct test_attr *attr = (struct test_attr *) fattr;
  89. char *string;
  90. for (key = va_arg(ap, int);
  91. key != AST_FORMAT_ATTR_END;
  92. key = va_arg(ap, int))
  93. {
  94. switch (key) {
  95. case TEST_ATTR_KEY_SAMP_RATE:
  96. attr->samp_flags = (va_arg(ap, int) | attr->samp_flags);
  97. break;
  98. case TEST_ATTR_KEY_STRING:
  99. string = va_arg(ap, char *);
  100. if (!ast_strlen_zero(string)) {
  101. ast_copy_string(attr->string, string, sizeof(attr->string));
  102. }
  103. break;
  104. default:
  105. ast_log(LOG_WARNING, "unknown attribute type %d\n", key);
  106. }
  107. }
  108. }
  109. /*! uLaw does not actually have any attributes associated with it.
  110. * This is just for the purpose of testing. We are guaranteed there
  111. * will never exist a interface for uLaw already. */
  112. static struct ast_format_attr_interface test_interface = {
  113. .id = AST_FORMAT_TESTLAW,
  114. .format_attr_cmp = test_cmp,
  115. .format_attr_get_joint = test_getjoint,
  116. .format_attr_set = test_set
  117. };
  118. /*!
  119. * \internal
  120. */
  121. AST_TEST_DEFINE(format_test1)
  122. {
  123. struct ast_format format1 = { 0, };
  124. struct ast_format format2 = { 0, };
  125. struct ast_format joint = { 0, };
  126. switch (cmd) {
  127. case TEST_INIT:
  128. info->name = "ast_format_test1";
  129. info->category = "/main/format/";
  130. info->summary = "Test ast_format with attributes.";
  131. info->description =
  132. "This test exercises the Ast Format API by creating and registering "
  133. "a custom ast_format_attr_interface and performing various function "
  134. "calls on ast_formats using the interface. ";
  135. return AST_TEST_NOT_RUN;
  136. case TEST_EXECUTE:
  137. break;
  138. }
  139. if (ast_format_attr_reg_interface(&test_interface)) {
  140. ast_test_status_update(test, "test_interface failed to register.\n");
  141. return AST_TEST_FAIL;
  142. }
  143. /* set a format with a single attribute. */
  144. ast_format_set(&format1, AST_FORMAT_TESTLAW, 1,
  145. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
  146. AST_FORMAT_ATTR_END);
  147. if (ast_format_isset(&format1, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, AST_FORMAT_ATTR_END)) {
  148. ast_test_status_update(test, "format1 did not set number attribute correctly.\n");
  149. return AST_TEST_FAIL;
  150. }
  151. if (!ast_format_isset(&format1, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_12KHZ, AST_FORMAT_ATTR_END)) {
  152. ast_test_status_update(test, "format1 did not determine isset on number correctly. \n");
  153. return AST_TEST_FAIL;
  154. }
  155. /* append the string attribute to a format with previous attributes already set */
  156. ast_format_append(&format1,
  157. TEST_ATTR_KEY_STRING,"String",
  158. AST_FORMAT_ATTR_END);
  159. if (ast_format_isset(&format1, TEST_ATTR_KEY_STRING, "String", AST_FORMAT_ATTR_END)) {
  160. ast_test_status_update(test, "format1 did not set string attribute correctly.\n");
  161. return AST_TEST_FAIL;
  162. }
  163. if (!ast_format_isset(&format1, TEST_ATTR_KEY_STRING, "Not a string", AST_FORMAT_ATTR_END)) {
  164. ast_test_status_update(test, "format1 did not determine isset on string correctly. \n");
  165. return AST_TEST_FAIL;
  166. }
  167. /* set format2 with both STRING and NUMBER at the same time */
  168. ast_format_set(&format2, AST_FORMAT_TESTLAW, 1,
  169. TEST_ATTR_KEY_STRING, "MOOOoo",
  170. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
  171. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
  172. AST_FORMAT_ATTR_END);
  173. /* perform isset with multiple key value pairs. */
  174. if (ast_format_isset(&format2,
  175. TEST_ATTR_KEY_STRING, "MOOOoo",
  176. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
  177. AST_FORMAT_ATTR_END)) {
  178. ast_test_status_update(test, "format2 did not set attributes correctly.\n");
  179. return AST_TEST_FAIL;
  180. }
  181. if (!ast_format_isset(&format2,
  182. TEST_ATTR_KEY_STRING, "WRONG",
  183. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
  184. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
  185. AST_FORMAT_ATTR_END)) {
  186. ast_test_status_update(test, "format2 did not deterine isset correctly.\n");
  187. return AST_TEST_FAIL;
  188. }
  189. /* get joint attributes between format1 and format2. */
  190. if (ast_format_joint(&format1, &format2, &joint)) {
  191. ast_test_status_update(test, "failed to get joint attributes.\n");
  192. return AST_TEST_FAIL;
  193. }
  194. if (ast_format_isset(&joint, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, AST_FORMAT_ATTR_END)) {
  195. ast_test_status_update(test, "joint attribute was not what we expected.\n");
  196. return AST_TEST_FAIL;
  197. }
  198. /* exercise compare functions */
  199. if (ast_format_cmp(&format1, &format2) != AST_FORMAT_CMP_NOT_EQUAL) {
  200. ast_test_status_update(test, "cmp 1 failed.\n");
  201. return AST_TEST_FAIL;
  202. }
  203. if (ast_format_cmp(&format1, &format1) != AST_FORMAT_CMP_EQUAL) {
  204. ast_test_status_update(test, "cmp 2 failed.\n");
  205. return AST_TEST_FAIL;
  206. }
  207. if (ast_format_cmp(&joint, &format1) != AST_FORMAT_CMP_SUBSET) {
  208. ast_test_status_update(test, "cmp 3 failed.\n");
  209. return AST_TEST_FAIL;
  210. }
  211. /* unregister interface */
  212. if (ast_format_attr_unreg_interface(&test_interface)) {
  213. ast_test_status_update(test, "test_interface failed to unregister.\n");
  214. return AST_TEST_FAIL;
  215. }
  216. return AST_TEST_PASS;
  217. }
  218. /*!
  219. * \internal
  220. */
  221. AST_TEST_DEFINE(format_test2)
  222. {
  223. struct ast_format format = { 0, };
  224. switch (cmd) {
  225. case TEST_INIT:
  226. info->name = "ast_format_test2";
  227. info->category = "/main/format/";
  228. info->summary = "Test ast_format unique id and category system";
  229. info->description =
  230. "This test exercises the Ast Format unique id and category "
  231. "system by creating formats of various types and verifying "
  232. "their category matches what we expect.";
  233. return AST_TEST_NOT_RUN;
  234. case TEST_EXECUTE:
  235. break;
  236. }
  237. ast_format_set(&format, AST_FORMAT_ULAW, 0);
  238. if (AST_FORMAT_GET_TYPE(format.id) != AST_FORMAT_TYPE_AUDIO) {
  239. ast_test_status_update(test, "audio type failed\n");
  240. return AST_TEST_FAIL;
  241. }
  242. ast_format_set(&format, AST_FORMAT_H264, 0);
  243. if (AST_FORMAT_GET_TYPE(format.id) != AST_FORMAT_TYPE_VIDEO) {
  244. ast_test_status_update(test, "video type failed\n");
  245. return AST_TEST_FAIL;
  246. }
  247. ast_format_set(&format, AST_FORMAT_JPEG, 0);
  248. if (AST_FORMAT_GET_TYPE(format.id) != AST_FORMAT_TYPE_IMAGE) {
  249. ast_test_status_update(test, "image type failed\n");
  250. return AST_TEST_FAIL;
  251. }
  252. ast_format_set(&format, AST_FORMAT_T140, 0);
  253. if (AST_FORMAT_GET_TYPE(format.id) != AST_FORMAT_TYPE_TEXT) {
  254. ast_test_status_update(test, "text type failed\n");
  255. return AST_TEST_FAIL;
  256. }
  257. return AST_TEST_PASS;
  258. }
  259. static int container_test1_helper(struct ast_format_cap *cap1, struct ast_format_cap *cap2, struct ast_test *test)
  260. {
  261. int res = AST_TEST_PASS;
  262. struct ast_format_cap *cap_joint = NULL;
  263. struct ast_format tmpformat;
  264. if (ast_format_attr_reg_interface(&test_interface)) {
  265. ast_test_status_update(test, "test_interface failed to register.\n");
  266. ast_format_cap_destroy(cap1);
  267. ast_format_cap_destroy(cap2);
  268. return AST_TEST_FAIL;
  269. }
  270. ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0));
  271. ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0));
  272. ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_G722, 0));
  273. ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_ALAW, 0));
  274. ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_H264, 0));
  275. ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_H263, 0));
  276. ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_T140, 0));
  277. ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_JPEG, 0));
  278. ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
  279. TEST_ATTR_KEY_STRING, "testing caps hooray",
  280. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
  281. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
  282. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_32KHZ,
  283. AST_FORMAT_ATTR_END));
  284. /* Test is compatible */
  285. if (!ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_ALAW, 0)) ||
  286. !ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0)) ||
  287. !ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0)) ||
  288. !ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_H264, 0)) ||
  289. !ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_JPEG, 0)) ||
  290. !ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_T140, 0))) {
  291. ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 1.\n");
  292. res = AST_TEST_FAIL;
  293. goto test3_cleanup;
  294. }
  295. /* Test things that are not compatible */
  296. if (ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_SPEEX, 0)) ||
  297. ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_SPEEX16, 0)) ||
  298. ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_H261, 0))) {
  299. ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 2.\n");
  300. res = AST_TEST_FAIL;
  301. goto test3_cleanup;
  302. }
  303. /* Test compatiblity with format with attributes. */
  304. if (!ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
  305. TEST_ATTR_KEY_STRING, "testing caps hooray",
  306. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
  307. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
  308. AST_FORMAT_ATTR_END))) {
  309. ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 3.\n");
  310. res = AST_TEST_FAIL;
  311. goto test3_cleanup;
  312. }
  313. if (!ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
  314. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
  315. AST_FORMAT_ATTR_END))) {
  316. ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 4.\n");
  317. res = AST_TEST_FAIL;
  318. goto test3_cleanup;
  319. }
  320. if (ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
  321. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
  322. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_48KHZ, /* 48khz was not compatible, so this should fail iscompatible check */
  323. AST_FORMAT_ATTR_END))) {
  324. ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 5.\n");
  325. res = AST_TEST_FAIL;
  326. goto test3_cleanup;
  327. }
  328. /* Lets start testing the functions that compare ast_format_cap objects.
  329. * Genreate the cap2 object to contain some similar formats as cap1
  330. * and some different formats as well. */
  331. ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0));
  332. ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0));
  333. ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_SIREN7, 0));
  334. ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_H261, 0));
  335. ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_T140, 0));
  336. ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
  337. TEST_ATTR_KEY_STRING, "testing caps hooray",
  338. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
  339. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_12KHZ,
  340. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
  341. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_32KHZ,
  342. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_48KHZ,
  343. AST_FORMAT_ATTR_END));
  344. /* find joint formats between cap1 and cap2 */
  345. cap_joint = ast_format_cap_joint(cap1, cap2);
  346. if (!cap_joint) {
  347. ast_test_status_update(test, "failed to create joint capabilities correctly.\n");
  348. res = AST_TEST_FAIL;
  349. goto test3_cleanup;
  350. }
  351. /* determine if cap_joint is what we think it should be */
  352. if (!ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0)) ||
  353. !ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0)) ||
  354. !ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_T140, 0)) ||
  355. !ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
  356. TEST_ATTR_KEY_STRING, "testing caps hooray",
  357. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
  358. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
  359. AST_FORMAT_ATTR_END))) {
  360. ast_test_status_update(test, "ast cap_joint failed to properly detect compatibility test 1.\n");
  361. res = AST_TEST_FAIL;
  362. goto test3_cleanup;
  363. }
  364. /* make sure joint cap does not have formats that should not be there */
  365. if (ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_SIREN7, 0)) ||
  366. ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
  367. TEST_ATTR_KEY_STRING, "testing caps hooray",
  368. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
  369. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
  370. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_48KHZ,
  371. AST_FORMAT_ATTR_END))) {
  372. ast_test_status_update(test, "ast cap_joint failed to properly detect compatibility test 1.\n");
  373. res = AST_TEST_FAIL;
  374. goto test3_cleanup;
  375. }
  376. /* Lets test removing a capability */
  377. if (ast_format_cap_remove(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_T140, 0))) {
  378. ast_test_status_update(test, "ast_format_cap_remove failed. \n");
  379. res = AST_TEST_FAIL;
  380. goto test3_cleanup;
  381. }
  382. /* Lets make sure what we just removed does not still exist */
  383. if (ast_format_cap_iscompatible(cap_joint, &tmpformat)) {
  384. ast_test_status_update(test, "ast_format_cap_remove failed 2. \n");
  385. res = AST_TEST_FAIL;
  386. goto test3_cleanup;
  387. }
  388. /* Lets test removing a capability by id.*/
  389. if (ast_format_cap_remove_byid(cap_joint, AST_FORMAT_GSM)) {
  390. ast_test_status_update(test, "ast_format_cap_remove failed 3. \n");
  391. res = AST_TEST_FAIL;
  392. goto test3_cleanup;
  393. }
  394. /* Lets make sure what we just removed does not still exist */
  395. if (ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0))) {
  396. ast_test_status_update(test, "ast_format_cap_remove failed 4. \n");
  397. res = AST_TEST_FAIL;
  398. goto test3_cleanup;
  399. }
  400. /* lets test getting joint formats by type */
  401. ast_format_cap_destroy(cap_joint);
  402. if (!(cap_joint = ast_format_cap_get_type(cap1, AST_FORMAT_TYPE_VIDEO))) {
  403. ast_test_status_update(test, "ast_format_cap_get_type failed.\n");
  404. res = AST_TEST_FAIL;
  405. goto test3_cleanup;
  406. }
  407. /* lets make sure our joint capability structure has what we expect */
  408. if (!ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_H264, 0)) ||
  409. !ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_H263, 0))) {
  410. ast_test_status_update(test, "get_type failed 2.\n");
  411. res = AST_TEST_FAIL;
  412. goto test3_cleanup;
  413. }
  414. /* now make sure joint does not have anything but video */
  415. if (ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_ALAW, 0)) ||
  416. ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0)) ||
  417. ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0)) ||
  418. ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_JPEG, 0)) ||
  419. ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_T140, 0))) {
  420. ast_test_status_update(test, "get_type failed 3.\n");
  421. res = AST_TEST_FAIL;
  422. goto test3_cleanup;
  423. }
  424. /* now lets remove everythign from cap_joint */
  425. ast_format_cap_remove_all(cap_joint);
  426. if (!ast_format_cap_is_empty(cap_joint)) {
  427. ast_test_status_update(test, "failed to remove all\n");
  428. res = AST_TEST_FAIL;
  429. goto test3_cleanup;
  430. }
  431. /* now lets add all by type */
  432. ast_format_cap_add_all_by_type(cap_joint, AST_FORMAT_TYPE_AUDIO);
  433. if (ast_format_cap_is_empty(cap_joint)) {
  434. ast_test_status_update(test, "failed to add all by type AUDIO\n");
  435. res = AST_TEST_FAIL;
  436. }
  437. ast_format_cap_iter_start(cap_joint);
  438. while (!(ast_format_cap_iter_next(cap_joint, &tmpformat))) {
  439. if (AST_FORMAT_GET_TYPE(tmpformat.id) != AST_FORMAT_TYPE_AUDIO) {
  440. ast_test_status_update(test, "failed to add all by type AUDIO\n");
  441. res = AST_TEST_FAIL;
  442. ast_format_cap_iter_end(cap_joint);
  443. goto test3_cleanup;
  444. }
  445. }
  446. ast_format_cap_iter_end(cap_joint);
  447. /* test append */
  448. ast_format_cap_append(cap_joint, cap1);
  449. ast_format_cap_iter_start(cap1);
  450. while (!(ast_format_cap_iter_next(cap1, &tmpformat))) {
  451. if (!ast_format_cap_iscompatible(cap_joint, &tmpformat)) {
  452. ast_test_status_update(test, "failed to append format capabilities.\n");
  453. res = AST_TEST_FAIL;
  454. ast_format_cap_iter_end(cap1);
  455. goto test3_cleanup;
  456. }
  457. }
  458. ast_format_cap_iter_end(cap1);
  459. /* test copy */
  460. cap1 = ast_format_cap_destroy(cap1);
  461. cap1 = ast_format_cap_dup(cap_joint);
  462. if (!ast_format_cap_identical(cap_joint, cap1)) {
  463. ast_test_status_update(test, "failed to copy capabilities\n");
  464. res = AST_TEST_FAIL;
  465. goto test3_cleanup;
  466. }
  467. /* test remove by type */
  468. ast_format_cap_remove_bytype(cap_joint, AST_FORMAT_TYPE_AUDIO);
  469. if (ast_format_cap_has_type(cap_joint, AST_FORMAT_TYPE_AUDIO)) {
  470. ast_test_status_update(test, "failed to remove all by type audio\n");
  471. res = AST_TEST_FAIL;
  472. goto test3_cleanup;
  473. }
  474. if (!ast_format_cap_has_type(cap_joint, AST_FORMAT_TYPE_TEXT)) { /* it should still have text */
  475. ast_test_status_update(test, "failed to remove all by type audio\n");
  476. res = AST_TEST_FAIL;
  477. goto test3_cleanup;
  478. }
  479. ast_format_cap_iter_start(cap_joint);
  480. while (!(ast_format_cap_iter_next(cap_joint, &tmpformat))) {
  481. if (AST_FORMAT_GET_TYPE(tmpformat.id) == AST_FORMAT_TYPE_AUDIO) {
  482. ast_test_status_update(test, "failed to remove all by type audio\n");
  483. res = AST_TEST_FAIL;
  484. ast_format_cap_iter_end(cap_joint);
  485. goto test3_cleanup;
  486. }
  487. }
  488. ast_format_cap_iter_end(cap_joint);
  489. /* test add all */
  490. ast_format_cap_remove_all(cap_joint);
  491. ast_format_cap_add_all(cap_joint);
  492. {
  493. int video = 0, audio = 0, text = 0, image = 0;
  494. ast_format_cap_iter_start(cap_joint);
  495. while (!(ast_format_cap_iter_next(cap_joint, &tmpformat))) {
  496. switch (AST_FORMAT_GET_TYPE(tmpformat.id)) {
  497. case AST_FORMAT_TYPE_AUDIO:
  498. audio++;
  499. break;
  500. case AST_FORMAT_TYPE_VIDEO:
  501. video++;
  502. break;
  503. case AST_FORMAT_TYPE_TEXT:
  504. text++;
  505. break;
  506. case AST_FORMAT_TYPE_IMAGE:
  507. image++;
  508. break;
  509. }
  510. }
  511. ast_format_cap_iter_end(cap_joint);
  512. if (!video || !audio || !text || !image) {
  513. ast_test_status_update(test, "failed to add all\n");
  514. res = AST_TEST_FAIL;
  515. ast_format_cap_iter_end(cap_joint);
  516. goto test3_cleanup;
  517. }
  518. }
  519. /* test copy2 */
  520. ast_format_cap_copy(cap2, cap_joint);
  521. if (!ast_format_cap_identical(cap2, cap_joint)) {
  522. ast_test_status_update(test, "ast_format_cap_copy failed\n");
  523. res = AST_TEST_FAIL;
  524. goto test3_cleanup;
  525. }
  526. test3_cleanup:
  527. ast_format_cap_destroy(cap1);
  528. ast_format_cap_destroy(cap2);
  529. ast_format_cap_destroy(cap_joint);
  530. /* unregister interface */
  531. if (ast_format_attr_unreg_interface(&test_interface)) {
  532. ast_test_status_update(test, "test_interface failed to unregister.\n");
  533. res = AST_TEST_FAIL;
  534. }
  535. return res;
  536. }
  537. /*!
  538. * \internal
  539. */
  540. AST_TEST_DEFINE(container_test1_nolock)
  541. {
  542. struct ast_format_cap *cap1;
  543. struct ast_format_cap *cap2;
  544. switch (cmd) {
  545. case TEST_INIT:
  546. info->name = "container_test_1_no_locking";
  547. info->category = "/main/format/";
  548. info->summary = "Test ast_format and ast_format_cap structures, no locking";
  549. info->description =
  550. "This test exercises the Ast Format Capability API by creating "
  551. "capability structures and performing various API calls on them.";
  552. return AST_TEST_NOT_RUN;
  553. case TEST_EXECUTE:
  554. break;
  555. }
  556. cap1 = ast_format_cap_alloc_nolock();
  557. cap2 = ast_format_cap_alloc_nolock();
  558. if (!cap1 || !cap2) {
  559. ast_test_status_update(test, "cap alloc failed.\n");
  560. return AST_TEST_FAIL;
  561. }
  562. return container_test1_helper(cap1, cap2, test);
  563. }
  564. /*!
  565. * \internal
  566. */
  567. AST_TEST_DEFINE(container_test1_withlock)
  568. {
  569. struct ast_format_cap *cap1;
  570. struct ast_format_cap *cap2;
  571. switch (cmd) {
  572. case TEST_INIT:
  573. info->name = "container_test1_with_locking";
  574. info->category = "/main/format/";
  575. info->summary = "Test ast_format and ast_format_cap structures, with locking";
  576. info->description =
  577. "This test exercises the Ast Format Capability API by creating "
  578. "capability structures and performing various API calls on them.";
  579. return AST_TEST_NOT_RUN;
  580. case TEST_EXECUTE:
  581. break;
  582. }
  583. cap1 = ast_format_cap_alloc();
  584. cap2 = ast_format_cap_alloc();
  585. if (!cap1 || !cap2) {
  586. ast_test_status_update(test, "cap alloc failed.\n");
  587. return AST_TEST_FAIL;
  588. }
  589. return container_test1_helper(cap1, cap2, test);
  590. }
  591. static int container_test2_no_locking_helper(struct ast_format_cap *cap, struct ast_test *test)
  592. {
  593. int num = 0;
  594. struct ast_format tmpformat = { 0, };
  595. ast_format_cap_add(cap, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0));
  596. ast_format_cap_add(cap, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0));
  597. ast_format_cap_add(cap, ast_format_set(&tmpformat, AST_FORMAT_G722, 0));
  598. ast_format_cap_iter_start(cap);
  599. while (!ast_format_cap_iter_next(cap, &tmpformat)) {
  600. num++;
  601. }
  602. ast_format_cap_iter_end(cap);
  603. ast_format_cap_iter_start(cap);
  604. while (!ast_format_cap_iter_next(cap, &tmpformat)) {
  605. num++;
  606. }
  607. ast_format_cap_iter_end(cap);
  608. ast_format_cap_destroy(cap);
  609. ast_test_status_update(test, "%d items iterated over\n", num);
  610. return (num == 6) ? AST_TEST_PASS : AST_TEST_FAIL;
  611. }
  612. /*!
  613. * \internal
  614. */
  615. AST_TEST_DEFINE(container_test2_no_locking)
  616. {
  617. struct ast_format_cap *cap;
  618. switch (cmd) {
  619. case TEST_INIT:
  620. info->name = "container_test2_no_locking";
  621. info->category = "/main/format/";
  622. info->summary = "Test ast_format_cap iterator, no locking";
  623. info->description =
  624. "This test exercises the Ast Capability API iterators.";
  625. return AST_TEST_NOT_RUN;
  626. case TEST_EXECUTE:
  627. break;
  628. }
  629. cap = ast_format_cap_alloc_nolock();
  630. if (!cap) {
  631. ast_test_status_update(test, "alloc failed\n");
  632. return AST_TEST_FAIL;
  633. }
  634. return container_test2_no_locking_helper(cap, test);
  635. }
  636. /*!
  637. * \internal
  638. */
  639. AST_TEST_DEFINE(container_test2_with_locking)
  640. {
  641. struct ast_format_cap *cap;
  642. switch (cmd) {
  643. case TEST_INIT:
  644. info->name = "container_test2_with_locking";
  645. info->category = "/main/format/";
  646. info->summary = "Test ast_format_cap iterator, with locking";
  647. info->description =
  648. "This test exercises the Ast Capability API iterators.";
  649. return AST_TEST_NOT_RUN;
  650. case TEST_EXECUTE:
  651. break;
  652. }
  653. cap = ast_format_cap_alloc();
  654. if (!cap) {
  655. ast_test_status_update(test, "alloc failed\n");
  656. return AST_TEST_FAIL;
  657. }
  658. return container_test2_no_locking_helper(cap, test);
  659. }
  660. static int container_test3_helper(int nolocking, struct ast_test *test)
  661. {
  662. int x;
  663. int res = AST_TEST_PASS;
  664. struct ast_format_cap *cap1;
  665. struct ast_format_cap *cap2;
  666. struct ast_format_cap *joint;
  667. for (x = 0; x < 2000; x++) {
  668. if (nolocking) {
  669. cap1 = ast_format_cap_alloc_nolock();
  670. cap2 = ast_format_cap_alloc_nolock();
  671. joint = ast_format_cap_alloc_nolock();
  672. } else {
  673. cap1 = ast_format_cap_alloc();
  674. cap2 = ast_format_cap_alloc();
  675. joint = ast_format_cap_alloc();
  676. }
  677. if (!cap1 || !cap2 || !joint) {
  678. ast_test_status_update(test, "cap alloc fail\n");
  679. return AST_TEST_FAIL;
  680. }
  681. ast_format_cap_add_all(cap1);
  682. ast_format_cap_add_all_by_type(cap2, AST_FORMAT_TYPE_AUDIO);
  683. ast_format_cap_joint_copy(cap1, cap2, joint);
  684. if (!(ast_format_cap_identical(cap2, joint))) {
  685. ast_test_status_update(test, "failed identical test\n");
  686. res = AST_TEST_FAIL;
  687. cap1 = ast_format_cap_destroy(cap1);
  688. cap2 = ast_format_cap_destroy(cap2);
  689. joint = ast_format_cap_destroy(joint);
  690. break;
  691. }
  692. cap1 = ast_format_cap_destroy(cap1);
  693. cap2 = ast_format_cap_destroy(cap2);
  694. joint = ast_format_cap_destroy(joint);
  695. }
  696. return res;
  697. }
  698. /*!
  699. * \internal
  700. */
  701. AST_TEST_DEFINE(container_test3_nolock)
  702. {
  703. switch (cmd) {
  704. case TEST_INIT:
  705. info->name = "container_test3_no_locking";
  706. info->category = "/main/format/";
  707. info->summary = "Load Test ast_format_cap no locking.";
  708. info->description =
  709. "This test exercises the Ast Capability API and its iterators for the purpose "
  710. "of measuring performance.";
  711. return AST_TEST_NOT_RUN;
  712. case TEST_EXECUTE:
  713. break;
  714. }
  715. return container_test3_helper(1, test);
  716. }
  717. /*!
  718. * \internal
  719. */
  720. AST_TEST_DEFINE(container_test3_withlock)
  721. {
  722. switch (cmd) {
  723. case TEST_INIT:
  724. info->name = "container_test3_with_locking";
  725. info->category = "/main/format/";
  726. info->summary = "Load Test ast_format_cap with locking.";
  727. info->description =
  728. "This test exercises the Ast Capability API and its iterators for the purpose "
  729. "of measuring performance.";
  730. return AST_TEST_NOT_RUN;
  731. case TEST_EXECUTE:
  732. break;
  733. }
  734. return container_test3_helper(0, test);
  735. }
  736. static int unload_module(void)
  737. {
  738. AST_TEST_UNREGISTER(format_test1);
  739. AST_TEST_UNREGISTER(format_test2);
  740. AST_TEST_UNREGISTER(container_test1_nolock);
  741. AST_TEST_UNREGISTER(container_test1_withlock);
  742. AST_TEST_UNREGISTER(container_test2_no_locking);
  743. AST_TEST_UNREGISTER(container_test2_with_locking);
  744. AST_TEST_UNREGISTER(container_test3_nolock);
  745. AST_TEST_UNREGISTER(container_test3_withlock);
  746. return 0;
  747. }
  748. static int load_module(void)
  749. {
  750. AST_TEST_REGISTER(format_test1);
  751. AST_TEST_REGISTER(format_test2);
  752. AST_TEST_REGISTER(container_test1_nolock);
  753. AST_TEST_REGISTER(container_test1_withlock);
  754. AST_TEST_REGISTER(container_test2_no_locking);
  755. AST_TEST_REGISTER(container_test2_with_locking);
  756. AST_TEST_REGISTER(container_test3_nolock);
  757. AST_TEST_REGISTER(container_test3_withlock);
  758. return AST_MODULE_LOAD_SUCCESS;
  759. }
  760. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ast_format API Tests");