xref: /freebsd/contrib/atf/atf-c/atf-c.3 (revision 6ba2210ee039f2f12878c217bcf058e9c8b26b29)
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
211ATF provides a C programming interface to implement test programs.
212C-based test programs follow this template:
213.Bd -literal -offset indent
214\&... C-specific includes go here ...
215
216#include <atf-c.h>
217
218ATF_TC(tc1);
219ATF_TC_HEAD(tc1, tc)
220{
221    ... first test case's header ...
222}
223ATF_TC_BODY(tc1, tc)
224{
225    ... first test case's body ...
226}
227
228ATF_TC_WITH_CLEANUP(tc2);
229ATF_TC_HEAD(tc2, tc)
230{
231    ... second test case's header ...
232}
233ATF_TC_BODY(tc2, tc)
234{
235    ... second test case's body ...
236}
237ATF_TC_CLEANUP(tc2, tc)
238{
239    ... second test case's cleanup ...
240}
241
242ATF_TC_WITHOUT_HEAD(tc3);
243ATF_TC_BODY(tc3, tc)
244{
245    ... third test case's body ...
246}
247
248\&... additional test cases ...
249
250ATF_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
261Test cases have an identifier and are composed of three different parts:
262the header, the body and an optional cleanup routine, all of which are
263described in
264.Xr atf-test-case 4 .
265To define test cases, one can use the
266.Fn ATF_TC ,
267.Fn ATF_TC_WITH_CLEANUP
268or the
269.Fn ATF_TC_WITHOUT_HEAD
270macros, which take a single parameter specifying the test case's name.
271.Fn ATF_TC ,
272requires to define a head and a body for the test case,
273.Fn ATF_TC_WITH_CLEANUP
274requires to define a head, a body and a cleanup for the test case and
275.Fn ATF_TC_WITHOUT_HEAD
276requires only a body for the test case.
277It is important to note that these
278.Em do not
279set the test case up for execution when the program is run.
280In order to do so, a later registration is needed with the
281.Fn ATF_TP_ADD_TC
282macro detailed in
283.Sx Program initialization .
284.Pp
285Later on, one must define the three parts of the body by means of three
286functions.
287Their headers are given by the
288.Fn ATF_TC_HEAD ,
289.Fn ATF_TC_BODY
290and
291.Fn ATF_TC_CLEANUP
292macros, all of which take the test case name provided to the
293.Fn ATF_TC
294.Fn ATF_TC_WITH_CLEANUP ,
295or
296.Fn ATF_TC_WITHOUT_HEAD
297macros and the name of the variable that will hold a pointer to the
298test case data.
299Following each of these, a block of code is expected, surrounded by the
300opening and closing brackets.
301.Ss Program initialization
302The library provides a way to easily define the test program's
303.Fn main
304function.
305You should never define one on your own, but rely on the
306library to do it for you.
307This is done by using the
308.Fn ATF_TP_ADD_TCS
309macro, which is passed the name of the object that will hold the test
310cases, i.e., the test program instance.
311This name can be whatever you want as long as it is a valid variable
312identifier.
313.Pp
314After the macro, you are supposed to provide the body of a function, which
315should only use the
316.Fn ATF_TP_ADD_TC
317macro to register the test cases the test program will execute and return
318a success error code.
319The first parameter of this macro matches the name you provided in the
320former call.
321The success status can be returned using the
322.Fn atf_no_error
323function.
324.Ss Header definitions
325The test case's header can define the meta-data by using the
326.Fn atf_tc_set_md_var
327method, which takes three parameters: the first one points to the test
328case data, the second one specifies the meta-data variable to be set
329and the third one specifies its value.
330Both of them are strings.
331.Ss Configuration variables
332The test case has read-only access to the current configuration variables
333by 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 ,
346and the
347.Ft long
348.Fn atf_tc_get_config_var_as_long_wd
349functions, which can be called in any of the three parts of a test case.
350.Pp
351The
352.Sq _wd
353variants take a default value for the variable which is returned if the
354variable is not defined.
355The other functions without the
356.Sq _wd
357suffix
358.Em require
359the variable to be defined.
360.Ss Access to the source directory
361It is possible to get the path to the test case's source directory from any
362of its three components by querying the
363.Sq srcdir
364configuration variable.
365.Ss Requiring programs
366Aside from the
367.Va require.progs
368meta-data variable available in the header only, one can also check for
369additional programs in the test case's body by using the
370.Fn atf_tc_require_prog
371function, which takes the base name or full path of a single binary.
372Relative paths are forbidden.
373If it is not found, the test case will be automatically skipped.
374.Ss Test case finalization
375The test case finalizes either when the body reaches its end, at which
376point the test is assumed to have
377.Em passed ,
378unless any non-fatal errors were raised using
379.Fn atf_tc_fail_nonfatal ,
380or at any explicit call to
381.Fn atf_tc_pass ,
382.Fn atf_tc_fail
383or
384.Fn atf_tc_skip .
385These three functions terminate the execution of the test case immediately.
386The cleanup routine will be processed afterwards in a completely automated
387way, regardless of the test case's termination reason.
388.Pp
389.Fn atf_tc_pass
390does not take any parameters.
391.Fn atf_tc_fail ,
392.Fn atf_tc_fail_nonfatal
393and
394.Fn atf_tc_skip
395take a format string and a variable list of parameters, which describe, in
396a user-friendly manner, why the test case failed or was skipped,
397respectively.
398It is very important to provide a clear error message in both cases so that
399the user can quickly know why the test did not pass.
400.Ss Expectations
401Everything explained in the previous section changes when the test case
402expectations are redefined by the programmer.
403.Pp
404Each test case has an internal state called
405.Sq expect
406that describes what the test case expectations are at any point in time.
407The value of this property can change during execution by any of:
408.Bl -tag -width indent
409.It Fn atf_tc_expect_death "reason" "..."
410Expects the test case to exit prematurely regardless of the nature of the
411exit.
412.It Fn atf_tc_expect_exit "exitcode" "reason" "..."
413Expects the test case to exit cleanly.
414If
415.Va exitcode
416is not
417.Sq -1 ,
418the runtime engine will validate that the exit code of the test case
419matches the one provided in this call.
420Otherwise, the exact value will be ignored.
421.It Fn atf_tc_expect_fail "reason" "..."
422Any failure (be it fatal or non-fatal) raised in this mode is recorded.
423However, such failures do not report the test case as failed; instead, the
424test case finalizes cleanly and is reported as
425.Sq expected failure ;
426this report includes the provided
427.Fa reason
428as part of it.
429If no error is raised while running in this mode, then the test case is
430reported as
431.Sq failed .
432.Pp
433This mode is useful to reproduce actual known bugs in tests.
434Whenever the developer fixes the bug later on, the test case will start
435reporting a failure, signaling the developer that the test case must be
436adjusted to the new conditions.
437In this situation, it is useful, for example, to set
438.Fa reason
439as the bug number for tracking purposes.
440.It Fn atf_tc_expect_pass
441This is the normal mode of execution.
442In this mode, any failure is reported as such to the user and the test case
443is marked as
444.Sq failed .
445.It Fn atf_tc_expect_signal "signo" "reason" "..."
446Expects the test case to terminate due to the reception of a signal.
447If
448.Va signo
449is not
450.Sq -1 ,
451the runtime engine will validate that the signal that terminated the test
452case matches the one provided in this call.
453Otherwise, the exact value will be ignored.
454.It Fn atf_tc_expect_timeout "reason" "..."
455Expects the test case to execute for longer than its timeout.
456.El
457.Ss Helper macros for common checks
458The library provides several macros that are very handy in multiple
459situations.
460These basically check some condition after executing a given statement or
461processing a given expression and, if the condition is not met, they
462report the test case as failed.
463.Pp
464The
465.Sq REQUIRE
466variant of the macros immediately abort the test case as soon as an error
467condition is detected by calling the
468.Fn atf_tc_fail
469function.
470Use this variant whenever it makes no sense to continue the execution of a
471test case when the checked condition is not met.
472The
473.Sq CHECK
474variant, on the other hand, reports a failure as soon as it is encountered
475using the
476.Fn atf_tc_fail_nonfatal
477function, but the execution of the test case continues as if nothing had
478happened.
479Use this variant whenever the checked condition is important as a result of
480the test case, but there are other conditions that can be subsequently
481checked on the same run without aborting.
482.Pp
483Additionally, the
484.Sq MSG
485variants take an extra set of parameters to explicitly specify the failure
486message.
487This failure message is formatted according to the
488.Xr printf 3
489formatters.
490.Pp
491.Fn ATF_CHECK ,
492.Fn ATF_CHECK_MSG ,
493.Fn ATF_REQUIRE
494and
495.Fn ATF_REQUIRE_MSG
496take 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
501and
502.Fn ATF_REQUIRE_EQ_MSG
503take two expressions and fail if the two evaluated values are not equal.
504The common style is to put the expected value in the first parameter and the
505observed value in the second parameter.
506.Pp
507.Fn ATF_CHECK_MATCH ,
508.Fn ATF_CHECK_MATCH_MSG ,
509.Fn ATF_REQUIRE_MATCH
510and
511.Fn ATF_REQUIRE_MATCH_MSG
512take a regular expression and a string and fail if the regular expression does
513not match the given string.
514Note that the regular expression is not anchored, so it will match anywhere in
515the string.
516.Pp
517.Fn ATF_CHECK_STREQ ,
518.Fn ATF_CHECK_STREQ_MSG ,
519.Fn ATF_REQUIRE_STREQ
520and
521.Fn ATF_REQUIRE_STREQ_MSG
522take two strings and fail if the two are not equal character by character.
523The common style is to put the expected string in the first parameter and the
524observed string in the second parameter.
525.Pp
526.Fn ATF_CHECK_INTEQ ,
527.Fn ATF_CHECK_INTEQ_MSG ,
528.Fn ATF_REQUIRE_INTEQ
529and
530.Fn ATF_REQUIRE_INTQ_MSG
531take two integers and fail if the two are not equal.
532The common style is to put the expected integer in the first parameter and the
533observed integer in the second parameter.
534.Pp
535.Fn ATF_CHECK_ERRNO
536and
537.Fn ATF_REQUIRE_ERRNO
538take, first, the error code that the check is expecting to find in the
539.Va errno
540variable and, second, a boolean expression that, if evaluates to true,
541means that a call failed and
542.Va errno
543has to be checked against the first value.
544.Ss Utility functions
545The following functions are provided as part of the
546.Nm
547API to simplify the creation of a variety of tests.
548In 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
556Prints the contents of
557.Fa file
558to 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
568Returns true if the given
569.Fa file
570matches 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
580Copies the file
581.Fa source
582to
583.Fa destination .
584The 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
594Creates
595.Fa file
596with the text given in
597.Fa contents ,
598which 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
606Checks if
607.Fa file
608exists.
609.Ed
610.Pp
611.Ft pid_t
612.Fo atf_utils_fork
613.Fa "void"
614.Fc
615.Bd -ragged -offset indent
616Forks a process and redirects the standard output and standard error of the
617child to files for later validation with
618.Fn atf_utils_wait .
619Fails 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
627Frees 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
637Searches for the
638.Fa regexp ,
639which is a formatting string representing the regular expression,
640in the
641.Fa file .
642The 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
652Searches for the
653.Fa regexp ,
654which is a formatting string representing the regular expression,
655in the literal string
656.Fa str .
657The 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
665Reads a line from the file descriptor
666.Fa fd .
667The line, if any, is returned as a dynamically-allocated buffer that must be
668released with
669.Xr free 3 .
670If 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
680Redirects the given file descriptor
681.Fa fd
682to
683.Fa file .
684This function exits the process in case of an error and does not properly mark
685the test case as failed.
686As a result, it should only be used in subprocesses of the test case; specially
687those 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
699Waits and validates the result of a subprocess spawned with
700.Fn atf_utils_fork .
701The validation involves checking that the subprocess exited cleanly and returned
702the code specified in
703.Fa expected_exit_status
704and that its standard output and standard error match the strings given in
705.Fa expected_stdout
706and
707.Fa expected_stderr .
708.Pp
709If any of the
710.Fa expected_stdout
711or
712.Fa expected_stderr
713strings are prefixed with
714.Sq save: ,
715then they specify the name of the file into which to store the stdout or stderr
716of the subprocess, and no comparison is performed.
717.Ed
718.Sh ENVIRONMENT
719The following variables are recognized by
720.Nm
721but should not be overridden other than for testing purposes:
722.Pp
723.Bl -tag -width ATFXBUILDXCXXFLAGSXX -compact
724.It Va ATF_BUILD_CC
725Path to the C compiler.
726.It Va ATF_BUILD_CFLAGS
727C compiler flags.
728.It Va ATF_BUILD_CPP
729Path to the C/C++ preprocessor.
730.It Va ATF_BUILD_CPPFLAGS
731C/C++ preprocessor flags.
732.It Va ATF_BUILD_CXX
733Path to the C++ compiler.
734.It Va ATF_BUILD_CXXFLAGS
735C++ compiler flags.
736.El
737.Sh EXAMPLES
738The following shows a complete test program with a single test case that
739validates the addition operator:
740.Bd -literal -offset indent
741#include <atf-c.h>
742
743ATF_TC(addition);
744ATF_TC_HEAD(addition, tc)
745{
746    atf_tc_set_md_var(tc, "descr",
747                      "Sample tests for the addition operator");
748}
749ATF_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
760ATF_TC(string_formatting);
761ATF_TC_HEAD(string_formatting, tc)
762{
763    atf_tc_set_md_var(tc, "descr",
764                      "Sample tests for the snprintf");
765}
766ATF_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
773ATF_TC(open_failure);
774ATF_TC_HEAD(open_failure, tc)
775{
776    atf_tc_set_md_var(tc, "descr",
777                      "Sample tests for the open function");
778}
779ATF_TC_BODY(open_failure, tc)
780{
781    ATF_CHECK_ERRNO(ENOENT, open("non-existent", O_RDONLY) == -1);
782}
783
784ATF_TC(known_bug);
785ATF_TC_HEAD(known_bug, tc)
786{
787    atf_tc_set_md_var(tc, "descr",
788                      "Reproduces a known bug");
789}
790ATF_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
798ATF_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
811