1 /* 2 * Copyright 2013 Google Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * * Neither the name of Google Inc. nor the names of its contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 29 30 /* 31 * INTRODUCTION 32 * 33 * This plain test program mimics the structure and contents of its 34 * ATF-based counterpart. It attempts to represent various test cases 35 * in different separate functions and just calls them all from main(). 36 * 37 * In reality, plain test programs can be much simpler. All they have 38 * to do is return 0 on success and non-0 otherwise. 39 */ 40 41 #include <err.h> 42 #include <stdarg.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 47 static int failed; 48 static int test_num = 1; 49 50 #define TEST_COUNT 7 51 52 static void 53 fail(const char *fmt, ...) 54 { 55 char *msg; 56 va_list ap; 57 58 failed = 1; 59 60 va_start(ap, fmt); 61 if (vasprintf(&msg, fmt, ap) == -1) 62 err(1, NULL); 63 va_end(ap); 64 printf("not ok %d - %s\n", test_num, msg); 65 free(msg); 66 67 test_num++; 68 } 69 70 static void 71 pass(void) 72 { 73 74 printf("ok %d\n", test_num); 75 test_num++; 76 } 77 78 static void 79 skip(int skip_num) 80 { 81 int i; 82 83 for (i = 0; i < skip_num; i++) { 84 printf("not ok %d # SKIP\n", test_num); 85 test_num++; 86 } 87 } 88 89 static void 90 snprintf__two_formatters(void) 91 { 92 char buffer[128]; 93 94 if (snprintf(buffer, sizeof(buffer), "%s, %s!", "Hello", 95 "tests") <= 0) { 96 fail("snprintf with two formatters failed"); 97 skip(1); 98 } else { 99 pass(); 100 if (strcmp(buffer, "Hello, tests!") != 0) 101 fail("Bad formatting: got %s", buffer); 102 else 103 pass(); 104 } 105 } 106 107 static void 108 snprintf__overflow(void) 109 { 110 char buffer[10]; 111 112 if (snprintf(buffer, sizeof(buffer), "0123456789abcdef") != 16) { 113 fail("snprintf did not return the expected " 114 "number of characters"); 115 skip(1); 116 return; 117 } 118 pass(); 119 120 if (strcmp(buffer, "012345678") != 0) 121 fail("Bad formatting: got %s", buffer); 122 else 123 pass(); 124 } 125 126 static void 127 fprintf__simple_string(void) 128 { 129 FILE *file; 130 char buffer[128]; 131 size_t length; 132 const char *contents = "This is a message\n"; 133 134 file = fopen("test.txt", "w+"); 135 if (fprintf(file, "%s", contents) <= 0) { 136 fail("fprintf failed to write to file"); 137 skip(2); 138 return; 139 } 140 pass(); 141 rewind(file); 142 length = fread(buffer, 1, sizeof(buffer) - 1, file); 143 if (length != strlen(contents)) { 144 fail("fread failed"); 145 skip(1); 146 return; 147 } 148 pass(); 149 buffer[length] = '\0'; 150 fclose(file); 151 152 if (strcmp(buffer, contents) != 0) 153 fail("Written and read data differ"); 154 else 155 pass(); 156 157 /* Of special note here is that we are NOT deleting the temporary 158 * files we created in this test. Kyua takes care of this cleanup 159 * automatically and tests can (and should) rely on this behavior. */ 160 } 161 162 int 163 main(void) 164 { 165 /* If you have read the printf_test.c counterpart in the atf/ 166 * directory, you may think that the sequencing of tests below and 167 * the exposed behavior to the user is very similar. But you'd be 168 * wrong. 169 * 170 * There are two major differences with this and the ATF version. 171 * The first is that the code below has no provisions to detect 172 * failures in one test and continue running the other tests: the 173 * first failure causes the whole test program to exit. The second 174 * is that this particular main() has no arguments: without ATF, 175 * all test programs may expose a different command-line interface, 176 * and this is an issue for consistency purposes. */ 177 printf("1..%d\n", TEST_COUNT); 178 179 snprintf__two_formatters(); 180 snprintf__overflow(); 181 fprintf__simple_string(); 182 183 return (failed); 184 } 185