1314818Sngie/* $NetBSD: t_link.c,v 1.3 2017/01/13 20:42:36 christos Exp $ */ 2272343Sngie 3272343Sngie/*- 4272343Sngie * Copyright (c) 2011 The NetBSD Foundation, Inc. 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * This code is derived from software contributed to The NetBSD Foundation 8272343Sngie * by Jukka Ruohonen. 9272343Sngie * 10272343Sngie * Redistribution and use in source and binary forms, with or without 11272343Sngie * modification, are permitted provided that the following conditions 12272343Sngie * are met: 13272343Sngie * 1. Redistributions of source code must retain the above copyright 14272343Sngie * notice, this list of conditions and the following disclaimer. 15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 16272343Sngie * notice, this list of conditions and the following disclaimer in the 17272343Sngie * documentation and/or other materials provided with the distribution. 18272343Sngie * 19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22272343Sngie * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29272343Sngie * POSSIBILITY OF SUCH DAMAGE. 30272343Sngie */ 31272343Sngie#include <sys/cdefs.h> 32314818Sngie__RCSID("$NetBSD: t_link.c,v 1.3 2017/01/13 20:42:36 christos Exp $"); 33272343Sngie 34272343Sngie#include <sys/param.h> 35272343Sngie#include <sys/stat.h> 36272343Sngie 37272343Sngie#include <atf-c.h> 38272343Sngie#include <errno.h> 39272343Sngie#include <fcntl.h> 40314818Sngie#include <limits.h> 41272343Sngie#include <stdio.h> 42272343Sngie#include <string.h> 43272343Sngie#include <unistd.h> 44272343Sngie 45272343Sngiestatic const char *getpath(void); 46272343Sngiestatic char path[] = "link"; 47272343Sngiestatic const char *pathl; 48272343Sngie 49272343Sngiestatic const char * 50272343Sngiegetpath(void) 51272343Sngie{ 52272343Sngie static char buf[LINE_MAX]; 53272343Sngie 54272343Sngie (void)memset(buf, '\0', sizeof(buf)); 55272343Sngie 56272343Sngie if (getcwd(buf, sizeof(buf)) == NULL) 57272343Sngie return NULL; 58272343Sngie 59272343Sngie (void)strlcat(buf, path, sizeof(buf)); 60272343Sngie (void)strlcat(buf, ".link", sizeof(buf)); 61272343Sngie 62272343Sngie return buf; 63272343Sngie} 64272343Sngie 65272343SngieATF_TC_WITH_CLEANUP(link_count); 66272343SngieATF_TC_HEAD(link_count, tc) 67272343Sngie{ 68272343Sngie atf_tc_set_md_var(tc, "descr", "link(2) counts are incremented?"); 69272343Sngie} 70272343Sngie 71272343SngieATF_TC_BODY(link_count, tc) 72272343Sngie{ 73272343Sngie struct stat sa, sb; 74272343Sngie int fd; 75272343Sngie 76272343Sngie (void)memset(&sa, 0, sizeof(struct stat)); 77272343Sngie (void)memset(&sb, 0, sizeof(struct stat)); 78272343Sngie 79272343Sngie pathl = getpath(); 80272343Sngie fd = open(path, O_RDWR | O_CREAT, 0600); 81272343Sngie 82272343Sngie ATF_REQUIRE(fd >= 0); 83272343Sngie ATF_REQUIRE(pathl != NULL); 84272343Sngie 85272343Sngie ATF_REQUIRE(stat(path, &sa) == 0); 86272343Sngie ATF_REQUIRE(link(path, pathl) == 0); 87272343Sngie ATF_REQUIRE(stat(path, &sb) == 0); 88272343Sngie 89272343Sngie if (sa.st_nlink != sb.st_nlink - 1) 90272343Sngie atf_tc_fail("incorrect link(2) count"); 91272343Sngie 92272343Sngie ATF_REQUIRE(close(fd) == 0); 93272343Sngie ATF_REQUIRE(unlink(path) == 0); 94272343Sngie ATF_REQUIRE(unlink(pathl) == 0); 95272343Sngie} 96272343Sngie 97272343SngieATF_TC_CLEANUP(link_count, tc) 98272343Sngie{ 99272343Sngie (void)unlink(path); 100272343Sngie (void)unlink(pathl); 101272343Sngie} 102272343Sngie 103272343SngieATF_TC_WITH_CLEANUP(link_err); 104272343SngieATF_TC_HEAD(link_err, tc) 105272343Sngie{ 106272343Sngie atf_tc_set_md_var(tc, "descr", "Test error conditions of link(2)"); 107272343Sngie} 108272343Sngie 109272343SngieATF_TC_BODY(link_err, tc) 110272343Sngie{ 111272343Sngie char buf[MAXPATHLEN + 1]; 112272343Sngie int fd; 113272343Sngie 114272343Sngie (void)memset(buf, 'x', sizeof(buf)); 115272343Sngie 116272343Sngie pathl = getpath(); 117272343Sngie fd = open(path, O_RDWR | O_CREAT, 0600); 118272343Sngie 119272343Sngie ATF_REQUIRE(fd >= 0); 120272343Sngie ATF_REQUIRE(pathl != NULL); 121272343Sngie 122272343Sngie errno = 0; 123272343Sngie ATF_REQUIRE(link(path, pathl) == 0); 124272343Sngie ATF_REQUIRE_ERRNO(EEXIST, link(path, pathl) == -1); 125272343Sngie 126272343Sngie errno = 0; 127272343Sngie ATF_REQUIRE_ERRNO(ENAMETOOLONG, link(buf, "xxx") == -1); 128272343Sngie 129272343Sngie errno = 0; 130272343Sngie ATF_REQUIRE_ERRNO(ENOENT, link(path, "/d/c/b/a") == -1); 131272343Sngie 132272343Sngie errno = 0; 133272343Sngie ATF_REQUIRE_ERRNO(ENOENT, link("/a/b/c/d", path) == -1); 134272343Sngie 135272343Sngie errno = 0; 136272343Sngie ATF_REQUIRE_ERRNO(ENOENT, link("/a/b/c/d", "/d/c/b/a") == -1); 137272343Sngie 138272343Sngie errno = 0; 139272343Sngie ATF_REQUIRE_ERRNO(EFAULT, link(path, (const char *)-1) == -1); 140272343Sngie 141272343Sngie errno = 0; 142272343Sngie ATF_REQUIRE_ERRNO(EFAULT, link((const char *)-1, "xxx") == -1); 143272343Sngie 144272343Sngie ATF_REQUIRE(close(fd) == 0); 145272343Sngie ATF_REQUIRE(unlink(path) == 0); 146272343Sngie ATF_REQUIRE(unlink(pathl) == 0); 147272343Sngie} 148272343Sngie 149272343SngieATF_TC_CLEANUP(link_err, tc) 150272343Sngie{ 151272343Sngie (void)unlink(path); 152272343Sngie (void)unlink(pathl); 153272343Sngie} 154272343Sngie 155272343SngieATF_TC(link_perm); 156272343SngieATF_TC_HEAD(link_perm, tc) 157272343Sngie{ 158272343Sngie atf_tc_set_md_var(tc, "descr", "Test permissions with link(2)"); 159272343Sngie atf_tc_set_md_var(tc, "require.user", "unprivileged"); 160272343Sngie} 161272343Sngie 162272343SngieATF_TC_BODY(link_perm, tc) 163272343Sngie{ 164272343Sngie int rv; 165272343Sngie 166272343Sngie errno = 0; 167272343Sngie rv = link("/root", "/root.link"); 168272343Sngie ATF_REQUIRE_MSG(rv == -1 && (errno == EACCES || errno == EPERM), 169272343Sngie "link to a directory did not fail with EPERM or EACCESS; link() " 170272343Sngie "returned %d, errno %d", rv, errno); 171272343Sngie 172272343Sngie errno = 0; 173272343Sngie ATF_REQUIRE_ERRNO(EACCES, 174272343Sngie link("/root/.profile", "/root/.profile.link") == -1); 175272343Sngie} 176272343Sngie 177272343SngieATF_TC_WITH_CLEANUP(link_stat); 178272343SngieATF_TC_HEAD(link_stat, tc) 179272343Sngie{ 180272343Sngie atf_tc_set_md_var(tc, "descr", "Check stat(2) of a linked file"); 181272343Sngie} 182272343Sngie 183272343SngieATF_TC_BODY(link_stat, tc) 184272343Sngie{ 185272343Sngie struct stat sa, sb; 186272343Sngie int fd; 187272343Sngie 188272343Sngie (void)memset(&sa, 0, sizeof(struct stat)); 189272343Sngie (void)memset(&sb, 0, sizeof(struct stat)); 190272343Sngie 191272343Sngie pathl = getpath(); 192272343Sngie fd = open(path, O_RDWR | O_CREAT, 0600); 193272343Sngie 194272343Sngie ATF_REQUIRE(fd >= 0); 195272343Sngie ATF_REQUIRE(pathl != NULL); 196272343Sngie 197272343Sngie ATF_REQUIRE(link(path, pathl) == 0); 198272343Sngie ATF_REQUIRE(stat(path, &sa) == 0); 199272343Sngie ATF_REQUIRE(lstat(pathl, &sb) == 0); 200272343Sngie 201272343Sngie if (sa.st_uid != sb.st_uid) 202272343Sngie atf_tc_fail("unequal UIDs"); 203272343Sngie 204272343Sngie if (sa.st_mode != sb.st_mode) 205272343Sngie atf_tc_fail("unequal modes"); 206272343Sngie 207272343Sngie if (sa.st_ino != sb.st_ino) 208272343Sngie atf_tc_fail("unequal inodes"); 209272343Sngie 210272343Sngie ATF_REQUIRE(close(fd) == 0); 211272343Sngie ATF_REQUIRE(unlink(path) == 0); 212272343Sngie ATF_REQUIRE(unlink(pathl) == 0); 213272343Sngie} 214272343Sngie 215272343SngieATF_TC_CLEANUP(link_stat, tc) 216272343Sngie{ 217272343Sngie (void)unlink(path); 218272343Sngie (void)unlink(pathl); 219272343Sngie} 220272343Sngie 221272343SngieATF_TP_ADD_TCS(tp) 222272343Sngie{ 223272343Sngie 224272343Sngie ATF_TP_ADD_TC(tp, link_count); 225272343Sngie ATF_TP_ADD_TC(tp, link_err); 226272343Sngie ATF_TP_ADD_TC(tp, link_perm); 227272343Sngie ATF_TP_ADD_TC(tp, link_stat); 228272343Sngie 229272343Sngie return atf_no_error(); 230272343Sngie} 231