open-tests: port to glibc with _FORTIFY_SOURCE and -O1
[gnulib.git] / tests / test-open.h
1 /* Test of opening a file descriptor.
2    Copyright (C) 2007-2013 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 /* Make test_open always inline if we're using Fortify, which defines
20    __always_inline to do that.  Do nothing otherwise.  This works
21    around a glibc bug whereby 'open' cannot be used as a function
22    pointer when _FORTIFY_SOURCE is positive.  */
23
24 #ifndef __always_inline
25 #define __always_inline
26 #endif
27
28 /* This file is designed to test both open(n,buf[,mode]) and
29    openat(AT_FDCWD,n,buf[,mode]).  FUNC is the function to test.
30    Assumes that BASE and ASSERT are already defined, and that
31    appropriate headers are already included.  If PRINT, warn before
32    skipping symlink tests with status 77.  */
33
34 static int __always_inline
35 test_open (int (*func) (char const *, int, ...), bool print)
36 {
37   int fd;
38   /* Remove anything from prior partial run.  */
39   unlink (BASE "file");
40
41   /* Cannot create directory.  */
42   errno = 0;
43   ASSERT (func ("nonexist.ent/", O_CREAT | O_RDONLY, 0600) == -1);
44   ASSERT (errno == ENOTDIR || errno == EISDIR || errno == ENOENT
45           || errno == EINVAL);
46
47   /* Create a regular file.  */
48   fd = func (BASE "file", O_CREAT | O_RDONLY, 0600);
49   ASSERT (0 <= fd);
50   ASSERT (close (fd) == 0);
51
52   /* Trailing slash handling.  */
53   errno = 0;
54   ASSERT (func (BASE "file/", O_RDONLY) == -1);
55   ASSERT (errno == ENOTDIR || errno == EISDIR || errno == EINVAL);
56
57   /* Directories cannot be opened for writing.  */
58   errno = 0;
59   ASSERT (func (".", O_WRONLY) == -1);
60   ASSERT (errno == EISDIR || errno == EACCES);
61
62   /* /dev/null must exist, and be writable.  */
63   fd = func ("/dev/null", O_RDONLY);
64   ASSERT (0 <= fd);
65   {
66     char c;
67     ASSERT (read (fd, &c, 1) == 0);
68   }
69   ASSERT (close (fd) == 0);
70   fd = func ("/dev/null", O_WRONLY);
71   ASSERT (0 <= fd);
72   ASSERT (write (fd, "c", 1) == 1);
73   ASSERT (close (fd) == 0);
74
75   /* Although O_NONBLOCK on regular files can be ignored, it must not
76      cause a failure.  */
77   fd = func (BASE "file", O_NONBLOCK | O_RDONLY);
78   ASSERT (0 <= fd);
79   ASSERT (close (fd) == 0);
80
81   /* Symlink handling, where supported.  */
82   if (symlink (BASE "file", BASE "link") != 0)
83     {
84       ASSERT (unlink (BASE "file") == 0);
85       if (print)
86         fputs ("skipping test: symlinks not supported on this file system\n",
87                stderr);
88       return 77;
89     }
90   errno = 0;
91   ASSERT (func (BASE "link/", O_RDONLY) == -1);
92   ASSERT (errno == ENOTDIR);
93   fd = func (BASE "link", O_RDONLY);
94   ASSERT (0 <= fd);
95   ASSERT (close (fd) == 0);
96
97   /* Cleanup.  */
98   ASSERT (unlink (BASE "file") == 0);
99   ASSERT (unlink (BASE "link") == 0);
100
101   return 0;
102 }