tests-util: Adding test to verify "allow-negative" flag
authorSwen Schillig <swen@linux.ibm.com>
Mon, 3 Jun 2019 08:13:52 +0000 (10:13 +0200)
committerRalph Boehme <slow@samba.org>
Sun, 30 Jun 2019 11:32:18 +0000 (11:32 +0000)
The standard string to integer conversion routines allow strings
with a leading "-" to indicate a negative number.
However, the returned value is always an unsigned value representing
the bit-pattern of this negative value.
Typically, this behaviour is NOT wanted and therefore the standard
behavior of the internal smb_strtoul(l) return an erros in such situations.
It can be enabled though by using the flag SMB_STR_ALLOW_NEGATIVE.
This test verifies the correct processing.

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 854d1d2023fcb3bd952f2938b1f9a15b02a93d8d..f12021fe2a2ce7d159efc97f2c88bba97b403364 100644 (file)
@@ -31,6 +31,9 @@
 
 #include "lib/util/samba_util.h"
 
+#include "limits.h"
+#include "string.h"
+
 struct test_trim_string_data {
        const char *desc;
        const char *in;
@@ -506,6 +509,38 @@ static bool test_smb_strtoul_no_number(struct torture_context *tctx)
        return true;
 }
 
+static bool test_smb_strtoul_allow_negative(struct torture_context *tctx)
+{
+       const char *number = "-1";
+       const char *number2 = "-1-1";
+       unsigned long res = 0;
+       unsigned long long res2 = 0;
+       char *end_ptr = NULL;
+       int err;
+
+       err = 0;
+       res = smb_strtoul(number, NULL, 0, &err, SMB_STR_ALLOW_NEGATIVE);
+       torture_assert(tctx, err == 0, "strtoul_err: Unexpected error");
+       torture_assert(tctx, res == ULONG_MAX, "strtoul_err: Unexpected value");
+
+       err = 0;
+       res2 = smb_strtoull(number, NULL, 0, &err, SMB_STR_ALLOW_NEGATIVE);
+       torture_assert(tctx, err == 0, "strtoull_err: Unexpected error");
+       torture_assert(tctx, res2 == ULLONG_MAX, "strtoull_err: Unexpected value");
+
+       err = 0;
+       smb_strtoul(number2, &end_ptr, 0, &err, SMB_STR_ALLOW_NEGATIVE);
+       torture_assert(tctx, err == 0, "strtoul_err: Unexpected error");
+       torture_assert(tctx, end_ptr[0] == '-', "strtoul_err: Unexpected end pointer");
+
+       err = 0;
+       smb_strtoull(number2, &end_ptr, 0, &err, SMB_STR_ALLOW_NEGATIVE);
+       torture_assert(tctx, err == 0, "strtoull_err: Unexpected error");
+       torture_assert(tctx, end_ptr[0] == '-', "strtoull_err: Unexpected end pointer");
+
+       return true;
+}
+
 struct torture_suite *torture_local_util(TALLOC_CTX *mem_ctx)
 {
        struct torture_suite *suite =
@@ -526,5 +561,8 @@ struct torture_suite *torture_local_util(TALLOC_CTX *mem_ctx)
        torture_suite_add_simple_test(suite,
                                      "smb_strtoul(l) no number",
                                      test_smb_strtoul_no_number);
+       torture_suite_add_simple_test(suite,
+                                     "smb_strtoul(l) allow_negative",
+                                     test_smb_strtoul_allow_negative);
        return suite;
 }