1 /* $NetBSD: t_utimensat.c,v 1.4 2012/11/22 14:59:59 martin 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_utimensat.c,v 1.4 2012/11/22 14:59:59 martin Exp $"); 33 34 #include <atf-c.h> 35 #include <errno.h> 36 #include <fcntl.h> 37 #include <limits.h> 38 #include <paths.h> 39 #include <stdio.h> 40 #include <string.h> 41 #include <unistd.h> 42 #include <sys/param.h> 43 #include <sys/time.h> 44 45 #define DIR "dir" 46 #define FILE "dir/utimensat" 47 #define BASEFILE "utimensat" 48 #define LINK "dir/symlink" 49 #define BASELINK "symlink" 50 #define FILEERR "dir/symlink" 51 52 const struct timespec tptr[] = { 53 { 0x12345678, 987654321 }, 54 { 0x15263748, 123456789 }, 55 }; 56 57 ATF_TC_WITH_CLEANUP(utimensat_fd); 58 ATF_TC_HEAD(utimensat_fd, tc) 59 { 60 atf_tc_set_md_var(tc, "descr", "See that utimensat works with fd"); 61 } 62 63 ATF_TC_BODY(utimensat_fd, tc) 64 { 65 int dfd; 66 int fd; 67 struct stat st; 68 69 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 70 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 71 ATF_REQUIRE(close(fd) == 0); 72 73 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1); 74 ATF_REQUIRE(utimensat(dfd, BASEFILE, tptr, 0) == 0); 75 ATF_REQUIRE(close(dfd) == 0); 76 77 ATF_REQUIRE(stat(FILE, &st) == 0); 78 ATF_REQUIRE(st.st_atimespec.tv_sec == tptr[0].tv_sec); 79 ATF_REQUIRE(st.st_atimespec.tv_nsec == tptr[0].tv_nsec); 80 ATF_REQUIRE(st.st_mtimespec.tv_sec == tptr[1].tv_sec); 81 ATF_REQUIRE(st.st_mtimespec.tv_nsec == tptr[1].tv_nsec); 82 } 83 84 ATF_TC_CLEANUP(utimensat_fd, tc) 85 { 86 (void)unlink(FILE); 87 (void)unlink(FILEERR); 88 (void)rmdir(DIR); 89 } 90 91 ATF_TC_WITH_CLEANUP(utimensat_fdcwd); 92 ATF_TC_HEAD(utimensat_fdcwd, tc) 93 { 94 atf_tc_set_md_var(tc, "descr", 95 "See that utimensat works with fd as AT_FDCWD"); 96 } 97 98 ATF_TC_BODY(utimensat_fdcwd, tc) 99 { 100 int fd; 101 struct stat st; 102 103 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 104 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 105 ATF_REQUIRE(close(fd) == 0); 106 107 ATF_REQUIRE(chdir(DIR) == 0); 108 ATF_REQUIRE(utimensat(AT_FDCWD, BASEFILE, tptr, 0) == 0); 109 110 ATF_REQUIRE(stat(BASEFILE, &st) == 0); 111 ATF_REQUIRE(st.st_atimespec.tv_sec == tptr[0].tv_sec); 112 ATF_REQUIRE(st.st_atimespec.tv_nsec == tptr[0].tv_nsec); 113 ATF_REQUIRE(st.st_mtimespec.tv_sec == tptr[1].tv_sec); 114 ATF_REQUIRE(st.st_mtimespec.tv_nsec == tptr[1].tv_nsec); 115 } 116 117 ATF_TC_CLEANUP(utimensat_fdcwd, tc) 118 { 119 (void)unlink(FILE); 120 (void)unlink(FILEERR); 121 (void)rmdir(DIR); 122 } 123 124 ATF_TC_WITH_CLEANUP(utimensat_fdcwderr); 125 ATF_TC_HEAD(utimensat_fdcwderr, tc) 126 { 127 atf_tc_set_md_var(tc, "descr", 128 "See that utimensat fails with fd as AT_FDCWD and bad path"); 129 } 130 131 ATF_TC_BODY(utimensat_fdcwderr, tc) 132 { 133 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 134 ATF_REQUIRE(utimensat(AT_FDCWD, FILEERR, tptr, 0) == -1); 135 } 136 137 ATF_TC_CLEANUP(utimensat_fdcwderr, tc) 138 { 139 (void)unlink(FILE); 140 (void)unlink(FILEERR); 141 (void)rmdir(DIR); 142 } 143 144 ATF_TC_WITH_CLEANUP(utimensat_fderr1); 145 ATF_TC_HEAD(utimensat_fderr1, tc) 146 { 147 atf_tc_set_md_var(tc, "descr", "See that utimensat fail with bad path"); 148 } 149 150 ATF_TC_BODY(utimensat_fderr1, tc) 151 { 152 int dfd; 153 154 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 155 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1); 156 ATF_REQUIRE(utimensat(dfd, FILEERR, tptr, 0) == -1); 157 ATF_REQUIRE(close(dfd) == 0); 158 } 159 160 ATF_TC_CLEANUP(utimensat_fderr1, tc) 161 { 162 (void)unlink(FILE); 163 (void)unlink(FILEERR); 164 (void)rmdir(DIR); 165 } 166 167 ATF_TC_WITH_CLEANUP(utimensat_fderr2); 168 ATF_TC_HEAD(utimensat_fderr2, tc) 169 { 170 atf_tc_set_md_var(tc, "descr", "See that utimensat fails with bad fdat"); 171 } 172 173 ATF_TC_BODY(utimensat_fderr2, tc) 174 { 175 int dfd; 176 int fd; 177 char cwd[MAXPATHLEN]; 178 179 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 180 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 181 ATF_REQUIRE(close(fd) == 0); 182 183 ATF_REQUIRE((dfd = open(getcwd(cwd, MAXPATHLEN), O_RDONLY, 0)) != -1); 184 ATF_REQUIRE(utimensat(dfd, BASEFILE, tptr, 0) == -1); 185 ATF_REQUIRE(close(dfd) == 0); 186 } 187 188 ATF_TC_CLEANUP(utimensat_fderr2, tc) 189 { 190 (void)unlink(FILE); 191 (void)unlink(FILEERR); 192 (void)rmdir(DIR); 193 } 194 195 ATF_TC_WITH_CLEANUP(utimensat_fderr3); 196 ATF_TC_HEAD(utimensat_fderr3, tc) 197 { 198 atf_tc_set_md_var(tc, "descr", "See that utimensat fails with fd as -1"); 199 } 200 201 ATF_TC_BODY(utimensat_fderr3, tc) 202 { 203 int fd; 204 205 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 206 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 207 ATF_REQUIRE(close(fd) == 0); 208 209 ATF_REQUIRE(utimensat(-1, FILE, tptr, 0) == -1); 210 } 211 212 ATF_TC_CLEANUP(utimensat_fderr3, tc) 213 { 214 (void)unlink(FILE); 215 (void)unlink(FILEERR); 216 (void)rmdir(DIR); 217 } 218 219 ATF_TC_WITH_CLEANUP(utimensat_fdlink); 220 ATF_TC_HEAD(utimensat_fdlink, tc) 221 { 222 atf_tc_set_md_var(tc, "descr", "See that utimensat works on symlink"); 223 } 224 225 ATF_TC_BODY(utimensat_fdlink, tc) 226 { 227 int dfd; 228 struct stat st; 229 230 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 231 ATF_REQUIRE(symlink(FILE, LINK) == 0); /* NB: FILE does not exists */ 232 233 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1); 234 235 ATF_REQUIRE(utimensat(dfd, BASELINK, tptr, 0) == -1); 236 ATF_REQUIRE(errno = ENOENT); 237 238 ATF_REQUIRE(utimensat(dfd, BASELINK, tptr, AT_SYMLINK_NOFOLLOW) == 0); 239 240 ATF_REQUIRE(close(dfd) == 0); 241 242 ATF_REQUIRE(lstat(LINK, &st) == 0); 243 ATF_REQUIRE(st.st_atimespec.tv_sec == tptr[0].tv_sec); 244 ATF_REQUIRE(st.st_atimespec.tv_nsec == tptr[0].tv_nsec); 245 ATF_REQUIRE(st.st_mtimespec.tv_sec == tptr[1].tv_sec); 246 ATF_REQUIRE(st.st_mtimespec.tv_nsec == tptr[1].tv_nsec); 247 } 248 249 ATF_TC_CLEANUP(utimensat_fdlink, tc) 250 { 251 (void)unlink(LINK); 252 (void)rmdir(DIR); 253 } 254 255 ATF_TP_ADD_TCS(tp) 256 { 257 258 ATF_TP_ADD_TC(tp, utimensat_fd); 259 ATF_TP_ADD_TC(tp, utimensat_fdcwd); 260 ATF_TP_ADD_TC(tp, utimensat_fdcwderr); 261 ATF_TP_ADD_TC(tp, utimensat_fderr1); 262 ATF_TP_ADD_TC(tp, utimensat_fderr2); 263 ATF_TP_ADD_TC(tp, utimensat_fderr3); 264 ATF_TP_ADD_TC(tp, utimensat_fdlink); 265 266 return atf_no_error(); 267 } 268