| 1 |
/* Test of opening a file stream. |
| 2 |
Copyright (C) 2007-2025 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 <https://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
/* Written by Bruno Haible <[email protected]>, 2007. */ |
| 18 |
|
| 19 |
/* Include <config.h> and a form of <stdio.h> first. */ |
| 20 |
|
| 21 |
#include <errno.h> |
| 22 |
#include <unistd.h> |
| 23 |
|
| 24 |
#include "macros.h" |
| 25 |
|
| 26 |
/* Tell GCC not to warn about the specific edge cases tested here. */ |
| 27 |
#if _GL_GNUC_PREREQ (10, 0) |
| 28 |
# pragma GCC diagnostic ignored "-Wanalyzer-file-leak" |
| 29 |
#endif |
| 30 |
|
| 31 |
/* Test fopen. Assumes BASE is defined. */ |
| 32 |
|
| 33 |
static int |
| 34 |
test_fopen (void) |
| 35 |
{ |
| 36 |
FILE *f; |
| 37 |
/* Remove anything from prior partial run. */ |
| 38 |
unlink (BASE "file"); |
| 39 |
|
| 40 |
/* Read requires existing file. */ |
| 41 |
errno = 0; |
| 42 |
ASSERT (fopen (BASE "file", "r") == NULL); |
| 43 |
ASSERT (errno == ENOENT); |
| 44 |
|
| 45 |
/* Write can create a file. */ |
| 46 |
f = fopen (BASE "file", "w"); |
| 47 |
ASSERT (f); |
| 48 |
ASSERT (fclose (f) == 0); |
| 49 |
|
| 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); |
| 54 |
|
| 55 |
errno = 0; |
| 56 |
ASSERT (fopen (BASE "file/", "r+") == NULL); |
| 57 |
ASSERT (errno == ENOTDIR || errno == EISDIR || errno == EINVAL); |
| 58 |
|
| 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); |
| 64 |
|
| 65 |
/* Directories cannot be opened for writing. */ |
| 66 |
errno = 0; |
| 67 |
ASSERT (fopen (".", "w") == NULL); |
| 68 |
ASSERT (errno == EISDIR || errno == EINVAL || errno == EACCES); |
| 69 |
|
| 70 |
errno = 0; |
| 71 |
ASSERT (fopen ("./", "w") == NULL); |
| 72 |
ASSERT (errno == EISDIR || errno == EINVAL || errno == EACCES); |
| 73 |
|
| 74 |
errno = 0; |
| 75 |
ASSERT (fopen (".", "r+") == NULL); |
| 76 |
ASSERT (errno == EISDIR || errno == EINVAL || errno == EACCES); |
| 77 |
|
| 78 |
errno = 0; |
| 79 |
ASSERT (fopen ("./", "r+") == NULL); |
| 80 |
ASSERT (errno == EISDIR || errno == EINVAL || errno == EACCES); |
| 81 |
|
| 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); |
| 89 |
|
| 90 |
/* Cleanup. */ |
| 91 |
ASSERT (unlink (BASE "file") == 0); |
| 92 |
|
| 93 |
return 0; |
| 94 |
} |