Branch data Line data Source code
1 : : /* Test of lstat() function.
2 : : Copyright (C) 2008-2012 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 Simon Josefsson, 2008; and Eric Blake, 2009. */
18 : :
19 : : /* This file is designed to test both lstat(n,buf) and
20 : : fstatat(AT_FDCWD,n,buf,AT_SYMLINK_NOFOLLOW). FUNC is the function
21 : : to test. Assumes that BASE and ASSERT are already defined, and
22 : : that appropriate headers are already included. If PRINT, warn
23 : : before skipping symlink tests with status 77. */
24 : :
25 : : static int
26 : 1 : test_lstat_func (int (*func) (char const *, struct stat *), bool print)
27 : : {
28 : : struct stat st1;
29 : : struct stat st2;
30 : :
31 : : /* Test for common directories. */
32 [ - + ]: 1 : ASSERT (func (".", &st1) == 0);
33 [ - + ]: 1 : ASSERT (func ("./", &st2) == 0);
34 [ + - ][ - + ]: 1 : ASSERT (SAME_INODE (st1, st2));
35 [ - + ]: 1 : ASSERT (S_ISDIR (st1.st_mode));
36 [ - + ]: 1 : ASSERT (S_ISDIR (st2.st_mode));
37 [ - + ]: 1 : ASSERT (func ("/", &st1) == 0);
38 [ - + ]: 1 : ASSERT (func ("///", &st2) == 0);
39 [ + - ][ - + ]: 1 : ASSERT (SAME_INODE (st1, st2));
40 [ - + ]: 1 : ASSERT (S_ISDIR (st1.st_mode));
41 [ - + ]: 1 : ASSERT (S_ISDIR (st2.st_mode));
42 [ - + ]: 1 : ASSERT (func ("..", &st1) == 0);
43 [ - + ]: 1 : ASSERT (S_ISDIR (st1.st_mode));
44 : :
45 : : /* Test for error conditions. */
46 : 1 : errno = 0;
47 [ - + ]: 1 : ASSERT (func ("", &st1) == -1);
48 [ - + ]: 1 : ASSERT (errno == ENOENT);
49 : 1 : errno = 0;
50 [ - + ]: 1 : ASSERT (func ("nosuch", &st1) == -1);
51 [ - + ]: 1 : ASSERT (errno == ENOENT);
52 : 1 : errno = 0;
53 [ - + ]: 1 : ASSERT (func ("nosuch/", &st1) == -1);
54 [ - + ]: 1 : ASSERT (errno == ENOENT);
55 : :
56 [ - + ]: 1 : ASSERT (close (creat (BASE "file", 0600)) == 0);
57 [ - + ]: 1 : ASSERT (func (BASE "file", &st1) == 0);
58 [ - + ]: 1 : ASSERT (S_ISREG (st1.st_mode));
59 : 1 : errno = 0;
60 [ - + ]: 1 : ASSERT (func (BASE "file/", &st1) == -1);
61 [ - + ]: 1 : ASSERT (errno == ENOTDIR);
62 : :
63 : : /* Now for some symlink tests, where supported. We set up:
64 : : link1 -> directory
65 : : link2 -> file
66 : : link3 -> dangling
67 : : link4 -> loop
68 : : then test behavior both with and without trailing slash.
69 : : */
70 [ - + ]: 1 : if (symlink (".", BASE "link1") != 0)
71 : : {
72 [ # # ]: 0 : ASSERT (unlink (BASE "file") == 0);
73 [ # # ]: 0 : if (print)
74 : 0 : fputs ("skipping test: symlinks not supported on this file system\n",
75 : : stderr);
76 : 0 : return 77;
77 : : }
78 [ - + ]: 1 : ASSERT (symlink (BASE "file", BASE "link2") == 0);
79 [ - + ]: 1 : ASSERT (symlink (BASE "nosuch", BASE "link3") == 0);
80 [ - + ]: 1 : ASSERT (symlink (BASE "link4", BASE "link4") == 0);
81 : :
82 [ - + ]: 1 : ASSERT (func (BASE "link1", &st1) == 0);
83 [ - + ]: 1 : ASSERT (S_ISLNK (st1.st_mode));
84 [ - + ]: 1 : ASSERT (func (BASE "link1/", &st1) == 0);
85 [ - + ]: 1 : ASSERT (stat (BASE "link1", &st2) == 0);
86 [ - + ]: 1 : ASSERT (S_ISDIR (st1.st_mode));
87 [ - + ]: 1 : ASSERT (S_ISDIR (st2.st_mode));
88 [ + - ][ - + ]: 1 : ASSERT (SAME_INODE (st1, st2));
89 : :
90 [ - + ]: 1 : ASSERT (func (BASE "link2", &st1) == 0);
91 [ - + ]: 1 : ASSERT (S_ISLNK (st1.st_mode));
92 : 1 : errno = 0;
93 [ - + ]: 1 : ASSERT (func (BASE "link2/", &st1) == -1);
94 [ - + ]: 1 : ASSERT (errno == ENOTDIR);
95 : :
96 [ - + ]: 1 : ASSERT (func (BASE "link3", &st1) == 0);
97 [ - + ]: 1 : ASSERT (S_ISLNK (st1.st_mode));
98 : 1 : errno = 0;
99 [ - + ]: 1 : ASSERT (func (BASE "link3/", &st1) == -1);
100 [ - + ]: 1 : ASSERT (errno == ENOENT);
101 : :
102 [ - + ]: 1 : ASSERT (func (BASE "link4", &st1) == 0);
103 [ - + ]: 1 : ASSERT (S_ISLNK (st1.st_mode));
104 : 1 : errno = 0;
105 [ - + ]: 1 : ASSERT (func (BASE "link4/", &st1) == -1);
106 [ - + ]: 1 : ASSERT (errno == ELOOP);
107 : :
108 : : /* Cleanup. */
109 [ - + ]: 1 : ASSERT (unlink (BASE "file") == 0);
110 [ - + ]: 1 : ASSERT (unlink (BASE "link1") == 0);
111 [ - + ]: 1 : ASSERT (unlink (BASE "link2") == 0);
112 [ - + ]: 1 : ASSERT (unlink (BASE "link3") == 0);
113 [ - + ]: 1 : ASSERT (unlink (BASE "link4") == 0);
114 : :
115 : 1 : return 0;
116 : : }
|