Skip to content
Snippets Groups Projects
test_sorcery.c 120 KiB
Newer Older
  • Learn to ignore specific revisions
  • 		info->name = "object_retrieve_regex";
    		info->category = "/main/sorcery/";
    		info->summary = "sorcery multiple object retrieval using regex unit test";
    		info->description =
    			"Test multiple object retrieval in sorcery using regular expression for matching";
    		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-98joe"))) {
    		ast_test_status_update(test, "Failed to allocate a known object type\n");
    		return AST_TEST_FAIL;
    	}
    
    	if (ast_sorcery_create(sorcery, obj)) {
    		ast_test_status_update(test, "Failed to create object using in-memory wizard\n");
    		return AST_TEST_FAIL;
    	}
    
    	ao2_cleanup(obj);
    
    	if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah-93joe"))) {
    		ast_test_status_update(test, "Failed to allocate second instance of a known object type\n");
    		return AST_TEST_FAIL;
    	}
    
    	if (ast_sorcery_create(sorcery, obj)) {
    		ast_test_status_update(test, "Failed to create second object using in-memory wizard\n");
    		return AST_TEST_FAIL;
    	}
    
    	ao2_cleanup(obj);
    
    	if (!(obj = ast_sorcery_alloc(sorcery, "test", "neener-93joe"))) {
    		ast_test_status_update(test, "Failed to allocate third instance of a known object type\n");
    		return AST_TEST_FAIL;
    	}
    
    	if (ast_sorcery_create(sorcery, obj)) {
    		ast_test_status_update(test, "Failed to create third object using in-memory wizard\n");
    		return AST_TEST_FAIL;
    	}
    
    	if (!(objects = ast_sorcery_retrieve_by_regex(sorcery, "test", "^blah-"))) {
    		ast_test_status_update(test, "Failed to retrieve a container of objects\n");
    		return AST_TEST_FAIL;
    	} else if (ao2_container_count(objects) != 2) {
    		ast_test_status_update(test, "Received a container with incorrect number of objects in it\n");
    		return AST_TEST_FAIL;
    	}
    
    	return AST_TEST_PASS;
    }
    
    
    AST_TEST_DEFINE(object_update)
    {
    	RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
    	RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
    	RAII_VAR(struct test_sorcery_object *, obj2, NULL, ao2_cleanup);
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = "object_update";
    		info->category = "/main/sorcery/";
    		info->summary = "sorcery object update unit test";
    		info->description =
    			"Test object updating 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 (ast_sorcery_create(sorcery, obj)) {
    		ast_test_status_update(test, "Failed to create object using in-memory wizard\n");
    		return AST_TEST_FAIL;
    	}
    
    	if (!(obj2 = ast_sorcery_copy(sorcery, obj))) {
    		ast_test_status_update(test, "Failed to allocate a known object type for updating\n");
    		return AST_TEST_FAIL;
    	}
    
    	ao2_cleanup(obj);
    
    	if (ast_sorcery_update(sorcery, obj2)) {
    		ast_test_status_update(test, "Failed to update sorcery with new object\n");
    		return AST_TEST_FAIL;
    	}
    
    	if (!(obj = ast_sorcery_retrieve_by_id(sorcery, "test", "blah"))) {
    		ast_test_status_update(test, "Failed to retrieve properly updated object\n");
    		return AST_TEST_FAIL;
    	} else if (obj != obj2) {
    		ast_test_status_update(test, "Object retrieved is not the updated object\n");
    		return AST_TEST_FAIL;
    	}
    
    	return AST_TEST_PASS;
    }
    
    AST_TEST_DEFINE(object_update_uncreated)
    {
    	RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
    	RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = "object_update_uncreated";
    		info->category = "/main/sorcery/";
    		info->summary = "sorcery object update unit test";
    		info->description =
    			"Test updating of an uncreated object 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 (!ast_sorcery_update(sorcery, obj)) {
    		ast_test_status_update(test, "Successfully updated an object which has not been created yet\n");
    		return AST_TEST_FAIL;
    	}
    
    	return AST_TEST_PASS;
    }
    
    AST_TEST_DEFINE(object_delete)
    {
    	RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
    	RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = "object_delete";
    		info->category = "/main/sorcery/";
    		info->summary = "sorcery object deletion unit test";
    		info->description =
    			"Test object deletion 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 (ast_sorcery_create(sorcery, obj)) {
    		ast_test_status_update(test, "Failed to create object using in-memory wizard\n");
    		return AST_TEST_FAIL;
    	}
    
    	if (ast_sorcery_delete(sorcery, obj)) {
    		ast_test_status_update(test, "Failed to delete object using in-memory wizard\n");
    		return AST_TEST_FAIL;
    	}
    
    	ao2_cleanup(obj);
    
    	if ((obj = ast_sorcery_retrieve_by_id(sorcery, "test", "blah"))) {
    		ast_test_status_update(test, "Retrieved deleted object that should not be there\n");
    		return AST_TEST_FAIL;
    	}
    
    	return AST_TEST_PASS;
    }
    
    AST_TEST_DEFINE(object_delete_uncreated)
    {
    	RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
    	RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = "object_delete_uncreated";
    		info->category = "/main/sorcery/";
    		info->summary = "sorcery object deletion unit test";
    		info->description =
    			"Test object deletion of an uncreated object 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 (!ast_sorcery_delete(sorcery, obj)) {
    		ast_test_status_update(test, "Successfully deleted an object which was never created\n");
    		return AST_TEST_FAIL;
    	}
    
    	return AST_TEST_PASS;
    }
    
    
    AST_TEST_DEFINE(object_is_stale)
    {
    	RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
    	RAII_VAR(struct ast_sorcery_wizard *, wizard1, &test_wizard, ast_sorcery_wizard_unregister);
    	RAII_VAR(struct ast_sorcery_wizard *, wizard2, &test_wizard2, ast_sorcery_wizard_unregister);
    	RAII_VAR(struct test_sorcery_object *, obj1, NULL, ao2_cleanup);
    	RAII_VAR(struct test_sorcery_object *, obj2, NULL, ao2_cleanup);
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = "object_is_stale";
    		info->category = "/main/sorcery/";
    		info->summary = "sorcery object staleness unit test";
    		info->description =
    			"Test whether sorcery will query a wizard correctly if asked\n"
    			"if an object is stale.";
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    
    	if (ast_sorcery_wizard_register(&test_wizard)) {
    		ast_test_status_update(test, "Failed to register a perfectly valid sorcery wizard\n");
    		return AST_TEST_FAIL;
    	}
    
    	if (ast_sorcery_wizard_register(&test_wizard2)) {
    		ast_test_status_update(test, "Failed to register a perfectly valid sorcery wizard\n");
    		return AST_TEST_FAIL;
    	}
    
    	if (!(sorcery = ast_sorcery_open())) {
    		ast_test_status_update(test, "Failed to open sorcery structure\n");
    		return AST_TEST_FAIL;
    	}
    
    	if ((ast_sorcery_apply_default(sorcery, "test", "test", NULL) != AST_SORCERY_APPLY_SUCCESS) ||
    		ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL)) {
    		return AST_TEST_FAIL;
    	}
    
    	ast_sorcery_object_field_register_nodoc(sorcery, "test", "bob", "5", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, bob));
    	ast_sorcery_object_field_register_nodoc(sorcery, "test", "joe", "10", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, joe));
    	ast_sorcery_object_field_register_custom_nodoc(sorcery, "test", "jim", "444", jim_handler, NULL, jim_vl, 0, 0);
    	ast_sorcery_object_field_register_custom_nodoc(sorcery, "test", "jack", "888,999", jack_handler, jack_str, NULL, 0, 0);
    
    
    	if ((ast_sorcery_apply_default(sorcery, "test2", "test2", "test2data") != AST_SORCERY_APPLY_SUCCESS) ||
    		ast_sorcery_internal_object_register(sorcery, "test2", test_sorcery_object_alloc, NULL, NULL)) {
    		return AST_TEST_FAIL;
    	}
    
    	ast_sorcery_object_field_register_nodoc(sorcery, "test2", "bob", "5", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, bob));
    	ast_sorcery_object_field_register_nodoc(sorcery, "test2", "joe", "10", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, joe));
    	ast_sorcery_object_field_register_custom_nodoc(sorcery, "test2", "jim", "444", jim_handler, NULL, jim_vl, 0, 0);
    	ast_sorcery_object_field_register_custom_nodoc(sorcery, "test2", "jack", "888,999", jack_handler, jack_str, NULL, 0, 0);
    
    
    	if (!(obj1 = ast_sorcery_alloc(sorcery, "test", "blah"))) {
    		ast_test_status_update(test, "Failed to allocate a known object type\n");
    		return AST_TEST_FAIL;
    	}
    
    	if (!(obj2 = ast_sorcery_alloc(sorcery, "test2", "blah"))) {
    		ast_test_status_update(test, "Failed to allocate a known object type\n");
    		return AST_TEST_FAIL;
    	}
    
    	/* The 'test' wizard has no is_stale callback */
    	ast_test_validate(test, ast_sorcery_is_stale(sorcery, obj1) == 0);
    
    	/* The 'test2' wizard should return stale */
    	ast_test_validate(test, ast_sorcery_is_stale(sorcery, obj2) == 1);
    	ast_test_validate(test, cache.is_stale == 1);
    
    	return AST_TEST_PASS;
    }
    
    
    AST_TEST_DEFINE(caching_wizard_behavior)
    {
    	struct ast_flags flags = { CONFIG_FLAG_NOCACHE };
    	struct ast_config *config;
    	RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
    	RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
    	RAII_VAR(struct test_sorcery_object *, obj2, NULL, ao2_cleanup);
    	int res = AST_TEST_FAIL;
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = "caching_wizard_behavior";
    		info->category = "/main/sorcery/";
    		info->summary = "sorcery caching wizard behavior unit test";
    		info->description =
    			"Test internal behavior of caching wizards";
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    
    	if (!(config = ast_config_load2("sorcery.conf", "test_sorcery_cache", flags))) {
    		ast_test_status_update(test, "Sorcery configuration file not present - skipping caching_wizard_behavior test\n");
    		return AST_TEST_NOT_RUN;
    	}
    
    
    	if (!ast_category_get(config, "test_sorcery_cache", NULL)) {
    
    		ast_test_status_update(test, "Sorcery configuration file does not contain 'test_sorcery_cache' section\n");
    		ast_config_destroy(config);
    		return AST_TEST_NOT_RUN;
    	}
    
    	ast_config_destroy(config);
    
    	if (ast_sorcery_wizard_register(&test_wizard)) {
    		ast_test_status_update(test, "Failed to register a perfectly valid sorcery wizard\n");
    		return AST_TEST_FAIL;
    	}
    
    	if (!(sorcery = ast_sorcery_open())) {
    		ast_test_status_update(test, "Failed to open sorcery structure\n");
    		goto end;
    	}
    
    
    	if (ast_sorcery_apply_config(sorcery, "test_sorcery_cache") != AST_SORCERY_APPLY_SUCCESS) {
    
    		ast_test_status_update(test, "Failed to apply configured object mappings\n");
    		goto end;
    	}
    
    
    Matthew Jordan's avatar
    Matthew Jordan committed
    	if (ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL)) {
    
    		ast_test_status_update(test, "Failed to register object type\n");
    		goto end;
    	}
    
    	if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
    		ast_test_status_update(test, "Failed to allocate a known object type\n");
    		goto end;
    	}
    
    	if (ast_sorcery_create(sorcery, obj)) {
    		ast_test_status_update(test, "Failed to create object using in-memory wizard\n");
    		goto end;
    	}
    
    	ao2_cleanup(obj);
    
    	if (!(obj = ast_sorcery_retrieve_by_id(sorcery, "test", "blah"))) {
    		ast_test_status_update(test, "Failed to retrieve just created object\n");
    		goto end;
    	} else if (!cache.created) {
    		ast_test_status_update(test, "Caching wizard was not told to cache just created object\n");
    		goto end;
    	} else if (!(obj2 = ast_sorcery_retrieve_by_id(sorcery, "test", "blah"))) {
    		ast_test_status_update(test, "Failed to retrieve just cached object\n");
    		goto end;
    	} else if (obj == obj2) {
    		ast_test_status_update(test, "Returned object is *NOT* a cached object\n");
    		goto end;
    	} else if (ast_sorcery_update(sorcery, obj)) {
    		ast_test_status_update(test, "Failed to update a known stored object\n");
    		goto end;
    	} else if (!cache.updated) {
    		ast_test_status_update(test, "Caching wizard was not told to update object\n");
    		goto end;
    	} else if (ast_sorcery_delete(sorcery, obj)) {
    		ast_test_status_update(test, "Failed to delete a known stored object\n");
    		goto end;
    	} else if (!cache.deleted) {
    		ast_test_status_update(test, "Caching wizard was not told to delete object\n");
    		goto end;
    	}
    
    	ao2_cleanup(obj2);
    
    	if ((obj2 = ast_sorcery_retrieve_by_id(sorcery, "test", "blah"))) {
    		ast_test_status_update(test, "Retrieved an object that should have been deleted\n");
    		goto end;
    	}
    
    	res = AST_TEST_PASS;
    
    end:
    	ast_sorcery_unref(sorcery);
    	sorcery = NULL;
    
    	if (ast_sorcery_wizard_unregister(&test_wizard)) {
    		ast_test_status_update(test, "Failed to unregister test sorcery wizard\n");
    		return AST_TEST_FAIL;
    	}
    
    	return res;
    }
    
    
    AST_TEST_DEFINE(object_type_observer)
    {
    	RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
    	RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
    	int res = AST_TEST_FAIL;
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = "object_type_observer";
    		info->category = "/main/sorcery/";
    		info->summary = "sorcery object type observer unit test";
    		info->description =
    			"Test that object type observers get called when they should";
    		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 (!ast_sorcery_observer_add(sorcery, "test", NULL)) {
    		ast_test_status_update(test, "Successfully added a NULL observer when it should not be possible\n");
    		return AST_TEST_FAIL;
    	}
    
    	if (ast_sorcery_observer_add(sorcery, "test", &test_observer)) {
    		ast_test_status_update(test, "Failed to add a proper observer\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");
    		goto end;
    	}
    
    	ast_mutex_init(&observer.lock);
    	ast_cond_init(&observer.cond, NULL);
    	observer.created = NULL;
    	observer.updated = NULL;
    	observer.deleted = NULL;
    
    	if (ast_sorcery_create(sorcery, obj)) {
    		ast_test_status_update(test, "Failed to create object using in-memory wizard\n");
    		goto end;
    	}
    
    	ast_mutex_lock(&observer.lock);
    	while (!observer.created) {
            struct timeval start = ast_tvnow();
            struct timespec end = {
                    .tv_sec = start.tv_sec + 10,
                    .tv_nsec = start.tv_usec * 1000,
            };
    		if (ast_cond_timedwait(&observer.cond, &observer.lock, &end) == ETIMEDOUT) {
    			break;
    		}
    	}
    	ast_mutex_unlock(&observer.lock);
    
    	if (!observer.created) {
    		ast_test_status_update(test, "Failed to receive observer notification for object creation within suitable timeframe\n");
    		goto end;
    	}
    
    	if (ast_sorcery_update(sorcery, obj)) {
    		ast_test_status_update(test, "Failed to update object using in-memory wizard\n");
    		goto end;
    	}
    
    	ast_mutex_lock(&observer.lock);
    	while (!observer.updated) {
            struct timeval start = ast_tvnow();
            struct timespec end = {
                    .tv_sec = start.tv_sec + 10,
                    .tv_nsec = start.tv_usec * 1000,
            };
    		if (ast_cond_timedwait(&observer.cond, &observer.lock, &end) == ETIMEDOUT) {
    			break;
    		}
    	}
    	ast_mutex_unlock(&observer.lock);
    
    	if (!observer.updated) {
    		ast_test_status_update(test, "Failed to receive observer notification for object updating within suitable timeframe\n");
    		goto end;
    	}
    
    	if (ast_sorcery_delete(sorcery, obj)) {
    		ast_test_status_update(test, "Failed to delete object using in-memory wizard\n");
    		goto end;
    	}
    
    	ast_mutex_lock(&observer.lock);
    	while (!observer.deleted) {
            struct timeval start = ast_tvnow();
            struct timespec end = {
                    .tv_sec = start.tv_sec + 10,
                    .tv_nsec = start.tv_usec * 1000,
            };
    		if (ast_cond_timedwait(&observer.cond, &observer.lock, &end) == ETIMEDOUT) {
    			break;
    		}
    	}
    	ast_mutex_unlock(&observer.lock);
    
    	if (!observer.deleted) {
    		ast_test_status_update(test, "Failed to receive observer notification for object deletion within suitable timeframe\n");
    		goto end;
    	}
    
    	ast_sorcery_reload(sorcery);
    
    	ast_mutex_lock(&observer.lock);
    	while (!observer.loaded) {
            struct timeval start = ast_tvnow();
            struct timespec end = {
                    .tv_sec = start.tv_sec + 10,
                    .tv_nsec = start.tv_usec * 1000,
            };
    		if (ast_cond_timedwait(&observer.cond, &observer.lock, &end) == ETIMEDOUT) {
    			break;
    		}
    	}
    	ast_mutex_unlock(&observer.lock);
    
    	if (!observer.loaded) {
    		ast_test_status_update(test, "Failed to receive observer notification for object type load within suitable timeframe\n");
    		goto end;
    	}
    
    	res = AST_TEST_PASS;
    
    end:
    	observer.created = NULL;
    	observer.updated = NULL;
    	observer.deleted = NULL;
    	ast_mutex_destroy(&observer.lock);
    	ast_cond_destroy(&observer.cond);
    
    	return res;
    }
    
    
    AST_TEST_DEFINE(configuration_file_wizard)
    {
    	struct ast_flags flags = { CONFIG_FLAG_NOCACHE };
    	struct ast_config *config;
    	RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
    	RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = "configuration_file_wizard";
    		info->category = "/main/sorcery/";
    		info->summary = "sorcery configuration file wizard unit test";
    		info->description =
    			"Test the configuration file wizard in sorcery";
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    
    	if (!(config = ast_config_load2("test_sorcery.conf", "test_sorcery", flags))) {
    		ast_test_status_update(test, "Test sorcery configuration file wizard file not present - skipping configuration_file_wizard test\n");
    		return AST_TEST_NOT_RUN;
    	}
    
    	ast_config_destroy(config);
    
    	if (!(sorcery = ast_sorcery_open())) {
    		ast_test_status_update(test, "Failed to open sorcery structure\n");
    		return AST_TEST_FAIL;
    	}
    
    
    	if (ast_sorcery_apply_default(sorcery, "test", "config", "test_sorcery.conf") != AST_SORCERY_APPLY_SUCCESS) {
    
    		ast_test_status_update(test, "Could not set a default wizard of the 'config' type, so skipping since it may not be loaded\n");
    		return AST_TEST_NOT_RUN;
    	}
    
    
    Matthew Jordan's avatar
    Matthew Jordan committed
    	if (ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL)) {
    
    		ast_test_status_update(test, "Failed to register object type\n");
    		return AST_TEST_FAIL;
    	}
    
    
    Matthew Jordan's avatar
    Matthew Jordan committed
    	ast_sorcery_object_field_register_nodoc(sorcery, "test", "bob", "5", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, bob));
    	ast_sorcery_object_field_register_nodoc(sorcery, "test", "joe", "10", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, joe));
    
    
    	ast_sorcery_load(sorcery);
    
    	if ((obj = ast_sorcery_retrieve_by_id(sorcery, "test", "hey2"))) {
    		ast_test_status_update(test, "Retrieved object which has an unknown field\n");
    		return AST_TEST_FAIL;
    	} else if (!(obj = ast_sorcery_retrieve_by_id(sorcery, "test", "hey"))) {
    		ast_test_status_update(test, "Failed to retrieve a known object that has been configured in the configuration file\n");
    		return AST_TEST_FAIL;
    	} else if (obj->bob != 98) {
    		ast_test_status_update(test, "Value of 'bob' on object is not what is configured in configuration file\n");
    		return AST_TEST_FAIL;
    	} else if (obj->joe != 41) {
    		ast_test_status_update(test, "Value of 'joe' on object is not what is configured in configuration file\n");
    		return AST_TEST_FAIL;
    	}
    
    	return AST_TEST_PASS;
    }
    
    AST_TEST_DEFINE(configuration_file_wizard_with_file_integrity)
    {
    	struct ast_flags flags = { CONFIG_FLAG_NOCACHE };
    	struct ast_config *config;
    	RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
    	RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = "configuration_file_wizard_with_file_integrity";
    		info->category = "/main/sorcery/";
    		info->summary = "sorcery configuration file wizard file integrity unit test";
    		info->description =
    			"Test the configuration file wizard with file integrity turned on in sorcery";
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    
    	if (!(config = ast_config_load2("test_sorcery.conf", "test_sorcery", flags))) {
    		ast_test_status_update(test, "Test sorcery configuration file wizard file not present - skipping configuration_file_wizard_with_file_integrity test\n");
    		return AST_TEST_NOT_RUN;
    	}
    
    	ast_config_destroy(config);
    
    	if (!(sorcery = ast_sorcery_open())) {
    		ast_test_status_update(test, "Failed to open sorcery structure\n");
    		return AST_TEST_FAIL;
    	}
    
    
    	if (ast_sorcery_apply_default(sorcery, "test", "config", "test_sorcery.conf,integrity=file") != AST_SORCERY_APPLY_SUCCESS) {
    
    		ast_test_status_update(test, "Could not set a default wizard of the 'config' type, so skipping since it may not be loaded\n");
    		return AST_TEST_NOT_RUN;
    	}
    
    
    Matthew Jordan's avatar
    Matthew Jordan committed
    	if (ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL)) {
    
    		ast_test_status_update(test, "Failed to register object type\n");
    		return AST_TEST_FAIL;
    	}
    
    
    Matthew Jordan's avatar
    Matthew Jordan committed
    	ast_sorcery_object_field_register_nodoc(sorcery, "test", "bob", "5", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, bob));
    	ast_sorcery_object_field_register_nodoc(sorcery, "test", "joe", "10", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, joe));
    
    
    	ast_sorcery_load(sorcery);
    
    	if ((obj = ast_sorcery_retrieve_by_id(sorcery, "test", "hey"))) {
    		ast_test_status_update(test, "Retrieved object which has an unknown field\n");
    		return AST_TEST_FAIL;
    	}
    
    	return AST_TEST_PASS;
    }
    
    AST_TEST_DEFINE(configuration_file_wizard_with_criteria)
    {
    	struct ast_flags flags = { CONFIG_FLAG_NOCACHE };
    	struct ast_config *config;
    	RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
    	RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = "configuration_file_wizard_with_criteria";
    		info->category = "/main/sorcery/";
    		info->summary = "sorcery configuration file wizard with criteria unit test";
    		info->description =
    			"Test the configuration file wizard with criteria matching in sorcery";
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    
    	if (!(config = ast_config_load2("test_sorcery.conf", "test_sorcery", flags))) {
    		ast_test_status_update(test, "Test sorcery configuration file wizard file not present - skipping configuration_file_wizard_with_criteria test\n");
    		return AST_TEST_NOT_RUN;
    	}
    
    	ast_config_destroy(config);
    
    	if (!(sorcery = ast_sorcery_open())) {
    		ast_test_status_update(test, "Failed to open sorcery structure\n");
    		return AST_TEST_FAIL;
    	}
    
    
    	if (ast_sorcery_apply_default(sorcery, "test", "config", "test_sorcery.conf,criteria=type=zombies") != AST_SORCERY_APPLY_SUCCESS) {
    
    		ast_test_status_update(test, "Could not set a default wizard of the 'config' type, so skipping since it may not be loaded\n");
    		return AST_TEST_NOT_RUN;
    	}
    
    
    Matthew Jordan's avatar
    Matthew Jordan committed
    	if (ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL)) {
    
    		ast_test_status_update(test, "Failed to register object type\n");
    		return AST_TEST_FAIL;
    	}
    
    
    Matthew Jordan's avatar
    Matthew Jordan committed
    	ast_sorcery_object_field_register_nodoc(sorcery, "test", "bob", "5", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, bob));
    	ast_sorcery_object_field_register_nodoc(sorcery, "test", "joe", "10", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, joe));
    	ast_sorcery_object_field_register_nodoc(sorcery, "test", "type", NULL, OPT_NOOP_T, 0, NULL);
    
    
    	ast_sorcery_load(sorcery);
    
    	if ((obj = ast_sorcery_retrieve_by_id(sorcery, "test", "hey"))) {
    		ast_test_status_update(test, "Retrieved object which did not match criteria\n");
    		return AST_TEST_FAIL;
    	} else if (!(obj = ast_sorcery_retrieve_by_id(sorcery, "test", "hey2"))) {
    		ast_test_status_update(test, "Failed to retrieve a known object which matches criteria\n");
    		return AST_TEST_FAIL;
    	}
    
    	return AST_TEST_PASS;
    }
    
    AST_TEST_DEFINE(configuration_file_wizard_retrieve_field)
    {
    	struct ast_flags flags = { CONFIG_FLAG_NOCACHE };
    	struct ast_config *config;
    	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 *, fields, ast_variable_new("joe", "41", ""), ast_variables_destroy);
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = "configuration_file_wizard_retrieve_field";
    		info->category = "/main/sorcery/";
    		info->summary = "sorcery configuration file wizard field retrieval unit test";
    		info->description =
    			"Test the configuration file wizard retrieval using field in sorcery";
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    
    	if (!(config = ast_config_load2("test_sorcery.conf", "test_sorcery", flags))) {
    		ast_test_status_update(test, "Test sorcery configuration file wizard file not present - skipping configuration_file_wizard_retrieve_field test\n");
    		return AST_TEST_NOT_RUN;
    	}
    
    	ast_config_destroy(config);
    
    	if (!(sorcery = ast_sorcery_open())) {
    		ast_test_status_update(test, "Failed to open sorcery structure\n");
    		return AST_TEST_FAIL;
    	}
    
    
    	if (ast_sorcery_apply_default(sorcery, "test", "config", "test_sorcery.conf") != AST_SORCERY_APPLY_SUCCESS) {
    
    		ast_test_status_update(test, "Could not set a default wizard of the 'config' type, so skipping since it may not be loaded\n");
    		return AST_TEST_NOT_RUN;
    	}
    
    
    Matthew Jordan's avatar
    Matthew Jordan committed
    	if (ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL)) {
    
    		ast_test_status_update(test, "Failed to register object type\n");
    		return AST_TEST_FAIL;
    	}
    
    
    Matthew Jordan's avatar
    Matthew Jordan committed
    	ast_sorcery_object_field_register_nodoc(sorcery, "test", "bob", "5", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, bob));
    	ast_sorcery_object_field_register_nodoc(sorcery, "test", "joe", "10", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, joe));
    
    
    	ast_sorcery_load(sorcery);
    
    	if (!(obj = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_DEFAULT, fields))) {
    		ast_test_status_update(test, "Failed to retrieve a known object that has been configured with the correct field\n");
    		return AST_TEST_FAIL;
    	} else if (strcmp(ast_sorcery_object_get_id(obj), "hey")) {
    		ast_test_status_update(test, "Retrieved object has incorrect object id of '%s'\n", ast_sorcery_object_get_id(obj));
    		return AST_TEST_FAIL;
    	}
    
    	return AST_TEST_PASS;
    }
    
    AST_TEST_DEFINE(configuration_file_wizard_retrieve_multiple)
    {
    	struct ast_flags flags = { CONFIG_FLAG_NOCACHE };
    	struct ast_config *config;
    	RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
    	RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup);
    	RAII_VAR(struct ast_variable *, fields, ast_variable_new("joe", "99", ""), ast_variables_destroy);
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = "configuration_file_wizard_retrieve_multiple";
    		info->category = "/main/sorcery/";
    		info->summary = "sorcery configuration file wizard multiple retrieval unit test";
    		info->description =
    			"Test the configuration file wizard multiple retrieval in sorcery";
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    
    	if (!(config = ast_config_load2("test_sorcery.conf", "test_sorcery", flags))) {
    		ast_test_status_update(test, "Test sorcery configuration file wizard file not present - skipping configuration_file_wizard_retrieve_multiple test\n");
    		return AST_TEST_NOT_RUN;
    	}
    
    	ast_config_destroy(config);
    
    	if (!fields) {
    		ast_test_status_update(test, "Failed to create fields for multiple retrieve\n");
    		return AST_TEST_FAIL;
    	}
    
    	if (!(sorcery = ast_sorcery_open())) {
    		ast_test_status_update(test, "Failed to open sorcery structure\n");
    		return AST_TEST_FAIL;
    	}
    
    
    	if (ast_sorcery_apply_default(sorcery, "test", "config", "test_sorcery.conf") != AST_SORCERY_APPLY_SUCCESS) {
    
    		ast_test_status_update(test, "Could not set a default wizard of the 'config' type, so skipping since it may not be loaded\n");
    		return AST_TEST_NOT_RUN;
    	}
    
    
    Matthew Jordan's avatar
    Matthew Jordan committed
    	if (ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL)) {
    
    		ast_test_status_update(test, "Failed to register object type\n");
    		return AST_TEST_FAIL;
    	}
    
    
    Matthew Jordan's avatar
    Matthew Jordan committed
    	ast_sorcery_object_field_register_nodoc(sorcery, "test", "bob", "5", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, bob));
    	ast_sorcery_object_field_register_nodoc(sorcery, "test", "joe", "10", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, joe));
    
    
    	ast_sorcery_load(sorcery);
    
    	if (!(objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE, fields))) {
    		ast_test_status_update(test, "Failed to retrieve an empty container when retrieving multiple\n");
    		return AST_TEST_FAIL;
    	} else if (ao2_container_count(objects)) {
    		ast_test_status_update(test, "Received a container with objects when there should be none in it\n");
    		return AST_TEST_FAIL;
    	}
    
    	ao2_cleanup(objects);
    	ast_variables_destroy(fields);
    
    	if (!(fields = ast_variable_new("joe", "41", ""))) {
    		ast_test_status_update(test, "Failed to create fields for multiple retrieve\n");
    		return AST_TEST_FAIL;
    	} else if (!(objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE, fields))) {
    		ast_test_status_update(test, "Failed to retrieve a container when retrieving multiple\n");
    		return AST_TEST_FAIL;
    	} else if (ao2_container_count(objects) != 1) {
    		ast_test_status_update(test, "Received a container with no objects in it when there should be\n");
    		return AST_TEST_FAIL;
    	}
    
    	return AST_TEST_PASS;
    }
    
    AST_TEST_DEFINE(configuration_file_wizard_retrieve_multiple_all)
    {
    	struct ast_flags flags = { CONFIG_FLAG_NOCACHE };
    	struct ast_config *config;
    	RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
    	RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup);
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = "configuration_file_wizard_retrieve_multiple_all";
    		info->category = "/main/sorcery/";
    		info->summary = "sorcery configuration file wizard multiple retrieve all unit test";
    		info->description =
    			"Test the configuration file wizard multiple retrieve all in sorcery";
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    
    	if (!(config = ast_config_load2("test_sorcery.conf", "test_sorcery", flags))) {
    		ast_test_status_update(test, "Test sorcery configuration file wizard file not present - skipping configuration_file_wizard_retrieve_multiple_all test\n");
    		return AST_TEST_NOT_RUN;
    	}
    
    	ast_config_destroy(config);
    
    	if (!(sorcery = ast_sorcery_open())) {
    		ast_test_status_update(test, "Failed to open sorcery structure\n");
    		return AST_TEST_FAIL;
    	}
    
    
    	if (ast_sorcery_apply_default(sorcery, "test", "config", "test_sorcery.conf") != AST_SORCERY_APPLY_SUCCESS) {
    
    		ast_test_status_update(test, "Could not set a default wizard of the 'config' type, so skipping since it may not be loaded\n");
    		return AST_TEST_NOT_RUN;
    	}
    
    
    Matthew Jordan's avatar
    Matthew Jordan committed
    	if (ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL)) {
    
    		ast_test_status_update(test, "Failed to register object type\n");
    		return AST_TEST_FAIL;
    	}
    
    
    Matthew Jordan's avatar
    Matthew Jordan committed
    	ast_sorcery_object_field_register_nodoc(sorcery, "test", "bob", "5", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, bob));
    	ast_sorcery_object_field_register_nodoc(sorcery, "test", "joe", "10", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, joe));
    
    
    	ast_sorcery_load(sorcery);
    
    	if (!(objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL))) {
    		ast_test_status_update(test, "Failed to retrieve a container with all objects when there should be one\n");
    		return AST_TEST_FAIL;
    	} else if (ao2_container_count(objects) != 2) {
    		ast_test_status_update(test, "Returned container does not have the correct number of objects in it\n");
    		return AST_TEST_FAIL;
    	}
    
    	return AST_TEST_PASS;
    }
    
    
    AST_TEST_DEFINE(dialplan_function)
    {
    	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);
    	struct ast_str *buf;
    	char expression[256];
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = "dialplan_function";
    		info->category = "/main/sorcery/";
    		info->summary = "AST_SORCERY dialplan function";
    		info->description =
    			"Test the AST_SORCERY dialplan function";
    		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 (ast_sorcery_create(sorcery, obj)) {
    		ast_test_status_update(test, "Failed to create a known object type\n");
    		return AST_TEST_FAIL;
    	}
    
    	if (!(buf = ast_str_create(16))) {
    		ast_test_status_update(test, "Failed to allocate return buffer\n");
    		return AST_TEST_FAIL;
    	}
    
    	ast_str_reset(buf);
    	snprintf(expression, sizeof(expression), "AST_SORCERY(%s,%s,%s,%s)", "notest_sorcery", "test", "blah", "bob");
    	if (!ast_func_read2(NULL, expression, &buf, 16)) {
    		ast_free(buf);
    		ast_test_status_update(test, "Retrieved a non-existent module\n");
    		return AST_TEST_FAIL;
    	}
    
    	ast_str_reset(buf);
    	snprintf(expression, sizeof(expression), "AST_SORCERY(%s,%s,%s,%s)", "test_sorcery", "notest", "blah", "bob");
    	if (!ast_func_read2(NULL, expression, &buf, 16)) {
    		ast_free(buf);
    		ast_test_status_update(test, "Retrieved a non-existent type\n");
    		return AST_TEST_FAIL;
    	}
    
    	ast_str_reset(buf);
    	snprintf(expression, sizeof(expression), "AST_SORCERY(%s,%s,%s,%s)", "test_sorcery", "test", "noid", "bob");
    	if (!ast_func_read2(NULL, expression, &buf, 16)) {
    		ast_free(buf);
    		ast_test_status_update(test, "Retrieved a non-existent id\n");
    		return AST_TEST_FAIL;
    	}