1/*	$NetBSD: t_strtol.c,v 1.7 2017/07/06 21:08:44 joerg Exp $ */
2
3/*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__RCSID("$NetBSD: t_strtol.c,v 1.7 2017/07/06 21:08:44 joerg Exp $");
34
35#include <atf-c.h>
36#include <errno.h>
37#include <stdlib.h>
38#include <string.h>
39#include <limits.h>
40
41struct test {
42	const char	*str;
43	int64_t		 res;
44	int		 base;
45	const char	*end;
46};
47
48static void	check(struct test *, long int, long long int, char *);
49
50static void
51check(struct test *t, long int li, long long int lli, char *end)
52{
53
54	if (li != -1 && li != t->res)
55		atf_tc_fail_nonfatal("strtol(%s, &end, %d) failed "
56		    "(rv = %ld)", t->str, t->base, li);
57
58	if (lli != -1 && lli != t->res)
59		atf_tc_fail_nonfatal("strtoll(%s, NULL, %d) failed "
60		    "(rv = %lld)", t->str, t->base, lli);
61
62	if ((t->end != NULL && strcmp(t->end, end) != 0) ||
63	    (t->end == NULL && *end != '\0'))
64		atf_tc_fail_nonfatal("invalid end pointer (%p) from "
65		    "strtol(%p, &end, %d)", end, t->str, t->base);
66}
67
68ATF_TC(strtol_base);
69ATF_TC_HEAD(strtol_base, tc)
70{
71	atf_tc_set_md_var(tc, "descr", "Test strtol(3) with different bases");
72}
73
74ATF_TC_BODY(strtol_base, tc)
75{
76	struct test t[] = {
77		{ "123456789",			 123456789,  0, NULL	},
78		{ "111010110111100110100010101", 123456789,  2, NULL	},
79		{ "22121022020212200",		 123456789,  3, NULL	},
80		{ "13112330310111",		 123456789,  4, NULL	},
81		{ "223101104124",		 123456789,  5, NULL	},
82		{ "20130035113",		 123456789,  6, NULL	},
83		{ "3026236221",			 123456789,  7, NULL	},
84		{ "726746425",			 123456789,  8, NULL	},
85		{ "277266780",			 123456789,  9, NULL	},
86		{ "123456789",			 123456789, 10, NULL	},
87		{ "63762A05",			 123456789, 11, NULL	},
88		{ "35418A99",			 123456789, 12, NULL	},
89		{ "1C767471",			 123456789, 13, NULL	},
90		{ "12579781",			 123456789, 14, NULL	},
91		{ "AC89BC9",			 123456789, 15, NULL	},
92		{ "75BCD15",			 123456789, 16, NULL	},
93		{ "1234567",			    342391,  8, NULL	},
94		{ "01234567",			    342391,  0, NULL	},
95		{ "0123456789",			 123456789, 10, NULL	},
96		{ "0x75bcd15",			 123456789,  0, NULL	},
97		{ " 0xX",			         0,  0, "xX"	},
98		{ " 0xX",			         0, 16, "xX"	},
99		{ " 0XX",			         0,  0, "XX"	},
100		{ " 0XX",			         0, 16, "XX"	},
101	};
102
103	long long int lli;
104	long int li;
105	long long int ulli;
106	long int uli;
107	char *end, *end2;
108	size_t i;
109
110	for (i = 0; i < __arraycount(t); i++) {
111
112		li = strtol(t[i].str, &end, t[i].base);
113		lli = strtoll(t[i].str, NULL, t[i].base);
114
115		uli = strtoul(t[i].str, &end2, t[i].base);
116		ulli = strtoull(t[i].str, NULL, t[i].base);
117
118		check(&t[i], li, lli, end);
119
120		if (li != uli)
121			atf_tc_fail_nonfatal("strtoul(%s, NULL, %d) failed "
122			    "(rv = %lu)", t[i].str, t[i].base, uli);
123		if (end != end2)
124			atf_tc_fail_nonfatal("invalid end pointer ('%p') from "
125			    "strtoul(%s, &end, %d)", end2, t[i].str, t[i].base);
126		if (lli != ulli)
127			atf_tc_fail_nonfatal("strtoull(%s, NULL, %d) failed "
128			    "(rv = %llu)", t[i].str, t[i].base, ulli);
129	}
130}
131
132ATF_TC(strtol_case);
133ATF_TC_HEAD(strtol_case, tc)
134{
135	atf_tc_set_md_var(tc, "descr", "Case insensitivity with strtol(3)");
136}
137
138ATF_TC_BODY(strtol_case, tc)
139{
140	struct test t[] = {
141		{ "abcd",	0xabcd, 16, NULL	},
142		{ "     dcba",	0xdcba, 16, NULL	},
143		{ "abcd dcba",	0xabcd, 16, " dcba"	},
144		{ "abc0x123",	0xabc0, 16, "x123"	},
145		{ "abcd\0x123",	0xabcd, 16, "\0x123"	},
146		{ "ABCD",	0xabcd, 16, NULL	},
147		{ "aBcD",	0xabcd, 16, NULL	},
148		{ "0xABCD",	0xabcd, 16, NULL	},
149		{ "0xABCDX",	0xabcd, 16, "X"		},
150	};
151
152	long long int lli;
153	long int li;
154	char *end;
155	size_t i;
156
157	for (i = 0; i < __arraycount(t); i++) {
158
159		li = strtol(t[i].str, &end, t[i].base);
160		lli = strtoll(t[i].str, NULL, t[i].base);
161
162		check(&t[i], li, lli, end);
163	}
164}
165
166ATF_TC(strtol_range);
167ATF_TC_HEAD(strtol_range, tc)
168{
169	atf_tc_set_md_var(tc, "descr", "Test ERANGE from strtol(3)");
170}
171
172ATF_TC_BODY(strtol_range, tc)
173{
174
175#if LONG_MAX == 0x7fffffff	/* XXX: Is this portable? */
176
177	struct test t[] = {
178		{ "20000000000", 2147483647, 8, NULL },
179		{ "2147483648",  2147483647, 10, NULL },
180		{ "80000000",	 2147483647, 16, NULL },
181	};
182#else
183	struct test t[] = {
184		{ "1000000000000000000000", 9223372036854775807, 8, NULL },
185		{ "9223372036854775808",    9223372036854775807, 10, NULL },
186		{ "8000000000000000",       9223372036854775807, 16, NULL },
187	};
188#endif
189
190	long int li;
191	char *end;
192	size_t i;
193
194	for (i = 0; i < __arraycount(t); i++) {
195
196		errno = 0;
197		li = strtol(t[i].str, &end, t[i].base);
198
199		if (errno != ERANGE)
200			atf_tc_fail("strtol(3) did not catch ERANGE");
201
202		check(&t[i], li, -1, end);
203	}
204}
205
206ATF_TC(strtol_signed);
207ATF_TC_HEAD(strtol_signed, tc)
208{
209	atf_tc_set_md_var(tc, "descr", "A basic test of strtol(3)");
210}
211
212ATF_TC_BODY(strtol_signed, tc)
213{
214	struct test t[] = {
215		{ "1",		 1, 0, NULL	},
216		{ " 2",		 2, 0, NULL	},
217		{ "  3",	 3, 0, NULL	},
218		{ " -3",	-3, 0, NULL	},
219		{ "--1",	 0, 0, "--1"	},
220		{ "+-2",	 0, 0, "+-2"	},
221		{ "++3",	 0, 0, "++3"	},
222		{ "+9",		 9, 0, NULL	},
223		{ "+123",      123, 0, NULL	},
224		{ "-1 3",       -1, 0, " 3"	},
225		{ "-1.3",       -1, 0, ".3"	},
226		{ "-  3",        0, 0, "-  3"	},
227		{ "+33.",       33, 0, "."	},
228		{ "30x0",       30, 0, "x0"	},
229	};
230
231	long long int lli;
232	long int li;
233	char *end;
234	size_t i;
235
236	for (i = 0; i < __arraycount(t); i++) {
237
238		li = strtol(t[i].str, &end, t[i].base);
239		lli = strtoll(t[i].str, NULL, t[i].base);
240
241		check(&t[i], li, lli, end);
242	}
243}
244
245ATF_TP_ADD_TCS(tp)
246{
247
248	ATF_TP_ADD_TC(tp, strtol_base);
249	ATF_TP_ADD_TC(tp, strtol_case);
250	ATF_TP_ADD_TC(tp, strtol_range);
251	ATF_TP_ADD_TC(tp, strtol_signed);
252
253	return atf_no_error();
254}
255