NEWS.stable: log cherry-pick [ae006b4]->[4a9738e] strtoimax: Avoid link error on...
[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   /* We change fd 2 later, so save it in fd 10.  */
41   if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
42       || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
43     return 2;
44
45   ASSERT (freopen (BASE ".tmp", "w+", stderr) == stderr);
46
47   /* Test that perror does not clobber strerror buffer.  */
48   {
49     const char *msg1;
50     const char *msg2;
51     const char *msg3;
52     const char *msg4;
53     char *str1;
54     char *str2;
55     char *str3;
56     char *str4;
57
58     msg1 = strerror (ENOENT);
59     ASSERT (msg1);
60     str1 = strdup (msg1);
61     ASSERT (str1);
62
63     msg2 = strerror (ERANGE);
64     ASSERT (msg2);
65     str2 = strdup (msg2);
66     ASSERT (str2);
67
68     msg3 = strerror (-4);
69     ASSERT (msg3);
70     str3 = strdup (msg3);
71     ASSERT (str3);
72
73     msg4 = strerror (1729576);
74     ASSERT (msg4);
75     str4 = strdup (msg4);
76     ASSERT (str4);
77
78     errno = EACCES;
79     perror ("");
80     errno = -5;
81     perror ("");
82     ASSERT (!ferror (stderr));
83     ASSERT (msg1 == msg2 || msg1 == msg4 || STREQ (msg1, str1));
84     ASSERT (msg2 == msg4 || STREQ (msg2, str2));
85     ASSERT (msg3 == msg4 || STREQ (msg3, str3));
86     ASSERT (STREQ (msg4, str4));
87
88     free (str1);
89     free (str2);
90     free (str3);
91     free (str4);
92   }
93
94   /* Test that perror uses the same message as strerror.  */
95   {
96     int errs[] = { EACCES, 0, -3, };
97     int i;
98     for (i = 0; i < SIZEOF (errs); i++)
99       {
100         char buf[256];
101         char *err = strerror (errs[i]);
102
103         ASSERT (err);
104         ASSERT (strlen (err) < sizeof buf);
105         rewind (stderr);
106         ASSERT (ftruncate (fileno (stderr), 0) == 0);
107         errno = errs[i];
108         perror (NULL);
109         ASSERT (!ferror (stderr));
110         rewind (stderr);
111         ASSERT (fgets (buf, sizeof buf, stderr) == buf);
112         ASSERT (strstr (buf, err));
113       }
114   }
115
116   /* Test that perror reports write failure.  */
117   {
118     ASSERT (freopen (BASE ".tmp", "r", stderr) == stderr);
119     ASSERT (setvbuf (stderr, NULL, _IONBF, BUFSIZ) == 0);
120     errno = -1;
121     ASSERT (!ferror (stderr));
122     perror (NULL);
123 #if 0
124     /* Commented out until cygwin behaves:
125        http://sourceware.org/ml/newlib/2011/msg00228.html */
126     ASSERT (errno > 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 }