From a5623363d6042290fe652a1ca5ce5a85a821236f Mon Sep 17 00:00:00 2001 From: Pavel Březina Date: Fri, 7 Nov 2014 14:47:12 +0100 Subject: tests: be_ptask MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Resolves: https://fedorahosted.org/sssd/ticket/1939 Reviewed-by: Lukáš Slebodník --- src/tests/cmocka/test_be_ptask.c | 864 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 864 insertions(+) create mode 100644 src/tests/cmocka/test_be_ptask.c (limited to 'src/tests/cmocka') diff --git a/src/tests/cmocka/test_be_ptask.c b/src/tests/cmocka/test_be_ptask.c new file mode 100644 index 000000000..e06cc90c3 --- /dev/null +++ b/src/tests/cmocka/test_be_ptask.c @@ -0,0 +1,864 @@ +/* + Authors: + Pavel Březina + + Copyright (C) 2014 Red Hat + + 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 . +*/ + +#include +#include +#include +#include +#include + +#include "providers/dp_backend.h" +#include "providers/dp_ptask_private.h" +#include "providers/dp_ptask.h" +#include "tests/cmocka/common_mock.h" +#include "tests/common.h" + +#define DELAY 2 +#define PERIOD 10 + +#define new_test(test) \ + unit_test_setup_teardown(test_ ## test, test_setup, test_teardown) + +struct test_ctx { + struct be_ctx *be_ctx; + + time_t when; + bool done; + + bool add_online_cb_called; + bool add_offline_cb_called; +}; + +#define mark_online(test_ctx) do { \ + test_ctx->be_ctx->offstat.went_offline = 0; \ + test_ctx->be_ctx->offstat.offline = false; \ +} while (0) + +#define mark_offline(test_ctx) do { \ + test_ctx->be_ctx->offstat.went_offline = time(NULL); \ + test_ctx->be_ctx->offstat.offline = true; \ +} while (0) + +/* Since both test_ctx->done and ptask->req is marked as finished already + * in the sync _send function before a new execution is scheduled we need to + * rely on the fact that ptask->req is set to zero when a new timer is + * created. This way we guarantee that the condition is true only when + * the ptask is executed and a new one is scheduled. */ +#define is_sync_ptask_finished(test_ctx, ptask) \ + (test_ctx->done && ptask->req == NULL) + +/* Mock few backend functions so we don't have to bring the whole + * data provider into this test. */ + +bool be_is_offline(struct be_ctx *ctx) +{ + return ctx->offstat.offline; +} + +int be_add_online_cb(TALLOC_CTX *mem_ctx, + struct be_ctx *ctx, + be_callback_t cb, + void *pvt, + struct be_cb **online_cb) +{ + struct test_ctx *test_ctx = NULL; + + test_ctx = sss_mock_ptr_type(struct test_ctx *); + test_ctx->add_online_cb_called = true; + + return ERR_OK; +} + +int be_add_offline_cb(TALLOC_CTX *mem_ctx, + struct be_ctx *ctx, + be_callback_t cb, + void *pvt, + struct be_cb **offline_cb) +{ + struct test_ctx *test_ctx = NULL; + + test_ctx = sss_mock_ptr_type(struct test_ctx *); + test_ctx->add_offline_cb_called = true; + + return ERR_OK; +} + +struct test_be_ptask_state { + struct test_ctx *test_ctx; +}; + +struct tevent_req * test_be_ptask_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct be_ctx *be_ctx, + struct be_ptask *be_ptask, + void *pvt) +{ + struct test_be_ptask_state *state = NULL; + struct test_ctx *test_ctx = NULL; + struct tevent_req *req = NULL; + + assert_non_null(ev); + assert_non_null(be_ctx); + assert_non_null(be_ptask); + assert_non_null(pvt); + + test_ctx = talloc_get_type(pvt, struct test_ctx); + assert_non_null(test_ctx); + + test_ctx->when = time(NULL); + + req = tevent_req_create(mem_ctx, &state, struct test_be_ptask_state); + assert_non_null(req); + + state->test_ctx = test_ctx; + + tevent_req_done(req); + tevent_req_post(req, ev); + return req; +} + +struct tevent_req * test_be_ptask_null_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct be_ctx *be_ctx, + struct be_ptask *be_ptask, + void *pvt) +{ + struct test_ctx *test_ctx = NULL; + assert_non_null(ev); + assert_non_null(be_ctx); + assert_non_null(be_ptask); + assert_non_null(pvt); + + test_ctx = talloc_get_type(pvt, struct test_ctx); + assert_non_null(test_ctx); + + test_ctx->when = time(NULL); + test_ctx->done = true; + + return NULL; +} + +struct tevent_req * test_be_ptask_timeout_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct be_ctx *be_ctx, + struct be_ptask *be_ptask, + void *pvt) +{ + struct test_be_ptask_state *state = NULL; + struct test_ctx *test_ctx = NULL; + struct tevent_req *req = NULL; + + assert_non_null(ev); + assert_non_null(be_ctx); + assert_non_null(be_ptask); + assert_non_null(pvt); + + test_ctx = talloc_get_type(pvt, struct test_ctx); + assert_non_null(test_ctx); + + test_ctx->when = time(NULL); + + req = tevent_req_create(mem_ctx, &state, struct test_be_ptask_state); + assert_non_null(req); + + state->test_ctx = test_ctx; + + /* we won't finish the request */ + + return req; +} + +errno_t test_be_ptask_recv(struct tevent_req *req) +{ + struct test_be_ptask_state *state = NULL; + + state = tevent_req_data(req, struct test_be_ptask_state); + assert_non_null(state); + + state->test_ctx->done = true; + + TEVENT_REQ_RETURN_ON_ERROR(req); + + return ERR_OK; +} + +errno_t test_be_ptask_error_recv(struct tevent_req *req) +{ + struct test_be_ptask_state *state = NULL; + + state = tevent_req_data(req, struct test_be_ptask_state); + assert_non_null(state); + + state->test_ctx->done = true; + + return ERR_INTERNAL; +} + +errno_t test_be_ptask_sync(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct be_ctx *be_ctx, + struct be_ptask *be_ptask, + void *pvt) +{ + struct test_ctx *test_ctx = NULL; + + assert_non_null(ev); + assert_non_null(be_ctx); + assert_non_null(be_ptask); + assert_non_null(pvt); + + test_ctx = talloc_get_type(pvt, struct test_ctx); + assert_non_null(test_ctx); + + test_ctx->when = time(NULL); + test_ctx->done = true; + + return ERR_OK; +} + +errno_t test_be_ptask_sync_error(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct be_ctx *be_ctx, + struct be_ptask *be_ptask, + void *pvt) +{ + struct test_ctx *test_ctx = NULL; + + assert_non_null(ev); + assert_non_null(be_ctx); + assert_non_null(be_ptask); + assert_non_null(pvt); + + test_ctx = talloc_get_type(pvt, struct test_ctx); + assert_non_null(test_ctx); + + test_ctx->when = time(NULL); + test_ctx->done = true; + + return ERR_INTERNAL; +} + +void test_setup(void **state) +{ + struct test_ctx *test_ctx = NULL; + + assert_true(leak_check_setup()); + + test_ctx = talloc_zero(global_talloc_context, struct test_ctx); + assert_non_null(test_ctx); + + /* create be_ctx, only ev and offline field should be used */ + test_ctx->be_ctx = talloc_zero(test_ctx, struct be_ctx); + assert_non_null(test_ctx->be_ctx); + + test_ctx->be_ctx->ev = tevent_context_init(test_ctx->be_ctx); + assert_non_null(test_ctx->be_ctx->ev); + + *state = test_ctx; +} + +void test_teardown(void **state) +{ + talloc_zfree(*state); + assert_true(leak_check_teardown()); +} + +void test_be_ptask_create_einval_be(void **state) +{ + struct test_ctx *test_ctx = (struct test_ctx *)(*state); + struct be_ptask *ptask = NULL; + errno_t ret; + + ret = be_ptask_create(test_ctx, NULL, PERIOD, 0, 0, 0, 0, + BE_PTASK_OFFLINE_SKIP, 0, test_be_ptask_send, + test_be_ptask_recv, NULL, "Test ptask", &ptask); + assert_int_equal(ret, EINVAL); + assert_null(ptask); +} + +void test_be_ptask_create_einval_period(void **state) +{ + struct test_ctx *test_ctx = (struct test_ctx *)(*state); + struct be_ptask *ptask = NULL; + errno_t ret; + + ret = be_ptask_create(test_ctx, test_ctx->be_ctx, 0, 0, 0, 0, 0, + BE_PTASK_OFFLINE_SKIP, 0, test_be_ptask_send, + test_be_ptask_recv, NULL, "Test ptask", &ptask); + assert_int_equal(ret, EINVAL); + assert_null(ptask); +} + +void test_be_ptask_create_einval_send(void **state) +{ + struct test_ctx *test_ctx = (struct test_ctx *)(*state); + struct be_ptask *ptask = NULL; + errno_t ret; + + ret = be_ptask_create(test_ctx, test_ctx->be_ctx, PERIOD, 0, 0, 0, 0, + BE_PTASK_OFFLINE_SKIP, 0, NULL, + test_be_ptask_recv, NULL, "Test ptask", &ptask); + assert_int_equal(ret, EINVAL); + assert_null(ptask); +} + +void test_be_ptask_create_einval_recv(void **state) +{ + struct test_ctx *test_ctx = (struct test_ctx *)(*state); + struct be_ptask *ptask = NULL; + errno_t ret; + + ret = be_ptask_create(test_ctx, test_ctx->be_ctx, PERIOD, 0, 0, 0, 0, + BE_PTASK_OFFLINE_SKIP, 0, test_be_ptask_send, + NULL, NULL, "Test ptask", &ptask); + assert_int_equal(ret, EINVAL); + assert_null(ptask); +} + +void test_be_ptask_create_einval_name(void **state) +{ + struct test_ctx *test_ctx = (struct test_ctx *)(*state); + struct be_ptask *ptask = NULL; + errno_t ret; + + ret = be_ptask_create(test_ctx, test_ctx->be_ctx, PERIOD, 0, 0, 0, 0, + BE_PTASK_OFFLINE_SKIP, 0, test_be_ptask_send, + test_be_ptask_recv, NULL, NULL, &ptask); + assert_int_equal(ret, EINVAL); + assert_null(ptask); +} + +void test_be_ptask_create_no_delay(void **state) +{ + struct test_ctx *test_ctx = (struct test_ctx *)(*state); + struct be_ptask *ptask = NULL; + time_t now; + errno_t ret; + + now = time(NULL); + ret = be_ptask_create(test_ctx, test_ctx->be_ctx, PERIOD, 0, 0, 0, 0, + BE_PTASK_OFFLINE_SKIP, 0, test_be_ptask_send, + test_be_ptask_recv, test_ctx, "Test ptask", &ptask); + assert_int_equal(ret, ERR_OK); + assert_non_null(ptask); + assert_non_null(ptask->timer); + + while (!test_ctx->done) { + tevent_loop_once(test_ctx->be_ctx->ev); + } + + assert_true(now <= ptask->last_execution); + assert_true(now <= test_ctx->when); + assert_true(ptask->last_execution <= test_ctx->when); + + be_ptask_destroy(&ptask); + assert_null(ptask); +} + +void test_be_ptask_create_first_delay(void **state) +{ + struct test_ctx *test_ctx = (struct test_ctx *)(*state); + struct be_ptask *ptask = NULL; + time_t now; + errno_t ret; + + now = time(NULL); + ret = be_ptask_create(test_ctx, test_ctx->be_ctx, PERIOD, DELAY, 0, 0, 0, + BE_PTASK_OFFLINE_SKIP, 0, test_be_ptask_send, + test_be_ptask_recv, test_ctx, "Test ptask", &ptask); + assert_int_equal(ret, ERR_OK); + assert_non_null(ptask); + assert_non_null(ptask->timer); + + while (!test_ctx->done) { + tevent_loop_once(test_ctx->be_ctx->ev); + } + + assert_true(now + DELAY <= ptask->last_execution); + assert_true(now + DELAY <= test_ctx->when); + assert_true(ptask->last_execution <= test_ctx->when); + + be_ptask_destroy(&ptask); + assert_null(ptask); +} + +void test_be_ptask_disable(void **state) +{ + struct test_ctx *test_ctx = (struct test_ctx *)(*state); + struct be_ptask *ptask = NULL; + errno_t ret; + + ret = be_ptask_create(test_ctx, test_ctx->be_ctx, PERIOD, 0, 0, 0, 0, + BE_PTASK_OFFLINE_SKIP, 0, test_be_ptask_send, + test_be_ptask_recv, test_ctx, "Test ptask", &ptask); + assert_int_equal(ret, ERR_OK); + assert_non_null(ptask); + assert_non_null(ptask->timer); + + be_ptask_disable(ptask); + + assert_null(ptask->timer); + assert_false(ptask->enabled); + + be_ptask_destroy(&ptask); + assert_null(ptask); +} + +void test_be_ptask_enable(void **state) +{ + struct test_ctx *test_ctx = (struct test_ctx *)(*state); + struct be_ptask *ptask = NULL; + time_t now; + errno_t ret; + + now = time(NULL); + ret = be_ptask_create(test_ctx, test_ctx->be_ctx, PERIOD, 0, 0, 0, 0, + BE_PTASK_OFFLINE_SKIP, 0, test_be_ptask_send, + test_be_ptask_recv, test_ctx, "Test ptask", &ptask); + assert_int_equal(ret, ERR_OK); + assert_non_null(ptask); + assert_non_null(ptask->timer); + + be_ptask_disable(ptask); + + now = time(NULL); + be_ptask_enable(ptask); + assert_non_null(ptask->timer); + + while (!test_ctx->done) { + tevent_loop_once(test_ctx->be_ctx->ev); + } + + assert_true(now <= ptask->last_execution); + assert_true(now <= test_ctx->when); + assert_true(ptask->last_execution <= test_ctx->when); + + be_ptask_destroy(&ptask); + assert_null(ptask); +} + +void test_be_ptask_enable_delay(void **state) +{ + struct test_ctx *test_ctx = (struct test_ctx *)(*state); + struct be_ptask *ptask = NULL; + time_t now; + errno_t ret; + + ret = be_ptask_create(test_ctx, test_ctx->be_ctx, PERIOD, 0, DELAY, 0, 0, + BE_PTASK_OFFLINE_SKIP, 0, test_be_ptask_send, + test_be_ptask_recv, test_ctx, "Test ptask", &ptask); + assert_int_equal(ret, ERR_OK); + assert_non_null(ptask); + assert_non_null(ptask->timer); + + while (!test_ctx->done) { + tevent_loop_once(test_ctx->be_ctx->ev); + } + + be_ptask_disable(ptask); + test_ctx->done = false; + now = time(NULL); + be_ptask_enable(ptask); + + while (!test_ctx->done) { + tevent_loop_once(test_ctx->be_ctx->ev); + } + + assert_true(now + DELAY <= ptask->last_execution); + assert_true(now + DELAY <= test_ctx->when); + assert_true(ptask->last_execution <= test_ctx->when); + + be_ptask_destroy(&ptask); + assert_null(ptask); +} + +void test_be_ptask_offline_skip(void **state) +{ + struct test_ctx *test_ctx = (struct test_ctx *)(*state); + struct be_ptask *ptask = NULL; + time_t next_execution; + time_t now; + errno_t ret; + + mark_offline(test_ctx); + + now = time(NULL); + ret = be_ptask_create(test_ctx, test_ctx->be_ctx, PERIOD, 0, 0, 0, 0, + BE_PTASK_OFFLINE_SKIP, 0, test_be_ptask_send, + test_be_ptask_recv, test_ctx, "Test ptask", &ptask); + assert_int_equal(ret, ERR_OK); + assert_non_null(ptask); + assert_non_null(ptask->timer); + + next_execution = ptask->next_execution; + assert_true(now <= next_execution); + + while (ptask->next_execution == next_execution && !test_ctx->done) { + tevent_loop_once(test_ctx->be_ctx->ev); + } + + assert_true(next_execution + PERIOD <= ptask->next_execution); + assert_true(ptask->enabled); + assert_non_null(ptask->timer); + + be_ptask_destroy(&ptask); + assert_null(ptask); +} + +void test_be_ptask_offline_disable(void **state) +{ + struct test_ctx *test_ctx = (struct test_ctx *)(*state); + struct be_ptask *ptask = NULL; + errno_t ret; + + mark_offline(test_ctx); + + will_return(be_add_online_cb, test_ctx); + will_return(be_add_offline_cb, test_ctx); + + ret = be_ptask_create(test_ctx, test_ctx->be_ctx, PERIOD, 0, 0, 0, 0, + BE_PTASK_OFFLINE_DISABLE, 0, test_be_ptask_send, + test_be_ptask_recv, test_ctx, "Test ptask", &ptask); + assert_int_equal(ret, ERR_OK); + assert_non_null(ptask); + assert_non_null(ptask->timer); + + assert_true(test_ctx->add_online_cb_called); + assert_true(test_ctx->add_offline_cb_called); + + while (ptask->enabled && !test_ctx->done) { + tevent_loop_once(test_ctx->be_ctx->ev); + } + + assert_false(ptask->enabled); + assert_false(test_ctx->done); + assert_null(ptask->timer); + + be_ptask_destroy(&ptask); + assert_null(ptask); +} + +void test_be_ptask_offline_execute(void **state) +{ + struct test_ctx *test_ctx = (struct test_ctx *)(*state); + struct be_ptask *ptask = NULL; + errno_t ret; + + mark_offline(test_ctx); + + ret = be_ptask_create(test_ctx, test_ctx->be_ctx, PERIOD, 0, 0, 0, 0, + BE_PTASK_OFFLINE_EXECUTE, 0, test_be_ptask_send, + test_be_ptask_recv, test_ctx, "Test ptask", &ptask); + assert_int_equal(ret, ERR_OK); + assert_non_null(ptask); + assert_non_null(ptask->timer); + + while (!test_ctx->done) { + tevent_loop_once(test_ctx->be_ctx->ev); + } + + assert_true(ptask->enabled); + assert_non_null(ptask->timer); + + be_ptask_destroy(&ptask); + assert_null(ptask); +} + +void test_be_ptask_reschedule_ok(void **state) +{ + struct test_ctx *test_ctx = (struct test_ctx *)(*state); + struct be_ptask *ptask = NULL; + time_t next_execution; + time_t now; + errno_t ret; + + now = time(NULL); + ret = be_ptask_create(test_ctx, test_ctx->be_ctx, PERIOD, 0, 0, 0, 0, + BE_PTASK_OFFLINE_SKIP, 0, test_be_ptask_send, + test_be_ptask_recv, test_ctx, "Test ptask", &ptask); + assert_int_equal(ret, ERR_OK); + assert_non_null(ptask); + assert_non_null(ptask->timer); + + next_execution = ptask->next_execution; + + while (!test_ctx->done) { + tevent_loop_once(test_ctx->be_ctx->ev); + } + + assert_true(now <= ptask->last_execution); + assert_true(now <= test_ctx->when); + assert_true(ptask->last_execution <= test_ctx->when); + + assert_true(next_execution + PERIOD <= ptask->next_execution); + assert_non_null(ptask->timer); + + be_ptask_destroy(&ptask); + assert_null(ptask); +} + +void test_be_ptask_reschedule_null(void **state) +{ + struct test_ctx *test_ctx = (struct test_ctx *)(*state); + struct be_ptask *ptask = NULL; + time_t now = 0; + errno_t ret; + + ret = be_ptask_create(test_ctx, test_ctx->be_ctx, PERIOD, 0, 0, 0, 0, + BE_PTASK_OFFLINE_SKIP, 0, test_be_ptask_null_send, + test_be_ptask_recv, test_ctx, "Test ptask", + &ptask); + assert_int_equal(ret, ERR_OK); + assert_non_null(ptask); + assert_non_null(ptask->timer); + + while (!test_ctx->done) { + now = time(NULL); + tevent_loop_once(test_ctx->be_ctx->ev); + } + + assert_true(now + PERIOD <= ptask->next_execution); + assert_non_null(ptask->timer); + + be_ptask_destroy(&ptask); + assert_null(ptask); +} + +void test_be_ptask_reschedule_error(void **state) +{ + struct test_ctx *test_ctx = (struct test_ctx *)(*state); + struct be_ptask *ptask = NULL; + time_t now = 0; + errno_t ret; + + ret = be_ptask_create(test_ctx, test_ctx->be_ctx, PERIOD, 0, 0, 0, 0, + BE_PTASK_OFFLINE_SKIP, 0, test_be_ptask_send, + test_be_ptask_error_recv, test_ctx, "Test ptask", + &ptask); + assert_int_equal(ret, ERR_OK); + assert_non_null(ptask); + assert_non_null(ptask->timer); + + while (!test_ctx->done) { + now = time(NULL); + tevent_loop_once(test_ctx->be_ctx->ev); + } + + assert_true(now + PERIOD <= ptask->next_execution); + assert_non_null(ptask->timer); + + be_ptask_destroy(&ptask); + assert_null(ptask); +} + +void test_be_ptask_reschedule_timeout(void **state) +{ + struct test_ctx *test_ctx = (struct test_ctx *)(*state); + struct be_ptask *ptask = NULL; + time_t now = 0; + errno_t ret; + + ret = be_ptask_create(test_ctx, test_ctx->be_ctx, PERIOD, 0, 0, 0, 1, + BE_PTASK_OFFLINE_SKIP, 0, test_be_ptask_timeout_send, + test_be_ptask_error_recv, test_ctx, "Test ptask", + &ptask); + assert_int_equal(ret, ERR_OK); + assert_non_null(ptask); + assert_non_null(ptask->timer); + + /* first iterate until the task is executed */ + while (!test_ctx->done && ptask->req == NULL) { + tevent_loop_once(test_ctx->be_ctx->ev); + } + + /* then iterate until the request is destroyed */ + while (!test_ctx->done && ptask->req != NULL) { + now = time(NULL); + tevent_loop_once(test_ctx->be_ctx->ev); + } + + assert_false(test_ctx->done); + assert_true(now + PERIOD <= ptask->next_execution); + assert_non_null(ptask->timer); + + be_ptask_destroy(&ptask); + assert_null(ptask); +} + +void test_be_ptask_get_period(void **state) +{ + struct test_ctx *test_ctx = (struct test_ctx *)(*state); + struct be_ptask *ptask = NULL; + time_t out_period; + errno_t ret; + + ret = be_ptask_create(test_ctx, test_ctx->be_ctx, PERIOD, 0, 0, 0, 0, + BE_PTASK_OFFLINE_SKIP, 0, test_be_ptask_send, + test_be_ptask_recv, test_ctx, "Test ptask", &ptask); + assert_int_equal(ret, ERR_OK); + assert_non_null(ptask); + + out_period = be_ptask_get_period(ptask); + assert_true(PERIOD == out_period); + + be_ptask_destroy(&ptask); + assert_null(ptask); +} + +void test_be_ptask_create_sync(void **state) +{ + struct test_ctx *test_ctx = (struct test_ctx *)(*state); + struct be_ptask *ptask = NULL; + time_t now; + errno_t ret; + + now = time(NULL); + ret = be_ptask_create_sync(test_ctx, test_ctx->be_ctx, PERIOD, 0, 0, 0, 0, + BE_PTASK_OFFLINE_SKIP, 0, test_be_ptask_sync, + test_ctx, "Test ptask", &ptask); + assert_int_equal(ret, ERR_OK); + assert_non_null(ptask); + assert_non_null(ptask->timer); + + while (!test_ctx->done) { + tevent_loop_once(test_ctx->be_ctx->ev); + } + + assert_true(now <= ptask->last_execution); + assert_true(now <= test_ctx->when); + assert_true(ptask->last_execution <= test_ctx->when); + + be_ptask_destroy(&ptask); + assert_null(ptask); +} + +void test_be_ptask_sync_reschedule_ok(void **state) +{ + struct test_ctx *test_ctx = (struct test_ctx *)(*state); + struct be_ptask *ptask = NULL; + time_t next_execution; + time_t now; + errno_t ret; + + now = time(NULL); + ret = be_ptask_create_sync(test_ctx, test_ctx->be_ctx, PERIOD, 0, 0, 0, 0, + BE_PTASK_OFFLINE_SKIP, 0, test_be_ptask_sync, + test_ctx, "Test ptask", &ptask); + assert_int_equal(ret, ERR_OK); + assert_non_null(ptask); + assert_non_null(ptask->timer); + + next_execution = ptask->next_execution; + + while (!is_sync_ptask_finished(test_ctx, ptask)) { + tevent_loop_once(test_ctx->be_ctx->ev); + } + + assert_true(now <= ptask->last_execution); + assert_true(now <= test_ctx->when); + assert_true(ptask->last_execution <= test_ctx->when); + + assert_true(next_execution + PERIOD <= ptask->next_execution); + assert_non_null(ptask->timer); + + be_ptask_destroy(&ptask); + assert_null(ptask); +} + +void test_be_ptask_sync_reschedule_error(void **state) +{ + struct test_ctx *test_ctx = (struct test_ctx *)(*state); + struct be_ptask *ptask = NULL; + time_t now = 0; + errno_t ret; + + ret = be_ptask_create_sync(test_ctx, test_ctx->be_ctx, PERIOD, 0, 0, 0, 0, + BE_PTASK_OFFLINE_SKIP, 0, + test_be_ptask_sync_error, + test_ctx, "Test ptask", &ptask); + assert_int_equal(ret, ERR_OK); + assert_non_null(ptask); + assert_non_null(ptask->timer); + + while (!is_sync_ptask_finished(test_ctx, ptask)) { + now = time(NULL); + tevent_loop_once(test_ctx->be_ctx->ev); + } + + assert_true(now + PERIOD <= ptask->next_execution); + assert_non_null(ptask->timer); + + be_ptask_destroy(&ptask); + assert_null(ptask); +} + +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[] = { + new_test(be_ptask_create_einval_be), + new_test(be_ptask_create_einval_period), + new_test(be_ptask_create_einval_send), + new_test(be_ptask_create_einval_recv), + new_test(be_ptask_create_einval_name), + new_test(be_ptask_create_no_delay), + new_test(be_ptask_create_first_delay), + new_test(be_ptask_disable), + new_test(be_ptask_enable), + new_test(be_ptask_enable_delay), + new_test(be_ptask_offline_skip), + new_test(be_ptask_offline_disable), + new_test(be_ptask_offline_execute), + new_test(be_ptask_reschedule_ok), + new_test(be_ptask_reschedule_null), + new_test(be_ptask_reschedule_error), + new_test(be_ptask_reschedule_timeout), + new_test(be_ptask_get_period), + new_test(be_ptask_create_sync), + new_test(be_ptask_sync_reschedule_ok), + new_test(be_ptask_sync_reschedule_error) + }; + + /* 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_CLI_INIT(debug_level); + + return run_tests(tests); +} -- cgit