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