1 | /* |
---|
2 | * getnameinfo test suite. |
---|
3 | * |
---|
4 | * Written by Russ Allbery <rra@stanford.edu> |
---|
5 | * Copyright 2009 Board of Trustees, Leland Stanford Jr. University |
---|
6 | * Copyright (c) 2004, 2005, 2006, 2007 |
---|
7 | * by Internet Systems Consortium, Inc. ("ISC") |
---|
8 | * Copyright (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
---|
9 | * 2002, 2003 by The Internet Software Consortium and Rich Salz |
---|
10 | * |
---|
11 | * See LICENSE for licensing terms. |
---|
12 | */ |
---|
13 | |
---|
14 | #include <config.h> |
---|
15 | #include <portable/system.h> |
---|
16 | #include <portable/socket.h> |
---|
17 | |
---|
18 | #include <tests/tap/basic.h> |
---|
19 | #include <util/util.h> |
---|
20 | |
---|
21 | int test_getnameinfo(const struct sockaddr *, socklen_t, char *, socklen_t, |
---|
22 | char *, socklen_t, int); |
---|
23 | |
---|
24 | /* Linux doesn't provide EAI_OVERFLOW, so make up our own for testing. */ |
---|
25 | #ifndef EAI_OVERFLOW |
---|
26 | # define EAI_OVERFLOW 10 |
---|
27 | #endif |
---|
28 | |
---|
29 | int |
---|
30 | main(void) |
---|
31 | { |
---|
32 | char node[256], service[256]; |
---|
33 | struct sockaddr_in sin; |
---|
34 | struct sockaddr *sa = (struct sockaddr *) &sin; |
---|
35 | int status; |
---|
36 | struct hostent *hp; |
---|
37 | struct servent *serv; |
---|
38 | char *name; |
---|
39 | |
---|
40 | plan(26); |
---|
41 | |
---|
42 | /* |
---|
43 | * Test the easy stuff that requires no assumptions. Hopefully everyone |
---|
44 | * has nntp, exec, and biff as services. exec and biff are special since |
---|
45 | * they're one of the rare universal pairs of services that are both on |
---|
46 | * the same port, but with different protocols. |
---|
47 | */ |
---|
48 | memset(&sin, 0, sizeof(sin)); |
---|
49 | sin.sin_family = AF_INET; |
---|
50 | sin.sin_port = htons(119); |
---|
51 | inet_aton("10.20.30.40", &sin.sin_addr); |
---|
52 | status = test_getnameinfo(sa, sizeof(sin), NULL, 0, NULL, 0, 0); |
---|
53 | is_int(EAI_NONAME, status, "EAI_NONAME from NULL"); |
---|
54 | status = test_getnameinfo(sa, sizeof(sin), node, 0, service, 0, 0); |
---|
55 | is_int(EAI_NONAME, status, "EAI_NONAME from length of 0"); |
---|
56 | status = test_getnameinfo(sa, sizeof(sin), NULL, 0, service, |
---|
57 | sizeof(service), 0); |
---|
58 | is_int(0, status, "lookup of port 119"); |
---|
59 | serv = getservbyname("nntp", "tcp"); |
---|
60 | if (serv == NULL) |
---|
61 | skip_block(5, "nntp service not found"); |
---|
62 | else { |
---|
63 | is_string("nntp", service, "...found nntp"); |
---|
64 | service[0] = '\0'; |
---|
65 | status = test_getnameinfo(sa, sizeof(sin), NULL, 0, service, 1, 0); |
---|
66 | is_int(EAI_OVERFLOW, status, "EAI_OVERFLOW with one character"); |
---|
67 | status = test_getnameinfo(sa, sizeof(sin), NULL, 0, service, 4, 0); |
---|
68 | is_int(EAI_OVERFLOW, status, "EAI_OVERFLOW with four characters"); |
---|
69 | status = test_getnameinfo(sa, sizeof(sin), NULL, 0, service, 5, 0); |
---|
70 | is_int(0, status, "fits in five characters"); |
---|
71 | is_string("nntp", service, "...and found nntp"); |
---|
72 | } |
---|
73 | status = test_getnameinfo(sa, sizeof(sin), NULL, 0, service, |
---|
74 | sizeof(service), NI_NUMERICSERV); |
---|
75 | is_int(0, status, "NI_NUMERICSERV"); |
---|
76 | is_string("119", service, "...and returns number"); |
---|
77 | sin.sin_port = htons(512); |
---|
78 | status = test_getnameinfo(sa, sizeof(sin), NULL, 0, service, |
---|
79 | sizeof(service), 0); |
---|
80 | is_int(0, status, "lookup of 512 TCP"); |
---|
81 | serv = getservbyname("exec", "tcp"); |
---|
82 | if (serv == NULL) |
---|
83 | skip("exec service not found"); |
---|
84 | else |
---|
85 | is_string("exec", service, "...and found exec"); |
---|
86 | status = test_getnameinfo(sa, sizeof(sin), NULL, 0, service, |
---|
87 | sizeof(service), NI_DGRAM); |
---|
88 | is_int(0, status, "lookup of 512 UDP"); |
---|
89 | serv = getservbyname("biff", "udp"); |
---|
90 | if (serv == NULL) |
---|
91 | skip("biff service not found"); |
---|
92 | else |
---|
93 | is_string("biff", service, "...and found biff"); |
---|
94 | status = test_getnameinfo(sa, sizeof(sin), node, sizeof(node), NULL, 0, |
---|
95 | NI_NUMERICHOST); |
---|
96 | is_int(0, status, "lookup with NI_NUMERICHOST"); |
---|
97 | is_string("10.20.30.40", node, "...and found correct IP address"); |
---|
98 | node[0] = '\0'; |
---|
99 | status = test_getnameinfo(sa, sizeof(sin), node, 1, NULL, 0, |
---|
100 | NI_NUMERICHOST); |
---|
101 | is_int(EAI_OVERFLOW, status, "EAI_OVERFLOW with one character"); |
---|
102 | status = test_getnameinfo(sa, sizeof(sin), node, 11, NULL, 0, |
---|
103 | NI_NUMERICHOST); |
---|
104 | is_int(EAI_OVERFLOW, status, "EAI_OVERFLOW with 11 characters"); |
---|
105 | status = test_getnameinfo(sa, sizeof(sin), node, 12, NULL, 0, |
---|
106 | NI_NUMERICHOST); |
---|
107 | is_int(0, status, "fits into 12 characters"); |
---|
108 | is_string("10.20.30.40", node, "...and found correct IP address"); |
---|
109 | |
---|
110 | /* |
---|
111 | * Okay, now it gets annoying. Do a forward and then reverse lookup of |
---|
112 | * some well-known host and make sure that getnameinfo returns the same |
---|
113 | * results. This may need to be skipped. |
---|
114 | */ |
---|
115 | hp = gethostbyname("www.isc.org"); |
---|
116 | if (hp == NULL) |
---|
117 | skip_block(2, "cannot look up www.isc.org"); |
---|
118 | else { |
---|
119 | memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr)); |
---|
120 | hp = gethostbyaddr((const void *) &sin.sin_addr, sizeof(sin.sin_addr), |
---|
121 | AF_INET); |
---|
122 | if (hp == NULL || strchr(hp->h_name, '.') == NULL) |
---|
123 | skip_block(2, "cannot reverse-lookup www.isc.org"); |
---|
124 | else { |
---|
125 | name = xstrdup(hp->h_name); |
---|
126 | status = test_getnameinfo(sa, sizeof(sin), node, sizeof(node), |
---|
127 | NULL, 0, 0); |
---|
128 | is_int(0, status, "lookup of www.isc.org IP address"); |
---|
129 | is_string(name, node, "...matches gethostbyaddr"); |
---|
130 | free(name); |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | /* Hope that no one is weird enough to put 0.0.0.0 into DNS. */ |
---|
135 | inet_aton("0.0.0.0", &sin.sin_addr); |
---|
136 | status = test_getnameinfo(sa, sizeof(sin), node, sizeof(node), NULL, 0, 0); |
---|
137 | is_int(0, status, "lookup of 0.0.0.0"); |
---|
138 | is_string("0.0.0.0", node, "...returns the IP address"); |
---|
139 | node[0] = '\0'; |
---|
140 | status = test_getnameinfo(sa, sizeof(sin), node, sizeof(node), NULL, 0, |
---|
141 | NI_NAMEREQD); |
---|
142 | is_int(EAI_NONAME, status, "...fails with NI_NAMEREQD"); |
---|
143 | |
---|
144 | sin.sin_family = AF_UNIX; |
---|
145 | status = test_getnameinfo(sa, sizeof(sin), node, sizeof(node), NULL, 0, 0); |
---|
146 | is_int(EAI_FAMILY, status, "EAI_FAMILY"); |
---|
147 | |
---|
148 | return 0; |
---|
149 | } |
---|