diff options
Diffstat (limited to 'tools/regression/lib/libc/nss/test-gethostby.c')
-rw-r--r-- | tools/regression/lib/libc/nss/test-gethostby.c | 298 |
1 files changed, 149 insertions, 149 deletions
diff --git a/tools/regression/lib/libc/nss/test-gethostby.c b/tools/regression/lib/libc/nss/test-gethostby.c index 77455e7..c6cacc0 100644 --- a/tools/regression/lib/libc/nss/test-gethostby.c +++ b/tools/regression/lib/libc/nss/test-gethostby.c @@ -90,7 +90,7 @@ static int hostent_test_correctness(struct hostent *, void *); static int hostent_test_gethostbyaddr(struct hostent *, void *); static int hostent_test_getaddrinfo_eq(struct hostent *, void *); static int hostent_test_getnameinfo_eq(struct hostent *, void *); - + static void usage(void) __attribute__((__noreturn__)); IMPLEMENT_TEST_DATA(hostent) @@ -103,7 +103,7 @@ __gethostbyname2(const char *name, int af) { struct hostent *he; int error; - + if (use_ipnode_functions == 0) he = gethostbyname2(name, af); else { @@ -112,7 +112,7 @@ __gethostbyname2(const char *name, int af) if (he == NULL); errno = error; } - + return (he); } @@ -121,7 +121,7 @@ __gethostbyaddr(const void *addr, socklen_t len, int af) { struct hostent *he; int error; - + if (use_ipnode_functions == 0) he = gethostbyaddr(addr, len, af); else { @@ -130,8 +130,8 @@ __gethostbyaddr(const void *addr, socklen_t len, int af) if (he == NULL) errno = error; } - - return (he); + + return (he); } static void @@ -147,44 +147,44 @@ clone_hostent(struct hostent *dest, struct hostent const *src) { assert(dest != NULL); assert(src != NULL); - + char **cp; int aliases_num; int addrs_num; size_t offset; - + memset(dest, 0, sizeof(struct hostent)); - + if (src->h_name != NULL) { dest->h_name = strdup(src->h_name); assert(dest->h_name != NULL); } - + dest->h_addrtype = src->h_addrtype; dest->h_length = src->h_length; - + if (src->h_aliases != NULL) { aliases_num = 0; for (cp = src->h_aliases; *cp; ++cp) ++aliases_num; - + dest->h_aliases = (char **)malloc((aliases_num + 1) * (sizeof(char *))); assert(dest->h_aliases != NULL); memset(dest->h_aliases, 0, (aliases_num + 1) * (sizeof(char *))); - + for (cp = src->h_aliases; *cp; ++cp) { dest->h_aliases[cp - src->h_aliases] = strdup(*cp); assert(dest->h_aliases[cp - src->h_aliases] != NULL); } } - + if (src->h_addr_list != NULL) { addrs_num = 0; for (cp = src->h_addr_list; *cp; ++cp) ++addrs_num; - + dest->h_addr_list = (char **)malloc((addrs_num + 1) * (sizeof(char *))); assert(dest->h_addr_list != NULL); @@ -193,7 +193,7 @@ clone_hostent(struct hostent *dest, struct hostent const *src) for (cp = src->h_addr_list; *cp; ++cp) { offset = cp - src->h_addr_list; - dest->h_addr_list[offset] = + dest->h_addr_list[offset] = (char *)malloc(src->h_length); assert(dest->h_addr_list[offset] != NULL); memcpy(dest->h_addr_list[offset], @@ -202,15 +202,15 @@ clone_hostent(struct hostent *dest, struct hostent const *src) } } -static void +static void free_hostent(struct hostent *ht) { char **cp; - + assert(ht != NULL); - + free(ht->h_name); - + if (ht->h_aliases != NULL) { for (cp = ht->h_aliases; *cp; ++cp) free(*cp); @@ -224,33 +224,33 @@ free_hostent(struct hostent *ht) } } -static int +static int compare_hostent(struct hostent *ht1, struct hostent *ht2, void *mdata) { char **c1, **c2, **ct, **cb; int b; - + if (ht1 == ht2) return 0; - + if ((ht1 == NULL) || (ht2 == NULL)) goto errfin; - + if ((ht1->h_name == NULL) || (ht2->h_name == NULL)) goto errfin; - + if ((ht1->h_addrtype != ht2->h_addrtype) || - (ht1->h_length != ht2->h_length) || + (ht1->h_length != ht2->h_length) || (strcmp(ht1->h_name, ht2->h_name) != 0)) goto errfin; - + c1 = ht1->h_aliases; c2 = ht2->h_aliases; - + if (((ht1->h_aliases == NULL) || (ht2->h_aliases == NULL)) && (ht1->h_aliases != ht2->h_aliases)) goto errfin; - + if ((c1 != NULL) && (c2 != NULL)) { cb = c1; for (;*c1; ++c1) { @@ -286,14 +286,14 @@ compare_hostent(struct hostent *ht1, struct hostent *ht2, void *mdata) } } } - + c1 = ht1->h_addr_list; c2 = ht2->h_addr_list; - + if (((ht1->h_addr_list == NULL) || (ht2->h_addr_list== NULL)) && (ht1->h_addr_list != ht2->h_addr_list)) goto errfin; - + if ((c1 != NULL) && (c2 != NULL)) { cb = c1; for (;*c1; ++c1) { @@ -311,7 +311,7 @@ compare_hostent(struct hostent *ht1, struct hostent *ht2, void *mdata) goto errfin; } } - + c1 = cb; for (;*c2; ++c2) { b = 0; @@ -331,7 +331,7 @@ compare_hostent(struct hostent *ht1, struct hostent *ht2, void *mdata) } return 0; - + errfin: if ((debug) && (mdata == NULL)) { printf("following structures are not equal:\n"); @@ -346,28 +346,28 @@ static int check_addrinfo_for_name(struct addrinfo *ai, char const *name) { struct addrinfo *ai2; - + for (ai2 = ai; ai2 != NULL; ai2 = ai2->ai_next) { if (strcmp(ai2->ai_canonname, name) == 0) return (0); } - + return (-1); } static int -check_addrinfo_for_addr(struct addrinfo *ai, char const *addr, +check_addrinfo_for_addr(struct addrinfo *ai, char const *addr, socklen_t addrlen, int af) { struct addrinfo *ai2; - + for (ai2 = ai; ai2 != NULL; ai2 = ai2->ai_next) { if (af != ai2->ai_family) continue; - + switch (af) { case AF_INET: - if (memcmp(addr, + if (memcmp(addr, (void *)&((struct sockaddr_in *)ai2->ai_addr)->sin_addr, min(addrlen, ai2->ai_addrlen)) == 0) return (0); @@ -382,24 +382,24 @@ check_addrinfo_for_addr(struct addrinfo *ai, char const *addr, break; } } - + return (-1); } -static int +static int is_hostent_equal(struct hostent *he, struct addrinfo *ai) { char **cp; int rv; - + if (debug) printf("checking equality of he and ai\n"); - + rv = check_addrinfo_for_name(ai, he->h_name); if (rv != 0) { if (debug) printf("not equal - he->h_name couldn't be found\n"); - + return (rv); } @@ -409,14 +409,14 @@ is_hostent_equal(struct hostent *he, struct addrinfo *ai) if (rv != 0) { if (debug) printf("not equal - one of he->h_addr_list couldn't be found\n"); - + return (rv); } } - + if (debug) printf("equal\n"); - + return (0); } @@ -426,14 +426,14 @@ sdump_hostent(struct hostent *ht, char *buffer, size_t buflen) char **cp; size_t i; int written; - + written = snprintf(buffer, buflen, "%s %d %d", ht->h_name, ht->h_addrtype, ht->h_length); buffer += written; if (written > buflen) return; buflen -= written; - + if (ht->h_aliases != NULL) { if (*(ht->h_aliases) != NULL) { for (cp = ht->h_aliases; *cp; ++cp) { @@ -442,53 +442,53 @@ sdump_hostent(struct hostent *ht, char *buffer, size_t buflen) if (written > buflen) return; buflen -= written; - + if (buflen == 0) - return; + return; } } else { written = snprintf(buffer, buflen, " noaliases"); buffer += written; if (written > buflen) return; - buflen -= written; + buflen -= written; } } else { written = snprintf(buffer, buflen, " (null)"); buffer += written; if (written > buflen) return; - buflen -= written; + buflen -= written; } - + written = snprintf(buffer, buflen, " : "); buffer += written; if (written > buflen) return; - buflen -= written; + buflen -= written; if (ht->h_addr_list != NULL) { if (*(ht->h_addr_list) != NULL) { for (cp = ht->h_addr_list; *cp; ++cp) { for (i = 0; i < ht->h_length; ++i ) { - written = snprintf(buffer, buflen, + written = snprintf(buffer, buflen, i + 1 != ht->h_length ? "%d." : "%d", (unsigned char)(*cp)[i]); buffer += written; if (written > buflen) return; buflen -= written; - + if (buflen == 0) return; } - + if (*(cp + 1) ) { written = snprintf(buffer, buflen, " "); buffer += written; if (written > buflen) return; - buflen -= written; + buflen -= written; } } } else { @@ -496,42 +496,42 @@ sdump_hostent(struct hostent *ht, char *buffer, size_t buflen) buffer += written; if (written > buflen) return; - buflen -= written; + buflen -= written; } } else { written = snprintf(buffer, buflen, " (null)"); buffer += written; if (written > buflen) return; - buflen -= written; - } + buflen -= written; + } } static int hostent_read_hostlist_func(struct hostent *he, char *line) { struct hostent *result; - int rv; - + int rv; + if (debug) printf("resolving %s: ", line); result = __gethostbyname2(line, af_type); if (result != NULL) { if (debug) printf("found\n"); - + rv = hostent_test_correctness(result, NULL); if (rv != 0) { __freehostent(result); return (rv); } - + clone_hostent(he, result); __freehostent(result); } else { if (debug) printf("not found\n"); - + memset(he, 0, sizeof(struct hostent)); he->h_name = strdup(line); assert(he->h_name != NULL); @@ -543,12 +543,12 @@ static int hostent_read_snapshot_addr(char *addr, unsigned char *result, size_t len) { char *s, *ps, *ts; - + ps = addr; while ( (s = strsep(&ps, ".")) != NULL) { if (len == 0) return (-1); - + *result = (unsigned char)strtol(s, &ts, 10); ++result; if (*ts != '\0') @@ -571,7 +571,7 @@ hostent_read_snapshot_func(struct hostent *ht, char *line) if (debug) printf("1 line read from snapshot:\n%s\n", line); - + rv = 0; i = 0; sl1 = sl2 = NULL; @@ -595,15 +595,15 @@ hostent_read_snapshot_func(struct hostent *ht, char *line) if (*ts != '\0') goto fin; break; - + case 3: if (sl1 == NULL) { if (strcmp(s, "(null)") == 0) return (0); - + sl1 = sl_init(); assert(sl1 != NULL); - + if (strcmp(s, "noaliases") != 0) { ts = strdup(s); assert(ts != NULL); @@ -618,16 +618,16 @@ hostent_read_snapshot_func(struct hostent *ht, char *line) sl_add(sl1, ts); } } - break; + break; case 4: if (sl2 == NULL) { if (strcmp(s, "(null)") == 0) return (0); - + sl2 = sl_init(); assert(sl2 != NULL); - + if (strcmp(s, "noaddrs") != 0) { ts = (char *)malloc(ht->h_length); assert(ts != NULL); @@ -650,9 +650,9 @@ hostent_read_snapshot_func(struct hostent *ht, char *line) } break; default: - break; + break; }; - + if ((i != 3) && (i != 4)) ++i; } @@ -667,19 +667,19 @@ fin: ht->h_addr_list = sl2->sl_str; } - if ((i != 4) || (rv != 0)) { + if ((i != 4) || (rv != 0)) { free_hostent(ht); memset(ht, 0, sizeof(struct hostent)); return (-1); } - + /* NOTE: is it a dirty hack or not? */ free(sl1); - free(sl2); + free(sl2); return (0); } -static void +static void dump_hostent(struct hostent *result) { if (result != NULL) { @@ -697,34 +697,34 @@ hostent_test_correctness(struct hostent *ht, void *mdata) printf("testing correctness with the following data:\n"); dump_hostent(ht); } - + if (ht == NULL) goto errfin; - + if (ht->h_name == NULL) goto errfin; if (!((ht->h_addrtype >= 0) && (ht->h_addrtype < AF_MAX))) goto errfin; - - if ((ht->h_length != sizeof(struct in_addr)) && + + if ((ht->h_length != sizeof(struct in_addr)) && (ht->h_length != sizeof(struct in6_addr))) goto errfin; - + if (ht->h_aliases == NULL) goto errfin; - + if (ht->h_addr_list == NULL) goto errfin; - + if (debug) printf("correct\n"); - - return (0); + + return (0); errfin: if (debug) printf("incorrect\n"); - + return (-1); } @@ -733,23 +733,23 @@ hostent_test_gethostbyaddr(struct hostent *he, void *mdata) { struct hostent *result; struct hostent_test_data *addr_test_data; - int rv; - + int rv; + addr_test_data = (struct hostent_test_data *)mdata; - + /* We should omit unresolved hostents */ if (he->h_addr_list != NULL) { char **cp; for (cp = he->h_addr_list; *cp; ++cp) { if (debug) printf("doing reverse lookup for %s\n", he->h_name); - + result = __gethostbyaddr(*cp, he->h_length, he->h_addrtype); if (result == NULL) { if (debug) printf("warning: reverse lookup failed\n"); - + continue; } rv = hostent_test_correctness(result, NULL); @@ -757,14 +757,14 @@ hostent_test_gethostbyaddr(struct hostent *he, void *mdata) __freehostent(result); return (rv); } - + if (addr_test_data != NULL) TEST_DATA_APPEND(hostent, addr_test_data, result); - + __freehostent(result); } } - + return (0); } @@ -772,16 +772,16 @@ static int hostent_test_getaddrinfo_eq(struct hostent *he, void *mdata) { struct addrinfo *ai, hints; - int rv; - + int rv; + ai = NULL; memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = af_type; hints.ai_flags = AI_CANONNAME; - + if (debug) printf("using getaddrinfo() to resolve %s\n", he->h_name); - + /* struct hostent *he was not resolved */ if (he->h_addr_list == NULL) { /* We can be sure that he->h_name is not NULL */ @@ -798,20 +798,20 @@ hostent_test_getaddrinfo_eq(struct hostent *he, void *mdata) printf("not ok - should have beed resolved\n"); return (-1); } - + rv = is_hostent_equal(he, ai); if (rv != 0) { if (debug) printf("not ok - addrinfo and hostent are not equal\n"); return (-1); } - + } - + return (0); } -static int +static int hostent_test_getnameinfo_eq(struct hostent *he, void *mdata) { char buffer[NI_MAXHOST]; @@ -820,13 +820,13 @@ hostent_test_getnameinfo_eq(struct hostent *he, void *mdata) struct sockaddr *saddr; struct hostent *result; int rv; - + if (he->h_addr_list != NULL) { char **cp; for (cp = he->h_addr_list; *cp; ++cp) { if (debug) printf("doing reverse lookup for %s\n", he->h_name); - + result = __gethostbyaddr(*cp, he->h_length, he->h_addrtype); if (result != NULL) { @@ -837,16 +837,16 @@ hostent_test_getnameinfo_eq(struct hostent *he, void *mdata) } } else { if (debug) - printf("reverse lookup failed\n"); + printf("reverse lookup failed\n"); } - + switch (he->h_addrtype) { case AF_INET: memset(&sin, 0, sizeof(struct sockaddr_in)); sin.sin_len = sizeof(struct sockaddr_in); sin.sin_family = AF_INET; memcpy(&sin.sin_addr, *cp, he->h_length); - + saddr = (struct sockaddr *)&sin; break; case AF_INET6: @@ -854,7 +854,7 @@ hostent_test_getnameinfo_eq(struct hostent *he, void *mdata) sin6.sin6_len = sizeof(struct sockaddr_in6); sin6.sin6_family = AF_INET6; memcpy(&sin6.sin6_addr, *cp, he->h_length); - + saddr = (struct sockaddr *)&sin6; break; default: @@ -863,38 +863,38 @@ hostent_test_getnameinfo_eq(struct hostent *he, void *mdata) he->h_addrtype); continue; } - + assert(saddr != NULL); - rv = getnameinfo(saddr, saddr->sa_len, buffer, + rv = getnameinfo(saddr, saddr->sa_len, buffer, sizeof(buffer), NULL, 0, NI_NAMEREQD); - + if ((rv != 0) && (result != NULL)) { if (debug) printf("not ok - getnameinfo() didn't make the reverse lookup, when it should have (%s)\n", gai_strerror(rv)); return (rv); } - + if ((rv == 0) && (result == NULL)) { if (debug) - printf("not ok - getnameinfo() made the reverse lookup, when it shouldn't have\n"); + printf("not ok - getnameinfo() made the reverse lookup, when it shouldn't have\n"); return (rv); } - + if ((rv != 0) && (result == NULL)) { if (debug) printf("ok - both getnameinfo() and ***byaddr() failed\n"); - + continue; } - + if (debug) printf("comparing %s with %s\n", result->h_name, - buffer); - + buffer); + rv = strcmp(result->h_name, buffer); __freehostent(result); - + if (rv != 0) { if (debug) printf("not ok - getnameinfo() and ***byaddr() results are not equal\n"); @@ -903,10 +903,10 @@ hostent_test_getnameinfo_eq(struct hostent *he, void *mdata) if (debug) printf("ok - getnameinfo() and ***byaddr() results are equal\n"); } - } + } } - - return (0); + + return (0); } static void @@ -923,13 +923,13 @@ main(int argc, char **argv) { struct hostent_test_data td, td_addr, td_snap; char *snapshot_file, *hostlist_file; - res_state statp; + res_state statp; int rv; int c; - + if (argc < 2) usage(); - + snapshot_file = NULL; hostlist_file = NULL; while ((c = getopt(argc, argv, "nad2iod46mAcMs:f:")) != -1) @@ -986,37 +986,37 @@ main(int argc, char **argv) if (use_ipnode_functions == 0) { statp = __res_state(); - if ((statp == NULL) || ((statp->options & RES_INIT) == 0 && + if ((statp == NULL) || ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1)) { if (debug) printf("error: can't init res_state\n"); - + free(snapshot_file); free(hostlist_file); return (-1); } - - if (use_ipv6_mapping == 0) + + if (use_ipv6_mapping == 0) statp->options &= ~RES_USE_INET6; else statp->options |= RES_USE_INET6; } - + TEST_DATA_INIT(hostent, &td, clone_hostent, free_hostent); TEST_DATA_INIT(hostent, &td_addr, clone_hostent, free_hostent); TEST_DATA_INIT(hostent, &td_snap, clone_hostent, free_hostent); - + if (hostlist_file == NULL) usage(); - + if (access(hostlist_file, R_OK) != 0) { if (debug) printf("can't access the hostlist file %s\n", hostlist_file); - + usage(); } - + if (debug) printf("building host lists from %s\n", hostlist_file); @@ -1024,9 +1024,9 @@ main(int argc, char **argv) hostent_read_hostlist_func); if (rv != 0) goto fin; - + if (snapshot_file != NULL) { - if (access(snapshot_file, W_OK | R_OK) != 0) { + if (access(snapshot_file, W_OK | R_OK) != 0) { if (errno == ENOENT) { if (method != TEST_GETHOSTBYADDR) method = TEST_BUILD_SNAPSHOT; @@ -1036,7 +1036,7 @@ main(int argc, char **argv) if (debug) printf("can't access the snapshot file %s\n", snapshot_file); - + rv = -1; goto fin; } @@ -1050,7 +1050,7 @@ main(int argc, char **argv) } } } - + switch (method) { case TEST_GETHOSTBYNAME2: if (snapshot_file != NULL) @@ -1062,7 +1062,7 @@ main(int argc, char **argv) hostent_test_gethostbyaddr, (void *)&td_addr); if (snapshot_file != NULL) - rv = DO_2PASS_TEST(hostent, &td_addr, &td_snap, + rv = DO_2PASS_TEST(hostent, &td_addr, &td_snap, compare_hostent, NULL); break; case TEST_GETHOSTBYNAME2_GETADDRINFO: @@ -1075,7 +1075,7 @@ main(int argc, char **argv) break; case TEST_BUILD_SNAPSHOT: if (snapshot_file != NULL) { - rv = TEST_SNAPSHOT_FILE_WRITE(hostent, snapshot_file, &td, + rv = TEST_SNAPSHOT_FILE_WRITE(hostent, snapshot_file, &td, sdump_hostent); } break; @@ -1083,8 +1083,8 @@ main(int argc, char **argv) if (snapshot_file != NULL) { rv = DO_1PASS_TEST(hostent, &td, hostent_test_gethostbyaddr, (void *)&td_addr); - - rv = TEST_SNAPSHOT_FILE_WRITE(hostent, snapshot_file, + + rv = TEST_SNAPSHOT_FILE_WRITE(hostent, snapshot_file, &td_addr, sdump_hostent); } break; |