Deleted Added
full compact
getrpc_test.c (291363) getrpc_test.c (292323)
1/*-
2 * Copyright (c) 2006 Michael Bushkov <bushman@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 12 unchanged lines hidden (view full) ---

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
24 * SUCH DAMAGE.
25 *
26 */
27
28#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2006 Michael Bushkov <bushman@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 12 unchanged lines hidden (view full) ---

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
24 * SUCH DAMAGE.
25 *
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: head/tools/regression/lib/libc/nss/test-getrpc.c 291363 2015-11-26 08:58:13Z ngie $");
29__FBSDID("$FreeBSD: head/lib/libc/tests/nss/getrpc_test.c 292323 2015-12-16 08:09:03Z ngie $");
30
31#include <arpa/inet.h>
32#include <rpc/rpc.h>
30
31#include <arpa/inet.h>
32#include <rpc/rpc.h>
33#include <assert.h>
34#include <errno.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <stringlist.h>
39#include <unistd.h>
33#include <errno.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#include <stringlist.h>
38#include <unistd.h>
39
40#include <atf-c.h>
41
40#include "testutil.h"
41
42enum test_methods {
43 TEST_GETRPCENT,
44 TEST_GETRPCBYNAME,
45 TEST_GETRPCBYNUMBER,
46 TEST_GETRPCENT_2PASS,
47 TEST_BUILD_SNAPSHOT
48};
49
42#include "testutil.h"
43
44enum test_methods {
45 TEST_GETRPCENT,
46 TEST_GETRPCBYNAME,
47 TEST_GETRPCBYNUMBER,
48 TEST_GETRPCENT_2PASS,
49 TEST_BUILD_SNAPSHOT
50};
51
50static int debug = 0;
51static enum test_methods method = TEST_BUILD_SNAPSHOT;
52
53DECLARE_TEST_DATA(rpcent)
54DECLARE_TEST_FILE_SNAPSHOT(rpcent)
55DECLARE_1PASS_TEST(rpcent)
56DECLARE_2PASS_TEST(rpcent)
57
58static void clone_rpcent(struct rpcent *, struct rpcent const *);
59static int compare_rpcent(struct rpcent *, struct rpcent *, void *);
60static void dump_rpcent(struct rpcent *);

--- 15 unchanged lines hidden (view full) ---

76IMPLEMENT_TEST_DATA(rpcent)
77IMPLEMENT_TEST_FILE_SNAPSHOT(rpcent)
78IMPLEMENT_1PASS_TEST(rpcent)
79IMPLEMENT_2PASS_TEST(rpcent)
80
81static void
82clone_rpcent(struct rpcent *dest, struct rpcent const *src)
83{
52DECLARE_TEST_DATA(rpcent)
53DECLARE_TEST_FILE_SNAPSHOT(rpcent)
54DECLARE_1PASS_TEST(rpcent)
55DECLARE_2PASS_TEST(rpcent)
56
57static void clone_rpcent(struct rpcent *, struct rpcent const *);
58static int compare_rpcent(struct rpcent *, struct rpcent *, void *);
59static void dump_rpcent(struct rpcent *);

--- 15 unchanged lines hidden (view full) ---

75IMPLEMENT_TEST_DATA(rpcent)
76IMPLEMENT_TEST_FILE_SNAPSHOT(rpcent)
77IMPLEMENT_1PASS_TEST(rpcent)
78IMPLEMENT_2PASS_TEST(rpcent)
79
80static void
81clone_rpcent(struct rpcent *dest, struct rpcent const *src)
82{
84 assert(dest != NULL);
85 assert(src != NULL);
83 ATF_REQUIRE(dest != NULL);
84 ATF_REQUIRE(src != NULL);
86
87 char **cp;
88 int aliases_num;
89
90 memset(dest, 0, sizeof(struct rpcent));
91
92 if (src->r_name != NULL) {
93 dest->r_name = strdup(src->r_name);
85
86 char **cp;
87 int aliases_num;
88
89 memset(dest, 0, sizeof(struct rpcent));
90
91 if (src->r_name != NULL) {
92 dest->r_name = strdup(src->r_name);
94 assert(dest->r_name != NULL);
93 ATF_REQUIRE(dest->r_name != NULL);
95 }
96
97 dest->r_number = src->r_number;
98
99 if (src->r_aliases != NULL) {
100 aliases_num = 0;
101 for (cp = src->r_aliases; *cp; ++cp)
102 ++aliases_num;
103
94 }
95
96 dest->r_number = src->r_number;
97
98 if (src->r_aliases != NULL) {
99 aliases_num = 0;
100 for (cp = src->r_aliases; *cp; ++cp)
101 ++aliases_num;
102
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 *)));
103 dest->r_aliases = calloc(1, (aliases_num + 1) * sizeof(char *));
104 ATF_REQUIRE(dest->r_aliases != NULL);
107
108 for (cp = src->r_aliases; *cp; ++cp) {
109 dest->r_aliases[cp - src->r_aliases] = strdup(*cp);
105
106 for (cp = src->r_aliases; *cp; ++cp) {
107 dest->r_aliases[cp - src->r_aliases] = strdup(*cp);
110 assert(dest->r_aliases[cp - src->r_aliases] != NULL);
108 ATF_REQUIRE(dest->r_aliases[cp - src->r_aliases] != NULL);
111 }
112 }
113}
114
115static void
116free_rpcent(struct rpcent *rpc)
117{
118 char **cp;
119
109 }
110 }
111}
112
113static void
114free_rpcent(struct rpcent *rpc)
115{
116 char **cp;
117
120 assert(rpc != NULL);
118 ATF_REQUIRE(rpc != NULL);
121
122 free(rpc->r_name);
123
124 for (cp = rpc->r_aliases; *cp; ++cp)
125 free(*cp);
126 free(rpc->r_aliases);
127}
128

--- 23 unchanged lines hidden (view full) ---

152 goto errfin;
153
154 if ((*c1 != '\0') || (*c2 != '\0'))
155 goto errfin;
156
157 return 0;
158
159errfin:
119
120 free(rpc->r_name);
121
122 for (cp = rpc->r_aliases; *cp; ++cp)
123 free(*cp);
124 free(rpc->r_aliases);
125}
126

--- 23 unchanged lines hidden (view full) ---

150 goto errfin;
151
152 if ((*c1 != '\0') || (*c2 != '\0'))
153 goto errfin;
154
155 return 0;
156
157errfin:
160 if ((debug) && (mdata == NULL)) {
158 if (mdata == NULL) {
161 printf("following structures are not equal:\n");
162 dump_rpcent(rpc1);
163 dump_rpcent(rpc2);
164 }
165
166 return (-1);
167}
168

--- 30 unchanged lines hidden (view full) ---

199
200static int
201rpcent_read_snapshot_func(struct rpcent *rpc, char *line)
202{
203 StringList *sl;
204 char *s, *ps, *ts;
205 int i;
206
159 printf("following structures are not equal:\n");
160 dump_rpcent(rpc1);
161 dump_rpcent(rpc2);
162 }
163
164 return (-1);
165}
166

--- 30 unchanged lines hidden (view full) ---

197
198static int
199rpcent_read_snapshot_func(struct rpcent *rpc, char *line)
200{
201 StringList *sl;
202 char *s, *ps, *ts;
203 int i;
204
207 if (debug)
208 printf("1 line read from snapshot:\n%s\n", line);
205 printf("1 line read from snapshot:\n%s\n", line);
209
210 i = 0;
211 sl = NULL;
212 ps = line;
213 memset(rpc, 0, sizeof(struct rpcent));
206
207 i = 0;
208 sl = NULL;
209 ps = line;
210 memset(rpc, 0, sizeof(struct rpcent));
214 while ( (s = strsep(&ps, " ")) != NULL) {
211 while ((s = strsep(&ps, " ")) != NULL) {
215 switch (i) {
212 switch (i) {
216 case 0:
213 case 0:
217 rpc->r_name = strdup(s);
214 rpc->r_name = strdup(s);
218 assert(rpc->r_name != NULL);
215 ATF_REQUIRE(rpc->r_name != NULL);
219 break;
220
216 break;
217
221 case 1:
222 rpc->r_number = (int)strtol(s, &ts, 10);
223 if (*ts != '\0') {
224 free(rpc->r_name);
225 return (-1);
226 }
218 case 1:
219 rpc->r_number = (int)strtol(s, &ts, 10);
220 if (*ts != '\0') {
221 free(rpc->r_name);
222 return (-1);
223 }
227 break;
228
224 break;
225
229 default:
230 if (sl == NULL) {
231 if (strcmp(s, "(null)") == 0)
232 return (0);
226 default:
227 if (sl == NULL) {
228 if (strcmp(s, "(null)") == 0)
229 return (0);
233
230
234 sl = sl_init();
235 assert(sl != NULL);
231 sl = sl_init();
232 ATF_REQUIRE(sl != NULL);
236
233
237 if (strcmp(s, "noaliases") != 0) {
238 ts = strdup(s);
239 assert(ts != NULL);
240 sl_add(sl, ts);
241 }
242 } else {
234 if (strcmp(s, "noaliases") != 0) {
243 ts = strdup(s);
235 ts = strdup(s);
244 assert(ts != NULL);
236 ATF_REQUIRE(ts != NULL);
245 sl_add(sl, ts);
246 }
237 sl_add(sl, ts);
238 }
239 } else {
240 ts = strdup(s);
241 ATF_REQUIRE(ts != NULL);
242 sl_add(sl, ts);
243 }
247 break;
244 break;
248 };
249 ++i;
245 }
246 i++;
250 }
251
252 if (i < 3) {
253 free(rpc->r_name);
254 memset(rpc, 0, sizeof(struct rpcent));
255 return (-1);
256 }
257

--- 31 unchanged lines hidden (view full) ---

289 endrpcent();
290
291 return (0);
292}
293
294static int
295rpcent_test_correctness(struct rpcent *rpc, void *mdata)
296{
247 }
248
249 if (i < 3) {
250 free(rpc->r_name);
251 memset(rpc, 0, sizeof(struct rpcent));
252 return (-1);
253 }
254

--- 31 unchanged lines hidden (view full) ---

286 endrpcent();
287
288 return (0);
289}
290
291static int
292rpcent_test_correctness(struct rpcent *rpc, void *mdata)
293{
297 if (debug) {
298 printf("testing correctness with the following data:\n");
299 dump_rpcent(rpc);
300 }
301
294
295 printf("testing correctness with the following data:\n");
296 dump_rpcent(rpc);
297
302 if (rpc == NULL)
303 goto errfin;
304
305 if (rpc->r_name == NULL)
306 goto errfin;
307
308 if (rpc->r_number < 0)
309 goto errfin;
310
311 if (rpc->r_aliases == NULL)
312 goto errfin;
313
298 if (rpc == NULL)
299 goto errfin;
300
301 if (rpc->r_name == NULL)
302 goto errfin;
303
304 if (rpc->r_number < 0)
305 goto errfin;
306
307 if (rpc->r_aliases == NULL)
308 goto errfin;
309
314 if (debug)
315 printf("correct\n");
310 printf("correct\n");
316
317 return (0);
318errfin:
311
312 return (0);
313errfin:
319 if (debug)
320 printf("incorrect\n");
314 printf("incorrect\n");
321
322 return (-1);
323}
324
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

--- 7 unchanged lines hidden (view full) ---

336}
337
338static int
339rpcent_test_getrpcbyname(struct rpcent *rpc_model, void *mdata)
340{
341 char **alias;
342 struct rpcent *rpc;
343
315
316 return (-1);
317}
318
319/* rpcent_check_ambiguity() is needed when one port+rpc is associated with
320 * more than one peice (these cases are usually marked as PROBLEM in
321 * /etc/peices. This functions is needed also when one peice+rpc is
322 * associated with several ports. We have to check all the rpcent structures

--- 7 unchanged lines hidden (view full) ---

330}
331
332static int
333rpcent_test_getrpcbyname(struct rpcent *rpc_model, void *mdata)
334{
335 char **alias;
336 struct rpcent *rpc;
337
344 if (debug) {
345 printf("testing getrpcbyname() with the following data:\n");
346 dump_rpcent(rpc_model);
347 }
338 printf("testing getrpcbyname() with the following data:\n");
339 dump_rpcent(rpc_model);
348
349 rpc = getrpcbyname(rpc_model->r_name);
350 if (rpcent_test_correctness(rpc, NULL) != 0)
351 goto errfin;
352
353 if ((compare_rpcent(rpc, rpc_model, NULL) != 0) &&
354 (rpcent_check_ambiguity((struct rpcent_test_data *)mdata, rpc)
355 !=0))

--- 6 unchanged lines hidden (view full) ---

362 goto errfin;
363
364 if ((compare_rpcent(rpc, rpc_model, NULL) != 0) &&
365 (rpcent_check_ambiguity(
366 (struct rpcent_test_data *)mdata, rpc) != 0))
367 goto errfin;
368 }
369
340
341 rpc = getrpcbyname(rpc_model->r_name);
342 if (rpcent_test_correctness(rpc, NULL) != 0)
343 goto errfin;
344
345 if ((compare_rpcent(rpc, rpc_model, NULL) != 0) &&
346 (rpcent_check_ambiguity((struct rpcent_test_data *)mdata, rpc)
347 !=0))

--- 6 unchanged lines hidden (view full) ---

354 goto errfin;
355
356 if ((compare_rpcent(rpc, rpc_model, NULL) != 0) &&
357 (rpcent_check_ambiguity(
358 (struct rpcent_test_data *)mdata, rpc) != 0))
359 goto errfin;
360 }
361
370 if (debug)
371 printf("ok\n");
362 printf("ok\n");
372 return (0);
373
374errfin:
363 return (0);
364
365errfin:
375 if (debug)
376 printf("not ok\n");
366 printf("not ok\n");
377
378 return (-1);
379}
380
381static int
382rpcent_test_getrpcbynumber(struct rpcent *rpc_model, void *mdata)
383{
384 struct rpcent *rpc;
385
367
368 return (-1);
369}
370
371static int
372rpcent_test_getrpcbynumber(struct rpcent *rpc_model, void *mdata)
373{
374 struct rpcent *rpc;
375
386 if (debug) {
387 printf("testing getrpcbyport() with the following data...\n");
388 dump_rpcent(rpc_model);
389 }
376 printf("testing getrpcbyport() with the following data...\n");
377 dump_rpcent(rpc_model);
390
391 rpc = getrpcbynumber(rpc_model->r_number);
378
379 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)
395 != 0))) {
396 if (debug)
380 if (rpcent_test_correctness(rpc, NULL) != 0 ||
381 (compare_rpcent(rpc, rpc_model, NULL) != 0 &&
382 rpcent_check_ambiguity((struct rpcent_test_data *)mdata, rpc)
383 != 0)) {
397 printf("not ok\n");
384 printf("not ok\n");
398 return (-1);
385 return (-1);
399 } else {
386 } else {
400 if (debug)
401 printf("ok\n");
387 printf("ok\n");
402 return (0);
388 return (0);
403 }
404}
405
406static int
407rpcent_test_getrpcent(struct rpcent *rpc, void *mdata)
408{
389 }
390}
391
392static int
393rpcent_test_getrpcent(struct rpcent *rpc, void *mdata)
394{
409 /* Only correctness can be checked when doing 1-pass test for
410 * getrpcent(). */
395
396 /*
397 * Only correctness can be checked when doing 1-pass test for
398 * getrpcent().
399 */
411 return (rpcent_test_correctness(rpc, NULL));
412}
413
400 return (rpcent_test_correctness(rpc, NULL));
401}
402
414static void
415usage(void)
416{
417 (void)fprintf(stderr,
418 "Usage: %s -nve2 [-d] [-s <file>]\n",
419 getprogname());
420 exit(1);
421}
422
423int
403int
424main(int argc, char **argv)
404run_tests(const char *snapshot_file, enum test_methods method)
425{
426 struct rpcent_test_data td, td_snap, td_2pass;
405{
406 struct rpcent_test_data td, td_snap, td_2pass;
427 char *snapshot_file;
428 int rv;
407 int rv;
429 int c;
430
408
431 if (argc < 2)
432 usage();
433
434 snapshot_file = NULL;
435 while ((c = getopt(argc, argv, "nve2ds:")) != -1)
436 switch (c) {
437 case 'd':
438 debug++;
439 break;
440 case 'n':
441 method = TEST_GETRPCBYNAME;
442 break;
443 case 'v':
444 method = TEST_GETRPCBYNUMBER;
445 break;
446 case 'e':
447 method = TEST_GETRPCENT;
448 break;
449 case '2':
450 method = TEST_GETRPCENT_2PASS;
451 break;
452 case 's':
453 snapshot_file = strdup(optarg);
454 break;
455 default:
456 usage();
457 }
458
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) {
463 if (errno == ENOENT)
464 method = TEST_BUILD_SNAPSHOT;
465 else {
409 TEST_DATA_INIT(rpcent, &td, clone_rpcent, free_rpcent);
410 TEST_DATA_INIT(rpcent, &td_snap, clone_rpcent, free_rpcent);
411 if (snapshot_file != NULL) {
412 if (access(snapshot_file, W_OK | R_OK) != 0) {
413 if (errno == ENOENT)
414 method = TEST_BUILD_SNAPSHOT;
415 else {
466 if (debug)
467 printf("can't access the file %s\n",
468 snapshot_file);
416 printf("can't access the file %s\n",
417 snapshot_file);
469
470 rv = -1;
471 goto fin;
472 }
473 } else {
474 if (method == TEST_BUILD_SNAPSHOT) {
475 rv = 0;
476 goto fin;

--- 43 unchanged lines hidden (view full) ---

520 case TEST_BUILD_SNAPSHOT:
521 if (snapshot_file != NULL)
522 rv = TEST_SNAPSHOT_FILE_WRITE(rpcent, snapshot_file, &td,
523 sdump_rpcent);
524 break;
525 default:
526 rv = 0;
527 break;
418
419 rv = -1;
420 goto fin;
421 }
422 } else {
423 if (method == TEST_BUILD_SNAPSHOT) {
424 rv = 0;
425 goto fin;

--- 43 unchanged lines hidden (view full) ---

469 case TEST_BUILD_SNAPSHOT:
470 if (snapshot_file != NULL)
471 rv = TEST_SNAPSHOT_FILE_WRITE(rpcent, snapshot_file, &td,
472 sdump_rpcent);
473 break;
474 default:
475 rv = 0;
476 break;
528 };
477 }
529
530fin:
531 TEST_DATA_DESTROY(rpcent, &td_snap);
532 TEST_DATA_DESTROY(rpcent, &td);
478
479fin:
480 TEST_DATA_DESTROY(rpcent, &td_snap);
481 TEST_DATA_DESTROY(rpcent, &td);
533 free(snapshot_file);
482
534 return (rv);
535}
483 return (rv);
484}
485
486#define SNAPSHOT_FILE "snapshot_rpc"
487
488ATF_TC_WITHOUT_HEAD(build_snapshot);
489ATF_TC_BODY(build_snapshot, tc)
490{
491
492 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
493}
494
495ATF_TC_WITHOUT_HEAD(getrpcbyname);
496ATF_TC_BODY(getrpcbyname, tc)
497{
498
499 ATF_REQUIRE(run_tests(NULL, TEST_GETRPCBYNAME) == 0);
500}
501
502ATF_TC_WITHOUT_HEAD(getrpcbyname_with_snapshot);
503ATF_TC_BODY(getrpcbyname_with_snapshot, tc)
504{
505
506 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
507 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETRPCBYNAME) == 0);
508}
509
510ATF_TC_WITHOUT_HEAD(getrpcbynumber);
511ATF_TC_BODY(getrpcbynumber, tc)
512{
513
514 ATF_REQUIRE(run_tests(NULL, TEST_GETRPCBYNUMBER) == 0);
515}
516
517ATF_TC_WITHOUT_HEAD(getrpcbynumber_with_snapshot);
518ATF_TC_BODY(getrpcbynumber_with_snapshot, tc)
519{
520
521 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
522 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETRPCBYNUMBER) == 0);
523}
524
525ATF_TC_WITHOUT_HEAD(getrpcbyent);
526ATF_TC_BODY(getrpcbyent, tc)
527{
528
529 ATF_REQUIRE(run_tests(NULL, TEST_GETRPCENT) == 0);
530}
531
532ATF_TC_WITHOUT_HEAD(getrpcbyent_with_snapshot);
533ATF_TC_BODY(getrpcbyent_with_snapshot, tc)
534{
535
536 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
537 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETRPCENT) == 0);
538}
539
540ATF_TC_WITHOUT_HEAD(getrpcbyent_with_two_pass);
541ATF_TC_BODY(getrpcbyent_with_two_pass, tc)
542{
543
544 ATF_REQUIRE(run_tests(NULL, TEST_GETRPCENT_2PASS) == 0);
545}
546
547ATF_TP_ADD_TCS(tp)
548{
549
550 ATF_TP_ADD_TC(tp, build_snapshot);
551 ATF_TP_ADD_TC(tp, getrpcbyname);
552 ATF_TP_ADD_TC(tp, getrpcbyname_with_snapshot);
553 ATF_TP_ADD_TC(tp, getrpcbynumber);
554 ATF_TP_ADD_TC(tp, getrpcbynumber_with_snapshot);
555 ATF_TP_ADD_TC(tp, getrpcbyent);
556 ATF_TP_ADD_TC(tp, getrpcbyent_with_snapshot);
557 ATF_TP_ADD_TC(tp, getrpcbyent_with_two_pass);
558
559 return (atf_no_error());
560}