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