nsstest.c 5.7 KB
Newer Older
M
Michal Privoznik 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright (C) 2016 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include "testutils.h"

R
Roman Bogorodskiy 已提交
23
#ifdef NSS
M
Michal Privoznik 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

# include <arpa/inet.h>
# include "libvirt_nss.h"
# include "virsocketaddr.h"

# define VIR_FROM_THIS VIR_FROM_NONE

# define BUF_SIZE 1024

struct testNSSData {
    const char *hostname;
    const char *const *ipAddr;
    int af;
};

static int
testGetHostByName(const void *opaque)
{
    const struct testNSSData *data = opaque;
    const bool existent = data->hostname && data->ipAddr && data->ipAddr[0];
    int ret = -1;
    struct hostent resolved;
    char buf[BUF_SIZE] = { 0 };
    char **addrList;
    int rv, tmp_errno = 0, tmp_herrno = 0;
49
    size_t i = 0;
M
Michal Privoznik 已提交
50 51 52

    memset(&resolved, 0, sizeof(resolved));

53 54 55 56 57 58
    rv = NSS_NAME(gethostbyname2)(data->hostname,
                                  data->af,
                                  &resolved,
                                  buf, sizeof(buf),
                                  &tmp_errno,
                                  &tmp_herrno);
M
Michal Privoznik 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

    if (rv == NSS_STATUS_TRYAGAIN ||
        rv == NSS_STATUS_UNAVAIL ||
        rv == NSS_STATUS_RETURN) {
        /* Resolving failed in unexpected fashion. */
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Resolving of %s failed due to internal error",
                       data->hostname);
        goto cleanup;
    } else if (rv == NSS_STATUS_NOTFOUND) {
        /* Resolving failed. Should it? */
        if (!existent)
            ret = 0;
        else
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "Resolving of %s failed",
                           data->hostname);
        goto cleanup;
    }

    /* Resolving succeeded. Should it? */
    if (!existent) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Resolving of %s succeeded but was expected to fail",
                       data->hostname);
        goto cleanup;
    }

    /* Now lets see if resolved address match our expectations. */

    if (!resolved.h_name) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "resolved.h_name empty");
        goto cleanup;
    }

    if (data->af != AF_UNSPEC &&
        resolved.h_addrtype != data->af) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Expected AF_INET (%d) got %d",
                       data->af, resolved.h_addrtype);
        goto cleanup;
    }

    if ((resolved.h_addrtype == AF_INET && resolved.h_length != 4) ||
        (resolved.h_addrtype == AF_INET6 && resolved.h_length != 16)) {
        /* IPv4 addresses are encoded into 4 bytes */
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Expected %d bytes long address, got %d",
                       resolved.h_addrtype == AF_INET ? 4 : 16,
                       resolved.h_length);
        goto cleanup;
    }

    if (!resolved.h_addr_list) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "resolved.h_addr_list empty");
        goto cleanup;
    }

    addrList = resolved.h_addr_list;
120
    i = 0;
M
Michal Privoznik 已提交
121 122 123
    while (*addrList) {
        virSocketAddr sa;
        char *ipAddr;
124
        void *address = *addrList;
M
Michal Privoznik 已提交
125 126 127 128

        memset(&sa, 0, sizeof(sa));

        if (resolved.h_addrtype == AF_INET) {
129
            virSocketAddrSetIPv4AddrNetOrder(&sa, *((uint32_t *) address));
M
Michal Privoznik 已提交
130
        } else {
131
            virSocketAddrSetIPv6AddrNetOrder(&sa, address);
M
Michal Privoznik 已提交
132 133 134 135 136 137 138
        }

        if (!(ipAddr = virSocketAddrFormat(&sa))) {
            /* error reported by helper */
            goto cleanup;
        }

139
        if (STRNEQ_NULLABLE(data->ipAddr[i], ipAddr)) {
M
Michal Privoznik 已提交
140
            virReportError(VIR_ERR_INTERNAL_ERROR,
141 142
                           "Unexpected address %s, expecting %s",
                           ipAddr, NULLSTR(data->ipAddr[i]));
M
Michal Privoznik 已提交
143 144 145 146 147 148 149 150 151
            VIR_FREE(ipAddr);
            goto cleanup;
        }
        VIR_FREE(ipAddr);

        addrList++;
        i++;
    }

152
    if (data->ipAddr[i]) {
M
Michal Privoznik 已提交
153
        virReportError(VIR_ERR_INTERNAL_ERROR,
154 155
                       "Expected %s address, got NULL",
                       data->ipAddr[i]);
M
Michal Privoznik 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168
        goto cleanup;
    }

    ret = 0;
 cleanup:
    return ret;
}

static int
mymain(void)
{
    int ret = 0;

169 170 171 172 173 174 175 176
# define DO_TEST(name, family, ...) \
    do { \
        const char *addr[] = { __VA_ARGS__, NULL}; \
        struct testNSSData data = { \
            .hostname = name, .ipAddr = addr, .af = family, \
        }; \
        if (virTestRun(name, testGetHostByName, &data) < 0) \
            ret = -1; \
M
Michal Privoznik 已提交
177 178
    } while (0)

179
# if !defined(LIBVIRT_NSS_GUEST)
M
Michal Privoznik 已提交
180 181 182 183 184
    DO_TEST("fedora", AF_INET, "192.168.122.197", "192.168.122.198", "192.168.122.199");
    DO_TEST("gentoo", AF_INET, "192.168.122.254");
    DO_TEST("gentoo", AF_INET6, "2001:1234:dead:beef::2");
    DO_TEST("gentoo", AF_UNSPEC, "192.168.122.254");
    DO_TEST("non-existent", AF_UNSPEC, NULL);
185 186 187 188
# else /* defined(LIBVIRT_NSS_GUEST) */
    DO_TEST("debian", AF_INET, "192.168.122.2");
    DO_TEST("suse", AF_INET, "192.168.122.3");
# endif /* defined(LIBVIRT_NSS_GUEST) */
M
Michal Privoznik 已提交
189 190 191 192

    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

193
VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("nss"))
M
Michal Privoznik 已提交
194 195 196 197 198 199 200
#else
int
main(void)
{
    return EXIT_AM_SKIP;
}
#endif