test_fo_srv.c revision a299f900981343904d7c9c5d148e30b8e0b2c460
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek Jakub Hrozek <jhrozek@redhat.com>
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek Copyright (C) 2014 Red Hat
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek SSSD tests: Resolver tests using a fake resolver library
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 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 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 Hrozekenum host_database default_host_dbs[] = { DB_FILES, DB_DNS, DB_SENTINEL };
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek/* mock resolver interface. The resolver test is separate */
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekint resolv_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev_ctx,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekresolv_gethostbyname_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekint resolv_gethostbyname_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstruct tevent_req *resolv_discover_srv_send(TALLOC_CTX *mem_ctx,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekerrno_t resolv_discover_srv_recv(TALLOC_CTX *mem_ctx,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek /* Need to always consume all mocked values */
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek reply_list = sss_mock_ptr_type(struct ares_srv_reply *);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstruct ares_srv_reply *pop_lowest_prio(struct ares_srv_reply **r)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek /* iter points to the lowest prio. Prev points to the item before */
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekint resolv_sort_srv_reply(struct ares_srv_reply **reply)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek } while (r != NULL);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstruct tevent_req *resolv_get_domain_send(TALLOC_CTX *mem_ctx,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekerrno_t resolv_get_domain_recv(TALLOC_CTX *mem_ctx,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek/* The unit test */
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek global_mock_context = talloc_new(global_talloc_context);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek test_ctx->fo_ctx = fo_context_init(test_ctx, &fopts);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek test_ctx->srv_ctx = fo_resolve_srv_dns_ctx_init(test_ctx, test_ctx->resolv,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek ok = fo_set_srv_lookup_plugin(test_ctx->fo_ctx,
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 Hrozekstatic void mock_srv_results(struct ares_srv_reply *reply_list,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek will_return(resolv_discover_srv_recv, reply_list);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek will_return(resolv_discover_srv_recv, dns_domain);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozekstatic void check_server(struct test_fo_ctx *ctx,
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek const char *name)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_int_equal(fo_get_server_port(srv), port);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek assert_string_equal(fo_get_server_name(srv), name);
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);
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozekmock_ares_reply(TALLOC_CTX *mem_ctx, const char *hostname,
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek s = talloc_zero(mem_ctx, struct ares_srv_reply);
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozekstatic void test_fo_srv_mock_dns(struct test_fo_ctx *test_ctx,
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek s1 = mock_ares_reply(test_ctx, "ldap1.sssd.com", 100, 1, 389);
4a4af8e1b6a9bab7c7a34d86055a400376e3829eJakub Hrozek s2 = mock_ares_reply(test_ctx, "ldap2.sssd.com", 100, 2, 389);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek dns_domain = talloc_strdup(test_ctx, "sssd.com");
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek ret = fo_add_srv_server(test_ctx->fo_svc, "_ldap", "sssd.com",
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozekstatic void test_fo_srv_step1(struct test_fo_ctx *test_ctx)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek req = fo_resolve_service_send(test_ctx, test_ctx->ctx->ev,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek tevent_req_set_callback(req, test_fo_srv_done1, test_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstatic void test_fo_srv_done1(struct tevent_req *req)
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek tevent_req_callback_data(req, struct test_fo_ctx);
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 /* Mark the server as working and request the service again. The same server
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek * must be returned */
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek req = fo_resolve_service_send(test_ctx, test_ctx->ctx->ev,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek tevent_req_set_callback(req, test_fo_srv_done2, test_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstatic void test_fo_srv_done2(struct tevent_req *req)
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek tevent_req_callback_data(req, struct test_fo_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek /* Must be ldap1 again */
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek check_server(test_ctx, srv, 389, "ldap1.sssd.com");
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek /* Mark it at wrong, next lookup should yield ldap2 */
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek req = fo_resolve_service_send(test_ctx, test_ctx->ctx->ev,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek tevent_req_set_callback(req, test_fo_srv_done3, test_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstatic void test_fo_srv_done3(struct tevent_req *req)
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek tevent_req_callback_data(req, struct test_fo_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek /* Must be ldap2 now */
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek check_server(test_ctx, srv, 389, "ldap2.sssd.com");
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek /* Mark is at wrong, next lookup must reach the end of the server list */
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek req = fo_resolve_service_send(test_ctx, test_ctx->ctx->ev,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek tevent_req_set_callback(req, test_fo_srv_done4, test_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstatic void test_fo_srv_done4(struct tevent_req *req)
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek tevent_req_callback_data(req, struct test_fo_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek /* No servers are left..*/
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek /* reset the server status and try again.. */
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek req = fo_resolve_service_send(test_ctx, test_ctx->ctx->ev,
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek tevent_req_set_callback(req, test_fo_srv_done5, test_ctx);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozekstatic void test_fo_srv_done5(struct tevent_req *req)
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek tevent_req_callback_data(req, struct test_fo_ctx);
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 /* OK, we made a full circle with the test, done */
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek/* Make sure that two queries more than TTL seconds apart resolve
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek * into two different lists
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);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozekstatic void test_fo_srv_ttl_change_step(struct test_fo_ctx *test_ctx)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek ret = fo_add_srv_server(test_ctx->fo_svc, "_ldap", "sssd.com",
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 req = fo_resolve_service_send(test_ctx, test_ctx->ctx->ev,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek tevent_req_set_callback(req, test_fo_srv_before, test_ctx);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstatic void test_fo_srv_before(struct tevent_req *req)
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek tevent_req_callback_data(req, struct test_fo_ctx);
10c07e188323a2f9824b5e34379f3b1a9b37759eJakub Hrozek ret = fo_resolve_service_recv(req, test_ctx, &test_ctx->srv);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek DEBUG(SSSDBG_TRACE_FUNC, "Before TTL change\n");
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 /* Simulate changing the DNS environment. Change the host names */
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek s1 = mock_ares_reply(test_ctx, "ldap1.sssd.com", 100, 2, 389);
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek s2 = mock_ares_reply(test_ctx, "ldap2.sssd.com", 100, 1, 389);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek dns_domain = talloc_strdup(test_ctx, "sssd.com");
9797aa5907191cef5db8279e20ec75fd0abbe980Jakub Hrozek mock_srv_results(s1, test_ctx->ttl, dns_domain);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek req = fo_resolve_service_send(test_ctx, test_ctx->ctx->ev,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek tevent_req_set_callback(req, test_fo_srv_after, test_ctx);
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozekstatic void test_fo_srv_after2(struct tevent_req *req);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekstatic void test_fo_srv_after(struct tevent_req *req)
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek tevent_req_callback_data(req, struct test_fo_ctx);
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 fo_set_server_status(test_ctx->srv, SERVER_WORKING);
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek /* Must be a different server now */
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek check_server(test_ctx, srv, 389, "ldap2.sssd.com");
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 s2 = mock_ares_reply(test_ctx, "ldap2.sssd.com", 100, 2, 389);
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek dns_domain = talloc_strdup(test_ctx, "sssd.com");
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek mock_srv_results(s1, test_ctx->ttl, dns_domain);
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek req = fo_resolve_service_send(test_ctx, test_ctx->ctx->ev,
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek tevent_req_set_callback(req, test_fo_srv_after2, test_ctx);
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozekstatic void test_fo_srv_after2(struct tevent_req *req)
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek tevent_req_callback_data(req, struct test_fo_ctx);
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek /* Must be a different server now */
a92f68763a57b211a1bf6b80b6dd80c4a1aa2738Jakub Hrozek check_server(test_ctx, srv, 389, "ldap1.sssd.com");
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozekstatic void test_fo_srv_dup_done(struct tevent_req *req);
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 ret = fo_add_srv_server(test_ctx->fo_svc, "_ldap", "sssd.com",
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 req = fo_resolve_service_send(test_ctx, test_ctx->ctx->ev,
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek tevent_req_set_callback(req, test_fo_srv_dup_done, test_ctx);
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek req = fo_resolve_service_send(test_ctx, test_ctx->ctx->ev,
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek tevent_req_set_callback(req, test_fo_srv_dup_done, test_ctx);
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozekstatic void test_fo_srv_dup_done(struct tevent_req *req)
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek tevent_req_callback_data(req, struct test_fo_ctx);
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek const char *name;
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek ret = fo_resolve_service_recv(req, test_ctx, &test_ctx->srv);
bc58e1cfee742178f95922d964349d6c262f6df7Jakub Hrozek cmocka_unit_test_setup_teardown(test_fo_hostlist,
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek cmocka_unit_test_setup_teardown(test_fo_srv_ttl_change,
9797aa5907191cef5db8279e20ec75fd0abbe980Jakub Hrozek cmocka_unit_test_setup_teardown(test_fo_srv_ttl_zero,
a299f900981343904d7c9c5d148e30b8e0b2c460Jakub Hrozek cmocka_unit_test_setup_teardown(test_fo_srv_duplicates,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek /* Set debug level to invalid value so we can deside if -d 0 was used. */
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek pc = poptGetContext(argv[0], argc, argv, long_options, 0);
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 */