printf_test.c revision 292656
1/* $FreeBSD: stable/10/share/examples/tests/tests/atf/printf_test.c 292656 2015-12-23 10:34:11Z ngie $
2 *
3 * Copyright 2013 Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 *   notice, this list of conditions and the following disclaimer.
12 * * 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 * * Neither the name of Google Inc. nor the names of its contributors
16 *   may be used to endorse or promote products derived from this software
17 *   without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
30
31/*
32 * INTRODUCTION
33 *
34 * This sample test program implements various test cases for the printf(3)
35 * family of functions in order to demonstrate the usage of the ATF C API
36 * (see atf-c-api(3)).
37 *
38 * Note that this test program is called printf_test because it is intended
39 * to validate various functions of the printf(3) family.  For this reason,
40 * each test is prefixed with the name of the function under test followed
41 * by a description of the specific condition being validated.  You should
42 * use a similar naming scheme for your own tests.
43 */
44
45#include <atf-c.h>
46#include <stdio.h>
47#include <string.h>
48
49/*
50 * This is the simplest form of a test case definition: a test case
51 * without a header.
52 *
53 * In most cases, this is the definition you will want to use.  However,
54 * make absolutely sure that the test case name is descriptive enough.
55 * Multi-word test case names are encouraged.  Keep in mind that these
56 * are exposed to the reader in the test reports, and the goal is for
57 * the combination of the test program plus the name of the test case to
58 * give a pretty clear idea of what specific condition the test is
59 * validating.
60 */
61ATF_TC_WITHOUT_HEAD(snprintf__two_formatters);
62ATF_TC_BODY(snprintf__two_formatters, tc)
63{
64	char buffer[128];
65
66	/* This first require-style check invokes the function we are
67	 * interested in testing.  This will cause the test to fail if
68	 * the condition provided to ATF_REQUIRE is not met. */
69	ATF_REQUIRE(snprintf(buffer, sizeof(buffer), "%s, %s!",
70	    "Hello", "tests") > 0);
71
72	/* This second check-style check compares that the result of the
73	 * snprintf call we performed above is correct.  We use a check
74	 * instead of a require. */
75	ATF_CHECK_STREQ("Hello, tests!", buffer);
76}
77
78/*
79 * This is a more complex form of a test case definition: a test case
80 * with a header and a body.  You should always favor the simpler
81 * definition above unless you have to override specific metadata
82 * variables.
83 *
84 * See atf-test-case(4) and kyua-atf-interface(1) for details on all
85 * available properties.
86 */
87ATF_TC(snprintf__overflow);
88ATF_TC_HEAD(snprintf__overflow, tc)
89{
90	/* In this specific case, we define a textual description for
91	 * the test case, which is later exported to the reports for
92	 * documentation purposes.
93	 *
94	 * However, note again that you should favor highly descriptive
95	 * test case names to textual descriptions.  */
96	atf_tc_set_md_var(tc, "descr", "This test case validates the proper "
97	    "truncation of the output string from snprintf when it does not "
98	    "fit the provided buffer.");
99}
100ATF_TC_BODY(snprintf__overflow, tc)
101{
102	char buffer[10];
103
104	/* This is a similar test to the above, but in this case we do the
105	 * test ourselves and forego the ATF_* macros.  Note that we use the
106	 * atf_tc_fail() function instead of exit(2) or similar because we
107	 * want Kyua to have access to the failure message.
108	 *
109	 * In general, prefer using the ATF_* macros wherever possible.  Only
110	 * resort to manual tests when the macros are unsuitable (and consider
111	 * filing a feature request to get a new macro if you think your case
112	 * is generic enough). */
113	if (snprintf(buffer, sizeof(buffer), "0123456789abcdef") != 16)
114		atf_tc_fail("snprintf did not return the expected number "
115		    "of characters");
116
117	ATF_CHECK(strcmp(buffer, "012345678") == 0);
118}
119
120/*
121 * Another simple test case, but this time with side-effects.  This
122 * particular test case modifies the contents of the current directory
123 * and does not clean up after itself, which is perfectly fine.
124 */
125ATF_TC_WITHOUT_HEAD(fprintf__simple_string);
126ATF_TC_BODY(fprintf__simple_string, tc)
127{
128	const char *contents = "This is a message\n";
129
130	FILE *output = fopen("test.txt", "w");
131	ATF_REQUIRE(fprintf(output, "%s", contents) > 0);
132	fclose(output);
133
134	/* The ATF C library provides more than just macros to verify the
135	 * outcome of expressions.  It also includes various helper functions
136	 * to work with files and processes.  Here is just a simple
137	 * example. */
138	ATF_REQUIRE(atf_utils_compare_file("test.txt", contents));
139
140	/* Of special note here is that we are NOT deleting the
141	 * temporary files we created in this test.  Kyua takes care of
142	 * this cleanup automatically and tests can (and should) rely on
143	 * this behavior. */
144}
145
146/*
147 * Lastly, we tell ATF which test cases exist in this program.  This
148 * function should not do anything other than this registration.
149 */
150ATF_TP_ADD_TCS(tp)
151{
152	ATF_TP_ADD_TC(tp, snprintf__two_formatters);
153	ATF_TP_ADD_TC(tp, snprintf__overflow);
154	ATF_TP_ADD_TC(tp, fprintf__simple_string);
155
156	return (atf_no_error());
157}
158