summaryrefslogtreecommitdiff
path: root/tests/test_sorcery.c
diff options
context:
space:
mode:
authorJoshua Colp <jcolp@digium.com>2013-06-22 14:26:25 +0000
committerJoshua Colp <jcolp@digium.com>2013-06-22 14:26:25 +0000
commita330d0867e3155e34ecbfd23a84fe6e7ebf51469 (patch)
treed19cbb217482b0dc2952afe2495d59b868d1ff82 /tests/test_sorcery.c
parent77002bc377f19ea11e60732c486b6ef371688773 (diff)
Make sorcery details opaque and add extended fields.
Sorcery specific object information is now opaque and allocated with the object. This means that modules do not need to be recompiled if the sorcery specific part is changed. It also means that sorcery can store additional information on objects and ensure it is freed or the reference count decreased when the object goes away. To facilitate the above a generic sorcery allocator function has been added which also ensures that allocated objects do not have a lock. Extended fields have been added thanks to all of the above which allows specific fields to be marked as extended, and thus simply stored as-is within the object. Type safety is *NOT* enforced on these fields. A consumer of them has to query and ultimately perform their own safety check. What does this mean? Extra modules can extend already defined structures without having to modify them. Tests have also been included to verify extended field functionality. Review: https://reviewboard.asterisk.org/r/2585/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@392586 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'tests/test_sorcery.c')
-rw-r--r--tests/test_sorcery.c69
1 files changed, 68 insertions, 1 deletions
diff --git a/tests/test_sorcery.c b/tests/test_sorcery.c
index e1d4b7fce..868c58268 100644
--- a/tests/test_sorcery.c
+++ b/tests/test_sorcery.c
@@ -49,7 +49,7 @@ struct test_sorcery_object {
/*! \brief Internal function to allocate a test object */
static void *test_sorcery_object_alloc(const char *id)
{
- return ao2_alloc(sizeof(struct test_sorcery_object), NULL);
+ return ast_sorcery_generic_alloc(sizeof(struct test_sorcery_object), NULL);
}
/*! \brief Internal function for object set transformation */
@@ -1300,6 +1300,71 @@ AST_TEST_DEFINE(objectset_apply_fields)
return res;
}
+AST_TEST_DEFINE(extended_fields)
+{
+ int res = AST_TEST_PASS;
+ RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
+ RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
+ RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
+ const char *value;
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "extended_fields";
+ info->category = "/main/sorcery/";
+ info->summary = "sorcery object extended fields unit test";
+ info->description =
+ "Test extended fields support in sorcery";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ if (!(sorcery = alloc_and_initialize_sorcery())) {
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
+ ast_test_status_update(test, "Failed to allocate a known object type\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (!(objset = ast_variable_new("@testing", "toast", ""))) {
+ ast_test_status_update(test, "Failed to create an object set, test could not occur\n");
+ res = AST_TEST_FAIL;
+ } else if (ast_sorcery_objectset_apply(sorcery, obj, objset)) {
+ ast_test_status_update(test, "Failed to apply valid object set to object\n");
+ res = AST_TEST_FAIL;
+ } else if (!(value = ast_sorcery_object_get_extended(obj, "testing"))) {
+ ast_test_status_update(test, "Extended field, which was set using object set, could not be found\n");
+ res = AST_TEST_FAIL;
+ } else if (strcmp(value, "toast")) {
+ ast_test_status_update(test, "Extended field does not contain expected value\n");
+ res = AST_TEST_FAIL;
+ } else if (ast_sorcery_object_set_extended(obj, "@tacos", "supreme")) {
+ ast_test_status_update(test, "Extended field could not be set\n");
+ res = AST_TEST_FAIL;
+ } else if (!(value = ast_sorcery_object_get_extended(obj, "tacos"))) {
+ ast_test_status_update(test, "Extended field, which was set using the API, could not be found\n");
+ res = AST_TEST_FAIL;
+ } else if (strcmp(value, "supreme")) {
+ ast_test_status_update(test, "Extended field does not contain expected value\n");
+ res = AST_TEST_FAIL;
+ } else if (ast_sorcery_object_set_extended(obj, "@tacos", "canadian")) {
+ ast_test_status_update(test, "Extended field could not be set a second time\n");
+ res = AST_TEST_FAIL;
+ } else if (!(value = ast_sorcery_object_get_extended(obj, "tacos"))) {
+ ast_test_status_update(test, "Extended field, which was set using the API, could not be found\n");
+ res = AST_TEST_FAIL;
+ } else if (strcmp(value, "canadian")) {
+ ast_test_status_update(test, "Extended field does not contain expected value\n");
+ res = AST_TEST_FAIL;
+ }
+
+ return res;
+}
+
AST_TEST_DEFINE(changeset_create)
{
int res = AST_TEST_PASS;
@@ -2604,6 +2669,7 @@ static int unload_module(void)
AST_TEST_UNREGISTER(objectset_apply_invalid);
AST_TEST_UNREGISTER(objectset_transform);
AST_TEST_UNREGISTER(objectset_apply_fields);
+ AST_TEST_UNREGISTER(extended_fields);
AST_TEST_UNREGISTER(changeset_create);
AST_TEST_UNREGISTER(changeset_create_unchanged);
AST_TEST_UNREGISTER(object_create);
@@ -2651,6 +2717,7 @@ static int load_module(void)
AST_TEST_REGISTER(objectset_apply_invalid);
AST_TEST_REGISTER(objectset_transform);
AST_TEST_REGISTER(objectset_apply_fields);
+ AST_TEST_REGISTER(extended_fields);
AST_TEST_REGISTER(changeset_create);
AST_TEST_REGISTER(changeset_create_unchanged);
AST_TEST_REGISTER(object_create);