1 .\" Copyright (c) 2008 The NetBSD Foundation, Inc.
2 .\" All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
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.
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.
33 .Nm ATF_CHECK_EQ_MSG ,
35 .Nm ATF_CHECK_MATCH_MSG ,
37 .Nm ATF_CHECK_STREQ_MSG ,
39 .Nm ATF_CHECK_INTEQ_MSG ,
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 ,
54 .Nm ATF_TC_BODY_NAME ,
56 .Nm ATF_TC_CLEANUP_NAME ,
58 .Nm ATF_TC_HEAD_NAME ,
60 .Nm ATF_TC_WITH_CLEANUP ,
61 .Nm ATF_TC_WITHOUT_HEAD ,
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 ,
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 ,
78 .Nm atf_tc_fail_nonfatal ,
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 ,
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 ,
93 .Nd C API to write ATF-based test programs
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
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"
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"
148 .Fn atf_tc_skip "reason"
150 .Fo atf_utils_cat_file
151 .Fa "const char *file"
152 .Fa "const char *prefix"
155 .Fo atf_utils_compare_file
156 .Fa "const char *file"
157 .Fa "const char *contents"
160 .Fo atf_utils_copy_file
161 .Fa "const char *source"
162 .Fa "const char *destination"
165 .Fo atf_utils_create_file
166 .Fa "const char *file"
167 .Fa "const char *contents"
171 .Fo atf_utils_file_exists
172 .Fa "const char *file"
179 .Fo atf_utils_free_charpp
183 .Fo atf_utils_grep_file
184 .Fa "const char *regexp"
185 .Fa "const char *file"
189 .Fo atf_utils_grep_string
190 .Fa "const char *regexp"
191 .Fa "const char *str"
195 .Fo atf_utils_readline
199 .Fo atf_utils_redirect
201 .Fa "const char *file"
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"
211 ATF provides a C programming interface to implement test programs.
212 C-based test programs follow this template:
213 .Bd -literal -offset indent
214 \&... C-specific includes go here ...
221 ... first test case's header ...
225 ... first test case's body ...
228 ATF_TC_WITH_CLEANUP(tc2);
231 ... second test case's header ...
235 ... second test case's body ...
237 ATF_TC_CLEANUP(tc2, tc)
239 ... second test case's cleanup ...
242 ATF_TC_WITHOUT_HEAD(tc3);
245 ... third test case's body ...
248 \&... additional test cases ...
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 ...
257 return atf_no_error();
260 .Ss Definition of test cases
261 Test cases have an identifier and are composed of three different parts:
262 the header, the body and an optional cleanup routine, all of which are
264 .Xr atf-test-case 4 .
265 To define test cases, one can use the
267 .Fn ATF_TC_WITH_CLEANUP
269 .Fn ATF_TC_WITHOUT_HEAD
270 macros, which take a single parameter specifying the test case's name.
272 requires to define a head and a body for the test case,
273 .Fn ATF_TC_WITH_CLEANUP
274 requires to define a head, a body and a cleanup for the test case and
275 .Fn ATF_TC_WITHOUT_HEAD
276 requires only a body for the test case.
277 It is important to note that these
279 set the test case up for execution when the program is run.
280 In order to do so, a later registration is needed with the
283 .Sx Program initialization .
285 Later on, one must define the three parts of the body by means of three
287 Their headers are given by the
292 macros, all of which take the test case name provided to the
294 .Fn ATF_TC_WITH_CLEANUP ,
296 .Fn ATF_TC_WITHOUT_HEAD
297 macros and the name of the variable that will hold a pointer to the
299 Following each of these, a block of code is expected, surrounded by the
300 opening and closing brackets.
301 .Ss Program initialization
302 The library provides a way to easily define the test program's
305 You should never define one on your own, but rely on the
306 library to do it for you.
307 This is done by using the
309 macro, which is passed the name of the object that will hold the test
310 cases, i.e., the test program instance.
311 This name can be whatever you want as long as it is a valid variable
314 After the macro, you are supposed to provide the body of a function, which
317 macro to register the test cases the test program will execute and return
318 a success error code.
319 The first parameter of this macro matches the name you provided in the
321 The success status can be returned using the
324 .Ss Header definitions
325 The test case's header can define the meta-data by using the
326 .Fn atf_tc_set_md_var
327 method, which takes three parameters: the first one points to the test
328 case data, the second one specifies the meta-data variable to be set
329 and the third one specifies its value.
330 Both of them are strings.
331 .Ss Configuration variables
332 The test case has read-only access to the current configuration variables
335 .Fn atf_tc_has_config_var ,
337 .Fn atf_tc_get_config_var ,
339 .Fn atf_tc_get_config_var_wd ,
341 .Fn atf_tc_get_config_var_as_bool ,
343 .Fn atf_tc_get_config_var_as_bool_wd ,
345 .Fn atf_tc_get_config_var_as_long ,
348 .Fn atf_tc_get_config_var_as_long_wd
349 functions, which can be called in any of the three parts of a test case.
353 variants take a default value for the variable which is returned if the
354 variable is not defined.
355 The other functions without the
359 the variable to be defined.
360 .Ss Access to the source directory
361 It is possible to get the path to the test case's source directory from any
362 of its three components by querying the
364 configuration variable.
365 .Ss Requiring programs
368 meta-data variable available in the header only, one can also check for
369 additional programs in the test case's body by using the
370 .Fn atf_tc_require_prog
371 function, which takes the base name or full path of a single binary.
372 Relative paths are forbidden.
373 If it is not found, the test case will be automatically skipped.
374 .Ss Test case finalization
375 The test case finalizes either when the body reaches its end, at which
376 point the test is assumed to have
378 unless any non-fatal errors were raised using
379 .Fn atf_tc_fail_nonfatal ,
380 or at any explicit call to
385 These three functions terminate the execution of the test case immediately.
386 The cleanup routine will be processed afterwards in a completely automated
387 way, regardless of the test case's termination reason.
390 does not take any parameters.
392 .Fn atf_tc_fail_nonfatal
395 take a format string and a variable list of parameters, which describe, in
396 a user-friendly manner, why the test case failed or was skipped,
398 It is very important to provide a clear error message in both cases so that
399 the user can quickly know why the test did not pass.
401 Everything explained in the previous section changes when the test case
402 expectations are redefined by the programmer.
404 Each test case has an internal state called
406 that describes what the test case expectations are at any point in time.
407 The value of this property can change during execution by any of:
408 .Bl -tag -width indent
409 .It Fn atf_tc_expect_death "reason" "..."
410 Expects the test case to exit prematurely regardless of the nature of the
412 .It Fn atf_tc_expect_exit "exitcode" "reason" "..."
413 Expects the test case to exit cleanly.
418 the runtime engine will validate that the exit code of the test case
419 matches the one provided in this call.
420 Otherwise, the exact value will be ignored.
421 .It Fn atf_tc_expect_fail "reason" "..."
422 Any failure (be it fatal or non-fatal) raised in this mode is recorded.
423 However, such failures do not report the test case as failed; instead, the
424 test case finalizes cleanly and is reported as
425 .Sq expected failure ;
426 this report includes the provided
429 If no error is raised while running in this mode, then the test case is
433 This mode is useful to reproduce actual known bugs in tests.
434 Whenever the developer fixes the bug later on, the test case will start
435 reporting a failure, signaling the developer that the test case must be
436 adjusted to the new conditions.
437 In this situation, it is useful, for example, to set
439 as the bug number for tracking purposes.
440 .It Fn atf_tc_expect_pass
441 This is the normal mode of execution.
442 In this mode, any failure is reported as such to the user and the test case
445 .It Fn atf_tc_expect_signal "signo" "reason" "..."
446 Expects the test case to terminate due to the reception of a signal.
451 the runtime engine will validate that the signal that terminated the test
452 case matches the one provided in this call.
453 Otherwise, the exact value will be ignored.
454 .It Fn atf_tc_expect_timeout "reason" "..."
455 Expects the test case to execute for longer than its timeout.
457 .Ss Helper macros for common checks
458 The library provides several macros that are very handy in multiple
460 These basically check some condition after executing a given statement or
461 processing a given expression and, if the condition is not met, they
462 report the test case as failed.
466 variant of the macros immediately abort the test case as soon as an error
467 condition is detected by calling the
470 Use this variant whenever it makes no sense to continue the execution of a
471 test case when the checked condition is not met.
474 variant, on the other hand, reports a failure as soon as it is encountered
476 .Fn atf_tc_fail_nonfatal
477 function, but the execution of the test case continues as if nothing had
479 Use this variant whenever the checked condition is important as a result of
480 the test case, but there are other conditions that can be subsequently
481 checked on the same run without aborting.
485 variants take an extra set of parameters to explicitly specify the failure
487 This failure message is formatted according to the
496 take an expression and fail if the expression evaluates to false.
499 .Fn ATF_CHECK_EQ_MSG ,
502 .Fn ATF_REQUIRE_EQ_MSG
503 take two expressions and fail if the two evaluated values are not equal.
504 The common style is to put the expected value in the first parameter and the
505 observed value in the second parameter.
507 .Fn ATF_CHECK_MATCH ,
508 .Fn ATF_CHECK_MATCH_MSG ,
509 .Fn ATF_REQUIRE_MATCH
511 .Fn ATF_REQUIRE_MATCH_MSG
512 take a regular expression and a string and fail if the regular expression does
513 not match the given string.
514 Note that the regular expression is not anchored, so it will match anywhere in
517 .Fn ATF_CHECK_STREQ ,
518 .Fn ATF_CHECK_STREQ_MSG ,
519 .Fn ATF_REQUIRE_STREQ
521 .Fn ATF_REQUIRE_STREQ_MSG
522 take two strings and fail if the two are not equal character by character.
523 The common style is to put the expected string in the first parameter and the
524 observed string in the second parameter.
526 .Fn ATF_CHECK_INTEQ ,
527 .Fn ATF_CHECK_INTEQ_MSG ,
528 .Fn ATF_REQUIRE_INTEQ
530 .Fn ATF_REQUIRE_INTQ_MSG
531 take two integers and fail if the two are not equal.
532 The common style is to put the expected integer in the first parameter and the
533 observed integer in the second parameter.
537 .Fn ATF_REQUIRE_ERRNO
538 take, first, the error code that the check is expecting to find in the
540 variable and, second, a boolean expression that, if evaluates to true,
541 means that a call failed and
543 has to be checked against the first value.
544 .Ss Utility functions
545 The following functions are provided as part of the
547 API to simplify the creation of a variety of tests.
548 In particular, these are useful to write tests for command-line interfaces.
551 .Fo atf_utils_cat_file
552 .Fa "const char *file"
553 .Fa "const char *prefix"
555 .Bd -ragged -offset indent
556 Prints the contents of
558 to the standard output, prefixing every line with the string in
563 .Fo atf_utils_compare_file
564 .Fa "const char *file"
565 .Fa "const char *contents"
567 .Bd -ragged -offset indent
568 Returns true if the given
570 matches exactly the expected inlined
575 .Fo atf_utils_copy_file
576 .Fa "const char *source"
577 .Fa "const char *destination"
579 .Bd -ragged -offset indent
584 The permissions of the file are preserved during the code.
588 .Fo atf_utils_create_file
589 .Fa "const char *file"
590 .Fa "const char *contents"
593 .Bd -ragged -offset indent
596 with the text given in
598 which is a formatting string that uses the rest of the variable arguments.
602 .Fo atf_utils_file_exists
603 .Fa "const char *file"
605 .Bd -ragged -offset indent
615 .Bd -ragged -offset indent
616 Forks a process and redirects the standard output and standard error of the
617 child to files for later validation with
619 Fails the test case if the fork fails, so this does not return an error.
623 .Fo atf_utils_free_charpp
626 .Bd -ragged -offset indent
627 Frees a dynamically-allocated array of dynamically-allocated strings.
631 .Fo atf_utils_grep_file
632 .Fa "const char *regexp"
633 .Fa "const char *file"
636 .Bd -ragged -offset indent
639 which is a formatting string representing the regular expression,
642 The variable arguments are used to construct the regular expression.
646 .Fo atf_utils_grep_string
647 .Fa "const char *regexp"
648 .Fa "const char *str"
651 .Bd -ragged -offset indent
654 which is a formatting string representing the regular expression,
655 in the literal string
657 The variable arguments are used to construct the regular expression.
661 .Fo atf_utils_readline
664 .Bd -ragged -offset indent
665 Reads a line from the file descriptor
667 The line, if any, is returned as a dynamically-allocated buffer that must be
670 If there was nothing to read, returns
675 .Fo atf_utils_redirect
677 .Fa "const char *file"
679 .Bd -ragged -offset indent
680 Redirects the given file descriptor
684 This function exits the process in case of an error and does not properly mark
685 the test case as failed.
686 As a result, it should only be used in subprocesses of the test case; specially
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"
698 .Bd -ragged -offset indent
699 Waits and validates the result of a subprocess spawned with
701 The validation involves checking that the subprocess exited cleanly and returned
702 the code specified in
703 .Fa expected_exit_status
704 and that its standard output and standard error match the strings given in
707 .Fa expected_stderr .
713 strings are prefixed with
715 then they specify the name of the file into which to store the stdout or stderr
716 of the subprocess, and no comparison is performed.
719 The following variables are recognized by
721 but should not be overridden other than for testing purposes:
723 .Bl -tag -width ATFXBUILDXCXXFLAGSXX -compact
725 Path to the C compiler.
726 .It Va ATF_BUILD_CFLAGS
729 Path to the C/C++ preprocessor.
730 .It Va ATF_BUILD_CPPFLAGS
731 C/C++ preprocessor flags.
733 Path to the C++ compiler.
734 .It Va ATF_BUILD_CXXFLAGS
738 The following shows a complete test program with a single test case that
739 validates the addition operator:
740 .Bd -literal -offset indent
744 ATF_TC_HEAD(addition, tc)
746 atf_tc_set_md_var(tc, "descr",
747 "Sample tests for the addition operator");
749 ATF_TC_BODY(addition, tc)
751 ATF_CHECK_EQ(0, 0 + 0);
752 ATF_CHECK_EQ(1, 0 + 1);
753 ATF_CHECK_EQ(1, 1 + 0);
755 ATF_CHECK_EQ(2, 1 + 1);
757 ATF_CHECK_EQ(300, 100 + 200);
760 ATF_TC(string_formatting);
761 ATF_TC_HEAD(string_formatting, tc)
763 atf_tc_set_md_var(tc, "descr",
764 "Sample tests for the snprintf");
766 ATF_TC_BODY(string_formatting, tc)
769 snprintf(buf, sizeof(buf), "a %s", "string");
770 ATF_CHECK_STREQ_MSG("a string", buf, "%s is not working");
773 ATF_TC(open_failure);
774 ATF_TC_HEAD(open_failure, tc)
776 atf_tc_set_md_var(tc, "descr",
777 "Sample tests for the open function");
779 ATF_TC_BODY(open_failure, tc)
781 ATF_CHECK_ERRNO(ENOENT, open("non-existent", O_RDONLY) == -1);
785 ATF_TC_HEAD(known_bug, tc)
787 atf_tc_set_md_var(tc, "descr",
788 "Reproduces a known bug");
790 ATF_TC_BODY(known_bug, tc)
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);
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);
805 return atf_no_error();
809 .Xr atf-test-program 1 ,