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