2 * Copyright (c) 2006 Michael Bushkov <bushman@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <arpa/inet.h>
32 #include <sys/socket.h>
33 #include <sys/types.h>
34 #include <netinet/in.h>
42 #include <stringlist.h>
47 #define min(a,b) (((a)<(b))?(a):(b))
53 TEST_GETHOSTBYNAME2_GETADDRINFO,
54 TEST_GETHOSTBYADDR_GETNAMEINFO,
56 TEST_BUILD_ADDR_SNAPSHOT
59 static int use_ipnode_functions = 0;
60 static int use_ipv6_mapping = 0;
61 static int ipnode_flags = 0;
63 static int af_type = AF_INET;
64 static enum test_methods method = TEST_BUILD_SNAPSHOT;
66 DECLARE_TEST_DATA(hostent)
67 DECLARE_TEST_FILE_SNAPSHOT(hostent)
68 DECLARE_1PASS_TEST(hostent)
69 DECLARE_2PASS_TEST(hostent)
71 /* These stubs will use gethostby***() or getipnodeby***() functions,
72 * depending on the use_ipnode_functions global variable value */
73 static struct hostent *__gethostbyname2(const char *, int);
74 static struct hostent *__gethostbyaddr(const void *, socklen_t, int);
75 static void __freehostent(struct hostent *);
77 static void clone_hostent(struct hostent *, struct hostent const *);
78 static int compare_hostent(struct hostent *, struct hostent *, void *);
79 static void dump_hostent(struct hostent *);
80 static void free_hostent(struct hostent *);
82 static int is_hostent_equal(struct hostent *, struct addrinfo *);
84 static void sdump_hostent(struct hostent *, char *, size_t);
85 static int hostent_read_hostlist_func(struct hostent *, char *);
86 static int hostent_read_snapshot_addr(char *, unsigned char *, size_t);
87 static int hostent_read_snapshot_func(struct hostent *, char *);
89 static int hostent_test_correctness(struct hostent *, void *);
90 static int hostent_test_gethostbyaddr(struct hostent *, void *);
91 static int hostent_test_getaddrinfo_eq(struct hostent *, void *);
92 static int hostent_test_getnameinfo_eq(struct hostent *, void *);
94 static void usage(void) __attribute__((__noreturn__));
96 IMPLEMENT_TEST_DATA(hostent)
97 IMPLEMENT_TEST_FILE_SNAPSHOT(hostent)
98 IMPLEMENT_1PASS_TEST(hostent)
99 IMPLEMENT_2PASS_TEST(hostent)
101 static struct hostent *
102 __gethostbyname2(const char *name, int af)
107 if (use_ipnode_functions == 0)
108 he = gethostbyname2(name, af);
111 he = getipnodebyname(name, af, ipnode_flags, &error);
119 static struct hostent *
120 __gethostbyaddr(const void *addr, socklen_t len, int af)
125 if (use_ipnode_functions == 0)
126 he = gethostbyaddr(addr, len, af);
129 he = getipnodebyaddr(addr, len, af, &error);
138 __freehostent(struct hostent *he)
140 /* NOTE: checking for he != NULL - just in case */
141 if ((use_ipnode_functions != 0) && (he != NULL))
146 clone_hostent(struct hostent *dest, struct hostent const *src)
148 assert(dest != NULL);
156 memset(dest, 0, sizeof(struct hostent));
158 if (src->h_name != NULL) {
159 dest->h_name = strdup(src->h_name);
160 assert(dest->h_name != NULL);
163 dest->h_addrtype = src->h_addrtype;
164 dest->h_length = src->h_length;
166 if (src->h_aliases != NULL) {
168 for (cp = src->h_aliases; *cp; ++cp)
171 dest->h_aliases = (char **)malloc((aliases_num + 1) *
173 assert(dest->h_aliases != NULL);
174 memset(dest->h_aliases, 0, (aliases_num + 1) *
177 for (cp = src->h_aliases; *cp; ++cp) {
178 dest->h_aliases[cp - src->h_aliases] = strdup(*cp);
179 assert(dest->h_aliases[cp - src->h_aliases] != NULL);
183 if (src->h_addr_list != NULL) {
185 for (cp = src->h_addr_list; *cp; ++cp)
188 dest->h_addr_list = (char **)malloc((addrs_num + 1) *
190 assert(dest->h_addr_list != NULL);
191 memset(dest->h_addr_list, 0, (addrs_num + 1) *
194 for (cp = src->h_addr_list; *cp; ++cp) {
195 offset = cp - src->h_addr_list;
196 dest->h_addr_list[offset] =
197 (char *)malloc(src->h_length);
198 assert(dest->h_addr_list[offset] != NULL);
199 memcpy(dest->h_addr_list[offset],
200 src->h_addr_list[offset], src->h_length);
206 free_hostent(struct hostent *ht)
214 if (ht->h_aliases != NULL) {
215 for (cp = ht->h_aliases; *cp; ++cp)
220 if (ht->h_addr_list != NULL) {
221 for (cp = ht->h_addr_list; *cp; ++cp)
223 free(ht->h_addr_list);
228 compare_hostent(struct hostent *ht1, struct hostent *ht2, void *mdata)
230 char **c1, **c2, **ct, **cb;
236 if ((ht1 == NULL) || (ht2 == NULL))
239 if ((ht1->h_name == NULL) || (ht2->h_name == NULL))
242 if ((ht1->h_addrtype != ht2->h_addrtype) ||
243 (ht1->h_length != ht2->h_length) ||
244 (strcmp(ht1->h_name, ht2->h_name) != 0))
250 if (((ht1->h_aliases == NULL) || (ht2->h_aliases == NULL)) &&
251 (ht1->h_aliases != ht2->h_aliases))
254 if ((c1 != NULL) && (c2 != NULL)) {
258 for (ct = c2; *ct; ++ct) {
259 if (strcmp(*c1, *ct) == 0) {
266 printf("h1 aliases item can't be "\
267 "found in h2 aliases\n");
275 for (ct = c1; *ct; ++ct) {
276 if (strcmp(*c2, *ct) == 0) {
283 printf("h2 aliases item can't be "\
284 " found in h1 aliases\n");
290 c1 = ht1->h_addr_list;
291 c2 = ht2->h_addr_list;
293 if (((ht1->h_addr_list == NULL) || (ht2->h_addr_list== NULL)) &&
294 (ht1->h_addr_list != ht2->h_addr_list))
297 if ((c1 != NULL) && (c2 != NULL)) {
301 for (ct = c2; *ct; ++ct) {
302 if (memcmp(*c1, *ct, ht1->h_length) == 0) {
309 printf("h1 addresses item can't be "\
310 "found in h2 addresses\n");
318 for (ct = c1; *ct; ++ct) {
319 if (memcmp(*c2, *ct, ht1->h_length) == 0) {
326 printf("h2 addresses item can't be "\
327 "found in h1 addresses\n");
336 if ((debug) && (mdata == NULL)) {
337 printf("following structures are not equal:\n");
346 check_addrinfo_for_name(struct addrinfo *ai, char const *name)
348 struct addrinfo *ai2;
350 for (ai2 = ai; ai2 != NULL; ai2 = ai2->ai_next) {
351 if (strcmp(ai2->ai_canonname, name) == 0)
359 check_addrinfo_for_addr(struct addrinfo *ai, char const *addr,
360 socklen_t addrlen, int af)
362 struct addrinfo *ai2;
364 for (ai2 = ai; ai2 != NULL; ai2 = ai2->ai_next) {
365 if (af != ai2->ai_family)
371 (void *)&((struct sockaddr_in *)ai2->ai_addr)->sin_addr,
372 min(addrlen, ai2->ai_addrlen)) == 0)
377 (void *)&((struct sockaddr_in6 *)ai2->ai_addr)->sin6_addr,
378 min(addrlen, ai2->ai_addrlen)) == 0)
390 is_hostent_equal(struct hostent *he, struct addrinfo *ai)
396 printf("checking equality of he and ai\n");
398 rv = check_addrinfo_for_name(ai, he->h_name);
401 printf("not equal - he->h_name couldn't be found\n");
406 for (cp = he->h_addr_list; *cp; ++cp) {
407 rv = check_addrinfo_for_addr(ai, *cp, he->h_length,
411 printf("not equal - one of he->h_addr_list couldn't be found\n");
424 sdump_hostent(struct hostent *ht, char *buffer, size_t buflen)
430 written = snprintf(buffer, buflen, "%s %d %d",
431 ht->h_name, ht->h_addrtype, ht->h_length);
433 if (written > buflen)
437 if (ht->h_aliases != NULL) {
438 if (*(ht->h_aliases) != NULL) {
439 for (cp = ht->h_aliases; *cp; ++cp) {
440 written = snprintf(buffer, buflen, " %s",*cp);
442 if (written > buflen)
450 written = snprintf(buffer, buflen, " noaliases");
452 if (written > buflen)
457 written = snprintf(buffer, buflen, " (null)");
459 if (written > buflen)
464 written = snprintf(buffer, buflen, " : ");
466 if (written > buflen)
470 if (ht->h_addr_list != NULL) {
471 if (*(ht->h_addr_list) != NULL) {
472 for (cp = ht->h_addr_list; *cp; ++cp) {
473 for (i = 0; i < ht->h_length; ++i ) {
474 written = snprintf(buffer, buflen,
475 i + 1 != ht->h_length ? "%d." : "%d",
476 (unsigned char)(*cp)[i]);
478 if (written > buflen)
487 written = snprintf(buffer, buflen, " ");
489 if (written > buflen)
495 written = snprintf(buffer, buflen, " noaddrs");
497 if (written > buflen)
502 written = snprintf(buffer, buflen, " (null)");
504 if (written > buflen)
511 hostent_read_hostlist_func(struct hostent *he, char *line)
513 struct hostent *result;
517 printf("resolving %s: ", line);
518 result = __gethostbyname2(line, af_type);
519 if (result != NULL) {
523 rv = hostent_test_correctness(result, NULL);
525 __freehostent(result);
529 clone_hostent(he, result);
530 __freehostent(result);
533 printf("not found\n");
535 memset(he, 0, sizeof(struct hostent));
536 he->h_name = strdup(line);
537 assert(he->h_name != NULL);
543 hostent_read_snapshot_addr(char *addr, unsigned char *result, size_t len)
548 while ( (s = strsep(&ps, ".")) != NULL) {
552 *result = (unsigned char)strtol(s, &ts, 10);
566 hostent_read_snapshot_func(struct hostent *ht, char *line)
568 StringList *sl1, *sl2;
573 printf("1 line read from snapshot:\n%s\n", line);
579 memset(ht, 0, sizeof(struct hostent));
580 while ( (s = strsep(&ps, " ")) != NULL) {
583 ht->h_name = strdup(s);
584 assert(ht->h_name != NULL);
588 ht->h_addrtype = (int)strtol(s, &ts, 10);
594 ht->h_length = (int)strtol(s, &ts, 10);
601 if (strcmp(s, "(null)") == 0)
607 if (strcmp(s, "noaliases") != 0) {
613 if (strcmp(s, ":") == 0)
625 if (strcmp(s, "(null)") == 0)
631 if (strcmp(s, "noaddrs") != 0) {
632 ts = (char *)malloc(ht->h_length);
634 memset(ts, 0, ht->h_length);
635 rv = hostent_read_snapshot_addr(s,\
636 (unsigned char *)ts, ht->h_length);
642 ts = (char *)malloc(ht->h_length);
644 memset(ts, 0, ht->h_length);
645 rv = hostent_read_snapshot_addr(s,\
646 (unsigned char *)ts, ht->h_length);
656 if ((i != 3) && (i != 4))
663 ht->h_aliases = sl1->sl_str;
667 ht->h_addr_list = sl2->sl_str;
670 if ((i != 4) || (rv != 0)) {
672 memset(ht, 0, sizeof(struct hostent));
676 /* NOTE: is it a dirty hack or not? */
683 dump_hostent(struct hostent *result)
685 if (result != NULL) {
687 sdump_hostent(result, buffer, sizeof(buffer));
688 printf("%s\n", buffer);
694 hostent_test_correctness(struct hostent *ht, void *mdata)
697 printf("testing correctness with the following data:\n");
704 if (ht->h_name == NULL)
707 if (!((ht->h_addrtype >= 0) && (ht->h_addrtype < AF_MAX)))
710 if ((ht->h_length != sizeof(struct in_addr)) &&
711 (ht->h_length != sizeof(struct in6_addr)))
714 if (ht->h_aliases == NULL)
717 if (ht->h_addr_list == NULL)
726 printf("incorrect\n");
732 hostent_test_gethostbyaddr(struct hostent *he, void *mdata)
734 struct hostent *result;
735 struct hostent_test_data *addr_test_data;
738 addr_test_data = (struct hostent_test_data *)mdata;
740 /* We should omit unresolved hostents */
741 if (he->h_addr_list != NULL) {
743 for (cp = he->h_addr_list; *cp; ++cp) {
745 printf("doing reverse lookup for %s\n", he->h_name);
747 result = __gethostbyaddr(*cp, he->h_length,
749 if (result == NULL) {
751 printf("warning: reverse lookup failed\n");
755 rv = hostent_test_correctness(result, NULL);
757 __freehostent(result);
761 if (addr_test_data != NULL)
762 TEST_DATA_APPEND(hostent, addr_test_data, result);
764 __freehostent(result);
772 hostent_test_getaddrinfo_eq(struct hostent *he, void *mdata)
774 struct addrinfo *ai, hints;
778 memset(&hints, 0, sizeof(struct addrinfo));
779 hints.ai_family = af_type;
780 hints.ai_flags = AI_CANONNAME;
783 printf("using getaddrinfo() to resolve %s\n", he->h_name);
785 /* struct hostent *he was not resolved */
786 if (he->h_addr_list == NULL) {
787 /* We can be sure that he->h_name is not NULL */
788 rv = getaddrinfo(he->h_name, NULL, &hints, &ai);
791 printf("not ok - shouldn't have been resolved\n");
795 rv = getaddrinfo(he->h_name, NULL, &hints, &ai);
798 printf("not ok - should have beed resolved\n");
802 rv = is_hostent_equal(he, ai);
805 printf("not ok - addrinfo and hostent are not equal\n");
815 hostent_test_getnameinfo_eq(struct hostent *he, void *mdata)
817 char buffer[NI_MAXHOST];
818 struct sockaddr_in sin;
819 struct sockaddr_in6 sin6;
820 struct sockaddr *saddr;
821 struct hostent *result;
824 if (he->h_addr_list != NULL) {
826 for (cp = he->h_addr_list; *cp; ++cp) {
828 printf("doing reverse lookup for %s\n", he->h_name);
830 result = __gethostbyaddr(*cp, he->h_length,
832 if (result != NULL) {
833 rv = hostent_test_correctness(result, NULL);
835 __freehostent(result);
840 printf("reverse lookup failed\n");
843 switch (he->h_addrtype) {
845 memset(&sin, 0, sizeof(struct sockaddr_in));
846 sin.sin_len = sizeof(struct sockaddr_in);
847 sin.sin_family = AF_INET;
848 memcpy(&sin.sin_addr, *cp, he->h_length);
850 saddr = (struct sockaddr *)&sin;
853 memset(&sin6, 0, sizeof(struct sockaddr_in6));
854 sin6.sin6_len = sizeof(struct sockaddr_in6);
855 sin6.sin6_family = AF_INET6;
856 memcpy(&sin6.sin6_addr, *cp, he->h_length);
858 saddr = (struct sockaddr *)&sin6;
862 printf("warning: %d family is unsupported\n",
867 assert(saddr != NULL);
868 rv = getnameinfo(saddr, saddr->sa_len, buffer,
869 sizeof(buffer), NULL, 0, NI_NAMEREQD);
871 if ((rv != 0) && (result != NULL)) {
873 printf("not ok - getnameinfo() didn't make the reverse lookup, when it should have (%s)\n",
878 if ((rv == 0) && (result == NULL)) {
880 printf("not ok - getnameinfo() made the reverse lookup, when it shouldn't have\n");
884 if ((rv != 0) && (result == NULL)) {
886 printf("ok - both getnameinfo() and ***byaddr() failed\n");
892 printf("comparing %s with %s\n", result->h_name,
895 rv = strcmp(result->h_name, buffer);
896 __freehostent(result);
900 printf("not ok - getnameinfo() and ***byaddr() results are not equal\n");
904 printf("ok - getnameinfo() and ***byaddr() results are equal\n");
915 (void)fprintf(stderr,
916 "Usage: %s -na2i [-o] [-d] [-46] [-mAcM] [-C] [-s <file>] -f <file>\n",
922 main(int argc, char **argv)
924 struct hostent_test_data td, td_addr, td_snap;
925 char *snapshot_file, *hostlist_file;
933 snapshot_file = NULL;
934 hostlist_file = NULL;
935 while ((c = getopt(argc, argv, "nad2iod46mAcMs:f:")) != -1)
945 use_ipv6_mapping = 1;
946 ipnode_flags |= AI_V4MAPPED_CFG;
950 use_ipv6_mapping = 1;
951 ipnode_flags |= AI_V4MAPPED;
954 ipnode_flags |= AI_ADDRCONFIG;
957 ipnode_flags |= AI_ALL;
960 use_ipnode_functions = 1;
966 method = TEST_GETHOSTBYNAME2;
969 method = TEST_GETHOSTBYADDR;
972 method = TEST_GETHOSTBYNAME2_GETADDRINFO;
975 method = TEST_GETHOSTBYADDR_GETNAMEINFO;
978 snapshot_file = strdup(optarg);
981 hostlist_file = strdup(optarg);
987 if (use_ipnode_functions == 0) {
988 statp = __res_state();
989 if ((statp == NULL) || ((statp->options & RES_INIT) == 0 &&
990 res_ninit(statp) == -1)) {
992 printf("error: can't init res_state\n");
999 if (use_ipv6_mapping == 0)
1000 statp->options &= ~RES_USE_INET6;
1002 statp->options |= RES_USE_INET6;
1005 TEST_DATA_INIT(hostent, &td, clone_hostent, free_hostent);
1006 TEST_DATA_INIT(hostent, &td_addr, clone_hostent, free_hostent);
1007 TEST_DATA_INIT(hostent, &td_snap, clone_hostent, free_hostent);
1009 if (hostlist_file == NULL)
1012 if (access(hostlist_file, R_OK) != 0) {
1014 printf("can't access the hostlist file %s\n",
1021 printf("building host lists from %s\n", hostlist_file);
1023 rv = TEST_SNAPSHOT_FILE_READ(hostent, hostlist_file, &td,
1024 hostent_read_hostlist_func);
1028 if (snapshot_file != NULL) {
1029 if (access(snapshot_file, W_OK | R_OK) != 0) {
1030 if (errno == ENOENT) {
1031 if (method != TEST_GETHOSTBYADDR)
1032 method = TEST_BUILD_SNAPSHOT;
1034 method = TEST_BUILD_ADDR_SNAPSHOT;
1037 printf("can't access the snapshot file %s\n",
1044 rv = TEST_SNAPSHOT_FILE_READ(hostent, snapshot_file,
1045 &td_snap, hostent_read_snapshot_func);
1048 printf("error reading snapshot file\n");
1055 case TEST_GETHOSTBYNAME2:
1056 if (snapshot_file != NULL)
1057 rv = DO_2PASS_TEST(hostent, &td, &td_snap,
1058 compare_hostent, NULL);
1060 case TEST_GETHOSTBYADDR:
1061 rv = DO_1PASS_TEST(hostent, &td,
1062 hostent_test_gethostbyaddr, (void *)&td_addr);
1064 if (snapshot_file != NULL)
1065 rv = DO_2PASS_TEST(hostent, &td_addr, &td_snap,
1066 compare_hostent, NULL);
1068 case TEST_GETHOSTBYNAME2_GETADDRINFO:
1069 rv = DO_1PASS_TEST(hostent, &td,
1070 hostent_test_getaddrinfo_eq, NULL);
1072 case TEST_GETHOSTBYADDR_GETNAMEINFO:
1073 rv = DO_1PASS_TEST(hostent, &td,
1074 hostent_test_getnameinfo_eq, NULL);
1076 case TEST_BUILD_SNAPSHOT:
1077 if (snapshot_file != NULL) {
1078 rv = TEST_SNAPSHOT_FILE_WRITE(hostent, snapshot_file, &td,
1082 case TEST_BUILD_ADDR_SNAPSHOT:
1083 if (snapshot_file != NULL) {
1084 rv = DO_1PASS_TEST(hostent, &td,
1085 hostent_test_gethostbyaddr, (void *)&td_addr);
1087 rv = TEST_SNAPSHOT_FILE_WRITE(hostent, snapshot_file,
1088 &td_addr, sdump_hostent);
1097 TEST_DATA_DESTROY(hostent, &td_snap);
1098 TEST_DATA_DESTROY(hostent, &td_addr);
1099 TEST_DATA_DESTROY(hostent, &td);
1100 free(hostlist_file);
1101 free(snapshot_file);