strerror: relax test for Solaris
[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   buf[0] = '\0';
38   ASSERT (strerror_r (EACCES, buf, sizeof (buf)) == 0);
39   ASSERT (buf[0] != '\0');
40
41   buf[0] = '\0';
42   ASSERT (strerror_r (ETIMEDOUT, buf, sizeof (buf)) == 0);
43   ASSERT (buf[0] != '\0');
44
45   buf[0] = '\0';
46   ASSERT (strerror_r (EOVERFLOW, buf, sizeof (buf)) == 0);
47   ASSERT (buf[0] != '\0');
48
49   /* POSIX requires strerror (0) to succeed.  Reject use of "Unknown
50      error", but allow "Success", "No error", or even Solaris' "Error
51      0" which are distinct patterns from true out-of-range strings.
52      http://austingroupbugs.net/view.php?id=382  */
53   buf[0] = '\0';
54   ret = strerror_r (0, buf, sizeof (buf));
55   ASSERT (ret == 0);
56   ASSERT (buf[0]);
57   ASSERT (strstr (buf, "nknown") == NULL);
58
59   /* Test results with out-of-range errnum and enough room.  */
60
61   buf[0] = '^';
62   ret = strerror_r (-3, buf, sizeof (buf));
63   ASSERT (ret == 0 || ret == EINVAL);
64   if (ret == 0)
65     ASSERT (buf[0] != '^');
66
67   /* Test results with a too small buffer.  */
68
69   ASSERT (strerror_r (EACCES, buf, sizeof (buf)) == 0);
70   {
71     size_t len = strlen (buf);
72     size_t i;
73
74     for (i = 0; i <= len; i++)
75       {
76         strcpy (buf, "BADFACE");
77         ret = strerror_r (EACCES, buf, i);
78         if (ret == 0)
79           {
80             /* Truncated result.  POSIX allows this, and it actually
81                happens on AIX 6.1 and Cygwin.  */
82             ASSERT ((strcmp (buf, "BADFACE") == 0) == (i == 0));
83           }
84         else
85           {
86             /* Failure.  */
87             ASSERT (ret == ERANGE);
88             /* buf is clobbered nevertheless, on FreeBSD and MacOS X.  */
89           }
90       }
91
92     strcpy (buf, "BADFACE");
93     ret = strerror_r (EACCES, buf, len + 1);
94     ASSERT (ret == 0);
95   }
96
97   return 0;
98 }