maint: update copyright
[gnulib.git] / tests / test-fopen.h
1 /* Test of opening a file stream.
2    Copyright (C) 2007-2014 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 <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 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 /* Test fopen.  Assumes BASE is defined.  */
27
28 static int
29 test_fopen (void)
30 {
31   FILE *f;
32   /* Remove anything from prior partial run.  */
33   unlink (BASE "file");
34
35   /* Read requires existing file.  */
36   errno = 0;
37   ASSERT (fopen (BASE "file", "r") == NULL);
38   ASSERT (errno == ENOENT);
39
40   /* Write can create a file.  */
41   f = fopen (BASE "file", "w");
42   ASSERT (f);
43   ASSERT (fclose (f) == 0);
44
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);
49
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);
55
56   /* Directories cannot be opened for writing.  */
57   errno = 0;
58   ASSERT (fopen (".", "w") == NULL);
59   ASSERT (errno == EISDIR || errno == EINVAL || errno == EACCES);
60
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);
68
69   /* Cleanup.  */
70   ASSERT (unlink (BASE "file") == 0);
71
72   return 0;
73 }