source: branches/libc-0.6/src/libctests/glibc/nss/test-netdb.c

Last change on this file was 2137, checked in by bird, 20 years ago

More libc.

  • Property cvs2svn:cvs-rev set to 1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 9.1 KB
Line 
1/* Copyright (C) 1998,99,2000,01 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Andreas Jaeger <aj@suse.de>, 1998.
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20/*
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).
31*/
32
33#include <netdb.h>
34#ifdef HAVE_RPC_NETDB_H
35#include <rpc/netdb.h>
36#endif
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <arpa/inet.h>
41#include <netinet/in.h>
42#include <sys/param.h>
43#include <sys/socket.h>
44#include <unistd.h>
45#include <errno.h>
46#include "nss.h"
47
48/*
49 The following define is necessary for glibc 2.0.6
50*/
51#ifndef INET6_ADDRSTRLEN
52# define INET6_ADDRSTRLEN 46
53#endif
54
55int error_count;
56
57static void
58output_servent (const char *call, struct servent *sptr)
59{
60 char **pptr;
61
62 if (sptr == NULL)
63 printf ("Call: %s returned NULL\n", call);
64 else
65 {
66 printf ("Call: %s, returned: s_name: %s, s_port: %d, s_proto: %s\n",
67 call, sptr->s_name, ntohs(sptr->s_port), sptr->s_proto);
68 for (pptr = sptr->s_aliases; *pptr != NULL; pptr++)
69 printf (" alias: %s\n", *pptr);
70 }
71}
72
73
74static void
75test_services (void)
76{
77 struct servent *sptr;
78
79 sptr = getservbyname ("domain", "tcp");
80 output_servent ("getservbyname (\"domain\", \"tcp\")", sptr);
81
82 sptr = getservbyname ("domain", "udp");
83 output_servent ("getservbyname (\"domain\", \"udp\")", sptr);
84
85 sptr = getservbyname ("domain", NULL);
86 output_servent ("getservbyname (\"domain\", NULL)", sptr);
87
88 sptr = getservbyname ("not-existant", NULL);
89 output_servent ("getservbyname (\"not-existant\", NULL)", sptr);
90
91 /* This shouldn't return anything. */
92 sptr = getservbyname ("", "");
93 output_servent ("getservbyname (\"\", \"\")", sptr);
94
95 sptr = getservbyname ("", "tcp");
96 output_servent ("getservbyname (\"\", \"tcp\")", sptr);
97
98 sptr = getservbyport (htons(53), "tcp");
99 output_servent ("getservbyport (htons(53), \"tcp\")", sptr);
100
101 sptr = getservbyport (htons(53), NULL);
102 output_servent ("getservbyport (htons(53), NULL)", sptr);
103
104 sptr = getservbyport (htons(1), "udp"); /* shouldn't exist */
105 output_servent ("getservbyport (htons(1), \"udp\")", sptr);
106
107 setservent (0);
108 do
109 {
110 sptr = getservent ();
111 output_servent ("getservent ()", sptr);
112 }
113 while (sptr != NULL);
114 endservent ();
115}
116
117
118static void
119output_hostent (const char *call, struct hostent *hptr)
120{
121 char **pptr;
122 char buf[INET6_ADDRSTRLEN];
123
124 if (hptr == NULL)
125 printf ("Call: %s returned NULL\n", call);
126 else
127 {
128 printf ("Call: %s returned: name: %s, addr_type: %d\n",
129 call, hptr->h_name, hptr->h_addrtype);
130 if (hptr->h_aliases)
131 for (pptr = hptr->h_aliases; *pptr != NULL; pptr++)
132 printf (" alias: %s\n", *pptr);
133
134 for (pptr = hptr->h_addr_list; *pptr != NULL; pptr++)
135 printf (" ip: %s\n",
136 inet_ntop (hptr->h_addrtype, *pptr, buf, sizeof (buf)));
137 }
138}
139
140static void
141test_hosts (void)
142{
143 struct hostent *hptr1, *hptr2;
144 char *name = NULL;
145 size_t namelen = 0;
146 struct in_addr ip;
147
148 hptr1 = gethostbyname ("localhost");
149 hptr2 = gethostbyname ("LocalHost");
150 if (hptr1 != NULL || hptr2 != NULL)
151 {
152 if (hptr1 == NULL)
153 {
154 printf ("localhost not found - but LocalHost found:-(\n");
155 ++error_count;
156 }
157 else if (hptr2 == NULL)
158 {
159 printf ("LocalHost not found - but localhost found:-(\n");
160 ++error_count;
161 }
162 else if (strcmp (hptr1->h_name, hptr2->h_name) != 0)
163 {
164 printf ("localhost and LocalHost have different canoncial name\n");
165 printf ("gethostbyname (\"localhost\")->%s\n", hptr1->h_name);
166 printf ("gethostbyname (\"LocalHost\")->%s\n", hptr2->h_name);
167 ++error_count;
168 }
169 else
170 output_hostent ("gethostbyname(\"localhost\")", hptr1);
171 }
172
173 hptr1 = gethostbyname ("127.0.0.1");
174 output_hostent ("gethostbyname (\"127.0.0.1\")", hptr1);
175
176 hptr1 = gethostbyname ("10.1234");
177 output_hostent ("gethostbyname (\"10.1234\")", hptr1);
178
179 hptr1 = gethostbyname2 ("localhost", AF_INET);
180 output_hostent ("gethostbyname2 (\"localhost\", AF_INET)", hptr1);
181
182 while (gethostname (name, namelen) < 0 && errno == ENAMETOOLONG)
183 {
184 namelen += 2; /* tiny increments to test a lot */
185 name = realloc (name, namelen);
186 }
187 if (gethostname (name, namelen) == 0)
188 {
189 printf ("Hostname: %s\n", name);
190 if (name != NULL)
191 {
192 hptr1 = gethostbyname (name);
193 output_hostent ("gethostbyname (gethostname(...))", hptr1);
194 }
195 }
196
197 ip.s_addr = htonl (INADDR_LOOPBACK);
198 hptr1 = gethostbyaddr ((char *) &ip, sizeof(ip), AF_INET);
199 if (hptr1 != NULL)
200 {
201 printf ("official name of 127.0.0.1: %s\n", hptr1->h_name);
202 }
203
204 sethostent (0);
205 do
206 {
207 hptr1 = gethostent ();
208 output_hostent ("gethostent ()", hptr1);
209 }
210 while (hptr1 != NULL);
211 endhostent ();
212
213}
214
215
216static void
217output_netent (const char *call, struct netent *nptr)
218{
219 char **pptr;
220
221 if (nptr == NULL)
222 printf ("Call: %s returned NULL\n", call);
223 else
224 {
225 struct in_addr ip;
226
227 ip.s_addr = htonl(nptr->n_net);
228 printf ("Call: %s, returned: n_name: %s, network_number: %s\n",
229 call, nptr->n_name, inet_ntoa (ip));
230
231 for (pptr = nptr->n_aliases; *pptr != NULL; pptr++)
232 printf (" alias: %s\n", *pptr);
233 }
234}
235
236static void
237test_network (void)
238{
239 struct netent *nptr;
240 u_int32_t ip;
241
242 /*
243 This test needs the following line in /etc/networks:
244 loopback 127.0.0.0
245 */
246 nptr = getnetbyname ("loopback");
247 output_netent ("getnetbyname (\"loopback\")",nptr);
248
249 nptr = getnetbyname ("LoopBACK");
250 output_netent ("getnetbyname (\"LoopBACK\")",nptr);
251
252 ip = inet_network ("127.0.0.0");
253 nptr = getnetbyaddr (ip, AF_INET);
254 output_netent ("getnetbyaddr (inet_network (\"127.0.0.0\"), AF_INET)",nptr);
255
256 setnetent (0);
257 do
258 {
259 nptr = getnetent ();
260 output_netent ("getnetent ()", nptr);
261 }
262 while (nptr != NULL);
263 endnetent ();
264}
265
266
267static void
268output_protoent (const char *call, struct protoent *prptr)
269{
270 char **pptr;
271
272 if (prptr == NULL)
273 printf ("Call: %s returned NULL\n", call);
274 else
275 {
276 printf ("Call: %s, returned: p_name: %s, p_proto: %d\n",
277 call, prptr->p_name, prptr->p_proto);
278 for (pptr = prptr->p_aliases; *pptr != NULL; pptr++)
279 printf (" alias: %s\n", *pptr);
280 }
281}
282
283
284static void
285test_protocols (void)
286{
287 struct protoent *prptr;
288
289 prptr = getprotobyname ("IP");
290 output_protoent ("getprotobyname (\"IP\")", prptr);
291
292 prptr = getprotobynumber (1);
293 output_protoent ("getprotobynumber (1)", prptr);
294
295 setprotoent (0);
296 do
297 {
298 prptr = getprotoent ();
299 output_protoent ("getprotoent ()", prptr);
300 }
301 while (prptr != NULL);
302 endprotoent ();
303}
304
305
306#ifdef HAVE_RPC_NETDB_H
307static void
308output_rpcent (const char *call, struct rpcent *rptr)
309{
310 char **pptr;
311
312 if (rptr == NULL)
313 printf ("Call: %s returned NULL\n", call);
314 else
315 {
316 printf ("Call: %s, returned: r_name: %s, r_number: %d\n",
317 call, rptr->r_name, rptr->r_number);
318 for (pptr = rptr->r_aliases; *pptr != NULL; pptr++)
319 printf (" alias: %s\n", *pptr);
320 }
321}
322
323static void
324test_rpc (void)
325{
326 struct rpcent *rptr;
327
328 rptr = getrpcbyname ("portmap");
329 output_rpcent ("getrpcyname (\"portmap\")", rptr);
330
331 rptr = getrpcbynumber (100000);
332 output_rpcent ("getrpcbynumber (100000)", rptr);
333
334 setrpcent (0);
335 do
336 {
337 rptr = getrpcent ();
338 output_rpcent ("getrpcent ()", rptr);
339 }
340 while (rptr != NULL);
341 endrpcent ();
342}
343#endif
344
345#if 0 /* not very useful when you don't have this function... */
346/* Override /etc/nsswitch.conf for this program. This is mainly
347 useful for developers. */
348static void __attribute__ ((unused))
349setdb (const char *dbname)
350{
351 if (strcmp ("db", dbname))
352 {
353 /*
354 db is not implemented for hosts, networks
355 */
356 __nss_configure_lookup ("hosts", dbname);
357 __nss_configure_lookup ("networks", dbname);
358 }
359 __nss_configure_lookup ("protocols", dbname);
360 __nss_configure_lookup ("rpc", dbname);
361 __nss_configure_lookup ("services", dbname);
362}
363#endif
364
365int
366main (void)
367{
368 /*
369 setdb ("db");
370 */
371
372 test_hosts ();
373 test_network ();
374 test_protocols ();
375#ifdef HAVE_RPC_NETDB_H
376 test_rpc ();
377#endif
378 test_services ();
379
380 if (error_count)
381 printf ("\n %d errors occurred!\n", error_count);
382 else
383 printf ("No visible errors occurred!\n");
384
385 return (error_count != 0);
386}
Note: See TracBrowser for help on using the repository browser.