Skip to content

Commit

Permalink
tests-util: Adding test to verify "allow no conversion" flag
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Ralph Boehme <[email protected]>
Reviewed-by: Christof Schmitt <[email protected]>

Autobuild-User(master): Ralph Böhme <[email protected]>
Autobuild-Date(master): Sun Jun 30 12:47:24 UTC 2019 on sn-devel-184
  • Loading branch information
sswen authored and slowfranklin committed Jun 30, 2019
1 parent dac981a commit d538329
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lib/util/tests/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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;
}

0 comments on commit d538329

Please sign in to comment.