1258299Sjmmv/* $FreeBSD$
2258299Sjmmv *
3258299Sjmmv * Copyright 2013 Google Inc.
4258299Sjmmv * All rights reserved.
5258299Sjmmv *
6258299Sjmmv * Redistribution and use in source and binary forms, with or without
7258299Sjmmv * modification, are permitted provided that the following conditions are
8258299Sjmmv * met:
9258299Sjmmv *
10258299Sjmmv * * Redistributions of source code must retain the above copyright
11258299Sjmmv *   notice, this list of conditions and the following disclaimer.
12258299Sjmmv * * Redistributions in binary form must reproduce the above copyright
13258299Sjmmv *   notice, this list of conditions and the following disclaimer in the
14258299Sjmmv *   documentation and/or other materials provided with the distribution.
15258299Sjmmv * * Neither the name of Google Inc. nor the names of its contributors
16258299Sjmmv *   may be used to endorse or promote products derived from this software
17258299Sjmmv *   without specific prior written permission.
18258299Sjmmv *
19258299Sjmmv * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20258299Sjmmv * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21258299Sjmmv * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22258299Sjmmv * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23258299Sjmmv * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24258299Sjmmv * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25258299Sjmmv * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26258299Sjmmv * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27258299Sjmmv * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28258299Sjmmv * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29258299Sjmmv * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
30258299Sjmmv
31258299Sjmmv/*
32258299Sjmmv * INTRODUCTION
33258299Sjmmv *
34258299Sjmmv * This plain test program mimics the structure and contents of its
35258299Sjmmv * ATF-based counterpart.  It attempts to represent various test cases
36258299Sjmmv * in different separate functions and just calls them all from main().
37258299Sjmmv *
38258299Sjmmv * In reality, plain test programs can be much simpler.  All they have
39258299Sjmmv * to do is return 0 on success and non-0 otherwise.
40258299Sjmmv */
41258299Sjmmv
42258299Sjmmv#include <err.h>
43258299Sjmmv#include <stdio.h>
44258299Sjmmv#include <stdlib.h>
45258299Sjmmv#include <string.h>
46258299Sjmmv
47258299Sjmmvstatic void
48258299Sjmmvsnprintf__two_formatters(void)
49258299Sjmmv{
50258299Sjmmv	char buffer[128];
51258299Sjmmv
52258299Sjmmv	if (snprintf(buffer, sizeof(buffer), "%s, %s!", "Hello",
53258299Sjmmv	    "tests") <= 0)
54258299Sjmmv		errx(EXIT_FAILURE, "snprintf with two formatters failed");
55258299Sjmmv
56258299Sjmmv	if (strcmp(buffer, "Hello, tests!") != 0)
57258299Sjmmv		errx(EXIT_FAILURE, "Bad formatting: got %s", buffer);
58258299Sjmmv}
59258299Sjmmv
60258299Sjmmvstatic void
61258299Sjmmvsnprintf__overflow(void)
62258299Sjmmv{
63258299Sjmmv	char buffer[10];
64258299Sjmmv
65258299Sjmmv	if (snprintf(buffer, sizeof(buffer), "0123456789abcdef") != 16)
66258299Sjmmv		errx(EXIT_FAILURE, "snprintf did not return the expected "
67258299Sjmmv		    "number of characters");
68258299Sjmmv
69258299Sjmmv	if (strcmp(buffer, "012345678") != 0)
70258299Sjmmv		errx(EXIT_FAILURE, "Bad formatting: got %s", buffer);
71258299Sjmmv}
72258299Sjmmv
73258299Sjmmvstatic void
74258299Sjmmvfprintf__simple_string(void)
75258299Sjmmv{
76258299Sjmmv	FILE *file;
77258299Sjmmv	char buffer[128];
78258299Sjmmv	size_t length;
79258299Sjmmv	const char *contents = "This is a message\n";
80258299Sjmmv
81258299Sjmmv	file = fopen("test.txt", "w+");
82258299Sjmmv	if (fprintf(file, "%s", contents) <= 0)
83258299Sjmmv		err(EXIT_FAILURE, "fprintf failed to write to file");
84258299Sjmmv	rewind(file);
85258299Sjmmv	length = fread(buffer, 1, sizeof(buffer) - 1, file);
86258299Sjmmv	if (length != strlen(contents))
87258299Sjmmv		err(EXIT_FAILURE, "fread failed");
88258299Sjmmv	buffer[length] = '\0';
89258299Sjmmv	fclose(file);
90258299Sjmmv
91258299Sjmmv	if (strcmp(buffer, contents) != 0)
92258299Sjmmv		errx(EXIT_FAILURE, "Written and read data differ");
93258299Sjmmv
94258299Sjmmv	/* Of special note here is that we are NOT deleting the temporary
95258299Sjmmv	 * files we created in this test.  Kyua takes care of this cleanup
96258299Sjmmv	 * automatically and tests can (and should) rely on this behavior. */
97258299Sjmmv}
98258299Sjmmv
99258299Sjmmvint
100258299Sjmmvmain(void)
101258299Sjmmv{
102258299Sjmmv	/* If you have read the printf_test.c counterpart in the atf/
103258299Sjmmv	 * directory, you may think that the sequencing of tests below and
104258299Sjmmv	 * the exposed behavior to the user is very similar.  But you'd be
105258299Sjmmv	 * wrong.
106258299Sjmmv	 *
107258299Sjmmv	 * There are two major differences with this and the ATF version.
108258299Sjmmv	 * The first is that the code below has no provisions to detect
109258299Sjmmv	 * failures in one test and continue running the other tests: the
110258299Sjmmv	 * first failure causes the whole test program to exit.  The second
111258299Sjmmv	 * is that this particular main() has no arguments: without ATF,
112258299Sjmmv	 * all test programs may expose a different command-line interface,
113258299Sjmmv	 * and this is an issue for consistency purposes. */
114258299Sjmmv	snprintf__two_formatters();
115258299Sjmmv	snprintf__overflow();
116258299Sjmmv	fprintf__simple_string();
117258299Sjmmv
118258299Sjmmv	return EXIT_SUCCESS;
119258299Sjmmv}
120