123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860 |
- /*
- * Asterisk -- An open source telephony toolkit.
- *
- * Copyright (C) 2010, Digium, Inc.
- *
- * David Vossel <dvossel@digium.com>
- *
- * See http://www.asterisk.org for more information about
- * the Asterisk project. Please do not directly contact
- * any of the maintainers of this project for assistance;
- * the project provides a web site, mailing lists and IRC
- * channels for your use.
- *
- * This program is free software, distributed under the terms of
- * the GNU General Public License Version 2. See the LICENSE file
- * at the top of the source tree.
- */
- /*!
- * \file
- * \brief Tests for the ast_event API
- *
- * \author David Vossel <dvossel@digium.com>
- *
- * \ingroup tests
- *
- */
- /*** MODULEINFO
- <depend>TEST_FRAMEWORK</depend>
- ***/
- #include "asterisk.h"
- ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
- #include "asterisk/module.h"
- #include "asterisk/test.h"
- #include "asterisk/format.h"
- #include "asterisk/format_cap.h"
- #include "asterisk/strings.h"
- /*! These are the keys for accessing attributes */
- enum test_attr_keys {
- TEST_ATTR_KEY_SAMP_RATE,
- TEST_ATTR_KEY_STRING,
- };
- /*! These are the values for the TEST_ATTR_KEY_SAMP_RATE key */
- enum test_attr_vals_samp {
- TEST_ATTR_VAL_SAMP_8KHZ = (1 << 0),
- TEST_ATTR_VAL_SAMP_12KHZ = (1 << 1),
- TEST_ATTR_VAL_SAMP_16KHZ = (1 << 2),
- TEST_ATTR_VAL_SAMP_32KHZ = (1 << 3),
- TEST_ATTR_VAL_SAMP_48KHZ = (1 << 4),
- };
- /*! This is the attribute structure used for our test interface. */
- struct test_attr {
- enum test_attr_vals_samp samp_flags;
- char string[32];
- };
- static enum ast_format_cmp_res test_cmp(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2)
- {
- struct test_attr *attr1 = (struct test_attr *) fattr1;
- struct test_attr *attr2 = (struct test_attr *) fattr2;
- if ((attr1->samp_flags == attr2->samp_flags) &&
- !(strcmp(attr1->string, attr2->string))) {
- return AST_FORMAT_CMP_EQUAL;
- }
- if ((attr1->samp_flags != (attr1->samp_flags & attr2->samp_flags)) ||
- (!ast_strlen_zero(attr1->string) && strcmp(attr1->string, attr2->string))) {
- return AST_FORMAT_CMP_NOT_EQUAL;
- }
- return AST_FORMAT_CMP_SUBSET;
- }
- static int test_getjoint(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2, struct ast_format_attr *result)
- {
- struct test_attr *attr1 = (struct test_attr *) fattr1;
- struct test_attr *attr2 = (struct test_attr *) fattr2;
- struct test_attr *attr_res = (struct test_attr *) result;
- int joint = -1;
- attr_res->samp_flags = (attr1->samp_flags & attr2->samp_flags);
- if (attr_res->samp_flags) {
- joint = 0;
- }
- if (!strcmp(attr1->string, attr2->string)) {
- ast_copy_string(attr_res->string, attr1->string, sizeof(attr_res->string));
- joint = 0;
- }
- return joint;
- }
- static void test_set(struct ast_format_attr *fattr, va_list ap)
- {
- enum test_attr_keys key;
- struct test_attr *attr = (struct test_attr *) fattr;
- char *string;
- for (key = va_arg(ap, int);
- key != AST_FORMAT_ATTR_END;
- key = va_arg(ap, int))
- {
- switch (key) {
- case TEST_ATTR_KEY_SAMP_RATE:
- attr->samp_flags = (va_arg(ap, int) | attr->samp_flags);
- break;
- case TEST_ATTR_KEY_STRING:
- string = va_arg(ap, char *);
- if (!ast_strlen_zero(string)) {
- ast_copy_string(attr->string, string, sizeof(attr->string));
- }
- break;
- default:
- ast_log(LOG_WARNING, "unknown attribute type %d\n", key);
- }
- }
- }
- /*! uLaw does not actually have any attributes associated with it.
- * This is just for the purpose of testing. We are guaranteed there
- * will never exist a interface for uLaw already. */
- static struct ast_format_attr_interface test_interface = {
- .id = AST_FORMAT_TESTLAW,
- .format_attr_cmp = test_cmp,
- .format_attr_get_joint = test_getjoint,
- .format_attr_set = test_set
- };
- /*!
- * \internal
- */
- AST_TEST_DEFINE(format_test1)
- {
- struct ast_format format1 = { 0, };
- struct ast_format format2 = { 0, };
- struct ast_format joint = { 0, };
- switch (cmd) {
- case TEST_INIT:
- info->name = "ast_format_test1";
- info->category = "/main/format/";
- info->summary = "Test ast_format with attributes.";
- info->description =
- "This test exercises the Ast Format API by creating and registering "
- "a custom ast_format_attr_interface and performing various function "
- "calls on ast_formats using the interface. ";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- if (ast_format_attr_reg_interface(&test_interface)) {
- ast_test_status_update(test, "test_interface failed to register.\n");
- return AST_TEST_FAIL;
- }
- /* set a format with a single attribute. */
- ast_format_set(&format1, AST_FORMAT_TESTLAW, 1,
- TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
- AST_FORMAT_ATTR_END);
- if (ast_format_isset(&format1, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, AST_FORMAT_ATTR_END)) {
- ast_test_status_update(test, "format1 did not set number attribute correctly.\n");
- return AST_TEST_FAIL;
- }
- if (!ast_format_isset(&format1, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_12KHZ, AST_FORMAT_ATTR_END)) {
- ast_test_status_update(test, "format1 did not determine isset on number correctly. \n");
- return AST_TEST_FAIL;
- }
- /* append the string attribute to a format with previous attributes already set */
- ast_format_append(&format1,
- TEST_ATTR_KEY_STRING,"String",
- AST_FORMAT_ATTR_END);
- if (ast_format_isset(&format1, TEST_ATTR_KEY_STRING, "String", AST_FORMAT_ATTR_END)) {
- ast_test_status_update(test, "format1 did not set string attribute correctly.\n");
- return AST_TEST_FAIL;
- }
- if (!ast_format_isset(&format1, TEST_ATTR_KEY_STRING, "Not a string", AST_FORMAT_ATTR_END)) {
- ast_test_status_update(test, "format1 did not determine isset on string correctly. \n");
- return AST_TEST_FAIL;
- }
- /* set format2 with both STRING and NUMBER at the same time */
- ast_format_set(&format2, AST_FORMAT_TESTLAW, 1,
- TEST_ATTR_KEY_STRING, "MOOOoo",
- TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
- TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
- AST_FORMAT_ATTR_END);
- /* perform isset with multiple key value pairs. */
- if (ast_format_isset(&format2,
- TEST_ATTR_KEY_STRING, "MOOOoo",
- TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
- AST_FORMAT_ATTR_END)) {
- ast_test_status_update(test, "format2 did not set attributes correctly.\n");
- return AST_TEST_FAIL;
- }
- if (!ast_format_isset(&format2,
- TEST_ATTR_KEY_STRING, "WRONG",
- TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
- TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
- AST_FORMAT_ATTR_END)) {
- ast_test_status_update(test, "format2 did not deterine isset correctly.\n");
- return AST_TEST_FAIL;
- }
- /* get joint attributes between format1 and format2. */
- if (ast_format_joint(&format1, &format2, &joint)) {
- ast_test_status_update(test, "failed to get joint attributes.\n");
- return AST_TEST_FAIL;
- }
- if (ast_format_isset(&joint, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, AST_FORMAT_ATTR_END)) {
- ast_test_status_update(test, "joint attribute was not what we expected.\n");
- return AST_TEST_FAIL;
- }
- /* exercise compare functions */
- if (ast_format_cmp(&format1, &format2) != AST_FORMAT_CMP_NOT_EQUAL) {
- ast_test_status_update(test, "cmp 1 failed.\n");
- return AST_TEST_FAIL;
- }
- if (ast_format_cmp(&format1, &format1) != AST_FORMAT_CMP_EQUAL) {
- ast_test_status_update(test, "cmp 2 failed.\n");
- return AST_TEST_FAIL;
- }
- if (ast_format_cmp(&joint, &format1) != AST_FORMAT_CMP_SUBSET) {
- ast_test_status_update(test, "cmp 3 failed.\n");
- return AST_TEST_FAIL;
- }
- /* unregister interface */
- if (ast_format_attr_unreg_interface(&test_interface)) {
- ast_test_status_update(test, "test_interface failed to unregister.\n");
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- /*!
- * \internal
- */
- AST_TEST_DEFINE(format_test2)
- {
- struct ast_format format = { 0, };
- switch (cmd) {
- case TEST_INIT:
- info->name = "ast_format_test2";
- info->category = "/main/format/";
- info->summary = "Test ast_format unique id and category system";
- info->description =
- "This test exercises the Ast Format unique id and category "
- "system by creating formats of various types and verifying "
- "their category matches what we expect.";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- ast_format_set(&format, AST_FORMAT_ULAW, 0);
- if (AST_FORMAT_GET_TYPE(format.id) != AST_FORMAT_TYPE_AUDIO) {
- ast_test_status_update(test, "audio type failed\n");
- return AST_TEST_FAIL;
- }
- ast_format_set(&format, AST_FORMAT_H264, 0);
- if (AST_FORMAT_GET_TYPE(format.id) != AST_FORMAT_TYPE_VIDEO) {
- ast_test_status_update(test, "video type failed\n");
- return AST_TEST_FAIL;
- }
- ast_format_set(&format, AST_FORMAT_JPEG, 0);
- if (AST_FORMAT_GET_TYPE(format.id) != AST_FORMAT_TYPE_IMAGE) {
- ast_test_status_update(test, "image type failed\n");
- return AST_TEST_FAIL;
- }
- ast_format_set(&format, AST_FORMAT_T140, 0);
- if (AST_FORMAT_GET_TYPE(format.id) != AST_FORMAT_TYPE_TEXT) {
- ast_test_status_update(test, "text type failed\n");
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- static int container_test1_helper(struct ast_format_cap *cap1, struct ast_format_cap *cap2, struct ast_test *test)
- {
- int res = AST_TEST_PASS;
- struct ast_format_cap *cap_joint = NULL;
- struct ast_format tmpformat;
- if (ast_format_attr_reg_interface(&test_interface)) {
- ast_test_status_update(test, "test_interface failed to register.\n");
- ast_format_cap_destroy(cap1);
- ast_format_cap_destroy(cap2);
- return AST_TEST_FAIL;
- }
- ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0));
- ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0));
- ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_G722, 0));
- ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_ALAW, 0));
- ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_H264, 0));
- ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_H263, 0));
- ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_T140, 0));
- ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_JPEG, 0));
- ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
- TEST_ATTR_KEY_STRING, "testing caps hooray",
- TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
- TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
- TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_32KHZ,
- AST_FORMAT_ATTR_END));
- /* Test is compatible */
- if (!ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_ALAW, 0)) ||
- !ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0)) ||
- !ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0)) ||
- !ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_H264, 0)) ||
- !ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_JPEG, 0)) ||
- !ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_T140, 0))) {
- ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 1.\n");
- res = AST_TEST_FAIL;
- goto test3_cleanup;
- }
- /* Test things that are not compatible */
- if (ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_SPEEX, 0)) ||
- ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_SPEEX16, 0)) ||
- ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_H261, 0))) {
- ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 2.\n");
- res = AST_TEST_FAIL;
- goto test3_cleanup;
- }
- /* Test compatiblity with format with attributes. */
- if (!ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
- TEST_ATTR_KEY_STRING, "testing caps hooray",
- TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
- TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
- AST_FORMAT_ATTR_END))) {
- ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 3.\n");
- res = AST_TEST_FAIL;
- goto test3_cleanup;
- }
- if (!ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
- TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
- AST_FORMAT_ATTR_END))) {
- ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 4.\n");
- res = AST_TEST_FAIL;
- goto test3_cleanup;
- }
- if (ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
- TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
- TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_48KHZ, /* 48khz was not compatible, so this should fail iscompatible check */
- AST_FORMAT_ATTR_END))) {
- ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 5.\n");
- res = AST_TEST_FAIL;
- goto test3_cleanup;
- }
- /* Lets start testing the functions that compare ast_format_cap objects.
- * Genreate the cap2 object to contain some similar formats as cap1
- * and some different formats as well. */
- ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0));
- ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0));
- ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_SIREN7, 0));
- ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_H261, 0));
- ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_T140, 0));
- ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
- TEST_ATTR_KEY_STRING, "testing caps hooray",
- TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
- TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_12KHZ,
- TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
- TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_32KHZ,
- TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_48KHZ,
- AST_FORMAT_ATTR_END));
- /* find joint formats between cap1 and cap2 */
- cap_joint = ast_format_cap_joint(cap1, cap2);
- if (!cap_joint) {
- ast_test_status_update(test, "failed to create joint capabilities correctly.\n");
- res = AST_TEST_FAIL;
- goto test3_cleanup;
- }
- /* determine if cap_joint is what we think it should be */
- if (!ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0)) ||
- !ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0)) ||
- !ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_T140, 0)) ||
- !ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
- TEST_ATTR_KEY_STRING, "testing caps hooray",
- TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
- TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
- AST_FORMAT_ATTR_END))) {
- ast_test_status_update(test, "ast cap_joint failed to properly detect compatibility test 1.\n");
- res = AST_TEST_FAIL;
- goto test3_cleanup;
- }
- /* make sure joint cap does not have formats that should not be there */
- if (ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_SIREN7, 0)) ||
- ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
- TEST_ATTR_KEY_STRING, "testing caps hooray",
- TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
- TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
- TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_48KHZ,
- AST_FORMAT_ATTR_END))) {
- ast_test_status_update(test, "ast cap_joint failed to properly detect compatibility test 1.\n");
- res = AST_TEST_FAIL;
- goto test3_cleanup;
- }
- /* Lets test removing a capability */
- if (ast_format_cap_remove(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_T140, 0))) {
- ast_test_status_update(test, "ast_format_cap_remove failed. \n");
- res = AST_TEST_FAIL;
- goto test3_cleanup;
- }
- /* Lets make sure what we just removed does not still exist */
- if (ast_format_cap_iscompatible(cap_joint, &tmpformat)) {
- ast_test_status_update(test, "ast_format_cap_remove failed 2. \n");
- res = AST_TEST_FAIL;
- goto test3_cleanup;
- }
- /* Lets test removing a capability by id.*/
- if (ast_format_cap_remove_byid(cap_joint, AST_FORMAT_GSM)) {
- ast_test_status_update(test, "ast_format_cap_remove failed 3. \n");
- res = AST_TEST_FAIL;
- goto test3_cleanup;
- }
- /* Lets make sure what we just removed does not still exist */
- if (ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0))) {
- ast_test_status_update(test, "ast_format_cap_remove failed 4. \n");
- res = AST_TEST_FAIL;
- goto test3_cleanup;
- }
- /* lets test getting joint formats by type */
- ast_format_cap_destroy(cap_joint);
- if (!(cap_joint = ast_format_cap_get_type(cap1, AST_FORMAT_TYPE_VIDEO))) {
- ast_test_status_update(test, "ast_format_cap_get_type failed.\n");
- res = AST_TEST_FAIL;
- goto test3_cleanup;
- }
- /* lets make sure our joint capability structure has what we expect */
- if (!ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_H264, 0)) ||
- !ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_H263, 0))) {
- ast_test_status_update(test, "get_type failed 2.\n");
- res = AST_TEST_FAIL;
- goto test3_cleanup;
- }
- /* now make sure joint does not have anything but video */
- if (ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_ALAW, 0)) ||
- ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0)) ||
- ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0)) ||
- ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_JPEG, 0)) ||
- ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_T140, 0))) {
- ast_test_status_update(test, "get_type failed 3.\n");
- res = AST_TEST_FAIL;
- goto test3_cleanup;
- }
- /* now lets remove everythign from cap_joint */
- ast_format_cap_remove_all(cap_joint);
- if (!ast_format_cap_is_empty(cap_joint)) {
- ast_test_status_update(test, "failed to remove all\n");
- res = AST_TEST_FAIL;
- goto test3_cleanup;
- }
- /* now lets add all by type */
- ast_format_cap_add_all_by_type(cap_joint, AST_FORMAT_TYPE_AUDIO);
- if (ast_format_cap_is_empty(cap_joint)) {
- ast_test_status_update(test, "failed to add all by type AUDIO\n");
- res = AST_TEST_FAIL;
- }
- ast_format_cap_iter_start(cap_joint);
- while (!(ast_format_cap_iter_next(cap_joint, &tmpformat))) {
- if (AST_FORMAT_GET_TYPE(tmpformat.id) != AST_FORMAT_TYPE_AUDIO) {
- ast_test_status_update(test, "failed to add all by type AUDIO\n");
- res = AST_TEST_FAIL;
- ast_format_cap_iter_end(cap_joint);
- goto test3_cleanup;
- }
- }
- ast_format_cap_iter_end(cap_joint);
- /* test append */
- ast_format_cap_append(cap_joint, cap1);
- ast_format_cap_iter_start(cap1);
- while (!(ast_format_cap_iter_next(cap1, &tmpformat))) {
- if (!ast_format_cap_iscompatible(cap_joint, &tmpformat)) {
- ast_test_status_update(test, "failed to append format capabilities.\n");
- res = AST_TEST_FAIL;
- ast_format_cap_iter_end(cap1);
- goto test3_cleanup;
- }
- }
- ast_format_cap_iter_end(cap1);
- /* test copy */
- cap1 = ast_format_cap_destroy(cap1);
- cap1 = ast_format_cap_dup(cap_joint);
- if (!ast_format_cap_identical(cap_joint, cap1)) {
- ast_test_status_update(test, "failed to copy capabilities\n");
- res = AST_TEST_FAIL;
- goto test3_cleanup;
- }
- /* test remove by type */
- ast_format_cap_remove_bytype(cap_joint, AST_FORMAT_TYPE_AUDIO);
- if (ast_format_cap_has_type(cap_joint, AST_FORMAT_TYPE_AUDIO)) {
- ast_test_status_update(test, "failed to remove all by type audio\n");
- res = AST_TEST_FAIL;
- goto test3_cleanup;
- }
- if (!ast_format_cap_has_type(cap_joint, AST_FORMAT_TYPE_TEXT)) { /* it should still have text */
- ast_test_status_update(test, "failed to remove all by type audio\n");
- res = AST_TEST_FAIL;
- goto test3_cleanup;
- }
- ast_format_cap_iter_start(cap_joint);
- while (!(ast_format_cap_iter_next(cap_joint, &tmpformat))) {
- if (AST_FORMAT_GET_TYPE(tmpformat.id) == AST_FORMAT_TYPE_AUDIO) {
- ast_test_status_update(test, "failed to remove all by type audio\n");
- res = AST_TEST_FAIL;
- ast_format_cap_iter_end(cap_joint);
- goto test3_cleanup;
- }
- }
- ast_format_cap_iter_end(cap_joint);
- /* test add all */
- ast_format_cap_remove_all(cap_joint);
- ast_format_cap_add_all(cap_joint);
- {
- int video = 0, audio = 0, text = 0, image = 0;
- ast_format_cap_iter_start(cap_joint);
- while (!(ast_format_cap_iter_next(cap_joint, &tmpformat))) {
- switch (AST_FORMAT_GET_TYPE(tmpformat.id)) {
- case AST_FORMAT_TYPE_AUDIO:
- audio++;
- break;
- case AST_FORMAT_TYPE_VIDEO:
- video++;
- break;
- case AST_FORMAT_TYPE_TEXT:
- text++;
- break;
- case AST_FORMAT_TYPE_IMAGE:
- image++;
- break;
- }
- }
- ast_format_cap_iter_end(cap_joint);
- if (!video || !audio || !text || !image) {
- ast_test_status_update(test, "failed to add all\n");
- res = AST_TEST_FAIL;
- ast_format_cap_iter_end(cap_joint);
- goto test3_cleanup;
- }
- }
- /* test copy2 */
- ast_format_cap_copy(cap2, cap_joint);
- if (!ast_format_cap_identical(cap2, cap_joint)) {
- ast_test_status_update(test, "ast_format_cap_copy failed\n");
- res = AST_TEST_FAIL;
- goto test3_cleanup;
- }
- test3_cleanup:
- ast_format_cap_destroy(cap1);
- ast_format_cap_destroy(cap2);
- ast_format_cap_destroy(cap_joint);
- /* unregister interface */
- if (ast_format_attr_unreg_interface(&test_interface)) {
- ast_test_status_update(test, "test_interface failed to unregister.\n");
- res = AST_TEST_FAIL;
- }
- return res;
- }
- /*!
- * \internal
- */
- AST_TEST_DEFINE(container_test1_nolock)
- {
- struct ast_format_cap *cap1;
- struct ast_format_cap *cap2;
- switch (cmd) {
- case TEST_INIT:
- info->name = "container_test_1_no_locking";
- info->category = "/main/format/";
- info->summary = "Test ast_format and ast_format_cap structures, no locking";
- info->description =
- "This test exercises the Ast Format Capability API by creating "
- "capability structures and performing various API calls on them.";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- cap1 = ast_format_cap_alloc_nolock();
- cap2 = ast_format_cap_alloc_nolock();
- if (!cap1 || !cap2) {
- ast_test_status_update(test, "cap alloc failed.\n");
- return AST_TEST_FAIL;
- }
- return container_test1_helper(cap1, cap2, test);
- }
- /*!
- * \internal
- */
- AST_TEST_DEFINE(container_test1_withlock)
- {
- struct ast_format_cap *cap1;
- struct ast_format_cap *cap2;
- switch (cmd) {
- case TEST_INIT:
- info->name = "container_test1_with_locking";
- info->category = "/main/format/";
- info->summary = "Test ast_format and ast_format_cap structures, with locking";
- info->description =
- "This test exercises the Ast Format Capability API by creating "
- "capability structures and performing various API calls on them.";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- cap1 = ast_format_cap_alloc();
- cap2 = ast_format_cap_alloc();
- if (!cap1 || !cap2) {
- ast_test_status_update(test, "cap alloc failed.\n");
- return AST_TEST_FAIL;
- }
- return container_test1_helper(cap1, cap2, test);
- }
- static int container_test2_no_locking_helper(struct ast_format_cap *cap, struct ast_test *test)
- {
- int num = 0;
- struct ast_format tmpformat = { 0, };
- ast_format_cap_add(cap, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0));
- ast_format_cap_add(cap, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0));
- ast_format_cap_add(cap, ast_format_set(&tmpformat, AST_FORMAT_G722, 0));
- ast_format_cap_iter_start(cap);
- while (!ast_format_cap_iter_next(cap, &tmpformat)) {
- num++;
- }
- ast_format_cap_iter_end(cap);
- ast_format_cap_iter_start(cap);
- while (!ast_format_cap_iter_next(cap, &tmpformat)) {
- num++;
- }
- ast_format_cap_iter_end(cap);
- ast_format_cap_destroy(cap);
- ast_test_status_update(test, "%d items iterated over\n", num);
- return (num == 6) ? AST_TEST_PASS : AST_TEST_FAIL;
- }
- /*!
- * \internal
- */
- AST_TEST_DEFINE(container_test2_no_locking)
- {
- struct ast_format_cap *cap;
- switch (cmd) {
- case TEST_INIT:
- info->name = "container_test2_no_locking";
- info->category = "/main/format/";
- info->summary = "Test ast_format_cap iterator, no locking";
- info->description =
- "This test exercises the Ast Capability API iterators.";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- cap = ast_format_cap_alloc_nolock();
- if (!cap) {
- ast_test_status_update(test, "alloc failed\n");
- return AST_TEST_FAIL;
- }
- return container_test2_no_locking_helper(cap, test);
- }
- /*!
- * \internal
- */
- AST_TEST_DEFINE(container_test2_with_locking)
- {
- struct ast_format_cap *cap;
- switch (cmd) {
- case TEST_INIT:
- info->name = "container_test2_with_locking";
- info->category = "/main/format/";
- info->summary = "Test ast_format_cap iterator, with locking";
- info->description =
- "This test exercises the Ast Capability API iterators.";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- cap = ast_format_cap_alloc();
- if (!cap) {
- ast_test_status_update(test, "alloc failed\n");
- return AST_TEST_FAIL;
- }
- return container_test2_no_locking_helper(cap, test);
- }
- static int container_test3_helper(int nolocking, struct ast_test *test)
- {
- int x;
- int res = AST_TEST_PASS;
- struct ast_format_cap *cap1;
- struct ast_format_cap *cap2;
- struct ast_format_cap *joint;
- for (x = 0; x < 2000; x++) {
- if (nolocking) {
- cap1 = ast_format_cap_alloc_nolock();
- cap2 = ast_format_cap_alloc_nolock();
- joint = ast_format_cap_alloc_nolock();
- } else {
- cap1 = ast_format_cap_alloc();
- cap2 = ast_format_cap_alloc();
- joint = ast_format_cap_alloc();
- }
- if (!cap1 || !cap2 || !joint) {
- ast_test_status_update(test, "cap alloc fail\n");
- return AST_TEST_FAIL;
- }
- ast_format_cap_add_all(cap1);
- ast_format_cap_add_all_by_type(cap2, AST_FORMAT_TYPE_AUDIO);
- ast_format_cap_joint_copy(cap1, cap2, joint);
- if (!(ast_format_cap_identical(cap2, joint))) {
- ast_test_status_update(test, "failed identical test\n");
- res = AST_TEST_FAIL;
- cap1 = ast_format_cap_destroy(cap1);
- cap2 = ast_format_cap_destroy(cap2);
- joint = ast_format_cap_destroy(joint);
- break;
- }
- cap1 = ast_format_cap_destroy(cap1);
- cap2 = ast_format_cap_destroy(cap2);
- joint = ast_format_cap_destroy(joint);
- }
- return res;
- }
- /*!
- * \internal
- */
- AST_TEST_DEFINE(container_test3_nolock)
- {
- switch (cmd) {
- case TEST_INIT:
- info->name = "container_test3_no_locking";
- info->category = "/main/format/";
- info->summary = "Load Test ast_format_cap no locking.";
- info->description =
- "This test exercises the Ast Capability API and its iterators for the purpose "
- "of measuring performance.";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- return container_test3_helper(1, test);
- }
- /*!
- * \internal
- */
- AST_TEST_DEFINE(container_test3_withlock)
- {
- switch (cmd) {
- case TEST_INIT:
- info->name = "container_test3_with_locking";
- info->category = "/main/format/";
- info->summary = "Load Test ast_format_cap with locking.";
- info->description =
- "This test exercises the Ast Capability API and its iterators for the purpose "
- "of measuring performance.";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- return container_test3_helper(0, test);
- }
- static int unload_module(void)
- {
- AST_TEST_UNREGISTER(format_test1);
- AST_TEST_UNREGISTER(format_test2);
- AST_TEST_UNREGISTER(container_test1_nolock);
- AST_TEST_UNREGISTER(container_test1_withlock);
- AST_TEST_UNREGISTER(container_test2_no_locking);
- AST_TEST_UNREGISTER(container_test2_with_locking);
- AST_TEST_UNREGISTER(container_test3_nolock);
- AST_TEST_UNREGISTER(container_test3_withlock);
- return 0;
- }
- static int load_module(void)
- {
- AST_TEST_REGISTER(format_test1);
- AST_TEST_REGISTER(format_test2);
- AST_TEST_REGISTER(container_test1_nolock);
- AST_TEST_REGISTER(container_test1_withlock);
- AST_TEST_REGISTER(container_test2_no_locking);
- AST_TEST_REGISTER(container_test2_with_locking);
- AST_TEST_REGISTER(container_test3_nolock);
- AST_TEST_REGISTER(container_test3_withlock);
- return AST_MODULE_LOAD_SUCCESS;
- }
- AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ast_format API Tests");
|