3 * Copyright 2013 Google Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
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.
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. */
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
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.
50 * This is the simplest form of a test case definition: a test case
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
61 ATF_TC_WITHOUT_HEAD(snprintf__two_formatters);
62 ATF_TC_BODY(snprintf__two_formatters, tc)
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);
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);
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
84 * See atf-test-case(4) and kyua-atf-interface(1) for details on all
85 * available properties.
87 ATF_TC(snprintf__overflow);
88 ATF_TC_HEAD(snprintf__overflow, tc)
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.
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.");
100 ATF_TC_BODY(snprintf__overflow, tc)
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.
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 "
117 ATF_CHECK(strcmp(buffer, "012345678") == 0);
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.
125 ATF_TC_WITHOUT_HEAD(fprintf__simple_string);
126 ATF_TC_BODY(fprintf__simple_string, tc)
128 const char *contents = "This is a message\n";
130 FILE *output = fopen("test.txt", "w");
131 ATF_REQUIRE(fprintf(output, "%s", contents) > 0);
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
138 ATF_REQUIRE(atf_utils_compare_file("test.txt", contents));
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
147 * Lastly, we tell ATF which test cases exist in this program. This
148 * function should not do anything other than this registration.
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);
156 return (atf_no_error());