1 /* Test of perror() function.
2 Copyright (C) 2011 Free Software Foundation, Inc.
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)
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.
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. */
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
35 #define BASE "test-perror2"
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)
45 ASSERT (freopen (BASE ".tmp", "w+", stderr) == stderr);
47 /* Test that perror does not clobber strerror buffer. */
58 msg1 = strerror (ENOENT);
63 msg2 = strerror (ERANGE);
73 msg4 = strerror (1729576);
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));
94 /* Test that perror uses the same message as strerror. */
96 int errs[] = { EACCES, 0, -3, };
98 for (i = 0; i < SIZEOF (errs); i++)
101 char *err = strerror (errs[i]);
104 ASSERT (strlen (err) < sizeof buf);
106 ASSERT (ftruncate (fileno (stderr), 0) == 0);
109 ASSERT (!ferror (stderr));
111 ASSERT (fgets (buf, sizeof buf, stderr) == buf);
112 ASSERT (strstr (buf, err));
116 /* Test that perror reports write failure. */
118 ASSERT (freopen (BASE ".tmp", "r", stderr) == stderr);
119 ASSERT (setvbuf (stderr, NULL, _IONBF, BUFSIZ) == 0);
121 ASSERT (!ferror (stderr));
124 /* Commented out until cygwin behaves:
125 http://sourceware.org/ml/newlib/2011/msg00228.html */
127 /* Commented out until glibc behaves:
128 http://sourceware.org/bugzilla/show_bug.cgi?id=12792 */
129 ASSERT (ferror (stderr));
133 ASSERT (fclose (stderr) == 0);
134 ASSERT (remove (BASE ".tmp") == 0);