summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorSwen Schillig <swen@linux.ibm.com>2019-06-03 10:58:11 +0200
committerRalph Boehme <slow@samba.org>2019-06-30 12:47:24 +0000
commitd5383297f0389d01d42479b074f5e81619e03ddb (patch)
treeb247cea275b9e5cfef2dea22f4501f9f2dc3b5ce /lib
parentdac981a3887fe79650b38e63799e344b22c8f5f1 (diff)
downloadsamba-d5383297f0389d01d42479b074f5e81619e03ddb.tar.gz
tests-util: Adding test to verify "allow no conversion" flag
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
Diffstat (limited to 'lib')
-rw-r--r--lib/util/tests/util.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/util/tests/util.c b/lib/util/tests/util.c
index 4741f466b35..4876144bcdc 100644
--- a/lib/util/tests/util.c
+++ b/lib/util/tests/util.c
@@ -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;
}