This source file includes following definitions.
- test_readlink
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 static int
26 test_readlink (ssize_t (*func) (char const *, char *, size_t), bool print)
27 {
28 char buf[80];
29
30
31
32 memset (buf, 0xff, sizeof buf);
33 errno = 0;
34 ASSERT (func ("no_such", buf, sizeof buf) == -1);
35 ASSERT (errno == ENOENT);
36 errno = 0;
37 ASSERT (func ("no_such/", buf, sizeof buf) == -1);
38 ASSERT (errno == ENOENT);
39 errno = 0;
40 ASSERT (func ("", buf, sizeof buf) == -1);
41 ASSERT (errno == ENOENT || errno == EINVAL);
42 errno = 0;
43 ASSERT (func (".", buf, sizeof buf) == -1);
44 ASSERT (errno == EINVAL);
45 errno = 0;
46 ASSERT (func ("./", buf, sizeof buf) == -1);
47 ASSERT (errno == EINVAL);
48 ASSERT (close (creat (BASE "file", 0600)) == 0);
49 errno = 0;
50 ASSERT (func (BASE "file", buf, sizeof buf) == -1);
51 ASSERT (errno == EINVAL);
52 errno = 0;
53 ASSERT (func (BASE "file/", buf, sizeof buf) == -1);
54 ASSERT (errno == ENOTDIR || errno == EINVAL);
55
56
57 if (symlink (BASE "dir", BASE "link"))
58 {
59 ASSERT (unlink (BASE "file") == 0);
60 if (print)
61 fputs ("skipping test: symlinks not supported on this file system\n",
62 stderr);
63 return 77;
64 }
65 ASSERT (mkdir (BASE "dir", 0700) == 0);
66 errno = 0;
67 ASSERT (func (BASE "link/", buf, sizeof buf) == -1);
68 ASSERT (errno == EINVAL);
69 ASSERT (symlink (BASE "link", BASE "link2") == 0);
70 errno = 0;
71 ASSERT (func (BASE "link2/", buf, sizeof buf) == -1);
72 ASSERT (errno == EINVAL);
73 ASSERT (unlink (BASE "link2") == 0);
74 ASSERT (symlink (BASE "file", BASE "link2") == 0);
75 errno = 0;
76 ASSERT (func (BASE "link2/", buf, sizeof buf) == -1);
77 ASSERT (errno == ENOTDIR || errno == EINVAL);
78 ASSERT (unlink (BASE "file") == 0);
79 ASSERT (unlink (BASE "link2") == 0);
80 {
81
82
83 int i;
84 for (i = 0; i < sizeof buf; i++)
85 ASSERT (buf[i] == (char) 0xff);
86 }
87 {
88 size_t len = strlen (BASE "dir");
89
90
91
92 ssize_t result;
93 errno = 0;
94 result = readlink (BASE "link", buf, 1);
95 if (result == -1)
96 {
97 ASSERT (errno == ERANGE);
98 ASSERT (buf[0] == (char) 0xff);
99 }
100 else
101 {
102 ASSERT (result == 1);
103 ASSERT (buf[0] == BASE[0]);
104 }
105 ASSERT (buf[1] == (char) 0xff);
106 ASSERT (func (BASE "link", buf, len) == len);
107 ASSERT (strncmp (buf, BASE "dir", len) == 0);
108 ASSERT (buf[len] == (char) 0xff);
109 ASSERT (func (BASE "link", buf, sizeof buf) == len);
110 ASSERT (strncmp (buf, BASE "dir", len) == 0);
111
112
113 ASSERT (buf[len] == '\0' || buf[len] == (char) 0xff);
114 }
115 ASSERT (rmdir (BASE "dir") == 0);
116 ASSERT (unlink (BASE "link") == 0);
117
118 return 0;
119 }