strerror_r: avoid clobbering strerror on cygwin
[gnulib.git] / tests / test-perror2.c
1 /* Test of perror() function.
2    Copyright (C) 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 <stdio.h>
21
22 #include <errno.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 /* This test intentionally parses stderr.  So, we arrange to have fd 10
27    (outside the range of interesting fd's during the test) set up to
28    duplicate the original stderr.  */
29 #define BACKUP_STDERR_FILENO 10
30 #define ASSERT_STREAM myerr
31 #include "macros.h"
32
33 static FILE *myerr;
34
35 #define BASE "test-perror2"
36
37 int
38 main (void)
39 {
40   FILE *fp;
41
42   /* We change fd 2 later, so save it in fd 10.  */
43   if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
44       || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
45     return 2;
46
47   ASSERT (freopen (BASE ".tmp", "w+", stderr) == stderr);
48
49   /* Test that perror does not clobber strerror buffer.  */
50   {
51     const char *msg1;
52     const char *msg2;
53     const char *msg3;
54     const char *msg4;
55     char *str1;
56     char *str2;
57     char *str3;
58     char *str4;
59
60     msg1 = strerror (ENOENT);
61     ASSERT (msg1);
62     str1 = strdup (msg1);
63     ASSERT (str1);
64
65     msg2 = strerror (ERANGE);
66     ASSERT (msg2);
67     str2 = strdup (msg2);
68     ASSERT (str2);
69
70     msg3 = strerror (-4);
71     ASSERT (msg3);
72     str3 = strdup (msg3);
73     ASSERT (str3);
74
75     msg4 = strerror (1729576);
76     ASSERT (msg4);
77     str4 = strdup (msg4);
78     ASSERT (str4);
79
80     errno = EACCES;
81     perror ("");
82     errno = -5;
83     perror ("");
84     ASSERT (!ferror (stderr));
85     ASSERT (msg1 == msg2 || msg1 == msg4 || STREQ (msg1, str1));
86     ASSERT (msg2 == msg4 || STREQ (msg2, str2));
87     ASSERT (msg3 == msg4 || STREQ (msg3, str3));
88     ASSERT (STREQ (msg4, str4));
89
90     free (str1);
91     free (str2);
92     free (str3);
93     free (str4);
94   }
95
96   /* Test that perror uses the same message as strerror.  */
97   {
98     int errs[] = { EACCES, 0, -3, };
99     int i;
100     for (i = 0; i < SIZEOF (errs); i++)
101       {
102         char buf[256];
103         char *err = strerror (errs[i]);
104
105         ASSERT (err);
106         ASSERT (strlen (err) < sizeof buf);
107         rewind (stderr);
108         ASSERT (ftruncate (fileno (stderr), 0) == 0);
109         errno = errs[i];
110         perror (NULL);
111         ASSERT (!ferror (stderr));
112         rewind (stderr);
113         ASSERT (fgets (buf, sizeof buf, stderr) == buf);
114         ASSERT (strstr (buf, err));
115       }
116   }
117
118   /* Test that perror reports write failure.  */
119   {
120     ASSERT (freopen (BASE ".tmp", "r", stderr) == stderr);
121     ASSERT (setvbuf (stderr, NULL, _IOLBF, BUFSIZ) == 0);
122     errno = -1;
123     ASSERT (!ferror (stderr));
124     perror (NULL);
125     ASSERT (errno > 0);
126 #if 0
127     /* Commented out until glibc behaves:
128        http://sourceware.org/bugzilla/show_bug.cgi?id=12792 */
129     ASSERT (ferror (stderr));
130 #endif
131   }
132
133   ASSERT (fclose (stderr) == 0);
134   ASSERT (remove (BASE ".tmp") == 0);
135
136   return 0;
137 }