tests-util: Adding test to verify "no-conversion" detection
authorSwen Schillig <swen@linux.ibm.com>
Wed, 10 Apr 2019 08:52:35 +0000 (10:52 +0200)
committerRalph Boehme <slow@samba.org>
Sun, 30 Jun 2019 11:32:18 +0000 (11:32 +0000)
The standard string to integer conversion routines return zero
if a string was to be converted which did not reflect a number.
It is not flag'ed as an error.
The wrapper functions strtoul_err() and strtoull_err() are expected
to exactly do this.

Signed-off-by: Swen Schillig <swen@linux.ibm.com>
Reviewed-by: Ralph Boehme <slow@samba.org>
Reviewed-by: Christof Schmitt <cs@samba.org>
lib/util/tests/util.c

index c45f3835cf76a66bca3d58de50036361f5bba0c5..de9dca1ffc57efabf035d6f37d06f152fdb92b94 100644 (file)
@@ -481,6 +481,31 @@ static bool test_strtoul_err_negative(struct torture_context *tctx)
        return true;
 }
 
+static bool test_strtoul_err_no_number(struct torture_context *tctx)
+{
+       const char *number = "ghijk";
+       const char *blank = "";
+       int err;
+
+       err = 0;
+       strtoul_err(number, NULL, 0, &err);
+       torture_assert(tctx, err == EINVAL, "strtoul_err: Expected EINVAL");
+
+       err = 0;
+       strtoull_err(number, NULL, 0, &err);
+       torture_assert(tctx, err == EINVAL, "strtoull_err: Expected EINVAL");
+
+       err = 0;
+       strtoul_err(blank, NULL, 0, &err);
+       torture_assert(tctx, err == EINVAL, "strtoul_err: Expected EINVAL");
+
+       err = 0;
+       strtoull_err(blank, NULL, 0, &err);
+       torture_assert(tctx, err == EINVAL, "strtoull_err: Expected EINVAL");
+
+       return true;
+}
+
 struct torture_suite *torture_local_util(TALLOC_CTX *mem_ctx)
 {
        struct torture_suite *suite =
@@ -498,5 +523,8 @@ struct torture_suite *torture_local_util(TALLOC_CTX *mem_ctx)
        torture_suite_add_simple_test(suite,
                                      "strtoul(l)_err negative",
                                      test_strtoul_err_negative);
+       torture_suite_add_simple_test(suite,
+                                     "strtoul(l)_err no number",
+                                     test_strtoul_err_no_number);
        return suite;
 }