Added gnutls_x509_crt_set_policy()
[gnutls.git] / gl / tests / test-open.h
blobcab1d27d9bd63aa17f879f0d4e5f678fed17a6a4
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. */
25 static int
26 test_open (int (*func) (char const *, int, ...), bool print)
28 int fd;
29 /* Remove anything from prior partial run. */
30 unlink (BASE "file");
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);
38 /* Create a regular file. */
39 fd = func (BASE "file", O_CREAT | O_RDONLY, 0600);
40 ASSERT (0 <= fd);
41 ASSERT (close (fd) == 0);
43 /* Trailing slash handling. */
44 errno = 0;
45 ASSERT (func (BASE "file/", O_RDONLY) == -1);
46 ASSERT (errno == ENOTDIR || errno == EISDIR || errno == EINVAL);
48 /* Directories cannot be opened for writing. */
49 errno = 0;
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);
55 ASSERT (0 <= fd);
57 char c;
58 ASSERT (read (fd, &c, 1) == 0);
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);
66 /* Although O_NONBLOCK on regular files can be ignored, it must not
67 cause a failure. */
68 fd = func (BASE "file", O_NONBLOCK | O_RDONLY);
69 ASSERT (0 <= fd);
70 ASSERT (close (fd) == 0);
72 /* Symlink handling, where supported. */
73 if (symlink (BASE "file", BASE "link") != 0)
75 ASSERT (unlink (BASE "file") == 0);
76 if (print)
77 fputs ("skipping test: symlinks not supported on this file system\n",
78 stderr);
79 return 77;
81 errno = 0;
82 ASSERT (func (BASE "link/", O_RDONLY) == -1);
83 ASSERT (errno == ENOTDIR);
84 fd = func (BASE "link", O_RDONLY);
85 ASSERT (0 <= fd);
86 ASSERT (close (fd) == 0);
88 /* Cleanup. */
89 ASSERT (unlink (BASE "file") == 0);
90 ASSERT (unlink (BASE "link") == 0);
92 return 0;