1 /* Test of opening a file descriptor.
2 Copyright (C) 2007-2012 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 of the License, or
7 (at your option) any later version.
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, see <http://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
19 /* This file is designed to test both open(n,buf[,mode]) and
20 openat(AT_FDCWD,n,buf[,mode]). FUNC is the function to test.
21 Assumes that BASE and ASSERT are already defined, and that
22 appropriate headers are already included. If PRINT, warn before
23 skipping symlink tests with status 77. */
26 test_open (int (*func
) (char const *, int, ...), bool print
)
29 /* Remove anything from prior partial run. */
32 /* Cannot create directory. */
34 ASSERT (func ("nonexist.ent/", O_CREAT
| O_RDONLY
, 0600) == -1);
35 ASSERT (errno
== ENOTDIR
|| errno
== EISDIR
|| errno
== ENOENT
38 /* Create a regular file. */
39 fd
= func (BASE
"file", O_CREAT
| O_RDONLY
, 0600);
41 ASSERT (close (fd
) == 0);
43 /* Trailing slash handling. */
45 ASSERT (func (BASE
"file/", O_RDONLY
) == -1);
46 ASSERT (errno
== ENOTDIR
|| errno
== EISDIR
|| errno
== EINVAL
);
48 /* Directories cannot be opened for writing. */
50 ASSERT (func (".", O_WRONLY
) == -1);
51 ASSERT (errno
== EISDIR
|| errno
== EACCES
);
53 /* /dev/null must exist, and be writable. */
54 fd
= func ("/dev/null", O_RDONLY
);
58 ASSERT (read (fd
, &c
, 1) == 0);
60 ASSERT (close (fd
) == 0);
61 fd
= func ("/dev/null", O_WRONLY
);
63 ASSERT (write (fd
, "c", 1) == 1);
64 ASSERT (close (fd
) == 0);
66 /* Although O_NONBLOCK on regular files can be ignored, it must not
68 fd
= func (BASE
"file", O_NONBLOCK
| O_RDONLY
);
70 ASSERT (close (fd
) == 0);
72 /* Symlink handling, where supported. */
73 if (symlink (BASE
"file", BASE
"link") != 0)
75 ASSERT (unlink (BASE
"file") == 0);
77 fputs ("skipping test: symlinks not supported on this file system\n",
82 ASSERT (func (BASE
"link/", O_RDONLY
) == -1);
83 ASSERT (errno
== ENOTDIR
);
84 fd
= func (BASE
"link", O_RDONLY
);
86 ASSERT (close (fd
) == 0);
89 ASSERT (unlink (BASE
"file") == 0);
90 ASSERT (unlink (BASE
"link") == 0);