Branch data Line data Source code
1 : : /* Test of opening a file descriptor.
2 : : Copyright (C) 2007-2012 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 : : /* 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. */
24 : :
25 : : static int
26 : 1 : test_open (int (*func) (char const *, int, ...), bool print)
27 : : {
28 : : int fd;
29 : : /* Remove anything from prior partial run. */
30 : 1 : unlink (BASE "file");
31 : :
32 : : /* Cannot create directory. */
33 : 1 : errno = 0;
34 [ - + ]: 1 : ASSERT (func ("nonexist.ent/", O_CREAT | O_RDONLY, 0600) == -1);
35 [ + - ][ - + ]: 1 : ASSERT (errno == ENOTDIR || errno == EISDIR || errno == ENOENT
[ # # ][ # # ]
36 : : || errno == EINVAL);
37 : :
38 : : /* Create a regular file. */
39 : 1 : fd = func (BASE "file", O_CREAT | O_RDONLY, 0600);
40 [ - + ]: 1 : ASSERT (0 <= fd);
41 [ - + ]: 1 : ASSERT (close (fd) == 0);
42 : :
43 : : /* Trailing slash handling. */
44 : 1 : errno = 0;
45 [ - + ]: 1 : ASSERT (func (BASE "file/", O_RDONLY) == -1);
46 [ - + ][ # # ]: 1 : ASSERT (errno == ENOTDIR || errno == EISDIR || errno == EINVAL);
[ # # ]
47 : :
48 : : /* Directories cannot be opened for writing. */
49 : 1 : errno = 0;
50 [ - + ]: 1 : ASSERT (func (".", O_WRONLY) == -1);
51 [ - + ][ # # ]: 1 : ASSERT (errno == EISDIR || errno == EACCES);
52 : :
53 : : /* /dev/null must exist, and be writable. */
54 : 1 : fd = func ("/dev/null", O_RDONLY);
55 [ - + ]: 1 : ASSERT (0 <= fd);
56 : : {
57 : : char c;
58 [ - + ]: 1 : ASSERT (read (fd, &c, 1) == 0);
59 : : }
60 [ - + ]: 1 : ASSERT (close (fd) == 0);
61 : 1 : fd = func ("/dev/null", O_WRONLY);
62 [ - + ]: 1 : ASSERT (0 <= fd);
63 [ - + ]: 1 : ASSERT (write (fd, "c", 1) == 1);
64 [ - + ]: 1 : ASSERT (close (fd) == 0);
65 : :
66 : : /* Although O_NONBLOCK on regular files can be ignored, it must not
67 : : cause a failure. */
68 : 1 : fd = func (BASE "file", O_NONBLOCK | O_RDONLY);
69 [ - + ]: 1 : ASSERT (0 <= fd);
70 [ - + ]: 1 : ASSERT (close (fd) == 0);
71 : :
72 : : /* Symlink handling, where supported. */
73 [ - + ]: 1 : if (symlink (BASE "file", BASE "link") != 0)
74 : : {
75 [ # # ]: 0 : ASSERT (unlink (BASE "file") == 0);
76 [ # # ]: 0 : if (print)
77 : 0 : fputs ("skipping test: symlinks not supported on this file system\n",
78 : : stderr);
79 : 0 : return 77;
80 : : }
81 : 1 : errno = 0;
82 [ - + ]: 1 : ASSERT (func (BASE "link/", O_RDONLY) == -1);
83 [ - + ]: 1 : ASSERT (errno == ENOTDIR);
84 : 1 : fd = func (BASE "link", O_RDONLY);
85 [ - + ]: 1 : ASSERT (0 <= fd);
86 [ - + ]: 1 : ASSERT (close (fd) == 0);
87 : :
88 : : /* Cleanup. */
89 [ - + ]: 1 : ASSERT (unlink (BASE "file") == 0);
90 [ - + ]: 1 : ASSERT (unlink (BASE "link") == 0);
91 : :
92 : 1 : return 0;
93 : : }
|