Index: stable/10/contrib/atf/atf-c/atf-c-api.3 =================================================================== --- stable/10/contrib/atf/atf-c/atf-c-api.3 (revision 315182) +++ stable/10/contrib/atf/atf-c/atf-c-api.3 (nonexistent) @@ -1,772 +0,0 @@ -.\" -.\" Automated Testing Framework (atf) -.\" -.\" Copyright (c) 2008 The NetBSD Foundation, Inc. -.\" All rights reserved. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND -.\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, -.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. -.\" IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY -.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE -.\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER -.\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN -.\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -.\" -.Dd March 2, 2014 -.Dt ATF-C-API 3 -.Os -.Sh NAME -.Nm atf-c-api , -.Nm ATF_CHECK , -.Nm ATF_CHECK_MSG , -.Nm ATF_CHECK_EQ , -.Nm ATF_CHECK_EQ_MSG , -.Nm ATF_CHECK_MATCH , -.Nm ATF_CHECK_MATCH_MSG , -.Nm ATF_CHECK_STREQ , -.Nm ATF_CHECK_STREQ_MSG , -.Nm ATF_CHECK_ERRNO , -.Nm ATF_REQUIRE , -.Nm ATF_REQUIRE_MSG , -.Nm ATF_REQUIRE_EQ , -.Nm ATF_REQUIRE_EQ_MSG , -.Nm ATF_REQUIRE_MATCH , -.Nm ATF_REQUIRE_MATCH_MSG , -.Nm ATF_REQUIRE_STREQ , -.Nm ATF_REQUIRE_STREQ_MSG , -.Nm ATF_REQUIRE_ERRNO , -.Nm ATF_TC , -.Nm ATF_TC_BODY , -.Nm ATF_TC_BODY_NAME , -.Nm ATF_TC_CLEANUP , -.Nm ATF_TC_CLEANUP_NAME , -.Nm ATF_TC_HEAD , -.Nm ATF_TC_HEAD_NAME , -.Nm ATF_TC_NAME , -.Nm ATF_TC_WITH_CLEANUP , -.Nm ATF_TC_WITHOUT_HEAD , -.Nm ATF_TP_ADD_TC , -.Nm ATF_TP_ADD_TCS , -.Nm atf_tc_get_config_var , -.Nm atf_tc_get_config_var_wd , -.Nm atf_tc_get_config_var_as_bool , -.Nm atf_tc_get_config_var_as_bool_wd , -.Nm atf_tc_get_config_var_as_long , -.Nm atf_tc_get_config_var_as_long_wd , -.Nm atf_no_error , -.Nm atf_tc_expect_death , -.Nm atf_tc_expect_exit , -.Nm atf_tc_expect_fail , -.Nm atf_tc_expect_pass , -.Nm atf_tc_expect_signal , -.Nm atf_tc_expect_timeout , -.Nm atf_tc_fail , -.Nm atf_tc_fail_nonfatal , -.Nm atf_tc_pass , -.Nm atf_tc_skip , -.Nm atf_utils_cat_file , -.Nm atf_utils_compare_file , -.Nm atf_utils_copy_file , -.Nm atf_utils_create_file , -.Nm atf_utils_file_exists , -.Nm atf_utils_fork , -.Nm atf_utils_free_charpp , -.Nm atf_utils_grep_file , -.Nm atf_utils_grep_string , -.Nm atf_utils_readline , -.Nm atf_utils_redirect , -.Nm atf_utils_wait -.Nd C API to write ATF-based test programs -.Sh SYNOPSIS -.In atf-c.h -.Fn ATF_CHECK "expression" -.Fn ATF_CHECK_MSG "expression" "fail_msg_fmt" ... -.Fn ATF_CHECK_EQ "expression_1" "expression_2" -.Fn ATF_CHECK_EQ_MSG "expression_1" "expression_2" "fail_msg_fmt" ... -.Fn ATF_CHECK_MATCH "regexp" "string" -.Fn ATF_CHECK_MATCH_MSG "regexp" "string" "fail_msg_fmt" ... -.Fn ATF_CHECK_STREQ "string_1" "string_2" -.Fn ATF_CHECK_STREQ_MSG "string_1" "string_2" "fail_msg_fmt" ... -.Fn ATF_CHECK_ERRNO "exp_errno" "bool_expression" -.Fn ATF_REQUIRE "expression" -.Fn ATF_REQUIRE_MSG "expression" "fail_msg_fmt" ... -.Fn ATF_REQUIRE_EQ "expression_1" "expression_2" -.Fn ATF_REQUIRE_EQ_MSG "expression_1" "expression_2" "fail_msg_fmt" ... -.Fn ATF_REQUIRE_MATCH "regexp" "string" -.Fn ATF_REQUIRE_MATCH_MSG "regexp" "string" "fail_msg_fmt" ... -.Fn ATF_REQUIRE_STREQ "string_1" "string_2" -.Fn ATF_REQUIRE_STREQ_MSG "string_1" "string_2" "fail_msg_fmt" ... -.Fn ATF_REQUIRE_ERRNO "exp_errno" "bool_expression" -.Fn ATF_TC "name" -.Fn ATF_TC_BODY "name" "tc" -.Fn ATF_TC_BODY_NAME "name" -.Fn ATF_TC_CLEANUP "name" "tc" -.Fn ATF_TC_CLEANUP_NAME "name" -.Fn ATF_TC_HEAD "name" "tc" -.Fn ATF_TC_HEAD_NAME "name" -.Fn ATF_TC_NAME "name" -.Fn ATF_TC_WITH_CLEANUP "name" -.Fn ATF_TC_WITHOUT_HEAD "name" -.Fn ATF_TP_ADD_TC "tp_name" "tc_name" -.Fn ATF_TP_ADD_TCS "tp_name" -.Fn atf_tc_get_config_var "tc" "varname" -.Fn atf_tc_get_config_var_wd "tc" "variable_name" "default_value" -.Fn atf_tc_get_config_var_as_bool "tc" "variable_name" -.Fn atf_tc_get_config_var_as_bool_wd "tc" "variable_name" "default_value" -.Fn atf_tc_get_config_var_as_long "tc" "variable_name" -.Fn atf_tc_get_config_var_as_long_wd "tc" "variable_name" "default_value" -.Fn atf_no_error -.Fn atf_tc_expect_death "reason" "..." -.Fn atf_tc_expect_exit "exitcode" "reason" "..." -.Fn atf_tc_expect_fail "reason" "..." -.Fn atf_tc_expect_pass -.Fn atf_tc_expect_signal "signo" "reason" "..." -.Fn atf_tc_expect_timeout "reason" "..." -.Fn atf_tc_fail "reason" -.Fn atf_tc_fail_nonfatal "reason" -.Fn atf_tc_pass -.Fn atf_tc_skip "reason" -.Ft void -.Fo atf_utils_cat_file -.Fa "const char *file" -.Fa "const char *prefix" -.Fc -.Ft bool -.Fo atf_utils_compare_file -.Fa "const char *file" -.Fa "const char *contents" -.Fc -.Ft void -.Fo atf_utils_copy_file -.Fa "const char *source" -.Fa "const char *destination" -.Fc -.Ft void -.Fo atf_utils_create_file -.Fa "const char *file" -.Fa "const char *contents" -.Fa "..." -.Fc -.Ft void -.Fo atf_utils_file_exists -.Fa "const char *file" -.Fc -.Ft pid_t -.Fo atf_utils_fork -.Fa "void" -.Fc -.Ft void -.Fo atf_utils_free_charpp -.Fa "char **argv" -.Fc -.Ft bool -.Fo atf_utils_grep_file -.Fa "const char *regexp" -.Fa "const char *file" -.Fa "..." -.Fc -.Ft bool -.Fo atf_utils_grep_string -.Fa "const char *regexp" -.Fa "const char *str" -.Fa "..." -.Fc -.Ft char * -.Fo atf_utils_readline -.Fa "int fd" -.Fc -.Ft void -.Fo atf_utils_redirect -.Fa "const int fd" -.Fa "const char *file" -.Fc -.Ft void -.Fo atf_utils_wait -.Fa "const pid_t pid" -.Fa "const int expected_exit_status" -.Fa "const char *expected_stdout" -.Fa "const char *expected_stderr" -.Fc -.Sh DESCRIPTION -ATF provides a C programming interface to implement test programs. -C-based test programs follow this template: -.Bd -literal -offset indent -.Ns ... C-specific includes go here ... - -#include - -ATF_TC(tc1); -ATF_TC_HEAD(tc1, tc) -{ - ... first test case's header ... -} -ATF_TC_BODY(tc1, tc) -{ - ... first test case's body ... -} - -ATF_TC_WITH_CLEANUP(tc2); -ATF_TC_HEAD(tc2, tc) -{ - ... second test case's header ... -} -ATF_TC_BODY(tc2, tc) -{ - ... second test case's body ... -} -ATF_TC_CLEANUP(tc2, tc) -{ - ... second test case's cleanup ... -} - -ATF_TC_WITHOUT_HEAD(tc3); -ATF_TC_BODY(tc3, tc) -{ - ... third test case's body ... -} - -.Ns ... additional test cases ... - -ATF_TP_ADD_TCS(tp) -{ - ATF_TP_ADD_TC(tcs, tc1); - ATF_TP_ADD_TC(tcs, tc2); - ATF_TP_ADD_TC(tcs, tc3); - ... add additional test cases ... - - return atf_no_error(); -} -.Ed -.Ss Definition of test cases -Test cases have an identifier and are composed of three different parts: -the header, the body and an optional cleanup routine, all of which are -described in -.Xr atf-test-case 4 . -To define test cases, one can use the -.Fn ATF_TC , -.Fn ATF_TC_WITH_CLEANUP -or the -.Fn ATF_TC_WITHOUT_HEAD -macros, which take a single parameter specifiying the test case's name. -.Fn ATF_TC , -requires to define a head and a body for the test case, -.Fn ATF_TC_WITH_CLEANUP -requires to define a head, a body and a cleanup for the test case and -.Fn ATF_TC_WITHOUT_HEAD -requires only a body for the test case. -It is important to note that these -.Em do not -set the test case up for execution when the program is run. -In order to do so, a later registration is needed with the -.Fn ATF_TP_ADD_TC -macro detailed in -.Sx Program initialization . -.Pp -Later on, one must define the three parts of the body by means of three -functions. -Their headers are given by the -.Fn ATF_TC_HEAD , -.Fn ATF_TC_BODY -and -.Fn ATF_TC_CLEANUP -macros, all of which take the test case name provided to the -.Fn ATF_TC -.Fn ATF_TC_WITH_CLEANUP , -or -.Fn ATF_TC_WITHOUT_HEAD -macros and the name of the variable that will hold a pointer to the -test case data. -Following each of these, a block of code is expected, surrounded by the -opening and closing brackets. -.Ss Program initialization -The library provides a way to easily define the test program's -.Fn main -function. -You should never define one on your own, but rely on the -library to do it for you. -This is done by using the -.Fn ATF_TP_ADD_TCS -macro, which is passed the name of the object that will hold the test -cases; i.e. the test program instance. -This name can be whatever you want as long as it is a valid variable -identifier. -.Pp -After the macro, you are supposed to provide the body of a function, which -should only use the -.Fn ATF_TP_ADD_TC -macro to register the test cases the test program will execute and return -a success error code. -The first parameter of this macro matches the name you provided in the -former call. -The success status can be returned using the -.Fn atf_no_error -function. -.Ss Header definitions -The test case's header can define the meta-data by using the -.Fn atf_tc_set_md_var -method, which takes three parameters: the first one points to the test -case data, the second one specifies the meta-data variable to be set -and the third one specifies its value. -Both of them are strings. -.Ss Configuration variables -The test case has read-only access to the current configuration variables -by means of the -.Ft bool -.Fn atf_tc_has_config_var , -.Ft const char * -.Fn atf_tc_get_config_var , -.Ft const char * -.Fn atf_tc_get_config_var_wd , -.Ft bool -.Fn atf_tc_get_config_var_as_bool , -.Ft bool -.Fn atf_tc_get_config_var_as_bool_wd , -.Ft long -.Fn atf_tc_get_config_var_as_long , -and the -.Ft long -.Fn atf_tc_get_config_var_as_long_wd -functions, which can be called in any of the three parts of a test case. -.Pp -The -.Sq _wd -variants take a default value for the variable which is returned if the -variable is not defined. -The other functions without the -.Sq _wd -suffix -.Em require -the variable to be defined. -.Ss Access to the source directory -It is possible to get the path to the test case's source directory from any -of its three components by querying the -.Sq srcdir -configuration variable. -.Ss Requiring programs -Aside from the -.Va require.progs -meta-data variable available in the header only, one can also check for -additional programs in the test case's body by using the -.Fn atf_tc_require_prog -function, which takes the base name or full path of a single binary. -Relative paths are forbidden. -If it is not found, the test case will be automatically skipped. -.Ss Test case finalization -The test case finalizes either when the body reaches its end, at which -point the test is assumed to have -.Em passed , -unless any non-fatal errors were raised using -.Fn atf_tc_fail_nonfatal , -or at any explicit call to -.Fn atf_tc_pass , -.Fn atf_tc_fail -or -.Fn atf_tc_skip . -These three functions terminate the execution of the test case immediately. -The cleanup routine will be processed afterwards in a completely automated -way, regardless of the test case's termination reason. -.Pp -.Fn atf_tc_pass -does not take any parameters. -.Fn atf_tc_fail , -.Fn atf_tc_fail_nonfatal -and -.Fn atf_tc_skip -take a format string and a variable list of parameters, which describe, in -a user-friendly manner, why the test case failed or was skipped, -respectively. -It is very important to provide a clear error message in both cases so that -the user can quickly know why the test did not pass. -.Ss Expectations -Everything explained in the previous section changes when the test case -expectations are redefined by the programmer. -.Pp -Each test case has an internal state called -.Sq expect -that describes what the test case expectations are at any point in time. -The value of this property can change during execution by any of: -.Bl -tag -width indent -.It Fn atf_tc_expect_death "reason" "..." -Expects the test case to exit prematurely regardless of the nature of the -exit. -.It Fn atf_tc_expect_exit "exitcode" "reason" "..." -Expects the test case to exit cleanly. -If -.Va exitcode -is not -.Sq -1 , -the runtime engine will validate that the exit code of the test case -matches the one provided in this call. -Otherwise, the exact value will be ignored. -.It Fn atf_tc_expect_fail "reason" "..." -Any failure (be it fatal or non-fatal) raised in this mode is recorded. -However, such failures do not report the test case as failed; instead, the -test case finalizes cleanly and is reported as -.Sq expected failure ; -this report includes the provided -.Fa reason -as part of it. -If no error is raised while running in this mode, then the test case is -reported as -.Sq failed . -.Pp -This mode is useful to reproduce actual known bugs in tests. -Whenever the developer fixes the bug later on, the test case will start -reporting a failure, signaling the developer that the test case must be -adjusted to the new conditions. -In this situation, it is useful, for example, to set -.Fa reason -as the bug number for tracking purposes. -.It Fn atf_tc_expect_pass -This is the normal mode of execution. -In this mode, any failure is reported as such to the user and the test case -is marked as -.Sq failed . -.It Fn atf_tc_expect_signal "signo" "reason" "..." -Expects the test case to terminate due to the reception of a signal. -If -.Va signo -is not -.Sq -1 , -the runtime engine will validate that the signal that terminated the test -case matches the one provided in this call. -Otherwise, the exact value will be ignored. -.It Fn atf_tc_expect_timeout "reason" "..." -Expects the test case to execute for longer than its timeout. -.El -.Ss Helper macros for common checks -The library provides several macros that are very handy in multiple -situations. -These basically check some condition after executing a given statement or -processing a given expression and, if the condition is not met, they -report the test case as failed. -.Pp -The -.Sq REQUIRE -variant of the macros immediately abort the test case as soon as an error -condition is detected by calling the -.Fn atf_tc_fail -function. -Use this variant whenever it makes no sense to continue the execution of a -test case when the checked condition is not met. -The -.Sq CHECK -variant, on the other hand, reports a failure as soon as it is encountered -using the -.Fn atf_tc_fail_nonfatal -function, but the execution of the test case continues as if nothing had -happened. -Use this variant whenever the checked condition is important as a result of -the test case, but there are other conditions that can be subsequently -checked on the same run without aborting. -.Pp -Additionally, the -.Sq MSG -variants take an extra set of parameters to explicitly specify the failure -message. -This failure message is formatted according to the -.Xr printf 3 -formatters. -.Pp -.Fn ATF_CHECK , -.Fn ATF_CHECK_MSG , -.Fn ATF_REQUIRE -and -.Fn ATF_REQUIRE_MSG -take an expression and fail if the expression evaluates to false. -.Pp -.Fn ATF_CHECK_EQ , -.Fn ATF_CHECK_EQ_MSG , -.Fn ATF_REQUIRE_EQ -and -.Fn ATF_REQUIRE_EQ_MSG -take two expressions and fail if the two evaluated values are not equal. -.Pp -.Fn ATF_CHECK_MATCH , -.Fn ATF_CHECK_MATCH_MSG , -.Fn ATF_REQUIRE_MATCH -and -.Fn ATF_REQUIRE_MATCH_MSG -take a regular expression and a string and fail if the regular expression does -not match the given string. -Note that the regular expression is not anchored, so it will match anywhere in -the string. -.Pp -.Fn ATF_CHECK_STREQ , -.Fn ATF_CHECK_STREQ_MSG , -.Fn ATF_REQUIRE_STREQ -and -.Fn ATF_REQUIRE_STREQ_MSG -take two strings and fail if the two are not equal character by character. -.Pp -.Fn ATF_CHECK_ERRNO -and -.Fn ATF_REQUIRE_ERRNO -take, first, the error code that the check is expecting to find in the -.Va errno -variable and, second, a boolean expression that, if evaluates to true, -means that a call failed and -.Va errno -has to be checked against the first value. -.Ss Utility functions -The following functions are provided as part of the -.Nm -API to simplify the creation of a variety of tests. -In particular, these are useful to write tests for command-line interfaces. -.Pp -.Ft void -.Fo atf_utils_cat_file -.Fa "const char *file" -.Fa "const char *prefix" -.Fc -.Bd -ragged -offset indent -Prints the contents of -.Fa file -to the standard output, prefixing every line with the string in -.Fa prefix . -.Ed -.Pp -.Ft bool -.Fo atf_utils_compare_file -.Fa "const char *file" -.Fa "const char *contents" -.Fc -.Bd -ragged -offset indent -Returns true if the given -.Fa file -matches exactly the expected inlined -.Fa contents . -.Ed -.Pp -.Ft void -.Fo atf_utils_copy_file -.Fa "const char *source" -.Fa "const char *destination" -.Fc -.Bd -ragged -offset indent -Copies the file -.Fa source -to -.Fa destination . -The permissions of the file are preserved during the code. -.Ed -.Pp -.Ft void -.Fo atf_utils_create_file -.Fa "const char *file" -.Fa "const char *contents" -.Fa "..." -.Fc -.Bd -ragged -offset indent -Creates -.Fa file -with the text given in -.Fa contents , -which is a formatting string that uses the rest of the variable arguments. -.Ed -.Pp -.Ft void -.Fo atf_utils_file_exists -.Fa "const char *file" -.Fc -.Bd -ragged -offset indent -Checks if -.Fa file -exists. -.Ed -.Pp -.Ft pid_t -.Fo atf_utils_fork -.Fa "void" -.Fc -.Bd -ragged -offset indent -Forks a process and redirects the standard output and standard error of the -child to files for later validation with -.Fn atf_utils_wait . -Fails the test case if the fork fails, so this does not return an error. -.Ed -.Pp -.Ft void -.Fo atf_utils_free_charpp -.Fa "char **argv" -.Fc -.Bd -ragged -offset indent -Frees a dynamically-allocated array of dynamically-allocated strings. -.Ed -.Pp -.Ft bool -.Fo atf_utils_grep_file -.Fa "const char *regexp" -.Fa "const char *file" -.Fa "..." -.Fc -.Bd -ragged -offset indent -Searches for the -.Fa regexp , -which is a formatting string representing the regular expression, -in the -.Fa file . -The variable arguments are used to construct the regular expression. -.Ed -.Pp -.Ft bool -.Fo atf_utils_grep_string -.Fa "const char *regexp" -.Fa "const char *str" -.Fa "..." -.Fc -.Bd -ragged -offset indent -Searches for the -.Fa regexp , -which is a formatting string representing the regular expression, -in the literal string -.Fa str . -The variable arguments are used to construct the regular expression. -.Ed -.Pp -.Ft char * -.Fo atf_utils_readline -.Fa "int fd" -.Fc -.Bd -ragged -offset indent -Reads a line from the file descriptor -.Fa fd . -The line, if any, is returned as a dynamically-allocated buffer that must be -released with -.Xr free 3 . -If there was nothing to read, returns -.Sq NULL . -.Ed -.Pp -.Ft void -.Fo atf_utils_redirect -.Fa "const int fd" -.Fa "const char *file" -.Fc -.Bd -ragged -offset indent -Redirects the given file descriptor -.Fa fd -to -.Fa file . -This function exits the process in case of an error and does not properly mark -the test case as failed. -As a result, it should only be used in subprocesses of the test case; specially -those spawned by -.Fn atf_utils_fork . -.Ed -.Pp -.Ft void -.Fo atf_utils_wait -.Fa "const pid_t pid" -.Fa "const int expected_exit_status" -.Fa "const char *expected_stdout" -.Fa "const char *expected_stderr" -.Fc -.Bd -ragged -offset indent -Waits and validates the result of a subprocess spawned with -.Fn atf_utils_wait . -The validation involves checking that the subprocess exited cleanly and returned -the code specified in -.Fa expected_exit_status -and that its standard output and standard error match the strings given in -.Fa expected_stdout -and -.Fa expected_stderr . -.Pp -If any of the -.Fa expected_stdout -or -.Fa expected_stderr -strings are prefixed with -.Sq save: , -then they specify the name of the file into which to store the stdout or stderr -of the subprocess, and no comparison is performed. -.Ed -.Sh EXAMPLES -The following shows a complete test program with a single test case that -validates the addition operator: -.Bd -literal -offset indent -#include - -ATF_TC(addition); -ATF_TC_HEAD(addition, tc) -{ - atf_tc_set_md_var(tc, "descr", - "Sample tests for the addition operator"); -} -ATF_TC_BODY(addition, tc) -{ - ATF_CHECK_EQ(0 + 0, 0); - ATF_CHECK_EQ(0 + 1, 1); - ATF_CHECK_EQ(1 + 0, 1); - - ATF_CHECK_EQ(1 + 1, 2); - - ATF_CHECK_EQ(100 + 200, 300); -} - -ATF_TC(string_formatting); -ATF_TC_HEAD(string_formatting, tc) -{ - atf_tc_set_md_var(tc, "descr", - "Sample tests for the snprintf"); -} -ATF_TC_BODY(string_formatting, tc) -{ - char buf[1024]; - snprintf(buf, sizeof(buf), "a %s", "string"); - ATF_CHECK_STREQ_MSG("a string", buf, "%s is not working"); -} - -ATF_TC(open_failure); -ATF_TC_HEAD(open_failure, tc) -{ - atf_tc_set_md_var(tc, "descr", - "Sample tests for the open function"); -} -ATF_TC_BODY(open_failure, tc) -{ - ATF_CHECK_ERRNO(ENOENT, open("non-existent", O_RDONLY) == -1); -} - -ATF_TC(known_bug); -ATF_TC_HEAD(known_bug, tc) -{ - atf_tc_set_md_var(tc, "descr", - "Reproduces a known bug"); -} -ATF_TC_BODY(known_bug, tc) -{ - atf_tc_expect_fail("See bug number foo/bar"); - ATF_CHECK_EQ(3, 1 + 1); - atf_tc_expect_pass(); - ATF_CHECK_EQ(3, 1 + 2); -} - -ATF_TP_ADD_TCS(tp) -{ - ATF_TP_ADD_TC(tp, addition); - ATF_TP_ADD_TC(tp, string_formatting); - ATF_TP_ADD_TC(tp, open_failure); - ATF_TP_ADD_TC(tp, known_bug); - - return atf_no_error(); -} -.Ed -.Sh SEE ALSO -.Xr atf-test-program 1 , -.Xr atf-test-case 4 Index: stable/10/contrib/atf/atf-c/atf-c.3 =================================================================== --- stable/10/contrib/atf/atf-c/atf-c.3 (revision 315182) +++ stable/10/contrib/atf/atf-c/atf-c.3 (revision 315183) @@ -1,793 +1,793 @@ .\" Copyright (c) 2008 The NetBSD Foundation, Inc. .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND .\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF .\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. .\" IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY .\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE .\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER .\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN .\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -.Dd October 13, 2014 +.Dd March 6, 2017 .Dt ATF-C 3 .Os .Sh NAME .Nm atf-c , .Nm ATF_CHECK , .Nm ATF_CHECK_MSG , .Nm ATF_CHECK_EQ , .Nm ATF_CHECK_EQ_MSG , .Nm ATF_CHECK_MATCH , .Nm ATF_CHECK_MATCH_MSG , .Nm ATF_CHECK_STREQ , .Nm ATF_CHECK_STREQ_MSG , .Nm ATF_CHECK_ERRNO , .Nm ATF_REQUIRE , .Nm ATF_REQUIRE_MSG , .Nm ATF_REQUIRE_EQ , .Nm ATF_REQUIRE_EQ_MSG , .Nm ATF_REQUIRE_MATCH , .Nm ATF_REQUIRE_MATCH_MSG , .Nm ATF_REQUIRE_STREQ , .Nm ATF_REQUIRE_STREQ_MSG , .Nm ATF_REQUIRE_ERRNO , .Nm ATF_TC , .Nm ATF_TC_BODY , .Nm ATF_TC_BODY_NAME , .Nm ATF_TC_CLEANUP , .Nm ATF_TC_CLEANUP_NAME , .Nm ATF_TC_HEAD , .Nm ATF_TC_HEAD_NAME , .Nm ATF_TC_NAME , .Nm ATF_TC_WITH_CLEANUP , .Nm ATF_TC_WITHOUT_HEAD , .Nm ATF_TP_ADD_TC , .Nm ATF_TP_ADD_TCS , .Nm atf_tc_get_config_var , .Nm atf_tc_get_config_var_wd , .Nm atf_tc_get_config_var_as_bool , .Nm atf_tc_get_config_var_as_bool_wd , .Nm atf_tc_get_config_var_as_long , .Nm atf_tc_get_config_var_as_long_wd , .Nm atf_no_error , .Nm atf_tc_expect_death , .Nm atf_tc_expect_exit , .Nm atf_tc_expect_fail , .Nm atf_tc_expect_pass , .Nm atf_tc_expect_signal , .Nm atf_tc_expect_timeout , .Nm atf_tc_fail , .Nm atf_tc_fail_nonfatal , .Nm atf_tc_pass , .Nm atf_tc_skip , .Nm atf_utils_cat_file , .Nm atf_utils_compare_file , .Nm atf_utils_copy_file , .Nm atf_utils_create_file , .Nm atf_utils_file_exists , .Nm atf_utils_fork , .Nm atf_utils_free_charpp , .Nm atf_utils_grep_file , .Nm atf_utils_grep_string , .Nm atf_utils_readline , .Nm atf_utils_redirect , .Nm atf_utils_wait .Nd C API to write ATF-based test programs .Sh SYNOPSIS .In atf-c.h .\" NO_CHECK_STYLE_BEGIN .Fn ATF_CHECK "expression" .Fn ATF_CHECK_MSG "expression" "fail_msg_fmt" ... .Fn ATF_CHECK_EQ "expected_expression" "actual_expression" .Fn ATF_CHECK_EQ_MSG "expected_expression" "actual_expression" "fail_msg_fmt" ... .Fn ATF_CHECK_MATCH "regexp" "string" .Fn ATF_CHECK_MATCH_MSG "regexp" "string" "fail_msg_fmt" ... .Fn ATF_CHECK_STREQ "string_1" "string_2" .Fn ATF_CHECK_STREQ_MSG "string_1" "string_2" "fail_msg_fmt" ... .Fn ATF_CHECK_ERRNO "expected_errno" "bool_expression" .Fn ATF_REQUIRE "expression" .Fn ATF_REQUIRE_MSG "expression" "fail_msg_fmt" ... .Fn ATF_REQUIRE_EQ "expected_expression" "actual_expression" .Fn ATF_REQUIRE_EQ_MSG "expected_expression" "actual_expression" "fail_msg_fmt" ... .Fn ATF_REQUIRE_MATCH "regexp" "string" .Fn ATF_REQUIRE_MATCH_MSG "regexp" "string" "fail_msg_fmt" ... .Fn ATF_REQUIRE_STREQ "expected_string" "actual_string" .Fn ATF_REQUIRE_STREQ_MSG "expected_string" "actual_string" "fail_msg_fmt" ... .Fn ATF_REQUIRE_ERRNO "expected_errno" "bool_expression" .\" NO_CHECK_STYLE_END .Fn ATF_TC "name" .Fn ATF_TC_BODY "name" "tc" .Fn ATF_TC_BODY_NAME "name" .Fn ATF_TC_CLEANUP "name" "tc" .Fn ATF_TC_CLEANUP_NAME "name" .Fn ATF_TC_HEAD "name" "tc" .Fn ATF_TC_HEAD_NAME "name" .Fn ATF_TC_NAME "name" .Fn ATF_TC_WITH_CLEANUP "name" .Fn ATF_TC_WITHOUT_HEAD "name" .Fn ATF_TP_ADD_TC "tp_name" "tc_name" .Fn ATF_TP_ADD_TCS "tp_name" .Fn atf_tc_get_config_var "tc" "varname" .Fn atf_tc_get_config_var_wd "tc" "variable_name" "default_value" .Fn atf_tc_get_config_var_as_bool "tc" "variable_name" .Fn atf_tc_get_config_var_as_bool_wd "tc" "variable_name" "default_value" .Fn atf_tc_get_config_var_as_long "tc" "variable_name" .Fn atf_tc_get_config_var_as_long_wd "tc" "variable_name" "default_value" .Fn atf_no_error .Fn atf_tc_expect_death "reason" "..." .Fn atf_tc_expect_exit "exitcode" "reason" "..." .Fn atf_tc_expect_fail "reason" "..." .Fn atf_tc_expect_pass .Fn atf_tc_expect_signal "signo" "reason" "..." .Fn atf_tc_expect_timeout "reason" "..." .Fn atf_tc_fail "reason" .Fn atf_tc_fail_nonfatal "reason" .Fn atf_tc_pass .Fn atf_tc_skip "reason" .Ft void .Fo atf_utils_cat_file .Fa "const char *file" .Fa "const char *prefix" .Fc .Ft bool .Fo atf_utils_compare_file .Fa "const char *file" .Fa "const char *contents" .Fc .Ft void .Fo atf_utils_copy_file .Fa "const char *source" .Fa "const char *destination" .Fc .Ft void .Fo atf_utils_create_file .Fa "const char *file" .Fa "const char *contents" .Fa "..." .Fc .Ft void .Fo atf_utils_file_exists .Fa "const char *file" .Fc .Ft pid_t .Fo atf_utils_fork .Fa "void" .Fc .Ft void .Fo atf_utils_free_charpp .Fa "char **argv" .Fc .Ft bool .Fo atf_utils_grep_file .Fa "const char *regexp" .Fa "const char *file" .Fa "..." .Fc .Ft bool .Fo atf_utils_grep_string .Fa "const char *regexp" .Fa "const char *str" .Fa "..." .Fc .Ft char * .Fo atf_utils_readline .Fa "int fd" .Fc .Ft void .Fo atf_utils_redirect .Fa "const int fd" .Fa "const char *file" .Fc .Ft void .Fo atf_utils_wait .Fa "const pid_t pid" .Fa "const int expected_exit_status" .Fa "const char *expected_stdout" .Fa "const char *expected_stderr" .Fc .Sh DESCRIPTION ATF provides a C programming interface to implement test programs. C-based test programs follow this template: .Bd -literal -offset indent -.Ns ... C-specific includes go here ... +\&... C-specific includes go here ... #include ATF_TC(tc1); ATF_TC_HEAD(tc1, tc) { ... first test case's header ... } ATF_TC_BODY(tc1, tc) { ... first test case's body ... } ATF_TC_WITH_CLEANUP(tc2); ATF_TC_HEAD(tc2, tc) { ... second test case's header ... } ATF_TC_BODY(tc2, tc) { ... second test case's body ... } ATF_TC_CLEANUP(tc2, tc) { ... second test case's cleanup ... } ATF_TC_WITHOUT_HEAD(tc3); ATF_TC_BODY(tc3, tc) { ... third test case's body ... } -.Ns ... additional test cases ... +\&... additional test cases ... ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tcs, tc1); ATF_TP_ADD_TC(tcs, tc2); ATF_TP_ADD_TC(tcs, tc3); ... add additional test cases ... return atf_no_error(); } .Ed .Ss Definition of test cases Test cases have an identifier and are composed of three different parts: the header, the body and an optional cleanup routine, all of which are described in .Xr atf-test-case 4 . To define test cases, one can use the .Fn ATF_TC , .Fn ATF_TC_WITH_CLEANUP or the .Fn ATF_TC_WITHOUT_HEAD -macros, which take a single parameter specifiying the test case's name. +macros, which take a single parameter specifying the test case's name. .Fn ATF_TC , requires to define a head and a body for the test case, .Fn ATF_TC_WITH_CLEANUP requires to define a head, a body and a cleanup for the test case and .Fn ATF_TC_WITHOUT_HEAD requires only a body for the test case. It is important to note that these .Em do not set the test case up for execution when the program is run. In order to do so, a later registration is needed with the .Fn ATF_TP_ADD_TC macro detailed in .Sx Program initialization . .Pp Later on, one must define the three parts of the body by means of three functions. Their headers are given by the .Fn ATF_TC_HEAD , .Fn ATF_TC_BODY and .Fn ATF_TC_CLEANUP macros, all of which take the test case name provided to the .Fn ATF_TC .Fn ATF_TC_WITH_CLEANUP , or .Fn ATF_TC_WITHOUT_HEAD macros and the name of the variable that will hold a pointer to the test case data. Following each of these, a block of code is expected, surrounded by the opening and closing brackets. .Ss Program initialization The library provides a way to easily define the test program's .Fn main function. You should never define one on your own, but rely on the library to do it for you. This is done by using the .Fn ATF_TP_ADD_TCS macro, which is passed the name of the object that will hold the test -cases; i.e. the test program instance. +cases, i.e., the test program instance. This name can be whatever you want as long as it is a valid variable identifier. .Pp After the macro, you are supposed to provide the body of a function, which should only use the .Fn ATF_TP_ADD_TC macro to register the test cases the test program will execute and return a success error code. The first parameter of this macro matches the name you provided in the former call. The success status can be returned using the .Fn atf_no_error function. .Ss Header definitions The test case's header can define the meta-data by using the .Fn atf_tc_set_md_var method, which takes three parameters: the first one points to the test case data, the second one specifies the meta-data variable to be set and the third one specifies its value. Both of them are strings. .Ss Configuration variables The test case has read-only access to the current configuration variables by means of the .Ft bool .Fn atf_tc_has_config_var , .Ft const char * .Fn atf_tc_get_config_var , .Ft const char * .Fn atf_tc_get_config_var_wd , .Ft bool .Fn atf_tc_get_config_var_as_bool , .Ft bool .Fn atf_tc_get_config_var_as_bool_wd , .Ft long .Fn atf_tc_get_config_var_as_long , and the .Ft long .Fn atf_tc_get_config_var_as_long_wd functions, which can be called in any of the three parts of a test case. .Pp The .Sq _wd variants take a default value for the variable which is returned if the variable is not defined. The other functions without the .Sq _wd suffix .Em require the variable to be defined. .Ss Access to the source directory It is possible to get the path to the test case's source directory from any of its three components by querying the .Sq srcdir configuration variable. .Ss Requiring programs Aside from the .Va require.progs meta-data variable available in the header only, one can also check for additional programs in the test case's body by using the .Fn atf_tc_require_prog function, which takes the base name or full path of a single binary. Relative paths are forbidden. If it is not found, the test case will be automatically skipped. .Ss Test case finalization The test case finalizes either when the body reaches its end, at which point the test is assumed to have .Em passed , unless any non-fatal errors were raised using .Fn atf_tc_fail_nonfatal , or at any explicit call to .Fn atf_tc_pass , .Fn atf_tc_fail or .Fn atf_tc_skip . These three functions terminate the execution of the test case immediately. The cleanup routine will be processed afterwards in a completely automated way, regardless of the test case's termination reason. .Pp .Fn atf_tc_pass does not take any parameters. .Fn atf_tc_fail , .Fn atf_tc_fail_nonfatal and .Fn atf_tc_skip take a format string and a variable list of parameters, which describe, in a user-friendly manner, why the test case failed or was skipped, respectively. It is very important to provide a clear error message in both cases so that the user can quickly know why the test did not pass. .Ss Expectations Everything explained in the previous section changes when the test case expectations are redefined by the programmer. .Pp Each test case has an internal state called .Sq expect that describes what the test case expectations are at any point in time. The value of this property can change during execution by any of: .Bl -tag -width indent .It Fn atf_tc_expect_death "reason" "..." Expects the test case to exit prematurely regardless of the nature of the exit. .It Fn atf_tc_expect_exit "exitcode" "reason" "..." Expects the test case to exit cleanly. If .Va exitcode is not .Sq -1 , the runtime engine will validate that the exit code of the test case matches the one provided in this call. Otherwise, the exact value will be ignored. .It Fn atf_tc_expect_fail "reason" "..." Any failure (be it fatal or non-fatal) raised in this mode is recorded. However, such failures do not report the test case as failed; instead, the test case finalizes cleanly and is reported as .Sq expected failure ; this report includes the provided .Fa reason as part of it. If no error is raised while running in this mode, then the test case is reported as .Sq failed . .Pp This mode is useful to reproduce actual known bugs in tests. Whenever the developer fixes the bug later on, the test case will start reporting a failure, signaling the developer that the test case must be adjusted to the new conditions. In this situation, it is useful, for example, to set .Fa reason as the bug number for tracking purposes. .It Fn atf_tc_expect_pass This is the normal mode of execution. In this mode, any failure is reported as such to the user and the test case is marked as .Sq failed . .It Fn atf_tc_expect_signal "signo" "reason" "..." Expects the test case to terminate due to the reception of a signal. If .Va signo is not .Sq -1 , the runtime engine will validate that the signal that terminated the test case matches the one provided in this call. Otherwise, the exact value will be ignored. .It Fn atf_tc_expect_timeout "reason" "..." Expects the test case to execute for longer than its timeout. .El .Ss Helper macros for common checks The library provides several macros that are very handy in multiple situations. These basically check some condition after executing a given statement or processing a given expression and, if the condition is not met, they report the test case as failed. .Pp The .Sq REQUIRE variant of the macros immediately abort the test case as soon as an error condition is detected by calling the .Fn atf_tc_fail function. Use this variant whenever it makes no sense to continue the execution of a test case when the checked condition is not met. The .Sq CHECK variant, on the other hand, reports a failure as soon as it is encountered using the .Fn atf_tc_fail_nonfatal function, but the execution of the test case continues as if nothing had happened. Use this variant whenever the checked condition is important as a result of the test case, but there are other conditions that can be subsequently checked on the same run without aborting. .Pp Additionally, the .Sq MSG variants take an extra set of parameters to explicitly specify the failure message. This failure message is formatted according to the .Xr printf 3 formatters. .Pp .Fn ATF_CHECK , .Fn ATF_CHECK_MSG , .Fn ATF_REQUIRE and .Fn ATF_REQUIRE_MSG take an expression and fail if the expression evaluates to false. .Pp .Fn ATF_CHECK_EQ , .Fn ATF_CHECK_EQ_MSG , .Fn ATF_REQUIRE_EQ and .Fn ATF_REQUIRE_EQ_MSG take two expressions and fail if the two evaluated values are not equal. The common style is to put the expected value in the first parameter and the actual value in the second parameter. .Pp .Fn ATF_CHECK_MATCH , .Fn ATF_CHECK_MATCH_MSG , .Fn ATF_REQUIRE_MATCH and .Fn ATF_REQUIRE_MATCH_MSG take a regular expression and a string and fail if the regular expression does not match the given string. Note that the regular expression is not anchored, so it will match anywhere in the string. .Pp .Fn ATF_CHECK_STREQ , .Fn ATF_CHECK_STREQ_MSG , .Fn ATF_REQUIRE_STREQ and .Fn ATF_REQUIRE_STREQ_MSG take two strings and fail if the two are not equal character by character. The common style is to put the expected string in the first parameter and the actual string in the second parameter. .Pp .Fn ATF_CHECK_ERRNO and .Fn ATF_REQUIRE_ERRNO take, first, the error code that the check is expecting to find in the .Va errno variable and, second, a boolean expression that, if evaluates to true, means that a call failed and .Va errno has to be checked against the first value. .Ss Utility functions The following functions are provided as part of the .Nm API to simplify the creation of a variety of tests. In particular, these are useful to write tests for command-line interfaces. .Pp .Ft void .Fo atf_utils_cat_file .Fa "const char *file" .Fa "const char *prefix" .Fc .Bd -ragged -offset indent Prints the contents of .Fa file to the standard output, prefixing every line with the string in .Fa prefix . .Ed .Pp .Ft bool .Fo atf_utils_compare_file .Fa "const char *file" .Fa "const char *contents" .Fc .Bd -ragged -offset indent Returns true if the given .Fa file matches exactly the expected inlined .Fa contents . .Ed .Pp .Ft void .Fo atf_utils_copy_file .Fa "const char *source" .Fa "const char *destination" .Fc .Bd -ragged -offset indent Copies the file .Fa source to .Fa destination . The permissions of the file are preserved during the code. .Ed .Pp .Ft void .Fo atf_utils_create_file .Fa "const char *file" .Fa "const char *contents" .Fa "..." .Fc .Bd -ragged -offset indent Creates .Fa file with the text given in .Fa contents , which is a formatting string that uses the rest of the variable arguments. .Ed .Pp .Ft void .Fo atf_utils_file_exists .Fa "const char *file" .Fc .Bd -ragged -offset indent Checks if .Fa file exists. .Ed .Pp .Ft pid_t .Fo atf_utils_fork .Fa "void" .Fc .Bd -ragged -offset indent Forks a process and redirects the standard output and standard error of the child to files for later validation with .Fn atf_utils_wait . Fails the test case if the fork fails, so this does not return an error. .Ed .Pp .Ft void .Fo atf_utils_free_charpp .Fa "char **argv" .Fc .Bd -ragged -offset indent Frees a dynamically-allocated array of dynamically-allocated strings. .Ed .Pp .Ft bool .Fo atf_utils_grep_file .Fa "const char *regexp" .Fa "const char *file" .Fa "..." .Fc .Bd -ragged -offset indent Searches for the .Fa regexp , which is a formatting string representing the regular expression, in the .Fa file . The variable arguments are used to construct the regular expression. .Ed .Pp .Ft bool .Fo atf_utils_grep_string .Fa "const char *regexp" .Fa "const char *str" .Fa "..." .Fc .Bd -ragged -offset indent Searches for the .Fa regexp , which is a formatting string representing the regular expression, in the literal string .Fa str . The variable arguments are used to construct the regular expression. .Ed .Pp .Ft char * .Fo atf_utils_readline .Fa "int fd" .Fc .Bd -ragged -offset indent Reads a line from the file descriptor .Fa fd . The line, if any, is returned as a dynamically-allocated buffer that must be released with .Xr free 3 . If there was nothing to read, returns .Sq NULL . .Ed .Pp .Ft void .Fo atf_utils_redirect .Fa "const int fd" .Fa "const char *file" .Fc .Bd -ragged -offset indent Redirects the given file descriptor .Fa fd to .Fa file . This function exits the process in case of an error and does not properly mark the test case as failed. As a result, it should only be used in subprocesses of the test case; specially those spawned by .Fn atf_utils_fork . .Ed .Pp .Ft void .Fo atf_utils_wait .Fa "const pid_t pid" .Fa "const int expected_exit_status" .Fa "const char *expected_stdout" .Fa "const char *expected_stderr" .Fc .Bd -ragged -offset indent Waits and validates the result of a subprocess spawned with .Fn atf_utils_wait . The validation involves checking that the subprocess exited cleanly and returned the code specified in .Fa expected_exit_status and that its standard output and standard error match the strings given in .Fa expected_stdout and .Fa expected_stderr . .Pp If any of the .Fa expected_stdout or .Fa expected_stderr strings are prefixed with .Sq save: , then they specify the name of the file into which to store the stdout or stderr of the subprocess, and no comparison is performed. .Ed .Sh ENVIRONMENT The following variables are recognized by .Nm but should not be overridden other than for testing purposes: .Pp .Bl -tag -width ATFXBUILDXCXXFLAGSXX -compact .It Va ATF_BUILD_CC Path to the C compiler. .It Va ATF_BUILD_CFLAGS C compiler flags. .It Va ATF_BUILD_CPP Path to the C/C++ preprocessor. .It Va ATF_BUILD_CPPFLAGS C/C++ preprocessor flags. .It Va ATF_BUILD_CXX Path to the C++ compiler. .It Va ATF_BUILD_CXXFLAGS C++ compiler flags. .El .Sh EXAMPLES The following shows a complete test program with a single test case that validates the addition operator: .Bd -literal -offset indent #include ATF_TC(addition); ATF_TC_HEAD(addition, tc) { atf_tc_set_md_var(tc, "descr", "Sample tests for the addition operator"); } ATF_TC_BODY(addition, tc) { ATF_CHECK_EQ(0, 0 + 0); ATF_CHECK_EQ(1, 0 + 1); ATF_CHECK_EQ(1, 1 + 0); ATF_CHECK_EQ(2, 1 + 1); ATF_CHECK_EQ(300, 100 + 200); } ATF_TC(string_formatting); ATF_TC_HEAD(string_formatting, tc) { atf_tc_set_md_var(tc, "descr", "Sample tests for the snprintf"); } ATF_TC_BODY(string_formatting, tc) { char buf[1024]; snprintf(buf, sizeof(buf), "a %s", "string"); ATF_CHECK_STREQ_MSG("a string", buf, "%s is not working"); } ATF_TC(open_failure); ATF_TC_HEAD(open_failure, tc) { atf_tc_set_md_var(tc, "descr", "Sample tests for the open function"); } ATF_TC_BODY(open_failure, tc) { ATF_CHECK_ERRNO(ENOENT, open("non-existent", O_RDONLY) == -1); } ATF_TC(known_bug); ATF_TC_HEAD(known_bug, tc) { atf_tc_set_md_var(tc, "descr", "Reproduces a known bug"); } ATF_TC_BODY(known_bug, tc) { atf_tc_expect_fail("See bug number foo/bar"); ATF_CHECK_EQ(3, 1 + 1); atf_tc_expect_pass(); ATF_CHECK_EQ(3, 1 + 2); } ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, addition); ATF_TP_ADD_TC(tp, string_formatting); ATF_TP_ADD_TC(tp, open_failure); ATF_TP_ADD_TC(tp, known_bug); return atf_no_error(); } .Ed .Sh SEE ALSO .Xr atf-test-program 1 , .Xr atf-test-case 4 Index: stable/10/contrib/atf/atf-c++/atf-c++-api.3 =================================================================== --- stable/10/contrib/atf/atf-c++/atf-c++-api.3 (revision 315182) +++ stable/10/contrib/atf/atf-c++/atf-c++-api.3 (nonexistent) @@ -1,632 +0,0 @@ -.\" -.\" Automated Testing Framework (atf) -.\" -.\" Copyright (c) 2008 The NetBSD Foundation, Inc. -.\" All rights reserved. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND -.\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, -.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. -.\" IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY -.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE -.\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER -.\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN -.\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -.\" -.Dd March 2, 2014 -.Dt ATF-C++-API 3 -.Os -.Sh NAME -.Nm atf-c++-api , -.Nm ATF_ADD_TEST_CASE , -.Nm ATF_CHECK_ERRNO , -.Nm ATF_FAIL , -.Nm ATF_INIT_TEST_CASES , -.Nm ATF_PASS , -.Nm ATF_REQUIRE , -.Nm ATF_REQUIRE_EQ , -.Nm ATF_REQUIRE_ERRNO , -.Nm ATF_REQUIRE_IN , -.Nm ATF_REQUIRE_MATCH , -.Nm ATF_REQUIRE_NOT_IN , -.Nm ATF_REQUIRE_THROW , -.Nm ATF_REQUIRE_THROW_RE , -.Nm ATF_SKIP , -.Nm ATF_TEST_CASE , -.Nm ATF_TEST_CASE_BODY , -.Nm ATF_TEST_CASE_CLEANUP , -.Nm ATF_TEST_CASE_HEAD , -.Nm ATF_TEST_CASE_NAME , -.Nm ATF_TEST_CASE_USE , -.Nm ATF_TEST_CASE_WITH_CLEANUP , -.Nm ATF_TEST_CASE_WITHOUT_HEAD , -.Nm atf::utils::cat_file , -.Nm atf::utils::compare_file , -.Nm atf::utils::copy_file , -.Nm atf::utils::create_file , -.Nm atf::utils::file_exists , -.Nm atf::utils::fork , -.Nm atf::utils::grep_collection , -.Nm atf::utils::grep_file , -.Nm atf::utils::grep_string , -.Nm atf::utils::redirect , -.Nm atf::utils::wait -.Nd C++ API to write ATF-based test programs -.Sh SYNOPSIS -.In atf-c++.hpp -.Fn ATF_ADD_TEST_CASE "tcs" "name" -.Fn ATF_CHECK_ERRNO "exp_errno" "bool_expression" -.Fn ATF_FAIL "reason" -.Fn ATF_INIT_TEST_CASES "tcs" -.Fn ATF_PASS -.Fn ATF_REQUIRE "expression" -.Fn ATF_REQUIRE_EQ "expression_1" "expression_2" -.Fn ATF_REQUIRE_ERRNO "exp_errno" "bool_expression" -.Fn ATF_REQUIRE_IN "element" "collection" -.Fn ATF_REQUIRE_MATCH "regexp" "string_expression" -.Fn ATF_REQUIRE_NOT_IN "element" "collection" -.Fn ATF_REQUIRE_THROW "expected_exception" "statement" -.Fn ATF_REQUIRE_THROW_RE "expected_exception" "regexp" "statement" -.Fn ATF_SKIP "reason" -.Fn ATF_TEST_CASE "name" -.Fn ATF_TEST_CASE_BODY "name" -.Fn ATF_TEST_CASE_CLEANUP "name" -.Fn ATF_TEST_CASE_HEAD "name" -.Fn ATF_TEST_CASE_NAME "name" -.Fn ATF_TEST_CASE_USE "name" -.Fn ATF_TEST_CASE_WITH_CLEANUP "name" -.Fn ATF_TEST_CASE_WITHOUT_HEAD "name" -.Ft void -.Fo atf::utils::cat_file -.Fa "const std::string& path" -.Fa "const std::string& prefix" -.Fc -.Ft bool -.Fo atf::utils::compare_file -.Fa "const std::string& path" -.Fa "const std::string& contents" -.Fc -.Ft void -.Fo atf::utils::copy_file -.Fa "const std::string& source" -.Fa "const std::string& destination" -.Fc -.Ft void -.Fo atf::utils::create_file -.Fa "const std::string& path" -.Fa "const std::string& contents" -.Fc -.Ft void -.Fo atf::utils::file_exists -.Fa "const std::string& path" -.Fc -.Ft pid_t -.Fo atf::utils::fork -.Fa "void" -.Fc -.Ft bool -.Fo atf::utils::grep_collection -.Fa "const std::string& regexp" -.Fa "const Collection& collection" -.Fc -.Ft bool -.Fo atf::utils::grep_file -.Fa "const std::string& regexp" -.Fa "const std::string& path" -.Fc -.Ft bool -.Fo atf::utils::grep_string -.Fa "const std::string& regexp" -.Fa "const std::string& path" -.Fc -.Ft void -.Fo atf::utils::redirect -.Fa "const int fd" -.Fa "const std::string& path" -.Fc -.Ft void -.Fo atf::utils::wait -.Fa "const pid_t pid" -.Fa "const int expected_exit_status" -.Fa "const std::string& expected_stdout" -.Fa "const std::string& expected_stderr" -.Fc -.Sh DESCRIPTION -ATF provides a C++ programming interface to implement test programs. -C++-based test programs follow this template: -.Bd -literal -offset indent -extern "C" { -.Ns ... C-specific includes go here ... -} - -.Ns ... C++-specific includes go here ... - -#include - -ATF_TEST_CASE(tc1); -ATF_TEST_CASE_HEAD(tc1) -{ - ... first test case's header ... -} -ATF_TEST_CASE_BODY(tc1) -{ - ... first test case's body ... -} - -ATF_TEST_CASE_WITH_CLEANUP(tc2); -ATF_TEST_CASE_HEAD(tc2) -{ - ... second test case's header ... -} -ATF_TEST_CASE_BODY(tc2) -{ - ... second test case's body ... -} -ATF_TEST_CASE_CLEANUP(tc2) -{ - ... second test case's cleanup ... -} - -ATF_TEST_CASE(tc3); -ATF_TEST_CASE_BODY(tc3) -{ - ... third test case's body ... -} - -.Ns ... additional test cases ... - -ATF_INIT_TEST_CASES(tcs) -{ - ATF_ADD_TEST_CASE(tcs, tc1); - ATF_ADD_TEST_CASE(tcs, tc2); - ATF_ADD_TEST_CASE(tcs, tc3); - ... add additional test cases ... -} -.Ed -.Ss Definition of test cases -Test cases have an identifier and are composed of three different parts: -the header, the body and an optional cleanup routine, all of which are -described in -.Xr atf-test-case 4 . -To define test cases, one can use the -.Fn ATF_TEST_CASE , -.Fn ATF_TEST_CASE_WITH_CLEANUP -or the -.Fn ATF_TEST_CASE_WITHOUT_HEAD -macros, which take a single parameter specifiying the test case's -name. -.Fn ATF_TEST_CASE , -requires to define a head and a body for the test case, -.Fn ATF_TEST_CASE_WITH_CLEANUP -requires to define a head, a body and a cleanup for the test case and -.Fn ATF_TEST_CASE_WITHOUT_HEAD -requires only a body for the test case. -It is important to note that these -.Em do not -set the test case up for execution when the program is run. -In order to do so, a later registration is needed through the -.Fn ATF_ADD_TEST_CASE -macro detailed in -.Sx Program initialization . -.Pp -Later on, one must define the three parts of the body by means of three -functions. -Their headers are given by the -.Fn ATF_TEST_CASE_HEAD , -.Fn ATF_TEST_CASE_BODY -and -.Fn ATF_TEST_CASE_CLEANUP -macros, all of which take the test case's name. -Following each of these, a block of code is expected, surrounded by the -opening and closing brackets. -.Pp -Additionally, the -.Fn ATF_TEST_CASE_NAME -macro can be used to obtain the name of the class corresponding to a -particular test case, as the name is internally manged by the library to -prevent clashes with other user identifiers. -Similarly, the -.Fn ATF_TEST_CASE_USE -macro can be executed on a particular test case to mark it as "used" and -thus prevent compiler warnings regarding unused symbols. -Note that -.Em you should never have to use these macros during regular operation. -.Ss Program initialization -The library provides a way to easily define the test program's -.Fn main -function. -You should never define one on your own, but rely on the -library to do it for you. -This is done by using the -.Fn ATF_INIT_TEST_CASES -macro, which is passed the name of the list that will hold the test cases. -This name can be whatever you want as long as it is a valid variable value. -.Pp -After the macro, you are supposed to provide the body of a function, which -should only use the -.Fn ATF_ADD_TEST_CASE -macro to register the test cases the test program will execute. -The first parameter of this macro matches the name you provided in the -former call. -.Ss Header definitions -The test case's header can define the meta-data by using the -.Fn set_md_var -method, which takes two parameters: the first one specifies the -meta-data variable to be set and the second one specifies its value. -Both of them are strings. -.Ss Configuration variables -The test case has read-only access to the current configuration variables -by means of the -.Ft bool -.Fn has_config_var -and the -.Ft std::string -.Fn get_config_var -methods, which can be called in any of the three parts of a test case. -.Ss Access to the source directory -It is possible to get the path to the test case's source directory from any -of its three components by querying the -.Sq srcdir -configuration variable. -.Ss Requiring programs -Aside from the -.Va require.progs -meta-data variable available in the header only, one can also check for -additional programs in the test case's body by using the -.Fn require_prog -function, which takes the base name or full path of a single binary. -Relative paths are forbidden. -If it is not found, the test case will be automatically skipped. -.Ss Test case finalization -The test case finalizes either when the body reaches its end, at which -point the test is assumed to have -.Em passed , -or at any explicit call to -.Fn ATF_PASS , -.Fn ATF_FAIL -or -.Fn ATF_SKIP . -These three macros terminate the execution of the test case immediately. -The cleanup routine will be processed afterwards in a completely automated -way, regardless of the test case's termination reason. -.Pp -.Fn ATF_PASS -does not take any parameters. -.Fn ATF_FAIL -and -.Fn ATF_SKIP -take a single string that describes why the test case failed or -was skipped, respectively. -It is very important to provide a clear error message in both cases so that -the user can quickly know why the test did not pass. -.Ss Expectations -Everything explained in the previous section changes when the test case -expectations are redefined by the programmer. -.Pp -Each test case has an internal state called -.Sq expect -that describes what the test case expectations are at any point in time. -The value of this property can change during execution by any of: -.Bl -tag -width indent -.It Fn expect_death "reason" -Expects the test case to exit prematurely regardless of the nature of the -exit. -.It Fn expect_exit "exitcode" "reason" -Expects the test case to exit cleanly. -If -.Va exitcode -is not -.Sq -1 , -the runtime engine will validate that the exit code of the test case -matches the one provided in this call. -Otherwise, the exact value will be ignored. -.It Fn expect_fail "reason" -Any failure (be it fatal or non-fatal) raised in this mode is recorded. -However, such failures do not report the test case as failed; instead, the -test case finalizes cleanly and is reported as -.Sq expected failure ; -this report includes the provided -.Fa reason -as part of it. -If no error is raised while running in this mode, then the test case is -reported as -.Sq failed . -.Pp -This mode is useful to reproduce actual known bugs in tests. -Whenever the developer fixes the bug later on, the test case will start -reporting a failure, signaling the developer that the test case must be -adjusted to the new conditions. -In this situation, it is useful, for example, to set -.Fa reason -as the bug number for tracking purposes. -.It Fn expect_pass -This is the normal mode of execution. -In this mode, any failure is reported as such to the user and the test case -is marked as -.Sq failed . -.It Fn expect_race "reason" -Any failure or timeout during the execution of the test case will be -considered as if a race condition has been triggered and reported as such. -If no problems arise, the test will continue execution as usual. -.It Fn expect_signal "signo" "reason" -Expects the test case to terminate due to the reception of a signal. -If -.Va signo -is not -.Sq -1 , -the runtime engine will validate that the signal that terminated the test -case matches the one provided in this call. -Otherwise, the exact value will be ignored. -.It Fn expect_timeout "reason" -Expects the test case to execute for longer than its timeout. -.El -.Ss Helper macros for common checks -The library provides several macros that are very handy in multiple -situations. -These basically check some condition after executing a given statement or -processing a given expression and, if the condition is not met, they -automatically call -.Fn ATF_FAIL -with an appropriate error message. -.Pp -.Fn ATF_REQUIRE -takes an expression and raises a failure if it evaluates to false. -.Pp -.Fn ATF_REQUIRE_EQ -takes two expressions and raises a failure if the two do not evaluate to -the same exact value. -.Pp -.Fn ATF_REQUIRE_IN -takes an element and a collection and validates that the element is present in -the collection. -.Pp -.Fn ATF_REQUIRE_MATCH -takes a regular expression and a string and raises a failure if the regular -expression does not match the string. -.Pp -.Fn ATF_REQUIRE_NOT_IN -takes an element and a collection and validates that the element is not present -in the collection. -.Pp -.Fn ATF_REQUIRE_THROW -takes the name of an exception and a statement and raises a failure if -the statement does not throw the specified exception. -.Fn ATF_REQUIRE_THROW_RE -takes the name of an exception, a regular expresion and a statement and raises a -failure if the statement does not throw the specified exception and if the -message of the exception does not match the regular expression. -.Pp -.Fn ATF_CHECK_ERRNO -and -.Fn ATF_REQUIRE_ERRNO -take, first, the error code that the check is expecting to find in the -.Va errno -variable and, second, a boolean expression that, if evaluates to true, -means that a call failed and -.Va errno -has to be checked against the first value. -.Ss Utility functions -The following functions are provided as part of the -.Nm -API to simplify the creation of a variety of tests. -In particular, these are useful to write tests for command-line interfaces. -.Pp -.Ft void -.Fo atf::utils::cat_file -.Fa "const std::string& path" -.Fa "const std::string& prefix" -.Fc -.Bd -ragged -offset indent -Prints the contents of -.Fa path -to the standard output, prefixing every line with the string in -.Fa prefix . -.Ed -.Pp -.Ft bool -.Fo atf::utils::compare_file -.Fa "const std::string& path" -.Fa "const std::string& contents" -.Fc -.Bd -ragged -offset indent -Returns true if the given -.Fa path -matches exactly the expected inlined -.Fa contents . -.Ed -.Pp -.Ft void -.Fo atf::utils::copy_file -.Fa "const std::string& source" -.Fa "const std::string& destination" -.Fc -.Bd -ragged -offset indent -Copies the file -.Fa source -to -.Fa destination . -The permissions of the file are preserved during the code. -.Ed -.Pp -.Ft void -.Fo atf::utils::create_file -.Fa "const std::string& path" -.Fa "const std::string& contents" -.Fc -.Bd -ragged -offset indent -Creates -.Fa file -with the text given in -.Fa contents . -.Ed -.Pp -.Ft void -.Fo atf::utils::file_exists -.Fa "const std::string& path" -.Fc -.Bd -ragged -offset indent -Checks if -.Fa path -exists. -.Ed -.Pp -.Ft pid_t -.Fo atf::utils::fork -.Fa "void" -.Fc -.Bd -ragged -offset indent -Forks a process and redirects the standard output and standard error of the -child to files for later validation with -.Fn atf::utils::wait . -Fails the test case if the fork fails, so this does not return an error. -.Ed -.Pp -.Ft bool -.Fo atf::utils::grep_collection -.Fa "const std::string& regexp" -.Fa "const Collection& collection" -.Fc -.Bd -ragged -offset indent -Searches for the regular expression -.Fa regexp -in any of the strings contained in the -.Fa collection . -This is a template that accepts any one-dimensional container of strings. -.Ed -.Pp -.Ft bool -.Fo atf::utils::grep_file -.Fa "const std::string& regexp" -.Fa "const std::string& path" -.Fc -.Bd -ragged -offset indent -Searches for the regular expression -.Fa regexp -in the file -.Fa path . -The variable arguments are used to construct the regular expression. -.Ed -.Pp -.Ft bool -.Fo atf::utils::grep_string -.Fa "const std::string& regexp" -.Fa "const std::string& str" -.Fc -.Bd -ragged -offset indent -Searches for the regular expression -.Fa regexp -in the string -.Fa str . -.Ed -.Ft void -.Fo atf::utils::redirect -.Fa "const int fd" -.Fa "const std::string& path" -.Fc -.Bd -ragged -offset indent -Redirects the given file descriptor -.Fa fd -to the file -.Fa path . -This function exits the process in case of an error and does not properly mark -the test case as failed. -As a result, it should only be used in subprocesses of the test case; specially -those spawned by -.Fn atf::utils::fork . -.Ed -.Pp -.Ft void -.Fo atf::utils::wait -.Fa "const pid_t pid" -.Fa "const int expected_exit_status" -.Fa "const std::string& expected_stdout" -.Fa "const std::string& expected_stderr" -.Fc -.Bd -ragged -offset indent -Waits and validates the result of a subprocess spawned with -.Fn atf::utils::wait . -The validation involves checking that the subprocess exited cleanly and returned -the code specified in -.Fa expected_exit_status -and that its standard output and standard error match the strings given in -.Fa expected_stdout -and -.Fa expected_stderr . -.Pp -If any of the -.Fa expected_stdout -or -.Fa expected_stderr -strings are prefixed with -.Sq save: , -then they specify the name of the file into which to store the stdout or stderr -of the subprocess, and no comparison is performed. -.Ed -.Sh EXAMPLES -The following shows a complete test program with a single test case that -validates the addition operator: -.Bd -literal -offset indent -#include - -ATF_TEST_CASE(addition); -ATF_TEST_CASE_HEAD(addition) -{ - set_md_var("descr", "Sample tests for the addition operator"); -} -ATF_TEST_CASE_BODY(addition) -{ - ATF_REQUIRE_EQ(0 + 0, 0); - ATF_REQUIRE_EQ(0 + 1, 1); - ATF_REQUIRE_EQ(1 + 0, 1); - - ATF_REQUIRE_EQ(1 + 1, 2); - - ATF_REQUIRE_EQ(100 + 200, 300); -} - -ATF_TEST_CASE(open_failure); -ATF_TEST_CASE_HEAD(open_failure) -{ - set_md_var("descr", "Sample tests for the open function"); -} -ATF_TEST_CASE_BODY(open_failure) -{ - ATF_REQUIRE_ERRNO(ENOENT, open("non-existent", O_RDONLY) == -1); -} - -ATF_TEST_CASE(known_bug); -ATF_TEST_CASE_HEAD(known_bug) -{ - set_md_var("descr", "Reproduces a known bug"); -} -ATF_TEST_CASE_BODY(known_bug) -{ - expect_fail("See bug number foo/bar"); - ATF_REQUIRE_EQ(3, 1 + 1); - expect_pass(); - ATF_REQUIRE_EQ(3, 1 + 2); -} - -ATF_INIT_TEST_CASES(tcs) -{ - ATF_ADD_TEST_CASE(tcs, addition); - ATF_ADD_TEST_CASE(tcs, open_failure); - ATF_ADD_TEST_CASE(tcs, known_bug); -} -.Ed -.Sh SEE ALSO -.Xr atf-test-program 1 , -.Xr atf-test-case 4 Index: stable/10/contrib/atf/atf-c++/atf-c++.3 =================================================================== --- stable/10/contrib/atf/atf-c++/atf-c++.3 (revision 315182) +++ stable/10/contrib/atf/atf-c++/atf-c++.3 (revision 315183) @@ -1,649 +1,649 @@ .\" Copyright (c) 2008 The NetBSD Foundation, Inc. .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND .\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF .\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. .\" IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY .\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE .\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER .\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN .\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -.Dd October 13, 2014 +.Dd March 6, 2017 .Dt ATF-C++ 3 .Os .Sh NAME .Nm atf-c++ , .Nm ATF_ADD_TEST_CASE , .Nm ATF_CHECK_ERRNO , .Nm ATF_FAIL , .Nm ATF_INIT_TEST_CASES , .Nm ATF_PASS , .Nm ATF_REQUIRE , .Nm ATF_REQUIRE_EQ , .Nm ATF_REQUIRE_ERRNO , .Nm ATF_REQUIRE_IN , .Nm ATF_REQUIRE_MATCH , .Nm ATF_REQUIRE_NOT_IN , .Nm ATF_REQUIRE_THROW , .Nm ATF_REQUIRE_THROW_RE , .Nm ATF_SKIP , .Nm ATF_TEST_CASE , .Nm ATF_TEST_CASE_BODY , .Nm ATF_TEST_CASE_CLEANUP , .Nm ATF_TEST_CASE_HEAD , .Nm ATF_TEST_CASE_NAME , .Nm ATF_TEST_CASE_USE , .Nm ATF_TEST_CASE_WITH_CLEANUP , .Nm ATF_TEST_CASE_WITHOUT_HEAD , .Nm atf::utils::cat_file , .Nm atf::utils::compare_file , .Nm atf::utils::copy_file , .Nm atf::utils::create_file , .Nm atf::utils::file_exists , .Nm atf::utils::fork , .Nm atf::utils::grep_collection , .Nm atf::utils::grep_file , .Nm atf::utils::grep_string , .Nm atf::utils::redirect , .Nm atf::utils::wait .Nd C++ API to write ATF-based test programs .Sh SYNOPSIS .In atf-c++.hpp .Fn ATF_ADD_TEST_CASE "tcs" "name" .Fn ATF_CHECK_ERRNO "expected_errno" "bool_expression" .Fn ATF_FAIL "reason" .Fn ATF_INIT_TEST_CASES "tcs" .Fn ATF_PASS .Fn ATF_REQUIRE "expression" .Fn ATF_REQUIRE_EQ "expected_expression" "actual_expression" .Fn ATF_REQUIRE_ERRNO "expected_errno" "bool_expression" .Fn ATF_REQUIRE_IN "element" "collection" .Fn ATF_REQUIRE_MATCH "regexp" "string_expression" .Fn ATF_REQUIRE_NOT_IN "element" "collection" .Fn ATF_REQUIRE_THROW "expected_exception" "statement" .Fn ATF_REQUIRE_THROW_RE "expected_exception" "regexp" "statement" .Fn ATF_SKIP "reason" .Fn ATF_TEST_CASE "name" .Fn ATF_TEST_CASE_BODY "name" .Fn ATF_TEST_CASE_CLEANUP "name" .Fn ATF_TEST_CASE_HEAD "name" .Fn ATF_TEST_CASE_NAME "name" .Fn ATF_TEST_CASE_USE "name" .Fn ATF_TEST_CASE_WITH_CLEANUP "name" .Fn ATF_TEST_CASE_WITHOUT_HEAD "name" .Ft void .Fo atf::utils::cat_file .Fa "const std::string& path" .Fa "const std::string& prefix" .Fc .Ft bool .Fo atf::utils::compare_file .Fa "const std::string& path" .Fa "const std::string& contents" .Fc .Ft void .Fo atf::utils::copy_file .Fa "const std::string& source" .Fa "const std::string& destination" .Fc .Ft void .Fo atf::utils::create_file .Fa "const std::string& path" .Fa "const std::string& contents" .Fc .Ft void .Fo atf::utils::file_exists .Fa "const std::string& path" .Fc .Ft pid_t .Fo atf::utils::fork .Fa "void" .Fc .Ft bool .Fo atf::utils::grep_collection .Fa "const std::string& regexp" .Fa "const Collection& collection" .Fc .Ft bool .Fo atf::utils::grep_file .Fa "const std::string& regexp" .Fa "const std::string& path" .Fc .Ft bool .Fo atf::utils::grep_string .Fa "const std::string& regexp" .Fa "const std::string& path" .Fc .Ft void .Fo atf::utils::redirect .Fa "const int fd" .Fa "const std::string& path" .Fc .Ft void .Fo atf::utils::wait .Fa "const pid_t pid" .Fa "const int expected_exit_status" .Fa "const std::string& expected_stdout" .Fa "const std::string& expected_stderr" .Fc .Sh DESCRIPTION ATF provides a C++ programming interface to implement test programs. C++-based test programs follow this template: .Bd -literal -offset indent extern "C" { -.Ns ... C-specific includes go here ... +\&... C-specific includes go here ... } -.Ns ... C++-specific includes go here ... +\&... C++-specific includes go here ... #include ATF_TEST_CASE(tc1); ATF_TEST_CASE_HEAD(tc1) { ... first test case's header ... } ATF_TEST_CASE_BODY(tc1) { ... first test case's body ... } ATF_TEST_CASE_WITH_CLEANUP(tc2); ATF_TEST_CASE_HEAD(tc2) { ... second test case's header ... } ATF_TEST_CASE_BODY(tc2) { ... second test case's body ... } ATF_TEST_CASE_CLEANUP(tc2) { ... second test case's cleanup ... } ATF_TEST_CASE(tc3); ATF_TEST_CASE_BODY(tc3) { ... third test case's body ... } -.Ns ... additional test cases ... +\&... additional test cases ... ATF_INIT_TEST_CASES(tcs) { ATF_ADD_TEST_CASE(tcs, tc1); ATF_ADD_TEST_CASE(tcs, tc2); ATF_ADD_TEST_CASE(tcs, tc3); ... add additional test cases ... } .Ed .Ss Definition of test cases Test cases have an identifier and are composed of three different parts: the header, the body and an optional cleanup routine, all of which are described in .Xr atf-test-case 4 . To define test cases, one can use the .Fn ATF_TEST_CASE , .Fn ATF_TEST_CASE_WITH_CLEANUP or the .Fn ATF_TEST_CASE_WITHOUT_HEAD -macros, which take a single parameter specifiying the test case's +macros, which take a single parameter specifying the test case's name. .Fn ATF_TEST_CASE , requires to define a head and a body for the test case, .Fn ATF_TEST_CASE_WITH_CLEANUP requires to define a head, a body and a cleanup for the test case and .Fn ATF_TEST_CASE_WITHOUT_HEAD requires only a body for the test case. It is important to note that these .Em do not set the test case up for execution when the program is run. In order to do so, a later registration is needed through the .Fn ATF_ADD_TEST_CASE macro detailed in .Sx Program initialization . .Pp Later on, one must define the three parts of the body by means of three functions. Their headers are given by the .Fn ATF_TEST_CASE_HEAD , .Fn ATF_TEST_CASE_BODY and .Fn ATF_TEST_CASE_CLEANUP macros, all of which take the test case's name. Following each of these, a block of code is expected, surrounded by the opening and closing brackets. .Pp Additionally, the .Fn ATF_TEST_CASE_NAME macro can be used to obtain the name of the class corresponding to a -particular test case, as the name is internally manged by the library to +particular test case, as the name is internally managed by the library to prevent clashes with other user identifiers. Similarly, the .Fn ATF_TEST_CASE_USE macro can be executed on a particular test case to mark it as "used" and thus prevent compiler warnings regarding unused symbols. Note that .Em you should never have to use these macros during regular operation. .Ss Program initialization The library provides a way to easily define the test program's .Fn main function. You should never define one on your own, but rely on the library to do it for you. This is done by using the .Fn ATF_INIT_TEST_CASES macro, which is passed the name of the list that will hold the test cases. This name can be whatever you want as long as it is a valid variable value. .Pp After the macro, you are supposed to provide the body of a function, which should only use the .Fn ATF_ADD_TEST_CASE macro to register the test cases the test program will execute. The first parameter of this macro matches the name you provided in the former call. .Ss Header definitions The test case's header can define the meta-data by using the .Fn set_md_var method, which takes two parameters: the first one specifies the meta-data variable to be set and the second one specifies its value. Both of them are strings. .Ss Configuration variables The test case has read-only access to the current configuration variables by means of the .Ft bool .Fn has_config_var and the .Ft std::string .Fn get_config_var methods, which can be called in any of the three parts of a test case. .Ss Access to the source directory It is possible to get the path to the test case's source directory from any of its three components by querying the .Sq srcdir configuration variable. .Ss Requiring programs Aside from the .Va require.progs meta-data variable available in the header only, one can also check for additional programs in the test case's body by using the .Fn require_prog function, which takes the base name or full path of a single binary. Relative paths are forbidden. If it is not found, the test case will be automatically skipped. .Ss Test case finalization The test case finalizes either when the body reaches its end, at which point the test is assumed to have .Em passed , or at any explicit call to .Fn ATF_PASS , .Fn ATF_FAIL or .Fn ATF_SKIP . These three macros terminate the execution of the test case immediately. The cleanup routine will be processed afterwards in a completely automated way, regardless of the test case's termination reason. .Pp .Fn ATF_PASS does not take any parameters. .Fn ATF_FAIL and .Fn ATF_SKIP take a single string that describes why the test case failed or was skipped, respectively. It is very important to provide a clear error message in both cases so that the user can quickly know why the test did not pass. .Ss Expectations Everything explained in the previous section changes when the test case expectations are redefined by the programmer. .Pp Each test case has an internal state called .Sq expect that describes what the test case expectations are at any point in time. The value of this property can change during execution by any of: .Bl -tag -width indent .It Fn expect_death "reason" Expects the test case to exit prematurely regardless of the nature of the exit. .It Fn expect_exit "exitcode" "reason" Expects the test case to exit cleanly. If .Va exitcode is not .Sq -1 , the runtime engine will validate that the exit code of the test case matches the one provided in this call. Otherwise, the exact value will be ignored. .It Fn expect_fail "reason" Any failure (be it fatal or non-fatal) raised in this mode is recorded. However, such failures do not report the test case as failed; instead, the test case finalizes cleanly and is reported as .Sq expected failure ; this report includes the provided .Fa reason as part of it. If no error is raised while running in this mode, then the test case is reported as .Sq failed . .Pp This mode is useful to reproduce actual known bugs in tests. Whenever the developer fixes the bug later on, the test case will start reporting a failure, signaling the developer that the test case must be adjusted to the new conditions. In this situation, it is useful, for example, to set .Fa reason as the bug number for tracking purposes. .It Fn expect_pass This is the normal mode of execution. In this mode, any failure is reported as such to the user and the test case is marked as .Sq failed . .It Fn expect_race "reason" Any failure or timeout during the execution of the test case will be considered as if a race condition has been triggered and reported as such. If no problems arise, the test will continue execution as usual. .It Fn expect_signal "signo" "reason" Expects the test case to terminate due to the reception of a signal. If .Va signo is not .Sq -1 , the runtime engine will validate that the signal that terminated the test case matches the one provided in this call. Otherwise, the exact value will be ignored. .It Fn expect_timeout "reason" Expects the test case to execute for longer than its timeout. .El .Ss Helper macros for common checks The library provides several macros that are very handy in multiple situations. These basically check some condition after executing a given statement or processing a given expression and, if the condition is not met, they automatically call .Fn ATF_FAIL with an appropriate error message. .Pp .Fn ATF_REQUIRE takes an expression and raises a failure if it evaluates to false. .Pp .Fn ATF_REQUIRE_EQ takes two expressions and raises a failure if the two do not evaluate to the same exact value. The common style is to put the expected value in the first parameter and the actual value in the second parameter. .Pp .Fn ATF_REQUIRE_IN takes an element and a collection and validates that the element is present in the collection. .Pp .Fn ATF_REQUIRE_MATCH takes a regular expression and a string and raises a failure if the regular expression does not match the string. .Pp .Fn ATF_REQUIRE_NOT_IN takes an element and a collection and validates that the element is not present in the collection. .Pp .Fn ATF_REQUIRE_THROW takes the name of an exception and a statement and raises a failure if the statement does not throw the specified exception. .Fn ATF_REQUIRE_THROW_RE takes the name of an exception, a regular expresion and a statement and raises a failure if the statement does not throw the specified exception and if the message of the exception does not match the regular expression. .Pp .Fn ATF_CHECK_ERRNO and .Fn ATF_REQUIRE_ERRNO take, first, the error code that the check is expecting to find in the .Va errno variable and, second, a boolean expression that, if evaluates to true, means that a call failed and .Va errno has to be checked against the first value. .Ss Utility functions The following functions are provided as part of the .Nm API to simplify the creation of a variety of tests. In particular, these are useful to write tests for command-line interfaces. .Pp .Ft void .Fo atf::utils::cat_file .Fa "const std::string& path" .Fa "const std::string& prefix" .Fc .Bd -ragged -offset indent Prints the contents of .Fa path to the standard output, prefixing every line with the string in .Fa prefix . .Ed .Pp .Ft bool .Fo atf::utils::compare_file .Fa "const std::string& path" .Fa "const std::string& contents" .Fc .Bd -ragged -offset indent Returns true if the given .Fa path matches exactly the expected inlined .Fa contents . .Ed .Pp .Ft void .Fo atf::utils::copy_file .Fa "const std::string& source" .Fa "const std::string& destination" .Fc .Bd -ragged -offset indent Copies the file .Fa source to .Fa destination . The permissions of the file are preserved during the code. .Ed .Pp .Ft void .Fo atf::utils::create_file .Fa "const std::string& path" .Fa "const std::string& contents" .Fc .Bd -ragged -offset indent Creates .Fa file with the text given in .Fa contents . .Ed .Pp .Ft void .Fo atf::utils::file_exists .Fa "const std::string& path" .Fc .Bd -ragged -offset indent Checks if .Fa path exists. .Ed .Pp .Ft pid_t .Fo atf::utils::fork .Fa "void" .Fc .Bd -ragged -offset indent Forks a process and redirects the standard output and standard error of the child to files for later validation with .Fn atf::utils::wait . Fails the test case if the fork fails, so this does not return an error. .Ed .Pp .Ft bool .Fo atf::utils::grep_collection .Fa "const std::string& regexp" .Fa "const Collection& collection" .Fc .Bd -ragged -offset indent Searches for the regular expression .Fa regexp in any of the strings contained in the .Fa collection . This is a template that accepts any one-dimensional container of strings. .Ed .Pp .Ft bool .Fo atf::utils::grep_file .Fa "const std::string& regexp" .Fa "const std::string& path" .Fc .Bd -ragged -offset indent Searches for the regular expression .Fa regexp in the file .Fa path . The variable arguments are used to construct the regular expression. .Ed .Pp .Ft bool .Fo atf::utils::grep_string .Fa "const std::string& regexp" .Fa "const std::string& str" .Fc .Bd -ragged -offset indent Searches for the regular expression .Fa regexp in the string .Fa str . .Ed .Ft void .Fo atf::utils::redirect .Fa "const int fd" .Fa "const std::string& path" .Fc .Bd -ragged -offset indent Redirects the given file descriptor .Fa fd to the file .Fa path . This function exits the process in case of an error and does not properly mark the test case as failed. As a result, it should only be used in subprocesses of the test case; specially those spawned by .Fn atf::utils::fork . .Ed .Pp .Ft void .Fo atf::utils::wait .Fa "const pid_t pid" .Fa "const int expected_exit_status" .Fa "const std::string& expected_stdout" .Fa "const std::string& expected_stderr" .Fc .Bd -ragged -offset indent Waits and validates the result of a subprocess spawned with .Fn atf::utils::wait . The validation involves checking that the subprocess exited cleanly and returned the code specified in .Fa expected_exit_status and that its standard output and standard error match the strings given in .Fa expected_stdout and .Fa expected_stderr . .Pp If any of the .Fa expected_stdout or .Fa expected_stderr strings are prefixed with .Sq save: , then they specify the name of the file into which to store the stdout or stderr of the subprocess, and no comparison is performed. .Ed .Sh ENVIRONMENT The following variables are recognized by .Nm but should not be overridden other than for testing purposes: .Pp .Bl -tag -width ATFXBUILDXCXXFLAGSXX -compact .It Va ATF_BUILD_CC Path to the C compiler. .It Va ATF_BUILD_CFLAGS C compiler flags. .It Va ATF_BUILD_CPP Path to the C/C++ preprocessor. .It Va ATF_BUILD_CPPFLAGS C/C++ preprocessor flags. .It Va ATF_BUILD_CXX Path to the C++ compiler. .It Va ATF_BUILD_CXXFLAGS C++ compiler flags. .El .Sh EXAMPLES The following shows a complete test program with a single test case that validates the addition operator: .Bd -literal -offset indent #include ATF_TEST_CASE(addition); ATF_TEST_CASE_HEAD(addition) { set_md_var("descr", "Sample tests for the addition operator"); } ATF_TEST_CASE_BODY(addition) { ATF_REQUIRE_EQ(0, 0 + 0); ATF_REQUIRE_EQ(1, 0 + 1); ATF_REQUIRE_EQ(1, 1 + 0); ATF_REQUIRE_EQ(2, 1 + 1); ATF_REQUIRE_EQ(300, 100 + 200); } ATF_TEST_CASE(open_failure); ATF_TEST_CASE_HEAD(open_failure) { set_md_var("descr", "Sample tests for the open function"); } ATF_TEST_CASE_BODY(open_failure) { ATF_REQUIRE_ERRNO(ENOENT, open("non-existent", O_RDONLY) == -1); } ATF_TEST_CASE(known_bug); ATF_TEST_CASE_HEAD(known_bug) { set_md_var("descr", "Reproduces a known bug"); } ATF_TEST_CASE_BODY(known_bug) { expect_fail("See bug number foo/bar"); ATF_REQUIRE_EQ(3, 1 + 1); expect_pass(); ATF_REQUIRE_EQ(3, 1 + 2); } ATF_INIT_TEST_CASES(tcs) { ATF_ADD_TEST_CASE(tcs, addition); ATF_ADD_TEST_CASE(tcs, open_failure); ATF_ADD_TEST_CASE(tcs, known_bug); } .Ed .Sh SEE ALSO .Xr atf-test-program 1 , .Xr atf-test-case 4 Index: stable/10/contrib/atf/atf-sh/atf-sh-api.3 =================================================================== --- stable/10/contrib/atf/atf-sh/atf-sh-api.3 (revision 315182) +++ stable/10/contrib/atf/atf-sh/atf-sh-api.3 (nonexistent) @@ -1,340 +0,0 @@ -.\" -.\" Automated Testing Framework (atf) -.\" -.\" Copyright (c) 2008 The NetBSD Foundation, Inc. -.\" All rights reserved. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND -.\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, -.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. -.\" IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY -.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE -.\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER -.\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN -.\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -.\" -.Dd March 2, 2014 -.Dt ATF-SH-API 3 -.Os -.Sh NAME -.Nm atf_add_test_case , -.Nm atf_check , -.Nm atf_check_equal , -.Nm atf_config_get , -.Nm atf_config_has , -.Nm atf_expect_death , -.Nm atf_expect_exit , -.Nm atf_expect_fail , -.Nm atf_expect_pass , -.Nm atf_expect_signal , -.Nm atf_expect_timeout , -.Nm atf_fail , -.Nm atf_get , -.Nm atf_get_srcdir , -.Nm atf_pass , -.Nm atf_require_prog , -.Nm atf_set , -.Nm atf_skip , -.Nm atf_test_case -.Nd POSIX shell API to write ATF-based test programs -.Sh SYNOPSIS -.Fn atf_add_test_case "name" -.Fn atf_check "command" -.Fn atf_check_equal "expr1" "expr2" -.Fn atf_config_get "var_name" -.Fn atf_config_has "var_name" -.Fn atf_expect_death "reason" "..." -.Fn atf_expect_exit "exitcode" "reason" "..." -.Fn atf_expect_fail "reason" "..." -.Fn atf_expect_pass -.Fn atf_expect_signal "signo" "reason" "..." -.Fn atf_expect_timeout "reason" "..." -.Fn atf_fail "reason" -.Fn atf_get "var_name" -.Fn atf_get_srcdir -.Fn atf_pass -.Fn atf_require_prog "prog_name" -.Fn atf_set "var_name" "value" -.Fn atf_skip "reason" -.Fn atf_test_case "name" "cleanup" -.Sh DESCRIPTION -ATF -provides a simple but powerful interface to easily write test programs in -the POSIX shell language. -These are extremely helpful given that they are trivial to write due to the -language simplicity and the great deal of available external tools, so they -are often ideal to test other applications at the user level. -.Pp -Test programs written using this library must be run using the -.Xr atf-sh 1 -interpreter by putting the following on their very first line: -.Bd -literal -offset indent -#! /usr/bin/env atf-sh -.Ed -.Pp -Shell-based test programs always follow this template: -.Bd -literal -offset indent -atf_test_case tc1 -tc1_head() { - ... first test case's header ... -} -tc1_body() { - ... first test case's body ... -} - -atf_test_case tc2 cleanup -tc2_head() { - ... second test case's header ... -} -tc2_body() { - ... second test case's body ... -} -tc2_cleanup() { - ... second test case's cleanup ... -} - -.Ns ... additional test cases ... - -atf_init_test_cases() { - atf_add_test_case tc1 - atf_add_test_case tc2 - ... add additional test cases ... -} -.Ed -.Ss Definition of test cases -Test cases have an identifier and are composed of three different parts: -the header, the body and an optional cleanup routine, all of which are -described in -.Xr atf-test-case 4 . -To define test cases, one can use the -.Fn atf_test_case -function, which takes a first parameter specifiying the test case's -name and instructs the library to set things up to accept it as a valid -test case. -The second parameter is optional and, if provided, must be -.Sq cleanup ; -providing this parameter allows defining a cleanup routine for the test -case. -It is important to note that this function -.Em does not -set the test case up for execution when the program is run. -In order to do so, a later registration is needed through the -.Fn atf_add_test_case -function detailed in -.Sx Program initialization . -.Pp -Later on, one must define the three parts of the body by providing two -or three functions (remember that the cleanup routine is optional). -These functions are named after the test case's identifier, and are -.Fn _head , -.Fn _body -and -.Fn _cleanup. -None of these take parameters when executed. -.Ss Program initialization -The test program must define an -.Fn atf_init_test_cases -function, which is in charge of registering the test cases that will be -executed at run time by using the -.Fn atf_add_test_case -function, which takes the name of a test case as its single parameter. -This main function should not do anything else, except maybe sourcing -auxiliary source files that define extra variables and functions. -.Ss Configuration variables -The test case has read-only access to the current configuration variables -through the -.Fn atf_config_has -and -.Fn atf_config_get -methods. -The former takes a single parameter specifying a variable name and returns -a boolean indicating whether the variable is defined or not. -The latter can take one or two parameters. -If it takes only one, it specifies the variable from which to get the -value, and this variable must be defined. -If it takes two, the second one specifies a default value to be returned -if the variable is not available. -.Ss Access to the source directory -It is possible to get the path to the test case's source directory from -anywhere in the test program by using the -.Fn atf_get_srcdir -function. -It is interesting to note that this can be used inside -.Fn atf_init_test_cases -to silently include additional helper files from the source directory. -.Ss Requiring programs -Aside from the -.Va require.progs -meta-data variable available in the header only, one can also check for -additional programs in the test case's body by using the -.Fn atf_require_prog -function, which takes the base name or full path of a single binary. -Relative paths are forbidden. -If it is not found, the test case will be automatically skipped. -.Ss Test case finalization -The test case finalizes either when the body reaches its end, at which -point the test is assumed to have -.Em passed , -or at any explicit call to -.Fn atf_pass , -.Fn atf_fail -or -.Fn atf_skip . -These three functions terminate the execution of the test case immediately. -The cleanup routine will be processed afterwards in a completely automated -way, regardless of the test case's termination reason. -.Pp -.Fn atf_pass -does not take any parameters. -.Fn atf_fail -and -.Fn atf_skip -take a single string parameter that describes why the test case failed or -was skipped, respectively. -It is very important to provide a clear error message in both cases so that -the user can quickly know why the test did not pass. -.Ss Expectations -Everything explained in the previous section changes when the test case -expectations are redefined by the programmer. -.Pp -Each test case has an internal state called -.Sq expect -that describes what the test case expectations are at any point in time. -The value of this property can change during execution by any of: -.Bl -tag -width indent -.It Fn atf_expect_death "reason" "..." -Expects the test case to exit prematurely regardless of the nature of the -exit. -.It Fn atf_expect_exit "exitcode" "reason" "..." -Expects the test case to exit cleanly. -If -.Va exitcode -is not -.Sq -1 , -the runtime engine will validate that the exit code of the test case -matches the one provided in this call. -Otherwise, the exact value will be ignored. -.It Fn atf_expect_fail "reason" -Any failure raised in this mode is recorded, but such failures do not report -the test case as failed; instead, the test case finalizes cleanly and is -reported as -.Sq expected failure ; -this report includes the provided -.Fa reason -as part of it. -If no error is raised while running in this mode, then the test case is -reported as -.Sq failed . -.Pp -This mode is useful to reproduce actual known bugs in tests. -Whenever the developer fixes the bug later on, the test case will start -reporting a failure, signaling the developer that the test case must be -adjusted to the new conditions. -In this situation, it is useful, for example, to set -.Fa reason -as the bug number for tracking purposes. -.It Fn atf_expect_pass -This is the normal mode of execution. -In this mode, any failure is reported as such to the user and the test case -is marked as -.Sq failed . -.It Fn atf_expect_signal "signo" "reason" "..." -Expects the test case to terminate due to the reception of a signal. -If -.Va signo -is not -.Sq -1 , -the runtime engine will validate that the signal that terminated the test -case matches the one provided in this call. -Otherwise, the exact value will be ignored. -.It Fn atf_expect_timeout "reason" "..." -Expects the test case to execute for longer than its timeout. -.El -.Ss Helper functions for common checks -.Fn atf_check [options] command [args] -.Pp -This function wraps the execution of the -.Nm atf-check -tool and makes the test case fail if the tool reports failure. -You should always use this function instead of the tool in your scripts. -For more details on the parameters of this function, refer to -.Xr atf-check 1 . -.Pp -.Fn atf_check_equal expr1 expr2 -.Pp -This function takes two expressions, evaluates them and, if their -results differ, aborts the test case with an appropriate failure message. -.Sh EXAMPLES -The following shows a complete test program with a single test case that -validates the addition operator: -.Bd -literal -offset indent -atf_test_case addition -addition_head() { - atf_set "descr" "Sample tests for the addition operator" -} -addition_body() { - atf_check_equal $((0 + 0)) 0 - atf_check_equal $((0 + 1)) 1 - atf_check_equal $((1 + 0)) 0 - - atf_check_equal $((1 + 1)) 2 - - atf_check_equal $((100 + 200)) 300 -} - -atf_init_test_cases() { - atf_add_test_case addition -} -.Ed -.Pp -This other example shows how to include a file with extra helper functions -in the test program: -.Bd -literal -offset indent -.Ns ... definition of test cases ... - -atf_init_test_cases() { - . $(atf_get_srcdir)/helper_functions.sh - - atf_add_test_case foo1 - atf_add_test_case foo2 -} -.Ed -.Pp -This example demonstrates the use of the very useful -.Fn atf_check -function: -.Bd -literal -offset indent -# Check for silent output -atf_check -s exit:0 -o empty -e empty 'true' - -# Check for silent output and failure -atf_check -s exit:1 -o empty -e empty 'false' - -# Check for known stdout and silent stderr -echo foo >expout -atf_check -s exit:0 -o file:expout -e empty 'echo foo' - -# Generate a file for later inspection -atf_check -s exit:0 -o save:stdout -e empty 'ls' -grep foo ls || atf_fail "foo file not found in listing" - -# Or just do the match along the way -atf_check -s exit:0 -o match:"^foo$" -e empty 'ls' -.Ed -.Sh SEE ALSO -.Xr atf-sh 1 , -.Xr atf-test-program 1 , -.Xr atf-test-case 4 Index: stable/10/contrib/atf/atf-sh/atf-check.1 =================================================================== --- stable/10/contrib/atf/atf-sh/atf-check.1 (revision 315182) +++ stable/10/contrib/atf/atf-sh/atf-check.1 (revision 315183) @@ -1,160 +1,162 @@ .\" Copyright (c) 2008 The NetBSD Foundation, Inc. .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND .\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF .\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. .\" IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY .\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE .\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER .\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN .\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -.Dd October 5, 2014 +.Dd March 6, 2017 .Dt ATF-CHECK 1 .Os .Sh NAME .Nm atf-check .Nd executes a command and analyzes its results .Sh SYNOPSIS .Nm .Op Fl s Ar qual:value .Op Fl o Ar action:arg ... .Op Fl e Ar action:arg ... .Op Fl x .Ar command .Sh DESCRIPTION .Nm executes a given command and analyzes its results, including exit code, stdout and stderr. .Pp -.Em Test cases must use -.Em Xr atf-sh 3 Ns ' Ns s -.Em Nm atf_check -.Em builtin function instead of calling this utility directly. +.Bf Em +Test cases must use +.Xr atf-sh 3 Ns ' Ns s +.Nm atf_check +builtin function instead of calling this utility directly. +.Ef .Pp In the first synopsis form, .Nm will execute the provided command and apply checks specified by arguments. By default it will act as if it was run with .Fl s .Ar exit:0 .Fl o .Ar empty .Fl e .Ar empty . Multiple checks for the same output channel are allowed and, if specified, their results will be combined as a logical and (meaning that the output must match all the provided checks). .Pp In the second synopsis form, .Nm will print information about all supported options and their purpose. .Pp The following options are available: .Bl -tag -width XqualXvalueXX .It Fl s Ar qual:value Analyzes termination status. Must be one of: .Bl -tag -width signal: -compact .It Ar exit: checks that the program exited cleanly and that its exit status is equal to .Va value . The exit code can be omitted altogether, in which case any clean exit is accepted. .It Ar ignore ignores the exit check. .It Ar signal: checks that the program exited due to a signal and that the signal that terminated it is .Va value . The signal can be specified both as a number or as a name, or it can also be omitted altogether, in which case any signal is accepted. .El .Pp Most of these checkers can be prefixed by the .Sq not- string, which effectively reverses the check. .It Fl o Ar action:arg Analyzes standard output. Must be one of: .Bl -tag -width inline: -compact .It Ar empty checks that stdout is empty .It Ar ignore ignores stdout .It Ar file: compares stdout with given file .It Ar inline: compares stdout with inline value .It Ar match: looks for a regular expression in stdout .It Ar save: saves stdout to given file .El .Pp Most of these checkers can be prefixed by the .Sq not- string, which effectively reverses the check. .It Fl e Ar action:arg Analyzes standard error (syntax identical to above) .It Fl x Executes .Ar command as a shell command line, executing it with the system shell defined by .Va ATF_SHELL . You should avoid using this flag if at all possible to prevent shell quoting issues. .El -.Sh EXIT STATUS -.Nm -exits 0 on success, and other (unspecified) value on failure. .Sh ENVIRONMENT .Bl -tag -width ATFXSHELLXX -compact .It Va ATF_SHELL Path to the system shell to be used when the .Fl x is given to run commands. .El +.Sh EXIT STATUS +.Nm +exits 0 on success, and other (unspecified) value on failure. .Sh EXAMPLES The following are sample invocations from within a test case. Note that we use the .Nm atf_check function provided by .Xr atf-sh 3 instead of executing .Nm directly: .Bd -literal -offset indent # Exit code 0, nothing on stdout/stderr atf_check 'true' # Typical usage if failure is expected atf_check -s not-exit:0 'false' # Checking stdout/stderr echo foobar >expout atf_check -o file:expout -e inline:"xx\etyy\en" \e 'echo foobar ; printf "xx\etyy\en" >&2' # Checking for a crash atf_check -s signal:sigsegv my_program # Combined checks atf_check -o match:foo -o not-match:bar echo foo baz .Ed .Sh SEE ALSO .Xr atf-sh 1 Index: stable/10/contrib/atf/atf-sh/atf-sh.1 =================================================================== --- stable/10/contrib/atf/atf-sh/atf-sh.1 (revision 315182) +++ stable/10/contrib/atf/atf-sh/atf-sh.1 (revision 315183) @@ -1,107 +1,104 @@ .\" Copyright (c) 2010 The NetBSD Foundation, Inc. .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND .\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF .\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. .\" IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY .\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE .\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER .\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN .\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -.Dd September 27, 2014 +.Dd March 6, 2017 .Dt ATF-SH 1 .Os .Sh NAME .Nm atf-sh -.Op Fl s Ar shell .Nd interpreter for shell-based test programs .Sh SYNOPSIS .Nm +.Op Fl s Ar shell .Ar script .Sh DESCRIPTION .Nm is an interpreter that runs the test program given in .Ar script after loading the .Xr atf-sh 3 library. .Pp .Nm is not a real interpreter though: it is just a wrapper around the system-wide shell defined by .Va ATF_SHELL . .Nm executes the interpreter, loads the .Xr atf-sh 3 library and then runs the script. You must consider .Nm atf-sh to be a POSIX shell by default and thus should not use any non-standard extensions. .Pp The following options are available: .Bl -tag -width XsXshellXXX .It Fl s Ar shell Specifies the shell to use instead of the value provided by .Va ATF_SHELL . .El .Sh ENVIRONMENT .Bl -tag -width ATFXLIBEXECDIRXX -compact .It Va ATF_LIBEXECDIR Overrides the builtin directory where .Nm is located. Should not be overridden other than for testing purposes. .It Va ATF_PKGDATADIR Overrides the builtin directory where .Pa libatf-sh.subr is located. Should not be overridden other than for testing purposes. .It Va ATF_SHELL Path to the system shell to be used in the generated scripts. Scripts must not rely on this variable being set to select a specific interpreter. +.It Va ATF_SHELL +Path to the system shell to be used in the generated scripts. .El .Sh EXAMPLES Scripts using .Xr atf-sh 3 should start with: .Bd -literal -offset indent #! /usr/bin/env atf-sh .Ed .Pp Alternatively, if you want to explicitly choose a shell interpreter, you cannot rely on .Xr env 1 to find .Nm . Instead, you have to hardcode the path to .Nm in the script and then use the .Fl s option afterwards as a .Em single parameter : .Bd -literal -offset indent #! /path/to/bin/atf-sh -s/bin/bash .Ed -.Sh ENVIRONMENT -.Bl -tag -width ATFXSHELLXX -compact -.It Va ATF_SHELL -Path to the system shell to be used in the generated scripts. -.El .Sh SEE ALSO .Xr atf-sh 3 Index: stable/10/contrib/atf/atf-sh/atf-sh.3 =================================================================== --- stable/10/contrib/atf/atf-sh/atf-sh.3 (revision 315182) +++ stable/10/contrib/atf/atf-sh/atf-sh.3 (revision 315183) @@ -1,372 +1,372 @@ .\" Copyright (c) 2008 The NetBSD Foundation, Inc. .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND .\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF .\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. .\" IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY .\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE .\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER .\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN .\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -.Dd October 13, 2014 +.Dd March 6, 2017 .Dt ATF-SH 3 .Os .Sh NAME .Nm atf_add_test_case , .Nm atf_check , .Nm atf_check_equal , .Nm atf_config_get , .Nm atf_config_has , .Nm atf_expect_death , .Nm atf_expect_exit , .Nm atf_expect_fail , .Nm atf_expect_pass , .Nm atf_expect_signal , .Nm atf_expect_timeout , .Nm atf_fail , .Nm atf_get , .Nm atf_get_srcdir , .Nm atf_pass , .Nm atf_require_prog , .Nm atf_set , .Nm atf_skip , .Nm atf_test_case .Nd POSIX shell API to write ATF-based test programs .Sh SYNOPSIS .Nm atf_add_test_case .Qq name .Nm atf_check .Qq command .Nm atf_check_equal .Qq expected_expression .Qq actual_expression .Nm atf_config_get .Qq var_name .Nm atf_config_has .Qq var_name .Nm atf_expect_death .Qq reason .Qq ... .Nm atf_expect_exit .Qq exitcode .Qq reason .Qq ... .Nm atf_expect_fail .Qq reason .Qq ... .Nm atf_expect_pass .Qq .Nm atf_expect_signal .Qq signo .Qq reason .Qq ... .Nm atf_expect_timeout .Qq reason .Qq ... .Nm atf_fail .Qq reason .Nm atf_get .Qq var_name .Nm atf_get_srcdir .Nm atf_pass .Nm atf_require_prog .Qq prog_name .Nm atf_set .Qq var_name .Qq value .Nm atf_skip .Qq reason .Nm atf_test_case .Qq name .Qq cleanup .Sh DESCRIPTION ATF provides a simple but powerful interface to easily write test programs in the POSIX shell language. These are extremely helpful given that they are trivial to write due to the language simplicity and the great deal of available external tools, so they are often ideal to test other applications at the user level. .Pp Test programs written using this library must be run using the .Xr atf-sh 1 interpreter by putting the following on their very first line: .Bd -literal -offset indent #! /usr/bin/env atf-sh .Ed .Pp Shell-based test programs always follow this template: .Bd -literal -offset indent atf_test_case tc1 tc1_head() { ... first test case's header ... } tc1_body() { ... first test case's body ... } atf_test_case tc2 cleanup tc2_head() { ... second test case's header ... } tc2_body() { ... second test case's body ... } tc2_cleanup() { ... second test case's cleanup ... } -.Ns ... additional test cases ... +\&... additional test cases ... atf_init_test_cases() { atf_add_test_case tc1 atf_add_test_case tc2 ... add additional test cases ... } .Ed .Ss Definition of test cases Test cases have an identifier and are composed of three different parts: the header, the body and an optional cleanup routine, all of which are described in .Xr atf-test-case 4 . To define test cases, one can use the .Nm atf_test_case -function, which takes a first parameter specifiying the test case's +function, which takes a first parameter specifying the test case's name and instructs the library to set things up to accept it as a valid test case. The second parameter is optional and, if provided, must be .Sq cleanup ; providing this parameter allows defining a cleanup routine for the test case. It is important to note that this function .Em does not set the test case up for execution when the program is run. In order to do so, a later registration is needed through the .Nm atf_add_test_case function detailed in .Sx Program initialization . .Pp Later on, one must define the three parts of the body by providing two or three functions (remember that the cleanup routine is optional). These functions are named after the test case's identifier, and are .Nm \*(Ltid\*(Gt_head , .Nm \*(Ltid\*(Gt_body and .Nm \*(Ltid\*(Gt_cleanup . None of these take parameters when executed. .Ss Program initialization The test program must define an .Nm atf_init_test_cases function, which is in charge of registering the test cases that will be executed at run time by using the .Nm atf_add_test_case function, which takes the name of a test case as its single parameter. This main function should not do anything else, except maybe sourcing auxiliary source files that define extra variables and functions. .Ss Configuration variables The test case has read-only access to the current configuration variables through the .Nm atf_config_has and .Nm atf_config_get methods. The former takes a single parameter specifying a variable name and returns a boolean indicating whether the variable is defined or not. The latter can take one or two parameters. If it takes only one, it specifies the variable from which to get the value, and this variable must be defined. If it takes two, the second one specifies a default value to be returned if the variable is not available. .Ss Access to the source directory It is possible to get the path to the test case's source directory from anywhere in the test program by using the .Nm atf_get_srcdir function. It is interesting to note that this can be used inside .Nm atf_init_test_cases to silently include additional helper files from the source directory. .Ss Requiring programs Aside from the .Va require.progs meta-data variable available in the header only, one can also check for additional programs in the test case's body by using the .Nm atf_require_prog function, which takes the base name or full path of a single binary. Relative paths are forbidden. If it is not found, the test case will be automatically skipped. .Ss Test case finalization The test case finalizes either when the body reaches its end, at which point the test is assumed to have .Em passed , or at any explicit call to .Nm atf_pass , .Nm atf_fail or .Nm atf_skip . These three functions terminate the execution of the test case immediately. The cleanup routine will be processed afterwards in a completely automated way, regardless of the test case's termination reason. .Pp .Nm atf_pass does not take any parameters. .Nm atf_fail and .Nm atf_skip take a single string parameter that describes why the test case failed or was skipped, respectively. It is very important to provide a clear error message in both cases so that the user can quickly know why the test did not pass. .Ss Expectations Everything explained in the previous section changes when the test case expectations are redefined by the programmer. .Pp Each test case has an internal state called .Sq expect that describes what the test case expectations are at any point in time. The value of this property can change during execution by any of: .Bl -tag -width indent .It Nm atf_expect_death Qo reason Qc Qo ... Qc Expects the test case to exit prematurely regardless of the nature of the exit. .It Nm atf_expect_exit Qo exitcode Qc Qo reason Qc Qo ... Qc Expects the test case to exit cleanly. If .Va exitcode is not .Sq -1 , the runtime engine will validate that the exit code of the test case matches the one provided in this call. Otherwise, the exact value will be ignored. .It Nm atf_expect_fail Qo reason Qc Any failure raised in this mode is recorded, but such failures do not report the test case as failed; instead, the test case finalizes cleanly and is reported as .Sq expected failure ; this report includes the provided .Fa reason as part of it. If no error is raised while running in this mode, then the test case is reported as .Sq failed . .Pp This mode is useful to reproduce actual known bugs in tests. Whenever the developer fixes the bug later on, the test case will start reporting a failure, signaling the developer that the test case must be adjusted to the new conditions. In this situation, it is useful, for example, to set .Fa reason as the bug number for tracking purposes. .It Nm atf_expect_pass This is the normal mode of execution. In this mode, any failure is reported as such to the user and the test case is marked as .Sq failed . .It Nm atf_expect_signal Qo signo Qc Qo reason Qc Qo ... Qc Expects the test case to terminate due to the reception of a signal. If .Va signo is not .Sq -1 , the runtime engine will validate that the signal that terminated the test case matches the one provided in this call. Otherwise, the exact value will be ignored. .It Nm atf_expect_timeout Qo reason Qc Qo ... Qc Expects the test case to execute for longer than its timeout. .El .Ss Helper functions for common checks .Bl -tag -width indent .It Nm atf_check Qo [options] Qc Qo command Qc Qo [args] Qc Executes a command, performs checks on its exit code and its output, and fails the test case if any of the checks is not successful. This function is particularly useful in integration tests that verify the correct functioning of a binary. .Pp Internally, this function is just a wrapper over the .Xr atf-check 1 tool (whose manual page provides all details on the calling syntax). You should always use the .Nm atf_check function instead of the .Xr atf-check 1 tool in your scripts; the latter is not even in the path. .It Nm atf_check_equal Qo expected_expression Qc Qo actual_expression Qc This function takes two expressions, evaluates them and, if their results differ, aborts the test case with an appropriate failure message. The common style is to put the expected value in the first parameter and the actual value in the second parameter. .El .Sh EXAMPLES The following shows a complete test program with a single test case that validates the addition operator: .Bd -literal -offset indent atf_test_case addition addition_head() { atf_set "descr" "Sample tests for the addition operator" } addition_body() { atf_check_equal 0 $((0 + 0)) atf_check_equal 1 $((0 + 1)) atf_check_equal 1 $((1 + 0)) atf_check_equal 2 $((1 + 1)) atf_check_equal 300 $((100 + 200)) } atf_init_test_cases() { atf_add_test_case addition } .Ed .Pp This other example shows how to include a file with extra helper functions in the test program: .Bd -literal -offset indent -.Ns ... definition of test cases ... +\&... definition of test cases ... atf_init_test_cases() { . $(atf_get_srcdir)/helper_functions.sh atf_add_test_case foo1 atf_add_test_case foo2 } .Ed .Pp This example demonstrates the use of the very useful .Nm atf_check function: .Bd -literal -offset indent # Check for silent output atf_check -s exit:0 -o empty -e empty 'true' # Check for silent output and failure atf_check -s exit:1 -o empty -e empty 'false' # Check for known stdout and silent stderr echo foo >expout atf_check -s exit:0 -o file:expout -e empty 'echo foo' # Generate a file for later inspection atf_check -s exit:0 -o save:stdout -e empty 'ls' grep foo ls || atf_fail "foo file not found in listing" # Or just do the match along the way atf_check -s exit:0 -o match:"^foo$" -e empty 'ls' .Ed .Sh SEE ALSO .Xr atf-check 1 , .Xr atf-sh 1 , .Xr atf-test-program 1 , .Xr atf-test-case 4 Index: stable/10/contrib/atf/doc/atf-test-case.4 =================================================================== --- stable/10/contrib/atf/doc/atf-test-case.4 (revision 315182) +++ stable/10/contrib/atf/doc/atf-test-case.4 (revision 315183) @@ -1,321 +1,321 @@ .\" Copyright (c) 2007 The NetBSD Foundation, Inc. .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND .\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF .\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. .\" IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY .\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE .\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER .\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN .\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -.Dd October 5, 2014 +.Dd March 6, 2017 .Dt ATF-TEST-CASE 4 .Os .Sh NAME .Nm atf-test-case .Nd generic description of test cases .Sh DESCRIPTION A .Em test case is a piece of code that stress-tests a specific feature of the software. This feature is typically self-contained enough, either in the amount of code that implements it or in the general idea that describes it, to warrant its independent testing. Given this, test cases are very fine-grained, but they attempt to group similar smaller tests which are semantically related. .Pp A test case is defined by three components regardless of the language it is implemented in: a header, a body and a cleanup routine. The .Em header is, basically, a declarative piece of code that defines several properties to describe what the test case does and how it behaves. In other words: it defines the test case's .Em meta-data , further described in the .Sx Meta-data section. The .Em body is the test case itself. It executes all actions needed to reproduce the test, and checks for failures. This body is only executed if the abstract conditions specified by the header are met. The .Em cleanup routine is a piece of code always executed after the body, regardless of the exit status of the test case. It can be used to undo side-effects of the test case. Note that almost all side-effects of a test case are automatically cleaned up by the library; this is explained in more detail in the rest of this document. .Pp It is extremely important to keep the separation between a test case's header and body well-defined, because the header is .Em always parsed, whereas the body is only executed when the conditions defined in the header are met and when the user specifies that test case. .Pp At last, test cases are always contained into test programs. The test programs act as a front-end to them, providing a consistent interface to the user and several APIs to ease their implementation. .Ss Results Upon termination, a test case reports a status and, optionally, a textual reason describing why the test reported such status. The caller must ensure that the test case really performed the task that its status describes, as the test program may be bogus and therefore providing a -misleading result (e.g. providing a result that indicates success but the -error code of the program says otherwise). +misleading result, e.g., providing a result that indicates success but the +error code of the program says otherwise. .Pp The possible exit status of a test case are one of the following: .Bl -tag -width expectedXfailureXX .It expected_death The test case expects to terminate abruptly. .It expected_exit The test case expects to exit cleanly. .It expected_failure The test case expects to exit with a controller fatal/non-fatal failure. If this happens, the test program exits with a success error code. .It expected_signal The test case expects to receive a signal that makes it terminate. .It expected_timeout The test case expects to execute for longer than its timeout. .It passed The test case was executed successfully. The test program exits with a success error code. .It skipped The test case could not be executed because some preconditions were not met. This is not a failure because it can typically be resolved by adjusting the system to meet the necessary conditions. This is always accompanied by a .Em reason , a message describing why the test was skipped. The test program exits with a success error code. .It failed An error appeared during the execution of the test case. This is always accompanied by a .Em reason , a message describing why the test failed. The test program exits with a failure error code. .El .Pp The usefulness of the .Sq expected_* results comes when writing test cases that verify known failures caused, in general, due to programming errors (aka bugs). Whenever the faulty condition that the .Sq expected_* result is trying to cover is fixed, then the test case will be reported as .Sq failed and the developer will have to adjust it to match its new condition. .Pp It is important to note that all .Sq expected_* results are only provided as a .Em hint to the caller; the caller must verify that the test case did actually terminate as the expected condition says. .Ss Input/output Test cases are free to print whatever they want to their .Xr stdout 4 and .Xr stderr 4 file descriptors. They are, in fact, encouraged to print status information as they execute to keep the user informed of their actions. This is specially important for long test cases. .Pp Test cases will log their results to an auxiliary file, which is then collected by the test program they are contained in. The developer need not care about this as long as he uses the correct APIs to implement the test cases. .Pp The standard input of the test cases is unconditionally connected to .Sq /dev/zero . .Ss Meta-data The following list describes all meta-data properties interpreted internally by ATF. You are free to define new properties in your test cases and use them as you wish, but non-standard properties must be prefixed by .Sq X- . .Bl -tag -width requireXmachineXX .It descr Type: textual. Required. .Pp A brief textual description of the test case's purpose. Will be shown to the user in reports. Also good for documentation purposes. .It has.cleanup Type: boolean. Optional. .Pp If set to true, specifies that the test case has a cleanup routine that has to be executed by the runtime engine during the cleanup phase of the execution. This property is automatically set by the framework when defining a test case with a cleanup routine, so it should never be set by hand. .It ident Type: textual. Required. .Pp The test case's identifier. Must be unique inside the test program and should be short but descriptive. .It require.arch Type: textual. Optional. .Pp A whitespace separated list of architectures that the test case can be run under without causing errors due to an architecture mismatch. .It require.config Type: textual. Optional. .Pp A whitespace separated list of configuration variables that must be defined to execute the test case. If any of the required variables is not defined, the test case is .Em skipped . .It require.diskspace Type: integer. Optional. Specifies the minimum amount of available disk space needed by the test. The value can have a size suffix such as .Sq K , .Sq M , .Sq G or .Sq T to make the amount of bytes easier to type and read. .It require.files Type: textual. Optional. .Pp A whitespace separated list of files that must be present to execute the test case. The names of these files must be absolute paths. If any of the required files is not found, the test case is .Em skipped . .It require.machine Type: textual. Optional. .Pp A whitespace separated list of machine types that the test case can be run under without causing errors due to a machine type mismatch. .It require.memory Type: integer. Optional. Specifies the minimum amount of physical memory needed by the test. The value can have a size suffix such as .Sq K , .Sq M , .Sq G or .Sq T to make the amount of bytes easier to type and read. .It require.progs Type: textual. Optional. .Pp A whitespace separated list of programs that must be present to execute the test case. These can be given as plain names, in which case they are looked in the user's .Ev PATH , or as absolute paths. If any of the required programs is not found, the test case is .Em skipped . .It require.user Type: textual. Optional. .Pp The required privileges to execute the test case. Can be one of .Sq root or .Sq unprivileged . .Pp If the test case is running as a regular user and this property is .Sq root , the test case is .Em skipped . .Pp If the test case is running as root and this property is .Sq unprivileged , the runtime engine will automatically drop the privileges if the .Sq unprivileged-user configuration property is set; otherwise the test case is .Em skipped . .It timeout Type: integral. Optional; defaults to .Sq 300 . .Pp Specifies the maximum amount of time the test case can run. This is particularly useful because some tests can stall either because they are incorrectly coded or because they trigger an anomalous behavior of the program. It is not acceptable for these tests to stall the whole execution of the test program. .Pp Can optionally be set to zero, in which case the test case has no run-time limit. This is discouraged. .El .Ss Environment Every time a test case is executed, several environment variables are cleared or reseted to sane values to ensure they do not make the test fail due to unexpected conditions. These variables are: .Bl -tag -width LCXMESSAGESXX .It Ev HOME Set to the work directory's path. .It Ev LANG Undefined. .It Ev LC_ALL Undefined. .It Ev LC_COLLATE Undefined. .It Ev LC_CTYPE Undefined. .It Ev LC_MESSAGES Undefined. .It Ev LC_MONETARY Undefined. .It Ev LC_NUMERIC Undefined. .It Ev LC_TIME Undefined. .It Ev TZ Hardcoded to .Sq UTC . .El .Ss Work directories The test program always creates a temporary directory and switches to it before running the test case's body. This way the test case is free to modify its current directory as it wishes, and the runtime engine will be able to clean it up later on in a safe way, removing any traces of its execution from the system. To do so, the runtime engine will perform a recursive removal of the work directory without crossing mount points; if a mount point is found, the file system will be unmounted (if possible). .Ss File creation mode mask (umask) Test cases are always executed with a file creation mode mask (umask) of .Sq 0022 . The test case's code is free to change this during execution. .Sh SEE ALSO .Xr atf-test-program 1 Index: stable/10 =================================================================== --- stable/10 (revision 315182) +++ stable/10 (revision 315183) Property changes on: stable/10 ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /head:r314793,314796-314805