test-open: support mingw errno values
[gnulib.git] / tests / test-open.h
1 /* Test of opening a file descriptor.
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 <fcntl.h> first.  */
20
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25
26 #define ASSERT(expr) \
27   do                                                                         \
28     {                                                                        \
29       if (!(expr))                                                           \
30         {                                                                    \
31           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
32           fflush (stderr);                                                   \
33           abort ();                                                          \
34         }                                                                    \
35     }                                                                        \
36   while (0)
37
38 /* Test fopen.  Assumes BASE is defined.  */
39
40 static int
41 test_open (void)
42 {
43   int fd;
44   /* Remove anything from prior partial run.  */
45   unlink (BASE "file");
46
47   /* Cannot create directory.  */
48   errno = 0;
49   ASSERT (open ("nonexist.ent/", O_CREAT | O_RDONLY, 0600) == -1);
50   ASSERT (errno == ENOTDIR || errno == EISDIR || errno == ENOENT
51           || errno == EINVAL);
52
53   /* Create a regular file.  */
54   fd = open (BASE "file", O_CREAT | O_RDONLY, 0600);
55   ASSERT (0 <= fd);
56   ASSERT (close (fd) == 0);
57
58   /* Trailing slash handling.  */
59   errno = 0;
60   ASSERT (open (BASE "file/", O_RDONLY) == -1);
61   ASSERT (errno == ENOTDIR || errno == EISDIR || errno == EINVAL);
62
63   /* Directories cannot be opened for writing.  */
64   errno = 0;
65   ASSERT (open (".", O_WRONLY) == -1);
66   ASSERT (errno == EISDIR || errno == EACCES);
67
68   /* /dev/null must exist, and be writable.  */
69   fd = open ("/dev/null", O_RDONLY);
70   ASSERT (0 <= fd);
71   {
72     char c;
73     ASSERT (read (fd, &c, 1) == 0);
74   }
75   ASSERT (close (fd) == 0);
76   fd = open ("/dev/null", O_WRONLY);
77   ASSERT (0 <= fd);
78   ASSERT (write (fd, "c", 1) == 1);
79   ASSERT (close (fd) == 0);
80
81   /* Cleanup.  */
82   ASSERT (unlink (BASE "file") == 0);
83
84   return 0;
85 }