1/* Parse /etc/hosts in multi mode with a trailing long line (bug 21915).
2 Copyright (C) 2017-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19
20#include <dlfcn.h>
21#include <errno.h>
22#include <gnu/lib-names.h>
23#include <netdb.h>
24#include <nss.h>
25#include <support/check.h>
26#include <support/check_nss.h>
27#include <support/namespace.h>
28#include <support/test-driver.h>
29#include <support/xunistd.h>
30
31struct support_chroot *chroot_env;
32
33#define X10 "XXXXXXXXXX"
34#define X100 X10 X10 X10 X10 X10 X10 X10 X10 X10 X10
35#define X1000 X100 X100 X100 X100 X100 X100 X100 X100 X100 X100
36
37static void
38prepare (int argc, char **argv)
39{
40 chroot_env = support_chroot_create
41 ((struct support_chroot_configuration)
42 {
43 .resolv_conf = "",
44 .hosts =
45 "127.0.0.1 localhost localhost.localdomain\n"
46 "::1 localhost localhost.localdomain\n"
47 "192.0.2.1 example.com\n"
48 "#" X1000 X100 "\n",
49 .host_conf = "multi on\n",
50 });
51}
52
53static int
54do_test (void)
55{
56 support_become_root ();
57 if (!support_can_chroot ())
58 return EXIT_UNSUPPORTED;
59
60 __nss_configure_lookup (dbname: "hosts", string: "files");
61 if (dlopen (LIBNSS_FILES_SO, RTLD_LAZY) == NULL)
62 FAIL_EXIT1 ("could not load " LIBNSS_DNS_SO ": %s", dlerror ());
63
64 xchroot (path: chroot_env->path_chroot);
65
66 errno = ERANGE;
67 h_errno = NETDB_INTERNAL;
68 check_hostent (query_description: "gethostbyname example.com",
69 gethostbyname (name: "example.com"),
70 expected: "name: example.com\n"
71 "address: 192.0.2.1\n");
72 errno = ERANGE;
73 h_errno = NETDB_INTERNAL;
74 check_hostent (query_description: "gethostbyname2 AF_INET example.com",
75 gethostbyname2 (name: "example.com", AF_INET),
76 expected: "name: example.com\n"
77 "address: 192.0.2.1\n");
78 {
79 struct addrinfo hints =
80 {
81 .ai_family = AF_UNSPEC,
82 .ai_socktype = SOCK_STREAM,
83 .ai_protocol = IPPROTO_TCP,
84 };
85 errno = ERANGE;
86 h_errno = NETDB_INTERNAL;
87 struct addrinfo *ai;
88 int ret = getaddrinfo ("example.com", "80", &hints, &ai);
89 check_addrinfo (query_description: "example.com AF_UNSPEC", ai, ret,
90 expected: "address: STREAM/TCP 192.0.2.1 80\n");
91 if (ret == 0)
92 freeaddrinfo (ai);
93
94 hints.ai_family = AF_INET;
95 errno = ERANGE;
96 h_errno = NETDB_INTERNAL;
97 ret = getaddrinfo ("example.com", "80", &hints, &ai);
98 check_addrinfo (query_description: "example.com AF_INET", ai, ret,
99 expected: "address: STREAM/TCP 192.0.2.1 80\n");
100 if (ret == 0)
101 freeaddrinfo (ai);
102 }
103
104 support_chroot_free (chroot_env);
105 return 0;
106}
107
108#define PREPARE prepare
109#include <support/test-driver.c>
110

source code of glibc/nss/tst-nss-files-hosts-erange.c