a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher/*
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher Authors:
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher Jakub Hrozek <jhrozek@redhat.com>
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher Copyright (C) 2013 Red Hat
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher SSSD tests: InfoPipe responder
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher This program is free software; you can redistribute it and/or modify
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher it under the terms of the GNU General Public License as published by
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher the Free Software Foundation; either version 3 of the License, or
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher (at your option) any later version.
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher This program is distributed in the hope that it will be useful,
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek but WITHOUT ANY WARRANTY; without even the implied warranty of
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek GNU General Public License for more details.
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek You should have received a copy of the GNU General Public License
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek along with this program. If not, see <http://www.gnu.org/licenses/>.
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek*/
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek#include <popt.h>
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek#include "db/sysdb.h"
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek#include "tests/cmocka/common_mock.h"
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek#include "tests/cmocka/common_mock_resp.h"
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek#include "responder/ifp/ifp_private.h"
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek#include "sbus/sssd_dbus_private.h"
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek/* dbus library checks for valid object paths when unit testing, we don't
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek * want that */
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek#undef DBUS_TYPE_OBJECT_PATH
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek#define DBUS_TYPE_OBJECT_PATH ((int) 's')
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozekstatic struct ifp_ctx *
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozekmock_ifp_ctx(TALLOC_CTX *mem_ctx)
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek{
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek struct ifp_ctx *ifp_ctx;
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek ifp_ctx = talloc_zero(mem_ctx, struct ifp_ctx);
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek assert_non_null(ifp_ctx);
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek ifp_ctx->rctx = mock_rctx(ifp_ctx, NULL, NULL, NULL);
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek assert_non_null(ifp_ctx->rctx);
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek ifp_ctx->rctx->allowed_uids = talloc_array(ifp_ctx->rctx, uint32_t, 1);
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek assert_non_null(ifp_ctx->rctx->allowed_uids);
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek ifp_ctx->rctx->allowed_uids[0] = geteuid();
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek ifp_ctx->rctx->allowed_uids_count = 1;
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek ifp_ctx->sysbus = talloc_zero(ifp_ctx, struct sysbus_ctx);
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek assert_non_null(ifp_ctx->sysbus);
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek ifp_ctx->sysbus->conn = talloc_zero(ifp_ctx, struct sbus_connection);
3dfa09a826e5f63b4948462c2452937fc329834dJakub Hrozek assert_non_null(ifp_ctx->sysbus->conn);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher return ifp_ctx;
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher}
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagherstatic struct sbus_request *
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallaghermock_sbus_request(TALLOC_CTX *mem_ctx, uid_t client)
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher{
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher struct sbus_request *sr;
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher sr = talloc_zero(mem_ctx, struct sbus_request);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_non_null(sr);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher sr->conn = talloc_zero(sr, struct sbus_connection);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_non_null(sr->conn);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher sr->message = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_non_null(sr->message);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher dbus_message_set_serial(sr->message, 1);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher sr->client = client;
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher return sr;
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher}
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallaghervoid ifp_test_req_create(void **state)
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher{
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher struct ifp_req *ireq;
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher struct sbus_request *sr;
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher struct ifp_ctx *ifp_ctx;
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher errno_t ret;
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_true(leak_check_setup());
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher ifp_ctx = mock_ifp_ctx(global_talloc_context);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_non_null(ifp_ctx);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher check_leaks_push(ifp_ctx);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher sr = mock_sbus_request(ifp_ctx, geteuid());
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_non_null(sr);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher check_leaks_push(sr);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher ret = ifp_req_create(sr, ifp_ctx, &ireq);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_int_equal(ret, EOK);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher talloc_free(ireq);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_true(check_leaks_pop(sr) == true);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher talloc_free(sr);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_true(check_leaks_pop(ifp_ctx) == true);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher talloc_free(ifp_ctx);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_true(leak_check_teardown());
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher}
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallaghervoid ifp_test_req_wrong_uid(void **state)
3955667b6e5071cc1264422cb9d702534cf9bc21Michal Židek{
3955667b6e5071cc1264422cb9d702534cf9bc21Michal Židek struct ifp_req *ireq;
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher struct sbus_request *sr;
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher struct ifp_ctx *ifp_ctx;
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher errno_t ret;
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_true(leak_check_setup());
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher ifp_ctx = mock_ifp_ctx(global_talloc_context);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_non_null(ifp_ctx);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher check_leaks_push(ifp_ctx);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher sr = mock_sbus_request(ifp_ctx, geteuid()+1);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_non_null(sr);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher ret = ifp_req_create(sr, ifp_ctx, &ireq);
724426603e727bdbf08fe3fb2637f6f5d7d0881eOndrej Kos assert_int_equal(ret, EACCES);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher talloc_free(sr);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_true(check_leaks_pop(ifp_ctx) == true);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher talloc_free(ifp_ctx);
724426603e727bdbf08fe3fb2637f6f5d7d0881eOndrej Kos
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_true(leak_check_teardown());
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher}
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
ceb40cb8846ff755f841466908954087f927eae7Jakub Hrozekvoid test_el_to_dict(void **state)
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher{
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher static struct sbus_request *sr;
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher dbus_bool_t dbret;
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher DBusMessageIter iter;
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher DBusMessageIter iter_dict;
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher struct ldb_message_element *el;
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher errno_t ret;
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher char *attr_name;
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher char *attr_val;
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher sr = mock_sbus_request(global_talloc_context, geteuid());
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_non_null(sr);
724426603e727bdbf08fe3fb2637f6f5d7d0881eOndrej Kos
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher el = talloc(sr, struct ldb_message_element);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_non_null(el);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher el->name = "numbers";
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher el->values = talloc_array(el, struct ldb_val, 2);
724426603e727bdbf08fe3fb2637f6f5d7d0881eOndrej Kos assert_non_null(el->values);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher el->num_values = 2;
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher el->values[0].data = (uint8_t *) discard_const("one");
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher el->values[0].length = strlen("one") + 1;
67ca9e7c006d8619f446c018eabf29eab1368ba5Ondrej Kos el->values[1].data = (uint8_t *) discard_const("two");
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher el->values[1].length = strlen("two") + 1;
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher dbus_message_iter_init_append(sr->message, &iter);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher dbret = dbus_message_iter_open_container(
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher &iter, DBUS_TYPE_ARRAY,
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher DBUS_TYPE_STRING_AS_STRING
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher DBUS_TYPE_VARIANT_AS_STRING
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher &iter_dict);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_true(dbret == TRUE);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
13aea9c2b9c48dd614095b4551021868812ba2f0Pavel Reichl ret = ifp_add_ldb_el_to_dict(&iter_dict, el);
13aea9c2b9c48dd614095b4551021868812ba2f0Pavel Reichl assert_int_equal(ret, EOK);
13aea9c2b9c48dd614095b4551021868812ba2f0Pavel Reichl
13aea9c2b9c48dd614095b4551021868812ba2f0Pavel Reichl dbret = dbus_message_iter_close_container(&iter, &iter_dict);
13aea9c2b9c48dd614095b4551021868812ba2f0Pavel Reichl assert_true(dbret == TRUE);
13aea9c2b9c48dd614095b4551021868812ba2f0Pavel Reichl
13aea9c2b9c48dd614095b4551021868812ba2f0Pavel Reichl /* Test the reply contains what we expect */
13aea9c2b9c48dd614095b4551021868812ba2f0Pavel Reichl dbus_message_iter_init(sr->message, &iter);
13aea9c2b9c48dd614095b4551021868812ba2f0Pavel Reichl assert_int_equal(dbus_message_iter_get_arg_type(&iter),
d9de4b26f44a344025bbfa23104b7b67935fae35Pavel Reichl DBUS_TYPE_ARRAY);
d9de4b26f44a344025bbfa23104b7b67935fae35Pavel Reichl dbus_message_iter_recurse(&iter, &iter);
d9de4b26f44a344025bbfa23104b7b67935fae35Pavel Reichl assert_int_equal(dbus_message_iter_get_arg_type(&iter),
13aea9c2b9c48dd614095b4551021868812ba2f0Pavel Reichl DBUS_TYPE_DICT_ENTRY);
13aea9c2b9c48dd614095b4551021868812ba2f0Pavel Reichl
13aea9c2b9c48dd614095b4551021868812ba2f0Pavel Reichl dbus_message_iter_recurse(&iter, &iter_dict);
13aea9c2b9c48dd614095b4551021868812ba2f0Pavel Reichl dbus_message_iter_get_basic(&iter_dict, &attr_name);
13aea9c2b9c48dd614095b4551021868812ba2f0Pavel Reichl assert_string_equal(attr_name, "numbers");
13aea9c2b9c48dd614095b4551021868812ba2f0Pavel Reichl
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher dbus_message_iter_next(&iter_dict);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_int_equal(dbus_message_iter_get_arg_type(&iter_dict),
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher DBUS_TYPE_VARIANT);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher dbus_message_iter_recurse(&iter_dict, &iter_dict);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_int_equal(dbus_message_iter_get_arg_type(&iter_dict),
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher DBUS_TYPE_ARRAY);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher dbus_message_iter_recurse(&iter_dict, &iter_dict);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher dbus_message_iter_get_basic(&iter_dict, &attr_val);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_string_equal(attr_val, "one");
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_true(dbus_message_iter_next(&iter_dict));
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher dbus_message_iter_get_basic(&iter_dict, &attr_val);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_string_equal(attr_val, "two");
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_false(dbus_message_iter_next(&iter_dict));
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher talloc_free(sr);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher}
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagherstatic void assert_string_list_equal(const char **s1,
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher const char **s2)
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher{
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher int i;
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher for (i=0; s1[i]; i++) {
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_non_null(s2[i]);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_string_equal(s1[i], s2[i]);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher }
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_null(s2[i]);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher}
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagherstatic void attr_parse_test(const char *expected[], const char *input)
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher{
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher const char **res;
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher TALLOC_CTX *test_ctx;
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher test_ctx = talloc_new(NULL);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_non_null(test_ctx);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher res = ifp_parse_user_attr_list(test_ctx, input);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher if (expected) {
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher /* Positive test */
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_non_null(res);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_string_list_equal(res, expected);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher } else {
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher /* Negative test */
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher assert_null(res);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher }
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher talloc_free(test_ctx);
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher}
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagherstatic void attr_parse_test_ex(const char *expected[], const char *input,
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher const char **defaults)
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher{
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher const char **res;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl TALLOC_CTX *test_ctx;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl test_ctx = talloc_new(NULL);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl assert_non_null(test_ctx);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl res = parse_attr_list_ex(test_ctx, input, defaults);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (expected) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl /* Positive test */
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl assert_non_null(res);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl assert_string_list_equal(res, expected);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl } else {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl /* Negative test */
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl assert_null(res);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl talloc_free(test_ctx);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl}
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichlvoid test_attr_acl(void **state)
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher{
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher /* Test defaults */
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher const char *exp_defaults[] = { SYSDB_NAME, SYSDB_UIDNUM,
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher SYSDB_GIDNUM, SYSDB_GECOS,
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher SYSDB_HOMEDIR, SYSDB_SHELL,
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose "groups", "domain", "domainname", NULL };
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose attr_parse_test(exp_defaults, NULL);
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose /* Test adding some attributes to the defaults */
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose const char *exp_add[] = { "telephoneNumber", "streetAddress",
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose SYSDB_NAME, SYSDB_UIDNUM,
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose SYSDB_GIDNUM, SYSDB_GECOS,
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose SYSDB_HOMEDIR, SYSDB_SHELL,
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose "groups", "domain", "domainname", NULL };
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose attr_parse_test(exp_add, "+telephoneNumber, +streetAddress");
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose /* Test removing some attributes to the defaults */
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose const char *exp_rm[] = { SYSDB_NAME,
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose SYSDB_GIDNUM, SYSDB_GECOS,
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose SYSDB_HOMEDIR, "groups",
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose "domain", "domainname",
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose NULL };
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose attr_parse_test(exp_rm, "-"SYSDB_SHELL ",-"SYSDB_UIDNUM);
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose /* Test both add and remove */
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose const char *exp_add_rm[] = { "telephoneNumber",
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose SYSDB_NAME, SYSDB_UIDNUM,
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose SYSDB_GIDNUM, SYSDB_GECOS,
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose SYSDB_HOMEDIR, "groups",
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose "domain", "domainname",
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose NULL };
781b04eb4b1d103120d736b81f1a74ea76a72947Jakub Hrozek attr_parse_test(exp_add_rm, "+telephoneNumber, -"SYSDB_SHELL);
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose /* Test rm trumps add */
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose const char *exp_add_rm_override[] = { SYSDB_NAME, SYSDB_UIDNUM,
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose SYSDB_GIDNUM, SYSDB_GECOS,
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose SYSDB_HOMEDIR, SYSDB_SHELL,
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose "groups", "domain",
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose "domainname", NULL };
36c266d467e9105041b33e9b1cdcd9ff073d893eSumit Bose attr_parse_test(exp_add_rm_override,
a23919ed39d212f9f5694d9b103c84641fdb7680Stephen Gallagher "+telephoneNumber, -telephoneNumber, +telephoneNumber");
/* Remove all */
const char *rm_all[] = { NULL };
attr_parse_test(rm_all, "-"SYSDB_NAME ", -"SYSDB_UIDNUM
", -"SYSDB_GIDNUM ", -"SYSDB_GECOS
", -"SYSDB_HOMEDIR ", -"SYSDB_SHELL", -groups, "
"-domain, -domainname");
/* Malformed list */
attr_parse_test(NULL, "missing_plus_or_minus");
}
void test_attr_acl_ex(void **state)
{
/* Test defaults */
const char *exp_defaults[] = { "abc", "123", "xyz", NULL };
attr_parse_test_ex(exp_defaults, NULL, exp_defaults);
/* Test adding some attributes to the defaults */
const char *exp_add[] = { "telephoneNumber", "streetAddress",
"abc", "123", "xyz",
NULL };
attr_parse_test_ex(exp_add, "+telephoneNumber, +streetAddress",
exp_defaults);
/* Test removing some attributes to the defaults */
const char *exp_rm[] = { "123", NULL };
attr_parse_test_ex(exp_rm, "-abc, -xyz", exp_defaults);
/* Test adding with empty defaults */
const char *exp_add_empty[] = { "telephoneNumber", "streetAddress",
NULL };
attr_parse_test_ex(exp_add_empty, "+telephoneNumber, +streetAddress", NULL);
/* Test removing with empty defaults */
const char *rm_all[] = { NULL };
attr_parse_test_ex(rm_all, "-telephoneNumber, -streetAddress", NULL);
}
void test_attr_allowed(void **state)
{
const char *whitelist[] = { "name", "gecos", NULL };
const char *emptylist[] = { NULL };
assert_true(ifp_attr_allowed(whitelist, "name"));
assert_true(ifp_attr_allowed(whitelist, "gecos"));
assert_false(ifp_attr_allowed(whitelist, "password"));
assert_false(ifp_attr_allowed(emptylist, "name"));
assert_false(ifp_attr_allowed(NULL, "name"));
}
struct ifp_test_req_ctx {
struct ifp_req *ireq;
struct sbus_request *sr;
struct ifp_ctx *ifp_ctx;
};
static int ifp_test_req_setup(void **state)
{
struct ifp_test_req_ctx *test_ctx;
errno_t ret;
assert_true(leak_check_setup());
test_ctx = talloc_zero(global_talloc_context, struct ifp_test_req_ctx);
assert_non_null(test_ctx);
test_ctx->ifp_ctx = mock_ifp_ctx(test_ctx);
assert_non_null(test_ctx->ifp_ctx);
test_ctx->sr = mock_sbus_request(test_ctx, geteuid());
assert_non_null(test_ctx->sr);
ret = ifp_req_create(test_ctx->sr, test_ctx->ifp_ctx, &test_ctx->ireq);
assert_int_equal(ret, EOK);
assert_non_null(test_ctx->ireq);
check_leaks_push(test_ctx);
*state = test_ctx;
return 0;
}
static int ifp_test_req_teardown(void **state)
{
struct ifp_test_req_ctx *test_ctx = talloc_get_type_abort(*state,
struct ifp_test_req_ctx);
assert_true(check_leaks_pop(test_ctx) == true);
dbus_message_unref(test_ctx->sr->message);
talloc_free(test_ctx);
assert_true(leak_check_teardown());
return 0;
}
int main(int argc, const char *argv[])
{
poptContext pc;
int opt;
struct poptOption long_options[] = {
POPT_AUTOHELP
SSSD_DEBUG_OPTS
POPT_TABLEEND
};
const struct CMUnitTest tests[] = {
cmocka_unit_test(ifp_test_req_create),
cmocka_unit_test(ifp_test_req_wrong_uid),
cmocka_unit_test_setup_teardown(test_el_to_dict,
ifp_test_req_setup,
ifp_test_req_teardown),
cmocka_unit_test(test_attr_acl),
cmocka_unit_test(test_attr_acl_ex),
cmocka_unit_test(test_attr_allowed),
};
/* Set debug level to invalid value so we can decide 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_CLI_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 cmocka_run_group_tests(tests, NULL, NULL);
}