strerror_r: enforce POSIX recommendations
[gnulib.git] / tests / test-strerror_r.c
1 /* Test of strerror_r() function.
2    Copyright (C) 2007-2011 Free Software Foundation, Inc.
3
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)
7    any later version.
8
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.
13
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.  */
17
18 #include <config.h>
19
20 #include <string.h>
21
22 #include "signature.h"
23 SIGNATURE_CHECK (strerror_r, int, (int, char *, size_t));
24
25 #include <errno.h>
26
27 #include "macros.h"
28
29 int
30 main (void)
31 {
32   char buf[100];
33   int ret;
34
35   /* Test results with valid errnum and enough room.  */
36
37   errno = 0;
38   buf[0] = '\0';
39   ASSERT (strerror_r (EACCES, buf, sizeof buf) == 0);
40   ASSERT (buf[0] != '\0');
41   ASSERT (errno == 0);
42   ASSERT (strlen (buf) < sizeof buf);
43
44   errno = 0;
45   buf[0] = '\0';
46   ASSERT (strerror_r (ETIMEDOUT, buf, sizeof buf) == 0);
47   ASSERT (buf[0] != '\0');
48   ASSERT (errno == 0);
49   ASSERT (strlen (buf) < sizeof buf);
50
51   errno = 0;
52   buf[0] = '\0';
53   ASSERT (strerror_r (EOVERFLOW, buf, sizeof buf) == 0);
54   ASSERT (buf[0] != '\0');
55   ASSERT (errno == 0);
56   ASSERT (strlen (buf) < sizeof buf);
57
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  */
62   errno = 0;
63   buf[0] = '\0';
64   ret = strerror_r (0, buf, sizeof buf);
65   ASSERT (ret == 0);
66   ASSERT (buf[0]);
67   ASSERT (errno == 0);
68   ASSERT (strstr (buf, "nknown") == NULL);
69
70   /* Test results with out-of-range errnum and enough room.  POSIX
71      allows an empty string on success, and allows an unchanged buf on
72      error, but these are not useful, so we guarantee contents.  */
73   errno = 0;
74   buf[0] = '^';
75   ret = strerror_r (-3, buf, sizeof buf);
76   ASSERT (ret == 0 || ret == EINVAL);
77   ASSERT (buf[0] != '^');
78   ASSERT (*buf);
79   ASSERT (errno == 0);
80   ASSERT (strlen (buf) < sizeof buf);
81
82   /* Test results with a too small buffer.  POSIX requires an error;
83      only ERANGE for 0 and valid errors, and a choice of ERANGE or
84      EINVAL for out-of-range values.  On error, POSIX permits buf to
85      be empty, unchanged, or unterminated, but these are not useful,
86      so we guarantee NUL-terminated truncated contents for all but
87      size 0.  http://austingroupbugs.net/view.php?id=398  */
88   {
89     int errs[] = { EACCES, 0, -3, };
90     int j;
91     for (j = 0; j < SIZEOF (errs); j++)
92       {
93         int err = errs[j];
94         char buf2[sizeof buf] = "";
95         size_t len;
96         size_t i;
97
98         strerror_r (err, buf2, sizeof buf2);
99         len = strlen (buf2);
100
101         for (i = 0; i <= len; i++)
102           {
103             strcpy (buf, "BADFACE");
104             errno = 0;
105             ret = strerror_r (err, buf, i);
106             ASSERT (errno == 0);
107             if (err < 0)
108               ASSERT (ret == ERANGE || ret == EINVAL);
109             else
110               ASSERT (ret == ERANGE);
111             if (i == 0)
112               ASSERT (strcmp (buf, "BADFACE") == 0);
113             else
114               {
115                 ASSERT (strncmp (buf, buf2, i - 1) == 0);
116                 ASSERT (buf[i - 1] == '\0');
117               }
118           }
119
120         strcpy (buf, "BADFACE");
121         errno = 0;
122         ret = strerror_r (err, buf, len + 1);
123         ASSERT (ret != ERANGE);
124         ASSERT (errno == 0);
125         ASSERT (strcmp (buf, buf2) == 0);
126       }
127   }
128
129 #if GNULIB_STRERROR
130   /* Test that strerror_r does not clobber strerror buffer.  On some
131      platforms, this test can only succeed if gnulib also replaces
132      strerror.  */
133   {
134     const char *msg1;
135     const char *msg2;
136     const char *msg3;
137     const char *msg4;
138     char *str1;
139     char *str2;
140     char *str3;
141     char *str4;
142
143     msg1 = strerror (ENOENT);
144     ASSERT (msg1);
145     str1 = strdup (msg1);
146     ASSERT (str1);
147
148     msg2 = strerror (ERANGE);
149     ASSERT (msg2);
150     str2 = strdup (msg2);
151     ASSERT (str2);
152
153     msg3 = strerror (-4);
154     ASSERT (msg3);
155     str3 = strdup (msg3);
156     ASSERT (str3);
157
158     msg4 = strerror (1729576);
159     ASSERT (msg4);
160     str4 = strdup (msg4);
161     ASSERT (str4);
162
163     strerror_r (EACCES, buf, sizeof buf);
164     strerror_r (-5, buf, sizeof buf);
165     ASSERT (msg1 == msg2 || msg1 == msg4 || STREQ (msg1, str1));
166     ASSERT (msg2 == msg4 || STREQ (msg2, str2));
167     ASSERT (msg3 == msg4 || STREQ (msg3, str3));
168     ASSERT (STREQ (msg4, str4));
169
170     free (str1);
171     free (str2);
172     free (str3);
173     free (str4);
174   }
175 #endif
176
177   return 0;
178 }