* string/string.h (strndupa): Add cast for C++ conformance.
[glibc.git] / nss / test-netdb.c
blob837b078de62f80fe7b193742b3af6ccb50f61eaa
1 /* Copyright (C) 1998, 1999, 2000 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
21 Testing of some network related lookup functions.
22 The system databases looked up are:
23 - /etc/services
24 - /etc/hosts
25 - /etc/networks
26 - /etc/protocols
27 - /etc/rpc
28 The tests try to be fairly generic and simple so that they work on
29 every possible setup (and might therefore not detect some possible
30 errors).
33 #include <netdb.h>
34 #include <rpc/netdb.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <arpa/inet.h>
39 #include <netinet/in.h>
40 #include <sys/param.h>
41 #include <sys/socket.h>
42 #include <unistd.h>
43 #include "nss.h"
46 The following define is necessary for glibc 2.0.6
48 #ifndef INET6_ADDRSTRLEN
49 # define INET6_ADDRSTRLEN 46
50 #endif
52 int error_count;
54 static void
55 output_servent (const char *call, struct servent *sptr)
57 char **pptr;
59 if (sptr == NULL)
60 printf ("Call: %s returned NULL\n", call);
61 else
63 printf ("Call: %s, returned: s_name: %s, s_port: %d, s_proto: %s\n",
64 call, sptr->s_name, ntohs(sptr->s_port), sptr->s_proto);
65 for (pptr = sptr->s_aliases; *pptr != NULL; pptr++)
66 printf (" alias: %s\n", *pptr);
71 static void
72 test_services (void)
74 struct servent *sptr;
76 sptr = getservbyname ("domain", "tcp");
77 output_servent ("getservbyname (\"domain\", \"tcp\")", sptr);
79 sptr = getservbyname ("domain", "udp");
80 output_servent ("getservbyname (\"domain\", \"udp\")", sptr);
82 sptr = getservbyname ("domain", NULL);
83 output_servent ("getservbyname (\"domain\", NULL)", sptr);
85 sptr = getservbyname ("not-existant", NULL);
86 output_servent ("getservbyname (\"not-existant\", NULL)", sptr);
88 /* This shouldn't return anything. */
89 sptr = getservbyname ("", "");
90 output_servent ("getservbyname (\"\", \"\")", sptr);
92 sptr = getservbyname ("", "tcp");
93 output_servent ("getservbyname (\"\", \"tcp\")", sptr);
95 sptr = getservbyport (htons(53), "tcp");
96 output_servent ("getservbyport (htons(53), \"tcp\")", sptr);
98 sptr = getservbyport (htons(53), NULL);
99 output_servent ("getservbyport (htons(53), NULL)", sptr);
101 sptr = getservbyport (htons(1), "udp"); /* shouldn't exist */
102 output_servent ("getservbyport (htons(1), \"udp\")", sptr);
104 setservent (0);
107 sptr = getservent ();
108 output_servent ("getservent ()", sptr);
110 while (sptr != NULL);
111 endservent ();
115 static void
116 output_hostent (const char *call, struct hostent *hptr)
118 char **pptr;
119 char buf[INET6_ADDRSTRLEN];
121 if (hptr == NULL)
122 printf ("Call: %s returned NULL\n", call);
123 else
125 printf ("Call: %s returned: name: %s, addr_type: %d\n",
126 call, hptr->h_name, hptr->h_addrtype);
127 if (hptr->h_aliases)
128 for (pptr = hptr->h_aliases; *pptr != NULL; pptr++)
129 printf (" alias: %s\n", *pptr);
131 for (pptr = hptr->h_addr_list; *pptr != NULL; pptr++)
132 printf (" ip: %s\n",
133 inet_ntop (hptr->h_addrtype, *pptr, buf, sizeof (buf)));
137 static void
138 test_hosts (void)
140 struct hostent *hptr1, *hptr2;
141 char name[MAXHOSTNAMELEN];
142 size_t namelen = sizeof(name);
143 struct in_addr ip;
145 hptr1 = gethostbyname ("localhost");
146 hptr2 = gethostbyname ("LocalHost");
147 if (hptr1 != NULL || hptr2 != NULL)
149 if (hptr1 == NULL)
151 printf ("localhost not found - but LocalHost found:-(\n");
152 ++error_count;
154 else if (hptr2 == NULL)
156 printf ("LocalHost not found - but localhost found:-(\n");
157 ++error_count;
159 else if (strcmp (hptr1->h_name, hptr2->h_name) != 0)
161 printf ("localhost and LocalHost have different canoncial name\n");
162 printf ("gethostbyname (\"localhost\")->%s\n", hptr1->h_name);
163 printf ("gethostbyname (\"LocalHost\")->%s\n", hptr2->h_name);
164 ++error_count;
166 else
167 output_hostent ("gethostbyname(\"localhost\")", hptr1);
170 hptr1 = gethostbyname ("127.0.0.1");
171 output_hostent ("gethostbyname (\"127.0.0.1\")", hptr1);
173 hptr1 = gethostbyname ("10.1234");
174 output_hostent ("gethostbyname (\"10.1234\")", hptr1);
176 hptr1 = gethostbyname2 ("localhost", AF_INET);
177 output_hostent ("gethostbyname2 (\"localhost\", AF_INET)", hptr1);
179 if (gethostname (name, namelen) == 0)
181 printf ("Hostname: %s\n", name);
182 hptr1 = gethostbyname (name);
183 output_hostent ("gethostbyname (gethostname(...))", hptr1);
186 ip.s_addr = htonl (INADDR_LOOPBACK);
187 hptr1 = gethostbyaddr ((char *)&ip, sizeof(ip), AF_INET);
188 if (hptr1 != NULL)
190 printf ("official name of 127.0.0.1: %s\n", hptr1->h_name);
193 sethostent (0);
196 hptr1 = gethostent ();
197 output_hostent ("gethostent ()", hptr1);
199 while (hptr1 != NULL);
200 endhostent ();
205 static void
206 output_netent (const char *call, struct netent *nptr)
208 char **pptr;
210 if (nptr == NULL)
211 printf ("Call: %s returned NULL\n", call);
212 else
214 struct in_addr ip;
216 ip.s_addr = htonl(nptr->n_net);
217 printf ("Call: %s, returned: n_name: %s, network_number: %s\n",
218 call, nptr->n_name, inet_ntoa (ip));
220 for (pptr = nptr->n_aliases; *pptr != NULL; pptr++)
221 printf (" alias: %s\n", *pptr);
225 static void
226 test_network (void)
228 struct netent *nptr;
229 u_int32_t ip;
232 This test needs the following line in /etc/networks:
233 loopback 127.0.0.0
235 nptr = getnetbyname ("loopback");
236 output_netent ("getnetbyname (\"loopback\")",nptr);
238 nptr = getnetbyname ("LoopBACK");
239 output_netent ("getnetbyname (\"LoopBACK\")",nptr);
241 ip = inet_network ("127.0.0.0");
242 nptr = getnetbyaddr (ip, AF_INET);
243 output_netent ("getnetbyaddr (inet_network (\"127.0.0.0\"), AF_INET)",nptr);
245 setnetent (0);
248 nptr = getnetent ();
249 output_netent ("getnetent ()", nptr);
251 while (nptr != NULL);
252 endnetent ();
256 static void
257 output_protoent (const char *call, struct protoent *prptr)
259 char **pptr;
261 if (prptr == NULL)
262 printf ("Call: %s returned NULL\n", call);
263 else
265 printf ("Call: %s, returned: p_name: %s, p_proto: %d\n",
266 call, prptr->p_name, prptr->p_proto);
267 for (pptr = prptr->p_aliases; *pptr != NULL; pptr++)
268 printf (" alias: %s\n", *pptr);
273 static void
274 test_protocols (void)
276 struct protoent *prptr;
278 prptr = getprotobyname ("IP");
279 output_protoent ("getprotobyname (\"IP\")", prptr);
281 prptr = getprotobynumber (1);
282 output_protoent ("getprotobynumber (1)", prptr);
284 setprotoent (0);
287 prptr = getprotoent ();
288 output_protoent ("getprotoent ()", prptr);
290 while (prptr != NULL);
291 endprotoent ();
295 static void
296 output_rpcent (const char *call, struct rpcent *rptr)
298 char **pptr;
300 if (rptr == NULL)
301 printf ("Call: %s returned NULL\n", call);
302 else
304 printf ("Call: %s, returned: r_name: %s, r_number: %d\n",
305 call, rptr->r_name, rptr->r_number);
306 for (pptr = rptr->r_aliases; *pptr != NULL; pptr++)
307 printf (" alias: %s\n", *pptr);
311 static void
312 test_rpc (void)
314 struct rpcent *rptr;
316 rptr = getrpcbyname ("portmap");
317 output_rpcent ("getrpcyname (\"portmap\")", rptr);
319 rptr = getrpcbynumber (100000);
320 output_rpcent ("getrpcbynumber (100000)", rptr);
322 setrpcent (0);
325 rptr = getrpcent ();
326 output_rpcent ("getrpcent ()", rptr);
328 while (rptr != NULL);
329 endrpcent ();
332 /* Override /etc/nsswitch.conf for this program. This is mainly
333 useful for developers. */
334 static void __attribute__ ((unused))
335 setdb (const char *dbname)
337 if (strcmp ("db", dbname))
340 db is not implemented for hosts, networks
342 __nss_configure_lookup ("hosts", dbname);
343 __nss_configure_lookup ("networks", dbname);
345 __nss_configure_lookup ("protocols", dbname);
346 __nss_configure_lookup ("rpc", dbname);
347 __nss_configure_lookup ("services", dbname);
352 main (void)
355 setdb ("db");
358 test_hosts ();
359 test_network ();
360 test_protocols ();
361 test_rpc ();
362 test_services ();
364 if (error_count)
365 printf ("\n %d errors occurred!\n", error_count);
366 else
367 printf ("No visible errors occurred!\n");
369 return (error_count != 0);