1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <locale.h>
5 #include <math.h>
6 #include <sys/socket.h>
7
8 #include "alloc-util.h"
9 #include "errno-list.h"
10 #include "log.h"
11 #include "parse-util.h"
12 #include "string-util.h"
13 #include "tests.h"
14
TEST(parse_boolean)15 TEST(parse_boolean) {
16 assert_se(parse_boolean("1") == 1);
17 assert_se(parse_boolean("y") == 1);
18 assert_se(parse_boolean("Y") == 1);
19 assert_se(parse_boolean("yes") == 1);
20 assert_se(parse_boolean("YES") == 1);
21 assert_se(parse_boolean("true") == 1);
22 assert_se(parse_boolean("TRUE") == 1);
23 assert_se(parse_boolean("on") == 1);
24 assert_se(parse_boolean("ON") == 1);
25
26 assert_se(parse_boolean("0") == 0);
27 assert_se(parse_boolean("n") == 0);
28 assert_se(parse_boolean("N") == 0);
29 assert_se(parse_boolean("no") == 0);
30 assert_se(parse_boolean("NO") == 0);
31 assert_se(parse_boolean("false") == 0);
32 assert_se(parse_boolean("FALSE") == 0);
33 assert_se(parse_boolean("off") == 0);
34 assert_se(parse_boolean("OFF") == 0);
35
36 assert_se(parse_boolean("garbage") < 0);
37 assert_se(parse_boolean("") < 0);
38 assert_se(parse_boolean("full") < 0);
39 }
40
TEST(parse_pid)41 TEST(parse_pid) {
42 int r;
43 pid_t pid;
44
45 r = parse_pid("100", &pid);
46 assert_se(r == 0);
47 assert_se(pid == 100);
48
49 r = parse_pid("0x7FFFFFFF", &pid);
50 assert_se(r == 0);
51 assert_se(pid == 2147483647);
52
53 pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */
54 r = parse_pid("0", &pid);
55 assert_se(r == -ERANGE);
56 assert_se(pid == 65);
57
58 pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */
59 r = parse_pid("-100", &pid);
60 assert_se(r == -ERANGE);
61 assert_se(pid == 65);
62
63 pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */
64 r = parse_pid("0xFFFFFFFFFFFFFFFFF", &pid);
65 assert_se(r == -ERANGE);
66 assert_se(pid == 65);
67
68 r = parse_pid("junk", &pid);
69 assert_se(r == -EINVAL);
70
71 r = parse_pid("", &pid);
72 assert_se(r == -EINVAL);
73 }
74
TEST(parse_mode)75 TEST(parse_mode) {
76 mode_t m;
77
78 assert_se(parse_mode("-1", &m) < 0);
79 assert_se(parse_mode("+1", &m) < 0);
80 assert_se(parse_mode("", &m) < 0);
81 assert_se(parse_mode("888", &m) < 0);
82 assert_se(parse_mode("77777", &m) < 0);
83
84 assert_se(parse_mode("544", &m) >= 0 && m == 0544);
85 assert_se(parse_mode("0544", &m) >= 0 && m == 0544);
86 assert_se(parse_mode("00544", &m) >= 0 && m == 0544);
87 assert_se(parse_mode("777", &m) >= 0 && m == 0777);
88 assert_se(parse_mode("0777", &m) >= 0 && m == 0777);
89 assert_se(parse_mode("00777", &m) >= 0 && m == 0777);
90 assert_se(parse_mode("7777", &m) >= 0 && m == 07777);
91 assert_se(parse_mode("07777", &m) >= 0 && m == 07777);
92 assert_se(parse_mode("007777", &m) >= 0 && m == 07777);
93 assert_se(parse_mode("0", &m) >= 0 && m == 0);
94 assert_se(parse_mode(" 1", &m) >= 0 && m == 1);
95 }
96
TEST(parse_size_iec)97 TEST(parse_size_iec) {
98 uint64_t bytes;
99
100 assert_se(parse_size("", 1024, &bytes) == -EINVAL);
101
102 assert_se(parse_size("111", 1024, &bytes) == 0);
103 assert_se(bytes == 111);
104
105 assert_se(parse_size("111.4", 1024, &bytes) == 0);
106 assert_se(bytes == 111);
107
108 assert_se(parse_size(" 112 B", 1024, &bytes) == 0);
109 assert_se(bytes == 112);
110
111 assert_se(parse_size(" 112.6 B", 1024, &bytes) == 0);
112 assert_se(bytes == 112);
113
114 assert_se(parse_size("3.5 K", 1024, &bytes) == 0);
115 assert_se(bytes == 3*1024 + 512);
116
117 assert_se(parse_size("3. K", 1024, &bytes) == 0);
118 assert_se(bytes == 3*1024);
119
120 assert_se(parse_size("3.0 K", 1024, &bytes) == 0);
121 assert_se(bytes == 3*1024);
122
123 assert_se(parse_size("3. 0 K", 1024, &bytes) == -EINVAL);
124
125 assert_se(parse_size(" 4 M 11.5K", 1024, &bytes) == 0);
126 assert_se(bytes == 4*1024*1024 + 11 * 1024 + 512);
127
128 assert_se(parse_size("3B3.5G", 1024, &bytes) == -EINVAL);
129
130 assert_se(parse_size("3.5G3B", 1024, &bytes) == 0);
131 assert_se(bytes == 3ULL*1024*1024*1024 + 512*1024*1024 + 3);
132
133 assert_se(parse_size("3.5G 4B", 1024, &bytes) == 0);
134 assert_se(bytes == 3ULL*1024*1024*1024 + 512*1024*1024 + 4);
135
136 assert_se(parse_size("3B3G4T", 1024, &bytes) == -EINVAL);
137
138 assert_se(parse_size("4T3G3B", 1024, &bytes) == 0);
139 assert_se(bytes == (4ULL*1024 + 3)*1024*1024*1024 + 3);
140
141 assert_se(parse_size(" 4 T 3 G 3 B", 1024, &bytes) == 0);
142 assert_se(bytes == (4ULL*1024 + 3)*1024*1024*1024 + 3);
143
144 assert_se(parse_size("12P", 1024, &bytes) == 0);
145 assert_se(bytes == 12ULL * 1024*1024*1024*1024*1024);
146
147 assert_se(parse_size("12P12P", 1024, &bytes) == -EINVAL);
148
149 assert_se(parse_size("3E 2P", 1024, &bytes) == 0);
150 assert_se(bytes == (3 * 1024 + 2ULL) * 1024*1024*1024*1024*1024);
151
152 assert_se(parse_size("12X", 1024, &bytes) == -EINVAL);
153
154 assert_se(parse_size("12.5X", 1024, &bytes) == -EINVAL);
155
156 assert_se(parse_size("12.5e3", 1024, &bytes) == -EINVAL);
157
158 assert_se(parse_size("1024E", 1024, &bytes) == -ERANGE);
159 assert_se(parse_size("-1", 1024, &bytes) == -ERANGE);
160 assert_se(parse_size("-1024E", 1024, &bytes) == -ERANGE);
161
162 assert_se(parse_size("-1024P", 1024, &bytes) == -ERANGE);
163
164 assert_se(parse_size("-10B 20K", 1024, &bytes) == -ERANGE);
165 }
166
TEST(parse_size_si)167 TEST(parse_size_si) {
168 uint64_t bytes;
169
170 assert_se(parse_size("", 1000, &bytes) == -EINVAL);
171
172 assert_se(parse_size("111", 1000, &bytes) == 0);
173 assert_se(bytes == 111);
174
175 assert_se(parse_size("111.4", 1000, &bytes) == 0);
176 assert_se(bytes == 111);
177
178 assert_se(parse_size(" 112 B", 1000, &bytes) == 0);
179 assert_se(bytes == 112);
180
181 assert_se(parse_size(" 112.6 B", 1000, &bytes) == 0);
182 assert_se(bytes == 112);
183
184 assert_se(parse_size("3.5 K", 1000, &bytes) == 0);
185 assert_se(bytes == 3*1000 + 500);
186
187 assert_se(parse_size("3. K", 1000, &bytes) == 0);
188 assert_se(bytes == 3*1000);
189
190 assert_se(parse_size("3.0 K", 1000, &bytes) == 0);
191 assert_se(bytes == 3*1000);
192
193 assert_se(parse_size("3. 0 K", 1000, &bytes) == -EINVAL);
194
195 assert_se(parse_size(" 4 M 11.5K", 1000, &bytes) == 0);
196 assert_se(bytes == 4*1000*1000 + 11 * 1000 + 500);
197
198 assert_se(parse_size("3B3.5G", 1000, &bytes) == -EINVAL);
199
200 assert_se(parse_size("3.5G3B", 1000, &bytes) == 0);
201 assert_se(bytes == 3ULL*1000*1000*1000 + 500*1000*1000 + 3);
202
203 assert_se(parse_size("3.5G 4B", 1000, &bytes) == 0);
204 assert_se(bytes == 3ULL*1000*1000*1000 + 500*1000*1000 + 4);
205
206 assert_se(parse_size("3B3G4T", 1000, &bytes) == -EINVAL);
207
208 assert_se(parse_size("4T3G3B", 1000, &bytes) == 0);
209 assert_se(bytes == (4ULL*1000 + 3)*1000*1000*1000 + 3);
210
211 assert_se(parse_size(" 4 T 3 G 3 B", 1000, &bytes) == 0);
212 assert_se(bytes == (4ULL*1000 + 3)*1000*1000*1000 + 3);
213
214 assert_se(parse_size("12P", 1000, &bytes) == 0);
215 assert_se(bytes == 12ULL * 1000*1000*1000*1000*1000);
216
217 assert_se(parse_size("12P12P", 1000, &bytes) == -EINVAL);
218
219 assert_se(parse_size("3E 2P", 1000, &bytes) == 0);
220 assert_se(bytes == (3 * 1000 + 2ULL) * 1000*1000*1000*1000*1000);
221
222 assert_se(parse_size("12X", 1000, &bytes) == -EINVAL);
223
224 assert_se(parse_size("12.5X", 1000, &bytes) == -EINVAL);
225
226 assert_se(parse_size("12.5e3", 1000, &bytes) == -EINVAL);
227
228 assert_se(parse_size("1000E", 1000, &bytes) == -ERANGE);
229 assert_se(parse_size("-1", 1000, &bytes) == -ERANGE);
230 assert_se(parse_size("-1000E", 1000, &bytes) == -ERANGE);
231
232 assert_se(parse_size("-1000P", 1000, &bytes) == -ERANGE);
233
234 assert_se(parse_size("-10B 20K", 1000, &bytes) == -ERANGE);
235 }
236
TEST(parse_range)237 TEST(parse_range) {
238 unsigned lower, upper;
239
240 /* Successful cases */
241 assert_se(parse_range("111", &lower, &upper) == 0);
242 assert_se(lower == 111);
243 assert_se(upper == 111);
244
245 assert_se(parse_range("111-123", &lower, &upper) == 0);
246 assert_se(lower == 111);
247 assert_se(upper == 123);
248
249 assert_se(parse_range("123-111", &lower, &upper) == 0);
250 assert_se(lower == 123);
251 assert_se(upper == 111);
252
253 assert_se(parse_range("123-123", &lower, &upper) == 0);
254 assert_se(lower == 123);
255 assert_se(upper == 123);
256
257 assert_se(parse_range("0", &lower, &upper) == 0);
258 assert_se(lower == 0);
259 assert_se(upper == 0);
260
261 assert_se(parse_range("0-15", &lower, &upper) == 0);
262 assert_se(lower == 0);
263 assert_se(upper == 15);
264
265 assert_se(parse_range("15-0", &lower, &upper) == 0);
266 assert_se(lower == 15);
267 assert_se(upper == 0);
268
269 assert_se(parse_range("128-65535", &lower, &upper) == 0);
270 assert_se(lower == 128);
271 assert_se(upper == 65535);
272
273 assert_se(parse_range("1024-4294967295", &lower, &upper) == 0);
274 assert_se(lower == 1024);
275 assert_se(upper == 4294967295);
276
277 /* Leading whitespace is acceptable */
278 assert_se(parse_range(" 111", &lower, &upper) == 0);
279 assert_se(lower == 111);
280 assert_se(upper == 111);
281
282 assert_se(parse_range(" 111-123", &lower, &upper) == 0);
283 assert_se(lower == 111);
284 assert_se(upper == 123);
285
286 assert_se(parse_range("111- 123", &lower, &upper) == 0);
287 assert_se(lower == 111);
288 assert_se(upper == 123);
289
290 assert_se(parse_range("\t111-\t123", &lower, &upper) == 0);
291 assert_se(lower == 111);
292 assert_se(upper == 123);
293
294 assert_se(parse_range(" \t 111- \t 123", &lower, &upper) == 0);
295 assert_se(lower == 111);
296 assert_se(upper == 123);
297
298 /* Error cases, make sure they fail as expected */
299 lower = upper = 9999;
300 assert_se(parse_range("111garbage", &lower, &upper) == -EINVAL);
301 assert_se(lower == 9999);
302 assert_se(upper == 9999);
303
304 assert_se(parse_range("garbage111", &lower, &upper) == -EINVAL);
305 assert_se(lower == 9999);
306 assert_se(upper == 9999);
307
308 assert_se(parse_range("garbage", &lower, &upper) == -EINVAL);
309 assert_se(lower == 9999);
310 assert_se(upper == 9999);
311
312 assert_se(parse_range("111-123garbage", &lower, &upper) == -EINVAL);
313 assert_se(lower == 9999);
314 assert_se(upper == 9999);
315
316 assert_se(parse_range("111garbage-123", &lower, &upper) == -EINVAL);
317 assert_se(lower == 9999);
318 assert_se(upper == 9999);
319
320 /* Empty string */
321 lower = upper = 9999;
322 assert_se(parse_range("", &lower, &upper) == -EINVAL);
323 assert_se(lower == 9999);
324 assert_se(upper == 9999);
325
326 /* 111--123 will pass -123 to safe_atou which returns -ERANGE for negative */
327 assert_se(parse_range("111--123", &lower, &upper) == -ERANGE);
328 assert_se(lower == 9999);
329 assert_se(upper == 9999);
330
331 assert_se(parse_range("-123", &lower, &upper) == -EINVAL);
332 assert_se(lower == 9999);
333 assert_se(upper == 9999);
334
335 assert_se(parse_range("-111-123", &lower, &upper) == -EINVAL);
336 assert_se(lower == 9999);
337 assert_se(upper == 9999);
338
339 assert_se(parse_range("111-123-", &lower, &upper) == -EINVAL);
340 assert_se(lower == 9999);
341 assert_se(upper == 9999);
342
343 assert_se(parse_range("111.4-123", &lower, &upper) == -EINVAL);
344 assert_se(lower == 9999);
345 assert_se(upper == 9999);
346
347 assert_se(parse_range("111-123.4", &lower, &upper) == -EINVAL);
348 assert_se(lower == 9999);
349 assert_se(upper == 9999);
350
351 assert_se(parse_range("111,4-123", &lower, &upper) == -EINVAL);
352 assert_se(lower == 9999);
353 assert_se(upper == 9999);
354
355 assert_se(parse_range("111-123,4", &lower, &upper) == -EINVAL);
356 assert_se(lower == 9999);
357 assert_se(upper == 9999);
358
359 /* Error on trailing dash */
360 assert_se(parse_range("111-", &lower, &upper) == -EINVAL);
361 assert_se(lower == 9999);
362 assert_se(upper == 9999);
363
364 assert_se(parse_range("111-123-", &lower, &upper) == -EINVAL);
365 assert_se(lower == 9999);
366 assert_se(upper == 9999);
367
368 assert_se(parse_range("111--", &lower, &upper) == -EINVAL);
369 assert_se(lower == 9999);
370 assert_se(upper == 9999);
371
372 assert_se(parse_range("111- ", &lower, &upper) == -EINVAL);
373 assert_se(lower == 9999);
374 assert_se(upper == 9999);
375
376 /* Whitespace is not a separator */
377 assert_se(parse_range("111 123", &lower, &upper) == -EINVAL);
378 assert_se(lower == 9999);
379 assert_se(upper == 9999);
380
381 assert_se(parse_range("111\t123", &lower, &upper) == -EINVAL);
382 assert_se(lower == 9999);
383 assert_se(upper == 9999);
384
385 assert_se(parse_range("111 \t 123", &lower, &upper) == -EINVAL);
386 assert_se(lower == 9999);
387 assert_se(upper == 9999);
388
389 /* Trailing whitespace is invalid (from safe_atou) */
390 assert_se(parse_range("111 ", &lower, &upper) == -EINVAL);
391 assert_se(lower == 9999);
392 assert_se(upper == 9999);
393
394 assert_se(parse_range("111-123 ", &lower, &upper) == -EINVAL);
395 assert_se(lower == 9999);
396 assert_se(upper == 9999);
397
398 assert_se(parse_range("111 -123", &lower, &upper) == -EINVAL);
399 assert_se(lower == 9999);
400 assert_se(upper == 9999);
401
402 assert_se(parse_range("111 -123 ", &lower, &upper) == -EINVAL);
403 assert_se(lower == 9999);
404 assert_se(upper == 9999);
405
406 assert_se(parse_range("111\t-123\t", &lower, &upper) == -EINVAL);
407 assert_se(lower == 9999);
408 assert_se(upper == 9999);
409
410 assert_se(parse_range("111 \t -123 \t ", &lower, &upper) == -EINVAL);
411 assert_se(lower == 9999);
412 assert_se(upper == 9999);
413
414 /* Out of the "unsigned" range, this is 1<<64 */
415 assert_se(parse_range("0-18446744073709551616", &lower, &upper) == -ERANGE);
416 assert_se(lower == 9999);
417 assert_se(upper == 9999);
418 }
419
TEST(safe_atolli)420 TEST(safe_atolli) {
421 int r;
422 long long l;
423
424 r = safe_atolli("12345", &l);
425 assert_se(r == 0);
426 assert_se(l == 12345);
427
428 r = safe_atolli(" 12345", &l);
429 assert_se(r == 0);
430 assert_se(l == 12345);
431
432 r = safe_atolli("-12345", &l);
433 assert_se(r == 0);
434 assert_se(l == -12345);
435
436 r = safe_atolli(" -12345", &l);
437 assert_se(r == 0);
438 assert_se(l == -12345);
439
440 r = safe_atolli("0x5", &l);
441 assert_se(r == 0);
442 assert_se(l == 5);
443
444 r = safe_atolli("0o6", &l);
445 assert_se(r == 0);
446 assert_se(l == 6);
447
448 r = safe_atolli("0B101", &l);
449 assert_se(r == 0);
450 assert_se(l == 5);
451
452 r = safe_atolli("12345678901234567890", &l);
453 assert_se(r == -ERANGE);
454
455 r = safe_atolli("-12345678901234567890", &l);
456 assert_se(r == -ERANGE);
457
458 r = safe_atolli("junk", &l);
459 assert_se(r == -EINVAL);
460
461 r = safe_atolli("123x", &l);
462 assert_se(r == -EINVAL);
463
464 r = safe_atolli("12.3", &l);
465 assert_se(r == -EINVAL);
466
467 r = safe_atolli("", &l);
468 assert_se(r == -EINVAL);
469 }
470
TEST(safe_atou16)471 TEST(safe_atou16) {
472 int r;
473 uint16_t l;
474
475 r = safe_atou16("12345", &l);
476 assert_se(r == 0);
477 assert_se(l == 12345);
478
479 r = safe_atou16(" 12345", &l);
480 assert_se(r == 0);
481 assert_se(l == 12345);
482
483 r = safe_atou16("123456", &l);
484 assert_se(r == -ERANGE);
485
486 r = safe_atou16("-1", &l);
487 assert_se(r == -ERANGE);
488
489 r = safe_atou16(" -1", &l);
490 assert_se(r == -ERANGE);
491
492 r = safe_atou16("junk", &l);
493 assert_se(r == -EINVAL);
494
495 r = safe_atou16("123x", &l);
496 assert_se(r == -EINVAL);
497
498 r = safe_atou16("12.3", &l);
499 assert_se(r == -EINVAL);
500
501 r = safe_atou16("", &l);
502 assert_se(r == -EINVAL);
503 }
504
TEST(safe_atoi16)505 TEST(safe_atoi16) {
506 int r;
507 int16_t l;
508
509 r = safe_atoi16("-12345", &l);
510 assert_se(r == 0);
511 assert_se(l == -12345);
512
513 r = safe_atoi16(" -12345", &l);
514 assert_se(r == 0);
515 assert_se(l == -12345);
516
517 r = safe_atoi16("32767", &l);
518 assert_se(r == 0);
519 assert_se(l == 32767);
520
521 r = safe_atoi16(" 32767", &l);
522 assert_se(r == 0);
523 assert_se(l == 32767);
524
525 r = safe_atoi16("0o11", &l);
526 assert_se(r == 0);
527 assert_se(l == 9);
528
529 r = safe_atoi16("0B110", &l);
530 assert_se(r == 0);
531 assert_se(l == 6);
532
533 r = safe_atoi16("36536", &l);
534 assert_se(r == -ERANGE);
535
536 r = safe_atoi16("-32769", &l);
537 assert_se(r == -ERANGE);
538
539 r = safe_atoi16("junk", &l);
540 assert_se(r == -EINVAL);
541
542 r = safe_atoi16("123x", &l);
543 assert_se(r == -EINVAL);
544
545 r = safe_atoi16("12.3", &l);
546 assert_se(r == -EINVAL);
547
548 r = safe_atoi16("", &l);
549 assert_se(r == -EINVAL);
550 }
551
TEST(safe_atoux16)552 TEST(safe_atoux16) {
553 int r;
554 uint16_t l;
555
556 r = safe_atoux16("1234", &l);
557 assert_se(r == 0);
558 assert_se(l == 0x1234);
559
560 r = safe_atoux16("abcd", &l);
561 assert_se(r == 0);
562 assert_se(l == 0xabcd);
563
564 r = safe_atoux16(" 1234", &l);
565 assert_se(r == 0);
566 assert_se(l == 0x1234);
567
568 r = safe_atoux16("12345", &l);
569 assert_se(r == -ERANGE);
570
571 r = safe_atoux16("-1", &l);
572 assert_se(r == -ERANGE);
573
574 r = safe_atoux16(" -1", &l);
575 assert_se(r == -ERANGE);
576
577 r = safe_atoux16("0b1", &l);
578 assert_se(r == 0);
579 assert_se(l == 177);
580
581 r = safe_atoux16("0o70", &l);
582 assert_se(r == -EINVAL);
583
584 r = safe_atoux16("junk", &l);
585 assert_se(r == -EINVAL);
586
587 r = safe_atoux16("123x", &l);
588 assert_se(r == -EINVAL);
589
590 r = safe_atoux16("12.3", &l);
591 assert_se(r == -EINVAL);
592
593 r = safe_atoux16("", &l);
594 assert_se(r == -EINVAL);
595 }
596
TEST(safe_atou64)597 TEST(safe_atou64) {
598 int r;
599 uint64_t l;
600
601 r = safe_atou64("12345", &l);
602 assert_se(r == 0);
603 assert_se(l == 12345);
604
605 r = safe_atou64(" 12345", &l);
606 assert_se(r == 0);
607 assert_se(l == 12345);
608
609 r = safe_atou64("0o11", &l);
610 assert_se(r == 0);
611 assert_se(l == 9);
612
613 r = safe_atou64("0b11", &l);
614 assert_se(r == 0);
615 assert_se(l == 3);
616
617 r = safe_atou64("18446744073709551617", &l);
618 assert_se(r == -ERANGE);
619
620 r = safe_atou64("-1", &l);
621 assert_se(r == -ERANGE);
622
623 r = safe_atou64(" -1", &l);
624 assert_se(r == -ERANGE);
625
626 r = safe_atou64("junk", &l);
627 assert_se(r == -EINVAL);
628
629 r = safe_atou64("123x", &l);
630 assert_se(r == -EINVAL);
631
632 r = safe_atou64("12.3", &l);
633 assert_se(r == -EINVAL);
634
635 r = safe_atou64("", &l);
636 assert_se(r == -EINVAL);
637 }
638
TEST(safe_atoi64)639 TEST(safe_atoi64) {
640 int r;
641 int64_t l;
642
643 r = safe_atoi64("-12345", &l);
644 assert_se(r == 0);
645 assert_se(l == -12345);
646
647 r = safe_atoi64(" -12345", &l);
648 assert_se(r == 0);
649 assert_se(l == -12345);
650
651 r = safe_atoi64("32767", &l);
652 assert_se(r == 0);
653 assert_se(l == 32767);
654
655 r = safe_atoi64(" 32767", &l);
656 assert_se(r == 0);
657 assert_se(l == 32767);
658
659 r = safe_atoi64(" 0o20", &l);
660 assert_se(r == 0);
661 assert_se(l == 16);
662
663 r = safe_atoi64(" 0b01010", &l);
664 assert_se(r == 0);
665 assert_se(l == 10);
666
667 r = safe_atoi64("9223372036854775813", &l);
668 assert_se(r == -ERANGE);
669
670 r = safe_atoi64("-9223372036854775813", &l);
671 assert_se(r == -ERANGE);
672
673 r = safe_atoi64("junk", &l);
674 assert_se(r == -EINVAL);
675
676 r = safe_atoi64("123x", &l);
677 assert_se(r == -EINVAL);
678
679 r = safe_atoi64("12.3", &l);
680 assert_se(r == -EINVAL);
681
682 r = safe_atoi64("", &l);
683 assert_se(r == -EINVAL);
684 }
685
TEST(safe_atoux64)686 TEST(safe_atoux64) {
687 int r;
688 uint64_t l;
689
690 r = safe_atoux64("12345", &l);
691 assert_se(r == 0);
692 assert_se(l == 0x12345);
693
694 r = safe_atoux64(" 12345", &l);
695 assert_se(r == 0);
696 assert_se(l == 0x12345);
697
698 r = safe_atoux64("0x12345", &l);
699 assert_se(r == 0);
700 assert_se(l == 0x12345);
701
702 r = safe_atoux64("0b11011", &l);
703 assert_se(r == 0);
704 assert_se(l == 11603985);
705
706 r = safe_atoux64("0o11011", &l);
707 assert_se(r == -EINVAL);
708
709 r = safe_atoux64("18446744073709551617", &l);
710 assert_se(r == -ERANGE);
711
712 r = safe_atoux64("-1", &l);
713 assert_se(r == -ERANGE);
714
715 r = safe_atoux64(" -1", &l);
716 assert_se(r == -ERANGE);
717
718 r = safe_atoux64("junk", &l);
719 assert_se(r == -EINVAL);
720
721 r = safe_atoux64("123x", &l);
722 assert_se(r == -EINVAL);
723
724 r = safe_atoux64("12.3", &l);
725 assert_se(r == -EINVAL);
726
727 r = safe_atoux64("", &l);
728 assert_se(r == -EINVAL);
729 }
730
TEST(safe_atod)731 TEST(safe_atod) {
732 int r;
733 double d;
734 char *e;
735
736 r = safe_atod("junk", &d);
737 assert_se(r == -EINVAL);
738
739 r = safe_atod("0.2244", &d);
740 assert_se(r == 0);
741 assert_se(fabs(d - 0.2244) < 0.000001);
742
743 r = safe_atod("0,5", &d);
744 assert_se(r == -EINVAL);
745
746 errno = 0;
747 strtod("0,5", &e);
748 assert_se(*e == ',');
749
750 r = safe_atod("", &d);
751 assert_se(r == -EINVAL);
752
753 /* Check if this really is locale independent */
754 if (setlocale(LC_NUMERIC, "de_DE.utf8")) {
755
756 r = safe_atod("0.2244", &d);
757 assert_se(r == 0);
758 assert_se(fabs(d - 0.2244) < 0.000001);
759
760 r = safe_atod("0,5", &d);
761 assert_se(r == -EINVAL);
762
763 errno = 0;
764 assert_se(fabs(strtod("0,5", &e) - 0.5) < 0.00001);
765
766 r = safe_atod("", &d);
767 assert_se(r == -EINVAL);
768 }
769
770 /* And check again, reset */
771 assert_se(setlocale(LC_NUMERIC, "C"));
772
773 r = safe_atod("0.2244", &d);
774 assert_se(r == 0);
775 assert_se(fabs(d - 0.2244) < 0.000001);
776
777 r = safe_atod("0,5", &d);
778 assert_se(r == -EINVAL);
779
780 errno = 0;
781 strtod("0,5", &e);
782 assert_se(*e == ',');
783
784 r = safe_atod("", &d);
785 assert_se(r == -EINVAL);
786 }
787
TEST(parse_nice)788 TEST(parse_nice) {
789 int n;
790
791 assert_se(parse_nice("0", &n) >= 0 && n == 0);
792 assert_se(parse_nice("+0", &n) >= 0 && n == 0);
793 assert_se(parse_nice("-1", &n) >= 0 && n == -1);
794 assert_se(parse_nice("-2", &n) >= 0 && n == -2);
795 assert_se(parse_nice("1", &n) >= 0 && n == 1);
796 assert_se(parse_nice("2", &n) >= 0 && n == 2);
797 assert_se(parse_nice("+1", &n) >= 0 && n == 1);
798 assert_se(parse_nice("+2", &n) >= 0 && n == 2);
799 assert_se(parse_nice("-20", &n) >= 0 && n == -20);
800 assert_se(parse_nice("19", &n) >= 0 && n == 19);
801 assert_se(parse_nice("+19", &n) >= 0 && n == 19);
802
803 assert_se(parse_nice("", &n) == -EINVAL);
804 assert_se(parse_nice("-", &n) == -EINVAL);
805 assert_se(parse_nice("+", &n) == -EINVAL);
806 assert_se(parse_nice("xx", &n) == -EINVAL);
807 assert_se(parse_nice("-50", &n) == -ERANGE);
808 assert_se(parse_nice("50", &n) == -ERANGE);
809 assert_se(parse_nice("+50", &n) == -ERANGE);
810 assert_se(parse_nice("-21", &n) == -ERANGE);
811 assert_se(parse_nice("20", &n) == -ERANGE);
812 assert_se(parse_nice("+20", &n) == -ERANGE);
813 }
814
TEST(parse_errno)815 TEST(parse_errno) {
816 assert_se(parse_errno("EILSEQ") == EILSEQ);
817 assert_se(parse_errno("EINVAL") == EINVAL);
818 assert_se(parse_errno("0") == 0);
819 assert_se(parse_errno("1") == 1);
820 assert_se(parse_errno("4095") == 4095);
821
822 assert_se(parse_errno("-1") == -ERANGE);
823 assert_se(parse_errno("-3") == -ERANGE);
824 assert_se(parse_errno("4096") == -ERANGE);
825
826 assert_se(parse_errno("") == -EINVAL);
827 assert_se(parse_errno("12.3") == -EINVAL);
828 assert_se(parse_errno("123junk") == -EINVAL);
829 assert_se(parse_errno("junk123") == -EINVAL);
830 assert_se(parse_errno("255EILSEQ") == -EINVAL);
831 assert_se(parse_errno("EINVAL12") == -EINVAL);
832 assert_se(parse_errno("-EINVAL") == -EINVAL);
833 assert_se(parse_errno("EINVALaaa") == -EINVAL);
834 }
835
TEST(parse_mtu)836 TEST(parse_mtu) {
837 uint32_t mtu = 0;
838
839 assert_se(parse_mtu(AF_UNSPEC, "1500", &mtu) >= 0 && mtu == 1500);
840 assert_se(parse_mtu(AF_UNSPEC, "1400", &mtu) >= 0 && mtu == 1400);
841 assert_se(parse_mtu(AF_UNSPEC, "65535", &mtu) >= 0 && mtu == 65535);
842 assert_se(parse_mtu(AF_UNSPEC, "65536", &mtu) >= 0 && mtu == 65536);
843 assert_se(parse_mtu(AF_UNSPEC, "4294967295", &mtu) >= 0 && mtu == 4294967295);
844 assert_se(parse_mtu(AF_UNSPEC, "500", &mtu) >= 0 && mtu == 500);
845 assert_se(parse_mtu(AF_UNSPEC, "1280", &mtu) >= 0 && mtu == 1280);
846 assert_se(parse_mtu(AF_INET6, "1280", &mtu) >= 0 && mtu == 1280);
847 assert_se(parse_mtu(AF_INET6, "1279", &mtu) == -ERANGE);
848 assert_se(parse_mtu(AF_UNSPEC, "4294967296", &mtu) == -ERANGE);
849 assert_se(parse_mtu(AF_INET6, "4294967296", &mtu) == -ERANGE);
850 assert_se(parse_mtu(AF_INET6, "68", &mtu) == -ERANGE);
851 assert_se(parse_mtu(AF_UNSPEC, "68", &mtu) >= 0 && mtu == 68);
852 assert_se(parse_mtu(AF_UNSPEC, "67", &mtu) == -ERANGE);
853 assert_se(parse_mtu(AF_UNSPEC, "0", &mtu) == -ERANGE);
854 assert_se(parse_mtu(AF_UNSPEC, "", &mtu) == -EINVAL);
855 }
856
TEST(parse_loadavg_fixed_point)857 TEST(parse_loadavg_fixed_point) {
858 loadavg_t fp;
859
860 assert_se(parse_loadavg_fixed_point("1.23", &fp) == 0);
861 assert_se(LOADAVG_INT_SIDE(fp) == 1);
862 assert_se(LOADAVG_DECIMAL_SIDE(fp) == 23);
863
864 assert_se(parse_loadavg_fixed_point("1.80", &fp) == 0);
865 assert_se(LOADAVG_INT_SIDE(fp) == 1);
866 assert_se(LOADAVG_DECIMAL_SIDE(fp) == 80);
867
868 assert_se(parse_loadavg_fixed_point("0.07", &fp) == 0);
869 assert_se(LOADAVG_INT_SIDE(fp) == 0);
870 assert_se(LOADAVG_DECIMAL_SIDE(fp) == 7);
871
872 assert_se(parse_loadavg_fixed_point("0.00", &fp) == 0);
873 assert_se(LOADAVG_INT_SIDE(fp) == 0);
874 assert_se(LOADAVG_DECIMAL_SIDE(fp) == 0);
875
876 assert_se(parse_loadavg_fixed_point("4096.57", &fp) == 0);
877 assert_se(LOADAVG_INT_SIDE(fp) == 4096);
878 assert_se(LOADAVG_DECIMAL_SIDE(fp) == 57);
879
880 /* Caps out at 2 digit fracs */
881 assert_se(parse_loadavg_fixed_point("1.100", &fp) == -ERANGE);
882
883 assert_se(parse_loadavg_fixed_point("4096.4096", &fp) == -ERANGE);
884 assert_se(parse_loadavg_fixed_point("-4000.5", &fp) == -ERANGE);
885 assert_se(parse_loadavg_fixed_point("18446744073709551615.5", &fp) == -ERANGE);
886 assert_se(parse_loadavg_fixed_point("foobar", &fp) == -EINVAL);
887 assert_se(parse_loadavg_fixed_point("3333", &fp) == -EINVAL);
888 assert_se(parse_loadavg_fixed_point("1.2.3", &fp) == -EINVAL);
889 assert_se(parse_loadavg_fixed_point(".", &fp) == -EINVAL);
890 assert_se(parse_loadavg_fixed_point("", &fp) == -EINVAL);
891 }
892
893 DEFINE_TEST_MAIN(LOG_INFO);
894