PERFORCE change 100566 for review
Michael Bushkov
bushman at FreeBSD.org
Tue Jul 4 17:40:44 UTC 2006
http://perforce.freebsd.org/chv.cgi?CH=100566
Change 100566 by bushman at bushman_nss_ldap_cached on 2006/07/04 17:39:57
gethostby***() regression test finished - some issues with host aliases left, though. The test can be possibly extended by adding support for snapshot file for hostents received via gethostbyaddr().
Affected files ...
.. //depot/projects/soc2006/nss_ldap_cached/src/tools/regression/lib/libc/nss/Makefile#5 edit
.. //depot/projects/soc2006/nss_ldap_cached/src/tools/regression/lib/libc/nss/test-gethostby.c#2 edit
.. //depot/projects/soc2006/nss_ldap_cached/src/tools/regression/lib/libc/nss/test-gethostby.t#2 edit
Differences ...
==== //depot/projects/soc2006/nss_ldap_cached/src/tools/regression/lib/libc/nss/Makefile#5 (text+ko) ====
==== //depot/projects/soc2006/nss_ldap_cached/src/tools/regression/lib/libc/nss/test-gethostby.c#2 (text+ko) ====
@@ -30,6 +30,8 @@
#include <arpa/inet.h>
#include <sys/socket.h>
+#include <sys/types.h>
+#include <netinet/in.h>
#include <assert.h>
#include <errno.h>
#include <netdb.h>
@@ -40,11 +42,14 @@
#include <unistd.h>
#include "testutil.h"
+#ifndef min
+#define min(a,b) (((a)<(b))?(a):(b))
+#endif
+
enum test_methods {
TEST_GETHOSTBYNAME2,
TEST_GETHOSTBYADDR,
TEST_GETHOSTBYNAME2_GETADDRINFO,
- TEST_GETHOSTBYADDR_GETNAMEINFO,
TEST_BUILD_SNAPSHOT
};
@@ -183,28 +188,34 @@
c1 = ht1->h_aliases;
c2 = ht2->h_aliases;
- if ((ht1->h_aliases == NULL) || (ht2->h_aliases == NULL))
+ if (((ht1->h_aliases == NULL) || (ht2->h_aliases == NULL)) &&
+ (ht1->h_aliases != ht2->h_aliases))
goto errfin;
- for (;*c1 && *c2; ++c1, ++c2)
- if (strcmp(*c1, *c2) != 0)
- goto errfin;
+ if ((c1 != NULL) && (c2 != NULL)) {
+ for (;*c1 && *c2; ++c1, ++c2)
+ if (strcmp(*c1, *c2) != 0)
+ goto errfin;
if ((*c1 != NULL) || (*c2 != NULL))
goto errfin;
+ }
c1 = ht1->h_addr_list;
c2 = ht2->h_addr_list;
- if ((ht1->h_addr_list == NULL) || (ht2->h_addr_list== NULL))
+ if (((ht1->h_addr_list == NULL) || (ht2->h_addr_list== NULL)) &&
+ (ht1->h_addr_list != ht2->h_addr_list))
goto errfin;
- for (;*c1 && *c2; ++c1, ++c2)
- if (memcmp(*c1, *c2, ht1->h_length) != 0)
+ if ((c1 != NULL) && (c2 != NULL)) {
+ for (;*c1 && *c2; ++c1, ++c2)
+ if (memcmp(*c1, *c2, ht1->h_length) != 0)
+ goto errfin;
+
+ if ((*c1 != NULL) || (*c2 != NULL))
goto errfin;
-
- if ((*c1 != NULL) || (*c2 != NULL))
- goto errfin;
+ }
return 0;
@@ -218,6 +229,64 @@
return (-1);
}
+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,
+ socklen_t addrlen, int af)
+{
+ struct addrinfo *ai2;
+
+/* char buffer[100];
+ memset(buffer, 0, sizeof(buffer));
+
+ char *res = inet_ntop(af, addr, buffer,
+ sizeof(buffer));
+ if (res != NULL)
+ printf("===he %s %s\n", buffer, ai->ai_canonname);
+
+*/
+ for (ai2 = ai; ai2 != NULL; ai2 = ai2->ai_next) {
+ if (af != ai2->ai_family)
+ continue;
+
+/* res = inet_ntop(ai2->ai_family, (void *)&((struct sockaddr_in *)ai2->ai_addr)->sin_addr, buffer,
+ sizeof(buffer));
+ if (res != NULL)
+ printf("===ai %s\n", buffer);*/
+
+ switch (af) {
+ case AF_INET:
+ if (memcmp(addr,
+ (void *)&((struct sockaddr_in *)ai2->ai_addr)->sin_addr,
+ min(addrlen, ai2->ai_addrlen)) == 0)
+ return (0);
+ break;
+ case AF_INET6:
+ if (memcmp(addr,
+ (void *)&((struct sockaddr_in6 *)ai2->ai_addr)->sin6_addr,
+ min(addrlen, ai2->ai_addrlen)) == 0)
+ return (0);
+ break;
+ default:
+ break;
+ }
+ }
+
+ return (-1);
+}
+
static int
is_hostent_equal(struct hostent *he, struct addrinfo *ai)
{
@@ -225,10 +294,42 @@
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);
+ }
+
+ /* getaddrinfo doesn't provide the aliases information */
/* for (cp = he->h_aliases; *cp; ++cp) {
- for (ai2 = ai; ai2 != NULL; ai2 = ai2->ai_next)
- if (strcmp(
+ rv = check_addrinfo_for_name(ai, *cp);
+ if (rv != 0) {
+ if (debug)
+ printf("not equal - one of he->h_aliases couldn't be found\n");
+
+ return (rv);
+ }
}*/
+
+ for (cp = he->h_addr_list; *cp; ++cp) {
+ rv = check_addrinfo_for_addr(ai, *cp, he->h_length,
+ he->h_addrtype);
+ 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);
}
@@ -323,7 +424,7 @@
if (result != NULL) {
if (debug)
printf("found\n");
-
+
rv = hostent_test_correctness(result, NULL);
if (rv != 0)
return (rv);
@@ -553,6 +654,12 @@
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);
if (rv != 0)
return (rv);
@@ -573,7 +680,8 @@
ai = NULL;
memset(&hints, 0, sizeof(struct addrinfo));
- hints.ai_flags = af_type;
+ hints.ai_family = af_type;
+ hints.ai_flags = AI_CANONNAME;
if (debug)
printf("using getaddrinfo() to resolve %s\n", he->h_name);
@@ -595,15 +703,152 @@
return (-1);
}
-// rv =
+ 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 void
+usage(void)
+{
+ (void)fprintf(stderr,
+ "Usage: %s [-dna2] [-s <file>] -f <file>\n",
+ getprogname());
+ exit(1);
+}
+
int
main(int argc, char **argv)
{
+ struct hostent_test_data td, td_snap;
+ char *snapshot_file, *hostlist_file;
+ int rv;
+ int c;
+
+ if (argc < 2)
+ usage();
+
+ snapshot_file = NULL;
+ hostlist_file = NULL;
+ while ((c = getopt(argc, argv, "na2ds:f:")) != -1)
+ switch (c) {
+ case 'd':
+ debug++;
+ break;
+ case 'n':
+ method = TEST_GETHOSTBYNAME2;
+ break;
+ case 'a':
+ method = TEST_GETHOSTBYADDR;
+ break;
+ case '2':
+ method = TEST_GETHOSTBYNAME2_GETADDRINFO;
+ break;
+ case 's':
+ snapshot_file = strdup(optarg);
+ break;
+ case 'f':
+ hostlist_file = strdup(optarg);
+ break;
+ default:
+ usage();
+ }
+
+ TEST_DATA_INIT(hostent, &td, 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);
+ rv = TEST_SNAPSHOT_FILE_READ(hostent, hostlist_file, &td,
+ hostent_read_hostlist_func);
+ if (rv != 0)
+ goto fin;
+
+ if (snapshot_file != NULL) {
+ if (access(snapshot_file, W_OK | R_OK) != 0) {
+ if (errno == ENOENT)
+ method = TEST_BUILD_SNAPSHOT;
+ else {
+ if (debug)
+ printf("can't access the snapshot file %s\n",
+ snapshot_file);
+
+ rv = -1;
+ goto fin;
+ }
+ } else {
+ if (method == TEST_BUILD_SNAPSHOT) {
+ rv = 0;
+ goto fin;
+ }
+
+ TEST_SNAPSHOT_FILE_READ(hostent, snapshot_file,
+ &td_snap, hostent_read_snapshot_func);
+ }
+ }
+
+ switch (method) {
+ case TEST_GETHOSTBYNAME2:
+ if (snapshot_file != NULL)
+ rv = DO_2PASS_TEST(hostent, &td, &td_snap,
+ compare_hostent, NULL);
+ break;
+ case TEST_GETHOSTBYADDR:
+ if (snapshot_file == NULL)
+ rv = DO_1PASS_TEST(hostent, &td,
+ hostent_test_gethostbyaddr, (void *)&td);
+ else
+ rv = DO_1PASS_TEST(hostent, &td_snap,
+ hostent_test_gethostbyaddr, (void *)&td_snap);
+ break;
+ case TEST_GETHOSTBYNAME2_GETADDRINFO:
+ if (snapshot_file == NULL)
+ rv = DO_1PASS_TEST(hostent, &td,
+ hostent_test_getaddrinfo_eq, (void *)&td);
+ else
+ rv = DO_1PASS_TEST(hostent, &td_snap,
+ hostent_test_getaddrinfo_eq, (void *)&td);
+ break;
+ case TEST_BUILD_SNAPSHOT:
+ if (snapshot_file != NULL)
+ rv = TEST_SNAPSHOT_FILE_WRITE(hostent, snapshot_file, &td,
+ sdump_hostent);
+ break;
+ default:
+ rv = 0;
+ break;
+ };
+
+fin:
+ TEST_DATA_DESTROY(hostent, &td_snap);
+ TEST_DATA_DESTROY(hostent, &td);
+ free(hostlist_file);
+ free(snapshot_file);
+ return (rv);
+}
+
+
+/*int
+main(int argc, char **argv)
+{
struct hostent_test_data td;
debug = 1;
@@ -611,7 +856,7 @@
hostent_fill_gethostbyname2_data("../resolv/mach", &td);
TEST_DATA_DESTROY(hostent, &td);
return (0);
-}
+}*/
/*int
main(int argc, char **argv)
==== //depot/projects/soc2006/nss_ldap_cached/src/tools/regression/lib/libc/nss/test-gethostby.t#2 (text+ko) ====
@@ -1,0 +1,29 @@
+#!/bin/sh
+# $FreeBSD$
+
+do_test() {
+ number=$1
+ comment=$2
+ opt=$3
+ if ./$executable $opt; then
+ echo "ok $number - $comment"
+ else
+ echo "not ok $number - $comment"
+ fi
+}
+
+cd `dirname $0`
+
+executable=`basename $0 .t`
+
+make $executable 2>&1 > /dev/null
+
+echo 1..8
+do_test 1 'gethostbyname2()' '-n -f ../resolv/mach'
+do_test 2 'gethostbyaddr()' '-a -f ../resolv/mach'
+do_test 3 'gethostbyname()-getaddrinfo()' '-2 -f ../resolv/mach'
+do_test 5 'building snapshot, if needed' '-s snapshot_ht -f ../resolv/mach'
+do_test 6 'gethostbyname2() snapshot' '-n -s snapshot_serv -f ../resolv/mach'
+do_test 7 'gethostbyaddr() snapshot' '-a -s snapshot_serv -f ../resolv/mach'
+do_test 8 'gethostbyname2()-getaddrinfo snapshot'\
+ '-2 -s snapshot_serv -f ../resolv/mach'
More information about the p4-projects
mailing list