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
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.
13 *
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
24 * SUCH DAMAGE.
25 *
26 */
27
28 #include <sys/cdefs.h>
29 #include <arpa/inet.h>
30 #include <assert.h>
31 #include <errno.h>
32 #include <netdb.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stringlist.h>
37 #include <unistd.h>
38
39 #include <atf-c.h>
40
41 #include "testutil.h"
42
43 enum test_methods {
44 TEST_GETPROTOENT,
45 TEST_GETPROTOBYNAME,
46 TEST_GETPROTOBYNUMBER,
47 TEST_GETPROTOENT_2PASS,
48 TEST_BUILD_SNAPSHOT
49 };
50
51 DECLARE_TEST_DATA(protoent)
52 DECLARE_TEST_FILE_SNAPSHOT(protoent)
53 DECLARE_1PASS_TEST(protoent)
54 DECLARE_2PASS_TEST(protoent)
55
56 static void clone_protoent(struct protoent *, struct protoent const *);
57 static int compare_protoent(struct protoent *, struct protoent *, void *);
58 static void dump_protoent(struct protoent *);
59 static void free_protoent(struct protoent *);
60
61 static void sdump_protoent(struct protoent *, char *, size_t);
62 static int protoent_read_snapshot_func(struct protoent *, char *);
63
64 static int protoent_check_ambiguity(struct protoent_test_data *,
65 struct protoent *);
66 static int protoent_fill_test_data(struct protoent_test_data *);
67 static int protoent_test_correctness(struct protoent *, void *);
68 static int protoent_test_getprotobyname(struct protoent *, void *);
69 static int protoent_test_getprotobynumber(struct protoent *, void *);
70 static int protoent_test_getprotoent(struct protoent *, void *);
71
72 IMPLEMENT_TEST_DATA(protoent)
IMPLEMENT_TEST_FILE_SNAPSHOT(protoent)73 IMPLEMENT_TEST_FILE_SNAPSHOT(protoent)
74 IMPLEMENT_1PASS_TEST(protoent)
75 IMPLEMENT_2PASS_TEST(protoent)
76
77 static void
78 clone_protoent(struct protoent *dest, struct protoent const *src)
79 {
80 assert(dest != NULL);
81 assert(src != NULL);
82
83 char **cp;
84 int aliases_num;
85
86 memset(dest, 0, sizeof(struct protoent));
87
88 if (src->p_name != NULL) {
89 dest->p_name = strdup(src->p_name);
90 assert(dest->p_name != NULL);
91 }
92
93 dest->p_proto = src->p_proto;
94
95 if (src->p_aliases != NULL) {
96 aliases_num = 0;
97 for (cp = src->p_aliases; *cp; ++cp)
98 ++aliases_num;
99
100 dest->p_aliases = calloc(aliases_num + 1, sizeof(char *));
101 assert(dest->p_aliases != NULL);
102
103 for (cp = src->p_aliases; *cp; ++cp) {
104 dest->p_aliases[cp - src->p_aliases] = strdup(*cp);
105 assert(dest->p_aliases[cp - src->p_aliases] != NULL);
106 }
107 }
108 }
109
110 static void
free_protoent(struct protoent * pe)111 free_protoent(struct protoent *pe)
112 {
113 char **cp;
114
115 assert(pe != NULL);
116
117 free(pe->p_name);
118
119 for (cp = pe->p_aliases; *cp; ++cp)
120 free(*cp);
121 free(pe->p_aliases);
122 }
123
124 static int
compare_protoent(struct protoent * pe1,struct protoent * pe2,void * mdata)125 compare_protoent(struct protoent *pe1, struct protoent *pe2, void *mdata)
126 {
127 char **c1, **c2;
128
129 if (pe1 == pe2)
130 return 0;
131
132 if ((pe1 == NULL) || (pe2 == NULL))
133 goto errfin;
134
135 if ((strcmp(pe1->p_name, pe2->p_name) != 0) ||
136 (pe1->p_proto != pe2->p_proto))
137 goto errfin;
138
139 c1 = pe1->p_aliases;
140 c2 = pe2->p_aliases;
141
142 if ((pe1->p_aliases == NULL) || (pe2->p_aliases == NULL))
143 goto errfin;
144
145 for (;*c1 && *c2; ++c1, ++c2)
146 if (strcmp(*c1, *c2) != 0)
147 goto errfin;
148
149 if ((*c1 != NULL) || (*c2 != NULL))
150 goto errfin;
151
152 return 0;
153
154 errfin:
155 if (mdata == NULL) {
156 printf("following structures are not equal:\n");
157 dump_protoent(pe1);
158 dump_protoent(pe2);
159 }
160
161 return (-1);
162 }
163
164 static void
sdump_protoent(struct protoent * pe,char * buffer,size_t buflen)165 sdump_protoent(struct protoent *pe, char *buffer, size_t buflen)
166 {
167 char **cp;
168 int written;
169
170 written = snprintf(buffer, buflen, "%s %d",
171 pe->p_name, pe->p_proto);
172 buffer += written;
173 if (written > (int)buflen)
174 return;
175 buflen -= written;
176
177 if (pe->p_aliases != NULL) {
178 if (*(pe->p_aliases) != NULL) {
179 for (cp = pe->p_aliases; *cp; ++cp) {
180 written = snprintf(buffer, buflen, " %s", *cp);
181 buffer += written;
182 if (written > (int)buflen)
183 return;
184 buflen -= written;
185
186 if (buflen == 0)
187 return;
188 }
189 } else
190 snprintf(buffer, buflen, " noaliases");
191 } else
192 snprintf(buffer, buflen, " (null)");
193 }
194
195 static int
protoent_read_snapshot_func(struct protoent * pe,char * line)196 protoent_read_snapshot_func(struct protoent *pe, char *line)
197 {
198 StringList *sl;
199 char *s, *ps, *ts;
200 int i;
201
202 printf("1 line read from snapshot:\n%s\n", line);
203
204 i = 0;
205 sl = NULL;
206 ps = line;
207 memset(pe, 0, sizeof(struct protoent));
208 while ( (s = strsep(&ps, " ")) != NULL) {
209 switch (i) {
210 case 0:
211 pe->p_name = strdup(s);
212 assert(pe->p_name != NULL);
213 break;
214
215 case 1:
216 pe->p_proto = (int)strtol(s, &ts, 10);
217 if (*ts != '\0') {
218 free(pe->p_name);
219 return (-1);
220 }
221 break;
222
223 default:
224 if (sl == NULL) {
225 if (strcmp(s, "(null)") == 0)
226 return (0);
227
228 sl = sl_init();
229 assert(sl != NULL);
230
231 if (strcmp(s, "noaliases") != 0) {
232 ts = strdup(s);
233 assert(ts != NULL);
234 sl_add(sl, ts);
235 }
236 } else {
237 ts = strdup(s);
238 assert(ts != NULL);
239 sl_add(sl, ts);
240 }
241 break;
242 }
243 ++i;
244 }
245
246 if (i < 3) {
247 free(pe->p_name);
248 memset(pe, 0, sizeof(struct protoent));
249 return (-1);
250 }
251
252 sl_add(sl, NULL);
253 pe->p_aliases = sl->sl_str;
254
255 /* NOTE: is it a dirty hack or not? */
256 free(sl);
257 return (0);
258 }
259
260 static void
dump_protoent(struct protoent * result)261 dump_protoent(struct protoent *result)
262 {
263 if (result != NULL) {
264 char buffer[1024];
265 sdump_protoent(result, buffer, sizeof(buffer));
266 printf("%s\n", buffer);
267 } else
268 printf("(null)\n");
269 }
270
271 static int
protoent_fill_test_data(struct protoent_test_data * td)272 protoent_fill_test_data(struct protoent_test_data *td)
273 {
274 struct protoent *pe;
275
276 setprotoent(1);
277 while ((pe = getprotoent()) != NULL) {
278 if (protoent_test_correctness(pe, NULL) == 0)
279 TEST_DATA_APPEND(protoent, td, pe);
280 else
281 return (-1);
282 }
283 endprotoent();
284
285 return (0);
286 }
287
288 static int
protoent_test_correctness(struct protoent * pe,void * mdata __unused)289 protoent_test_correctness(struct protoent *pe, void *mdata __unused)
290 {
291 printf("testing correctness with the following data:\n");
292 dump_protoent(pe);
293
294 if (pe == NULL)
295 goto errfin;
296
297 if (pe->p_name == NULL)
298 goto errfin;
299
300 if (pe->p_proto < 0)
301 goto errfin;
302
303 if (pe->p_aliases == NULL)
304 goto errfin;
305
306 printf("correct\n");
307
308 return (0);
309 errfin:
310 printf("incorrect\n");
311
312 return (-1);
313 }
314
315 /* protoent_check_ambiguity() is needed when one port+proto is associated with
316 * more than one piece (these cases are usually marked as PROBLEM in
317 * /etc/peices. This functions is needed also when one piece+proto is
318 * associated with several ports. We have to check all the protoent structures
319 * to make sure that pe really exists and correct */
320 static int
protoent_check_ambiguity(struct protoent_test_data * td,struct protoent * pe)321 protoent_check_ambiguity(struct protoent_test_data *td, struct protoent *pe)
322 {
323
324 return (TEST_DATA_FIND(protoent, td, pe, compare_protoent,
325 NULL) != NULL ? 0 : -1);
326 }
327
328 static int
protoent_test_getprotobyname(struct protoent * pe_model,void * mdata)329 protoent_test_getprotobyname(struct protoent *pe_model, void *mdata)
330 {
331 char **alias;
332 struct protoent *pe;
333
334 printf("testing getprotobyname() with the following data:\n");
335 dump_protoent(pe_model);
336
337 pe = getprotobyname(pe_model->p_name);
338 if (protoent_test_correctness(pe, NULL) != 0)
339 goto errfin;
340
341 if ((compare_protoent(pe, pe_model, NULL) != 0) &&
342 (protoent_check_ambiguity((struct protoent_test_data *)mdata, pe)
343 !=0))
344 goto errfin;
345
346 for (alias = pe_model->p_aliases; *alias; ++alias) {
347 pe = getprotobyname(*alias);
348
349 if (protoent_test_correctness(pe, NULL) != 0)
350 goto errfin;
351
352 if ((compare_protoent(pe, pe_model, NULL) != 0) &&
353 (protoent_check_ambiguity(
354 (struct protoent_test_data *)mdata, pe) != 0))
355 goto errfin;
356 }
357
358 printf("ok\n");
359 return (0);
360
361 errfin:
362 printf("not ok\n");
363
364 return (-1);
365 }
366
367 static int
protoent_test_getprotobynumber(struct protoent * pe_model,void * mdata)368 protoent_test_getprotobynumber(struct protoent *pe_model, void *mdata)
369 {
370 struct protoent *pe;
371
372 printf("testing getprotobyport() with the following data...\n");
373 dump_protoent(pe_model);
374
375 pe = getprotobynumber(pe_model->p_proto);
376 if ((protoent_test_correctness(pe, NULL) != 0) ||
377 ((compare_protoent(pe, pe_model, NULL) != 0) &&
378 (protoent_check_ambiguity((struct protoent_test_data *)mdata, pe)
379 != 0))) {
380 printf("not ok\n");
381 return (-1);
382 } else {
383 printf("ok\n");
384 return (0);
385 }
386 }
387
388 static int
protoent_test_getprotoent(struct protoent * pe,void * mdata __unused)389 protoent_test_getprotoent(struct protoent *pe, void *mdata __unused)
390 {
391 /* Only correctness can be checked when doing 1-pass test for
392 * getprotoent(). */
393 return (protoent_test_correctness(pe, NULL));
394 }
395
396 static int
run_tests(const char * snapshot_file,enum test_methods method)397 run_tests(const char *snapshot_file, enum test_methods method)
398 {
399 struct protoent_test_data td, td_snap, td_2pass;
400 int rv;
401
402 TEST_DATA_INIT(protoent, &td, clone_protoent, free_protoent);
403 TEST_DATA_INIT(protoent, &td_snap, clone_protoent, free_protoent);
404 if (snapshot_file != NULL) {
405 if (access(snapshot_file, W_OK | R_OK) != 0) {
406 if (errno == ENOENT)
407 method = TEST_BUILD_SNAPSHOT;
408 else {
409 printf("can't access the file %s\n",
410 snapshot_file);
411
412 rv = -1;
413 goto fin;
414 }
415 } else {
416 if (method == TEST_BUILD_SNAPSHOT) {
417 rv = 0;
418 goto fin;
419 }
420
421 TEST_SNAPSHOT_FILE_READ(protoent, snapshot_file,
422 &td_snap, protoent_read_snapshot_func);
423 }
424 }
425
426 rv = protoent_fill_test_data(&td);
427 if (rv == -1)
428 return (-1);
429 switch (method) {
430 case TEST_GETPROTOBYNAME:
431 if (snapshot_file == NULL)
432 rv = DO_1PASS_TEST(protoent, &td,
433 protoent_test_getprotobyname, (void *)&td);
434 else
435 rv = DO_1PASS_TEST(protoent, &td_snap,
436 protoent_test_getprotobyname, (void *)&td_snap);
437 break;
438 case TEST_GETPROTOBYNUMBER:
439 if (snapshot_file == NULL)
440 rv = DO_1PASS_TEST(protoent, &td,
441 protoent_test_getprotobynumber, (void *)&td);
442 else
443 rv = DO_1PASS_TEST(protoent, &td_snap,
444 protoent_test_getprotobynumber, (void *)&td_snap);
445 break;
446 case TEST_GETPROTOENT:
447 if (snapshot_file == NULL)
448 rv = DO_1PASS_TEST(protoent, &td,
449 protoent_test_getprotoent, (void *)&td);
450 else
451 rv = DO_2PASS_TEST(protoent, &td, &td_snap,
452 compare_protoent, NULL);
453 break;
454 case TEST_GETPROTOENT_2PASS:
455 TEST_DATA_INIT(protoent, &td_2pass, clone_protoent,
456 free_protoent);
457 rv = protoent_fill_test_data(&td_2pass);
458 if (rv != -1)
459 rv = DO_2PASS_TEST(protoent, &td, &td_2pass,
460 compare_protoent, NULL);
461 TEST_DATA_DESTROY(protoent, &td_2pass);
462 break;
463 case TEST_BUILD_SNAPSHOT:
464 if (snapshot_file != NULL)
465 rv = TEST_SNAPSHOT_FILE_WRITE(protoent, snapshot_file,
466 &td, sdump_protoent);
467 break;
468 default:
469 rv = 0;
470 break;
471 }
472
473 fin:
474 TEST_DATA_DESTROY(protoent, &td_snap);
475 TEST_DATA_DESTROY(protoent, &td);
476
477 return (rv);
478 }
479
480 #define SNAPSHOT_FILE "snapshot_proto"
481
482 ATF_TC_WITHOUT_HEAD(build_snapshot);
ATF_TC_BODY(build_snapshot,tc)483 ATF_TC_BODY(build_snapshot, tc)
484 {
485
486 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
487 }
488
489 ATF_TC_WITHOUT_HEAD(getprotoent);
ATF_TC_BODY(getprotoent,tc)490 ATF_TC_BODY(getprotoent, tc)
491 {
492
493 ATF_REQUIRE(run_tests(NULL, TEST_GETPROTOENT) == 0);
494 }
495
496 ATF_TC_WITHOUT_HEAD(getprotoent_with_snapshot);
ATF_TC_BODY(getprotoent_with_snapshot,tc)497 ATF_TC_BODY(getprotoent_with_snapshot, tc)
498 {
499
500 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
501 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETPROTOENT) == 0);
502 }
503
504 ATF_TC_WITHOUT_HEAD(getprotoent_with_two_pass);
ATF_TC_BODY(getprotoent_with_two_pass,tc)505 ATF_TC_BODY(getprotoent_with_two_pass, tc)
506 {
507
508 ATF_REQUIRE(run_tests(NULL, TEST_GETPROTOENT_2PASS) == 0);
509 }
510
511 ATF_TC_WITHOUT_HEAD(getprotobyname);
ATF_TC_BODY(getprotobyname,tc)512 ATF_TC_BODY(getprotobyname, tc)
513 {
514
515 ATF_REQUIRE(run_tests(NULL, TEST_GETPROTOBYNAME) == 0);
516 }
517
518 ATF_TC_WITHOUT_HEAD(getprotobyname_with_snapshot);
ATF_TC_BODY(getprotobyname_with_snapshot,tc)519 ATF_TC_BODY(getprotobyname_with_snapshot, tc)
520 {
521
522 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
523 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETPROTOBYNAME) == 0);
524 }
525
526 ATF_TC_WITHOUT_HEAD(getprotobynumber);
ATF_TC_BODY(getprotobynumber,tc)527 ATF_TC_BODY(getprotobynumber, tc)
528 {
529
530 ATF_REQUIRE(run_tests(NULL, TEST_GETPROTOBYNUMBER) == 0);
531 }
532
533 ATF_TC_WITHOUT_HEAD(getprotobynumber_with_snapshot);
ATF_TC_BODY(getprotobynumber_with_snapshot,tc)534 ATF_TC_BODY(getprotobynumber_with_snapshot, tc)
535 {
536
537 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
538 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETPROTOBYNUMBER) == 0);
539 }
540
ATF_TP_ADD_TCS(tp)541 ATF_TP_ADD_TCS(tp)
542 {
543
544 ATF_TP_ADD_TC(tp, build_snapshot);
545 ATF_TP_ADD_TC(tp, getprotoent);
546 ATF_TP_ADD_TC(tp, getprotoent_with_snapshot);
547 ATF_TP_ADD_TC(tp, getprotoent_with_two_pass);
548 ATF_TP_ADD_TC(tp, getprotobyname);
549 ATF_TP_ADD_TC(tp, getprotobyname_with_snapshot);
550 ATF_TP_ADD_TC(tp, getprotobynumber);
551 ATF_TP_ADD_TC(tp, getprotobynumber_with_snapshot);
552
553 return (atf_no_error());
554 }
555