Merge pull request #29224 from keszybz/netdev-config-parsing

Use a helper to simplify parsing of ranges in netdev config and related changes
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2023-09-22 14:29:47 +02:00
committed by GitHub
35 changed files with 588 additions and 979 deletions
+23 -21
View File
@@ -17,13 +17,13 @@ static void test_int(int i) {
assert_se(ip_protocol_from_name(ip_protocol_to_name(parse_ip_protocol(str))) == i);
}
static void test_int_fail(int i) {
static void test_int_fail(int i, int error) {
char str[DECIMAL_STR_MAX(int)];
assert_se(!ip_protocol_to_name(i));
xsprintf(str, "%i", i);
assert_se(parse_ip_protocol(str) == -EINVAL);
assert_se(parse_ip_protocol(str) == error);
}
static void test_str(const char *s) {
@@ -31,39 +31,41 @@ static void test_str(const char *s) {
assert_se(streq(ip_protocol_to_name(parse_ip_protocol(s)), s));
}
static void test_str_fail(const char *s) {
static void test_str_fail(const char *s, int error) {
assert_se(ip_protocol_from_name(s) == -EINVAL);
assert_se(parse_ip_protocol(s) == -EINVAL);
}
static void test_parse_ip_protocol_one(const char *s, int expected) {
assert_se(parse_ip_protocol(s) == expected);
assert_se(parse_ip_protocol(s) == error);
}
TEST(integer) {
test_int(IPPROTO_TCP);
test_int(IPPROTO_DCCP);
test_int_fail(-1);
test_int_fail(1024 * 1024);
test_int_fail(-1, -ERANGE);
test_int_fail(1024 * 1024, -EPROTONOSUPPORT);
}
TEST(string) {
test_str("sctp");
test_str("udp");
test_str_fail("hoge");
test_str_fail("-1");
test_str_fail("1000000000");
test_str_fail("hoge", -EINVAL);
test_str_fail("-1", -ERANGE);
test_str_fail("1000000000", -EPROTONOSUPPORT);
}
TEST(parse_ip_protocol) {
test_parse_ip_protocol_one("sctp", IPPROTO_SCTP);
test_parse_ip_protocol_one("ScTp", IPPROTO_SCTP);
test_parse_ip_protocol_one("ip", IPPROTO_IP);
test_parse_ip_protocol_one("", IPPROTO_IP);
test_parse_ip_protocol_one("1", 1);
test_parse_ip_protocol_one("0", 0);
test_parse_ip_protocol_one("-10", -EINVAL);
test_parse_ip_protocol_one("100000000", -EINVAL);
assert_se(parse_ip_protocol("sctp") == IPPROTO_SCTP);
assert_se(parse_ip_protocol("ScTp") == IPPROTO_SCTP);
assert_se(parse_ip_protocol("ip") == IPPROTO_IP);
assert_se(parse_ip_protocol("") == IPPROTO_IP);
assert_se(parse_ip_protocol("1") == 1);
assert_se(parse_ip_protocol("0") == 0);
assert_se(parse_ip_protocol("-10") == -ERANGE);
assert_se(parse_ip_protocol("100000000") == -EPROTONOSUPPORT);
}
TEST(parse_ip_protocol_full) {
assert_se(parse_ip_protocol_full("-1", true) == -ERANGE);
assert_se(parse_ip_protocol_full("0", true) == 0);
assert_se(parse_ip_protocol_full("11", true) == 11);
}
DEFINE_TEST_MAIN(LOG_INFO);
+26
View File
@@ -417,6 +417,32 @@ TEST(parse_range) {
assert_se(upper == 9999);
}
TEST(safe_atou_bounded) {
int r;
unsigned x;
r = safe_atou_bounded("12345", 12, 20000, &x);
assert_se(r == 0);
assert_se(x == 12345);
r = safe_atou_bounded("12", 12, 20000, &x);
assert_se(r == 0);
assert_se(x == 12);
r = safe_atou_bounded("20000", 12, 20000, &x);
assert_se(r == 0);
assert_se(x == 20000);
r = safe_atou_bounded("-1", 12, 20000, &x);
assert_se(r == -ERANGE);
r = safe_atou_bounded("11", 12, 20000, &x);
assert_se(r == -ERANGE);
r = safe_atou_bounded("20001", 12, 20000, &x);
assert_se(r == -ERANGE);
}
TEST(safe_atolli) {
int r;
long long l;