summaryrefslogtreecommitdiffstats
path: root/src/tests
diff options
context:
space:
mode:
authorJakub Hrozek <jhrozek@redhat.com>2014-01-06 15:27:44 +0100
committerJakub Hrozek <jhrozek@redhat.com>2014-05-13 21:46:54 +0200
commitf92ace4a52602e8c38a34f2392bec3deeac2dddd (patch)
treefe8e5c7a5666df507048515073e1ddda447283db /src/tests
parent07976be2a09ebb1346c841d4a32e417a8bb43862 (diff)
downloadsssd-f92ace4a52602e8c38a34f2392bec3deeac2dddd.tar.gz
sssd-f92ace4a52602e8c38a34f2392bec3deeac2dddd.tar.xz
sssd-f92ace4a52602e8c38a34f2392bec3deeac2dddd.zip
IFP: Add utility functions
Adds a number of utility functions, most importanly ifp_req_create(). The ifp_req is a structure that will be passed along with the ifp request and would provide easy access to both the sbus_request data and per-responder data, like the ifp_ctx. Also includes a utility function to split a path prefix from a full path and add a ldb_element into a dictionary. These will be reused later. Reviewed-by: Pavel Březina <pbrezina@redhat.com> Reviewed-by: Stef Walter <stefw@redhat.com>
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/cmocka/test_ifp.c210
1 files changed, 210 insertions, 0 deletions
diff --git a/src/tests/cmocka/test_ifp.c b/src/tests/cmocka/test_ifp.c
new file mode 100644
index 000000000..161f8ffe4
--- /dev/null
+++ b/src/tests/cmocka/test_ifp.c
@@ -0,0 +1,210 @@
+/*
+ Authors:
+ Jakub Hrozek <jhrozek@redhat.com>
+
+ Copyright (C) 2013 Red Hat
+
+ SSSD tests: InfoPipe responder
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <popt.h>
+
+#include "db/sysdb.h"
+#include "tests/cmocka/common_mock.h"
+#include "responder/ifp/ifp_private.h"
+#include "sbus/sssd_dbus_private.h"
+
+static struct ifp_ctx *
+mock_ifp_ctx(TALLOC_CTX *mem_ctx)
+{
+ struct ifp_ctx *ifp_ctx;
+
+ ifp_ctx = talloc_zero(mem_ctx, struct ifp_ctx);
+ assert_non_null(ifp_ctx);
+
+ ifp_ctx->sysbus = talloc_zero(ifp_ctx, struct sysbus_ctx);
+ assert_non_null(ifp_ctx->sysbus);
+
+ ifp_ctx->sysbus->conn = talloc_zero(ifp_ctx, struct sbus_connection);
+ assert_non_null(ifp_ctx->sysbus->conn);
+
+ return ifp_ctx;
+}
+
+static struct sbus_request *
+mock_sbus_request(TALLOC_CTX *mem_ctx)
+{
+ struct sbus_request *sr;
+
+ sr = talloc_zero(mem_ctx, struct sbus_request);
+ assert_non_null(sr);
+
+ sr->conn = talloc_zero(sr, struct sbus_connection);
+ assert_non_null(sr->conn);
+
+ sr->message = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
+ assert_non_null(sr->message);
+ dbus_message_set_serial(sr->message, 1);
+
+ return sr;
+}
+
+void ifp_test_req_create(void **state)
+{
+ struct ifp_req *ireq;
+ struct sbus_request *sr;
+ struct ifp_ctx *ifp_ctx;
+ errno_t ret;
+
+ assert_true(leak_check_setup());
+
+ ifp_ctx = mock_ifp_ctx(global_talloc_context);
+ assert_non_null(ifp_ctx);
+ check_leaks_push(ifp_ctx);
+
+ sr = mock_sbus_request(ifp_ctx);
+ assert_non_null(sr);
+ check_leaks_push(sr);
+
+ ret = ifp_req_create(sr, ifp_ctx, &ireq);
+ assert_int_equal(ret, EOK);
+ talloc_free(ireq);
+
+ assert_true(check_leaks_pop(sr) == true);
+ talloc_free(sr);
+
+ assert_true(check_leaks_pop(ifp_ctx) == true);
+ talloc_free(ifp_ctx);
+
+ assert_true(leak_check_teardown());
+}
+
+void test_path_prefix(void **state)
+{
+ const char *prefix = "foo";
+
+ assert_non_null(ifp_path_strip_prefix("foobar", prefix));
+ assert_null(ifp_path_strip_prefix("notfoo", prefix));
+}
+
+void test_el_to_dict(void **state)
+{
+ static struct sbus_request *sr;
+ dbus_bool_t dbret;
+ DBusMessageIter iter;
+ DBusMessageIter iter_dict;
+ struct ldb_message_element *el;
+ errno_t ret;
+ char *attr_name;
+ char *attr_val;
+
+ sr = mock_sbus_request(global_talloc_context);
+ assert_non_null(sr);
+
+ el = talloc(sr, struct ldb_message_element);
+ assert_non_null(el);
+ el->name = "numbers";
+ el->values = talloc_array(el, struct ldb_val, 2);
+ assert_non_null(el->values);
+ el->num_values = 2;
+ el->values[0].data = (uint8_t *) discard_const("one");
+ el->values[0].length = strlen("one") + 1;
+ el->values[1].data = (uint8_t *) discard_const("two");
+ el->values[1].length = strlen("two") + 1;
+
+ dbus_message_iter_init_append(sr->message, &iter);
+ dbret = dbus_message_iter_open_container(
+ &iter, DBUS_TYPE_ARRAY,
+ DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
+ DBUS_TYPE_STRING_AS_STRING
+ DBUS_TYPE_VARIANT_AS_STRING
+ DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
+ &iter_dict);
+ assert_true(dbret == TRUE);
+
+ ret = ifp_add_ldb_el_to_dict(&iter_dict, el);
+ assert_int_equal(ret, EOK);
+
+ dbret = dbus_message_iter_close_container(&iter, &iter_dict);
+ assert_true(dbret == TRUE);
+
+ /* Test the reply contains what we expect */
+ dbus_message_iter_init(sr->message, &iter);
+ assert_int_equal(dbus_message_iter_get_arg_type(&iter),
+ DBUS_TYPE_ARRAY);
+ dbus_message_iter_recurse(&iter, &iter);
+ assert_int_equal(dbus_message_iter_get_arg_type(&iter),
+ DBUS_TYPE_DICT_ENTRY);
+
+ dbus_message_iter_recurse(&iter, &iter_dict);
+ dbus_message_iter_get_basic(&iter_dict, &attr_name);
+ assert_string_equal(attr_name, "numbers");
+
+ dbus_message_iter_next(&iter_dict);
+ assert_int_equal(dbus_message_iter_get_arg_type(&iter_dict),
+ DBUS_TYPE_VARIANT);
+ dbus_message_iter_recurse(&iter_dict, &iter_dict);
+ assert_int_equal(dbus_message_iter_get_arg_type(&iter_dict),
+ DBUS_TYPE_ARRAY);
+
+ dbus_message_iter_recurse(&iter_dict, &iter_dict);
+ dbus_message_iter_get_basic(&iter_dict, &attr_val);
+ assert_string_equal(attr_val, "one");
+ assert_true(dbus_message_iter_next(&iter_dict));
+ dbus_message_iter_get_basic(&iter_dict, &attr_val);
+ assert_string_equal(attr_val, "two");
+ assert_false(dbus_message_iter_next(&iter_dict));
+}
+
+int main(int argc, const char *argv[])
+{
+ poptContext pc;
+ int opt;
+ struct poptOption long_options[] = {
+ POPT_AUTOHELP
+ SSSD_DEBUG_OPTS
+ POPT_TABLEEND
+ };
+
+ const UnitTest tests[] = {
+ unit_test(ifp_test_req_create),
+ unit_test(test_path_prefix),
+ unit_test(test_el_to_dict),
+ };
+
+ /* Set debug level to invalid value so we can deside if -d 0 was used. */
+ debug_level = SSSDBG_INVALID;
+
+ pc = poptGetContext(argv[0], argc, argv, long_options, 0);
+ while((opt = poptGetNextOpt(pc)) != -1) {
+ switch(opt) {
+ default:
+ fprintf(stderr, "\nInvalid option %s: %s\n\n",
+ poptBadOption(pc, 0), poptStrerror(opt));
+ poptPrintUsage(pc, stderr, 0);
+ return 1;
+ }
+ }
+ poptFreeContext(pc);
+
+ DEBUG_INIT(debug_level);
+
+ /* Even though normally the tests should clean up after themselves
+ * they might not after a failed run. Remove the old db to be sure */
+ tests_set_cwd();
+
+ return run_tests(tests);
+}