symlink: new module, for Solaris 9 bug
[pspp] / tests / test-symlink.c
1 /* Tests of symlink.
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 <sys/stat.h>
28
29 #define ASSERT(expr) \
30   do                                                                         \
31     {                                                                        \
32       if (!(expr))                                                           \
33         {                                                                    \
34           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__);  \
35           fflush (stderr);                                                   \
36           abort ();                                                          \
37         }                                                                    \
38     }                                                                        \
39   while (0)
40
41 #define BASE "test-symlink.t"
42
43 int
44 main ()
45 {
46   /* Remove any leftovers from a previous partial run.  */
47   ASSERT (system ("rm -rf " BASE "*") == 0);
48
49   if (symlink ("nowhere", BASE "link1"))
50     {
51       fputs ("skipping test: symlinks not supported on this filesystem\n",
52              stderr);
53       return 77;
54     }
55
56   /* Some systems allow the creation of 0-length symlinks as a synonym
57      for "."; but most reject it.  */
58   errno = 0;
59   if (symlink ("", BASE "link2") == -1)
60     ASSERT (errno == ENOENT || errno == EINVAL);
61   else
62     ASSERT (unlink (BASE "link2") == 0);
63
64   /* Sanity checks of failures.  */
65   errno = 0;
66   ASSERT (symlink ("nowhere", "") == -1);
67   ASSERT (errno == ENOENT);
68   errno = 0;
69   ASSERT (symlink ("nowhere", ".") == -1);
70   ASSERT (errno == EEXIST || errno == EINVAL);
71   errno = 0;
72   ASSERT (symlink ("somewhere", BASE "link1") == -1);
73   ASSERT (errno == EEXIST);
74   errno = 0;
75   ASSERT (symlink ("nowhere", BASE "link2/") == -1);
76   ASSERT (errno == ENOTDIR || errno == ENOENT);
77   ASSERT (mkdir (BASE "dir", 0700) == 0);
78   errno = 0;
79   ASSERT (symlink ("nowhere", BASE "dir") == -1);
80   ASSERT (errno == EEXIST);
81   errno = 0;
82   ASSERT (symlink ("nowhere", BASE "dir/") == -1);
83   ASSERT (errno == EEXIST);
84   ASSERT (close (creat (BASE "file", 0600)) == 0);
85   errno = 0;
86   ASSERT (symlink ("nowhere", BASE "file") == -1);
87   ASSERT (errno == EEXIST);
88   errno = 0;
89   ASSERT (symlink ("nowhere", BASE "file/") == -1);
90   ASSERT (errno == EEXIST || errno == ENOTDIR);
91
92   ASSERT (rmdir (BASE "dir") == 0);
93   ASSERT (unlink (BASE "file") == 0);
94   ASSERT (unlink (BASE "link1") == 0);
95
96   return 0;
97 }