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