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>
38 #include <stringlist.h>
51 static enum test_methods method
= TEST_BUILD_SNAPSHOT
;
53 DECLARE_TEST_DATA(rpcent
)
54 DECLARE_TEST_FILE_SNAPSHOT(rpcent
)
55 DECLARE_1PASS_TEST(rpcent
)
56 DECLARE_2PASS_TEST(rpcent
)
58 static void clone_rpcent(struct rpcent
*, struct rpcent
const *);
59 static int compare_rpcent(struct rpcent
*, struct rpcent
*, void *);
60 static void dump_rpcent(struct rpcent
*);
61 static void free_rpcent(struct rpcent
*);
63 static void sdump_rpcent(struct rpcent
*, char *, size_t);
64 static int rpcent_read_snapshot_func(struct rpcent
*, char *);
66 static int rpcent_check_ambiguity(struct rpcent_test_data
*,
68 static int rpcent_fill_test_data(struct rpcent_test_data
*);
69 static int rpcent_test_correctness(struct rpcent
*, void *);
70 static int rpcent_test_getrpcbyname(struct rpcent
*, void *);
71 static int rpcent_test_getrpcbynumber(struct rpcent
*, void *);
72 static int rpcent_test_getrpcent(struct rpcent
*, void *);
74 static void usage(void) __attribute__((__noreturn__
));
76 IMPLEMENT_TEST_DATA(rpcent
)
77 IMPLEMENT_TEST_FILE_SNAPSHOT(rpcent
)
78 IMPLEMENT_1PASS_TEST(rpcent
)
79 IMPLEMENT_2PASS_TEST(rpcent
)
82 clone_rpcent(struct rpcent
*dest
, struct rpcent
const *src
)
90 memset(dest
, 0, sizeof(struct rpcent
));
92 if (src
->r_name
!= NULL
) {
93 dest
->r_name
= strdup(src
->r_name
);
94 assert(dest
->r_name
!= NULL
);
97 dest
->r_number
= src
->r_number
;
99 if (src
->r_aliases
!= NULL
) {
101 for (cp
= src
->r_aliases
; *cp
; ++cp
)
104 dest
->r_aliases
= (char **)malloc((aliases_num
+1) * (sizeof(char *)));
105 assert(dest
->r_aliases
!= NULL
);
106 memset(dest
->r_aliases
, 0, (aliases_num
+1) * (sizeof(char *)));
108 for (cp
= src
->r_aliases
; *cp
; ++cp
) {
109 dest
->r_aliases
[cp
- src
->r_aliases
] = strdup(*cp
);
110 assert(dest
->r_aliases
[cp
- src
->r_aliases
] != NULL
);
116 free_rpcent(struct rpcent
*rpc
)
124 for (cp
= rpc
->r_aliases
; *cp
; ++cp
)
126 free(rpc
->r_aliases
);
130 compare_rpcent(struct rpcent
*rpc1
, struct rpcent
*rpc2
, void *mdata
)
137 if ((rpc1
== NULL
) || (rpc2
== NULL
))
140 if ((strcmp(rpc1
->r_name
, rpc2
->r_name
) != 0) ||
141 (rpc1
->r_number
!= rpc2
->r_number
))
144 c1
= rpc1
->r_aliases
;
145 c2
= rpc2
->r_aliases
;
147 if ((rpc1
->r_aliases
== NULL
) || (rpc2
->r_aliases
== NULL
))
150 for (;*c1
&& *c2
; ++c1
, ++c2
)
151 if (strcmp(*c1
, *c2
) != 0)
154 if ((*c1
!= '\0') || (*c2
!= '\0'))
160 if ((debug
) && (mdata
== NULL
)) {
161 printf("following structures are not equal:\n");
170 sdump_rpcent(struct rpcent
*rpc
, char *buffer
, size_t buflen
)
175 written
= snprintf(buffer
, buflen
, "%s %d",
176 rpc
->r_name
, rpc
->r_number
);
178 if (written
> buflen
)
182 if (rpc
->r_aliases
!= NULL
) {
183 if (*(rpc
->r_aliases
) != '\0') {
184 for (cp
= rpc
->r_aliases
; *cp
; ++cp
) {
185 written
= snprintf(buffer
, buflen
, " %s",*cp
);
187 if (written
> buflen
)
195 snprintf(buffer
, buflen
, " noaliases");
197 snprintf(buffer
, buflen
, " (null)");
201 rpcent_read_snapshot_func(struct rpcent
*rpc
, char *line
)
208 printf("1 line read from snapshot:\n%s\n", line
);
213 memset(rpc
, 0, sizeof(struct rpcent
));
214 while ( (s
= strsep(&ps
, " ")) != NULL
) {
217 rpc
->r_name
= strdup(s
);
218 assert(rpc
->r_name
!= NULL
);
222 rpc
->r_number
= (int)strtol(s
, &ts
, 10);
231 if (strcmp(s
, "(null)") == 0)
237 if (strcmp(s
, "noaliases") != 0) {
254 memset(rpc
, 0, sizeof(struct rpcent
));
259 rpc
->r_aliases
= sl
->sl_str
;
261 /* NOTE: is it a dirty hack or not? */
267 dump_rpcent(struct rpcent
*result
)
269 if (result
!= NULL
) {
271 sdump_rpcent(result
, buffer
, sizeof(buffer
));
272 printf("%s\n", buffer
);
278 rpcent_fill_test_data(struct rpcent_test_data
*td
)
283 while ((rpc
= getrpcent()) != NULL
) {
284 if (rpcent_test_correctness(rpc
, NULL
) == 0)
285 TEST_DATA_APPEND(rpcent
, td
, rpc
);
295 rpcent_test_correctness(struct rpcent
*rpc
, void *mdata
)
298 printf("testing correctness with the following data:\n");
305 if (rpc
->r_name
== NULL
)
308 if (rpc
->r_number
< 0)
311 if (rpc
->r_aliases
== NULL
)
320 printf("incorrect\n");
325 /* rpcent_check_ambiguity() is needed when one port+rpc is associated with
326 * more than one peice (these cases are usually marked as PROBLEM in
327 * /etc/peices. This functions is needed also when one peice+rpc is
328 * associated with several ports. We have to check all the rpcent structures
329 * to make sure that rpc really exists and correct */
331 rpcent_check_ambiguity(struct rpcent_test_data
*td
, struct rpcent
*rpc
)
334 return (TEST_DATA_FIND(rpcent
, td
, rpc
, compare_rpcent
,
335 NULL
) != NULL
? 0 : -1);
339 rpcent_test_getrpcbyname(struct rpcent
*rpc_model
, void *mdata
)
345 printf("testing getrpcbyname() with the following data:\n");
346 dump_rpcent(rpc_model
);
349 rpc
= getrpcbyname(rpc_model
->r_name
);
350 if (rpcent_test_correctness(rpc
, NULL
) != 0)
353 if ((compare_rpcent(rpc
, rpc_model
, NULL
) != 0) &&
354 (rpcent_check_ambiguity((struct rpcent_test_data
*)mdata
, rpc
)
358 for (alias
= rpc_model
->r_aliases
; *alias
; ++alias
) {
359 rpc
= getrpcbyname(*alias
);
361 if (rpcent_test_correctness(rpc
, NULL
) != 0)
364 if ((compare_rpcent(rpc
, rpc_model
, NULL
) != 0) &&
365 (rpcent_check_ambiguity(
366 (struct rpcent_test_data
*)mdata
, rpc
) != 0))
382 rpcent_test_getrpcbynumber(struct rpcent
*rpc_model
, void *mdata
)
387 printf("testing getrpcbyport() with the following data...\n");
388 dump_rpcent(rpc_model
);
391 rpc
= getrpcbynumber(rpc_model
->r_number
);
392 if ((rpcent_test_correctness(rpc
, NULL
) != 0) ||
393 ((compare_rpcent(rpc
, rpc_model
, NULL
) != 0) &&
394 (rpcent_check_ambiguity((struct rpcent_test_data
*)mdata
, rpc
)
407 rpcent_test_getrpcent(struct rpcent
*rpc
, void *mdata
)
409 /* Only correctness can be checked when doing 1-pass test for
411 return (rpcent_test_correctness(rpc
, NULL
));
417 (void)fprintf(stderr
,
418 "Usage: %s -nve2 [-d] [-s <file>]\n",
424 main(int argc
, char **argv
)
426 struct rpcent_test_data td
, td_snap
, td_2pass
;
434 snapshot_file
= NULL
;
435 while ((c
= getopt(argc
, argv
, "nve2ds:")) != -1)
441 method
= TEST_GETRPCBYNAME
;
444 method
= TEST_GETRPCBYNUMBER
;
447 method
= TEST_GETRPCENT
;
450 method
= TEST_GETRPCENT_2PASS
;
453 snapshot_file
= strdup(optarg
);
459 TEST_DATA_INIT(rpcent
, &td
, clone_rpcent
, free_rpcent
);
460 TEST_DATA_INIT(rpcent
, &td_snap
, clone_rpcent
, free_rpcent
);
461 if (snapshot_file
!= NULL
) {
462 if (access(snapshot_file
, W_OK
| R_OK
) != 0) {
464 method
= TEST_BUILD_SNAPSHOT
;
467 printf("can't access the file %s\n",
474 if (method
== TEST_BUILD_SNAPSHOT
) {
479 TEST_SNAPSHOT_FILE_READ(rpcent
, snapshot_file
,
480 &td_snap
, rpcent_read_snapshot_func
);
484 rv
= rpcent_fill_test_data(&td
);
488 case TEST_GETRPCBYNAME
:
489 if (snapshot_file
== NULL
)
490 rv
= DO_1PASS_TEST(rpcent
, &td
,
491 rpcent_test_getrpcbyname
, (void *)&td
);
493 rv
= DO_1PASS_TEST(rpcent
, &td_snap
,
494 rpcent_test_getrpcbyname
, (void *)&td_snap
);
496 case TEST_GETRPCBYNUMBER
:
497 if (snapshot_file
== NULL
)
498 rv
= DO_1PASS_TEST(rpcent
, &td
,
499 rpcent_test_getrpcbynumber
, (void *)&td
);
501 rv
= DO_1PASS_TEST(rpcent
, &td_snap
,
502 rpcent_test_getrpcbynumber
, (void *)&td_snap
);
505 if (snapshot_file
== NULL
)
506 rv
= DO_1PASS_TEST(rpcent
, &td
, rpcent_test_getrpcent
,
509 rv
= DO_2PASS_TEST(rpcent
, &td
, &td_snap
,
510 compare_rpcent
, NULL
);
512 case TEST_GETRPCENT_2PASS
:
513 TEST_DATA_INIT(rpcent
, &td_2pass
, clone_rpcent
, free_rpcent
);
514 rv
= rpcent_fill_test_data(&td_2pass
);
516 rv
= DO_2PASS_TEST(rpcent
, &td
, &td_2pass
,
517 compare_rpcent
, NULL
);
518 TEST_DATA_DESTROY(rpcent
, &td_2pass
);
520 case TEST_BUILD_SNAPSHOT
:
521 if (snapshot_file
!= NULL
)
522 rv
= TEST_SNAPSHOT_FILE_WRITE(rpcent
, snapshot_file
, &td
,
531 TEST_DATA_DESTROY(rpcent
, &td_snap
);
532 TEST_DATA_DESTROY(rpcent
, &td
);