unlink: new module, for Solaris 9 bug
[pspp] / tests / test-unlink.c
1 /* Tests of unlink.
2    Copyright (C) 2009 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 Eric Blake <ebb9@byu.net>, 2009.  */
18
19 #include <config.h>
20
21 #include <unistd.h>
22
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/stat.h>
29
30 #include "unlinkdir.h"
31
32 #if !HAVE_SYMLINK
33 # define symlink(a,b) (-1)
34 #endif
35
36 #define ASSERT(expr) \
37   do                                                                         \
38     {                                                                        \
39       if (!(expr))                                                           \
40         {                                                                    \
41           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__);  \
42           fflush (stderr);                                                   \
43           abort ();                                                          \
44         }                                                                    \
45     }                                                                        \
46   while (0)
47
48 #define BASE "test-unlink.t"
49
50 int
51 main ()
52 {
53   /* Remove any leftovers from a previous partial run.  */
54   ASSERT (system ("rm -rf " BASE "*") == 0);
55
56   /* Setup.  */
57   ASSERT (mkdir (BASE "dir", 0700) == 0);
58   ASSERT (close (creat (BASE "dir/file", 0600)) == 0);
59
60   /* Basic error conditions.  */
61   errno = 0;
62   ASSERT (unlink ("") == -1);
63   ASSERT (errno == ENOENT);
64   errno = 0;
65   ASSERT (unlink (BASE "nosuch") == -1);
66   ASSERT (errno == ENOENT);
67   errno = 0;
68   ASSERT (unlink (BASE "nosuch/") == -1);
69   ASSERT (errno == ENOENT);
70   /* Resulting errno after directories is rather varied across
71      implementations (EPERM, EINVAL, EACCES, EBUSY, EISDIR, ENOTSUP);
72      however, we must be careful to not attempt unlink on a directory
73      unless we know it must fail.  */
74   if (cannot_unlink_dir ())
75     {
76       ASSERT (unlink (".") == -1);
77       ASSERT (unlink ("..") == -1);
78       ASSERT (unlink ("/") == -1);
79       ASSERT (unlink (BASE "dir") == -1);
80       ASSERT (mkdir (BASE "dir1", 0700) == 0);
81       ASSERT (unlink (BASE "dir1") == -1);
82       ASSERT (rmdir (BASE "dir1") == 0);
83     }
84   errno = 0;
85   ASSERT (unlink (BASE "dir/file/") == -1);
86   ASSERT (errno == ENOTDIR);
87
88   /* Test symlink behavior.  Specifying trailing slash will attempt
89      unlink of a directory, so only attempt it if we know it must
90      fail.  */
91   if (symlink (BASE "dir", BASE "link") != 0)
92     {
93       ASSERT (unlink (BASE "dir/file") == 0);
94       ASSERT (rmdir (BASE "dir") == 0);
95       fputs ("skipping test: symlinks not supported on this filesystem\n",
96              stderr);
97       return 77;
98     }
99   if (cannot_unlink_dir ())
100     ASSERT (unlink (BASE "link/") == -1);
101   ASSERT (unlink (BASE "link") == 0);
102   ASSERT (symlink (BASE "dir/file", BASE "link") == 0);
103   /* Order here proves unlink of a symlink does not follow through to
104      the file.  */
105   ASSERT (unlink (BASE "link") == 0);
106   ASSERT (unlink (BASE "dir/file") == 0);
107   ASSERT (rmdir (BASE "dir") == 0);
108
109   return 0;
110 }