test_be_ptask.c revision babaca78cc196e7e0dcc3e972347951a081159f2
6N/A/*
6N/A Authors:
6N/A Pavel Březina <pbrezina@redhat.com>
6N/A
6N/A Copyright (C) 2014 Red Hat
6N/A
6N/A This program is free software; you can redistribute it and/or modify
6N/A it under the terms of the GNU General Public License as published by
6N/A the Free Software Foundation; either version 3 of the License, or
6N/A (at your option) any later version.
6N/A
6N/A This program is distributed in the hope that it will be useful,
6N/A but WITHOUT ANY WARRANTY; without even the implied warranty of
6N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6N/A GNU General Public License for more details.
6N/A
6N/A You should have received a copy of the GNU General Public License
6N/A along with this program. If not, see <http://www.gnu.org/licenses/>.
6N/A*/
6N/A
6N/A#include <talloc.h>
6N/A#include <tevent.h>
6N/A#include <errno.h>
6N/A#include <popt.h>
6N/A#include <time.h>
6N/A
6N/A#include "providers/dp_backend.h"
6N/A#include "providers/dp_ptask_private.h"
6N/A#include "providers/dp_ptask.h"
6N/A#include "tests/cmocka/common_mock.h"
6N/A#include "tests/common.h"
6N/A
6N/A#define DELAY 2
6N/A#define PERIOD 1
6N/A
6N/A#define new_test(test) \
6N/A unit_test_setup_teardown(test_ ## test, test_setup, test_teardown)
6N/A
6N/Astruct test_ctx {
6N/A struct be_ctx *be_ctx;
6N/A
6N/A time_t when;
6N/A bool done;
6N/A
6N/A bool add_online_cb_called;
6N/A bool add_offline_cb_called;
6N/A};
6N/A
6N/A#define mark_online(test_ctx) do { \
6N/A test_ctx->be_ctx->offstat.went_offline = 0; \
6N/A test_ctx->be_ctx->offstat.offline = false; \
6N/A} while (0)
6N/A
6N/A#define mark_offline(test_ctx) do { \
6N/A test_ctx->be_ctx->offstat.went_offline = time(NULL); \
6N/A test_ctx->be_ctx->offstat.offline = true; \
6N/A} while (0)
6N/A
6N/A/* Since both test_ctx->done and ptask->req is marked as finished already
6N/A * in the sync _send function before a new execution is scheduled we need to
6N/A * rely on the fact that ptask->req is set to zero when a new timer is
6N/A * created. This way we guarantee that the condition is true only when
6N/A * the ptask is executed and a new one is scheduled. */
6N/A#define is_sync_ptask_finished(test_ctx, ptask) \
6N/A (test_ctx->done && ptask->req == NULL)
6N/A
6N/A/* Mock few backend functions so we don't have to bring the whole
6N/A * data provider into this test. */
6N/A
6N/Abool be_is_offline(struct be_ctx *ctx)
6N/A{
6N/A return ctx->offstat.offline;
6N/A}
6N/A
6N/Aint be_add_online_cb(TALLOC_CTX *mem_ctx,
6N/A struct be_ctx *ctx,
6N/A be_callback_t cb,
6N/A void *pvt,
6N/A struct be_cb **online_cb)
6N/A{
6N/A struct test_ctx *test_ctx = NULL;
6N/A
6N/A test_ctx = sss_mock_ptr_type(struct test_ctx *);
6N/A test_ctx->add_online_cb_called = true;
6N/A
6N/A return ERR_OK;
6N/A}
6N/A
6N/Aint be_add_offline_cb(TALLOC_CTX *mem_ctx,
6N/A struct be_ctx *ctx,
6N/A be_callback_t cb,
6N/A void *pvt,
6N/A struct be_cb **offline_cb)
6N/A{
6N/A struct test_ctx *test_ctx = NULL;
6N/A
6N/A test_ctx = sss_mock_ptr_type(struct test_ctx *);
6N/A test_ctx->add_offline_cb_called = true;
6N/A
6N/A return ERR_OK;
6N/A}
6N/A
6N/Astruct test_be_ptask_state {
6N/A struct test_ctx *test_ctx;
6N/A};
6N/A
6N/Astruct tevent_req * test_be_ptask_send(TALLOC_CTX *mem_ctx,
6N/A struct tevent_context *ev,
6N/A struct be_ctx *be_ctx,
6N/A struct be_ptask *be_ptask,
6N/A void *pvt)
6N/A{
6N/A struct test_be_ptask_state *state = NULL;
6N/A struct test_ctx *test_ctx = NULL;
6N/A struct tevent_req *req = NULL;
6N/A
6N/A assert_non_null(ev);
6N/A assert_non_null(be_ctx);
6N/A assert_non_null(be_ptask);
6N/A assert_non_null(pvt);
6N/A
6N/A test_ctx = talloc_get_type(pvt, struct test_ctx);
6N/A assert_non_null(test_ctx);
6N/A
6N/A test_ctx->when = time(NULL);
6N/A
6N/A req = tevent_req_create(mem_ctx, &state, struct test_be_ptask_state);
6N/A assert_non_null(req);
6N/A
6N/A state->test_ctx = test_ctx;
6N/A
6N/A tevent_req_done(req);
6N/A tevent_req_post(req, ev);
6N/A return req;
6N/A}
6N/A
6N/Astruct tevent_req * test_be_ptask_null_send(TALLOC_CTX *mem_ctx,
6N/A struct tevent_context *ev,
6N/A struct be_ctx *be_ctx,
6N/A struct be_ptask *be_ptask,
6N/A void *pvt)
6N/A{
6N/A struct test_ctx *test_ctx = NULL;
6N/A assert_non_null(ev);
6N/A assert_non_null(be_ctx);
6N/A assert_non_null(be_ptask);
6N/A assert_non_null(pvt);
6N/A
6N/A test_ctx = talloc_get_type(pvt, struct test_ctx);
6N/A assert_non_null(test_ctx);
6N/A
6N/A test_ctx->when = time(NULL);
6N/A test_ctx->done = true;
6N/A
6N/A return NULL;
6N/A}
6N/A
6N/Astruct tevent_req * test_be_ptask_timeout_send(TALLOC_CTX *mem_ctx,
6N/A struct tevent_context *ev,
6N/A struct be_ctx *be_ctx,
6N/A struct be_ptask *be_ptask,
6N/A void *pvt)
6N/A{
6N/A struct test_be_ptask_state *state = NULL;
6N/A struct test_ctx *test_ctx = NULL;
6N/A struct tevent_req *req = NULL;
6N/A
6N/A assert_non_null(ev);
6N/A assert_non_null(be_ctx);
6N/A assert_non_null(be_ptask);
6N/A assert_non_null(pvt);
6N/A
6N/A test_ctx = talloc_get_type(pvt, struct test_ctx);
6N/A assert_non_null(test_ctx);
6N/A
6N/A test_ctx->when = time(NULL);
6N/A
6N/A req = tevent_req_create(mem_ctx, &state, struct test_be_ptask_state);
6N/A assert_non_null(req);
6N/A
6N/A state->test_ctx = test_ctx;
6N/A
6N/A /* we won't finish the request */
6N/A
6N/A return req;
6N/A}
6N/A
6N/Aerrno_t test_be_ptask_recv(struct tevent_req *req)
6N/A{
6N/A struct test_be_ptask_state *state = NULL;
6N/A
6N/A state = tevent_req_data(req, struct test_be_ptask_state);
6N/A assert_non_null(state);
6N/A
6N/A state->test_ctx->done = true;
6N/A
6N/A TEVENT_REQ_RETURN_ON_ERROR(req);
6N/A
6N/A return ERR_OK;
6N/A}
6N/A
6N/Aerrno_t test_be_ptask_error_recv(struct tevent_req *req)
6N/A{
6N/A struct test_be_ptask_state *state = NULL;
6N/A
6N/A state = tevent_req_data(req, struct test_be_ptask_state);
6N/A assert_non_null(state);
6N/A
6N/A state->test_ctx->done = true;
6N/A
6N/A return ERR_INTERNAL;
6N/A}
6N/A
6N/Aerrno_t test_be_ptask_sync(TALLOC_CTX *mem_ctx,
6N/A struct tevent_context *ev,
6N/A struct be_ctx *be_ctx,
6N/A struct be_ptask *be_ptask,
6N/A void *pvt)
6N/A{
6N/A struct test_ctx *test_ctx = NULL;
6N/A
6N/A assert_non_null(ev);
6N/A assert_non_null(be_ctx);
6N/A assert_non_null(be_ptask);
6N/A assert_non_null(pvt);
6N/A
6N/A test_ctx = talloc_get_type(pvt, struct test_ctx);
6N/A assert_non_null(test_ctx);
6N/A
6N/A test_ctx->when = time(NULL);
6N/A test_ctx->done = true;
6N/A
6N/A return ERR_OK;
6N/A}
6N/A
6N/Aerrno_t test_be_ptask_sync_error(TALLOC_CTX *mem_ctx,
6N/A struct tevent_context *ev,
6N/A struct be_ctx *be_ctx,
6N/A struct be_ptask *be_ptask,
6N/A void *pvt)
6N/A{
6N/A struct test_ctx *test_ctx = NULL;
6N/A
6N/A assert_non_null(ev);
6N/A assert_non_null(be_ctx);
6N/A assert_non_null(be_ptask);
6N/A assert_non_null(pvt);
6N/A
6N/A test_ctx = talloc_get_type(pvt, struct test_ctx);
6N/A assert_non_null(test_ctx);
6N/A
6N/A test_ctx->when = time(NULL);
6N/A test_ctx->done = true;
6N/A
6N/A return ERR_INTERNAL;
6N/A}
6N/A
6N/Avoid test_setup(void **state)
6N/A{
6N/A struct test_ctx *test_ctx = NULL;
6N/A
6N/A assert_true(leak_check_setup());
6N/A
6N/A test_ctx = talloc_zero(global_talloc_context, struct test_ctx);
6N/A assert_non_null(test_ctx);
6N/A
6N/A /* create be_ctx, only ev and offline field should be used */
6N/A test_ctx->be_ctx = talloc_zero(test_ctx, struct be_ctx);
6N/A assert_non_null(test_ctx->be_ctx);
6N/A
6N/A test_ctx->be_ctx->ev = tevent_context_init(test_ctx->be_ctx);
6N/A assert_non_null(test_ctx->be_ctx->ev);
6N/A
6N/A *state = test_ctx;
6N/A}
6N/A
6N/Avoid test_teardown(void **state)
6N/A{
6N/A talloc_zfree(*state);
6N/A assert_true(leak_check_teardown());
6N/A}
6N/A
6N/Avoid test_be_ptask_create_einval_be(void **state)
6N/A{
6N/A struct test_ctx *test_ctx = (struct test_ctx *)(*state);
6N/A struct be_ptask *ptask = NULL;
6N/A errno_t ret;
6N/A
6N/A ret = be_ptask_create(test_ctx, NULL, PERIOD, 0, 0, 0, 0,
6N/A BE_PTASK_OFFLINE_SKIP, 0, test_be_ptask_send,
6N/A 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_reschedule_backoff(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, PERIOD*2, 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);
/* first run */
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_int_equal(PERIOD*2, ptask->period);
assert_non_null(ptask->timer);
test_ctx->done = false;
/* second run */
now = time(NULL);
next_execution = ptask->next_execution;
while (!test_ctx->done) {
tevent_loop_once(test_ctx->be_ctx->ev);
}
assert_true(now + PERIOD <= ptask->last_execution);
assert_true(now + PERIOD <= test_ctx->when);
assert_true(ptask->last_execution <= test_ctx->when);
assert_true(next_execution + PERIOD*2 <= ptask->next_execution);
assert_int_equal(PERIOD*2, ptask->period);
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);
}
void test_be_ptask_sync_reschedule_backoff(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, PERIOD*2,
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);
/* first run */
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_int_equal(PERIOD*2, ptask->period);
assert_non_null(ptask->timer);
test_ctx->done = false;
/* second run */
now = time(NULL);
next_execution = ptask->next_execution;
while (!is_sync_ptask_finished(test_ctx, ptask)) {
tevent_loop_once(test_ctx->be_ctx->ev);
}
assert_true(now + PERIOD <= ptask->last_execution);
assert_true(now + PERIOD <= test_ctx->when);
assert_true(ptask->last_execution <= test_ctx->when);
assert_true(next_execution + PERIOD*2 <= ptask->next_execution);
assert_int_equal(PERIOD*2, ptask->period);
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_reschedule_backoff),
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),
new_test(be_ptask_sync_reschedule_backoff)
};
/* 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);
}