123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479 |
- /*
- * Asterisk -- An open source telephony toolkit.
- *
- * Copyright (C) 2014, Digium, Inc.
- *
- * Joshua Colp <jcolp@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 Format Capabilities API Unit Tests
- *
- * \author Joshua Colp <jcolp@digium.com>
- *
- */
- /*** MODULEINFO
- <depend>TEST_FRAMEWORK</depend>
- <support_level>core</support_level>
- ***/
- #include "asterisk.h"
- ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
- #include "asterisk/test.h"
- #include "asterisk/module.h"
- #include "asterisk/codec.h"
- #include "asterisk/frame.h"
- #include "asterisk/format.h"
- #include "asterisk/format_cap.h"
- AST_TEST_DEFINE(format_cap_alloc)
- {
- struct ast_format_cap *caps;
- switch (cmd) {
- case TEST_INIT:
- info->name = "format_cap_alloc";
- info->category = "/main/format_cap/";
- info->summary = "format capabilities allocation unit test";
- info->description =
- "Test that allocation of a format capabilities structure succeeds";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- }
- ao2_ref(caps, -1);
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(format_cap_append_single)
- {
- RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
- RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, retrieved, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = __PRETTY_FUNCTION__;
- info->category = "/main/format_cap/";
- info->summary = "format capabilities adding unit test";
- info->description =
- "Test that adding a single format to a format capabilities structure succeeds";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- }
- codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
- if (!codec) {
- ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
- return AST_TEST_FAIL;
- }
- format = ast_format_create(codec);
- if (!format) {
- ast_test_status_update(test, "Could not create format using built-in codec\n");
- return AST_TEST_FAIL;
- }
- if (ast_format_cap_append(caps, format, 42)) {
- ast_test_status_update(test, "Could not add newly created format to capabilities structure\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_count(caps) != 1) {
- ast_test_status_update(test, "Number of formats in capabilities structure should be 1 but is %zu\n",
- ast_format_cap_count(caps));
- return AST_TEST_FAIL;
- }
- retrieved = ast_format_cap_get_format(caps, 0);
- if (!retrieved) {
- ast_test_status_update(test, "Attempted to get single format from capabilities structure but got nothing\n");
- return AST_TEST_FAIL;
- } else if (retrieved != format) {
- ast_test_status_update(test, "Retrieved format is not the same as the one we added\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_get_format_framing(caps, retrieved) != 42) {
- ast_test_status_update(test, "Framing for format in capabilities structure does not match what we provided\n");
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(format_cap_append_multiple)
- {
- RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
- RAII_VAR(struct ast_codec *, ulaw, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, ulaw_format, NULL, ao2_cleanup);
- RAII_VAR(struct ast_codec *, alaw, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, alaw_format, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, retrieved, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = __PRETTY_FUNCTION__;
- info->category = "/main/format_cap/";
- info->summary = "format capabilities adding unit test";
- info->description =
- "Test that adding multiple formats to a format capabilities structure succeeds";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- }
- ulaw = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
- if (!ulaw) {
- ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
- return AST_TEST_FAIL;
- }
- ulaw_format = ast_format_create(ulaw);
- if (!ulaw_format) {
- ast_test_status_update(test, "Could not create ulaw format using built-in codec\n");
- return AST_TEST_FAIL;
- }
- alaw = ast_codec_get("alaw", AST_MEDIA_TYPE_AUDIO, 8000);
- if (!alaw) {
- ast_test_status_update(test, "Could not retrieve built-in alaw codec\n");
- return AST_TEST_FAIL;
- }
- alaw_format = ast_format_create(alaw);
- if (!alaw_format) {
- ast_test_status_update(test, "Could not create alaw format using built-in codec\n");
- return AST_TEST_FAIL;
- }
- if (ast_format_cap_append(caps, ulaw_format, 42)) {
- ast_test_status_update(test, "Could not add newly created ulaw format to capabilities structure\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_append(caps, alaw_format, 84)) {
- ast_test_status_update(test, "Could not add newly created alaw format to capabilities structure\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_count(caps) != 2) {
- ast_test_status_update(test, "Number of formats in capabilities structure should be 2 but is %zu\n",
- ast_format_cap_count(caps));
- return AST_TEST_FAIL;
- }
- retrieved = ast_format_cap_get_format(caps, 0);
- if (!retrieved) {
- ast_test_status_update(test, "Attempted to get first format from capabilities structure but got nothing\n");
- return AST_TEST_FAIL;
- } else if (retrieved != ulaw_format) {
- ast_test_status_update(test, "First retrieved format is not the ulaw one we added\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_get_format_framing(caps, retrieved) != 42) {
- ast_test_status_update(test, "Framing for ulaw format in capabilities structure does not match what we provided\n");
- }
- ao2_ref(retrieved, -1);
- retrieved = ast_format_cap_get_format(caps, 1);
- if (!retrieved) {
- ast_test_status_update(test, "Attempted to get second format from capabilities structure but got nothing\n");
- return AST_TEST_FAIL;
- } else if (retrieved != alaw_format) {
- ast_test_status_update(test, "First retrieved format is not the alaw one we added\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_get_format_framing(caps, retrieved) != 84) {
- ast_test_status_update(test, "Framing for alaw format in capabilities structure does not match what we provided\n");
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(format_cap_append_all_unknown)
- {
- RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = __PRETTY_FUNCTION__;
- info->category = "/main/format_cap/";
- info->summary = "format capabilities adding unit test";
- info->description =
- "Test that adding of all formats to a format capabilities structure succeeds";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_append_by_type(caps, AST_MEDIA_TYPE_UNKNOWN)) {
- ast_test_status_update(test, "Failed to add all media formats of all types to capabilities structure\n");
- return AST_TEST_FAIL;
- } else if (!ast_format_cap_has_type(caps, AST_MEDIA_TYPE_AUDIO)) {
- ast_test_status_update(test, "Added all media formats but no audio formats exist when they should\n");
- return AST_TEST_FAIL;
- } else if (!ast_format_cap_has_type(caps, AST_MEDIA_TYPE_VIDEO)) {
- ast_test_status_update(test, "Added all media formats but no video formats exist when they should\n");
- return AST_TEST_FAIL;
- } else if ((ast_format_cap_count(caps) + 1) != (ast_codec_get_max() - 1)) {
- ast_test_status_update(test, "The number of formats in the capabilities structure does not match known number\n");
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(format_cap_append_all_audio)
- {
- RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = __PRETTY_FUNCTION__;
- info->category = "/main/format_cap/";
- info->summary = "format capabilities adding unit test";
- info->description =
- "Test that adding of all audio formats to a format capabilities structure succeeds";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_append_by_type(caps, AST_MEDIA_TYPE_AUDIO)) {
- ast_test_status_update(test, "Failed to add all audio media formats to capabilities structure\n");
- return AST_TEST_FAIL;
- } else if (!ast_format_cap_has_type(caps, AST_MEDIA_TYPE_AUDIO)) {
- ast_test_status_update(test, "Added audio media formats but no audio formats exist when they should\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_has_type(caps, AST_MEDIA_TYPE_VIDEO)) {
- ast_test_status_update(test, "Added only audio media formats but video formats exist when they should not\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_has_type(caps, AST_MEDIA_TYPE_TEXT)) {
- ast_test_status_update(test, "Added only audio media formats but text formats exist when they should not\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_has_type(caps, AST_MEDIA_TYPE_IMAGE)) {
- ast_test_status_update(test, "Added only audio media formats but image formats exist when they should not\n");
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(format_cap_append_duplicate)
- {
- RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
- RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, format_named, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, retrieved, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = __PRETTY_FUNCTION__;
- info->category = "/main/format_cap/";
- info->summary = "format capabilities duplication unit test";
- info->description =
- "Test that adding a single format multiple times to a capabilities structure results in only a single format";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- }
- codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
- if (!codec) {
- ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
- return AST_TEST_FAIL;
- }
- format = ast_format_create(codec);
- if (!format) {
- ast_test_status_update(test, "Could not create format using built-in codec\n");
- return AST_TEST_FAIL;
- }
- format_named = ast_format_create_named("ulaw@20", codec);
- if (!format_named) {
- ast_test_status_update(test, "Could not create named format using built-in codec\n");
- return AST_TEST_FAIL;
- }
- if (ast_format_cap_append(caps, format, 42)) {
- ast_test_status_update(test, "Could not add newly created format to capabilities structure\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_count(caps) != 1) {
- ast_test_status_update(test, "Number of formats in capabilities structure should be 1 but is %zu\n",
- ast_format_cap_count(caps));
- return AST_TEST_FAIL;
- }
- /* Note: regardless of it being a duplicate, ast_format_cap_append should return success */
- if (ast_format_cap_append(caps, format, 0)) {
- ast_test_status_update(test, "Adding of duplicate format to capabilities structure failed\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_count(caps) != 1) {
- ast_test_status_update(test, "Number of formats in capabilities structure should be 1 but is %zu\n",
- ast_format_cap_count(caps));
- return AST_TEST_FAIL;
- }
- if (ast_format_cap_append(caps, format_named, 0)) {
- ast_test_status_update(test, "Adding of duplicate named format to capabilities structure failed\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_count(caps) != 1) {
- ast_test_status_update(test, "Number of formats in capabilities structure should be 1 but is %zu\n",
- ast_format_cap_count(caps));
- return AST_TEST_FAIL;
- }
- retrieved = ast_format_cap_get_format(caps, 0);
- if (!retrieved) {
- ast_test_status_update(test, "Attempted to get single format from capabilities structure but got nothing\n");
- return AST_TEST_FAIL;
- } else if (retrieved != format) {
- ast_test_status_update(test, "Retrieved format is not the same as the one we added\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_get_format_framing(caps, retrieved) != 42) {
- ast_test_status_update(test, "Framing for format in capabilities structure does not match what we provided\n");
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(format_cap_append_from_cap)
- {
- RAII_VAR(struct ast_format_cap *, dst_caps, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format_cap *, src_caps, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = __PRETTY_FUNCTION__;
- info->category = "/main/format_cap/";
- info->summary = "format capabilities append unit test";
- info->description =
- "Test that appending video formats from one capabilities structure to another succeeds";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- dst_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!dst_caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_append_by_type(dst_caps, AST_MEDIA_TYPE_AUDIO)) {
- ast_test_status_update(test, "Failed to add all audio media formats to capabilities structure\n");
- return AST_TEST_FAIL;
- }
- src_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!src_caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_append_by_type(src_caps, AST_MEDIA_TYPE_VIDEO)) {
- ast_test_status_update(test, "Failed to add all video media formats to capabilities structure\n");
- return AST_TEST_FAIL;
- }
- if (ast_format_cap_append_from_cap(dst_caps, src_caps, AST_MEDIA_TYPE_UNKNOWN)) {
- ast_test_status_update(test, "Failed to append formats to capabilities structure\n");
- return AST_TEST_FAIL;
- } else if (!ast_format_cap_has_type(dst_caps, AST_MEDIA_TYPE_AUDIO)) {
- ast_test_status_update(test, "Successfully appended video formats to destination capabilities but it no longer contains audio formats\n");
- return AST_TEST_FAIL;
- } else if (!ast_format_cap_has_type(dst_caps, AST_MEDIA_TYPE_VIDEO)) {
- ast_test_status_update(test, "Successfully appended formats but video formats do not exist in destination capabilities\n");
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(format_cap_append_from_cap_duplicate)
- {
- RAII_VAR(struct ast_format_cap *, dst_caps, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format_cap *, src_caps, NULL, ao2_cleanup);
- unsigned int count;
- unsigned int total_count;
- switch (cmd) {
- case TEST_INIT:
- info->name = __PRETTY_FUNCTION__;
- info->category = "/main/format_cap/";
- info->summary = "format capabilities append duplicate unit test";
- info->description =
- "Test that appending capabilities structures multiple times does not result in duplicate formats";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- dst_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!dst_caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_append_by_type(dst_caps, AST_MEDIA_TYPE_AUDIO)) {
- ast_test_status_update(test, "Failed to add all audio media formats to capabilities structure\n");
- return AST_TEST_FAIL;
- }
- src_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!src_caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_append_by_type(src_caps, AST_MEDIA_TYPE_VIDEO)) {
- ast_test_status_update(test, "Failed to add all video media formats to capabilities structure\n");
- return AST_TEST_FAIL;
- }
- total_count = ast_format_cap_count(src_caps) + ast_format_cap_count(dst_caps);
- if (ast_format_cap_append_from_cap(dst_caps, src_caps, AST_MEDIA_TYPE_UNKNOWN)) {
- ast_test_status_update(test, "Failed to append formats to capabilities structure\n");
- return AST_TEST_FAIL;
- } else if (!ast_format_cap_has_type(dst_caps, AST_MEDIA_TYPE_AUDIO)) {
- ast_test_status_update(test, "Successfully appended video formats to destination capabilities but it no longer contains audio formats\n");
- return AST_TEST_FAIL;
- } else if (!ast_format_cap_has_type(dst_caps, AST_MEDIA_TYPE_VIDEO)) {
- ast_test_status_update(test, "Successfully appended formats but video formats do not exist in destination capabilities\n");
- return AST_TEST_FAIL;
- }
- count = ast_format_cap_count(dst_caps);
- if (ast_format_cap_append_from_cap(dst_caps, src_caps, AST_MEDIA_TYPE_UNKNOWN)) {
- ast_test_status_update(test, "Failed to append duplicate formats to capabilities structure\n");
- return AST_TEST_FAIL;
- }
- ast_test_validate(test, count == ast_format_cap_count(dst_caps));
- ast_test_validate(test, count == total_count);
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(format_cap_set_framing)
- {
- RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
- RAII_VAR(struct ast_codec *, ulaw, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, ulaw_format, NULL, ao2_cleanup);
- RAII_VAR(struct ast_codec *, alaw, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, alaw_format, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = "format_cap_set_framing";
- info->category = "/main/format_cap/";
- info->summary = "format capabilities framing unit test";
- info->description =
- "Test that global framing on a format capabilities structure is used when it should be";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- }
- ast_format_cap_set_framing(caps, 160);
- ast_test_validate(test, ast_format_cap_get_framing(caps) == 160);
- ulaw = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
- if (!ulaw) {
- ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
- return AST_TEST_FAIL;
- }
- ulaw_format = ast_format_create(ulaw);
- if (!ulaw_format) {
- ast_test_status_update(test, "Could not create ulaw format using built-in codec\n");
- return AST_TEST_FAIL;
- }
- alaw = ast_codec_get("alaw", AST_MEDIA_TYPE_AUDIO, 8000);
- if (!alaw) {
- ast_test_status_update(test, "Could not retrieve built-in alaw codec\n");
- return AST_TEST_FAIL;
- }
- alaw_format = ast_format_create(alaw);
- if (!alaw_format) {
- ast_test_status_update(test, "Could not create alaw format using built-in codec\n");
- return AST_TEST_FAIL;
- }
- if (ast_format_cap_append(caps, ulaw_format, 42)) {
- ast_test_status_update(test, "Could not add newly created ulaw format to capabilities structure\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_append(caps, alaw_format, 0)) {
- ast_test_status_update(test, "Could not add newly created alaw format to capabilities structure\n");
- return AST_TEST_FAIL;
- }
- if (ast_format_cap_get_format_framing(caps, ulaw_format) != 42) {
- ast_test_status_update(test, "Added ulaw format to capabilities structure with explicit framing but did not get it back\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_get_format_framing(caps, alaw_format) != ast_format_get_default_ms(alaw_format)) {
- ast_test_status_update(test, "Added alaw format to capabilities structure with no explicit framing but did not get global back\n");
- return AST_TEST_FAIL;
- }
- ast_test_validate(test, ast_format_cap_get_framing(caps) == ast_format_get_default_ms(alaw_format));
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(format_cap_remove_single)
- {
- RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
- RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = "format_cap_remove_single";
- info->category = "/main/format_cap/";
- info->summary = "format capabilities removal unit test";
- info->description =
- "Test that removing a single format from a format capabilities structure succeeds";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- }
- codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
- if (!codec) {
- ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
- return AST_TEST_FAIL;
- }
- format = ast_format_create(codec);
- if (!format) {
- ast_test_status_update(test, "Could not create format using built-in codec\n");
- return AST_TEST_FAIL;
- }
- if (ast_format_cap_append(caps, format, 42)) {
- ast_test_status_update(test, "Could not add newly created format to capabilities structure\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_remove(caps, format)) {
- ast_test_status_update(test, "Could not remove format that was just added to capabilities structure\n");
- return AST_TEST_FAIL;
- } else if (!ast_format_cap_remove(caps, format)) {
- ast_test_status_update(test, "Successfully removed a format twice from the capabilities structure\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_count(caps)) {
- ast_test_status_update(test, "Capabilities structure should be empty but instead it contains '%zu' formats\n",
- ast_format_cap_count(caps));
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(format_cap_remove_multiple)
- {
- RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
- RAII_VAR(struct ast_codec *, ulaw, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, ulaw_format, NULL, ao2_cleanup);
- RAII_VAR(struct ast_codec *, alaw, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, alaw_format, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, retrieved, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = "format_cap_remove_multiple";
- info->category = "/main/format_cap/";
- info->summary = "format capabilities removal unit test";
- info->description =
- "Test that removing a format from a format capabilities structure containing multiple formats succeeds";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- }
- ulaw = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
- if (!ulaw) {
- ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
- return AST_TEST_FAIL;
- }
- ulaw_format = ast_format_create(ulaw);
- if (!ulaw_format) {
- ast_test_status_update(test, "Could not create ulaw format using built-in codec\n");
- return AST_TEST_FAIL;
- }
- alaw = ast_codec_get("alaw", AST_MEDIA_TYPE_AUDIO, 8000);
- if (!alaw) {
- ast_test_status_update(test, "Could not retrieve built-in alaw codec\n");
- return AST_TEST_FAIL;
- }
- alaw_format = ast_format_create(alaw);
- if (!alaw_format) {
- ast_test_status_update(test, "Could not create alaw format using built-in codec\n");
- return AST_TEST_FAIL;
- }
- if (ast_format_cap_append(caps, ulaw_format, 42)) {
- ast_test_status_update(test, "Could not add newly created ulaw format to capabilities structure\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_append(caps, alaw_format, 84)) {
- ast_test_status_update(test, "Could not add newly created alaw format to capabilities structure\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_remove(caps, ulaw_format)) {
- ast_test_status_update(test, "Could not remove the ulaw format we just added to capabilities structure\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_count(caps) != 1) {
- ast_test_status_update(test, "Capabilities structure should contain 1 format but it contains '%zu'\n",
- ast_format_cap_count(caps));
- return AST_TEST_FAIL;
- }
- retrieved = ast_format_cap_get_format(caps, 0);
- if (!retrieved) {
- ast_test_status_update(test, "Attempted to get first format from capabilities structure but got nothing\n");
- return AST_TEST_FAIL;
- } else if (retrieved != alaw_format) {
- ast_test_status_update(test, "First retrieved format is not the alaw one we added\n");
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(format_cap_remove_bytype)
- {
- RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = "format_cap_remove_bytype";
- info->category = "/main/format_cap/";
- info->summary = "format capabilities removal unit test";
- info->description =
- "Test that removal of a specific type of format from a format capabilities structure succeeds";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_append_by_type(caps, AST_MEDIA_TYPE_UNKNOWN)) {
- ast_test_status_update(test, "Failed to add all media formats of all types to capabilities structure\n");
- return AST_TEST_FAIL;
- }
- ast_format_cap_remove_by_type(caps, AST_MEDIA_TYPE_AUDIO);
- if (ast_format_cap_has_type(caps, AST_MEDIA_TYPE_AUDIO)) {
- ast_test_status_update(test, "Removed all audio type formats from capabilities structure but some remain\n");
- return AST_TEST_FAIL;
- } else if (!ast_format_cap_has_type(caps, AST_MEDIA_TYPE_VIDEO)) {
- ast_test_status_update(test, "Removed audio type formats from capabilities structure but video are gone as well\n");
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(format_cap_remove_all)
- {
- RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = "format_cap_remove_all";
- info->category = "/main/format_cap/";
- info->summary = "format capabilities removal unit test";
- info->description =
- "Test that removal of all formats from a format capabilities structure succeeds";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_append_by_type(caps, AST_MEDIA_TYPE_UNKNOWN)) {
- ast_test_status_update(test, "Failed to add all media formats of all types to capabilities structure\n");
- return AST_TEST_FAIL;
- }
- ast_format_cap_remove_by_type(caps, AST_MEDIA_TYPE_UNKNOWN);
- if (ast_format_cap_count(caps)) {
- ast_test_status_update(test, "Removed all formats from capabilities structure but some remain\n");
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(format_cap_get_compatible_format)
- {
- RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
- RAII_VAR(struct ast_codec *, ulaw, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, ulaw_format, NULL, ao2_cleanup);
- RAII_VAR(struct ast_codec *, alaw, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, alaw_format, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, compatible, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = "format_cap_get_compatible_format";
- info->category = "/main/format_cap/";
- info->summary = "format capabilities negotiation unit test";
- info->description =
- "Test that getting a compatible format from a capabilities structure succeeds";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- }
- ulaw = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
- if (!ulaw) {
- ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
- return AST_TEST_FAIL;
- }
- ulaw_format = ast_format_create(ulaw);
- if (!ulaw_format) {
- ast_test_status_update(test, "Could not create ulaw format using built-in codec\n");
- return AST_TEST_FAIL;
- }
- alaw = ast_codec_get("alaw", AST_MEDIA_TYPE_AUDIO, 8000);
- if (!alaw) {
- ast_test_status_update(test, "Could not retrieve built-in alaw codec\n");
- return AST_TEST_FAIL;
- }
- alaw_format = ast_format_create(alaw);
- if (!alaw_format) {
- ast_test_status_update(test, "Could not create alaw format using built-in codec\n");
- return AST_TEST_FAIL;
- }
- if (ast_format_cap_append(caps, ulaw_format, 42)) {
- ast_test_status_update(test, "Could not add newly created ulaw format to capabilities structure\n");
- return AST_TEST_FAIL;
- }
- compatible = ast_format_cap_get_compatible_format(caps, alaw_format);
- if (compatible) {
- ast_test_status_update(test, "Retrieved a compatible format from capabilities structure when none should exist\n");
- return AST_TEST_FAIL;
- }
- compatible = ast_format_cap_get_compatible_format(caps, ulaw_format);
- if (!compatible) {
- ast_test_status_update(test, "Did not retrieve a compatible format from capabilities structure when there should be one\n");
- return AST_TEST_FAIL;
- } else if (compatible != ulaw_format) {
- ast_test_status_update(test, "Compatible format is not the format we added to the capabilities structure\n");
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(format_cap_iscompatible_format)
- {
- RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
- RAII_VAR(struct ast_codec *, ulaw, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, ulaw_format, NULL, ao2_cleanup);
- RAII_VAR(struct ast_codec *, alaw, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, alaw_format, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = "format_cap_iscompatible_format";
- info->category = "/main/format_cap/";
- info->summary = "format capabilities negotiation unit test";
- info->description =
- "Test that checking whether a format is compatible with a capabilities structure succeeds";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- }
- ulaw = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
- if (!ulaw) {
- ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
- return AST_TEST_FAIL;
- }
- ulaw_format = ast_format_create(ulaw);
- if (!ulaw_format) {
- ast_test_status_update(test, "Could not create ulaw format using built-in codec\n");
- return AST_TEST_FAIL;
- }
- alaw = ast_codec_get("alaw", AST_MEDIA_TYPE_AUDIO, 8000);
- if (!alaw) {
- ast_test_status_update(test, "Could not retrieve built-in alaw codec\n");
- return AST_TEST_FAIL;
- }
- alaw_format = ast_format_create(alaw);
- if (!alaw_format) {
- ast_test_status_update(test, "Could not create alaw format using built-in codec\n");
- return AST_TEST_FAIL;
- }
- if (ast_format_cap_append(caps, ulaw_format, 42)) {
- ast_test_status_update(test, "Could not add newly created ulaw format to capabilities structure\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_iscompatible_format(caps, alaw_format) != AST_FORMAT_CMP_NOT_EQUAL) {
- ast_test_status_update(test, "Alaw format is compatible with capabilities structure when it only contains ulaw\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_iscompatible_format(caps, ulaw_format) == AST_FORMAT_CMP_NOT_EQUAL) {
- ast_test_status_update(test, "Ulaw format is not compatible with capabilities structure when it should be\n");
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(format_cap_get_compatible)
- {
- RAII_VAR(struct ast_format_cap *, alaw_caps, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format_cap *, ulaw_caps, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format_cap *, compatible_caps, NULL, ao2_cleanup);
- RAII_VAR(struct ast_codec *, ulaw, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, ulaw_format, NULL, ao2_cleanup);
- RAII_VAR(struct ast_codec *, alaw, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, alaw_format, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = "format_cap_get_compatible";
- info->category = "/main/format_cap/";
- info->summary = "format capabilities negotiation unit test";
- info->description =
- "Test that getting the compatible formats between two capabilities structures succeeds";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- alaw_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!alaw_caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- }
- ulaw_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!ulaw_caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- }
- compatible_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!compatible_caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- }
- ulaw = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
- if (!ulaw) {
- ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
- return AST_TEST_FAIL;
- }
- ulaw_format = ast_format_create(ulaw);
- if (!ulaw_format) {
- ast_test_status_update(test, "Could not create ulaw format using built-in codec\n");
- return AST_TEST_FAIL;
- }
- alaw = ast_codec_get("alaw", AST_MEDIA_TYPE_AUDIO, 8000);
- if (!alaw) {
- ast_test_status_update(test, "Could not retrieve built-in alaw codec\n");
- return AST_TEST_FAIL;
- }
- alaw_format = ast_format_create(alaw);
- if (!alaw_format) {
- ast_test_status_update(test, "Could not create alaw format using built-in codec\n");
- return AST_TEST_FAIL;
- }
- if (ast_format_cap_append(ulaw_caps, ulaw_format, 0)) {
- ast_test_status_update(test, "Could not add ulaw format to ulaw capabilities\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_append(alaw_caps, alaw_format, 0)) {
- ast_test_status_update(test, "Could not add alaw format to alaw capabilities\n");
- return AST_TEST_FAIL;
- }
- ast_format_cap_get_compatible(ulaw_caps, alaw_caps, compatible_caps);
- if (ast_format_cap_count(compatible_caps)) {
- ast_test_status_update(test, "A compatible format exists when none should\n");
- return AST_TEST_FAIL;
- }
- ast_format_cap_get_compatible(ulaw_caps, ulaw_caps, compatible_caps);
- if (!ast_format_cap_count(compatible_caps)) {
- ast_test_status_update(test, "No compatible formats exist when 1 should\n");
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(format_cap_iscompatible)
- {
- RAII_VAR(struct ast_format_cap *, alaw_caps, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format_cap *, ulaw_caps, NULL, ao2_cleanup);
- RAII_VAR(struct ast_codec *, ulaw, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, ulaw_format, NULL, ao2_cleanup);
- RAII_VAR(struct ast_codec *, alaw, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, alaw_format, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = "format_cap_iscompatible";
- info->category = "/main/format_cap/";
- info->summary = "format capabilities negotiation unit test";
- info->description =
- "Test that checking if there are compatible formats between two capabilities structures succeeds";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- alaw_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!alaw_caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- }
- ulaw_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!ulaw_caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- }
- ulaw = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
- if (!ulaw) {
- ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
- return AST_TEST_FAIL;
- }
- ulaw_format = ast_format_create(ulaw);
- if (!ulaw_format) {
- ast_test_status_update(test, "Could not create ulaw format using built-in codec\n");
- return AST_TEST_FAIL;
- }
- alaw = ast_codec_get("alaw", AST_MEDIA_TYPE_AUDIO, 8000);
- if (!alaw) {
- ast_test_status_update(test, "Could not retrieve built-in alaw codec\n");
- return AST_TEST_FAIL;
- }
- alaw_format = ast_format_create(alaw);
- if (!alaw_format) {
- ast_test_status_update(test, "Could not create alaw format using built-in codec\n");
- return AST_TEST_FAIL;
- }
- if (ast_format_cap_append(ulaw_caps, ulaw_format, 0)) {
- ast_test_status_update(test, "Could not add ulaw format to ulaw capabilities\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_append(alaw_caps, alaw_format, 0)) {
- ast_test_status_update(test, "Could not add alaw format to alaw capabilities\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_iscompatible(ulaw_caps, alaw_caps)) {
- ast_test_status_update(test, "Two capability structures that should not be compatible are\n");
- return AST_TEST_FAIL;
- } else if (!ast_format_cap_iscompatible(ulaw_caps, ulaw_caps)) {
- ast_test_status_update(test, "Capability structure is not compatible with itself\n");
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(format_cap_get_names)
- {
- RAII_VAR(struct ast_format_cap *, empty_caps, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format_cap *, multi_caps, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format_cap *, alaw_caps, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format_cap *, ulaw_caps, NULL, ao2_cleanup);
- RAII_VAR(struct ast_codec *, ulaw, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, ulaw_format, NULL, ao2_cleanup);
- RAII_VAR(struct ast_codec *, alaw, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, alaw_format, NULL, ao2_cleanup);
- struct ast_str *buffer = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
- switch (cmd) {
- case TEST_INIT:
- info->name = "format_cap_get_names";
- info->category = "/main/format_cap/";
- info->summary = "Test getting the names of formats";
- info->description =
- "Test that obtaining the names from a format capabilities structure\n"
- "produces the expected output.";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- empty_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!empty_caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- }
- multi_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!multi_caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- }
- alaw_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!alaw_caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- }
- ulaw_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!ulaw_caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- }
- ulaw = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
- if (!ulaw) {
- ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
- return AST_TEST_FAIL;
- }
- ulaw_format = ast_format_create(ulaw);
- if (!ulaw_format) {
- ast_test_status_update(test, "Could not create ulaw format using built-in codec\n");
- return AST_TEST_FAIL;
- }
- alaw = ast_codec_get("alaw", AST_MEDIA_TYPE_AUDIO, 8000);
- if (!alaw) {
- ast_test_status_update(test, "Could not retrieve built-in alaw codec\n");
- return AST_TEST_FAIL;
- }
- alaw_format = ast_format_create(alaw);
- if (!alaw_format) {
- ast_test_status_update(test, "Could not create alaw format using built-in codec\n");
- return AST_TEST_FAIL;
- }
- if (ast_format_cap_append(ulaw_caps, ulaw_format, 0)) {
- ast_test_status_update(test, "Could not add ulaw format to ulaw capabilities\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_append(alaw_caps, alaw_format, 0)) {
- ast_test_status_update(test, "Could not add alaw format to alaw capabilities\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_append(multi_caps, ulaw_format, 0)) {
- ast_test_status_update(test, "Could not add ulaw format to multi capabilities\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_append(multi_caps, alaw_format, 0)) {
- ast_test_status_update(test, "Could not add alaw format to multi capabilities\n");
- return AST_TEST_FAIL;
- }
- ast_format_cap_get_names(empty_caps, &buffer);
- ast_test_validate(test, !strcmp(ast_str_buffer(buffer), "(nothing)"));
- ast_format_cap_get_names(ulaw_caps, &buffer);
- ast_test_validate(test, !strcmp(ast_str_buffer(buffer), "(ulaw)"));
- ast_format_cap_get_names(alaw_caps, &buffer);
- ast_test_validate(test, !strcmp(ast_str_buffer(buffer), "(alaw)"));
- ast_format_cap_get_names(multi_caps, &buffer);
- ast_test_validate(test, !strcmp(ast_str_buffer(buffer), "(ulaw|alaw)"));
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(format_cap_best_by_type)
- {
- RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
- RAII_VAR(struct ast_codec *, ulaw, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, ulaw_format, NULL, ao2_cleanup);
- RAII_VAR(struct ast_codec *, alaw, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, alaw_format, NULL, ao2_cleanup);
- RAII_VAR(struct ast_codec *, h263, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, h263_format, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, best_format, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = __PRETTY_FUNCTION__;
- info->category = "/main/format_cap/";
- info->summary = "format capabilities best by type unit test";
- info->description =
- "Test that we can get the best format type out of a capabilities structure";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- }
- ulaw = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
- if (!ulaw) {
- ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
- return AST_TEST_FAIL;
- }
- ulaw_format = ast_format_create(ulaw);
- if (!ulaw_format) {
- ast_test_status_update(test, "Could not create ulaw format using built-in codec\n");
- return AST_TEST_FAIL;
- }
- alaw = ast_codec_get("alaw", AST_MEDIA_TYPE_AUDIO, 8000);
- if (!alaw) {
- ast_test_status_update(test, "Could not retrieve built-in alaw codec\n");
- return AST_TEST_FAIL;
- }
- alaw_format = ast_format_create(alaw);
- if (!alaw_format) {
- ast_test_status_update(test, "Could not create alaw format using built-in codec\n");
- return AST_TEST_FAIL;
- }
- h263 = ast_codec_get("h263", AST_MEDIA_TYPE_VIDEO, 0);
- if (!h263) {
- ast_test_status_update(test, "Could not retrieve built-in h263 codec\n");
- return AST_TEST_FAIL;
- }
- h263_format = ast_format_create(h263);
- if (!alaw_format) {
- ast_test_status_update(test, "Could not create h263 format using built-in codec\n");
- return AST_TEST_FAIL;
- }
- if (ast_format_cap_append(caps, ulaw_format, 0)) {
- ast_test_status_update(test, "Could not add ulaw format to capabilities\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_append(caps, alaw_format, 0)) {
- ast_test_status_update(test, "Could not add alaw format to capabilities\n");
- return AST_TEST_FAIL;
- } else if (ast_format_cap_append(caps, h263_format, 0)) {
- ast_test_status_update(test, "Could not add h263 format to capabilities\n");
- return AST_TEST_FAIL;
- }
- best_format = ast_format_cap_get_best_by_type(caps, AST_MEDIA_TYPE_UNKNOWN);
- ast_test_validate(test, ast_format_cmp(best_format, ulaw_format) == AST_FORMAT_CMP_EQUAL);
- ao2_ref(best_format, -1);
- best_format = ast_format_cap_get_best_by_type(caps, AST_MEDIA_TYPE_AUDIO);
- ast_test_validate(test, ast_format_cmp(best_format, ulaw_format) == AST_FORMAT_CMP_EQUAL);
- ao2_ref(best_format, -1);
- best_format = ast_format_cap_get_best_by_type(caps, AST_MEDIA_TYPE_VIDEO);
- ast_test_validate(test, ast_format_cmp(best_format, h263_format) == AST_FORMAT_CMP_EQUAL);
- ao2_ref(best_format, -1);
- best_format = ast_format_cap_get_best_by_type(caps, AST_MEDIA_TYPE_IMAGE);
- ast_test_validate(test, best_format == NULL);
- best_format = ast_format_cap_get_best_by_type(caps, AST_MEDIA_TYPE_TEXT);
- ast_test_validate(test, best_format == NULL);
- return AST_TEST_PASS;
- }
- static int test_law_samples(struct ast_frame *frame)
- {
- return frame->datalen;
- }
- static int test_law_length(unsigned int samples)
- {
- return samples;
- }
- static struct ast_codec test_law = {
- .name = "test_law",
- .description = "format cap unit test codec",
- .type = AST_MEDIA_TYPE_AUDIO,
- .sample_rate = 8000,
- .minimum_ms = 10,
- .maximum_ms = 150,
- .default_ms = 20,
- .samples_count = test_law_samples,
- .get_length = test_law_length,
- .smooth = 1,
- };
- static enum ast_format_cmp_res test_law_cmp(const struct ast_format *format1, const struct ast_format *format2)
- {
- ast_log(LOG_ERROR, "Comparing format1 %p and format2 %p\n", format1, format2);
- return format1 == format2 ? AST_FORMAT_CMP_EQUAL : AST_FORMAT_CMP_NOT_EQUAL;
- }
- static void test_law_destroy(struct ast_format *format)
- {
- }
- static int test_law_clone(const struct ast_format *src, struct ast_format *dst)
- {
- return 0;
- }
- static struct ast_format_interface test_law_interface = {
- .format_cmp = test_law_cmp,
- .format_clone = test_law_clone,
- .format_destroy = test_law_destroy,
- };
- AST_TEST_DEFINE(format_cap_replace_from_cap)
- {
- RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format_cap *, replace_caps, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format_cap *, result_caps, NULL, ao2_cleanup);
- RAII_VAR(struct ast_codec *, ulaw, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, ulaw_format, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, ulaw_format_variant, NULL, ao2_cleanup);
- RAII_VAR(struct ast_codec *, alaw, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format *, alaw_format, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = __PRETTY_FUNCTION__;
- info->category = "/main/format_cap/";
- info->summary = "format capabilities adding unit test";
- info->description =
- "Test that adding multiple formats to a format capabilities structure succeeds";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- replace_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- result_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!caps || !replace_caps || !result_caps) {
- ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
- return AST_TEST_FAIL;
- }
- ulaw = ast_codec_get("test_law", AST_MEDIA_TYPE_AUDIO, 8000);
- if (!ulaw) {
- ast_test_status_update(test, "Could not retrieve test_law codec\n");
- return AST_TEST_FAIL;
- }
- ulaw_format = ast_format_create(ulaw);
- if (!ulaw_format) {
- ast_test_status_update(test, "Could not create ulaw format using built-in codec\n");
- return AST_TEST_FAIL;
- }
- ulaw_format_variant = ast_format_create(ulaw);
- if (!ulaw_format_variant) {
- ast_test_status_update(test, "Could not create ulaw format variant using built-in codec\n");
- return AST_TEST_FAIL;
- }
- alaw = ast_codec_get("alaw", AST_MEDIA_TYPE_AUDIO, 8000);
- if (!alaw) {
- ast_test_status_update(test, "Could not retrieve built-in alaw codec\n");
- return AST_TEST_FAIL;
- }
- alaw_format = ast_format_create(alaw);
- if (!alaw_format) {
- ast_test_status_update(test, "Could not create alaw format using built-in codec\n");
- return AST_TEST_FAIL;
- }
- /* fill caps with ulaw and alaw */
- if (ast_format_cap_append(caps, ulaw_format, 42)) {
- ast_test_status_update(test, "Could not add ulaw format to capabilities structure\n");
- return AST_TEST_FAIL;
- }
- if (ast_format_cap_append(caps, alaw_format, 84)) {
- ast_test_status_update(test, "Could not add alaw format to capabilities structure\n");
- return AST_TEST_FAIL;
- }
- if (ast_format_cap_count(caps) != 2) {
- ast_test_status_update(test, "Number of formats in capabilities structure should be 2 but is %zu\n",
- ast_format_cap_count(caps));
- return AST_TEST_FAIL;
- }
- /* fill replace_caps with the ulaw variant */
- if (ast_format_cap_append(replace_caps, ulaw_format_variant, 42)) {
- ast_test_status_update(test, "Could not add ulaw format to capabilities structure\n");
- return AST_TEST_FAIL;
- }
- if (ast_format_cap_count(replace_caps) != 1) {
- ast_test_status_update(test, "Number of formats in capabilities structure should be 1 but is %zu\n",
- ast_format_cap_count(replace_caps));
- return AST_TEST_FAIL;
- }
- /* fill result_caps with ulaw_variant and alaw */
- if (ast_format_cap_append(result_caps, ulaw_format_variant, 42)) {
- ast_test_status_update(test, "Could not add ulaw variant to capabilities structure\n");
- return AST_TEST_FAIL;
- }
- if (ast_format_cap_append(result_caps, alaw_format, 84)) {
- ast_test_status_update(test, "Could not add alaw format to capabilities structure\n");
- return AST_TEST_FAIL;
- }
- if (ast_format_cap_count(result_caps) != 2) {
- ast_test_status_update(test, "Number of formats in capabilities structure should be 2 but is %zu\n",
- ast_format_cap_count(result_caps));
- return AST_TEST_FAIL;
- }
- /* replace caps formats from replace_caps */
- ast_format_cap_replace_from_cap(caps, replace_caps, AST_MEDIA_TYPE_UNKNOWN);
- /* compare result_caps with caps */
- if (!ast_format_cap_identical(caps, result_caps)) {
- ast_test_status_update(test, "Actual and expected result caps differ\n");
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- static int unload_module(void)
- {
- AST_TEST_UNREGISTER(format_cap_alloc);
- AST_TEST_UNREGISTER(format_cap_append_single);
- AST_TEST_UNREGISTER(format_cap_append_multiple);
- AST_TEST_UNREGISTER(format_cap_append_all_unknown);
- AST_TEST_UNREGISTER(format_cap_append_all_audio);
- AST_TEST_UNREGISTER(format_cap_append_duplicate);
- AST_TEST_UNREGISTER(format_cap_append_from_cap);
- AST_TEST_UNREGISTER(format_cap_append_from_cap_duplicate);
- AST_TEST_UNREGISTER(format_cap_set_framing);
- AST_TEST_UNREGISTER(format_cap_remove_single);
- AST_TEST_UNREGISTER(format_cap_remove_multiple);
- AST_TEST_UNREGISTER(format_cap_remove_bytype);
- AST_TEST_UNREGISTER(format_cap_remove_all);
- AST_TEST_UNREGISTER(format_cap_get_names);
- AST_TEST_UNREGISTER(format_cap_get_compatible_format);
- AST_TEST_UNREGISTER(format_cap_iscompatible_format);
- AST_TEST_UNREGISTER(format_cap_get_compatible);
- AST_TEST_UNREGISTER(format_cap_iscompatible);
- AST_TEST_UNREGISTER(format_cap_best_by_type);
- AST_TEST_UNREGISTER(format_cap_replace_from_cap);
- return 0;
- }
- static int load_module(void)
- {
- AST_TEST_REGISTER(format_cap_alloc);
- AST_TEST_REGISTER(format_cap_append_single);
- AST_TEST_REGISTER(format_cap_append_multiple);
- AST_TEST_REGISTER(format_cap_append_all_unknown);
- AST_TEST_REGISTER(format_cap_append_all_audio);
- AST_TEST_REGISTER(format_cap_append_duplicate);
- AST_TEST_REGISTER(format_cap_append_from_cap);
- AST_TEST_REGISTER(format_cap_append_from_cap_duplicate);
- AST_TEST_REGISTER(format_cap_set_framing);
- AST_TEST_REGISTER(format_cap_remove_single);
- AST_TEST_REGISTER(format_cap_remove_multiple);
- AST_TEST_REGISTER(format_cap_remove_bytype);
- AST_TEST_REGISTER(format_cap_remove_all);
- AST_TEST_REGISTER(format_cap_get_names);
- AST_TEST_REGISTER(format_cap_get_compatible_format);
- AST_TEST_REGISTER(format_cap_iscompatible_format);
- AST_TEST_REGISTER(format_cap_get_compatible);
- AST_TEST_REGISTER(format_cap_iscompatible);
- AST_TEST_REGISTER(format_cap_best_by_type);
- AST_TEST_REGISTER(format_cap_replace_from_cap);
- ast_codec_register(&test_law);
- ast_format_interface_register("test_law", &test_law_interface);
- return AST_MODULE_LOAD_SUCCESS;
- }
- AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Format capabilities API test module");
|