]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/atf/atf-c/atf-c.3
ssh: update to OpenSSH v8.8p1
[FreeBSD/FreeBSD.git] / contrib / atf / atf-c / atf-c.3
1 .\" Copyright (c) 2008 The NetBSD Foundation, Inc.
2 .\" All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\"
13 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14 .\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15 .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 .\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 .\" IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18 .\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 .\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 .\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 .\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 .Dd February 23, 2021
26 .Dt ATF-C 3
27 .Os
28 .Sh NAME
29 .Nm atf-c ,
30 .Nm ATF_CHECK ,
31 .Nm ATF_CHECK_MSG ,
32 .Nm ATF_CHECK_EQ ,
33 .Nm ATF_CHECK_EQ_MSG ,
34 .Nm ATF_CHECK_MATCH ,
35 .Nm ATF_CHECK_MATCH_MSG ,
36 .Nm ATF_CHECK_STREQ ,
37 .Nm ATF_CHECK_STREQ_MSG ,
38 .Nm ATF_CHECK_INTEQ ,
39 .Nm ATF_CHECK_INTEQ_MSG ,
40 .Nm ATF_CHECK_ERRNO ,
41 .Nm ATF_REQUIRE ,
42 .Nm ATF_REQUIRE_MSG ,
43 .Nm ATF_REQUIRE_EQ ,
44 .Nm ATF_REQUIRE_EQ_MSG ,
45 .Nm ATF_REQUIRE_MATCH ,
46 .Nm ATF_REQUIRE_MATCH_MSG ,
47 .Nm ATF_REQUIRE_STREQ ,
48 .Nm ATF_REQUIRE_STREQ_MSG ,
49 .Nm ATF_REQUIRE_INTEQ ,
50 .Nm ATF_REQUIRE_INTEQ_MSG ,
51 .Nm ATF_REQUIRE_ERRNO ,
52 .Nm ATF_TC ,
53 .Nm ATF_TC_BODY ,
54 .Nm ATF_TC_BODY_NAME ,
55 .Nm ATF_TC_CLEANUP ,
56 .Nm ATF_TC_CLEANUP_NAME ,
57 .Nm ATF_TC_HEAD ,
58 .Nm ATF_TC_HEAD_NAME ,
59 .Nm ATF_TC_NAME ,
60 .Nm ATF_TC_WITH_CLEANUP ,
61 .Nm ATF_TC_WITHOUT_HEAD ,
62 .Nm ATF_TP_ADD_TC ,
63 .Nm ATF_TP_ADD_TCS ,
64 .Nm atf_tc_get_config_var ,
65 .Nm atf_tc_get_config_var_wd ,
66 .Nm atf_tc_get_config_var_as_bool ,
67 .Nm atf_tc_get_config_var_as_bool_wd ,
68 .Nm atf_tc_get_config_var_as_long ,
69 .Nm atf_tc_get_config_var_as_long_wd ,
70 .Nm atf_no_error ,
71 .Nm atf_tc_expect_death ,
72 .Nm atf_tc_expect_exit ,
73 .Nm atf_tc_expect_fail ,
74 .Nm atf_tc_expect_pass ,
75 .Nm atf_tc_expect_signal ,
76 .Nm atf_tc_expect_timeout ,
77 .Nm atf_tc_fail ,
78 .Nm atf_tc_fail_nonfatal ,
79 .Nm atf_tc_pass ,
80 .Nm atf_tc_skip ,
81 .Nm atf_utils_cat_file ,
82 .Nm atf_utils_compare_file ,
83 .Nm atf_utils_copy_file ,
84 .Nm atf_utils_create_file ,
85 .Nm atf_utils_file_exists ,
86 .Nm atf_utils_fork ,
87 .Nm atf_utils_free_charpp ,
88 .Nm atf_utils_grep_file ,
89 .Nm atf_utils_grep_string ,
90 .Nm atf_utils_readline ,
91 .Nm atf_utils_redirect ,
92 .Nm atf_utils_wait
93 .Nd C API to write ATF-based test programs
94 .Sh SYNOPSIS
95 .In atf-c.h
96 .\" NO_CHECK_STYLE_BEGIN
97 .Fn ATF_CHECK "expression"
98 .Fn ATF_CHECK_MSG "expression" "fail_msg_fmt" ...
99 .Fn ATF_CHECK_EQ "expected_expression" "actual_expression"
100 .Fn ATF_CHECK_EQ_MSG "expected_expression" "actual_expression" "fail_msg_fmt" ...
101 .Fn ATF_CHECK_MATCH "regexp" "string"
102 .Fn ATF_CHECK_MATCH_MSG "regexp" "string" "fail_msg_fmt" ...
103 .Fn ATF_CHECK_STREQ "expected_string" "actual_string"
104 .Fn ATF_CHECK_STREQ_MSG "expected_string" "actual_string" "fail_msg_fmt" ...
105 .Fn ATF_CHECK_INTEQ "expected_int" "actual_int"
106 .Fn ATF_CHECK_INTEQ_MSG "expected_int" "actual_int" "fail_msg_fmt" ...
107 .Fn ATF_CHECK_ERRNO "expected_errno" "bool_expression"
108 .Fn ATF_REQUIRE "expression"
109 .Fn ATF_REQUIRE_MSG "expression" "fail_msg_fmt" ...
110 .Fn ATF_REQUIRE_EQ "expected_expression" "actual_expression"
111 .Fn ATF_REQUIRE_EQ_MSG "expected_expression" "actual_expression" "fail_msg_fmt" ...
112 .Fn ATF_REQUIRE_MATCH "regexp" "string"
113 .Fn ATF_REQUIRE_MATCH_MSG "regexp" "string" "fail_msg_fmt" ...
114 .Fn ATF_REQUIRE_STREQ "expected_string" "actual_string"
115 .Fn ATF_REQUIRE_STREQ_MSG "expected_string" "actual_string" "fail_msg_fmt" ...
116 .Fn ATF_REQUIRE_INTEQ "expected_int" "actual_int"
117 .Fn ATF_REQUIRE_INTEQ_MSG "expected_int" "actual_int" "fail_msg_fmt" ...
118 .Fn ATF_REQUIRE_ERRNO "expected_errno" "bool_expression"
119 .\" NO_CHECK_STYLE_END
120 .Fn ATF_TC "name"
121 .Fn ATF_TC_BODY "name" "tc"
122 .Fn ATF_TC_BODY_NAME "name"
123 .Fn ATF_TC_CLEANUP "name" "tc"
124 .Fn ATF_TC_CLEANUP_NAME "name"
125 .Fn ATF_TC_HEAD "name" "tc"
126 .Fn ATF_TC_HEAD_NAME "name"
127 .Fn ATF_TC_NAME "name"
128 .Fn ATF_TC_WITH_CLEANUP "name"
129 .Fn ATF_TC_WITHOUT_HEAD "name"
130 .Fn ATF_TP_ADD_TC "tp_name" "tc_name"
131 .Fn ATF_TP_ADD_TCS "tp_name"
132 .Fn atf_tc_get_config_var "tc" "varname"
133 .Fn atf_tc_get_config_var_wd "tc" "variable_name" "default_value"
134 .Fn atf_tc_get_config_var_as_bool "tc" "variable_name"
135 .Fn atf_tc_get_config_var_as_bool_wd "tc" "variable_name" "default_value"
136 .Fn atf_tc_get_config_var_as_long "tc" "variable_name"
137 .Fn atf_tc_get_config_var_as_long_wd "tc" "variable_name" "default_value"
138 .Fn atf_no_error
139 .Fn atf_tc_expect_death "reason" "..."
140 .Fn atf_tc_expect_exit "exitcode" "reason" "..."
141 .Fn atf_tc_expect_fail "reason" "..."
142 .Fn atf_tc_expect_pass
143 .Fn atf_tc_expect_signal "signo" "reason" "..."
144 .Fn atf_tc_expect_timeout "reason" "..."
145 .Fn atf_tc_fail "reason"
146 .Fn atf_tc_fail_nonfatal "reason"
147 .Fn atf_tc_pass
148 .Fn atf_tc_skip "reason"
149 .Ft void
150 .Fo atf_utils_cat_file
151 .Fa "const char *file"
152 .Fa "const char *prefix"
153 .Fc
154 .Ft bool
155 .Fo atf_utils_compare_file
156 .Fa "const char *file"
157 .Fa "const char *contents"
158 .Fc
159 .Ft void
160 .Fo atf_utils_copy_file
161 .Fa "const char *source"
162 .Fa "const char *destination"
163 .Fc
164 .Ft void
165 .Fo atf_utils_create_file
166 .Fa "const char *file"
167 .Fa "const char *contents"
168 .Fa "..."
169 .Fc
170 .Ft void
171 .Fo atf_utils_file_exists
172 .Fa "const char *file"
173 .Fc
174 .Ft pid_t
175 .Fo atf_utils_fork
176 .Fa "void"
177 .Fc
178 .Ft void
179 .Fo atf_utils_free_charpp
180 .Fa "char **argv"
181 .Fc
182 .Ft bool
183 .Fo atf_utils_grep_file
184 .Fa "const char *regexp"
185 .Fa "const char *file"
186 .Fa "..."
187 .Fc
188 .Ft bool
189 .Fo atf_utils_grep_string
190 .Fa "const char *regexp"
191 .Fa "const char *str"
192 .Fa "..."
193 .Fc
194 .Ft char *
195 .Fo atf_utils_readline
196 .Fa "int fd"
197 .Fc
198 .Ft void
199 .Fo atf_utils_redirect
200 .Fa "const int fd"
201 .Fa "const char *file"
202 .Fc
203 .Ft void
204 .Fo atf_utils_wait
205 .Fa "const pid_t pid"
206 .Fa "const int expected_exit_status"
207 .Fa "const char *expected_stdout"
208 .Fa "const char *expected_stderr"
209 .Fc
210 .Sh DESCRIPTION
211 ATF provides a C programming interface to implement test programs.
212 C-based test programs follow this template:
213 .Bd -literal -offset indent
214 \&... C-specific includes go here ...
215
216 #include <atf-c.h>
217
218 ATF_TC(tc1);
219 ATF_TC_HEAD(tc1, tc)
220 {
221     ... first test case's header ...
222 }
223 ATF_TC_BODY(tc1, tc)
224 {
225     ... first test case's body ...
226 }
227
228 ATF_TC_WITH_CLEANUP(tc2);
229 ATF_TC_HEAD(tc2, tc)
230 {
231     ... second test case's header ...
232 }
233 ATF_TC_BODY(tc2, tc)
234 {
235     ... second test case's body ...
236 }
237 ATF_TC_CLEANUP(tc2, tc)
238 {
239     ... second test case's cleanup ...
240 }
241
242 ATF_TC_WITHOUT_HEAD(tc3);
243 ATF_TC_BODY(tc3, tc)
244 {
245     ... third test case's body ...
246 }
247
248 \&... additional test cases ...
249
250 ATF_TP_ADD_TCS(tp)
251 {
252     ATF_TP_ADD_TC(tcs, tc1);
253     ATF_TP_ADD_TC(tcs, tc2);
254     ATF_TP_ADD_TC(tcs, tc3);
255     ... add additional test cases ...
256
257     return atf_no_error();
258 }
259 .Ed
260 .Ss Definition of test cases
261 Test cases have an identifier and are composed of three different parts:
262 the header, the body and an optional cleanup routine, all of which are
263 described in
264 .Xr atf-test-case 4 .
265 To define test cases, one can use the
266 .Fn ATF_TC ,
267 .Fn ATF_TC_WITH_CLEANUP
268 or the
269 .Fn ATF_TC_WITHOUT_HEAD
270 macros, which take a single parameter specifying the test case's name.
271 .Fn ATF_TC ,
272 requires to define a head and a body for the test case,
273 .Fn ATF_TC_WITH_CLEANUP
274 requires to define a head, a body and a cleanup for the test case and
275 .Fn ATF_TC_WITHOUT_HEAD
276 requires only a body for the test case.
277 It is important to note that these
278 .Em do not
279 set the test case up for execution when the program is run.
280 In order to do so, a later registration is needed with the
281 .Fn ATF_TP_ADD_TC
282 macro detailed in
283 .Sx Program initialization .
284 .Pp
285 Later on, one must define the three parts of the body by means of three
286 functions.
287 Their headers are given by the
288 .Fn ATF_TC_HEAD ,
289 .Fn ATF_TC_BODY
290 and
291 .Fn ATF_TC_CLEANUP
292 macros, all of which take the test case name provided to the
293 .Fn ATF_TC
294 .Fn ATF_TC_WITH_CLEANUP ,
295 or
296 .Fn ATF_TC_WITHOUT_HEAD
297 macros and the name of the variable that will hold a pointer to the
298 test case data.
299 Following each of these, a block of code is expected, surrounded by the
300 opening and closing brackets.
301 .Ss Program initialization
302 The library provides a way to easily define the test program's
303 .Fn main
304 function.
305 You should never define one on your own, but rely on the
306 library to do it for you.
307 This is done by using the
308 .Fn ATF_TP_ADD_TCS
309 macro, which is passed the name of the object that will hold the test
310 cases, i.e., the test program instance.
311 This name can be whatever you want as long as it is a valid variable
312 identifier.
313 .Pp
314 After the macro, you are supposed to provide the body of a function, which
315 should only use the
316 .Fn ATF_TP_ADD_TC
317 macro to register the test cases the test program will execute and return
318 a success error code.
319 The first parameter of this macro matches the name you provided in the
320 former call.
321 The success status can be returned using the
322 .Fn atf_no_error
323 function.
324 .Ss Header definitions
325 The test case's header can define the meta-data by using the
326 .Fn atf_tc_set_md_var
327 method, which takes three parameters: the first one points to the test
328 case data, the second one specifies the meta-data variable to be set
329 and the third one specifies its value.
330 Both of them are strings.
331 .Ss Configuration variables
332 The test case has read-only access to the current configuration variables
333 by means of the
334 .Ft bool
335 .Fn atf_tc_has_config_var ,
336 .Ft const char *
337 .Fn atf_tc_get_config_var ,
338 .Ft const char *
339 .Fn atf_tc_get_config_var_wd ,
340 .Ft bool
341 .Fn atf_tc_get_config_var_as_bool ,
342 .Ft bool
343 .Fn atf_tc_get_config_var_as_bool_wd ,
344 .Ft long
345 .Fn atf_tc_get_config_var_as_long ,
346 and the
347 .Ft long
348 .Fn atf_tc_get_config_var_as_long_wd
349 functions, which can be called in any of the three parts of a test case.
350 .Pp
351 The
352 .Sq _wd
353 variants take a default value for the variable which is returned if the
354 variable is not defined.
355 The other functions without the
356 .Sq _wd
357 suffix
358 .Em require
359 the variable to be defined.
360 .Ss Access to the source directory
361 It is possible to get the path to the test case's source directory from any
362 of its three components by querying the
363 .Sq srcdir
364 configuration variable.
365 .Ss Requiring programs
366 Aside from the
367 .Va require.progs
368 meta-data variable available in the header only, one can also check for
369 additional programs in the test case's body by using the
370 .Fn atf_tc_require_prog
371 function, which takes the base name or full path of a single binary.
372 Relative paths are forbidden.
373 If it is not found, the test case will be automatically skipped.
374 .Ss Test case finalization
375 The test case finalizes either when the body reaches its end, at which
376 point the test is assumed to have
377 .Em passed ,
378 unless any non-fatal errors were raised using
379 .Fn atf_tc_fail_nonfatal ,
380 or at any explicit call to
381 .Fn atf_tc_pass ,
382 .Fn atf_tc_fail
383 or
384 .Fn atf_tc_skip .
385 These three functions terminate the execution of the test case immediately.
386 The cleanup routine will be processed afterwards in a completely automated
387 way, regardless of the test case's termination reason.
388 .Pp
389 .Fn atf_tc_pass
390 does not take any parameters.
391 .Fn atf_tc_fail ,
392 .Fn atf_tc_fail_nonfatal
393 and
394 .Fn atf_tc_skip
395 take a format string and a variable list of parameters, which describe, in
396 a user-friendly manner, why the test case failed or was skipped,
397 respectively.
398 It is very important to provide a clear error message in both cases so that
399 the user can quickly know why the test did not pass.
400 .Ss Expectations
401 Everything explained in the previous section changes when the test case
402 expectations are redefined by the programmer.
403 .Pp
404 Each test case has an internal state called
405 .Sq expect
406 that describes what the test case expectations are at any point in time.
407 The value of this property can change during execution by any of:
408 .Bl -tag -width indent
409 .It Fn atf_tc_expect_death "reason" "..."
410 Expects the test case to exit prematurely regardless of the nature of the
411 exit.
412 .It Fn atf_tc_expect_exit "exitcode" "reason" "..."
413 Expects the test case to exit cleanly.
414 If
415 .Va exitcode
416 is not
417 .Sq -1 ,
418 the runtime engine will validate that the exit code of the test case
419 matches the one provided in this call.
420 Otherwise, the exact value will be ignored.
421 .It Fn atf_tc_expect_fail "reason" "..."
422 Any failure (be it fatal or non-fatal) raised in this mode is recorded.
423 However, such failures do not report the test case as failed; instead, the
424 test case finalizes cleanly and is reported as
425 .Sq expected failure ;
426 this report includes the provided
427 .Fa reason
428 as part of it.
429 If no error is raised while running in this mode, then the test case is
430 reported as
431 .Sq failed .
432 .Pp
433 This mode is useful to reproduce actual known bugs in tests.
434 Whenever the developer fixes the bug later on, the test case will start
435 reporting a failure, signaling the developer that the test case must be
436 adjusted to the new conditions.
437 In this situation, it is useful, for example, to set
438 .Fa reason
439 as the bug number for tracking purposes.
440 .It Fn atf_tc_expect_pass
441 This is the normal mode of execution.
442 In this mode, any failure is reported as such to the user and the test case
443 is marked as
444 .Sq failed .
445 .It Fn atf_tc_expect_signal "signo" "reason" "..."
446 Expects the test case to terminate due to the reception of a signal.
447 If
448 .Va signo
449 is not
450 .Sq -1 ,
451 the runtime engine will validate that the signal that terminated the test
452 case matches the one provided in this call.
453 Otherwise, the exact value will be ignored.
454 .It Fn atf_tc_expect_timeout "reason" "..."
455 Expects the test case to execute for longer than its timeout.
456 .El
457 .Ss Helper macros for common checks
458 The library provides several macros that are very handy in multiple
459 situations.
460 These basically check some condition after executing a given statement or
461 processing a given expression and, if the condition is not met, they
462 report the test case as failed.
463 .Pp
464 The
465 .Sq REQUIRE
466 variant of the macros immediately abort the test case as soon as an error
467 condition is detected by calling the
468 .Fn atf_tc_fail
469 function.
470 Use this variant whenever it makes no sense to continue the execution of a
471 test case when the checked condition is not met.
472 The
473 .Sq CHECK
474 variant, on the other hand, reports a failure as soon as it is encountered
475 using the
476 .Fn atf_tc_fail_nonfatal
477 function, but the execution of the test case continues as if nothing had
478 happened.
479 Use this variant whenever the checked condition is important as a result of
480 the test case, but there are other conditions that can be subsequently
481 checked on the same run without aborting.
482 .Pp
483 Additionally, the
484 .Sq MSG
485 variants take an extra set of parameters to explicitly specify the failure
486 message.
487 This failure message is formatted according to the
488 .Xr printf 3
489 formatters.
490 .Pp
491 .Fn ATF_CHECK ,
492 .Fn ATF_CHECK_MSG ,
493 .Fn ATF_REQUIRE
494 and
495 .Fn ATF_REQUIRE_MSG
496 take an expression and fail if the expression evaluates to false.
497 .Pp
498 .Fn ATF_CHECK_EQ ,
499 .Fn ATF_CHECK_EQ_MSG ,
500 .Fn ATF_REQUIRE_EQ
501 and
502 .Fn ATF_REQUIRE_EQ_MSG
503 take two expressions and fail if the two evaluated values are not equal.
504 The common style is to put the expected value in the first parameter and the
505 observed value in the second parameter.
506 .Pp
507 .Fn ATF_CHECK_MATCH ,
508 .Fn ATF_CHECK_MATCH_MSG ,
509 .Fn ATF_REQUIRE_MATCH
510 and
511 .Fn ATF_REQUIRE_MATCH_MSG
512 take a regular expression and a string and fail if the regular expression does
513 not match the given string.
514 Note that the regular expression is not anchored, so it will match anywhere in
515 the string.
516 .Pp
517 .Fn ATF_CHECK_STREQ ,
518 .Fn ATF_CHECK_STREQ_MSG ,
519 .Fn ATF_REQUIRE_STREQ
520 and
521 .Fn ATF_REQUIRE_STREQ_MSG
522 take two strings and fail if the two are not equal character by character.
523 The common style is to put the expected string in the first parameter and the
524 observed string in the second parameter.
525 .Pp
526 .Fn ATF_CHECK_INTEQ ,
527 .Fn ATF_CHECK_INTEQ_MSG ,
528 .Fn ATF_REQUIRE_INTEQ
529 and
530 .Fn ATF_REQUIRE_INTQ_MSG
531 take two integers and fail if the two are not equal.
532 The common style is to put the expected integer in the first parameter and the
533 observed integer in the second parameter.
534 .Pp
535 .Fn ATF_CHECK_ERRNO
536 and
537 .Fn ATF_REQUIRE_ERRNO
538 take, first, the error code that the check is expecting to find in the
539 .Va errno
540 variable and, second, a boolean expression that, if evaluates to true,
541 means that a call failed and
542 .Va errno
543 has to be checked against the first value.
544 .Ss Utility functions
545 The following functions are provided as part of the
546 .Nm
547 API to simplify the creation of a variety of tests.
548 In particular, these are useful to write tests for command-line interfaces.
549 .Pp
550 .Ft void
551 .Fo atf_utils_cat_file
552 .Fa "const char *file"
553 .Fa "const char *prefix"
554 .Fc
555 .Bd -ragged -offset indent
556 Prints the contents of
557 .Fa file
558 to the standard output, prefixing every line with the string in
559 .Fa prefix .
560 .Ed
561 .Pp
562 .Ft bool
563 .Fo atf_utils_compare_file
564 .Fa "const char *file"
565 .Fa "const char *contents"
566 .Fc
567 .Bd -ragged -offset indent
568 Returns true if the given
569 .Fa file
570 matches exactly the expected inlined
571 .Fa contents .
572 .Ed
573 .Pp
574 .Ft void
575 .Fo atf_utils_copy_file
576 .Fa "const char *source"
577 .Fa "const char *destination"
578 .Fc
579 .Bd -ragged -offset indent
580 Copies the file
581 .Fa source
582 to
583 .Fa destination .
584 The permissions of the file are preserved during the code.
585 .Ed
586 .Pp
587 .Ft void
588 .Fo atf_utils_create_file
589 .Fa "const char *file"
590 .Fa "const char *contents"
591 .Fa "..."
592 .Fc
593 .Bd -ragged -offset indent
594 Creates
595 .Fa file
596 with the text given in
597 .Fa contents ,
598 which is a formatting string that uses the rest of the variable arguments.
599 .Ed
600 .Pp
601 .Ft void
602 .Fo atf_utils_file_exists
603 .Fa "const char *file"
604 .Fc
605 .Bd -ragged -offset indent
606 Checks if
607 .Fa file
608 exists.
609 .Ed
610 .Pp
611 .Ft pid_t
612 .Fo atf_utils_fork
613 .Fa "void"
614 .Fc
615 .Bd -ragged -offset indent
616 Forks a process and redirects the standard output and standard error of the
617 child to files for later validation with
618 .Fn atf_utils_wait .
619 Fails the test case if the fork fails, so this does not return an error.
620 .Ed
621 .Pp
622 .Ft void
623 .Fo atf_utils_free_charpp
624 .Fa "char **argv"
625 .Fc
626 .Bd -ragged -offset indent
627 Frees a dynamically-allocated array of dynamically-allocated strings.
628 .Ed
629 .Pp
630 .Ft bool
631 .Fo atf_utils_grep_file
632 .Fa "const char *regexp"
633 .Fa "const char *file"
634 .Fa "..."
635 .Fc
636 .Bd -ragged -offset indent
637 Searches for the
638 .Fa regexp ,
639 which is a formatting string representing the regular expression,
640 in the
641 .Fa file .
642 The variable arguments are used to construct the regular expression.
643 .Ed
644 .Pp
645 .Ft bool
646 .Fo atf_utils_grep_string
647 .Fa "const char *regexp"
648 .Fa "const char *str"
649 .Fa "..."
650 .Fc
651 .Bd -ragged -offset indent
652 Searches for the
653 .Fa regexp ,
654 which is a formatting string representing the regular expression,
655 in the literal string
656 .Fa str .
657 The variable arguments are used to construct the regular expression.
658 .Ed
659 .Pp
660 .Ft char *
661 .Fo atf_utils_readline
662 .Fa "int fd"
663 .Fc
664 .Bd -ragged -offset indent
665 Reads a line from the file descriptor
666 .Fa fd .
667 The line, if any, is returned as a dynamically-allocated buffer that must be
668 released with
669 .Xr free 3 .
670 If there was nothing to read, returns
671 .Sq NULL .
672 .Ed
673 .Pp
674 .Ft void
675 .Fo atf_utils_redirect
676 .Fa "const int fd"
677 .Fa "const char *file"
678 .Fc
679 .Bd -ragged -offset indent
680 Redirects the given file descriptor
681 .Fa fd
682 to
683 .Fa file .
684 This function exits the process in case of an error and does not properly mark
685 the test case as failed.
686 As a result, it should only be used in subprocesses of the test case; specially
687 those spawned by
688 .Fn atf_utils_fork .
689 .Ed
690 .Pp
691 .Ft void
692 .Fo atf_utils_wait
693 .Fa "const pid_t pid"
694 .Fa "const int expected_exit_status"
695 .Fa "const char *expected_stdout"
696 .Fa "const char *expected_stderr"
697 .Fc
698 .Bd -ragged -offset indent
699 Waits and validates the result of a subprocess spawned with
700 .Fn atf_utils_fork .
701 The validation involves checking that the subprocess exited cleanly and returned
702 the code specified in
703 .Fa expected_exit_status
704 and that its standard output and standard error match the strings given in
705 .Fa expected_stdout
706 and
707 .Fa expected_stderr .
708 .Pp
709 If any of the
710 .Fa expected_stdout
711 or
712 .Fa expected_stderr
713 strings are prefixed with
714 .Sq save: ,
715 then they specify the name of the file into which to store the stdout or stderr
716 of the subprocess, and no comparison is performed.
717 .Ed
718 .Sh ENVIRONMENT
719 The following variables are recognized by
720 .Nm
721 but should not be overridden other than for testing purposes:
722 .Pp
723 .Bl -tag -width ATFXBUILDXCXXFLAGSXX -compact
724 .It Va ATF_BUILD_CC
725 Path to the C compiler.
726 .It Va ATF_BUILD_CFLAGS
727 C compiler flags.
728 .It Va ATF_BUILD_CPP
729 Path to the C/C++ preprocessor.
730 .It Va ATF_BUILD_CPPFLAGS
731 C/C++ preprocessor flags.
732 .It Va ATF_BUILD_CXX
733 Path to the C++ compiler.
734 .It Va ATF_BUILD_CXXFLAGS
735 C++ compiler flags.
736 .El
737 .Sh EXAMPLES
738 The following shows a complete test program with a single test case that
739 validates the addition operator:
740 .Bd -literal -offset indent
741 #include <atf-c.h>
742
743 ATF_TC(addition);
744 ATF_TC_HEAD(addition, tc)
745 {
746     atf_tc_set_md_var(tc, "descr",
747                       "Sample tests for the addition operator");
748 }
749 ATF_TC_BODY(addition, tc)
750 {
751     ATF_CHECK_EQ(0, 0 + 0);
752     ATF_CHECK_EQ(1, 0 + 1);
753     ATF_CHECK_EQ(1, 1 + 0);
754
755     ATF_CHECK_EQ(2, 1 + 1);
756
757     ATF_CHECK_EQ(300, 100 + 200);
758 }
759
760 ATF_TC(string_formatting);
761 ATF_TC_HEAD(string_formatting, tc)
762 {
763     atf_tc_set_md_var(tc, "descr",
764                       "Sample tests for the snprintf");
765 }
766 ATF_TC_BODY(string_formatting, tc)
767 {
768     char buf[1024];
769     snprintf(buf, sizeof(buf), "a %s", "string");
770     ATF_CHECK_STREQ_MSG("a string", buf, "%s is not working");
771 }
772
773 ATF_TC(open_failure);
774 ATF_TC_HEAD(open_failure, tc)
775 {
776     atf_tc_set_md_var(tc, "descr",
777                       "Sample tests for the open function");
778 }
779 ATF_TC_BODY(open_failure, tc)
780 {
781     ATF_CHECK_ERRNO(ENOENT, open("non-existent", O_RDONLY) == -1);
782 }
783
784 ATF_TC(known_bug);
785 ATF_TC_HEAD(known_bug, tc)
786 {
787     atf_tc_set_md_var(tc, "descr",
788                       "Reproduces a known bug");
789 }
790 ATF_TC_BODY(known_bug, tc)
791 {
792     atf_tc_expect_fail("See bug number foo/bar");
793     ATF_CHECK_EQ(3, 1 + 1);
794     atf_tc_expect_pass();
795     ATF_CHECK_EQ(3, 1 + 2);
796 }
797
798 ATF_TP_ADD_TCS(tp)
799 {
800     ATF_TP_ADD_TC(tp, addition);
801     ATF_TP_ADD_TC(tp, string_formatting);
802     ATF_TP_ADD_TC(tp, open_failure);
803     ATF_TP_ADD_TC(tp, known_bug);
804
805     return atf_no_error();
806 }
807 .Ed
808 .Sh SEE ALSO
809 .Xr atf-test-program 1 ,
810 .Xr atf-test-case 4