1 /* $NetBSD: t_utimensat.c,v 1.5 2013/03/17 04:46:06 jmmv 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.5 2013/03/17 04:46:06 jmmv 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 #ifdef __FreeBSD__ 44 #include <sys/stat.h> 45 #endif 46 #include <sys/time.h> 47 48 #define DIR "dir" 49 #define FILE "dir/utimensat" 50 #define BASEFILE "utimensat" 51 #define LINK "dir/symlink" 52 #define BASELINK "symlink" 53 #define FILEERR "dir/symlink" 54 55 const struct timespec tptr[] = { 56 { 0x12345678, 987654321 }, 57 { 0x15263748, 123456789 }, 58 }; 59 60 ATF_TC(utimensat_fd); 61 ATF_TC_HEAD(utimensat_fd, tc) 62 { 63 atf_tc_set_md_var(tc, "descr", "See that utimensat works with fd"); 64 } 65 ATF_TC_BODY(utimensat_fd, tc) 66 { 67 int dfd; 68 int fd; 69 struct stat st; 70 71 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 72 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 73 ATF_REQUIRE(close(fd) == 0); 74 75 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1); 76 ATF_REQUIRE(utimensat(dfd, BASEFILE, tptr, 0) == 0); 77 ATF_REQUIRE(close(dfd) == 0); 78 79 ATF_REQUIRE(stat(FILE, &st) == 0); 80 ATF_REQUIRE(st.st_atimespec.tv_sec == tptr[0].tv_sec); 81 ATF_REQUIRE(st.st_atimespec.tv_nsec == tptr[0].tv_nsec); 82 ATF_REQUIRE(st.st_mtimespec.tv_sec == tptr[1].tv_sec); 83 ATF_REQUIRE(st.st_mtimespec.tv_nsec == tptr[1].tv_nsec); 84 } 85 86 ATF_TC(utimensat_fdcwd); 87 ATF_TC_HEAD(utimensat_fdcwd, tc) 88 { 89 atf_tc_set_md_var(tc, "descr", 90 "See that utimensat works with fd as AT_FDCWD"); 91 } 92 ATF_TC_BODY(utimensat_fdcwd, tc) 93 { 94 int fd; 95 struct stat st; 96 97 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 98 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 99 ATF_REQUIRE(close(fd) == 0); 100 101 ATF_REQUIRE(chdir(DIR) == 0); 102 ATF_REQUIRE(utimensat(AT_FDCWD, BASEFILE, tptr, 0) == 0); 103 104 ATF_REQUIRE(stat(BASEFILE, &st) == 0); 105 ATF_REQUIRE(st.st_atimespec.tv_sec == tptr[0].tv_sec); 106 ATF_REQUIRE(st.st_atimespec.tv_nsec == tptr[0].tv_nsec); 107 ATF_REQUIRE(st.st_mtimespec.tv_sec == tptr[1].tv_sec); 108 ATF_REQUIRE(st.st_mtimespec.tv_nsec == tptr[1].tv_nsec); 109 } 110 111 ATF_TC(utimensat_fdcwderr); 112 ATF_TC_HEAD(utimensat_fdcwderr, tc) 113 { 114 atf_tc_set_md_var(tc, "descr", 115 "See that utimensat fails with fd as AT_FDCWD and bad path"); 116 } 117 ATF_TC_BODY(utimensat_fdcwderr, tc) 118 { 119 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 120 ATF_REQUIRE(utimensat(AT_FDCWD, FILEERR, tptr, 0) == -1); 121 } 122 123 ATF_TC(utimensat_fderr1); 124 ATF_TC_HEAD(utimensat_fderr1, tc) 125 { 126 atf_tc_set_md_var(tc, "descr", "See that utimensat fail with bad path"); 127 } 128 ATF_TC_BODY(utimensat_fderr1, tc) 129 { 130 int dfd; 131 132 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 133 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1); 134 ATF_REQUIRE(utimensat(dfd, FILEERR, tptr, 0) == -1); 135 ATF_REQUIRE(close(dfd) == 0); 136 } 137 138 ATF_TC(utimensat_fderr2); 139 ATF_TC_HEAD(utimensat_fderr2, tc) 140 { 141 atf_tc_set_md_var(tc, "descr", "See that utimensat fails with bad fdat"); 142 } 143 ATF_TC_BODY(utimensat_fderr2, tc) 144 { 145 int dfd; 146 int fd; 147 char cwd[MAXPATHLEN]; 148 149 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 150 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 151 ATF_REQUIRE(close(fd) == 0); 152 153 ATF_REQUIRE((dfd = open(getcwd(cwd, MAXPATHLEN), O_RDONLY, 0)) != -1); 154 ATF_REQUIRE(utimensat(dfd, BASEFILE, tptr, 0) == -1); 155 ATF_REQUIRE(close(dfd) == 0); 156 } 157 158 ATF_TC(utimensat_fderr3); 159 ATF_TC_HEAD(utimensat_fderr3, tc) 160 { 161 atf_tc_set_md_var(tc, "descr", "See that utimensat fails with fd as -1"); 162 } 163 ATF_TC_BODY(utimensat_fderr3, tc) 164 { 165 int fd; 166 167 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 168 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 169 ATF_REQUIRE(close(fd) == 0); 170 171 ATF_REQUIRE(utimensat(-1, FILE, tptr, 0) == -1); 172 } 173 174 ATF_TC(utimensat_fdlink); 175 ATF_TC_HEAD(utimensat_fdlink, tc) 176 { 177 atf_tc_set_md_var(tc, "descr", "See that utimensat works on symlink"); 178 } 179 ATF_TC_BODY(utimensat_fdlink, tc) 180 { 181 int dfd; 182 struct stat st; 183 184 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 185 ATF_REQUIRE(symlink(FILE, LINK) == 0); /* NB: FILE does not exists */ 186 187 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1); 188 189 ATF_REQUIRE(utimensat(dfd, BASELINK, tptr, 0) == -1); 190 ATF_REQUIRE(errno = ENOENT); 191 192 ATF_REQUIRE(utimensat(dfd, BASELINK, tptr, AT_SYMLINK_NOFOLLOW) == 0); 193 194 ATF_REQUIRE(close(dfd) == 0); 195 196 ATF_REQUIRE(lstat(LINK, &st) == 0); 197 ATF_REQUIRE(st.st_atimespec.tv_sec == tptr[0].tv_sec); 198 ATF_REQUIRE(st.st_atimespec.tv_nsec == tptr[0].tv_nsec); 199 ATF_REQUIRE(st.st_mtimespec.tv_sec == tptr[1].tv_sec); 200 ATF_REQUIRE(st.st_mtimespec.tv_nsec == tptr[1].tv_nsec); 201 } 202 203 ATF_TP_ADD_TCS(tp) 204 { 205 206 ATF_TP_ADD_TC(tp, utimensat_fd); 207 ATF_TP_ADD_TC(tp, utimensat_fdcwd); 208 ATF_TP_ADD_TC(tp, utimensat_fdcwderr); 209 ATF_TP_ADD_TC(tp, utimensat_fderr1); 210 ATF_TP_ADD_TC(tp, utimensat_fderr2); 211 ATF_TP_ADD_TC(tp, utimensat_fderr3); 212 ATF_TP_ADD_TC(tp, utimensat_fdlink); 213 214 return atf_no_error(); 215 } 216