]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/atf/atf-c/atf-c-api.3
Update atf to 0.18 and remove the code of the deprecated tools.
[FreeBSD/stable/10.git] / contrib / atf / atf-c / atf-c-api.3
1 .\"
2 .\" Automated Testing Framework (atf)
3 .\"
4 .\" Copyright (c) 2008 The NetBSD Foundation, Inc.
5 .\" All rights reserved.
6 .\"
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
9 .\" are met:
10 .\" 1. Redistributions of source code must retain the above copyright
11 .\"    notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\"    notice, this list of conditions and the following disclaimer in the
14 .\"    documentation and/or other materials provided with the distribution.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 .\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 .\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 .\" IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 .\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 .\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 .\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 .\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 .\"
29 .Dd November 15, 2013
30 .Dt ATF-C-API 3
31 .Os
32 .Sh NAME
33 .Nm atf-c-api ,
34 .Nm ATF_CHECK ,
35 .Nm ATF_CHECK_MSG ,
36 .Nm ATF_CHECK_EQ ,
37 .Nm ATF_CHECK_EQ_MSG ,
38 .Nm ATF_CHECK_MATCH ,
39 .Nm ATF_CHECK_MATCH_MSG ,
40 .Nm ATF_CHECK_STREQ ,
41 .Nm ATF_CHECK_STREQ_MSG ,
42 .Nm ATF_CHECK_ERRNO ,
43 .Nm ATF_REQUIRE ,
44 .Nm ATF_REQUIRE_MSG ,
45 .Nm ATF_REQUIRE_EQ ,
46 .Nm ATF_REQUIRE_EQ_MSG ,
47 .Nm ATF_REQUIRE_MATCH ,
48 .Nm ATF_REQUIRE_MATCH_MSG ,
49 .Nm ATF_REQUIRE_STREQ ,
50 .Nm ATF_REQUIRE_STREQ_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 .Fn ATF_CHECK "expression"
97 .Fn ATF_CHECK_MSG "expression" "fail_msg_fmt" ...
98 .Fn ATF_CHECK_EQ "expression_1" "expression_2"
99 .Fn ATF_CHECK_EQ_MSG "expression_1" "expression_2" "fail_msg_fmt" ...
100 .Fn ATF_CHECK_MATCH "regexp" "string"
101 .Fn ATF_CHECK_MATCH_MSG "regexp" "string" "fail_msg_fmt" ...
102 .Fn ATF_CHECK_STREQ "string_1" "string_2"
103 .Fn ATF_CHECK_STREQ_MSG "string_1" "string_2" "fail_msg_fmt" ...
104 .Fn ATF_CHECK_ERRNO "exp_errno" "bool_expression"
105 .Fn ATF_REQUIRE "expression"
106 .Fn ATF_REQUIRE_MSG "expression" "fail_msg_fmt" ...
107 .Fn ATF_REQUIRE_EQ "expression_1" "expression_2"
108 .Fn ATF_REQUIRE_EQ_MSG "expression_1" "expression_2" "fail_msg_fmt" ...
109 .Fn ATF_REQUIRE_MATCH "regexp" "string"
110 .Fn ATF_REQUIRE_MATCH_MSG "regexp" "string" "fail_msg_fmt" ...
111 .Fn ATF_REQUIRE_STREQ "string_1" "string_2"
112 .Fn ATF_REQUIRE_STREQ_MSG "string_1" "string_2" "fail_msg_fmt" ...
113 .Fn ATF_REQUIRE_ERRNO "exp_errno" "bool_expression"
114 .Fn ATF_TC "name"
115 .Fn ATF_TC_BODY "name" "tc"
116 .Fn ATF_TC_BODY_NAME "name"
117 .Fn ATF_TC_CLEANUP "name" "tc"
118 .Fn ATF_TC_CLEANUP_NAME "name"
119 .Fn ATF_TC_HEAD "name" "tc"
120 .Fn ATF_TC_HEAD_NAME "name"
121 .Fn ATF_TC_NAME "name"
122 .Fn ATF_TC_WITH_CLEANUP "name"
123 .Fn ATF_TC_WITHOUT_HEAD "name"
124 .Fn ATF_TP_ADD_TC "tp_name" "tc_name"
125 .Fn ATF_TP_ADD_TCS "tp_name"
126 .Fn atf_tc_get_config_var "tc" "varname"
127 .Fn atf_tc_get_config_var_wd "tc" "variable_name" "default_value"
128 .Fn atf_tc_get_config_var_as_bool "tc" "variable_name"
129 .Fn atf_tc_get_config_var_as_bool_wd "tc" "variable_name" "default_value"
130 .Fn atf_tc_get_config_var_as_long "tc" "variable_name"
131 .Fn atf_tc_get_config_var_as_long_wd "tc" "variable_name" "default_value"
132 .Fn atf_no_error
133 .Fn atf_tc_expect_death "reason" "..."
134 .Fn atf_tc_expect_exit "exitcode" "reason" "..."
135 .Fn atf_tc_expect_fail "reason" "..."
136 .Fn atf_tc_expect_pass
137 .Fn atf_tc_expect_signal "signo" "reason" "..."
138 .Fn atf_tc_expect_timeout "reason" "..."
139 .Fn atf_tc_fail "reason"
140 .Fn atf_tc_fail_nonfatal "reason"
141 .Fn atf_tc_pass
142 .Fn atf_tc_skip "reason"
143 .Ft void
144 .Fo atf_utils_cat_file
145 .Fa "const char *file"
146 .Fa "const char *prefix"
147 .Fc
148 .Ft bool
149 .Fo atf_utils_compare_file
150 .Fa "const char *file"
151 .Fa "const char *contents"
152 .Fc
153 .Ft void
154 .Fo atf_utils_copy_file
155 .Fa "const char *source"
156 .Fa "const char *destination"
157 .Fc
158 .Ft void
159 .Fo atf_utils_create_file
160 .Fa "const char *file"
161 .Fa "const char *contents"
162 .Fa "..."
163 .Fc
164 .Ft void
165 .Fo atf_utils_file_exists
166 .Fa "const char *file"
167 .Fc
168 .Ft pid_t
169 .Fo atf_utils_fork
170 .Fa "void"
171 .Fc
172 .Ft void
173 .Fo atf_utils_free_charpp
174 .Fa "char **argv"
175 .Fc
176 .Ft bool
177 .Fo atf_utils_grep_file
178 .Fa "const char *regexp"
179 .Fa "const char *file"
180 .Fa "..."
181 .Fc
182 .Ft bool
183 .Fo atf_utils_grep_string
184 .Fa "const char *regexp"
185 .Fa "const char *str"
186 .Fa "..."
187 .Fc
188 .Ft char *
189 .Fo atf_utils_readline
190 .Fa "int fd"
191 .Fc
192 .Ft void
193 .Fo atf_utils_redirect
194 .Fa "const int fd"
195 .Fa "const char *file"
196 .Fc
197 .Ft void
198 .Fo atf_utils_wait
199 .Fa "const pid_t pid"
200 .Fa "const int expected_exit_status"
201 .Fa "const char *expected_stdout"
202 .Fa "const char *expected_stderr"
203 .Fc
204 .Sh DESCRIPTION
205 ATF provides a C programming interface to implement test programs.
206 C-based test programs follow this template:
207 .Bd -literal -offset indent
208 .Ns ... C-specific includes go here ...
209
210 #include <atf-c.h>
211
212 ATF_TC(tc1);
213 ATF_TC_HEAD(tc1, tc)
214 {
215     ... first test case's header ...
216 }
217 ATF_TC_BODY(tc1, tc)
218 {
219     ... first test case's body ...
220 }
221
222 ATF_TC_WITH_CLEANUP(tc2);
223 ATF_TC_HEAD(tc2, tc)
224 {
225     ... second test case's header ...
226 }
227 ATF_TC_BODY(tc2, tc)
228 {
229     ... second test case's body ...
230 }
231 ATF_TC_CLEANUP(tc2, tc)
232 {
233     ... second test case's cleanup ...
234 }
235
236 ATF_TC_WITHOUT_HEAD(tc3);
237 ATF_TC_BODY(tc3, tc)
238 {
239     ... third test case's body ...
240 }
241
242 .Ns ... additional test cases ...
243
244 ATF_TP_ADD_TCS(tp)
245 {
246     ATF_TP_ADD_TC(tcs, tc1);
247     ATF_TP_ADD_TC(tcs, tc2);
248     ATF_TP_ADD_TC(tcs, tc3);
249     ... add additional test cases ...
250
251     return atf_no_error();
252 }
253 .Ed
254 .Ss Definition of test cases
255 Test cases have an identifier and are composed of three different parts:
256 the header, the body and an optional cleanup routine, all of which are
257 described in
258 .Xr atf-test-case 4 .
259 To define test cases, one can use the
260 .Fn ATF_TC ,
261 .Fn ATF_TC_WITH_CLEANUP
262 or the
263 .Fn ATF_TC_WITHOUT_HEAD
264 macros, which take a single parameter specifiying the test case's name.
265 .Fn ATF_TC ,
266 requires to define a head and a body for the test case,
267 .Fn ATF_TC_WITH_CLEANUP
268 requires to define a head, a body and a cleanup for the test case and
269 .Fn ATF_TC_WITHOUT_HEAD
270 requires only a body for the test case.
271 It is important to note that these
272 .Em do not
273 set the test case up for execution when the program is run.
274 In order to do so, a later registration is needed with the
275 .Fn ATF_TP_ADD_TC
276 macro detailed in
277 .Sx Program initialization .
278 .Pp
279 Later on, one must define the three parts of the body by means of three
280 functions.
281 Their headers are given by the
282 .Fn ATF_TC_HEAD ,
283 .Fn ATF_TC_BODY
284 and
285 .Fn ATF_TC_CLEANUP
286 macros, all of which take the test case name provided to the
287 .Fn ATF_TC
288 .Fn ATF_TC_WITH_CLEANUP ,
289 or
290 .Fn ATF_TC_WITHOUT_HEAD
291 macros and the name of the variable that will hold a pointer to the
292 test case data.
293 Following each of these, a block of code is expected, surrounded by the
294 opening and closing brackets.
295 .Ss Program initialization
296 The library provides a way to easily define the test program's
297 .Fn main
298 function.
299 You should never define one on your own, but rely on the
300 library to do it for you.
301 This is done by using the
302 .Fn ATF_TP_ADD_TCS
303 macro, which is passed the name of the object that will hold the test
304 cases; i.e. the test program instance.
305 This name can be whatever you want as long as it is a valid variable
306 identifier.
307 .Pp
308 After the macro, you are supposed to provide the body of a function, which
309 should only use the
310 .Fn ATF_TP_ADD_TC
311 macro to register the test cases the test program will execute and return
312 a success error code.
313 The first parameter of this macro matches the name you provided in the
314 former call.
315 The success status can be returned using the
316 .Fn atf_no_error
317 function.
318 .Ss Header definitions
319 The test case's header can define the meta-data by using the
320 .Fn atf_tc_set_md_var
321 method, which takes three parameters: the first one points to the test
322 case data, the second one specifies the meta-data variable to be set
323 and the third one specifies its value.
324 Both of them are strings.
325 .Ss Configuration variables
326 The test case has read-only access to the current configuration variables
327 by means of the
328 .Ft bool
329 .Fn atf_tc_has_config_var ,
330 .Ft const char *
331 .Fn atf_tc_get_config_var ,
332 .Ft const char *
333 .Fn atf_tc_get_config_var_wd ,
334 .Ft bool
335 .Fn atf_tc_get_config_var_as_bool ,
336 .Ft bool
337 .Fn atf_tc_get_config_var_as_bool_wd ,
338 .Ft long
339 .Fn atf_tc_get_config_var_as_long ,
340 and the
341 .Ft long
342 .Fn atf_tc_get_config_var_as_long_wd
343 functions, which can be called in any of the three parts of a test case.
344 .Pp
345 The
346 .Sq _wd
347 variants take a default value for the variable which is returned if the
348 variable is not defined.
349 The other functions without the
350 .Sq _wd
351 suffix
352 .Em require
353 the variable to be defined.
354 .Ss Access to the source directory
355 It is possible to get the path to the test case's source directory from any
356 of its three components by querying the
357 .Sq srcdir
358 configuration variable.
359 .Ss Requiring programs
360 Aside from the
361 .Va require.progs
362 meta-data variable available in the header only, one can also check for
363 additional programs in the test case's body by using the
364 .Fn atf_tc_require_prog
365 function, which takes the base name or full path of a single binary.
366 Relative paths are forbidden.
367 If it is not found, the test case will be automatically skipped.
368 .Ss Test case finalization
369 The test case finalizes either when the body reaches its end, at which
370 point the test is assumed to have
371 .Em passed ,
372 unless any non-fatal errors were raised using
373 .Fn atf_tc_fail_nonfatal ,
374 or at any explicit call to
375 .Fn atf_tc_pass ,
376 .Fn atf_tc_fail
377 or
378 .Fn atf_tc_skip .
379 These three functions terminate the execution of the test case immediately.
380 The cleanup routine will be processed afterwards in a completely automated
381 way, regardless of the test case's termination reason.
382 .Pp
383 .Fn atf_tc_pass
384 does not take any parameters.
385 .Fn atf_tc_fail ,
386 .Fn atf_tc_fail_nonfatal
387 and
388 .Fn atf_tc_skip
389 take a format string and a variable list of parameters, which describe, in
390 a user-friendly manner, why the test case failed or was skipped,
391 respectively.
392 It is very important to provide a clear error message in both cases so that
393 the user can quickly know why the test did not pass.
394 .Ss Expectations
395 Everything explained in the previous section changes when the test case
396 expectations are redefined by the programmer.
397 .Pp
398 Each test case has an internal state called
399 .Sq expect
400 that describes what the test case expectations are at any point in time.
401 The value of this property can change during execution by any of:
402 .Bl -tag -width indent
403 .It Fn atf_tc_expect_death "reason" "..."
404 Expects the test case to exit prematurely regardless of the nature of the
405 exit.
406 .It Fn atf_tc_expect_exit "exitcode" "reason" "..."
407 Expects the test case to exit cleanly.
408 If
409 .Va exitcode
410 is not
411 .Sq -1 ,
412 .Xr atf-run 1
413 will validate that the exit code of the test case matches the one provided
414 in this call.
415 Otherwise, the exact value will be ignored.
416 .It Fn atf_tc_expect_fail "reason" "..."
417 Any failure (be it fatal or non-fatal) raised in this mode is recorded.
418 However, such failures do not report the test case as failed; instead, the
419 test case finalizes cleanly and is reported as
420 .Sq expected failure ;
421 this report includes the provided
422 .Fa reason
423 as part of it.
424 If no error is raised while running in this mode, then the test case is
425 reported as
426 .Sq failed .
427 .Pp
428 This mode is useful to reproduce actual known bugs in tests.
429 Whenever the developer fixes the bug later on, the test case will start
430 reporting a failure, signaling the developer that the test case must be
431 adjusted to the new conditions.
432 In this situation, it is useful, for example, to set
433 .Fa reason
434 as the bug number for tracking purposes.
435 .It Fn atf_tc_expect_pass
436 This is the normal mode of execution.
437 In this mode, any failure is reported as such to the user and the test case
438 is marked as
439 .Sq failed .
440 .It Fn atf_tc_expect_signal "signo" "reason" "..."
441 Expects the test case to terminate due to the reception of a signal.
442 If
443 .Va signo
444 is not
445 .Sq -1 ,
446 .Xr atf-run 1
447 will validate that the signal that terminated the test case matches the one
448 provided in this call.
449 Otherwise, the exact value will be ignored.
450 .It Fn atf_tc_expect_timeout "reason" "..."
451 Expects the test case to execute for longer than its timeout.
452 .El
453 .Ss Helper macros for common checks
454 The library provides several macros that are very handy in multiple
455 situations.
456 These basically check some condition after executing a given statement or
457 processing a given expression and, if the condition is not met, they
458 report the test case as failed.
459 .Pp
460 The
461 .Sq REQUIRE
462 variant of the macros immediately abort the test case as soon as an error
463 condition is detected by calling the
464 .Fn atf_tc_fail
465 function.
466 Use this variant whenever it makes no sense to continue the execution of a
467 test case when the checked condition is not met.
468 The
469 .Sq CHECK
470 variant, on the other hand, reports a failure as soon as it is encountered
471 using the
472 .Fn atf_tc_fail_nonfatal
473 function, but the execution of the test case continues as if nothing had
474 happened.
475 Use this variant whenever the checked condition is important as a result of
476 the test case, but there are other conditions that can be subsequently
477 checked on the same run without aborting.
478 .Pp
479 Additionally, the
480 .Sq MSG
481 variants take an extra set of parameters to explicitly specify the failure
482 message.
483 This failure message is formatted according to the
484 .Xr printf 3
485 formatters.
486 .Pp
487 .Fn ATF_CHECK ,
488 .Fn ATF_CHECK_MSG ,
489 .Fn ATF_REQUIRE
490 and
491 .Fn ATF_REQUIRE_MSG
492 take an expression and fail if the expression evaluates to false.
493 .Pp
494 .Fn ATF_CHECK_EQ ,
495 .Fn ATF_CHECK_EQ_MSG ,
496 .Fn ATF_REQUIRE_EQ
497 and
498 .Fn ATF_REQUIRE_EQ_MSG
499 take two expressions and fail if the two evaluated values are not equal.
500 .Pp
501 .Fn ATF_CHECK_MATCH ,
502 .Fn ATF_CHECK_MATCH_MSG ,
503 .Fn ATF_REQUIRE_MATCH
504 and
505 .Fn ATF_REQUIRE_MATCH_MSG
506 take a regular expression and a string and fail if the regular expression does
507 not match the given string.
508 Note that the regular expression is not anchored, so it will match anywhere in
509 the string.
510 .Pp
511 .Fn ATF_CHECK_STREQ ,
512 .Fn ATF_CHECK_STREQ_MSG ,
513 .Fn ATF_REQUIRE_STREQ
514 and
515 .Fn ATF_REQUIRE_STREQ_MSG
516 take two strings and fail if the two are not equal character by character.
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 EXAMPLES
702 The following shows a complete test program with a single test case that
703 validates the addition operator:
704 .Bd -literal -offset indent
705 #include <atf-c.h>
706
707 ATF_TC(addition);
708 ATF_TC_HEAD(addition, tc)
709 {
710     atf_tc_set_md_var(tc, "descr",
711                       "Sample tests for the addition operator");
712 }
713 ATF_TC_BODY(addition, tc)
714 {
715     ATF_CHECK_EQ(0 + 0, 0);
716     ATF_CHECK_EQ(0 + 1, 1);
717     ATF_CHECK_EQ(1 + 0, 1);
718
719     ATF_CHECK_EQ(1 + 1, 2);
720
721     ATF_CHECK_EQ(100 + 200, 300);
722 }
723
724 ATF_TC(string_formatting);
725 ATF_TC_HEAD(string_formatting, tc)
726 {
727     atf_tc_set_md_var(tc, "descr",
728                       "Sample tests for the snprintf");
729 }
730 ATF_TC_BODY(string_formatting, tc)
731 {
732     char buf[1024];
733     snprintf(buf, sizeof(buf), "a %s", "string");
734     ATF_CHECK_STREQ_MSG("a string", buf, "%s is not working");
735 }
736
737 ATF_TC(open_failure);
738 ATF_TC_HEAD(open_failure, tc)
739 {
740     atf_tc_set_md_var(tc, "descr",
741                       "Sample tests for the open function");
742 }
743 ATF_TC_BODY(open_failure, tc)
744 {
745     ATF_CHECK_ERRNO(ENOENT, open("non-existent", O_RDONLY) == -1);
746 }
747
748 ATF_TC(known_bug);
749 ATF_TC_HEAD(known_bug, tc)
750 {
751     atf_tc_set_md_var(tc, "descr",
752                       "Reproduces a known bug");
753 }
754 ATF_TC_BODY(known_bug, tc)
755 {
756     atf_tc_expect_fail("See bug number foo/bar");
757     ATF_CHECK_EQ(3, 1 + 1);
758     atf_tc_expect_pass();
759     ATF_CHECK_EQ(3, 1 + 2);
760 }
761
762 ATF_TP_ADD_TCS(tp)
763 {
764     ATF_TP_ADD_TC(tp, addition);
765     ATF_TP_ADD_TC(tp, string_formatting);
766     ATF_TP_ADD_TC(tp, open_failure);
767     ATF_TP_ADD_TC(tp, known_bug);
768
769     return atf_no_error();
770 }
771 .Ed
772 .Sh SEE ALSO
773 .Xr atf-test-program 1 ,
774 .Xr atf-test-case 4 ,
775 .Xr atf 7