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