1 /* Test of strerror_r() function.
2 Copyright (C) 2007-2011 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 #include "signature.h"
23 SIGNATURE_CHECK (strerror_r, int, (int, char *, size_t));
35 /* Test results with valid errnum and enough room. */
39 ASSERT (strerror_r (EACCES, buf, sizeof buf) == 0);
40 ASSERT (buf[0] != '\0');
42 ASSERT (strlen (buf) < sizeof buf);
46 ASSERT (strerror_r (ETIMEDOUT, buf, sizeof buf) == 0);
47 ASSERT (buf[0] != '\0');
49 ASSERT (strlen (buf) < sizeof buf);
53 ASSERT (strerror_r (EOVERFLOW, buf, sizeof buf) == 0);
54 ASSERT (buf[0] != '\0');
56 ASSERT (strlen (buf) < sizeof buf);
58 /* POSIX requires strerror (0) to succeed. Reject use of "Unknown
59 error", but allow "Success", "No error", or even Solaris' "Error
60 0" which are distinct patterns from true out-of-range strings.
61 http://austingroupbugs.net/view.php?id=382 */
64 ret = strerror_r (0, buf, sizeof buf);
68 ASSERT (strstr (buf, "nknown") == NULL);
69 ASSERT (strstr (buf, "ndefined") == NULL);
71 /* Test results with out-of-range errnum and enough room. POSIX
72 allows an empty string on success, and allows an unchanged buf on
73 error, but these are not useful, so we guarantee contents. */
76 ret = strerror_r (-3, buf, sizeof buf);
77 ASSERT (ret == 0 || ret == EINVAL);
78 ASSERT (buf[0] != '^');
81 ASSERT (strlen (buf) < sizeof buf);
83 /* Test results with a too small buffer. POSIX requires an error;
84 only ERANGE for 0 and valid errors, and a choice of ERANGE or
85 EINVAL for out-of-range values. On error, POSIX permits buf to
86 be empty, unchanged, or unterminated, but these are not useful,
87 so we guarantee NUL-terminated truncated contents for all but
88 size 0. http://austingroupbugs.net/view.php?id=398. Also ensure
89 that no out-of-bounds writes occur. */
91 int errs[] = { EACCES, 0, -3, };
94 buf[sizeof buf - 1] = '\0';
95 for (j = 0; j < SIZEOF (errs); j++)
98 char buf2[sizeof buf] = "";
102 strerror_r (err, buf2, sizeof buf2);
104 ASSERT (len < sizeof buf);
106 for (i = 0; i <= len; i++)
108 memset (buf, '^', sizeof buf - 1);
110 ret = strerror_r (err, buf, i);
113 ASSERT (ret == ERANGE || ret == EINVAL);
115 ASSERT (ret == ERANGE);
118 ASSERT (strncmp (buf, buf2, i - 1) == 0);
119 ASSERT (buf[i - 1] == '\0');
121 ASSERT (strspn (buf + i, "^") == sizeof buf - 1 - i);
124 strcpy (buf, "BADFACE");
126 ret = strerror_r (err, buf, len + 1);
127 ASSERT (ret != ERANGE);
129 ASSERT (strcmp (buf, buf2) == 0);
134 /* Test that strerror_r does not clobber strerror buffer. On some
135 platforms, this test can only succeed if gnulib also replaces
147 msg1 = strerror (ENOENT);
149 str1 = strdup (msg1);
152 msg2 = strerror (ERANGE);
154 str2 = strdup (msg2);
157 msg3 = strerror (-4);
159 str3 = strdup (msg3);
162 msg4 = strerror (1729576);
164 str4 = strdup (msg4);
167 strerror_r (EACCES, buf, sizeof buf);
168 strerror_r (-5, buf, sizeof buf);
169 ASSERT (msg1 == msg2 || msg1 == msg4 || STREQ (msg1, str1));
170 ASSERT (msg2 == msg4 || STREQ (msg2, str2));
171 ASSERT (msg3 == msg4 || STREQ (msg3, str3));
172 ASSERT (STREQ (msg4, str4));