tests-util: Adding strtoul(l)_err() test leaving errno untouched
authorSwen Schillig <swen@linux.ibm.com>
Wed, 10 Apr 2019 08:24:52 +0000 (10:24 +0200)
committerRalph Boehme <slow@samba.org>
Sun, 30 Jun 2019 11:32:17 +0000 (11:32 +0000)
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@linux.ibm.com>
Reviewed-by: Ralph Boehme <slow@samba.org>
Reviewed-by: Christof Schmitt <cs@samba.org>
lib/util/tests/util.c

index ad9c6606e59cd4374663b4fb8608e0247abd9635..3f59ce6f6dc47de00e6ca9fee77501a265ce8d64 100644 (file)
@@ -3,6 +3,7 @@
  *
  * Copyright Martin Schwenke <martin@meltin.net> 2016
  * Copyright Christof Schmitt <cs@samba.org> 2018
+ * Copyright Swen Schillig <swen@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";
+       unsigned long int val = 0;
+       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;
 }