bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger This file is part of systemd.
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger Copyright 2010 Lennart Poettering
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger Copyright 2013 Thomas H.P. Andersen
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger systemd is free software; you can redistribute it and/or modify it
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger under the terms of the GNU Lesser General Public License as published by
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger the Free Software Foundation; either version 2.1 of the License, or
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger (at your option) any later version.
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger systemd is distributed in the hope that it will be useful, but
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger WITHOUT ANY WARRANTY; without even the implied warranty of
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger Lesser General Public License for more details.
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger You should have received a copy of the GNU Lesser General Public License
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger along with systemd; If not, see <http://www.gnu.org/licenses/>.
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_boolean("false") == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_boolean("FALSE") == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_boolean("garbage") < 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger r = parse_pid("0xFFFFFFFFFFFFFFFFF", &pid);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_mode("77777", &m) < 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_mode("544", &m) >= 0 && m == 0544);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_mode("777", &m) >= 0 && m == 0777);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_mode("7777", &m) >= 0 && m == 07777);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_mode("0", &m) >= 0 && m == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size("111", 1024, &bytes) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size("111.4", 1024, &bytes) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size(" 112 B", 1024, &bytes) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size(" 112.6 B", 1024, &bytes) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size("3.5 K", 1024, &bytes) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size("3. K", 1024, &bytes) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size("3.0 K", 1024, &bytes) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size("3. 0 K", 1024, &bytes) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size(" 4 M 11.5K", 1024, &bytes) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(bytes == 4*1024*1024 + 11 * 1024 + 512);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size("3B3.5G", 1024, &bytes) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size("3.5G3B", 1024, &bytes) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(bytes == 3ULL*1024*1024*1024 + 512*1024*1024 + 3);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size("3.5G 4B", 1024, &bytes) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(bytes == 3ULL*1024*1024*1024 + 512*1024*1024 + 4);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size("3B3G4T", 1024, &bytes) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size("4T3G3B", 1024, &bytes) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(bytes == (4ULL*1024 + 3)*1024*1024*1024 + 3);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size(" 4 T 3 G 3 B", 1024, &bytes) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(bytes == (4ULL*1024 + 3)*1024*1024*1024 + 3);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size("12P", 1024, &bytes) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(bytes == 12ULL * 1024*1024*1024*1024*1024);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size("12P12P", 1024, &bytes) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size("3E 2P", 1024, &bytes) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(bytes == (3 * 1024 + 2ULL) * 1024*1024*1024*1024*1024);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size("12X", 1024, &bytes) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size("12.5X", 1024, &bytes) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size("12.5e3", 1024, &bytes) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size("1024E", 1024, &bytes) == -ERANGE);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size("-1", 1024, &bytes) == -ERANGE);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size("-1024E", 1024, &bytes) == -ERANGE);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size("-1024P", 1024, &bytes) == -ERANGE);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_size("-10B 20K", 1024, &bytes) == -ERANGE);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger /* Successful cases */
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("111", &lower, &upper) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("111-123", &lower, &upper) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("123-111", &lower, &upper) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("123-123", &lower, &upper) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("0", &lower, &upper) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("0-15", &lower, &upper) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("15-0", &lower, &upper) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("128-65535", &lower, &upper) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("1024-4294967295", &lower, &upper) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger /* Leading whitespace is acceptable */
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range(" 111", &lower, &upper) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range(" 111-123", &lower, &upper) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("111- 123", &lower, &upper) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("\t111-\t123", &lower, &upper) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range(" \t 111- \t 123", &lower, &upper) == 0);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger /* Error cases, make sure they fail as expected */
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("111garbage", &lower, &upper) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("garbage111", &lower, &upper) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("garbage", &lower, &upper) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("111-123garbage", &lower, &upper) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("111garbage-123", &lower, &upper) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger /* Empty string */
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("", &lower, &upper) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger /* 111--123 will pass -123 to safe_atou which returns -ERANGE for negative */
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("111--123", &lower, &upper) == -ERANGE);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("-111-123", &lower, &upper) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("111-123-", &lower, &upper) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("111.4-123", &lower, &upper) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("111-123.4", &lower, &upper) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("111,4-123", &lower, &upper) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("111-123,4", &lower, &upper) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger /* Error on trailing dash */
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("111-", &lower, &upper) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("111-123-", &lower, &upper) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("111--", &lower, &upper) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("111- ", &lower, &upper) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger /* Whitespace is not a separator */
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("111 123", &lower, &upper) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("111\t123", &lower, &upper) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("111 \t 123", &lower, &upper) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger /* Trailing whitespace is invalid (from safe_atou) */
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("111 ", &lower, &upper) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("111-123 ", &lower, &upper) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("111 -123", &lower, &upper) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("111 -123 ", &lower, &upper) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("111\t-123\t", &lower, &upper) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("111 \t -123 \t ", &lower, &upper) == -EINVAL);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger /* Out of the "unsigned" range, this is 1<<64 */
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(parse_range("0-18446744073709551616", &lower, &upper) == -ERANGE);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(fabs(d - 0.2244) < 0.000001);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger /* Check if this really is locale independent */
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger if (setlocale(LC_NUMERIC, "de_DE.utf8")) {
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(fabs(d - 0.2244) < 0.000001);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(fabs(strtod("0,5", &e) - 0.5) < 0.00001);
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger /* And check again, reset */
bb269eed0e447a2f973677246959b403d480ed63Filipe Brandenburger assert_se(fabs(d - 0.2244) < 0.000001);