Revision e3f9fe2d
b/include/qemu-common.h | ||
---|---|---|
173 | 173 |
int fcntl_setfl(int fd, int flag); |
174 | 174 |
int qemu_parse_fd(const char *param); |
175 | 175 |
|
176 |
int parse_uint(const char *s, unsigned long long *value, char **endptr, |
|
177 |
int base); |
|
178 |
int parse_uint_full(const char *s, unsigned long long *value, int base); |
|
179 |
|
|
176 | 180 |
/* |
177 | 181 |
* strtosz() suffixes used to specify the default treatment of an |
178 | 182 |
* argument passed to strtosz() without an explicit suffix. |
b/tests/Makefile | ||
---|---|---|
52 | 52 |
gcov-files-test-x86-cpuid-y = |
53 | 53 |
check-unit-y += tests/test-xbzrle$(EXESUF) |
54 | 54 |
gcov-files-test-xbzrle-y = xbzrle.c |
55 |
check-unit-y += tests/test-cutils$(EXESUF) |
|
56 |
gcov-files-test-cutils-y += util/cutils.c |
|
55 | 57 |
|
56 | 58 |
check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh |
57 | 59 |
|
... | ... | |
101 | 103 |
tests/test-hbitmap$(EXESUF): tests/test-hbitmap.o libqemuutil.a libqemustub.a |
102 | 104 |
tests/test-x86-cpuid$(EXESUF): tests/test-x86-cpuid.o |
103 | 105 |
tests/test-xbzrle$(EXESUF): tests/test-xbzrle.o xbzrle.o page_cache.o libqemuutil.a |
106 |
tests/test-cutils$(EXESUF): tests/test-cutils.o util/cutils.o |
|
104 | 107 |
|
105 | 108 |
tests/test-qapi-types.c tests/test-qapi-types.h :\ |
106 | 109 |
$(SRC_PATH)/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-types.py |
b/tests/test-cutils.c | ||
---|---|---|
1 |
/* |
|
2 |
* cutils.c unit-tests |
|
3 |
* |
|
4 |
* Copyright (C) 2013 Red Hat Inc. |
|
5 |
* |
|
6 |
* Authors: |
|
7 |
* Eduardo Habkost <ehabkost@redhat.com> |
|
8 |
* |
|
9 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
10 |
* of this software and associated documentation files (the "Software"), to deal |
|
11 |
* in the Software without restriction, including without limitation the rights |
|
12 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
13 |
* copies of the Software, and to permit persons to whom the Software is |
|
14 |
* furnished to do so, subject to the following conditions: |
|
15 |
* |
|
16 |
* The above copyright notice and this permission notice shall be included in |
|
17 |
* all copies or substantial portions of the Software. |
|
18 |
* |
|
19 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
20 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
21 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
|
22 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
23 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
24 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
25 |
* THE SOFTWARE. |
|
26 |
*/ |
|
27 |
|
|
28 |
#include <glib.h> |
|
29 |
#include <errno.h> |
|
30 |
#include <string.h> |
|
31 |
|
|
32 |
#include "qemu-common.h" |
|
33 |
|
|
34 |
|
|
35 |
static void test_parse_uint_null(void) |
|
36 |
{ |
|
37 |
unsigned long long i = 999; |
|
38 |
char f = 'X'; |
|
39 |
char *endptr = &f; |
|
40 |
int r; |
|
41 |
|
|
42 |
r = parse_uint(NULL, &i, &endptr, 0); |
|
43 |
|
|
44 |
g_assert_cmpint(r, ==, -EINVAL); |
|
45 |
g_assert_cmpint(i, ==, 0); |
|
46 |
g_assert(endptr == NULL); |
|
47 |
} |
|
48 |
|
|
49 |
static void test_parse_uint_empty(void) |
|
50 |
{ |
|
51 |
unsigned long long i = 999; |
|
52 |
char f = 'X'; |
|
53 |
char *endptr = &f; |
|
54 |
const char *str = ""; |
|
55 |
int r; |
|
56 |
|
|
57 |
r = parse_uint(str, &i, &endptr, 0); |
|
58 |
|
|
59 |
g_assert_cmpint(r, ==, -EINVAL); |
|
60 |
g_assert_cmpint(i, ==, 0); |
|
61 |
g_assert(endptr == str); |
|
62 |
} |
|
63 |
|
|
64 |
static void test_parse_uint_whitespace(void) |
|
65 |
{ |
|
66 |
unsigned long long i = 999; |
|
67 |
char f = 'X'; |
|
68 |
char *endptr = &f; |
|
69 |
const char *str = " \t "; |
|
70 |
int r; |
|
71 |
|
|
72 |
r = parse_uint(str, &i, &endptr, 0); |
|
73 |
|
|
74 |
g_assert_cmpint(r, ==, -EINVAL); |
|
75 |
g_assert_cmpint(i, ==, 0); |
|
76 |
g_assert(endptr == str); |
|
77 |
} |
|
78 |
|
|
79 |
|
|
80 |
static void test_parse_uint_invalid(void) |
|
81 |
{ |
|
82 |
unsigned long long i = 999; |
|
83 |
char f = 'X'; |
|
84 |
char *endptr = &f; |
|
85 |
const char *str = " \t xxx"; |
|
86 |
int r; |
|
87 |
|
|
88 |
r = parse_uint(str, &i, &endptr, 0); |
|
89 |
|
|
90 |
g_assert_cmpint(r, ==, -EINVAL); |
|
91 |
g_assert_cmpint(i, ==, 0); |
|
92 |
g_assert(endptr == str); |
|
93 |
} |
|
94 |
|
|
95 |
|
|
96 |
static void test_parse_uint_trailing(void) |
|
97 |
{ |
|
98 |
unsigned long long i = 999; |
|
99 |
char f = 'X'; |
|
100 |
char *endptr = &f; |
|
101 |
const char *str = "123xxx"; |
|
102 |
int r; |
|
103 |
|
|
104 |
r = parse_uint(str, &i, &endptr, 0); |
|
105 |
|
|
106 |
g_assert_cmpint(r, ==, 0); |
|
107 |
g_assert_cmpint(i, ==, 123); |
|
108 |
g_assert(endptr == str + 3); |
|
109 |
} |
|
110 |
|
|
111 |
static void test_parse_uint_correct(void) |
|
112 |
{ |
|
113 |
unsigned long long i = 999; |
|
114 |
char f = 'X'; |
|
115 |
char *endptr = &f; |
|
116 |
const char *str = "123"; |
|
117 |
int r; |
|
118 |
|
|
119 |
r = parse_uint(str, &i, &endptr, 0); |
|
120 |
|
|
121 |
g_assert_cmpint(r, ==, 0); |
|
122 |
g_assert_cmpint(i, ==, 123); |
|
123 |
g_assert(endptr == str + strlen(str)); |
|
124 |
} |
|
125 |
|
|
126 |
static void test_parse_uint_octal(void) |
|
127 |
{ |
|
128 |
unsigned long long i = 999; |
|
129 |
char f = 'X'; |
|
130 |
char *endptr = &f; |
|
131 |
const char *str = "0123"; |
|
132 |
int r; |
|
133 |
|
|
134 |
r = parse_uint(str, &i, &endptr, 0); |
|
135 |
|
|
136 |
g_assert_cmpint(r, ==, 0); |
|
137 |
g_assert_cmpint(i, ==, 0123); |
|
138 |
g_assert(endptr == str + strlen(str)); |
|
139 |
} |
|
140 |
|
|
141 |
static void test_parse_uint_decimal(void) |
|
142 |
{ |
|
143 |
unsigned long long i = 999; |
|
144 |
char f = 'X'; |
|
145 |
char *endptr = &f; |
|
146 |
const char *str = "0123"; |
|
147 |
int r; |
|
148 |
|
|
149 |
r = parse_uint(str, &i, &endptr, 10); |
|
150 |
|
|
151 |
g_assert_cmpint(r, ==, 0); |
|
152 |
g_assert_cmpint(i, ==, 123); |
|
153 |
g_assert(endptr == str + strlen(str)); |
|
154 |
} |
|
155 |
|
|
156 |
|
|
157 |
static void test_parse_uint_llong_max(void) |
|
158 |
{ |
|
159 |
unsigned long long i = 999; |
|
160 |
char f = 'X'; |
|
161 |
char *endptr = &f; |
|
162 |
char *str = g_strdup_printf("%llu", (unsigned long long)LLONG_MAX + 1); |
|
163 |
int r; |
|
164 |
|
|
165 |
r = parse_uint(str, &i, &endptr, 0); |
|
166 |
|
|
167 |
g_assert_cmpint(r, ==, 0); |
|
168 |
g_assert_cmpint(i, ==, (unsigned long long)LLONG_MAX + 1); |
|
169 |
g_assert(endptr == str + strlen(str)); |
|
170 |
|
|
171 |
g_free(str); |
|
172 |
} |
|
173 |
|
|
174 |
static void test_parse_uint_overflow(void) |
|
175 |
{ |
|
176 |
unsigned long long i = 999; |
|
177 |
char f = 'X'; |
|
178 |
char *endptr = &f; |
|
179 |
const char *str = "99999999999999999999999999999999999999"; |
|
180 |
int r; |
|
181 |
|
|
182 |
r = parse_uint(str, &i, &endptr, 0); |
|
183 |
|
|
184 |
g_assert_cmpint(r, ==, -ERANGE); |
|
185 |
g_assert_cmpint(i, ==, ULLONG_MAX); |
|
186 |
g_assert(endptr == str + strlen(str)); |
|
187 |
} |
|
188 |
|
|
189 |
static void test_parse_uint_negative(void) |
|
190 |
{ |
|
191 |
unsigned long long i = 999; |
|
192 |
char f = 'X'; |
|
193 |
char *endptr = &f; |
|
194 |
const char *str = " \t -321"; |
|
195 |
int r; |
|
196 |
|
|
197 |
r = parse_uint(str, &i, &endptr, 0); |
|
198 |
|
|
199 |
g_assert_cmpint(r, ==, -ERANGE); |
|
200 |
g_assert_cmpint(i, ==, 0); |
|
201 |
g_assert(endptr == str + strlen(str)); |
|
202 |
} |
|
203 |
|
|
204 |
|
|
205 |
static void test_parse_uint_full_trailing(void) |
|
206 |
{ |
|
207 |
unsigned long long i = 999; |
|
208 |
const char *str = "123xxx"; |
|
209 |
int r; |
|
210 |
|
|
211 |
r = parse_uint_full(str, &i, 0); |
|
212 |
|
|
213 |
g_assert_cmpint(r, ==, -EINVAL); |
|
214 |
g_assert_cmpint(i, ==, 0); |
|
215 |
} |
|
216 |
|
|
217 |
static void test_parse_uint_full_correct(void) |
|
218 |
{ |
|
219 |
unsigned long long i = 999; |
|
220 |
const char *str = "123"; |
|
221 |
int r; |
|
222 |
|
|
223 |
r = parse_uint_full(str, &i, 0); |
|
224 |
|
|
225 |
g_assert_cmpint(r, ==, 0); |
|
226 |
g_assert_cmpint(i, ==, 123); |
|
227 |
} |
|
228 |
|
|
229 |
int main(int argc, char **argv) |
|
230 |
{ |
|
231 |
g_test_init(&argc, &argv, NULL); |
|
232 |
|
|
233 |
g_test_add_func("/cutils/parse_uint/null", test_parse_uint_null); |
|
234 |
g_test_add_func("/cutils/parse_uint/empty", test_parse_uint_empty); |
|
235 |
g_test_add_func("/cutils/parse_uint/whitespace", |
|
236 |
test_parse_uint_whitespace); |
|
237 |
g_test_add_func("/cutils/parse_uint/invalid", test_parse_uint_invalid); |
|
238 |
g_test_add_func("/cutils/parse_uint/trailing", test_parse_uint_trailing); |
|
239 |
g_test_add_func("/cutils/parse_uint/correct", test_parse_uint_correct); |
|
240 |
g_test_add_func("/cutils/parse_uint/octal", test_parse_uint_octal); |
|
241 |
g_test_add_func("/cutils/parse_uint/decimal", test_parse_uint_decimal); |
|
242 |
g_test_add_func("/cutils/parse_uint/llong_max", test_parse_uint_llong_max); |
|
243 |
g_test_add_func("/cutils/parse_uint/overflow", test_parse_uint_overflow); |
|
244 |
g_test_add_func("/cutils/parse_uint/negative", test_parse_uint_negative); |
|
245 |
g_test_add_func("/cutils/parse_uint_full/trailing", |
|
246 |
test_parse_uint_full_trailing); |
|
247 |
g_test_add_func("/cutils/parse_uint_full/correct", |
|
248 |
test_parse_uint_full_correct); |
|
249 |
|
|
250 |
return g_test_run(); |
|
251 |
} |
b/util/cutils.c | ||
---|---|---|
270 | 270 |
return strtosz_suffix(nptr, end, STRTOSZ_DEFSUFFIX_MB); |
271 | 271 |
} |
272 | 272 |
|
273 |
/** |
|
274 |
* parse_uint: |
|
275 |
* |
|
276 |
* @s: String to parse |
|
277 |
* @value: Destination for parsed integer value |
|
278 |
* @endptr: Destination for pointer to first character not consumed |
|
279 |
* @base: integer base, between 2 and 36 inclusive, or 0 |
|
280 |
* |
|
281 |
* Parse unsigned integer |
|
282 |
* |
|
283 |
* Parsed syntax is like strtoull()'s: arbitrary whitespace, a single optional |
|
284 |
* '+' or '-', an optional "0x" if @base is 0 or 16, one or more digits. |
|
285 |
* |
|
286 |
* If @s is null, or @base is invalid, or @s doesn't start with an |
|
287 |
* integer in the syntax above, set *@value to 0, *@endptr to @s, and |
|
288 |
* return -EINVAL. |
|
289 |
* |
|
290 |
* Set *@endptr to point right beyond the parsed integer (even if the integer |
|
291 |
* overflows or is negative, all digits will be parsed and *@endptr will |
|
292 |
* point right beyond them). |
|
293 |
* |
|
294 |
* If the integer is negative, set *@value to 0, and return -ERANGE. |
|
295 |
* |
|
296 |
* If the integer overflows unsigned long long, set *@value to |
|
297 |
* ULLONG_MAX, and return -ERANGE. |
|
298 |
* |
|
299 |
* Else, set *@value to the parsed integer, and return 0. |
|
300 |
*/ |
|
301 |
int parse_uint(const char *s, unsigned long long *value, char **endptr, |
|
302 |
int base) |
|
303 |
{ |
|
304 |
int r = 0; |
|
305 |
char *endp = (char *)s; |
|
306 |
unsigned long long val = 0; |
|
307 |
|
|
308 |
if (!s) { |
|
309 |
r = -EINVAL; |
|
310 |
goto out; |
|
311 |
} |
|
312 |
|
|
313 |
errno = 0; |
|
314 |
val = strtoull(s, &endp, base); |
|
315 |
if (errno) { |
|
316 |
r = -errno; |
|
317 |
goto out; |
|
318 |
} |
|
319 |
|
|
320 |
if (endp == s) { |
|
321 |
r = -EINVAL; |
|
322 |
goto out; |
|
323 |
} |
|
324 |
|
|
325 |
/* make sure we reject negative numbers: */ |
|
326 |
while (isspace((unsigned char)*s)) { |
|
327 |
s++; |
|
328 |
} |
|
329 |
if (*s == '-') { |
|
330 |
val = 0; |
|
331 |
r = -ERANGE; |
|
332 |
goto out; |
|
333 |
} |
|
334 |
|
|
335 |
out: |
|
336 |
*value = val; |
|
337 |
*endptr = endp; |
|
338 |
return r; |
|
339 |
} |
|
340 |
|
|
341 |
/** |
|
342 |
* parse_uint_full: |
|
343 |
* |
|
344 |
* @s: String to parse |
|
345 |
* @value: Destination for parsed integer value |
|
346 |
* @base: integer base, between 2 and 36 inclusive, or 0 |
|
347 |
* |
|
348 |
* Parse unsigned integer from entire string |
|
349 |
* |
|
350 |
* Have the same behavior of parse_uint(), but with an additional check |
|
351 |
* for additional data after the parsed number. If extra characters are present |
|
352 |
* after the parsed number, the function will return -EINVAL, and *@v will |
|
353 |
* be set to 0. |
|
354 |
*/ |
|
355 |
int parse_uint_full(const char *s, unsigned long long *value, int base) |
|
356 |
{ |
|
357 |
char *endp; |
|
358 |
int r; |
|
359 |
|
|
360 |
r = parse_uint(s, value, &endp, base); |
|
361 |
if (r < 0) { |
|
362 |
return r; |
|
363 |
} |
|
364 |
if (*endp) { |
|
365 |
*value = 0; |
|
366 |
return -EINVAL; |
|
367 |
} |
|
368 |
|
|
369 |
return 0; |
|
370 |
} |
|
371 |
|
|
273 | 372 |
int qemu_parse_fd(const char *param) |
274 | 373 |
{ |
275 | 374 |
int fd; |
Also available in: Unified diff