[PATCH] tests-util: adding tests for the new wrappers strtoul_err and strtoull_err

swen swen at linux.ibm.com
Fri Apr 12 07:19:16 UTC 2019


Hi

Adding three tests for the above mentioned two new wrappers.

Please review and push if happy.

Thanks for your support in advance.

Cheers Swen
-------------- next part --------------
From d939052aacadd1e3a274f8fc647e627ce4305f4f Mon Sep 17 00:00:00 2001
From: Swen Schillig <swen at linux.ibm.com>
Date: Wed, 10 Apr 2019 10:24:52 +0200
Subject: [PATCH 1/3] tests-util: Adding strtoul(l)_err() test leaving errno
 untouched

The wrapper functions strtoul_err() and strtoull_err() trigger
other functions/routines which modify errno.
However, callers of those wrapper functions expect errno to be unchanged.
This test verifies the expectation.

Signed-off-by: Swen Schillig <swen at linux.ibm.com>
---
 lib/util/tests/util.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/lib/util/tests/util.c b/lib/util/tests/util.c
index ad9c6606e59..c73d58a7f27 100644
--- a/lib/util/tests/util.c
+++ b/lib/util/tests/util.c
@@ -3,6 +3,7 @@
  *
  * Copyright Martin Schwenke <martin at meltin.net> 2016
  * Copyright Christof Schmitt <cs at samba.org> 2018
+ * Copyright Swen Schillig <swen at linux.ibm.com> 2019
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -421,6 +422,31 @@ done:
 	return ret;
 }
 
+static bool test_strtoul_err_errno_check(struct torture_context *tctx)
+{
+	const char *number = "123";
+	int unsigned long int val = 0;
+	int unsigned long long int vall = 0;
+	int err;
+
+	/* select an error code which is not set by the strtoul_err routines */
+	errno = EAGAIN;
+	err = EAGAIN;
+	val = strtoul_err(number, NULL, 0, &err);
+	torture_assert(tctx, errno == EAGAIN, "strtoul_err: Expected EAGAIN");
+	torture_assert(tctx, err == 0, "strtoul_err: Expected err = 0");
+	torture_assert(tctx, val == 123, "strtoul_err: Expected value 123");
+
+	/* set err to an impossible value again before continuing */
+	err = EAGAIN;
+	vall = strtoull_err(number, NULL, 0, &err);
+	torture_assert(tctx, errno == EAGAIN, "strtoull_err: Expected EAGAIN");
+	torture_assert(tctx, err == 0, "strtoul_err: Expected err = 0");
+	torture_assert(tctx, vall == 123, "strtoul_err: Expected value 123");
+
+	return true;
+}
+
 struct torture_suite *torture_local_util(TALLOC_CTX *mem_ctx)
 {
 	struct torture_suite *suite =
@@ -432,5 +458,8 @@ struct torture_suite *torture_local_util(TALLOC_CTX *mem_ctx)
 	torture_suite_add_simple_test(suite,
 				      "directory_create_or_exist",
 				      test_directory_create_or_exist);
+	torture_suite_add_simple_test(suite,
+				      "strtoul(l)_err errno",
+				      test_strtoul_err_errno_check);
 	return suite;
 }
-- 
2.20.1


From 0983c628a4735158f8f05844f3bcf70bb354c8ce Mon Sep 17 00:00:00 2001
From: Swen Schillig <swen at linux.ibm.com>
Date: Wed, 10 Apr 2019 10:44:06 +0200
Subject: [PATCH 2/3] tests-util: Adding test to verify negative "number"
 detection

Verify that a string representing a negative number is throwing an error.

Signed-off-by: Swen Schillig <swen at linux.ibm.com>
---
 lib/util/tests/util.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/lib/util/tests/util.c b/lib/util/tests/util.c
index c73d58a7f27..96e7f6e8511 100644
--- a/lib/util/tests/util.c
+++ b/lib/util/tests/util.c
@@ -447,6 +447,40 @@ static bool test_strtoul_err_errno_check(struct torture_context *tctx)
 	return true;
 }
 
+static bool test_strtoul_err_negative(struct torture_context *tctx)
+{
+	const char *number = "-132";
+	const char *number2 = "132-";
+	unsigned long int val = 0;
+	unsigned long long int vall = 0;
+	int err;
+
+	err = 0;
+	strtoul_err(number, NULL, 0, &err);
+	torture_assert(tctx, err == EINVAL, "strtoul_err: Expected EINVAL");
+
+	err = 0;
+	strtoull_err(number, NULL, 0, &err);
+	torture_assert(tctx, err == EINVAL, "strtoull_err: Expected EINVAL");
+
+	/* it is allowed to have a "-" sign after a number,
+	 * e.g. as part of a formular, however, it is not supposed to
+	 * have an effect on the converted value.
+	 */
+
+	err = 0;
+	val = strtoul_err(number2, NULL, 0, &err);
+	torture_assert(tctx, err == 0, "strtoul_err: Expected no error");
+	torture_assert(tctx, val == 132, "strtoul_err: Wrong value");
+
+	err = 0;
+	vall = strtoull_err(number2, NULL, 0, &err);
+	torture_assert(tctx, err == 0, "strtoull_err: Expected no error");
+	torture_assert(tctx, vall == 132, "strtoull_err: Wrong value");
+
+	return true;
+}
+
 struct torture_suite *torture_local_util(TALLOC_CTX *mem_ctx)
 {
 	struct torture_suite *suite =
@@ -461,5 +495,8 @@ struct torture_suite *torture_local_util(TALLOC_CTX *mem_ctx)
 	torture_suite_add_simple_test(suite,
 				      "strtoul(l)_err errno",
 				      test_strtoul_err_errno_check);
+	torture_suite_add_simple_test(suite,
+				      "strtoul(l)_err negative",
+				      test_strtoul_err_negative);
 	return suite;
 }
-- 
2.20.1


From 68e645c1b73aa224d08297f47a30fde76559859a Mon Sep 17 00:00:00 2001
From: Swen Schillig <swen at linux.ibm.com>
Date: Wed, 10 Apr 2019 10:52:35 +0200
Subject: [PATCH 3/3] tests-util: Adding test to verify "no-conversion"
 detection

The standard string to integer conversion routines return zero
if a string was to be converted which did not reflect a number.
It is not flag'ed as an error.
The wrapper functions strtoul_err() and strtoull_err() are expected
to exactly do this.

Signed-off-by: Swen Schillig <swen at linux.ibm.com>
---
 lib/util/tests/util.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/lib/util/tests/util.c b/lib/util/tests/util.c
index 96e7f6e8511..4448f6a81b9 100644
--- a/lib/util/tests/util.c
+++ b/lib/util/tests/util.c
@@ -481,6 +481,31 @@ static bool test_strtoul_err_negative(struct torture_context *tctx)
 	return true;
 }
 
+static bool test_strtoul_err_no_number(struct torture_context *tctx)
+{
+	const char *number = "ghijk";
+	const char *blank = "";
+	int err;
+
+	err = 0;
+	strtoul_err(number, NULL, 0, &err);
+	torture_assert(tctx, err == EINVAL, "strtoul_err: Expected EINVAL");
+
+	err = 0;
+	strtoull_err(number, NULL, 0, &err);
+	torture_assert(tctx, err == EINVAL, "strtoull_err: Expected EINVAL");
+
+	err = 0;
+	strtoul_err(blank, NULL, 0, &err);
+	torture_assert(tctx, err == EINVAL, "strtoul_err: Expected EINVAL");
+
+	err = 0;
+	strtoull_err(blank, NULL, 0, &err);
+	torture_assert(tctx, err == EINVAL, "strtoull_err: Expected EINVAL");
+
+	return true;
+}
+
 struct torture_suite *torture_local_util(TALLOC_CTX *mem_ctx)
 {
 	struct torture_suite *suite =
@@ -498,5 +523,8 @@ struct torture_suite *torture_local_util(TALLOC_CTX *mem_ctx)
 	torture_suite_add_simple_test(suite,
 				      "strtoul(l)_err negative",
 				      test_strtoul_err_negative);
+	torture_suite_add_simple_test(suite,
+				      "strtoul(l)_err no number",
+				      test_strtoul_err_no_number);
 	return suite;
 }
-- 
2.20.1

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://lists.samba.org/pipermail/samba-technical/attachments/20190412/544dcc3a/signature.sig>


More information about the samba-technical mailing list