test-open: support mingw errno values
[gnulib.git] / tests / test-fopen.h
1 /* Test of opening a file stream.
2    Copyright (C) 2007-2009 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 of the License, or
7    (at your option) 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, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 /* Include <config.h> and a form of <stdio.h> first.  */
20
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24
25 #define ASSERT(expr) \
26   do                                                                         \
27     {                                                                        \
28       if (!(expr))                                                           \
29         {                                                                    \
30           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
31           fflush (stderr);                                                   \
32           abort ();                                                          \
33         }                                                                    \
34     }                                                                        \
35   while (0)
36
37 /* Test fopen.  Assumes BASE is defined.  */
38
39 static int
40 test_fopen (void)
41 {
42   FILE *f;
43   /* Remove anything from prior partial run.  */
44   unlink (BASE "file");
45
46   /* Read requires existing file.  */
47   errno = 0;
48   ASSERT (fopen (BASE "file", "r") == NULL);
49   ASSERT (errno == ENOENT);
50
51   /* Write can create a file.  */
52   f = fopen (BASE "file", "w");
53   ASSERT (f);
54   ASSERT (fclose (f) == 0);
55
56   /* Trailing slash is invalid on non-directory.  */
57   errno = 0;
58   ASSERT (fopen (BASE "file/", "r") == NULL);
59   ASSERT (errno == ENOTDIR || errno == EISDIR || errno == EINVAL);
60
61   /* Cannot create a directory.  */
62   errno = 0;
63   ASSERT (fopen ("nonexist.ent/", "w") == NULL);
64   ASSERT (errno == ENOTDIR || errno == EISDIR || errno == ENOENT
65           || errno == EINVAL);
66
67   /* Directories cannot be opened for writing.  */
68   errno = 0;
69   ASSERT (fopen (".", "w") == NULL);
70   ASSERT (errno == EISDIR || errno == EINVAL || errno == EACCES);
71
72   /* /dev/null must exist, and be writable.  */
73   f = fopen ("/dev/null", "r");
74   ASSERT (f);
75   ASSERT (fclose (f) == 0);
76   f = fopen ("/dev/null", "w");
77   ASSERT (f);
78   ASSERT (fclose (f) == 0);
79
80   /* Cleanup.  */
81   ASSERT (unlink (BASE "file") == 0);
82
83   return 0;
84 }