1 /* Test of opening a file stream.
2 Copyright (C) 2007-2011 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 /* Include <config.h> and a form of <stdio.h> first. */
26 /* Test fopen. Assumes BASE is defined. */
32 /* Remove anything from prior partial run. */
35 /* Read requires existing file. */
37 ASSERT (fopen (BASE
"file", "r") == NULL
);
38 ASSERT (errno
== ENOENT
);
40 /* Write can create a file. */
41 f
= fopen (BASE
"file", "w");
43 ASSERT (fclose (f
) == 0);
45 /* Trailing slash is invalid on non-directory. */
47 ASSERT (fopen (BASE
"file/", "r") == NULL
);
48 ASSERT (errno
== ENOTDIR
|| errno
== EISDIR
|| errno
== EINVAL
);
50 /* Cannot create a directory. */
52 ASSERT (fopen ("nonexist.ent/", "w") == NULL
);
53 ASSERT (errno
== ENOTDIR
|| errno
== EISDIR
|| errno
== ENOENT
56 /* Directories cannot be opened for writing. */
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");
64 ASSERT (fclose (f
) == 0);
65 f
= fopen ("/dev/null", "w");
67 ASSERT (fclose (f
) == 0);
70 ASSERT (unlink (BASE
"file") == 0);