1258299Sjmmv/* $FreeBSD$
2258299Sjmmv *
3258299Sjmmv * Copyright 2013 Google Inc.
4258299Sjmmv * All rights reserved.
5258299Sjmmv *
6258299Sjmmv * Redistribution and use in source and binary forms, with or without
7258299Sjmmv * modification, are permitted provided that the following conditions are
8258299Sjmmv * met:
9258299Sjmmv *
10258299Sjmmv * * Redistributions of source code must retain the above copyright
11258299Sjmmv *   notice, this list of conditions and the following disclaimer.
12258299Sjmmv * * Redistributions in binary form must reproduce the above copyright
13258299Sjmmv *   notice, this list of conditions and the following disclaimer in the
14258299Sjmmv *   documentation and/or other materials provided with the distribution.
15258299Sjmmv * * Neither the name of Google Inc. nor the names of its contributors
16258299Sjmmv *   may be used to endorse or promote products derived from this software
17258299Sjmmv *   without specific prior written permission.
18258299Sjmmv *
19258299Sjmmv * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20258299Sjmmv * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21258299Sjmmv * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22258299Sjmmv * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23258299Sjmmv * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24258299Sjmmv * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25258299Sjmmv * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26258299Sjmmv * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27258299Sjmmv * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28258299Sjmmv * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29258299Sjmmv * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
30258299Sjmmv
31258299Sjmmv/*
32258299Sjmmv * INTRODUCTION
33258299Sjmmv *
34258299Sjmmv * This sample test program implements various test cases for the printf(3)
35258299Sjmmv * family of functions in order to demonstrate the usage of the ATF C API
36258299Sjmmv * (see atf-c-api(3)).
37258299Sjmmv *
38258299Sjmmv * Note that this test program is called printf_test because it is intended
39258299Sjmmv * to validate various functions of the printf(3) family.  For this reason,
40258299Sjmmv * each test is prefixed with the name of the function under test followed
41258299Sjmmv * by a description of the specific condition being validated.  You should
42258299Sjmmv * use a similar naming scheme for your own tests.
43258299Sjmmv */
44258299Sjmmv
45258299Sjmmv#include <atf-c.h>
46258299Sjmmv#include <stdio.h>
47258299Sjmmv#include <string.h>
48258299Sjmmv
49258299Sjmmv/*
50258299Sjmmv * This is the simplest form of a test case definition: a test case
51258299Sjmmv * without a header.
52258299Sjmmv *
53258299Sjmmv * In most cases, this is the definition you will want to use.  However,
54258299Sjmmv * make absolutely sure that the test case name is descriptive enough.
55258299Sjmmv * Multi-word test case names are encouraged.  Keep in mind that these
56258299Sjmmv * are exposed to the reader in the test reports, and the goal is for
57258299Sjmmv * the combination of the test program plus the name of the test case to
58258299Sjmmv * give a pretty clear idea of what specific condition the test is
59258299Sjmmv * validating.
60258299Sjmmv */
61258299SjmmvATF_TC_WITHOUT_HEAD(snprintf__two_formatters);
62258299SjmmvATF_TC_BODY(snprintf__two_formatters, tc)
63258299Sjmmv{
64258299Sjmmv	char buffer[128];
65258299Sjmmv
66258299Sjmmv	/* This first require-style check invokes the function we are
67258299Sjmmv	 * interested in testing.  This will cause the test to fail if
68258299Sjmmv	 * the condition provided to ATF_REQUIRE is not met. */
69258299Sjmmv	ATF_REQUIRE(snprintf(buffer, sizeof(buffer), "%s, %s!",
70258299Sjmmv	    "Hello", "tests") > 0);
71258299Sjmmv
72258299Sjmmv	/* This second check-style check compares that the result of the
73258299Sjmmv	 * snprintf call we performed above is correct.  We use a check
74258299Sjmmv	 * instead of a require. */
75258299Sjmmv	ATF_CHECK_STREQ("Hello, tests!", buffer);
76258299Sjmmv}
77258299Sjmmv
78258299Sjmmv/*
79258299Sjmmv * This is a more complex form of a test case definition: a test case
80258299Sjmmv * with a header and a body.  You should always favor the simpler
81258299Sjmmv * definition above unless you have to override specific metadata
82258299Sjmmv * variables.
83258299Sjmmv *
84258299Sjmmv * See atf-test-case(4) and kyua-atf-interface(1) for details on all
85258299Sjmmv * available properties.
86258299Sjmmv */
87258299SjmmvATF_TC(snprintf__overflow);
88258299SjmmvATF_TC_HEAD(snprintf__overflow, tc)
89258299Sjmmv{
90258299Sjmmv	/* In this specific case, we define a textual description for
91258299Sjmmv	 * the test case, which is later exported to the reports for
92258299Sjmmv	 * documentation purposes.
93258299Sjmmv	 *
94258299Sjmmv	 * However, note again that you should favor highly descriptive
95258299Sjmmv	 * test case names to textual descriptions.  */
96258299Sjmmv	atf_tc_set_md_var(tc, "descr", "This test case validates the proper "
97258299Sjmmv	    "truncation of the output string from snprintf when it does not "
98258299Sjmmv	    "fit the provided buffer.");
99258299Sjmmv}
100258299SjmmvATF_TC_BODY(snprintf__overflow, tc)
101258299Sjmmv{
102258299Sjmmv	char buffer[10];
103258299Sjmmv
104258299Sjmmv	/* This is a similar test to the above, but in this case we do the
105258299Sjmmv	 * test ourselves and forego the ATF_* macros.  Note that we use the
106258299Sjmmv	 * atf_tc_fail() function instead of exit(2) or similar because we
107258299Sjmmv	 * want Kyua to have access to the failure message.
108258299Sjmmv	 *
109258299Sjmmv	 * In general, prefer using the ATF_* macros wherever possible.  Only
110258299Sjmmv	 * resort to manual tests when the macros are unsuitable (and consider
111258299Sjmmv	 * filing a feature request to get a new macro if you think your case
112258299Sjmmv	 * is generic enough). */
113258299Sjmmv	if (snprintf(buffer, sizeof(buffer), "0123456789abcdef") != 16)
114258299Sjmmv		atf_tc_fail("snprintf did not return the expected number "
115258299Sjmmv		    "of characters");
116258299Sjmmv
117258299Sjmmv	ATF_CHECK(strcmp(buffer, "012345678") == 0);
118258299Sjmmv}
119258299Sjmmv
120258299Sjmmv/*
121258299Sjmmv * Another simple test case, but this time with side-effects.  This
122258299Sjmmv * particular test case modifies the contents of the current directory
123258299Sjmmv * and does not clean up after itself, which is perfectly fine.
124258299Sjmmv */
125258299SjmmvATF_TC_WITHOUT_HEAD(fprintf__simple_string);
126258299SjmmvATF_TC_BODY(fprintf__simple_string, tc)
127258299Sjmmv{
128258299Sjmmv	const char *contents = "This is a message\n";
129258299Sjmmv
130258299Sjmmv	FILE *output = fopen("test.txt", "w");
131258299Sjmmv	ATF_REQUIRE(fprintf(output, "%s", contents) > 0);
132258299Sjmmv	fclose(output);
133258299Sjmmv
134258299Sjmmv	/* The ATF C library provides more than just macros to verify the
135258299Sjmmv	 * outcome of expressions.  It also includes various helper functions
136258299Sjmmv	 * to work with files and processes.  Here is just a simple
137258299Sjmmv	 * example. */
138258299Sjmmv	ATF_REQUIRE(atf_utils_compare_file("test.txt", contents));
139258299Sjmmv
140258299Sjmmv	/* Of special note here is that we are NOT deleting the
141258299Sjmmv	 * temporary files we created in this test.  Kyua takes care of
142258299Sjmmv	 * this cleanup automatically and tests can (and should) rely on
143258299Sjmmv	 * this behavior. */
144258299Sjmmv}
145258299Sjmmv
146258299Sjmmv/*
147258299Sjmmv * Lastly, we tell ATF which test cases exist in this program.  This
148258299Sjmmv * function should not do anything other than this registration.
149258299Sjmmv */
150258299SjmmvATF_TP_ADD_TCS(tp)
151258299Sjmmv{
152258299Sjmmv	ATF_TP_ADD_TC(tp, snprintf__two_formatters);
153258299Sjmmv	ATF_TP_ADD_TC(tp, snprintf__overflow);
154258299Sjmmv	ATF_TP_ADD_TC(tp, fprintf__simple_string);
155258299Sjmmv}
156