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