test_fo_srv.c revision a299f900981343904d7c9c5d148e30b8e0b2c460
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek/*
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek Authors:
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek Jakub Hrozek <jhrozek@redhat.com>
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek Copyright (C) 2014 Red Hat
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek SSSD tests: Resolver tests using a fake resolver library
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek This program is free software; you can redistribute it and/or modify
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek it under the terms of the GNU General Public License as published by
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek the Free Software Foundation; either version 3 of the License, or
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek (at your option) any later version.
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek This program is distributed in the hope that it will be useful,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek but WITHOUT ANY WARRANTY; without even the implied warranty of
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek GNU General Public License for more details.
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek You should have received a copy of the GNU General Public License
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek along with this program. If not, see <http://www.gnu.org/licenses/>.
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek*/
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek#include <talloc.h>
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek#include <tevent.h>
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek#include <errno.h>
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek#include <popt.h>
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek#include <arpa/inet.h>
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek#include <netinet/in.h>
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek#include <sys/types.h>
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek#include <stdarg.h>
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek#include <stdlib.h>
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek#include "providers/fail_over_srv.h"
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek#include "tests/cmocka/common_mock.h"
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek#include "tests/cmocka/common_mock_resp.h"
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek#define TEST_RESOLV_TIMEOUT 5
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek#define TEST_FO_TIMEOUT 3000
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek#define TEST_SRV_TTL 500
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek#define TEST_SRV_SHORT_TTL 2
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstatic TALLOC_CTX *global_mock_context = NULL;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekenum host_database default_host_dbs[] = { DB_FILES, DB_DNS, DB_SENTINEL };
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstruct resolv_ctx {
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek int foo;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek};
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek/* mock resolver interface. The resolver test is separate */
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekint resolv_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev_ctx,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek int timeout, struct resolv_ctx **ctxp)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek{
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek *ctxp = talloc(mem_ctx, struct resolv_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek return EOK;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek}
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstruct tevent_req *
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekresolv_gethostbyname_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct resolv_ctx *ctx, const char *name,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek enum restrict_family family_order,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek enum host_database *db)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek{
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek return test_req_succeed_send(mem_ctx, ev);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek}
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekint resolv_gethostbyname_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek int *status, int *timeouts,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct resolv_hostent **rhostent)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek{
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek return test_request_recv(req);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek}
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekconst char *resolv_strerror(int ares_code)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek{
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek return NULL;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek}
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstruct tevent_req *resolv_discover_srv_send(TALLOC_CTX *mem_ctx,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct tevent_context *ev,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct resolv_ctx *resolv_ctx,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek const char *service,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek const char *protocol,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek const char **discovery_domains)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek{
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek return test_req_succeed_send(mem_ctx, ev);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek}
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekerrno_t resolv_discover_srv_recv(TALLOC_CTX *mem_ctx,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct tevent_req *req,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct ares_srv_reply **_reply_list,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek uint32_t *_ttl,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek char **_dns_domain)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek{
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct ares_srv_reply *reply_list;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek uint32_t ttl;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek char *dns_domain;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek /* Need to always consume all mocked values */
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek reply_list = sss_mock_ptr_type(struct ares_srv_reply *);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek ttl = sss_mock_ptr_type(uint32_t);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek dns_domain = sss_mock_ptr_type(char *);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek if (_reply_list != NULL) {
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek *_reply_list = reply_list;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek }
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek if (_ttl != NULL) {
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek *_ttl = ttl;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek }
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek if (_dns_domain != NULL) {
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek *_dns_domain = dns_domain;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek }
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek return test_request_recv(req);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek}
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstruct ares_srv_reply *pop_lowest_prio(struct ares_srv_reply **r)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek{
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct ares_srv_reply *lowest;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct ares_srv_reply *iter;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct ares_srv_reply *prev;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek lowest = *r;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek iter = lowest;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek while (iter != NULL) {
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek if (iter->priority < lowest->priority) {
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek lowest = iter;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek }
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek iter = iter->next;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek }
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek prev = NULL;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek iter = *r;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek while (iter != lowest) {
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek prev = iter;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek iter = iter->next;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek }
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek /* iter points to the lowest prio. Prev points to the item before */
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek if (prev) {
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek prev->next = lowest->next;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek } else {
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek *r = lowest->next;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek }
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek return lowest;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek}
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekint resolv_sort_srv_reply(struct ares_srv_reply **reply)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek{
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct ares_srv_reply *r;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct ares_srv_reply *lowest;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct ares_srv_reply *sorted = NULL;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct ares_srv_reply *sorted_head = NULL;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek r = *reply;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek if (r == NULL || r->next == NULL) {
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek return EOK;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek }
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek do {
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek lowest = pop_lowest_prio(&r);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek if (sorted) {
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek sorted->next = lowest;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek sorted = sorted->next;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek } else {
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek sorted = lowest;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek sorted_head = sorted;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek }
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek } while (r != NULL);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek *reply = sorted_head;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek return EOK;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek}
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstruct tevent_req *resolv_get_domain_send(TALLOC_CTX *mem_ctx,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct tevent_context *ev,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct resolv_ctx *resolv_ctx,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek const char *hostname,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek enum host_database *host_dbs,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek enum restrict_family family_order)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek{
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek return test_req_succeed_send(mem_ctx, ev);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek}
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekerrno_t resolv_get_domain_recv(TALLOC_CTX *mem_ctx,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct tevent_req *req,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek char **_dns_domain)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek{
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek return test_request_recv(req);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek}
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek/* The unit test */
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozekstruct test_fo_ctx {
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct resolv_ctx *resolv;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct fo_ctx *fo_ctx;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct fo_resolve_srv_dns_ctx *srv_ctx;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct fo_service *fo_svc;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct sss_test_ctx *ctx;
9797aa5907191cef5db8279e20ec75fd0abbe980Jakub Hrozek int ttl;
10c07e188323a2f9824b5e34379f3b1a9b37759eJakub Hrozek
10c07e188323a2f9824b5e34379f3b1a9b37759eJakub Hrozek struct fo_server *srv;
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek int num_done;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek};
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekint test_fo_srv_data_cmp(void *ud1, void *ud2)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek{
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek return strcasecmp((char*) ud1, (char*) ud2);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek}
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozekstatic int test_fo_setup(void **state)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek{
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek struct test_fo_ctx *test_ctx;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek errno_t ret;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct fo_options fopts;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_true(leak_check_setup());
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek global_mock_context = talloc_new(global_talloc_context);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_non_null(global_mock_context);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek test_ctx = talloc_zero(global_mock_context,
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek struct test_fo_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_non_null(test_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek test_ctx->ctx = create_ev_test_ctx(test_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_non_null(test_ctx->ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek ret = resolv_init(test_ctx, test_ctx->ctx->ev,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek TEST_RESOLV_TIMEOUT, &test_ctx->resolv);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_non_null(test_ctx->resolv);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek memset(&fopts, 0, sizeof(fopts));
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek fopts.retry_timeout = TEST_FO_TIMEOUT;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek fopts.family_order = IPV4_FIRST;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek test_ctx->fo_ctx = fo_context_init(test_ctx, &fopts);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_non_null(test_ctx->fo_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek ret = fo_new_service(test_ctx->fo_ctx, "ldap",
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek test_fo_srv_data_cmp,
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek &test_ctx->fo_svc);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek assert_int_equal(ret, ERR_OK);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek *state = test_ctx;
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek return 0;
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek}
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozekstatic int test_fo_teardown(void **state)
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek{
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek struct test_fo_ctx *test_ctx =
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek talloc_get_type(*state, struct test_fo_ctx);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek talloc_free(test_ctx);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek talloc_free(global_mock_context);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek assert_true(leak_check_teardown());
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek return 0;
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek}
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozekstatic int test_fo_srv_setup(void **state)
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek{
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek struct test_fo_ctx *test_ctx;
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek bool ok;
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek test_fo_setup(state);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek test_ctx = *state;
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek test_ctx->srv_ctx = fo_resolve_srv_dns_ctx_init(test_ctx, test_ctx->resolv,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek IPV4_FIRST, default_host_dbs,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek "client.sssd.com", "sssd.local");
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_non_null(test_ctx->srv_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek ok = fo_set_srv_lookup_plugin(test_ctx->fo_ctx,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek fo_resolve_srv_dns_send,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek fo_resolve_srv_dns_recv,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek test_ctx->srv_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_true(ok);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek *state = test_ctx;
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek return 0;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek}
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozekstatic int test_fo_srv_teardown(void **state)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek{
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek test_fo_teardown(state);
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek return 0;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek}
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek/* reply_list and dns_domain must be a talloc context so it can be used as
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek * talloc_steal argument later
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek */
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstatic void mock_srv_results(struct ares_srv_reply *reply_list,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek uint32_t ttl,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek char *dns_domain)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek{
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek will_return(resolv_discover_srv_recv, reply_list);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek will_return(resolv_discover_srv_recv, ttl);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek will_return(resolv_discover_srv_recv, dns_domain);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek}
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozekstatic void check_server(struct test_fo_ctx *ctx,
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek struct fo_server *srv,
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek int port,
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek const char *name)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek{
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_non_null(srv);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_int_equal(fo_get_server_port(srv), port);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_string_equal(fo_get_server_name(srv), name);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek if (ctx->srv_ctx) {
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek assert_true(fo_is_srv_lookup(srv));
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek }
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek}
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozekstatic void test_fo_srv_step1(struct test_fo_ctx *test_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstatic void test_fo_srv_done1(struct tevent_req *req);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstatic void test_fo_srv_done2(struct tevent_req *req);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstatic void test_fo_srv_done3(struct tevent_req *req);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstatic void test_fo_srv_done4(struct tevent_req *req);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozekstatic void test_fo_srv_done5(struct tevent_req *req);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozekstruct ares_srv_reply *
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozekmock_ares_reply(TALLOC_CTX *mem_ctx, const char *hostname,
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek int weight, int priority, int port)
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek{
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek struct ares_srv_reply *s;
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek s = talloc_zero(mem_ctx, struct ares_srv_reply);
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek if (s == NULL) {
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek return NULL;
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek }
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek s->host = talloc_strdup(s, hostname);
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek if (s->host == NULL) {
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek talloc_free(s);
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek return NULL;
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek }
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek s->weight = weight;
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek s->priority = priority;
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek s->port = port;
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek return s;
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek}
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozekstatic void test_fo_srv_mock_dns(struct test_fo_ctx *test_ctx,
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek int ttl)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek{
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct ares_srv_reply *s1;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct ares_srv_reply *s2;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek char *dns_domain;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek s1 = mock_ares_reply(test_ctx, "ldap1.sssd.com", 100, 1, 389);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_non_null(s1);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek s2 = mock_ares_reply(test_ctx, "ldap2.sssd.com", 100, 2, 389);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_non_null(s2);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek s1->next = s2;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek dns_domain = talloc_strdup(test_ctx, "sssd.com");
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_non_null(dns_domain);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek mock_srv_results(s1, ttl, dns_domain);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek}
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozekstatic void test_fo_srv(void **state)
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek{
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek errno_t ret;
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek struct test_fo_ctx *test_ctx =
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek talloc_get_type(*state, struct test_fo_ctx);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek test_fo_srv_mock_dns(test_ctx, TEST_SRV_TTL);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek ret = fo_add_srv_server(test_ctx->fo_svc, "_ldap", "sssd.com",
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek "sssd.local", "tcp", test_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_int_equal(ret, ERR_OK);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek test_fo_srv_step1(test_ctx);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek ret = test_ev_loop(test_ctx->ctx);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek assert_int_equal(ret, ERR_OK);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek}
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozekstatic void test_fo_srv_step1(struct test_fo_ctx *test_ctx)
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek{
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek struct tevent_req *req;
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek req = fo_resolve_service_send(test_ctx, test_ctx->ctx->ev,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek test_ctx->resolv, test_ctx->fo_ctx,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek test_ctx->fo_svc);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_non_null(req);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek tevent_req_set_callback(req, test_fo_srv_done1, test_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek}
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstatic void test_fo_srv_done1(struct tevent_req *req)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek{
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek struct test_fo_ctx *test_ctx = \
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek tevent_req_callback_data(req, struct test_fo_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct fo_server *srv;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek errno_t ret;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
10c07e188323a2f9824b5e34379f3b1a9b37759eJakub Hrozek ret = fo_resolve_service_recv(req, req, &srv);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek talloc_zfree(req);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_int_equal(ret, ERR_OK);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek /* ldap1.sssd.com has lower priority, it must always be first */
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek check_server(test_ctx, srv, 389, "ldap1.sssd.com");
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek /* Mark the server as working and request the service again. The same server
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek * must be returned */
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek fo_set_server_status(srv, SERVER_WORKING);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek req = fo_resolve_service_send(test_ctx, test_ctx->ctx->ev,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek test_ctx->resolv, test_ctx->fo_ctx,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek test_ctx->fo_svc);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_non_null(req);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek tevent_req_set_callback(req, test_fo_srv_done2, test_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek}
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstatic void test_fo_srv_done2(struct tevent_req *req)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek{
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek struct test_fo_ctx *test_ctx = \
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek tevent_req_callback_data(req, struct test_fo_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct fo_server *srv;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek errno_t ret;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
10c07e188323a2f9824b5e34379f3b1a9b37759eJakub Hrozek ret = fo_resolve_service_recv(req, req, &srv);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek talloc_zfree(req);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_int_equal(ret, ERR_OK);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek /* Must be ldap1 again */
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek check_server(test_ctx, srv, 389, "ldap1.sssd.com");
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek /* Mark it at wrong, next lookup should yield ldap2 */
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek fo_set_server_status(srv, SERVER_NOT_WORKING);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek req = fo_resolve_service_send(test_ctx, test_ctx->ctx->ev,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek test_ctx->resolv, test_ctx->fo_ctx,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek test_ctx->fo_svc);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_non_null(req);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek tevent_req_set_callback(req, test_fo_srv_done3, test_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek}
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstatic void test_fo_srv_done3(struct tevent_req *req)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek{
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek struct test_fo_ctx *test_ctx = \
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek tevent_req_callback_data(req, struct test_fo_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct fo_server *srv;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek errno_t ret;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
10c07e188323a2f9824b5e34379f3b1a9b37759eJakub Hrozek ret = fo_resolve_service_recv(req, req, &srv);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek talloc_zfree(req);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_int_equal(ret, ERR_OK);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek /* Must be ldap2 now */
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek check_server(test_ctx, srv, 389, "ldap2.sssd.com");
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek /* Mark is at wrong, next lookup must reach the end of the server list */
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek fo_set_server_status(srv, SERVER_NOT_WORKING);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek req = fo_resolve_service_send(test_ctx, test_ctx->ctx->ev,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek test_ctx->resolv, test_ctx->fo_ctx,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek test_ctx->fo_svc);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_non_null(req);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek tevent_req_set_callback(req, test_fo_srv_done4, test_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek}
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstatic void test_fo_srv_done4(struct tevent_req *req)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek{
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek struct test_fo_ctx *test_ctx = \
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek tevent_req_callback_data(req, struct test_fo_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct fo_server *srv;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek errno_t ret;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
10c07e188323a2f9824b5e34379f3b1a9b37759eJakub Hrozek ret = fo_resolve_service_recv(req, req, &srv);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek talloc_zfree(req);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek /* No servers are left..*/
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_int_equal(ret, ENOENT);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek /* reset the server status and try again.. */
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek fo_reset_servers(test_ctx->fo_svc);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek if (test_ctx->srv_ctx) {
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek test_fo_srv_mock_dns(test_ctx, TEST_SRV_TTL);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek }
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek req = fo_resolve_service_send(test_ctx, test_ctx->ctx->ev,
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek test_ctx->resolv, test_ctx->fo_ctx,
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek test_ctx->fo_svc);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek assert_non_null(req);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek tevent_req_set_callback(req, test_fo_srv_done5, test_ctx);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek}
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozekstatic void test_fo_srv_done5(struct tevent_req *req)
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek{
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek struct test_fo_ctx *test_ctx = \
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek tevent_req_callback_data(req, struct test_fo_ctx);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek struct fo_server *srv;
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek errno_t ret;
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek
10c07e188323a2f9824b5e34379f3b1a9b37759eJakub Hrozek ret = fo_resolve_service_recv(req, req, &srv);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek talloc_zfree(req);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek assert_int_equal(ret, ERR_OK);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek /* ldap1.sssd.com has lower priority, it must always be first */
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek check_server(test_ctx, srv, 389, "ldap1.sssd.com");
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek /* OK, we made a full circle with the test, done */
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek test_ctx->ctx->error = ERR_OK;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek test_ctx->ctx->done = true;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek}
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek/* Make sure that two queries more than TTL seconds apart resolve
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek * into two different lists
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek */
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozekstatic void test_fo_srv_ttl_change_step(struct test_fo_ctx *test_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstatic void test_fo_srv_before(struct tevent_req *req);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstatic void test_fo_srv_after(struct tevent_req *req);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekvoid test_fo_srv_ttl_change(void **state)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek{
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek struct test_fo_ctx *test_ctx =
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek talloc_get_type(*state, struct test_fo_ctx);
9797aa5907191cef5db8279e20ec75fd0abbe980Jakub Hrozek
9797aa5907191cef5db8279e20ec75fd0abbe980Jakub Hrozek test_ctx->ttl = TEST_SRV_SHORT_TTL;
9797aa5907191cef5db8279e20ec75fd0abbe980Jakub Hrozek test_fo_srv_ttl_change_step(test_ctx);
9797aa5907191cef5db8279e20ec75fd0abbe980Jakub Hrozek}
9797aa5907191cef5db8279e20ec75fd0abbe980Jakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozekstatic void test_fo_srv_ttl_change_step(struct test_fo_ctx *test_ctx)
9797aa5907191cef5db8279e20ec75fd0abbe980Jakub Hrozek{
9797aa5907191cef5db8279e20ec75fd0abbe980Jakub Hrozek errno_t ret;
9797aa5907191cef5db8279e20ec75fd0abbe980Jakub Hrozek struct tevent_req *req;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek test_fo_srv_mock_dns(test_ctx, test_ctx->ttl);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek ret = fo_add_srv_server(test_ctx->fo_svc, "_ldap", "sssd.com",
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek "sssd.local", "tcp", test_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_int_equal(ret, ERR_OK);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek ret = fo_add_server(test_ctx->fo_svc, "ldap1.sssd.com",
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek 389, (void *) discard_const("ldap://ldap1.sssd.com"),
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek true);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_int_equal(ret, ERR_OK);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek req = fo_resolve_service_send(test_ctx, test_ctx->ctx->ev,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek test_ctx->resolv, test_ctx->fo_ctx,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek test_ctx->fo_svc);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_non_null(req);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek tevent_req_set_callback(req, test_fo_srv_before, test_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek ret = test_ev_loop(test_ctx->ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_int_equal(ret, ERR_OK);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek}
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstatic void test_fo_srv_before(struct tevent_req *req)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek{
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek struct test_fo_ctx *test_ctx = \
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek tevent_req_callback_data(req, struct test_fo_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct ares_srv_reply *s1;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct ares_srv_reply *s2;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek char *dns_domain;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek errno_t ret;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
10c07e188323a2f9824b5e34379f3b1a9b37759eJakub Hrozek ret = fo_resolve_service_recv(req, test_ctx, &test_ctx->srv);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek talloc_zfree(req);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_int_equal(ret, ERR_OK);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek DEBUG(SSSDBG_TRACE_FUNC, "Before TTL change\n");
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
10c07e188323a2f9824b5e34379f3b1a9b37759eJakub Hrozek check_server(test_ctx, test_ctx->srv, 389, "ldap1.sssd.com");
10c07e188323a2f9824b5e34379f3b1a9b37759eJakub Hrozek fo_set_server_status(test_ctx->srv, SERVER_WORKING);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek /* Simulate changing the DNS environment. Change the host names */
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek s1 = mock_ares_reply(test_ctx, "ldap1.sssd.com", 100, 2, 389);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_non_null(s1);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek s2 = mock_ares_reply(test_ctx, "ldap2.sssd.com", 100, 1, 389);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_non_null(s2);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek s1->next = s2;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek dns_domain = talloc_strdup(test_ctx, "sssd.com");
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_non_null(dns_domain);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
9797aa5907191cef5db8279e20ec75fd0abbe980Jakub Hrozek mock_srv_results(s1, test_ctx->ttl, dns_domain);
9797aa5907191cef5db8279e20ec75fd0abbe980Jakub Hrozek sleep(test_ctx->ttl + 1);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek req = fo_resolve_service_send(test_ctx, test_ctx->ctx->ev,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek test_ctx->resolv, test_ctx->fo_ctx,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek test_ctx->fo_svc);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_non_null(req);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek tevent_req_set_callback(req, test_fo_srv_after, test_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek}
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozekstatic void test_fo_srv_after2(struct tevent_req *req);
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstatic void test_fo_srv_after(struct tevent_req *req)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek{
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek struct test_fo_ctx *test_ctx = \
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek tevent_req_callback_data(req, struct test_fo_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct fo_server *srv;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek errno_t ret;
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek struct ares_srv_reply *s1;
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek struct ares_srv_reply *s2;
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek char *dns_domain;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
10c07e188323a2f9824b5e34379f3b1a9b37759eJakub Hrozek ret = fo_resolve_service_recv(req, req, &srv);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek talloc_zfree(req);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_int_equal(ret, ERR_OK);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
10c07e188323a2f9824b5e34379f3b1a9b37759eJakub Hrozek /* Try accessing server from a previous iteration. The
10c07e188323a2f9824b5e34379f3b1a9b37759eJakub Hrozek * server should be collapsed, but at least we shouldn't crash
10c07e188323a2f9824b5e34379f3b1a9b37759eJakub Hrozek */
10c07e188323a2f9824b5e34379f3b1a9b37759eJakub Hrozek fo_set_server_status(test_ctx->srv, SERVER_WORKING);
10c07e188323a2f9824b5e34379f3b1a9b37759eJakub Hrozek
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek sleep(test_ctx->ttl + 1);
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek /* Must be a different server now */
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek check_server(test_ctx, srv, 389, "ldap2.sssd.com");
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek /* Simulate changing the DNS environment. Change the host names */
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek s1 = mock_ares_reply(test_ctx, "ldap1.sssd.com", 100, 1, 389);
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek assert_non_null(s1);
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek s2 = mock_ares_reply(test_ctx, "ldap2.sssd.com", 100, 2, 389);
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek assert_non_null(s2);
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek s1->next = s2;
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek dns_domain = talloc_strdup(test_ctx, "sssd.com");
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek assert_non_null(dns_domain);
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek mock_srv_results(s1, test_ctx->ttl, dns_domain);
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek sleep(test_ctx->ttl + 1);
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek req = fo_resolve_service_send(test_ctx, test_ctx->ctx->ev,
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek test_ctx->resolv, test_ctx->fo_ctx,
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek test_ctx->fo_svc);
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek assert_non_null(req);
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek tevent_req_set_callback(req, test_fo_srv_after2, test_ctx);
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek}
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozekstatic void test_fo_srv_after2(struct tevent_req *req)
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek{
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek struct test_fo_ctx *test_ctx = \
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek tevent_req_callback_data(req, struct test_fo_ctx);
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek struct fo_server *srv;
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek errno_t ret;
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek ret = fo_resolve_service_recv(req, req, &srv);
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek talloc_zfree(req);
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek assert_int_equal(ret, ERR_OK);
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek /* Must be a different server now */
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek check_server(test_ctx, srv, 389, "ldap1.sssd.com");
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek test_ctx->ctx->error = ERR_OK;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek test_ctx->ctx->done = true;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek}
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
9797aa5907191cef5db8279e20ec75fd0abbe980Jakub Hrozekvoid test_fo_srv_ttl_zero(void **state)
9797aa5907191cef5db8279e20ec75fd0abbe980Jakub Hrozek{
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek struct test_fo_ctx *test_ctx =
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek talloc_get_type(*state, struct test_fo_ctx);
9797aa5907191cef5db8279e20ec75fd0abbe980Jakub Hrozek
9797aa5907191cef5db8279e20ec75fd0abbe980Jakub Hrozek test_ctx->ttl = 0;
9797aa5907191cef5db8279e20ec75fd0abbe980Jakub Hrozek test_fo_srv_ttl_change_step(test_ctx);
9797aa5907191cef5db8279e20ec75fd0abbe980Jakub Hrozek}
9797aa5907191cef5db8279e20ec75fd0abbe980Jakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozekstatic void test_fo_hostlist(void **state)
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek{
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek errno_t ret;
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek struct test_fo_ctx *test_ctx =
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek talloc_get_type(*state, struct test_fo_ctx);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek ret = fo_add_server(test_ctx->fo_svc,
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek "ldap1.sssd.com", 389, test_ctx, true);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek assert_int_equal(ret, ERR_OK);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek ret = fo_add_server(test_ctx->fo_svc,
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek "ldap2.sssd.com", 389, test_ctx, true);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek assert_int_equal(ret, ERR_OK);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek test_fo_srv_step1(test_ctx);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek ret = test_ev_loop(test_ctx->ctx);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek assert_int_equal(ret, ERR_OK);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek}
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozekstatic void test_fo_srv_dup_done(struct tevent_req *req);
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek/* Test that running two parallel SRV queries doesn't return an error.
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek * This is a regression test for https://fedorahosted.org/sssd/ticket/3131
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek */
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozekvoid test_fo_srv_duplicates(void **state)
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek{
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek errno_t ret;
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek struct tevent_req *req;
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek struct test_fo_ctx *test_ctx =
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek talloc_get_type(*state, struct test_fo_ctx);
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek test_fo_srv_mock_dns(test_ctx, test_ctx->ttl);
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek test_fo_srv_mock_dns(test_ctx, test_ctx->ttl);
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek ret = fo_add_srv_server(test_ctx->fo_svc, "_ldap", "sssd.com",
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek "sssd.local", "tcp", test_ctx);
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek assert_int_equal(ret, ERR_OK);
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek ret = fo_add_server(test_ctx->fo_svc, "ldap1.sssd.com",
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek 389, (void *) discard_const("ldap://ldap1.sssd.com"),
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek true);
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek assert_int_equal(ret, ERR_OK);
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek req = fo_resolve_service_send(test_ctx, test_ctx->ctx->ev,
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek test_ctx->resolv, test_ctx->fo_ctx,
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek test_ctx->fo_svc);
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek assert_non_null(req);
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek tevent_req_set_callback(req, test_fo_srv_dup_done, test_ctx);
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek req = fo_resolve_service_send(test_ctx, test_ctx->ctx->ev,
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek test_ctx->resolv, test_ctx->fo_ctx,
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek test_ctx->fo_svc);
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek assert_non_null(req);
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek tevent_req_set_callback(req, test_fo_srv_dup_done, test_ctx);
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek ret = test_ev_loop(test_ctx->ctx);
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek assert_int_equal(ret, ERR_OK);
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek}
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozekstatic void test_fo_srv_dup_done(struct tevent_req *req)
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek{
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek struct test_fo_ctx *test_ctx = \
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek tevent_req_callback_data(req, struct test_fo_ctx);
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek errno_t ret;
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek const char *name;
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek ret = fo_resolve_service_recv(req, test_ctx, &test_ctx->srv);
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek talloc_zfree(req);
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek assert_int_equal(ret, EOK);
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek name = fo_get_server_name(test_ctx->srv);
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek assert_string_equal(name, "ldap1.sssd.com");
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek test_ctx->num_done++;
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek if (test_ctx->num_done == 2) {
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek test_ctx->ctx->error = ERR_OK;
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek test_ctx->ctx->done = true;
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek }
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek}
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekint main(int argc, const char *argv[])
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek{
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek int rv;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek poptContext pc;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek int opt;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek struct poptOption long_options[] = {
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek POPT_AUTOHELP
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek SSSD_DEBUG_OPTS
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek POPT_TABLEEND
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek };
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek const struct CMUnitTest tests[] = {
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek cmocka_unit_test_setup_teardown(test_fo_hostlist,
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek test_fo_setup,
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek test_fo_teardown),
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek cmocka_unit_test_setup_teardown(test_fo_srv,
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek test_fo_srv_setup,
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek test_fo_srv_teardown),
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek cmocka_unit_test_setup_teardown(test_fo_srv_ttl_change,
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek test_fo_srv_setup,
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek test_fo_srv_teardown),
9797aa5907191cef5db8279e20ec75fd0abbe980Jakub Hrozek cmocka_unit_test_setup_teardown(test_fo_srv_ttl_zero,
9797aa5907191cef5db8279e20ec75fd0abbe980Jakub Hrozek test_fo_srv_setup,
9797aa5907191cef5db8279e20ec75fd0abbe980Jakub Hrozek test_fo_srv_teardown),
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek cmocka_unit_test_setup_teardown(test_fo_srv_duplicates,
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek test_fo_srv_setup,
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek test_fo_srv_teardown),
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek };
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek /* Set debug level to invalid value so we can deside if -d 0 was used. */
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek debug_level = SSSDBG_INVALID;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek pc = poptGetContext(argv[0], argc, argv, long_options, 0);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek while((opt = poptGetNextOpt(pc)) != -1) {
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek switch(opt) {
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek default:
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek fprintf(stderr, "\nInvalid option %s: %s\n\n",
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek poptBadOption(pc, 0), poptStrerror(opt));
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek poptPrintUsage(pc, stderr, 0);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek return 1;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek }
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek }
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek poptFreeContext(pc);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek DEBUG_CLI_INIT(debug_level);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek /* Even though normally the tests should clean up after themselves
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek * they might not after a failed run. Remove the old db to be sure */
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek tests_set_cwd();
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek rv = cmocka_run_group_tests(tests, NULL, NULL);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek return rv;
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek}