remote: add sysusers file to create 'libvirt' group
[libvirt.git] / tests / nsstest.c
blob4b7895db7e933bde44b3637192a0e1554dcea2f4
1 /*
2 * Copyright (C) 2016 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
19 #include <config.h>
21 #include "testutils.h"
23 #ifdef WITH_NSS
25 # include "libvirt_nss.h"
27 # define VIR_FROM_THIS VIR_FROM_NONE
29 # define BUF_SIZE 1024
31 struct testNSSData {
32 const char *hostname;
33 const char *const *ipAddr;
34 int af;
37 static int
38 testGetHostByName(const void *opaque)
40 const struct testNSSData *data = opaque;
41 const bool existent = data->hostname && data->ipAddr && data->ipAddr[0];
42 struct hostent resolved = { 0 };
43 char buf[BUF_SIZE] = { 0 };
44 char **addrList;
45 int rv, tmp_errno = 0, tmp_herrno = 0;
46 size_t i = 0;
48 rv = NSS_NAME(gethostbyname2)(data->hostname,
49 data->af,
50 &resolved,
51 buf, sizeof(buf),
52 &tmp_errno,
53 &tmp_herrno);
55 if (rv == NSS_STATUS_TRYAGAIN ||
56 rv == NSS_STATUS_UNAVAIL ||
57 rv == NSS_STATUS_RETURN) {
58 /* Resolving failed in unexpected fashion. */
59 virReportError(VIR_ERR_INTERNAL_ERROR,
60 "Resolving of %s failed due to internal error",
61 data->hostname);
62 return -1;
63 } else if (rv == NSS_STATUS_NOTFOUND) {
64 /* Resolving failed. Should it? */
65 if (!existent)
66 return 0;
67 else
68 virReportError(VIR_ERR_INTERNAL_ERROR,
69 "Resolving of %s failed",
70 data->hostname);
71 return -1;
74 /* Resolving succeeded. Should it? */
75 if (!existent) {
76 virReportError(VIR_ERR_INTERNAL_ERROR,
77 "Resolving of %s succeeded but was expected to fail",
78 data->hostname);
79 return -1;
82 /* Now lets see if resolved address match our expectations. */
84 if (!resolved.h_name) {
85 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
86 "resolved.h_name empty");
87 return -1;
90 if (data->af != AF_UNSPEC &&
91 resolved.h_addrtype != data->af) {
92 virReportError(VIR_ERR_INTERNAL_ERROR,
93 "Expected AF_INET (%d) got %d",
94 data->af, resolved.h_addrtype);
95 return -1;
98 if ((resolved.h_addrtype == AF_INET && resolved.h_length != 4) ||
99 (resolved.h_addrtype == AF_INET6 && resolved.h_length != 16)) {
100 /* IPv4 addresses are encoded into 4 bytes */
101 virReportError(VIR_ERR_INTERNAL_ERROR,
102 "Expected %d bytes long address, got %d",
103 resolved.h_addrtype == AF_INET ? 4 : 16,
104 resolved.h_length);
105 return -1;
108 if (!resolved.h_addr_list) {
109 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
110 "resolved.h_addr_list empty");
111 return -1;
114 addrList = resolved.h_addr_list;
115 i = 0;
116 while (*addrList) {
117 virSocketAddr sa = { 0 };
118 g_autofree char *ipAddr = NULL;
119 void *address = *addrList;
121 if (resolved.h_addrtype == AF_INET) {
122 virSocketAddrSetIPv4AddrNetOrder(&sa, *((uint32_t *) address));
123 } else {
124 virSocketAddrSetIPv6AddrNetOrder(&sa, address);
127 if (!(ipAddr = virSocketAddrFormat(&sa))) {
128 /* error reported by helper */
129 return -1;
132 if (STRNEQ_NULLABLE(data->ipAddr[i], ipAddr)) {
133 virReportError(VIR_ERR_INTERNAL_ERROR,
134 "Unexpected address %s, expecting %s",
135 ipAddr, NULLSTR(data->ipAddr[i]));
136 return -1;
139 addrList++;
140 i++;
143 if (data->ipAddr[i]) {
144 virReportError(VIR_ERR_INTERNAL_ERROR,
145 "Expected %s address, got NULL",
146 data->ipAddr[i]);
147 return -1;
150 return 0;
153 static int
154 mymain(void)
156 int ret = 0;
158 # define DO_TEST(name, family, ...) \
159 do { \
160 const char *addr[] = { __VA_ARGS__, NULL}; \
161 struct testNSSData data = { \
162 .hostname = name, .ipAddr = addr, .af = family, \
163 }; \
164 if (virTestRun(name, testGetHostByName, &data) < 0) \
165 ret = -1; \
166 } while (0)
168 # if !defined(LIBVIRT_NSS_GUEST)
169 DO_TEST("fedora", AF_INET, "192.168.122.197", "192.168.122.198", "192.168.122.199", "192.168.122.3");
170 DO_TEST("gentoo", AF_INET, "192.168.122.254");
171 DO_TEST("Gentoo", AF_INET, "192.168.122.254");
172 DO_TEST("gentoo", AF_INET6, "2001:1234:dead:beef::2");
173 DO_TEST("gentoo", AF_UNSPEC, "192.168.122.254");
174 DO_TEST("non-existent", AF_UNSPEC, NULL);
175 # else /* defined(LIBVIRT_NSS_GUEST) */
176 DO_TEST("debian", AF_INET, "192.168.122.2");
177 DO_TEST("suse", AF_INET, "192.168.122.3");
178 # endif /* defined(LIBVIRT_NSS_GUEST) */
180 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
183 VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("nss"))
184 #else
186 main(void)
188 return EXIT_AM_SKIP;
190 #endif