naccyde / rpms / systemd

Forked from rpms/systemd a year ago
Clone
21255d
From 457eada27f606e39f0efc6adc226542fd11eb815 Mon Sep 17 00:00:00 2001
21255d
From: Lennart Poettering <lennart@poettering.net>
21255d
Date: Mon, 1 Jun 2020 17:48:41 +0200
21255d
Subject: [PATCH] tests: beef up integer parsing tests
21255d
21255d
(cherry picked from commit 53c6db99fa4b52f97e19977f21d3133f8ceb3dcd)
21255d
21255d
Related: #1848373
21255d
---
21255d
 src/test/test-parse-util.c | 58 ++++++++++++++++++++++++++++++++++++++
21255d
 src/test/test-user-util.c  | 40 ++++++++++++++++++++++++++
21255d
 2 files changed, 98 insertions(+)
21255d
21255d
diff --git a/src/test/test-parse-util.c b/src/test/test-parse-util.c
21255d
index 8b182d9bdc..699499b665 100644
21255d
--- a/src/test/test-parse-util.c
21255d
+++ b/src/test/test-parse-util.c
21255d
@@ -75,14 +75,22 @@ static void test_parse_mode(void) {
21255d
         mode_t m;
21255d
 
21255d
         assert_se(parse_mode("-1", &m) < 0);
21255d
+        assert_se(parse_mode("+1", &m) < 0);
21255d
         assert_se(parse_mode("", &m) < 0);
21255d
         assert_se(parse_mode("888", &m) < 0);
21255d
         assert_se(parse_mode("77777", &m) < 0);
21255d
 
21255d
         assert_se(parse_mode("544", &m) >= 0 && m == 0544);
21255d
+        assert_se(parse_mode("0544", &m) >= 0 && m == 0544);
21255d
+        assert_se(parse_mode("00544", &m) >= 0 && m == 0544);
21255d
         assert_se(parse_mode("777", &m) >= 0 && m == 0777);
21255d
+        assert_se(parse_mode("0777", &m) >= 0 && m == 0777);
21255d
+        assert_se(parse_mode("00777", &m) >= 0 && m == 0777);
21255d
         assert_se(parse_mode("7777", &m) >= 0 && m == 07777);
21255d
+        assert_se(parse_mode("07777", &m) >= 0 && m == 07777);
21255d
+        assert_se(parse_mode("007777", &m) >= 0 && m == 07777);
21255d
         assert_se(parse_mode("0", &m) >= 0 && m == 0);
21255d
+        assert_se(parse_mode(" 1", &m) >= 0 && m == 1);
21255d
 }
21255d
 
21255d
 static void test_parse_size(void) {
21255d
@@ -358,6 +366,18 @@ static void test_safe_atolli(void) {
21255d
         assert_se(r == 0);
21255d
         assert_se(l == -12345);
21255d
 
21255d
+        r = safe_atolli("0x5", &l);
21255d
+        assert_se(r == 0);
21255d
+        assert_se(l == 5);
21255d
+
21255d
+        r = safe_atolli("0o6", &l);
21255d
+        assert_se(r == 0);
21255d
+        assert_se(l == 6);
21255d
+
21255d
+        r = safe_atolli("0B101", &l);
21255d
+        assert_se(r == 0);
21255d
+        assert_se(l == 5);
21255d
+
21255d
         r = safe_atolli("12345678901234567890", &l);
21255d
         assert_se(r == -ERANGE);
21255d
 
21255d
@@ -431,6 +451,14 @@ static void test_safe_atoi16(void) {
21255d
         assert_se(r == 0);
21255d
         assert_se(l == 32767);
21255d
 
21255d
+        r = safe_atoi16("0o11", &l);
21255d
+        assert_se(r == 0);
21255d
+        assert_se(l == 9);
21255d
+
21255d
+        r = safe_atoi16("0B110", &l);
21255d
+        assert_se(r == 0);
21255d
+        assert_se(l == 6);
21255d
+
21255d
         r = safe_atoi16("36536", &l);
21255d
         assert_se(r == -ERANGE);
21255d
 
21255d
@@ -475,6 +503,13 @@ static void test_safe_atoux16(void) {
21255d
         r = safe_atoux16("  -1", &l);
21255d
         assert_se(r == -ERANGE);
21255d
 
21255d
+        r = safe_atoux16("0b1", &l);
21255d
+        assert_se(r == 0);
21255d
+        assert_se(l == 177);
21255d
+
21255d
+        r = safe_atoux16("0o70", &l);
21255d
+        assert_se(r == -EINVAL);
21255d
+
21255d
         r = safe_atoux16("junk", &l);
21255d
         assert_se(r == -EINVAL);
21255d
 
21255d
@@ -500,6 +535,14 @@ static void test_safe_atou64(void) {
21255d
         assert_se(r == 0);
21255d
         assert_se(l == 12345);
21255d
 
21255d
+        r = safe_atou64("0o11", &l);
21255d
+        assert_se(r == 0);
21255d
+        assert_se(l == 9);
21255d
+
21255d
+        r = safe_atou64("0b11", &l);
21255d
+        assert_se(r == 0);
21255d
+        assert_se(l == 3);
21255d
+
21255d
         r = safe_atou64("18446744073709551617", &l);
21255d
         assert_se(r == -ERANGE);
21255d
 
21255d
@@ -542,6 +585,14 @@ static void test_safe_atoi64(void) {
21255d
         assert_se(r == 0);
21255d
         assert_se(l == 32767);
21255d
 
21255d
+        r = safe_atoi64("  0o20", &l);
21255d
+        assert_se(r == 0);
21255d
+        assert_se(l == 16);
21255d
+
21255d
+        r = safe_atoi64("  0b01010", &l);
21255d
+        assert_se(r == 0);
21255d
+        assert_se(l == 10);
21255d
+
21255d
         r = safe_atoi64("9223372036854775813", &l);
21255d
         assert_se(r == -ERANGE);
21255d
 
21255d
@@ -577,6 +628,13 @@ static void test_safe_atoux64(void) {
21255d
         assert_se(r == 0);
21255d
         assert_se(l == 0x12345);
21255d
 
21255d
+        r = safe_atoux64("0b11011", &l);
21255d
+        assert_se(r == 0);
21255d
+        assert_se(l == 11603985);
21255d
+
21255d
+        r = safe_atoux64("0o11011", &l);
21255d
+        assert_se(r == -EINVAL);
21255d
+
21255d
         r = safe_atoux64("18446744073709551617", &l);
21255d
         assert_se(r == -ERANGE);
21255d
 
21255d
diff --git a/src/test/test-user-util.c b/src/test/test-user-util.c
21255d
index 99203f7e48..04e86f5ac3 100644
21255d
--- a/src/test/test-user-util.c
21255d
+++ b/src/test/test-user-util.c
21255d
@@ -40,6 +40,22 @@ static void test_parse_uid(void) {
21255d
 
21255d
         log_info("/* %s */", __func__);
21255d
 
21255d
+        r = parse_uid("0", &uid);
21255d
+        assert_se(r == 0);
21255d
+        assert_se(uid == 0);
21255d
+
21255d
+        r = parse_uid("1", &uid);
21255d
+        assert_se(r == 0);
21255d
+        assert_se(uid == 1);
21255d
+
21255d
+        r = parse_uid("01", &uid);
21255d
+        assert_se(r == -EINVAL);
21255d
+        assert_se(uid == 1);
21255d
+
21255d
+        r = parse_uid("001", &uid);
21255d
+        assert_se(r == -EINVAL);
21255d
+        assert_se(uid == 1);
21255d
+
21255d
         r = parse_uid("100", &uid);
21255d
         assert_se(r == 0);
21255d
         assert_se(uid == 100);
21255d
@@ -52,6 +68,14 @@ static void test_parse_uid(void) {
21255d
         assert_se(r == -EINVAL);
21255d
         assert_se(uid == 100);
21255d
 
21255d
+        r = parse_uid("0o1234", &uid);
21255d
+        assert_se(r == -EINVAL);
21255d
+        assert_se(uid == 100);
21255d
+
21255d
+        r = parse_uid("0b1234", &uid);
21255d
+        assert_se(r == -EINVAL);
21255d
+        assert_se(uid == 100);
21255d
+
21255d
         r = parse_uid("+1234", &uid);
21255d
         assert_se(r == -EINVAL);
21255d
         assert_se(uid == 100);
21255d
@@ -68,6 +92,14 @@ static void test_parse_uid(void) {
21255d
         assert_se(r == -EINVAL);
21255d
         assert_se(uid == 100);
21255d
 
21255d
+        r = parse_uid("001234", &uid);
21255d
+        assert_se(r == -EINVAL);
21255d
+        assert_se(uid == 100);
21255d
+
21255d
+        r = parse_uid("0001234", &uid);
21255d
+        assert_se(r == -EINVAL);
21255d
+        assert_se(uid == 100);
21255d
+
21255d
         r = parse_uid("-0", &uid);
21255d
         assert_se(r == -EINVAL);
21255d
         assert_se(uid == 100);
21255d
@@ -76,6 +108,14 @@ static void test_parse_uid(void) {
21255d
         assert_se(r == -EINVAL);
21255d
         assert_se(uid == 100);
21255d
 
21255d
+        r = parse_uid("00", &uid);
21255d
+        assert_se(r == -EINVAL);
21255d
+        assert_se(uid == 100);
21255d
+
21255d
+        r = parse_uid("000", &uid);
21255d
+        assert_se(r == -EINVAL);
21255d
+        assert_se(uid == 100);
21255d
+
21255d
         r = parse_uid("asdsdas", &uid);
21255d
         assert_se(r == -EINVAL);
21255d
         assert_se(uid == 100);