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/>.
21 #include "testutils.h"
25 # include "libvirt_nss.h"
27 # define VIR_FROM_THIS VIR_FROM_NONE
29 # define BUF_SIZE 1024
33 const char *const *ipAddr
;
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 };
45 int rv
, tmp_errno
= 0, tmp_herrno
= 0;
48 rv
= NSS_NAME(gethostbyname2
)(data
->hostname
,
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",
63 } else if (rv
== NSS_STATUS_NOTFOUND
) {
64 /* Resolving failed. Should it? */
68 virReportError(VIR_ERR_INTERNAL_ERROR
,
69 "Resolving of %s failed",
74 /* Resolving succeeded. Should it? */
76 virReportError(VIR_ERR_INTERNAL_ERROR
,
77 "Resolving of %s succeeded but was expected to fail",
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");
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
);
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,
108 if (!resolved
.h_addr_list
) {
109 virReportError(VIR_ERR_INTERNAL_ERROR
, "%s",
110 "resolved.h_addr_list empty");
114 addrList
= resolved
.h_addr_list
;
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
));
124 virSocketAddrSetIPv6AddrNetOrder(&sa
, address
);
127 if (!(ipAddr
= virSocketAddrFormat(&sa
))) {
128 /* error reported by helper */
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
]));
143 if (data
->ipAddr
[i
]) {
144 virReportError(VIR_ERR_INTERNAL_ERROR
,
145 "Expected %s address, got NULL",
158 # define DO_TEST(name, family, ...) \
160 const char *addr[] = { __VA_ARGS__, NULL}; \
161 struct testNSSData data = { \
162 .hostname = name, .ipAddr = addr, .af = family, \
164 if (virTestRun(name, testGetHostByName, &data) < 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"))