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