test_dyndns.c revision a7401bf72db3a6eb62b1628f9dd141f7118e3510
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes/*
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes Authors:
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes Jakub Hrozek <jhrozek@redhat.com>
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes Copyright (C) 2013 Red Hat
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes SSSD tests: Dynamic DNS tests
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes This program is free software; you can redistribute it and/or modify
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes it under the terms of the GNU General Public License as published by
0662ed52e814f8f08ef0e09956413a792584eddffuankg the Free Software Foundation; either version 3 of the License, or
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes (at your option) any later version.
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes This program is distributed in the hope that it will be useful,
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes but WITHOUT ANY WARRANTY; without even the implied warranty of
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes GNU General Public License for more details.
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
70953fb44a7140fe206c3a5f011e24209c8c5c6abnicholes You should have received a copy of the GNU General Public License
70953fb44a7140fe206c3a5f011e24209c8c5c6abnicholes along with this program. If not, see <http://www.gnu.org/licenses/>.
16b55a35cff91315d261d1baa776138af465c4e4fuankg*/
16b55a35cff91315d261d1baa776138af465c4e4fuankg
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes#include <talloc.h>
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes#include <tevent.h>
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes#include <errno.h>
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes#include <popt.h>
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes#include <unistd.h>
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes#include <sys/types.h>
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes#include <ifaddrs.h>
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes#include <arpa/inet.h>
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes/* In order to access opaque types */
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes#include "providers/dp_dyndns.c"
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes#include "tests/cmocka/common_mock.h"
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes#include "src/providers/dp_dyndns.h"
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes#define TESTS_PATH "tests_dyndns"
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes#define TEST_CONF_DB "test_dyndns_conf.ldb"
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes#define TEST_SYSDB_FILE "cache_dyndns_test.ldb"
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes#define TEST_DOM_NAME "dyndns_test"
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes#define TEST_ID_PROVIDER "ldap"
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholesenum mock_nsupdate_states {
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes MOCK_NSUPDATE_OK,
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes MOCK_NSUPDATE_ERR,
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes MOCK_NSUPDATE_TIMEOUT,
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes};
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholesstruct dyndns_test_ctx {
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes struct sss_test_ctx *tctx;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes struct be_ctx *be_ctx;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes struct be_nsupdate_ctx *update_ctx;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes enum mock_nsupdate_states state;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes int child_status;
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg int child_retval;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes};
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholesstatic struct dyndns_test_ctx *dyndns_test_ctx;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholesvoid __wrap_execv(const char *path, char *const argv[])
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes{
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes int err;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes switch (dyndns_test_ctx->state) {
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes case MOCK_NSUPDATE_OK:
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes DEBUG(SSSDBG_FUNC_DATA, ("nsupdate success test case\n"));
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes err = 0;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes break;
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg case MOCK_NSUPDATE_ERR:
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes DEBUG(SSSDBG_FUNC_DATA, ("nsupdate error test case\n"));
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes err = 1;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes break;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes case MOCK_NSUPDATE_TIMEOUT:
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes DEBUG(SSSDBG_FUNC_DATA, ("nsupdate timeout test case\n"));
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes err = 2;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes sleep(3);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes break;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes default:
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes DEBUG(SSSDBG_CRIT_FAILURE, ("unknown test case\n"));
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes err = 255;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes break;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes }
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes DEBUG(SSSDBG_TRACE_LIBS, ("Child exiting with status %d\n", err));
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes _exit(err);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes}
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholesint __wrap_getifaddrs(struct ifaddrs **_ifap)
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes{
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes struct ifaddrs *ifap = NULL;
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg struct ifaddrs *ifap_prev = NULL;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes struct ifaddrs *ifap_head = NULL;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes char *name;
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg char *straddr;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes while ((name = sss_mock_ptr_type(char *)) != NULL) {
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes straddr = sss_mock_ptr_type(char *);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes if (straddr == NULL) {
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes errno = EINVAL;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes goto fail;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes }
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes ifap = talloc_zero(global_talloc_context, struct ifaddrs);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes if (ifap == NULL) {
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes errno = ENOMEM; /* getifaddrs sets errno, too */
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg goto fail;
0662ed52e814f8f08ef0e09956413a792584eddffuankg }
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg if (ifap_prev) {
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes ifap_prev->ifa_next = ifap;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes } else {
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes ifap_head = ifap;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes }
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes ifap_prev = ifap;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes ifap->ifa_name = talloc_strdup(ifap, name);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes if (ifap == NULL) {
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes errno = ENOMEM;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes goto fail;
0662ed52e814f8f08ef0e09956413a792584eddffuankg }
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes ifap->ifa_addr = (struct sockaddr *) talloc(ifap, struct sockaddr_in);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes if (ifap->ifa_addr == NULL) {
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes errno = ENOMEM;
0662ed52e814f8f08ef0e09956413a792584eddffuankg goto fail;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes }
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes ((struct sockaddr_in *) ifap->ifa_addr)->sin_family = AF_INET;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes /* convert straddr into ifa_addr */
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes if (inet_pton(AF_INET, straddr,
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes &(((struct sockaddr_in *) ifap->ifa_addr)->sin_addr)) != 1) {
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes goto fail;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes }
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes }
0662ed52e814f8f08ef0e09956413a792584eddffuankg
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes *_ifap = ifap_head;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes return 0;
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg
405f61494d3ed3ca9c054dacc05a53513e172145bnicholesfail:
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes talloc_free(ifap);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes return -1;
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg}
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholesvoid __wrap_freeifaddrs(struct ifaddrs *ifap)
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes{
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes talloc_free(ifap);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes}
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholesstatic void dyndns_test_done(struct tevent_req *req)
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes{
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes struct dyndns_test_ctx *ctx =
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes tevent_req_callback_data(req, struct dyndns_test_ctx);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes ctx->child_retval = -1;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes ctx->tctx->error = be_nsupdate_recv(req, &ctx->child_status);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes talloc_zfree(req);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes ctx->tctx->done = true;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes}
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholesvoid will_return_getifaddrs(const char *ifname, const char *straddr)
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes{
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes will_return(__wrap_getifaddrs, ifname);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes if (ifname) {
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes will_return(__wrap_getifaddrs, straddr);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes }
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes}
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholesvoid dyndns_test_get_ifaddr(void **state)
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg{
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes errno_t ret;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes struct sss_iface_addr *addrlist;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes char straddr[128];
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes check_leaks_push(dyndns_test_ctx);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes will_return_getifaddrs("eth0", "192.168.0.1");
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes will_return_getifaddrs("eth1", "192.168.0.2");
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes will_return_getifaddrs(NULL, NULL); /* sentinel */
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes ret = sss_iface_addr_list_get(dyndns_test_ctx, "eth0", &addrlist);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes assert_int_equal(ret, EOK);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes /* There must be only one address with the correct value */
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes assert_non_null(addrlist);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes assert_non_null(addrlist->addr);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes assert_null(addrlist->next);
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg assert_null(addrlist->prev);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes assert_non_null(inet_ntop(AF_INET,
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes &((struct sockaddr_in *) addrlist->addr)->sin_addr,
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes straddr, INET6_ADDRSTRLEN));
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes assert_string_equal(straddr, "192.168.0.1");
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes talloc_free(addrlist);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes assert_true(check_leaks_pop(dyndns_test_ctx) == true);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes}
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholesvoid dyndns_test_ok(void **state)
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes{
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes struct tevent_req *req;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes errno_t ret;
8ffac2c334103c0336602aaede650cb578611151fuankg TALLOC_CTX *tmp_ctx;
8ffac2c334103c0336602aaede650cb578611151fuankg
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes tmp_ctx = talloc_new(global_talloc_context);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes assert_non_null(tmp_ctx);
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg check_leaks_push(tmp_ctx);
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes dyndns_test_ctx->state = MOCK_NSUPDATE_OK;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes req = be_nsupdate_send(tmp_ctx, dyndns_test_ctx->tctx->ev,
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes BE_NSUPDATE_AUTH_GSS_TSIG,
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes discard_const("test message"), false);
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg assert_non_null(req);
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg tevent_req_set_callback(req, dyndns_test_done, dyndns_test_ctx);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes /* Wait until the test finishes with EOK */
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes ret = test_ev_loop(dyndns_test_ctx->tctx);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes DEBUG(SSSDBG_TRACE_LIBS,
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg ("Child request returned [%d]: %s\n", ret, strerror(ret)));
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes assert_int_equal(ret, EOK);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes assert_true(WIFEXITED(dyndns_test_ctx->child_status));
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes assert_int_equal(WEXITSTATUS(dyndns_test_ctx->child_status), 0);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes assert_true(check_leaks_pop(tmp_ctx) == true);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes talloc_free(tmp_ctx);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes}
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholesvoid dyndns_test_error(void **state)
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg{
0662ed52e814f8f08ef0e09956413a792584eddffuankg struct tevent_req *req;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes errno_t ret;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes TALLOC_CTX *tmp_ctx;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes tmp_ctx = talloc_new(global_talloc_context);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes assert_non_null(tmp_ctx);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes check_leaks_push(tmp_ctx);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes dyndns_test_ctx->state = MOCK_NSUPDATE_ERR;
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes req = be_nsupdate_send(tmp_ctx, dyndns_test_ctx->tctx->ev,
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes BE_NSUPDATE_AUTH_GSS_TSIG,
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes discard_const("test message"), false);
0662ed52e814f8f08ef0e09956413a792584eddffuankg assert_non_null(req);
405f61494d3ed3ca9c054dacc05a53513e172145bnicholes tevent_req_set_callback(req, dyndns_test_done, dyndns_test_ctx);
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg
/* Wait until the test finishes with EIO (child error) */
ret = test_ev_loop(dyndns_test_ctx->tctx);
DEBUG(SSSDBG_TRACE_LIBS,
("Child request returned [%d]: %s\n", ret, strerror(ret)));
assert_int_equal(ret, ERR_DYNDNS_FAILED);
assert_true(WIFEXITED(dyndns_test_ctx->child_status));
assert_int_equal(WEXITSTATUS(dyndns_test_ctx->child_status), 1);
assert_true(check_leaks_pop(tmp_ctx) == true);
talloc_free(tmp_ctx);
}
void dyndns_test_timeout(void **state)
{
struct tevent_req *req;
errno_t ret;
TALLOC_CTX *tmp_ctx;
tmp_ctx = talloc_new(global_talloc_context);
assert_non_null(tmp_ctx);
check_leaks_push(tmp_ctx);
dyndns_test_ctx->state = MOCK_NSUPDATE_TIMEOUT;
req = be_nsupdate_send(tmp_ctx, dyndns_test_ctx->tctx->ev,
BE_NSUPDATE_AUTH_GSS_TSIG,
discard_const("test message"), false);
assert_non_null(req);
tevent_req_set_callback(req, dyndns_test_done, dyndns_test_ctx);
/* Wait until the test finishes with EIO (child error) */
ret = test_ev_loop(dyndns_test_ctx->tctx);
/* The event queue may not be empty. We need to make sure that all events
* are processed. Unfortunately, tevent_loop_wait() contains a bug that
* prevents exiting the loop even if there are no remaining events, thus
* we have to use tevent_loop_once().
*
* FIXME: use tevent_loop_wait() when the bug is fixed
* https://bugzilla.samba.org/show_bug.cgi?id=10012
*/
tevent_loop_once(dyndns_test_ctx->tctx->ev); /* SIGCHLD handler */
tevent_loop_once(dyndns_test_ctx->tctx->ev); /* nsupdate_child_handler */
DEBUG(SSSDBG_TRACE_LIBS,
("Child request returned [%d]: %s\n", ret, strerror(ret)));
assert_int_equal(ret, ERR_DYNDNS_TIMEOUT);
assert_true(check_leaks_pop(tmp_ctx) == true);
talloc_free(tmp_ctx);
}
void dyndns_test_timer(void *pvt)
{
struct dyndns_test_ctx *ctx = talloc_get_type(pvt, struct dyndns_test_ctx);
static int ncalls = 0;
ncalls++;
if (ncalls == 1) {
be_nsupdate_timer_schedule(ctx->tctx->ev, ctx->update_ctx);
} else if (ncalls == 2) {
ctx->tctx->done = true;
}
ctx->tctx->error = ERR_OK;
}
void dyndns_test_interval(void **state)
{
errno_t ret;
TALLOC_CTX *tmp_ctx;
tmp_ctx = talloc_new(global_talloc_context);
assert_non_null(tmp_ctx);
check_leaks_push(tmp_ctx);
ret = be_nsupdate_init(tmp_ctx, dyndns_test_ctx->be_ctx, NULL,
&dyndns_test_ctx->update_ctx);
assert_int_equal(ret, EOK);
ret = be_nsupdate_init_timer(dyndns_test_ctx->update_ctx,
dyndns_test_ctx->be_ctx->ev,
dyndns_test_timer, dyndns_test_ctx);
assert_int_equal(ret, EOK);
/* Wait until the timer hits */
ret = test_ev_loop(dyndns_test_ctx->tctx);
DEBUG(SSSDBG_TRACE_LIBS,
("Child request returned [%d]: %s\n", ret, strerror(ret)));
assert_int_equal(ret, ERR_OK);
talloc_free(dyndns_test_ctx->update_ctx);
assert_true(check_leaks_pop(tmp_ctx) == true);
talloc_free(tmp_ctx);
}
/* Testsuite setup and teardown */
void dyndns_test_setup(void **state)
{
struct sss_test_conf_param params[] = {
{ "dyndns_update", "true" },
{ "dyndns_refresh_interval", "2" },
{ NULL, NULL }, /* Sentinel */
};
assert_true(leak_check_setup());
dyndns_test_ctx = talloc_zero(global_talloc_context, struct dyndns_test_ctx);
assert_non_null(dyndns_test_ctx);
dyndns_test_ctx->tctx = create_dom_test_ctx(dyndns_test_ctx, TESTS_PATH,
TEST_CONF_DB, TEST_SYSDB_FILE,
TEST_DOM_NAME, TEST_ID_PROVIDER,
params);
assert_non_null(dyndns_test_ctx->tctx);
dyndns_test_ctx->be_ctx = talloc_zero(dyndns_test_ctx, struct be_ctx);
assert_non_null(dyndns_test_ctx->be_ctx);
dyndns_test_ctx->be_ctx->cdb = dyndns_test_ctx->tctx->confdb;
dyndns_test_ctx->be_ctx->ev = dyndns_test_ctx->tctx->ev;
dyndns_test_ctx->be_ctx->conf_path = talloc_asprintf(dyndns_test_ctx,
CONFDB_DOMAIN_PATH_TMPL,
TEST_DOM_NAME);
assert_non_null(dyndns_test_ctx->be_ctx->conf_path);
}
void dyndns_test_teardown(void **state)
{
talloc_free(dyndns_test_ctx);
assert_true(leak_check_teardown());
}
int main(int argc, const char *argv[])
{
int rv;
int no_cleanup = 0;
poptContext pc;
int opt;
struct poptOption long_options[] = {
POPT_AUTOHELP
SSSD_DEBUG_OPTS
{"no-cleanup", 'n', POPT_ARG_NONE, &no_cleanup, 0,
_("Do not delete the test database after a test run"), NULL },
POPT_TABLEEND
};
const UnitTest tests[] = {
/* Utility functions unit test */
unit_test(dyndns_test_get_ifaddr),
/* Dynamic DNS update unit tests*/
unit_test_setup_teardown(dyndns_test_ok,
dyndns_test_setup, dyndns_test_teardown),
unit_test_setup_teardown(dyndns_test_error,
dyndns_test_setup, dyndns_test_teardown),
unit_test_setup_teardown(dyndns_test_timeout,
dyndns_test_setup, dyndns_test_teardown),
unit_test_setup_teardown(dyndns_test_interval,
dyndns_test_setup, dyndns_test_teardown),
};
/* 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_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();
test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_SYSDB_FILE);
test_dom_suite_setup(TESTS_PATH);
rv = run_tests(tests);
if (rv == 0 && !no_cleanup) {
test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_SYSDB_FILE);
}
return rv;
}