1.\" 2.\" Automated Testing Framework (atf) 3.\" 4.\" Copyright (c) 2008 The NetBSD Foundation, Inc. 5.\" All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 17.\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 18.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20.\" IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY 21.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 23.\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25.\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27.\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28.\" 29.Dd November 15, 2013 30.Dt ATF-C++-API 3 31.Os 32.Sh NAME 33.Nm atf-c++-api , 34.Nm ATF_ADD_TEST_CASE , 35.Nm ATF_CHECK_ERRNO , 36.Nm ATF_FAIL , 37.Nm ATF_INIT_TEST_CASES , 38.Nm ATF_PASS , 39.Nm ATF_REQUIRE , 40.Nm ATF_REQUIRE_EQ , 41.Nm ATF_REQUIRE_ERRNO , 42.Nm ATF_REQUIRE_IN , 43.Nm ATF_REQUIRE_MATCH , 44.Nm ATF_REQUIRE_NOT_IN , 45.Nm ATF_REQUIRE_THROW , 46.Nm ATF_REQUIRE_THROW_RE , 47.Nm ATF_SKIP , 48.Nm ATF_TEST_CASE , 49.Nm ATF_TEST_CASE_BODY , 50.Nm ATF_TEST_CASE_CLEANUP , 51.Nm ATF_TEST_CASE_HEAD , 52.Nm ATF_TEST_CASE_NAME , 53.Nm ATF_TEST_CASE_USE , 54.Nm ATF_TEST_CASE_WITH_CLEANUP , 55.Nm ATF_TEST_CASE_WITHOUT_HEAD , 56.Nm atf::utils::cat_file , 57.Nm atf::utils::compare_file , 58.Nm atf::utils::copy_file , 59.Nm atf::utils::create_file , 60.Nm atf::utils::file_exists , 61.Nm atf::utils::fork , 62.Nm atf::utils::grep_collection , 63.Nm atf::utils::grep_file , 64.Nm atf::utils::grep_string , 65.Nm atf::utils::redirect , 66.Nm atf::utils::wait 67.Nd C++ API to write ATF-based test programs 68.Sh SYNOPSIS 69.In atf-c++.hpp 70.Fn ATF_ADD_TEST_CASE "tcs" "name" 71.Fn ATF_CHECK_ERRNO "exp_errno" "bool_expression" 72.Fn ATF_FAIL "reason" 73.Fn ATF_INIT_TEST_CASES "tcs" 74.Fn ATF_PASS 75.Fn ATF_REQUIRE "expression" 76.Fn ATF_REQUIRE_EQ "expression_1" "expression_2" 77.Fn ATF_REQUIRE_ERRNO "exp_errno" "bool_expression" 78.Fn ATF_REQUIRE_IN "element" "collection" 79.Fn ATF_REQUIRE_MATCH "regexp" "string_expression" 80.Fn ATF_REQUIRE_NOT_IN "element" "collection" 81.Fn ATF_REQUIRE_THROW "expected_exception" "statement" 82.Fn ATF_REQUIRE_THROW_RE "expected_exception" "regexp" "statement" 83.Fn ATF_SKIP "reason" 84.Fn ATF_TEST_CASE "name" 85.Fn ATF_TEST_CASE_BODY "name" 86.Fn ATF_TEST_CASE_CLEANUP "name" 87.Fn ATF_TEST_CASE_HEAD "name" 88.Fn ATF_TEST_CASE_NAME "name" 89.Fn ATF_TEST_CASE_USE "name" 90.Fn ATF_TEST_CASE_WITH_CLEANUP "name" 91.Fn ATF_TEST_CASE_WITHOUT_HEAD "name" 92.Ft void 93.Fo atf::utils::cat_file 94.Fa "const std::string& path" 95.Fa "const std::string& prefix" 96.Fc 97.Ft bool 98.Fo atf::utils::compare_file 99.Fa "const std::string& path" 100.Fa "const std::string& contents" 101.Fc 102.Ft void 103.Fo atf::utils::copy_file 104.Fa "const std::string& source" 105.Fa "const std::string& destination" 106.Fc 107.Ft void 108.Fo atf::utils::create_file 109.Fa "const std::string& path" 110.Fa "const std::string& contents" 111.Fc 112.Ft void 113.Fo atf::utils::file_exists 114.Fa "const std::string& path" 115.Fc 116.Ft pid_t 117.Fo atf::utils::fork 118.Fa "void" 119.Fc 120.Ft bool 121.Fo atf::utils::grep_collection 122.Fa "const std::string& regexp" 123.Fa "const Collection& collection" 124.Fc 125.Ft bool 126.Fo atf::utils::grep_file 127.Fa "const std::string& regexp" 128.Fa "const std::string& path" 129.Fc 130.Ft bool 131.Fo atf::utils::grep_string 132.Fa "const std::string& regexp" 133.Fa "const std::string& path" 134.Fc 135.Ft void 136.Fo atf::utils::redirect 137.Fa "const int fd" 138.Fa "const std::string& path" 139.Fc 140.Ft void 141.Fo atf::utils::wait 142.Fa "const pid_t pid" 143.Fa "const int expected_exit_status" 144.Fa "const std::string& expected_stdout" 145.Fa "const std::string& expected_stderr" 146.Fc 147.Sh DESCRIPTION 148ATF provides a C++ programming interface to implement test programs. 149C++-based test programs follow this template: 150.Bd -literal -offset indent 151extern "C" { 152.Ns ... C-specific includes go here ... 153} 154 155.Ns ... C++-specific includes go here ... 156 157#include <atf-c++.hpp> 158 159ATF_TEST_CASE(tc1); 160ATF_TEST_CASE_HEAD(tc1) 161{ 162 ... first test case's header ... 163} 164ATF_TEST_CASE_BODY(tc1) 165{ 166 ... first test case's body ... 167} 168 169ATF_TEST_CASE_WITH_CLEANUP(tc2); 170ATF_TEST_CASE_HEAD(tc2) 171{ 172 ... second test case's header ... 173} 174ATF_TEST_CASE_BODY(tc2) 175{ 176 ... second test case's body ... 177} 178ATF_TEST_CASE_CLEANUP(tc2) 179{ 180 ... second test case's cleanup ... 181} 182 183ATF_TEST_CASE(tc3); 184ATF_TEST_CASE_BODY(tc3) 185{ 186 ... third test case's body ... 187} 188 189.Ns ... additional test cases ... 190 191ATF_INIT_TEST_CASES(tcs) 192{ 193 ATF_ADD_TEST_CASE(tcs, tc1); 194 ATF_ADD_TEST_CASE(tcs, tc2); 195 ATF_ADD_TEST_CASE(tcs, tc3); 196 ... add additional test cases ... 197} 198.Ed 199.Ss Definition of test cases 200Test cases have an identifier and are composed of three different parts: 201the header, the body and an optional cleanup routine, all of which are 202described in 203.Xr atf-test-case 4 . 204To define test cases, one can use the 205.Fn ATF_TEST_CASE , 206.Fn ATF_TEST_CASE_WITH_CLEANUP 207or the 208.Fn ATF_TEST_CASE_WITHOUT_HEAD 209macros, which take a single parameter specifiying the test case's 210name. 211.Fn ATF_TEST_CASE , 212requires to define a head and a body for the test case, 213.Fn ATF_TEST_CASE_WITH_CLEANUP 214requires to define a head, a body and a cleanup for the test case and 215.Fn ATF_TEST_CASE_WITHOUT_HEAD 216requires only a body for the test case. 217It is important to note that these 218.Em do not 219set the test case up for execution when the program is run. 220In order to do so, a later registration is needed through the 221.Fn ATF_ADD_TEST_CASE 222macro detailed in 223.Sx Program initialization . 224.Pp 225Later on, one must define the three parts of the body by means of three 226functions. 227Their headers are given by the 228.Fn ATF_TEST_CASE_HEAD , 229.Fn ATF_TEST_CASE_BODY 230and 231.Fn ATF_TEST_CASE_CLEANUP 232macros, all of which take the test case's name. 233Following each of these, a block of code is expected, surrounded by the 234opening and closing brackets. 235.Pp 236Additionally, the 237.Fn ATF_TEST_CASE_NAME 238macro can be used to obtain the name of the class corresponding to a 239particular test case, as the name is internally manged by the library to 240prevent clashes with other user identifiers. 241Similarly, the 242.Fn ATF_TEST_CASE_USE 243macro can be executed on a particular test case to mark it as "used" and 244thus prevent compiler warnings regarding unused symbols. 245Note that 246.Em you should never have to use these macros during regular operation. 247.Ss Program initialization 248The library provides a way to easily define the test program's 249.Fn main 250function. 251You should never define one on your own, but rely on the 252library to do it for you. 253This is done by using the 254.Fn ATF_INIT_TEST_CASES 255macro, which is passed the name of the list that will hold the test cases. 256This name can be whatever you want as long as it is a valid variable value. 257.Pp 258After the macro, you are supposed to provide the body of a function, which 259should only use the 260.Fn ATF_ADD_TEST_CASE 261macro to register the test cases the test program will execute. 262The first parameter of this macro matches the name you provided in the 263former call. 264.Ss Header definitions 265The test case's header can define the meta-data by using the 266.Fn set_md_var 267method, which takes two parameters: the first one specifies the 268meta-data variable to be set and the second one specifies its value. 269Both of them are strings. 270.Ss Configuration variables 271The test case has read-only access to the current configuration variables 272by means of the 273.Ft bool 274.Fn has_config_var 275and the 276.Ft std::string 277.Fn get_config_var 278methods, which can be called in any of the three parts of a test case. 279.Ss Access to the source directory 280It is possible to get the path to the test case's source directory from any 281of its three components by querying the 282.Sq srcdir 283configuration variable. 284.Ss Requiring programs 285Aside from the 286.Va require.progs 287meta-data variable available in the header only, one can also check for 288additional programs in the test case's body by using the 289.Fn require_prog 290function, which takes the base name or full path of a single binary. 291Relative paths are forbidden. 292If it is not found, the test case will be automatically skipped. 293.Ss Test case finalization 294The test case finalizes either when the body reaches its end, at which 295point the test is assumed to have 296.Em passed , 297or at any explicit call to 298.Fn ATF_PASS , 299.Fn ATF_FAIL 300or 301.Fn ATF_SKIP . 302These three macros terminate the execution of the test case immediately. 303The cleanup routine will be processed afterwards in a completely automated 304way, regardless of the test case's termination reason. 305.Pp 306.Fn ATF_PASS 307does not take any parameters. 308.Fn ATF_FAIL 309and 310.Fn ATF_SKIP 311take a single string that describes why the test case failed or 312was skipped, respectively. 313It is very important to provide a clear error message in both cases so that 314the user can quickly know why the test did not pass. 315.Ss Expectations 316Everything explained in the previous section changes when the test case 317expectations are redefined by the programmer. 318.Pp 319Each test case has an internal state called 320.Sq expect 321that describes what the test case expectations are at any point in time. 322The value of this property can change during execution by any of: 323.Bl -tag -width indent 324.It Fn expect_death "reason" 325Expects the test case to exit prematurely regardless of the nature of the 326exit. 327.It Fn expect_exit "exitcode" "reason" 328Expects the test case to exit cleanly. 329If 330.Va exitcode 331is not 332.Sq -1 , 333.Xr atf-run 1 334will validate that the exit code of the test case matches the one provided 335in this call. 336Otherwise, the exact value will be ignored. 337.It Fn expect_fail "reason" 338Any failure (be it fatal or non-fatal) raised in this mode is recorded. 339However, such failures do not report the test case as failed; instead, the 340test case finalizes cleanly and is reported as 341.Sq expected failure ; 342this report includes the provided 343.Fa reason 344as part of it. 345If no error is raised while running in this mode, then the test case is 346reported as 347.Sq failed . 348.Pp 349This mode is useful to reproduce actual known bugs in tests. 350Whenever the developer fixes the bug later on, the test case will start 351reporting a failure, signaling the developer that the test case must be 352adjusted to the new conditions. 353In this situation, it is useful, for example, to set 354.Fa reason 355as the bug number for tracking purposes. 356.It Fn expect_pass 357This is the normal mode of execution. 358In this mode, any failure is reported as such to the user and the test case 359is marked as 360.Sq failed . 361.It Fn expect_race "reason" 362Any failure or timeout during the execution of the test case will be 363considered as if a race condition has been triggered and reported as such. 364If no problems arise, the test will continue execution as usual. 365.It Fn expect_signal "signo" "reason" 366Expects the test case to terminate due to the reception of a signal. 367If 368.Va signo 369is not 370.Sq -1 , 371.Xr atf-run 1 372will validate that the signal that terminated the test case matches the one 373provided in this call. 374Otherwise, the exact value will be ignored. 375.It Fn expect_timeout "reason" 376Expects the test case to execute for longer than its timeout. 377.El 378.Ss Helper macros for common checks 379The library provides several macros that are very handy in multiple 380situations. 381These basically check some condition after executing a given statement or 382processing a given expression and, if the condition is not met, they 383automatically call 384.Fn ATF_FAIL 385with an appropriate error message. 386.Pp 387.Fn ATF_REQUIRE 388takes an expression and raises a failure if it evaluates to false. 389.Pp 390.Fn ATF_REQUIRE_EQ 391takes two expressions and raises a failure if the two do not evaluate to 392the same exact value. 393.Pp 394.Fn ATF_REQUIRE_IN 395takes an element and a collection and validates that the element is present in 396the collection. 397.Pp 398.Fn ATF_REQUIRE_MATCH 399takes a regular expression and a string and raises a failure if the regular 400expression does not match the string. 401.Pp 402.Fn ATF_REQUIRE_NOT_IN 403takes an element and a collection and validates that the element is not present 404in the collection. 405.Pp 406.Fn ATF_REQUIRE_THROW 407takes the name of an exception and a statement and raises a failure if 408the statement does not throw the specified exception. 409.Fn ATF_REQUIRE_THROW_RE 410takes the name of an exception, a regular expresion and a statement and raises a 411failure if the statement does not throw the specified exception and if the 412message of the exception does not match the regular expression. 413.Pp 414.Fn ATF_CHECK_ERRNO 415and 416.Fn ATF_REQUIRE_ERRNO 417take, first, the error code that the check is expecting to find in the 418.Va errno 419variable and, second, a boolean expression that, if evaluates to true, 420means that a call failed and 421.Va errno 422has to be checked against the first value. 423.Ss Utility functions 424The following functions are provided as part of the 425.Nm 426API to simplify the creation of a variety of tests. 427In particular, these are useful to write tests for command-line interfaces. 428.Pp 429.Ft void 430.Fo atf::utils::cat_file 431.Fa "const std::string& path" 432.Fa "const std::string& prefix" 433.Fc 434.Bd -ragged -offset indent 435Prints the contents of 436.Fa path 437to the standard output, prefixing every line with the string in 438.Fa prefix . 439.Ed 440.Pp 441.Ft bool 442.Fo atf::utils::compare_file 443.Fa "const std::string& path" 444.Fa "const std::string& contents" 445.Fc 446.Bd -ragged -offset indent 447Returns true if the given 448.Fa path 449matches exactly the expected inlined 450.Fa contents . 451.Ed 452.Pp 453.Ft void 454.Fo atf::utils::copy_file 455.Fa "const std::string& source" 456.Fa "const std::string& destination" 457.Fc 458.Bd -ragged -offset indent 459Copies the file 460.Fa source 461to 462.Fa destination . 463The permissions of the file are preserved during the code. 464.Ed 465.Pp 466.Ft void 467.Fo atf::utils::create_file 468.Fa "const std::string& path" 469.Fa "const std::string& contents" 470.Fc 471.Bd -ragged -offset indent 472Creates 473.Fa file 474with the text given in 475.Fa contents . 476.Ed 477.Pp 478.Ft void 479.Fo atf::utils::file_exists 480.Fa "const std::string& path" 481.Fc 482.Bd -ragged -offset indent 483Checks if 484.Fa path 485exists. 486.Ed 487.Pp 488.Ft pid_t 489.Fo atf::utils::fork 490.Fa "void" 491.Fc 492.Bd -ragged -offset indent 493Forks a process and redirects the standard output and standard error of the 494child to files for later validation with 495.Fn atf::utils::wait . 496Fails the test case if the fork fails, so this does not return an error. 497.Ed 498.Pp 499.Ft bool 500.Fo atf::utils::grep_collection 501.Fa "const std::string& regexp" 502.Fa "const Collection& collection" 503.Fc 504.Bd -ragged -offset indent 505Searches for the regular expression 506.Fa regexp 507in any of the strings contained in the 508.Fa collection . 509This is a template that accepts any one-dimensional container of strings. 510.Ed 511.Pp 512.Ft bool 513.Fo atf::utils::grep_file 514.Fa "const std::string& regexp" 515.Fa "const std::string& path" 516.Fc 517.Bd -ragged -offset indent 518Searches for the regular expression 519.Fa regexp 520in the file 521.Fa path . 522The variable arguments are used to construct the regular expression. 523.Ed 524.Pp 525.Ft bool 526.Fo atf::utils::grep_string 527.Fa "const std::string& regexp" 528.Fa "const std::string& str" 529.Fc 530.Bd -ragged -offset indent 531Searches for the regular expression 532.Fa regexp 533in the string 534.Fa str . 535.Ed 536.Ft void 537.Fo atf::utils::redirect 538.Fa "const int fd" 539.Fa "const std::string& path" 540.Fc 541.Bd -ragged -offset indent 542Redirects the given file descriptor 543.Fa fd 544to the file 545.Fa path . 546This function exits the process in case of an error and does not properly mark 547the test case as failed. 548As a result, it should only be used in subprocesses of the test case; specially 549those spawned by 550.Fn atf::utils::fork . 551.Ed 552.Pp 553.Ft void 554.Fo atf::utils::wait 555.Fa "const pid_t pid" 556.Fa "const int expected_exit_status" 557.Fa "const std::string& expected_stdout" 558.Fa "const std::string& expected_stderr" 559.Fc 560.Bd -ragged -offset indent 561Waits and validates the result of a subprocess spawned with 562.Fn atf::utils::wait . 563The validation involves checking that the subprocess exited cleanly and returned 564the code specified in 565.Fa expected_exit_status 566and that its standard output and standard error match the strings given in 567.Fa expected_stdout 568and 569.Fa expected_stderr . 570.Pp 571If any of the 572.Fa expected_stdout 573or 574.Fa expected_stderr 575strings are prefixed with 576.Sq save: , 577then they specify the name of the file into which to store the stdout or stderr 578of the subprocess, and no comparison is performed. 579.Ed 580.Sh EXAMPLES 581The following shows a complete test program with a single test case that 582validates the addition operator: 583.Bd -literal -offset indent 584#include <atf-c++.hpp> 585 586ATF_TEST_CASE(addition); 587ATF_TEST_CASE_HEAD(addition) 588{ 589 set_md_var("descr", "Sample tests for the addition operator"); 590} 591ATF_TEST_CASE_BODY(addition) 592{ 593 ATF_REQUIRE_EQ(0 + 0, 0); 594 ATF_REQUIRE_EQ(0 + 1, 1); 595 ATF_REQUIRE_EQ(1 + 0, 1); 596 597 ATF_REQUIRE_EQ(1 + 1, 2); 598 599 ATF_REQUIRE_EQ(100 + 200, 300); 600} 601 602ATF_TEST_CASE(open_failure); 603ATF_TEST_CASE_HEAD(open_failure) 604{ 605 set_md_var("descr", "Sample tests for the open function"); 606} 607ATF_TEST_CASE_BODY(open_failure) 608{ 609 ATF_REQUIRE_ERRNO(ENOENT, open("non-existent", O_RDONLY) == -1); 610} 611 612ATF_TEST_CASE(known_bug); 613ATF_TEST_CASE_HEAD(known_bug) 614{ 615 set_md_var("descr", "Reproduces a known bug"); 616} 617ATF_TEST_CASE_BODY(known_bug) 618{ 619 expect_fail("See bug number foo/bar"); 620 ATF_REQUIRE_EQ(3, 1 + 1); 621 expect_pass(); 622 ATF_REQUIRE_EQ(3, 1 + 2); 623} 624 625ATF_INIT_TEST_CASES(tcs) 626{ 627 ATF_ADD_TEST_CASE(tcs, addition); 628 ATF_ADD_TEST_CASE(tcs, open_failure); 629 ATF_ADD_TEST_CASE(tcs, known_bug); 630} 631.Ed 632.Sh SEE ALSO 633.Xr atf-test-program 1 , 634.Xr atf-test-case 4 , 635.Xr atf 7 636