xref: /freebsd-src/share/examples/tests/tests/atf/printf_test.c (revision d1d7a273707a50d4ad1691b2c4dbf645dfa253ea)
1*d1d7a273SEd Maste /*
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
421203fddSJulio Merino  * Copyright 2013 Google Inc.
521203fddSJulio Merino  * All rights reserved.
621203fddSJulio Merino  *
721203fddSJulio Merino  * Redistribution and use in source and binary forms, with or without
821203fddSJulio Merino  * modification, are permitted provided that the following conditions are
921203fddSJulio Merino  * met:
1021203fddSJulio Merino  *
1121203fddSJulio Merino  * * Redistributions of source code must retain the above copyright
1221203fddSJulio Merino  *   notice, this list of conditions and the following disclaimer.
1321203fddSJulio Merino  * * Redistributions in binary form must reproduce the above copyright
1421203fddSJulio Merino  *   notice, this list of conditions and the following disclaimer in the
1521203fddSJulio Merino  *   documentation and/or other materials provided with the distribution.
1621203fddSJulio Merino  * * Neither the name of Google Inc. nor the names of its contributors
1721203fddSJulio Merino  *   may be used to endorse or promote products derived from this software
1821203fddSJulio Merino  *   without specific prior written permission.
1921203fddSJulio Merino  *
2021203fddSJulio Merino  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2121203fddSJulio Merino  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2221203fddSJulio Merino  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2321203fddSJulio Merino  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2421203fddSJulio Merino  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2521203fddSJulio Merino  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2621203fddSJulio Merino  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2721203fddSJulio Merino  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2821203fddSJulio Merino  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2921203fddSJulio Merino  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3021203fddSJulio Merino  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
3121203fddSJulio Merino 
3221203fddSJulio Merino /*
3321203fddSJulio Merino  * INTRODUCTION
3421203fddSJulio Merino  *
3521203fddSJulio Merino  * This sample test program implements various test cases for the printf(3)
3621203fddSJulio Merino  * family of functions in order to demonstrate the usage of the ATF C API
3721203fddSJulio Merino  * (see atf-c-api(3)).
3821203fddSJulio Merino  *
3921203fddSJulio Merino  * Note that this test program is called printf_test because it is intended
4021203fddSJulio Merino  * to validate various functions of the printf(3) family.  For this reason,
4121203fddSJulio Merino  * each test is prefixed with the name of the function under test followed
4221203fddSJulio Merino  * by a description of the specific condition being validated.  You should
4321203fddSJulio Merino  * use a similar naming scheme for your own tests.
4421203fddSJulio Merino  */
4521203fddSJulio Merino 
4621203fddSJulio Merino #include <atf-c.h>
4721203fddSJulio Merino #include <stdio.h>
4821203fddSJulio Merino #include <string.h>
4921203fddSJulio Merino 
5021203fddSJulio Merino /*
5121203fddSJulio Merino  * This is the simplest form of a test case definition: a test case
5221203fddSJulio Merino  * without a header.
5321203fddSJulio Merino  *
5421203fddSJulio Merino  * In most cases, this is the definition you will want to use.  However,
5521203fddSJulio Merino  * make absolutely sure that the test case name is descriptive enough.
5621203fddSJulio Merino  * Multi-word test case names are encouraged.  Keep in mind that these
5721203fddSJulio Merino  * are exposed to the reader in the test reports, and the goal is for
5821203fddSJulio Merino  * the combination of the test program plus the name of the test case to
5921203fddSJulio Merino  * give a pretty clear idea of what specific condition the test is
6021203fddSJulio Merino  * validating.
6121203fddSJulio Merino  */
6221203fddSJulio Merino ATF_TC_WITHOUT_HEAD(snprintf__two_formatters);
ATF_TC_BODY(snprintf__two_formatters,tc)6321203fddSJulio Merino ATF_TC_BODY(snprintf__two_formatters, tc)
6421203fddSJulio Merino {
6521203fddSJulio Merino 	char buffer[128];
6621203fddSJulio Merino 
6721203fddSJulio Merino 	/* This first require-style check invokes the function we are
6821203fddSJulio Merino 	 * interested in testing.  This will cause the test to fail if
6921203fddSJulio Merino 	 * the condition provided to ATF_REQUIRE is not met. */
7021203fddSJulio Merino 	ATF_REQUIRE(snprintf(buffer, sizeof(buffer), "%s, %s!",
7121203fddSJulio Merino 	    "Hello", "tests") > 0);
7221203fddSJulio Merino 
7321203fddSJulio Merino 	/* This second check-style check compares that the result of the
7421203fddSJulio Merino 	 * snprintf call we performed above is correct.  We use a check
7521203fddSJulio Merino 	 * instead of a require. */
7621203fddSJulio Merino 	ATF_CHECK_STREQ("Hello, tests!", buffer);
7721203fddSJulio Merino }
7821203fddSJulio Merino 
7921203fddSJulio Merino /*
8021203fddSJulio Merino  * This is a more complex form of a test case definition: a test case
8121203fddSJulio Merino  * with a header and a body.  You should always favor the simpler
8221203fddSJulio Merino  * definition above unless you have to override specific metadata
8321203fddSJulio Merino  * variables.
8421203fddSJulio Merino  *
8521203fddSJulio Merino  * See atf-test-case(4) and kyua-atf-interface(1) for details on all
8621203fddSJulio Merino  * available properties.
8721203fddSJulio Merino  */
8821203fddSJulio Merino ATF_TC(snprintf__overflow);
ATF_TC_HEAD(snprintf__overflow,tc)8921203fddSJulio Merino ATF_TC_HEAD(snprintf__overflow, tc)
9021203fddSJulio Merino {
9121203fddSJulio Merino 	/* In this specific case, we define a textual description for
9221203fddSJulio Merino 	 * the test case, which is later exported to the reports for
9321203fddSJulio Merino 	 * documentation purposes.
9421203fddSJulio Merino 	 *
9521203fddSJulio Merino 	 * However, note again that you should favor highly descriptive
9621203fddSJulio Merino 	 * test case names to textual descriptions.  */
9721203fddSJulio Merino 	atf_tc_set_md_var(tc, "descr", "This test case validates the proper "
9821203fddSJulio Merino 	    "truncation of the output string from snprintf when it does not "
9921203fddSJulio Merino 	    "fit the provided buffer.");
10021203fddSJulio Merino }
ATF_TC_BODY(snprintf__overflow,tc)10121203fddSJulio Merino ATF_TC_BODY(snprintf__overflow, tc)
10221203fddSJulio Merino {
10321203fddSJulio Merino 	char buffer[10];
10421203fddSJulio Merino 
10521203fddSJulio Merino 	/* This is a similar test to the above, but in this case we do the
10621203fddSJulio Merino 	 * test ourselves and forego the ATF_* macros.  Note that we use the
10721203fddSJulio Merino 	 * atf_tc_fail() function instead of exit(2) or similar because we
10821203fddSJulio Merino 	 * want Kyua to have access to the failure message.
10921203fddSJulio Merino 	 *
11021203fddSJulio Merino 	 * In general, prefer using the ATF_* macros wherever possible.  Only
11121203fddSJulio Merino 	 * resort to manual tests when the macros are unsuitable (and consider
11221203fddSJulio Merino 	 * filing a feature request to get a new macro if you think your case
11321203fddSJulio Merino 	 * is generic enough). */
11421203fddSJulio Merino 	if (snprintf(buffer, sizeof(buffer), "0123456789abcdef") != 16)
11521203fddSJulio Merino 		atf_tc_fail("snprintf did not return the expected number "
11621203fddSJulio Merino 		    "of characters");
11721203fddSJulio Merino 
11821203fddSJulio Merino 	ATF_CHECK(strcmp(buffer, "012345678") == 0);
11921203fddSJulio Merino }
12021203fddSJulio Merino 
12121203fddSJulio Merino /*
12221203fddSJulio Merino  * Another simple test case, but this time with side-effects.  This
12321203fddSJulio Merino  * particular test case modifies the contents of the current directory
12421203fddSJulio Merino  * and does not clean up after itself, which is perfectly fine.
12521203fddSJulio Merino  */
12621203fddSJulio Merino ATF_TC_WITHOUT_HEAD(fprintf__simple_string);
ATF_TC_BODY(fprintf__simple_string,tc)12721203fddSJulio Merino ATF_TC_BODY(fprintf__simple_string, tc)
12821203fddSJulio Merino {
12921203fddSJulio Merino 	const char *contents = "This is a message\n";
13021203fddSJulio Merino 
13121203fddSJulio Merino 	FILE *output = fopen("test.txt", "w");
13221203fddSJulio Merino 	ATF_REQUIRE(fprintf(output, "%s", contents) > 0);
13321203fddSJulio Merino 	fclose(output);
13421203fddSJulio Merino 
13521203fddSJulio Merino 	/* The ATF C library provides more than just macros to verify the
13621203fddSJulio Merino 	 * outcome of expressions.  It also includes various helper functions
13721203fddSJulio Merino 	 * to work with files and processes.  Here is just a simple
13821203fddSJulio Merino 	 * example. */
13921203fddSJulio Merino 	ATF_REQUIRE(atf_utils_compare_file("test.txt", contents));
14021203fddSJulio Merino 
14121203fddSJulio Merino 	/* Of special note here is that we are NOT deleting the
14221203fddSJulio Merino 	 * temporary files we created in this test.  Kyua takes care of
14321203fddSJulio Merino 	 * this cleanup automatically and tests can (and should) rely on
14421203fddSJulio Merino 	 * this behavior. */
14521203fddSJulio Merino }
14621203fddSJulio Merino 
14721203fddSJulio Merino /*
14821203fddSJulio Merino  * Lastly, we tell ATF which test cases exist in this program.  This
14921203fddSJulio Merino  * function should not do anything other than this registration.
15021203fddSJulio Merino  */
ATF_TP_ADD_TCS(tp)15121203fddSJulio Merino ATF_TP_ADD_TCS(tp)
15221203fddSJulio Merino {
15321203fddSJulio Merino 	ATF_TP_ADD_TC(tp, snprintf__two_formatters);
15421203fddSJulio Merino 	ATF_TP_ADD_TC(tp, snprintf__overflow);
15521203fddSJulio Merino 	ATF_TP_ADD_TC(tp, fprintf__simple_string);
1569c7fedcaSEnji Cooper 
1579c7fedcaSEnji Cooper 	return (atf_no_error());
15821203fddSJulio Merino }
159