b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina/*
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina Copyright (C) 2015 Red Hat
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina This program is free software; you can redistribute it and/or modify
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina it under the terms of the GNU General Public License as published by
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina the Free Software Foundation; either version 3 of the License, or
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina (at your option) any later version.
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina This program is distributed in the hope that it will be useful,
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina but WITHOUT ANY WARRANTY; without even the implied warranty of
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina GNU General Public License for more details.
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina You should have received a copy of the GNU General Public License
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina along with this program. If not, see <http://www.gnu.org/licenses/>.
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina*/
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina#include <talloc.h>
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina#include <tevent.h>
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina#include <errno.h>
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina#include <popt.h>
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina#include "providers/data_provider/dp_private.h"
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina#include "tests/cmocka/common_mock.h"
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina#include "tests/common.h"
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březinastruct test_ctx {
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina hash_table_t *table;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina};
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březinastatic int test_setup(void **state)
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina{
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina struct test_ctx *test_ctx = NULL;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina errno_t ret;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_true(leak_check_setup());
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina test_ctx = talloc_zero(global_talloc_context, struct test_ctx);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_non_null(test_ctx);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina ret = dp_req_table_init(test_ctx, &test_ctx->table);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_int_equal(ret, EOK);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_non_null(test_ctx->table);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina check_leaks_push(test_ctx);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina *state = test_ctx;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina return 0;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina}
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březinastatic int test_teardown(void **state)
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina{
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina struct test_ctx *test_ctx;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina test_ctx = talloc_get_type_abort(*state, struct test_ctx);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_true(check_leaks_pop(test_ctx));
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina talloc_zfree(test_ctx);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina return 0;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina}
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březinastatic const char *get_req_key(struct test_ctx* test_ctx)
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina{
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina const char *req_name;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina req_name = dp_req_table_key(test_ctx,
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina DPT_ID, DPM_ACCOUNT_HANDLER,
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina DP_FAST_REPLY,
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina "custom_part");
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_non_null(req_name);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina return req_name;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina}
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březinastatic void test_add_del_req(void **state)
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina{
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina errno_t ret;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina bool is_present;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina const char *key;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina struct dp_table_value *tv;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina struct dp_table_value *tv2;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina struct sbus_request *sbus_req;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina struct test_ctx *test_ctx = talloc_get_type(*state, struct test_ctx);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina struct tevent_req *req;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina req = tevent_req_create(test_ctx, &state, struct test_ctx);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_non_null(req);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina key = get_req_key(test_ctx);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina is_present = dp_req_table_has_key(test_ctx->table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_false(is_present);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina sbus_req = talloc(test_ctx, struct sbus_request);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_non_null(sbus_req);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina ret = dp_req_table_add(test_ctx->table, key, req, sbus_req);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_int_equal(ret, EOK);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina is_present = dp_req_table_has_key(test_ctx->table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_true(is_present);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina tv = dp_req_table_lookup(test_ctx->table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_non_null(tv);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina dp_req_table_del(test_ctx->table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina tv2 = dp_req_table_lookup(test_ctx->table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_null(tv2);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina is_present = dp_req_table_has_key(test_ctx->table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_false(is_present);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina talloc_free(discard_const(key));
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina talloc_free(tv);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina talloc_free(sbus_req);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina talloc_free(req);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina}
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březinastatic void test_del_non_present_req(void **state)
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina{
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina bool is_present;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina hash_table_t *table;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina const char *key;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina struct test_ctx *test_ctx = talloc_get_type(*state, struct test_ctx);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina table = test_ctx->table;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina key = get_req_key(test_ctx);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina is_present = dp_req_table_has_key(table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_false(is_present);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina dp_req_table_del(table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina is_present = dp_req_table_has_key(table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_false(is_present);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina talloc_free(discard_const(key));
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina}
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březinastatic void test_mult_req(void **state)
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina{
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina errno_t ret;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina bool is_present;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina hash_table_t *table;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina const char *key;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina struct sbus_request *sbus_req;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina struct sbus_request *sbus_req2;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina struct dp_table_value *tv;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina struct dp_table_value *tv2;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina struct test_ctx *test_ctx = talloc_get_type(*state, struct test_ctx);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina struct tevent_req *req;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina req = tevent_req_create(test_ctx, &state, struct test_ctx);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_non_null(req);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina table = test_ctx->table;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina key = get_req_key(test_ctx);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
57c5ea8825c7179fd93382dbcbb07e828e5aec19René Genz /* Add 1st request */
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina is_present = dp_req_table_has_key(table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_false(is_present);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina sbus_req = talloc(test_ctx, struct sbus_request);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_non_null(sbus_req);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina ret = dp_req_table_add(table, key, req, sbus_req);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_int_equal(ret, EOK);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina is_present = dp_req_table_has_key(table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_true(is_present);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina tv = dp_req_table_lookup(table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_non_null(tv);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_ptr_equal(tv->req, req);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_ptr_equal(tv->list->sbus_req, sbus_req);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina /* Add 2nd request */
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina is_present = dp_req_table_has_key(table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_true(is_present);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina sbus_req2 = talloc(test_ctx, struct sbus_request);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_non_null(sbus_req2);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina ret = dp_req_table_add(table, key, NULL, sbus_req2);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_int_equal(ret, EOK);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina is_present = dp_req_table_has_key(table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_true(is_present);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina tv = dp_req_table_lookup(table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_non_null(tv);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_ptr_equal(tv->req, req);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_ptr_equal(tv->list->sbus_req, sbus_req2);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_non_null(tv->list->next);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_ptr_equal(tv->list->next->sbus_req, sbus_req);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina /* Del req */
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina dp_req_table_del(table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina is_present = dp_req_table_has_key(table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_false(is_present);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina tv2 = dp_req_table_lookup(table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_null(tv2);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina /* Free memory */
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina talloc_free(discard_const(key));
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina talloc_free(tv);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina talloc_free(sbus_req);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina talloc_free(sbus_req2);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina talloc_free(req);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina}
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina/* This test is aimed to test 'dp_sbus_req_item_destructor()' */
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březinastatic void test_destructor_req(void **state)
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina{
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina errno_t ret;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina bool is_present;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina hash_table_t *table;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina const char *key;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina struct dp_table_value *tv;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina struct dp_table_value *tv2;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina const int N = 5;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina const int MAGIC = 3;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina struct sbus_request *sbus_req[N];
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina struct test_ctx *test_ctx = talloc_get_type(*state, struct test_ctx);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina struct tevent_req *req;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina req = tevent_req_create(test_ctx, &state, struct test_ctx);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_non_null(req);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina table = test_ctx->table;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina key = get_req_key(test_ctx);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina is_present = dp_req_table_has_key(table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_false(is_present);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina /* Insert N sbus_requests for req_name */
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina for (int i = 0; i < N; i++) {
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina sbus_req[i] = talloc(test_ctx, struct sbus_request);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_non_null(sbus_req[i]);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina ret = dp_req_table_add(table, key, req, sbus_req[i]);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_int_equal(ret, EOK);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina }
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina /* Check */
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina is_present = dp_req_table_has_key(table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_true(is_present);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina tv = dp_req_table_lookup(table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_non_null(tv);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_ptr_equal(tv->req, req);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina struct dp_sbus_req_item *ri = tv->list;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina for (int i = 0; i < N; i++) {
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_ptr_equal(ri->sbus_req, sbus_req[N-i-1]);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina ri = ri->next;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina }
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_null(ri);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina /* Del one req */
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina talloc_free(sbus_req[MAGIC]);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina /* Check that only magic is missing */
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina is_present = dp_req_table_has_key(table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_true(is_present);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina tv = dp_req_table_lookup(table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_non_null(tv);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_ptr_equal(tv->req, req);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina ri = tv->list;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_ptr_equal(ri->sbus_req, sbus_req[N-1]);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina ri = ri->next;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina /* Skip deleted MAGIC request */
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_ptr_equal(ri->sbus_req, sbus_req[N-3]);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina ri = ri->next;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_ptr_equal(ri->sbus_req, sbus_req[N-4]);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina ri = ri->next;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_ptr_equal(ri->sbus_req, sbus_req[N-5]);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina ri = ri->next;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_null(ri);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina /* misc */
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina dp_req_table_del(table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina is_present = dp_req_table_has_key(table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_false(is_present);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina tv2 = dp_req_table_lookup(table, key);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina assert_null(tv2);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina /* Free memory */
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina for (int i = 0; i < N; i++) {
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina if (i != MAGIC) {
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina talloc_free(sbus_req[i]);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina }
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina }
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina talloc_free(discard_const(key));
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina talloc_free(tv);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina talloc_free(req);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina}
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březinaint main(int argc, const char *argv[])
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina{
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina poptContext pc;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina int opt;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina int no_cleanup = 0;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina struct poptOption long_options[] = {
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina POPT_AUTOHELP
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina SSSD_DEBUG_OPTS
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina {"no-cleanup", 'n', POPT_ARG_NONE, &no_cleanup, 0,
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina _("Do not delete the test database after a test run"), NULL },
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina POPT_TABLEEND
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina };
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina const struct CMUnitTest tests[] = {
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina cmocka_unit_test_setup_teardown(test_add_del_req,
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina test_setup,
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina test_teardown),
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina cmocka_unit_test_setup_teardown(test_del_non_present_req,
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina test_setup,
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina test_teardown),
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina cmocka_unit_test_setup_teardown(test_mult_req,
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina test_setup,
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina test_teardown),
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina cmocka_unit_test_setup_teardown(test_destructor_req,
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina test_setup,
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina test_teardown),
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina };
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
57c5ea8825c7179fd93382dbcbb07e828e5aec19René Genz /* Set debug level to invalid value so we can decide if -d 0 was used. */
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina debug_level = SSSDBG_INVALID;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina pc = poptGetContext(argv[0], argc, argv, long_options, 0);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina while((opt = poptGetNextOpt(pc)) != -1) {
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina switch(opt) {
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina default:
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina fprintf(stderr, "\nInvalid option %s: %s\n\n",
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina poptBadOption(pc, 0), poptStrerror(opt));
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina poptPrintUsage(pc, stderr, 0);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina return 1;
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina }
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina }
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina poptFreeContext(pc);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina DEBUG_CLI_INIT(debug_level);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina tests_set_cwd();
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina return cmocka_run_group_tests(tests, NULL, NULL);
b9d83e10cec267ae11fee64a30f42a12bbf7abe4Pavel Březina}