PERFORCE change 100392 for review
Michael Bushkov
bushman at FreeBSD.org
Sat Jul 1 12:17:53 UTC 2006
http://perforce.freebsd.org/chv.cgi?CH=100392
Change 100392 by bushman at bushman_nss_ldap_cached on 2006/07/01 12:17:06
test-getserv.c truncated - some of its functionality was moved to testutil.h in more general form
Affected files ...
.. //depot/projects/soc2006/nss_ldap_cached/src/tools/regression/lib/libc/nss/test-getserv.c#3 edit
.. //depot/projects/soc2006/nss_ldap_cached/src/tools/regression/lib/libc/nss/testutil.h#3 edit
Differences ...
==== //depot/projects/soc2006/nss_ldap_cached/src/tools/regression/lib/libc/nss/test-getserv.c#3 (text+ko) ====
@@ -43,15 +43,17 @@
TEST_GETSERVENT,
TEST_GETSERVBYNAME,
TEST_GETSERVBYPORT,
- TEST_GETSERVENT_2PASS
+ TEST_GETSERVENT_2PASS,
+ TEST_BUILD_SNAPSHOT
};
static int debug = 0;
static enum test_methods method = TEST_GETSERVENT;
DECLARE_TEST_DATA(servent)
-
-static int check_servent_ambiguity_func(struct servent *, void *);
+DECLARE_FILE_SNAPSHOT(servent)
+DECLARE_1PASS_TEST(servent)
+DECLARE_2PASS_TEST(servent)
static void clone_servent(struct servent *, struct servent const *);
static int compare_servent(struct servent *, struct servent *, void *);
@@ -59,24 +61,24 @@
static void free_servent(struct servent *);
static void sdump_servent(struct servent *, char *, size_t);
-static int servent_1pass_test(int (*)(struct servent *, void *));
-static int servent_2pass_test();
+static int servent_read_snapshot_func(struct servent *, char *);
+
+static int check_servent_ambiguity_func(struct servent *, void *);
static int servent_check_ambiguity(struct servent_test_data *,
struct servent *);
-static void servent_fill_test_data(struct servent_test_data *);
+
+static int servent_fill_test_data(struct servent_test_data *);
static int servent_test_correctness(struct servent *, void *);
static int servent_test_getservbyname(struct servent *, void *);
static int servent_test_getservbyport(struct servent *, void *);
static int servent_test_getservent(struct servent *, void *);
-
-static int servent_read_snapshot_func(struct servent *, char *);
-static int servent_read_snapshot(char const *, struct servent_test_data *);
-static int servent_write_snapshot(char const *, struct servent_test_data *);
-static int servent_write_snapshot_func(struct servent *, void *);
static void usage(void) __attribute__((__noreturn__));
IMPLEMENT_TEST_DATA(servent)
+IMPLEMENT_FILE_SNAPSHOT(servent)
+IMPLEMENT_1PASS_TEST(servent)
+IMPLEMENT_2PASS_TEST(servent)
static void
clone_servent(struct servent *dest, struct servent const *src)
@@ -194,6 +196,79 @@
snprintf(buffer, buflen, " (null)");
}
+static int
+servent_read_snapshot_func(struct servent *serv, char *line)
+{
+ StringList *sl;
+ char *s, *ps, *ts;
+ int i;
+
+ if (debug)
+ printf("1 line read from snapshot:\n%s\n", line);
+
+ i = 0;
+ sl = NULL;
+ ps = line;
+ memset(serv, 0, sizeof(struct servent));
+ while ( (s = strsep(&ps, " ")) != NULL) {
+ switch (i) {
+ case 0:
+ serv->s_name = strdup(s);
+ assert(serv->s_name != NULL);
+ break;
+
+ case 1:
+ serv->s_port = htons(
+ (int)strtol(s, &ts, 10));
+ if (*ts != '\0') {
+ free(serv->s_name);
+ return (-1);
+ }
+ break;
+
+ case 2:
+ serv->s_proto = strdup(s);
+ assert(serv->s_proto != NULL);
+ break;
+
+ default:
+ if (sl == NULL) {
+ if (strcmp(s, "(null)") == 0)
+ return (0);
+
+ sl = sl_init();
+ assert(sl != NULL);
+
+ if (strcmp(s, "noaliases") != 0) {
+ ts = strdup(s);
+ assert(ts != NULL);
+ sl_add(sl, ts);
+ }
+ } else {
+ ts = strdup(s);
+ assert(ts != NULL);
+ sl_add(sl, ts);
+ }
+ break;
+ };
+ ++i;
+ }
+
+ if (i < 3) {
+ free(serv->s_name);
+ free(serv->s_proto);
+ memset(serv, 0, sizeof(struct servent));
+ return (-1);
+ }
+
+ sl_add(sl, NULL);
+ serv->s_aliases = sl->sl_str;
+
+ /* NOTE: is it a dirty hack or not? */
+ free(sl);
+ return (0);
+}
+
static void
dump_servent(struct servent *result)
{
@@ -202,7 +277,7 @@
printf("%s\n", buffer);
}
-static void
+static int
servent_fill_test_data(struct servent_test_data *td)
{
struct servent *serv;
@@ -212,27 +287,8 @@
test_data_append(td, serv);
}
endservent();
-}
-
-static int
-servent_build_snapshot(char const *fname)
-{
- struct servent_test_data td;
- int rv;
-
- if (debug)
- printf("building snapshot file %s\n", fname);
- test_data_init(&td, clone_servent, free_servent);
-
- servent_fill_test_data(&td);
- rv = servent_write_snapshot(fname, &td);
-
- test_data_destroy(&td);
-
- if (debug)
- printf("%s\n", rv == 0 ? "ok" : "not ok");
- return (rv);
+ return (0);
}
static int
@@ -255,12 +311,12 @@
goto errfin;
if (debug)
- printf("ok\n");
+ printf("correct\n");
return (0);
errfin:
if (debug)
- printf("not ok\n");
+ printf("incorrect\n");
return (-1);
}
@@ -300,6 +356,9 @@
}
serv = getservbyname(serv_model->s_name, serv_model->s_proto);
+ if (servent_test_correctness(serv, NULL) != 0)
+ goto errfin;
+
if ((compare_servent(serv, serv_model, NULL) != 0) &&
(servent_check_ambiguity((struct servent_test_data *)mdata, serv)
!=0))
@@ -307,6 +366,10 @@
for (alias = serv_model->s_aliases; *alias; ++alias) {
serv = getservbyname(*alias, serv_model->s_proto);
+
+ if (servent_test_correctness(serv, NULL) != 0)
+ goto errfin;
+
if ((compare_servent(serv, serv_model, NULL) != 0) &&
(servent_check_ambiguity(
(struct servent_test_data *)mdata, serv) != 0))
@@ -335,9 +398,10 @@
}
serv = getservbyport(serv_model->s_port, serv_model->s_proto);
- if ((compare_servent(serv, serv_model, NULL) != 0) &&
+ if ((servent_test_correctness(serv, NULL) != 0) ||
+ ((compare_servent(serv, serv_model, NULL) != 0) &&
(servent_check_ambiguity((struct servent_test_data *)mdata, serv)
- != 0)) {
+ != 0))) {
if (debug)
printf("not ok\n");
return (-1);
@@ -353,258 +417,9 @@
{
/* Only correctness should be checked when doing 1-pass test for
* getservent(). Correctness is always checked in servent_1pass_test */
- return (0);
+ return (servent_test_correctness(serv, NULL));
}
-static int
-servent_1pass_test(int (*tf)(struct servent *, void *))
-{
- struct servent_test_data td;
- int rv;
-
- test_data_init(&td, clone_servent, free_servent);
- servent_fill_test_data(&td);
-
- rv = test_data_foreach(&td, servent_test_correctness, NULL);
- if (rv != 0)
- goto fin;
- rv = test_data_foreach(&td, tf, (void *)&td);
-
-fin:
- test_data_destroy(&td);
-
- return (rv);
-}
-
-static int
-servent_snapshot_1pass_test(char const *fname,
- int (*tf)(struct servent *, void *))
-{
- struct servent_test_data td;
- int rv;
-
- test_data_init(&td, clone_servent, free_servent);
- rv = servent_read_snapshot(fname, &td);
- if (rv != 0) {
- if (debug)
- printf("error reading snapshot file %s\n", fname);
- goto fin;
- }
-
- rv = test_data_foreach(&td, servent_test_correctness, NULL);
- if (rv != 0)
- goto fin;
- rv = test_data_foreach(&td, tf, (void *)&td);
-
-fin:
- test_data_destroy(&td);
-
- return (rv);
-}
-
-static int
-servent_2pass_test()
-{
- struct servent_test_data td1, td2;
- int rv;
-
- test_data_init(&td1, clone_servent, free_servent);
- test_data_init(&td2, clone_servent, free_servent);
-
- servent_fill_test_data(&td1);
- servent_fill_test_data(&td2);
-
- if (debug)
- printf("testing equality of two getservent() result-sets\n");
- rv = test_data_compare(&td1, &td2, compare_servent, NULL);
-
- test_data_destroy(&td1);
- test_data_destroy(&td2);
-
- if (debug)
- printf("%s\n", rv == 0 ? "ok" : "not ok");
- return (rv);
-}
-
-static int
-servent_snapshot_2pass_test(char const *fname)
-{
- struct servent_test_data td1, td2;
- int rv;
-
- test_data_init(&td1, clone_servent, free_servent);
- test_data_init(&td2, clone_servent, free_servent);
-
- rv = servent_read_snapshot(fname, &td1);
- if (rv != 0) {
- if (debug)
- printf("error reading snapshot file %s\n", fname);
- goto fin;
- }
- servent_fill_test_data(&td2);
-
- if (debug)
- printf("testing equality of snapshot and current result-set\n");
- rv = test_data_compare(&td1, &td2, compare_servent, NULL);
-
-fin:
- test_data_destroy(&td1);
- test_data_destroy(&td2);
-
- if (debug)
- printf("%s\n", rv == 0 ? "ok" : "not ok");
- return (rv);
-}
-
-static int
-servent_write_snapshot_func(struct servent *serv, void *mdata)
-{
- char buffer[1024];
- FILE *fo;
-
- assert(serv != NULL);
- assert(mdata != NULL);
-
- fo = (FILE *)mdata;
- sdump_servent(serv, buffer, sizeof(buffer));
- fputs(buffer, fo);
- fputc('\n', fo);
-
- return (0);
-}
-
-static int
-servent_write_snapshot(char const *fname, struct servent_test_data *td)
-{
- FILE *fo;
-
- assert(fname != NULL);
- assert(td != NULL);
-
- fo = fopen(fname, "w");
- if (fo == NULL)
- return (-1);
-
- test_data_foreach(td, servent_write_snapshot_func, (void *)fo);
- fclose(fo);
-
- return (0);
-}
-
-static int
-servent_read_snapshot_func(struct servent *serv, char *line)
-{
- StringList *sl;
- char *s, *ps, *ts;
- int i;
-
- i = 0;
- sl = NULL;
- ps = line;
- memset(serv, 0, sizeof(struct servent));
- while ( (s = strsep(&ps, " ")) != NULL) {
- switch (i) {
- case 0:
- serv->s_name = strdup(s);
- assert(serv->s_name != NULL);
- break;
-
- case 1:
- serv->s_port = htons(
- (int)strtol(s, &ts, 10));
- if (*ts != '\0') {
- free(serv->s_name);
- return (-1);
- }
- break;
-
- case 2:
- serv->s_proto = strdup(s);
- assert(serv->s_proto != NULL);
- break;
-
- default:
- if (sl == NULL) {
- if (strcmp(s, "(null)") == 0)
- return (0);
-
- sl = sl_init();
- assert(sl != NULL);
-
- if (strcmp(s, "noaliases") != 0) {
- ts = strdup(s);
- assert(ts != NULL);
- sl_add(sl, ts);
- }
- } else {
- ts = strdup(s);
- assert(ts != NULL);
- sl_add(sl, ts);
- }
- break;
- };
- ++i;
- }
-
- if (i < 3) {
- free(serv->s_name);
- free(serv->s_proto);
- memset(serv, 0, sizeof(struct servent));
- return (-1);
- }
-
- sl_add(sl, NULL);
- serv->s_aliases = sl->sl_str;
-
- /* NOTE: is it a dirty hack or not? */
- free(sl);
- return (0);
-}
-
-static int
-servent_read_snapshot(char const *fname, struct servent_test_data *td)
-{
- char buffer[1024];
- struct servent serv;
- char *s;
- FILE *fi;
- size_t len;
- int rv;
-
- assert(fname != NULL);
- assert(td != NULL);
-
- fi = fopen(fname, "r");
- if (fi == NULL)
- return (-1);
-
- memset(buffer, 0, sizeof(buffer));
- while (!feof(fi)) {
- s = fgets(buffer, sizeof(buffer), fi);
- if (s != NULL) {
- len = strlen(s);
- if (len == 0)
- continue;
- if (buffer[len - 1] == '\n')
- buffer[len -1] = '\0';
-
- rv = servent_read_snapshot_func(&serv, s);
- if (rv == 0) {
- if (debug) {
- printf("1 line read from snapshot:\n");
- dump_servent(&serv);
- }
- test_data_append(td, &serv);
- td->free_func(&serv);
- }
- }
- }
-
- fclose(fi);
-
- return (0);
-}
-
static void
usage(void)
{
@@ -617,13 +432,14 @@
int
main(int argc, char **argv)
{
+ struct servent_test_data td, td_snap, td_2pass;
char *snapshot_file;
int rv;
int c;
if (argc < 2)
usage();
-
+
snapshot_file = NULL;
while ((c = getopt(argc, argv, "npe2ds:")) != -1)
switch (c) {
@@ -649,12 +465,13 @@
usage();
}
+ test_data_init(&td, clone_servent, free_servent);
+ test_data_init(&td_snap, clone_servent, free_servent);
if (snapshot_file != NULL) {
if (access(snapshot_file, W_OK | R_OK) != 0) {
- if (errno == ENOENT) {
- rv = servent_build_snapshot(snapshot_file);
- goto fin;
- } else {
+ if (errno == ENOENT)
+ method = TEST_BUILD_SNAPSHOT;
+ else {
if (debug)
printf("can't access the file %s\n",
snapshot_file);
@@ -662,32 +479,46 @@
rv = -1;
goto fin;
}
- }
+ } else
+ snapshot_read(snapshot_file, &td_snap,
+ servent_read_snapshot_func);
}
+ servent_fill_test_data(&td);
switch (method) {
case TEST_GETSERVBYNAME:
if (snapshot_file == NULL)
- rv = servent_1pass_test(servent_test_getservbyname);
+ rv = servent_1pass_test(&td,
+ servent_test_getservbyname, (void *)&td);
else
- rv = servent_snapshot_1pass_test(snapshot_file,
- servent_test_getservbyname);
+ rv = servent_1pass_test(&td_snap,
+ servent_test_getservbyname, (void *)&td_snap);
break;
case TEST_GETSERVBYPORT:
if (snapshot_file == NULL)
- rv = servent_1pass_test(servent_test_getservbyport);
+ rv = servent_1pass_test(&td,
+ servent_test_getservbyport, (void *)&td);
else
- rv = servent_snapshot_1pass_test(snapshot_file,
- servent_test_getservbyport);
+ rv = servent_1pass_test(&td_snap,
+ servent_test_getservbyport, (void *)&td_snap);
break;
case TEST_GETSERVENT:
if (snapshot_file == NULL)
- rv = servent_1pass_test(servent_test_getservent);
+ rv = servent_1pass_test(&td, servent_test_getservent,
+ (void *)&td);
else
- rv = servent_snapshot_2pass_test(snapshot_file);
+ rv = servent_2pass_test(&td, &td_snap,
+ compare_servent, NULL);
break;
case TEST_GETSERVENT_2PASS:
- rv = servent_2pass_test();
+ test_data_init(&td_2pass, clone_servent, free_servent);
+ servent_fill_test_data(&td_2pass);
+ rv = servent_2pass_test(&td, &td_2pass,
+ compare_servent, NULL);
+ test_data_destroy(&td_2pass);
+ break;
+ case TEST_BUILD_SNAPSHOT:
+ rv = snapshot_write(snapshot_file, &td, sdump_servent);
break;
default:
rv = -1;
@@ -695,6 +526,8 @@
};
fin:
+ test_data_destroy(&td_snap);
+ test_data_destroy(&td);
free(snapshot_file);
return (rv);
}
==== //depot/projects/soc2006/nss_ldap_cached/src/tools/regression/lib/libc/nss/testutil.h#3 (text+ko) ====
@@ -42,7 +42,8 @@
}; \
\
static void test_data_init(struct ent##_test_data *, \
- void (*)(struct ent *, struct ent const *), void (*)(struct ent *));\
+ void (*)(struct ent *, struct ent const *), \
+ void (*freef)(struct ent *)); \
static void test_data_destroy(struct ent##_test_data *); \
\
static void test_data_append(struct ent##_test_data *, struct ent *data);\
@@ -50,7 +51,8 @@
void *), void *); \
static int test_data_compare(struct ent##_test_data *, \
struct ent##_test_data *, int (*)(struct ent *, struct ent *, \
- void *), void *);
+ void *), void *); \
+static void test_data_clear(struct ent##_test_data *);
#define IMPLEMENT_TEST_DATA(ent) \
static void \
@@ -71,16 +73,7 @@
static void \
test_data_destroy(struct ent##_test_data *td) \
{ \
- struct ent##_entry *e; \
- assert(td != NULL); \
- \
- while (!STAILQ_EMPTY(&td->snapshot_data)) { \
- e = STAILQ_FIRST(&td->snapshot_data); \
- STAILQ_REMOVE_HEAD(&td->snapshot_data, entries); \
- \
- td->free_func(&e->data); \
- free(e); \
- } \
+ test_data_clear(td); \
} \
\
static void \
@@ -148,4 +141,146 @@
} while (rv == 0); \
\
return (rv); \
+} \
+ \
+static void \
+test_data_clear(struct ent##_test_data *td) \
+{ \
+ struct ent##_entry *e; \
+ assert(td != NULL); \
+ \
+ while (!STAILQ_EMPTY(&td->snapshot_data)) { \
+ e = STAILQ_FIRST(&td->snapshot_data); \
+ STAILQ_REMOVE_HEAD(&td->snapshot_data, entries); \
+ \
+ td->free_func(&e->data); \
+ free(e); \
+ } \
+}
+
+ \
+#define DECLARE_FILE_SNAPSHOT(ent) \
+struct ent##_snp_param { \
+ FILE *fp; \
+ void (*sdump_func)(struct ent *, char *, size_t); \
+}; \
+ \
+static int snapshot_write_func(struct ent *, void *); \
+static int snapshot_write(char const *, struct ent##_test_data *, \
+ void (*)(struct ent *, char *, size_t)); \
+static int snapshot_read(char const *, struct ent##_test_data *, \
+ int (*)(struct ent *, char *));
+
+#define IMPLEMENT_FILE_SNAPSHOT(ent) \
+static int \
+snapshot_write_func(struct ent *data, void *mdata) \
+{ \
+ char buffer[1024]; \
+ struct ent##_snp_param *param; \
+ \
+ assert(data != NULL); \
+ assert(mdata != NULL); \
+ \
+ param = (struct ent##_snp_param *)mdata; \
+ param->sdump_func(data, buffer, sizeof(buffer)); \
+ fputs(buffer, param->fp); \
+ fputc('\n', param->fp); \
+ \
+ return (0); \
+} \
+ \
+static int \
+snapshot_write(char const *fname, struct ent##_test_data *td, \
+ void (*sdump_func)(struct ent *, char *, size_t)) \
+{ \
+ struct ent##_snp_param param; \
+ \
+ assert(fname != NULL); \
+ assert(td != NULL); \
+ \
+ param.fp = fopen(fname, "w"); \
+ if (param.fp == NULL) \
+ return (-1); \
+ \
+ param.sdump_func = sdump_func; \
+ test_data_foreach(td, snapshot_write_func, ¶m); \
+ fclose(param.fp); \
+ \
+ return (0); \
+} \
+ \
+static int \
+snapshot_read(char const *fname, struct ent##_test_data *td, \
+ int (*read_func)(struct ent *, char *)) \
+{ \
+ char buffer[1024]; \
+ struct ent data; \
+ char *s; \
+ FILE *fi; \
+ size_t len; \
+ int rv; \
+ \
+ assert(fname != NULL); \
+ assert(td != NULL); \
+ \
+ fi = fopen(fname, "r"); \
+ if (fi == NULL) \
+ return (-1); \
+ \
+ memset(buffer, 0, sizeof(buffer)); \
+ while (!feof(fi)) { \
+ s = fgets(buffer, sizeof(buffer), fi); \
+ if (s != NULL) { \
+ len = strlen(s); \
+ if (len == 0) \
+ continue; \
+ if (buffer[len - 1] == '\n') \
+ buffer[len -1] = '\0'; \
+ \
+ rv = read_func(&data, s); \
+ if (rv == 0) { \
+ test_data_append(td, &data); \
+ td->free_func(&data); \
+ } \
+ } \
+ } \
+ \
+ fclose(fi); \
+ \
+ return (0); \
+}
+
+#define DECLARE_1PASS_TEST(ent) \
+static int ent##_1pass_test(struct ent##_test_data *, \
+ int (*)(struct servent *, void *), \
+ void *);
+
+#define IMPLEMENT_1PASS_TEST(ent) \
+static int \
+ent##_1pass_test(struct ent##_test_data *td, \
+ int (*tf)(struct servent *, void *), \
+ void *mdata) \
+{ \
+ int rv; \
+ rv = test_data_foreach(td, tf, mdata); \
+ \
+ return (rv); \
+}
+
+#define DECLARE_2PASS_TEST(ent) \
+static int ent##_2pass_test(struct ent##_test_data *, \
+ struct ent##_test_data *, \
+ int (*)(struct ent *, struct ent *, void *), void *);
+
+#define IMPLEMENT_2PASS_TEST(ent) \
+static int \
+ent##_2pass_test(struct ent##_test_data *td1, \
+ struct ent##_test_data *td2, \
+ int (*cmp_func)(struct ent *, struct ent *, void *), \
+ void *cmp_mdata) \
+{ \
+ int rv; \
+ \
+ rv = test_data_compare(td1, td2, cmp_func, cmp_mdata); \
+ return (rv); \
}
More information about the p4-projects
mailing list