tst-leaks: raise timeout to 5 seconds
[glibc.git] / nss / test-netdb.c
blob70885493112877c19556dd0b6f7d51b5944d81e4
1 /* Copyright (C) 1998-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Andreas Jaeger <aj@suse.de>, 1998.
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.
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.
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 <http://www.gnu.org/licenses/>. */
20 Testing of some network related lookup functions.
21 The system databases looked up are:
22 - /etc/services
23 - /etc/hosts
24 - /etc/networks
25 - /etc/protocols
26 The tests try to be fairly generic and simple so that they work on
27 every possible setup (and might therefore not detect some possible
28 errors).
31 #include <netdb.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <arpa/inet.h>
36 #include <netinet/in.h>
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 #include <unistd.h>
40 #include <errno.h>
41 #include "nss.h"
44 The following define is necessary for glibc 2.0.6
46 #ifndef INET6_ADDRSTRLEN
47 # define INET6_ADDRSTRLEN 46
48 #endif
50 int error_count;
52 static void
53 output_servent (const char *call, struct servent *sptr)
55 char **pptr;
57 if (sptr == NULL)
58 printf ("Call: %s returned NULL\n", call);
59 else
61 printf ("Call: %s, returned: s_name: %s, s_port: %d, s_proto: %s\n",
62 call, sptr->s_name, ntohs(sptr->s_port), sptr->s_proto);
63 for (pptr = sptr->s_aliases; *pptr != NULL; pptr++)
64 printf (" alias: %s\n", *pptr);
69 static void
70 test_services (void)
72 struct servent *sptr;
74 sptr = getservbyname ("domain", "tcp");
75 output_servent ("getservbyname (\"domain\", \"tcp\")", sptr);
77 sptr = getservbyname ("domain", "udp");
78 output_servent ("getservbyname (\"domain\", \"udp\")", sptr);
80 sptr = getservbyname ("domain", NULL);
81 output_servent ("getservbyname (\"domain\", NULL)", sptr);
83 sptr = getservbyname ("not-existant", NULL);
84 output_servent ("getservbyname (\"not-existant\", NULL)", sptr);
86 /* This shouldn't return anything. */
87 sptr = getservbyname ("", "");
88 output_servent ("getservbyname (\"\", \"\")", sptr);
90 sptr = getservbyname ("", "tcp");
91 output_servent ("getservbyname (\"\", \"tcp\")", sptr);
93 sptr = getservbyport (htons(53), "tcp");
94 output_servent ("getservbyport (htons(53), \"tcp\")", sptr);
96 sptr = getservbyport (htons(53), NULL);
97 output_servent ("getservbyport (htons(53), NULL)", sptr);
99 sptr = getservbyport (htons(1), "udp"); /* shouldn't exist */
100 output_servent ("getservbyport (htons(1), \"udp\")", sptr);
102 setservent (0);
105 sptr = getservent ();
106 output_servent ("getservent ()", sptr);
108 while (sptr != NULL);
109 endservent ();
113 static void
114 output_hostent (const char *call, struct hostent *hptr)
116 char **pptr;
117 char buf[INET6_ADDRSTRLEN];
119 if (hptr == NULL)
120 printf ("Call: %s returned NULL\n", call);
121 else
123 printf ("Call: %s returned: name: %s, addr_type: %d\n",
124 call, hptr->h_name, hptr->h_addrtype);
125 if (hptr->h_aliases)
126 for (pptr = hptr->h_aliases; *pptr != NULL; pptr++)
127 printf (" alias: %s\n", *pptr);
129 for (pptr = hptr->h_addr_list; *pptr != NULL; pptr++)
130 printf (" ip: %s\n",
131 inet_ntop (hptr->h_addrtype, *pptr, buf, sizeof (buf)));
135 static void
136 test_hosts (void)
138 struct hostent *hptr1, *hptr2;
139 char *name = NULL;
140 size_t namelen = 0;
141 struct in_addr ip;
143 hptr1 = gethostbyname ("localhost");
144 hptr2 = gethostbyname ("LocalHost");
145 if (hptr1 != NULL || hptr2 != NULL)
147 if (hptr1 == NULL)
149 printf ("localhost not found - but LocalHost found:-(\n");
150 ++error_count;
152 else if (hptr2 == NULL)
154 printf ("LocalHost not found - but localhost found:-(\n");
155 ++error_count;
157 else if (strcmp (hptr1->h_name, hptr2->h_name) != 0)
159 printf ("localhost and LocalHost have different canoncial name\n");
160 printf ("gethostbyname (\"localhost\")->%s\n", hptr1->h_name);
161 printf ("gethostbyname (\"LocalHost\")->%s\n", hptr2->h_name);
162 ++error_count;
164 else
165 output_hostent ("gethostbyname(\"localhost\")", hptr1);
168 hptr1 = gethostbyname ("127.0.0.1");
169 output_hostent ("gethostbyname (\"127.0.0.1\")", hptr1);
171 hptr1 = gethostbyname ("10.1234");
172 output_hostent ("gethostbyname (\"10.1234\")", hptr1);
174 hptr1 = gethostbyname2 ("localhost", AF_INET);
175 output_hostent ("gethostbyname2 (\"localhost\", AF_INET)", hptr1);
177 while (gethostname (name, namelen) < 0 && errno == ENAMETOOLONG)
179 namelen += 2; /* tiny increments to test a lot */
180 name = realloc (name, namelen);
182 if (gethostname (name, namelen) == 0)
184 printf ("Hostname: %s\n", name);
185 if (name != NULL)
187 hptr1 = gethostbyname (name);
188 output_hostent ("gethostbyname (gethostname(...))", hptr1);
192 ip.s_addr = htonl (INADDR_LOOPBACK);
193 hptr1 = gethostbyaddr ((char *) &ip, sizeof(ip), AF_INET);
194 if (hptr1 != NULL)
196 printf ("official name of 127.0.0.1: %s\n", hptr1->h_name);
199 sethostent (0);
202 hptr1 = gethostent ();
203 output_hostent ("gethostent ()", hptr1);
205 while (hptr1 != NULL);
206 endhostent ();
211 static void
212 output_netent (const char *call, struct netent *nptr)
214 char **pptr;
216 if (nptr == NULL)
217 printf ("Call: %s returned NULL\n", call);
218 else
220 struct in_addr ip;
222 ip.s_addr = htonl(nptr->n_net);
223 printf ("Call: %s, returned: n_name: %s, network_number: %s\n",
224 call, nptr->n_name, inet_ntoa (ip));
226 for (pptr = nptr->n_aliases; *pptr != NULL; pptr++)
227 printf (" alias: %s\n", *pptr);
231 static void
232 test_network (void)
234 struct netent *nptr;
235 u_int32_t ip;
238 This test needs the following line in /etc/networks:
239 loopback 127.0.0.0
241 nptr = getnetbyname ("loopback");
242 output_netent ("getnetbyname (\"loopback\")",nptr);
244 nptr = getnetbyname ("LoopBACK");
245 output_netent ("getnetbyname (\"LoopBACK\")",nptr);
247 ip = inet_network ("127.0.0.0");
248 nptr = getnetbyaddr (ip, AF_INET);
249 output_netent ("getnetbyaddr (inet_network (\"127.0.0.0\"), AF_INET)",nptr);
251 setnetent (0);
254 nptr = getnetent ();
255 output_netent ("getnetent ()", nptr);
257 while (nptr != NULL);
258 endnetent ();
262 static void
263 output_protoent (const char *call, struct protoent *prptr)
265 char **pptr;
267 if (prptr == NULL)
268 printf ("Call: %s returned NULL\n", call);
269 else
271 printf ("Call: %s, returned: p_name: %s, p_proto: %d\n",
272 call, prptr->p_name, prptr->p_proto);
273 for (pptr = prptr->p_aliases; *pptr != NULL; pptr++)
274 printf (" alias: %s\n", *pptr);
279 static void
280 test_protocols (void)
282 struct protoent *prptr;
284 prptr = getprotobyname ("IP");
285 output_protoent ("getprotobyname (\"IP\")", prptr);
287 prptr = getprotobynumber (1);
288 output_protoent ("getprotobynumber (1)", prptr);
290 setprotoent (0);
293 prptr = getprotoent ();
294 output_protoent ("getprotoent ()", prptr);
296 while (prptr != NULL);
297 endprotoent ();
301 /* Override /etc/nsswitch.conf for this program. This is mainly
302 useful for developers. */
303 static void __attribute__ ((unused))
304 setdb (const char *dbname)
306 if (strcmp ("db", dbname))
309 db is not implemented for hosts, networks
311 __nss_configure_lookup ("hosts", dbname);
312 __nss_configure_lookup ("networks", dbname);
314 __nss_configure_lookup ("protocols", dbname);
315 __nss_configure_lookup ("services", dbname);
319 static int
320 do_test (void)
323 setdb ("db");
326 test_hosts ();
327 test_network ();
328 test_protocols ();
329 test_services ();
331 if (error_count)
332 printf ("\n %d errors occurred!\n", error_count);
333 else
334 printf ("No visible errors occurred!\n");
336 return (error_count != 0);
339 #define TEST_FUNCTION do_test ()
340 #include "../test-skeleton.c"