Line data Source code
1 : /* Tests of symlink.
2 : Copyright (C) 2009-2020 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 <https://www.gnu.org/licenses/>. */
16 :
17 : /* Written by Eric Blake <ebb9@byu.net>, 2009. */
18 :
19 : /* This file is designed to test both symlink(a,b) and
20 : symlinkat(a,AT_FDCWD,b). FUNC is the function to test. Assumes
21 : that BASE and ASSERT are already defined, and that appropriate
22 : headers are already included. If PRINT, warn before skipping
23 : symlink tests with status 77. */
24 :
25 : static int
26 1 : test_symlink (int (*func) (char const *, char const *), bool print)
27 : {
28 1 : if (func ("nowhere", BASE "link1"))
29 : {
30 0 : if (print)
31 0 : fputs ("skipping test: symlinks not supported on this file system\n",
32 : stderr);
33 0 : return 77;
34 : }
35 :
36 : /* Some systems allow the creation of 0-length symlinks as a synonym
37 : for "."; but most reject it. */
38 : {
39 : int status;
40 1 : errno = 0;
41 1 : status = func ("", BASE "link2");
42 1 : if (status == -1)
43 1 : ASSERT (errno == ENOENT || errno == EINVAL);
44 : else
45 : {
46 0 : ASSERT (status == 0);
47 0 : ASSERT (unlink (BASE "link2") == 0);
48 : }
49 : }
50 :
51 : /* Sanity checks of failures. */
52 1 : errno = 0;
53 1 : ASSERT (func ("nowhere", "") == -1);
54 1 : ASSERT (errno == ENOENT);
55 1 : errno = 0;
56 1 : ASSERT (func ("nowhere", ".") == -1);
57 1 : ASSERT (errno == EEXIST || errno == EINVAL);
58 1 : errno = 0;
59 1 : ASSERT (func ("somewhere", BASE "link1") == -1);
60 1 : ASSERT (errno == EEXIST);
61 1 : errno = 0;
62 1 : ASSERT (func ("nowhere", BASE "link2/") == -1);
63 1 : ASSERT (errno == ENOTDIR || errno == ENOENT);
64 1 : ASSERT (mkdir (BASE "dir", 0700) == 0);
65 1 : errno = 0;
66 1 : ASSERT (func ("nowhere", BASE "dir") == -1);
67 1 : ASSERT (errno == EEXIST);
68 1 : errno = 0;
69 1 : ASSERT (func ("nowhere", BASE "dir/") == -1);
70 1 : ASSERT (errno == EEXIST || errno == EINVAL
71 : || errno == ENOENT /* Lustre FS on Linux */);
72 1 : ASSERT (close (creat (BASE "file", 0600)) == 0);
73 1 : errno = 0;
74 1 : ASSERT (func ("nowhere", BASE "file") == -1);
75 1 : ASSERT (errno == EEXIST);
76 1 : errno = 0;
77 1 : ASSERT (func ("nowhere", BASE "file/") == -1);
78 1 : ASSERT (errno == EEXIST || errno == ENOTDIR || errno == ENOENT);
79 :
80 : /* Trailing slash must always be rejected. */
81 1 : ASSERT (unlink (BASE "link1") == 0);
82 1 : ASSERT (func (BASE "link2", BASE "link1") == 0);
83 1 : errno = 0;
84 1 : ASSERT (func (BASE "nowhere", BASE "link1/") == -1);
85 1 : ASSERT (errno == EEXIST || errno == ENOTDIR || errno == ENOENT);
86 1 : errno = 0;
87 1 : ASSERT (unlink (BASE "link2") == -1);
88 1 : ASSERT (errno == ENOENT);
89 :
90 : /* Cleanup. */
91 1 : ASSERT (rmdir (BASE "dir") == 0);
92 1 : ASSERT (unlink (BASE "file") == 0);
93 1 : ASSERT (unlink (BASE "link1") == 0);
94 :
95 1 : return 0;
96 : }
|