tests-util: Adding test to verify "full-string-conversion" flag
authorSwen Schillig <swen@linux.ibm.com>
Mon, 3 Jun 2019 08:37:07 +0000 (10:37 +0200)
committerRalph Boehme <slow@samba.org>
Sun, 30 Jun 2019 11:32:19 +0000 (11:32 +0000)
The standard string to integer conversion routines stop at the first
character which cannot be converted to a number.
However, if such a character is found, it is not considered an error.
With the flag "SMB_STR_FULL_STR_CONV" enabled, an error will be returned
if the string could not be converted entirely.

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 f12021fe2a2ce7d159efc97f2c88bba97b403364..4741f466b35c01f3e94be18d33585dbba84fcabf 100644 (file)
@@ -541,6 +541,31 @@ static bool test_smb_strtoul_allow_negative(struct torture_context *tctx)
        return true;
 }
 
+static bool test_smb_strtoul_full_string(struct torture_context *tctx)
+{
+       const char *number = "123 ";
+       const char *number2 = "123";
+       int err;
+
+       err = 0;
+       smb_strtoul(number, NULL, 0, &err, SMB_STR_FULL_STR_CONV);
+       torture_assert(tctx, err == EINVAL, "strtoul_err: Expected EINVAL");
+
+       err = 0;
+       smb_strtoull(number, NULL, 0, &err, SMB_STR_FULL_STR_CONV);
+       torture_assert(tctx, err == EINVAL, "strtoull_err: Expected EINVAL");
+
+       err = 0;
+       smb_strtoul(number2, NULL, 0, &err, SMB_STR_FULL_STR_CONV);
+       torture_assert(tctx, err == 0, "strtoul_err: Unexpected error");
+
+       err = 0;
+       smb_strtoull(number2, NULL, 0, &err, SMB_STR_FULL_STR_CONV);
+       torture_assert(tctx, err == 0, "strtoull_err: Unexpected error");
+
+       return true;
+}
+
 struct torture_suite *torture_local_util(TALLOC_CTX *mem_ctx)
 {
        struct torture_suite *suite =
@@ -564,5 +589,8 @@ struct torture_suite *torture_local_util(TALLOC_CTX *mem_ctx)
        torture_suite_add_simple_test(suite,
                                      "smb_strtoul(l) allow_negative",
                                      test_smb_strtoul_allow_negative);
+       torture_suite_add_simple_test(suite,
+                                     "smb_strtoul(l) full string conversion",
+                                     test_smb_strtoul_full_string);
        return suite;
 }