symlinkat: new module
[gnulib.git] / tests / test-symlinkat.c
1 /* Tests of symlinkat and readlinkat.
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 #ifndef HAVE_SYMLINK
31 # define HAVE_SYMLINK 0
32 #endif
33
34 #define ASSERT(expr) \
35   do                                                                         \
36     {                                                                        \
37       if (!(expr))                                                           \
38         {                                                                    \
39           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__);  \
40           fflush (stderr);                                                   \
41           abort ();                                                          \
42         }                                                                    \
43     }                                                                        \
44   while (0)
45
46 int
47 main ()
48 {
49   char buf[80];
50
51   /* Create handle for future use.  */
52   int dfd = openat (AT_FDCWD, ".", O_RDONLY);
53   ASSERT (0 <= dfd);
54
55   /* Sanity checks of failures.  Mingw lacks symlinkat, but readlinkat
56      can still distinguish between various errors.  */
57   errno = 0;
58   ASSERT (readlinkat (AT_FDCWD, "no_such", buf, sizeof buf) == -1);
59   ASSERT (errno == ENOENT);
60   errno = 0;
61   ASSERT (readlinkat (dfd, "no_such", buf, sizeof buf) == -1);
62   ASSERT (errno == ENOENT);
63   errno = 0;
64   ASSERT (readlinkat (AT_FDCWD, "", buf, sizeof buf) == -1);
65   ASSERT (errno == ENOENT);
66   errno = 0;
67   ASSERT (readlinkat (dfd, "", buf, sizeof buf) == -1);
68   ASSERT (errno == ENOENT);
69   errno = 0;
70   ASSERT (readlinkat (AT_FDCWD, ".", buf, sizeof buf) == -1);
71   ASSERT (errno == EINVAL);
72   errno = 0;
73   ASSERT (readlinkat (dfd, ".", buf, sizeof buf) == -1);
74   ASSERT (errno == EINVAL);
75   errno = 0;
76   ASSERT (symlinkat ("who cares", AT_FDCWD, "") == -1);
77   ASSERT (errno == ENOENT || errno == ENOSYS);
78   errno = 0;
79   ASSERT (symlinkat ("who cares", dfd, "") == -1);
80   ASSERT (errno == ENOENT || errno == ENOSYS);
81
82   /* Skip everything else on mingw.  */
83   if (HAVE_SYMLINK)
84     {
85       const char *linkname = "test-symlinkat.link";
86       const char *contents = "don't matter!";
87       int exp = strlen (contents);
88
89       /* Create link while cwd is '.', then read it in '..'.  */
90       ASSERT (symlinkat (contents, AT_FDCWD, linkname) == 0);
91       errno = 0;
92       ASSERT (symlinkat (contents, dfd, linkname) == -1);
93       ASSERT (errno == EEXIST);
94       ASSERT (chdir ("..") == 0);
95       errno = 0;
96       ASSERT (readlinkat (AT_FDCWD, linkname, buf, sizeof buf) == -1);
97       ASSERT (errno == ENOENT);
98       ASSERT (readlinkat (dfd, linkname, buf, sizeof buf) == exp);
99       ASSERT (strncmp (contents, buf, exp) == 0);
100       ASSERT (unlinkat (dfd, linkname, 0) == 0);
101
102       /* Create link while cwd is '..', then read it in '.'.  */
103       ASSERT (symlinkat (contents, dfd, linkname) == 0);
104       ASSERT (fchdir (dfd) == 0);
105       errno = 0;
106       ASSERT (symlinkat (contents, AT_FDCWD, linkname) == -1);
107       ASSERT (errno == EEXIST);
108       buf[0] = '\0';
109       ASSERT (readlinkat (AT_FDCWD, linkname, buf, sizeof buf) == exp);
110       ASSERT (strncmp (contents, buf, exp) == 0);
111       buf[0] = '\0';
112       ASSERT (readlinkat (dfd, linkname, buf, sizeof buf) == exp);
113       ASSERT (strncmp (contents, buf, exp) == 0);
114       ASSERT (unlink (linkname) == 0);
115     }
116
117   ASSERT (close (dfd) == 0);
118
119   return 0;
120 }