1 /* Test of opening a file stream.
2 Copyright (C) 2007-2009 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. */
25 #define ASSERT(expr) \
30 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
37 /* Test fopen. Assumes BASE is defined. */
43 /* Remove anything from prior partial run. */
46 /* Read requires existing file. */
48 ASSERT (fopen (BASE "file", "r") == NULL);
49 ASSERT (errno == ENOENT);
51 /* Write can create a file. */
52 f = fopen (BASE "file", "w");
54 ASSERT (fclose (f) == 0);
56 /* Trailing slash is invalid on non-directory. */
58 ASSERT (fopen (BASE "file/", "r") == NULL);
59 ASSERT (errno == ENOTDIR || errno == EISDIR);
61 /* Cannot create a directory. */
63 ASSERT (fopen ("nonexist.ent/", "w") == NULL);
64 ASSERT (errno == ENOTDIR || errno == EISDIR || errno == ENOENT);
66 /* Directories cannot be opened for writing. */
68 ASSERT (fopen (".", "w") == NULL);
69 ASSERT (errno == EISDIR || errno == EINVAL);
71 /* /dev/null must exist, and be writable. */
72 f = fopen ("/dev/null", "r");
74 ASSERT (fclose (f) == 0);
75 f = fopen ("/dev/null", "w");
77 ASSERT (fclose (f) == 0);
80 ASSERT (unlink (BASE "file") == 0);