t_printf.c revision 330520
1/* $NetBSD: t_printf.c,v 1.8 2012/04/11 16:21:42 jruoho Exp $ */
2
3/*-
4 * Copyright (c) 2010 The NetBSD Foundation, 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
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/types.h>
30#include <sys/resource.h>
31#include <atf-c.h>
32#include <math.h>
33#include <stdio.h>
34#include <stdint.h>
35#include <string.h>
36#include <time.h>
37#include <stdlib.h>
38
39#ifndef __NetBSD__
40#include <signal.h>
41#endif
42
43ATF_TC(snprintf_c99);
44ATF_TC_HEAD(snprintf_c99, tc)
45{
46
47	atf_tc_set_md_var(tc, "descr",
48	    "Test printf(3) C99 conformance (PR lib/22019)");
49}
50
51ATF_TC_BODY(snprintf_c99, tc)
52{
53	char s[4];
54
55	(void)memset(s, '\0', sizeof(s));
56	(void)snprintf(s, sizeof(s), "%#.o", 0);
57	(void)printf("printf = %#.o\n", 0);
58	(void)fprintf(stderr, "snprintf = %s", s);
59
60	ATF_REQUIRE(strlen(s) == 1);
61	ATF_REQUIRE(s[0] == '0');
62}
63
64ATF_TC(snprintf_dotzero);
65ATF_TC_HEAD(snprintf_dotzero, tc)
66{
67
68	atf_tc_set_md_var(tc, "descr",
69	    "PR lib/32951: %%.0f formats (0.0,0.5] to \"0.\"");
70}
71
72ATF_TC_BODY(snprintf_dotzero, tc)
73{
74	char s[4];
75
76	ATF_CHECK(snprintf(s, sizeof(s), "%.0f", 0.1) == 1);
77	ATF_REQUIRE_STREQ(s, "0");
78}
79
80ATF_TC(snprintf_posarg);
81ATF_TC_HEAD(snprintf_posarg, tc)
82{
83
84	atf_tc_set_md_var(tc, "descr", "test for positional arguments");
85}
86
87ATF_TC_BODY(snprintf_posarg, tc)
88{
89	char s[16];
90
91	ATF_CHECK(snprintf(s, sizeof(s), "%1$d", -23) == 3);
92	ATF_REQUIRE_STREQ(s, "-23");
93}
94
95ATF_TC(snprintf_posarg_width);
96ATF_TC_HEAD(snprintf_posarg_width, tc)
97{
98
99	atf_tc_set_md_var(tc, "descr", "test for positional arguments with "
100	    "field width");
101}
102
103ATF_TC_BODY(snprintf_posarg_width, tc)
104{
105	char s[16];
106
107	ATF_CHECK(snprintf(s, sizeof(s), "%1$*2$d", -23, 4) == 4);
108	ATF_REQUIRE_STREQ(s, " -23");
109}
110
111ATF_TC(snprintf_posarg_error);
112ATF_TC_HEAD(snprintf_posarg_error, tc)
113{
114
115	atf_tc_set_md_var(tc, "descr", "test for positional arguments out "
116	    "of bounds");
117}
118
119ATF_TC_BODY(snprintf_posarg_error, tc)
120{
121	char s[16], fmt[32];
122
123#ifndef __NetBSD__
124	atf_tc_expect_signal(SIGSEGV,
125	    "some non-NetBSD platforms including FreeBSD don't validate "
126	    "negative size; testcase blows up with SIGSEGV");
127#endif
128
129	snprintf(fmt, sizeof(fmt), "%%%zu$d", SIZE_MAX / sizeof(size_t));
130
131	ATF_CHECK(snprintf(s, sizeof(s), fmt, -23) == -1);
132}
133
134ATF_TC(snprintf_float);
135ATF_TC_HEAD(snprintf_float, tc)
136{
137
138	atf_tc_set_md_var(tc, "descr", "test that floating conversions don't"
139	    " leak memory");
140#ifdef	__FreeBSD__
141	atf_tc_set_md_var(tc, "require.memory", "64m");
142	atf_tc_set_md_var(tc, "require.user", "root");
143#endif
144}
145
146ATF_TC_BODY(snprintf_float, tc)
147{
148	union {
149		double d;
150		uint64_t bits;
151	} u;
152	uint32_t ul, uh;
153	time_t now;
154	char buf[1000];
155	struct rlimit rl;
156
157#ifdef	__FreeBSD__
158	rl.rlim_cur = rl.rlim_max = 32 * 1024 * 1024;
159	ATF_CHECK(setrlimit(RLIMIT_AS, &rl) != -1);
160	rl.rlim_cur = rl.rlim_max = 32 * 1024 * 1024;
161	ATF_CHECK(setrlimit(RLIMIT_DATA, &rl) != -1);
162#else
163	rl.rlim_cur = rl.rlim_max = 1 * 1024 * 1024;
164	ATF_CHECK(setrlimit(RLIMIT_AS, &rl) != -1);
165	rl.rlim_cur = rl.rlim_max = 1 * 1024 * 1024;
166	ATF_CHECK(setrlimit(RLIMIT_DATA, &rl) != -1);
167#endif
168
169	time(&now);
170	srand(now);
171	for (size_t i = 0; i < 10000; i++) {
172		ul = rand();
173		uh = rand();
174		u.bits = (uint64_t)uh << 32 | ul;
175		ATF_CHECK(snprintf(buf, sizeof buf, " %.2f", u.d) != -1);
176	}
177}
178
179ATF_TC(sprintf_zeropad);
180ATF_TC_HEAD(sprintf_zeropad, tc)
181{
182	atf_tc_set_md_var(tc, "descr",
183	    "Test output format zero padding (PR lib/44113)");
184}
185
186ATF_TC_BODY(sprintf_zeropad, tc)
187{
188	char str[1024];
189
190	ATF_CHECK(sprintf(str, "%010f", 0.0) == 10);
191	ATF_REQUIRE_STREQ(str, "000.000000");
192
193	/* ieeefp */
194#ifndef __vax__
195	/* printf(3) should ignore zero padding for nan/inf */
196	ATF_CHECK(sprintf(str, "%010f", NAN) == 10);
197	ATF_REQUIRE_STREQ(str, "       nan");
198	ATF_CHECK(sprintf(str, "%010f", INFINITY) == 10);
199	ATF_REQUIRE_STREQ(str, "       inf");
200#endif
201}
202
203ATF_TP_ADD_TCS(tp)
204{
205
206	ATF_TP_ADD_TC(tp, snprintf_c99);
207	ATF_TP_ADD_TC(tp, snprintf_dotzero);
208	ATF_TP_ADD_TC(tp, snprintf_posarg);
209	ATF_TP_ADD_TC(tp, snprintf_posarg_width);
210	ATF_TP_ADD_TC(tp, snprintf_posarg_error);
211	ATF_TP_ADD_TC(tp, snprintf_float);
212	ATF_TP_ADD_TC(tp, sprintf_zeropad);
213
214	return atf_no_error();
215}
216