maint.mk: Update system header list for #include syntax checks.
[gnulib.git] / tests / test-fopen.h
blob526736222495b7ea1dea7813751c21acf736f28c
1 /* Test of opening a file stream.
2 Copyright (C) 2007-2024 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 /* Tell GCC not to warn about the specific edge cases tested here. */
27 #if __GNUC__ >= 10
28 # pragma GCC diagnostic ignored "-Wanalyzer-file-leak"
29 #endif
31 /* Test fopen. Assumes BASE is defined. */
33 static int
34 test_fopen (void)
36 FILE *f;
37 /* Remove anything from prior partial run. */
38 unlink (BASE "file");
40 /* Read requires existing file. */
41 errno = 0;
42 ASSERT (fopen (BASE "file", "r") == NULL);
43 ASSERT (errno == ENOENT);
45 /* Write can create a file. */
46 f = fopen (BASE "file", "w");
47 ASSERT (f);
48 ASSERT (fclose (f) == 0);
50 /* Trailing slash is invalid on non-directory. */
51 errno = 0;
52 ASSERT (fopen (BASE "file/", "r") == NULL);
53 ASSERT (errno == ENOTDIR || errno == EISDIR || errno == EINVAL);
55 errno = 0;
56 ASSERT (fopen (BASE "file/", "r+") == NULL);
57 ASSERT (errno == ENOTDIR || errno == EISDIR || errno == EINVAL);
59 /* Cannot create a directory. */
60 errno = 0;
61 ASSERT (fopen ("nonexist.ent/", "w") == NULL);
62 ASSERT (errno == ENOTDIR || errno == EISDIR || errno == ENOENT
63 || errno == EINVAL);
65 /* Directories cannot be opened for writing. */
66 errno = 0;
67 ASSERT (fopen (".", "w") == NULL);
68 ASSERT (errno == EISDIR || errno == EINVAL || errno == EACCES);
70 errno = 0;
71 ASSERT (fopen ("./", "w") == NULL);
72 ASSERT (errno == EISDIR || errno == EINVAL || errno == EACCES);
74 errno = 0;
75 ASSERT (fopen (".", "r+") == NULL);
76 ASSERT (errno == EISDIR || errno == EINVAL || errno == EACCES);
78 errno = 0;
79 ASSERT (fopen ("./", "r+") == NULL);
80 ASSERT (errno == EISDIR || errno == EINVAL || errno == EACCES);
82 /* /dev/null must exist, and be writable. */
83 f = fopen ("/dev/null", "r");
84 ASSERT (f);
85 ASSERT (fclose (f) == 0);
86 f = fopen ("/dev/null", "w");
87 ASSERT (f);
88 ASSERT (fclose (f) == 0);
90 /* Cleanup. */
91 ASSERT (unlink (BASE "file") == 0);
93 return 0;