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.c230
1 files changed, 0 insertions, 230 deletions
diff --git a/src/regress/lib/libc/sys/t_link.c b/src/regress/lib/libc/sys/t_link.c
deleted file mode 100644
index 12dbd8b595..0000000000
--- a/src/regress/lib/libc/sys/t_link.c
+++ /dev/null
@@ -1,230 +0,0 @@
1/* $OpenBSD: t_link.c,v 1.2 2021/12/13 16:56:48 deraadt 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/stat.h>
36
37#include "atf-c.h"
38#include <errno.h>
39#include <fcntl.h>
40#include <limits.h>
41#include <stdio.h>
42#include <string.h>
43#include <unistd.h>
44
45static const char *getpath(void);
46static char path[] = "link";
47static const char *pathl;
48
49static const char *
50getpath(void)
51{
52 static char buf[LINE_MAX];
53
54 (void)memset(buf, '\0', sizeof(buf));
55
56 if (getcwd(buf, sizeof(buf)) == NULL)
57 return NULL;
58
59 (void)strlcat(buf, path, sizeof(buf));
60 (void)strlcat(buf, ".link", sizeof(buf));
61
62 return buf;
63}
64
65ATF_TC_WITH_CLEANUP(link_count);
66ATF_TC_HEAD(link_count, tc)
67{
68 atf_tc_set_md_var(tc, "descr", "link(2) counts are incremented?");
69}
70
71ATF_TC_BODY(link_count, tc)
72{
73 struct stat sa, sb;
74 int fd;
75
76 (void)memset(&sa, 0, sizeof(struct stat));
77 (void)memset(&sb, 0, sizeof(struct stat));
78
79 pathl = getpath();
80 fd = open(path, O_RDWR | O_CREAT, 0600);
81
82 ATF_REQUIRE(fd >= 0);
83 ATF_REQUIRE(pathl != NULL);
84
85 ATF_REQUIRE(stat(path, &sa) == 0);
86 ATF_REQUIRE(link(path, pathl) == 0);
87 ATF_REQUIRE(stat(path, &sb) == 0);
88
89 if (sa.st_nlink != sb.st_nlink - 1)
90 atf_tc_fail("incorrect link(2) count");
91
92 ATF_REQUIRE(close(fd) == 0);
93 ATF_REQUIRE(unlink(path) == 0);
94 ATF_REQUIRE(unlink(pathl) == 0);
95}
96
97ATF_TC_CLEANUP(link_count, tc)
98{
99 (void)unlink(path);
100 (void)unlink(pathl);
101}
102
103ATF_TC_WITH_CLEANUP(link_err);
104ATF_TC_HEAD(link_err, tc)
105{
106 atf_tc_set_md_var(tc, "descr", "Test error conditions of link(2)");
107}
108
109ATF_TC_BODY(link_err, tc)
110{
111 char buf[PATH_MAX + 1];
112 int fd;
113
114 (void)memset(buf, 'x', sizeof(buf));
115
116 pathl = getpath();
117 fd = open(path, O_RDWR | O_CREAT, 0600);
118
119 ATF_REQUIRE(fd >= 0);
120 ATF_REQUIRE(pathl != NULL);
121
122 errno = 0;
123 ATF_REQUIRE(link(path, pathl) == 0);
124 ATF_REQUIRE_ERRNO(EEXIST, link(path, pathl) == -1);
125
126 errno = 0;
127 ATF_REQUIRE_ERRNO(ENAMETOOLONG, link(buf, "xxx") == -1);
128
129 errno = 0;
130 ATF_REQUIRE_ERRNO(ENOENT, link(path, "/d/c/b/a") == -1);
131
132 errno = 0;
133 ATF_REQUIRE_ERRNO(ENOENT, link("/a/b/c/d", path) == -1);
134
135 errno = 0;
136 ATF_REQUIRE_ERRNO(ENOENT, link("/a/b/c/d", "/d/c/b/a") == -1);
137
138 errno = 0;
139 ATF_REQUIRE_ERRNO(EFAULT, link(path, (const char *)-1) == -1);
140
141 errno = 0;
142 ATF_REQUIRE_ERRNO(EFAULT, link((const char *)-1, "xxx") == -1);
143
144 ATF_REQUIRE(close(fd) == 0);
145 ATF_REQUIRE(unlink(path) == 0);
146 ATF_REQUIRE(unlink(pathl) == 0);
147}
148
149ATF_TC_CLEANUP(link_err, tc)
150{
151 (void)unlink(path);
152 (void)unlink(pathl);
153}
154
155ATF_TC(link_perm);
156ATF_TC_HEAD(link_perm, tc)
157{
158 atf_tc_set_md_var(tc, "descr", "Test permissions with link(2)");
159 atf_tc_set_md_var(tc, "require.user", "unprivileged");
160}
161
162ATF_TC_BODY(link_perm, tc)
163{
164 int rv;
165
166 errno = 0;
167 rv = link("/root", "/root.link");
168 ATF_REQUIRE_MSG(rv == -1 && (errno == EACCES || errno == EPERM),
169 "link to a directory did not fail with EPERM or EACCESS; link() "
170 "returned %d, errno %d", rv, errno);
171
172 errno = 0;
173 ATF_REQUIRE_ERRNO(EACCES,
174 link("/root/.profile", "/root/.profile.link") == -1);
175}
176
177ATF_TC_WITH_CLEANUP(link_stat);
178ATF_TC_HEAD(link_stat, tc)
179{
180 atf_tc_set_md_var(tc, "descr", "Check stat(2) of a linked file");
181}
182
183ATF_TC_BODY(link_stat, tc)
184{
185 struct stat sa, sb;
186 int fd;
187
188 (void)memset(&sa, 0, sizeof(struct stat));
189 (void)memset(&sb, 0, sizeof(struct stat));
190
191 pathl = getpath();
192 fd = open(path, O_RDWR | O_CREAT, 0600);
193
194 ATF_REQUIRE(fd >= 0);
195 ATF_REQUIRE(pathl != NULL);
196
197 ATF_REQUIRE(link(path, pathl) == 0);
198 ATF_REQUIRE(stat(path, &sa) == 0);
199 ATF_REQUIRE(lstat(pathl, &sb) == 0);
200
201 if (sa.st_uid != sb.st_uid)
202 atf_tc_fail("unequal UIDs");
203
204 if (sa.st_mode != sb.st_mode)
205 atf_tc_fail("unequal modes");
206
207 if (sa.st_ino != sb.st_ino)
208 atf_tc_fail("unequal inodes");
209
210 ATF_REQUIRE(close(fd) == 0);
211 ATF_REQUIRE(unlink(path) == 0);
212 ATF_REQUIRE(unlink(pathl) == 0);
213}
214
215ATF_TC_CLEANUP(link_stat, tc)
216{
217 (void)unlink(path);
218 (void)unlink(pathl);
219}
220
221ATF_TP_ADD_TCS(tp)
222{
223
224 ATF_TP_ADD_TC(tp, link_count);
225 ATF_TP_ADD_TC(tp, link_err);
226 ATF_TP_ADD_TC(tp, link_perm);
227 ATF_TP_ADD_TC(tp, link_stat);
228
229 return atf_no_error();
230}