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