Blame SOURCES/0004-krb5-locator-add-unit-tests.patch

cdf651
From 3d6b8b306cdbd4ec15b36a1e7936d219204e08dc Mon Sep 17 00:00:00 2001
cdf651
From: Sumit Bose <sbose@redhat.com>
cdf651
Date: Thu, 24 May 2018 17:14:42 +0200
cdf651
Subject: [PATCH] krb5 locator: add unit tests
cdf651
cdf651
Unit test for existing and new functionality of the Kerberos locator
cdf651
plugin.
cdf651
cdf651
Related to https://pagure.io/SSSD/sssd/issue/941
cdf651
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
cdf651
cdf651
(cherry picked from commit 2124275fe494a0241a552538c70f40c2291f3795)
cdf651
---
cdf651
 Makefile.am                                   |  20 +
cdf651
 src/krb5_plugin/sssd_krb5_locator_plugin.c    |  16 +
cdf651
 .../cmocka/test_sssd_krb5_locator_plugin.c    | 631 ++++++++++++++++++
cdf651
 3 files changed, 667 insertions(+)
cdf651
 create mode 100644 src/tests/cmocka/test_sssd_krb5_locator_plugin.c
cdf651
cdf651
diff --git a/Makefile.am b/Makefile.am
cdf651
index 9539b3cff8544cf406e3e19ab23e76e9cc8234ee..9055130ed74057987795285c243ff47584cf8316 100644
cdf651
--- a/Makefile.am
cdf651
+++ b/Makefile.am
cdf651
@@ -288,6 +288,7 @@ if HAVE_CMOCKA
cdf651
         krb5_common_test \
cdf651
         test_iobuf \
cdf651
         sss_certmap_test \
cdf651
+        test_sssd_krb5_locator_plugin \
cdf651
         $(NULL)
cdf651
 
cdf651
 
cdf651
@@ -3518,6 +3519,25 @@ sss_certmap_test_LDADD = \
cdf651
     libsss_certmap.la \
cdf651
     $(NULL)
cdf651
 
cdf651
+test_sssd_krb5_locator_plugin_SOURCES = \
cdf651
+    src/tests/cmocka/test_sssd_krb5_locator_plugin.c \
cdf651
+    src/krb5_plugin/sssd_krb5_locator_plugin.c \
cdf651
+    $(NULL)
cdf651
+test_sssd_krb5_locator_plugin_CFLAGS = \
cdf651
+    $(AM_CFLAGS) \
cdf651
+    $(POPT_CFLAGS) \
cdf651
+    $(TALLOC_CFLAGS) \
cdf651
+    $(KRB5_CFLAGS) \
cdf651
+    -DTEST_PUBCONF_PATH=\"$(abs_builddir)/src/tests/cmocka/pubconf\" \
cdf651
+    $(NULL)
cdf651
+test_sssd_krb5_locator_plugin_LDADD = \
cdf651
+    $(CMOCKA_LIBS) \
cdf651
+    $(POPT_LIBS) \
cdf651
+    $(TALLOC_LIBS) \
cdf651
+    $(KRB5_LIBS) \
cdf651
+    libsss_test_common.la \
cdf651
+    $(NULL)
cdf651
+
cdf651
 if BUILD_KCM
cdf651
 test_kcm_json_SOURCES = \
cdf651
     src/tests/cmocka/test_kcm_json_marshalling.c \
cdf651
diff --git a/src/krb5_plugin/sssd_krb5_locator_plugin.c b/src/krb5_plugin/sssd_krb5_locator_plugin.c
cdf651
index 9874fd2d1ce63b69099f057dd05f6e353a12ce75..952d487c276ed51e0c3a018b0d0af59ca214525f 100644
cdf651
--- a/src/krb5_plugin/sssd_krb5_locator_plugin.c
cdf651
+++ b/src/krb5_plugin/sssd_krb5_locator_plugin.c
cdf651
@@ -38,6 +38,22 @@
cdf651
 
cdf651
 #include "providers/krb5/krb5_common.h"
cdf651
 
cdf651
+/* The following override of KDCINFO_TMPL and KPASSWDINFO_TMPL is not very
cdf651
+ * elegant but since they are defined in krb5_common.h with the help of
cdf651
+ * PUBCONF_PATH from config.h and PUBCONF_PATH can by set by a configure
cdf651
+ * options I didn't found another way to change the path for a unit test. */
cdf651
+#ifdef TEST_PUBCONF_PATH
cdf651
+#ifdef KDCINFO_TMPL
cdf651
+#undef KDCINFO_TMPL
cdf651
+#endif
cdf651
+#define KDCINFO_TMPL TEST_PUBCONF_PATH"/kdcinfo.%s"
cdf651
+
cdf651
+#ifdef KPASSWDINFO_TMPL
cdf651
+#undef KPASSWDINFO_TMPL
cdf651
+#endif
cdf651
+#define KPASSWDINFO_TMPL TEST_PUBCONF_PATH"/kpasswdinfo.%s"
cdf651
+#endif /* TEST_PUBCONF_PATH */
cdf651
+
cdf651
 #define DEFAULT_KERBEROS_PORT 88
cdf651
 #define DEFAULT_KADMIN_PORT 749
cdf651
 #define DEFAULT_KPASSWD_PORT 464
cdf651
diff --git a/src/tests/cmocka/test_sssd_krb5_locator_plugin.c b/src/tests/cmocka/test_sssd_krb5_locator_plugin.c
cdf651
new file mode 100644
cdf651
index 0000000000000000000000000000000000000000..3e7d00632ddb59da5474c0544eee6fc67edc5570
cdf651
--- /dev/null
cdf651
+++ b/src/tests/cmocka/test_sssd_krb5_locator_plugin.c
cdf651
@@ -0,0 +1,631 @@
cdf651
+/*
cdf651
+    SSSD
cdf651
+
cdf651
+    Unit test for SSSD's MIT Kerberos locator plugin
cdf651
+
cdf651
+    Authors:
cdf651
+        Sumit Bose <sbose@redhat.com>
cdf651
+
cdf651
+    Copyright (C) 2018 Red Hat
cdf651
+
cdf651
+    This program is free software; you can redistribute it and/or modify
cdf651
+    it under the terms of the GNU General Public License as published by
cdf651
+    the Free Software Foundation; either version 3 of the License, or
cdf651
+    (at your option) any later version.
cdf651
+
cdf651
+    This program is distributed in the hope that it will be useful,
cdf651
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
cdf651
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
cdf651
+    GNU General Public License for more details.
cdf651
+
cdf651
+    You should have received a copy of the GNU General Public License
cdf651
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
cdf651
+*/
cdf651
+#include "config.h"
cdf651
+
cdf651
+#include <popt.h>
cdf651
+#include <sys/types.h>
cdf651
+#include <sys/socket.h>
cdf651
+#include <sys/stat.h>
cdf651
+#include <string.h>
cdf651
+#include <fcntl.h>
cdf651
+#include <netdb.h>
cdf651
+#include <krb5/krb5.h>
cdf651
+#include <krb5/locate_plugin.h>
cdf651
+
cdf651
+#include "tests/cmocka/common_mock.h"
cdf651
+
cdf651
+#define TEST_REALM "TEST.REALM"
cdf651
+#define TEST_IP_1 "123.231.132.213"
cdf651
+#define TEST_IPV6_1_PURE "7025:4d2d:2b06:e321:d971:16c0:6eeb:cc41"
cdf651
+#define TEST_IPV6_1 "["TEST_IPV6_1_PURE"]"
cdf651
+#define TEST_SERVICE_1 "22334"
cdf651
+#define TEST_SERVICE_2 "54321"
cdf651
+#define TEST_IP_1_WITH_SERVICE TEST_IP_1":"TEST_SERVICE_1
cdf651
+#define TEST_IPV6_1_WITH_SERVICE TEST_IPV6_1":"TEST_SERVICE_2
cdf651
+
cdf651
+struct test_state {
cdf651
+    void *dummy;
cdf651
+};
cdf651
+
cdf651
+static int setup(void **state)
cdf651
+{
cdf651
+    struct test_state *ts = NULL;
cdf651
+
cdf651
+    assert_true(leak_check_setup());
cdf651
+
cdf651
+    ts = talloc(global_talloc_context, struct test_state);
cdf651
+    assert_non_null(ts);
cdf651
+
cdf651
+    check_leaks_push(ts);
cdf651
+    *state = (void *)ts;
cdf651
+
cdf651
+    unlink(TEST_PUBCONF_PATH"/kdcinfo."TEST_REALM);
cdf651
+    rmdir(TEST_PUBCONF_PATH);
cdf651
+
cdf651
+    return 0;
cdf651
+}
cdf651
+
cdf651
+static int teardown(void **state)
cdf651
+{
cdf651
+    struct test_state *ts = talloc_get_type_abort(*state, struct test_state);
cdf651
+
cdf651
+    assert_non_null(ts);
cdf651
+
cdf651
+    assert_true(check_leaks_pop(ts));
cdf651
+    talloc_free(ts);
cdf651
+    assert_true(leak_check_teardown());
cdf651
+    return 0;
cdf651
+}
cdf651
+
cdf651
+/* Taken from MIT Kerberos src/lib/krb5/os/locate_kdc.c and
cdf651
+ * lib/krb5/os/os-proto.h */
cdf651
+
cdf651
+typedef enum {
cdf651
+    TCP_OR_UDP = 0,
cdf651
+    TCP,
cdf651
+    UDP,
cdf651
+    HTTPS,
cdf651
+} k5_transport;
cdf651
+
cdf651
+/* A single server hostname or address. */
cdf651
+struct server_entry {
cdf651
+    char *hostname;             /* NULL -> use addrlen/addr instead */
cdf651
+    int port;                   /* Used only if hostname set */
cdf651
+    k5_transport transport;     /* May be 0 for UDP/TCP if hostname set */
cdf651
+    char *uri_path;             /* Used only if transport is HTTPS */
cdf651
+    int family;                 /* May be 0 (aka AF_UNSPEC) if hostname set */
cdf651
+    int master;                 /* True, false, or -1 for unknown. */
cdf651
+    size_t addrlen;
cdf651
+    struct sockaddr_storage addr;
cdf651
+};
cdf651
+
cdf651
+/* A list of server hostnames/addresses. */
cdf651
+struct serverlist {
cdf651
+    struct server_entry *servers;
cdf651
+    size_t nservers;
cdf651
+};
cdf651
+#define SERVERLIST_INIT { NULL, 0 }
cdf651
+
cdf651
+/* Free up everything pointed to by the serverlist structure, but don't
cdf651
+ *  * free the structure itself. */
cdf651
+void
cdf651
+k5_free_serverlist (struct serverlist *list)
cdf651
+{
cdf651
+    size_t i;
cdf651
+
cdf651
+    for (i = 0; i < list->nservers; i++) {
cdf651
+        free(list->servers[i].hostname);
cdf651
+        free(list->servers[i].uri_path);
cdf651
+    }
cdf651
+    free(list->servers);
cdf651
+    list->servers = NULL;
cdf651
+    list->nservers = 0;
cdf651
+}
cdf651
+
cdf651
+/* Make room for a new server entry in list and return a pointer to the new
cdf651
+ * entry.  (Do not increment list->nservers.) */
cdf651
+static struct server_entry *
cdf651
+new_server_entry(struct serverlist *list)
cdf651
+{
cdf651
+    struct server_entry *newservers, *entry;
cdf651
+    size_t newspace = (list->nservers + 1) * sizeof(struct server_entry);
cdf651
+
cdf651
+    newservers = realloc(list->servers, newspace);
cdf651
+    if (newservers == NULL)
cdf651
+        return NULL;
cdf651
+    list->servers = newservers;
cdf651
+    entry = &newservers[list->nservers];
cdf651
+    memset(entry, 0, sizeof(*entry));
cdf651
+    entry->master = -1;
cdf651
+    return entry;
cdf651
+}
cdf651
+
cdf651
+/* Add an address entry to list. */
cdf651
+static int
cdf651
+add_addr_to_list(struct serverlist *list, k5_transport transport, int family,
cdf651
+                 size_t addrlen, struct sockaddr *addr)
cdf651
+{
cdf651
+    struct server_entry *entry;
cdf651
+
cdf651
+    entry = new_server_entry(list);
cdf651
+    if (entry == NULL)
cdf651
+        return ENOMEM;
cdf651
+    entry->transport = transport;
cdf651
+    entry->family = family;
cdf651
+    entry->hostname = NULL;
cdf651
+    entry->uri_path = NULL;
cdf651
+    entry->addrlen = addrlen;
cdf651
+    memcpy(&entry->addr, addr, addrlen);
cdf651
+    list->nservers++;
cdf651
+    return 0;
cdf651
+}
cdf651
+
cdf651
+struct module_callback_data {
cdf651
+    int out_of_mem;
cdf651
+    struct serverlist *list;
cdf651
+};
cdf651
+
cdf651
+static int
cdf651
+module_callback(void *cbdata, int socktype, struct sockaddr *sa)
cdf651
+{
cdf651
+    struct module_callback_data *d = cbdata;
cdf651
+    size_t addrlen;
cdf651
+    k5_transport transport;
cdf651
+
cdf651
+    if (socktype != SOCK_STREAM && socktype != SOCK_DGRAM)
cdf651
+        return 0;
cdf651
+    if (sa->sa_family == AF_INET)
cdf651
+        addrlen = sizeof(struct sockaddr_in);
cdf651
+    else if (sa->sa_family == AF_INET6)
cdf651
+        addrlen = sizeof(struct sockaddr_in6);
cdf651
+    else
cdf651
+        return 0;
cdf651
+    transport = (socktype == SOCK_STREAM) ? TCP : UDP;
cdf651
+    if (add_addr_to_list(d->list, transport, sa->sa_family, addrlen,
cdf651
+                         sa) != 0) {
cdf651
+        /* Assumes only error is ENOMEM.  */
cdf651
+        d->out_of_mem = 1;
cdf651
+        return 1;
cdf651
+    }
cdf651
+    return 0;
cdf651
+}
cdf651
+
cdf651
+krb5_error_code sssd_krb5_locator_init(krb5_context context,
cdf651
+                                       void **private_data);
cdf651
+void sssd_krb5_locator_close(void *private_data);
cdf651
+
cdf651
+krb5_error_code sssd_krb5_locator_lookup(void *private_data,
cdf651
+                    enum locate_service_type svc,
cdf651
+                    const char *realm,
cdf651
+                    int socktype,
cdf651
+                    int family,
cdf651
+                    int (*cbfunc)(void *, int, struct sockaddr *),
cdf651
+                    void *cbdata);
cdf651
+
cdf651
+void test_init(void **state)
cdf651
+{
cdf651
+    krb5_context ctx;
cdf651
+    krb5_error_code kerr;
cdf651
+    void *priv;
cdf651
+
cdf651
+    kerr = krb5_init_context (&ctx;;
cdf651
+    assert_int_equal(kerr, 0);
cdf651
+
cdf651
+    kerr = sssd_krb5_locator_init(ctx, &priv;;
cdf651
+    assert_int_equal(kerr, 0);
cdf651
+
cdf651
+    sssd_krb5_locator_close(priv);
cdf651
+
cdf651
+    krb5_free_context(ctx);
cdf651
+}
cdf651
+
cdf651
+void test_failed_lookup(void **state)
cdf651
+{
cdf651
+    krb5_context ctx;
cdf651
+    krb5_error_code kerr;
cdf651
+    void *priv;
cdf651
+    struct module_callback_data cbdata = { 0 };
cdf651
+
cdf651
+
cdf651
+    kerr = krb5_init_context (&ctx;;
cdf651
+    assert_int_equal(kerr, 0);
cdf651
+
cdf651
+    kerr = sssd_krb5_locator_init(ctx, &priv;;
cdf651
+    assert_int_equal(kerr, 0);
cdf651
+
cdf651
+    kerr = sssd_krb5_locator_lookup(NULL, -1, NULL, -1, -1, NULL, NULL);
cdf651
+    assert_int_equal(kerr, KRB5_PLUGIN_NO_HANDLE);
cdf651
+
cdf651
+    kerr = sssd_krb5_locator_lookup(priv, -1, NULL, -1, -1, NULL, NULL);
cdf651
+    assert_int_equal(kerr, KRB5_PLUGIN_NO_HANDLE);
cdf651
+
cdf651
+    kerr = sssd_krb5_locator_lookup(priv, locate_service_kdc , NULL, -1, -1,
cdf651
+                                    NULL, NULL);
cdf651
+    assert_int_equal(kerr, KRB5_PLUGIN_NO_HANDLE);
cdf651
+
cdf651
+    kerr = sssd_krb5_locator_lookup(priv, locate_service_kdc , TEST_REALM, -1,
cdf651
+                                    -1, NULL, NULL);
cdf651
+    assert_int_equal(kerr, KRB5_PLUGIN_NO_HANDLE);
cdf651
+
cdf651
+    kerr = sssd_krb5_locator_lookup(priv, locate_service_kdc , TEST_REALM,
cdf651
+                                    SOCK_DGRAM, -1, NULL, NULL);
cdf651
+    assert_int_equal(kerr, KRB5_PLUGIN_NO_HANDLE);
cdf651
+
cdf651
+    kerr = sssd_krb5_locator_lookup(priv, locate_service_kdc , TEST_REALM,
cdf651
+                                    SOCK_DGRAM, AF_INET6, NULL, NULL);
cdf651
+    assert_int_equal(kerr, KRB5_PLUGIN_NO_HANDLE);
cdf651
+
cdf651
+    kerr = sssd_krb5_locator_lookup(priv, locate_service_kdc , TEST_REALM,
cdf651
+                                    SOCK_DGRAM, AF_INET6, module_callback,
cdf651
+                                    NULL);
cdf651
+    assert_int_equal(kerr, KRB5_PLUGIN_NO_HANDLE);
cdf651
+
cdf651
+    kerr = sssd_krb5_locator_lookup(priv, locate_service_kdc , TEST_REALM,
cdf651
+                                    SOCK_DGRAM, AF_INET6, module_callback,
cdf651
+                                    &cbdata);
cdf651
+    assert_int_equal(kerr, KRB5_PLUGIN_NO_HANDLE);
cdf651
+
cdf651
+    sssd_krb5_locator_close(priv);
cdf651
+
cdf651
+    krb5_free_context(ctx);
cdf651
+}
cdf651
+
cdf651
+void test_empty(void **state)
cdf651
+{
cdf651
+    krb5_context ctx;
cdf651
+    krb5_error_code kerr;
cdf651
+    void *priv;
cdf651
+    int fd;
cdf651
+    struct module_callback_data cbdata = { 0 };
cdf651
+
cdf651
+    kerr = krb5_init_context (&ctx;;
cdf651
+    assert_int_equal(kerr, 0);
cdf651
+
cdf651
+    kerr = sssd_krb5_locator_init(ctx, &priv;;
cdf651
+    assert_int_equal(kerr, 0);
cdf651
+
cdf651
+    mkdir(TEST_PUBCONF_PATH, 0777);
cdf651
+    fd = open(TEST_PUBCONF_PATH"/kdcinfo."TEST_REALM, O_CREAT, 0777);
cdf651
+    assert_int_not_equal(fd, -1);
cdf651
+    close(fd);
cdf651
+
cdf651
+    kerr = sssd_krb5_locator_lookup(priv, locate_service_kdc , TEST_REALM,
cdf651
+                                    SOCK_DGRAM, AF_INET6, module_callback,
cdf651
+                                    &cbdata);
cdf651
+    assert_int_equal(kerr, KRB5_PLUGIN_NO_HANDLE);
cdf651
+    unlink(TEST_PUBCONF_PATH"/kdcinfo."TEST_REALM);
cdf651
+    rmdir(TEST_PUBCONF_PATH);
cdf651
+
cdf651
+    sssd_krb5_locator_close(priv);
cdf651
+
cdf651
+    krb5_free_context(ctx);
cdf651
+}
cdf651
+
cdf651
+void test_single(void **state)
cdf651
+{
cdf651
+    krb5_context ctx;
cdf651
+    krb5_error_code kerr;
cdf651
+    void *priv;
cdf651
+    int fd;
cdf651
+    struct serverlist list = SERVERLIST_INIT;
cdf651
+    struct module_callback_data cbdata = { 0 };
cdf651
+    ssize_t s;
cdf651
+    int ret;
cdf651
+    char host[NI_MAXHOST];
cdf651
+    char service[NI_MAXSERV];
cdf651
+
cdf651
+    cbdata.list = &list;
cdf651
+
cdf651
+    kerr = krb5_init_context (&ctx;;
cdf651
+    assert_int_equal(kerr, 0);
cdf651
+
cdf651
+    kerr = sssd_krb5_locator_init(ctx, &priv;;
cdf651
+    assert_int_equal(kerr, 0);
cdf651
+
cdf651
+    mkdir(TEST_PUBCONF_PATH, 0777);
cdf651
+    fd = open(TEST_PUBCONF_PATH"/kdcinfo."TEST_REALM, O_CREAT|O_RDWR, 0777);
cdf651
+    assert_int_not_equal(fd, -1);
cdf651
+    s = write(fd, TEST_IP_1, sizeof(TEST_IP_1));
cdf651
+    assert_int_equal(s, sizeof(TEST_IP_1));
cdf651
+    close(fd);
cdf651
+
cdf651
+    kerr = sssd_krb5_locator_lookup(priv, locate_service_kdc , TEST_REALM,
cdf651
+                                    SOCK_DGRAM, AF_INET6, module_callback,
cdf651
+                                    &cbdata);
cdf651
+    assert_int_equal(kerr, 0);
cdf651
+
cdf651
+    /* We asked for AF_INET6, but TEST_IP_1 is IPv4 */
cdf651
+    assert_int_equal(list.nservers, 0);
cdf651
+    assert_null(list.servers);
cdf651
+
cdf651
+    kerr = sssd_krb5_locator_lookup(priv, locate_service_kdc , TEST_REALM,
cdf651
+                                    SOCK_DGRAM, AF_INET, module_callback,
cdf651
+                                    &cbdata);
cdf651
+    assert_int_equal(kerr, 0);
cdf651
+    assert_int_equal(list.nservers, 1);
cdf651
+    assert_non_null(list.servers);
cdf651
+    assert_int_equal(list.servers[0].addrlen, 16);
cdf651
+    ret = getnameinfo((struct sockaddr *) &list.servers[0].addr,
cdf651
+                      list.servers[0].addrlen,
cdf651
+                      host, sizeof(host), service, sizeof(service),
cdf651
+                      NI_NUMERICHOST|NI_NUMERICSERV);
cdf651
+    assert_int_equal(ret, 0);
cdf651
+    assert_string_equal(TEST_IP_1, host);
cdf651
+    assert_string_equal("88", service);
cdf651
+
cdf651
+    k5_free_serverlist(&list);
cdf651
+
cdf651
+    kerr = sssd_krb5_locator_lookup(priv, locate_service_kdc , TEST_REALM,
cdf651
+                                    SOCK_DGRAM, AF_UNSPEC, module_callback,
cdf651
+                                    &cbdata);
cdf651
+    assert_int_equal(kerr, 0);
cdf651
+    assert_int_equal(list.nservers, 1);
cdf651
+    assert_non_null(list.servers);
cdf651
+    assert_int_equal(list.servers[0].addrlen, 16);
cdf651
+    ret = getnameinfo((struct sockaddr *) &list.servers[0].addr,
cdf651
+                      list.servers[0].addrlen,
cdf651
+                      host, sizeof(host), service, sizeof(service),
cdf651
+                      NI_NUMERICHOST|NI_NUMERICSERV);
cdf651
+    assert_int_equal(ret, 0);
cdf651
+    assert_string_equal(TEST_IP_1, host);
cdf651
+    assert_string_equal("88", service);
cdf651
+
cdf651
+    k5_free_serverlist(&list);
cdf651
+
cdf651
+    unlink(TEST_PUBCONF_PATH"/kdcinfo."TEST_REALM);
cdf651
+    rmdir(TEST_PUBCONF_PATH);
cdf651
+    sssd_krb5_locator_close(priv);
cdf651
+
cdf651
+    krb5_free_context(ctx);
cdf651
+}
cdf651
+
cdf651
+struct test_data {
cdf651
+    const char *ip;
cdf651
+    bool found;
cdf651
+};
cdf651
+
cdf651
+void test_multi_check_results(struct test_data *test_data,
cdf651
+                              struct serverlist *list,
cdf651
+                              const char *exp_service)
cdf651
+{
cdf651
+    int ret;
cdf651
+    char host[NI_MAXHOST];
cdf651
+    char service[NI_MAXSERV];
cdf651
+    size_t c;
cdf651
+    size_t d;
cdf651
+
cdf651
+    /* To make sure each result from list has a matching entry in test_data we
cdf651
+     * use a flag to mark found entries, this way we can properly detect is
cdf651
+     * the same address is used multiple times. */
cdf651
+    for (d = 0; test_data[d].ip != NULL; d++) {
cdf651
+        test_data[d].found = false;
cdf651
+    }
cdf651
+
cdf651
+    for (c = 0; c < list->nservers; c++) {
cdf651
+        ret = getnameinfo((struct sockaddr *) &list->servers[c].addr,
cdf651
+                          list->servers[c].addrlen,
cdf651
+                          host, sizeof(host), service, sizeof(service),
cdf651
+                          NI_NUMERICHOST|NI_NUMERICSERV);
cdf651
+        assert_int_equal(ret, 0);
cdf651
+        assert_string_equal(exp_service, service);
cdf651
+        for (d = 0; test_data[d].ip != NULL; d++) {
cdf651
+            /* Compare result with test_data, be aware that the test_data has
cdf651
+             * '[]' around IPv& addresses */
cdf651
+            if (strncmp(host,
cdf651
+                        test_data[d].ip + (test_data[d].ip[0] == '[' ? 1 : 0),
cdf651
+                        strlen(host)) == 0 && !test_data[d].found) {
cdf651
+                test_data[d].found = true;
cdf651
+                break;
cdf651
+            }
cdf651
+        }
cdf651
+        /* Make sure we found the result in the list */
cdf651
+        assert_non_null(test_data[d].ip);
cdf651
+    }
cdf651
+}
cdf651
+
cdf651
+void test_multi(void **state)
cdf651
+{
cdf651
+    krb5_context ctx;
cdf651
+    krb5_error_code kerr;
cdf651
+    void *priv;
cdf651
+    int fd;
cdf651
+    struct serverlist list = SERVERLIST_INIT;
cdf651
+    struct module_callback_data cbdata = { 0 };
cdf651
+    ssize_t s;
cdf651
+    size_t c;
cdf651
+    struct test_data test_data[] = {
cdf651
+                           {TEST_IP_1, false},
cdf651
+                           {TEST_IPV6_1, false},
cdf651
+                           {"[c89a:565b:4510:5b9f:41fe:ea81:87a0:f21b]", false},
cdf651
+                           {"155.42.66.53", false},
cdf651
+                           {"[f812:5941:ba69:2bae:e806:3b68:770d:d75e]", false},
cdf651
+                           {"[3ad3:9dda:50e4:3c82:548f:eaa1:e120:6dd]", false},
cdf651
+                           {"55.116.79.183", false},
cdf651
+                           {"[ce8a:ee99:98cd:d8cd:218d:393e:d5a9:dc52]", false},
cdf651
+                           /* the following address is added twice to check if
cdf651
+                            * an address can be added more than once. */
cdf651
+                           {"37.230.88.162", false},
cdf651
+                           {"37.230.88.162", false},
cdf651
+                           {NULL, false} };
cdf651
+
cdf651
+    cbdata.list = &list;
cdf651
+
cdf651
+    kerr = krb5_init_context (&ctx;;
cdf651
+    assert_int_equal(kerr, 0);
cdf651
+
cdf651
+    kerr = sssd_krb5_locator_init(ctx, &priv;;
cdf651
+    assert_int_equal(kerr, 0);
cdf651
+
cdf651
+    mkdir(TEST_PUBCONF_PATH, 0777);
cdf651
+    fd = open(TEST_PUBCONF_PATH"/kdcinfo."TEST_REALM, O_CREAT|O_RDWR, 0777);
cdf651
+    assert_int_not_equal(fd, -1);
cdf651
+    for (c = 0; test_data[c].ip != NULL; c++) {
cdf651
+        s = write(fd, test_data[c].ip, strlen(test_data[c].ip));
cdf651
+        assert_int_equal(s, strlen(test_data[c].ip));
cdf651
+        s = write(fd, "\n", 1);
cdf651
+        assert_int_equal(s, 1);
cdf651
+    }
cdf651
+    close(fd);
cdf651
+
cdf651
+    kerr = sssd_krb5_locator_lookup(priv, locate_service_kdc , TEST_REALM,
cdf651
+                                    SOCK_DGRAM, AF_INET6, module_callback,
cdf651
+                                    &cbdata);
cdf651
+    assert_int_equal(kerr, 0);
cdf651
+
cdf651
+    assert_int_equal(list.nservers, 5);
cdf651
+    assert_non_null(list.servers);
cdf651
+    test_multi_check_results(test_data, &list, "88");
cdf651
+
cdf651
+    k5_free_serverlist(&list);
cdf651
+
cdf651
+    kerr = sssd_krb5_locator_lookup(priv, locate_service_kdc , TEST_REALM,
cdf651
+                                    SOCK_DGRAM, AF_INET, module_callback,
cdf651
+                                    &cbdata);
cdf651
+    assert_int_equal(kerr, 0);
cdf651
+
cdf651
+    assert_int_equal(list.nservers, 5);
cdf651
+    assert_non_null(list.servers);
cdf651
+    test_multi_check_results(test_data, &list, "88");
cdf651
+
cdf651
+
cdf651
+    k5_free_serverlist(&list);
cdf651
+
cdf651
+    kerr = sssd_krb5_locator_lookup(priv, locate_service_kdc , TEST_REALM,
cdf651
+                                    SOCK_DGRAM, AF_UNSPEC, module_callback,
cdf651
+                                    &cbdata);
cdf651
+    assert_int_equal(kerr, 0);
cdf651
+
cdf651
+    assert_int_equal(list.nservers, 10);
cdf651
+    assert_non_null(list.servers);
cdf651
+    test_multi_check_results(test_data, &list, "88");
cdf651
+
cdf651
+    k5_free_serverlist(&list);
cdf651
+
cdf651
+    unlink(TEST_PUBCONF_PATH"/kdcinfo."TEST_REALM);
cdf651
+    rmdir(TEST_PUBCONF_PATH);
cdf651
+    sssd_krb5_locator_close(priv);
cdf651
+
cdf651
+    krb5_free_context(ctx);
cdf651
+}
cdf651
+
cdf651
+void test_service(void **state)
cdf651
+{
cdf651
+    krb5_context ctx;
cdf651
+    krb5_error_code kerr;
cdf651
+    void *priv;
cdf651
+    int fd;
cdf651
+    struct serverlist list = SERVERLIST_INIT;
cdf651
+    struct module_callback_data cbdata = { 0 };
cdf651
+    ssize_t s;
cdf651
+    int ret;
cdf651
+    char host[NI_MAXHOST];
cdf651
+    char service[NI_MAXSERV];
cdf651
+
cdf651
+    cbdata.list = &list;
cdf651
+
cdf651
+    kerr = krb5_init_context (&ctx;;
cdf651
+    assert_int_equal(kerr, 0);
cdf651
+
cdf651
+    kerr = sssd_krb5_locator_init(ctx, &priv;;
cdf651
+    assert_int_equal(kerr, 0);
cdf651
+
cdf651
+    mkdir(TEST_PUBCONF_PATH, 0777);
cdf651
+    fd = open(TEST_PUBCONF_PATH"/kdcinfo."TEST_REALM, O_CREAT|O_RDWR, 0777);
cdf651
+    assert_int_not_equal(fd, -1);
cdf651
+    s = write(fd, TEST_IP_1_WITH_SERVICE, sizeof(TEST_IP_1_WITH_SERVICE));
cdf651
+    assert_int_equal(s, sizeof(TEST_IP_1_WITH_SERVICE));
cdf651
+    s = write(fd, "\n", 1);
cdf651
+    assert_int_equal(s, 1);
cdf651
+    s = write(fd, TEST_IPV6_1_WITH_SERVICE, sizeof(TEST_IPV6_1_WITH_SERVICE));
cdf651
+    assert_int_equal(s, sizeof(TEST_IPV6_1_WITH_SERVICE));
cdf651
+    close(fd);
cdf651
+
cdf651
+    kerr = sssd_krb5_locator_lookup(priv, locate_service_kdc , TEST_REALM,
cdf651
+                                    SOCK_DGRAM, AF_INET6, module_callback,
cdf651
+                                    &cbdata);
cdf651
+    assert_int_equal(kerr, 0);
cdf651
+
cdf651
+    assert_int_equal(list.nservers, 1);
cdf651
+    assert_non_null(list.servers);
cdf651
+    ret = getnameinfo((struct sockaddr *) &list.servers[0].addr,
cdf651
+                      list.servers[0].addrlen,
cdf651
+                      host, sizeof(host), service, sizeof(service),
cdf651
+                      NI_NUMERICHOST|NI_NUMERICSERV);
cdf651
+    assert_int_equal(ret, 0);
cdf651
+    assert_string_equal(TEST_IPV6_1_PURE, host);
cdf651
+    assert_string_equal(TEST_SERVICE_2, service);
cdf651
+
cdf651
+    k5_free_serverlist(&list);
cdf651
+
cdf651
+    kerr = sssd_krb5_locator_lookup(priv, locate_service_kdc , TEST_REALM,
cdf651
+                                    SOCK_DGRAM, AF_INET, module_callback,
cdf651
+                                    &cbdata);
cdf651
+    assert_int_equal(kerr, 0);
cdf651
+    assert_int_equal(list.nservers, 1);
cdf651
+    assert_non_null(list.servers);
cdf651
+    ret = getnameinfo((struct sockaddr *) &list.servers[0].addr,
cdf651
+                      list.servers[0].addrlen,
cdf651
+                      host, sizeof(host), service, sizeof(service),
cdf651
+                      NI_NUMERICHOST|NI_NUMERICSERV);
cdf651
+    assert_int_equal(ret, 0);
cdf651
+    assert_string_equal(TEST_IP_1, host);
cdf651
+    assert_string_equal(TEST_SERVICE_1, service);
cdf651
+
cdf651
+    k5_free_serverlist(&list);
cdf651
+
cdf651
+
cdf651
+    unlink(TEST_PUBCONF_PATH"/kdcinfo."TEST_REALM);
cdf651
+    rmdir(TEST_PUBCONF_PATH);
cdf651
+    sssd_krb5_locator_close(priv);
cdf651
+
cdf651
+    krb5_free_context(ctx);
cdf651
+}
cdf651
+
cdf651
+int main(int argc, const char *argv[])
cdf651
+{
cdf651
+    poptContext pc;
cdf651
+    int opt;
cdf651
+    int ret;
cdf651
+    struct poptOption long_options[] = {
cdf651
+        POPT_AUTOHELP
cdf651
+        SSSD_DEBUG_OPTS
cdf651
+        POPT_TABLEEND
cdf651
+    };
cdf651
+
cdf651
+    const struct CMUnitTest tests[] = {
cdf651
+        cmocka_unit_test_setup_teardown(test_init,
cdf651
+                                        setup, teardown),
cdf651
+        cmocka_unit_test_setup_teardown(test_failed_lookup,
cdf651
+                                        setup, teardown),
cdf651
+        cmocka_unit_test_setup_teardown(test_empty,
cdf651
+                                        setup, teardown),
cdf651
+        cmocka_unit_test_setup_teardown(test_single,
cdf651
+                                        setup, teardown),
cdf651
+        cmocka_unit_test_setup_teardown(test_multi,
cdf651
+                                        setup, teardown),
cdf651
+        cmocka_unit_test_setup_teardown(test_service,
cdf651
+                                        setup, teardown),
cdf651
+    };
cdf651
+
cdf651
+    /* Set debug level to invalid value so we can decide if -d 0 was used. */
cdf651
+    debug_level = SSSDBG_INVALID;
cdf651
+
cdf651
+    pc = poptGetContext(argv[0], argc, argv, long_options, 0);
cdf651
+    while((opt = poptGetNextOpt(pc)) != -1) {
cdf651
+        switch(opt) {
cdf651
+        default:
cdf651
+            fprintf(stderr, "\nInvalid option %s: %s\n\n",
cdf651
+                    poptBadOption(pc, 0), poptStrerror(opt));
cdf651
+            poptPrintUsage(pc, stderr, 0);
cdf651
+            return 1;
cdf651
+        }
cdf651
+    }
cdf651
+    poptFreeContext(pc);
cdf651
+
cdf651
+    DEBUG_CLI_INIT(debug_level);
cdf651
+
cdf651
+    ret = cmocka_run_group_tests(tests, NULL, NULL);
cdf651
+
cdf651
+    return ret;
cdf651
+}
cdf651
-- 
cdf651
2.17.1
cdf651