]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
tests-util: Adding test to verify "allow no conversion" flag
authorSwen Schillig <swen@linux.ibm.com>
Mon, 3 Jun 2019 08:58:11 +0000 (10:58 +0200)
committerRalph Boehme <slow@samba.org>
Sun, 30 Jun 2019 12:47:24 +0000 (12:47 +0000)
The internal string conversion routines smb_strtoul(l) return
an error if the provided string could not be converted to an integer.
This can be the case if the string is empty or if it starts with non-numeric
characters which cannot be converted.
The standard C library, however, does allow this and simply returns 0 as the
converted value.
If this behaviour is wanted, it can be enabled by using
the "SMB_STR_ALLOW_NO_CONVERSION" flag.

Signed-off-by: Swen Schillig <swen@linux.ibm.com>
Reviewed-by: Ralph Boehme <slow@samba.org>
Reviewed-by: Christof Schmitt <cs@samba.org>
Autobuild-User(master): Ralph Böhme <slow@samba.org>
Autobuild-Date(master): Sun Jun 30 12:47:24 UTC 2019 on sn-devel-184

lib/util/tests/util.c

index 4741f466b35c01f3e94be18d33585dbba84fcabf..4876144bcdcedad01447e2bd505aff5cc3529c91 100644 (file)
@@ -566,6 +566,36 @@ static bool test_smb_strtoul_full_string(struct torture_context *tctx)
        return true;
 }
 
+static bool test_smb_strtoul_allow_no_conversion(struct torture_context *tctx)
+{
+       const char *number = "";
+       const char *number2 = "xyz";
+       unsigned long int n1 = 0;
+       unsigned long long int n2 = 0;
+       int err;
+
+       err = 0;
+       smb_strtoul(number, NULL, 0, &err, SMB_STR_ALLOW_NO_CONVERSION);
+       torture_assert(tctx, err == 0, "strtoul_err: Unexpected error");
+       torture_assert(tctx, n1 == 0, "strtoul_err: Unexpected value");
+
+       err = 0;
+       smb_strtoull(number, NULL, 0, &err, SMB_STR_ALLOW_NO_CONVERSION);
+       torture_assert(tctx, err == 0, "strtoull_err: Unexpected error");
+       torture_assert(tctx, n2 == 0, "strtoull_err: Unexpected value");
+
+       err = 0;
+       smb_strtoul(number2, NULL, 0, &err, SMB_STR_ALLOW_NO_CONVERSION);
+       torture_assert(tctx, err == 0, "strtoul_err: Unexpected error");
+       torture_assert(tctx, n1 == 0, "strtoul_err: Unexpected value");
+
+       err = 0;
+       smb_strtoull(number2, NULL, 0, &err, SMB_STR_ALLOW_NO_CONVERSION);
+       torture_assert(tctx, err == 0, "strtoull_err: Unexpected error");
+       torture_assert(tctx, n2 == 0, "strtoull_err: Unexpected value");
+
+       return true;
+}
 struct torture_suite *torture_local_util(TALLOC_CTX *mem_ctx)
 {
        struct torture_suite *suite =
@@ -592,5 +622,8 @@ struct torture_suite *torture_local_util(TALLOC_CTX *mem_ctx)
        torture_suite_add_simple_test(suite,
                                      "smb_strtoul(l) full string conversion",
                                      test_smb_strtoul_full_string);
+       torture_suite_add_simple_test(suite,
+                                     "smb_strtoul(l) allow no conversion",
+                                     test_smb_strtoul_allow_no_conversion);
        return suite;
 }