perror: work around FreeBSD bug
[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
91   /* Test that perror uses the same message as strerror.  */
92   {
93     int errs[] = { EACCES, 0, -3, };
94     int i;
95     for (i = 0; i < SIZEOF (errs); i++)
96       {
97         char buf[256];
98         char *err = strerror (errs[i]);
99
100         ASSERT (err);
101         ASSERT (strlen (err) < sizeof buf);
102         rewind (stderr);
103         ASSERT (ftruncate (fileno (stderr), 0) == 0);
104         errno = errs[i];
105         perror (NULL);
106         ASSERT (!ferror (stderr));
107         rewind (stderr);
108         ASSERT (fgets (buf, sizeof buf, stderr) == buf);
109         ASSERT (strstr (buf, err));
110       }
111   }
112
113   /* Test that perror reports write failure.  */
114   {
115     ASSERT (freopen (BASE ".tmp", "r", stderr) == stderr);
116     ASSERT (setvbuf (stderr, NULL, _IOLBF, BUFSIZ) == 0);
117     errno = -1;
118     ASSERT (!ferror (stderr));
119     perror (NULL);
120     ASSERT (errno > 0);
121 #if 0
122     /* Commented out until glibc behaves:
123        http://sourceware.org/bugzilla/show_bug.cgi?id=12792 */
124     ASSERT (ferror (stderr));
125 #endif
126   }
127
128   ASSERT (fclose (stderr) == 0);
129   ASSERT (remove (BASE ".tmp") == 0);
130
131   return 0;
132 }