1/* $NetBSD: t_fchmodat.c,v 1.3 2017/01/10 15:13:56 christos Exp $ */ 2 3/*- 4 * Copyright (c) 2012 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Emmanuel Dreyfus. 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_fchmodat.c,v 1.3 2017/01/10 15:13:56 christos Exp $"); 33 34#include <sys/param.h> 35#include <sys/stat.h> 36#include <atf-c.h> 37#include <errno.h> 38#include <fcntl.h> 39#include <limits.h> 40#include <paths.h> 41#include <stdio.h> 42#include <string.h> 43#include <unistd.h> 44 45#define DIR "dir" 46#define FILE "dir/fchmodat" 47#define BASEFILE "fchmodat" 48#define LINK "dir/symlink" 49#define BASELINK "symlink" 50#define FILEERR "dir/fchmodaterr" 51 52ATF_TC(fchmodat_fd); 53ATF_TC_HEAD(fchmodat_fd, tc) 54{ 55 atf_tc_set_md_var(tc, "descr", "See that fchmodat works with fd"); 56} 57ATF_TC_BODY(fchmodat_fd, tc) 58{ 59 int dfd; 60 int fd; 61 struct stat st; 62 63 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 64 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 65 ATF_REQUIRE(close(fd) == 0); 66 67 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1); 68 ATF_REQUIRE(fchmodat(dfd, BASEFILE, 0600, 0) == 0); 69 ATF_REQUIRE(close(dfd) == 0); 70 71 ATF_REQUIRE(stat(FILE, &st) == 0); 72 ATF_REQUIRE(st.st_mode = 0600); 73} 74 75ATF_TC(fchmodat_fdcwd); 76ATF_TC_HEAD(fchmodat_fdcwd, tc) 77{ 78 atf_tc_set_md_var(tc, "descr", 79 "See that fchmodat works with fd as AT_FDCWD"); 80} 81ATF_TC_BODY(fchmodat_fdcwd, tc) 82{ 83 int fd; 84 struct stat st; 85 86 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 87 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 88 ATF_REQUIRE(close(fd) == 0); 89 90 ATF_REQUIRE(chdir(DIR) == 0); 91 ATF_REQUIRE(fchmodat(AT_FDCWD, BASEFILE, 0600, 0) == 0); 92 93 ATF_REQUIRE(stat(BASEFILE, &st) == 0); 94 ATF_REQUIRE(st.st_mode = 0600); 95} 96 97ATF_TC(fchmodat_fdcwderr); 98ATF_TC_HEAD(fchmodat_fdcwderr, tc) 99{ 100 atf_tc_set_md_var(tc, "descr", 101 "See that fchmodat fails with fd as AT_FDCWD and bad path"); 102} 103ATF_TC_BODY(fchmodat_fdcwderr, tc) 104{ 105 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 106 ATF_REQUIRE(fchmodat(AT_FDCWD, FILEERR, 0600, 0) == -1); 107} 108 109ATF_TC(fchmodat_fderr1); 110ATF_TC_HEAD(fchmodat_fderr1, tc) 111{ 112 atf_tc_set_md_var(tc, "descr", "See that fchmodat fail with bad path"); 113} 114ATF_TC_BODY(fchmodat_fderr1, tc) 115{ 116 int dfd; 117 118 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 119 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1); 120 ATF_REQUIRE(fchmodat(dfd, FILEERR, 0600, 0) == -1); 121 ATF_REQUIRE(close(dfd) == 0); 122} 123 124ATF_TC(fchmodat_fderr2); 125ATF_TC_HEAD(fchmodat_fderr2, tc) 126{ 127 atf_tc_set_md_var(tc, "descr", "See that fchmodat fails with bad fdat"); 128} 129ATF_TC_BODY(fchmodat_fderr2, tc) 130{ 131 int dfd; 132 int fd; 133 char cwd[MAXPATHLEN]; 134 135 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 136 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 137 ATF_REQUIRE(close(fd) == 0); 138 139 ATF_REQUIRE((dfd = open(getcwd(cwd, MAXPATHLEN), O_RDONLY, 0)) != -1); 140 ATF_REQUIRE(fchmodat(dfd, BASEFILE, 0600, 0) == -1); 141 ATF_REQUIRE(close(dfd) == 0); 142} 143 144ATF_TC(fchmodat_fderr3); 145ATF_TC_HEAD(fchmodat_fderr3, tc) 146{ 147 atf_tc_set_md_var(tc, "descr", "See that fchmodat fails with fd as -1"); 148} 149ATF_TC_BODY(fchmodat_fderr3, tc) 150{ 151 int fd; 152 153 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 154 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 155 ATF_REQUIRE(close(fd) == 0); 156 157 ATF_REQUIRE(fchmodat(-1, FILE, 0600, 0) == -1); 158} 159 160ATF_TC(fchmodat_fdlink); 161ATF_TC_HEAD(fchmodat_fdlink, tc) 162{ 163 atf_tc_set_md_var(tc, "descr", "See that fchmodat works on symlink"); 164} 165ATF_TC_BODY(fchmodat_fdlink, tc) 166{ 167 int dfdlink; 168 struct stat st; 169 170 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 171 ATF_REQUIRE(symlink(FILE, LINK) == 0); 172 173 ATF_REQUIRE((dfdlink = open(DIR, O_RDONLY, 0)) != -1); 174 175 ATF_REQUIRE(fchmodat(dfdlink, BASELINK, 0600, 0) == -1); 176 ATF_REQUIRE(errno = ENOENT); 177 178 ATF_REQUIRE(fchmodat(dfdlink, BASELINK, 0600, AT_SYMLINK_NOFOLLOW) == 0); 179 180 ATF_REQUIRE(close(dfdlink) == 0); 181 182 ATF_REQUIRE(lstat(LINK, &st) == 0); 183 ATF_REQUIRE(st.st_mode = 0600); 184} 185 186ATF_TP_ADD_TCS(tp) 187{ 188 189 ATF_TP_ADD_TC(tp, fchmodat_fd); 190 ATF_TP_ADD_TC(tp, fchmodat_fdcwd); 191 ATF_TP_ADD_TC(tp, fchmodat_fdcwderr); 192 ATF_TP_ADD_TC(tp, fchmodat_fderr1); 193 ATF_TP_ADD_TC(tp, fchmodat_fderr2); 194 ATF_TP_ADD_TC(tp, fchmodat_fderr3); 195 ATF_TP_ADD_TC(tp, fchmodat_fdlink); 196 197 return atf_no_error(); 198} 199