summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/sys/t_link.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/regress/lib/libc/sys/t_link.c')
-rw-r--r--src/regress/lib/libc/sys/t_link.c234
1 files changed, 234 insertions, 0 deletions
diff --git a/src/regress/lib/libc/sys/t_link.c b/src/regress/lib/libc/sys/t_link.c
new file mode 100644
index 0000000000..291fd9cda4
--- /dev/null
+++ b/src/regress/lib/libc/sys/t_link.c
@@ -0,0 +1,234 @@
1/* $OpenBSD: t_link.c,v 1.1.1.1 2019/11/19 19:57:03 bluhm Exp $ */
2/* $NetBSD: t_link.c,v 1.3 2017/01/13 20:42:36 christos Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/cdefs.h>
36__RCSID("$NetBSD: t_link.c,v 1.3 2017/01/13 20:42:36 christos Exp $");
37
38#include <sys/param.h>
39#include <sys/stat.h>
40
41#include "atf-c.h"
42#include <errno.h>
43#include <fcntl.h>
44#include <limits.h>
45#include <stdio.h>
46#include <string.h>
47#include <unistd.h>
48
49static const char *getpath(void);
50static char path[] = "link";
51static const char *pathl;
52
53static const char *
54getpath(void)
55{
56 static char buf[LINE_MAX];
57
58 (void)memset(buf, '\0', sizeof(buf));
59
60 if (getcwd(buf, sizeof(buf)) == NULL)
61 return NULL;
62
63 (void)strlcat(buf, path, sizeof(buf));
64 (void)strlcat(buf, ".link", sizeof(buf));
65
66 return buf;
67}
68
69ATF_TC_WITH_CLEANUP(link_count);
70ATF_TC_HEAD(link_count, tc)
71{
72 atf_tc_set_md_var(tc, "descr", "link(2) counts are incremented?");
73}
74
75ATF_TC_BODY(link_count, tc)
76{
77 struct stat sa, sb;
78 int fd;
79
80 (void)memset(&sa, 0, sizeof(struct stat));
81 (void)memset(&sb, 0, sizeof(struct stat));
82
83 pathl = getpath();
84 fd = open(path, O_RDWR | O_CREAT, 0600);
85
86 ATF_REQUIRE(fd >= 0);
87 ATF_REQUIRE(pathl != NULL);
88
89 ATF_REQUIRE(stat(path, &sa) == 0);
90 ATF_REQUIRE(link(path, pathl) == 0);
91 ATF_REQUIRE(stat(path, &sb) == 0);
92
93 if (sa.st_nlink != sb.st_nlink - 1)
94 atf_tc_fail("incorrect link(2) count");
95
96 ATF_REQUIRE(close(fd) == 0);
97 ATF_REQUIRE(unlink(path) == 0);
98 ATF_REQUIRE(unlink(pathl) == 0);
99}
100
101ATF_TC_CLEANUP(link_count, tc)
102{
103 (void)unlink(path);
104 (void)unlink(pathl);
105}
106
107ATF_TC_WITH_CLEANUP(link_err);
108ATF_TC_HEAD(link_err, tc)
109{
110 atf_tc_set_md_var(tc, "descr", "Test error conditions of link(2)");
111}
112
113ATF_TC_BODY(link_err, tc)
114{
115 char buf[MAXPATHLEN + 1];
116 int fd;
117
118 (void)memset(buf, 'x', sizeof(buf));
119
120 pathl = getpath();
121 fd = open(path, O_RDWR | O_CREAT, 0600);
122
123 ATF_REQUIRE(fd >= 0);
124 ATF_REQUIRE(pathl != NULL);
125
126 errno = 0;
127 ATF_REQUIRE(link(path, pathl) == 0);
128 ATF_REQUIRE_ERRNO(EEXIST, link(path, pathl) == -1);
129
130 errno = 0;
131 ATF_REQUIRE_ERRNO(ENAMETOOLONG, link(buf, "xxx") == -1);
132
133 errno = 0;
134 ATF_REQUIRE_ERRNO(ENOENT, link(path, "/d/c/b/a") == -1);
135
136 errno = 0;
137 ATF_REQUIRE_ERRNO(ENOENT, link("/a/b/c/d", path) == -1);
138
139 errno = 0;
140 ATF_REQUIRE_ERRNO(ENOENT, link("/a/b/c/d", "/d/c/b/a") == -1);
141
142 errno = 0;
143 ATF_REQUIRE_ERRNO(EFAULT, link(path, (const char *)-1) == -1);
144
145 errno = 0;
146 ATF_REQUIRE_ERRNO(EFAULT, link((const char *)-1, "xxx") == -1);
147
148 ATF_REQUIRE(close(fd) == 0);
149 ATF_REQUIRE(unlink(path) == 0);
150 ATF_REQUIRE(unlink(pathl) == 0);
151}
152
153ATF_TC_CLEANUP(link_err, tc)
154{
155 (void)unlink(path);
156 (void)unlink(pathl);
157}
158
159ATF_TC(link_perm);
160ATF_TC_HEAD(link_perm, tc)
161{
162 atf_tc_set_md_var(tc, "descr", "Test permissions with link(2)");
163 atf_tc_set_md_var(tc, "require.user", "unprivileged");
164}
165
166ATF_TC_BODY(link_perm, tc)
167{
168 int rv;
169
170 errno = 0;
171 rv = link("/root", "/root.link");
172 ATF_REQUIRE_MSG(rv == -1 && (errno == EACCES || errno == EPERM),
173 "link to a directory did not fail with EPERM or EACCESS; link() "
174 "returned %d, errno %d", rv, errno);
175
176 errno = 0;
177 ATF_REQUIRE_ERRNO(EACCES,
178 link("/root/.profile", "/root/.profile.link") == -1);
179}
180
181ATF_TC_WITH_CLEANUP(link_stat);
182ATF_TC_HEAD(link_stat, tc)
183{
184 atf_tc_set_md_var(tc, "descr", "Check stat(2) of a linked file");
185}
186
187ATF_TC_BODY(link_stat, tc)
188{
189 struct stat sa, sb;
190 int fd;
191
192 (void)memset(&sa, 0, sizeof(struct stat));
193 (void)memset(&sb, 0, sizeof(struct stat));
194
195 pathl = getpath();
196 fd = open(path, O_RDWR | O_CREAT, 0600);
197
198 ATF_REQUIRE(fd >= 0);
199 ATF_REQUIRE(pathl != NULL);
200
201 ATF_REQUIRE(link(path, pathl) == 0);
202 ATF_REQUIRE(stat(path, &sa) == 0);
203 ATF_REQUIRE(lstat(pathl, &sb) == 0);
204
205 if (sa.st_uid != sb.st_uid)
206 atf_tc_fail("unequal UIDs");
207
208 if (sa.st_mode != sb.st_mode)
209 atf_tc_fail("unequal modes");
210
211 if (sa.st_ino != sb.st_ino)
212 atf_tc_fail("unequal inodes");
213
214 ATF_REQUIRE(close(fd) == 0);
215 ATF_REQUIRE(unlink(path) == 0);
216 ATF_REQUIRE(unlink(pathl) == 0);
217}
218
219ATF_TC_CLEANUP(link_stat, tc)
220{
221 (void)unlink(path);
222 (void)unlink(pathl);
223}
224
225ATF_TP_ADD_TCS(tp)
226{
227
228 ATF_TP_ADD_TC(tp, link_count);
229 ATF_TP_ADD_TC(tp, link_err);
230 ATF_TP_ADD_TC(tp, link_perm);
231 ATF_TP_ADD_TC(tp, link_stat);
232
233 return atf_no_error();
234}