1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_link.c,v 1.2 2014/04/21 14:39:36 martin Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc * Copyright (c) 2011 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc * All rights reserved.
611be35a1SLionel Sambuc *
711be35a1SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
811be35a1SLionel Sambuc * by Jukka Ruohonen.
911be35a1SLionel Sambuc *
1011be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
1111be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
1211be35a1SLionel Sambuc * are met:
1311be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1411be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1511be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1611be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1711be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
1811be35a1SLionel Sambuc *
1911be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2011be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2111be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2211be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2311be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2411be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2511be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2611be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2711be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2811be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2911be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
3011be35a1SLionel Sambuc */
3111be35a1SLionel Sambuc #include <sys/cdefs.h>
32*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_link.c,v 1.2 2014/04/21 14:39:36 martin Exp $");
3311be35a1SLionel Sambuc
3411be35a1SLionel Sambuc #include <sys/param.h>
3511be35a1SLionel Sambuc #include <sys/stat.h>
3611be35a1SLionel Sambuc
3711be35a1SLionel Sambuc #include <atf-c.h>
3811be35a1SLionel Sambuc #include <errno.h>
3911be35a1SLionel Sambuc #include <fcntl.h>
4011be35a1SLionel Sambuc #include <stdio.h>
4111be35a1SLionel Sambuc #include <string.h>
4211be35a1SLionel Sambuc #include <unistd.h>
4311be35a1SLionel Sambuc
4411be35a1SLionel Sambuc static const char *getpath(void);
4511be35a1SLionel Sambuc static char path[] = "link";
4611be35a1SLionel Sambuc static const char *pathl;
4711be35a1SLionel Sambuc
4811be35a1SLionel Sambuc static const char *
getpath(void)4911be35a1SLionel Sambuc getpath(void)
5011be35a1SLionel Sambuc {
5111be35a1SLionel Sambuc static char buf[LINE_MAX];
5211be35a1SLionel Sambuc
5311be35a1SLionel Sambuc (void)memset(buf, '\0', sizeof(buf));
5411be35a1SLionel Sambuc
5511be35a1SLionel Sambuc if (getcwd(buf, sizeof(buf)) == NULL)
5611be35a1SLionel Sambuc return NULL;
5711be35a1SLionel Sambuc
5811be35a1SLionel Sambuc (void)strlcat(buf, path, sizeof(buf));
5911be35a1SLionel Sambuc (void)strlcat(buf, ".link", sizeof(buf));
6011be35a1SLionel Sambuc
6111be35a1SLionel Sambuc return buf;
6211be35a1SLionel Sambuc }
6311be35a1SLionel Sambuc
6411be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(link_count);
ATF_TC_HEAD(link_count,tc)6511be35a1SLionel Sambuc ATF_TC_HEAD(link_count, tc)
6611be35a1SLionel Sambuc {
6711be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "link(2) counts are incremented?");
6811be35a1SLionel Sambuc }
6911be35a1SLionel Sambuc
ATF_TC_BODY(link_count,tc)7011be35a1SLionel Sambuc ATF_TC_BODY(link_count, tc)
7111be35a1SLionel Sambuc {
7211be35a1SLionel Sambuc struct stat sa, sb;
7311be35a1SLionel Sambuc int fd;
7411be35a1SLionel Sambuc
7511be35a1SLionel Sambuc (void)memset(&sa, 0, sizeof(struct stat));
7611be35a1SLionel Sambuc (void)memset(&sb, 0, sizeof(struct stat));
7711be35a1SLionel Sambuc
7811be35a1SLionel Sambuc pathl = getpath();
7911be35a1SLionel Sambuc fd = open(path, O_RDWR | O_CREAT, 0600);
8011be35a1SLionel Sambuc
8111be35a1SLionel Sambuc ATF_REQUIRE(fd >= 0);
8211be35a1SLionel Sambuc ATF_REQUIRE(pathl != NULL);
8311be35a1SLionel Sambuc
8411be35a1SLionel Sambuc ATF_REQUIRE(stat(path, &sa) == 0);
8511be35a1SLionel Sambuc ATF_REQUIRE(link(path, pathl) == 0);
8611be35a1SLionel Sambuc ATF_REQUIRE(stat(path, &sb) == 0);
8711be35a1SLionel Sambuc
8811be35a1SLionel Sambuc if (sa.st_nlink != sb.st_nlink - 1)
8911be35a1SLionel Sambuc atf_tc_fail("incorrect link(2) count");
9011be35a1SLionel Sambuc
9111be35a1SLionel Sambuc ATF_REQUIRE(close(fd) == 0);
9211be35a1SLionel Sambuc ATF_REQUIRE(unlink(path) == 0);
9311be35a1SLionel Sambuc ATF_REQUIRE(unlink(pathl) == 0);
9411be35a1SLionel Sambuc }
9511be35a1SLionel Sambuc
ATF_TC_CLEANUP(link_count,tc)9611be35a1SLionel Sambuc ATF_TC_CLEANUP(link_count, tc)
9711be35a1SLionel Sambuc {
9811be35a1SLionel Sambuc (void)unlink(path);
9911be35a1SLionel Sambuc (void)unlink(pathl);
10011be35a1SLionel Sambuc }
10111be35a1SLionel Sambuc
10211be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(link_err);
ATF_TC_HEAD(link_err,tc)10311be35a1SLionel Sambuc ATF_TC_HEAD(link_err, tc)
10411be35a1SLionel Sambuc {
10511be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test error conditions of link(2)");
10611be35a1SLionel Sambuc }
10711be35a1SLionel Sambuc
ATF_TC_BODY(link_err,tc)10811be35a1SLionel Sambuc ATF_TC_BODY(link_err, tc)
10911be35a1SLionel Sambuc {
11011be35a1SLionel Sambuc char buf[MAXPATHLEN + 1];
11111be35a1SLionel Sambuc int fd;
11211be35a1SLionel Sambuc
11311be35a1SLionel Sambuc (void)memset(buf, 'x', sizeof(buf));
11411be35a1SLionel Sambuc
11511be35a1SLionel Sambuc pathl = getpath();
11611be35a1SLionel Sambuc fd = open(path, O_RDWR | O_CREAT, 0600);
11711be35a1SLionel Sambuc
11811be35a1SLionel Sambuc ATF_REQUIRE(fd >= 0);
11911be35a1SLionel Sambuc ATF_REQUIRE(pathl != NULL);
12011be35a1SLionel Sambuc
12111be35a1SLionel Sambuc errno = 0;
12211be35a1SLionel Sambuc ATF_REQUIRE(link(path, pathl) == 0);
12311be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EEXIST, link(path, pathl) == -1);
12411be35a1SLionel Sambuc
12511be35a1SLionel Sambuc errno = 0;
12611be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(ENAMETOOLONG, link(buf, "xxx") == -1);
12711be35a1SLionel Sambuc
12811be35a1SLionel Sambuc errno = 0;
12911be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(ENOENT, link(path, "/d/c/b/a") == -1);
13011be35a1SLionel Sambuc
13111be35a1SLionel Sambuc errno = 0;
13211be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(ENOENT, link("/a/b/c/d", path) == -1);
13311be35a1SLionel Sambuc
13411be35a1SLionel Sambuc errno = 0;
13511be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(ENOENT, link("/a/b/c/d", "/d/c/b/a") == -1);
13611be35a1SLionel Sambuc
13711be35a1SLionel Sambuc errno = 0;
13811be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EFAULT, link(path, (const char *)-1) == -1);
13911be35a1SLionel Sambuc
14011be35a1SLionel Sambuc errno = 0;
14111be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EFAULT, link((const char *)-1, "xxx") == -1);
14211be35a1SLionel Sambuc
14311be35a1SLionel Sambuc ATF_REQUIRE(close(fd) == 0);
14411be35a1SLionel Sambuc ATF_REQUIRE(unlink(path) == 0);
14511be35a1SLionel Sambuc ATF_REQUIRE(unlink(pathl) == 0);
14611be35a1SLionel Sambuc }
14711be35a1SLionel Sambuc
ATF_TC_CLEANUP(link_err,tc)14811be35a1SLionel Sambuc ATF_TC_CLEANUP(link_err, tc)
14911be35a1SLionel Sambuc {
15011be35a1SLionel Sambuc (void)unlink(path);
15111be35a1SLionel Sambuc (void)unlink(pathl);
15211be35a1SLionel Sambuc }
15311be35a1SLionel Sambuc
15411be35a1SLionel Sambuc ATF_TC(link_perm);
ATF_TC_HEAD(link_perm,tc)15511be35a1SLionel Sambuc ATF_TC_HEAD(link_perm, tc)
15611be35a1SLionel Sambuc {
15711be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test permissions with link(2)");
15811be35a1SLionel Sambuc atf_tc_set_md_var(tc, "require.user", "unprivileged");
15911be35a1SLionel Sambuc }
16011be35a1SLionel Sambuc
ATF_TC_BODY(link_perm,tc)16111be35a1SLionel Sambuc ATF_TC_BODY(link_perm, tc)
16211be35a1SLionel Sambuc {
163*0a6a1f1dSLionel Sambuc int rv;
16411be35a1SLionel Sambuc
16511be35a1SLionel Sambuc errno = 0;
166*0a6a1f1dSLionel Sambuc rv = link("/root", "/root.link");
167*0a6a1f1dSLionel Sambuc ATF_REQUIRE_MSG(rv == -1 && (errno == EACCES || errno == EPERM),
168*0a6a1f1dSLionel Sambuc "link to a directory did not fail with EPERM or EACCESS; link() "
169*0a6a1f1dSLionel Sambuc "returned %d, errno %d", rv, errno);
17011be35a1SLionel Sambuc
17111be35a1SLionel Sambuc errno = 0;
17211be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EACCES,
17311be35a1SLionel Sambuc link("/root/.profile", "/root/.profile.link") == -1);
17411be35a1SLionel Sambuc }
17511be35a1SLionel Sambuc
17611be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(link_stat);
ATF_TC_HEAD(link_stat,tc)17711be35a1SLionel Sambuc ATF_TC_HEAD(link_stat, tc)
17811be35a1SLionel Sambuc {
17911be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Check stat(2) of a linked file");
18011be35a1SLionel Sambuc }
18111be35a1SLionel Sambuc
ATF_TC_BODY(link_stat,tc)18211be35a1SLionel Sambuc ATF_TC_BODY(link_stat, tc)
18311be35a1SLionel Sambuc {
18411be35a1SLionel Sambuc struct stat sa, sb;
18511be35a1SLionel Sambuc int fd;
18611be35a1SLionel Sambuc
18711be35a1SLionel Sambuc (void)memset(&sa, 0, sizeof(struct stat));
18811be35a1SLionel Sambuc (void)memset(&sb, 0, sizeof(struct stat));
18911be35a1SLionel Sambuc
19011be35a1SLionel Sambuc pathl = getpath();
19111be35a1SLionel Sambuc fd = open(path, O_RDWR | O_CREAT, 0600);
19211be35a1SLionel Sambuc
19311be35a1SLionel Sambuc ATF_REQUIRE(fd >= 0);
19411be35a1SLionel Sambuc ATF_REQUIRE(pathl != NULL);
19511be35a1SLionel Sambuc
19611be35a1SLionel Sambuc ATF_REQUIRE(link(path, pathl) == 0);
19711be35a1SLionel Sambuc ATF_REQUIRE(stat(path, &sa) == 0);
19811be35a1SLionel Sambuc ATF_REQUIRE(lstat(pathl, &sb) == 0);
19911be35a1SLionel Sambuc
20011be35a1SLionel Sambuc if (sa.st_uid != sb.st_uid)
20111be35a1SLionel Sambuc atf_tc_fail("unequal UIDs");
20211be35a1SLionel Sambuc
20311be35a1SLionel Sambuc if (sa.st_mode != sb.st_mode)
20411be35a1SLionel Sambuc atf_tc_fail("unequal modes");
20511be35a1SLionel Sambuc
20611be35a1SLionel Sambuc if (sa.st_ino != sb.st_ino)
20711be35a1SLionel Sambuc atf_tc_fail("unequal inodes");
20811be35a1SLionel Sambuc
20911be35a1SLionel Sambuc ATF_REQUIRE(close(fd) == 0);
21011be35a1SLionel Sambuc ATF_REQUIRE(unlink(path) == 0);
21111be35a1SLionel Sambuc ATF_REQUIRE(unlink(pathl) == 0);
21211be35a1SLionel Sambuc }
21311be35a1SLionel Sambuc
ATF_TC_CLEANUP(link_stat,tc)21411be35a1SLionel Sambuc ATF_TC_CLEANUP(link_stat, tc)
21511be35a1SLionel Sambuc {
21611be35a1SLionel Sambuc (void)unlink(path);
21711be35a1SLionel Sambuc (void)unlink(pathl);
21811be35a1SLionel Sambuc }
21911be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)22011be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
22111be35a1SLionel Sambuc {
22211be35a1SLionel Sambuc
22311be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, link_count);
22411be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, link_err);
22511be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, link_perm);
22611be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, link_stat);
22711be35a1SLionel Sambuc
22811be35a1SLionel Sambuc return atf_no_error();
22911be35a1SLionel Sambuc }
230