dosname: Change IS_RELATIVE_FILE_NAME.
[gnulib.git] / tests / test-fopen.h
blobfb036af6b72e15399a20236d7dc5951999701010
1 /* Test of opening a file stream.
2 Copyright (C) 2007-2020 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 <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
19 /* Include <config.h> and a form of <stdio.h> first. */
21 #include <errno.h>
22 #include <unistd.h>
24 #include "macros.h"
26 /* Test fopen. Assumes BASE is defined. */
28 static int
29 test_fopen (void)
31 FILE *f;
32 /* Remove anything from prior partial run. */
33 unlink (BASE "file");
35 /* Read requires existing file. */
36 errno = 0;
37 ASSERT (fopen (BASE "file", "r") == NULL);
38 ASSERT (errno == ENOENT);
40 /* Write can create a file. */
41 f = fopen (BASE "file", "w");
42 ASSERT (f);
43 ASSERT (fclose (f) == 0);
45 /* Trailing slash is invalid on non-directory. */
46 errno = 0;
47 ASSERT (fopen (BASE "file/", "r") == NULL);
48 ASSERT (errno == ENOTDIR || errno == EISDIR || errno == EINVAL);
50 /* Cannot create a directory. */
51 errno = 0;
52 ASSERT (fopen ("nonexist.ent/", "w") == NULL);
53 ASSERT (errno == ENOTDIR || errno == EISDIR || errno == ENOENT
54 || errno == EINVAL);
56 /* Directories cannot be opened for writing. */
57 errno = 0;
58 ASSERT (fopen (".", "w") == NULL);
59 ASSERT (errno == EISDIR || errno == EINVAL || errno == EACCES);
61 /* /dev/null must exist, and be writable. */
62 f = fopen ("/dev/null", "r");
63 ASSERT (f);
64 ASSERT (fclose (f) == 0);
65 f = fopen ("/dev/null", "w");
66 ASSERT (f);
67 ASSERT (fclose (f) == 0);
69 /* Cleanup. */
70 ASSERT (unlink (BASE "file") == 0);
72 return 0;