da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek/*
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek Authors:
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek Jakub Hrozek <jhrozek@redhat.com>
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek Copyright (C) 2013 Red Hat
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek SSSD tests: Common utilities for tests that exercise domains
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek This program is free software; you can redistribute it and/or modify
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek it under the terms of the GNU General Public License as published by
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek the Free Software Foundation; either version 3 of the License, or
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek (at your option) any later version.
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek This program is distributed in the hope that it will be useful,
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek but WITHOUT ANY WARRANTY; without even the implied warranty of
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek GNU General Public License for more details.
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek You should have received a copy of the GNU General Public License
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek along with this program. If not, see <http://www.gnu.org/licenses/>.
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek*/
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek#include <tevent.h>
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek#include <talloc.h>
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek#include <errno.h>
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek#include "tests/common.h"
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozekstruct sss_test_ctx *
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozekcreate_ev_test_ctx(TALLOC_CTX *mem_ctx)
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek{
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek struct sss_test_ctx *test_ctx;
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek test_ctx = talloc_zero(mem_ctx, struct sss_test_ctx);
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek if (test_ctx == NULL) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "talloc_zero failed\n");
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek goto fail;
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek }
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek /* Create an event context */
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek test_ctx->ev = tevent_context_init(test_ctx);
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek if (test_ctx->ev == NULL) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "tevent_context_init failed\n");
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek goto fail;
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek }
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek return test_ctx;
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozekfail:
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek talloc_free(test_ctx);
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek return NULL;
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek}
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozekstruct tevent_req *
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozektest_request_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev, errno_t err)
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek{
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek struct tevent_req *req;
f51639d8e48e353402e5f649f2fac7ff3143dfbdPavel Březina int *state;
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek req = tevent_req_create(mem_ctx, &state, int);
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek if (!req) return NULL;
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek if (err == EOK) {
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek tevent_req_done(req);
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek } else {
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek tevent_req_error(req, err);
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek }
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek tevent_req_post(req, ev);
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek return req;
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek}
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozekerrno_t test_request_recv(struct tevent_req *req)
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek{
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek TEVENT_REQ_RETURN_ON_ERROR(req);
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek return EOK;
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek}
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozekint test_ev_loop(struct sss_test_ctx *tctx)
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek{
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek while (!tctx->done)
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek tevent_loop_once(tctx->ev);
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek return tctx->error;
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek}
eca74a9559ce1b0f123c14906ad8394fc303f468Jakub Hrozek
eca74a9559ce1b0f123c14906ad8394fc303f468Jakub Hrozekvoid test_ev_done(struct sss_test_ctx *tctx, errno_t ret)
eca74a9559ce1b0f123c14906ad8394fc303f468Jakub Hrozek{
eca74a9559ce1b0f123c14906ad8394fc303f468Jakub Hrozek tctx->error = ret;
eca74a9559ce1b0f123c14906ad8394fc303f468Jakub Hrozek tctx->done = true;
eca74a9559ce1b0f123c14906ad8394fc303f468Jakub Hrozek}