123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873 |
- /*
- * Asterisk -- An open source telephony toolkit.
- *
- * Copyright (C) 2013, 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 Bucket 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__, "")
- #include <sys/stat.h>
- #include "asterisk/test.h"
- #include "asterisk/module.h"
- #include "asterisk/bucket.h"
- #include "asterisk/logger.h"
- #include "asterisk/json.h"
- #include "asterisk/file.h"
- /*! \brief Test state structure for scheme wizards */
- struct bucket_test_state {
- /*! \brief Whether the object has been created or not */
- unsigned int created:1;
- /*! \brief Whether the object has been updated or not */
- unsigned int updated:1;
- /*! \brief Whether the object has been deleted or not */
- unsigned int deleted:1;
- };
- /*! \brief Global scope structure for testing bucket wizards */
- static struct bucket_test_state bucket_test_wizard_state;
- static void bucket_test_wizard_clear(void)
- {
- bucket_test_wizard_state.created = 0;
- bucket_test_wizard_state.updated = 0;
- bucket_test_wizard_state.deleted = 0;
- }
- static int bucket_test_wizard_create(const struct ast_sorcery *sorcery, void *data, void *object)
- {
- if (bucket_test_wizard_state.created) {
- return -1;
- }
- bucket_test_wizard_state.created = 1;
- return 0;
- }
- static int bucket_test_wizard_update(const struct ast_sorcery *sorcery, void *data, void *object)
- {
- if (bucket_test_wizard_state.updated) {
- return -1;
- }
- bucket_test_wizard_state.updated = 1;
- return 0;
- }
- static void *bucket_test_wizard_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type,
- const char *id)
- {
- if (!strcmp(type, "bucket")) {
- return ast_bucket_alloc(id);
- } else if (!strcmp(type, "file")) {
- return ast_bucket_file_alloc(id);
- } else {
- return NULL;
- }
- }
- static int bucket_test_wizard_delete(const struct ast_sorcery *sorcery, void *data, void *object)
- {
- if (bucket_test_wizard_state.deleted) {
- return -1;
- }
- bucket_test_wizard_state.deleted = 1;
- return 0;
- }
- static struct ast_sorcery_wizard bucket_test_wizard = {
- .name = "test",
- .create = bucket_test_wizard_create,
- .retrieve_id = bucket_test_wizard_retrieve_id,
- .delete = bucket_test_wizard_delete,
- };
- static struct ast_sorcery_wizard bucket_file_test_wizard = {
- .name = "test",
- .create = bucket_test_wizard_create,
- .update = bucket_test_wizard_update,
- .retrieve_id = bucket_test_wizard_retrieve_id,
- .delete = bucket_test_wizard_delete,
- };
- AST_TEST_DEFINE(bucket_scheme_register)
- {
- switch (cmd) {
- case TEST_INIT:
- info->name = "bucket_scheme_register_unregister";
- info->category = "/main/bucket/";
- info->summary = "bucket scheme registration/unregistration unit test";
- info->description =
- "Test registration and unregistration of bucket scheme";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- if (!ast_bucket_scheme_register("", NULL, NULL, NULL, NULL)) {
- ast_test_status_update(test, "Successfully registered a Bucket scheme without name or wizards\n");
- return AST_TEST_FAIL;
- }
- if (!ast_bucket_scheme_register("test", &bucket_test_wizard, &bucket_file_test_wizard, NULL, NULL)) {
- ast_test_status_update(test, "Successfully registered a Bucket scheme twice\n");
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(bucket_alloc)
- {
- RAII_VAR(struct ast_bucket *, bucket, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = "bucket_alloc";
- info->category = "/main/bucket/";
- info->summary = "bucket allocation unit test";
- info->description =
- "Test allocation of buckets";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- if ((bucket = ast_bucket_alloc(""))) {
- ast_test_status_update(test, "Allocated a bucket with no URI provided\n");
- return AST_TEST_FAIL;
- }
- if (!(bucket = ast_bucket_alloc("test:///tmp/bob"))) {
- ast_test_status_update(test, "Failed to allocate bucket\n");
- return AST_TEST_FAIL;
- }
- if (strcmp(ast_sorcery_object_get_id(bucket), "test:///tmp/bob")) {
- ast_test_status_update(test, "URI within allocated bucket is '%s' and should be test:///tmp/bob\n",
- ast_sorcery_object_get_id(bucket));
- return AST_TEST_FAIL;
- }
- if (strcmp(bucket->scheme, "test")) {
- ast_test_status_update(test, "Scheme within allocated bucket is '%s' and should be test\n",
- bucket->scheme);
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(bucket_create)
- {
- RAII_VAR(struct ast_bucket *, bucket, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = "bucket_create";
- info->category = "/main/bucket/";
- info->summary = "bucket creation unit test";
- info->description =
- "Test creation of buckets";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- if (!(bucket = ast_bucket_alloc("test:///tmp/bob"))) {
- ast_test_status_update(test, "Failed to allocate bucket\n");
- return AST_TEST_FAIL;
- }
- bucket_test_wizard_clear();
- if (ast_bucket_create(bucket)) {
- ast_test_status_update(test, "Failed to create bucket with URI '%s'\n",
- ast_sorcery_object_get_id(bucket));
- return AST_TEST_FAIL;
- }
- if (!bucket_test_wizard_state.created) {
- ast_test_status_update(test, "Bucket creation returned success but scheme implementation never actually created it\n");
- return AST_TEST_FAIL;
- }
- if (!ast_bucket_create(bucket)) {
- ast_test_status_update(test, "Successfully created bucket with URI '%s' twice\n",
- ast_sorcery_object_get_id(bucket));
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(bucket_delete)
- {
- RAII_VAR(struct ast_bucket *, bucket, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = "bucket_delete";
- info->category = "/main/bucket/";
- info->summary = "bucket deletion unit test";
- info->description =
- "Test deletion of buckets";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- if (!(bucket = ast_bucket_alloc("test:///tmp/bob"))) {
- ast_test_status_update(test, "Failed to allocate bucket\n");
- return AST_TEST_FAIL;
- }
- bucket_test_wizard_clear();
- if (ast_bucket_delete(bucket)) {
- ast_test_status_update(test, "Failed to delete bucket with URI '%s'\n",
- ast_sorcery_object_get_id(bucket));
- return AST_TEST_FAIL;
- }
- if (!bucket_test_wizard_state.deleted) {
- ast_test_status_update(test, "Bucket deletion returned success but scheme implementation never actually deleted it\n");
- return AST_TEST_FAIL;
- }
- if (!ast_bucket_delete(bucket)) {
- ast_test_status_update(test, "Successfully deleted bucket with URI '%s' twice\n",
- ast_sorcery_object_get_id(bucket));
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(bucket_json)
- {
- RAII_VAR(struct ast_bucket *, bucket, NULL, ao2_cleanup);
- RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
- RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
- switch (cmd) {
- case TEST_INIT:
- info->name = "bucket_json";
- info->category = "/main/bucket/";
- info->summary = "bucket json unit test";
- info->description =
- "Test creation of JSON for a bucket";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- if (!(bucket = ast_bucket_alloc("test:///tmp/bob"))) {
- ast_test_status_update(test, "Failed to allocate bucket\n");
- return AST_TEST_FAIL;
- }
- ast_str_container_add(bucket->buckets, "test:///tmp/bob/joe");
- ast_str_container_add(bucket->files, "test:///tmp/bob/recording.wav");
- expected = ast_json_pack("{s: s, s: s, s: [s], s: s, s: [s], s: s}",
- "modified", "0.000000", "created", "0.000000",
- "buckets", "test:///tmp/bob/joe",
- "scheme", "test",
- "files", "test:///tmp/bob/recording.wav",
- "id", "test:///tmp/bob");
- if (!expected) {
- ast_test_status_update(test, "Could not produce JSON for expected bucket value\n");
- return AST_TEST_FAIL;
- }
- json = ast_bucket_json(bucket);
- if (!json) {
- ast_test_status_update(test, "Could not produce JSON for a valid bucket\n");
- return AST_TEST_FAIL;
- }
- if (!ast_json_equal(json, expected)) {
- ast_test_status_update(test, "Bucket JSON does not match expected output\n");
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(bucket_retrieve)
- {
- RAII_VAR(struct ast_bucket *, bucket, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = "bucket_retrieve";
- info->category = "/main/bucket/";
- info->summary = "bucket retrieval unit test";
- info->description =
- "Test retrieval of buckets";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- if (!(bucket = ast_bucket_retrieve("test://tmp/bob"))) {
- ast_test_status_update(test, "Failed to retrieve known valid bucket\n");
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(bucket_file_alloc)
- {
- RAII_VAR(struct ast_bucket_file *, file, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = "bucket_file_alloc";
- info->category = "/main/bucket/";
- info->summary = "bucket file allocation unit test";
- info->description =
- "Test allocation of bucket files";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- if ((file = ast_bucket_file_alloc(""))) {
- ast_test_status_update(test, "Allocated a file with no URI provided\n");
- return AST_TEST_FAIL;
- }
- if (!(file = ast_bucket_file_alloc("test:///tmp/bob"))) {
- ast_test_status_update(test, "Failed to allocate file\n");
- return AST_TEST_FAIL;
- }
- if (ast_strlen_zero(file->path)) {
- ast_test_status_update(test, "Expected temporary path in allocated file");
- return AST_TEST_FAIL;
- }
- if (strcmp(ast_sorcery_object_get_id(file), "test:///tmp/bob")) {
- ast_test_status_update(test, "URI within allocated file is '%s' and should be test:///tmp/bob\n",
- ast_sorcery_object_get_id(file));
- return AST_TEST_FAIL;
- }
- if (strcmp(file->scheme, "test")) {
- ast_test_status_update(test, "Scheme within allocated file is '%s' and should be test\n",
- file->scheme);
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(bucket_file_create)
- {
- RAII_VAR(struct ast_bucket_file *, file, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = "bucket_file_create";
- info->category = "/main/bucket/";
- info->summary = "file creation unit test";
- info->description =
- "Test creation of files";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- if (!(file = ast_bucket_file_alloc("test:///tmp/bob"))) {
- ast_test_status_update(test, "Failed to allocate file\n");
- return AST_TEST_FAIL;
- }
- bucket_test_wizard_clear();
- if (ast_bucket_file_create(file)) {
- ast_test_status_update(test, "Failed to create file with URI '%s'\n",
- ast_sorcery_object_get_id(file));
- return AST_TEST_FAIL;
- }
- if (!bucket_test_wizard_state.created) {
- ast_test_status_update(test, "Bucket file creation returned success but scheme implementation never actually created it\n");
- return AST_TEST_FAIL;
- }
- if (!ast_bucket_file_create(file)) {
- ast_test_status_update(test, "Successfully created file with URI '%s' twice\n",
- ast_sorcery_object_get_id(file));
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(bucket_file_copy)
- {
- RAII_VAR(struct ast_bucket_file *, file, NULL, ao2_cleanup);
- RAII_VAR(struct ast_bucket_file *, copy, NULL, ao2_cleanup);
- FILE *temporary;
- struct stat old, new;
- RAII_VAR(struct ast_bucket_metadata *, metadata, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = "bucket_file_copy";
- info->category = "/main/bucket/";
- info->summary = "bucket file copying unit test";
- info->description =
- "Test copying of bucket files";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- if (!(file = ast_bucket_file_alloc("test:///tmp/bob"))) {
- ast_test_status_update(test, "Failed to allocate file\n");
- return AST_TEST_FAIL;
- }
- ast_bucket_file_metadata_set(file, "bob", "joe");
- if (!(temporary = fopen(file->path, "w"))) {
- ast_test_status_update(test, "Failed to open temporary file '%s'\n", file->path);
- return AST_TEST_FAIL;
- }
- fprintf(temporary, "bob");
- fclose(temporary);
- if (!(copy = ast_bucket_file_copy(file, "test:///tmp/bob2"))) {
- ast_test_status_update(test, "Failed to copy file '%s' to test:///tmp/bob2\n",
- ast_sorcery_object_get_id(file));
- return AST_TEST_FAIL;
- }
- if (stat(file->path, &old)) {
- ast_test_status_update(test, "Failed to retrieve information on old file '%s'\n", file->path);
- return AST_TEST_FAIL;
- }
- if (stat(copy->path, &new)) {
- ast_test_status_update(test, "Failed to retrieve information on copy file '%s'\n", copy->path);
- return AST_TEST_FAIL;
- }
- if (old.st_size != new.st_size) {
- ast_test_status_update(test, "Copying of underlying temporary file failed\n");
- return AST_TEST_FAIL;
- }
- if (ao2_container_count(file->metadata) != ao2_container_count(copy->metadata)) {
- ast_test_status_update(test, "Number of metadata entries does not match original\n");
- return AST_TEST_FAIL;
- }
- metadata = ast_bucket_file_metadata_get(copy, "bob");
- if (!metadata) {
- ast_test_status_update(test, "Copy of file does not have expected metadata\n");
- return AST_TEST_FAIL;
- }
- if (strcmp(metadata->value, "joe")) {
- ast_test_status_update(test, "Copy of file contains metadata for 'bob' but value is not what it should be\n");
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(bucket_file_retrieve)
- {
- RAII_VAR(struct ast_bucket_file *, file, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = "bucket_file_retrieve";
- info->category = "/main/bucket/";
- info->summary = "file retrieval unit test";
- info->description =
- "Test retrieval of files";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- if (!(file = ast_bucket_file_retrieve("test://tmp/bob"))) {
- ast_test_status_update(test, "Failed to retrieve known valid file\n");
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(bucket_file_update)
- {
- RAII_VAR(struct ast_bucket_file *, file, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = "bucket_file_update";
- info->category = "/main/bucket/";
- info->summary = "file updating unit test";
- info->description =
- "Test updating of files";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- if (!(file = ast_bucket_file_alloc("test:///tmp/bob"))) {
- ast_test_status_update(test, "Failed to allocate file\n");
- return AST_TEST_FAIL;
- }
- bucket_test_wizard_clear();
- if (ast_bucket_file_update(file)) {
- ast_test_status_update(test, "Failed to update file with URI '%s'\n",
- ast_sorcery_object_get_id(file));
- return AST_TEST_FAIL;
- }
- if (!bucket_test_wizard_state.updated) {
- ast_test_status_update(test, "Successfully returned file was updated, but it was not\n");
- return AST_TEST_FAIL;
- }
- if (!ast_bucket_file_update(file)) {
- ast_test_status_update(test, "Successfully updated file with URI '%s' twice\n",
- ast_sorcery_object_get_id(file));
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(bucket_file_delete)
- {
- RAII_VAR(struct ast_bucket_file *, file, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = "bucket_file_delete";
- info->category = "/main/bucket/";
- info->summary = "file deletion unit test";
- info->description =
- "Test deletion of files";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- if (!(file = ast_bucket_file_alloc("test:///tmp/bob"))) {
- ast_test_status_update(test, "Failed to allocate file\n");
- return AST_TEST_FAIL;
- }
- bucket_test_wizard_clear();
- if (ast_bucket_file_delete(file)) {
- ast_test_status_update(test, "Failed to delete file with URI '%s'\n",
- ast_sorcery_object_get_id(file));
- return AST_TEST_FAIL;
- }
- if (!bucket_test_wizard_state.deleted) {
- ast_test_status_update(test, "Bucket file deletion returned success but scheme implementation never actually deleted it\n");
- return AST_TEST_FAIL;
- }
- if (!ast_bucket_file_delete(file)) {
- ast_test_status_update(test, "Successfully deleted file with URI '%s' twice\n",
- ast_sorcery_object_get_id(file));
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(bucket_file_metadata_set)
- {
- RAII_VAR(struct ast_bucket_file *, file, NULL, ao2_cleanup);
- RAII_VAR(struct ast_bucket_metadata *, metadata, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = "bucket_file_metadata_set";
- info->category = "/main/bucket/";
- info->summary = "file metadata setting unit test";
- info->description =
- "Test setting of metadata on files";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- if (!(file = ast_bucket_file_alloc("test:///tmp/bob"))) {
- ast_test_status_update(test, "Failed to allocate file\n");
- return AST_TEST_FAIL;
- }
- if (ao2_container_count(file->metadata) != 0) {
- ast_test_status_update(test, "Newly allocated file has metadata count of '%d' when should be 0\n",
- ao2_container_count(file->metadata));
- return AST_TEST_FAIL;
- }
- if (ast_bucket_file_metadata_set(file, "bob", "joe")) {
- ast_test_status_update(test, "Failed to set metadata 'bob' to 'joe' on newly allocated file\n");
- return AST_TEST_FAIL;
- }
- if (!(metadata = ao2_find(file->metadata, "bob", OBJ_KEY))) {
- ast_test_status_update(test, "Failed to find set metadata 'bob' on newly allocated file\n");
- return AST_TEST_FAIL;
- }
- if (strcmp(metadata->value, "joe")) {
- ast_test_status_update(test, "Metadata has value '%s' when should be 'joe'\n",
- metadata->value);
- return AST_TEST_FAIL;
- }
- ao2_cleanup(metadata);
- metadata = NULL;
- if (ast_bucket_file_metadata_set(file, "bob", "fred")) {
- ast_test_status_update(test, "Failed to overwrite metadata 'bob' with new value 'fred'\n");
- return AST_TEST_FAIL;
- }
- if (!(metadata = ao2_find(file->metadata, "bob", OBJ_KEY))) {
- ast_test_status_update(test, "Failed to find overwritten metadata 'bob' on newly allocated file\n");
- return AST_TEST_FAIL;
- }
- if (strcmp(metadata->value, "fred")) {
- ast_test_status_update(test, "Metadata has value '%s' when should be 'fred'\n",
- metadata->value);
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(bucket_file_metadata_unset)
- {
- RAII_VAR(struct ast_bucket_file *, file, NULL, ao2_cleanup);
- RAII_VAR(struct ast_bucket_metadata *, metadata, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = "bucket_file_metadata_unset";
- info->category = "/main/bucket/";
- info->summary = "file metadata unsetting unit test";
- info->description =
- "Test unsetting of metadata on files";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- if (!(file = ast_bucket_file_alloc("test:///tmp/bob"))) {
- ast_test_status_update(test, "Failed to allocate file\n");
- return AST_TEST_FAIL;
- }
- if (ast_bucket_file_metadata_set(file, "bob", "joe")) {
- ast_test_status_update(test, "Failed to set metadata 'bob' to 'joe' on newly allocated file\n");
- return AST_TEST_FAIL;
- }
- if (ast_bucket_file_metadata_unset(file, "bob")) {
- ast_test_status_update(test, "Failed to unset metadata 'bob' on newly allocated file\n");
- return AST_TEST_FAIL;
- }
- if ((metadata = ao2_find(file->metadata, "bob", OBJ_KEY))) {
- ast_test_status_update(test, "Metadata 'bob' was unset, but can still be found\n");
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(bucket_file_metadata_get)
- {
- RAII_VAR(struct ast_bucket_file *, file, NULL, ao2_cleanup);
- RAII_VAR(struct ast_bucket_metadata *, metadata, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = "bucket_file_metadata_get";
- info->category = "/main/bucket/";
- info->summary = "file metadata getting unit test";
- info->description =
- "Test getting of metadata on files";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- if (!(file = ast_bucket_file_alloc("test:///tmp/bob"))) {
- ast_test_status_update(test, "Failed to allocate file\n");
- return AST_TEST_FAIL;
- }
- if (ast_bucket_file_metadata_set(file, "bob", "joe")) {
- ast_test_status_update(test, "Failed to set metadata 'bob' to 'joe' on newly allocated file\n");
- return AST_TEST_FAIL;
- }
- if (!(metadata = ast_bucket_file_metadata_get(file, "bob"))) {
- ast_test_status_update(test, "Failed to retrieve metadata 'bob' that was just set\n");
- return AST_TEST_FAIL;
- }
- if (strcmp(metadata->value, "joe")) {
- ast_test_status_update(test, "Retrieved metadata value is '%s' while it should be 'joe'\n",
- metadata->value);
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(bucket_file_json)
- {
- RAII_VAR(struct ast_bucket_file *, file, NULL, ao2_cleanup);
- RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
- RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
- switch (cmd) {
- case TEST_INIT:
- info->name = "bucket_file_json";
- info->category = "/main/bucket/";
- info->summary = "file json unit test";
- info->description =
- "Test creation of JSON for a file";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- if (!(file = ast_bucket_file_alloc("test:///tmp/bob"))) {
- ast_test_status_update(test, "Failed to allocate bucket\n");
- return AST_TEST_FAIL;
- }
- if (ast_bucket_file_metadata_set(file, "bob", "joe")) {
- ast_test_status_update(test, "Failed to set metadata 'bob' to 'joe' on newly allocated file\n");
- return AST_TEST_FAIL;
- }
- expected = ast_json_pack("{s: s, s: s, s: s, s: s, s: {s :s}}",
- "modified", "0.000000", "created", "0.000000", "scheme", "test",
- "id", "test:///tmp/bob", "metadata", "bob", "joe");
- if (!expected) {
- ast_test_status_update(test, "Could not produce JSON for expected bucket file value\n");
- return AST_TEST_FAIL;
- }
- json = ast_bucket_file_json(file);
- if (!json) {
- ast_test_status_update(test, "Could not produce JSON for a valid file\n");
- return AST_TEST_FAIL;
- }
- if (!ast_json_equal(json, expected)) {
- ast_test_status_update(test, "Bucket file JSON does not match expected output\n");
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- static int unload_module(void)
- {
- AST_TEST_UNREGISTER(bucket_scheme_register);
- AST_TEST_UNREGISTER(bucket_alloc);
- AST_TEST_UNREGISTER(bucket_create);
- AST_TEST_UNREGISTER(bucket_delete);
- AST_TEST_UNREGISTER(bucket_retrieve);
- AST_TEST_UNREGISTER(bucket_json);
- AST_TEST_UNREGISTER(bucket_file_alloc);
- AST_TEST_UNREGISTER(bucket_file_create);
- AST_TEST_UNREGISTER(bucket_file_copy);
- AST_TEST_UNREGISTER(bucket_file_retrieve);
- AST_TEST_UNREGISTER(bucket_file_update);
- AST_TEST_UNREGISTER(bucket_file_delete);
- AST_TEST_UNREGISTER(bucket_file_metadata_set);
- AST_TEST_UNREGISTER(bucket_file_metadata_unset);
- AST_TEST_UNREGISTER(bucket_file_metadata_get);
- AST_TEST_UNREGISTER(bucket_file_json);
- return 0;
- }
- static int load_module(void)
- {
- if (ast_bucket_scheme_register("test", &bucket_test_wizard, &bucket_file_test_wizard,
- ast_bucket_file_temporary_create, ast_bucket_file_temporary_destroy)) {
- ast_log(LOG_ERROR, "Failed to register Bucket test wizard scheme implementation\n");
- return AST_MODULE_LOAD_DECLINE;
- }
- AST_TEST_REGISTER(bucket_scheme_register);
- AST_TEST_REGISTER(bucket_alloc);
- AST_TEST_REGISTER(bucket_create);
- AST_TEST_REGISTER(bucket_delete);
- AST_TEST_REGISTER(bucket_retrieve);
- AST_TEST_REGISTER(bucket_json);
- AST_TEST_REGISTER(bucket_file_alloc);
- AST_TEST_REGISTER(bucket_file_create);
- AST_TEST_REGISTER(bucket_file_copy);
- AST_TEST_REGISTER(bucket_file_retrieve);
- AST_TEST_REGISTER(bucket_file_update);
- AST_TEST_REGISTER(bucket_file_delete);
- AST_TEST_REGISTER(bucket_file_metadata_set);
- AST_TEST_REGISTER(bucket_file_metadata_unset);
- AST_TEST_REGISTER(bucket_file_metadata_get);
- AST_TEST_REGISTER(bucket_file_json);
- return AST_MODULE_LOAD_SUCCESS;
- }
- AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Bucket test module");
|