From: Swen Schillig Date: Mon, 3 Jun 2019 08:58:11 +0000 (+0200) Subject: tests-util: Adding test to verify "allow no conversion" flag X-Git-Tag: ldb-2.0.5~129 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d5383297f0389d01d42479b074f5e81619e03ddb;p=thirdparty%2Fsamba.git 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 Reviewed-by: Ralph Boehme Reviewed-by: Christof Schmitt Autobuild-User(master): Ralph Böhme Autobuild-Date(master): Sun Jun 30 12:47:24 UTC 2019 on sn-devel-184 --- 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; }