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